inet_frag.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #ifndef __NET_FRAG_H__
  2. #define __NET_FRAG_H__
  3. #include <linux/percpu_counter.h>
  4. struct netns_frags {
  5. int nqueues;
  6. struct list_head lru_list;
  7. spinlock_t lru_lock;
  8. /* The percpu_counter "mem" need to be cacheline aligned.
  9. * mem.count must not share cacheline with other writers
  10. */
  11. struct percpu_counter mem ____cacheline_aligned_in_smp;
  12. /* sysctls */
  13. int timeout;
  14. int high_thresh;
  15. int low_thresh;
  16. };
  17. struct inet_frag_queue {
  18. spinlock_t lock;
  19. struct timer_list timer; /* when will this queue expire? */
  20. struct list_head lru_list; /* lru list member */
  21. struct hlist_node list;
  22. atomic_t refcnt;
  23. struct sk_buff *fragments; /* list of received fragments */
  24. struct sk_buff *fragments_tail;
  25. ktime_t stamp;
  26. int len; /* total length of orig datagram */
  27. int meat;
  28. __u8 last_in; /* first/last segment arrived? */
  29. #define INET_FRAG_COMPLETE 4
  30. #define INET_FRAG_FIRST_IN 2
  31. #define INET_FRAG_LAST_IN 1
  32. u16 max_size;
  33. struct netns_frags *net;
  34. };
  35. #define INETFRAGS_HASHSZ 1024
  36. /* averaged:
  37. * max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
  38. * rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
  39. * struct frag_queue))
  40. */
  41. #define INETFRAGS_MAXDEPTH 128
  42. struct inet_frag_bucket {
  43. struct hlist_head chain;
  44. spinlock_t chain_lock;
  45. };
  46. struct inet_frags {
  47. struct inet_frag_bucket hash[INETFRAGS_HASHSZ];
  48. /* This rwlock is a global lock (seperate per IPv4, IPv6 and
  49. * netfilter). Important to keep this on a seperate cacheline.
  50. * Its primarily a rebuild protection rwlock.
  51. */
  52. rwlock_t lock ____cacheline_aligned_in_smp;
  53. int secret_interval;
  54. struct timer_list secret_timer;
  55. /* The first call to hashfn is responsible to initialize
  56. * rnd. This is best done with net_get_random_once.
  57. */
  58. u32 rnd;
  59. int qsize;
  60. unsigned int (*hashfn)(struct inet_frag_queue *);
  61. bool (*match)(struct inet_frag_queue *q, void *arg);
  62. void (*constructor)(struct inet_frag_queue *q,
  63. void *arg);
  64. void (*destructor)(struct inet_frag_queue *);
  65. void (*skb_free)(struct sk_buff *);
  66. void (*frag_expire)(unsigned long data);
  67. };
  68. void inet_frags_init(struct inet_frags *);
  69. void inet_frags_fini(struct inet_frags *);
  70. void inet_frags_init_net(struct netns_frags *nf);
  71. void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
  72. void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
  73. void inet_frag_destroy(struct inet_frag_queue *q,
  74. struct inet_frags *f, int *work);
  75. int inet_frag_evictor(struct netns_frags *nf, struct inet_frags *f, bool force);
  76. struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
  77. struct inet_frags *f, void *key, unsigned int hash)
  78. __releases(&f->lock);
  79. void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
  80. const char *prefix);
  81. static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
  82. {
  83. if (atomic_dec_and_test(&q->refcnt))
  84. inet_frag_destroy(q, f, NULL);
  85. }
  86. /* Memory Tracking Functions. */
  87. /* The default percpu_counter batch size is not big enough to scale to
  88. * fragmentation mem acct sizes.
  89. * The mem size of a 64K fragment is approx:
  90. * (44 fragments * 2944 truesize) + frag_queue struct(200) = 129736 bytes
  91. */
  92. static unsigned int frag_percpu_counter_batch = 130000;
  93. static inline int frag_mem_limit(struct netns_frags *nf)
  94. {
  95. return percpu_counter_read(&nf->mem);
  96. }
  97. static inline void sub_frag_mem_limit(struct inet_frag_queue *q, int i)
  98. {
  99. __percpu_counter_add(&q->net->mem, -i, frag_percpu_counter_batch);
  100. }
  101. static inline void add_frag_mem_limit(struct inet_frag_queue *q, int i)
  102. {
  103. __percpu_counter_add(&q->net->mem, i, frag_percpu_counter_batch);
  104. }
  105. static inline void init_frag_mem_limit(struct netns_frags *nf)
  106. {
  107. percpu_counter_init(&nf->mem, 0);
  108. }
  109. static inline int sum_frag_mem_limit(struct netns_frags *nf)
  110. {
  111. int res;
  112. local_bh_disable();
  113. res = percpu_counter_sum_positive(&nf->mem);
  114. local_bh_enable();
  115. return res;
  116. }
  117. static inline void inet_frag_lru_move(struct inet_frag_queue *q)
  118. {
  119. spin_lock(&q->net->lru_lock);
  120. if (!list_empty(&q->lru_list))
  121. list_move_tail(&q->lru_list, &q->net->lru_list);
  122. spin_unlock(&q->net->lru_lock);
  123. }
  124. static inline void inet_frag_lru_del(struct inet_frag_queue *q)
  125. {
  126. spin_lock(&q->net->lru_lock);
  127. list_del_init(&q->lru_list);
  128. q->net->nqueues--;
  129. spin_unlock(&q->net->lru_lock);
  130. }
  131. static inline void inet_frag_lru_add(struct netns_frags *nf,
  132. struct inet_frag_queue *q)
  133. {
  134. spin_lock(&nf->lru_lock);
  135. list_add_tail(&q->lru_list, &nf->lru_list);
  136. q->net->nqueues++;
  137. spin_unlock(&nf->lru_lock);
  138. }
  139. /* RFC 3168 support :
  140. * We want to check ECN values of all fragments, do detect invalid combinations.
  141. * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
  142. */
  143. #define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
  144. #define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
  145. #define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
  146. #define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
  147. extern const u8 ip_frag_ecn_table[16];
  148. #endif