When there is no incoming data traffic in the tunnel, BFD decay allows the bfd
session to increase the min_rx. This is helpful in that usually some tunnels
are rarely used. And cpu consumption can be reduced by processing fewer bfd
control packets.

Signed-off-by: Alex Wang <[email protected]>
---
 lib/bfd.c              |   89 ++++++++++++++++++++++++++++++++++++++++++++++--
 lib/bfd.h              |    6 +++-
 ofproto/ofproto-dpif.c |    3 +-
 vswitchd/vswitch.xml   |    7 ++++
 4 files changed, 100 insertions(+), 5 deletions(-)

diff --git a/lib/bfd.c b/lib/bfd.c
index aa1a3f7..4a6a0a6 100644
--- a/lib/bfd.c
+++ b/lib/bfd.c
@@ -152,6 +152,9 @@ struct bfd {
     bool cpath_down;              /* Concatenated Path Down. */
     uint8_t mult;                 /* bfd.DetectMult. */
 
+    struct netdev *netdev;
+    uint64_t rx_packets;          /* Packets received by 'netdev'. */
+
     enum state state;             /* bfd.SessionState. */
     enum state rmt_state;         /* bfd.RemoteSessionState. */
 
@@ -182,6 +185,13 @@ struct bfd {
 
     int ref_cnt;
     int forwarding_override;      /* Manual override of 'forwarding' status. */
+
+    /* BFD decay related variables. */
+    bool decay_enabled;           /* Flag that enables bfd decay. */
+    int decay_count;              /* Counter of bfd control packets, */
+                                  /* between decay detect interval. */
+    int decay_interval;
+    long long int decay_detect_time;
 };
 
 static bool bfd_in_poll(const struct bfd *);
@@ -191,6 +201,9 @@ static const char *bfd_state_str(enum state);
 static long long int bfd_min_tx(const struct bfd *);
 static long long int bfd_tx_interval(const struct bfd *);
 static long long int bfd_rx_interval(const struct bfd *);
+static void bfd_decay_compute_interval(struct bfd *);
+static uint64_t bfd_rx_packets(struct bfd *);
+static void bfd_decay(struct bfd *);
 static void bfd_set_next_tx(struct bfd *);
 static void bfd_set_state(struct bfd *, enum state, enum diag);
 static uint32_t generate_discriminator(void);
@@ -243,7 +256,8 @@ bfd_get_status(const struct bfd *bfd, struct smap *smap)
  * Also returns NULL if cfg is NULL. */
 struct bfd *
 bfd_configure(struct bfd *bfd, const char *name,
-              const struct smap *cfg)
+              const struct smap *cfg,
+              struct netdev *netdev)
 {
     static uint16_t udp_src = 0;
     static bool init = false;
@@ -276,6 +290,11 @@ bfd_configure(struct bfd *bfd, const char *name,
         bfd->min_tx = 1000;
         bfd->mult = 3;
         bfd->ref_cnt = 1;
+        bfd->netdev = netdev;
+        bfd->rx_packets = 0;
+        bfd->decay_count = 0;
+        bfd->decay_interval = 0;
+        bfd->decay_detect_time = 0;
 
         /* RFC 5881 section 4
          * The source port MUST be in the range 49152 through 65535.  The same
@@ -309,6 +328,11 @@ bfd_configure(struct bfd *bfd, const char *name,
         bfd_poll(bfd);
     }
 
+    bfd->decay_enabled = smap_get_bool(cfg, "decay_enable", false);
+    if (bfd->decay_enabled) {
+        bfd_decay_compute_interval(bfd);
+    }
+
     cpath_down = smap_get_bool(cfg, "cpath_down", false);
     if (bfd->cpath_down != cpath_down) {
         bfd->cpath_down = cpath_down;
@@ -360,11 +384,19 @@ bfd_wait(const struct bfd *bfd)
 void
 bfd_run(struct bfd *bfd)
 {
-    if (bfd->state > STATE_DOWN && time_msec() >= bfd->detect_time) {
+    long long int cur_time = time_msec();
+
+    if (bfd->state > STATE_DOWN && cur_time >= bfd->detect_time) {
         bfd_set_state(bfd, STATE_DOWN, DIAG_EXPIRED);
     }
 
-    if (bfd->min_tx != bfd->cfg_min_tx || bfd->min_rx != bfd->cfg_min_rx) {
+    if (bfd->state > STATE_DOWN && bfd->decay_enabled
+        && cur_time >= bfd->decay_detect_time) {
+        bfd_decay(bfd);
+    }
+
+    if (!bfd->decay_enabled &&
+        (bfd->min_tx != bfd->cfg_min_tx || bfd->min_rx != bfd->cfg_min_rx)) {
         bfd_poll(bfd);
     }
 }
@@ -497,6 +529,10 @@ bfd_process_packet(struct bfd *bfd, const struct flow 
*flow,
 
     log_msg(VLL_DBG, msg, "Received BFD control message", bfd);
 
+    if (bfd->decay_enabled) {
+        bfd->decay_count++;
+    }
+
     if (version != BFD_VERSION) {
         log_msg(VLL_WARN, msg, "Incorrect version", bfd);
         return;
@@ -804,6 +840,53 @@ bfd_set_state(struct bfd *bfd, enum state state, enum diag 
diag)
     }
 }
 
+/* Compute the decay interval based on the configured min_rx.
+ * This function should be invoked only by bfd_configure(). */
+static void
+bfd_decay_compute_interval(struct bfd *bfd)
+{
+    bfd->decay_interval = bfd->cfg_min_rx * bfd->mult * bfd->mult * bfd->mult;
+    bfd->decay_detect_time = time_msec() + bfd->decay_interval;
+}
+
+static uint64_t
+bfd_rx_packets(struct bfd *bfd)
+{
+    struct netdev_stats stats;
+
+    if (bfd->netdev && !netdev_get_stats(bfd->netdev, &stats)) {
+        return stats.rx_packets;
+    } else {
+        return 0;
+    }
+}
+
+static void
+bfd_decay(struct bfd *bfd)
+{
+    uint64_t rx_packets = bfd_rx_packets(bfd);
+    int64_t diff;
+
+    diff = abs(rx_packets - bfd->rx_packets - bfd->decay_count);
+
+    if (diff <= (4000 / bfd->min_rx + 1)) {
+        /* Decay. */
+        if (bfd->min_rx != bfd->decay_interval) {
+            bfd->min_rx += bfd->cfg_min_rx * bfd->mult;
+            if (bfd->min_rx > bfd->decay_interval) {
+                bfd->min_rx = bfd->decay_interval;
+            }
+        }
+    } else {
+        /* Restore the min_rx. */
+        bfd->min_rx = bfd->cfg_min_rx;
+    }
+
+    bfd->decay_count = 0;
+    bfd->rx_packets = rx_packets;
+    bfd->decay_detect_time = bfd->decay_interval + time_msec();
+}
+
 static uint32_t
 generate_discriminator(void)
 {
diff --git a/lib/bfd.h b/lib/bfd.h
index ab854d8..6e90cc3 100644
--- a/lib/bfd.h
+++ b/lib/bfd.h
@@ -18,9 +18,12 @@
 #define BFD_PACKET_LEN 24
 #define BFD_DEST_PORT 3784
 
+#include <stdlib.h>
 #include <stdbool.h>
 #include <inttypes.h>
 
+#include "netdev.h"
+
 struct bfd;
 struct flow;
 struct flow_wildcards;
@@ -39,7 +42,8 @@ void bfd_process_packet(struct bfd *, const struct flow *,
                         const struct ofpbuf *);
 
 struct bfd *bfd_configure(struct bfd *, const char *name,
-                          const struct smap *smap);
+                          const struct smap *smap,
+                          struct netdev *netdev);
 struct bfd *bfd_ref(const struct bfd *);
 void bfd_unref(struct bfd *);
 
diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c
index 9ba7671..c9bd19a 100644
--- a/ofproto/ofproto-dpif.c
+++ b/ofproto/ofproto-dpif.c
@@ -1882,7 +1882,8 @@ set_bfd(struct ofport *ofport_, const struct smap *cfg)
     struct bfd *old;
 
     old = ofport->bfd;
-    ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev), cfg);
+    ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
+                                cfg, ofport->up.netdev);
     if (ofport->bfd != old) {
         ofproto->backer->need_revalidate = REV_RECONFIGURE;
     }
diff --git a/vswitchd/vswitch.xml b/vswitchd/vswitch.xml
index 6c08f70..846fc38 100644
--- a/vswitchd/vswitch.xml
+++ b/vswitchd/vswitch.xml
@@ -1880,6 +1880,13 @@
           specified.  Defaults to <code>100</code>.
       </column>
 
+      <column name="bfd" key="decay_enable" type='{"type": "boolean"}'>
+          Allow the BFD session decay the min_rx when there is no incoming data
+          traffic in the tunnel. This is helpful in that usually some tunnels
+          are rarely used. And decaying the min_rx can reduce the number of BFD
+          control packets processed and the cpu consumption.
+      </column>
+
       <column name="bfd" key="cpath_down" type='{"type": "boolean"}'>
           Concatenated path down may be used when the local system should not
           have traffic forwarded to it for some reason other than a connectivty
-- 
1.7.9.5

_______________________________________________
dev mailing list
[email protected]
http://openvswitch.org/mailman/listinfo/dev

Reply via email to