网络设备驱动调试级别可以在驱动初始化过程中赋初值,并通过ethtool_ops中.get_msglevel获取,通过.set_msglevel进行设置或修改,并通过如netif_msg_drv这样的宏函数来在需要打印调试信息时进行判断,为真时输出对应级别的调试信息,调试级别及可用的判断宏函数如下:
//include/linux/netdevice.h
#define __NETIF_MSG_BIT(bit) ((u32)1 << (bit))
#define __NETIF_MSG(name) __NETIF_MSG_BIT(NETIF_MSG_ ## name ## _BIT)
//驱动程序级别的消息
#define NETIF_MSG_DRV __NETIF_MSG(DRV)
//探测设备时的消息
#define NETIF_MSG_PROBE __NETIF_MSG(PROBE)
//链路状态变化的消息
#define NETIF_MSG_LINK __NETIF_MSG(LINK)
//定时器相关的消息
#define NETIF_MSG_TIMER __NETIF_MSG(TIMER)
//网络接口关闭时的消息
#define NETIF_MSG_IFDOWN __NETIF_MSG(IFDOWN)
//网络接口启动时的消息
#define NETIF_MSG_IFUP __NETIF_MSG(IFUP)
//接收错误的消息
#define NETIF_MSG_RX_ERR __NETIF_MSG(RX_ERR)
//发送错误的消息
#define NETIF_MSG_TX_ERR __NETIF_MSG(TX_ERR)
//发送队列相关的消息
#define NETIF_MSG_TX_QUEUED __NETIF_MSG(TX_QUEUED)
//中断相关的消息
#define NETIF_MSG_INTR __NETIF_MSG(INTR)
//发送完成的消息
#define NETIF_MSG_TX_DONE __NETIF_MSG(TX_DONE)
//接收状态的消息
#define NETIF_MSG_RX_STATUS __NETIF_MSG(RX_STATUS)
//数据包内容的消息
#define NETIF_MSG_PKTDATA __NETIF_MSG(PKTDATA)
//硬件相关的消息
#define NETIF_MSG_HW __NETIF_MSG(HW)
//Wake-on-LAN 相关的消息
#define NETIF_MSG_WOL __NETIF_MSG(WOL)
#define netif_msg_drv(p) ((p)->msg_enable & NETIF_MSG_DRV)
#define netif_msg_probe(p) ((p)->msg_enable & NETIF_MSG_PROBE)
#define netif_msg_link(p) ((p)->msg_enable & NETIF_MSG_LINK)
#define netif_msg_timer(p) ((p)->msg_enable & NETIF_MSG_TIMER)
#define netif_msg_ifdown(p) ((p)->msg_enable & NETIF_MSG_IFDOWN)
#define netif_msg_ifup(p) ((p)->msg_enable & NETIF_MSG_IFUP)
#define netif_msg_rx_err(p) ((p)->msg_enable & NETIF_MSG_RX_ERR)
#define netif_msg_tx_err(p) ((p)->msg_enable & NETIF_MSG_TX_ERR)
#define netif_msg_tx_queued(p) ((p)->msg_enable & NETIF_MSG_TX_QUEUED)
#define netif_msg_intr(p) ((p)->msg_enable & NETIF_MSG_INTR)
#define netif_msg_tx_done(p) ((p)->msg_enable & NETIF_MSG_TX_DONE)
#define netif_msg_rx_status(p) ((p)->msg_enable & NETIF_MSG_RX_STATUS)
#define netif_msg_pktdata(p) ((p)->msg_enable & NETIF_MSG_PKTDATA)
#define netif_msg_hw(p) ((p)->msg_enable & NETIF_MSG_HW)
#define netif_msg_wol(p) ((p)->msg_enable & NETIF_MSG_WOL)
使用示例,如在STMMAC驱动接收处理中:
//drivers/net/ethernet/stmicro/stmmac/stmmac_main.c
static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
{
……
//如调试级别使能了接收调试,则输出接收调试信息
if (netif_msg_rx_status(priv)) {
void *rx_head;
netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
if (priv->extend_desc) {
rx_head = (void *)rx_q->dma_erx;
desc_size = sizeof(struct dma_extended_desc);
} else {
rx_head = (void *)rx_q->dma_rx;
desc_size = sizeof(struct dma_desc);
}
stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true,
rx_q->dma_rx_phy, desc_size);
}
……
}