1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/*-
* Copyright (c) 2021
* Alexander V. Chernikov <melifaro@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _NET_ROUTE_DEBUG_H_
#define _NET_ROUTE_DEBUG_H_
#include <sys/sysctl.h>
#include <sys/syslog.h>
/* DEBUG logic */
#if defined(DEBUG_MOD_NAME) && defined(DEBUG_MAX_LEVEL)
#ifndef _DEBUG_SYSCTL_OID
#define _DEBUG_SYSCTL_OID _net_route_debug
SYSCTL_DECL(_net_route_debug);
#endif
#define DEBUG_VAR_NAME _DEBUG_VAR_NAME(DEBUG_MOD_NAME)
#define _DEBUG_VAR_NAME(a) _DEBUG_VAR_NAME_INDIRECT(a)
#define _DEBUG_VAR_NAME_INDIRECT(prefix) prefix##_debug_level
#define DEBUG_PREFIX_NAME _DEBUG_PREFIX_NAME(DEBUG_MOD_NAME)
#define _DEBUG_PREFIX_NAME(n) __DEBUG_PREFIX_NAME(n)
#define __DEBUG_PREFIX_NAME(n) #n
#define _DECLARE_DEBUG(_default_level) \
static int DEBUG_VAR_NAME = _default_level; \
SYSCTL_INT(_DEBUG_SYSCTL_OID, OID_AUTO, DEBUG_VAR_NAME, \
CTLFLAG_RW | CTLFLAG_RWTUN, \
&(DEBUG_VAR_NAME), 0, "debuglevel")
/* Additional tracing levels not defined by log.h */
#ifndef LOG_DEBUG2
#define LOG_DEBUG2 8
#endif
#ifndef LOG_DEBUG3
#define LOG_DEBUG3 9
#endif
/*
* Severity usage guidelines:
*
* LOG_WARNING - subsystem-global errors ("multipath init failed")
*
* LOG_INFO - subsystem non-transient errors ("Failed to unlink nexhop").
* All logging <= LOG_INFO by default will be written to syslog.
*
* LOG_DEBUG - subsystem debug. Not-too often events (hash resizes, recoverable failures).
* These are compiled in by default on production. Turning it it should NOT notable affect
* performance
* LOG_DEBUG2 - more debug. Per-item level (nhg,nh,route) debug, up to multiple lines per item.
* This is NOT compiled in by default. Turning it on should NOT seriously impact performance
* LOG_DEBUG3 - last debug level. Per-item large debug outputs.
* This is NOT compiled in by default. All performance bets are off.
*/
#define _output printf
#define _DEBUG_PASS_MSG(_l) (DEBUG_VAR_NAME >= (_l))
#define IF_DEBUG_LEVEL(_l) \
if ((DEBUG_MAX_LEVEL >= (_l)) && (__predict_false(DEBUG_VAR_NAME >= (_l))))
/*
* Logging for events specific for particular family and fib
* Example: [nhop_neigh] inet.0 find_lle: <message>
*/
#define FIB_LOG(_l, _fib, _fam, _fmt, ...) \
FIB_LOG_##_l(_l, _fib, _fam, _fmt, ## __VA_ARGS__)
#define _FIB_LOG(_l, _fib, _fam, _fmt, ...) \
if (_DEBUG_PASS_MSG(_l)) { \
_output("[" DEBUG_PREFIX_NAME "] %s.%u %s: " _fmt "\n", \
rib_print_family(_fam), _fib, __func__, ##__VA_ARGS__); \
}
/* Same as FIB_LOG, but uses nhop to get fib and family */
#define FIB_NH_LOG(_l, _nh, _fmt, ...) \
FIB_LOG_##_l(_l, nhop_get_fibnum(_nh), nhop_get_upper_family(_nh), \
_fmt, ## __VA_ARGS__)
/* Same as FIB_LOG, but uses rib_head to get fib and family */
#define FIB_RH_LOG(_l, _rh, _fmt, ...) \
FIB_LOG_##_l(_l, (_rh)->rib_fibnum, (_rh)->rib_family, _fmt, \
## __VA_ARGS__)
/* Same as FIB_LOG, but uses nh_control to get fib and family from linked rib */
#define FIB_CTL_LOG(_l, _ctl, _fmt, ...) \
FIB_LOG_##_l(_l, (_ctl)->ctl_rh->rib_fibnum, \
(_ctl)->ctl_rh->rib_family, _fmt, ## __VA_ARGS__)
/*
* Generic logging for routing subsystem
* Example: [nhop_neigh] nhops_update_neigh: <message>
*/
#define RT_LOG(_l, _fmt, ...) RT_LOG_##_l(_l, _fmt, ## __VA_ARGS__)
#define _RT_LOG(_l, _fmt, ...) if (_DEBUG_PASS_MSG(_l)) { \
_output("[" DEBUG_PREFIX_NAME "] %s: " _fmt "\n", __func__, ##__VA_ARGS__); \
}
/*
* Wrapper logic to avoid compiling high levels of debugging messages for
* production systems.
*/
#if DEBUG_MAX_LEVEL>=LOG_DEBUG3
#define FIB_LOG_LOG_DEBUG3 _FIB_LOG
#define RT_LOG_LOG_DEBUG3 _RT_LOG
#else
#define FIB_LOG_LOG_DEBUG3(_l, _fib, _fam, _fmt, ...)
#define RT_LOG_LOG_DEBUG3(_l, _fmt, ...)
#endif
#if DEBUG_MAX_LEVEL>=LOG_DEBUG2
#define FIB_LOG_LOG_DEBUG2 _FIB_LOG
#define RT_LOG_LOG_DEBUG2 _RT_LOG
#else
#define FIB_LOG_LOG_DEBUG2(_l, _fib, _fam, _fmt, ...)
#define RT_LOG_LOG_DEBUG2(_l, _fmt, ...)
#endif
#if DEBUG_MAX_LEVEL>=LOG_DEBUG
#define FIB_LOG_LOG_DEBUG _FIB_LOG
#define RT_LOG_LOG_DEBUG _RT_LOG
#else
#define FIB_LOG_LOG_DEBUG(_l, _fib, _fam, _fmt, ...)
#define RT_LOG_LOG_DEBUG(_l, _fmt, ...)
#endif
#if DEBUG_MAX_LEVEL>=LOG_INFO
#define FIB_LOG_LOG_INFO _FIB_LOG
#define RT_LOG_LOG_INFO _RT_LOG
#else
#define FIB_LOG_LOG_INFO(_l, _fib, _fam, _fmt, ...)
#define RT_LOG_LOG_INFO(_l, _fmt, ...)
#endif
#define FIB_LOG_LOG_NOTICE _FIB_LOG
#define FIB_LOG_LOG_ERR _FIB_LOG
#define FIB_LOG_LOG_WARNING _FIB_LOG
#define RT_LOG_LOG_NOTICE _RT_LOG
#define RT_LOG_LOG_ERR _RT_LOG
#define RT_LOG_LOG_WARNING _RT_LOG
#endif
/* Helpers for fancy-printing various objects */
struct nhop_object;
struct nhgrp_object;
struct llentry;
struct nhop_neigh;
struct rtentry;
struct ifnet;
#define NHOP_PRINT_BUFSIZE 48
char *nhop_print_buf(const struct nhop_object *nh, char *buf, size_t bufsize);
char *nhop_print_buf_any(const struct nhop_object *nh, char *buf, size_t bufsize);
char *nhgrp_print_buf(const struct nhgrp_object *nhg, char *buf, size_t bufsize);
char *llentry_print_buf(const struct llentry *lle, struct ifnet *ifp, int family, char *buf,
size_t bufsize);
char *llentry_print_buf_lltable(const struct llentry *lle, char *buf, size_t bufsize);
char *neigh_print_buf(const struct nhop_neigh *nn, char *buf, size_t bufsize);
char *rt_print_buf(const struct rtentry *rt, char *buf, size_t bufsize);
const char *rib_print_cmd(int rib_cmd);
#endif
|