Reputation: 986
I am working on a kernel module, in which i want to transfer a structure via skb. I can do so by putting each of data element of struct in skb; however my question is: can I put complete structure in skb in one shot and send it across ?
Upvotes: 0
Views: 695
Reputation: 1385
I tried to improve the above answer provided by @Fingolfin and stackoverflow not allowing me to do that.
After allocation new SKB by using alloc_skb(). You want to transfer data (your_structure) to skb in one shot.
skb = alloc_skb(len, GFP_KERNEL);
Initially head, tail and data are pointing at same location and end pointer points to the last. Use this link to understand more.
You got skb now. By using skb_put() you can add data to a buffer.
void *skb_put(struct sk_buff *skb, unsigned int len)
skb is the buffer to use and len is the amount of data to add. This function extends the used data area of the buffer. people got confused it gives the starting address of user data area but actually it is other way around. Basically, we are appending data to the tail of a skb buffer.
The skb_put() function used to increment the len and tail values after data has been placed in the sk_buff *skb
.
Snippet below -
void *skb_put(struct sk_buff *skb, unsigned int len)
{
void *tmp = skb_tail_pointer(skb);
SKB_LINEAR_ASSERT(skb);
skb->tail += len;
skb->len += len;
if (unlikely(skb->tail > skb->end))
skb_over_panic(skb, len, __builtin_return_address(0));
return tmp;
}
Now copy your data to skb buff -
tmp = skb_put(skb, struct_len);
memcpy(tmp,struct_data, struct_len);
You can also use below api instead of mentioned above -
skb_put_data(skb, struct_data, struct_len);
Upvotes: 0
Reputation: 5533
You can simply get a pointer to the whole struct then memcpy the contents to your buffer.
/* skb_put returns a pointer to the beginning of the data area in the skb*/
unsigned char *skb_data = skb_put(skb, size_of_data);
unsigned char *your_data = (unsigned char *) your_struct;
memcpy(skb_data, your_data, size_of_data);
Of course, make sure the skb has enough data space, you can do that using the skb_tailroom
function.
Upvotes: 2