xref: /linux/net/smc/smc_rx.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Shared Memory Communications over RDMA (SMC-R) and RoCE
3  *
4  * Manage RMBE
5  * copy new RMBE data into user space
6  *
7  * Copyright IBM Corp. 2016
8  *
9  * Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
10  */
11 
12 #include <linux/net.h>
13 #include <linux/rcupdate.h>
14 #include <linux/sched/signal.h>
15 
16 #include <net/sock.h>
17 
18 #include "smc.h"
19 #include "smc_core.h"
20 #include "smc_cdc.h"
21 #include "smc_tx.h" /* smc_tx_consumer_update() */
22 #include "smc_rx.h"
23 
24 /* callback implementation for sk.sk_data_ready()
25  * to wakeup rcvbuf consumers that blocked with smc_rx_wait_data().
26  * indirectly called by smc_cdc_msg_recv_action().
27  */
28 static void smc_rx_data_ready(struct sock *sk)
29 {
30 	struct socket_wq *wq;
31 
32 	/* derived from sock_def_readable() */
33 	/* called already in smc_listen_work() */
34 	rcu_read_lock();
35 	wq = rcu_dereference(sk->sk_wq);
36 	if (skwq_has_sleeper(wq))
37 		wake_up_interruptible_sync_poll(&wq->wait, POLLIN | POLLPRI |
38 						POLLRDNORM | POLLRDBAND);
39 	if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
40 	    (sk->sk_state == SMC_CLOSED))
41 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
42 	else
43 		sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
44 	rcu_read_unlock();
45 }
46 
47 /* blocks rcvbuf consumer until >=len bytes available or timeout or interrupted
48  *   @smc    smc socket
49  *   @timeo  pointer to max seconds to wait, pointer to value 0 for no timeout
50  * Returns:
51  * 1 if at least 1 byte available in rcvbuf or if socket error/shutdown.
52  * 0 otherwise (nothing in rcvbuf nor timeout, e.g. interrupted).
53  */
54 static int smc_rx_wait_data(struct smc_sock *smc, long *timeo)
55 {
56 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
57 	struct smc_connection *conn = &smc->conn;
58 	struct sock *sk = &smc->sk;
59 	int rc;
60 
61 	if (atomic_read(&conn->bytes_to_rcv))
62 		return 1;
63 	sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
64 	add_wait_queue(sk_sleep(sk), &wait);
65 	rc = sk_wait_event(sk, timeo,
66 			   sk->sk_err ||
67 			   sk->sk_shutdown & RCV_SHUTDOWN ||
68 			   sock_flag(sk, SOCK_DONE) ||
69 			   atomic_read(&conn->bytes_to_rcv) ||
70 			   smc_cdc_rxed_any_close_or_senddone(conn),
71 			   &wait);
72 	remove_wait_queue(sk_sleep(sk), &wait);
73 	sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
74 	return rc;
75 }
76 
77 /* rcvbuf consumer: main API called by socket layer.
78  * called under sk lock.
79  */
80 int smc_rx_recvmsg(struct smc_sock *smc, struct msghdr *msg, size_t len,
81 		   int flags)
82 {
83 	size_t copylen, read_done = 0, read_remaining = len;
84 	size_t chunk_len, chunk_off, chunk_len_sum;
85 	struct smc_connection *conn = &smc->conn;
86 	union smc_host_cursor cons;
87 	int readable, chunk;
88 	char *rcvbuf_base;
89 	struct sock *sk;
90 	long timeo;
91 	int target;		/* Read at least these many bytes */
92 	int rc;
93 
94 	if (unlikely(flags & MSG_ERRQUEUE))
95 		return -EINVAL; /* future work for sk.sk_family == AF_SMC */
96 	if (flags & MSG_OOB)
97 		return -EINVAL; /* future work */
98 
99 	sk = &smc->sk;
100 	if (sk->sk_state == SMC_LISTEN)
101 		return -ENOTCONN;
102 	timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
103 	target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
104 
105 	msg->msg_namelen = 0;
106 	/* we currently use 1 RMBE per RMB, so RMBE == RMB base addr */
107 	rcvbuf_base = conn->rmb_desc->cpu_addr;
108 
109 	do { /* while (read_remaining) */
110 		if (read_done >= target)
111 			break;
112 
113 		if (atomic_read(&conn->bytes_to_rcv))
114 			goto copy;
115 
116 		if (read_done) {
117 			if (sk->sk_err ||
118 			    sk->sk_state == SMC_CLOSED ||
119 			    (sk->sk_shutdown & RCV_SHUTDOWN) ||
120 			    !timeo ||
121 			    signal_pending(current) ||
122 			    smc_cdc_rxed_any_close_or_senddone(conn) ||
123 			    conn->local_tx_ctrl.conn_state_flags.
124 			    peer_conn_abort)
125 				break;
126 		} else {
127 			if (sock_flag(sk, SOCK_DONE))
128 				break;
129 			if (sk->sk_err) {
130 				read_done = sock_error(sk);
131 				break;
132 			}
133 			if (sk->sk_shutdown & RCV_SHUTDOWN ||
134 			    smc_cdc_rxed_any_close_or_senddone(conn) ||
135 			    conn->local_tx_ctrl.conn_state_flags.
136 			    peer_conn_abort)
137 				break;
138 			if (sk->sk_state == SMC_CLOSED) {
139 				if (!sock_flag(sk, SOCK_DONE)) {
140 					/* This occurs when user tries to read
141 					 * from never connected socket.
142 					 */
143 					read_done = -ENOTCONN;
144 					break;
145 				}
146 				break;
147 			}
148 			if (signal_pending(current)) {
149 				read_done = sock_intr_errno(timeo);
150 				break;
151 			}
152 		}
153 
154 		if (!atomic_read(&conn->bytes_to_rcv)) {
155 			smc_rx_wait_data(smc, &timeo);
156 			continue;
157 		}
158 
159 copy:
160 		/* initialize variables for 1st iteration of subsequent loop */
161 		/* could be just 1 byte, even after smc_rx_wait_data above */
162 		readable = atomic_read(&conn->bytes_to_rcv);
163 		/* not more than what user space asked for */
164 		copylen = min_t(size_t, read_remaining, readable);
165 		smc_curs_write(&cons,
166 			       smc_curs_read(&conn->local_tx_ctrl.cons, conn),
167 			       conn);
168 		/* determine chunks where to read from rcvbuf */
169 		/* either unwrapped case, or 1st chunk of wrapped case */
170 		chunk_len = min_t(size_t,
171 				  copylen, conn->rmbe_size - cons.count);
172 		chunk_len_sum = chunk_len;
173 		chunk_off = cons.count;
174 		for (chunk = 0; chunk < 2; chunk++) {
175 			if (!(flags & MSG_TRUNC)) {
176 				rc = memcpy_to_msg(msg, rcvbuf_base + chunk_off,
177 						   chunk_len);
178 				if (rc) {
179 					if (!read_done)
180 						read_done = -EFAULT;
181 					goto out;
182 				}
183 			}
184 			read_remaining -= chunk_len;
185 			read_done += chunk_len;
186 
187 			if (chunk_len_sum == copylen)
188 				break; /* either on 1st or 2nd iteration */
189 			/* prepare next (== 2nd) iteration */
190 			chunk_len = copylen - chunk_len; /* remainder */
191 			chunk_len_sum += chunk_len;
192 			chunk_off = 0; /* modulo offset in recv ring buffer */
193 		}
194 
195 		/* update cursors */
196 		if (!(flags & MSG_PEEK)) {
197 			smc_curs_add(conn->rmbe_size, &cons, copylen);
198 			/* increased in recv tasklet smc_cdc_msg_rcv() */
199 			smp_mb__before_atomic();
200 			atomic_sub(copylen, &conn->bytes_to_rcv);
201 			/* guarantee 0 <= bytes_to_rcv <= rmbe_size */
202 			smp_mb__after_atomic();
203 			smc_curs_write(&conn->local_tx_ctrl.cons,
204 				       smc_curs_read(&cons, conn),
205 				       conn);
206 			/* send consumer cursor update if required */
207 			/* similar to advertising new TCP rcv_wnd if required */
208 			smc_tx_consumer_update(conn);
209 		}
210 	} while (read_remaining);
211 out:
212 	return read_done;
213 }
214 
215 /* Initialize receive properties on connection establishment. NB: not __init! */
216 void smc_rx_init(struct smc_sock *smc)
217 {
218 	smc->sk.sk_data_ready = smc_rx_data_ready;
219 }
220