Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / surf / surf_routing_generic.cpp
1 /* Copyright (c) 2009-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/platf_interface.h"    // platform creation API internal interface
8
9 #include "surf_routing_generic.hpp"
10 #include "network_interface.hpp"
11 #include "xbt/graph.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
14
15 static int no_bypassroute_declared = 1;
16
17 void generic_free_route(sg_platf_route_cbarg_t route)
18 {
19   if (route) {
20     xbt_dynar_free(&route->link_list);
21     xbt_free(route);
22   }
23 }
24
25 void AsGeneric::parseRoute(sg_platf_route_cbarg_t /*route*/){
26   THROW_IMPOSSIBLE;
27 }
28
29 void AsGeneric::parseASroute(sg_platf_route_cbarg_t /*route*/){
30   THROW_IMPOSSIBLE;
31 }
32
33 void AsGeneric::getRouteAndLatency(RoutingEdgePtr /*src*/, RoutingEdgePtr /*dst*/, sg_platf_route_cbarg_t /*into*/, double */*latency*/){
34   THROW_IMPOSSIBLE;
35 }
36
37 AsGeneric::AsGeneric() {
38   p_bypassRoutes = xbt_dict_new_homogeneous((void (*)(void *)) generic_free_route);
39 }
40
41 AsGeneric::~AsGeneric() {
42   xbt_dict_free(&p_bypassRoutes);
43 }
44
45 int AsGeneric::parsePU(RoutingEdgePtr elm)
46 {
47   XBT_DEBUG("Load process unit \"%s\"", elm->getName());
48   xbt_dynar_push_as(p_indexNetworkElm, RoutingEdgePtr, elm);
49   return xbt_dynar_length(p_indexNetworkElm)-1;
50 }
51
52 int AsGeneric::parseAS(RoutingEdgePtr elm)
53 {
54   XBT_DEBUG("Load Autonomous system \"%s\"", elm->getName());
55   xbt_dynar_push_as(p_indexNetworkElm, RoutingEdgePtr, elm);
56   return xbt_dynar_length(p_indexNetworkElm)-1;
57 }
58
59 void AsGeneric::parseBypassroute(sg_platf_route_cbarg_t e_route)
60 {
61   char *src = (char*)(e_route->src);
62   char *dst = (char*)(e_route->dst);
63
64   if(e_route->gw_dst)
65     XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
66   else
67     XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
68   xbt_dict_t dict_bypassRoutes = p_bypassRoutes;
69   char *route_name;
70
71   route_name = bprintf("%s#%s", src, dst);
72   xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
73       "Invalid count of links, must be greater than zero (%s,%s)",
74       src, dst);
75   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
76       "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
77       src, e_route->gw_src->getName(), dst, e_route->gw_dst->getName());
78
79   sg_platf_route_cbarg_t new_e_route = NULL;
80   if(e_route->gw_dst)
81     new_e_route =  newExtendedRoute(SURF_ROUTING_RECURSIVE, e_route, 1);
82   else
83     new_e_route =  newExtendedRoute(SURF_ROUTING_BASE, e_route, 1);
84
85   xbt_dynar_free(&(e_route->link_list));
86
87   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
88   no_bypassroute_declared = 0;
89   xbt_free(route_name);
90 }
91
92 /* ************************************************************************** */
93 /* *********************** GENERIC BUSINESS METHODS ************************* */
94
95 xbt_dynar_t AsGeneric::getOneLinkRoutes() { // FIXME: kill that stub
96   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
97   return NULL;
98 }
99
100 static const char *instr_node_name(xbt_node_t node)
101 {
102   void *data = xbt_graph_node_get_data(node);
103   char *str = (char *) data;
104   return str;
105 }
106
107 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
108                               xbt_dict_t nodes)
109 {
110   xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
111   if (ret)
112     return ret;
113
114   ret = xbt_graph_new_node(graph, xbt_strdup(name));
115   xbt_dict_set(nodes, name, ret, NULL);
116   return ret;
117 }
118
119 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
120                               xbt_dict_t edges)
121 {
122   xbt_edge_t ret;
123
124   const char *sn = instr_node_name(s);
125   const char *dn = instr_node_name(d);
126   int len = strlen(sn) + strlen(dn) + 1;
127   char *name = (char *) xbt_malloc(len * sizeof(char));
128
129
130   snprintf(name, len, "%s%s", sn, dn);
131   ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
132   if (ret == NULL) {
133     snprintf(name, len, "%s%s", dn, sn);
134     ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
135   }
136
137   if (ret == NULL) {
138     ret = xbt_graph_new_edge(graph, s, d, NULL);
139     xbt_dict_set(edges, name, ret, NULL);
140   }
141   free(name);
142   return ret;
143 }
144
145 void AsGeneric::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
146 {
147   int src, dst;
148   int table_size = xbt_dynar_length(p_indexNetworkElm);
149
150
151   for (src = 0; src < table_size; src++) {
152     RoutingEdgePtr my_src =
153         xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdgePtr);
154     for (dst = 0; dst < table_size; dst++) {
155       if (src == dst)
156         continue;
157       RoutingEdgePtr my_dst =
158           xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdgePtr);
159
160       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
161       route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
162
163       getRouteAndLatency(my_src, my_dst, route, NULL);
164
165       XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->getName(), my_dst->getName());
166
167       unsigned int cpt;
168       void *link;
169
170       xbt_node_t current, previous;
171       const char *previous_name, *current_name;
172
173       if (route->gw_src) {
174         previous = new_xbt_graph_node(graph, route->gw_src->getName(), nodes);
175         previous_name = route->gw_src->getName();
176       } else {
177         previous = new_xbt_graph_node(graph, my_src->getName(), nodes);
178         previous_name = my_src->getName();
179       }
180
181       xbt_dynar_foreach(route->link_list, cpt, link) {
182         const char *link_name = ((ResourcePtr) link)->getName();
183         current = new_xbt_graph_node(graph, link_name, nodes);
184         current_name = link_name;
185         new_xbt_graph_edge(graph, previous, current, edges);
186         XBT_DEBUG ("  %s -> %s", previous_name, current_name);
187         previous = current;
188         previous_name = current_name;
189       }
190
191       if (route->gw_dst) {
192         current = new_xbt_graph_node(graph, route->gw_dst->getName(), nodes);
193         current_name = route->gw_dst->getName();
194       } else {
195         current = new_xbt_graph_node(graph, my_dst->getName(), nodes);
196         current_name = my_dst->getName();
197       }
198       new_xbt_graph_edge(graph, previous, current, edges);
199       XBT_DEBUG ("  %s -> %s", previous_name, current_name);
200
201       xbt_dynar_free (&(route->link_list));
202       xbt_free (route);
203     }
204   }
205 }
206
207 sg_platf_route_cbarg_t AsGeneric::getBypassRoute(RoutingEdgePtr src,
208                                                RoutingEdgePtr dst,
209                                                double *lat)
210 {
211   // If never set a bypass route return NULL without any further computations
212   XBT_DEBUG("generic_get_bypassroute from %s to %s", src->getName(), dst->getName());
213   if (no_bypassroute_declared)
214     return NULL;
215
216   sg_platf_route_cbarg_t e_route_bypass = NULL;
217   xbt_dict_t dict_bypassRoutes = p_bypassRoutes;
218
219   if(dst->getRcComponent() == this && src->getRcComponent() == this ){
220     char *route_name = bprintf("%s#%s", src->getName(), dst->getName());
221     e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
222     if(e_route_bypass)
223       XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
224     free(route_name);
225   }
226   else{
227     AsPtr src_as, dst_as;
228     int index_src, index_dst;
229     xbt_dynar_t path_src = NULL;
230     xbt_dynar_t path_dst = NULL;
231     AsPtr current = NULL;
232     AsPtr *current_src = NULL;
233     AsPtr *current_dst = NULL;
234
235     if (src == NULL || dst == NULL)
236       xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
237               src ? src->getName() : "(null)",
238               dst ? dst->getName() : "(null)", p_name);
239
240     src_as = src->getRcComponent();
241     dst_as = dst->getRcComponent();
242
243     /* (2) find the path to the root routing component */
244     path_src = xbt_dynar_new(sizeof(AsPtr), NULL);
245     current = src_as;
246     while (current != NULL) {
247       xbt_dynar_push(path_src, &current);
248       current = current->p_routingFather;
249     }
250     path_dst = xbt_dynar_new(sizeof(AsPtr), NULL);
251     current = dst_as;
252     while (current != NULL) {
253       xbt_dynar_push(path_dst, &current);
254       current = current->p_routingFather;
255     }
256
257     /* (3) find the common father */
258     index_src = path_src->used - 1;
259     index_dst = path_dst->used - 1;
260     current_src = (AsPtr *) xbt_dynar_get_ptr(path_src, index_src);
261     current_dst = (AsPtr *) xbt_dynar_get_ptr(path_dst, index_dst);
262     while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
263       xbt_dynar_pop_ptr(path_src);
264       xbt_dynar_pop_ptr(path_dst);
265       index_src--;
266       index_dst--;
267       current_src = (AsPtr *) xbt_dynar_get_ptr(path_src, index_src);
268       current_dst = (AsPtr *) xbt_dynar_get_ptr(path_dst, index_dst);
269     }
270
271     int max_index_src = path_src->used - 1;
272     int max_index_dst = path_dst->used - 1;
273
274     int max_index = max(max_index_src, max_index_dst);
275     int i, max;
276
277     for (max = 0; max <= max_index; max++) {
278       for (i = 0; i < max; i++) {
279         if (i <= max_index_src && max <= max_index_dst) {
280           char *route_name = bprintf("%s#%s",
281               (*(AsPtr *)
282                   (xbt_dynar_get_ptr(path_src, i)))->p_name,
283                   (*(AsPtr *)
284                       (xbt_dynar_get_ptr(path_dst, max)))->p_name);
285           e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
286           xbt_free(route_name);
287         }
288         if (e_route_bypass)
289           break;
290         if (max <= max_index_src && i <= max_index_dst) {
291           char *route_name = bprintf("%s#%s",
292               (*(AsPtr *)
293                   (xbt_dynar_get_ptr(path_src, max)))->p_name,
294                   (*(AsPtr *)
295                       (xbt_dynar_get_ptr(path_dst, i)))->p_name);
296           e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
297           xbt_free(route_name);
298         }
299         if (e_route_bypass)
300           break;
301       }
302
303       if (e_route_bypass)
304         break;
305
306       if (max <= max_index_src && max <= max_index_dst) {
307         char *route_name = bprintf("%s#%s",
308             (*(AsPtr *)
309                 (xbt_dynar_get_ptr(path_src, max)))->p_name,
310                 (*(AsPtr *)
311                     (xbt_dynar_get_ptr(path_dst, max)))->p_name);
312         e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
313         xbt_free(route_name);
314       }
315       if (e_route_bypass)
316         break;
317     }
318
319     xbt_dynar_free(&path_src);
320     xbt_dynar_free(&path_dst);
321   }
322
323   sg_platf_route_cbarg_t new_e_route = NULL;
324   if (e_route_bypass) {
325     NetworkLinkPtr link;
326     unsigned int cpt = 0;
327     new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
328     new_e_route->gw_src = e_route_bypass->gw_src;
329     new_e_route->gw_dst = e_route_bypass->gw_dst;
330     new_e_route->link_list =
331         xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
332     xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
333       xbt_dynar_push(new_e_route->link_list, &link);
334       if (lat)
335         *lat += link->getLatency();
336     }
337   }
338
339   return new_e_route;
340 }
341
342 /* ************************************************************************** */
343 /* ************************* GENERIC AUX FUNCTIONS ************************** */
344 /* change a route containing link names into a route containing link entities */
345 sg_platf_route_cbarg_t AsGeneric::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
346       sg_platf_route_cbarg_t routearg, int change_order) {
347
348   sg_platf_route_cbarg_t result;
349   char *link_name;
350   unsigned int cpt;
351
352   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
353   result->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
354
355   xbt_assert(hierarchy == SURF_ROUTING_BASE
356       || hierarchy == SURF_ROUTING_RECURSIVE,
357       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
358
359   if (hierarchy == SURF_ROUTING_RECURSIVE) {
360
361     xbt_assert(routearg->gw_src && routearg->gw_dst,
362         "NULL is obviously a bad gateway");
363
364     /* remeber not erase the gateway names */
365     result->gw_src = routearg->gw_src;
366     result->gw_dst = routearg->gw_dst;
367   }
368
369   xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
370
371     void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
372     if (link) {
373       if (change_order)
374         xbt_dynar_push(result->link_list, &link);
375       else
376         xbt_dynar_unshift(result->link_list, &link);
377     } else
378       THROWF(mismatch_error, 0, "Link %s not found", link_name);
379   }
380
381   return result;
382 }
383
384
385
386 AsPtr AsGeneric::asExist(AsPtr to_find)
387 {
388   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
389   xbt_dict_cursor_t cursor = NULL;
390   char *key;
391   int found = 0;
392   AsGenericPtr elem;
393   xbt_dict_foreach(p_routingSons, cursor, key, elem) {
394     if (to_find == elem || elem->asExist(to_find)) {
395       found = 1;
396       break;
397     }
398   }
399   if (found)
400     return to_find;
401   return NULL;
402 }
403
404 AsPtr AsGeneric::autonomousSystemExist(char *element)
405 {
406   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
407   AsPtr element_as, result, elem;
408   xbt_dict_cursor_t cursor = NULL;
409   char *key;
410   element_as = ((RoutingEdgePtr)
411       xbt_lib_get_or_null(as_router_lib, element,
412           ROUTING_ASR_LEVEL))->getRcComponent();
413   result = ((AsPtr) - 1);
414   if (element_as != this)
415     result = asExist(element_as);
416
417   int found = 0;
418   if (result) {
419     xbt_dict_foreach(element_as->p_routingSons, cursor, key, elem) {
420       found = !strcmp(elem->p_name, element);
421       if (found)
422         break;
423     }
424     if (found)
425       return element_as;
426   }
427   return NULL;
428 }
429
430 AsPtr AsGeneric::processingUnitsExist(char *element)
431 {
432   AsPtr element_as;
433   element_as = ((RoutingEdgePtr)
434       xbt_lib_get_or_null(host_lib,
435           element, ROUTING_HOST_LEVEL))->getRcComponent();
436   if (element_as == this)
437     return element_as;
438   return asExist(element_as);
439 }
440
441 void AsGeneric::srcDstCheck(RoutingEdgePtr src, RoutingEdgePtr dst)
442 {
443   if (src == NULL || dst == NULL)
444     xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
445             src ? src->getName() : "(null)",
446             dst ? dst->getName() : "(null)",
447             p_name);
448
449   AsPtr src_as = src->getRcComponent();
450   AsPtr dst_as = dst->getRcComponent();
451
452   if (src_as != dst_as)
453     xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
454         src->getName(), src_as->p_name,
455         dst->getName(), dst_as->p_name);
456
457   if (this != dst_as)
458     xbt_die
459     ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
460         src->getName(),
461         dst->getName(),
462         src_as->p_name,
463         dst_as->p_name,
464         p_name);
465 }