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
33 #include <linux/hash.h>
34 #include <linux/mlx5/fs.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include "en.h"
38
39 #define ARFS_HASH_SHIFT BITS_PER_BYTE
40 #define ARFS_HASH_SIZE BIT(BITS_PER_BYTE)
41
42 struct arfs_table {
43 struct mlx5e_flow_table ft;
44 struct mlx5_flow_handle *default_rule;
45 struct hlist_head rules_hash[ARFS_HASH_SIZE];
46 };
47
48 enum {
49 MLX5E_ARFS_STATE_ENABLED,
50 };
51
52 enum arfs_type {
53 ARFS_IPV4_TCP,
54 ARFS_IPV6_TCP,
55 ARFS_IPV4_UDP,
56 ARFS_IPV6_UDP,
57 ARFS_NUM_TYPES,
58 };
59
60 struct mlx5e_arfs_tables {
61 struct arfs_table arfs_tables[ARFS_NUM_TYPES];
62 /* Protect aRFS rules list */
63 spinlock_t arfs_lock;
64 int last_filter_id;
65 struct workqueue_struct *wq;
66 unsigned long state;
67 };
68
69 struct arfs_tuple {
70 __be16 etype;
71 u8 ip_proto;
72 union {
73 __be32 src_ipv4;
74 struct in6_addr src_ipv6;
75 };
76 union {
77 __be32 dst_ipv4;
78 struct in6_addr dst_ipv6;
79 };
80 __be16 src_port;
81 __be16 dst_port;
82 };
83
84 struct arfs_rule {
85 struct mlx5e_priv *priv;
86 struct work_struct arfs_work;
87 struct mlx5_flow_handle *rule;
88 struct hlist_node hlist;
89 int rxq;
90 /* Flow ID passed to ndo_rx_flow_steer */
91 int flow_id;
92 /* Filter ID returned by ndo_rx_flow_steer */
93 int filter_id;
94 struct arfs_tuple tuple;
95 };
96
97 #define mlx5e_for_each_arfs_rule(hn, tmp, arfs_tables, i, j) \
98 for (i = 0; i < ARFS_NUM_TYPES; i++) \
99 mlx5e_for_each_hash_arfs_rule(hn, tmp, arfs_tables[i].rules_hash, j)
100
101 #define mlx5e_for_each_hash_arfs_rule(hn, tmp, hash, j) \
102 for (j = 0; j < ARFS_HASH_SIZE; j++) \
103 hlist_for_each_entry_safe(hn, tmp, &hash[j], hlist)
104
arfs_get_tt(enum arfs_type type)105 static enum mlx5_traffic_types arfs_get_tt(enum arfs_type type)
106 {
107 switch (type) {
108 case ARFS_IPV4_TCP:
109 return MLX5_TT_IPV4_TCP;
110 case ARFS_IPV4_UDP:
111 return MLX5_TT_IPV4_UDP;
112 case ARFS_IPV6_TCP:
113 return MLX5_TT_IPV6_TCP;
114 case ARFS_IPV6_UDP:
115 return MLX5_TT_IPV6_UDP;
116 default:
117 return -EINVAL;
118 }
119 }
120
arfs_disable(struct mlx5e_flow_steering * fs)121 static int arfs_disable(struct mlx5e_flow_steering *fs)
122 {
123 struct mlx5_ttc_table *ttc = mlx5e_fs_get_ttc(fs, false);
124 int err, i;
125
126 for (i = 0; i < ARFS_NUM_TYPES; i++) {
127 /* Modify ttc rules destination back to their default */
128 err = mlx5_ttc_fwd_default_dest(ttc, arfs_get_tt(i));
129 if (err) {
130 fs_err(fs,
131 "%s: modify ttc[%d] default destination failed, err(%d)\n",
132 __func__, arfs_get_tt(i), err);
133 return err;
134 }
135 }
136 return 0;
137 }
138
139 static void arfs_del_rules(struct mlx5e_flow_steering *fs);
140
mlx5e_arfs_disable(struct mlx5e_flow_steering * fs)141 int mlx5e_arfs_disable(struct mlx5e_flow_steering *fs)
142 {
143 /* Moving to switchdev mode, fs->arfs is freed by mlx5e_nic_profile
144 * cleanup_rx callback and it is not recreated when
145 * mlx5e_uplink_rep_profile is loaded as mlx5e_create_flow_steering()
146 * is not called by the uplink_rep profile init_rx callback. Thus, if
147 * ntuple is set, moving to switchdev flow will enter this function
148 * with fs->arfs nullified.
149 */
150 if (!mlx5e_fs_get_arfs(fs))
151 return 0;
152
153 arfs_del_rules(fs);
154
155 return arfs_disable(fs);
156 }
157
mlx5e_arfs_enable(struct mlx5e_flow_steering * fs)158 int mlx5e_arfs_enable(struct mlx5e_flow_steering *fs)
159 {
160 struct mlx5_ttc_table *ttc = mlx5e_fs_get_ttc(fs, false);
161 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs);
162 struct mlx5_flow_destination dest = {};
163 int err, i;
164
165 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
166 for (i = 0; i < ARFS_NUM_TYPES; i++) {
167 dest.ft = arfs->arfs_tables[i].ft.t;
168 /* Modify ttc rules destination to point on the aRFS FTs */
169 err = mlx5_ttc_fwd_dest(ttc, arfs_get_tt(i), &dest);
170 if (err) {
171 fs_err(fs, "%s: modify ttc[%d] dest to arfs, failed err(%d)\n",
172 __func__, arfs_get_tt(i), err);
173 arfs_disable(fs);
174 return err;
175 }
176 }
177 set_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state);
178
179 return 0;
180 }
181
arfs_destroy_table(struct arfs_table * arfs_t)182 static void arfs_destroy_table(struct arfs_table *arfs_t)
183 {
184 mlx5_del_flow_rules(arfs_t->default_rule);
185 mlx5e_destroy_flow_table(&arfs_t->ft);
186 }
187
_mlx5e_cleanup_tables(struct mlx5e_flow_steering * fs)188 static void _mlx5e_cleanup_tables(struct mlx5e_flow_steering *fs)
189 {
190 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs);
191 int i;
192
193 arfs_del_rules(fs);
194 destroy_workqueue(arfs->wq);
195 for (i = 0; i < ARFS_NUM_TYPES; i++) {
196 if (!IS_ERR_OR_NULL(arfs->arfs_tables[i].ft.t))
197 arfs_destroy_table(&arfs->arfs_tables[i]);
198 }
199 }
200
mlx5e_arfs_destroy_tables(struct mlx5e_flow_steering * fs,bool ntuple)201 void mlx5e_arfs_destroy_tables(struct mlx5e_flow_steering *fs, bool ntuple)
202 {
203 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs);
204
205 if (!ntuple)
206 return;
207
208 _mlx5e_cleanup_tables(fs);
209 mlx5e_fs_set_arfs(fs, NULL);
210 kvfree(arfs);
211 }
212
arfs_add_default_rule(struct mlx5e_flow_steering * fs,struct mlx5e_rx_res * rx_res,enum arfs_type type)213 static int arfs_add_default_rule(struct mlx5e_flow_steering *fs,
214 struct mlx5e_rx_res *rx_res,
215 enum arfs_type type)
216 {
217 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs);
218 struct arfs_table *arfs_t = &arfs->arfs_tables[type];
219 struct mlx5_flow_destination dest = {};
220 MLX5_DECLARE_FLOW_ACT(flow_act);
221 enum mlx5_traffic_types tt;
222 int err = 0;
223
224 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
225 tt = arfs_get_tt(type);
226 if (tt == -EINVAL) {
227 fs_err(fs, "%s: bad arfs_type: %d\n", __func__, type);
228 return -EINVAL;
229 }
230
231 /* FIXME: Must use mlx5_ttc_get_default_dest(),
232 * but can't since TTC default is not setup yet !
233 */
234 dest.tir_num = mlx5e_rx_res_get_tirn_rss(rx_res, tt);
235 arfs_t->default_rule = mlx5_add_flow_rules(arfs_t->ft.t, NULL,
236 &flow_act,
237 &dest, 1);
238 if (IS_ERR(arfs_t->default_rule)) {
239 err = PTR_ERR(arfs_t->default_rule);
240 arfs_t->default_rule = NULL;
241 fs_err(fs, "%s: add rule failed, arfs type=%d\n", __func__, type);
242 }
243
244 return err;
245 }
246
247 #define MLX5E_ARFS_NUM_GROUPS 2
248 #define MLX5E_ARFS_GROUP1_SIZE (BIT(16) - 1)
249 #define MLX5E_ARFS_GROUP2_SIZE BIT(0)
250 #define MLX5E_ARFS_TABLE_SIZE (MLX5E_ARFS_GROUP1_SIZE +\
251 MLX5E_ARFS_GROUP2_SIZE)
arfs_create_groups(struct mlx5e_flow_table * ft,enum arfs_type type)252 static int arfs_create_groups(struct mlx5e_flow_table *ft,
253 enum arfs_type type)
254 {
255 int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
256 void *outer_headers_c;
257 int ix = 0;
258 u32 *in;
259 int err;
260 u8 *mc;
261
262 ft->g = kcalloc(MLX5E_ARFS_NUM_GROUPS,
263 sizeof(*ft->g), GFP_KERNEL);
264 if (!ft->g)
265 return -ENOMEM;
266
267 in = kvzalloc(inlen, GFP_KERNEL);
268 if (!in) {
269 err = -ENOMEM;
270 goto err_free_g;
271 }
272
273 mc = MLX5_ADDR_OF(create_flow_group_in, in, match_criteria);
274 outer_headers_c = MLX5_ADDR_OF(fte_match_param, mc,
275 outer_headers);
276 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, ethertype);
277 switch (type) {
278 case ARFS_IPV4_TCP:
279 case ARFS_IPV6_TCP:
280 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport);
281 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport);
282 break;
283 case ARFS_IPV4_UDP:
284 case ARFS_IPV6_UDP:
285 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, udp_dport);
286 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, udp_sport);
287 break;
288 default:
289 err = -EINVAL;
290 goto err_free_in;
291 }
292
293 switch (type) {
294 case ARFS_IPV4_TCP:
295 case ARFS_IPV4_UDP:
296 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c,
297 src_ipv4_src_ipv6.ipv4_layout.ipv4);
298 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c,
299 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
300 break;
301 case ARFS_IPV6_TCP:
302 case ARFS_IPV6_UDP:
303 memset(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
304 src_ipv4_src_ipv6.ipv6_layout.ipv6),
305 0xff, 16);
306 memset(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
307 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
308 0xff, 16);
309 break;
310 default:
311 err = -EINVAL;
312 goto err_free_in;
313 }
314
315 MLX5_SET_CFG(in, match_criteria_enable, MLX5_MATCH_OUTER_HEADERS);
316 MLX5_SET_CFG(in, start_flow_index, ix);
317 ix += MLX5E_ARFS_GROUP1_SIZE;
318 MLX5_SET_CFG(in, end_flow_index, ix - 1);
319 ft->g[ft->num_groups] = mlx5_create_flow_group(ft->t, in);
320 if (IS_ERR(ft->g[ft->num_groups]))
321 goto err_clean_group;
322 ft->num_groups++;
323
324 memset(in, 0, inlen);
325 MLX5_SET_CFG(in, start_flow_index, ix);
326 ix += MLX5E_ARFS_GROUP2_SIZE;
327 MLX5_SET_CFG(in, end_flow_index, ix - 1);
328 ft->g[ft->num_groups] = mlx5_create_flow_group(ft->t, in);
329 if (IS_ERR(ft->g[ft->num_groups]))
330 goto err_clean_group;
331 ft->num_groups++;
332
333 kvfree(in);
334 return 0;
335
336 err_clean_group:
337 err = PTR_ERR(ft->g[ft->num_groups]);
338 ft->g[ft->num_groups] = NULL;
339 err_free_in:
340 kvfree(in);
341 err_free_g:
342 kfree(ft->g);
343 ft->g = NULL;
344 return err;
345 }
346
arfs_create_table(struct mlx5e_flow_steering * fs,struct mlx5e_rx_res * rx_res,enum arfs_type type)347 static int arfs_create_table(struct mlx5e_flow_steering *fs,
348 struct mlx5e_rx_res *rx_res,
349 enum arfs_type type)
350 {
351 struct mlx5_flow_namespace *ns = mlx5e_fs_get_ns(fs, false);
352 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs);
353 struct mlx5e_flow_table *ft = &arfs->arfs_tables[type].ft;
354 struct mlx5_flow_table_attr ft_attr = {};
355 int err;
356
357 ft->num_groups = 0;
358
359 ft_attr.max_fte = MLX5E_ARFS_TABLE_SIZE;
360 ft_attr.level = MLX5E_ARFS_FT_LEVEL;
361 ft_attr.prio = MLX5E_NIC_PRIO;
362
363 ft->t = mlx5_create_flow_table(ns, &ft_attr);
364 if (IS_ERR(ft->t)) {
365 err = PTR_ERR(ft->t);
366 ft->t = NULL;
367 return err;
368 }
369
370 err = arfs_create_groups(ft, type);
371 if (err)
372 goto err;
373
374 err = arfs_add_default_rule(fs, rx_res, type);
375 if (err)
376 goto err;
377
378 return 0;
379 err:
380 mlx5e_destroy_flow_table(ft);
381 return err;
382 }
383
mlx5e_arfs_create_tables(struct mlx5e_flow_steering * fs,struct mlx5e_rx_res * rx_res,bool ntuple)384 int mlx5e_arfs_create_tables(struct mlx5e_flow_steering *fs,
385 struct mlx5e_rx_res *rx_res, bool ntuple)
386 {
387 struct mlx5e_arfs_tables *arfs;
388 int err = -ENOMEM;
389 int i;
390
391 if (!ntuple)
392 return 0;
393
394 arfs = kvzalloc(sizeof(*arfs), GFP_KERNEL);
395 if (!arfs)
396 return -ENOMEM;
397
398 spin_lock_init(&arfs->arfs_lock);
399 arfs->wq = create_singlethread_workqueue("mlx5e_arfs");
400 if (!arfs->wq)
401 goto err;
402
403 mlx5e_fs_set_arfs(fs, arfs);
404
405 for (i = 0; i < ARFS_NUM_TYPES; i++) {
406 err = arfs_create_table(fs, rx_res, i);
407 if (err)
408 goto err_des;
409 }
410 return 0;
411
412 err_des:
413 _mlx5e_cleanup_tables(fs);
414 err:
415 mlx5e_fs_set_arfs(fs, NULL);
416 kvfree(arfs);
417 return err;
418 }
419
420 #define MLX5E_ARFS_EXPIRY_QUOTA 60
421
arfs_may_expire_flow(struct mlx5e_priv * priv)422 static void arfs_may_expire_flow(struct mlx5e_priv *priv)
423 {
424 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(priv->fs);
425 struct arfs_rule *arfs_rule;
426 struct hlist_node *htmp;
427 HLIST_HEAD(del_list);
428 int quota = 0;
429 int i;
430 int j;
431
432 spin_lock_bh(&arfs->arfs_lock);
433 mlx5e_for_each_arfs_rule(arfs_rule, htmp, arfs->arfs_tables, i, j) {
434 if (!work_pending(&arfs_rule->arfs_work) &&
435 rps_may_expire_flow(priv->netdev,
436 arfs_rule->rxq, arfs_rule->flow_id,
437 arfs_rule->filter_id)) {
438 hlist_del_init(&arfs_rule->hlist);
439 hlist_add_head(&arfs_rule->hlist, &del_list);
440 if (quota++ > MLX5E_ARFS_EXPIRY_QUOTA)
441 break;
442 }
443 }
444 spin_unlock_bh(&arfs->arfs_lock);
445 hlist_for_each_entry_safe(arfs_rule, htmp, &del_list, hlist) {
446 if (arfs_rule->rule) {
447 mlx5_del_flow_rules(arfs_rule->rule);
448 priv->channel_stats[arfs_rule->rxq]->rq.arfs_expired++;
449 }
450 hlist_del(&arfs_rule->hlist);
451 kfree(arfs_rule);
452 }
453 }
454
arfs_del_rules(struct mlx5e_flow_steering * fs)455 static void arfs_del_rules(struct mlx5e_flow_steering *fs)
456 {
457 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(fs);
458 struct hlist_node *htmp;
459 struct arfs_rule *rule;
460 HLIST_HEAD(del_list);
461 int i;
462 int j;
463
464 clear_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state);
465
466 spin_lock_bh(&arfs->arfs_lock);
467 mlx5e_for_each_arfs_rule(rule, htmp, arfs->arfs_tables, i, j) {
468 hlist_del_init(&rule->hlist);
469 hlist_add_head(&rule->hlist, &del_list);
470 }
471 spin_unlock_bh(&arfs->arfs_lock);
472
473 hlist_for_each_entry_safe(rule, htmp, &del_list, hlist) {
474 cancel_work_sync(&rule->arfs_work);
475 if (rule->rule)
476 mlx5_del_flow_rules(rule->rule);
477 hlist_del(&rule->hlist);
478 kfree(rule);
479 }
480 }
481
482 static struct hlist_head *
arfs_hash_bucket(struct arfs_table * arfs_t,__be16 src_port,__be16 dst_port)483 arfs_hash_bucket(struct arfs_table *arfs_t, __be16 src_port,
484 __be16 dst_port)
485 {
486 unsigned long l;
487 int bucket_idx;
488
489 l = (__force unsigned long)src_port |
490 ((__force unsigned long)dst_port << 2);
491
492 bucket_idx = hash_long(l, ARFS_HASH_SHIFT);
493
494 return &arfs_t->rules_hash[bucket_idx];
495 }
496
arfs_get_table(struct mlx5e_arfs_tables * arfs,u8 ip_proto,__be16 etype)497 static struct arfs_table *arfs_get_table(struct mlx5e_arfs_tables *arfs,
498 u8 ip_proto, __be16 etype)
499 {
500 if (etype == htons(ETH_P_IP) && ip_proto == IPPROTO_TCP)
501 return &arfs->arfs_tables[ARFS_IPV4_TCP];
502 if (etype == htons(ETH_P_IP) && ip_proto == IPPROTO_UDP)
503 return &arfs->arfs_tables[ARFS_IPV4_UDP];
504 if (etype == htons(ETH_P_IPV6) && ip_proto == IPPROTO_TCP)
505 return &arfs->arfs_tables[ARFS_IPV6_TCP];
506 if (etype == htons(ETH_P_IPV6) && ip_proto == IPPROTO_UDP)
507 return &arfs->arfs_tables[ARFS_IPV6_UDP];
508
509 return NULL;
510 }
511
arfs_add_rule(struct mlx5e_priv * priv,struct arfs_rule * arfs_rule)512 static struct mlx5_flow_handle *arfs_add_rule(struct mlx5e_priv *priv,
513 struct arfs_rule *arfs_rule)
514 {
515 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(priv->fs);
516 struct arfs_tuple *tuple = &arfs_rule->tuple;
517 struct mlx5_flow_handle *rule = NULL;
518 struct mlx5_flow_destination dest = {};
519 MLX5_DECLARE_FLOW_ACT(flow_act);
520 struct arfs_table *arfs_table;
521 struct mlx5_flow_spec *spec;
522 struct mlx5_flow_table *ft;
523 int err = 0;
524
525 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
526 if (!spec) {
527 priv->channel_stats[arfs_rule->rxq]->rq.arfs_err++;
528 err = -ENOMEM;
529 goto out;
530 }
531 spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
532 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
533 outer_headers.ethertype);
534 MLX5_SET(fte_match_param, spec->match_value, outer_headers.ethertype,
535 ntohs(tuple->etype));
536 arfs_table = arfs_get_table(arfs, tuple->ip_proto, tuple->etype);
537 if (!arfs_table) {
538 WARN_ONCE(1, "arfs table does not exist for etype %u and ip_proto %u\n",
539 tuple->etype, tuple->ip_proto);
540 err = -EINVAL;
541 goto out;
542 }
543
544 ft = arfs_table->ft.t;
545 if (tuple->ip_proto == IPPROTO_TCP) {
546 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
547 outer_headers.tcp_dport);
548 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
549 outer_headers.tcp_sport);
550 MLX5_SET(fte_match_param, spec->match_value, outer_headers.tcp_dport,
551 ntohs(tuple->dst_port));
552 MLX5_SET(fte_match_param, spec->match_value, outer_headers.tcp_sport,
553 ntohs(tuple->src_port));
554 } else {
555 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
556 outer_headers.udp_dport);
557 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
558 outer_headers.udp_sport);
559 MLX5_SET(fte_match_param, spec->match_value, outer_headers.udp_dport,
560 ntohs(tuple->dst_port));
561 MLX5_SET(fte_match_param, spec->match_value, outer_headers.udp_sport,
562 ntohs(tuple->src_port));
563 }
564 if (tuple->etype == htons(ETH_P_IP)) {
565 memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
566 outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4),
567 &tuple->src_ipv4,
568 4);
569 memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
570 outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
571 &tuple->dst_ipv4,
572 4);
573 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
574 outer_headers.src_ipv4_src_ipv6.ipv4_layout.ipv4);
575 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria,
576 outer_headers.dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
577 } else {
578 memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
579 outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6),
580 &tuple->src_ipv6,
581 16);
582 memcpy(MLX5_ADDR_OF(fte_match_param, spec->match_value,
583 outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
584 &tuple->dst_ipv6,
585 16);
586 memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
587 outer_headers.src_ipv4_src_ipv6.ipv6_layout.ipv6),
588 0xff,
589 16);
590 memset(MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
591 outer_headers.dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
592 0xff,
593 16);
594 }
595 dest.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
596 dest.tir_num = mlx5e_rx_res_get_tirn_direct(priv->rx_res, arfs_rule->rxq);
597 rule = mlx5_add_flow_rules(ft, spec, &flow_act, &dest, 1);
598 if (IS_ERR(rule)) {
599 err = PTR_ERR(rule);
600 priv->channel_stats[arfs_rule->rxq]->rq.arfs_err++;
601 netdev_dbg(priv->netdev,
602 "%s: add rule(filter id=%d, rq idx=%d, ip proto=0x%x) failed,err=%d\n",
603 __func__, arfs_rule->filter_id, arfs_rule->rxq,
604 tuple->ip_proto, err);
605 }
606
607 out:
608 kvfree(spec);
609 return err ? ERR_PTR(err) : rule;
610 }
611
arfs_modify_rule_rq(struct mlx5e_priv * priv,struct mlx5_flow_handle * rule,u16 rxq)612 static void arfs_modify_rule_rq(struct mlx5e_priv *priv,
613 struct mlx5_flow_handle *rule, u16 rxq)
614 {
615 struct mlx5_flow_destination dst = {};
616 int err = 0;
617
618 dst.type = MLX5_FLOW_DESTINATION_TYPE_TIR;
619 dst.tir_num = mlx5e_rx_res_get_tirn_direct(priv->rx_res, rxq);
620 err = mlx5_modify_rule_destination(rule, &dst, NULL);
621 if (err) {
622 priv->channel_stats[rxq]->rq.arfs_err++;
623 netdev_warn(priv->netdev,
624 "Failed to modify aRFS rule destination to rq=%d\n", rxq);
625 }
626 }
627
arfs_handle_work(struct work_struct * work)628 static void arfs_handle_work(struct work_struct *work)
629 {
630 struct arfs_rule *arfs_rule = container_of(work,
631 struct arfs_rule,
632 arfs_work);
633 struct mlx5e_priv *priv = arfs_rule->priv;
634 struct mlx5e_arfs_tables *arfs;
635 struct mlx5_flow_handle *rule;
636
637 arfs = mlx5e_fs_get_arfs(priv->fs);
638 if (!test_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state))
639 return;
640
641 if (!arfs_rule->rule) {
642 rule = arfs_add_rule(priv, arfs_rule);
643 if (IS_ERR(rule))
644 goto out;
645 arfs_rule->rule = rule;
646 priv->channel_stats[arfs_rule->rxq]->rq.arfs_add++;
647 } else {
648 arfs_modify_rule_rq(priv, arfs_rule->rule,
649 arfs_rule->rxq);
650 }
651 out:
652 arfs_may_expire_flow(priv);
653 }
654
arfs_alloc_rule(struct mlx5e_priv * priv,struct arfs_table * arfs_t,const struct flow_keys * fk,u16 rxq,u32 flow_id)655 static struct arfs_rule *arfs_alloc_rule(struct mlx5e_priv *priv,
656 struct arfs_table *arfs_t,
657 const struct flow_keys *fk,
658 u16 rxq, u32 flow_id)
659 {
660 struct mlx5e_arfs_tables *arfs = mlx5e_fs_get_arfs(priv->fs);
661 struct arfs_rule *rule;
662 struct arfs_tuple *tuple;
663
664 rule = kzalloc(sizeof(*rule), GFP_ATOMIC);
665 if (!rule) {
666 priv->channel_stats[rxq]->rq.arfs_err++;
667 return NULL;
668 }
669
670 rule->priv = priv;
671 rule->rxq = rxq;
672 INIT_WORK(&rule->arfs_work, arfs_handle_work);
673
674 tuple = &rule->tuple;
675 tuple->etype = fk->basic.n_proto;
676 tuple->ip_proto = fk->basic.ip_proto;
677 if (tuple->etype == htons(ETH_P_IP)) {
678 tuple->src_ipv4 = fk->addrs.v4addrs.src;
679 tuple->dst_ipv4 = fk->addrs.v4addrs.dst;
680 } else {
681 memcpy(&tuple->src_ipv6, &fk->addrs.v6addrs.src,
682 sizeof(struct in6_addr));
683 memcpy(&tuple->dst_ipv6, &fk->addrs.v6addrs.dst,
684 sizeof(struct in6_addr));
685 }
686 tuple->src_port = fk->ports.src;
687 tuple->dst_port = fk->ports.dst;
688
689 rule->flow_id = flow_id;
690 rule->filter_id = arfs->last_filter_id++ % RPS_NO_FILTER;
691
692 hlist_add_head(&rule->hlist,
693 arfs_hash_bucket(arfs_t, tuple->src_port,
694 tuple->dst_port));
695 return rule;
696 }
697
arfs_cmp(const struct arfs_tuple * tuple,const struct flow_keys * fk)698 static bool arfs_cmp(const struct arfs_tuple *tuple, const struct flow_keys *fk)
699 {
700 if (tuple->src_port != fk->ports.src || tuple->dst_port != fk->ports.dst)
701 return false;
702 if (tuple->etype != fk->basic.n_proto)
703 return false;
704 if (tuple->etype == htons(ETH_P_IP))
705 return tuple->src_ipv4 == fk->addrs.v4addrs.src &&
706 tuple->dst_ipv4 == fk->addrs.v4addrs.dst;
707 if (tuple->etype == htons(ETH_P_IPV6))
708 return !memcmp(&tuple->src_ipv6, &fk->addrs.v6addrs.src,
709 sizeof(struct in6_addr)) &&
710 !memcmp(&tuple->dst_ipv6, &fk->addrs.v6addrs.dst,
711 sizeof(struct in6_addr));
712 return false;
713 }
714
arfs_find_rule(struct arfs_table * arfs_t,const struct flow_keys * fk)715 static struct arfs_rule *arfs_find_rule(struct arfs_table *arfs_t,
716 const struct flow_keys *fk)
717 {
718 struct arfs_rule *arfs_rule;
719 struct hlist_head *head;
720
721 head = arfs_hash_bucket(arfs_t, fk->ports.src, fk->ports.dst);
722 hlist_for_each_entry(arfs_rule, head, hlist) {
723 if (arfs_cmp(&arfs_rule->tuple, fk))
724 return arfs_rule;
725 }
726
727 return NULL;
728 }
729
mlx5e_rx_flow_steer(struct net_device * dev,const struct sk_buff * skb,u16 rxq_index,u32 flow_id)730 int mlx5e_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
731 u16 rxq_index, u32 flow_id)
732 {
733 struct mlx5e_priv *priv = netdev_priv(dev);
734 struct mlx5e_arfs_tables *arfs;
735 struct arfs_rule *arfs_rule;
736 struct arfs_table *arfs_t;
737 struct flow_keys fk;
738
739 arfs = mlx5e_fs_get_arfs(priv->fs);
740 if (!skb_flow_dissect_flow_keys(skb, &fk, 0))
741 return -EPROTONOSUPPORT;
742
743 if (fk.basic.n_proto != htons(ETH_P_IP) &&
744 fk.basic.n_proto != htons(ETH_P_IPV6))
745 return -EPROTONOSUPPORT;
746
747 if (skb->encapsulation)
748 return -EPROTONOSUPPORT;
749
750 arfs_t = arfs_get_table(arfs, fk.basic.ip_proto, fk.basic.n_proto);
751 if (!arfs_t)
752 return -EPROTONOSUPPORT;
753
754 spin_lock_bh(&arfs->arfs_lock);
755 if (!test_bit(MLX5E_ARFS_STATE_ENABLED, &arfs->state)) {
756 spin_unlock_bh(&arfs->arfs_lock);
757 return -EPERM;
758 }
759
760 arfs_rule = arfs_find_rule(arfs_t, &fk);
761 if (arfs_rule) {
762 if (arfs_rule->rxq == rxq_index || work_busy(&arfs_rule->arfs_work)) {
763 spin_unlock_bh(&arfs->arfs_lock);
764 return arfs_rule->filter_id;
765 }
766
767 priv->channel_stats[rxq_index]->rq.arfs_request_in++;
768 priv->channel_stats[arfs_rule->rxq]->rq.arfs_request_out++;
769 arfs_rule->rxq = rxq_index;
770 } else {
771 arfs_rule = arfs_alloc_rule(priv, arfs_t, &fk, rxq_index, flow_id);
772 if (!arfs_rule) {
773 spin_unlock_bh(&arfs->arfs_lock);
774 return -ENOMEM;
775 }
776 }
777 queue_work(arfs->wq, &arfs_rule->arfs_work);
778 spin_unlock_bh(&arfs->arfs_lock);
779 return arfs_rule->filter_id;
780 }
781
782