DPDK 25.11.0
rte_meter.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5#ifndef __INCLUDE_RTE_METER_H__
6#define __INCLUDE_RTE_METER_H__
7
18#include <stdint.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/*
25 * Application Programmer's Interface (API)
26 */
27
36};
37
42 uint64_t cir;
43 uint64_t cbs;
44 uint64_t ebs;
45};
46
51 uint64_t cir;
52 uint64_t pir;
53 uint64_t cbs;
54 uint64_t pbs;
55};
56
62 uint64_t cir;
63 uint64_t eir;
64 uint64_t cbs;
65 uint64_t ebs;
66};
67
72struct rte_meter_srtcm_profile;
73
78struct rte_meter_trtcm_profile;
79
84struct rte_meter_trtcm_rfc4115_profile;
85
87struct rte_meter_srtcm;
88
90struct rte_meter_trtcm;
91
97
108int
109rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
110 struct rte_meter_srtcm_params *params);
111
122int
123rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
124 struct rte_meter_trtcm_params *params);
135int
137 struct rte_meter_trtcm_rfc4115_profile *p,
138 struct rte_meter_trtcm_rfc4115_params *params);
139
150int
151rte_meter_srtcm_config(struct rte_meter_srtcm *m,
152 struct rte_meter_srtcm_profile *p);
153
164int
166 struct rte_meter_trtcm_profile *p);
167
178int
180 struct rte_meter_trtcm_rfc4115_profile *p);
181
196static inline enum rte_color
197rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
198 struct rte_meter_srtcm_profile *p,
199 uint64_t time,
200 uint32_t pkt_len);
201
218static inline enum rte_color
219rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
220 struct rte_meter_srtcm_profile *p,
221 uint64_t time,
222 uint32_t pkt_len,
223 enum rte_color pkt_color);
224
239static inline enum rte_color
241 struct rte_meter_trtcm_profile *p,
242 uint64_t time,
243 uint32_t pkt_len);
244
261static inline enum rte_color
263 struct rte_meter_trtcm_profile *p,
264 uint64_t time,
265 uint32_t pkt_len,
266 enum rte_color pkt_color);
267
282static inline enum rte_color
284 struct rte_meter_trtcm_rfc4115 *m,
285 struct rte_meter_trtcm_rfc4115_profile *p,
286 uint64_t time,
287 uint32_t pkt_len);
288
305static inline enum rte_color
307 struct rte_meter_trtcm_rfc4115 *m,
308 struct rte_meter_trtcm_rfc4115_profile *p,
309 uint64_t time,
310 uint32_t pkt_len,
311 enum rte_color pkt_color);
312
313/*
314 * Inline implementation of run-time methods
315 */
316
317struct rte_meter_srtcm_profile {
318 uint64_t cbs;
320 uint64_t ebs;
322 uint64_t cir_period;
324 uint64_t cir_bytes_per_period;
326};
327
328/* Internal data structure storing the srTCM run-time context per metered traffic flow. */
329struct rte_meter_srtcm {
330 uint64_t time; /* Time of latest update of C and E token buckets */
331 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
332 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
333};
334
335struct rte_meter_trtcm_profile {
336 uint64_t cbs;
338 uint64_t pbs;
340 uint64_t cir_period;
342 uint64_t cir_bytes_per_period;
344 uint64_t pir_period;
346 uint64_t pir_bytes_per_period;
348};
349
355 uint64_t time_tc;
357 uint64_t time_tp;
359 uint64_t tc;
361 uint64_t tp;
363};
364
365struct rte_meter_trtcm_rfc4115_profile {
366 uint64_t cbs;
368 uint64_t ebs;
370 uint64_t cir_period;
372 uint64_t cir_bytes_per_period;
374 uint64_t eir_period;
376 uint64_t eir_bytes_per_period;
378};
379
385 uint64_t time_tc;
387 uint64_t time_te;
389 uint64_t tc;
391 uint64_t te;
393};
394
395static inline enum rte_color
396rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
397 struct rte_meter_srtcm_profile *p,
398 uint64_t time,
399 uint32_t pkt_len)
400{
401 uint64_t time_diff, n_periods, tc, te;
402
403 /* Bucket update */
404 time_diff = time - m->time;
405 n_periods = time_diff / p->cir_period;
406 m->time += n_periods * p->cir_period;
407
408 /* Put the tokens overflowing from tc into te bucket */
409 tc = m->tc + n_periods * p->cir_bytes_per_period;
410 te = m->te;
411 if (tc > p->cbs) {
412 te += (tc - p->cbs);
413 if (te > p->ebs)
414 te = p->ebs;
415 tc = p->cbs;
416 }
417
418 /* Color logic */
419 if (tc >= pkt_len) {
420 m->tc = tc - pkt_len;
421 m->te = te;
422 return RTE_COLOR_GREEN;
423 }
424
425 if (te >= pkt_len) {
426 m->tc = tc;
427 m->te = te - pkt_len;
428 return RTE_COLOR_YELLOW;
429 }
430
431 m->tc = tc;
432 m->te = te;
433 return RTE_COLOR_RED;
434}
435
436static inline enum rte_color
437rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
438 struct rte_meter_srtcm_profile *p,
439 uint64_t time,
440 uint32_t pkt_len,
441 enum rte_color pkt_color)
442{
443 uint64_t time_diff, n_periods, tc, te;
444
445 /* Bucket update */
446 time_diff = time - m->time;
447 n_periods = time_diff / p->cir_period;
448 m->time += n_periods * p->cir_period;
449
450 /* Put the tokens overflowing from tc into te bucket */
451 tc = m->tc + n_periods * p->cir_bytes_per_period;
452 te = m->te;
453 if (tc > p->cbs) {
454 te += (tc - p->cbs);
455 if (te > p->ebs)
456 te = p->ebs;
457 tc = p->cbs;
458 }
459
460 /* Color logic */
461 if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
462 m->tc = tc - pkt_len;
463 m->te = te;
464 return RTE_COLOR_GREEN;
465 }
466
467 if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
468 m->tc = tc;
469 m->te = te - pkt_len;
470 return RTE_COLOR_YELLOW;
471 }
472
473 m->tc = tc;
474 m->te = te;
475 return RTE_COLOR_RED;
476}
477
478static inline enum rte_color
480 struct rte_meter_trtcm_profile *p,
481 uint64_t time,
482 uint32_t pkt_len)
483{
484 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
485
486 /* Bucket update */
487 time_diff_tc = time - m->time_tc;
488 time_diff_tp = time - m->time_tp;
489 n_periods_tc = time_diff_tc / p->cir_period;
490 n_periods_tp = time_diff_tp / p->pir_period;
491 m->time_tc += n_periods_tc * p->cir_period;
492 m->time_tp += n_periods_tp * p->pir_period;
493
494 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
495 if (tc > p->cbs)
496 tc = p->cbs;
497
498 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
499 if (tp > p->pbs)
500 tp = p->pbs;
501
502 /* Color logic */
503 if (tp < pkt_len) {
504 m->tc = tc;
505 m->tp = tp;
506 return RTE_COLOR_RED;
507 }
508
509 if (tc < pkt_len) {
510 m->tc = tc;
511 m->tp = tp - pkt_len;
512 return RTE_COLOR_YELLOW;
513 }
514
515 m->tc = tc - pkt_len;
516 m->tp = tp - pkt_len;
517 return RTE_COLOR_GREEN;
518}
519
520static inline enum rte_color
522 struct rte_meter_trtcm_profile *p,
523 uint64_t time,
524 uint32_t pkt_len,
525 enum rte_color pkt_color)
526{
527 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
528
529 /* Bucket update */
530 time_diff_tc = time - m->time_tc;
531 time_diff_tp = time - m->time_tp;
532 n_periods_tc = time_diff_tc / p->cir_period;
533 n_periods_tp = time_diff_tp / p->pir_period;
534 m->time_tc += n_periods_tc * p->cir_period;
535 m->time_tp += n_periods_tp * p->pir_period;
536
537 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
538 if (tc > p->cbs)
539 tc = p->cbs;
540
541 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
542 if (tp > p->pbs)
543 tp = p->pbs;
544
545 /* Color logic */
546 if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
547 m->tc = tc;
548 m->tp = tp;
549 return RTE_COLOR_RED;
550 }
551
552 if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
553 m->tc = tc;
554 m->tp = tp - pkt_len;
555 return RTE_COLOR_YELLOW;
556 }
557
558 m->tc = tc - pkt_len;
559 m->tp = tp - pkt_len;
560 return RTE_COLOR_GREEN;
561}
562
563static inline enum rte_color
565 struct rte_meter_trtcm_rfc4115 *m,
566 struct rte_meter_trtcm_rfc4115_profile *p,
567 uint64_t time,
568 uint32_t pkt_len)
569{
570 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
571
572 /* Bucket update */
573 time_diff_tc = time - m->time_tc;
574 time_diff_te = time - m->time_te;
575 n_periods_tc = time_diff_tc / p->cir_period;
576 n_periods_te = time_diff_te / p->eir_period;
577 m->time_tc += n_periods_tc * p->cir_period;
578 m->time_te += n_periods_te * p->eir_period;
579
580 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
581 if (tc > p->cbs)
582 tc = p->cbs;
583
584 te = m->te + n_periods_te * p->eir_bytes_per_period;
585 if (te > p->ebs)
586 te = p->ebs;
587
588 /* Color logic */
589 if (tc >= pkt_len) {
590 m->tc = tc - pkt_len;
591 m->te = te;
592 return RTE_COLOR_GREEN;
593 }
594 if (te >= pkt_len) {
595 m->tc = tc;
596 m->te = te - pkt_len;
597 return RTE_COLOR_YELLOW;
598 }
599
600 /* If we end up here the color is RED */
601 m->tc = tc;
602 m->te = te;
603 return RTE_COLOR_RED;
604}
605
606static inline enum rte_color
608 struct rte_meter_trtcm_rfc4115 *m,
609 struct rte_meter_trtcm_rfc4115_profile *p,
610 uint64_t time,
611 uint32_t pkt_len,
612 enum rte_color pkt_color)
613{
614 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
615
616 /* Bucket update */
617 time_diff_tc = time - m->time_tc;
618 time_diff_te = time - m->time_te;
619 n_periods_tc = time_diff_tc / p->cir_period;
620 n_periods_te = time_diff_te / p->eir_period;
621 m->time_tc += n_periods_tc * p->cir_period;
622 m->time_te += n_periods_te * p->eir_period;
623
624 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
625 if (tc > p->cbs)
626 tc = p->cbs;
627
628 te = m->te + n_periods_te * p->eir_bytes_per_period;
629 if (te > p->ebs)
630 te = p->ebs;
631
632 /* Color logic */
633 if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
634 m->tc = tc - pkt_len;
635 m->te = te;
636 return RTE_COLOR_GREEN;
637 }
638
639 if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
640 m->tc = tc;
641 m->te = te - pkt_len;
642 return RTE_COLOR_YELLOW;
643 }
644
645 /* If we end up here the color is RED */
646 m->tc = tc;
647 m->te = te;
648 return RTE_COLOR_RED;
649}
650
651
652#ifdef __cplusplus
653}
654#endif
655
656#endif /* __INCLUDE_RTE_METER_H__ */
static enum rte_color rte_meter_trtcm_rfc4115_color_blind_check(struct rte_meter_trtcm_rfc4115 *m, struct rte_meter_trtcm_rfc4115_profile *p, uint64_t time, uint32_t pkt_len)
Definition: rte_meter.h:564
static enum rte_color rte_meter_trtcm_rfc4115_color_aware_check(struct rte_meter_trtcm_rfc4115 *m, struct rte_meter_trtcm_rfc4115_profile *p, uint64_t time, uint32_t pkt_len, enum rte_color pkt_color)
Definition: rte_meter.h:607
static enum rte_color rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p, uint64_t time, uint32_t pkt_len)
Definition: rte_meter.h:479
static enum rte_color rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p, uint64_t time, uint32_t pkt_len, enum rte_color pkt_color)
Definition: rte_meter.h:521
rte_color
Definition: rte_meter.h:31
@ RTE_COLOR_GREEN
Definition: rte_meter.h:32
@ RTE_COLOR_YELLOW
Definition: rte_meter.h:33
@ RTE_COLOR_RED
Definition: rte_meter.h:34
@ RTE_COLORS
Definition: rte_meter.h:35
int rte_meter_trtcm_rfc4115_profile_config(struct rte_meter_trtcm_rfc4115_profile *p, struct rte_meter_trtcm_rfc4115_params *params)
int rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p, struct rte_meter_srtcm_params *params)
int rte_meter_srtcm_config(struct rte_meter_srtcm *m, struct rte_meter_srtcm_profile *p)
int rte_meter_trtcm_rfc4115_config(struct rte_meter_trtcm_rfc4115 *m, struct rte_meter_trtcm_rfc4115_profile *p)
static enum rte_color rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m, struct rte_meter_srtcm_profile *p, uint64_t time, uint32_t pkt_len, enum rte_color pkt_color)
Definition: rte_meter.h:437
int rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p)
int rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p, struct rte_meter_trtcm_params *params)
static enum rte_color rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m, struct rte_meter_srtcm_profile *p, uint64_t time, uint32_t pkt_len)
Definition: rte_meter.h:396
uint64_t time_tp
Definition: rte_meter.h:357
uint64_t tc
Definition: rte_meter.h:359
uint64_t tp
Definition: rte_meter.h:361
uint64_t time_tc
Definition: rte_meter.h:355