]>
git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_utils_counters.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * This file is part of the PCEPlib, a PCEP protocol library.
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
7 * Author : Brady Johnson <brady@voltanet.io>
13 * Implementation of PCEP Counters.
22 #include "pcep_utils_counters.h"
23 #include "pcep_utils_logging.h"
24 #include "pcep_utils_memory.h"
26 struct counters_group
*create_counters_group(const char *group_name
,
27 uint16_t max_subgroups
)
29 if (group_name
== NULL
) {
32 "%s: Cannot create counters group: group_name is NULL.",
37 if (max_subgroups
> MAX_COUNTER_GROUPS
) {
40 "%s: Cannot create counters group: max_subgroups [%d] is larger than max the [%d].",
41 __func__
, max_subgroups
, MAX_COUNTER_GROUPS
);
45 struct counters_group
*group
=
46 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counters_group
));
47 memset(group
, 0, sizeof(struct counters_group
));
49 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counters_subgroup
*)
50 * (max_subgroups
+ 1));
51 memset(group
->subgroups
, 0,
52 sizeof(struct counters_subgroup
*) * (max_subgroups
+ 1));
54 strlcpy(group
->counters_group_name
, group_name
,
55 sizeof(group
->counters_group_name
));
56 group
->max_subgroups
= max_subgroups
;
57 group
->start_time
= time(NULL
);
62 struct counters_subgroup
*create_counters_subgroup(const char *subgroup_name
,
64 uint16_t max_counters
)
66 if (subgroup_name
== NULL
) {
69 "%s: Cannot create counters subgroup: subgroup_name is NULL.",
74 if (max_counters
> MAX_COUNTERS
) {
77 "%s: Cannot create counters subgroup: max_counters [%d] is larger than the max [%d].",
78 __func__
, max_counters
, MAX_COUNTERS
);
82 if (subgroup_id
> MAX_COUNTER_GROUPS
) {
85 "%s: Cannot create counters subgroup: subgroup_id [%d] is larger than max the [%d].",
86 __func__
, subgroup_id
, MAX_COUNTER_GROUPS
);
90 struct counters_subgroup
*subgroup
=
91 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counters_subgroup
));
92 memset(subgroup
, 0, sizeof(struct counters_subgroup
));
93 subgroup
->counters
= pceplib_malloc(
94 PCEPLIB_INFRA
, sizeof(struct counter
*) * (max_counters
+ 1));
95 memset(subgroup
->counters
, 0,
96 sizeof(struct counter
*) * (max_counters
+ 1));
98 strlcpy(subgroup
->counters_subgroup_name
, subgroup_name
,
99 sizeof(subgroup
->counters_subgroup_name
));
100 subgroup
->subgroup_id
= subgroup_id
;
101 subgroup
->max_counters
= max_counters
;
106 struct counters_subgroup
*
107 clone_counters_subgroup(struct counters_subgroup
*subgroup
,
108 const char *subgroup_name
, uint16_t subgroup_id
)
110 if (subgroup
== NULL
) {
113 "%s: Cannot clone counters subgroup: input counters_subgroup is NULL.",
118 if (subgroup_name
== NULL
) {
121 "%s: Cannot clone counters subgroup: subgroup_name is NULL.",
126 if (subgroup_id
> MAX_COUNTER_GROUPS
) {
129 "%s: Cannot clone counters subgroup: subgroup_id [%d] is larger than max the [%d].",
130 __func__
, subgroup_id
, MAX_COUNTER_GROUPS
);
134 struct counters_subgroup
*cloned_subgroup
= create_counters_subgroup(
135 subgroup_name
, subgroup_id
, subgroup
->max_counters
);
137 for (; i
<= subgroup
->max_counters
; i
++) {
138 struct counter
*counter
= subgroup
->counters
[i
];
139 if (counter
!= NULL
) {
140 create_subgroup_counter(cloned_subgroup
,
142 counter
->counter_name
);
146 return cloned_subgroup
;
149 bool add_counters_subgroup(struct counters_group
*group
,
150 struct counters_subgroup
*subgroup
)
155 "%s: Cannot add counters subgroup: counters_group is NULL.",
160 if (subgroup
== NULL
) {
163 "%s: Cannot add counters subgroup: counters_subgroup is NULL.",
168 if (subgroup
->subgroup_id
>= group
->max_subgroups
) {
171 "%s: Cannot add counters subgroup: counters_subgroup id [%d] is larger than the group max_subgroups [%d].",
172 __func__
, subgroup
->subgroup_id
, group
->max_subgroups
);
176 group
->num_subgroups
++;
177 group
->subgroups
[subgroup
->subgroup_id
] = subgroup
;
182 bool create_subgroup_counter(struct counters_subgroup
*subgroup
,
183 uint32_t counter_id
, const char *counter_name
)
185 if (subgroup
== NULL
) {
188 "%s: Cannot create subgroup counter: counters_subgroup is NULL.",
193 if (counter_id
>= subgroup
->max_counters
) {
196 "%s: Cannot create subgroup counter: counter_id [%d] is larger than the subgroup max_counters [%d].",
197 __func__
, counter_id
, subgroup
->max_counters
);
201 if (counter_name
== NULL
) {
204 "%s: Cannot create subgroup counter: counter_name is NULL.",
209 struct counter
*counter
=
210 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counter
));
211 memset(counter
, 0, sizeof(struct counter
));
212 counter
->counter_id
= counter_id
;
213 strlcpy(counter
->counter_name
, counter_name
,
214 sizeof(counter
->counter_name
));
216 subgroup
->num_counters
++;
217 subgroup
->counters
[counter
->counter_id
] = counter
;
222 bool delete_counters_group(struct counters_group
*group
)
227 "%s: Cannot delete group counters: counters_group is NULL.",
233 for (; i
<= group
->max_subgroups
; i
++) {
234 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
235 if (subgroup
!= NULL
) {
236 delete_counters_subgroup(subgroup
);
240 pceplib_free(PCEPLIB_INFRA
, group
->subgroups
);
241 pceplib_free(PCEPLIB_INFRA
, group
);
246 bool delete_counters_subgroup(struct counters_subgroup
*subgroup
)
248 if (subgroup
== NULL
|| subgroup
->counters
== NULL
) {
251 "%s: Cannot delete subgroup counters: counters_subgroup is NULL.",
257 for (; i
<= subgroup
->max_counters
; i
++) {
258 struct counter
*counter
= subgroup
->counters
[i
];
259 if (counter
!= NULL
) {
260 pceplib_free(PCEPLIB_INFRA
, counter
);
264 pceplib_free(PCEPLIB_INFRA
, subgroup
->counters
);
265 pceplib_free(PCEPLIB_INFRA
, subgroup
);
270 bool reset_group_counters(struct counters_group
*group
)
275 "%s: Cannot reset group counters: counters_group is NULL.",
281 for (; i
<= group
->max_subgroups
; i
++) {
282 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
283 if (subgroup
!= NULL
) {
284 reset_subgroup_counters(subgroup
);
288 group
->start_time
= time(NULL
);
293 bool reset_subgroup_counters(struct counters_subgroup
*subgroup
)
295 if (subgroup
== NULL
) {
298 "%s: Cannot reset subgroup counters: counters_subgroup is NULL.",
304 for (; i
<= subgroup
->max_counters
; i
++) {
305 struct counter
*counter
= subgroup
->counters
[i
];
306 if (counter
!= NULL
) {
307 counter
->counter_value
= 0;
314 bool increment_counter(struct counters_group
*group
, uint16_t subgroup_id
,
320 "%s: Cannot increment counter: counters_group is NULL.",
325 if (subgroup_id
>= group
->max_subgroups
) {
328 "%s: Cannot increment counter: subgroup_id [%d] is larger than the group max_subgroups [%d].",
329 __func__
, subgroup_id
, group
->max_subgroups
);
333 struct counters_subgroup
*subgroup
= group
->subgroups
[subgroup_id
];
334 if (subgroup
== NULL
) {
337 "%s: Cannot increment counter: counters_subgroup in counters_group is NULL.",
342 return increment_subgroup_counter(subgroup
, counter_id
);
345 bool increment_subgroup_counter(struct counters_subgroup
*subgroup
,
348 if (subgroup
== NULL
) {
351 "%s: Cannot increment counter: counters_subgroup is NULL.",
356 if (counter_id
>= subgroup
->max_counters
) {
359 "%s: Cannot increment counter: counter_id [%d] is larger than the subgroup max_counters [%d].",
360 __func__
, counter_id
, subgroup
->max_counters
);
364 if (subgroup
->counters
[counter_id
] == NULL
) {
367 "%s: Cannot increment counter: No counter exists for counter_id [%d].",
368 __func__
, counter_id
);
372 subgroup
->counters
[counter_id
]->counter_value
++;
377 bool dump_counters_group_to_log(struct counters_group
*group
)
382 "%s: Cannot dump group counters to log: counters_group is NULL.",
387 time_t now
= time(NULL
);
390 "%s: PCEP Counters group:\n %s \n Sub-Groups [%d] \n Active for [%d seconds]",
391 __func__
, group
->counters_group_name
, group
->num_subgroups
,
392 (now
- group
->start_time
));
395 for (; i
<= group
->max_subgroups
; i
++) {
396 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
397 if (subgroup
!= NULL
) {
398 dump_counters_subgroup_to_log(subgroup
);
405 bool dump_counters_subgroup_to_log(struct counters_subgroup
*subgroup
)
407 if (subgroup
== NULL
) {
410 "%s: Cannot dump subgroup counters to log: counters_subgroup is NULL.",
416 "%s: \tPCEP Counters sub-group [%s] with [%d] counters",
417 __func__
, subgroup
->counters_subgroup_name
,
418 subgroup
->num_counters
);
421 for (; i
<= subgroup
->max_counters
; i
++) {
422 struct counter
*counter
= subgroup
->counters
[i
];
423 if (counter
!= NULL
) {
424 pcep_log(LOG_INFO
, "%s: \t\t%s %d", __func__
,
425 counter
->counter_name
, counter
->counter_value
);
432 struct counters_subgroup
*find_subgroup(const struct counters_group
*group
,
433 uint16_t subgroup_id
)
436 for (; i
<= group
->max_subgroups
; i
++) {
437 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
438 if (subgroup
!= NULL
) {
439 if (subgroup
->subgroup_id
== subgroup_id
) {
448 uint32_t subgroup_counters_total(struct counters_subgroup
*subgroup
)
450 if (subgroup
== NULL
) {
453 uint32_t counter_total
= 0;
455 for (; i
<= subgroup
->max_counters
; i
++) {
456 struct counter
*counter
= subgroup
->counters
[i
];
457 if (counter
!= NULL
) {
458 counter_total
+= counter
->counter_value
;
462 return counter_total
;