Logo AND Algorithmique Numérique Distribuée

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