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_handle *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_handle *
58 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
59 struct mlx5_flow_spec *spec,
60 u32 action, u32 flow_tag)
62 struct mlx5_core_dev *dev = priv->mdev;
63 struct mlx5_flow_destination dest = { 0 };
64 struct mlx5_flow_act flow_act = {
69 struct mlx5_fc *counter = NULL;
70 struct mlx5_flow_handle *rule;
71 bool table_created = false;
73 if (action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
74 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
75 dest.ft = priv->fs.vlan.ft.t;
76 } else if (action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
77 counter = mlx5_fc_create(dev, true);
79 return ERR_CAST(counter);
81 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
82 dest.counter = counter;
85 if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
87 mlx5_create_auto_grouped_flow_table(priv->fs.ns,
89 MLX5E_TC_TABLE_NUM_ENTRIES,
90 MLX5E_TC_TABLE_NUM_GROUPS,
92 if (IS_ERR(priv->fs.tc.t)) {
93 netdev_err(priv->netdev,
94 "Failed to create tc offload table\n");
95 rule = ERR_CAST(priv->fs.tc.t);
102 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
103 rule = mlx5_add_flow_rules(priv->fs.tc.t, spec, &flow_act, &dest, 1);
112 mlx5_destroy_flow_table(priv->fs.tc.t);
113 priv->fs.tc.t = NULL;
116 mlx5_fc_destroy(dev, counter);
121 static struct mlx5_flow_handle *
122 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
123 struct mlx5_flow_spec *spec,
124 struct mlx5_esw_flow_attr *attr)
126 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
129 err = mlx5_eswitch_add_vlan_action(esw, attr);
133 return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
136 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
137 struct mlx5_flow_handle *rule,
138 struct mlx5_esw_flow_attr *attr)
140 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
141 struct mlx5_fc *counter = NULL;
143 counter = mlx5_flow_rule_counter(rule);
145 if (esw && esw->mode == SRIOV_OFFLOADS)
146 mlx5_eswitch_del_vlan_action(esw, attr);
148 mlx5_del_flow_rules(rule);
150 mlx5_fc_destroy(priv->mdev, counter);
152 if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
153 mlx5_destroy_flow_table(priv->fs.tc.t);
154 priv->fs.tc.t = NULL;
158 static int parse_cls_flower(struct mlx5e_priv *priv, struct mlx5_flow_spec *spec,
159 struct tc_cls_flower_offload *f)
161 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
163 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
168 if (f->dissector->used_keys &
169 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
170 BIT(FLOW_DISSECTOR_KEY_BASIC) |
171 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
172 BIT(FLOW_DISSECTOR_KEY_VLAN) |
173 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
174 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
175 BIT(FLOW_DISSECTOR_KEY_PORTS))) {
176 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
177 f->dissector->used_keys);
181 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
182 struct flow_dissector_key_control *key =
183 skb_flow_dissector_target(f->dissector,
184 FLOW_DISSECTOR_KEY_CONTROL,
186 addr_type = key->addr_type;
189 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
190 struct flow_dissector_key_basic *key =
191 skb_flow_dissector_target(f->dissector,
192 FLOW_DISSECTOR_KEY_BASIC,
194 struct flow_dissector_key_basic *mask =
195 skb_flow_dissector_target(f->dissector,
196 FLOW_DISSECTOR_KEY_BASIC,
198 ip_proto = key->ip_proto;
200 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
201 ntohs(mask->n_proto));
202 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
203 ntohs(key->n_proto));
205 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
207 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
211 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
212 struct flow_dissector_key_eth_addrs *key =
213 skb_flow_dissector_target(f->dissector,
214 FLOW_DISSECTOR_KEY_ETH_ADDRS,
216 struct flow_dissector_key_eth_addrs *mask =
217 skb_flow_dissector_target(f->dissector,
218 FLOW_DISSECTOR_KEY_ETH_ADDRS,
221 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
224 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
228 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
231 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
236 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
237 struct flow_dissector_key_vlan *key =
238 skb_flow_dissector_target(f->dissector,
239 FLOW_DISSECTOR_KEY_VLAN,
241 struct flow_dissector_key_vlan *mask =
242 skb_flow_dissector_target(f->dissector,
243 FLOW_DISSECTOR_KEY_VLAN,
246 MLX5_SET(fte_match_set_lyr_2_4, headers_c, vlan_tag, 1);
247 MLX5_SET(fte_match_set_lyr_2_4, headers_v, vlan_tag, 1);
249 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
250 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
254 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
255 struct flow_dissector_key_ipv4_addrs *key =
256 skb_flow_dissector_target(f->dissector,
257 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
259 struct flow_dissector_key_ipv4_addrs *mask =
260 skb_flow_dissector_target(f->dissector,
261 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
264 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
265 src_ipv4_src_ipv6.ipv4_layout.ipv4),
266 &mask->src, sizeof(mask->src));
267 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
268 src_ipv4_src_ipv6.ipv4_layout.ipv4),
269 &key->src, sizeof(key->src));
270 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
271 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
272 &mask->dst, sizeof(mask->dst));
273 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
274 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
275 &key->dst, sizeof(key->dst));
278 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
279 struct flow_dissector_key_ipv6_addrs *key =
280 skb_flow_dissector_target(f->dissector,
281 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
283 struct flow_dissector_key_ipv6_addrs *mask =
284 skb_flow_dissector_target(f->dissector,
285 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
288 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
289 src_ipv4_src_ipv6.ipv6_layout.ipv6),
290 &mask->src, sizeof(mask->src));
291 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
292 src_ipv4_src_ipv6.ipv6_layout.ipv6),
293 &key->src, sizeof(key->src));
295 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
296 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
297 &mask->dst, sizeof(mask->dst));
298 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
299 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
300 &key->dst, sizeof(key->dst));
303 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
304 struct flow_dissector_key_ports *key =
305 skb_flow_dissector_target(f->dissector,
306 FLOW_DISSECTOR_KEY_PORTS,
308 struct flow_dissector_key_ports *mask =
309 skb_flow_dissector_target(f->dissector,
310 FLOW_DISSECTOR_KEY_PORTS,
314 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
315 tcp_sport, ntohs(mask->src));
316 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
317 tcp_sport, ntohs(key->src));
319 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
320 tcp_dport, ntohs(mask->dst));
321 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
322 tcp_dport, ntohs(key->dst));
326 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
327 udp_sport, ntohs(mask->src));
328 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
329 udp_sport, ntohs(key->src));
331 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
332 udp_dport, ntohs(mask->dst));
333 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
334 udp_dport, ntohs(key->dst));
337 netdev_err(priv->netdev,
338 "Only UDP and TCP transport are supported\n");
346 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
347 u32 *action, u32 *flow_tag)
349 const struct tc_action *a;
352 if (tc_no_actions(exts))
355 *flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
358 tcf_exts_to_list(exts, &actions);
359 list_for_each_entry(a, &actions, list) {
360 /* Only support a single action per rule */
364 if (is_tcf_gact_shot(a)) {
365 *action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
366 if (MLX5_CAP_FLOWTABLE(priv->mdev,
367 flow_table_properties_nic_receive.flow_counter))
368 *action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
372 if (is_tcf_skbedit_mark(a)) {
373 u32 mark = tcf_skbedit_mark(a);
375 if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
376 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
382 *action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
392 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
393 struct mlx5_esw_flow_attr *attr)
395 const struct tc_action *a;
398 if (tc_no_actions(exts))
401 memset(attr, 0, sizeof(*attr));
402 attr->in_rep = priv->ppriv;
404 tcf_exts_to_list(exts, &actions);
405 list_for_each_entry(a, &actions, list) {
406 if (is_tcf_gact_shot(a)) {
407 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
408 MLX5_FLOW_CONTEXT_ACTION_COUNT;
412 if (is_tcf_mirred_egress_redirect(a)) {
413 int ifindex = tcf_mirred_ifindex(a);
414 struct net_device *out_dev;
415 struct mlx5e_priv *out_priv;
417 out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
419 if (!switchdev_port_same_parent_id(priv->netdev, out_dev)) {
420 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
421 priv->netdev->name, out_dev->name);
425 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
426 MLX5_FLOW_CONTEXT_ACTION_COUNT;
427 out_priv = netdev_priv(out_dev);
428 attr->out_rep = out_priv->ppriv;
432 if (is_tcf_vlan(a)) {
433 if (tcf_vlan_action(a) == VLAN_F_POP) {
434 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
435 } else if (tcf_vlan_action(a) == VLAN_F_PUSH) {
436 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
439 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
440 attr->vlan = tcf_vlan_push_vid(a);
450 int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
451 struct tc_cls_flower_offload *f)
453 struct mlx5e_tc_table *tc = &priv->fs.tc;
455 bool fdb_flow = false;
456 u32 flow_tag, action;
457 struct mlx5e_tc_flow *flow;
458 struct mlx5_flow_spec *spec;
459 struct mlx5_flow_handle *old = NULL;
460 struct mlx5_esw_flow_attr *old_attr = NULL;
461 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
463 if (esw && esw->mode == SRIOV_OFFLOADS)
466 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
470 old_attr = flow->attr;
473 flow = kzalloc(sizeof(*flow) + sizeof(struct mlx5_esw_flow_attr),
476 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
479 spec = mlx5_vzalloc(sizeof(*spec));
480 if (!spec || !flow) {
485 flow->cookie = f->cookie;
487 err = parse_cls_flower(priv, spec, f);
492 flow->attr = (struct mlx5_esw_flow_attr *)(flow + 1);
493 err = parse_tc_fdb_actions(priv, f->exts, flow->attr);
496 flow->rule = mlx5e_tc_add_fdb_flow(priv, spec, flow->attr);
498 err = parse_tc_nic_actions(priv, f->exts, &action, &flow_tag);
501 flow->rule = mlx5e_tc_add_nic_flow(priv, spec, action, flow_tag);
504 if (IS_ERR(flow->rule)) {
505 err = PTR_ERR(flow->rule);
509 err = rhashtable_insert_fast(&tc->ht, &flow->node,
515 mlx5e_tc_del_flow(priv, old, old_attr);
520 mlx5_del_flow_rules(flow->rule);
530 int mlx5e_delete_flower(struct mlx5e_priv *priv,
531 struct tc_cls_flower_offload *f)
533 struct mlx5e_tc_flow *flow;
534 struct mlx5e_tc_table *tc = &priv->fs.tc;
536 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
541 rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
543 mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
550 int mlx5e_stats_flower(struct mlx5e_priv *priv,
551 struct tc_cls_flower_offload *f)
553 struct mlx5e_tc_table *tc = &priv->fs.tc;
554 struct mlx5e_tc_flow *flow;
556 struct mlx5_fc *counter;
562 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
567 counter = mlx5_flow_rule_counter(flow->rule);
571 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
573 tcf_exts_to_list(f->exts, &actions);
574 list_for_each_entry(a, &actions, list)
575 tcf_action_stats_update(a, bytes, packets, lastuse);
580 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
581 .head_offset = offsetof(struct mlx5e_tc_flow, node),
582 .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
583 .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
584 .automatic_shrinking = true,
587 int mlx5e_tc_init(struct mlx5e_priv *priv)
589 struct mlx5e_tc_table *tc = &priv->fs.tc;
591 tc->ht_params = mlx5e_tc_flow_ht_params;
592 return rhashtable_init(&tc->ht, &tc->ht_params);
595 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
597 struct mlx5e_tc_flow *flow = ptr;
598 struct mlx5e_priv *priv = arg;
600 mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
604 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
606 struct mlx5e_tc_table *tc = &priv->fs.tc;
608 rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
610 if (!IS_ERR_OR_NULL(tc->t)) {
611 mlx5_destroy_flow_table(tc->t);