Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
46309a6a5ad3ba9222bca86627e3787ff152320f
[simgrid.git] / src / surf / surf_routing_rulebased.c
1 /* Copyright (c) 2009, 2010, 2011. 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 #include "surf_routing_private.h"
7 #include <pcre.h>               /* regular expression library */
8
9 /* Global vars */
10 extern routing_global_t global_routing;
11 extern xbt_dynar_t parsed_link_list;
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_rulebased, surf, "Routing part of surf");
14
15 /* Routing model structure */
16
17 typedef struct {
18   s_as_t generic_routing;
19   xbt_dict_t dict_processing_units;
20   xbt_dict_t dict_autonomous_systems;
21   xbt_dynar_t list_route;
22   xbt_dynar_t list_ASroute;
23 } s_AS_rulebased_t, *AS_rulebased_t;
24
25 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
26 typedef struct s_rule_route_extended s_rule_route_extended_t,
27     *rule_route_extended_t;
28
29 struct s_rule_route {
30   xbt_dynar_t re_str_link;      // dynar of char*
31   pcre *re_src;
32   pcre *re_dst;
33 };
34
35 struct s_rule_route_extended {
36   s_rule_route_t generic_rule_route;
37   char *re_src_gateway;
38   char *re_dst_gateway;
39 };
40
41 static void rule_route_free(void *e)
42 {
43   rule_route_t *elem = (rule_route_t *) (e);
44   if (*elem) {
45     xbt_dynar_free(&(*elem)->re_str_link);
46     pcre_free((*elem)->re_src);
47     pcre_free((*elem)->re_dst);
48     xbt_free(*elem);
49   }
50   *elem = NULL;
51 }
52
53 static void rule_route_extended_free(void *e)
54 {
55   rule_route_extended_t *elem = (rule_route_extended_t *) e;
56   if (*elem) {
57     xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
58     pcre_free((*elem)->generic_rule_route.re_src);
59     pcre_free((*elem)->generic_rule_route.re_dst);
60     xbt_free((*elem)->re_src_gateway);
61     xbt_free((*elem)->re_dst_gateway);
62     xbt_free(*elem);
63   }
64 }
65
66 /* Parse routing model functions */
67
68 static void model_rulebased_parse_PU(AS_t rc, const char *name)
69 {
70   AS_rulebased_t routing = (AS_rulebased_t) rc;
71   xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
72 }
73
74 static void model_rulebased_parse_AS(AS_t rc, const char *name)
75 {
76   AS_rulebased_t routing = (AS_rulebased_t) rc;
77   xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
78                NULL);
79 }
80
81 static void model_rulebased_parse_route(AS_t rc,
82                                       const char *src, const char *dst,
83                                       route_t route)
84 {
85   AS_rulebased_t routing = (AS_rulebased_t) rc;
86   rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
87   const char *error;
88   int erroffset;
89
90   if(!strcmp(rc->model_desc->name,"Vivaldi")){
91           if(!xbt_dynar_is_empty(route->link_list))
92                   xbt_die("You can't have link_ctn with Model Vivaldi.");
93   }
94
95   ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
96   xbt_assert(ruleroute->re_src,
97               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
98               erroffset, src, error);
99   ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
100   xbt_assert(ruleroute->re_src,
101               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
102               erroffset, dst, error);
103   ruleroute->re_str_link = route->link_list;
104   xbt_dynar_push(routing->list_route, &ruleroute);
105   xbt_free(route);
106 }
107
108 static void model_rulebased_parse_ASroute(AS_t rc,
109                                         const char *src, const char *dst,
110                                         route_t route)
111 {
112   AS_rulebased_t routing = (AS_rulebased_t) rc;
113   rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
114   const char *error;
115   int erroffset;
116
117   if(!strcmp(rc->model_desc->name,"Vivaldi")){
118           if(!xbt_dynar_is_empty(route->link_list))
119                   xbt_die("You can't have link_ctn with Model Vivaldi.");
120   }
121
122   ruleroute_e->generic_rule_route.re_src =
123       pcre_compile(src, 0, &error, &erroffset, NULL);
124   xbt_assert(ruleroute_e->generic_rule_route.re_src,
125               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
126               erroffset, src, error);
127   ruleroute_e->generic_rule_route.re_dst =
128       pcre_compile(dst, 0, &error, &erroffset, NULL);
129   xbt_assert(ruleroute_e->generic_rule_route.re_src,
130               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
131               erroffset, dst, error);
132   ruleroute_e->generic_rule_route.re_str_link =
133       route->link_list;
134   ruleroute_e->re_src_gateway = route->src_gateway;
135   ruleroute_e->re_dst_gateway = route->dst_gateway;
136   xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
137 //  xbt_free(route->src_gateway);
138 //  xbt_free(route->dst_gateway);
139   xbt_free(route);
140 }
141
142 static void model_rulebased_parse_bypassroute(AS_t rc,
143                                             const char *src,
144                                             const char *dst,
145                                             route_t e_route)
146 {
147   xbt_die("bypass routing not supported for Route-Based model");
148 }
149
150 #define BUFFER_SIZE 4096        /* result buffer size */
151 #define OVECCOUNT 30            /* should be a multiple of 3 */
152
153 static char *remplace(char *value, const char **src_list, int src_size,
154                       const char **dst_list, int dst_size)
155 {
156   char result[BUFFER_SIZE];
157   int i_res = 0;
158   int i = 0;
159
160   while (value[i]) {
161     if (value[i] == '$') {
162       i++;                      /* skip the '$' */
163       if (value[i] < '0' || value[i] > '9')
164         xbt_die("bad string parameter, no number indication, at offset: "
165                 "%d (\"%s\")", i, value);
166
167       /* solve the number */
168       int number = value[i++] - '0';
169       while (value[i] >= '0' && value[i] <= '9')
170         number = 10 * number + (value[i++] - '0');
171
172       /* solve the indication */
173       const char **param_list;
174       _XBT_GNUC_UNUSED int param_size;
175       if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
176         param_list = src_list;
177         param_size = src_size;
178       } else if (value[i] == 'd' && value[i + 1] == 's'
179                  && value[i + 2] == 't') {
180         param_list = dst_list;
181         param_size = dst_size;
182       } else {
183         xbt_die("bad string parameter, support only \"src\" and \"dst\", "
184                 "at offset: %d (\"%s\")", i, value);
185       }
186       i += 3;
187
188       xbt_assert(number < param_size,
189                  "bad string parameter, not enough length param_size, "
190                  "at offset: %d (\"%s\") %d %d", i, value, param_size, number);
191
192       const char *param = param_list[number];
193       int j = 0;
194       while (param[j] && i_res < BUFFER_SIZE)
195         result[i_res++] = param[j++];
196     } else {
197       result[i_res++] = value[i++]; /* next char */
198     }
199     if (i_res >= BUFFER_SIZE)
200       xbt_die("solving string \"%s\", small buffer size (%d)",
201               value, BUFFER_SIZE);
202   }
203   result[i_res++] = '\0';
204   char *res = xbt_malloc(i_res);
205   return memcpy(res, result, i_res);
206 }
207
208 static void rulebased_get_route(AS_t rc,
209                                 const char *src, const char *dst,
210                                 route_t res);
211 static xbt_dynar_t rulebased_get_onelink_routes(AS_t rc)
212 {
213   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
214
215   //We have already bypass cluster routes with network NS3
216   if(!strcmp(surf_network_model->name,"network NS3"))
217         return ret;
218
219   AS_rulebased_t routing = (AS_rulebased_t)rc;
220
221   xbt_dict_cursor_t c1 = NULL;
222   char *k1, *d1;
223
224   //find router
225   char *router = NULL;
226   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
227     if (routing_get_network_element_type(k1) == SURF_NETWORK_ELEMENT_ROUTER){
228       router = k1;
229       break;
230     }
231   }
232
233   if (!router)
234     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
235
236   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
237     route_t route = xbt_new0(s_route_t,1);
238     route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
239     rulebased_get_route (rc, router, k1, route);
240
241     int number_of_links = xbt_dynar_length(route->link_list);
242
243     if(number_of_links == 1) {
244                 //loopback
245     }
246     else{
247                 if (number_of_links != 2) {
248                   xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
249                 }
250
251                 void *link_ptr;
252                 xbt_dynar_get_cpy (route->link_list, 1, &link_ptr);
253                 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
254                 onelink->src = xbt_strdup (k1);
255                 onelink->dst = xbt_strdup (router);
256                 onelink->link_ptr = link_ptr;
257                 xbt_dynar_push (ret, &onelink);
258     }
259   }
260   return ret;
261 }
262
263 /* Business methods */
264 static void rulebased_get_route(AS_t rc,
265                                 const char *src, const char *dst,
266                                 route_t route)
267 {
268   xbt_assert(rc && src
269               && dst,
270               "Invalid params for \"get_route\" function at AS \"%s\"",
271               rc->name);
272
273   /* set utils vars */
274   AS_rulebased_t routing = (AS_rulebased_t) rc;
275
276   int are_processing_units=0;
277   xbt_dynar_t rule_list;
278   if (xbt_dict_get_or_null(routing->dict_processing_units, src)
279       && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
280     are_processing_units = 1;
281     rule_list = routing->list_route;
282   } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
283              && xbt_dict_get_or_null(routing->dict_autonomous_systems,
284                                      dst)) {
285     are_processing_units = 0;
286     rule_list = routing->list_ASroute;
287   } else
288     THROWF(arg_error,0,"No route from '%s' to '%s'",src,dst);
289
290   int rc_src = -1;
291   int rc_dst = -1;
292   int src_length = (int) strlen(src);
293   int dst_length = (int) strlen(dst);
294
295   rule_route_t ruleroute;
296   unsigned int cpt;
297   int ovector_src[OVECCOUNT];
298   int ovector_dst[OVECCOUNT];
299   const char **list_src = NULL;
300   const char **list_dst = NULL;
301   _XBT_GNUC_UNUSED int res;
302   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
303     rc_src =
304         pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
305                   ovector_src, OVECCOUNT);
306     if (rc_src >= 0) {
307       rc_dst =
308           pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
309                     ovector_dst, OVECCOUNT);
310       if (rc_dst >= 0) {
311         res = pcre_get_substring_list(src, ovector_src, rc_src, &list_src);
312         xbt_assert(!res, "error solving substring list for src \"%s\"", src);
313         res = pcre_get_substring_list(dst, ovector_dst, rc_dst, &list_dst);
314         xbt_assert(!res, "error solving substring list for src \"%s\"", dst);
315         char *link_name;
316         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
317           char *new_link_name =
318               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
319           void *link =
320                           xbt_lib_get_or_null(link_lib, new_link_name, SURF_LINK_LEVEL);
321           if (link)
322             xbt_dynar_push(route->link_list, &link);
323           else
324             THROWF(mismatch_error, 0, "Link %s not found", new_link_name);
325           xbt_free(new_link_name);
326         }
327       }
328     }
329     if (rc_src >= 0 && rc_dst >= 0)
330       break;
331   }
332
333   if (rc_src >= 0 && rc_dst >= 0) {
334     /* matched src and dest, nothing more to do (?) */
335   } else if (!strcmp(src, dst) && are_processing_units) {
336     xbt_dynar_push(route->link_list, &(global_routing->loopback));
337   } else {
338     THROWF(arg_error,0,"No route from '%s' to '%s'??",src,dst);
339     //xbt_dynar_reset(route->link_list);
340   }
341
342   if (!are_processing_units && !xbt_dynar_is_empty(route->link_list)) {
343     rule_route_extended_t ruleroute_extended =
344         (rule_route_extended_t) ruleroute;
345     route->src_gateway =
346         remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
347                  list_dst, rc_dst);
348     route->dst_gateway =
349         remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
350                  list_dst, rc_dst);
351   }
352
353   if (list_src)
354     pcre_free_substring_list(list_src);
355   if (list_dst)
356     pcre_free_substring_list(list_dst);
357 }
358
359 static route_t rulebased_get_bypass_route(AS_t rc, const char *src, const char *dst) {
360   return NULL;
361 }
362
363 static void rulebased_finalize(AS_t rc)
364 {
365   AS_rulebased_t routing =
366       (AS_rulebased_t) rc;
367   if (routing) {
368     xbt_dict_free(&routing->dict_processing_units);
369     xbt_dict_free(&routing->dict_autonomous_systems);
370     xbt_dynar_free(&routing->list_route);
371     xbt_dynar_free(&routing->list_ASroute);
372     /* Delete structure */
373     xbt_free(routing);
374   }
375 }
376
377 /* Creation routing model functions */
378 AS_t model_rulebased_create(void) {
379
380   AS_rulebased_t new_component = (AS_rulebased_t)
381       model_generic_create_sized(sizeof(s_AS_rulebased_t));
382
383   new_component->generic_routing.parse_PU = model_rulebased_parse_PU;
384   new_component->generic_routing.parse_AS = model_rulebased_parse_AS;
385   new_component->generic_routing.parse_route = model_rulebased_parse_route;
386   new_component->generic_routing.parse_ASroute = model_rulebased_parse_ASroute;
387   new_component->generic_routing.parse_bypassroute = model_rulebased_parse_bypassroute;
388   new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
389   new_component->generic_routing.get_route = rulebased_get_route;
390   new_component->generic_routing.get_bypass_route = rulebased_get_bypass_route;
391   new_component->generic_routing.finalize = rulebased_finalize;
392   /* initialization of internal structures */
393   new_component->dict_processing_units = xbt_dict_new();
394   new_component->dict_autonomous_systems = xbt_dict_new();
395   new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
396   new_component->list_ASroute =
397       xbt_dynar_new(sizeof(rule_route_extended_t),
398                     &rule_route_extended_free);
399
400   return (AS_t) new_component;
401 }