]> git.proxmox.com Git - mirror_frr.git/blame - lib/log.c
Merge pull request #7220 from idryzhov/fix-clear-isis
[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
164 bprintfrr(&fb, "in thread %s scheduled from %s:%d\n",
165 tc->funcname, tc->schedfrom, tc->schedfrom_line);
d1265948 166
0bdeb5e5 167 zlog_sigsafe(fb.buf, fb.pos - fb.buf);
063ee52a 168}
169
170/* Log a backtrace using only async-signal-safe functions.
171 Needs to be enhanced to support syslog logging. */
d62a17ae 172void zlog_backtrace_sigsafe(int priority, void *program_counter)
063ee52a 173{
68b8a15f 174#ifdef HAVE_LIBUNWIND
c22f6d8c
DL
175 char buf[256];
176 struct fbuf fb = { .buf = buf, .len = sizeof(buf) };
68b8a15f
DL
177 unw_cursor_t cursor;
178 unw_context_t uc;
179 unw_word_t ip, off, sp;
180 Dl_info dlinfo;
181
182 unw_getcontext(&uc);
183 unw_init_local(&cursor, &uc);
184 while (unw_step(&cursor) > 0) {
185 char name[128] = "?";
186
187 unw_get_reg(&cursor, UNW_REG_IP, &ip);
188 unw_get_reg(&cursor, UNW_REG_SP, &sp);
189
c22f6d8c
DL
190 if (!unw_get_proc_name(&cursor, buf, sizeof(buf), &off))
191 snprintfrr(name, sizeof(name), "%s+%#lx",
192 buf, (long)off);
68b8a15f 193
c22f6d8c
DL
194 fb.pos = buf;
195 if (unw_is_signal_frame(&cursor))
196 bprintfrr(&fb, " ---- signal ----\n");
197 bprintfrr(&fb, "%-30s %16lx %16lx", name, (long)ip, (long)sp);
198 if (dladdr((void *)ip, &dlinfo))
199 bprintfrr(&fb, " %s (mapped at %p)",
200 dlinfo.dli_fname, dlinfo.dli_fbase);
201 bprintfrr(&fb, "\n");
0bdeb5e5 202 zlog_sigsafe(fb.buf, fb.pos - fb.buf);
68b8a15f 203 }
0bdeb5e5 204#elif defined(HAVE_GLIBC_BACKTRACE)
d62a17ae 205 void *array[64];
0bdeb5e5 206 int size, i;
c22f6d8c
DL
207 char buf[128];
208 struct fbuf fb = { .buf = buf, .pos = buf, .len = sizeof(buf) };
209 char **bt = NULL;
59a06a91 210
d62a17ae 211 size = backtrace(array, array_size(array));
212 if (size <= 0 || (size_t)size > array_size(array))
213 return;
214
0bdeb5e5
DL
215 bprintfrr(&fb, "Backtrace for %d stack frames:", size);
216 zlog_sigsafe(fb.pos, fb.buf - fb.pos);
217
218 bt = backtrace_symbols(array, size);
219
220 for (i = 0; i < size; i++) {
221 fb.pos = buf;
222 if (bt)
223 bprintfrr(&fb, "%s", bt[i]);
224 else
225 bprintfrr(&fb, "[bt %d] 0x%tx", i,
226 (ptrdiff_t)(array[i]));
227 zlog_sigsafe(fb.buf, fb.pos - fb.buf);
d62a17ae 228 }
0bdeb5e5
DL
229 if (bt)
230 free(bt);
fb66b29c 231#endif /* HAVE_STRACK_TRACE */
063ee52a 232}
233
d62a17ae 234void zlog_backtrace(int priority)
063ee52a 235{
68b8a15f
DL
236#ifdef HAVE_LIBUNWIND
237 char buf[100];
238 unw_cursor_t cursor;
239 unw_context_t uc;
240 unw_word_t ip, off, sp;
241 Dl_info dlinfo;
242
243 unw_getcontext(&uc);
244 unw_init_local(&cursor, &uc);
245 zlog(priority, "Backtrace:");
246 while (unw_step(&cursor) > 0) {
247 char name[128] = "?";
248
249 unw_get_reg(&cursor, UNW_REG_IP, &ip);
250 unw_get_reg(&cursor, UNW_REG_SP, &sp);
251
252 if (unw_is_signal_frame(&cursor))
253 zlog(priority, " ---- signal ----");
254
255 if (!unw_get_proc_name(&cursor, buf, sizeof(buf), &off))
256 snprintf(name, sizeof(name), "%s+%#lx",
257 buf, (long)off);
258
259 if (dladdr((void *)ip, &dlinfo))
260 zlog(priority, "%-30s %16lx %16lx %s (mapped at %p)",
261 name, (long)ip, (long)sp,
262 dlinfo.dli_fname, dlinfo.dli_fbase);
263 else
264 zlog(priority, "%-30s %16lx %16lx",
265 name, (long)ip, (long)sp);
266 }
267#elif defined(HAVE_GLIBC_BACKTRACE)
d62a17ae 268 void *array[20];
269 int size, i;
270 char **strings;
271
272 size = backtrace(array, array_size(array));
273 if (size <= 0 || (size_t)size > array_size(array)) {
09c866e3 274 flog_err_sys(
450971aa 275 EC_LIB_SYSTEM_CALL,
3efd0893 276 "Cannot get backtrace, returned invalid # of frames %d (valid range is between 1 and %lu)",
09c866e3 277 size, (unsigned long)(array_size(array)));
d62a17ae 278 return;
279 }
280 zlog(priority, "Backtrace for %d stack frames:", size);
281 if (!(strings = backtrace_symbols(array, size))) {
450971aa 282 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 283 "Cannot get backtrace symbols (out of memory?)");
d62a17ae 284 for (i = 0; i < size; i++)
285 zlog(priority, "[bt %d] %p", i, array[i]);
286 } else {
287 for (i = 0; i < size; i++)
288 zlog(priority, "[bt %d] %s", i, strings[i]);
289 free(strings);
290 }
68b8a15f
DL
291#else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
292 zlog(priority, "No backtrace available on this platform.");
293#endif
59a06a91 294}
295
d62a17ae 296void zlog_thread_info(int log_level)
d1265948 297{
d62a17ae 298 struct thread *tc;
299 tc = pthread_getspecific(thread_current);
300
301 if (tc)
302 zlog(log_level,
3efd0893 303 "Current thread function %s, scheduled from file %s, line %u",
d62a17ae 304 tc->funcname, tc->schedfrom, tc->schedfrom_line);
305 else
306 zlog(log_level, "Current thread not known/applicable");
d1265948
DL
307}
308
d62a17ae 309void _zlog_assert_failed(const char *assertion, const char *file,
310 unsigned int line, const char *function)
cee3df1e 311{
d62a17ae 312 zlog(LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
313 assertion, file, line, (function ? function : "?"));
314 zlog_backtrace(LOG_CRIT);
315 zlog_thread_info(LOG_CRIT);
9eed278b 316 log_memstats(stderr, "log");
d62a17ae 317 abort();
cee3df1e 318}
319
d62a17ae 320void memory_oom(size_t size, const char *name)
3b4cd783 321{
0bdeb5e5
DL
322 zlog(LOG_CRIT,
323 "out of memory: failed to allocate %zu bytes for %s object",
324 size, name);
325 zlog_backtrace(LOG_CRIT);
326 log_memstats(stderr, "log");
3b4cd783
DL
327 abort();
328}
6b0655a2 329
ca359769 330/* Wrapper around strerror to handle case where it returns NULL. */
d62a17ae 331const char *safe_strerror(int errnum)
ca359769 332{
d62a17ae 333 const char *s = strerror(errnum);
334 return (s != NULL) ? s : "Unknown error";
ca359769 335}
f52d13cb 336
d6d672aa
PJ
337#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
338static const struct zebra_desc_table command_types[] = {
d62a17ae 339 DESC_ENTRY(ZEBRA_INTERFACE_ADD),
340 DESC_ENTRY(ZEBRA_INTERFACE_DELETE),
341 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD),
342 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE),
343 DESC_ENTRY(ZEBRA_INTERFACE_UP),
344 DESC_ENTRY(ZEBRA_INTERFACE_DOWN),
e0ae31b8 345 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER),
0e51b4a3
RW
346 DESC_ENTRY(ZEBRA_ROUTE_ADD),
347 DESC_ENTRY(ZEBRA_ROUTE_DELETE),
7ea7b86e 348 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER),
d62a17ae 349 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD),
350 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE),
351 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
352 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
353 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD),
354 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE),
355 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE),
356 DESC_ENTRY(ZEBRA_HELLO),
4cf8bb32 357 DESC_ENTRY(ZEBRA_CAPABILITIES),
d62a17ae 358 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER),
359 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER),
360 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE),
361 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD),
362 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE),
363 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE),
364 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER),
365 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER),
366 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE),
d62a17ae 367 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER),
368 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER),
369 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE),
370 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY),
74489921
RW
371 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD),
372 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL),
d62a17ae 373 DESC_ENTRY(ZEBRA_VRF_UNREGISTER),
374 DESC_ENTRY(ZEBRA_VRF_ADD),
375 DESC_ENTRY(ZEBRA_VRF_DELETE),
c83c5e44 376 DESC_ENTRY(ZEBRA_VRF_LABEL),
d62a17ae 377 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE),
378 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER),
4cf8bb32 379 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER),
d62a17ae 380 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV),
381 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV),
382 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB),
383 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS),
384 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD),
385 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE),
ea6b290b 386 DESC_ENTRY(ZEBRA_MPLS_LABELS_REPLACE),
31f937fb
SM
387 DESC_ENTRY(ZEBRA_SR_POLICY_SET),
388 DESC_ENTRY(ZEBRA_SR_POLICY_DELETE),
389 DESC_ENTRY(ZEBRA_SR_POLICY_NOTIFY_STATUS),
d62a17ae 390 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS),
391 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT),
f533be73 392 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC),
d62a17ae 393 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK),
394 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK),
4cf8bb32
DS
395 DESC_ENTRY(ZEBRA_FEC_REGISTER),
396 DESC_ENTRY(ZEBRA_FEC_UNREGISTER),
397 DESC_ENTRY(ZEBRA_FEC_UPDATE),
d62a17ae 398 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI),
1a98c087 399 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW),
fc08a52f 400 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP),
31310b25 401 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET),
50f74cf1 402 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD),
403 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL),
ce5160c0
AK
404 DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_ADD),
405 DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_DEL),
406 DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_ADD),
407 DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_DEL),
d62a17ae 408 DESC_ENTRY(ZEBRA_VNI_ADD),
409 DESC_ENTRY(ZEBRA_VNI_DEL),
b7cfce93
MK
410 DESC_ENTRY(ZEBRA_L3VNI_ADD),
411 DESC_ENTRY(ZEBRA_L3VNI_DEL),
d62a17ae 412 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD),
413 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL),
414 DESC_ENTRY(ZEBRA_MACIP_ADD),
415 DESC_ENTRY(ZEBRA_MACIP_DEL),
31310b25
MK
416 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD),
417 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL),
d62a17ae 418 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD),
419 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL),
3950b52c 420 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION),
6833ae01 421 DESC_ENTRY(ZEBRA_PW_ADD),
422 DESC_ENTRY(ZEBRA_PW_DELETE),
423 DESC_ENTRY(ZEBRA_PW_SET),
424 DESC_ENTRY(ZEBRA_PW_UNSET),
425 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE),
e16abbb3
DS
426 DESC_ENTRY(ZEBRA_RULE_ADD),
427 DESC_ENTRY(ZEBRA_RULE_DELETE),
428 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER),
75fb51c1
PG
429 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT),
430 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK),
431 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK),
d59c13af
PG
432 DESC_ENTRY(ZEBRA_IPSET_CREATE),
433 DESC_ENTRY(ZEBRA_IPSET_DESTROY),
434 DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD),
435 DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE),
4cf8bb32
DS
436 DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER),
437 DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER),
438 DESC_ENTRY(ZEBRA_IPTABLE_ADD),
439 DESC_ENTRY(ZEBRA_IPTABLE_DELETE),
440 DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER),
fbac9605 441 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL),
4ab3321f
AK
442 DESC_ENTRY(ZEBRA_VXLAN_SG_ADD),
443 DESC_ENTRY(ZEBRA_VXLAN_SG_DEL),
ecbbc3a7 444 DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY),
d1a3e8df
AK
445 DESC_ENTRY(ZEBRA_MLAG_PROCESS_UP),
446 DESC_ENTRY(ZEBRA_MLAG_PROCESS_DOWN),
447 DESC_ENTRY(ZEBRA_MLAG_CLIENT_REGISTER),
448 DESC_ENTRY(ZEBRA_MLAG_CLIENT_UNREGISTER),
449 DESC_ENTRY(ZEBRA_MLAG_FORWARD_MSG),
9ab0b2a3 450 DESC_ENTRY(ZEBRA_ERROR),
ff491140
MS
451 DESC_ENTRY(ZEBRA_CLIENT_CAPABILITIES),
452 DESC_ENTRY(ZEBRA_OPAQUE_MESSAGE),
453 DESC_ENTRY(ZEBRA_OPAQUE_REGISTER),
d68e74b4 454 DESC_ENTRY(ZEBRA_OPAQUE_UNREGISTER),
b36bedd2
SW
455 DESC_ENTRY(ZEBRA_NEIGH_DISCOVER),
456 DESC_ENTRY(ZEBRA_NHG_ADD),
391c7a3b
SW
457 DESC_ENTRY(ZEBRA_NHG_DEL),
458 DESC_ENTRY(ZEBRA_NHG_NOTIFY_OWNER)};
d6d672aa
PJ
459#undef DESC_ENTRY
460
d62a17ae 461static const struct zebra_desc_table unknown = {0, "unknown", '?'};
d6d672aa 462
d7c0a89a 463static const struct zebra_desc_table *zroute_lookup(unsigned int zroute)
f52d13cb 464{
d7c0a89a 465 unsigned int i;
d62a17ae 466
467 if (zroute >= array_size(route_types)) {
450971aa 468 flog_err(EC_LIB_DEVELOPMENT, "unknown zebra route type: %u",
1c50c1c0 469 zroute);
d62a17ae 470 return &unknown;
471 }
472 if (zroute == route_types[zroute].type)
473 return &route_types[zroute];
474 for (i = 0; i < array_size(route_types); i++) {
475 if (zroute == route_types[i].type) {
476 zlog_warn(
3efd0893 477 "internal error: route type table out of order while searching for %u, please notify developers",
d62a17ae 478 zroute);
479 return &route_types[i];
480 }
481 }
450971aa 482 flog_err(EC_LIB_DEVELOPMENT,
1c50c1c0 483 "internal error: cannot find route type %u in table!", zroute);
d62a17ae 484 return &unknown;
f52d13cb 485}
486
d7c0a89a 487const char *zebra_route_string(unsigned int zroute)
f52d13cb 488{
d62a17ae 489 return zroute_lookup(zroute)->string;
f52d13cb 490}
491
d7c0a89a 492char zebra_route_char(unsigned int zroute)
f52d13cb 493{
d62a17ae 494 return zroute_lookup(zroute)->chr;
f52d13cb 495}
d6d672aa 496
d62a17ae 497const char *zserv_command_string(unsigned int command)
d6d672aa 498{
d62a17ae 499 if (command >= array_size(command_types)) {
450971aa 500 flog_err(EC_LIB_DEVELOPMENT, "unknown zserv command type: %u",
1c50c1c0 501 command);
d62a17ae 502 return unknown.string;
503 }
504 return command_types[command].string;
d6d672aa 505}
7514fb77 506
d62a17ae 507int proto_name2num(const char *s)
7514fb77 508{
d62a17ae 509 unsigned i;
7514fb77 510
d62a17ae 511 for (i = 0; i < array_size(route_types); ++i)
512 if (strcasecmp(s, route_types[i].string) == 0)
513 return route_types[i].type;
514 return -1;
7514fb77 515}
e0ca5fde 516
d62a17ae 517int proto_redistnum(int afi, const char *s)
e0ca5fde 518{
d62a17ae 519 if (!s)
520 return -1;
521
522 if (afi == AFI_IP) {
523 if (strmatch(s, "kernel"))
524 return ZEBRA_ROUTE_KERNEL;
525 else if (strmatch(s, "connected"))
526 return ZEBRA_ROUTE_CONNECT;
527 else if (strmatch(s, "static"))
528 return ZEBRA_ROUTE_STATIC;
529 else if (strmatch(s, "rip"))
530 return ZEBRA_ROUTE_RIP;
531 else if (strmatch(s, "eigrp"))
532 return ZEBRA_ROUTE_EIGRP;
533 else if (strmatch(s, "ospf"))
534 return ZEBRA_ROUTE_OSPF;
535 else if (strmatch(s, "isis"))
536 return ZEBRA_ROUTE_ISIS;
537 else if (strmatch(s, "bgp"))
538 return ZEBRA_ROUTE_BGP;
539 else if (strmatch(s, "table"))
540 return ZEBRA_ROUTE_TABLE;
541 else if (strmatch(s, "vnc"))
542 return ZEBRA_ROUTE_VNC;
543 else if (strmatch(s, "vnc-direct"))
544 return ZEBRA_ROUTE_VNC_DIRECT;
545 else if (strmatch(s, "nhrp"))
546 return ZEBRA_ROUTE_NHRP;
547 else if (strmatch(s, "babel"))
548 return ZEBRA_ROUTE_BABEL;
8a71d93d
DS
549 else if (strmatch(s, "sharp"))
550 return ZEBRA_ROUTE_SHARP;
7c0cbd0e
CF
551 else if (strmatch(s, "openfabric"))
552 return ZEBRA_ROUTE_OPENFABRIC;
d62a17ae 553 }
554 if (afi == AFI_IP6) {
555 if (strmatch(s, "kernel"))
556 return ZEBRA_ROUTE_KERNEL;
557 else if (strmatch(s, "connected"))
558 return ZEBRA_ROUTE_CONNECT;
559 else if (strmatch(s, "static"))
560 return ZEBRA_ROUTE_STATIC;
561 else if (strmatch(s, "ripng"))
562 return ZEBRA_ROUTE_RIPNG;
563 else if (strmatch(s, "ospf6"))
564 return ZEBRA_ROUTE_OSPF6;
565 else if (strmatch(s, "isis"))
566 return ZEBRA_ROUTE_ISIS;
567 else if (strmatch(s, "bgp"))
568 return ZEBRA_ROUTE_BGP;
569 else if (strmatch(s, "table"))
570 return ZEBRA_ROUTE_TABLE;
571 else if (strmatch(s, "vnc"))
572 return ZEBRA_ROUTE_VNC;
573 else if (strmatch(s, "vnc-direct"))
574 return ZEBRA_ROUTE_VNC_DIRECT;
575 else if (strmatch(s, "nhrp"))
576 return ZEBRA_ROUTE_NHRP;
577 else if (strmatch(s, "babel"))
578 return ZEBRA_ROUTE_BABEL;
8a71d93d
DS
579 else if (strmatch(s, "sharp"))
580 return ZEBRA_ROUTE_SHARP;
7c0cbd0e
CF
581 else if (strmatch(s, "openfabric"))
582 return ZEBRA_ROUTE_OPENFABRIC;
d62a17ae 583 }
584 return -1;
e0ca5fde 585}
99a6c6cd 586
46171e25 587void zlog_hexdump(const void *mem, size_t len)
d62a17ae 588{
46171e25
DL
589 char line[64];
590 const uint8_t *src = mem;
591 const uint8_t *end = src + len;
592
593 if (len == 0) {
594 zlog_debug("%016lx: (zero length / no data)", (long)src);
595 return;
596 }
597
598 while (src < end) {
599 struct fbuf fb = {
600 .buf = line,
601 .pos = line,
602 .len = sizeof(line),
603 };
604 const uint8_t *lineend = src + 8;
605 unsigned line_bytes = 0;
606
607 bprintfrr(&fb, "%016lx: ", (long)src);
608
609 while (src < lineend && src < end) {
610 bprintfrr(&fb, "%02x ", *src++);
611 line_bytes++;
612 }
613 if (line_bytes < 8)
614 bprintfrr(&fb, "%*s", (8 - line_bytes) * 3, "");
615
616 src -= line_bytes;
617 while (src < lineend && src < end && fb.pos < fb.buf + fb.len) {
618 uint8_t byte = *src++;
619
620 if (isprint(byte))
621 *fb.pos++ = byte;
622 else
623 *fb.pos++ = '.';
d62a17ae 624 }
46171e25
DL
625
626 zlog_debug("%.*s", (int)(fb.pos - fb.buf), fb.buf);
d62a17ae 627 }
99a6c6cd 628}
1f806fc2 629
d62a17ae 630const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen)
1f806fc2 631{
d62a17ae 632 const char *inbuf = in;
633 char *pos = buf, *end = buf + bufsz;
634 const char *iend = inbuf + inlen;
635
636 memset(buf, 0, bufsz);
637 for (; inbuf < iend; inbuf++) {
638 /* don't write partial escape sequence */
639 if (end - pos < 5)
640 break;
641
642 if (*inbuf == '\n')
643 snprintf(pos, end - pos, "\\n");
644 else if (*inbuf == '\r')
645 snprintf(pos, end - pos, "\\r");
646 else if (*inbuf == '\t')
647 snprintf(pos, end - pos, "\\t");
648 else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127)
649 snprintf(pos, end - pos, "\\x%02hhx", *inbuf);
650 else
651 *pos = *inbuf;
652
653 pos += strlen(pos);
654 }
655 return buf;
1f806fc2 656}