Logo AND Algorithmique Numérique Distribuée

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