]>
Commit | Line | Data |
---|---|---|
74971473 JG |
1 | /* |
2 | * This file is part of the PCEPlib, a PCEP protocol library. | |
3 | * | |
4 | * Copyright (C) 2020 Volta Networks https://voltanet.io/ | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
18 | * | |
19 | * Author : Brady Johnson <brady@voltanet.io> | |
20 | * | |
21 | */ | |
22 | ||
23 | ||
24 | /* | |
25 | * Definitions of PCEP Counters. | |
26 | */ | |
27 | ||
28 | #ifndef PCEP_UTILS_INCLUDE_PCEP_UTILS_COUNTERS_H_ | |
29 | #define PCEP_UTILS_INCLUDE_PCEP_UTILS_COUNTERS_H_ | |
30 | ||
31 | #include <stdbool.h> | |
32 | #include <stdint.h> | |
33 | ||
34 | #ifdef __cplusplus | |
35 | extern "C" { | |
36 | #endif | |
37 | ||
38 | /* | |
39 | * Example Counter group with sub-groups and counters | |
40 | * | |
41 | * pcep_counters { | |
42 | * counters_group_rx { | |
43 | * message_open; | |
44 | * message_keepalive; | |
45 | * message_pcreq; | |
46 | * } | |
47 | * counters_group_tx { | |
48 | * message_open; | |
49 | * message_keepalive; | |
50 | * message_pcreq; | |
51 | * } | |
52 | * counters_group_events { | |
53 | * pcc_connect; | |
54 | * pce_connect; | |
55 | * pcc_disconnect; | |
56 | * pce_disconnect; | |
57 | * } | |
58 | * } | |
59 | * | |
60 | * To create the above structure of groups, sub-groups, and counters, do the | |
61 | * following: | |
62 | * | |
63 | * struct counters_subgroup *rx_subgroup = create_counters_subgroup("rx | |
64 | * counters", 3); struct counters_subgroup *tx_subgroup = | |
65 | * create_counters_subgroup("tx counters", 3); struct counters_subgroup | |
66 | * *events_subgroup = create_counters_subgroup("events counters", 4); | |
67 | * | |
68 | * Use message_id: PCEP_TYPE_OPEN=1 | |
69 | * create_subgroup_counter(rx_subgroup, 1, "Message Open"); | |
70 | * create_subgroup_counter(rx_subgroup, 2, "Message KeepAlive"); | |
71 | * create_subgroup_counter(rx_subgroup, 3, "Message PcReq"); | |
72 | * | |
73 | * create_subgroup_counter(tx_subgroup, 1, "Message Open"); | |
74 | * create_subgroup_counter(tx_subgroup, 2, "Message KeepAlive"); | |
75 | * create_subgroup_counter(tx_subgroup, 3, "Message PcReq"); | |
76 | * | |
77 | * create_subgroup_counter(events_subgroup, 1, "PCC Connect"); | |
78 | * create_subgroup_counter(events_subgroup, 2, "PCE Connect"); | |
79 | * create_subgroup_counter(events_subgroup, 3, "PCC Disconnect"); | |
80 | * create_subgroup_counter(events_subgroup, 4, "PCE Disconnect"); | |
81 | * | |
82 | * struct counters_group *cntrs_group = create_counters_group("PCEP Counters", | |
83 | * 3); add_counters_subgroup(cntrs_group, rx_subgroup); | |
84 | * add_counters_subgroup(cntrs_group, tx_subgroup); | |
85 | * add_counters_subgroup(cntrs_group, events_subgroup); | |
86 | */ | |
87 | ||
88 | #define MAX_COUNTER_STR_LENGTH 128 | |
89 | #define MAX_COUNTER_GROUPS 500 | |
90 | #define MAX_COUNTERS 500 | |
91 | ||
92 | struct counter { | |
93 | uint16_t counter_id; | |
94 | char counter_name[MAX_COUNTER_STR_LENGTH]; | |
95 | uint32_t counter_value; | |
96 | }; | |
97 | ||
98 | struct counters_subgroup { | |
99 | char counters_subgroup_name[MAX_COUNTER_STR_LENGTH]; | |
100 | uint16_t subgroup_id; | |
101 | uint16_t num_counters; | |
102 | uint16_t max_counters; | |
103 | /* Array of (struct counter *) allocated when the subgroup is created. | |
104 | * The array is indexed by the struct counter->counter_id */ | |
105 | struct counter **counters; | |
106 | }; | |
107 | ||
108 | struct counters_group { | |
109 | char counters_group_name[MAX_COUNTER_STR_LENGTH]; | |
110 | uint16_t num_subgroups; | |
111 | uint16_t max_subgroups; | |
112 | time_t start_time; | |
113 | /* Array of (struct counters_subgroup *) allocated when the group is | |
114 | * created. The subgroup is indexed by the (struct counters_subgroup | |
115 | * *)->subgroup_id */ | |
116 | struct counters_subgroup **subgroups; | |
117 | }; | |
118 | ||
119 | /* | |
120 | * Create a counters group with the given group_name and number of subgroups. | |
121 | * Subgroup_ids are 0-based, so take that into account when setting | |
122 | * max_subgroups. Return true on success or false if group_name is NULL or | |
123 | * max_subgroups >= MAX_COUNTER_GROUPS. | |
124 | */ | |
125 | struct counters_group *create_counters_group(const char *group_name, | |
126 | uint16_t max_subgroups); | |
127 | ||
128 | /* | |
129 | * Create a counters subgroup with the given subgroup_name, subgroup_id and | |
130 | * number of counters. The subgroup_id is 0-based. counter_ids are 0-based, so | |
131 | * take that into account when setting max_counters. Return true on success or | |
132 | * false if subgroup_name is NULL, subgroup_id >= MAX_COUNTER_GROUPS, or | |
133 | * max_counters >= MAX_COUNTERS. | |
134 | */ | |
135 | struct counters_subgroup *create_counters_subgroup(const char *subgroup_name, | |
136 | uint16_t subgroup_id, | |
137 | uint16_t max_counters); | |
138 | ||
139 | /* | |
140 | * Add a counter_subgroup to a counter_group. | |
141 | * Return true on success or false if group is NULL or if subgroup is NULL. | |
142 | */ | |
143 | bool add_counters_subgroup(struct counters_group *group, | |
144 | struct counters_subgroup *subgroup); | |
145 | ||
146 | /* | |
147 | * Clone a subgroup and set a new name and subgroup_id for the new subgroup. | |
148 | * This is useful for RX and TX counters: just create the RX counters and clone | |
149 | * it for the TX counters. | |
150 | */ | |
151 | struct counters_subgroup * | |
152 | clone_counters_subgroup(struct counters_subgroup *subgroup, | |
153 | const char *subgroup_name, uint16_t subgroup_id); | |
154 | ||
155 | /* | |
156 | * Create a counter in a subgroup with the given counter_id and counter_name. | |
157 | * The counter_id is 0-based. | |
158 | * Return true on success or false if subgroup is NULL, counter_id >= | |
159 | * MAX_COUNTERS, or if counter_name is NULL. | |
160 | */ | |
161 | bool create_subgroup_counter(struct counters_subgroup *subgroup, | |
162 | uint32_t counter_id, const char *counter_name); | |
163 | ||
164 | /* | |
165 | * Delete the counters_group and recursively delete all subgroups and their | |
166 | * counters. Return true on success or false if group is NULL. | |
167 | */ | |
168 | bool delete_counters_group(struct counters_group *group); | |
169 | ||
170 | /* | |
171 | * Delete the counters_subgroup and all its counters counters. | |
172 | * Return true on success or false if subgroup is NULL. | |
173 | */ | |
174 | bool delete_counters_subgroup(struct counters_subgroup *subgroup); | |
175 | ||
176 | /* | |
177 | * Reset all the counters in all sub-groups contained in this group. | |
178 | * Return true on success or false if group is NULL. | |
179 | */ | |
180 | bool reset_group_counters(struct counters_group *group); | |
181 | ||
182 | /* | |
183 | * Reset all the counters in this subgroup. | |
184 | * Return true on success or false if subgroup is NULL. | |
185 | */ | |
186 | bool reset_subgroup_counters(struct counters_subgroup *subgroup); | |
187 | ||
188 | /* | |
189 | * Increment a counter given a counter_group, subgroup_id, and counter_id. | |
190 | * Return true on success or false if group is NULL, subgroup_id >= | |
191 | * MAX_COUNTER_GROUPS, or counter_id >= MAX_COUNTERS. | |
192 | */ | |
193 | bool increment_counter(struct counters_group *group, uint16_t subgroup_id, | |
194 | uint16_t counter_id); | |
195 | ||
196 | /* | |
197 | * Increment a counter given the counter_subgroup and counter_id. | |
198 | * Return true on success or false if subgroup is NULL or counter_id >= | |
199 | * MAX_COUNTERS. | |
200 | */ | |
201 | bool increment_subgroup_counter(struct counters_subgroup *subgroup, | |
202 | uint16_t counter_id); | |
203 | ||
204 | /* | |
205 | * Dump the counter_group info and all its counter_subgroups. | |
206 | * Return true on success or false if group is NULL. | |
207 | */ | |
208 | bool dump_counters_group_to_log(struct counters_group *group); | |
209 | ||
210 | /* | |
211 | * Dump all the counters in a counter_subgroup. | |
212 | * Return true on success or false if subgroup is NULL. | |
213 | */ | |
214 | bool dump_counters_subgroup_to_log(struct counters_subgroup *subgroup); | |
215 | ||
216 | /* | |
217 | * Search for a counters_subgroup by subgroup_id in a counters_group | |
218 | * and return it, if found, else return NULL. | |
219 | */ | |
220 | struct counters_subgroup *find_subgroup(const struct counters_group *group, | |
221 | uint16_t subgroup_id); | |
222 | ||
223 | /* | |
224 | * Given a counters_subgroup, return the sum of all the counters. | |
225 | */ | |
226 | uint32_t subgroup_counters_total(struct counters_subgroup *subgroup); | |
227 | ||
228 | #ifdef __cplusplus | |
229 | } | |
230 | #endif | |
231 | ||
232 | #endif /* PCEP_UTILS_INCLUDE_PCEP_UTILS_COUNTERS_H_ */ |