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/xdr.h>
18 #include <linux/export.h>
22 * xdr_skb_read_bits - copy some data bits from skb to internal buffer
23 * @desc: sk_buff copy helper
24 * @to: copy destination
25 * @len: number of bytes to copy
27 * Possibly called several times to iterate over an sk_buff and copy
31 xdr_skb_read_bits(struct xdr_skb_reader
*desc
, void *to
, size_t len
)
33 if (len
> desc
->count
)
35 if (unlikely(skb_copy_bits(desc
->skb
, desc
->offset
, to
, len
)))
43 * xdr_skb_read_and_csum_bits - copy and checksum from skb to buffer
44 * @desc: sk_buff copy helper
45 * @to: copy destination
46 * @len: number of bytes to copy
48 * Same as skb_read_bits, but calculate a checksum at the same time.
50 static size_t xdr_skb_read_and_csum_bits(struct xdr_skb_reader
*desc
, void *to
, size_t len
)
55 if (len
> desc
->count
)
58 csum2
= skb_copy_and_csum_bits(desc
->skb
, pos
, to
, len
, 0);
59 desc
->csum
= csum_block_add(desc
->csum
, csum2
, pos
);
66 * xdr_partial_copy_from_skb - copy data out of an skb
67 * @xdr: target XDR buffer
68 * @base: starting offset
69 * @desc: sk_buff copy helper
70 * @copy_actor: virtual method for copying data
74 xdr_partial_copy_from_skb(struct xdr_buf
*xdr
, unsigned int base
, struct xdr_skb_reader
*desc
, xdr_skb_read_actor copy_actor
)
76 struct page
**ppage
= xdr
->pages
;
77 unsigned int len
, pglen
= xdr
->page_len
;
81 len
= xdr
->head
[0].iov_len
;
84 ret
= copy_actor(desc
, (char *)xdr
->head
[0].iov_base
+ base
, len
);
86 if (ret
!= len
|| !desc
->count
)
92 if (unlikely(pglen
== 0))
94 if (unlikely(base
>= pglen
)) {
98 if (base
|| xdr
->page_base
) {
100 base
+= xdr
->page_base
;
101 ppage
+= base
>> PAGE_SHIFT
;
107 /* ACL likes to be lazy in allocating pages - ACLs
108 * are small by default but can get huge. */
109 if ((xdr
->flags
& XDRBUF_SPARSE_PAGES
) && *ppage
== NULL
) {
110 *ppage
= alloc_page(GFP_NOWAIT
| __GFP_NOWARN
);
111 if (unlikely(*ppage
== NULL
)) {
119 kaddr
= kmap_atomic(*ppage
);
124 ret
= copy_actor(desc
, kaddr
+ base
, len
);
129 ret
= copy_actor(desc
, kaddr
, len
);
131 flush_dcache_page(*ppage
);
132 kunmap_atomic(kaddr
);
134 if (ret
!= len
|| !desc
->count
)
137 } while ((pglen
-= len
) != 0);
139 len
= xdr
->tail
[0].iov_len
;
141 copied
+= copy_actor(desc
, (char *)xdr
->tail
[0].iov_base
+ base
, len
- base
);
147 * csum_partial_copy_to_xdr - checksum and copy data
148 * @xdr: target XDR buffer
151 * We have set things up such that we perform the checksum of the UDP
152 * packet in parallel with the copies into the RPC client iovec. -DaveM
154 int csum_partial_copy_to_xdr(struct xdr_buf
*xdr
, struct sk_buff
*skb
)
156 struct xdr_skb_reader desc
;
160 desc
.count
= skb
->len
- desc
.offset
;
162 if (skb_csum_unnecessary(skb
))
165 desc
.csum
= csum_partial(skb
->data
, desc
.offset
, skb
->csum
);
166 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_and_csum_bits
) < 0)
168 if (desc
.offset
!= skb
->len
) {
170 csum2
= skb_checksum(skb
, desc
.offset
, skb
->len
- desc
.offset
, 0);
171 desc
.csum
= csum_block_add(desc
.csum
, csum2
, desc
.offset
);
175 if (csum_fold(desc
.csum
))
177 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
178 !skb
->csum_complete_sw
)
179 netdev_rx_csum_fault(skb
->dev
, skb
);
182 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_bits
) < 0)
188 EXPORT_SYMBOL_GPL(csum_partial_copy_to_xdr
);