request_sock.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * NET Generic infrastructure for Network protocols.
  3. *
  4. * Definitions for request_sock
  5. *
  6. * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  7. *
  8. * From code originally in include/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 _REQUEST_SOCK_H
  16. #define _REQUEST_SOCK_H
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/types.h>
  20. #include <net/sock.h>
  21. struct request_sock;
  22. struct sk_buff;
  23. struct dst_entry;
  24. struct proto;
  25. struct request_sock_ops {
  26. int family;
  27. kmem_cache_t *slab;
  28. int obj_size;
  29. int (*rtx_syn_ack)(struct sock *sk,
  30. struct request_sock *req,
  31. struct dst_entry *dst);
  32. void (*send_ack)(struct sk_buff *skb,
  33. struct request_sock *req);
  34. void (*send_reset)(struct sk_buff *skb);
  35. void (*destructor)(struct request_sock *req);
  36. };
  37. /* struct request_sock - mini sock to represent a connection request
  38. */
  39. struct request_sock {
  40. struct request_sock *dl_next; /* Must be first member! */
  41. u16 mss;
  42. u8 retrans;
  43. u8 __pad;
  44. /* The following two fields can be easily recomputed I think -AK */
  45. u32 window_clamp; /* window clamp at creation time */
  46. u32 rcv_wnd; /* rcv_wnd offered first time */
  47. u32 ts_recent;
  48. unsigned long expires;
  49. struct request_sock_ops *rsk_ops;
  50. struct sock *sk;
  51. u32 secid;
  52. u32 peer_secid;
  53. };
  54. static inline struct request_sock *reqsk_alloc(struct request_sock_ops *ops)
  55. {
  56. struct request_sock *req = kmem_cache_alloc(ops->slab, SLAB_ATOMIC);
  57. if (req != NULL)
  58. req->rsk_ops = ops;
  59. return req;
  60. }
  61. static inline void __reqsk_free(struct request_sock *req)
  62. {
  63. kmem_cache_free(req->rsk_ops->slab, req);
  64. }
  65. static inline void reqsk_free(struct request_sock *req)
  66. {
  67. req->rsk_ops->destructor(req);
  68. __reqsk_free(req);
  69. }
  70. extern int sysctl_max_syn_backlog;
  71. /** struct listen_sock - listen state
  72. *
  73. * @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs
  74. */
  75. struct listen_sock {
  76. u8 max_qlen_log;
  77. /* 3 bytes hole, try to use */
  78. int qlen;
  79. int qlen_young;
  80. int clock_hand;
  81. u32 hash_rnd;
  82. u32 nr_table_entries;
  83. struct request_sock *syn_table[0];
  84. };
  85. /** struct request_sock_queue - queue of request_socks
  86. *
  87. * @rskq_accept_head - FIFO head of established children
  88. * @rskq_accept_tail - FIFO tail of established children
  89. * @rskq_defer_accept - User waits for some data after accept()
  90. * @syn_wait_lock - serializer
  91. *
  92. * %syn_wait_lock is necessary only to avoid proc interface having to grab the main
  93. * lock sock while browsing the listening hash (otherwise it's deadlock prone).
  94. *
  95. * This lock is acquired in read mode only from listening_get_next() seq_file
  96. * op and it's acquired in write mode _only_ from code that is actively
  97. * changing rskq_accept_head. All readers that are holding the master sock lock
  98. * don't need to grab this lock in read mode too as rskq_accept_head. writes
  99. * are always protected from the main sock lock.
  100. */
  101. struct request_sock_queue {
  102. struct request_sock *rskq_accept_head;
  103. struct request_sock *rskq_accept_tail;
  104. rwlock_t syn_wait_lock;
  105. u8 rskq_defer_accept;
  106. /* 3 bytes hole, try to pack */
  107. struct listen_sock *listen_opt;
  108. };
  109. extern int reqsk_queue_alloc(struct request_sock_queue *queue,
  110. const int nr_table_entries);
  111. static inline struct listen_sock *reqsk_queue_yank_listen_sk(struct request_sock_queue *queue)
  112. {
  113. struct listen_sock *lopt;
  114. write_lock_bh(&queue->syn_wait_lock);
  115. lopt = queue->listen_opt;
  116. queue->listen_opt = NULL;
  117. write_unlock_bh(&queue->syn_wait_lock);
  118. return lopt;
  119. }
  120. static inline void __reqsk_queue_destroy(struct request_sock_queue *queue)
  121. {
  122. kfree(reqsk_queue_yank_listen_sk(queue));
  123. }
  124. extern void reqsk_queue_destroy(struct request_sock_queue *queue);
  125. static inline struct request_sock *
  126. reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
  127. {
  128. struct request_sock *req = queue->rskq_accept_head;
  129. queue->rskq_accept_head = NULL;
  130. return req;
  131. }
  132. static inline int reqsk_queue_empty(struct request_sock_queue *queue)
  133. {
  134. return queue->rskq_accept_head == NULL;
  135. }
  136. static inline void reqsk_queue_unlink(struct request_sock_queue *queue,
  137. struct request_sock *req,
  138. struct request_sock **prev_req)
  139. {
  140. write_lock(&queue->syn_wait_lock);
  141. *prev_req = req->dl_next;
  142. write_unlock(&queue->syn_wait_lock);
  143. }
  144. static inline void reqsk_queue_add(struct request_sock_queue *queue,
  145. struct request_sock *req,
  146. struct sock *parent,
  147. struct sock *child)
  148. {
  149. req->sk = child;
  150. sk_acceptq_added(parent);
  151. if (queue->rskq_accept_head == NULL)
  152. queue->rskq_accept_head = req;
  153. else
  154. queue->rskq_accept_tail->dl_next = req;
  155. queue->rskq_accept_tail = req;
  156. req->dl_next = NULL;
  157. }
  158. static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue)
  159. {
  160. struct request_sock *req = queue->rskq_accept_head;
  161. BUG_TRAP(req != NULL);
  162. queue->rskq_accept_head = req->dl_next;
  163. if (queue->rskq_accept_head == NULL)
  164. queue->rskq_accept_tail = NULL;
  165. return req;
  166. }
  167. static inline struct sock *reqsk_queue_get_child(struct request_sock_queue *queue,
  168. struct sock *parent)
  169. {
  170. struct request_sock *req = reqsk_queue_remove(queue);
  171. struct sock *child = req->sk;
  172. BUG_TRAP(child != NULL);
  173. sk_acceptq_removed(parent);
  174. __reqsk_free(req);
  175. return child;
  176. }
  177. static inline int reqsk_queue_removed(struct request_sock_queue *queue,
  178. struct request_sock *req)
  179. {
  180. struct listen_sock *lopt = queue->listen_opt;
  181. if (req->retrans == 0)
  182. --lopt->qlen_young;
  183. return --lopt->qlen;
  184. }
  185. static inline int reqsk_queue_added(struct request_sock_queue *queue)
  186. {
  187. struct listen_sock *lopt = queue->listen_opt;
  188. const int prev_qlen = lopt->qlen;
  189. lopt->qlen_young++;
  190. lopt->qlen++;
  191. return prev_qlen;
  192. }
  193. static inline int reqsk_queue_len(const struct request_sock_queue *queue)
  194. {
  195. return queue->listen_opt != NULL ? queue->listen_opt->qlen : 0;
  196. }
  197. static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
  198. {
  199. return queue->listen_opt->qlen_young;
  200. }
  201. static inline int reqsk_queue_is_full(const struct request_sock_queue *queue)
  202. {
  203. return queue->listen_opt->qlen >> queue->listen_opt->max_qlen_log;
  204. }
  205. static inline void reqsk_queue_hash_req(struct request_sock_queue *queue,
  206. u32 hash, struct request_sock *req,
  207. unsigned long timeout)
  208. {
  209. struct listen_sock *lopt = queue->listen_opt;
  210. req->expires = jiffies + timeout;
  211. req->retrans = 0;
  212. req->sk = NULL;
  213. req->dl_next = lopt->syn_table[hash];
  214. write_lock(&queue->syn_wait_lock);
  215. lopt->syn_table[hash] = req;
  216. write_unlock(&queue->syn_wait_lock);
  217. }
  218. #endif /* _REQUEST_SOCK_H */