1 /*
2 * collectd - src/dpdkstat.c
3 * MIT License
4 *
5 * Copyright(c) 2016 Intel Corporation. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Maryam Tahhan <maryam.tahhan@intel.com>
27 * Harry van Haaren <harry.van.haaren@intel.com>
28 * Taras Chornyi <tarasx.chornyi@intel.com>
29 * Serhiy Pshyk <serhiyx.pshyk@intel.com>
30 * Krzysztof Matczak <krzysztofx.matczak@intel.com>
31 */
33 #include "collectd.h"
35 #include "common.h"
36 #include "utils_dpdk.h"
38 #include <rte_config.h>
39 #include <rte_ethdev.h>
41 #define DPDK_STATS_PLUGIN "dpdkstat"
42 #define DPDK_STATS_NAME "dpdk_collectd_stats"
44 #define DPDK_STATS_TRACE() \
45 DEBUG("%s:%s:%d pid=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__, getpid())
47 struct dpdk_stats_config_s {
48 cdtime_t interval;
49 uint32_t enabled_port_mask;
50 char port_name[RTE_MAX_ETHPORTS][DATA_MAX_NAME_LEN];
51 };
52 typedef struct dpdk_stats_config_s dpdk_stats_config_t;
54 #define RTE_VERSION_16_07 RTE_VERSION_NUM(16, 7, 0, 16)
56 #if RTE_VERSION < RTE_VERSION_16_07
57 #define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value
58 #define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xstats[index].name
59 #define DPDK_STATS_CTX_GET_XSTAT_SIZE sizeof(struct rte_eth_xstats)
60 #define DPDK_STATS_CTX_INIT(ctx) \
61 do { \
62 ctx->xstats = (struct rte_eth_xstats *)&ctx->raw_data[0]; \
63 } while (0)
64 #else
65 #define DPDK_STATS_XSTAT_GET_VALUE(ctx, index) ctx->xstats[index].value
66 #define DPDK_STATS_XSTAT_GET_NAME(ctx, index) ctx->xnames[index].name
67 #define DPDK_STATS_CTX_GET_XSTAT_SIZE \
68 (sizeof(struct rte_eth_xstat) + sizeof(struct rte_eth_xstat_name))
69 #define DPDK_STATS_CTX_INIT(ctx) \
70 do { \
71 ctx->xstats = (struct rte_eth_xstat *)&ctx->raw_data[0]; \
72 ctx->xnames = \
73 (struct rte_eth_xstat_name *)&ctx \
74 ->raw_data[ctx->stats_count * sizeof(struct rte_eth_xstat)]; \
75 } while (0)
76 #endif
78 struct dpdk_stats_ctx_s {
79 dpdk_stats_config_t config;
80 uint32_t stats_count;
81 uint32_t ports_count;
82 cdtime_t port_read_time[RTE_MAX_ETHPORTS];
83 uint32_t port_stats_count[RTE_MAX_ETHPORTS];
84 #if RTE_VERSION < RTE_VERSION_16_07
85 struct rte_eth_xstats *xstats;
86 #else
87 struct rte_eth_xstat *xstats;
88 struct rte_eth_xstat_name *xnames;
89 #endif
90 char raw_data[];
91 };
92 typedef struct dpdk_stats_ctx_s dpdk_stats_ctx_t;
94 #define DPDK_STATS_CTX_GET(a) ((dpdk_stats_ctx_t *)dpdk_helper_priv_get(a))
96 dpdk_helper_ctx_t *g_hc = NULL;
97 static char g_shm_name[DATA_MAX_NAME_LEN] = DPDK_STATS_NAME;
98 static int dpdk_stats_reinit_helper();
99 static void dpdk_stats_default_config(void) {
100 dpdk_stats_ctx_t *ec = DPDK_STATS_CTX_GET(g_hc);
102 ec->config.interval = plugin_get_interval();
103 for (int i = 0; i < RTE_MAX_ETHPORTS; i++) {
104 ec->config.port_name[i][0] = 0;
105 }
106 }
108 static int dpdk_stats_preinit(void) {
109 DPDK_STATS_TRACE();
111 if (g_hc != NULL) {
112 /* already initialized if config callback was called before init callback */
113 DEBUG("dpdk_stats_preinit: helper already initialized");
114 return 0;
115 }
117 int ret = dpdk_helper_init(g_shm_name, sizeof(dpdk_stats_ctx_t), &g_hc);
118 if (ret != 0) {
119 char errbuf[ERR_BUF_SIZE];
120 ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
121 g_shm_name, sstrerror(errno, errbuf, sizeof(errbuf)));
122 return ret;
123 }
125 dpdk_stats_default_config();
126 return ret;
127 }
129 static int dpdk_stats_config(oconfig_item_t *ci) {
130 DPDK_STATS_TRACE();
132 int ret = dpdk_stats_preinit();
133 if (ret)
134 return ret;
136 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
138 for (int i = 0; i < ci->children_num; i++) {
139 oconfig_item_t *child = ci->children + i;
141 if ((strcasecmp("EnabledPortMask", child->key) == 0) &&
142 (child->values[0].type == OCONFIG_TYPE_NUMBER)) {
143 ctx->config.enabled_port_mask = child->values[0].value.number;
144 DEBUG("%s: Enabled Port Mask 0x%X", DPDK_STATS_PLUGIN,
145 ctx->config.enabled_port_mask);
146 } else if (strcasecmp("SharedMemObj", child->key) == 0) {
147 cf_util_get_string_buffer(child, g_shm_name, sizeof(g_shm_name));
148 DEBUG("%s: Shared memory object %s", DPDK_STATS_PLUGIN, g_shm_name);
149 dpdk_stats_reinit_helper();
150 } else if (strcasecmp("EAL", child->key) == 0) {
151 ret = dpdk_helper_eal_config_parse(g_hc, child);
152 if (ret)
153 return ret;
154 }
155 }
157 int port_num = 0;
159 /* parse port names after EnabledPortMask was parsed */
160 for (int i = 0; i < ci->children_num; i++) {
161 oconfig_item_t *child = ci->children + i;
163 if (strcasecmp("PortName", child->key) == 0) {
165 while (!(ctx->config.enabled_port_mask & (1 << port_num)))
166 port_num++;
168 cf_util_get_string_buffer(child, ctx->config.port_name[port_num],
169 sizeof(ctx->config.port_name[port_num]));
170 DEBUG("%s: Port %d Name: %s", DPDK_STATS_PLUGIN, port_num,
171 ctx->config.port_name[port_num]);
173 port_num++;
174 }
175 }
177 return ret;
178 }
180 static int dpdk_helper_stats_get(dpdk_helper_ctx_t *phc) {
181 int len = 0;
182 int ret = 0;
183 int stats = 0;
184 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
186 /* get stats from DPDK */
187 for (uint8_t i = 0; i < ctx->ports_count; i++) {
188 if (!(ctx->config.enabled_port_mask & (1 << i)))
189 continue;
191 ctx->port_read_time[i] = cdtime();
192 /* Store available stats array length for port */
193 len = ctx->port_stats_count[i];
195 ret = rte_eth_xstats_get(i, &ctx->xstats[stats], len);
196 if (ret < 0 || ret > len) {
197 DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
198 ": Error reading stats (port=%d; len=%d, ret=%d)\n",
199 i, len, ret);
200 ctx->port_stats_count[i] = 0;
201 return -1;
202 }
203 #if RTE_VERSION >= RTE_VERSION_16_07
204 ret = rte_eth_xstats_get_names(i, &ctx->xnames[stats], len);
205 if (ret < 0 || ret > len) {
206 DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
207 ": Error reading stat names (port=%d; len=%d ret=%d)\n",
208 i, len, ret);
209 ctx->port_stats_count[i] = 0;
210 return -1;
211 }
212 #endif
213 ctx->port_stats_count[i] = ret;
214 stats += ctx->port_stats_count[i];
215 }
217 assert(stats <= ctx->stats_count);
218 return 0;
219 }
221 static int dpdk_helper_stats_count_get(dpdk_helper_ctx_t *phc) {
222 uint8_t ports = dpdk_helper_eth_dev_count();
223 if (ports == 0)
224 return -ENODEV;
226 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
227 ctx->ports_count = ports;
229 int len = 0;
230 int stats_count = 0;
231 for (int i = 0; i < ports; i++) {
232 if (!(ctx->config.enabled_port_mask & (1 << i)))
233 continue;
234 #if RTE_VERSION >= RTE_VERSION_16_07
235 len = rte_eth_xstats_get_names(i, NULL, 0);
236 #else
237 len = rte_eth_xstats_get(i, NULL, 0);
238 #endif
239 if (len < 0) {
240 DPDK_CHILD_LOG("%s: Cannot get stats count\n", DPDK_STATS_PLUGIN);
241 return -1;
242 }
243 ctx->port_stats_count[i] = len;
244 stats_count += len;
245 }
247 DPDK_CHILD_LOG("%s:%s:%d stats_count=%d\n", DPDK_STATS_PLUGIN, __FUNCTION__,
248 __LINE__, stats_count);
250 return stats_count;
251 }
253 static int dpdk_stats_get_size(dpdk_helper_ctx_t *phc) {
254 return (dpdk_helper_data_size_get(phc) - sizeof(dpdk_stats_ctx_t));
255 }
257 int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) {
258 /* this function is called from helper context */
260 if (phc == NULL) {
261 DPDK_CHILD_LOG("%s: Invalid argument(phc)\n", DPDK_STATS_PLUGIN);
262 return -EINVAL;
263 }
265 if (cmd != DPDK_CMD_GET_STATS) {
266 DPDK_CHILD_LOG("%s: Unknown command (cmd=%d)\n", DPDK_STATS_PLUGIN, cmd);
267 return -EINVAL;
268 }
270 int stats_count = dpdk_helper_stats_count_get(phc);
271 if (stats_count < 0) {
272 return stats_count;
273 }
275 DPDK_STATS_CTX_GET(phc)->stats_count = stats_count;
276 int stats_size = stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE;
278 if (dpdk_stats_get_size(phc) < stats_size) {
279 DPDK_CHILD_LOG(
280 DPDK_STATS_PLUGIN
281 ":%s:%d not enough space for stats (available=%d, needed=%d)\n",
282 __FUNCTION__, __LINE__, (int)dpdk_stats_get_size(phc), stats_size);
283 return -ENOBUFS;
284 }
286 return dpdk_helper_stats_get(phc);
287 }
289 static void dpdk_stats_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
290 const char *cnt_name) {
291 char *type_end;
292 type_end = strrchr(cnt_name, '_');
294 if ((type_end != NULL) && (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
295 if (strstr(type_end, "bytes") != NULL) {
296 sstrncpy(cnt_type, "if_rx_octets", cnt_type_len);
297 } else if (strstr(type_end, "error") != NULL) {
298 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
299 } else if (strstr(type_end, "dropped") != NULL) {
300 sstrncpy(cnt_type, "if_rx_dropped", cnt_type_len);
301 } else if (strstr(type_end, "packets") != NULL) {
302 sstrncpy(cnt_type, "if_rx_packets", cnt_type_len);
303 } else if (strstr(type_end, "_placement") != NULL) {
304 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
305 } else if (strstr(type_end, "_buff") != NULL) {
306 sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
307 } else {
308 /* Does not fit obvious type: use a more generic one */
309 sstrncpy(cnt_type, "derive", cnt_type_len);
310 }
312 } else if ((type_end != NULL) &&
313 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
314 if (strstr(type_end, "bytes") != NULL) {
315 sstrncpy(cnt_type, "if_tx_octets", cnt_type_len);
316 } else if (strstr(type_end, "error") != NULL) {
317 sstrncpy(cnt_type, "if_tx_errors", cnt_type_len);
318 } else if (strstr(type_end, "dropped") != NULL) {
319 sstrncpy(cnt_type, "if_tx_dropped", cnt_type_len);
320 } else if (strstr(type_end, "packets") != NULL) {
321 sstrncpy(cnt_type, "if_tx_packets", cnt_type_len);
322 } else {
323 /* Does not fit obvious type: use a more generic one */
324 sstrncpy(cnt_type, "derive", cnt_type_len);
325 }
326 } else if ((type_end != NULL) &&
327 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
329 if (strstr(type_end, "_filters") != NULL) {
330 sstrncpy(cnt_type, "operations", cnt_type_len);
331 } else if (strstr(type_end, "error") != NULL)
332 sstrncpy(cnt_type, "errors", cnt_type_len);
334 } else if ((type_end != NULL) &&
335 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
336 if (strstr(type_end, "error") != NULL) {
337 sstrncpy(cnt_type, "errors", cnt_type_len);
338 }
339 } else {
340 /* Does not fit obvious type, or strrchr error:
341 * use a more generic type */
342 sstrncpy(cnt_type, "derive", cnt_type_len);
343 }
344 }
346 static void dpdk_stats_counter_submit(const char *plugin_instance,
347 const char *cnt_name, derive_t value,
348 cdtime_t port_read_time) {
349 value_list_t vl = VALUE_LIST_INIT;
350 vl.values = &(value_t){.derive = value};
351 vl.values_len = 1;
352 vl.time = port_read_time;
353 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
354 sstrncpy(vl.plugin, DPDK_STATS_PLUGIN, sizeof(vl.plugin));
355 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
356 dpdk_stats_resolve_cnt_type(vl.type, sizeof(vl.type), cnt_name);
357 sstrncpy(vl.type_instance, cnt_name, sizeof(vl.type_instance));
358 plugin_dispatch_values(&vl);
359 }
361 static int dpdk_stats_counters_dispatch(dpdk_helper_ctx_t *phc) {
362 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
364 /* dispatch stats values to collectd */
366 DEBUG("%s:%s:%d ports=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__,
367 ctx->ports_count);
369 int stats_count = 0;
371 for (int i = 0; i < ctx->ports_count; i++) {
372 if (!(ctx->config.enabled_port_mask & (1 << i)))
373 continue;
375 char dev_name[64];
376 if (ctx->config.port_name[i][0] != 0) {
377 ssnprintf(dev_name, sizeof(dev_name), "%s", ctx->config.port_name[i]);
378 } else {
379 ssnprintf(dev_name, sizeof(dev_name), "port.%d", i);
380 }
382 DEBUG(" === Dispatch stats for port %d (name=%s; stats_count=%d)", i,
383 dev_name, ctx->port_stats_count[i]);
385 for (int j = 0; j < ctx->port_stats_count[i]; j++) {
386 const char *cnt_name = DPDK_STATS_XSTAT_GET_NAME(ctx, stats_count);
387 if (cnt_name == NULL)
388 WARNING("dpdkstat: Invalid counter name");
389 else
390 dpdk_stats_counter_submit(
391 dev_name, cnt_name,
392 (derive_t)DPDK_STATS_XSTAT_GET_VALUE(ctx, stats_count),
393 ctx->port_read_time[i]);
394 stats_count++;
396 assert(stats_count <= ctx->stats_count);
397 }
398 }
400 return 0;
401 }
403 static int dpdk_stats_reinit_helper() {
404 DPDK_STATS_TRACE();
406 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
408 size_t data_size = sizeof(dpdk_stats_ctx_t) +
409 (ctx->stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE);
411 DEBUG("%s:%d helper reinit (new_size=%zu)", __FUNCTION__, __LINE__,
412 data_size);
414 dpdk_stats_ctx_t tmp_ctx;
415 dpdk_eal_config_t tmp_eal;
417 memcpy(&tmp_ctx, ctx, sizeof(dpdk_stats_ctx_t));
418 dpdk_helper_eal_config_get(g_hc, &tmp_eal);
420 dpdk_helper_shutdown(g_hc);
422 g_hc = NULL;
424 int ret;
425 ret = dpdk_helper_init(g_shm_name, data_size, &g_hc);
426 if (ret != 0) {
427 char errbuf[ERR_BUF_SIZE];
428 ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
429 g_shm_name, sstrerror(errno, errbuf, sizeof(errbuf)));
430 return ret;
431 }
433 ctx = DPDK_STATS_CTX_GET(g_hc);
434 memcpy(ctx, &tmp_ctx, sizeof(dpdk_stats_ctx_t));
435 DPDK_STATS_CTX_INIT(ctx);
436 dpdk_helper_eal_config_set(g_hc, &tmp_eal);
438 return ret;
439 }
441 static int dpdk_stats_read(user_data_t *ud) {
442 DPDK_STATS_TRACE();
444 int ret = 0;
446 if (g_hc == NULL) {
447 ERROR("dpdk stats plugin not initialized");
448 return -EINVAL;
449 }
451 dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
453 int result = 0;
454 ret = dpdk_helper_command(g_hc, DPDK_CMD_GET_STATS, &result,
455 ctx->config.interval);
456 if (ret != 0) {
457 return 0;
458 }
460 if (result == -ENOBUFS) {
461 dpdk_stats_reinit_helper();
462 } else if (result == -ENODEV) {
463 dpdk_helper_shutdown(g_hc);
464 } else if (result == 0) {
465 dpdk_stats_counters_dispatch(g_hc);
466 }
468 return 0;
469 }
471 static int dpdk_stats_init(void) {
472 DPDK_STATS_TRACE();
473 int ret = 0;
475 ret = dpdk_stats_preinit();
476 if (ret != 0) {
477 return ret;
478 }
480 return 0;
481 }
483 static int dpdk_stats_shutdown(void) {
484 DPDK_STATS_TRACE();
486 int ret = 0;
488 ret = dpdk_helper_shutdown(g_hc);
489 g_hc = NULL;
490 if (ret != 0) {
491 ERROR("%s: failed to cleanup %s helper", DPDK_STATS_PLUGIN, g_shm_name);
492 return ret;
493 }
495 return ret;
496 }
498 void module_register(void) {
499 plugin_register_init(DPDK_STATS_PLUGIN, dpdk_stats_init);
500 plugin_register_complex_config(DPDK_STATS_PLUGIN, dpdk_stats_config);
501 plugin_register_complex_read(NULL, DPDK_STATS_PLUGIN, dpdk_stats_read, 0,
502 NULL);
503 plugin_register_shutdown(DPDK_STATS_PLUGIN, dpdk_stats_shutdown);
504 }