Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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    * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
45    * @verbatim
46    *         platform root
47    *               |
48    *              ...                <- possibly long path
49    *               |
50    *         common_ancestor
51    *           /        \
52    *          /          \
53    *         /            \          <- direct link
54    *        /              \
55    *       /                \
56    *  src_ancestor     dst_ancestor  <- must be different in the recursive case
57    *      |                   |
58    *     ...                 ...     <-- possibly long (or null) pathes
59    *      |                   |
60    *     src                 dst
61    *  @endverbatim
62    *
63    *  In the base case (when src and dst are in the same AS), things are as follows:
64    *  @verbatim
65    *                  platform root
66    *                        |
67    *                       ...                      <- possibly long path
68    *                        |
69    * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
70    *                   /        \
71    *                  /          \                  <- direct links
72    *                 /            \
73    *              src              dst
74    *  @endverbatim
75    *
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.
80    * @verbatim
81    *            platform root
82    *                  |
83    *                 ...                <-- possibly long path
84    *                  |
85    *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
86    *                  |
87    *                 ...                <-- possibly long (or null) path
88    *                  |
89    *                 dst
90    *  @endverbatim
91    */
92   static void find_common_ancestors(NetCard* src, NetCard* dst,
93                                     /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
94   {
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;
100       return;
101     }
102
103   /* engage the full recursive search */
104
105 #define ROUTING_HIERARCHY_MAXDEPTH 32 /* increase if it is not enough */
106     AsImpl* path_src[ROUTING_HIERARCHY_MAXDEPTH];
107     AsImpl* path_dst[ROUTING_HIERARCHY_MAXDEPTH];
108     int index_src = 0;
109     int index_dst = 0;
110     AsImpl* current_src;
111     AsImpl* current_dst;
112
113     /* (1) find the path to root of src and dst*/
114     AsImpl* src_as = src->containingAS();
115     AsImpl* dst_as = dst->containingAS();
116
117     xbt_assert(src_as, "Host %s must be in an AS", src->name());
118     xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
119
120     /* (2) find the path to the root routing component */
121     for (AsImpl* current = src_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
122       xbt_assert(index_src < ROUTING_HIERARCHY_MAXDEPTH,
123                  "ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
124       path_src[index_src++] = current;
125     }
126     for (AsImpl* current = dst_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
127       xbt_assert(index_dst < ROUTING_HIERARCHY_MAXDEPTH, "ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
128       path_dst[index_dst++] = current;
129     }
130
131     /* (3) find the common father.
132      * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
133      * So we move them down simultaneously as long as they point to the same content.
134      *
135      * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
136      */
137     do {
138       current_src = path_src[--index_src];
139       current_dst = path_dst[--index_dst];
140     } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
141
142     /* (4) we found the difference at least. Finalize the returned values */
143     *src_ancestor    = current_src; /* the first different father of src */
144     *dst_ancestor    = current_dst; /* the first different father of dst */
145     if (current_src == current_dst) { // One is the ancestor of the other
146       *common_ancestor = current_src;
147     } else {
148       *common_ancestor = path_src[index_src + 1];
149     }
150 #undef ROUTING_HIERARCHY_MAXDEPTH
151     }
152
153     /* PRECONDITION: this is the common ancestor of src and dst */
154     bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
155                                 /* OUT */ std::vector<surf::Link*>* links, double* latency)
156     {
157       // If never set a bypass route return nullptr without any further computations
158       XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
159       if (bypassRoutes_.empty())
160         return false;
161
162       /* Base case, no recursion is needed */
163       if(dst->containingAS() == this && src->containingAS() == this ){
164         if (bypassRoutes_.find({src->name(),dst->name()}) != bypassRoutes_.end()) {
165           std::vector<surf::Link*>* bypassedRoute = bypassRoutes_.at({src->name(), dst->name()});
166           for (surf::Link* link : *bypassedRoute) {
167             links->push_back(link);
168             if (latency)
169               *latency += link->latency();
170           }
171           XBT_DEBUG("Found a bypass route with %zu links",bypassedRoute->size());
172           return true;
173         }
174         return false;
175       }
176
177       /* Engage recursive search */
178
179       std::vector<surf::Link*>* bypassedRoute = nullptr;
180
181       /* (1) find the path to the root routing component */
182       std::vector<As*> path_src;
183       As *current = src->containingAS();
184       while (current != nullptr) {
185         path_src.push_back(current);
186         current = current->father_;
187       }
188
189       std::vector<As*> path_dst;
190       current = dst->containingAS();
191       while (current != nullptr) {
192         path_dst.push_back(current);
193         current = current->father_;
194       }
195
196       /* (2) find the common father */
197       while (path_src.size() > 1 && path_dst.size() >1
198           && path_src.at(path_src.size() -1) == path_dst.at(path_dst.size() -1)) {
199         path_src.pop_back();
200         path_dst.pop_back();
201       }
202
203       int max_index_src = path_src.size() - 1;
204       int max_index_dst = path_dst.size() - 1;
205
206       int max_index = std::max(max_index_src, max_index_dst);
207
208       for (int max = 0; max <= max_index; max++) {
209         for (int i = 0; i < max; i++) {
210           if (i <= max_index_src && max <= max_index_dst) {
211             const std::pair<std::string, std::string> key = {path_src.at(i)->name(), path_dst.at(max)->name()};
212             if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
213               bypassedRoute = bypassRoutes_.at(key);
214               break;
215             }
216           }
217           if (max <= max_index_src && i <= max_index_dst) {
218             const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(i)->name()};
219             if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
220               bypassedRoute = bypassRoutes_.at(key);
221               break;
222             }
223           }
224         }
225
226         if (bypassedRoute)
227           break;
228
229         if (max <= max_index_src && max <= max_index_dst) {
230           const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(max)->name()};
231           if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
232             bypassedRoute = bypassRoutes_.at(key);
233             break;
234           }
235         }
236       }
237
238       if (bypassedRoute) {
239         for (surf::Link* link : *bypassedRoute) {
240           links->push_back(link);
241           if (latency)
242             *latency += link->latency();
243         }
244         return true;
245       }
246       return false;
247     }
248
249     /**
250      * \brief Recursive function for getRouteAndLatency
251      *
252      * \param src the source host
253      * \param dst the destination host
254      * \param links Where to store the links and the gw information
255      * \param latency If not nullptr, the latency of all links will be added in it
256      */
257     void AsImpl::getRouteRecursive(routing::NetCard *src, routing::NetCard *dst,
258         /* OUT */ std::vector<surf::Link*> * links, double *latency)
259     {
260       s_sg_platf_route_cbarg_t route;
261       memset(&route,0,sizeof(route));
262
263       XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name(), dst->name());
264
265       /* Find how src and dst are interconnected */
266       AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
267       find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
268       XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
269           common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
270
271       /* Check whether a direct bypass is defined. If so, use it and bail out */
272       if (common_ancestor->getBypassRoute(src, dst, links, latency))
273         return;
274
275       /* If src and dst are in the same AS, life is good */
276       if (src_ancestor == dst_ancestor) {       /* SURF_ROUTING_BASE */
277         route.link_list = links;
278         common_ancestor->getRouteAndLatency(src, dst, &route, latency);
279         return;
280       }
281
282       /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
283
284       route.link_list = new std::vector<surf::Link*>();
285
286       common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
287       xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr),
288           "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
289
290       /* If source gateway is not our source, we have to recursively find our way up to this point */
291       if (src != route.gw_src)
292         getRouteRecursive(src, route.gw_src, links, latency);
293       for (auto link: *route.link_list)
294         links->push_back(link);
295       delete route.link_list;
296
297       /* If dest gateway is not our destination, we have to recursively find our way from this point */
298       if (route.gw_dst != dst)
299         getRouteRecursive(route.gw_dst, dst, links, latency);
300
301     }
302
303 }}} // namespace