xref: /illumos-gate/usr/src/uts/common/io/ib/clients/rdsv3/rds_recv.c (revision 80166370c2a40fb8d363daf1c827e4ddc7d69cf6)
1 /*
2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3  */
4 
5 /*
6  * This file contains code imported from the OFED rds source file recv.c
7  * Oracle elects to have and use the contents of rds_recv.c under and governed
8  * by the OpenIB.org BSD license (see below for full license text). However,
9  * the following notice accompanied the original version of this file:
10  */
11 
12 /*
13  * Copyright (c) 2006 Oracle.  All rights reserved.
14  *
15  * This software is available to you under a choice of one of two
16  * licenses.  You may choose to be licensed under the terms of the GNU
17  * General Public License (GPL) Version 2, available from the file
18  * COPYING in the main directory of this source tree, or the
19  * OpenIB.org BSD license below:
20  *
21  *     Redistribution and use in source and binary forms, with or
22  *     without modification, are permitted provided that the following
23  *     conditions are met:
24  *
25  *      - Redistributions of source code must retain the above
26  *        copyright notice, this list of conditions and the following
27  *        disclaimer.
28  *
29  *      - Redistributions in binary form must reproduce the above
30  *        copyright notice, this list of conditions and the following
31  *        disclaimer in the documentation and/or other materials
32  *        provided with the distribution.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
38  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
39  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41  * SOFTWARE.
42  *
43  */
44 #include <sys/rds.h>
45 
46 #include <sys/ib/clients/rdsv3/rdsv3.h>
47 #include <sys/ib/clients/rdsv3/rdma.h>
48 #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
49 
50 void
51 rdsv3_inc_init(struct rdsv3_incoming *inc, struct rdsv3_connection *conn,
52     uint32_be_t saddr)
53 {
54 	RDSV3_DPRINTF5("rdsv3_inc_init", "Enter(inc: %p, conn: %p)", inc, conn);
55 	inc->i_refcount = 1;
56 	list_link_init(&inc->i_item);
57 	inc->i_conn = conn;
58 	inc->i_saddr = saddr;
59 	inc->i_rdma_cookie = 0;
60 }
61 
62 void
63 rdsv3_inc_addref(struct rdsv3_incoming *inc)
64 {
65 	RDSV3_DPRINTF4("rdsv3_inc_addref",
66 	    "addref inc %p ref %d", inc, atomic_get(&inc->i_refcount));
67 	atomic_add_32(&inc->i_refcount, 1);
68 }
69 
70 void
71 rdsv3_inc_put(struct rdsv3_incoming *inc)
72 {
73 	RDSV3_DPRINTF4("rdsv3_inc_put", "put inc %p ref %d",
74 	    inc, atomic_get(&inc->i_refcount));
75 	if (atomic_dec_and_test(&inc->i_refcount)) {
76 		ASSERT(!list_link_active(&inc->i_item));
77 
78 		inc->i_conn->c_trans->inc_free(inc);
79 	}
80 }
81 
82 /*ARGSUSED*/
83 static void
84 rdsv3_recv_rcvbuf_delta(struct rdsv3_sock *rs, struct rsock *sk,
85     struct rdsv3_cong_map *map,
86     int delta, uint16_be_t port)
87 {
88 	int now_congested;
89 
90 	RDSV3_DPRINTF4("rdsv3_recv_rcvbuf_delta",
91 	    "Enter(rs: %p, map: %p, delta: %d, port: %d)",
92 	    rs, map, delta, port);
93 
94 	if (delta == 0)
95 		return;
96 
97 	rs->rs_rcv_bytes += delta;
98 	now_congested = rs->rs_rcv_bytes > rdsv3_sk_rcvbuf(rs);
99 
100 	RDSV3_DPRINTF5("rdsv3_recv_rcvbuf_delta",
101 	    "rs %p (%u.%u.%u.%u:%u) recv bytes %d buf %d "
102 	    "now_cong %d delta %d",
103 	    rs, NIPQUAD(rs->rs_bound_addr),
104 	    (int)ntohs(rs->rs_bound_port), rs->rs_rcv_bytes,
105 	    rdsv3_sk_rcvbuf(rs), now_congested, delta);
106 
107 	/* wasn't -> am congested */
108 	if (!rs->rs_congested && now_congested) {
109 		rs->rs_congested = 1;
110 		rdsv3_cong_set_bit(map, port);
111 		rdsv3_cong_queue_updates(map);
112 	}
113 	/* was -> aren't congested */
114 	/*
115 	 * Require more free space before reporting uncongested to prevent
116 	 * bouncing cong/uncong state too often
117 	 */
118 	else if (rs->rs_congested &&
119 	    (rs->rs_rcv_bytes < (rdsv3_sk_rcvbuf(rs)/2))) {
120 		rs->rs_congested = 0;
121 		rdsv3_cong_clear_bit(map, port);
122 		rdsv3_cong_queue_updates(map);
123 	}
124 
125 	/* do nothing if no change in cong state */
126 
127 	RDSV3_DPRINTF4("rdsv3_recv_rcvbuf_delta", "Return(rs: %p)", rs);
128 }
129 
130 /*
131  * Process all extension headers that come with this message.
132  */
133 static void
134 rdsv3_recv_incoming_exthdrs(struct rdsv3_incoming *inc, struct rdsv3_sock *rs)
135 {
136 	struct rdsv3_header *hdr = &inc->i_hdr;
137 	unsigned int pos = 0, type, len;
138 	union {
139 		struct rdsv3_ext_header_version version;
140 		struct rdsv3_ext_header_rdma rdma;
141 		struct rdsv3_ext_header_rdma_dest rdma_dest;
142 	} buffer;
143 
144 	RDSV3_DPRINTF4("rdsv3_recv_incoming_exthdrs", "Enter");
145 	while (1) {
146 		len = sizeof (buffer);
147 		type = rdsv3_message_next_extension(hdr, &pos, &buffer, &len);
148 		if (type == RDSV3_EXTHDR_NONE)
149 			break;
150 		RDSV3_DPRINTF4("recv_incoming_exthdrs", "type %d", type);
151 		/* Process extension header here */
152 		switch (type) {
153 		case RDSV3_EXTHDR_RDMA:
154 			rdsv3_rdma_unuse(rs, ntohl(buffer.rdma.h_rdma_rkey),
155 			    0);
156 			break;
157 
158 		case RDSV3_EXTHDR_RDMA_DEST:
159 			/*
160 			 * We ignore the size for now. We could stash it
161 			 * somewhere and use it for error checking.
162 			 */
163 			inc->i_rdma_cookie = rdsv3_rdma_make_cookie(
164 			    ntohl(buffer.rdma_dest.h_rdma_rkey),
165 			    ntohl(buffer.rdma_dest.h_rdma_offset));
166 
167 			break;
168 		}
169 	}
170 	RDSV3_DPRINTF4("rdsv3_recv_incoming_exthdrs", "Return");
171 }
172 
173 /*
174  * The transport must make sure that this is serialized against other
175  * rx and conn reset on this specific conn.
176  *
177  * We currently assert that only one fragmented message will be sent
178  * down a connection at a time.  This lets us reassemble in the conn
179  * instead of per-flow which means that we don't have to go digging through
180  * flows to tear down partial reassembly progress on conn failure and
181  * we save flow lookup and locking for each frag arrival.  It does mean
182  * that small messages will wait behind large ones.  Fragmenting at all
183  * is only to reduce the memory consumption of pre-posted buffers.
184  *
185  * The caller passes in saddr and daddr instead of us getting it from the
186  * conn.  This lets loopback, who only has one conn for both directions,
187  * tell us which roles the addrs in the conn are playing for this message.
188  */
189 /* ARGSUSED */
190 void
191 rdsv3_recv_incoming(struct rdsv3_connection *conn, uint32_be_t saddr,
192     uint32_be_t daddr, struct rdsv3_incoming *inc, int gfp)
193 {
194 	struct rdsv3_sock *rs = NULL;
195 	struct rsock *sk;
196 
197 	inc->i_conn = conn;
198 	inc->i_rx_jiffies = jiffies;
199 
200 	RDSV3_DPRINTF5("rdsv3_recv_incoming",
201 	    "conn %p next %llu inc %p seq %llu len %u sport %u dport %u "
202 	    "flags 0x%x rx_jiffies %lu", conn,
203 	    (unsigned long long)conn->c_next_rx_seq,
204 	    inc,
205 	    (unsigned long long)ntohll(inc->i_hdr.h_sequence),
206 	    ntohl(inc->i_hdr.h_len),
207 	    ntohs(inc->i_hdr.h_sport),
208 	    ntohs(inc->i_hdr.h_dport),
209 	    inc->i_hdr.h_flags,
210 	    inc->i_rx_jiffies);
211 
212 	/*
213 	 * Sequence numbers should only increase.  Messages get their
214 	 * sequence number as they're queued in a sending conn.  They
215 	 * can be dropped, though, if the sending socket is closed before
216 	 * they hit the wire.  So sequence numbers can skip forward
217 	 * under normal operation.  They can also drop back in the conn
218 	 * failover case as previously sent messages are resent down the
219 	 * new instance of a conn.  We drop those, otherwise we have
220 	 * to assume that the next valid seq does not come after a
221 	 * hole in the fragment stream.
222 	 *
223 	 * The headers don't give us a way to realize if fragments of
224 	 * a message have been dropped.  We assume that frags that arrive
225 	 * to a flow are part of the current message on the flow that is
226 	 * being reassembled.  This means that senders can't drop messages
227 	 * from the sending conn until all their frags are sent.
228 	 *
229 	 * XXX we could spend more on the wire to get more robust failure
230 	 * detection, arguably worth it to avoid data corruption.
231 	 */
232 	if (ntohll(inc->i_hdr.h_sequence) < conn->c_next_rx_seq &&
233 	    (inc->i_hdr.h_flags & RDSV3_FLAG_RETRANSMITTED)) {
234 		rdsv3_stats_inc(s_recv_drop_old_seq);
235 		goto out;
236 	}
237 	conn->c_next_rx_seq = ntohll(inc->i_hdr.h_sequence) + 1;
238 
239 	if (rdsv3_sysctl_ping_enable && inc->i_hdr.h_dport == 0) {
240 		rdsv3_stats_inc(s_recv_ping);
241 		(void) rdsv3_send_pong(conn, inc->i_hdr.h_sport);
242 		goto out;
243 	}
244 
245 	rs = rdsv3_find_bound(conn, inc->i_hdr.h_dport);
246 	if (!rs) {
247 		rdsv3_stats_inc(s_recv_drop_no_sock);
248 		goto out;
249 	}
250 
251 	/* Process extension headers */
252 	rdsv3_recv_incoming_exthdrs(inc, rs);
253 
254 	/* We can be racing with rdsv3_release() which marks the socket dead. */
255 	sk = rdsv3_rs_to_sk(rs);
256 
257 	/* serialize with rdsv3_release -> sock_orphan */
258 	rw_enter(&rs->rs_recv_lock, RW_WRITER);
259 	if (!rdsv3_sk_sock_flag(sk, SOCK_DEAD)) {
260 		int error, bytes;
261 		RDSV3_DPRINTF5("rdsv3_recv_incoming",
262 		    "adding inc %p to rs %p's recv queue", inc, rs);
263 		rdsv3_stats_inc(s_recv_queued);
264 		rdsv3_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
265 		    ntohl(inc->i_hdr.h_len),
266 		    inc->i_hdr.h_dport);
267 		rdsv3_inc_addref(inc);
268 		list_insert_tail(&rs->rs_recv_queue, inc);
269 		bytes = rs->rs_rcv_bytes;
270 		rw_exit(&rs->rs_recv_lock);
271 
272 		__rdsv3_wake_sk_sleep(sk);
273 
274 		/* wake up anyone waiting in poll */
275 		sk->sk_upcalls->su_recv(sk->sk_upper_handle, NULL,
276 		    bytes, 0, &error, NULL);
277 		if (error != 0) {
278 			RDSV3_DPRINTF2("rdsv3_recv_incoming",
279 			    "su_recv returned: %d", error);
280 		}
281 	} else {
282 		rdsv3_stats_inc(s_recv_drop_dead_sock);
283 		rw_exit(&rs->rs_recv_lock);
284 	}
285 
286 out:
287 	if (rs)
288 		rdsv3_sock_put(rs);
289 }
290 
291 /*
292  * be very careful here.  This is being called as the condition in
293  * wait_event_*() needs to cope with being called many times.
294  */
295 static int
296 rdsv3_next_incoming(struct rdsv3_sock *rs, struct rdsv3_incoming **inc)
297 {
298 	if (!*inc) {
299 		rw_enter(&rs->rs_recv_lock, RW_READER);
300 		if (!list_is_empty(&rs->rs_recv_queue)) {
301 			*inc = list_head(&rs->rs_recv_queue);
302 			rdsv3_inc_addref(*inc);
303 		}
304 		rw_exit(&rs->rs_recv_lock);
305 	}
306 
307 	return (*inc != NULL);
308 }
309 
310 static int
311 rdsv3_still_queued(struct rdsv3_sock *rs, struct rdsv3_incoming *inc,
312     int drop)
313 {
314 	struct rsock *sk = rdsv3_rs_to_sk(rs);
315 	int ret = 0;
316 
317 	RDSV3_DPRINTF4("rdsv3_still_queued", "Enter rs: %p inc: %p drop: %d",
318 	    rs, inc, drop);
319 
320 	rw_enter(&rs->rs_recv_lock, RW_WRITER);
321 	if (list_link_active(&inc->i_item)) {
322 		ret = 1;
323 		if (drop) {
324 			/* XXX make sure this i_conn is reliable */
325 			rdsv3_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
326 			    -ntohl(inc->i_hdr.h_len),
327 			    inc->i_hdr.h_dport);
328 			list_remove_node(&inc->i_item);
329 			rdsv3_inc_put(inc);
330 		}
331 	}
332 	rw_exit(&rs->rs_recv_lock);
333 
334 	RDSV3_DPRINTF5("rdsv3_still_queued",
335 	    "inc %p rs %p still %d dropped %d", inc, rs, ret, drop);
336 	return (ret);
337 }
338 
339 /*
340  * Pull errors off the error queue.
341  * If msghdr is NULL, we will just purge the error queue.
342  */
343 int
344 rdsv3_notify_queue_get(struct rdsv3_sock *rs, struct msghdr *msghdr)
345 {
346 	struct rdsv3_notifier *notifier;
347 	struct rds_rdma_notify cmsg;
348 	unsigned int count = 0, max_messages = ~0U;
349 	list_t copy;
350 	int err = 0;
351 
352 	RDSV3_DPRINTF4("rdsv3_notify_queue_get", "Enter(rs: %p)", rs);
353 
354 	list_create(&copy, sizeof (struct rdsv3_notifier),
355 	    offsetof(struct rdsv3_notifier, n_list));
356 
357 
358 	/*
359 	 * put_cmsg copies to user space and thus may sleep. We can't do this
360 	 * with rs_lock held, so first grab as many notifications as we can
361 	 * stuff
362 	 * in the user provided cmsg buffer. We don't try to copy more, to avoid
363 	 * losing notifications - except when the buffer is so small that
364 	 * it wouldn't
365 	 * even hold a single notification. Then we give him as much of this
366 	 * single
367 	 * msg as we can squeeze in, and set MSG_CTRUNC.
368 	 */
369 	if (msghdr) {
370 		max_messages =
371 		    msghdr->msg_controllen / CMSG_SPACE(sizeof (cmsg));
372 		if (!max_messages)
373 			max_messages = 1;
374 	}
375 
376 	mutex_enter(&rs->rs_lock);
377 	while (!list_is_empty(&rs->rs_notify_queue) && count < max_messages) {
378 		notifier = list_remove_head(&rs->rs_notify_queue);
379 		list_insert_tail(&copy, notifier);
380 		count++;
381 	}
382 	mutex_exit(&rs->rs_lock);
383 
384 	if (!count)
385 		return (0);
386 
387 	while (!list_is_empty(&copy)) {
388 		notifier = list_remove_head(&copy);
389 
390 		if (msghdr) {
391 			cmsg.user_token = notifier->n_user_token;
392 			cmsg.status  = notifier->n_status;
393 
394 			err = rdsv3_put_cmsg(msghdr, SOL_RDS,
395 			    RDS_CMSG_RDMA_STATUS, sizeof (cmsg), &cmsg);
396 			if (err)
397 				break;
398 		}
399 
400 		kmem_free(notifier, sizeof (struct rdsv3_notifier));
401 	}
402 
403 	/*
404 	 * If we bailed out because of an error in put_cmsg,
405 	 * we may be left with one or more notifications that we
406 	 * didn't process. Return them to the head of the list.
407 	 */
408 	if (!list_is_empty(&copy)) {
409 		mutex_enter(&rs->rs_lock);
410 		list_splice(&copy, &rs->rs_notify_queue);
411 		mutex_exit(&rs->rs_lock);
412 	}
413 
414 	RDSV3_DPRINTF4("rdsv3_notify_queue_get", "Return(rs: %p)", rs);
415 
416 	return (err);
417 }
418 
419 /*
420  * Queue a congestion notification
421  */
422 static int
423 rdsv3_notify_cong(struct rdsv3_sock *rs, struct msghdr *msghdr)
424 {
425 	uint64_t notify = rs->rs_cong_notify;
426 	int err;
427 
428 	err = rdsv3_put_cmsg(msghdr, SOL_RDS, RDS_CMSG_CONG_UPDATE,
429 	    sizeof (notify), &notify);
430 	if (err)
431 		return (err);
432 
433 	mutex_enter(&rs->rs_lock);
434 	rs->rs_cong_notify &= ~notify;
435 	mutex_exit(&rs->rs_lock);
436 
437 	return (0);
438 }
439 
440 /*
441  * Receive any control messages.
442  */
443 static int
444 rdsv3_cmsg_recv(struct rdsv3_incoming *inc, struct msghdr *msg)
445 {
446 	return (rdsv3_put_cmsg(msg, SOL_RDS, RDS_CMSG_RDMA_DEST,
447 	    sizeof (inc->i_rdma_cookie), &inc->i_rdma_cookie));
448 }
449 
450 int
451 rdsv3_recvmsg(struct rdsv3_sock *rs, uio_t *uio,
452     struct nmsghdr *msg, size_t size, int msg_flags)
453 {
454 	struct rsock *sk = rdsv3_rs_to_sk(rs);
455 	long timeo;
456 	int ret = 0;
457 	struct sockaddr_in *sin = NULL;
458 	struct rdsv3_incoming *inc = NULL;
459 	boolean_t nonblock = B_FALSE;
460 
461 	RDSV3_DPRINTF4("rdsv3_recvmsg",
462 	    "Enter(rs: %p size: %d msg_flags: 0x%x)", rs, size, msg_flags);
463 
464 	if ((uio->uio_fmode & (FNDELAY | FNONBLOCK)) ||
465 	    (msg_flags & MSG_DONTWAIT))
466 		nonblock = B_TRUE;
467 
468 	/* udp_recvmsg()->sock_recvtimeo() gets away without locking too.. */
469 	timeo = rdsv3_rcvtimeo(sk, nonblock);
470 
471 	if (msg_flags & MSG_OOB)
472 		goto out;
473 
474 	/* mark the first cmsg position */
475 	if (msg) {
476 		msg->msg_control = NULL;
477 	}
478 
479 	while (1) {
480 		/*
481 		 * If there are pending notifications, do those -
482 		 * and nothing else
483 		 */
484 		if (!list_is_empty(&rs->rs_notify_queue)) {
485 			ret = rdsv3_notify_queue_get(rs, msg);
486 
487 			if (msg && msg->msg_namelen) {
488 				sin = kmem_zalloc(sizeof (struct sockaddr_in),
489 				    KM_SLEEP);
490 				sin->sin_family = AF_INET_OFFLOAD;
491 				if (inc) {
492 					sin->sin_port = inc->i_hdr.h_sport;
493 					sin->sin_addr.s_addr = inc->i_saddr;
494 				}
495 				msg->msg_namelen = sizeof (struct sockaddr_in);
496 				msg->msg_name = sin;
497 			}
498 			break;
499 		}
500 
501 		if (rs->rs_cong_notify) {
502 			ret = rdsv3_notify_cong(rs, msg);
503 			goto out;
504 		}
505 
506 		if (!rdsv3_next_incoming(rs, &inc)) {
507 			if (nonblock) {
508 				ret = -EAGAIN;
509 				break;
510 			}
511 
512 			RDSV3_DPRINTF3("rdsv3_recvmsg",
513 			    "Before wait (rs: %p)", rs);
514 
515 #if 0
516 			ret = rdsv3_wait_sig(sk->sk_sleep,
517 			    !(list_is_empty(&rs->rs_notify_queue) &&
518 			    !rs->rs_cong_notify &&
519 			    !rdsv3_next_incoming(rs, &inc)));
520 			if (ret == 0) {
521 				/* signal/timeout pending */
522 				RDSV3_DPRINTF2("rdsv3_recvmsg",
523 				    "woke due to signal");
524 				ret = -ERESTART;
525 			}
526 #else
527 			mutex_enter(&sk->sk_sleep->waitq_mutex);
528 			sk->sk_sleep->waitq_waiters++;
529 			while ((list_is_empty(&rs->rs_notify_queue) &&
530 			    !rs->rs_cong_notify &&
531 			    !rdsv3_next_incoming(rs, &inc))) {
532 				ret = cv_wait_sig(&sk->sk_sleep->waitq_cv,
533 				    &sk->sk_sleep->waitq_mutex);
534 				if (ret == 0) {
535 					/* signal/timeout pending */
536 					RDSV3_DPRINTF2("rdsv3_recvmsg",
537 					    "woke due to signal");
538 					ret = -ERESTART;
539 					break;
540 				}
541 			}
542 			sk->sk_sleep->waitq_waiters--;
543 			mutex_exit(&sk->sk_sleep->waitq_mutex);
544 #endif
545 
546 			RDSV3_DPRINTF5("rdsv3_recvmsg",
547 			    "recvmsg woke rs: %p inc %p ret %d",
548 			    rs, inc, -ret);
549 
550 			if (ret < 0)
551 				break;
552 
553 			/*
554 			 * if the wakeup was due to rs_notify_queue or
555 			 * rs_cong_notify then we need to handle those first.
556 			 */
557 			continue;
558 		}
559 
560 		RDSV3_DPRINTF5("rdsv3_recvmsg",
561 		    "copying inc %p from %u.%u.%u.%u:%u to user", inc,
562 		    NIPQUAD(inc->i_conn->c_faddr),
563 		    ntohs(inc->i_hdr.h_sport));
564 
565 		ret = inc->i_conn->c_trans->inc_copy_to_user(inc, uio, size);
566 		if (ret < 0)
567 			break;
568 
569 		/*
570 		 * if the message we just copied isn't at the head of the
571 		 * recv queue then someone else raced us to return it, try
572 		 * to get the next message.
573 		 */
574 		if (!rdsv3_still_queued(rs, inc, !(msg_flags & MSG_PEEK))) {
575 			rdsv3_inc_put(inc);
576 			inc = NULL;
577 			rdsv3_stats_inc(s_recv_deliver_raced);
578 			continue;
579 		}
580 
581 		if (ret < ntohl(inc->i_hdr.h_len)) {
582 			if (msg_flags & MSG_TRUNC)
583 				ret = ntohl(inc->i_hdr.h_len);
584 			msg->msg_flags |= MSG_TRUNC;
585 		}
586 
587 		if (rdsv3_cmsg_recv(inc, msg)) {
588 			ret = -EFAULT;
589 			goto out;
590 		}
591 
592 		rdsv3_stats_inc(s_recv_delivered);
593 
594 		if (msg->msg_namelen) {
595 			sin = kmem_alloc(sizeof (struct sockaddr_in), KM_SLEEP);
596 			sin->sin_family = AF_INET_OFFLOAD;
597 			sin->sin_port = inc->i_hdr.h_sport;
598 			sin->sin_addr.s_addr = inc->i_saddr;
599 			(void) memset(sin->sin_zero, 0,
600 			    sizeof (sin->sin_zero));
601 			msg->msg_namelen = sizeof (struct sockaddr_in);
602 			msg->msg_name = sin;
603 		}
604 		break;
605 	}
606 
607 	if (inc)
608 		rdsv3_inc_put(inc);
609 
610 out:
611 	RDSV3_DPRINTF4("rdsv3_recvmsg", "Return(rs: %p, ret: %d)", rs, ret);
612 
613 	return (ret);
614 }
615 
616 /*
617  * The socket is being shut down and we're asked to drop messages that were
618  * queued for recvmsg.  The caller has unbound the socket so the receive path
619  * won't queue any more incoming fragments or messages on the socket.
620  */
621 void
622 rdsv3_clear_recv_queue(struct rdsv3_sock *rs)
623 {
624 	struct rsock *sk = rdsv3_rs_to_sk(rs);
625 	struct rdsv3_incoming *inc, *tmp;
626 
627 	RDSV3_DPRINTF4("rdsv3_clear_recv_queue", "Enter(rs: %p)", rs);
628 
629 	rw_enter(&rs->rs_recv_lock, RW_WRITER);
630 	RDSV3_FOR_EACH_LIST_NODE_SAFE(inc, tmp, &rs->rs_recv_queue, i_item) {
631 		rdsv3_recv_rcvbuf_delta(rs, sk, inc->i_conn->c_lcong,
632 		    -ntohl(inc->i_hdr.h_len),
633 		    inc->i_hdr.h_dport);
634 		list_remove_node(&inc->i_item);
635 		rdsv3_inc_put(inc);
636 	}
637 	rw_exit(&rs->rs_recv_lock);
638 
639 	RDSV3_DPRINTF4("rdsv3_clear_recv_queue", "Return(rs: %p)", rs);
640 }
641 
642 /*
643  * inc->i_saddr isn't used here because it is only set in the receive
644  * path.
645  */
646 void
647 rdsv3_inc_info_copy(struct rdsv3_incoming *inc,
648     struct rdsv3_info_iterator *iter,
649     uint32_be_t saddr, uint32_be_t daddr, int flip)
650 {
651 	struct rds_info_message minfo;
652 
653 	minfo.seq = ntohll(inc->i_hdr.h_sequence);
654 	minfo.len = ntohl(inc->i_hdr.h_len);
655 
656 	if (flip) {
657 		minfo.laddr = daddr;
658 		minfo.faddr = saddr;
659 		minfo.lport = inc->i_hdr.h_dport;
660 		minfo.fport = inc->i_hdr.h_sport;
661 	} else {
662 		minfo.laddr = saddr;
663 		minfo.faddr = daddr;
664 		minfo.lport = inc->i_hdr.h_sport;
665 		minfo.fport = inc->i_hdr.h_dport;
666 	}
667 
668 	rdsv3_info_copy(iter, &minfo, sizeof (minfo));
669 }
670