1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "surf_routing_private.h"
10 extern routing_global_t global_routing;
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
14 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
16 /* Routing model structure */
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;
23 /* Business methods */
24 static xbt_dynar_t full_get_onelink_routes(AS_t rc)
26 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
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);
38 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
40 route_t route = TO_ROUTE_FULL(*src_id, *dst_id);
42 if (xbt_dynar_length(route->link_list) == 1) {
44 *(void **) xbt_dynar_get_ptr(route->link_list,
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);
56 xbt_dynar_push(ret, &onelink);
64 static void full_get_route_and_latency(AS_t rc,
65 const char *src, const char *dst,
66 route_t res,double *lat)
70 routing_component_full_t routing = (routing_component_full_t) rc;
71 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
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);
76 if (!src_id || !dst_id)
77 THROWF(arg_error,0,"No route from '%s' to '%s'",src,dst);
79 route_t e_route = NULL;
83 e_route = TO_ROUTE_FULL(*src_id, *dst_id);
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);
91 *lat += surf_network_model->extension.network.get_link_latency(link);
96 static void full_finalize(AS_t rc)
98 routing_component_full_t routing = (routing_component_full_t) rc;
99 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
102 /* Delete routing table */
103 for (i = 0; i < table_size; i++)
104 for (j = 0; j < table_size; j++)
105 generic_free_route(TO_ROUTE_FULL(i, j));
106 xbt_free(routing->routing_table);
107 model_generic_finalize(rc);
111 /* Creation routing model functions */
113 AS_t model_full_create(void)
115 routing_component_full_t new_component = (routing_component_full_t)
116 model_generic_create_sized(sizeof(s_routing_component_full_t));
118 new_component->generic_routing.parse_route = model_full_set_route;
119 new_component->generic_routing.parse_ASroute = model_full_set_route;
120 new_component->generic_routing.get_route_and_latency = full_get_route_and_latency;
121 new_component->generic_routing.get_onelink_routes =
122 full_get_onelink_routes;
123 new_component->generic_routing.finalize = full_finalize;
125 return (AS_t) new_component;
128 void model_full_end(AS_t current_routing)
134 routing_component_full_t routing =
135 ((routing_component_full_t) current_routing);
136 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
138 /* Create table if necessary */
139 if(!routing->routing_table)
140 routing->routing_table = xbt_new0(route_t, table_size * table_size);
142 /* Add the loopback if needed */
143 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
144 for (i = 0; i < table_size; i++) {
145 e_route = TO_ROUTE_FULL(i, i);
147 e_route = xbt_new0(s_route_t, 1);
148 e_route->src_gateway = NULL;
149 e_route->dst_gateway = NULL;
150 e_route->link_list = xbt_dynar_new(global_routing->size_of_link, NULL);
151 xbt_dynar_push(e_route->link_list,
152 &global_routing->loopback);
153 TO_ROUTE_FULL(i, i) = e_route;
159 static int full_pointer_resource_cmp(const void *a, const void *b) {
163 void model_full_set_route(AS_t rc, const char *src,
164 const char *dst, route_t route)
166 int *src_id, *dst_id;
167 src_id = xbt_dict_get_or_null(rc->to_index, src);
168 dst_id = xbt_dict_get_or_null(rc->to_index, dst);
169 routing_component_full_t routing = ((routing_component_full_t) rc);
170 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
172 xbt_assert(src_id, "Network elements %s not found", src);
173 xbt_assert(dst_id, "Network elements %s not found", dst);
175 xbt_assert(!xbt_dynar_is_empty(route->link_list),
176 "Invalid count of links, must be greater than zero (%s,%s)",
179 if(!routing->routing_table)
180 routing->routing_table = xbt_new0(route_t, table_size * table_size);
182 if(TO_ROUTE_FULL(*src_id, *dst_id))
186 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
187 xbt_dynar_foreach(route->link_list,i,link_name)
189 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
190 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
191 xbt_dynar_push(link_route_to_test,&link);
193 xbt_assert(!xbt_dynar_compare(
194 (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 "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);
201 if(!route->dst_gateway && !route->src_gateway)
202 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
204 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
205 route->src_gateway, dst, route->dst_gateway);
206 if(routing_get_network_element_type((const char*)route->dst_gateway) == SURF_NETWORK_ELEMENT_NULL)
207 xbt_die("The dst_gateway '%s' does not exist!",route->dst_gateway);
208 if(routing_get_network_element_type((const char*)route->src_gateway) == SURF_NETWORK_ELEMENT_NULL)
209 xbt_die("The src_gateway '%s' does not exist!",route->src_gateway);
211 TO_ROUTE_FULL(*src_id, *dst_id) = generic_new_extended_route(rc->hierarchy,route,1);
212 xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->link_list, 0);
215 if( A_surfxml_route_symmetrical == A_surfxml_route_symmetrical_YES
216 || A_surfxml_ASroute_symmetrical == A_surfxml_ASroute_symmetrical_YES )
218 if(route->dst_gateway && route->src_gateway)
221 gw_tmp = route->src_gateway;
222 route->src_gateway = route->dst_gateway;
223 route->dst_gateway = gw_tmp;
225 if(TO_ROUTE_FULL(*dst_id, *src_id))
229 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
230 for(i=xbt_dynar_length(route->link_list) ;i>0 ;i--)
232 link_name = xbt_dynar_get_as(route->link_list,i-1,void *);
233 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
234 xbt_assert(link,"Link : '%s' doesn't exists.",link_name);
235 xbt_dynar_push(link_route_to_test,&link);
237 xbt_assert(!xbt_dynar_compare(
238 (void*)TO_ROUTE_FULL(*dst_id, *src_id)->link_list,
239 (void*)link_route_to_test,
240 (int_f_cpvoid_cpvoid_t) full_pointer_resource_cmp),
241 "The route between \"%s\" and \"%s\" already exists", src,dst);
245 if(!route->dst_gateway && !route->src_gateway)
246 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
248 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
249 route->src_gateway, src, route->dst_gateway);
250 TO_ROUTE_FULL(*dst_id, *src_id) = generic_new_extended_route(rc->hierarchy,route,0);
251 xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->link_list, 0);