Logo AND Algorithmique Numérique Distribuée

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