如何从skb提取整个数据包

如何从skb提取整个数据包

本文介绍了如何从skb提取整个数据包,包括以太网头,ip和tcp以及设备驱动程序的轮询方法中的有效负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在realtek的r8169驱动程序中确实如此

in r8169 driver from realtek it does

    rx_buf = page_address(tp->Rx_databuff[entry]);
    dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
    prefetch(rx_buf);
    skb_copy_to_linear_data(skb, rx_buf, pkt_size);<----//Do I get packet at this????
    skb->tail += pkt_size;
    skb->len = pkt_size;
    dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
    //csum...
    skb->protocol = eth_type_trans(skb, dev);
    napi_gro_receive(&tp->napi, skb);

这是从驱动程序轮询调用的rtl_rx函数内部.我想在上面的代码中知道如何在之后的哪一行从skb中提取整个数据包.

this is inside rtl_rx function called from poll of driver. I like to know in above code how can I extract the entire packet from skb at which line afterwards.

我假设在这一行

             skb_copy_to_linear_data(skb, rx_buf, pkt_size);

我应该有一个数据包,但是想知道我创建kmalloc对象的正确方法

I should have a packet, but like to know the correct way I can create a kmalloc obect like

         void   *packet=   kmalloc(....sizeof(struct ethhdr)+sizeof(struct iphdr)+sizeof(tcphdr))

并从void * packet中读取以太网ip和tcp标头

and read ethernet ip and tcp headers from void *packet

如何实现

或者我应该简单地做skb_netword_header,skb_tcp_header等...,以便在上面的行中填充skb后从skb中提取标头和有效载荷,

Or should I simple do skb_netword_header, skb_tcp_header, etc... to extract the headers and payload from skb after it get populated in above lines,

或者我可以简单地投射为

or can I simply cast as

              rx_buf = page_address(tp->Rx_databuff[entry]);
              struct ether_header ethhdr_of_packet=(struct eher_header *) rx_buf;

应该行吗?

推荐答案

突出显示的行(带有 skb_copy_to_linear_data()的那一行)确实从缓冲区复制了整个数据包数据在驱动程序内部Rx环( rx_buf )中 到skb的数据缓冲区.

The highlighted line (the one with skb_copy_to_linear_data()) indeed copies entire packet data from the buffer in the driver-internal Rx ring (rx_buf) to the data buffer of the skb.

static inline void skb_copy_to_linear_data(struct sk_buff *skb,
                       const void *from,
                       const unsigned int len)
{
    memcpy(skb->data, from, len);
}

rx_buf 指针投射到以太网标头也应该可以.但是,在您的问题中,像这样访问数据包头的目的相当模糊.您是要仅打印(转储")数据包,还是要将数据复制到完全不同的缓冲区以在其他地方使用?

Casting the rx_buf pointer to Ethernet header should be OK, too. However, the purpose of accessing the packet header like this is rather vague in the question of yours. Are you trying to just print ("dump") the packet or do you intend to copy the packet data to a completely different buffer to be consumed elsewhere?

这篇关于如何从skb提取整个数据包,包括以太网头,ip和tcp以及设备驱动程序的轮询方法中的有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:48