bfd: Require bfd control packet received in forwarding_if_rx mode.
[sliver-openvswitch.git] / lib / bfd.c
index 7da6fd9..2d53bd2 100644 (file)
--- a/lib/bfd.c
+++ b/lib/bfd.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013 Nicira, Inc.
+/* Copyright (c) 2013, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
 #include <netinet/ip.h>
 
 #include "byte-order.h"
+#include "connectivity.h"
 #include "csum.h"
 #include "dpif.h"
 #include "dynamic-string.h"
@@ -29,7 +30,6 @@
 #include "hmap.h"
 #include "list.h"
 #include "netdev.h"
-#include "netlink.h"
 #include "odp-util.h"
 #include "ofpbuf.h"
 #include "ovs-thread.h"
 #include "packets.h"
 #include "poll-loop.h"
 #include "random.h"
+#include "seq.h"
 #include "smap.h"
 #include "timeval.h"
+#include "unaligned.h"
 #include "unixctl.h"
 #include "util.h"
 #include "vlog.h"
@@ -169,6 +171,9 @@ struct bfd {
     uint8_t eth_dst[ETH_ADDR_LEN];/* Ethernet destination address. */
     bool eth_dst_set;             /* 'eth_dst' set through database. */
 
+    ovs_be32 ip_src;              /* IPv4 source address. */
+    ovs_be32 ip_dst;              /* IPv4 destination address. */
+
     uint16_t udp_src;             /* UDP source port. */
 
     /* All timers in milliseconds. */
@@ -186,10 +191,11 @@ struct bfd {
     long long int next_tx;        /* Next TX time. */
     long long int detect_time;    /* RFC 5880 6.8.4 Detection time. */
 
+    bool last_forwarding;         /* Last calculation of forwarding flag. */
     int forwarding_override;      /* Manual override of 'forwarding' status. */
 
     atomic_bool check_tnl_key;    /* Verify tunnel key of inbound packets? */
-    atomic_int ref_cnt;
+    struct ovs_refcount ref_cnt;
 
     /* When forward_if_rx is true, bfd_forwarding() will return
      * true as long as there are incoming packets received.
@@ -197,6 +203,12 @@ struct bfd {
     bool forwarding_if_rx;
     long long int forwarding_if_rx_detect_time;
 
+    /* When 'bfd->forwarding_if_rx' is set, at least one bfd control packet
+     * is required to be received every 100 * bfd->cfg_min_rx.  If bfd
+     * control packet is not received within this interval, even if data
+     * packets are received, the bfd->forwarding will still be false. */
+    long long int demand_rx_bfd_time;
+
     /* BFD decay related variables. */
     bool in_decay;                /* True when bfd is in decay. */
     int decay_min_rx;             /* min_rx is set to decay_min_rx when */
@@ -205,13 +217,21 @@ struct bfd {
                                   /* detect interval. */
     uint64_t decay_rx_packets;    /* Packets received by 'netdev'. */
     long long int decay_detect_time; /* Decay detection time. */
+
+    uint64_t flap_count;          /* Counts bfd forwarding flaps. */
+
+    /* True when the variables returned by bfd_get_status() are changed
+     * since last check. */
+    bool status_changed;
 };
 
 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
 static struct hmap all_bfds__ = HMAP_INITIALIZER(&all_bfds__);
 static struct hmap *const all_bfds OVS_GUARDED_BY(mutex) = &all_bfds__;
 
-static bool bfd_forwarding__(const struct bfd *) OVS_REQUIRES(mutex);
+static bool bfd_lookup_ip(const char *host_name, struct in_addr *)
+    OVS_REQUIRES(mutex);
+static bool bfd_forwarding__(struct bfd *) OVS_REQUIRES(mutex);
 static bool bfd_in_poll(const struct bfd *) OVS_REQUIRES(mutex);
 static void bfd_poll(struct bfd *bfd) OVS_REQUIRES(mutex);
 static const char *bfd_diag_str(enum diag) OVS_REQUIRES(mutex);
@@ -230,7 +250,8 @@ static void bfd_put_details(struct ds *, const struct bfd *)
 static uint64_t bfd_rx_packets(const struct bfd *) OVS_REQUIRES(mutex);
 static void bfd_try_decay(struct bfd *) OVS_REQUIRES(mutex);
 static void bfd_decay_update(struct bfd *) OVS_REQUIRES(mutex);
-static void bfd_check_rx(struct bfd *) OVS_REQUIRES(mutex);
+static void bfd_status_changed(struct bfd *) OVS_REQUIRES(mutex);
+
 static void bfd_forwarding_if_rx_update(struct bfd *) OVS_REQUIRES(mutex);
 static void bfd_unixctl_show(struct unixctl_conn *, int argc,
                              const char *argv[], void *aux OVS_UNUSED);
@@ -245,7 +266,7 @@ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(20, 20);
 /* Returns true if the interface on which 'bfd' is running may be used to
  * forward traffic according to the BFD session state. */
 bool
-bfd_forwarding(const struct bfd *bfd) OVS_EXCLUDED(mutex)
+bfd_forwarding(struct bfd *bfd) OVS_EXCLUDED(mutex)
 {
     bool ret;
 
@@ -255,6 +276,34 @@ bfd_forwarding(const struct bfd *bfd) OVS_EXCLUDED(mutex)
     return ret;
 }
 
+/* When forwarding_if_rx is enabled, if there are packets received,
+ * updates forwarding_if_rx_detect_time. */
+void
+bfd_account_rx(struct bfd *bfd, const struct dpif_flow_stats *stats)
+{
+    if (stats->n_packets && bfd->forwarding_if_rx) {
+        ovs_mutex_lock(&mutex);
+        bfd_forwarding__(bfd);
+        bfd_forwarding_if_rx_update(bfd);
+        bfd_forwarding__(bfd);
+        ovs_mutex_unlock(&mutex);
+    }
+}
+
+/* Returns and resets the 'bfd->status_changed'. */
+bool
+bfd_check_status_change(struct bfd *bfd) OVS_EXCLUDED(mutex)
+{
+    bool ret;
+
+    ovs_mutex_lock(&mutex);
+    ret = bfd->status_changed;
+    bfd->status_changed = false;
+    ovs_mutex_unlock(&mutex);
+
+    return ret;
+}
+
 /* Returns a 'smap' of key value pairs representing the status of 'bfd'
  * intended for the OVS database. */
 void
@@ -262,9 +311,12 @@ bfd_get_status(const struct bfd *bfd, struct smap *smap)
     OVS_EXCLUDED(mutex)
 {
     ovs_mutex_lock(&mutex);
-    smap_add(smap, "forwarding", bfd_forwarding__(bfd)? "true" : "false");
+    smap_add(smap, "forwarding",
+             bfd_forwarding__(CONST_CAST(struct bfd *, bfd))
+             ? "true" : "false");
     smap_add(smap, "state", bfd_state_str(bfd->state));
     smap_add(smap, "diagnostic", bfd_diag_str(bfd->diag));
+    smap_add_format(smap, "flap_count", "%"PRIu64, bfd->flap_count);
 
     if (bfd->state != STATE_DOWN) {
         smap_add(smap, "remote_state", bfd_state_str(bfd->rmt_state));
@@ -290,7 +342,8 @@ bfd_configure(struct bfd *bfd, const char *name, const struct smap *cfg,
     bool need_poll = false;
     bool cfg_min_rx_changed = false;
     bool cpath_down, forwarding_if_rx;
-    const char *hwaddr;
+    const char *hwaddr, *ip_src, *ip_dst;
+    struct in_addr in_addr;
     uint8_t ea[ETH_ADDR_LEN];
 
     if (ovsthread_once_start(&once)) {
@@ -318,10 +371,11 @@ bfd_configure(struct bfd *bfd, const char *name, const struct smap *cfg,
         bfd->diag = DIAG_NONE;
         bfd->min_tx = 1000;
         bfd->mult = 3;
-        atomic_init(&bfd->ref_cnt, 1);
+        ovs_refcount_init(&bfd->ref_cnt);
         bfd->netdev = netdev_ref(netdev);
         bfd->rx_packets = bfd_rx_packets(bfd);
         bfd->in_decay = false;
+        bfd->flap_count = 0;
 
         /* RFC 5881 section 4
          * The source port MUST be in the range 49152 through 65535.  The same
@@ -379,9 +433,7 @@ bfd_configure(struct bfd *bfd, const char *name, const struct smap *cfg,
     cpath_down = smap_get_bool(cfg, "cpath_down", false);
     if (bfd->cpath_down != cpath_down) {
         bfd->cpath_down = cpath_down;
-        if (bfd->diag == DIAG_NONE || bfd->diag == DIAG_CPATH_DOWN) {
-            bfd_set_state(bfd, bfd->state, DIAG_NONE);
-        }
+        bfd_set_state(bfd, bfd->state, DIAG_NONE);
         need_poll = true;
     }
 
@@ -394,6 +446,20 @@ bfd_configure(struct bfd *bfd, const char *name, const struct smap *cfg,
         bfd->eth_dst_set = false;
     }
 
+    ip_src = smap_get(cfg, "bfd_src_ip");
+    if (ip_src && bfd_lookup_ip(ip_src, &in_addr)) {
+        memcpy(&bfd->ip_src, &in_addr, sizeof in_addr);
+    } else {
+        bfd->ip_src = htonl(0xA9FE0100); /* 169.254.1.0. */
+    }
+
+    ip_dst = smap_get(cfg, "bfd_dst_ip");
+    if (ip_dst && bfd_lookup_ip(ip_dst, &in_addr)) {
+        memcpy(&bfd->ip_dst, &in_addr, sizeof in_addr);
+    } else {
+        bfd->ip_dst = htonl(0xA9FE0101); /* 169.254.1.1. */
+    }
+
     forwarding_if_rx = smap_get_bool(cfg, "forwarding_if_rx", false);
     if (bfd->forwarding_if_rx != forwarding_if_rx) {
         bfd->forwarding_if_rx = forwarding_if_rx;
@@ -416,9 +482,7 @@ bfd_ref(const struct bfd *bfd_)
 {
     struct bfd *bfd = CONST_CAST(struct bfd *, bfd_);
     if (bfd) {
-        int orig;
-        atomic_add(&bfd->ref_cnt, 1, &orig);
-        ovs_assert(orig > 0);
+        ovs_refcount_ref(&bfd->ref_cnt);
     }
     return bfd;
 }
@@ -426,35 +490,43 @@ bfd_ref(const struct bfd *bfd_)
 void
 bfd_unref(struct bfd *bfd) OVS_EXCLUDED(mutex)
 {
-    if (bfd) {
-        int orig;
-
-        atomic_sub(&bfd->ref_cnt, 1, &orig);
-        ovs_assert(orig > 0);
-        if (orig == 1) {
-            ovs_mutex_lock(&mutex);
-            hmap_remove(all_bfds, &bfd->node);
-            netdev_close(bfd->netdev);
-            free(bfd->name);
-            free(bfd);
-            ovs_mutex_unlock(&mutex);
-        }
+    if (bfd && ovs_refcount_unref(&bfd->ref_cnt) == 1) {
+        ovs_mutex_lock(&mutex);
+        hmap_remove(all_bfds, &bfd->node);
+        netdev_close(bfd->netdev);
+        free(bfd->name);
+        free(bfd);
+        ovs_mutex_unlock(&mutex);
     }
 }
 
 void
 bfd_wait(const struct bfd *bfd) OVS_EXCLUDED(mutex)
 {
-    ovs_mutex_lock(&mutex);
-    if (bfd->flags & FLAG_FINAL) {
-        poll_immediate_wake();
+    poll_timer_wait_until(bfd_wake_time(bfd));
+}
+
+/* Returns the next wake up time. */
+long long int
+bfd_wake_time(const struct bfd *bfd) OVS_EXCLUDED(mutex)
+{
+    long long int retval;
+
+    if (!bfd) {
+        return LLONG_MAX;
     }
 
-    poll_timer_wait_until(bfd->next_tx);
-    if (bfd->state > STATE_DOWN) {
-        poll_timer_wait_until(bfd->detect_time);
+    ovs_mutex_lock(&mutex);
+    if (bfd->flags & FLAG_FINAL) {
+        retval = 0;
+    } else {
+        retval = bfd->next_tx;
+        if (bfd->state > STATE_DOWN) {
+            retval = MIN(bfd->detect_time, retval);
+        }
     }
     ovs_mutex_unlock(&mutex);
+    return retval;
 }
 
 void
@@ -470,6 +542,7 @@ bfd_run(struct bfd *bfd) OVS_EXCLUDED(mutex)
     if (bfd->state > STATE_DOWN && now >= bfd->detect_time) {
         bfd_set_state(bfd, STATE_DOWN, DIAG_EXPIRED);
     }
+    bfd_forwarding__(bfd);
 
     /* Decay may only happen when state is STATE_UP, bfd->decay_min_rx is
      * configured, and decay_detect_time is reached. */
@@ -478,9 +551,6 @@ bfd_run(struct bfd *bfd) OVS_EXCLUDED(mutex)
         bfd_try_decay(bfd);
     }
 
-    /* Always checks the reception of any packet. */
-    bfd_check_rx(bfd);
-
     if (bfd->min_tx != bfd->cfg_min_tx
         || (bfd->min_rx != bfd->cfg_min_rx && bfd->min_rx != bfd->decay_min_rx)
         || bfd->in_decay != old_in_decay) {
@@ -536,8 +606,8 @@ bfd_put_packet(struct bfd *bfd, struct ofpbuf *p,
     ip->ip_ttl = MAXTTL;
     ip->ip_tos = IPTOS_LOWDELAY | IPTOS_THROUGHPUT;
     ip->ip_proto = IPPROTO_UDP;
-    ip->ip_src = htonl(0xA9FE0100); /* 169.254.1.0 Link Local. */
-    ip->ip_dst = htonl(0xA9FE0101); /* 169.254.1.1 Link Local. */
+    put_16aligned_be32(&ip->ip_src, bfd->ip_src);
+    put_16aligned_be32(&ip->ip_dst, bfd->ip_dst);
     ip->ip_csum = csum(ip, sizeof *ip);
 
     udp = ofpbuf_put_zeros(p, sizeof *udp);
@@ -609,6 +679,11 @@ bfd_process_packet(struct bfd *bfd, const struct flow *flow,
     enum flags flags;
     uint8_t version;
     struct msg *msg;
+    const uint8_t *l7 = ofpbuf_get_udp_payload(p);
+
+    if (!l7) {
+        return; /* No UDP payload. */
+    }
 
     /* This function is designed to follow section RFC 5880 6.8.6 closely. */
 
@@ -616,15 +691,19 @@ bfd_process_packet(struct bfd *bfd, const struct flow *flow,
     /* Increments the decay rx counter. */
     bfd->decay_rx_ctl++;
 
+    bfd_forwarding__(bfd);
+
     if (flow->nw_ttl != 255) {
         /* XXX Should drop in the kernel to prevent DOS. */
         goto out;
     }
 
-    msg = ofpbuf_at(p, (uint8_t *)p->l7 - (uint8_t *)p->data, BFD_PACKET_LEN);
+    msg = ofpbuf_at(p, l7 - (uint8_t *)ofpbuf_data(p), BFD_PACKET_LEN);
     if (!msg) {
-        VLOG_INFO_RL(&rl, "%s: Received unparseable BFD control message.",
-                     bfd->name);
+        VLOG_INFO_RL(&rl, "%s: Received too-short BFD control message (only "
+                     "%"PRIdPTR" bytes long, at least %d required).",
+                     bfd->name, (uint8_t *) ofpbuf_tail(p) - l7,
+                     BFD_PACKET_LEN);
         goto out;
     }
 
@@ -694,6 +773,10 @@ bfd_process_packet(struct bfd *bfd, const struct flow *flow,
         goto out;
     }
 
+    if (bfd->rmt_state != rmt_state) {
+        bfd_status_changed(bfd);
+    }
+
     bfd->rmt_disc = ntohl(msg->my_disc);
     bfd->rmt_state = rmt_state;
     bfd->rmt_flags = flags;
@@ -719,7 +802,9 @@ bfd_process_packet(struct bfd *bfd, const struct flow *flow,
     rmt_min_rx = MAX(ntohl(msg->min_rx) / 1000, 1);
     if (bfd->rmt_min_rx != rmt_min_rx) {
         bfd->rmt_min_rx = rmt_min_rx;
-        bfd_set_next_tx(bfd);
+        if (bfd->next_tx) {
+            bfd_set_next_tx(bfd);
+        }
         log_msg(VLL_INFO, msg, "New remote min_rx", bfd);
     }
 
@@ -757,12 +842,17 @@ bfd_process_packet(struct bfd *bfd, const struct flow *flow,
             break;
         case STATE_ADMIN_DOWN:
         default:
-            NOT_REACHED();
+            OVS_NOT_REACHED();
         }
     }
     /* XXX: RFC 5880 Section 6.8.6 Demand mode related calculations here. */
 
+    if (bfd->forwarding_if_rx) {
+        bfd->demand_rx_bfd_time = time_msec() + 100 * bfd->cfg_min_rx;
+    }
+
 out:
+    bfd_forwarding__(bfd);
     ovs_mutex_unlock(&mutex);
 }
 
@@ -787,24 +877,49 @@ bfd_set_netdev(struct bfd *bfd, const struct netdev *netdev)
 }
 
 \f
+/* Updates the forwarding flag.  If override is not configured and
+ * the forwarding flag value changes, increments the flap count.
+ *
+ * Note this function may be called multiple times in a function
+ * (e.g. bfd_account_rx) before and after the bfd state or status
+ * change.  This is to capture any forwarding flag flap. */
 static bool
-bfd_forwarding__(const struct bfd *bfd) OVS_REQUIRES(mutex)
+bfd_forwarding__(struct bfd *bfd) OVS_REQUIRES(mutex)
 {
-    long long int time;
+    long long int now = time_msec();
+    bool forwarding_if_rx;
+    bool last_forwarding = bfd->last_forwarding;
 
     if (bfd->forwarding_override != -1) {
         return bfd->forwarding_override == 1;
     }
 
-    time = bfd->forwarding_if_rx_detect_time;
-    return (bfd->state == STATE_UP
-            || (bfd->forwarding_if_rx && time > time_msec()))
-           && bfd->rmt_diag != DIAG_PATH_DOWN
-           && bfd->rmt_diag != DIAG_CPATH_DOWN
-           && bfd->rmt_diag != DIAG_RCPATH_DOWN;
+    forwarding_if_rx = bfd->forwarding_if_rx
+                       && bfd->forwarding_if_rx_detect_time > now
+                       && bfd->demand_rx_bfd_time > now;
+
+    bfd->last_forwarding = (bfd->state == STATE_UP || forwarding_if_rx)
+                           && bfd->rmt_diag != DIAG_PATH_DOWN
+                           && bfd->rmt_diag != DIAG_CPATH_DOWN
+                           && bfd->rmt_diag != DIAG_RCPATH_DOWN;
+    if (bfd->last_forwarding != last_forwarding) {
+        bfd->flap_count++;
+        bfd_status_changed(bfd);
+    }
+    return bfd->last_forwarding;
 }
 
 /* Helpers. */
+static bool
+bfd_lookup_ip(const char *host_name, struct in_addr *addr)
+{
+    if (!inet_pton(AF_INET, host_name, addr)) {
+        VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
+        return false;
+    }
+    return true;
+}
+
 static bool
 bfd_in_poll(const struct bfd *bfd) OVS_REQUIRES(mutex)
 {
@@ -962,7 +1077,7 @@ static void
 bfd_set_state(struct bfd *bfd, enum state state, enum diag diag)
     OVS_REQUIRES(mutex)
 {
-    if (diag == DIAG_NONE && bfd->cpath_down) {
+    if (bfd->cpath_down) {
         diag = DIAG_CPATH_DOWN;
     }
 
@@ -1001,6 +1116,8 @@ bfd_set_state(struct bfd *bfd, enum state state, enum diag diag)
         if (bfd->state == STATE_UP && bfd->decay_min_rx) {
             bfd_decay_update(bfd);
         }
+
+        bfd_status_changed(bfd);
     }
 }
 
@@ -1047,26 +1164,14 @@ bfd_decay_update(struct bfd * bfd) OVS_REQUIRES(mutex)
     bfd->decay_detect_time = MAX(bfd->decay_min_rx, 2000) + time_msec();
 }
 
-/* Checks if there are packets received during the time since last call.
- * If forwarding_if_rx is enabled and packets are received, updates the
- * forwarding_if_rx_detect_time. */
+/* Records the status change and changes the global connectivity seq. */
 static void
-bfd_check_rx(struct bfd *bfd) OVS_REQUIRES(mutex)
+bfd_status_changed(struct bfd *bfd) OVS_REQUIRES(mutex)
 {
-    uint64_t rx_packets = bfd_rx_packets(bfd);
-    int64_t diff;
-
-    diff = rx_packets - bfd->rx_packets;
-    bfd->rx_packets = rx_packets;
-    if (diff < 0) {
-        VLOG_INFO_RL(&rl, "rx_packets count is smaller than last time.");
-    }
-    if (bfd->forwarding_if_rx && diff > 0) {
-        bfd_forwarding_if_rx_update(bfd);
-    }
+    seq_change(connectivity_seq_get());
+    bfd->status_changed = true;
 }
 
-/* Updates the forwarding_if_rx_detect_time. */
 static void
 bfd_forwarding_if_rx_update(struct bfd *bfd) OVS_REQUIRES(mutex)
 {
@@ -1118,7 +1223,8 @@ static void
 bfd_put_details(struct ds *ds, const struct bfd *bfd) OVS_REQUIRES(mutex)
 {
     ds_put_format(ds, "\tForwarding: %s\n",
-                  bfd_forwarding__(bfd) ? "true" : "false");
+                  bfd_forwarding__(CONST_CAST(struct bfd *, bfd))
+                  ? "true" : "false");
     ds_put_format(ds, "\tDetect Multiplier: %d\n", bfd->mult);
     ds_put_format(ds, "\tConcatenated Path Down: %s\n",
                   bfd->cpath_down ? "true" : "false");
@@ -1213,9 +1319,11 @@ bfd_unixctl_set_forwarding_override(struct unixctl_conn *conn, int argc,
             goto out;
         }
         bfd->forwarding_override = forwarding_override;
+        bfd_status_changed(bfd);
     } else {
         HMAP_FOR_EACH (bfd, node, all_bfds) {
             bfd->forwarding_override = forwarding_override;
+            bfd_status_changed(bfd);
         }
     }