xref: /openbmc/linux/net/rds/ib.c (revision 1e2b44e78eead7bcadfbf96f70d95773191541c9)
1 /*
2  * Copyright (c) 2006, 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/kernel.h>
34 #include <linux/in.h>
35 #include <linux/if.h>
36 #include <linux/netdevice.h>
37 #include <linux/inetdevice.h>
38 #include <linux/if_arp.h>
39 #include <linux/delay.h>
40 #include <linux/slab.h>
41 #include <linux/module.h>
42 #include <net/addrconf.h>
43 
44 #include "rds_single_path.h"
45 #include "rds.h"
46 #include "ib.h"
47 #include "ib_mr.h"
48 
49 static unsigned int rds_ib_mr_1m_pool_size = RDS_MR_1M_POOL_SIZE;
50 static unsigned int rds_ib_mr_8k_pool_size = RDS_MR_8K_POOL_SIZE;
51 unsigned int rds_ib_retry_count = RDS_IB_DEFAULT_RETRY_COUNT;
52 static atomic_t rds_ib_unloading;
53 
54 module_param(rds_ib_mr_1m_pool_size, int, 0444);
55 MODULE_PARM_DESC(rds_ib_mr_1m_pool_size, " Max number of 1M mr per HCA");
56 module_param(rds_ib_mr_8k_pool_size, int, 0444);
57 MODULE_PARM_DESC(rds_ib_mr_8k_pool_size, " Max number of 8K mr per HCA");
58 module_param(rds_ib_retry_count, int, 0444);
59 MODULE_PARM_DESC(rds_ib_retry_count, " Number of hw retries before reporting an error");
60 
61 /*
62  * we have a clumsy combination of RCU and a rwsem protecting this list
63  * because it is used both in the get_mr fast path and while blocking in
64  * the FMR flushing path.
65  */
66 DECLARE_RWSEM(rds_ib_devices_lock);
67 struct list_head rds_ib_devices;
68 
69 /* NOTE: if also grabbing ibdev lock, grab this first */
70 DEFINE_SPINLOCK(ib_nodev_conns_lock);
71 LIST_HEAD(ib_nodev_conns);
72 
73 static void rds_ib_nodev_connect(void)
74 {
75 	struct rds_ib_connection *ic;
76 
77 	spin_lock(&ib_nodev_conns_lock);
78 	list_for_each_entry(ic, &ib_nodev_conns, ib_node)
79 		rds_conn_connect_if_down(ic->conn);
80 	spin_unlock(&ib_nodev_conns_lock);
81 }
82 
83 static void rds_ib_dev_shutdown(struct rds_ib_device *rds_ibdev)
84 {
85 	struct rds_ib_connection *ic;
86 	unsigned long flags;
87 
88 	spin_lock_irqsave(&rds_ibdev->spinlock, flags);
89 	list_for_each_entry(ic, &rds_ibdev->conn_list, ib_node)
90 		rds_conn_drop(ic->conn);
91 	spin_unlock_irqrestore(&rds_ibdev->spinlock, flags);
92 }
93 
94 /*
95  * rds_ib_destroy_mr_pool() blocks on a few things and mrs drop references
96  * from interrupt context so we push freing off into a work struct in krdsd.
97  */
98 static void rds_ib_dev_free(struct work_struct *work)
99 {
100 	struct rds_ib_ipaddr *i_ipaddr, *i_next;
101 	struct rds_ib_device *rds_ibdev = container_of(work,
102 					struct rds_ib_device, free_work);
103 
104 	if (rds_ibdev->mr_8k_pool)
105 		rds_ib_destroy_mr_pool(rds_ibdev->mr_8k_pool);
106 	if (rds_ibdev->mr_1m_pool)
107 		rds_ib_destroy_mr_pool(rds_ibdev->mr_1m_pool);
108 	if (rds_ibdev->pd)
109 		ib_dealloc_pd(rds_ibdev->pd);
110 
111 	list_for_each_entry_safe(i_ipaddr, i_next, &rds_ibdev->ipaddr_list, list) {
112 		list_del(&i_ipaddr->list);
113 		kfree(i_ipaddr);
114 	}
115 
116 	kfree(rds_ibdev->vector_load);
117 
118 	kfree(rds_ibdev);
119 }
120 
121 void rds_ib_dev_put(struct rds_ib_device *rds_ibdev)
122 {
123 	BUG_ON(refcount_read(&rds_ibdev->refcount) == 0);
124 	if (refcount_dec_and_test(&rds_ibdev->refcount))
125 		queue_work(rds_wq, &rds_ibdev->free_work);
126 }
127 
128 static void rds_ib_add_one(struct ib_device *device)
129 {
130 	struct rds_ib_device *rds_ibdev;
131 	bool has_fr, has_fmr;
132 
133 	/* Only handle IB (no iWARP) devices */
134 	if (device->node_type != RDMA_NODE_IB_CA)
135 		return;
136 
137 	rds_ibdev = kzalloc_node(sizeof(struct rds_ib_device), GFP_KERNEL,
138 				 ibdev_to_node(device));
139 	if (!rds_ibdev)
140 		return;
141 
142 	spin_lock_init(&rds_ibdev->spinlock);
143 	refcount_set(&rds_ibdev->refcount, 1);
144 	INIT_WORK(&rds_ibdev->free_work, rds_ib_dev_free);
145 
146 	rds_ibdev->max_wrs = device->attrs.max_qp_wr;
147 	rds_ibdev->max_sge = min(device->attrs.max_sge, RDS_IB_MAX_SGE);
148 
149 	has_fr = (device->attrs.device_cap_flags &
150 		  IB_DEVICE_MEM_MGT_EXTENSIONS);
151 	has_fmr = (device->alloc_fmr && device->dealloc_fmr &&
152 		   device->map_phys_fmr && device->unmap_fmr);
153 	rds_ibdev->use_fastreg = (has_fr && !has_fmr);
154 
155 	rds_ibdev->fmr_max_remaps = device->attrs.max_map_per_fmr?: 32;
156 	rds_ibdev->max_1m_mrs = device->attrs.max_mr ?
157 		min_t(unsigned int, (device->attrs.max_mr / 2),
158 		      rds_ib_mr_1m_pool_size) : rds_ib_mr_1m_pool_size;
159 
160 	rds_ibdev->max_8k_mrs = device->attrs.max_mr ?
161 		min_t(unsigned int, ((device->attrs.max_mr / 2) * RDS_MR_8K_SCALE),
162 		      rds_ib_mr_8k_pool_size) : rds_ib_mr_8k_pool_size;
163 
164 	rds_ibdev->max_initiator_depth = device->attrs.max_qp_init_rd_atom;
165 	rds_ibdev->max_responder_resources = device->attrs.max_qp_rd_atom;
166 
167 	rds_ibdev->vector_load = kcalloc(device->num_comp_vectors,
168 					 sizeof(int),
169 					 GFP_KERNEL);
170 	if (!rds_ibdev->vector_load) {
171 		pr_err("RDS/IB: %s failed to allocate vector memory\n",
172 			__func__);
173 		goto put_dev;
174 	}
175 
176 	rds_ibdev->dev = device;
177 	rds_ibdev->pd = ib_alloc_pd(device, 0);
178 	if (IS_ERR(rds_ibdev->pd)) {
179 		rds_ibdev->pd = NULL;
180 		goto put_dev;
181 	}
182 
183 	rds_ibdev->mr_1m_pool =
184 		rds_ib_create_mr_pool(rds_ibdev, RDS_IB_MR_1M_POOL);
185 	if (IS_ERR(rds_ibdev->mr_1m_pool)) {
186 		rds_ibdev->mr_1m_pool = NULL;
187 		goto put_dev;
188 	}
189 
190 	rds_ibdev->mr_8k_pool =
191 		rds_ib_create_mr_pool(rds_ibdev, RDS_IB_MR_8K_POOL);
192 	if (IS_ERR(rds_ibdev->mr_8k_pool)) {
193 		rds_ibdev->mr_8k_pool = NULL;
194 		goto put_dev;
195 	}
196 
197 	rdsdebug("RDS/IB: max_mr = %d, max_wrs = %d, max_sge = %d, fmr_max_remaps = %d, max_1m_mrs = %d, max_8k_mrs = %d\n",
198 		 device->attrs.max_fmr, rds_ibdev->max_wrs, rds_ibdev->max_sge,
199 		 rds_ibdev->fmr_max_remaps, rds_ibdev->max_1m_mrs,
200 		 rds_ibdev->max_8k_mrs);
201 
202 	pr_info("RDS/IB: %s: %s supported and preferred\n",
203 		device->name,
204 		rds_ibdev->use_fastreg ? "FRMR" : "FMR");
205 
206 	INIT_LIST_HEAD(&rds_ibdev->ipaddr_list);
207 	INIT_LIST_HEAD(&rds_ibdev->conn_list);
208 
209 	down_write(&rds_ib_devices_lock);
210 	list_add_tail_rcu(&rds_ibdev->list, &rds_ib_devices);
211 	up_write(&rds_ib_devices_lock);
212 	refcount_inc(&rds_ibdev->refcount);
213 
214 	ib_set_client_data(device, &rds_ib_client, rds_ibdev);
215 	refcount_inc(&rds_ibdev->refcount);
216 
217 	rds_ib_nodev_connect();
218 
219 put_dev:
220 	rds_ib_dev_put(rds_ibdev);
221 }
222 
223 /*
224  * New connections use this to find the device to associate with the
225  * connection.  It's not in the fast path so we're not concerned about the
226  * performance of the IB call.  (As of this writing, it uses an interrupt
227  * blocking spinlock to serialize walking a per-device list of all registered
228  * clients.)
229  *
230  * RCU is used to handle incoming connections racing with device teardown.
231  * Rather than use a lock to serialize removal from the client_data and
232  * getting a new reference, we use an RCU grace period.  The destruction
233  * path removes the device from client_data and then waits for all RCU
234  * readers to finish.
235  *
236  * A new connection can get NULL from this if its arriving on a
237  * device that is in the process of being removed.
238  */
239 struct rds_ib_device *rds_ib_get_client_data(struct ib_device *device)
240 {
241 	struct rds_ib_device *rds_ibdev;
242 
243 	rcu_read_lock();
244 	rds_ibdev = ib_get_client_data(device, &rds_ib_client);
245 	if (rds_ibdev)
246 		refcount_inc(&rds_ibdev->refcount);
247 	rcu_read_unlock();
248 	return rds_ibdev;
249 }
250 
251 /*
252  * The IB stack is letting us know that a device is going away.  This can
253  * happen if the underlying HCA driver is removed or if PCI hotplug is removing
254  * the pci function, for example.
255  *
256  * This can be called at any time and can be racing with any other RDS path.
257  */
258 static void rds_ib_remove_one(struct ib_device *device, void *client_data)
259 {
260 	struct rds_ib_device *rds_ibdev = client_data;
261 
262 	if (!rds_ibdev)
263 		return;
264 
265 	rds_ib_dev_shutdown(rds_ibdev);
266 
267 	/* stop connection attempts from getting a reference to this device. */
268 	ib_set_client_data(device, &rds_ib_client, NULL);
269 
270 	down_write(&rds_ib_devices_lock);
271 	list_del_rcu(&rds_ibdev->list);
272 	up_write(&rds_ib_devices_lock);
273 
274 	/*
275 	 * This synchronize rcu is waiting for readers of both the ib
276 	 * client data and the devices list to finish before we drop
277 	 * both of those references.
278 	 */
279 	synchronize_rcu();
280 	rds_ib_dev_put(rds_ibdev);
281 	rds_ib_dev_put(rds_ibdev);
282 }
283 
284 struct ib_client rds_ib_client = {
285 	.name   = "rds_ib",
286 	.add    = rds_ib_add_one,
287 	.remove = rds_ib_remove_one
288 };
289 
290 static int rds_ib_conn_info_visitor(struct rds_connection *conn,
291 				    void *buffer)
292 {
293 	struct rds_info_rdma_connection *iinfo = buffer;
294 	struct rds_ib_connection *ic;
295 
296 	/* We will only ever look at IB transports */
297 	if (conn->c_trans != &rds_ib_transport)
298 		return 0;
299 	if (conn->c_isv6)
300 		return 0;
301 
302 	iinfo->src_addr = conn->c_laddr.s6_addr32[3];
303 	iinfo->dst_addr = conn->c_faddr.s6_addr32[3];
304 
305 	memset(&iinfo->src_gid, 0, sizeof(iinfo->src_gid));
306 	memset(&iinfo->dst_gid, 0, sizeof(iinfo->dst_gid));
307 	if (rds_conn_state(conn) == RDS_CONN_UP) {
308 		struct rds_ib_device *rds_ibdev;
309 
310 		ic = conn->c_transport_data;
311 
312 		rdma_read_gids(ic->i_cm_id, (union ib_gid *)&iinfo->src_gid,
313 			       (union ib_gid *)&iinfo->dst_gid);
314 
315 		rds_ibdev = ic->rds_ibdev;
316 		iinfo->max_send_wr = ic->i_send_ring.w_nr;
317 		iinfo->max_recv_wr = ic->i_recv_ring.w_nr;
318 		iinfo->max_send_sge = rds_ibdev->max_sge;
319 		rds_ib_get_mr_info(rds_ibdev, iinfo);
320 	}
321 	return 1;
322 }
323 
324 static void rds_ib_ic_info(struct socket *sock, unsigned int len,
325 			   struct rds_info_iterator *iter,
326 			   struct rds_info_lengths *lens)
327 {
328 	u64 buffer[(sizeof(struct rds_info_rdma_connection) + 7) / 8];
329 
330 	rds_for_each_conn_info(sock, len, iter, lens,
331 				rds_ib_conn_info_visitor,
332 				buffer,
333 				sizeof(struct rds_info_rdma_connection));
334 }
335 
336 /*
337  * Early RDS/IB was built to only bind to an address if there is an IPoIB
338  * device with that address set.
339  *
340  * If it were me, I'd advocate for something more flexible.  Sending and
341  * receiving should be device-agnostic.  Transports would try and maintain
342  * connections between peers who have messages queued.  Userspace would be
343  * allowed to influence which paths have priority.  We could call userspace
344  * asserting this policy "routing".
345  */
346 static int rds_ib_laddr_check(struct net *net, const struct in6_addr *addr,
347 			      __u32 scope_id)
348 {
349 	int ret;
350 	struct rdma_cm_id *cm_id;
351 	struct sockaddr_in6 sin6;
352 	struct sockaddr_in sin;
353 	struct sockaddr *sa;
354 	bool isv4;
355 
356 	isv4 = ipv6_addr_v4mapped(addr);
357 	/* Create a CMA ID and try to bind it. This catches both
358 	 * IB and iWARP capable NICs.
359 	 */
360 	cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler,
361 			       NULL, RDMA_PS_TCP, IB_QPT_RC);
362 	if (IS_ERR(cm_id))
363 		return PTR_ERR(cm_id);
364 
365 	if (isv4) {
366 		memset(&sin, 0, sizeof(sin));
367 		sin.sin_family = AF_INET;
368 		sin.sin_addr.s_addr = addr->s6_addr32[3];
369 		sa = (struct sockaddr *)&sin;
370 	} else {
371 		memset(&sin6, 0, sizeof(sin6));
372 		sin6.sin6_family = AF_INET6;
373 		sin6.sin6_addr = *addr;
374 		sin6.sin6_scope_id = scope_id;
375 		sa = (struct sockaddr *)&sin6;
376 
377 		/* XXX Do a special IPv6 link local address check here.  The
378 		 * reason is that rdma_bind_addr() always succeeds with IPv6
379 		 * link local address regardless it is indeed configured in a
380 		 * system.
381 		 */
382 		if (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL) {
383 			struct net_device *dev;
384 
385 			if (scope_id == 0)
386 				return -EADDRNOTAVAIL;
387 
388 			/* Use init_net for now as RDS is not network
389 			 * name space aware.
390 			 */
391 			dev = dev_get_by_index(&init_net, scope_id);
392 			if (!dev)
393 				return -EADDRNOTAVAIL;
394 			if (!ipv6_chk_addr(&init_net, addr, dev, 1)) {
395 				dev_put(dev);
396 				return -EADDRNOTAVAIL;
397 			}
398 			dev_put(dev);
399 		}
400 	}
401 
402 	/* rdma_bind_addr will only succeed for IB & iWARP devices */
403 	ret = rdma_bind_addr(cm_id, sa);
404 	/* due to this, we will claim to support iWARP devices unless we
405 	   check node_type. */
406 	if (ret || !cm_id->device ||
407 	    cm_id->device->node_type != RDMA_NODE_IB_CA)
408 		ret = -EADDRNOTAVAIL;
409 
410 	rdsdebug("addr %pI6c%%%u ret %d node type %d\n",
411 		 addr, scope_id, ret,
412 		 cm_id->device ? cm_id->device->node_type : -1);
413 
414 	rdma_destroy_id(cm_id);
415 
416 	return ret;
417 }
418 
419 static void rds_ib_unregister_client(void)
420 {
421 	ib_unregister_client(&rds_ib_client);
422 	/* wait for rds_ib_dev_free() to complete */
423 	flush_workqueue(rds_wq);
424 }
425 
426 static void rds_ib_set_unloading(void)
427 {
428 	atomic_set(&rds_ib_unloading, 1);
429 }
430 
431 static bool rds_ib_is_unloading(struct rds_connection *conn)
432 {
433 	struct rds_conn_path *cp = &conn->c_path[0];
434 
435 	return (test_bit(RDS_DESTROY_PENDING, &cp->cp_flags) ||
436 		atomic_read(&rds_ib_unloading) != 0);
437 }
438 
439 void rds_ib_exit(void)
440 {
441 	rds_ib_set_unloading();
442 	synchronize_rcu();
443 	rds_info_deregister_func(RDS_INFO_IB_CONNECTIONS, rds_ib_ic_info);
444 	rds_ib_unregister_client();
445 	rds_ib_destroy_nodev_conns();
446 	rds_ib_sysctl_exit();
447 	rds_ib_recv_exit();
448 	rds_trans_unregister(&rds_ib_transport);
449 	rds_ib_mr_exit();
450 }
451 
452 struct rds_transport rds_ib_transport = {
453 	.laddr_check		= rds_ib_laddr_check,
454 	.xmit_path_complete	= rds_ib_xmit_path_complete,
455 	.xmit			= rds_ib_xmit,
456 	.xmit_rdma		= rds_ib_xmit_rdma,
457 	.xmit_atomic		= rds_ib_xmit_atomic,
458 	.recv_path		= rds_ib_recv_path,
459 	.conn_alloc		= rds_ib_conn_alloc,
460 	.conn_free		= rds_ib_conn_free,
461 	.conn_path_connect	= rds_ib_conn_path_connect,
462 	.conn_path_shutdown	= rds_ib_conn_path_shutdown,
463 	.inc_copy_to_user	= rds_ib_inc_copy_to_user,
464 	.inc_free		= rds_ib_inc_free,
465 	.cm_initiate_connect	= rds_ib_cm_initiate_connect,
466 	.cm_handle_connect	= rds_ib_cm_handle_connect,
467 	.cm_connect_complete	= rds_ib_cm_connect_complete,
468 	.stats_info_copy	= rds_ib_stats_info_copy,
469 	.exit			= rds_ib_exit,
470 	.get_mr			= rds_ib_get_mr,
471 	.sync_mr		= rds_ib_sync_mr,
472 	.free_mr		= rds_ib_free_mr,
473 	.flush_mrs		= rds_ib_flush_mrs,
474 	.t_owner		= THIS_MODULE,
475 	.t_name			= "infiniband",
476 	.t_unloading		= rds_ib_is_unloading,
477 	.t_type			= RDS_TRANS_IB
478 };
479 
480 int rds_ib_init(void)
481 {
482 	int ret;
483 
484 	INIT_LIST_HEAD(&rds_ib_devices);
485 
486 	ret = rds_ib_mr_init();
487 	if (ret)
488 		goto out;
489 
490 	ret = ib_register_client(&rds_ib_client);
491 	if (ret)
492 		goto out_mr_exit;
493 
494 	ret = rds_ib_sysctl_init();
495 	if (ret)
496 		goto out_ibreg;
497 
498 	ret = rds_ib_recv_init();
499 	if (ret)
500 		goto out_sysctl;
501 
502 	rds_trans_register(&rds_ib_transport);
503 
504 	rds_info_register_func(RDS_INFO_IB_CONNECTIONS, rds_ib_ic_info);
505 
506 	goto out;
507 
508 out_sysctl:
509 	rds_ib_sysctl_exit();
510 out_ibreg:
511 	rds_ib_unregister_client();
512 out_mr_exit:
513 	rds_ib_mr_exit();
514 out:
515 	return ret;
516 }
517 
518 MODULE_LICENSE("GPL");
519 
520