Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in bypass routing
[simgrid.git] / src / kernel / routing / AsRoutedGraph.cpp
1 /* Copyright (c) 2009-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/dict.h"
7 #include "xbt/log.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/dynar.h"
10 #include "xbt/graph.h"
11
12 #include "src/kernel/routing/AsRoutedGraph.hpp"
13 #include "src/surf/network_interface.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
16
17 void routing_route_free(sg_platf_route_cbarg_t route)
18 {
19   if (route) {
20     delete route->link_list;
21     xbt_free(route);
22   }
23 }
24
25 namespace simgrid {
26 namespace kernel {
27 namespace routing {
28
29 AsRoutedGraph::AsRoutedGraph(As* father, const char* name) : AsImpl(father, name)
30 {
31 }
32
33 AsRoutedGraph::~AsRoutedGraph()=default;
34
35 }}} // namespace simgrid::kernel::routing
36
37 /* ************************************************************************** */
38 /* *********************** GENERIC BUSINESS METHODS ************************* */
39
40 static const char *instr_node_name(xbt_node_t node)
41 {
42   void *data = xbt_graph_node_get_data(node);
43   char *str = (char *) data;
44   return str;
45 }
46
47 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name, xbt_dict_t nodes)
48 {
49   xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
50   if (ret)
51     return ret;
52
53   ret = xbt_graph_new_node(graph, xbt_strdup(name));
54   xbt_dict_set(nodes, name, ret, nullptr);
55   return ret;
56 }
57
58 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
59 {
60   const char *sn = instr_node_name(s);
61   const char *dn = instr_node_name(d);
62   int len = strlen(sn) + strlen(dn) + 1;
63   char *name = (char *) xbt_malloc(len * sizeof(char));
64
65   snprintf(name, len, "%s%s", sn, dn);
66   xbt_edge_t ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
67   if (ret == nullptr) {
68     snprintf(name, len, "%s%s", dn, sn);
69     ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
70   }
71
72   if (ret == nullptr) {
73     ret = xbt_graph_new_edge(graph, s, d, nullptr);
74     xbt_dict_set(edges, name, ret, nullptr);
75   }
76   free(name);
77   return ret;
78 }
79
80 namespace simgrid {
81 namespace kernel {
82 namespace routing {
83
84   xbt_dynar_t AsRoutedGraph::getOneLinkRoutes()
85   {
86     xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
87     sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t,1);
88     route->link_list = new std::vector<Link*>();
89
90     int table_size = static_cast<int>(vertices_.size());
91     for(int src=0; src < table_size; src++) {
92       for(int dst=0; dst< table_size; dst++) {
93         route->link_list->clear();
94         NetCard *src_elm = vertices_.at(src);
95         NetCard *dst_elm = vertices_.at(dst);
96         this->getRouteAndLatency(src_elm, dst_elm,route, nullptr);
97
98         if (route->link_list->size() == 1) {
99           Link *link = route->link_list->at(0);
100           Onelink *onelink;
101           if (hierarchy_ == RoutingMode::base)
102             onelink = new Onelink(link, src_elm, dst_elm);
103           else if (hierarchy_ == RoutingMode::recursive)
104             onelink = new Onelink(link, route->gw_src, route->gw_dst);
105           else
106             onelink = new Onelink(link, nullptr, nullptr);
107           xbt_dynar_push(ret, &onelink);
108         }
109       }
110     }
111     return ret;
112   }
113
114 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
115 {
116   for (auto my_src: vertices_){
117     for (auto my_dst: vertices_){
118       if (my_src == my_dst)
119         continue;
120
121       sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
122       route->link_list = new std::vector<Link*>();
123
124       getRouteAndLatency(my_src, my_dst, route, nullptr);
125
126       XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
127
128       xbt_node_t current, previous;
129       const char *previous_name, *current_name;
130
131       if (route->gw_src) {
132         previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
133         previous_name = route->gw_src->name();
134       } else {
135         previous = new_xbt_graph_node(graph, my_src->name(), nodes);
136         previous_name = my_src->name();
137       }
138
139       for (auto link: *route->link_list) {
140         const char *link_name = link->getName();
141         current = new_xbt_graph_node(graph, link_name, nodes);
142         current_name = link_name;
143         new_xbt_graph_edge(graph, previous, current, edges);
144         XBT_DEBUG ("  %s -> %s", previous_name, current_name);
145         previous = current;
146         previous_name = current_name;
147       }
148
149       if (route->gw_dst) {
150         current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
151         current_name = route->gw_dst->name();
152       } else {
153         current = new_xbt_graph_node(graph, my_dst->name(), nodes);
154         current_name = my_dst->name();
155       }
156       new_xbt_graph_edge(graph, previous, current, edges);
157       XBT_DEBUG ("  %s -> %s", previous_name, current_name);
158
159       delete route->link_list;
160       xbt_free (route);
161     }
162   }
163 }
164
165 /* ************************************************************************** */
166 /* ************************* GENERIC AUX FUNCTIONS ************************** */
167 /* change a route containing link names into a route containing link entities */
168 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(RoutingMode hierarchy, sg_platf_route_cbarg_t routearg, int change_order)
169 {
170   sg_platf_route_cbarg_t result;
171
172   result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
173   result->link_list = new std::vector<Link*>();
174
175   xbt_assert(hierarchy == RoutingMode::base || hierarchy == RoutingMode::recursive,
176       "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
177
178   if (hierarchy == RoutingMode::recursive) {
179     xbt_assert(routearg->gw_src && routearg->gw_dst, "nullptr is obviously a deficient gateway");
180
181     result->gw_src = routearg->gw_src;
182     result->gw_dst = routearg->gw_dst;
183   }
184
185   for (auto link : *routearg->link_list) {
186     if (change_order)
187       result->link_list->push_back(link);
188     else
189       result->link_list->insert(result->link_list->begin(), link);
190   }
191
192   return result;
193 }
194
195 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
196 {
197   xbt_assert(src,"Cannot find a route from nullptr to %s", dst->name());
198   xbt_assert(dst,"Cannot find a route from %s to nullptr", src->name());
199
200   As *src_as = src->containingAS();
201   As *dst_as = dst->containingAS();
202
203   xbt_assert(src_as == dst_as, "Internal error: %s@%s and %s@%s are not in the same AS as expected. Please report that bug.",
204         src->name(), src_as->name(), dst->name(), dst_as->name());
205
206   xbt_assert(this == dst_as,
207       "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
208         src->name(), dst->name(),  src_as->name(), dst_as->name(),  name());
209 }
210 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
211   NetCard *src = route->src;
212   NetCard *dst = route->dst;
213   const char *srcName = src->name();
214   const char *dstName = dst->name();
215
216   if(!route->gw_dst && !route->gw_src) {
217     XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
218     xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
219     xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
220     xbt_assert(! route->link_list->empty(), "Empty route (between %s and %s) forbidden.", srcName, dstName);
221     xbt_assert(! src->isAS(), "When defining a route, src cannot be an AS such as '%s'. Did you meant to have an ASroute?", srcName);
222     xbt_assert(! dst->isAS(), "When defining a route, dst cannot be an AS such as '%s'. Did you meant to have an ASroute?", dstName);
223   } else {
224     XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
225     xbt_assert(src->isAS(), "When defining an ASroute, src must be an AS but '%s' is not", srcName);
226     xbt_assert(dst->isAS(), "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
227
228     xbt_assert(route->gw_src->isHost() || route->gw_src->isRouter(),
229         "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
230     xbt_assert(route->gw_dst->isHost() || route->gw_dst->isRouter(),
231         "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
232
233     xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
234
235     xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
236         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
237     xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
238         srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
239     xbt_assert(! route->link_list->empty(), "Empty route (between %s@%s and %s@%s) forbidden.",
240         srcName,route->gw_src->name(), dstName,route->gw_dst->name());
241   }
242 }
243
244 }}}