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>
56 struct mlx5_nic_flow_attr {
62 struct mlx5_flow_table *hairpin_ft;
66 MLX5E_TC_FLOW_ESWITCH = BIT(0),
67 MLX5E_TC_FLOW_NIC = BIT(1),
68 MLX5E_TC_FLOW_OFFLOADED = BIT(2),
69 MLX5E_TC_FLOW_HAIRPIN = BIT(3),
70 MLX5E_TC_FLOW_HAIRPIN_RSS = BIT(4),
73 struct mlx5e_tc_flow {
74 struct rhash_head node;
77 struct mlx5_flow_handle *rule;
78 struct list_head encap; /* flows sharing the same encap ID */
79 struct list_head mod_hdr; /* flows sharing the same mod hdr ID */
80 struct list_head hairpin; /* flows sharing the same hairpin */
82 struct mlx5_esw_flow_attr esw_attr[0];
83 struct mlx5_nic_flow_attr nic_attr[0];
87 struct mlx5e_tc_flow_parse_attr {
88 struct ip_tunnel_info tun_info;
89 struct mlx5_flow_spec spec;
90 int num_mod_hdr_actions;
91 void *mod_hdr_actions;
96 MLX5_HEADER_TYPE_VXLAN = 0x0,
97 MLX5_HEADER_TYPE_NVGRE = 0x1,
100 #define MLX5E_TC_TABLE_NUM_GROUPS 4
101 #define MLX5E_TC_TABLE_MAX_GROUP_SIZE BIT(16)
103 struct mlx5e_hairpin {
104 struct mlx5_hairpin *pair;
106 struct mlx5_core_dev *func_mdev;
107 struct mlx5e_priv *func_priv;
112 struct mlx5e_rqt indir_rqt;
113 u32 indir_tirn[MLX5E_NUM_INDIR_TIRS];
114 struct mlx5e_ttc_table ttc;
117 struct mlx5e_hairpin_entry {
118 /* a node of a hash table which keeps all the hairpin entries */
119 struct hlist_node hairpin_hlist;
121 /* flows sharing the same hairpin */
122 struct list_head flows;
126 struct mlx5e_hairpin *hp;
134 struct mlx5e_mod_hdr_entry {
135 /* a node of a hash table which keeps all the mod_hdr entries */
136 struct hlist_node mod_hdr_hlist;
138 /* flows sharing the same mod_hdr entry */
139 struct list_head flows;
141 struct mod_hdr_key key;
146 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)
148 static inline u32 hash_mod_hdr_info(struct mod_hdr_key *key)
150 return jhash(key->actions,
151 key->num_actions * MLX5_MH_ACT_SZ, 0);
154 static inline int cmp_mod_hdr_info(struct mod_hdr_key *a,
155 struct mod_hdr_key *b)
157 if (a->num_actions != b->num_actions)
160 return memcmp(a->actions, b->actions, a->num_actions * MLX5_MH_ACT_SZ);
163 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
164 struct mlx5e_tc_flow *flow,
165 struct mlx5e_tc_flow_parse_attr *parse_attr)
167 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
168 int num_actions, actions_size, namespace, err;
169 struct mlx5e_mod_hdr_entry *mh;
170 struct mod_hdr_key key;
174 num_actions = parse_attr->num_mod_hdr_actions;
175 actions_size = MLX5_MH_ACT_SZ * num_actions;
177 key.actions = parse_attr->mod_hdr_actions;
178 key.num_actions = num_actions;
180 hash_key = hash_mod_hdr_info(&key);
182 if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
183 namespace = MLX5_FLOW_NAMESPACE_FDB;
184 hash_for_each_possible(esw->offloads.mod_hdr_tbl, mh,
185 mod_hdr_hlist, hash_key) {
186 if (!cmp_mod_hdr_info(&mh->key, &key)) {
192 namespace = MLX5_FLOW_NAMESPACE_KERNEL;
193 hash_for_each_possible(priv->fs.tc.mod_hdr_tbl, mh,
194 mod_hdr_hlist, hash_key) {
195 if (!cmp_mod_hdr_info(&mh->key, &key)) {
205 mh = kzalloc(sizeof(*mh) + actions_size, GFP_KERNEL);
209 mh->key.actions = (void *)mh + sizeof(*mh);
210 memcpy(mh->key.actions, key.actions, actions_size);
211 mh->key.num_actions = num_actions;
212 INIT_LIST_HEAD(&mh->flows);
214 err = mlx5_modify_header_alloc(priv->mdev, namespace,
221 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
222 hash_add(esw->offloads.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
224 hash_add(priv->fs.tc.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
227 list_add(&flow->mod_hdr, &mh->flows);
228 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
229 flow->esw_attr->mod_hdr_id = mh->mod_hdr_id;
231 flow->nic_attr->mod_hdr_id = mh->mod_hdr_id;
240 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
241 struct mlx5e_tc_flow *flow)
243 struct list_head *next = flow->mod_hdr.next;
245 list_del(&flow->mod_hdr);
247 if (list_empty(next)) {
248 struct mlx5e_mod_hdr_entry *mh;
250 mh = list_entry(next, struct mlx5e_mod_hdr_entry, flows);
252 mlx5_modify_header_dealloc(priv->mdev, mh->mod_hdr_id);
253 hash_del(&mh->mod_hdr_hlist);
259 struct mlx5_core_dev *mlx5e_hairpin_get_mdev(struct net *net, int ifindex)
261 struct net_device *netdev;
262 struct mlx5e_priv *priv;
264 netdev = __dev_get_by_index(net, ifindex);
265 priv = netdev_priv(netdev);
269 static int mlx5e_hairpin_create_transport(struct mlx5e_hairpin *hp)
271 u32 in[MLX5_ST_SZ_DW(create_tir_in)] = {0};
275 err = mlx5_core_alloc_transport_domain(hp->func_mdev, &hp->tdn);
279 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
281 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
282 MLX5_SET(tirc, tirc, inline_rqn, hp->pair->rqn[0]);
283 MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
285 err = mlx5_core_create_tir(hp->func_mdev, in, MLX5_ST_SZ_BYTES(create_tir_in), &hp->tirn);
292 mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
297 static void mlx5e_hairpin_destroy_transport(struct mlx5e_hairpin *hp)
299 mlx5_core_destroy_tir(hp->func_mdev, hp->tirn);
300 mlx5_core_dealloc_transport_domain(hp->func_mdev, hp->tdn);
303 static void mlx5e_hairpin_fill_rqt_rqns(struct mlx5e_hairpin *hp, void *rqtc)
305 u32 indirection_rqt[MLX5E_INDIR_RQT_SIZE], rqn;
306 struct mlx5e_priv *priv = hp->func_priv;
307 int i, ix, sz = MLX5E_INDIR_RQT_SIZE;
309 mlx5e_build_default_indir_rqt(indirection_rqt, sz,
312 for (i = 0; i < sz; i++) {
314 if (priv->channels.params.rss_hfunc == ETH_RSS_HASH_XOR)
315 ix = mlx5e_bits_invert(i, ilog2(sz));
316 ix = indirection_rqt[ix];
317 rqn = hp->pair->rqn[ix];
318 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
322 static int mlx5e_hairpin_create_indirect_rqt(struct mlx5e_hairpin *hp)
324 int inlen, err, sz = MLX5E_INDIR_RQT_SIZE;
325 struct mlx5e_priv *priv = hp->func_priv;
326 struct mlx5_core_dev *mdev = priv->mdev;
330 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
331 in = kvzalloc(inlen, GFP_KERNEL);
335 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
337 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
338 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
340 mlx5e_hairpin_fill_rqt_rqns(hp, rqtc);
342 err = mlx5_core_create_rqt(mdev, in, inlen, &hp->indir_rqt.rqtn);
344 hp->indir_rqt.enabled = true;
350 static int mlx5e_hairpin_create_indirect_tirs(struct mlx5e_hairpin *hp)
352 struct mlx5e_priv *priv = hp->func_priv;
353 u32 in[MLX5_ST_SZ_DW(create_tir_in)];
357 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
358 memset(in, 0, MLX5_ST_SZ_BYTES(create_tir_in));
359 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
361 MLX5_SET(tirc, tirc, transport_domain, hp->tdn);
362 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
363 MLX5_SET(tirc, tirc, indirect_table, hp->indir_rqt.rqtn);
364 mlx5e_build_indir_tir_ctx_hash(&priv->channels.params, tt, tirc, false);
366 err = mlx5_core_create_tir(hp->func_mdev, in,
367 MLX5_ST_SZ_BYTES(create_tir_in), &hp->indir_tirn[tt]);
369 mlx5_core_warn(hp->func_mdev, "create indirect tirs failed, %d\n", err);
370 goto err_destroy_tirs;
376 for (i = 0; i < tt; i++)
377 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[i]);
381 static void mlx5e_hairpin_destroy_indirect_tirs(struct mlx5e_hairpin *hp)
385 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
386 mlx5_core_destroy_tir(hp->func_mdev, hp->indir_tirn[tt]);
389 static void mlx5e_hairpin_set_ttc_params(struct mlx5e_hairpin *hp,
390 struct ttc_params *ttc_params)
392 struct mlx5_flow_table_attr *ft_attr = &ttc_params->ft_attr;
395 memset(ttc_params, 0, sizeof(*ttc_params));
397 ttc_params->any_tt_tirn = hp->tirn;
399 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++)
400 ttc_params->indir_tirn[tt] = hp->indir_tirn[tt];
402 ft_attr->max_fte = MLX5E_NUM_TT;
403 ft_attr->level = MLX5E_TC_TTC_FT_LEVEL;
404 ft_attr->prio = MLX5E_TC_PRIO;
407 static int mlx5e_hairpin_rss_init(struct mlx5e_hairpin *hp)
409 struct mlx5e_priv *priv = hp->func_priv;
410 struct ttc_params ttc_params;
413 err = mlx5e_hairpin_create_indirect_rqt(hp);
417 err = mlx5e_hairpin_create_indirect_tirs(hp);
419 goto err_create_indirect_tirs;
421 mlx5e_hairpin_set_ttc_params(hp, &ttc_params);
422 err = mlx5e_create_ttc_table(priv, &ttc_params, &hp->ttc);
424 goto err_create_ttc_table;
426 netdev_dbg(priv->netdev, "add hairpin: using %d channels rss ttc table id %x\n",
427 hp->num_channels, hp->ttc.ft.t->id);
431 err_create_ttc_table:
432 mlx5e_hairpin_destroy_indirect_tirs(hp);
433 err_create_indirect_tirs:
434 mlx5e_destroy_rqt(priv, &hp->indir_rqt);
439 static void mlx5e_hairpin_rss_cleanup(struct mlx5e_hairpin *hp)
441 struct mlx5e_priv *priv = hp->func_priv;
443 mlx5e_destroy_ttc_table(priv, &hp->ttc);
444 mlx5e_hairpin_destroy_indirect_tirs(hp);
445 mlx5e_destroy_rqt(priv, &hp->indir_rqt);
448 static struct mlx5e_hairpin *
449 mlx5e_hairpin_create(struct mlx5e_priv *priv, struct mlx5_hairpin_params *params,
452 struct mlx5_core_dev *func_mdev, *peer_mdev;
453 struct mlx5e_hairpin *hp;
454 struct mlx5_hairpin *pair;
457 hp = kzalloc(sizeof(*hp), GFP_KERNEL);
459 return ERR_PTR(-ENOMEM);
461 func_mdev = priv->mdev;
462 peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
464 pair = mlx5_core_hairpin_create(func_mdev, peer_mdev, params);
467 goto create_pair_err;
470 hp->func_mdev = func_mdev;
471 hp->func_priv = priv;
472 hp->num_channels = params->num_channels;
474 err = mlx5e_hairpin_create_transport(hp);
476 goto create_transport_err;
478 if (hp->num_channels > 1) {
479 err = mlx5e_hairpin_rss_init(hp);
487 mlx5e_hairpin_destroy_transport(hp);
488 create_transport_err:
489 mlx5_core_hairpin_destroy(hp->pair);
495 static void mlx5e_hairpin_destroy(struct mlx5e_hairpin *hp)
497 if (hp->num_channels > 1)
498 mlx5e_hairpin_rss_cleanup(hp);
499 mlx5e_hairpin_destroy_transport(hp);
500 mlx5_core_hairpin_destroy(hp->pair);
504 static inline u32 hash_hairpin_info(u16 peer_vhca_id, u8 prio)
506 return (peer_vhca_id << 16 | prio);
509 static struct mlx5e_hairpin_entry *mlx5e_hairpin_get(struct mlx5e_priv *priv,
510 u16 peer_vhca_id, u8 prio)
512 struct mlx5e_hairpin_entry *hpe;
513 u32 hash_key = hash_hairpin_info(peer_vhca_id, prio);
515 hash_for_each_possible(priv->fs.tc.hairpin_tbl, hpe,
516 hairpin_hlist, hash_key) {
517 if (hpe->peer_vhca_id == peer_vhca_id && hpe->prio == prio)
524 #define UNKNOWN_MATCH_PRIO 8
526 static int mlx5e_hairpin_get_prio(struct mlx5e_priv *priv,
527 struct mlx5_flow_spec *spec, u8 *match_prio)
529 void *headers_c, *headers_v;
530 u8 prio_val, prio_mask = 0;
533 #ifdef CONFIG_MLX5_CORE_EN_DCB
534 if (priv->dcbx_dp.trust_state != MLX5_QPTS_TRUST_PCP) {
535 netdev_warn(priv->netdev,
536 "only PCP trust state supported for hairpin\n");
540 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, outer_headers);
541 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
543 vlan_present = MLX5_GET(fte_match_set_lyr_2_4, headers_v, cvlan_tag);
545 prio_mask = MLX5_GET(fte_match_set_lyr_2_4, headers_c, first_prio);
546 prio_val = MLX5_GET(fte_match_set_lyr_2_4, headers_v, first_prio);
549 if (!vlan_present || !prio_mask) {
550 prio_val = UNKNOWN_MATCH_PRIO;
551 } else if (prio_mask != 0x7) {
552 netdev_warn(priv->netdev,
553 "masked priority match not supported for hairpin\n");
557 *match_prio = prio_val;
561 static int mlx5e_hairpin_flow_add(struct mlx5e_priv *priv,
562 struct mlx5e_tc_flow *flow,
563 struct mlx5e_tc_flow_parse_attr *parse_attr)
565 int peer_ifindex = parse_attr->mirred_ifindex;
566 struct mlx5_hairpin_params params;
567 struct mlx5_core_dev *peer_mdev;
568 struct mlx5e_hairpin_entry *hpe;
569 struct mlx5e_hairpin *hp;
576 peer_mdev = mlx5e_hairpin_get_mdev(dev_net(priv->netdev), peer_ifindex);
577 if (!MLX5_CAP_GEN(priv->mdev, hairpin) || !MLX5_CAP_GEN(peer_mdev, hairpin)) {
578 netdev_warn(priv->netdev, "hairpin is not supported\n");
582 peer_id = MLX5_CAP_GEN(peer_mdev, vhca_id);
583 err = mlx5e_hairpin_get_prio(priv, &parse_attr->spec, &match_prio);
586 hpe = mlx5e_hairpin_get(priv, peer_id, match_prio);
590 hpe = kzalloc(sizeof(*hpe), GFP_KERNEL);
594 INIT_LIST_HEAD(&hpe->flows);
595 hpe->peer_vhca_id = peer_id;
596 hpe->prio = match_prio;
598 params.log_data_size = 15;
599 params.log_data_size = min_t(u8, params.log_data_size,
600 MLX5_CAP_GEN(priv->mdev, log_max_hairpin_wq_data_sz));
601 params.log_data_size = max_t(u8, params.log_data_size,
602 MLX5_CAP_GEN(priv->mdev, log_min_hairpin_wq_data_sz));
604 params.log_num_packets = params.log_data_size -
605 MLX5_MPWRQ_MIN_LOG_STRIDE_SZ(priv->mdev);
606 params.log_num_packets = min_t(u8, params.log_num_packets,
607 MLX5_CAP_GEN(priv->mdev, log_max_hairpin_num_packets));
609 params.q_counter = priv->q_counter;
610 /* set hairpin pair per each 50Gbs share of the link */
611 mlx5e_get_max_linkspeed(priv->mdev, &link_speed);
612 link_speed = max_t(u32, link_speed, 50000);
613 link_speed64 = link_speed;
614 do_div(link_speed64, 50000);
615 params.num_channels = link_speed64;
617 hp = mlx5e_hairpin_create(priv, ¶ms, peer_ifindex);
620 goto create_hairpin_err;
623 netdev_dbg(priv->netdev, "add hairpin: tirn %x rqn %x peer %s sqn %x prio %d (log) data %d packets %d\n",
624 hp->tirn, hp->pair->rqn[0], hp->pair->peer_mdev->priv.name,
625 hp->pair->sqn[0], match_prio, params.log_data_size, params.log_num_packets);
628 hash_add(priv->fs.tc.hairpin_tbl, &hpe->hairpin_hlist,
629 hash_hairpin_info(peer_id, match_prio));
632 if (hpe->hp->num_channels > 1) {
633 flow->flags |= MLX5E_TC_FLOW_HAIRPIN_RSS;
634 flow->nic_attr->hairpin_ft = hpe->hp->ttc.ft.t;
636 flow->nic_attr->hairpin_tirn = hpe->hp->tirn;
638 list_add(&flow->hairpin, &hpe->flows);
647 static void mlx5e_hairpin_flow_del(struct mlx5e_priv *priv,
648 struct mlx5e_tc_flow *flow)
650 struct list_head *next = flow->hairpin.next;
652 list_del(&flow->hairpin);
654 /* no more hairpin flows for us, release the hairpin pair */
655 if (list_empty(next)) {
656 struct mlx5e_hairpin_entry *hpe;
658 hpe = list_entry(next, struct mlx5e_hairpin_entry, flows);
660 netdev_dbg(priv->netdev, "del hairpin: peer %s\n",
661 hpe->hp->pair->peer_mdev->priv.name);
663 mlx5e_hairpin_destroy(hpe->hp);
664 hash_del(&hpe->hairpin_hlist);
669 static struct mlx5_flow_handle *
670 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
671 struct mlx5e_tc_flow_parse_attr *parse_attr,
672 struct mlx5e_tc_flow *flow)
674 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
675 struct mlx5_core_dev *dev = priv->mdev;
676 struct mlx5_flow_destination dest[2] = {};
677 struct mlx5_flow_act flow_act = {
678 .action = attr->action,
679 .has_flow_tag = true,
680 .flow_tag = attr->flow_tag,
683 struct mlx5_fc *counter = NULL;
684 struct mlx5_flow_handle *rule;
685 bool table_created = false;
686 int err, dest_ix = 0;
688 if (flow->flags & MLX5E_TC_FLOW_HAIRPIN) {
689 err = mlx5e_hairpin_flow_add(priv, flow, parse_attr);
692 goto err_add_hairpin_flow;
694 if (flow->flags & MLX5E_TC_FLOW_HAIRPIN_RSS) {
695 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
696 dest[dest_ix].ft = attr->hairpin_ft;
698 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_TIR;
699 dest[dest_ix].tir_num = attr->hairpin_tirn;
702 } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
703 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
704 dest[dest_ix].ft = priv->fs.vlan.ft.t;
708 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
709 counter = mlx5_fc_create(dev, true);
710 if (IS_ERR(counter)) {
711 rule = ERR_CAST(counter);
714 dest[dest_ix].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
715 dest[dest_ix].counter = counter;
719 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
720 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
721 flow_act.modify_id = attr->mod_hdr_id;
722 kfree(parse_attr->mod_hdr_actions);
725 goto err_create_mod_hdr_id;
729 if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
730 int tc_grp_size, tc_tbl_size;
731 u32 max_flow_counter;
733 max_flow_counter = (MLX5_CAP_GEN(dev, max_flow_counter_31_16) << 16) |
734 MLX5_CAP_GEN(dev, max_flow_counter_15_0);
736 tc_grp_size = min_t(int, max_flow_counter, MLX5E_TC_TABLE_MAX_GROUP_SIZE);
738 tc_tbl_size = min_t(int, tc_grp_size * MLX5E_TC_TABLE_NUM_GROUPS,
739 BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev, log_max_ft_size)));
742 mlx5_create_auto_grouped_flow_table(priv->fs.ns,
745 MLX5E_TC_TABLE_NUM_GROUPS,
746 MLX5E_TC_FT_LEVEL, 0);
747 if (IS_ERR(priv->fs.tc.t)) {
748 netdev_err(priv->netdev,
749 "Failed to create tc offload table\n");
750 rule = ERR_CAST(priv->fs.tc.t);
754 table_created = true;
757 if (attr->match_level != MLX5_MATCH_NONE)
758 parse_attr->spec.match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
760 rule = mlx5_add_flow_rules(priv->fs.tc.t, &parse_attr->spec,
761 &flow_act, dest, dest_ix);
770 mlx5_destroy_flow_table(priv->fs.tc.t);
771 priv->fs.tc.t = NULL;
774 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
775 mlx5e_detach_mod_hdr(priv, flow);
776 err_create_mod_hdr_id:
777 mlx5_fc_destroy(dev, counter);
779 if (flow->flags & MLX5E_TC_FLOW_HAIRPIN)
780 mlx5e_hairpin_flow_del(priv, flow);
781 err_add_hairpin_flow:
785 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
786 struct mlx5e_tc_flow *flow)
788 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
789 struct mlx5_fc *counter = NULL;
791 counter = mlx5_flow_rule_counter(flow->rule);
792 mlx5_del_flow_rules(flow->rule);
793 mlx5_fc_destroy(priv->mdev, counter);
795 if (!mlx5e_tc_num_filters(priv) && priv->fs.tc.t) {
796 mlx5_destroy_flow_table(priv->fs.tc.t);
797 priv->fs.tc.t = NULL;
800 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
801 mlx5e_detach_mod_hdr(priv, flow);
803 if (flow->flags & MLX5E_TC_FLOW_HAIRPIN)
804 mlx5e_hairpin_flow_del(priv, flow);
807 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
808 struct mlx5e_tc_flow *flow);
810 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
811 struct ip_tunnel_info *tun_info,
812 struct net_device *mirred_dev,
813 struct net_device **encap_dev,
814 struct mlx5e_tc_flow *flow);
816 static struct mlx5_flow_handle *
817 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
818 struct mlx5e_tc_flow_parse_attr *parse_attr,
819 struct mlx5e_tc_flow *flow)
821 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
822 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
823 struct net_device *out_dev, *encap_dev = NULL;
824 struct mlx5_flow_handle *rule = NULL;
825 struct mlx5e_rep_priv *rpriv;
826 struct mlx5e_priv *out_priv;
829 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
830 out_dev = __dev_get_by_index(dev_net(priv->netdev),
831 attr->parse_attr->mirred_ifindex);
832 err = mlx5e_attach_encap(priv, &parse_attr->tun_info,
833 out_dev, &encap_dev, flow);
837 goto err_attach_encap;
839 out_priv = netdev_priv(encap_dev);
840 rpriv = out_priv->ppriv;
841 attr->out_rep = rpriv->rep;
842 attr->out_mdev = out_priv->mdev;
845 err = mlx5_eswitch_add_vlan_action(esw, attr);
851 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
852 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
853 kfree(parse_attr->mod_hdr_actions);
860 /* we get here if (1) there's no error (rule being null) or when
861 * (2) there's an encap action and we're on -EAGAIN (no valid neigh)
863 if (rule != ERR_PTR(-EAGAIN)) {
864 rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
871 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
872 mlx5e_detach_mod_hdr(priv, flow);
874 mlx5_eswitch_del_vlan_action(esw, attr);
876 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
877 mlx5e_detach_encap(priv, flow);
882 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
883 struct mlx5e_tc_flow *flow)
885 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
886 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
888 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
889 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
890 mlx5_eswitch_del_offloaded_rule(esw, flow->rule, attr);
893 mlx5_eswitch_del_vlan_action(esw, attr);
895 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
896 mlx5e_detach_encap(priv, flow);
897 kvfree(attr->parse_attr);
900 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
901 mlx5e_detach_mod_hdr(priv, flow);
904 void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
905 struct mlx5e_encap_entry *e)
907 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
908 struct mlx5_esw_flow_attr *esw_attr;
909 struct mlx5e_tc_flow *flow;
912 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
913 e->encap_size, e->encap_header,
916 mlx5_core_warn(priv->mdev, "Failed to offload cached encapsulation header, %d\n",
920 e->flags |= MLX5_ENCAP_ENTRY_VALID;
921 mlx5e_rep_queue_neigh_stats_work(priv);
923 list_for_each_entry(flow, &e->flows, encap) {
924 esw_attr = flow->esw_attr;
925 esw_attr->encap_id = e->encap_id;
926 flow->rule = mlx5_eswitch_add_offloaded_rule(esw, &esw_attr->parse_attr->spec, esw_attr);
927 if (IS_ERR(flow->rule)) {
928 err = PTR_ERR(flow->rule);
929 mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
933 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
937 void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
938 struct mlx5e_encap_entry *e)
940 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
941 struct mlx5e_tc_flow *flow;
943 list_for_each_entry(flow, &e->flows, encap) {
944 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
945 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
946 mlx5_eswitch_del_offloaded_rule(esw, flow->rule, flow->esw_attr);
950 if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
951 e->flags &= ~MLX5_ENCAP_ENTRY_VALID;
952 mlx5_encap_dealloc(priv->mdev, e->encap_id);
956 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
958 struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
959 u64 bytes, packets, lastuse = 0;
960 struct mlx5e_tc_flow *flow;
961 struct mlx5e_encap_entry *e;
962 struct mlx5_fc *counter;
963 struct neigh_table *tbl;
964 bool neigh_used = false;
967 if (m_neigh->family == AF_INET)
969 #if IS_ENABLED(CONFIG_IPV6)
970 else if (m_neigh->family == AF_INET6)
976 list_for_each_entry(e, &nhe->encap_list, encap_list) {
977 if (!(e->flags & MLX5_ENCAP_ENTRY_VALID))
979 list_for_each_entry(flow, &e->flows, encap) {
980 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
981 counter = mlx5_flow_rule_counter(flow->rule);
982 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
983 if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
994 nhe->reported_lastuse = jiffies;
996 /* find the relevant neigh according to the cached device and
999 n = neigh_lookup(tbl, &m_neigh->dst_ip, m_neigh->dev);
1001 WARN(1, "The neighbour already freed\n");
1005 neigh_event_send(n, NULL);
1010 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
1011 struct mlx5e_tc_flow *flow)
1013 struct list_head *next = flow->encap.next;
1015 list_del(&flow->encap);
1016 if (list_empty(next)) {
1017 struct mlx5e_encap_entry *e;
1019 e = list_entry(next, struct mlx5e_encap_entry, flows);
1020 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1022 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
1023 mlx5_encap_dealloc(priv->mdev, e->encap_id);
1025 hash_del_rcu(&e->encap_hlist);
1026 kfree(e->encap_header);
1031 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
1032 struct mlx5e_tc_flow *flow)
1034 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
1035 mlx5e_tc_del_fdb_flow(priv, flow);
1037 mlx5e_tc_del_nic_flow(priv, flow);
1040 static void parse_vxlan_attr(struct mlx5_flow_spec *spec,
1041 struct tc_cls_flower_offload *f)
1043 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1045 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1047 void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1049 void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1052 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
1053 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
1055 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
1056 struct flow_dissector_key_keyid *key =
1057 skb_flow_dissector_target(f->dissector,
1058 FLOW_DISSECTOR_KEY_ENC_KEYID,
1060 struct flow_dissector_key_keyid *mask =
1061 skb_flow_dissector_target(f->dissector,
1062 FLOW_DISSECTOR_KEY_ENC_KEYID,
1064 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
1065 be32_to_cpu(mask->keyid));
1066 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
1067 be32_to_cpu(key->keyid));
1071 static int parse_tunnel_attr(struct mlx5e_priv *priv,
1072 struct mlx5_flow_spec *spec,
1073 struct tc_cls_flower_offload *f)
1075 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1077 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1080 struct flow_dissector_key_control *enc_control =
1081 skb_flow_dissector_target(f->dissector,
1082 FLOW_DISSECTOR_KEY_ENC_CONTROL,
1085 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
1086 struct flow_dissector_key_ports *key =
1087 skb_flow_dissector_target(f->dissector,
1088 FLOW_DISSECTOR_KEY_ENC_PORTS,
1090 struct flow_dissector_key_ports *mask =
1091 skb_flow_dissector_target(f->dissector,
1092 FLOW_DISSECTOR_KEY_ENC_PORTS,
1094 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1095 struct mlx5e_rep_priv *uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
1096 struct net_device *up_dev = uplink_rpriv->netdev;
1097 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
1099 /* Full udp dst port must be given */
1100 if (memchr_inv(&mask->dst, 0xff, sizeof(mask->dst)))
1101 goto vxlan_match_offload_err;
1103 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->dst)) &&
1104 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
1105 parse_vxlan_attr(spec, f);
1107 netdev_warn(priv->netdev,
1108 "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->dst));
1112 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1113 udp_dport, ntohs(mask->dst));
1114 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1115 udp_dport, ntohs(key->dst));
1117 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1118 udp_sport, ntohs(mask->src));
1119 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1120 udp_sport, ntohs(key->src));
1121 } else { /* udp dst port must be given */
1122 vxlan_match_offload_err:
1123 netdev_warn(priv->netdev,
1124 "IP tunnel decap offload supported only for vxlan, must set UDP dport\n");
1128 if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1129 struct flow_dissector_key_ipv4_addrs *key =
1130 skb_flow_dissector_target(f->dissector,
1131 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
1133 struct flow_dissector_key_ipv4_addrs *mask =
1134 skb_flow_dissector_target(f->dissector,
1135 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
1137 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1138 src_ipv4_src_ipv6.ipv4_layout.ipv4,
1140 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1141 src_ipv4_src_ipv6.ipv4_layout.ipv4,
1144 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1145 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
1147 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1148 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
1151 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
1152 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
1153 } else if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1154 struct flow_dissector_key_ipv6_addrs *key =
1155 skb_flow_dissector_target(f->dissector,
1156 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
1158 struct flow_dissector_key_ipv6_addrs *mask =
1159 skb_flow_dissector_target(f->dissector,
1160 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
1163 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1164 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1165 &mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1166 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1167 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1168 &key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1170 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1171 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1172 &mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1173 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1174 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1175 &key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
1177 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
1178 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
1181 /* Enforce DMAC when offloading incoming tunneled flows.
1182 * Flow counters require a match on the DMAC.
1184 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
1185 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
1186 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1187 dmac_47_16), priv->netdev->dev_addr);
1189 /* let software handle IP fragments */
1190 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
1191 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
1196 static int __parse_cls_flower(struct mlx5e_priv *priv,
1197 struct mlx5_flow_spec *spec,
1198 struct tc_cls_flower_offload *f,
1201 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1203 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1208 *match_level = MLX5_MATCH_NONE;
1210 if (f->dissector->used_keys &
1211 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1212 BIT(FLOW_DISSECTOR_KEY_BASIC) |
1213 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
1214 BIT(FLOW_DISSECTOR_KEY_VLAN) |
1215 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
1216 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
1217 BIT(FLOW_DISSECTOR_KEY_PORTS) |
1218 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
1219 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
1220 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
1221 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
1222 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
1223 BIT(FLOW_DISSECTOR_KEY_TCP) |
1224 BIT(FLOW_DISSECTOR_KEY_IP))) {
1225 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
1226 f->dissector->used_keys);
1230 if ((dissector_uses_key(f->dissector,
1231 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) ||
1232 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID) ||
1233 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) &&
1234 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
1235 struct flow_dissector_key_control *key =
1236 skb_flow_dissector_target(f->dissector,
1237 FLOW_DISSECTOR_KEY_ENC_CONTROL,
1239 switch (key->addr_type) {
1240 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1241 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1242 if (parse_tunnel_attr(priv, spec, f))
1249 /* In decap flow, header pointers should point to the inner
1250 * headers, outer header were already set by parse_tunnel_attr
1252 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
1254 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
1258 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1259 struct flow_dissector_key_eth_addrs *key =
1260 skb_flow_dissector_target(f->dissector,
1261 FLOW_DISSECTOR_KEY_ETH_ADDRS,
1263 struct flow_dissector_key_eth_addrs *mask =
1264 skb_flow_dissector_target(f->dissector,
1265 FLOW_DISSECTOR_KEY_ETH_ADDRS,
1268 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1271 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1275 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1278 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1282 if (!is_zero_ether_addr(mask->src) || !is_zero_ether_addr(mask->dst))
1283 *match_level = MLX5_MATCH_L2;
1286 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
1287 struct flow_dissector_key_vlan *key =
1288 skb_flow_dissector_target(f->dissector,
1289 FLOW_DISSECTOR_KEY_VLAN,
1291 struct flow_dissector_key_vlan *mask =
1292 skb_flow_dissector_target(f->dissector,
1293 FLOW_DISSECTOR_KEY_VLAN,
1295 if (mask->vlan_id || mask->vlan_priority) {
1296 MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
1297 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
1299 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
1300 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
1302 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
1303 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
1305 *match_level = MLX5_MATCH_L2;
1309 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
1310 struct flow_dissector_key_basic *key =
1311 skb_flow_dissector_target(f->dissector,
1312 FLOW_DISSECTOR_KEY_BASIC,
1314 struct flow_dissector_key_basic *mask =
1315 skb_flow_dissector_target(f->dissector,
1316 FLOW_DISSECTOR_KEY_BASIC,
1318 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
1319 ntohs(mask->n_proto));
1320 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
1321 ntohs(key->n_proto));
1324 *match_level = MLX5_MATCH_L2;
1327 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
1328 struct flow_dissector_key_control *key =
1329 skb_flow_dissector_target(f->dissector,
1330 FLOW_DISSECTOR_KEY_CONTROL,
1333 struct flow_dissector_key_control *mask =
1334 skb_flow_dissector_target(f->dissector,
1335 FLOW_DISSECTOR_KEY_CONTROL,
1337 addr_type = key->addr_type;
1339 /* the HW doesn't support frag first/later */
1340 if (mask->flags & FLOW_DIS_FIRST_FRAG)
1343 if (mask->flags & FLOW_DIS_IS_FRAGMENT) {
1344 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
1345 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
1346 key->flags & FLOW_DIS_IS_FRAGMENT);
1348 /* the HW doesn't need L3 inline to match on frag=no */
1349 if (!(key->flags & FLOW_DIS_IS_FRAGMENT))
1350 *match_level = MLX5_INLINE_MODE_L2;
1351 /* *** L2 attributes parsing up to here *** */
1353 *match_level = MLX5_INLINE_MODE_IP;
1357 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
1358 struct flow_dissector_key_basic *key =
1359 skb_flow_dissector_target(f->dissector,
1360 FLOW_DISSECTOR_KEY_BASIC,
1362 struct flow_dissector_key_basic *mask =
1363 skb_flow_dissector_target(f->dissector,
1364 FLOW_DISSECTOR_KEY_BASIC,
1366 ip_proto = key->ip_proto;
1368 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1370 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1374 *match_level = MLX5_MATCH_L3;
1377 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1378 struct flow_dissector_key_ipv4_addrs *key =
1379 skb_flow_dissector_target(f->dissector,
1380 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1382 struct flow_dissector_key_ipv4_addrs *mask =
1383 skb_flow_dissector_target(f->dissector,
1384 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
1387 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1388 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1389 &mask->src, sizeof(mask->src));
1390 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1391 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1392 &key->src, sizeof(key->src));
1393 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1394 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1395 &mask->dst, sizeof(mask->dst));
1396 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1397 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1398 &key->dst, sizeof(key->dst));
1400 if (mask->src || mask->dst)
1401 *match_level = MLX5_MATCH_L3;
1404 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1405 struct flow_dissector_key_ipv6_addrs *key =
1406 skb_flow_dissector_target(f->dissector,
1407 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1409 struct flow_dissector_key_ipv6_addrs *mask =
1410 skb_flow_dissector_target(f->dissector,
1411 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
1414 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1415 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1416 &mask->src, sizeof(mask->src));
1417 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1418 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1419 &key->src, sizeof(key->src));
1421 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1422 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1423 &mask->dst, sizeof(mask->dst));
1424 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1425 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1426 &key->dst, sizeof(key->dst));
1428 if (ipv6_addr_type(&mask->src) != IPV6_ADDR_ANY ||
1429 ipv6_addr_type(&mask->dst) != IPV6_ADDR_ANY)
1430 *match_level = MLX5_MATCH_L3;
1433 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_IP)) {
1434 struct flow_dissector_key_ip *key =
1435 skb_flow_dissector_target(f->dissector,
1436 FLOW_DISSECTOR_KEY_IP,
1438 struct flow_dissector_key_ip *mask =
1439 skb_flow_dissector_target(f->dissector,
1440 FLOW_DISSECTOR_KEY_IP,
1443 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn, mask->tos & 0x3);
1444 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, key->tos & 0x3);
1446 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp, mask->tos >> 2);
1447 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, key->tos >> 2);
1449 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit, mask->ttl);
1450 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit, key->ttl);
1453 !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
1454 ft_field_support.outer_ipv4_ttl))
1457 if (mask->tos || mask->ttl)
1458 *match_level = MLX5_MATCH_L3;
1461 /* *** L3 attributes parsing up to here *** */
1463 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
1464 struct flow_dissector_key_ports *key =
1465 skb_flow_dissector_target(f->dissector,
1466 FLOW_DISSECTOR_KEY_PORTS,
1468 struct flow_dissector_key_ports *mask =
1469 skb_flow_dissector_target(f->dissector,
1470 FLOW_DISSECTOR_KEY_PORTS,
1474 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1475 tcp_sport, ntohs(mask->src));
1476 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1477 tcp_sport, ntohs(key->src));
1479 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1480 tcp_dport, ntohs(mask->dst));
1481 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1482 tcp_dport, ntohs(key->dst));
1486 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1487 udp_sport, ntohs(mask->src));
1488 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1489 udp_sport, ntohs(key->src));
1491 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1492 udp_dport, ntohs(mask->dst));
1493 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1494 udp_dport, ntohs(key->dst));
1497 netdev_err(priv->netdev,
1498 "Only UDP and TCP transport are supported\n");
1502 if (mask->src || mask->dst)
1503 *match_level = MLX5_MATCH_L4;
1506 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_TCP)) {
1507 struct flow_dissector_key_tcp *key =
1508 skb_flow_dissector_target(f->dissector,
1509 FLOW_DISSECTOR_KEY_TCP,
1511 struct flow_dissector_key_tcp *mask =
1512 skb_flow_dissector_target(f->dissector,
1513 FLOW_DISSECTOR_KEY_TCP,
1516 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
1517 ntohs(mask->flags));
1518 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
1522 *match_level = MLX5_MATCH_L4;
1528 static int parse_cls_flower(struct mlx5e_priv *priv,
1529 struct mlx5e_tc_flow *flow,
1530 struct mlx5_flow_spec *spec,
1531 struct tc_cls_flower_offload *f)
1533 struct mlx5_core_dev *dev = priv->mdev;
1534 struct mlx5_eswitch *esw = dev->priv.eswitch;
1535 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1536 struct mlx5_eswitch_rep *rep;
1540 err = __parse_cls_flower(priv, spec, f, &match_level);
1542 if (!err && (flow->flags & MLX5E_TC_FLOW_ESWITCH)) {
1544 if (rep->vport != FDB_UPLINK_VPORT &&
1545 (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
1546 esw->offloads.inline_mode < match_level)) {
1547 netdev_warn(priv->netdev,
1548 "Flow is not offloaded due to min inline setting, required %d actual %d\n",
1549 match_level, esw->offloads.inline_mode);
1554 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
1555 flow->esw_attr->match_level = match_level;
1557 flow->nic_attr->match_level = match_level;
1562 struct pedit_headers {
1570 static int pedit_header_offsets[] = {
1571 [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
1572 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
1573 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
1574 [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
1575 [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
1578 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
1580 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
1581 struct pedit_headers *masks,
1582 struct pedit_headers *vals)
1584 u32 *curr_pmask, *curr_pval;
1586 if (hdr_type >= __PEDIT_HDR_TYPE_MAX)
1589 curr_pmask = (u32 *)(pedit_header(masks, hdr_type) + offset);
1590 curr_pval = (u32 *)(pedit_header(vals, hdr_type) + offset);
1592 if (*curr_pmask & mask) /* disallow acting twice on the same location */
1595 *curr_pmask |= mask;
1596 *curr_pval |= (val & mask);
1604 struct mlx5_fields {
1610 #define OFFLOAD(fw_field, size, field, off) \
1611 {MLX5_ACTION_IN_FIELD_OUT_ ## fw_field, size, offsetof(struct pedit_headers, field) + (off)}
1613 static struct mlx5_fields fields[] = {
1614 OFFLOAD(DMAC_47_16, 4, eth.h_dest[0], 0),
1615 OFFLOAD(DMAC_15_0, 2, eth.h_dest[4], 0),
1616 OFFLOAD(SMAC_47_16, 4, eth.h_source[0], 0),
1617 OFFLOAD(SMAC_15_0, 2, eth.h_source[4], 0),
1618 OFFLOAD(ETHERTYPE, 2, eth.h_proto, 0),
1620 OFFLOAD(IP_TTL, 1, ip4.ttl, 0),
1621 OFFLOAD(SIPV4, 4, ip4.saddr, 0),
1622 OFFLOAD(DIPV4, 4, ip4.daddr, 0),
1624 OFFLOAD(SIPV6_127_96, 4, ip6.saddr.s6_addr32[0], 0),
1625 OFFLOAD(SIPV6_95_64, 4, ip6.saddr.s6_addr32[1], 0),
1626 OFFLOAD(SIPV6_63_32, 4, ip6.saddr.s6_addr32[2], 0),
1627 OFFLOAD(SIPV6_31_0, 4, ip6.saddr.s6_addr32[3], 0),
1628 OFFLOAD(DIPV6_127_96, 4, ip6.daddr.s6_addr32[0], 0),
1629 OFFLOAD(DIPV6_95_64, 4, ip6.daddr.s6_addr32[1], 0),
1630 OFFLOAD(DIPV6_63_32, 4, ip6.daddr.s6_addr32[2], 0),
1631 OFFLOAD(DIPV6_31_0, 4, ip6.daddr.s6_addr32[3], 0),
1632 OFFLOAD(IPV6_HOPLIMIT, 1, ip6.hop_limit, 0),
1634 OFFLOAD(TCP_SPORT, 2, tcp.source, 0),
1635 OFFLOAD(TCP_DPORT, 2, tcp.dest, 0),
1636 OFFLOAD(TCP_FLAGS, 1, tcp.ack_seq, 5),
1638 OFFLOAD(UDP_SPORT, 2, udp.source, 0),
1639 OFFLOAD(UDP_DPORT, 2, udp.dest, 0),
1642 /* On input attr->num_mod_hdr_actions tells how many HW actions can be parsed at
1643 * max from the SW pedit action. On success, it says how many HW actions were
1646 static int offload_pedit_fields(struct pedit_headers *masks,
1647 struct pedit_headers *vals,
1648 struct mlx5e_tc_flow_parse_attr *parse_attr)
1650 struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
1651 int i, action_size, nactions, max_actions, first, last, next_z;
1652 void *s_masks_p, *a_masks_p, *vals_p;
1653 struct mlx5_fields *f;
1654 u8 cmd, field_bsize;
1661 set_masks = &masks[TCA_PEDIT_KEY_EX_CMD_SET];
1662 add_masks = &masks[TCA_PEDIT_KEY_EX_CMD_ADD];
1663 set_vals = &vals[TCA_PEDIT_KEY_EX_CMD_SET];
1664 add_vals = &vals[TCA_PEDIT_KEY_EX_CMD_ADD];
1666 action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1667 action = parse_attr->mod_hdr_actions;
1668 max_actions = parse_attr->num_mod_hdr_actions;
1671 for (i = 0; i < ARRAY_SIZE(fields); i++) {
1673 /* avoid seeing bits set from previous iterations */
1677 s_masks_p = (void *)set_masks + f->offset;
1678 a_masks_p = (void *)add_masks + f->offset;
1680 memcpy(&s_mask, s_masks_p, f->size);
1681 memcpy(&a_mask, a_masks_p, f->size);
1683 if (!s_mask && !a_mask) /* nothing to offload here */
1686 if (s_mask && a_mask) {
1687 printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
1691 if (nactions == max_actions) {
1692 printk(KERN_WARNING "mlx5: parsed %d pedit actions, can't do more\n", nactions);
1697 cmd = MLX5_ACTION_TYPE_SET;
1699 vals_p = (void *)set_vals + f->offset;
1700 /* clear to denote we consumed this field */
1701 memset(s_masks_p, 0, f->size);
1703 cmd = MLX5_ACTION_TYPE_ADD;
1705 vals_p = (void *)add_vals + f->offset;
1706 /* clear to denote we consumed this field */
1707 memset(a_masks_p, 0, f->size);
1710 field_bsize = f->size * BITS_PER_BYTE;
1712 if (field_bsize == 32) {
1713 mask_be32 = *(__be32 *)&mask;
1714 mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
1715 } else if (field_bsize == 16) {
1716 mask_be16 = *(__be16 *)&mask;
1717 mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
1720 first = find_first_bit(&mask, field_bsize);
1721 next_z = find_next_zero_bit(&mask, field_bsize, first);
1722 last = find_last_bit(&mask, field_bsize);
1723 if (first < next_z && next_z < last) {
1724 printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
1729 MLX5_SET(set_action_in, action, action_type, cmd);
1730 MLX5_SET(set_action_in, action, field, f->field);
1732 if (cmd == MLX5_ACTION_TYPE_SET) {
1733 MLX5_SET(set_action_in, action, offset, first);
1734 /* length is num of bits to be written, zero means length of 32 */
1735 MLX5_SET(set_action_in, action, length, (last - first + 1));
1738 if (field_bsize == 32)
1739 MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
1740 else if (field_bsize == 16)
1741 MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
1742 else if (field_bsize == 8)
1743 MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
1745 action += action_size;
1749 parse_attr->num_mod_hdr_actions = nactions;
1753 static int alloc_mod_hdr_actions(struct mlx5e_priv *priv,
1754 const struct tc_action *a, int namespace,
1755 struct mlx5e_tc_flow_parse_attr *parse_attr)
1757 int nkeys, action_size, max_actions;
1759 nkeys = tcf_pedit_nkeys(a);
1760 action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1762 if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
1763 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, max_modify_header_actions);
1764 else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
1765 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, max_modify_header_actions);
1767 /* can get up to crazingly 16 HW actions in 32 bits pedit SW key */
1768 max_actions = min(max_actions, nkeys * 16);
1770 parse_attr->mod_hdr_actions = kcalloc(max_actions, action_size, GFP_KERNEL);
1771 if (!parse_attr->mod_hdr_actions)
1774 parse_attr->num_mod_hdr_actions = max_actions;
1778 static const struct pedit_headers zero_masks = {};
1780 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
1781 const struct tc_action *a, int namespace,
1782 struct mlx5e_tc_flow_parse_attr *parse_attr)
1784 struct pedit_headers masks[__PEDIT_CMD_MAX], vals[__PEDIT_CMD_MAX], *cmd_masks;
1785 int nkeys, i, err = -EOPNOTSUPP;
1786 u32 mask, val, offset;
1789 nkeys = tcf_pedit_nkeys(a);
1791 memset(masks, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1792 memset(vals, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1794 for (i = 0; i < nkeys; i++) {
1795 htype = tcf_pedit_htype(a, i);
1796 cmd = tcf_pedit_cmd(a, i);
1797 err = -EOPNOTSUPP; /* can't be all optimistic */
1799 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
1800 netdev_warn(priv->netdev, "legacy pedit isn't offloaded\n");
1804 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET && cmd != TCA_PEDIT_KEY_EX_CMD_ADD) {
1805 netdev_warn(priv->netdev, "pedit cmd %d isn't offloaded\n", cmd);
1809 mask = tcf_pedit_mask(a, i);
1810 val = tcf_pedit_val(a, i);
1811 offset = tcf_pedit_offset(a, i);
1813 err = set_pedit_val(htype, ~mask, val, offset, &masks[cmd], &vals[cmd]);
1818 err = alloc_mod_hdr_actions(priv, a, namespace, parse_attr);
1822 err = offload_pedit_fields(masks, vals, parse_attr);
1824 goto out_dealloc_parsed_actions;
1826 for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
1827 cmd_masks = &masks[cmd];
1828 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
1829 netdev_warn(priv->netdev, "attempt to offload an unsupported field (cmd %d)\n", cmd);
1830 print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
1831 16, 1, cmd_masks, sizeof(zero_masks), true);
1833 goto out_dealloc_parsed_actions;
1839 out_dealloc_parsed_actions:
1840 kfree(parse_attr->mod_hdr_actions);
1845 static bool csum_offload_supported(struct mlx5e_priv *priv, u32 action, u32 update_flags)
1847 u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
1848 TCA_CSUM_UPDATE_FLAG_UDP;
1850 /* The HW recalcs checksums only if re-writing headers */
1851 if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
1852 netdev_warn(priv->netdev,
1853 "TC csum action is only offloaded with pedit\n");
1857 if (update_flags & ~prot_flags) {
1858 netdev_warn(priv->netdev,
1859 "can't offload TC csum action for some header/s - flags %#x\n",
1867 static bool modify_header_match_supported(struct mlx5_flow_spec *spec,
1868 struct tcf_exts *exts)
1870 const struct tc_action *a;
1871 bool modify_ip_header;
1878 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
1879 ethertype = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ethertype);
1881 /* for non-IP we only re-write MACs, so we're okay */
1882 if (ethertype != ETH_P_IP && ethertype != ETH_P_IPV6)
1885 modify_ip_header = false;
1886 tcf_exts_to_list(exts, &actions);
1887 list_for_each_entry(a, &actions, list) {
1888 if (!is_tcf_pedit(a))
1891 nkeys = tcf_pedit_nkeys(a);
1892 for (i = 0; i < nkeys; i++) {
1893 htype = tcf_pedit_htype(a, i);
1894 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP4 ||
1895 htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP6) {
1896 modify_ip_header = true;
1902 ip_proto = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ip_protocol);
1903 if (modify_ip_header && ip_proto != IPPROTO_TCP &&
1904 ip_proto != IPPROTO_UDP && ip_proto != IPPROTO_ICMP) {
1905 pr_info("can't offload re-write of ip proto %d\n", ip_proto);
1913 static bool actions_match_supported(struct mlx5e_priv *priv,
1914 struct tcf_exts *exts,
1915 struct mlx5e_tc_flow_parse_attr *parse_attr,
1916 struct mlx5e_tc_flow *flow)
1920 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
1921 actions = flow->esw_attr->action;
1923 actions = flow->nic_attr->action;
1925 if (actions & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
1926 return modify_header_match_supported(&parse_attr->spec, exts);
1931 static bool same_hw_devs(struct mlx5e_priv *priv, struct mlx5e_priv *peer_priv)
1933 struct mlx5_core_dev *fmdev, *pmdev;
1934 u16 func_id, peer_id;
1937 pmdev = peer_priv->mdev;
1939 func_id = (u16)((fmdev->pdev->bus->number << 8) | PCI_SLOT(fmdev->pdev->devfn));
1940 peer_id = (u16)((pmdev->pdev->bus->number << 8) | PCI_SLOT(pmdev->pdev->devfn));
1942 return (func_id == peer_id);
1945 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1946 struct mlx5e_tc_flow_parse_attr *parse_attr,
1947 struct mlx5e_tc_flow *flow)
1949 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
1950 const struct tc_action *a;
1955 if (!tcf_exts_has_actions(exts))
1958 attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
1960 tcf_exts_to_list(exts, &actions);
1961 list_for_each_entry(a, &actions, list) {
1962 if (is_tcf_gact_shot(a)) {
1963 action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
1964 if (MLX5_CAP_FLOWTABLE(priv->mdev,
1965 flow_table_properties_nic_receive.flow_counter))
1966 action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
1970 if (is_tcf_pedit(a)) {
1971 err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_KERNEL,
1976 action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1977 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1981 if (is_tcf_csum(a)) {
1982 if (csum_offload_supported(priv, action,
1983 tcf_csum_update_flags(a)))
1989 if (is_tcf_mirred_egress_redirect(a)) {
1990 struct net_device *peer_dev = tcf_mirred_dev(a);
1992 if (priv->netdev->netdev_ops == peer_dev->netdev_ops &&
1993 same_hw_devs(priv, netdev_priv(peer_dev))) {
1994 parse_attr->mirred_ifindex = peer_dev->ifindex;
1995 flow->flags |= MLX5E_TC_FLOW_HAIRPIN;
1996 action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1997 MLX5_FLOW_CONTEXT_ACTION_COUNT;
1999 netdev_warn(priv->netdev, "device %s not on same HW, can't offload\n",
2006 if (is_tcf_skbedit_mark(a)) {
2007 u32 mark = tcf_skbedit_mark(a);
2009 if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
2010 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
2015 attr->flow_tag = mark;
2016 action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
2023 attr->action = action;
2024 if (!actions_match_supported(priv, exts, parse_attr, flow))
2030 static inline int cmp_encap_info(struct ip_tunnel_key *a,
2031 struct ip_tunnel_key *b)
2033 return memcmp(a, b, sizeof(*a));
2036 static inline int hash_encap_info(struct ip_tunnel_key *key)
2038 return jhash(key, sizeof(*key), 0);
2041 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
2042 struct net_device *mirred_dev,
2043 struct net_device **out_dev,
2045 struct neighbour **out_n,
2048 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2049 struct mlx5e_rep_priv *uplink_rpriv;
2051 struct neighbour *n = NULL;
2053 #if IS_ENABLED(CONFIG_INET)
2056 rt = ip_route_output_key(dev_net(mirred_dev), fl4);
2057 ret = PTR_ERR_OR_ZERO(rt);
2063 uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
2064 /* if the egress device isn't on the same HW e-switch, we use the uplink */
2065 if (!switchdev_port_same_parent_id(priv->netdev, rt->dst.dev))
2066 *out_dev = uplink_rpriv->netdev;
2068 *out_dev = rt->dst.dev;
2070 *out_ttl = ip4_dst_hoplimit(&rt->dst);
2071 n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
2080 static bool is_merged_eswitch_dev(struct mlx5e_priv *priv,
2081 struct net_device *peer_netdev)
2083 struct mlx5e_priv *peer_priv;
2085 peer_priv = netdev_priv(peer_netdev);
2087 return (MLX5_CAP_ESW(priv->mdev, merged_eswitch) &&
2088 (priv->netdev->netdev_ops == peer_netdev->netdev_ops) &&
2089 same_hw_devs(priv, peer_priv) &&
2090 MLX5_VPORT_MANAGER(peer_priv->mdev) &&
2091 (peer_priv->mdev->priv.eswitch->mode == SRIOV_OFFLOADS));
2094 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
2095 struct net_device *mirred_dev,
2096 struct net_device **out_dev,
2098 struct neighbour **out_n,
2101 struct neighbour *n = NULL;
2102 struct dst_entry *dst;
2104 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
2105 struct mlx5e_rep_priv *uplink_rpriv;
2106 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2109 ret = ipv6_stub->ipv6_dst_lookup(dev_net(mirred_dev), NULL, &dst,
2114 *out_ttl = ip6_dst_hoplimit(dst);
2116 uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
2117 /* if the egress device isn't on the same HW e-switch, we use the uplink */
2118 if (!switchdev_port_same_parent_id(priv->netdev, dst->dev))
2119 *out_dev = uplink_rpriv->netdev;
2121 *out_dev = dst->dev;
2126 n = dst_neigh_lookup(dst, &fl6->daddr);
2135 static void gen_vxlan_header_ipv4(struct net_device *out_dev,
2136 char buf[], int encap_size,
2137 unsigned char h_dest[ETH_ALEN],
2141 __be16 udp_dst_port,
2144 struct ethhdr *eth = (struct ethhdr *)buf;
2145 struct iphdr *ip = (struct iphdr *)((char *)eth + sizeof(struct ethhdr));
2146 struct udphdr *udp = (struct udphdr *)((char *)ip + sizeof(struct iphdr));
2147 struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
2149 memset(buf, 0, encap_size);
2151 ether_addr_copy(eth->h_dest, h_dest);
2152 ether_addr_copy(eth->h_source, out_dev->dev_addr);
2153 eth->h_proto = htons(ETH_P_IP);
2159 ip->protocol = IPPROTO_UDP;
2163 udp->dest = udp_dst_port;
2164 vxh->vx_flags = VXLAN_HF_VNI;
2165 vxh->vx_vni = vxlan_vni_field(vx_vni);
2168 static void gen_vxlan_header_ipv6(struct net_device *out_dev,
2169 char buf[], int encap_size,
2170 unsigned char h_dest[ETH_ALEN],
2172 struct in6_addr *daddr,
2173 struct in6_addr *saddr,
2174 __be16 udp_dst_port,
2177 struct ethhdr *eth = (struct ethhdr *)buf;
2178 struct ipv6hdr *ip6h = (struct ipv6hdr *)((char *)eth + sizeof(struct ethhdr));
2179 struct udphdr *udp = (struct udphdr *)((char *)ip6h + sizeof(struct ipv6hdr));
2180 struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
2182 memset(buf, 0, encap_size);
2184 ether_addr_copy(eth->h_dest, h_dest);
2185 ether_addr_copy(eth->h_source, out_dev->dev_addr);
2186 eth->h_proto = htons(ETH_P_IPV6);
2188 ip6_flow_hdr(ip6h, 0, 0);
2189 /* the HW fills up ipv6 payload len */
2190 ip6h->nexthdr = IPPROTO_UDP;
2191 ip6h->hop_limit = ttl;
2192 ip6h->daddr = *daddr;
2193 ip6h->saddr = *saddr;
2195 udp->dest = udp_dst_port;
2196 vxh->vx_flags = VXLAN_HF_VNI;
2197 vxh->vx_vni = vxlan_vni_field(vx_vni);
2200 static int mlx5e_create_encap_header_ipv4(struct mlx5e_priv *priv,
2201 struct net_device *mirred_dev,
2202 struct mlx5e_encap_entry *e)
2204 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
2205 int ipv4_encap_size = ETH_HLEN + sizeof(struct iphdr) + VXLAN_HLEN;
2206 struct ip_tunnel_key *tun_key = &e->tun_info.key;
2207 struct net_device *out_dev;
2208 struct neighbour *n = NULL;
2209 struct flowi4 fl4 = {};
2214 if (max_encap_size < ipv4_encap_size) {
2215 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
2216 ipv4_encap_size, max_encap_size);
2220 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
2224 switch (e->tunnel_type) {
2225 case MLX5_HEADER_TYPE_VXLAN:
2226 fl4.flowi4_proto = IPPROTO_UDP;
2227 fl4.fl4_dport = tun_key->tp_dst;
2233 fl4.flowi4_tos = tun_key->tos;
2234 fl4.daddr = tun_key->u.ipv4.dst;
2235 fl4.saddr = tun_key->u.ipv4.src;
2237 err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev,
2242 /* used by mlx5e_detach_encap to lookup a neigh hash table
2243 * entry in the neigh hash table when a user deletes a rule
2245 e->m_neigh.dev = n->dev;
2246 e->m_neigh.family = n->ops->family;
2247 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
2248 e->out_dev = out_dev;
2250 /* It's importent to add the neigh to the hash table before checking
2251 * the neigh validity state. So if we'll get a notification, in case the
2252 * neigh changes it's validity state, we would find the relevant neigh
2255 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
2259 read_lock_bh(&n->lock);
2260 nud_state = n->nud_state;
2261 ether_addr_copy(e->h_dest, n->ha);
2262 read_unlock_bh(&n->lock);
2264 switch (e->tunnel_type) {
2265 case MLX5_HEADER_TYPE_VXLAN:
2266 gen_vxlan_header_ipv4(out_dev, encap_header,
2267 ipv4_encap_size, e->h_dest, ttl,
2269 fl4.saddr, tun_key->tp_dst,
2270 tunnel_id_to_key32(tun_key->tun_id));
2274 goto destroy_neigh_entry;
2276 e->encap_size = ipv4_encap_size;
2277 e->encap_header = encap_header;
2279 if (!(nud_state & NUD_VALID)) {
2280 neigh_event_send(n, NULL);
2285 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
2286 ipv4_encap_size, encap_header, &e->encap_id);
2288 goto destroy_neigh_entry;
2290 e->flags |= MLX5_ENCAP_ENTRY_VALID;
2291 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
2295 destroy_neigh_entry:
2296 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
2298 kfree(encap_header);
2305 static int mlx5e_create_encap_header_ipv6(struct mlx5e_priv *priv,
2306 struct net_device *mirred_dev,
2307 struct mlx5e_encap_entry *e)
2309 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
2310 int ipv6_encap_size = ETH_HLEN + sizeof(struct ipv6hdr) + VXLAN_HLEN;
2311 struct ip_tunnel_key *tun_key = &e->tun_info.key;
2312 struct net_device *out_dev;
2313 struct neighbour *n = NULL;
2314 struct flowi6 fl6 = {};
2319 if (max_encap_size < ipv6_encap_size) {
2320 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
2321 ipv6_encap_size, max_encap_size);
2325 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
2329 switch (e->tunnel_type) {
2330 case MLX5_HEADER_TYPE_VXLAN:
2331 fl6.flowi6_proto = IPPROTO_UDP;
2332 fl6.fl6_dport = tun_key->tp_dst;
2339 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
2340 fl6.daddr = tun_key->u.ipv6.dst;
2341 fl6.saddr = tun_key->u.ipv6.src;
2343 err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev,
2348 /* used by mlx5e_detach_encap to lookup a neigh hash table
2349 * entry in the neigh hash table when a user deletes a rule
2351 e->m_neigh.dev = n->dev;
2352 e->m_neigh.family = n->ops->family;
2353 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
2354 e->out_dev = out_dev;
2356 /* It's importent to add the neigh to the hash table before checking
2357 * the neigh validity state. So if we'll get a notification, in case the
2358 * neigh changes it's validity state, we would find the relevant neigh
2361 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
2365 read_lock_bh(&n->lock);
2366 nud_state = n->nud_state;
2367 ether_addr_copy(e->h_dest, n->ha);
2368 read_unlock_bh(&n->lock);
2370 switch (e->tunnel_type) {
2371 case MLX5_HEADER_TYPE_VXLAN:
2372 gen_vxlan_header_ipv6(out_dev, encap_header,
2373 ipv6_encap_size, e->h_dest, ttl,
2375 &fl6.saddr, tun_key->tp_dst,
2376 tunnel_id_to_key32(tun_key->tun_id));
2380 goto destroy_neigh_entry;
2383 e->encap_size = ipv6_encap_size;
2384 e->encap_header = encap_header;
2386 if (!(nud_state & NUD_VALID)) {
2387 neigh_event_send(n, NULL);
2392 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
2393 ipv6_encap_size, encap_header, &e->encap_id);
2395 goto destroy_neigh_entry;
2397 e->flags |= MLX5_ENCAP_ENTRY_VALID;
2398 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
2402 destroy_neigh_entry:
2403 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
2405 kfree(encap_header);
2412 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
2413 struct ip_tunnel_info *tun_info,
2414 struct net_device *mirred_dev,
2415 struct net_device **encap_dev,
2416 struct mlx5e_tc_flow *flow)
2418 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2419 struct mlx5e_rep_priv *uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw,
2421 struct net_device *up_dev = uplink_rpriv->netdev;
2422 unsigned short family = ip_tunnel_info_af(tun_info);
2423 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
2424 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
2425 struct ip_tunnel_key *key = &tun_info->key;
2426 struct mlx5e_encap_entry *e;
2427 int tunnel_type, err = 0;
2431 /* udp dst port must be set */
2432 if (!memchr_inv(&key->tp_dst, 0, sizeof(key->tp_dst)))
2433 goto vxlan_encap_offload_err;
2435 /* setting udp src port isn't supported */
2436 if (memchr_inv(&key->tp_src, 0, sizeof(key->tp_src))) {
2437 vxlan_encap_offload_err:
2438 netdev_warn(priv->netdev,
2439 "must set udp dst port and not set udp src port\n");
2443 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->tp_dst)) &&
2444 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap)) {
2445 tunnel_type = MLX5_HEADER_TYPE_VXLAN;
2447 netdev_warn(priv->netdev,
2448 "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->tp_dst));
2452 hash_key = hash_encap_info(key);
2454 hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
2455 encap_hlist, hash_key) {
2456 if (!cmp_encap_info(&e->tun_info.key, key)) {
2462 /* must verify if encap is valid or not */
2466 e = kzalloc(sizeof(*e), GFP_KERNEL);
2470 e->tun_info = *tun_info;
2471 e->tunnel_type = tunnel_type;
2472 INIT_LIST_HEAD(&e->flows);
2474 if (family == AF_INET)
2475 err = mlx5e_create_encap_header_ipv4(priv, mirred_dev, e);
2476 else if (family == AF_INET6)
2477 err = mlx5e_create_encap_header_ipv6(priv, mirred_dev, e);
2479 if (err && err != -EAGAIN)
2482 hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
2485 list_add(&flow->encap, &e->flows);
2486 *encap_dev = e->out_dev;
2487 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
2488 attr->encap_id = e->encap_id;
2499 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
2500 struct mlx5e_tc_flow_parse_attr *parse_attr,
2501 struct mlx5e_tc_flow *flow)
2503 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
2504 struct mlx5e_rep_priv *rpriv = priv->ppriv;
2505 struct ip_tunnel_info *info = NULL;
2506 const struct tc_action *a;
2511 if (!tcf_exts_has_actions(exts))
2514 attr->in_rep = rpriv->rep;
2515 attr->in_mdev = priv->mdev;
2517 tcf_exts_to_list(exts, &actions);
2518 list_for_each_entry(a, &actions, list) {
2519 if (is_tcf_gact_shot(a)) {
2520 action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
2521 MLX5_FLOW_CONTEXT_ACTION_COUNT;
2525 if (is_tcf_pedit(a)) {
2528 err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_FDB,
2533 action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2537 if (is_tcf_csum(a)) {
2538 if (csum_offload_supported(priv, action,
2539 tcf_csum_update_flags(a)))
2545 if (is_tcf_mirred_egress_redirect(a)) {
2546 struct net_device *out_dev;
2547 struct mlx5e_priv *out_priv;
2549 out_dev = tcf_mirred_dev(a);
2551 if (switchdev_port_same_parent_id(priv->netdev,
2553 is_merged_eswitch_dev(priv, out_dev)) {
2554 action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2555 MLX5_FLOW_CONTEXT_ACTION_COUNT;
2556 out_priv = netdev_priv(out_dev);
2557 rpriv = out_priv->ppriv;
2558 attr->out_rep = rpriv->rep;
2559 attr->out_mdev = out_priv->mdev;
2561 parse_attr->mirred_ifindex = out_dev->ifindex;
2562 parse_attr->tun_info = *info;
2563 attr->parse_attr = parse_attr;
2564 action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
2565 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
2566 MLX5_FLOW_CONTEXT_ACTION_COUNT;
2567 /* attr->out_rep is resolved when we handle encap */
2569 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
2570 priv->netdev->name, out_dev->name);
2576 if (is_tcf_tunnel_set(a)) {
2577 info = tcf_tunnel_info(a);
2585 if (is_tcf_vlan(a)) {
2586 if (tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
2587 action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
2588 } else if (tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
2589 action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
2590 attr->vlan_vid = tcf_vlan_push_vid(a);
2591 if (mlx5_eswitch_vlan_actions_supported(priv->mdev)) {
2592 attr->vlan_prio = tcf_vlan_push_prio(a);
2593 attr->vlan_proto = tcf_vlan_push_proto(a);
2594 if (!attr->vlan_proto)
2595 attr->vlan_proto = htons(ETH_P_8021Q);
2596 } else if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q) ||
2597 tcf_vlan_push_prio(a)) {
2600 } else { /* action is TCA_VLAN_ACT_MODIFY */
2606 if (is_tcf_tunnel_release(a)) {
2607 action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2614 attr->action = action;
2615 if (!actions_match_supported(priv, exts, parse_attr, flow))
2621 int mlx5e_configure_flower(struct mlx5e_priv *priv,
2622 struct tc_cls_flower_offload *f)
2624 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2625 struct mlx5e_tc_flow_parse_attr *parse_attr;
2626 struct mlx5e_tc_table *tc = &priv->fs.tc;
2627 struct mlx5e_tc_flow *flow;
2628 int attr_size, err = 0;
2631 if (esw && esw->mode == SRIOV_OFFLOADS) {
2632 flow_flags = MLX5E_TC_FLOW_ESWITCH;
2633 attr_size = sizeof(struct mlx5_esw_flow_attr);
2635 flow_flags = MLX5E_TC_FLOW_NIC;
2636 attr_size = sizeof(struct mlx5_nic_flow_attr);
2639 flow = kzalloc(sizeof(*flow) + attr_size, GFP_KERNEL);
2640 parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
2641 if (!parse_attr || !flow) {
2646 flow->cookie = f->cookie;
2647 flow->flags = flow_flags;
2649 err = parse_cls_flower(priv, flow, &parse_attr->spec, f);
2653 if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
2654 err = parse_tc_fdb_actions(priv, f->exts, parse_attr, flow);
2657 flow->rule = mlx5e_tc_add_fdb_flow(priv, parse_attr, flow);
2659 err = parse_tc_nic_actions(priv, f->exts, parse_attr, flow);
2662 flow->rule = mlx5e_tc_add_nic_flow(priv, parse_attr, flow);
2665 if (IS_ERR(flow->rule)) {
2666 err = PTR_ERR(flow->rule);
2672 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
2674 if (!(flow->flags & MLX5E_TC_FLOW_ESWITCH) ||
2675 !(flow->esw_attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP))
2678 err = rhashtable_insert_fast(&tc->ht, &flow->node,
2681 mlx5e_tc_del_flow(priv, flow);
2693 int mlx5e_delete_flower(struct mlx5e_priv *priv,
2694 struct tc_cls_flower_offload *f)
2696 struct mlx5e_tc_flow *flow;
2697 struct mlx5e_tc_table *tc = &priv->fs.tc;
2699 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2704 rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
2706 mlx5e_tc_del_flow(priv, flow);
2713 int mlx5e_stats_flower(struct mlx5e_priv *priv,
2714 struct tc_cls_flower_offload *f)
2716 struct mlx5e_tc_table *tc = &priv->fs.tc;
2717 struct mlx5e_tc_flow *flow;
2718 struct mlx5_fc *counter;
2723 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2728 if (!(flow->flags & MLX5E_TC_FLOW_OFFLOADED))
2731 counter = mlx5_flow_rule_counter(flow->rule);
2735 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
2737 tcf_exts_stats_update(f->exts, bytes, packets, lastuse);
2742 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
2743 .head_offset = offsetof(struct mlx5e_tc_flow, node),
2744 .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
2745 .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
2746 .automatic_shrinking = true,
2749 int mlx5e_tc_init(struct mlx5e_priv *priv)
2751 struct mlx5e_tc_table *tc = &priv->fs.tc;
2753 hash_init(tc->mod_hdr_tbl);
2754 hash_init(tc->hairpin_tbl);
2756 tc->ht_params = mlx5e_tc_flow_ht_params;
2757 return rhashtable_init(&tc->ht, &tc->ht_params);
2760 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
2762 struct mlx5e_tc_flow *flow = ptr;
2763 struct mlx5e_priv *priv = arg;
2765 mlx5e_tc_del_flow(priv, flow);
2769 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
2771 struct mlx5e_tc_table *tc = &priv->fs.tc;
2773 rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
2775 if (!IS_ERR_OR_NULL(tc->t)) {
2776 mlx5_destroy_flow_table(tc->t);