Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
useless cosmetics
[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 routing_component_t current_routing;
12 extern model_type_t current_routing_model;
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
15
16 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
17
18 /* Routing model structure */
19
20 typedef struct {
21   s_routing_component_t generic_routing;
22   route_extended_t *routing_table;
23 } s_routing_component_full_t, *routing_component_full_t;
24
25 /* Business methods */
26 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
27 {
28   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
29
30   routing_component_full_t routing = (routing_component_full_t) rc;
31   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
32   xbt_dict_cursor_t c1 = NULL, c2 = NULL;
33   char *k1, *d1, *k2, *d2;
34   xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
35     xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
36       int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
37       int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
38       xbt_assert(src_id
39                   && dst_id,
40                   "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
41                   k1, k2);
42       route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
43       if (route) {
44         if (xbt_dynar_length(route->generic_route.link_list) == 1) {
45           void *link =
46               *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
47                                            0);
48           onelink_t onelink = xbt_new0(s_onelink_t, 1);
49           onelink->link_ptr = link;
50           if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
51             onelink->src = xbt_strdup(k1);
52             onelink->dst = xbt_strdup(k2);
53           } else if (routing->generic_routing.hierarchy ==
54                      SURF_ROUTING_RECURSIVE) {
55             onelink->src = xbt_strdup(route->src_gateway);
56             onelink->dst = xbt_strdup(route->dst_gateway);
57           }
58           xbt_dynar_push(ret, &onelink);
59         }
60       }
61     }
62   }
63   return ret;
64 }
65
66 static route_extended_t full_get_route(routing_component_t rc,
67                                        const char *src, const char *dst)
68 {
69   xbt_assert(rc && src
70               && dst,
71               "Invalid params for \"get_route\" function at AS \"%s\"",
72               rc->name);
73
74   /* set utils vars */
75   routing_component_full_t routing = (routing_component_full_t) rc;
76   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
77
78   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
79   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
80   xbt_assert(src_id
81               && dst_id,
82               "Ask for route \"from\"(%s)  or \"to\"(%s) no found in the local table",
83               src, dst);
84
85   route_extended_t e_route = NULL;
86   route_extended_t new_e_route = NULL;
87   void *link;
88   unsigned int cpt = 0;
89
90   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
91
92   if (e_route) {
93     new_e_route = xbt_new0(s_route_extended_t, 1);
94     new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
95     new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
96     new_e_route->generic_route.link_list =
97         xbt_dynar_new(global_routing->size_of_link, NULL);
98     xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
99       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
100     }
101   }
102   return new_e_route;
103 }
104
105 static void full_finalize(routing_component_t rc)
106 {
107   routing_component_full_t routing = (routing_component_full_t) rc;
108   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
109   int i, j;
110   if (routing) {
111     /* Delete routing table */
112     for (i = 0; i < table_size; i++)
113       for (j = 0; j < table_size; j++)
114         generic_free_extended_route(TO_ROUTE_FULL(i, j));
115     xbt_free(routing->routing_table);
116     /* Delete bypass dict */
117     xbt_dict_free(&rc->bypassRoutes);
118     /* Delete index dict */
119     xbt_dict_free(&rc->to_index);
120     /* Delete structure */
121     xbt_free(rc);
122   }
123 }
124
125 /* Creation routing model functions */
126
127 void *model_full_create(void)
128 {
129   routing_component_full_t new_component =
130       xbt_new0(s_routing_component_full_t, 1);
131   new_component->generic_routing.set_processing_unit =
132       generic_set_processing_unit;
133   new_component->generic_routing.set_autonomous_system =
134       generic_set_autonomous_system;
135   new_component->generic_routing.set_route = model_full_set_route;
136   new_component->generic_routing.set_ASroute = model_full_set_route;
137   new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
138   new_component->generic_routing.get_route = full_get_route;
139   new_component->generic_routing.get_latency = generic_get_link_latency;
140   new_component->generic_routing.get_onelink_routes =
141       full_get_onelink_routes;
142   new_component->generic_routing.get_bypass_route =
143       generic_get_bypassroute;
144   new_component->generic_routing.finalize = full_finalize;
145   new_component->generic_routing.to_index = xbt_dict_new();
146   new_component->generic_routing.bypassRoutes = xbt_dict_new();
147   new_component->generic_routing.get_network_element_type = get_network_element_type;
148   return new_component;
149 }
150
151 void model_full_load(void)
152 {
153   /* use "surfxml_add_callback" to add a parse function call */
154 }
155
156 void model_full_unload(void)
157 {
158   /* use "surfxml_del_callback" to remove a parse function call */
159 }
160
161 void model_full_end(void)
162 {
163   unsigned int i;
164   route_extended_t e_route;
165
166   /* set utils vars */
167   routing_component_full_t routing =
168       ((routing_component_full_t) current_routing);
169   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
170
171   /* Create table if necessary */
172   if(!routing->routing_table)
173           routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
174
175   /* Add the loopback if needed */
176   if (current_routing->hierarchy == SURF_ROUTING_BASE) {
177     for (i = 0; i < table_size; i++) {
178       e_route = TO_ROUTE_FULL(i, i);
179       if (!e_route) {
180         e_route = xbt_new0(s_route_extended_t, 1);
181         e_route->src_gateway = NULL;
182         e_route->dst_gateway = NULL;
183         e_route->generic_route.link_list =
184             xbt_dynar_new(global_routing->size_of_link, NULL);
185         xbt_dynar_push(e_route->generic_route.link_list,
186                        &global_routing->loopback);
187         TO_ROUTE_FULL(i, i) = e_route;
188       }
189     }
190   }
191 }
192
193 void model_full_set_route(routing_component_t rc, const char *src,
194                 const char *dst, name_route_extended_t route)
195 {
196         int *src_id, *dst_id;
197         src_id = xbt_dict_get_or_null(rc->to_index, src);
198         dst_id = xbt_dict_get_or_null(rc->to_index, dst);
199         routing_component_full_t routing = ((routing_component_full_t) rc);
200         size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
201
202         xbt_assert(src_id, "Network elements %s not found", src);
203         xbt_assert(dst_id, "Network elements %s not found", dst);
204
205         xbt_assert(xbt_dynar_length(route->generic_route.link_list) > 0,
206                           "Invalid count of links, must be greater than zero (%s,%s)",
207                           src, dst);
208
209         if(!routing->routing_table)
210                 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
211
212         if(TO_ROUTE_FULL(*src_id, *dst_id))
213         {
214                 char * link_name;
215                 unsigned int i;
216                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
217                 xbt_dynar_foreach(route->generic_route.link_list,i,link_name)
218                 {
219                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
220                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
221                         xbt_dynar_push(link_route_to_test,&link);
222                 }
223                 xbt_assert(!xbt_dynar_compare(
224                           (void*)TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list,
225                           (void*)link_route_to_test,
226                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
227                           "The route between \"%s\" and \"%s\" already exists. If you are trying to define a reverse route, you must set the symmetrical=no attribute to your routes tags.", src,dst);
228         }
229         else
230         {
231                   if(!route->dst_gateway && !route->src_gateway)
232                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
233                   else{
234                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
235                                  route->src_gateway, dst, route->dst_gateway);
236                           if(global_routing->get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
237                                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
238                           if(global_routing->get_network_element_type((const char*)route->src_gateway) == 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) = generic_new_extended_route(rc->hierarchy,route,1);
242               xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list, 0);
243         }
244
245         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
246                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
247         {
248                 if(route->dst_gateway && route->src_gateway)
249                 {
250                   char *gw_tmp ;
251                   gw_tmp = route->src_gateway;
252                   route->src_gateway = route->dst_gateway;
253                   route->dst_gateway = gw_tmp;
254                 }
255                 if(TO_ROUTE_FULL(*dst_id, *src_id))
256                 {
257                         char * link_name;
258                         unsigned int i;
259                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
260                         for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
261                         {
262                                 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
263                                 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
264                                 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
265                                 xbt_dynar_push(link_route_to_test,&link);
266                         }
267                         xbt_assert(!xbt_dynar_compare(
268                                   (void*)TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list,
269                               (void*)link_route_to_test,
270                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
271                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
272                 }
273                 else
274                 {
275                           if(!route->dst_gateway && !route->src_gateway)
276                                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
277                           else
278                                   XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
279                                          route->src_gateway, src, route->dst_gateway);
280                       TO_ROUTE_FULL(*dst_id, *src_id) = generic_new_extended_route(rc->hierarchy,route,0);
281                       xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list, 0);
282                 }
283         }
284
285         if (rc->hierarchy == SURF_ROUTING_BASE) generic_free_route((route_t)route) ;
286         else generic_free_extended_route((route_extended_t)route);
287 }