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"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
15 extern routing_platf_t routing_platf;
16 extern int surf_parse_lineno;
18 #define TO_ROUTE_FULL(i,j) p_routingTable[(i)+(j)*table_size]
20 AS_t model_full_create(void)
25 void model_full_end(AS_t _routing)
28 sg_platf_route_cbarg_t e_route;
31 AsFullPtr routing = ((AsFullPtr) _routing);
32 int table_size = (int)xbt_dynar_length(routing->p_indexNetworkElm);
34 /* Create table if necessary */
35 if (!routing->p_routingTable)
36 routing->p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
38 /* Add the loopback if needed */
39 if (routing_platf->p_loopback && routing->p_hierarchy == SURF_ROUTING_BASE) {
40 for (i = 0; i < table_size; i++) {
41 e_route = routing->TO_ROUTE_FULL(i, i);
43 e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
44 e_route->gw_src = NULL;
45 e_route->gw_dst = NULL;
46 e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
47 xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
48 routing->TO_ROUTE_FULL(i, i) = e_route;
59 int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
61 /* Delete routing table */
62 for (i = 0; i < table_size; i++)
63 for (j = 0; j < table_size; j++) {
64 if (TO_ROUTE_FULL(i,j)){
65 if (TO_ROUTE_FULL(i,j)->link_list)
66 xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
67 xbt_free(TO_ROUTE_FULL(i,j));
70 xbt_free(p_routingTable);
73 xbt_dynar_t AsFull::getOneLinkRoutes()
75 xbt_dynar_t ret = xbt_dynar_new(sizeof(OnelinkPtr), xbt_free);
78 int table_size = xbt_dynar_length(p_indexNetworkElm);
80 for(src=0; src < table_size; src++) {
81 for(dst=0; dst< table_size; dst++) {
82 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
84 if (xbt_dynar_length(route->link_list) == 1) {
85 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
87 if (p_hierarchy == SURF_ROUTING_BASE) {
88 RoutingEdgePtr tmp_src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_routing_edge_t);
90 RoutingEdgePtr tmp_dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_routing_edge_t);
92 onelink = new Onelink(link, tmp_src, tmp_dst);
93 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
94 onelink = new Onelink(link, route->gw_src, route->gw_dst);
96 onelink = new Onelink(link, NULL, NULL);
97 xbt_dynar_push(ret, &onelink);
98 XBT_DEBUG("Push route from '%d' to '%d'",
108 void AsFull::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
110 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
117 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
119 sg_platf_route_cbarg_t e_route = NULL;
121 unsigned int cpt = 0;
123 e_route = TO_ROUTE_FULL(src->m_id, dst->m_id);
126 res->gw_src = e_route->gw_src;
127 res->gw_dst = e_route->gw_dst;
128 xbt_dynar_foreach(e_route->link_list, cpt, link) {
129 xbt_dynar_push(res->link_list, &link);
131 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
136 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
140 static int full_pointer_resource_cmp(const void *a, const void *b)
145 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
148 char *src = (char*)(route->src);
149 char *dst = (char*)(route->dst);
150 RoutingEdgePtr src_net_elm, dst_net_elm;
151 src_net_elm = sg_routing_edge_by_name_or_null(src);
152 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
154 xbt_assert(src_net_elm, "Network elements %s not found", src);
155 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
157 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
159 xbt_assert(!xbt_dynar_is_empty(route->link_list),
160 "Invalid count of links, must be greater than zero (%s,%s)",
164 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
166 if (TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)) {
169 xbt_dynar_t link_route_to_test =
170 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
171 xbt_dynar_foreach(route->link_list, i, link_name) {
172 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
173 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
174 xbt_dynar_push(link_route_to_test, &link);
176 if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
177 link_route_to_test, full_pointer_resource_cmp)) {
178 surf_parse_error("A route between \"%s\" and \"%s\" already exists "
179 "with a different content. "
180 "If you are trying to define a reverse route, "
181 "you must set the symmetrical=no attribute to "
182 "your routes tags.", src, dst);
184 surf_parse_warn("Ignoring the identical redefinition of the route "
185 "between \"%s\" and \"%s\"", src, dst);
188 if (!route->gw_dst && !route->gw_dst)
189 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
191 // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
192 // AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
193 // if (subas == NULL)
194 // surf_parse_error("The source of an ASroute must be a sub-AS "
195 // "declared within the current AS, "
196 // "but '%s' is not an AS within '%s'", src, rc->name);
197 // if (subas->to_index
198 // && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
199 // surf_parse_error("In an ASroute, source gateway must be part of "
200 // "the source sub-AS (in particular, being in a "
201 // "sub-sub-AS is not allowed), "
202 // "but '%s' is not in '%s'.",
203 // route->src_gateway, subas->name);
205 // subas = xbt_dict_get_or_null(rc->routing_sons, dst);
206 // if (subas == NULL)
207 // surf_parse_error("The destination of an ASroute must be a sub-AS "
208 // "declared within the current AS, "
209 // "but '%s' is not an AS within '%s'", dst, rc->name);
210 // if (subas->to_index
211 // && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
212 // surf_parse_error("In an ASroute, destination gateway must be "
213 // "part of the destination sub-AS (in particular, "
214 // "in a sub-sub-AS is not allowed), "
215 // "but '%s' is not in '%s'.",
216 // route->dst_gateway, subas->name);
218 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
219 src, route->gw_src->p_name, dst, route->gw_dst->p_name);
220 if (route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
221 xbt_die("The dst_gateway '%s' does not exist!", route->gw_dst->p_name);
222 if (route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
223 xbt_die("The src_gateway '%s' does not exist!", route->gw_src->p_name);
225 TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 1);
226 xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list, 0);
229 if ( (route->symmetrical == TRUE && as_route == 0)
230 || (route->symmetrical == TRUE && as_route == 1)
232 if (route->gw_dst && route->gw_src) {
233 sg_routing_edge_t gw_tmp;
234 gw_tmp = route->gw_src;
235 route->gw_src = route->gw_dst;
236 route->gw_dst = gw_tmp;
238 if (TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)) {
241 xbt_dynar_t link_route_to_test =
242 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
243 for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
244 link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
245 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
246 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
247 xbt_dynar_push(link_route_to_test, &link);
249 xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
251 full_pointer_resource_cmp),
252 "The route between \"%s\" and \"%s\" already exists", src,
255 if (!route->gw_dst && !route->gw_src)
256 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
258 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
259 dst, route->gw_src->p_name, src, route->gw_dst->p_name);
260 TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 0);
261 xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list, 0);
264 xbt_dynar_free(&route->link_list);