Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
983fd5f92970307955127999e788b7f45dc36fe9
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009, 2010. 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
8
9 #include <float.h>
10 #include "gras_config.h"
11
12 #ifdef HAVE_PCRE_LIB
13 #include <pcre.h>               /* regular expresion library */
14 #endif
15 #include "surf_private.h"
16 #include "xbt/dynar.h"
17 #include "xbt/str.h"
18 #include "xbt/config.h"
19 #include "xbt/graph.h"
20 #include "xbt/set.h"
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
23
24 /* Global vars */
25 routing_global_t global_routing = NULL;
26 routing_component_t current_routing = NULL;
27 model_type_t current_routing_model = NULL;
28
29 /* Prototypes of each model */
30 static void *model_full_create(void);   /* create structures for full routing model */
31 static void model_full_load(void);      /* load parse functions for full routing model */
32 static void model_full_unload(void);    /* unload parse functions for full routing model */
33 static void model_full_end(void);       /* finalize the creation of full routing model */
34
35 static void *model_floyd_create(void);  /* create structures for floyd routing model */
36 static void model_floyd_load(void);     /* load parse functions for floyd routing model */
37 static void model_floyd_unload(void);   /* unload parse functions for floyd routing model */
38 static void model_floyd_end(void);      /* finalize the creation of floyd routing model */
39
40 static void *model_dijkstra_both_create(int cached);    /* create by calling dijkstra or dijkstracache */
41 static void *model_dijkstra_create(void);       /* create structures for dijkstra routing model */
42 static void *model_dijkstracache_create(void);  /* create structures for dijkstracache routing model */
43 static void model_dijkstra_both_load(void);     /* load parse functions for dijkstra routing model */
44 static void model_dijkstra_both_unload(void);   /* unload parse functions for dijkstra routing model */
45 static void model_dijkstra_both_end(void);      /* finalize the creation of dijkstra routing model */
46
47 static void *model_rulebased_create(void);      /* create structures for rulebased routing model */
48 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
49 static void model_rulebased_unload(void);       /* unload parse functions for rulebased routing model */
50 static void model_rulebased_end(void);  /* finalize the creation of rulebased routing model */
51
52 static void *model_none_create(void);   /* none routing model */
53 static void model_none_load(void);      /* none routing model */
54 static void model_none_unload(void);    /* none routing model */
55 static void model_none_end(void);       /* none routing model */
56
57 static void routing_parse_Scluster(void);  /*cluster bypass */
58
59 static void routing_parse_Sconfig(void);        /*config Tag */
60 static void routing_parse_Econfig(void);        /*config Tag */
61
62 /* this lines are only for replace use like index in the model table */
63 typedef enum {
64   SURF_MODEL_FULL = 0,
65   SURF_MODEL_FLOYD,
66   SURF_MODEL_DIJKSTRA,
67   SURF_MODEL_DIJKSTRACACHE,
68   SURF_MODEL_NONE,
69 #ifdef HAVE_PCRE_LIB
70   SURF_MODEL_RULEBASED
71 #endif
72 } e_routing_types;
73
74
75 /* must be finish with null and carefull if change de order */
76 struct s_model_type routing_models[] = { {"Full",
77                                           "Full routing data (fast, large memory requirements, fully expressive)",
78                                           model_full_create,
79                                           model_full_load,
80                                           model_full_unload,
81                                           model_full_end},
82 {"Floyd",
83  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
84  model_floyd_create, model_floyd_load, model_floyd_unload,
85  model_floyd_end},
86 {"Dijkstra",
87  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
88  model_dijkstra_create, model_dijkstra_both_load,
89  model_dijkstra_both_unload, model_dijkstra_both_end},
90 {"DijkstraCache",
91  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
92  model_dijkstracache_create, model_dijkstra_both_load,
93  model_dijkstra_both_unload, model_dijkstra_both_end},
94 {"none", "No routing (usable with Constant network only)",
95  model_none_create, model_none_load, model_none_unload, model_none_end},
96 #ifdef HAVE_PCRE_LIB
97 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
98  model_rulebased_load, model_rulebased_unload, model_rulebased_end},
99 #endif
100 {NULL, NULL, NULL, NULL, NULL, NULL}
101 };
102
103 /* ************************************************************************** */
104 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
105
106 static void generic_set_processing_unit(routing_component_t rc,
107                                         const char *name);
108 static void generic_set_autonomous_system(routing_component_t rc,
109                                           const char *name);
110 static void generic_set_route(routing_component_t rc, const char *src,
111                               const char *dst, route_t route);
112 static void generic_set_ASroute(routing_component_t rc, const char *src,
113                                 const char *dst, route_extended_t e_route);
114 static void generic_set_bypassroute(routing_component_t rc,
115                                     const char *src, const char *dst,
116                                     route_extended_t e_route);
117
118 /* ************************************************************************** */
119 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
120
121 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
122 static route_extended_t generic_get_bypassroute(routing_component_t rc,
123                                                 const char *src,
124                                                 const char *dst);
125
126 /* ************************************************************************** */
127 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
128
129 static route_extended_t
130 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
131                            void *data, int order);
132 static void generic_free_route(route_t route);
133 static void generic_free_extended_route(route_extended_t e_route);
134 static routing_component_t
135 generic_autonomous_system_exist(routing_component_t rc, char *element);
136 static routing_component_t
137 generic_processing_units_exist(routing_component_t rc, char *element);
138 static void generic_src_dst_check(routing_component_t rc, const char *src,
139                                   const char *dst);
140
141 /* ************************************************************************** */
142 /* **************************** GLOBAL FUNCTIONS **************************** */
143
144 /* global parse functions */
145 static char *src = NULL;        /* temporary store the source name of a route */
146 static char *dst = NULL;        /* temporary store the destination name of a route */
147 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
148 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
149 static xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
150 /**
151  * \brief Add a "host" to the network element list
152  */
153 static void parse_S_host(char *host_id)
154 {
155   network_element_info_t info = NULL;
156   if (current_routing->hierarchy == SURF_ROUTING_NULL)
157     current_routing->hierarchy = SURF_ROUTING_BASE;
158   xbt_assert1(!xbt_dict_get_or_null
159               (global_routing->where_network_elements, host_id),
160               "Reading a host, processing unit \"%s\" already exist",
161               host_id);
162   xbt_assert1(current_routing->set_processing_unit,
163               "no defined method \"set_processing_unit\" in \"%s\"",
164               current_routing->name);
165   (*(current_routing->set_processing_unit)) (current_routing, host_id);
166   info = xbt_new0(s_network_element_info_t, 1);
167   info->rc_component = current_routing;
168   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
169   xbt_dict_set(global_routing->where_network_elements, host_id,
170                (void *) info, NULL);
171 }
172
173 /*
174  * \brief Add a host to the network element list from XML
175  */
176 static void parse_S_host_XML(void)
177 {
178   parse_S_host(A_surfxml_host_id);
179 }
180
181 /*
182  * \brief Add a host to the network element list from lua script
183  */
184 static void parse_S_host_lua(char *host_id)
185 {
186   parse_S_host(host_id);
187 }
188
189
190 /**
191  * \brief Add a "router" to the network element list
192  */
193 static void parse_S_router(void)
194 {
195   network_element_info_t info = NULL;
196
197   if (current_routing->hierarchy == SURF_ROUTING_NULL)
198     current_routing->hierarchy = SURF_ROUTING_BASE;
199   xbt_assert1(!xbt_dict_get_or_null
200               (global_routing->where_network_elements,
201                A_surfxml_router_id),
202               "Reading a router, processing unit \"%s\" already exist",
203               A_surfxml_router_id);
204   xbt_assert1(current_routing->set_processing_unit,
205               "no defined method \"set_processing_unit\" in \"%s\"",
206               current_routing->name);
207   (*(current_routing->set_processing_unit)) (current_routing,
208                                              A_surfxml_router_id);
209   info = xbt_new0(s_network_element_info_t, 1);
210   info->rc_component = current_routing;
211   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
212   xbt_dict_set(global_routing->where_network_elements, A_surfxml_router_id,
213                (void *) info, NULL);
214 #ifdef HAVE_TRACING
215   TRACE_surf_host_declaration(A_surfxml_router_id, 0);
216 #endif
217 }
218
219 /**
220  * \brief Set the endponints for a route
221  */
222 static void parse_S_route_new_and_endpoints(char *src_id, char *dst_id)
223 {
224   if (src != NULL && dst != NULL && link_list != NULL)
225     THROW2(arg_error, 0, "Route between %s to %s can not be defined",
226            src_id, dst_id);
227   src = src_id;
228   dst = dst_id;
229   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
230               "Some limits are null in the route between \"%s\" and \"%s\"",
231               src, dst);
232   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
233 }
234
235 /**
236  * \breif Set the endpoints for a route from XML
237  */
238 static void parse_S_route_new_and_endpoints_XML(void)
239 {
240   parse_S_route_new_and_endpoints(A_surfxml_route_src,
241                                   A_surfxml_route_dst);
242 }
243
244 /**
245  * \breif Set the endpoints for a route from lua
246  */
247 static void parse_S_route_new_and_endpoints_lua(char *id_src, char *id_dst)
248 {
249   parse_S_route_new_and_endpoints(id_src, id_dst);
250 }
251
252 /**
253  * \brief Set the endponints and gateways for a ASroute
254  */
255 static void parse_S_ASroute_new_and_endpoints(void)
256 {
257   if (src != NULL && dst != NULL && link_list != NULL)
258     THROW2(arg_error, 0, "Route between %s to %s can not be defined",
259            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
260   src = A_surfxml_ASroute_src;
261   dst = A_surfxml_ASroute_dst;
262   gw_src = A_surfxml_ASroute_gw_src;
263   gw_dst = A_surfxml_ASroute_gw_dst;
264   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
265               || strlen(gw_dst) > 0,
266               "Some limits are null in the route between \"%s\" and \"%s\"",
267               src, dst);
268   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
269 }
270
271 /**
272  * \brief Set the endponints for a bypassRoute
273  */
274 static void parse_S_bypassRoute_new_and_endpoints(void)
275 {
276   if (src != NULL && dst != NULL && link_list != NULL)
277     THROW2(arg_error, 0,
278            "Bypass Route between %s to %s can not be defined",
279            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
280   src = A_surfxml_bypassRoute_src;
281   dst = A_surfxml_bypassRoute_dst;
282   gw_src = A_surfxml_bypassRoute_gw_src;
283   gw_dst = A_surfxml_bypassRoute_gw_dst;
284   xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
285               || strlen(gw_dst) > 0,
286               "Some limits are null in the route between \"%s\" and \"%s\"",
287               src, dst);
288   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
289 }
290
291 /**
292  * \brief Set a new link on the actual list of link for a route or ASroute
293  */
294 static void parse_E_link_ctn_new_elem(char *link_id)
295 {
296   char *val;
297   val = xbt_strdup(link_id);
298   xbt_dynar_push(link_list, &val);
299 }
300
301 /**
302  * \brief Set a new link on the actual list of link for a route or ASroute from XML
303  */
304
305 static void parse_E_link_ctn_new_elem_XML(void)
306 {
307   if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
308           parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
309   if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP)
310           parse_E_link_ctn_new_elem(bprintf("%s_UP",A_surfxml_link_ctn_id));
311   if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN)
312           parse_E_link_ctn_new_elem(bprintf("%s_DOWN",A_surfxml_link_ctn_id));
313 }
314
315 /**
316  * \brief Set a new link on the actual list of link for a route or ASroute from lua
317  */
318 static void parse_E_link_c_ctn_new_elem_lua(char *link_id)
319 {
320   parse_E_link_ctn_new_elem(link_id);
321 }
322
323 /**
324  * \brief Store the route by calling the set_route function of the current routing component
325  */
326 static void parse_E_route_store_route(void)
327 {
328   route_t route = xbt_new0(s_route_t, 1);
329   route->link_list = link_list;
330   xbt_assert1(current_routing->set_route,
331               "no defined method \"set_route\" in \"%s\"",
332               current_routing->name);
333   (*(current_routing->set_route)) (current_routing, src, dst, route);
334   link_list = NULL;
335   src = NULL;
336   dst = NULL;
337 }
338
339 /**
340  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
341  */
342 static void parse_E_ASroute_store_route(void)
343 {
344   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
345   e_route->generic_route.link_list = link_list;
346   e_route->src_gateway = xbt_strdup(gw_src);
347   e_route->dst_gateway = xbt_strdup(gw_dst);
348   xbt_assert1(current_routing->set_ASroute,
349               "no defined method \"set_ASroute\" in \"%s\"",
350               current_routing->name);
351   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
352   link_list = NULL;
353   src = NULL;
354   dst = NULL;
355   gw_src = NULL;
356   gw_dst = NULL;
357 }
358
359 /**
360  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
361  */
362 static void parse_E_bypassRoute_store_route(void)
363 {
364   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
365   e_route->generic_route.link_list = link_list;
366   e_route->src_gateway = xbt_strdup(gw_src);
367   e_route->dst_gateway = xbt_strdup(gw_dst);
368   xbt_assert1(current_routing->set_bypassroute,
369               "no defined method \"set_bypassroute\" in \"%s\"",
370               current_routing->name);
371   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
372                                          e_route);
373   link_list = NULL;
374   src = NULL;
375   dst = NULL;
376   gw_src = NULL;
377   gw_dst = NULL;
378 }
379
380 /**
381  * \brief Make a new routing component
382  *
383  * make the new structure and
384  * set the parsing functions to allows parsing the part of the routing tree
385  */
386 static void parse_S_AS(char *AS_id, char *AS_routing)
387 {
388   routing_component_t new_routing;
389   model_type_t model = NULL;
390   char *wanted = AS_routing;
391   int cpt;
392   /* seach the routing model */
393   for (cpt = 0; routing_models[cpt].name; cpt++)
394     if (!strcmp(wanted, routing_models[cpt].name))
395       model = &routing_models[cpt];
396   /* if its not exist, error */
397   if (model == NULL) {
398     fprintf(stderr, "Routing model %s not found. Existing models:\n",
399             wanted);
400     for (cpt = 0; routing_models[cpt].name; cpt++)
401       if (!strcmp(wanted, routing_models[cpt].name))
402         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
403                 routing_models[cpt].desc);
404     xbt_die(NULL);
405   }
406
407   /* make a new routing component */
408   new_routing = (routing_component_t) (*(model->create)) ();
409   new_routing->routing = model;
410   new_routing->hierarchy = SURF_ROUTING_NULL;
411   new_routing->name = xbt_strdup(AS_id);
412   new_routing->routing_sons = xbt_dict_new();
413
414   if (current_routing == NULL && global_routing->root == NULL) {
415
416     /* it is the first one */
417     new_routing->routing_father = NULL;
418     global_routing->root = new_routing;
419
420   } else if (current_routing != NULL && global_routing->root != NULL) {
421
422     xbt_assert1(!xbt_dict_get_or_null
423                 (current_routing->routing_sons, AS_id),
424                 "The AS \"%s\" already exist", AS_id);
425     /* it is a part of the tree */
426     new_routing->routing_father = current_routing;
427     /* set the father behavior */
428     if (current_routing->hierarchy == SURF_ROUTING_NULL)
429       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
430     /* add to the sons dictionary */
431     xbt_dict_set(current_routing->routing_sons, AS_id,
432                  (void *) new_routing, NULL);
433     /* add to the father element list */
434     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
435     /* unload the prev parse rules */
436     (*(current_routing->routing->unload)) ();
437
438   } else {
439     THROW0(arg_error, 0, "All defined components must be belong to a AS");
440   }
441   /* set the new parse rules */
442   (*(new_routing->routing->load)) ();
443   /* set the new current component of the tree */
444   current_routing = new_routing;
445 }
446
447 /*
448  * Detect the routing model type of the routing component from XML platforms
449  */
450 static void parse_S_AS_XML(void)
451 {
452   parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
453 }
454
455 /*
456  * define the routing model type of routing component from lua script
457  */
458 static void parse_S_AS_lua(char *id, char *mode)
459 {
460   parse_S_AS(id, mode);
461 }
462
463
464 /**
465  * \brief Finish the creation of a new routing component
466  *
467  * When you finish to read the routing component, other structures must be created. 
468  * the "end" method allow to do that for any routing model type
469  */
470 static void parse_E_AS(char *AS_id)
471 {
472
473   if (current_routing == NULL) {
474     THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
475   } else {
476     network_element_info_t info = NULL;
477     xbt_assert1(!xbt_dict_get_or_null
478                 (global_routing->where_network_elements,
479                  current_routing->name), "The AS \"%s\" already exist",
480                 current_routing->name);
481     info = xbt_new0(s_network_element_info_t, 1);
482     info->rc_component = current_routing->routing_father;
483     info->rc_type = SURF_NETWORK_ELEMENT_AS;
484     xbt_dict_set(global_routing->where_network_elements,
485                  current_routing->name, info, NULL);
486     (*(current_routing->routing->unload)) ();
487     (*(current_routing->routing->end)) ();
488     current_routing = current_routing->routing_father;
489     if (current_routing != NULL)
490       (*(current_routing->routing->load)) ();
491   }
492 }
493
494 /*
495  * \brief Finish the creation of a new routing component from XML
496  */
497 static void parse_E_AS_XML(void)
498 {
499   parse_E_AS(A_surfxml_AS_id);
500 }
501
502 /*
503  * \brief Finish the creation of a new routing component from lua
504  */
505 static void parse_E_AS_lua(char *id)
506 {
507   parse_E_AS(id);
508 }
509
510 /* Aux Business methods */
511
512 /**
513  * \brief Get the AS father and the first elements of the chain
514  *
515  * \param src the source host name 
516  * \param dst the destination host name
517  * 
518  * Get the common father of the to processing units, and the first different 
519  * father in the chain
520  */
521 static xbt_dynar_t elements_father(const char *src, const char *dst)
522 {
523
524   xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
525
526   xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
527
528   routing_component_t src_as, dst_as;
529   int index_src, index_dst, index_father_src, index_father_dst;
530   xbt_dynar_t path_src = NULL;
531   xbt_dynar_t path_dst = NULL;
532   routing_component_t current = NULL;
533   routing_component_t *current_src = NULL;
534   routing_component_t *current_dst = NULL;
535   routing_component_t *father = NULL;
536
537   /* (1) find the as where the src and dst are located */
538   src_as = ((network_element_info_t)
539             xbt_dict_get_or_null(global_routing->where_network_elements,
540                                  src))->rc_component;
541   dst_as = ((network_element_info_t)
542             xbt_dict_get_or_null(global_routing->where_network_elements,
543                                  dst))->rc_component;
544   xbt_assert2(src_as
545               && dst_as,
546               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
547               dst);
548
549   /* (2) find the path to the root routing component */
550   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
551   current = src_as;
552   while (current != NULL) {
553     xbt_dynar_push(path_src, &current);
554     current = current->routing_father;
555   }
556   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
557   current = dst_as;
558   while (current != NULL) {
559     xbt_dynar_push(path_dst, &current);
560     current = current->routing_father;
561   }
562
563   /* (3) find the common father */
564   index_src = (path_src->used) - 1;
565   index_dst = (path_dst->used) - 1;
566   current_src = xbt_dynar_get_ptr(path_src, index_src);
567   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
568   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
569     current_src = xbt_dynar_get_ptr(path_src, index_src);
570     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
571     index_src--;
572     index_dst--;
573   }
574   index_src++;
575   index_dst++;
576   current_src = xbt_dynar_get_ptr(path_src, index_src);
577   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
578
579   /* (4) they are not in the same routing component, make the path */
580   index_father_src = index_src + 1;
581   index_father_dst = index_dst + 1;
582
583   if (*current_src == *current_dst)
584     father = current_src;
585   else
586     father = xbt_dynar_get_ptr(path_src, index_father_src);
587
588   /* (5) result generation */
589   xbt_dynar_push(result, father);       /* first same the father of src and dst */
590   xbt_dynar_push(result, current_src);  /* second the first different father of src */
591   xbt_dynar_push(result, current_dst);  /* three  the first different father of dst */
592
593   xbt_dynar_free(&path_src);
594   xbt_dynar_free(&path_dst);
595
596   return result;
597 }
598
599 /* Global Business methods */
600
601 /**
602  * \brief Recursive function for get_route
603  *
604  * \param src the source host name 
605  * \param dst the destination host name
606  * 
607  * This fuction is call by "get_route". It allow to walk through the 
608  * routing components tree.
609  */
610 static route_extended_t _get_route(const char *src, const char *dst)
611 {
612
613   void *link;
614   unsigned int cpt = 0;
615
616   DEBUG2("Solve route  \"%s\" to \"%s\"", src, dst);
617
618   xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
619
620   route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
621       NULL;
622
623   xbt_dynar_t elem_father_list = elements_father(src, dst);
624
625   routing_component_t common_father =
626       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
627   routing_component_t src_father =
628       xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
629   routing_component_t dst_father =
630       xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
631
632   e_route = xbt_new0(s_route_extended_t, 1);
633   e_route->src_gateway = NULL;
634   e_route->dst_gateway = NULL;
635   e_route->generic_route.link_list =
636       xbt_dynar_new(global_routing->size_of_link, NULL);
637
638   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
639
640     if (strcmp(src, dst)) {
641       e_route_cnt =
642           (*(common_father->get_route)) (common_father, src, dst);
643       xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
644                   dst);
645       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
646         xbt_dynar_push(e_route->generic_route.link_list, &link);
647       }
648       generic_free_extended_route(e_route_cnt);
649     }
650
651   } else {                      /* SURF_ROUTING_RECURSIVE */
652
653     route_extended_t e_route_bypass = NULL;
654
655     if (common_father->get_bypass_route)
656       e_route_bypass =
657           (*(common_father->get_bypass_route)) (common_father, src, dst);
658
659     if (e_route_bypass)
660       e_route_cnt = e_route_bypass;
661     else
662       e_route_cnt =
663           (*(common_father->get_route)) (common_father, src_father->name,
664                                          dst_father->name);
665
666     xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
667                 src_father->name, dst_father->name);
668
669     xbt_assert2((e_route_cnt->src_gateway == NULL) ==
670                 (e_route_cnt->dst_gateway == NULL),
671                 "bad gateway for route between \"%s\" and \"%s\"", src,
672                 dst);
673
674     if (src != e_route_cnt->src_gateway) {
675       e_route_src = _get_route(src, e_route_cnt->src_gateway);
676       xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
677                   e_route_cnt->src_gateway);
678       xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
679         xbt_dynar_push(e_route->generic_route.link_list, &link);
680       }
681     }
682
683     xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
684       xbt_dynar_push(e_route->generic_route.link_list, &link);
685     }
686
687     if (e_route_cnt->dst_gateway != dst) {
688       e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
689       xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
690                   e_route_cnt->dst_gateway, dst);
691       xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
692         xbt_dynar_push(e_route->generic_route.link_list, &link);
693       }
694     }
695
696     e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
697     e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
698
699     generic_free_extended_route(e_route_src);
700     generic_free_extended_route(e_route_cnt);
701     generic_free_extended_route(e_route_dst);
702   }
703
704   xbt_dynar_free(&elem_father_list);
705
706   return e_route;
707 }
708
709 /**
710  * \brief Generic method: find a route between hosts
711  *
712  * \param src the source host name 
713  * \param dst the destination host name
714  * 
715  * walk through the routing components tree and find a route between hosts
716  * by calling the differents "get_route" functions in each routing component.
717  * No need to free the returned dynar. It will be freed at the next call.
718  */
719 static xbt_dynar_t get_route(const char *src, const char *dst)
720 {
721
722   route_extended_t e_route;
723   xbt_dynar_t elem_father_list = elements_father(src, dst);
724   routing_component_t common_father =
725       xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
726
727   if (strcmp(src, dst))
728     e_route = _get_route(src, dst);
729   else
730     e_route = (*(common_father->get_route)) (common_father, src, dst);
731
732   xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
733
734   if (global_routing->last_route)
735     xbt_dynar_free(&(global_routing->last_route));
736   global_routing->last_route = e_route->generic_route.link_list;
737
738   if (e_route->src_gateway)
739     xbt_free(e_route->src_gateway);
740   if (e_route->dst_gateway)
741     xbt_free(e_route->dst_gateway);
742
743   xbt_free(e_route);
744   xbt_dynar_free(&elem_father_list);
745
746   if (xbt_dynar_length(global_routing->last_route) == 0)
747     return NULL;
748   else
749     return global_routing->last_route;
750 }
751
752 /**
753  * \brief Generic method: find a route between hosts
754  *
755  * \param src the source host name
756  * \param dst the destination host name
757  *
758  * walk through the routing components tree and find a route between hosts
759  * by calling the differents "get_route" functions in each routing component.
760  * Leaves the caller the responsability to clean the returned dynar.
761  */
762 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
763 {
764         xbt_dynar_t d = get_route(src,dst);
765         global_routing->last_route = NULL;
766         return d;
767 }
768
769 /**
770  * \brief Recursive function for finalize
771  *
772  * \param rc the source host name 
773  * 
774  * This fuction is call by "finalize". It allow to finalize the 
775  * AS or routing components. It delete all the structures.
776  */
777 static void _finalize(routing_component_t rc)
778 {
779   if (rc) {
780     xbt_dict_cursor_t cursor = NULL;
781     char *key;
782     routing_component_t elem;
783     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
784       _finalize(elem);
785     }
786     xbt_dict_t tmp_sons = rc->routing_sons;
787     char *tmp_name = rc->name;
788     xbt_dict_free(&tmp_sons);
789     xbt_free(tmp_name);
790     xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
791                 current_routing->name);
792     (*(rc->finalize)) (rc);
793   }
794 }
795
796 /**
797  * \brief Generic method: delete all the routing structures
798  * 
799  * walk through the routing components tree and delete the structures
800  * by calling the differents "finalize" functions in each routing component
801  */
802 static void finalize(void)
803 {
804   /* delete recursibly all the tree */
805   _finalize(global_routing->root);
806   /* delete "where" dict */
807   xbt_dict_free(&(global_routing->where_network_elements));
808   /* delete last_route */
809   xbt_dynar_free(&(global_routing->last_route));
810   /* delete global routing structure */
811   xbt_free(global_routing);
812 }
813
814 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
815 {
816   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
817
818   //adding my one link routes
819   unsigned int cpt;
820   void *link;
821   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
822   if (onelink_mine) {
823     xbt_dynar_foreach(onelink_mine, cpt, link) {
824       xbt_dynar_push(ret, &link);
825     }
826   }
827   //recursing
828   char *key;
829   xbt_dict_cursor_t cursor = NULL;
830   routing_component_t rc_child;
831   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
832     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
833     if (onelink_child) {
834       xbt_dynar_foreach(onelink_child, cpt, link) {
835         xbt_dynar_push(ret, &link);
836       }
837     }
838   }
839   return ret;
840 }
841
842 static xbt_dynar_t get_onelink_routes(void)
843 {
844   return recursive_get_onelink_routes(global_routing->root);
845 }
846
847 static e_surf_network_element_type_t get_network_element_type(const char
848                                                               *name)
849 {
850   network_element_info_t rc = NULL;
851   rc = xbt_dict_get(global_routing->where_network_elements, name);
852   return rc->rc_type;
853 }
854
855 /**
856  * \brief Generic method: create the global routing schema
857  * 
858  * Make a global routing structure and set all the parsing functions.
859  */
860 void routing_model_create(size_t size_of_links, void *loopback)
861 {
862
863   /* config the uniq global routing */
864   global_routing = xbt_new0(s_routing_global_t, 1);
865   global_routing->where_network_elements = xbt_dict_new();
866   global_routing->root = NULL;
867   global_routing->get_route = get_route;
868   global_routing->get_route_no_cleanup = get_route_no_cleanup;
869   global_routing->get_onelink_routes = get_onelink_routes;
870   global_routing->get_network_element_type = get_network_element_type;
871   global_routing->finalize = finalize;
872   global_routing->loopback = loopback;
873   global_routing->size_of_link = size_of_links;
874   global_routing->last_route = NULL;
875
876   /* no current routing at moment */
877   current_routing = NULL;
878
879   /* parse generic elements */
880   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
881   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
882
883   surfxml_add_callback(STag_surfxml_route_cb_list,
884                        &parse_S_route_new_and_endpoints_XML);
885   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
886                        &parse_S_ASroute_new_and_endpoints);
887   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
888                        &parse_S_bypassRoute_new_and_endpoints);
889
890   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
891                        &parse_E_link_ctn_new_elem_XML);
892
893   surfxml_add_callback(ETag_surfxml_route_cb_list,
894                        &parse_E_route_store_route);
895   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
896                        &parse_E_ASroute_store_route);
897   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
898                        &parse_E_bypassRoute_store_route);
899
900   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
901   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
902
903   surfxml_add_callback(STag_surfxml_cluster_cb_list,
904                        &routing_parse_Scluster);
905
906   surfxml_add_callback(STag_surfxml_config_cb_list,
907                                            &routing_parse_Sconfig);
908   surfxml_add_callback(ETag_surfxml_config_cb_list,
909                                            &routing_parse_Econfig);
910 }
911
912 /* ************************************************************************** */
913 /* *************************** FULL ROUTING ********************************* */
914
915 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
916
917 /* Routing model structure */
918
919 typedef struct {
920   s_routing_component_t generic_routing;
921   route_extended_t *routing_table;
922 } s_routing_component_full_t, *routing_component_full_t;
923
924 /* Business methods */
925 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
926 {
927   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
928
929   routing_component_full_t routing = (routing_component_full_t) rc;
930   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
931   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
932   char *k1, *d1, *k2, *d2;
933   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
934     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
935       int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
936       int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
937       xbt_assert2(src_id
938                   && dst_id,
939                   "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
940                   src, dst);
941       route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
942       if (route) {
943         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
944           void *link =
945               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
946                                            0);
947           onelink_t onelink = xbt_new0(s_onelink_t, 1);
948           onelink->link_ptr = link;
949           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
950             onelink->src = xbt_strdup(k1);
951             onelink->dst = xbt_strdup(k2);
952           } else if (routing->generic_routing.hierarchy ==
953                      SURF_ROUTING_RECURSIVE) {
954             onelink->src = xbt_strdup(route->src_gateway);
955             onelink->dst = xbt_strdup(route->dst_gateway);
956           }
957           xbt_dynar_push(ret, &onelink);
958         }
959       }
960     }
961   }
962   return ret;
963 }
964
965 static route_extended_t full_get_route(routing_component_t rc,
966                                        const char *src, const char *dst)
967 {
968   xbt_assert1(rc && src
969               && dst,
970               "Invalid params for \"get_route\" function at AS \"%s\"",
971               rc->name);
972
973   /* set utils vars */
974   routing_component_full_t routing = (routing_component_full_t) rc;
975   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
976
977   generic_src_dst_check(rc, src, dst);
978   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
979   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
980   xbt_assert2(src_id
981               && dst_id,
982               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
983               src, dst);
984
985   route_extended_t e_route = NULL;
986   route_extended_t new_e_route = NULL;
987   void *link;
988   unsigned int cpt = 0;
989
990   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
991
992   if (e_route) {
993     new_e_route = xbt_new0(s_route_extended_t, 1);
994     new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
995     new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
996     new_e_route->generic_route.link_list =
997         xbt_dynar_new(global_routing->size_of_link, NULL);
998     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
999       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1000     }
1001   }
1002   return new_e_route;
1003 }
1004
1005 static void full_finalize(routing_component_t rc)
1006 {
1007   routing_component_full_t routing = (routing_component_full_t) rc;
1008   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1009   int i, j;
1010   if (routing) {
1011     /* Delete routing table */
1012     for (i = 0; i < table_size; i++)
1013       for (j = 0; j < table_size; j++)
1014         generic_free_extended_route(TO_ROUTE_FULL(i, j));
1015     xbt_free(routing->routing_table);
1016     /* Delete bypass dict */
1017     xbt_dict_free(&rc->bypassRoutes);
1018     /* Delete index dict */
1019     xbt_dict_free(&rc->to_index);
1020     /* Delete structure */
1021     xbt_free(rc);
1022   }
1023 }
1024
1025 /* Creation routing model functions */
1026
1027 static void *model_full_create(void)
1028 {
1029   routing_component_full_t new_component =
1030       xbt_new0(s_routing_component_full_t, 1);
1031   new_component->generic_routing.set_processing_unit =
1032       generic_set_processing_unit;
1033   new_component->generic_routing.set_autonomous_system =
1034       generic_set_autonomous_system;
1035   new_component->generic_routing.set_route = generic_set_route;
1036   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1037   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1038   new_component->generic_routing.get_route = full_get_route;
1039   new_component->generic_routing.get_onelink_routes =
1040       full_get_onelink_routes;
1041   new_component->generic_routing.get_bypass_route =
1042       generic_get_bypassroute;
1043   new_component->generic_routing.finalize = full_finalize;
1044   new_component->generic_routing.to_index = xbt_dict_new();
1045   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1046   return new_component;
1047 }
1048
1049 static void model_full_load(void)
1050 {
1051   /* use "surfxml_add_callback" to add a parse function call */
1052 }
1053
1054 static void model_full_unload(void)
1055 {
1056   /* use "surfxml_del_callback" to remove a parse function call */
1057 }
1058
1059 static void model_full_end(void)
1060 {
1061   unsigned int i, j;
1062   route_extended_t e_route;
1063
1064   /* set utils vars */
1065   routing_component_full_t routing =
1066       ((routing_component_full_t) current_routing);
1067   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1068
1069   /* Create table if necessary */
1070   if(!routing->routing_table)
1071   {
1072           routing->routing_table =
1073                   xbt_new0(route_extended_t, table_size * table_size);
1074   }
1075
1076   /* Add the loopback if needed */
1077   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1078     for (i = 0; i < table_size; i++) {
1079       e_route = TO_ROUTE_FULL(i, i);
1080       if (!e_route) {
1081         e_route = xbt_new0(s_route_extended_t, 1);
1082         e_route->src_gateway = NULL;
1083         e_route->dst_gateway = NULL;
1084         e_route->generic_route.link_list =
1085             xbt_dynar_new(global_routing->size_of_link, NULL);
1086         xbt_dynar_push(e_route->generic_route.link_list,
1087                        &global_routing->loopback);
1088         TO_ROUTE_FULL(i, i) = e_route;
1089       }
1090     }
1091   }
1092
1093   /* Shrink the dynar routes (save unused slots) */
1094   for (i = 0; i < table_size; i++)
1095     for (j = 0; j < table_size; j++)
1096       if (TO_ROUTE_FULL(i, j))
1097         xbt_dynar_shrink(TO_ROUTE_FULL(i, j)->generic_route.link_list, 0);
1098 }
1099
1100 /* ************************************************************************** */
1101 /* *************************** FLOYD ROUTING ******************************** */
1102
1103 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
1104 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1105 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1106
1107 /* Routing model structure */
1108
1109 typedef struct {
1110   s_routing_component_t generic_routing;
1111   /* vars for calculate the floyd algorith. */
1112   int *predecessor_table;
1113   route_extended_t *link_table; /* char* -> int* */
1114 } s_routing_component_floyd_t, *routing_component_floyd_t;
1115
1116 static route_extended_t floyd_get_route(routing_component_t rc,
1117                                         const char *src, const char *dst);
1118
1119 /* Business methods */
1120 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1121 {
1122   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1123
1124   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1125   //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1126   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1127   char *k1, *d1, *k2, *d2;
1128   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1129     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1130       route_extended_t route = floyd_get_route(rc, k1, k2);
1131       if (route) {
1132         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1133           void *link =
1134               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1135                                            0);
1136           onelink_t onelink = xbt_new0(s_onelink_t, 1);
1137           onelink->link_ptr = link;
1138           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1139             onelink->src = xbt_strdup(k1);
1140             onelink->dst = xbt_strdup(k2);
1141           } else if (routing->generic_routing.hierarchy ==
1142                      SURF_ROUTING_RECURSIVE) {
1143             onelink->src = xbt_strdup(route->src_gateway);
1144             onelink->dst = xbt_strdup(route->dst_gateway);
1145           }
1146           xbt_dynar_push(ret, &onelink);
1147         }
1148       }
1149     }
1150   }
1151   return ret;
1152 }
1153
1154 static route_extended_t floyd_get_route(routing_component_t rc,
1155                                         const char *src, const char *dst)
1156 {
1157   xbt_assert1(rc && src
1158               && dst,
1159               "Invalid params for \"get_route\" function at AS \"%s\"",
1160               rc->name);
1161
1162   /* set utils vars */
1163   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1164   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1165
1166   generic_src_dst_check(rc, src, dst);
1167   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1168   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1169   xbt_assert2(src_id
1170               && dst_id,
1171               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1172               src, dst);
1173
1174   /* create a result route */
1175   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1176   new_e_route->generic_route.link_list =
1177       xbt_dynar_new(global_routing->size_of_link, NULL);
1178   new_e_route->src_gateway = NULL;
1179   new_e_route->dst_gateway = NULL;
1180
1181   int first = 1;
1182   int pred = *dst_id;
1183   int prev_pred = 0;
1184   char *gw_src = NULL, *gw_dst =
1185       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1186   unsigned int cpt;
1187   void *link;
1188   xbt_dynar_t links;
1189
1190   do {
1191     prev_pred = pred;
1192     pred = TO_FLOYD_PRED(*src_id, pred);
1193     if (pred == -1)             /* if no pred in route -> no route to host */
1194       break;
1195     xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1196                 "Invalid link for the route between \"%s\" or \"%s\"", src,
1197                 dst);
1198
1199     prev_gw_src = gw_src;
1200     prev_gw_dst = gw_dst;
1201
1202     route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1203     gw_src = e_route->src_gateway;
1204     gw_dst = e_route->dst_gateway;
1205
1206     if (first)
1207       first_gw = gw_dst;
1208
1209     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1210         && strcmp(gw_dst, prev_gw_src)) {
1211       xbt_dynar_t e_route_as_to_as =
1212           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1213       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1214                   gw_dst, prev_gw_src);
1215       links = e_route_as_to_as;
1216       int pos = 0;
1217       xbt_dynar_foreach(links, cpt, link) {
1218         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1219                             &link);
1220         pos++;
1221       }
1222     }
1223
1224     links = e_route->generic_route.link_list;
1225     xbt_dynar_foreach(links, cpt, link) {
1226       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1227     }
1228     first = 0;
1229
1230   } while (pred != *src_id);
1231   xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1232               *src_id, *dst_id, src, dst);
1233
1234   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1235     new_e_route->src_gateway = xbt_strdup(gw_src);
1236     new_e_route->dst_gateway = xbt_strdup(first_gw);
1237   }
1238
1239   return new_e_route;
1240 }
1241
1242 static void floyd_finalize(routing_component_t rc)
1243 {
1244   routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1245   int i, j;
1246   size_t table_size;
1247   if (routing) {
1248     table_size = xbt_dict_length(routing->generic_routing.to_index);
1249     /* Delete link_table */
1250     for (i = 0; i < table_size; i++)
1251       for (j = 0; j < table_size; j++)
1252         generic_free_extended_route(TO_FLOYD_LINK(i, j));
1253     xbt_free(routing->link_table);
1254     /* Delete bypass dict */
1255     xbt_dict_free(&routing->generic_routing.bypassRoutes);
1256     /* Delete index dict */
1257     xbt_dict_free(&(routing->generic_routing.to_index));
1258     /* Delete dictionary index dict, predecessor and links table */
1259     xbt_free(routing->predecessor_table);
1260     /* Delete structure */
1261     xbt_free(rc);
1262   }
1263 }
1264
1265 static void *model_floyd_create(void)
1266 {
1267   routing_component_floyd_t new_component =
1268       xbt_new0(s_routing_component_floyd_t, 1);
1269   new_component->generic_routing.set_processing_unit =
1270       generic_set_processing_unit;
1271   new_component->generic_routing.set_autonomous_system =
1272       generic_set_autonomous_system;
1273   new_component->generic_routing.set_route = generic_set_route;
1274   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1275   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1276   new_component->generic_routing.get_route = floyd_get_route;
1277   new_component->generic_routing.get_onelink_routes =
1278       floyd_get_onelink_routes;
1279   new_component->generic_routing.get_bypass_route =
1280       generic_get_bypassroute;
1281   new_component->generic_routing.finalize = floyd_finalize;
1282   new_component->generic_routing.to_index = xbt_dict_new();
1283   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1284   new_component->generic_routing.parse_routes = xbt_dict_new();
1285   return new_component;
1286 }
1287
1288 static void model_floyd_load(void)
1289 {
1290   /* use "surfxml_add_callback" to add a parse function call */
1291 }
1292
1293 static void model_floyd_unload(void)
1294 {
1295   /* use "surfxml_del_callback" to remove a parse function call */
1296 }
1297
1298 static void model_floyd_end(void)
1299 {
1300
1301   routing_component_floyd_t routing =
1302       ((routing_component_floyd_t) current_routing);
1303   xbt_dict_cursor_t cursor = NULL;
1304   double *cost_table;
1305   char *key, *data, *end;
1306   const char *sep = "#";
1307   xbt_dynar_t keys;
1308   int src_id, dst_id;
1309   unsigned int i, j, a, b, c;
1310
1311   /* set the size of inicial table */
1312   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1313
1314   /* Create Cost, Predecessor and Link tables */
1315   cost_table = xbt_new0(double, table_size * table_size);       /* link cost from host to host */
1316   routing->predecessor_table = xbt_new0(int, table_size * table_size);  /* predecessor host numbers */
1317   routing->link_table = xbt_new0(route_extended_t, table_size * table_size);    /* actual link between src and dst */
1318
1319   /* Initialize costs and predecessors */
1320   for (i = 0; i < table_size; i++)
1321     for (j = 0; j < table_size; j++) {
1322       TO_FLOYD_COST(i, j) = DBL_MAX;
1323       TO_FLOYD_PRED(i, j) = -1;
1324       TO_FLOYD_LINK(i, j) = NULL;       /* fixed, missing in the previous version */
1325     }
1326
1327   /* Put the routes in position */
1328   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1329     keys = xbt_str_split_str(key, sep);
1330     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1331     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1332     TO_FLOYD_LINK(src_id, dst_id) =
1333         generic_new_extended_route(current_routing->hierarchy, data, 0);
1334     TO_FLOYD_PRED(src_id, dst_id) = src_id;
1335     /* set link cost */
1336     TO_FLOYD_COST(src_id, dst_id) = ((TO_FLOYD_LINK(src_id, dst_id))->generic_route.link_list)->used;   /* count of links, old model assume 1 */
1337     xbt_dynar_free(&keys);
1338   }
1339
1340   /* Add the loopback if needed */
1341   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1342     for (i = 0; i < table_size; i++) {
1343       route_extended_t e_route = TO_FLOYD_LINK(i, i);
1344       if (!e_route) {
1345         e_route = xbt_new0(s_route_extended_t, 1);
1346         e_route->src_gateway = NULL;
1347         e_route->dst_gateway = NULL;
1348         e_route->generic_route.link_list =
1349             xbt_dynar_new(global_routing->size_of_link, NULL);
1350         xbt_dynar_push(e_route->generic_route.link_list,
1351                        &global_routing->loopback);
1352         TO_FLOYD_LINK(i, i) = e_route;
1353         TO_FLOYD_PRED(i, i) = i;
1354         TO_FLOYD_COST(i, i) = 1;
1355       }
1356     }
1357   }
1358   /* Calculate path costs */
1359   for (c = 0; c < table_size; c++) {
1360     for (a = 0; a < table_size; a++) {
1361       for (b = 0; b < table_size; b++) {
1362         if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1363           if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1364               (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1365                TO_FLOYD_COST(a, b))) {
1366             TO_FLOYD_COST(a, b) =
1367                 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1368             TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1369           }
1370         }
1371       }
1372     }
1373   }
1374
1375   /* delete the parse table */
1376   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1377     route_t route = (route_t) data;
1378     xbt_dynar_free(&(route->link_list));
1379     xbt_free(data);
1380   }
1381
1382   /* delete parse dict */
1383   xbt_dict_free(&(routing->generic_routing.parse_routes));
1384
1385   /* cleanup */
1386   xbt_free(cost_table);
1387 }
1388
1389 /* ************************************************************************** */
1390 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1391
1392 typedef struct {
1393   s_routing_component_t generic_routing;
1394   xbt_graph_t route_graph;      /* xbt_graph */
1395   xbt_dict_t graph_node_map;    /* map */
1396   xbt_dict_t route_cache;       /* use in cache mode */
1397   int cached;
1398 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1399
1400
1401 typedef struct graph_node_data {
1402   int id;
1403   int graph_id;                 /* used for caching internal graph id's */
1404 } s_graph_node_data_t, *graph_node_data_t;
1405
1406 typedef struct graph_node_map_element {
1407   xbt_node_t node;
1408 } s_graph_node_map_element_t, *graph_node_map_element_t;
1409
1410 typedef struct route_cache_element {
1411   int *pred_arr;
1412   int size;
1413 } s_route_cache_element_t, *route_cache_element_t;
1414
1415 /* Free functions */
1416
1417 static void route_cache_elem_free(void *e)
1418 {
1419   route_cache_element_t elm = (route_cache_element_t) e;
1420   if (elm) {
1421     xbt_free(elm->pred_arr);
1422     xbt_free(elm);
1423   }
1424 }
1425
1426 static void graph_node_map_elem_free(void *e)
1427 {
1428   graph_node_map_element_t elm = (graph_node_map_element_t) e;
1429   if (elm) {
1430     xbt_free(elm);
1431   }
1432 }
1433
1434 static void graph_edge_data_free(void *e)
1435 {
1436   route_extended_t e_route = (route_extended_t) e;
1437   if (e_route) {
1438     xbt_dynar_free(&(e_route->generic_route.link_list));
1439     if (e_route->src_gateway)
1440       xbt_free(e_route->src_gateway);
1441     if (e_route->dst_gateway)
1442       xbt_free(e_route->dst_gateway);
1443     xbt_free(e_route);
1444   }
1445 }
1446
1447 /* Utility functions */
1448
1449 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1450                                        int id, int graph_id)
1451 {
1452   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1453   xbt_node_t node = NULL;
1454   graph_node_data_t data = NULL;
1455   graph_node_map_element_t elm = NULL;
1456
1457   data = xbt_new0(struct graph_node_data, 1);
1458   data->id = id;
1459   data->graph_id = graph_id;
1460   node = xbt_graph_new_node(routing->route_graph, data);
1461
1462   elm = xbt_new0(struct graph_node_map_element, 1);
1463   elm->node = node;
1464   xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1465                    (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1466
1467   return node;
1468 }
1469
1470 static graph_node_map_element_t
1471 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1472 {
1473   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1474   graph_node_map_element_t elm = (graph_node_map_element_t)
1475       xbt_dict_get_or_null_ext(routing->graph_node_map,
1476                                (char *) (&id),
1477                                sizeof(int));
1478   return elm;
1479 }
1480
1481 /* Parsing */
1482
1483 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1484                                int dst_id, route_extended_t e_route)
1485 {
1486   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1487
1488   xbt_node_t src = NULL;
1489   xbt_node_t dst = NULL;
1490   graph_node_map_element_t src_elm = (graph_node_map_element_t)
1491       xbt_dict_get_or_null_ext(routing->graph_node_map,
1492                                (char *) (&src_id),
1493                                sizeof(int));
1494   graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1495       xbt_dict_get_or_null_ext(routing->graph_node_map,
1496                                (char *) (&dst_id),
1497                                sizeof(int));
1498
1499   if (src_elm)
1500     src = src_elm->node;
1501
1502   if (dst_elm)
1503     dst = dst_elm->node;
1504
1505   /* add nodes if they don't exist in the graph */
1506   if (src_id == dst_id && src == NULL && dst == NULL) {
1507     src = route_graph_new_node(rc, src_id, -1);
1508     dst = src;
1509   } else {
1510     if (src == NULL) {
1511       src = route_graph_new_node(rc, src_id, -1);
1512     }
1513     if (dst == NULL) {
1514       dst = route_graph_new_node(rc, dst_id, -1);
1515     }
1516   }
1517
1518   /* add link as edge to graph */
1519   xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1520 }
1521
1522 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1523 {
1524   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1525
1526   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1527
1528   xbt_node_t node = NULL;
1529   unsigned int cursor2;
1530   xbt_dynar_foreach(nodes, cursor2, node) {
1531     xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1532     xbt_edge_t edge = NULL;
1533     unsigned int cursor;
1534
1535     int found = 0;
1536     xbt_dynar_foreach(out_edges, cursor, edge) {
1537       xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1538       if (other_node == node) {
1539         found = 1;
1540         break;
1541       }
1542     }
1543
1544     if (!found) {
1545       route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1546       e_route->src_gateway = NULL;
1547       e_route->dst_gateway = NULL;
1548       e_route->generic_route.link_list =
1549           xbt_dynar_new(global_routing->size_of_link, NULL);
1550       xbt_dynar_push(e_route->generic_route.link_list,
1551                      &global_routing->loopback);
1552       xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1553     }
1554   }
1555 }
1556
1557 /* Business methods */
1558 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1559 {
1560   xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1561 }
1562
1563 static route_extended_t dijkstra_get_route(routing_component_t rc,
1564                                            const char *src,
1565                                            const char *dst)
1566 {
1567   xbt_assert1(rc && src
1568               && dst,
1569               "Invalid params for \"get_route\" function at AS \"%s\"",
1570               rc->name);
1571
1572   /* set utils vars */
1573   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1574
1575   generic_src_dst_check(rc, src, dst);
1576   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1577   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1578   xbt_assert2(src_id
1579               && dst_id,
1580               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
1581               src, dst);
1582
1583   /* create a result route */
1584   route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1585   new_e_route->generic_route.link_list =
1586       xbt_dynar_new(global_routing->size_of_link, NULL);
1587   new_e_route->src_gateway = NULL;
1588   new_e_route->dst_gateway = NULL;
1589
1590   int *pred_arr = NULL;
1591   int src_node_id = 0;
1592   int dst_node_id = 0;
1593   int *nodeid = NULL;
1594   int v;
1595   route_extended_t e_route;
1596   int size = 0;
1597   unsigned int cpt;
1598   void *link;
1599   xbt_dynar_t links = NULL;
1600   route_cache_element_t elm = NULL;
1601   xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1602
1603   /* Use the graph_node id mapping set to quickly find the nodes */
1604   graph_node_map_element_t src_elm =
1605       graph_node_map_search(routing, *src_id);
1606   graph_node_map_element_t dst_elm =
1607       graph_node_map_search(routing, *dst_id);
1608   xbt_assert2(src_elm != NULL
1609               && dst_elm != NULL, "src %d or dst %d does not exist",
1610               *src_id, *dst_id);
1611   src_node_id = ((graph_node_data_t)
1612                  xbt_graph_node_get_data(src_elm->node))->graph_id;
1613   dst_node_id = ((graph_node_data_t)
1614                  xbt_graph_node_get_data(dst_elm->node))->graph_id;
1615
1616   /* if the src and dst are the same *//* fixed, missing in the previous version */
1617   if (src_node_id == dst_node_id) {
1618
1619     xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1620     xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1621     xbt_edge_t edge =
1622         xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1623
1624     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1625                 *dst_id);
1626
1627     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1628
1629     links = e_route->generic_route.link_list;
1630     xbt_dynar_foreach(links, cpt, link) {
1631       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1632     }
1633
1634     return new_e_route;
1635   }
1636
1637   if (routing->cached) {
1638     /*check if there is a cached predecessor list avail */
1639     elm = (route_cache_element_t)
1640         xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
1641                                  sizeof(int));
1642   }
1643
1644   if (elm) {                    /* cached mode and cache hit */
1645     pred_arr = elm->pred_arr;
1646   } else {                      /* not cached mode or cache miss */
1647     double *cost_arr = NULL;
1648     xbt_heap_t pqueue = NULL;
1649     int i = 0;
1650
1651     int nr_nodes = xbt_dynar_length(nodes);
1652     cost_arr = xbt_new0(double, nr_nodes);      /* link cost from src to other hosts */
1653     pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1654     pqueue = xbt_heap_new(nr_nodes, xbt_free);
1655
1656     /* initialize */
1657     cost_arr[src_node_id] = 0.0;
1658
1659     for (i = 0; i < nr_nodes; i++) {
1660       if (i != src_node_id) {
1661         cost_arr[i] = DBL_MAX;
1662       }
1663
1664       pred_arr[i] = 0;
1665
1666       /* initialize priority queue */
1667       nodeid = xbt_new0(int, 1);
1668       *nodeid = i;
1669       xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1670
1671     }
1672
1673     /* apply dijkstra using the indexes from the graph's node array */
1674     while (xbt_heap_size(pqueue) > 0) {
1675       int *v_id = xbt_heap_pop(pqueue);
1676       xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1677       xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1678       xbt_edge_t edge = NULL;
1679       unsigned int cursor;
1680
1681       xbt_dynar_foreach(out_edges, cursor, edge) {
1682         xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1683         graph_node_data_t data = xbt_graph_node_get_data(u_node);
1684         int u_id = data->graph_id;
1685         route_extended_t tmp_e_route =
1686             (route_extended_t) xbt_graph_edge_get_data(edge);
1687         int cost_v_u = (tmp_e_route->generic_route.link_list)->used;    /* count of links, old model assume 1 */
1688
1689         if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1690           pred_arr[u_id] = *v_id;
1691           cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1692           nodeid = xbt_new0(int, 1);
1693           *nodeid = u_id;
1694           xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1695         }
1696       }
1697
1698       /* free item popped from pqueue */
1699       xbt_free(v_id);
1700     }
1701
1702     xbt_free(cost_arr);
1703     xbt_heap_free(pqueue);
1704   }
1705
1706   /* compose route path with links */
1707   char *gw_src = NULL, *gw_dst =
1708       NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1709
1710   for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1711     xbt_node_t node_pred_v =
1712         xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1713     xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1714     xbt_edge_t edge =
1715         xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1716
1717     xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1718                 *dst_id);
1719
1720     prev_gw_src = gw_src;
1721     prev_gw_dst = gw_dst;
1722
1723     e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1724     gw_src = e_route->src_gateway;
1725     gw_dst = e_route->dst_gateway;
1726
1727     if (v == dst_node_id)
1728       first_gw = gw_dst;
1729
1730     if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
1731         && strcmp(gw_dst, prev_gw_src)) {
1732       xbt_dynar_t e_route_as_to_as =
1733           (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1734       xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1735                   gw_dst, prev_gw_src);
1736       links = e_route_as_to_as;
1737       int pos = 0;
1738       xbt_dynar_foreach(links, cpt, link) {
1739         xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1740                             &link);
1741         pos++;
1742       }
1743     }
1744
1745     links = e_route->generic_route.link_list;
1746     xbt_dynar_foreach(links, cpt, link) {
1747       xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1748     }
1749     size++;
1750   }
1751
1752   if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1753     new_e_route->src_gateway = xbt_strdup(gw_src);
1754     new_e_route->dst_gateway = xbt_strdup(first_gw);
1755   }
1756
1757   if (routing->cached && elm == NULL) {
1758     /* add to predecessor list of the current src-host to cache */
1759     elm = xbt_new0(struct route_cache_element, 1);
1760     elm->pred_arr = pred_arr;
1761     elm->size = size;
1762     xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
1763                      (xbt_set_elm_t) elm, &route_cache_elem_free);
1764   }
1765
1766   if (!routing->cached)
1767     xbt_free(pred_arr);
1768
1769   return new_e_route;
1770 }
1771
1772 static void dijkstra_finalize(routing_component_t rc)
1773 {
1774   routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1775
1776   if (routing) {
1777     xbt_graph_free_graph(routing->route_graph, &xbt_free,
1778                          &graph_edge_data_free, &xbt_free);
1779     xbt_dict_free(&routing->graph_node_map);
1780     if (routing->cached)
1781       xbt_dict_free(&routing->route_cache);
1782     /* Delete bypass dict */
1783     xbt_dict_free(&routing->generic_routing.bypassRoutes);
1784     /* Delete index dict */
1785     xbt_dict_free(&(routing->generic_routing.to_index));
1786     /* Delete structure */
1787     xbt_free(routing);
1788   }
1789 }
1790
1791 /* Creation routing model functions */
1792
1793 static void *model_dijkstra_both_create(int cached)
1794 {
1795   routing_component_dijkstra_t new_component =
1796       xbt_new0(s_routing_component_dijkstra_t, 1);
1797   new_component->generic_routing.set_processing_unit =
1798       generic_set_processing_unit;
1799   new_component->generic_routing.set_autonomous_system =
1800       generic_set_autonomous_system;
1801   new_component->generic_routing.set_route = generic_set_route;
1802   new_component->generic_routing.set_ASroute = generic_set_ASroute;
1803   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1804   new_component->generic_routing.get_route = dijkstra_get_route;
1805   new_component->generic_routing.get_onelink_routes =
1806       dijkstra_get_onelink_routes;
1807   new_component->generic_routing.get_bypass_route =
1808       generic_get_bypassroute;
1809   new_component->generic_routing.finalize = dijkstra_finalize;
1810   new_component->cached = cached;
1811   new_component->generic_routing.to_index = xbt_dict_new();
1812   new_component->generic_routing.bypassRoutes = xbt_dict_new();
1813   new_component->generic_routing.parse_routes = xbt_dict_new();
1814   return new_component;
1815 }
1816
1817 static void *model_dijkstra_create(void)
1818 {
1819   return model_dijkstra_both_create(0);
1820 }
1821
1822 static void *model_dijkstracache_create(void)
1823 {
1824   return model_dijkstra_both_create(1);
1825 }
1826
1827 static void model_dijkstra_both_load(void)
1828 {
1829   /* use "surfxml_add_callback" to add a parse function call */
1830 }
1831
1832 static void model_dijkstra_both_unload(void)
1833 {
1834   /* use "surfxml_del_callback" to remove a parse function call */
1835 }
1836
1837 static void model_dijkstra_both_end(void)
1838 {
1839   routing_component_dijkstra_t routing =
1840       (routing_component_dijkstra_t) current_routing;
1841   xbt_dict_cursor_t cursor = NULL;
1842   char *key, *data, *end;
1843   const char *sep = "#";
1844   xbt_dynar_t keys;
1845   xbt_node_t node = NULL;
1846   unsigned int cursor2;
1847   xbt_dynar_t nodes = NULL;
1848   int src_id, dst_id;
1849   route_t route;
1850
1851   /* Create the topology graph */
1852   routing->route_graph = xbt_graph_new_graph(1, NULL);
1853   routing->graph_node_map = xbt_dict_new();
1854
1855   if (routing->cached)
1856     routing->route_cache = xbt_dict_new();
1857
1858   /* Put the routes in position */
1859   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1860     keys = xbt_str_split_str(key, sep);
1861     src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1862     dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1863     route_extended_t e_route =
1864         generic_new_extended_route(current_routing->hierarchy, data, 0);
1865     route_new_dijkstra(routing, src_id, dst_id, e_route);
1866     xbt_dynar_free(&keys);
1867   }
1868
1869   /* delete the parse table */
1870   xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1871     route = (route_t) data;
1872     xbt_dynar_free(&(route->link_list));
1873     xbt_free(data);
1874   }
1875
1876   /* delete parse dict */
1877   xbt_dict_free(&(routing->generic_routing.parse_routes));
1878
1879   /* Add the loopback if needed */
1880   if (current_routing->hierarchy == SURF_ROUTING_BASE)
1881     add_loopback_dijkstra(routing);
1882
1883   /* initialize graph indexes in nodes after graph has been built */
1884   nodes = xbt_graph_get_nodes(routing->route_graph);
1885
1886   xbt_dynar_foreach(nodes, cursor2, node) {
1887     graph_node_data_t data = xbt_graph_node_get_data(node);
1888     data->graph_id = cursor2;
1889   }
1890
1891 }
1892
1893 #ifdef HAVE_PCRE_LIB
1894 /* ************************************************** */
1895 /* ************** RULE-BASED ROUTING **************** */
1896
1897 /* Routing model structure */
1898
1899 typedef struct {
1900   s_routing_component_t generic_routing;
1901   xbt_dict_t dict_processing_units;
1902   xbt_dict_t dict_autonomous_systems;
1903   xbt_dynar_t list_route;
1904   xbt_dynar_t list_ASroute;
1905 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
1906
1907 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
1908 typedef struct s_rule_route_extended s_rule_route_extended_t,
1909     *rule_route_extended_t;
1910
1911 struct s_rule_route {
1912   xbt_dynar_t re_str_link;      // dynar of char*
1913   pcre *re_src;
1914   pcre *re_dst;
1915 };
1916
1917 struct s_rule_route_extended {
1918   s_rule_route_t generic_rule_route;
1919   char *re_src_gateway;
1920   char *re_dst_gateway;
1921 };
1922
1923 static void rule_route_free(void *e)
1924 {
1925   rule_route_t *elem = (rule_route_t *) (e);
1926   if (*elem) {
1927     xbt_dynar_free(&(*elem)->re_str_link);
1928     pcre_free((*elem)->re_src);
1929     pcre_free((*elem)->re_dst);
1930     xbt_free((*elem));
1931   }
1932   (*elem) = NULL;
1933 }
1934
1935 static void rule_route_extended_free(void *e)
1936 {
1937   rule_route_extended_t *elem = (rule_route_extended_t *) e;
1938   if (*elem) {
1939     xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
1940     pcre_free((*elem)->generic_rule_route.re_src);
1941     pcre_free((*elem)->generic_rule_route.re_dst);
1942     xbt_free((*elem)->re_src_gateway);
1943     xbt_free((*elem)->re_dst_gateway);
1944     xbt_free((*elem));
1945   }
1946 }
1947
1948 /* Parse routing model functions */
1949
1950 static void model_rulebased_set_processing_unit(routing_component_t rc,
1951                                                 const char *name)
1952 {
1953   routing_component_rulebased_t routing =
1954       (routing_component_rulebased_t) rc;
1955   xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
1956 }
1957
1958 static void model_rulebased_set_autonomous_system(routing_component_t rc,
1959                                                   const char *name)
1960 {
1961   routing_component_rulebased_t routing =
1962       (routing_component_rulebased_t) rc;
1963   xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
1964                NULL);
1965 }
1966
1967 static void model_rulebased_set_route(routing_component_t rc,
1968                                       const char *src, const char *dst,
1969                                       route_t route)
1970 {
1971   routing_component_rulebased_t routing =
1972       (routing_component_rulebased_t) rc;
1973   rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
1974   const char *error;
1975   int erroffset;
1976   ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
1977   xbt_assert3(ruleroute->re_src,
1978               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
1979               erroffset, src, error);
1980   ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
1981   xbt_assert3(ruleroute->re_src,
1982               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
1983               erroffset, dst, error);
1984   ruleroute->re_str_link = route->link_list;
1985   xbt_dynar_push(routing->list_route, &ruleroute);
1986   xbt_free(route);
1987 }
1988
1989 static void model_rulebased_set_ASroute(routing_component_t rc,
1990                                         const char *src, const char *dst,
1991                                         route_extended_t route)
1992 {
1993   routing_component_rulebased_t routing =
1994       (routing_component_rulebased_t) rc;
1995   rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
1996   const char *error;
1997   int erroffset;
1998   ruleroute_e->generic_rule_route.re_src =
1999       pcre_compile(src, 0, &error, &erroffset, NULL);
2000   xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2001               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2002               erroffset, src, error);
2003   ruleroute_e->generic_rule_route.re_dst =
2004       pcre_compile(dst, 0, &error, &erroffset, NULL);
2005   xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2006               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2007               erroffset, dst, error);
2008   ruleroute_e->generic_rule_route.re_str_link =
2009       route->generic_route.link_list;
2010   ruleroute_e->re_src_gateway = route->src_gateway;
2011   ruleroute_e->re_dst_gateway = route->dst_gateway;
2012   xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2013   xbt_free(route->src_gateway);
2014   xbt_free(route->dst_gateway);
2015   xbt_free(route);
2016 }
2017
2018 static void model_rulebased_set_bypassroute(routing_component_t rc,
2019                                             const char *src,
2020                                             const char *dst,
2021                                             route_extended_t e_route)
2022 {
2023   xbt_die("bypass routing not supported for Route-Based model");
2024 }
2025
2026 #define BUFFER_SIZE 4096        /* result buffer size */
2027 #define OVECCOUNT 30            /* should be a multiple of 3 */
2028
2029 static char *remplace(char *value, const char **src_list, int src_size,
2030                       const char **dst_list, int dst_size)
2031 {
2032
2033   char result_result[BUFFER_SIZE];
2034   int i_result_buffer;
2035   int value_length = (int) strlen(value);
2036   int number = 0;
2037
2038   int i = 0;
2039   i_result_buffer = 0;
2040   do {
2041     if (value[i] == '$') {
2042       i++;                      // skip $
2043
2044       // find the number      
2045       int number_length = 0;
2046       while ('0' <= value[i + number_length]
2047              && value[i + number_length] <= '9') {
2048         number_length++;
2049       }
2050       xbt_assert2(number_length != 0,
2051                   "bad string parameter, no number indication, at offset: %d (\"%s\")",
2052                   i, value);
2053
2054       // solve number
2055       number = atoi(value + i);
2056       i = i + number_length;
2057       xbt_assert2(i + 2 < value_length,
2058                   "bad string parameter, too few chars, at offset: %d (\"%s\")",
2059                   i, value);
2060
2061       // solve the indication
2062       const char **param_list;
2063       int param_size;
2064       if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2065         param_list = src_list;
2066         param_size = src_size;
2067       } else if (value[i] == 'd' && value[i + 1] == 's'
2068                  && value[i + 2] == 't') {
2069         param_list = dst_list;
2070         param_size = dst_size;
2071       } else {
2072         xbt_assert2(0,
2073                     "bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",
2074                     i, value);
2075       }
2076       i = i + 3;
2077
2078       xbt_assert4(param_size >= number,
2079                   "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2080                   i, value, param_size, number);
2081
2082       const char *param = param_list[number];
2083       int size = strlen(param);
2084       int cp;
2085       for (cp = 0; cp < size; cp++) {
2086         result_result[i_result_buffer] = param[cp];
2087         i_result_buffer++;
2088         if (i_result_buffer >= BUFFER_SIZE)
2089           break;
2090       }
2091     } else {
2092       result_result[i_result_buffer] = value[i];
2093       i_result_buffer++;
2094       i++;                      // next char
2095     }
2096
2097   } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2098
2099   xbt_assert2(i_result_buffer < BUFFER_SIZE,
2100               "solving string \"%s\", small buffer size (%d)", value,
2101               BUFFER_SIZE);
2102   result_result[i_result_buffer] = 0;
2103   return xbt_strdup(result_result);
2104 }
2105
2106 static route_extended_t rulebased_get_route(routing_component_t rc,
2107                                             const char *src,
2108                                             const char *dst);
2109 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2110 {
2111   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2112   routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2113
2114   xbt_dict_cursor_t c1 = NULL;
2115   char *k1, *d1;
2116
2117   //find router
2118   char *router = NULL;
2119   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2120     if (strstr (k1, "router")){
2121       router = k1;
2122     }
2123   }
2124   if (!router){
2125     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2126   }
2127
2128   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2129     route_extended_t route = rulebased_get_route (rc, router, k1);
2130
2131     int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2132     if (number_of_links != 3) {
2133       xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2134     }
2135
2136     void *link_ptr;
2137     xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2138     onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2139     onelink->src = xbt_strdup (k1);
2140     onelink->dst = xbt_strdup (router);
2141     onelink->link_ptr = link_ptr;
2142     xbt_dynar_push (ret, &onelink);
2143   }
2144   return ret;
2145 }
2146
2147 /* Business methods */
2148 static route_extended_t rulebased_get_route(routing_component_t rc,
2149                                             const char *src,
2150                                             const char *dst)
2151 {
2152   xbt_assert1(rc && src
2153               && dst,
2154               "Invalid params for \"get_route\" function at AS \"%s\"",
2155               rc->name);
2156
2157   /* set utils vars */
2158   routing_component_rulebased_t routing =
2159       (routing_component_rulebased_t) rc;
2160
2161   int are_processing_units;
2162   xbt_dynar_t rule_list;
2163   if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2164       && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2165     are_processing_units = 1;
2166     rule_list = routing->list_route;
2167   } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2168              && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2169                                      dst)) {
2170     are_processing_units = 0;
2171     rule_list = routing->list_ASroute;
2172   } else
2173     xbt_assert2(NULL,
2174                 "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
2175                 src, dst);
2176
2177   int rc_src = -1;
2178   int rc_dst = -1;
2179   int src_length = (int) strlen(src);
2180   int dst_length = (int) strlen(dst);
2181
2182   xbt_dynar_t links_list =
2183       xbt_dynar_new(global_routing->size_of_link, NULL);
2184
2185   rule_route_t ruleroute;
2186   unsigned int cpt;
2187   int ovector_src[OVECCOUNT];
2188   int ovector_dst[OVECCOUNT];
2189   const char **list_src = NULL;
2190   const char **list_dst = NULL;
2191   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2192     rc_src =
2193         pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2194                   ovector_src, OVECCOUNT);
2195     if (rc_src >= 0) {
2196       rc_dst =
2197           pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2198                     ovector_dst, OVECCOUNT);
2199       if (rc_dst >= 0) {
2200         xbt_assert1(!pcre_get_substring_list
2201                     (src, ovector_src, rc_src, &list_src),
2202                     "error solving substring list for src \"%s\"", src);
2203         xbt_assert1(!pcre_get_substring_list
2204                     (dst, ovector_dst, rc_dst, &list_dst),
2205                     "error solving substring list for src \"%s\"", dst);
2206         char *link_name;
2207         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2208           char *new_link_name =
2209               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2210           void *link =
2211               xbt_dict_get_or_null(surf_network_model->resource_set,
2212                                    new_link_name);
2213           if (link)
2214             xbt_dynar_push(links_list, &link);
2215           else
2216             THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2217           xbt_free(new_link_name);
2218         }
2219       }
2220     }
2221     if (rc_src >= 0 && rc_dst >= 0)
2222       break;
2223   }
2224
2225   route_extended_t new_e_route = NULL;
2226   if (rc_src >= 0 && rc_dst >= 0) {
2227     new_e_route = xbt_new0(s_route_extended_t, 1);
2228     new_e_route->generic_route.link_list = links_list;
2229   } else if (!strcmp(src, dst) && are_processing_units) {
2230     new_e_route = xbt_new0(s_route_extended_t, 1);
2231     xbt_dynar_push(links_list, &(global_routing->loopback));
2232     new_e_route->generic_route.link_list = links_list;
2233   } else {
2234     xbt_dynar_free(&link_list);
2235   }
2236
2237   if (!are_processing_units && new_e_route) {
2238     rule_route_extended_t ruleroute_extended =
2239         (rule_route_extended_t) ruleroute;
2240     new_e_route->src_gateway =
2241         remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2242                  list_dst, rc_dst);
2243     new_e_route->dst_gateway =
2244         remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2245                  list_dst, rc_dst);
2246   }
2247
2248   if (list_src)
2249     pcre_free_substring_list(list_src);
2250   if (list_dst)
2251     pcre_free_substring_list(list_dst);
2252
2253   return new_e_route;
2254 }
2255
2256 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2257                                                    const char *src,
2258                                                    const char *dst)
2259 {
2260   return NULL;
2261 }
2262
2263 static void rulebased_finalize(routing_component_t rc)
2264 {
2265   routing_component_rulebased_t routing =
2266       (routing_component_rulebased_t) rc;
2267   if (routing) {
2268     xbt_dict_free(&routing->dict_processing_units);
2269     xbt_dict_free(&routing->dict_autonomous_systems);
2270     xbt_dynar_free(&routing->list_route);
2271     xbt_dynar_free(&routing->list_ASroute);
2272     /* Delete structure */
2273     xbt_free(routing);
2274   }
2275 }
2276
2277 /* Creation routing model functions */
2278 static void *model_rulebased_create(void)
2279 {
2280   routing_component_rulebased_t new_component =
2281       xbt_new0(s_routing_component_rulebased_t, 1);
2282   new_component->generic_routing.set_processing_unit =
2283       model_rulebased_set_processing_unit;
2284   new_component->generic_routing.set_autonomous_system =
2285       model_rulebased_set_autonomous_system;
2286   new_component->generic_routing.set_route = model_rulebased_set_route;
2287   new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2288   new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2289   new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2290   new_component->generic_routing.get_route = rulebased_get_route;
2291   new_component->generic_routing.get_bypass_route = generic_get_bypassroute;       //rulebased_get_bypass_route;
2292   new_component->generic_routing.finalize = rulebased_finalize;
2293   /* initialization of internal structures */
2294   new_component->dict_processing_units = xbt_dict_new();
2295   new_component->dict_autonomous_systems = xbt_dict_new();
2296   new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2297   new_component->list_ASroute =
2298       xbt_dynar_new(sizeof(rule_route_extended_t),
2299                     &rule_route_extended_free);
2300   return new_component;
2301 }
2302
2303 static void model_rulebased_load(void)
2304 {
2305   /* use "surfxml_add_callback" to add a parse function call */
2306 }
2307
2308 static void model_rulebased_unload(void)
2309 {
2310   /* use "surfxml_del_callback" to remove a parse function call */
2311 }
2312
2313 static void model_rulebased_end(void)
2314 {
2315 }
2316
2317 #endif                          /* HAVE_PCRE_LIB */
2318
2319 /* ************************************************************************** */
2320 /* ******************************* NO ROUTING ******************************* */
2321
2322 /* Routing model structure */
2323 typedef struct {
2324   s_routing_component_t generic_routing;
2325 } s_routing_component_none_t, *routing_component_none_t;
2326
2327 /* Business methods */
2328 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2329 {
2330   return NULL;
2331 }
2332
2333 static route_extended_t none_get_route(routing_component_t rc,
2334                                        const char *src, const char *dst)
2335 {
2336   return NULL;
2337 }
2338
2339 static route_extended_t none_get_bypass_route(routing_component_t rc,
2340                                               const char *src,
2341                                               const char *dst)
2342 {
2343   return NULL;
2344 }
2345
2346 static void none_finalize(routing_component_t rc)
2347 {
2348   xbt_free(rc);
2349 }
2350
2351 static void none_set_processing_unit(routing_component_t rc,
2352                                      const char *name)
2353 {
2354 }
2355
2356 static void none_set_autonomous_system(routing_component_t rc,
2357                                        const char *name)
2358 {
2359 }
2360
2361 /* Creation routing model functions */
2362 static void *model_none_create(void)
2363 {
2364   routing_component_none_t new_component =
2365       xbt_new0(s_routing_component_none_t, 1);
2366   new_component->generic_routing.set_processing_unit =
2367       none_set_processing_unit;
2368   new_component->generic_routing.set_autonomous_system =
2369       none_set_autonomous_system;
2370   new_component->generic_routing.set_route = NULL;
2371   new_component->generic_routing.set_ASroute = NULL;
2372   new_component->generic_routing.set_bypassroute = NULL;
2373   new_component->generic_routing.get_route = none_get_route;
2374   new_component->generic_routing.get_onelink_routes =
2375       none_get_onelink_routes;
2376   new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2377   new_component->generic_routing.finalize = none_finalize;
2378   return new_component;
2379 }
2380
2381 static void model_none_load(void)
2382 {
2383 }
2384
2385 static void model_none_unload(void)
2386 {
2387 }
2388
2389 static void model_none_end(void)
2390 {
2391 }
2392
2393 /* ************************************************** */
2394 /* ********** PATERN FOR NEW ROUTING **************** */
2395
2396 /* The minimal configuration of a new routing model need the next functions,
2397  * also you need to set at the start of the file, the new model in the model
2398  * list. Remember keep the null ending of the list.
2399  */
2400 /*** Routing model structure ***/
2401 // typedef struct {
2402 //   s_routing_component_t generic_routing;
2403 //   /* things that your routing model need */
2404 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2405
2406 /*** Parse routing model functions ***/
2407 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2408 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2409 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2410 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2411 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2412
2413 /*** Business methods ***/
2414 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2415 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2416 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2417
2418 /*** Creation routing model functions ***/
2419 // static void* model_NEW_create(void) {
2420 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
2421 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2422 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2423 //   new_component->generic_routing.set_route = model_NEW_set_route;
2424 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2425 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2426 //   new_component->generic_routing.get_route = NEW_get_route;
2427 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2428 //   new_component->generic_routing.finalize = NEW_finalize;
2429 //   /* initialization of internal structures */
2430 //   return new_component;
2431 // } /* mandatory */
2432 // static void  model_NEW_load(void) {}   /* mandatory */
2433 // static void  model_NEW_unload(void) {} /* mandatory */
2434 // static void  model_NEW_end(void) {}    /* mandatory */
2435
2436 /* ************************************************************************** */
2437 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2438
2439 static void generic_set_processing_unit(routing_component_t rc,
2440                                         const char *name)
2441 {
2442   DEBUG1("Load process unit \"%s\"", name);
2443   int *id = xbt_new0(int, 1);
2444   xbt_dict_t _to_index;
2445   _to_index = current_routing->to_index;
2446   *id = xbt_dict_length(_to_index);
2447   xbt_dict_set(_to_index, name, id, xbt_free);
2448 }
2449
2450 static void generic_set_autonomous_system(routing_component_t rc,
2451                                           const char *name)
2452 {
2453   DEBUG1("Load Autonomous system \"%s\"", name);
2454   int *id = xbt_new0(int, 1);
2455   xbt_dict_t _to_index;
2456   _to_index = current_routing->to_index;
2457   *id = xbt_dict_length(_to_index);
2458   xbt_dict_set(_to_index, name, id, xbt_free);
2459 }
2460
2461 static void generic_set_route(routing_component_t rc, const char *src,
2462                               const char *dst, route_t route)
2463 {
2464   DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2465   xbt_dict_t _parse_routes;
2466   xbt_dict_t _to_index;
2467   char *route_name;
2468   int *src_id, *dst_id;
2469   unsigned long nb_links = xbt_dynar_length(route->link_list);
2470
2471   _to_index = current_routing->to_index;
2472   _parse_routes = current_routing->parse_routes;
2473   src_id = xbt_dict_get_or_null(_to_index, src);
2474   dst_id = xbt_dict_get_or_null(_to_index, dst);
2475   routing_component_full_t routing = ((routing_component_full_t) current_routing);
2476   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
2477   route_t route_to_test;
2478
2479   xbt_assert2(src_id
2480               && dst_id, "Network elements %s or %s not found", src, dst);
2481
2482   xbt_assert2(xbt_dynar_length(route->link_list) > 0,
2483               "Invalid count of links, must be greater than zero (%s,%s)",
2484               src, dst);
2485
2486   if(!strcmp(rc->routing->name,"Full"))
2487   {
2488           /* Create the routing table */
2489           if(!routing->routing_table)
2490                   routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
2491
2492           route_to_test = &(TO_ROUTE_FULL(*src_id, *dst_id))->generic_route;
2493   }
2494   else
2495           route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2496
2497   if(route_to_test)
2498   xbt_assert2(!xbt_dynar_compare(
2499                   (void*)route->link_list,
2500                   (void*)route_to_test->link_list,
2501                   (int_f_cpvoid_cpvoid_t) strcmp),
2502           "The route between \"%s\" and \"%s\" already exist", src,dst);
2503
2504   if(!strcmp(rc->routing->name,"Full"))
2505   {
2506           TO_ROUTE_FULL(*src_id, *dst_id) =
2507                 generic_new_extended_route(current_routing->hierarchy, route, 1);
2508   }
2509   else
2510   {
2511           route_name = bprintf("%d#%d", *src_id, *dst_id);
2512           xbt_dict_set(_parse_routes, route_name, route, NULL);
2513           xbt_free(route_name);
2514   }
2515
2516   if(A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES)
2517   {
2518           int i;
2519           route_t route_sym = xbt_new0(s_route_t, 1);
2520           route_sym->link_list = xbt_dynar_new(sizeof(char *),NULL);
2521           for(i=nb_links ; i>0 ; i--)
2522           {
2523                  char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(route->link_list, i-1, char *)));
2524                  link_name = xbt_strdup(xbt_dynar_get_as(route->link_list, i-1, char *));
2525                  xbt_dynar_push_as(route_sym->link_list ,char *, link_name);
2526           }
2527           DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
2528
2529           if(!strcmp(rc->routing->name,"Full"))
2530           {
2531                   TO_ROUTE_FULL(*dst_id, *src_id) =
2532                                 generic_new_extended_route(current_routing->hierarchy, route_sym, 1);
2533           }
2534           else
2535           {
2536                   route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d",*dst_id, *src_id));
2537                   if(route_to_test)
2538                   xbt_assert2(!xbt_dynar_compare(
2539                                   (void*)route_sym->link_list,
2540                                   (void*)route_to_test->link_list,
2541                                   (int_f_cpvoid_cpvoid_t) strcmp),
2542                           "The route between \"%s\" and \"%s\" already exist", dst,src);
2543
2544                   xbt_dict_set(_parse_routes, bprintf("%d#%d",*dst_id, *src_id), route_sym, NULL);
2545           }
2546   }
2547 }
2548
2549 static void generic_set_ASroute(routing_component_t rc, const char *src,
2550                                 const char *dst, route_extended_t e_route)
2551 {
2552   DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2553          e_route->src_gateway, dst, e_route->dst_gateway);
2554
2555   xbt_dict_t _parse_routes;
2556   xbt_dict_t _to_index;
2557   char *route_name;
2558   int *src_id, *dst_id;
2559   _to_index = current_routing->to_index;
2560   _parse_routes = current_routing->parse_routes;
2561
2562   src_id = xbt_dict_get_or_null(_to_index, src);
2563   dst_id = xbt_dict_get_or_null(_to_index, dst);
2564   routing_component_full_t routing = ((routing_component_full_t) current_routing);
2565   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
2566   route_t route_to_test;
2567
2568   xbt_assert2(src_id
2569               && dst_id, "Network elements %s or %s not found", src, dst);
2570
2571   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2572               "Invalid count of links, must be greater than zero (%s,%s)",
2573               src, dst);
2574
2575   if(!strcmp(rc->routing->name,"Full"))
2576   {
2577           /* Create the routing table */
2578           if(!routing->routing_table)
2579                   routing->routing_table =  xbt_new0(route_extended_t, table_size * table_size);
2580
2581           route_to_test = &((route_extended_t)(TO_ROUTE_FULL(*src_id, *dst_id)))->generic_route;
2582   }
2583   else
2584           route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2585
2586   if(route_to_test)
2587   xbt_assert4(!xbt_dynar_compare(
2588                   (void*) (&e_route->generic_route)->link_list,
2589                   (void*) route_to_test->link_list,
2590                   (int_f_cpvoid_cpvoid_t) strcmp),
2591                   "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2592                                 src, e_route->src_gateway, dst, e_route->dst_gateway);
2593
2594   if(!strcmp(rc->routing->name,"Full"))
2595   {
2596           TO_ROUTE_FULL(*src_id, *dst_id) =
2597                           generic_new_extended_route(current_routing->hierarchy, &(e_route->generic_route), 1);
2598   }
2599   else
2600   {
2601   route_name = bprintf("%d#%d", *src_id, *dst_id);
2602   xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2603   xbt_free(route_name);
2604   }
2605
2606   unsigned long nb_links = xbt_dynar_length(e_route->generic_route.link_list);
2607
2608   if(A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES)
2609   {
2610           int i;
2611           route_extended_t route_sym = xbt_new0(s_route_extended_t, 1);
2612           route_sym->generic_route.link_list = xbt_dynar_new(sizeof(char *),NULL);
2613           for(i=nb_links ; i>0 ; i--)
2614           {
2615                  char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *)));
2616                  link_name = bprintf("%s",xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *));
2617                  xbt_dynar_push_as(route_sym->generic_route.link_list ,char *, link_name);
2618           }
2619           route_sym->src_gateway = xbt_new0( char,strlen(e_route->dst_gateway) );
2620           route_sym->src_gateway = bprintf("%s",e_route->dst_gateway);
2621           route_sym->dst_gateway = xbt_new0( char,strlen(e_route->src_gateway) );
2622           route_sym->dst_gateway = bprintf("%s",e_route->src_gateway);
2623           DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",dst, route_sym->src_gateway,src,route_sym->dst_gateway);
2624
2625           if(!strcmp(rc->routing->name,"Full"))
2626           {
2627                   TO_ROUTE_FULL(*dst_id, *src_id) =
2628                                 generic_new_extended_route(current_routing->hierarchy, &(route_sym->generic_route), 1);
2629           }
2630           else
2631           {
2632                   route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d", *dst_id, *src_id));
2633                   if(route_to_test)
2634                   xbt_assert4(!xbt_dynar_compare(
2635                                   (void*) (&route_sym->generic_route)->link_list,
2636                                   (void*) route_to_test->link_list,
2637                                   (int_f_cpvoid_cpvoid_t) strcmp),
2638                                   "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2639                                                                   dst, route_sym->src_gateway, src, route_sym->dst_gateway);
2640                   xbt_dict_set(_parse_routes, bprintf("%d#%d", *dst_id, *src_id), route_sym, NULL);
2641           }
2642    }
2643 }
2644
2645 static void generic_set_bypassroute(routing_component_t rc,
2646                                     const char *src, const char *dst,
2647                                     route_extended_t e_route)
2648 {
2649   DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2650   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2651   char *route_name;
2652
2653   route_name = bprintf("%s#%s", src, dst);
2654   xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2655               "Invalid count of links, must be greater than zero (%s,%s)",
2656               src, dst);
2657   xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2658               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2659               src, e_route->src_gateway, dst, e_route->dst_gateway);
2660
2661   route_extended_t new_e_route =
2662       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2663   xbt_dynar_free(&(e_route->generic_route.link_list));
2664   xbt_free(e_route);
2665
2666   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2667                (void (*)(void *)) generic_free_extended_route);
2668   xbt_free(route_name);
2669 }
2670
2671 /* ************************************************************************** */
2672 /* *********************** GENERIC BUSINESS METHODS ************************* */
2673
2674 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2675 {
2676   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2677 }
2678
2679 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2680                                                 const char *src,
2681                                                 const char *dst)
2682 {
2683   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2684   routing_component_t src_as, dst_as;
2685   int index_src, index_dst;
2686   xbt_dynar_t path_src = NULL;
2687   xbt_dynar_t path_dst = NULL;
2688   routing_component_t current = NULL;
2689   routing_component_t *current_src = NULL;
2690   routing_component_t *current_dst = NULL;
2691
2692   /* (1) find the as where the src and dst are located */
2693   src_as = ((network_element_info_t)
2694             xbt_dict_get_or_null(global_routing->where_network_elements,
2695                                  src))->rc_component;
2696   dst_as = ((network_element_info_t)
2697             xbt_dict_get_or_null(global_routing->where_network_elements,
2698                                  dst))->rc_component;
2699   xbt_assert2(src_as
2700               && dst_as,
2701               "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2702               dst);
2703
2704   /* (2) find the path to the root routing component */
2705   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2706   current = src_as;
2707   while (current != NULL) {
2708     xbt_dynar_push(path_src, &current);
2709     current = current->routing_father;
2710   }
2711   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2712   current = dst_as;
2713   while (current != NULL) {
2714     xbt_dynar_push(path_dst, &current);
2715     current = current->routing_father;
2716   }
2717
2718   /* (3) find the common father */
2719   index_src = (path_src->used) - 1;
2720   index_dst = (path_dst->used) - 1;
2721   current_src = xbt_dynar_get_ptr(path_src, index_src);
2722   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2723   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2724     routing_component_t *tmp_src, *tmp_dst;
2725     tmp_src = xbt_dynar_pop_ptr(path_src);
2726     tmp_dst = xbt_dynar_pop_ptr(path_dst);
2727     index_src--;
2728     index_dst--;
2729     current_src = xbt_dynar_get_ptr(path_src, index_src);
2730     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2731   }
2732
2733   int max_index_src = (path_src->used) - 1;
2734   int max_index_dst = (path_dst->used) - 1;
2735
2736   int max_index = max(max_index_src, max_index_dst);
2737   int i, max;
2738
2739   route_extended_t e_route_bypass = NULL;
2740
2741   for (max = 0; max <= max_index; max++) {
2742     for (i = 0; i < max; i++) {
2743       if (i <= max_index_src && max <= max_index_dst) {
2744         char *route_name = bprintf("%s#%s",
2745                                    (*(routing_component_t *)
2746                                     (xbt_dynar_get_ptr
2747                                      (path_src, i)))->name,
2748                                    (*(routing_component_t *)
2749                                     (xbt_dynar_get_ptr
2750                                      (path_dst, max)))->name);
2751         e_route_bypass =
2752             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2753         xbt_free(route_name);
2754       }
2755       if (e_route_bypass)
2756         break;
2757       if (max <= max_index_src && i <= max_index_dst) {
2758         char *route_name = bprintf("%s#%s",
2759                                    (*(routing_component_t *)
2760                                     (xbt_dynar_get_ptr
2761                                      (path_src, max)))->name,
2762                                    (*(routing_component_t *)
2763                                     (xbt_dynar_get_ptr
2764                                      (path_dst, i)))->name);
2765         e_route_bypass =
2766             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2767         xbt_free(route_name);
2768       }
2769       if (e_route_bypass)
2770         break;
2771     }
2772
2773     if (e_route_bypass)
2774       break;
2775
2776     if (max <= max_index_src && max <= max_index_dst) {
2777       char *route_name = bprintf("%s#%s",
2778                                  (*(routing_component_t *)
2779                                   (xbt_dynar_get_ptr
2780                                    (path_src, max)))->name,
2781                                  (*(routing_component_t *)
2782                                   (xbt_dynar_get_ptr
2783                                    (path_dst, max)))->name);
2784       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2785       xbt_free(route_name);
2786     }
2787     if (e_route_bypass)
2788       break;
2789   }
2790
2791   xbt_dynar_free(&path_src);
2792   xbt_dynar_free(&path_dst);
2793
2794   route_extended_t new_e_route = NULL;
2795
2796   if (e_route_bypass) {
2797     void *link;
2798     unsigned int cpt = 0;
2799     new_e_route = xbt_new0(s_route_extended_t, 1);
2800     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2801     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2802     new_e_route->generic_route.link_list =
2803         xbt_dynar_new(global_routing->size_of_link, NULL);
2804     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2805       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2806     }
2807   }
2808
2809   return new_e_route;
2810 }
2811
2812 /* ************************************************************************** */
2813 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2814
2815 static route_extended_t
2816 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2817                            void *data, int order)
2818 {
2819
2820   char *link_name;
2821   route_extended_t e_route, new_e_route;
2822   route_t route;
2823   unsigned int cpt;
2824   xbt_dynar_t links = NULL, links_id = NULL;
2825
2826   new_e_route = xbt_new0(s_route_extended_t, 1);
2827   new_e_route->generic_route.link_list =
2828       xbt_dynar_new(global_routing->size_of_link, NULL);
2829   new_e_route->src_gateway = NULL;
2830   new_e_route->dst_gateway = NULL;
2831
2832   xbt_assert0(hierarchy == SURF_ROUTING_BASE
2833               || hierarchy == SURF_ROUTING_RECURSIVE,
2834               "the hierarchy type is not defined");
2835
2836   if (hierarchy == SURF_ROUTING_BASE) {
2837
2838     route = (route_t) data;
2839     links = route->link_list;
2840
2841   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2842
2843     e_route = (route_extended_t) data;
2844     xbt_assert0(e_route->src_gateway
2845                 && e_route->dst_gateway, "bad gateway, is null");
2846     links = e_route->generic_route.link_list;
2847
2848     /* remeber not erase the gateway names */
2849     new_e_route->src_gateway = e_route->src_gateway;
2850     new_e_route->dst_gateway = e_route->dst_gateway;
2851   }
2852
2853   links_id = new_e_route->generic_route.link_list;
2854
2855   xbt_dynar_foreach(links, cpt, link_name) {
2856
2857     void *link =
2858         xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2859     if (link) {
2860       if (order)
2861         xbt_dynar_push(links_id, &link);
2862       else
2863         xbt_dynar_unshift(links_id, &link);
2864     } else
2865       THROW1(mismatch_error, 0, "Link %s not found", link_name);
2866   }
2867
2868   return new_e_route;
2869 }
2870
2871 static void generic_free_route(route_t route)
2872 {
2873   if (route) {
2874     xbt_dynar_free(&(route->link_list));
2875     xbt_free(route);
2876   }
2877 }
2878
2879 static void generic_free_extended_route(route_extended_t e_route)
2880 {
2881   if (e_route) {
2882     xbt_dynar_free(&(e_route->generic_route.link_list));
2883     if (e_route->src_gateway)
2884       xbt_free(e_route->src_gateway);
2885     if (e_route->dst_gateway)
2886       xbt_free(e_route->dst_gateway);
2887     xbt_free(e_route);
2888   }
2889 }
2890
2891 static routing_component_t generic_as_exist(routing_component_t find_from,
2892                                             routing_component_t to_find)
2893 {
2894   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2895   xbt_dict_cursor_t cursor = NULL;
2896   char *key;
2897   int found = 0;
2898   routing_component_t elem;
2899   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2900     if (to_find == elem || generic_as_exist(elem, to_find)) {
2901       found = 1;
2902       break;
2903     }
2904   }
2905   if (found)
2906     return to_find;
2907   return NULL;
2908 }
2909
2910 static routing_component_t
2911 generic_autonomous_system_exist(routing_component_t rc, char *element)
2912 {
2913   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2914   routing_component_t element_as, result, elem;
2915   xbt_dict_cursor_t cursor = NULL;
2916   char *key;
2917   element_as = ((network_element_info_t)
2918                 xbt_dict_get_or_null
2919                 (global_routing->where_network_elements,
2920                  element))->rc_component;
2921   result = ((routing_component_t) - 1);
2922   if (element_as != rc)
2923     result = generic_as_exist(rc, element_as);
2924
2925   int found = 0;
2926   if (result) {
2927     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2928       found = !strcmp(elem->name, element);
2929       if (found)
2930         break;
2931     }
2932     if (found)
2933       return element_as;
2934   }
2935   return NULL;
2936 }
2937
2938 static routing_component_t
2939 generic_processing_units_exist(routing_component_t rc, char *element)
2940 {
2941   routing_component_t element_as;
2942   element_as = ((network_element_info_t)
2943                 xbt_dict_get_or_null
2944                 (global_routing->where_network_elements,
2945                  element))->rc_component;
2946   if (element_as == rc)
2947     return element_as;
2948   return generic_as_exist(rc, element_as);
2949 }
2950
2951 static void generic_src_dst_check(routing_component_t rc, const char *src,
2952                                   const char *dst)
2953 {
2954
2955   routing_component_t src_as = ((network_element_info_t)
2956                                 xbt_dict_get_or_null
2957                                 (global_routing->where_network_elements,
2958                                  src))->rc_component;
2959   routing_component_t dst_as = ((network_element_info_t)
2960                                 xbt_dict_get_or_null
2961                                 (global_routing->where_network_elements,
2962                                  dst))->rc_component;
2963
2964   xbt_assert3(src_as != NULL && dst_as != NULL,
2965               "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2966               src, dst, rc->name);
2967   xbt_assert4(src_as == dst_as,
2968               "The src(%s in %s) and dst(%s in %s) are in differents AS",
2969               src, src_as->name, dst, dst_as->name);
2970   xbt_assert2(rc == dst_as,
2971               "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2972               rc->name, dst_as->name);
2973 }
2974
2975 static void routing_parse_Sconfig(void)
2976 {
2977   //TODO
2978   DEBUG0("WARNING tag config not yet implemented.");
2979   DEBUG1("Configuration name = %s",A_surfxml_config_id);
2980 }
2981
2982 static void routing_parse_Econfig(void)
2983 {
2984   //TODO
2985   xbt_dict_cursor_t cursor = NULL;
2986   char *key;
2987   char *elem;
2988   xbt_dict_foreach(current_property_set, cursor, key, elem) {
2989           DEBUG2("property : %s = %s",key,elem);
2990         }
2991 }
2992
2993 static void routing_parse_Scluster(void)
2994 {
2995   static int AX_ptr = 0;
2996
2997   char *cluster_id = A_surfxml_cluster_id;
2998   char *cluster_prefix = A_surfxml_cluster_prefix;
2999   char *cluster_suffix = A_surfxml_cluster_suffix;
3000   char *cluster_radical = A_surfxml_cluster_radical;
3001   char *cluster_power = A_surfxml_cluster_power;
3002   char *cluster_bw = A_surfxml_cluster_bw;
3003   char *cluster_lat = A_surfxml_cluster_lat;
3004   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
3005   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
3006   char *host_id, *groups, *link_id = NULL;
3007   char *router_id, *link_router, *link_backbone, *route_src_dst;
3008   unsigned int iter;
3009   int start, end, i;
3010   xbt_dynar_t radical_elements;
3011   xbt_dynar_t radical_ends;
3012   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
3013   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
3014
3015 #ifndef HAVE_PCRE_LIB
3016   xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
3017   char *route_src, *route_dst;
3018   int j;
3019 #endif
3020
3021   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3022   static unsigned int surfxml_buffer_stack_stack[1024];
3023
3024   surfxml_buffer_stack_stack[0] = 0;
3025
3026   surfxml_bufferstack_push(1);
3027
3028   SURFXML_BUFFER_SET(AS_id, cluster_id);
3029 #ifdef HAVE_PCRE_LIB
3030   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
3031   DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
3032 #else
3033   SURFXML_BUFFER_SET(AS_routing, "Full");
3034   DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3035 #endif
3036   SURFXML_START_TAG(AS);
3037
3038   radical_elements = xbt_str_split(cluster_radical, ",");
3039   xbt_dynar_foreach(radical_elements, iter, groups) {
3040     radical_ends = xbt_str_split(groups, "-");
3041     switch (xbt_dynar_length(radical_ends)) {
3042     case 1:
3043       surf_parse_get_int(&start,
3044                          xbt_dynar_get_as(radical_ends, 0, char *));
3045       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3046 #ifndef HAVE_PCRE_LIB
3047       xbt_dynar_push_as(tab_elements_num, int, start);
3048 #endif
3049       link_id = bprintf("%s_link_%d", cluster_id, start);
3050
3051       DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3052       A_surfxml_host_state = A_surfxml_host_state_ON;
3053       SURFXML_BUFFER_SET(host_id, host_id);
3054       SURFXML_BUFFER_SET(host_power, cluster_power);
3055       SURFXML_BUFFER_SET(host_availability, "1.0");
3056       SURFXML_BUFFER_SET(host_availability_file, "");
3057       SURFXML_BUFFER_SET(host_state_file, "");
3058       SURFXML_START_TAG(host);
3059       SURFXML_END_TAG(host);
3060
3061       DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3062       A_surfxml_link_state = A_surfxml_link_state_ON;
3063       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3064       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3065           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3066       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3067           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3068       SURFXML_BUFFER_SET(link_id, link_id);
3069       SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3070       SURFXML_BUFFER_SET(link_latency, cluster_lat);
3071       SURFXML_BUFFER_SET(link_bandwidth_file, "");
3072       SURFXML_BUFFER_SET(link_latency_file, "");
3073       SURFXML_BUFFER_SET(link_state_file, "");
3074       SURFXML_START_TAG(link);
3075       SURFXML_END_TAG(link);
3076
3077       break;
3078
3079     case 2:
3080
3081       surf_parse_get_int(&start,
3082                          xbt_dynar_get_as(radical_ends, 0, char *));
3083       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3084       DEBUG2("Create hosts and links from %d to %d", start, end);
3085       for (i = start; i <= end; i++) {
3086         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3087 #ifndef HAVE_PCRE_LIB
3088         xbt_dynar_push_as(tab_elements_num, int, i);
3089 #endif
3090         link_id = bprintf("%s_link_%d", cluster_id, i);
3091
3092         DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3093         A_surfxml_host_state = A_surfxml_host_state_ON;
3094         SURFXML_BUFFER_SET(host_id, host_id);
3095         SURFXML_BUFFER_SET(host_power, cluster_power);
3096         SURFXML_BUFFER_SET(host_availability, "1.0");
3097         SURFXML_BUFFER_SET(host_availability_file, "");
3098         SURFXML_BUFFER_SET(host_state_file, "");
3099         SURFXML_START_TAG(host);
3100         SURFXML_END_TAG(host);
3101
3102         DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3103         A_surfxml_link_state = A_surfxml_link_state_ON;
3104         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3105         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3106             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3107         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3108             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3109         SURFXML_BUFFER_SET(link_id, link_id);
3110         SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3111         SURFXML_BUFFER_SET(link_latency, cluster_lat);
3112         SURFXML_BUFFER_SET(link_bandwidth_file, "");
3113         SURFXML_BUFFER_SET(link_latency_file, "");
3114         SURFXML_BUFFER_SET(link_state_file, "");
3115         SURFXML_START_TAG(link);
3116         SURFXML_END_TAG(link);
3117       }
3118       break;
3119
3120     default:
3121       DEBUG0("Malformed radical");
3122     }
3123
3124     xbt_dynar_free(&radical_ends);
3125   }
3126
3127   DEBUG0(" ");
3128   router_id =
3129       bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3130               cluster_suffix);
3131   link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3132   link_backbone = bprintf("%s_backbone", cluster_id);
3133
3134   DEBUG1("<router id=\"%s\"/>", router_id);
3135   SURFXML_BUFFER_SET(router_id, router_id);;
3136   SURFXML_START_TAG(router);
3137   SURFXML_END_TAG(router);
3138
3139   DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,cluster_bw, cluster_lat);
3140   A_surfxml_link_state = A_surfxml_link_state_ON;
3141   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3142   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3143   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
3144   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3145   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3146   SURFXML_BUFFER_SET(link_id, link_router);
3147   SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3148   SURFXML_BUFFER_SET(link_latency, cluster_lat);
3149   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3150   SURFXML_BUFFER_SET(link_latency_file, "");
3151   SURFXML_BUFFER_SET(link_state_file, "");
3152   SURFXML_START_TAG(link);
3153   SURFXML_END_TAG(link);
3154
3155   DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bw, cluster_lat);
3156   A_surfxml_link_state = A_surfxml_link_state_ON;
3157   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3158   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3159   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
3160   SURFXML_BUFFER_SET(link_id, link_backbone);
3161   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3162   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3163   SURFXML_BUFFER_SET(link_bandwidth_file, "");
3164   SURFXML_BUFFER_SET(link_latency_file, "");
3165   SURFXML_BUFFER_SET(link_state_file, "");
3166   SURFXML_START_TAG(link);
3167   SURFXML_END_TAG(link);
3168
3169   char *new_suffix = bprintf("%s", "");
3170
3171   radical_elements = xbt_str_split(cluster_suffix, ".");
3172   xbt_dynar_foreach(radical_elements, iter, groups) {
3173     if (strcmp(groups, "")) {
3174       new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3175     }
3176   }
3177   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3178
3179   DEBUG0(" ");
3180
3181 #ifdef HAVE_PCRE_LIB
3182
3183   DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3184   DEBUG0("symetrical=\"NO\">");
3185   SURFXML_BUFFER_SET(route_src, route_src_dst);
3186   SURFXML_BUFFER_SET(route_dst, route_src_dst);
3187   A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3188   SURFXML_START_TAG(route);
3189
3190   DEBUG1("<link_ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3191   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3192   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3193   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3194   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3195   SURFXML_START_TAG(link_ctn);
3196   SURFXML_END_TAG(link_ctn);
3197
3198   DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3199   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3200   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3201   SURFXML_START_TAG(link_ctn);
3202   SURFXML_END_TAG(link_ctn);
3203
3204   DEBUG1("<link_ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3205   SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3206   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3207   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3208   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3209   SURFXML_START_TAG(link_ctn);
3210   SURFXML_END_TAG(link_ctn);
3211
3212   DEBUG0("</route>");
3213   SURFXML_END_TAG(route);
3214 #else
3215   for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3216     for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3217       if (i == xbt_dynar_length(tab_elements_num)) {
3218         route_src = router_id;
3219       } else {
3220         route_src =
3221             bprintf("%s%d%s", cluster_prefix,
3222                     xbt_dynar_get_as(tab_elements_num, i, int),
3223                     cluster_suffix);
3224       }
3225
3226       if (j == xbt_dynar_length(tab_elements_num)) {
3227         route_dst = router_id;
3228       } else {
3229         route_dst =
3230             bprintf("%s%d%s", cluster_prefix,
3231                     xbt_dynar_get_as(tab_elements_num, j, int),
3232                     cluster_suffix);
3233       }
3234
3235       DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3236       DEBUG0("symetrical=\"NO\">");
3237       SURFXML_BUFFER_SET(route_src, route_src);
3238       SURFXML_BUFFER_SET(route_dst, route_dst);
3239       A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3240       SURFXML_START_TAG(route);
3241
3242       if (i == xbt_dynar_length(tab_elements_num)) {
3243         route_src = link_router;
3244       } else {
3245         route_src =
3246             bprintf("%s_link_%d", cluster_id,
3247                     xbt_dynar_get_as(tab_elements_num, i, int));
3248       }
3249
3250       if (j == xbt_dynar_length(tab_elements_num)) {
3251         route_dst = link_router;
3252       } else {
3253         route_dst =
3254             bprintf("%s_link_%d", cluster_id,
3255                     xbt_dynar_get_as(tab_elements_num, j, int));
3256       }
3257
3258       DEBUG1("<link_ctn\tid=\"%s\"/>", route_src);
3259       SURFXML_BUFFER_SET(link_ctn_id, route_src);
3260       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3261       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3262       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3263       SURFXML_START_TAG(link_ctn);
3264       SURFXML_END_TAG(link_ctn);
3265
3266       DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3267       SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3268       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3269       SURFXML_START_TAG(link_ctn);
3270       SURFXML_END_TAG(link_ctn);
3271
3272       DEBUG1("<link_ctn\tid=\"%s\"/>", route_dst);
3273       SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3274       A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3275       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3276       {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3277       SURFXML_START_TAG(link_ctn);
3278       SURFXML_END_TAG(link_ctn);
3279
3280       DEBUG0("</route>");
3281       SURFXML_END_TAG(route);
3282     }
3283   }
3284   xbt_dynar_free(&tab_elements_num);
3285 #endif
3286
3287   DEBUG0("</AS>");
3288   SURFXML_END_TAG(AS);
3289   DEBUG0(" ");
3290
3291   surfxml_bufferstack_pop(1);
3292 }
3293
3294 /*
3295  * New methods to init the routing model component from the lua script
3296  */
3297
3298 /*
3299  * calling parse_S_AS_lua with lua values
3300  */
3301 void routing_AS_init(const char *AS_id, const char *AS_routing)
3302 {
3303   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3304 }
3305
3306 /*
3307  * calling parse_E_AS_lua to fisnish the creation of routing component
3308  */
3309 void routing_AS_end(const char *AS_id)
3310 {
3311   parse_E_AS_lua((char *) AS_id);
3312 }
3313
3314 /*
3315  * add a host to the network element list
3316  */
3317
3318 void routing_add_host(const char *host_id)
3319 {
3320   parse_S_host_lua((char *) host_id);
3321 }
3322
3323 /*
3324  * Set a new link on the actual list of link for a route or ASroute
3325  */
3326 void routing_add_link(const char *link_id)
3327 {
3328   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3329 }
3330
3331 /*
3332  *Set the endpoints for a route
3333  */
3334 void routing_set_route(const char *src_id, const char *dst_id)
3335 {
3336   parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3337 }
3338
3339 /*
3340  * Store the route by calling parse_E_route_store_route
3341  */
3342 void routing_store_route(void)
3343 {
3344   parse_E_route_store_route();
3345 }