1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/bitfield.h>
35 #include <net/pkt_cls.h>
36 #include <net/switchdev.h>
37 #include <net/tc_act/tc_gact.h>
38 #include <net/tc_act/tc_mirred.h>
39 #include <net/tc_act/tc_vlan.h>
40 
41 #include "cmsg.h"
42 #include "main.h"
43 #include "../nfp_net_repr.h"
44 
45 static void nfp_fl_pop_vlan(struct nfp_fl_pop_vlan *pop_vlan)
46 {
47 	size_t act_size = sizeof(struct nfp_fl_pop_vlan);
48 	u16 tmp_pop_vlan_op;
49 
50 	tmp_pop_vlan_op =
51 		FIELD_PREP(NFP_FL_ACT_LEN_LW, act_size >> NFP_FL_LW_SIZ) |
52 		FIELD_PREP(NFP_FL_ACT_JMP_ID, NFP_FL_ACTION_OPCODE_POP_VLAN);
53 
54 	pop_vlan->a_op = cpu_to_be16(tmp_pop_vlan_op);
55 	pop_vlan->reserved = 0;
56 }
57 
58 static void
59 nfp_fl_push_vlan(struct nfp_fl_push_vlan *push_vlan,
60 		 const struct tc_action *action)
61 {
62 	size_t act_size = sizeof(struct nfp_fl_push_vlan);
63 	struct tcf_vlan *vlan = to_vlan(action);
64 	u16 tmp_push_vlan_tci;
65 	u16 tmp_push_vlan_op;
66 
67 	tmp_push_vlan_op =
68 		FIELD_PREP(NFP_FL_ACT_LEN_LW, act_size >> NFP_FL_LW_SIZ) |
69 		FIELD_PREP(NFP_FL_ACT_JMP_ID, NFP_FL_ACTION_OPCODE_PUSH_VLAN);
70 
71 	push_vlan->a_op = cpu_to_be16(tmp_push_vlan_op);
72 	/* Set action push vlan parameters. */
73 	push_vlan->reserved = 0;
74 	push_vlan->vlan_tpid = tcf_vlan_push_proto(action);
75 
76 	tmp_push_vlan_tci =
77 		FIELD_PREP(NFP_FL_PUSH_VLAN_PRIO, vlan->tcfv_push_prio) |
78 		FIELD_PREP(NFP_FL_PUSH_VLAN_VID, vlan->tcfv_push_vid) |
79 		NFP_FL_PUSH_VLAN_CFI;
80 	push_vlan->vlan_tci = cpu_to_be16(tmp_push_vlan_tci);
81 }
82 
83 static int
84 nfp_fl_output(struct nfp_fl_output *output, const struct tc_action *action,
85 	      struct nfp_fl_payload *nfp_flow, bool last,
86 	      struct net_device *in_dev)
87 {
88 	size_t act_size = sizeof(struct nfp_fl_output);
89 	struct net_device *out_dev;
90 	u16 tmp_output_op;
91 	int ifindex;
92 
93 	/* Set action opcode to output action. */
94 	tmp_output_op =
95 		FIELD_PREP(NFP_FL_ACT_LEN_LW, act_size >> NFP_FL_LW_SIZ) |
96 		FIELD_PREP(NFP_FL_ACT_JMP_ID, NFP_FL_ACTION_OPCODE_OUTPUT);
97 
98 	output->a_op = cpu_to_be16(tmp_output_op);
99 
100 	/* Set action output parameters. */
101 	output->flags = cpu_to_be16(last ? NFP_FL_OUT_FLAGS_LAST : 0);
102 
103 	ifindex = tcf_mirred_ifindex(action);
104 	out_dev = __dev_get_by_index(dev_net(in_dev), ifindex);
105 	if (!out_dev)
106 		return -EOPNOTSUPP;
107 
108 	/* Only offload egress ports are on the same device as the ingress
109 	 * port.
110 	 */
111 	if (!switchdev_port_same_parent_id(in_dev, out_dev))
112 		return -EOPNOTSUPP;
113 
114 	output->port = cpu_to_be32(nfp_repr_get_port_id(out_dev));
115 	if (!output->port)
116 		return -EOPNOTSUPP;
117 
118 	nfp_flow->meta.shortcut = output->port;
119 
120 	return 0;
121 }
122 
123 static int
124 nfp_flower_loop_action(const struct tc_action *a,
125 		       struct nfp_fl_payload *nfp_fl, int *a_len,
126 		       struct net_device *netdev)
127 {
128 	struct nfp_fl_push_vlan *psh_v;
129 	struct nfp_fl_pop_vlan *pop_v;
130 	struct nfp_fl_output *output;
131 	int err;
132 
133 	if (is_tcf_gact_shot(a)) {
134 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_DROP);
135 	} else if (is_tcf_mirred_egress_redirect(a)) {
136 		if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
137 			return -EOPNOTSUPP;
138 
139 		output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
140 		err = nfp_fl_output(output, a, nfp_fl, true, netdev);
141 		if (err)
142 			return err;
143 
144 		*a_len += sizeof(struct nfp_fl_output);
145 	} else if (is_tcf_mirred_egress_mirror(a)) {
146 		if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
147 			return -EOPNOTSUPP;
148 
149 		output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
150 		err = nfp_fl_output(output, a, nfp_fl, false, netdev);
151 		if (err)
152 			return err;
153 
154 		*a_len += sizeof(struct nfp_fl_output);
155 	} else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
156 		if (*a_len + sizeof(struct nfp_fl_pop_vlan) > NFP_FL_MAX_A_SIZ)
157 			return -EOPNOTSUPP;
158 
159 		pop_v = (struct nfp_fl_pop_vlan *)&nfp_fl->action_data[*a_len];
160 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_POPV);
161 
162 		nfp_fl_pop_vlan(pop_v);
163 		*a_len += sizeof(struct nfp_fl_pop_vlan);
164 	} else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
165 		if (*a_len + sizeof(struct nfp_fl_push_vlan) > NFP_FL_MAX_A_SIZ)
166 			return -EOPNOTSUPP;
167 
168 		psh_v = (struct nfp_fl_push_vlan *)&nfp_fl->action_data[*a_len];
169 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
170 
171 		nfp_fl_push_vlan(psh_v, a);
172 		*a_len += sizeof(struct nfp_fl_push_vlan);
173 	} else {
174 		/* Currently we do not handle any other actions. */
175 		return -EOPNOTSUPP;
176 	}
177 
178 	return 0;
179 }
180 
181 int nfp_flower_compile_action(struct tc_cls_flower_offload *flow,
182 			      struct net_device *netdev,
183 			      struct nfp_fl_payload *nfp_flow)
184 {
185 	int act_len, act_cnt, err;
186 	const struct tc_action *a;
187 	LIST_HEAD(actions);
188 
189 	memset(nfp_flow->action_data, 0, NFP_FL_MAX_A_SIZ);
190 	nfp_flow->meta.act_len = 0;
191 	act_len = 0;
192 	act_cnt = 0;
193 
194 	tcf_exts_to_list(flow->exts, &actions);
195 	list_for_each_entry(a, &actions, list) {
196 		err = nfp_flower_loop_action(a, nfp_flow, &act_len, netdev);
197 		if (err)
198 			return err;
199 		act_cnt++;
200 	}
201 
202 	/* We optimise when the action list is small, this can unfortunately
203 	 * not happen once we have more than one action in the action list.
204 	 */
205 	if (act_cnt > 1)
206 		nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
207 
208 	nfp_flow->meta.act_len = act_len;
209 
210 	return 0;
211 }
212