New upstream version 18.02
[deb_dpdk.git] / lib / librte_meter / rte_meter.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <inttypes.h>
6 #include <stdio.h>
7 #include <math.h>
8
9 #include <rte_common.h>
10 #include <rte_log.h>
11 #include <rte_cycles.h>
12
13 #include "rte_meter.h"
14
15 #ifndef RTE_METER_TB_PERIOD_MIN
16 #define RTE_METER_TB_PERIOD_MIN      100
17 #endif
18
19 static void
20 rte_meter_get_tb_params(uint64_t hz, uint64_t rate, uint64_t *tb_period, uint64_t *tb_bytes_per_period)
21 {
22         double period = ((double) hz) / ((double) rate);
23
24         if (period >= RTE_METER_TB_PERIOD_MIN) {
25                 *tb_bytes_per_period = 1;
26                 *tb_period = (uint64_t) period;
27         } else {
28                 *tb_bytes_per_period = (uint64_t) ceil(RTE_METER_TB_PERIOD_MIN / period);
29                 *tb_period = (hz * (*tb_bytes_per_period)) / rate;
30         }
31 }
32
33 int
34 rte_meter_srtcm_config(struct rte_meter_srtcm *m, struct rte_meter_srtcm_params *params)
35 {
36         uint64_t hz;
37
38         /* Check input parameters */
39         if ((m == NULL) || (params == NULL)) {
40                 return -1;
41         }
42
43         if ((params->cir == 0) || ((params->cbs == 0) && (params->ebs == 0))) {
44                 return -2;
45         }
46
47         /* Initialize srTCM run-time structure */
48         hz = rte_get_tsc_hz();
49         m->time = rte_get_tsc_cycles();
50         m->tc = m->cbs = params->cbs;
51         m->te = m->ebs = params->ebs;
52         rte_meter_get_tb_params(hz, params->cir, &m->cir_period, &m->cir_bytes_per_period);
53
54         RTE_LOG(INFO, METER, "Low level srTCM config: \n"
55                 "\tCIR period = %" PRIu64 ", CIR bytes per period = %" PRIu64 "\n",
56                 m->cir_period, m->cir_bytes_per_period);
57
58         return 0;
59 }
60
61 int
62 rte_meter_trtcm_config(struct rte_meter_trtcm *m, struct rte_meter_trtcm_params *params)
63 {
64         uint64_t hz;
65
66         /* Check input parameters */
67         if ((m == NULL) || (params == NULL)) {
68                 return -1;
69         }
70
71         if ((params->cir == 0) || (params->pir == 0) || (params->pir < params->cir) ||
72                 (params->cbs == 0) || (params->pbs == 0)) {
73                 return -2;
74         }
75
76         /* Initialize trTCM run-time structure */
77         hz = rte_get_tsc_hz();
78         m->time_tc = m->time_tp = rte_get_tsc_cycles();
79         m->tc = m->cbs = params->cbs;
80         m->tp = m->pbs = params->pbs;
81         rte_meter_get_tb_params(hz, params->cir, &m->cir_period, &m->cir_bytes_per_period);
82         rte_meter_get_tb_params(hz, params->pir, &m->pir_period, &m->pir_bytes_per_period);
83
84         RTE_LOG(INFO, METER, "Low level trTCM config: \n"
85                 "\tCIR period = %" PRIu64 ", CIR bytes per period = %" PRIu64 "\n"
86                 "\tPIR period = %" PRIu64 ", PIR bytes per period = %" PRIu64 "\n",
87                 m->cir_period, m->cir_bytes_per_period,
88                 m->pir_period, m->pir_bytes_per_period);
89
90         return 0;
91 }