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_full.hpp"
8 #include "src/surf/network_interface.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
12 #define TO_ROUTE_FULL(i,j) routingTable_[(i)+(j)*table_size]
16 AsFull::AsFull(const char*name)
23 sg_platf_route_cbarg_t e_route;
26 int table_size = (int)xbt_dynar_length(vertices_);
28 /* Create table if necessary */
30 routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
32 /* Add the loopback if needed */
33 if (routing_platf->loopback_ && hierarchy_ == SURF_ROUTING_BASE) {
34 for (i = 0; i < table_size; i++) {
35 e_route = TO_ROUTE_FULL(i, i);
37 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
38 e_route->gw_src = NULL;
39 e_route->gw_dst = NULL;
40 e_route->link_list = new std::vector<Link*>();
41 e_route->link_list->push_back(routing_platf->loopback_);
42 TO_ROUTE_FULL(i, i) = e_route;
50 int table_size = (int)xbt_dynar_length(vertices_);
52 /* Delete routing table */
53 for (i = 0; i < table_size; i++)
54 for (j = 0; j < table_size; j++) {
55 if (TO_ROUTE_FULL(i,j)){
56 delete TO_ROUTE_FULL(i,j)->link_list;
57 xbt_free(TO_ROUTE_FULL(i,j));
60 xbt_free(routingTable_);
64 xbt_dynar_t AsFull::getOneLinkRoutes()
66 xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
69 int table_size = xbt_dynar_length(vertices_);
71 for(src=0; src < table_size; src++) {
72 for(dst=0; dst< table_size; dst++) {
73 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
75 if (route->link_list->size() == 1) {
76 Link *link = route->link_list->at(0);
78 if (hierarchy_ == SURF_ROUTING_BASE) {
79 NetCard *tmp_src = xbt_dynar_get_as(vertices_, src, sg_netcard_t);
81 NetCard *tmp_dst = xbt_dynar_get_as(vertices_, dst, sg_netcard_t);
83 onelink = new Onelink(link, tmp_src, tmp_dst);
84 } else if (hierarchy_ == SURF_ROUTING_RECURSIVE)
85 onelink = new Onelink(link, route->gw_src, route->gw_dst);
87 onelink = new Onelink(link, NULL, NULL);
88 xbt_dynar_push(ret, &onelink);
89 XBT_DEBUG("Push route from '%d' to '%d'",
99 void AsFull::getRouteAndLatency(NetCard *src, NetCard *dst, sg_platf_route_cbarg_t res, double *lat)
101 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
108 size_t table_size = xbt_dynar_length(vertices_);
110 sg_platf_route_cbarg_t e_route = NULL;
112 e_route = TO_ROUTE_FULL(src->id(), dst->id());
115 res->gw_src = e_route->gw_src;
116 res->gw_dst = e_route->gw_dst;
117 for (auto link : *e_route->link_list) {
118 res->link_list->push_back(link);
120 *lat += static_cast<Link*>(link)->getLatency();
125 void AsFull::addRoute(sg_platf_route_cbarg_t route)
127 const char *src = route->src;
128 const char *dst = route->dst;
129 NetCard *src_net_elm = sg_netcard_by_name_or_null(src);
130 NetCard *dst_net_elm = sg_netcard_by_name_or_null(dst);
132 addRouteCheckParams(route);
134 size_t table_size = xbt_dynar_length(vertices_);
137 routingTable_ = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
139 /* Check that the route does not already exist */
140 if (route->gw_dst) // AS route (to adapt the error message, if any)
141 xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
142 "The route between %s@%s and %s@%s already exists (Rq: routes are symmetrical by default).",
143 src,route->gw_src->name(),dst,route->gw_dst->name());
145 xbt_assert(nullptr == TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()),
146 "The route between %s and %s already exists (Rq: routes are symmetrical by default).", src,dst);
148 /* Add the route to the base */
149 TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id()) = newExtendedRoute(hierarchy_, route, 1);
150 TO_ROUTE_FULL(src_net_elm->id(), dst_net_elm->id())->link_list->shrink_to_fit();
152 if (route->symmetrical == TRUE && src_net_elm != dst_net_elm) {
153 if (route->gw_dst && route->gw_src) {
154 NetCard* gw_tmp = route->gw_src;
155 route->gw_src = route->gw_dst;
156 route->gw_dst = gw_tmp;
158 if (route->gw_dst) // AS route (to adapt the error message, if any)
159 xbt_assert(nullptr == TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()),
160 "The route between %s@%s and %s@%s already exists. You should not declare the reverse path as symmetrical.",
161 dst,route->gw_dst->name(),src,route->gw_src->name());
163 xbt_assert(nullptr == TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()),
164 "The route between %s and %s already exists. You should not declare the reverse path as symmetrical.", dst,src);
166 TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id()) = newExtendedRoute(hierarchy_, route, 0);
167 TO_ROUTE_FULL(dst_net_elm->id(), src_net_elm->id())->link_list->shrink_to_fit();