DPDK  23.07.0
rte_meter.h
Go to the documentation of this file.
1 
2 /* SPDX-License-Identifier: BSD-3-Clause
3  * Copyright(c) 2010-2014 Intel Corporation
4  */
5 
6 #ifndef __INCLUDE_RTE_METER_H__
7 #define __INCLUDE_RTE_METER_H__
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
23 #include <stdint.h>
24 
25 /*
26  * Application Programmer's Interface (API)
27  */
28 
32 enum rte_color {
37 };
38 
43  uint64_t cir;
44  uint64_t cbs;
45  uint64_t ebs;
46 };
47 
52  uint64_t cir;
53  uint64_t pir;
54  uint64_t cbs;
55  uint64_t pbs;
56 };
57 
63  uint64_t cir;
64  uint64_t eir;
65  uint64_t cbs;
66  uint64_t ebs;
67 };
68 
73 struct rte_meter_srtcm_profile;
74 
79 struct rte_meter_trtcm_profile;
80 
85 struct rte_meter_trtcm_rfc4115_profile;
86 
88 struct rte_meter_srtcm;
89 
91 struct rte_meter_trtcm;
92 
98 
109 int
110 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
111  struct rte_meter_srtcm_params *params);
112 
123 int
124 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
125  struct rte_meter_trtcm_params *params);
139 int
141  struct rte_meter_trtcm_rfc4115_profile *p,
142  struct rte_meter_trtcm_rfc4115_params *params);
143 
154 int
155 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
156  struct rte_meter_srtcm_profile *p);
157 
168 int
170  struct rte_meter_trtcm_profile *p);
171 
185 int
187  struct rte_meter_trtcm_rfc4115_profile *p);
188 
203 static inline enum rte_color
204 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
205  struct rte_meter_srtcm_profile *p,
206  uint64_t time,
207  uint32_t pkt_len);
208 
225 static inline enum rte_color
226 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
227  struct rte_meter_srtcm_profile *p,
228  uint64_t time,
229  uint32_t pkt_len,
230  enum rte_color pkt_color);
231 
246 static inline enum rte_color
248  struct rte_meter_trtcm_profile *p,
249  uint64_t time,
250  uint32_t pkt_len);
251 
268 static inline enum rte_color
270  struct rte_meter_trtcm_profile *p,
271  uint64_t time,
272  uint32_t pkt_len,
273  enum rte_color pkt_color);
274 
292 static inline enum rte_color
294  struct rte_meter_trtcm_rfc4115 *m,
295  struct rte_meter_trtcm_rfc4115_profile *p,
296  uint64_t time,
297  uint32_t pkt_len);
298 
318 static inline enum rte_color
320  struct rte_meter_trtcm_rfc4115 *m,
321  struct rte_meter_trtcm_rfc4115_profile *p,
322  uint64_t time,
323  uint32_t pkt_len,
324  enum rte_color pkt_color);
325 
326 /*
327  * Inline implementation of run-time methods
328  */
329 
330 struct rte_meter_srtcm_profile {
331  uint64_t cbs;
333  uint64_t ebs;
335  uint64_t cir_period;
337  uint64_t cir_bytes_per_period;
339 };
340 
341 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
342 struct rte_meter_srtcm {
343  uint64_t time; /* Time of latest update of C and E token buckets */
344  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
345  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
346 };
347 
348 struct rte_meter_trtcm_profile {
349  uint64_t cbs;
351  uint64_t pbs;
353  uint64_t cir_period;
355  uint64_t cir_bytes_per_period;
357  uint64_t pir_period;
359  uint64_t pir_bytes_per_period;
361 };
362 
368  uint64_t time_tc;
370  uint64_t time_tp;
372  uint64_t tc;
374  uint64_t tp;
376 };
377 
378 struct rte_meter_trtcm_rfc4115_profile {
379  uint64_t cbs;
381  uint64_t ebs;
383  uint64_t cir_period;
385  uint64_t cir_bytes_per_period;
387  uint64_t eir_period;
389  uint64_t eir_bytes_per_period;
391 };
392 
398  uint64_t time_tc;
400  uint64_t time_te;
402  uint64_t tc;
404  uint64_t te;
406 };
407 
408 static inline enum rte_color
409 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
410  struct rte_meter_srtcm_profile *p,
411  uint64_t time,
412  uint32_t pkt_len)
413 {
414  uint64_t time_diff, n_periods, tc, te;
415 
416  /* Bucket update */
417  time_diff = time - m->time;
418  n_periods = time_diff / p->cir_period;
419  m->time += n_periods * p->cir_period;
420 
421  /* Put the tokens overflowing from tc into te bucket */
422  tc = m->tc + n_periods * p->cir_bytes_per_period;
423  te = m->te;
424  if (tc > p->cbs) {
425  te += (tc - p->cbs);
426  if (te > p->ebs)
427  te = p->ebs;
428  tc = p->cbs;
429  }
430 
431  /* Color logic */
432  if (tc >= pkt_len) {
433  m->tc = tc - pkt_len;
434  m->te = te;
435  return RTE_COLOR_GREEN;
436  }
437 
438  if (te >= pkt_len) {
439  m->tc = tc;
440  m->te = te - pkt_len;
441  return RTE_COLOR_YELLOW;
442  }
443 
444  m->tc = tc;
445  m->te = te;
446  return RTE_COLOR_RED;
447 }
448 
449 static inline enum rte_color
450 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
451  struct rte_meter_srtcm_profile *p,
452  uint64_t time,
453  uint32_t pkt_len,
454  enum rte_color pkt_color)
455 {
456  uint64_t time_diff, n_periods, tc, te;
457 
458  /* Bucket update */
459  time_diff = time - m->time;
460  n_periods = time_diff / p->cir_period;
461  m->time += n_periods * p->cir_period;
462 
463  /* Put the tokens overflowing from tc into te bucket */
464  tc = m->tc + n_periods * p->cir_bytes_per_period;
465  te = m->te;
466  if (tc > p->cbs) {
467  te += (tc - p->cbs);
468  if (te > p->ebs)
469  te = p->ebs;
470  tc = p->cbs;
471  }
472 
473  /* Color logic */
474  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
475  m->tc = tc - pkt_len;
476  m->te = te;
477  return RTE_COLOR_GREEN;
478  }
479 
480  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
481  m->tc = tc;
482  m->te = te - pkt_len;
483  return RTE_COLOR_YELLOW;
484  }
485 
486  m->tc = tc;
487  m->te = te;
488  return RTE_COLOR_RED;
489 }
490 
491 static inline enum rte_color
493  struct rte_meter_trtcm_profile *p,
494  uint64_t time,
495  uint32_t pkt_len)
496 {
497  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
498 
499  /* Bucket update */
500  time_diff_tc = time - m->time_tc;
501  time_diff_tp = time - m->time_tp;
502  n_periods_tc = time_diff_tc / p->cir_period;
503  n_periods_tp = time_diff_tp / p->pir_period;
504  m->time_tc += n_periods_tc * p->cir_period;
505  m->time_tp += n_periods_tp * p->pir_period;
506 
507  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
508  if (tc > p->cbs)
509  tc = p->cbs;
510 
511  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
512  if (tp > p->pbs)
513  tp = p->pbs;
514 
515  /* Color logic */
516  if (tp < pkt_len) {
517  m->tc = tc;
518  m->tp = tp;
519  return RTE_COLOR_RED;
520  }
521 
522  if (tc < pkt_len) {
523  m->tc = tc;
524  m->tp = tp - pkt_len;
525  return RTE_COLOR_YELLOW;
526  }
527 
528  m->tc = tc - pkt_len;
529  m->tp = tp - pkt_len;
530  return RTE_COLOR_GREEN;
531 }
532 
533 static inline enum rte_color
535  struct rte_meter_trtcm_profile *p,
536  uint64_t time,
537  uint32_t pkt_len,
538  enum rte_color pkt_color)
539 {
540  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
541 
542  /* Bucket update */
543  time_diff_tc = time - m->time_tc;
544  time_diff_tp = time - m->time_tp;
545  n_periods_tc = time_diff_tc / p->cir_period;
546  n_periods_tp = time_diff_tp / p->pir_period;
547  m->time_tc += n_periods_tc * p->cir_period;
548  m->time_tp += n_periods_tp * p->pir_period;
549 
550  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
551  if (tc > p->cbs)
552  tc = p->cbs;
553 
554  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
555  if (tp > p->pbs)
556  tp = p->pbs;
557 
558  /* Color logic */
559  if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
560  m->tc = tc;
561  m->tp = tp;
562  return RTE_COLOR_RED;
563  }
564 
565  if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
566  m->tc = tc;
567  m->tp = tp - pkt_len;
568  return RTE_COLOR_YELLOW;
569  }
570 
571  m->tc = tc - pkt_len;
572  m->tp = tp - pkt_len;
573  return RTE_COLOR_GREEN;
574 }
575 
576 static inline enum rte_color
578  struct rte_meter_trtcm_rfc4115 *m,
579  struct rte_meter_trtcm_rfc4115_profile *p,
580  uint64_t time,
581  uint32_t pkt_len)
582 {
583  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
584 
585  /* Bucket update */
586  time_diff_tc = time - m->time_tc;
587  time_diff_te = time - m->time_te;
588  n_periods_tc = time_diff_tc / p->cir_period;
589  n_periods_te = time_diff_te / p->eir_period;
590  m->time_tc += n_periods_tc * p->cir_period;
591  m->time_te += n_periods_te * p->eir_period;
592 
593  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
594  if (tc > p->cbs)
595  tc = p->cbs;
596 
597  te = m->te + n_periods_te * p->eir_bytes_per_period;
598  if (te > p->ebs)
599  te = p->ebs;
600 
601  /* Color logic */
602  if (tc >= pkt_len) {
603  m->tc = tc - pkt_len;
604  m->te = te;
605  return RTE_COLOR_GREEN;
606  }
607  if (te >= pkt_len) {
608  m->tc = tc;
609  m->te = te - pkt_len;
610  return RTE_COLOR_YELLOW;
611  }
612 
613  /* If we end up here the color is RED */
614  m->tc = tc;
615  m->te = te;
616  return RTE_COLOR_RED;
617 }
618 
619 static inline enum rte_color
621  struct rte_meter_trtcm_rfc4115 *m,
622  struct rte_meter_trtcm_rfc4115_profile *p,
623  uint64_t time,
624  uint32_t pkt_len,
625  enum rte_color pkt_color)
626 {
627  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
628 
629  /* Bucket update */
630  time_diff_tc = time - m->time_tc;
631  time_diff_te = time - m->time_te;
632  n_periods_tc = time_diff_tc / p->cir_period;
633  n_periods_te = time_diff_te / p->eir_period;
634  m->time_tc += n_periods_tc * p->cir_period;
635  m->time_te += n_periods_te * p->eir_period;
636 
637  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
638  if (tc > p->cbs)
639  tc = p->cbs;
640 
641  te = m->te + n_periods_te * p->eir_bytes_per_period;
642  if (te > p->ebs)
643  te = p->ebs;
644 
645  /* Color logic */
646  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
647  m->tc = tc - pkt_len;
648  m->te = te;
649  return RTE_COLOR_GREEN;
650  }
651 
652  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
653  m->tc = tc;
654  m->te = te - pkt_len;
655  return RTE_COLOR_YELLOW;
656  }
657 
658  /* If we end up here the color is RED */
659  m->tc = tc;
660  m->te = te;
661  return RTE_COLOR_RED;
662 }
663 
664 
665 #ifdef __cplusplus
666 }
667 #endif
668 
669 #endif /* __INCLUDE_RTE_METER_H__ */
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:409
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:577
int rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p, struct rte_meter_trtcm_params *params)
int rte_meter_srtcm_config(struct rte_meter_srtcm *m, struct rte_meter_srtcm_profile *p)
uint64_t tc
Definition: rte_meter.h:372
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:492
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:450
int rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p)
uint64_t time_tc
Definition: rte_meter.h:368
uint64_t tp
Definition: rte_meter.h:374
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:534
int rte_meter_trtcm_rfc4115_config(struct rte_meter_trtcm_rfc4115 *m, struct rte_meter_trtcm_rfc4115_profile *p)
int rte_meter_trtcm_rfc4115_profile_config(struct rte_meter_trtcm_rfc4115_profile *p, struct rte_meter_trtcm_rfc4115_params *params)
uint64_t time_tp
Definition: rte_meter.h:370
rte_color
Definition: rte_meter.h:32
int rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p, struct rte_meter_srtcm_params *params)
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:620