DPDK  20.05.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 
24 #include <stdint.h>
25 
26 #include "rte_compat.h"
27 
28 /*
29  * Application Programmer's Interface (API)
30  *
31  ***/
32 
36 enum rte_color {
41 };
42 
47  uint64_t cir;
48  uint64_t cbs;
49  uint64_t ebs;
50 };
51 
56  uint64_t cir;
57  uint64_t pir;
58  uint64_t cbs;
59  uint64_t pbs;
60 };
61 
67  uint64_t cir;
68  uint64_t eir;
69  uint64_t cbs;
70  uint64_t ebs;
71 };
72 
77 struct rte_meter_srtcm_profile;
78 
83 struct rte_meter_trtcm_profile;
84 
89 struct rte_meter_trtcm_rfc4115_profile;
90 
92 struct rte_meter_srtcm;
93 
95 struct rte_meter_trtcm;
96 
102 
113 int
114 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
115  struct rte_meter_srtcm_params *params);
116 
127 int
128 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
129  struct rte_meter_trtcm_params *params);
143 int
145  struct rte_meter_trtcm_rfc4115_profile *p,
146  struct rte_meter_trtcm_rfc4115_params *params);
147 
158 int
159 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
160  struct rte_meter_srtcm_profile *p);
161 
172 int
174  struct rte_meter_trtcm_profile *p);
175 
189 int
191  struct rte_meter_trtcm_rfc4115_profile *p);
192 
207 static inline enum rte_color
208 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
209  struct rte_meter_srtcm_profile *p,
210  uint64_t time,
211  uint32_t pkt_len);
212 
229 static inline enum rte_color
230 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
231  struct rte_meter_srtcm_profile *p,
232  uint64_t time,
233  uint32_t pkt_len,
234  enum rte_color pkt_color);
235 
250 static inline enum rte_color
252  struct rte_meter_trtcm_profile *p,
253  uint64_t time,
254  uint32_t pkt_len);
255 
272 static inline enum rte_color
274  struct rte_meter_trtcm_profile *p,
275  uint64_t time,
276  uint32_t pkt_len,
277  enum rte_color pkt_color);
278 
296 static inline enum rte_color
298  struct rte_meter_trtcm_rfc4115 *m,
299  struct rte_meter_trtcm_rfc4115_profile *p,
300  uint64_t time,
301  uint32_t pkt_len);
302 
322 static inline enum rte_color
324  struct rte_meter_trtcm_rfc4115 *m,
325  struct rte_meter_trtcm_rfc4115_profile *p,
326  uint64_t time,
327  uint32_t pkt_len,
328  enum rte_color pkt_color);
329 
330 /*
331  * Inline implementation of run-time methods
332  *
333  ***/
334 
335 struct rte_meter_srtcm_profile {
336  uint64_t cbs;
338  uint64_t ebs;
340  uint64_t cir_period;
342  uint64_t cir_bytes_per_period;
344 };
345 
346 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
347 struct rte_meter_srtcm {
348  uint64_t time; /* Time of latest update of C and E token buckets */
349  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
350  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
351 };
352 
353 struct rte_meter_trtcm_profile {
354  uint64_t cbs;
356  uint64_t pbs;
358  uint64_t cir_period;
360  uint64_t cir_bytes_per_period;
362  uint64_t pir_period;
364  uint64_t pir_bytes_per_period;
366 };
367 
373  uint64_t time_tc;
375  uint64_t time_tp;
377  uint64_t tc;
379  uint64_t tp;
381 };
382 
383 struct rte_meter_trtcm_rfc4115_profile {
384  uint64_t cbs;
386  uint64_t ebs;
388  uint64_t cir_period;
390  uint64_t cir_bytes_per_period;
392  uint64_t eir_period;
394  uint64_t eir_bytes_per_period;
396 };
397 
403  uint64_t time_tc;
405  uint64_t time_te;
407  uint64_t tc;
409  uint64_t te;
411 };
412 
413 static inline enum rte_color
414 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
415  struct rte_meter_srtcm_profile *p,
416  uint64_t time,
417  uint32_t pkt_len)
418 {
419  uint64_t time_diff, n_periods, tc, te;
420 
421  /* Bucket update */
422  time_diff = time - m->time;
423  n_periods = time_diff / p->cir_period;
424  m->time += n_periods * p->cir_period;
425 
426  /* Put the tokens overflowing from tc into te bucket */
427  tc = m->tc + n_periods * p->cir_bytes_per_period;
428  te = m->te;
429  if (tc > p->cbs) {
430  te += (tc - p->cbs);
431  if (te > p->ebs)
432  te = p->ebs;
433  tc = p->cbs;
434  }
435 
436  /* Color logic */
437  if (tc >= pkt_len) {
438  m->tc = tc - pkt_len;
439  m->te = te;
440  return RTE_COLOR_GREEN;
441  }
442 
443  if (te >= pkt_len) {
444  m->tc = tc;
445  m->te = te - pkt_len;
446  return RTE_COLOR_YELLOW;
447  }
448 
449  m->tc = tc;
450  m->te = te;
451  return RTE_COLOR_RED;
452 }
453 
454 static inline enum rte_color
455 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
456  struct rte_meter_srtcm_profile *p,
457  uint64_t time,
458  uint32_t pkt_len,
459  enum rte_color pkt_color)
460 {
461  uint64_t time_diff, n_periods, tc, te;
462 
463  /* Bucket update */
464  time_diff = time - m->time;
465  n_periods = time_diff / p->cir_period;
466  m->time += n_periods * p->cir_period;
467 
468  /* Put the tokens overflowing from tc into te bucket */
469  tc = m->tc + n_periods * p->cir_bytes_per_period;
470  te = m->te;
471  if (tc > p->cbs) {
472  te += (tc - p->cbs);
473  if (te > p->ebs)
474  te = p->ebs;
475  tc = p->cbs;
476  }
477 
478  /* Color logic */
479  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
480  m->tc = tc - pkt_len;
481  m->te = te;
482  return RTE_COLOR_GREEN;
483  }
484 
485  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
486  m->tc = tc;
487  m->te = te - pkt_len;
488  return RTE_COLOR_YELLOW;
489  }
490 
491  m->tc = tc;
492  m->te = te;
493  return RTE_COLOR_RED;
494 }
495 
496 static inline enum rte_color
498  struct rte_meter_trtcm_profile *p,
499  uint64_t time,
500  uint32_t pkt_len)
501 {
502  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
503 
504  /* Bucket update */
505  time_diff_tc = time - m->time_tc;
506  time_diff_tp = time - m->time_tp;
507  n_periods_tc = time_diff_tc / p->cir_period;
508  n_periods_tp = time_diff_tp / p->pir_period;
509  m->time_tc += n_periods_tc * p->cir_period;
510  m->time_tp += n_periods_tp * p->pir_period;
511 
512  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
513  if (tc > p->cbs)
514  tc = p->cbs;
515 
516  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
517  if (tp > p->pbs)
518  tp = p->pbs;
519 
520  /* Color logic */
521  if (tp < pkt_len) {
522  m->tc = tc;
523  m->tp = tp;
524  return RTE_COLOR_RED;
525  }
526 
527  if (tc < pkt_len) {
528  m->tc = tc;
529  m->tp = tp - pkt_len;
530  return RTE_COLOR_YELLOW;
531  }
532 
533  m->tc = tc - pkt_len;
534  m->tp = tp - pkt_len;
535  return RTE_COLOR_GREEN;
536 }
537 
538 static inline enum rte_color
540  struct rte_meter_trtcm_profile *p,
541  uint64_t time,
542  uint32_t pkt_len,
543  enum rte_color pkt_color)
544 {
545  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
546 
547  /* Bucket update */
548  time_diff_tc = time - m->time_tc;
549  time_diff_tp = time - m->time_tp;
550  n_periods_tc = time_diff_tc / p->cir_period;
551  n_periods_tp = time_diff_tp / p->pir_period;
552  m->time_tc += n_periods_tc * p->cir_period;
553  m->time_tp += n_periods_tp * p->pir_period;
554 
555  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
556  if (tc > p->cbs)
557  tc = p->cbs;
558 
559  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
560  if (tp > p->pbs)
561  tp = p->pbs;
562 
563  /* Color logic */
564  if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
565  m->tc = tc;
566  m->tp = tp;
567  return RTE_COLOR_RED;
568  }
569 
570  if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
571  m->tc = tc;
572  m->tp = tp - pkt_len;
573  return RTE_COLOR_YELLOW;
574  }
575 
576  m->tc = tc - pkt_len;
577  m->tp = tp - pkt_len;
578  return RTE_COLOR_GREEN;
579 }
580 
581 static inline enum rte_color
583  struct rte_meter_trtcm_rfc4115 *m,
584  struct rte_meter_trtcm_rfc4115_profile *p,
585  uint64_t time,
586  uint32_t pkt_len)
587 {
588  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
589 
590  /* Bucket update */
591  time_diff_tc = time - m->time_tc;
592  time_diff_te = time - m->time_te;
593  n_periods_tc = time_diff_tc / p->cir_period;
594  n_periods_te = time_diff_te / p->eir_period;
595  m->time_tc += n_periods_tc * p->cir_period;
596  m->time_te += n_periods_te * p->eir_period;
597 
598  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
599  if (tc > p->cbs)
600  tc = p->cbs;
601 
602  te = m->te + n_periods_te * p->eir_bytes_per_period;
603  if (te > p->ebs)
604  te = p->ebs;
605 
606  /* Color logic */
607  if (tc >= pkt_len) {
608  m->tc = tc - pkt_len;
609  m->te = te;
610  return RTE_COLOR_GREEN;
611  }
612  if (te >= pkt_len) {
613  m->tc = tc;
614  m->te = te - pkt_len;
615  return RTE_COLOR_YELLOW;
616  }
617 
618  /* If we end up here the color is RED */
619  m->tc = tc;
620  m->te = te;
621  return RTE_COLOR_RED;
622 }
623 
624 static inline enum rte_color
626  struct rte_meter_trtcm_rfc4115 *m,
627  struct rte_meter_trtcm_rfc4115_profile *p,
628  uint64_t time,
629  uint32_t pkt_len,
630  enum rte_color pkt_color)
631 {
632  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
633 
634  /* Bucket update */
635  time_diff_tc = time - m->time_tc;
636  time_diff_te = time - m->time_te;
637  n_periods_tc = time_diff_tc / p->cir_period;
638  n_periods_te = time_diff_te / p->eir_period;
639  m->time_tc += n_periods_tc * p->cir_period;
640  m->time_te += n_periods_te * p->eir_period;
641 
642  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
643  if (tc > p->cbs)
644  tc = p->cbs;
645 
646  te = m->te + n_periods_te * p->eir_bytes_per_period;
647  if (te > p->ebs)
648  te = p->ebs;
649 
650  /* Color logic */
651  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
652  m->tc = tc - pkt_len;
653  m->te = te;
654  return RTE_COLOR_GREEN;
655  }
656 
657  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
658  m->tc = tc;
659  m->te = te - pkt_len;
660  return RTE_COLOR_YELLOW;
661  }
662 
663  /* If we end up here the color is RED */
664  m->tc = tc;
665  m->te = te;
666  return RTE_COLOR_RED;
667 }
668 
669 
670 #ifdef __cplusplus
671 }
672 #endif
673 
674 #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:414
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:582
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:377
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:497
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:455
int rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p)
uint64_t time_tc
Definition: rte_meter.h:373
uint64_t tp
Definition: rte_meter.h:379
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:539
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:375
rte_color
Definition: rte_meter.h:36
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:625