Skip to content

Commit

Permalink
lib/tc: add ICMP type and code match offload
Browse files Browse the repository at this point in the history
Add TC offload support for classifying ICMPv4/6 type and code.

$ ovs-appctl dpctl/add-flow 'recirc_id(0),in_port(3),eth(),\
  eth_type(0x0800),ipv4(proto=1),icmp(type=9,code=0)' 2

$ ovs-appctl dpctl/dump-flows
  ... icmp(type=9,code=0) ...

$ tc filter show dev <ethx> ingress
  ...
  eth_type ipv4
  ip_proto icmp
  icmp_type 9
  icmp_code 0
  not_in_hw
  action order 1: mirred (Egress Redirect to device <ethy>) stolen
  ...

Signed-off-by: Maor Dickman <[email protected]>
Reviewed-by: Roi Dayan <[email protected]>
Signed-off-by: Simon Horman <[email protected]>
  • Loading branch information
dickmanmaor authored and shorman-netronome committed Feb 1, 2021
1 parent d5c429a commit 75e1e6f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
38 changes: 14 additions & 24 deletions lib/netdev-offload-tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,12 @@ parse_tc_flower_to_match(struct tc_flower *flower,
} else if (key->ip_proto == IPPROTO_SCTP) {
match_set_tp_dst_masked(match, key->sctp_dst, mask->sctp_dst);
match_set_tp_src_masked(match, key->sctp_src, mask->sctp_src);
} else if (key->ip_proto == IPPROTO_ICMP ||
key->ip_proto == IPPROTO_ICMPV6) {
match_set_tp_dst_masked(match, htons(key->icmp_code),
htons(mask->icmp_code));
match_set_tp_src_masked(match, htons(key->icmp_type),
htons(mask->icmp_type));
}

if (mask->ct_state) {
Expand Down Expand Up @@ -1329,18 +1335,6 @@ test_key_and_mask(struct match *match)
}

if (key->dl_type == htons(ETH_TYPE_IP) &&
key->nw_proto == IPPROTO_ICMP) {
if (mask->tp_src) {
VLOG_DBG_RL(&rl,
"offloading attribute icmp_type isn't supported");
return EOPNOTSUPP;
}
if (mask->tp_dst) {
VLOG_DBG_RL(&rl,
"offloading attribute icmp_code isn't supported");
return EOPNOTSUPP;
}
} else if (key->dl_type == htons(ETH_TYPE_IP) &&
key->nw_proto == IPPROTO_IGMP) {
if (mask->tp_src) {
VLOG_DBG_RL(&rl,
Expand All @@ -1352,18 +1346,6 @@ test_key_and_mask(struct match *match)
"offloading attribute igmp_code isn't supported");
return EOPNOTSUPP;
}
} else if (key->dl_type == htons(ETH_TYPE_IPV6) &&
key->nw_proto == IPPROTO_ICMPV6) {
if (mask->tp_src) {
VLOG_DBG_RL(&rl,
"offloading attribute icmpv6_type isn't supported");
return EOPNOTSUPP;
}
if (mask->tp_dst) {
VLOG_DBG_RL(&rl,
"offloading attribute icmpv6_code isn't supported");
return EOPNOTSUPP;
}
} else if (key->dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)) {
VLOG_DBG_RL(&rl,
"offloading of non-ethernet packets isn't supported");
Expand Down Expand Up @@ -1631,6 +1613,14 @@ netdev_tc_flow_put(struct netdev *netdev, struct match *match,
flower.mask.sctp_src = mask->tp_src;
mask->tp_src = 0;
mask->tp_dst = 0;
} else if (key->nw_proto == IPPROTO_ICMP ||
key->nw_proto == IPPROTO_ICMPV6) {
flower.key.icmp_code = (uint8_t) ntohs(key->tp_dst);
flower.mask.icmp_code = (uint8_t) ntohs (mask->tp_dst);
flower.key.icmp_type = (uint8_t) ntohs(key->tp_src);
flower.mask.icmp_type = (uint8_t) ntohs(mask->tp_src);
mask->tp_src = 0;
mask->tp_dst = 0;
}

if (key->dl_type == htons(ETH_P_IP)) {
Expand Down
48 changes: 48 additions & 0 deletions lib/tc.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,22 @@ static const struct nl_policy tca_flower_policy[] = {
[TCA_FLOWER_KEY_CT_LABELS] = { .type = NL_A_U128, .optional = true, },
[TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NL_A_U128,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NL_A_U8,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NL_A_U8,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NL_A_U8,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NL_A_U8,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NL_A_U8,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NL_A_U8,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NL_A_U8,
.optional = true, },
[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NL_A_U8,
.optional = true, },
};

static const struct nl_policy tca_flower_terse_policy[] = {
Expand Down Expand Up @@ -908,6 +924,32 @@ nl_parse_flower_ip(struct nlattr **attrs, struct tc_flower *flower) {
mask->sctp_dst =
nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_DST_MASK]);
}
} else if (ip_proto == IPPROTO_ICMP) {
if (attrs[TCA_FLOWER_KEY_ICMPV4_CODE_MASK]) {
key->icmp_code =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV4_CODE]);
mask->icmp_code =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV4_CODE]);
}
if (attrs[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK]) {
key->icmp_type =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK]);
mask->icmp_type =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK]);
}
} else if (ip_proto == IPPROTO_ICMPV6) {
if (attrs[TCA_FLOWER_KEY_ICMPV6_CODE_MASK]) {
key->icmp_code =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV6_CODE]);
mask->icmp_code =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV6_CODE]);
}
if (attrs[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK]) {
key->icmp_type =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK]);
mask->icmp_type =
nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK]);
}
}

if (attrs[TCA_FLOWER_KEY_IP_TTL_MASK]) {
Expand Down Expand Up @@ -2812,6 +2854,12 @@ nl_msg_put_flower_options(struct ofpbuf *request, struct tc_flower *flower)
} else if (flower->key.ip_proto == IPPROTO_SCTP) {
FLOWER_PUT_MASKED_VALUE(sctp_src, TCA_FLOWER_KEY_SCTP_SRC);
FLOWER_PUT_MASKED_VALUE(sctp_dst, TCA_FLOWER_KEY_SCTP_DST);
} else if (flower->key.ip_proto == IPPROTO_ICMP) {
FLOWER_PUT_MASKED_VALUE(icmp_code, TCA_FLOWER_KEY_ICMPV4_CODE);
FLOWER_PUT_MASKED_VALUE(icmp_type, TCA_FLOWER_KEY_ICMPV4_TYPE);
} else if (flower->key.ip_proto == IPPROTO_ICMPV6) {
FLOWER_PUT_MASKED_VALUE(icmp_code, TCA_FLOWER_KEY_ICMPV6_CODE);
FLOWER_PUT_MASKED_VALUE(icmp_type, TCA_FLOWER_KEY_ICMPV6_TYPE);
}

FLOWER_PUT_MASKED_VALUE(ct_state, TCA_FLOWER_KEY_CT_STATE);
Expand Down
3 changes: 3 additions & 0 deletions lib/tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ struct tc_flower_key {
ovs_be16 sctp_src;
ovs_be16 sctp_dst;

uint8_t icmp_code;
uint8_t icmp_type;

uint16_t vlan_id[FLOW_MAX_VLAN_HEADERS];
uint8_t vlan_prio[FLOW_MAX_VLAN_HEADERS];

Expand Down

0 comments on commit 75e1e6f

Please sign in to comment.