下面是我当前的实现(在原始 rx.c 文件中,
int i = 0;
位于struct sta_info *dsta;
之后,packet_number++;
位于skb = rx->skb;
之前):
#include <linux/ip.h>
#include <linux/byteorder/generic.h>
unsigned int packet_number = 1;
(etc.)
static void ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
{
(etc.)
int i = 0;
unsigned int *byte_pointer;
unsigned long byte_contents = 0;
if (ntohs(ehdr->h_proto) == 0x0806) {
printk("Packet %d:", packet_number);
for (i = 0; i < 34; i++) {
byte_pointer = (unsigned int *) (rx->skb->data + i);
byte_contents = (unsigned long) (&byte_pointer);
printk(" %lx", byte_contents);
}
}
printk("\n");
packet_number++;
但是,运行
dmesg
显示 printk()
输出后,这就是我得到的:
[ 103.373847] Packet 6: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 103.375720] Packet 7: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 103.572086] Packet 8: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 103.913790] Packet 12: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 108.940615] Packet 20: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[ 114.221127] Packet 28: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[ 143.390755] Packet 1354: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 144.394113] Packet 1401: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 194.730646] Packet 2041: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[ 208.784474] Packet 2059: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 247.409613] Packet 2130: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[ 272.241113] Packet 2171: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 272.241957] Packet 2172: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 272.648982] Packet 2174: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[ 273.877091] Packet 2180: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[ 273.878104] Packet 2181: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
这些是我想要的输出:
Packet xx: 74 46 a4 08 06 ...
Packet xx: 55 32 3e 45 98 ...
Packet xx: 00 1b 2c 70 55 ...
rx->skb->data
的数据类型是unsigned char *,为了使其与%x
兼容,我必须进行这些转换,最终需要使用unsigned long和%lx
来停止编译发生警告。
您能否告诉我我做错了什么,以及如何
printk()
每个数据包的前 34 个字节(以太网和 IP 标头)(以太网类型:0x0806,协议:ARP)?
如果要将其放在多行上,则必须使用
%*ph
说明符或 print_hex_dump()
。无论如何,这并不是为时间紧迫的零件而设计的。因此,在这种情况下,您必须切换到具有相应 trace_printk()
调用的跟踪点。
对于慢速的,示例如下。
pr_info("Packet %d: %34ph\n", packet_number, rx->skb->data);
或
pr_info("Packet %d:\n", packet_number);
print_hex_dump(KERN_INFO, "Packet:", DUMP_PREFIX_OFFSET, 16, 1, rx->skb->data, 34, false);
byte_pointer 是一个指针,
(&byte_pointer)是一个指针的指针,你需要的只是*byte_pointer。
尝试以下代码...
static void ieee80211_deliver_skb( struct ieee80211_rx_data *rx )
{
(etc.)
int i = 0;
unsigned char *byte_pointer;
unsigned char byte_contents = 0;
if (ntohs(ehdr->h_proto) == 0x0806) {
printk("Packet %d:", packet_number);
for (i = 0; i < 34; i++) {
byte_pointer = (unsigned char *) (rx->skb->data + i);
byte_contents = *byte_pointer;
printk(" %02x", byte_contents);
}
}
printk("\n");
packet_number++;
}
或更短
u8 *p;
if (ntohs(ehdr->h_proto) == 0x0806) {
printk("Packet %d:", packet_number);
for (i = 0; i < 34; i++) {
p = (u8 *)(rx->skb->data + i);
printk(" %02x", *p);
}
}
顺便说一下,Tx/Rx 流中的大量 printk 消息是一件危险的事情。
如果您使用的不是 PC/NB,而是嵌入式系统,频繁传入的数据包会带来过多的 printk 处理程序调用。系统可能不会消耗这些消息,然后由于 printk 缓冲区溢出而导致内核恐慌。