Logo AND Algorithmique Numérique Distribuée

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