21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/capability.h>
27 #include <linux/errno.h>
28 #include <linux/socket.h>
30 #include <linux/net.h>
33 #include <linux/udp.h>
34 #include <linux/tcp.h>
40 #ifdef CONFIG_SUNRPC_BACKCHANNEL
51 static void xs_close(
struct rpc_xprt *xprt);
56 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
57 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
58 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
60 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
61 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
63 #define XS_TCP_LINGER_TO (15U * HZ)
77 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
78 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
79 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
80 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
81 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
91 .
procname =
"udp_slot_table_entries",
92 .data = &xprt_udp_slot_table_entries,
93 .maxlen =
sizeof(
unsigned int),
96 .extra1 = &min_slot_table_size,
97 .extra2 = &max_slot_table_size
100 .procname =
"tcp_slot_table_entries",
101 .data = &xprt_tcp_slot_table_entries,
102 .maxlen =
sizeof(
unsigned int),
105 .extra1 = &min_slot_table_size,
106 .extra2 = &max_slot_table_size
109 .procname =
"tcp_max_slot_table_entries",
110 .data = &xprt_max_tcp_slot_table_entries,
111 .maxlen =
sizeof(
unsigned int),
114 .extra1 = &min_slot_table_size,
115 .extra2 = &max_tcp_slot_table_limit
118 .procname =
"min_resvport",
119 .data = &xprt_min_resvport,
120 .maxlen =
sizeof(
unsigned int),
123 .extra1 = &xprt_min_resvport_limit,
124 .extra2 = &xprt_max_resvport_limit
127 .procname =
"max_resvport",
128 .data = &xprt_max_resvport,
129 .maxlen =
sizeof(
unsigned int),
132 .extra1 = &xprt_min_resvport_limit,
133 .extra2 = &xprt_max_resvport_limit
136 .procname =
"tcp_fin_timeout",
137 .data = &xs_tcp_fin_timeout,
138 .maxlen =
sizeof(xs_tcp_fin_timeout),
149 .child = xs_tunables_table
159 #define XS_BIND_TO (60U * HZ)
165 #define XS_UDP_REEST_TO (2U * HZ)
176 #define XS_TCP_INIT_REEST_TO (3U * HZ)
177 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
184 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
187 # undef RPC_DEBUG_DATA
188 # define RPCDBG_FACILITY RPCDBG_TRANS
191 #ifdef RPC_DEBUG_DATA
198 for (j = 0; j < count && j < 128; j += 4) {
205 buf[j], buf[j+1], buf[j+2], buf[j+3]);
262 #define TCP_RCV_LAST_FRAG (1UL << 0)
263 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
264 #define TCP_RCV_COPY_XID (1UL << 2)
265 #define TCP_RCV_COPY_DATA (1UL << 3)
266 #define TCP_RCV_READ_CALLDIR (1UL << 4)
267 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
272 #define TCP_RPC_REPLY (1UL << 6)
274 static inline struct sockaddr *xs_addr(
struct rpc_xprt *xprt)
276 return (
struct sockaddr *) &xprt->addr;
279 static inline struct sockaddr_un *xs_addr_un(
struct rpc_xprt *xprt)
284 static inline struct sockaddr_in *xs_addr_in(
struct rpc_xprt *xprt)
289 static inline struct sockaddr_in6 *xs_addr_in6(
struct rpc_xprt *xprt)
294 static void xs_format_common_peer_addresses(
struct rpc_xprt *xprt)
296 struct sockaddr *sap = xs_addr(xprt);
304 sun = xs_addr_un(xprt);
306 xprt->address_strings[RPC_DISPLAY_ADDR] =
311 xprt->address_strings[RPC_DISPLAY_ADDR] =
313 sin = xs_addr_in(xprt);
318 xprt->address_strings[RPC_DISPLAY_ADDR] =
320 sin6 = xs_addr_in6(xprt);
330 static void xs_format_common_peer_ports(
struct rpc_xprt *xprt)
332 struct sockaddr *sap = xs_addr(xprt);
335 snprintf(buf,
sizeof(buf),
"%u", rpc_get_port(sap));
338 snprintf(buf,
sizeof(buf),
"%4hx", rpc_get_port(sap));
342 static void xs_format_peer_addresses(
struct rpc_xprt *xprt,
346 xprt->address_strings[RPC_DISPLAY_PROTO] =
protocol;
347 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
348 xs_format_common_peer_addresses(xprt);
349 xs_format_common_peer_ports(xprt);
352 static void xs_update_peer_port(
struct rpc_xprt *xprt)
354 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
355 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
357 xs_format_common_peer_ports(xprt);
360 static void xs_free_peer_addresses(
struct rpc_xprt *xprt)
364 for (i = 0; i < RPC_DISPLAY_MAX; i++)
366 case RPC_DISPLAY_PROTO:
367 case RPC_DISPLAY_NETID:
370 kfree(xprt->address_strings[i]);
374 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
376 static int xs_send_kvec(
struct socket *
sock,
struct sockaddr *
addr,
int addrlen,
struct kvec *vec,
unsigned int base,
int more)
380 .msg_namelen = addrlen,
385 .iov_len = vec->
iov_len - base,
393 static int xs_send_pagedata(
struct socket *sock,
struct xdr_buf *xdr,
unsigned int base,
int more)
396 unsigned int remainder;
399 remainder = xdr->page_len - base;
400 base += xdr->page_base;
408 if (remainder != 0 || more)
410 err = sock->
ops->sendpage(sock, *ppage, base, len, flags);
411 if (remainder == 0 || err != len)
433 static int xs_sendpages(
struct socket *sock,
struct sockaddr *addr,
int addrlen,
struct xdr_buf *xdr,
unsigned int base)
435 unsigned int remainder = xdr->len - base;
447 if (base < xdr->
head[0].iov_len || addr !=
NULL) {
448 unsigned int len = xdr->head[0].iov_len - base;
450 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
451 if (remainder == 0 || err != len)
456 base -= xdr->head[0].iov_len;
458 if (base < xdr->page_len) {
459 unsigned int len = xdr->page_len - base;
461 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
462 if (remainder == 0 || err != len)
467 base -= xdr->page_len;
469 if (base >= xdr->tail[0].iov_len)
471 err = xs_send_kvec(sock,
NULL, 0, &xdr->tail[0], base, 0);
484 transport->
inet->sk_write_pending--;
493 static int xs_nospace(
struct rpc_task *task)
496 struct rpc_xprt *xprt = req->rq_xprt;
500 dprintk(
"RPC: %5u xmit incomplete (%u left of %u)\n",
501 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
505 spin_lock_bh(&xprt->transport_lock);
508 if (xprt_connected(xprt)) {
515 transport->
inet->sk_write_pending++;
524 spin_unlock_bh(&xprt->transport_lock);
531 static inline void xs_encode_stream_record_marker(
struct xdr_buf *buf)
533 u32 reclen = buf->len -
sizeof(rpc_fraghdr);
534 rpc_fraghdr *base = buf->head[0].iov_base;
535 *base =
cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
549 static int xs_local_send_request(
struct rpc_task *task)
552 struct rpc_xprt *xprt = req->rq_xprt;
555 struct xdr_buf *xdr = &req->rq_snd_buf;
558 xs_encode_stream_record_marker(&req->rq_snd_buf);
560 xs_pktdump(
"packet data:",
561 req->rq_svec->iov_base, req->rq_svec->iov_len);
563 status = xs_sendpages(transport->
sock,
NULL, 0,
564 xdr, req->rq_bytes_sent);
566 __func__, xdr->len - req->rq_bytes_sent, status);
567 if (
likely(status >= 0)) {
568 req->rq_bytes_sent +=
status;
569 req->rq_xmit_bytes_sent +=
status;
570 if (
likely(req->rq_bytes_sent >= req->rq_slen)) {
571 req->rq_bytes_sent = 0;
579 status = xs_nospace(task);
582 dprintk(
"RPC: sendmsg returned unrecognized error %d\n",
603 static int xs_udp_send_request(
struct rpc_task *task)
606 struct rpc_xprt *xprt = req->rq_xprt;
608 struct xdr_buf *xdr = &req->rq_snd_buf;
611 xs_pktdump(
"packet data:",
612 req->rq_svec->iov_base,
613 req->rq_svec->iov_len);
615 if (!xprt_bound(xprt))
617 status = xs_sendpages(transport->
sock,
622 dprintk(
"RPC: xs_udp_send_request(%u) = %d\n",
623 xdr->len - req->rq_bytes_sent, status);
626 req->rq_xmit_bytes_sent +=
status;
627 if (status >= req->rq_slen)
639 status = xs_nospace(task);
642 dprintk(
"RPC: sendmsg returned unrecognized error %d\n",
662 static void xs_tcp_shutdown(
struct rpc_xprt *xprt)
685 static int xs_tcp_send_request(
struct rpc_task *task)
688 struct rpc_xprt *xprt = req->rq_xprt;
690 struct xdr_buf *xdr = &req->rq_snd_buf;
693 xs_encode_stream_record_marker(&req->rq_snd_buf);
695 xs_pktdump(
"packet data:",
696 req->rq_svec->iov_base,
697 req->rq_svec->iov_len);
703 status = xs_sendpages(transport->
sock,
704 NULL, 0, xdr, req->rq_bytes_sent);
706 dprintk(
"RPC: xs_tcp_send_request(%u) = %d\n",
707 xdr->len - req->rq_bytes_sent, status);
714 req->rq_bytes_sent +=
status;
715 req->rq_xmit_bytes_sent +=
status;
716 if (
likely(req->rq_bytes_sent >= req->rq_slen)) {
717 req->rq_bytes_sent = 0;
733 status = xs_nospace(task);
736 dprintk(
"RPC: sendmsg returned unrecognized error %d\n",
739 xs_tcp_shutdown(xprt);
758 static void xs_tcp_release_xprt(
struct rpc_xprt *xprt,
struct rpc_task *task)
760 struct rpc_rqst *
req;
762 if (task != xprt->snd_task)
769 if (req->rq_bytes_sent == 0)
771 if (req->rq_bytes_sent == req->rq_snd_buf.len)
773 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
785 static void xs_restore_old_callbacks(
struct sock_xprt *transport,
struct sock *sk)
792 static void xs_reset_transport(
struct sock_xprt *transport)
795 struct sock *sk = transport->
inet;
808 xs_restore_old_callbacks(transport, sk);
826 static void xs_close(
struct rpc_xprt *xprt)
830 dprintk(
"RPC: xs_close xprt %p\n", xprt);
832 xs_reset_transport(transport);
833 xprt->reestablish_timeout = 0;
836 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
837 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
843 static void xs_tcp_close(
struct rpc_xprt *xprt)
848 xs_tcp_shutdown(xprt);
856 static void xs_destroy(
struct rpc_xprt *xprt)
860 dprintk(
"RPC: xs_destroy xprt %p\n", xprt);
865 xs_free_peer_addresses(xprt);
870 static inline struct rpc_xprt *xprt_from_sock(
struct sock *sk)
875 static int xs_local_copy_to_xdr(
struct xdr_buf *xdr,
struct sk_buff *
skb)
877 struct xdr_skb_reader desc = {
879 .offset =
sizeof(rpc_fraghdr),
880 .
count = skb->
len -
sizeof(rpc_fraghdr),
897 static void xs_local_data_ready(
struct sock *sk,
int len)
900 struct rpc_xprt *xprt;
901 struct rpc_rqst *rovr;
903 int err, repsize, copied;
908 dprintk(
"RPC: %s...\n", __func__);
909 xprt = xprt_from_sock(sk);
917 repsize = skb->
len -
sizeof(rpc_fraghdr);
919 dprintk(
"RPC: impossible RPC reply size %d\n", repsize);
924 xp = skb_header_pointer(skb,
sizeof(rpc_fraghdr),
sizeof(_xid), &_xid);
929 spin_lock(&xprt->transport_lock);
933 task = rovr->rq_task;
935 copied = rovr->rq_private_buf.buflen;
936 if (copied > repsize)
939 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
940 dprintk(
"RPC: sk_buff copy failed\n");
947 spin_unlock(&xprt->transport_lock);
960 static void xs_udp_data_ready(
struct sock *sk,
int len)
963 struct rpc_xprt *xprt;
964 struct rpc_rqst *rovr;
966 int err, repsize, copied;
971 dprintk(
"RPC: xs_udp_data_ready...\n");
972 if (!(xprt = xprt_from_sock(sk)))
978 repsize = skb->
len -
sizeof(
struct udphdr);
980 dprintk(
"RPC: impossible RPC reply size %d!\n", repsize);
985 xp = skb_header_pointer(skb,
sizeof(
struct udphdr),
986 sizeof(_xid), &_xid);
991 spin_lock(&xprt->transport_lock);
995 task = rovr->rq_task;
997 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1012 spin_unlock(&xprt->transport_lock);
1023 static void xs_tcp_force_close(
struct rpc_xprt *xprt)
1025 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1029 static inline void xs_tcp_read_fraghdr(
struct rpc_xprt *xprt,
struct xdr_skb_reader *
desc)
1043 if (transport->
tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1047 transport->
tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1054 dprintk(
"RPC: invalid TCP record fragment length\n");
1055 xs_tcp_force_close(xprt);
1058 dprintk(
"RPC: reading TCP record fragment of length %d\n",
1062 static void xs_tcp_check_fraghdr(
struct sock_xprt *transport)
1075 static inline void xs_tcp_read_xid(
struct sock_xprt *transport,
struct xdr_skb_reader *desc)
1081 dprintk(
"RPC: reading XID (%Zu bytes)\n", len);
1090 dprintk(
"RPC: reading %s XID %08x\n",
1094 xs_tcp_check_fraghdr(transport);
1097 static inline void xs_tcp_read_calldir(
struct sock_xprt *transport,
1098 struct xdr_skb_reader *desc)
1112 dprintk(
"RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
1135 dprintk(
"RPC: invalid request message type\n");
1136 xs_tcp_force_close(&transport->
xprt);
1138 xs_tcp_check_fraghdr(transport);
1141 static inline void xs_tcp_read_common(
struct rpc_xprt *xprt,
1142 struct xdr_skb_reader *desc,
1143 struct rpc_rqst *
req)
1151 rcvbuf = &req->rq_private_buf;
1166 struct xdr_skb_reader my_desc;
1169 memcpy(&my_desc, desc,
sizeof(my_desc));
1170 my_desc.count = len;
1194 dprintk(
"RPC: XID %08x truncated request\n",
1196 dprintk(
"RPC: xprt = %p, tcp_copied = %lu, "
1197 "tcp_offset = %u, tcp_reclen = %u\n",
1203 dprintk(
"RPC: XID %08x read %Zd bytes\n",
1205 dprintk(
"RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1206 "tcp_reclen = %u\n", xprt, transport->
tcp_copied,
1209 if (transport->
tcp_copied == req->rq_private_buf.buflen)
1221 static inline int xs_tcp_read_reply(
struct rpc_xprt *xprt,
1222 struct xdr_skb_reader *desc)
1226 struct rpc_rqst *
req;
1231 spin_lock(&xprt->transport_lock);
1234 dprintk(
"RPC: XID %08x request not found!\n",
1236 spin_unlock(&xprt->transport_lock);
1240 xs_tcp_read_common(xprt, desc, req);
1245 spin_unlock(&xprt->transport_lock);
1249 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1257 static inline int xs_tcp_read_callback(
struct rpc_xprt *xprt,
1258 struct xdr_skb_reader *desc)
1262 struct rpc_rqst *
req;
1271 req->rq_xid = transport->
tcp_xid;
1272 dprintk(
"RPC: read callback XID %08x\n",
ntohl(req->rq_xid));
1273 xs_tcp_read_common(xprt, desc, req);
1276 struct svc_serv *bc_serv = xprt->bc_serv;
1284 dprintk(
"RPC: add callback request to list\n");
1285 spin_lock(&bc_serv->sv_cb_lock);
1286 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1287 spin_unlock(&bc_serv->sv_cb_lock);
1288 wake_up(&bc_serv->sv_cb_waitq);
1291 req->rq_private_buf.len = transport->
tcp_copied;
1296 static inline int _xs_tcp_read_data(
struct rpc_xprt *xprt,
1297 struct xdr_skb_reader *desc)
1303 xs_tcp_read_reply(xprt, desc) :
1304 xs_tcp_read_callback(xprt, desc);
1307 static inline int _xs_tcp_read_data(
struct rpc_xprt *xprt,
1308 struct xdr_skb_reader *desc)
1310 return xs_tcp_read_reply(xprt, desc);
1318 static void xs_tcp_read_data(
struct rpc_xprt *xprt,
1319 struct xdr_skb_reader *desc)
1324 if (_xs_tcp_read_data(xprt, desc) == 0)
1325 xs_tcp_check_fraghdr(transport);
1335 static inline void xs_tcp_read_discard(
struct sock_xprt *transport,
struct xdr_skb_reader *desc)
1340 if (len > desc->count)
1343 desc->offset += len;
1345 dprintk(
"RPC: discarded %Zu bytes\n", len);
1346 xs_tcp_check_fraghdr(transport);
1351 struct rpc_xprt *xprt = rd_desc->
arg.
data;
1353 struct xdr_skb_reader desc = {
1359 dprintk(
"RPC: xs_tcp_data_recv started\n");
1364 xs_tcp_read_fraghdr(xprt, &desc);
1369 xs_tcp_read_xid(transport, &desc);
1374 xs_tcp_read_calldir(transport, &desc);
1379 xs_tcp_read_data(xprt, &desc);
1383 xs_tcp_read_discard(transport, &desc);
1384 }
while (desc.count);
1385 dprintk(
"RPC: xs_tcp_data_recv done\n");
1386 return len - desc.count;
1395 static void xs_tcp_data_ready(
struct sock *sk,
int bytes)
1397 struct rpc_xprt *xprt;
1401 dprintk(
"RPC: xs_tcp_data_ready...\n");
1404 if (!(xprt = xprt_from_sock(sk)))
1409 if (xprt->reestablish_timeout)
1410 xprt->reestablish_timeout = 0;
1415 rd_desc.
count = 65536;
1427 static void xs_tcp_schedule_linger_timeout(
struct rpc_xprt *xprt,
1432 if (xprt_test_and_set_connecting(xprt))
1434 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1440 static void xs_tcp_cancel_linger_timeout(
struct rpc_xprt *xprt)
1446 if (!
test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1449 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1450 xprt_clear_connecting(xprt);
1453 static void xs_sock_reset_connection_flags(
struct rpc_xprt *xprt)
1456 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1457 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1458 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1463 static void xs_sock_mark_closed(
struct rpc_xprt *xprt)
1465 xs_sock_reset_connection_flags(xprt);
1475 static void xs_tcp_state_change(
struct sock *sk)
1477 struct rpc_xprt *xprt;
1480 if (!(xprt = xprt_from_sock(sk)))
1482 dprintk(
"RPC: xs_tcp_state_change client %p...\n", xprt);
1483 dprintk(
"RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1484 sk->sk_state, xprt_connected(xprt),
1489 switch (sk->sk_state) {
1491 spin_lock(&xprt->transport_lock);
1492 if (!xprt_test_and_set_connected(xprt)) {
1505 spin_unlock(&xprt->transport_lock);
1509 xprt->connect_cookie++;
1510 xprt->reestablish_timeout = 0;
1511 set_bit(XPRT_CLOSING, &xprt->state);
1513 clear_bit(XPRT_CONNECTED, &xprt->state);
1514 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1516 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1520 xprt->connect_cookie++;
1521 clear_bit(XPRT_CONNECTED, &xprt->state);
1522 xs_tcp_force_close(xprt);
1532 set_bit(XPRT_CLOSING, &xprt->state);
1533 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1535 clear_bit(XPRT_CONNECTED, &xprt->state);
1539 xs_tcp_cancel_linger_timeout(xprt);
1540 xs_sock_mark_closed(xprt);
1546 static void xs_write_space(
struct sock *sk)
1549 struct rpc_xprt *xprt;
1555 if (
unlikely(!(xprt = xprt_from_sock(sk))))
1573 static void xs_udp_write_space(
struct sock *sk)
1578 if (sock_writeable(sk))
1594 static void xs_tcp_write_space(
struct sock *sk)
1599 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1605 static void xs_udp_do_set_buffer_size(
struct rpc_xprt *xprt)
1608 struct sock *sk = transport->
inet;
1629 static void xs_udp_set_buffer_size(
struct rpc_xprt *xprt,
size_t sndsize,
size_t rcvsize)
1635 transport->
sndsize = sndsize + 1024;
1638 transport->
rcvsize = rcvsize + 1024;
1640 xs_udp_do_set_buffer_size(xprt);
1649 static void xs_udp_timer(
struct rpc_task *task)
1654 static unsigned short xs_get_random_port(
void)
1656 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1658 return rand + xprt_min_resvport;
1667 static void xs_set_port(
struct rpc_xprt *xprt,
unsigned short port)
1669 dprintk(
"RPC: setting port for xprt %p to %u\n", xprt, port);
1671 rpc_set_port(xs_addr(xprt), port);
1672 xs_update_peer_port(xprt);
1675 static unsigned short xs_get_srcport(
struct sock_xprt *transport)
1677 unsigned short port = transport->
srcport;
1679 if (port == 0 && transport->
xprt.resvport)
1680 port = xs_get_random_port();
1684 static unsigned short xs_next_srcport(
struct sock_xprt *transport,
unsigned short port)
1688 if (!transport->
xprt.resvport)
1691 return xprt_max_resvport;
1698 unsigned short port = xs_get_srcport(transport);
1699 unsigned short last;
1703 rpc_set_port((
struct sockaddr *)&myaddr, port);
1705 transport->
xprt.addrlen);
1713 port = xs_next_srcport(transport, port);
1718 if (myaddr.ss_family ==
AF_INET)
1719 dprintk(
"RPC: %s %pI4:%u: %s (%d)\n", __func__,
1721 port, err ?
"failed" :
"ok", err);
1723 dprintk(
"RPC: %s %pI6:%u: %s (%d)\n", __func__,
1725 port, err ?
"failed" :
"ok", err);
1732 static void xs_local_rpcbind(
struct rpc_task *task)
1734 xprt_set_bound(task->tk_xprt);
1737 static void xs_local_set_port(
struct rpc_xprt *xprt,
unsigned short port)
1741 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1745 static inline void xs_reclassify_socketu(
struct socket *sock)
1747 struct sock *sk = sock->
sk;
1751 &xs_slock_key[1],
"sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1754 static inline void xs_reclassify_socket4(
struct socket *sock)
1756 struct sock *sk = sock->
sk;
1760 &xs_slock_key[0],
"sk_lock-AF_INET-RPC", &xs_key[0]);
1763 static inline void xs_reclassify_socket6(
struct socket *sock)
1765 struct sock *sk = sock->
sk;
1769 &xs_slock_key[1],
"sk_lock-AF_INET6-RPC", &xs_key[1]);
1772 static inline void xs_reclassify_socket(
int family,
struct socket *sock)
1776 xs_reclassify_socketu(sock);
1779 xs_reclassify_socket4(sock);
1782 xs_reclassify_socket6(sock);
1787 static inline void xs_reclassify_socketu(
struct socket *sock)
1791 static inline void xs_reclassify_socket4(
struct socket *sock)
1795 static inline void xs_reclassify_socket6(
struct socket *sock)
1799 static inline void xs_reclassify_socket(
int family,
struct socket *sock)
1804 static struct socket *xs_create_sock(
struct rpc_xprt *xprt,
1810 err =
__sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1812 dprintk(
"RPC: can't create %d transport socket (%d).\n",
1816 xs_reclassify_socket(family, sock);
1818 err = xs_bind(transport, sock);
1826 return ERR_PTR(err);
1829 static int xs_local_finish_connecting(
struct rpc_xprt *xprt,
1835 if (!transport->
inet) {
1836 struct sock *sk = sock->
sk;
1840 xs_save_old_callbacks(transport, sk);
1847 xprt_clear_connected(xprt);
1857 xprt->stat.connect_count++;
1858 xprt->stat.connect_start =
jiffies;
1874 struct rpc_xprt *xprt = &transport->
xprt;
1880 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1884 dprintk(
"RPC: can't create AF_LOCAL "
1885 "transport socket (%d).\n", -status);
1888 xs_reclassify_socketu(sock);
1890 dprintk(
"RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1891 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1893 status = xs_local_finish_connecting(xprt, sock);
1896 dprintk(
"RPC: xprt %p connected to %s\n",
1897 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1898 xprt_set_connected(xprt);
1901 dprintk(
"RPC: xprt %p: socket %s does not exist\n",
1902 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1907 xprt->address_strings[RPC_DISPLAY_ADDR]);
1911 xprt_clear_connecting(xprt);
1916 #ifdef CONFIG_SUNRPC_SWAP
1917 static void xs_set_memalloc(
struct rpc_xprt *xprt)
1932 int xs_swapper(
struct rpc_xprt *xprt,
int enable)
1940 xs_set_memalloc(xprt);
1941 }
else if (xprt->swapper) {
1950 static void xs_set_memalloc(
struct rpc_xprt *xprt)
1955 static void xs_udp_finish_connecting(
struct rpc_xprt *xprt,
struct socket *sock)
1959 if (!transport->
inet) {
1960 struct sock *sk = sock->
sk;
1964 xs_save_old_callbacks(transport, sk);
1972 xprt_set_connected(xprt);
1978 xs_set_memalloc(xprt);
1982 xs_udp_do_set_buffer_size(xprt);
1985 static void xs_udp_setup_socket(
struct work_struct *work)
1989 struct rpc_xprt *xprt = &transport->
xprt;
1996 xs_reset_transport(transport);
1997 sock = xs_create_sock(xprt, transport,
2002 dprintk(
"RPC: worker connecting xprt %p via %s to "
2003 "%s (port %s)\n", xprt,
2004 xprt->address_strings[RPC_DISPLAY_PROTO],
2005 xprt->address_strings[RPC_DISPLAY_ADDR],
2006 xprt->address_strings[RPC_DISPLAY_PORT]);
2008 xs_udp_finish_connecting(xprt, sock);
2011 xprt_clear_connecting(xprt);
2020 static void xs_abort_connection(
struct sock_xprt *transport)
2025 dprintk(
"RPC: disconnecting xprt %p to reuse port\n", transport);
2031 memset(&any, 0,
sizeof(any));
2035 xs_sock_reset_connection_flags(&transport->
xprt);
2036 dprintk(
"RPC: AF_UNSPEC connect return code %d\n", result);
2039 static void xs_tcp_reuse_connection(
struct sock_xprt *transport)
2041 unsigned int state = transport->
inet->sk_state;
2047 if (transport->
inet->sk_shutdown == 0)
2049 dprintk(
"RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2050 __func__, transport->
inet->sk_shutdown);
2056 if (transport->
inet->sk_shutdown == 0)
2058 dprintk(
"RPC: %s: ESTABLISHED/SYN_SENT "
2059 "sk_shutdown set to %d\n",
2060 __func__, transport->
inet->sk_shutdown);
2062 xs_abort_connection(transport);
2065 static int xs_tcp_finish_connecting(
struct rpc_xprt *xprt,
struct socket *sock)
2070 if (!transport->
inet) {
2071 struct sock *sk = sock->
sk;
2075 xs_save_old_callbacks(transport, sk);
2086 tcp_sk(sk)->linger2 = 0;
2089 xprt_clear_connected(xprt);
2098 if (!xprt_bound(xprt))
2101 xs_set_memalloc(xprt);
2104 xprt->stat.connect_count++;
2105 xprt->stat.connect_start =
jiffies;
2111 xprt->connect_cookie++;
2127 static void xs_tcp_setup_socket(
struct work_struct *work)
2132 struct rpc_xprt *xprt = &transport->
xprt;
2138 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
2139 sock = xs_create_sock(xprt, transport,
2142 status = PTR_ERR(sock);
2151 xs_tcp_reuse_connection(transport);
2157 dprintk(
"RPC: worker connecting xprt %p via %s to "
2158 "%s (port %s)\n", xprt,
2159 xprt->address_strings[RPC_DISPLAY_PROTO],
2160 xprt->address_strings[RPC_DISPLAY_ADDR],
2161 xprt->address_strings[RPC_DISPLAY_PORT]);
2163 status = xs_tcp_finish_connecting(xprt, sock);
2164 dprintk(
"RPC: %p connect status %d connected %d sock state %d\n",
2165 xprt, -status, xprt_connected(xprt),
2166 sock->
sk->sk_state);
2169 printk(
"%s: connect returned unhandled error %d\n",
2175 xs_tcp_force_close(xprt);
2184 xprt_clear_connecting(xprt);
2196 xprt_clear_connecting(xprt);
2214 static void xs_connect(
struct rpc_task *task)
2216 struct rpc_xprt *xprt = task->tk_xprt;
2220 dprintk(
"RPC: xs_connect delayed xprt %p for %lu "
2222 xprt, xprt->reestablish_timeout /
HZ);
2225 xprt->reestablish_timeout);
2226 xprt->reestablish_timeout <<= 1;
2232 dprintk(
"RPC: xs_connect scheduled xprt %p\n", xprt);
2244 static void xs_local_print_stats(
struct rpc_xprt *xprt,
struct seq_file *
seq)
2248 if (xprt_connected(xprt))
2251 seq_printf(seq,
"\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2252 "%llu %llu %lu %llu %llu\n",
2253 xprt->stat.bind_count,
2254 xprt->stat.connect_count,
2255 xprt->stat.connect_time,
2259 xprt->stat.bad_xids,
2262 xprt->stat.max_slots,
2263 xprt->stat.sending_u,
2264 xprt->stat.pending_u);
2273 static void xs_udp_print_stats(
struct rpc_xprt *xprt,
struct seq_file *seq)
2277 seq_printf(seq,
"\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2280 xprt->stat.bind_count,
2283 xprt->stat.bad_xids,
2286 xprt->stat.max_slots,
2287 xprt->stat.sending_u,
2288 xprt->stat.pending_u);
2297 static void xs_tcp_print_stats(
struct rpc_xprt *xprt,
struct seq_file *seq)
2302 if (xprt_connected(xprt))
2305 seq_printf(seq,
"\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2306 "%llu %llu %lu %llu %llu\n",
2308 xprt->stat.bind_count,
2309 xprt->stat.connect_count,
2310 xprt->stat.connect_time,
2314 xprt->stat.bad_xids,
2317 xprt->stat.max_slots,
2318 xprt->stat.sending_u,
2319 xprt->stat.pending_u);
2327 static void *bc_malloc(
struct rpc_task *task,
size_t size)
2347 static void bc_free(
void *
buffer)
2362 static int bc_sendto(
struct rpc_rqst *req)
2365 struct xdr_buf *xbufp = &req->rq_snd_buf;
2366 struct rpc_xprt *xprt = req->rq_xprt;
2370 unsigned long headoff;
2371 unsigned long tailoff;
2373 xs_encode_stream_record_marker(xbufp);
2375 tailoff = (
unsigned long)xbufp->tail[0].iov_base & ~
PAGE_MASK;
2376 headoff = (
unsigned long)xbufp->head[0].iov_base & ~
PAGE_MASK;
2379 xbufp->tail[0].iov_base, tailoff);
2381 if (len != xbufp->len) {
2392 static int bc_send_request(
struct rpc_task *task)
2394 struct rpc_rqst *req = task->
tk_rqstp;
2399 dprintk(
"sending request with xid: %08x\n",
ntohl(req->rq_xid));
2403 xprt = req->rq_xprt->bc_xprt;
2419 len = bc_sendto(req);
2432 static void bc_close(
struct rpc_xprt *xprt)
2441 static void bc_destroy(
struct rpc_xprt *xprt)
2445 static struct rpc_xprt_ops xs_local_ops = {
2447 .release_xprt = xs_tcp_release_xprt,
2449 .rpcbind = xs_local_rpcbind,
2450 .set_port = xs_local_set_port,
2451 .connect = xs_connect,
2454 .send_request = xs_local_send_request,
2457 .destroy = xs_destroy,
2458 .print_stats = xs_local_print_stats,
2461 static struct rpc_xprt_ops xs_udp_ops = {
2462 .set_buffer_size = xs_udp_set_buffer_size,
2467 .set_port = xs_set_port,
2468 .connect = xs_connect,
2471 .send_request = xs_udp_send_request,
2473 .timer = xs_udp_timer,
2476 .destroy = xs_destroy,
2477 .print_stats = xs_udp_print_stats,
2480 static struct rpc_xprt_ops xs_tcp_ops = {
2482 .release_xprt = xs_tcp_release_xprt,
2485 .set_port = xs_set_port,
2486 .connect = xs_connect,
2489 .send_request = xs_tcp_send_request,
2491 .close = xs_tcp_close,
2492 .destroy = xs_destroy,
2493 .print_stats = xs_tcp_print_stats,
2500 static struct rpc_xprt_ops bc_tcp_ops = {
2504 .rpcbind = xs_local_rpcbind,
2505 .buf_alloc = bc_malloc,
2506 .buf_free = bc_free,
2507 .send_request = bc_send_request,
2510 .destroy = bc_destroy,
2511 .print_stats = xs_tcp_print_stats,
2514 static int xs_init_anyaddr(
const int family,
struct sockaddr *sap)
2529 memcpy(sap, &sin,
sizeof(sin));
2532 memcpy(sap, &sin6,
sizeof(sin6));
2535 dprintk(
"RPC: %s: Bad address family\n", __func__);
2541 static struct rpc_xprt *xs_setup_xprt(
struct xprt_create *
args,
2542 unsigned int slot_table_size,
2543 unsigned int max_slot_table_size)
2545 struct rpc_xprt *xprt;
2548 if (args->addrlen >
sizeof(xprt->addr)) {
2549 dprintk(
"RPC: xs_setup_xprt: address too large\n");
2550 return ERR_PTR(-
EBADF);
2553 xprt =
xprt_alloc(args->net,
sizeof(*
new), slot_table_size,
2554 max_slot_table_size);
2556 dprintk(
"RPC: xs_setup_xprt: couldn't allocate "
2562 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2563 xprt->addrlen = args->addrlen;
2565 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2568 err = xs_init_anyaddr(args->dstaddr->sa_family,
2572 return ERR_PTR(err);
2579 static const struct rpc_timeout xs_local_default_timeout = {
2580 .to_initval = 10 *
HZ,
2581 .to_maxval = 10 *
HZ,
2591 static struct rpc_xprt *xs_setup_local(
struct xprt_create *args)
2595 struct rpc_xprt *xprt;
2596 struct rpc_xprt *
ret;
2598 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2599 xprt_max_tcp_slot_table_entries);
2605 xprt->tsh_size =
sizeof(rpc_fraghdr) /
sizeof(
u32);
2606 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2612 xprt->ops = &xs_local_ops;
2613 xprt->timeout = &xs_local_default_timeout;
2618 dprintk(
"RPC: bad AF_LOCAL address: %s\n",
2623 xprt_set_bound(xprt);
2625 xs_local_setup_socket);
2626 xs_format_peer_addresses(xprt,
"local", RPCBIND_NETID_LOCAL);
2633 dprintk(
"RPC: set up xprt to %s via AF_LOCAL\n",
2634 xprt->address_strings[RPC_DISPLAY_ADDR]);
2644 static const struct rpc_timeout xs_udp_default_timeout = {
2645 .to_initval = 5 *
HZ,
2646 .to_maxval = 30 *
HZ,
2647 .to_increment = 5 *
HZ,
2656 static struct rpc_xprt *xs_setup_udp(
struct xprt_create *args)
2658 struct sockaddr *addr = args->dstaddr;
2659 struct rpc_xprt *xprt;
2661 struct rpc_xprt *
ret;
2663 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2664 xprt_udp_slot_table_entries);
2672 xprt->max_payload = (1
U << 16) - (
MAX_HEADER << 3);
2678 xprt->ops = &xs_udp_ops;
2680 xprt->timeout = &xs_udp_default_timeout;
2685 xprt_set_bound(xprt);
2688 xs_udp_setup_socket);
2689 xs_format_peer_addresses(xprt,
"udp", RPCBIND_NETID_UDP);
2693 xprt_set_bound(xprt);
2696 xs_udp_setup_socket);
2697 xs_format_peer_addresses(xprt,
"udp", RPCBIND_NETID_UDP6);
2704 if (xprt_bound(xprt))
2705 dprintk(
"RPC: set up xprt to %s (port %s) via %s\n",
2706 xprt->address_strings[RPC_DISPLAY_ADDR],
2707 xprt->address_strings[RPC_DISPLAY_PORT],
2708 xprt->address_strings[RPC_DISPLAY_PROTO]);
2710 dprintk(
"RPC: set up xprt to %s (autobind) via %s\n",
2711 xprt->address_strings[RPC_DISPLAY_ADDR],
2712 xprt->address_strings[RPC_DISPLAY_PROTO]);
2722 static const struct rpc_timeout xs_tcp_default_timeout = {
2723 .to_initval = 60 *
HZ,
2724 .to_maxval = 60 *
HZ,
2733 static struct rpc_xprt *xs_setup_tcp(
struct xprt_create *args)
2735 struct sockaddr *addr = args->dstaddr;
2736 struct rpc_xprt *xprt;
2738 struct rpc_xprt *
ret;
2740 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2741 xprt_max_tcp_slot_table_entries);
2747 xprt->tsh_size =
sizeof(rpc_fraghdr) /
sizeof(
u32);
2748 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2754 xprt->ops = &xs_tcp_ops;
2755 xprt->timeout = &xs_tcp_default_timeout;
2760 xprt_set_bound(xprt);
2763 xs_tcp_setup_socket);
2764 xs_format_peer_addresses(xprt,
"tcp", RPCBIND_NETID_TCP);
2768 xprt_set_bound(xprt);
2771 xs_tcp_setup_socket);
2772 xs_format_peer_addresses(xprt,
"tcp", RPCBIND_NETID_TCP6);
2779 if (xprt_bound(xprt))
2780 dprintk(
"RPC: set up xprt to %s (port %s) via %s\n",
2781 xprt->address_strings[RPC_DISPLAY_ADDR],
2782 xprt->address_strings[RPC_DISPLAY_PORT],
2783 xprt->address_strings[RPC_DISPLAY_PROTO]);
2785 dprintk(
"RPC: set up xprt to %s (autobind) via %s\n",
2786 xprt->address_strings[RPC_DISPLAY_ADDR],
2787 xprt->address_strings[RPC_DISPLAY_PROTO]);
2803 static struct rpc_xprt *xs_setup_bc_tcp(
struct xprt_create *args)
2805 struct sockaddr *addr = args->dstaddr;
2806 struct rpc_xprt *xprt;
2809 struct rpc_xprt *
ret;
2811 if (args->bc_xprt->xpt_bc_xprt) {
2818 return args->bc_xprt->xpt_bc_xprt;
2820 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2821 xprt_tcp_slot_table_entries);
2827 xprt->tsh_size =
sizeof(rpc_fraghdr) /
sizeof(
u32);
2828 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2829 xprt->timeout = &xs_tcp_default_timeout;
2832 xprt_set_bound(xprt);
2833 xprt->bind_timeout = 0;
2834 xprt->reestablish_timeout = 0;
2835 xprt->idle_timeout = 0;
2837 xprt->ops = &bc_tcp_ops;
2841 xs_format_peer_addresses(xprt,
"tcp",
2845 xs_format_peer_addresses(xprt,
"tcp",
2846 RPCBIND_NETID_TCP6);
2853 dprintk(
"RPC: set up xprt to %s (port %s) via %s\n",
2854 xprt->address_strings[RPC_DISPLAY_ADDR],
2855 xprt->address_strings[RPC_DISPLAY_PORT],
2856 xprt->address_strings[RPC_DISPLAY_PROTO]);
2866 args->bc_xprt->xpt_bc_xprt = xprt;
2867 xprt->bc_xprt = args->bc_xprt;
2876 xprt_set_connected(xprt);
2888 static struct xprt_class xs_local_transport = {
2890 .name =
"named UNIX socket",
2892 .ident = XPRT_TRANSPORT_LOCAL,
2893 .setup = xs_setup_local,
2896 static struct xprt_class xs_udp_transport = {
2900 .ident = XPRT_TRANSPORT_UDP,
2901 .setup = xs_setup_udp,
2904 static struct xprt_class xs_tcp_transport = {
2908 .ident = XPRT_TRANSPORT_TCP,
2909 .setup = xs_setup_tcp,
2912 static struct xprt_class xs_bc_tcp_transport = {
2914 .name =
"tcp NFSv4.1 backchannel",
2916 .ident = XPRT_TRANSPORT_BC_TCP,
2917 .setup = xs_setup_bc_tcp,
2927 if (!sunrpc_table_header)
2946 if (sunrpc_table_header) {
2948 sunrpc_table_header =
NULL;
2958 static int param_set_uint_minmax(
const char *
val,
2960 unsigned int min,
unsigned int max)
2968 if (ret == -
EINVAL || num < min || num > max)
2970 *((
unsigned int *)kp->
arg) = num;
2974 static int param_set_portnr(
const char *val,
const struct kernel_param *kp)
2976 return param_set_uint_minmax(val, kp,
2982 .set = param_set_portnr,
2986 #define param_check_portnr(name, p) \
2987 __param_check(name, p, unsigned int);
2992 static int param_set_slot_table_size(
const char *val,
2995 return param_set_uint_minmax(val, kp,
2997 RPC_MAX_SLOT_TABLE);
3001 .set = param_set_slot_table_size,
3005 #define param_check_slot_table_size(name, p) \
3006 __param_check(name, p, unsigned int);
3008 static int param_set_max_slot_table_size(
const char *val,
3011 return param_set_uint_minmax(val, kp,
3013 RPC_MAX_SLOT_TABLE_LIMIT);
3017 .set = param_set_max_slot_table_size,
3021 #define param_check_max_slot_table_size(name, p) \
3022 __param_check(name, p, unsigned int);
3025 slot_table_size, 0644);
3027 max_slot_table_size, 0644);
3029 slot_table_size, 0644);