Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move one method higher in As hierarchy
[simgrid.git] / src / surf / surf_routing_generic.cpp
1 /* Copyright (c) 2009-2015. 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 <cstdlib>
8
9 #include <algorithm>
10
11 #include <xbt/dict.h>
12 #include <xbt/log.h>
13 #include <xbt/sysdep.h>
14 #include <xbt/dynar.h>
15 #include <xbt/graph.h>
16
17 #include "simgrid/platf_interface.h"    // platform creation API internal interface
18
19 #include "surf_routing_generic.hpp"
20 #include "surf_routing_private.hpp"
21 #include "network_interface.hpp"
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
24
25 static int no_bypassroute_declared = 1;
26
27 void routing_route_free(sg_platf_route_cbarg_t route)
28 {
29   if (route) {
30     xbt_dynar_free(&route->link_list);
31     xbt_free(route);
32   }
33 }
34
35 namespace simgrid {
36 namespace surf {
37   
38 AsGeneric::AsGeneric(const char*name)
39   : As(name)
40 {
41 }
42
43 AsGeneric::~AsGeneric()
44 {
45   xbt_dict_free(&bypassRoutes_);
46 }
47
48 void AsGeneric::parseBypassroute(sg_platf_route_cbarg_t e_route)
49 {
50   char *src = (char*)(e_route->src);
51   char *dst = (char*)(e_route->dst);
52
53   if(e_route->gw_dst)
54     XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
55   else
56     XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
57   xbt_dict_t dict_bypassRoutes = bypassRoutes_;
58   char *route_name;
59
60   route_name = bprintf("%s#%s", src, dst);
61   xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
62       "Invalid count of links, must be greater than zero (%s,%s)",
63       src, dst);
64   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
65       "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
66       src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
67
68   sg_platf_route_cbarg_t new_e_route = NULL;
69   if(e_route->gw_dst)
70     new_e_route =  newExtendedRoute(SURF_ROUTING_RECURSIVE, e_route, 1);
71   else
72     new_e_route =  newExtendedRoute(SURF_ROUTING_BASE, e_route, 1);
73
74   xbt_dynar_free(&(e_route->link_list));
75
76   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
77   no_bypassroute_declared = 0;
78   xbt_free(route_name);
79 }
80
81 }
82 }
83
84 /* ************************************************************************** */
85 /* *********************** GENERIC BUSINESS METHODS ************************* */
86
87 static const char *instr_node_name(xbt_node_t node)
88 {
89   void *data = xbt_graph_node_get_data(node);
90   char *str = (char *) data;
91   return str;
92 }
93
94 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
95                               xbt_dict_t nodes)
96 {
97   xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
98   if (ret)
99     return ret;
100
101   ret = xbt_graph_new_node(graph, xbt_strdup(name));
102   xbt_dict_set(nodes, name, ret, NULL);
103   return ret;
104 }
105
106 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
107                               xbt_dict_t edges)
108 {
109   xbt_edge_t ret;
110
111   const char *sn = instr_node_name(s);
112   const char *dn = instr_node_name(d);
113   int len = strlen(sn) + strlen(dn) + 1;
114   char *name = (char *) xbt_malloc(len * sizeof(char));
115
116
117   snprintf(name, len, "%s%s", sn, dn);
118   ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
119   if (ret == NULL) {
120     snprintf(name, len, "%s%s", dn, sn);
121     ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
122   }
123
124   if (ret == NULL) {
125     ret = xbt_graph_new_edge(graph, s, d, NULL);
126     xbt_dict_set(edges, name, ret, NULL);
127   }
128   free(name);
129   return ret;
130 }
131
132 namespace simgrid {
133 namespace surf {
134
135 void AsGeneric::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
136 {
137   int src, dst;
138   int table_size = xbt_dynar_length(vertices_);
139
140
141   for (src = 0; src < table_size; src++) {
142     NetCard *my_src =
143         xbt_dynar_get_as(vertices_, src, NetCard*);
144     for (dst = 0; dst < table_size; dst++) {
145       if (src == dst)
146         continue;
147       NetCard *my_dst =
148           xbt_dynar_get_as(vertices_, dst, NetCard*);
149
150       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
151       route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
152
153       getRouteAndLatency(my_src, my_dst, route, NULL);
154
155       XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
156
157       unsigned int cpt;
158       void *link;
159
160       xbt_node_t current, previous;
161       const char *previous_name, *current_name;
162
163       if (route->gw_src) {
164         previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
165         previous_name = route->gw_src->name();
166       } else {
167         previous = new_xbt_graph_node(graph, my_src->name(), nodes);
168         previous_name = my_src->name();
169       }
170
171       xbt_dynar_foreach(route->link_list, cpt, link) {
172         const char *link_name = static_cast<simgrid::surf::Resource*>(
173           link)->getName();
174         current = new_xbt_graph_node(graph, link_name, nodes);
175         current_name = link_name;
176         new_xbt_graph_edge(graph, previous, current, edges);
177         XBT_DEBUG ("  %s -> %s", previous_name, current_name);
178         previous = current;
179         previous_name = current_name;
180       }
181
182       if (route->gw_dst) {
183         current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
184         current_name = route->gw_dst->name();
185       } else {
186         current = new_xbt_graph_node(graph, my_dst->name(), nodes);
187         current_name = my_dst->name();
188       }
189       new_xbt_graph_edge(graph, previous, current, edges);
190       XBT_DEBUG ("  %s -> %s", previous_name, current_name);
191
192       xbt_dynar_free (&(route->link_list));
193       xbt_free (route);
194     }
195   }
196 }
197
198 sg_platf_route_cbarg_t AsGeneric::getBypassRoute(NetCard *src,
199                                                NetCard *dst,
200                                                double *lat)
201 {
202   // If never set a bypass route return NULL without any further computations
203   XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
204   if (no_bypassroute_declared)
205     return NULL;
206
207   sg_platf_route_cbarg_t e_route_bypass = NULL;
208   xbt_dict_t dict_bypassRoutes = bypassRoutes_;
209
210   if(dst->containingAS() == this && src->containingAS() == this ){
211     char *route_name = bprintf("%s#%s", src->name(), dst->name());
212     e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
213     if(e_route_bypass)
214       XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
215     free(route_name);
216   }
217   else{
218     As *src_as, *dst_as;
219     int index_src, index_dst;
220     xbt_dynar_t path_src = NULL;
221     xbt_dynar_t path_dst = NULL;
222     As *current = NULL;
223     As **current_src = NULL;
224     As **current_dst = NULL;
225
226     if (src == NULL || dst == NULL)
227       xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
228               src ? src->name() : "(null)",
229               dst ? dst->name() : "(null)", name_);
230
231     src_as = src->containingAS();
232     dst_as = dst->containingAS();
233
234     /* (2) find the path to the root routing component */
235     path_src = xbt_dynar_new(sizeof(As*), NULL);
236     current = src_as;
237     while (current != NULL) {
238       xbt_dynar_push(path_src, &current);
239       current = current->father_;
240     }
241     path_dst = xbt_dynar_new(sizeof(As*), NULL);
242     current = dst_as;
243     while (current != NULL) {
244       xbt_dynar_push(path_dst, &current);
245       current = current->father_;
246     }
247
248     /* (3) find the common father */
249     index_src = path_src->used - 1;
250     index_dst = path_dst->used - 1;
251     current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
252     current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
253     while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
254       xbt_dynar_pop_ptr(path_src);
255       xbt_dynar_pop_ptr(path_dst);
256       index_src--;
257       index_dst--;
258       current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
259       current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
260     }
261
262     int max_index_src = path_src->used - 1;
263     int max_index_dst = path_dst->used - 1;
264
265     int max_index = std::max(max_index_src, max_index_dst);
266     int i, max;
267
268     for (max = 0; max <= max_index; max++) {
269       for (i = 0; i < max; i++) {
270         if (i <= max_index_src && max <= max_index_dst) {
271           char *route_name = bprintf("%s#%s",
272               (*(As **)
273                   (xbt_dynar_get_ptr(path_src, i)))->name_,
274                   (*(As **)
275                       (xbt_dynar_get_ptr(path_dst, max)))->name_);
276           e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
277           xbt_free(route_name);
278         }
279         if (e_route_bypass)
280           break;
281         if (max <= max_index_src && i <= max_index_dst) {
282           char *route_name = bprintf("%s#%s",
283               (*(As **)
284                   (xbt_dynar_get_ptr(path_src, max)))->name_,
285                   (*(As **)
286                       (xbt_dynar_get_ptr(path_dst, i)))->name_);
287           e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
288           xbt_free(route_name);
289         }
290         if (e_route_bypass)
291           break;
292       }
293
294       if (e_route_bypass)
295         break;
296
297       if (max <= max_index_src && max <= max_index_dst) {
298         char *route_name = bprintf("%s#%s",
299             (*(As **)
300                 (xbt_dynar_get_ptr(path_src, max)))->name_,
301                 (*(As **)
302                     (xbt_dynar_get_ptr(path_dst, max)))->name_);
303         e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
304         xbt_free(route_name);
305       }
306       if (e_route_bypass)
307         break;
308     }
309
310     xbt_dynar_free(&path_src);
311     xbt_dynar_free(&path_dst);
312   }
313
314   sg_platf_route_cbarg_t new_e_route = NULL;
315   if (e_route_bypass) {
316     Link* link;
317     unsigned int cpt = 0;
318     new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
319     new_e_route->gw_src = e_route_bypass->gw_src;
320     new_e_route->gw_dst = e_route_bypass->gw_dst;
321     new_e_route->link_list =
322         xbt_dynar_new(sizeof(Link*), NULL);
323     xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
324       xbt_dynar_push(new_e_route->link_list, &link);
325       if (lat)
326         *lat += link->getLatency();
327     }
328   }
329
330   return new_e_route;
331 }
332
333 /* ************************************************************************** */
334 /* ************************* GENERIC AUX FUNCTIONS ************************** */
335 /* change a route containing link names into a route containing link entities */
336 sg_platf_route_cbarg_t AsGeneric::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
337       sg_platf_route_cbarg_t routearg, int change_order) {
338
339   sg_platf_route_cbarg_t result;
340   char *link_name;
341   unsigned int cpt;
342
343   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
344   result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
345
346   xbt_assert(hierarchy == SURF_ROUTING_BASE
347       || hierarchy == SURF_ROUTING_RECURSIVE,
348       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
349
350   if (hierarchy == SURF_ROUTING_RECURSIVE) {
351
352     xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
353
354     /* remember not erase the gateway names */
355     result->gw_src = routearg->gw_src;
356     result->gw_dst = routearg->gw_dst;
357   }
358
359   xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
360
361     Link *link = Link::byName(link_name);
362     if (link) {
363       if (change_order)
364         xbt_dynar_push(result->link_list, &link);
365       else
366         xbt_dynar_unshift(result->link_list, &link);
367     } else
368       THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
369   }
370
371   return result;
372 }
373
374 void AsGeneric::getRouteCheckParams(NetCard *src, NetCard *dst)
375 {
376   xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
377   xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
378
379   As *src_as = src->containingAS();
380   As *dst_as = dst->containingAS();
381
382   xbt_assert(src_as == dst_as, "Internal error: %s@%s and %s@%s are not in the same AS as expected. Please report that bug.",
383         src->name(), src_as->name_, dst->name(), dst_as->name_);
384
385   xbt_assert(this == dst_as,
386       "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
387         src->name(), dst->name(),  src_as->name_, dst_as->name_,  name_);
388 }
389 void AsGeneric::addRouteCheckParams(sg_platf_route_cbarg_t route) {
390   const char *srcName = route->src;
391   const char *dstName = route->dst;
392   NetCard *src = sg_netcard_by_name_or_null(srcName);
393   NetCard *dst = sg_netcard_by_name_or_null(dstName);
394
395   if(!route->gw_dst && !route->gw_src) {
396     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
397     xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
398     xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
399     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
400     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
401         "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
402     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
403         "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
404   } else {
405     XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
406     xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
407         "When defining an ASroute, src must be an AS but '%s' is not", srcName);
408     xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
409         "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
410
411     xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
412         "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
413     xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
414         "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
415
416     xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
417
418     xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
419         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
420     xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
421         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
422     xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
423         srcName,route->gw_src->name(), dstName,route->gw_dst->name());
424   }
425 }
426
427 }
428 }