Logo AND Algorithmique Numérique Distribuée

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