dpif-netlink: add GRE creation support
[cascardo/ovs.git] / lib / dpif-netlink.c
index b8b908f..5f1b867 100644 (file)
@@ -25,6 +25,7 @@
 #include <net/if.h>
 #include <linux/types.h>
 #include <linux/ip.h>
+#include <linux/if_tunnel.h>
 #include <linux/pkt_sched.h>
 #include <linux/rtnetlink.h>
 #include <poll.h>
@@ -963,6 +964,12 @@ netdev_vxlan_destroy(const char *name)
     return netdev_linux_destroy(name);
 }
 
+static int
+netdev_gre_destroy(const char *name)
+{
+    return netdev_linux_destroy(name);
+}
+
 /*
  * On some older systems, these enums are not defined.
  */
@@ -977,6 +984,10 @@ netdev_vxlan_destroy(const char *name)
 #define IFLA_VXLAN_COLLECT_METADATA 25
 #endif
 
+#if IFLA_GRE_MAX < 18
+#define IFLA_GRE_COLLECT_METADATA 18
+#endif
+
 static int
 netdev_vxlan_create(struct netdev *netdev)
 {
@@ -1031,6 +1042,115 @@ netdev_vxlan_create(struct netdev *netdev)
     return err;
 }
 
+/*
+ * On some Linux versions, creating the device with IFLA_GRE_COLLECT_METADATA
+ * will succeed, even though that attribute is not supported. We need to verify
+ * the device has been created with that attribute. In case it has not, we
+ * destroy it and use the compat code.
+ */
+static int
+netdev_gre_verify(const char *name)
+{
+    int err;
+    struct ofpbuf request, *reply;
+    struct ifinfomsg *ifmsg;
+
+    static const struct nl_policy rtlink_policy[] = {
+        [IFLA_LINKINFO] = { .type = NL_A_NESTED },
+    };
+    static const struct nl_policy linkinfo_policy[] = {
+        [IFLA_INFO_KIND] = { .type = NL_A_STRING },
+        [IFLA_INFO_DATA] = { .type = NL_A_NESTED },
+    };
+    static const struct nl_policy gre_policy[] = {
+        [IFLA_GRE_COLLECT_METADATA] = { .type = NL_A_FLAG },
+    };
+
+    ofpbuf_init(&request, 0);
+    nl_msg_put_nlmsghdr(&request, 0, RTM_GETLINK,
+                        NLM_F_REQUEST);
+    ofpbuf_put_zeros(&request, sizeof(struct ifinfomsg));
+    nl_msg_put_string(&request, IFLA_IFNAME, name);
+
+    err = nl_transact(NETLINK_ROUTE, &request, &reply);
+    if (!err) {
+        struct nlattr *rtlink[ARRAY_SIZE(rtlink_policy)];
+        struct nlattr *linkinfo[ARRAY_SIZE(linkinfo_policy)];
+        struct nlattr *gre[ARRAY_SIZE(gre_policy)];
+
+        err = EINVAL;
+        ifmsg = ofpbuf_at(reply, NLMSG_HDRLEN, sizeof *ifmsg);
+        if (nl_policy_parse(reply, NLMSG_HDRLEN + sizeof *ifmsg,
+            rtlink_policy, rtlink, ARRAY_SIZE(rtlink_policy))) {
+            if (nl_parse_nested(rtlink[IFLA_LINKINFO], linkinfo_policy,
+                linkinfo, ARRAY_SIZE(linkinfo_policy)) &&
+                !strcmp(nl_attr_get_string(linkinfo[IFLA_INFO_KIND]),
+                        "gretap")) {
+                if (nl_parse_nested(linkinfo[IFLA_INFO_DATA], gre_policy, gre,
+                    ARRAY_SIZE(gre_policy)) &&
+                    nl_attr_get_flag(gre[IFLA_GRE_COLLECT_METADATA])) {
+                        err = 0;
+                }
+            }
+        }
+        ofpbuf_uninit(reply);
+    }
+    ofpbuf_uninit(&request);
+    return err;
+}
+
+static int
+netdev_gre_create(struct netdev *netdev)
+{
+    int err;
+    struct ofpbuf request, *reply;
+    size_t linkinfo_off, infodata_off;
+    char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
+    const char *name = netdev_vport_get_dpif_port(netdev,
+                                                  namebuf, sizeof namebuf);
+    struct ifinfomsg *ifinfo;
+    const struct netdev_tunnel_config *tnl_cfg;
+    tnl_cfg = netdev_get_tunnel_config(netdev);
+    if (!tnl_cfg) { /* or assert? */
+        return EINVAL;
+    }
+
+    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, "gretap");
+        infodata_off = nl_msg_start_nested(&request, IFLA_INFO_DATA);
+            nl_msg_put_flag(&request, IFLA_GRE_COLLECT_METADATA);
+        nl_msg_end_nested(&request, infodata_off);
+    nl_msg_end_nested(&request, linkinfo_off);
+
+    err = nl_transact(NETLINK_ROUTE, &request, &reply);
+
+    if (!err) {
+        ofpbuf_uninit(reply);
+    }
+
+    if (!err && (err = netdev_gre_verify(name))) {
+        netdev_gre_destroy(name);
+    }
+
+    /*
+     * If tunnel metadata is not supported, EEXIST will be returned for zero
+     * addresses tunnel. We still need to verify metadata has been set as above.
+     */
+    if (err == EINVAL || err == EEXIST) {
+        err = EOPNOTSUPP;
+    }
+
+    ofpbuf_uninit(&request);
+    return err;
+}
+
 #else
 
 static int
@@ -1039,12 +1159,24 @@ netdev_vxlan_create(struct netdev *netdev OVS_UNUSED)
     return EOPNOTSUPP;
 }
 
+static int
+netdev_gre_create(struct netdev *netdev OVS_UNUSED)
+{
+    return EOPNOTSUPP;
+}
+
 static int
 netdev_vxlan_destroy(const char *name OVS_UNUSED)
 {
     return EOPNOTSUPP;
 }
 
+static int
+netdev_gre_destroy(const char *name OVS_UNUSED)
+{
+    return EOPNOTSUPP;
+}
+
 #endif
 
 static int
@@ -1058,6 +1190,7 @@ dpif_netlink_port_create(struct netdev *netdev)
     case OVS_VPORT_TYPE_VXLAN:
         return netdev_vxlan_create(netdev);
     case OVS_VPORT_TYPE_GRE:
+        return netdev_gre_create(netdev);
     case OVS_VPORT_TYPE_GENEVE:
     case OVS_VPORT_TYPE_NETDEV:
     case OVS_VPORT_TYPE_INTERNAL:
@@ -1078,6 +1211,7 @@ dpif_netlink_port_destroy(const char *name, const char *type)
     case OVS_VPORT_TYPE_VXLAN:
         return netdev_vxlan_destroy(name);
     case OVS_VPORT_TYPE_GRE:
+        return netdev_gre_destroy(name);
     case OVS_VPORT_TYPE_GENEVE:
     case OVS_VPORT_TYPE_NETDEV:
     case OVS_VPORT_TYPE_INTERNAL: