Logo AND Algorithmique Numérique Distribuée

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