5f9769
From 9f42e93b6a25bff87074156586505a6e8968f8cb Mon Sep 17 00:00:00 2001
5f9769
Message-Id: <9f42e93b6a25bff87074156586505a6e8968f8cb.1610538323.git.lorenzo.bianconi@redhat.com>
5f9769
From: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
5f9769
Date: Tue, 12 Jan 2021 13:10:56 +0100
5f9769
Subject: [PATCH] bfd: introduce IPv6 support
5f9769
5f9769
Introduce IPv6 support to ovn controller BFD implementation
5f9769
5f9769
Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com>
5f9769
Acked-by: Mark Michelson <mmichels@redhat.com>
5f9769
Signed-off-by: Numan Siddique <numans@ovn.org>
5f9769
---
5f9769
 NEWS                 |   2 +-
5f9769
 controller/pinctrl.c | 112 ++++++++++++++++++++++++++++---------------
5f9769
 tests/system-ovn.at  |  16 ++++++-
5f9769
 3 files changed, 89 insertions(+), 41 deletions(-)
5f9769
5f9769
--- a/NEWS
5f9769
+++ b/NEWS
5f9769
@@ -2,7 +2,7 @@ Post-v20.12.0
5f9769
 -------------------------
5f9769
   - Support ECMP multiple nexthops for reroute router policies.
5f9769
    - BFD protocol support according to RFC880 [0]. Introduce next-hop BFD
5f9769
-     availability check for OVN static routes. IPv6 is not suported yet.
5f9769
+     availability check for OVN static routes.
5f9769
      [0] https://tools.ietf.org/html/rfc5880)
5f9769
 
5f9769
 OVN v20.12.0 - 18 Dec 2020
5f9769
--- a/controller/pinctrl.c
5f9769
+++ b/controller/pinctrl.c
5f9769
@@ -6393,10 +6393,10 @@ struct bfd_entry {
5f9769
 
5f9769
     /* L2 source address */
5f9769
     struct eth_addr src_mac;
5f9769
-    /* IPv4 source address */
5f9769
-    ovs_be32 ip_src;
5f9769
-    /* IPv4 destination address */
5f9769
-    ovs_be32 ip_dst;
5f9769
+    /* IP source address */
5f9769
+    struct in6_addr ip_src;
5f9769
+    /* IP destination address */
5f9769
+    struct in6_addr ip_dst;
5f9769
     /* RFC 5881 section 4
5f9769
      * The source port MUST be in the range 49152 through 65535.
5f9769
      * The same UDP source port number MUST be used for all BFD
5f9769
@@ -6458,20 +6458,17 @@ pinctrl_find_bfd_monitor_entry_by_port(c
5f9769
 }
5f9769
 
5f9769
 static struct bfd_entry *
5f9769
-pinctrl_find_bfd_monitor_entry_by_disc(ovs_be32 ip, ovs_be32 disc)
5f9769
+pinctrl_find_bfd_monitor_entry_by_disc(char *ip, ovs_be32 disc)
5f9769
 {
5f9769
-    char *ip_src = xasprintf(IP_FMT, IP_ARGS(ip));
5f9769
     struct bfd_entry *ret = NULL, *entry;
5f9769
 
5f9769
-    HMAP_FOR_EACH_WITH_HASH (entry, node, hash_string(ip_src, 0),
5f9769
+    HMAP_FOR_EACH_WITH_HASH (entry, node, hash_string(ip, 0),
5f9769
                              &bfd_monitor_map) {
5f9769
         if (entry->local_disc == disc) {
5f9769
             ret = entry;
5f9769
             break;
5f9769
         }
5f9769
     }
5f9769
-
5f9769
-    free(ip_src);
5f9769
     return ret;
5f9769
 }
5f9769
 
5f9769
@@ -6501,33 +6498,28 @@ static void
5f9769
 bfd_monitor_put_bfd_msg(struct bfd_entry *entry, struct dp_packet *packet,
5f9769
                         bool final)
5f9769
 {
5f9769
-    struct udp_header *udp;
5f9769
-    struct bfd_msg *msg;
5f9769
+    int payload_len = sizeof(struct udp_header) + sizeof(struct bfd_msg);
5f9769
 
5f9769
     /* Properly align after the ethernet header */
5f9769
     dp_packet_reserve(packet, 2);
5f9769
-    struct eth_header *eth = dp_packet_put_uninit(packet, sizeof *eth);
5f9769
-    eth->eth_dst = eth_addr_broadcast;
5f9769
-    eth->eth_src = entry->src_mac;
5f9769
-    eth->eth_type = htons(ETH_TYPE_IP);
5f9769
-
5f9769
-    struct ip_header *ip = dp_packet_put_zeros(packet, sizeof *ip);
5f9769
-    ip->ip_ihl_ver = IP_IHL_VER(5, 4);
5f9769
-    ip->ip_tot_len = htons(sizeof *ip + sizeof *udp + sizeof *msg);
5f9769
-    ip->ip_ttl = MAXTTL;
5f9769
-    ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
5f9769
-    ip->ip_proto = IPPROTO_UDP;
5f9769
-    put_16aligned_be32(&ip->ip_src, entry->ip_src);
5f9769
-    put_16aligned_be32(&ip->ip_dst, entry->ip_dst);
5f9769
-    /* Checksum has already been zeroed by put_zeros call. */
5f9769
-    ip->ip_csum = csum(ip, sizeof *ip);
5f9769
+    if (IN6_IS_ADDR_V4MAPPED(&entry->ip_src)) {
5f9769
+        ovs_be32 ip_src = in6_addr_get_mapped_ipv4(&entry->ip_src);
5f9769
+        ovs_be32 ip_dst = in6_addr_get_mapped_ipv4(&entry->ip_dst);
5f9769
+        pinctrl_compose_ipv4(packet, entry->src_mac, eth_addr_broadcast,
5f9769
+                             ip_src, ip_dst, IPPROTO_UDP, MAXTTL, payload_len);
5f9769
+    } else {
5f9769
+        pinctrl_compose_ipv6(packet, entry->src_mac, eth_addr_broadcast,
5f9769
+                             &entry->ip_src, &entry->ip_dst, IPPROTO_UDP,
5f9769
+                             MAXTTL, payload_len);
5f9769
+    }
5f9769
 
5f9769
-    udp = dp_packet_put_zeros(packet, sizeof *udp);
5f9769
+    struct udp_header *udp = dp_packet_put_zeros(packet, sizeof *udp);
5f9769
+    udp->udp_len = htons(payload_len);
5f9769
+    udp->udp_csum = 0;
5f9769
     udp->udp_src = htons(entry->udp_src);
5f9769
     udp->udp_dst = htons(BFD_DEST_PORT);
5f9769
-    udp->udp_len = htons(sizeof *udp + sizeof *msg);
5f9769
 
5f9769
-    msg = dp_packet_put_zeros(packet, sizeof *msg);
5f9769
+    struct bfd_msg *msg = dp_packet_put_zeros(packet, sizeof *msg);
5f9769
     msg->vers_diag = (BFD_VERSION << 5);
5f9769
     msg->mult = entry->local_mult;
5f9769
     msg->length = BFD_PACKET_LEN;
5f9769
@@ -6538,6 +6530,17 @@ bfd_monitor_put_bfd_msg(struct bfd_entry
5f9769
     /* min_tx and min_rx are in us - RFC 5880 page 9 */
5f9769
     msg->min_tx = htonl(entry->local_min_tx * 1000);
5f9769
     msg->min_rx = htonl(entry->local_min_rx * 1000);
5f9769
+
5f9769
+    if (!IN6_IS_ADDR_V4MAPPED(&entry->ip_src)) {
5f9769
+        /* IPv6 needs UDP checksum calculated */
5f9769
+        uint32_t csum = packet_csum_pseudoheader6(dp_packet_l3(packet));
5f9769
+        int len = (uint8_t *)udp - (uint8_t *)dp_packet_eth(packet);
5f9769
+        csum = csum_continue(csum, udp, dp_packet_size(packet) - len);
5f9769
+        udp->udp_csum = csum_finish(csum);
5f9769
+        if (!udp->udp_csum) {
5f9769
+            udp->udp_csum = htons(0xffff);
5f9769
+        }
5f9769
+    }
5f9769
 }
5f9769
 
5f9769
 static void
5f9769
@@ -6736,9 +6739,18 @@ pinctrl_handle_bfd_msg(struct rconn *swc
5f9769
         return;
5f9769
     }
5f9769
 
5f9769
+    char *ip_src;
5f9769
+    if (ip_flow->dl_type == htons(ETH_TYPE_IP)) {
5f9769
+        ip_src = normalize_ipv4_prefix(ip_flow->nw_src, 32);
5f9769
+    } else {
5f9769
+        ip_src = normalize_ipv6_prefix(&ip_flow->ipv6_src, 128);
5f9769
+    }
5f9769
+
5f9769
     const struct bfd_msg *msg = dp_packet_get_udp_payload(pkt_in);
5f9769
-    struct bfd_entry *entry = pinctrl_find_bfd_monitor_entry_by_disc(
5f9769
-            ip_flow->nw_src, msg->your_disc);
5f9769
+    struct bfd_entry *entry =
5f9769
+        pinctrl_find_bfd_monitor_entry_by_disc(ip_src, msg->your_disc);
5f9769
+    free(ip_src);
5f9769
+
5f9769
     if (!entry) {
5f9769
         return;
5f9769
     }
5f9769
@@ -6821,10 +6833,21 @@ static void
5f9769
 bfd_monitor_check_sb_conf(const struct sbrec_bfd *sb_bt,
5f9769
                           struct bfd_entry *entry)
5f9769
 {
5f9769
-    ovs_be32 ip_dst;
5f9769
+    struct lport_addresses dst_addr;
5f9769
+
5f9769
+    if (extract_ip_addresses(sb_bt->dst_ip, &dst_addr)) {
5f9769
+        struct in6_addr addr;
5f9769
+
5f9769
+        if (dst_addr.n_ipv6_addrs > 0) {
5f9769
+            addr = dst_addr.ipv6_addrs[0].addr;
5f9769
+        } else {
5f9769
+            addr = in6_addr_mapped_ipv4(dst_addr.ipv4_addrs[0].addr);
5f9769
+        }
5f9769
 
5f9769
-    if (ip_parse(sb_bt->dst_ip, &ip_dst) && ip_dst != entry->ip_dst) {
5f9769
-        entry->ip_dst = ip_dst;
5f9769
+        if (!ipv6_addr_equals(&addr, &entry->ip_dst)) {
5f9769
+            entry->ip_dst = addr;
5f9769
+        }
5f9769
+        destroy_lport_addresses(&dst_addr);
5f9769
     }
5f9769
 
5f9769
     if (sb_bt->min_tx != entry->local_min_tx) {
5f9769
@@ -6889,11 +6912,15 @@ bfd_monitor_run(struct ovsdb_idl_txn *ov
5f9769
         entry = pinctrl_find_bfd_monitor_entry_by_port(
5f9769
                 bt->dst_ip, bt->src_port);
5f9769
         if (!entry) {
5f9769
-            ovs_be32 ip_dst, ip_src = htonl(BFD_DEFAULT_SRC_IP);
5f9769
             struct eth_addr ea = eth_addr_zero;
5f9769
+            struct lport_addresses dst_addr;
5f9769
+            struct in6_addr ip_src, ip_dst;
5f9769
             int i;
5f9769
 
5f9769
-            if (!ip_parse(bt->dst_ip, &ip_dst)) {
5f9769
+            ip_dst = in6_addr_mapped_ipv4(htonl(BFD_DEFAULT_DST_IP));
5f9769
+            ip_src = in6_addr_mapped_ipv4(htonl(BFD_DEFAULT_SRC_IP));
5f9769
+
5f9769
+            if (!extract_ip_addresses(bt->dst_ip, &dst_addr)) {
5f9769
                 continue;
5f9769
             }
5f9769
 
5f9769
@@ -6905,13 +6932,20 @@ bfd_monitor_run(struct ovsdb_idl_txn *ov
5f9769
                 }
5f9769
 
5f9769
                 ea = laddrs.ea;
5f9769
-                if (laddrs.n_ipv4_addrs > 0) {
5f9769
-                    ip_src = laddrs.ipv4_addrs[0].addr;
5f9769
+                if (dst_addr.n_ipv6_addrs > 0 && laddrs.n_ipv6_addrs > 0) {
5f9769
+                    ip_dst = dst_addr.ipv6_addrs[0].addr;
5f9769
+                    ip_src = laddrs.ipv6_addrs[0].addr;
5f9769
+                    destroy_lport_addresses(&laddrs);
5f9769
+                    break;
5f9769
+                } else if (laddrs.n_ipv4_addrs > 0) {
5f9769
+                    ip_dst = in6_addr_mapped_ipv4(dst_addr.ipv4_addrs[0].addr);
5f9769
+                    ip_src = in6_addr_mapped_ipv4(laddrs.ipv4_addrs[0].addr);
5f9769
                     destroy_lport_addresses(&laddrs);
5f9769
                     break;
5f9769
                 }
5f9769
                 destroy_lport_addresses(&laddrs);
5f9769
             }
5f9769
+            destroy_lport_addresses(&dst_addr);
5f9769
 
5f9769
             if (eth_addr_is_zero(ea)) {
5f9769
                 continue;
5f9769
--- a/tests/system-ovn.at
5f9769
+++ b/tests/system-ovn.at
5f9769
@@ -5563,7 +5563,7 @@ check ovn-nbctl ls-add public
5f9769
 
5f9769
 check ovn-nbctl lrp-add R1 rp-sw0 00:00:01:01:02:03 192.168.1.1/24
5f9769
 check ovn-nbctl lrp-add R1 rp-sw1 00:00:03:01:02:03 192.168.2.1/24
5f9769
-check ovn-nbctl lrp-add R1 rp-public 00:00:02:01:02:03 172.16.1.1/24 \
5f9769
+check ovn-nbctl lrp-add R1 rp-public 00:00:02:01:02:03 172.16.1.1/24 1000::a/64 \
5f9769
     -- lrp-set-gateway-chassis rp-public hv1
5f9769
 
5f9769
 check ovn-nbctl lsp-add sw0 sw0-rp -- set Logical_Switch_Port sw0-rp \
5f9769
@@ -5593,6 +5593,7 @@ ADD_NAMESPACES(server)
5f9769
 NS_CHECK_EXEC([server], [ip link set dev lo up])
5f9769
 ADD_VETH(s1, server, br-ext, "172.16.1.50/24", "f0:00:00:01:02:05", \
5f9769
          "172.16.1.1")
5f9769
+NS_CHECK_EXEC([server], [ip addr add 1000::b/64 dev s1])
5f9769
 
5f9769
 AT_CHECK([ovs-vsctl set Open_vSwitch . external-ids:ovn-bridge-mappings=phynet:br-ext])
5f9769
 check ovn-nbctl lsp-add public public1 \
5f9769
@@ -5652,6 +5653,19 @@ sleep 5
5f9769
 kill $(pidof tcpdump)
5f9769
 AT_CHECK([grep -qi bfd bfd.pcap],[1])
5f9769
 
5f9769
+uuid_v6=$(ovn-nbctl create bfd logical_port=rp-public dst_ip=\"1000::b\")
5f9769
+check ovn-nbctl lr-route-add R1 2000::/64 1000::b
5f9769
+route_uuid_v6=$(fetch_column nb:logical_router_static_route _uuid ip_prefix=\"2000::/64\")
5f9769
+ovn-nbctl set logical_router_static_route $route_uuid_v6 bfd=$uuid_v6
5f9769
+check ovn-nbctl --wait=hv sync
5f9769
+NS_CHECK_EXEC([server], [bfdd-beacon --listen=1000::b], [0])
5f9769
+NS_CHECK_EXEC([server], [bfdd-control allow 1000::a], [0], [dnl
5f9769
+Allowing connections from 1000::a
5f9769
+])
5f9769
+
5f9769
+wait_column "up" nb:bfd status logical_port=rp-public
5f9769
+ovn-nbctl destroy bfd $uuid_v6
5f9769
+
5f9769
 kill $(pidof ovn-controller)
5f9769
 
5f9769
 as ovn-sb