1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <net/tcp.h>
36
37#include "rds.h"
38#include "tcp.h"
39
40static struct kmem_cache *rds_tcp_incoming_slab;
41
42static void rds_tcp_inc_purge(struct rds_incoming *inc)
43{
44 struct rds_tcp_incoming *tinc;
45 tinc = container_of(inc, struct rds_tcp_incoming, ti_inc);
46 rdsdebug("purging tinc %p inc %p\n", tinc, inc);
47 skb_queue_purge(&tinc->ti_skb_list);
48}
49
50void rds_tcp_inc_free(struct rds_incoming *inc)
51{
52 struct rds_tcp_incoming *tinc;
53 tinc = container_of(inc, struct rds_tcp_incoming, ti_inc);
54 rds_tcp_inc_purge(inc);
55 rdsdebug("freeing tinc %p inc %p\n", tinc, inc);
56 kmem_cache_free(rds_tcp_incoming_slab, tinc);
57}
58
59
60
61
62int rds_tcp_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
63{
64 struct rds_tcp_incoming *tinc;
65 struct sk_buff *skb;
66 int ret = 0;
67
68 if (!iov_iter_count(to))
69 goto out;
70
71 tinc = container_of(inc, struct rds_tcp_incoming, ti_inc);
72
73 skb_queue_walk(&tinc->ti_skb_list, skb) {
74 unsigned long to_copy, skb_off;
75 for (skb_off = 0; skb_off < skb->len; skb_off += to_copy) {
76 to_copy = iov_iter_count(to);
77 to_copy = min(to_copy, skb->len - skb_off);
78
79 if (skb_copy_datagram_iter(skb, skb_off, to, to_copy))
80 return -EFAULT;
81
82 rds_stats_add(s_copy_to_user, to_copy);
83 ret += to_copy;
84
85 if (!iov_iter_count(to))
86 goto out;
87 }
88 }
89out:
90 return ret;
91}
92
93
94
95
96
97
98
99
100
101
102
103
104
105static void rds_tcp_cong_recv(struct rds_connection *conn,
106 struct rds_tcp_incoming *tinc)
107{
108 struct sk_buff *skb;
109 unsigned int to_copy, skb_off;
110 unsigned int map_off;
111 unsigned int map_page;
112 struct rds_cong_map *map;
113 int ret;
114
115
116 if (be32_to_cpu(tinc->ti_inc.i_hdr.h_len) != RDS_CONG_MAP_BYTES)
117 return;
118
119 map_page = 0;
120 map_off = 0;
121 map = conn->c_fcong;
122
123 skb_queue_walk(&tinc->ti_skb_list, skb) {
124 skb_off = 0;
125 while (skb_off < skb->len) {
126 to_copy = min_t(unsigned int, PAGE_SIZE - map_off,
127 skb->len - skb_off);
128
129 BUG_ON(map_page >= RDS_CONG_MAP_PAGES);
130
131
132 ret = skb_copy_bits(skb, skb_off,
133 (void *)map->m_page_addrs[map_page] + map_off,
134 to_copy);
135 BUG_ON(ret != 0);
136
137 skb_off += to_copy;
138 map_off += to_copy;
139 if (map_off == PAGE_SIZE) {
140 map_off = 0;
141 map_page++;
142 }
143 }
144 }
145
146 rds_cong_map_updated(map, ~(u64) 0);
147}
148
149struct rds_tcp_desc_arg {
150 struct rds_connection *conn;
151 gfp_t gfp;
152};
153
154static int rds_tcp_data_recv(read_descriptor_t *desc, struct sk_buff *skb,
155 unsigned int offset, size_t len)
156{
157 struct rds_tcp_desc_arg *arg = desc->arg.data;
158 struct rds_connection *conn = arg->conn;
159 struct rds_tcp_connection *tc = conn->c_transport_data;
160 struct rds_tcp_incoming *tinc = tc->t_tinc;
161 struct sk_buff *clone;
162 size_t left = len, to_copy;
163
164 rdsdebug("tcp data tc %p skb %p offset %u len %zu\n", tc, skb, offset,
165 len);
166
167
168
169
170
171 while (left) {
172 if (!tinc) {
173 tinc = kmem_cache_alloc(rds_tcp_incoming_slab,
174 arg->gfp);
175 if (!tinc) {
176 desc->error = -ENOMEM;
177 goto out;
178 }
179 tc->t_tinc = tinc;
180 rdsdebug("alloced tinc %p\n", tinc);
181 rds_inc_init(&tinc->ti_inc, conn, conn->c_faddr);
182
183
184
185
186 skb_queue_head_init(&tinc->ti_skb_list);
187 }
188
189 if (left && tc->t_tinc_hdr_rem) {
190 to_copy = min(tc->t_tinc_hdr_rem, left);
191 rdsdebug("copying %zu header from skb %p\n", to_copy,
192 skb);
193 skb_copy_bits(skb, offset,
194 (char *)&tinc->ti_inc.i_hdr +
195 sizeof(struct rds_header) -
196 tc->t_tinc_hdr_rem,
197 to_copy);
198 tc->t_tinc_hdr_rem -= to_copy;
199 left -= to_copy;
200 offset += to_copy;
201
202 if (tc->t_tinc_hdr_rem == 0) {
203
204 tc->t_tinc_data_rem =
205 be32_to_cpu(tinc->ti_inc.i_hdr.h_len);
206 }
207 }
208
209 if (left && tc->t_tinc_data_rem) {
210 clone = skb_clone(skb, arg->gfp);
211 if (!clone) {
212 desc->error = -ENOMEM;
213 goto out;
214 }
215
216 to_copy = min(tc->t_tinc_data_rem, left);
217 if (!pskb_pull(clone, offset) ||
218 pskb_trim(clone, to_copy)) {
219 pr_warn("rds_tcp_data_recv: pull/trim failed "
220 "left %zu data_rem %zu skb_len %d\n",
221 left, tc->t_tinc_data_rem, skb->len);
222 kfree_skb(clone);
223 desc->error = -ENOMEM;
224 goto out;
225 }
226 skb_queue_tail(&tinc->ti_skb_list, clone);
227
228 rdsdebug("skb %p data %p len %d off %u to_copy %zu -> "
229 "clone %p data %p len %d\n",
230 skb, skb->data, skb->len, offset, to_copy,
231 clone, clone->data, clone->len);
232
233 tc->t_tinc_data_rem -= to_copy;
234 left -= to_copy;
235 offset += to_copy;
236 }
237
238 if (tc->t_tinc_hdr_rem == 0 && tc->t_tinc_data_rem == 0) {
239 if (tinc->ti_inc.i_hdr.h_flags == RDS_FLAG_CONG_BITMAP)
240 rds_tcp_cong_recv(conn, tinc);
241 else
242 rds_recv_incoming(conn, conn->c_faddr,
243 conn->c_laddr, &tinc->ti_inc,
244 arg->gfp);
245
246 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
247 tc->t_tinc_data_rem = 0;
248 tc->t_tinc = NULL;
249 rds_inc_put(&tinc->ti_inc);
250 tinc = NULL;
251 }
252 }
253out:
254 rdsdebug("returning len %zu left %zu skb len %d rx queue depth %d\n",
255 len, left, skb->len,
256 skb_queue_len(&tc->t_sock->sk->sk_receive_queue));
257 return len - left;
258}
259
260
261static int rds_tcp_read_sock(struct rds_connection *conn, gfp_t gfp)
262{
263 struct rds_tcp_connection *tc = conn->c_transport_data;
264 struct socket *sock = tc->t_sock;
265 read_descriptor_t desc;
266 struct rds_tcp_desc_arg arg;
267
268
269 arg.conn = conn;
270 arg.gfp = gfp;
271 desc.arg.data = &arg;
272 desc.error = 0;
273 desc.count = 1;
274
275 tcp_read_sock(sock->sk, &desc, rds_tcp_data_recv);
276 rdsdebug("tcp_read_sock for tc %p gfp 0x%x returned %d\n", tc, gfp,
277 desc.error);
278
279 return desc.error;
280}
281
282
283
284
285
286
287
288
289int rds_tcp_recv(struct rds_connection *conn)
290{
291 struct rds_tcp_connection *tc = conn->c_transport_data;
292 struct socket *sock = tc->t_sock;
293 int ret = 0;
294
295 rdsdebug("recv worker conn %p tc %p sock %p\n", conn, tc, sock);
296
297 lock_sock(sock->sk);
298 ret = rds_tcp_read_sock(conn, GFP_KERNEL);
299 release_sock(sock->sk);
300
301 return ret;
302}
303
304void rds_tcp_data_ready(struct sock *sk)
305{
306 void (*ready)(struct sock *sk);
307 struct rds_connection *conn;
308 struct rds_tcp_connection *tc;
309
310 rdsdebug("data ready sk %p\n", sk);
311
312 read_lock(&sk->sk_callback_lock);
313 conn = sk->sk_user_data;
314 if (!conn) {
315 ready = sk->sk_data_ready;
316 goto out;
317 }
318
319 tc = conn->c_transport_data;
320 ready = tc->t_orig_data_ready;
321 rds_tcp_stats_inc(s_tcp_data_ready_calls);
322
323 if (rds_tcp_read_sock(conn, GFP_ATOMIC) == -ENOMEM)
324 queue_delayed_work(rds_wq, &conn->c_recv_w, 0);
325out:
326 read_unlock(&sk->sk_callback_lock);
327 ready(sk);
328}
329
330int rds_tcp_recv_init(void)
331{
332 rds_tcp_incoming_slab = kmem_cache_create("rds_tcp_incoming",
333 sizeof(struct rds_tcp_incoming),
334 0, 0, NULL);
335 if (!rds_tcp_incoming_slab)
336 return -ENOMEM;
337 return 0;
338}
339
340void rds_tcp_recv_exit(void)
341{
342 kmem_cache_destroy(rds_tcp_incoming_slab);
343}
344