1 /* Copyright (c) 2009-2013. 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"
8 #include "surf_routing_full.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
16 extern routing_platf_t routing_platf;
17 extern int surf_parse_lineno;
19 #define TO_ROUTE_FULL(i,j) p_routingTable[(i)+(j)*table_size]
21 AS_t model_full_create(void)
26 void model_full_end(AS_t _routing)
29 sg_platf_route_cbarg_t e_route;
32 AsFullPtr routing = ((AsFullPtr) _routing);
33 size_t table_size = xbt_dynar_length(routing->p_indexNetworkElm);
35 /* Create table if necessary */
36 if (!routing->p_routingTable)
37 routing->p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
39 /* Add the loopback if needed */
40 if (routing_platf->p_loopback && routing->p_hierarchy == SURF_ROUTING_BASE) {
41 for (i = 0; i < table_size; i++) {
42 e_route = routing->TO_ROUTE_FULL(i, i);
44 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
45 e_route->gw_src = NULL;
46 e_route->gw_dst = NULL;
47 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
48 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
49 routing->TO_ROUTE_FULL(i, i) = e_route;
60 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
62 /* Delete routing table */
63 for (i = 0; i < table_size; i++)
64 for (j = 0; j < table_size; j++)
65 delete TO_ROUTE_FULL(i,j);
66 xbt_free(p_routingTable);
69 xbt_dynar_t AsFull::getOneLinkRoutes()
71 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
74 int table_size = xbt_dynar_length(p_indexNetworkElm);
76 for(src=0; src < table_size; src++) {
77 for(dst=0; dst< table_size; dst++) {
78 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
80 if (xbt_dynar_length(route->link_list) == 1) {
81 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
82 onelink_t onelink = xbt_new0(s_onelink_t, 1);
83 onelink->link_ptr = link;
84 if (p_hierarchy == SURF_ROUTING_BASE) {
85 onelink->src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_routing_edge_t);
86 onelink->src->m_id = src;
87 onelink->dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_routing_edge_t);
88 onelink->dst->m_id = dst;
89 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
90 onelink->src = route->gw_src;
91 onelink->dst = route->gw_dst;
93 xbt_dynar_push(ret, &onelink);
94 XBT_DEBUG("Push route from '%d' to '%d'",
104 void AsFull::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
106 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
113 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
115 sg_platf_route_cbarg_t e_route = NULL;
117 unsigned int cpt = 0;
119 e_route = TO_ROUTE_FULL(src->m_id, dst->m_id);
122 res->gw_src = e_route->gw_src;
123 res->gw_dst = e_route->gw_dst;
124 xbt_dynar_foreach(e_route->link_list, cpt, link) {
125 xbt_dynar_push(res->link_list, &link);
127 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
132 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
136 static int full_pointer_resource_cmp(const void *a, const void *b)
141 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
144 char *src = (char*)(route->src);
145 char *dst = (char*)(route->dst);
146 RoutingEdgePtr src_net_elm, dst_net_elm;
147 src_net_elm = sg_routing_edge_by_name_or_null(src);
148 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
150 xbt_assert(src_net_elm, "Network elements %s not found", src);
151 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
153 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
155 xbt_assert(!xbt_dynar_is_empty(route->link_list),
156 "Invalid count of links, must be greater than zero (%s,%s)",
160 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
162 if (TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)) {
165 xbt_dynar_t link_route_to_test =
166 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
167 xbt_dynar_foreach(route->link_list, i, link_name) {
168 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
169 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
170 xbt_dynar_push(link_route_to_test, &link);
172 if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
173 link_route_to_test, full_pointer_resource_cmp)) {
174 surf_parse_error("A route between \"%s\" and \"%s\" already exists "
175 "with a different content. "
176 "If you are trying to define a reverse route, "
177 "you must set the symmetrical=no attribute to "
178 "your routes tags.", src, dst);
180 surf_parse_warn("Ignoring the identical redefinition of the route "
181 "between \"%s\" and \"%s\"", src, dst);
184 if (!route->gw_dst && !route->gw_dst)
185 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
187 // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
188 // AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
189 // if (subas == NULL)
190 // surf_parse_error("The source of an ASroute must be a sub-AS "
191 // "declared within the current AS, "
192 // "but '%s' is not an AS within '%s'", src, rc->name);
193 // if (subas->to_index
194 // && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
195 // surf_parse_error("In an ASroute, source gateway must be part of "
196 // "the source sub-AS (in particular, being in a "
197 // "sub-sub-AS is not allowed), "
198 // "but '%s' is not in '%s'.",
199 // route->src_gateway, subas->name);
201 // subas = xbt_dict_get_or_null(rc->routing_sons, dst);
202 // if (subas == NULL)
203 // surf_parse_error("The destination of an ASroute must be a sub-AS "
204 // "declared within the current AS, "
205 // "but '%s' is not an AS within '%s'", dst, rc->name);
206 // if (subas->to_index
207 // && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
208 // surf_parse_error("In an ASroute, destination gateway must be "
209 // "part of the destination sub-AS (in particular, "
210 // "in a sub-sub-AS is not allowed), "
211 // "but '%s' is not in '%s'.",
212 // route->dst_gateway, subas->name);
214 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
215 src, route->gw_src->p_name, dst, route->gw_dst->p_name);
216 if (route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
217 xbt_die("The dst_gateway '%s' does not exist!", route->gw_dst->p_name);
218 if (route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
219 xbt_die("The src_gateway '%s' does not exist!", route->gw_src->p_name);
221 TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 1);
222 xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list, 0);
225 if ( (route->symmetrical == TRUE && as_route == 0)
226 || (route->symmetrical == TRUE && as_route == 1)
228 if (route->gw_dst && route->gw_src) {
229 sg_routing_edge_t gw_tmp;
230 gw_tmp = route->gw_src;
231 route->gw_src = route->gw_dst;
232 route->gw_dst = gw_tmp;
234 if (TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)) {
237 xbt_dynar_t link_route_to_test =
238 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
239 for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
240 link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
241 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
242 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
243 xbt_dynar_push(link_route_to_test, &link);
245 xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
247 full_pointer_resource_cmp),
248 "The route between \"%s\" and \"%s\" already exists", src,
251 if (!route->gw_dst && !route->gw_src)
252 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
254 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
255 dst, route->gw_src->p_name, src, route->gw_dst->p_name);
256 TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 0);
257 xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list, 0);
260 xbt_dynar_free(&route->link_list);