2 * linux/net/sunrpc/socklib.c
4 * Common socket helper routines for RPC client and server
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/compiler.h>
10 #include <linux/netdevice.h>
11 #include <linux/gfp.h>
12 #include <linux/skbuff.h>
13 #include <linux/types.h>
14 #include <linux/pagemap.h>
15 #include <linux/udp.h>
16 #include <linux/sunrpc/xdr.h>
17 #include <linux/export.h>
21 * xdr_skb_read_bits - copy some data bits from skb to internal buffer
22 * @desc: sk_buff copy helper
23 * @to: copy destination
24 * @len: number of bytes to copy
26 * Possibly called several times to iterate over an sk_buff and copy
30 xdr_skb_read_bits(struct xdr_skb_reader
*desc
, void *to
, size_t len
)
32 if (len
> desc
->count
)
34 if (unlikely(skb_copy_bits(desc
->skb
, desc
->offset
, to
, len
)))
42 * xdr_skb_read_and_csum_bits - copy and checksum from skb to buffer
43 * @desc: sk_buff copy helper
44 * @to: copy destination
45 * @len: number of bytes to copy
47 * Same as skb_read_bits, but calculate a checksum at the same time.
49 static size_t xdr_skb_read_and_csum_bits(struct xdr_skb_reader
*desc
, void *to
, size_t len
)
54 if (len
> desc
->count
)
57 csum2
= skb_copy_and_csum_bits(desc
->skb
, pos
, to
, len
, 0);
58 desc
->csum
= csum_block_add(desc
->csum
, csum2
, pos
);
65 * xdr_partial_copy_from_skb - copy data out of an skb
66 * @xdr: target XDR buffer
67 * @base: starting offset
68 * @desc: sk_buff copy helper
69 * @copy_actor: virtual method for copying data
73 xdr_partial_copy_from_skb(struct xdr_buf
*xdr
, unsigned int base
, struct xdr_skb_reader
*desc
, xdr_skb_read_actor copy_actor
)
75 struct page
**ppage
= xdr
->pages
;
76 unsigned int len
, pglen
= xdr
->page_len
;
80 len
= xdr
->head
[0].iov_len
;
83 ret
= copy_actor(desc
, (char *)xdr
->head
[0].iov_base
+ base
, len
);
85 if (ret
!= len
|| !desc
->count
)
91 if (unlikely(pglen
== 0))
93 if (unlikely(base
>= pglen
)) {
97 if (base
|| xdr
->page_base
) {
99 base
+= xdr
->page_base
;
100 ppage
+= base
>> PAGE_SHIFT
;
106 /* ACL likes to be lazy in allocating pages - ACLs
107 * are small by default but can get huge. */
108 if ((xdr
->flags
& XDRBUF_SPARSE_PAGES
) && *ppage
== NULL
) {
109 *ppage
= alloc_page(GFP_NOWAIT
| __GFP_NOWARN
);
110 if (unlikely(*ppage
== NULL
)) {
118 kaddr
= kmap_atomic(*ppage
);
123 ret
= copy_actor(desc
, kaddr
+ base
, len
);
128 ret
= copy_actor(desc
, kaddr
, len
);
130 flush_dcache_page(*ppage
);
131 kunmap_atomic(kaddr
);
133 if (ret
!= len
|| !desc
->count
)
136 } while ((pglen
-= len
) != 0);
138 len
= xdr
->tail
[0].iov_len
;
140 copied
+= copy_actor(desc
, (char *)xdr
->tail
[0].iov_base
+ base
, len
- base
);
146 * csum_partial_copy_to_xdr - checksum and copy data
147 * @xdr: target XDR buffer
150 * We have set things up such that we perform the checksum of the UDP
151 * packet in parallel with the copies into the RPC client iovec. -DaveM
153 int csum_partial_copy_to_xdr(struct xdr_buf
*xdr
, struct sk_buff
*skb
)
155 struct xdr_skb_reader desc
;
159 desc
.count
= skb
->len
- desc
.offset
;
161 if (skb_csum_unnecessary(skb
))
164 desc
.csum
= csum_partial(skb
->data
, desc
.offset
, skb
->csum
);
165 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_and_csum_bits
) < 0)
167 if (desc
.offset
!= skb
->len
) {
169 csum2
= skb_checksum(skb
, desc
.offset
, skb
->len
- desc
.offset
, 0);
170 desc
.csum
= csum_block_add(desc
.csum
, csum2
, desc
.offset
);
174 if (csum_fold(desc
.csum
))
176 if (unlikely(skb
->ip_summed
== CHECKSUM_COMPLETE
) &&
177 !skb
->csum_complete_sw
)
178 netdev_rx_csum_fault(skb
->dev
, skb
);
181 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_bits
) < 0)
187 EXPORT_SYMBOL_GPL(csum_partial_copy_to_xdr
);