inet_timewait_sock.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Definitions for a generic INET TIMEWAIT sock
  7. *
  8. * From code originally in net/tcp.h
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #ifndef _INET_TIMEWAIT_SOCK_
  16. #define _INET_TIMEWAIT_SOCK_
  17. #include <linux/config.h>
  18. #include <linux/ip.h>
  19. #include <linux/list.h>
  20. #include <linux/timer.h>
  21. #include <linux/types.h>
  22. #include <linux/workqueue.h>
  23. #include <net/sock.h>
  24. #include <net/tcp_states.h>
  25. #include <asm/atomic.h>
  26. struct inet_hashinfo;
  27. #define INET_TWDR_RECYCLE_SLOTS_LOG 5
  28. #define INET_TWDR_RECYCLE_SLOTS (1 << INET_TWDR_RECYCLE_SLOTS_LOG)
  29. /*
  30. * If time > 4sec, it is "slow" path, no recycling is required,
  31. * so that we select tick to get range about 4 seconds.
  32. */
  33. #if HZ <= 16 || HZ > 4096
  34. # error Unsupported: HZ <= 16 or HZ > 4096
  35. #elif HZ <= 32
  36. # define INET_TWDR_RECYCLE_TICK (5 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  37. #elif HZ <= 64
  38. # define INET_TWDR_RECYCLE_TICK (6 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  39. #elif HZ <= 128
  40. # define INET_TWDR_RECYCLE_TICK (7 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  41. #elif HZ <= 256
  42. # define INET_TWDR_RECYCLE_TICK (8 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  43. #elif HZ <= 512
  44. # define INET_TWDR_RECYCLE_TICK (9 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  45. #elif HZ <= 1024
  46. # define INET_TWDR_RECYCLE_TICK (10 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  47. #elif HZ <= 2048
  48. # define INET_TWDR_RECYCLE_TICK (11 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  49. #else
  50. # define INET_TWDR_RECYCLE_TICK (12 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  51. #endif
  52. /* TIME_WAIT reaping mechanism. */
  53. #define INET_TWDR_TWKILL_SLOTS 8 /* Please keep this a power of 2. */
  54. #define INET_TWDR_TWKILL_QUOTA 100
  55. struct inet_timewait_death_row {
  56. /* Short-time timewait calendar */
  57. int twcal_hand;
  58. int twcal_jiffie;
  59. struct timer_list twcal_timer;
  60. struct hlist_head twcal_row[INET_TWDR_RECYCLE_SLOTS];
  61. spinlock_t death_lock;
  62. int tw_count;
  63. int period;
  64. u32 thread_slots;
  65. struct work_struct twkill_work;
  66. struct timer_list tw_timer;
  67. int slot;
  68. struct hlist_head cells[INET_TWDR_TWKILL_SLOTS];
  69. struct inet_hashinfo *hashinfo;
  70. int sysctl_tw_recycle;
  71. int sysctl_max_tw_buckets;
  72. };
  73. extern void inet_twdr_hangman(unsigned long data);
  74. extern void inet_twdr_twkill_work(void *data);
  75. extern void inet_twdr_twcal_tick(unsigned long data);
  76. #if (BITS_PER_LONG == 64)
  77. #define INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES 8
  78. #else
  79. #define INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES 4
  80. #endif
  81. struct inet_bind_bucket;
  82. /*
  83. * This is a TIME_WAIT sock. It works around the memory consumption
  84. * problems of sockets in such a state on heavily loaded servers, but
  85. * without violating the protocol specification.
  86. */
  87. struct inet_timewait_sock {
  88. /*
  89. * Now struct sock also uses sock_common, so please just
  90. * don't add nothing before this first member (__tw_common) --acme
  91. */
  92. struct sock_common __tw_common;
  93. #define tw_family __tw_common.skc_family
  94. #define tw_state __tw_common.skc_state
  95. #define tw_reuse __tw_common.skc_reuse
  96. #define tw_bound_dev_if __tw_common.skc_bound_dev_if
  97. #define tw_node __tw_common.skc_node
  98. #define tw_bind_node __tw_common.skc_bind_node
  99. #define tw_refcnt __tw_common.skc_refcnt
  100. #define tw_prot __tw_common.skc_prot
  101. volatile unsigned char tw_substate;
  102. /* 3 bits hole, try to pack */
  103. unsigned char tw_rcv_wscale;
  104. /* Socket demultiplex comparisons on incoming packets. */
  105. /* these five are in inet_sock */
  106. __u16 tw_sport;
  107. __u32 tw_daddr __attribute__((aligned(INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES)));
  108. __u32 tw_rcv_saddr;
  109. __u16 tw_dport;
  110. __u16 tw_num;
  111. /* And these are ours. */
  112. __u8 tw_ipv6only:1;
  113. /* 31 bits hole, try to pack */
  114. int tw_hashent;
  115. int tw_timeout;
  116. unsigned long tw_ttd;
  117. struct inet_bind_bucket *tw_tb;
  118. struct hlist_node tw_death_node;
  119. };
  120. static inline void inet_twsk_add_node(struct inet_timewait_sock *tw,
  121. struct hlist_head *list)
  122. {
  123. hlist_add_head(&tw->tw_node, list);
  124. }
  125. static inline void inet_twsk_add_bind_node(struct inet_timewait_sock *tw,
  126. struct hlist_head *list)
  127. {
  128. hlist_add_head(&tw->tw_bind_node, list);
  129. }
  130. static inline int inet_twsk_dead_hashed(const struct inet_timewait_sock *tw)
  131. {
  132. return tw->tw_death_node.pprev != NULL;
  133. }
  134. static inline void inet_twsk_dead_node_init(struct inet_timewait_sock *tw)
  135. {
  136. tw->tw_death_node.pprev = NULL;
  137. }
  138. static inline void __inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  139. {
  140. __hlist_del(&tw->tw_death_node);
  141. inet_twsk_dead_node_init(tw);
  142. }
  143. static inline int inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  144. {
  145. if (inet_twsk_dead_hashed(tw)) {
  146. __inet_twsk_del_dead_node(tw);
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. #define inet_twsk_for_each(tw, node, head) \
  152. hlist_for_each_entry(tw, node, head, tw_node)
  153. #define inet_twsk_for_each_inmate(tw, node, jail) \
  154. hlist_for_each_entry(tw, node, jail, tw_death_node)
  155. #define inet_twsk_for_each_inmate_safe(tw, node, safe, jail) \
  156. hlist_for_each_entry_safe(tw, node, safe, jail, tw_death_node)
  157. static inline struct inet_timewait_sock *inet_twsk(const struct sock *sk)
  158. {
  159. return (struct inet_timewait_sock *)sk;
  160. }
  161. static inline u32 inet_rcv_saddr(const struct sock *sk)
  162. {
  163. return likely(sk->sk_state != TCP_TIME_WAIT) ?
  164. inet_sk(sk)->rcv_saddr : inet_twsk(sk)->tw_rcv_saddr;
  165. }
  166. static inline void inet_twsk_put(struct inet_timewait_sock *tw)
  167. {
  168. if (atomic_dec_and_test(&tw->tw_refcnt)) {
  169. #ifdef SOCK_REFCNT_DEBUG
  170. printk(KERN_DEBUG "%s timewait_sock %p released\n",
  171. tw->tw_prot->name, tw);
  172. #endif
  173. kmem_cache_free(tw->tw_prot->twsk_slab, tw);
  174. }
  175. }
  176. extern struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
  177. const int state);
  178. extern void __inet_twsk_kill(struct inet_timewait_sock *tw,
  179. struct inet_hashinfo *hashinfo);
  180. extern void __inet_twsk_hashdance(struct inet_timewait_sock *tw,
  181. struct sock *sk,
  182. struct inet_hashinfo *hashinfo);
  183. extern void inet_twsk_schedule(struct inet_timewait_sock *tw,
  184. struct inet_timewait_death_row *twdr,
  185. const int timeo, const int timewait_len);
  186. extern void inet_twsk_deschedule(struct inet_timewait_sock *tw,
  187. struct inet_timewait_death_row *twdr);
  188. #endif /* _INET_TIMEWAIT_SOCK_ */