ia64/xen-unstable

view linux-2.4.30-xen-sparse/include/linux/skbuff.h @ 5157:f1ac5983d4d8

bitkeeper revision 1.1555 (4294b5f0B3iu-SnB9loIMnLXO0loTA)

Ported genapic to Xen: support for bigsmp and numa platforms such as
es7000.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed May 25 17:29:20 2005 +0000 (2005-05-25)
parents 85fcf3b1b7a5
children 56a63f9f378f
line source
1 /*
2 * Definitions for the 'struct sk_buff' memory handlers.
3 *
4 * Authors:
5 * Alan Cox, <gw4pts@gw4pts.ampr.org>
6 * Florian La Roche, <rzsfl@rz.uni-sb.de>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
14 #ifndef _LINUX_SKBUFF_H
15 #define _LINUX_SKBUFF_H
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/time.h>
21 #include <linux/cache.h>
23 #include <asm/atomic.h>
24 #include <asm/types.h>
25 #include <linux/spinlock.h>
26 #include <linux/mm.h>
27 #include <linux/highmem.h>
29 #define HAVE_ALLOC_SKB /* For the drivers to know */
30 #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
31 #define SLAB_SKB /* Slabified skbuffs */
33 #define CHECKSUM_NONE 0
34 #define CHECKSUM_HW 1
35 #define CHECKSUM_UNNECESSARY 2
37 #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES-1)) & ~(SMP_CACHE_BYTES-1))
38 #define SKB_MAX_ORDER(X,ORDER) (((PAGE_SIZE<<(ORDER)) - (X) - sizeof(struct skb_shared_info))&~(SMP_CACHE_BYTES-1))
39 #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X),0))
40 #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0,2))
42 /* A. Checksumming of received packets by device.
43 *
44 * NONE: device failed to checksum this packet.
45 * skb->csum is undefined.
46 *
47 * UNNECESSARY: device parsed packet and wouldbe verified checksum.
48 * skb->csum is undefined.
49 * It is bad option, but, unfortunately, many of vendors do this.
50 * Apparently with secret goal to sell you new device, when you
51 * will add new protocol to your host. F.e. IPv6. 8)
52 *
53 * HW: the most generic way. Device supplied checksum of _all_
54 * the packet as seen by netif_rx in skb->csum.
55 * NOTE: Even if device supports only some protocols, but
56 * is able to produce some skb->csum, it MUST use HW,
57 * not UNNECESSARY.
58 *
59 * B. Checksumming on output.
60 *
61 * NONE: skb is checksummed by protocol or csum is not required.
62 *
63 * HW: device is required to csum packet as seen by hard_start_xmit
64 * from skb->h.raw to the end and to record the checksum
65 * at skb->h.raw+skb->csum.
66 *
67 * Device must show its capabilities in dev->features, set
68 * at device setup time.
69 * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
70 * everything.
71 * NETIF_F_NO_CSUM - loopback or reliable single hop media.
72 * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
73 * TCP/UDP over IPv4. Sigh. Vendors like this
74 * way by an unknown reason. Though, see comment above
75 * about CHECKSUM_UNNECESSARY. 8)
76 *
77 * Any questions? No questions, good. --ANK
78 */
80 #ifdef __i386__
81 #define NET_CALLER(arg) (*(((void**)&arg)-1))
82 #else
83 #define NET_CALLER(arg) __builtin_return_address(0)
84 #endif
86 #ifdef CONFIG_NETFILTER
87 struct nf_conntrack {
88 atomic_t use;
89 void (*destroy)(struct nf_conntrack *);
90 };
92 struct nf_ct_info {
93 struct nf_conntrack *master;
94 };
95 #endif
97 struct sk_buff_head {
98 /* These two members must be first. */
99 struct sk_buff * next;
100 struct sk_buff * prev;
102 __u32 qlen;
103 spinlock_t lock;
104 };
106 struct sk_buff;
108 #define MAX_SKB_FRAGS 6
110 typedef struct skb_frag_struct skb_frag_t;
112 struct skb_frag_struct
113 {
114 struct page *page;
115 __u16 page_offset;
116 __u16 size;
117 };
119 /* This data is invariant across clones and lives at
120 * the end of the header data, ie. at skb->end.
121 */
122 struct skb_shared_info {
123 atomic_t dataref;
124 unsigned int nr_frags;
125 struct sk_buff *frag_list;
126 skb_frag_t frags[MAX_SKB_FRAGS];
127 };
129 struct sk_buff {
130 /* These two members must be first. */
131 struct sk_buff * next; /* Next buffer in list */
132 struct sk_buff * prev; /* Previous buffer in list */
134 struct sk_buff_head * list; /* List we are on */
135 struct sock *sk; /* Socket we are owned by */
136 struct timeval stamp; /* Time we arrived */
137 struct net_device *dev; /* Device we arrived on/are leaving by */
138 struct net_device *real_dev; /* For support of point to point protocols
139 (e.g. 802.3ad) over bonding, we must save the
140 physical device that got the packet before
141 replacing skb->dev with the virtual device. */
143 /* Transport layer header */
144 union
145 {
146 struct tcphdr *th;
147 struct udphdr *uh;
148 struct icmphdr *icmph;
149 struct igmphdr *igmph;
150 struct iphdr *ipiph;
151 struct spxhdr *spxh;
152 unsigned char *raw;
153 } h;
155 /* Network layer header */
156 union
157 {
158 struct iphdr *iph;
159 struct ipv6hdr *ipv6h;
160 struct arphdr *arph;
161 struct ipxhdr *ipxh;
162 unsigned char *raw;
163 } nh;
165 /* Link layer header */
166 union
167 {
168 struct ethhdr *ethernet;
169 unsigned char *raw;
170 } mac;
172 struct dst_entry *dst;
174 /*
175 * This is the control buffer. It is free to use for every
176 * layer. Please put your private variables there. If you
177 * want to keep them across layers you have to do a skb_clone()
178 * first. This is owned by whoever has the skb queued ATM.
179 */
180 char cb[48];
182 unsigned int len; /* Length of actual data */
183 unsigned int data_len;
184 unsigned int csum; /* Checksum */
185 unsigned char __unused, /* Dead field, may be reused */
186 cloned, /* head may be cloned (check refcnt to be sure). */
187 pkt_type, /* Packet class */
188 ip_summed; /* Driver fed us an IP checksum */
189 __u32 priority; /* Packet queueing priority */
190 atomic_t users; /* User count - see datagram.c,tcp.c */
191 unsigned short protocol; /* Packet protocol from driver. */
192 unsigned short security; /* Security level of packet */
193 unsigned int truesize; /* Buffer size */
195 unsigned char *head; /* Head of buffer */
196 unsigned char *data; /* Data head pointer */
197 unsigned char *tail; /* Tail pointer */
198 unsigned char *end; /* End pointer */
200 void (*destructor)(struct sk_buff *); /* Destruct function */
201 #ifdef CONFIG_NETFILTER
202 /* Can be used for communication between hooks. */
203 unsigned long nfmark;
204 /* Cache info */
205 __u32 nfcache;
206 /* Associated connection, if any */
207 struct nf_ct_info *nfct;
208 #ifdef CONFIG_NETFILTER_DEBUG
209 unsigned int nf_debug;
210 #endif
211 #endif /*CONFIG_NETFILTER*/
213 #if defined(CONFIG_HIPPI)
214 union{
215 __u32 ifield;
216 } private;
217 #endif
219 #ifdef CONFIG_NET_SCHED
220 __u32 tc_index; /* traffic control index */
221 #endif
222 };
224 #ifdef __KERNEL__
225 /*
226 * Handling routines are only of interest to the kernel
227 */
228 #include <linux/slab.h>
230 #include <asm/system.h>
232 extern void __kfree_skb(struct sk_buff *skb);
233 extern struct sk_buff * alloc_skb(unsigned int size, int priority);
234 extern struct sk_buff * alloc_skb_from_cache(kmem_cache_t *cp, unsigned int size, int priority);
235 extern void kfree_skbmem(struct sk_buff *skb);
236 extern struct sk_buff * skb_clone(struct sk_buff *skb, int priority);
237 extern struct sk_buff * skb_copy(const struct sk_buff *skb, int priority);
238 extern struct sk_buff * pskb_copy(struct sk_buff *skb, int gfp_mask);
239 extern int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask);
240 extern struct sk_buff * skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom);
241 extern struct sk_buff * skb_copy_expand(const struct sk_buff *skb,
242 int newheadroom,
243 int newtailroom,
244 int priority);
245 extern struct sk_buff * skb_pad(struct sk_buff *skb, int pad);
246 #define dev_kfree_skb(a) kfree_skb(a)
247 extern void skb_over_panic(struct sk_buff *skb, int len, void *here);
248 extern void skb_under_panic(struct sk_buff *skb, int len, void *here);
250 /* Internal */
251 #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
253 /**
254 * skb_queue_empty - check if a queue is empty
255 * @list: queue head
256 *
257 * Returns true if the queue is empty, false otherwise.
258 */
260 static inline int skb_queue_empty(struct sk_buff_head *list)
261 {
262 return (list->next == (struct sk_buff *) list);
263 }
265 /**
266 * skb_get - reference buffer
267 * @skb: buffer to reference
268 *
269 * Makes another reference to a socket buffer and returns a pointer
270 * to the buffer.
271 */
273 static inline struct sk_buff *skb_get(struct sk_buff *skb)
274 {
275 atomic_inc(&skb->users);
276 return skb;
277 }
279 /*
280 * If users==1, we are the only owner and are can avoid redundant
281 * atomic change.
282 */
284 /**
285 * kfree_skb - free an sk_buff
286 * @skb: buffer to free
287 *
288 * Drop a reference to the buffer and free it if the usage count has
289 * hit zero.
290 */
292 static inline void kfree_skb(struct sk_buff *skb)
293 {
294 if (likely(atomic_read(&skb->users) == 1))
295 smp_rmb();
296 else if (likely(!atomic_dec_and_test(&skb->users)))
297 return;
298 __kfree_skb(skb);
299 }
301 /**
302 * skb_cloned - is the buffer a clone
303 * @skb: buffer to check
304 *
305 * Returns true if the buffer was generated with skb_clone() and is
306 * one of multiple shared copies of the buffer. Cloned buffers are
307 * shared data so must not be written to under normal circumstances.
308 */
310 static inline int skb_cloned(struct sk_buff *skb)
311 {
312 return skb->cloned && atomic_read(&skb_shinfo(skb)->dataref) != 1;
313 }
315 /**
316 * skb_shared - is the buffer shared
317 * @skb: buffer to check
318 *
319 * Returns true if more than one person has a reference to this
320 * buffer.
321 */
323 static inline int skb_shared(struct sk_buff *skb)
324 {
325 return (atomic_read(&skb->users) != 1);
326 }
328 /**
329 * skb_share_check - check if buffer is shared and if so clone it
330 * @skb: buffer to check
331 * @pri: priority for memory allocation
332 *
333 * If the buffer is shared the buffer is cloned and the old copy
334 * drops a reference. A new clone with a single reference is returned.
335 * If the buffer is not shared the original buffer is returned. When
336 * being called from interrupt status or with spinlocks held pri must
337 * be GFP_ATOMIC.
338 *
339 * NULL is returned on a memory allocation failure.
340 */
342 static inline struct sk_buff *skb_share_check(struct sk_buff *skb, int pri)
343 {
344 if (skb_shared(skb)) {
345 struct sk_buff *nskb;
346 nskb = skb_clone(skb, pri);
347 kfree_skb(skb);
348 return nskb;
349 }
350 return skb;
351 }
354 /*
355 * Copy shared buffers into a new sk_buff. We effectively do COW on
356 * packets to handle cases where we have a local reader and forward
357 * and a couple of other messy ones. The normal one is tcpdumping
358 * a packet thats being forwarded.
359 */
361 /**
362 * skb_unshare - make a copy of a shared buffer
363 * @skb: buffer to check
364 * @pri: priority for memory allocation
365 *
366 * If the socket buffer is a clone then this function creates a new
367 * copy of the data, drops a reference count on the old copy and returns
368 * the new copy with the reference count at 1. If the buffer is not a clone
369 * the original buffer is returned. When called with a spinlock held or
370 * from interrupt state @pri must be %GFP_ATOMIC
371 *
372 * %NULL is returned on a memory allocation failure.
373 */
375 static inline struct sk_buff *skb_unshare(struct sk_buff *skb, int pri)
376 {
377 struct sk_buff *nskb;
378 if(!skb_cloned(skb))
379 return skb;
380 nskb=skb_copy(skb, pri);
381 kfree_skb(skb); /* Free our shared copy */
382 return nskb;
383 }
385 /**
386 * skb_peek
387 * @list_: list to peek at
388 *
389 * Peek an &sk_buff. Unlike most other operations you _MUST_
390 * be careful with this one. A peek leaves the buffer on the
391 * list and someone else may run off with it. You must hold
392 * the appropriate locks or have a private queue to do this.
393 *
394 * Returns %NULL for an empty list or a pointer to the head element.
395 * The reference count is not incremented and the reference is therefore
396 * volatile. Use with caution.
397 */
399 static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
400 {
401 struct sk_buff *list = ((struct sk_buff *)list_)->next;
402 if (list == (struct sk_buff *)list_)
403 list = NULL;
404 return list;
405 }
407 /**
408 * skb_peek_tail
409 * @list_: list to peek at
410 *
411 * Peek an &sk_buff. Unlike most other operations you _MUST_
412 * be careful with this one. A peek leaves the buffer on the
413 * list and someone else may run off with it. You must hold
414 * the appropriate locks or have a private queue to do this.
415 *
416 * Returns %NULL for an empty list or a pointer to the tail element.
417 * The reference count is not incremented and the reference is therefore
418 * volatile. Use with caution.
419 */
421 static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
422 {
423 struct sk_buff *list = ((struct sk_buff *)list_)->prev;
424 if (list == (struct sk_buff *)list_)
425 list = NULL;
426 return list;
427 }
429 /**
430 * skb_queue_len - get queue length
431 * @list_: list to measure
432 *
433 * Return the length of an &sk_buff queue.
434 */
436 static inline __u32 skb_queue_len(struct sk_buff_head *list_)
437 {
438 return(list_->qlen);
439 }
441 static inline void skb_queue_head_init(struct sk_buff_head *list)
442 {
443 spin_lock_init(&list->lock);
444 list->prev = (struct sk_buff *)list;
445 list->next = (struct sk_buff *)list;
446 list->qlen = 0;
447 }
449 /*
450 * Insert an sk_buff at the start of a list.
451 *
452 * The "__skb_xxxx()" functions are the non-atomic ones that
453 * can only be called with interrupts disabled.
454 */
456 /**
457 * __skb_queue_head - queue a buffer at the list head
458 * @list: list to use
459 * @newsk: buffer to queue
460 *
461 * Queue a buffer at the start of a list. This function takes no locks
462 * and you must therefore hold required locks before calling it.
463 *
464 * A buffer cannot be placed on two lists at the same time.
465 */
467 static inline void __skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
468 {
469 struct sk_buff *prev, *next;
471 newsk->list = list;
472 list->qlen++;
473 prev = (struct sk_buff *)list;
474 next = prev->next;
475 newsk->next = next;
476 newsk->prev = prev;
477 next->prev = newsk;
478 prev->next = newsk;
479 }
482 /**
483 * skb_queue_head - queue a buffer at the list head
484 * @list: list to use
485 * @newsk: buffer to queue
486 *
487 * Queue a buffer at the start of the list. This function takes the
488 * list lock and can be used safely with other locking &sk_buff functions
489 * safely.
490 *
491 * A buffer cannot be placed on two lists at the same time.
492 */
494 static inline void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
495 {
496 unsigned long flags;
498 spin_lock_irqsave(&list->lock, flags);
499 __skb_queue_head(list, newsk);
500 spin_unlock_irqrestore(&list->lock, flags);
501 }
503 /**
504 * __skb_queue_tail - queue a buffer at the list tail
505 * @list: list to use
506 * @newsk: buffer to queue
507 *
508 * Queue a buffer at the end of a list. This function takes no locks
509 * and you must therefore hold required locks before calling it.
510 *
511 * A buffer cannot be placed on two lists at the same time.
512 */
515 static inline void __skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
516 {
517 struct sk_buff *prev, *next;
519 newsk->list = list;
520 list->qlen++;
521 next = (struct sk_buff *)list;
522 prev = next->prev;
523 newsk->next = next;
524 newsk->prev = prev;
525 next->prev = newsk;
526 prev->next = newsk;
527 }
529 /**
530 * skb_queue_tail - queue a buffer at the list tail
531 * @list: list to use
532 * @newsk: buffer to queue
533 *
534 * Queue a buffer at the tail of the list. This function takes the
535 * list lock and can be used safely with other locking &sk_buff functions
536 * safely.
537 *
538 * A buffer cannot be placed on two lists at the same time.
539 */
541 static inline void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
542 {
543 unsigned long flags;
545 spin_lock_irqsave(&list->lock, flags);
546 __skb_queue_tail(list, newsk);
547 spin_unlock_irqrestore(&list->lock, flags);
548 }
550 /**
551 * __skb_dequeue - remove from the head of the queue
552 * @list: list to dequeue from
553 *
554 * Remove the head of the list. This function does not take any locks
555 * so must be used with appropriate locks held only. The head item is
556 * returned or %NULL if the list is empty.
557 */
559 static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
560 {
561 struct sk_buff *next, *prev, *result;
563 prev = (struct sk_buff *) list;
564 next = prev->next;
565 result = NULL;
566 if (next != prev) {
567 result = next;
568 next = next->next;
569 list->qlen--;
570 next->prev = prev;
571 prev->next = next;
572 result->next = NULL;
573 result->prev = NULL;
574 result->list = NULL;
575 }
576 return result;
577 }
579 /**
580 * skb_dequeue - remove from the head of the queue
581 * @list: list to dequeue from
582 *
583 * Remove the head of the list. The list lock is taken so the function
584 * may be used safely with other locking list functions. The head item is
585 * returned or %NULL if the list is empty.
586 */
588 static inline struct sk_buff *skb_dequeue(struct sk_buff_head *list)
589 {
590 unsigned long flags;
591 struct sk_buff *result;
593 spin_lock_irqsave(&list->lock, flags);
594 result = __skb_dequeue(list);
595 spin_unlock_irqrestore(&list->lock, flags);
596 return result;
597 }
599 /*
600 * Insert a packet on a list.
601 */
603 static inline void __skb_insert(struct sk_buff *newsk,
604 struct sk_buff * prev, struct sk_buff *next,
605 struct sk_buff_head * list)
606 {
607 newsk->next = next;
608 newsk->prev = prev;
609 next->prev = newsk;
610 prev->next = newsk;
611 newsk->list = list;
612 list->qlen++;
613 }
615 /**
616 * skb_insert - insert a buffer
617 * @old: buffer to insert before
618 * @newsk: buffer to insert
619 *
620 * Place a packet before a given packet in a list. The list locks are taken
621 * and this function is atomic with respect to other list locked calls
622 * A buffer cannot be placed on two lists at the same time.
623 */
625 static inline void skb_insert(struct sk_buff *old, struct sk_buff *newsk)
626 {
627 unsigned long flags;
629 spin_lock_irqsave(&old->list->lock, flags);
630 __skb_insert(newsk, old->prev, old, old->list);
631 spin_unlock_irqrestore(&old->list->lock, flags);
632 }
634 /*
635 * Place a packet after a given packet in a list.
636 */
638 static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk)
639 {
640 __skb_insert(newsk, old, old->next, old->list);
641 }
643 /**
644 * skb_append - append a buffer
645 * @old: buffer to insert after
646 * @newsk: buffer to insert
647 *
648 * Place a packet after a given packet in a list. The list locks are taken
649 * and this function is atomic with respect to other list locked calls.
650 * A buffer cannot be placed on two lists at the same time.
651 */
654 static inline void skb_append(struct sk_buff *old, struct sk_buff *newsk)
655 {
656 unsigned long flags;
658 spin_lock_irqsave(&old->list->lock, flags);
659 __skb_append(old, newsk);
660 spin_unlock_irqrestore(&old->list->lock, flags);
661 }
663 /*
664 * remove sk_buff from list. _Must_ be called atomically, and with
665 * the list known..
666 */
668 static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
669 {
670 struct sk_buff * next, * prev;
672 list->qlen--;
673 next = skb->next;
674 prev = skb->prev;
675 skb->next = NULL;
676 skb->prev = NULL;
677 skb->list = NULL;
678 next->prev = prev;
679 prev->next = next;
680 }
682 /**
683 * skb_unlink - remove a buffer from a list
684 * @skb: buffer to remove
685 *
686 * Place a packet after a given packet in a list. The list locks are taken
687 * and this function is atomic with respect to other list locked calls
688 *
689 * Works even without knowing the list it is sitting on, which can be
690 * handy at times. It also means that THE LIST MUST EXIST when you
691 * unlink. Thus a list must have its contents unlinked before it is
692 * destroyed.
693 */
695 static inline void skb_unlink(struct sk_buff *skb)
696 {
697 struct sk_buff_head *list = skb->list;
699 if(list) {
700 unsigned long flags;
702 spin_lock_irqsave(&list->lock, flags);
703 if(skb->list == list)
704 __skb_unlink(skb, skb->list);
705 spin_unlock_irqrestore(&list->lock, flags);
706 }
707 }
709 /* XXX: more streamlined implementation */
711 /**
712 * __skb_dequeue_tail - remove from the tail of the queue
713 * @list: list to dequeue from
714 *
715 * Remove the tail of the list. This function does not take any locks
716 * so must be used with appropriate locks held only. The tail item is
717 * returned or %NULL if the list is empty.
718 */
720 static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
721 {
722 struct sk_buff *skb = skb_peek_tail(list);
723 if (skb)
724 __skb_unlink(skb, list);
725 return skb;
726 }
728 /**
729 * skb_dequeue - remove from the head of the queue
730 * @list: list to dequeue from
731 *
732 * Remove the head of the list. The list lock is taken so the function
733 * may be used safely with other locking list functions. The tail item is
734 * returned or %NULL if the list is empty.
735 */
737 static inline struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
738 {
739 unsigned long flags;
740 struct sk_buff *result;
742 spin_lock_irqsave(&list->lock, flags);
743 result = __skb_dequeue_tail(list);
744 spin_unlock_irqrestore(&list->lock, flags);
745 return result;
746 }
748 static inline int skb_is_nonlinear(const struct sk_buff *skb)
749 {
750 return skb->data_len;
751 }
753 static inline unsigned int skb_headlen(const struct sk_buff *skb)
754 {
755 return skb->len - skb->data_len;
756 }
758 #define SKB_PAGE_ASSERT(skb) do { if (skb_shinfo(skb)->nr_frags) out_of_line_bug(); } while (0)
759 #define SKB_FRAG_ASSERT(skb) do { if (skb_shinfo(skb)->frag_list) out_of_line_bug(); } while (0)
760 #define SKB_LINEAR_ASSERT(skb) do { if (skb_is_nonlinear(skb)) out_of_line_bug(); } while (0)
762 /*
763 * Add data to an sk_buff
764 */
766 static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
767 {
768 unsigned char *tmp=skb->tail;
769 SKB_LINEAR_ASSERT(skb);
770 skb->tail+=len;
771 skb->len+=len;
772 return tmp;
773 }
775 /**
776 * skb_put - add data to a buffer
777 * @skb: buffer to use
778 * @len: amount of data to add
779 *
780 * This function extends the used data area of the buffer. If this would
781 * exceed the total buffer size the kernel will panic. A pointer to the
782 * first byte of the extra data is returned.
783 */
785 static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
786 {
787 unsigned char *tmp=skb->tail;
788 SKB_LINEAR_ASSERT(skb);
789 skb->tail+=len;
790 skb->len+=len;
791 if(skb->tail>skb->end) {
792 skb_over_panic(skb, len, current_text_addr());
793 }
794 return tmp;
795 }
797 static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
798 {
799 skb->data-=len;
800 skb->len+=len;
801 return skb->data;
802 }
804 /**
805 * skb_push - add data to the start of a buffer
806 * @skb: buffer to use
807 * @len: amount of data to add
808 *
809 * This function extends the used data area of the buffer at the buffer
810 * start. If this would exceed the total buffer headroom the kernel will
811 * panic. A pointer to the first byte of the extra data is returned.
812 */
814 static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
815 {
816 skb->data-=len;
817 skb->len+=len;
818 if(skb->data<skb->head) {
819 skb_under_panic(skb, len, current_text_addr());
820 }
821 return skb->data;
822 }
824 static inline char *__skb_pull(struct sk_buff *skb, unsigned int len)
825 {
826 skb->len-=len;
827 if (skb->len < skb->data_len)
828 out_of_line_bug();
829 return skb->data+=len;
830 }
832 /**
833 * skb_pull - remove data from the start of a buffer
834 * @skb: buffer to use
835 * @len: amount of data to remove
836 *
837 * This function removes data from the start of a buffer, returning
838 * the memory to the headroom. A pointer to the next data in the buffer
839 * is returned. Once the data has been pulled future pushes will overwrite
840 * the old data.
841 */
843 static inline unsigned char * skb_pull(struct sk_buff *skb, unsigned int len)
844 {
845 if (len > skb->len)
846 return NULL;
847 return __skb_pull(skb,len);
848 }
850 extern unsigned char * __pskb_pull_tail(struct sk_buff *skb, int delta);
852 static inline char *__pskb_pull(struct sk_buff *skb, unsigned int len)
853 {
854 if (len > skb_headlen(skb) &&
855 __pskb_pull_tail(skb, len-skb_headlen(skb)) == NULL)
856 return NULL;
857 skb->len -= len;
858 return skb->data += len;
859 }
861 static inline unsigned char * pskb_pull(struct sk_buff *skb, unsigned int len)
862 {
863 if (len > skb->len)
864 return NULL;
865 return __pskb_pull(skb,len);
866 }
868 static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
869 {
870 if (len <= skb_headlen(skb))
871 return 1;
872 if (len > skb->len)
873 return 0;
874 return (__pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL);
875 }
877 /**
878 * skb_headroom - bytes at buffer head
879 * @skb: buffer to check
880 *
881 * Return the number of bytes of free space at the head of an &sk_buff.
882 */
884 static inline int skb_headroom(const struct sk_buff *skb)
885 {
886 return skb->data-skb->head;
887 }
889 /**
890 * skb_tailroom - bytes at buffer end
891 * @skb: buffer to check
892 *
893 * Return the number of bytes of free space at the tail of an sk_buff
894 */
896 static inline int skb_tailroom(const struct sk_buff *skb)
897 {
898 return skb_is_nonlinear(skb) ? 0 : skb->end-skb->tail;
899 }
901 /**
902 * skb_reserve - adjust headroom
903 * @skb: buffer to alter
904 * @len: bytes to move
905 *
906 * Increase the headroom of an empty &sk_buff by reducing the tail
907 * room. This is only allowed for an empty buffer.
908 */
910 static inline void skb_reserve(struct sk_buff *skb, unsigned int len)
911 {
912 skb->data+=len;
913 skb->tail+=len;
914 }
916 extern int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc);
918 static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
919 {
920 if (!skb->data_len) {
921 skb->len = len;
922 skb->tail = skb->data+len;
923 } else {
924 ___pskb_trim(skb, len, 0);
925 }
926 }
928 /**
929 * skb_trim - remove end from a buffer
930 * @skb: buffer to alter
931 * @len: new length
932 *
933 * Cut the length of a buffer down by removing data from the tail. If
934 * the buffer is already under the length specified it is not modified.
935 */
937 static inline void skb_trim(struct sk_buff *skb, unsigned int len)
938 {
939 if (skb->len > len) {
940 __skb_trim(skb, len);
941 }
942 }
945 static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
946 {
947 if (!skb->data_len) {
948 skb->len = len;
949 skb->tail = skb->data+len;
950 return 0;
951 } else {
952 return ___pskb_trim(skb, len, 1);
953 }
954 }
956 static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
957 {
958 if (len < skb->len)
959 return __pskb_trim(skb, len);
960 return 0;
961 }
963 /**
964 * skb_orphan - orphan a buffer
965 * @skb: buffer to orphan
966 *
967 * If a buffer currently has an owner then we call the owner's
968 * destructor function and make the @skb unowned. The buffer continues
969 * to exist but is no longer charged to its former owner.
970 */
973 static inline void skb_orphan(struct sk_buff *skb)
974 {
975 if (skb->destructor)
976 skb->destructor(skb);
977 skb->destructor = NULL;
978 skb->sk = NULL;
979 }
981 /**
982 * skb_purge - empty a list
983 * @list: list to empty
984 *
985 * Delete all buffers on an &sk_buff list. Each buffer is removed from
986 * the list and one reference dropped. This function takes the list
987 * lock and is atomic with respect to other list locking functions.
988 */
991 static inline void skb_queue_purge(struct sk_buff_head *list)
992 {
993 struct sk_buff *skb;
994 while ((skb=skb_dequeue(list))!=NULL)
995 kfree_skb(skb);
996 }
998 /**
999 * __skb_purge - empty a list
1000 * @list: list to empty
1002 * Delete all buffers on an &sk_buff list. Each buffer is removed from
1003 * the list and one reference dropped. This function does not take the
1004 * list lock and the caller must hold the relevant locks to use it.
1005 */
1008 static inline void __skb_queue_purge(struct sk_buff_head *list)
1010 struct sk_buff *skb;
1011 while ((skb=__skb_dequeue(list))!=NULL)
1012 kfree_skb(skb);
1015 /**
1016 * __dev_alloc_skb - allocate an skbuff for sending
1017 * @length: length to allocate
1018 * @gfp_mask: get_free_pages mask, passed to alloc_skb
1020 * Allocate a new &sk_buff and assign it a usage count of one. The
1021 * buffer has unspecified headroom built in. Users should allocate
1022 * the headroom they think they need without accounting for the
1023 * built in space. The built in space is used for optimisations.
1025 * %NULL is returned in there is no free memory.
1026 */
1027 #ifndef CONFIG_XEN
1028 static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
1029 int gfp_mask)
1031 struct sk_buff *skb = alloc_skb(length+16, gfp_mask);
1032 if (skb)
1033 skb_reserve(skb,16);
1034 return skb;
1036 #else
1037 extern struct sk_buff *__dev_alloc_skb(unsigned int length, int gfp_mask);
1038 #endif
1040 /**
1041 * dev_alloc_skb - allocate an skbuff for sending
1042 * @length: length to allocate
1044 * Allocate a new &sk_buff and assign it a usage count of one. The
1045 * buffer has unspecified headroom built in. Users should allocate
1046 * the headroom they think they need without accounting for the
1047 * built in space. The built in space is used for optimisations.
1049 * %NULL is returned in there is no free memory. Although this function
1050 * allocates memory it can be called from an interrupt.
1051 */
1053 static inline struct sk_buff *dev_alloc_skb(unsigned int length)
1055 return __dev_alloc_skb(length, GFP_ATOMIC);
1058 /**
1059 * skb_cow - copy header of skb when it is required
1060 * @skb: buffer to cow
1061 * @headroom: needed headroom
1063 * If the skb passed lacks sufficient headroom or its data part
1064 * is shared, data is reallocated. If reallocation fails, an error
1065 * is returned and original skb is not changed.
1067 * The result is skb with writable area skb->head...skb->tail
1068 * and at least @headroom of space at head.
1069 */
1071 static inline int
1072 skb_cow(struct sk_buff *skb, unsigned int headroom)
1074 int delta = (headroom > 16 ? headroom : 16) - skb_headroom(skb);
1076 if (delta < 0)
1077 delta = 0;
1079 if (delta || skb_cloned(skb))
1080 return pskb_expand_head(skb, (delta+15)&~15, 0, GFP_ATOMIC);
1081 return 0;
1084 /**
1085 * skb_padto - pad an skbuff up to a minimal size
1086 * @skb: buffer to pad
1087 * @len: minimal length
1089 * Pads up a buffer to ensure the trailing bytes exist and are
1090 * blanked. If the buffer already contains sufficient data it
1091 * is untouched. Returns the buffer, which may be a replacement
1092 * for the original, or NULL for out of memory - in which case
1093 * the original buffer is still freed.
1094 */
1096 static inline struct sk_buff *skb_padto(struct sk_buff *skb, unsigned int len)
1098 unsigned int size = skb->len;
1099 if(likely(size >= len))
1100 return skb;
1101 return skb_pad(skb, len-size);
1104 /**
1105 * skb_linearize - convert paged skb to linear one
1106 * @skb: buffer to linarize
1107 * @gfp: allocation mode
1109 * If there is no free memory -ENOMEM is returned, otherwise zero
1110 * is returned and the old skb data released. */
1111 int skb_linearize(struct sk_buff *skb, int gfp);
1113 static inline void *kmap_skb_frag(const skb_frag_t *frag)
1115 #ifdef CONFIG_HIGHMEM
1116 if (in_irq())
1117 out_of_line_bug();
1119 local_bh_disable();
1120 #endif
1121 return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
1124 static inline void kunmap_skb_frag(void *vaddr)
1126 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
1127 #ifdef CONFIG_HIGHMEM
1128 local_bh_enable();
1129 #endif
1132 #define skb_queue_walk(queue, skb) \
1133 for (skb = (queue)->next; \
1134 (skb != (struct sk_buff *)(queue)); \
1135 skb=skb->next)
1138 extern struct sk_buff * skb_recv_datagram(struct sock *sk,unsigned flags,int noblock, int *err);
1139 extern unsigned int datagram_poll(struct file *file, struct socket *sock, struct poll_table_struct *wait);
1140 extern int skb_copy_datagram(const struct sk_buff *from, int offset, char *to,int size);
1141 extern int skb_copy_datagram_iovec(const struct sk_buff *from, int offset, struct iovec *to,int size);
1142 extern int skb_copy_and_csum_datagram(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int *csump);
1143 extern int skb_copy_and_csum_datagram_iovec(const struct sk_buff *skb, int hlen, struct iovec *iov);
1144 extern void skb_free_datagram(struct sock * sk, struct sk_buff *skb);
1146 extern unsigned int skb_checksum(const struct sk_buff *skb, int offset, int len, unsigned int csum);
1147 extern int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
1148 extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to, int len, unsigned int csum);
1149 extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
1151 extern void skb_init(void);
1152 extern void skb_add_mtu(int mtu);
1154 #ifdef CONFIG_NETFILTER
1155 static inline void
1156 nf_conntrack_put(struct nf_ct_info *nfct)
1158 if (nfct && atomic_dec_and_test(&nfct->master->use))
1159 nfct->master->destroy(nfct->master);
1161 static inline void
1162 nf_conntrack_get(struct nf_ct_info *nfct)
1164 if (nfct)
1165 atomic_inc(&nfct->master->use);
1167 static inline void
1168 nf_reset(struct sk_buff *skb)
1170 nf_conntrack_put(skb->nfct);
1171 skb->nfct = NULL;
1172 #ifdef CONFIG_NETFILTER_DEBUG
1173 skb->nf_debug = 0;
1174 #endif
1176 #else /* CONFIG_NETFILTER */
1177 static inline void nf_reset(struct sk_buff *skb) {}
1178 #endif /* CONFIG_NETFILTER */
1180 #endif /* __KERNEL__ */
1181 #endif /* _LINUX_SKBUFF_H */