xref: /linux-6.15/include/net/net_debug.h (revision dbefa1f3)
15b87be9eSEric Dumazet /* SPDX-License-Identifier: GPL-2.0 */
25b87be9eSEric Dumazet #ifndef _LINUX_NET_DEBUG_H
35b87be9eSEric Dumazet #define _LINUX_NET_DEBUG_H
45b87be9eSEric Dumazet 
55b87be9eSEric Dumazet #include <linux/bug.h>
65b87be9eSEric Dumazet #include <linux/kern_levels.h>
75b87be9eSEric Dumazet 
85b87be9eSEric Dumazet struct net_device;
95b87be9eSEric Dumazet 
105b87be9eSEric Dumazet __printf(3, 4) __cold
115b87be9eSEric Dumazet void netdev_printk(const char *level, const struct net_device *dev,
125b87be9eSEric Dumazet 		   const char *format, ...);
135b87be9eSEric Dumazet __printf(2, 3) __cold
145b87be9eSEric Dumazet void netdev_emerg(const struct net_device *dev, const char *format, ...);
155b87be9eSEric Dumazet __printf(2, 3) __cold
165b87be9eSEric Dumazet void netdev_alert(const struct net_device *dev, const char *format, ...);
175b87be9eSEric Dumazet __printf(2, 3) __cold
185b87be9eSEric Dumazet void netdev_crit(const struct net_device *dev, const char *format, ...);
195b87be9eSEric Dumazet __printf(2, 3) __cold
205b87be9eSEric Dumazet void netdev_err(const struct net_device *dev, const char *format, ...);
215b87be9eSEric Dumazet __printf(2, 3) __cold
225b87be9eSEric Dumazet void netdev_warn(const struct net_device *dev, const char *format, ...);
235b87be9eSEric Dumazet __printf(2, 3) __cold
245b87be9eSEric Dumazet void netdev_notice(const struct net_device *dev, const char *format, ...);
255b87be9eSEric Dumazet __printf(2, 3) __cold
265b87be9eSEric Dumazet void netdev_info(const struct net_device *dev, const char *format, ...);
275b87be9eSEric Dumazet 
285b87be9eSEric Dumazet #define netdev_level_once(level, dev, fmt, ...)			\
295b87be9eSEric Dumazet do {								\
30*dbefa1f3SMasahiro Yamada 	static bool __section(".data..once") __print_once;	\
315b87be9eSEric Dumazet 								\
325b87be9eSEric Dumazet 	if (!__print_once) {					\
335b87be9eSEric Dumazet 		__print_once = true;				\
345b87be9eSEric Dumazet 		netdev_printk(level, dev, fmt, ##__VA_ARGS__);	\
355b87be9eSEric Dumazet 	}							\
365b87be9eSEric Dumazet } while (0)
375b87be9eSEric Dumazet 
385b87be9eSEric Dumazet #define netdev_emerg_once(dev, fmt, ...) \
395b87be9eSEric Dumazet 	netdev_level_once(KERN_EMERG, dev, fmt, ##__VA_ARGS__)
405b87be9eSEric Dumazet #define netdev_alert_once(dev, fmt, ...) \
415b87be9eSEric Dumazet 	netdev_level_once(KERN_ALERT, dev, fmt, ##__VA_ARGS__)
425b87be9eSEric Dumazet #define netdev_crit_once(dev, fmt, ...) \
435b87be9eSEric Dumazet 	netdev_level_once(KERN_CRIT, dev, fmt, ##__VA_ARGS__)
445b87be9eSEric Dumazet #define netdev_err_once(dev, fmt, ...) \
455b87be9eSEric Dumazet 	netdev_level_once(KERN_ERR, dev, fmt, ##__VA_ARGS__)
465b87be9eSEric Dumazet #define netdev_warn_once(dev, fmt, ...) \
475b87be9eSEric Dumazet 	netdev_level_once(KERN_WARNING, dev, fmt, ##__VA_ARGS__)
485b87be9eSEric Dumazet #define netdev_notice_once(dev, fmt, ...) \
495b87be9eSEric Dumazet 	netdev_level_once(KERN_NOTICE, dev, fmt, ##__VA_ARGS__)
505b87be9eSEric Dumazet #define netdev_info_once(dev, fmt, ...) \
515b87be9eSEric Dumazet 	netdev_level_once(KERN_INFO, dev, fmt, ##__VA_ARGS__)
525b87be9eSEric Dumazet 
535b87be9eSEric Dumazet #if defined(CONFIG_DYNAMIC_DEBUG) || \
545b87be9eSEric Dumazet 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
555b87be9eSEric Dumazet #define netdev_dbg(__dev, format, args...)			\
565b87be9eSEric Dumazet do {								\
575b87be9eSEric Dumazet 	dynamic_netdev_dbg(__dev, format, ##args);		\
585b87be9eSEric Dumazet } while (0)
595b87be9eSEric Dumazet #elif defined(DEBUG)
605b87be9eSEric Dumazet #define netdev_dbg(__dev, format, args...)			\
615b87be9eSEric Dumazet 	netdev_printk(KERN_DEBUG, __dev, format, ##args)
625b87be9eSEric Dumazet #else
635b87be9eSEric Dumazet #define netdev_dbg(__dev, format, args...)			\
645b87be9eSEric Dumazet ({								\
655b87be9eSEric Dumazet 	if (0)							\
665b87be9eSEric Dumazet 		netdev_printk(KERN_DEBUG, __dev, format, ##args); \
675b87be9eSEric Dumazet })
685b87be9eSEric Dumazet #endif
695b87be9eSEric Dumazet 
705b87be9eSEric Dumazet #if defined(VERBOSE_DEBUG)
715b87be9eSEric Dumazet #define netdev_vdbg	netdev_dbg
725b87be9eSEric Dumazet #else
735b87be9eSEric Dumazet 
745b87be9eSEric Dumazet #define netdev_vdbg(dev, format, args...)			\
755b87be9eSEric Dumazet ({								\
765b87be9eSEric Dumazet 	if (0)							\
775b87be9eSEric Dumazet 		netdev_printk(KERN_DEBUG, dev, format, ##args);	\
785b87be9eSEric Dumazet 	0;							\
795b87be9eSEric Dumazet })
805b87be9eSEric Dumazet #endif
815b87be9eSEric Dumazet 
825b87be9eSEric Dumazet /* netif printk helpers, similar to netdev_printk */
835b87be9eSEric Dumazet 
845b87be9eSEric Dumazet #define netif_printk(priv, type, level, dev, fmt, args...)	\
855b87be9eSEric Dumazet do {					  			\
865b87be9eSEric Dumazet 	if (netif_msg_##type(priv))				\
875b87be9eSEric Dumazet 		netdev_printk(level, (dev), fmt, ##args);	\
885b87be9eSEric Dumazet } while (0)
895b87be9eSEric Dumazet 
905b87be9eSEric Dumazet #define netif_level(level, priv, type, dev, fmt, args...)	\
915b87be9eSEric Dumazet do {								\
925b87be9eSEric Dumazet 	if (netif_msg_##type(priv))				\
935b87be9eSEric Dumazet 		netdev_##level(dev, fmt, ##args);		\
945b87be9eSEric Dumazet } while (0)
955b87be9eSEric Dumazet 
965b87be9eSEric Dumazet #define netif_emerg(priv, type, dev, fmt, args...)		\
975b87be9eSEric Dumazet 	netif_level(emerg, priv, type, dev, fmt, ##args)
985b87be9eSEric Dumazet #define netif_alert(priv, type, dev, fmt, args...)		\
995b87be9eSEric Dumazet 	netif_level(alert, priv, type, dev, fmt, ##args)
1005b87be9eSEric Dumazet #define netif_crit(priv, type, dev, fmt, args...)		\
1015b87be9eSEric Dumazet 	netif_level(crit, priv, type, dev, fmt, ##args)
1025b87be9eSEric Dumazet #define netif_err(priv, type, dev, fmt, args...)		\
1035b87be9eSEric Dumazet 	netif_level(err, priv, type, dev, fmt, ##args)
1045b87be9eSEric Dumazet #define netif_warn(priv, type, dev, fmt, args...)		\
1055b87be9eSEric Dumazet 	netif_level(warn, priv, type, dev, fmt, ##args)
1065b87be9eSEric Dumazet #define netif_notice(priv, type, dev, fmt, args...)		\
1075b87be9eSEric Dumazet 	netif_level(notice, priv, type, dev, fmt, ##args)
1085b87be9eSEric Dumazet #define netif_info(priv, type, dev, fmt, args...)		\
1095b87be9eSEric Dumazet 	netif_level(info, priv, type, dev, fmt, ##args)
1105b87be9eSEric Dumazet 
1115b87be9eSEric Dumazet #if defined(CONFIG_DYNAMIC_DEBUG) || \
1125b87be9eSEric Dumazet 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
1135b87be9eSEric Dumazet #define netif_dbg(priv, type, netdev, format, args...)		\
1145b87be9eSEric Dumazet do {								\
1155b87be9eSEric Dumazet 	if (netif_msg_##type(priv))				\
1165b87be9eSEric Dumazet 		dynamic_netdev_dbg(netdev, format, ##args);	\
1175b87be9eSEric Dumazet } while (0)
1185b87be9eSEric Dumazet #elif defined(DEBUG)
1195b87be9eSEric Dumazet #define netif_dbg(priv, type, dev, format, args...)		\
1205b87be9eSEric Dumazet 	netif_printk(priv, type, KERN_DEBUG, dev, format, ##args)
1215b87be9eSEric Dumazet #else
1225b87be9eSEric Dumazet #define netif_dbg(priv, type, dev, format, args...)			\
1235b87be9eSEric Dumazet ({									\
1245b87be9eSEric Dumazet 	if (0)								\
1255b87be9eSEric Dumazet 		netif_printk(priv, type, KERN_DEBUG, dev, format, ##args); \
1265b87be9eSEric Dumazet 	0;								\
1275b87be9eSEric Dumazet })
1285b87be9eSEric Dumazet #endif
1295b87be9eSEric Dumazet 
1305b87be9eSEric Dumazet /* if @cond then downgrade to debug, else print at @level */
1315b87be9eSEric Dumazet #define netif_cond_dbg(priv, type, netdev, cond, level, fmt, args...)     \
1325b87be9eSEric Dumazet 	do {                                                              \
1335b87be9eSEric Dumazet 		if (cond)                                                 \
1345b87be9eSEric Dumazet 			netif_dbg(priv, type, netdev, fmt, ##args);       \
1355b87be9eSEric Dumazet 		else                                                      \
1365b87be9eSEric Dumazet 			netif_ ## level(priv, type, netdev, fmt, ##args); \
1375b87be9eSEric Dumazet 	} while (0)
1385b87be9eSEric Dumazet 
1395b87be9eSEric Dumazet #if defined(VERBOSE_DEBUG)
1405b87be9eSEric Dumazet #define netif_vdbg	netif_dbg
1415b87be9eSEric Dumazet #else
1425b87be9eSEric Dumazet #define netif_vdbg(priv, type, dev, format, args...)		\
1435b87be9eSEric Dumazet ({								\
1445b87be9eSEric Dumazet 	if (0)							\
1455b87be9eSEric Dumazet 		netif_printk(priv, type, KERN_DEBUG, dev, format, ##args); \
1465b87be9eSEric Dumazet 	0;							\
1475b87be9eSEric Dumazet })
1485b87be9eSEric Dumazet #endif
1495b87be9eSEric Dumazet 
1505b87be9eSEric Dumazet 
151d268c1f5SEric Dumazet #if defined(CONFIG_DEBUG_NET)
152d268c1f5SEric Dumazet #define DEBUG_NET_WARN_ON_ONCE(cond) ((void)WARN_ON_ONCE(cond))
153d268c1f5SEric Dumazet #define DEBUG_NET_WARN_ONCE(cond, format...) ((void)WARN_ONCE(cond, format))
154d268c1f5SEric Dumazet #else
155d268c1f5SEric Dumazet #define DEBUG_NET_WARN_ON_ONCE(cond) BUILD_BUG_ON_INVALID(cond)
156d268c1f5SEric Dumazet #define DEBUG_NET_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond)
1575b87be9eSEric Dumazet #endif
158 
159 #endif	/* _LINUX_NET_DEBUG_H */
160