aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/mellanox/mlx5/core/en/tc/int_port.c
blob: ca834bbcb44f1953b42c9a1e6db21856aa2d5aa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
/* Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */

#include <linux/mlx5/fs.h>
#include "en/mapping.h"
#include "en/tc/int_port.h"
#include "en.h"
#include "en_rep.h"
#include "en_tc.h"

struct mlx5e_tc_int_port {
	enum mlx5e_tc_int_port_type type;
	int ifindex;
	u32 match_metadata;
	u32 mapping;
	struct list_head list;
	struct mlx5_flow_handle *rx_rule;
	refcount_t refcnt;
	struct rcu_head rcu_head;
};

struct mlx5e_tc_int_port_priv {
	struct mlx5_core_dev *dev;
	struct mutex int_ports_lock; /* Protects int ports list */
	struct list_head int_ports; /* Uses int_ports_lock */
	u16 num_ports;
	bool ul_rep_rx_ready; /* Set when uplink is performing teardown */
	struct mapping_ctx *metadata_mapping; /* Metadata for source port rewrite and matching */
};

bool mlx5e_tc_int_port_supported(const struct mlx5_eswitch *esw)
{
	return mlx5_eswitch_vport_match_metadata_enabled(esw) &&
	       MLX5_CAP_GEN(esw->dev, reg_c_preserve);
}

u32 mlx5e_tc_int_port_get_metadata(struct mlx5e_tc_int_port *int_port)
{
	return int_port->match_metadata;
}

int mlx5e_tc_int_port_get_flow_source(struct mlx5e_tc_int_port *int_port)
{
	/* For egress forwarding we can have the case
	 * where the packet came from a vport and redirected
	 * to int port or it came from the uplink, going
	 * via internal port and hairpinned back to uplink
	 * so we set the source to any port in this case.
	 */
	return int_port->type == MLX5E_TC_INT_PORT_EGRESS ?
		MLX5_FLOW_CONTEXT_FLOW_SOURCE_ANY_VPORT :
		MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
}

u32 mlx5e_tc_int_port_get_metadata_for_match(struct mlx5e_tc_int_port *int_port)
{
	return int_port->match_metadata << (32 - ESW_SOURCE_PORT_METADATA_BITS);
}

static struct mlx5_flow_handle *
mlx5e_int_port_create_rx_rule(struct mlx5_eswitch *esw,
			      struct mlx5e_tc_int_port *int_port,
			      struct mlx5_flow_destination *dest)

{
	struct mlx5_flow_context *flow_context;
	struct mlx5_flow_act flow_act = {};
	struct mlx5_flow_handle *flow_rule;
	struct mlx5_flow_spec *spec;
	void *misc;

	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
	if (!spec)
		return ERR_PTR(-ENOMEM);

	misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, misc_parameters_2);
	MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
		 mlx5e_tc_int_port_get_metadata_for_match(int_port));

	misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, misc_parameters_2);
	MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
		 mlx5_eswitch_get_vport_metadata_mask());

	spec->match_criteria_enable = MLX5_MATCH_MISC_PARAMETERS_2;

	/* Overwrite flow tag with the int port metadata mapping
	 * instead of the chain mapping.
	 */
	flow_context = &spec->flow_context;
	flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
	flow_context->flow_tag = int_port->mapping;
	flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
	flow_rule = mlx5_add_flow_rules(esw->offloads.ft_offloads, spec,
					&flow_act, dest, 1);
	if (IS_ERR(flow_rule))
		mlx5_core_warn(esw->dev, "ft offloads: Failed to add internal vport rx rule err %ld\n",
			       PTR_ERR(flow_rule));

	kvfree(spec);

	return flow_rule;
}

static struct mlx5e_tc_int_port *
mlx5e_int_port_lookup(struct mlx5e_tc_int_port_priv *priv,
		      int ifindex,
		      enum mlx5e_tc_int_port_type type)
{
	struct mlx5e_tc_int_port *int_port;

	if (!priv->ul_rep_rx_ready)
		goto not_found;

	list_for_each_entry(int_port, &priv->int_ports, list)
		if (int_port->ifindex == ifindex && int_port->type == type) {
			refcount_inc(&int_port->refcnt);
			return int_port;
		}

not_found:
	return NULL;
}

static int mlx5e_int_port_metadata_alloc(struct mlx5e_tc_int_port_priv *priv,
					 int ifindex, enum mlx5e_tc_int_port_type type,
					 u32 *id)
{
	u32 mapped_key[2] = {type, ifindex};
	int err;

	err = mapping_add(priv->metadata_mapping, mapped_key, id);
	if (err)
		return err;

	/* Fill upper 4 bits of PFNUM with reserved value */
	*id |= 0xf << ESW_VPORT_BITS;

	return 0;
}

static void mlx5e_int_port_metadata_free(struct mlx5e_tc_int_port_priv *priv,
					 u32 id)
{
	id &= (1 << ESW_VPORT_BITS) - 1;
	mapping_remove(priv->metadata_mapping, id);
}

/* Must be called with priv->int_ports_lock held */
static struct mlx5e_tc_int_port *
mlx5e_int_port_add(struct mlx5e_tc_int_port_priv *priv,
		   int ifindex,
		   enum mlx5e_tc_int_port_type type)
{
	struct mlx5_eswitch *esw = priv->dev->priv.eswitch;
	struct mlx5_mapped_obj mapped_obj = {};
	struct mlx5e_rep_priv *uplink_rpriv;
	struct mlx5e_tc_int_port *int_port;
	struct mlx5_flow_destination dest;
	struct mapping_ctx *ctx;
	u32 match_metadata;
	u32 mapping;
	int err;

	if (priv->num_ports == MLX5E_TC_MAX_INT_PORT_NUM) {
		mlx5_core_dbg(priv->dev, "Cannot add a new int port, max supported %d",
			      MLX5E_TC_MAX_INT_PORT_NUM);
		return ERR_PTR(-ENOSPC);
	}

	int_port = kzalloc(sizeof(*int_port), GFP_KERNEL);
	if (!int_port)
		return ERR_PTR(-ENOMEM);

	err = mlx5e_int_port_metadata_alloc(priv, ifindex, type, &match_metadata);
	if (err) {
		mlx5_core_warn(esw->dev, "Cannot add a new internal port, metadata allocation failed for ifindex %d",
			       ifindex);
		goto err_metadata;
	}

	/* map metadata to reg_c0 object for miss handling */
	ctx = esw->offloads.reg_c0_obj_pool;
	mapped_obj.type = MLX5_MAPPED_OBJ_INT_PORT_METADATA;
	mapped_obj.int_port_metadata = match_metadata;
	err = mapping_add(ctx, &mapped_obj, &mapping);
	if (err)
		goto err_map;

	int_port->type = type;
	int_port->ifindex = ifindex;
	int_port->match_metadata = match_metadata;
	int_port->mapping = mapping;

	/* Create a match on internal vport metadata in vport table */
	uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);

	dest.type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
	dest.ft = uplink_rpriv->root_ft;

	int_port->rx_rule = mlx5e_int_port_create_rx_rule(esw, int_port, &dest);
	if (IS_ERR(int_port->rx_rule)) {
		err = PTR_ERR(int_port->rx_rule);
		mlx5_core_warn(esw->dev, "Can't add internal port rx rule, err %d", err);
		goto err_rx_rule;
	}

	refcount_set(&int_port->refcnt, 1);
	list_add_rcu(&int_port->list, &priv->int_ports);
	priv->num_ports++;

	return int_port;

err_rx_rule:
	mapping_remove(ctx, int_port->mapping);

err_map:
	mlx5e_int_port_metadata_free(priv, match_metadata);

err_metadata:
	kfree(int_port);

	return ERR_PTR(err);
}

/* Must be called with priv->int_ports_lock held */
static void
mlx5e_int_port_remove(struct mlx5e_tc_int_port_priv *priv,
		      struct mlx5e_tc_int_port *int_port)
{
	struct mlx5_eswitch *esw = priv->dev->priv.eswitch;
	struct mapping_ctx *ctx;

	ctx = esw->offloads.reg_c0_obj_pool;

	list_del_rcu(&int_port->list);

	/* The following parameters are not used by the
	 * rcu readers of this int_port object so it is
	 * safe to release them.
	 */
	if (int_port->rx_rule)
		mlx5_del_flow_rules(int_port->rx_rule);
	mapping_remove(ctx, int_port->mapping);
	mlx5e_int_port_metadata_free(priv, int_port->match_metadata);
	kfree_rcu(int_port);
	priv->num_ports--;
}

/* Must be called with rcu_read_lock held */
static struct mlx5e_tc_int_port *
mlx5e_int_port_get_from_metadata(struct mlx5e_tc_int_port_priv *priv,
				 u32 metadata)
{
	struct mlx5e_tc_int_port *int_port;

	list_for_each_entry_rcu(int_port, &priv->int_ports, list)
		if (int_port->match_metadata == metadata)
			return int_port;

	return NULL;
}

struct mlx5e_tc_int_port *
mlx5e_tc_int_port_get(struct mlx5e_tc_int_port_priv *priv,
		      int ifindex,
		      enum mlx5e_tc_int_port_type type)
{
	struct mlx5e_tc_int_port *int_port;

	if (!priv)
		return ERR_PTR(-EOPNOTSUPP);

	mutex_lock(&priv->int_ports_lock);

	/* Reject request if ul rep not ready */
	if (!priv->ul_rep_rx_ready) {
		int_port = ERR_PTR(-EOPNOTSUPP);
		goto done;
	}

	int_port = mlx5e_int_port_lookup(priv, ifindex, type);
	if (int_port)
		goto done;

	/* Alloc and add new int port to list */
	int_port = mlx5e_int_port_add(priv, ifindex, type);

done:
	mutex_unlock(&priv->int_ports_lock);

	return int_port;
}

void
mlx5e_tc_int_port_put(struct mlx5e_tc_int_port_priv *priv,
		      struct mlx5e_tc_int_port *int_port)
{
	if (!refcount_dec_and_mutex_lock(&int_port->refcnt, &priv->int_ports_lock))
		return;

	mlx5e_int_port_remove(priv, int_port);
	mutex_unlock(&priv->int_ports_lock);
}

struct mlx5e_tc_int_port_priv *
mlx5e_tc_int_port_init(struct mlx5e_priv *priv)
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
	struct mlx5e_tc_int_port_priv *int_port_priv;
	u64 mapping_id;

	if (!mlx5e_tc_int_port_supported(esw))
		return NULL;

	int_port_priv = kzalloc(sizeof(*int_port_priv), GFP_KERNEL);
	if (!int_port_priv)
		return NULL;

	mapping_id = mlx5_query_nic_system_image_guid(priv->mdev);

	int_port_priv->metadata_mapping = mapping_create_for_id(mapping_id, MAPPING_TYPE_INT_PORT,
								sizeof(u32) * 2,
								(1 << ESW_VPORT_BITS) - 1, true);
	if (IS_ERR(int_port_priv->metadata_mapping)) {
		mlx5_core_warn(priv->mdev, "Can't allocate metadata mapping of int port offload, err=%ld\n",
			       PTR_ERR(int_port_priv->metadata_mapping));
		goto err_mapping;
	}

	int_port_priv->dev = priv->mdev;
	mutex_init(&int_port_priv->int_ports_lock);
	INIT_LIST_HEAD(&int_port_priv->int_ports);

	return int_port_priv;

err_mapping:
	kfree(int_port_priv);

	return NULL;
}

void
mlx5e_tc_int_port_cleanup(struct mlx5e_tc_int_port_priv *priv)
{
	if (!priv)
		return;

	mutex_destroy(&priv->int_ports_lock);
	mapping_destroy(priv->metadata_mapping);
	kfree(priv);
}

/* Int port rx rules reside in ul rep rx tables.
 * It is possible the ul rep will go down while there are
 * still int port rules in its rx table so proper cleanup
 * is required to free resources.
 */
void mlx5e_tc_int_port_init_rep_rx(struct mlx5e_priv *priv)
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
	struct mlx5_rep_uplink_priv *uplink_priv;
	struct mlx5e_tc_int_port_priv *ppriv;
	struct mlx5e_rep_priv *uplink_rpriv;

	uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
	uplink_priv = &uplink_rpriv->uplink_priv;

	ppriv = uplink_priv->int_port_priv;

	if (!ppriv)
		return;

	mutex_lock(&ppriv->int_ports_lock);
	ppriv->ul_rep_rx_ready = true;
	mutex_unlock(&ppriv->int_ports_lock);
}

void mlx5e_tc_int_port_cleanup_rep_rx(struct mlx5e_priv *priv)
{
	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
	struct mlx5_rep_uplink_priv *uplink_priv;
	struct mlx5e_tc_int_port_priv *ppriv;
	struct mlx5e_rep_priv *uplink_rpriv;
	struct mlx5e_tc_int_port *int_port;

	uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
	uplink_priv = &uplink_rpriv->uplink_priv;

	ppriv = uplink_priv->int_port_priv;

	if (!ppriv)
		return;

	mutex_lock(&ppriv->int_ports_lock);

	ppriv->ul_rep_rx_ready = false;

	list_for_each_entry(int_port, &ppriv->int_ports, list) {
		if (!IS_ERR_OR_NULL(int_port->rx_rule))
			mlx5_del_flow_rules(int_port->rx_rule);

		int_port->rx_rule = NULL;
	}

	mutex_unlock(&ppriv->int_ports_lock);
}

bool
mlx5e_tc_int_port_dev_fwd(struct mlx5e_tc_int_port_priv *priv,
			  struct sk_buff *skb, u32 int_vport_metadata,
			  bool *forward_tx)
{
	enum mlx5e_tc_int_port_type fwd_type;
	struct mlx5e_tc_int_port *int_port;
	struct net_device *dev;
	int ifindex;

	if (!priv)
		return false;

	rcu_read_lock();
	int_port = mlx5e_int_port_get_from_metadata(priv, int_vport_metadata);
	if (!int_port) {
		rcu_read_unlock();
		mlx5_core_dbg(priv->dev, "Unable to find int port with metadata 0x%.8x\n",
			      int_vport_metadata);
		return false;
	}

	ifindex = int_port->ifindex;
	fwd_type = int_port->type;
	rcu_read_unlock();

	dev = dev_get_by_index(&init_net, ifindex);
	if (!dev) {
		mlx5_core_dbg(priv->dev,
			      "Couldn't find internal port device with ifindex: %d\n",
			      ifindex);
		return false;
	}

	skb->skb_iif = dev->ifindex;
	skb->dev = dev;

	if (fwd_type == MLX5E_TC_INT_PORT_INGRESS) {
		skb->pkt_type = PACKET_HOST;
		skb_set_redirected(skb, true);
		*forward_tx = false;
	} else {
		skb_reset_network_header(skb);
		skb_push_rcsum(skb, skb->mac_len);
		skb_set_redirected(skb, false);
		*forward_tx = true;
	}

	return true;
}