]> git.proxmox.com Git - mirror_frr.git/blame - lib/log.c
*: Rename quagga_timestamp with frr_timestamp
[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. */
e36f61b5 67size_t frr_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 memory_oom(size_t size, const char *name)
3b4cd783 315{
0bdeb5e5
DL
316 zlog(LOG_CRIT,
317 "out of memory: failed to allocate %zu bytes for %s object",
318 size, name);
319 zlog_backtrace(LOG_CRIT);
320 log_memstats(stderr, "log");
3b4cd783
DL
321 abort();
322}
6b0655a2 323
ca359769 324/* Wrapper around strerror to handle case where it returns NULL. */
d62a17ae 325const char *safe_strerror(int errnum)
ca359769 326{
d62a17ae 327 const char *s = strerror(errnum);
328 return (s != NULL) ? s : "Unknown error";
ca359769 329}
f52d13cb 330
d6d672aa
PJ
331#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
332static const struct zebra_desc_table command_types[] = {
d62a17ae 333 DESC_ENTRY(ZEBRA_INTERFACE_ADD),
334 DESC_ENTRY(ZEBRA_INTERFACE_DELETE),
335 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD),
336 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE),
337 DESC_ENTRY(ZEBRA_INTERFACE_UP),
338 DESC_ENTRY(ZEBRA_INTERFACE_DOWN),
e0ae31b8 339 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER),
0e51b4a3
RW
340 DESC_ENTRY(ZEBRA_ROUTE_ADD),
341 DESC_ENTRY(ZEBRA_ROUTE_DELETE),
7ea7b86e 342 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER),
d62a17ae 343 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD),
344 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE),
345 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
346 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
347 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD),
348 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE),
349 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE),
350 DESC_ENTRY(ZEBRA_HELLO),
4cf8bb32 351 DESC_ENTRY(ZEBRA_CAPABILITIES),
d62a17ae 352 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER),
353 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER),
354 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE),
355 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD),
356 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE),
357 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE),
d62a17ae 358 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER),
359 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER),
360 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE),
361 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY),
74489921
RW
362 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD),
363 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL),
d62a17ae 364 DESC_ENTRY(ZEBRA_VRF_UNREGISTER),
365 DESC_ENTRY(ZEBRA_VRF_ADD),
366 DESC_ENTRY(ZEBRA_VRF_DELETE),
c83c5e44 367 DESC_ENTRY(ZEBRA_VRF_LABEL),
d62a17ae 368 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE),
369 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER),
4cf8bb32 370 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER),
d62a17ae 371 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV),
372 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV),
373 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB),
374 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS),
375 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD),
376 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE),
ea6b290b 377 DESC_ENTRY(ZEBRA_MPLS_LABELS_REPLACE),
31f937fb
SM
378 DESC_ENTRY(ZEBRA_SR_POLICY_SET),
379 DESC_ENTRY(ZEBRA_SR_POLICY_DELETE),
380 DESC_ENTRY(ZEBRA_SR_POLICY_NOTIFY_STATUS),
d62a17ae 381 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS),
382 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT),
f533be73 383 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC),
d62a17ae 384 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK),
385 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK),
4cf8bb32
DS
386 DESC_ENTRY(ZEBRA_FEC_REGISTER),
387 DESC_ENTRY(ZEBRA_FEC_UNREGISTER),
388 DESC_ENTRY(ZEBRA_FEC_UPDATE),
d62a17ae 389 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI),
1a98c087 390 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW),
fc08a52f 391 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP),
31310b25 392 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET),
50f74cf1 393 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD),
394 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL),
ce5160c0
AK
395 DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_ADD),
396 DESC_ENTRY(ZEBRA_REMOTE_ES_VTEP_DEL),
397 DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_ADD),
398 DESC_ENTRY(ZEBRA_LOCAL_ES_EVI_DEL),
d62a17ae 399 DESC_ENTRY(ZEBRA_VNI_ADD),
400 DESC_ENTRY(ZEBRA_VNI_DEL),
b7cfce93
MK
401 DESC_ENTRY(ZEBRA_L3VNI_ADD),
402 DESC_ENTRY(ZEBRA_L3VNI_DEL),
d62a17ae 403 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD),
404 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL),
405 DESC_ENTRY(ZEBRA_MACIP_ADD),
406 DESC_ENTRY(ZEBRA_MACIP_DEL),
31310b25
MK
407 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD),
408 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL),
d62a17ae 409 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD),
410 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL),
3950b52c 411 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION),
6833ae01 412 DESC_ENTRY(ZEBRA_PW_ADD),
413 DESC_ENTRY(ZEBRA_PW_DELETE),
414 DESC_ENTRY(ZEBRA_PW_SET),
415 DESC_ENTRY(ZEBRA_PW_UNSET),
416 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE),
e16abbb3
DS
417 DESC_ENTRY(ZEBRA_RULE_ADD),
418 DESC_ENTRY(ZEBRA_RULE_DELETE),
419 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER),
75fb51c1
PG
420 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT),
421 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK),
422 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK),
d59c13af
PG
423 DESC_ENTRY(ZEBRA_IPSET_CREATE),
424 DESC_ENTRY(ZEBRA_IPSET_DESTROY),
425 DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD),
426 DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE),
4cf8bb32
DS
427 DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER),
428 DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER),
429 DESC_ENTRY(ZEBRA_IPTABLE_ADD),
430 DESC_ENTRY(ZEBRA_IPTABLE_DELETE),
431 DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER),
fbac9605 432 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL),
4ab3321f
AK
433 DESC_ENTRY(ZEBRA_VXLAN_SG_ADD),
434 DESC_ENTRY(ZEBRA_VXLAN_SG_DEL),
ecbbc3a7 435 DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY),
d1a3e8df
AK
436 DESC_ENTRY(ZEBRA_MLAG_PROCESS_UP),
437 DESC_ENTRY(ZEBRA_MLAG_PROCESS_DOWN),
438 DESC_ENTRY(ZEBRA_MLAG_CLIENT_REGISTER),
439 DESC_ENTRY(ZEBRA_MLAG_CLIENT_UNREGISTER),
440 DESC_ENTRY(ZEBRA_MLAG_FORWARD_MSG),
9ab0b2a3 441 DESC_ENTRY(ZEBRA_ERROR),
ff491140
MS
442 DESC_ENTRY(ZEBRA_CLIENT_CAPABILITIES),
443 DESC_ENTRY(ZEBRA_OPAQUE_MESSAGE),
444 DESC_ENTRY(ZEBRA_OPAQUE_REGISTER),
d68e74b4 445 DESC_ENTRY(ZEBRA_OPAQUE_UNREGISTER),
b36bedd2
SW
446 DESC_ENTRY(ZEBRA_NEIGH_DISCOVER),
447 DESC_ENTRY(ZEBRA_NHG_ADD),
391c7a3b 448 DESC_ENTRY(ZEBRA_NHG_DEL),
77b38a4a 449 DESC_ENTRY(ZEBRA_NHG_NOTIFY_OWNER),
581e797e 450 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_REQUEST),
7bfa7d02
AK
451 DESC_ENTRY(ZEBRA_CLIENT_CLOSE_NOTIFY),
452 DESC_ENTRY(ZEBRA_EVPN_REMOTE_NH_ADD),
fda64ab4
PG
453 DESC_ENTRY(ZEBRA_EVPN_REMOTE_NH_DEL),
454 DESC_ENTRY(ZEBRA_NHRP_NEIGH_ADDED),
455 DESC_ENTRY(ZEBRA_NHRP_NEIGH_REMOVED),
7723e8d3
PG
456 DESC_ENTRY(ZEBRA_NHRP_NEIGH_GET),
457 DESC_ENTRY(ZEBRA_NHRP_NEIGH_REGISTER),
05657ec2 458 DESC_ENTRY(ZEBRA_NHRP_NEIGH_UNREGISTER),
d603c077
PG
459 DESC_ENTRY(ZEBRA_NEIGH_IP_ADD),
460 DESC_ENTRY(ZEBRA_NEIGH_IP_DEL),
632d8306
PG
461 DESC_ENTRY(ZEBRA_CONFIGURE_ARP),
462 DESC_ENTRY(ZEBRA_GRE_GET),
463 DESC_ENTRY(ZEBRA_GRE_UPDATE),
464 DESC_ENTRY(ZEBRA_GRE_SOURCE_SET)};
d6d672aa
PJ
465#undef DESC_ENTRY
466
d62a17ae 467static const struct zebra_desc_table unknown = {0, "unknown", '?'};
d6d672aa 468
d7c0a89a 469static const struct zebra_desc_table *zroute_lookup(unsigned int zroute)
f52d13cb 470{
d7c0a89a 471 unsigned int i;
d62a17ae 472
473 if (zroute >= array_size(route_types)) {
450971aa 474 flog_err(EC_LIB_DEVELOPMENT, "unknown zebra route type: %u",
1c50c1c0 475 zroute);
d62a17ae 476 return &unknown;
477 }
478 if (zroute == route_types[zroute].type)
479 return &route_types[zroute];
480 for (i = 0; i < array_size(route_types); i++) {
481 if (zroute == route_types[i].type) {
482 zlog_warn(
3efd0893 483 "internal error: route type table out of order while searching for %u, please notify developers",
d62a17ae 484 zroute);
485 return &route_types[i];
486 }
487 }
450971aa 488 flog_err(EC_LIB_DEVELOPMENT,
1c50c1c0 489 "internal error: cannot find route type %u in table!", zroute);
d62a17ae 490 return &unknown;
f52d13cb 491}
492
d7c0a89a 493const char *zebra_route_string(unsigned int zroute)
f52d13cb 494{
d62a17ae 495 return zroute_lookup(zroute)->string;
f52d13cb 496}
497
d7c0a89a 498char zebra_route_char(unsigned int zroute)
f52d13cb 499{
d62a17ae 500 return zroute_lookup(zroute)->chr;
f52d13cb 501}
d6d672aa 502
d62a17ae 503const char *zserv_command_string(unsigned int command)
d6d672aa 504{
d62a17ae 505 if (command >= array_size(command_types)) {
450971aa 506 flog_err(EC_LIB_DEVELOPMENT, "unknown zserv command type: %u",
1c50c1c0 507 command);
d62a17ae 508 return unknown.string;
509 }
510 return command_types[command].string;
d6d672aa 511}
7514fb77 512
d62a17ae 513int proto_name2num(const char *s)
7514fb77 514{
d62a17ae 515 unsigned i;
7514fb77 516
d62a17ae 517 for (i = 0; i < array_size(route_types); ++i)
518 if (strcasecmp(s, route_types[i].string) == 0)
519 return route_types[i].type;
520 return -1;
7514fb77 521}
e0ca5fde 522
d62a17ae 523int proto_redistnum(int afi, const char *s)
e0ca5fde 524{
d62a17ae 525 if (!s)
526 return -1;
527
528 if (afi == AFI_IP) {
529 if (strmatch(s, "kernel"))
530 return ZEBRA_ROUTE_KERNEL;
531 else if (strmatch(s, "connected"))
532 return ZEBRA_ROUTE_CONNECT;
533 else if (strmatch(s, "static"))
534 return ZEBRA_ROUTE_STATIC;
535 else if (strmatch(s, "rip"))
536 return ZEBRA_ROUTE_RIP;
537 else if (strmatch(s, "eigrp"))
538 return ZEBRA_ROUTE_EIGRP;
539 else if (strmatch(s, "ospf"))
540 return ZEBRA_ROUTE_OSPF;
541 else if (strmatch(s, "isis"))
542 return ZEBRA_ROUTE_ISIS;
543 else if (strmatch(s, "bgp"))
544 return ZEBRA_ROUTE_BGP;
545 else if (strmatch(s, "table"))
546 return ZEBRA_ROUTE_TABLE;
547 else if (strmatch(s, "vnc"))
548 return ZEBRA_ROUTE_VNC;
549 else if (strmatch(s, "vnc-direct"))
550 return ZEBRA_ROUTE_VNC_DIRECT;
551 else if (strmatch(s, "nhrp"))
552 return ZEBRA_ROUTE_NHRP;
553 else if (strmatch(s, "babel"))
554 return ZEBRA_ROUTE_BABEL;
8a71d93d
DS
555 else if (strmatch(s, "sharp"))
556 return ZEBRA_ROUTE_SHARP;
7c0cbd0e
CF
557 else if (strmatch(s, "openfabric"))
558 return ZEBRA_ROUTE_OPENFABRIC;
d62a17ae 559 }
560 if (afi == AFI_IP6) {
561 if (strmatch(s, "kernel"))
562 return ZEBRA_ROUTE_KERNEL;
563 else if (strmatch(s, "connected"))
564 return ZEBRA_ROUTE_CONNECT;
565 else if (strmatch(s, "static"))
566 return ZEBRA_ROUTE_STATIC;
567 else if (strmatch(s, "ripng"))
568 return ZEBRA_ROUTE_RIPNG;
569 else if (strmatch(s, "ospf6"))
570 return ZEBRA_ROUTE_OSPF6;
571 else if (strmatch(s, "isis"))
572 return ZEBRA_ROUTE_ISIS;
573 else if (strmatch(s, "bgp"))
574 return ZEBRA_ROUTE_BGP;
575 else if (strmatch(s, "table"))
576 return ZEBRA_ROUTE_TABLE;
577 else if (strmatch(s, "vnc"))
578 return ZEBRA_ROUTE_VNC;
579 else if (strmatch(s, "vnc-direct"))
580 return ZEBRA_ROUTE_VNC_DIRECT;
581 else if (strmatch(s, "nhrp"))
582 return ZEBRA_ROUTE_NHRP;
583 else if (strmatch(s, "babel"))
584 return ZEBRA_ROUTE_BABEL;
8a71d93d
DS
585 else if (strmatch(s, "sharp"))
586 return ZEBRA_ROUTE_SHARP;
7c0cbd0e
CF
587 else if (strmatch(s, "openfabric"))
588 return ZEBRA_ROUTE_OPENFABRIC;
d62a17ae 589 }
590 return -1;
e0ca5fde 591}
99a6c6cd 592
46171e25 593void zlog_hexdump(const void *mem, size_t len)
d62a17ae 594{
46171e25
DL
595 char line[64];
596 const uint8_t *src = mem;
597 const uint8_t *end = src + len;
598
599 if (len == 0) {
600 zlog_debug("%016lx: (zero length / no data)", (long)src);
601 return;
602 }
603
604 while (src < end) {
605 struct fbuf fb = {
606 .buf = line,
607 .pos = line,
608 .len = sizeof(line),
609 };
610 const uint8_t *lineend = src + 8;
611 unsigned line_bytes = 0;
612
613 bprintfrr(&fb, "%016lx: ", (long)src);
614
615 while (src < lineend && src < end) {
616 bprintfrr(&fb, "%02x ", *src++);
617 line_bytes++;
618 }
619 if (line_bytes < 8)
620 bprintfrr(&fb, "%*s", (8 - line_bytes) * 3, "");
621
622 src -= line_bytes;
623 while (src < lineend && src < end && fb.pos < fb.buf + fb.len) {
624 uint8_t byte = *src++;
625
626 if (isprint(byte))
627 *fb.pos++ = byte;
628 else
629 *fb.pos++ = '.';
d62a17ae 630 }
46171e25
DL
631
632 zlog_debug("%.*s", (int)(fb.pos - fb.buf), fb.buf);
d62a17ae 633 }
99a6c6cd 634}
1f806fc2 635
d62a17ae 636const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen)
1f806fc2 637{
d62a17ae 638 const char *inbuf = in;
639 char *pos = buf, *end = buf + bufsz;
640 const char *iend = inbuf + inlen;
641
642 memset(buf, 0, bufsz);
643 for (; inbuf < iend; inbuf++) {
644 /* don't write partial escape sequence */
645 if (end - pos < 5)
646 break;
647
648 if (*inbuf == '\n')
649 snprintf(pos, end - pos, "\\n");
650 else if (*inbuf == '\r')
651 snprintf(pos, end - pos, "\\r");
652 else if (*inbuf == '\t')
653 snprintf(pos, end - pos, "\\t");
654 else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127)
655 snprintf(pos, end - pos, "\\x%02hhx", *inbuf);
656 else
657 *pos = *inbuf;
658
659 pos += strlen(pos);
660 }
661 return buf;
1f806fc2 662}