1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 
4 #include <net/bareudp.h>
5 #include "act.h"
6 #include "en/tc_priv.h"
7 
8 static bool
9 tc_act_can_offload_mpls_push(struct mlx5e_tc_act_parse_state *parse_state,
10 			     const struct flow_action_entry *act,
11 			     int act_index)
12 {
13 	struct netlink_ext_ack *extack = parse_state->extack;
14 	struct mlx5e_priv *priv = parse_state->flow->priv;
15 
16 	if (!MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, reformat_l2_to_l3_tunnel) ||
17 	    act->mpls_push.proto != htons(ETH_P_MPLS_UC)) {
18 		NL_SET_ERR_MSG_MOD(extack, "mpls push is supported only for mpls_uc protocol");
19 		return false;
20 	}
21 
22 	return true;
23 }
24 
25 static int
26 tc_act_parse_mpls_push(struct mlx5e_tc_act_parse_state *parse_state,
27 		       const struct flow_action_entry *act,
28 		       struct mlx5e_priv *priv,
29 		       struct mlx5_flow_attr *attr)
30 {
31 	parse_state->mpls_push = true;
32 
33 	return 0;
34 }
35 
36 static bool
37 tc_act_can_offload_mpls_pop(struct mlx5e_tc_act_parse_state *parse_state,
38 			    const struct flow_action_entry *act,
39 			    int act_index)
40 {
41 	struct netlink_ext_ack *extack = parse_state->extack;
42 	struct mlx5e_tc_flow *flow = parse_state->flow;
43 	struct net_device *filter_dev;
44 
45 	filter_dev = flow->attr->parse_attr->filter_dev;
46 
47 	/* we only support mpls pop if it is the first action
48 	 * and the filter net device is bareudp. Subsequent
49 	 * actions can be pedit and the last can be mirred
50 	 * egress redirect.
51 	 */
52 	if (act_index) {
53 		NL_SET_ERR_MSG_MOD(extack, "mpls pop supported only as first action");
54 		return false;
55 	}
56 
57 	if (!netif_is_bareudp(filter_dev)) {
58 		NL_SET_ERR_MSG_MOD(extack, "mpls pop supported only on bareudp devices");
59 		return false;
60 	}
61 
62 	return true;
63 }
64 
65 static int
66 tc_act_parse_mpls_pop(struct mlx5e_tc_act_parse_state *parse_state,
67 		      const struct flow_action_entry *act,
68 		      struct mlx5e_priv *priv,
69 		      struct mlx5_flow_attr *attr)
70 {
71 	attr->parse_attr->eth.h_proto = act->mpls_pop.proto;
72 	attr->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
73 	flow_flag_set(parse_state->flow, L3_TO_L2_DECAP);
74 
75 	return 0;
76 }
77 
78 struct mlx5e_tc_act mlx5e_tc_act_mpls_push = {
79 	.can_offload = tc_act_can_offload_mpls_push,
80 	.parse_action = tc_act_parse_mpls_push,
81 };
82 
83 struct mlx5e_tc_act mlx5e_tc_act_mpls_pop = {
84 	.can_offload = tc_act_can_offload_mpls_pop,
85 	.parse_action = tc_act_parse_mpls_pop,
86 };
87