1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2 * All rights reserved. */
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 */
10 extern routing_global_t global_routing;
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_rulebased, surf, "Routing part of surf");
14 /* Routing model structure */
17 s_as_t generic_routing;
18 xbt_dict_t dict_processing_units;
19 xbt_dict_t dict_autonomous_systems;
20 xbt_dynar_t list_route;
21 xbt_dynar_t list_ASroute;
22 } s_AS_rulebased_t, *AS_rulebased_t;
24 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
25 typedef struct s_rule_route_extended s_rule_route_extended_t,
26 *rule_route_extended_t;
29 xbt_dynar_t re_str_link; // dynar of char*
34 struct s_rule_route_extended {
35 s_rule_route_t generic_rule_route;
40 static void rule_route_free(void *e)
42 rule_route_t *elem = (rule_route_t *) (e);
44 xbt_dynar_free(&(*elem)->re_str_link);
45 pcre_free((*elem)->re_src);
46 pcre_free((*elem)->re_dst);
52 static void rule_route_extended_free(void *e)
54 rule_route_extended_t *elem = (rule_route_extended_t *) e;
56 xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
57 pcre_free((*elem)->generic_rule_route.re_src);
58 pcre_free((*elem)->generic_rule_route.re_dst);
59 xbt_free((*elem)->re_src_gateway);
60 xbt_free((*elem)->re_dst_gateway);
65 /* Parse routing model functions */
67 static void model_rulebased_parse_PU(AS_t rc, const char *name)
69 AS_rulebased_t routing = (AS_rulebased_t) rc;
70 xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
73 static void model_rulebased_parse_AS(AS_t rc, const char *name)
75 AS_rulebased_t routing = (AS_rulebased_t) rc;
76 xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1), NULL);
79 static void model_rulebased_parse_route(AS_t rc,
80 const char *src, const char *dst,
83 AS_rulebased_t routing = (AS_rulebased_t) rc;
84 rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
88 if(!strcmp(rc->model_desc->name,"Vivaldi")){
89 if(!xbt_dynar_is_empty(route->link_list))
90 xbt_die("You can't have link_ctn with Model Vivaldi.");
93 ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
94 xbt_assert(ruleroute->re_src,
95 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
96 erroffset, src, error);
97 ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
98 xbt_assert(ruleroute->re_src,
99 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
100 erroffset, dst, error);
102 ruleroute->re_str_link = route->link_list;
103 route->link_list = NULL; // Don't free it twice in each container
105 xbt_dynar_push(routing->list_route, &ruleroute);
108 static void model_rulebased_parse_ASroute(AS_t rc,
109 const char *src, const char *dst,
112 AS_rulebased_t routing = (AS_rulebased_t) rc;
113 rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
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.");
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 =
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);
138 /* make sure that they don't get freed */
139 route->link_list = NULL;
140 route->src_gateway = route->dst_gateway = NULL;
143 static void model_rulebased_parse_bypassroute(AS_t rc,
148 xbt_die("bypass routing not supported for Route-Based model");
151 #define BUFFER_SIZE 4096 /* result buffer size */
152 #define OVECCOUNT 30 /* should be a multiple of 3 */
154 static char *remplace(char *value, const char **src_list, int src_size,
155 const char **dst_list, int dst_size)
157 char result[BUFFER_SIZE];
162 if (value[i] == '$') {
163 i++; /* skip the '$' */
164 if (value[i] < '0' || value[i] > '9')
165 xbt_die("bad string parameter, no number indication, at offset: "
166 "%d (\"%s\")", i, value);
168 /* solve the number */
169 int number = value[i++] - '0';
170 while (value[i] >= '0' && value[i] <= '9')
171 number = 10 * number + (value[i++] - '0');
173 /* solve the indication */
174 const char **param_list;
175 _XBT_GNUC_UNUSED int param_size;
176 if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
177 param_list = src_list;
178 param_size = src_size;
179 } else if (value[i] == 'd' && value[i + 1] == 's'
180 && value[i + 2] == 't') {
181 param_list = dst_list;
182 param_size = dst_size;
184 xbt_die("bad string parameter, support only \"src\" and \"dst\", "
185 "at offset: %d (\"%s\")", i, value);
189 xbt_assert(number < param_size,
190 "bad string parameter, not enough length param_size, "
191 "at offset: %d (\"%s\") %d %d", i, value, param_size, number);
193 const char *param = param_list[number];
195 while (param[j] && i_res < BUFFER_SIZE)
196 result[i_res++] = param[j++];
198 result[i_res++] = value[i++]; /* next char */
200 if (i_res >= BUFFER_SIZE)
201 xbt_die("solving string \"%s\", small buffer size (%d)",
204 result[i_res++] = '\0';
205 char *res = xbt_malloc(i_res);
206 return memcpy(res, result, i_res);
209 static void rulebased_get_route_and_latency(AS_t rc,
210 const char *src, const char *dst,
211 route_t res,double*lat);
212 static xbt_dynar_t rulebased_get_onelink_routes(AS_t rc)
214 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
216 //We have already bypass cluster routes with network NS3
217 if(!strcmp(surf_network_model->name,"network NS3"))
220 AS_rulebased_t routing = (AS_rulebased_t)rc;
222 xbt_dict_cursor_t c1 = NULL;
227 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
228 if (routing_get_network_element_type(k1) == SURF_NETWORK_ELEMENT_ROUTER){
235 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
237 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
238 route_t route = xbt_new0(s_route_t,1);
239 route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
240 rulebased_get_route_and_latency (rc, router, k1, route,NULL);
242 int number_of_links = xbt_dynar_length(route->link_list);
244 if(number_of_links == 1) {
248 if (number_of_links != 2) {
249 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
253 xbt_dynar_get_cpy (route->link_list, 1, &link_ptr);
254 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
255 onelink->src = xbt_strdup (k1);
256 onelink->dst = xbt_strdup (router);
257 onelink->link_ptr = link_ptr;
258 xbt_dynar_push (ret, &onelink);
264 /* Business methods */
265 static void rulebased_get_route_and_latency(AS_t rc,
266 const char *src, const char *dst,
267 route_t route, double *lat)
271 "Invalid params for \"get_route\" function at AS \"%s\"",
275 AS_rulebased_t routing = (AS_rulebased_t) rc;
277 int are_processing_units=0;
278 xbt_dynar_t rule_list;
279 if (xbt_dict_get_or_null(routing->dict_processing_units, src)
280 && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
281 are_processing_units = 1;
282 rule_list = routing->list_route;
283 } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
284 && xbt_dict_get_or_null(routing->dict_autonomous_systems,
286 are_processing_units = 0;
287 rule_list = routing->list_ASroute;
289 THROWF(arg_error,0,"No route from '%s' to '%s'",src,dst);
293 int src_length = (int) strlen(src);
294 int dst_length = (int) strlen(dst);
296 rule_route_t ruleroute;
298 int ovector_src[OVECCOUNT];
299 int ovector_dst[OVECCOUNT];
300 const char **list_src = NULL;
301 const char **list_dst = NULL;
302 _XBT_GNUC_UNUSED int res;
303 xbt_dynar_foreach(rule_list, cpt, ruleroute) {
305 pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
306 ovector_src, OVECCOUNT);
309 pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
310 ovector_dst, OVECCOUNT);
312 res = pcre_get_substring_list(src, ovector_src, rc_src, &list_src);
313 xbt_assert(!res, "error solving substring list for src \"%s\"", src);
314 res = pcre_get_substring_list(dst, ovector_dst, rc_dst, &list_dst);
315 xbt_assert(!res, "error solving substring list for src \"%s\"", dst);
317 xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
318 char *new_link_name =
319 remplace(link_name, list_src, rc_src, list_dst, rc_dst);
321 xbt_lib_get_or_null(link_lib, new_link_name, SURF_LINK_LEVEL);
323 xbt_dynar_push(route->link_list, &link);
325 *lat += surf_network_model->extension.network.get_link_latency(link);
327 THROWF(mismatch_error, 0, "Link %s not found", new_link_name);
328 xbt_free(new_link_name);
332 if (rc_src >= 0 && rc_dst >= 0)
336 if (rc_src >= 0 && rc_dst >= 0) {
337 /* matched src and dest, nothing more to do (?) */
338 } else if (!strcmp(src, dst) && are_processing_units) {
339 xbt_dynar_push(route->link_list, &(global_routing->loopback));
341 *lat += surf_network_model->extension.network.get_link_latency(global_routing->loopback);
343 THROWF(arg_error,0,"No route from '%s' to '%s'??",src,dst);
344 //xbt_dynar_reset(route->link_list);
347 if (!are_processing_units && !xbt_dynar_is_empty(route->link_list)) {
348 rule_route_extended_t ruleroute_extended =
349 (rule_route_extended_t) ruleroute;
351 remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
354 remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
359 pcre_free_substring_list(list_src);
361 pcre_free_substring_list(list_dst);
364 static route_t rulebased_get_bypass_route(AS_t rc, const char *src, const char *dst) {
368 static void rulebased_finalize(AS_t rc)
370 AS_rulebased_t routing =
373 xbt_dict_free(&routing->dict_processing_units);
374 xbt_dict_free(&routing->dict_autonomous_systems);
375 xbt_dynar_free(&routing->list_route);
376 xbt_dynar_free(&routing->list_ASroute);
378 model_generic_finalize(rc);
382 /* Creation routing model functions */
383 AS_t model_rulebased_create(void) {
385 AS_rulebased_t new_component = (AS_rulebased_t)
386 model_generic_create_sized(sizeof(s_AS_rulebased_t));
388 new_component->generic_routing.parse_PU = model_rulebased_parse_PU;
389 new_component->generic_routing.parse_AS = model_rulebased_parse_AS;
390 new_component->generic_routing.parse_route = model_rulebased_parse_route;
391 new_component->generic_routing.parse_ASroute = model_rulebased_parse_ASroute;
392 new_component->generic_routing.parse_bypassroute = model_rulebased_parse_bypassroute;
393 new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
394 new_component->generic_routing.get_route_and_latency = rulebased_get_route_and_latency;
395 new_component->generic_routing.get_bypass_route = rulebased_get_bypass_route;
396 new_component->generic_routing.finalize = rulebased_finalize;
397 /* initialization of internal structures */
398 new_component->dict_processing_units = xbt_dict_new_homogeneous(NULL);
399 new_component->dict_autonomous_systems = xbt_dict_new_homogeneous(NULL);
400 new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
401 new_component->list_ASroute =
402 xbt_dynar_new(sizeof(rule_route_extended_t),
403 &rule_route_extended_free);
405 return (AS_t) new_component;