Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Cleanup mc_hash
[simgrid.git] / src / surf / surf_routing_full.cpp
1 /* Copyright (c) 2009-2014. 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_full.hpp"
8 #include "network_interface.hpp"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_full, surf, "Routing part of surf");
11
12 /* Global vars */
13 extern routing_platf_t routing_platf;
14 extern int surf_parse_lineno;
15
16 #define TO_ROUTE_FULL(i,j) p_routingTable[(i)+(j)*table_size]
17
18 AS_t model_full_create(void)
19 {
20   return new AsFull();
21 }
22
23 void model_full_end(AS_t _routing)
24 {
25   int i;
26   sg_platf_route_cbarg_t e_route;
27
28   /* set utils vars */
29   AsFullPtr routing = ((AsFullPtr) _routing);
30   int table_size = (int)xbt_dynar_length(routing->p_indexNetworkElm);
31
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);
35
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);
40       if (!e_route) {
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;
47       }
48     }
49   }
50 }
51
52 AsFull::AsFull(){
53   p_routingTable = 0;
54 }
55
56 AsFull::~AsFull(){
57   if (p_routingTable) {
58     int table_size = (int)xbt_dynar_length(p_indexNetworkElm);
59     int i, j;
60     /* Delete routing table */
61     for (i = 0; i < table_size; i++)
62       for (j = 0; j < table_size; j++) {
63         if (TO_ROUTE_FULL(i,j)){
64           xbt_dynar_free(&TO_ROUTE_FULL(i,j)->link_list);
65           xbt_free(TO_ROUTE_FULL(i,j));
66         }
67       }
68     xbt_free(p_routingTable);
69   }
70 }
71
72 xbt_dynar_t AsFull::getOneLinkRoutes()
73 {
74   xbt_dynar_t ret = xbt_dynar_new(sizeof(OnelinkPtr), xbt_free_f);
75
76   int src, dst;
77   int table_size = xbt_dynar_length(p_indexNetworkElm);
78
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);
82       if (route) {
83         if (xbt_dynar_length(route->link_list) == 1) {
84           void *link = *(void **) xbt_dynar_get_ptr(route->link_list, 0);
85           OnelinkPtr onelink;
86           if (p_hierarchy == SURF_ROUTING_BASE) {
87                 RoutingEdgePtr tmp_src = xbt_dynar_get_as(p_indexNetworkElm, src, sg_routing_edge_t);
88             tmp_src->setId(src);
89                 RoutingEdgePtr tmp_dst = xbt_dynar_get_as(p_indexNetworkElm, dst, sg_routing_edge_t);
90                 tmp_dst->setId(dst);
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);
94           else
95             onelink = new Onelink(link, NULL, NULL);
96           xbt_dynar_push(ret, &onelink);
97           XBT_DEBUG("Push route from '%d' to '%d'",
98               src,
99               dst);
100         }
101       }
102     }
103   }
104   return ret;
105 }
106
107 void AsFull::getRouteAndLatency(RoutingEdgePtr src, RoutingEdgePtr dst, sg_platf_route_cbarg_t res, double *lat)
108 {
109   XBT_DEBUG("full_get_route_and_latency from %s[%d] to %s[%d]",
110       src->getName(),
111       src->getId(),
112       dst->getName(),
113       dst->getId());
114
115   /* set utils vars */
116   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
117
118   sg_platf_route_cbarg_t e_route = NULL;
119   void *link;
120   unsigned int cpt = 0;
121
122   e_route = TO_ROUTE_FULL(src->getId(), dst->getId());
123
124   if (e_route) {
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);
129       if (lat)
130         *lat += static_cast<NetworkLinkPtr>(link)->getLatency();
131     }
132   }
133 }
134
135 void AsFull::parseASroute(sg_platf_route_cbarg_t route){
136   parseRoute(route);
137 }
138
139 static int full_pointer_resource_cmp(const void *a, const void *b)
140 {
141   return a != b;
142 }
143
144 void AsFull::parseRoute(sg_platf_route_cbarg_t route)
145 {
146   int as_route = 0;
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);
152
153   xbt_assert(src_net_elm, "Network elements %s not found", src);
154   xbt_assert(dst_net_elm, "Network elements %s not found", dst);
155
156   size_t table_size = xbt_dynar_length(p_indexNetworkElm);
157
158   xbt_assert(!xbt_dynar_is_empty(route->link_list),
159       "Invalid count of links, must be greater than zero (%s,%s)",
160       src, dst);
161
162   if (!p_routingTable)
163     p_routingTable = xbt_new0(sg_platf_route_cbarg_t, table_size * table_size);
164
165   if (TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())) {
166     char *link_name;
167     unsigned int i;
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);
174     }
175     if (xbt_dynar_compare(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->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);
182     } else {
183       surf_parse_warn("Ignoring the identical redefinition of the route "
184           "between \"%s\" and \"%s\"", src, dst);
185     }
186   } else {
187     if (!route->gw_src && !route->gw_dst)
188       XBT_DEBUG("Load Route from \"%s\" to \"%s\"", src, dst);
189     else {
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);
203       //
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);
216       as_route = 1;
217       XBT_DEBUG("Load ASroute from \"%s\" to \"%s\"", src, dst);
218       if (!route->gw_src ||
219           route->gw_src->getRcType() == SURF_NETWORK_ELEMENT_NULL)
220         surf_parse_error("The src_gateway \"%s\" does not exist!",
221                 route->gw_src ? route->gw_src->getName() : "(null)");
222       if (!route->gw_dst ||
223           route->gw_dst->getRcType() == SURF_NETWORK_ELEMENT_NULL)
224         surf_parse_error("The dst_gateway \"%s\" does not exist!",
225                 route->gw_dst ? route->gw_dst->getName() : "(null)");
226       XBT_DEBUG("ASroute goes from \"%s\" to \"%s\"",
227                 route->gw_src->getName(), route->gw_dst->getName());
228     }
229     TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId()) = newExtendedRoute(p_hierarchy, route, 1);
230     xbt_dynar_shrink(TO_ROUTE_FULL(src_net_elm->getId(), dst_net_elm->getId())->link_list, 0);
231   }
232
233   if ( (route->symmetrical == TRUE && as_route == 0)
234       || (route->symmetrical == TRUE && as_route == 1)
235   ) {
236     if (route->gw_dst && route->gw_src) {
237       sg_routing_edge_t gw_tmp;
238       gw_tmp = route->gw_src;
239       route->gw_src = route->gw_dst;
240       route->gw_dst = gw_tmp;
241     }
242     if (TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())) {
243       char *link_name;
244       unsigned int i;
245       xbt_dynar_t link_route_to_test =
246           xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
247       for (i = xbt_dynar_length(route->link_list); i > 0; i--) {
248         link_name = xbt_dynar_get_as(route->link_list, i - 1, char *);
249         void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
250         xbt_assert(link, "Link : '%s' doesn't exists.", link_name);
251         xbt_dynar_push(link_route_to_test, &link);
252       }
253       xbt_assert(!xbt_dynar_compare(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list,
254           link_route_to_test,
255           full_pointer_resource_cmp),
256           "The route between \"%s\" and \"%s\" already exists", src,
257           dst);
258     } else {
259       if (!route->gw_dst && !route->gw_src)
260         XBT_DEBUG("Load Route from \"%s\" to \"%s\"", dst, src);
261       else
262         XBT_DEBUG("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",
263             dst, route->gw_src->getName(), src, route->gw_dst->getName());
264       TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId()) = newExtendedRoute(p_hierarchy, route, 0);
265       xbt_dynar_shrink(TO_ROUTE_FULL(dst_net_elm->getId(), src_net_elm->getId())->link_list, 0);
266     }
267   }
268   xbt_dynar_free(&route->link_list);
269 }
270
271
272
273