Logo AND Algorithmique Numérique Distribuée

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