2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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>
55 struct mlx5_nic_flow_attr {
62 MLX5E_TC_FLOW_ESWITCH = BIT(0),
63 MLX5E_TC_FLOW_NIC = BIT(1),
64 MLX5E_TC_FLOW_OFFLOADED = BIT(2),
67 struct mlx5e_tc_flow {
68 struct rhash_head node;
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 */
75 struct mlx5_esw_flow_attr esw_attr[0];
76 struct mlx5_nic_flow_attr nic_attr[0];
80 struct mlx5e_tc_flow_parse_attr {
81 struct ip_tunnel_info tun_info;
82 struct mlx5_flow_spec spec;
83 int num_mod_hdr_actions;
84 void *mod_hdr_actions;
89 MLX5_HEADER_TYPE_VXLAN = 0x0,
90 MLX5_HEADER_TYPE_NVGRE = 0x1,
93 #define MLX5E_TC_TABLE_NUM_ENTRIES 1024
94 #define MLX5E_TC_TABLE_NUM_GROUPS 4
101 struct mlx5e_mod_hdr_entry {
102 /* a node of a hash table which keeps all the mod_hdr entries */
103 struct hlist_node mod_hdr_hlist;
105 /* flows sharing the same mod_hdr entry */
106 struct list_head flows;
108 struct mod_hdr_key key;
113 #define MLX5_MH_ACT_SZ MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto)
115 static inline u32 hash_mod_hdr_info(struct mod_hdr_key *key)
117 return jhash(key->actions,
118 key->num_actions * MLX5_MH_ACT_SZ, 0);
121 static inline int cmp_mod_hdr_info(struct mod_hdr_key *a,
122 struct mod_hdr_key *b)
124 if (a->num_actions != b->num_actions)
127 return memcmp(a->actions, b->actions, a->num_actions * MLX5_MH_ACT_SZ);
130 static int mlx5e_attach_mod_hdr(struct mlx5e_priv *priv,
131 struct mlx5e_tc_flow *flow,
132 struct mlx5e_tc_flow_parse_attr *parse_attr)
134 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
135 int num_actions, actions_size, namespace, err;
136 struct mlx5e_mod_hdr_entry *mh;
137 struct mod_hdr_key key;
141 num_actions = parse_attr->num_mod_hdr_actions;
142 actions_size = MLX5_MH_ACT_SZ * num_actions;
144 key.actions = parse_attr->mod_hdr_actions;
145 key.num_actions = num_actions;
147 hash_key = hash_mod_hdr_info(&key);
149 if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
150 namespace = MLX5_FLOW_NAMESPACE_FDB;
151 hash_for_each_possible(esw->offloads.mod_hdr_tbl, mh,
152 mod_hdr_hlist, hash_key) {
153 if (!cmp_mod_hdr_info(&mh->key, &key)) {
159 namespace = MLX5_FLOW_NAMESPACE_KERNEL;
160 hash_for_each_possible(priv->fs.tc.mod_hdr_tbl, mh,
161 mod_hdr_hlist, hash_key) {
162 if (!cmp_mod_hdr_info(&mh->key, &key)) {
172 mh = kzalloc(sizeof(*mh) + actions_size, GFP_KERNEL);
176 mh->key.actions = (void *)mh + sizeof(*mh);
177 memcpy(mh->key.actions, key.actions, actions_size);
178 mh->key.num_actions = num_actions;
179 INIT_LIST_HEAD(&mh->flows);
181 err = mlx5_modify_header_alloc(priv->mdev, namespace,
188 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
189 hash_add(esw->offloads.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
191 hash_add(priv->fs.tc.mod_hdr_tbl, &mh->mod_hdr_hlist, hash_key);
194 list_add(&flow->mod_hdr, &mh->flows);
195 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
196 flow->esw_attr->mod_hdr_id = mh->mod_hdr_id;
198 flow->nic_attr->mod_hdr_id = mh->mod_hdr_id;
207 static void mlx5e_detach_mod_hdr(struct mlx5e_priv *priv,
208 struct mlx5e_tc_flow *flow)
210 struct list_head *next = flow->mod_hdr.next;
212 list_del(&flow->mod_hdr);
214 if (list_empty(next)) {
215 struct mlx5e_mod_hdr_entry *mh;
217 mh = list_entry(next, struct mlx5e_mod_hdr_entry, flows);
219 mlx5_modify_header_dealloc(priv->mdev, mh->mod_hdr_id);
220 hash_del(&mh->mod_hdr_hlist);
225 static struct mlx5_flow_handle *
226 mlx5e_tc_add_nic_flow(struct mlx5e_priv *priv,
227 struct mlx5e_tc_flow_parse_attr *parse_attr,
228 struct mlx5e_tc_flow *flow)
230 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
231 struct mlx5_core_dev *dev = priv->mdev;
232 struct mlx5_flow_destination dest = {};
233 struct mlx5_flow_act flow_act = {
234 .action = attr->action,
235 .flow_tag = attr->flow_tag,
238 struct mlx5_fc *counter = NULL;
239 struct mlx5_flow_handle *rule;
240 bool table_created = false;
243 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_FWD_DEST) {
244 dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
245 dest.ft = priv->fs.vlan.ft.t;
246 } else if (attr->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
247 counter = mlx5_fc_create(dev, true);
249 return ERR_CAST(counter);
251 dest.type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
252 dest.counter = counter;
255 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
256 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
257 flow_act.modify_id = attr->mod_hdr_id;
258 kfree(parse_attr->mod_hdr_actions);
261 goto err_create_mod_hdr_id;
265 if (IS_ERR_OR_NULL(priv->fs.tc.t)) {
267 mlx5_create_auto_grouped_flow_table(priv->fs.ns,
269 MLX5E_TC_TABLE_NUM_ENTRIES,
270 MLX5E_TC_TABLE_NUM_GROUPS,
272 if (IS_ERR(priv->fs.tc.t)) {
273 netdev_err(priv->netdev,
274 "Failed to create tc offload table\n");
275 rule = ERR_CAST(priv->fs.tc.t);
279 table_created = true;
282 parse_attr->spec.match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
283 rule = mlx5_add_flow_rules(priv->fs.tc.t, &parse_attr->spec,
284 &flow_act, &dest, 1);
293 mlx5_destroy_flow_table(priv->fs.tc.t);
294 priv->fs.tc.t = NULL;
297 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
298 mlx5e_detach_mod_hdr(priv, flow);
299 err_create_mod_hdr_id:
300 mlx5_fc_destroy(dev, counter);
305 static void mlx5e_tc_del_nic_flow(struct mlx5e_priv *priv,
306 struct mlx5e_tc_flow *flow)
308 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
309 struct mlx5_fc *counter = NULL;
311 counter = mlx5_flow_rule_counter(flow->rule);
312 mlx5_del_flow_rules(flow->rule);
313 mlx5_fc_destroy(priv->mdev, counter);
315 if (!mlx5e_tc_num_filters(priv) && (priv->fs.tc.t)) {
316 mlx5_destroy_flow_table(priv->fs.tc.t);
317 priv->fs.tc.t = NULL;
320 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
321 mlx5e_detach_mod_hdr(priv, flow);
324 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
325 struct mlx5e_tc_flow *flow);
327 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
328 struct ip_tunnel_info *tun_info,
329 struct net_device *mirred_dev,
330 struct net_device **encap_dev,
331 struct mlx5e_tc_flow *flow);
333 static struct mlx5_flow_handle *
334 mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv,
335 struct mlx5e_tc_flow_parse_attr *parse_attr,
336 struct mlx5e_tc_flow *flow)
338 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
339 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
340 struct net_device *out_dev, *encap_dev = NULL;
341 struct mlx5_flow_handle *rule = NULL;
342 struct mlx5e_rep_priv *rpriv;
343 struct mlx5e_priv *out_priv;
346 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
347 out_dev = __dev_get_by_index(dev_net(priv->netdev),
348 attr->parse_attr->mirred_ifindex);
349 err = mlx5e_attach_encap(priv, &parse_attr->tun_info,
350 out_dev, &encap_dev, flow);
354 goto err_attach_encap;
356 out_priv = netdev_priv(encap_dev);
357 rpriv = out_priv->ppriv;
358 attr->out_rep = rpriv->rep;
361 err = mlx5_eswitch_add_vlan_action(esw, attr);
367 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) {
368 err = mlx5e_attach_mod_hdr(priv, flow, parse_attr);
369 kfree(parse_attr->mod_hdr_actions);
376 /* we get here if (1) there's no error (rule being null) or when
377 * (2) there's an encap action and we're on -EAGAIN (no valid neigh)
379 if (rule != ERR_PTR(-EAGAIN)) {
380 rule = mlx5_eswitch_add_offloaded_rule(esw, &parse_attr->spec, attr);
387 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
388 mlx5e_detach_mod_hdr(priv, flow);
390 mlx5_eswitch_del_vlan_action(esw, attr);
392 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP)
393 mlx5e_detach_encap(priv, flow);
398 static void mlx5e_tc_del_fdb_flow(struct mlx5e_priv *priv,
399 struct mlx5e_tc_flow *flow)
401 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
402 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
404 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
405 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
406 mlx5_eswitch_del_offloaded_rule(esw, flow->rule, attr);
409 mlx5_eswitch_del_vlan_action(esw, attr);
411 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP) {
412 mlx5e_detach_encap(priv, flow);
413 kvfree(attr->parse_attr);
416 if (attr->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
417 mlx5e_detach_mod_hdr(priv, flow);
420 void mlx5e_tc_encap_flows_add(struct mlx5e_priv *priv,
421 struct mlx5e_encap_entry *e)
423 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
424 struct mlx5_esw_flow_attr *esw_attr;
425 struct mlx5e_tc_flow *flow;
428 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
429 e->encap_size, e->encap_header,
432 mlx5_core_warn(priv->mdev, "Failed to offload cached encapsulation header, %d\n",
436 e->flags |= MLX5_ENCAP_ENTRY_VALID;
437 mlx5e_rep_queue_neigh_stats_work(priv);
439 list_for_each_entry(flow, &e->flows, encap) {
440 esw_attr = flow->esw_attr;
441 esw_attr->encap_id = e->encap_id;
442 flow->rule = mlx5_eswitch_add_offloaded_rule(esw, &esw_attr->parse_attr->spec, esw_attr);
443 if (IS_ERR(flow->rule)) {
444 err = PTR_ERR(flow->rule);
445 mlx5_core_warn(priv->mdev, "Failed to update cached encapsulation flow, %d\n",
449 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
453 void mlx5e_tc_encap_flows_del(struct mlx5e_priv *priv,
454 struct mlx5e_encap_entry *e)
456 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
457 struct mlx5e_tc_flow *flow;
459 list_for_each_entry(flow, &e->flows, encap) {
460 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
461 flow->flags &= ~MLX5E_TC_FLOW_OFFLOADED;
462 mlx5_eswitch_del_offloaded_rule(esw, flow->rule, flow->esw_attr);
466 if (e->flags & MLX5_ENCAP_ENTRY_VALID) {
467 e->flags &= ~MLX5_ENCAP_ENTRY_VALID;
468 mlx5_encap_dealloc(priv->mdev, e->encap_id);
472 void mlx5e_tc_update_neigh_used_value(struct mlx5e_neigh_hash_entry *nhe)
474 struct mlx5e_neigh *m_neigh = &nhe->m_neigh;
475 u64 bytes, packets, lastuse = 0;
476 struct mlx5e_tc_flow *flow;
477 struct mlx5e_encap_entry *e;
478 struct mlx5_fc *counter;
479 struct neigh_table *tbl;
480 bool neigh_used = false;
483 if (m_neigh->family == AF_INET)
485 #if IS_ENABLED(CONFIG_IPV6)
486 else if (m_neigh->family == AF_INET6)
487 tbl = ipv6_stub->nd_tbl;
492 list_for_each_entry(e, &nhe->encap_list, encap_list) {
493 if (!(e->flags & MLX5_ENCAP_ENTRY_VALID))
495 list_for_each_entry(flow, &e->flows, encap) {
496 if (flow->flags & MLX5E_TC_FLOW_OFFLOADED) {
497 counter = mlx5_flow_rule_counter(flow->rule);
498 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
499 if (time_after((unsigned long)lastuse, nhe->reported_lastuse)) {
508 nhe->reported_lastuse = jiffies;
510 /* find the relevant neigh according to the cached device and
513 n = neigh_lookup(tbl, &m_neigh->dst_ip, m_neigh->dev);
515 WARN(1, "The neighbour already freed\n");
519 neigh_event_send(n, NULL);
524 static void mlx5e_detach_encap(struct mlx5e_priv *priv,
525 struct mlx5e_tc_flow *flow)
527 struct list_head *next = flow->encap.next;
529 list_del(&flow->encap);
530 if (list_empty(next)) {
531 struct mlx5e_encap_entry *e;
533 e = list_entry(next, struct mlx5e_encap_entry, flows);
534 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
536 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
537 mlx5_encap_dealloc(priv->mdev, e->encap_id);
539 hash_del_rcu(&e->encap_hlist);
540 kfree(e->encap_header);
545 static void mlx5e_tc_del_flow(struct mlx5e_priv *priv,
546 struct mlx5e_tc_flow *flow)
548 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
549 mlx5e_tc_del_fdb_flow(priv, flow);
551 mlx5e_tc_del_nic_flow(priv, flow);
554 static void parse_vxlan_attr(struct mlx5_flow_spec *spec,
555 struct tc_cls_flower_offload *f)
557 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
559 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
561 void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
563 void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
566 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
567 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
569 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
570 struct flow_dissector_key_keyid *key =
571 skb_flow_dissector_target(f->dissector,
572 FLOW_DISSECTOR_KEY_ENC_KEYID,
574 struct flow_dissector_key_keyid *mask =
575 skb_flow_dissector_target(f->dissector,
576 FLOW_DISSECTOR_KEY_ENC_KEYID,
578 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
579 be32_to_cpu(mask->keyid));
580 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
581 be32_to_cpu(key->keyid));
585 static int parse_tunnel_attr(struct mlx5e_priv *priv,
586 struct mlx5_flow_spec *spec,
587 struct tc_cls_flower_offload *f)
589 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
591 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
594 struct flow_dissector_key_control *enc_control =
595 skb_flow_dissector_target(f->dissector,
596 FLOW_DISSECTOR_KEY_ENC_CONTROL,
599 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
600 struct flow_dissector_key_ports *key =
601 skb_flow_dissector_target(f->dissector,
602 FLOW_DISSECTOR_KEY_ENC_PORTS,
604 struct flow_dissector_key_ports *mask =
605 skb_flow_dissector_target(f->dissector,
606 FLOW_DISSECTOR_KEY_ENC_PORTS,
608 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
609 struct net_device *up_dev = mlx5_eswitch_get_uplink_netdev(esw);
610 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
612 /* Full udp dst port must be given */
613 if (memchr_inv(&mask->dst, 0xff, sizeof(mask->dst)))
614 goto vxlan_match_offload_err;
616 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->dst)) &&
617 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
618 parse_vxlan_attr(spec, f);
620 netdev_warn(priv->netdev,
621 "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->dst));
625 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
626 udp_dport, ntohs(mask->dst));
627 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
628 udp_dport, ntohs(key->dst));
630 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
631 udp_sport, ntohs(mask->src));
632 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
633 udp_sport, ntohs(key->src));
634 } else { /* udp dst port must be given */
635 vxlan_match_offload_err:
636 netdev_warn(priv->netdev,
637 "IP tunnel decap offload supported only for vxlan, must set UDP dport\n");
641 if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
642 struct flow_dissector_key_ipv4_addrs *key =
643 skb_flow_dissector_target(f->dissector,
644 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
646 struct flow_dissector_key_ipv4_addrs *mask =
647 skb_flow_dissector_target(f->dissector,
648 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
650 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
651 src_ipv4_src_ipv6.ipv4_layout.ipv4,
653 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
654 src_ipv4_src_ipv6.ipv4_layout.ipv4,
657 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
658 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
660 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
661 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
664 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
665 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IP);
666 } else if (enc_control->addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
667 struct flow_dissector_key_ipv6_addrs *key =
668 skb_flow_dissector_target(f->dissector,
669 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
671 struct flow_dissector_key_ipv6_addrs *mask =
672 skb_flow_dissector_target(f->dissector,
673 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS,
676 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
677 src_ipv4_src_ipv6.ipv6_layout.ipv6),
678 &mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
679 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
680 src_ipv4_src_ipv6.ipv6_layout.ipv6),
681 &key->src, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
683 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
684 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
685 &mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
686 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
687 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
688 &key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6));
690 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ethertype);
691 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype, ETH_P_IPV6);
694 /* Enforce DMAC when offloading incoming tunneled flows.
695 * Flow counters require a match on the DMAC.
697 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
698 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
699 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
700 dmac_47_16), priv->netdev->dev_addr);
702 /* let software handle IP fragments */
703 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
704 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
709 static int __parse_cls_flower(struct mlx5e_priv *priv,
710 struct mlx5_flow_spec *spec,
711 struct tc_cls_flower_offload *f,
714 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
716 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
721 *min_inline = MLX5_INLINE_MODE_L2;
723 if (f->dissector->used_keys &
724 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
725 BIT(FLOW_DISSECTOR_KEY_BASIC) |
726 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
727 BIT(FLOW_DISSECTOR_KEY_VLAN) |
728 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
729 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
730 BIT(FLOW_DISSECTOR_KEY_PORTS) |
731 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
732 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) |
733 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) |
734 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) |
735 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) |
736 BIT(FLOW_DISSECTOR_KEY_TCP) |
737 BIT(FLOW_DISSECTOR_KEY_IP))) {
738 netdev_warn(priv->netdev, "Unsupported key used: 0x%x\n",
739 f->dissector->used_keys);
743 if ((dissector_uses_key(f->dissector,
744 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) ||
745 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID) ||
746 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_PORTS)) &&
747 dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
748 struct flow_dissector_key_control *key =
749 skb_flow_dissector_target(f->dissector,
750 FLOW_DISSECTOR_KEY_ENC_CONTROL,
752 switch (key->addr_type) {
753 case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
754 case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
755 if (parse_tunnel_attr(priv, spec, f))
762 /* In decap flow, header pointers should point to the inner
763 * headers, outer header were already set by parse_tunnel_attr
765 headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
767 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
771 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
772 struct flow_dissector_key_control *key =
773 skb_flow_dissector_target(f->dissector,
774 FLOW_DISSECTOR_KEY_CONTROL,
777 struct flow_dissector_key_control *mask =
778 skb_flow_dissector_target(f->dissector,
779 FLOW_DISSECTOR_KEY_CONTROL,
781 addr_type = key->addr_type;
783 if (mask->flags & FLOW_DIS_IS_FRAGMENT) {
784 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
785 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag,
786 key->flags & FLOW_DIS_IS_FRAGMENT);
788 /* the HW doesn't need L3 inline to match on frag=no */
789 if (key->flags & FLOW_DIS_IS_FRAGMENT)
790 *min_inline = MLX5_INLINE_MODE_IP;
794 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
795 struct flow_dissector_key_basic *key =
796 skb_flow_dissector_target(f->dissector,
797 FLOW_DISSECTOR_KEY_BASIC,
799 struct flow_dissector_key_basic *mask =
800 skb_flow_dissector_target(f->dissector,
801 FLOW_DISSECTOR_KEY_BASIC,
803 ip_proto = key->ip_proto;
805 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ethertype,
806 ntohs(mask->n_proto));
807 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ethertype,
808 ntohs(key->n_proto));
810 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
812 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
816 *min_inline = MLX5_INLINE_MODE_IP;
819 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
820 struct flow_dissector_key_eth_addrs *key =
821 skb_flow_dissector_target(f->dissector,
822 FLOW_DISSECTOR_KEY_ETH_ADDRS,
824 struct flow_dissector_key_eth_addrs *mask =
825 skb_flow_dissector_target(f->dissector,
826 FLOW_DISSECTOR_KEY_ETH_ADDRS,
829 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
832 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
836 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
839 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
844 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
845 struct flow_dissector_key_vlan *key =
846 skb_flow_dissector_target(f->dissector,
847 FLOW_DISSECTOR_KEY_VLAN,
849 struct flow_dissector_key_vlan *mask =
850 skb_flow_dissector_target(f->dissector,
851 FLOW_DISSECTOR_KEY_VLAN,
853 if (mask->vlan_id || mask->vlan_priority) {
854 MLX5_SET(fte_match_set_lyr_2_4, headers_c, cvlan_tag, 1);
855 MLX5_SET(fte_match_set_lyr_2_4, headers_v, cvlan_tag, 1);
857 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_vid, mask->vlan_id);
858 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_vid, key->vlan_id);
860 MLX5_SET(fte_match_set_lyr_2_4, headers_c, first_prio, mask->vlan_priority);
861 MLX5_SET(fte_match_set_lyr_2_4, headers_v, first_prio, key->vlan_priority);
865 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
866 struct flow_dissector_key_ipv4_addrs *key =
867 skb_flow_dissector_target(f->dissector,
868 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
870 struct flow_dissector_key_ipv4_addrs *mask =
871 skb_flow_dissector_target(f->dissector,
872 FLOW_DISSECTOR_KEY_IPV4_ADDRS,
875 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
876 src_ipv4_src_ipv6.ipv4_layout.ipv4),
877 &mask->src, sizeof(mask->src));
878 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
879 src_ipv4_src_ipv6.ipv4_layout.ipv4),
880 &key->src, sizeof(key->src));
881 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
882 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
883 &mask->dst, sizeof(mask->dst));
884 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
885 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
886 &key->dst, sizeof(key->dst));
888 if (mask->src || mask->dst)
889 *min_inline = MLX5_INLINE_MODE_IP;
892 if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
893 struct flow_dissector_key_ipv6_addrs *key =
894 skb_flow_dissector_target(f->dissector,
895 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
897 struct flow_dissector_key_ipv6_addrs *mask =
898 skb_flow_dissector_target(f->dissector,
899 FLOW_DISSECTOR_KEY_IPV6_ADDRS,
902 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
903 src_ipv4_src_ipv6.ipv6_layout.ipv6),
904 &mask->src, sizeof(mask->src));
905 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
906 src_ipv4_src_ipv6.ipv6_layout.ipv6),
907 &key->src, sizeof(key->src));
909 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
910 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
911 &mask->dst, sizeof(mask->dst));
912 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
913 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
914 &key->dst, sizeof(key->dst));
916 if (ipv6_addr_type(&mask->src) != IPV6_ADDR_ANY ||
917 ipv6_addr_type(&mask->dst) != IPV6_ADDR_ANY)
918 *min_inline = MLX5_INLINE_MODE_IP;
921 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_IP)) {
922 struct flow_dissector_key_ip *key =
923 skb_flow_dissector_target(f->dissector,
924 FLOW_DISSECTOR_KEY_IP,
926 struct flow_dissector_key_ip *mask =
927 skb_flow_dissector_target(f->dissector,
928 FLOW_DISSECTOR_KEY_IP,
931 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn, mask->tos & 0x3);
932 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn, key->tos & 0x3);
934 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp, mask->tos >> 2);
935 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp, key->tos >> 2);
937 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit, mask->ttl);
938 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit, key->ttl);
941 !MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev,
942 ft_field_support.outer_ipv4_ttl))
945 if (mask->tos || mask->ttl)
946 *min_inline = MLX5_INLINE_MODE_IP;
949 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
950 struct flow_dissector_key_ports *key =
951 skb_flow_dissector_target(f->dissector,
952 FLOW_DISSECTOR_KEY_PORTS,
954 struct flow_dissector_key_ports *mask =
955 skb_flow_dissector_target(f->dissector,
956 FLOW_DISSECTOR_KEY_PORTS,
960 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
961 tcp_sport, ntohs(mask->src));
962 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
963 tcp_sport, ntohs(key->src));
965 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
966 tcp_dport, ntohs(mask->dst));
967 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
968 tcp_dport, ntohs(key->dst));
972 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
973 udp_sport, ntohs(mask->src));
974 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
975 udp_sport, ntohs(key->src));
977 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
978 udp_dport, ntohs(mask->dst));
979 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
980 udp_dport, ntohs(key->dst));
983 netdev_err(priv->netdev,
984 "Only UDP and TCP transport are supported\n");
988 if (mask->src || mask->dst)
989 *min_inline = MLX5_INLINE_MODE_TCP_UDP;
992 if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_TCP)) {
993 struct flow_dissector_key_tcp *key =
994 skb_flow_dissector_target(f->dissector,
995 FLOW_DISSECTOR_KEY_TCP,
997 struct flow_dissector_key_tcp *mask =
998 skb_flow_dissector_target(f->dissector,
999 FLOW_DISSECTOR_KEY_TCP,
1002 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_flags,
1003 ntohs(mask->flags));
1004 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_flags,
1008 *min_inline = MLX5_INLINE_MODE_TCP_UDP;
1014 static int parse_cls_flower(struct mlx5e_priv *priv,
1015 struct mlx5e_tc_flow *flow,
1016 struct mlx5_flow_spec *spec,
1017 struct tc_cls_flower_offload *f)
1019 struct mlx5_core_dev *dev = priv->mdev;
1020 struct mlx5_eswitch *esw = dev->priv.eswitch;
1021 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1022 struct mlx5_eswitch_rep *rep;
1026 err = __parse_cls_flower(priv, spec, f, &min_inline);
1028 if (!err && (flow->flags & MLX5E_TC_FLOW_ESWITCH)) {
1030 if (rep->vport != FDB_UPLINK_VPORT &&
1031 (esw->offloads.inline_mode != MLX5_INLINE_MODE_NONE &&
1032 esw->offloads.inline_mode < min_inline)) {
1033 netdev_warn(priv->netdev,
1034 "Flow is not offloaded due to min inline setting, required %d actual %d\n",
1035 min_inline, esw->offloads.inline_mode);
1043 struct pedit_headers {
1051 static int pedit_header_offsets[] = {
1052 [TCA_PEDIT_KEY_EX_HDR_TYPE_ETH] = offsetof(struct pedit_headers, eth),
1053 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP4] = offsetof(struct pedit_headers, ip4),
1054 [TCA_PEDIT_KEY_EX_HDR_TYPE_IP6] = offsetof(struct pedit_headers, ip6),
1055 [TCA_PEDIT_KEY_EX_HDR_TYPE_TCP] = offsetof(struct pedit_headers, tcp),
1056 [TCA_PEDIT_KEY_EX_HDR_TYPE_UDP] = offsetof(struct pedit_headers, udp),
1059 #define pedit_header(_ph, _htype) ((void *)(_ph) + pedit_header_offsets[_htype])
1061 static int set_pedit_val(u8 hdr_type, u32 mask, u32 val, u32 offset,
1062 struct pedit_headers *masks,
1063 struct pedit_headers *vals)
1065 u32 *curr_pmask, *curr_pval;
1067 if (hdr_type >= __PEDIT_HDR_TYPE_MAX)
1070 curr_pmask = (u32 *)(pedit_header(masks, hdr_type) + offset);
1071 curr_pval = (u32 *)(pedit_header(vals, hdr_type) + offset);
1073 if (*curr_pmask & mask) /* disallow acting twice on the same location */
1076 *curr_pmask |= mask;
1077 *curr_pval |= (val & mask);
1085 struct mlx5_fields {
1091 #define OFFLOAD(fw_field, size, field, off) \
1092 {MLX5_ACTION_IN_FIELD_OUT_ ## fw_field, size, offsetof(struct pedit_headers, field) + (off)}
1094 static struct mlx5_fields fields[] = {
1095 OFFLOAD(DMAC_47_16, 4, eth.h_dest[0], 0),
1096 OFFLOAD(DMAC_47_16, 4, eth.h_dest[0], 0),
1097 OFFLOAD(DMAC_15_0, 2, eth.h_dest[4], 0),
1098 OFFLOAD(SMAC_47_16, 4, eth.h_source[0], 0),
1099 OFFLOAD(SMAC_15_0, 2, eth.h_source[4], 0),
1100 OFFLOAD(ETHERTYPE, 2, eth.h_proto, 0),
1102 OFFLOAD(IP_TTL, 1, ip4.ttl, 0),
1103 OFFLOAD(SIPV4, 4, ip4.saddr, 0),
1104 OFFLOAD(DIPV4, 4, ip4.daddr, 0),
1106 OFFLOAD(SIPV6_127_96, 4, ip6.saddr.s6_addr32[0], 0),
1107 OFFLOAD(SIPV6_95_64, 4, ip6.saddr.s6_addr32[1], 0),
1108 OFFLOAD(SIPV6_63_32, 4, ip6.saddr.s6_addr32[2], 0),
1109 OFFLOAD(SIPV6_31_0, 4, ip6.saddr.s6_addr32[3], 0),
1110 OFFLOAD(DIPV6_127_96, 4, ip6.daddr.s6_addr32[0], 0),
1111 OFFLOAD(DIPV6_95_64, 4, ip6.daddr.s6_addr32[1], 0),
1112 OFFLOAD(DIPV6_63_32, 4, ip6.daddr.s6_addr32[2], 0),
1113 OFFLOAD(DIPV6_31_0, 4, ip6.daddr.s6_addr32[3], 0),
1114 OFFLOAD(IPV6_HOPLIMIT, 1, ip6.hop_limit, 0),
1116 OFFLOAD(TCP_SPORT, 2, tcp.source, 0),
1117 OFFLOAD(TCP_DPORT, 2, tcp.dest, 0),
1118 OFFLOAD(TCP_FLAGS, 1, tcp.ack_seq, 5),
1120 OFFLOAD(UDP_SPORT, 2, udp.source, 0),
1121 OFFLOAD(UDP_DPORT, 2, udp.dest, 0),
1124 /* On input attr->num_mod_hdr_actions tells how many HW actions can be parsed at
1125 * max from the SW pedit action. On success, it says how many HW actions were
1128 static int offload_pedit_fields(struct pedit_headers *masks,
1129 struct pedit_headers *vals,
1130 struct mlx5e_tc_flow_parse_attr *parse_attr)
1132 struct pedit_headers *set_masks, *add_masks, *set_vals, *add_vals;
1133 int i, action_size, nactions, max_actions, first, last, next_z;
1134 void *s_masks_p, *a_masks_p, *vals_p;
1135 struct mlx5_fields *f;
1136 u8 cmd, field_bsize;
1143 set_masks = &masks[TCA_PEDIT_KEY_EX_CMD_SET];
1144 add_masks = &masks[TCA_PEDIT_KEY_EX_CMD_ADD];
1145 set_vals = &vals[TCA_PEDIT_KEY_EX_CMD_SET];
1146 add_vals = &vals[TCA_PEDIT_KEY_EX_CMD_ADD];
1148 action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1149 action = parse_attr->mod_hdr_actions;
1150 max_actions = parse_attr->num_mod_hdr_actions;
1153 for (i = 0; i < ARRAY_SIZE(fields); i++) {
1155 /* avoid seeing bits set from previous iterations */
1159 s_masks_p = (void *)set_masks + f->offset;
1160 a_masks_p = (void *)add_masks + f->offset;
1162 memcpy(&s_mask, s_masks_p, f->size);
1163 memcpy(&a_mask, a_masks_p, f->size);
1165 if (!s_mask && !a_mask) /* nothing to offload here */
1168 if (s_mask && a_mask) {
1169 printk(KERN_WARNING "mlx5: can't set and add to the same HW field (%x)\n", f->field);
1173 if (nactions == max_actions) {
1174 printk(KERN_WARNING "mlx5: parsed %d pedit actions, can't do more\n", nactions);
1179 cmd = MLX5_ACTION_TYPE_SET;
1181 vals_p = (void *)set_vals + f->offset;
1182 /* clear to denote we consumed this field */
1183 memset(s_masks_p, 0, f->size);
1185 cmd = MLX5_ACTION_TYPE_ADD;
1187 vals_p = (void *)add_vals + f->offset;
1188 /* clear to denote we consumed this field */
1189 memset(a_masks_p, 0, f->size);
1192 field_bsize = f->size * BITS_PER_BYTE;
1194 if (field_bsize == 32) {
1195 mask_be32 = *(__be32 *)&mask;
1196 mask = (__force unsigned long)cpu_to_le32(be32_to_cpu(mask_be32));
1197 } else if (field_bsize == 16) {
1198 mask_be16 = *(__be16 *)&mask;
1199 mask = (__force unsigned long)cpu_to_le16(be16_to_cpu(mask_be16));
1202 first = find_first_bit(&mask, field_bsize);
1203 next_z = find_next_zero_bit(&mask, field_bsize, first);
1204 last = find_last_bit(&mask, field_bsize);
1205 if (first < next_z && next_z < last) {
1206 printk(KERN_WARNING "mlx5: rewrite of few sub-fields (mask %lx) isn't offloaded\n",
1211 MLX5_SET(set_action_in, action, action_type, cmd);
1212 MLX5_SET(set_action_in, action, field, f->field);
1214 if (cmd == MLX5_ACTION_TYPE_SET) {
1215 MLX5_SET(set_action_in, action, offset, first);
1216 /* length is num of bits to be written, zero means length of 32 */
1217 MLX5_SET(set_action_in, action, length, (last - first + 1));
1220 if (field_bsize == 32)
1221 MLX5_SET(set_action_in, action, data, ntohl(*(__be32 *)vals_p) >> first);
1222 else if (field_bsize == 16)
1223 MLX5_SET(set_action_in, action, data, ntohs(*(__be16 *)vals_p) >> first);
1224 else if (field_bsize == 8)
1225 MLX5_SET(set_action_in, action, data, *(u8 *)vals_p >> first);
1227 action += action_size;
1231 parse_attr->num_mod_hdr_actions = nactions;
1235 static int alloc_mod_hdr_actions(struct mlx5e_priv *priv,
1236 const struct tc_action *a, int namespace,
1237 struct mlx5e_tc_flow_parse_attr *parse_attr)
1239 int nkeys, action_size, max_actions;
1241 nkeys = tcf_pedit_nkeys(a);
1242 action_size = MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto);
1244 if (namespace == MLX5_FLOW_NAMESPACE_FDB) /* FDB offloading */
1245 max_actions = MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, max_modify_header_actions);
1246 else /* namespace is MLX5_FLOW_NAMESPACE_KERNEL - NIC offloading */
1247 max_actions = MLX5_CAP_FLOWTABLE_NIC_RX(priv->mdev, max_modify_header_actions);
1249 /* can get up to crazingly 16 HW actions in 32 bits pedit SW key */
1250 max_actions = min(max_actions, nkeys * 16);
1252 parse_attr->mod_hdr_actions = kcalloc(max_actions, action_size, GFP_KERNEL);
1253 if (!parse_attr->mod_hdr_actions)
1256 parse_attr->num_mod_hdr_actions = max_actions;
1260 static const struct pedit_headers zero_masks = {};
1262 static int parse_tc_pedit_action(struct mlx5e_priv *priv,
1263 const struct tc_action *a, int namespace,
1264 struct mlx5e_tc_flow_parse_attr *parse_attr)
1266 struct pedit_headers masks[__PEDIT_CMD_MAX], vals[__PEDIT_CMD_MAX], *cmd_masks;
1267 int nkeys, i, err = -EOPNOTSUPP;
1268 u32 mask, val, offset;
1271 nkeys = tcf_pedit_nkeys(a);
1273 memset(masks, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1274 memset(vals, 0, sizeof(struct pedit_headers) * __PEDIT_CMD_MAX);
1276 for (i = 0; i < nkeys; i++) {
1277 htype = tcf_pedit_htype(a, i);
1278 cmd = tcf_pedit_cmd(a, i);
1279 err = -EOPNOTSUPP; /* can't be all optimistic */
1281 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK) {
1282 printk(KERN_WARNING "mlx5: legacy pedit isn't offloaded\n");
1286 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET && cmd != TCA_PEDIT_KEY_EX_CMD_ADD) {
1287 printk(KERN_WARNING "mlx5: pedit cmd %d isn't offloaded\n", cmd);
1291 mask = tcf_pedit_mask(a, i);
1292 val = tcf_pedit_val(a, i);
1293 offset = tcf_pedit_offset(a, i);
1295 err = set_pedit_val(htype, ~mask, val, offset, &masks[cmd], &vals[cmd]);
1300 err = alloc_mod_hdr_actions(priv, a, namespace, parse_attr);
1304 err = offload_pedit_fields(masks, vals, parse_attr);
1306 goto out_dealloc_parsed_actions;
1308 for (cmd = 0; cmd < __PEDIT_CMD_MAX; cmd++) {
1309 cmd_masks = &masks[cmd];
1310 if (memcmp(cmd_masks, &zero_masks, sizeof(zero_masks))) {
1311 printk(KERN_WARNING "mlx5: attempt to offload an unsupported field (cmd %d)\n",
1313 print_hex_dump(KERN_WARNING, "mask: ", DUMP_PREFIX_ADDRESS,
1314 16, 1, cmd_masks, sizeof(zero_masks), true);
1316 goto out_dealloc_parsed_actions;
1322 out_dealloc_parsed_actions:
1323 kfree(parse_attr->mod_hdr_actions);
1328 static bool csum_offload_supported(struct mlx5e_priv *priv, u32 action, u32 update_flags)
1330 u32 prot_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR | TCA_CSUM_UPDATE_FLAG_TCP |
1331 TCA_CSUM_UPDATE_FLAG_UDP;
1333 /* The HW recalcs checksums only if re-writing headers */
1334 if (!(action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)) {
1335 netdev_warn(priv->netdev,
1336 "TC csum action is only offloaded with pedit\n");
1340 if (update_flags & ~prot_flags) {
1341 netdev_warn(priv->netdev,
1342 "can't offload TC csum action for some header/s - flags %#x\n",
1350 static bool modify_header_match_supported(struct mlx5_flow_spec *spec,
1351 struct tcf_exts *exts)
1353 const struct tc_action *a;
1354 bool modify_ip_header;
1361 headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, outer_headers);
1362 ethertype = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ethertype);
1364 /* for non-IP we only re-write MACs, so we're okay */
1365 if (ethertype != ETH_P_IP && ethertype != ETH_P_IPV6)
1368 modify_ip_header = false;
1369 tcf_exts_to_list(exts, &actions);
1370 list_for_each_entry(a, &actions, list) {
1371 if (!is_tcf_pedit(a))
1374 nkeys = tcf_pedit_nkeys(a);
1375 for (i = 0; i < nkeys; i++) {
1376 htype = tcf_pedit_htype(a, i);
1377 if (htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP4 ||
1378 htype == TCA_PEDIT_KEY_EX_HDR_TYPE_IP6) {
1379 modify_ip_header = true;
1385 ip_proto = MLX5_GET(fte_match_set_lyr_2_4, headers_v, ip_protocol);
1386 if (modify_ip_header && ip_proto != IPPROTO_TCP && ip_proto != IPPROTO_UDP) {
1387 pr_info("can't offload re-write of ip proto %d\n", ip_proto);
1395 static bool actions_match_supported(struct mlx5e_priv *priv,
1396 struct tcf_exts *exts,
1397 struct mlx5e_tc_flow_parse_attr *parse_attr,
1398 struct mlx5e_tc_flow *flow)
1402 if (flow->flags & MLX5E_TC_FLOW_ESWITCH)
1403 actions = flow->esw_attr->action;
1405 actions = flow->nic_attr->action;
1407 if (actions & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
1408 return modify_header_match_supported(&parse_attr->spec, exts);
1413 static int parse_tc_nic_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1414 struct mlx5e_tc_flow_parse_attr *parse_attr,
1415 struct mlx5e_tc_flow *flow)
1417 struct mlx5_nic_flow_attr *attr = flow->nic_attr;
1418 const struct tc_action *a;
1422 if (!tcf_exts_has_actions(exts))
1425 attr->flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
1428 tcf_exts_to_list(exts, &actions);
1429 list_for_each_entry(a, &actions, list) {
1430 if (is_tcf_gact_shot(a)) {
1431 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
1432 if (MLX5_CAP_FLOWTABLE(priv->mdev,
1433 flow_table_properties_nic_receive.flow_counter))
1434 attr->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
1438 if (is_tcf_pedit(a)) {
1439 err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_KERNEL,
1444 attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR |
1445 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1449 if (is_tcf_csum(a)) {
1450 if (csum_offload_supported(priv, attr->action,
1451 tcf_csum_update_flags(a)))
1457 if (is_tcf_skbedit_mark(a)) {
1458 u32 mark = tcf_skbedit_mark(a);
1460 if (mark & ~MLX5E_TC_FLOW_ID_MASK) {
1461 netdev_warn(priv->netdev, "Bad flow mark - only 16 bit is supported: 0x%x\n",
1466 attr->flow_tag = mark;
1467 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1474 if (!actions_match_supported(priv, exts, parse_attr, flow))
1480 static inline int cmp_encap_info(struct ip_tunnel_key *a,
1481 struct ip_tunnel_key *b)
1483 return memcmp(a, b, sizeof(*a));
1486 static inline int hash_encap_info(struct ip_tunnel_key *key)
1488 return jhash(key, sizeof(*key), 0);
1491 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
1492 struct net_device *mirred_dev,
1493 struct net_device **out_dev,
1495 struct neighbour **out_n,
1498 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1500 struct neighbour *n = NULL;
1502 #if IS_ENABLED(CONFIG_INET)
1505 rt = ip_route_output_key(dev_net(mirred_dev), fl4);
1506 ret = PTR_ERR_OR_ZERO(rt);
1512 /* if the egress device isn't on the same HW e-switch, we use the uplink */
1513 if (!switchdev_port_same_parent_id(priv->netdev, rt->dst.dev))
1514 *out_dev = mlx5_eswitch_get_uplink_netdev(esw);
1516 *out_dev = rt->dst.dev;
1518 *out_ttl = ip4_dst_hoplimit(&rt->dst);
1519 n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
1528 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
1529 struct net_device *mirred_dev,
1530 struct net_device **out_dev,
1532 struct neighbour **out_n,
1535 struct neighbour *n = NULL;
1536 struct dst_entry *dst;
1538 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
1539 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1542 ret = ipv6_stub->ipv6_dst_lookup(dev_net(mirred_dev), NULL, &dst,
1547 *out_ttl = ip6_dst_hoplimit(dst);
1549 /* if the egress device isn't on the same HW e-switch, we use the uplink */
1550 if (!switchdev_port_same_parent_id(priv->netdev, dst->dev))
1551 *out_dev = mlx5_eswitch_get_uplink_netdev(esw);
1553 *out_dev = dst->dev;
1558 n = dst_neigh_lookup(dst, &fl6->daddr);
1567 static void gen_vxlan_header_ipv4(struct net_device *out_dev,
1568 char buf[], int encap_size,
1569 unsigned char h_dest[ETH_ALEN],
1573 __be16 udp_dst_port,
1576 struct ethhdr *eth = (struct ethhdr *)buf;
1577 struct iphdr *ip = (struct iphdr *)((char *)eth + sizeof(struct ethhdr));
1578 struct udphdr *udp = (struct udphdr *)((char *)ip + sizeof(struct iphdr));
1579 struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1581 memset(buf, 0, encap_size);
1583 ether_addr_copy(eth->h_dest, h_dest);
1584 ether_addr_copy(eth->h_source, out_dev->dev_addr);
1585 eth->h_proto = htons(ETH_P_IP);
1591 ip->protocol = IPPROTO_UDP;
1595 udp->dest = udp_dst_port;
1596 vxh->vx_flags = VXLAN_HF_VNI;
1597 vxh->vx_vni = vxlan_vni_field(vx_vni);
1600 static void gen_vxlan_header_ipv6(struct net_device *out_dev,
1601 char buf[], int encap_size,
1602 unsigned char h_dest[ETH_ALEN],
1604 struct in6_addr *daddr,
1605 struct in6_addr *saddr,
1606 __be16 udp_dst_port,
1609 struct ethhdr *eth = (struct ethhdr *)buf;
1610 struct ipv6hdr *ip6h = (struct ipv6hdr *)((char *)eth + sizeof(struct ethhdr));
1611 struct udphdr *udp = (struct udphdr *)((char *)ip6h + sizeof(struct ipv6hdr));
1612 struct vxlanhdr *vxh = (struct vxlanhdr *)((char *)udp + sizeof(struct udphdr));
1614 memset(buf, 0, encap_size);
1616 ether_addr_copy(eth->h_dest, h_dest);
1617 ether_addr_copy(eth->h_source, out_dev->dev_addr);
1618 eth->h_proto = htons(ETH_P_IPV6);
1620 ip6_flow_hdr(ip6h, 0, 0);
1621 /* the HW fills up ipv6 payload len */
1622 ip6h->nexthdr = IPPROTO_UDP;
1623 ip6h->hop_limit = ttl;
1624 ip6h->daddr = *daddr;
1625 ip6h->saddr = *saddr;
1627 udp->dest = udp_dst_port;
1628 vxh->vx_flags = VXLAN_HF_VNI;
1629 vxh->vx_vni = vxlan_vni_field(vx_vni);
1632 static int mlx5e_create_encap_header_ipv4(struct mlx5e_priv *priv,
1633 struct net_device *mirred_dev,
1634 struct mlx5e_encap_entry *e)
1636 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1637 int ipv4_encap_size = ETH_HLEN + sizeof(struct iphdr) + VXLAN_HLEN;
1638 struct ip_tunnel_key *tun_key = &e->tun_info.key;
1639 struct net_device *out_dev;
1640 struct neighbour *n = NULL;
1641 struct flowi4 fl4 = {};
1646 if (max_encap_size < ipv4_encap_size) {
1647 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1648 ipv4_encap_size, max_encap_size);
1652 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
1656 switch (e->tunnel_type) {
1657 case MLX5_HEADER_TYPE_VXLAN:
1658 fl4.flowi4_proto = IPPROTO_UDP;
1659 fl4.fl4_dport = tun_key->tp_dst;
1665 fl4.flowi4_tos = tun_key->tos;
1666 fl4.daddr = tun_key->u.ipv4.dst;
1667 fl4.saddr = tun_key->u.ipv4.src;
1669 err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev,
1674 /* used by mlx5e_detach_encap to lookup a neigh hash table
1675 * entry in the neigh hash table when a user deletes a rule
1677 e->m_neigh.dev = n->dev;
1678 e->m_neigh.family = n->ops->family;
1679 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1680 e->out_dev = out_dev;
1682 /* It's importent to add the neigh to the hash table before checking
1683 * the neigh validity state. So if we'll get a notification, in case the
1684 * neigh changes it's validity state, we would find the relevant neigh
1687 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1691 read_lock_bh(&n->lock);
1692 nud_state = n->nud_state;
1693 ether_addr_copy(e->h_dest, n->ha);
1694 read_unlock_bh(&n->lock);
1696 switch (e->tunnel_type) {
1697 case MLX5_HEADER_TYPE_VXLAN:
1698 gen_vxlan_header_ipv4(out_dev, encap_header,
1699 ipv4_encap_size, e->h_dest, ttl,
1701 fl4.saddr, tun_key->tp_dst,
1702 tunnel_id_to_key32(tun_key->tun_id));
1706 goto destroy_neigh_entry;
1708 e->encap_size = ipv4_encap_size;
1709 e->encap_header = encap_header;
1711 if (!(nud_state & NUD_VALID)) {
1712 neigh_event_send(n, NULL);
1717 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1718 ipv4_encap_size, encap_header, &e->encap_id);
1720 goto destroy_neigh_entry;
1722 e->flags |= MLX5_ENCAP_ENTRY_VALID;
1723 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1727 destroy_neigh_entry:
1728 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1730 kfree(encap_header);
1737 static int mlx5e_create_encap_header_ipv6(struct mlx5e_priv *priv,
1738 struct net_device *mirred_dev,
1739 struct mlx5e_encap_entry *e)
1741 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
1742 int ipv6_encap_size = ETH_HLEN + sizeof(struct ipv6hdr) + VXLAN_HLEN;
1743 struct ip_tunnel_key *tun_key = &e->tun_info.key;
1744 struct net_device *out_dev;
1745 struct neighbour *n = NULL;
1746 struct flowi6 fl6 = {};
1751 if (max_encap_size < ipv6_encap_size) {
1752 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
1753 ipv6_encap_size, max_encap_size);
1757 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
1761 switch (e->tunnel_type) {
1762 case MLX5_HEADER_TYPE_VXLAN:
1763 fl6.flowi6_proto = IPPROTO_UDP;
1764 fl6.fl6_dport = tun_key->tp_dst;
1771 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
1772 fl6.daddr = tun_key->u.ipv6.dst;
1773 fl6.saddr = tun_key->u.ipv6.src;
1775 err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev,
1780 /* used by mlx5e_detach_encap to lookup a neigh hash table
1781 * entry in the neigh hash table when a user deletes a rule
1783 e->m_neigh.dev = n->dev;
1784 e->m_neigh.family = n->ops->family;
1785 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
1786 e->out_dev = out_dev;
1788 /* It's importent to add the neigh to the hash table before checking
1789 * the neigh validity state. So if we'll get a notification, in case the
1790 * neigh changes it's validity state, we would find the relevant neigh
1793 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
1797 read_lock_bh(&n->lock);
1798 nud_state = n->nud_state;
1799 ether_addr_copy(e->h_dest, n->ha);
1800 read_unlock_bh(&n->lock);
1802 switch (e->tunnel_type) {
1803 case MLX5_HEADER_TYPE_VXLAN:
1804 gen_vxlan_header_ipv6(out_dev, encap_header,
1805 ipv6_encap_size, e->h_dest, ttl,
1807 &fl6.saddr, tun_key->tp_dst,
1808 tunnel_id_to_key32(tun_key->tun_id));
1812 goto destroy_neigh_entry;
1815 e->encap_size = ipv6_encap_size;
1816 e->encap_header = encap_header;
1818 if (!(nud_state & NUD_VALID)) {
1819 neigh_event_send(n, NULL);
1824 err = mlx5_encap_alloc(priv->mdev, e->tunnel_type,
1825 ipv6_encap_size, encap_header, &e->encap_id);
1827 goto destroy_neigh_entry;
1829 e->flags |= MLX5_ENCAP_ENTRY_VALID;
1830 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
1834 destroy_neigh_entry:
1835 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
1837 kfree(encap_header);
1844 static int mlx5e_attach_encap(struct mlx5e_priv *priv,
1845 struct ip_tunnel_info *tun_info,
1846 struct net_device *mirred_dev,
1847 struct net_device **encap_dev,
1848 struct mlx5e_tc_flow *flow)
1850 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
1851 struct net_device *up_dev = mlx5_eswitch_get_uplink_netdev(esw);
1852 unsigned short family = ip_tunnel_info_af(tun_info);
1853 struct mlx5e_priv *up_priv = netdev_priv(up_dev);
1854 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1855 struct ip_tunnel_key *key = &tun_info->key;
1856 struct mlx5e_encap_entry *e;
1857 int tunnel_type, err = 0;
1861 /* udp dst port must be set */
1862 if (!memchr_inv(&key->tp_dst, 0, sizeof(key->tp_dst)))
1863 goto vxlan_encap_offload_err;
1865 /* setting udp src port isn't supported */
1866 if (memchr_inv(&key->tp_src, 0, sizeof(key->tp_src))) {
1867 vxlan_encap_offload_err:
1868 netdev_warn(priv->netdev,
1869 "must set udp dst port and not set udp src port\n");
1873 if (mlx5e_vxlan_lookup_port(up_priv, be16_to_cpu(key->tp_dst)) &&
1874 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap)) {
1875 tunnel_type = MLX5_HEADER_TYPE_VXLAN;
1877 netdev_warn(priv->netdev,
1878 "%d isn't an offloaded vxlan udp dport\n", be16_to_cpu(key->tp_dst));
1882 hash_key = hash_encap_info(key);
1884 hash_for_each_possible_rcu(esw->offloads.encap_tbl, e,
1885 encap_hlist, hash_key) {
1886 if (!cmp_encap_info(&e->tun_info.key, key)) {
1892 /* must verify if encap is valid or not */
1896 e = kzalloc(sizeof(*e), GFP_KERNEL);
1900 e->tun_info = *tun_info;
1901 e->tunnel_type = tunnel_type;
1902 INIT_LIST_HEAD(&e->flows);
1904 if (family == AF_INET)
1905 err = mlx5e_create_encap_header_ipv4(priv, mirred_dev, e);
1906 else if (family == AF_INET6)
1907 err = mlx5e_create_encap_header_ipv6(priv, mirred_dev, e);
1909 if (err && err != -EAGAIN)
1912 hash_add_rcu(esw->offloads.encap_tbl, &e->encap_hlist, hash_key);
1915 list_add(&flow->encap, &e->flows);
1916 *encap_dev = e->out_dev;
1917 if (e->flags & MLX5_ENCAP_ENTRY_VALID)
1918 attr->encap_id = e->encap_id;
1929 static int parse_tc_fdb_actions(struct mlx5e_priv *priv, struct tcf_exts *exts,
1930 struct mlx5e_tc_flow_parse_attr *parse_attr,
1931 struct mlx5e_tc_flow *flow)
1933 struct mlx5_esw_flow_attr *attr = flow->esw_attr;
1934 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1935 struct ip_tunnel_info *info = NULL;
1936 const struct tc_action *a;
1941 if (!tcf_exts_has_actions(exts))
1944 memset(attr, 0, sizeof(*attr));
1945 attr->in_rep = rpriv->rep;
1947 tcf_exts_to_list(exts, &actions);
1948 list_for_each_entry(a, &actions, list) {
1949 if (is_tcf_gact_shot(a)) {
1950 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DROP |
1951 MLX5_FLOW_CONTEXT_ACTION_COUNT;
1955 if (is_tcf_pedit(a)) {
1956 err = parse_tc_pedit_action(priv, a, MLX5_FLOW_NAMESPACE_FDB,
1961 attr->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
1965 if (is_tcf_csum(a)) {
1966 if (csum_offload_supported(priv, attr->action,
1967 tcf_csum_update_flags(a)))
1973 if (is_tcf_mirred_egress_redirect(a)) {
1974 int ifindex = tcf_mirred_ifindex(a);
1975 struct net_device *out_dev;
1976 struct mlx5e_priv *out_priv;
1978 out_dev = __dev_get_by_index(dev_net(priv->netdev), ifindex);
1980 if (switchdev_port_same_parent_id(priv->netdev,
1982 attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1983 MLX5_FLOW_CONTEXT_ACTION_COUNT;
1984 out_priv = netdev_priv(out_dev);
1985 rpriv = out_priv->ppriv;
1986 attr->out_rep = rpriv->rep;
1988 parse_attr->mirred_ifindex = ifindex;
1989 parse_attr->tun_info = *info;
1990 attr->parse_attr = parse_attr;
1991 attr->action |= MLX5_FLOW_CONTEXT_ACTION_ENCAP |
1992 MLX5_FLOW_CONTEXT_ACTION_FWD_DEST |
1993 MLX5_FLOW_CONTEXT_ACTION_COUNT;
1994 /* attr->out_rep is resolved when we handle encap */
1996 pr_err("devices %s %s not on same switch HW, can't offload forwarding\n",
1997 priv->netdev->name, out_dev->name);
2003 if (is_tcf_tunnel_set(a)) {
2004 info = tcf_tunnel_info(a);
2012 if (is_tcf_vlan(a)) {
2013 if (tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
2014 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_POP;
2015 } else if (tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
2016 if (tcf_vlan_push_proto(a) != htons(ETH_P_8021Q))
2019 attr->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
2020 attr->vlan = tcf_vlan_push_vid(a);
2021 } else { /* action is TCA_VLAN_ACT_MODIFY */
2027 if (is_tcf_tunnel_release(a)) {
2028 attr->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2035 if (!actions_match_supported(priv, exts, parse_attr, flow))
2041 int mlx5e_configure_flower(struct mlx5e_priv *priv,
2042 struct tc_cls_flower_offload *f)
2044 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
2045 struct mlx5e_tc_flow_parse_attr *parse_attr;
2046 struct mlx5e_tc_table *tc = &priv->fs.tc;
2047 struct mlx5e_tc_flow *flow;
2048 int attr_size, err = 0;
2051 if (esw && esw->mode == SRIOV_OFFLOADS) {
2052 flow_flags = MLX5E_TC_FLOW_ESWITCH;
2053 attr_size = sizeof(struct mlx5_esw_flow_attr);
2055 flow_flags = MLX5E_TC_FLOW_NIC;
2056 attr_size = sizeof(struct mlx5_nic_flow_attr);
2059 flow = kzalloc(sizeof(*flow) + attr_size, GFP_KERNEL);
2060 parse_attr = kvzalloc(sizeof(*parse_attr), GFP_KERNEL);
2061 if (!parse_attr || !flow) {
2066 flow->cookie = f->cookie;
2067 flow->flags = flow_flags;
2069 err = parse_cls_flower(priv, flow, &parse_attr->spec, f);
2073 if (flow->flags & MLX5E_TC_FLOW_ESWITCH) {
2074 err = parse_tc_fdb_actions(priv, f->exts, parse_attr, flow);
2077 flow->rule = mlx5e_tc_add_fdb_flow(priv, parse_attr, flow);
2079 err = parse_tc_nic_actions(priv, f->exts, parse_attr, flow);
2082 flow->rule = mlx5e_tc_add_nic_flow(priv, parse_attr, flow);
2085 if (IS_ERR(flow->rule)) {
2086 err = PTR_ERR(flow->rule);
2092 flow->flags |= MLX5E_TC_FLOW_OFFLOADED;
2094 err = rhashtable_insert_fast(&tc->ht, &flow->node,
2099 if (flow->flags & MLX5E_TC_FLOW_ESWITCH &&
2100 !(flow->esw_attr->action & MLX5_FLOW_CONTEXT_ACTION_ENCAP))
2105 mlx5e_tc_del_flow(priv, flow);
2113 int mlx5e_delete_flower(struct mlx5e_priv *priv,
2114 struct tc_cls_flower_offload *f)
2116 struct mlx5e_tc_flow *flow;
2117 struct mlx5e_tc_table *tc = &priv->fs.tc;
2119 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2124 rhashtable_remove_fast(&tc->ht, &flow->node, tc->ht_params);
2126 mlx5e_tc_del_flow(priv, flow);
2133 int mlx5e_stats_flower(struct mlx5e_priv *priv,
2134 struct tc_cls_flower_offload *f)
2136 struct mlx5e_tc_table *tc = &priv->fs.tc;
2137 struct mlx5e_tc_flow *flow;
2138 struct mlx5_fc *counter;
2143 flow = rhashtable_lookup_fast(&tc->ht, &f->cookie,
2148 if (!(flow->flags & MLX5E_TC_FLOW_OFFLOADED))
2151 counter = mlx5_flow_rule_counter(flow->rule);
2155 mlx5_fc_query_cached(counter, &bytes, &packets, &lastuse);
2157 tcf_exts_stats_update(f->exts, bytes, packets, lastuse);
2162 static const struct rhashtable_params mlx5e_tc_flow_ht_params = {
2163 .head_offset = offsetof(struct mlx5e_tc_flow, node),
2164 .key_offset = offsetof(struct mlx5e_tc_flow, cookie),
2165 .key_len = sizeof(((struct mlx5e_tc_flow *)0)->cookie),
2166 .automatic_shrinking = true,
2169 int mlx5e_tc_init(struct mlx5e_priv *priv)
2171 struct mlx5e_tc_table *tc = &priv->fs.tc;
2173 hash_init(tc->mod_hdr_tbl);
2175 tc->ht_params = mlx5e_tc_flow_ht_params;
2176 return rhashtable_init(&tc->ht, &tc->ht_params);
2179 static void _mlx5e_tc_del_flow(void *ptr, void *arg)
2181 struct mlx5e_tc_flow *flow = ptr;
2182 struct mlx5e_priv *priv = arg;
2184 mlx5e_tc_del_flow(priv, flow);
2188 void mlx5e_tc_cleanup(struct mlx5e_priv *priv)
2190 struct mlx5e_tc_table *tc = &priv->fs.tc;
2192 rhashtable_free_and_destroy(&tc->ht, _mlx5e_tc_del_flow, priv);
2194 if (!IS_ERR_OR_NULL(tc->t)) {
2195 mlx5_destroy_flow_table(tc->t);