Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite remplace in surf_routing_rulebase and avoid calls to strlen..
[simgrid.git] / src / surf / surf_routing_floyd.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 "surf_routing_private.h"
8
9 /* Global vars */
10 extern routing_global_t global_routing;
11 extern routing_component_t current_routing;
12 extern model_type_t current_routing_model;
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_floyd, surf, "Routing part of surf");
15
16 #define TO_FLOYD_COST(i,j) (routing->cost_table)[(i)+(j)*table_size]
17 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
18 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
19
20 /* Routing model structure */
21
22 typedef struct {
23   s_routing_component_t generic_routing;
24   /* vars for calculate the floyd algorith. */
25   int *predecessor_table;
26   double *cost_table;
27   route_extended_t *link_table; /* char* -> int* */
28 } s_routing_component_floyd_t, *routing_component_floyd_t;
29
30 static route_extended_t floyd_get_route(routing_component_t rc,
31                                         const char *src, const char *dst);
32
33 /* Business methods */
34 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
35 {
36   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
37
38   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
39   //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
40   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
41   char *k1, *d1, *k2, *d2;
42   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
43     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
44       route_extended_t route = floyd_get_route(rc, k1, k2);
45       if (route) {
46         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
47           void *link =
48               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
49                                            0);
50           onelink_t onelink = xbt_new0(s_onelink_t, 1);
51           onelink->link_ptr = link;
52           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
53             onelink->src = xbt_strdup(k1);
54             onelink->dst = xbt_strdup(k2);
55           } else if (routing->generic_routing.hierarchy ==
56                      SURF_ROUTING_RECURSIVE) {
57             onelink->src = xbt_strdup(route->src_gateway);
58             onelink->dst = xbt_strdup(route->dst_gateway);
59           }
60           xbt_dynar_push(ret, &onelink);
61         }
62       }
63     }
64   }
65   return ret;
66 }
67
68 static route_extended_t floyd_get_route(routing_component_t rc,
69                                         const char *src, const char *dst)
70 {
71   xbt_assert(rc && src
72               && dst,
73               "Invalid params for \"get_route\" function at AS \"%s\"",
74               rc->name);
75
76   /* set utils vars */
77   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
78   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
79
80   generic_src_dst_check(rc, src, dst);
81   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
82   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
83   xbt_assert(src_id
84               && dst_id,
85               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
86               src, dst);
87
88   /* create a result route */
89   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
90   new_e_route->generic_route.link_list =
91       xbt_dynar_new(global_routing->size_of_link, NULL);
92   new_e_route->src_gateway = NULL;
93   new_e_route->dst_gateway = NULL;
94
95   int first = 1;
96   int pred = *dst_id;
97   int prev_pred = 0;
98   char *gw_src = NULL, *gw_dst =
99       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
100   unsigned int cpt;
101   void *link;
102   xbt_dynar_t links;
103
104   do {
105     prev_pred = pred;
106     pred = TO_FLOYD_PRED(*src_id, pred);
107     if (pred == -1)             /* if no pred in route -> no route to host */
108       break;
109     xbt_assert(TO_FLOYD_LINK(pred, prev_pred),
110                 "Invalid link for the route between \"%s\" or \"%s\"", src,
111                 dst);
112
113     prev_gw_src = gw_src;
114     prev_gw_dst = gw_dst;
115
116     route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
117     gw_src = e_route->src_gateway;
118     gw_dst = e_route->dst_gateway;
119
120     if (first)
121       first_gw = gw_dst;
122
123     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
124         && strcmp(gw_dst, prev_gw_src)) {
125       xbt_dynar_t e_route_as_to_as =
126           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
127       xbt_assert(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
128                   gw_dst, prev_gw_src);
129       links = e_route_as_to_as;
130       int pos = 0;
131       xbt_dynar_foreach(links, cpt, link) {
132         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
133                             &link);
134         pos++;
135       }
136     }
137
138     links = e_route->generic_route.link_list;
139     xbt_dynar_foreach(links, cpt, link) {
140       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
141     }
142     first = 0;
143
144   } while (pred != *src_id);
145   xbt_assert(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
146               *src_id, *dst_id, src, dst);
147
148   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
149     new_e_route->src_gateway = xbt_strdup(gw_src);
150     new_e_route->dst_gateway = xbt_strdup(first_gw);
151   }
152
153   return new_e_route;
154 }
155
156 static void floyd_finalize(routing_component_t rc)
157 {
158   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
159   int i, j;
160   size_t table_size;
161   if (routing) {
162     table_size = xbt_dict_length(routing->generic_routing.to_index);
163     /* Delete link_table */
164     for (i = 0; i < table_size; i++)
165       for (j = 0; j < table_size; j++)
166         generic_free_extended_route(TO_FLOYD_LINK(i, j));
167     xbt_free(routing->link_table);
168     /* Delete bypass dict */
169     xbt_dict_free(&routing->generic_routing.bypassRoutes);
170     /* Delete index dict */
171     xbt_dict_free(&(routing->generic_routing.to_index));
172     /* Delete dictionary index dict, predecessor and links table */
173     xbt_free(routing->predecessor_table);
174     /* Delete structure */
175     xbt_free(rc);
176   }
177 }
178
179 void *model_floyd_create(void)
180 {
181   routing_component_floyd_t new_component =
182       xbt_new0(s_routing_component_floyd_t, 1);
183   new_component->generic_routing.set_processing_unit =
184       generic_set_processing_unit;
185   new_component->generic_routing.set_autonomous_system =
186       generic_set_autonomous_system;
187   new_component->generic_routing.set_route = model_floyd_set_route;
188   new_component->generic_routing.set_ASroute = model_floyd_set_route;
189   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
190   new_component->generic_routing.get_route = floyd_get_route;
191   new_component->generic_routing.get_latency = generic_get_link_latency;
192   new_component->generic_routing.get_onelink_routes =
193       floyd_get_onelink_routes;
194   new_component->generic_routing.get_bypass_route =
195       generic_get_bypassroute;
196   new_component->generic_routing.finalize = floyd_finalize;
197   new_component->generic_routing.to_index = xbt_dict_new();
198   new_component->generic_routing.bypassRoutes = xbt_dict_new();
199   new_component->generic_routing.get_network_element_type = get_network_element_type;
200   return new_component;
201 }
202
203 void model_floyd_load(void)
204 {
205   /* use "surfxml_add_callback" to add a parse function call */
206 }
207
208 void model_floyd_unload(void)
209 {
210   /* use "surfxml_del_callback" to remove a parse function call */
211 }
212
213 void model_floyd_end(void)
214 {
215
216         routing_component_floyd_t routing =
217           ((routing_component_floyd_t) current_routing);
218
219         unsigned int i, j, a, b, c;
220
221         /* set the size of table routing */
222         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
223
224         if(!routing->link_table)
225         {
226                 /* Create Cost, Predecessor and Link tables */
227                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
228                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
229                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
230
231                 /* Initialize costs and predecessors */
232                 for (i = 0; i < table_size; i++)
233                 for (j = 0; j < table_size; j++) {
234                   TO_FLOYD_COST(i, j) = DBL_MAX;
235                   TO_FLOYD_PRED(i, j) = -1;
236                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
237                 }
238         }
239
240         /* Add the loopback if needed */
241         if (current_routing->hierarchy == SURF_ROUTING_BASE) {
242                 for (i = 0; i < table_size; i++) {
243                   route_extended_t e_route = TO_FLOYD_LINK(i, i);
244                   if (!e_route) {
245                         e_route = xbt_new0(s_route_extended_t, 1);
246                         e_route->src_gateway = NULL;
247                         e_route->dst_gateway = NULL;
248                         e_route->generic_route.link_list =
249                                 xbt_dynar_new(global_routing->size_of_link, NULL);
250                         xbt_dynar_push(e_route->generic_route.link_list,
251                                                    &global_routing->loopback);
252                         TO_FLOYD_LINK(i, i) = e_route;
253                         TO_FLOYD_PRED(i, i) = i;
254                         TO_FLOYD_COST(i, i) = 1;
255                   }
256                 }
257         }
258         /* Calculate path costs */
259         for (c = 0; c < table_size; c++) {
260                 for (a = 0; a < table_size; a++) {
261                   for (b = 0; b < table_size; b++) {
262                         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
263                           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
264                                   (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
265                                    TO_FLOYD_COST(a, b))) {
266                                 TO_FLOYD_COST(a, b) =
267                                         TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
268                                 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
269                           }
270                         }
271                   }
272                 }
273         }
274 }
275
276 void model_floyd_set_route(routing_component_t rc, const char *src,
277         const char *dst, name_route_extended_t route)
278 {
279         routing_component_floyd_t routing = (routing_component_floyd_t) rc;
280
281         /* set the size of table routing */
282         size_t table_size = xbt_dict_length(rc->to_index);
283         int *src_id, *dst_id;
284         int i,j;
285
286         src_id = xbt_dict_get_or_null(rc->to_index, src);
287         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
288
289         xbt_assert(src_id, "Network elements %s not found", src);
290         xbt_assert(dst_id, "Network elements %s not found", dst);
291
292         if(!routing->link_table)
293         {
294                 /* Create Cost, Predecessor and Link tables */
295                 routing->cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
296                 routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
297                 routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
298
299                 /* Initialize costs and predecessors */
300                 for (i = 0; i < table_size; i++)
301                 for (j = 0; j < table_size; j++) {
302                   TO_FLOYD_COST(i, j) = DBL_MAX;
303                   TO_FLOYD_PRED(i, j) = -1;
304                   TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
305                 }
306         }
307
308         if(TO_FLOYD_LINK(*src_id, *dst_id))
309         {
310                 if(!route->dst_gateway && !route->src_gateway)
311                         XBT_DEBUG("See Route from \"%s\" to \"%s\"", src, dst);
312                 else
313                         XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
314                                  route->src_gateway, dst, route->dst_gateway);
315                 char * link_name;
316                 unsigned int cpt;
317                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
318                 xbt_dynar_foreach(route->generic_route.link_list,cpt,link_name)
319                 {
320                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
321                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
322                         xbt_dynar_push(link_route_to_test,&link);
323                 }
324                 xbt_assert(!xbt_dynar_compare(
325                           (void*)TO_FLOYD_LINK(*src_id, *dst_id)->generic_route.link_list,
326                           (void*)link_route_to_test,
327                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
328                           "The route between \"%s\" and \"%s\" already exists", src,dst);
329         }
330         else
331         {
332                 if(!route->dst_gateway && !route->src_gateway)
333                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
334                 else{
335                         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
336                                  route->src_gateway, dst, route->dst_gateway);
337                         if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
338                                 xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
339                         if(global_routing->get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
340                                 xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
341                 }
342             TO_FLOYD_LINK(*src_id, *dst_id) =
343                         generic_new_extended_route(rc->hierarchy, route, 1);
344             TO_FLOYD_PRED(*src_id, *dst_id) = *src_id;
345             TO_FLOYD_COST(*src_id, *dst_id) =
346                         ((TO_FLOYD_LINK(*src_id, *dst_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
347         }
348
349         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
350                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
351         {
352                 if(TO_FLOYD_LINK(*dst_id, *src_id))
353                 {
354                         if(!route->dst_gateway && !route->src_gateway)
355                           XBT_DEBUG("See Route from \"%s\" to \"%s\"", dst, src);
356                         else
357                           XBT_DEBUG("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
358                                          route->src_gateway, src, route->dst_gateway);
359                         char * link_name;
360                         unsigned int i;
361                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
362                         for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
363                         {
364                                 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
365                                 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
366                                 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
367                                 xbt_dynar_push(link_route_to_test,&link);
368                         }
369                         xbt_assert(!xbt_dynar_compare(
370                                   (void*)TO_FLOYD_LINK(*dst_id, *src_id)->generic_route.link_list,
371                               (void*)link_route_to_test,
372                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
373                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
374                 }
375                 else
376                 {
377                         if(route->dst_gateway && route->src_gateway)
378                         {
379                           char *gw_src = xbt_strdup(route->src_gateway);
380                           char *gw_dst = xbt_strdup(route->dst_gateway);
381                           route->src_gateway = gw_dst;
382                           route->dst_gateway = gw_src;
383                         }
384
385                         if(!route->dst_gateway && !route->src_gateway)
386                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
387                         else
388                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
389                                          route->src_gateway, src, route->dst_gateway);
390
391                     TO_FLOYD_LINK(*dst_id, *src_id) =
392                                 generic_new_extended_route(rc->hierarchy, route, 0);
393                     TO_FLOYD_PRED(*dst_id, *src_id) = *dst_id;
394                     TO_FLOYD_COST(*dst_id, *src_id) =
395                                 ((TO_FLOYD_LINK(*dst_id, *src_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
396                 }
397         }
398 }