How to printk the first 34 bytes (Ethernet and IP

2019-07-09 23:37发布

  • Operating system: Ubuntu 15.10 Desktop (64-bit)
  • Kernel: 4.3
  • Driver: Wireless
  • Path: /net/mac80211/rx.c

Below is my current implementation (int i = 0; is just after struct sta_info *dsta;, and packet_number++; is just before skb = rx->skb; in the original rx.c file):

#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++;

However, after running dmesg to display the printk outputs, this is what I get:

[  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

These are the outputs that I want instead:

Packet xx: 74 46 a4 08 06 ...
Packet xx: 55 32 3e 45 98 ...
Packet xx: 00 1b 2c 70 55 ...

The data type of rx->skb->data is an unsigned char *, and in order to make it compatible with %x, I had to do those casts and ended up needing to use unsigned long and %lx instead to stop compilation warnings from occurring.

Can you please tell me what I am doing wrong, and how I can printk the first 34 bytes (Ethernet and IP headers) of each packet (EtherType: 0x0806, Protocol: ARP)?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-09 23:58

byte_pointer is a pointer,
(&byte_pointer) is a pointer of pointer, all you need is *byte_pointer.

try following code...

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++;
}

or shorter

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);
    }
}




by the way, a lot of printk message in Tx/Rx flow is a dangerous thing.
if you are no using PC/NB but a embedded system, frequently coming packets bring too many printk handler calling. system may not consume those message, then cause kernel panic because of printk buffer overflow.

查看更多
ら.Afraid
3楼-- · 2019-07-10 00:15

You have to use either %*ph specifier or print_hex_dump() if you want to place it on several lines. In any case this one is not designed for time-critical parts. So, in that case you have to switch to trace points with corresponding trace_printk() calls.

For the slow ones the example is below.

pr_info("Packet %d: %34ph\n", packet_number, rx->skb->data);

or

pr_info("Packet %d:\n", packet_number);
print_hex_dump(KERN_INFO, "Packet:", DUMP_PREFIX_OFFSET, 16, 1, rx->skb->data, 34, false);
查看更多
登录 后发表回答