net/null: internalize create function
[dpdk.git] / lib / librte_metrics / rte_metrics.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /**
35  * @file
36  *
37  * DPDK Metrics module
38  *
39  * Metrics are statistics that are not generated by PMDs, and hence
40  * are better reported through a mechanism that is independent from
41  * the ethdev-based extended statistics. Providers will typically
42  * be other libraries and consumers will typically be applications.
43  *
44  * Metric information is populated using a push model, where producers
45  * update the values contained within the metric library by calling
46  * an update function on the relevant metrics. Consumers receive
47  * metric information by querying the central metric data, which is
48  * held in shared memory. Currently only bulk querying of metrics
49  * by consumers is supported.
50  */
51
52 #ifndef _RTE_METRICS_H_
53 #define _RTE_METRICS_H_
54
55 /** Maximum length of metric name (including null-terminator) */
56 #define RTE_METRICS_MAX_NAME_LEN 64
57
58 /**
59  * Global metric special id.
60  *
61  * When used for the port_id parameter when calling
62  * rte_metrics_update_metric() or rte_metrics_update_metric(),
63  * the global metric, which are not associated with any specific
64  * port (i.e. device), are updated.
65  */
66 #define RTE_METRICS_GLOBAL -1
67
68
69 /**
70  * A name-key lookup for metrics.
71  *
72  * An array of this structure is returned by rte_metrics_get_names().
73  * The struct rte_metric_value references these names via their array index.
74  */
75 struct rte_metric_name {
76         /** String describing metric */
77         char name[RTE_METRICS_MAX_NAME_LEN];
78 };
79
80
81 /**
82  * Metric value structure.
83  *
84  * This structure is used by rte_metrics_get_values() to return metrics,
85  * which are statistics that are not generated by PMDs. It maps a name key,
86  * which corresponds to an index in the array returned by
87  * rte_metrics_get_names().
88  */
89 struct rte_metric_value {
90         /** Numeric identifier of metric. */
91         uint16_t key;
92         /** Value for metric */
93         uint64_t value;
94 };
95
96
97 /**
98  * Initializes metric module. This function must be called from
99  * a primary process before metrics are used.
100  *
101  * @param socket_id
102  *   Socket to use for shared memory allocation.
103  */
104 void rte_metrics_init(int socket_id);
105
106 /**
107  * Register a metric, making it available as a reporting parameter.
108  *
109  * Registering a metric is the way producers declare a parameter
110  * that they wish to be reported. Once registered, the associated
111  * numeric key can be obtained via rte_metrics_get_names(), which
112  * is required for updating said metric's value.
113  *
114  * @param name
115  *   Metric name
116  *
117  * @return
118  *  - Zero or positive: Success (index key of new metric)
119  *  - -EIO: Error, unable to access metrics shared memory
120  *    (rte_metrics_init() not called)
121  *  - -EINVAL: Error, invalid parameters
122  *  - -ENOMEM: Error, maximum metrics reached
123  */
124 int rte_metrics_reg_name(const char *name);
125
126 /**
127  * Register a set of metrics.
128  *
129  * This is a bulk version of rte_metrics_reg_names() and aside from
130  * handling multiple keys at once is functionally identical.
131  *
132  * @param names
133  *   List of metric names
134  *
135  * @param cnt_names
136  *   Number of metrics in set
137  *
138  * @return
139  *  - Zero or positive: Success (index key of start of set)
140  *  - -EIO: Error, unable to access metrics shared memory
141  *    (rte_metrics_init() not called)
142  *  - -EINVAL: Error, invalid parameters
143  *  - -ENOMEM: Error, maximum metrics reached
144  */
145 int rte_metrics_reg_names(const char * const *names, uint16_t cnt_names);
146
147 /**
148  * Get metric name-key lookup table.
149  *
150  * @param names
151  *   A struct rte_metric_name array of at least *capacity* in size to
152  *   receive key names. If this is NULL, function returns the required
153  *   number of elements for this array.
154  *
155  * @param capacity
156  *   Size (number of elements) of struct rte_metric_name array.
157  *   Disregarded if names is NULL.
158  *
159  * @return
160  *   - Positive value above capacity: error, *names* is too small.
161  *     Return value is required size.
162  *   - Positive value equal or less than capacity: Success. Return
163  *     value is number of elements filled in.
164  *   - Negative value: error.
165  */
166 int rte_metrics_get_names(
167         struct rte_metric_name *names,
168         uint16_t capacity);
169
170 /**
171  * Get metric value table.
172  *
173  * @param port_id
174  *   Port id to query
175  *
176  * @param values
177  *   A struct rte_metric_value array of at least *capacity* in size to
178  *   receive metric ids and values. If this is NULL, function returns
179  *   the required number of elements for this array.
180  *
181  * @param capacity
182  *   Size (number of elements) of struct rte_metric_value array.
183  *   Disregarded if names is NULL.
184  *
185  * @return
186  *   - Positive value above capacity: error, *values* is too small.
187  *     Return value is required size.
188  *   - Positive value equal or less than capacity: Success. Return
189  *     value is number of elements filled in.
190  *   - Negative value: error.
191  */
192 int rte_metrics_get_values(
193         int port_id,
194         struct rte_metric_value *values,
195         uint16_t capacity);
196
197 /**
198  * Updates a metric
199  *
200  * @param port_id
201  *   Port to update metrics for
202  * @param key
203  *   Id of metric to update
204  * @param value
205  *   New value
206  *
207  * @return
208  *   - -EIO if unable to access shared metrics memory
209  *   - Zero on success
210  */
211 int rte_metrics_update_value(
212         int port_id,
213         uint16_t key,
214         const uint64_t value);
215
216 /**
217  * Updates a metric set. Note that it is an error to try to
218  * update across a set boundary.
219  *
220  * @param port_id
221  *   Port to update metrics for
222  * @param key
223  *   Base id of metrics set to update
224  * @param values
225  *   Set of new values
226  * @param count
227  *   Number of new values
228  *
229  * @return
230  *   - -ERANGE if count exceeds metric set size
231  *   - -EIO if unable to access shared metrics memory
232  *   - Zero on success
233  */
234 int rte_metrics_update_values(
235         int port_id,
236         uint16_t key,
237         const uint64_t *values,
238         uint32_t count);
239
240 #endif