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