1 /* Copyright (c) 2009-2015. 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 "src/surf/surf_routing_private.hpp"
8 #include "src/surf/surf_routing_full.hpp"
9 #include "src/surf/network_interface.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
13 #define TO_ROUTE_FULL(i,j) p_routingTable[(i)+(j)*table_size]
17 AsFull::AsFull(const char*name)
24 sg_platf_route_cbarg_t e_route;
27 int table_size = (int)xbt_dynar_length(vertices_);
29 /* Create table if necessary */
31 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
33 /* Add the loopback if needed */
34 if (routing_platf->p_loopback && hierarchy_ == SURF_ROUTING_BASE) {
35 for (i = 0; i < table_size; i++) {
36 e_route = TO_ROUTE_FULL(i, i);
38 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
39 e_route->gw_src = NULL;
40 e_route->gw_dst = NULL;
41 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
42 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
43 TO_ROUTE_FULL(i, i) = e_route;
51 int table_size = (int)xbt_dynar_length(vertices_);
53 /* Delete routing table */
54 for (i = 0; i < table_size; i++)
55 for (j = 0; j < table_size; j++) {
56 if (TO_ROUTE_FULL(i,j)){
57 xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
58 xbt_free(TO_ROUTE_FULL(i,j));
61 xbt_free(p_routingTable);
65 xbt_dynar_t AsFull::getOneLinkRoutes()
67 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
70 int table_size = xbt_dynar_length(vertices_);
72 for(src=0; src < table_size; src++) {
73 for(dst=0; dst< table_size; dst++) {
74 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
76 if (xbt_dynar_length(route->link_list) == 1) {
77 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
79 if (hierarchy_ == SURF_ROUTING_BASE) {
80 NetCard *tmp_src = xbt_dynar_get_as(vertices_, src, sg_netcard_t);
82 NetCard *tmp_dst = xbt_dynar_get_as(vertices_, dst, sg_netcard_t);
84 onelink = new Onelink(link, tmp_src, tmp_dst);
85 } else if (hierarchy_ == SURF_ROUTING_RECURSIVE)
86 onelink = new Onelink(link, route->gw_src, route->gw_dst);
88 onelink = new Onelink(link, NULL, NULL);
89 xbt_dynar_push(ret, &onelink);
90 XBT_DEBUG("Push route from '%d' to '%d'",
100 void AsFull::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
102 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
109 size_t table_size = xbt_dynar_length(vertices_);
111 sg_platf_route_cbarg_t e_route = NULL;
113 unsigned int cpt = 0;
115 e_route = TO_ROUTE_FULL(src->getId(), dst->getId());
118 res->gw_src = e_route->gw_src;
119 res->gw_dst = e_route->gw_dst;
120 xbt_dynar_foreach(e_route->link_list, cpt, link) {
121 xbt_dynar_push(res->link_list, &link);
123 *lat += static_cast<Link*>(link)->getLatency();
128 static int full_pointer_resource_cmp(const void *a, const void *b)
133 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
136 char *src = (char*)(route->src);
137 char *dst = (char*)(route->dst);
138 NetCard *src_net_elm, *dst_net_elm;
139 src_net_elm = sg_netcard_by_name_or_null(src);
140 dst_net_elm = sg_netcard_by_name_or_null(dst);
142 xbt_assert(src_net_elm, "Network elements %s not found", src);
143 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
145 size_t table_size = xbt_dynar_length(vertices_);
147 xbt_assert(!xbt_dynar_is_empty(route->link_list),
148 "Invalid count of links, must be greater than zero (%s,%s)",
152 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
154 if (TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())) {
157 xbt_dynar_t link_route_to_test =
158 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
159 xbt_dynar_foreach(route->link_list, i, link_name) {
160 void *link = Link::byName(link_name);
161 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
162 xbt_dynar_push(link_route_to_test, &link);
164 if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->link_list,
165 link_route_to_test, full_pointer_resource_cmp)) {
166 surf_parse_error("A route between \"%s\" and \"%s\" already exists "
167 "with a different content. "
168 "If you are trying to define a reverse route, "
169 "you must set the symmetrical=no attribute to "
170 "your routes tags.", src, dst);
172 surf_parse_warn("Ignoring the identical redefinition of the route "
173 "between \"%s\" and \"%s\"", src, dst);
176 if (!route->gw_src && !route->gw_dst)
177 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
179 // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
180 // AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
181 // if (subas == NULL)
182 // surf_parse_error("The source of an ASroute must be a sub-AS "
183 // "declared within the current AS, "
184 // "but '%s' is not an AS within '%s'", src, rc->name);
185 // if (subas->to_index
186 // && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
187 // surf_parse_error("In an ASroute, source gateway must be part of "
188 // "the source sub-AS (in particular, being in a "
189 // "sub-sub-AS is not allowed), "
190 // "but '%s' is not in '%s'.",
191 // route->src_gateway, subas->name);
193 // subas = xbt_dict_get_or_null(rc->routing_sons, dst);
194 // if (subas == NULL)
195 // surf_parse_error("The destination of an ASroute must be a sub-AS "
196 // "declared within the current AS, "
197 // "but '%s' is not an AS within '%s'", dst, rc->name);
198 // if (subas->to_index
199 // && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
200 // surf_parse_error("In an ASroute, destination gateway must be "
201 // "part of the destination sub-AS (in particular, "
202 // "in a sub-sub-AS is not allowed), "
203 // "but '%s' is not in '%s'.",
204 // route->dst_gateway, subas->name);
206 XBT_DEBUG("Load ASroute from \"%s\" to \"%s\"", src, dst);
207 if (!route->gw_src ||
208 route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
209 surf_parse_error("The src_gateway \"%s\" does not exist!",
210 route->gw_src ? route->gw_src->getName() : "(null)");
211 if (!route->gw_dst ||
212 route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
213 surf_parse_error("The dst_gateway \"%s\" does not exist!",
214 route->gw_dst ? route->gw_dst->getName() : "(null)");
215 XBT_DEBUG("ASroute goes from \"%s\" to \"%s\"",
216 route->gw_src->getName(), route->gw_dst->getName());
218 TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId()) = newExtendedRoute(hierarchy_, route, 1);
219 xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->link_list, 0);
222 if ( (route->symmetrical == TRUE && as_route == 0)
223 || (route->symmetrical == TRUE && as_route == 1)
225 if (route->gw_dst && route->gw_src) {
227 gw_tmp = route->gw_src;
228 route->gw_src = route->gw_dst;
229 route->gw_dst = gw_tmp;
231 if (TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())) {
234 xbt_dynar_t link_route_to_test =
235 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
236 for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
237 link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
238 void *link = Link::byName(link_name);
239 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
240 xbt_dynar_push(link_route_to_test, &link);
242 xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list,
244 full_pointer_resource_cmp),
245 "The route between \"%s\" and \"%s\" already exists", src,
248 if (!route->gw_dst && !route->gw_src)
249 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
251 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
252 dst, route->gw_src->getName(), src, route->gw_dst->getName());
253 TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId()) = newExtendedRoute(hierarchy_, route, 0);
254 xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list, 0);
257 xbt_dynar_free(&route->link_list);