Data transfer on the link layer of the linux protocol stack -sk

  
 

sk_buff structure analysis sk_buff is the second important structure we encountered, often abbreviated as skb in the kernel. In linux 2.6.21 it is defined as: struct sk_buff {//points to the next skbstruct sk_buff *next ;//Previous skbstruct sk_buff *prev; struct sk_buf0f_head *list; //corresponding sock. This is also an important structure. In the transport layer, we will analyze the struct sock *sk; //receive or send the timestamp struct timeval stamp; //receive or send the corresponding net_devicestruct net_device * dev; //receive net_devicestruct net_device *input_dev;//The real net_device corresponding to the data packet. The virtual device can discuss the struct net_device *real_dev;//ip layer related information in the subsequent bridge mode analysis. union {struct tcphdr *th;struct udphdr *uh;struct icmphdr * icmph; struct igmphdr * igmph; struct iphdr * ipiph; struct ipv6hdr * ipv6h; unsigned char * raw;} h; information protocol layer union //{struct iphdr * iph; struct ipv6hdr * ipv6h; struct arphdr * arph ;unsigned char *raw;} nh;//link layer related information union {unsigned char *raw;} mac; //in the routing subsystem to analyze this structure struct dst_entry * dst; struct sec_path * sp; /** This is the control buffer. It is free to use for every * layer. Please put your private variables there. If you * want to keep them across layers you have to do a skb_clone () * first . This is owned by whoever has the skb queued ATM * /char cb [40];. //data length of each layer unsigned int len, data_len, mac_len, csum; unsigned char local_df, cloned, pkt_type, ip_summed; __ u32 priority; unsigned short protocol, security; void (* destructor) (struct sk_buff * skb); # ifdef CONFIG_NETFILTERunsigned long nfmark; __ u32 nfcache; __ u32 nfctinfo; struct nf_conntrack * nfct; #ifdef CONFIG_NETFILTER_DEBUGunsigned int nf_debug; # endif # ifdef CONFIG_BRIDGE_NETFILTERstruct nf_bridge_info * nf_bridge; # endif # endif /* CONFIG_NETFILTER * /# if defined (CONFIG_HIPPI) union {__u32 ifield;} private; # endif # ifdef CONFIG_NET_SCHED__u32 tc_index; /* traffic control index * /# ifdef CONFIG_NET_CLS_ACT__u32 tc_verd; /* traffic control verdict * /__ u32 tc_classid; /* traffic control classid * /# endif # endif /* These elements must be at the end, see alloc_skb () for details * /unsigned int truesize;. //reference count atomic_t users; //start of storage Address unsigned char *head, //​​network data starting address * data, //​​store network data End address * tail, the end address of the memory space //* end;} corresponding to the NIC driver on our analysis. The received data is stored in the area between data and tail. Skb usually has several commonly used functions. The analysis is as follows: struct sk_buff *alloc_skb(unsigned int size, int gfp_mask) allocates the storage space to sixe's skb, and the memory allocation level is gfp_mask. Note the meaning of the storage space here. That is, skb->data to skb->tail area struct sk_buff *skb_clone(struct sk_buff *skb, int priority) cloned skb points to the same structure, and will increase the skb reference count struct sk_buff *skb_copy(const Struct sk_buff *skb, int priority) Copy a brand new skbvoid kfree_skb(struct sk_buff *skb) When the skb reference count is 1, release the skbunsigned char *skb_put(struct sk_buff *skb, unsigned int len) to make skb storage Space expansion len. Even if the tail pointer moves down unsigned char *skb_push(struct sk_buff *skb, unsigned int len) push, the data is pushed out, so that the data pointer is under the layer. Void skb_reserve(struct sk_buff *skb, unsigned int len) This operation causes the data pointer to move down with the tail pointer at the same time, that is, the space before the storage area is expanded. skb_headroom(const struct sk_buff *skb) The amount of space available before returning data is int skb_tailroom( Const struct sk_buff *skb) Returns the amount of space available in the cache

Copyright © Windows knowledge All Rights Reserved