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