DPDK  21.11.7-rc1
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);
140 int
142  struct rte_meter_trtcm_rfc4115_profile *p,
143  struct rte_meter_trtcm_rfc4115_params *params);
144 
155 int
156 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
157  struct rte_meter_srtcm_profile *p);
158 
169 int
171  struct rte_meter_trtcm_profile *p);
172 
183 int
185  struct rte_meter_trtcm_rfc4115_profile *p);
186 
201 static inline enum rte_color
202 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
203  struct rte_meter_srtcm_profile *p,
204  uint64_t time,
205  uint32_t pkt_len);
206 
223 static inline enum rte_color
224 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
225  struct rte_meter_srtcm_profile *p,
226  uint64_t time,
227  uint32_t pkt_len,
228  enum rte_color pkt_color);
229 
244 static inline enum rte_color
246  struct rte_meter_trtcm_profile *p,
247  uint64_t time,
248  uint32_t pkt_len);
249 
266 static inline enum rte_color
268  struct rte_meter_trtcm_profile *p,
269  uint64_t time,
270  uint32_t pkt_len,
271  enum rte_color pkt_color);
272 
287 static inline enum rte_color
289  struct rte_meter_trtcm_rfc4115 *m,
290  struct rte_meter_trtcm_rfc4115_profile *p,
291  uint64_t time,
292  uint32_t pkt_len);
293 
310 static inline enum rte_color
312  struct rte_meter_trtcm_rfc4115 *m,
313  struct rte_meter_trtcm_rfc4115_profile *p,
314  uint64_t time,
315  uint32_t pkt_len,
316  enum rte_color pkt_color);
317 
318 /*
319  * Inline implementation of run-time methods
320  *
321  ***/
322 
323 struct rte_meter_srtcm_profile {
324  uint64_t cbs;
326  uint64_t ebs;
328  uint64_t cir_period;
330  uint64_t cir_bytes_per_period;
332 };
333 
334 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
335 struct rte_meter_srtcm {
336  uint64_t time; /* Time of latest update of C and E token buckets */
337  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
338  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
339 };
340 
341 struct rte_meter_trtcm_profile {
342  uint64_t cbs;
344  uint64_t pbs;
346  uint64_t cir_period;
348  uint64_t cir_bytes_per_period;
350  uint64_t pir_period;
352  uint64_t pir_bytes_per_period;
354 };
355 
361  uint64_t time_tc;
363  uint64_t time_tp;
365  uint64_t tc;
367  uint64_t tp;
369 };
370 
371 struct rte_meter_trtcm_rfc4115_profile {
372  uint64_t cbs;
374  uint64_t ebs;
376  uint64_t cir_period;
378  uint64_t cir_bytes_per_period;
380  uint64_t eir_period;
382  uint64_t eir_bytes_per_period;
384 };
385 
391  uint64_t time_tc;
393  uint64_t time_te;
395  uint64_t tc;
397  uint64_t te;
399 };
400 
401 static inline enum rte_color
402 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
403  struct rte_meter_srtcm_profile *p,
404  uint64_t time,
405  uint32_t pkt_len)
406 {
407  uint64_t time_diff, n_periods, tc, te;
408 
409  /* Bucket update */
410  time_diff = time - m->time;
411  n_periods = time_diff / p->cir_period;
412  m->time += n_periods * p->cir_period;
413 
414  /* Put the tokens overflowing from tc into te bucket */
415  tc = m->tc + n_periods * p->cir_bytes_per_period;
416  te = m->te;
417  if (tc > p->cbs) {
418  te += (tc - p->cbs);
419  if (te > p->ebs)
420  te = p->ebs;
421  tc = p->cbs;
422  }
423 
424  /* Color logic */
425  if (tc >= pkt_len) {
426  m->tc = tc - pkt_len;
427  m->te = te;
428  return RTE_COLOR_GREEN;
429  }
430 
431  if (te >= pkt_len) {
432  m->tc = tc;
433  m->te = te - pkt_len;
434  return RTE_COLOR_YELLOW;
435  }
436 
437  m->tc = tc;
438  m->te = te;
439  return RTE_COLOR_RED;
440 }
441 
442 static inline enum rte_color
443 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
444  struct rte_meter_srtcm_profile *p,
445  uint64_t time,
446  uint32_t pkt_len,
447  enum rte_color pkt_color)
448 {
449  uint64_t time_diff, n_periods, tc, te;
450 
451  /* Bucket update */
452  time_diff = time - m->time;
453  n_periods = time_diff / p->cir_period;
454  m->time += n_periods * p->cir_period;
455 
456  /* Put the tokens overflowing from tc into te bucket */
457  tc = m->tc + n_periods * p->cir_bytes_per_period;
458  te = m->te;
459  if (tc > p->cbs) {
460  te += (tc - p->cbs);
461  if (te > p->ebs)
462  te = p->ebs;
463  tc = p->cbs;
464  }
465 
466  /* Color logic */
467  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
468  m->tc = tc - pkt_len;
469  m->te = te;
470  return RTE_COLOR_GREEN;
471  }
472 
473  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
474  m->tc = tc;
475  m->te = te - pkt_len;
476  return RTE_COLOR_YELLOW;
477  }
478 
479  m->tc = tc;
480  m->te = te;
481  return RTE_COLOR_RED;
482 }
483 
484 static inline enum rte_color
486  struct rte_meter_trtcm_profile *p,
487  uint64_t time,
488  uint32_t pkt_len)
489 {
490  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
491 
492  /* Bucket update */
493  time_diff_tc = time - m->time_tc;
494  time_diff_tp = time - m->time_tp;
495  n_periods_tc = time_diff_tc / p->cir_period;
496  n_periods_tp = time_diff_tp / p->pir_period;
497  m->time_tc += n_periods_tc * p->cir_period;
498  m->time_tp += n_periods_tp * p->pir_period;
499 
500  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
501  if (tc > p->cbs)
502  tc = p->cbs;
503 
504  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
505  if (tp > p->pbs)
506  tp = p->pbs;
507 
508  /* Color logic */
509  if (tp < pkt_len) {
510  m->tc = tc;
511  m->tp = tp;
512  return RTE_COLOR_RED;
513  }
514 
515  if (tc < pkt_len) {
516  m->tc = tc;
517  m->tp = tp - pkt_len;
518  return RTE_COLOR_YELLOW;
519  }
520 
521  m->tc = tc - pkt_len;
522  m->tp = tp - pkt_len;
523  return RTE_COLOR_GREEN;
524 }
525 
526 static inline enum rte_color
528  struct rte_meter_trtcm_profile *p,
529  uint64_t time,
530  uint32_t pkt_len,
531  enum rte_color pkt_color)
532 {
533  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
534 
535  /* Bucket update */
536  time_diff_tc = time - m->time_tc;
537  time_diff_tp = time - m->time_tp;
538  n_periods_tc = time_diff_tc / p->cir_period;
539  n_periods_tp = time_diff_tp / p->pir_period;
540  m->time_tc += n_periods_tc * p->cir_period;
541  m->time_tp += n_periods_tp * p->pir_period;
542 
543  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
544  if (tc > p->cbs)
545  tc = p->cbs;
546 
547  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
548  if (tp > p->pbs)
549  tp = p->pbs;
550 
551  /* Color logic */
552  if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
553  m->tc = tc;
554  m->tp = tp;
555  return RTE_COLOR_RED;
556  }
557 
558  if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
559  m->tc = tc;
560  m->tp = tp - pkt_len;
561  return RTE_COLOR_YELLOW;
562  }
563 
564  m->tc = tc - pkt_len;
565  m->tp = tp - pkt_len;
566  return RTE_COLOR_GREEN;
567 }
568 
569 static inline enum rte_color
571  struct rte_meter_trtcm_rfc4115 *m,
572  struct rte_meter_trtcm_rfc4115_profile *p,
573  uint64_t time,
574  uint32_t pkt_len)
575 {
576  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
577 
578  /* Bucket update */
579  time_diff_tc = time - m->time_tc;
580  time_diff_te = time - m->time_te;
581  n_periods_tc = time_diff_tc / p->cir_period;
582  n_periods_te = time_diff_te / p->eir_period;
583  m->time_tc += n_periods_tc * p->cir_period;
584  m->time_te += n_periods_te * p->eir_period;
585 
586  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
587  if (tc > p->cbs)
588  tc = p->cbs;
589 
590  te = m->te + n_periods_te * p->eir_bytes_per_period;
591  if (te > p->ebs)
592  te = p->ebs;
593 
594  /* Color logic */
595  if (tc >= pkt_len) {
596  m->tc = tc - pkt_len;
597  m->te = te;
598  return RTE_COLOR_GREEN;
599  }
600  if (te >= pkt_len) {
601  m->tc = tc;
602  m->te = te - pkt_len;
603  return RTE_COLOR_YELLOW;
604  }
605 
606  /* If we end up here the color is RED */
607  m->tc = tc;
608  m->te = te;
609  return RTE_COLOR_RED;
610 }
611 
612 static inline enum rte_color
614  struct rte_meter_trtcm_rfc4115 *m,
615  struct rte_meter_trtcm_rfc4115_profile *p,
616  uint64_t time,
617  uint32_t pkt_len,
618  enum rte_color pkt_color)
619 {
620  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
621 
622  /* Bucket update */
623  time_diff_tc = time - m->time_tc;
624  time_diff_te = time - m->time_te;
625  n_periods_tc = time_diff_tc / p->cir_period;
626  n_periods_te = time_diff_te / p->eir_period;
627  m->time_tc += n_periods_tc * p->cir_period;
628  m->time_te += n_periods_te * p->eir_period;
629 
630  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
631  if (tc > p->cbs)
632  tc = p->cbs;
633 
634  te = m->te + n_periods_te * p->eir_bytes_per_period;
635  if (te > p->ebs)
636  te = p->ebs;
637 
638  /* Color logic */
639  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
640  m->tc = tc - pkt_len;
641  m->te = te;
642  return RTE_COLOR_GREEN;
643  }
644 
645  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
646  m->tc = tc;
647  m->te = te - pkt_len;
648  return RTE_COLOR_YELLOW;
649  }
650 
651  /* If we end up here the color is RED */
652  m->tc = tc;
653  m->te = te;
654  return RTE_COLOR_RED;
655 }
656 
657 
658 #ifdef __cplusplus
659 }
660 #endif
661 
662 #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:402
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:570
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:365
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:485
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:443
int rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p)
uint64_t time_tc
Definition: rte_meter.h:361
uint64_t tp
Definition: rte_meter.h:367
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:527
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:363
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:613