Logo AND Algorithmique Numérique Distribuée

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