xref: /freebsd/sys/net/route/route_debug.h (revision ec1b18c7353ad20d2d373e58931aeccfccd5599d)
163f7f392SAlexander V. Chernikov /*-
263f7f392SAlexander V. Chernikov  * Copyright (c) 2021
363f7f392SAlexander V. Chernikov  * 	Alexander V. Chernikov <melifaro@FreeBSD.org>
463f7f392SAlexander V. Chernikov  *
563f7f392SAlexander V. Chernikov  * Redistribution and use in source and binary forms, with or without
663f7f392SAlexander V. Chernikov  * modification, are permitted provided that the following conditions
763f7f392SAlexander V. Chernikov  * are met:
863f7f392SAlexander V. Chernikov  * 1. Redistributions of source code must retain the above copyright
963f7f392SAlexander V. Chernikov  *    notice, this list of conditions and the following disclaimer.
1063f7f392SAlexander V. Chernikov  * 2. Redistributions in binary form must reproduce the above copyright
1163f7f392SAlexander V. Chernikov  *    notice, this list of conditions and the following disclaimer in the
1263f7f392SAlexander V. Chernikov  *    documentation and/or other materials provided with the distribution.
1363f7f392SAlexander V. Chernikov  * 3. Neither the name of the University nor the names of its contributors
1463f7f392SAlexander V. Chernikov  *    may be used to endorse or promote products derived from this software
1563f7f392SAlexander V. Chernikov  *    without specific prior written permission.
1663f7f392SAlexander V. Chernikov  *
1763f7f392SAlexander V. Chernikov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1863f7f392SAlexander V. Chernikov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1963f7f392SAlexander V. Chernikov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2063f7f392SAlexander V. Chernikov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2163f7f392SAlexander V. Chernikov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2263f7f392SAlexander V. Chernikov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2363f7f392SAlexander V. Chernikov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2463f7f392SAlexander V. Chernikov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2563f7f392SAlexander V. Chernikov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2663f7f392SAlexander V. Chernikov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2763f7f392SAlexander V. Chernikov  * SUCH DAMAGE.
2863f7f392SAlexander V. Chernikov  */
2963f7f392SAlexander V. Chernikov 
3063f7f392SAlexander V. Chernikov #ifndef _NET_ROUTE_DEBUG_H_
3163f7f392SAlexander V. Chernikov #define _NET_ROUTE_DEBUG_H_
3263f7f392SAlexander V. Chernikov 
3363f7f392SAlexander V. Chernikov #include <sys/sysctl.h>
3463f7f392SAlexander V. Chernikov #include <sys/syslog.h>
3563f7f392SAlexander V. Chernikov 
3663f7f392SAlexander V. Chernikov /* DEBUG logic */
3763f7f392SAlexander V. Chernikov #if defined(DEBUG_MOD_NAME) && defined(DEBUG_MAX_LEVEL)
3863f7f392SAlexander V. Chernikov 
394bccbf03SAlexander V. Chernikov #ifndef	_DEBUG_SYSCTL_OID
404bccbf03SAlexander V. Chernikov #define	_DEBUG_SYSCTL_OID	_net_route_debug
414bccbf03SAlexander V. Chernikov SYSCTL_DECL(_net_route_debug);
424bccbf03SAlexander V. Chernikov #endif
434bccbf03SAlexander V. Chernikov 
4463f7f392SAlexander V. Chernikov #define DEBUG_VAR_NAME                  	_DEBUG_VAR_NAME(DEBUG_MOD_NAME)
4563f7f392SAlexander V. Chernikov #define _DEBUG_VAR_NAME(a)			_DEBUG_VAR_NAME_INDIRECT(a)
4663f7f392SAlexander V. Chernikov #define _DEBUG_VAR_NAME_INDIRECT(prefix)	prefix##_debug_level
4763f7f392SAlexander V. Chernikov 
4863f7f392SAlexander V. Chernikov #define DEBUG_PREFIX_NAME			_DEBUG_PREFIX_NAME(DEBUG_MOD_NAME)
4963f7f392SAlexander V. Chernikov #define _DEBUG_PREFIX_NAME(n)			__DEBUG_PREFIX_NAME(n)
5063f7f392SAlexander V. Chernikov #define __DEBUG_PREFIX_NAME(n)			#n
5163f7f392SAlexander V. Chernikov 
5263f7f392SAlexander V. Chernikov #define	_DECLARE_DEBUG(_default_level)  	        		\
5363f7f392SAlexander V. Chernikov 	static int DEBUG_VAR_NAME = _default_level;	                \
544bccbf03SAlexander V. Chernikov         SYSCTL_INT(_DEBUG_SYSCTL_OID, OID_AUTO, DEBUG_VAR_NAME,          \
5563f7f392SAlexander V. Chernikov 		CTLFLAG_RW | CTLFLAG_RWTUN,				\
5663f7f392SAlexander V. Chernikov                 &(DEBUG_VAR_NAME), 0, "debuglevel")
5763f7f392SAlexander V. Chernikov 
5863f7f392SAlexander V. Chernikov /* Additional tracing levels not defined by log.h */
5963f7f392SAlexander V. Chernikov #ifndef	LOG_DEBUG2
6063f7f392SAlexander V. Chernikov #define	LOG_DEBUG2	8
6163f7f392SAlexander V. Chernikov #endif
6263f7f392SAlexander V. Chernikov #ifndef LOG_DEBUG3
6363f7f392SAlexander V. Chernikov #define LOG_DEBUG3      9
6463f7f392SAlexander V. Chernikov #endif
6563f7f392SAlexander V. Chernikov 
666fa8ed43SAlexander V. Chernikov /*
676fa8ed43SAlexander V. Chernikov  * Severity usage guidelines:
686fa8ed43SAlexander V. Chernikov  *
696fa8ed43SAlexander V. Chernikov  * LOG_WARNING - subsystem-global errors ("multipath init failed")
706fa8ed43SAlexander V. Chernikov  *
716fa8ed43SAlexander V. Chernikov  * LOG_INFO - subsystem non-transient errors ("Failed to unlink nexhop").
726fa8ed43SAlexander V. Chernikov  *  All logging <= LOG_INFO by default will be written to syslog.
736fa8ed43SAlexander V. Chernikov  *
746fa8ed43SAlexander V. Chernikov  * LOG_DEBUG - subsystem debug. Not-too often events (hash resizes, recoverable failures).
756fa8ed43SAlexander V. Chernikov  *  These are compiled in by default on production. Turning it it should NOT notable affect
766fa8ed43SAlexander V. Chernikov  *  performance
776fa8ed43SAlexander V. Chernikov  * LOG_DEBUG2 - more debug. Per-item level (nhg,nh,route) debug, up to multiple lines per item.
786fa8ed43SAlexander V. Chernikov  *  This is NOT compiled in by default. Turning it on should NOT seriously impact performance
796fa8ed43SAlexander V. Chernikov  * LOG_DEBUG3 - last debug level. Per-item large debug outputs.
806fa8ed43SAlexander V. Chernikov  *  This is NOT compiled in by default. All performance bets are off.
816fa8ed43SAlexander V. Chernikov  */
826fa8ed43SAlexander V. Chernikov 
8363f7f392SAlexander V. Chernikov #define	_output			printf
8463f7f392SAlexander V. Chernikov #define	_DEBUG_PASS_MSG(_l)	(DEBUG_VAR_NAME >= (_l))
8563f7f392SAlexander V. Chernikov 
86*ec1b18c7SMark Johnston #define	IF_DEBUG_LEVEL(_l)	\
87*ec1b18c7SMark Johnston 	if ((DEBUG_MAX_LEVEL >= (_l)) && (__predict_false(DEBUG_VAR_NAME >= (_l))))
88578a99c9SAlexander V. Chernikov 
8963f7f392SAlexander V. Chernikov /*
9063f7f392SAlexander V. Chernikov  * Logging for events specific for particular family and fib
91*ec1b18c7SMark Johnston  * Example: [nhop_neigh] inet.0 find_lle: <message>
9263f7f392SAlexander V. Chernikov  */
93*ec1b18c7SMark Johnston #define	FIB_LOG(_l, _fib, _fam, _fmt, ...)				\
94*ec1b18c7SMark Johnston 	FIB_LOG_##_l(_l, _fib, _fam, _fmt, ## __VA_ARGS__)
95*ec1b18c7SMark Johnston #define	_FIB_LOG(_l, _fib, _fam, _fmt, ...)				\
96*ec1b18c7SMark Johnston 	if (_DEBUG_PASS_MSG(_l)) {					\
97*ec1b18c7SMark Johnston 		_output("[" DEBUG_PREFIX_NAME "] %s.%u %s: " _fmt "\n",	\
98*ec1b18c7SMark Johnston 		    rib_print_family(_fam), _fib, __func__, ##__VA_ARGS__); \
9963f7f392SAlexander V. Chernikov 	}
10063f7f392SAlexander V. Chernikov 
10163f7f392SAlexander V. Chernikov /* Same as FIB_LOG, but uses nhop to get fib and family */
102*ec1b18c7SMark Johnston #define	FIB_NH_LOG(_l, _nh, _fmt, ...)					\
103*ec1b18c7SMark Johnston 	FIB_LOG_##_l(_l, nhop_get_fibnum(_nh), nhop_get_upper_family(_nh), \
104*ec1b18c7SMark Johnston 	    _fmt, ## __VA_ARGS__)
1055d6894bdSAlexander V. Chernikov /* Same as FIB_LOG, but uses rib_head to get fib and family */
106*ec1b18c7SMark Johnston #define	FIB_RH_LOG(_l, _rh, _fmt, ...)					\
107*ec1b18c7SMark Johnston 	FIB_LOG_##_l(_l, (_rh)->rib_fibnum, (_rh)->rib_family, _fmt,	\
108*ec1b18c7SMark Johnston 	    ## __VA_ARGS__)
1095c23343bSAlexander V. Chernikov /* Same as FIB_LOG, but uses nh_control to get fib and family from linked rib */
110*ec1b18c7SMark Johnston #define	FIB_CTL_LOG(_l, _ctl, _fmt, ...)				\
111*ec1b18c7SMark Johnston 	FIB_LOG_##_l(_l, (_ctl)->ctl_rh->rib_fibnum,			\
112*ec1b18c7SMark Johnston 	    (_ctl)->ctl_rh->rib_family, _fmt, ## __VA_ARGS__)
11363f7f392SAlexander V. Chernikov 
11463f7f392SAlexander V. Chernikov /*
11563f7f392SAlexander V. Chernikov  * Generic logging for routing subsystem
116*ec1b18c7SMark Johnston  * Example: [nhop_neigh] nhops_update_neigh: <message>
11763f7f392SAlexander V. Chernikov  */
11863f7f392SAlexander V. Chernikov #define	RT_LOG(_l, _fmt, ...)	RT_LOG_##_l(_l, _fmt, ## __VA_ARGS__)
11928f69107SGleb Smirnoff #define	_RT_LOG(_l, _fmt, ...)	do {					\
12028f69107SGleb Smirnoff 	if (_DEBUG_PASS_MSG(_l))					\
12128f69107SGleb Smirnoff 		_output("[" DEBUG_PREFIX_NAME "] %s: " _fmt "\n",	\
12228f69107SGleb Smirnoff 		     __func__, ##__VA_ARGS__);				\
12328f69107SGleb Smirnoff } while (0)
12463f7f392SAlexander V. Chernikov 
12563f7f392SAlexander V. Chernikov /*
126*ec1b18c7SMark Johnston  * Wrapper logic to avoid compiling high levels of debugging messages for
127*ec1b18c7SMark Johnston  * production systems.
12863f7f392SAlexander V. Chernikov  */
1295d6894bdSAlexander V. Chernikov #if DEBUG_MAX_LEVEL>=LOG_DEBUG3
13063f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_DEBUG3	_FIB_LOG
13163f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_DEBUG3	_RT_LOG
13263f7f392SAlexander V. Chernikov #else
13363f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_DEBUG3(_l, _fib, _fam, _fmt, ...)
13463f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_DEBUG3(_l, _fmt, ...)
13563f7f392SAlexander V. Chernikov #endif
13663f7f392SAlexander V. Chernikov #if DEBUG_MAX_LEVEL>=LOG_DEBUG2
13763f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_DEBUG2	_FIB_LOG
13863f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_DEBUG2	_RT_LOG
13963f7f392SAlexander V. Chernikov #else
14063f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_DEBUG2(_l, _fib, _fam, _fmt, ...)
14163f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_DEBUG2(_l, _fmt, ...)
14263f7f392SAlexander V. Chernikov #endif
14363f7f392SAlexander V. Chernikov #if DEBUG_MAX_LEVEL>=LOG_DEBUG
14463f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_DEBUG	_FIB_LOG
14563f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_DEBUG	_RT_LOG
14663f7f392SAlexander V. Chernikov #else
14763f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_DEBUG(_l, _fib, _fam, _fmt, ...)
14863f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_DEBUG(_l, _fmt, ...)
14963f7f392SAlexander V. Chernikov #endif
15063f7f392SAlexander V. Chernikov #if DEBUG_MAX_LEVEL>=LOG_INFO
15163f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_INFO	_FIB_LOG
15263f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_INFO	_RT_LOG
15363f7f392SAlexander V. Chernikov #else
15463f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_INFO(_l, _fib, _fam, _fmt, ...)
15563f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_INFO(_l, _fmt, ...)
15663f7f392SAlexander V. Chernikov #endif
15763f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_NOTICE	_FIB_LOG
15863f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_ERR         _FIB_LOG
15963f7f392SAlexander V. Chernikov #define	FIB_LOG_LOG_WARNING	_FIB_LOG
16063f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_NOTICE	_RT_LOG
16163f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_ERR          _RT_LOG
16263f7f392SAlexander V. Chernikov #define	RT_LOG_LOG_WARNING	_RT_LOG
16363f7f392SAlexander V. Chernikov 
16463f7f392SAlexander V. Chernikov #endif
16563f7f392SAlexander V. Chernikov 
16663f7f392SAlexander V. Chernikov /* Helpers for fancy-printing various objects */
16763f7f392SAlexander V. Chernikov struct nhop_object;
1685d6894bdSAlexander V. Chernikov struct nhgrp_object;
16963f7f392SAlexander V. Chernikov struct llentry;
17063f7f392SAlexander V. Chernikov struct nhop_neigh;
17127f107e1SAlexander V. Chernikov struct rtentry;
172578a99c9SAlexander V. Chernikov struct ifnet;
17363f7f392SAlexander V. Chernikov 
1745d6894bdSAlexander V. Chernikov #define	NHOP_PRINT_BUFSIZE	48
17563f7f392SAlexander V. Chernikov char *nhop_print_buf(const struct nhop_object *nh, char *buf, size_t bufsize);
1765d6894bdSAlexander V. Chernikov char *nhop_print_buf_any(const struct nhop_object *nh, char *buf, size_t bufsize);
1775d6894bdSAlexander V. Chernikov char *nhgrp_print_buf(const struct nhgrp_object *nhg, char *buf, size_t bufsize);
17863f7f392SAlexander V. Chernikov char *llentry_print_buf(const struct llentry *lle, struct ifnet *ifp, int family, char *buf,
17963f7f392SAlexander V. Chernikov     size_t bufsize);
18063f7f392SAlexander V. Chernikov char *llentry_print_buf_lltable(const struct llentry *lle, char *buf, size_t bufsize);
18163f7f392SAlexander V. Chernikov char *neigh_print_buf(const struct nhop_neigh *nn, char *buf, size_t bufsize);
18227f107e1SAlexander V. Chernikov char *rt_print_buf(const struct rtentry *rt, char *buf, size_t bufsize);
18327f107e1SAlexander V. Chernikov const char *rib_print_cmd(int rib_cmd);
18463f7f392SAlexander V. Chernikov 
18563f7f392SAlexander V. Chernikov #endif
186