DPDK  22.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 
27 /*
28  * Application Programmer's Interface (API)
29  *
30  ***/
31 
35 enum rte_color {
40 };
41 
46  uint64_t cir;
47  uint64_t cbs;
48  uint64_t ebs;
49 };
50 
55  uint64_t cir;
56  uint64_t pir;
57  uint64_t cbs;
58  uint64_t pbs;
59 };
60 
66  uint64_t cir;
67  uint64_t eir;
68  uint64_t cbs;
69  uint64_t ebs;
70 };
71 
76 struct rte_meter_srtcm_profile;
77 
82 struct rte_meter_trtcm_profile;
83 
88 struct rte_meter_trtcm_rfc4115_profile;
89 
91 struct rte_meter_srtcm;
92 
94 struct rte_meter_trtcm;
95 
101 
112 int
113 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
114  struct rte_meter_srtcm_params *params);
115 
126 int
127 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
128  struct rte_meter_trtcm_params *params);
139 int
141  struct rte_meter_trtcm_rfc4115_profile *p,
142  struct rte_meter_trtcm_rfc4115_params *params);
143 
154 int
155 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
156  struct rte_meter_srtcm_profile *p);
157 
168 int
170  struct rte_meter_trtcm_profile *p);
171 
182 int
184  struct rte_meter_trtcm_rfc4115_profile *p);
185 
200 static inline enum rte_color
201 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
202  struct rte_meter_srtcm_profile *p,
203  uint64_t time,
204  uint32_t pkt_len);
205 
222 static inline enum rte_color
223 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
224  struct rte_meter_srtcm_profile *p,
225  uint64_t time,
226  uint32_t pkt_len,
227  enum rte_color pkt_color);
228 
243 static inline enum rte_color
245  struct rte_meter_trtcm_profile *p,
246  uint64_t time,
247  uint32_t pkt_len);
248 
265 static inline enum rte_color
267  struct rte_meter_trtcm_profile *p,
268  uint64_t time,
269  uint32_t pkt_len,
270  enum rte_color pkt_color);
271 
286 static inline enum rte_color
288  struct rte_meter_trtcm_rfc4115 *m,
289  struct rte_meter_trtcm_rfc4115_profile *p,
290  uint64_t time,
291  uint32_t pkt_len);
292 
309 static inline enum rte_color
311  struct rte_meter_trtcm_rfc4115 *m,
312  struct rte_meter_trtcm_rfc4115_profile *p,
313  uint64_t time,
314  uint32_t pkt_len,
315  enum rte_color pkt_color);
316 
317 /*
318  * Inline implementation of run-time methods
319  *
320  ***/
321 
322 struct rte_meter_srtcm_profile {
323  uint64_t cbs;
325  uint64_t ebs;
327  uint64_t cir_period;
329  uint64_t cir_bytes_per_period;
331 };
332 
333 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
334 struct rte_meter_srtcm {
335  uint64_t time; /* Time of latest update of C and E token buckets */
336  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
337  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
338 };
339 
340 struct rte_meter_trtcm_profile {
341  uint64_t cbs;
343  uint64_t pbs;
345  uint64_t cir_period;
347  uint64_t cir_bytes_per_period;
349  uint64_t pir_period;
351  uint64_t pir_bytes_per_period;
353 };
354 
360  uint64_t time_tc;
362  uint64_t time_tp;
364  uint64_t tc;
366  uint64_t tp;
368 };
369 
370 struct rte_meter_trtcm_rfc4115_profile {
371  uint64_t cbs;
373  uint64_t ebs;
375  uint64_t cir_period;
377  uint64_t cir_bytes_per_period;
379  uint64_t eir_period;
381  uint64_t eir_bytes_per_period;
383 };
384 
390  uint64_t time_tc;
392  uint64_t time_te;
394  uint64_t tc;
396  uint64_t te;
398 };
399 
400 static inline enum rte_color
401 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
402  struct rte_meter_srtcm_profile *p,
403  uint64_t time,
404  uint32_t pkt_len)
405 {
406  uint64_t time_diff, n_periods, tc, te;
407 
408  /* Bucket update */
409  time_diff = time - m->time;
410  n_periods = time_diff / p->cir_period;
411  m->time += n_periods * p->cir_period;
412 
413  /* Put the tokens overflowing from tc into te bucket */
414  tc = m->tc + n_periods * p->cir_bytes_per_period;
415  te = m->te;
416  if (tc > p->cbs) {
417  te += (tc - p->cbs);
418  if (te > p->ebs)
419  te = p->ebs;
420  tc = p->cbs;
421  }
422 
423  /* Color logic */
424  if (tc >= pkt_len) {
425  m->tc = tc - pkt_len;
426  m->te = te;
427  return RTE_COLOR_GREEN;
428  }
429 
430  if (te >= pkt_len) {
431  m->tc = tc;
432  m->te = te - pkt_len;
433  return RTE_COLOR_YELLOW;
434  }
435 
436  m->tc = tc;
437  m->te = te;
438  return RTE_COLOR_RED;
439 }
440 
441 static inline enum rte_color
442 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
443  struct rte_meter_srtcm_profile *p,
444  uint64_t time,
445  uint32_t pkt_len,
446  enum rte_color pkt_color)
447 {
448  uint64_t time_diff, n_periods, tc, te;
449 
450  /* Bucket update */
451  time_diff = time - m->time;
452  n_periods = time_diff / p->cir_period;
453  m->time += n_periods * p->cir_period;
454 
455  /* Put the tokens overflowing from tc into te bucket */
456  tc = m->tc + n_periods * p->cir_bytes_per_period;
457  te = m->te;
458  if (tc > p->cbs) {
459  te += (tc - p->cbs);
460  if (te > p->ebs)
461  te = p->ebs;
462  tc = p->cbs;
463  }
464 
465  /* Color logic */
466  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
467  m->tc = tc - pkt_len;
468  m->te = te;
469  return RTE_COLOR_GREEN;
470  }
471 
472  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
473  m->tc = tc;
474  m->te = te - pkt_len;
475  return RTE_COLOR_YELLOW;
476  }
477 
478  m->tc = tc;
479  m->te = te;
480  return RTE_COLOR_RED;
481 }
482 
483 static inline enum rte_color
485  struct rte_meter_trtcm_profile *p,
486  uint64_t time,
487  uint32_t pkt_len)
488 {
489  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
490 
491  /* Bucket update */
492  time_diff_tc = time - m->time_tc;
493  time_diff_tp = time - m->time_tp;
494  n_periods_tc = time_diff_tc / p->cir_period;
495  n_periods_tp = time_diff_tp / p->pir_period;
496  m->time_tc += n_periods_tc * p->cir_period;
497  m->time_tp += n_periods_tp * p->pir_period;
498 
499  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
500  if (tc > p->cbs)
501  tc = p->cbs;
502 
503  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
504  if (tp > p->pbs)
505  tp = p->pbs;
506 
507  /* Color logic */
508  if (tp < pkt_len) {
509  m->tc = tc;
510  m->tp = tp;
511  return RTE_COLOR_RED;
512  }
513 
514  if (tc < pkt_len) {
515  m->tc = tc;
516  m->tp = tp - pkt_len;
517  return RTE_COLOR_YELLOW;
518  }
519 
520  m->tc = tc - pkt_len;
521  m->tp = tp - pkt_len;
522  return RTE_COLOR_GREEN;
523 }
524 
525 static inline enum rte_color
527  struct rte_meter_trtcm_profile *p,
528  uint64_t time,
529  uint32_t pkt_len,
530  enum rte_color pkt_color)
531 {
532  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
533 
534  /* Bucket update */
535  time_diff_tc = time - m->time_tc;
536  time_diff_tp = time - m->time_tp;
537  n_periods_tc = time_diff_tc / p->cir_period;
538  n_periods_tp = time_diff_tp / p->pir_period;
539  m->time_tc += n_periods_tc * p->cir_period;
540  m->time_tp += n_periods_tp * p->pir_period;
541 
542  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
543  if (tc > p->cbs)
544  tc = p->cbs;
545 
546  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
547  if (tp > p->pbs)
548  tp = p->pbs;
549 
550  /* Color logic */
551  if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
552  m->tc = tc;
553  m->tp = tp;
554  return RTE_COLOR_RED;
555  }
556 
557  if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
558  m->tc = tc;
559  m->tp = tp - pkt_len;
560  return RTE_COLOR_YELLOW;
561  }
562 
563  m->tc = tc - pkt_len;
564  m->tp = tp - pkt_len;
565  return RTE_COLOR_GREEN;
566 }
567 
568 static inline enum rte_color
570  struct rte_meter_trtcm_rfc4115 *m,
571  struct rte_meter_trtcm_rfc4115_profile *p,
572  uint64_t time,
573  uint32_t pkt_len)
574 {
575  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
576 
577  /* Bucket update */
578  time_diff_tc = time - m->time_tc;
579  time_diff_te = time - m->time_te;
580  n_periods_tc = time_diff_tc / p->cir_period;
581  n_periods_te = time_diff_te / p->eir_period;
582  m->time_tc += n_periods_tc * p->cir_period;
583  m->time_te += n_periods_te * p->eir_period;
584 
585  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
586  if (tc > p->cbs)
587  tc = p->cbs;
588 
589  te = m->te + n_periods_te * p->eir_bytes_per_period;
590  if (te > p->ebs)
591  te = p->ebs;
592 
593  /* Color logic */
594  if (tc >= pkt_len) {
595  m->tc = tc - pkt_len;
596  m->te = te;
597  return RTE_COLOR_GREEN;
598  }
599  if (te >= pkt_len) {
600  m->tc = tc;
601  m->te = te - pkt_len;
602  return RTE_COLOR_YELLOW;
603  }
604 
605  /* If we end up here the color is RED */
606  m->tc = tc;
607  m->te = te;
608  return RTE_COLOR_RED;
609 }
610 
611 static inline enum rte_color
613  struct rte_meter_trtcm_rfc4115 *m,
614  struct rte_meter_trtcm_rfc4115_profile *p,
615  uint64_t time,
616  uint32_t pkt_len,
617  enum rte_color pkt_color)
618 {
619  uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
620 
621  /* Bucket update */
622  time_diff_tc = time - m->time_tc;
623  time_diff_te = time - m->time_te;
624  n_periods_tc = time_diff_tc / p->cir_period;
625  n_periods_te = time_diff_te / p->eir_period;
626  m->time_tc += n_periods_tc * p->cir_period;
627  m->time_te += n_periods_te * p->eir_period;
628 
629  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
630  if (tc > p->cbs)
631  tc = p->cbs;
632 
633  te = m->te + n_periods_te * p->eir_bytes_per_period;
634  if (te > p->ebs)
635  te = p->ebs;
636 
637  /* Color logic */
638  if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
639  m->tc = tc - pkt_len;
640  m->te = te;
641  return RTE_COLOR_GREEN;
642  }
643 
644  if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
645  m->tc = tc;
646  m->te = te - pkt_len;
647  return RTE_COLOR_YELLOW;
648  }
649 
650  /* If we end up here the color is RED */
651  m->tc = tc;
652  m->te = te;
653  return RTE_COLOR_RED;
654 }
655 
656 
657 #ifdef __cplusplus
658 }
659 #endif
660 
661 #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:401
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:569
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:364
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:484
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:442
int rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_profile *p)
uint64_t time_tc
Definition: rte_meter.h:360
uint64_t tp
Definition: rte_meter.h:366
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:526
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:362
rte_color
Definition: rte_meter.h:35
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:612