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