]> git.proxmox.com Git - mirror_frr.git/blame - lib/log.c
Merge pull request #12837 from donaldsharp/unlikely_routemap
[mirror_frr.git] / lib / log.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
274a4a44 2/*
274a4a44 3 * Logging of zebra
718e3744 4 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
718e3744 5 */
6
ab0181ee 7#define FRR_DEFINE_DESC_TABLE
e0ca5fde 8
718e3744 9#include <zebra.h>
10
8613585e 11#include "zclient.h"
718e3744 12#include "log.h"
13#include "memory.h"
14#include "command.h"
481bc15f 15#include "lib_errors.h"
77d9c926 16#include "lib/hook.h"
807f5b98 17#include "printfrr.h"
00dffa8c 18#include "frr_pthread.h"
481bc15f 19
68b8a15f
DL
20#ifdef HAVE_LIBUNWIND
21#define UNW_LOCAL_ONLY
22#include <libunwind.h>
23#include <dlfcn.h>
24#endif
25
56b40679
QY
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 */
d62a17ae 38const char *lookup_msg(const struct message *mz, int kz, const char *nf)
56b40679 39{
d62a17ae 40 static struct message nt = {0};
41 const char *rz = nf ? nf : "(no message found)";
42 const struct message *pnt;
43 for (pnt = mz; memcmp(pnt, &nt, sizeof(struct message)); pnt++)
44 if (pnt->key == kz) {
45 rz = pnt->str ? pnt->str : rz;
46 break;
47 }
48 return rz;
56b40679 49}
718e3744 50
56b40679 51/* For time string format. */
e36f61b5 52size_t frr_timestamp(int timestamp_precision, char *buf, size_t buflen)
718e3744 53{
d62a17ae 54 static struct {
55 time_t last;
56 size_t len;
57 char buf[28];
58 } cache;
59 struct timeval clock;
60
61 gettimeofday(&clock, NULL);
62
63 /* first, we update the cache if the time has changed */
64 if (cache.last != clock.tv_sec) {
a2700b50 65 struct tm tm;
d62a17ae 66 cache.last = clock.tv_sec;
a2700b50 67 localtime_r(&cache.last, &tm);
d62a17ae 68 cache.len = strftime(cache.buf, sizeof(cache.buf),
a2700b50 69 "%Y/%m/%d %H:%M:%S", &tm);
d62a17ae 70 }
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 if (buflen > cache.len) {
77 memcpy(buf, cache.buf, cache.len);
78 if ((timestamp_precision > 0)
79 && (buflen > cache.len + 1 + timestamp_precision)) {
80 /* should we worry about locale issues? */
81 static const int divisor[] = {0, 100000, 10000, 1000,
82 100, 10, 1};
83 int prec;
84 char *p = buf + cache.len + 1
85 + (prec = timestamp_precision);
86 *p-- = '\0';
87 while (prec > 6)
88 /* this is unlikely to happen, but protect anyway */
89 {
90 *p-- = '0';
91 prec--;
92 }
93 clock.tv_usec /= divisor[prec];
94 do {
95 *p-- = '0' + (clock.tv_usec % 10);
96 clock.tv_usec /= 10;
97 } while (--prec > 0);
98 *p = '.';
99 return cache.len + 1 + timestamp_precision;
100 }
101 buf[cache.len] = '\0';
102 return cache.len;
1ed72e0b 103 }
d62a17ae 104 if (buflen > 0)
105 buf[0] = '\0';
106 return 0;
1ed72e0b 107}
718e3744 108
c22f6d8c
DL
109/*
110 * crash handling
111 *
112 * NB: only AS-Safe (async-signal) functions can be used here!
113 */
7d149b8e 114
c22f6d8c
DL
115/* Note: the goal here is to use only async-signal-safe functions. */
116void zlog_signal(int signo, const char *action, void *siginfo_v,
117 void *program_counter)
118{
c22f6d8c 119 siginfo_t *siginfo = siginfo_v;
c22f6d8c
DL
120 time_t now;
121 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
122 + 100];
c22f6d8c
DL
123 struct fbuf fb = { .buf = buf, .pos = buf, .len = sizeof(buf) };
124
125 time(&now);
c22f6d8c
DL
126
127 bprintfrr(&fb, "Received signal %d at %lld", signo, (long long)now);
c22f6d8c
DL
128 if (program_counter)
129 bprintfrr(&fb, " (si_addr 0x%tx, PC 0x%tx)",
130 (ptrdiff_t)siginfo->si_addr,
131 (ptrdiff_t)program_counter);
132 else
133 bprintfrr(&fb, " (si_addr 0x%tx)",
134 (ptrdiff_t)siginfo->si_addr);
c22f6d8c
DL
135 bprintfrr(&fb, "; %s\n", action);
136
0bdeb5e5 137 zlog_sigsafe(fb.buf, fb.pos - fb.buf);
c22f6d8c 138
0bdeb5e5 139 zlog_backtrace_sigsafe(LOG_CRIT, program_counter);
c22f6d8c
DL
140
141 fb.pos = buf;
d62a17ae 142
e6685141 143 struct event *tc;
d62a17ae 144 tc = pthread_getspecific(thread_current);
d1265948 145
c22f6d8c
DL
146 if (!tc)
147 bprintfrr(&fb, "no thread information available\n");
148 else
60a3efec
DL
149 bprintfrr(&fb, "in thread %s scheduled from %s:%d %s()\n",
150 tc->xref->funcname, tc->xref->xref.file,
151 tc->xref->xref.line, tc->xref->xref.func);
d1265948 152
0bdeb5e5 153 zlog_sigsafe(fb.buf, fb.pos - fb.buf);
063ee52a 154}
155
156/* Log a backtrace using only async-signal-safe functions.
157 Needs to be enhanced to support syslog logging. */
d62a17ae 158void zlog_backtrace_sigsafe(int priority, void *program_counter)
063ee52a 159{
68b8a15f 160#ifdef HAVE_LIBUNWIND
c22f6d8c
DL
161 char buf[256];
162 struct fbuf fb = { .buf = buf, .len = sizeof(buf) };
68b8a15f
DL
163 unw_cursor_t cursor;
164 unw_context_t uc;
165 unw_word_t ip, off, sp;
166 Dl_info dlinfo;
167
63e04039
DS
168 memset(&uc, 0, sizeof(uc));
169 memset(&cursor, 0, sizeof(cursor));
170
68b8a15f
DL
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
c22f6d8c
DL
179 if (!unw_get_proc_name(&cursor, buf, sizeof(buf), &off))
180 snprintfrr(name, sizeof(name), "%s+%#lx",
181 buf, (long)off);
68b8a15f 182
c22f6d8c
DL
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");
0bdeb5e5 191 zlog_sigsafe(fb.buf, fb.pos - fb.buf);
68b8a15f 192 }
0bdeb5e5 193#elif defined(HAVE_GLIBC_BACKTRACE)
d62a17ae 194 void *array[64];
0bdeb5e5 195 int size, i;
c22f6d8c
DL
196 char buf[128];
197 struct fbuf fb = { .buf = buf, .pos = buf, .len = sizeof(buf) };
198 char **bt = NULL;
59a06a91 199
d62a17ae 200 size = backtrace(array, array_size(array));
201 if (size <= 0 || (size_t)size > array_size(array))
202 return;
203
0bdeb5e5
DL
204 bprintfrr(&fb, "Backtrace for %d stack frames:", size);
205 zlog_sigsafe(fb.pos, fb.buf - fb.pos);
206
207 bt = backtrace_symbols(array, size);
208
209 for (i = 0; i < size; i++) {
210 fb.pos = buf;
211 if (bt)
212 bprintfrr(&fb, "%s", bt[i]);
213 else
214 bprintfrr(&fb, "[bt %d] 0x%tx", i,
215 (ptrdiff_t)(array[i]));
216 zlog_sigsafe(fb.buf, fb.pos - fb.buf);
d62a17ae 217 }
0bdeb5e5
DL
218 if (bt)
219 free(bt);
fb66b29c 220#endif /* HAVE_STRACK_TRACE */
063ee52a 221}
222
d62a17ae 223void zlog_backtrace(int priority)
063ee52a 224{
68b8a15f
DL
225#ifdef HAVE_LIBUNWIND
226 char buf[100];
8486b790 227 unw_cursor_t cursor = {};
68b8a15f
DL
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)
d62a17ae 257 void *array[20];
258 int size, i;
259 char **strings;
260
261 size = backtrace(array, array_size(array));
262 if (size <= 0 || (size_t)size > array_size(array)) {
09c866e3 263 flog_err_sys(
450971aa 264 EC_LIB_SYSTEM_CALL,
3efd0893 265 "Cannot get backtrace, returned invalid # of frames %d (valid range is between 1 and %lu)",
09c866e3 266 size, (unsigned long)(array_size(array)));
d62a17ae 267 return;
268 }
269 zlog(priority, "Backtrace for %d stack frames:", size);
270 if (!(strings = backtrace_symbols(array, size))) {
450971aa 271 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 272 "Cannot get backtrace symbols (out of memory?)");
d62a17ae 273 for (i = 0; i < size; i++)
274 zlog(priority, "[bt %d] %p", i, array[i]);
275 } else {
276 for (i = 0; i < size; i++)
277 zlog(priority, "[bt %d] %s", i, strings[i]);
278 free(strings);
279 }
68b8a15f
DL
280#else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
281 zlog(priority, "No backtrace available on this platform.");
282#endif
59a06a91 283}
284
d62a17ae 285void zlog_thread_info(int log_level)
d1265948 286{
e6685141 287 struct event *tc;
d62a17ae 288 tc = pthread_getspecific(thread_current);
289
290 if (tc)
291 zlog(log_level,
60a3efec
DL
292 "Current thread function %s, scheduled from file %s, line %u in %s()",
293 tc->xref->funcname, tc->xref->xref.file,
294 tc->xref->xref.line, tc->xref->xref.func);
d62a17ae 295 else
296 zlog(log_level, "Current thread not known/applicable");
d1265948
DL
297}
298
d62a17ae 299void memory_oom(size_t size, const char *name)
3b4cd783 300{
0bdeb5e5
DL
301 zlog(LOG_CRIT,
302 "out of memory: failed to allocate %zu bytes for %s object",
303 size, name);
304 zlog_backtrace(LOG_CRIT);
305 log_memstats(stderr, "log");
3b4cd783
DL
306 abort();
307}
6b0655a2 308
ca359769 309/* Wrapper around strerror to handle case where it returns NULL. */
d62a17ae 310const char *safe_strerror(int errnum)
ca359769 311{
d62a17ae 312 const char *s = strerror(errnum);
313 return (s != NULL) ? s : "Unknown error";
ca359769 314}
f52d13cb 315
d6d672aa
PJ
316#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
317static const struct zebra_desc_table command_types[] = {
d62a17ae 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),
e0ae31b8 324 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER),
447586fd 325 DESC_ENTRY(ZEBRA_INTERFACE_SET_PROTODOWN),
0e51b4a3
RW
326 DESC_ENTRY(ZEBRA_ROUTE_ADD),
327 DESC_ENTRY(ZEBRA_ROUTE_DELETE),
7ea7b86e 328 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER),
d62a17ae 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),
4cf8bb32 337 DESC_ENTRY(ZEBRA_CAPABILITIES),
d62a17ae 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),
d62a17ae 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),
74489921
RW
348 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD),
349 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL),
d62a17ae 350 DESC_ENTRY(ZEBRA_VRF_UNREGISTER),
351 DESC_ENTRY(ZEBRA_VRF_ADD),
352 DESC_ENTRY(ZEBRA_VRF_DELETE),
c83c5e44 353 DESC_ENTRY(ZEBRA_VRF_LABEL),
d62a17ae 354 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE),
355 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER),
4cf8bb32 356 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER),
d62a17ae 357 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV),
358 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV),
34ee41c6 359 DESC_ENTRY(ZEBRA_NEXTHOP_LOOKUP_MRIB),
d62a17ae 360 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS),
361 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD),
362 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE),
ea6b290b 363 DESC_ENTRY(ZEBRA_MPLS_LABELS_REPLACE),
31f937fb
SM
364 DESC_ENTRY(ZEBRA_SR_POLICY_SET),
365 DESC_ENTRY(ZEBRA_SR_POLICY_DELETE),
366 DESC_ENTRY(ZEBRA_SR_POLICY_NOTIFY_STATUS),
d62a17ae 367 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS),
368 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT),
f533be73 369 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC),
d62a17ae 370 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK),
371 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK),
4cf8bb32
DS
372 DESC_ENTRY(ZEBRA_FEC_REGISTER),
373 DESC_ENTRY(ZEBRA_FEC_UNREGISTER),
374 DESC_ENTRY(ZEBRA_FEC_UPDATE),
1a98c087 375 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW),
fc08a52f 376 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP),
31310b25 377 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET),
447586fd 378 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI),
50f74cf1 379 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD),
380 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL),
ce5160c0
AK
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),
d62a17ae 385 DESC_ENTRY(ZEBRA_VNI_ADD),
386 DESC_ENTRY(ZEBRA_VNI_DEL),
b7cfce93
MK
387 DESC_ENTRY(ZEBRA_L3VNI_ADD),
388 DESC_ENTRY(ZEBRA_L3VNI_DEL),
d62a17ae 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),
31310b25
MK
393 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD),
394 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL),
d62a17ae 395 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD),
396 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL),
3950b52c 397 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION),
6833ae01 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),
e16abbb3
DS
403 DESC_ENTRY(ZEBRA_RULE_ADD),
404 DESC_ENTRY(ZEBRA_RULE_DELETE),
405 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER),
75fb51c1
PG
406 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT),
407 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK),
408 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK),
d59c13af
PG
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),
4cf8bb32
DS
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),
fbac9605 418 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL),
4ab3321f
AK
419 DESC_ENTRY(ZEBRA_VXLAN_SG_ADD),
420 DESC_ENTRY(ZEBRA_VXLAN_SG_DEL),
ecbbc3a7 421 DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY),
d1a3e8df
AK
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),
447586fd
MS
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),
9ab0b2a3 436 DESC_ENTRY(ZEBRA_ERROR),
ff491140
MS
437 DESC_ENTRY(ZEBRA_CLIENT_CAPABILITIES),
438 DESC_ENTRY(ZEBRA_OPAQUE_MESSAGE),
439 DESC_ENTRY(ZEBRA_OPAQUE_REGISTER),
d68e74b4 440 DESC_ENTRY(ZEBRA_OPAQUE_UNREGISTER),
b36bedd2 441 DESC_ENTRY(ZEBRA_NEIGH_DISCOVER),
581e797e 442 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_REQUEST),
7bfa7d02 443 DESC_ENTRY(ZEBRA_CLIENT_CLOSE_NOTIFY),
fda64ab4
PG
444 DESC_ENTRY(ZEBRA_NHRP_NEIGH_ADDED),
445 DESC_ENTRY(ZEBRA_NHRP_NEIGH_REMOVED),
7723e8d3
PG
446 DESC_ENTRY(ZEBRA_NHRP_NEIGH_GET),
447 DESC_ENTRY(ZEBRA_NHRP_NEIGH_REGISTER),
05657ec2 448 DESC_ENTRY(ZEBRA_NHRP_NEIGH_UNREGISTER),
d603c077
PG
449 DESC_ENTRY(ZEBRA_NEIGH_IP_ADD),
450 DESC_ENTRY(ZEBRA_NEIGH_IP_DEL),
632d8306
PG
451 DESC_ENTRY(ZEBRA_CONFIGURE_ARP),
452 DESC_ENTRY(ZEBRA_GRE_GET),
453 DESC_ENTRY(ZEBRA_GRE_UPDATE),
447586fd
MS
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)};
d6d672aa
PJ
461#undef DESC_ENTRY
462
d62a17ae 463static const struct zebra_desc_table unknown = {0, "unknown", '?'};
d6d672aa 464
d7c0a89a 465static const struct zebra_desc_table *zroute_lookup(unsigned int zroute)
f52d13cb 466{
d7c0a89a 467 unsigned int i;
d62a17ae 468
469 if (zroute >= array_size(route_types)) {
450971aa 470 flog_err(EC_LIB_DEVELOPMENT, "unknown zebra route type: %u",
1c50c1c0 471 zroute);
d62a17ae 472 return &unknown;
473 }
474 if (zroute == route_types[zroute].type)
475 return &route_types[zroute];
476 for (i = 0; i < array_size(route_types); i++) {
477 if (zroute == route_types[i].type) {
478 zlog_warn(
3efd0893 479 "internal error: route type table out of order while searching for %u, please notify developers",
d62a17ae 480 zroute);
481 return &route_types[i];
482 }
483 }
450971aa 484 flog_err(EC_LIB_DEVELOPMENT,
1c50c1c0 485 "internal error: cannot find route type %u in table!", zroute);
d62a17ae 486 return &unknown;
f52d13cb 487}
488
d7c0a89a 489const char *zebra_route_string(unsigned int zroute)
f52d13cb 490{
d62a17ae 491 return zroute_lookup(zroute)->string;
f52d13cb 492}
493
d7c0a89a 494char zebra_route_char(unsigned int zroute)
f52d13cb 495{
d62a17ae 496 return zroute_lookup(zroute)->chr;
f52d13cb 497}
d6d672aa 498
d62a17ae 499const char *zserv_command_string(unsigned int command)
d6d672aa 500{
d62a17ae 501 if (command >= array_size(command_types)) {
450971aa 502 flog_err(EC_LIB_DEVELOPMENT, "unknown zserv command type: %u",
1c50c1c0 503 command);
d62a17ae 504 return unknown.string;
505 }
506 return command_types[command].string;
d6d672aa 507}
7514fb77 508
115ccb9a
DS
509#define DESC_ENTRY(T) [(T)] = {(T), (#T), '\0'}
510static 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
519const char *zserv_gr_client_cap_string(uint32_t zcc)
520{
521 if (zcc >= array_size(gr_client_cap_types)) {
522 flog_err(EC_LIB_DEVELOPMENT, "unknown zserv command type: %u",
523 zcc);
524 return unknown.string;
525 }
526 return gr_client_cap_types[zcc].string;
527}
528
d62a17ae 529int proto_name2num(const char *s)
7514fb77 530{
d62a17ae 531 unsigned i;
7514fb77 532
d62a17ae 533 for (i = 0; i < array_size(route_types); ++i)
534 if (strcasecmp(s, route_types[i].string) == 0)
535 return route_types[i].type;
536 return -1;
7514fb77 537}
e0ca5fde 538
d62a17ae 539int proto_redistnum(int afi, const char *s)
e0ca5fde 540{
d62a17ae 541 if (!s)
542 return -1;
543
544 if (afi == AFI_IP) {
545 if (strmatch(s, "kernel"))
546 return ZEBRA_ROUTE_KERNEL;
547 else if (strmatch(s, "connected"))
548 return ZEBRA_ROUTE_CONNECT;
549 else if (strmatch(s, "static"))
550 return ZEBRA_ROUTE_STATIC;
551 else if (strmatch(s, "rip"))
552 return ZEBRA_ROUTE_RIP;
553 else if (strmatch(s, "eigrp"))
554 return ZEBRA_ROUTE_EIGRP;
555 else if (strmatch(s, "ospf"))
556 return ZEBRA_ROUTE_OSPF;
557 else if (strmatch(s, "isis"))
558 return ZEBRA_ROUTE_ISIS;
559 else if (strmatch(s, "bgp"))
560 return ZEBRA_ROUTE_BGP;
561 else if (strmatch(s, "table"))
562 return ZEBRA_ROUTE_TABLE;
563 else if (strmatch(s, "vnc"))
564 return ZEBRA_ROUTE_VNC;
565 else if (strmatch(s, "vnc-direct"))
566 return ZEBRA_ROUTE_VNC_DIRECT;
567 else if (strmatch(s, "nhrp"))
568 return ZEBRA_ROUTE_NHRP;
569 else if (strmatch(s, "babel"))
570 return ZEBRA_ROUTE_BABEL;
8a71d93d
DS
571 else if (strmatch(s, "sharp"))
572 return ZEBRA_ROUTE_SHARP;
7c0cbd0e
CF
573 else if (strmatch(s, "openfabric"))
574 return ZEBRA_ROUTE_OPENFABRIC;
d62a17ae 575 }
576 if (afi == AFI_IP6) {
577 if (strmatch(s, "kernel"))
578 return ZEBRA_ROUTE_KERNEL;
579 else if (strmatch(s, "connected"))
580 return ZEBRA_ROUTE_CONNECT;
581 else if (strmatch(s, "static"))
582 return ZEBRA_ROUTE_STATIC;
583 else if (strmatch(s, "ripng"))
584 return ZEBRA_ROUTE_RIPNG;
585 else if (strmatch(s, "ospf6"))
586 return ZEBRA_ROUTE_OSPF6;
587 else if (strmatch(s, "isis"))
588 return ZEBRA_ROUTE_ISIS;
589 else if (strmatch(s, "bgp"))
590 return ZEBRA_ROUTE_BGP;
591 else if (strmatch(s, "table"))
592 return ZEBRA_ROUTE_TABLE;
593 else if (strmatch(s, "vnc"))
594 return ZEBRA_ROUTE_VNC;
595 else if (strmatch(s, "vnc-direct"))
596 return ZEBRA_ROUTE_VNC_DIRECT;
597 else if (strmatch(s, "nhrp"))
598 return ZEBRA_ROUTE_NHRP;
599 else if (strmatch(s, "babel"))
600 return ZEBRA_ROUTE_BABEL;
8a71d93d
DS
601 else if (strmatch(s, "sharp"))
602 return ZEBRA_ROUTE_SHARP;
7c0cbd0e
CF
603 else if (strmatch(s, "openfabric"))
604 return ZEBRA_ROUTE_OPENFABRIC;
d62a17ae 605 }
606 return -1;
e0ca5fde 607}
99a6c6cd 608
46171e25 609void zlog_hexdump(const void *mem, size_t len)
d62a17ae 610{
46171e25
DL
611 char line[64];
612 const uint8_t *src = mem;
613 const uint8_t *end = src + len;
614
615 if (len == 0) {
616 zlog_debug("%016lx: (zero length / no data)", (long)src);
617 return;
618 }
619
620 while (src < end) {
621 struct fbuf fb = {
622 .buf = line,
623 .pos = line,
624 .len = sizeof(line),
625 };
626 const uint8_t *lineend = src + 8;
627 unsigned line_bytes = 0;
628
629 bprintfrr(&fb, "%016lx: ", (long)src);
630
631 while (src < lineend && src < end) {
632 bprintfrr(&fb, "%02x ", *src++);
633 line_bytes++;
634 }
635 if (line_bytes < 8)
636 bprintfrr(&fb, "%*s", (8 - line_bytes) * 3, "");
637
638 src -= line_bytes;
639 while (src < lineend && src < end && fb.pos < fb.buf + fb.len) {
640 uint8_t byte = *src++;
641
642 if (isprint(byte))
643 *fb.pos++ = byte;
644 else
645 *fb.pos++ = '.';
d62a17ae 646 }
46171e25
DL
647
648 zlog_debug("%.*s", (int)(fb.pos - fb.buf), fb.buf);
d62a17ae 649 }
99a6c6cd 650}
1f806fc2 651
d62a17ae 652const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen)
1f806fc2 653{
d62a17ae 654 const char *inbuf = in;
655 char *pos = buf, *end = buf + bufsz;
656 const char *iend = inbuf + inlen;
657
658 memset(buf, 0, bufsz);
659 for (; inbuf < iend; inbuf++) {
660 /* don't write partial escape sequence */
661 if (end - pos < 5)
662 break;
663
664 if (*inbuf == '\n')
665 snprintf(pos, end - pos, "\\n");
666 else if (*inbuf == '\r')
667 snprintf(pos, end - pos, "\\r");
668 else if (*inbuf == '\t')
669 snprintf(pos, end - pos, "\\t");
670 else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127)
671 snprintf(pos, end - pos, "\\x%02hhx", *inbuf);
672 else
673 *pos = *inbuf;
674
675 pos += strlen(pos);
676 }
677 return buf;
1f806fc2 678}