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 xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
66 xbt_free(TO_ROUTE_FULL(i,j));
69 xbt_free(p_routingTable);
72 xbt_dynar_t AsFull::getOneLinkRoutes()
74 xbt_dynar_t ret = xbt_dynar_new(sizeof(OnelinkPtr), xbt_free);
77 int table_size = xbt_dynar_length(p_indexNetworkElm);
79 for(src=0; src < table_size; src++) {
80 for(dst=0; dst< table_size; dst++) {
81 sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
83 if (xbt_dynar_length(route->link_list) == 1) {
84 void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
86 if (p_hierarchy == SURF_ROUTING_BASE) {
87 RoutingEdgePtr tmp_src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_routing_edge_t);
89 RoutingEdgePtr tmp_dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_routing_edge_t);
91 onelink = new Onelink(link, tmp_src, tmp_dst);
92 } else if (p_hierarchy == SURF_ROUTING_RECURSIVE)
93 onelink = new Onelink(link, route->gw_src, route->gw_dst);
95 onelink = new Onelink(link, NULL, NULL);
96 xbt_dynar_push(ret, &onelink);
97 XBT_DEBUG("Push route from '%d' to '%d'",
107 void AsFull::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
109 XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
116 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
118 sg_platf_route_cbarg_t e_route = NULL;
120 unsigned int cpt = 0;
122 e_route = TO_ROUTE_FULL(src->m_id, dst->m_id);
125 res->gw_src = e_route->gw_src;
126 res->gw_dst = e_route->gw_dst;
127 xbt_dynar_foreach(e_route->link_list, cpt, link) {
128 xbt_dynar_push(res->link_list, &link);
130 *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
135 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
139 static int full_pointer_resource_cmp(const void *a, const void *b)
144 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
147 char *src = (char*)(route->src);
148 char *dst = (char*)(route->dst);
149 RoutingEdgePtr src_net_elm, dst_net_elm;
150 src_net_elm = sg_routing_edge_by_name_or_null(src);
151 dst_net_elm = sg_routing_edge_by_name_or_null(dst);
153 xbt_assert(src_net_elm, "Network elements %s not found", src);
154 xbt_assert(dst_net_elm, "Network elements %s not found", dst);
156 size_t table_size = xbt_dynar_length(p_indexNetworkElm);
158 xbt_assert(!xbt_dynar_is_empty(route->link_list),
159 "Invalid count of links, must be greater than zero (%s,%s)",
163 p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
165 if (TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)) {
168 xbt_dynar_t link_route_to_test =
169 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
170 xbt_dynar_foreach(route->link_list, i, link_name) {
171 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
172 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
173 xbt_dynar_push(link_route_to_test, &link);
175 if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
176 link_route_to_test, full_pointer_resource_cmp)) {
177 surf_parse_error("A route between \"%s\" and \"%s\" already exists "
178 "with a different content. "
179 "If you are trying to define a reverse route, "
180 "you must set the symmetrical=no attribute to "
181 "your routes tags.", src, dst);
183 surf_parse_warn("Ignoring the identical redefinition of the route "
184 "between \"%s\" and \"%s\"", src, dst);
187 if (!route->gw_dst && !route->gw_dst)
188 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
190 // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
191 // AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
192 // if (subas == NULL)
193 // surf_parse_error("The source of an ASroute must be a sub-AS "
194 // "declared within the current AS, "
195 // "but '%s' is not an AS within '%s'", src, rc->name);
196 // if (subas->to_index
197 // && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
198 // surf_parse_error("In an ASroute, source gateway must be part of "
199 // "the source sub-AS (in particular, being in a "
200 // "sub-sub-AS is not allowed), "
201 // "but '%s' is not in '%s'.",
202 // route->src_gateway, subas->name);
204 // subas = xbt_dict_get_or_null(rc->routing_sons, dst);
205 // if (subas == NULL)
206 // surf_parse_error("The destination of an ASroute must be a sub-AS "
207 // "declared within the current AS, "
208 // "but '%s' is not an AS within '%s'", dst, rc->name);
209 // if (subas->to_index
210 // && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
211 // surf_parse_error("In an ASroute, destination gateway must be "
212 // "part of the destination sub-AS (in particular, "
213 // "in a sub-sub-AS is not allowed), "
214 // "but '%s' is not in '%s'.",
215 // route->dst_gateway, subas->name);
217 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
218 src, route->gw_src->p_name, dst, route->gw_dst->p_name);
219 if (route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
220 xbt_die("The dst_gateway '%s' does not exist!", route->gw_dst->p_name);
221 if (route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
222 xbt_die("The src_gateway '%s' does not exist!", route->gw_src->p_name);
224 TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 1);
225 xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list, 0);
228 if ( (route->symmetrical == TRUE && as_route == 0)
229 || (route->symmetrical == TRUE && as_route == 1)
231 if (route->gw_dst && route->gw_src) {
232 sg_routing_edge_t gw_tmp;
233 gw_tmp = route->gw_src;
234 route->gw_src = route->gw_dst;
235 route->gw_dst = gw_tmp;
237 if (TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)) {
240 xbt_dynar_t link_route_to_test =
241 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
242 for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
243 link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
244 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
245 xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
246 xbt_dynar_push(link_route_to_test, &link);
248 xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
250 full_pointer_resource_cmp),
251 "The route between \"%s\" and \"%s\" already exists", src,
254 if (!route->gw_dst && !route->gw_src)
255 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
257 XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
258 dst, route->gw_src->p_name, src, route->gw_dst->p_name);
259 TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 0);
260 xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list, 0);
263 xbt_dynar_free(&route->link_list);