[ovs-dev,v3,2/2] lib/tc: add geneve with option match offload

Message ID 1536173557-23165-3-git-send-email-pieter.jansenvanvuuren@netronome.com
State New
Headers show
Series
  • add TC offload support for geneve with options
Related show

Commit Message

Pieter Jansen van Vuuren Sept. 5, 2018, 6:52 p.m.
Add TC offload support for classifying geneve tunnels with options.

Signed-off-by: Pieter Jansen van Vuuren <pieter.jansenvanvuuren@netronome.com>
Reviewed-by: Simon Horman <simon.horman@netronome.com>
---
 include/linux/pkt_cls.h  | 25 +++++++++++
 lib/netdev-tc-offloads.c | 14 ++++++
 lib/tc.c                 | 96 ++++++++++++++++++++++++++++++++++++++++
 lib/tc.h                 |  1 +
 4 files changed, 136 insertions(+)

Patch

diff --git a/include/linux/pkt_cls.h b/include/linux/pkt_cls.h
index a3300418e..1384d71f9 100644
--- a/include/linux/pkt_cls.h
+++ b/include/linux/pkt_cls.h
@@ -204,10 +204,35 @@  enum {
 	TCA_FLOWER_KEY_ENC_IP_TOS_MASK,	/* u8 */
 	TCA_FLOWER_KEY_ENC_IP_TTL,		/* u8 */
 	TCA_FLOWER_KEY_ENC_IP_TTL_MASK,	/* u8 */
+	TCA_FLOWER_KEY_ENC_OPTS,
+	TCA_FLOWER_KEY_ENC_OPTS_MASK,
 
 	__TCA_FLOWER_MAX,
 };
 
+enum {
+	TCA_FLOWER_KEY_ENC_OPTS_UNSPEC,
+	TCA_FLOWER_KEY_ENC_OPTS_GENEVE, /* Nested
+					 * TCA_TUNNEL_KEY_ENC_OPTS_GENEVE
+					 * attributes
+					 */
+	__TCA_FLOWER_KEY_ENC_OPTS_MAX,
+};
+
+#define TCA_FLOWER_KEY_ENC_OPTS_MAX (__TCA_FLOWER_KEY_ENC_OPTS_MAX - 1)
+
+enum {
+	TCA_FLOWER_KEY_ENC_OPT_GENEVE_UNSPEC,
+	TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,            /* u16 */
+	TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,             /* u8 */
+	TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,             /* 4 to 128 bytes */
+
+	__TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
+};
+
+#define TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX \
+		(__TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX - 1)
+
 enum {
 	TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT = (1 << 0),
 	TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST = (1 << 1),
diff --git a/lib/netdev-tc-offloads.c b/lib/netdev-tc-offloads.c
index d07dcb2aa..b84c88c31 100644
--- a/lib/netdev-tc-offloads.c
+++ b/lib/netdev-tc-offloads.c
@@ -542,6 +542,17 @@  parse_tc_flower_to_match(struct tc_flower *flower,
         if (flower->tunnel.tp_dst) {
             match_set_tun_tp_dst(match, flower->tunnel.tp_dst);
         }
+        memcpy(match->flow.tunnel.metadata.opts.gnv,
+               flower->tunnel.metadata.opts.gnv,
+               flower->tunnel.metadata.present.len);
+        match->flow.tunnel.metadata.present.len =
+               flower->tunnel.metadata.present.len;
+        match->flow.tunnel.flags |= FLOW_TNL_F_UDPIF;
+        memset(match->wc.masks.tunnel.metadata.opts.gnv, 0xff,
+               flower->tunnel.metadata.present.len);
+        match->wc.masks.tunnel.metadata.present.len =
+               flower->tunnel.metadata.present.len;
+        match->wc.masks.tunnel.flags |= FLOW_TNL_F_UDPIF;
     }
 
     act_off = nl_msg_start_nested(buf, OVS_FLOW_ATTR_ACTIONS);
@@ -1003,6 +1014,9 @@  netdev_tc_flow_put(struct netdev *netdev, struct match *match,
         flower.tunnel.ttl = tnl->ip_ttl;
         flower.tunnel.tp_src = tnl->tp_src;
         flower.tunnel.tp_dst = tnl->tp_dst;
+        memcpy(flower.tunnel.metadata.opts.gnv, tnl->metadata.opts.gnv,
+               tnl->metadata.present.len);
+        flower.tunnel.metadata.present.len = tnl->metadata.present.len;
         flower.tunnel.tunnel = true;
     }
     memset(&mask->tunnel, 0, sizeof mask->tunnel);
diff --git a/lib/tc.c b/lib/tc.c
index 57c4e067b..8334d42aa 100644
--- a/lib/tc.c
+++ b/lib/tc.c
@@ -321,6 +321,9 @@  static const struct nl_policy tca_flower_policy[] = {
                                     .optional = true, },
     [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NL_A_U8,
                                          .optional = true, },
+    [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NL_A_NESTED, .optional = true, },
+    [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NL_A_NESTED,
+                                       .optional = true, },
 };
 
 static void
@@ -387,6 +390,64 @@  nl_parse_flower_vlan(struct nlattr **attrs, struct tc_flower *flower)
     }
 }
 
+static void
+nl_parse_geneve_key(const struct nlattr *in_nlattr, struct tc_flower *flower)
+{
+    struct geneve_opt *opt = NULL;
+    const struct ofpbuf *msg;
+    struct nlattr *nla;
+    struct ofpbuf buf;
+    size_t left;
+    int cnt;
+
+    nl_attr_get_nested(in_nlattr, &buf);
+    msg = &buf;
+
+    cnt = 0;
+    NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
+        uint16_t type = nl_attr_type(nla);
+
+        switch (type) {
+        case TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS:
+            opt = &flower->tunnel.metadata.opts.gnv[cnt];
+            opt->opt_class = nl_attr_get_be16(nla);
+            cnt += sizeof(struct geneve_opt) / 4;
+            flower->tunnel.metadata.present.len += sizeof(struct geneve_opt);
+            break;
+        case TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE:
+            opt->type = nl_attr_get_u8(nla);
+            break;
+        case TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA:
+            opt->length = nl_attr_get_size(nla) / 4;
+            memcpy(opt + 1, nl_attr_get_unspec(nla, 1), opt->length * 4);
+            cnt += opt->length;
+            flower->tunnel.metadata.present.len += opt->length * 4;
+            break;
+        }
+    }
+}
+
+static void
+nl_parse_flower_tunnel_opts(struct nlattr *options, struct tc_flower *flower)
+{
+    const struct ofpbuf *msg;
+    struct nlattr *nla;
+    struct ofpbuf buf;
+    size_t left;
+
+    nl_attr_get_nested(options, &buf);
+    msg = &buf;
+
+    NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
+        uint16_t type = nl_attr_type(nla);
+        switch (type) {
+        case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
+            nl_parse_geneve_key(nla, flower);
+            break;
+        }
+    }
+}
+
 static void
 nl_parse_flower_tunnel(struct nlattr **attrs, struct tc_flower *flower)
 {
@@ -423,6 +484,9 @@  nl_parse_flower_tunnel(struct nlattr **attrs, struct tc_flower *flower)
         flower->tunnel.ttl =
             nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TTL]);
     }
+    if (attrs[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
+         nl_parse_flower_tunnel_opts(attrs[TCA_FLOWER_KEY_ENC_OPTS], flower);
+    }
 }
 
 static void
@@ -1714,6 +1778,37 @@  nl_msg_put_masked_value(struct ofpbuf *request, uint16_t type,
     nl_msg_put_unspec(request, type, data, len);
 }
 
+static void
+nl_msg_put_flower_tunnel_opts(struct ofpbuf *request, struct tc_flower *flower)
+{
+    struct geneve_opt *opt;
+    size_t outer, inner;
+    int len, cnt = 0;
+
+    len = flower->tunnel.metadata.present.len;
+    if (!len) {
+        return;
+    }
+
+    outer = nl_msg_start_nested(request, TCA_FLOWER_KEY_ENC_OPTS);
+    while (len) {
+        opt = &flower->tunnel.metadata.opts.gnv[cnt];
+        inner = nl_msg_start_nested(request, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
+
+        nl_msg_put_be16(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
+                        opt->opt_class);
+        nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, opt->type);
+        nl_msg_put_unspec(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, opt + 1,
+                          opt->length * 4);
+
+        cnt += sizeof(struct geneve_opt) / 4 + opt->length;
+        len -= sizeof(struct geneve_opt) + opt->length * 4;
+
+        nl_msg_end_nested(request, inner);
+    }
+    nl_msg_end_nested(request, outer);
+}
+
 static void
 nl_msg_put_flower_tunnel(struct ofpbuf *request, struct tc_flower *flower)
 {
@@ -1741,6 +1836,7 @@  nl_msg_put_flower_tunnel(struct ofpbuf *request, struct tc_flower *flower)
     }
     nl_msg_put_be16(request, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, tp_dst);
     nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_KEY_ID, id);
+    nl_msg_put_flower_tunnel_opts(request, flower);
 }
 
 #define FLOWER_PUT_MASKED_VALUE(member, type) \
diff --git a/lib/tc.h b/lib/tc.h
index 4ae06eb27..acc6607da 100644
--- a/lib/tc.h
+++ b/lib/tc.h
@@ -190,6 +190,7 @@  struct tc_flower {
         ovs_be16 tp_src;
         ovs_be16 tp_dst;
         ovs_be64 id;
+        struct tun_metadata metadata;
     } tunnel;
 
     struct tc_cookie act_cookie;