Logo AND Algorithmique Numérique Distribuée

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