Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename a global to avoid namespace pollution (and other 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
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_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_t route = TO_ROUTE_FULL(*src_id, *dst_id);
41       if (route) {
42         if (xbt_dynar_length(route->link_list) == 1) {
43           void *link =
44               *(void **) xbt_dynar_get_ptr(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 void full_get_route(AS_t rc,
65                            const char *src, const char *dst,
66                            route_t res)
67 {
68
69   /* set utils vars */
70   routing_component_full_t routing = (routing_component_full_t) rc;
71   size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
72
73   int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
74   int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
75
76   if (!src_id || !dst_id)
77     THROWF(arg_error,0,"No route from '%s' to '%s'",src,dst);
78
79   route_t e_route = NULL;
80   void *link;
81   unsigned int cpt = 0;
82
83   e_route = TO_ROUTE_FULL(*src_id, *dst_id);
84
85   if (e_route) {
86     res->src_gateway = xbt_strdup(e_route->src_gateway);
87     res->dst_gateway = xbt_strdup(e_route->dst_gateway);
88     xbt_dynar_foreach(e_route->link_list, cpt, link) {
89       xbt_dynar_push(res->link_list, &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 = full_get_route;
119   new_component->generic_routing.get_onelink_routes =
120       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,
150                        &global_routing->loopback);
151         TO_ROUTE_FULL(i, i) = e_route;
152       }
153     }
154   }
155 }
156
157 static int surf_pointer_resource_cmp(const void *a, const void *b) {
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         {
182                 char * link_name;
183                 unsigned int i;
184                 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
185                 xbt_dynar_foreach(route->link_list,i,link_name)
186                 {
187                         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
188                         xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
189                         xbt_dynar_push(link_route_to_test,&link);
190                 }
191                 xbt_assert(!xbt_dynar_compare(
192                           (void*)TO_ROUTE_FULL(*src_id, *dst_id)->link_list,
193                           (void*)link_route_to_test,
194                           (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
195                           "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);
196         }
197         else
198         {
199                   if(!route->dst_gateway && !route->src_gateway)
200                           XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
201                   else{
202                           XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
203                                  route->src_gateway, dst, route->dst_gateway);
204                           if(routing_get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
205                                   xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
206                           if(routing_get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
207                                   xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
208                   }
209               TO_ROUTE_FULL(*src_id, *dst_id) = generic_new_extended_route(rc->hierarchy,route,1);
210               xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->link_list, 0);
211         }
212
213         if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
214                 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
215         {
216                 if(route->dst_gateway && route->src_gateway)
217                 {
218                   char *gw_tmp ;
219                   gw_tmp = route->src_gateway;
220                   route->src_gateway = route->dst_gateway;
221                   route->dst_gateway = gw_tmp;
222                 }
223                 if(TO_ROUTE_FULL(*dst_id, *src_id))
224                 {
225                         char * link_name;
226                         unsigned int i;
227                         xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
228                         for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
229                         {
230                                 link_name = xbt_dynar_get_as(route->link_list,i-1,void *);
231                                 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
232                                 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
233                                 xbt_dynar_push(link_route_to_test,&link);
234                         }
235                         xbt_assert(!xbt_dynar_compare(
236                                   (void*)TO_ROUTE_FULL(*dst_id, *src_id)->link_list,
237                               (void*)link_route_to_test,
238                                   (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
239                                   "The route between \"%s\" and \"%s\" already exists", src,dst);
240                 }
241                 else
242                 {
243                           if(!route->dst_gateway && !route->src_gateway)
244                                   XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
245                           else
246                                   XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
247                                          route->src_gateway, src, route->dst_gateway);
248                       TO_ROUTE_FULL(*dst_id, *src_id) = generic_new_extended_route(rc->hierarchy,route,0);
249                       xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->link_list, 0);
250                 }
251         }
252
253         generic_free_route((route_t)route) ;
254 }