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 "surf_routing_private.hpp"
21 #include "network_interface.hpp"
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
25 static int no_bypassroute_declared = 1;
27 void generic_free_route(sg_platf_route_cbarg_t route)
30 xbt_dynar_free(&route->link_list);
38 void AsGeneric::parseRoute(sg_platf_route_cbarg_t /*route*/){
42 void AsGeneric::parseASroute(sg_platf_route_cbarg_t /*route*/){
46 void AsGeneric::getRouteAndLatency(RoutingEdge */*src*/, RoutingEdge */*dst*/, sg_platf_route_cbarg_t /*into*/, double */*latency*/){
50 AsGeneric::AsGeneric() {
51 p_bypassRoutes = xbt_dict_new_homogeneous((void (*)(void *)) generic_free_route);
54 AsGeneric::~AsGeneric() {
55 xbt_dict_free(&p_bypassRoutes);
58 int AsGeneric::parsePU(RoutingEdge *elm)
60 XBT_DEBUG("Load process unit \"%s\"", elm->getName());
61 xbt_dynar_push_as(p_indexNetworkElm, RoutingEdge*, elm);
62 return xbt_dynar_length(p_indexNetworkElm)-1;
65 int AsGeneric::parseAS(RoutingEdge *elm)
67 XBT_DEBUG("Load Autonomous system \"%s\"", elm->getName());
68 xbt_dynar_push_as(p_indexNetworkElm, RoutingEdge*, elm);
69 return xbt_dynar_length(p_indexNetworkElm)-1;
72 void AsGeneric::parseBypassroute(sg_platf_route_cbarg_t e_route)
74 char *src = (char*)(e_route->src);
75 char *dst = (char*)(e_route->dst);
78 XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
80 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
81 xbt_dict_t dict_bypassRoutes = p_bypassRoutes;
84 route_name = bprintf("%s#%s", src, dst);
85 xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
86 "Invalid count of links, must be greater than zero (%s,%s)",
88 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
89 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
90 src, e_route->gw_src->getName(), dst, e_route->gw_dst->getName());
92 sg_platf_route_cbarg_t new_e_route = NULL;
94 new_e_route = newExtendedRoute(SURF_ROUTING_RECURSIVE, e_route, 1);
96 new_e_route = newExtendedRoute(SURF_ROUTING_BASE, e_route, 1);
98 xbt_dynar_free(&(e_route->link_list));
100 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
101 no_bypassroute_declared = 0;
102 xbt_free(route_name);
108 /* ************************************************************************** */
109 /* *********************** GENERIC BUSINESS METHODS ************************* */
111 static const char *instr_node_name(xbt_node_t node)
113 void *data = xbt_graph_node_get_data(node);
114 char *str = (char *) data;
118 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
121 xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
125 ret = xbt_graph_new_node(graph, xbt_strdup(name));
126 xbt_dict_set(nodes, name, ret, NULL);
130 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
135 const char *sn = instr_node_name(s);
136 const char *dn = instr_node_name(d);
137 int len = strlen(sn) + strlen(dn) + 1;
138 char *name = (char *) xbt_malloc(len * sizeof(char));
141 snprintf(name, len, "%s%s", sn, dn);
142 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
144 snprintf(name, len, "%s%s", dn, sn);
145 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
149 ret = xbt_graph_new_edge(graph, s, d, NULL);
150 xbt_dict_set(edges, name, ret, NULL);
159 xbt_dynar_t AsGeneric::getOneLinkRoutes() { // FIXME: kill that stub
160 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
164 void AsGeneric::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
167 int table_size = xbt_dynar_length(p_indexNetworkElm);
170 for (src = 0; src < table_size; src++) {
171 RoutingEdge *my_src =
172 xbt_dynar_get_as(p_indexNetworkElm, src, RoutingEdge*);
173 for (dst = 0; dst < table_size; dst++) {
176 RoutingEdge *my_dst =
177 xbt_dynar_get_as(p_indexNetworkElm, dst, RoutingEdge*);
179 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
180 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
182 getRouteAndLatency(my_src, my_dst, route, NULL);
184 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->getName(), my_dst->getName());
189 xbt_node_t current, previous;
190 const char *previous_name, *current_name;
193 previous = new_xbt_graph_node(graph, route->gw_src->getName(), nodes);
194 previous_name = route->gw_src->getName();
196 previous = new_xbt_graph_node(graph, my_src->getName(), nodes);
197 previous_name = my_src->getName();
200 xbt_dynar_foreach(route->link_list, cpt, link) {
201 const char *link_name = static_cast<simgrid::surf::Resource*>(
203 current = new_xbt_graph_node(graph, link_name, nodes);
204 current_name = link_name;
205 new_xbt_graph_edge(graph, previous, current, edges);
206 XBT_DEBUG (" %s -> %s", previous_name, current_name);
208 previous_name = current_name;
212 current = new_xbt_graph_node(graph, route->gw_dst->getName(), nodes);
213 current_name = route->gw_dst->getName();
215 current = new_xbt_graph_node(graph, my_dst->getName(), nodes);
216 current_name = my_dst->getName();
218 new_xbt_graph_edge(graph, previous, current, edges);
219 XBT_DEBUG (" %s -> %s", previous_name, current_name);
221 xbt_dynar_free (&(route->link_list));
227 sg_platf_route_cbarg_t AsGeneric::getBypassRoute(RoutingEdge *src,
231 // If never set a bypass route return NULL without any further computations
232 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->getName(), dst->getName());
233 if (no_bypassroute_declared)
236 sg_platf_route_cbarg_t e_route_bypass = NULL;
237 xbt_dict_t dict_bypassRoutes = p_bypassRoutes;
239 if(dst->getRcComponent() == this && src->getRcComponent() == this ){
240 char *route_name = bprintf("%s#%s", src->getName(), dst->getName());
241 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
243 XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
248 int index_src, index_dst;
249 xbt_dynar_t path_src = NULL;
250 xbt_dynar_t path_dst = NULL;
252 As **current_src = NULL;
253 As **current_dst = NULL;
255 if (src == NULL || dst == NULL)
256 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
257 src ? src->getName() : "(null)",
258 dst ? dst->getName() : "(null)", p_name);
260 src_as = src->getRcComponent();
261 dst_as = dst->getRcComponent();
263 /* (2) find the path to the root routing component */
264 path_src = xbt_dynar_new(sizeof(As*), NULL);
266 while (current != NULL) {
267 xbt_dynar_push(path_src, ¤t);
268 current = current->p_routingFather;
270 path_dst = xbt_dynar_new(sizeof(As*), NULL);
272 while (current != NULL) {
273 xbt_dynar_push(path_dst, ¤t);
274 current = current->p_routingFather;
277 /* (3) find the common father */
278 index_src = path_src->used - 1;
279 index_dst = path_dst->used - 1;
280 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
281 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
282 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
283 xbt_dynar_pop_ptr(path_src);
284 xbt_dynar_pop_ptr(path_dst);
287 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
288 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
291 int max_index_src = path_src->used - 1;
292 int max_index_dst = path_dst->used - 1;
294 int max_index = std::max(max_index_src, max_index_dst);
297 for (max = 0; max <= max_index; max++) {
298 for (i = 0; i < max; i++) {
299 if (i <= max_index_src && max <= max_index_dst) {
300 char *route_name = bprintf("%s#%s",
302 (xbt_dynar_get_ptr(path_src, i)))->p_name,
304 (xbt_dynar_get_ptr(path_dst, max)))->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);
310 if (max <= max_index_src && i <= max_index_dst) {
311 char *route_name = bprintf("%s#%s",
313 (xbt_dynar_get_ptr(path_src, max)))->p_name,
315 (xbt_dynar_get_ptr(path_dst, i)))->p_name);
316 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
317 xbt_free(route_name);
326 if (max <= max_index_src && max <= max_index_dst) {
327 char *route_name = bprintf("%s#%s",
329 (xbt_dynar_get_ptr(path_src, max)))->p_name,
331 (xbt_dynar_get_ptr(path_dst, max)))->p_name);
332 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
333 xbt_free(route_name);
339 xbt_dynar_free(&path_src);
340 xbt_dynar_free(&path_dst);
343 sg_platf_route_cbarg_t new_e_route = NULL;
344 if (e_route_bypass) {
346 unsigned int cpt = 0;
347 new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
348 new_e_route->gw_src = e_route_bypass->gw_src;
349 new_e_route->gw_dst = e_route_bypass->gw_dst;
350 new_e_route->link_list =
351 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
352 xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
353 xbt_dynar_push(new_e_route->link_list, &link);
355 *lat += link->getLatency();
362 /* ************************************************************************** */
363 /* ************************* GENERIC AUX FUNCTIONS ************************** */
364 /* change a route containing link names into a route containing link entities */
365 sg_platf_route_cbarg_t AsGeneric::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
366 sg_platf_route_cbarg_t routearg, int change_order) {
368 sg_platf_route_cbarg_t result;
372 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
373 result->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
375 xbt_assert(hierarchy == SURF_ROUTING_BASE
376 || hierarchy == SURF_ROUTING_RECURSIVE,
377 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
379 if (hierarchy == SURF_ROUTING_RECURSIVE) {
381 xbt_assert(routearg->gw_src && routearg->gw_dst,
382 "NULL is obviously a bad gateway");
384 /* remember not erase the gateway names */
385 result->gw_src = routearg->gw_src;
386 result->gw_dst = routearg->gw_dst;
389 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
391 Link *link = Link::byName(link_name);
394 xbt_dynar_push(result->link_list, &link);
396 xbt_dynar_unshift(result->link_list, &link);
398 THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
406 As *AsGeneric::asExist(As *to_find)
408 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
409 xbt_dict_cursor_t cursor = NULL;
413 xbt_dict_foreach(p_routingSons, cursor, key, elem) {
414 if (to_find == elem || elem->asExist(to_find)) {
424 As *AsGeneric::autonomousSystemExist(char *element)
426 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
427 As *element_as, *result, *elem;
428 xbt_dict_cursor_t cursor = NULL;
430 element_as = ((RoutingEdge*)
431 xbt_lib_get_or_null(as_router_lib, element,
432 ROUTING_ASR_LEVEL))->getRcComponent();
433 result = ((As*) - 1);
434 if (element_as != this)
435 result = asExist(element_as);
439 xbt_dict_foreach(element_as->p_routingSons, cursor, key, elem) {
440 found = !strcmp(elem->p_name, element);
450 As *AsGeneric::processingUnitsExist(char *element)
452 As *element_as = sg_host_edge(sg_host_by_name(element)) ->getRcComponent();
453 if (element_as == this)
455 return asExist(element_as);
458 void AsGeneric::srcDstCheck(RoutingEdge *src, RoutingEdge *dst)
460 if (src == NULL || dst == NULL)
461 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
462 src ? src->getName() : "(null)",
463 dst ? dst->getName() : "(null)",
466 As *src_as = src->getRcComponent();
467 As *dst_as = dst->getRcComponent();
469 if (src_as != dst_as)
470 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
471 src->getName(), src_as->p_name,
472 dst->getName(), dst_as->p_name);
476 ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",