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]
19 sg_platf_route_cbarg_t e_route;
22 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
24 /* Create table if necessary */
26 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
28 /* Add the loopback if needed */
29 if (routing_platf->p_loopback && p_hierarchy == SURF_ROUTING_BASE) {
30 for (i = 0; i < table_size; i++) {
31 e_route = TO_ROUTE_FULL(i, i);
33 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
34 e_route->gw_src = NULL;
35 e_route->gw_dst = NULL;
36 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
37 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
38 TO_ROUTE_FULL(i, i) = e_route;
46 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
48 /* Delete routing table */
49 for (i = 0; i < table_size; i++)
50 for (j = 0; j < table_size; j++) {
51 if (TO_ROUTE_FULL(i,j)){
52 xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
53 xbt_free(TO_ROUTE_FULL(i,j));
56 xbt_free(p_routingTable);
60 xbt_dynar_t AsFull::getOneLinkRoutes()
62 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
65 int table_size = xbt_dynar_length(p_indexNetworkElm);
67 for(src=0; src < table_size; src++) {
68 for(dst=0; dst< table_size; dst++) {
69 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
71 if (xbt_dynar_length(route->link_list) == 1) {
72 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
74 if (p_hierarchy == SURF_ROUTING_BASE) {
75 NetCard *tmp_src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_netcard_t);
77 NetCard *tmp_dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_netcard_t);
79 onelink = new Onelink(link, tmp_src, tmp_dst);
80 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
81 onelink = new Onelink(link, route->gw_src, route->gw_dst);
83 onelink = new Onelink(link, NULL, NULL);
84 xbt_dynar_push(ret, &onelink);
85 XBT_DEBUG("Push route from '%d' to '%d'",
95 void AsFull::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
97 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
104 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
106 sg_platf_route_cbarg_t e_route = NULL;
108 unsigned int cpt = 0;
110 e_route = TO_ROUTE_FULL(src->getId(), dst->getId());
113 res->gw_src = e_route->gw_src;
114 res->gw_dst = e_route->gw_dst;
115 xbt_dynar_foreach(e_route->link_list, cpt, link) {
116 xbt_dynar_push(res->link_list, &link);
118 *lat += static_cast<Link*>(link)->getLatency();
123 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
127 static int full_pointer_resource_cmp(const void *a, const void *b)
132 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
135 char *src = (char*)(route->src);
136 char *dst = (char*)(route->dst);
137 NetCard *src_net_elm, *dst_net_elm;
138 src_net_elm = sg_netcard_by_name_or_null(src);
139 dst_net_elm = sg_netcard_by_name_or_null(dst);
141 xbt_assert(src_net_elm, "Network elements %s not found", src);
142 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
144 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
146 xbt_assert(!xbt_dynar_is_empty(route->link_list),
147 "Invalid count of links, must be greater than zero (%s,%s)",
151 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
153 if (TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())) {
156 xbt_dynar_t link_route_to_test =
157 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
158 xbt_dynar_foreach(route->link_list, i, link_name) {
159 void *link = Link::byName(link_name);
160 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
161 xbt_dynar_push(link_route_to_test, &link);
163 if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->link_list,
164 link_route_to_test, full_pointer_resource_cmp)) {
165 surf_parse_error("A route between \"%s\" and \"%s\" already exists "
166 "with a different content. "
167 "If you are trying to define a reverse route, "
168 "you must set the symmetrical=no attribute to "
169 "your routes tags.", src, dst);
171 surf_parse_warn("Ignoring the identical redefinition of the route "
172 "between \"%s\" and \"%s\"", src, dst);
175 if (!route->gw_src && !route->gw_dst)
176 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
178 // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
179 // AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
180 // if (subas == NULL)
181 // surf_parse_error("The source of an ASroute must be a sub-AS "
182 // "declared within the current AS, "
183 // "but '%s' is not an AS within '%s'", src, rc->name);
184 // if (subas->to_index
185 // && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
186 // surf_parse_error("In an ASroute, source gateway must be part of "
187 // "the source sub-AS (in particular, being in a "
188 // "sub-sub-AS is not allowed), "
189 // "but '%s' is not in '%s'.",
190 // route->src_gateway, subas->name);
192 // subas = xbt_dict_get_or_null(rc->routing_sons, dst);
193 // if (subas == NULL)
194 // surf_parse_error("The destination of an ASroute must be a sub-AS "
195 // "declared within the current AS, "
196 // "but '%s' is not an AS within '%s'", dst, rc->name);
197 // if (subas->to_index
198 // && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
199 // surf_parse_error("In an ASroute, destination gateway must be "
200 // "part of the destination sub-AS (in particular, "
201 // "in a sub-sub-AS is not allowed), "
202 // "but '%s' is not in '%s'.",
203 // route->dst_gateway, subas->name);
205 XBT_DEBUG("Load ASroute from \"%s\" to \"%s\"", src, dst);
206 if (!route->gw_src ||
207 route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
208 surf_parse_error("The src_gateway \"%s\" does not exist!",
209 route->gw_src ? route->gw_src->getName() : "(null)");
210 if (!route->gw_dst ||
211 route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
212 surf_parse_error("The dst_gateway \"%s\" does not exist!",
213 route->gw_dst ? route->gw_dst->getName() : "(null)");
214 XBT_DEBUG("ASroute goes from \"%s\" to \"%s\"",
215 route->gw_src->getName(), route->gw_dst->getName());
217 TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId()) = newExtendedRoute(p_hierarchy, route, 1);
218 xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->link_list, 0);
221 if ( (route->symmetrical == TRUE && as_route == 0)
222 || (route->symmetrical == TRUE && as_route == 1)
224 if (route->gw_dst && route->gw_src) {
226 gw_tmp = route->gw_src;
227 route->gw_src = route->gw_dst;
228 route->gw_dst = gw_tmp;
230 if (TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())) {
233 xbt_dynar_t link_route_to_test =
234 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
235 for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
236 link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
237 void *link = Link::byName(link_name);
238 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
239 xbt_dynar_push(link_route_to_test, &link);
241 xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list,
243 full_pointer_resource_cmp),
244 "The route between \"%s\" and \"%s\" already exists", src,
247 if (!route->gw_dst && !route->gw_src)
248 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
250 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
251 dst, route->gw_src->getName(), src, route->gw_dst->getName());
252 TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId()) = newExtendedRoute(p_hierarchy, route, 0);
253 xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list, 0);
256 xbt_dynar_free(&route->link_list);