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