xref: /linux/net/rds/rdma_transport.c (revision 8795a739e5c72abeec51caf36b6df2b37e5720c5)
1 /*
2  * Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 #include <linux/module.h>
34 #include <rdma/rdma_cm.h>
35 
36 #include "rds_single_path.h"
37 #include "rdma_transport.h"
38 #include "ib.h"
39 
40 /* Global IPv4 and IPv6 RDS RDMA listener cm_id */
41 static struct rdma_cm_id *rds_rdma_listen_id;
42 #if IS_ENABLED(CONFIG_IPV6)
43 static struct rdma_cm_id *rds6_rdma_listen_id;
44 #endif
45 
46 /* Per IB specification 7.7.3, service level is a 4-bit field. */
47 #define TOS_TO_SL(tos)		((tos) & 0xF)
48 
49 static int rds_rdma_cm_event_handler_cmn(struct rdma_cm_id *cm_id,
50 					 struct rdma_cm_event *event,
51 					 bool isv6)
52 {
53 	/* this can be null in the listening path */
54 	struct rds_connection *conn = cm_id->context;
55 	struct rds_transport *trans;
56 	int ret = 0;
57 	int *err;
58 	u8 len;
59 
60 	rdsdebug("conn %p id %p handling event %u (%s)\n", conn, cm_id,
61 		 event->event, rdma_event_msg(event->event));
62 
63 	if (cm_id->device->node_type == RDMA_NODE_IB_CA)
64 		trans = &rds_ib_transport;
65 
66 	/* Prevent shutdown from tearing down the connection
67 	 * while we're executing. */
68 	if (conn) {
69 		mutex_lock(&conn->c_cm_lock);
70 
71 		/* If the connection is being shut down, bail out
72 		 * right away. We return 0 so cm_id doesn't get
73 		 * destroyed prematurely */
74 		if (rds_conn_state(conn) == RDS_CONN_DISCONNECTING) {
75 			/* Reject incoming connections while we're tearing
76 			 * down an existing one. */
77 			if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST)
78 				ret = 1;
79 			goto out;
80 		}
81 	}
82 
83 	switch (event->event) {
84 	case RDMA_CM_EVENT_CONNECT_REQUEST:
85 		ret = trans->cm_handle_connect(cm_id, event, isv6);
86 		break;
87 
88 	case RDMA_CM_EVENT_ADDR_RESOLVED:
89 		rdma_set_service_type(cm_id, conn->c_tos);
90 		/* XXX do we need to clean up if this fails? */
91 		ret = rdma_resolve_route(cm_id,
92 					 RDS_RDMA_RESOLVE_TIMEOUT_MS);
93 		break;
94 
95 	case RDMA_CM_EVENT_ROUTE_RESOLVED:
96 		/* Connection could have been dropped so make sure the
97 		 * cm_id is valid before proceeding
98 		 */
99 		if (conn) {
100 			struct rds_ib_connection *ibic;
101 
102 			ibic = conn->c_transport_data;
103 			if (ibic && ibic->i_cm_id == cm_id) {
104 				cm_id->route.path_rec[0].sl =
105 					TOS_TO_SL(conn->c_tos);
106 				ret = trans->cm_initiate_connect(cm_id, isv6);
107 			} else {
108 				rds_conn_drop(conn);
109 			}
110 		}
111 		break;
112 
113 	case RDMA_CM_EVENT_ESTABLISHED:
114 		if (conn)
115 			trans->cm_connect_complete(conn, event);
116 		break;
117 
118 	case RDMA_CM_EVENT_REJECTED:
119 		if (!conn)
120 			break;
121 		err = (int *)rdma_consumer_reject_data(cm_id, event, &len);
122 		if (!err ||
123 		    (err && len >= sizeof(*err) &&
124 		     ((*err) <= RDS_RDMA_REJ_INCOMPAT))) {
125 			pr_warn("RDS/RDMA: conn <%pI6c, %pI6c> rejected, dropping connection\n",
126 				&conn->c_laddr, &conn->c_faddr);
127 
128 			if (!conn->c_tos)
129 				conn->c_proposed_version = RDS_PROTOCOL_COMPAT_VERSION;
130 
131 			rds_conn_drop(conn);
132 		}
133 		rdsdebug("Connection rejected: %s\n",
134 			 rdma_reject_msg(cm_id, event->status));
135 		break;
136 	case RDMA_CM_EVENT_ADDR_ERROR:
137 	case RDMA_CM_EVENT_ROUTE_ERROR:
138 	case RDMA_CM_EVENT_CONNECT_ERROR:
139 	case RDMA_CM_EVENT_UNREACHABLE:
140 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
141 	case RDMA_CM_EVENT_ADDR_CHANGE:
142 		if (conn)
143 			rds_conn_drop(conn);
144 		break;
145 
146 	case RDMA_CM_EVENT_DISCONNECTED:
147 		if (!conn)
148 			break;
149 		rdsdebug("DISCONNECT event - dropping connection "
150 			 "%pI6c->%pI6c\n", &conn->c_laddr,
151 			 &conn->c_faddr);
152 		rds_conn_drop(conn);
153 		break;
154 
155 	case RDMA_CM_EVENT_TIMEWAIT_EXIT:
156 		if (conn) {
157 			pr_info("RDS: RDMA_CM_EVENT_TIMEWAIT_EXIT event: dropping connection %pI6c->%pI6c\n",
158 				&conn->c_laddr, &conn->c_faddr);
159 			rds_conn_drop(conn);
160 		}
161 		break;
162 
163 	default:
164 		/* things like device disconnect? */
165 		printk(KERN_ERR "RDS: unknown event %u (%s)!\n",
166 		       event->event, rdma_event_msg(event->event));
167 		break;
168 	}
169 
170 out:
171 	if (conn)
172 		mutex_unlock(&conn->c_cm_lock);
173 
174 	rdsdebug("id %p event %u (%s) handling ret %d\n", cm_id, event->event,
175 		 rdma_event_msg(event->event), ret);
176 
177 	return ret;
178 }
179 
180 int rds_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
181 			      struct rdma_cm_event *event)
182 {
183 	return rds_rdma_cm_event_handler_cmn(cm_id, event, false);
184 }
185 
186 #if IS_ENABLED(CONFIG_IPV6)
187 int rds6_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
188 			       struct rdma_cm_event *event)
189 {
190 	return rds_rdma_cm_event_handler_cmn(cm_id, event, true);
191 }
192 #endif
193 
194 static int rds_rdma_listen_init_common(rdma_cm_event_handler handler,
195 				       struct sockaddr *sa,
196 				       struct rdma_cm_id **ret_cm_id)
197 {
198 	struct rdma_cm_id *cm_id;
199 	int ret;
200 
201 	cm_id = rdma_create_id(&init_net, handler, NULL,
202 			       RDMA_PS_TCP, IB_QPT_RC);
203 	if (IS_ERR(cm_id)) {
204 		ret = PTR_ERR(cm_id);
205 		printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
206 		       "rdma_create_id() returned %d\n", ret);
207 		return ret;
208 	}
209 
210 	/*
211 	 * XXX I bet this binds the cm_id to a device.  If we want to support
212 	 * fail-over we'll have to take this into consideration.
213 	 */
214 	ret = rdma_bind_addr(cm_id, sa);
215 	if (ret) {
216 		printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
217 		       "rdma_bind_addr() returned %d\n", ret);
218 		goto out;
219 	}
220 
221 	ret = rdma_listen(cm_id, 128);
222 	if (ret) {
223 		printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
224 		       "rdma_listen() returned %d\n", ret);
225 		goto out;
226 	}
227 
228 	rdsdebug("cm %p listening on port %u\n", cm_id, RDS_PORT);
229 
230 	*ret_cm_id = cm_id;
231 	cm_id = NULL;
232 out:
233 	if (cm_id)
234 		rdma_destroy_id(cm_id);
235 	return ret;
236 }
237 
238 /* Initialize the RDS RDMA listeners.  We create two listeners for
239  * compatibility reason.  The one on RDS_PORT is used for IPv4
240  * requests only.  The one on RDS_CM_PORT is used for IPv6 requests
241  * only.  So only IPv6 enabled RDS module will communicate using this
242  * port.
243  */
244 static int rds_rdma_listen_init(void)
245 {
246 	int ret;
247 #if IS_ENABLED(CONFIG_IPV6)
248 	struct sockaddr_in6 sin6;
249 #endif
250 	struct sockaddr_in sin;
251 
252 	sin.sin_family = PF_INET;
253 	sin.sin_addr.s_addr = htonl(INADDR_ANY);
254 	sin.sin_port = htons(RDS_PORT);
255 	ret = rds_rdma_listen_init_common(rds_rdma_cm_event_handler,
256 					  (struct sockaddr *)&sin,
257 					  &rds_rdma_listen_id);
258 	if (ret != 0)
259 		return ret;
260 
261 #if IS_ENABLED(CONFIG_IPV6)
262 	sin6.sin6_family = PF_INET6;
263 	sin6.sin6_addr = in6addr_any;
264 	sin6.sin6_port = htons(RDS_CM_PORT);
265 	sin6.sin6_scope_id = 0;
266 	sin6.sin6_flowinfo = 0;
267 	ret = rds_rdma_listen_init_common(rds6_rdma_cm_event_handler,
268 					  (struct sockaddr *)&sin6,
269 					  &rds6_rdma_listen_id);
270 	/* Keep going even when IPv6 is not enabled in the system. */
271 	if (ret != 0)
272 		rdsdebug("Cannot set up IPv6 RDMA listener\n");
273 #endif
274 	return 0;
275 }
276 
277 static void rds_rdma_listen_stop(void)
278 {
279 	if (rds_rdma_listen_id) {
280 		rdsdebug("cm %p\n", rds_rdma_listen_id);
281 		rdma_destroy_id(rds_rdma_listen_id);
282 		rds_rdma_listen_id = NULL;
283 	}
284 #if IS_ENABLED(CONFIG_IPV6)
285 	if (rds6_rdma_listen_id) {
286 		rdsdebug("cm %p\n", rds6_rdma_listen_id);
287 		rdma_destroy_id(rds6_rdma_listen_id);
288 		rds6_rdma_listen_id = NULL;
289 	}
290 #endif
291 }
292 
293 static int rds_rdma_init(void)
294 {
295 	int ret;
296 
297 	ret = rds_ib_init();
298 	if (ret)
299 		goto out;
300 
301 	ret = rds_rdma_listen_init();
302 	if (ret)
303 		rds_ib_exit();
304 out:
305 	return ret;
306 }
307 module_init(rds_rdma_init);
308 
309 static void rds_rdma_exit(void)
310 {
311 	/* stop listening first to ensure no new connections are attempted */
312 	rds_rdma_listen_stop();
313 	rds_ib_exit();
314 }
315 module_exit(rds_rdma_exit);
316 
317 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
318 MODULE_DESCRIPTION("RDS: IB transport");
319 MODULE_LICENSE("Dual BSD/GPL");
320