1 /* Copyright (c) 2006-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
9 #include "simgrid/s4u/as.hpp"
10 #include "src/surf/surf_routing.hpp"
11 #include "src/surf/network_interface.hpp" // Link FIXME: move to proper header
13 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_as,"S4U autonomous systems");
18 As::As(const char *name)
19 : name_(xbt_strdup(name))
28 xbt_dict_cursor_t cursor = NULL;
31 xbt_dict_foreach(children_, cursor, key, elem) {
36 xbt_dict_free(&children_);
37 xbt_dynar_free(&vertices_);
38 xbt_dynar_free(&upDownLinks);
39 for (auto &kv : bypassRoutes_)
45 xbt_dict_t As::children()
57 xbt_dynar_t As::hosts()
59 xbt_dynar_t res = xbt_dynar_new(sizeof(sg_host_t), NULL);
61 for (unsigned int index = 0; index < xbt_dynar_length(vertices_); index++) {
62 simgrid::surf::NetCard *card = xbt_dynar_get_as(vertices_, index, simgrid::surf::NetCard*);
63 simgrid::s4u::Host *host = simgrid::s4u::Host::by_name_or_null(card->name());
65 xbt_dynar_push(res, &host);
70 xbt_dynar_t As::getOneLinkRoutes() {
74 int As::addComponent(surf::NetCard *elm) {
75 xbt_dynar_push_as(vertices_, surf::NetCard*, elm);
76 return xbt_dynar_length(vertices_)-1;
79 void As::addRoute(sg_platf_route_cbarg_t /*route*/){
80 xbt_die("AS %s does not accept new routes (wrong class).",name_);
83 /** @brief Get the common ancestor and its first childs in each line leading to src and dst */
84 static void find_common_ancestors(surf::NetCard *src, surf::NetCard *dst,
85 /* OUT */ As **common_ancestor, As **src_ancestor, As **dst_ancestor)
87 #define ROUTING_HIERARCHY_MAXDEPTH 32 /* increase if it is not enough */
88 simgrid::s4u::As *path_src[ROUTING_HIERARCHY_MAXDEPTH];
89 simgrid::s4u::As *path_dst[ROUTING_HIERARCHY_MAXDEPTH];
92 simgrid::s4u::As *current_src;
93 simgrid::s4u::As *current_dst;
94 simgrid::s4u::As *father;
96 /* (1) find the path to root of src and dst*/
97 simgrid::s4u::As *src_as = src->containingAS();
98 simgrid::s4u::As *dst_as = dst->containingAS();
100 xbt_assert(src_as, "Host %s must be in an AS", src->name());
101 xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
103 /* (2) find the path to the root routing component */
104 for (simgrid::s4u::As *current = src_as; current != NULL; current = current->father()) {
105 xbt_assert(index_src < ROUTING_HIERARCHY_MAXDEPTH, "ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
106 path_src[index_src++] = current;
108 for (simgrid::s4u::As *current = dst_as; current != NULL; current = current->father()) {
109 xbt_assert(index_dst < ROUTING_HIERARCHY_MAXDEPTH,"ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
110 path_dst[index_dst++] = current;
113 /* (3) find the common father.
114 * Before that, index_src and index_dst may be different, they both point to NULL in path_src/path_dst
115 * So we move them down simultaneously as long as they point to the same content.
118 current_src = path_src[--index_src];
119 current_dst = path_dst[--index_dst];
120 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
122 /* (4) if we did not find a difference (index_src or index_dst went to 0), both elements are in the same AS */
123 if (current_src == current_dst)
124 father = current_src;
125 else // we found a difference
126 father = path_src[index_src + 1];
128 /* (5) result generation */
129 *common_ancestor = father; /* the common father of src and dst */
130 *src_ancestor = current_src; /* the first different father of src */
131 *dst_ancestor = current_dst; /* the first different father of dst */
132 #undef ROUTING_HIERARCHY_MAXDEPTH
136 /* PRECONDITION: this is the common ancestor of src and dst */
137 std::vector<surf::Link*> *As::getBypassRoute(surf::NetCard *src, surf::NetCard *dst)
139 // If never set a bypass route return NULL without any further computations
140 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
141 if (bypassRoutes_.empty())
144 std::vector<surf::Link*> *bypassedRoute = nullptr;
146 if(dst->containingAS() == this && src->containingAS() == this ){
147 if (bypassRoutes_.find({src->name(),dst->name()}) != bypassRoutes_.end()) {
148 bypassedRoute = bypassRoutes_.at({src->name(),dst->name()});
149 XBT_DEBUG("Found a bypass route with %zu links",bypassedRoute->size());
151 return bypassedRoute;
154 /* (2) find the path to the root routing component */
155 std::vector<As*> path_src;
156 As *current = src->containingAS();
157 while (current != NULL) {
158 path_src.push_back(current);
159 current = current->father_;
162 std::vector<As*> path_dst;
163 current = dst->containingAS();
164 while (current != NULL) {
165 path_dst.push_back(current);
166 current = current->father_;
169 /* (3) find the common father */
170 while (path_src.size() > 1 && path_dst.size() >1
171 && path_src.at(path_src.size() -1) == path_dst.at(path_dst.size() -1)) {
176 int max_index_src = path_src.size() - 1;
177 int max_index_dst = path_dst.size() - 1;
179 int max_index = std::max(max_index_src, max_index_dst);
181 for (int max = 0; max <= max_index; max++) {
182 for (int i = 0; i < max; i++) {
183 if (i <= max_index_src && max <= max_index_dst) {
184 const std::pair<std::string, std::string> key = {path_src.at(i)->name_, path_dst.at(max)->name_};
185 if (bypassRoutes_.find(key) != bypassRoutes_.end())
186 bypassedRoute = bypassRoutes_.at(key);
190 if (max <= max_index_src && i <= max_index_dst) {
191 const std::pair<std::string, std::string> key = {path_src.at(max)->name_, path_dst.at(i)->name_};
192 if (bypassRoutes_.find(key) != bypassRoutes_.end())
193 bypassedRoute = bypassRoutes_.at(key);
202 if (max <= max_index_src && max <= max_index_dst) {
203 const std::pair<std::string, std::string> key = {path_src.at(max)->name_, path_dst.at(max)->name_};
204 if (bypassRoutes_.find(key) != bypassRoutes_.end())
205 bypassedRoute = bypassRoutes_.at(key);
211 return bypassedRoute;
214 void As::addBypassRoute(sg_platf_route_cbarg_t e_route){
215 const char *src = e_route->src;
216 const char *dst = e_route->dst;
218 /* Argument validity checks */
219 if (e_route->gw_dst) {
220 XBT_DEBUG("Load bypassASroute from %s@%s to %s@%s",
221 src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
222 xbt_assert(!e_route->link_list->empty(), "Bypass route between %s@%s and %s@%s cannot be empty.",
223 src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
224 xbt_assert(bypassRoutes_.find({src,dst}) == bypassRoutes_.end(), "The bypass route between %s@%s and %s@%s already exists.",
225 src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
227 XBT_DEBUG("Load bypassRoute from %s to %s", src, dst);
228 xbt_assert(!e_route->link_list->empty(), "Bypass route between %s and %s cannot be empty.", src, dst);
229 xbt_assert(bypassRoutes_.find({src,dst}) == bypassRoutes_.end(), "The bypass route between %s and %s already exists.", src, dst);
232 /* Build a copy that will be stored in the dict */
233 std::vector<surf::Link*> *newRoute = new std::vector<surf::Link*>();
234 for (auto link: *e_route->link_list)
235 newRoute->push_back(link);
238 bypassRoutes_.insert({{src,dst}, newRoute});
242 * \brief Recursive function for getRouteAndLatency
244 * \param src the source host
245 * \param dst the destination host
246 * \param links Where to store the links and the gw information
247 * \param latency If not NULL, the latency of all links will be added in it
249 void As::getRouteRecursive(surf::NetCard *src, surf::NetCard *dst,
250 /* OUT */ std::vector<surf::Link*> * links, double *latency)
252 s_sg_platf_route_cbarg_t route;
253 memset(&route,0,sizeof(route));
255 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src->name(), dst->name());
257 /* Find how src and dst are interconnected */
258 simgrid::s4u::As *common_ancestor, *src_ancestor, *dst_ancestor;
259 find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
260 XBT_DEBUG("elements_father: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
261 common_ancestor->name_, src_ancestor->name_, dst_ancestor->name_);
263 /* Check whether a direct bypass is defined. If so, use it and bail out */
264 std::vector<surf::Link*> *bypassed_route = common_ancestor->getBypassRoute(src, dst);
265 if (nullptr != bypassed_route) {
266 for (surf::Link *link : *bypassed_route) {
267 links->push_back(link);
269 *latency += link->getLatency();
274 /* If src and dst are in the same AS, life is good */
275 if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
276 route.link_list = links;
277 common_ancestor->getRouteAndLatency(src, dst, &route, latency);
281 /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
283 route.link_list = new std::vector<surf::Link*>();
285 common_ancestor->getRouteAndLatency(src_ancestor->netcard_, dst_ancestor->netcard_, &route, latency);
286 xbt_assert((route.gw_src != NULL) && (route.gw_dst != NULL),
287 "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
289 /* If source gateway is not our source, we have to recursively find our way up to this point */
290 if (src != route.gw_src)
291 getRouteRecursive(src, route.gw_src, links, latency);
292 for (auto link: *route.link_list)
293 links->push_back(link);
295 /* If dest gateway is not our destination, we have to recursively find our way from this point */
296 if (route.gw_dst != dst)
297 getRouteRecursive(route.gw_dst, dst, links, latency);