1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This software is available to you under a choice of one of two
5*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
6*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
7*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the
8*4882a593Smuzhiyun * OpenIB.org BSD license below:
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or
11*4882a593Smuzhiyun * without modification, are permitted provided that the following
12*4882a593Smuzhiyun * conditions are met:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * - Redistributions of source code must retain the above
15*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
16*4882a593Smuzhiyun * disclaimer.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above
19*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
20*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials
21*4882a593Smuzhiyun * provided with the distribution.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*4882a593Smuzhiyun * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*4882a593Smuzhiyun * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*4882a593Smuzhiyun * SOFTWARE.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun #include <linux/kernel.h>
34*4882a593Smuzhiyun #include <linux/slab.h>
35*4882a593Smuzhiyun #include <net/tcp.h>
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include "rds.h"
38*4882a593Smuzhiyun #include "tcp.h"
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static struct kmem_cache *rds_tcp_incoming_slab;
41*4882a593Smuzhiyun
rds_tcp_inc_purge(struct rds_incoming * inc)42*4882a593Smuzhiyun static void rds_tcp_inc_purge(struct rds_incoming *inc)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun struct rds_tcp_incoming *tinc;
45*4882a593Smuzhiyun tinc = container_of(inc, struct rds_tcp_incoming, ti_inc);
46*4882a593Smuzhiyun rdsdebug("purging tinc %p inc %p\n", tinc, inc);
47*4882a593Smuzhiyun skb_queue_purge(&tinc->ti_skb_list);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
rds_tcp_inc_free(struct rds_incoming * inc)50*4882a593Smuzhiyun void rds_tcp_inc_free(struct rds_incoming *inc)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun struct rds_tcp_incoming *tinc;
53*4882a593Smuzhiyun tinc = container_of(inc, struct rds_tcp_incoming, ti_inc);
54*4882a593Smuzhiyun rds_tcp_inc_purge(inc);
55*4882a593Smuzhiyun rdsdebug("freeing tinc %p inc %p\n", tinc, inc);
56*4882a593Smuzhiyun kmem_cache_free(rds_tcp_incoming_slab, tinc);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * this is pretty lame, but, whatever.
61*4882a593Smuzhiyun */
rds_tcp_inc_copy_to_user(struct rds_incoming * inc,struct iov_iter * to)62*4882a593Smuzhiyun int rds_tcp_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun struct rds_tcp_incoming *tinc;
65*4882a593Smuzhiyun struct sk_buff *skb;
66*4882a593Smuzhiyun int ret = 0;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun if (!iov_iter_count(to))
69*4882a593Smuzhiyun goto out;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun tinc = container_of(inc, struct rds_tcp_incoming, ti_inc);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun skb_queue_walk(&tinc->ti_skb_list, skb) {
74*4882a593Smuzhiyun unsigned long to_copy, skb_off;
75*4882a593Smuzhiyun for (skb_off = 0; skb_off < skb->len; skb_off += to_copy) {
76*4882a593Smuzhiyun to_copy = iov_iter_count(to);
77*4882a593Smuzhiyun to_copy = min(to_copy, skb->len - skb_off);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (skb_copy_datagram_iter(skb, skb_off, to, to_copy))
80*4882a593Smuzhiyun return -EFAULT;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun rds_stats_add(s_copy_to_user, to_copy);
83*4882a593Smuzhiyun ret += to_copy;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun if (!iov_iter_count(to))
86*4882a593Smuzhiyun goto out;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun out:
90*4882a593Smuzhiyun return ret;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * We have a series of skbs that have fragmented pieces of the congestion
95*4882a593Smuzhiyun * bitmap. They must add up to the exact size of the congestion bitmap. We
96*4882a593Smuzhiyun * use the skb helpers to copy those into the pages that make up the in-memory
97*4882a593Smuzhiyun * congestion bitmap for the remote address of this connection. We then tell
98*4882a593Smuzhiyun * the congestion core that the bitmap has been changed so that it can wake up
99*4882a593Smuzhiyun * sleepers.
100*4882a593Smuzhiyun *
101*4882a593Smuzhiyun * This is racing with sending paths which are using test_bit to see if the
102*4882a593Smuzhiyun * bitmap indicates that their recipient is congested.
103*4882a593Smuzhiyun */
104*4882a593Smuzhiyun
rds_tcp_cong_recv(struct rds_connection * conn,struct rds_tcp_incoming * tinc)105*4882a593Smuzhiyun static void rds_tcp_cong_recv(struct rds_connection *conn,
106*4882a593Smuzhiyun struct rds_tcp_incoming *tinc)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun struct sk_buff *skb;
109*4882a593Smuzhiyun unsigned int to_copy, skb_off;
110*4882a593Smuzhiyun unsigned int map_off;
111*4882a593Smuzhiyun unsigned int map_page;
112*4882a593Smuzhiyun struct rds_cong_map *map;
113*4882a593Smuzhiyun int ret;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* catch completely corrupt packets */
116*4882a593Smuzhiyun if (be32_to_cpu(tinc->ti_inc.i_hdr.h_len) != RDS_CONG_MAP_BYTES)
117*4882a593Smuzhiyun return;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun map_page = 0;
120*4882a593Smuzhiyun map_off = 0;
121*4882a593Smuzhiyun map = conn->c_fcong;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun skb_queue_walk(&tinc->ti_skb_list, skb) {
124*4882a593Smuzhiyun skb_off = 0;
125*4882a593Smuzhiyun while (skb_off < skb->len) {
126*4882a593Smuzhiyun to_copy = min_t(unsigned int, PAGE_SIZE - map_off,
127*4882a593Smuzhiyun skb->len - skb_off);
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun BUG_ON(map_page >= RDS_CONG_MAP_PAGES);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /* only returns 0 or -error */
132*4882a593Smuzhiyun ret = skb_copy_bits(skb, skb_off,
133*4882a593Smuzhiyun (void *)map->m_page_addrs[map_page] + map_off,
134*4882a593Smuzhiyun to_copy);
135*4882a593Smuzhiyun BUG_ON(ret != 0);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun skb_off += to_copy;
138*4882a593Smuzhiyun map_off += to_copy;
139*4882a593Smuzhiyun if (map_off == PAGE_SIZE) {
140*4882a593Smuzhiyun map_off = 0;
141*4882a593Smuzhiyun map_page++;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun rds_cong_map_updated(map, ~(u64) 0);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun struct rds_tcp_desc_arg {
150*4882a593Smuzhiyun struct rds_conn_path *conn_path;
151*4882a593Smuzhiyun gfp_t gfp;
152*4882a593Smuzhiyun };
153*4882a593Smuzhiyun
rds_tcp_data_recv(read_descriptor_t * desc,struct sk_buff * skb,unsigned int offset,size_t len)154*4882a593Smuzhiyun static int rds_tcp_data_recv(read_descriptor_t *desc, struct sk_buff *skb,
155*4882a593Smuzhiyun unsigned int offset, size_t len)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun struct rds_tcp_desc_arg *arg = desc->arg.data;
158*4882a593Smuzhiyun struct rds_conn_path *cp = arg->conn_path;
159*4882a593Smuzhiyun struct rds_tcp_connection *tc = cp->cp_transport_data;
160*4882a593Smuzhiyun struct rds_tcp_incoming *tinc = tc->t_tinc;
161*4882a593Smuzhiyun struct sk_buff *clone;
162*4882a593Smuzhiyun size_t left = len, to_copy;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun rdsdebug("tcp data tc %p skb %p offset %u len %zu\n", tc, skb, offset,
165*4882a593Smuzhiyun len);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /*
168*4882a593Smuzhiyun * tcp_read_sock() interprets partial progress as an indication to stop
169*4882a593Smuzhiyun * processing.
170*4882a593Smuzhiyun */
171*4882a593Smuzhiyun while (left) {
172*4882a593Smuzhiyun if (!tinc) {
173*4882a593Smuzhiyun tinc = kmem_cache_alloc(rds_tcp_incoming_slab,
174*4882a593Smuzhiyun arg->gfp);
175*4882a593Smuzhiyun if (!tinc) {
176*4882a593Smuzhiyun desc->error = -ENOMEM;
177*4882a593Smuzhiyun goto out;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun tc->t_tinc = tinc;
180*4882a593Smuzhiyun rdsdebug("alloced tinc %p\n", tinc);
181*4882a593Smuzhiyun rds_inc_path_init(&tinc->ti_inc, cp,
182*4882a593Smuzhiyun &cp->cp_conn->c_faddr);
183*4882a593Smuzhiyun tinc->ti_inc.i_rx_lat_trace[RDS_MSG_RX_HDR] =
184*4882a593Smuzhiyun local_clock();
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun * XXX * we might be able to use the __ variants when
188*4882a593Smuzhiyun * we've already serialized at a higher level.
189*4882a593Smuzhiyun */
190*4882a593Smuzhiyun skb_queue_head_init(&tinc->ti_skb_list);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun if (left && tc->t_tinc_hdr_rem) {
194*4882a593Smuzhiyun to_copy = min(tc->t_tinc_hdr_rem, left);
195*4882a593Smuzhiyun rdsdebug("copying %zu header from skb %p\n", to_copy,
196*4882a593Smuzhiyun skb);
197*4882a593Smuzhiyun skb_copy_bits(skb, offset,
198*4882a593Smuzhiyun (char *)&tinc->ti_inc.i_hdr +
199*4882a593Smuzhiyun sizeof(struct rds_header) -
200*4882a593Smuzhiyun tc->t_tinc_hdr_rem,
201*4882a593Smuzhiyun to_copy);
202*4882a593Smuzhiyun tc->t_tinc_hdr_rem -= to_copy;
203*4882a593Smuzhiyun left -= to_copy;
204*4882a593Smuzhiyun offset += to_copy;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun if (tc->t_tinc_hdr_rem == 0) {
207*4882a593Smuzhiyun /* could be 0 for a 0 len message */
208*4882a593Smuzhiyun tc->t_tinc_data_rem =
209*4882a593Smuzhiyun be32_to_cpu(tinc->ti_inc.i_hdr.h_len);
210*4882a593Smuzhiyun tinc->ti_inc.i_rx_lat_trace[RDS_MSG_RX_START] =
211*4882a593Smuzhiyun local_clock();
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun if (left && tc->t_tinc_data_rem) {
216*4882a593Smuzhiyun to_copy = min(tc->t_tinc_data_rem, left);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun clone = pskb_extract(skb, offset, to_copy, arg->gfp);
219*4882a593Smuzhiyun if (!clone) {
220*4882a593Smuzhiyun desc->error = -ENOMEM;
221*4882a593Smuzhiyun goto out;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun skb_queue_tail(&tinc->ti_skb_list, clone);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun rdsdebug("skb %p data %p len %d off %u to_copy %zu -> "
227*4882a593Smuzhiyun "clone %p data %p len %d\n",
228*4882a593Smuzhiyun skb, skb->data, skb->len, offset, to_copy,
229*4882a593Smuzhiyun clone, clone->data, clone->len);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun tc->t_tinc_data_rem -= to_copy;
232*4882a593Smuzhiyun left -= to_copy;
233*4882a593Smuzhiyun offset += to_copy;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (tc->t_tinc_hdr_rem == 0 && tc->t_tinc_data_rem == 0) {
237*4882a593Smuzhiyun struct rds_connection *conn = cp->cp_conn;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun if (tinc->ti_inc.i_hdr.h_flags == RDS_FLAG_CONG_BITMAP)
240*4882a593Smuzhiyun rds_tcp_cong_recv(conn, tinc);
241*4882a593Smuzhiyun else
242*4882a593Smuzhiyun rds_recv_incoming(conn, &conn->c_faddr,
243*4882a593Smuzhiyun &conn->c_laddr,
244*4882a593Smuzhiyun &tinc->ti_inc,
245*4882a593Smuzhiyun arg->gfp);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun tc->t_tinc_hdr_rem = sizeof(struct rds_header);
248*4882a593Smuzhiyun tc->t_tinc_data_rem = 0;
249*4882a593Smuzhiyun tc->t_tinc = NULL;
250*4882a593Smuzhiyun rds_inc_put(&tinc->ti_inc);
251*4882a593Smuzhiyun tinc = NULL;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun out:
255*4882a593Smuzhiyun rdsdebug("returning len %zu left %zu skb len %d rx queue depth %d\n",
256*4882a593Smuzhiyun len, left, skb->len,
257*4882a593Smuzhiyun skb_queue_len(&tc->t_sock->sk->sk_receive_queue));
258*4882a593Smuzhiyun return len - left;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /* the caller has to hold the sock lock */
rds_tcp_read_sock(struct rds_conn_path * cp,gfp_t gfp)262*4882a593Smuzhiyun static int rds_tcp_read_sock(struct rds_conn_path *cp, gfp_t gfp)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun struct rds_tcp_connection *tc = cp->cp_transport_data;
265*4882a593Smuzhiyun struct socket *sock = tc->t_sock;
266*4882a593Smuzhiyun read_descriptor_t desc;
267*4882a593Smuzhiyun struct rds_tcp_desc_arg arg;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /* It's like glib in the kernel! */
270*4882a593Smuzhiyun arg.conn_path = cp;
271*4882a593Smuzhiyun arg.gfp = gfp;
272*4882a593Smuzhiyun desc.arg.data = &arg;
273*4882a593Smuzhiyun desc.error = 0;
274*4882a593Smuzhiyun desc.count = 1; /* give more than one skb per call */
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun tcp_read_sock(sock->sk, &desc, rds_tcp_data_recv);
277*4882a593Smuzhiyun rdsdebug("tcp_read_sock for tc %p gfp 0x%x returned %d\n", tc, gfp,
278*4882a593Smuzhiyun desc.error);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun return desc.error;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun * We hold the sock lock to serialize our rds_tcp_recv->tcp_read_sock from
285*4882a593Smuzhiyun * data_ready.
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * if we fail to allocate we're in trouble.. blindly wait some time before
288*4882a593Smuzhiyun * trying again to see if the VM can free up something for us.
289*4882a593Smuzhiyun */
rds_tcp_recv_path(struct rds_conn_path * cp)290*4882a593Smuzhiyun int rds_tcp_recv_path(struct rds_conn_path *cp)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun struct rds_tcp_connection *tc = cp->cp_transport_data;
293*4882a593Smuzhiyun struct socket *sock = tc->t_sock;
294*4882a593Smuzhiyun int ret = 0;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun rdsdebug("recv worker path [%d] tc %p sock %p\n",
297*4882a593Smuzhiyun cp->cp_index, tc, sock);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun lock_sock(sock->sk);
300*4882a593Smuzhiyun ret = rds_tcp_read_sock(cp, GFP_KERNEL);
301*4882a593Smuzhiyun release_sock(sock->sk);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun return ret;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
rds_tcp_data_ready(struct sock * sk)306*4882a593Smuzhiyun void rds_tcp_data_ready(struct sock *sk)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun void (*ready)(struct sock *sk);
309*4882a593Smuzhiyun struct rds_conn_path *cp;
310*4882a593Smuzhiyun struct rds_tcp_connection *tc;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun rdsdebug("data ready sk %p\n", sk);
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun read_lock_bh(&sk->sk_callback_lock);
315*4882a593Smuzhiyun cp = sk->sk_user_data;
316*4882a593Smuzhiyun if (!cp) { /* check for teardown race */
317*4882a593Smuzhiyun ready = sk->sk_data_ready;
318*4882a593Smuzhiyun goto out;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun tc = cp->cp_transport_data;
322*4882a593Smuzhiyun ready = tc->t_orig_data_ready;
323*4882a593Smuzhiyun rds_tcp_stats_inc(s_tcp_data_ready_calls);
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun if (rds_tcp_read_sock(cp, GFP_ATOMIC) == -ENOMEM) {
326*4882a593Smuzhiyun rcu_read_lock();
327*4882a593Smuzhiyun if (!rds_destroy_pending(cp->cp_conn))
328*4882a593Smuzhiyun queue_delayed_work(rds_wq, &cp->cp_recv_w, 0);
329*4882a593Smuzhiyun rcu_read_unlock();
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun out:
332*4882a593Smuzhiyun read_unlock_bh(&sk->sk_callback_lock);
333*4882a593Smuzhiyun ready(sk);
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
rds_tcp_recv_init(void)336*4882a593Smuzhiyun int rds_tcp_recv_init(void)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun rds_tcp_incoming_slab = kmem_cache_create("rds_tcp_incoming",
339*4882a593Smuzhiyun sizeof(struct rds_tcp_incoming),
340*4882a593Smuzhiyun 0, 0, NULL);
341*4882a593Smuzhiyun if (!rds_tcp_incoming_slab)
342*4882a593Smuzhiyun return -ENOMEM;
343*4882a593Smuzhiyun return 0;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
rds_tcp_recv_exit(void)346*4882a593Smuzhiyun void rds_tcp_recv_exit(void)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun kmem_cache_destroy(rds_tcp_incoming_slab);
349*4882a593Smuzhiyun }
350