rdma_transport.c 6.0 KB
Newer Older
A
Andy Grover 已提交
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
/*
 * Copyright (c) 2009 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.
 *
 */
33
#include <linux/module.h>
A
Andy Grover 已提交
34 35 36
#include <rdma/rdma_cm.h>

#include "rdma_transport.h"
37
#include "ib.h"
A
Andy Grover 已提交
38

39
static struct rdma_cm_id *rds_rdma_listen_id;
A
Andy Grover 已提交
40 41 42 43 44 45 46 47 48

int rds_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
			      struct rdma_cm_event *event)
{
	/* this can be null in the listening path */
	struct rds_connection *conn = cm_id->context;
	struct rds_transport *trans;
	int ret = 0;

49
	rdsdebug("conn %p id %p handling event %u (%s)\n", conn, cm_id,
50
		 event->event, rdma_event_msg(event->event));
A
Andy Grover 已提交
51

52
	if (cm_id->device->node_type == RDMA_NODE_IB_CA)
A
Andy Grover 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
		trans = &rds_ib_transport;

	/* Prevent shutdown from tearing down the connection
	 * while we're executing. */
	if (conn) {
		mutex_lock(&conn->c_cm_lock);

		/* If the connection is being shut down, bail out
		 * right away. We return 0 so cm_id doesn't get
		 * destroyed prematurely */
		if (rds_conn_state(conn) == RDS_CONN_DISCONNECTING) {
			/* Reject incoming connections while we're tearing
			 * down an existing one. */
			if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST)
				ret = 1;
			goto out;
		}
	}

	switch (event->event) {
	case RDMA_CM_EVENT_CONNECT_REQUEST:
		ret = trans->cm_handle_connect(cm_id, event);
		break;

	case RDMA_CM_EVENT_ADDR_RESOLVED:
		/* XXX do we need to clean up if this fails? */
		ret = rdma_resolve_route(cm_id,
					 RDS_RDMA_RESOLVE_TIMEOUT_MS);
		break;

	case RDMA_CM_EVENT_ROUTE_RESOLVED:
84 85 86 87 88 89 90 91 92 93 94 95
		/* Connection could have been dropped so make sure the
		 * cm_id is valid before proceeding
		 */
		if (conn) {
			struct rds_ib_connection *ibic;

			ibic = conn->c_transport_data;
			if (ibic && ibic->i_cm_id == cm_id)
				ret = trans->cm_initiate_connect(cm_id);
			else
				rds_conn_drop(conn);
		}
A
Andy Grover 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
		break;

	case RDMA_CM_EVENT_ESTABLISHED:
		trans->cm_connect_complete(conn, event);
		break;

	case RDMA_CM_EVENT_ADDR_ERROR:
	case RDMA_CM_EVENT_ROUTE_ERROR:
	case RDMA_CM_EVENT_CONNECT_ERROR:
	case RDMA_CM_EVENT_UNREACHABLE:
	case RDMA_CM_EVENT_REJECTED:
	case RDMA_CM_EVENT_DEVICE_REMOVAL:
	case RDMA_CM_EVENT_ADDR_CHANGE:
		if (conn)
			rds_conn_drop(conn);
		break;

	case RDMA_CM_EVENT_DISCONNECTED:
114
		rdsdebug("DISCONNECT event - dropping connection "
A
Andy Grover 已提交
115 116 117 118 119
			"%pI4->%pI4\n", &conn->c_laddr,
			 &conn->c_faddr);
		rds_conn_drop(conn);
		break;

120 121 122 123 124 125 126 127
	case RDMA_CM_EVENT_TIMEWAIT_EXIT:
		if (conn) {
			pr_info("RDS: RDMA_CM_EVENT_TIMEWAIT_EXIT event: dropping connection %pI4->%pI4\n",
				&conn->c_laddr, &conn->c_faddr);
			rds_conn_drop(conn);
		}
		break;

A
Andy Grover 已提交
128 129
	default:
		/* things like device disconnect? */
130
		printk(KERN_ERR "RDS: unknown event %u (%s)!\n",
131
		       event->event, rdma_event_msg(event->event));
A
Andy Grover 已提交
132 133 134 135 136 137 138
		break;
	}

out:
	if (conn)
		mutex_unlock(&conn->c_cm_lock);

139
	rdsdebug("id %p event %u (%s) handling ret %d\n", cm_id, event->event,
140
		 rdma_event_msg(event->event), ret);
A
Andy Grover 已提交
141 142 143 144

	return ret;
}

145
static int rds_rdma_listen_init(void)
A
Andy Grover 已提交
146 147 148 149 150
{
	struct sockaddr_in sin;
	struct rdma_cm_id *cm_id;
	int ret;

151 152
	cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler, NULL,
			       RDMA_PS_TCP, IB_QPT_RC);
A
Andy Grover 已提交
153 154
	if (IS_ERR(cm_id)) {
		ret = PTR_ERR(cm_id);
155
		printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
A
Andy Grover 已提交
156
		       "rdma_create_id() returned %d\n", ret);
157
		return ret;
A
Andy Grover 已提交
158 159
	}

160
	sin.sin_family = AF_INET;
A
Andy Grover 已提交
161 162 163 164 165 166 167 168 169
	sin.sin_addr.s_addr = (__force u32)htonl(INADDR_ANY);
	sin.sin_port = (__force u16)htons(RDS_PORT);

	/*
	 * XXX I bet this binds the cm_id to a device.  If we want to support
	 * fail-over we'll have to take this into consideration.
	 */
	ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin);
	if (ret) {
170
		printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
A
Andy Grover 已提交
171 172 173 174 175 176
		       "rdma_bind_addr() returned %d\n", ret);
		goto out;
	}

	ret = rdma_listen(cm_id, 128);
	if (ret) {
177
		printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
A
Andy Grover 已提交
178 179 180 181 182 183
		       "rdma_listen() returned %d\n", ret);
		goto out;
	}

	rdsdebug("cm %p listening on port %u\n", cm_id, RDS_PORT);

184
	rds_rdma_listen_id = cm_id;
A
Andy Grover 已提交
185 186 187 188 189 190 191 192 193
	cm_id = NULL;
out:
	if (cm_id)
		rdma_destroy_id(cm_id);
	return ret;
}

static void rds_rdma_listen_stop(void)
{
194 195 196 197
	if (rds_rdma_listen_id) {
		rdsdebug("cm %p\n", rds_rdma_listen_id);
		rdma_destroy_id(rds_rdma_listen_id);
		rds_rdma_listen_id = NULL;
A
Andy Grover 已提交
198 199 200
	}
}

201
static int rds_rdma_init(void)
A
Andy Grover 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
{
	int ret;

	ret = rds_rdma_listen_init();
	if (ret)
		goto out;

	ret = rds_ib_init();
	if (ret)
		goto err_ib_init;

	goto out;

err_ib_init:
	rds_rdma_listen_stop();
out:
	return ret;
}
220
module_init(rds_rdma_init);
A
Andy Grover 已提交
221

222
static void rds_rdma_exit(void)
A
Andy Grover 已提交
223 224 225 226 227
{
	/* stop listening first to ensure no new connections are attempted */
	rds_rdma_listen_stop();
	rds_ib_exit();
}
228 229 230
module_exit(rds_rdma_exit);

MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
231
MODULE_DESCRIPTION("RDS: IB transport");
232
MODULE_LICENSE("Dual BSD/GPL");
A
Andy Grover 已提交
233