[ovs-dev,v3,6/7] dpif-netlink-rtnl: add GENEVE creation support

Submitted by Eric Garver on April 13, 2017, 8:47 p.m.

Details

Message ID 20170413204732.16707-7-e@erig.me
State Changes Requested
Delegated to: Joe Stringer
Headers show

Commit Message

Eric Garver April 13, 2017, 8:47 p.m.
Creates GENEVE devices using rtnetlink and tunnel metadata.

Co-Authored-by: Thadeu Lima de Souza Cascardo <cascardo@redhat.com>
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@redhat.com>
Signed-off-by: Eric Garver <e@erig.me>
---
 lib/dpif-netlink-rtnl.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 114 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/lib/dpif-netlink-rtnl.c b/lib/dpif-netlink-rtnl.c
index 73022b5d86d0..5a3326ea1f63 100644
--- a/lib/dpif-netlink-rtnl.c
+++ b/lib/dpif-netlink-rtnl.c
@@ -47,6 +47,15 @@ 
 #define IFLA_GRE_COLLECT_METADATA 18
 #endif
 
+#ifndef IFLA_GENEVE_MAX
+#define IFLA_GENEVE_MAX 0
+#endif
+#if IFLA_GENEVE_MAX < 10
+#define IFLA_GENEVE_PORT 5
+#define IFLA_GENEVE_COLLECT_METADATA 6
+#define IFLA_GENEVE_UDP_ZERO_CSUM6_RX 10
+#endif
+
 static const struct nl_policy rtlink_policy[] = {
     [IFLA_LINKINFO] = { .type = NL_A_NESTED },
 };
@@ -295,6 +304,109 @@  dpif_netlink_rtnl_gre_create(struct netdev *netdev)
     return dpif_netlink_rtnl_gre_create_kind(netdev, "gretap");
 }
 
+static int
+dpif_netlink_rtnl_geneve_verify(struct netdev *netdev, const char *name,
+                             const char *kind)
+{
+    const struct netdev_tunnel_config *tnl_cfg;
+    struct ifinfomsg *ifmsg;
+    struct ofpbuf *reply;
+    int err;
+
+    static const struct nl_policy geneve_policy[] = {
+        [IFLA_GENEVE_COLLECT_METADATA] = { .type = NL_A_FLAG },
+        [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NL_A_U8 },
+        [IFLA_GENEVE_PORT] = { .type = NL_A_U16 },
+    };
+
+    tnl_cfg = netdev_get_tunnel_config(netdev);
+    if (!tnl_cfg) {
+        return EINVAL;
+    }
+
+    err = dpif_netlink_rtnl_getlink(name, &reply);
+
+    if (!err) {
+        struct nlattr *rtlink[ARRAY_SIZE(rtlink_policy)];
+        struct nlattr *linkinfo[ARRAY_SIZE(linkinfo_policy)];
+        struct nlattr *geneve[ARRAY_SIZE(geneve_policy)];
+
+        ifmsg = ofpbuf_at(reply, NLMSG_HDRLEN, sizeof *ifmsg);
+        if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof *ifmsg,
+                             rtlink_policy, rtlink, ARRAY_SIZE(rtlink_policy))
+            || !nl_parse_nested(rtlink[IFLA_LINKINFO], linkinfo_policy,
+                                linkinfo, ARRAY_SIZE(linkinfo_policy))
+            || strcmp(nl_attr_get_string(linkinfo[IFLA_INFO_KIND]), kind)
+            || !nl_parse_nested(linkinfo[IFLA_INFO_DATA], geneve_policy,
+                                geneve, ARRAY_SIZE(geneve_policy))) {
+            err = EINVAL;
+        }
+        if (!err) {
+            if (!nl_attr_get_flag(geneve[IFLA_GENEVE_COLLECT_METADATA])
+                || 1 != nl_attr_get_u8(geneve[IFLA_GENEVE_UDP_ZERO_CSUM6_RX])
+                || (tnl_cfg->dst_port !=
+                    nl_attr_get_be16(geneve[IFLA_GENEVE_PORT]))) {
+                err = EINVAL;
+            }
+        }
+        ofpbuf_delete(reply);
+    }
+
+    return err;
+}
+
+static int
+dpif_netlink_rtnl_geneve_create_kind(struct netdev *netdev, const char *kind)
+{
+    const struct netdev_tunnel_config *tnl_cfg;
+    char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
+    size_t linkinfo_off, infodata_off;
+    struct ifinfomsg *ifinfo;
+    struct ofpbuf request;
+    const char *name;
+    int err;
+
+    tnl_cfg = netdev_get_tunnel_config(netdev);
+    if (!tnl_cfg) {
+        return EINVAL;
+    }
+
+    name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
+
+    ofpbuf_init(&request, 0);
+    nl_msg_put_nlmsghdr(&request, 0, RTM_NEWLINK,
+                        NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE);
+    ifinfo = ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg));
+    ifinfo->ifi_change = ifinfo->ifi_flags = IFF_UP;
+    nl_msg_put_string(&request, IFLA_IFNAME, name);
+    nl_msg_put_u32(&request, IFLA_MTU, UINT16_MAX);
+    linkinfo_off = nl_msg_start_nested(&request, IFLA_LINKINFO);
+    nl_msg_put_string(&request, IFLA_INFO_KIND, kind);
+    infodata_off = nl_msg_start_nested(&request, IFLA_INFO_DATA);
+
+    nl_msg_put_flag(&request, IFLA_GENEVE_COLLECT_METADATA);
+    nl_msg_put_u8(&request, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, 1);
+    nl_msg_put_be16(&request, IFLA_GENEVE_PORT, tnl_cfg->dst_port);
+
+    nl_msg_end_nested(&request, infodata_off);
+    nl_msg_end_nested(&request, linkinfo_off);
+
+    err = nl_transact(NETLINK_ROUTE, &request, NULL);
+    ofpbuf_uninit(&request);
+
+    if (!err && (err = dpif_netlink_rtnl_geneve_verify(netdev, name, kind))) {
+        dpif_netlink_rtnl_destroy(name);
+    }
+
+    return err;
+}
+
+static int
+dpif_netlink_rtnl_geneve_create(struct netdev *netdev)
+{
+    return dpif_netlink_rtnl_geneve_create_kind(netdev, "geneve");
+}
+
 int
 dpif_netlink_rtnl_port_create(struct netdev *netdev)
 {
@@ -304,6 +416,7 @@  dpif_netlink_rtnl_port_create(struct netdev *netdev)
     case OVS_VPORT_TYPE_GRE:
         return dpif_netlink_rtnl_gre_create(netdev);
     case OVS_VPORT_TYPE_GENEVE:
+        return dpif_netlink_rtnl_geneve_create(netdev);
     case OVS_VPORT_TYPE_NETDEV:
     case OVS_VPORT_TYPE_INTERNAL:
     case OVS_VPORT_TYPE_LISP:
@@ -322,8 +435,8 @@  dpif_netlink_rtnl_port_destroy(const char *name, const char *type)
     switch (netdev_to_ovs_vport_type(type)) {
     case OVS_VPORT_TYPE_VXLAN:
     case OVS_VPORT_TYPE_GRE:
-        return dpif_netlink_rtnl_destroy(name);
     case OVS_VPORT_TYPE_GENEVE:
+        return dpif_netlink_rtnl_destroy(name);
     case OVS_VPORT_TYPE_NETDEV:
     case OVS_VPORT_TYPE_INTERNAL:
     case OVS_VPORT_TYPE_LISP: