DPDK  18.08.1
rte_meter.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef __INCLUDE_RTE_METER_H__
6 #define __INCLUDE_RTE_METER_H__
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
22 #include <stdint.h>
23 
24 /*
25  * Application Programmer's Interface (API)
26  *
27  ***/
28 
35 };
36 
41  uint64_t cir;
42  uint64_t cbs;
43  uint64_t ebs;
44 };
45 
50  uint64_t cir;
51  uint64_t pir;
52  uint64_t cbs;
53  uint64_t pbs;
54 };
55 
60 struct rte_meter_srtcm_profile;
61 
66 struct rte_meter_trtcm_profile;
67 
69 struct rte_meter_srtcm;
70 
72 struct rte_meter_trtcm;
73 
84 int
85 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
86  struct rte_meter_srtcm_params *params);
87 
98 int
99 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
100  struct rte_meter_trtcm_params *params);
101 
112 int
113 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
114  struct rte_meter_srtcm_profile *p);
115 
126 int
128  struct rte_meter_trtcm_profile *p);
129 
144 static inline enum rte_meter_color
145 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
146  struct rte_meter_srtcm_profile *p,
147  uint64_t time,
148  uint32_t pkt_len);
149 
166 static inline enum rte_meter_color
167 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
168  struct rte_meter_srtcm_profile *p,
169  uint64_t time,
170  uint32_t pkt_len,
171  enum rte_meter_color pkt_color);
172 
187 static inline enum rte_meter_color
189  struct rte_meter_trtcm_profile *p,
190  uint64_t time,
191  uint32_t pkt_len);
192 
209 static inline enum rte_meter_color
211  struct rte_meter_trtcm_profile *p,
212  uint64_t time,
213  uint32_t pkt_len,
214  enum rte_meter_color pkt_color);
215 
216 /*
217  * Inline implementation of run-time methods
218  *
219  ***/
220 
221 struct rte_meter_srtcm_profile {
222  uint64_t cbs;
224  uint64_t ebs;
226  uint64_t cir_period;
228  uint64_t cir_bytes_per_period;
230 };
231 
232 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
233 struct rte_meter_srtcm {
234  uint64_t time; /* Time of latest update of C and E token buckets */
235  uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
236  uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
237 };
238 
239 struct rte_meter_trtcm_profile {
240  uint64_t cbs;
242  uint64_t pbs;
244  uint64_t cir_period;
246  uint64_t cir_bytes_per_period;
248  uint64_t pir_period;
250  uint64_t pir_bytes_per_period;
252 };
253 
259  uint64_t time_tc;
261  uint64_t time_tp;
263  uint64_t tc;
265  uint64_t tp;
267 };
268 
269 static inline enum rte_meter_color
270 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
271  struct rte_meter_srtcm_profile *p,
272  uint64_t time,
273  uint32_t pkt_len)
274 {
275  uint64_t time_diff, n_periods, tc, te;
276 
277  /* Bucket update */
278  time_diff = time - m->time;
279  n_periods = time_diff / p->cir_period;
280  m->time += n_periods * p->cir_period;
281 
282  /* Put the tokens overflowing from tc into te bucket */
283  tc = m->tc + n_periods * p->cir_bytes_per_period;
284  te = m->te;
285  if (tc > p->cbs) {
286  te += (tc - p->cbs);
287  if (te > p->ebs)
288  te = p->ebs;
289  tc = p->cbs;
290  }
291 
292  /* Color logic */
293  if (tc >= pkt_len) {
294  m->tc = tc - pkt_len;
295  m->te = te;
296  return e_RTE_METER_GREEN;
297  }
298 
299  if (te >= pkt_len) {
300  m->tc = tc;
301  m->te = te - pkt_len;
302  return e_RTE_METER_YELLOW;
303  }
304 
305  m->tc = tc;
306  m->te = te;
307  return e_RTE_METER_RED;
308 }
309 
310 static inline enum rte_meter_color
311 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
312  struct rte_meter_srtcm_profile *p,
313  uint64_t time,
314  uint32_t pkt_len,
315  enum rte_meter_color pkt_color)
316 {
317  uint64_t time_diff, n_periods, tc, te;
318 
319  /* Bucket update */
320  time_diff = time - m->time;
321  n_periods = time_diff / p->cir_period;
322  m->time += n_periods * p->cir_period;
323 
324  /* Put the tokens overflowing from tc into te bucket */
325  tc = m->tc + n_periods * p->cir_bytes_per_period;
326  te = m->te;
327  if (tc > p->cbs) {
328  te += (tc - p->cbs);
329  if (te > p->ebs)
330  te = p->ebs;
331  tc = p->cbs;
332  }
333 
334  /* Color logic */
335  if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
336  m->tc = tc - pkt_len;
337  m->te = te;
338  return e_RTE_METER_GREEN;
339  }
340 
341  if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
342  m->tc = tc;
343  m->te = te - pkt_len;
344  return e_RTE_METER_YELLOW;
345  }
346 
347  m->tc = tc;
348  m->te = te;
349  return e_RTE_METER_RED;
350 }
351 
352 static inline enum rte_meter_color
354  struct rte_meter_trtcm_profile *p,
355  uint64_t time,
356  uint32_t pkt_len)
357 {
358  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
359 
360  /* Bucket update */
361  time_diff_tc = time - m->time_tc;
362  time_diff_tp = time - m->time_tp;
363  n_periods_tc = time_diff_tc / p->cir_period;
364  n_periods_tp = time_diff_tp / p->pir_period;
365  m->time_tc += n_periods_tc * p->cir_period;
366  m->time_tp += n_periods_tp * p->pir_period;
367 
368  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
369  if (tc > p->cbs)
370  tc = p->cbs;
371 
372  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
373  if (tp > p->pbs)
374  tp = p->pbs;
375 
376  /* Color logic */
377  if (tp < pkt_len) {
378  m->tc = tc;
379  m->tp = tp;
380  return e_RTE_METER_RED;
381  }
382 
383  if (tc < pkt_len) {
384  m->tc = tc;
385  m->tp = tp - pkt_len;
386  return e_RTE_METER_YELLOW;
387  }
388 
389  m->tc = tc - pkt_len;
390  m->tp = tp - pkt_len;
391  return e_RTE_METER_GREEN;
392 }
393 
394 static inline enum rte_meter_color
396  struct rte_meter_trtcm_profile *p,
397  uint64_t time,
398  uint32_t pkt_len,
399  enum rte_meter_color pkt_color)
400 {
401  uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
402 
403  /* Bucket update */
404  time_diff_tc = time - m->time_tc;
405  time_diff_tp = time - m->time_tp;
406  n_periods_tc = time_diff_tc / p->cir_period;
407  n_periods_tp = time_diff_tp / p->pir_period;
408  m->time_tc += n_periods_tc * p->cir_period;
409  m->time_tp += n_periods_tp * p->pir_period;
410 
411  tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
412  if (tc > p->cbs)
413  tc = p->cbs;
414 
415  tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
416  if (tp > p->pbs)
417  tp = p->pbs;
418 
419  /* Color logic */
420  if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
421  m->tc = tc;
422  m->tp = tp;
423  return e_RTE_METER_RED;
424  }
425 
426  if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
427  m->tc = tc;
428  m->tp = tp - pkt_len;
429  return e_RTE_METER_YELLOW;
430  }
431 
432  m->tc = tc - pkt_len;
433  m->tp = tp - pkt_len;
434  return e_RTE_METER_GREEN;
435 }
436 
437 #ifdef __cplusplus
438 }
439 #endif
440 
441 #endif /* __INCLUDE_RTE_METER_H__ */