xref: /onnv-gate/usr/src/uts/common/io/ib/clients/rdsv3/loop.c (revision 12763:4b30642bc04e)
112198SEiji.Ota@Sun.COM /*
212198SEiji.Ota@Sun.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
312198SEiji.Ota@Sun.COM  */
412198SEiji.Ota@Sun.COM 
512198SEiji.Ota@Sun.COM /*
6*12763SGiri.Adari@Sun.COM  * This file contains code imported from the OFED rds source file loop.c
7*12763SGiri.Adari@Sun.COM  * Oracle elects to have and use the contents of loop.c under and governed
8*12763SGiri.Adari@Sun.COM  * by the OpenIB.org BSD license (see below for full license text). However,
9*12763SGiri.Adari@Sun.COM  * the following notice accompanied the original version of this file:
10*12763SGiri.Adari@Sun.COM  */
11*12763SGiri.Adari@Sun.COM 
12*12763SGiri.Adari@Sun.COM /*
1312198SEiji.Ota@Sun.COM  * Copyright (c) 2006 Oracle.  All rights reserved.
1412198SEiji.Ota@Sun.COM  *
1512198SEiji.Ota@Sun.COM  * This software is available to you under a choice of one of two
1612198SEiji.Ota@Sun.COM  * licenses.  You may choose to be licensed under the terms of the GNU
1712198SEiji.Ota@Sun.COM  * General Public License (GPL) Version 2, available from the file
1812198SEiji.Ota@Sun.COM  * COPYING in the main directory of this source tree, or the
1912198SEiji.Ota@Sun.COM  * OpenIB.org BSD license below:
2012198SEiji.Ota@Sun.COM  *
2112198SEiji.Ota@Sun.COM  *     Redistribution and use in source and binary forms, with or
2212198SEiji.Ota@Sun.COM  *     without modification, are permitted provided that the following
2312198SEiji.Ota@Sun.COM  *     conditions are met:
2412198SEiji.Ota@Sun.COM  *
2512198SEiji.Ota@Sun.COM  *      - Redistributions of source code must retain the above
2612198SEiji.Ota@Sun.COM  *        copyright notice, this list of conditions and the following
2712198SEiji.Ota@Sun.COM  *        disclaimer.
2812198SEiji.Ota@Sun.COM  *
2912198SEiji.Ota@Sun.COM  *      - Redistributions in binary form must reproduce the above
3012198SEiji.Ota@Sun.COM  *        copyright notice, this list of conditions and the following
3112198SEiji.Ota@Sun.COM  *        disclaimer in the documentation and/or other materials
3212198SEiji.Ota@Sun.COM  *        provided with the distribution.
3312198SEiji.Ota@Sun.COM  *
3412198SEiji.Ota@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3512198SEiji.Ota@Sun.COM  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3612198SEiji.Ota@Sun.COM  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3712198SEiji.Ota@Sun.COM  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
3812198SEiji.Ota@Sun.COM  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3912198SEiji.Ota@Sun.COM  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
4012198SEiji.Ota@Sun.COM  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
4112198SEiji.Ota@Sun.COM  * SOFTWARE.
4212198SEiji.Ota@Sun.COM  *
4312198SEiji.Ota@Sun.COM  */
4412198SEiji.Ota@Sun.COM #include <sys/rds.h>
4512198SEiji.Ota@Sun.COM 
4612198SEiji.Ota@Sun.COM #include <sys/ib/clients/rdsv3/rdsv3.h>
4712198SEiji.Ota@Sun.COM #include <sys/ib/clients/rdsv3/loop.h>
4812198SEiji.Ota@Sun.COM #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
4912198SEiji.Ota@Sun.COM 
5012198SEiji.Ota@Sun.COM kmutex_t loop_conns_lock;
5112198SEiji.Ota@Sun.COM list_t loop_conns;
5212198SEiji.Ota@Sun.COM 
5312198SEiji.Ota@Sun.COM /*
5412198SEiji.Ota@Sun.COM  * This 'loopback' transport is a special case for flows that originate
5512198SEiji.Ota@Sun.COM  * and terminate on the same machine.
5612198SEiji.Ota@Sun.COM  *
5712198SEiji.Ota@Sun.COM  * Connection build-up notices if the destination address is thought of
5812198SEiji.Ota@Sun.COM  * as a local address by a transport.  At that time it decides to use the
5912198SEiji.Ota@Sun.COM  * loopback transport instead of the bound transport of the sending socket.
6012198SEiji.Ota@Sun.COM  *
6112198SEiji.Ota@Sun.COM  * The loopback transport's sending path just hands the sent rds_message
6212198SEiji.Ota@Sun.COM  * straight to the receiving path via an embedded rds_incoming.
6312198SEiji.Ota@Sun.COM  */
6412198SEiji.Ota@Sun.COM 
6512198SEiji.Ota@Sun.COM /*
6612198SEiji.Ota@Sun.COM  * Usually a message transits both the sender and receiver's conns as it
6712198SEiji.Ota@Sun.COM  * flows to the receiver.  In the loopback case, though, the receive path
6812198SEiji.Ota@Sun.COM  * is handed the sending conn so the sense of the addresses is reversed.
6912198SEiji.Ota@Sun.COM  */
7012198SEiji.Ota@Sun.COM static int
rdsv3_loop_xmit(struct rdsv3_connection * conn,struct rdsv3_message * rm,unsigned int hdr_off,unsigned int sg,unsigned int off)7112198SEiji.Ota@Sun.COM rdsv3_loop_xmit(struct rdsv3_connection *conn, struct rdsv3_message *rm,
7212198SEiji.Ota@Sun.COM     unsigned int hdr_off, unsigned int sg,
7312198SEiji.Ota@Sun.COM     unsigned int off)
7412198SEiji.Ota@Sun.COM {
7512676SEiji.Ota@Sun.COM 	/* Do not send cong updates to loopback */
7612676SEiji.Ota@Sun.COM 	if (rm->m_inc.i_hdr.h_flags & RDSV3_FLAG_CONG_BITMAP) {
7712676SEiji.Ota@Sun.COM 		rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
7812676SEiji.Ota@Sun.COM 		return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
7912676SEiji.Ota@Sun.COM 	}
8012198SEiji.Ota@Sun.COM 	ASSERT(!(hdr_off || sg || off));
8112198SEiji.Ota@Sun.COM 
8212198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_xmit", "Enter(conn: %p, rm: %p)", conn, rm);
8312198SEiji.Ota@Sun.COM 
8412198SEiji.Ota@Sun.COM 	rdsv3_inc_init(&rm->m_inc, conn, conn->c_laddr);
8512676SEiji.Ota@Sun.COM 	/* For the embedded inc. Matching put is in loop_inc_free() */
8612676SEiji.Ota@Sun.COM 	rdsv3_message_addref(rm);
8712198SEiji.Ota@Sun.COM 
8812198SEiji.Ota@Sun.COM 	rdsv3_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,
8912198SEiji.Ota@Sun.COM 	    KM_NOSLEEP);
9012198SEiji.Ota@Sun.COM 
9112198SEiji.Ota@Sun.COM 	rdsv3_send_drop_acked(conn, ntohll(rm->m_inc.i_hdr.h_sequence),
9212198SEiji.Ota@Sun.COM 	    NULL);
9312198SEiji.Ota@Sun.COM 
9412198SEiji.Ota@Sun.COM 	rdsv3_inc_put(&rm->m_inc);
9512198SEiji.Ota@Sun.COM 
9612198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_xmit", "Return(conn: %p, rm: %p)", conn, rm);
9712198SEiji.Ota@Sun.COM 
9812198SEiji.Ota@Sun.COM 	return (sizeof (struct rdsv3_header) +
9912198SEiji.Ota@Sun.COM 	    ntohl(rm->m_inc.i_hdr.h_len));
10012198SEiji.Ota@Sun.COM }
10112198SEiji.Ota@Sun.COM 
10212676SEiji.Ota@Sun.COM /*
10312676SEiji.Ota@Sun.COM  * See rds_loop_xmit(). Since our inc is embedded in the rm, we
10412676SEiji.Ota@Sun.COM  * make sure the rm lives at least until the inc is done.
10512676SEiji.Ota@Sun.COM  */
10612676SEiji.Ota@Sun.COM static void
rdsv3_loop_inc_free(struct rdsv3_incoming * inc)10712676SEiji.Ota@Sun.COM rdsv3_loop_inc_free(struct rdsv3_incoming *inc)
10812676SEiji.Ota@Sun.COM {
10912676SEiji.Ota@Sun.COM 	struct rdsv3_message *rm = container_of(inc, struct rdsv3_message,
11012676SEiji.Ota@Sun.COM 	    m_inc);
11112676SEiji.Ota@Sun.COM 	rdsv3_message_put(rm);
11212676SEiji.Ota@Sun.COM }
11312676SEiji.Ota@Sun.COM 
11412198SEiji.Ota@Sun.COM static int
rdsv3_loop_xmit_cong_map(struct rdsv3_connection * conn,struct rdsv3_cong_map * map,unsigned long offset)11512198SEiji.Ota@Sun.COM rdsv3_loop_xmit_cong_map(struct rdsv3_connection *conn,
11612198SEiji.Ota@Sun.COM     struct rdsv3_cong_map *map,
11712198SEiji.Ota@Sun.COM     unsigned long offset)
11812198SEiji.Ota@Sun.COM {
11912198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Enter(conn: %p)", conn);
12012198SEiji.Ota@Sun.COM 
12112198SEiji.Ota@Sun.COM 	ASSERT(!offset);
12212198SEiji.Ota@Sun.COM 	ASSERT(map == conn->c_lcong);
12312198SEiji.Ota@Sun.COM 
12412198SEiji.Ota@Sun.COM 	rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
12512198SEiji.Ota@Sun.COM 
12612198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Return(conn: %p)", conn);
12712198SEiji.Ota@Sun.COM 
12812198SEiji.Ota@Sun.COM 	return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
12912198SEiji.Ota@Sun.COM }
13012198SEiji.Ota@Sun.COM 
13112198SEiji.Ota@Sun.COM /* we need to at least give the thread something to succeed */
13212198SEiji.Ota@Sun.COM /* ARGSUSED */
13312198SEiji.Ota@Sun.COM static int
rdsv3_loop_recv(struct rdsv3_connection * conn)13412198SEiji.Ota@Sun.COM rdsv3_loop_recv(struct rdsv3_connection *conn)
13512198SEiji.Ota@Sun.COM {
13612198SEiji.Ota@Sun.COM 	return (0);
13712198SEiji.Ota@Sun.COM }
13812198SEiji.Ota@Sun.COM 
13912414SEiji.Ota@Sun.COM struct rdsv3_loop_connection {
14012198SEiji.Ota@Sun.COM 	struct list_node loop_node;
14112198SEiji.Ota@Sun.COM 	struct rdsv3_connection *conn;
14212198SEiji.Ota@Sun.COM };
14312198SEiji.Ota@Sun.COM 
14412198SEiji.Ota@Sun.COM /*
14512198SEiji.Ota@Sun.COM  * Even the loopback transport needs to keep track of its connections,
14612198SEiji.Ota@Sun.COM  * so it can call rdsv3_conn_destroy() on them on exit. N.B. there are
14712198SEiji.Ota@Sun.COM  * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
14812198SEiji.Ota@Sun.COM  * multiple loopback conns allocated, although rather useless.
14912198SEiji.Ota@Sun.COM  */
15012198SEiji.Ota@Sun.COM /* ARGSUSED */
15112198SEiji.Ota@Sun.COM static int
rdsv3_loop_conn_alloc(struct rdsv3_connection * conn,int gfp)15212198SEiji.Ota@Sun.COM rdsv3_loop_conn_alloc(struct rdsv3_connection *conn, int gfp)
15312198SEiji.Ota@Sun.COM {
15412198SEiji.Ota@Sun.COM 	struct rdsv3_loop_connection *lc;
15512198SEiji.Ota@Sun.COM 
15612198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Enter(conn: %p)", conn);
15712198SEiji.Ota@Sun.COM 
15812198SEiji.Ota@Sun.COM 	lc = kmem_zalloc(sizeof (struct rdsv3_loop_connection), KM_NOSLEEP);
15912676SEiji.Ota@Sun.COM 	if (!lc)
16012198SEiji.Ota@Sun.COM 		return (-ENOMEM);
16112198SEiji.Ota@Sun.COM 
16212198SEiji.Ota@Sun.COM 	list_link_init(&lc->loop_node);
16312198SEiji.Ota@Sun.COM 	lc->conn = conn;
16412198SEiji.Ota@Sun.COM 	conn->c_transport_data = lc;
16512198SEiji.Ota@Sun.COM 
16612198SEiji.Ota@Sun.COM 	mutex_enter(&loop_conns_lock);
16712198SEiji.Ota@Sun.COM 	list_insert_tail(&loop_conns, lc);
16812198SEiji.Ota@Sun.COM 	mutex_exit(&loop_conns_lock);
16912198SEiji.Ota@Sun.COM 
17012198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Return(conn: %p)", conn);
17112198SEiji.Ota@Sun.COM 
17212198SEiji.Ota@Sun.COM 	return (0);
17312198SEiji.Ota@Sun.COM }
17412198SEiji.Ota@Sun.COM 
17512198SEiji.Ota@Sun.COM static void
rdsv3_loop_conn_free(void * arg)17612198SEiji.Ota@Sun.COM rdsv3_loop_conn_free(void *arg)
17712198SEiji.Ota@Sun.COM {
17812198SEiji.Ota@Sun.COM 	struct rdsv3_loop_connection *lc = arg;
17912198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF5("rdsv3_loop_conn_free", "lc %p\n", lc);
18012198SEiji.Ota@Sun.COM 	list_remove_node(&lc->loop_node);
18112198SEiji.Ota@Sun.COM 	kmem_free(lc, sizeof (struct rdsv3_loop_connection));
18212198SEiji.Ota@Sun.COM }
18312198SEiji.Ota@Sun.COM 
18412198SEiji.Ota@Sun.COM static int
rdsv3_loop_conn_connect(struct rdsv3_connection * conn)18512198SEiji.Ota@Sun.COM rdsv3_loop_conn_connect(struct rdsv3_connection *conn)
18612198SEiji.Ota@Sun.COM {
18712198SEiji.Ota@Sun.COM 	rdsv3_connect_complete(conn);
18812198SEiji.Ota@Sun.COM 	return (0);
18912198SEiji.Ota@Sun.COM }
19012198SEiji.Ota@Sun.COM 
19112198SEiji.Ota@Sun.COM /* ARGSUSED */
19212198SEiji.Ota@Sun.COM static void
rdsv3_loop_conn_shutdown(struct rdsv3_connection * conn)19312198SEiji.Ota@Sun.COM rdsv3_loop_conn_shutdown(struct rdsv3_connection *conn)
19412198SEiji.Ota@Sun.COM {
19512198SEiji.Ota@Sun.COM }
19612198SEiji.Ota@Sun.COM 
19712198SEiji.Ota@Sun.COM void
rdsv3_loop_exit(void)19812198SEiji.Ota@Sun.COM rdsv3_loop_exit(void)
19912198SEiji.Ota@Sun.COM {
20012198SEiji.Ota@Sun.COM 	struct rdsv3_loop_connection *lc, *_lc;
20112198SEiji.Ota@Sun.COM 	list_t tmp_list;
20212198SEiji.Ota@Sun.COM 
20312198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Enter");
20412198SEiji.Ota@Sun.COM 
20512198SEiji.Ota@Sun.COM 	list_create(&tmp_list, sizeof (struct rdsv3_loop_connection),
20612198SEiji.Ota@Sun.COM 	    offsetof(struct rdsv3_loop_connection, loop_node));
20712198SEiji.Ota@Sun.COM 
20812198SEiji.Ota@Sun.COM 	/* avoid calling conn_destroy with irqs off */
20912198SEiji.Ota@Sun.COM 	mutex_enter(&loop_conns_lock);
21012198SEiji.Ota@Sun.COM 	list_splice(&loop_conns, &tmp_list);
21112198SEiji.Ota@Sun.COM 	mutex_exit(&loop_conns_lock);
21212198SEiji.Ota@Sun.COM 
21312198SEiji.Ota@Sun.COM 	RDSV3_FOR_EACH_LIST_NODE_SAFE(lc, _lc, &tmp_list, loop_node) {
21412198SEiji.Ota@Sun.COM 		ASSERT(!lc->conn->c_passive);
21512198SEiji.Ota@Sun.COM 		rdsv3_conn_destroy(lc->conn);
21612198SEiji.Ota@Sun.COM 	}
21712198SEiji.Ota@Sun.COM 
21812198SEiji.Ota@Sun.COM 	list_destroy(&loop_conns);
21912198SEiji.Ota@Sun.COM 	mutex_destroy(&loop_conns_lock);
22012198SEiji.Ota@Sun.COM 
22112198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Return");
22212198SEiji.Ota@Sun.COM }
22312198SEiji.Ota@Sun.COM 
22412198SEiji.Ota@Sun.COM /*
22512198SEiji.Ota@Sun.COM  * This is missing .xmit_* because loop doesn't go through generic
22612198SEiji.Ota@Sun.COM  * rdsv3_send_xmit() and doesn't call rdsv3_recv_incoming().  .listen_stop and
22712198SEiji.Ota@Sun.COM  * .laddr_check are missing because transport.c doesn't iterate over
22812198SEiji.Ota@Sun.COM  * rdsv3_loop_transport.
22912198SEiji.Ota@Sun.COM  */
23012198SEiji.Ota@Sun.COM #ifndef __lock_lint
23112198SEiji.Ota@Sun.COM struct rdsv3_transport rdsv3_loop_transport = {
23212198SEiji.Ota@Sun.COM 	.xmit			= rdsv3_loop_xmit,
23312198SEiji.Ota@Sun.COM 	.xmit_cong_map		= rdsv3_loop_xmit_cong_map,
23412198SEiji.Ota@Sun.COM 	.recv			= rdsv3_loop_recv,
23512198SEiji.Ota@Sun.COM 	.conn_alloc		= rdsv3_loop_conn_alloc,
23612198SEiji.Ota@Sun.COM 	.conn_free		= rdsv3_loop_conn_free,
23712198SEiji.Ota@Sun.COM 	.conn_connect		= rdsv3_loop_conn_connect,
23812198SEiji.Ota@Sun.COM 	.conn_shutdown		= rdsv3_loop_conn_shutdown,
23912198SEiji.Ota@Sun.COM 	.inc_copy_to_user	= rdsv3_message_inc_copy_to_user,
24012676SEiji.Ota@Sun.COM 	.inc_free		= rdsv3_loop_inc_free,
24112198SEiji.Ota@Sun.COM 	.t_name			= "loopback",
24212198SEiji.Ota@Sun.COM };
24312198SEiji.Ota@Sun.COM #else
24412198SEiji.Ota@Sun.COM struct rdsv3_transport rdsv3_loop_transport;
24512198SEiji.Ota@Sun.COM #endif
246