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(NetCard */*src*/, NetCard */*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(NetCard *elm)
60 XBT_DEBUG("Load process unit \"%s\"", elm->getName());
61 xbt_dynar_push_as(p_indexNetworkElm, NetCard*, elm);
62 return xbt_dynar_length(p_indexNetworkElm)-1;
65 int AsGeneric::parseAS(NetCard *elm)
67 XBT_DEBUG("Load Autonomous system \"%s\"", elm->getName());
68 xbt_dynar_push_as(p_indexNetworkElm, NetCard*, 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 void AsGeneric::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
162 int table_size = xbt_dynar_length(p_indexNetworkElm);
165 for (src = 0; src < table_size; src++) {
167 xbt_dynar_get_as(p_indexNetworkElm, src, NetCard*);
168 for (dst = 0; dst < table_size; dst++) {
172 xbt_dynar_get_as(p_indexNetworkElm, dst, NetCard*);
174 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
175 route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
177 getRouteAndLatency(my_src, my_dst, route, NULL);
179 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->getName(), my_dst->getName());
184 xbt_node_t current, previous;
185 const char *previous_name, *current_name;
188 previous = new_xbt_graph_node(graph, route->gw_src->getName(), nodes);
189 previous_name = route->gw_src->getName();
191 previous = new_xbt_graph_node(graph, my_src->getName(), nodes);
192 previous_name = my_src->getName();
195 xbt_dynar_foreach(route->link_list, cpt, link) {
196 const char *link_name = static_cast<simgrid::surf::Resource*>(
198 current = new_xbt_graph_node(graph, link_name, nodes);
199 current_name = link_name;
200 new_xbt_graph_edge(graph, previous, current, edges);
201 XBT_DEBUG (" %s -> %s", previous_name, current_name);
203 previous_name = current_name;
207 current = new_xbt_graph_node(graph, route->gw_dst->getName(), nodes);
208 current_name = route->gw_dst->getName();
210 current = new_xbt_graph_node(graph, my_dst->getName(), nodes);
211 current_name = my_dst->getName();
213 new_xbt_graph_edge(graph, previous, current, edges);
214 XBT_DEBUG (" %s -> %s", previous_name, current_name);
216 xbt_dynar_free (&(route->link_list));
222 sg_platf_route_cbarg_t AsGeneric::getBypassRoute(NetCard *src,
226 // If never set a bypass route return NULL without any further computations
227 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->getName(), dst->getName());
228 if (no_bypassroute_declared)
231 sg_platf_route_cbarg_t e_route_bypass = NULL;
232 xbt_dict_t dict_bypassRoutes = p_bypassRoutes;
234 if(dst->getRcComponent() == this && src->getRcComponent() == this ){
235 char *route_name = bprintf("%s#%s", src->getName(), dst->getName());
236 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
238 XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
243 int index_src, index_dst;
244 xbt_dynar_t path_src = NULL;
245 xbt_dynar_t path_dst = NULL;
247 As **current_src = NULL;
248 As **current_dst = NULL;
250 if (src == NULL || dst == NULL)
251 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
252 src ? src->getName() : "(null)",
253 dst ? dst->getName() : "(null)", p_name);
255 src_as = src->getRcComponent();
256 dst_as = dst->getRcComponent();
258 /* (2) find the path to the root routing component */
259 path_src = xbt_dynar_new(sizeof(As*), NULL);
261 while (current != NULL) {
262 xbt_dynar_push(path_src, ¤t);
263 current = current->p_routingFather;
265 path_dst = xbt_dynar_new(sizeof(As*), NULL);
267 while (current != NULL) {
268 xbt_dynar_push(path_dst, ¤t);
269 current = current->p_routingFather;
272 /* (3) find the common father */
273 index_src = path_src->used - 1;
274 index_dst = path_dst->used - 1;
275 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
276 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
277 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
278 xbt_dynar_pop_ptr(path_src);
279 xbt_dynar_pop_ptr(path_dst);
282 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
283 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
286 int max_index_src = path_src->used - 1;
287 int max_index_dst = path_dst->used - 1;
289 int max_index = std::max(max_index_src, max_index_dst);
292 for (max = 0; max <= max_index; max++) {
293 for (i = 0; i < max; i++) {
294 if (i <= max_index_src && max <= max_index_dst) {
295 char *route_name = bprintf("%s#%s",
297 (xbt_dynar_get_ptr(path_src, i)))->p_name,
299 (xbt_dynar_get_ptr(path_dst, max)))->p_name);
300 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
301 xbt_free(route_name);
305 if (max <= max_index_src && i <= max_index_dst) {
306 char *route_name = bprintf("%s#%s",
308 (xbt_dynar_get_ptr(path_src, max)))->p_name,
310 (xbt_dynar_get_ptr(path_dst, i)))->p_name);
311 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
312 xbt_free(route_name);
321 if (max <= max_index_src && max <= max_index_dst) {
322 char *route_name = bprintf("%s#%s",
324 (xbt_dynar_get_ptr(path_src, max)))->p_name,
326 (xbt_dynar_get_ptr(path_dst, max)))->p_name);
327 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(dict_bypassRoutes, route_name);
328 xbt_free(route_name);
334 xbt_dynar_free(&path_src);
335 xbt_dynar_free(&path_dst);
338 sg_platf_route_cbarg_t new_e_route = NULL;
339 if (e_route_bypass) {
341 unsigned int cpt = 0;
342 new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
343 new_e_route->gw_src = e_route_bypass->gw_src;
344 new_e_route->gw_dst = e_route_bypass->gw_dst;
345 new_e_route->link_list =
346 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
347 xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
348 xbt_dynar_push(new_e_route->link_list, &link);
350 *lat += link->getLatency();
357 /* ************************************************************************** */
358 /* ************************* GENERIC AUX FUNCTIONS ************************** */
359 /* change a route containing link names into a route containing link entities */
360 sg_platf_route_cbarg_t AsGeneric::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
361 sg_platf_route_cbarg_t routearg, int change_order) {
363 sg_platf_route_cbarg_t result;
367 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
368 result->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
370 xbt_assert(hierarchy == SURF_ROUTING_BASE
371 || hierarchy == SURF_ROUTING_RECURSIVE,
372 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
374 if (hierarchy == SURF_ROUTING_RECURSIVE) {
376 xbt_assert(routearg->gw_src && routearg->gw_dst,
377 "NULL is obviously a bad gateway");
379 /* remember not erase the gateway names */
380 result->gw_src = routearg->gw_src;
381 result->gw_dst = routearg->gw_dst;
384 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
386 Link *link = Link::byName(link_name);
389 xbt_dynar_push(result->link_list, &link);
391 xbt_dynar_unshift(result->link_list, &link);
393 THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
401 As *AsGeneric::asExist(As *to_find)
403 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
404 xbt_dict_cursor_t cursor = NULL;
408 xbt_dict_foreach(p_routingSons, cursor, key, elem) {
409 if (to_find == elem || elem->asExist(to_find)) {
419 As *AsGeneric::autonomousSystemExist(char *element)
421 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
422 As *element_as, *result, *elem;
423 xbt_dict_cursor_t cursor = NULL;
425 element_as = ((NetCard*)
426 xbt_lib_get_or_null(as_router_lib, element,
427 ROUTING_ASR_LEVEL))->getRcComponent();
428 result = ((As*) - 1);
429 if (element_as != this)
430 result = asExist(element_as);
434 xbt_dict_foreach(element_as->p_routingSons, cursor, key, elem) {
435 found = !strcmp(elem->p_name, element);
445 As *AsGeneric::processingUnitsExist(char *element)
447 As *element_as = sg_host_by_name(element)->pimpl_netcard ->getRcComponent();
448 if (element_as == this)
450 return asExist(element_as);
453 void AsGeneric::srcDstCheck(NetCard *src, NetCard *dst)
455 if (src == NULL || dst == NULL)
456 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
457 src ? src->getName() : "(null)",
458 dst ? dst->getName() : "(null)",
461 As *src_as = src->getRcComponent();
462 As *dst_as = dst->getRcComponent();
464 if (src_as != dst_as)
465 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
466 src->getName(), src_as->p_name,
467 dst->getName(), dst_as->p_name);
471 ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",