aboutsummaryrefslogtreecommitdiffstats
path: root/telemetry/vpp-collectd/vpp-hicn/vpp_hicn.c
blob: a724c1124fc613b078b9acb4845134d57d3a2441 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * 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.
 */

/* Keep order as it is */
#include "common.h"
#include <config.h>

#define counter_t vpp_counter_t
#include <vapi/hicn.api.vapi.h>
#include <vapi/vapi_safe.h>
#undef counter_t

DEFINE_VAPI_MSG_IDS_HICN_API_JSON
vapi_ctx_t vapi_ctx;

/************** 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 packets_dsrc[1] = {
    {"packets", DS_TYPE_GAUGE, 0, NAN},
};

static data_source_t interests_dsrc[1] = {
    {"interests", DS_TYPE_GAUGE, 0, NAN},
};

static data_source_t data_dsrc[1] = {
    {"data", DS_TYPE_GAUGE, 0, NAN},
};

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

/************** DATA SETS NODE ****************************/
static data_set_t pkts_processed_ds = {
    "pkts_processed",
    STATIC_ARRAY_SIZE(packets_dsrc),
    packets_dsrc,
};

static data_set_t pkts_interest_count_ds = {
    "pkts_interest_count",
    STATIC_ARRAY_SIZE(packets_dsrc),
    packets_dsrc,
};

static data_set_t pkts_data_count_ds = {
    "pkts_data_count",
    STATIC_ARRAY_SIZE(packets_dsrc),
    packets_dsrc,
};

static data_set_t pkts_from_cache_count_ds = {
    "pkts_from_cache_count",
    STATIC_ARRAY_SIZE(packets_dsrc),
    packets_dsrc,
};

static data_set_t pkts_no_pit_count_ds = {
    "pkts_no_pit_count",
    STATIC_ARRAY_SIZE(packets_dsrc),
    packets_dsrc,
};

static data_set_t pit_expired_count_ds = {
    "pit_expired_count",
    STATIC_ARRAY_SIZE(interests_dsrc),
    interests_dsrc,
};

static data_set_t cs_expired_count_ds = {
    "cs_expired_count",
    STATIC_ARRAY_SIZE(data_dsrc),
    data_dsrc,
};

static data_set_t cs_lru_count_ds = {
    "cs_lru_count",
    STATIC_ARRAY_SIZE(data_dsrc),
    data_dsrc,
};

static data_set_t pkts_drop_no_buf_ds = {
    "pkts_drop_no_buf",
    STATIC_ARRAY_SIZE(packets_dsrc),
    packets_dsrc,
};

static data_set_t interests_aggregated_ds = {
    "interests_aggregated",
    STATIC_ARRAY_SIZE(interests_dsrc),
    interests_dsrc,
};

static data_set_t interests_retx_ds = {
    "interests_retx",
    STATIC_ARRAY_SIZE(interests_dsrc),
    interests_dsrc,
};

static data_set_t interests_hash_collision_ds = {
    "interests_hash_collision",
    STATIC_ARRAY_SIZE(interests_dsrc),
    interests_dsrc,
};

static data_set_t pit_entries_count_ds = {
    "pit_entries_count",
    STATIC_ARRAY_SIZE(interests_dsrc),
    interests_dsrc,
};

static data_set_t cs_entries_count_ds = {
    "cs_entries_count",
    STATIC_ARRAY_SIZE(data_dsrc),
    data_dsrc,
};

static data_set_t cs_entries_ntw_count_ds = {
    "cs_entries_ntw_count",
    STATIC_ARRAY_SIZE(data_dsrc),
    data_dsrc,
};

/************** DATA SETS FACE ****************************/
static data_set_t irx_ds = {
    "irx",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t itx_ds = {
    "itx",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t drx_ds = {
    "drx",
    STATIC_ARRAY_SIZE(combined_dsrc),
    combined_dsrc,
};

static data_set_t dtx_ds = {
    "dtx",
    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_hicn", 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);
}

/**********************************************************/
/********** CALLBACK FUNCTIONS ****************************/
/**********************************************************/
/*
 * This function is called for each configuration item.
 */
static int vpp_hicn_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;
}

/*
 * Callback called by the hICN plugin API when node stats are ready.
 */
static vapi_error_e
parse_node_stats(vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv,
                 bool is_last,
                 vapi_payload_hicn_api_node_stats_get_reply *reply) {
  if (reply == NULL || rv != VAPI_OK)
    return rv;

  if (reply->retval != VAPI_OK)
    return reply->retval;

  char *node_name = "node";
  value_t values[1];
  cdtime_t timestamp = cdtime();

  values[0] = (value_t){.gauge = reply->pkts_processed};
  submit(node_name, pkts_processed_ds.type, values, 1, &timestamp);
  values[0] = (value_t){.gauge = reply->pkts_interest_count};
  submit(node_name, pkts_interest_count_ds.type, values, 1, &timestamp);
  values[0] = (value_t){.gauge = reply->pkts_data_count};
  submit(node_name, pkts_data_count_ds.type, values, 1, &timestamp);
  values[0] = (value_t){.gauge = reply->interests_retx};
  submit(node_name, interests_retx_ds.type, values, 1, &timestamp);
  values[0] = (value_t){.gauge = reply->pit_entries_count};
  submit(node_name, pit_entries_count_ds.type, values, 1, &timestamp);
  values[0] = (value_t){.gauge = reply->cs_entries_count};
  submit(node_name, cs_entries_count_ds.type, values, 1, &timestamp);

  if (verbose) {
    values[0] = (value_t){.gauge = reply->pkts_from_cache_count};
    submit(node_name, pkts_from_cache_count_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->interests_aggregated};
    submit(node_name, interests_aggregated_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->cs_expired_count};
    submit(node_name, cs_expired_count_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->cs_lru_count};
    submit(node_name, cs_lru_count_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->pit_expired_count};
    submit(node_name, pit_expired_count_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->pkts_no_pit_count};
    submit(node_name, pkts_no_pit_count_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->pkts_drop_no_buf};
    submit(node_name, pkts_drop_no_buf_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->interests_hash_collision};
    submit(node_name, interests_hash_collision_ds.type, values, 1, &timestamp);
    values[0] = (value_t){.gauge = reply->cs_entries_ntw_count};
    submit(node_name, cs_entries_ntw_count_ds.type, values, 1, &timestamp);
  }

  return VAPI_OK;
}

/*
 * Callback called by the hICN plugin API when face stats are ready.
 */
static vapi_error_e
parse_face_stats(vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv,
                 bool is_last,
                 vapi_payload_hicn_api_face_stats_details *reply) {
  if (reply == NULL || rv != VAPI_OK)
    return rv;

  if (reply->retval != VAPI_OK)
    return reply->retval;

  char face_name[10];
  snprintf(face_name, 10, "face%u", reply->faceid);
  value_t values[2];
  cdtime_t timestamp = cdtime();

  values[0] = (value_t){.derive = reply->irx_packets};
  values[1] = (value_t){.derive = reply->irx_bytes};
  submit(face_name, irx_ds.type, values, 2, &timestamp);
  values[0] = (value_t){.derive = reply->itx_packets};
  values[1] = (value_t){.derive = reply->itx_bytes};
  submit(face_name, itx_ds.type, values, 2, &timestamp);
  values[0] = (value_t){.derive = reply->drx_packets};
  values[1] = (value_t){.derive = reply->drx_bytes};
  submit(face_name, drx_ds.type, values, 2, &timestamp);
  values[0] = (value_t){.derive = reply->dtx_packets};
  values[1] = (value_t){.derive = reply->dtx_bytes};
  submit(face_name, dtx_ds.type, values, 2, &timestamp);

  return VAPI_OK;
}

/*
 * This function is called once upon startup to initialize the plugin.
 */
static int vpp_hicn_init(void) {
  int ret = vapi_connect_safe(&vapi_ctx, 0);

  if (ret)
    plugin_log(LOG_ERR, "vpp_hicn plugin: vapi_connect_safe failed");

  return ret;
}

/*
 * This function is called in regular intervalls to collect the data.
 */
static int vpp_hicn_read(void) {
  int err = VAPI_OK;

  vapi_lock();

  // NODE
  vapi_msg_hicn_api_node_stats_get *hicn_node_stats_msg;
  hicn_node_stats_msg = vapi_alloc_hicn_api_node_stats_get(vapi_ctx);

  if (!hicn_node_stats_msg) {
    plugin_log(LOG_ERR,
               "vpp_hicn plugin: could not create hicn_node_stats message");
    err = VAPI_ENOMEM;
    goto END;
  }

  err = vapi_hicn_api_node_stats_get(vapi_ctx, hicn_node_stats_msg,
                                     parse_node_stats, NULL);

  if (err) {
    plugin_log(LOG_ERR,
               "vpp_hicn plugin: query of node stats failed with error %d",
               err);
    goto END;
  }

  // FACES
  vapi_msg_hicn_api_face_stats_dump *hicn_face_stats_msg;
  hicn_face_stats_msg = vapi_alloc_hicn_api_face_stats_dump(vapi_ctx);

  if (!hicn_face_stats_msg) {
    plugin_log(LOG_ERR,
               "vpp_hicn plugin: could not create hicn_face_stats message");
    err = VAPI_ENOMEM;
    goto END;
  }

  err = vapi_hicn_api_face_stats_dump(vapi_ctx, hicn_face_stats_msg,
                                      parse_face_stats, NULL);

  if (err) {
    plugin_log(LOG_ERR,
               "vpp_hicn plugin: query of face stats failed with error %d",
               err);
    goto END;
  }

END:
  vapi_unlock();

  return err;
}

/*
 * This function is called when plugin_log () has been used.
 */
static void vpp_hicn_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_hicn_shutdown(void) {
  plugin_log(LOG_INFO, "vpp_hicn plugin: shutting down");

  int ret = vapi_disconnect_safe();
  plugin_log(LOG_INFO, "vpp_hicn plugin: disconnect vapi %s",
             ret == 0 ? "ok" : "error");

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

  return ret;
}

/*
 * This function is called after loading the plugin to register it with
 * collectd.
 */
void module_register(void) {
  // data sets face
  plugin_register_data_set(&irx_ds);
  plugin_register_data_set(&itx_ds);
  plugin_register_data_set(&drx_ds);
  plugin_register_data_set(&dtx_ds);
  // data sets node
  plugin_register_data_set(&pkts_processed_ds);
  plugin_register_data_set(&pkts_interest_count_ds);
  plugin_register_data_set(&pkts_data_count_ds);
  plugin_register_data_set(&pkts_from_cache_count_ds);
  plugin_register_data_set(&pkts_no_pit_count_ds);
  plugin_register_data_set(&pit_expired_count_ds);
  plugin_register_data_set(&cs_expired_count_ds);
  plugin_register_data_set(&cs_lru_count_ds);
  plugin_register_data_set(&pkts_drop_no_buf_ds);
  plugin_register_data_set(&interests_aggregated_ds);
  plugin_register_data_set(&interests_retx_ds);
  plugin_register_data_set(&interests_hash_collision_ds);
  plugin_register_data_set(&pit_entries_count_ds);
  plugin_register_data_set(&cs_entries_count_ds);
  plugin_register_data_set(&cs_entries_ntw_count_ds);
  // callbacks
  plugin_register_log("vpp_hicn", vpp_hicn_log, /* user data */ NULL);
  plugin_register_config("vpp_hicn", vpp_hicn_config, config_keys,
                         config_keys_num);
  plugin_register_init("vpp_hicn", vpp_hicn_init);
  plugin_register_read("vpp_hicn", vpp_hicn_read);
  plugin_register_shutdown("vpp_hicn", vpp_hicn_shutdown);
  return;
}