1 /* Copyright (c) 2009-2013. 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. */
7 #include "surf_routing_full.hpp"
8 #include "network_interface.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
13 extern routing_platf_t routing_platf;
14 extern int surf_parse_lineno;
16 #define TO_ROUTE_FULL(i,j) p_routingTable[(i)+(j)*table_size]
18 AS_t model_full_create(void)
23 void model_full_end(AS_t _routing)
26 sg_platf_route_cbarg_t e_route;
29 AsFullPtr routing = ((AsFullPtr) _routing);
30 int table_size = (int)xbt_dynar_length(routing->p_indexNetworkElm);
32 /* Create table if necessary */
33 if (!routing->p_routingTable)
34 routing->p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
36 /* Add the loopback if needed */
37 if (routing_platf->p_loopback && routing->p_hierarchy == SURF_ROUTING_BASE) {
38 for (i = 0; i < table_size; i++) {
39 e_route = routing->TO_ROUTE_FULL(i, i);
41 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
42 e_route->gw_src = NULL;
43 e_route->gw_dst = NULL;
44 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
45 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
46 routing->TO_ROUTE_FULL(i, i) = e_route;
57 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
59 /* Delete routing table */
60 for (i = 0; i < table_size; i++)
61 for (j = 0; j < table_size; j++) {
62 if (TO_ROUTE_FULL(i,j)){
63 xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
64 xbt_free(TO_ROUTE_FULL(i,j));
67 xbt_free(p_routingTable);
70 xbt_dynar_t AsFull::getOneLinkRoutes()
72 xbt_dynar_t ret = xbt_dynar_new(sizeof(OnelinkPtr), xbt_free);
75 int table_size = xbt_dynar_length(p_indexNetworkElm);
77 for(src=0; src < table_size; src++) {
78 for(dst=0; dst< table_size; dst++) {
79 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
81 if (xbt_dynar_length(route->link_list) == 1) {
82 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
84 if (p_hierarchy == SURF_ROUTING_BASE) {
85 RoutingEdgePtr tmp_src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_routing_edge_t);
87 RoutingEdgePtr tmp_dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_routing_edge_t);
89 onelink = new Onelink(link, tmp_src, tmp_dst);
90 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
91 onelink = new Onelink(link, route->gw_src, route->gw_dst);
93 onelink = new Onelink(link, NULL, NULL);
94 xbt_dynar_push(ret, &onelink);
95 XBT_DEBUG("Push route from '%d' to '%d'",
105 void AsFull::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
107 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
114 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
116 sg_platf_route_cbarg_t e_route = NULL;
118 unsigned int cpt = 0;
120 e_route = TO_ROUTE_FULL(src->m_id, dst->m_id);
123 res->gw_src = e_route->gw_src;
124 res->gw_dst = e_route->gw_dst;
125 xbt_dynar_foreach(e_route->link_list, cpt, link) {
126 xbt_dynar_push(res->link_list, &link);
128 *lat += static_cast<NetworkLinkPtr>(link)->getLatency();
133 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
137 static int full_pointer_resource_cmp(const void *a, const void *b)
142 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
145 char *src = (char*)(route->src);
146 char *dst = (char*)(route->dst);
147 RoutingEdgePtr src_net_elm, dst_net_elm;
148 src_net_elm = sg_routing_edge_by_name_or_null(src);
149 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
151 xbt_assert(src_net_elm, "Network elements %s not found", src);
152 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
154 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
156 xbt_assert(!xbt_dynar_is_empty(route->link_list),
157 "Invalid count of links, must be greater than zero (%s,%s)",
161 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
163 if (TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)) {
166 xbt_dynar_t link_route_to_test =
167 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
168 xbt_dynar_foreach(route->link_list, i, link_name) {
169 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
170 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
171 xbt_dynar_push(link_route_to_test, &link);
173 if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
174 link_route_to_test, full_pointer_resource_cmp)) {
175 surf_parse_error("A route between \"%s\" and \"%s\" already exists "
176 "with a different content. "
177 "If you are trying to define a reverse route, "
178 "you must set the symmetrical=no attribute to "
179 "your routes tags.", src, dst);
181 surf_parse_warn("Ignoring the identical redefinition of the route "
182 "between \"%s\" and \"%s\"", src, dst);
185 if (!route->gw_dst && !route->gw_dst)
186 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
188 // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
189 // AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
190 // if (subas == NULL)
191 // surf_parse_error("The source of an ASroute must be a sub-AS "
192 // "declared within the current AS, "
193 // "but '%s' is not an AS within '%s'", src, rc->name);
194 // if (subas->to_index
195 // && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
196 // surf_parse_error("In an ASroute, source gateway must be part of "
197 // "the source sub-AS (in particular, being in a "
198 // "sub-sub-AS is not allowed), "
199 // "but '%s' is not in '%s'.",
200 // route->src_gateway, subas->name);
202 // subas = xbt_dict_get_or_null(rc->routing_sons, dst);
203 // if (subas == NULL)
204 // surf_parse_error("The destination of an ASroute must be a sub-AS "
205 // "declared within the current AS, "
206 // "but '%s' is not an AS within '%s'", dst, rc->name);
207 // if (subas->to_index
208 // && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
209 // surf_parse_error("In an ASroute, destination gateway must be "
210 // "part of the destination sub-AS (in particular, "
211 // "in a sub-sub-AS is not allowed), "
212 // "but '%s' is not in '%s'.",
213 // route->dst_gateway, subas->name);
215 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
216 src, route->gw_src->p_name, dst, route->gw_dst->p_name);
217 if (route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
218 xbt_die("The dst_gateway '%s' does not exist!", route->gw_dst->p_name);
219 if (route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
220 xbt_die("The src_gateway '%s' does not exist!", route->gw_src->p_name);
222 TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 1);
223 xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list, 0);
226 if ( (route->symmetrical == TRUE && as_route == 0)
227 || (route->symmetrical == TRUE && as_route == 1)
229 if (route->gw_dst && route->gw_src) {
230 sg_routing_edge_t gw_tmp;
231 gw_tmp = route->gw_src;
232 route->gw_src = route->gw_dst;
233 route->gw_dst = gw_tmp;
235 if (TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)) {
238 xbt_dynar_t link_route_to_test =
239 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
240 for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
241 link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
242 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
243 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
244 xbt_dynar_push(link_route_to_test, &link);
246 xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
248 full_pointer_resource_cmp),
249 "The route between \"%s\" and \"%s\" already exists", src,
252 if (!route->gw_dst && !route->gw_src)
253 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
255 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
256 dst, route->gw_src->p_name, src, route->gw_dst->p_name);
257 TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 0);
258 xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list, 0);
261 xbt_dynar_free(&route->link_list);