Logo AND Algorithmique Numérique Distribuée

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