Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
improve the doc of the routing API
[simgrid.git] / src / kernel / routing / AsImpl.cpp
1 /* Copyright (c) 2006-2016. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "xbt/log.h"
7
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
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(AsImpl,surf, "Implementation of S4U autonomous systems");
13
14 namespace simgrid {
15   namespace kernel {
16   namespace routing {
17
18   AsImpl::AsImpl(As* father, const char* name) : As(father, name)
19   {
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);
22
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());
26   }
27   AsImpl::~AsImpl() = default;
28
29   void AsImpl::attachHost(s4u::Host* host)
30   {
31     if (hierarchy_ == RoutingMode::unset)
32       hierarchy_ = RoutingMode::base;
33
34     host->pimpl_netcard = new NetCardImpl(host->name().c_str(), NetCard::Type::Host, this);
35   }
36
37   xbt_dynar_t AsImpl::getOneLinkRoutes()
38   {
39     return nullptr;
40   }
41
42   /** @brief Get the common ancestor and its first children in each line leading to src and dst
43    *
44    * After this call, common_ancestor, src_ancestor and dst_ancestor are set as follows.
45    * @verbatim
46    *         platform root
47    *               |
48    *              ...
49    *               |
50    *         common_ancestor
51    *          /           \
52    *         /             \
53    *        /               \
54    *       /                 \
55    *  src_ancestor     dst_ancestor
56    *      |                   |
57    *     ...                 ...  <-- possibly long pathes
58    *      |                   |
59    *     src                 dst
60    *  @endverbatim
61    */
62   static void find_common_ancestors(NetCard* src, NetCard* dst,
63                                     /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
64   {
65 #define ROUTING_HIERARCHY_MAXDEPTH 32 /* increase if it is not enough */
66     AsImpl* path_src[ROUTING_HIERARCHY_MAXDEPTH];
67     AsImpl* path_dst[ROUTING_HIERARCHY_MAXDEPTH];
68     int index_src = 0;
69     int index_dst = 0;
70     AsImpl* current_src;
71     AsImpl* current_dst;
72     AsImpl* father;
73
74     /* (1) find the path to root of src and dst*/
75     AsImpl* src_as = src->containingAS();
76     AsImpl* dst_as = dst->containingAS();
77
78     xbt_assert(src_as, "Host %s must be in an AS", src->name());
79     xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
80
81     /* (2) find the path to the root routing component */
82     for (AsImpl* current = src_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
83       xbt_assert(index_src < ROUTING_HIERARCHY_MAXDEPTH,
84                  "ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
85       path_src[index_src++] = current;
86     }
87     for (AsImpl* current = dst_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
88       xbt_assert(index_dst < ROUTING_HIERARCHY_MAXDEPTH, "ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
89       path_dst[index_dst++] = current;
90     }
91
92     /* (3) find the common father.
93      * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
94      * So we move them down simultaneously as long as they point to the same content.
95      *
96      * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
97      */
98     do {
99       current_src = path_src[--index_src];
100       current_dst = path_dst[--index_dst];
101     } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
102
103     /* (4) if we did not find a difference (index_src or index_dst went to 0), both elements are in the same AS */
104     if (current_src == current_dst)
105       father = current_src;
106     else // we found a difference
107       father = path_src[index_src + 1];
108
109     /* (5) result generation */
110     *common_ancestor = father;      /* the common father of src and dst */
111     *src_ancestor    = current_src; /* the first different father of src */
112     *dst_ancestor    = current_dst; /* the first different father of dst */
113 #undef ROUTING_HIERARCHY_MAXDEPTH
114     }
115
116     /* PRECONDITION: this is the common ancestor of src and dst */
117     bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
118                                 /* OUT */ std::vector<surf::Link*>* links, double* latency)
119     {
120       // If never set a bypass route return nullptr without any further computations
121       XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
122       if (bypassRoutes_.empty())
123         return false;
124
125       /* Base case, no recursion is needed */
126       if(dst->containingAS() == this && src->containingAS() == this ){
127         if (bypassRoutes_.find({src->name(),dst->name()}) != bypassRoutes_.end()) {
128           std::vector<surf::Link*>* bypassedRoute = bypassRoutes_.at({src->name(), dst->name()});
129           for (surf::Link* link : *bypassedRoute) {
130             links->push_back(link);
131             if (latency)
132               *latency += link->latency();
133           }
134           XBT_DEBUG("Found a bypass route with %zu links",bypassedRoute->size());
135           return true;
136         }
137         return false;
138       }
139
140       /* Engage recursive search */
141
142       std::vector<surf::Link*>* bypassedRoute = nullptr;
143
144       /* (1) find the path to the root routing component */
145       std::vector<As*> path_src;
146       As *current = src->containingAS();
147       while (current != nullptr) {
148         path_src.push_back(current);
149         current = current->father_;
150       }
151
152       std::vector<As*> path_dst;
153       current = dst->containingAS();
154       while (current != nullptr) {
155         path_dst.push_back(current);
156         current = current->father_;
157       }
158
159       /* (2) find the common father */
160       while (path_src.size() > 1 && path_dst.size() >1
161           && path_src.at(path_src.size() -1) == path_dst.at(path_dst.size() -1)) {
162         path_src.pop_back();
163         path_dst.pop_back();
164       }
165
166       int max_index_src = path_src.size() - 1;
167       int max_index_dst = path_dst.size() - 1;
168
169       int max_index = std::max(max_index_src, max_index_dst);
170
171       for (int max = 0; max <= max_index; max++) {
172         for (int i = 0; i < max; i++) {
173           if (i <= max_index_src && max <= max_index_dst) {
174             const std::pair<std::string, std::string> key = {path_src.at(i)->name(), path_dst.at(max)->name()};
175             if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
176               bypassedRoute = bypassRoutes_.at(key);
177               break;
178             }
179           }
180           if (max <= max_index_src && i <= max_index_dst) {
181             const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(i)->name()};
182             if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
183               bypassedRoute = bypassRoutes_.at(key);
184               break;
185             }
186           }
187         }
188
189         if (bypassedRoute)
190           break;
191
192         if (max <= max_index_src && max <= max_index_dst) {
193           const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(max)->name()};
194           if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
195             bypassedRoute = bypassRoutes_.at(key);
196             break;
197           }
198         }
199       }
200
201       if (bypassedRoute) {
202         for (surf::Link* link : *bypassedRoute) {
203           links->push_back(link);
204           if (latency)
205             *latency += link->latency();
206         }
207         return true;
208       }
209       return false;
210     }
211
212     /**
213      * \brief Recursive function for getRouteAndLatency
214      *
215      * \param src the source host
216      * \param dst the destination host
217      * \param links Where to store the links and the gw information
218      * \param latency If not nullptr, the latency of all links will be added in it
219      */
220     void AsImpl::getRouteRecursive(routing::NetCard *src, routing::NetCard *dst,
221         /* OUT */ std::vector<surf::Link*> * links, double *latency)
222     {
223       s_sg_platf_route_cbarg_t route;
224       memset(&route,0,sizeof(route));
225
226       XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name(), dst->name());
227
228       /* Find how src and dst are interconnected */
229       AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
230       find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
231       XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
232           common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
233
234       /* Check whether a direct bypass is defined. If so, use it and bail out */
235       if (common_ancestor->getBypassRoute(src, dst, links, latency))
236         return;
237
238       /* If src and dst are in the same AS, life is good */
239       if (src_ancestor == dst_ancestor) {       /* SURF_ROUTING_BASE */
240         route.link_list = links;
241         common_ancestor->getRouteAndLatency(src, dst, &route, latency);
242         return;
243       }
244
245       /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
246
247       route.link_list = new std::vector<surf::Link*>();
248
249       common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
250       xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr),
251           "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
252
253       /* If source gateway is not our source, we have to recursively find our way up to this point */
254       if (src != route.gw_src)
255         getRouteRecursive(src, route.gw_src, links, latency);
256       for (auto link: *route.link_list)
257         links->push_back(link);
258       delete route.link_list;
259
260       /* If dest gateway is not our destination, we have to recursively find our way from this point */
261       if (route.gw_dst != dst)
262         getRouteRecursive(route.gw_dst, dst, links, latency);
263
264     }
265
266 }}} // namespace