DPDK  19.11.14
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 __rte_experimental
144 int
146  struct rte_meter_trtcm_rfc4115_profile *p,
147  struct rte_meter_trtcm_rfc4115_params *params);
148 
159 int
160 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
161  struct rte_meter_srtcm_profile *p);
162 
173 int
175  struct rte_meter_trtcm_profile *p);
176 
190 __rte_experimental
191 int
193  struct rte_meter_trtcm_rfc4115_profile *p);
194 
209 static inline enum rte_color
210 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
211  struct rte_meter_srtcm_profile *p,
212  uint64_t time,
213  uint32_t pkt_len);
214 
231 static inline enum rte_color
232 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
233  struct rte_meter_srtcm_profile *p,
234  uint64_t time,
235  uint32_t pkt_len,
236  enum rte_color pkt_color);
237 
252 static inline enum rte_color
254  struct rte_meter_trtcm_profile *p,
255  uint64_t time,
256  uint32_t pkt_len);
257 
274 static inline enum rte_color
276  struct rte_meter_trtcm_profile *p,
277  uint64_t time,
278  uint32_t pkt_len,
279  enum rte_color pkt_color);
280 
298 __rte_experimental
299 static inline enum rte_color
301  struct rte_meter_trtcm_rfc4115 *m,
302  struct rte_meter_trtcm_rfc4115_profile *p,
303  uint64_t time,
304  uint32_t pkt_len);
305 
325 __rte_experimental
326 static inline enum rte_color
328  struct rte_meter_trtcm_rfc4115 *m,
329  struct rte_meter_trtcm_rfc4115_profile *p,
330  uint64_t time,
331  uint32_t pkt_len,
332  enum rte_color pkt_color);
333 
334 /*
335  * Inline implementation of run-time methods
336  *
337  ***/
338 
339 struct rte_meter_srtcm_profile {
340  uint64_t cbs;
342  uint64_t ebs;
344  uint64_t cir_period;
346  uint64_t cir_bytes_per_period;
348 };
349 
350 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
351 struct rte_meter_srtcm {
352  uint64_t time; /* Time of latest update of C and E token buckets */
353  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
354  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
355 };
356 
357 struct rte_meter_trtcm_profile {
358  uint64_t cbs;
360  uint64_t pbs;
362  uint64_t cir_period;
364  uint64_t cir_bytes_per_period;
366  uint64_t pir_period;
368  uint64_t pir_bytes_per_period;
370 };
371 
377  uint64_t time_tc;
379  uint64_t time_tp;
381  uint64_t tc;
383  uint64_t tp;
385 };
386 
387 struct rte_meter_trtcm_rfc4115_profile {
388  uint64_t cbs;
390  uint64_t ebs;
392  uint64_t cir_period;
394  uint64_t cir_bytes_per_period;
396  uint64_t eir_period;
398  uint64_t eir_bytes_per_period;
400 };
401 
407  uint64_t time_tc;
409  uint64_t time_te;
411  uint64_t tc;
413  uint64_t te;
415 };
416 
417 static inline enum rte_color
418 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
419  struct rte_meter_srtcm_profile *p,
420  uint64_t time,
421  uint32_t pkt_len)
422 {
423  uint64_t time_diff, n_periods, tc, te;
424 
425  /* Bucket update */
426  time_diff = time - m->time;
427  n_periods = time_diff / p->cir_period;
428  m->time += n_periods * p->cir_period;
429 
430  /* Put the tokens overflowing from tc into te bucket */
431  tc = m->tc + n_periods * p->cir_bytes_per_period;
432  te = m->te;
433  if (tc > p->cbs) {
434  te += (tc - p->cbs);
435  if (te > p->ebs)
436  te = p->ebs;
437  tc = p->cbs;
438  }
439 
440  /* Color logic */
441  if (tc >= pkt_len) {
442  m->tc = tc - pkt_len;
443  m->te = te;
444  return RTE_COLOR_GREEN;
445  }
446 
447  if (te >= pkt_len) {
448  m->tc = tc;
449  m->te = te - pkt_len;
450  return RTE_COLOR_YELLOW;
451  }
452 
453  m->tc = tc;
454  m->te = te;
455  return RTE_COLOR_RED;
456 }
457 
458 static inline enum rte_color
459 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
460  struct rte_meter_srtcm_profile *p,
461  uint64_t time,
462  uint32_t pkt_len,
463  enum rte_color pkt_color)
464 {
465  uint64_t time_diff, n_periods, tc, te;
466 
467  /* Bucket update */
468  time_diff = time - m->time;
469  n_periods = time_diff / p->cir_period;
470  m->time += n_periods * p->cir_period;
471 
472  /* Put the tokens overflowing from tc into te bucket */
473  tc = m->tc + n_periods * p->cir_bytes_per_period;
474  te = m->te;
475  if (tc > p->cbs) {
476  te += (tc - p->cbs);
477  if (te > p->ebs)
478  te = p->ebs;
479  tc = p->cbs;
480  }
481 
482  /* Color logic */
483  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
484  m->tc = tc - pkt_len;
485  m->te = te;
486  return RTE_COLOR_GREEN;
487  }
488 
489  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
490  m->tc = tc;
491  m->te = te - pkt_len;
492  return RTE_COLOR_YELLOW;
493  }
494 
495  m->tc = tc;
496  m->te = te;
497  return RTE_COLOR_RED;
498 }
499 
500 static inline enum rte_color
502  struct rte_meter_trtcm_profile *p,
503  uint64_t time,
504  uint32_t pkt_len)
505 {
506  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
507 
508  /* Bucket update */
509  time_diff_tc = time - m->time_tc;
510  time_diff_tp = time - m->time_tp;
511  n_periods_tc = time_diff_tc / p->cir_period;
512  n_periods_tp = time_diff_tp / p->pir_period;
513  m->time_tc += n_periods_tc * p->cir_period;
514  m->time_tp += n_periods_tp * p->pir_period;
515 
516  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
517  if (tc > p->cbs)
518  tc = p->cbs;
519 
520  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
521  if (tp > p->pbs)
522  tp = p->pbs;
523 
524  /* Color logic */
525  if (tp < pkt_len) {
526  m->tc = tc;
527  m->tp = tp;
528  return RTE_COLOR_RED;
529  }
530 
531  if (tc < pkt_len) {
532  m->tc = tc;
533  m->tp = tp - pkt_len;
534  return RTE_COLOR_YELLOW;
535  }
536 
537  m->tc = tc - pkt_len;
538  m->tp = tp - pkt_len;
539  return RTE_COLOR_GREEN;
540 }
541 
542 static inline enum rte_color
544  struct rte_meter_trtcm_profile *p,
545  uint64_t time,
546  uint32_t pkt_len,
547  enum rte_color pkt_color)
548 {
549  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
550 
551  /* Bucket update */
552  time_diff_tc = time - m->time_tc;
553  time_diff_tp = time - m->time_tp;
554  n_periods_tc = time_diff_tc / p->cir_period;
555  n_periods_tp = time_diff_tp / p->pir_period;
556  m->time_tc += n_periods_tc * p->cir_period;
557  m->time_tp += n_periods_tp * p->pir_period;
558 
559  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
560  if (tc > p->cbs)
561  tc = p->cbs;
562 
563  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
564  if (tp > p->pbs)
565  tp = p->pbs;
566 
567  /* Color logic */
568  if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
569  m->tc = tc;
570  m->tp = tp;
571  return RTE_COLOR_RED;
572  }
573 
574  if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
575  m->tc = tc;
576  m->tp = tp - pkt_len;
577  return RTE_COLOR_YELLOW;
578  }
579 
580  m->tc = tc - pkt_len;
581  m->tp = tp - pkt_len;
582  return RTE_COLOR_GREEN;
583 }
584 
585 __rte_experimental
586 static inline enum rte_color
588  struct rte_meter_trtcm_rfc4115 *m,
589  struct rte_meter_trtcm_rfc4115_profile *p,
590  uint64_t time,
591  uint32_t pkt_len)
592 {
593  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
594 
595  /* Bucket update */
596  time_diff_tc = time - m->time_tc;
597  time_diff_te = time - m->time_te;
598  n_periods_tc = time_diff_tc / p->cir_period;
599  n_periods_te = time_diff_te / p->eir_period;
600  m->time_tc += n_periods_tc * p->cir_period;
601  m->time_te += n_periods_te * p->eir_period;
602 
603  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
604  if (tc > p->cbs)
605  tc = p->cbs;
606 
607  te = m->te + n_periods_te * p->eir_bytes_per_period;
608  if (te > p->ebs)
609  te = p->ebs;
610 
611  /* Color logic */
612  if (tc >= pkt_len) {
613  m->tc = tc - pkt_len;
614  m->te = te;
615  return RTE_COLOR_GREEN;
616  }
617  if (te >= pkt_len) {
618  m->tc = tc;
619  m->te = te - pkt_len;
620  return RTE_COLOR_YELLOW;
621  }
622 
623  /* If we end up here the color is RED */
624  m->tc = tc;
625  m->te = te;
626  return RTE_COLOR_RED;
627 }
628 
629 __rte_experimental
630 static inline enum rte_color
632  struct rte_meter_trtcm_rfc4115 *m,
633  struct rte_meter_trtcm_rfc4115_profile *p,
634  uint64_t time,
635  uint32_t pkt_len,
636  enum rte_color pkt_color)
637 {
638  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
639 
640  /* Bucket update */
641  time_diff_tc = time - m->time_tc;
642  time_diff_te = time - m->time_te;
643  n_periods_tc = time_diff_tc / p->cir_period;
644  n_periods_te = time_diff_te / p->eir_period;
645  m->time_tc += n_periods_tc * p->cir_period;
646  m->time_te += n_periods_te * p->eir_period;
647 
648  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
649  if (tc > p->cbs)
650  tc = p->cbs;
651 
652  te = m->te + n_periods_te * p->eir_bytes_per_period;
653  if (te > p->ebs)
654  te = p->ebs;
655 
656  /* Color logic */
657  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
658  m->tc = tc - pkt_len;
659  m->te = te;
660  return RTE_COLOR_GREEN;
661  }
662 
663  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
664  m->tc = tc;
665  m->te = te - pkt_len;
666  return RTE_COLOR_YELLOW;
667  }
668 
669  /* If we end up here the color is RED */
670  m->tc = tc;
671  m->te = te;
672  return RTE_COLOR_RED;
673 }
674 
675 
676 #ifdef __cplusplus
677 }
678 #endif
679 
680 #endif /* __INCLUDE_RTE_METER_H__ */
__rte_experimental int rte_meter_trtcm_rfc4115_profile_config(struct rte_meter_trtcm_rfc4115_profile *p, struct rte_meter_trtcm_rfc4115_params *params)
static __rte_experimental 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:587
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:418
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:381
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:501
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:459
int rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p)
uint64_t time_tc
Definition: rte_meter.h:377
uint64_t tp
Definition: rte_meter.h:383
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:543
uint64_t time_tp
Definition: rte_meter.h:379
__rte_experimental int rte_meter_trtcm_rfc4115_config(struct rte_meter_trtcm_rfc4115 *m, struct rte_meter_trtcm_rfc4115_profile *p)
rte_color
Definition: rte_meter.h:36
static __rte_experimental 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:631
int rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p, struct rte_meter_srtcm_params *params)