What's the correct way to process all the payload of a sk_buff packet in Linux

后端 未结 1 1096
终归单人心
终归单人心 2021-02-04 19:41

I\'m currently trying to debug an ATM encapsulation layer that runs on-top of Ethernet. Basically the ATM cells are stored in order after the ethernet header. However I suspect

相关标签:
1条回答
  • 2021-02-04 20:00

    The implementation of socket buffers are comprised of a linear data buffer and one or more page buffers.

    The existence of paged data in a socket buffer is indicated by skb->data_len member being non-zero.

    bool skb_is_nonlinear(const struct sk_buff *skb) defined in /include/linux/skbuff.h is used to test this.

    The amount of non-paged data at skb->data can be calculated as skb->len - skb->data_len. unsigned int skb_headlen(const struct sk_buff *skb) defined in /include/linux/skbuff.h is used to test this.

    The skb->data pointer only points to the non paged data, which what the driver you described possibly relies on.

    void *skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer) is defined in /include/linux/skbuff.h . It takes the socket buffer, byte offset and byte length your want and a local data buffer which is only used if the data is in one of the page buffers.

    It returns a pointer to either the data in the linear data buffer from skb->data or the pointer to the local data buffer supplied, or NULL if your offset and length were incorrect.

    For pieces of data larger than protocol headers you either want to use

    int skb_copy_bits(const struct sk_buff *skb, int offset,void *to, int len);

    to copy from a given socket buffer ,byte offset and byte length to a given kernel buffer.

    or

    int skb_copy_datagram_iovec(const struct sk_buff *from, int offset, struct iovec *to, int size);

    to to copy from a given socket buffer ,byte offset and byte length to a given iovec structure in user space.

    Examples of usage can been seen in netfilter code and in other Ethernet drivers.

    For further information

    • Socket Buffer Introduction by David S. Miller
    • Socket Buffer Data Layout by David S. Miller
    • Socket Buffer Tutorial by Roberto Innocente, O.S.Adewale
    0 讨论(0)
提交回复
热议问题