Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement bypassRoute and bypassASroute. Add an example.
[simgrid.git] / src / surf / surf_routing_rulebased.c
index fad6426..2b2ab9a 100644 (file)
@@ -4,27 +4,20 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 #include "surf_routing_private.h"
-
-#ifdef HAVE_PCRE_LIB
 #include <pcre.h>               /* regular expression library */
 
 /* Global vars */
 extern routing_global_t global_routing;
-extern routing_component_t current_routing;
-extern model_type_t current_routing_model;
-extern xbt_dynar_t link_list;
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_rulebased, surf, "Routing part of surf");
 
 /* Routing model structure */
 
 typedef struct {
-  s_routing_component_t generic_routing;
-  xbt_dict_t dict_processing_units;
-  xbt_dict_t dict_autonomous_systems;
+  s_as_t generic_routing;
   xbt_dynar_t list_route;
   xbt_dynar_t list_ASroute;
-} s_routing_component_rulebased_t, *routing_component_rulebased_t;
+} s_AS_rulebased_t, *AS_rulebased_t;
 
 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
 typedef struct s_rule_route_extended s_rule_route_extended_t,
@@ -69,35 +62,31 @@ static void rule_route_extended_free(void *e)
 
 /* Parse routing model functions */
 
-static void model_rulebased_set_processing_unit(routing_component_t rc,
-                                                const char *name)
+static int model_rulebased_parse_PU(AS_t rc, network_element_t elm)
 {
-  routing_component_rulebased_t routing =
-      (routing_component_rulebased_t) rc;
-  xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
+  AS_rulebased_t routing = (AS_rulebased_t) rc;
+  xbt_dynar_push(routing->generic_routing.index_network_elm,(void *)elm);
+  return -1;
 }
 
-static void model_rulebased_set_autonomous_system(routing_component_t rc,
-                                                  const char *name)
+static int model_rulebased_parse_AS(AS_t rc, network_element_t elm)
 {
-  routing_component_rulebased_t routing =
-      (routing_component_rulebased_t) rc;
-  xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
-               NULL);
+  AS_rulebased_t routing = (AS_rulebased_t) rc;
+  xbt_dynar_push(routing->generic_routing.index_network_elm,(void *)elm);
+  return -1;
 }
 
-static void model_rulebased_set_route(routing_component_t rc,
+static void model_rulebased_parse_route(AS_t rc,
                                       const char *src, const char *dst,
-                                      name_route_extended_t route)
+                                      route_t route)
 {
-  routing_component_rulebased_t routing =
-      (routing_component_rulebased_t) rc;
+  AS_rulebased_t routing = (AS_rulebased_t) rc;
   rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
   const char *error;
   int erroffset;
 
-  if(!strcmp(rc->routing->name,"Vivaldi")){
-         if(xbt_dynar_length(route->generic_route.link_list) != 0)
+  if(!strcmp(rc->model_desc->name,"Vivaldi")){
+         if(!xbt_dynar_is_empty(route->link_list))
                  xbt_die("You can't have link_ctn with Model Vivaldi.");
   }
 
@@ -109,23 +98,24 @@ static void model_rulebased_set_route(routing_component_t rc,
   xbt_assert(ruleroute->re_src,
               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
               erroffset, dst, error);
-  ruleroute->re_str_link = route->generic_route.link_list;
+
+  ruleroute->re_str_link = route->link_list;
+  route->link_list = NULL; // Don't free it twice in each container
+
   xbt_dynar_push(routing->list_route, &ruleroute);
-  xbt_free(route);
 }
 
-static void model_rulebased_set_ASroute(routing_component_t rc,
+static void model_rulebased_parse_ASroute(AS_t rc,
                                         const char *src, const char *dst,
-                                        name_route_extended_t route)
+                                        route_t route)
 {
-  routing_component_rulebased_t routing =
-      (routing_component_rulebased_t) rc;
+  AS_rulebased_t routing = (AS_rulebased_t) rc;
   rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
   const char *error;
   int erroffset;
 
-  if(!strcmp(rc->routing->name,"Vivaldi")){
-         if(xbt_dynar_length(route->generic_route.link_list) != 0)
+  if(!strcmp(rc->model_desc->name,"Vivaldi")){
+         if(!xbt_dynar_is_empty(route->link_list))
                  xbt_die("You can't have link_ctn with Model Vivaldi.");
   }
 
@@ -140,19 +130,20 @@ static void model_rulebased_set_ASroute(routing_component_t rc,
               "PCRE compilation failed at offset %d (\"%s\"): %s\n",
               erroffset, dst, error);
   ruleroute_e->generic_rule_route.re_str_link =
-      route->generic_route.link_list;
-  ruleroute_e->re_src_gateway = route->src_gateway;
-  ruleroute_e->re_dst_gateway = route->dst_gateway;
+      route->link_list;
+  ruleroute_e->re_src_gateway = xbt_strdup((char *)route->src_gateway); // DIRTY HACK possible only
+  ruleroute_e->re_dst_gateway = xbt_strdup((char *)route->dst_gateway); // because of what is in routing_parse_E_ASroute
   xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
-//  xbt_free(route->src_gateway);
-//  xbt_free(route->dst_gateway);
-  xbt_free(route);
+
+  /* make sure that they don't get freed */
+  route->link_list = NULL;
+  route->src_gateway = route->dst_gateway = NULL;
 }
 
-static void model_rulebased_set_bypassroute(routing_component_t rc,
+static void model_rulebased_parse_bypassroute(AS_t rc,
                                             const char *src,
                                             const char *dst,
-                                            route_extended_t e_route)
+                                            route_t e_route)
 {
   xbt_die("bypass routing not supported for Route-Based model");
 }
@@ -181,7 +172,7 @@ static char *remplace(char *value, const char **src_list, int src_size,
 
       /* solve the indication */
       const char **param_list;
-      int param_size;
+      _XBT_GNUC_UNUSED int param_size;
       if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
         param_list = src_list;
         param_size = src_size;
@@ -215,38 +206,37 @@ static char *remplace(char *value, const char **src_list, int src_size,
   return memcpy(res, result, i_res);
 }
 
-static route_extended_t rulebased_get_route(routing_component_t rc,
-                                            const char *src,
-                                            const char *dst);
-static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
+static void rulebased_get_route_and_latency(AS_t rc,
+    network_element_t src, network_element_t dst,
+                                route_t res,double*lat);
+static xbt_dynar_t rulebased_get_onelink_routes(AS_t rc)
 {
   xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
-
   //We have already bypass cluster routes with network NS3
   if(!strcmp(surf_network_model->name,"network NS3"))
        return ret;
 
-  routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
-
-  xbt_dict_cursor_t c1 = NULL;
-  char *k1, *d1;
+  char *k1;
 
   //find router
-  char *router = NULL;
-  xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
-    if (rc->get_network_element_type(k1) == SURF_NETWORK_ELEMENT_ROUTER){
-      router = k1;
-    }
+  network_element_t router = NULL;
+  xbt_lib_cursor_t cursor;
+  xbt_lib_foreach(as_router_lib, cursor, k1, router)
+  {
+    if (router->rc_type == SURF_NETWORK_ELEMENT_ROUTER)
+      break;
   }
 
-  if (!router){
+  if (!router)
     xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
-  }
 
-  xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
-    route_extended_t route = rulebased_get_route (rc, router, k1);
+  network_element_t host = NULL;
+  xbt_lib_foreach(as_router_lib, cursor, k1, host){
+    route_t route = xbt_new0(s_route_t,1);
+    route->link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
+    rulebased_get_route_and_latency (rc, router, host, route,NULL);
 
-    int number_of_links = xbt_dynar_length(route->generic_route.link_list);
+    int number_of_links = xbt_dynar_length(route->link_list);
 
     if(number_of_links == 1) {
                //loopback
@@ -257,10 +247,10 @@ static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
                }
 
                void *link_ptr;
-               xbt_dynar_get_cpy (route->generic_route.link_list, 1, &link_ptr);
+               xbt_dynar_get_cpy (route->link_list, 1, &link_ptr);
                onelink_t onelink = xbt_new0 (s_onelink_t, 1);
-               onelink->src = xbt_strdup (k1);
-               onelink->dst = xbt_strdup (router);
+               onelink->src = host;
+               onelink->dst = router;
                onelink->link_ptr = link_ptr;
                xbt_dynar_push (ret, &onelink);
     }
@@ -269,41 +259,38 @@ static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
 }
 
 /* Business methods */
-static route_extended_t rulebased_get_route(routing_component_t rc,
-                                            const char *src,
-                                            const char *dst)
+static void rulebased_get_route_and_latency(AS_t rc,
+    network_element_t src, network_element_t dst,
+                                route_t route, double *lat)
 {
+  XBT_DEBUG("rulebased_get_route_and_latency from '%s' to '%s'",src->name,dst->name);
   xbt_assert(rc && src
               && dst,
               "Invalid params for \"get_route\" function at AS \"%s\"",
               rc->name);
 
   /* set utils vars */
-  routing_component_rulebased_t routing =
-      (routing_component_rulebased_t) rc;
+  AS_rulebased_t routing = (AS_rulebased_t) rc;
+
+  char* src_name = src->name;
+  char* dst_name = dst->name;
 
   int are_processing_units=0;
   xbt_dynar_t rule_list;
-  if (xbt_dict_get_or_null(routing->dict_processing_units, src)
-      && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
+  if ((src->rc_type == SURF_NETWORK_ELEMENT_HOST || src->rc_type == SURF_NETWORK_ELEMENT_ROUTER)&&
+      (dst->rc_type == SURF_NETWORK_ELEMENT_HOST || dst->rc_type == SURF_NETWORK_ELEMENT_ROUTER)){
     are_processing_units = 1;
     rule_list = routing->list_route;
-  } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
-             && xbt_dict_get_or_null(routing->dict_autonomous_systems,
-                                     dst)) {
+  } else if (src->rc_type == SURF_NETWORK_ELEMENT_AS && dst->rc_type == SURF_NETWORK_ELEMENT_AS) {
     are_processing_units = 0;
     rule_list = routing->list_ASroute;
   } else
-    xbt_die("Ask for route \"from\"(%s)  or \"to\"(%s) no found in "
-            "the local table", src, dst);
+    THROWF(arg_error,0,"No route from '%s' to '%s'",src_name,dst_name);
 
   int rc_src = -1;
   int rc_dst = -1;
-  int src_length = (int) strlen(src);
-  int dst_length = (int) strlen(dst);
-
-  xbt_dynar_t links_list =
-      xbt_dynar_new(global_routing->size_of_link, NULL);
+  int src_length = (int) strlen(src_name);
+  int dst_length = (int) strlen(dst_name);
 
   rule_route_t ruleroute;
   unsigned int cpt;
@@ -311,29 +298,31 @@ static route_extended_t rulebased_get_route(routing_component_t rc,
   int ovector_dst[OVECCOUNT];
   const char **list_src = NULL;
   const char **list_dst = NULL;
-  int res;
+  _XBT_GNUC_UNUSED int res;
   xbt_dynar_foreach(rule_list, cpt, ruleroute) {
     rc_src =
-        pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
+        pcre_exec(ruleroute->re_src, NULL, src_name, src_length, 0, 0,
                   ovector_src, OVECCOUNT);
     if (rc_src >= 0) {
       rc_dst =
-          pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
+          pcre_exec(ruleroute->re_dst, NULL, dst_name, dst_length, 0, 0,
                     ovector_dst, OVECCOUNT);
       if (rc_dst >= 0) {
-        res = pcre_get_substring_list(src, ovector_src, rc_src, &list_src);
-        xbt_assert(!res, "error solving substring list for src \"%s\"", src);
-        res = pcre_get_substring_list(dst, ovector_dst, rc_dst, &list_dst);
-        xbt_assert(!res, "error solving substring list for src \"%s\"", dst);
+        res = pcre_get_substring_list(src_name, ovector_src, rc_src, &list_src);
+        xbt_assert(!res, "error solving substring list for src \"%s\"", src_name);
+        res = pcre_get_substring_list(dst_name, ovector_dst, rc_dst, &list_dst);
+        xbt_assert(!res, "error solving substring list for dst \"%s\"", dst_name);
         char *link_name;
         xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
           char *new_link_name =
               remplace(link_name, list_src, rc_src, list_dst, rc_dst);
           void *link =
                          xbt_lib_get_or_null(link_lib, new_link_name, SURF_LINK_LEVEL);
-          if (link)
-            xbt_dynar_push(links_list, &link);
-          else
+          if (link) {
+            xbt_dynar_push(route->link_list, &link);
+            if (lat)
+              *lat += surf_network_model->extension.network.get_link_latency(link);
+          } else
             THROWF(mismatch_error, 0, "Link %s not found", new_link_name);
           xbt_free(new_link_name);
         }
@@ -343,98 +332,90 @@ static route_extended_t rulebased_get_route(routing_component_t rc,
       break;
   }
 
-  route_extended_t new_e_route = NULL;
   if (rc_src >= 0 && rc_dst >= 0) {
-    new_e_route = xbt_new0(s_route_extended_t, 1);
-    new_e_route->generic_route.link_list = links_list;
-  } else if (!strcmp(src, dst) && are_processing_units) {
-    new_e_route = xbt_new0(s_route_extended_t, 1);
-    xbt_dynar_push(links_list, &(global_routing->loopback));
-    new_e_route->generic_route.link_list = links_list;
+    /* matched src and dest, nothing more to do (?) */
+  } else if (!strcmp(src_name, dst_name) && are_processing_units) {
+    xbt_dynar_push(route->link_list, &(global_routing->loopback));
+    if (lat)
+      *lat += surf_network_model->extension.network.get_link_latency(global_routing->loopback);
   } else {
-    xbt_dynar_free(&link_list);
+    THROWF(arg_error,0,"No route from '%s' to '%s'??",src_name,dst_name);
+    //xbt_dynar_reset(route->link_list);
   }
 
-  if (!are_processing_units && new_e_route) {
+  if (!are_processing_units && !xbt_dynar_is_empty(route->link_list)) {
     rule_route_extended_t ruleroute_extended =
         (rule_route_extended_t) ruleroute;
-    new_e_route->src_gateway =
-        remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
-                 list_dst, rc_dst);
-    new_e_route->dst_gateway =
-        remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
-                 list_dst, rc_dst);
+    char *gw_src_name = remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
+        list_dst, rc_dst);
+    route->src_gateway = xbt_lib_get_or_null(host_lib, gw_src_name,
+                                             ROUTING_HOST_LEVEL);
+    route->src_gateway = xbt_lib_get_or_null(host_lib, gw_src_name,
+                                             ROUTING_HOST_LEVEL);
+    if (!route->src_gateway)
+      route->src_gateway = xbt_lib_get_or_null(as_router_lib, gw_src_name,
+                                               ROUTING_ASR_LEVEL);
+    if (!route->src_gateway)
+      route->src_gateway = xbt_lib_get_or_null(as_router_lib, gw_src_name,
+                                               ROUTING_ASR_LEVEL);
+    xbt_free(gw_src_name);
+
+    char *gw_dst_name = remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
+        list_dst, rc_dst);
+    route->dst_gateway = xbt_lib_get_or_null(host_lib, gw_dst_name,
+                                             ROUTING_HOST_LEVEL);
+    route->dst_gateway = xbt_lib_get_or_null(host_lib, gw_dst_name,
+                                             ROUTING_HOST_LEVEL);
+    if (!route->dst_gateway)
+      route->dst_gateway = xbt_lib_get_or_null(as_router_lib, gw_dst_name,
+                                               ROUTING_ASR_LEVEL);
+    if (!route->dst_gateway)
+      route->dst_gateway = xbt_lib_get_or_null(as_router_lib, gw_dst_name,
+                                               ROUTING_ASR_LEVEL);
+    xbt_free(gw_dst_name);
   }
 
   if (list_src)
     pcre_free_substring_list(list_src);
   if (list_dst)
     pcre_free_substring_list(list_dst);
-
-  return new_e_route;
 }
 
-static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
-                                                   const char *src,
-                                                   const char *dst)
-{
+static route_t rulebased_get_bypass_route(AS_t rc, network_element_t src, network_element_t dst, double *lat) {
   return NULL;
 }
 
-static void rulebased_finalize(routing_component_t rc)
+static void rulebased_finalize(AS_t rc)
 {
-  routing_component_rulebased_t routing =
-      (routing_component_rulebased_t) rc;
+  AS_rulebased_t routing =
+      (AS_rulebased_t) rc;
   if (routing) {
-    xbt_dict_free(&routing->dict_processing_units);
-    xbt_dict_free(&routing->dict_autonomous_systems);
     xbt_dynar_free(&routing->list_route);
     xbt_dynar_free(&routing->list_ASroute);
-    /* Delete structure */
-    xbt_free(routing);
+    model_generic_finalize(rc);
   }
 }
 
 /* Creation routing model functions */
-void *model_rulebased_create(void)
-{
-  routing_component_rulebased_t new_component =
-      xbt_new0(s_routing_component_rulebased_t, 1);
-  new_component->generic_routing.set_processing_unit =
-      model_rulebased_set_processing_unit;
-  new_component->generic_routing.set_autonomous_system =
-      model_rulebased_set_autonomous_system;
-  new_component->generic_routing.set_route = model_rulebased_set_route;
-  new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
-  new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
+AS_t model_rulebased_create(void) {
+
+  AS_rulebased_t new_component = (AS_rulebased_t)
+      model_generic_create_sized(sizeof(s_AS_rulebased_t));
+
+  new_component->generic_routing.parse_PU = model_rulebased_parse_PU;
+  new_component->generic_routing.parse_AS = model_rulebased_parse_AS;
+  new_component->generic_routing.parse_route = model_rulebased_parse_route;
+  new_component->generic_routing.parse_ASroute = model_rulebased_parse_ASroute;
+  new_component->generic_routing.parse_bypassroute = model_rulebased_parse_bypassroute;
   new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
-  new_component->generic_routing.get_route = rulebased_get_route;
-  new_component->generic_routing.get_latency = generic_get_link_latency;
+  new_component->generic_routing.get_route_and_latency = rulebased_get_route_and_latency;
   new_component->generic_routing.get_bypass_route = rulebased_get_bypass_route;
   new_component->generic_routing.finalize = rulebased_finalize;
-  new_component->generic_routing.get_network_element_type = get_network_element_type;
   /* initialization of internal structures */
-  new_component->dict_processing_units = xbt_dict_new();
-  new_component->dict_autonomous_systems = xbt_dict_new();
   new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
   new_component->list_ASroute =
       xbt_dynar_new(sizeof(rule_route_extended_t),
                     &rule_route_extended_free);
-  return new_component;
-}
 
-void model_rulebased_load(void)
-{
-  /* use "surfxml_add_callback" to add a parse function call */
+  return (AS_t) new_component;
 }
-
-void model_rulebased_unload(void)
-{
-  /* use "surfxml_del_callback" to remove a parse function call */
-}
-
-void model_rulebased_end(void)
-{
-}
-
-#endif                          /* HAVE_PCRE_LIB */