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
8static bool
9tc_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			     struct mlx5_flow_attr *attr)
13{
14	struct netlink_ext_ack *extack = parse_state->extack;
15	struct mlx5e_priv *priv = parse_state->flow->priv;
16
17	if (!MLX5_CAP_ESW_FLOWTABLE_FDB(priv->mdev, reformat_l2_to_l3_tunnel) ||
18	    act->mpls_push.proto != htons(ETH_P_MPLS_UC)) {
19		NL_SET_ERR_MSG_MOD(extack, "mpls push is supported only for mpls_uc protocol");
20		return false;
21	}
22
23	return true;
24}
25
26static void
27copy_mpls_info(struct mlx5e_mpls_info *mpls_info,
28	       const struct flow_action_entry *act)
29{
30	mpls_info->label = act->mpls_push.label;
31	mpls_info->tc = act->mpls_push.tc;
32	mpls_info->bos = act->mpls_push.bos;
33	mpls_info->ttl = act->mpls_push.ttl;
34}
35
36static int
37tc_act_parse_mpls_push(struct mlx5e_tc_act_parse_state *parse_state,
38		       const struct flow_action_entry *act,
39		       struct mlx5e_priv *priv,
40		       struct mlx5_flow_attr *attr)
41{
42	parse_state->mpls_push = true;
43	copy_mpls_info(&parse_state->mpls_info, act);
44
45	return 0;
46}
47
48static bool
49tc_act_can_offload_mpls_pop(struct mlx5e_tc_act_parse_state *parse_state,
50			    const struct flow_action_entry *act,
51			    int act_index,
52			    struct mlx5_flow_attr *attr)
53{
54	struct netlink_ext_ack *extack = parse_state->extack;
55	struct net_device *filter_dev;
56
57	filter_dev = attr->parse_attr->filter_dev;
58
59	/* we only support mpls pop if it is the first action
60	 * or it is second action after tunnel key unset
61	 * and the filter net device is bareudp. Subsequent
62	 * actions can be pedit and the last can be mirred
63	 * egress redirect.
64	 */
65	if ((act_index == 1 && !parse_state->decap) || act_index > 1) {
66		NL_SET_ERR_MSG_MOD(extack, "mpls pop supported only as first action or with decap");
67		return false;
68	}
69
70	if (!netif_is_bareudp(filter_dev)) {
71		NL_SET_ERR_MSG_MOD(extack, "mpls pop supported only on bareudp devices");
72		return false;
73	}
74
75	return true;
76}
77
78static int
79tc_act_parse_mpls_pop(struct mlx5e_tc_act_parse_state *parse_state,
80		      const struct flow_action_entry *act,
81		      struct mlx5e_priv *priv,
82		      struct mlx5_flow_attr *attr)
83{
84	attr->esw_attr->eth.h_proto = act->mpls_pop.proto;
85	attr->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
86	flow_flag_set(parse_state->flow, L3_TO_L2_DECAP);
87
88	return 0;
89}
90
91struct mlx5e_tc_act mlx5e_tc_act_mpls_push = {
92	.can_offload = tc_act_can_offload_mpls_push,
93	.parse_action = tc_act_parse_mpls_push,
94};
95
96struct mlx5e_tc_act mlx5e_tc_act_mpls_pop = {
97	.can_offload = tc_act_can_offload_mpls_pop,
98	.parse_action = tc_act_parse_mpls_pop,
99};
100