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/sched.h>
19 #include <linux/sunrpc/xdr.h>
20 #include <linux/export.h>
25 * Helper structure for copying from an sk_buff.
27 struct xdr_skb_reader {
34 typedef size_t (*xdr_skb_read_actor)(struct xdr_skb_reader *desc, void *to,
38 * xdr_skb_read_bits - copy some data bits from skb to internal buffer
39 * @desc: sk_buff copy helper
40 * @to: copy destination
41 * @len: number of bytes to copy
43 * Possibly called several times to iterate over an sk_buff and copy
47 xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len)
49 if (len > desc->count)
51 if (unlikely(skb_copy_bits(desc->skb, desc->offset, to, len)))
59 * xdr_skb_read_and_csum_bits - copy and checksum from skb to buffer
60 * @desc: sk_buff copy helper
61 * @to: copy destination
62 * @len: number of bytes to copy
64 * Same as skb_read_bits, but calculate a checksum at the same time.
66 static size_t xdr_skb_read_and_csum_bits(struct xdr_skb_reader *desc, void *to, size_t len)
71 if (len > desc->count)
74 csum2 = skb_copy_and_csum_bits(desc->skb, pos, to, len);
75 desc->csum = csum_block_add(desc->csum, csum2, pos);
82 * xdr_partial_copy_from_skb - copy data out of an skb
83 * @xdr: target XDR buffer
84 * @base: starting offset
85 * @desc: sk_buff copy helper
86 * @copy_actor: virtual method for copying data
90 xdr_partial_copy_from_skb(struct xdr_buf *xdr, unsigned int base, struct xdr_skb_reader *desc, xdr_skb_read_actor copy_actor)
92 struct page **ppage = xdr->pages;
93 unsigned int len, pglen = xdr->page_len;
97 len = xdr->head[0].iov_len;
100 ret = copy_actor(desc, (char *)xdr->head[0].iov_base + base, len);
102 if (ret != len || !desc->count)
108 if (unlikely(pglen == 0))
110 if (unlikely(base >= pglen)) {
114 if (base || xdr->page_base) {
116 base += xdr->page_base;
117 ppage += base >> PAGE_SHIFT;
123 /* ACL likes to be lazy in allocating pages - ACLs
124 * are small by default but can get huge. */
125 if ((xdr->flags & XDRBUF_SPARSE_PAGES) && *ppage == NULL) {
126 *ppage = alloc_page(GFP_NOWAIT | __GFP_NOWARN);
127 if (unlikely(*ppage == NULL)) {
135 kaddr = kmap_atomic(*ppage);
140 ret = copy_actor(desc, kaddr + base, len);
145 ret = copy_actor(desc, kaddr, len);
147 flush_dcache_page(*ppage);
148 kunmap_atomic(kaddr);
150 if (ret != len || !desc->count)
153 } while ((pglen -= len) != 0);
155 len = xdr->tail[0].iov_len;
157 copied += copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base);
163 * csum_partial_copy_to_xdr - checksum and copy data
164 * @xdr: target XDR buffer
167 * We have set things up such that we perform the checksum of the UDP
168 * packet in parallel with the copies into the RPC client iovec. -DaveM
170 int csum_partial_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
172 struct xdr_skb_reader desc;
176 desc.count = skb->len - desc.offset;
178 if (skb_csum_unnecessary(skb))
181 desc.csum = csum_partial(skb->data, desc.offset, skb->csum);
182 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_and_csum_bits) < 0)
184 if (desc.offset != skb->len) {
186 csum2 = skb_checksum(skb, desc.offset, skb->len - desc.offset, 0);
187 desc.csum = csum_block_add(desc.csum, csum2, desc.offset);
191 if (csum_fold(desc.csum))
193 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
194 !skb->csum_complete_sw)
195 netdev_rx_csum_fault(skb->dev, skb);
198 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
204 EXPORT_SYMBOL_GPL(csum_partial_copy_to_xdr);
206 static inline int xprt_sendmsg(struct socket *sock, struct msghdr *msg,
210 iov_iter_advance(&msg->msg_iter, seek);
211 return sock_sendmsg(sock, msg);
214 static int xprt_send_kvec(struct socket *sock, struct msghdr *msg,
215 struct kvec *vec, size_t seek)
217 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, 1, vec->iov_len);
218 return xprt_sendmsg(sock, msg, seek);
221 static int xprt_send_pagedata(struct socket *sock, struct msghdr *msg,
222 struct xdr_buf *xdr, size_t base)
224 iov_iter_bvec(&msg->msg_iter, ITER_SOURCE, xdr->bvec, xdr_buf_pagecount(xdr),
225 xdr->page_len + xdr->page_base);
226 return xprt_sendmsg(sock, msg, base + xdr->page_base);
231 * - sending from byte 0 of the message
232 * - the message is wholly contained in @xdr's head iovec
234 static int xprt_send_rm_and_kvec(struct socket *sock, struct msghdr *msg,
235 rpc_fraghdr marker, struct kvec *vec,
238 struct kvec iov[2] = {
241 .iov_len = sizeof(marker)
245 size_t len = iov[0].iov_len + iov[1].iov_len;
247 iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, iov, 2, len);
248 return xprt_sendmsg(sock, msg, base);
252 * xprt_sock_sendmsg - write an xdr_buf directly to a socket
253 * @sock: open socket to send on
254 * @msg: socket message metadata
255 * @xdr: xdr_buf containing this request
256 * @base: starting position in the buffer
257 * @marker: stream record marker field
258 * @sent_p: return the total number of bytes successfully queued for sending
261 * On success, returns zero and fills in @sent_p.
262 * %-ENOTSOCK if @sock is not a struct socket.
264 int xprt_sock_sendmsg(struct socket *sock, struct msghdr *msg,
265 struct xdr_buf *xdr, unsigned int base,
266 rpc_fraghdr marker, unsigned int *sent_p)
268 unsigned int rmsize = marker ? sizeof(marker) : 0;
269 unsigned int remainder = rmsize + xdr->len - base;
278 msg->msg_flags |= MSG_MORE;
279 want = xdr->head[0].iov_len + rmsize;
281 unsigned int len = want - base;
285 msg->msg_flags &= ~MSG_MORE;
287 err = xprt_send_rm_and_kvec(sock, msg, marker,
288 &xdr->head[0], base);
290 err = xprt_send_kvec(sock, msg, &xdr->head[0], base);
291 if (remainder == 0 || err != len)
299 if (base < xdr->page_len) {
300 unsigned int len = xdr->page_len - base;
304 msg->msg_flags &= ~MSG_MORE;
305 err = xprt_send_pagedata(sock, msg, xdr, base);
306 if (remainder == 0 || err != len)
311 base -= xdr->page_len;
314 if (base >= xdr->tail[0].iov_len)
316 msg->msg_flags &= ~MSG_MORE;
317 err = xprt_send_kvec(sock, msg, &xdr->tail[0], base);