Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge commit '045db1657e870c721be490b411868f4181a12ced' into surf++
[simgrid.git] / src / surf / surf_routing_full.cpp
1 /* Copyright (c) 2009-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "surf_routing_private.h"
8 #include "surf_routing_full.hpp"
9 #include "network.hpp"
10
11 extern "C" {
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
13 }
14
15 /* Global vars */
16 extern routing_platf_t routing_platf;
17 extern int surf_parse_lineno;
18
19 #define TO_ROUTE_FULL(i,j) p_routingTable[(i)+(j)*table_size]
20
21 AS_t model_full_create(void)
22 {
23   return new AsFull();
24 }
25
26 void model_full_end(AS_t _routing)
27 {
28   unsigned int i;
29   sg_platf_route_cbarg_t e_route;
30
31   /* set utils vars */
32   AsFullPtr routing = ((AsFullPtr) _routing);
33   size_t table_size = xbt_dynar_length(routing->p_indexNetworkElm);
34
35   /* Create table if necessary */
36   if (!routing->p_routingTable)
37     routing->p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
38
39   /* Add the loopback if needed */
40   if (routing_platf->p_loopback && routing->p_hierarchy == SURF_ROUTING_BASE) {
41     for (i = 0; i < table_size; i++) {
42       e_route = routing->TO_ROUTE_FULL(i, i);
43       if (!e_route) {
44         e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
45         e_route->gw_src = NULL;
46         e_route->gw_dst = NULL;
47         e_route->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
48         xbt_dynar_push(e_route->link_list, &routing_platf->p_loopback);
49         routing->TO_ROUTE_FULL(i, i) = e_route;
50       }
51     }
52   }
53 }
54
55 AsFull::AsFull(){
56   p_routingTable = 0;
57 }
58
59 AsFull::~AsFull(){
60   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
61   int i, j;
62   /* Delete routing table */
63   for (i = 0; i < table_size; i++)
64     for (j = 0; j < table_size; j++)
65       delete TO_ROUTE_FULL(i,j);
66   xbt_free(p_routingTable);
67 }
68
69 xbt_dynar_t AsFull::getOneLinkRoutes()
70 {
71   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
72
73   int src, dst;
74   int table_size = xbt_dynar_length(p_indexNetworkElm);
75
76   for(src=0; src < table_size; src++) {
77     for(dst=0; dst< table_size; dst++) {
78       sg_platf_route_cbarg_t route = TO_ROUTE_FULL(src,dst);
79       if (route) {
80         if (xbt_dynar_length(route->link_list) == 1) {
81           void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
82           onelink_t onelink = xbt_new0(s_onelink_t, 1);
83           onelink->link_ptr = link;
84           if (p_hierarchy == SURF_ROUTING_BASE) {
85             onelink->src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_routing_edge_t);
86             onelink->src->m_id = src;
87             onelink->dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_routing_edge_t);
88             onelink->dst->m_id = dst;
89           } else if (p_hierarchy == SURF_ROUTING_RECURSIVE) {
90             onelink->src = route->gw_src;
91             onelink->dst = route->gw_dst;
92           }
93           xbt_dynar_push(ret, &onelink);
94           XBT_DEBUG("Push route from '%d' to '%d'",
95               src,
96               dst);
97         }
98       }
99     }
100   }
101   return ret;
102 }
103
104 void AsFull::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
105 {
106   XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
107       src->p_name,
108       src->m_id,
109       dst->p_name,
110       dst->m_id  );
111
112   /* set utils vars */
113   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
114
115   sg_platf_route_cbarg_t e_route = NULL;
116   void *link;
117   unsigned int cpt = 0;
118
119   e_route = TO_ROUTE_FULL(src->m_id, dst->m_id);
120
121   if (e_route) {
122     res->gw_src = e_route->gw_src;
123     res->gw_dst = e_route->gw_dst;
124     xbt_dynar_foreach(e_route->link_list, cpt, link) {
125       xbt_dynar_push(res->link_list, &link);
126       if (lat)
127         *lat += dynamic_cast<NetworkCm02LinkPtr>(static_cast<ResourcePtr>(link))->getLatency();
128     }
129   }
130 }
131
132 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
133   parseRoute(route);
134 }
135
136 static int full_pointer_resource_cmp(const void *a, const void *b)
137 {
138   return a != b;
139 }
140
141 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
142 {
143   int as_route = 0;
144   char *src = (char*)(route->src);
145   char *dst = (char*)(route->dst);
146   RoutingEdgePtr src_net_elm, dst_net_elm;
147   src_net_elm = sg_routing_edge_by_name_or_null(src);
148   dst_net_elm = sg_routing_edge_by_name_or_null(dst);
149
150   xbt_assert(src_net_elm, "Network elements %s not found", src);
151   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
152
153   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
154
155   xbt_assert(!xbt_dynar_is_empty(route->link_list),
156       "Invalid count of links, must be greater than zero (%s,%s)",
157       src, dst);
158
159   if (!p_routingTable)
160     p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
161
162   if (TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)) {
163     char *link_name;
164     unsigned int i;
165     xbt_dynar_t link_route_to_test =
166         xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
167     xbt_dynar_foreach(route->link_list, i, link_name) {
168       void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
169       xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
170       xbt_dynar_push(link_route_to_test, &link);
171     }
172     if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list,
173         link_route_to_test, full_pointer_resource_cmp)) {
174       surf_parse_error("A route between \"%s\" and \"%s\" already exists "
175           "with a different content. "
176           "If you are trying to define a reverse route, "
177           "you must set the symmetrical=no attribute to "
178           "your routes tags.", src, dst);
179     } else {
180       surf_parse_warn("Ignoring the identical redefinition of the route "
181           "between \"%s\" and \"%s\"", src, dst);
182     }
183   } else {
184     if (!route->gw_dst && !route->gw_dst)
185       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
186     else {
187       // FIXME We can call a gw which is down the current AS (cf g5k.xml) but not upper.
188       //      AS_t subas = xbt_dict_get_or_null(rc->routing_sons, src);
189       //      if (subas == NULL)
190       //        surf_parse_error("The source of an ASroute must be a sub-AS "
191       //                         "declared within the current AS, "
192       //                         "but '%s' is not an AS within '%s'", src, rc->name);
193       //      if (subas->to_index
194       //          && xbt_dict_get_or_null(subas->to_index, route->src_gateway) == NULL)
195       //        surf_parse_error("In an ASroute, source gateway must be part of "
196       //                         "the source sub-AS (in particular, being in a "
197       //                         "sub-sub-AS is not allowed), "
198       //                         "but '%s' is not in '%s'.",
199       //                         route->src_gateway, subas->name);
200       //
201       //      subas = xbt_dict_get_or_null(rc->routing_sons, dst);
202       //      if (subas == NULL)
203       //        surf_parse_error("The destination of an ASroute must be a sub-AS "
204       //                         "declared within the current AS, "
205       //                         "but '%s' is not an AS within '%s'", dst, rc->name);
206       //      if (subas->to_index
207       //          && xbt_dict_get_or_null(subas->to_index, route->dst_gateway) == NULL)
208       //        surf_parse_error("In an ASroute, destination gateway must be "
209       //                         "part of the destination sub-AS (in particular, "
210       //                         "in a sub-sub-AS is not allowed), "
211       //                         "but '%s' is not in '%s'.",
212       //                         route->dst_gateway, subas->name);
213       as_route = 1;
214       XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
215           src, route->gw_src->p_name, dst, route->gw_dst->p_name);
216       if (route->gw_dst->p_rcType == SURF_NETWORK_ELEMENT_NULL)
217         xbt_die("The dst_gateway '%s' does not exist!", route->gw_dst->p_name);
218       if (route->gw_src->p_rcType == SURF_NETWORK_ELEMENT_NULL)
219         xbt_die("The src_gateway '%s' does not exist!", route->gw_src->p_name);
220     }
221     TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 1);
222     xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->m_id, dst_net_elm->m_id)->link_list, 0);
223   }
224
225   if ( (route->symmetrical == TRUE && as_route == 0)
226       || (route->symmetrical == TRUE && as_route == 1)
227   ) {
228     if (route->gw_dst && route->gw_src) {
229       sg_routing_edge_t gw_tmp;
230       gw_tmp = route->gw_src;
231       route->gw_src = route->gw_dst;
232       route->gw_dst = gw_tmp;
233     }
234     if (TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)) {
235       char *link_name;
236       unsigned int i;
237       xbt_dynar_t link_route_to_test =
238           xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
239       for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
240         link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
241         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
242         xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
243         xbt_dynar_push(link_route_to_test, &link);
244       }
245       xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list,
246           link_route_to_test,
247           full_pointer_resource_cmp),
248           "The route between \"%s\" and \"%s\" already exists", src,
249           dst);
250     } else {
251       if (!route->gw_dst && !route->gw_src)
252         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
253       else
254         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
255             dst, route->gw_src->p_name, src, route->gw_dst->p_name);
256       TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id) = newExtendedRoute(p_hierarchy, route, 0);
257       xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->m_id, src_net_elm->m_id)->link_list, 0);
258     }
259   }
260   xbt_dynar_free(&route->link_list);
261 }
262
263
264
265