1a246b010SChuck Lever /* 2a246b010SChuck Lever * linux/net/sunrpc/xprtsock.c 3a246b010SChuck Lever * 4a246b010SChuck Lever * Client-side transport implementation for sockets. 5a246b010SChuck Lever * 6a246b010SChuck Lever * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com> 7a246b010SChuck Lever * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com> 8a246b010SChuck Lever * TCP NFS related read + write fixes 9a246b010SChuck Lever * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 10a246b010SChuck Lever * 11a246b010SChuck Lever * Rewrite of larges part of the code in order to stabilize TCP stuff. 12a246b010SChuck Lever * Fix behaviour when socket buffer is full. 13a246b010SChuck Lever * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> 1455aa4f58SChuck Lever * 1555aa4f58SChuck Lever * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> 16a246b010SChuck Lever */ 17a246b010SChuck Lever 18a246b010SChuck Lever #include <linux/types.h> 19a246b010SChuck Lever #include <linux/slab.h> 20a246b010SChuck Lever #include <linux/capability.h> 21a246b010SChuck Lever #include <linux/sched.h> 22a246b010SChuck Lever #include <linux/pagemap.h> 23a246b010SChuck Lever #include <linux/errno.h> 24a246b010SChuck Lever #include <linux/socket.h> 25a246b010SChuck Lever #include <linux/in.h> 26a246b010SChuck Lever #include <linux/net.h> 27a246b010SChuck Lever #include <linux/mm.h> 28a246b010SChuck Lever #include <linux/udp.h> 29a246b010SChuck Lever #include <linux/tcp.h> 30a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 31a246b010SChuck Lever #include <linux/file.h> 32a246b010SChuck Lever 33a246b010SChuck Lever #include <net/sock.h> 34a246b010SChuck Lever #include <net/checksum.h> 35a246b010SChuck Lever #include <net/udp.h> 36a246b010SChuck Lever #include <net/tcp.h> 37a246b010SChuck Lever 389903cd1cSChuck Lever /* 399903cd1cSChuck Lever * Maximum port number to use when requesting a reserved port. 409903cd1cSChuck Lever */ 419903cd1cSChuck Lever #define XS_MAX_RESVPORT (800U) 429903cd1cSChuck Lever 43*262965f5SChuck Lever /* 44*262965f5SChuck Lever * How many times to try sending a request on a socket before waiting 45*262965f5SChuck Lever * for the socket buffer to clear. 46*262965f5SChuck Lever */ 47*262965f5SChuck Lever #define XS_SENDMSG_RETRY (10U) 48*262965f5SChuck Lever 49a246b010SChuck Lever #ifdef RPC_DEBUG 50a246b010SChuck Lever # undef RPC_DEBUG_DATA 519903cd1cSChuck Lever # define RPCDBG_FACILITY RPCDBG_TRANS 52a246b010SChuck Lever #endif 53a246b010SChuck Lever 54a246b010SChuck Lever #ifdef RPC_DEBUG_DATA 559903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count) 56a246b010SChuck Lever { 57a246b010SChuck Lever u8 *buf = (u8 *) packet; 58a246b010SChuck Lever int j; 59a246b010SChuck Lever 60a246b010SChuck Lever dprintk("RPC: %s\n", msg); 61a246b010SChuck Lever for (j = 0; j < count && j < 128; j += 4) { 62a246b010SChuck Lever if (!(j & 31)) { 63a246b010SChuck Lever if (j) 64a246b010SChuck Lever dprintk("\n"); 65a246b010SChuck Lever dprintk("0x%04x ", j); 66a246b010SChuck Lever } 67a246b010SChuck Lever dprintk("%02x%02x%02x%02x ", 68a246b010SChuck Lever buf[j], buf[j+1], buf[j+2], buf[j+3]); 69a246b010SChuck Lever } 70a246b010SChuck Lever dprintk("\n"); 71a246b010SChuck Lever } 72a246b010SChuck Lever #else 739903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) 74a246b010SChuck Lever { 75a246b010SChuck Lever /* NOP */ 76a246b010SChuck Lever } 77a246b010SChuck Lever #endif 78a246b010SChuck Lever 79b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) 80b4b5cc85SChuck Lever 81b4b5cc85SChuck Lever static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len) 82b4b5cc85SChuck Lever { 83b4b5cc85SChuck Lever struct kvec iov = { 84b4b5cc85SChuck Lever .iov_base = xdr->head[0].iov_base + base, 85b4b5cc85SChuck Lever .iov_len = len - base, 86b4b5cc85SChuck Lever }; 87b4b5cc85SChuck Lever struct msghdr msg = { 88b4b5cc85SChuck Lever .msg_name = addr, 89b4b5cc85SChuck Lever .msg_namelen = addrlen, 90b4b5cc85SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 91b4b5cc85SChuck Lever }; 92b4b5cc85SChuck Lever 93b4b5cc85SChuck Lever if (xdr->len > len) 94b4b5cc85SChuck Lever msg.msg_flags |= MSG_MORE; 95b4b5cc85SChuck Lever 96b4b5cc85SChuck Lever if (likely(iov.iov_len)) 97b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 98b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, NULL, 0, 0); 99b4b5cc85SChuck Lever } 100b4b5cc85SChuck Lever 101b4b5cc85SChuck Lever static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len) 102b4b5cc85SChuck Lever { 103b4b5cc85SChuck Lever struct kvec iov = { 104b4b5cc85SChuck Lever .iov_base = xdr->tail[0].iov_base + base, 105b4b5cc85SChuck Lever .iov_len = len - base, 106b4b5cc85SChuck Lever }; 107b4b5cc85SChuck Lever struct msghdr msg = { 108b4b5cc85SChuck Lever .msg_flags = XS_SENDMSG_FLAGS, 109b4b5cc85SChuck Lever }; 110b4b5cc85SChuck Lever 111b4b5cc85SChuck Lever return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len); 112b4b5cc85SChuck Lever } 113b4b5cc85SChuck Lever 1149903cd1cSChuck Lever /** 1159903cd1cSChuck Lever * xs_sendpages - write pages directly to a socket 1169903cd1cSChuck Lever * @sock: socket to send on 1179903cd1cSChuck Lever * @addr: UDP only -- address of destination 1189903cd1cSChuck Lever * @addrlen: UDP only -- length of destination address 1199903cd1cSChuck Lever * @xdr: buffer containing this request 1209903cd1cSChuck Lever * @base: starting position in the buffer 1219903cd1cSChuck Lever * 122a246b010SChuck Lever */ 123*262965f5SChuck Lever static inline int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 124a246b010SChuck Lever { 125a246b010SChuck Lever struct page **ppage = xdr->pages; 126a246b010SChuck Lever unsigned int len, pglen = xdr->page_len; 127a246b010SChuck Lever int err, ret = 0; 128a246b010SChuck Lever ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int); 129a246b010SChuck Lever 130*262965f5SChuck Lever if (unlikely(!sock)) 131*262965f5SChuck Lever return -ENOTCONN; 132*262965f5SChuck Lever 133*262965f5SChuck Lever clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags); 134*262965f5SChuck Lever 135a246b010SChuck Lever len = xdr->head[0].iov_len; 136a246b010SChuck Lever if (base < len || (addr != NULL && base == 0)) { 137b4b5cc85SChuck Lever err = xs_send_head(sock, addr, addrlen, xdr, base, len); 138a246b010SChuck Lever if (ret == 0) 139a246b010SChuck Lever ret = err; 140a246b010SChuck Lever else if (err > 0) 141a246b010SChuck Lever ret += err; 142b4b5cc85SChuck Lever if (err != (len - base)) 143a246b010SChuck Lever goto out; 144a246b010SChuck Lever base = 0; 145a246b010SChuck Lever } else 146a246b010SChuck Lever base -= len; 147a246b010SChuck Lever 148b4b5cc85SChuck Lever if (unlikely(pglen == 0)) 149a246b010SChuck Lever goto copy_tail; 150b4b5cc85SChuck Lever if (unlikely(base >= pglen)) { 151a246b010SChuck Lever base -= pglen; 152a246b010SChuck Lever goto copy_tail; 153a246b010SChuck Lever } 154a246b010SChuck Lever if (base || xdr->page_base) { 155a246b010SChuck Lever pglen -= base; 156a246b010SChuck Lever base += xdr->page_base; 157a246b010SChuck Lever ppage += base >> PAGE_CACHE_SHIFT; 158a246b010SChuck Lever base &= ~PAGE_CACHE_MASK; 159a246b010SChuck Lever } 160a246b010SChuck Lever 161a246b010SChuck Lever sendpage = sock->ops->sendpage ? : sock_no_sendpage; 162a246b010SChuck Lever do { 163b4b5cc85SChuck Lever int flags = XS_SENDMSG_FLAGS; 164a246b010SChuck Lever 165a246b010SChuck Lever len = PAGE_CACHE_SIZE; 166a246b010SChuck Lever if (base) 167a246b010SChuck Lever len -= base; 168a246b010SChuck Lever if (pglen < len) 169a246b010SChuck Lever len = pglen; 170a246b010SChuck Lever 171a246b010SChuck Lever if (pglen != len || xdr->tail[0].iov_len != 0) 172a246b010SChuck Lever flags |= MSG_MORE; 173a246b010SChuck Lever 174a246b010SChuck Lever /* Hmm... We might be dealing with highmem pages */ 175a246b010SChuck Lever if (PageHighMem(*ppage)) 176a246b010SChuck Lever sendpage = sock_no_sendpage; 177a246b010SChuck Lever err = sendpage(sock, *ppage, base, len, flags); 178a246b010SChuck Lever if (ret == 0) 179a246b010SChuck Lever ret = err; 180a246b010SChuck Lever else if (err > 0) 181a246b010SChuck Lever ret += err; 182a246b010SChuck Lever if (err != len) 183a246b010SChuck Lever goto out; 184a246b010SChuck Lever base = 0; 185a246b010SChuck Lever ppage++; 186a246b010SChuck Lever } while ((pglen -= len) != 0); 187a246b010SChuck Lever copy_tail: 188a246b010SChuck Lever len = xdr->tail[0].iov_len; 189a246b010SChuck Lever if (base < len) { 190b4b5cc85SChuck Lever err = xs_send_tail(sock, xdr, base, len); 191a246b010SChuck Lever if (ret == 0) 192a246b010SChuck Lever ret = err; 193a246b010SChuck Lever else if (err > 0) 194a246b010SChuck Lever ret += err; 195a246b010SChuck Lever } 196a246b010SChuck Lever out: 197a246b010SChuck Lever return ret; 198a246b010SChuck Lever } 199a246b010SChuck Lever 2009903cd1cSChuck Lever /** 201*262965f5SChuck Lever * xs_nospace - place task on wait queue if transmit was incomplete 202*262965f5SChuck Lever * @task: task to put to sleep 2039903cd1cSChuck Lever * 204a246b010SChuck Lever */ 205*262965f5SChuck Lever static void xs_nospace(struct rpc_task *task) 206a246b010SChuck Lever { 207*262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 208*262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 209a246b010SChuck Lever 210*262965f5SChuck Lever dprintk("RPC: %4d xmit incomplete (%u left of %u)\n", 211*262965f5SChuck Lever task->tk_pid, req->rq_slen - req->rq_bytes_sent, 212*262965f5SChuck Lever req->rq_slen); 213a246b010SChuck Lever 214*262965f5SChuck Lever if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) { 215*262965f5SChuck Lever /* Protect against races with write_space */ 216*262965f5SChuck Lever spin_lock_bh(&xprt->transport_lock); 217a246b010SChuck Lever 218*262965f5SChuck Lever /* Don't race with disconnect */ 219*262965f5SChuck Lever if (!xprt_connected(xprt)) 220*262965f5SChuck Lever task->tk_status = -ENOTCONN; 221*262965f5SChuck Lever else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags)) 222*262965f5SChuck Lever xprt_wait_for_buffer_space(task); 223a246b010SChuck Lever 224*262965f5SChuck Lever spin_unlock_bh(&xprt->transport_lock); 225*262965f5SChuck Lever } else 226*262965f5SChuck Lever /* Keep holding the socket if it is blocked */ 227*262965f5SChuck Lever rpc_delay(task, HZ>>4); 228a246b010SChuck Lever } 229a246b010SChuck Lever 2309903cd1cSChuck Lever /** 231*262965f5SChuck Lever * xs_udp_send_request - write an RPC request to a UDP socket 2329903cd1cSChuck Lever * @task: address of RPC task that manages the state of an RPC request 2339903cd1cSChuck Lever * 2349903cd1cSChuck Lever * Return values: 2359903cd1cSChuck Lever * 0: The request has been sent 2369903cd1cSChuck Lever * EAGAIN: The socket was blocked, please call again later to 2379903cd1cSChuck Lever * complete the request 238*262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 2399903cd1cSChuck Lever * other: Some other error occured, the request was not sent 2409903cd1cSChuck Lever */ 241*262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task) 242a246b010SChuck Lever { 243a246b010SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 244a246b010SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 245*262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 246*262965f5SChuck Lever int status; 247*262965f5SChuck Lever 248*262965f5SChuck Lever xs_pktdump("packet data:", 249*262965f5SChuck Lever req->rq_svec->iov_base, 250*262965f5SChuck Lever req->rq_svec->iov_len); 251*262965f5SChuck Lever 252*262965f5SChuck Lever req->rq_xtime = jiffies; 253*262965f5SChuck Lever status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr, 254*262965f5SChuck Lever sizeof(xprt->addr), xdr, req->rq_bytes_sent); 255*262965f5SChuck Lever 256*262965f5SChuck Lever dprintk("RPC: xs_udp_send_request(%u) = %d\n", 257*262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 258*262965f5SChuck Lever 259*262965f5SChuck Lever if (likely(status >= (int) req->rq_slen)) 260*262965f5SChuck Lever return 0; 261*262965f5SChuck Lever 262*262965f5SChuck Lever /* Still some bytes left; set up for a retry later. */ 263*262965f5SChuck Lever if (status > 0) 264*262965f5SChuck Lever status = -EAGAIN; 265*262965f5SChuck Lever 266*262965f5SChuck Lever switch (status) { 267*262965f5SChuck Lever case -ENETUNREACH: 268*262965f5SChuck Lever case -EPIPE: 269*262965f5SChuck Lever case -ECONNREFUSED: 270*262965f5SChuck Lever /* When the server has died, an ICMP port unreachable message 271*262965f5SChuck Lever * prompts ECONNREFUSED. */ 272*262965f5SChuck Lever break; 273*262965f5SChuck Lever case -EAGAIN: 274*262965f5SChuck Lever xs_nospace(task); 275*262965f5SChuck Lever break; 276*262965f5SChuck Lever default: 277*262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 278*262965f5SChuck Lever -status); 279*262965f5SChuck Lever break; 280*262965f5SChuck Lever } 281*262965f5SChuck Lever 282*262965f5SChuck Lever return status; 283*262965f5SChuck Lever } 284*262965f5SChuck Lever 285*262965f5SChuck Lever /** 286*262965f5SChuck Lever * xs_tcp_send_request - write an RPC request to a TCP socket 287*262965f5SChuck Lever * @task: address of RPC task that manages the state of an RPC request 288*262965f5SChuck Lever * 289*262965f5SChuck Lever * Return values: 290*262965f5SChuck Lever * 0: The request has been sent 291*262965f5SChuck Lever * EAGAIN: The socket was blocked, please call again later to 292*262965f5SChuck Lever * complete the request 293*262965f5SChuck Lever * ENOTCONN: Caller needs to invoke connect logic then call again 294*262965f5SChuck Lever * other: Some other error occured, the request was not sent 295*262965f5SChuck Lever * 296*262965f5SChuck Lever * XXX: In the case of soft timeouts, should we eventually give up 297*262965f5SChuck Lever * if sendmsg is not able to make progress? 298*262965f5SChuck Lever */ 299*262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task) 300*262965f5SChuck Lever { 301*262965f5SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 302*262965f5SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 303*262965f5SChuck Lever struct xdr_buf *xdr = &req->rq_snd_buf; 304*262965f5SChuck Lever u32 *marker = req->rq_svec[0].iov_base; 305a246b010SChuck Lever int status, retry = 0; 306a246b010SChuck Lever 307a246b010SChuck Lever /* Write the record marker */ 308a246b010SChuck Lever *marker = htonl(0x80000000|(req->rq_slen-sizeof(*marker))); 309*262965f5SChuck Lever 310*262965f5SChuck Lever xs_pktdump("packet data:", 311*262965f5SChuck Lever req->rq_svec->iov_base, 312*262965f5SChuck Lever req->rq_svec->iov_len); 313a246b010SChuck Lever 314a246b010SChuck Lever /* Continue transmitting the packet/record. We must be careful 315a246b010SChuck Lever * to cope with writespace callbacks arriving _after_ we have 316*262965f5SChuck Lever * called sendmsg(). */ 317a246b010SChuck Lever while (1) { 318a246b010SChuck Lever req->rq_xtime = jiffies; 319*262965f5SChuck Lever status = xs_sendpages(xprt->sock, NULL, 0, xdr, 320*262965f5SChuck Lever req->rq_bytes_sent); 321a246b010SChuck Lever 322*262965f5SChuck Lever dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 323*262965f5SChuck Lever xdr->len - req->rq_bytes_sent, status); 324*262965f5SChuck Lever 325*262965f5SChuck Lever if (unlikely(status < 0)) 326a246b010SChuck Lever break; 327a246b010SChuck Lever 328a246b010SChuck Lever /* If we've sent the entire packet, immediately 329a246b010SChuck Lever * reset the count of bytes sent. */ 330*262965f5SChuck Lever req->rq_bytes_sent += status; 331*262965f5SChuck Lever if (likely(req->rq_bytes_sent >= req->rq_slen)) { 332a246b010SChuck Lever req->rq_bytes_sent = 0; 333a246b010SChuck Lever return 0; 334a246b010SChuck Lever } 335*262965f5SChuck Lever 336a246b010SChuck Lever status = -EAGAIN; 337*262965f5SChuck Lever if (retry++ > XS_SENDMSG_RETRY) 338a246b010SChuck Lever break; 339a246b010SChuck Lever } 340a246b010SChuck Lever 341*262965f5SChuck Lever switch (status) { 342*262965f5SChuck Lever case -EAGAIN: 343*262965f5SChuck Lever xs_nospace(task); 344*262965f5SChuck Lever break; 345*262965f5SChuck Lever case -ECONNREFUSED: 346*262965f5SChuck Lever case -ECONNRESET: 347*262965f5SChuck Lever case -ENOTCONN: 348*262965f5SChuck Lever case -EPIPE: 349*262965f5SChuck Lever status = -ENOTCONN; 350*262965f5SChuck Lever break; 351*262965f5SChuck Lever default: 352*262965f5SChuck Lever dprintk("RPC: sendmsg returned unrecognized error %d\n", 353*262965f5SChuck Lever -status); 354a246b010SChuck Lever break; 355a246b010SChuck Lever } 356a246b010SChuck Lever 357a246b010SChuck Lever return status; 358a246b010SChuck Lever } 359a246b010SChuck Lever 3609903cd1cSChuck Lever /** 3619903cd1cSChuck Lever * xs_close - close a socket 3629903cd1cSChuck Lever * @xprt: transport 3639903cd1cSChuck Lever * 364a246b010SChuck Lever */ 3659903cd1cSChuck Lever static void xs_close(struct rpc_xprt *xprt) 366a246b010SChuck Lever { 367a246b010SChuck Lever struct socket *sock = xprt->sock; 368a246b010SChuck Lever struct sock *sk = xprt->inet; 369a246b010SChuck Lever 370a246b010SChuck Lever if (!sk) 371a246b010SChuck Lever return; 372a246b010SChuck Lever 3739903cd1cSChuck Lever dprintk("RPC: xs_close xprt %p\n", xprt); 3749903cd1cSChuck Lever 375a246b010SChuck Lever write_lock_bh(&sk->sk_callback_lock); 376a246b010SChuck Lever xprt->inet = NULL; 377a246b010SChuck Lever xprt->sock = NULL; 378a246b010SChuck Lever 379a246b010SChuck Lever sk->sk_user_data = NULL; 380a246b010SChuck Lever sk->sk_data_ready = xprt->old_data_ready; 381a246b010SChuck Lever sk->sk_state_change = xprt->old_state_change; 382a246b010SChuck Lever sk->sk_write_space = xprt->old_write_space; 383a246b010SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 384a246b010SChuck Lever 385a246b010SChuck Lever sk->sk_no_check = 0; 386a246b010SChuck Lever 387a246b010SChuck Lever sock_release(sock); 388a246b010SChuck Lever } 389a246b010SChuck Lever 3909903cd1cSChuck Lever /** 3919903cd1cSChuck Lever * xs_destroy - prepare to shutdown a transport 3929903cd1cSChuck Lever * @xprt: doomed transport 3939903cd1cSChuck Lever * 3949903cd1cSChuck Lever */ 3959903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt) 396a246b010SChuck Lever { 3979903cd1cSChuck Lever dprintk("RPC: xs_destroy xprt %p\n", xprt); 3989903cd1cSChuck Lever 39955aa4f58SChuck Lever cancel_delayed_work(&xprt->connect_worker); 400a246b010SChuck Lever flush_scheduled_work(); 401a246b010SChuck Lever 402a246b010SChuck Lever xprt_disconnect(xprt); 4039903cd1cSChuck Lever xs_close(xprt); 404a246b010SChuck Lever kfree(xprt->slot); 405a246b010SChuck Lever } 406a246b010SChuck Lever 4079903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 4089903cd1cSChuck Lever { 4099903cd1cSChuck Lever return (struct rpc_xprt *) sk->sk_user_data; 4109903cd1cSChuck Lever } 4119903cd1cSChuck Lever 4129903cd1cSChuck Lever /** 4139903cd1cSChuck Lever * xs_udp_data_ready - "data ready" callback for UDP sockets 4149903cd1cSChuck Lever * @sk: socket with data to read 4159903cd1cSChuck Lever * @len: how much data to read 4169903cd1cSChuck Lever * 417a246b010SChuck Lever */ 4189903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len) 419a246b010SChuck Lever { 420a246b010SChuck Lever struct rpc_task *task; 421a246b010SChuck Lever struct rpc_xprt *xprt; 422a246b010SChuck Lever struct rpc_rqst *rovr; 423a246b010SChuck Lever struct sk_buff *skb; 424a246b010SChuck Lever int err, repsize, copied; 425a246b010SChuck Lever u32 _xid, *xp; 426a246b010SChuck Lever 427a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 4289903cd1cSChuck Lever dprintk("RPC: xs_udp_data_ready...\n"); 4299903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 430a246b010SChuck Lever goto out; 431a246b010SChuck Lever 432a246b010SChuck Lever if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL) 433a246b010SChuck Lever goto out; 434a246b010SChuck Lever 435a246b010SChuck Lever if (xprt->shutdown) 436a246b010SChuck Lever goto dropit; 437a246b010SChuck Lever 438a246b010SChuck Lever repsize = skb->len - sizeof(struct udphdr); 439a246b010SChuck Lever if (repsize < 4) { 4409903cd1cSChuck Lever dprintk("RPC: impossible RPC reply size %d!\n", repsize); 441a246b010SChuck Lever goto dropit; 442a246b010SChuck Lever } 443a246b010SChuck Lever 444a246b010SChuck Lever /* Copy the XID from the skb... */ 445a246b010SChuck Lever xp = skb_header_pointer(skb, sizeof(struct udphdr), 446a246b010SChuck Lever sizeof(_xid), &_xid); 447a246b010SChuck Lever if (xp == NULL) 448a246b010SChuck Lever goto dropit; 449a246b010SChuck Lever 450a246b010SChuck Lever /* Look up and lock the request corresponding to the given XID */ 4514a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 452a246b010SChuck Lever rovr = xprt_lookup_rqst(xprt, *xp); 453a246b010SChuck Lever if (!rovr) 454a246b010SChuck Lever goto out_unlock; 455a246b010SChuck Lever task = rovr->rq_task; 456a246b010SChuck Lever 457a246b010SChuck Lever dprintk("RPC: %4d received reply\n", task->tk_pid); 458a246b010SChuck Lever 459a246b010SChuck Lever if ((copied = rovr->rq_private_buf.buflen) > repsize) 460a246b010SChuck Lever copied = repsize; 461a246b010SChuck Lever 462a246b010SChuck Lever /* Suck it into the iovec, verify checksum if not done by hw. */ 463a246b010SChuck Lever if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) 464a246b010SChuck Lever goto out_unlock; 465a246b010SChuck Lever 466a246b010SChuck Lever /* Something worked... */ 467a246b010SChuck Lever dst_confirm(skb->dst); 468a246b010SChuck Lever 469a246b010SChuck Lever xprt_complete_rqst(xprt, rovr, copied); 470a246b010SChuck Lever 471a246b010SChuck Lever out_unlock: 4724a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 473a246b010SChuck Lever dropit: 474a246b010SChuck Lever skb_free_datagram(sk, skb); 475a246b010SChuck Lever out: 476a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 477a246b010SChuck Lever } 478a246b010SChuck Lever 4799903cd1cSChuck Lever static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len) 480a246b010SChuck Lever { 481a246b010SChuck Lever if (len > desc->count) 482a246b010SChuck Lever len = desc->count; 483a246b010SChuck Lever if (skb_copy_bits(desc->skb, desc->offset, p, len)) { 484a246b010SChuck Lever dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n", 485a246b010SChuck Lever len, desc->count); 486a246b010SChuck Lever return 0; 487a246b010SChuck Lever } 488a246b010SChuck Lever desc->offset += len; 489a246b010SChuck Lever desc->count -= len; 490a246b010SChuck Lever dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n", 491a246b010SChuck Lever len, desc->count); 492a246b010SChuck Lever return len; 493a246b010SChuck Lever } 494a246b010SChuck Lever 4959903cd1cSChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc) 496a246b010SChuck Lever { 497a246b010SChuck Lever size_t len, used; 498a246b010SChuck Lever char *p; 499a246b010SChuck Lever 500a246b010SChuck Lever p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset; 501a246b010SChuck Lever len = sizeof(xprt->tcp_recm) - xprt->tcp_offset; 5029903cd1cSChuck Lever used = xs_tcp_copy_data(desc, p, len); 503a246b010SChuck Lever xprt->tcp_offset += used; 504a246b010SChuck Lever if (used != len) 505a246b010SChuck Lever return; 506a246b010SChuck Lever xprt->tcp_reclen = ntohl(xprt->tcp_recm); 507a246b010SChuck Lever if (xprt->tcp_reclen & 0x80000000) 508a246b010SChuck Lever xprt->tcp_flags |= XPRT_LAST_FRAG; 509a246b010SChuck Lever else 510a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_LAST_FRAG; 511a246b010SChuck Lever xprt->tcp_reclen &= 0x7fffffff; 512a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_COPY_RECM; 513a246b010SChuck Lever xprt->tcp_offset = 0; 514a246b010SChuck Lever /* Sanity check of the record length */ 515a246b010SChuck Lever if (xprt->tcp_reclen < 4) { 5169903cd1cSChuck Lever dprintk("RPC: invalid TCP record fragment length\n"); 517a246b010SChuck Lever xprt_disconnect(xprt); 5189903cd1cSChuck Lever return; 519a246b010SChuck Lever } 520a246b010SChuck Lever dprintk("RPC: reading TCP record fragment of length %d\n", 521a246b010SChuck Lever xprt->tcp_reclen); 522a246b010SChuck Lever } 523a246b010SChuck Lever 5249903cd1cSChuck Lever static void xs_tcp_check_recm(struct rpc_xprt *xprt) 525a246b010SChuck Lever { 526a246b010SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n", 527a246b010SChuck Lever xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags); 528a246b010SChuck Lever if (xprt->tcp_offset == xprt->tcp_reclen) { 529a246b010SChuck Lever xprt->tcp_flags |= XPRT_COPY_RECM; 530a246b010SChuck Lever xprt->tcp_offset = 0; 531a246b010SChuck Lever if (xprt->tcp_flags & XPRT_LAST_FRAG) { 532a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_COPY_DATA; 533a246b010SChuck Lever xprt->tcp_flags |= XPRT_COPY_XID; 534a246b010SChuck Lever xprt->tcp_copied = 0; 535a246b010SChuck Lever } 536a246b010SChuck Lever } 537a246b010SChuck Lever } 538a246b010SChuck Lever 5399903cd1cSChuck Lever static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc) 540a246b010SChuck Lever { 541a246b010SChuck Lever size_t len, used; 542a246b010SChuck Lever char *p; 543a246b010SChuck Lever 544a246b010SChuck Lever len = sizeof(xprt->tcp_xid) - xprt->tcp_offset; 545a246b010SChuck Lever dprintk("RPC: reading XID (%Zu bytes)\n", len); 546a246b010SChuck Lever p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset; 5479903cd1cSChuck Lever used = xs_tcp_copy_data(desc, p, len); 548a246b010SChuck Lever xprt->tcp_offset += used; 549a246b010SChuck Lever if (used != len) 550a246b010SChuck Lever return; 551a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_COPY_XID; 552a246b010SChuck Lever xprt->tcp_flags |= XPRT_COPY_DATA; 553a246b010SChuck Lever xprt->tcp_copied = 4; 554a246b010SChuck Lever dprintk("RPC: reading reply for XID %08x\n", 555a246b010SChuck Lever ntohl(xprt->tcp_xid)); 5569903cd1cSChuck Lever xs_tcp_check_recm(xprt); 557a246b010SChuck Lever } 558a246b010SChuck Lever 5599903cd1cSChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc) 560a246b010SChuck Lever { 561a246b010SChuck Lever struct rpc_rqst *req; 562a246b010SChuck Lever struct xdr_buf *rcvbuf; 563a246b010SChuck Lever size_t len; 564a246b010SChuck Lever ssize_t r; 565a246b010SChuck Lever 566a246b010SChuck Lever /* Find and lock the request corresponding to this xid */ 5674a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 568a246b010SChuck Lever req = xprt_lookup_rqst(xprt, xprt->tcp_xid); 569a246b010SChuck Lever if (!req) { 570a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_COPY_DATA; 571a246b010SChuck Lever dprintk("RPC: XID %08x request not found!\n", 572a246b010SChuck Lever ntohl(xprt->tcp_xid)); 5734a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 574a246b010SChuck Lever return; 575a246b010SChuck Lever } 576a246b010SChuck Lever 577a246b010SChuck Lever rcvbuf = &req->rq_private_buf; 578a246b010SChuck Lever len = desc->count; 579a246b010SChuck Lever if (len > xprt->tcp_reclen - xprt->tcp_offset) { 580a246b010SChuck Lever skb_reader_t my_desc; 581a246b010SChuck Lever 582a246b010SChuck Lever len = xprt->tcp_reclen - xprt->tcp_offset; 583a246b010SChuck Lever memcpy(&my_desc, desc, sizeof(my_desc)); 584a246b010SChuck Lever my_desc.count = len; 585a246b010SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, 5869903cd1cSChuck Lever &my_desc, xs_tcp_copy_data); 587a246b010SChuck Lever desc->count -= r; 588a246b010SChuck Lever desc->offset += r; 589a246b010SChuck Lever } else 590a246b010SChuck Lever r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied, 5919903cd1cSChuck Lever desc, xs_tcp_copy_data); 592a246b010SChuck Lever 593a246b010SChuck Lever if (r > 0) { 594a246b010SChuck Lever xprt->tcp_copied += r; 595a246b010SChuck Lever xprt->tcp_offset += r; 596a246b010SChuck Lever } 597a246b010SChuck Lever if (r != len) { 598a246b010SChuck Lever /* Error when copying to the receive buffer, 599a246b010SChuck Lever * usually because we weren't able to allocate 600a246b010SChuck Lever * additional buffer pages. All we can do now 601a246b010SChuck Lever * is turn off XPRT_COPY_DATA, so the request 602a246b010SChuck Lever * will not receive any additional updates, 603a246b010SChuck Lever * and time out. 604a246b010SChuck Lever * Any remaining data from this record will 605a246b010SChuck Lever * be discarded. 606a246b010SChuck Lever */ 607a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_COPY_DATA; 608a246b010SChuck Lever dprintk("RPC: XID %08x truncated request\n", 609a246b010SChuck Lever ntohl(xprt->tcp_xid)); 610a246b010SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", 611a246b010SChuck Lever xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); 612a246b010SChuck Lever goto out; 613a246b010SChuck Lever } 614a246b010SChuck Lever 615a246b010SChuck Lever dprintk("RPC: XID %08x read %Zd bytes\n", 616a246b010SChuck Lever ntohl(xprt->tcp_xid), r); 617a246b010SChuck Lever dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n", 618a246b010SChuck Lever xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen); 619a246b010SChuck Lever 620a246b010SChuck Lever if (xprt->tcp_copied == req->rq_private_buf.buflen) 621a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_COPY_DATA; 622a246b010SChuck Lever else if (xprt->tcp_offset == xprt->tcp_reclen) { 623a246b010SChuck Lever if (xprt->tcp_flags & XPRT_LAST_FRAG) 624a246b010SChuck Lever xprt->tcp_flags &= ~XPRT_COPY_DATA; 625a246b010SChuck Lever } 626a246b010SChuck Lever 627a246b010SChuck Lever out: 628a246b010SChuck Lever if (!(xprt->tcp_flags & XPRT_COPY_DATA)) { 629a246b010SChuck Lever dprintk("RPC: %4d received reply complete\n", 630a246b010SChuck Lever req->rq_task->tk_pid); 631a246b010SChuck Lever xprt_complete_rqst(xprt, req, xprt->tcp_copied); 632a246b010SChuck Lever } 6334a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 6349903cd1cSChuck Lever xs_tcp_check_recm(xprt); 635a246b010SChuck Lever } 636a246b010SChuck Lever 6379903cd1cSChuck Lever static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc) 638a246b010SChuck Lever { 639a246b010SChuck Lever size_t len; 640a246b010SChuck Lever 641a246b010SChuck Lever len = xprt->tcp_reclen - xprt->tcp_offset; 642a246b010SChuck Lever if (len > desc->count) 643a246b010SChuck Lever len = desc->count; 644a246b010SChuck Lever desc->count -= len; 645a246b010SChuck Lever desc->offset += len; 646a246b010SChuck Lever xprt->tcp_offset += len; 647a246b010SChuck Lever dprintk("RPC: discarded %Zu bytes\n", len); 6489903cd1cSChuck Lever xs_tcp_check_recm(xprt); 649a246b010SChuck Lever } 650a246b010SChuck Lever 6519903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len) 652a246b010SChuck Lever { 653a246b010SChuck Lever struct rpc_xprt *xprt = rd_desc->arg.data; 654a246b010SChuck Lever skb_reader_t desc = { 655a246b010SChuck Lever .skb = skb, 656a246b010SChuck Lever .offset = offset, 657a246b010SChuck Lever .count = len, 658a246b010SChuck Lever .csum = 0 659a246b010SChuck Lever }; 660a246b010SChuck Lever 6619903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv started\n"); 662a246b010SChuck Lever do { 663a246b010SChuck Lever /* Read in a new fragment marker if necessary */ 664a246b010SChuck Lever /* Can we ever really expect to get completely empty fragments? */ 665a246b010SChuck Lever if (xprt->tcp_flags & XPRT_COPY_RECM) { 6669903cd1cSChuck Lever xs_tcp_read_fraghdr(xprt, &desc); 667a246b010SChuck Lever continue; 668a246b010SChuck Lever } 669a246b010SChuck Lever /* Read in the xid if necessary */ 670a246b010SChuck Lever if (xprt->tcp_flags & XPRT_COPY_XID) { 6719903cd1cSChuck Lever xs_tcp_read_xid(xprt, &desc); 672a246b010SChuck Lever continue; 673a246b010SChuck Lever } 674a246b010SChuck Lever /* Read in the request data */ 675a246b010SChuck Lever if (xprt->tcp_flags & XPRT_COPY_DATA) { 6769903cd1cSChuck Lever xs_tcp_read_request(xprt, &desc); 677a246b010SChuck Lever continue; 678a246b010SChuck Lever } 679a246b010SChuck Lever /* Skip over any trailing bytes on short reads */ 6809903cd1cSChuck Lever xs_tcp_read_discard(xprt, &desc); 681a246b010SChuck Lever } while (desc.count); 6829903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_recv done\n"); 683a246b010SChuck Lever return len - desc.count; 684a246b010SChuck Lever } 685a246b010SChuck Lever 6869903cd1cSChuck Lever /** 6879903cd1cSChuck Lever * xs_tcp_data_ready - "data ready" callback for TCP sockets 6889903cd1cSChuck Lever * @sk: socket with data to read 6899903cd1cSChuck Lever * @bytes: how much data to read 6909903cd1cSChuck Lever * 6919903cd1cSChuck Lever */ 6929903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes) 693a246b010SChuck Lever { 694a246b010SChuck Lever struct rpc_xprt *xprt; 695a246b010SChuck Lever read_descriptor_t rd_desc; 696a246b010SChuck Lever 697a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 6989903cd1cSChuck Lever dprintk("RPC: xs_tcp_data_ready...\n"); 6999903cd1cSChuck Lever if (!(xprt = xprt_from_sock(sk))) 700a246b010SChuck Lever goto out; 701a246b010SChuck Lever if (xprt->shutdown) 702a246b010SChuck Lever goto out; 703a246b010SChuck Lever 7049903cd1cSChuck Lever /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */ 705a246b010SChuck Lever rd_desc.arg.data = xprt; 706a246b010SChuck Lever rd_desc.count = 65536; 7079903cd1cSChuck Lever tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv); 708a246b010SChuck Lever out: 709a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 710a246b010SChuck Lever } 711a246b010SChuck Lever 7129903cd1cSChuck Lever /** 7139903cd1cSChuck Lever * xs_tcp_state_change - callback to handle TCP socket state changes 7149903cd1cSChuck Lever * @sk: socket whose state has changed 7159903cd1cSChuck Lever * 7169903cd1cSChuck Lever */ 7179903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk) 718a246b010SChuck Lever { 719a246b010SChuck Lever struct rpc_xprt *xprt; 720a246b010SChuck Lever 721a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 722a246b010SChuck Lever if (!(xprt = xprt_from_sock(sk))) 723a246b010SChuck Lever goto out; 7249903cd1cSChuck Lever dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); 725a246b010SChuck Lever dprintk("RPC: state %x conn %d dead %d zapped %d\n", 726a246b010SChuck Lever sk->sk_state, xprt_connected(xprt), 727a246b010SChuck Lever sock_flag(sk, SOCK_DEAD), 728a246b010SChuck Lever sock_flag(sk, SOCK_ZAPPED)); 729a246b010SChuck Lever 730a246b010SChuck Lever switch (sk->sk_state) { 731a246b010SChuck Lever case TCP_ESTABLISHED: 7324a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 733a246b010SChuck Lever if (!xprt_test_and_set_connected(xprt)) { 734a246b010SChuck Lever /* Reset TCP record info */ 735a246b010SChuck Lever xprt->tcp_offset = 0; 736a246b010SChuck Lever xprt->tcp_reclen = 0; 737a246b010SChuck Lever xprt->tcp_copied = 0; 738a246b010SChuck Lever xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID; 73944fbac22SChuck Lever xprt_wake_pending_tasks(xprt, 0); 740a246b010SChuck Lever } 7414a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 742a246b010SChuck Lever break; 743a246b010SChuck Lever case TCP_SYN_SENT: 744a246b010SChuck Lever case TCP_SYN_RECV: 745a246b010SChuck Lever break; 746a246b010SChuck Lever default: 747a246b010SChuck Lever xprt_disconnect(xprt); 748a246b010SChuck Lever break; 749a246b010SChuck Lever } 750a246b010SChuck Lever out: 751a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 752a246b010SChuck Lever } 753a246b010SChuck Lever 7549903cd1cSChuck Lever /** 755c7b2cae8SChuck Lever * xs_udp_write_space - callback invoked when socket buffer space 756c7b2cae8SChuck Lever * becomes available 7579903cd1cSChuck Lever * @sk: socket whose state has changed 7589903cd1cSChuck Lever * 759a246b010SChuck Lever * Called when more output buffer space is available for this socket. 760a246b010SChuck Lever * We try not to wake our writers until they can make "significant" 761c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 762a246b010SChuck Lever * with a bunch of small requests. 763a246b010SChuck Lever */ 764c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk) 765a246b010SChuck Lever { 766a246b010SChuck Lever read_lock(&sk->sk_callback_lock); 767c7b2cae8SChuck Lever 768c7b2cae8SChuck Lever /* from net/core/sock.c:sock_def_write_space */ 769c7b2cae8SChuck Lever if (sock_writeable(sk)) { 770c7b2cae8SChuck Lever struct socket *sock; 771c7b2cae8SChuck Lever struct rpc_xprt *xprt; 772c7b2cae8SChuck Lever 773c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 774a246b010SChuck Lever goto out; 775c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 776c7b2cae8SChuck Lever goto out; 777c7b2cae8SChuck Lever if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) 778a246b010SChuck Lever goto out; 779a246b010SChuck Lever 780c7b2cae8SChuck Lever xprt_write_space(xprt); 781a246b010SChuck Lever } 782a246b010SChuck Lever 783c7b2cae8SChuck Lever out: 784c7b2cae8SChuck Lever read_unlock(&sk->sk_callback_lock); 785c7b2cae8SChuck Lever } 786c7b2cae8SChuck Lever 787c7b2cae8SChuck Lever /** 788c7b2cae8SChuck Lever * xs_tcp_write_space - callback invoked when socket buffer space 789c7b2cae8SChuck Lever * becomes available 790c7b2cae8SChuck Lever * @sk: socket whose state has changed 791c7b2cae8SChuck Lever * 792c7b2cae8SChuck Lever * Called when more output buffer space is available for this socket. 793c7b2cae8SChuck Lever * We try not to wake our writers until they can make "significant" 794c7b2cae8SChuck Lever * progress, otherwise we'll waste resources thrashing kernel_sendmsg 795c7b2cae8SChuck Lever * with a bunch of small requests. 796c7b2cae8SChuck Lever */ 797c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk) 798c7b2cae8SChuck Lever { 799c7b2cae8SChuck Lever read_lock(&sk->sk_callback_lock); 800c7b2cae8SChuck Lever 801c7b2cae8SChuck Lever /* from net/core/stream.c:sk_stream_write_space */ 802c7b2cae8SChuck Lever if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) { 803c7b2cae8SChuck Lever struct socket *sock; 804c7b2cae8SChuck Lever struct rpc_xprt *xprt; 805c7b2cae8SChuck Lever 806c7b2cae8SChuck Lever if (unlikely(!(sock = sk->sk_socket))) 807c7b2cae8SChuck Lever goto out; 808c7b2cae8SChuck Lever if (unlikely(!(xprt = xprt_from_sock(sk)))) 809c7b2cae8SChuck Lever goto out; 810c7b2cae8SChuck Lever if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags))) 811a246b010SChuck Lever goto out; 812a246b010SChuck Lever 813c7b2cae8SChuck Lever xprt_write_space(xprt); 814c7b2cae8SChuck Lever } 815c7b2cae8SChuck Lever 816a246b010SChuck Lever out: 817a246b010SChuck Lever read_unlock(&sk->sk_callback_lock); 818a246b010SChuck Lever } 819a246b010SChuck Lever 8209903cd1cSChuck Lever /** 8219903cd1cSChuck Lever * xs_set_buffer_size - set send and receive limits 8229903cd1cSChuck Lever * @xprt: generic transport 8239903cd1cSChuck Lever * 8249903cd1cSChuck Lever * Set socket send and receive limits based on the 8259903cd1cSChuck Lever * sndsize and rcvsize fields in the generic transport 8269903cd1cSChuck Lever * structure. This applies only to UDP sockets. 827a246b010SChuck Lever */ 8289903cd1cSChuck Lever static void xs_set_buffer_size(struct rpc_xprt *xprt) 829a246b010SChuck Lever { 830a246b010SChuck Lever struct sock *sk = xprt->inet; 831a246b010SChuck Lever 832a246b010SChuck Lever if (xprt->stream) 833a246b010SChuck Lever return; 834a246b010SChuck Lever if (xprt->rcvsize) { 835a246b010SChuck Lever sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 836a246b010SChuck Lever sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2; 837a246b010SChuck Lever } 838a246b010SChuck Lever if (xprt->sndsize) { 839a246b010SChuck Lever sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 840a246b010SChuck Lever sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2; 841a246b010SChuck Lever sk->sk_write_space(sk); 842a246b010SChuck Lever } 843a246b010SChuck Lever } 844a246b010SChuck Lever 8459903cd1cSChuck Lever static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock) 846a246b010SChuck Lever { 847a246b010SChuck Lever struct sockaddr_in myaddr = { 848a246b010SChuck Lever .sin_family = AF_INET, 849a246b010SChuck Lever }; 850a246b010SChuck Lever int err, port; 851a246b010SChuck Lever 852a246b010SChuck Lever /* Were we already bound to a given port? Try to reuse it */ 853a246b010SChuck Lever port = xprt->port; 854a246b010SChuck Lever do { 855a246b010SChuck Lever myaddr.sin_port = htons(port); 856a246b010SChuck Lever err = sock->ops->bind(sock, (struct sockaddr *) &myaddr, 857a246b010SChuck Lever sizeof(myaddr)); 858a246b010SChuck Lever if (err == 0) { 859a246b010SChuck Lever xprt->port = port; 8609903cd1cSChuck Lever dprintk("RPC: xs_bindresvport bound to port %u\n", 8619903cd1cSChuck Lever port); 862a246b010SChuck Lever return 0; 863a246b010SChuck Lever } 864a246b010SChuck Lever if (--port == 0) 8659903cd1cSChuck Lever port = XS_MAX_RESVPORT; 866a246b010SChuck Lever } while (err == -EADDRINUSE && port != xprt->port); 867a246b010SChuck Lever 8689903cd1cSChuck Lever dprintk("RPC: can't bind to reserved port (%d).\n", -err); 869a246b010SChuck Lever return err; 870a246b010SChuck Lever } 871a246b010SChuck Lever 8729903cd1cSChuck Lever /** 873b0d93ad5SChuck Lever * xs_udp_connect_worker - set up a UDP socket 8749903cd1cSChuck Lever * @args: RPC transport to connect 8759903cd1cSChuck Lever * 8769903cd1cSChuck Lever * Invoked by a work queue tasklet. 877a246b010SChuck Lever */ 878b0d93ad5SChuck Lever static void xs_udp_connect_worker(void *args) 879a246b010SChuck Lever { 880a246b010SChuck Lever struct rpc_xprt *xprt = (struct rpc_xprt *) args; 881a246b010SChuck Lever struct socket *sock = xprt->sock; 882b0d93ad5SChuck Lever int err, status = -EIO; 883a246b010SChuck Lever 884a246b010SChuck Lever if (xprt->shutdown || xprt->addr.sin_port == 0) 885a246b010SChuck Lever goto out; 886a246b010SChuck Lever 887b0d93ad5SChuck Lever dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt); 8889903cd1cSChuck Lever 889b0d93ad5SChuck Lever /* Start by resetting any existing state */ 8909903cd1cSChuck Lever xs_close(xprt); 891b0d93ad5SChuck Lever 892b0d93ad5SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) { 893b0d93ad5SChuck Lever dprintk("RPC: can't create UDP transport socket (%d).\n", -err); 894a246b010SChuck Lever goto out; 895a246b010SChuck Lever } 896a246b010SChuck Lever 897b0d93ad5SChuck Lever if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { 898b0d93ad5SChuck Lever sock_release(sock); 899b0d93ad5SChuck Lever goto out; 900b0d93ad5SChuck Lever } 901b0d93ad5SChuck Lever 902b0d93ad5SChuck Lever if (!xprt->inet) { 903b0d93ad5SChuck Lever struct sock *sk = sock->sk; 904b0d93ad5SChuck Lever 905b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 906b0d93ad5SChuck Lever 907b0d93ad5SChuck Lever sk->sk_user_data = xprt; 908b0d93ad5SChuck Lever xprt->old_data_ready = sk->sk_data_ready; 909b0d93ad5SChuck Lever xprt->old_state_change = sk->sk_state_change; 910b0d93ad5SChuck Lever xprt->old_write_space = sk->sk_write_space; 911b0d93ad5SChuck Lever sk->sk_data_ready = xs_udp_data_ready; 912b0d93ad5SChuck Lever sk->sk_write_space = xs_udp_write_space; 913b0d93ad5SChuck Lever sk->sk_no_check = UDP_CSUM_NORCV; 914b0d93ad5SChuck Lever 915b0d93ad5SChuck Lever xprt_set_connected(xprt); 916b0d93ad5SChuck Lever 917b0d93ad5SChuck Lever /* Reset to new socket */ 918b0d93ad5SChuck Lever xprt->sock = sock; 919b0d93ad5SChuck Lever xprt->inet = sk; 920b0d93ad5SChuck Lever 921b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 922b0d93ad5SChuck Lever } 923b0d93ad5SChuck Lever xs_set_buffer_size(xprt); 924a246b010SChuck Lever status = 0; 925b0d93ad5SChuck Lever out: 926b0d93ad5SChuck Lever xprt_wake_pending_tasks(xprt, status); 927b0d93ad5SChuck Lever xprt_clear_connecting(xprt); 928b0d93ad5SChuck Lever } 929b0d93ad5SChuck Lever 930b0d93ad5SChuck Lever /** 931b0d93ad5SChuck Lever * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint 932b0d93ad5SChuck Lever * @args: RPC transport to connect 933b0d93ad5SChuck Lever * 934b0d93ad5SChuck Lever * Invoked by a work queue tasklet. 935b0d93ad5SChuck Lever */ 936b0d93ad5SChuck Lever static void xs_tcp_connect_worker(void *args) 937b0d93ad5SChuck Lever { 938b0d93ad5SChuck Lever struct rpc_xprt *xprt = (struct rpc_xprt *)args; 939b0d93ad5SChuck Lever struct socket *sock = xprt->sock; 940b0d93ad5SChuck Lever int err, status = -EIO; 941b0d93ad5SChuck Lever 942b0d93ad5SChuck Lever if (xprt->shutdown || xprt->addr.sin_port == 0) 943a246b010SChuck Lever goto out; 944a246b010SChuck Lever 945b0d93ad5SChuck Lever dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt); 946b0d93ad5SChuck Lever 947b0d93ad5SChuck Lever /* Start by resetting any existing socket state */ 948b0d93ad5SChuck Lever xs_close(xprt); 949b0d93ad5SChuck Lever 950b0d93ad5SChuck Lever if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) { 951b0d93ad5SChuck Lever dprintk("RPC: can't create TCP transport socket (%d).\n", -err); 952b0d93ad5SChuck Lever goto out; 953b0d93ad5SChuck Lever } 954b0d93ad5SChuck Lever 955b0d93ad5SChuck Lever if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) { 956b0d93ad5SChuck Lever sock_release(sock); 957b0d93ad5SChuck Lever goto out; 958b0d93ad5SChuck Lever } 959b0d93ad5SChuck Lever 960b0d93ad5SChuck Lever if (!xprt->inet) { 961b0d93ad5SChuck Lever struct sock *sk = sock->sk; 962b0d93ad5SChuck Lever 963b0d93ad5SChuck Lever write_lock_bh(&sk->sk_callback_lock); 964b0d93ad5SChuck Lever 965b0d93ad5SChuck Lever sk->sk_user_data = xprt; 966b0d93ad5SChuck Lever xprt->old_data_ready = sk->sk_data_ready; 967b0d93ad5SChuck Lever xprt->old_state_change = sk->sk_state_change; 968b0d93ad5SChuck Lever xprt->old_write_space = sk->sk_write_space; 969b0d93ad5SChuck Lever sk->sk_data_ready = xs_tcp_data_ready; 970b0d93ad5SChuck Lever sk->sk_state_change = xs_tcp_state_change; 971b0d93ad5SChuck Lever sk->sk_write_space = xs_tcp_write_space; 972b0d93ad5SChuck Lever tcp_sk(sk)->nonagle = 1; 973b0d93ad5SChuck Lever 974b0d93ad5SChuck Lever xprt_clear_connected(xprt); 975b0d93ad5SChuck Lever 976b0d93ad5SChuck Lever /* Reset to new socket */ 977b0d93ad5SChuck Lever xprt->sock = sock; 978b0d93ad5SChuck Lever xprt->inet = sk; 979b0d93ad5SChuck Lever 980b0d93ad5SChuck Lever write_unlock_bh(&sk->sk_callback_lock); 981b0d93ad5SChuck Lever } 982b0d93ad5SChuck Lever 983b0d93ad5SChuck Lever /* Tell the socket layer to start connecting... */ 984a246b010SChuck Lever status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr, 985a246b010SChuck Lever sizeof(xprt->addr), O_NONBLOCK); 986a246b010SChuck Lever dprintk("RPC: %p connect status %d connected %d sock state %d\n", 987a246b010SChuck Lever xprt, -status, xprt_connected(xprt), sock->sk->sk_state); 988a246b010SChuck Lever if (status < 0) { 989a246b010SChuck Lever switch (status) { 990a246b010SChuck Lever case -EINPROGRESS: 991a246b010SChuck Lever case -EALREADY: 992a246b010SChuck Lever goto out_clear; 993a246b010SChuck Lever } 994a246b010SChuck Lever } 995a246b010SChuck Lever out: 99644fbac22SChuck Lever xprt_wake_pending_tasks(xprt, status); 997a246b010SChuck Lever out_clear: 9982226feb6SChuck Lever xprt_clear_connecting(xprt); 999a246b010SChuck Lever } 1000a246b010SChuck Lever 10019903cd1cSChuck Lever /** 10029903cd1cSChuck Lever * xs_connect - connect a socket to a remote endpoint 10039903cd1cSChuck Lever * @task: address of RPC task that manages state of connect request 10049903cd1cSChuck Lever * 10059903cd1cSChuck Lever * TCP: If the remote end dropped the connection, delay reconnecting. 10069903cd1cSChuck Lever */ 10079903cd1cSChuck Lever static void xs_connect(struct rpc_task *task) 1008a246b010SChuck Lever { 1009a246b010SChuck Lever struct rpc_xprt *xprt = task->tk_xprt; 1010a246b010SChuck Lever 1011b0d93ad5SChuck Lever if (xprt_test_and_set_connecting(xprt)) 1012b0d93ad5SChuck Lever return; 1013b0d93ad5SChuck Lever 10149903cd1cSChuck Lever if (xprt->sock != NULL) { 10159903cd1cSChuck Lever dprintk("RPC: xs_connect delayed xprt %p\n", xprt); 101655aa4f58SChuck Lever schedule_delayed_work(&xprt->connect_worker, 1017a246b010SChuck Lever RPC_REESTABLISH_TIMEOUT); 10189903cd1cSChuck Lever } else { 10199903cd1cSChuck Lever dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); 102055aa4f58SChuck Lever schedule_work(&xprt->connect_worker); 1021b0d93ad5SChuck Lever 1022a246b010SChuck Lever /* flush_scheduled_work can sleep... */ 1023a246b010SChuck Lever if (!RPC_IS_ASYNC(task)) 1024a246b010SChuck Lever flush_scheduled_work(); 1025a246b010SChuck Lever } 1026a246b010SChuck Lever } 1027a246b010SChuck Lever 1028*262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = { 10299903cd1cSChuck Lever .set_buffer_size = xs_set_buffer_size, 10309903cd1cSChuck Lever .connect = xs_connect, 1031*262965f5SChuck Lever .send_request = xs_udp_send_request, 1032*262965f5SChuck Lever .close = xs_close, 1033*262965f5SChuck Lever .destroy = xs_destroy, 1034*262965f5SChuck Lever }; 1035*262965f5SChuck Lever 1036*262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = { 1037*262965f5SChuck Lever .set_buffer_size = xs_set_buffer_size, 1038*262965f5SChuck Lever .connect = xs_connect, 1039*262965f5SChuck Lever .send_request = xs_tcp_send_request, 10409903cd1cSChuck Lever .close = xs_close, 10419903cd1cSChuck Lever .destroy = xs_destroy, 1042a246b010SChuck Lever }; 1043a246b010SChuck Lever 1044a246b010SChuck Lever extern unsigned int xprt_udp_slot_table_entries; 1045a246b010SChuck Lever extern unsigned int xprt_tcp_slot_table_entries; 1046a246b010SChuck Lever 10479903cd1cSChuck Lever /** 10489903cd1cSChuck Lever * xs_setup_udp - Set up transport to use a UDP socket 10499903cd1cSChuck Lever * @xprt: transport to set up 10509903cd1cSChuck Lever * @to: timeout parameters 10519903cd1cSChuck Lever * 10529903cd1cSChuck Lever */ 1053a246b010SChuck Lever int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to) 1054a246b010SChuck Lever { 1055a246b010SChuck Lever size_t slot_table_size; 1056a246b010SChuck Lever 1057a246b010SChuck Lever dprintk("RPC: setting up udp-ipv4 transport...\n"); 1058a246b010SChuck Lever 1059a246b010SChuck Lever xprt->max_reqs = xprt_udp_slot_table_entries; 1060a246b010SChuck Lever slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); 1061a246b010SChuck Lever xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); 1062a246b010SChuck Lever if (xprt->slot == NULL) 1063a246b010SChuck Lever return -ENOMEM; 1064a246b010SChuck Lever memset(xprt->slot, 0, slot_table_size); 1065a246b010SChuck Lever 1066a246b010SChuck Lever xprt->prot = IPPROTO_UDP; 10679903cd1cSChuck Lever xprt->port = XS_MAX_RESVPORT; 1068a246b010SChuck Lever xprt->stream = 0; 1069a246b010SChuck Lever xprt->nocong = 0; 1070a246b010SChuck Lever xprt->cwnd = RPC_INITCWND; 1071a246b010SChuck Lever xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; 1072a246b010SChuck Lever /* XXX: header size can vary due to auth type, IPv6, etc. */ 1073a246b010SChuck Lever xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); 1074a246b010SChuck Lever 1075b0d93ad5SChuck Lever INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt); 1076a246b010SChuck Lever 1077*262965f5SChuck Lever xprt->ops = &xs_udp_ops; 1078a246b010SChuck Lever 1079a246b010SChuck Lever if (to) 1080a246b010SChuck Lever xprt->timeout = *to; 1081a246b010SChuck Lever else 10829903cd1cSChuck Lever xprt_set_timeout(&xprt->timeout, 5, 5 * HZ); 1083a246b010SChuck Lever 1084a246b010SChuck Lever return 0; 1085a246b010SChuck Lever } 1086a246b010SChuck Lever 10879903cd1cSChuck Lever /** 10889903cd1cSChuck Lever * xs_setup_tcp - Set up transport to use a TCP socket 10899903cd1cSChuck Lever * @xprt: transport to set up 10909903cd1cSChuck Lever * @to: timeout parameters 10919903cd1cSChuck Lever * 10929903cd1cSChuck Lever */ 1093a246b010SChuck Lever int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to) 1094a246b010SChuck Lever { 1095a246b010SChuck Lever size_t slot_table_size; 1096a246b010SChuck Lever 1097a246b010SChuck Lever dprintk("RPC: setting up tcp-ipv4 transport...\n"); 1098a246b010SChuck Lever 1099a246b010SChuck Lever xprt->max_reqs = xprt_tcp_slot_table_entries; 1100a246b010SChuck Lever slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]); 1101a246b010SChuck Lever xprt->slot = kmalloc(slot_table_size, GFP_KERNEL); 1102a246b010SChuck Lever if (xprt->slot == NULL) 1103a246b010SChuck Lever return -ENOMEM; 1104a246b010SChuck Lever memset(xprt->slot, 0, slot_table_size); 1105a246b010SChuck Lever 1106a246b010SChuck Lever xprt->prot = IPPROTO_TCP; 11079903cd1cSChuck Lever xprt->port = XS_MAX_RESVPORT; 1108a246b010SChuck Lever xprt->stream = 1; 1109a246b010SChuck Lever xprt->nocong = 1; 1110a246b010SChuck Lever xprt->cwnd = RPC_MAXCWND(xprt); 1111a246b010SChuck Lever xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0; 1112a246b010SChuck Lever xprt->max_payload = (1U << 31) - 1; 1113a246b010SChuck Lever 1114b0d93ad5SChuck Lever INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt); 1115a246b010SChuck Lever 1116*262965f5SChuck Lever xprt->ops = &xs_tcp_ops; 1117a246b010SChuck Lever 1118a246b010SChuck Lever if (to) 1119a246b010SChuck Lever xprt->timeout = *to; 1120a246b010SChuck Lever else 11219903cd1cSChuck Lever xprt_set_timeout(&xprt->timeout, 2, 60 * HZ); 1122a246b010SChuck Lever 1123a246b010SChuck Lever return 0; 1124a246b010SChuck Lever } 1125