aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/mvpp2/mrvl_mtr.c
blob: 9adcd9751bcbbf8d84c5e9ac79f4ed232c3f8a3b (plain)
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Marvell International Ltd.
 * Copyright(c) 2018 Semihalf.
 * All rights reserved.
 */

#include <rte_log.h>
#include <rte_malloc.h>

#include "mrvl_mtr.h"

/** Maximum meter rate */
#define MRVL_SRTCM_RFC2697_CIR_MAX 1023000

/** Invalid plcr bit */
#define MRVL_PLCR_BIT_INVALID -1

/**
 * Return meter object capabilities.
 *
 * @param dev Pointer to the device (unused).
 * @param cap Pointer to the meter object capabilities.
 * @param error Pointer to the error (unused).
 * @returns 0 always.
 */
static int
mrvl_capabilities_get(struct rte_eth_dev *dev __rte_unused,
			  struct rte_mtr_capabilities *cap,
			  struct rte_mtr_error *error __rte_unused)
{
	struct rte_mtr_capabilities capa = {
		.n_max = PP2_CLS_PLCR_NUM,
		.n_shared_max = PP2_CLS_PLCR_NUM,
		.shared_n_flows_per_mtr_max = -1,
		.meter_srtcm_rfc2697_n_max = PP2_CLS_PLCR_NUM,
		.meter_rate_max = MRVL_SRTCM_RFC2697_CIR_MAX,
	};

	memcpy(cap, &capa, sizeof(capa));

	return 0;
}

/**
 * Get profile using it's id.
 *
 * @param priv Pointer to the port's private data.
 * @param meter_profile_id Profile id used by the meter.
 * @returns Pointer to the profile if exists, NULL otherwise.
 */
static struct mrvl_mtr_profile *
mrvl_mtr_profile_from_id(struct mrvl_priv *priv, uint32_t meter_profile_id)
{
	struct mrvl_mtr_profile *profile = NULL;

	LIST_FOREACH(profile, &priv->profiles, next)
		if (profile->profile_id == meter_profile_id)
			break;

	return profile;
}

/**
 * Add profile to the list of profiles.
 *
 * @param dev Pointer to the device.
 * @param meter_profile_id Id of the new profile.
 * @param profile Pointer to the profile configuration.
 * @param error Pointer to the error.
 * @returns 0 on success, negative value otherwise.
 */
static int
mrvl_meter_profile_add(struct rte_eth_dev *dev, uint32_t meter_profile_id,
		       struct rte_mtr_meter_profile *profile,
		       struct rte_mtr_error *error)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr_profile *prof;

	if (!profile)
		return -rte_mtr_error_set(error, EINVAL,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, NULL);

	if (profile->alg != RTE_MTR_SRTCM_RFC2697)
		return -rte_mtr_error_set(error, EINVAL,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL,
					  "Only srTCM RFC 2697 is supported\n");

	prof = mrvl_mtr_profile_from_id(priv, meter_profile_id);
	if (prof)
		return -rte_mtr_error_set(error, EEXIST,
					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
					  NULL, "Profile id already exists\n");

	prof = rte_zmalloc_socket(NULL, sizeof(*prof), 0, rte_socket_id());
	if (!prof)
		return -rte_mtr_error_set(error, ENOMEM,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, NULL);

	prof->profile_id = meter_profile_id;
	memcpy(&prof->profile, profile, sizeof(*profile));

	LIST_INSERT_HEAD(&priv->profiles, prof, next);

	return 0;
}

/**
 * Remove profile from the list of profiles.
 *
 * @param dev Pointer to the device.
 * @param meter_profile_id Id of the profile to remove.
 * @param error Pointer to the error.
 * @returns 0 on success, negative value otherwise.
 */
static int
mrvl_meter_profile_delete(struct rte_eth_dev *dev,
			      uint32_t meter_profile_id,
			      struct rte_mtr_error *error)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr_profile *profile;

	profile = mrvl_mtr_profile_from_id(priv, meter_profile_id);
	if (!profile)
		return -rte_mtr_error_set(error, ENODEV,
					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
					  NULL, "Profile id does not exist\n");

	if (profile->refcnt)
		return -rte_mtr_error_set(error, EPERM,
					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
					  NULL, "Profile is used\n");

	LIST_REMOVE(profile, next);
	rte_free(profile);

	return 0;
}

/**
 * Get meter using it's id.
 *
 * @param priv Pointer to port's private data.
 * @param mtr_id Id of the meter.
 * @returns Pointer to the meter if exists, NULL otherwise.
 */
static struct mrvl_mtr *
mrvl_mtr_from_id(struct mrvl_priv *priv, uint32_t mtr_id)
{
	struct mrvl_mtr *mtr = NULL;

	LIST_FOREACH(mtr, &priv->mtrs, next)
		if (mtr->mtr_id == mtr_id)
			break;

	return mtr;
}

/**
 * Reserve a policer bit in a bitmap.
 *
 * @param plcrs Pointer to the policers bitmap.
 * @returns Reserved bit number on success, negative value otherwise.
 */
static int
mrvl_reserve_plcr(uint32_t *plcrs)
{
	uint32_t i, num;

	num = PP2_CLS_PLCR_NUM;
	if (num > sizeof(uint32_t) * 8) {
		num = sizeof(uint32_t) * 8;
		MRVL_LOG(WARNING, "Plcrs number was limited to 32.");
	}

	for (i = 0; i < num; i++) {
		uint32_t bit = BIT(i);

		if (!(*plcrs & bit)) {
			*plcrs |= bit;

			return i;
		}
	}

	return -1;
}

/**
 * Enable meter object.
 *
 * @param dev Pointer to the device.
 * @param mtr_id Id of the meter.
 * @param error Pointer to the error.
 * @returns 0 in success, negative value otherwise.
 */
static int
mrvl_meter_enable(struct rte_eth_dev *dev, uint32_t mtr_id,
		  struct rte_mtr_error *error)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr *mtr = mrvl_mtr_from_id(priv, mtr_id);
	struct pp2_cls_plcr_params params;
	char match[MRVL_MATCH_LEN];
	struct rte_flow *flow;
	int ret;

	if (!priv->ppio)
		return -rte_mtr_error_set(error, EPERM,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, "Port is uninitialized\n");

	if (!mtr)
		return -rte_mtr_error_set(error, ENODEV,
					  RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
					  "Meter id does not exist\n");

	if (mtr->plcr)
		goto skip;

	mtr->plcr_bit = mrvl_reserve_plcr(&priv->used_plcrs);
	if (mtr->plcr_bit < 0)
		return -rte_mtr_error_set(error, ENOSPC,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL,
					  "Failed to reserve plcr entry\n");

	memset(&params, 0, sizeof(params));
	snprintf(match, sizeof(match), "policer-%d:%d", priv->pp_id,
		 mtr->plcr_bit);
	params.match = match;
	params.token_unit = PP2_CLS_PLCR_BYTES_TOKEN_UNIT;
	params.color_mode = PP2_CLS_PLCR_COLOR_BLIND_MODE;
	params.cir = mtr->profile->profile.srtcm_rfc2697.cir;
	params.cbs = mtr->profile->profile.srtcm_rfc2697.cbs;
	params.ebs = mtr->profile->profile.srtcm_rfc2697.ebs;

	ret = pp2_cls_plcr_init(&params, &mtr->plcr);
	if (ret) {
		priv->used_plcrs &= ~BIT(mtr->plcr_bit);
		mtr->plcr_bit = MRVL_PLCR_BIT_INVALID;

		return -rte_mtr_error_set(error, -ret,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, "Failed to setup policer\n");
	}

	mtr->enabled = 1;
skip:
	/* iterate over flows that have this mtr attached */
	LIST_FOREACH(flow, &priv->flows, next) {
		if (flow->mtr != mtr)
			continue;

		flow->action.plcr = mtr->plcr;

		ret = pp2_cls_tbl_modify_rule(priv->cls_tbl, &flow->rule,
					      &flow->action);
		if (ret)
			return -rte_mtr_error_set(error, -ret,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, "Failed to update cls rule\n");
	}

	return 0;
}

/**
 * Disable meter object.
 *
 * @param dev Pointer to the device.
 * @param mtr Id of the meter.
 * @param error Pointer to the error.
 * @returns 0 on success, negative value otherwise.
 */
static int
mrvl_meter_disable(struct rte_eth_dev *dev, uint32_t mtr_id,
		       struct rte_mtr_error *error)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr *mtr = mrvl_mtr_from_id(priv, mtr_id);
	struct rte_flow *flow;
	int ret;

	if (!mtr)
		return -rte_mtr_error_set(error, ENODEV,
					  RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
					  "Meter id does not exist\n");

	LIST_FOREACH(flow, &priv->flows, next) {
		if (flow->mtr != mtr)
			continue;

		flow->action.plcr = NULL;

		ret = pp2_cls_tbl_modify_rule(priv->cls_tbl, &flow->rule,
					      &flow->action);
		if (ret)
			return -rte_mtr_error_set(error, -ret,
					RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					NULL, "Failed to disable meter\n");
	}

	mtr->enabled = 0;

	return 0;
}

/**
 * Create new meter.
 *
 * @param dev Pointer to the device.
 * @param mtr_id Id of the meter.
 * @param params Pointer to the meter parameters.
 * @param shared Flags indicating whether meter is shared.
 * @param error Pointer to the error.
 * @returns 0 on success, negative value otherwise.
 */
static int
mrvl_create(struct rte_eth_dev *dev, uint32_t mtr_id,
	    struct rte_mtr_params *params, int shared,
	    struct rte_mtr_error *error)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr_profile *profile;
	struct mrvl_mtr *mtr;

	mtr = mrvl_mtr_from_id(priv, mtr_id);
	if (mtr)
		return -rte_mtr_error_set(error, EEXIST,
					  RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
					  "Meter id already exists\n");

	mtr = rte_zmalloc_socket(NULL, sizeof(*mtr), 0, rte_socket_id());
	if (!mtr)
		return -rte_mtr_error_set(error, ENOMEM,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, NULL);

	profile = mrvl_mtr_profile_from_id(priv, params->meter_profile_id);
	if (!profile)
		return -rte_mtr_error_set(error, EINVAL,
					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
					  NULL, "Profile id does not exist\n");

	mtr->shared = shared;
	mtr->mtr_id = mtr_id;
	mtr->plcr_bit = MRVL_PLCR_BIT_INVALID;
	mtr->profile = profile;
	profile->refcnt++;
	LIST_INSERT_HEAD(&priv->mtrs, mtr, next);

	if (params->meter_enable)
		return mrvl_meter_enable(dev, mtr_id, error);

	return 0;
}

/**
 * Destroy meter object.
 *
 * @param dev Pointer to the device.
 * @param mtr_id Id of the meter object.
 * @param error Pointer to the error.
 * @returns 0 on success, negative value otherwise.
 */
static int
mrvl_destroy(struct rte_eth_dev *dev, uint32_t mtr_id,
		 struct rte_mtr_error *error)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr *mtr;

	if (!priv->ppio)
		return -rte_mtr_error_set(error, EPERM,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, "Port is uninitialized\n");

	mtr = mrvl_mtr_from_id(priv, mtr_id);
	if (!mtr)
		return -rte_mtr_error_set(error, EEXIST,
					  RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
					  "Meter id does not exist\n");

	if (mtr->refcnt)
		return -rte_mtr_error_set(error, EPERM,
					  RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
					  "Meter is used\n");

	LIST_REMOVE(mtr, next);
	mtr->profile->refcnt--;

	if (mtr->plcr_bit != MRVL_PLCR_BIT_INVALID)
		priv->used_plcrs &= ~BIT(mtr->plcr_bit);

	if (mtr->plcr)
		pp2_cls_plcr_deinit(mtr->plcr);

	rte_free(mtr);

	return 0;
}

/**
 * Update profile used by the meter.
 *
 * @param dev Pointer to the device.
 * @param mtr_id Id of the meter object.
 * @param error Pointer to the error.
 * @returns 0 on success, negative value otherwise.
 */
static int
mrvl_meter_profile_update(struct rte_eth_dev *dev, uint32_t mtr_id,
			  uint32_t meter_profile_id,
			  struct rte_mtr_error *error)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr_profile *profile;
	struct mrvl_mtr *mtr;
	int ret, enabled = 0;

	if (!priv->ppio)
		return -rte_mtr_error_set(error, EPERM,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
					  NULL, "Port is uninitialized\n");

	mtr = mrvl_mtr_from_id(priv, mtr_id);
	if (!mtr)
		return -rte_mtr_error_set(error, EEXIST,
					  RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
					  "Meter id does not exist\n");

	profile = mrvl_mtr_profile_from_id(priv, meter_profile_id);
	if (!profile)
		return -rte_mtr_error_set(error, EINVAL,
					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
					  NULL, "Profile id does not exist\n");

	ret = mrvl_meter_disable(dev, mtr_id, error);
	if (ret)
		return -rte_mtr_error_set(error, EPERM,
					  RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
					  NULL);

	if (mtr->plcr) {
		enabled = 1;
		pp2_cls_plcr_deinit(mtr->plcr);
		mtr->plcr = NULL;
	}

	mtr->profile->refcnt--;
	mtr->profile = profile;
	profile->refcnt++;

	if (enabled)
		return mrvl_meter_enable(dev, mtr_id, error);

	return 0;
}

const struct rte_mtr_ops mrvl_mtr_ops = {
	.capabilities_get = mrvl_capabilities_get,
	.meter_profile_add = mrvl_meter_profile_add,
	.meter_profile_delete = mrvl_meter_profile_delete,
	.create = mrvl_create,
	.destroy = mrvl_destroy,
	.meter_enable = mrvl_meter_enable,
	.meter_disable = mrvl_meter_disable,
	.meter_profile_update = mrvl_meter_profile_update,
};

/**
 * Initialize metering resources.
 *
 * @param dev Pointer to the device.
 */
void
mrvl_mtr_init(struct rte_eth_dev *dev)
{
	struct mrvl_priv *priv = dev->data->dev_private;

	LIST_INIT(&priv->profiles);
	LIST_INIT(&priv->mtrs);
}

/**
 * Cleanup metering resources.
 *
 * @param dev Pointer to the device.
 */
void
mrvl_mtr_deinit(struct rte_eth_dev *dev)
{
	struct mrvl_priv *priv = dev->data->dev_private;
	struct mrvl_mtr_profile *profile, *tmp_profile;
	struct mrvl_mtr *mtr, *tmp_mtr;

	for (mtr = LIST_FIRST(&priv->mtrs);
	     mtr && (tmp_mtr = LIST_NEXT(mtr, next), 1);
	     mtr = tmp_mtr)
		mrvl_destroy(dev, mtr->mtr_id, NULL);

	for (profile = LIST_FIRST(&priv->profiles);
	     profile && (tmp_profile = LIST_NEXT(profile, next), 1);
	     profile = tmp_profile)
		mrvl_meter_profile_delete(dev, profile->profile_id, NULL);
}