Logo AND Algorithmique Numérique Distribuée

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