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