Logo AND Algorithmique Numérique Distribuée

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