Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * Logging of zebra |
4 | | * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro |
5 | | */ |
6 | | |
7 | | #define FRR_DEFINE_DESC_TABLE |
8 | | |
9 | | #include <zebra.h> |
10 | | |
11 | | #include "zclient.h" |
12 | | #include "log.h" |
13 | | #include "memory.h" |
14 | | #include "command.h" |
15 | | #include "lib_errors.h" |
16 | | #include "lib/hook.h" |
17 | | #include "printfrr.h" |
18 | | #include "frr_pthread.h" |
19 | | |
20 | | #ifdef HAVE_LIBUNWIND |
21 | | #define UNW_LOCAL_ONLY |
22 | | #include <libunwind.h> |
23 | | #include <dlfcn.h> |
24 | | #endif |
25 | | |
26 | | /** |
27 | | * Looks up a message in a message list by key. |
28 | | * |
29 | | * If the message is not found, returns the provided error message. |
30 | | * |
31 | | * Terminates when it hits a struct message that's all zeros. |
32 | | * |
33 | | * @param mz the message list |
34 | | * @param kz the message key |
35 | | * @param nf the message to return if not found |
36 | | * @return the message |
37 | | */ |
38 | | const char *lookup_msg(const struct message *mz, int kz, const char *nf) |
39 | 21.9k | { |
40 | 21.9k | static struct message nt = {0}; |
41 | 21.9k | const char *rz = nf ? nf : "(no message found)"; |
42 | 21.9k | const struct message *pnt; |
43 | 91.9k | for (pnt = mz; memcmp(pnt, &nt, sizeof(struct message)); pnt++) |
44 | 70.7k | if (pnt->key == kz) { |
45 | 710 | rz = pnt->str ? pnt->str : rz; |
46 | 710 | break; |
47 | 710 | } |
48 | 21.9k | return rz; |
49 | 21.9k | } |
50 | | |
51 | | /* For time string format. */ |
52 | | size_t frr_timestamp(int timestamp_precision, char *buf, size_t buflen) |
53 | 0 | { |
54 | 0 | static struct { |
55 | 0 | time_t last; |
56 | 0 | size_t len; |
57 | 0 | char buf[28]; |
58 | 0 | } cache; |
59 | 0 | struct timeval clock; |
60 | |
|
61 | 0 | gettimeofday(&clock, NULL); |
62 | | |
63 | | /* first, we update the cache if the time has changed */ |
64 | 0 | if (cache.last != clock.tv_sec) { |
65 | 0 | struct tm tm; |
66 | 0 | cache.last = clock.tv_sec; |
67 | 0 | localtime_r(&cache.last, &tm); |
68 | 0 | cache.len = strftime(cache.buf, sizeof(cache.buf), |
69 | 0 | "%Y/%m/%d %H:%M:%S", &tm); |
70 | 0 | } |
71 | | /* note: it's not worth caching the subsecond part, because |
72 | | chances are that back-to-back calls are not sufficiently close |
73 | | together |
74 | | for the clock not to have ticked forward */ |
75 | |
|
76 | 0 | if (buflen > cache.len) { |
77 | 0 | memcpy(buf, cache.buf, cache.len); |
78 | 0 | if ((timestamp_precision > 0) |
79 | 0 | && (buflen > cache.len + 1 + timestamp_precision)) { |
80 | | /* should we worry about locale issues? */ |
81 | 0 | static const int divisor[] = {0, 100000, 10000, 1000, |
82 | 0 | 100, 10, 1}; |
83 | 0 | int prec; |
84 | 0 | char *p = buf + cache.len + 1 |
85 | 0 | + (prec = timestamp_precision); |
86 | 0 | *p-- = '\0'; |
87 | 0 | while (prec > 6) |
88 | | /* this is unlikely to happen, but protect anyway */ |
89 | 0 | { |
90 | 0 | *p-- = '0'; |
91 | 0 | prec--; |
92 | 0 | } |
93 | 0 | clock.tv_usec /= divisor[prec]; |
94 | 0 | do { |
95 | 0 | *p-- = '0' + (clock.tv_usec % 10); |
96 | 0 | clock.tv_usec /= 10; |
97 | 0 | } while (--prec > 0); |
98 | 0 | *p = '.'; |
99 | 0 | return cache.len + 1 + timestamp_precision; |
100 | 0 | } |
101 | 0 | buf[cache.len] = '\0'; |
102 | 0 | return cache.len; |
103 | 0 | } |
104 | 0 | if (buflen > 0) |
105 | 0 | buf[0] = '\0'; |
106 | 0 | return 0; |
107 | 0 | } |
108 | | |
109 | | /* |
110 | | * crash handling |
111 | | * |
112 | | * NB: only AS-Safe (async-signal) functions can be used here! |
113 | | */ |
114 | | |
115 | | /* Note: the goal here is to use only async-signal-safe functions. */ |
116 | | void zlog_signal(int signo, const char *action, void *siginfo_v, |
117 | | void *program_counter) |
118 | 0 | { |
119 | 0 | siginfo_t *siginfo = siginfo_v; |
120 | 0 | time_t now; |
121 | 0 | char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...") |
122 | 0 | + 100]; |
123 | 0 | struct fbuf fb = { .buf = buf, .pos = buf, .len = sizeof(buf) }; |
124 | |
|
125 | 0 | time(&now); |
126 | |
|
127 | 0 | bprintfrr(&fb, "Received signal %d at %lld", signo, (long long)now); |
128 | 0 | if (program_counter) |
129 | 0 | bprintfrr(&fb, " (si_addr 0x%tx, PC 0x%tx)", |
130 | 0 | (ptrdiff_t)siginfo->si_addr, |
131 | 0 | (ptrdiff_t)program_counter); |
132 | 0 | else |
133 | 0 | bprintfrr(&fb, " (si_addr 0x%tx)", |
134 | 0 | (ptrdiff_t)siginfo->si_addr); |
135 | 0 | bprintfrr(&fb, "; %s\n", action); |
136 | |
|
137 | 0 | zlog_sigsafe(fb.buf, fb.pos - fb.buf); |
138 | |
|
139 | 0 | zlog_backtrace_sigsafe(LOG_CRIT, program_counter); |
140 | |
|
141 | 0 | fb.pos = buf; |
142 | |
|
143 | 0 | struct event *tc; |
144 | 0 | tc = pthread_getspecific(thread_current); |
145 | |
|
146 | 0 | if (!tc) |
147 | 0 | bprintfrr(&fb, "no thread information available\n"); |
148 | 0 | else |
149 | 0 | bprintfrr(&fb, "in thread %s scheduled from %s:%d %s()\n", |
150 | 0 | tc->xref->funcname, tc->xref->xref.file, |
151 | 0 | tc->xref->xref.line, tc->xref->xref.func); |
152 | |
|
153 | 0 | zlog_sigsafe(fb.buf, fb.pos - fb.buf); |
154 | 0 | } |
155 | | |
156 | | /* Log a backtrace using only async-signal-safe functions. |
157 | | Needs to be enhanced to support syslog logging. */ |
158 | | void zlog_backtrace_sigsafe(int priority, void *program_counter) |
159 | 0 | { |
160 | | #ifdef HAVE_LIBUNWIND |
161 | | char buf[256]; |
162 | | struct fbuf fb = { .buf = buf, .len = sizeof(buf) }; |
163 | | unw_cursor_t cursor; |
164 | | unw_context_t uc; |
165 | | unw_word_t ip, off, sp; |
166 | | Dl_info dlinfo; |
167 | | |
168 | | memset(&uc, 0, sizeof(uc)); |
169 | | memset(&cursor, 0, sizeof(cursor)); |
170 | | |
171 | | unw_getcontext(&uc); |
172 | | unw_init_local(&cursor, &uc); |
173 | | while (unw_step(&cursor) > 0) { |
174 | | char name[128] = "?"; |
175 | | |
176 | | unw_get_reg(&cursor, UNW_REG_IP, &ip); |
177 | | unw_get_reg(&cursor, UNW_REG_SP, &sp); |
178 | | |
179 | | if (!unw_get_proc_name(&cursor, buf, sizeof(buf), &off)) |
180 | | snprintfrr(name, sizeof(name), "%s+%#lx", |
181 | | buf, (long)off); |
182 | | |
183 | | fb.pos = buf; |
184 | | if (unw_is_signal_frame(&cursor)) |
185 | | bprintfrr(&fb, " ---- signal ----\n"); |
186 | | bprintfrr(&fb, "%-30s %16lx %16lx", name, (long)ip, (long)sp); |
187 | | if (dladdr((void *)ip, &dlinfo)) |
188 | | bprintfrr(&fb, " %s (mapped at %p)", |
189 | | dlinfo.dli_fname, dlinfo.dli_fbase); |
190 | | bprintfrr(&fb, "\n"); |
191 | | zlog_sigsafe(fb.buf, fb.pos - fb.buf); |
192 | | } |
193 | | #elif defined(HAVE_GLIBC_BACKTRACE) |
194 | | void *array[64]; |
195 | 0 | int size, i; |
196 | 0 | char buf[128]; |
197 | 0 | struct fbuf fb = { .buf = buf, .pos = buf, .len = sizeof(buf) }; |
198 | 0 | char **bt = NULL; |
199 | |
|
200 | 0 | size = backtrace(array, array_size(array)); |
201 | 0 | if (size <= 0 || (size_t)size > array_size(array)) |
202 | 0 | return; |
203 | | |
204 | 0 | bprintfrr(&fb, "Backtrace for %d stack frames:", size); |
205 | 0 | zlog_sigsafe(fb.pos, fb.buf - fb.pos); |
206 | |
|
207 | 0 | bt = backtrace_symbols(array, size); |
208 | |
|
209 | 0 | for (i = 0; i < size; i++) { |
210 | 0 | fb.pos = buf; |
211 | 0 | if (bt) |
212 | 0 | bprintfrr(&fb, "%s", bt[i]); |
213 | 0 | else |
214 | 0 | bprintfrr(&fb, "[bt %d] 0x%tx", i, |
215 | 0 | (ptrdiff_t)(array[i])); |
216 | 0 | zlog_sigsafe(fb.buf, fb.pos - fb.buf); |
217 | 0 | } |
218 | 0 | if (bt) |
219 | 0 | free(bt); |
220 | 0 | #endif /* HAVE_STRACK_TRACE */ |
221 | 0 | } |
222 | | |
223 | | void zlog_backtrace(int priority) |
224 | 150 | { |
225 | | #ifdef HAVE_LIBUNWIND |
226 | | char buf[100]; |
227 | | unw_cursor_t cursor = {}; |
228 | | unw_context_t uc; |
229 | | unw_word_t ip, off, sp; |
230 | | Dl_info dlinfo; |
231 | | |
232 | | unw_getcontext(&uc); |
233 | | unw_init_local(&cursor, &uc); |
234 | | zlog(priority, "Backtrace:"); |
235 | | while (unw_step(&cursor) > 0) { |
236 | | char name[128] = "?"; |
237 | | |
238 | | unw_get_reg(&cursor, UNW_REG_IP, &ip); |
239 | | unw_get_reg(&cursor, UNW_REG_SP, &sp); |
240 | | |
241 | | if (unw_is_signal_frame(&cursor)) |
242 | | zlog(priority, " ---- signal ----"); |
243 | | |
244 | | if (!unw_get_proc_name(&cursor, buf, sizeof(buf), &off)) |
245 | | snprintf(name, sizeof(name), "%s+%#lx", |
246 | | buf, (long)off); |
247 | | |
248 | | if (dladdr((void *)ip, &dlinfo)) |
249 | | zlog(priority, "%-30s %16lx %16lx %s (mapped at %p)", |
250 | | name, (long)ip, (long)sp, |
251 | | dlinfo.dli_fname, dlinfo.dli_fbase); |
252 | | else |
253 | | zlog(priority, "%-30s %16lx %16lx", |
254 | | name, (long)ip, (long)sp); |
255 | | } |
256 | | #elif defined(HAVE_GLIBC_BACKTRACE) |
257 | | void *array[20]; |
258 | 150 | int size, i; |
259 | 150 | char **strings; |
260 | | |
261 | 150 | size = backtrace(array, array_size(array)); |
262 | 150 | if (size <= 0 || (size_t)size > array_size(array)) { |
263 | 0 | flog_err_sys( |
264 | 0 | EC_LIB_SYSTEM_CALL, |
265 | 0 | "Cannot get backtrace, returned invalid # of frames %d (valid range is between 1 and %lu)", |
266 | 0 | size, (unsigned long)(array_size(array))); |
267 | 0 | return; |
268 | 0 | } |
269 | 150 | zlog(priority, "Backtrace for %d stack frames:", size); |
270 | 150 | if (!(strings = backtrace_symbols(array, size))) { |
271 | 0 | flog_err_sys(EC_LIB_SYSTEM_CALL, |
272 | 0 | "Cannot get backtrace symbols (out of memory?)"); |
273 | 0 | for (i = 0; i < size; i++) |
274 | 0 | zlog(priority, "[bt %d] %p", i, array[i]); |
275 | 150 | } else { |
276 | 1.81k | for (i = 0; i < size; i++) |
277 | 1.66k | zlog(priority, "[bt %d] %s", i, strings[i]); |
278 | 150 | free(strings); |
279 | 150 | } |
280 | | #else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */ |
281 | | zlog(priority, "No backtrace available on this platform."); |
282 | | #endif |
283 | 150 | } |
284 | | |
285 | | void zlog_thread_info(int log_level) |
286 | 0 | { |
287 | 0 | struct event *tc; |
288 | 0 | tc = pthread_getspecific(thread_current); |
289 | |
|
290 | 0 | if (tc) |
291 | 0 | zlog(log_level, |
292 | 0 | "Current thread function %s, scheduled from file %s, line %u in %s()", |
293 | 0 | tc->xref->funcname, tc->xref->xref.file, |
294 | 0 | tc->xref->xref.line, tc->xref->xref.func); |
295 | 0 | else |
296 | 0 | zlog(log_level, "Current thread not known/applicable"); |
297 | 0 | } |
298 | | |
299 | | void memory_oom(size_t size, const char *name) |
300 | 0 | { |
301 | 0 | zlog(LOG_CRIT, |
302 | 0 | "out of memory: failed to allocate %zu bytes for %s object", |
303 | 0 | size, name); |
304 | 0 | zlog_backtrace(LOG_CRIT); |
305 | 0 | log_memstats(stderr, "log"); |
306 | 0 | abort(); |
307 | 0 | } |
308 | | |
309 | | /* Wrapper around strerror to handle case where it returns NULL. */ |
310 | | const char *safe_strerror(int errnum) |
311 | 0 | { |
312 | 0 | const char *s = strerror(errnum); |
313 | 0 | return (s != NULL) ? s : "Unknown error"; |
314 | 0 | } |
315 | | |
316 | | #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' } |
317 | | static const struct zebra_desc_table command_types[] = { |
318 | | DESC_ENTRY(ZEBRA_INTERFACE_ADD), |
319 | | DESC_ENTRY(ZEBRA_INTERFACE_DELETE), |
320 | | DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD), |
321 | | DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE), |
322 | | DESC_ENTRY(ZEBRA_INTERFACE_UP), |
323 | | DESC_ENTRY(ZEBRA_INTERFACE_DOWN), |
324 | | DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER), |
325 | | DESC_ENTRY(ZEBRA_INTERFACE_SET_PROTODOWN), |
326 | | DESC_ENTRY(ZEBRA_ROUTE_ADD), |
327 | | DESC_ENTRY(ZEBRA_ROUTE_DELETE), |
328 | | DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER), |
329 | | DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD), |
330 | | DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE), |
331 | | DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD), |
332 | | DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE), |
333 | | DESC_ENTRY(ZEBRA_ROUTER_ID_ADD), |
334 | | DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE), |
335 | | DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE), |
336 | | DESC_ENTRY(ZEBRA_HELLO), |
337 | | DESC_ENTRY(ZEBRA_CAPABILITIES), |
338 | | DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER), |
339 | | DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER), |
340 | | DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE), |
341 | | DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD), |
342 | | DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE), |
343 | | DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE), |
344 | | DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER), |
345 | | DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER), |
346 | | DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE), |
347 | | DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY), |
348 | | DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD), |
349 | | DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL), |
350 | | DESC_ENTRY(ZEBRA_VRF_UNREGISTER), |
351 | | DESC_ENTRY(ZEBRA_VRF_ADD), |
352 | | DESC_ENTRY(ZEBRA_VRF_DELETE), |
353 | | DESC_ENTRY(ZEBRA_VRF_LABEL), |
354 | | DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE), |
355 | | DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER), |
356 | | DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER), |
357 | | DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV), |
358 | | DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV), |
359 | | DESC_ENTRY(ZEBRA_NEXTHOP_LOOKUP_MRIB), |
360 | | DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS), |
361 | | DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD), |
362 | | DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE), |
363 | | DESC_ENTRY(ZEBRA_MPLS_LABELS_REPLACE), |
364 | | DESC_ENTRY(ZEBRA_SR_POLICY_SET), |
365 | | DESC_ENTRY(ZEBRA_SR_POLICY_DELETE), |
366 | | DESC_ENTRY(ZEBRA_SR_POLICY_NOTIFY_STATUS), |
367 | | DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS), |
368 | | DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT), |
369 | | DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC), |
370 | | DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK), |
371 | | DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK), |
372 | | DESC_ENTRY(ZEBRA_FEC_REGISTER), |
373 | | DESC_ENTRY(ZEBRA_FEC_UNREGISTER), |
374 | | DESC_ENTRY(ZEBRA_FEC_UPDATE), |
375 | | DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW), |
376 | | DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP), |
377 | | DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET), |
378 | | DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI), |
379 | | DESC_ENTRY(ZEBRA_LOCAL_ES_ADD), |
380 | | DESC_ENTRY(ZEBRA_LOCAL_ES_DEL), |
381 | | DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_ADD), |
382 | | DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_DEL), |
383 | | DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_ADD), |
384 | | DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_DEL), |
385 | | DESC_ENTRY(ZEBRA_VNI_ADD), |
386 | | DESC_ENTRY(ZEBRA_VNI_DEL), |
387 | | DESC_ENTRY(ZEBRA_L3VNI_ADD), |
388 | | DESC_ENTRY(ZEBRA_L3VNI_DEL), |
389 | | DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD), |
390 | | DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL), |
391 | | DESC_ENTRY(ZEBRA_MACIP_ADD), |
392 | | DESC_ENTRY(ZEBRA_MACIP_DEL), |
393 | | DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD), |
394 | | DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL), |
395 | | DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD), |
396 | | DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL), |
397 | | DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION), |
398 | | DESC_ENTRY(ZEBRA_PW_ADD), |
399 | | DESC_ENTRY(ZEBRA_PW_DELETE), |
400 | | DESC_ENTRY(ZEBRA_PW_SET), |
401 | | DESC_ENTRY(ZEBRA_PW_UNSET), |
402 | | DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE), |
403 | | DESC_ENTRY(ZEBRA_RULE_ADD), |
404 | | DESC_ENTRY(ZEBRA_RULE_DELETE), |
405 | | DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER), |
406 | | DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT), |
407 | | DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK), |
408 | | DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK), |
409 | | DESC_ENTRY(ZEBRA_IPSET_CREATE), |
410 | | DESC_ENTRY(ZEBRA_IPSET_DESTROY), |
411 | | DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD), |
412 | | DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE), |
413 | | DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER), |
414 | | DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER), |
415 | | DESC_ENTRY(ZEBRA_IPTABLE_ADD), |
416 | | DESC_ENTRY(ZEBRA_IPTABLE_DELETE), |
417 | | DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER), |
418 | | DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL), |
419 | | DESC_ENTRY(ZEBRA_VXLAN_SG_ADD), |
420 | | DESC_ENTRY(ZEBRA_VXLAN_SG_DEL), |
421 | | DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY), |
422 | | DESC_ENTRY(ZEBRA_MLAG_PROCESS_UP), |
423 | | DESC_ENTRY(ZEBRA_MLAG_PROCESS_DOWN), |
424 | | DESC_ENTRY(ZEBRA_MLAG_CLIENT_REGISTER), |
425 | | DESC_ENTRY(ZEBRA_MLAG_CLIENT_UNREGISTER), |
426 | | DESC_ENTRY(ZEBRA_MLAG_FORWARD_MSG), |
427 | | DESC_ENTRY(ZEBRA_NHG_ADD), |
428 | | DESC_ENTRY(ZEBRA_NHG_DEL), |
429 | | DESC_ENTRY(ZEBRA_NHG_NOTIFY_OWNER), |
430 | | DESC_ENTRY(ZEBRA_EVPN_REMOTE_NH_ADD), |
431 | | DESC_ENTRY(ZEBRA_EVPN_REMOTE_NH_DEL), |
432 | | DESC_ENTRY(ZEBRA_SRV6_LOCATOR_ADD), |
433 | | DESC_ENTRY(ZEBRA_SRV6_LOCATOR_DELETE), |
434 | | DESC_ENTRY(ZEBRA_SRV6_MANAGER_GET_LOCATOR_CHUNK), |
435 | | DESC_ENTRY(ZEBRA_SRV6_MANAGER_RELEASE_LOCATOR_CHUNK), |
436 | | DESC_ENTRY(ZEBRA_ERROR), |
437 | | DESC_ENTRY(ZEBRA_CLIENT_CAPABILITIES), |
438 | | DESC_ENTRY(ZEBRA_OPAQUE_MESSAGE), |
439 | | DESC_ENTRY(ZEBRA_OPAQUE_REGISTER), |
440 | | DESC_ENTRY(ZEBRA_OPAQUE_UNREGISTER), |
441 | | DESC_ENTRY(ZEBRA_NEIGH_DISCOVER), |
442 | | DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_REQUEST), |
443 | | DESC_ENTRY(ZEBRA_CLIENT_CLOSE_NOTIFY), |
444 | | DESC_ENTRY(ZEBRA_NHRP_NEIGH_ADDED), |
445 | | DESC_ENTRY(ZEBRA_NHRP_NEIGH_REMOVED), |
446 | | DESC_ENTRY(ZEBRA_NHRP_NEIGH_GET), |
447 | | DESC_ENTRY(ZEBRA_NHRP_NEIGH_REGISTER), |
448 | | DESC_ENTRY(ZEBRA_NHRP_NEIGH_UNREGISTER), |
449 | | DESC_ENTRY(ZEBRA_NEIGH_IP_ADD), |
450 | | DESC_ENTRY(ZEBRA_NEIGH_IP_DEL), |
451 | | DESC_ENTRY(ZEBRA_CONFIGURE_ARP), |
452 | | DESC_ENTRY(ZEBRA_GRE_GET), |
453 | | DESC_ENTRY(ZEBRA_GRE_UPDATE), |
454 | | DESC_ENTRY(ZEBRA_GRE_SOURCE_SET), |
455 | | DESC_ENTRY(ZEBRA_TC_QDISC_INSTALL), |
456 | | DESC_ENTRY(ZEBRA_TC_QDISC_UNINSTALL), |
457 | | DESC_ENTRY(ZEBRA_TC_CLASS_ADD), |
458 | | DESC_ENTRY(ZEBRA_TC_CLASS_DELETE), |
459 | | DESC_ENTRY(ZEBRA_TC_FILTER_ADD), |
460 | | DESC_ENTRY(ZEBRA_TC_FILTER_DELETE)}; |
461 | | #undef DESC_ENTRY |
462 | | |
463 | | static const struct zebra_desc_table unknown = {0, "unknown", '?'}; |
464 | | |
465 | | static const struct zebra_desc_table *zroute_lookup(unsigned int zroute) |
466 | 0 | { |
467 | 0 | unsigned int i; |
468 | |
|
469 | 0 | if (zroute >= array_size(route_types)) { |
470 | 0 | flog_err(EC_LIB_DEVELOPMENT, "unknown zebra route type: %u", |
471 | 0 | zroute); |
472 | 0 | return &unknown; |
473 | 0 | } |
474 | 0 | if (zroute == route_types[zroute].type) |
475 | 0 | return &route_types[zroute]; |
476 | 0 | for (i = 0; i < array_size(route_types); i++) { |
477 | 0 | if (zroute == route_types[i].type) { |
478 | 0 | zlog_warn( |
479 | 0 | "internal error: route type table out of order while searching for %u, please notify developers", |
480 | 0 | zroute); |
481 | 0 | return &route_types[i]; |
482 | 0 | } |
483 | 0 | } |
484 | 0 | flog_err(EC_LIB_DEVELOPMENT, |
485 | 0 | "internal error: cannot find route type %u in table!", zroute); |
486 | 0 | return &unknown; |
487 | 0 | } |
488 | | |
489 | | const char *zebra_route_string(unsigned int zroute) |
490 | 0 | { |
491 | 0 | return zroute_lookup(zroute)->string; |
492 | 0 | } |
493 | | |
494 | | char zebra_route_char(unsigned int zroute) |
495 | 0 | { |
496 | 0 | return zroute_lookup(zroute)->chr; |
497 | 0 | } |
498 | | |
499 | | const char *zserv_command_string(unsigned int command) |
500 | 0 | { |
501 | 0 | if (command >= array_size(command_types)) { |
502 | 0 | flog_err(EC_LIB_DEVELOPMENT, "unknown zserv command type: %u", |
503 | 0 | command); |
504 | 0 | return unknown.string; |
505 | 0 | } |
506 | 0 | return command_types[command].string; |
507 | 0 | } |
508 | | |
509 | | #define DESC_ENTRY(T) [(T)] = {(T), (#T), '\0'} |
510 | | static const struct zebra_desc_table gr_client_cap_types[] = { |
511 | | DESC_ENTRY(ZEBRA_CLIENT_GR_CAPABILITIES), |
512 | | DESC_ENTRY(ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE), |
513 | | DESC_ENTRY(ZEBRA_CLIENT_ROUTE_UPDATE_PENDING), |
514 | | DESC_ENTRY(ZEBRA_CLIENT_GR_DISABLE), |
515 | | DESC_ENTRY(ZEBRA_CLIENT_RIB_STALE_TIME), |
516 | | }; |
517 | | #undef DESC_ENTRY |
518 | | |
519 | | const char *zserv_gr_client_cap_string(uint32_t zcc) |
520 | 0 | { |
521 | 0 | if (zcc >= array_size(gr_client_cap_types)) { |
522 | 0 | flog_err(EC_LIB_DEVELOPMENT, "unknown zserv command type: %u", |
523 | 0 | zcc); |
524 | 0 | return unknown.string; |
525 | 0 | } |
526 | 0 | return gr_client_cap_types[zcc].string; |
527 | 0 | } |
528 | | |
529 | | int proto_name2num(const char *s) |
530 | 0 | { |
531 | 0 | unsigned i; |
532 | |
|
533 | 0 | for (i = 0; i < array_size(route_types); ++i) |
534 | 0 | if (strcasecmp(s, route_types[i].string) == 0) |
535 | 0 | return route_types[i].type; |
536 | 0 | return -1; |
537 | 0 | } |
538 | | |
539 | | int proto_redistnum(int afi, const char *s) |
540 | 0 | { |
541 | 0 | if (!s) |
542 | 0 | return -1; |
543 | | |
544 | 0 | if (afi == AFI_IP) { |
545 | 0 | if (strmatch(s, "kernel")) |
546 | 0 | return ZEBRA_ROUTE_KERNEL; |
547 | 0 | else if (strmatch(s, "connected")) |
548 | 0 | return ZEBRA_ROUTE_CONNECT; |
549 | 0 | else if (strmatch(s, "static")) |
550 | 0 | return ZEBRA_ROUTE_STATIC; |
551 | 0 | else if (strmatch(s, "rip")) |
552 | 0 | return ZEBRA_ROUTE_RIP; |
553 | 0 | else if (strmatch(s, "eigrp")) |
554 | 0 | return ZEBRA_ROUTE_EIGRP; |
555 | 0 | else if (strmatch(s, "ospf")) |
556 | 0 | return ZEBRA_ROUTE_OSPF; |
557 | 0 | else if (strmatch(s, "isis")) |
558 | 0 | return ZEBRA_ROUTE_ISIS; |
559 | 0 | else if (strmatch(s, "bgp")) |
560 | 0 | return ZEBRA_ROUTE_BGP; |
561 | 0 | else if (strmatch(s, "table")) |
562 | 0 | return ZEBRA_ROUTE_TABLE; |
563 | 0 | else if (strmatch(s, "vnc")) |
564 | 0 | return ZEBRA_ROUTE_VNC; |
565 | 0 | else if (strmatch(s, "vnc-direct")) |
566 | 0 | return ZEBRA_ROUTE_VNC_DIRECT; |
567 | 0 | else if (strmatch(s, "nhrp")) |
568 | 0 | return ZEBRA_ROUTE_NHRP; |
569 | 0 | else if (strmatch(s, "babel")) |
570 | 0 | return ZEBRA_ROUTE_BABEL; |
571 | 0 | else if (strmatch(s, "sharp")) |
572 | 0 | return ZEBRA_ROUTE_SHARP; |
573 | 0 | else if (strmatch(s, "openfabric")) |
574 | 0 | return ZEBRA_ROUTE_OPENFABRIC; |
575 | 0 | } |
576 | 0 | if (afi == AFI_IP6) { |
577 | 0 | if (strmatch(s, "kernel")) |
578 | 0 | return ZEBRA_ROUTE_KERNEL; |
579 | 0 | else if (strmatch(s, "connected")) |
580 | 0 | return ZEBRA_ROUTE_CONNECT; |
581 | 0 | else if (strmatch(s, "static")) |
582 | 0 | return ZEBRA_ROUTE_STATIC; |
583 | 0 | else if (strmatch(s, "ripng")) |
584 | 0 | return ZEBRA_ROUTE_RIPNG; |
585 | 0 | else if (strmatch(s, "ospf6")) |
586 | 0 | return ZEBRA_ROUTE_OSPF6; |
587 | 0 | else if (strmatch(s, "isis")) |
588 | 0 | return ZEBRA_ROUTE_ISIS; |
589 | 0 | else if (strmatch(s, "bgp")) |
590 | 0 | return ZEBRA_ROUTE_BGP; |
591 | 0 | else if (strmatch(s, "table")) |
592 | 0 | return ZEBRA_ROUTE_TABLE; |
593 | 0 | else if (strmatch(s, "vnc")) |
594 | 0 | return ZEBRA_ROUTE_VNC; |
595 | 0 | else if (strmatch(s, "vnc-direct")) |
596 | 0 | return ZEBRA_ROUTE_VNC_DIRECT; |
597 | 0 | else if (strmatch(s, "nhrp")) |
598 | 0 | return ZEBRA_ROUTE_NHRP; |
599 | 0 | else if (strmatch(s, "babel")) |
600 | 0 | return ZEBRA_ROUTE_BABEL; |
601 | 0 | else if (strmatch(s, "sharp")) |
602 | 0 | return ZEBRA_ROUTE_SHARP; |
603 | 0 | else if (strmatch(s, "openfabric")) |
604 | 0 | return ZEBRA_ROUTE_OPENFABRIC; |
605 | 0 | } |
606 | 0 | return -1; |
607 | 0 | } |
608 | | |
609 | | void zlog_hexdump(const void *mem, size_t len) |
610 | 0 | { |
611 | 0 | char line[64]; |
612 | 0 | const uint8_t *src = mem; |
613 | 0 | const uint8_t *end = src + len; |
614 | |
|
615 | 0 | if (len == 0) { |
616 | 0 | zlog_debug("%016lx: (zero length / no data)", (long)src); |
617 | 0 | return; |
618 | 0 | } |
619 | | |
620 | 0 | while (src < end) { |
621 | 0 | struct fbuf fb = { |
622 | 0 | .buf = line, |
623 | 0 | .pos = line, |
624 | 0 | .len = sizeof(line), |
625 | 0 | }; |
626 | 0 | const uint8_t *lineend = src + 8; |
627 | 0 | unsigned line_bytes = 0; |
628 | |
|
629 | 0 | bprintfrr(&fb, "%016lx: ", (long)src); |
630 | |
|
631 | 0 | while (src < lineend && src < end) { |
632 | 0 | bprintfrr(&fb, "%02x ", *src++); |
633 | 0 | line_bytes++; |
634 | 0 | } |
635 | 0 | if (line_bytes < 8) |
636 | 0 | bprintfrr(&fb, "%*s", (8 - line_bytes) * 3, ""); |
637 | |
|
638 | 0 | src -= line_bytes; |
639 | 0 | while (src < lineend && src < end && fb.pos < fb.buf + fb.len) { |
640 | 0 | uint8_t byte = *src++; |
641 | |
|
642 | 0 | if (isprint(byte)) |
643 | 0 | *fb.pos++ = byte; |
644 | 0 | else |
645 | 0 | *fb.pos++ = '.'; |
646 | 0 | } |
647 | |
|
648 | 0 | zlog_debug("%.*s", (int)(fb.pos - fb.buf), fb.buf); |
649 | 0 | } |
650 | 0 | } |
651 | | |
652 | | const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen) |
653 | 0 | { |
654 | 0 | const char *inbuf = in; |
655 | 0 | char *pos = buf, *end = buf + bufsz; |
656 | 0 | const char *iend = inbuf + inlen; |
657 | |
|
658 | 0 | memset(buf, 0, bufsz); |
659 | 0 | for (; inbuf < iend; inbuf++) { |
660 | | /* don't write partial escape sequence */ |
661 | 0 | if (end - pos < 5) |
662 | 0 | break; |
663 | | |
664 | 0 | if (*inbuf == '\n') |
665 | 0 | snprintf(pos, end - pos, "\\n"); |
666 | 0 | else if (*inbuf == '\r') |
667 | 0 | snprintf(pos, end - pos, "\\r"); |
668 | 0 | else if (*inbuf == '\t') |
669 | 0 | snprintf(pos, end - pos, "\\t"); |
670 | 0 | else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127) |
671 | 0 | snprintf(pos, end - pos, "\\x%02hhx", *inbuf); |
672 | 0 | else |
673 | 0 | *pos = *inbuf; |
674 | |
|
675 | 0 | pos += strlen(pos); |
676 | 0 | } |
677 | 0 | return buf; |
678 | 0 | } |