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/sch_generic.h>
35 #include <net/pkt_cls.h>
36 #include <net/tc_act/tc_gact.h>
37 #include <net/tc_act/tc_skbedit.h>
38 #include <linux/mlx5/fs.h>
39 #include <linux/mlx5/device.h>
40 #include <linux/rhashtable.h>
41 #include <net/switchdev.h>
42 #include <net/tc_act/tc_mirred.h>
43 #include <net/tc_act/tc_vlan.h>
44 #include <net/tc_act/tc_tunnel_key.h>
45 #include <net/tc_act/tc_pedit.h>
46 #include <net/tc_act/tc_csum.h>
47 #include <net/vxlan.h>
55 struct mlx5_nic_flow_attr {
62 MLX5E_TC_FLOW_ESWITCH = BIT(0),
63 MLX5E_TC_FLOW_NIC = BIT(1),
64 MLX5E_TC_FLOW_OFFLOADED = BIT(2),
67 struct mlx5e_tc_flow {
68 struct rhash_head node;
71 struct mlx5_flow_handle *rule;
72 struct list_head encap; /* flows sharing the same encap ID */
73 struct list_head mod_hdr; /* flows sharing the same mod hdr ID */
75 struct mlx5_esw_flow_attr esw_attr[0];
76 struct mlx5_nic_flow_attr nic_attr[0];
80 struct mlx5e_tc_flow_parse_attr {
81 struct mlx5_flow_spec spec;
82 int num_mod_hdr_actions;
83 void *mod_hdr_actions;
87 MLX5_HEADER_TYPE_VXLAN = 0x0,
88 MLX5_HEADER_TYPE_NVGRE = 0x1,
91 #define MLX5E_TC_TABLE_NUM_ENTRIES 1024
92 #define MLX5E_TC_TABLE_NUM_GROUPS 4
99 struct mlx5e_mod_hdr_entry {
100 /* a node of a hash table which keeps all the mod_hdr entries */
101 struct hlist_node mod_hdr_hlist;
103 /* flows sharing the same mod_hdr entry */
104 struct list_head flows;
106 struct mod_hdr_key key;
111 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)
113 static inline u32 hash_mod_hdr_info(struct mod_hdr_key *key)
115 return jhash(key->actions,
116 key->num_actions * MLX5_MH_ACT_SZ, 0);
119 static inline int cmp_mod_hdr_info(struct mod_hdr_key *a,
120 struct mod_hdr_key *b)
122 if (a->num_actions != b->num_actions)
125 return memcmp(a->actions, b->actions, a->num_actions * MLX5_MH_ACT_SZ);
128 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
129 struct mlx5e_tc_flow *flow,
130 struct mlx5e_tc_flow_parse_attr *parse_attr)
132 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
133 int num_actions, actions_size, namespace, err;
134 struct mlx5e_mod_hdr_entry *mh;
135 struct mod_hdr_key key;
139 num_actions = parse_attr->num_mod_hdr_actions;
140 actions_size = MLX5_MH_ACT_SZ * num_actions;
142 key.actions = parse_attr->mod_hdr_actions;
143 key.num_actions = num_actions;
145 hash_key = hash_mod_hdr_info(&key);
147 if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
148 namespace = MLX5_FLOW_NAMESPACE_FDB;
149 hash_for_each_possible(esw->offloads.mod_hdr_tbl, mh,
150 mod_hdr_hlist, hash_key) {
151 if (!cmp_mod_hdr_info(&mh->key, &key)) {
157 namespace = MLX5_FLOW_NAMESPACE_KERNEL;
158 hash_for_each_possible(priv->fs.tc.mod_hdr_tbl, mh,
159 mod_hdr_hlist, hash_key) {
160 if (!cmp_mod_hdr_info(&mh->key, &key)) {
170 mh = kzalloc(sizeof(*mh) + actions_size, GFP_KERNEL);
174 mh->key.actions = (void *)mh + sizeof(*mh);
175 memcpy(mh->key.actions, key.actions, actions_size);
176 mh->key.num_actions = num_actions;
177 INIT_LIST_HEAD(&mh->flows);
179 err = mlx5_modify_header_alloc(priv->mdev, namespace,
186 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
187 hash_add(esw->offloads.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
189 hash_add(priv->fs.tc.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
192 list_add(&flow->mod_hdr, &mh->flows);
193 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
194 flow->esw_attr->mod_hdr_id = mh->mod_hdr_id;
196 flow->nic_attr->mod_hdr_id = mh->mod_hdr_id;
205 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
206 struct mlx5e_tc_flow *flow)
208 struct list_head *next = flow->mod_hdr.next;
210 list_del(&flow->mod_hdr);
212 if (list_empty(next)) {
213 struct mlx5e_mod_hdr_entry *mh;
215 mh = list_entry(next, struct mlx5e_mod_hdr_entry, flows);
217 mlx5_modify_header_dealloc(priv->mdev, mh->mod_hdr_id);
218 hash_del(&mh->mod_hdr_hlist);
223 static struct mlx5_flow_handle *
224 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
225 struct mlx5e_tc_flow_parse_attr *parse_attr,
226 struct mlx5e_tc_flow *flow)
228 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
229 struct mlx5_core_dev *dev = priv->mdev;
230 struct mlx5_flow_destination dest = {};
231 struct mlx5_flow_act flow_act = {
232 .action = attr->action,
233 .flow_tag = attr->flow_tag,
236 struct mlx5_fc *counter = NULL;
237 struct mlx5_flow_handle *rule;
238 bool table_created = false;
241 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
242 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
243 dest.ft = priv->fs.vlan.ft.t;
244 } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
245 counter = mlx5_fc_create(dev, true);
247 return ERR_CAST(counter);
249 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
250 dest.counter = counter;
253 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
254 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
255 flow_act.modify_id = attr->mod_hdr_id;
256 kfree(parse_attr->mod_hdr_actions);
259 goto err_create_mod_hdr_id;
263 if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
265 mlx5_create_auto_grouped_flow_table(priv->fs.ns,
267 MLX5E_TC_TABLE_NUM_ENTRIES,
268 MLX5E_TC_TABLE_NUM_GROUPS,
270 if (IS_ERR(priv->fs.tc.t)) {
271 netdev_err(priv->netdev,
272 "Failed to create tc offload table\n");
273 rule = ERR_CAST(priv->fs.tc.t);
277 table_created = true;
280 parse_attr->spec.match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
281 rule = mlx5_add_flow_rules(priv->fs.tc.t, &parse_attr->spec,
282 &flow_act, &dest, 1);
291 mlx5_destroy_flow_table(priv->fs.tc.t);
292 priv->fs.tc.t = NULL;
295 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
296 mlx5e_detach_mod_hdr(priv, flow);
297 err_create_mod_hdr_id:
298 mlx5_fc_destroy(dev, counter);
303 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
304 struct mlx5e_tc_flow *flow)
306 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
307 struct mlx5_fc *counter = NULL;
309 counter = mlx5_flow_rule_counter(flow->rule);
310 mlx5_del_flow_rules(flow->rule);
311 mlx5_fc_destroy(priv->mdev, counter);
313 if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
314 mlx5_destroy_flow_table(priv->fs.tc.t);
315 priv->fs.tc.t = NULL;
318 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
319 mlx5e_detach_mod_hdr(priv, flow);
322 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
323 struct mlx5e_tc_flow *flow);
325 static struct mlx5_flow_handle *
326 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
327 struct mlx5e_tc_flow_parse_attr *parse_attr,
328 struct mlx5e_tc_flow *flow)
330 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
331 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
332 struct mlx5_flow_handle *rule;
335 err = mlx5_eswitch_add_vlan_action(esw, attr);
341 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
342 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
343 kfree(parse_attr->mod_hdr_actions);
350 rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
357 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
358 mlx5e_detach_mod_hdr(priv, flow);
360 mlx5_eswitch_del_vlan_action(esw, attr);
362 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
363 mlx5e_detach_encap(priv, flow);
367 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
368 struct mlx5e_tc_flow *flow)
370 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
371 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
373 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
374 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
375 mlx5_eswitch_del_offloaded_rule(esw, flow->rule, attr);
378 mlx5_eswitch_del_vlan_action(esw, attr);
380 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
381 mlx5e_detach_encap(priv, flow);
382 kvfree(attr->parse_attr);
385 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
386 mlx5e_detach_mod_hdr(priv, flow);
389 void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
390 struct mlx5e_encap_entry *e)
392 struct mlx5e_tc_flow *flow;
395 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
396 e->encap_size, e->encap_header,
399 mlx5_core_warn(priv->mdev, "Failed to offload cached encapsulation header, %d\n",
403 e->flags |= MLX5_ENCAP_ENTRY_VALID;
404 mlx5e_rep_queue_neigh_stats_work(priv);
406 list_for_each_entry(flow, &e->flows, encap) {
407 flow->esw_attr->encap_id = e->encap_id;
408 flow->rule = mlx5e_tc_add_fdb_flow(priv,
409 flow->esw_attr->parse_attr,
411 if (IS_ERR(flow->rule)) {
412 err = PTR_ERR(flow->rule);
413 mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
417 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
421 void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
422 struct mlx5e_encap_entry *e)
424 struct mlx5e_tc_flow *flow;
425 struct mlx5_fc *counter;
427 list_for_each_entry(flow, &e->flows, encap) {
428 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
429 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
430 counter = mlx5_flow_rule_counter(flow->rule);
431 mlx5_del_flow_rules(flow->rule);
432 mlx5_fc_destroy(priv->mdev, counter);
436 if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
437 e->flags &= ~MLX5_ENCAP_ENTRY_VALID;
438 mlx5_encap_dealloc(priv->mdev, e->encap_id);
442 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
444 struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
445 u64 bytes, packets, lastuse = 0;
446 struct mlx5e_tc_flow *flow;
447 struct mlx5e_encap_entry *e;
448 struct mlx5_fc *counter;
449 struct neigh_table *tbl;
450 bool neigh_used = false;
453 if (m_neigh->family == AF_INET)
455 #if IS_ENABLED(CONFIG_IPV6)
456 else if (m_neigh->family == AF_INET6)
457 tbl = ipv6_stub->nd_tbl;
462 list_for_each_entry(e, &nhe->encap_list, encap_list) {
463 if (!(e->flags & MLX5_ENCAP_ENTRY_VALID))
465 list_for_each_entry(flow, &e->flows, encap) {
466 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
467 counter = mlx5_flow_rule_counter(flow->rule);
468 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
469 if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
478 nhe->reported_lastuse = jiffies;
480 /* find the relevant neigh according to the cached device and
483 n = neigh_lookup(tbl, &m_neigh->dst_ip, m_neigh->dev);
485 WARN(1, "The neighbour already freed\n");
489 neigh_event_send(n, NULL);
494 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
495 struct mlx5e_tc_flow *flow)
497 struct list_head *next = flow->encap.next;
499 list_del(&flow->encap);
500 if (list_empty(next)) {
501 struct mlx5e_encap_entry *e;
503 e = list_entry(next, struct mlx5e_encap_entry, flows);
504 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
506 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
507 mlx5_encap_dealloc(priv->mdev, e->encap_id);
509 hash_del_rcu(&e->encap_hlist);
510 kfree(e->encap_header);
515 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
516 struct mlx5e_tc_flow *flow)
518 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
519 mlx5e_tc_del_fdb_flow(priv, flow);
521 mlx5e_tc_del_nic_flow(priv, flow);
524 static void parse_vxlan_attr(struct mlx5_flow_spec *spec,
525 struct tc_cls_flower_offload *f)
527 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
529 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
531 void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
533 void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
536 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
537 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
539 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
540 struct flow_dissector_key_keyid *key =
541 skb_flow_dissector_target(f->dissector,
542 FLOW_DISSECTOR_KEY_ENC_KEYID,
544 struct flow_dissector_key_keyid *mask =
545 skb_flow_dissector_target(f->dissector,
546 FLOW_DISSECTOR_KEY_ENC_KEYID,
548 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
549 be32_to_cpu(mask->keyid));
550 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
551 be32_to_cpu(key->keyid));
555 static int parse_tunnel_attr(struct mlx5e_priv *priv,
556 struct mlx5_flow_spec *spec,
557 struct tc_cls_flower_offload *f)
559 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
561 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
564 struct flow_dissector_key_control *enc_control =
565 skb_flow_dissector_target(f->dissector,
566 FLOW_DISSECTOR_KEY_ENC_CONTROL,
569 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
570 struct flow_dissector_key_ports *key =
571 skb_flow_dissector_target(f->dissector,
572 FLOW_DISSECTOR_KEY_ENC_PORTS,
574 struct flow_dissector_key_ports *mask =
575 skb_flow_dissector_target(f->dissector,
576 FLOW_DISSECTOR_KEY_ENC_PORTS,
578 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
579 struct net_device *up_dev = mlx5_eswitch_get_uplink_netdev(esw);
580 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
582 /* Full udp dst port must be given */
583 if (memchr_inv(&mask->dst, 0xff, sizeof(mask->dst)))
584 goto vxlan_match_offload_err;
586 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->dst)) &&
587 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
588 parse_vxlan_attr(spec, f);
590 netdev_warn(priv->netdev,
591 "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->dst));
595 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
596 udp_dport, ntohs(mask->dst));
597 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
598 udp_dport, ntohs(key->dst));
600 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
601 udp_sport, ntohs(mask->src));
602 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
603 udp_sport, ntohs(key->src));
604 } else { /* udp dst port must be given */
605 vxlan_match_offload_err:
606 netdev_warn(priv->netdev,
607 "IP tunnel decap offload supported only for vxlan, must set UDP dport\n");
611 if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
612 struct flow_dissector_key_ipv4_addrs *key =
613 skb_flow_dissector_target(f->dissector,
614 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
616 struct flow_dissector_key_ipv4_addrs *mask =
617 skb_flow_dissector_target(f->dissector,
618 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
620 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
621 src_ipv4_src_ipv6.ipv4_layout.ipv4,
623 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
624 src_ipv4_src_ipv6.ipv4_layout.ipv4,
627 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
628 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
630 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
631 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
634 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
635 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
636 } else if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
637 struct flow_dissector_key_ipv6_addrs *key =
638 skb_flow_dissector_target(f->dissector,
639 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
641 struct flow_dissector_key_ipv6_addrs *mask =
642 skb_flow_dissector_target(f->dissector,
643 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
646 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
647 src_ipv4_src_ipv6.ipv6_layout.ipv6),
648 &mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
649 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
650 src_ipv4_src_ipv6.ipv6_layout.ipv6),
651 &key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
653 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
654 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
655 &mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
656 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
657 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
658 &key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
660 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
661 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
664 /* Enforce DMAC when offloading incoming tunneled flows.
665 * Flow counters require a match on the DMAC.
667 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
668 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
669 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
670 dmac_47_16), priv->netdev->dev_addr);
672 /* let software handle IP fragments */
673 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
674 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
679 static int __parse_cls_flower(struct mlx5e_priv *priv,
680 struct mlx5_flow_spec *spec,
681 struct tc_cls_flower_offload *f,
684 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
686 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
691 *min_inline = MLX5_INLINE_MODE_L2;
693 if (f->dissector->used_keys &
694 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
695 BIT(FLOW_DISSECTOR_KEY_BASIC) |
696 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
697 BIT(FLOW_DISSECTOR_KEY_VLAN) |
698 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
699 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
700 BIT(FLOW_DISSECTOR_KEY_PORTS) |
701 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
702 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
703 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
704 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
705 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
706 BIT(FLOW_DISSECTOR_KEY_TCP) |
707 BIT(FLOW_DISSECTOR_KEY_IP))) {
708 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
709 f->dissector->used_keys);
713 if ((dissector_uses_key(f->dissector,
714 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) ||
715 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID) ||
716 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) &&
717 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
718 struct flow_dissector_key_control *key =
719 skb_flow_dissector_target(f->dissector,
720 FLOW_DISSECTOR_KEY_ENC_CONTROL,
722 switch (key->addr_type) {
723 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
724 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
725 if (parse_tunnel_attr(priv, spec, f))
732 /* In decap flow, header pointers should point to the inner
733 * headers, outer header were already set by parse_tunnel_attr
735 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
737 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
741 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
742 struct flow_dissector_key_control *key =
743 skb_flow_dissector_target(f->dissector,
744 FLOW_DISSECTOR_KEY_CONTROL,
747 struct flow_dissector_key_control *mask =
748 skb_flow_dissector_target(f->dissector,
749 FLOW_DISSECTOR_KEY_CONTROL,
751 addr_type = key->addr_type;
753 if (mask->flags & FLOW_DIS_IS_FRAGMENT) {
754 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
755 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
756 key->flags & FLOW_DIS_IS_FRAGMENT);
758 /* the HW doesn't need L3 inline to match on frag=no */
759 if (key->flags & FLOW_DIS_IS_FRAGMENT)
760 *min_inline = MLX5_INLINE_MODE_IP;
764 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
765 struct flow_dissector_key_basic *key =
766 skb_flow_dissector_target(f->dissector,
767 FLOW_DISSECTOR_KEY_BASIC,
769 struct flow_dissector_key_basic *mask =
770 skb_flow_dissector_target(f->dissector,
771 FLOW_DISSECTOR_KEY_BASIC,
773 ip_proto = key->ip_proto;
775 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
776 ntohs(mask->n_proto));
777 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
778 ntohs(key->n_proto));
780 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
782 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
786 *min_inline = MLX5_INLINE_MODE_IP;
789 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
790 struct flow_dissector_key_eth_addrs *key =
791 skb_flow_dissector_target(f->dissector,
792 FLOW_DISSECTOR_KEY_ETH_ADDRS,
794 struct flow_dissector_key_eth_addrs *mask =
795 skb_flow_dissector_target(f->dissector,
796 FLOW_DISSECTOR_KEY_ETH_ADDRS,
799 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
802 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
806 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
809 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
814 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
815 struct flow_dissector_key_vlan *key =
816 skb_flow_dissector_target(f->dissector,
817 FLOW_DISSECTOR_KEY_VLAN,
819 struct flow_dissector_key_vlan *mask =
820 skb_flow_dissector_target(f->dissector,
821 FLOW_DISSECTOR_KEY_VLAN,
823 if (mask->vlan_id || mask->vlan_priority) {
824 MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
825 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
827 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
828 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
830 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
831 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
835 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
836 struct flow_dissector_key_ipv4_addrs *key =
837 skb_flow_dissector_target(f->dissector,
838 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
840 struct flow_dissector_key_ipv4_addrs *mask =
841 skb_flow_dissector_target(f->dissector,
842 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
845 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
846 src_ipv4_src_ipv6.ipv4_layout.ipv4),
847 &mask->src, sizeof(mask->src));
848 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
849 src_ipv4_src_ipv6.ipv4_layout.ipv4),
850 &key->src, sizeof(key->src));
851 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
852 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
853 &mask->dst, sizeof(mask->dst));
854 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
855 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
856 &key->dst, sizeof(key->dst));
858 if (mask->src || mask->dst)
859 *min_inline = MLX5_INLINE_MODE_IP;
862 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
863 struct flow_dissector_key_ipv6_addrs *key =
864 skb_flow_dissector_target(f->dissector,
865 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
867 struct flow_dissector_key_ipv6_addrs *mask =
868 skb_flow_dissector_target(f->dissector,
869 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
872 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
873 src_ipv4_src_ipv6.ipv6_layout.ipv6),
874 &mask->src, sizeof(mask->src));
875 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
876 src_ipv4_src_ipv6.ipv6_layout.ipv6),
877 &key->src, sizeof(key->src));
879 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
880 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
881 &mask->dst, sizeof(mask->dst));
882 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
883 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
884 &key->dst, sizeof(key->dst));
886 if (ipv6_addr_type(&mask->src) != IPV6_ADDR_ANY ||
887 ipv6_addr_type(&mask->dst) != IPV6_ADDR_ANY)
888 *min_inline = MLX5_INLINE_MODE_IP;
891 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_IP)) {
892 struct flow_dissector_key_ip *key =
893 skb_flow_dissector_target(f->dissector,
894 FLOW_DISSECTOR_KEY_IP,
896 struct flow_dissector_key_ip *mask =
897 skb_flow_dissector_target(f->dissector,
898 FLOW_DISSECTOR_KEY_IP,
901 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn, mask->tos & 0x3);
902 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, key->tos & 0x3);
904 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp, mask->tos >> 2);
905 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, key->tos >> 2);
907 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit, mask->ttl);
908 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit, key->ttl);
911 !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
912 ft_field_support.outer_ipv4_ttl))
915 if (mask->tos || mask->ttl)
916 *min_inline = MLX5_INLINE_MODE_IP;
919 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
920 struct flow_dissector_key_ports *key =
921 skb_flow_dissector_target(f->dissector,
922 FLOW_DISSECTOR_KEY_PORTS,
924 struct flow_dissector_key_ports *mask =
925 skb_flow_dissector_target(f->dissector,
926 FLOW_DISSECTOR_KEY_PORTS,
930 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
931 tcp_sport, ntohs(mask->src));
932 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
933 tcp_sport, ntohs(key->src));
935 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
936 tcp_dport, ntohs(mask->dst));
937 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
938 tcp_dport, ntohs(key->dst));
942 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
943 udp_sport, ntohs(mask->src));
944 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
945 udp_sport, ntohs(key->src));
947 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
948 udp_dport, ntohs(mask->dst));
949 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
950 udp_dport, ntohs(key->dst));
953 netdev_err(priv->netdev,
954 "Only UDP and TCP transport are supported\n");
958 if (mask->src || mask->dst)
959 *min_inline = MLX5_INLINE_MODE_TCP_UDP;
962 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_TCP)) {
963 struct flow_dissector_key_tcp *key =
964 skb_flow_dissector_target(f->dissector,
965 FLOW_DISSECTOR_KEY_TCP,
967 struct flow_dissector_key_tcp *mask =
968 skb_flow_dissector_target(f->dissector,
969 FLOW_DISSECTOR_KEY_TCP,
972 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
974 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
978 *min_inline = MLX5_INLINE_MODE_TCP_UDP;
984 static int parse_cls_flower(struct mlx5e_priv *priv,
985 struct mlx5e_tc_flow *flow,
986 struct mlx5_flow_spec *spec,
987 struct tc_cls_flower_offload *f)
989 struct mlx5_core_dev *dev = priv->mdev;
990 struct mlx5_eswitch *esw = dev->priv.eswitch;
991 struct mlx5e_rep_priv *rpriv = priv->ppriv;
992 struct mlx5_eswitch_rep *rep;
996 err = __parse_cls_flower(priv, spec, f, &min_inline);
998 if (!err && (flow->flags & MLX5E_TC_FLOW_ESWITCH)) {
1000 if (rep->vport != FDB_UPLINK_VPORT &&
1001 (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
1002 esw->offloads.inline_mode < min_inline)) {
1003 netdev_warn(priv->netdev,
1004 "Flow is not offloaded due to min inline setting, required %d actual %d\n",
1005 min_inline, esw->offloads.inline_mode);
1013 struct pedit_headers {
1021 static int pedit_header_offsets[] = {
1022 [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
1023 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
1024 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
1025 [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
1026 [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
1029 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
1031 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
1032 struct pedit_headers *masks,
1033 struct pedit_headers *vals)
1035 u32 *curr_pmask, *curr_pval;
1037 if (hdr_type >= __PEDIT_HDR_TYPE_MAX)
1040 curr_pmask = (u32 *)(pedit_header(masks, hdr_type) + offset);
1041 curr_pval = (u32 *)(pedit_header(vals, hdr_type) + offset);
1043 if (*curr_pmask & mask) /* disallow acting twice on the same location */
1046 *curr_pmask |= mask;
1047 *curr_pval |= (val & mask);
1055 struct mlx5_fields {
1061 static struct mlx5_fields fields[] = {
1062 {MLX5_ACTION_IN_FIELD_OUT_DMAC_47_16, 4, offsetof(struct pedit_headers, eth.h_dest[0])},
1063 {MLX5_ACTION_IN_FIELD_OUT_DMAC_15_0, 2, offsetof(struct pedit_headers, eth.h_dest[4])},
1064 {MLX5_ACTION_IN_FIELD_OUT_SMAC_47_16, 4, offsetof(struct pedit_headers, eth.h_source[0])},
1065 {MLX5_ACTION_IN_FIELD_OUT_SMAC_15_0, 2, offsetof(struct pedit_headers, eth.h_source[4])},
1066 {MLX5_ACTION_IN_FIELD_OUT_ETHERTYPE, 2, offsetof(struct pedit_headers, eth.h_proto)},
1068 {MLX5_ACTION_IN_FIELD_OUT_IP_TTL, 1, offsetof(struct pedit_headers, ip4.ttl)},
1069 {MLX5_ACTION_IN_FIELD_OUT_SIPV4, 4, offsetof(struct pedit_headers, ip4.saddr)},
1070 {MLX5_ACTION_IN_FIELD_OUT_DIPV4, 4, offsetof(struct pedit_headers, ip4.daddr)},
1072 {MLX5_ACTION_IN_FIELD_OUT_SIPV6_127_96, 4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[0])},
1073 {MLX5_ACTION_IN_FIELD_OUT_SIPV6_95_64, 4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[1])},
1074 {MLX5_ACTION_IN_FIELD_OUT_SIPV6_63_32, 4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[2])},
1075 {MLX5_ACTION_IN_FIELD_OUT_SIPV6_31_0, 4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[3])},
1076 {MLX5_ACTION_IN_FIELD_OUT_DIPV6_127_96, 4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[0])},
1077 {MLX5_ACTION_IN_FIELD_OUT_DIPV6_95_64, 4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[1])},
1078 {MLX5_ACTION_IN_FIELD_OUT_DIPV6_63_32, 4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[2])},
1079 {MLX5_ACTION_IN_FIELD_OUT_DIPV6_31_0, 4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[3])},
1081 {MLX5_ACTION_IN_FIELD_OUT_TCP_SPORT, 2, offsetof(struct pedit_headers, tcp.source)},
1082 {MLX5_ACTION_IN_FIELD_OUT_TCP_DPORT, 2, offsetof(struct pedit_headers, tcp.dest)},
1083 {MLX5_ACTION_IN_FIELD_OUT_TCP_FLAGS, 1, offsetof(struct pedit_headers, tcp.ack_seq) + 5},
1085 {MLX5_ACTION_IN_FIELD_OUT_UDP_SPORT, 2, offsetof(struct pedit_headers, udp.source)},
1086 {MLX5_ACTION_IN_FIELD_OUT_UDP_DPORT, 2, offsetof(struct pedit_headers, udp.dest)},
1089 /* On input attr->num_mod_hdr_actions tells how many HW actions can be parsed at
1090 * max from the SW pedit action. On success, it says how many HW actions were
1093 static int offload_pedit_fields(struct pedit_headers *masks,
1094 struct pedit_headers *vals,
1095 struct mlx5e_tc_flow_parse_attr *parse_attr)
1097 struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
1098 int i, action_size, nactions, max_actions, first, last, next_z;
1099 void *s_masks_p, *a_masks_p, *vals_p;
1100 struct mlx5_fields *f;
1101 u8 cmd, field_bsize;
1108 set_masks = &masks[TCA_PEDIT_KEY_EX_CMD_SET];
1109 add_masks = &masks[TCA_PEDIT_KEY_EX_CMD_ADD];
1110 set_vals = &vals[TCA_PEDIT_KEY_EX_CMD_SET];
1111 add_vals = &vals[TCA_PEDIT_KEY_EX_CMD_ADD];
1113 action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1114 action = parse_attr->mod_hdr_actions;
1115 max_actions = parse_attr->num_mod_hdr_actions;
1118 for (i = 0; i < ARRAY_SIZE(fields); i++) {
1120 /* avoid seeing bits set from previous iterations */
1124 s_masks_p = (void *)set_masks + f->offset;
1125 a_masks_p = (void *)add_masks + f->offset;
1127 memcpy(&s_mask, s_masks_p, f->size);
1128 memcpy(&a_mask, a_masks_p, f->size);
1130 if (!s_mask && !a_mask) /* nothing to offload here */
1133 if (s_mask && a_mask) {
1134 printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
1138 if (nactions == max_actions) {
1139 printk(KERN_WARNING "mlx5: parsed %d pedit actions, can't do more\n", nactions);
1144 cmd = MLX5_ACTION_TYPE_SET;
1146 vals_p = (void *)set_vals + f->offset;
1147 /* clear to denote we consumed this field */
1148 memset(s_masks_p, 0, f->size);
1150 cmd = MLX5_ACTION_TYPE_ADD;
1152 vals_p = (void *)add_vals + f->offset;
1153 /* clear to denote we consumed this field */
1154 memset(a_masks_p, 0, f->size);
1157 field_bsize = f->size * BITS_PER_BYTE;
1159 if (field_bsize == 32) {
1160 mask_be32 = *(__be32 *)&mask;
1161 mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
1162 } else if (field_bsize == 16) {
1163 mask_be16 = *(__be16 *)&mask;
1164 mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
1167 first = find_first_bit(&mask, field_bsize);
1168 next_z = find_next_zero_bit(&mask, field_bsize, first);
1169 last = find_last_bit(&mask, field_bsize);
1170 if (first < next_z && next_z < last) {
1171 printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
1176 MLX5_SET(set_action_in, action, action_type, cmd);
1177 MLX5_SET(set_action_in, action, field, f->field);
1179 if (cmd == MLX5_ACTION_TYPE_SET) {
1180 MLX5_SET(set_action_in, action, offset, first);
1181 /* length is num of bits to be written, zero means length of 32 */
1182 MLX5_SET(set_action_in, action, length, (last - first + 1));
1185 if (field_bsize == 32)
1186 MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
1187 else if (field_bsize == 16)
1188 MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
1189 else if (field_bsize == 8)
1190 MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
1192 action += action_size;
1196 parse_attr->num_mod_hdr_actions = nactions;
1200 static int alloc_mod_hdr_actions(struct mlx5e_priv *priv,
1201 const struct tc_action *a, int namespace,
1202 struct mlx5e_tc_flow_parse_attr *parse_attr)
1204 int nkeys, action_size, max_actions;
1206 nkeys = tcf_pedit_nkeys(a);
1207 action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1209 if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
1210 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, max_modify_header_actions);
1211 else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
1212 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, max_modify_header_actions);
1214 /* can get up to crazingly 16 HW actions in 32 bits pedit SW key */
1215 max_actions = min(max_actions, nkeys * 16);
1217 parse_attr->mod_hdr_actions = kcalloc(max_actions, action_size, GFP_KERNEL);
1218 if (!parse_attr->mod_hdr_actions)
1221 parse_attr->num_mod_hdr_actions = max_actions;
1225 static const struct pedit_headers zero_masks = {};
1227 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
1228 const struct tc_action *a, int namespace,
1229 struct mlx5e_tc_flow_parse_attr *parse_attr)
1231 struct pedit_headers masks[__PEDIT_CMD_MAX], vals[__PEDIT_CMD_MAX], *cmd_masks;
1232 int nkeys, i, err = -EOPNOTSUPP;
1233 u32 mask, val, offset;
1236 nkeys = tcf_pedit_nkeys(a);
1238 memset(masks, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1239 memset(vals, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1241 for (i = 0; i < nkeys; i++) {
1242 htype = tcf_pedit_htype(a, i);
1243 cmd = tcf_pedit_cmd(a, i);
1244 err = -EOPNOTSUPP; /* can't be all optimistic */
1246 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
1247 printk(KERN_WARNING "mlx5: legacy pedit isn't offloaded\n");
1251 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET && cmd != TCA_PEDIT_KEY_EX_CMD_ADD) {
1252 printk(KERN_WARNING "mlx5: pedit cmd %d isn't offloaded\n", cmd);
1256 mask = tcf_pedit_mask(a, i);
1257 val = tcf_pedit_val(a, i);
1258 offset = tcf_pedit_offset(a, i);
1260 err = set_pedit_val(htype, ~mask, val, offset, &masks[cmd], &vals[cmd]);
1265 err = alloc_mod_hdr_actions(priv, a, namespace, parse_attr);
1269 err = offload_pedit_fields(masks, vals, parse_attr);
1271 goto out_dealloc_parsed_actions;
1273 for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
1274 cmd_masks = &masks[cmd];
1275 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
1276 printk(KERN_WARNING "mlx5: attempt to offload an unsupported field (cmd %d)\n",
1278 print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
1279 16, 1, cmd_masks, sizeof(zero_masks), true);
1281 goto out_dealloc_parsed_actions;
1287 out_dealloc_parsed_actions:
1288 kfree(parse_attr->mod_hdr_actions);
1293 static bool csum_offload_supported(struct mlx5e_priv *priv, u32 action, u32 update_flags)
1295 u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
1296 TCA_CSUM_UPDATE_FLAG_UDP;
1298 /* The HW recalcs checksums only if re-writing headers */
1299 if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
1300 netdev_warn(priv->netdev,
1301 "TC csum action is only offloaded with pedit\n");
1305 if (update_flags & ~prot_flags) {
1306 netdev_warn(priv->netdev,
1307 "can't offload TC csum action for some header/s - flags %#x\n",
1315 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1316 struct mlx5e_tc_flow_parse_attr *parse_attr,
1317 struct mlx5e_tc_flow *flow)
1319 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
1320 const struct tc_action *a;
1324 if (tc_no_actions(exts))
1327 attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
1330 tcf_exts_to_list(exts, &actions);
1331 list_for_each_entry(a, &actions, list) {
1332 if (is_tcf_gact_shot(a)) {
1333 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
1334 if (MLX5_CAP_FLOWTABLE(priv->mdev,
1335 flow_table_properties_nic_receive.flow_counter))
1336 attr->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
1340 if (is_tcf_pedit(a)) {
1341 err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_KERNEL,
1346 attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1347 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1351 if (is_tcf_csum(a)) {
1352 if (csum_offload_supported(priv, attr->action,
1353 tcf_csum_update_flags(a)))
1359 if (is_tcf_skbedit_mark(a)) {
1360 u32 mark = tcf_skbedit_mark(a);
1362 if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
1363 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
1368 attr->flow_tag = mark;
1369 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1379 static inline int cmp_encap_info(struct ip_tunnel_key *a,
1380 struct ip_tunnel_key *b)
1382 return memcmp(a, b, sizeof(*a));
1385 static inline int hash_encap_info(struct ip_tunnel_key *key)
1387 return jhash(key, sizeof(*key), 0);
1390 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
1391 struct net_device *mirred_dev,
1392 struct net_device **out_dev,
1394 struct neighbour **out_n,
1397 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1399 struct neighbour *n = NULL;
1401 #if IS_ENABLED(CONFIG_INET)
1404 rt = ip_route_output_key(dev_net(mirred_dev), fl4);
1405 ret = PTR_ERR_OR_ZERO(rt);
1411 /* if the egress device isn't on the same HW e-switch, we use the uplink */
1412 if (!switchdev_port_same_parent_id(priv->netdev, rt->dst.dev))
1413 *out_dev = mlx5_eswitch_get_uplink_netdev(esw);
1415 *out_dev = rt->dst.dev;
1417 *out_ttl = ip4_dst_hoplimit(&rt->dst);
1418 n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
1427 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
1428 struct net_device *mirred_dev,
1429 struct net_device **out_dev,
1431 struct neighbour **out_n,
1434 struct neighbour *n = NULL;
1435 struct dst_entry *dst;
1437 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
1438 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1441 dst = ip6_route_output(dev_net(mirred_dev), NULL, fl6);
1448 *out_ttl = ip6_dst_hoplimit(dst);
1450 /* if the egress device isn't on the same HW e-switch, we use the uplink */
1451 if (!switchdev_port_same_parent_id(priv->netdev, dst->dev))
1452 *out_dev = mlx5_eswitch_get_uplink_netdev(esw);
1454 *out_dev = dst->dev;
1459 n = dst_neigh_lookup(dst, &fl6->daddr);
1468 static void gen_vxlan_header_ipv4(struct net_device *out_dev,
1469 char buf[], int encap_size,
1470 unsigned char h_dest[ETH_ALEN],
1474 __be16 udp_dst_port,
1477 struct ethhdr *eth = (struct ethhdr *)buf;
1478 struct iphdr *ip = (struct iphdr *)((char *)eth + sizeof(struct ethhdr));
1479 struct udphdr *udp = (struct udphdr *)((char *)ip + sizeof(struct iphdr));
1480 struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1482 memset(buf, 0, encap_size);
1484 ether_addr_copy(eth->h_dest, h_dest);
1485 ether_addr_copy(eth->h_source, out_dev->dev_addr);
1486 eth->h_proto = htons(ETH_P_IP);
1492 ip->protocol = IPPROTO_UDP;
1496 udp->dest = udp_dst_port;
1497 vxh->vx_flags = VXLAN_HF_VNI;
1498 vxh->vx_vni = vxlan_vni_field(vx_vni);
1501 static void gen_vxlan_header_ipv6(struct net_device *out_dev,
1502 char buf[], int encap_size,
1503 unsigned char h_dest[ETH_ALEN],
1505 struct in6_addr *daddr,
1506 struct in6_addr *saddr,
1507 __be16 udp_dst_port,
1510 struct ethhdr *eth = (struct ethhdr *)buf;
1511 struct ipv6hdr *ip6h = (struct ipv6hdr *)((char *)eth + sizeof(struct ethhdr));
1512 struct udphdr *udp = (struct udphdr *)((char *)ip6h + sizeof(struct ipv6hdr));
1513 struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1515 memset(buf, 0, encap_size);
1517 ether_addr_copy(eth->h_dest, h_dest);
1518 ether_addr_copy(eth->h_source, out_dev->dev_addr);
1519 eth->h_proto = htons(ETH_P_IPV6);
1521 ip6_flow_hdr(ip6h, 0, 0);
1522 /* the HW fills up ipv6 payload len */
1523 ip6h->nexthdr = IPPROTO_UDP;
1524 ip6h->hop_limit = ttl;
1525 ip6h->daddr = *daddr;
1526 ip6h->saddr = *saddr;
1528 udp->dest = udp_dst_port;
1529 vxh->vx_flags = VXLAN_HF_VNI;
1530 vxh->vx_vni = vxlan_vni_field(vx_vni);
1533 static int mlx5e_create_encap_header_ipv4(struct mlx5e_priv *priv,
1534 struct net_device *mirred_dev,
1535 struct mlx5e_encap_entry *e)
1537 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1538 int ipv4_encap_size = ETH_HLEN + sizeof(struct iphdr) + VXLAN_HLEN;
1539 struct ip_tunnel_key *tun_key = &e->tun_info.key;
1540 struct net_device *out_dev;
1541 struct neighbour *n = NULL;
1542 struct flowi4 fl4 = {};
1547 if (max_encap_size < ipv4_encap_size) {
1548 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1549 ipv4_encap_size, max_encap_size);
1553 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
1557 switch (e->tunnel_type) {
1558 case MLX5_HEADER_TYPE_VXLAN:
1559 fl4.flowi4_proto = IPPROTO_UDP;
1560 fl4.fl4_dport = tun_key->tp_dst;
1566 fl4.flowi4_tos = tun_key->tos;
1567 fl4.daddr = tun_key->u.ipv4.dst;
1568 fl4.saddr = tun_key->u.ipv4.src;
1570 err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev,
1575 /* used by mlx5e_detach_encap to lookup a neigh hash table
1576 * entry in the neigh hash table when a user deletes a rule
1578 e->m_neigh.dev = n->dev;
1579 e->m_neigh.family = n->ops->family;
1580 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1581 e->out_dev = out_dev;
1583 /* It's importent to add the neigh to the hash table before checking
1584 * the neigh validity state. So if we'll get a notification, in case the
1585 * neigh changes it's validity state, we would find the relevant neigh
1588 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1592 read_lock_bh(&n->lock);
1593 nud_state = n->nud_state;
1594 ether_addr_copy(e->h_dest, n->ha);
1595 read_unlock_bh(&n->lock);
1597 switch (e->tunnel_type) {
1598 case MLX5_HEADER_TYPE_VXLAN:
1599 gen_vxlan_header_ipv4(out_dev, encap_header,
1600 ipv4_encap_size, e->h_dest, ttl,
1602 fl4.saddr, tun_key->tp_dst,
1603 tunnel_id_to_key32(tun_key->tun_id));
1607 goto destroy_neigh_entry;
1609 e->encap_size = ipv4_encap_size;
1610 e->encap_header = encap_header;
1612 if (!(nud_state & NUD_VALID)) {
1613 neigh_event_send(n, NULL);
1618 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1619 ipv4_encap_size, encap_header, &e->encap_id);
1621 goto destroy_neigh_entry;
1623 e->flags |= MLX5_ENCAP_ENTRY_VALID;
1624 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1628 destroy_neigh_entry:
1629 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1631 kfree(encap_header);
1637 static int mlx5e_create_encap_header_ipv6(struct mlx5e_priv *priv,
1638 struct net_device *mirred_dev,
1639 struct mlx5e_encap_entry *e)
1641 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1642 int ipv6_encap_size = ETH_HLEN + sizeof(struct ipv6hdr) + VXLAN_HLEN;
1643 struct ip_tunnel_key *tun_key = &e->tun_info.key;
1644 struct net_device *out_dev;
1645 struct neighbour *n = NULL;
1646 struct flowi6 fl6 = {};
1651 if (max_encap_size < ipv6_encap_size) {
1652 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1653 ipv6_encap_size, max_encap_size);
1657 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
1661 switch (e->tunnel_type) {
1662 case MLX5_HEADER_TYPE_VXLAN:
1663 fl6.flowi6_proto = IPPROTO_UDP;
1664 fl6.fl6_dport = tun_key->tp_dst;
1671 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
1672 fl6.daddr = tun_key->u.ipv6.dst;
1673 fl6.saddr = tun_key->u.ipv6.src;
1675 err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev,
1680 /* used by mlx5e_detach_encap to lookup a neigh hash table
1681 * entry in the neigh hash table when a user deletes a rule
1683 e->m_neigh.dev = n->dev;
1684 e->m_neigh.family = n->ops->family;
1685 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1686 e->out_dev = out_dev;
1688 /* It's importent to add the neigh to the hash table before checking
1689 * the neigh validity state. So if we'll get a notification, in case the
1690 * neigh changes it's validity state, we would find the relevant neigh
1693 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1697 read_lock_bh(&n->lock);
1698 nud_state = n->nud_state;
1699 ether_addr_copy(e->h_dest, n->ha);
1700 read_unlock_bh(&n->lock);
1702 switch (e->tunnel_type) {
1703 case MLX5_HEADER_TYPE_VXLAN:
1704 gen_vxlan_header_ipv6(out_dev, encap_header,
1705 ipv6_encap_size, e->h_dest, ttl,
1707 &fl6.saddr, tun_key->tp_dst,
1708 tunnel_id_to_key32(tun_key->tun_id));
1712 goto destroy_neigh_entry;
1715 e->encap_size = ipv6_encap_size;
1716 e->encap_header = encap_header;
1718 if (!(nud_state & NUD_VALID)) {
1719 neigh_event_send(n, NULL);
1724 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1725 ipv6_encap_size, encap_header, &e->encap_id);
1727 goto destroy_neigh_entry;
1729 e->flags |= MLX5_ENCAP_ENTRY_VALID;
1730 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1734 destroy_neigh_entry:
1735 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1737 kfree(encap_header);
1743 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
1744 struct ip_tunnel_info *tun_info,
1745 struct net_device *mirred_dev,
1746 struct net_device **encap_dev,
1747 struct mlx5e_tc_flow *flow)
1749 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1750 struct net_device *up_dev = mlx5_eswitch_get_uplink_netdev(esw);
1751 unsigned short family = ip_tunnel_info_af(tun_info);
1752 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
1753 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1754 struct ip_tunnel_key *key = &tun_info->key;
1755 struct mlx5e_encap_entry *e;
1756 int tunnel_type, err = 0;
1760 /* udp dst port must be set */
1761 if (!memchr_inv(&key->tp_dst, 0, sizeof(key->tp_dst)))
1762 goto vxlan_encap_offload_err;
1764 /* setting udp src port isn't supported */
1765 if (memchr_inv(&key->tp_src, 0, sizeof(key->tp_src))) {
1766 vxlan_encap_offload_err:
1767 netdev_warn(priv->netdev,
1768 "must set udp dst port and not set udp src port\n");
1772 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->tp_dst)) &&
1773 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap)) {
1774 tunnel_type = MLX5_HEADER_TYPE_VXLAN;
1776 netdev_warn(priv->netdev,
1777 "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->tp_dst));
1781 hash_key = hash_encap_info(key);
1783 hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
1784 encap_hlist, hash_key) {
1785 if (!cmp_encap_info(&e->tun_info.key, key)) {
1794 e = kzalloc(sizeof(*e), GFP_KERNEL);
1798 e->tun_info = *tun_info;
1799 e->tunnel_type = tunnel_type;
1800 INIT_LIST_HEAD(&e->flows);
1802 if (family == AF_INET)
1803 err = mlx5e_create_encap_header_ipv4(priv, mirred_dev, e);
1804 else if (family == AF_INET6)
1805 err = mlx5e_create_encap_header_ipv6(priv, mirred_dev, e);
1807 if (err && err != -EAGAIN)
1810 hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
1813 list_add(&flow->encap, &e->flows);
1814 *encap_dev = e->out_dev;
1815 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
1816 attr->encap_id = e->encap_id;
1825 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1826 struct mlx5e_tc_flow_parse_attr *parse_attr,
1827 struct mlx5e_tc_flow *flow)
1829 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1830 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1831 struct ip_tunnel_info *info = NULL;
1832 const struct tc_action *a;
1837 if (tc_no_actions(exts))
1840 memset(attr, 0, sizeof(*attr));
1841 attr->in_rep = rpriv->rep;
1843 tcf_exts_to_list(exts, &actions);
1844 list_for_each_entry(a, &actions, list) {
1845 if (is_tcf_gact_shot(a)) {
1846 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
1847 MLX5_FLOW_CONTEXT_ACTION_COUNT;
1851 if (is_tcf_pedit(a)) {
1852 err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_FDB,
1857 attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1861 if (is_tcf_csum(a)) {
1862 if (csum_offload_supported(priv, attr->action,
1863 tcf_csum_update_flags(a)))
1869 if (is_tcf_mirred_egress_redirect(a)) {
1870 int ifindex = tcf_mirred_ifindex(a);
1871 struct net_device *out_dev, *encap_dev = NULL;
1872 struct mlx5e_priv *out_priv;
1874 out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
1876 if (switchdev_port_same_parent_id(priv->netdev,
1878 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1879 MLX5_FLOW_CONTEXT_ACTION_COUNT;
1880 out_priv = netdev_priv(out_dev);
1881 rpriv = out_priv->ppriv;
1882 attr->out_rep = rpriv->rep;
1884 err = mlx5e_attach_encap(priv, info,
1885 out_dev, &encap_dev, flow);
1886 if (err && err != -EAGAIN)
1888 attr->action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1889 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1890 MLX5_FLOW_CONTEXT_ACTION_COUNT;
1891 out_priv = netdev_priv(encap_dev);
1892 rpriv = out_priv->ppriv;
1893 attr->out_rep = rpriv->rep;
1894 attr->parse_attr = parse_attr;
1896 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
1897 priv->netdev->name, out_dev->name);
1903 if (is_tcf_tunnel_set(a)) {
1904 info = tcf_tunnel_info(a);
1912 if (is_tcf_vlan(a)) {
1913 if (tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
1914 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
1915 } else if (tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
1916 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
1919 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
1920 attr->vlan = tcf_vlan_push_vid(a);
1921 } else { /* action is TCA_VLAN_ACT_MODIFY */
1927 if (is_tcf_tunnel_release(a)) {
1928 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
1937 int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
1938 struct tc_cls_flower_offload *f)
1940 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1941 struct mlx5e_tc_flow_parse_attr *parse_attr;
1942 struct mlx5e_tc_table *tc = &priv->fs.tc;
1943 struct mlx5e_tc_flow *flow;
1944 int attr_size, err = 0;
1947 if (esw && esw->mode == SRIOV_OFFLOADS) {
1948 flow_flags = MLX5E_TC_FLOW_ESWITCH;
1949 attr_size = sizeof(struct mlx5_esw_flow_attr);
1951 flow_flags = MLX5E_TC_FLOW_NIC;
1952 attr_size = sizeof(struct mlx5_nic_flow_attr);
1955 flow = kzalloc(sizeof(*flow) + attr_size, GFP_KERNEL);
1956 parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
1957 if (!parse_attr || !flow) {
1962 flow->cookie = f->cookie;
1963 flow->flags = flow_flags;
1965 err = parse_cls_flower(priv, flow, &parse_attr->spec, f);
1969 if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
1970 err = parse_tc_fdb_actions(priv, f->exts, parse_attr, flow);
1972 goto err_handle_encap_flow;
1973 flow->rule = mlx5e_tc_add_fdb_flow(priv, parse_attr, flow);
1975 err = parse_tc_nic_actions(priv, f->exts, parse_attr, flow);
1978 flow->rule = mlx5e_tc_add_nic_flow(priv, parse_attr, flow);
1981 if (IS_ERR(flow->rule)) {
1982 err = PTR_ERR(flow->rule);
1986 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
1987 err = rhashtable_insert_fast(&tc->ht, &flow->node,
1992 if (flow->flags & MLX5E_TC_FLOW_ESWITCH &&
1993 !(flow->esw_attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP))
1998 mlx5e_tc_del_flow(priv, flow);
2000 err_handle_encap_flow:
2001 if (err == -EAGAIN) {
2002 err = rhashtable_insert_fast(&tc->ht, &flow->node,
2005 mlx5e_tc_del_flow(priv, flow);
2016 int mlx5e_delete_flower(struct mlx5e_priv *priv,
2017 struct tc_cls_flower_offload *f)
2019 struct mlx5e_tc_flow *flow;
2020 struct mlx5e_tc_table *tc = &priv->fs.tc;
2022 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2027 rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
2029 mlx5e_tc_del_flow(priv, flow);
2036 int mlx5e_stats_flower(struct mlx5e_priv *priv,
2037 struct tc_cls_flower_offload *f)
2039 struct mlx5e_tc_table *tc = &priv->fs.tc;
2040 struct mlx5e_tc_flow *flow;
2041 struct mlx5_fc *counter;
2046 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2051 if (!(flow->flags & MLX5E_TC_FLOW_OFFLOADED))
2054 counter = mlx5_flow_rule_counter(flow->rule);
2058 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
2060 tcf_exts_stats_update(f->exts, bytes, packets, lastuse);
2065 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
2066 .head_offset = offsetof(struct mlx5e_tc_flow, node),
2067 .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
2068 .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
2069 .automatic_shrinking = true,
2072 int mlx5e_tc_init(struct mlx5e_priv *priv)
2074 struct mlx5e_tc_table *tc = &priv->fs.tc;
2076 hash_init(tc->mod_hdr_tbl);
2078 tc->ht_params = mlx5e_tc_flow_ht_params;
2079 return rhashtable_init(&tc->ht, &tc->ht_params);
2082 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
2084 struct mlx5e_tc_flow *flow = ptr;
2085 struct mlx5e_priv *priv = arg;
2087 mlx5e_tc_del_flow(priv, flow);
2091 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
2093 struct mlx5e_tc_table *tc = &priv->fs.tc;
2095 rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
2097 if (!IS_ERR_OR_NULL(tc->t)) {
2098 mlx5_destroy_flow_table(tc->t);