net/mlx5e: Add TC vlan action for SRIOV offloads
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_tc.c
CommitLineData
e8f887ac
AV
1/*
2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 */
32
e3a2b7ed
AV
33#include <net/flow_dissector.h>
34#include <net/pkt_cls.h>
35#include <net/tc_act/tc_gact.h>
12185a9f 36#include <net/tc_act/tc_skbedit.h>
e8f887ac
AV
37#include <linux/mlx5/fs.h>
38#include <linux/mlx5/device.h>
39#include <linux/rhashtable.h>
03a9d11e
OG
40#include <net/switchdev.h>
41#include <net/tc_act/tc_mirred.h>
776b12b6 42#include <net/tc_act/tc_vlan.h>
e8f887ac
AV
43#include "en.h"
44#include "en_tc.h"
03a9d11e 45#include "eswitch.h"
e8f887ac
AV
46
47struct mlx5e_tc_flow {
48 struct rhash_head node;
49 u64 cookie;
50 struct mlx5_flow_rule *rule;
776b12b6 51 struct mlx5_esw_flow_attr *attr;
e8f887ac
AV
52};
53
acff797c
MG
54#define MLX5E_TC_TABLE_NUM_ENTRIES 1024
55#define MLX5E_TC_TABLE_NUM_GROUPS 4
e8f887ac 56
5c40348c
OG
57static struct mlx5_flow_rule *mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
58 struct mlx5_flow_spec *spec,
59 u32 action, u32 flow_tag)
e8f887ac 60{
aad7e08d
AV
61 struct mlx5_core_dev *dev = priv->mdev;
62 struct mlx5_flow_destination dest = { 0 };
63 struct mlx5_fc *counter = NULL;
e8f887ac
AV
64 struct mlx5_flow_rule *rule;
65 bool table_created = false;
66
aad7e08d
AV
67 if (action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
68 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
69 dest.ft = priv->fs.vlan.ft.t;
55130287 70 } else if (action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
aad7e08d
AV
71 counter = mlx5_fc_create(dev, true);
72 if (IS_ERR(counter))
73 return ERR_CAST(counter);
74
75 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
76 dest.counter = counter;
77 }
78
acff797c
MG
79 if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
80 priv->fs.tc.t =
81 mlx5_create_auto_grouped_flow_table(priv->fs.ns,
82 MLX5E_TC_PRIO,
83 MLX5E_TC_TABLE_NUM_ENTRIES,
84 MLX5E_TC_TABLE_NUM_GROUPS,
d63cd286 85 0);
acff797c 86 if (IS_ERR(priv->fs.tc.t)) {
e8f887ac
AV
87 netdev_err(priv->netdev,
88 "Failed to create tc offload table\n");
aad7e08d
AV
89 rule = ERR_CAST(priv->fs.tc.t);
90 goto err_create_ft;
e8f887ac
AV
91 }
92
93 table_created = true;
94 }
95
c5bb1730
MG
96 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
97 rule = mlx5_add_flow_rule(priv->fs.tc.t, spec,
e8f887ac 98 action, flow_tag,
aad7e08d
AV
99 &dest);
100
101 if (IS_ERR(rule))
102 goto err_add_rule;
103
104 return rule;
e8f887ac 105
aad7e08d
AV
106err_add_rule:
107 if (table_created) {
acff797c
MG
108 mlx5_destroy_flow_table(priv->fs.tc.t);
109 priv->fs.tc.t = NULL;
e8f887ac 110 }
aad7e08d
AV
111err_create_ft:
112 mlx5_fc_destroy(dev, counter);
e8f887ac
AV
113
114 return rule;
115}
116
adb4c123
OG
117static struct mlx5_flow_rule *mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
118 struct mlx5_flow_spec *spec,
776b12b6 119 struct mlx5_esw_flow_attr *attr)
adb4c123
OG
120{
121 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
8b32580d
OG
122 int err;
123
124 err = mlx5_eswitch_add_vlan_action(esw, attr);
125 if (err)
126 return ERR_PTR(err);
adb4c123 127
776b12b6 128 return mlx5_eswitch_add_offloaded_rule(esw, spec, attr);
adb4c123
OG
129}
130
e8f887ac 131static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
8b32580d
OG
132 struct mlx5_flow_rule *rule,
133 struct mlx5_esw_flow_attr *attr)
e8f887ac 134{
8b32580d 135 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
aad7e08d
AV
136 struct mlx5_fc *counter = NULL;
137
138 counter = mlx5_flow_rule_counter(rule);
139
8b32580d
OG
140 if (esw && esw->mode == SRIOV_OFFLOADS)
141 mlx5_eswitch_del_vlan_action(esw, attr);
142
e8f887ac
AV
143 mlx5_del_flow_rule(rule);
144
aad7e08d
AV
145 mlx5_fc_destroy(priv->mdev, counter);
146
5c40348c 147 if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
acff797c
MG
148 mlx5_destroy_flow_table(priv->fs.tc.t);
149 priv->fs.tc.t = NULL;
e8f887ac
AV
150 }
151}
152
c5bb1730 153static int parse_cls_flower(struct mlx5e_priv *priv, struct mlx5_flow_spec *spec,
e3a2b7ed
AV
154 struct tc_cls_flower_offload *f)
155{
c5bb1730
MG
156 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
157 outer_headers);
158 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
159 outer_headers);
e3a2b7ed
AV
160 u16 addr_type = 0;
161 u8 ip_proto = 0;
162
163 if (f->dissector->used_keys &
164 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
165 BIT(FLOW_DISSECTOR_KEY_BASIC) |
166 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
167 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
168 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
169 BIT(FLOW_DISSECTOR_KEY_PORTS))) {
170 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
171 f->dissector->used_keys);
172 return -EOPNOTSUPP;
173 }
174
175 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
176 struct flow_dissector_key_control *key =
177 skb_flow_dissector_target(f->dissector,
1dbd0d37 178 FLOW_DISSECTOR_KEY_CONTROL,
e3a2b7ed
AV
179 f->key);
180 addr_type = key->addr_type;
181 }
182
183 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
184 struct flow_dissector_key_basic *key =
185 skb_flow_dissector_target(f->dissector,
186 FLOW_DISSECTOR_KEY_BASIC,
187 f->key);
188 struct flow_dissector_key_basic *mask =
189 skb_flow_dissector_target(f->dissector,
190 FLOW_DISSECTOR_KEY_BASIC,
191 f->mask);
192 ip_proto = key->ip_proto;
193
194 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
195 ntohs(mask->n_proto));
196 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
197 ntohs(key->n_proto));
198
199 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
200 mask->ip_proto);
201 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
202 key->ip_proto);
203 }
204
205 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
206 struct flow_dissector_key_eth_addrs *key =
207 skb_flow_dissector_target(f->dissector,
208 FLOW_DISSECTOR_KEY_ETH_ADDRS,
209 f->key);
210 struct flow_dissector_key_eth_addrs *mask =
211 skb_flow_dissector_target(f->dissector,
212 FLOW_DISSECTOR_KEY_ETH_ADDRS,
213 f->mask);
214
215 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
216 dmac_47_16),
217 mask->dst);
218 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
219 dmac_47_16),
220 key->dst);
221
222 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
223 smac_47_16),
224 mask->src);
225 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
226 smac_47_16),
227 key->src);
228 }
229
230 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
231 struct flow_dissector_key_ipv4_addrs *key =
232 skb_flow_dissector_target(f->dissector,
233 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
234 f->key);
235 struct flow_dissector_key_ipv4_addrs *mask =
236 skb_flow_dissector_target(f->dissector,
237 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
238 f->mask);
239
240 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
241 src_ipv4_src_ipv6.ipv4_layout.ipv4),
242 &mask->src, sizeof(mask->src));
243 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
244 src_ipv4_src_ipv6.ipv4_layout.ipv4),
245 &key->src, sizeof(key->src));
246 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
247 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
248 &mask->dst, sizeof(mask->dst));
249 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
250 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
251 &key->dst, sizeof(key->dst));
252 }
253
254 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
255 struct flow_dissector_key_ipv6_addrs *key =
256 skb_flow_dissector_target(f->dissector,
257 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
258 f->key);
259 struct flow_dissector_key_ipv6_addrs *mask =
260 skb_flow_dissector_target(f->dissector,
261 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
262 f->mask);
263
264 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
265 src_ipv4_src_ipv6.ipv6_layout.ipv6),
266 &mask->src, sizeof(mask->src));
267 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
268 src_ipv4_src_ipv6.ipv6_layout.ipv6),
269 &key->src, sizeof(key->src));
270
271 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
272 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
273 &mask->dst, sizeof(mask->dst));
274 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
275 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
276 &key->dst, sizeof(key->dst));
277 }
278
279 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
280 struct flow_dissector_key_ports *key =
281 skb_flow_dissector_target(f->dissector,
282 FLOW_DISSECTOR_KEY_PORTS,
283 f->key);
284 struct flow_dissector_key_ports *mask =
285 skb_flow_dissector_target(f->dissector,
286 FLOW_DISSECTOR_KEY_PORTS,
287 f->mask);
288 switch (ip_proto) {
289 case IPPROTO_TCP:
290 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
291 tcp_sport, ntohs(mask->src));
292 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
293 tcp_sport, ntohs(key->src));
294
295 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
296 tcp_dport, ntohs(mask->dst));
297 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
298 tcp_dport, ntohs(key->dst));
299 break;
300
301 case IPPROTO_UDP:
302 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
303 udp_sport, ntohs(mask->src));
304 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
305 udp_sport, ntohs(key->src));
306
307 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
308 udp_dport, ntohs(mask->dst));
309 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
310 udp_dport, ntohs(key->dst));
311 break;
312 default:
313 netdev_err(priv->netdev,
314 "Only UDP and TCP transport are supported\n");
315 return -EINVAL;
316 }
317 }
318
319 return 0;
320}
321
5c40348c
OG
322static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
323 u32 *action, u32 *flow_tag)
e3a2b7ed
AV
324{
325 const struct tc_action *a;
22dc13c8 326 LIST_HEAD(actions);
e3a2b7ed
AV
327
328 if (tc_no_actions(exts))
329 return -EINVAL;
330
331 *flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
332 *action = 0;
333
22dc13c8
WC
334 tcf_exts_to_list(exts, &actions);
335 list_for_each_entry(a, &actions, list) {
e3a2b7ed
AV
336 /* Only support a single action per rule */
337 if (*action)
338 return -EINVAL;
339
340 if (is_tcf_gact_shot(a)) {
341 *action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
aad7e08d
AV
342 if (MLX5_CAP_FLOWTABLE(priv->mdev,
343 flow_table_properties_nic_receive.flow_counter))
344 *action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
e3a2b7ed
AV
345 continue;
346 }
347
348 if (is_tcf_skbedit_mark(a)) {
349 u32 mark = tcf_skbedit_mark(a);
350
351 if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
352 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
353 mark);
354 return -EINVAL;
355 }
356
357 *flow_tag = mark;
358 *action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
359 continue;
360 }
361
362 return -EINVAL;
363 }
364
365 return 0;
366}
367
03a9d11e 368static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
776b12b6 369 struct mlx5_esw_flow_attr *attr)
03a9d11e
OG
370{
371 const struct tc_action *a;
22dc13c8 372 LIST_HEAD(actions);
03a9d11e
OG
373
374 if (tc_no_actions(exts))
375 return -EINVAL;
376
776b12b6
OG
377 memset(attr, 0, sizeof(*attr));
378 attr->in_rep = priv->ppriv;
03a9d11e 379
22dc13c8
WC
380 tcf_exts_to_list(exts, &actions);
381 list_for_each_entry(a, &actions, list) {
03a9d11e 382 if (is_tcf_gact_shot(a)) {
8b32580d
OG
383 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
384 MLX5_FLOW_CONTEXT_ACTION_COUNT;
03a9d11e
OG
385 continue;
386 }
387
388 if (is_tcf_mirred_redirect(a)) {
389 int ifindex = tcf_mirred_ifindex(a);
390 struct net_device *out_dev;
391 struct mlx5e_priv *out_priv;
03a9d11e
OG
392
393 out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
394
395 if (!switchdev_port_same_parent_id(priv->netdev, out_dev)) {
396 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
397 priv->netdev->name, out_dev->name);
398 return -EINVAL;
399 }
400
8b32580d 401 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
03a9d11e 402 out_priv = netdev_priv(out_dev);
776b12b6 403 attr->out_rep = out_priv->ppriv;
03a9d11e
OG
404 continue;
405 }
406
8b32580d
OG
407 if (is_tcf_vlan(a)) {
408 if (tcf_vlan_action(a) == VLAN_F_POP) {
409 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
410 } else if (tcf_vlan_action(a) == VLAN_F_PUSH) {
411 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
412 return -EOPNOTSUPP;
413
414 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
415 attr->vlan = tcf_vlan_push_vid(a);
416 }
417 continue;
418 }
419
03a9d11e
OG
420 return -EINVAL;
421 }
422 return 0;
423}
424
e3a2b7ed
AV
425int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
426 struct tc_cls_flower_offload *f)
427{
acff797c 428 struct mlx5e_tc_table *tc = &priv->fs.tc;
e3a2b7ed 429 int err = 0;
776b12b6
OG
430 bool fdb_flow = false;
431 u32 flow_tag, action;
e3a2b7ed 432 struct mlx5e_tc_flow *flow;
c5bb1730 433 struct mlx5_flow_spec *spec;
e3a2b7ed 434 struct mlx5_flow_rule *old = NULL;
8b32580d 435 struct mlx5_esw_flow_attr *old_attr;
adb4c123 436 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
e3a2b7ed 437
776b12b6
OG
438 if (esw && esw->mode == SRIOV_OFFLOADS)
439 fdb_flow = true;
440
e3a2b7ed
AV
441 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
442 tc->ht_params);
776b12b6 443 if (flow) {
e3a2b7ed 444 old = flow->rule;
8b32580d 445 old_attr = flow->attr;
776b12b6
OG
446 } else {
447 if (fdb_flow)
448 flow = kzalloc(sizeof(*flow) + sizeof(struct mlx5_esw_flow_attr),
449 GFP_KERNEL);
450 else
451 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
452 }
e3a2b7ed 453
c5bb1730
MG
454 spec = mlx5_vzalloc(sizeof(*spec));
455 if (!spec || !flow) {
e3a2b7ed
AV
456 err = -ENOMEM;
457 goto err_free;
458 }
459
460 flow->cookie = f->cookie;
461
c5bb1730 462 err = parse_cls_flower(priv, spec, f);
e3a2b7ed
AV
463 if (err < 0)
464 goto err_free;
465
776b12b6
OG
466 if (fdb_flow) {
467 flow->attr = (struct mlx5_esw_flow_attr *)(flow + 1);
468 err = parse_tc_fdb_actions(priv, f->exts, flow->attr);
adb4c123
OG
469 if (err < 0)
470 goto err_free;
776b12b6 471 flow->rule = mlx5e_tc_add_fdb_flow(priv, spec, flow->attr);
adb4c123
OG
472 } else {
473 err = parse_tc_nic_actions(priv, f->exts, &action, &flow_tag);
474 if (err < 0)
475 goto err_free;
476 flow->rule = mlx5e_tc_add_nic_flow(priv, spec, action, flow_tag);
477 }
e3a2b7ed 478
e3a2b7ed
AV
479 if (IS_ERR(flow->rule)) {
480 err = PTR_ERR(flow->rule);
5c40348c 481 goto err_free;
e3a2b7ed
AV
482 }
483
5c40348c
OG
484 err = rhashtable_insert_fast(&tc->ht, &flow->node,
485 tc->ht_params);
486 if (err)
487 goto err_del_rule;
488
e3a2b7ed 489 if (old)
8b32580d 490 mlx5e_tc_del_flow(priv, old, old_attr);
e3a2b7ed
AV
491
492 goto out;
493
5c40348c
OG
494err_del_rule:
495 mlx5_del_flow_rule(flow->rule);
e3a2b7ed
AV
496
497err_free:
498 if (!old)
499 kfree(flow);
500out:
c5bb1730 501 kvfree(spec);
e3a2b7ed
AV
502 return err;
503}
504
505int mlx5e_delete_flower(struct mlx5e_priv *priv,
506 struct tc_cls_flower_offload *f)
507{
508 struct mlx5e_tc_flow *flow;
acff797c 509 struct mlx5e_tc_table *tc = &priv->fs.tc;
e3a2b7ed
AV
510
511 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
512 tc->ht_params);
513 if (!flow)
514 return -EINVAL;
515
516 rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
517
8b32580d 518 mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
e3a2b7ed
AV
519
520 kfree(flow);
521
522 return 0;
523}
524
aad7e08d
AV
525int mlx5e_stats_flower(struct mlx5e_priv *priv,
526 struct tc_cls_flower_offload *f)
527{
528 struct mlx5e_tc_table *tc = &priv->fs.tc;
529 struct mlx5e_tc_flow *flow;
530 struct tc_action *a;
531 struct mlx5_fc *counter;
22dc13c8 532 LIST_HEAD(actions);
aad7e08d
AV
533 u64 bytes;
534 u64 packets;
535 u64 lastuse;
536
537 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
538 tc->ht_params);
539 if (!flow)
540 return -EINVAL;
541
542 counter = mlx5_flow_rule_counter(flow->rule);
543 if (!counter)
544 return 0;
545
546 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
547
22dc13c8
WC
548 tcf_exts_to_list(f->exts, &actions);
549 list_for_each_entry(a, &actions, list)
aad7e08d
AV
550 tcf_action_stats_update(a, bytes, packets, lastuse);
551
552 return 0;
553}
554
e8f887ac
AV
555static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
556 .head_offset = offsetof(struct mlx5e_tc_flow, node),
557 .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
558 .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
559 .automatic_shrinking = true,
560};
561
562int mlx5e_tc_init(struct mlx5e_priv *priv)
563{
acff797c 564 struct mlx5e_tc_table *tc = &priv->fs.tc;
e8f887ac
AV
565
566 tc->ht_params = mlx5e_tc_flow_ht_params;
567 return rhashtable_init(&tc->ht, &tc->ht_params);
568}
569
570static void _mlx5e_tc_del_flow(void *ptr, void *arg)
571{
572 struct mlx5e_tc_flow *flow = ptr;
573 struct mlx5e_priv *priv = arg;
574
8b32580d 575 mlx5e_tc_del_flow(priv, flow->rule, flow->attr);
e8f887ac
AV
576 kfree(flow);
577}
578
579void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
580{
acff797c 581 struct mlx5e_tc_table *tc = &priv->fs.tc;
e8f887ac
AV
582
583 rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
584
acff797c
MG
585 if (!IS_ERR_OR_NULL(tc->t)) {
586 mlx5_destroy_flow_table(tc->t);
587 tc->t = NULL;
e8f887ac
AV
588 }
589}