Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
and now with the right test so that not all our tests fail
[simgrid.git] / src / surf / surf_routing_full.c
1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf_routing_private.h"
8
9 /* Global vars */
10 extern routing_global_t global_routing;
11 extern int surf_parse_lineno;
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
14
15 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
16
17 /* Routing model structure */
18
19 typedef struct s_routing_component_full {
20   s_as_t generic_routing;
21   route_t *routing_table;
22 } s_routing_component_full_t, *routing_component_full_t;
23
24 /* Business methods */
25 static xbt_dynar_t full_get_onelink_routes(AS_t rc)
26 {
27   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
28
29   routing_component_full_t routing = (routing_component_full_t) rc;
30   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
31   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
32   char *k1, *d1, *k2, *d2;
33   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
34     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
35       int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
36       int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
37       xbt_assert(src_id
38                   && dst_id,
39                   "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
40                   k1, k2);
41       route_t route = TO_ROUTE_FULL(*src_id, *dst_id);
42       if (route) {
43         if (xbt_dynar_length(route->link_list) == 1) {
44           void *link =
45               *(void **) xbt_dynar_get_ptr(route->link_list,
46                                            0);
47           onelink_t onelink = xbt_new0(s_onelink_t, 1);
48           onelink->link_ptr = link;
49           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
50             onelink->src = xbt_strdup(k1);
51             onelink->dst = xbt_strdup(k2);
52           } else if (routing->generic_routing.hierarchy ==
53                      SURF_ROUTING_RECURSIVE) {
54             onelink->src = xbt_strdup(route->src_gateway);
55             onelink->dst = xbt_strdup(route->dst_gateway);
56           }
57           xbt_dynar_push(ret, &onelink);
58         }
59       }
60     }
61   }
62   return ret;
63 }
64
65 static void full_get_route_and_latency(AS_t rc,
66                            const char *src, const char *dst,
67                            route_t res,double *lat)
68 {
69
70   /* set utils vars */
71   routing_component_full_t routing = (routing_component_full_t) rc;
72   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
73
74   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
75   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
76
77   if (!src_id || !dst_id)
78     THROWF(arg_error,0,"No route from '%s' to '%s'",src,dst);
79
80   route_t e_route = NULL;
81   void *link;
82   unsigned int cpt = 0;
83
84   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
85
86   if (e_route) {
87     res->src_gateway = xbt_strdup(e_route->src_gateway);
88     res->dst_gateway = xbt_strdup(e_route->dst_gateway);
89     xbt_dynar_foreach(e_route->link_list, cpt, link) {
90       xbt_dynar_push(res->link_list, &link);
91       if (lat)
92         *lat += surf_network_model->extension.network.get_link_latency(link);
93     }
94   }
95 }
96
97 static void full_finalize(AS_t rc)
98 {
99   routing_component_full_t routing = (routing_component_full_t) rc;
100   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
101   int i, j;
102   if (routing) {
103     /* Delete routing table */
104     for (i = 0; i < table_size; i++)
105       for (j = 0; j < table_size; j++)
106         generic_free_route(TO_ROUTE_FULL(i, j));
107     xbt_free(routing->routing_table);
108     model_generic_finalize(rc);
109   }
110 }
111
112 /* Creation routing model functions */
113
114 AS_t model_full_create(void)
115 {
116   routing_component_full_t new_component = (routing_component_full_t)
117       model_generic_create_sized(sizeof(s_routing_component_full_t));
118
119   new_component->generic_routing.parse_route = model_full_set_route;
120   new_component->generic_routing.parse_ASroute = model_full_set_route;
121   new_component->generic_routing.get_route_and_latency = full_get_route_and_latency;
122   new_component->generic_routing.get_onelink_routes =
123       full_get_onelink_routes;
124   new_component->generic_routing.finalize = full_finalize;
125
126   return (AS_t) new_component;
127 }
128
129 void model_full_end(AS_t current_routing)
130 {
131   unsigned int i;
132   route_t e_route;
133
134   /* set utils vars */
135   routing_component_full_t routing =
136       ((routing_component_full_t) current_routing);
137   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
138
139   /* Create table if necessary */
140   if(!routing->routing_table)
141           routing->routing_table = xbt_new0(route_t, table_size * table_size);
142
143   /* Add the loopback if needed */
144   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
145     for (i = 0; i < table_size; i++) {
146       e_route = TO_ROUTE_FULL(i, i);
147       if (!e_route) {
148         e_route = xbt_new0(s_route_t, 1);
149         e_route->src_gateway = NULL;
150         e_route->dst_gateway = NULL;
151         e_route->link_list = xbt_dynar_new(global_routing->size_of_link, NULL);
152         xbt_dynar_push(e_route->link_list,
153                        &global_routing->loopback);
154         TO_ROUTE_FULL(i, i) = e_route;
155       }
156     }
157   }
158 }
159
160 static int full_pointer_resource_cmp(const void *a, const void *b) {
161   return a != b;
162 }
163
164 void model_full_set_route(AS_t rc, const char *src,
165                 const char *dst, route_t route)
166 {
167         int *src_id, *dst_id;
168         src_id = xbt_dict_get_or_null(rc->to_index, src);
169         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
170         routing_component_full_t routing = ((routing_component_full_t) rc);
171         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
172
173         xbt_assert(src_id, "Network elements %s not found", src);
174         xbt_assert(dst_id, "Network elements %s not found", dst);
175
176         xbt_assert(!xbt_dynar_is_empty(route->link_list),
177                           "Invalid count of links, must be greater than zero (%s,%s)",
178                           src, dst);
179
180         if(!routing->routing_table)
181                 routing->routing_table = xbt_new0(route_t, table_size * table_size);
182
183         if(TO_ROUTE_FULL(*src_id, *dst_id))
184         {
185                 char * link_name;
186                 unsigned int i;
187                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
188                 xbt_dynar_foreach(route->link_list,i,link_name)
189                 {
190                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
191                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
192                         xbt_dynar_push(link_route_to_test,&link);
193                 }
194                 if (xbt_dynar_compare((void*)TO_ROUTE_FULL(*src_id, *dst_id)->link_list,
195                                               (void*)link_route_to_test,
196                                               (int_f_cpvoid_cpvoid_t) full_pointer_resource_cmp)) {
197                         surf_parse_error("A route between \"%s\" and \"%s\" already exists with a different content. "
198                                                  "If you are trying to define a reverse route, you must set the symmetrical=no "
199                                                  "attribute to your routes tags.", src,dst);
200                 } else {
201                         surf_parse_warn("Ignoring the identical redefinition of the route between \"%s\" and \"%s\"",src,dst);
202                 }
203         }
204         else
205         {
206                   if(!route->dst_gateway && !route->src_gateway)
207                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
208                   else{
209                     AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
210                     if (subas==NULL)
211                       surf_parse_error("The source of an ASroute must be a sub-AS declared within the current AS, but '%s' is not an AS within '%s'",
212                           src,rc->name);
213                     if (xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
214                       surf_parse_error("In an ASroute, source gateway must be part of the source sub-AS (in particular, being in a sub-sub-AS is not allowed), but '%s' is not in '%s'.",
215                           route->src_gateway,subas->name);
216
217                     subas = xbt_dict_get_or_null(rc->routing_sons, dst);
218                     if (subas==NULL)
219                       surf_parse_error("The destination of an ASroute must be a sub-AS declared within the current AS, but '%s' is not an AS within '%s'",
220                           dst,rc->name);
221                     if (xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
222                       surf_parse_error("In an ASroute, destination gateway must be part of the destination sub-AS (in particular, in a sub-sub-AS is not allowed), but '%s' is not in '%s'.",
223                           route->dst_gateway,subas->name);
224
225                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
226                                  route->src_gateway, dst, route->dst_gateway);
227                           if(routing_get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
228                                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
229                           if(routing_get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
230                                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
231                   }
232               TO_ROUTE_FULL(*src_id, *dst_id) = generic_new_extended_route(rc->hierarchy,route,1);
233               xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->link_list, 0);
234         }
235
236         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
237                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
238         {
239                 if(route->dst_gateway && route->src_gateway)
240                 {
241                   char *gw_tmp ;
242                   gw_tmp = route->src_gateway;
243                   route->src_gateway = route->dst_gateway;
244                   route->dst_gateway = gw_tmp;
245                 }
246                 if(TO_ROUTE_FULL(*dst_id, *src_id))
247                 {
248                         char * link_name;
249                         unsigned int i;
250                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
251                         for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
252                         {
253                                 link_name = xbt_dynar_get_as(route->link_list,i-1,void *);
254                                 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
255                                 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
256                                 xbt_dynar_push(link_route_to_test,&link);
257                         }
258                         xbt_assert(!xbt_dynar_compare(
259                                   (void*)TO_ROUTE_FULL(*dst_id, *src_id)->link_list,
260                               (void*)link_route_to_test,
261                                   (int_f_cpvoid_cpvoid_t) full_pointer_resource_cmp),
262                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
263                 }
264                 else
265                 {
266                           if(!route->dst_gateway && !route->src_gateway)
267                                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
268                           else
269                                   XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
270                                          route->src_gateway, src, route->dst_gateway);
271                       TO_ROUTE_FULL(*dst_id, *src_id) = generic_new_extended_route(rc->hierarchy,route,0);
272                       xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->link_list, 0);
273                 }
274         }
275 }