Logo AND Algorithmique Numérique Distribuée

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