inet_frag.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef __NET_FRAG_H__
  2. #define __NET_FRAG_H__
  3. #include <linux/percpu_counter.h>
  4. struct netns_frags {
  5. /* The percpu_counter "mem" need to be cacheline aligned.
  6. * mem.count must not share cacheline with other writers
  7. */
  8. struct percpu_counter mem ____cacheline_aligned_in_smp;
  9. /* sysctls */
  10. int timeout;
  11. int high_thresh;
  12. int low_thresh;
  13. };
  14. /**
  15. * fragment queue flags
  16. *
  17. * @INET_FRAG_FIRST_IN: first fragment has arrived
  18. * @INET_FRAG_LAST_IN: final fragment has arrived
  19. * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
  20. */
  21. enum {
  22. INET_FRAG_FIRST_IN = BIT(0),
  23. INET_FRAG_LAST_IN = BIT(1),
  24. INET_FRAG_COMPLETE = BIT(2),
  25. };
  26. /**
  27. * struct inet_frag_queue - fragment queue
  28. *
  29. * @lock: spinlock protecting the queue
  30. * @timer: queue expiration timer
  31. * @list: hash bucket list
  32. * @refcnt: reference count of the queue
  33. * @fragments: received fragments head
  34. * @fragments_tail: received fragments tail
  35. * @stamp: timestamp of the last received fragment
  36. * @len: total length of the original datagram
  37. * @meat: length of received fragments so far
  38. * @flags: fragment queue flags
  39. * @max_size: maximum received fragment size
  40. * @net: namespace that this frag belongs to
  41. * @list_evictor: list of queues to forcefully evict (e.g. due to low memory)
  42. */
  43. struct inet_frag_queue {
  44. spinlock_t lock;
  45. struct timer_list timer;
  46. struct hlist_node list;
  47. atomic_t refcnt;
  48. struct sk_buff *fragments;
  49. struct sk_buff *fragments_tail;
  50. ktime_t stamp;
  51. int len;
  52. int meat;
  53. __u8 flags;
  54. u16 max_size;
  55. struct netns_frags *net;
  56. struct hlist_node list_evictor;
  57. };
  58. #define INETFRAGS_HASHSZ 1024
  59. /* averaged:
  60. * max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
  61. * rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
  62. * struct frag_queue))
  63. */
  64. #define INETFRAGS_MAXDEPTH 128
  65. struct inet_frag_bucket {
  66. struct hlist_head chain;
  67. spinlock_t chain_lock;
  68. };
  69. struct inet_frags {
  70. struct inet_frag_bucket hash[INETFRAGS_HASHSZ];
  71. struct work_struct frags_work;
  72. unsigned int next_bucket;
  73. unsigned long last_rebuild_jiffies;
  74. bool rebuild;
  75. /* The first call to hashfn is responsible to initialize
  76. * rnd. This is best done with net_get_random_once.
  77. *
  78. * rnd_seqlock is used to let hash insertion detect
  79. * when it needs to re-lookup the hash chain to use.
  80. */
  81. u32 rnd;
  82. seqlock_t rnd_seqlock;
  83. int qsize;
  84. unsigned int (*hashfn)(const struct inet_frag_queue *);
  85. bool (*match)(const struct inet_frag_queue *q,
  86. const void *arg);
  87. void (*constructor)(struct inet_frag_queue *q,
  88. const void *arg);
  89. void (*destructor)(struct inet_frag_queue *);
  90. void (*skb_free)(struct sk_buff *);
  91. void (*frag_expire)(unsigned long data);
  92. struct kmem_cache *frags_cachep;
  93. const char *frags_cache_name;
  94. };
  95. int inet_frags_init(struct inet_frags *);
  96. void inet_frags_fini(struct inet_frags *);
  97. static inline int inet_frags_init_net(struct netns_frags *nf)
  98. {
  99. return percpu_counter_init(&nf->mem, 0, GFP_KERNEL);
  100. }
  101. static inline void inet_frags_uninit_net(struct netns_frags *nf)
  102. {
  103. percpu_counter_destroy(&nf->mem);
  104. }
  105. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
  106. void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
  107. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f);
  108. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  109. struct inet_frags *f, void *key, unsigned int hash);
  110. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  111. const char *prefix);
  112. static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
  113. {
  114. if (atomic_dec_and_test(&q->refcnt))
  115. inet_frag_destroy(q, f);
  116. }
  117. static inline bool inet_frag_evicting(struct inet_frag_queue *q)
  118. {
  119. return !hlist_unhashed(&q->list_evictor);
  120. }
  121. /* Memory Tracking Functions. */
  122. /* The default percpu_counter batch size is not big enough to scale to
  123. * fragmentation mem acct sizes.
  124. * The mem size of a 64K fragment is approx:
  125. * (44 fragments * 2944 truesize) + frag_queue struct(200) = 129736 bytes
  126. */
  127. static unsigned int frag_percpu_counter_batch = 130000;
  128. static inline int frag_mem_limit(struct netns_frags *nf)
  129. {
  130. return percpu_counter_read(&nf->mem);
  131. }
  132. static inline void sub_frag_mem_limit(struct netns_frags *nf, int i)
  133. {
  134. __percpu_counter_add(&nf->mem, -i, frag_percpu_counter_batch);
  135. }
  136. static inline void add_frag_mem_limit(struct netns_frags *nf, int i)
  137. {
  138. __percpu_counter_add(&nf->mem, i, frag_percpu_counter_batch);
  139. }
  140. static inline unsigned int sum_frag_mem_limit(struct netns_frags *nf)
  141. {
  142. unsigned int res;
  143. local_bh_disable();
  144. res = percpu_counter_sum_positive(&nf->mem);
  145. local_bh_enable();
  146. return res;
  147. }
  148. /* RFC 3168 support :
  149. * We want to check ECN values of all fragments, do detect invalid combinations.
  150. * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
  151. */
  152. #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
  153. #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
  154. #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
  155. #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
  156. extern const u8 ip_frag_ecn_table[16];
  157. #endif