Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/net/sunrpc/svcsock.c | |
3 | * | |
4 | * These are the RPC server socket internals. | |
5 | * | |
6 | * The server scheduling algorithm does not always distribute the load | |
7 | * evenly when servicing a single client. May need to modify the | |
f6150c3c | 8 | * svc_xprt_enqueue procedure... |
1da177e4 LT |
9 | * |
10 | * TCP support is largely untested and may be a little slow. The problem | |
11 | * is that we currently do two separate recvfrom's, one for the 4-byte | |
12 | * record length, and the second for the actual record. This could possibly | |
13 | * be improved by always reading a minimum size of around 100 bytes and | |
14 | * tucking any superfluous bytes away in a temporary store. Still, that | |
15 | * leaves write requests out in the rain. An alternative may be to peek at | |
16 | * the first skb in the queue, and if it matches the next TCP sequence | |
17 | * number, to extract the record marker. Yuck. | |
18 | * | |
19 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> | |
20 | */ | |
21 | ||
172589cc | 22 | #include <linux/kernel.h> |
1da177e4 LT |
23 | #include <linux/sched.h> |
24 | #include <linux/errno.h> | |
25 | #include <linux/fcntl.h> | |
26 | #include <linux/net.h> | |
27 | #include <linux/in.h> | |
28 | #include <linux/inet.h> | |
29 | #include <linux/udp.h> | |
91483c4b | 30 | #include <linux/tcp.h> |
1da177e4 LT |
31 | #include <linux/unistd.h> |
32 | #include <linux/slab.h> | |
33 | #include <linux/netdevice.h> | |
34 | #include <linux/skbuff.h> | |
b41b66d6 | 35 | #include <linux/file.h> |
7dfb7103 | 36 | #include <linux/freezer.h> |
1da177e4 LT |
37 | #include <net/sock.h> |
38 | #include <net/checksum.h> | |
39 | #include <net/ip.h> | |
b92503b2 | 40 | #include <net/ipv6.h> |
c752f073 | 41 | #include <net/tcp_states.h> |
1da177e4 LT |
42 | #include <asm/uaccess.h> |
43 | #include <asm/ioctls.h> | |
44 | ||
45 | #include <linux/sunrpc/types.h> | |
ad06e4bd | 46 | #include <linux/sunrpc/clnt.h> |
1da177e4 LT |
47 | #include <linux/sunrpc/xdr.h> |
48 | #include <linux/sunrpc/svcsock.h> | |
49 | #include <linux/sunrpc/stats.h> | |
50 | ||
51 | /* SMP locking strategy: | |
52 | * | |
3262c816 GB |
53 | * svc_pool->sp_lock protects most of the fields of that pool. |
54 | * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt. | |
55 | * when both need to be taken (rare), svc_serv->sv_lock is first. | |
56 | * BKL protects svc_serv->sv_nrthread. | |
7ac1bea5 N |
57 | * svc_sock->sk_lock protects the svc_sock->sk_deferred list |
58 | * and the ->sk_info_authunix cache. | |
02fc6c36 TT |
59 | * svc_sock->sk_xprt.xpt_flags.XPT_BUSY prevents a svc_sock being |
60 | * enqueued multiply. | |
1da177e4 LT |
61 | * |
62 | * Some flags can be set to certain values at any time | |
63 | * providing that certain rules are followed: | |
64 | * | |
02fc6c36 | 65 | * XPT_CONN, XPT_DATA, can be set or cleared at any time. |
f6150c3c | 66 | * after a set, svc_xprt_enqueue must be called. |
1da177e4 LT |
67 | * after a clear, the socket must be read/accepted |
68 | * if this succeeds, it must be set again. | |
02fc6c36 TT |
69 | * XPT_CLOSE can set at any time. It is never cleared. |
70 | * xpt_ref contains a bias of '1' until XPT_DEAD is set. | |
e1b3157f | 71 | * so when xprt_ref hits zero, we know the transport is dead |
aaf68cfb | 72 | * and no-one is using it. |
02fc6c36 | 73 | * XPT_DEAD can only be set while XPT_BUSY is held which ensures |
aaf68cfb | 74 | * no other thread will be using the socket or will try to |
02fc6c36 | 75 | * set XPT_DEAD. |
1da177e4 LT |
76 | * |
77 | */ | |
78 | ||
360d8738 | 79 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT |
1da177e4 LT |
80 | |
81 | ||
82 | static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *, | |
6b174337 | 83 | int *errp, int flags); |
7a182083 | 84 | static void svc_delete_xprt(struct svc_xprt *xprt); |
1da177e4 LT |
85 | static void svc_udp_data_ready(struct sock *, int); |
86 | static int svc_udp_recvfrom(struct svc_rqst *); | |
87 | static int svc_udp_sendto(struct svc_rqst *); | |
7a182083 | 88 | static void svc_close_xprt(struct svc_xprt *xprt); |
755cceab TT |
89 | static void svc_sock_detach(struct svc_xprt *); |
90 | static void svc_sock_free(struct svc_xprt *); | |
1da177e4 | 91 | |
8c7b0172 | 92 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt); |
1da177e4 LT |
93 | static int svc_deferred_recv(struct svc_rqst *rqstp); |
94 | static struct cache_deferred_req *svc_defer(struct cache_req *req); | |
b700cbb1 TT |
95 | static struct svc_xprt *svc_create_socket(struct svc_serv *, int, |
96 | struct sockaddr *, int, int); | |
1da177e4 | 97 | |
36bdfc8b GB |
98 | /* apparently the "standard" is that clients close |
99 | * idle connections after 5 minutes, servers after | |
100 | * 6 minutes | |
101 | * http://www.connectathon.org/talks96/nfstcp.pdf | |
102 | */ | |
103 | static int svc_conn_age_period = 6*60; | |
104 | ||
ed07536e PZ |
105 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
106 | static struct lock_class_key svc_key[2]; | |
107 | static struct lock_class_key svc_slock_key[2]; | |
108 | ||
109 | static inline void svc_reclassify_socket(struct socket *sock) | |
110 | { | |
111 | struct sock *sk = sock->sk; | |
02b3d346 | 112 | BUG_ON(sock_owned_by_user(sk)); |
ed07536e PZ |
113 | switch (sk->sk_family) { |
114 | case AF_INET: | |
115 | sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD", | |
def13d74 TT |
116 | &svc_slock_key[0], |
117 | "sk_xprt.xpt_lock-AF_INET-NFSD", | |
118 | &svc_key[0]); | |
ed07536e PZ |
119 | break; |
120 | ||
121 | case AF_INET6: | |
122 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD", | |
def13d74 TT |
123 | &svc_slock_key[1], |
124 | "sk_xprt.xpt_lock-AF_INET6-NFSD", | |
125 | &svc_key[1]); | |
ed07536e PZ |
126 | break; |
127 | ||
128 | default: | |
129 | BUG(); | |
130 | } | |
131 | } | |
132 | #else | |
133 | static inline void svc_reclassify_socket(struct socket *sock) | |
134 | { | |
135 | } | |
136 | #endif | |
137 | ||
ad06e4bd CL |
138 | static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len) |
139 | { | |
140 | switch (addr->sa_family) { | |
141 | case AF_INET: | |
142 | snprintf(buf, len, "%u.%u.%u.%u, port=%u", | |
143 | NIPQUAD(((struct sockaddr_in *) addr)->sin_addr), | |
582ee43d | 144 | ntohs(((struct sockaddr_in *) addr)->sin_port)); |
ad06e4bd | 145 | break; |
5a05ed73 | 146 | |
ad06e4bd CL |
147 | case AF_INET6: |
148 | snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u", | |
149 | NIP6(((struct sockaddr_in6 *) addr)->sin6_addr), | |
582ee43d | 150 | ntohs(((struct sockaddr_in6 *) addr)->sin6_port)); |
ad06e4bd | 151 | break; |
5a05ed73 | 152 | |
ad06e4bd CL |
153 | default: |
154 | snprintf(buf, len, "unknown address type: %d", addr->sa_family); | |
155 | break; | |
156 | } | |
157 | return buf; | |
158 | } | |
159 | ||
160 | /** | |
161 | * svc_print_addr - Format rq_addr field for printing | |
162 | * @rqstp: svc_rqst struct containing address to print | |
163 | * @buf: target buffer for formatted address | |
164 | * @len: length of target buffer | |
165 | * | |
166 | */ | |
167 | char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len) | |
168 | { | |
27459f09 | 169 | return __svc_print_addr(svc_addr(rqstp), buf, len); |
ad06e4bd CL |
170 | } |
171 | EXPORT_SYMBOL_GPL(svc_print_addr); | |
172 | ||
1da177e4 | 173 | /* |
3262c816 | 174 | * Queue up an idle server thread. Must have pool->sp_lock held. |
1da177e4 | 175 | * Note: this is really a stack rather than a queue, so that we only |
3262c816 | 176 | * use as many different threads as we need, and the rest don't pollute |
1da177e4 LT |
177 | * the cache. |
178 | */ | |
179 | static inline void | |
3262c816 | 180 | svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp) |
1da177e4 | 181 | { |
3262c816 | 182 | list_add(&rqstp->rq_list, &pool->sp_threads); |
1da177e4 LT |
183 | } |
184 | ||
185 | /* | |
3262c816 | 186 | * Dequeue an nfsd thread. Must have pool->sp_lock held. |
1da177e4 LT |
187 | */ |
188 | static inline void | |
3262c816 | 189 | svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp) |
1da177e4 LT |
190 | { |
191 | list_del(&rqstp->rq_list); | |
192 | } | |
193 | ||
194 | /* | |
195 | * Release an skbuff after use | |
196 | */ | |
5148bf4e | 197 | static void svc_release_skb(struct svc_rqst *rqstp) |
1da177e4 | 198 | { |
5148bf4e | 199 | struct sk_buff *skb = rqstp->rq_xprt_ctxt; |
1da177e4 LT |
200 | struct svc_deferred_req *dr = rqstp->rq_deferred; |
201 | ||
202 | if (skb) { | |
5148bf4e | 203 | rqstp->rq_xprt_ctxt = NULL; |
1da177e4 LT |
204 | |
205 | dprintk("svc: service %p, releasing skb %p\n", rqstp, skb); | |
206 | skb_free_datagram(rqstp->rq_sock->sk_sk, skb); | |
207 | } | |
208 | if (dr) { | |
209 | rqstp->rq_deferred = NULL; | |
210 | kfree(dr); | |
211 | } | |
212 | } | |
213 | ||
1da177e4 LT |
214 | /* |
215 | * Queue up a socket with data pending. If there are idle nfsd | |
216 | * processes, wake 'em up. | |
217 | * | |
218 | */ | |
f6150c3c | 219 | void svc_xprt_enqueue(struct svc_xprt *xprt) |
1da177e4 | 220 | { |
f6150c3c | 221 | struct svc_serv *serv = xprt->xpt_server; |
bfd24160 | 222 | struct svc_pool *pool; |
1da177e4 | 223 | struct svc_rqst *rqstp; |
bfd24160 | 224 | int cpu; |
1da177e4 | 225 | |
f6150c3c | 226 | if (!(xprt->xpt_flags & |
02fc6c36 | 227 | ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED)))) |
1da177e4 | 228 | return; |
f6150c3c | 229 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) |
1da177e4 LT |
230 | return; |
231 | ||
bfd24160 | 232 | cpu = get_cpu(); |
f6150c3c | 233 | pool = svc_pool_for_cpu(xprt->xpt_server, cpu); |
bfd24160 GB |
234 | put_cpu(); |
235 | ||
3262c816 | 236 | spin_lock_bh(&pool->sp_lock); |
1da177e4 | 237 | |
3262c816 GB |
238 | if (!list_empty(&pool->sp_threads) && |
239 | !list_empty(&pool->sp_sockets)) | |
1da177e4 | 240 | printk(KERN_ERR |
f6150c3c TT |
241 | "svc_xprt_enqueue: " |
242 | "threads and transports both waiting??\n"); | |
1da177e4 | 243 | |
f6150c3c | 244 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) { |
1da177e4 | 245 | /* Don't enqueue dead sockets */ |
f6150c3c | 246 | dprintk("svc: transport %p is dead, not enqueued\n", xprt); |
1da177e4 LT |
247 | goto out_unlock; |
248 | } | |
249 | ||
c081a0c7 GB |
250 | /* Mark socket as busy. It will remain in this state until the |
251 | * server has processed all pending data and put the socket back | |
02fc6c36 | 252 | * on the idle list. We update XPT_BUSY atomically because |
c081a0c7 GB |
253 | * it also guards against trying to enqueue the svc_sock twice. |
254 | */ | |
f6150c3c | 255 | if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) { |
c081a0c7 | 256 | /* Don't enqueue socket while already enqueued */ |
f6150c3c | 257 | dprintk("svc: transport %p busy, not enqueued\n", xprt); |
1da177e4 LT |
258 | goto out_unlock; |
259 | } | |
f6150c3c TT |
260 | BUG_ON(xprt->xpt_pool != NULL); |
261 | xprt->xpt_pool = pool; | |
1da177e4 | 262 | |
323bee32 | 263 | /* Handle pending connection */ |
f6150c3c | 264 | if (test_bit(XPT_CONN, &xprt->xpt_flags)) |
323bee32 TT |
265 | goto process; |
266 | ||
267 | /* Handle close in-progress */ | |
f6150c3c | 268 | if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) |
323bee32 TT |
269 | goto process; |
270 | ||
271 | /* Check if we have space to reply to a request */ | |
f6150c3c | 272 | if (!xprt->xpt_ops->xpo_has_wspace(xprt)) { |
1da177e4 | 273 | /* Don't enqueue while not enough space for reply */ |
f6150c3c TT |
274 | dprintk("svc: no write space, transport %p not enqueued\n", |
275 | xprt); | |
276 | xprt->xpt_pool = NULL; | |
277 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
1da177e4 LT |
278 | goto out_unlock; |
279 | } | |
1da177e4 | 280 | |
323bee32 | 281 | process: |
3262c816 GB |
282 | if (!list_empty(&pool->sp_threads)) { |
283 | rqstp = list_entry(pool->sp_threads.next, | |
1da177e4 LT |
284 | struct svc_rqst, |
285 | rq_list); | |
f6150c3c TT |
286 | dprintk("svc: transport %p served by daemon %p\n", |
287 | xprt, rqstp); | |
3262c816 | 288 | svc_thread_dequeue(pool, rqstp); |
f6150c3c | 289 | if (rqstp->rq_xprt) |
cca5172a | 290 | printk(KERN_ERR |
f6150c3c TT |
291 | "svc_xprt_enqueue: server %p, rq_xprt=%p!\n", |
292 | rqstp, rqstp->rq_xprt); | |
293 | rqstp->rq_xprt = xprt; | |
294 | svc_xprt_get(xprt); | |
c6b0a9f8 | 295 | rqstp->rq_reserved = serv->sv_max_mesg; |
f6150c3c TT |
296 | atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved); |
297 | BUG_ON(xprt->xpt_pool != pool); | |
1da177e4 LT |
298 | wake_up(&rqstp->rq_wait); |
299 | } else { | |
f6150c3c TT |
300 | dprintk("svc: transport %p put into queue\n", xprt); |
301 | list_add_tail(&xprt->xpt_ready, &pool->sp_sockets); | |
302 | BUG_ON(xprt->xpt_pool != pool); | |
1da177e4 LT |
303 | } |
304 | ||
305 | out_unlock: | |
3262c816 | 306 | spin_unlock_bh(&pool->sp_lock); |
1da177e4 | 307 | } |
f6150c3c | 308 | EXPORT_SYMBOL_GPL(svc_xprt_enqueue); |
1da177e4 LT |
309 | |
310 | /* | |
3262c816 | 311 | * Dequeue the first socket. Must be called with the pool->sp_lock held. |
1da177e4 LT |
312 | */ |
313 | static inline struct svc_sock * | |
3262c816 | 314 | svc_sock_dequeue(struct svc_pool *pool) |
1da177e4 LT |
315 | { |
316 | struct svc_sock *svsk; | |
317 | ||
3262c816 | 318 | if (list_empty(&pool->sp_sockets)) |
1da177e4 LT |
319 | return NULL; |
320 | ||
3262c816 | 321 | svsk = list_entry(pool->sp_sockets.next, |
7a182083 TT |
322 | struct svc_sock, sk_xprt.xpt_ready); |
323 | list_del_init(&svsk->sk_xprt.xpt_ready); | |
1da177e4 LT |
324 | |
325 | dprintk("svc: socket %p dequeued, inuse=%d\n", | |
e1b3157f | 326 | svsk->sk_sk, atomic_read(&svsk->sk_xprt.xpt_ref.refcount)); |
1da177e4 LT |
327 | |
328 | return svsk; | |
329 | } | |
330 | ||
331 | /* | |
a6046f71 TT |
332 | * svc_xprt_received conditionally queues the transport for processing |
333 | * by another thread. The caller must hold the XPT_BUSY bit and must | |
334 | * not thereafter touch transport data. | |
335 | * | |
336 | * Note: XPT_DATA only gets cleared when a read-attempt finds no (or | |
337 | * insufficient) data. | |
1da177e4 | 338 | */ |
a6046f71 | 339 | void svc_xprt_received(struct svc_xprt *xprt) |
1da177e4 | 340 | { |
a6046f71 TT |
341 | BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags)); |
342 | xprt->xpt_pool = NULL; | |
343 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
344 | svc_xprt_enqueue(xprt); | |
1da177e4 | 345 | } |
a6046f71 | 346 | EXPORT_SYMBOL_GPL(svc_xprt_received); |
1da177e4 LT |
347 | |
348 | /** | |
349 | * svc_reserve - change the space reserved for the reply to a request. | |
350 | * @rqstp: The request in question | |
351 | * @space: new max space to reserve | |
352 | * | |
353 | * Each request reserves some space on the output queue of the socket | |
354 | * to make sure the reply fits. This function reduces that reserved | |
355 | * space to be the amount of space used already, plus @space. | |
356 | * | |
357 | */ | |
358 | void svc_reserve(struct svc_rqst *rqstp, int space) | |
359 | { | |
360 | space += rqstp->rq_res.head[0].iov_len; | |
361 | ||
362 | if (space < rqstp->rq_reserved) { | |
f6150c3c TT |
363 | struct svc_xprt *xprt = rqstp->rq_xprt; |
364 | atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved); | |
1da177e4 | 365 | rqstp->rq_reserved = space; |
1da177e4 | 366 | |
f6150c3c | 367 | svc_xprt_enqueue(xprt); |
1da177e4 LT |
368 | } |
369 | } | |
370 | ||
1da177e4 LT |
371 | static void |
372 | svc_sock_release(struct svc_rqst *rqstp) | |
373 | { | |
374 | struct svc_sock *svsk = rqstp->rq_sock; | |
375 | ||
5148bf4e | 376 | rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp); |
1da177e4 | 377 | |
44524359 | 378 | svc_free_res_pages(rqstp); |
1da177e4 LT |
379 | rqstp->rq_res.page_len = 0; |
380 | rqstp->rq_res.page_base = 0; | |
381 | ||
382 | ||
383 | /* Reset response buffer and release | |
384 | * the reservation. | |
385 | * But first, check that enough space was reserved | |
386 | * for the reply, otherwise we have a bug! | |
387 | */ | |
388 | if ((rqstp->rq_res.len) > rqstp->rq_reserved) | |
389 | printk(KERN_ERR "RPC request reserved %d but used %d\n", | |
390 | rqstp->rq_reserved, | |
391 | rqstp->rq_res.len); | |
392 | ||
393 | rqstp->rq_res.head[0].iov_len = 0; | |
394 | svc_reserve(rqstp, 0); | |
395 | rqstp->rq_sock = NULL; | |
396 | ||
e1b3157f | 397 | svc_xprt_put(&svsk->sk_xprt); |
1da177e4 LT |
398 | } |
399 | ||
400 | /* | |
401 | * External function to wake up a server waiting for data | |
3262c816 GB |
402 | * This really only makes sense for services like lockd |
403 | * which have exactly one thread anyway. | |
1da177e4 LT |
404 | */ |
405 | void | |
406 | svc_wake_up(struct svc_serv *serv) | |
407 | { | |
408 | struct svc_rqst *rqstp; | |
3262c816 GB |
409 | unsigned int i; |
410 | struct svc_pool *pool; | |
411 | ||
412 | for (i = 0; i < serv->sv_nrpools; i++) { | |
413 | pool = &serv->sv_pools[i]; | |
414 | ||
415 | spin_lock_bh(&pool->sp_lock); | |
416 | if (!list_empty(&pool->sp_threads)) { | |
417 | rqstp = list_entry(pool->sp_threads.next, | |
418 | struct svc_rqst, | |
419 | rq_list); | |
420 | dprintk("svc: daemon %p woken up.\n", rqstp); | |
421 | /* | |
422 | svc_thread_dequeue(pool, rqstp); | |
423 | rqstp->rq_sock = NULL; | |
424 | */ | |
425 | wake_up(&rqstp->rq_wait); | |
426 | } | |
427 | spin_unlock_bh(&pool->sp_lock); | |
1da177e4 | 428 | } |
1da177e4 LT |
429 | } |
430 | ||
b92503b2 CL |
431 | union svc_pktinfo_u { |
432 | struct in_pktinfo pkti; | |
b92503b2 | 433 | struct in6_pktinfo pkti6; |
b92503b2 | 434 | }; |
bc375ea7 DM |
435 | #define SVC_PKTINFO_SPACE \ |
436 | CMSG_SPACE(sizeof(union svc_pktinfo_u)) | |
b92503b2 CL |
437 | |
438 | static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh) | |
439 | { | |
440 | switch (rqstp->rq_sock->sk_sk->sk_family) { | |
441 | case AF_INET: { | |
442 | struct in_pktinfo *pki = CMSG_DATA(cmh); | |
443 | ||
444 | cmh->cmsg_level = SOL_IP; | |
445 | cmh->cmsg_type = IP_PKTINFO; | |
446 | pki->ipi_ifindex = 0; | |
447 | pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr; | |
448 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); | |
449 | } | |
450 | break; | |
5a05ed73 | 451 | |
b92503b2 CL |
452 | case AF_INET6: { |
453 | struct in6_pktinfo *pki = CMSG_DATA(cmh); | |
454 | ||
455 | cmh->cmsg_level = SOL_IPV6; | |
456 | cmh->cmsg_type = IPV6_PKTINFO; | |
457 | pki->ipi6_ifindex = 0; | |
458 | ipv6_addr_copy(&pki->ipi6_addr, | |
459 | &rqstp->rq_daddr.addr6); | |
460 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); | |
461 | } | |
462 | break; | |
b92503b2 CL |
463 | } |
464 | return; | |
465 | } | |
466 | ||
1da177e4 LT |
467 | /* |
468 | * Generic sendto routine | |
469 | */ | |
470 | static int | |
471 | svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr) | |
472 | { | |
473 | struct svc_sock *svsk = rqstp->rq_sock; | |
474 | struct socket *sock = svsk->sk_sock; | |
475 | int slen; | |
bc375ea7 DM |
476 | union { |
477 | struct cmsghdr hdr; | |
478 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; | |
479 | } buffer; | |
480 | struct cmsghdr *cmh = &buffer.hdr; | |
1da177e4 LT |
481 | int len = 0; |
482 | int result; | |
483 | int size; | |
484 | struct page **ppage = xdr->pages; | |
485 | size_t base = xdr->page_base; | |
486 | unsigned int pglen = xdr->page_len; | |
487 | unsigned int flags = MSG_MORE; | |
ad06e4bd | 488 | char buf[RPC_MAX_ADDRBUFLEN]; |
1da177e4 LT |
489 | |
490 | slen = xdr->len; | |
491 | ||
492 | if (rqstp->rq_prot == IPPROTO_UDP) { | |
b92503b2 CL |
493 | struct msghdr msg = { |
494 | .msg_name = &rqstp->rq_addr, | |
495 | .msg_namelen = rqstp->rq_addrlen, | |
496 | .msg_control = cmh, | |
497 | .msg_controllen = sizeof(buffer), | |
498 | .msg_flags = MSG_MORE, | |
499 | }; | |
500 | ||
501 | svc_set_cmsg_data(rqstp, cmh); | |
1da177e4 LT |
502 | |
503 | if (sock_sendmsg(sock, &msg, 0) < 0) | |
504 | goto out; | |
505 | } | |
506 | ||
507 | /* send head */ | |
508 | if (slen == xdr->head[0].iov_len) | |
509 | flags = 0; | |
44524359 N |
510 | len = kernel_sendpage(sock, rqstp->rq_respages[0], 0, |
511 | xdr->head[0].iov_len, flags); | |
1da177e4 LT |
512 | if (len != xdr->head[0].iov_len) |
513 | goto out; | |
514 | slen -= xdr->head[0].iov_len; | |
515 | if (slen == 0) | |
516 | goto out; | |
517 | ||
518 | /* send page data */ | |
519 | size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen; | |
520 | while (pglen > 0) { | |
521 | if (slen == size) | |
522 | flags = 0; | |
e6242e92 | 523 | result = kernel_sendpage(sock, *ppage, base, size, flags); |
1da177e4 LT |
524 | if (result > 0) |
525 | len += result; | |
526 | if (result != size) | |
527 | goto out; | |
528 | slen -= size; | |
529 | pglen -= size; | |
530 | size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen; | |
531 | base = 0; | |
532 | ppage++; | |
533 | } | |
534 | /* send tail */ | |
535 | if (xdr->tail[0].iov_len) { | |
44524359 N |
536 | result = kernel_sendpage(sock, rqstp->rq_respages[0], |
537 | ((unsigned long)xdr->tail[0].iov_base) | |
cca5172a | 538 | & (PAGE_SIZE-1), |
1da177e4 LT |
539 | xdr->tail[0].iov_len, 0); |
540 | ||
541 | if (result > 0) | |
542 | len += result; | |
543 | } | |
544 | out: | |
ad06e4bd CL |
545 | dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n", |
546 | rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, | |
547 | xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf))); | |
1da177e4 LT |
548 | |
549 | return len; | |
550 | } | |
551 | ||
80212d59 N |
552 | /* |
553 | * Report socket names for nfsdfs | |
554 | */ | |
555 | static int one_sock_name(char *buf, struct svc_sock *svsk) | |
556 | { | |
557 | int len; | |
558 | ||
559 | switch(svsk->sk_sk->sk_family) { | |
560 | case AF_INET: | |
561 | len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n", | |
562 | svsk->sk_sk->sk_protocol==IPPROTO_UDP? | |
563 | "udp" : "tcp", | |
564 | NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr), | |
565 | inet_sk(svsk->sk_sk)->num); | |
566 | break; | |
567 | default: | |
568 | len = sprintf(buf, "*unknown-%d*\n", | |
569 | svsk->sk_sk->sk_family); | |
570 | } | |
571 | return len; | |
572 | } | |
573 | ||
574 | int | |
b41b66d6 | 575 | svc_sock_names(char *buf, struct svc_serv *serv, char *toclose) |
80212d59 | 576 | { |
b41b66d6 | 577 | struct svc_sock *svsk, *closesk = NULL; |
80212d59 N |
578 | int len = 0; |
579 | ||
580 | if (!serv) | |
581 | return 0; | |
aaf68cfb | 582 | spin_lock_bh(&serv->sv_lock); |
7a182083 | 583 | list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) { |
80212d59 | 584 | int onelen = one_sock_name(buf+len, svsk); |
b41b66d6 N |
585 | if (toclose && strcmp(toclose, buf+len) == 0) |
586 | closesk = svsk; | |
587 | else | |
588 | len += onelen; | |
80212d59 | 589 | } |
aaf68cfb | 590 | spin_unlock_bh(&serv->sv_lock); |
b41b66d6 | 591 | if (closesk) |
5680c446 N |
592 | /* Should unregister with portmap, but you cannot |
593 | * unregister just one protocol... | |
594 | */ | |
7a182083 | 595 | svc_close_xprt(&closesk->sk_xprt); |
37a03472 N |
596 | else if (toclose) |
597 | return -ENOENT; | |
80212d59 N |
598 | return len; |
599 | } | |
600 | EXPORT_SYMBOL(svc_sock_names); | |
601 | ||
1da177e4 LT |
602 | /* |
603 | * Check input queue length | |
604 | */ | |
605 | static int | |
606 | svc_recv_available(struct svc_sock *svsk) | |
607 | { | |
1da177e4 LT |
608 | struct socket *sock = svsk->sk_sock; |
609 | int avail, err; | |
610 | ||
e6242e92 | 611 | err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail); |
1da177e4 LT |
612 | |
613 | return (err >= 0)? avail : err; | |
614 | } | |
615 | ||
616 | /* | |
617 | * Generic recvfrom routine. | |
618 | */ | |
619 | static int | |
620 | svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen) | |
621 | { | |
067d7817 | 622 | struct svc_sock *svsk = rqstp->rq_sock; |
1ba95105 CL |
623 | struct msghdr msg = { |
624 | .msg_flags = MSG_DONTWAIT, | |
625 | }; | |
a9747692 | 626 | struct sockaddr *sin; |
1ba95105 | 627 | int len; |
1da177e4 | 628 | |
1ba95105 CL |
629 | len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen, |
630 | msg.msg_flags); | |
1da177e4 LT |
631 | |
632 | /* sock_recvmsg doesn't fill in the name/namelen, so we must.. | |
1da177e4 | 633 | */ |
067d7817 CL |
634 | memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen); |
635 | rqstp->rq_addrlen = svsk->sk_remotelen; | |
1da177e4 | 636 | |
a9747692 FM |
637 | /* Destination address in request is needed for binding the |
638 | * source address in RPC callbacks later. | |
639 | */ | |
640 | sin = (struct sockaddr *)&svsk->sk_local; | |
641 | switch (sin->sa_family) { | |
642 | case AF_INET: | |
643 | rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr; | |
644 | break; | |
645 | case AF_INET6: | |
646 | rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr; | |
647 | break; | |
648 | } | |
649 | ||
1da177e4 | 650 | dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n", |
1ba95105 | 651 | svsk, iov[0].iov_base, iov[0].iov_len, len); |
1da177e4 LT |
652 | |
653 | return len; | |
654 | } | |
655 | ||
656 | /* | |
657 | * Set socket snd and rcv buffer lengths | |
658 | */ | |
659 | static inline void | |
660 | svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv) | |
661 | { | |
662 | #if 0 | |
663 | mm_segment_t oldfs; | |
664 | oldfs = get_fs(); set_fs(KERNEL_DS); | |
665 | sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, | |
666 | (char*)&snd, sizeof(snd)); | |
667 | sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, | |
668 | (char*)&rcv, sizeof(rcv)); | |
669 | #else | |
670 | /* sock_setsockopt limits use to sysctl_?mem_max, | |
671 | * which isn't acceptable. Until that is made conditional | |
672 | * on not having CAP_SYS_RESOURCE or similar, we go direct... | |
673 | * DaveM said I could! | |
674 | */ | |
675 | lock_sock(sock->sk); | |
676 | sock->sk->sk_sndbuf = snd * 2; | |
677 | sock->sk->sk_rcvbuf = rcv * 2; | |
678 | sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK; | |
679 | release_sock(sock->sk); | |
680 | #endif | |
681 | } | |
682 | /* | |
683 | * INET callback when data has been received on the socket. | |
684 | */ | |
685 | static void | |
686 | svc_udp_data_ready(struct sock *sk, int count) | |
687 | { | |
939bb7ef | 688 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 | 689 | |
939bb7ef NB |
690 | if (svsk) { |
691 | dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n", | |
02fc6c36 TT |
692 | svsk, sk, count, |
693 | test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); | |
694 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); | |
f6150c3c | 695 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef | 696 | } |
1da177e4 LT |
697 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
698 | wake_up_interruptible(sk->sk_sleep); | |
699 | } | |
700 | ||
701 | /* | |
702 | * INET callback when space is newly available on the socket. | |
703 | */ | |
704 | static void | |
705 | svc_write_space(struct sock *sk) | |
706 | { | |
707 | struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); | |
708 | ||
709 | if (svsk) { | |
710 | dprintk("svc: socket %p(inet %p), write_space busy=%d\n", | |
02fc6c36 | 711 | svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
f6150c3c | 712 | svc_xprt_enqueue(&svsk->sk_xprt); |
1da177e4 LT |
713 | } |
714 | ||
715 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) { | |
939bb7ef | 716 | dprintk("RPC svc_write_space: someone sleeping on %p\n", |
1da177e4 LT |
717 | svsk); |
718 | wake_up_interruptible(sk->sk_sleep); | |
719 | } | |
720 | } | |
721 | ||
7a37f578 N |
722 | static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp, |
723 | struct cmsghdr *cmh) | |
95756482 CL |
724 | { |
725 | switch (rqstp->rq_sock->sk_sk->sk_family) { | |
726 | case AF_INET: { | |
7a37f578 N |
727 | struct in_pktinfo *pki = CMSG_DATA(cmh); |
728 | rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr; | |
95756482 | 729 | break; |
7a37f578 | 730 | } |
95756482 | 731 | case AF_INET6: { |
7a37f578 N |
732 | struct in6_pktinfo *pki = CMSG_DATA(cmh); |
733 | ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr); | |
95756482 | 734 | break; |
7a37f578 | 735 | } |
95756482 | 736 | } |
95756482 CL |
737 | } |
738 | ||
1da177e4 LT |
739 | /* |
740 | * Receive a datagram from a UDP socket. | |
741 | */ | |
1da177e4 LT |
742 | static int |
743 | svc_udp_recvfrom(struct svc_rqst *rqstp) | |
744 | { | |
745 | struct svc_sock *svsk = rqstp->rq_sock; | |
bb5cf160 | 746 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 | 747 | struct sk_buff *skb; |
bc375ea7 DM |
748 | union { |
749 | struct cmsghdr hdr; | |
750 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; | |
751 | } buffer; | |
752 | struct cmsghdr *cmh = &buffer.hdr; | |
1da177e4 | 753 | int err, len; |
7a37f578 N |
754 | struct msghdr msg = { |
755 | .msg_name = svc_addr(rqstp), | |
756 | .msg_control = cmh, | |
757 | .msg_controllen = sizeof(buffer), | |
758 | .msg_flags = MSG_DONTWAIT, | |
759 | }; | |
1da177e4 | 760 | |
02fc6c36 | 761 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
1da177e4 LT |
762 | /* udp sockets need large rcvbuf as all pending |
763 | * requests are still in that buffer. sndbuf must | |
764 | * also be large enough that there is enough space | |
3262c816 GB |
765 | * for one reply per thread. We count all threads |
766 | * rather than threads in a particular pool, which | |
767 | * provides an upper bound on the number of threads | |
768 | * which will access the socket. | |
1da177e4 LT |
769 | */ |
770 | svc_sock_setbufsize(svsk->sk_sock, | |
c6b0a9f8 N |
771 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
772 | (serv->sv_nrthreads+3) * serv->sv_max_mesg); | |
1da177e4 | 773 | |
02fc6c36 | 774 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
05ed690e N |
775 | skb = NULL; |
776 | err = kernel_recvmsg(svsk->sk_sock, &msg, NULL, | |
777 | 0, 0, MSG_PEEK | MSG_DONTWAIT); | |
778 | if (err >= 0) | |
779 | skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err); | |
780 | ||
781 | if (skb == NULL) { | |
782 | if (err != -EAGAIN) { | |
783 | /* possibly an icmp error */ | |
784 | dprintk("svc: recvfrom returned error %d\n", -err); | |
02fc6c36 | 785 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 786 | } |
a6046f71 | 787 | svc_xprt_received(&svsk->sk_xprt); |
05ed690e | 788 | return -EAGAIN; |
1da177e4 | 789 | } |
7a37f578 | 790 | rqstp->rq_addrlen = sizeof(rqstp->rq_addr); |
b7aa0bf7 ED |
791 | if (skb->tstamp.tv64 == 0) { |
792 | skb->tstamp = ktime_get_real(); | |
cca5172a | 793 | /* Don't enable netstamp, sunrpc doesn't |
1da177e4 LT |
794 | need that much accuracy */ |
795 | } | |
b7aa0bf7 | 796 | svsk->sk_sk->sk_stamp = skb->tstamp; |
02fc6c36 | 797 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */ |
1da177e4 LT |
798 | |
799 | /* | |
800 | * Maybe more packets - kick another thread ASAP. | |
801 | */ | |
a6046f71 | 802 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
803 | |
804 | len = skb->len - sizeof(struct udphdr); | |
805 | rqstp->rq_arg.len = len; | |
806 | ||
95756482 | 807 | rqstp->rq_prot = IPPROTO_UDP; |
27459f09 | 808 | |
7a37f578 N |
809 | if (cmh->cmsg_level != IPPROTO_IP || |
810 | cmh->cmsg_type != IP_PKTINFO) { | |
811 | if (net_ratelimit()) | |
812 | printk("rpcsvc: received unknown control message:" | |
813 | "%d/%d\n", | |
814 | cmh->cmsg_level, cmh->cmsg_type); | |
815 | skb_free_datagram(svsk->sk_sk, skb); | |
816 | return 0; | |
817 | } | |
818 | svc_udp_get_dest_address(rqstp, cmh); | |
1da177e4 LT |
819 | |
820 | if (skb_is_nonlinear(skb)) { | |
821 | /* we have to copy */ | |
822 | local_bh_disable(); | |
823 | if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) { | |
824 | local_bh_enable(); | |
825 | /* checksum error */ | |
826 | skb_free_datagram(svsk->sk_sk, skb); | |
827 | return 0; | |
828 | } | |
829 | local_bh_enable(); | |
cca5172a | 830 | skb_free_datagram(svsk->sk_sk, skb); |
1da177e4 LT |
831 | } else { |
832 | /* we can use it in-place */ | |
833 | rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr); | |
834 | rqstp->rq_arg.head[0].iov_len = len; | |
fb286bb2 HX |
835 | if (skb_checksum_complete(skb)) { |
836 | skb_free_datagram(svsk->sk_sk, skb); | |
837 | return 0; | |
1da177e4 | 838 | } |
5148bf4e | 839 | rqstp->rq_xprt_ctxt = skb; |
1da177e4 LT |
840 | } |
841 | ||
842 | rqstp->rq_arg.page_base = 0; | |
843 | if (len <= rqstp->rq_arg.head[0].iov_len) { | |
844 | rqstp->rq_arg.head[0].iov_len = len; | |
845 | rqstp->rq_arg.page_len = 0; | |
44524359 | 846 | rqstp->rq_respages = rqstp->rq_pages+1; |
1da177e4 LT |
847 | } else { |
848 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; | |
44524359 | 849 | rqstp->rq_respages = rqstp->rq_pages + 1 + |
172589cc | 850 | DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE); |
1da177e4 LT |
851 | } |
852 | ||
853 | if (serv->sv_stats) | |
854 | serv->sv_stats->netudpcnt++; | |
855 | ||
856 | return len; | |
857 | } | |
858 | ||
859 | static int | |
860 | svc_udp_sendto(struct svc_rqst *rqstp) | |
861 | { | |
862 | int error; | |
863 | ||
864 | error = svc_sendto(rqstp, &rqstp->rq_res); | |
865 | if (error == -ECONNREFUSED) | |
866 | /* ICMP error on earlier request. */ | |
867 | error = svc_sendto(rqstp, &rqstp->rq_res); | |
868 | ||
869 | return error; | |
870 | } | |
871 | ||
e831fe65 TT |
872 | static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp) |
873 | { | |
874 | } | |
875 | ||
323bee32 TT |
876 | static int svc_udp_has_wspace(struct svc_xprt *xprt) |
877 | { | |
878 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
bb5cf160 | 879 | struct svc_serv *serv = xprt->xpt_server; |
323bee32 TT |
880 | unsigned long required; |
881 | ||
882 | /* | |
883 | * Set the SOCK_NOSPACE flag before checking the available | |
884 | * sock space. | |
885 | */ | |
886 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
7a90e8cc | 887 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
323bee32 TT |
888 | if (required*2 > sock_wspace(svsk->sk_sk)) |
889 | return 0; | |
890 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
891 | return 1; | |
892 | } | |
893 | ||
38a417cc TT |
894 | static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt) |
895 | { | |
896 | BUG(); | |
897 | return NULL; | |
898 | } | |
899 | ||
b700cbb1 TT |
900 | static struct svc_xprt *svc_udp_create(struct svc_serv *serv, |
901 | struct sockaddr *sa, int salen, | |
902 | int flags) | |
903 | { | |
904 | return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags); | |
905 | } | |
906 | ||
360d8738 | 907 | static struct svc_xprt_ops svc_udp_ops = { |
b700cbb1 | 908 | .xpo_create = svc_udp_create, |
5d137990 TT |
909 | .xpo_recvfrom = svc_udp_recvfrom, |
910 | .xpo_sendto = svc_udp_sendto, | |
5148bf4e | 911 | .xpo_release_rqst = svc_release_skb, |
755cceab TT |
912 | .xpo_detach = svc_sock_detach, |
913 | .xpo_free = svc_sock_free, | |
e831fe65 | 914 | .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr, |
323bee32 | 915 | .xpo_has_wspace = svc_udp_has_wspace, |
38a417cc | 916 | .xpo_accept = svc_udp_accept, |
360d8738 TT |
917 | }; |
918 | ||
919 | static struct svc_xprt_class svc_udp_class = { | |
920 | .xcl_name = "udp", | |
b700cbb1 | 921 | .xcl_owner = THIS_MODULE, |
360d8738 | 922 | .xcl_ops = &svc_udp_ops, |
49023155 | 923 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP, |
360d8738 TT |
924 | }; |
925 | ||
bb5cf160 | 926 | static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv) |
1da177e4 | 927 | { |
7a37f578 N |
928 | int one = 1; |
929 | mm_segment_t oldfs; | |
930 | ||
bb5cf160 | 931 | svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv); |
def13d74 | 932 | clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
933 | svsk->sk_sk->sk_data_ready = svc_udp_data_ready; |
934 | svsk->sk_sk->sk_write_space = svc_write_space; | |
1da177e4 LT |
935 | |
936 | /* initialise setting must have enough space to | |
cca5172a | 937 | * receive and respond to one request. |
1da177e4 LT |
938 | * svc_udp_recvfrom will re-adjust if necessary |
939 | */ | |
940 | svc_sock_setbufsize(svsk->sk_sock, | |
bb5cf160 TT |
941 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
942 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); | |
1da177e4 | 943 | |
02fc6c36 TT |
944 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */ |
945 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); | |
7a37f578 N |
946 | |
947 | oldfs = get_fs(); | |
948 | set_fs(KERNEL_DS); | |
949 | /* make sure we get destination address info */ | |
950 | svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO, | |
951 | (char __user *)&one, sizeof(one)); | |
952 | set_fs(oldfs); | |
1da177e4 LT |
953 | } |
954 | ||
955 | /* | |
956 | * A data_ready event on a listening socket means there's a connection | |
957 | * pending. Do not use state_change as a substitute for it. | |
958 | */ | |
959 | static void | |
960 | svc_tcp_listen_data_ready(struct sock *sk, int count_unused) | |
961 | { | |
939bb7ef | 962 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 LT |
963 | |
964 | dprintk("svc: socket %p TCP (listen) state change %d\n", | |
939bb7ef | 965 | sk, sk->sk_state); |
1da177e4 | 966 | |
939bb7ef NB |
967 | /* |
968 | * This callback may called twice when a new connection | |
969 | * is established as a child socket inherits everything | |
970 | * from a parent LISTEN socket. | |
971 | * 1) data_ready method of the parent socket will be called | |
972 | * when one of child sockets become ESTABLISHED. | |
973 | * 2) data_ready method of the child socket may be called | |
974 | * when it receives data before the socket is accepted. | |
975 | * In case of 2, we should ignore it silently. | |
976 | */ | |
977 | if (sk->sk_state == TCP_LISTEN) { | |
978 | if (svsk) { | |
02fc6c36 | 979 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 980 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef NB |
981 | } else |
982 | printk("svc: socket %p: no user data\n", sk); | |
1da177e4 | 983 | } |
939bb7ef | 984 | |
1da177e4 LT |
985 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
986 | wake_up_interruptible_all(sk->sk_sleep); | |
987 | } | |
988 | ||
989 | /* | |
990 | * A state change on a connected socket means it's dying or dead. | |
991 | */ | |
992 | static void | |
993 | svc_tcp_state_change(struct sock *sk) | |
994 | { | |
939bb7ef | 995 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 LT |
996 | |
997 | dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n", | |
939bb7ef | 998 | sk, sk->sk_state, sk->sk_user_data); |
1da177e4 | 999 | |
939bb7ef | 1000 | if (!svsk) |
1da177e4 | 1001 | printk("svc: socket %p: no user data\n", sk); |
939bb7ef | 1002 | else { |
02fc6c36 | 1003 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 1004 | svc_xprt_enqueue(&svsk->sk_xprt); |
1da177e4 | 1005 | } |
1da177e4 LT |
1006 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
1007 | wake_up_interruptible_all(sk->sk_sleep); | |
1008 | } | |
1009 | ||
1010 | static void | |
1011 | svc_tcp_data_ready(struct sock *sk, int count) | |
1012 | { | |
939bb7ef | 1013 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 LT |
1014 | |
1015 | dprintk("svc: socket %p TCP data ready (svsk %p)\n", | |
939bb7ef NB |
1016 | sk, sk->sk_user_data); |
1017 | if (svsk) { | |
02fc6c36 | 1018 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 1019 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef | 1020 | } |
1da177e4 LT |
1021 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
1022 | wake_up_interruptible(sk->sk_sleep); | |
1023 | } | |
1024 | ||
bcdb81ae CL |
1025 | static inline int svc_port_is_privileged(struct sockaddr *sin) |
1026 | { | |
1027 | switch (sin->sa_family) { | |
1028 | case AF_INET: | |
1029 | return ntohs(((struct sockaddr_in *)sin)->sin_port) | |
1030 | < PROT_SOCK; | |
bcdb81ae CL |
1031 | case AF_INET6: |
1032 | return ntohs(((struct sockaddr_in6 *)sin)->sin6_port) | |
1033 | < PROT_SOCK; | |
bcdb81ae CL |
1034 | default: |
1035 | return 0; | |
1036 | } | |
1037 | } | |
1038 | ||
1da177e4 LT |
1039 | /* |
1040 | * Accept a TCP connection | |
1041 | */ | |
38a417cc | 1042 | static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt) |
1da177e4 | 1043 | { |
38a417cc | 1044 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
cdd88b9f | 1045 | struct sockaddr_storage addr; |
1046 | struct sockaddr *sin = (struct sockaddr *) &addr; | |
bb5cf160 | 1047 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 LT |
1048 | struct socket *sock = svsk->sk_sock; |
1049 | struct socket *newsock; | |
1da177e4 LT |
1050 | struct svc_sock *newsvsk; |
1051 | int err, slen; | |
ad06e4bd | 1052 | char buf[RPC_MAX_ADDRBUFLEN]; |
1da177e4 LT |
1053 | |
1054 | dprintk("svc: tcp_accept %p sock %p\n", svsk, sock); | |
1055 | if (!sock) | |
38a417cc | 1056 | return NULL; |
1da177e4 | 1057 | |
02fc6c36 | 1058 | clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
e6242e92 SS |
1059 | err = kernel_accept(sock, &newsock, O_NONBLOCK); |
1060 | if (err < 0) { | |
1da177e4 LT |
1061 | if (err == -ENOMEM) |
1062 | printk(KERN_WARNING "%s: no more sockets!\n", | |
1063 | serv->sv_name); | |
e6242e92 | 1064 | else if (err != -EAGAIN && net_ratelimit()) |
1da177e4 LT |
1065 | printk(KERN_WARNING "%s: accept failed (err %d)!\n", |
1066 | serv->sv_name, -err); | |
38a417cc | 1067 | return NULL; |
1da177e4 | 1068 | } |
02fc6c36 | 1069 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1070 | |
cdd88b9f | 1071 | err = kernel_getpeername(newsock, sin, &slen); |
1da177e4 LT |
1072 | if (err < 0) { |
1073 | if (net_ratelimit()) | |
1074 | printk(KERN_WARNING "%s: peername failed (err %d)!\n", | |
1075 | serv->sv_name, -err); | |
1076 | goto failed; /* aborted connection or whatever */ | |
1077 | } | |
1078 | ||
1079 | /* Ideally, we would want to reject connections from unauthorized | |
ad06e4bd CL |
1080 | * hosts here, but when we get encryption, the IP of the host won't |
1081 | * tell us anything. For now just warn about unpriv connections. | |
1da177e4 | 1082 | */ |
cdd88b9f | 1083 | if (!svc_port_is_privileged(sin)) { |
1da177e4 | 1084 | dprintk(KERN_WARNING |
ad06e4bd | 1085 | "%s: connect from unprivileged port: %s\n", |
cca5172a | 1086 | serv->sv_name, |
cdd88b9f | 1087 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 | 1088 | } |
ad06e4bd | 1089 | dprintk("%s: connect from %s\n", serv->sv_name, |
cdd88b9f | 1090 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 LT |
1091 | |
1092 | /* make sure that a write doesn't block forever when | |
1093 | * low on memory | |
1094 | */ | |
1095 | newsock->sk->sk_sndtimeo = HZ*30; | |
1096 | ||
6b174337 CL |
1097 | if (!(newsvsk = svc_setup_socket(serv, newsock, &err, |
1098 | (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY)))) | |
1da177e4 | 1099 | goto failed; |
cdd88b9f | 1100 | memcpy(&newsvsk->sk_remote, sin, slen); |
067d7817 | 1101 | newsvsk->sk_remotelen = slen; |
a9747692 FM |
1102 | err = kernel_getsockname(newsock, sin, &slen); |
1103 | if (unlikely(err < 0)) { | |
1104 | dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err); | |
1105 | slen = offsetof(struct sockaddr, sa_data); | |
1106 | } | |
1107 | memcpy(&newsvsk->sk_local, sin, slen); | |
067d7817 | 1108 | |
f9f3cc4f TT |
1109 | if (serv->sv_stats) |
1110 | serv->sv_stats->nettcpconn++; | |
1111 | ||
1112 | return &newsvsk->sk_xprt; | |
1113 | ||
1114 | failed: | |
1115 | sock_release(newsock); | |
1116 | return NULL; | |
1117 | } | |
1118 | ||
1da177e4 LT |
1119 | /* |
1120 | * Receive data from a TCP socket. | |
1121 | */ | |
1122 | static int | |
1123 | svc_tcp_recvfrom(struct svc_rqst *rqstp) | |
1124 | { | |
1125 | struct svc_sock *svsk = rqstp->rq_sock; | |
bb5cf160 | 1126 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 | 1127 | int len; |
3cc03b16 | 1128 | struct kvec *vec; |
1da177e4 LT |
1129 | int pnum, vlen; |
1130 | ||
1131 | dprintk("svc: tcp_recv %p data %d conn %d close %d\n", | |
02fc6c36 TT |
1132 | svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags), |
1133 | test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags), | |
1134 | test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)); | |
1da177e4 | 1135 | |
02fc6c36 | 1136 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
1da177e4 LT |
1137 | /* sndbuf needs to have room for one request |
1138 | * per thread, otherwise we can stall even when the | |
1139 | * network isn't a bottleneck. | |
3262c816 GB |
1140 | * |
1141 | * We count all threads rather than threads in a | |
1142 | * particular pool, which provides an upper bound | |
1143 | * on the number of threads which will access the socket. | |
1144 | * | |
1da177e4 | 1145 | * rcvbuf just needs to be able to hold a few requests. |
cca5172a | 1146 | * Normally they will be removed from the queue |
1da177e4 LT |
1147 | * as soon a a complete request arrives. |
1148 | */ | |
1149 | svc_sock_setbufsize(svsk->sk_sock, | |
c6b0a9f8 N |
1150 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
1151 | 3 * serv->sv_max_mesg); | |
1da177e4 | 1152 | |
02fc6c36 | 1153 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1154 | |
1155 | /* Receive data. If we haven't got the record length yet, get | |
1156 | * the next four bytes. Otherwise try to gobble up as much as | |
1157 | * possible up to the complete record length. | |
1158 | */ | |
1159 | if (svsk->sk_tcplen < 4) { | |
1160 | unsigned long want = 4 - svsk->sk_tcplen; | |
1161 | struct kvec iov; | |
1162 | ||
1163 | iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen; | |
1164 | iov.iov_len = want; | |
1165 | if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0) | |
1166 | goto error; | |
1167 | svsk->sk_tcplen += len; | |
1168 | ||
1169 | if (len < want) { | |
1170 | dprintk("svc: short recvfrom while reading record length (%d of %lu)\n", | |
cca5172a | 1171 | len, want); |
a6046f71 | 1172 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1173 | return -EAGAIN; /* record header not complete */ |
1174 | } | |
1175 | ||
1176 | svsk->sk_reclen = ntohl(svsk->sk_reclen); | |
1177 | if (!(svsk->sk_reclen & 0x80000000)) { | |
1178 | /* FIXME: technically, a record can be fragmented, | |
1179 | * and non-terminal fragments will not have the top | |
1180 | * bit set in the fragment length header. | |
1181 | * But apparently no known nfs clients send fragmented | |
1182 | * records. */ | |
34e9a63b N |
1183 | if (net_ratelimit()) |
1184 | printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx" | |
1185 | " (non-terminal)\n", | |
1186 | (unsigned long) svsk->sk_reclen); | |
1da177e4 LT |
1187 | goto err_delete; |
1188 | } | |
1189 | svsk->sk_reclen &= 0x7fffffff; | |
1190 | dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen); | |
c6b0a9f8 | 1191 | if (svsk->sk_reclen > serv->sv_max_mesg) { |
34e9a63b N |
1192 | if (net_ratelimit()) |
1193 | printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx" | |
1194 | " (large)\n", | |
1195 | (unsigned long) svsk->sk_reclen); | |
1da177e4 LT |
1196 | goto err_delete; |
1197 | } | |
1198 | } | |
1199 | ||
1200 | /* Check whether enough data is available */ | |
1201 | len = svc_recv_available(svsk); | |
1202 | if (len < 0) | |
1203 | goto error; | |
1204 | ||
1205 | if (len < svsk->sk_reclen) { | |
1206 | dprintk("svc: incomplete TCP record (%d of %d)\n", | |
1207 | len, svsk->sk_reclen); | |
a6046f71 | 1208 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1209 | return -EAGAIN; /* record not complete */ |
1210 | } | |
1211 | len = svsk->sk_reclen; | |
02fc6c36 | 1212 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1213 | |
3cc03b16 | 1214 | vec = rqstp->rq_vec; |
1da177e4 LT |
1215 | vec[0] = rqstp->rq_arg.head[0]; |
1216 | vlen = PAGE_SIZE; | |
1217 | pnum = 1; | |
1218 | while (vlen < len) { | |
44524359 | 1219 | vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]); |
1da177e4 LT |
1220 | vec[pnum].iov_len = PAGE_SIZE; |
1221 | pnum++; | |
1222 | vlen += PAGE_SIZE; | |
1223 | } | |
44524359 | 1224 | rqstp->rq_respages = &rqstp->rq_pages[pnum]; |
1da177e4 LT |
1225 | |
1226 | /* Now receive data */ | |
1227 | len = svc_recvfrom(rqstp, vec, pnum, len); | |
1228 | if (len < 0) | |
1229 | goto error; | |
1230 | ||
1231 | dprintk("svc: TCP complete record (%d bytes)\n", len); | |
1232 | rqstp->rq_arg.len = len; | |
1233 | rqstp->rq_arg.page_base = 0; | |
1234 | if (len <= rqstp->rq_arg.head[0].iov_len) { | |
1235 | rqstp->rq_arg.head[0].iov_len = len; | |
1236 | rqstp->rq_arg.page_len = 0; | |
1237 | } else { | |
1238 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; | |
1239 | } | |
1240 | ||
5148bf4e | 1241 | rqstp->rq_xprt_ctxt = NULL; |
1da177e4 LT |
1242 | rqstp->rq_prot = IPPROTO_TCP; |
1243 | ||
1244 | /* Reset TCP read info */ | |
1245 | svsk->sk_reclen = 0; | |
1246 | svsk->sk_tcplen = 0; | |
1247 | ||
a6046f71 | 1248 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1249 | if (serv->sv_stats) |
1250 | serv->sv_stats->nettcpcnt++; | |
1251 | ||
1252 | return len; | |
1253 | ||
1254 | err_delete: | |
02fc6c36 | 1255 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1256 | return -EAGAIN; |
1257 | ||
1258 | error: | |
1259 | if (len == -EAGAIN) { | |
1260 | dprintk("RPC: TCP recvfrom got EAGAIN\n"); | |
a6046f71 | 1261 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1262 | } else { |
1263 | printk(KERN_NOTICE "%s: recvfrom returned errno %d\n", | |
bb5cf160 | 1264 | svsk->sk_xprt.xpt_server->sv_name, -len); |
93fbf1a5 | 1265 | goto err_delete; |
1da177e4 LT |
1266 | } |
1267 | ||
1268 | return len; | |
1269 | } | |
1270 | ||
1271 | /* | |
1272 | * Send out data on TCP socket. | |
1273 | */ | |
1274 | static int | |
1275 | svc_tcp_sendto(struct svc_rqst *rqstp) | |
1276 | { | |
1277 | struct xdr_buf *xbufp = &rqstp->rq_res; | |
1278 | int sent; | |
d8ed029d | 1279 | __be32 reclen; |
1da177e4 LT |
1280 | |
1281 | /* Set up the first element of the reply kvec. | |
1282 | * Any other kvecs that may be in use have been taken | |
1283 | * care of by the server implementation itself. | |
1284 | */ | |
1285 | reclen = htonl(0x80000000|((xbufp->len ) - 4)); | |
1286 | memcpy(xbufp->head[0].iov_base, &reclen, 4); | |
1287 | ||
02fc6c36 | 1288 | if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags)) |
1da177e4 LT |
1289 | return -ENOTCONN; |
1290 | ||
1291 | sent = svc_sendto(rqstp, &rqstp->rq_res); | |
1292 | if (sent != xbufp->len) { | |
1293 | printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n", | |
bb5cf160 | 1294 | rqstp->rq_sock->sk_xprt.xpt_server->sv_name, |
1da177e4 LT |
1295 | (sent<0)?"got error":"sent only", |
1296 | sent, xbufp->len); | |
02fc6c36 | 1297 | set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags); |
f6150c3c | 1298 | svc_xprt_enqueue(rqstp->rq_xprt); |
1da177e4 LT |
1299 | sent = -EAGAIN; |
1300 | } | |
1301 | return sent; | |
1302 | } | |
1303 | ||
e831fe65 TT |
1304 | /* |
1305 | * Setup response header. TCP has a 4B record length field. | |
1306 | */ | |
1307 | static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp) | |
1308 | { | |
1309 | struct kvec *resv = &rqstp->rq_res.head[0]; | |
1310 | ||
1311 | /* tcp needs a space for the record length... */ | |
1312 | svc_putnl(resv, 0); | |
1313 | } | |
1314 | ||
323bee32 TT |
1315 | static int svc_tcp_has_wspace(struct svc_xprt *xprt) |
1316 | { | |
1317 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
bb5cf160 | 1318 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
323bee32 TT |
1319 | int required; |
1320 | int wspace; | |
1321 | ||
1322 | /* | |
1323 | * Set the SOCK_NOSPACE flag before checking the available | |
1324 | * sock space. | |
1325 | */ | |
1326 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
7a90e8cc | 1327 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
323bee32 TT |
1328 | wspace = sk_stream_wspace(svsk->sk_sk); |
1329 | ||
1330 | if (wspace < sk_stream_min_wspace(svsk->sk_sk)) | |
1331 | return 0; | |
1332 | if (required * 2 > wspace) | |
1333 | return 0; | |
1334 | ||
1335 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
1336 | return 1; | |
1337 | } | |
1338 | ||
b700cbb1 TT |
1339 | static struct svc_xprt *svc_tcp_create(struct svc_serv *serv, |
1340 | struct sockaddr *sa, int salen, | |
1341 | int flags) | |
1342 | { | |
1343 | return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags); | |
1344 | } | |
1345 | ||
360d8738 | 1346 | static struct svc_xprt_ops svc_tcp_ops = { |
b700cbb1 | 1347 | .xpo_create = svc_tcp_create, |
5d137990 TT |
1348 | .xpo_recvfrom = svc_tcp_recvfrom, |
1349 | .xpo_sendto = svc_tcp_sendto, | |
5148bf4e | 1350 | .xpo_release_rqst = svc_release_skb, |
755cceab TT |
1351 | .xpo_detach = svc_sock_detach, |
1352 | .xpo_free = svc_sock_free, | |
e831fe65 | 1353 | .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr, |
323bee32 | 1354 | .xpo_has_wspace = svc_tcp_has_wspace, |
38a417cc | 1355 | .xpo_accept = svc_tcp_accept, |
360d8738 TT |
1356 | }; |
1357 | ||
1358 | static struct svc_xprt_class svc_tcp_class = { | |
1359 | .xcl_name = "tcp", | |
b700cbb1 | 1360 | .xcl_owner = THIS_MODULE, |
360d8738 | 1361 | .xcl_ops = &svc_tcp_ops, |
49023155 | 1362 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, |
360d8738 TT |
1363 | }; |
1364 | ||
1365 | void svc_init_xprt_sock(void) | |
1366 | { | |
1367 | svc_reg_xprt_class(&svc_tcp_class); | |
1368 | svc_reg_xprt_class(&svc_udp_class); | |
1369 | } | |
1370 | ||
1371 | void svc_cleanup_xprt_sock(void) | |
1372 | { | |
1373 | svc_unreg_xprt_class(&svc_tcp_class); | |
1374 | svc_unreg_xprt_class(&svc_udp_class); | |
1375 | } | |
1376 | ||
bb5cf160 | 1377 | static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv) |
1da177e4 LT |
1378 | { |
1379 | struct sock *sk = svsk->sk_sk; | |
1380 | struct tcp_sock *tp = tcp_sk(sk); | |
1381 | ||
bb5cf160 | 1382 | svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv); |
def13d74 | 1383 | set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1384 | if (sk->sk_state == TCP_LISTEN) { |
1385 | dprintk("setting up TCP socket for listening\n"); | |
02fc6c36 | 1386 | set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1387 | sk->sk_data_ready = svc_tcp_listen_data_ready; |
02fc6c36 | 1388 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1389 | } else { |
1390 | dprintk("setting up TCP socket for reading\n"); | |
1391 | sk->sk_state_change = svc_tcp_state_change; | |
1392 | sk->sk_data_ready = svc_tcp_data_ready; | |
1393 | sk->sk_write_space = svc_write_space; | |
1394 | ||
1395 | svsk->sk_reclen = 0; | |
1396 | svsk->sk_tcplen = 0; | |
1397 | ||
1398 | tp->nonagle = 1; /* disable Nagle's algorithm */ | |
1399 | ||
1400 | /* initialise setting must have enough space to | |
cca5172a | 1401 | * receive and respond to one request. |
1da177e4 LT |
1402 | * svc_tcp_recvfrom will re-adjust if necessary |
1403 | */ | |
1404 | svc_sock_setbufsize(svsk->sk_sock, | |
bb5cf160 TT |
1405 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
1406 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); | |
1da177e4 | 1407 | |
02fc6c36 TT |
1408 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
1409 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); | |
cca5172a | 1410 | if (sk->sk_state != TCP_ESTABLISHED) |
02fc6c36 | 1411 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1412 | } |
1413 | } | |
1414 | ||
1415 | void | |
1416 | svc_sock_update_bufs(struct svc_serv *serv) | |
1417 | { | |
1418 | /* | |
1419 | * The number of server threads has changed. Update | |
1420 | * rcvbuf and sndbuf accordingly on all sockets | |
1421 | */ | |
1422 | struct list_head *le; | |
1423 | ||
1424 | spin_lock_bh(&serv->sv_lock); | |
1425 | list_for_each(le, &serv->sv_permsocks) { | |
cca5172a | 1426 | struct svc_sock *svsk = |
7a182083 | 1427 | list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
02fc6c36 | 1428 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1429 | } |
1430 | list_for_each(le, &serv->sv_tempsocks) { | |
1431 | struct svc_sock *svsk = | |
7a182083 | 1432 | list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
02fc6c36 | 1433 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1434 | } |
1435 | spin_unlock_bh(&serv->sv_lock); | |
1436 | } | |
1437 | ||
e1b3157f TT |
1438 | /* |
1439 | * Make sure that we don't have too many active connections. If we | |
1440 | * have, something must be dropped. | |
1441 | * | |
1442 | * There's no point in trying to do random drop here for DoS | |
1443 | * prevention. The NFS clients does 1 reconnect in 15 seconds. An | |
1444 | * attacker can easily beat that. | |
1445 | * | |
1446 | * The only somewhat efficient mechanism would be if drop old | |
1447 | * connections from the same IP first. But right now we don't even | |
1448 | * record the client IP in svc_sock. | |
1449 | */ | |
1450 | static void svc_check_conn_limits(struct svc_serv *serv) | |
1451 | { | |
1452 | if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) { | |
1453 | struct svc_sock *svsk = NULL; | |
1454 | spin_lock_bh(&serv->sv_lock); | |
1455 | if (!list_empty(&serv->sv_tempsocks)) { | |
1456 | if (net_ratelimit()) { | |
1457 | /* Try to help the admin */ | |
1458 | printk(KERN_NOTICE "%s: too many open TCP " | |
1459 | "sockets, consider increasing the " | |
1460 | "number of nfsd threads\n", | |
1461 | serv->sv_name); | |
1462 | } | |
1463 | /* | |
1464 | * Always select the oldest socket. It's not fair, | |
1465 | * but so is life | |
1466 | */ | |
1467 | svsk = list_entry(serv->sv_tempsocks.prev, | |
1468 | struct svc_sock, | |
7a182083 | 1469 | sk_xprt.xpt_list); |
02fc6c36 | 1470 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
e1b3157f TT |
1471 | svc_xprt_get(&svsk->sk_xprt); |
1472 | } | |
1473 | spin_unlock_bh(&serv->sv_lock); | |
1474 | ||
1475 | if (svsk) { | |
f6150c3c | 1476 | svc_xprt_enqueue(&svsk->sk_xprt); |
e1b3157f TT |
1477 | svc_xprt_put(&svsk->sk_xprt); |
1478 | } | |
1479 | } | |
1480 | } | |
1481 | ||
1da177e4 | 1482 | /* |
3262c816 GB |
1483 | * Receive the next request on any socket. This code is carefully |
1484 | * organised not to touch any cachelines in the shared svc_serv | |
1485 | * structure, only cachelines in the local svc_pool. | |
1da177e4 LT |
1486 | */ |
1487 | int | |
6fb2b47f | 1488 | svc_recv(struct svc_rqst *rqstp, long timeout) |
1da177e4 | 1489 | { |
27459f09 | 1490 | struct svc_sock *svsk = NULL; |
6fb2b47f | 1491 | struct svc_serv *serv = rqstp->rq_server; |
3262c816 | 1492 | struct svc_pool *pool = rqstp->rq_pool; |
44524359 | 1493 | int len, i; |
1da177e4 LT |
1494 | int pages; |
1495 | struct xdr_buf *arg; | |
1496 | DECLARE_WAITQUEUE(wait, current); | |
1497 | ||
1498 | dprintk("svc: server %p waiting for data (to = %ld)\n", | |
1499 | rqstp, timeout); | |
1500 | ||
1501 | if (rqstp->rq_sock) | |
cca5172a | 1502 | printk(KERN_ERR |
1da177e4 LT |
1503 | "svc_recv: service %p, socket not NULL!\n", |
1504 | rqstp); | |
1505 | if (waitqueue_active(&rqstp->rq_wait)) | |
cca5172a | 1506 | printk(KERN_ERR |
1da177e4 LT |
1507 | "svc_recv: service %p, wait queue active!\n", |
1508 | rqstp); | |
1509 | ||
1da177e4 LT |
1510 | |
1511 | /* now allocate needed pages. If we get a failure, sleep briefly */ | |
c6b0a9f8 | 1512 | pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE; |
44524359 N |
1513 | for (i=0; i < pages ; i++) |
1514 | while (rqstp->rq_pages[i] == NULL) { | |
1515 | struct page *p = alloc_page(GFP_KERNEL); | |
1516 | if (!p) | |
1517 | schedule_timeout_uninterruptible(msecs_to_jiffies(500)); | |
1518 | rqstp->rq_pages[i] = p; | |
1da177e4 | 1519 | } |
250f3915 N |
1520 | rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */ |
1521 | BUG_ON(pages >= RPCSVC_MAXPAGES); | |
1da177e4 LT |
1522 | |
1523 | /* Make arg->head point to first page and arg->pages point to rest */ | |
1524 | arg = &rqstp->rq_arg; | |
44524359 | 1525 | arg->head[0].iov_base = page_address(rqstp->rq_pages[0]); |
1da177e4 | 1526 | arg->head[0].iov_len = PAGE_SIZE; |
44524359 | 1527 | arg->pages = rqstp->rq_pages + 1; |
1da177e4 LT |
1528 | arg->page_base = 0; |
1529 | /* save at least one page for response */ | |
1530 | arg->page_len = (pages-2)*PAGE_SIZE; | |
1531 | arg->len = (pages-1)*PAGE_SIZE; | |
1532 | arg->tail[0].iov_len = 0; | |
3e1d1d28 CL |
1533 | |
1534 | try_to_freeze(); | |
1887b935 | 1535 | cond_resched(); |
1da177e4 LT |
1536 | if (signalled()) |
1537 | return -EINTR; | |
1538 | ||
3262c816 GB |
1539 | spin_lock_bh(&pool->sp_lock); |
1540 | if ((svsk = svc_sock_dequeue(pool)) != NULL) { | |
1da177e4 | 1541 | rqstp->rq_sock = svsk; |
e1b3157f | 1542 | svc_xprt_get(&svsk->sk_xprt); |
c6b0a9f8 | 1543 | rqstp->rq_reserved = serv->sv_max_mesg; |
7a90e8cc | 1544 | atomic_add(rqstp->rq_reserved, &svsk->sk_xprt.xpt_reserved); |
1da177e4 LT |
1545 | } else { |
1546 | /* No data pending. Go to sleep */ | |
3262c816 | 1547 | svc_thread_enqueue(pool, rqstp); |
1da177e4 LT |
1548 | |
1549 | /* | |
1550 | * We have to be able to interrupt this wait | |
1551 | * to bring down the daemons ... | |
1552 | */ | |
1553 | set_current_state(TASK_INTERRUPTIBLE); | |
1554 | add_wait_queue(&rqstp->rq_wait, &wait); | |
3262c816 | 1555 | spin_unlock_bh(&pool->sp_lock); |
1da177e4 LT |
1556 | |
1557 | schedule_timeout(timeout); | |
1558 | ||
3e1d1d28 | 1559 | try_to_freeze(); |
1da177e4 | 1560 | |
3262c816 | 1561 | spin_lock_bh(&pool->sp_lock); |
1da177e4 LT |
1562 | remove_wait_queue(&rqstp->rq_wait, &wait); |
1563 | ||
1564 | if (!(svsk = rqstp->rq_sock)) { | |
3262c816 GB |
1565 | svc_thread_dequeue(pool, rqstp); |
1566 | spin_unlock_bh(&pool->sp_lock); | |
1da177e4 LT |
1567 | dprintk("svc: server %p, no data yet\n", rqstp); |
1568 | return signalled()? -EINTR : -EAGAIN; | |
1569 | } | |
1570 | } | |
3262c816 | 1571 | spin_unlock_bh(&pool->sp_lock); |
1da177e4 | 1572 | |
d7979ae4 | 1573 | len = 0; |
02fc6c36 TT |
1574 | if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)) { |
1575 | dprintk("svc_recv: found XPT_CLOSE\n"); | |
7a182083 | 1576 | svc_delete_xprt(&svsk->sk_xprt); |
02fc6c36 | 1577 | } else if (test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags)) { |
38a417cc TT |
1578 | struct svc_xprt *newxpt; |
1579 | newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt); | |
b700cbb1 TT |
1580 | if (newxpt) { |
1581 | /* | |
1582 | * We know this module_get will succeed because the | |
1583 | * listener holds a reference too | |
1584 | */ | |
1585 | __module_get(newxpt->xpt_class->xcl_owner); | |
bb5cf160 | 1586 | svc_check_conn_limits(svsk->sk_xprt.xpt_server); |
6bc5ab13 | 1587 | svc_xprt_received(newxpt); |
b700cbb1 | 1588 | } |
a6046f71 | 1589 | svc_xprt_received(&svsk->sk_xprt); |
d7979ae4 TT |
1590 | } else { |
1591 | dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n", | |
e1b3157f TT |
1592 | rqstp, pool->sp_id, svsk, |
1593 | atomic_read(&svsk->sk_xprt.xpt_ref.refcount)); | |
8c7b0172 TT |
1594 | rqstp->rq_deferred = svc_deferred_dequeue(&svsk->sk_xprt); |
1595 | if (rqstp->rq_deferred) { | |
1596 | svc_xprt_received(&svsk->sk_xprt); | |
1597 | len = svc_deferred_recv(rqstp); | |
1598 | } else | |
1599 | len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp); | |
d7979ae4 TT |
1600 | dprintk("svc: got len=%d\n", len); |
1601 | } | |
1da177e4 LT |
1602 | |
1603 | /* No data, incomplete (TCP) read, or accept() */ | |
1604 | if (len == 0 || len == -EAGAIN) { | |
1605 | rqstp->rq_res.len = 0; | |
1606 | svc_sock_release(rqstp); | |
1607 | return -EAGAIN; | |
1608 | } | |
02fc6c36 | 1609 | clear_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1610 | |
bcdb81ae | 1611 | rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp)); |
1da177e4 LT |
1612 | rqstp->rq_chandle.defer = svc_defer; |
1613 | ||
1614 | if (serv->sv_stats) | |
1615 | serv->sv_stats->netcnt++; | |
1616 | return len; | |
1617 | } | |
1618 | ||
cca5172a | 1619 | /* |
1da177e4 LT |
1620 | * Drop request |
1621 | */ | |
1622 | void | |
1623 | svc_drop(struct svc_rqst *rqstp) | |
1624 | { | |
1625 | dprintk("svc: socket %p dropped request\n", rqstp->rq_sock); | |
1626 | svc_sock_release(rqstp); | |
1627 | } | |
1628 | ||
1629 | /* | |
1630 | * Return reply to client. | |
1631 | */ | |
1632 | int | |
1633 | svc_send(struct svc_rqst *rqstp) | |
1634 | { | |
a50fea26 | 1635 | struct svc_xprt *xprt; |
1da177e4 LT |
1636 | int len; |
1637 | struct xdr_buf *xb; | |
1638 | ||
a50fea26 TT |
1639 | xprt = rqstp->rq_xprt; |
1640 | if (!xprt) | |
1da177e4 | 1641 | return -EFAULT; |
1da177e4 LT |
1642 | |
1643 | /* release the receive skb before sending the reply */ | |
5148bf4e | 1644 | rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp); |
1da177e4 LT |
1645 | |
1646 | /* calculate over-all length */ | |
1647 | xb = & rqstp->rq_res; | |
1648 | xb->len = xb->head[0].iov_len + | |
1649 | xb->page_len + | |
1650 | xb->tail[0].iov_len; | |
1651 | ||
a50fea26 TT |
1652 | /* Grab mutex to serialize outgoing data. */ |
1653 | mutex_lock(&xprt->xpt_mutex); | |
1654 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) | |
1da177e4 LT |
1655 | len = -ENOTCONN; |
1656 | else | |
a50fea26 TT |
1657 | len = xprt->xpt_ops->xpo_sendto(rqstp); |
1658 | mutex_unlock(&xprt->xpt_mutex); | |
1da177e4 LT |
1659 | svc_sock_release(rqstp); |
1660 | ||
1661 | if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN) | |
1662 | return 0; | |
1663 | return len; | |
1664 | } | |
1665 | ||
36bdfc8b GB |
1666 | /* |
1667 | * Timer function to close old temporary sockets, using | |
1668 | * a mark-and-sweep algorithm. | |
1669 | */ | |
1670 | static void | |
1671 | svc_age_temp_sockets(unsigned long closure) | |
1672 | { | |
1673 | struct svc_serv *serv = (struct svc_serv *)closure; | |
1674 | struct svc_sock *svsk; | |
1675 | struct list_head *le, *next; | |
1676 | LIST_HEAD(to_be_aged); | |
1677 | ||
1678 | dprintk("svc_age_temp_sockets\n"); | |
1679 | ||
1680 | if (!spin_trylock_bh(&serv->sv_lock)) { | |
1681 | /* busy, try again 1 sec later */ | |
1682 | dprintk("svc_age_temp_sockets: busy\n"); | |
1683 | mod_timer(&serv->sv_temptimer, jiffies + HZ); | |
1684 | return; | |
1685 | } | |
1686 | ||
1687 | list_for_each_safe(le, next, &serv->sv_tempsocks) { | |
7a182083 | 1688 | svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
36bdfc8b | 1689 | |
02fc6c36 | 1690 | if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags)) |
36bdfc8b | 1691 | continue; |
e1b3157f | 1692 | if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1 |
02fc6c36 | 1693 | || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)) |
36bdfc8b | 1694 | continue; |
e1b3157f | 1695 | svc_xprt_get(&svsk->sk_xprt); |
36bdfc8b | 1696 | list_move(le, &to_be_aged); |
02fc6c36 TT |
1697 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
1698 | set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags); | |
36bdfc8b GB |
1699 | } |
1700 | spin_unlock_bh(&serv->sv_lock); | |
1701 | ||
1702 | while (!list_empty(&to_be_aged)) { | |
1703 | le = to_be_aged.next; | |
7a182083 | 1704 | /* fiddling the sk_xprt.xpt_list node is safe 'cos we're XPT_DETACHED */ |
36bdfc8b | 1705 | list_del_init(le); |
7a182083 | 1706 | svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
36bdfc8b | 1707 | |
4bc6c497 | 1708 | dprintk("queuing svsk %p for closing\n", svsk); |
36bdfc8b GB |
1709 | |
1710 | /* a thread will dequeue and close it soon */ | |
f6150c3c | 1711 | svc_xprt_enqueue(&svsk->sk_xprt); |
e1b3157f | 1712 | svc_xprt_put(&svsk->sk_xprt); |
36bdfc8b GB |
1713 | } |
1714 | ||
1715 | mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ); | |
1716 | } | |
1717 | ||
1da177e4 LT |
1718 | /* |
1719 | * Initialize socket for RPC use and create svc_sock struct | |
1720 | * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF. | |
1721 | */ | |
6b174337 CL |
1722 | static struct svc_sock *svc_setup_socket(struct svc_serv *serv, |
1723 | struct socket *sock, | |
1724 | int *errp, int flags) | |
1da177e4 LT |
1725 | { |
1726 | struct svc_sock *svsk; | |
1727 | struct sock *inet; | |
6b174337 CL |
1728 | int pmap_register = !(flags & SVC_SOCK_ANONYMOUS); |
1729 | int is_temporary = flags & SVC_SOCK_TEMPORARY; | |
1da177e4 LT |
1730 | |
1731 | dprintk("svc: svc_setup_socket %p\n", sock); | |
0da974f4 | 1732 | if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) { |
1da177e4 LT |
1733 | *errp = -ENOMEM; |
1734 | return NULL; | |
1735 | } | |
1da177e4 LT |
1736 | |
1737 | inet = sock->sk; | |
1738 | ||
1739 | /* Register socket with portmapper */ | |
1740 | if (*errp >= 0 && pmap_register) | |
1741 | *errp = svc_register(serv, inet->sk_protocol, | |
1742 | ntohs(inet_sk(inet)->sport)); | |
1743 | ||
1744 | if (*errp < 0) { | |
1745 | kfree(svsk); | |
1746 | return NULL; | |
1747 | } | |
1748 | ||
02fc6c36 | 1749 | set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1750 | inet->sk_user_data = svsk; |
1751 | svsk->sk_sock = sock; | |
1752 | svsk->sk_sk = inet; | |
1753 | svsk->sk_ostate = inet->sk_state_change; | |
1754 | svsk->sk_odata = inet->sk_data_ready; | |
1755 | svsk->sk_owspace = inet->sk_write_space; | |
1da177e4 LT |
1756 | |
1757 | /* Initialize the socket */ | |
1758 | if (sock->type == SOCK_DGRAM) | |
bb5cf160 | 1759 | svc_udp_init(svsk, serv); |
1da177e4 | 1760 | else |
bb5cf160 | 1761 | svc_tcp_init(svsk, serv); |
1da177e4 LT |
1762 | |
1763 | spin_lock_bh(&serv->sv_lock); | |
6b174337 | 1764 | if (is_temporary) { |
02fc6c36 | 1765 | set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags); |
7a182083 | 1766 | list_add(&svsk->sk_xprt.xpt_list, &serv->sv_tempsocks); |
1da177e4 | 1767 | serv->sv_tmpcnt++; |
36bdfc8b GB |
1768 | if (serv->sv_temptimer.function == NULL) { |
1769 | /* setup timer to age temp sockets */ | |
1770 | setup_timer(&serv->sv_temptimer, svc_age_temp_sockets, | |
1771 | (unsigned long)serv); | |
1772 | mod_timer(&serv->sv_temptimer, | |
1773 | jiffies + svc_conn_age_period * HZ); | |
1774 | } | |
1da177e4 | 1775 | } else { |
02fc6c36 | 1776 | clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags); |
7a182083 | 1777 | list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks); |
1da177e4 LT |
1778 | } |
1779 | spin_unlock_bh(&serv->sv_lock); | |
1780 | ||
1781 | dprintk("svc: svc_setup_socket created %p (inet %p)\n", | |
1782 | svsk, svsk->sk_sk); | |
1783 | ||
1da177e4 LT |
1784 | return svsk; |
1785 | } | |
1786 | ||
b41b66d6 N |
1787 | int svc_addsock(struct svc_serv *serv, |
1788 | int fd, | |
1789 | char *name_return, | |
1790 | int *proto) | |
1791 | { | |
1792 | int err = 0; | |
1793 | struct socket *so = sockfd_lookup(fd, &err); | |
1794 | struct svc_sock *svsk = NULL; | |
1795 | ||
1796 | if (!so) | |
1797 | return err; | |
1798 | if (so->sk->sk_family != AF_INET) | |
1799 | err = -EAFNOSUPPORT; | |
1800 | else if (so->sk->sk_protocol != IPPROTO_TCP && | |
1801 | so->sk->sk_protocol != IPPROTO_UDP) | |
1802 | err = -EPROTONOSUPPORT; | |
1803 | else if (so->state > SS_UNCONNECTED) | |
1804 | err = -EISCONN; | |
1805 | else { | |
6b174337 | 1806 | svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS); |
e79eff1f | 1807 | if (svsk) { |
a6046f71 | 1808 | svc_xprt_received(&svsk->sk_xprt); |
b41b66d6 | 1809 | err = 0; |
e79eff1f | 1810 | } |
b41b66d6 N |
1811 | } |
1812 | if (err) { | |
1813 | sockfd_put(so); | |
1814 | return err; | |
1815 | } | |
1816 | if (proto) *proto = so->sk->sk_protocol; | |
1817 | return one_sock_name(name_return, svsk); | |
1818 | } | |
1819 | EXPORT_SYMBOL_GPL(svc_addsock); | |
1820 | ||
1da177e4 LT |
1821 | /* |
1822 | * Create socket for RPC service. | |
1823 | */ | |
b700cbb1 TT |
1824 | static struct svc_xprt *svc_create_socket(struct svc_serv *serv, |
1825 | int protocol, | |
1826 | struct sockaddr *sin, int len, | |
1827 | int flags) | |
1da177e4 LT |
1828 | { |
1829 | struct svc_sock *svsk; | |
1830 | struct socket *sock; | |
1831 | int error; | |
1832 | int type; | |
ad06e4bd | 1833 | char buf[RPC_MAX_ADDRBUFLEN]; |
1da177e4 | 1834 | |
ad06e4bd CL |
1835 | dprintk("svc: svc_create_socket(%s, %d, %s)\n", |
1836 | serv->sv_program->pg_name, protocol, | |
77f1f67a | 1837 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 LT |
1838 | |
1839 | if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) { | |
1840 | printk(KERN_WARNING "svc: only UDP and TCP " | |
1841 | "sockets supported\n"); | |
b700cbb1 | 1842 | return ERR_PTR(-EINVAL); |
1da177e4 LT |
1843 | } |
1844 | type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM; | |
1845 | ||
77f1f67a CL |
1846 | error = sock_create_kern(sin->sa_family, type, protocol, &sock); |
1847 | if (error < 0) | |
b700cbb1 | 1848 | return ERR_PTR(error); |
1da177e4 | 1849 | |
ed07536e PZ |
1850 | svc_reclassify_socket(sock); |
1851 | ||
18114746 | 1852 | if (type == SOCK_STREAM) |
77f1f67a CL |
1853 | sock->sk->sk_reuse = 1; /* allow address reuse */ |
1854 | error = kernel_bind(sock, sin, len); | |
18114746 ES |
1855 | if (error < 0) |
1856 | goto bummer; | |
1da177e4 LT |
1857 | |
1858 | if (protocol == IPPROTO_TCP) { | |
e6242e92 | 1859 | if ((error = kernel_listen(sock, 64)) < 0) |
1da177e4 LT |
1860 | goto bummer; |
1861 | } | |
1862 | ||
e79eff1f | 1863 | if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) { |
a6046f71 | 1864 | svc_xprt_received(&svsk->sk_xprt); |
b700cbb1 | 1865 | return (struct svc_xprt *)svsk; |
e79eff1f | 1866 | } |
1da177e4 LT |
1867 | |
1868 | bummer: | |
1869 | dprintk("svc: svc_create_socket error = %d\n", -error); | |
1870 | sock_release(sock); | |
b700cbb1 | 1871 | return ERR_PTR(error); |
1da177e4 LT |
1872 | } |
1873 | ||
755cceab TT |
1874 | /* |
1875 | * Detach the svc_sock from the socket so that no | |
1876 | * more callbacks occur. | |
1877 | */ | |
1878 | static void svc_sock_detach(struct svc_xprt *xprt) | |
1879 | { | |
1880 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
1881 | struct sock *sk = svsk->sk_sk; | |
1882 | ||
1883 | dprintk("svc: svc_sock_detach(%p)\n", svsk); | |
1884 | ||
1885 | /* put back the old socket callbacks */ | |
1886 | sk->sk_state_change = svsk->sk_ostate; | |
1887 | sk->sk_data_ready = svsk->sk_odata; | |
1888 | sk->sk_write_space = svsk->sk_owspace; | |
1889 | } | |
1890 | ||
1891 | /* | |
1892 | * Free the svc_sock's socket resources and the svc_sock itself. | |
1893 | */ | |
1894 | static void svc_sock_free(struct svc_xprt *xprt) | |
1895 | { | |
1896 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
1897 | dprintk("svc: svc_sock_free(%p)\n", svsk); | |
1898 | ||
755cceab TT |
1899 | if (svsk->sk_sock->file) |
1900 | sockfd_put(svsk->sk_sock); | |
1901 | else | |
1902 | sock_release(svsk->sk_sock); | |
1903 | kfree(svsk); | |
1904 | } | |
1905 | ||
1da177e4 | 1906 | /* |
7a182083 | 1907 | * Remove a dead transport |
1da177e4 | 1908 | */ |
7a182083 | 1909 | static void svc_delete_xprt(struct svc_xprt *xprt) |
1da177e4 | 1910 | { |
7a182083 | 1911 | struct svc_serv *serv = xprt->xpt_server; |
1da177e4 | 1912 | |
7a182083 TT |
1913 | dprintk("svc: svc_delete_xprt(%p)\n", xprt); |
1914 | xprt->xpt_ops->xpo_detach(xprt); | |
1da177e4 LT |
1915 | |
1916 | spin_lock_bh(&serv->sv_lock); | |
7a182083 TT |
1917 | if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags)) |
1918 | list_del_init(&xprt->xpt_list); | |
cca5172a | 1919 | /* |
7a182083 TT |
1920 | * We used to delete the transport from whichever list |
1921 | * it's sk_xprt.xpt_ready node was on, but we don't actually | |
3262c816 GB |
1922 | * need to. This is because the only time we're called |
1923 | * while still attached to a queue, the queue itself | |
1924 | * is about to be destroyed (in svc_destroy). | |
1925 | */ | |
7a182083 TT |
1926 | if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) { |
1927 | BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2); | |
1928 | if (test_bit(XPT_TEMP, &xprt->xpt_flags)) | |
1da177e4 | 1929 | serv->sv_tmpcnt--; |
7a182083 | 1930 | svc_xprt_put(xprt); |
aaf68cfb | 1931 | } |
d6740df9 | 1932 | spin_unlock_bh(&serv->sv_lock); |
aaf68cfb N |
1933 | } |
1934 | ||
7a182083 | 1935 | static void svc_close_xprt(struct svc_xprt *xprt) |
aaf68cfb | 1936 | { |
7a182083 TT |
1937 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
1938 | if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) | |
aaf68cfb N |
1939 | /* someone else will have to effect the close */ |
1940 | return; | |
1941 | ||
7a182083 TT |
1942 | svc_xprt_get(xprt); |
1943 | svc_delete_xprt(xprt); | |
1944 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
1945 | svc_xprt_put(xprt); | |
1da177e4 LT |
1946 | } |
1947 | ||
7a182083 | 1948 | void svc_close_all(struct list_head *xprt_list) |
cda1fd4a | 1949 | { |
7a182083 TT |
1950 | struct svc_xprt *xprt; |
1951 | struct svc_xprt *tmp; | |
1952 | ||
1953 | list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) { | |
1954 | set_bit(XPT_CLOSE, &xprt->xpt_flags); | |
1955 | if (test_bit(XPT_BUSY, &xprt->xpt_flags)) { | |
1956 | /* Waiting to be processed, but no threads left, | |
1957 | * So just remove it from the waiting list | |
1958 | */ | |
1959 | list_del_init(&xprt->xpt_ready); | |
1960 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
1961 | } | |
1962 | svc_close_xprt(xprt); | |
cda1fd4a | 1963 | } |
cda1fd4a N |
1964 | } |
1965 | ||
1da177e4 | 1966 | /* |
cca5172a | 1967 | * Handle defer and revisit of requests |
1da177e4 LT |
1968 | */ |
1969 | ||
1970 | static void svc_revisit(struct cache_deferred_req *dreq, int too_many) | |
1971 | { | |
1972 | struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle); | |
8c7b0172 | 1973 | struct svc_xprt *xprt = dr->xprt; |
1da177e4 LT |
1974 | |
1975 | if (too_many) { | |
8c7b0172 | 1976 | svc_xprt_put(xprt); |
1da177e4 LT |
1977 | kfree(dr); |
1978 | return; | |
1979 | } | |
1980 | dprintk("revisit queued\n"); | |
8c7b0172 TT |
1981 | dr->xprt = NULL; |
1982 | spin_lock(&xprt->xpt_lock); | |
1983 | list_add(&dr->handle.recent, &xprt->xpt_deferred); | |
1984 | spin_unlock(&xprt->xpt_lock); | |
1985 | set_bit(XPT_DEFERRED, &xprt->xpt_flags); | |
1986 | svc_xprt_enqueue(xprt); | |
1987 | svc_xprt_put(xprt); | |
1da177e4 LT |
1988 | } |
1989 | ||
1990 | static struct cache_deferred_req * | |
1991 | svc_defer(struct cache_req *req) | |
1992 | { | |
1993 | struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle); | |
1994 | int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len); | |
1995 | struct svc_deferred_req *dr; | |
1996 | ||
1997 | if (rqstp->rq_arg.page_len) | |
1998 | return NULL; /* if more than a page, give up FIXME */ | |
1999 | if (rqstp->rq_deferred) { | |
2000 | dr = rqstp->rq_deferred; | |
2001 | rqstp->rq_deferred = NULL; | |
2002 | } else { | |
2003 | int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; | |
2004 | /* FIXME maybe discard if size too large */ | |
2005 | dr = kmalloc(size, GFP_KERNEL); | |
2006 | if (dr == NULL) | |
2007 | return NULL; | |
2008 | ||
2009 | dr->handle.owner = rqstp->rq_server; | |
2010 | dr->prot = rqstp->rq_prot; | |
24422222 CL |
2011 | memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen); |
2012 | dr->addrlen = rqstp->rq_addrlen; | |
1918e341 | 2013 | dr->daddr = rqstp->rq_daddr; |
1da177e4 LT |
2014 | dr->argslen = rqstp->rq_arg.len >> 2; |
2015 | memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2); | |
2016 | } | |
e1b3157f | 2017 | svc_xprt_get(rqstp->rq_xprt); |
8c7b0172 | 2018 | dr->xprt = rqstp->rq_xprt; |
1da177e4 LT |
2019 | |
2020 | dr->handle.revisit = svc_revisit; | |
2021 | return &dr->handle; | |
2022 | } | |
2023 | ||
2024 | /* | |
2025 | * recv data from a deferred request into an active one | |
2026 | */ | |
2027 | static int svc_deferred_recv(struct svc_rqst *rqstp) | |
2028 | { | |
2029 | struct svc_deferred_req *dr = rqstp->rq_deferred; | |
2030 | ||
2031 | rqstp->rq_arg.head[0].iov_base = dr->args; | |
2032 | rqstp->rq_arg.head[0].iov_len = dr->argslen<<2; | |
2033 | rqstp->rq_arg.page_len = 0; | |
2034 | rqstp->rq_arg.len = dr->argslen<<2; | |
2035 | rqstp->rq_prot = dr->prot; | |
24422222 CL |
2036 | memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen); |
2037 | rqstp->rq_addrlen = dr->addrlen; | |
1918e341 | 2038 | rqstp->rq_daddr = dr->daddr; |
44524359 | 2039 | rqstp->rq_respages = rqstp->rq_pages; |
1da177e4 LT |
2040 | return dr->argslen<<2; |
2041 | } | |
2042 | ||
2043 | ||
8c7b0172 | 2044 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt) |
1da177e4 LT |
2045 | { |
2046 | struct svc_deferred_req *dr = NULL; | |
cca5172a | 2047 | |
8c7b0172 | 2048 | if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags)) |
1da177e4 | 2049 | return NULL; |
8c7b0172 TT |
2050 | spin_lock(&xprt->xpt_lock); |
2051 | clear_bit(XPT_DEFERRED, &xprt->xpt_flags); | |
2052 | if (!list_empty(&xprt->xpt_deferred)) { | |
2053 | dr = list_entry(xprt->xpt_deferred.next, | |
1da177e4 LT |
2054 | struct svc_deferred_req, |
2055 | handle.recent); | |
2056 | list_del_init(&dr->handle.recent); | |
8c7b0172 | 2057 | set_bit(XPT_DEFERRED, &xprt->xpt_flags); |
1da177e4 | 2058 | } |
8c7b0172 | 2059 | spin_unlock(&xprt->xpt_lock); |
1da177e4 LT |
2060 | return dr; |
2061 | } |