Blame view

net/rds/recv.c 20.7 KB
bdbe6fbc6   Andy Grover   RDS: recv.c
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
  /*
   * Copyright (c) 2006 Oracle.  All rights reserved.
   *
   * This software is available to you under a choice of one of two
   * licenses.  You may choose to be licensed under the terms of the GNU
   * General Public License (GPL) Version 2, available from the file
   * COPYING in the main directory of this source tree, or the
   * OpenIB.org BSD license below:
   *
   *     Redistribution and use in source and binary forms, with or
   *     without modification, are permitted provided that the following
   *     conditions are met:
   *
   *      - Redistributions of source code must retain the above
   *        copyright notice, this list of conditions and the following
   *        disclaimer.
   *
   *      - Redistributions in binary form must reproduce the above
   *        copyright notice, this list of conditions and the following
   *        disclaimer in the documentation and/or other materials
   *        provided with the distribution.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
   *
   */
  #include <linux/kernel.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
34
  #include <linux/slab.h>
bdbe6fbc6   Andy Grover   RDS: recv.c
35
36
  #include <net/sock.h>
  #include <linux/in.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
37
  #include <linux/export.h>
5711f8b35   santosh.shilimkar@oracle.com   RDS: Add support ...
38
39
  #include <linux/time.h>
  #include <linux/rds.h>
bdbe6fbc6   Andy Grover   RDS: recv.c
40
41
  
  #include "rds.h"
bdbe6fbc6   Andy Grover   RDS: recv.c
42
43
44
45
  
  void rds_inc_init(struct rds_incoming *inc, struct rds_connection *conn,
  		  __be32 saddr)
  {
3289025ae   Santosh Shilimkar   RDS: add receive ...
46
  	int i;
b7f029209   Reshetova, Elena   net, rds: convert...
47
  	refcount_set(&inc->i_refcount, 1);
bdbe6fbc6   Andy Grover   RDS: recv.c
48
49
50
51
  	INIT_LIST_HEAD(&inc->i_item);
  	inc->i_conn = conn;
  	inc->i_saddr = saddr;
  	inc->i_rdma_cookie = 0;
5711f8b35   santosh.shilimkar@oracle.com   RDS: Add support ...
52
53
  	inc->i_rx_tstamp.tv_sec = 0;
  	inc->i_rx_tstamp.tv_usec = 0;
3289025ae   Santosh Shilimkar   RDS: add receive ...
54
55
56
  
  	for (i = 0; i < RDS_RX_MAX_TRACES; i++)
  		inc->i_rx_lat_trace[i] = 0;
bdbe6fbc6   Andy Grover   RDS: recv.c
57
  }
616b757ae   Andy Grover   RDS: Export symbo...
58
  EXPORT_SYMBOL_GPL(rds_inc_init);
bdbe6fbc6   Andy Grover   RDS: recv.c
59

5e833e025   Sowmini Varadhan   RDS: rds_inc_path...
60
61
62
  void rds_inc_path_init(struct rds_incoming *inc, struct rds_conn_path *cp,
  		       __be32 saddr)
  {
b7f029209   Reshetova, Elena   net, rds: convert...
63
  	refcount_set(&inc->i_refcount, 1);
5e833e025   Sowmini Varadhan   RDS: rds_inc_path...
64
65
66
67
68
69
70
71
72
  	INIT_LIST_HEAD(&inc->i_item);
  	inc->i_conn = cp->cp_conn;
  	inc->i_conn_path = cp;
  	inc->i_saddr = saddr;
  	inc->i_rdma_cookie = 0;
  	inc->i_rx_tstamp.tv_sec = 0;
  	inc->i_rx_tstamp.tv_usec = 0;
  }
  EXPORT_SYMBOL_GPL(rds_inc_path_init);
ff51bf841   stephen hemminger   rds: make local f...
73
  static void rds_inc_addref(struct rds_incoming *inc)
bdbe6fbc6   Andy Grover   RDS: recv.c
74
  {
b7f029209   Reshetova, Elena   net, rds: convert...
75
76
77
  	rdsdebug("addref inc %p ref %d
  ", inc, refcount_read(&inc->i_refcount));
  	refcount_inc(&inc->i_refcount);
bdbe6fbc6   Andy Grover   RDS: recv.c
78
79
80
81
  }
  
  void rds_inc_put(struct rds_incoming *inc)
  {
b7f029209   Reshetova, Elena   net, rds: convert...
82
83
84
  	rdsdebug("put inc %p ref %d
  ", inc, refcount_read(&inc->i_refcount));
  	if (refcount_dec_and_test(&inc->i_refcount)) {
bdbe6fbc6   Andy Grover   RDS: recv.c
85
86
87
88
89
  		BUG_ON(!list_empty(&inc->i_item));
  
  		inc->i_conn->c_trans->inc_free(inc);
  	}
  }
616b757ae   Andy Grover   RDS: Export symbo...
90
  EXPORT_SYMBOL_GPL(rds_inc_put);
bdbe6fbc6   Andy Grover   RDS: recv.c
91
92
93
94
95
96
97
98
99
100
101
  
  static void rds_recv_rcvbuf_delta(struct rds_sock *rs, struct sock *sk,
  				  struct rds_cong_map *map,
  				  int delta, __be16 port)
  {
  	int now_congested;
  
  	if (delta == 0)
  		return;
  
  	rs->rs_rcv_bytes += delta;
192a798f5   Venkat Venkatsubra   RDS: add stat for...
102
103
104
105
  	if (delta > 0)
  		rds_stats_add(s_recv_bytes_added_to_socket, delta);
  	else
  		rds_stats_add(s_recv_bytes_removed_from_socket, -delta);
a4b57440d   Santosh Shilimkar   rds: avoid unenec...
106
107
108
109
  
  	/* loop transport doesn't send/recv congestion updates */
  	if (rs->rs_transport->t_type == RDS_TRANS_LOOP)
  		return;
bdbe6fbc6   Andy Grover   RDS: recv.c
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  	now_congested = rs->rs_rcv_bytes > rds_sk_rcvbuf(rs);
  
  	rdsdebug("rs %p (%pI4:%u) recv bytes %d buf %d "
  	  "now_cong %d delta %d
  ",
  	  rs, &rs->rs_bound_addr,
  	  ntohs(rs->rs_bound_port), rs->rs_rcv_bytes,
  	  rds_sk_rcvbuf(rs), now_congested, delta);
  
  	/* wasn't -> am congested */
  	if (!rs->rs_congested && now_congested) {
  		rs->rs_congested = 1;
  		rds_cong_set_bit(map, port);
  		rds_cong_queue_updates(map);
  	}
  	/* was -> aren't congested */
  	/* Require more free space before reporting uncongested to prevent
  	   bouncing cong/uncong state too often */
  	else if (rs->rs_congested && (rs->rs_rcv_bytes < (rds_sk_rcvbuf(rs)/2))) {
  		rs->rs_congested = 0;
  		rds_cong_clear_bit(map, port);
  		rds_cong_queue_updates(map);
  	}
  
  	/* do nothing if no change in cong state */
  }
905dd4184   Sowmini Varadhan   RDS: TCP: Track p...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  static void rds_conn_peer_gen_update(struct rds_connection *conn,
  				     u32 peer_gen_num)
  {
  	int i;
  	struct rds_message *rm, *tmp;
  	unsigned long flags;
  
  	WARN_ON(conn->c_trans->t_type != RDS_TRANS_TCP);
  	if (peer_gen_num != 0) {
  		if (conn->c_peer_gen_num != 0 &&
  		    peer_gen_num != conn->c_peer_gen_num) {
  			for (i = 0; i < RDS_MPATH_WORKERS; i++) {
  				struct rds_conn_path *cp;
  
  				cp = &conn->c_path[i];
  				spin_lock_irqsave(&cp->cp_lock, flags);
  				cp->cp_next_tx_seq = 1;
  				cp->cp_next_rx_seq = 0;
  				list_for_each_entry_safe(rm, tmp,
  							 &cp->cp_retrans,
  							 m_conn_item) {
  					set_bit(RDS_MSG_FLUSH, &rm->m_flags);
  				}
  				spin_unlock_irqrestore(&cp->cp_lock, flags);
  			}
  		}
  		conn->c_peer_gen_num = peer_gen_num;
  	}
  }
bdbe6fbc6   Andy Grover   RDS: recv.c
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  /*
   * Process all extension headers that come with this message.
   */
  static void rds_recv_incoming_exthdrs(struct rds_incoming *inc, struct rds_sock *rs)
  {
  	struct rds_header *hdr = &inc->i_hdr;
  	unsigned int pos = 0, type, len;
  	union {
  		struct rds_ext_header_version version;
  		struct rds_ext_header_rdma rdma;
  		struct rds_ext_header_rdma_dest rdma_dest;
  	} buffer;
  
  	while (1) {
  		len = sizeof(buffer);
  		type = rds_message_next_extension(hdr, &pos, &buffer, &len);
  		if (type == RDS_EXTHDR_NONE)
  			break;
  		/* Process extension header here */
  		switch (type) {
  		case RDS_EXTHDR_RDMA:
  			rds_rdma_unuse(rs, be32_to_cpu(buffer.rdma.h_rdma_rkey), 0);
  			break;
  
  		case RDS_EXTHDR_RDMA_DEST:
  			/* We ignore the size for now. We could stash it
  			 * somewhere and use it for error checking. */
  			inc->i_rdma_cookie = rds_rdma_make_cookie(
  					be32_to_cpu(buffer.rdma_dest.h_rdma_rkey),
  					be32_to_cpu(buffer.rdma_dest.h_rdma_offset));
  
  			break;
  		}
  	}
  }
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
200
201
202
203
204
205
206
  static void rds_recv_hs_exthdrs(struct rds_header *hdr,
  				struct rds_connection *conn)
  {
  	unsigned int pos = 0, type, len;
  	union {
  		struct rds_ext_header_version version;
  		u16 rds_npaths;
905dd4184   Sowmini Varadhan   RDS: TCP: Track p...
207
  		u32 rds_gen_num;
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
208
  	} buffer;
905dd4184   Sowmini Varadhan   RDS: TCP: Track p...
209
  	u32 new_peer_gen_num = 0;
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
210
211
212
213
214
215
216
217
218
219
  
  	while (1) {
  		len = sizeof(buffer);
  		type = rds_message_next_extension(hdr, &pos, &buffer, &len);
  		if (type == RDS_EXTHDR_NONE)
  			break;
  		/* Process extension header here */
  		switch (type) {
  		case RDS_EXTHDR_NPATHS:
  			conn->c_npaths = min_t(int, RDS_MPATH_WORKERS,
00354de57   Sowmini Varadhan   rds: tcp: various...
220
  					       be16_to_cpu(buffer.rds_npaths));
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
221
  			break;
905dd4184   Sowmini Varadhan   RDS: TCP: Track p...
222
  		case RDS_EXTHDR_GEN_NUM:
00354de57   Sowmini Varadhan   rds: tcp: various...
223
  			new_peer_gen_num = be32_to_cpu(buffer.rds_gen_num);
905dd4184   Sowmini Varadhan   RDS: TCP: Track p...
224
  			break;
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
225
226
227
228
229
230
231
232
  		default:
  			pr_warn_ratelimited("ignoring unknown exthdr type "
  					     "0x%x
  ", type);
  		}
  	}
  	/* if RDS_EXTHDR_NPATHS was not found, default to a single-path */
  	conn->c_npaths = max_t(int, conn->c_npaths, 1);
69b92b5b7   Sowmini Varadhan   rds: tcp: send ha...
233
  	conn->c_ping_triggered = 0;
905dd4184   Sowmini Varadhan   RDS: TCP: Track p...
234
  	rds_conn_peer_gen_update(conn, new_peer_gen_num);
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  }
  
  /* rds_start_mprds() will synchronously start multiple paths when appropriate.
   * The scheme is based on the following rules:
   *
   * 1. rds_sendmsg on first connect attempt sends the probe ping, with the
   *    sender's npaths (s_npaths)
   * 2. rcvr of probe-ping knows the mprds_paths = min(s_npaths, r_npaths). It
   *    sends back a probe-pong with r_npaths. After that, if rcvr is the
   *    smaller ip addr, it starts rds_conn_path_connect_if_down on all
   *    mprds_paths.
   * 3. sender gets woken up, and can move to rds_conn_path_connect_if_down.
   *    If it is the smaller ipaddr, rds_conn_path_connect_if_down can be
   *    called after reception of the probe-pong on all mprds_paths.
   *    Otherwise (sender of probe-ping is not the smaller ip addr): just call
   *    rds_conn_path_connect_if_down on the hashed path. (see rule 4)
69b92b5b7   Sowmini Varadhan   rds: tcp: send ha...
251
   * 4. rds_connect_worker must only trigger a connection if laddr < faddr.
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
252
253
254
255
256
257
258
   * 5. sender may end up queuing the packet on the cp. will get sent out later.
   *    when connection is completed.
   */
  static void rds_start_mprds(struct rds_connection *conn)
  {
  	int i;
  	struct rds_conn_path *cp;
00354de57   Sowmini Varadhan   rds: tcp: various...
259
260
  	if (conn->c_npaths > 1 &&
  	    IS_CANONICAL(conn->c_laddr, conn->c_faddr)) {
69b92b5b7   Sowmini Varadhan   rds: tcp: send ha...
261
  		for (i = 0; i < conn->c_npaths; i++) {
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
262
263
264
265
266
  			cp = &conn->c_path[i];
  			rds_conn_path_connect_if_down(cp);
  		}
  	}
  }
bdbe6fbc6   Andy Grover   RDS: recv.c
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
  /*
   * The transport must make sure that this is serialized against other
   * rx and conn reset on this specific conn.
   *
   * We currently assert that only one fragmented message will be sent
   * down a connection at a time.  This lets us reassemble in the conn
   * instead of per-flow which means that we don't have to go digging through
   * flows to tear down partial reassembly progress on conn failure and
   * we save flow lookup and locking for each frag arrival.  It does mean
   * that small messages will wait behind large ones.  Fragmenting at all
   * is only to reduce the memory consumption of pre-posted buffers.
   *
   * The caller passes in saddr and daddr instead of us getting it from the
   * conn.  This lets loopback, who only has one conn for both directions,
   * tell us which roles the addrs in the conn are playing for this message.
   */
  void rds_recv_incoming(struct rds_connection *conn, __be32 saddr, __be32 daddr,
6114eab53   Cong Wang   rds: remove the s...
284
  		       struct rds_incoming *inc, gfp_t gfp)
bdbe6fbc6   Andy Grover   RDS: recv.c
285
286
287
288
  {
  	struct rds_sock *rs = NULL;
  	struct sock *sk;
  	unsigned long flags;
ef9e62c2e   Sowmini Varadhan   RDS: recv path ge...
289
  	struct rds_conn_path *cp;
bdbe6fbc6   Andy Grover   RDS: recv.c
290
291
292
  
  	inc->i_conn = conn;
  	inc->i_rx_jiffies = jiffies;
ef9e62c2e   Sowmini Varadhan   RDS: recv path ge...
293
294
295
296
  	if (conn->c_trans->t_mp_capable)
  		cp = inc->i_conn_path;
  	else
  		cp = &conn->c_path[0];
bdbe6fbc6   Andy Grover   RDS: recv.c
297
298
299
300
  
  	rdsdebug("conn %p next %llu inc %p seq %llu len %u sport %u dport %u "
  		 "flags 0x%x rx_jiffies %lu
  ", conn,
ef9e62c2e   Sowmini Varadhan   RDS: recv path ge...
301
  		 (unsigned long long)cp->cp_next_rx_seq,
bdbe6fbc6   Andy Grover   RDS: recv.c
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  		 inc,
  		 (unsigned long long)be64_to_cpu(inc->i_hdr.h_sequence),
  		 be32_to_cpu(inc->i_hdr.h_len),
  		 be16_to_cpu(inc->i_hdr.h_sport),
  		 be16_to_cpu(inc->i_hdr.h_dport),
  		 inc->i_hdr.h_flags,
  		 inc->i_rx_jiffies);
  
  	/*
  	 * Sequence numbers should only increase.  Messages get their
  	 * sequence number as they're queued in a sending conn.  They
  	 * can be dropped, though, if the sending socket is closed before
  	 * they hit the wire.  So sequence numbers can skip forward
  	 * under normal operation.  They can also drop back in the conn
  	 * failover case as previously sent messages are resent down the
  	 * new instance of a conn.  We drop those, otherwise we have
  	 * to assume that the next valid seq does not come after a
  	 * hole in the fragment stream.
  	 *
  	 * The headers don't give us a way to realize if fragments of
  	 * a message have been dropped.  We assume that frags that arrive
  	 * to a flow are part of the current message on the flow that is
  	 * being reassembled.  This means that senders can't drop messages
  	 * from the sending conn until all their frags are sent.
  	 *
  	 * XXX we could spend more on the wire to get more robust failure
  	 * detection, arguably worth it to avoid data corruption.
  	 */
ef9e62c2e   Sowmini Varadhan   RDS: recv path ge...
330
  	if (be64_to_cpu(inc->i_hdr.h_sequence) < cp->cp_next_rx_seq &&
f64f9e719   Joe Perches   net: Move && and ...
331
  	    (inc->i_hdr.h_flags & RDS_FLAG_RETRANSMITTED)) {
bdbe6fbc6   Andy Grover   RDS: recv.c
332
333
334
  		rds_stats_inc(s_recv_drop_old_seq);
  		goto out;
  	}
ef9e62c2e   Sowmini Varadhan   RDS: recv path ge...
335
  	cp->cp_next_rx_seq = be64_to_cpu(inc->i_hdr.h_sequence) + 1;
bdbe6fbc6   Andy Grover   RDS: recv.c
336
337
  
  	if (rds_sysctl_ping_enable && inc->i_hdr.h_dport == 0) {
11bb62f7c   Sowmini Varadhan   RDS: Do not send ...
338
339
340
341
342
  		if (inc->i_hdr.h_sport == 0) {
  			rdsdebug("ignore ping with 0 sport from 0x%x
  ", saddr);
  			goto out;
  		}
bdbe6fbc6   Andy Grover   RDS: recv.c
343
  		rds_stats_inc(s_recv_ping);
45997e9e2   Sowmini Varadhan   RDS: Make rds_sen...
344
  		rds_send_pong(cp, inc->i_hdr.h_sport);
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
345
  		/* if this is a handshake ping, start multipath if necessary */
00354de57   Sowmini Varadhan   rds: tcp: various...
346
347
  		if (RDS_HS_PROBE(be16_to_cpu(inc->i_hdr.h_sport),
  				 be16_to_cpu(inc->i_hdr.h_dport))) {
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
348
349
350
351
352
  			rds_recv_hs_exthdrs(&inc->i_hdr, cp->cp_conn);
  			rds_start_mprds(cp->cp_conn);
  		}
  		goto out;
  	}
00354de57   Sowmini Varadhan   rds: tcp: various...
353
  	if (be16_to_cpu(inc->i_hdr.h_dport) ==  RDS_FLAG_PROBE_PORT &&
5916e2c15   Sowmini Varadhan   RDS: TCP: Enable ...
354
355
356
357
358
  	    inc->i_hdr.h_sport == 0) {
  		rds_recv_hs_exthdrs(&inc->i_hdr, cp->cp_conn);
  		/* if this is a handshake pong, start multipath if necessary */
  		rds_start_mprds(cp->cp_conn);
  		wake_up(&cp->cp_conn->c_hs_waitq);
bdbe6fbc6   Andy Grover   RDS: recv.c
359
360
361
362
  		goto out;
  	}
  
  	rs = rds_find_bound(daddr, inc->i_hdr.h_dport);
8690bfa17   Andy Grover   RDS: cleanup: rem...
363
  	if (!rs) {
bdbe6fbc6   Andy Grover   RDS: recv.c
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  		rds_stats_inc(s_recv_drop_no_sock);
  		goto out;
  	}
  
  	/* Process extension headers */
  	rds_recv_incoming_exthdrs(inc, rs);
  
  	/* We can be racing with rds_release() which marks the socket dead. */
  	sk = rds_rs_to_sk(rs);
  
  	/* serialize with rds_release -> sock_orphan */
  	write_lock_irqsave(&rs->rs_recv_lock, flags);
  	if (!sock_flag(sk, SOCK_DEAD)) {
  		rdsdebug("adding inc %p to rs %p's recv queue
  ", inc, rs);
  		rds_stats_inc(s_recv_queued);
  		rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
  				      be32_to_cpu(inc->i_hdr.h_len),
  				      inc->i_hdr.h_dport);
5711f8b35   santosh.shilimkar@oracle.com   RDS: Add support ...
383
384
  		if (sock_flag(sk, SOCK_RCVTSTAMP))
  			do_gettimeofday(&inc->i_rx_tstamp);
bdbe6fbc6   Andy Grover   RDS: recv.c
385
  		rds_inc_addref(inc);
3289025ae   Santosh Shilimkar   RDS: add receive ...
386
  		inc->i_rx_lat_trace[RDS_MSG_RX_END] = local_clock();
bdbe6fbc6   Andy Grover   RDS: recv.c
387
388
389
390
391
392
393
394
395
396
397
  		list_add_tail(&inc->i_item, &rs->rs_recv_queue);
  		__rds_wake_sk_sleep(sk);
  	} else {
  		rds_stats_inc(s_recv_drop_dead_sock);
  	}
  	write_unlock_irqrestore(&rs->rs_recv_lock, flags);
  
  out:
  	if (rs)
  		rds_sock_put(rs);
  }
616b757ae   Andy Grover   RDS: Export symbo...
398
  EXPORT_SYMBOL_GPL(rds_recv_incoming);
bdbe6fbc6   Andy Grover   RDS: recv.c
399
400
401
402
403
404
405
406
  
  /*
   * be very careful here.  This is being called as the condition in
   * wait_event_*() needs to cope with being called many times.
   */
  static int rds_next_incoming(struct rds_sock *rs, struct rds_incoming **inc)
  {
  	unsigned long flags;
8690bfa17   Andy Grover   RDS: cleanup: rem...
407
  	if (!*inc) {
bdbe6fbc6   Andy Grover   RDS: recv.c
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  		read_lock_irqsave(&rs->rs_recv_lock, flags);
  		if (!list_empty(&rs->rs_recv_queue)) {
  			*inc = list_entry(rs->rs_recv_queue.next,
  					  struct rds_incoming,
  					  i_item);
  			rds_inc_addref(*inc);
  		}
  		read_unlock_irqrestore(&rs->rs_recv_lock, flags);
  	}
  
  	return *inc != NULL;
  }
  
  static int rds_still_queued(struct rds_sock *rs, struct rds_incoming *inc,
  			    int drop)
  {
  	struct sock *sk = rds_rs_to_sk(rs);
  	int ret = 0;
  	unsigned long flags;
  
  	write_lock_irqsave(&rs->rs_recv_lock, flags);
  	if (!list_empty(&inc->i_item)) {
  		ret = 1;
  		if (drop) {
  			/* XXX make sure this i_conn is reliable */
  			rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
  					      -be32_to_cpu(inc->i_hdr.h_len),
  					      inc->i_hdr.h_dport);
  			list_del_init(&inc->i_item);
  			rds_inc_put(inc);
  		}
  	}
  	write_unlock_irqrestore(&rs->rs_recv_lock, flags);
  
  	rdsdebug("inc %p rs %p still %d dropped %d
  ", inc, rs, ret, drop);
  	return ret;
  }
  
  /*
   * Pull errors off the error queue.
   * If msghdr is NULL, we will just purge the error queue.
   */
  int rds_notify_queue_get(struct rds_sock *rs, struct msghdr *msghdr)
  {
  	struct rds_notifier *notifier;
f037590ff   Eric Dumazet   rds: fix a leak o...
454
  	struct rds_rdma_notify cmsg = { 0 }; /* fill holes with zero */
bdbe6fbc6   Andy Grover   RDS: recv.c
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  	unsigned int count = 0, max_messages = ~0U;
  	unsigned long flags;
  	LIST_HEAD(copy);
  	int err = 0;
  
  
  	/* put_cmsg copies to user space and thus may sleep. We can't do this
  	 * with rs_lock held, so first grab as many notifications as we can stuff
  	 * in the user provided cmsg buffer. We don't try to copy more, to avoid
  	 * losing notifications - except when the buffer is so small that it wouldn't
  	 * even hold a single notification. Then we give him as much of this single
  	 * msg as we can squeeze in, and set MSG_CTRUNC.
  	 */
  	if (msghdr) {
  		max_messages = msghdr->msg_controllen / CMSG_SPACE(sizeof(cmsg));
  		if (!max_messages)
  			max_messages = 1;
  	}
  
  	spin_lock_irqsave(&rs->rs_lock, flags);
  	while (!list_empty(&rs->rs_notify_queue) && count < max_messages) {
  		notifier = list_entry(rs->rs_notify_queue.next,
  				struct rds_notifier, n_list);
  		list_move(&notifier->n_list, &copy);
  		count++;
  	}
  	spin_unlock_irqrestore(&rs->rs_lock, flags);
  
  	if (!count)
  		return 0;
  
  	while (!list_empty(&copy)) {
  		notifier = list_entry(copy.next, struct rds_notifier, n_list);
  
  		if (msghdr) {
  			cmsg.user_token = notifier->n_user_token;
6200ed779   Andy Grover   RDS: Whitespace
491
  			cmsg.status = notifier->n_status;
bdbe6fbc6   Andy Grover   RDS: recv.c
492
493
  
  			err = put_cmsg(msghdr, SOL_RDS, RDS_CMSG_RDMA_STATUS,
6200ed779   Andy Grover   RDS: Whitespace
494
  				       sizeof(cmsg), &cmsg);
bdbe6fbc6   Andy Grover   RDS: recv.c
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
  			if (err)
  				break;
  		}
  
  		list_del_init(&notifier->n_list);
  		kfree(notifier);
  	}
  
  	/* If we bailed out because of an error in put_cmsg,
  	 * we may be left with one or more notifications that we
  	 * didn't process. Return them to the head of the list. */
  	if (!list_empty(&copy)) {
  		spin_lock_irqsave(&rs->rs_lock, flags);
  		list_splice(&copy, &rs->rs_notify_queue);
  		spin_unlock_irqrestore(&rs->rs_lock, flags);
  	}
  
  	return err;
  }
  
  /*
   * Queue a congestion notification
   */
  static int rds_notify_cong(struct rds_sock *rs, struct msghdr *msghdr)
  {
  	uint64_t notify = rs->rs_cong_notify;
  	unsigned long flags;
  	int err;
  
  	err = put_cmsg(msghdr, SOL_RDS, RDS_CMSG_CONG_UPDATE,
  			sizeof(notify), &notify);
  	if (err)
  		return err;
  
  	spin_lock_irqsave(&rs->rs_lock, flags);
  	rs->rs_cong_notify &= ~notify;
  	spin_unlock_irqrestore(&rs->rs_lock, flags);
  
  	return 0;
  }
  
  /*
   * Receive any control messages.
   */
5711f8b35   santosh.shilimkar@oracle.com   RDS: Add support ...
539
540
  static int rds_cmsg_recv(struct rds_incoming *inc, struct msghdr *msg,
  			 struct rds_sock *rs)
bdbe6fbc6   Andy Grover   RDS: recv.c
541
542
543
544
545
546
547
  {
  	int ret = 0;
  
  	if (inc->i_rdma_cookie) {
  		ret = put_cmsg(msg, SOL_RDS, RDS_CMSG_RDMA_DEST,
  				sizeof(inc->i_rdma_cookie), &inc->i_rdma_cookie);
  		if (ret)
3289025ae   Santosh Shilimkar   RDS: add receive ...
548
  			goto out;
bdbe6fbc6   Andy Grover   RDS: recv.c
549
  	}
5711f8b35   santosh.shilimkar@oracle.com   RDS: Add support ...
550
551
552
553
554
555
  	if ((inc->i_rx_tstamp.tv_sec != 0) &&
  	    sock_flag(rds_rs_to_sk(rs), SOCK_RCVTSTAMP)) {
  		ret = put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  			       sizeof(struct timeval),
  			       &inc->i_rx_tstamp);
  		if (ret)
3289025ae   Santosh Shilimkar   RDS: add receive ...
556
  			goto out;
5711f8b35   santosh.shilimkar@oracle.com   RDS: Add support ...
557
  	}
3289025ae   Santosh Shilimkar   RDS: add receive ...
558
559
560
  	if (rs->rs_rx_traces) {
  		struct rds_cmsg_rx_trace t;
  		int i, j;
30ffa967a   Eric Dumazet   rds: do not leak ...
561
  		memset(&t, 0, sizeof(t));
3289025ae   Santosh Shilimkar   RDS: add receive ...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
  		inc->i_rx_lat_trace[RDS_MSG_RX_CMSG] = local_clock();
  		t.rx_traces =  rs->rs_rx_traces;
  		for (i = 0; i < rs->rs_rx_traces; i++) {
  			j = rs->rs_rx_trace[i];
  			t.rx_trace_pos[i] = j;
  			t.rx_trace[i] = inc->i_rx_lat_trace[j + 1] -
  					  inc->i_rx_lat_trace[j];
  		}
  
  		ret = put_cmsg(msg, SOL_RDS, RDS_CMSG_RXPATH_LATENCY,
  			       sizeof(t), &t);
  		if (ret)
  			goto out;
  	}
  
  out:
  	return ret;
bdbe6fbc6   Andy Grover   RDS: recv.c
579
  }
1b7841404   Ying Xue   net: Remove iocb ...
580
581
  int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  		int msg_flags)
bdbe6fbc6   Andy Grover   RDS: recv.c
582
583
584
585
586
  {
  	struct sock *sk = sock->sk;
  	struct rds_sock *rs = rds_sk_to_rs(sk);
  	long timeo;
  	int ret = 0, nonblock = msg_flags & MSG_DONTWAIT;
342dfc306   Steffen Hurrle   net: add build-ti...
587
  	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
bdbe6fbc6   Andy Grover   RDS: recv.c
588
589
590
591
592
593
594
595
596
597
  	struct rds_incoming *inc = NULL;
  
  	/* udp_recvmsg()->sock_recvtimeo() gets away without locking too.. */
  	timeo = sock_rcvtimeo(sk, nonblock);
  
  	rdsdebug("size %zu flags 0x%x timeo %ld
  ", size, msg_flags, timeo);
  
  	if (msg_flags & MSG_OOB)
  		goto out;
edacaeae5   Andy Grover   RDS: Fix completi...
598
599
600
601
602
603
  	while (1) {
  		/* If there are pending notifications, do those - and nothing else */
  		if (!list_empty(&rs->rs_notify_queue)) {
  			ret = rds_notify_queue_get(rs, msg);
  			break;
  		}
bdbe6fbc6   Andy Grover   RDS: recv.c
604

edacaeae5   Andy Grover   RDS: Fix completi...
605
606
607
608
  		if (rs->rs_cong_notify) {
  			ret = rds_notify_cong(rs, msg);
  			break;
  		}
bdbe6fbc6   Andy Grover   RDS: recv.c
609

bdbe6fbc6   Andy Grover   RDS: recv.c
610
611
612
613
614
  		if (!rds_next_incoming(rs, &inc)) {
  			if (nonblock) {
  				ret = -EAGAIN;
  				break;
  			}
aa3951451   Eric Dumazet   net: sk_sleep() h...
615
  			timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
f64f9e719   Joe Perches   net: Move && and ...
616
617
618
  					(!list_empty(&rs->rs_notify_queue) ||
  					 rs->rs_cong_notify ||
  					 rds_next_incoming(rs, &inc)), timeo);
bdbe6fbc6   Andy Grover   RDS: recv.c
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
  			rdsdebug("recvmsg woke inc %p timeo %ld
  ", inc,
  				 timeo);
  			if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  				continue;
  
  			ret = timeo;
  			if (ret == 0)
  				ret = -ETIMEDOUT;
  			break;
  		}
  
  		rdsdebug("copying inc %p from %pI4:%u to user
  ", inc,
  			 &inc->i_conn->c_faddr,
  			 ntohs(inc->i_hdr.h_sport));
c0371da60   Al Viro   put iov_iter into...
635
  		ret = inc->i_conn->c_trans->inc_copy_to_user(inc, &msg->msg_iter);
bdbe6fbc6   Andy Grover   RDS: recv.c
636
637
638
639
640
641
642
643
644
645
646
647
  		if (ret < 0)
  			break;
  
  		/*
  		 * if the message we just copied isn't at the head of the
  		 * recv queue then someone else raced us to return it, try
  		 * to get the next message.
  		 */
  		if (!rds_still_queued(rs, inc, !(msg_flags & MSG_PEEK))) {
  			rds_inc_put(inc);
  			inc = NULL;
  			rds_stats_inc(s_recv_deliver_raced);
dc88e3b4c   Al Viro   rds: make use of ...
648
  			iov_iter_revert(&msg->msg_iter, ret);
bdbe6fbc6   Andy Grover   RDS: recv.c
649
650
651
652
653
654
655
656
  			continue;
  		}
  
  		if (ret < be32_to_cpu(inc->i_hdr.h_len)) {
  			if (msg_flags & MSG_TRUNC)
  				ret = be32_to_cpu(inc->i_hdr.h_len);
  			msg->msg_flags |= MSG_TRUNC;
  		}
5711f8b35   santosh.shilimkar@oracle.com   RDS: Add support ...
657
  		if (rds_cmsg_recv(inc, msg, rs)) {
bdbe6fbc6   Andy Grover   RDS: recv.c
658
659
660
661
662
  			ret = -EFAULT;
  			goto out;
  		}
  
  		rds_stats_inc(s_recv_delivered);
bdbe6fbc6   Andy Grover   RDS: recv.c
663
664
665
666
667
  		if (sin) {
  			sin->sin_family = AF_INET;
  			sin->sin_port = inc->i_hdr.h_sport;
  			sin->sin_addr.s_addr = inc->i_saddr;
  			memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
06b6a1cf6   Weiping Pan   rds: set correct ...
668
  			msg->msg_namelen = sizeof(*sin);
bdbe6fbc6   Andy Grover   RDS: recv.c
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
  		}
  		break;
  	}
  
  	if (inc)
  		rds_inc_put(inc);
  
  out:
  	return ret;
  }
  
  /*
   * The socket is being shut down and we're asked to drop messages that were
   * queued for recvmsg.  The caller has unbound the socket so the receive path
   * won't queue any more incoming fragments or messages on the socket.
   */
  void rds_clear_recv_queue(struct rds_sock *rs)
  {
  	struct sock *sk = rds_rs_to_sk(rs);
  	struct rds_incoming *inc, *tmp;
  	unsigned long flags;
  
  	write_lock_irqsave(&rs->rs_recv_lock, flags);
  	list_for_each_entry_safe(inc, tmp, &rs->rs_recv_queue, i_item) {
  		rds_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
  				      -be32_to_cpu(inc->i_hdr.h_len),
  				      inc->i_hdr.h_dport);
  		list_del_init(&inc->i_item);
  		rds_inc_put(inc);
  	}
  	write_unlock_irqrestore(&rs->rs_recv_lock, flags);
  }
  
  /*
   * inc->i_saddr isn't used here because it is only set in the receive
   * path.
   */
  void rds_inc_info_copy(struct rds_incoming *inc,
  		       struct rds_info_iterator *iter,
  		       __be32 saddr, __be32 daddr, int flip)
  {
  	struct rds_info_message minfo;
  
  	minfo.seq = be64_to_cpu(inc->i_hdr.h_sequence);
  	minfo.len = be32_to_cpu(inc->i_hdr.h_len);
  
  	if (flip) {
  		minfo.laddr = daddr;
  		minfo.faddr = saddr;
  		minfo.lport = inc->i_hdr.h_dport;
  		minfo.fport = inc->i_hdr.h_sport;
  	} else {
  		minfo.laddr = saddr;
  		minfo.faddr = daddr;
  		minfo.lport = inc->i_hdr.h_sport;
  		minfo.fport = inc->i_hdr.h_dport;
  	}
4116def23   Kangjie Lu   rds: fix an infol...
726
  	minfo.flags = 0;
bdbe6fbc6   Andy Grover   RDS: recv.c
727
728
  	rds_info_copy(iter, &minfo, sizeof(minfo));
  }