en_tc.c 16.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

33
34
35
#include <net/flow_dissector.h>
#include <net/pkt_cls.h>
#include <net/tc_act/tc_gact.h>
36
#include <net/tc_act/tc_skbedit.h>
37
38
39
#include <linux/mlx5/fs.h>
#include <linux/mlx5/device.h>
#include <linux/rhashtable.h>
40
41
#include <net/switchdev.h>
#include <net/tc_act/tc_mirred.h>
42
#include <net/tc_act/tc_vlan.h>
43
44
#include "en.h"
#include "en_tc.h"
45
#include "eswitch.h"
46
47
48
49

struct mlx5e_tc_flow {
	struct rhash_head	node;
	u64			cookie;
Mark Bloch's avatar
Mark Bloch committed
50
	struct mlx5_flow_handle *rule;
51
	struct mlx5_esw_flow_attr *attr;
52
53
};

54
55
#define MLX5E_TC_TABLE_NUM_ENTRIES 1024
#define MLX5E_TC_TABLE_NUM_GROUPS 4
56

Mark Bloch's avatar
Mark Bloch committed
57
58
59
60
static struct mlx5_flow_handle *
mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
		      struct mlx5_flow_spec *spec,
		      u32 action, u32 flow_tag)
61
{
62
63
64
	struct mlx5_core_dev *dev = priv->mdev;
	struct mlx5_flow_destination dest = { 0 };
	struct mlx5_fc *counter = NULL;
Mark Bloch's avatar
Mark Bloch committed
65
	struct mlx5_flow_handle *rule;
66
67
	bool table_created = false;

68
69
70
	if (action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
		dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
		dest.ft = priv->fs.vlan.ft.t;
71
	} else if (action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
72
73
74
75
76
77
78
79
		counter = mlx5_fc_create(dev, true);
		if (IS_ERR(counter))
			return ERR_CAST(counter);

		dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
		dest.counter = counter;
	}

80
81
82
83
84
85
	if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
		priv->fs.tc.t =
			mlx5_create_auto_grouped_flow_table(priv->fs.ns,
							    MLX5E_TC_PRIO,
							    MLX5E_TC_TABLE_NUM_ENTRIES,
							    MLX5E_TC_TABLE_NUM_GROUPS,
86
							    0);
87
		if (IS_ERR(priv->fs.tc.t)) {
88
89
			netdev_err(priv->netdev,
				   "Failed to create tc offload table\n");
90
91
			rule = ERR_CAST(priv->fs.tc.t);
			goto err_create_ft;
92
93
94
95
96
		}

		table_created = true;
	}

97
	spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
Mark Bloch's avatar
Mark Bloch committed
98
99
100
	rule = mlx5_add_flow_rules(priv->fs.tc.t, spec,
				   action, flow_tag,
				   &dest, 1);
101
102
103
104
105

	if (IS_ERR(rule))
		goto err_add_rule;

	return rule;
106

107
108
err_add_rule:
	if (table_created) {
109
110
		mlx5_destroy_flow_table(priv->fs.tc.t);
		priv->fs.tc.t = NULL;
111
	}
112
113
err_create_ft:
	mlx5_fc_destroy(dev, counter);
114
115
116
117

	return rule;
}

Mark Bloch's avatar
Mark Bloch committed
118
119
120
121
static struct mlx5_flow_handle *
mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
		      struct mlx5_flow_spec *spec,
		      struct mlx5_esw_flow_attr *attr)
122
123
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
124
125
126
127
128
	int err;

	err = mlx5_eswitch_add_vlan_action(esw, attr);
	if (err)
		return ERR_PTR(err);
129

130
	return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
131
132
}

133
static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
Mark Bloch's avatar
Mark Bloch committed
134
			      struct mlx5_flow_handle *rule,
135
			      struct mlx5_esw_flow_attr *attr)
136
{
137
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
138
139
140
141
	struct mlx5_fc *counter = NULL;

	counter = mlx5_flow_rule_counter(rule);

142
143
144
	if (esw && esw->mode == SRIOV_OFFLOADS)
		mlx5_eswitch_del_vlan_action(esw, attr);

Mark Bloch's avatar
Mark Bloch committed
145
	mlx5_del_flow_rules(rule);
146

147
148
	mlx5_fc_destroy(priv->mdev, counter);

149
	if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
150
151
		mlx5_destroy_flow_table(priv->fs.tc.t);
		priv->fs.tc.t = NULL;
152
153
154
	}
}

155
static int parse_cls_flower(struct mlx5e_priv *priv, struct mlx5_flow_spec *spec,
156
157
			    struct tc_cls_flower_offload *f)
{
158
159
160
161
	void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
				       outer_headers);
	void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
				       outer_headers);
162
163
164
165
166
167
168
	u16 addr_type = 0;
	u8 ip_proto = 0;

	if (f->dissector->used_keys &
	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
169
	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
170
171
172
173
174
175
176
177
178
179
180
	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
	      BIT(FLOW_DISSECTOR_KEY_PORTS))) {
		netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
			    f->dissector->used_keys);
		return -EOPNOTSUPP;
	}

	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
		struct flow_dissector_key_control *key =
			skb_flow_dissector_target(f->dissector,
181
						  FLOW_DISSECTOR_KEY_CONTROL,
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
						  f->key);
		addr_type = key->addr_type;
	}

	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
		struct flow_dissector_key_basic *key =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_BASIC,
						  f->key);
		struct flow_dissector_key_basic *mask =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_BASIC,
						  f->mask);
		ip_proto = key->ip_proto;

		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
			 ntohs(mask->n_proto));
		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
			 ntohs(key->n_proto));

		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
			 mask->ip_proto);
		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
			 key->ip_proto);
	}

	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
		struct flow_dissector_key_eth_addrs *key =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
						  f->key);
		struct flow_dissector_key_eth_addrs *mask =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_ETH_ADDRS,
						  f->mask);

		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
					     dmac_47_16),
				mask->dst);
		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
					     dmac_47_16),
				key->dst);

		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
					     smac_47_16),
				mask->src);
		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
					     smac_47_16),
				key->src);
	}

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
		struct flow_dissector_key_vlan *key =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_VLAN,
						  f->key);
		struct flow_dissector_key_vlan *mask =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_VLAN,
						  f->mask);
		if (mask->vlan_id) {
			MLX5_SET(fte_match_set_lyr_2_4, headers_c, vlan_tag, 1);
			MLX5_SET(fte_match_set_lyr_2_4, headers_v, vlan_tag, 1);

			MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
			MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
		}
	}

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
		struct flow_dissector_key_ipv4_addrs *key =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
						  f->key);
		struct flow_dissector_key_ipv4_addrs *mask =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_IPV4_ADDRS,
						  f->mask);

		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
		       &mask->src, sizeof(mask->src));
		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
		       &key->src, sizeof(key->src));
		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
		       &mask->dst, sizeof(mask->dst));
		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
		       &key->dst, sizeof(key->dst));
	}

	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
		struct flow_dissector_key_ipv6_addrs *key =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
						  f->key);
		struct flow_dissector_key_ipv6_addrs *mask =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_IPV6_ADDRS,
						  f->mask);

		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
		       &mask->src, sizeof(mask->src));
		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
		       &key->src, sizeof(key->src));

		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
		       &mask->dst, sizeof(mask->dst));
		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
		       &key->dst, sizeof(key->dst));
	}

	if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
		struct flow_dissector_key_ports *key =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_PORTS,
						  f->key);
		struct flow_dissector_key_ports *mask =
			skb_flow_dissector_target(f->dissector,
						  FLOW_DISSECTOR_KEY_PORTS,
						  f->mask);
		switch (ip_proto) {
		case IPPROTO_TCP:
			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
				 tcp_sport, ntohs(mask->src));
			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
				 tcp_sport, ntohs(key->src));

			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
				 tcp_dport, ntohs(mask->dst));
			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
				 tcp_dport, ntohs(key->dst));
			break;

		case IPPROTO_UDP:
			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
				 udp_sport, ntohs(mask->src));
			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
				 udp_sport, ntohs(key->src));

			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
				 udp_dport, ntohs(mask->dst));
			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
				 udp_dport, ntohs(key->dst));
			break;
		default:
			netdev_err(priv->netdev,
				   "Only UDP and TCP transport are supported\n");
			return -EINVAL;
		}
	}

	return 0;
}

343
344
static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
				u32 *action, u32 *flow_tag)
345
346
{
	const struct tc_action *a;
347
	LIST_HEAD(actions);
348
349
350
351
352
353
354

	if (tc_no_actions(exts))
		return -EINVAL;

	*flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
	*action = 0;

355
356
	tcf_exts_to_list(exts, &actions);
	list_for_each_entry(a, &actions, list) {
357
358
359
360
361
362
		/* Only support a single action per rule */
		if (*action)
			return -EINVAL;

		if (is_tcf_gact_shot(a)) {
			*action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
363
364
365
			if (MLX5_CAP_FLOWTABLE(priv->mdev,
					       flow_table_properties_nic_receive.flow_counter))
				*action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
			continue;
		}

		if (is_tcf_skbedit_mark(a)) {
			u32 mark = tcf_skbedit_mark(a);

			if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
				netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
					    mark);
				return -EINVAL;
			}

			*flow_tag = mark;
			*action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
			continue;
		}

		return -EINVAL;
	}

	return 0;
}

389
static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
390
				struct mlx5_esw_flow_attr *attr)
391
392
{
	const struct tc_action *a;
393
	LIST_HEAD(actions);
394
395
396
397

	if (tc_no_actions(exts))
		return -EINVAL;

398
399
	memset(attr, 0, sizeof(*attr));
	attr->in_rep = priv->ppriv;
400

401
402
	tcf_exts_to_list(exts, &actions);
	list_for_each_entry(a, &actions, list) {
403
		if (is_tcf_gact_shot(a)) {
404
405
			attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
					MLX5_FLOW_CONTEXT_ACTION_COUNT;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
			continue;
		}

		if (is_tcf_mirred_redirect(a)) {
			int ifindex = tcf_mirred_ifindex(a);
			struct net_device *out_dev;
			struct mlx5e_priv *out_priv;

			out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);

			if (!switchdev_port_same_parent_id(priv->netdev, out_dev)) {
				pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
				       priv->netdev->name, out_dev->name);
				return -EINVAL;
			}

422
			attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
423
			out_priv = netdev_priv(out_dev);
424
			attr->out_rep = out_priv->ppriv;
425
426
427
			continue;
		}

428
429
430
431
432
433
434
435
436
437
438
439
440
		if (is_tcf_vlan(a)) {
			if (tcf_vlan_action(a) == VLAN_F_POP) {
				attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
			} else if (tcf_vlan_action(a) == VLAN_F_PUSH) {
				if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
					return -EOPNOTSUPP;

				attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
				attr->vlan = tcf_vlan_push_vid(a);
			}
			continue;
		}

441
442
443
444
445
		return -EINVAL;
	}
	return 0;
}

446
447
448
int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
			   struct tc_cls_flower_offload *f)
{
449
	struct mlx5e_tc_table *tc = &priv->fs.tc;
450
	int err = 0;
451
452
	bool fdb_flow = false;
	u32 flow_tag, action;
453
	struct mlx5e_tc_flow *flow;
454
	struct mlx5_flow_spec *spec;
Mark Bloch's avatar
Mark Bloch committed
455
	struct mlx5_flow_handle *old = NULL;
456
	struct mlx5_esw_flow_attr *old_attr = NULL;
457
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
458

459
460
461
	if (esw && esw->mode == SRIOV_OFFLOADS)
		fdb_flow = true;

462
463
	flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
				      tc->ht_params);
464
	if (flow) {
465
		old = flow->rule;
466
		old_attr = flow->attr;
467
468
469
470
471
472
473
	} else {
		if (fdb_flow)
			flow = kzalloc(sizeof(*flow) + sizeof(struct mlx5_esw_flow_attr),
				       GFP_KERNEL);
		else
			flow = kzalloc(sizeof(*flow), GFP_KERNEL);
	}
474

475
476
	spec = mlx5_vzalloc(sizeof(*spec));
	if (!spec || !flow) {
477
478
479
480
481
482
		err = -ENOMEM;
		goto err_free;
	}

	flow->cookie = f->cookie;

483
	err = parse_cls_flower(priv, spec, f);
484
485
486
	if (err < 0)
		goto err_free;

487
488
489
	if (fdb_flow) {
		flow->attr  = (struct mlx5_esw_flow_attr *)(flow + 1);
		err = parse_tc_fdb_actions(priv, f->exts, flow->attr);
490
491
		if (err < 0)
			goto err_free;
492
		flow->rule = mlx5e_tc_add_fdb_flow(priv, spec, flow->attr);
493
494
495
496
497
498
	} else {
		err = parse_tc_nic_actions(priv, f->exts, &action, &flow_tag);
		if (err < 0)
			goto err_free;
		flow->rule = mlx5e_tc_add_nic_flow(priv, spec, action, flow_tag);
	}
499
500
501

	if (IS_ERR(flow->rule)) {
		err = PTR_ERR(flow->rule);
502
		goto err_free;
503
504
	}

505
506
507
508
509
	err = rhashtable_insert_fast(&tc->ht, &flow->node,
				     tc->ht_params);
	if (err)
		goto err_del_rule;

510
	if (old)
511
		mlx5e_tc_del_flow(priv, old, old_attr);
512
513
514

	goto out;

515
err_del_rule:
Mark Bloch's avatar
Mark Bloch committed
516
	mlx5_del_flow_rules(flow->rule);
517
518
519
520
521

err_free:
	if (!old)
		kfree(flow);
out:
522
	kvfree(spec);
523
524
525
526
527
528
529
	return err;
}

int mlx5e_delete_flower(struct mlx5e_priv *priv,
			struct tc_cls_flower_offload *f)
{
	struct mlx5e_tc_flow *flow;
530
	struct mlx5e_tc_table *tc = &priv->fs.tc;
531
532
533
534
535
536
537
538

	flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
				      tc->ht_params);
	if (!flow)
		return -EINVAL;

	rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);

539
	mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
540
541
542
543
544
545

	kfree(flow);

	return 0;
}

546
547
548
549
550
551
552
int mlx5e_stats_flower(struct mlx5e_priv *priv,
		       struct tc_cls_flower_offload *f)
{
	struct mlx5e_tc_table *tc = &priv->fs.tc;
	struct mlx5e_tc_flow *flow;
	struct tc_action *a;
	struct mlx5_fc *counter;
553
	LIST_HEAD(actions);
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
	u64 bytes;
	u64 packets;
	u64 lastuse;

	flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
				      tc->ht_params);
	if (!flow)
		return -EINVAL;

	counter = mlx5_flow_rule_counter(flow->rule);
	if (!counter)
		return 0;

	mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);

569
570
	tcf_exts_to_list(f->exts, &actions);
	list_for_each_entry(a, &actions, list)
571
572
573
574
575
		tcf_action_stats_update(a, bytes, packets, lastuse);

	return 0;
}

576
577
578
579
580
581
582
583
584
static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
	.head_offset = offsetof(struct mlx5e_tc_flow, node),
	.key_offset = offsetof(struct mlx5e_tc_flow, cookie),
	.key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
	.automatic_shrinking = true,
};

int mlx5e_tc_init(struct mlx5e_priv *priv)
{
585
	struct mlx5e_tc_table *tc = &priv->fs.tc;
586
587
588
589
590
591
592
593
594
595

	tc->ht_params = mlx5e_tc_flow_ht_params;
	return rhashtable_init(&tc->ht, &tc->ht_params);
}

static void _mlx5e_tc_del_flow(void *ptr, void *arg)
{
	struct mlx5e_tc_flow *flow = ptr;
	struct mlx5e_priv *priv = arg;

596
	mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
597
598
599
600
601
	kfree(flow);
}

void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
{
602
	struct mlx5e_tc_table *tc = &priv->fs.tc;
603
604
605

	rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);

606
607
608
	if (!IS_ERR_OR_NULL(tc->t)) {
		mlx5_destroy_flow_table(tc->t);
		tc->t = NULL;
609
610
	}
}