Logo AND Algorithmique Numérique Distribuée

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