Logo AND Algorithmique Numérique Distribuée

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