stream.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SUCS NET3:
  4. *
  5. * Generic stream handling routines. These are generic for most
  6. * protocols. Even IP. Tonight 8-).
  7. * This is used because TCP, LLC (others too) layer all have mostly
  8. * identical sendmsg() and recvmsg() code.
  9. * So we (will) share it here.
  10. *
  11. * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  12. * (from old tcp.c code)
  13. * Alan Cox <alan@lxorguk.ukuu.org.uk> (Borrowed comments 8-))
  14. */
  15. #include <linux/module.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/net.h>
  18. #include <linux/signal.h>
  19. #include <linux/tcp.h>
  20. #include <linux/wait.h>
  21. #include <net/sock.h>
  22. /**
  23. * sk_stream_write_space - stream socket write_space callback.
  24. * @sk: socket
  25. *
  26. * FIXME: write proper description
  27. */
  28. void sk_stream_write_space(struct sock *sk)
  29. {
  30. struct socket *sock = sk->sk_socket;
  31. struct socket_wq *wq;
  32. if (__sk_stream_is_writeable(sk, 1) && sock) {
  33. clear_bit(SOCK_NOSPACE, &sock->flags);
  34. rcu_read_lock();
  35. wq = rcu_dereference(sk->sk_wq);
  36. if (skwq_has_sleeper(wq))
  37. wake_up_interruptible_poll(&wq->wait, EPOLLOUT |
  38. EPOLLWRNORM | EPOLLWRBAND);
  39. if (wq && wq->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
  40. sock_wake_async(wq, SOCK_WAKE_SPACE, POLL_OUT);
  41. rcu_read_unlock();
  42. }
  43. }
  44. /**
  45. * sk_stream_wait_connect - Wait for a socket to get into the connected state
  46. * @sk: sock to wait on
  47. * @timeo_p: for how long to wait
  48. *
  49. * Must be called with the socket locked.
  50. */
  51. int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
  52. {
  53. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  54. struct task_struct *tsk = current;
  55. int done;
  56. do {
  57. int err = sock_error(sk);
  58. if (err)
  59. return err;
  60. if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
  61. return -EPIPE;
  62. if (!*timeo_p)
  63. return -EAGAIN;
  64. if (signal_pending(tsk))
  65. return sock_intr_errno(*timeo_p);
  66. add_wait_queue(sk_sleep(sk), &wait);
  67. sk->sk_write_pending++;
  68. done = sk_wait_event(sk, timeo_p,
  69. !READ_ONCE(sk->sk_err) &&
  70. !((1 << READ_ONCE(sk->sk_state)) &
  71. ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)), &wait);
  72. remove_wait_queue(sk_sleep(sk), &wait);
  73. sk->sk_write_pending--;
  74. } while (!done);
  75. return done < 0 ? done : 0;
  76. }
  77. EXPORT_SYMBOL(sk_stream_wait_connect);
  78. /**
  79. * sk_stream_closing - Return 1 if we still have things to send in our buffers.
  80. * @sk: socket to verify
  81. */
  82. static int sk_stream_closing(const struct sock *sk)
  83. {
  84. return (1 << READ_ONCE(sk->sk_state)) &
  85. (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK);
  86. }
  87. void sk_stream_wait_close(struct sock *sk, long timeout)
  88. {
  89. if (timeout) {
  90. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  91. add_wait_queue(sk_sleep(sk), &wait);
  92. do {
  93. if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk), &wait))
  94. break;
  95. } while (!signal_pending(current) && timeout);
  96. remove_wait_queue(sk_sleep(sk), &wait);
  97. }
  98. }
  99. EXPORT_SYMBOL(sk_stream_wait_close);
  100. /**
  101. * sk_stream_wait_memory - Wait for more memory for a socket
  102. * @sk: socket to wait for memory
  103. * @timeo_p: for how long
  104. */
  105. int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
  106. {
  107. int ret, err = 0;
  108. long vm_wait = 0;
  109. long current_timeo = *timeo_p;
  110. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  111. if (sk_stream_memory_free(sk))
  112. current_timeo = vm_wait = get_random_u32_below(HZ / 5) + 2;
  113. add_wait_queue(sk_sleep(sk), &wait);
  114. while (1) {
  115. sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  116. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  117. goto do_error;
  118. if (!*timeo_p)
  119. goto do_eagain;
  120. if (signal_pending(current))
  121. goto do_interrupted;
  122. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  123. if (sk_stream_memory_free(sk) && !vm_wait)
  124. break;
  125. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  126. sk->sk_write_pending++;
  127. ret = sk_wait_event(sk, &current_timeo, READ_ONCE(sk->sk_err) ||
  128. (READ_ONCE(sk->sk_shutdown) & SEND_SHUTDOWN) ||
  129. (sk_stream_memory_free(sk) && !vm_wait),
  130. &wait);
  131. sk->sk_write_pending--;
  132. if (ret < 0)
  133. goto do_error;
  134. if (vm_wait) {
  135. vm_wait -= current_timeo;
  136. current_timeo = *timeo_p;
  137. if (current_timeo != MAX_SCHEDULE_TIMEOUT &&
  138. (current_timeo -= vm_wait) < 0)
  139. current_timeo = 0;
  140. vm_wait = 0;
  141. }
  142. *timeo_p = current_timeo;
  143. }
  144. out:
  145. if (!sock_flag(sk, SOCK_DEAD))
  146. remove_wait_queue(sk_sleep(sk), &wait);
  147. return err;
  148. do_error:
  149. err = -EPIPE;
  150. goto out;
  151. do_eagain:
  152. /* Make sure that whenever EAGAIN is returned, EPOLLOUT event can
  153. * be generated later.
  154. * When TCP receives ACK packets that make room, tcp_check_space()
  155. * only calls tcp_new_space() if SOCK_NOSPACE is set.
  156. */
  157. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  158. err = -EAGAIN;
  159. goto out;
  160. do_interrupted:
  161. err = sock_intr_errno(*timeo_p);
  162. goto out;
  163. }
  164. EXPORT_SYMBOL(sk_stream_wait_memory);
  165. int sk_stream_error(struct sock *sk, int flags, int err)
  166. {
  167. if (err == -EPIPE)
  168. err = sock_error(sk) ? : -EPIPE;
  169. if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
  170. send_sig(SIGPIPE, current, 0);
  171. return err;
  172. }
  173. EXPORT_SYMBOL(sk_stream_error);
  174. void sk_stream_kill_queues(struct sock *sk)
  175. {
  176. /* First the read buffer. */
  177. __skb_queue_purge(&sk->sk_receive_queue);
  178. /* Next, the error queue.
  179. * We need to use queue lock, because other threads might
  180. * add packets to the queue without socket lock being held.
  181. */
  182. skb_queue_purge(&sk->sk_error_queue);
  183. /* Next, the write queue. */
  184. WARN_ON_ONCE(!skb_queue_empty(&sk->sk_write_queue));
  185. /* Account for returned memory. */
  186. sk_mem_reclaim_final(sk);
  187. WARN_ON_ONCE(sk->sk_wmem_queued);
  188. /* It is _impossible_ for the backlog to contain anything
  189. * when we get here. All user references to this socket
  190. * have gone away, only the net layer knows can touch it.
  191. */
  192. }
  193. EXPORT_SYMBOL(sk_stream_kill_queues);