Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not search the cluster backbones again and again, store them in the right location
[simgrid.git] / src / surf / surf_routing_generic.c
1 /* Copyright (c) 2009, 2010, 2011. 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 <pcre.h>               /* regular expression library */
8
9 #include "simgrid/platf_interface.h"    // platform creation API internal interface
10
11 #include "surf_routing_private.h"
12 #include "surf/surf_routing.h"
13 #include "surf/surfxml_parse_values.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
16
17 AS_t model_generic_create_sized(size_t childsize) {
18   AS_t new_component = model_none_create_sized(childsize);
19
20   new_component->parse_PU = generic_parse_PU;
21   new_component->parse_AS = generic_parse_AS;
22   new_component->parse_route = NULL;
23   new_component->parse_ASroute = NULL;
24   new_component->parse_bypassroute = generic_parse_bypassroute;
25   new_component->get_route = NULL;
26   new_component->get_latency = generic_get_link_latency;
27   new_component->get_onelink_routes = NULL;
28   new_component->get_bypass_route =
29       generic_get_bypassroute;
30   new_component->finalize = model_generic_finalize;
31
32   new_component->to_index = xbt_dict_new();
33   new_component->bypassRoutes = xbt_dict_new();
34
35   return new_component;
36 }
37 void model_generic_finalize(AS_t as) {
38   xbt_dict_free(&as->to_index);
39   xbt_dict_free(&as->bypassRoutes);
40   model_none_finalize(as);
41 }
42
43 void generic_parse_PU(AS_t as, const char *name)
44 {
45   XBT_DEBUG("Load process unit \"%s\"", name);
46   int *id = xbt_new0(int, 1);
47   xbt_dict_t _to_index;
48   _to_index = as->to_index;
49   *id = xbt_dict_length(_to_index);
50   xbt_dict_set(_to_index, name, id, xbt_free);
51 }
52
53 void generic_parse_AS(AS_t as, const char *name)
54 {
55   XBT_DEBUG("Load Autonomous system \"%s\"", name);
56   int *id = xbt_new0(int, 1);
57   xbt_dict_t _to_index;
58   _to_index = as->to_index;
59   *id = xbt_dict_length(_to_index);
60   xbt_dict_set(_to_index, name, id, xbt_free);
61 }
62
63 void generic_parse_bypassroute(AS_t rc,
64                              const char *src, const char *dst,
65                              route_extended_t e_route)
66 {
67   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
68   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
69   char *route_name;
70
71   route_name = bprintf("%s#%s", src, dst);
72   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
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->src_gateway, dst, e_route->dst_gateway);
78
79   route_extended_t new_e_route =
80       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
81   xbt_dynar_free(&(e_route->generic_route.link_list));
82   xbt_free(e_route);
83
84   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
85                (void (*)(void *)) generic_free_extended_route);
86   xbt_free(route_name);
87 }
88
89 /* ************************************************************************** */
90 /* *********************** GENERIC BUSINESS METHODS ************************* */
91
92 double generic_get_link_latency(AS_t rc,
93                                 const char *src, const char *dst,
94                                 route_extended_t route)
95 {
96   int need_to_clean = route ? 0 : 1;
97   void *link;
98   unsigned int i;
99   double latency = 0.0;
100
101   route = route ? route : rc->get_route(rc, src, dst);
102
103   xbt_dynar_foreach(route->generic_route.link_list, i, link) {
104     latency += surf_network_model->extension.network.get_link_latency(link);
105   }
106   if (need_to_clean)
107     generic_free_extended_route(route);
108   return latency;
109 }
110
111 xbt_dynar_t generic_get_onelink_routes(AS_t rc)
112 {
113   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
114 }
115
116 route_extended_t generic_get_bypassroute(AS_t rc,
117                                          const char *src, const char *dst)
118 {
119   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
120   AS_t src_as, dst_as;
121   int index_src, index_dst;
122   xbt_dynar_t path_src = NULL;
123   xbt_dynar_t path_dst = NULL;
124   AS_t current = NULL;
125   AS_t *current_src = NULL;
126   AS_t *current_dst = NULL;
127
128   /* (1) find the as where the src and dst are located */
129   void *src_data = xbt_lib_get_or_null(host_lib, src, ROUTING_HOST_LEVEL);
130   void *dst_data = xbt_lib_get_or_null(host_lib, dst, ROUTING_HOST_LEVEL);
131   if (!src_data)
132     src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
133   if (!dst_data)
134     dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
135
136   if (src_data == NULL || dst_data == NULL)
137     xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
138             src, dst, rc->name);
139
140   src_as = ((network_element_info_t) src_data)->rc_component;
141   dst_as = ((network_element_info_t) dst_data)->rc_component;
142
143   /* (2) find the path to the root routing component */
144   path_src = xbt_dynar_new(sizeof(AS_t), NULL);
145   current = src_as;
146   while (current != NULL) {
147     xbt_dynar_push(path_src, &current);
148     current = current->routing_father;
149   }
150   path_dst = xbt_dynar_new(sizeof(AS_t), NULL);
151   current = dst_as;
152   while (current != NULL) {
153     xbt_dynar_push(path_dst, &current);
154     current = current->routing_father;
155   }
156
157   /* (3) find the common father */
158   index_src = path_src->used - 1;
159   index_dst = path_dst->used - 1;
160   current_src = xbt_dynar_get_ptr(path_src, index_src);
161   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
162   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
163     xbt_dynar_pop_ptr(path_src);
164     xbt_dynar_pop_ptr(path_dst);
165     index_src--;
166     index_dst--;
167     current_src = xbt_dynar_get_ptr(path_src, index_src);
168     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
169   }
170
171   int max_index_src = path_src->used - 1;
172   int max_index_dst = path_dst->used - 1;
173
174   int max_index = max(max_index_src, max_index_dst);
175   int i, max;
176
177   route_extended_t e_route_bypass = NULL;
178
179   for (max = 0; max <= max_index; max++) {
180     for (i = 0; i < max; i++) {
181       if (i <= max_index_src && max <= max_index_dst) {
182         char *route_name = bprintf("%s#%s",
183                                    (*(AS_t *)
184                                     (xbt_dynar_get_ptr(path_src, i)))->name,
185                                    (*(AS_t *)
186                                     (xbt_dynar_get_ptr(path_dst, max)))->name);
187         e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
188         xbt_free(route_name);
189       }
190       if (e_route_bypass)
191         break;
192       if (max <= max_index_src && i <= max_index_dst) {
193         char *route_name = bprintf("%s#%s",
194                                    (*(AS_t *)
195                                     (xbt_dynar_get_ptr(path_src, max)))->name,
196                                    (*(AS_t *)
197                                     (xbt_dynar_get_ptr(path_dst, i)))->name);
198         e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
199         xbt_free(route_name);
200       }
201       if (e_route_bypass)
202         break;
203     }
204
205     if (e_route_bypass)
206       break;
207
208     if (max <= max_index_src && max <= max_index_dst) {
209       char *route_name = bprintf("%s#%s",
210                                  (*(AS_t *)
211                                   (xbt_dynar_get_ptr(path_src, max)))->name,
212                                  (*(AS_t *)
213                                   (xbt_dynar_get_ptr(path_dst, max)))->name);
214       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
215       xbt_free(route_name);
216     }
217     if (e_route_bypass)
218       break;
219   }
220
221   xbt_dynar_free(&path_src);
222   xbt_dynar_free(&path_dst);
223
224   route_extended_t new_e_route = NULL;
225
226   if (e_route_bypass) {
227     void *link;
228     unsigned int cpt = 0;
229     new_e_route = xbt_new0(s_route_extended_t, 1);
230     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
231     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
232     new_e_route->generic_route.link_list =
233         xbt_dynar_new(global_routing->size_of_link, NULL);
234     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
235       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
236     }
237   }
238
239   return new_e_route;
240 }
241
242 /* ************************************************************************** */
243 /* ************************* GENERIC AUX FUNCTIONS ************************** */
244
245 route_t
246 generic_new_route(e_surf_routing_hierarchy_t hierarchy, void *data, int order)
247 {
248
249   char *link_name;
250   route_t new_route;
251   unsigned int cpt;
252   xbt_dynar_t links = NULL, links_id = NULL;
253
254   new_route = xbt_new0(s_route_t, 1);
255   new_route->link_list = xbt_dynar_new(global_routing->size_of_link, NULL);
256
257   xbt_assert(hierarchy == SURF_ROUTING_BASE,
258              "the hierarchy type is not SURF_ROUTING_BASE");
259
260   links = ((route_t) data)->link_list;
261
262
263   links_id = new_route->link_list;
264
265   xbt_dynar_foreach(links, cpt, link_name) {
266
267     void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
268     if (link) {
269       if (order)
270         xbt_dynar_push(links_id, &link);
271       else
272         xbt_dynar_unshift(links_id, &link);
273     } else
274       THROWF(mismatch_error, 0, "Link %s not found", link_name);
275   }
276
277   return new_route;
278 }
279
280 route_extended_t
281 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
282                            void *data, int order)
283 {
284
285   char *link_name;
286   route_extended_t e_route, new_e_route;
287   route_t route;
288   unsigned int cpt;
289   xbt_dynar_t links = NULL, links_id = NULL;
290
291   new_e_route = xbt_new0(s_route_extended_t, 1);
292   new_e_route->generic_route.link_list =
293       xbt_dynar_new(global_routing->size_of_link, NULL);
294   new_e_route->src_gateway = NULL;
295   new_e_route->dst_gateway = NULL;
296
297   xbt_assert(hierarchy == SURF_ROUTING_BASE
298              || hierarchy == SURF_ROUTING_RECURSIVE,
299              "the hierarchy type is not defined");
300
301   if (hierarchy == SURF_ROUTING_BASE) {
302
303     route = (route_t) data;
304     links = route->link_list;
305
306   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
307
308     e_route = (route_extended_t) data;
309     xbt_assert(e_route->src_gateway
310                && e_route->dst_gateway, "bad gateway, is null");
311     links = e_route->generic_route.link_list;
312
313     /* remeber not erase the gateway names */
314     new_e_route->src_gateway = strdup(e_route->src_gateway);
315     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
316   }
317
318   links_id = new_e_route->generic_route.link_list;
319
320   xbt_dynar_foreach(links, cpt, link_name) {
321
322     void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
323     if (link) {
324       if (order)
325         xbt_dynar_push(links_id, &link);
326       else
327         xbt_dynar_unshift(links_id, &link);
328     } else
329       THROWF(mismatch_error, 0, "Link %s not found", link_name);
330   }
331
332   return new_e_route;
333 }
334
335 void generic_free_route(route_t route)
336 {
337   if (route) {
338     xbt_dynar_free(&(route->link_list));
339     xbt_free(route);
340   }
341 }
342
343 void generic_free_extended_route(route_extended_t e_route)
344 {
345   if (e_route) {
346     xbt_dynar_free(&(e_route->generic_route.link_list));
347     xbt_free(e_route->src_gateway);
348     xbt_free(e_route->dst_gateway);
349     xbt_free(e_route);
350   }
351 }
352
353 static AS_t generic_as_exist(AS_t find_from,
354                                             AS_t to_find)
355 {
356   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
357   xbt_dict_cursor_t cursor = NULL;
358   char *key;
359   int found = 0;
360   AS_t elem;
361   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
362     if (to_find == elem || generic_as_exist(elem, to_find)) {
363       found = 1;
364       break;
365     }
366   }
367   if (found)
368     return to_find;
369   return NULL;
370 }
371
372 AS_t
373 generic_autonomous_system_exist(AS_t rc, char *element)
374 {
375   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
376   AS_t element_as, result, elem;
377   xbt_dict_cursor_t cursor = NULL;
378   char *key;
379   element_as = ((network_element_info_t)
380                 xbt_lib_get_or_null(as_router_lib, element,
381                                     ROUTING_ASR_LEVEL))->rc_component;
382   result = ((AS_t) - 1);
383   if (element_as != rc)
384     result = generic_as_exist(rc, element_as);
385
386   int found = 0;
387   if (result) {
388     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
389       found = !strcmp(elem->name, element);
390       if (found)
391         break;
392     }
393     if (found)
394       return element_as;
395   }
396   return NULL;
397 }
398
399 AS_t
400 generic_processing_units_exist(AS_t rc, char *element)
401 {
402   AS_t element_as;
403   element_as = ((network_element_info_t)
404                 xbt_lib_get_or_null(host_lib,
405                                     element, ROUTING_HOST_LEVEL))->rc_component;
406   if (element_as == rc)
407     return element_as;
408   return generic_as_exist(rc, element_as);
409 }
410
411 void generic_src_dst_check(AS_t rc, const char *src,
412                            const char *dst)
413 {
414
415   void *src_data = xbt_lib_get_or_null(host_lib, src, ROUTING_HOST_LEVEL);
416   void *dst_data = xbt_lib_get_or_null(host_lib, dst, ROUTING_HOST_LEVEL);
417   if (!src_data)
418     src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
419   if (!dst_data)
420     dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
421
422   if (src_data == NULL || dst_data == NULL)
423     xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
424             src, dst, rc->name);
425
426   AS_t src_as =
427       ((network_element_info_t) src_data)->rc_component;
428   AS_t dst_as =
429       ((network_element_info_t) dst_data)->rc_component;
430
431   if (src_as != dst_as)
432     xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
433             src, src_as->name, dst, dst_as->name);
434   if (rc != dst_as)
435     xbt_die
436         ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
437          src, dst, src_as->name, dst_as->name, rc->name);
438 }