inet_timewait_sock.h 6.4 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/kmemcheck.h>
  18. #include <linux/list.h>
  19. #include <linux/timer.h>
  20. #include <linux/types.h>
  21. #include <linux/workqueue.h>
  22. #include <net/inet_sock.h>
  23. #include <net/sock.h>
  24. #include <net/tcp_states.h>
  25. #include <net/timewait_sock.h>
  26. #include <linux/atomic.h>
  27. struct inet_hashinfo;
  28. #define INET_TWDR_RECYCLE_SLOTS_LOG 5
  29. #define INET_TWDR_RECYCLE_SLOTS (1 << INET_TWDR_RECYCLE_SLOTS_LOG)
  30. /*
  31. * If time > 4sec, it is "slow" path, no recycling is required,
  32. * so that we select tick to get range about 4 seconds.
  33. */
  34. #if HZ <= 16 || HZ > 4096
  35. # error Unsupported: HZ <= 16 or HZ > 4096
  36. #elif HZ <= 32
  37. # define INET_TWDR_RECYCLE_TICK (5 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  38. #elif HZ <= 64
  39. # define INET_TWDR_RECYCLE_TICK (6 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  40. #elif HZ <= 128
  41. # define INET_TWDR_RECYCLE_TICK (7 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  42. #elif HZ <= 256
  43. # define INET_TWDR_RECYCLE_TICK (8 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  44. #elif HZ <= 512
  45. # define INET_TWDR_RECYCLE_TICK (9 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  46. #elif HZ <= 1024
  47. # define INET_TWDR_RECYCLE_TICK (10 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  48. #elif HZ <= 2048
  49. # define INET_TWDR_RECYCLE_TICK (11 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  50. #else
  51. # define INET_TWDR_RECYCLE_TICK (12 + 2 - INET_TWDR_RECYCLE_SLOTS_LOG)
  52. #endif
  53. static inline u32 inet_tw_time_stamp(void)
  54. {
  55. return jiffies;
  56. }
  57. /* TIME_WAIT reaping mechanism. */
  58. #define INET_TWDR_TWKILL_SLOTS 8 /* Please keep this a power of 2. */
  59. #define INET_TWDR_TWKILL_QUOTA 100
  60. struct inet_timewait_death_row {
  61. /* Short-time timewait calendar */
  62. int twcal_hand;
  63. unsigned long twcal_jiffie;
  64. struct timer_list twcal_timer;
  65. struct hlist_head twcal_row[INET_TWDR_RECYCLE_SLOTS];
  66. spinlock_t death_lock;
  67. int tw_count;
  68. int period;
  69. u32 thread_slots;
  70. struct work_struct twkill_work;
  71. struct timer_list tw_timer;
  72. int slot;
  73. struct hlist_head cells[INET_TWDR_TWKILL_SLOTS];
  74. struct inet_hashinfo *hashinfo;
  75. int sysctl_tw_recycle;
  76. int sysctl_max_tw_buckets;
  77. };
  78. void inet_twdr_hangman(unsigned long data);
  79. void inet_twdr_twkill_work(struct work_struct *work);
  80. void inet_twdr_twcal_tick(unsigned long data);
  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_nulls_node
  98. #define tw_bind_node __tw_common.skc_bind_node
  99. #define tw_refcnt __tw_common.skc_refcnt
  100. #define tw_hash __tw_common.skc_hash
  101. #define tw_prot __tw_common.skc_prot
  102. #define tw_net __tw_common.skc_net
  103. #define tw_daddr __tw_common.skc_daddr
  104. #define tw_v6_daddr __tw_common.skc_v6_daddr
  105. #define tw_rcv_saddr __tw_common.skc_rcv_saddr
  106. #define tw_v6_rcv_saddr __tw_common.skc_v6_rcv_saddr
  107. #define tw_dport __tw_common.skc_dport
  108. #define tw_num __tw_common.skc_num
  109. int tw_timeout;
  110. volatile unsigned char tw_substate;
  111. unsigned char tw_rcv_wscale;
  112. /* Socket demultiplex comparisons on incoming packets. */
  113. /* these three are in inet_sock */
  114. __be16 tw_sport;
  115. kmemcheck_bitfield_begin(flags);
  116. /* And these are ours. */
  117. unsigned int tw_ipv6only : 1,
  118. tw_transparent : 1,
  119. tw_pad : 6, /* 6 bits hole */
  120. tw_tos : 8,
  121. tw_pad2 : 16; /* 16 bits hole */
  122. kmemcheck_bitfield_end(flags);
  123. u32 tw_ttd;
  124. struct inet_bind_bucket *tw_tb;
  125. struct hlist_node tw_death_node;
  126. };
  127. #define tw_tclass tw_tos
  128. static inline int inet_twsk_dead_hashed(const struct inet_timewait_sock *tw)
  129. {
  130. return !hlist_unhashed(&tw->tw_death_node);
  131. }
  132. static inline void inet_twsk_dead_node_init(struct inet_timewait_sock *tw)
  133. {
  134. tw->tw_death_node.pprev = NULL;
  135. }
  136. static inline void __inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  137. {
  138. __hlist_del(&tw->tw_death_node);
  139. inet_twsk_dead_node_init(tw);
  140. }
  141. static inline int inet_twsk_del_dead_node(struct inet_timewait_sock *tw)
  142. {
  143. if (inet_twsk_dead_hashed(tw)) {
  144. __inet_twsk_del_dead_node(tw);
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. #define inet_twsk_for_each(tw, node, head) \
  150. hlist_nulls_for_each_entry(tw, node, head, tw_node)
  151. #define inet_twsk_for_each_inmate(tw, jail) \
  152. hlist_for_each_entry(tw, jail, tw_death_node)
  153. #define inet_twsk_for_each_inmate_safe(tw, safe, jail) \
  154. hlist_for_each_entry_safe(tw, safe, jail, tw_death_node)
  155. static inline struct inet_timewait_sock *inet_twsk(const struct sock *sk)
  156. {
  157. return (struct inet_timewait_sock *)sk;
  158. }
  159. void inet_twsk_free(struct inet_timewait_sock *tw);
  160. void inet_twsk_put(struct inet_timewait_sock *tw);
  161. int inet_twsk_unhash(struct inet_timewait_sock *tw);
  162. int inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
  163. struct inet_hashinfo *hashinfo);
  164. struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
  165. const int state);
  166. void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
  167. struct inet_hashinfo *hashinfo);
  168. void inet_twsk_schedule(struct inet_timewait_sock *tw,
  169. struct inet_timewait_death_row *twdr,
  170. const int timeo, const int timewait_len);
  171. void inet_twsk_deschedule(struct inet_timewait_sock *tw,
  172. struct inet_timewait_death_row *twdr);
  173. void inet_twsk_purge(struct inet_hashinfo *hashinfo,
  174. struct inet_timewait_death_row *twdr, int family);
  175. static inline
  176. struct net *twsk_net(const struct inet_timewait_sock *twsk)
  177. {
  178. return read_pnet(&twsk->tw_net);
  179. }
  180. static inline
  181. void twsk_net_set(struct inet_timewait_sock *twsk, struct net *net)
  182. {
  183. write_pnet(&twsk->tw_net, net);
  184. }
  185. #endif /* _INET_TIMEWAIT_SOCK_ */