Logo AND Algorithmique Numérique Distribuée

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