2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/flow_dissector.h>
34 #include <net/pkt_cls.h>
35 #include <net/tc_act/tc_gact.h>
36 #include <net/tc_act/tc_skbedit.h>
37 #include <linux/mlx5/fs.h>
38 #include <linux/mlx5/device.h>
39 #include <linux/rhashtable.h>
40 #include <net/switchdev.h>
41 #include <net/tc_act/tc_mirred.h>
42 #include <net/tc_act/tc_vlan.h>
47 struct mlx5e_tc_flow {
48 struct rhash_head node;
50 struct mlx5_flow_rule *rule;
51 struct mlx5_esw_flow_attr *attr;
54 #define MLX5E_TC_TABLE_NUM_ENTRIES 1024
55 #define MLX5E_TC_TABLE_NUM_GROUPS 4
57 static struct mlx5_flow_rule *mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
58 struct mlx5_flow_spec *spec,
59 u32 action, u32 flow_tag)
61 struct mlx5_core_dev *dev = priv->mdev;
62 struct mlx5_flow_destination dest = { 0 };
63 struct mlx5_fc *counter = NULL;
64 struct mlx5_flow_rule *rule;
65 bool table_created = false;
67 if (action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
68 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
69 dest.ft = priv->fs.vlan.ft.t;
70 } else if (action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
71 counter = mlx5_fc_create(dev, true);
73 return ERR_CAST(counter);
75 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
76 dest.counter = counter;
79 if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
81 mlx5_create_auto_grouped_flow_table(priv->fs.ns,
83 MLX5E_TC_TABLE_NUM_ENTRIES,
84 MLX5E_TC_TABLE_NUM_GROUPS,
86 if (IS_ERR(priv->fs.tc.t)) {
87 netdev_err(priv->netdev,
88 "Failed to create tc offload table\n");
89 rule = ERR_CAST(priv->fs.tc.t);
96 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
97 rule = mlx5_add_flow_rule(priv->fs.tc.t, spec,
108 mlx5_destroy_flow_table(priv->fs.tc.t);
109 priv->fs.tc.t = NULL;
112 mlx5_fc_destroy(dev, counter);
117 static struct mlx5_flow_rule *mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
118 struct mlx5_flow_spec *spec,
119 struct mlx5_esw_flow_attr *attr)
121 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
124 err = mlx5_eswitch_add_vlan_action(esw, attr);
128 return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
131 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
132 struct mlx5_flow_rule *rule,
133 struct mlx5_esw_flow_attr *attr)
135 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
136 struct mlx5_fc *counter = NULL;
138 counter = mlx5_flow_rule_counter(rule);
140 if (esw && esw->mode == SRIOV_OFFLOADS)
141 mlx5_eswitch_del_vlan_action(esw, attr);
143 mlx5_del_flow_rule(rule);
145 mlx5_fc_destroy(priv->mdev, counter);
147 if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
148 mlx5_destroy_flow_table(priv->fs.tc.t);
149 priv->fs.tc.t = NULL;
153 static int parse_cls_flower(struct mlx5e_priv *priv, struct mlx5_flow_spec *spec,
154 struct tc_cls_flower_offload *f)
156 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
158 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
163 if (f->dissector->used_keys &
164 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
165 BIT(FLOW_DISSECTOR_KEY_BASIC) |
166 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
167 BIT(FLOW_DISSECTOR_KEY_VLAN) |
168 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
169 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
170 BIT(FLOW_DISSECTOR_KEY_PORTS))) {
171 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
172 f->dissector->used_keys);
176 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
177 struct flow_dissector_key_control *key =
178 skb_flow_dissector_target(f->dissector,
179 FLOW_DISSECTOR_KEY_CONTROL,
181 addr_type = key->addr_type;
184 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
185 struct flow_dissector_key_basic *key =
186 skb_flow_dissector_target(f->dissector,
187 FLOW_DISSECTOR_KEY_BASIC,
189 struct flow_dissector_key_basic *mask =
190 skb_flow_dissector_target(f->dissector,
191 FLOW_DISSECTOR_KEY_BASIC,
193 ip_proto = key->ip_proto;
195 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
196 ntohs(mask->n_proto));
197 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
198 ntohs(key->n_proto));
200 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
202 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
206 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
207 struct flow_dissector_key_eth_addrs *key =
208 skb_flow_dissector_target(f->dissector,
209 FLOW_DISSECTOR_KEY_ETH_ADDRS,
211 struct flow_dissector_key_eth_addrs *mask =
212 skb_flow_dissector_target(f->dissector,
213 FLOW_DISSECTOR_KEY_ETH_ADDRS,
216 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
219 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
223 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
226 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
231 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
232 struct flow_dissector_key_vlan *key =
233 skb_flow_dissector_target(f->dissector,
234 FLOW_DISSECTOR_KEY_VLAN,
236 struct flow_dissector_key_vlan *mask =
237 skb_flow_dissector_target(f->dissector,
238 FLOW_DISSECTOR_KEY_VLAN,
240 if (mask->vlan_id || mask->vlan_priority) {
241 MLX5_SET(fte_match_set_lyr_2_4, headers_c, vlan_tag, 1);
242 MLX5_SET(fte_match_set_lyr_2_4, headers_v, vlan_tag, 1);
244 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
245 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
247 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
248 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
252 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
253 struct flow_dissector_key_ipv4_addrs *key =
254 skb_flow_dissector_target(f->dissector,
255 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
257 struct flow_dissector_key_ipv4_addrs *mask =
258 skb_flow_dissector_target(f->dissector,
259 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
262 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
263 src_ipv4_src_ipv6.ipv4_layout.ipv4),
264 &mask->src, sizeof(mask->src));
265 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
266 src_ipv4_src_ipv6.ipv4_layout.ipv4),
267 &key->src, sizeof(key->src));
268 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
269 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
270 &mask->dst, sizeof(mask->dst));
271 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
272 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
273 &key->dst, sizeof(key->dst));
276 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
277 struct flow_dissector_key_ipv6_addrs *key =
278 skb_flow_dissector_target(f->dissector,
279 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
281 struct flow_dissector_key_ipv6_addrs *mask =
282 skb_flow_dissector_target(f->dissector,
283 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
286 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
287 src_ipv4_src_ipv6.ipv6_layout.ipv6),
288 &mask->src, sizeof(mask->src));
289 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
290 src_ipv4_src_ipv6.ipv6_layout.ipv6),
291 &key->src, sizeof(key->src));
293 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
294 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
295 &mask->dst, sizeof(mask->dst));
296 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
297 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
298 &key->dst, sizeof(key->dst));
301 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
302 struct flow_dissector_key_ports *key =
303 skb_flow_dissector_target(f->dissector,
304 FLOW_DISSECTOR_KEY_PORTS,
306 struct flow_dissector_key_ports *mask =
307 skb_flow_dissector_target(f->dissector,
308 FLOW_DISSECTOR_KEY_PORTS,
312 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
313 tcp_sport, ntohs(mask->src));
314 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
315 tcp_sport, ntohs(key->src));
317 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
318 tcp_dport, ntohs(mask->dst));
319 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
320 tcp_dport, ntohs(key->dst));
324 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
325 udp_sport, ntohs(mask->src));
326 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
327 udp_sport, ntohs(key->src));
329 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
330 udp_dport, ntohs(mask->dst));
331 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
332 udp_dport, ntohs(key->dst));
335 netdev_err(priv->netdev,
336 "Only UDP and TCP transport are supported\n");
344 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
345 u32 *action, u32 *flow_tag)
347 const struct tc_action *a;
350 if (tc_no_actions(exts))
353 *flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
356 tcf_exts_to_list(exts, &actions);
357 list_for_each_entry(a, &actions, list) {
358 /* Only support a single action per rule */
362 if (is_tcf_gact_shot(a)) {
363 *action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
364 if (MLX5_CAP_FLOWTABLE(priv->mdev,
365 flow_table_properties_nic_receive.flow_counter))
366 *action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
370 if (is_tcf_skbedit_mark(a)) {
371 u32 mark = tcf_skbedit_mark(a);
373 if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
374 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
380 *action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
390 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
391 struct mlx5_esw_flow_attr *attr)
393 const struct tc_action *a;
396 if (tc_no_actions(exts))
399 memset(attr, 0, sizeof(*attr));
400 attr->in_rep = priv->ppriv;
402 tcf_exts_to_list(exts, &actions);
403 list_for_each_entry(a, &actions, list) {
404 if (is_tcf_gact_shot(a)) {
405 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
406 MLX5_FLOW_CONTEXT_ACTION_COUNT;
410 if (is_tcf_mirred_redirect(a)) {
411 int ifindex = tcf_mirred_ifindex(a);
412 struct net_device *out_dev;
413 struct mlx5e_priv *out_priv;
415 out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
417 if (!switchdev_port_same_parent_id(priv->netdev, out_dev)) {
418 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
419 priv->netdev->name, out_dev->name);
423 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
424 out_priv = netdev_priv(out_dev);
425 attr->out_rep = out_priv->ppriv;
429 if (is_tcf_vlan(a)) {
430 if (tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
431 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
432 } else if (tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
433 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
436 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
437 attr->vlan = tcf_vlan_push_vid(a);
438 } else { /* action is TCA_VLAN_ACT_MODIFY */
449 int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
450 struct tc_cls_flower_offload *f)
452 struct mlx5e_tc_table *tc = &priv->fs.tc;
454 bool fdb_flow = false;
455 u32 flow_tag, action;
456 struct mlx5e_tc_flow *flow;
457 struct mlx5_flow_spec *spec;
458 struct mlx5_flow_rule *old = NULL;
459 struct mlx5_esw_flow_attr *old_attr = NULL;
460 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
462 if (esw && esw->mode == SRIOV_OFFLOADS)
465 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
469 old_attr = flow->attr;
472 flow = kzalloc(sizeof(*flow) + sizeof(struct mlx5_esw_flow_attr),
475 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
478 spec = mlx5_vzalloc(sizeof(*spec));
479 if (!spec || !flow) {
484 flow->cookie = f->cookie;
486 err = parse_cls_flower(priv, spec, f);
491 flow->attr = (struct mlx5_esw_flow_attr *)(flow + 1);
492 err = parse_tc_fdb_actions(priv, f->exts, flow->attr);
495 flow->rule = mlx5e_tc_add_fdb_flow(priv, spec, flow->attr);
497 err = parse_tc_nic_actions(priv, f->exts, &action, &flow_tag);
500 flow->rule = mlx5e_tc_add_nic_flow(priv, spec, action, flow_tag);
503 if (IS_ERR(flow->rule)) {
504 err = PTR_ERR(flow->rule);
508 err = rhashtable_insert_fast(&tc->ht, &flow->node,
514 mlx5e_tc_del_flow(priv, old, old_attr);
519 mlx5_del_flow_rule(flow->rule);
529 int mlx5e_delete_flower(struct mlx5e_priv *priv,
530 struct tc_cls_flower_offload *f)
532 struct mlx5e_tc_flow *flow;
533 struct mlx5e_tc_table *tc = &priv->fs.tc;
535 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
540 rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
542 mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
549 int mlx5e_stats_flower(struct mlx5e_priv *priv,
550 struct tc_cls_flower_offload *f)
552 struct mlx5e_tc_table *tc = &priv->fs.tc;
553 struct mlx5e_tc_flow *flow;
555 struct mlx5_fc *counter;
561 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
566 counter = mlx5_flow_rule_counter(flow->rule);
570 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
574 tcf_exts_to_list(f->exts, &actions);
575 list_for_each_entry(a, &actions, list)
576 tcf_action_stats_update(a, bytes, packets, lastuse);
583 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
584 .head_offset = offsetof(struct mlx5e_tc_flow, node),
585 .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
586 .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
587 .automatic_shrinking = true,
590 int mlx5e_tc_init(struct mlx5e_priv *priv)
592 struct mlx5e_tc_table *tc = &priv->fs.tc;
594 tc->ht_params = mlx5e_tc_flow_ht_params;
595 return rhashtable_init(&tc->ht, &tc->ht_params);
598 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
600 struct mlx5e_tc_flow *flow = ptr;
601 struct mlx5e_priv *priv = arg;
603 mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
607 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
609 struct mlx5e_tc_table *tc = &priv->fs.tc;
611 rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
613 if (!IS_ERR_OR_NULL(tc->t)) {
614 mlx5_destroy_flow_table(tc->t);