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