Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_tc.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 #include <net/flow_dissector.h>
34 #include <net/sch_generic.h>
35 #include <net/pkt_cls.h>
36 #include <net/tc_act/tc_gact.h>
37 #include <net/tc_act/tc_skbedit.h>
38 #include <linux/mlx5/fs.h>
39 #include <linux/mlx5/device.h>
40 #include <linux/rhashtable.h>
41 #include <net/switchdev.h>
42 #include <net/tc_act/tc_mirred.h>
43 #include <net/tc_act/tc_vlan.h>
44 #include <net/tc_act/tc_tunnel_key.h>
45 #include <net/tc_act/tc_pedit.h>
46 #include <net/tc_act/tc_csum.h>
47 #include <net/vxlan.h>
48 #include <net/arp.h>
49 #include "en.h"
50 #include "en_rep.h"
51 #include "en_tc.h"
52 #include "eswitch.h"
53 #include "vxlan.h"
54
55 struct mlx5_nic_flow_attr {
56         u32 action;
57         u32 flow_tag;
58         u32 mod_hdr_id;
59 };
60
61 enum {
62         MLX5E_TC_FLOW_ESWITCH   = BIT(0),
63         MLX5E_TC_FLOW_NIC       = BIT(1),
64         MLX5E_TC_FLOW_OFFLOADED = BIT(2),
65 };
66
67 struct mlx5e_tc_flow {
68         struct rhash_head       node;
69         u64                     cookie;
70         u8                      flags;
71         struct mlx5_flow_handle *rule;
72         struct list_head        encap;   /* flows sharing the same encap ID */
73         struct list_head        mod_hdr; /* flows sharing the same mod hdr ID */
74         union {
75                 struct mlx5_esw_flow_attr esw_attr[0];
76                 struct mlx5_nic_flow_attr nic_attr[0];
77         };
78 };
79
80 struct mlx5e_tc_flow_parse_attr {
81         struct mlx5_flow_spec spec;
82         int num_mod_hdr_actions;
83         void *mod_hdr_actions;
84 };
85
86 enum {
87         MLX5_HEADER_TYPE_VXLAN = 0x0,
88         MLX5_HEADER_TYPE_NVGRE = 0x1,
89 };
90
91 #define MLX5E_TC_TABLE_NUM_ENTRIES 1024
92 #define MLX5E_TC_TABLE_NUM_GROUPS 4
93
94 struct mod_hdr_key {
95         int num_actions;
96         void *actions;
97 };
98
99 struct mlx5e_mod_hdr_entry {
100         /* a node of a hash table which keeps all the mod_hdr entries */
101         struct hlist_node mod_hdr_hlist;
102
103         /* flows sharing the same mod_hdr entry */
104         struct list_head flows;
105
106         struct mod_hdr_key key;
107
108         u32 mod_hdr_id;
109 };
110
111 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)
112
113 static inline u32 hash_mod_hdr_info(struct mod_hdr_key *key)
114 {
115         return jhash(key->actions,
116                      key->num_actions * MLX5_MH_ACT_SZ, 0);
117 }
118
119 static inline int cmp_mod_hdr_info(struct mod_hdr_key *a,
120                                    struct mod_hdr_key *b)
121 {
122         if (a->num_actions != b->num_actions)
123                 return 1;
124
125         return memcmp(a->actions, b->actions, a->num_actions * MLX5_MH_ACT_SZ);
126 }
127
128 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
129                                 struct mlx5e_tc_flow *flow,
130                                 struct mlx5e_tc_flow_parse_attr *parse_attr)
131 {
132         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
133         int num_actions, actions_size, namespace, err;
134         struct mlx5e_mod_hdr_entry *mh;
135         struct mod_hdr_key key;
136         bool found = false;
137         u32 hash_key;
138
139         num_actions  = parse_attr->num_mod_hdr_actions;
140         actions_size = MLX5_MH_ACT_SZ * num_actions;
141
142         key.actions = parse_attr->mod_hdr_actions;
143         key.num_actions = num_actions;
144
145         hash_key = hash_mod_hdr_info(&key);
146
147         if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
148                 namespace = MLX5_FLOW_NAMESPACE_FDB;
149                 hash_for_each_possible(esw->offloads.mod_hdr_tbl, mh,
150                                        mod_hdr_hlist, hash_key) {
151                         if (!cmp_mod_hdr_info(&mh->key, &key)) {
152                                 found = true;
153                                 break;
154                         }
155                 }
156         } else {
157                 namespace = MLX5_FLOW_NAMESPACE_KERNEL;
158                 hash_for_each_possible(priv->fs.tc.mod_hdr_tbl, mh,
159                                        mod_hdr_hlist, hash_key) {
160                         if (!cmp_mod_hdr_info(&mh->key, &key)) {
161                                 found = true;
162                                 break;
163                         }
164                 }
165         }
166
167         if (found)
168                 goto attach_flow;
169
170         mh = kzalloc(sizeof(*mh) + actions_size, GFP_KERNEL);
171         if (!mh)
172                 return -ENOMEM;
173
174         mh->key.actions = (void *)mh + sizeof(*mh);
175         memcpy(mh->key.actions, key.actions, actions_size);
176         mh->key.num_actions = num_actions;
177         INIT_LIST_HEAD(&mh->flows);
178
179         err = mlx5_modify_header_alloc(priv->mdev, namespace,
180                                        mh->key.num_actions,
181                                        mh->key.actions,
182                                        &mh->mod_hdr_id);
183         if (err)
184                 goto out_err;
185
186         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
187                 hash_add(esw->offloads.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
188         else
189                 hash_add(priv->fs.tc.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
190
191 attach_flow:
192         list_add(&flow->mod_hdr, &mh->flows);
193         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
194                 flow->esw_attr->mod_hdr_id = mh->mod_hdr_id;
195         else
196                 flow->nic_attr->mod_hdr_id = mh->mod_hdr_id;
197
198         return 0;
199
200 out_err:
201         kfree(mh);
202         return err;
203 }
204
205 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
206                                  struct mlx5e_tc_flow *flow)
207 {
208         struct list_head *next = flow->mod_hdr.next;
209
210         list_del(&flow->mod_hdr);
211
212         if (list_empty(next)) {
213                 struct mlx5e_mod_hdr_entry *mh;
214
215                 mh = list_entry(next, struct mlx5e_mod_hdr_entry, flows);
216
217                 mlx5_modify_header_dealloc(priv->mdev, mh->mod_hdr_id);
218                 hash_del(&mh->mod_hdr_hlist);
219                 kfree(mh);
220         }
221 }
222
223 static struct mlx5_flow_handle *
224 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
225                       struct mlx5e_tc_flow_parse_attr *parse_attr,
226                       struct mlx5e_tc_flow *flow)
227 {
228         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
229         struct mlx5_core_dev *dev = priv->mdev;
230         struct mlx5_flow_destination dest = {};
231         struct mlx5_flow_act flow_act = {
232                 .action = attr->action,
233                 .flow_tag = attr->flow_tag,
234                 .encap_id = 0,
235         };
236         struct mlx5_fc *counter = NULL;
237         struct mlx5_flow_handle *rule;
238         bool table_created = false;
239         int err;
240
241         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
242                 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
243                 dest.ft = priv->fs.vlan.ft.t;
244         } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
245                 counter = mlx5_fc_create(dev, true);
246                 if (IS_ERR(counter))
247                         return ERR_CAST(counter);
248
249                 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
250                 dest.counter = counter;
251         }
252
253         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
254                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
255                 flow_act.modify_id = attr->mod_hdr_id;
256                 kfree(parse_attr->mod_hdr_actions);
257                 if (err) {
258                         rule = ERR_PTR(err);
259                         goto err_create_mod_hdr_id;
260                 }
261         }
262
263         if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
264                 priv->fs.tc.t =
265                         mlx5_create_auto_grouped_flow_table(priv->fs.ns,
266                                                             MLX5E_TC_PRIO,
267                                                             MLX5E_TC_TABLE_NUM_ENTRIES,
268                                                             MLX5E_TC_TABLE_NUM_GROUPS,
269                                                             0, 0);
270                 if (IS_ERR(priv->fs.tc.t)) {
271                         netdev_err(priv->netdev,
272                                    "Failed to create tc offload table\n");
273                         rule = ERR_CAST(priv->fs.tc.t);
274                         goto err_create_ft;
275                 }
276
277                 table_created = true;
278         }
279
280         parse_attr->spec.match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
281         rule = mlx5_add_flow_rules(priv->fs.tc.t, &parse_attr->spec,
282                                    &flow_act, &dest, 1);
283
284         if (IS_ERR(rule))
285                 goto err_add_rule;
286
287         return rule;
288
289 err_add_rule:
290         if (table_created) {
291                 mlx5_destroy_flow_table(priv->fs.tc.t);
292                 priv->fs.tc.t = NULL;
293         }
294 err_create_ft:
295         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
296                 mlx5e_detach_mod_hdr(priv, flow);
297 err_create_mod_hdr_id:
298         mlx5_fc_destroy(dev, counter);
299
300         return rule;
301 }
302
303 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
304                                   struct mlx5e_tc_flow *flow)
305 {
306         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
307         struct mlx5_fc *counter = NULL;
308
309         counter = mlx5_flow_rule_counter(flow->rule);
310         mlx5_del_flow_rules(flow->rule);
311         mlx5_fc_destroy(priv->mdev, counter);
312
313         if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
314                 mlx5_destroy_flow_table(priv->fs.tc.t);
315                 priv->fs.tc.t = NULL;
316         }
317
318         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
319                 mlx5e_detach_mod_hdr(priv, flow);
320 }
321
322 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
323                                struct mlx5e_tc_flow *flow);
324
325 static struct mlx5_flow_handle *
326 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
327                       struct mlx5e_tc_flow_parse_attr *parse_attr,
328                       struct mlx5e_tc_flow *flow)
329 {
330         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
331         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
332         struct mlx5_flow_handle *rule;
333         int err;
334
335         err = mlx5_eswitch_add_vlan_action(esw, attr);
336         if (err) {
337                 rule = ERR_PTR(err);
338                 goto err_add_vlan;
339         }
340
341         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
342                 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
343                 kfree(parse_attr->mod_hdr_actions);
344                 if (err) {
345                         rule = ERR_PTR(err);
346                         goto err_mod_hdr;
347                 }
348         }
349
350         rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
351         if (IS_ERR(rule))
352                 goto err_add_rule;
353
354         return rule;
355
356 err_add_rule:
357         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
358                 mlx5e_detach_mod_hdr(priv, flow);
359 err_mod_hdr:
360         mlx5_eswitch_del_vlan_action(esw, attr);
361 err_add_vlan:
362         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
363                 mlx5e_detach_encap(priv, flow);
364         return rule;
365 }
366
367 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
368                                   struct mlx5e_tc_flow *flow)
369 {
370         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
371         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
372
373         if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
374                 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
375                 mlx5_eswitch_del_offloaded_rule(esw, flow->rule, attr);
376         }
377
378         mlx5_eswitch_del_vlan_action(esw, attr);
379
380         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
381                 mlx5e_detach_encap(priv, flow);
382                 kvfree(attr->parse_attr);
383         }
384
385         if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
386                 mlx5e_detach_mod_hdr(priv, flow);
387 }
388
389 void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
390                               struct mlx5e_encap_entry *e)
391 {
392         struct mlx5e_tc_flow *flow;
393         int err;
394
395         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
396                                e->encap_size, e->encap_header,
397                                &e->encap_id);
398         if (err) {
399                 mlx5_core_warn(priv->mdev, "Failed to offload cached encapsulation header, %d\n",
400                                err);
401                 return;
402         }
403         e->flags |= MLX5_ENCAP_ENTRY_VALID;
404         mlx5e_rep_queue_neigh_stats_work(priv);
405
406         list_for_each_entry(flow, &e->flows, encap) {
407                 flow->esw_attr->encap_id = e->encap_id;
408                 flow->rule = mlx5e_tc_add_fdb_flow(priv,
409                                                    flow->esw_attr->parse_attr,
410                                                    flow);
411                 if (IS_ERR(flow->rule)) {
412                         err = PTR_ERR(flow->rule);
413                         mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
414                                        err);
415                         continue;
416                 }
417                 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
418         }
419 }
420
421 void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
422                               struct mlx5e_encap_entry *e)
423 {
424         struct mlx5e_tc_flow *flow;
425         struct mlx5_fc *counter;
426
427         list_for_each_entry(flow, &e->flows, encap) {
428                 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
429                         flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
430                         counter = mlx5_flow_rule_counter(flow->rule);
431                         mlx5_del_flow_rules(flow->rule);
432                         mlx5_fc_destroy(priv->mdev, counter);
433                 }
434         }
435
436         if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
437                 e->flags &= ~MLX5_ENCAP_ENTRY_VALID;
438                 mlx5_encap_dealloc(priv->mdev, e->encap_id);
439         }
440 }
441
442 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
443 {
444         struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
445         u64 bytes, packets, lastuse = 0;
446         struct mlx5e_tc_flow *flow;
447         struct mlx5e_encap_entry *e;
448         struct mlx5_fc *counter;
449         struct neigh_table *tbl;
450         bool neigh_used = false;
451         struct neighbour *n;
452
453         if (m_neigh->family == AF_INET)
454                 tbl = &arp_tbl;
455 #if IS_ENABLED(CONFIG_IPV6)
456         else if (m_neigh->family == AF_INET6)
457                 tbl = ipv6_stub->nd_tbl;
458 #endif
459         else
460                 return;
461
462         list_for_each_entry(e, &nhe->encap_list, encap_list) {
463                 if (!(e->flags & MLX5_ENCAP_ENTRY_VALID))
464                         continue;
465                 list_for_each_entry(flow, &e->flows, encap) {
466                         if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
467                                 counter = mlx5_flow_rule_counter(flow->rule);
468                                 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
469                                 if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
470                                         neigh_used = true;
471                                         break;
472                                 }
473                         }
474                 }
475         }
476
477         if (neigh_used) {
478                 nhe->reported_lastuse = jiffies;
479
480                 /* find the relevant neigh according to the cached device and
481                  * dst ip pair
482                  */
483                 n = neigh_lookup(tbl, &m_neigh->dst_ip, m_neigh->dev);
484                 if (!n) {
485                         WARN(1, "The neighbour already freed\n");
486                         return;
487                 }
488
489                 neigh_event_send(n, NULL);
490                 neigh_release(n);
491         }
492 }
493
494 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
495                                struct mlx5e_tc_flow *flow)
496 {
497         struct list_head *next = flow->encap.next;
498
499         list_del(&flow->encap);
500         if (list_empty(next)) {
501                 struct mlx5e_encap_entry *e;
502
503                 e = list_entry(next, struct mlx5e_encap_entry, flows);
504                 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
505
506                 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
507                         mlx5_encap_dealloc(priv->mdev, e->encap_id);
508
509                 hash_del_rcu(&e->encap_hlist);
510                 kfree(e->encap_header);
511                 kfree(e);
512         }
513 }
514
515 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
516                               struct mlx5e_tc_flow *flow)
517 {
518         if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
519                 mlx5e_tc_del_fdb_flow(priv, flow);
520         else
521                 mlx5e_tc_del_nic_flow(priv, flow);
522 }
523
524 static void parse_vxlan_attr(struct mlx5_flow_spec *spec,
525                              struct tc_cls_flower_offload *f)
526 {
527         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
528                                        outer_headers);
529         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
530                                        outer_headers);
531         void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
532                                     misc_parameters);
533         void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
534                                     misc_parameters);
535
536         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
537         MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
538
539         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
540                 struct flow_dissector_key_keyid *key =
541                         skb_flow_dissector_target(f->dissector,
542                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
543                                                   f->key);
544                 struct flow_dissector_key_keyid *mask =
545                         skb_flow_dissector_target(f->dissector,
546                                                   FLOW_DISSECTOR_KEY_ENC_KEYID,
547                                                   f->mask);
548                 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
549                          be32_to_cpu(mask->keyid));
550                 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
551                          be32_to_cpu(key->keyid));
552         }
553 }
554
555 static int parse_tunnel_attr(struct mlx5e_priv *priv,
556                              struct mlx5_flow_spec *spec,
557                              struct tc_cls_flower_offload *f)
558 {
559         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
560                                        outer_headers);
561         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
562                                        outer_headers);
563
564         struct flow_dissector_key_control *enc_control =
565                 skb_flow_dissector_target(f->dissector,
566                                           FLOW_DISSECTOR_KEY_ENC_CONTROL,
567                                           f->key);
568
569         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
570                 struct flow_dissector_key_ports *key =
571                         skb_flow_dissector_target(f->dissector,
572                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
573                                                   f->key);
574                 struct flow_dissector_key_ports *mask =
575                         skb_flow_dissector_target(f->dissector,
576                                                   FLOW_DISSECTOR_KEY_ENC_PORTS,
577                                                   f->mask);
578                 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
579                 struct net_device *up_dev = mlx5_eswitch_get_uplink_netdev(esw);
580                 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
581
582                 /* Full udp dst port must be given */
583                 if (memchr_inv(&mask->dst, 0xff, sizeof(mask->dst)))
584                         goto vxlan_match_offload_err;
585
586                 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->dst)) &&
587                     MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
588                         parse_vxlan_attr(spec, f);
589                 else {
590                         netdev_warn(priv->netdev,
591                                     "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->dst));
592                         return -EOPNOTSUPP;
593                 }
594
595                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
596                          udp_dport, ntohs(mask->dst));
597                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
598                          udp_dport, ntohs(key->dst));
599
600                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
601                          udp_sport, ntohs(mask->src));
602                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
603                          udp_sport, ntohs(key->src));
604         } else { /* udp dst port must be given */
605 vxlan_match_offload_err:
606                 netdev_warn(priv->netdev,
607                             "IP tunnel decap offload supported only for vxlan, must set UDP dport\n");
608                 return -EOPNOTSUPP;
609         }
610
611         if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
612                 struct flow_dissector_key_ipv4_addrs *key =
613                         skb_flow_dissector_target(f->dissector,
614                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
615                                                   f->key);
616                 struct flow_dissector_key_ipv4_addrs *mask =
617                         skb_flow_dissector_target(f->dissector,
618                                                   FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
619                                                   f->mask);
620                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
621                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
622                          ntohl(mask->src));
623                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
624                          src_ipv4_src_ipv6.ipv4_layout.ipv4,
625                          ntohl(key->src));
626
627                 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
628                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
629                          ntohl(mask->dst));
630                 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
631                          dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
632                          ntohl(key->dst));
633
634                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
635                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
636         } else if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
637                 struct flow_dissector_key_ipv6_addrs *key =
638                         skb_flow_dissector_target(f->dissector,
639                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
640                                                   f->key);
641                 struct flow_dissector_key_ipv6_addrs *mask =
642                         skb_flow_dissector_target(f->dissector,
643                                                   FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
644                                                   f->mask);
645
646                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
647                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
648                        &mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
649                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
650                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
651                        &key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
652
653                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
654                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
655                        &mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
656                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
657                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
658                        &key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
659
660                 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
661                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
662         }
663
664         /* Enforce DMAC when offloading incoming tunneled flows.
665          * Flow counters require a match on the DMAC.
666          */
667         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
668         MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
669         ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
670                                      dmac_47_16), priv->netdev->dev_addr);
671
672         /* let software handle IP fragments */
673         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
674         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
675
676         return 0;
677 }
678
679 static int __parse_cls_flower(struct mlx5e_priv *priv,
680                               struct mlx5_flow_spec *spec,
681                               struct tc_cls_flower_offload *f,
682                               u8 *min_inline)
683 {
684         void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
685                                        outer_headers);
686         void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
687                                        outer_headers);
688         u16 addr_type = 0;
689         u8 ip_proto = 0;
690
691         *min_inline = MLX5_INLINE_MODE_L2;
692
693         if (f->dissector->used_keys &
694             ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
695               BIT(FLOW_DISSECTOR_KEY_BASIC) |
696               BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
697               BIT(FLOW_DISSECTOR_KEY_VLAN) |
698               BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
699               BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
700               BIT(FLOW_DISSECTOR_KEY_PORTS) |
701               BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
702               BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
703               BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
704               BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
705               BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
706               BIT(FLOW_DISSECTOR_KEY_TCP) |
707               BIT(FLOW_DISSECTOR_KEY_IP))) {
708                 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
709                             f->dissector->used_keys);
710                 return -EOPNOTSUPP;
711         }
712
713         if ((dissector_uses_key(f->dissector,
714                                 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) ||
715              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID) ||
716              dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) &&
717             dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
718                 struct flow_dissector_key_control *key =
719                         skb_flow_dissector_target(f->dissector,
720                                                   FLOW_DISSECTOR_KEY_ENC_CONTROL,
721                                                   f->key);
722                 switch (key->addr_type) {
723                 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
724                 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
725                         if (parse_tunnel_attr(priv, spec, f))
726                                 return -EOPNOTSUPP;
727                         break;
728                 default:
729                         return -EOPNOTSUPP;
730                 }
731
732                 /* In decap flow, header pointers should point to the inner
733                  * headers, outer header were already set by parse_tunnel_attr
734                  */
735                 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
736                                          inner_headers);
737                 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
738                                          inner_headers);
739         }
740
741         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
742                 struct flow_dissector_key_control *key =
743                         skb_flow_dissector_target(f->dissector,
744                                                   FLOW_DISSECTOR_KEY_CONTROL,
745                                                   f->key);
746
747                 struct flow_dissector_key_control *mask =
748                         skb_flow_dissector_target(f->dissector,
749                                                   FLOW_DISSECTOR_KEY_CONTROL,
750                                                   f->mask);
751                 addr_type = key->addr_type;
752
753                 if (mask->flags & FLOW_DIS_IS_FRAGMENT) {
754                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
755                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
756                                  key->flags & FLOW_DIS_IS_FRAGMENT);
757
758                         /* the HW doesn't need L3 inline to match on frag=no */
759                         if (key->flags & FLOW_DIS_IS_FRAGMENT)
760                                 *min_inline = MLX5_INLINE_MODE_IP;
761                 }
762         }
763
764         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
765                 struct flow_dissector_key_basic *key =
766                         skb_flow_dissector_target(f->dissector,
767                                                   FLOW_DISSECTOR_KEY_BASIC,
768                                                   f->key);
769                 struct flow_dissector_key_basic *mask =
770                         skb_flow_dissector_target(f->dissector,
771                                                   FLOW_DISSECTOR_KEY_BASIC,
772                                                   f->mask);
773                 ip_proto = key->ip_proto;
774
775                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
776                          ntohs(mask->n_proto));
777                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
778                          ntohs(key->n_proto));
779
780                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
781                          mask->ip_proto);
782                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
783                          key->ip_proto);
784
785                 if (mask->ip_proto)
786                         *min_inline = MLX5_INLINE_MODE_IP;
787         }
788
789         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
790                 struct flow_dissector_key_eth_addrs *key =
791                         skb_flow_dissector_target(f->dissector,
792                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
793                                                   f->key);
794                 struct flow_dissector_key_eth_addrs *mask =
795                         skb_flow_dissector_target(f->dissector,
796                                                   FLOW_DISSECTOR_KEY_ETH_ADDRS,
797                                                   f->mask);
798
799                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
800                                              dmac_47_16),
801                                 mask->dst);
802                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
803                                              dmac_47_16),
804                                 key->dst);
805
806                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
807                                              smac_47_16),
808                                 mask->src);
809                 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
810                                              smac_47_16),
811                                 key->src);
812         }
813
814         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
815                 struct flow_dissector_key_vlan *key =
816                         skb_flow_dissector_target(f->dissector,
817                                                   FLOW_DISSECTOR_KEY_VLAN,
818                                                   f->key);
819                 struct flow_dissector_key_vlan *mask =
820                         skb_flow_dissector_target(f->dissector,
821                                                   FLOW_DISSECTOR_KEY_VLAN,
822                                                   f->mask);
823                 if (mask->vlan_id || mask->vlan_priority) {
824                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
825                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
826
827                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
828                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
829
830                         MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
831                         MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
832                 }
833         }
834
835         if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
836                 struct flow_dissector_key_ipv4_addrs *key =
837                         skb_flow_dissector_target(f->dissector,
838                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
839                                                   f->key);
840                 struct flow_dissector_key_ipv4_addrs *mask =
841                         skb_flow_dissector_target(f->dissector,
842                                                   FLOW_DISSECTOR_KEY_IPV4_ADDRS,
843                                                   f->mask);
844
845                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
846                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
847                        &mask->src, sizeof(mask->src));
848                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
849                                     src_ipv4_src_ipv6.ipv4_layout.ipv4),
850                        &key->src, sizeof(key->src));
851                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
852                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
853                        &mask->dst, sizeof(mask->dst));
854                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
855                                     dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
856                        &key->dst, sizeof(key->dst));
857
858                 if (mask->src || mask->dst)
859                         *min_inline = MLX5_INLINE_MODE_IP;
860         }
861
862         if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
863                 struct flow_dissector_key_ipv6_addrs *key =
864                         skb_flow_dissector_target(f->dissector,
865                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
866                                                   f->key);
867                 struct flow_dissector_key_ipv6_addrs *mask =
868                         skb_flow_dissector_target(f->dissector,
869                                                   FLOW_DISSECTOR_KEY_IPV6_ADDRS,
870                                                   f->mask);
871
872                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
873                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
874                        &mask->src, sizeof(mask->src));
875                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
876                                     src_ipv4_src_ipv6.ipv6_layout.ipv6),
877                        &key->src, sizeof(key->src));
878
879                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
880                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
881                        &mask->dst, sizeof(mask->dst));
882                 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
883                                     dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
884                        &key->dst, sizeof(key->dst));
885
886                 if (ipv6_addr_type(&mask->src) != IPV6_ADDR_ANY ||
887                     ipv6_addr_type(&mask->dst) != IPV6_ADDR_ANY)
888                         *min_inline = MLX5_INLINE_MODE_IP;
889         }
890
891         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
892                 struct flow_dissector_key_ports *key =
893                         skb_flow_dissector_target(f->dissector,
894                                                   FLOW_DISSECTOR_KEY_PORTS,
895                                                   f->key);
896                 struct flow_dissector_key_ports *mask =
897                         skb_flow_dissector_target(f->dissector,
898                                                   FLOW_DISSECTOR_KEY_PORTS,
899                                                   f->mask);
900                 switch (ip_proto) {
901                 case IPPROTO_TCP:
902                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
903                                  tcp_sport, ntohs(mask->src));
904                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
905                                  tcp_sport, ntohs(key->src));
906
907                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
908                                  tcp_dport, ntohs(mask->dst));
909                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
910                                  tcp_dport, ntohs(key->dst));
911                         break;
912
913                 case IPPROTO_UDP:
914                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
915                                  udp_sport, ntohs(mask->src));
916                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
917                                  udp_sport, ntohs(key->src));
918
919                         MLX5_SET(fte_match_set_lyr_2_4, headers_c,
920                                  udp_dport, ntohs(mask->dst));
921                         MLX5_SET(fte_match_set_lyr_2_4, headers_v,
922                                  udp_dport, ntohs(key->dst));
923                         break;
924                 default:
925                         netdev_err(priv->netdev,
926                                    "Only UDP and TCP transport are supported\n");
927                         return -EINVAL;
928                 }
929
930                 if (mask->src || mask->dst)
931                         *min_inline = MLX5_INLINE_MODE_TCP_UDP;
932         }
933
934         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_IP)) {
935                 struct flow_dissector_key_ip *key =
936                         skb_flow_dissector_target(f->dissector,
937                                                   FLOW_DISSECTOR_KEY_IP,
938                                                   f->key);
939                 struct flow_dissector_key_ip *mask =
940                         skb_flow_dissector_target(f->dissector,
941                                                   FLOW_DISSECTOR_KEY_IP,
942                                                   f->mask);
943
944                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn, mask->tos & 0x3);
945                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, key->tos & 0x3);
946
947                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp, mask->tos >> 2);
948                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, key->tos  >> 2);
949
950                 if (mask->tos)
951                         *min_inline = MLX5_INLINE_MODE_IP;
952
953                 if (mask->ttl) /* currently not supported */
954                         return -EOPNOTSUPP;
955         }
956
957         if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_TCP)) {
958                 struct flow_dissector_key_tcp *key =
959                         skb_flow_dissector_target(f->dissector,
960                                                   FLOW_DISSECTOR_KEY_TCP,
961                                                   f->key);
962                 struct flow_dissector_key_tcp *mask =
963                         skb_flow_dissector_target(f->dissector,
964                                                   FLOW_DISSECTOR_KEY_TCP,
965                                                   f->mask);
966
967                 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
968                          ntohs(mask->flags));
969                 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
970                          ntohs(key->flags));
971
972                 if (mask->flags)
973                         *min_inline = MLX5_INLINE_MODE_TCP_UDP;
974         }
975
976         return 0;
977 }
978
979 static int parse_cls_flower(struct mlx5e_priv *priv,
980                             struct mlx5e_tc_flow *flow,
981                             struct mlx5_flow_spec *spec,
982                             struct tc_cls_flower_offload *f)
983 {
984         struct mlx5_core_dev *dev = priv->mdev;
985         struct mlx5_eswitch *esw = dev->priv.eswitch;
986         struct mlx5e_rep_priv *rpriv = priv->ppriv;
987         struct mlx5_eswitch_rep *rep;
988         u8 min_inline;
989         int err;
990
991         err = __parse_cls_flower(priv, spec, f, &min_inline);
992
993         if (!err && (flow->flags & MLX5E_TC_FLOW_ESWITCH)) {
994                 rep = rpriv->rep;
995                 if (rep->vport != FDB_UPLINK_VPORT &&
996                     (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
997                     esw->offloads.inline_mode < min_inline)) {
998                         netdev_warn(priv->netdev,
999                                     "Flow is not offloaded due to min inline setting, required %d actual %d\n",
1000                                     min_inline, esw->offloads.inline_mode);
1001                         return -EOPNOTSUPP;
1002                 }
1003         }
1004
1005         return err;
1006 }
1007
1008 struct pedit_headers {
1009         struct ethhdr  eth;
1010         struct iphdr   ip4;
1011         struct ipv6hdr ip6;
1012         struct tcphdr  tcp;
1013         struct udphdr  udp;
1014 };
1015
1016 static int pedit_header_offsets[] = {
1017         [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
1018         [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
1019         [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
1020         [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
1021         [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
1022 };
1023
1024 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
1025
1026 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
1027                          struct pedit_headers *masks,
1028                          struct pedit_headers *vals)
1029 {
1030         u32 *curr_pmask, *curr_pval;
1031
1032         if (hdr_type >= __PEDIT_HDR_TYPE_MAX)
1033                 goto out_err;
1034
1035         curr_pmask = (u32 *)(pedit_header(masks, hdr_type) + offset);
1036         curr_pval  = (u32 *)(pedit_header(vals, hdr_type) + offset);
1037
1038         if (*curr_pmask & mask)  /* disallow acting twice on the same location */
1039                 goto out_err;
1040
1041         *curr_pmask |= mask;
1042         *curr_pval  |= (val & mask);
1043
1044         return 0;
1045
1046 out_err:
1047         return -EOPNOTSUPP;
1048 }
1049
1050 struct mlx5_fields {
1051         u8  field;
1052         u8  size;
1053         u32 offset;
1054 };
1055
1056 static struct mlx5_fields fields[] = {
1057         {MLX5_ACTION_IN_FIELD_OUT_DMAC_47_16, 4, offsetof(struct pedit_headers, eth.h_dest[0])},
1058         {MLX5_ACTION_IN_FIELD_OUT_DMAC_15_0,  2, offsetof(struct pedit_headers, eth.h_dest[4])},
1059         {MLX5_ACTION_IN_FIELD_OUT_SMAC_47_16, 4, offsetof(struct pedit_headers, eth.h_source[0])},
1060         {MLX5_ACTION_IN_FIELD_OUT_SMAC_15_0,  2, offsetof(struct pedit_headers, eth.h_source[4])},
1061         {MLX5_ACTION_IN_FIELD_OUT_ETHERTYPE,  2, offsetof(struct pedit_headers, eth.h_proto)},
1062
1063         {MLX5_ACTION_IN_FIELD_OUT_IP_DSCP, 1, offsetof(struct pedit_headers, ip4.tos)},
1064         {MLX5_ACTION_IN_FIELD_OUT_IP_TTL,  1, offsetof(struct pedit_headers, ip4.ttl)},
1065         {MLX5_ACTION_IN_FIELD_OUT_SIPV4,   4, offsetof(struct pedit_headers, ip4.saddr)},
1066         {MLX5_ACTION_IN_FIELD_OUT_DIPV4,   4, offsetof(struct pedit_headers, ip4.daddr)},
1067
1068         {MLX5_ACTION_IN_FIELD_OUT_SIPV6_127_96, 4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[0])},
1069         {MLX5_ACTION_IN_FIELD_OUT_SIPV6_95_64,  4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[1])},
1070         {MLX5_ACTION_IN_FIELD_OUT_SIPV6_63_32,  4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[2])},
1071         {MLX5_ACTION_IN_FIELD_OUT_SIPV6_31_0,   4, offsetof(struct pedit_headers, ip6.saddr.s6_addr32[3])},
1072         {MLX5_ACTION_IN_FIELD_OUT_DIPV6_127_96, 4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[0])},
1073         {MLX5_ACTION_IN_FIELD_OUT_DIPV6_95_64,  4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[1])},
1074         {MLX5_ACTION_IN_FIELD_OUT_DIPV6_63_32,  4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[2])},
1075         {MLX5_ACTION_IN_FIELD_OUT_DIPV6_31_0,   4, offsetof(struct pedit_headers, ip6.daddr.s6_addr32[3])},
1076
1077         {MLX5_ACTION_IN_FIELD_OUT_TCP_SPORT, 2, offsetof(struct pedit_headers, tcp.source)},
1078         {MLX5_ACTION_IN_FIELD_OUT_TCP_DPORT, 2, offsetof(struct pedit_headers, tcp.dest)},
1079         {MLX5_ACTION_IN_FIELD_OUT_TCP_FLAGS, 1, offsetof(struct pedit_headers, tcp.ack_seq) + 5},
1080
1081         {MLX5_ACTION_IN_FIELD_OUT_UDP_SPORT, 2, offsetof(struct pedit_headers, udp.source)},
1082         {MLX5_ACTION_IN_FIELD_OUT_UDP_DPORT, 2, offsetof(struct pedit_headers, udp.dest)},
1083 };
1084
1085 /* On input attr->num_mod_hdr_actions tells how many HW actions can be parsed at
1086  * max from the SW pedit action. On success, it says how many HW actions were
1087  * actually parsed.
1088  */
1089 static int offload_pedit_fields(struct pedit_headers *masks,
1090                                 struct pedit_headers *vals,
1091                                 struct mlx5e_tc_flow_parse_attr *parse_attr)
1092 {
1093         struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
1094         int i, action_size, nactions, max_actions, first, last, next_z;
1095         void *s_masks_p, *a_masks_p, *vals_p;
1096         struct mlx5_fields *f;
1097         u8 cmd, field_bsize;
1098         u32 s_mask, a_mask;
1099         unsigned long mask;
1100         __be32 mask_be32;
1101         __be16 mask_be16;
1102         void *action;
1103
1104         set_masks = &masks[TCA_PEDIT_KEY_EX_CMD_SET];
1105         add_masks = &masks[TCA_PEDIT_KEY_EX_CMD_ADD];
1106         set_vals = &vals[TCA_PEDIT_KEY_EX_CMD_SET];
1107         add_vals = &vals[TCA_PEDIT_KEY_EX_CMD_ADD];
1108
1109         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1110         action = parse_attr->mod_hdr_actions;
1111         max_actions = parse_attr->num_mod_hdr_actions;
1112         nactions = 0;
1113
1114         for (i = 0; i < ARRAY_SIZE(fields); i++) {
1115                 f = &fields[i];
1116                 /* avoid seeing bits set from previous iterations */
1117                 s_mask = 0;
1118                 a_mask = 0;
1119
1120                 s_masks_p = (void *)set_masks + f->offset;
1121                 a_masks_p = (void *)add_masks + f->offset;
1122
1123                 memcpy(&s_mask, s_masks_p, f->size);
1124                 memcpy(&a_mask, a_masks_p, f->size);
1125
1126                 if (!s_mask && !a_mask) /* nothing to offload here */
1127                         continue;
1128
1129                 if (s_mask && a_mask) {
1130                         printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
1131                         return -EOPNOTSUPP;
1132                 }
1133
1134                 if (nactions == max_actions) {
1135                         printk(KERN_WARNING "mlx5: parsed %d pedit actions, can't do more\n", nactions);
1136                         return -EOPNOTSUPP;
1137                 }
1138
1139                 if (s_mask) {
1140                         cmd  = MLX5_ACTION_TYPE_SET;
1141                         mask = s_mask;
1142                         vals_p = (void *)set_vals + f->offset;
1143                         /* clear to denote we consumed this field */
1144                         memset(s_masks_p, 0, f->size);
1145                 } else {
1146                         cmd  = MLX5_ACTION_TYPE_ADD;
1147                         mask = a_mask;
1148                         vals_p = (void *)add_vals + f->offset;
1149                         /* clear to denote we consumed this field */
1150                         memset(a_masks_p, 0, f->size);
1151                 }
1152
1153                 field_bsize = f->size * BITS_PER_BYTE;
1154
1155                 if (field_bsize == 32) {
1156                         mask_be32 = *(__be32 *)&mask;
1157                         mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
1158                 } else if (field_bsize == 16) {
1159                         mask_be16 = *(__be16 *)&mask;
1160                         mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
1161                 }
1162
1163                 first = find_first_bit(&mask, field_bsize);
1164                 next_z = find_next_zero_bit(&mask, field_bsize, first);
1165                 last  = find_last_bit(&mask, field_bsize);
1166                 if (first < next_z && next_z < last) {
1167                         printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
1168                                mask);
1169                         return -EOPNOTSUPP;
1170                 }
1171
1172                 MLX5_SET(set_action_in, action, action_type, cmd);
1173                 MLX5_SET(set_action_in, action, field, f->field);
1174
1175                 if (cmd == MLX5_ACTION_TYPE_SET) {
1176                         MLX5_SET(set_action_in, action, offset, first);
1177                         /* length is num of bits to be written, zero means length of 32 */
1178                         MLX5_SET(set_action_in, action, length, (last - first + 1));
1179                 }
1180
1181                 if (field_bsize == 32)
1182                         MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
1183                 else if (field_bsize == 16)
1184                         MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
1185                 else if (field_bsize == 8)
1186                         MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
1187
1188                 action += action_size;
1189                 nactions++;
1190         }
1191
1192         parse_attr->num_mod_hdr_actions = nactions;
1193         return 0;
1194 }
1195
1196 static int alloc_mod_hdr_actions(struct mlx5e_priv *priv,
1197                                  const struct tc_action *a, int namespace,
1198                                  struct mlx5e_tc_flow_parse_attr *parse_attr)
1199 {
1200         int nkeys, action_size, max_actions;
1201
1202         nkeys = tcf_pedit_nkeys(a);
1203         action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1204
1205         if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
1206                 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, max_modify_header_actions);
1207         else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
1208                 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, max_modify_header_actions);
1209
1210         /* can get up to crazingly 16 HW actions in 32 bits pedit SW key */
1211         max_actions = min(max_actions, nkeys * 16);
1212
1213         parse_attr->mod_hdr_actions = kcalloc(max_actions, action_size, GFP_KERNEL);
1214         if (!parse_attr->mod_hdr_actions)
1215                 return -ENOMEM;
1216
1217         parse_attr->num_mod_hdr_actions = max_actions;
1218         return 0;
1219 }
1220
1221 static const struct pedit_headers zero_masks = {};
1222
1223 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
1224                                  const struct tc_action *a, int namespace,
1225                                  struct mlx5e_tc_flow_parse_attr *parse_attr)
1226 {
1227         struct pedit_headers masks[__PEDIT_CMD_MAX], vals[__PEDIT_CMD_MAX], *cmd_masks;
1228         int nkeys, i, err = -EOPNOTSUPP;
1229         u32 mask, val, offset;
1230         u8 cmd, htype;
1231
1232         nkeys = tcf_pedit_nkeys(a);
1233
1234         memset(masks, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1235         memset(vals,  0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1236
1237         for (i = 0; i < nkeys; i++) {
1238                 htype = tcf_pedit_htype(a, i);
1239                 cmd = tcf_pedit_cmd(a, i);
1240                 err = -EOPNOTSUPP; /* can't be all optimistic */
1241
1242                 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
1243                         printk(KERN_WARNING "mlx5: legacy pedit isn't offloaded\n");
1244                         goto out_err;
1245                 }
1246
1247                 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET && cmd != TCA_PEDIT_KEY_EX_CMD_ADD) {
1248                         printk(KERN_WARNING "mlx5: pedit cmd %d isn't offloaded\n", cmd);
1249                         goto out_err;
1250                 }
1251
1252                 mask = tcf_pedit_mask(a, i);
1253                 val = tcf_pedit_val(a, i);
1254                 offset = tcf_pedit_offset(a, i);
1255
1256                 err = set_pedit_val(htype, ~mask, val, offset, &masks[cmd], &vals[cmd]);
1257                 if (err)
1258                         goto out_err;
1259         }
1260
1261         err = alloc_mod_hdr_actions(priv, a, namespace, parse_attr);
1262         if (err)
1263                 goto out_err;
1264
1265         err = offload_pedit_fields(masks, vals, parse_attr);
1266         if (err < 0)
1267                 goto out_dealloc_parsed_actions;
1268
1269         for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
1270                 cmd_masks = &masks[cmd];
1271                 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
1272                         printk(KERN_WARNING "mlx5: attempt to offload an unsupported field (cmd %d)\n",
1273                                cmd);
1274                         print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
1275                                        16, 1, cmd_masks, sizeof(zero_masks), true);
1276                         err = -EOPNOTSUPP;
1277                         goto out_dealloc_parsed_actions;
1278                 }
1279         }
1280
1281         return 0;
1282
1283 out_dealloc_parsed_actions:
1284         kfree(parse_attr->mod_hdr_actions);
1285 out_err:
1286         return err;
1287 }
1288
1289 static bool csum_offload_supported(struct mlx5e_priv *priv, u32 action, u32 update_flags)
1290 {
1291         u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
1292                          TCA_CSUM_UPDATE_FLAG_UDP;
1293
1294         /*  The HW recalcs checksums only if re-writing headers */
1295         if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
1296                 netdev_warn(priv->netdev,
1297                             "TC csum action is only offloaded with pedit\n");
1298                 return false;
1299         }
1300
1301         if (update_flags & ~prot_flags) {
1302                 netdev_warn(priv->netdev,
1303                             "can't offload TC csum action for some header/s - flags %#x\n",
1304                             update_flags);
1305                 return false;
1306         }
1307
1308         return true;
1309 }
1310
1311 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1312                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
1313                                 struct mlx5e_tc_flow *flow)
1314 {
1315         struct mlx5_nic_flow_attr *attr = flow->nic_attr;
1316         const struct tc_action *a;
1317         LIST_HEAD(actions);
1318         int err;
1319
1320         if (tc_no_actions(exts))
1321                 return -EINVAL;
1322
1323         attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
1324         attr->action = 0;
1325
1326         tcf_exts_to_list(exts, &actions);
1327         list_for_each_entry(a, &actions, list) {
1328                 if (is_tcf_gact_shot(a)) {
1329                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
1330                         if (MLX5_CAP_FLOWTABLE(priv->mdev,
1331                                                flow_table_properties_nic_receive.flow_counter))
1332                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
1333                         continue;
1334                 }
1335
1336                 if (is_tcf_pedit(a)) {
1337                         err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_KERNEL,
1338                                                     parse_attr);
1339                         if (err)
1340                                 return err;
1341
1342                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1343                                         MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1344                         continue;
1345                 }
1346
1347                 if (is_tcf_csum(a)) {
1348                         if (csum_offload_supported(priv, attr->action,
1349                                                    tcf_csum_update_flags(a)))
1350                                 continue;
1351
1352                         return -EOPNOTSUPP;
1353                 }
1354
1355                 if (is_tcf_skbedit_mark(a)) {
1356                         u32 mark = tcf_skbedit_mark(a);
1357
1358                         if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
1359                                 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
1360                                             mark);
1361                                 return -EINVAL;
1362                         }
1363
1364                         attr->flow_tag = mark;
1365                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1366                         continue;
1367                 }
1368
1369                 return -EINVAL;
1370         }
1371
1372         return 0;
1373 }
1374
1375 static inline int cmp_encap_info(struct ip_tunnel_key *a,
1376                                  struct ip_tunnel_key *b)
1377 {
1378         return memcmp(a, b, sizeof(*a));
1379 }
1380
1381 static inline int hash_encap_info(struct ip_tunnel_key *key)
1382 {
1383         return jhash(key, sizeof(*key), 0);
1384 }
1385
1386 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
1387                                    struct net_device *mirred_dev,
1388                                    struct net_device **out_dev,
1389                                    struct flowi4 *fl4,
1390                                    struct neighbour **out_n,
1391                                    int *out_ttl)
1392 {
1393         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1394         struct rtable *rt;
1395         struct neighbour *n = NULL;
1396
1397 #if IS_ENABLED(CONFIG_INET)
1398         int ret;
1399
1400         rt = ip_route_output_key(dev_net(mirred_dev), fl4);
1401         ret = PTR_ERR_OR_ZERO(rt);
1402         if (ret)
1403                 return ret;
1404 #else
1405         return -EOPNOTSUPP;
1406 #endif
1407         /* if the egress device isn't on the same HW e-switch, we use the uplink */
1408         if (!switchdev_port_same_parent_id(priv->netdev, rt->dst.dev))
1409                 *out_dev = mlx5_eswitch_get_uplink_netdev(esw);
1410         else
1411                 *out_dev = rt->dst.dev;
1412
1413         *out_ttl = ip4_dst_hoplimit(&rt->dst);
1414         n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
1415         ip_rt_put(rt);
1416         if (!n)
1417                 return -ENOMEM;
1418
1419         *out_n = n;
1420         return 0;
1421 }
1422
1423 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
1424                                    struct net_device *mirred_dev,
1425                                    struct net_device **out_dev,
1426                                    struct flowi6 *fl6,
1427                                    struct neighbour **out_n,
1428                                    int *out_ttl)
1429 {
1430         struct neighbour *n = NULL;
1431         struct dst_entry *dst;
1432
1433 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
1434         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1435         int ret;
1436
1437         dst = ip6_route_output(dev_net(mirred_dev), NULL, fl6);
1438         ret = dst->error;
1439         if (ret) {
1440                 dst_release(dst);
1441                 return ret;
1442         }
1443
1444         *out_ttl = ip6_dst_hoplimit(dst);
1445
1446         /* if the egress device isn't on the same HW e-switch, we use the uplink */
1447         if (!switchdev_port_same_parent_id(priv->netdev, dst->dev))
1448                 *out_dev = mlx5_eswitch_get_uplink_netdev(esw);
1449         else
1450                 *out_dev = dst->dev;
1451 #else
1452         return -EOPNOTSUPP;
1453 #endif
1454
1455         n = dst_neigh_lookup(dst, &fl6->daddr);
1456         dst_release(dst);
1457         if (!n)
1458                 return -ENOMEM;
1459
1460         *out_n = n;
1461         return 0;
1462 }
1463
1464 static void gen_vxlan_header_ipv4(struct net_device *out_dev,
1465                                   char buf[], int encap_size,
1466                                   unsigned char h_dest[ETH_ALEN],
1467                                   int ttl,
1468                                   __be32 daddr,
1469                                   __be32 saddr,
1470                                   __be16 udp_dst_port,
1471                                   __be32 vx_vni)
1472 {
1473         struct ethhdr *eth = (struct ethhdr *)buf;
1474         struct iphdr  *ip = (struct iphdr *)((char *)eth + sizeof(struct ethhdr));
1475         struct udphdr *udp = (struct udphdr *)((char *)ip + sizeof(struct iphdr));
1476         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1477
1478         memset(buf, 0, encap_size);
1479
1480         ether_addr_copy(eth->h_dest, h_dest);
1481         ether_addr_copy(eth->h_source, out_dev->dev_addr);
1482         eth->h_proto = htons(ETH_P_IP);
1483
1484         ip->daddr = daddr;
1485         ip->saddr = saddr;
1486
1487         ip->ttl = ttl;
1488         ip->protocol = IPPROTO_UDP;
1489         ip->version = 0x4;
1490         ip->ihl = 0x5;
1491
1492         udp->dest = udp_dst_port;
1493         vxh->vx_flags = VXLAN_HF_VNI;
1494         vxh->vx_vni = vxlan_vni_field(vx_vni);
1495 }
1496
1497 static void gen_vxlan_header_ipv6(struct net_device *out_dev,
1498                                   char buf[], int encap_size,
1499                                   unsigned char h_dest[ETH_ALEN],
1500                                   int ttl,
1501                                   struct in6_addr *daddr,
1502                                   struct in6_addr *saddr,
1503                                   __be16 udp_dst_port,
1504                                   __be32 vx_vni)
1505 {
1506         struct ethhdr *eth = (struct ethhdr *)buf;
1507         struct ipv6hdr *ip6h = (struct ipv6hdr *)((char *)eth + sizeof(struct ethhdr));
1508         struct udphdr *udp = (struct udphdr *)((char *)ip6h + sizeof(struct ipv6hdr));
1509         struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1510
1511         memset(buf, 0, encap_size);
1512
1513         ether_addr_copy(eth->h_dest, h_dest);
1514         ether_addr_copy(eth->h_source, out_dev->dev_addr);
1515         eth->h_proto = htons(ETH_P_IPV6);
1516
1517         ip6_flow_hdr(ip6h, 0, 0);
1518         /* the HW fills up ipv6 payload len */
1519         ip6h->nexthdr     = IPPROTO_UDP;
1520         ip6h->hop_limit   = ttl;
1521         ip6h->daddr       = *daddr;
1522         ip6h->saddr       = *saddr;
1523
1524         udp->dest = udp_dst_port;
1525         vxh->vx_flags = VXLAN_HF_VNI;
1526         vxh->vx_vni = vxlan_vni_field(vx_vni);
1527 }
1528
1529 static int mlx5e_create_encap_header_ipv4(struct mlx5e_priv *priv,
1530                                           struct net_device *mirred_dev,
1531                                           struct mlx5e_encap_entry *e)
1532 {
1533         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1534         int ipv4_encap_size = ETH_HLEN + sizeof(struct iphdr) + VXLAN_HLEN;
1535         struct ip_tunnel_key *tun_key = &e->tun_info.key;
1536         struct net_device *out_dev;
1537         struct neighbour *n = NULL;
1538         struct flowi4 fl4 = {};
1539         char *encap_header;
1540         int ttl, err;
1541         u8 nud_state;
1542
1543         if (max_encap_size < ipv4_encap_size) {
1544                 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1545                                ipv4_encap_size, max_encap_size);
1546                 return -EOPNOTSUPP;
1547         }
1548
1549         encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
1550         if (!encap_header)
1551                 return -ENOMEM;
1552
1553         switch (e->tunnel_type) {
1554         case MLX5_HEADER_TYPE_VXLAN:
1555                 fl4.flowi4_proto = IPPROTO_UDP;
1556                 fl4.fl4_dport = tun_key->tp_dst;
1557                 break;
1558         default:
1559                 err = -EOPNOTSUPP;
1560                 goto out;
1561         }
1562         fl4.flowi4_tos = tun_key->tos;
1563         fl4.daddr = tun_key->u.ipv4.dst;
1564         fl4.saddr = tun_key->u.ipv4.src;
1565
1566         err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev,
1567                                       &fl4, &n, &ttl);
1568         if (err)
1569                 goto out;
1570
1571         /* used by mlx5e_detach_encap to lookup a neigh hash table
1572          * entry in the neigh hash table when a user deletes a rule
1573          */
1574         e->m_neigh.dev = n->dev;
1575         e->m_neigh.family = n->ops->family;
1576         memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1577         e->out_dev = out_dev;
1578
1579         /* It's importent to add the neigh to the hash table before checking
1580          * the neigh validity state. So if we'll get a notification, in case the
1581          * neigh changes it's validity state, we would find the relevant neigh
1582          * in the hash.
1583          */
1584         err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1585         if (err)
1586                 goto out;
1587
1588         read_lock_bh(&n->lock);
1589         nud_state = n->nud_state;
1590         ether_addr_copy(e->h_dest, n->ha);
1591         read_unlock_bh(&n->lock);
1592
1593         switch (e->tunnel_type) {
1594         case MLX5_HEADER_TYPE_VXLAN:
1595                 gen_vxlan_header_ipv4(out_dev, encap_header,
1596                                       ipv4_encap_size, e->h_dest, ttl,
1597                                       fl4.daddr,
1598                                       fl4.saddr, tun_key->tp_dst,
1599                                       tunnel_id_to_key32(tun_key->tun_id));
1600                 break;
1601         default:
1602                 err = -EOPNOTSUPP;
1603                 goto destroy_neigh_entry;
1604         }
1605         e->encap_size = ipv4_encap_size;
1606         e->encap_header = encap_header;
1607
1608         if (!(nud_state & NUD_VALID)) {
1609                 neigh_event_send(n, NULL);
1610                 err = -EAGAIN;
1611                 goto out;
1612         }
1613
1614         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1615                                ipv4_encap_size, encap_header, &e->encap_id);
1616         if (err)
1617                 goto destroy_neigh_entry;
1618
1619         e->flags |= MLX5_ENCAP_ENTRY_VALID;
1620         mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1621         neigh_release(n);
1622         return err;
1623
1624 destroy_neigh_entry:
1625         mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1626 out:
1627         kfree(encap_header);
1628         if (n)
1629                 neigh_release(n);
1630         return err;
1631 }
1632
1633 static int mlx5e_create_encap_header_ipv6(struct mlx5e_priv *priv,
1634                                           struct net_device *mirred_dev,
1635                                           struct mlx5e_encap_entry *e)
1636 {
1637         int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1638         int ipv6_encap_size = ETH_HLEN + sizeof(struct ipv6hdr) + VXLAN_HLEN;
1639         struct ip_tunnel_key *tun_key = &e->tun_info.key;
1640         struct net_device *out_dev;
1641         struct neighbour *n = NULL;
1642         struct flowi6 fl6 = {};
1643         char *encap_header;
1644         int err, ttl = 0;
1645         u8 nud_state;
1646
1647         if (max_encap_size < ipv6_encap_size) {
1648                 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1649                                ipv6_encap_size, max_encap_size);
1650                 return -EOPNOTSUPP;
1651         }
1652
1653         encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
1654         if (!encap_header)
1655                 return -ENOMEM;
1656
1657         switch (e->tunnel_type) {
1658         case MLX5_HEADER_TYPE_VXLAN:
1659                 fl6.flowi6_proto = IPPROTO_UDP;
1660                 fl6.fl6_dport = tun_key->tp_dst;
1661                 break;
1662         default:
1663                 err = -EOPNOTSUPP;
1664                 goto out;
1665         }
1666
1667         fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
1668         fl6.daddr = tun_key->u.ipv6.dst;
1669         fl6.saddr = tun_key->u.ipv6.src;
1670
1671         err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev,
1672                                       &fl6, &n, &ttl);
1673         if (err)
1674                 goto out;
1675
1676         /* used by mlx5e_detach_encap to lookup a neigh hash table
1677          * entry in the neigh hash table when a user deletes a rule
1678          */
1679         e->m_neigh.dev = n->dev;
1680         e->m_neigh.family = n->ops->family;
1681         memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1682         e->out_dev = out_dev;
1683
1684         /* It's importent to add the neigh to the hash table before checking
1685          * the neigh validity state. So if we'll get a notification, in case the
1686          * neigh changes it's validity state, we would find the relevant neigh
1687          * in the hash.
1688          */
1689         err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1690         if (err)
1691                 goto out;
1692
1693         read_lock_bh(&n->lock);
1694         nud_state = n->nud_state;
1695         ether_addr_copy(e->h_dest, n->ha);
1696         read_unlock_bh(&n->lock);
1697
1698         switch (e->tunnel_type) {
1699         case MLX5_HEADER_TYPE_VXLAN:
1700                 gen_vxlan_header_ipv6(out_dev, encap_header,
1701                                       ipv6_encap_size, e->h_dest, ttl,
1702                                       &fl6.daddr,
1703                                       &fl6.saddr, tun_key->tp_dst,
1704                                       tunnel_id_to_key32(tun_key->tun_id));
1705                 break;
1706         default:
1707                 err = -EOPNOTSUPP;
1708                 goto destroy_neigh_entry;
1709         }
1710
1711         e->encap_size = ipv6_encap_size;
1712         e->encap_header = encap_header;
1713
1714         if (!(nud_state & NUD_VALID)) {
1715                 neigh_event_send(n, NULL);
1716                 err = -EAGAIN;
1717                 goto out;
1718         }
1719
1720         err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1721                                ipv6_encap_size, encap_header, &e->encap_id);
1722         if (err)
1723                 goto destroy_neigh_entry;
1724
1725         e->flags |= MLX5_ENCAP_ENTRY_VALID;
1726         mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1727         neigh_release(n);
1728         return err;
1729
1730 destroy_neigh_entry:
1731         mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1732 out:
1733         kfree(encap_header);
1734         if (n)
1735                 neigh_release(n);
1736         return err;
1737 }
1738
1739 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
1740                               struct ip_tunnel_info *tun_info,
1741                               struct net_device *mirred_dev,
1742                               struct net_device **encap_dev,
1743                               struct mlx5e_tc_flow *flow)
1744 {
1745         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1746         struct net_device *up_dev = mlx5_eswitch_get_uplink_netdev(esw);
1747         unsigned short family = ip_tunnel_info_af(tun_info);
1748         struct mlx5e_priv *up_priv = netdev_priv(up_dev);
1749         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1750         struct ip_tunnel_key *key = &tun_info->key;
1751         struct mlx5e_encap_entry *e;
1752         int tunnel_type, err = 0;
1753         uintptr_t hash_key;
1754         bool found = false;
1755
1756         /* udp dst port must be set */
1757         if (!memchr_inv(&key->tp_dst, 0, sizeof(key->tp_dst)))
1758                 goto vxlan_encap_offload_err;
1759
1760         /* setting udp src port isn't supported */
1761         if (memchr_inv(&key->tp_src, 0, sizeof(key->tp_src))) {
1762 vxlan_encap_offload_err:
1763                 netdev_warn(priv->netdev,
1764                             "must set udp dst port and not set udp src port\n");
1765                 return -EOPNOTSUPP;
1766         }
1767
1768         if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->tp_dst)) &&
1769             MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap)) {
1770                 tunnel_type = MLX5_HEADER_TYPE_VXLAN;
1771         } else {
1772                 netdev_warn(priv->netdev,
1773                             "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->tp_dst));
1774                 return -EOPNOTSUPP;
1775         }
1776
1777         hash_key = hash_encap_info(key);
1778
1779         hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
1780                                    encap_hlist, hash_key) {
1781                 if (!cmp_encap_info(&e->tun_info.key, key)) {
1782                         found = true;
1783                         break;
1784                 }
1785         }
1786
1787         if (found)
1788                 goto attach_flow;
1789
1790         e = kzalloc(sizeof(*e), GFP_KERNEL);
1791         if (!e)
1792                 return -ENOMEM;
1793
1794         e->tun_info = *tun_info;
1795         e->tunnel_type = tunnel_type;
1796         INIT_LIST_HEAD(&e->flows);
1797
1798         if (family == AF_INET)
1799                 err = mlx5e_create_encap_header_ipv4(priv, mirred_dev, e);
1800         else if (family == AF_INET6)
1801                 err = mlx5e_create_encap_header_ipv6(priv, mirred_dev, e);
1802
1803         if (err && err != -EAGAIN)
1804                 goto out_err;
1805
1806         hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
1807
1808 attach_flow:
1809         list_add(&flow->encap, &e->flows);
1810         *encap_dev = e->out_dev;
1811         if (e->flags & MLX5_ENCAP_ENTRY_VALID)
1812                 attr->encap_id = e->encap_id;
1813
1814         return err;
1815
1816 out_err:
1817         kfree(e);
1818         return err;
1819 }
1820
1821 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1822                                 struct mlx5e_tc_flow_parse_attr *parse_attr,
1823                                 struct mlx5e_tc_flow *flow)
1824 {
1825         struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1826         struct mlx5e_rep_priv *rpriv = priv->ppriv;
1827         struct ip_tunnel_info *info = NULL;
1828         const struct tc_action *a;
1829         LIST_HEAD(actions);
1830         bool encap = false;
1831         int err = 0;
1832
1833         if (tc_no_actions(exts))
1834                 return -EINVAL;
1835
1836         memset(attr, 0, sizeof(*attr));
1837         attr->in_rep = rpriv->rep;
1838
1839         tcf_exts_to_list(exts, &actions);
1840         list_for_each_entry(a, &actions, list) {
1841                 if (is_tcf_gact_shot(a)) {
1842                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
1843                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
1844                         continue;
1845                 }
1846
1847                 if (is_tcf_pedit(a)) {
1848                         err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_FDB,
1849                                                     parse_attr);
1850                         if (err)
1851                                 return err;
1852
1853                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1854                         continue;
1855                 }
1856
1857                 if (is_tcf_csum(a)) {
1858                         if (csum_offload_supported(priv, attr->action,
1859                                                    tcf_csum_update_flags(a)))
1860                                 continue;
1861
1862                         return -EOPNOTSUPP;
1863                 }
1864
1865                 if (is_tcf_mirred_egress_redirect(a)) {
1866                         int ifindex = tcf_mirred_ifindex(a);
1867                         struct net_device *out_dev, *encap_dev = NULL;
1868                         struct mlx5e_priv *out_priv;
1869
1870                         out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
1871
1872                         if (switchdev_port_same_parent_id(priv->netdev,
1873                                                           out_dev)) {
1874                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1875                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
1876                                 out_priv = netdev_priv(out_dev);
1877                                 rpriv = out_priv->ppriv;
1878                                 attr->out_rep = rpriv->rep;
1879                         } else if (encap) {
1880                                 err = mlx5e_attach_encap(priv, info,
1881                                                          out_dev, &encap_dev, flow);
1882                                 if (err && err != -EAGAIN)
1883                                         return err;
1884                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1885                                         MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1886                                         MLX5_FLOW_CONTEXT_ACTION_COUNT;
1887                                 out_priv = netdev_priv(encap_dev);
1888                                 rpriv = out_priv->ppriv;
1889                                 attr->out_rep = rpriv->rep;
1890                                 attr->parse_attr = parse_attr;
1891                         } else {
1892                                 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
1893                                        priv->netdev->name, out_dev->name);
1894                                 return -EINVAL;
1895                         }
1896                         continue;
1897                 }
1898
1899                 if (is_tcf_tunnel_set(a)) {
1900                         info = tcf_tunnel_info(a);
1901                         if (info)
1902                                 encap = true;
1903                         else
1904                                 return -EOPNOTSUPP;
1905                         continue;
1906                 }
1907
1908                 if (is_tcf_vlan(a)) {
1909                         if (tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
1910                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
1911                         } else if (tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
1912                                 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
1913                                         return -EOPNOTSUPP;
1914
1915                                 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
1916                                 attr->vlan = tcf_vlan_push_vid(a);
1917                         } else { /* action is TCA_VLAN_ACT_MODIFY */
1918                                 return -EOPNOTSUPP;
1919                         }
1920                         continue;
1921                 }
1922
1923                 if (is_tcf_tunnel_release(a)) {
1924                         attr->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
1925                         continue;
1926                 }
1927
1928                 return -EINVAL;
1929         }
1930         return err;
1931 }
1932
1933 int mlx5e_configure_flower(struct mlx5e_priv *priv, __be16 protocol,
1934                            struct tc_cls_flower_offload *f)
1935 {
1936         struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1937         struct mlx5e_tc_flow_parse_attr *parse_attr;
1938         struct mlx5e_tc_table *tc = &priv->fs.tc;
1939         struct mlx5e_tc_flow *flow;
1940         int attr_size, err = 0;
1941         u8 flow_flags = 0;
1942
1943         if (esw && esw->mode == SRIOV_OFFLOADS) {
1944                 flow_flags = MLX5E_TC_FLOW_ESWITCH;
1945                 attr_size  = sizeof(struct mlx5_esw_flow_attr);
1946         } else {
1947                 flow_flags = MLX5E_TC_FLOW_NIC;
1948                 attr_size  = sizeof(struct mlx5_nic_flow_attr);
1949         }
1950
1951         flow = kzalloc(sizeof(*flow) + attr_size, GFP_KERNEL);
1952         parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
1953         if (!parse_attr || !flow) {
1954                 err = -ENOMEM;
1955                 goto err_free;
1956         }
1957
1958         flow->cookie = f->cookie;
1959         flow->flags = flow_flags;
1960
1961         err = parse_cls_flower(priv, flow, &parse_attr->spec, f);
1962         if (err < 0)
1963                 goto err_free;
1964
1965         if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
1966                 err = parse_tc_fdb_actions(priv, f->exts, parse_attr, flow);
1967                 if (err < 0)
1968                         goto err_handle_encap_flow;
1969                 flow->rule = mlx5e_tc_add_fdb_flow(priv, parse_attr, flow);
1970         } else {
1971                 err = parse_tc_nic_actions(priv, f->exts, parse_attr, flow);
1972                 if (err < 0)
1973                         goto err_free;
1974                 flow->rule = mlx5e_tc_add_nic_flow(priv, parse_attr, flow);
1975         }
1976
1977         if (IS_ERR(flow->rule)) {
1978                 err = PTR_ERR(flow->rule);
1979                 goto err_free;
1980         }
1981
1982         flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
1983         err = rhashtable_insert_fast(&tc->ht, &flow->node,
1984                                      tc->ht_params);
1985         if (err)
1986                 goto err_del_rule;
1987
1988         if (flow->flags & MLX5E_TC_FLOW_ESWITCH &&
1989             !(flow->esw_attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP))
1990                 kvfree(parse_attr);
1991         return err;
1992
1993 err_del_rule:
1994         mlx5e_tc_del_flow(priv, flow);
1995
1996 err_handle_encap_flow:
1997         if (err == -EAGAIN) {
1998                 err = rhashtable_insert_fast(&tc->ht, &flow->node,
1999                                              tc->ht_params);
2000                 if (err)
2001                         mlx5e_tc_del_flow(priv, flow);
2002                 else
2003                         return 0;
2004         }
2005
2006 err_free:
2007         kvfree(parse_attr);
2008         kfree(flow);
2009         return err;
2010 }
2011
2012 int mlx5e_delete_flower(struct mlx5e_priv *priv,
2013                         struct tc_cls_flower_offload *f)
2014 {
2015         struct mlx5e_tc_flow *flow;
2016         struct mlx5e_tc_table *tc = &priv->fs.tc;
2017
2018         flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2019                                       tc->ht_params);
2020         if (!flow)
2021                 return -EINVAL;
2022
2023         rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
2024
2025         mlx5e_tc_del_flow(priv, flow);
2026
2027         kfree(flow);
2028
2029         return 0;
2030 }
2031
2032 int mlx5e_stats_flower(struct mlx5e_priv *priv,
2033                        struct tc_cls_flower_offload *f)
2034 {
2035         struct mlx5e_tc_table *tc = &priv->fs.tc;
2036         struct mlx5e_tc_flow *flow;
2037         struct mlx5_fc *counter;
2038         u64 bytes;
2039         u64 packets;
2040         u64 lastuse;
2041
2042         flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2043                                       tc->ht_params);
2044         if (!flow)
2045                 return -EINVAL;
2046
2047         if (!(flow->flags & MLX5E_TC_FLOW_OFFLOADED))
2048                 return 0;
2049
2050         counter = mlx5_flow_rule_counter(flow->rule);
2051         if (!counter)
2052                 return 0;
2053
2054         mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
2055
2056         tcf_exts_stats_update(f->exts, bytes, packets, lastuse);
2057
2058         return 0;
2059 }
2060
2061 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
2062         .head_offset = offsetof(struct mlx5e_tc_flow, node),
2063         .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
2064         .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
2065         .automatic_shrinking = true,
2066 };
2067
2068 int mlx5e_tc_init(struct mlx5e_priv *priv)
2069 {
2070         struct mlx5e_tc_table *tc = &priv->fs.tc;
2071
2072         hash_init(tc->mod_hdr_tbl);
2073
2074         tc->ht_params = mlx5e_tc_flow_ht_params;
2075         return rhashtable_init(&tc->ht, &tc->ht_params);
2076 }
2077
2078 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
2079 {
2080         struct mlx5e_tc_flow *flow = ptr;
2081         struct mlx5e_priv *priv = arg;
2082
2083         mlx5e_tc_del_flow(priv, flow);
2084         kfree(flow);
2085 }
2086
2087 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
2088 {
2089         struct mlx5e_tc_table *tc = &priv->fs.tc;
2090
2091         rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
2092
2093         if (!IS_ERR_OR_NULL(tc->t)) {
2094                 mlx5_destroy_flow_table(tc->t);
2095                 tc->t = NULL;
2096         }
2097 }