DPDK  19.02.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 
43 /* New rte_color is defined and used to deprecate rte_meter_color soon. */
44 #define rte_meter_color rte_color
45 #define e_RTE_METER_GREEN RTE_COLOR_GREEN
46 #define e_RTE_METER_YELLOW RTE_COLOR_YELLOW
47 #define e_RTE_METER_RED RTE_COLOR_RED
48 #define e_RTE_METER_COLORS RTE_COLORS
49 
54  uint64_t cir;
55  uint64_t cbs;
56  uint64_t ebs;
57 };
58 
63  uint64_t cir;
64  uint64_t pir;
65  uint64_t cbs;
66  uint64_t pbs;
67 };
68 
74  uint64_t cir;
75  uint64_t eir;
76  uint64_t cbs;
77  uint64_t ebs;
78 };
79 
84 struct rte_meter_srtcm_profile;
85 
90 struct rte_meter_trtcm_profile;
91 
96 struct rte_meter_trtcm_rfc4115_profile;
97 
99 struct rte_meter_srtcm;
100 
102 struct rte_meter_trtcm;
103 
109 
120 int
121 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
122  struct rte_meter_srtcm_params *params);
123 
134 int
135 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
136  struct rte_meter_trtcm_params *params);
150 int __rte_experimental
152  struct rte_meter_trtcm_rfc4115_profile *p,
153  struct rte_meter_trtcm_rfc4115_params *params);
154 
165 int
166 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
167  struct rte_meter_srtcm_profile *p);
168 
179 int
181  struct rte_meter_trtcm_profile *p);
182 
196 int __rte_experimental
198  struct rte_meter_trtcm_rfc4115_profile *p);
199 
214 static inline enum rte_meter_color
215 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
216  struct rte_meter_srtcm_profile *p,
217  uint64_t time,
218  uint32_t pkt_len);
219 
236 static inline enum rte_meter_color
237 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
238  struct rte_meter_srtcm_profile *p,
239  uint64_t time,
240  uint32_t pkt_len,
241  enum rte_meter_color pkt_color);
242 
257 static inline enum rte_meter_color
259  struct rte_meter_trtcm_profile *p,
260  uint64_t time,
261  uint32_t pkt_len);
262 
279 static inline enum rte_meter_color
281  struct rte_meter_trtcm_profile *p,
282  uint64_t time,
283  uint32_t pkt_len,
284  enum rte_meter_color pkt_color);
285 
303 static inline enum rte_meter_color __rte_experimental
305  struct rte_meter_trtcm_rfc4115 *m,
306  struct rte_meter_trtcm_rfc4115_profile *p,
307  uint64_t time,
308  uint32_t pkt_len);
309 
329 static inline enum rte_meter_color __rte_experimental
331  struct rte_meter_trtcm_rfc4115 *m,
332  struct rte_meter_trtcm_rfc4115_profile *p,
333  uint64_t time,
334  uint32_t pkt_len,
335  enum rte_meter_color pkt_color);
336 
337 /*
338  * Inline implementation of run-time methods
339  *
340  ***/
341 
342 struct rte_meter_srtcm_profile {
343  uint64_t cbs;
345  uint64_t ebs;
347  uint64_t cir_period;
349  uint64_t cir_bytes_per_period;
351 };
352 
353 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
354 struct rte_meter_srtcm {
355  uint64_t time; /* Time of latest update of C and E token buckets */
356  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
357  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
358 };
359 
360 struct rte_meter_trtcm_profile {
361  uint64_t cbs;
363  uint64_t pbs;
365  uint64_t cir_period;
367  uint64_t cir_bytes_per_period;
369  uint64_t pir_period;
371  uint64_t pir_bytes_per_period;
373 };
374 
380  uint64_t time_tc;
382  uint64_t time_tp;
384  uint64_t tc;
386  uint64_t tp;
388 };
389 
390 struct rte_meter_trtcm_rfc4115_profile {
391  uint64_t cbs;
393  uint64_t ebs;
395  uint64_t cir_period;
397  uint64_t cir_bytes_per_period;
399  uint64_t eir_period;
401  uint64_t eir_bytes_per_period;
403 };
404 
410  uint64_t time_tc;
412  uint64_t time_te;
414  uint64_t tc;
416  uint64_t te;
418 };
419 
420 static inline enum rte_meter_color
421 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
422  struct rte_meter_srtcm_profile *p,
423  uint64_t time,
424  uint32_t pkt_len)
425 {
426  uint64_t time_diff, n_periods, tc, te;
427 
428  /* Bucket update */
429  time_diff = time - m->time;
430  n_periods = time_diff / p->cir_period;
431  m->time += n_periods * p->cir_period;
432 
433  /* Put the tokens overflowing from tc into te bucket */
434  tc = m->tc + n_periods * p->cir_bytes_per_period;
435  te = m->te;
436  if (tc > p->cbs) {
437  te += (tc - p->cbs);
438  if (te > p->ebs)
439  te = p->ebs;
440  tc = p->cbs;
441  }
442 
443  /* Color logic */
444  if (tc >= pkt_len) {
445  m->tc = tc - pkt_len;
446  m->te = te;
447  return e_RTE_METER_GREEN;
448  }
449 
450  if (te >= pkt_len) {
451  m->tc = tc;
452  m->te = te - pkt_len;
453  return e_RTE_METER_YELLOW;
454  }
455 
456  m->tc = tc;
457  m->te = te;
458  return e_RTE_METER_RED;
459 }
460 
461 static inline enum rte_meter_color
462 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
463  struct rte_meter_srtcm_profile *p,
464  uint64_t time,
465  uint32_t pkt_len,
466  enum rte_meter_color pkt_color)
467 {
468  uint64_t time_diff, n_periods, tc, te;
469 
470  /* Bucket update */
471  time_diff = time - m->time;
472  n_periods = time_diff / p->cir_period;
473  m->time += n_periods * p->cir_period;
474 
475  /* Put the tokens overflowing from tc into te bucket */
476  tc = m->tc + n_periods * p->cir_bytes_per_period;
477  te = m->te;
478  if (tc > p->cbs) {
479  te += (tc - p->cbs);
480  if (te > p->ebs)
481  te = p->ebs;
482  tc = p->cbs;
483  }
484 
485  /* Color logic */
486  if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
487  m->tc = tc - pkt_len;
488  m->te = te;
489  return e_RTE_METER_GREEN;
490  }
491 
492  if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
493  m->tc = tc;
494  m->te = te - pkt_len;
495  return e_RTE_METER_YELLOW;
496  }
497 
498  m->tc = tc;
499  m->te = te;
500  return e_RTE_METER_RED;
501 }
502 
503 static inline enum rte_meter_color
505  struct rte_meter_trtcm_profile *p,
506  uint64_t time,
507  uint32_t pkt_len)
508 {
509  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
510 
511  /* Bucket update */
512  time_diff_tc = time - m->time_tc;
513  time_diff_tp = time - m->time_tp;
514  n_periods_tc = time_diff_tc / p->cir_period;
515  n_periods_tp = time_diff_tp / p->pir_period;
516  m->time_tc += n_periods_tc * p->cir_period;
517  m->time_tp += n_periods_tp * p->pir_period;
518 
519  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
520  if (tc > p->cbs)
521  tc = p->cbs;
522 
523  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
524  if (tp > p->pbs)
525  tp = p->pbs;
526 
527  /* Color logic */
528  if (tp < pkt_len) {
529  m->tc = tc;
530  m->tp = tp;
531  return e_RTE_METER_RED;
532  }
533 
534  if (tc < pkt_len) {
535  m->tc = tc;
536  m->tp = tp - pkt_len;
537  return e_RTE_METER_YELLOW;
538  }
539 
540  m->tc = tc - pkt_len;
541  m->tp = tp - pkt_len;
542  return e_RTE_METER_GREEN;
543 }
544 
545 static inline enum rte_meter_color
547  struct rte_meter_trtcm_profile *p,
548  uint64_t time,
549  uint32_t pkt_len,
550  enum rte_meter_color pkt_color)
551 {
552  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
553 
554  /* Bucket update */
555  time_diff_tc = time - m->time_tc;
556  time_diff_tp = time - m->time_tp;
557  n_periods_tc = time_diff_tc / p->cir_period;
558  n_periods_tp = time_diff_tp / p->pir_period;
559  m->time_tc += n_periods_tc * p->cir_period;
560  m->time_tp += n_periods_tp * p->pir_period;
561 
562  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
563  if (tc > p->cbs)
564  tc = p->cbs;
565 
566  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
567  if (tp > p->pbs)
568  tp = p->pbs;
569 
570  /* Color logic */
571  if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
572  m->tc = tc;
573  m->tp = tp;
574  return e_RTE_METER_RED;
575  }
576 
577  if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
578  m->tc = tc;
579  m->tp = tp - pkt_len;
580  return e_RTE_METER_YELLOW;
581  }
582 
583  m->tc = tc - pkt_len;
584  m->tp = tp - pkt_len;
585  return e_RTE_METER_GREEN;
586 }
587 
588 static inline enum rte_meter_color __rte_experimental
590  struct rte_meter_trtcm_rfc4115 *m,
591  struct rte_meter_trtcm_rfc4115_profile *p,
592  uint64_t time,
593  uint32_t pkt_len)
594 {
595  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
596 
597  /* Bucket update */
598  time_diff_tc = time - m->time_tc;
599  time_diff_te = time - m->time_te;
600  n_periods_tc = time_diff_tc / p->cir_period;
601  n_periods_te = time_diff_te / p->eir_period;
602  m->time_tc += n_periods_tc * p->cir_period;
603  m->time_te += n_periods_te * p->eir_period;
604 
605  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
606  if (tc > p->cbs)
607  tc = p->cbs;
608 
609  te = m->te + n_periods_te * p->eir_bytes_per_period;
610  if (te > p->ebs)
611  te = p->ebs;
612 
613  /* Color logic */
614  if (tc >= pkt_len) {
615  m->tc = tc - pkt_len;
616  m->te = te;
617  return e_RTE_METER_GREEN;
618  }
619  if (te >= pkt_len) {
620  m->tc = tc;
621  m->te = te - pkt_len;
622  return e_RTE_METER_YELLOW;
623  }
624 
625  /* If we end up here the color is RED */
626  m->tc = tc;
627  m->te = te;
628  return e_RTE_METER_RED;
629 }
630 
631 static inline enum rte_meter_color __rte_experimental
633  struct rte_meter_trtcm_rfc4115 *m,
634  struct rte_meter_trtcm_rfc4115_profile *p,
635  uint64_t time,
636  uint32_t pkt_len,
637  enum rte_meter_color pkt_color)
638 {
639  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
640 
641  /* Bucket update */
642  time_diff_tc = time - m->time_tc;
643  time_diff_te = time - m->time_te;
644  n_periods_tc = time_diff_tc / p->cir_period;
645  n_periods_te = time_diff_te / p->eir_period;
646  m->time_tc += n_periods_tc * p->cir_period;
647  m->time_te += n_periods_te * p->eir_period;
648 
649  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
650  if (tc > p->cbs)
651  tc = p->cbs;
652 
653  te = m->te + n_periods_te * p->eir_bytes_per_period;
654  if (te > p->ebs)
655  te = p->ebs;
656 
657  /* Color logic */
658  if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
659  m->tc = tc - pkt_len;
660  m->te = te;
661  return e_RTE_METER_GREEN;
662  }
663 
664  if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
665  m->tc = tc;
666  m->te = te - pkt_len;
667  return e_RTE_METER_YELLOW;
668  }
669 
670  /* If we end up here the color is RED */
671  m->tc = tc;
672  m->te = te;
673  return e_RTE_METER_RED;
674 }
675 
676 
677 #ifdef __cplusplus
678 }
679 #endif
680 
681 #endif /* __INCLUDE_RTE_METER_H__ */