Logo AND Algorithmique Numérique Distribuée

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