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 "simgrid/s4u/host.hpp"
9 #include "src/kernel/routing/AsImpl.hpp"
10 #include "src/surf/network_interface.hpp" // Link FIXME: move to proper header
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(AsImpl,surf, "Implementation of S4U autonomous systems");
18 AsImpl::AsImpl(As* father, const char* name) : As(father, name)
20 xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL),
21 "Refusing to create a second AS called '%s'.", name);
23 netcard_ = new NetCardImpl(name, NetCard::Type::As, static_cast<AsImpl*>(father));
24 xbt_lib_set(as_router_lib, name, ROUTING_ASR_LEVEL, static_cast<void*>(netcard_));
25 XBT_DEBUG("AS '%s' created with the id '%d'", name, netcard_->id());
27 AsImpl::~AsImpl() = default;
29 void AsImpl::attachHost(s4u::Host* host)
31 if (hierarchy_ == RoutingMode::unset)
32 hierarchy_ = RoutingMode::base;
34 host->pimpl_netcard = new NetCardImpl(host->name().c_str(), NetCard::Type::Host, this);
37 xbt_dynar_t AsImpl::getOneLinkRoutes()
42 /** @brief Get the common ancestor and its first children in each line leading to src and dst
44 * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
48 * ... <- possibly long path
56 * src_ancestor dst_ancestor <- must be different in the recursive case
58 * ... ... <-- possibly long pathes (one hop or more)
63 * In the base case (when src and dst are in the same AS), things are as follows:
67 * ... <-- possibly long path
69 * common_ancestor==src_ancestor==dst_ancestor <-- all the same value
71 * / \ <-- direct links (exactly one hop)
76 * A specific recursive case occurs when src is the ancestor of dst. In this case,
77 * the base case routing should be used so the common_ancestor is specifically set
78 * to src_ancestor==dst_ancestor.
79 * Naturally, things are completely symmetrical if dst is the ancestor of src.
83 * ... <-- possibly long path
85 * src == src_ancestor==dst_ancestor==common_ancestor <-- same value
87 * ... <-- possibly long path (one hop or more)
92 static void find_common_ancestors(NetCard* src, NetCard* dst,
93 /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
95 /* Deal with the easy base case */
96 if (src->containingAS() == dst->containingAS()) {
97 *common_ancestor = src->containingAS();
98 *src_ancestor = *common_ancestor;
99 *dst_ancestor = *common_ancestor;
103 /* engage the full recursive search */
105 /* (1) find the path to root of src and dst*/
106 AsImpl* src_as = src->containingAS();
107 AsImpl* dst_as = dst->containingAS();
109 xbt_assert(src_as, "Host %s must be in an AS", src->name().c_str());
110 xbt_assert(dst_as, "Host %s must be in an AS", dst->name().c_str());
112 /* (2) find the path to the root routing component */
113 std::vector<AsImpl*> path_src;
114 AsImpl* current = src->containingAS();
115 while (current != nullptr) {
116 path_src.push_back(current);
117 current = static_cast<AsImpl*>(current->father());
119 std::vector<AsImpl*> path_dst;
120 current = dst->containingAS();
121 while (current != nullptr) {
122 path_dst.push_back(current);
123 current = static_cast<AsImpl*>(current->father());
126 /* (3) find the common father.
127 * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
128 * So we move them down simultaneously as long as they point to the same content.
130 * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
132 AsImpl* father = nullptr; // the AS we dropped on the previous loop iteration
133 while (path_src.size() > 1 && path_dst.size() > 1 &&
134 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
135 father = path_src.at(path_src.size() - 1);
140 /* (4) we found the difference at least. Finalize the returned values */
141 *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
142 *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
143 if (*src_ancestor == *dst_ancestor) { // src is the ancestor of dst, or the contrary
144 *common_ancestor = *src_ancestor;
146 *common_ancestor = father;
150 /* PRECONDITION: this is the common ancestor of src and dst */
151 bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
152 /* OUT */ std::vector<surf::Link*>* links, double* latency)
154 // If never set a bypass route return nullptr without any further computations
155 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name().c_str(), dst->name().c_str());
156 if (bypassRoutes_.empty())
159 /* Base case, no recursion is needed */
160 if (dst->containingAS() == this && src->containingAS() == this) {
161 if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
162 AsRoute* bypassedRoute = bypassRoutes_.at({src, dst});
163 for (surf::Link* link : bypassedRoute->links) {
164 links->push_back(link);
166 *latency += link->latency();
168 XBT_DEBUG("Found a bypass route with %zu links", bypassedRoute->links.size());
174 /* Engage recursive search */
177 /* (1) find the path to the root routing component */
178 std::vector<AsImpl*> path_src;
179 As* current = src->containingAS();
180 while (current != nullptr) {
181 path_src.push_back(static_cast<AsImpl*>(current));
182 current = current->father_;
185 std::vector<AsImpl*> path_dst;
186 current = dst->containingAS();
187 while (current != nullptr) {
188 path_dst.push_back(static_cast<AsImpl*>(current));
189 current = current->father_;
192 /* (2) find the common father */
193 while (path_src.size() > 1 && path_dst.size() > 1 &&
194 path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
199 int max_index_src = path_src.size() - 1;
200 int max_index_dst = path_dst.size() - 1;
202 int max_index = std::max(max_index_src, max_index_dst);
204 /* (3) Search for a bypass making the path up to the ancestor useless */
205 AsRoute* bypassedRoute = nullptr;
206 std::pair<kernel::routing::NetCard*, kernel::routing::NetCard*> key;
207 for (int max = 0; max <= max_index; max++) {
208 for (int i = 0; i < max; i++) {
209 if (i <= max_index_src && max <= max_index_dst) {
210 key = {path_src.at(i)->netcard_, path_dst.at(max)->netcard_};
211 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
212 bypassedRoute = bypassRoutes_.at(key);
216 if (max <= max_index_src && i <= max_index_dst) {
217 key = {path_src.at(max)->netcard_, path_dst.at(i)->netcard_};
218 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
219 bypassedRoute = bypassRoutes_.at(key);
228 if (max <= max_index_src && max <= max_index_dst) {
229 key = {path_src.at(max)->netcard_, path_dst.at(max)->netcard_};
230 if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
231 bypassedRoute = bypassRoutes_.at(key);
237 /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
239 if (src != key.first)
240 getRouteRecursive(src, const_cast<NetCard*>(bypassedRoute->gw_src), links, latency);
241 for (surf::Link* link : bypassedRoute->links) {
242 links->push_back(link);
244 *latency += link->latency();
246 if (dst != key.second)
247 getRouteRecursive(const_cast<NetCard*>(bypassedRoute->gw_dst), dst, links, latency);
254 * \brief Recursive function for getRouteAndLatency
256 * \param src the source host
257 * \param dst the destination host
258 * \param links Where to store the links and the gw information
259 * \param latency If not nullptr, the latency of all links will be added in it
261 void AsImpl::getRouteRecursive(routing::NetCard *src, routing::NetCard *dst,
262 /* OUT */ std::vector<surf::Link*> * links, double *latency)
264 s_sg_platf_route_cbarg_t route;
265 memset(&route,0,sizeof(route));
267 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name().c_str(), dst->name().c_str());
269 /* Find how src and dst are interconnected */
270 AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
271 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
272 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
273 common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
275 /* Check whether a direct bypass is defined. If so, use it and bail out */
276 if (common_ancestor->getBypassRoute(src, dst, links, latency))
279 /* If src and dst are in the same AS, life is good */
280 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
281 route.link_list = links;
282 common_ancestor->getRouteAndLatency(src, dst, &route, latency);
286 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
288 route.link_list = new std::vector<surf::Link*>();
290 common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
291 xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
292 src->name().c_str(), dst->name().c_str());
294 /* If source gateway is not our source, we have to recursively find our way up to this point */
295 if (src != route.gw_src)
296 getRouteRecursive(src, route.gw_src, links, latency);
297 for (auto link: *route.link_list)
298 links->push_back(link);
299 delete route.link_list;
301 /* If dest gateway is not our destination, we have to recursively find our way from this point */
302 if (route.gw_dst != dst)
303 getRouteRecursive(route.gw_dst, dst, links, latency);