Logo AND Algorithmique Numérique Distribuée

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