Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / dpdkstat.c
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  */
32
33 #include "collectd.h"
34
35 #include "common.h"
36 #include "utils_dpdk.h"
37
38 #include <rte_config.h>
39 #include <rte_ethdev.h>
40
41 #define DPDK_STATS_PLUGIN "dpdkstat"
42 #define DPDK_STATS_NAME "dpdk_collectd_stats"
43
44 #define DPDK_STATS_TRACE()                                                     \
45   DEBUG("%s:%s:%d pid=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__, getpid())
46
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;
53
54 #define RTE_VERSION_16_07 RTE_VERSION_NUM(16, 7, 0, 16)
55
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
77
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;
93
94 typedef enum {
95   DPDK_STAT_STATE_OKAY = 0,
96   DPDK_STAT_STATE_CFG_ERR,
97 } dpdk_stat_cfg_status;
98
99 #define DPDK_STATS_CTX_GET(a) ((dpdk_stats_ctx_t *)dpdk_helper_priv_get(a))
100
101 dpdk_helper_ctx_t *g_hc = NULL;
102 static char g_shm_name[DATA_MAX_NAME_LEN] = DPDK_STATS_NAME;
103 static dpdk_stat_cfg_status g_state = DPDK_STAT_STATE_OKAY;
104
105 static int dpdk_stats_reinit_helper();
106 static void dpdk_stats_default_config(void) {
107   dpdk_stats_ctx_t *ec = DPDK_STATS_CTX_GET(g_hc);
108
109   ec->config.interval = plugin_get_interval();
110   for (int i = 0; i < RTE_MAX_ETHPORTS; i++) {
111     ec->config.port_name[i][0] = 0;
112   }
113   /* Enable all ports by default */
114   ec->config.enabled_port_mask = ~0;
115 }
116
117 static int dpdk_stats_preinit(void) {
118   DPDK_STATS_TRACE();
119
120   if (g_hc != NULL) {
121     /* already initialized if config callback was called before init callback */
122     DEBUG("dpdk_stats_preinit: helper already initialized");
123     return 0;
124   }
125
126   int ret = dpdk_helper_init(g_shm_name, sizeof(dpdk_stats_ctx_t), &g_hc);
127   if (ret != 0) {
128     char errbuf[ERR_BUF_SIZE];
129     ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
130           g_shm_name, sstrerror(errno, errbuf, sizeof(errbuf)));
131     return ret;
132   }
133
134   dpdk_stats_default_config();
135   return ret;
136 }
137
138 static int dpdk_stats_config(oconfig_item_t *ci) {
139   DPDK_STATS_TRACE();
140
141   int ret = dpdk_stats_preinit();
142   if (ret) {
143     g_state = DPDK_STAT_STATE_CFG_ERR;
144     return 0;
145   }
146
147   dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
148
149   for (int i = 0; i < ci->children_num; i++) {
150     oconfig_item_t *child = ci->children + i;
151
152     if (strcasecmp("EnabledPortMask", child->key) == 0)
153       ret = cf_util_get_int(child, (int *)&ctx->config.enabled_port_mask);
154     else if (strcasecmp("SharedMemObj", child->key) == 0) {
155       ret = cf_util_get_string_buffer(child, g_shm_name, sizeof(g_shm_name));
156       if (ret == 0)
157         ret = dpdk_stats_reinit_helper();
158     } else if (strcasecmp("EAL", child->key) == 0)
159       ret = dpdk_helper_eal_config_parse(g_hc, child);
160     else if (strcasecmp("PortName", child->key) != 0) {
161       ERROR(DPDK_STATS_PLUGIN ": unrecognized configuration option %s",
162             child->key);
163       ret = -1;
164     }
165
166     if (ret != 0) {
167       g_state = DPDK_STAT_STATE_CFG_ERR;
168       return 0;
169     }
170   }
171
172   DEBUG(DPDK_STATS_PLUGIN ": Enabled Port Mask 0x%X",
173         ctx->config.enabled_port_mask);
174   DEBUG(DPDK_STATS_PLUGIN ": Shared memory object %s", g_shm_name);
175
176   int port_num = 0;
177
178   /* parse port names after EnabledPortMask was parsed */
179   for (int i = 0; i < ci->children_num; i++) {
180     oconfig_item_t *child = ci->children + i;
181
182     if (strcasecmp("PortName", child->key) == 0) {
183
184       while (!(ctx->config.enabled_port_mask & (1 << port_num)))
185         port_num++;
186
187       if (cf_util_get_string_buffer(child, ctx->config.port_name[port_num],
188                                     sizeof(ctx->config.port_name[port_num]))) {
189         g_state = DPDK_STAT_STATE_CFG_ERR;
190         return 0;
191       }
192
193       DEBUG(DPDK_STATS_PLUGIN ": Port %d Name: %s", port_num,
194             ctx->config.port_name[port_num]);
195
196       port_num++;
197     }
198   }
199
200   return 0;
201 }
202
203 static int dpdk_helper_stats_get(dpdk_helper_ctx_t *phc) {
204   int len = 0;
205   int ret = 0;
206   int stats = 0;
207   dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
208
209   /* get stats from DPDK */
210   for (uint8_t i = 0; i < ctx->ports_count; i++) {
211     if (!(ctx->config.enabled_port_mask & (1 << i)))
212       continue;
213
214     ctx->port_read_time[i] = cdtime();
215     /* Store available stats array length for port */
216     len = ctx->port_stats_count[i];
217
218     ret = rte_eth_xstats_get(i, &ctx->xstats[stats], len);
219     if (ret < 0 || ret > len) {
220       DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
221                      ": Error reading stats (port=%d; len=%d, ret=%d)\n",
222                      i, len, ret);
223       ctx->port_stats_count[i] = 0;
224       return -1;
225     }
226 #if RTE_VERSION >= RTE_VERSION_16_07
227     ret = rte_eth_xstats_get_names(i, &ctx->xnames[stats], len);
228     if (ret < 0 || ret > len) {
229       DPDK_CHILD_LOG(DPDK_STATS_PLUGIN
230                      ": Error reading stat names (port=%d; len=%d ret=%d)\n",
231                      i, len, ret);
232       ctx->port_stats_count[i] = 0;
233       return -1;
234     }
235 #endif
236     ctx->port_stats_count[i] = ret;
237     stats += ctx->port_stats_count[i];
238   }
239
240   assert(stats <= ctx->stats_count);
241   return 0;
242 }
243
244 static int dpdk_helper_stats_count_get(dpdk_helper_ctx_t *phc) {
245   uint8_t ports = dpdk_helper_eth_dev_count();
246   if (ports == 0)
247     return -ENODEV;
248
249   dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
250   ctx->ports_count = ports;
251
252   int len = 0;
253   int stats_count = 0;
254   for (int i = 0; i < ports; i++) {
255     if (!(ctx->config.enabled_port_mask & (1 << i)))
256       continue;
257 #if RTE_VERSION >= RTE_VERSION_16_07
258     len = rte_eth_xstats_get_names(i, NULL, 0);
259 #else
260     len = rte_eth_xstats_get(i, NULL, 0);
261 #endif
262     if (len < 0) {
263       DPDK_CHILD_LOG("%s: Cannot get stats count\n", DPDK_STATS_PLUGIN);
264       return -1;
265     }
266     ctx->port_stats_count[i] = len;
267     stats_count += len;
268   }
269
270   DPDK_CHILD_LOG("%s:%s:%d stats_count=%d\n", DPDK_STATS_PLUGIN, __FUNCTION__,
271                  __LINE__, stats_count);
272
273   return stats_count;
274 }
275
276 static int dpdk_stats_get_size(dpdk_helper_ctx_t *phc) {
277   return dpdk_helper_data_size_get(phc) - sizeof(dpdk_stats_ctx_t);
278 }
279
280 int dpdk_helper_command_handler(dpdk_helper_ctx_t *phc, enum DPDK_CMD cmd) {
281   /* this function is called from helper context */
282
283   if (phc == NULL) {
284     DPDK_CHILD_LOG("%s: Invalid argument(phc)\n", DPDK_STATS_PLUGIN);
285     return -EINVAL;
286   }
287
288   if (cmd != DPDK_CMD_GET_STATS) {
289     DPDK_CHILD_LOG("%s: Unknown command (cmd=%d)\n", DPDK_STATS_PLUGIN, cmd);
290     return -EINVAL;
291   }
292
293   int stats_count = dpdk_helper_stats_count_get(phc);
294   if (stats_count < 0) {
295     return stats_count;
296   }
297
298   DPDK_STATS_CTX_GET(phc)->stats_count = stats_count;
299   int stats_size = stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE;
300
301   if (dpdk_stats_get_size(phc) < stats_size) {
302     DPDK_CHILD_LOG(
303         DPDK_STATS_PLUGIN
304         ":%s:%d not enough space for stats (available=%d, needed=%d)\n",
305         __FUNCTION__, __LINE__, (int)dpdk_stats_get_size(phc), stats_size);
306     return -ENOBUFS;
307   }
308
309   return dpdk_helper_stats_get(phc);
310 }
311
312 static void dpdk_stats_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
313                                         const char *cnt_name) {
314   char *type_end;
315   type_end = strrchr(cnt_name, '_');
316
317   if ((type_end != NULL) && (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
318     if (strstr(type_end, "bytes") != NULL) {
319       sstrncpy(cnt_type, "if_rx_octets", cnt_type_len);
320     } else if (strstr(type_end, "error") != NULL) {
321       sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
322     } else if (strstr(type_end, "dropped") != NULL) {
323       sstrncpy(cnt_type, "if_rx_dropped", cnt_type_len);
324     } else if (strstr(type_end, "packets") != NULL) {
325       sstrncpy(cnt_type, "if_rx_packets", cnt_type_len);
326     } else if (strstr(type_end, "_placement") != NULL) {
327       sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
328     } else if (strstr(type_end, "_buff") != NULL) {
329       sstrncpy(cnt_type, "if_rx_errors", cnt_type_len);
330     } else {
331       /* Does not fit obvious type: use a more generic one */
332       sstrncpy(cnt_type, "derive", cnt_type_len);
333     }
334
335   } else if ((type_end != NULL) &&
336              (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
337     if (strstr(type_end, "bytes") != NULL) {
338       sstrncpy(cnt_type, "if_tx_octets", cnt_type_len);
339     } else if (strstr(type_end, "error") != NULL) {
340       sstrncpy(cnt_type, "if_tx_errors", cnt_type_len);
341     } else if (strstr(type_end, "dropped") != NULL) {
342       sstrncpy(cnt_type, "if_tx_dropped", cnt_type_len);
343     } else if (strstr(type_end, "packets") != NULL) {
344       sstrncpy(cnt_type, "if_tx_packets", cnt_type_len);
345     } else {
346       /* Does not fit obvious type: use a more generic one */
347       sstrncpy(cnt_type, "derive", cnt_type_len);
348     }
349   } else if ((type_end != NULL) &&
350              (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
351
352     if (strstr(type_end, "_filters") != NULL) {
353       sstrncpy(cnt_type, "operations", cnt_type_len);
354     } else if (strstr(type_end, "error") != NULL)
355       sstrncpy(cnt_type, "errors", cnt_type_len);
356
357   } else if ((type_end != NULL) &&
358              (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
359     if (strstr(type_end, "error") != NULL) {
360       sstrncpy(cnt_type, "errors", cnt_type_len);
361     }
362   } else {
363     /* Does not fit obvious type, or strrchr error:
364      *   use a more generic type */
365     sstrncpy(cnt_type, "derive", cnt_type_len);
366   }
367 }
368
369 static void dpdk_stats_counter_submit(const char *plugin_instance,
370                                       const char *cnt_name, derive_t value,
371                                       cdtime_t port_read_time) {
372   value_list_t vl = VALUE_LIST_INIT;
373   vl.values = &(value_t){.derive = value};
374   vl.values_len = 1;
375   vl.time = port_read_time;
376   sstrncpy(vl.plugin, DPDK_STATS_PLUGIN, sizeof(vl.plugin));
377   sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
378   dpdk_stats_resolve_cnt_type(vl.type, sizeof(vl.type), cnt_name);
379   sstrncpy(vl.type_instance, cnt_name, sizeof(vl.type_instance));
380   plugin_dispatch_values(&vl);
381 }
382
383 static int dpdk_stats_counters_dispatch(dpdk_helper_ctx_t *phc) {
384   dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(phc);
385
386   /* dispatch stats values to collectd */
387
388   DEBUG("%s:%s:%d ports=%u", DPDK_STATS_PLUGIN, __FUNCTION__, __LINE__,
389         ctx->ports_count);
390
391   int stats_count = 0;
392
393   for (int i = 0; i < ctx->ports_count; i++) {
394     if (!(ctx->config.enabled_port_mask & (1 << i)))
395       continue;
396
397     char dev_name[64];
398     if (ctx->config.port_name[i][0] != 0) {
399       snprintf(dev_name, sizeof(dev_name), "%s", ctx->config.port_name[i]);
400     } else {
401       snprintf(dev_name, sizeof(dev_name), "port.%d", i);
402     }
403
404     DEBUG(" === Dispatch stats for port %d (name=%s; stats_count=%d)", i,
405           dev_name, ctx->port_stats_count[i]);
406
407     for (int j = 0; j < ctx->port_stats_count[i]; j++) {
408       const char *cnt_name = DPDK_STATS_XSTAT_GET_NAME(ctx, stats_count);
409       if (cnt_name == NULL)
410         WARNING("dpdkstat: Invalid counter name");
411       else
412         dpdk_stats_counter_submit(
413             dev_name, cnt_name,
414             (derive_t)DPDK_STATS_XSTAT_GET_VALUE(ctx, stats_count),
415             ctx->port_read_time[i]);
416       stats_count++;
417
418       assert(stats_count <= ctx->stats_count);
419     }
420   }
421
422   return 0;
423 }
424
425 static int dpdk_stats_reinit_helper() {
426   DPDK_STATS_TRACE();
427
428   dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
429
430   size_t data_size = sizeof(dpdk_stats_ctx_t) +
431                      (ctx->stats_count * DPDK_STATS_CTX_GET_XSTAT_SIZE);
432
433   DEBUG("%s:%d helper reinit (new_size=%zu)", __FUNCTION__, __LINE__,
434         data_size);
435
436   dpdk_stats_ctx_t tmp_ctx;
437   dpdk_eal_config_t tmp_eal;
438
439   memcpy(&tmp_ctx, ctx, sizeof(dpdk_stats_ctx_t));
440   dpdk_helper_eal_config_get(g_hc, &tmp_eal);
441
442   dpdk_helper_shutdown(g_hc);
443
444   g_hc = NULL;
445
446   int ret;
447   ret = dpdk_helper_init(g_shm_name, data_size, &g_hc);
448   if (ret != 0) {
449     char errbuf[ERR_BUF_SIZE];
450     ERROR("%s: failed to initialize %s helper(error: %s)", DPDK_STATS_PLUGIN,
451           g_shm_name, sstrerror(errno, errbuf, sizeof(errbuf)));
452     return ret;
453   }
454
455   ctx = DPDK_STATS_CTX_GET(g_hc);
456   memcpy(ctx, &tmp_ctx, sizeof(dpdk_stats_ctx_t));
457   DPDK_STATS_CTX_INIT(ctx);
458   dpdk_helper_eal_config_set(g_hc, &tmp_eal);
459
460   return ret;
461 }
462
463 static int dpdk_stats_read(user_data_t *ud) {
464   DPDK_STATS_TRACE();
465
466   int ret = 0;
467
468   if (g_hc == NULL) {
469     ERROR("dpdk stats plugin not initialized");
470     return -EINVAL;
471   }
472
473   dpdk_stats_ctx_t *ctx = DPDK_STATS_CTX_GET(g_hc);
474
475   int result = 0;
476   ret = dpdk_helper_command(g_hc, DPDK_CMD_GET_STATS, &result,
477                             ctx->config.interval);
478   if (ret != 0) {
479     return 0;
480   }
481
482   if (result == -ENOBUFS) {
483     dpdk_stats_reinit_helper();
484   } else if (result == -ENODEV) {
485     dpdk_helper_shutdown(g_hc);
486   } else if (result == 0) {
487     dpdk_stats_counters_dispatch(g_hc);
488   }
489
490   return 0;
491 }
492
493 static int dpdk_stats_shutdown(void) {
494   DPDK_STATS_TRACE();
495
496   dpdk_helper_shutdown(g_hc);
497   g_hc = NULL;
498
499   return 0;
500 }
501
502 static int dpdk_stats_init(void) {
503   DPDK_STATS_TRACE();
504   int ret = 0;
505
506   if (g_state != DPDK_STAT_STATE_OKAY) {
507     dpdk_stats_shutdown();
508     return -1;
509   }
510
511   ret = dpdk_stats_preinit();
512   if (ret != 0) {
513     return ret;
514   }
515
516   return 0;
517 }
518
519 void module_register(void) {
520   plugin_register_init(DPDK_STATS_PLUGIN, dpdk_stats_init);
521   plugin_register_complex_config(DPDK_STATS_PLUGIN, dpdk_stats_config);
522   plugin_register_complex_read(NULL, DPDK_STATS_PLUGIN, dpdk_stats_read, 0,
523                                NULL);
524   plugin_register_shutdown(DPDK_STATS_PLUGIN, dpdk_stats_shutdown);
525 }