[Olsr-dev] [PATCH v2 1/1] smart gateway: add threshold configuration parameter

Ferry Huberts (spam-protected)
Mon Dec 12 13:01:43 CET 2011


From: Ferry Huberts <(spam-protected)>

We can now keep choosing a better gateway by setting the
configuration setting "SmartGatewayThreshold" to a value
larger than zero.

The costs for a new smart gateway must be less than the
costs of the current smart gateway multiplied by
SmartGatewayThreshold (its default is 1.0) in order for
it to be chosen as the new smart gateway.

This new configuration parameter has a similar function
and meaning as the NAT threshold configuration parameter.

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 Makefile.inc                        |    1 +
 README-Olsr-Extensions              |    1 +
 files/olsrd.conf.default.full       |    7 +++++++
 files/olsrd.conf.default.lq         |    7 +++++++
 files/olsrd.conf.default.lq-fisheye |    7 +++++++
 src/cfgparser/cfgfile_gen.c         |   10 ++++++++++
 src/cfgparser/olsrd_conf.c          |    8 ++++++++
 src/cfgparser/oparse.y              |   10 ++++++++++
 src/cfgparser/oscan.lex             |    5 +++++
 src/gateway_default_handler.c       |   13 ++++++++-----
 src/olsr_cfg.h                      |    2 ++
 11 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/Makefile.inc b/Makefile.inc
index a6da618..96fe94e 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -159,6 +159,7 @@ ifneq ($(MUDFLAP),0)
 LIBS +=		-lmudflapth
 endif
 LIBS +=		$(OS_LIB_PTHREAD)
+LIBS +=		-lm
 
 # extra options from the outside
 CPPFLAGS +=	$(EXTRA_CPPFLAGS)
diff --git a/README-Olsr-Extensions b/README-Olsr-Extensions
index f7b9e33..34292b8 100644
--- a/README-Olsr-Extensions
+++ b/README-Olsr-Extensions
@@ -268,6 +268,7 @@ set up the type of the uplink:
    translation. The maximum prefix length is 64 bits,
    the default is ::/0 (no prefix).
 
+SmartGatewayThreshold <yes/no>
 SmartGatewayUplink <none/ipv4/ipv6/both>
 SmartGatewayUplinkNAT <yes/no>
 SmartGatewaySpeed <uplink> <downlink>
diff --git a/files/olsrd.conf.default.full b/files/olsrd.conf.default.full
index 83e614b..3d89b4c 100644
--- a/files/olsrd.conf.default.full
+++ b/files/olsrd.conf.default.full
@@ -149,6 +149,13 @@
 
 # SmartGatewayAllowNAT yes
 
+# When another gateway than the current one has a cost of less than the cost
+# of the current gateway multiplied by SmartGatewayThreshold then the smart
+# gateway is switched to the other gateway.
+# (defaults to 0.0; no switching)
+
+# SmartGatewayThreshold 0.0
+
 # Defines what kind of Uplink this node will publish as a
 # smartgateway. The existence of the uplink is detected by
 # a route to 0.0.0.0/0, ::ffff:0:0/96 and/or 2000::/3.
diff --git a/files/olsrd.conf.default.lq b/files/olsrd.conf.default.lq
index 466f001..b7442b8 100644
--- a/files/olsrd.conf.default.lq
+++ b/files/olsrd.conf.default.lq
@@ -86,6 +86,13 @@
 
 # SmartGatewayAllowNAT yes
 
+# When another gateway than the current one has a cost of less than the cost
+# of the current gateway multiplied by SmartGatewayThreshold then the smart
+# gateway is switched to the other gateway.
+# (defaults to 0.0; no switching)
+
+# SmartGatewayThreshold 0.0
+
 # Defines what kind of Uplink this node will publish as a
 # smartgateway. The existence of the uplink is detected by
 # a route to 0.0.0.0/0, ::ffff:0:0/96 and/or 2000::/3.
diff --git a/files/olsrd.conf.default.lq-fisheye b/files/olsrd.conf.default.lq-fisheye
index ce5d8e5..a376557 100644
--- a/files/olsrd.conf.default.lq-fisheye
+++ b/files/olsrd.conf.default.lq-fisheye
@@ -86,6 +86,13 @@
 
 # SmartGatewayAllowNAT yes
 
+# When another gateway than the current one has a cost of less than the cost
+# of the current gateway multiplied by SmartGatewayThreshold then the smart
+# gateway is switched to the other gateway.
+# (defaults to 0.0; no switching)
+
+# SmartGatewayThreshold 0.0
+
 # Defines what kind of Uplink this node will publish as a
 # smartgateway. The existence of the uplink is detected by
 # a route to 0.0.0.0/0, ::ffff:0:0/96 and/or 2000::/3.
diff --git a/src/cfgparser/cfgfile_gen.c b/src/cfgparser/cfgfile_gen.c
index fc4ba12..eadf914 100644
--- a/src/cfgparser/cfgfile_gen.c
+++ b/src/cfgparser/cfgfile_gen.c
@@ -446,6 +446,16 @@ void olsrd_write_cnf_autobuf(struct autobuf *out, struct olsrd_config *cnf) {
       cnf->smart_gw_allow_nat ? "yes" : "no");
   abuf_puts(out,
     "\n"
+    "# When another gateway than the current one has a cost of less than the cost\n"
+    "# of the current gateway multiplied by SmartGatewayThreshold then the smart\n"
+    "# gateway is switched to the other gateway.\n"
+    "# (defaults to 0.0; no switching)\n"
+    "\n");
+  abuf_appendf(out, "%sSmartGatewayThreshold  %.1f\n",
+      cnf->smart_gw_thresh == DEF_GW_THRESH ? "# " : "",
+      cnf->smart_gw_thresh);
+  abuf_puts(out,
+    "\n"
     "# Defines what kind of Uplink this node will publish as a\n"
     "# smartgateway. The existence of the uplink is detected by\n"
     "# a route to 0.0.0.0/0, ::ffff:0:0/96 and/or 2000::/3.\n"
diff --git a/src/cfgparser/olsrd_conf.c b/src/cfgparser/olsrd_conf.c
index 95690e7..76a1780 100644
--- a/src/cfgparser/olsrd_conf.c
+++ b/src/cfgparser/olsrd_conf.c
@@ -549,6 +549,11 @@ olsrd_sanity_check_cnf(struct olsrd_config *cnf)
 	  fprintf(stderr, "Warning, you are using the min_tc_vtime hack. We hope you know what you are doing... contact olsr.org otherwise.\n");
   }
 
+  if (((cnf->smart_gw_thresh < 0.1) || (cnf->smart_gw_thresh > 1.0)) && (cnf->smart_gw_thresh != 0.0)) {
+    fprintf(stderr, "Smart gateway threshold %f is not allowed\n", cnf->smart_gw_thresh);
+    return -1;
+  }
+
   if (cnf->smart_gw_type >= GW_UPLINK_CNT) {
     fprintf(stderr, "Error, illegal gateway uplink type: %d\n", cnf->smart_gw_type);
     return -1;
@@ -758,6 +763,7 @@ set_default_cnf(struct olsrd_config *cnf)
 
   cnf->smart_gw_active = DEF_SMART_GW;
   cnf->smart_gw_allow_nat = DEF_GW_ALLOW_NAT;
+  cnf->smart_gw_thresh = DEF_GW_THRESH;
   cnf->smart_gw_type = DEF_GW_TYPE;
   cnf->smart_gw_uplink = DEF_UPLINK_SPEED;
   cnf->smart_gw_uplink_nat = DEF_GW_UPLINK_NAT;
@@ -875,6 +881,8 @@ olsrd_print_cnf(struct olsrd_config *cnf)
 
   printf("SmGw. Allow NAT  : %s\n", cnf->smart_gw_allow_nat ? "yes" : "no");
 
+  printf("SmGw. threshold  : %f\n", cnf->smart_gw_thresh);
+
   printf("Smart Gw. Uplink : %s\n", GW_UPLINK_TXT[cnf->smart_gw_type]);
 
   printf("SmGw. Uplink NAT : %s\n", cnf->smart_gw_uplink_nat ? "yes" : "no");
diff --git a/src/cfgparser/oparse.y b/src/cfgparser/oparse.y
index 24c9a41..d296b63 100644
--- a/src/cfgparser/oparse.y
+++ b/src/cfgparser/oparse.y
@@ -216,6 +216,7 @@ static int add_ipv6_addr(YYSTYPE ipaddr_arg, YYSTYPE prefixlen_arg)
 %token TOK_USE_NIIT
 %token TOK_SMART_GW
 %token TOK_SMART_GW_ALLOW_NAT
+%token TOK_SMART_GW_THRESH
 %token TOK_SMART_GW_UPLINK
 %token TOK_SMART_GW_UPLINK_NAT
 %token TOK_SMART_GW_SPEED
@@ -295,6 +296,7 @@ stmt:       idebug
           | suse_niit
           | bsmart_gw
           | bsmart_gw_allow_nat
+          | asmart_gw_thresh
           | ssmart_gw_uplink
           | bsmart_gw_uplink_nat
           | ismart_gw_speed
@@ -1286,6 +1288,14 @@ bsmart_gw_allow_nat: TOK_SMART_GW_ALLOW_NAT TOK_BOOLEAN
 }
 ;
 
+asmart_gw_thresh: TOK_SMART_GW_THRESH TOK_FLOAT
+{
+  PARSER_DEBUG_PRINTF("Smart gateway threshold: %0.2f\n", $2->floating);
+  olsr_cnf->smart_gw_thresh = $2->floating;
+  free($2);
+}
+;
+
 ssmart_gw_uplink: TOK_SMART_GW_UPLINK TOK_STRING
 {
 	PARSER_DEBUG_PRINTF("Smart gateway uplink: %s\n", $2->string);
diff --git a/src/cfgparser/oscan.lex b/src/cfgparser/oscan.lex
index 25a4ecb..785a118 100644
--- a/src/cfgparser/oscan.lex
+++ b/src/cfgparser/oscan.lex
@@ -471,6 +471,11 @@ IPV6ADDR {IPV6PAT1}|{IPV6PAT2}|{IPV6PAT3}|{IPV6PAT4}|{IPV6PAT5}|{IPV6PAT6}|{IPV6
     return TOK_SMART_GW_ALLOW_NAT;
 }
 
+"SmartGatewaySelectOnce" {
+    yylval = NULL;
+    return TOK_SMART_GW_THRESH;
+}
+
 "SmartGatewayUplink" {
     yylval = NULL;
     return TOK_SMART_GW_UPLINK;
diff --git a/src/gateway_default_handler.c b/src/gateway_default_handler.c
index a18dba8..0a83d51 100644
--- a/src/gateway_default_handler.c
+++ b/src/gateway_default_handler.c
@@ -14,6 +14,7 @@
 #include "lq_plugin.h"
 
 #include "assert.h"
+#include <math.h>
 
 #ifdef LINUX_NETLINK_ROUTING
 static uint32_t gw_def_nodecount, gw_def_stablecount;
@@ -43,6 +44,7 @@ static void gw_default_choose_gateway(void) {
   olsr_linkcost cost_ipv4, cost_ipv6;
   struct gateway_entry *gw;
   bool dual;
+  olsr_linkcost path_cost_times_threshold;
 
   cost_ipv4 = ROUTE_COST_BROKEN;
   cost_ipv6 = ROUTE_COST_BROKEN;
@@ -56,13 +58,14 @@ static void gw_default_choose_gateway(void) {
       continue;
     }
 
-    if (!gw_def_finished_ipv4 && gw->ipv4 && gw->ipv4nat == olsr_cnf->smart_gw_allow_nat && tc->path_cost < cost_ipv4) {
+    path_cost_times_threshold = (olsr_linkcost)(((float)tc->path_cost * olsr_cnf->smart_gw_thresh) + 0.5f);
+    if (!gw_def_finished_ipv4 && gw->ipv4 && gw->ipv4nat == olsr_cnf->smart_gw_allow_nat && path_cost_times_threshold < cost_ipv4) {
       inet_ipv4 = gw;
-      cost_ipv4 = tc->path_cost;
+      cost_ipv4 = path_cost_times_threshold;
     }
-    if (!gw_def_finished_ipv6 && gw->ipv6 && tc->path_cost < cost_ipv6) {
+    if (!gw_def_finished_ipv6 && gw->ipv6 && path_cost_times_threshold < cost_ipv6) {
       inet_ipv6 = gw;
-      cost_ipv6 = tc->path_cost;
+      cost_ipv6 = path_cost_times_threshold;
     }
   } OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
 
@@ -78,7 +81,7 @@ static void gw_default_choose_gateway(void) {
   }
 
   /* finished ? */
-  if (gw_def_finished_ipv4 && gw_def_finished_ipv6) {
+  if ((olsr_cnf->smart_gw_thresh == 0.0f) && gw_def_finished_ipv4 && gw_def_finished_ipv6) {
     olsr_stop_timer(gw_def_timer);
     gw_def_timer = NULL;
   }
diff --git a/src/olsr_cfg.h b/src/olsr_cfg.h
index b944706..4402e07 100644
--- a/src/olsr_cfg.h
+++ b/src/olsr_cfg.h
@@ -79,6 +79,7 @@
 #define DEF_USE_NIIT         true
 #define DEF_SMART_GW         false
 #define DEF_GW_ALLOW_NAT     true
+#define DEF_GW_THRESH        0.0
 #define DEF_GW_TYPE          GW_UPLINK_IPV46
 #define DEF_GW_UPLINK_NAT    true
 #define DEF_UPLINK_SPEED     128
@@ -255,6 +256,7 @@ struct olsrd_config {
   bool use_niit;
 
   bool smart_gw_active, smart_gw_allow_nat, smart_gw_uplink_nat;
+  float smart_gw_thresh;
   enum smart_gw_uplinktype smart_gw_type;
   uint32_t smart_gw_uplink, smart_gw_downlink;
   struct olsr_ip_prefix smart_gw_prefix;
-- 
1.7.7.4





More information about the Olsr-dev mailing list