Logo AND Algorithmique Numérique Distribuée

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