Linux-2.6.12-rc2
[linux-2.6/next.git] / net / core / stream.c
blob1e27a57b5a9754858be142c1dec91cf096b5329f
1 /*
2 * SUCS NET3:
4 * Generic stream handling routines. These are generic for most
5 * protocols. Even IP. Tonight 8-).
6 * This is used because TCP, LLC (others too) layer all have mostly
7 * identical sendmsg() and recvmsg() code.
8 * So we (will) share it here.
10 * Authors: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
11 * (from old tcp.c code)
12 * Alan Cox <alan@redhat.com> (Borrowed comments 8-))
15 #include <linux/module.h>
16 #include <linux/net.h>
17 #include <linux/signal.h>
18 #include <linux/tcp.h>
19 #include <linux/wait.h>
20 #include <net/sock.h>
22 /**
23 * sk_stream_write_space - stream socket write_space callback.
24 * sk - socket
26 * FIXME: write proper description
28 void sk_stream_write_space(struct sock *sk)
30 struct socket *sock = sk->sk_socket;
32 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk) && sock) {
33 clear_bit(SOCK_NOSPACE, &sock->flags);
35 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
36 wake_up_interruptible(sk->sk_sleep);
37 if (sock->fasync_list && !(sk->sk_shutdown & SEND_SHUTDOWN))
38 sock_wake_async(sock, 2, POLL_OUT);
42 EXPORT_SYMBOL(sk_stream_write_space);
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
49 * Must be called with the socket locked.
51 int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
53 struct task_struct *tsk = current;
54 DEFINE_WAIT(wait);
56 while (1) {
57 if (sk->sk_err)
58 return sock_error(sk);
59 if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
60 return -EPIPE;
61 if (!*timeo_p)
62 return -EAGAIN;
63 if (signal_pending(tsk))
64 return sock_intr_errno(*timeo_p);
66 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
67 sk->sk_write_pending++;
68 if (sk_wait_event(sk, timeo_p,
69 !((1 << sk->sk_state) &
70 ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))))
71 break;
72 finish_wait(sk->sk_sleep, &wait);
73 sk->sk_write_pending--;
75 return 0;
78 EXPORT_SYMBOL(sk_stream_wait_connect);
80 /**
81 * sk_stream_closing - Return 1 if we still have things to send in our buffers.
82 * @sk - socket to verify
84 static inline int sk_stream_closing(struct sock *sk)
86 return (1 << sk->sk_state) &
87 (TCPF_FIN_WAIT1 | TCPF_CLOSING | TCPF_LAST_ACK);
90 void sk_stream_wait_close(struct sock *sk, long timeout)
92 if (timeout) {
93 DEFINE_WAIT(wait);
95 do {
96 prepare_to_wait(sk->sk_sleep, &wait,
97 TASK_INTERRUPTIBLE);
98 if (sk_wait_event(sk, &timeout, !sk_stream_closing(sk)))
99 break;
100 } while (!signal_pending(current) && timeout);
102 finish_wait(sk->sk_sleep, &wait);
106 EXPORT_SYMBOL(sk_stream_wait_close);
109 * sk_stream_wait_memory - Wait for more memory for a socket
110 * @sk - socket to wait for memory
111 * @timeo_p - for how long
113 int sk_stream_wait_memory(struct sock *sk, long *timeo_p)
115 int err = 0;
116 long vm_wait = 0;
117 long current_timeo = *timeo_p;
118 DEFINE_WAIT(wait);
120 if (sk_stream_memory_free(sk))
121 current_timeo = vm_wait = (net_random() % (HZ / 5)) + 2;
123 while (1) {
124 set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
126 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
128 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
129 goto do_error;
130 if (!*timeo_p)
131 goto do_nonblock;
132 if (signal_pending(current))
133 goto do_interrupted;
134 clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
135 if (sk_stream_memory_free(sk) && !vm_wait)
136 break;
138 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
139 sk->sk_write_pending++;
140 sk_wait_event(sk, &current_timeo, sk_stream_memory_free(sk) &&
141 vm_wait);
142 sk->sk_write_pending--;
144 if (vm_wait) {
145 vm_wait -= current_timeo;
146 current_timeo = *timeo_p;
147 if (current_timeo != MAX_SCHEDULE_TIMEOUT &&
148 (current_timeo -= vm_wait) < 0)
149 current_timeo = 0;
150 vm_wait = 0;
152 *timeo_p = current_timeo;
154 out:
155 finish_wait(sk->sk_sleep, &wait);
156 return err;
158 do_error:
159 err = -EPIPE;
160 goto out;
161 do_nonblock:
162 err = -EAGAIN;
163 goto out;
164 do_interrupted:
165 err = sock_intr_errno(*timeo_p);
166 goto out;
169 EXPORT_SYMBOL(sk_stream_wait_memory);
171 void sk_stream_rfree(struct sk_buff *skb)
173 struct sock *sk = skb->sk;
175 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
176 sk->sk_forward_alloc += skb->truesize;
179 EXPORT_SYMBOL(sk_stream_rfree);
181 int sk_stream_error(struct sock *sk, int flags, int err)
183 if (err == -EPIPE)
184 err = sock_error(sk) ? : -EPIPE;
185 if (err == -EPIPE && !(flags & MSG_NOSIGNAL))
186 send_sig(SIGPIPE, current, 0);
187 return err;
190 EXPORT_SYMBOL(sk_stream_error);
192 void __sk_stream_mem_reclaim(struct sock *sk)
194 if (sk->sk_forward_alloc >= SK_STREAM_MEM_QUANTUM) {
195 atomic_sub(sk->sk_forward_alloc / SK_STREAM_MEM_QUANTUM,
196 sk->sk_prot->memory_allocated);
197 sk->sk_forward_alloc &= SK_STREAM_MEM_QUANTUM - 1;
198 if (*sk->sk_prot->memory_pressure &&
199 (atomic_read(sk->sk_prot->memory_allocated) <
200 sk->sk_prot->sysctl_mem[0]))
201 *sk->sk_prot->memory_pressure = 0;
205 EXPORT_SYMBOL(__sk_stream_mem_reclaim);
207 int sk_stream_mem_schedule(struct sock *sk, int size, int kind)
209 int amt = sk_stream_pages(size);
211 sk->sk_forward_alloc += amt * SK_STREAM_MEM_QUANTUM;
212 atomic_add(amt, sk->sk_prot->memory_allocated);
214 /* Under limit. */
215 if (atomic_read(sk->sk_prot->memory_allocated) < sk->sk_prot->sysctl_mem[0]) {
216 if (*sk->sk_prot->memory_pressure)
217 *sk->sk_prot->memory_pressure = 0;
218 return 1;
221 /* Over hard limit. */
222 if (atomic_read(sk->sk_prot->memory_allocated) > sk->sk_prot->sysctl_mem[2]) {
223 sk->sk_prot->enter_memory_pressure();
224 goto suppress_allocation;
227 /* Under pressure. */
228 if (atomic_read(sk->sk_prot->memory_allocated) > sk->sk_prot->sysctl_mem[1])
229 sk->sk_prot->enter_memory_pressure();
231 if (kind) {
232 if (atomic_read(&sk->sk_rmem_alloc) < sk->sk_prot->sysctl_rmem[0])
233 return 1;
234 } else if (sk->sk_wmem_queued < sk->sk_prot->sysctl_wmem[0])
235 return 1;
237 if (!*sk->sk_prot->memory_pressure ||
238 sk->sk_prot->sysctl_mem[2] > atomic_read(sk->sk_prot->sockets_allocated) *
239 sk_stream_pages(sk->sk_wmem_queued +
240 atomic_read(&sk->sk_rmem_alloc) +
241 sk->sk_forward_alloc))
242 return 1;
244 suppress_allocation:
246 if (!kind) {
247 sk_stream_moderate_sndbuf(sk);
249 /* Fail only if socket is _under_ its sndbuf.
250 * In this case we cannot block, so that we have to fail.
252 if (sk->sk_wmem_queued + size >= sk->sk_sndbuf)
253 return 1;
256 /* Alas. Undo changes. */
257 sk->sk_forward_alloc -= amt * SK_STREAM_MEM_QUANTUM;
258 atomic_sub(amt, sk->sk_prot->memory_allocated);
259 return 0;
262 EXPORT_SYMBOL(sk_stream_mem_schedule);
264 void sk_stream_kill_queues(struct sock *sk)
266 /* First the read buffer. */
267 __skb_queue_purge(&sk->sk_receive_queue);
269 /* Next, the error queue. */
270 __skb_queue_purge(&sk->sk_error_queue);
272 /* Next, the write queue. */
273 BUG_TRAP(skb_queue_empty(&sk->sk_write_queue));
275 /* Account for returned memory. */
276 sk_stream_mem_reclaim(sk);
278 BUG_TRAP(!sk->sk_wmem_queued);
279 BUG_TRAP(!sk->sk_forward_alloc);
281 /* It is _impossible_ for the backlog to contain anything
282 * when we get here. All user references to this socket
283 * have gone away, only the net layer knows can touch it.
287 EXPORT_SYMBOL(sk_stream_kill_queues);