Skip to content

Commit d8f9dfa

Browse files
lxindavem330
authored andcommitted
net: sched: allow flower to match vxlan options
This patch is to allow matching gbp option in vxlan. The options can be described in the form GBP/GBP_MASK, where GBP is represented as a 32bit hexadecimal value. Different from geneve, only one option can be set. And also, geneve options and vxlan options can't be set at the same time. # ip link add name vxlan0 type vxlan dstport 0 external # tc qdisc add dev vxlan0 ingress # tc filter add dev vxlan0 protocol ip parent ffff: \ flower \ enc_src_ip 10.0.99.192 \ enc_dst_ip 10.0.99.193 \ enc_key_id 11 \ vxlan_opts 01020304/ffffffff \ ip_proto udp \ action mirred egress redirect dev eth0 v1->v2: - add .strict_start_type for enc_opts_policy as Jakub noticed. - use Duplicate instead of Wrong in err msg for extack as Jakub suggested. Signed-off-by: Xin Long <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e20d4ff commit d8f9dfa

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

include/uapi/linux/pkt_cls.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ enum {
571571
* TCA_FLOWER_KEY_ENC_OPT_GENEVE_
572572
* attributes
573573
*/
574+
TCA_FLOWER_KEY_ENC_OPTS_VXLAN, /* Nested
575+
* TCA_FLOWER_KEY_ENC_OPT_VXLAN_
576+
* attributes
577+
*/
574578
__TCA_FLOWER_KEY_ENC_OPTS_MAX,
575579
};
576580

@@ -588,6 +592,15 @@ enum {
588592
#define TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX \
589593
(__TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX - 1)
590594

595+
enum {
596+
TCA_FLOWER_KEY_ENC_OPT_VXLAN_UNSPEC,
597+
TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, /* u32 */
598+
__TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX,
599+
};
600+
601+
#define TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX \
602+
(__TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX - 1)
603+
591604
enum {
592605
TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT = (1 << 0),
593606
TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST = (1 << 1),

net/sched/cls_flower.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <net/ip.h>
2323
#include <net/flow_dissector.h>
2424
#include <net/geneve.h>
25+
#include <net/vxlan.h>
2526

2627
#include <net/dst.h>
2728
#include <net/dst_metadata.h>
@@ -688,7 +689,10 @@ static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
688689

689690
static const struct nla_policy
690691
enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
692+
[TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = {
693+
.strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN },
691694
[TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
695+
[TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
692696
};
693697

694698
static const struct nla_policy
@@ -699,6 +703,11 @@ geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
699703
.len = 128 },
700704
};
701705

706+
static const struct nla_policy
707+
vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = {
708+
[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
709+
};
710+
702711
static void fl_set_key_val(struct nlattr **tb,
703712
void *val, int val_type,
704713
void *mask, int mask_type, int len)
@@ -928,6 +937,41 @@ static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
928937
return sizeof(struct geneve_opt) + data_len;
929938
}
930939

940+
static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
941+
int depth, int option_len,
942+
struct netlink_ext_ack *extack)
943+
{
944+
struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
945+
struct vxlan_metadata *md;
946+
int err;
947+
948+
md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
949+
memset(md, 0xff, sizeof(*md));
950+
951+
if (!depth)
952+
return sizeof(*md);
953+
954+
if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
955+
NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
956+
return -EINVAL;
957+
}
958+
959+
err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
960+
vxlan_opt_policy, extack);
961+
if (err < 0)
962+
return err;
963+
964+
if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
965+
NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
966+
return -EINVAL;
967+
}
968+
969+
if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP])
970+
md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
971+
972+
return sizeof(*md);
973+
}
974+
931975
static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
932976
struct fl_flow_key *mask,
933977
struct netlink_ext_ack *extack)
@@ -958,6 +1002,11 @@ static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
9581002
nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
9591003
switch (nla_type(nla_opt_key)) {
9601004
case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
1005+
if (key->enc_opts.dst_opt_type &&
1006+
key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) {
1007+
NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
1008+
return -EINVAL;
1009+
}
9611010
option_len = 0;
9621011
key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
9631012
option_len = fl_set_geneve_opt(nla_opt_key, key,
@@ -983,6 +1032,39 @@ static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
9831032
return -EINVAL;
9841033
}
9851034

1035+
if (msk_depth)
1036+
nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1037+
break;
1038+
case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
1039+
if (key->enc_opts.dst_opt_type) {
1040+
NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
1041+
return -EINVAL;
1042+
}
1043+
option_len = 0;
1044+
key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1045+
option_len = fl_set_vxlan_opt(nla_opt_key, key,
1046+
key_depth, option_len,
1047+
extack);
1048+
if (option_len < 0)
1049+
return option_len;
1050+
1051+
key->enc_opts.len += option_len;
1052+
/* At the same time we need to parse through the mask
1053+
* in order to verify exact and mask attribute lengths.
1054+
*/
1055+
mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1056+
option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
1057+
msk_depth, option_len,
1058+
extack);
1059+
if (option_len < 0)
1060+
return option_len;
1061+
1062+
mask->enc_opts.len += option_len;
1063+
if (key->enc_opts.len != mask->enc_opts.len) {
1064+
NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1065+
return -EINVAL;
1066+
}
1067+
9861068
if (msk_depth)
9871069
nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
9881070
break;
@@ -2135,6 +2217,28 @@ static int fl_dump_key_geneve_opt(struct sk_buff *skb,
21352217
return -EMSGSIZE;
21362218
}
21372219

2220+
static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
2221+
struct flow_dissector_key_enc_opts *enc_opts)
2222+
{
2223+
struct vxlan_metadata *md;
2224+
struct nlattr *nest;
2225+
2226+
nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
2227+
if (!nest)
2228+
goto nla_put_failure;
2229+
2230+
md = (struct vxlan_metadata *)&enc_opts->data[0];
2231+
if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
2232+
goto nla_put_failure;
2233+
2234+
nla_nest_end(skb, nest);
2235+
return 0;
2236+
2237+
nla_put_failure:
2238+
nla_nest_cancel(skb, nest);
2239+
return -EMSGSIZE;
2240+
}
2241+
21382242
static int fl_dump_key_ct(struct sk_buff *skb,
21392243
struct flow_dissector_key_ct *key,
21402244
struct flow_dissector_key_ct *mask)
@@ -2188,6 +2292,11 @@ static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
21882292
if (err)
21892293
goto nla_put_failure;
21902294
break;
2295+
case TUNNEL_VXLAN_OPT:
2296+
err = fl_dump_key_vxlan_opt(skb, enc_opts);
2297+
if (err)
2298+
goto nla_put_failure;
2299+
break;
21912300
default:
21922301
goto nla_put_failure;
21932302
}

0 commit comments

Comments
 (0)