1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "src/kernel/routing/AsImpl.hpp"
9 #include "src/surf/network_interface.hpp" // Link FIXME: move to proper header
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(AsImpl,surf, "Implementation of S4U autonomous systems");
17 AsImpl::AsImpl(As* father, const char* name) : As(father, name)
20 AsImpl::~AsImpl() = default;
22 xbt_dynar_t AsImpl::getOneLinkRoutes()
27 /** @brief Get the common ancestor and its first childs in each line leading to src and dst */
28 static void find_common_ancestors(NetCard *src, NetCard *dst,
29 /* OUT */ AsImpl **common_ancestor, AsImpl **src_ancestor, AsImpl **dst_ancestor)
31 #define ROUTING_HIERARCHY_MAXDEPTH 32 /* increase if it is not enough */
32 AsImpl *path_src[ROUTING_HIERARCHY_MAXDEPTH];
33 AsImpl *path_dst[ROUTING_HIERARCHY_MAXDEPTH];
40 /* (1) find the path to root of src and dst*/
41 AsImpl *src_as = src->containingAS();
42 AsImpl *dst_as = dst->containingAS();
44 xbt_assert(src_as, "Host %s must be in an AS", src->name());
45 xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
47 /* (2) find the path to the root routing component */
48 for (AsImpl *current = src_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
49 xbt_assert(index_src < ROUTING_HIERARCHY_MAXDEPTH, "ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
50 path_src[index_src++] = current;
52 for (AsImpl *current = dst_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
53 xbt_assert(index_dst < ROUTING_HIERARCHY_MAXDEPTH,"ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
54 path_dst[index_dst++] = current;
57 /* (3) find the common father.
58 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
59 * So we move them down simultaneously as long as they point to the same content.
62 current_src = path_src[--index_src];
63 current_dst = path_dst[--index_dst];
64 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
66 /* (4) if we did not find a difference (index_src or index_dst went to 0), both elements are in the same AS */
67 if (current_src == current_dst)
69 else // we found a difference
70 father = path_src[index_src + 1];
72 /* (5) result generation */
73 *common_ancestor = father; /* the common father of src and dst */
74 *src_ancestor = current_src; /* the first different father of src */
75 *dst_ancestor = current_dst; /* the first different father of dst */
76 #undef ROUTING_HIERARCHY_MAXDEPTH
80 /* PRECONDITION: this is the common ancestor of src and dst */
81 std::vector<surf::Link*> *AsImpl::getBypassRoute(routing::NetCard *src, routing::NetCard *dst)
83 // If never set a bypass route return nullptr without any further computations
84 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
85 if (bypassRoutes_.empty())
88 std::vector<surf::Link*> *bypassedRoute = nullptr;
90 if(dst->containingAS() == this && src->containingAS() == this ){
91 if (bypassRoutes_.find({src->name(),dst->name()}) != bypassRoutes_.end()) {
92 bypassedRoute = bypassRoutes_.at({src->name(),dst->name()});
93 XBT_DEBUG("Found a bypass route with %zu links",bypassedRoute->size());
98 /* (2) find the path to the root routing component */
99 std::vector<As*> path_src;
100 As *current = src->containingAS();
101 while (current != nullptr) {
102 path_src.push_back(current);
103 current = current->father_;
106 std::vector<As*> path_dst;
107 current = dst->containingAS();
108 while (current != nullptr) {
109 path_dst.push_back(current);
110 current = current->father_;
113 /* (3) find the common father */
114 while (path_src.size() > 1 && path_dst.size() >1
115 && path_src.at(path_src.size() -1) == path_dst.at(path_dst.size() -1)) {
120 int max_index_src = path_src.size() - 1;
121 int max_index_dst = path_dst.size() - 1;
123 int max_index = std::max(max_index_src, max_index_dst);
125 for (int max = 0; max <= max_index; max++) {
126 for (int i = 0; i < max; i++) {
127 if (i <= max_index_src && max <= max_index_dst) {
128 const std::pair<std::string, std::string> key = {path_src.at(i)->name(), path_dst.at(max)->name()};
129 if (bypassRoutes_.find(key) != bypassRoutes_.end())
130 bypassedRoute = bypassRoutes_.at(key);
134 if (max <= max_index_src && i <= max_index_dst) {
135 const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(i)->name()};
136 if (bypassRoutes_.find(key) != bypassRoutes_.end())
137 bypassedRoute = bypassRoutes_.at(key);
146 if (max <= max_index_src && max <= max_index_dst) {
147 const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(max)->name()};
148 if (bypassRoutes_.find(key) != bypassRoutes_.end())
149 bypassedRoute = bypassRoutes_.at(key);
155 return bypassedRoute;
159 * \brief Recursive function for getRouteAndLatency
161 * \param src the source host
162 * \param dst the destination host
163 * \param links Where to store the links and the gw information
164 * \param latency If not nullptr, the latency of all links will be added in it
166 void AsImpl::getRouteRecursive(routing::NetCard *src, routing::NetCard *dst,
167 /* OUT */ std::vector<surf::Link*> * links, double *latency)
169 s_sg_platf_route_cbarg_t route;
170 memset(&route,0,sizeof(route));
172 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name(), dst->name());
174 /* Find how src and dst are interconnected */
175 AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
176 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
177 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
178 common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
180 /* Check whether a direct bypass is defined. If so, use it and bail out */
181 std::vector<surf::Link*> *bypassed_route = common_ancestor->getBypassRoute(src, dst);
182 if (nullptr != bypassed_route) {
183 for (surf::Link *link : *bypassed_route) {
184 links->push_back(link);
186 *latency += link->getLatency();
191 /* If src and dst are in the same AS, life is good */
192 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
193 route.link_list = links;
194 common_ancestor->getRouteAndLatency(src, dst, &route, latency);
198 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
200 route.link_list = new std::vector<surf::Link*>();
202 common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
203 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr),
204 "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
206 /* If source gateway is not our source, we have to recursively find our way up to this point */
207 if (src != route.gw_src)
208 getRouteRecursive(src, route.gw_src, links, latency);
209 for (auto link: *route.link_list)
210 links->push_back(link);
211 delete route.link_list;
213 /* If dest gateway is not our destination, we have to recursively find our way from this point */
214 if (route.gw_dst != dst)
215 getRouteRecursive(route.gw_dst, dst, links, latency);