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