1 /* Copyright (c) 2009-2015. 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. */
13 #include <xbt/sysdep.h>
14 #include <xbt/dynar.h>
15 #include <xbt/graph.h>
17 #include "simgrid/platf_interface.h" // platform creation API internal interface
19 #include "surf_routing_generic.hpp"
20 #include "network_interface.hpp"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
24 static int no_bypassroute_declared = 1;
26 void generic_free_route(sg_platf_route_cbarg_t route)
29 xbt_dynar_free(&route->link_list);
34 void AsGeneric::parseRoute(sg_platf_route_cbarg_t /*route*/){
38 void AsGeneric::parseASroute(sg_platf_route_cbarg_t /*route*/){
42 void AsGeneric::getRouteAndLatency(RoutingEdge */*src*/, RoutingEdge */*dst*/, sg_platf_route_cbarg_t /*into*/, double */*latency*/){
46 AsGeneric::AsGeneric() {
47 p_bypassRoutes = xbt_dict_new_homogeneous((void (*)(void *)) generic_free_route);
50 AsGeneric::~AsGeneric() {
51 xbt_dict_free(&p_bypassRoutes);
54 int AsGeneric::parsePU(RoutingEdge *elm)
56 XBT_DEBUG("Load process unit \"%s\"", elm->getName());
57 xbt_dynar_push_as(p_indexNetworkElm, RoutingEdge*, elm);
58 return xbt_dynar_length(p_indexNetworkElm)-1;
61 int AsGeneric::parseAS(RoutingEdge *elm)
63 XBT_DEBUG("Load Autonomous system \"%s\"", elm->getName());
64 xbt_dynar_push_as(p_indexNetworkElm, RoutingEdge*, elm);
65 return xbt_dynar_length(p_indexNetworkElm)-1;
68 void AsGeneric::parseBypassroute(sg_platf_route_cbarg_t e_route)
70 char *src = (char*)(e_route->src);
71 char *dst = (char*)(e_route->dst);
74 XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
76 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
77 xbt_dict_t dict_bypassRoutes = p_bypassRoutes;
80 route_name = bprintf("%s#%s", src, dst);
81 xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
82 "Invalid count of links, must be greater than zero (%s,%s)",
84 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
85 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
86 src, e_route->gw_src->getName(), dst, e_route->gw_dst->getName());
88 sg_platf_route_cbarg_t new_e_route = NULL;
90 new_e_route = newExtendedRoute(SURF_ROUTING_RECURSIVE, e_route, 1);
92 new_e_route = newExtendedRoute(SURF_ROUTING_BASE, e_route, 1);
94 xbt_dynar_free(&(e_route->link_list));
96 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
97 no_bypassroute_declared = 0;
101 /* ************************************************************************** */
102 /* *********************** GENERIC BUSINESS METHODS ************************* */
104 xbt_dynar_t AsGeneric::getOneLinkRoutes() { // FIXME: kill that stub
105 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
109 static const char *instr_node_name(xbt_node_t node)
111 void *data = xbt_graph_node_get_data(node);
112 char *str = (char *) data;
116 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
119 xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
123 ret = xbt_graph_new_node(graph, xbt_strdup(name));
124 xbt_dict_set(nodes, name, ret, NULL);
128 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
133 const char *sn = instr_node_name(s);
134 const char *dn = instr_node_name(d);
135 int len = strlen(sn) + strlen(dn) + 1;
136 char *name = (char *) xbt_malloc(len * sizeof(char));
139 snprintf(name, len, "%s%s", sn, dn);
140 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
142 snprintf(name, len, "%s%s", dn, sn);
143 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
147 ret = xbt_graph_new_edge(graph, s, d, NULL);
148 xbt_dict_set(edges, name, ret, NULL);
154 void AsGeneric::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
157 int table_size = xbt_dynar_length(p_indexNetworkElm);
160 for (src = 0; src < table_size; src++) {
161 RoutingEdge *my_src =
162 xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdge*);
163 for (dst = 0; dst < table_size; dst++) {
166 RoutingEdge *my_dst =
167 xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdge*);
169 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
170 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
172 getRouteAndLatency(my_src, my_dst, route, NULL);
174 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->getName(), my_dst->getName());
179 xbt_node_t current, previous;
180 const char *previous_name, *current_name;
183 previous = new_xbt_graph_node(graph, route->gw_src->getName(), nodes);
184 previous_name = route->gw_src->getName();
186 previous = new_xbt_graph_node(graph, my_src->getName(), nodes);
187 previous_name = my_src->getName();
190 xbt_dynar_foreach(route->link_list, cpt, link) {
191 const char *link_name = static_cast<Resource*>(link)->getName();
192 current = new_xbt_graph_node(graph, link_name, nodes);
193 current_name = link_name;
194 new_xbt_graph_edge(graph, previous, current, edges);
195 XBT_DEBUG (" %s -> %s", previous_name, current_name);
197 previous_name = current_name;
201 current = new_xbt_graph_node(graph, route->gw_dst->getName(), nodes);
202 current_name = route->gw_dst->getName();
204 current = new_xbt_graph_node(graph, my_dst->getName(), nodes);
205 current_name = my_dst->getName();
207 new_xbt_graph_edge(graph, previous, current, edges);
208 XBT_DEBUG (" %s -> %s", previous_name, current_name);
210 xbt_dynar_free (&(route->link_list));
216 sg_platf_route_cbarg_t AsGeneric::getBypassRoute(RoutingEdge *src,
220 // If never set a bypass route return NULL without any further computations
221 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->getName(), dst->getName());
222 if (no_bypassroute_declared)
225 sg_platf_route_cbarg_t e_route_bypass = NULL;
226 xbt_dict_t dict_bypassRoutes = p_bypassRoutes;
228 if(dst->getRcComponent() == this && src->getRcComponent() == this ){
229 char *route_name = bprintf("%s#%s", src->getName(), dst->getName());
230 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
232 XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
237 int index_src, index_dst;
238 xbt_dynar_t path_src = NULL;
239 xbt_dynar_t path_dst = NULL;
241 As **current_src = NULL;
242 As **current_dst = NULL;
244 if (src == NULL || dst == NULL)
245 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
246 src ? src->getName() : "(null)",
247 dst ? dst->getName() : "(null)", p_name);
249 src_as = src->getRcComponent();
250 dst_as = dst->getRcComponent();
252 /* (2) find the path to the root routing component */
253 path_src = xbt_dynar_new(sizeof(As*), NULL);
255 while (current != NULL) {
256 xbt_dynar_push(path_src, ¤t);
257 current = current->p_routingFather;
259 path_dst = xbt_dynar_new(sizeof(As*), NULL);
261 while (current != NULL) {
262 xbt_dynar_push(path_dst, ¤t);
263 current = current->p_routingFather;
266 /* (3) find the common father */
267 index_src = path_src->used - 1;
268 index_dst = path_dst->used - 1;
269 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
270 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
271 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
272 xbt_dynar_pop_ptr(path_src);
273 xbt_dynar_pop_ptr(path_dst);
276 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
277 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
280 int max_index_src = path_src->used - 1;
281 int max_index_dst = path_dst->used - 1;
283 int max_index = std::max(max_index_src, max_index_dst);
286 for (max = 0; max <= max_index; max++) {
287 for (i = 0; i < max; i++) {
288 if (i <= max_index_src && max <= max_index_dst) {
289 char *route_name = bprintf("%s#%s",
291 (xbt_dynar_get_ptr(path_src, i)))->p_name,
293 (xbt_dynar_get_ptr(path_dst, max)))->p_name);
294 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
295 xbt_free(route_name);
299 if (max <= max_index_src && i <= max_index_dst) {
300 char *route_name = bprintf("%s#%s",
302 (xbt_dynar_get_ptr(path_src, max)))->p_name,
304 (xbt_dynar_get_ptr(path_dst, i)))->p_name);
305 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
306 xbt_free(route_name);
315 if (max <= max_index_src && max <= max_index_dst) {
316 char *route_name = bprintf("%s#%s",
318 (xbt_dynar_get_ptr(path_src, max)))->p_name,
320 (xbt_dynar_get_ptr(path_dst, max)))->p_name);
321 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
322 xbt_free(route_name);
328 xbt_dynar_free(&path_src);
329 xbt_dynar_free(&path_dst);
332 sg_platf_route_cbarg_t new_e_route = NULL;
333 if (e_route_bypass) {
335 unsigned int cpt = 0;
336 new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
337 new_e_route->gw_src = e_route_bypass->gw_src;
338 new_e_route->gw_dst = e_route_bypass->gw_dst;
339 new_e_route->link_list =
340 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
341 xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
342 xbt_dynar_push(new_e_route->link_list, &link);
344 *lat += link->getLatency();
351 /* ************************************************************************** */
352 /* ************************* GENERIC AUX FUNCTIONS ************************** */
353 /* change a route containing link names into a route containing link entities */
354 sg_platf_route_cbarg_t AsGeneric::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
355 sg_platf_route_cbarg_t routearg, int change_order) {
357 sg_platf_route_cbarg_t result;
361 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
362 result->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
364 xbt_assert(hierarchy == SURF_ROUTING_BASE
365 || hierarchy == SURF_ROUTING_RECURSIVE,
366 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
368 if (hierarchy == SURF_ROUTING_RECURSIVE) {
370 xbt_assert(routearg->gw_src && routearg->gw_dst,
371 "NULL is obviously a bad gateway");
373 /* remember not erase the gateway names */
374 result->gw_src = routearg->gw_src;
375 result->gw_dst = routearg->gw_dst;
378 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
380 Link *link = Link::byName(link_name);
383 xbt_dynar_push(result->link_list, &link);
385 xbt_dynar_unshift(result->link_list, &link);
387 THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
395 As *AsGeneric::asExist(As *to_find)
397 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
398 xbt_dict_cursor_t cursor = NULL;
402 xbt_dict_foreach(p_routingSons, cursor, key, elem) {
403 if (to_find == elem || elem->asExist(to_find)) {
413 As *AsGeneric::autonomousSystemExist(char *element)
415 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
416 As *element_as, *result, *elem;
417 xbt_dict_cursor_t cursor = NULL;
419 element_as = ((RoutingEdge*)
420 xbt_lib_get_or_null(as_router_lib, element,
421 ROUTING_ASR_LEVEL))->getRcComponent();
422 result = ((As*) - 1);
423 if (element_as != this)
424 result = asExist(element_as);
428 xbt_dict_foreach(element_as->p_routingSons, cursor, key, elem) {
429 found = !strcmp(elem->p_name, element);
439 As *AsGeneric::processingUnitsExist(char *element)
441 As *element_as = sg_host_edge(sg_host_by_name(element)) ->getRcComponent();
442 if (element_as == this)
444 return asExist(element_as);
447 void AsGeneric::srcDstCheck(RoutingEdge *src, RoutingEdge *dst)
449 if (src == NULL || dst == NULL)
450 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
451 src ? src->getName() : "(null)",
452 dst ? dst->getName() : "(null)",
455 As *src_as = src->getRcComponent();
456 As *dst_as = dst->getRcComponent();
458 if (src_as != dst_as)
459 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
460 src->getName(), src_as->p_name,
461 dst->getName(), dst_as->p_name);
465 ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",