Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c4a4c9f4d639bce074675448a766bb17f8cf4e72
[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 && dst_id,
38                  "Ask for route \"from\"(%s)  or \"to\"(%s) "
39                  "no found in the local table", k1, k2);
40       route_t route = TO_ROUTE_FULL(*src_id, *dst_id);
41       if (route) {
42         if (xbt_dynar_length(route->link_list) == 1) {
43           void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
44           onelink_t onelink = xbt_new0(s_onelink_t, 1);
45           onelink->link_ptr = link;
46           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
47             onelink->src = xbt_strdup(k1);
48             onelink->dst = xbt_strdup(k2);
49           } else if (routing->generic_routing.hierarchy ==
50                      SURF_ROUTING_RECURSIVE) {
51             onelink->src = xbt_strdup(route->src_gateway);
52             onelink->dst = xbt_strdup(route->dst_gateway);
53           }
54           xbt_dynar_push(ret, &onelink);
55         }
56       }
57     }
58   }
59   return ret;
60 }
61
62 static void full_get_route_and_latency(AS_t rc,
63                                        const char *src, const char *dst,
64                                        route_t res, double *lat)
65 {
66
67   /* set utils vars */
68   routing_component_full_t routing = (routing_component_full_t) rc;
69   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
70
71   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
72   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
73
74   if (!src_id || !dst_id)
75     THROWF(arg_error, 0, "No route from '%s' to '%s'", src, dst);
76
77   route_t e_route = NULL;
78   void *link;
79   unsigned int cpt = 0;
80
81   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
82
83   if (e_route) {
84     res->src_gateway = xbt_strdup(e_route->src_gateway);
85     res->dst_gateway = xbt_strdup(e_route->dst_gateway);
86     xbt_dynar_foreach(e_route->link_list, cpt, link) {
87       xbt_dynar_push(res->link_list, &link);
88       if (lat)
89         *lat += surf_network_model->extension.network.get_link_latency(link);
90     }
91   }
92 }
93
94 static void full_finalize(AS_t rc)
95 {
96   routing_component_full_t routing = (routing_component_full_t) rc;
97   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
98   int i, j;
99   if (routing) {
100     /* Delete routing table */
101     for (i = 0; i < table_size; i++)
102       for (j = 0; j < table_size; j++)
103         generic_free_route(TO_ROUTE_FULL(i, j));
104     xbt_free(routing->routing_table);
105     model_generic_finalize(rc);
106   }
107 }
108
109 /* Creation routing model functions */
110
111 AS_t model_full_create(void)
112 {
113   routing_component_full_t new_component = (routing_component_full_t)
114       model_generic_create_sized(sizeof(s_routing_component_full_t));
115
116   new_component->generic_routing.parse_route = model_full_set_route;
117   new_component->generic_routing.parse_ASroute = model_full_set_route;
118   new_component->generic_routing.get_route_and_latency =
119       full_get_route_and_latency;
120   new_component->generic_routing.get_onelink_routes = full_get_onelink_routes;
121   new_component->generic_routing.finalize = full_finalize;
122
123   return (AS_t) new_component;
124 }
125
126 void model_full_end(AS_t current_routing)
127 {
128   unsigned int i;
129   route_t e_route;
130
131   /* set utils vars */
132   routing_component_full_t routing =
133       ((routing_component_full_t) current_routing);
134   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
135
136   /* Create table if necessary */
137   if (!routing->routing_table)
138     routing->routing_table = xbt_new0(route_t, table_size * table_size);
139
140   /* Add the loopback if needed */
141   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
142     for (i = 0; i < table_size; i++) {
143       e_route = TO_ROUTE_FULL(i, i);
144       if (!e_route) {
145         e_route = xbt_new0(s_route_t, 1);
146         e_route->src_gateway = NULL;
147         e_route->dst_gateway = NULL;
148         e_route->link_list = xbt_dynar_new(global_routing->size_of_link, NULL);
149         xbt_dynar_push(e_route->link_list, &global_routing->loopback);
150         TO_ROUTE_FULL(i, i) = e_route;
151       }
152     }
153   }
154 }
155
156 static int full_pointer_resource_cmp(const void *a, const void *b)
157 {
158   return a != b;
159 }
160
161 void model_full_set_route(AS_t rc, const char *src,
162                           const char *dst, route_t route)
163 {
164   int *src_id, *dst_id;
165   src_id = xbt_dict_get_or_null(rc->to_index, src);
166   dst_id = xbt_dict_get_or_null(rc->to_index, dst);
167   routing_component_full_t routing = (routing_component_full_t) rc;
168   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
169
170   xbt_assert(src_id, "Network elements %s not found", src);
171   xbt_assert(dst_id, "Network elements %s not found", dst);
172
173   xbt_assert(!xbt_dynar_is_empty(route->link_list),
174              "Invalid count of links, must be greater than zero (%s,%s)",
175              src, dst);
176
177   if (!routing->routing_table)
178     routing->routing_table = xbt_new0(route_t, table_size * table_size);
179
180   if (TO_ROUTE_FULL(*src_id, *dst_id)) {
181     char *link_name;
182     unsigned int i;
183     xbt_dynar_t link_route_to_test =
184         xbt_dynar_new(global_routing->size_of_link, NULL);
185     xbt_dynar_foreach(route->link_list, i, link_name) {
186       void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
187       xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
188       xbt_dynar_push(link_route_to_test, &link);
189     }
190     if (xbt_dynar_compare(TO_ROUTE_FULL(*src_id, *dst_id)->link_list,
191                           link_route_to_test, full_pointer_resource_cmp)) {
192       surf_parse_error("A route between \"%s\" and \"%s\" already exists "
193                        "with a different content. "
194                        "If you are trying to define a reverse route, "
195                        "you must set the symmetrical=no attribute to "
196                        "your routes tags.", src, dst);
197     } else {
198       surf_parse_warn("Ignoring the identical redefinition of the route "
199                       "between \"%s\" and \"%s\"", src, dst);
200     }
201   } else {
202     if (!route->dst_gateway && !route->src_gateway)
203       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
204     else {
205 // FIXME We can call a gw wich is down the current AS (cf g5k.xml) but not upper.
206 //      AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
207 //      if (subas == NULL)
208 //        surf_parse_error("The source of an ASroute must be a sub-AS "
209 //                         "declared within the current AS, "
210 //                         "but '%s' is not an AS within '%s'", src, rc->name);
211 //      if (subas->to_index
212 //          && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
213 //        surf_parse_error("In an ASroute, source gateway must be part of "
214 //                         "the source sub-AS (in particular, being in a "
215 //                         "sub-sub-AS is not allowed), "
216 //                         "but '%s' is not in '%s'.",
217 //                         route->src_gateway, subas->name);
218 //
219 //      subas = xbt_dict_get_or_null(rc->routing_sons, dst);
220 //      if (subas == NULL)
221 //        surf_parse_error("The destination of an ASroute must be a sub-AS "
222 //                         "declared within the current AS, "
223 //                         "but '%s' is not an AS within '%s'", dst, rc->name);
224 //      if (subas->to_index
225 //          && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
226 //        surf_parse_error("In an ASroute, destination gateway must be "
227 //                         "part of the destination sub-AS (in particular, "
228 //                         "in a sub-sub-AS is not allowed), "
229 //                         "but '%s' is not in '%s'.",
230 //                         route->dst_gateway, subas->name);
231
232       XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
233                 src, route->src_gateway, dst, route->dst_gateway);
234       if (routing_get_network_element_type(route->dst_gateway) ==
235           SURF_NETWORK_ELEMENT_NULL)
236         xbt_die("The dst_gateway '%s' does not exist!", route->dst_gateway);
237       if (routing_get_network_element_type(route->src_gateway) ==
238           SURF_NETWORK_ELEMENT_NULL)
239         xbt_die("The src_gateway '%s' does not exist!", route->src_gateway);
240     }
241     TO_ROUTE_FULL(*src_id, *dst_id) =
242         generic_new_extended_route(rc->hierarchy, route, 1);
243     xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->link_list, 0);
244   }
245
246   if (A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
247       || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES) {
248     if (route->dst_gateway && route->src_gateway) {
249       char *gw_tmp;
250       gw_tmp = route->src_gateway;
251       route->src_gateway = route->dst_gateway;
252       route->dst_gateway = gw_tmp;
253     }
254     if (TO_ROUTE_FULL(*dst_id, *src_id)) {
255       char *link_name;
256       unsigned int i;
257       xbt_dynar_t link_route_to_test =
258           xbt_dynar_new(global_routing->size_of_link, NULL);
259       for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
260         link_name = xbt_dynar_get_as(route->link_list, i - 1, void *);
261         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
262         xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
263         xbt_dynar_push(link_route_to_test, &link);
264       }
265       xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(*dst_id, *src_id)->link_list,
266                                     link_route_to_test,
267                                     full_pointer_resource_cmp),
268                  "The route between \"%s\" and \"%s\" already exists", src,
269                  dst);
270     } else {
271       if (!route->dst_gateway && !route->src_gateway)
272         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
273       else
274         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
275                   dst, route->src_gateway, src, route->dst_gateway);
276       TO_ROUTE_FULL(*dst_id, *src_id) =
277           generic_new_extended_route(rc->hierarchy, route, 0);
278       xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->link_list, 0);
279     }
280   }
281 }