summaryrefslogtreecommitdiffstats
path: root/telemetry/vpp-collectd/vpp/vpp.c
blob: ff70f3503b2d2b4ea6186a9f6de0638fd62ae44f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "collectd.h"
#include "plugin.h"
#include "utils/common/common.h"

#define counter_t vpp_counter_t
#include <vpp-api/client/stat_client.h>
#include <vppinfra/vec.h>
#undef counter_t

/************** OPTIONS ***********************************/
static const char *config_keys[2] = {
    "Verbose",
    "Tag",
};
static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
static bool verbose = false;
static char *tag = NULL;

/************** DATA SOURCES ******************************/
static data_source_t combined_dsrc[2] = {
    {"packets", DS_TYPE_DERIVE, 0, NAN},
    {"bytes", DS_TYPE_DERIVE, 0, NAN},
};

static data_source_t simple_dsrc[1] = {
    {"packets", DS_TYPE_DERIVE, 0, NAN},
};

/************** DATA SETS *********************************/
static data_set_t if_drops_ds = {
    "if_drops",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_punt_ds = {
    "if_punt",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_ip4_ds = {
    "if_ip4",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_ip6_ds = {
    "if_ip6",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_rx_no_buf_ds = {
    "if_rx_no_buf",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_rx_miss_ds = {
    "if_rx_miss",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_rx_error_ds = {
    "if_rx_error",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_tx_error_ds = {
    "if_tx_error",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_mpls_ds = {
    "if_mpls",
    STATIC_ARRAY_SIZE(simple_dsrc),
    simple_dsrc,
};

static data_set_t if_rx_ds = {
    "if_rx",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t if_rx_unicast_ds = {
    "if_rx_unicast",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t if_rx_multicast_ds = {
    "if_rx_multicast",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t if_rx_broadcast_ds = {
    "if_rx_broadcast",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t if_tx_ds = {
    "if_tx",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t if_tx_unicast_ds = {
    "if_tx_unicast",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t if_tx_multicast_ds = {
    "if_tx_multicast",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t if_tx_broadcast_ds = {
    "if_tx_broadcast",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

/**********************************************************/
/********** UTILITY FUNCTIONS *****************************/
/**********************************************************/
/*
 * Utility function used by the read callback to populate a
 * value_list_t and pass it to plugin_dispatch_values.
 */
static int submit(const char *plugin_instance, const char *type,
                  value_t *values, size_t values_len, cdtime_t *timestamp) {
  value_list_t vl = VALUE_LIST_INIT;
  vl.values = values;
  vl.values_len = values_len;

  if (timestamp != NULL) {
    vl.time = *timestamp;
  }

  sstrncpy(vl.plugin, "vpp", sizeof(vl.plugin));
  sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
  sstrncpy(vl.type, type, sizeof(vl.type));

  if (tag != NULL) sstrncpy(vl.type_instance, tag, sizeof(vl.type_instance));

  return plugin_dispatch_values(&vl);
}

/*
 * Utility function to fetch the data set corresponding to the stat name.
 */
static int get_data_set(const char *stat_name, data_set_t *data_set_ptr) {
  if (data_set_ptr == NULL) {
    return 1;
  }

  if (strcmp(stat_name, "/if/rx") == 0) {
    *data_set_ptr = if_rx_ds;
  } else if (strcmp(stat_name, "/if/tx") == 0) {
    *data_set_ptr = if_tx_ds;
  } else if (strcmp(stat_name, "/if/ip4") == 0) {
    *data_set_ptr = if_ip4_ds;
  } else if (strcmp(stat_name, "/if/ip6") == 0) {
    *data_set_ptr = if_ip6_ds;
  } else if (strcmp(stat_name, "/if/drops") == 0) {
    *data_set_ptr = if_drops_ds;
  } else if (!verbose) {
    return 1;
  }

  if (verbose) {
    if (strcmp(stat_name, "/if/punt") == 0) {
      *data_set_ptr = if_punt_ds;
    } else if (strcmp(stat_name, "/if/mpls") == 0) {
      *data_set_ptr = if_mpls_ds;
    } else if (strcmp(stat_name, "/if/rx-no-buf") == 0) {
      *data_set_ptr = if_rx_no_buf_ds;
    } else if (strcmp(stat_name, "/if/rx-miss") == 0) {
      *data_set_ptr = if_rx_miss_ds;
    } else if (strcmp(stat_name, "/if/rx-error") == 0) {
      *data_set_ptr = if_rx_error_ds;
    } else if (strcmp(stat_name, "/if/rx-unicast") == 0) {
      *data_set_ptr = if_rx_unicast_ds;
    } else if (strcmp(stat_name, "/if/rx-multicast") == 0) {
      *data_set_ptr = if_rx_multicast_ds;
    } else if (strcmp(stat_name, "/if/rx-broadcast") == 0) {
      *data_set_ptr = if_rx_broadcast_ds;
    } else if (strcmp(stat_name, "/if/tx-error") == 0) {
      *data_set_ptr = if_tx_error_ds;
    } else if (strcmp(stat_name, "/if/tx-unicast") == 0) {
      *data_set_ptr = if_tx_unicast_ds;
    } else if (strcmp(stat_name, "/if/tx-multicast") == 0) {
      *data_set_ptr = if_tx_multicast_ds;
    } else if (strcmp(stat_name, "/if/tx-broadcast") == 0) {
      *data_set_ptr = if_tx_broadcast_ds;
    } else {
      return 1;
    }
  }

  return 0;
}

/**********************************************************/
/********** CALLBACK FUNCTIONS ****************************/
/**********************************************************/
/*
 * This function is called for each configuration item.
 */
static int vpp_config(const char *key, const char *value) {
  if (strcasecmp(key, "Verbose") == 0) {
    verbose = IS_TRUE(value);
  } else if (strcasecmp(key, "Tag") == 0) {
    if (tag != NULL) {
      free(tag);
      tag = NULL;
    }

    if (strcasecmp(value, "None")) {
      tag = strdup(value);
    }
  } else {
    return 1;
  }

  return 0;
}

/*
 * This function is called once upon startup to initialize the plugin.
 */
static int vpp_init(void) {
  /* Create a heap of 64MB */
  clib_mem_init(0, 64ULL << 20);

  u8 *stat_segment_name = (u8 *)STAT_SEGMENT_SOCKET_FILE;
  int ret = stat_segment_connect((char *)stat_segment_name);

  if (ret) plugin_log(LOG_ERR, "vpp plugin: connecting to segment failed");

  return ret;
}

/*
 * This function is called in regular intervalls to collect the data.
 */
static int vpp_read(void) {
  uint8_t **patterns = {0};
  char **interfaces = {0};

  vec_add1(patterns, (uint8_t *)"^/if");

  uint32_t *dir = stat_segment_ls(patterns);
  stat_segment_data_t *res = stat_segment_dump(dir);

  /* Read all available interfaces */
  for (int k = 0; k < vec_len(res); k++) {
    if (res[k].type == STAT_DIR_TYPE_NAME_VECTOR) {
      for (int i = 0; i < vec_len(res[k].name_vector); i++) {
        vec_add1(interfaces, (char *)res[k].name_vector[i]);
      }
      break;
    }
  }

  cdtime_t timestamp = cdtime();
  data_set_t data_set;
  int err = 0;

  /* Collect results for each interface and submit them */
  for (int i = 0; i < vec_len(res); i++) {
    switch (res[i].type) {
      case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
        for (int k = 0; k < vec_len(res[i].simple_counter_vec); k++) {
          for (int j = 0; j < vec_len(res[i].simple_counter_vec[k]); j++) {
            if (!interfaces[j]) {
              continue;
            }

            if (get_data_set(res[i].name, &data_set)) {
              continue;
            }

            value_t values[1] = {
                (value_t){.derive = res[i].simple_counter_vec[k][j]}};

            err = submit(interfaces[j], data_set.type, values, 1, &timestamp);

            if (err) goto END;
          }
        }
        break;

      case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
        for (int k = 0; k < vec_len(res[i].combined_counter_vec); k++) {
          for (int j = 0; j < vec_len(res[i].combined_counter_vec[k]); j++) {
            if (!interfaces[j]) {
              continue;
            }

            if (get_data_set(res[i].name, &data_set)) {
              continue;
            }

            value_t values[2] = {
                (value_t){.derive = res[i].combined_counter_vec[k][j].packets},
                (value_t){.derive = res[i].combined_counter_vec[k][j].bytes},
            };

            err = submit(interfaces[j], data_set.type, values, 2, &timestamp);

            if (err) goto END;
          }
        }
        break;

      case STAT_DIR_TYPE_SCALAR_INDEX:
        plugin_log(LOG_INFO, "vpp plugin: %.2f %s", res[i].scalar_value,
                   res[i].name);
        break;

      case STAT_DIR_TYPE_NAME_VECTOR:
        break;

      case STAT_DIR_TYPE_ERROR_INDEX:
        break;

      default:
        plugin_log(LOG_WARNING, "vpp plugin: unknown stat type %d",
                   res[i].type);
        break;
    }
  }

END:
  if (err)
    plugin_log(LOG_ERR,
               "vpp plugin: dispatching of results failed with error code %d.",
               err);

  stat_segment_data_free(res);

  return err;
}

/*
 * This function is called when plugin_log () has been used.
 */
static void vpp_log(int severity, const char *msg, user_data_t *ud) {
  printf("[LOG %i] %s\n", severity, msg);
  return;
}

/*
 * This function is called before shutting down collectd.
 */
static int vpp_shutdown(void) {
  plugin_log(LOG_INFO, "vpp plugin: shutting down");

  if (tag != NULL) {
    free(tag);
    tag = NULL;
  }

  stat_segment_disconnect();

  return 0;
}

/*
 * This function is called after loading the plugin to register it with
 * collectd.
 */
void module_register(void) {
  plugin_register_data_set(&if_drops_ds);
  plugin_register_data_set(&if_punt_ds);
  plugin_register_data_set(&if_ip4_ds);
  plugin_register_data_set(&if_ip6_ds);
  plugin_register_data_set(&if_rx_no_buf_ds);
  plugin_register_data_set(&if_rx_miss_ds);
  plugin_register_data_set(&if_rx_error_ds);
  plugin_register_data_set(&if_tx_error_ds);
  plugin_register_data_set(&if_mpls_ds);
  plugin_register_data_set(&if_rx_ds);
  plugin_register_data_set(&if_rx_unicast_ds);
  plugin_register_data_set(&if_rx_multicast_ds);
  plugin_register_data_set(&if_rx_broadcast_ds);
  plugin_register_data_set(&if_tx_ds);
  plugin_register_data_set(&if_tx_unicast_ds);
  plugin_register_data_set(&if_tx_multicast_ds);
  plugin_register_data_set(&if_tx_broadcast_ds);
  plugin_register_log("vpp", vpp_log, /* user data */ NULL);
  plugin_register_config("vpp", vpp_config, config_keys, config_keys_num);
  plugin_register_init("vpp", vpp_init);
  plugin_register_read("vpp", vpp_read);
  plugin_register_shutdown("vpp", vpp_shutdown);
  return;
}