inet_timewait_sock.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/kmemcheck.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/timer.h>
  21. #include <linux/types.h>
  22. #include <linux/workqueue.h>
  23. #include <net/inet_sock.h>
  24. #include <net/sock.h>
  25. #include <net/tcp_states.h>
  26. #include <net/timewait_sock.h>
  27. #include <asm/atomic.h>
  28. struct inet_hashinfo;
  29. #define INET_TWDR_RECYCLE_SLOTS_LOG 5
  30. #define INET_TWDR_RECYCLE_SLOTS (1 << INET_TWDR_RECYCLE_SLOTS_LOG)
  31. /*
  32. * If time > 4sec, it is "slow" path, no recycling is required,
  33. * so that we select tick to get range about 4 seconds.
  34. */
  35. #if HZ <= 16 || HZ > 4096
  36. # error Unsupported: HZ <= 16 or HZ > 4096
  37. #elif HZ <= 32
  38. # define INET_TWDR_RECYCLE_TICK (5 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  39. #elif HZ <= 64
  40. # define INET_TWDR_RECYCLE_TICK (6 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  41. #elif HZ <= 128
  42. # define INET_TWDR_RECYCLE_TICK (7 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  43. #elif HZ <= 256
  44. # define INET_TWDR_RECYCLE_TICK (8 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  45. #elif HZ <= 512
  46. # define INET_TWDR_RECYCLE_TICK (9 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  47. #elif HZ <= 1024
  48. # define INET_TWDR_RECYCLE_TICK (10 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  49. #elif HZ <= 2048
  50. # define INET_TWDR_RECYCLE_TICK (11 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  51. #else
  52. # define INET_TWDR_RECYCLE_TICK (12 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  53. #endif
  54. /* TIME_WAIT reaping mechanism. */
  55. #define INET_TWDR_TWKILL_SLOTS 8 /* Please keep this a power of 2. */
  56. #define INET_TWDR_TWKILL_QUOTA 100
  57. struct inet_timewait_death_row {
  58. /* Short-time timewait calendar */
  59. int twcal_hand;
  60. unsigned long twcal_jiffie;
  61. struct timer_list twcal_timer;
  62. struct hlist_head twcal_row[INET_TWDR_RECYCLE_SLOTS];
  63. spinlock_t death_lock;
  64. int tw_count;
  65. int period;
  66. u32 thread_slots;
  67. struct work_struct twkill_work;
  68. struct timer_list tw_timer;
  69. int slot;
  70. struct hlist_head cells[INET_TWDR_TWKILL_SLOTS];
  71. struct inet_hashinfo *hashinfo;
  72. int sysctl_tw_recycle;
  73. int sysctl_max_tw_buckets;
  74. };
  75. extern void inet_twdr_hangman(unsigned long data);
  76. extern void inet_twdr_twkill_work(struct work_struct *work);
  77. extern void inet_twdr_twcal_tick(unsigned long data);
  78. struct inet_bind_bucket;
  79. /*
  80. * This is a TIME_WAIT sock. It works around the memory consumption
  81. * problems of sockets in such a state on heavily loaded servers, but
  82. * without violating the protocol specification.
  83. */
  84. struct inet_timewait_sock {
  85. /*
  86. * Now struct sock also uses sock_common, so please just
  87. * don't add nothing before this first member (__tw_common) --acme
  88. */
  89. struct sock_common __tw_common;
  90. #define tw_family __tw_common.skc_family
  91. #define tw_state __tw_common.skc_state
  92. #define tw_reuse __tw_common.skc_reuse
  93. #define tw_bound_dev_if __tw_common.skc_bound_dev_if
  94. #define tw_node __tw_common.skc_nulls_node
  95. #define tw_bind_node __tw_common.skc_bind_node
  96. #define tw_refcnt __tw_common.skc_refcnt
  97. #define tw_hash __tw_common.skc_hash
  98. #define tw_prot __tw_common.skc_prot
  99. #define tw_net __tw_common.skc_net
  100. #define tw_daddr __tw_common.skc_daddr
  101. #define tw_rcv_saddr __tw_common.skc_rcv_saddr
  102. int tw_timeout;
  103. volatile unsigned char tw_substate;
  104. unsigned char tw_rcv_wscale;
  105. /* Socket demultiplex comparisons on incoming packets. */
  106. /* these three are in inet_sock */
  107. __be16 tw_sport;
  108. __be16 tw_dport;
  109. __u16 tw_num;
  110. kmemcheck_bitfield_begin(flags);
  111. /* And these are ours. */
  112. unsigned int tw_ipv6only : 1,
  113. tw_transparent : 1,
  114. tw_pad : 14, /* 14 bits hole */
  115. tw_ipv6_offset : 16;
  116. kmemcheck_bitfield_end(flags);
  117. unsigned long tw_ttd;
  118. struct inet_bind_bucket *tw_tb;
  119. struct hlist_node tw_death_node;
  120. };
  121. static inline void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw,
  122. struct hlist_nulls_head *list)
  123. {
  124. hlist_nulls_add_head_rcu(&tw->tw_node, list);
  125. }
  126. static inline void inet_twsk_add_bind_node(struct inet_timewait_sock *tw,
  127. struct hlist_head *list)
  128. {
  129. hlist_add_head(&tw->tw_bind_node, list);
  130. }
  131. static inline int inet_twsk_dead_hashed(const struct inet_timewait_sock *tw)
  132. {
  133. return !hlist_unhashed(&tw->tw_death_node);
  134. }
  135. static inline void inet_twsk_dead_node_init(struct inet_timewait_sock *tw)
  136. {
  137. tw->tw_death_node.pprev = NULL;
  138. }
  139. static inline void __inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  140. {
  141. __hlist_del(&tw->tw_death_node);
  142. inet_twsk_dead_node_init(tw);
  143. }
  144. static inline int inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  145. {
  146. if (inet_twsk_dead_hashed(tw)) {
  147. __inet_twsk_del_dead_node(tw);
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. #define inet_twsk_for_each(tw, node, head) \
  153. hlist_nulls_for_each_entry(tw, node, head, tw_node)
  154. #define inet_twsk_for_each_inmate(tw, node, jail) \
  155. hlist_for_each_entry(tw, node, jail, tw_death_node)
  156. #define inet_twsk_for_each_inmate_safe(tw, node, safe, jail) \
  157. hlist_for_each_entry_safe(tw, node, safe, jail, tw_death_node)
  158. static inline struct inet_timewait_sock *inet_twsk(const struct sock *sk)
  159. {
  160. return (struct inet_timewait_sock *)sk;
  161. }
  162. static inline __be32 sk_rcv_saddr(const struct sock *sk)
  163. {
  164. /* both inet_sk() and inet_twsk() store rcv_saddr in skc_rcv_saddr */
  165. return sk->__sk_common.skc_rcv_saddr;
  166. }
  167. extern void inet_twsk_put(struct inet_timewait_sock *tw);
  168. extern int inet_twsk_unhash(struct inet_timewait_sock *tw);
  169. extern int inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
  170. struct inet_hashinfo *hashinfo);
  171. extern struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
  172. const int state);
  173. extern void __inet_twsk_hashdance(struct inet_timewait_sock *tw,
  174. struct sock *sk,
  175. struct inet_hashinfo *hashinfo);
  176. extern void inet_twsk_schedule(struct inet_timewait_sock *tw,
  177. struct inet_timewait_death_row *twdr,
  178. const int timeo, const int timewait_len);
  179. extern void inet_twsk_deschedule(struct inet_timewait_sock *tw,
  180. struct inet_timewait_death_row *twdr);
  181. extern void inet_twsk_purge(struct inet_hashinfo *hashinfo,
  182. struct inet_timewait_death_row *twdr, int family);
  183. static inline
  184. struct net *twsk_net(const struct inet_timewait_sock *twsk)
  185. {
  186. #ifdef CONFIG_NET_NS
  187. return rcu_dereference_raw(twsk->tw_net); /* protected by locking, */
  188. /* reference counting, */
  189. /* initialization, or RCU. */
  190. #else
  191. return &init_net;
  192. #endif
  193. }
  194. static inline
  195. void twsk_net_set(struct inet_timewait_sock *twsk, struct net *net)
  196. {
  197. #ifdef CONFIG_NET_NS
  198. rcu_assign_pointer(twsk->tw_net, net);
  199. #endif
  200. }
  201. #endif /* _INET_TIMEWAIT_SOCK_ */