Logo AND Algorithmique Numérique Distribuée

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