Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c5fef783271996f24bb5e3de9a75fecf59c0eee6
[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   static void find_common_ancestors(NetCard* src, NetCard* dst,
44                                     /* OUT */ AsImpl** common_ancestor, AsImpl** src_ancestor, AsImpl** dst_ancestor)
45   {
46 #define ROUTING_HIERARCHY_MAXDEPTH 32 /* increase if it is not enough */
47     AsImpl* path_src[ROUTING_HIERARCHY_MAXDEPTH];
48     AsImpl* path_dst[ROUTING_HIERARCHY_MAXDEPTH];
49     int index_src = 0;
50     int index_dst = 0;
51     AsImpl* current_src;
52     AsImpl* current_dst;
53     AsImpl* father;
54
55     /* (1) find the path to root of src and dst*/
56     AsImpl* src_as = src->containingAS();
57     AsImpl* dst_as = dst->containingAS();
58
59     xbt_assert(src_as, "Host %s must be in an AS", src->name());
60     xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
61
62     /* (2) find the path to the root routing component */
63     for (AsImpl* current = src_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
64       xbt_assert(index_src < ROUTING_HIERARCHY_MAXDEPTH,
65                  "ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
66       path_src[index_src++] = current;
67     }
68     for (AsImpl* current = dst_as; current != nullptr; current = static_cast<AsImpl*>(current->father())) {
69       xbt_assert(index_dst < ROUTING_HIERARCHY_MAXDEPTH, "ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
70       path_dst[index_dst++] = current;
71     }
72
73     /* (3) find the common father.
74      * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
75      * So we move them down simultaneously as long as they point to the same content.
76      */
77     do {
78       current_src = path_src[--index_src];
79       current_dst = path_dst[--index_dst];
80     } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
81
82     /* (4) if we did not find a difference (index_src or index_dst went to 0), both elements are in the same AS */
83     if (current_src == current_dst)
84       father = current_src;
85     else // we found a difference
86       father = path_src[index_src + 1];
87
88     /* (5) result generation */
89     *common_ancestor = father;      /* the common father of src and dst */
90     *src_ancestor    = current_src; /* the first different father of src */
91     *dst_ancestor    = current_dst; /* the first different father of dst */
92 #undef ROUTING_HIERARCHY_MAXDEPTH
93     }
94
95     /* PRECONDITION: this is the common ancestor of src and dst */
96     bool AsImpl::getBypassRoute(routing::NetCard* src, routing::NetCard* dst,
97                                 /* OUT */ std::vector<surf::Link*>* links, double* latency)
98     {
99       // If never set a bypass route return nullptr without any further computations
100       XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
101       if (bypassRoutes_.empty())
102         return false;
103
104       /* Base case, no recursion is needed */
105       if(dst->containingAS() == this && src->containingAS() == this ){
106         if (bypassRoutes_.find({src->name(),dst->name()}) != bypassRoutes_.end()) {
107           std::vector<surf::Link*>* bypassedRoute = bypassRoutes_.at({src->name(), dst->name()});
108           for (surf::Link* link : *bypassedRoute) {
109             links->push_back(link);
110             if (latency)
111               *latency += link->latency();
112           }
113           XBT_DEBUG("Found a bypass route with %zu links",bypassedRoute->size());
114           return true;
115         }
116         return false;
117       }
118
119       /* Engage recursive search */
120
121       std::vector<surf::Link*>* bypassedRoute = nullptr;
122
123       /* (1) find the path to the root routing component */
124       std::vector<As*> path_src;
125       As *current = src->containingAS();
126       while (current != nullptr) {
127         path_src.push_back(current);
128         current = current->father_;
129       }
130
131       std::vector<As*> path_dst;
132       current = dst->containingAS();
133       while (current != nullptr) {
134         path_dst.push_back(current);
135         current = current->father_;
136       }
137
138       /* (2) find the common father */
139       while (path_src.size() > 1 && path_dst.size() >1
140           && path_src.at(path_src.size() -1) == path_dst.at(path_dst.size() -1)) {
141         path_src.pop_back();
142         path_dst.pop_back();
143       }
144
145       int max_index_src = path_src.size() - 1;
146       int max_index_dst = path_dst.size() - 1;
147
148       int max_index = std::max(max_index_src, max_index_dst);
149
150       for (int max = 0; max <= max_index; max++) {
151         for (int i = 0; i < max; i++) {
152           if (i <= max_index_src && max <= max_index_dst) {
153             const std::pair<std::string, std::string> key = {path_src.at(i)->name(), path_dst.at(max)->name()};
154             if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
155               bypassedRoute = bypassRoutes_.at(key);
156               break;
157             }
158           }
159           if (max <= max_index_src && i <= max_index_dst) {
160             const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(i)->name()};
161             if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
162               bypassedRoute = bypassRoutes_.at(key);
163               break;
164             }
165           }
166         }
167
168         if (bypassedRoute)
169           break;
170
171         if (max <= max_index_src && max <= max_index_dst) {
172           const std::pair<std::string, std::string> key = {path_src.at(max)->name(), path_dst.at(max)->name()};
173           if (bypassRoutes_.find(key) != bypassRoutes_.end()) {
174             bypassedRoute = bypassRoutes_.at(key);
175             break;
176           }
177         }
178       }
179
180       if (bypassedRoute) {
181         for (surf::Link* link : *bypassedRoute) {
182           links->push_back(link);
183           if (latency)
184             *latency += link->latency();
185         }
186         return true;
187       }
188       return false;
189     }
190
191     /**
192      * \brief Recursive function for getRouteAndLatency
193      *
194      * \param src the source host
195      * \param dst the destination host
196      * \param links Where to store the links and the gw information
197      * \param latency If not nullptr, the latency of all links will be added in it
198      */
199     void AsImpl::getRouteRecursive(routing::NetCard *src, routing::NetCard *dst,
200         /* OUT */ std::vector<surf::Link*> * links, double *latency)
201     {
202       s_sg_platf_route_cbarg_t route;
203       memset(&route,0,sizeof(route));
204
205       XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name(), dst->name());
206
207       /* Find how src and dst are interconnected */
208       AsImpl *common_ancestor, *src_ancestor, *dst_ancestor;
209       find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
210       XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
211           common_ancestor->name(), src_ancestor->name(), dst_ancestor->name());
212
213       /* Check whether a direct bypass is defined. If so, use it and bail out */
214       if (common_ancestor->getBypassRoute(src, dst, links, latency))
215         return;
216
217       /* If src and dst are in the same AS, life is good */
218       if (src_ancestor == dst_ancestor) {       /* SURF_ROUTING_BASE */
219         route.link_list = links;
220         common_ancestor->getRouteAndLatency(src, dst, &route, latency);
221         return;
222       }
223
224       /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
225
226       route.link_list = new std::vector<surf::Link*>();
227
228       common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
229       xbt_assert((route.gw_src != nullptr) && (route.gw_dst != nullptr),
230           "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
231
232       /* If source gateway is not our source, we have to recursively find our way up to this point */
233       if (src != route.gw_src)
234         getRouteRecursive(src, route.gw_src, links, latency);
235       for (auto link: *route.link_list)
236         links->push_back(link);
237       delete route.link_list;
238
239       /* If dest gateway is not our destination, we have to recursively find our way from this point */
240       if (route.gw_dst != dst)
241         getRouteRecursive(route.gw_dst, dst, links, latency);
242
243     }
244
245 }}} // namespace