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