Logo AND Algorithmique Numérique Distribuée

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