xref: /freebsd/sys/net/route/route_debug.h (revision 1f1e2261e341e6ca6862f82261066ef1705f0a7a)
1 /*-
2  * Copyright (c) 2021
3  * 	Alexander V. Chernikov <melifaro@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _NET_ROUTE_DEBUG_H_
33 #define _NET_ROUTE_DEBUG_H_
34 
35 #include <sys/sysctl.h>
36 #include <sys/syslog.h>
37 
38 
39 /* DEBUG logic */
40 #if defined(DEBUG_MOD_NAME) && defined(DEBUG_MAX_LEVEL)
41 
42 #define DEBUG_VAR_NAME                  	_DEBUG_VAR_NAME(DEBUG_MOD_NAME)
43 #define _DEBUG_VAR_NAME(a)			_DEBUG_VAR_NAME_INDIRECT(a)
44 #define _DEBUG_VAR_NAME_INDIRECT(prefix)	prefix##_debug_level
45 
46 #define DEBUG_PREFIX_NAME			_DEBUG_PREFIX_NAME(DEBUG_MOD_NAME)
47 #define _DEBUG_PREFIX_NAME(n)			__DEBUG_PREFIX_NAME(n)
48 #define __DEBUG_PREFIX_NAME(n)			#n
49 
50 #define	_DECLARE_DEBUG(_default_level)  	        		\
51 	SYSCTL_DECL(_net_route_debug);					\
52 	static int DEBUG_VAR_NAME = _default_level;	                \
53         SYSCTL_INT(_net_route_debug, OID_AUTO, DEBUG_VAR_NAME,          \
54 		CTLFLAG_RW | CTLFLAG_RWTUN,				\
55                 &(DEBUG_VAR_NAME), 0, "debuglevel")
56 
57 /* Additional tracing levels not defined by log.h */
58 #ifndef	LOG_DEBUG2
59 #define	LOG_DEBUG2	8
60 #endif
61 #ifndef LOG_DEBUG3
62 #define LOG_DEBUG3      9
63 #endif
64 
65 #define _output			printf
66 #define	_DEBUG_PASS_MSG(_l)	(DEBUG_VAR_NAME >= (_l))
67 
68 /*
69  * Logging for events specific for particular family and fib
70  * Example: [nhop_neigh] inet.0 find_lle: nhop nh#4/inet/vtnet0/10.0.0.1: mapped to lle NULL
71  */
72 #define	FIB_LOG(_l, _fib, _fam, _fmt, ...)	FIB_LOG_##_l(_l, _fib, _fam, _fmt, ## __VA_ARGS__)
73 #define	_FIB_LOG(_l, _fib, _fam, _fmt, ...)	if (_DEBUG_PASS_MSG(_l)) {	\
74 	_output("[" DEBUG_PREFIX_NAME "] %s.%u %s: " _fmt "\n", rib_print_family(_fam), _fib, __func__, ##__VA_ARGS__);	\
75 }
76 
77 /* Same as FIB_LOG, but uses nhop to get fib and family */
78 #define FIB_NH_LOG(_l, _nh, _fmt, ...)  FIB_LOG_##_l(_l, nhop_get_fibnum(_nh), nhop_get_upper_family(_nh), _fmt, ## __VA_ARGS__)
79 /* Same as FIB_LOG, but uses rib_head to get fib and family */
80 #define FIB_RH_LOG(_l, _rh, _fmt, ...)  FIB_LOG_##_l(_l, (_rh)->rib_fibnum, (_rh)->rib_family, _fmt, ## __VA_ARGS__)
81 
82 /*
83  * Generic logging for routing subsystem
84  * Example: [nhop_neigh] nhops_update_neigh: L2 prepend update from lle/inet/valid/vtnet0/10.0.0.157
85  */
86 #define	RT_LOG(_l, _fmt, ...)	RT_LOG_##_l(_l, _fmt, ## __VA_ARGS__)
87 #define	_RT_LOG(_l, _fmt, ...)	if (_DEBUG_PASS_MSG(_l)) {	\
88 	_output("[" DEBUG_PREFIX_NAME "] %s: " _fmt "\n",  __func__, ##__VA_ARGS__);	\
89 }
90 
91 
92 /*
93  * Wrapper logic to avoid compiling high levels of debugging messages for production systems.
94  */
95 #if DEBUG_MAX_LEVEL>=LOG_DEBUG3
96 #define	FIB_LOG_LOG_DEBUG3	_FIB_LOG
97 #define	RT_LOG_LOG_DEBUG3	_RT_LOG
98 #else
99 #define	FIB_LOG_LOG_DEBUG3(_l, _fib, _fam, _fmt, ...)
100 #define	RT_LOG_LOG_DEBUG3(_l, _fmt, ...)
101 #endif
102 #if DEBUG_MAX_LEVEL>=LOG_DEBUG2
103 #define	FIB_LOG_LOG_DEBUG2	_FIB_LOG
104 #define	RT_LOG_LOG_DEBUG2	_RT_LOG
105 #else
106 #define	FIB_LOG_LOG_DEBUG2(_l, _fib, _fam, _fmt, ...)
107 #define	RT_LOG_LOG_DEBUG2(_l, _fmt, ...)
108 #endif
109 #if DEBUG_MAX_LEVEL>=LOG_DEBUG
110 #define	FIB_LOG_LOG_DEBUG	_FIB_LOG
111 #define	RT_LOG_LOG_DEBUG	_RT_LOG
112 #else
113 #define	FIB_LOG_LOG_DEBUG(_l, _fib, _fam, _fmt, ...)
114 #define	RT_LOG_LOG_DEBUG(_l, _fmt, ...)
115 #endif
116 #if DEBUG_MAX_LEVEL>=LOG_INFO
117 #define	FIB_LOG_LOG_INFO	_FIB_LOG
118 #define	RT_LOG_LOG_INFO	_RT_LOG
119 #else
120 #define	FIB_LOG_LOG_INFO(_l, _fib, _fam, _fmt, ...)
121 #define	RT_LOG_LOG_INFO(_l, _fmt, ...)
122 #endif
123 #define	FIB_LOG_LOG_NOTICE	_FIB_LOG
124 #define	FIB_LOG_LOG_ERR         _FIB_LOG
125 #define	FIB_LOG_LOG_WARNING	_FIB_LOG
126 #define	RT_LOG_LOG_NOTICE	_RT_LOG
127 #define	RT_LOG_LOG_ERR          _RT_LOG
128 #define	RT_LOG_LOG_WARNING	_RT_LOG
129 
130 #endif
131 
132 /* Helpers for fancy-printing various objects */
133 struct nhop_object;
134 struct nhgrp_object;
135 struct llentry;
136 struct nhop_neigh;
137 
138 #define	NHOP_PRINT_BUFSIZE	48
139 char *nhop_print_buf(const struct nhop_object *nh, char *buf, size_t bufsize);
140 char *nhop_print_buf_any(const struct nhop_object *nh, char *buf, size_t bufsize);
141 char *nhgrp_print_buf(const struct nhgrp_object *nhg, char *buf, size_t bufsize);
142 char *llentry_print_buf(const struct llentry *lle, struct ifnet *ifp, int family, char *buf,
143     size_t bufsize);
144 char *llentry_print_buf_lltable(const struct llentry *lle, char *buf, size_t bufsize);
145 char *neigh_print_buf(const struct nhop_neigh *nn, char *buf, size_t bufsize);
146 
147 #endif
148