inet_frag.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * @INET_FRAG_EVICTED: frag queue is being evicted
  21. */
  22. enum {
  23. INET_FRAG_FIRST_IN = BIT(0),
  24. INET_FRAG_LAST_IN = BIT(1),
  25. INET_FRAG_COMPLETE = BIT(2),
  26. INET_FRAG_EVICTED = BIT(3)
  27. };
  28. /**
  29. * struct inet_frag_queue - fragment queue
  30. *
  31. * @lock: spinlock protecting the queue
  32. * @timer: queue expiration timer
  33. * @list: hash bucket list
  34. * @refcnt: reference count of the queue
  35. * @fragments: received fragments head
  36. * @fragments_tail: received fragments tail
  37. * @stamp: timestamp of the last received fragment
  38. * @len: total length of the original datagram
  39. * @meat: length of received fragments so far
  40. * @flags: fragment queue flags
  41. * @max_size: maximum received fragment size
  42. * @net: namespace that this frag belongs to
  43. */
  44. struct inet_frag_queue {
  45. spinlock_t lock;
  46. struct timer_list timer;
  47. struct hlist_node list;
  48. atomic_t refcnt;
  49. struct sk_buff *fragments;
  50. struct sk_buff *fragments_tail;
  51. ktime_t stamp;
  52. int len;
  53. int meat;
  54. __u8 flags;
  55. u16 max_size;
  56. struct netns_frags *net;
  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. void inet_frags_init_net(struct netns_frags *nf);
  98. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
  99. void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
  100. void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f);
  101. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  102. struct inet_frags *f, void *key, unsigned int hash);
  103. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  104. const char *prefix);
  105. static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
  106. {
  107. if (atomic_dec_and_test(&q->refcnt))
  108. inet_frag_destroy(q, f);
  109. }
  110. /* Memory Tracking Functions. */
  111. /* The default percpu_counter batch size is not big enough to scale to
  112. * fragmentation mem acct sizes.
  113. * The mem size of a 64K fragment is approx:
  114. * (44 fragments * 2944 truesize) + frag_queue struct(200) = 129736 bytes
  115. */
  116. static unsigned int frag_percpu_counter_batch = 130000;
  117. static inline int frag_mem_limit(struct netns_frags *nf)
  118. {
  119. return percpu_counter_read(&nf->mem);
  120. }
  121. static inline void sub_frag_mem_limit(struct inet_frag_queue *q, int i)
  122. {
  123. __percpu_counter_add(&q->net->mem, -i, frag_percpu_counter_batch);
  124. }
  125. static inline void add_frag_mem_limit(struct inet_frag_queue *q, int i)
  126. {
  127. __percpu_counter_add(&q->net->mem, i, frag_percpu_counter_batch);
  128. }
  129. static inline void init_frag_mem_limit(struct netns_frags *nf)
  130. {
  131. percpu_counter_init(&nf->mem, 0, GFP_KERNEL);
  132. }
  133. static inline unsigned int sum_frag_mem_limit(struct netns_frags *nf)
  134. {
  135. unsigned int res;
  136. local_bh_disable();
  137. res = percpu_counter_sum_positive(&nf->mem);
  138. local_bh_enable();
  139. return res;
  140. }
  141. /* RFC 3168 support :
  142. * We want to check ECN values of all fragments, do detect invalid combinations.
  143. * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
  144. */
  145. #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
  146. #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
  147. #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
  148. #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
  149. extern const u8 ip_frag_ecn_table[16];
  150. #endif