Logo AND Algorithmique Numérique Distribuée

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