Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4ff5801d07af067d0ff69b6531d58cfa9b292c7c
[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/kernel/routing/NetCard.hpp"
11 #include "src/surf/cpu_interface.hpp"
12 #include "src/surf/network_interface.hpp"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_route);
15
16 namespace simgrid {
17   namespace kernel {
18   namespace routing {
19
20   AsImpl::AsImpl(As* father, const char* name) : As(father, name)
21   {
22     xbt_assert(nullptr == xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL),
23                "Refusing to create a second AS called '%s'.", name);
24
25     netcard_ = new NetCard(name, NetCard::Type::As, static_cast<AsImpl*>(father));
26     xbt_lib_set(as_router_lib, name, ROUTING_ASR_LEVEL, static_cast<void*>(netcard_));
27     XBT_DEBUG("AS '%s' created with the id '%d'", name, netcard_->id());
28   }
29
30   simgrid::s4u::Host* AsImpl::createHost(const char* name, std::vector<double>* speedPerPstate, int coreAmount)
31   {
32     simgrid::s4u::Host* res = new simgrid::s4u::Host(name);
33
34     if (hierarchy_ == RoutingMode::unset)
35       hierarchy_ = RoutingMode::base;
36
37     res->pimpl_netcard = new NetCard(name, NetCard::Type::Host, this);
38
39     surf_cpu_model_pm->createCpu(res, speedPerPstate, coreAmount);
40
41     return res;
42   }
43
44   /** @brief Get the common ancestor and its first children in each line leading to src and dst
45    *
46    * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
47    * @verbatim
48    *         platform root
49    *               |
50    *              ...                <- possibly long path
51    *               |
52    *         common_ancestor
53    *           /        \
54    *          /          \
55    *         /            \          <- direct links
56    *        /              \
57    *       /                \
58    *  src_ancestor     dst_ancestor  <- must be different in the recursive case
59    *      |                   |
60    *     ...                 ...     <-- possibly long pathes (one hop or more)
61    *      |                   |
62    *     src                 dst
63    *  @endverbatim
64    *
65    *  In the base case (when src and dst are in the same AS), things are as follows:
66    *  @verbatim
67    *                  platform root
68    *                        |
69    *                       ...                      <-- possibly long path
70    *                        |
71    * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
72    *                   /        \
73    *                  /          \                  <-- direct links (exactly one hop)
74    *                 /            \
75    *              src              dst
76    *  @endverbatim
77    *
78    * A specific recursive case occurs when src is the ancestor of dst. In this case,
79    * the base case routing should be used so the common_ancestor is specifically set
80    * to src_ancestor==dst_ancestor.
81    * Naturally, things are completely symmetrical if dst is the ancestor of src.
82    * @verbatim
83    *            platform root
84    *                  |
85    *                 ...                <-- possibly long path
86    *                  |
87    *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
88    *                  |
89    *                 ...                <-- possibly long path (one hop or more)
90    *                  |
91    *                 dst
92    *  @endverbatim
93    */
94   static void find_common_ancestors(NetCard* src, NetCard* dst,
95                                     /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
96   {
97     /* Deal with the easy base case */
98     if (src->containingAS() == dst->containingAS()) {
99       *common_ancestor = src->containingAS();
100       *src_ancestor    = *common_ancestor;
101       *dst_ancestor    = *common_ancestor;
102       return;
103     }
104
105     /* engage the full recursive search */
106
107     /* (1) find the path to root of src and dst*/
108     AsImpl* src_as = src->containingAS();
109     AsImpl* dst_as = dst->containingAS();
110
111     xbt_assert(src_as, "Host %s must be in an AS", src->cname());
112     xbt_assert(dst_as, "Host %s must be in an AS", dst->cname());
113
114     /* (2) find the path to the root routing component */
115     std::vector<AsImpl*> path_src;
116     AsImpl* current = src->containingAS();
117     while (current != nullptr) {
118       path_src.push_back(current);
119       current = static_cast<AsImpl*>(current->father());
120     }
121     std::vector<AsImpl*> path_dst;
122     current = dst->containingAS();
123     while (current != nullptr) {
124       path_dst.push_back(current);
125       current = static_cast<AsImpl*>(current->father());
126     }
127
128     /* (3) find the common father.
129      * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
130      * So we move them down simultaneously as long as they point to the same content.
131      *
132      * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
133      */
134     AsImpl* father = nullptr; // the AS we dropped on the previous loop iteration
135     while (path_src.size() > 1 && path_dst.size() > 1 &&
136            path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
137       father = path_src.at(path_src.size() - 1);
138       path_src.pop_back();
139       path_dst.pop_back();
140     }
141
142     /* (4) we found the difference at least. Finalize the returned values */
143     *src_ancestor = path_src.at(path_src.size() - 1); /* the first different father of src */
144     *dst_ancestor = path_dst.at(path_dst.size() - 1); /* the first different father of dst */
145     if (*src_ancestor == *dst_ancestor) {             // src is the ancestor of dst, or the contrary
146       *common_ancestor = *src_ancestor;
147     } else {
148       *common_ancestor = father;
149     }
150   }
151
152   /* PRECONDITION: this is the common ancestor of src and dst */
153   bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
154                               /* OUT */ std::vector<surf::Link*>* links, double* latency)
155   {
156     // If never set a bypass route return nullptr without any further computations
157     if (bypassRoutes_.empty())
158       return false;
159
160     /* Base case, no recursion is needed */
161     if (dst->containingAS() == this && src->containingAS() == this) {
162       if (bypassRoutes_.find({src, dst}) != bypassRoutes_.end()) {
163         AsRoute* bypassedRoute = bypassRoutes_.at({src, dst});
164         for (surf::Link* link : bypassedRoute->links) {
165           links->push_back(link);
166           if (latency)
167             *latency += link->latency();
168         }
169         XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->cname(), dst->cname(),
170                   bypassedRoute->links.size());
171         return true;
172       }
173       return false;
174     }
175
176     /* Engage recursive search */
177
178
179     /* (1) find the path to the root routing component */
180     std::vector<AsImpl*> path_src;
181     As* current = src->containingAS();
182     while (current != nullptr) {
183       path_src.push_back(static_cast<AsImpl*>(current));
184       current = current->father_;
185     }
186
187     std::vector<AsImpl*> path_dst;
188     current = dst->containingAS();
189     while (current != nullptr) {
190       path_dst.push_back(static_cast<AsImpl*>(current));
191       current = current->father_;
192     }
193
194     /* (2) find the common father */
195     while (path_src.size() > 1 && path_dst.size() > 1 &&
196            path_src.at(path_src.size() - 1) == path_dst.at(path_dst.size() - 1)) {
197       path_src.pop_back();
198       path_dst.pop_back();
199     }
200
201     int max_index_src = path_src.size() - 1;
202     int max_index_dst = path_dst.size() - 1;
203
204     int max_index = std::max(max_index_src, max_index_dst);
205
206     /* (3) Search for a bypass making the path up to the ancestor useless */
207     AsRoute* bypassedRoute = nullptr;
208     std::pair<kernel::routing::NetCard*, kernel::routing::NetCard*> key;
209     for (int max = 0; max <= max_index; max++) {
210       for (int i = 0; i < max; i++) {
211         if (i <= max_index_src && max <= max_index_dst) {
212           key = {path_src.at(i)->netcard_, path_dst.at(max)->netcard_};
213           if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
214             bypassedRoute = bypassRoutes_.at(key);
215             break;
216           }
217         }
218         if (max <= max_index_src && i <= max_index_dst) {
219           key = {path_src.at(max)->netcard_, path_dst.at(i)->netcard_};
220           if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
221             bypassedRoute = bypassRoutes_.at(key);
222             break;
223           }
224         }
225       }
226
227       if (bypassedRoute)
228         break;
229
230       if (max <= max_index_src && max <= max_index_dst) {
231         key = {path_src.at(max)->netcard_, path_dst.at(max)->netcard_};
232         if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
233           bypassedRoute = bypassRoutes_.at(key);
234           break;
235         }
236       }
237     }
238
239     /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
240     if (bypassedRoute) {
241       XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
242                 "calls to getRoute",
243                 src->cname(), dst->cname(), bypassedRoute->links.size());
244       if (src != key.first)
245         getGlobalRoute(src, const_cast<NetCard*>(bypassedRoute->gw_src), links, latency);
246       for (surf::Link* link : bypassedRoute->links) {
247         links->push_back(link);
248         if (latency)
249           *latency += link->latency();
250       }
251       if (dst != key.second)
252         getGlobalRoute(const_cast<NetCard*>(bypassedRoute->gw_dst), dst, links, latency);
253       return true;
254     }
255     XBT_DEBUG("No bypass route from '%s' to '%s'.", src->cname(), dst->cname());
256     return false;
257     }
258
259     void AsImpl::getGlobalRoute(routing::NetCard* src, routing::NetCard* dst,
260                                 /* OUT */ std::vector<surf::Link*>* links, double* latency)
261     {
262       s_sg_platf_route_cbarg_t route;
263       memset(&route,0,sizeof(route));
264
265       XBT_DEBUG("Resolve route from '%s' to '%s'", src->cname(), dst->cname());
266
267       /* Find how src and dst are interconnected */
268       AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
269       find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
270       XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
271           common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
272
273       /* Check whether a direct bypass is defined. If so, use it and bail out */
274       if (common_ancestor->getBypassRoute(src, dst, links, latency))
275         return;
276
277       /* If src and dst are in the same AS, life is good */
278       if (src_ancestor == dst_ancestor) {       /* SURF_ROUTING_BASE */
279         route.link_list = links;
280         common_ancestor->getLocalRoute(src, dst, &route, latency);
281         return;
282       }
283
284       /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
285
286       route.link_list = new std::vector<surf::Link*>();
287
288       common_ancestor->getLocalRoute(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
289       xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr), "bad gateways for route from \"%s\" to \"%s\"",
290                  src->cname(), dst->cname());
291
292       /* If source gateway is not our source, we have to recursively find our way up to this point */
293       if (src != route.gw_src)
294         getGlobalRoute(src, route.gw_src, links, latency);
295       for (auto link: *route.link_list)
296         links->push_back(link);
297       delete route.link_list;
298
299       /* If dest gateway is not our destination, we have to recursively find our way from this point */
300       if (route.gw_dst != dst)
301         getGlobalRoute(route.gw_dst, dst, links, latency);
302     }
303
304 }}} // namespace