]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/net/ethernet/mellanox/mlx5/core/eswitch_offloads_termtbl.c
879d78e46e47d896cf935a8fd02f4aee04e10422
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch_offloads_termtbl.c
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"
6 #include "en_tc.h"
7 #include "fs_core.h"
8
9 struct 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
20 static u32
21 mlx5_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);
33 if (dest->vport.pkt_reformat)
34 hash = jhash(dest->vport.pkt_reformat,
35 sizeof(*dest->vport.pkt_reformat),
36 hash);
37 return hash;
38 }
39
40 static int
41 mlx5_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 {
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;
59 }
60
61 static int
62 mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev,
63 struct mlx5_termtbl_handle *tt,
64 struct mlx5_flow_act *flow_act)
65 {
66 struct mlx5_flow_table_attr ft_attr = {};
67 struct mlx5_flow_namespace *root_ns;
68 int err, err2;
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 */
79 ft_attr.flags = MLX5_FLOW_TABLE_TERMINATION | MLX5_FLOW_TABLE_UNMANAGED |
80 MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
81 ft_attr.prio = FDB_TC_OFFLOAD;
82 ft_attr.max_fte = 1;
83 ft_attr.level = 1;
84 ft_attr.autogroup.max_num_groups = 1;
85 tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, &ft_attr);
86 if (IS_ERR(tt->termtbl)) {
87 err = PTR_ERR(tt->termtbl);
88 esw_warn(dev, "Failed to create termination table, err %pe\n", tt->termtbl);
89 return err;
90 }
91
92 tt->rule = mlx5_add_flow_rules(tt->termtbl, NULL, flow_act,
93 &tt->dest, 1);
94 if (IS_ERR(tt->rule)) {
95 err = PTR_ERR(tt->rule);
96 esw_warn(dev, "Failed to create termination table rule, err %pe\n", tt->rule);
97 goto add_flow_err;
98 }
99 return 0;
100
101 add_flow_err:
102 err2 = mlx5_destroy_flow_table(tt->termtbl);
103 if (err2)
104 esw_warn(dev, "Failed to destroy termination table, err %d\n", err2);
105
106 return err;
107 }
108
109 static struct mlx5_termtbl_handle *
110 mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw,
111 struct mlx5_flow_act *flow_act,
112 struct mlx5_flow_destination *dest,
113 struct mlx5_esw_flow_attr *attr)
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);
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;
142 tt->dest.vport.flags = dest->vport.flags;
143 memcpy(&tt->flow_act, flow_act, sizeof(*flow_act));
144
145 err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act);
146 if (err)
147 goto tt_create_err;
148
149 hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key);
150 tt_add_ref:
151 tt->ref_count++;
152 mutex_unlock(&esw->offloads.termtbl_mutex);
153 return tt;
154 tt_create_err:
155 kfree(tt);
156 mutex_unlock(&esw->offloads.termtbl_mutex);
157 return ERR_PTR(err);
158 }
159
160 void
161 mlx5_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
176 static void
177 mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src,
178 struct mlx5_flow_act *dst)
179 {
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 }
193 }
194
195 static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch *esw,
196 const struct mlx5_flow_spec *spec)
197 {
198 u16 port_mask, port_value;
199
200 if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
201 return spec->flow_context.flow_source ==
202 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
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);
208 return (port_mask & port_value) == MLX5_VPORT_UPLINK;
209 }
210
211 bool
212 mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw,
213 struct mlx5_flow_attr *attr,
214 struct mlx5_flow_act *flow_act,
215 struct mlx5_flow_spec *spec)
216 {
217 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
218 int i;
219
220 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table) ||
221 !MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ignore_flow_level) ||
222 mlx5_esw_attr_flags_skip(attr->flags) ||
223 !mlx5_eswitch_offload_is_uplink_port(esw, spec))
224 return false;
225
226 /* push vlan on RX */
227 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)
228 return true;
229
230 /* hairpin */
231 for (i = esw_attr->split_count; i < esw_attr->out_count; i++)
232 if (esw_attr->dests[i].rep->vport == MLX5_VPORT_UPLINK)
233 return true;
234
235 return false;
236 }
237
238 struct mlx5_flow_handle *
239 mlx5_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
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
271 /* get the terminating table for the action list */
272 tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act,
273 &dest[i], attr);
274 if (IS_ERR(tt)) {
275 esw_warn(esw->dev, "Failed to get termination table, err %pe\n", tt);
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 */
292 flow_act->action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
293 flow_act->pkt_reformat = NULL;
294 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
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
301 revert_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);
326 out:
327 return rule;
328 }