Logo AND Algorithmique Numérique Distribuée

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