1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Marvell International Ltd.
3 * Copyright(c) 2018 Semihalf.
8 #include <rte_malloc.h>
12 /** Maximum meter rate */
13 #define MRVL_SRTCM_RFC2697_CIR_MAX 1023000
15 /** Invalid plcr bit */
16 #define MRVL_PLCR_BIT_INVALID -1
19 * Return meter object capabilities.
21 * @param dev Pointer to the device (unused).
22 * @param cap Pointer to the meter object capabilities.
23 * @param error Pointer to the error (unused).
27 mrvl_capabilities_get(struct rte_eth_dev *dev __rte_unused,
28 struct rte_mtr_capabilities *cap,
29 struct rte_mtr_error *error __rte_unused)
31 struct rte_mtr_capabilities capa = {
32 .n_max = PP2_CLS_PLCR_NUM,
33 .n_shared_max = PP2_CLS_PLCR_NUM,
34 .shared_n_flows_per_mtr_max = -1,
35 .meter_srtcm_rfc2697_n_max = PP2_CLS_PLCR_NUM,
36 .meter_rate_max = MRVL_SRTCM_RFC2697_CIR_MAX,
39 memcpy(cap, &capa, sizeof(capa));
45 * Get profile using it's id.
47 * @param priv Pointer to the port's private data.
48 * @param meter_profile_id Profile id used by the meter.
49 * @returns Pointer to the profile if exists, NULL otherwise.
51 static struct mrvl_mtr_profile *
52 mrvl_mtr_profile_from_id(struct mrvl_priv *priv, uint32_t meter_profile_id)
54 struct mrvl_mtr_profile *profile = NULL;
56 LIST_FOREACH(profile, &priv->profiles, next)
57 if (profile->profile_id == meter_profile_id)
64 * Add profile to the list of profiles.
66 * @param dev Pointer to the device.
67 * @param meter_profile_id Id of the new profile.
68 * @param profile Pointer to the profile configuration.
69 * @param error Pointer to the error.
70 * @returns 0 on success, negative value otherwise.
73 mrvl_meter_profile_add(struct rte_eth_dev *dev, uint32_t meter_profile_id,
74 struct rte_mtr_meter_profile *profile,
75 struct rte_mtr_error *error)
77 struct mrvl_priv *priv = dev->data->dev_private;
78 struct mrvl_mtr_profile *prof;
81 return -rte_mtr_error_set(error, EINVAL,
82 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
85 if (profile->alg != RTE_MTR_SRTCM_RFC2697)
86 return -rte_mtr_error_set(error, EINVAL,
87 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
89 "Only srTCM RFC 2697 is supported\n");
91 if (profile->packet_mode)
92 return -rte_mtr_error_set(error, EINVAL,
93 RTE_MTR_ERROR_TYPE_METER_PROFILE_PACKET_MODE,
95 "Packet mode is not supported\n");
97 prof = mrvl_mtr_profile_from_id(priv, meter_profile_id);
99 return -rte_mtr_error_set(error, EEXIST,
100 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
101 NULL, "Profile id already exists\n");
103 prof = rte_zmalloc_socket(NULL, sizeof(*prof), 0, rte_socket_id());
105 return -rte_mtr_error_set(error, ENOMEM,
106 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
109 prof->profile_id = meter_profile_id;
110 memcpy(&prof->profile, profile, sizeof(*profile));
112 LIST_INSERT_HEAD(&priv->profiles, prof, next);
118 * Remove profile from the list of profiles.
120 * @param dev Pointer to the device.
121 * @param meter_profile_id Id of the profile to remove.
122 * @param error Pointer to the error.
123 * @returns 0 on success, negative value otherwise.
126 mrvl_meter_profile_delete(struct rte_eth_dev *dev,
127 uint32_t meter_profile_id,
128 struct rte_mtr_error *error)
130 struct mrvl_priv *priv = dev->data->dev_private;
131 struct mrvl_mtr_profile *profile;
133 profile = mrvl_mtr_profile_from_id(priv, meter_profile_id);
135 return -rte_mtr_error_set(error, ENODEV,
136 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
137 NULL, "Profile id does not exist\n");
140 return -rte_mtr_error_set(error, EPERM,
141 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
142 NULL, "Profile is used\n");
144 LIST_REMOVE(profile, next);
151 * Get meter using it's id.
153 * @param priv Pointer to port's private data.
154 * @param mtr_id Id of the meter.
155 * @returns Pointer to the meter if exists, NULL otherwise.
157 static struct mrvl_mtr *
158 mrvl_mtr_from_id(struct mrvl_priv *priv, uint32_t mtr_id)
160 struct mrvl_mtr *mtr = NULL;
162 LIST_FOREACH(mtr, &priv->mtrs, next)
163 if (mtr->mtr_id == mtr_id)
170 * Reserve a policer bit in a bitmap.
172 * @param plcrs Pointer to the policers bitmap.
173 * @returns Reserved bit number on success, negative value otherwise.
176 mrvl_reserve_plcr(uint32_t *plcrs)
180 num = PP2_CLS_PLCR_NUM;
181 if (num > sizeof(uint32_t) * 8) {
182 num = sizeof(uint32_t) * 8;
183 MRVL_LOG(WARNING, "Plcrs number was limited to 32.");
186 for (i = 0; i < num; i++) {
187 uint32_t bit = BIT(i);
189 if (!(*plcrs & bit)) {
200 * Enable meter object.
202 * @param dev Pointer to the device.
203 * @param mtr_id Id of the meter.
204 * @param error Pointer to the error.
205 * @returns 0 in success, negative value otherwise.
208 mrvl_meter_enable(struct rte_eth_dev *dev, uint32_t mtr_id,
209 struct rte_mtr_error *error)
211 struct mrvl_priv *priv = dev->data->dev_private;
212 struct mrvl_mtr *mtr = mrvl_mtr_from_id(priv, mtr_id);
213 struct pp2_cls_plcr_params params;
214 char match[MRVL_MATCH_LEN];
215 struct rte_flow *flow;
219 return -rte_mtr_error_set(error, EPERM,
220 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
221 NULL, "Port is uninitialized\n");
224 return -rte_mtr_error_set(error, ENODEV,
225 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
226 "Meter id does not exist\n");
231 mtr->plcr_bit = mrvl_reserve_plcr(&priv->used_plcrs);
232 if (mtr->plcr_bit < 0)
233 return -rte_mtr_error_set(error, ENOSPC,
234 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
236 "Failed to reserve plcr entry\n");
238 memset(¶ms, 0, sizeof(params));
239 snprintf(match, sizeof(match), "policer-%d:%d", priv->pp_id,
241 params.match = match;
242 params.token_unit = PP2_CLS_PLCR_BYTES_TOKEN_UNIT;
243 params.color_mode = PP2_CLS_PLCR_COLOR_BLIND_MODE;
244 params.cir = mtr->profile->profile.srtcm_rfc2697.cir;
245 params.cbs = mtr->profile->profile.srtcm_rfc2697.cbs;
246 params.ebs = mtr->profile->profile.srtcm_rfc2697.ebs;
248 ret = pp2_cls_plcr_init(¶ms, &mtr->plcr);
250 priv->used_plcrs &= ~BIT(mtr->plcr_bit);
251 mtr->plcr_bit = MRVL_PLCR_BIT_INVALID;
253 return -rte_mtr_error_set(error, -ret,
254 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
255 NULL, "Failed to setup policer\n");
260 /* iterate over flows that have this mtr attached */
261 LIST_FOREACH(flow, &priv->flows, next) {
262 if (flow->mtr != mtr)
265 flow->action.plcr = mtr->plcr;
267 ret = pp2_cls_tbl_modify_rule(priv->cls_tbl, &flow->rule,
270 return -rte_mtr_error_set(error, -ret,
271 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
272 NULL, "Failed to update cls rule\n");
279 * Disable meter object.
281 * @param dev Pointer to the device.
282 * @param mtr Id of the meter.
283 * @param error Pointer to the error.
284 * @returns 0 on success, negative value otherwise.
287 mrvl_meter_disable(struct rte_eth_dev *dev, uint32_t mtr_id,
288 struct rte_mtr_error *error)
290 struct mrvl_priv *priv = dev->data->dev_private;
291 struct mrvl_mtr *mtr = mrvl_mtr_from_id(priv, mtr_id);
292 struct rte_flow *flow;
296 return -rte_mtr_error_set(error, ENODEV,
297 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
298 "Meter id does not exist\n");
300 LIST_FOREACH(flow, &priv->flows, next) {
301 if (flow->mtr != mtr)
304 flow->action.plcr = NULL;
306 ret = pp2_cls_tbl_modify_rule(priv->cls_tbl, &flow->rule,
309 return -rte_mtr_error_set(error, -ret,
310 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
311 NULL, "Failed to disable meter\n");
322 * @param dev Pointer to the device.
323 * @param mtr_id Id of the meter.
324 * @param params Pointer to the meter parameters.
325 * @param shared Flags indicating whether meter is shared.
326 * @param error Pointer to the error.
327 * @returns 0 on success, negative value otherwise.
330 mrvl_create(struct rte_eth_dev *dev, uint32_t mtr_id,
331 struct rte_mtr_params *params, int shared,
332 struct rte_mtr_error *error)
334 struct mrvl_priv *priv = dev->data->dev_private;
335 struct mrvl_mtr_profile *profile;
336 struct mrvl_mtr *mtr;
338 profile = mrvl_mtr_profile_from_id(priv, params->meter_profile_id);
340 return -rte_mtr_error_set(error, EINVAL,
341 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
342 NULL, "Profile id does not exist\n");
344 mtr = mrvl_mtr_from_id(priv, mtr_id);
346 return -rte_mtr_error_set(error, EEXIST,
347 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
348 "Meter id already exists\n");
350 mtr = rte_zmalloc_socket(NULL, sizeof(*mtr), 0, rte_socket_id());
352 return -rte_mtr_error_set(error, ENOMEM,
353 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
356 mtr->shared = shared;
357 mtr->mtr_id = mtr_id;
358 mtr->plcr_bit = MRVL_PLCR_BIT_INVALID;
359 mtr->profile = profile;
361 LIST_INSERT_HEAD(&priv->mtrs, mtr, next);
363 if (params->meter_enable)
364 return mrvl_meter_enable(dev, mtr_id, error);
370 * Destroy meter object.
372 * @param dev Pointer to the device.
373 * @param mtr_id Id of the meter object.
374 * @param error Pointer to the error.
375 * @returns 0 on success, negative value otherwise.
378 mrvl_destroy(struct rte_eth_dev *dev, uint32_t mtr_id,
379 struct rte_mtr_error *error)
381 struct mrvl_priv *priv = dev->data->dev_private;
382 struct mrvl_mtr *mtr;
385 return -rte_mtr_error_set(error, EPERM,
386 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
387 NULL, "Port is uninitialized\n");
389 mtr = mrvl_mtr_from_id(priv, mtr_id);
391 return -rte_mtr_error_set(error, EEXIST,
392 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
393 "Meter id does not exist\n");
396 return -rte_mtr_error_set(error, EPERM,
397 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
400 LIST_REMOVE(mtr, next);
401 mtr->profile->refcnt--;
403 if (mtr->plcr_bit != MRVL_PLCR_BIT_INVALID)
404 priv->used_plcrs &= ~BIT(mtr->plcr_bit);
407 pp2_cls_plcr_deinit(mtr->plcr);
415 * Update profile used by the meter.
417 * @param dev Pointer to the device.
418 * @param mtr_id Id of the meter object.
419 * @param error Pointer to the error.
420 * @returns 0 on success, negative value otherwise.
423 mrvl_meter_profile_update(struct rte_eth_dev *dev, uint32_t mtr_id,
424 uint32_t meter_profile_id,
425 struct rte_mtr_error *error)
427 struct mrvl_priv *priv = dev->data->dev_private;
428 struct mrvl_mtr_profile *profile;
429 struct mrvl_mtr *mtr;
430 int ret, enabled = 0;
433 return -rte_mtr_error_set(error, EPERM,
434 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
435 NULL, "Port is uninitialized\n");
437 mtr = mrvl_mtr_from_id(priv, mtr_id);
439 return -rte_mtr_error_set(error, EEXIST,
440 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
441 "Meter id does not exist\n");
443 profile = mrvl_mtr_profile_from_id(priv, meter_profile_id);
445 return -rte_mtr_error_set(error, EINVAL,
446 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
447 NULL, "Profile id does not exist\n");
449 ret = mrvl_meter_disable(dev, mtr_id, error);
451 return -rte_mtr_error_set(error, EPERM,
452 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
457 pp2_cls_plcr_deinit(mtr->plcr);
461 mtr->profile->refcnt--;
462 mtr->profile = profile;
466 return mrvl_meter_enable(dev, mtr_id, error);
471 const struct rte_mtr_ops mrvl_mtr_ops = {
472 .capabilities_get = mrvl_capabilities_get,
473 .meter_profile_add = mrvl_meter_profile_add,
474 .meter_profile_delete = mrvl_meter_profile_delete,
475 .create = mrvl_create,
476 .destroy = mrvl_destroy,
477 .meter_enable = mrvl_meter_enable,
478 .meter_disable = mrvl_meter_disable,
479 .meter_profile_update = mrvl_meter_profile_update,
483 * Initialize metering resources.
485 * @param dev Pointer to the device.
488 mrvl_mtr_init(struct rte_eth_dev *dev)
490 struct mrvl_priv *priv = dev->data->dev_private;
492 LIST_INIT(&priv->profiles);
493 LIST_INIT(&priv->mtrs);
497 * Cleanup metering resources.
499 * @param dev Pointer to the device.
502 mrvl_mtr_deinit(struct rte_eth_dev *dev)
504 struct mrvl_priv *priv = dev->data->dev_private;
505 struct mrvl_mtr_profile *profile, *tmp_profile;
506 struct mrvl_mtr *mtr, *tmp_mtr;
508 for (mtr = LIST_FIRST(&priv->mtrs);
509 mtr && (tmp_mtr = LIST_NEXT(mtr, next), 1);
511 mrvl_destroy(dev, mtr->mtr_id, NULL);
513 for (profile = LIST_FIRST(&priv->profiles);
514 profile && (tmp_profile = LIST_NEXT(profile, next), 1);
515 profile = tmp_profile)
516 mrvl_meter_profile_delete(dev, profile->profile_id, NULL);