DPDK  24.07.0-rc2
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);
136 int
138  struct rte_meter_trtcm_rfc4115_profile *p,
139  struct rte_meter_trtcm_rfc4115_params *params);
140 
151 int
152 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
153  struct rte_meter_srtcm_profile *p);
154 
165 int
167  struct rte_meter_trtcm_profile *p);
168 
179 int
181  struct rte_meter_trtcm_rfc4115_profile *p);
182 
197 static inline enum rte_color
198 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
199  struct rte_meter_srtcm_profile *p,
200  uint64_t time,
201  uint32_t pkt_len);
202 
219 static inline enum rte_color
220 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
221  struct rte_meter_srtcm_profile *p,
222  uint64_t time,
223  uint32_t pkt_len,
224  enum rte_color pkt_color);
225 
240 static inline enum rte_color
242  struct rte_meter_trtcm_profile *p,
243  uint64_t time,
244  uint32_t pkt_len);
245 
262 static inline enum rte_color
264  struct rte_meter_trtcm_profile *p,
265  uint64_t time,
266  uint32_t pkt_len,
267  enum rte_color pkt_color);
268 
283 static inline enum rte_color
285  struct rte_meter_trtcm_rfc4115 *m,
286  struct rte_meter_trtcm_rfc4115_profile *p,
287  uint64_t time,
288  uint32_t pkt_len);
289 
306 static inline enum rte_color
308  struct rte_meter_trtcm_rfc4115 *m,
309  struct rte_meter_trtcm_rfc4115_profile *p,
310  uint64_t time,
311  uint32_t pkt_len,
312  enum rte_color pkt_color);
313 
314 /*
315  * Inline implementation of run-time methods
316  */
317 
318 struct rte_meter_srtcm_profile {
319  uint64_t cbs;
321  uint64_t ebs;
323  uint64_t cir_period;
325  uint64_t cir_bytes_per_period;
327 };
328 
329 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
330 struct rte_meter_srtcm {
331  uint64_t time; /* Time of latest update of C and E token buckets */
332  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
333  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
334 };
335 
336 struct rte_meter_trtcm_profile {
337  uint64_t cbs;
339  uint64_t pbs;
341  uint64_t cir_period;
343  uint64_t cir_bytes_per_period;
345  uint64_t pir_period;
347  uint64_t pir_bytes_per_period;
349 };
350 
356  uint64_t time_tc;
358  uint64_t time_tp;
360  uint64_t tc;
362  uint64_t tp;
364 };
365 
366 struct rte_meter_trtcm_rfc4115_profile {
367  uint64_t cbs;
369  uint64_t ebs;
371  uint64_t cir_period;
373  uint64_t cir_bytes_per_period;
375  uint64_t eir_period;
377  uint64_t eir_bytes_per_period;
379 };
380 
386  uint64_t time_tc;
388  uint64_t time_te;
390  uint64_t tc;
392  uint64_t te;
394 };
395 
396 static inline enum rte_color
397 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
398  struct rte_meter_srtcm_profile *p,
399  uint64_t time,
400  uint32_t pkt_len)
401 {
402  uint64_t time_diff, n_periods, tc, te;
403 
404  /* Bucket update */
405  time_diff = time - m->time;
406  n_periods = time_diff / p->cir_period;
407  m->time += n_periods * p->cir_period;
408 
409  /* Put the tokens overflowing from tc into te bucket */
410  tc = m->tc + n_periods * p->cir_bytes_per_period;
411  te = m->te;
412  if (tc > p->cbs) {
413  te += (tc - p->cbs);
414  if (te > p->ebs)
415  te = p->ebs;
416  tc = p->cbs;
417  }
418 
419  /* Color logic */
420  if (tc >= pkt_len) {
421  m->tc = tc - pkt_len;
422  m->te = te;
423  return RTE_COLOR_GREEN;
424  }
425 
426  if (te >= pkt_len) {
427  m->tc = tc;
428  m->te = te - pkt_len;
429  return RTE_COLOR_YELLOW;
430  }
431 
432  m->tc = tc;
433  m->te = te;
434  return RTE_COLOR_RED;
435 }
436 
437 static inline enum rte_color
438 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
439  struct rte_meter_srtcm_profile *p,
440  uint64_t time,
441  uint32_t pkt_len,
442  enum rte_color pkt_color)
443 {
444  uint64_t time_diff, n_periods, tc, te;
445 
446  /* Bucket update */
447  time_diff = time - m->time;
448  n_periods = time_diff / p->cir_period;
449  m->time += n_periods * p->cir_period;
450 
451  /* Put the tokens overflowing from tc into te bucket */
452  tc = m->tc + n_periods * p->cir_bytes_per_period;
453  te = m->te;
454  if (tc > p->cbs) {
455  te += (tc - p->cbs);
456  if (te > p->ebs)
457  te = p->ebs;
458  tc = p->cbs;
459  }
460 
461  /* Color logic */
462  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
463  m->tc = tc - pkt_len;
464  m->te = te;
465  return RTE_COLOR_GREEN;
466  }
467 
468  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
469  m->tc = tc;
470  m->te = te - pkt_len;
471  return RTE_COLOR_YELLOW;
472  }
473 
474  m->tc = tc;
475  m->te = te;
476  return RTE_COLOR_RED;
477 }
478 
479 static inline enum rte_color
481  struct rte_meter_trtcm_profile *p,
482  uint64_t time,
483  uint32_t pkt_len)
484 {
485  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
486 
487  /* Bucket update */
488  time_diff_tc = time - m->time_tc;
489  time_diff_tp = time - m->time_tp;
490  n_periods_tc = time_diff_tc / p->cir_period;
491  n_periods_tp = time_diff_tp / p->pir_period;
492  m->time_tc += n_periods_tc * p->cir_period;
493  m->time_tp += n_periods_tp * p->pir_period;
494 
495  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
496  if (tc > p->cbs)
497  tc = p->cbs;
498 
499  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
500  if (tp > p->pbs)
501  tp = p->pbs;
502 
503  /* Color logic */
504  if (tp < pkt_len) {
505  m->tc = tc;
506  m->tp = tp;
507  return RTE_COLOR_RED;
508  }
509 
510  if (tc < pkt_len) {
511  m->tc = tc;
512  m->tp = tp - pkt_len;
513  return RTE_COLOR_YELLOW;
514  }
515 
516  m->tc = tc - pkt_len;
517  m->tp = tp - pkt_len;
518  return RTE_COLOR_GREEN;
519 }
520 
521 static inline enum rte_color
523  struct rte_meter_trtcm_profile *p,
524  uint64_t time,
525  uint32_t pkt_len,
526  enum rte_color pkt_color)
527 {
528  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
529 
530  /* Bucket update */
531  time_diff_tc = time - m->time_tc;
532  time_diff_tp = time - m->time_tp;
533  n_periods_tc = time_diff_tc / p->cir_period;
534  n_periods_tp = time_diff_tp / p->pir_period;
535  m->time_tc += n_periods_tc * p->cir_period;
536  m->time_tp += n_periods_tp * p->pir_period;
537 
538  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
539  if (tc > p->cbs)
540  tc = p->cbs;
541 
542  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
543  if (tp > p->pbs)
544  tp = p->pbs;
545 
546  /* Color logic */
547  if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
548  m->tc = tc;
549  m->tp = tp;
550  return RTE_COLOR_RED;
551  }
552 
553  if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
554  m->tc = tc;
555  m->tp = tp - pkt_len;
556  return RTE_COLOR_YELLOW;
557  }
558 
559  m->tc = tc - pkt_len;
560  m->tp = tp - pkt_len;
561  return RTE_COLOR_GREEN;
562 }
563 
564 static inline enum rte_color
566  struct rte_meter_trtcm_rfc4115 *m,
567  struct rte_meter_trtcm_rfc4115_profile *p,
568  uint64_t time,
569  uint32_t pkt_len)
570 {
571  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
572 
573  /* Bucket update */
574  time_diff_tc = time - m->time_tc;
575  time_diff_te = time - m->time_te;
576  n_periods_tc = time_diff_tc / p->cir_period;
577  n_periods_te = time_diff_te / p->eir_period;
578  m->time_tc += n_periods_tc * p->cir_period;
579  m->time_te += n_periods_te * p->eir_period;
580 
581  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
582  if (tc > p->cbs)
583  tc = p->cbs;
584 
585  te = m->te + n_periods_te * p->eir_bytes_per_period;
586  if (te > p->ebs)
587  te = p->ebs;
588 
589  /* Color logic */
590  if (tc >= pkt_len) {
591  m->tc = tc - pkt_len;
592  m->te = te;
593  return RTE_COLOR_GREEN;
594  }
595  if (te >= pkt_len) {
596  m->tc = tc;
597  m->te = te - pkt_len;
598  return RTE_COLOR_YELLOW;
599  }
600 
601  /* If we end up here the color is RED */
602  m->tc = tc;
603  m->te = te;
604  return RTE_COLOR_RED;
605 }
606 
607 static inline enum rte_color
609  struct rte_meter_trtcm_rfc4115 *m,
610  struct rte_meter_trtcm_rfc4115_profile *p,
611  uint64_t time,
612  uint32_t pkt_len,
613  enum rte_color pkt_color)
614 {
615  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
616 
617  /* Bucket update */
618  time_diff_tc = time - m->time_tc;
619  time_diff_te = time - m->time_te;
620  n_periods_tc = time_diff_tc / p->cir_period;
621  n_periods_te = time_diff_te / p->eir_period;
622  m->time_tc += n_periods_tc * p->cir_period;
623  m->time_te += n_periods_te * p->eir_period;
624 
625  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
626  if (tc > p->cbs)
627  tc = p->cbs;
628 
629  te = m->te + n_periods_te * p->eir_bytes_per_period;
630  if (te > p->ebs)
631  te = p->ebs;
632 
633  /* Color logic */
634  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
635  m->tc = tc - pkt_len;
636  m->te = te;
637  return RTE_COLOR_GREEN;
638  }
639 
640  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
641  m->tc = tc;
642  m->te = te - pkt_len;
643  return RTE_COLOR_YELLOW;
644  }
645 
646  /* If we end up here the color is RED */
647  m->tc = tc;
648  m->te = te;
649  return RTE_COLOR_RED;
650 }
651 
652 
653 #ifdef __cplusplus
654 }
655 #endif
656 
657 #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:397
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:565
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:360
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:480
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:438
int rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p)
uint64_t time_tc
Definition: rte_meter.h:356
uint64_t tp
Definition: rte_meter.h:362
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:522
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:358
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:608