]>
git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_utils_counters.c
2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
25 * Implementation of PCEP Counters.
34 #include "pcep_utils_counters.h"
35 #include "pcep_utils_logging.h"
36 #include "pcep_utils_memory.h"
38 struct counters_group
*create_counters_group(const char *group_name
,
39 uint16_t max_subgroups
)
41 if (group_name
== NULL
) {
44 "%s: Cannot create counters group: group_name is NULL.",
49 if (max_subgroups
> MAX_COUNTER_GROUPS
) {
52 "%s: Cannot create counters group: max_subgroups [%d] is larger than max the [%d].",
53 __func__
, max_subgroups
, MAX_COUNTER_GROUPS
);
57 struct counters_group
*group
=
58 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counters_group
));
59 memset(group
, 0, sizeof(struct counters_group
));
61 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counters_subgroup
*)
62 * (max_subgroups
+ 1));
63 memset(group
->subgroups
, 0,
64 sizeof(struct counters_subgroup
*) * (max_subgroups
+ 1));
66 strlcpy(group
->counters_group_name
, group_name
,
67 sizeof(group
->counters_group_name
));
68 group
->max_subgroups
= max_subgroups
;
69 group
->start_time
= time(NULL
);
74 struct counters_subgroup
*create_counters_subgroup(const char *subgroup_name
,
76 uint16_t max_counters
)
78 if (subgroup_name
== NULL
) {
81 "%s: Cannot create counters subgroup: subgroup_name is NULL.",
86 if (max_counters
> MAX_COUNTERS
) {
89 "%s: Cannot create counters subgroup: max_counters [%d] is larger than the max [%d].",
90 __func__
, max_counters
, MAX_COUNTERS
);
94 if (subgroup_id
> MAX_COUNTER_GROUPS
) {
97 "%s: Cannot create counters subgroup: subgroup_id [%d] is larger than max the [%d].",
98 __func__
, subgroup_id
, MAX_COUNTER_GROUPS
);
102 struct counters_subgroup
*subgroup
=
103 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counters_subgroup
));
104 memset(subgroup
, 0, sizeof(struct counters_subgroup
));
105 subgroup
->counters
= pceplib_malloc(
106 PCEPLIB_INFRA
, sizeof(struct counter
*) * (max_counters
+ 1));
107 memset(subgroup
->counters
, 0,
108 sizeof(struct counter
*) * (max_counters
+ 1));
110 strlcpy(subgroup
->counters_subgroup_name
, subgroup_name
,
111 sizeof(subgroup
->counters_subgroup_name
));
112 subgroup
->subgroup_id
= subgroup_id
;
113 subgroup
->max_counters
= max_counters
;
118 struct counters_subgroup
*
119 clone_counters_subgroup(struct counters_subgroup
*subgroup
,
120 const char *subgroup_name
, uint16_t subgroup_id
)
122 if (subgroup
== NULL
) {
125 "%s: Cannot clone counters subgroup: input counters_subgroup is NULL.",
130 if (subgroup_name
== NULL
) {
133 "%s: Cannot clone counters subgroup: subgroup_name is NULL.",
138 if (subgroup_id
> MAX_COUNTER_GROUPS
) {
141 "%s: Cannot clone counters subgroup: subgroup_id [%d] is larger than max the [%d].",
142 __func__
, subgroup_id
, MAX_COUNTER_GROUPS
);
146 struct counters_subgroup
*cloned_subgroup
= create_counters_subgroup(
147 subgroup_name
, subgroup_id
, subgroup
->max_counters
);
149 for (; i
<= subgroup
->max_counters
; i
++) {
150 struct counter
*counter
= subgroup
->counters
[i
];
151 if (counter
!= NULL
) {
152 create_subgroup_counter(cloned_subgroup
,
154 counter
->counter_name
);
158 return cloned_subgroup
;
161 bool add_counters_subgroup(struct counters_group
*group
,
162 struct counters_subgroup
*subgroup
)
167 "%s: Cannot add counters subgroup: counters_group is NULL.",
172 if (subgroup
== NULL
) {
175 "%s: Cannot add counters subgroup: counters_subgroup is NULL.",
180 if (subgroup
->subgroup_id
>= group
->max_subgroups
) {
183 "%s: Cannot add counters subgroup: counters_subgroup id [%d] is larger than the group max_subgroups [%d].",
184 __func__
, subgroup
->subgroup_id
, group
->max_subgroups
);
188 group
->num_subgroups
++;
189 group
->subgroups
[subgroup
->subgroup_id
] = subgroup
;
194 bool create_subgroup_counter(struct counters_subgroup
*subgroup
,
195 uint32_t counter_id
, const char *counter_name
)
197 if (subgroup
== NULL
) {
200 "%s: Cannot create subgroup counter: counters_subgroup is NULL.",
205 if (counter_id
>= subgroup
->max_counters
) {
208 "%s: Cannot create subgroup counter: counter_id [%d] is larger than the subgroup max_counters [%d].",
209 __func__
, counter_id
, subgroup
->max_counters
);
213 if (counter_name
== NULL
) {
216 "%s: Cannot create subgroup counter: counter_name is NULL.",
221 struct counter
*counter
=
222 pceplib_malloc(PCEPLIB_INFRA
, sizeof(struct counter
));
223 memset(counter
, 0, sizeof(struct counter
));
224 counter
->counter_id
= counter_id
;
225 strlcpy(counter
->counter_name
, counter_name
,
226 sizeof(counter
->counter_name
));
228 subgroup
->num_counters
++;
229 subgroup
->counters
[counter
->counter_id
] = counter
;
234 bool delete_counters_group(struct counters_group
*group
)
239 "%s: Cannot delete group counters: counters_group is NULL.",
245 for (; i
<= group
->max_subgroups
; i
++) {
246 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
247 if (subgroup
!= NULL
) {
248 delete_counters_subgroup(subgroup
);
252 pceplib_free(PCEPLIB_INFRA
, group
->subgroups
);
253 pceplib_free(PCEPLIB_INFRA
, group
);
258 bool delete_counters_subgroup(struct counters_subgroup
*subgroup
)
260 if (subgroup
== NULL
|| subgroup
->counters
== NULL
) {
263 "%s: Cannot delete subgroup counters: counters_subgroup is NULL.",
269 for (; i
<= subgroup
->max_counters
; i
++) {
270 struct counter
*counter
= subgroup
->counters
[i
];
271 if (counter
!= NULL
) {
272 pceplib_free(PCEPLIB_INFRA
, counter
);
276 pceplib_free(PCEPLIB_INFRA
, subgroup
->counters
);
277 pceplib_free(PCEPLIB_INFRA
, subgroup
);
282 bool reset_group_counters(struct counters_group
*group
)
287 "%s: Cannot reset group counters: counters_group is NULL.",
293 for (; i
<= group
->max_subgroups
; i
++) {
294 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
295 if (subgroup
!= NULL
) {
296 reset_subgroup_counters(subgroup
);
300 group
->start_time
= time(NULL
);
305 bool reset_subgroup_counters(struct counters_subgroup
*subgroup
)
307 if (subgroup
== NULL
) {
310 "%s: Cannot reset subgroup counters: counters_subgroup is NULL.",
316 for (; i
<= subgroup
->max_counters
; i
++) {
317 struct counter
*counter
= subgroup
->counters
[i
];
318 if (counter
!= NULL
) {
319 counter
->counter_value
= 0;
326 bool increment_counter(struct counters_group
*group
, uint16_t subgroup_id
,
332 "%s: Cannot increment counter: counters_group is NULL.",
337 if (subgroup_id
>= group
->max_subgroups
) {
340 "%s: Cannot increment counter: subgroup_id [%d] is larger than the group max_subgroups [%d].",
341 __func__
, subgroup_id
, group
->max_subgroups
);
345 struct counters_subgroup
*subgroup
= group
->subgroups
[subgroup_id
];
346 if (subgroup
== NULL
) {
349 "%s: Cannot increment counter: counters_subgroup in counters_group is NULL.",
354 return increment_subgroup_counter(subgroup
, counter_id
);
357 bool increment_subgroup_counter(struct counters_subgroup
*subgroup
,
360 if (subgroup
== NULL
) {
363 "%s: Cannot increment counter: counters_subgroup is NULL.",
368 if (counter_id
>= subgroup
->max_counters
) {
371 "%s: Cannot increment counter: counter_id [%d] is larger than the subgroup max_counters [%d].",
372 __func__
, counter_id
, subgroup
->max_counters
);
376 if (subgroup
->counters
[counter_id
] == NULL
) {
379 "%s: Cannot increment counter: No counter exists for counter_id [%d].",
380 __func__
, counter_id
);
384 subgroup
->counters
[counter_id
]->counter_value
++;
389 bool dump_counters_group_to_log(struct counters_group
*group
)
394 "%s: Cannot dump group counters to log: counters_group is NULL.",
399 time_t now
= time(NULL
);
402 "%s: PCEP Counters group:\n %s \n Sub-Groups [%d] \n Active for [%d seconds]",
403 __func__
, group
->counters_group_name
, group
->num_subgroups
,
404 (now
- group
->start_time
));
407 for (; i
<= group
->max_subgroups
; i
++) {
408 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
409 if (subgroup
!= NULL
) {
410 dump_counters_subgroup_to_log(subgroup
);
417 bool dump_counters_subgroup_to_log(struct counters_subgroup
*subgroup
)
419 if (subgroup
== NULL
) {
422 "%s: Cannot dump subgroup counters to log: counters_subgroup is NULL.",
428 "%s: \tPCEP Counters sub-group [%s] with [%d] counters",
429 __func__
, subgroup
->counters_subgroup_name
,
430 subgroup
->num_counters
);
433 for (; i
<= subgroup
->max_counters
; i
++) {
434 struct counter
*counter
= subgroup
->counters
[i
];
435 if (counter
!= NULL
) {
436 pcep_log(LOG_INFO
, "%s: \t\t%s %d", __func__
,
437 counter
->counter_name
, counter
->counter_value
);
444 struct counters_subgroup
*find_subgroup(const struct counters_group
*group
,
445 uint16_t subgroup_id
)
448 for (; i
<= group
->max_subgroups
; i
++) {
449 struct counters_subgroup
*subgroup
= group
->subgroups
[i
];
450 if (subgroup
!= NULL
) {
451 if (subgroup
->subgroup_id
== subgroup_id
) {
460 uint32_t subgroup_counters_total(struct counters_subgroup
*subgroup
)
462 if (subgroup
== NULL
) {
465 uint32_t counter_total
= 0;
467 for (; i
<= subgroup
->max_counters
; i
++) {
468 struct counter
*counter
= subgroup
->counters
[i
];
469 if (counter
!= NULL
) {
470 counter_total
+= counter
->counter_value
;
474 return counter_total
;