Logo AND Algorithmique Numérique Distribuée

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