📄 skb - linux network buffers.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0044)http://gnumonks.org/ftp/pub/doc/skb-doc.html -->
<HTML><HEAD><TITLE>skb - Linux network buffers</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1400" name=GENERATOR></HEAD>
<BODY>
<H1>skb - Linux network buffers</H1>
<H2>Harald Welte <CODE>laforge@gnumonks.org</CODE></H2>1.3, 2000/10/14 21:27:02
<P>
<HR>
<EM>Short description about the linux network buffers (skb's)</EM>
<HR>
<H2><A name=s1>1. Introduction</A></H2>
<P>At the time I wanted to know more about the Linux network stack, I always
wanted a document like this to exist. But unfortunately I never found one. After
I gained some basic knowledge about the Linux network stack internals, I wrote
one.
<P>I'm happy if this document is of any use for other people trying to learn
about the Linux kernel.
<P>Please let me know of any bugs in this document. It should resemble kernel
revision 2.4.0-test4
<P>
<H2><A name=s2>2. skbuff's</A></H2>
<P>skbuffs are the buffers in which the linux kernel handles network packets.
The packet is received by the network card, put into a skbuff and then passed to
the network stack, which uses the skbuff all the time.
<P>
<H2>2.1 struct sk_buff</H2>
<P>The struct sk_buff is defined in <linux/skbuff.h> as follows:
<P>
<DL>
<DT><B>next</B>
<DD>
<P>next buffer in list </P>
<DT><B>prev</B>
<DD>
<P>previous buffer in list </P>
<DT><B>list</B>
<DD>
<P>list we are on </P>
<DT><B>sk</B>
<DD>
<P>socket we belong to </P>
<DT><B>stamp</B>
<DD>
<P>timeval we arrived at </P>
<DT><B>dev</B>
<DD>
<P>device we are leaving by </P>
<DT><B>rx_dev</B>
<DD>
<P>device we arrived at </P>
<DT><B>h</B>
<DD>
<P>transport layer header (tcp,udp,icmp,igmp,spx,raw) </P>
<DT><B>nh</B>
<DD>
<P>network layer header (ip,ipv6,arp,ipx,raw) </P>
<DT><B>mac</B>
<DD>
<P>link layer header </P>
<DT><B>dst</B>
<DD>
<P>FIXME: </P>
<DT><B>cb</B>
<DD>
<P>control buffer, used internally </P>
<DT><B>len</B>
<DD>
<P>length of actual data </P>
<DT><B>csum</B>
<DD>
<P>checksum </P>
<DT><B>used</B>
<DD>
<P>FIXME: data moved to user and not MSG_PEEK </P>
<DT><B>is_clone</B>
<DD>
<P>we are a clone </P>
<DT><B>cloned</B>
<DD>
<P>head may be cloned </P>
<DT><B>pkt_type</B>
<DD>
<P>packet class </P>
<DT><B>ip_summed</B>
<DD>
<P>driver fed us ip checksum </P>
<DT><B>priority</B>
<DD>
<P>packet queuing priority </P>
<DT><B>users</B>
<DD>
<P>user count </P>
<DT><B>protocol</B>
<DD>
<P>packet protocol from driver </P>
<DT><B>security</B>
<DD>
<P>security level of packet </P>
<DT><B>truesize</B>
<DD>
<P>real size of the buffer </P>
<DT><B>head</B>
<DD>
<P>pointer to head of buffer </P>
<DT><B>data</B>
<DD>
<P>data head pointer </P>
<DT><B>tail</B>
<DD>
<P>tail pointer </P>
<DT><B>end</B>
<DD>
<P>end pointer </P>
<DT><B>destructor</B>
<DD>
<P>destructor function
<P></P>
<DT><B>nfmark</B>
<DD>
<P>netfilter mark </P>
<DT><B>nfcache</B>
<DD>
<P>netfilter internal caching info </P>
<DT><B>nfct</B>
<DD>
<P>associated connection, if any
<P></P>
<DT><B>tc_index</B>
<DD>
<P>traffic control index </P></DD></DL>
<P>
<H2>2.2 skb support functions</H2>
<P>There are a bunch of skb support functions provided by the sk_buff layer. I
briefly describe the most important ones in this section.
<P>
<P>
<H3>allocation / free / copy / clone and expansion functions</H3>
<P>
<P>
<DL>
<DT><B>struct sk_buff *alloc_skb(unsigned int size, int gfp_mask)</B>
<DD>
<P>This function allocates a new skb. This is provided by the skb layer to
initialize some privat data and do memory statistics. The returned buffer has
no headroom and a tailroom of /size/ bytes.
<P>
<P></P>
<DT><B>void kfree_skb(struct sk_buff *skb)</B>
<DD>
<P>Decrement the skb's usage count by one and free the skb if no references
left.
<P></P>
<DT><B>struct sk_buff *skb_get(struct sk_buff *skb)</B>
<DD>
<P>Increments the skb's usage count by one and returns a pointer to it.
<P></P>
<DT><B>struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask)</B>
<DD>
<P>This function clones a skb. Both copies share the packet data but have
their own struct sk_buff. The new copy is not owned by any socket, reference
count is 1.
<P>
<P></P>
<DT><B>struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask)</B>
<DD>
<P>Makes a real copy of the skb, including packet data. This is needed, if You
wish to modify the packet data. Reference count of the new skb is 1.
<P>
<P></P>
<DT><B>struct skb_copy_expand(const struct sk_buff *skb, int new_headroom, int
new_tailroom, int gfp_mask)</B>
<DD>
<P>Make a copy of the skb, including packet data. Additionally the new skb has
a haedroom of /new_headroom/ bytes size and a tailroom of /new_tailroom/
bytes.
<P></P></DD></DL>
<P>
<H3>anciliary functions</H3>
<P>
<P>
<P>
<DL>
<P>
<DT><B>int skb_cloned(struct sk_buff *skb)</B>
<DD>
<P>Is the skb a clone?
<P></P>
<DT><B>int skb_shared(struct sk_Buff *skb)</B>
<DD>
<P>Is this skb shared? (is the reference count > 1)?
<P></P></DD></DL>
<P>
<P>
<H3>operations on lists of skb's</H3>
<P>
<P>
<DL>
<DT><B>struct sk_buff *skb_peek(struct sk_buff_head *list_)</B>
<DD>
<P>peek a skb from front of the list; does not remove skb from the list
<P>
<P></P>
<DT><B>struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)</B>
<DD>
<P>peek a skb from tail of the list; does not remove sk from the list
<P>
<P></P>
<DT><B>__u32 skb_queue_len(sk_buff_head *list_)</B>
<DD>
<P>return the length of the given skb list
<P>
<P></P>
<DT><B>void skb_queue_head(struct sk_buff_head *list_, struct sk_buff
*newsk)</B>
<DD>
<P>enqueue a skb at the head of a given list
<P>
<P></P>
<DT><B>void skb_queue_tail(struct sk_buff_head *list_, struct sk_buff
*newsk)</B>
<DD>
<P>enqueue a skb at the end of a given list.
<P>
<P>struct sk_buff *skb_dequeue(struct sk_buff_head *list_)
<P>dequeue a skb from the head of the given list.
<P>
<P>struct sk_buff *sbk_dequeue_tail(struct sk_buff_head *list_)
<P>dequeue a skb from the tail of the given list </P></DD></DL>
<P>
<P>
<H3>operations on skb data</H3>
<P>
<P>
<DL>
<DT><B>unsigned char *skb_put(struct sk_buff *sbk, int len)</B>
<DD>
<P>extends the data area of the skb. if the total size exceeds the size of the
skb, the kernel will panic. A pointer to the first byte of new data is
returned.
<P></P>
<DT><B>unsigned char *skb_push(struct sk_buff *skb, int len)</B>
<DD>
<P>extends the data area of the skb. if the total size exceeds the size of the
skb, the kernel will panic. A pointer to the first byte of new data is
returned.
<P></P>
<DT><B>unsigned char *skb_pull(struct sk_buff *skb, int len)</B>
<DD>
<P>remove data from the start of a buffer, returning the bytes to headroom. A
pointr to the next data in the buffer is returned.
<P></P>
<DT><B>int skb_headroom(struct sk_buff *skb)</B>
<DD>
<P>return the amount of bytes of free space at the head of skb
<P></P>
<DT><B>int skb_tailroom(struct sk_buff *skb)</B>
<DD>
<P>return the amount of bytes of free space at the end of skb
<P></P>
<DT><B>struct sk_buff *skb_cow(struct sk_buff *skb, int headroom)</B>
<DD>
<P>if the buffer passed lacks sufficient headroom or is a clone it is copied
and additional headroom made available.
<P></P></DD></DL>
<P></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -