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(const char *name) : As(name) { }
18 AsImpl::~AsImpl() = default;
20 xbt_dynar_t AsImpl::getOneLinkRoutes() {
24 /** @brief Get the common ancestor and its first childs in each line leading to src and dst */
25 static void find_common_ancestors(NetCard *src, NetCard *dst,
26 /* OUT */ AsImpl **common_ancestor, AsImpl **src_ancestor, AsImpl **dst_ancestor)
28 #define ROUTING_HIERARCHY_MAXDEPTH 32 /* increase if it is not enough */
29 AsImpl *path_src[ROUTING_HIERARCHY_MAXDEPTH];
30 AsImpl *path_dst[ROUTING_HIERARCHY_MAXDEPTH];
37 /* (1) find the path to root of src and dst*/
38 AsImpl *src_as = src->containingAS();
39 AsImpl *dst_as = dst->containingAS();
41 xbt_assert(src_as, "Host %s must be in an AS", src->name());
42 xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
44 /* (2) find the path to the root routing component */
45 for (AsImpl *current = src_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
46 xbt_assert(index_src < ROUTING_HIERARCHY_MAXDEPTH, "ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
47 path_src[index_src++] = current;
49 for (AsImpl *current = dst_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
50 xbt_assert(index_dst < ROUTING_HIERARCHY_MAXDEPTH,"ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
51 path_dst[index_dst++] = current;
54 /* (3) find the common father.
55 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
56 * So we move them down simultaneously as long as they point to the same content.
59 current_src = path_src[--index_src];
60 current_dst = path_dst[--index_dst];
61 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
63 /* (4) if we did not find a difference (index_src or index_dst went to 0), both elements are in the same AS */
64 if (current_src == current_dst)
66 else // we found a difference
67 father = path_src[index_src + 1];
69 /* (5) result generation */
70 *common_ancestor = father; /* the common father of src and dst */
71 *src_ancestor = current_src; /* the first different father of src */
72 *dst_ancestor = current_dst; /* the first different father of dst */
73 #undef ROUTING_HIERARCHY_MAXDEPTH
77 /* PRECONDITION: this is the common ancestor of src and dst */
78 std::vector<surf::Link*> *AsImpl::getBypassRoute(routing::NetCard *src, routing::NetCard *dst)
80 // If never set a bypass route return nullptr without any further computations
81 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
82 if (bypassRoutes_.empty())
85 std::vector<surf::Link*> *bypassedRoute = nullptr;
87 if(dst->containingAS() == this && src->containingAS() == this ){
88 if (bypassRoutes_.find({src->name(),dst->name()}) != bypassRoutes_.end()) {
89 bypassedRoute = bypassRoutes_.at({src->name(),dst->name()});
90 XBT_DEBUG("Found a bypass route with %zu links",bypassedRoute->size());
95 /* (2) find the path to the root routing component */
96 std::vector<As*> path_src;
97 As *current = src->containingAS();
98 while (current != nullptr) {
99 path_src.push_back(current);
100 current = current->father_;
103 std::vector<As*> path_dst;
104 current = dst->containingAS();
105 while (current != nullptr) {
106 path_dst.push_back(current);
107 current = current->father_;
110 /* (3) find the common father */
111 while (path_src.size() > 1 && path_dst.size() >1
112 && path_src.at(path_src.size() -1) == path_dst.at(path_dst.size() -1)) {
117 int max_index_src = path_src.size() - 1;
118 int max_index_dst = path_dst.size() - 1;
120 int max_index = std::max(max_index_src, max_index_dst);
122 for (int max = 0; max <= max_index; max++) {
123 for (int i = 0; i < max; i++) {
124 if (i <= max_index_src && max <= max_index_dst) {
125 const std::pair<std::string, std::string> key = {path_src.at(i)->name(), path_dst.at(max)->name()};
126 if (bypassRoutes_.find(key) != bypassRoutes_.end())
127 bypassedRoute = bypassRoutes_.at(key);
131 if (max <= max_index_src && i <= max_index_dst) {
132 const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(i)->name()};
133 if (bypassRoutes_.find(key) != bypassRoutes_.end())
134 bypassedRoute = bypassRoutes_.at(key);
143 if (max <= max_index_src && max <= max_index_dst) {
144 const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(max)->name()};
145 if (bypassRoutes_.find(key) != bypassRoutes_.end())
146 bypassedRoute = bypassRoutes_.at(key);
152 return bypassedRoute;
156 * \brief Recursive function for getRouteAndLatency
158 * \param src the source host
159 * \param dst the destination host
160 * \param links Where to store the links and the gw information
161 * \param latency If not nullptr, the latency of all links will be added in it
163 void AsImpl::getRouteRecursive(routing::NetCard *src, routing::NetCard *dst,
164 /* OUT */ std::vector<surf::Link*> * links, double *latency)
166 s_sg_platf_route_cbarg_t route;
167 memset(&route,0,sizeof(route));
169 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name(), dst->name());
171 /* Find how src and dst are interconnected */
172 AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
173 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
174 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
175 common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
177 /* Check whether a direct bypass is defined. If so, use it and bail out */
178 std::vector<surf::Link*> *bypassed_route = common_ancestor->getBypassRoute(src, dst);
179 if (nullptr != bypassed_route) {
180 for (surf::Link *link : *bypassed_route) {
181 links->push_back(link);
183 *latency += link->getLatency();
188 /* If src and dst are in the same AS, life is good */
189 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
190 route.link_list = links;
191 common_ancestor->getRouteAndLatency(src, dst, &route, latency);
195 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
197 route.link_list = new std::vector<surf::Link*>();
199 common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
200 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr),
201 "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
203 /* If source gateway is not our source, we have to recursively find our way up to this point */
204 if (src != route.gw_src)
205 getRouteRecursive(src, route.gw_src, links, latency);
206 for (auto link: *route.link_list)
207 links->push_back(link);
208 delete route.link_list;
210 /* If dest gateway is not our destination, we have to recursively find our way from this point */
211 if (route.gw_dst != dst)
212 getRouteRecursive(route.gw_dst, dst, links, latency);