Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Don't malloc data if mysmysocket->remaining equals 0, just return.
[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
225   //We have already bypass cluster routes with network NS3
226   if(!strcmp(surf_network_model->name,"network NS3"))
227         return ret;
228
229   routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
230
231   xbt_dict_cursor_t c1 = NULL;
232   char *k1, *d1;
233
234   //find router
235   char *router = NULL;
236   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
237     if (rc->get_network_element_type(k1) == SURF_NETWORK_ELEMENT_ROUTER){
238       router = k1;
239     }
240   }
241
242   if (!router){
243     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
244   }
245
246   xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
247     route_extended_t route = rulebased_get_route (rc, router, k1);
248
249     int number_of_links = xbt_dynar_length(route->generic_route.link_list);
250
251     if(number_of_links == 1) {
252                 //loopback
253     }
254     else{
255                 if (number_of_links != 2) {
256                   xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
257                 }
258
259                 void *link_ptr;
260                 xbt_dynar_get_cpy (route->generic_route.link_list, 1, &link_ptr);
261                 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
262                 onelink->src = xbt_strdup (k1);
263                 onelink->dst = xbt_strdup (router);
264                 onelink->link_ptr = link_ptr;
265                 xbt_dynar_push (ret, &onelink);
266     }
267   }
268   return ret;
269 }
270
271 /* Business methods */
272 static route_extended_t rulebased_get_route(routing_component_t rc,
273                                             const char *src,
274                                             const char *dst)
275 {
276   xbt_assert(rc && src
277               && dst,
278               "Invalid params for \"get_route\" function at AS \"%s\"",
279               rc->name);
280
281   /* set utils vars */
282   routing_component_rulebased_t routing =
283       (routing_component_rulebased_t) rc;
284
285   int are_processing_units=0;
286   xbt_dynar_t rule_list;
287   if (xbt_dict_get_or_null(routing->dict_processing_units, src)
288       && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
289     are_processing_units = 1;
290     rule_list = routing->list_route;
291   } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
292              && xbt_dict_get_or_null(routing->dict_autonomous_systems,
293                                      dst)) {
294     are_processing_units = 0;
295     rule_list = routing->list_ASroute;
296   } else
297     xbt_die("Ask for route \"from\"(%s)  or \"to\"(%s) no found in "
298             "the local table", src, dst);
299
300   int rc_src = -1;
301   int rc_dst = -1;
302   int src_length = (int) strlen(src);
303   int dst_length = (int) strlen(dst);
304
305   xbt_dynar_t links_list =
306       xbt_dynar_new(global_routing->size_of_link, NULL);
307
308   rule_route_t ruleroute;
309   unsigned int cpt;
310   int ovector_src[OVECCOUNT];
311   int ovector_dst[OVECCOUNT];
312   const char **list_src = NULL;
313   const char **list_dst = NULL;
314   int res;
315   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
316     rc_src =
317         pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
318                   ovector_src, OVECCOUNT);
319     if (rc_src >= 0) {
320       rc_dst =
321           pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
322                     ovector_dst, OVECCOUNT);
323       if (rc_dst >= 0) {
324         res = pcre_get_substring_list(src, ovector_src, rc_src, &list_src);
325         xbt_assert(!res, "error solving substring list for src \"%s\"", src);
326         res = pcre_get_substring_list(dst, ovector_dst, rc_dst, &list_dst);
327         xbt_assert(!res, "error solving substring list for src \"%s\"", dst);
328         char *link_name;
329         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
330           char *new_link_name =
331               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
332           void *link =
333                           xbt_lib_get_or_null(link_lib, new_link_name, SURF_LINK_LEVEL);
334           if (link)
335             xbt_dynar_push(links_list, &link);
336           else
337             THROWF(mismatch_error, 0, "Link %s not found", new_link_name);
338           xbt_free(new_link_name);
339         }
340       }
341     }
342     if (rc_src >= 0 && rc_dst >= 0)
343       break;
344   }
345
346   route_extended_t new_e_route = NULL;
347   if (rc_src >= 0 && rc_dst >= 0) {
348     new_e_route = xbt_new0(s_route_extended_t, 1);
349     new_e_route->generic_route.link_list = links_list;
350   } else if (!strcmp(src, dst) && are_processing_units) {
351     new_e_route = xbt_new0(s_route_extended_t, 1);
352     xbt_dynar_push(links_list, &(global_routing->loopback));
353     new_e_route->generic_route.link_list = links_list;
354   } else {
355     xbt_dynar_free(&link_list);
356   }
357
358   if (!are_processing_units && new_e_route) {
359     rule_route_extended_t ruleroute_extended =
360         (rule_route_extended_t) ruleroute;
361     new_e_route->src_gateway =
362         remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
363                  list_dst, rc_dst);
364     new_e_route->dst_gateway =
365         remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
366                  list_dst, rc_dst);
367   }
368
369   if (list_src)
370     pcre_free_substring_list(list_src);
371   if (list_dst)
372     pcre_free_substring_list(list_dst);
373
374   return new_e_route;
375 }
376
377 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
378                                                    const char *src,
379                                                    const char *dst)
380 {
381   return NULL;
382 }
383
384 static void rulebased_finalize(routing_component_t rc)
385 {
386   routing_component_rulebased_t routing =
387       (routing_component_rulebased_t) rc;
388   if (routing) {
389     xbt_dict_free(&routing->dict_processing_units);
390     xbt_dict_free(&routing->dict_autonomous_systems);
391     xbt_dynar_free(&routing->list_route);
392     xbt_dynar_free(&routing->list_ASroute);
393     /* Delete structure */
394     xbt_free(routing);
395   }
396 }
397
398 /* Creation routing model functions */
399 void *model_rulebased_create(void)
400 {
401   routing_component_rulebased_t new_component =
402       xbt_new0(s_routing_component_rulebased_t, 1);
403   new_component->generic_routing.set_processing_unit =
404       model_rulebased_set_processing_unit;
405   new_component->generic_routing.set_autonomous_system =
406       model_rulebased_set_autonomous_system;
407   new_component->generic_routing.set_route = model_rulebased_set_route;
408   new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
409   new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
410   new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
411   new_component->generic_routing.get_route = rulebased_get_route;
412   new_component->generic_routing.get_latency = generic_get_link_latency;
413   new_component->generic_routing.get_bypass_route = rulebased_get_bypass_route;
414   new_component->generic_routing.finalize = rulebased_finalize;
415   new_component->generic_routing.get_network_element_type = get_network_element_type;
416   /* initialization of internal structures */
417   new_component->dict_processing_units = xbt_dict_new();
418   new_component->dict_autonomous_systems = xbt_dict_new();
419   new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
420   new_component->list_ASroute =
421       xbt_dynar_new(sizeof(rule_route_extended_t),
422                     &rule_route_extended_free);
423   return new_component;
424 }
425
426 void model_rulebased_load(void)
427 {
428   /* use "surfxml_add_callback" to add a parse function call */
429 }
430
431 void model_rulebased_unload(void)
432 {
433   /* use "surfxml_del_callback" to remove a parse function call */
434 }
435
436 void model_rulebased_end(void)
437 {
438 }
439
440 #endif                          /* HAVE_PCRE_LIB */