1 /* Copyright (c) 2009-2014. 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_full.hpp"
8 #include "network_interface.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
13 extern routing_platf_t routing_platf;
15 #define TO_ROUTE_FULL(i,j) p_routingTable[(i)+(j)*table_size]
17 AS_t model_full_create(void)
22 void model_full_end(AS_t _routing)
25 sg_platf_route_cbarg_t e_route;
28 AsFull *routing = static_cast<AsFull*>(_routing);
29 int table_size = (int)xbt_dynar_length(routing->p_indexNetworkElm);
31 /* Create table if necessary */
32 if (!routing->p_routingTable)
33 routing->p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
35 /* Add the loopback if needed */
36 if (routing_platf->p_loopback && routing->p_hierarchy == SURF_ROUTING_BASE) {
37 for (i = 0; i < table_size; i++) {
38 e_route = routing->TO_ROUTE_FULL(i, i);
40 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
41 e_route->gw_src = NULL;
42 e_route->gw_dst = NULL;
43 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
44 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
45 routing->TO_ROUTE_FULL(i, i) = e_route;
57 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
59 /* Delete routing table */
60 for (i = 0; i < table_size; i++)
61 for (j = 0; j < table_size; j++) {
62 if (TO_ROUTE_FULL(i,j)){
63 xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
64 xbt_free(TO_ROUTE_FULL(i,j));
67 xbt_free(p_routingTable);
71 xbt_dynar_t AsFull::getOneLinkRoutes()
73 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
76 int table_size = xbt_dynar_length(p_indexNetworkElm);
78 for(src=0; src < table_size; src++) {
79 for(dst=0; dst< table_size; dst++) {
80 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
82 if (xbt_dynar_length(route->link_list) == 1) {
83 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
85 if (p_hierarchy == SURF_ROUTING_BASE) {
86 RoutingEdge *tmp_src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_routing_edge_t);
88 RoutingEdge *tmp_dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_routing_edge_t);
90 onelink = new Onelink(link, tmp_src, tmp_dst);
91 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
92 onelink = new Onelink(link, route->gw_src, route->gw_dst);
94 onelink = new Onelink(link, NULL, NULL);
95 xbt_dynar_push(ret, &onelink);
96 XBT_DEBUG("Push route from '%d' to '%d'",
106 void AsFull::getRouteAndLatency(RoutingEdge *src, RoutingEdge *dst, sg_platf_route_cbarg_t res, double *lat)
108 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
115 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
117 sg_platf_route_cbarg_t e_route = NULL;
119 unsigned int cpt = 0;
121 e_route = TO_ROUTE_FULL(src->getId(), dst->getId());
124 res->gw_src = e_route->gw_src;
125 res->gw_dst = e_route->gw_dst;
126 xbt_dynar_foreach(e_route->link_list, cpt, link) {
127 xbt_dynar_push(res->link_list, &link);
129 *lat += static_cast<Link*>(link)->getLatency();
134 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
138 static int full_pointer_resource_cmp(const void *a, const void *b)
143 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
146 char *src = (char*)(route->src);
147 char *dst = (char*)(route->dst);
148 RoutingEdge *src_net_elm, *dst_net_elm;
149 src_net_elm = sg_routing_edge_by_name_or_null(src);
150 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
152 xbt_assert(src_net_elm, "Network elements %s not found", src);
153 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
155 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
157 xbt_assert(!xbt_dynar_is_empty(route->link_list),
158 "Invalid count of links, must be greater than zero (%s,%s)",
162 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
164 if (TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())) {
167 xbt_dynar_t link_route_to_test =
168 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
169 xbt_dynar_foreach(route->link_list, i, link_name) {
170 void *link = Link::byName(link_name);
171 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
172 xbt_dynar_push(link_route_to_test, &link);
174 if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->link_list,
175 link_route_to_test, full_pointer_resource_cmp)) {
176 surf_parse_error("A route between \"%s\" and \"%s\" already exists "
177 "with a different content. "
178 "If you are trying to define a reverse route, "
179 "you must set the symmetrical=no attribute to "
180 "your routes tags.", src, dst);
182 surf_parse_warn("Ignoring the identical redefinition of the route "
183 "between \"%s\" and \"%s\"", src, dst);
186 if (!route->gw_src && !route->gw_dst)
187 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
189 // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
190 // AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
191 // if (subas == NULL)
192 // surf_parse_error("The source of an ASroute must be a sub-AS "
193 // "declared within the current AS, "
194 // "but '%s' is not an AS within '%s'", src, rc->name);
195 // if (subas->to_index
196 // && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
197 // surf_parse_error("In an ASroute, source gateway must be part of "
198 // "the source sub-AS (in particular, being in a "
199 // "sub-sub-AS is not allowed), "
200 // "but '%s' is not in '%s'.",
201 // route->src_gateway, subas->name);
203 // subas = xbt_dict_get_or_null(rc->routing_sons, dst);
204 // if (subas == NULL)
205 // surf_parse_error("The destination of an ASroute must be a sub-AS "
206 // "declared within the current AS, "
207 // "but '%s' is not an AS within '%s'", dst, rc->name);
208 // if (subas->to_index
209 // && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
210 // surf_parse_error("In an ASroute, destination gateway must be "
211 // "part of the destination sub-AS (in particular, "
212 // "in a sub-sub-AS is not allowed), "
213 // "but '%s' is not in '%s'.",
214 // route->dst_gateway, subas->name);
216 XBT_DEBUG("Load ASroute from \"%s\" to \"%s\"", src, dst);
217 if (!route->gw_src ||
218 route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
219 surf_parse_error("The src_gateway \"%s\" does not exist!",
220 route->gw_src ? route->gw_src->getName() : "(null)");
221 if (!route->gw_dst ||
222 route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
223 surf_parse_error("The dst_gateway \"%s\" does not exist!",
224 route->gw_dst ? route->gw_dst->getName() : "(null)");
225 XBT_DEBUG("ASroute goes from \"%s\" to \"%s\"",
226 route->gw_src->getName(), route->gw_dst->getName());
228 TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId()) = newExtendedRoute(p_hierarchy, route, 1);
229 xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->link_list, 0);
232 if ( (route->symmetrical == TRUE && as_route == 0)
233 || (route->symmetrical == TRUE && as_route == 1)
235 if (route->gw_dst && route->gw_src) {
236 sg_routing_edge_t gw_tmp;
237 gw_tmp = route->gw_src;
238 route->gw_src = route->gw_dst;
239 route->gw_dst = gw_tmp;
241 if (TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())) {
244 xbt_dynar_t link_route_to_test =
245 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
246 for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
247 link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
248 void *link = Link::byName(link_name);
249 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
250 xbt_dynar_push(link_route_to_test, &link);
252 xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list,
254 full_pointer_resource_cmp),
255 "The route between \"%s\" and \"%s\" already exists", src,
258 if (!route->gw_dst && !route->gw_src)
259 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
261 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
262 dst, route->gw_src->getName(), src, route->gw_dst->getName());
263 TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId()) = newExtendedRoute(p_hierarchy, route, 0);
264 xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list, 0);
267 xbt_dynar_free(&route->link_list);