]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads_termtbl.c
net/mlx5e: Support accept action
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch_offloads_termtbl.c
CommitLineData
10caabda
OS
1// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2// Copyright (c) 2019 Mellanox Technologies.
3
4#include <linux/mlx5/fs.h>
5#include "eswitch.h"
c620b772 6#include "en_tc.h"
249ccc3c 7#include "fs_core.h"
10caabda
OS
8
9struct mlx5_termtbl_handle {
10 struct hlist_node termtbl_hlist;
11
12 struct mlx5_flow_table *termtbl;
13 struct mlx5_flow_act flow_act;
14 struct mlx5_flow_destination dest;
15
16 struct mlx5_flow_handle *rule;
17 int ref_count;
18};
19
20static u32
21mlx5_eswitch_termtbl_hash(struct mlx5_flow_act *flow_act,
22 struct mlx5_flow_destination *dest)
23{
24 u32 hash;
25
26 hash = jhash_1word(flow_act->action, 0);
27 hash = jhash((const void *)&flow_act->vlan,
28 sizeof(flow_act->vlan), hash);
29 hash = jhash((const void *)&dest->vport.num,
30 sizeof(dest->vport.num), hash);
31 hash = jhash((const void *)&dest->vport.vhca_id,
32 sizeof(dest->vport.num), hash);
249ccc3c
EC
33 if (dest->vport.pkt_reformat)
34 hash = jhash(dest->vport.pkt_reformat,
35 sizeof(*dest->vport.pkt_reformat),
36 hash);
10caabda
OS
37 return hash;
38}
39
40static int
41mlx5_eswitch_termtbl_cmp(struct mlx5_flow_act *flow_act1,
42 struct mlx5_flow_destination *dest1,
43 struct mlx5_flow_act *flow_act2,
44 struct mlx5_flow_destination *dest2)
45{
249ccc3c
EC
46 int ret;
47
48 ret = flow_act1->action != flow_act2->action ||
49 dest1->vport.num != dest2->vport.num ||
50 dest1->vport.vhca_id != dest2->vport.vhca_id ||
51 memcmp(&flow_act1->vlan, &flow_act2->vlan,
52 sizeof(flow_act1->vlan));
53 if (ret)
54 return ret;
55
56 return dest1->vport.pkt_reformat && dest2->vport.pkt_reformat ?
57 memcmp(dest1->vport.pkt_reformat, dest2->vport.pkt_reformat,
58 sizeof(*dest1->vport.pkt_reformat)) : 0;
10caabda
OS
59}
60
61static int
62mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev,
63 struct mlx5_termtbl_handle *tt,
64 struct mlx5_flow_act *flow_act)
65{
61dc7b01 66 struct mlx5_flow_table_attr ft_attr = {};
10caabda 67 struct mlx5_flow_namespace *root_ns;
fca08661 68 int err, err2;
10caabda
OS
69
70 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
71 if (!root_ns) {
72 esw_warn(dev, "Failed to get FDB flow namespace\n");
73 return -EOPNOTSUPP;
74 }
75
76 /* As this is the terminating action then the termination table is the
77 * same prio as the slow path
78 */
6ff51ab8 79 ft_attr.flags = MLX5_FLOW_TABLE_TERMINATION | MLX5_FLOW_TABLE_UNMANAGED |
249ccc3c 80 MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
6ff51ab8 81 ft_attr.prio = FDB_TC_OFFLOAD;
61dc7b01 82 ft_attr.max_fte = 1;
6ff51ab8 83 ft_attr.level = 1;
61dc7b01
PB
84 ft_attr.autogroup.max_num_groups = 1;
85 tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, &ft_attr);
10caabda 86 if (IS_ERR(tt->termtbl)) {
fca08661
RD
87 err = PTR_ERR(tt->termtbl);
88 esw_warn(dev, "Failed to create termination table, err %pe\n", tt->termtbl);
89 return err;
10caabda
OS
90 }
91
5c2aa8ae 92 tt->rule = mlx5_add_flow_rules(tt->termtbl, NULL, flow_act,
10caabda 93 &tt->dest, 1);
10caabda 94 if (IS_ERR(tt->rule)) {
fca08661
RD
95 err = PTR_ERR(tt->rule);
96 esw_warn(dev, "Failed to create termination table rule, err %pe\n", tt->rule);
10caabda
OS
97 goto add_flow_err;
98 }
99 return 0;
100
101add_flow_err:
fca08661
RD
102 err2 = mlx5_destroy_flow_table(tt->termtbl);
103 if (err2)
104 esw_warn(dev, "Failed to destroy termination table, err %d\n", err2);
10caabda 105
fca08661 106 return err;
10caabda
OS
107}
108
109static struct mlx5_termtbl_handle *
110mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw,
111 struct mlx5_flow_act *flow_act,
249ccc3c
EC
112 struct mlx5_flow_destination *dest,
113 struct mlx5_esw_flow_attr *attr)
10caabda
OS
114{
115 struct mlx5_termtbl_handle *tt;
116 bool found = false;
117 u32 hash_key;
118 int err;
119
120 mutex_lock(&esw->offloads.termtbl_mutex);
10caabda
OS
121 hash_key = mlx5_eswitch_termtbl_hash(flow_act, dest);
122 hash_for_each_possible(esw->offloads.termtbl_tbl, tt,
123 termtbl_hlist, hash_key) {
124 if (!mlx5_eswitch_termtbl_cmp(&tt->flow_act, &tt->dest,
125 flow_act, dest)) {
126 found = true;
127 break;
128 }
129 }
130 if (found)
131 goto tt_add_ref;
132
133 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
134 if (!tt) {
135 err = -ENOMEM;
136 goto tt_create_err;
137 }
138
139 tt->dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
140 tt->dest.vport.num = dest->vport.num;
141 tt->dest.vport.vhca_id = dest->vport.vhca_id;
249ccc3c 142 tt->dest.vport.flags = dest->vport.flags;
10caabda
OS
143 memcpy(&tt->flow_act, flow_act, sizeof(*flow_act));
144
145 err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act);
25cb3176 146 if (err)
10caabda 147 goto tt_create_err;
25cb3176 148
10caabda
OS
149 hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key);
150tt_add_ref:
151 tt->ref_count++;
152 mutex_unlock(&esw->offloads.termtbl_mutex);
153 return tt;
154tt_create_err:
155 kfree(tt);
156 mutex_unlock(&esw->offloads.termtbl_mutex);
157 return ERR_PTR(err);
158}
159
160void
161mlx5_eswitch_termtbl_put(struct mlx5_eswitch *esw,
162 struct mlx5_termtbl_handle *tt)
163{
164 mutex_lock(&esw->offloads.termtbl_mutex);
165 if (--tt->ref_count == 0)
166 hash_del(&tt->termtbl_hlist);
167 mutex_unlock(&esw->offloads.termtbl_mutex);
168
169 if (!tt->ref_count) {
170 mlx5_del_flow_rules(tt->rule);
171 mlx5_destroy_flow_table(tt->termtbl);
172 kfree(tt);
173 }
174}
175
176static void
177mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src,
178 struct mlx5_flow_act *dst)
179{
249ccc3c
EC
180 if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
181 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
182 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
183 memcpy(&dst->vlan[0], &src->vlan[0], sizeof(src->vlan[0]));
184 memset(&src->vlan[0], 0, sizeof(src->vlan[0]));
185
186 if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
187 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
188 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
189 memcpy(&dst->vlan[1], &src->vlan[1], sizeof(src->vlan[1]));
190 memset(&src->vlan[1], 0, sizeof(src->vlan[1]));
191 }
192 }
10caabda
OS
193}
194
d5dbcc4e
DL
195static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch *esw,
196 const struct mlx5_flow_spec *spec)
197{
1708dd54 198 u16 port_mask, port_value;
d5dbcc4e
DL
199
200 if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
950d3af7
DL
201 return spec->flow_context.flow_source ==
202 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
d5dbcc4e
DL
203
204 port_mask = MLX5_GET(fte_match_param, spec->match_criteria,
205 misc_parameters.source_port);
206 port_value = MLX5_GET(fte_match_param, spec->match_value,
207 misc_parameters.source_port);
1708dd54 208 return (port_mask & port_value) == MLX5_VPORT_UPLINK;
d5dbcc4e
DL
209}
210
10caabda
OS
211bool
212mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw,
c620b772 213 struct mlx5_flow_attr *attr,
10caabda
OS
214 struct mlx5_flow_act *flow_act,
215 struct mlx5_flow_spec *spec)
216{
c620b772 217 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
249ccc3c
EC
218 int i;
219
d8a2034f 220 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table) ||
6ff51ab8 221 !MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ignore_flow_level) ||
d557fbbd 222 mlx5_esw_attr_flags_skip(attr->flags) ||
249ccc3c 223 !mlx5_eswitch_offload_is_uplink_port(esw, spec))
10caabda
OS
224 return false;
225
226 /* push vlan on RX */
249ccc3c
EC
227 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)
228 return true;
229
230 /* hairpin */
c620b772
AL
231 for (i = esw_attr->split_count; i < esw_attr->out_count; i++)
232 if (esw_attr->dests[i].rep->vport == MLX5_VPORT_UPLINK)
249ccc3c
EC
233 return true;
234
235 return false;
10caabda
OS
236}
237
238struct mlx5_flow_handle *
239mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch *esw,
240 struct mlx5_flow_table *fdb,
241 struct mlx5_flow_spec *spec,
242 struct mlx5_esw_flow_attr *attr,
243 struct mlx5_flow_act *flow_act,
244 struct mlx5_flow_destination *dest,
245 int num_dest)
246{
247 struct mlx5_flow_act term_tbl_act = {};
248 struct mlx5_flow_handle *rule = NULL;
249 bool term_table_created = false;
250 int num_vport_dests = 0;
251 int i, curr_dest;
252
253 mlx5_eswitch_termtbl_actions_move(flow_act, &term_tbl_act);
254 term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
255
256 for (i = 0; i < num_dest; i++) {
257 struct mlx5_termtbl_handle *tt;
258
259 /* only vport destinations can be terminated */
260 if (dest[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT)
261 continue;
262
442b3d7b
JL
263 if (attr->dests[num_vport_dests].flags & MLX5_ESW_DEST_ENCAP) {
264 term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
265 term_tbl_act.pkt_reformat = attr->dests[num_vport_dests].pkt_reformat;
266 } else {
267 term_tbl_act.action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
268 term_tbl_act.pkt_reformat = NULL;
269 }
270
10caabda
OS
271 /* get the terminating table for the action list */
272 tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act,
249ccc3c 273 &dest[i], attr);
10caabda 274 if (IS_ERR(tt)) {
fca08661 275 esw_warn(esw->dev, "Failed to get termination table, err %pe\n", tt);
10caabda
OS
276 goto revert_changes;
277 }
278 attr->dests[num_vport_dests].termtbl = tt;
279 num_vport_dests++;
280
281 /* link the destination with the termination table */
282 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
283 dest[i].ft = tt->termtbl;
284 term_table_created = true;
285 }
286
287 /* at least one destination should reference a termination table */
288 if (!term_table_created)
289 goto revert_changes;
290
291 /* create the FTE */
442b3d7b
JL
292 flow_act->action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
293 flow_act->pkt_reformat = NULL;
6ff51ab8 294 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
10caabda
OS
295 rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);
296 if (IS_ERR(rule))
297 goto revert_changes;
298
299 goto out;
300
301revert_changes:
302 /* revert the changes that were made to the original flow_act
303 * and fall-back to the original rule actions
304 */
305 mlx5_eswitch_termtbl_actions_move(&term_tbl_act, flow_act);
306
307 for (curr_dest = 0; curr_dest < num_vport_dests; curr_dest++) {
308 struct mlx5_termtbl_handle *tt = attr->dests[curr_dest].termtbl;
309
310 /* search for the destination associated with the
311 * current term table
312 */
313 for (i = 0; i < num_dest; i++) {
314 if (dest[i].ft != tt->termtbl)
315 continue;
316
317 memset(&dest[i], 0, sizeof(dest[i]));
318 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
319 dest[i].vport.num = tt->dest.vport.num;
320 dest[i].vport.vhca_id = tt->dest.vport.vhca_id;
321 mlx5_eswitch_termtbl_put(esw, tt);
322 break;
323 }
324 }
325 rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);
326out:
327 return rule;
328}