From e70d5757ab32831300243d6a92772725b0714366 Mon Sep 17 00:00:00 2001 From: claudio Date: Thu, 1 Dec 2022 09:14:40 +0000 Subject: [PATCH] Extend and rename ometric_set_int_with_label to ometric_set_int_with_labels Instead of passing a single key value pair allow for multiple keys and values. This is needed for rpki-client where 2 extra key value pairs are needed for some values. To simplify passing simple values introduce a OKV() macro which creates a compound literal array with the NULL terminal. OK tb@ --- usr.sbin/bgpctl/ometric.c | 27 ++++-------- usr.sbin/bgpctl/ometric.h | 7 +-- usr.sbin/bgpctl/output_ometric.c | 76 +++++++++++++++++--------------- 3 files changed, 54 insertions(+), 56 deletions(-) diff --git a/usr.sbin/bgpctl/ometric.c b/usr.sbin/bgpctl/ometric.c index 698cba6e7fc..37abc64fd0f 100644 --- a/usr.sbin/bgpctl/ometric.c +++ b/usr.sbin/bgpctl/ometric.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ometric.c,v 1.3 2022/11/30 10:15:01 claudio Exp $ */ +/* $OpenBSD: ometric.c,v 1.4 2022/12/01 09:14:40 claudio Exp $ */ /* * Copyright (c) 2022 Claudio Jeker @@ -209,15 +209,10 @@ olabels_free(struct olabels *ol) * value needs to be freed with olabels_free(). */ static struct olabels * -olabels_add_extra(struct olabels *ol, const char *key, const char *value) +olabels_add_extras(struct olabels *ol, const char **keys, const char **values) { - const char *keys[2] = { key, NULL }; - const char *values[2] = { value, NULL }; struct olabels *new; - if (value == NULL || *value == '\0') - return ol; - new = olabels_new(keys, values); new->next = olabels_ref(ol); @@ -382,10 +377,8 @@ ometric_set_info(struct ometric *om, const char **keys, const char **values, if (om->type != OMT_INFO) errx(1, "%s incorrect ometric type", __func__); - if (keys != NULL) { - extra = olabels_new(keys, values); - extra->next = olabels_ref(ol); - } + if (keys != NULL) + extra = olabels_add_extras(ol, keys, values); ometric_set_int_value(om, 1, extra != NULL ? extra : ol); olabels_free(extra); @@ -397,7 +390,6 @@ ometric_set_info(struct ometric *om, const char **keys, const char **values, void ometric_set_state(struct ometric *om, const char *state, struct olabels *ol) { - struct olabels *extra; size_t i; int val; @@ -410,9 +402,8 @@ ometric_set_state(struct ometric *om, const char *state, struct olabels *ol) else val = 0; - extra = olabels_add_extra(ol, om->name, om->stateset[i]); - ometric_set_int_value(om, val, extra); - olabels_free(extra); + ometric_set_int_with_labels(om, val, OKV(om->name), + OKV(om->stateset[i]), ol); } } @@ -421,12 +412,12 @@ ometric_set_state(struct ometric *om, const char *state, struct olabels *ol) * the value is copied into the extra label. */ void -ometric_set_int_with_label(struct ometric *om, uint64_t val, const char *key, - const char *value, struct olabels *ol) +ometric_set_int_with_labels(struct ometric *om, uint64_t val, + const char **keys, const char **values, struct olabels *ol) { struct olabels *extra; - extra = olabels_add_extra(ol, key, value); + extra = olabels_add_extras(ol, keys, values); ometric_set_int(om, val, extra); olabels_free(extra); } diff --git a/usr.sbin/bgpctl/ometric.h b/usr.sbin/bgpctl/ometric.h index a1f7d8f974c..bd90f2d8b3d 100644 --- a/usr.sbin/bgpctl/ometric.h +++ b/usr.sbin/bgpctl/ometric.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ometric.h,v 1.2 2022/11/30 10:15:01 claudio Exp $ */ +/* $OpenBSD: ometric.h,v 1.3 2022/12/01 09:14:40 claudio Exp $ */ /* * Copyright (c) 2022 Claudio Jeker @@ -44,5 +44,6 @@ void ometric_set_float(struct ometric *, double, struct olabels *); void ometric_set_info(struct ometric *, const char **, const char **, struct olabels *); void ometric_set_state(struct ometric *, const char *, struct olabels *); -void ometric_set_int_with_label(struct ometric *, uint64_t, const char *, - const char *, struct olabels *); +void ometric_set_int_with_labels(struct ometric *, uint64_t, const char **, + const char **, struct olabels *); +#define OKV(...) (const char *[]){ __VA_ARGS__, NULL } diff --git a/usr.sbin/bgpctl/output_ometric.c b/usr.sbin/bgpctl/output_ometric.c index 23eff200088..0840c0b99cb 100644 --- a/usr.sbin/bgpctl/output_ometric.c +++ b/usr.sbin/bgpctl/output_ometric.c @@ -1,4 +1,4 @@ -/* $OpenBSD: output_ometric.c,v 1.4 2022/11/30 10:15:01 claudio Exp $ */ +/* $OpenBSD: output_ometric.c,v 1.5 2022/12/01 09:14:40 claudio Exp $ */ /* * Copyright (c) 2022 Claudio Jeker @@ -203,27 +203,31 @@ ometric_neighbor_stats(struct peer *p, struct parse_result *arg) ometric_set_int(peer_prefixes_transmit, p->stats.prefix_out_cnt, ol); ometric_set_int(peer_prefixes_receive, p->stats.prefix_cnt, ol); - ometric_set_int_with_label(peer_message_transmit, - p->stats.msg_sent_open, "message", "open", ol); - ometric_set_int_with_label(peer_message_transmit, - p->stats.msg_sent_notification, "message", "notification", ol); - ometric_set_int_with_label(peer_message_transmit, - p->stats.msg_sent_update, "message", "update", ol); - ometric_set_int_with_label(peer_message_transmit, - p->stats.msg_sent_keepalive, "message", "keepalive", ol); - ometric_set_int_with_label(peer_message_transmit, - p->stats.msg_sent_rrefresh, "message", "route_refresh", ol); - - ometric_set_int_with_label(peer_message_recieve, - p->stats.msg_rcvd_open, "message", "open", ol); - ometric_set_int_with_label(peer_message_recieve, - p->stats.msg_rcvd_notification, "message", "notification", ol); - ometric_set_int_with_label(peer_message_recieve, - p->stats.msg_rcvd_update, "message", "update", ol); - ometric_set_int_with_label(peer_message_recieve, - p->stats.msg_rcvd_keepalive, "message", "keepalive", ol); - ometric_set_int_with_label(peer_message_recieve, - p->stats.msg_rcvd_rrefresh, "message", "route_refresh", ol); + ometric_set_int_with_labels(peer_message_transmit, + p->stats.msg_sent_open, OKV("messages"), OKV("open"), ol); + ometric_set_int_with_labels(peer_message_transmit, + p->stats.msg_sent_notification, OKV("messages"), + OKV("notification"), ol); + ometric_set_int_with_labels(peer_message_transmit, + p->stats.msg_sent_update, OKV("messages"), OKV("update"), ol); + ometric_set_int_with_labels(peer_message_transmit, + p->stats.msg_sent_keepalive, OKV("messages"), OKV("keepalive"), ol); + ometric_set_int_with_labels(peer_message_transmit, + p->stats.msg_sent_rrefresh, OKV("messages"), OKV("route_refresh"), + ol); + + ometric_set_int_with_labels(peer_message_recieve, + p->stats.msg_rcvd_open, OKV("messages"), OKV("open"), ol); + ometric_set_int_with_labels(peer_message_recieve, + p->stats.msg_rcvd_notification, OKV("messages"), + OKV("notification"), ol); + ometric_set_int_with_labels(peer_message_recieve, + p->stats.msg_rcvd_update, OKV("messages"), OKV("update"), ol); + ometric_set_int_with_labels(peer_message_recieve, + p->stats.msg_rcvd_keepalive, OKV("messages"), OKV("keepalive"), ol); + ometric_set_int_with_labels(peer_message_recieve, + p->stats.msg_rcvd_rrefresh, OKV("messages"), OKV("route_refresh"), + ol); ometric_set_int(peer_update_transmit, p->stats.prefix_sent_update, ol); ometric_set_int(peer_update_pending, p->stats.pending_update, ol); @@ -249,13 +253,14 @@ ometric_rib_mem_element(const char *v, uint64_t count, uint64_t size, uint64_t refs) { if (count != UINT64_MAX) - ometric_set_int_with_label(rde_mem_count, count, "type", v, - NULL); + ometric_set_int_with_labels(rde_mem_count, count, + OKV("type"), OKV(v), NULL); if (size != UINT64_MAX) - ometric_set_int_with_label(rde_mem_size, size, "type", v, NULL); + ometric_set_int_with_labels(rde_mem_size, size, + OKV("type"), OKV(v), NULL); if (refs != UINT64_MAX) - ometric_set_int_with_label(rde_mem_ref_count, refs, "type", v, - NULL); + ometric_set_int_with_labels(rde_mem_ref_count, refs, + OKV("type"), OKV(v), NULL); } static void @@ -297,14 +302,15 @@ ometric_rib_mem(struct rde_memstats *stats) stats->attr_data, UINT64_MAX); ometric_set_int(rde_table_count, stats->aset_cnt, NULL); - ometric_set_int_with_label(rde_set_size, stats->aset_size, - "type", "as_set", NULL); - ometric_set_int_with_label(rde_set_count, stats->aset_nmemb, - "type", "as_set", NULL); - ometric_set_int_with_label(rde_set_size, stats->pset_size, - "type", "prefix_set", NULL); - ometric_set_int_with_label(rde_set_count, stats->pset_cnt, - "type", "prefix_set", NULL); + + ometric_set_int_with_labels(rde_set_size, stats->aset_size, + OKV("type"), OKV("as_set"), NULL); + ometric_set_int_with_labels(rde_set_count, stats->aset_nmemb, + OKV("type"), OKV("as_set"), NULL); + ometric_set_int_with_labels(rde_set_size, stats->pset_size, + OKV("type"), OKV("prefix_set"), NULL); + ometric_set_int_with_labels(rde_set_count, stats->pset_cnt, + OKV("type"), OKV("prefix_set"), NULL); ometric_rib_mem_element("set_total", UINT64_MAX, stats->aset_size + stats->pset_size, UINT64_MAX); } -- 2.20.1