1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/socklib.c
5 * Common socket helper routines for RPC client and server
7 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
10 #include <linux/compiler.h>
11 #include <linux/netdevice.h>
12 #include <linux/gfp.h>
13 #include <linux/skbuff.h>
14 #include <linux/types.h>
15 #include <linux/pagemap.h>
16 #include <linux/udp.h>
17 #include <linux/sunrpc/msg_prot.h>
18 #include <linux/sunrpc/xdr.h>
19 #include <linux/export.h>
24 * Helper structure for copying from an sk_buff.
26 struct xdr_skb_reader {
33 typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to,
37 * xdr_skb_read_bits - copy some data bits from skb to internal buffer
38 * @desc: sk_buff copy helper
39 * @to: copy destination
40 * @len: number of bytes to copy
42 * Possibly called several times to iterate over an sk_buff and copy
46 xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len)
48 if (len > desc->count)
50 if (unlikely(skb_copy_bits(desc->skb, desc->offset, to, len)))
58 * xdr_skb_read_and_csum_bits - copy and checksum from skb to buffer
59 * @desc: sk_buff copy helper
60 * @to: copy destination
61 * @len: number of bytes to copy
63 * Same as skb_read_bits, but calculate a checksum at the same time.
65 static size_t xdr_skb_read_and_csum_bits(struct xdr_skb_reader *desc, void *to, size_t len)
70 if (len > desc->count)
73 csum2 = skb_copy_and_csum_bits(desc->skb, pos, to, len);
74 desc->csum = csum_block_add(desc->csum, csum2, pos);
81 * xdr_partial_copy_from_skb - copy data out of an skb
82 * @xdr: target XDR buffer
83 * @base: starting offset
84 * @desc: sk_buff copy helper
85 * @copy_actor: virtual method for copying data
89 xdr_partial_copy_from_skb(struct xdr_buf *xdr, unsigned int base, struct xdr_skb_reader *desc, xdr_skb_read_actor copy_actor)
91 struct page **ppage = xdr->pages;
92 unsigned int len, pglen = xdr->page_len;
96 len = xdr->head[0].iov_len;
99 ret = copy_actor(desc, (char *)xdr->head[0].iov_base + base, len);
101 if (ret != len || !desc->count)
107 if (unlikely(pglen == 0))
109 if (unlikely(base >= pglen)) {
113 if (base || xdr->page_base) {
115 base += xdr->page_base;
116 ppage += base >> PAGE_SHIFT;
122 /* ACL likes to be lazy in allocating pages - ACLs
123 * are small by default but can get huge. */
124 if ((xdr->flags & XDRBUF_SPARSE_PAGES) && *ppage == NULL) {
125 *ppage = alloc_page(GFP_NOWAIT | __GFP_NOWARN);
126 if (unlikely(*ppage == NULL)) {
134 kaddr = kmap_atomic(*ppage);
139 ret = copy_actor(desc, kaddr + base, len);
144 ret = copy_actor(desc, kaddr, len);
146 flush_dcache_page(*ppage);
147 kunmap_atomic(kaddr);
149 if (ret != len || !desc->count)
152 } while ((pglen -= len) != 0);
154 len = xdr->tail[0].iov_len;
156 copied += copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base);
162 * csum_partial_copy_to_xdr - checksum and copy data
163 * @xdr: target XDR buffer
166 * We have set things up such that we perform the checksum of the UDP
167 * packet in parallel with the copies into the RPC client iovec. -DaveM
169 int csum_partial_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
171 struct xdr_skb_reader desc;
175 desc.count = skb->len - desc.offset;
177 if (skb_csum_unnecessary(skb))
180 desc.csum = csum_partial(skb->data, desc.offset, skb->csum);
181 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_and_csum_bits) < 0)
183 if (desc.offset != skb->len) {
185 csum2 = skb_checksum(skb, desc.offset, skb->len - desc.offset, 0);
186 desc.csum = csum_block_add(desc.csum, csum2, desc.offset);
190 if (csum_fold(desc.csum))
192 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
193 !skb->csum_complete_sw)
194 netdev_rx_csum_fault(skb->dev, skb);
197 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
203 EXPORT_SYMBOL_GPL(csum_partial_copy_to_xdr);
205 static inline int xprt_sendmsg(struct socket *sock, struct msghdr *msg,
209 iov_iter_advance(&msg->msg_iter, seek);
210 return sock_sendmsg(sock, msg);
213 static int xprt_send_kvec(struct socket *sock, struct msghdr *msg,
214 struct kvec *vec, size_t seek)
216 iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len);
217 return xprt_sendmsg(sock, msg, seek);
220 static int xprt_send_pagedata(struct socket *sock, struct msghdr *msg,
221 struct xdr_buf *xdr, size_t base)
225 err = xdr_alloc_bvec(xdr, GFP_KERNEL);
229 iov_iter_bvec(&msg->msg_iter, WRITE, xdr->bvec, xdr_buf_pagecount(xdr),
230 xdr->page_len + xdr->page_base);
231 return xprt_sendmsg(sock, msg, base + xdr->page_base);
236 * - sending from byte 0 of the message
237 * - the message is wholly contained in @xdr's head iovec
239 static int xprt_send_rm_and_kvec(struct socket *sock, struct msghdr *msg,
240 rpc_fraghdr marker, struct kvec *vec,
243 struct kvec iov[2] = {
246 .iov_len = sizeof(marker)
250 size_t len = iov[0].iov_len + iov[1].iov_len;
252 iov_iter_kvec(&msg->msg_iter, WRITE, iov, 2, len);
253 return xprt_sendmsg(sock, msg, base);
257 * xprt_sock_sendmsg - write an xdr_buf directly to a socket
258 * @sock: open socket to send on
259 * @msg: socket message metadata
260 * @xdr: xdr_buf containing this request
261 * @base: starting position in the buffer
262 * @marker: stream record marker field
263 * @sent_p: return the total number of bytes successfully queued for sending
266 * On success, returns zero and fills in @sent_p.
267 * %-ENOTSOCK if @sock is not a struct socket.
269 int xprt_sock_sendmsg(struct socket *sock, struct msghdr *msg,
270 struct xdr_buf *xdr, unsigned int base,
271 rpc_fraghdr marker, unsigned int *sent_p)
273 unsigned int rmsize = marker ? sizeof(marker) : 0;
274 unsigned int remainder = rmsize + xdr->len - base;
283 msg->msg_flags |= MSG_MORE;
284 want = xdr->head[0].iov_len + rmsize;
286 unsigned int len = want - base;
290 msg->msg_flags &= ~MSG_MORE;
292 err = xprt_send_rm_and_kvec(sock, msg, marker,
293 &xdr->head[0], base);
295 err = xprt_send_kvec(sock, msg, &xdr->head[0], base);
296 if (remainder == 0 || err != len)
304 if (base < xdr->page_len) {
305 unsigned int len = xdr->page_len - base;
309 msg->msg_flags &= ~MSG_MORE;
310 err = xprt_send_pagedata(sock, msg, xdr, base);
311 if (remainder == 0 || err != len)
316 base -= xdr->page_len;
319 if (base >= xdr->tail[0].iov_len)
321 msg->msg_flags &= ~MSG_MORE;
322 err = xprt_send_kvec(sock, msg, &xdr->tail[0], base);