blob: 7e55cfc69697dda67de2f2d0f9dc23026d85b436 [file] [log] [blame]
Chuck Lever094bb202005-08-11 16:25:20 -04001/*
2 * linux/net/sunrpc/socklib.c
3 *
4 * Common socket helper routines for RPC client and server
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
Herbert Xufb286bb2005-11-10 13:01:24 -08009#include <linux/compiler.h>
10#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/gfp.h>
Herbert Xufb286bb2005-11-10 13:01:24 -080012#include <linux/skbuff.h>
Chuck Lever094bb202005-08-11 16:25:20 -040013#include <linux/types.h>
14#include <linux/pagemap.h>
15#include <linux/udp.h>
16#include <linux/sunrpc/xdr.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040017#include <linux/export.h>
Chuck Lever094bb202005-08-11 16:25:20 -040018
19
20/**
Chuck Lever9d292312006-12-05 16:35:41 -050021 * xdr_skb_read_bits - copy some data bits from skb to internal buffer
Chuck Lever094bb202005-08-11 16:25:20 -040022 * @desc: sk_buff copy helper
23 * @to: copy destination
24 * @len: number of bytes to copy
25 *
26 * Possibly called several times to iterate over an sk_buff and copy
27 * data out of it.
28 */
Trond Myklebust550aebf2018-09-14 14:32:45 -040029static size_t
30xdr_skb_read_bits(struct xdr_skb_reader *desc, void *to, size_t len)
Chuck Lever094bb202005-08-11 16:25:20 -040031{
32 if (len > desc->count)
33 len = desc->count;
Chuck Lever9d292312006-12-05 16:35:41 -050034 if (unlikely(skb_copy_bits(desc->skb, desc->offset, to, len)))
Chuck Lever094bb202005-08-11 16:25:20 -040035 return 0;
36 desc->count -= len;
37 desc->offset += len;
38 return len;
39}
40
41/**
Chuck Lever9d292312006-12-05 16:35:41 -050042 * xdr_skb_read_and_csum_bits - copy and checksum from skb to buffer
Chuck Lever094bb202005-08-11 16:25:20 -040043 * @desc: sk_buff copy helper
44 * @to: copy destination
45 * @len: number of bytes to copy
46 *
47 * Same as skb_read_bits, but calculate a checksum at the same time.
48 */
Chuck Leverdd456472006-12-05 16:35:44 -050049static size_t xdr_skb_read_and_csum_bits(struct xdr_skb_reader *desc, void *to, size_t len)
Chuck Lever094bb202005-08-11 16:25:20 -040050{
Al Viro5f92a732006-11-14 21:36:54 -080051 unsigned int pos;
52 __wsum csum2;
Chuck Lever094bb202005-08-11 16:25:20 -040053
54 if (len > desc->count)
55 len = desc->count;
56 pos = desc->offset;
57 csum2 = skb_copy_and_csum_bits(desc->skb, pos, to, len, 0);
58 desc->csum = csum_block_add(desc->csum, csum2, pos);
59 desc->count -= len;
60 desc->offset += len;
61 return len;
62}
63
64/**
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
70 *
71 */
Trond Myklebustec846462018-09-14 14:38:05 -040072static ssize_t
73xdr_partial_copy_from_skb(struct xdr_buf *xdr, unsigned int base, struct xdr_skb_reader *desc, xdr_skb_read_actor copy_actor)
Chuck Lever094bb202005-08-11 16:25:20 -040074{
75 struct page **ppage = xdr->pages;
76 unsigned int len, pglen = xdr->page_len;
77 ssize_t copied = 0;
Chuck Lever322e2ef2007-10-26 13:30:59 -040078 size_t ret;
Chuck Lever094bb202005-08-11 16:25:20 -040079
80 len = xdr->head[0].iov_len;
81 if (base < len) {
82 len -= base;
83 ret = copy_actor(desc, (char *)xdr->head[0].iov_base + base, len);
84 copied += ret;
85 if (ret != len || !desc->count)
86 goto out;
87 base = 0;
88 } else
89 base -= len;
90
91 if (unlikely(pglen == 0))
92 goto copy_tail;
93 if (unlikely(base >= pglen)) {
94 base -= pglen;
95 goto copy_tail;
96 }
97 if (base || xdr->page_base) {
98 pglen -= base;
99 base += xdr->page_base;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300100 ppage += base >> PAGE_SHIFT;
101 base &= ~PAGE_MASK;
Chuck Lever094bb202005-08-11 16:25:20 -0400102 }
103 do {
104 char *kaddr;
105
106 /* ACL likes to be lazy in allocating pages - ACLs
107 * are small by default but can get huge. */
Trond Myklebust431f6eb2018-09-16 00:08:20 -0400108 if ((xdr->flags & XDRBUF_SPARSE_PAGES) && *ppage == NULL) {
Chuck Lever094bb202005-08-11 16:25:20 -0400109 *ppage = alloc_page(GFP_ATOMIC);
110 if (unlikely(*ppage == NULL)) {
111 if (copied == 0)
112 copied = -ENOMEM;
113 goto out;
114 }
115 }
116
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300117 len = PAGE_SIZE;
Cong Wangb8541782011-11-25 23:14:40 +0800118 kaddr = kmap_atomic(*ppage);
Chuck Lever094bb202005-08-11 16:25:20 -0400119 if (base) {
120 len -= base;
121 if (pglen < len)
122 len = pglen;
123 ret = copy_actor(desc, kaddr + base, len);
124 base = 0;
125 } else {
126 if (pglen < len)
127 len = pglen;
128 ret = copy_actor(desc, kaddr, len);
129 }
130 flush_dcache_page(*ppage);
Cong Wangb8541782011-11-25 23:14:40 +0800131 kunmap_atomic(kaddr);
Chuck Lever094bb202005-08-11 16:25:20 -0400132 copied += ret;
133 if (ret != len || !desc->count)
134 goto out;
135 ppage++;
136 } while ((pglen -= len) != 0);
137copy_tail:
138 len = xdr->tail[0].iov_len;
139 if (base < len)
140 copied += copy_actor(desc, (char *)xdr->tail[0].iov_base + base, len - base);
141out:
142 return copied;
143}
144
145/**
146 * csum_partial_copy_to_xdr - checksum and copy data
147 * @xdr: target XDR buffer
148 * @skb: source skb
149 *
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
152 */
153int csum_partial_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
154{
Chuck Leverdd456472006-12-05 16:35:44 -0500155 struct xdr_skb_reader desc;
Chuck Lever094bb202005-08-11 16:25:20 -0400156
157 desc.skb = skb;
Willem de Bruijn1da8c6812016-04-07 11:44:58 -0400158 desc.offset = 0;
Chuck Lever094bb202005-08-11 16:25:20 -0400159 desc.count = skb->len - desc.offset;
160
Herbert Xu60476372007-04-09 11:59:39 -0700161 if (skb_csum_unnecessary(skb))
Chuck Lever094bb202005-08-11 16:25:20 -0400162 goto no_checksum;
163
164 desc.csum = csum_partial(skb->data, desc.offset, skb->csum);
Chuck Lever9d292312006-12-05 16:35:41 -0500165 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_and_csum_bits) < 0)
Chuck Lever094bb202005-08-11 16:25:20 -0400166 return -1;
167 if (desc.offset != skb->len) {
Al Viro5f92a732006-11-14 21:36:54 -0800168 __wsum csum2;
Chuck Lever094bb202005-08-11 16:25:20 -0400169 csum2 = skb_checksum(skb, desc.offset, skb->len - desc.offset, 0);
170 desc.csum = csum_block_add(desc.csum, csum2, desc.offset);
171 }
172 if (desc.count)
173 return -1;
Al Virod3bc23e2006-11-14 21:24:49 -0800174 if (csum_fold(desc.csum))
Chuck Lever094bb202005-08-11 16:25:20 -0400175 return -1;
Tom Herbert7e3cead2014-06-10 18:54:19 -0700176 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
177 !skb->csum_complete_sw)
Cong Wang7fe50ac2018-11-12 14:47:18 -0800178 netdev_rx_csum_fault(skb->dev, skb);
Chuck Lever094bb202005-08-11 16:25:20 -0400179 return 0;
180no_checksum:
Chuck Lever9d292312006-12-05 16:35:41 -0500181 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
Chuck Lever094bb202005-08-11 16:25:20 -0400182 return -1;
183 if (desc.count)
184 return -1;
185 return 0;
186}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400187EXPORT_SYMBOL_GPL(csum_partial_copy_to_xdr);