transport.c 23.5 KB
Newer Older
1
/*
C
Chuck Lever 已提交
2
 * Copyright (c) 2014-2017 Oracle.  All rights reserved.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 * Copyright (c) 2003-2007 Network Appliance, Inc. 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 BSD-type
 * 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.
 *
 *      Neither the name of the Network Appliance, Inc. nor the names of
 *      its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written
 *      permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * transport.c
 *
 * This file contains the top-level implementation of an RPC RDMA
 * transport.
 *
 * Naming convention: functions beginning with xprt_ are part of the
 * transport switch. All others are RPC RDMA internal.
 */

#include <linux/module.h>
52
#include <linux/slab.h>
53
#include <linux/seq_file.h>
54
#include <linux/sunrpc/addr.h>
55 56 57

#include "xprt_rdma.h"

J
Jeff Layton 已提交
58
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
59 60 61 62 63 64 65 66
# define RPCDBG_FACILITY	RPCDBG_TRANS
#endif

/*
 * tunables
 */

static unsigned int xprt_rdma_slot_table_entries = RPCRDMA_DEF_SLOT_TABLE;
67
unsigned int xprt_rdma_max_inline_read = RPCRDMA_DEF_INLINE;
68
static unsigned int xprt_rdma_max_inline_write = RPCRDMA_DEF_INLINE;
69
unsigned int xprt_rdma_memreg_strategy		= RPCRDMA_FRWR;
70
int xprt_rdma_pad_optimize;
71

J
Jeff Layton 已提交
72
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
73 74 75

static unsigned int min_slot_table_size = RPCRDMA_MIN_SLOT_TABLE;
static unsigned int max_slot_table_size = RPCRDMA_MAX_SLOT_TABLE;
76 77
static unsigned int min_inline_size = RPCRDMA_MIN_INLINE;
static unsigned int max_inline_size = RPCRDMA_MAX_INLINE;
78 79 80 81
static unsigned int zero;
static unsigned int max_padding = PAGE_SIZE;
static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
static unsigned int max_memreg = RPCRDMA_LAST - 1;
82
static unsigned int dummy;
83 84 85

static struct ctl_table_header *sunrpc_table_header;

86
static struct ctl_table xr_tunables_table[] = {
87 88 89 90 91
	{
		.procname	= "rdma_slot_table_entries",
		.data		= &xprt_rdma_slot_table_entries,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
92
		.proc_handler	= proc_dointvec_minmax,
93 94 95 96 97 98 99 100
		.extra1		= &min_slot_table_size,
		.extra2		= &max_slot_table_size
	},
	{
		.procname	= "rdma_max_inline_read",
		.data		= &xprt_rdma_max_inline_read,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
101
		.proc_handler	= proc_dointvec_minmax,
102 103
		.extra1		= &min_inline_size,
		.extra2		= &max_inline_size,
104 105 106 107 108 109
	},
	{
		.procname	= "rdma_max_inline_write",
		.data		= &xprt_rdma_max_inline_write,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
110
		.proc_handler	= proc_dointvec_minmax,
111 112
		.extra1		= &min_inline_size,
		.extra2		= &max_inline_size,
113 114 115
	},
	{
		.procname	= "rdma_inline_write_padding",
116
		.data		= &dummy,
117 118
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
119
		.proc_handler	= proc_dointvec_minmax,
120 121 122 123 124 125 126 127
		.extra1		= &zero,
		.extra2		= &max_padding,
	},
	{
		.procname	= "rdma_memreg_strategy",
		.data		= &xprt_rdma_memreg_strategy,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
128
		.proc_handler	= proc_dointvec_minmax,
129 130 131
		.extra1		= &min_memreg,
		.extra2		= &max_memreg,
	},
132 133 134 135 136
	{
		.procname	= "rdma_pad_optimize",
		.data		= &xprt_rdma_pad_optimize,
		.maxlen		= sizeof(unsigned int),
		.mode		= 0644,
137
		.proc_handler	= proc_dointvec,
138
	},
139
	{ },
140 141
};

142
static struct ctl_table sunrpc_table[] = {
143 144 145 146 147
	{
		.procname	= "sunrpc",
		.mode		= 0555,
		.child		= xr_tunables_table
	},
148
	{ },
149 150 151 152
};

#endif

153
static const struct rpc_xprt_ops xprt_rdma_procs;
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static void
xprt_rdma_format_addresses4(struct rpc_xprt *xprt, struct sockaddr *sap)
{
	struct sockaddr_in *sin = (struct sockaddr_in *)sap;
	char buf[20];

	snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);

	xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA;
}

static void
xprt_rdma_format_addresses6(struct rpc_xprt *xprt, struct sockaddr *sap)
{
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
	char buf[40];

	snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);

	xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA6;
}

179
void
180
xprt_rdma_format_addresses(struct rpc_xprt *xprt, struct sockaddr *sap)
181
{
182 183 184 185 186 187 188 189 190 191 192 193 194
	char buf[128];

	switch (sap->sa_family) {
	case AF_INET:
		xprt_rdma_format_addresses4(xprt, sap);
		break;
	case AF_INET6:
		xprt_rdma_format_addresses6(xprt, sap);
		break;
	default:
		pr_err("rpcrdma: Unrecognized address family\n");
		return;
	}
195

196 197
	(void)rpc_ntop(sap, buf, sizeof(buf));
	xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
198

199
	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
200
	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
201

202
	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
203
	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
204

205
	xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
206 207
}

208
void
209 210
xprt_rdma_free_addresses(struct rpc_xprt *xprt)
{
211 212 213 214 215 216 217 218 219 220
	unsigned int i;

	for (i = 0; i < RPC_DISPLAY_MAX; i++)
		switch (i) {
		case RPC_DISPLAY_PROTO:
		case RPC_DISPLAY_NETID:
			continue;
		default:
			kfree(xprt->address_strings[i]);
		}
221 222
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
void
rpcrdma_conn_func(struct rpcrdma_ep *ep)
{
	schedule_delayed_work(&ep->rep_connect_worker, 0);
}

void
rpcrdma_connect_worker(struct work_struct *work)
{
	struct rpcrdma_ep *ep =
		container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
	struct rpcrdma_xprt *r_xprt =
		container_of(ep, struct rpcrdma_xprt, rx_ep);
	struct rpc_xprt *xprt = &r_xprt->rx_xprt;

	spin_lock_bh(&xprt->transport_lock);
	if (ep->rep_connected > 0) {
		if (!xprt_test_and_set_connected(xprt))
			xprt_wake_pending_tasks(xprt, 0);
	} else {
		if (xprt_test_and_clear_connected(xprt))
			xprt_wake_pending_tasks(xprt, -ENOTCONN);
	}
	spin_unlock_bh(&xprt->transport_lock);
}

249 250 251
static void
xprt_rdma_connect_worker(struct work_struct *work)
{
252 253 254
	struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
						   rx_connect_worker.work);
	struct rpc_xprt *xprt = &r_xprt->rx_xprt;
255 256
	int rc = 0;

257 258 259 260 261 262
	xprt_clear_connected(xprt);

	rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
	if (rc)
		xprt_wake_pending_tasks(xprt, rc);

263 264 265
	xprt_clear_connecting(xprt);
}

C
Chuck Lever 已提交
266 267 268 269 270 271
static void
xprt_rdma_inject_disconnect(struct rpc_xprt *xprt)
{
	struct rpcrdma_xprt *r_xprt = container_of(xprt, struct rpcrdma_xprt,
						   rx_xprt);

272
	trace_xprtrdma_inject_dsc(r_xprt);
C
Chuck Lever 已提交
273 274 275
	rdma_disconnect(r_xprt->rx_ia.ri_id);
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
/*
 * xprt_rdma_destroy
 *
 * Destroy the xprt.
 * Free all memory associated with the object, including its own.
 * NOTE: none of the *destroy methods free memory for their top-level
 * objects, even though they may have allocated it (they do free
 * private memory). It's up to the caller to handle it. In this
 * case (RDMA transport), all structure memory is inlined with the
 * struct rpcrdma_xprt.
 */
static void
xprt_rdma_destroy(struct rpc_xprt *xprt)
{
	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);

292
	trace_xprtrdma_destroy(r_xprt);
293

294
	cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
295 296 297

	xprt_clear_connected(xprt);

298
	rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
299
	rpcrdma_buffer_destroy(&r_xprt->rx_buf);
300 301 302
	rpcrdma_ia_close(&r_xprt->rx_ia);

	xprt_rdma_free_addresses(xprt);
303
	xprt_free(xprt);
304 305 306 307

	module_put(THIS_MODULE);
}

308 309 310 311 312
static const struct rpc_timeout xprt_rdma_default_timeout = {
	.to_initval = 60 * HZ,
	.to_maxval = 60 * HZ,
};

313 314 315 316 317 318 319 320 321 322 323 324
/**
 * xprt_setup_rdma - Set up transport to use RDMA
 *
 * @args: rpc transport arguments
 */
static struct rpc_xprt *
xprt_setup_rdma(struct xprt_create *args)
{
	struct rpcrdma_create_data_internal cdata;
	struct rpc_xprt *xprt;
	struct rpcrdma_xprt *new_xprt;
	struct rpcrdma_ep *new_ep;
325
	struct sockaddr *sap;
326 327 328 329 330 331 332
	int rc;

	if (args->addrlen > sizeof(xprt->addr)) {
		dprintk("RPC:       %s: address too large\n", __func__);
		return ERR_PTR(-EBADF);
	}

P
Pavel Emelyanov 已提交
333
	xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
334
			xprt_rdma_slot_table_entries,
335
			xprt_rdma_slot_table_entries);
336 337 338 339 340 341 342
	if (xprt == NULL) {
		dprintk("RPC:       %s: couldn't allocate rpcrdma_xprt\n",
			__func__);
		return ERR_PTR(-ENOMEM);
	}

	/* 60 second timeout, no retries */
343
	xprt->timeout = &xprt_rdma_default_timeout;
344 345 346
	xprt->bind_timeout = RPCRDMA_BIND_TO;
	xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
	xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
347 348 349 350 351 352 353 354

	xprt->resvport = 0;		/* privileged port not needed */
	xprt->tsh_size = 0;		/* RPC-RDMA handles framing */
	xprt->ops = &xprt_rdma_procs;

	/*
	 * Set up RDMA-specific connect data.
	 */
355
	sap = args->dstaddr;
356 357 358 359 360

	/* Ensure xprt->addr holds valid server TCP (not RDMA)
	 * address, for any side protocols which peek at it */
	xprt->prot = IPPROTO_TCP;
	xprt->addrlen = args->addrlen;
361
	memcpy(&xprt->addr, sap, xprt->addrlen);
362

363
	if (rpc_get_port(sap))
364
		xprt_set_bound(xprt);
365
	xprt_rdma_format_addresses(xprt, sap);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

	cdata.max_requests = xprt->max_reqs;

	cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
	cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */

	cdata.inline_wsize = xprt_rdma_max_inline_write;
	if (cdata.inline_wsize > cdata.wsize)
		cdata.inline_wsize = cdata.wsize;

	cdata.inline_rsize = xprt_rdma_max_inline_read;
	if (cdata.inline_rsize > cdata.rsize)
		cdata.inline_rsize = cdata.rsize;

	/*
	 * Create new transport instance, which includes initialized
	 *  o ia
	 *  o endpoint
	 *  o buffers
	 */

	new_xprt = rpcx_to_rdmax(xprt);

389
	rc = rpcrdma_ia_open(new_xprt);
390 391 392 393 394 395 396 397 398 399 400 401 402 403
	if (rc)
		goto out1;

	/*
	 * initialize and create ep
	 */
	new_xprt->rx_data = cdata;
	new_ep = &new_xprt->rx_ep;

	rc = rpcrdma_ep_create(&new_xprt->rx_ep,
				&new_xprt->rx_ia, &new_xprt->rx_data);
	if (rc)
		goto out2;

404
	rc = rpcrdma_buffer_create(new_xprt);
405 406 407
	if (rc)
		goto out3;

408 409
	INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
			  xprt_rdma_connect_worker);
410

411 412 413 414
	xprt->max_payload = new_xprt->rx_ia.ri_ops->ro_maxpages(new_xprt);
	if (xprt->max_payload == 0)
		goto out4;
	xprt->max_payload <<= PAGE_SHIFT;
415 416
	dprintk("RPC:       %s: transport data payload maximum: %zu bytes\n",
		__func__, xprt->max_payload);
417 418 419 420

	if (!try_module_get(THIS_MODULE))
		goto out4;

421 422 423
	dprintk("RPC:       %s: %s:%s\n", __func__,
		xprt->address_strings[RPC_DISPLAY_ADDR],
		xprt->address_strings[RPC_DISPLAY_PORT]);
424
	trace_xprtrdma_create(new_xprt);
425 426 427
	return xprt;

out4:
428 429
	rpcrdma_buffer_destroy(&new_xprt->rx_buf);
	rc = -ENODEV;
430
out3:
431
	rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
432 433 434
out2:
	rpcrdma_ia_close(&new_xprt->rx_ia);
out1:
435
	trace_xprtrdma_destroy(new_xprt);
436
	xprt_rdma_free_addresses(xprt);
437
	xprt_free(xprt);
438 439 440
	return ERR_PTR(rc);
}

441 442 443 444 445 446
/**
 * xprt_rdma_close - Close down RDMA connection
 * @xprt: generic transport to be closed
 *
 * Called during transport shutdown reconnect, or device
 * removal. Caller holds the transport's write lock.
447 448 449 450 451
 */
static void
xprt_rdma_close(struct rpc_xprt *xprt)
{
	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
452 453 454 455
	struct rpcrdma_ep *ep = &r_xprt->rx_ep;
	struct rpcrdma_ia *ia = &r_xprt->rx_ia;

	dprintk("RPC:       %s: closing xprt %p\n", __func__, xprt);
456

457 458 459 460 461 462 463 464
	if (test_and_clear_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags)) {
		xprt_clear_connected(xprt);
		rpcrdma_ia_remove(ia);
		return;
	}
	if (ep->rep_connected == -ENODEV)
		return;
	if (ep->rep_connected > 0)
465
		xprt->reestablish_timeout = 0;
466
	xprt_disconnect_done(xprt);
467
	rpcrdma_ep_disconnect(ep, ia);
468 469
}

470 471 472 473 474 475 476
/**
 * xprt_rdma_set_port - update server port with rpcbind result
 * @xprt: controlling RPC transport
 * @port: new port value
 *
 * Transport connect status is unchanged.
 */
477 478 479
static void
xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
{
480 481 482 483 484 485 486 487 488 489
	struct sockaddr *sap = (struct sockaddr *)&xprt->addr;
	char buf[8];

	dprintk("RPC:       %s: setting port for xprt %p (%s:%s) to %u\n",
		__func__, xprt,
		xprt->address_strings[RPC_DISPLAY_ADDR],
		xprt->address_strings[RPC_DISPLAY_PORT],
		port);

	rpc_set_port(sap, port);
490

491 492 493 494 495 496 497
	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
	snprintf(buf, sizeof(buf), "%u", port);
	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);

	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
	snprintf(buf, sizeof(buf), "%4hx", port);
	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
498 499
}

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
/**
 * xprt_rdma_timer - invoked when an RPC times out
 * @xprt: controlling RPC transport
 * @task: RPC task that timed out
 *
 * Invoked when the transport is still connected, but an RPC
 * retransmit timeout occurs.
 *
 * Since RDMA connections don't have a keep-alive, forcibly
 * disconnect and retry to connect. This drives full
 * detection of the network path, and retransmissions of
 * all pending RPCs.
 */
static void
xprt_rdma_timer(struct rpc_xprt *xprt, struct rpc_task *task)
{
	xprt_force_disconnect(xprt);
}

519
static void
520
xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
521 522 523
{
	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);

524 525
	if (r_xprt->rx_ep.rep_connected != 0) {
		/* Reconnect */
526 527
		schedule_delayed_work(&r_xprt->rx_connect_worker,
				      xprt->reestablish_timeout);
528
		xprt->reestablish_timeout <<= 1;
529 530 531 532
		if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
			xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
		else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
			xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
533
	} else {
534
		schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
535
		if (!RPC_IS_ASYNC(task))
536
			flush_delayed_work(&r_xprt->rx_connect_worker);
537 538 539
	}
}

540 541 542 543 544 545 546 547 548
static bool
rpcrdma_get_sendbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
		    size_t size, gfp_t flags)
{
	struct rpcrdma_regbuf *rb;

	if (req->rl_sendbuf && rdmab_length(req->rl_sendbuf) >= size)
		return true;

549
	rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
550 551 552
	if (IS_ERR(rb))
		return false;

553
	rpcrdma_free_regbuf(req->rl_sendbuf);
554
	r_xprt->rx_stats.hardway_register_count += size;
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
	req->rl_sendbuf = rb;
	return true;
}

/* The rq_rcv_buf is used only if a Reply chunk is necessary.
 * The decision to use a Reply chunk is made later in
 * rpcrdma_marshal_req. This buffer is registered at that time.
 *
 * Otherwise, the associated RPC Reply arrives in a separate
 * Receive buffer, arbitrarily chosen by the HCA. The buffer
 * allocated here for the RPC Reply is not utilized in that
 * case. See rpcrdma_inline_fixup.
 *
 * A regbuf is used here to remember the buffer size.
 */
static bool
rpcrdma_get_recvbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
		    size_t size, gfp_t flags)
{
	struct rpcrdma_regbuf *rb;

	if (req->rl_recvbuf && rdmab_length(req->rl_recvbuf) >= size)
		return true;

579
	rb = rpcrdma_alloc_regbuf(size, DMA_NONE, flags);
580 581 582
	if (IS_ERR(rb))
		return false;

583
	rpcrdma_free_regbuf(req->rl_recvbuf);
584 585 586 587 588
	r_xprt->rx_stats.hardway_register_count += size;
	req->rl_recvbuf = rb;
	return true;
}

589 590 591 592 593 594 595 596 597
/**
 * xprt_rdma_allocate - allocate transport resources for an RPC
 * @task: RPC task
 *
 * Return values:
 *        0:	Success; rq_buffer points to RPC buffer to use
 *   ENOMEM:	Out of memory, call again later
 *      EIO:	A permanent error occurred, do not retry
 *
598
 * The RDMA allocate/free functions need the task structure as a place
599 600
 * to hide the struct rpcrdma_req, which is necessary for the actual
 * send/recv sequence.
601
 *
602 603
 * xprt_rdma_allocate provides buffers that are already mapped for
 * DMA, and a local DMA lkey is provided for each.
604
 */
605 606
static int
xprt_rdma_allocate(struct rpc_task *task)
607
{
608 609
	struct rpc_rqst *rqst = task->tk_rqstp;
	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
610
	struct rpcrdma_req *req;
611
	gfp_t flags;
612

613
	req = rpcrdma_buffer_get(&r_xprt->rx_buf);
614
	if (req == NULL)
615
		goto out_get;
616

617
	flags = RPCRDMA_DEF_GFP;
618 619 620
	if (RPC_IS_SWAPPER(task))
		flags = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN;

621 622 623 624 625
	if (!rpcrdma_get_sendbuf(r_xprt, req, rqst->rq_callsize, flags))
		goto out_fail;
	if (!rpcrdma_get_recvbuf(r_xprt, req, rqst->rq_rcvsize, flags))
		goto out_fail;

626
	rpcrdma_set_xprtdata(rqst, req);
627
	rqst->rq_buffer = req->rl_sendbuf->rg_base;
628
	rqst->rq_rbuffer = req->rl_recvbuf->rg_base;
629
	trace_xprtrdma_allocate(task, req);
630
	return 0;
631 632

out_fail:
633
	rpcrdma_buffer_put(req);
634 635
out_get:
	trace_xprtrdma_allocate(task, NULL);
636
	return -ENOMEM;
637 638
}

639 640 641 642 643
/**
 * xprt_rdma_free - release resources allocated by xprt_rdma_allocate
 * @task: RPC task
 *
 * Caller guarantees rqst->rq_buffer is non-NULL.
644 645
 */
static void
646
xprt_rdma_free(struct rpc_task *task)
647
{
648 649 650
	struct rpc_rqst *rqst = task->tk_rqstp;
	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
651

652 653
	if (test_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags))
		rpcrdma_release_rqst(r_xprt, req);
654
	trace_xprtrdma_rpc_done(task, req);
655 656 657
	rpcrdma_buffer_put(req);
}

658 659 660 661
/**
 * xprt_rdma_send_request - marshal and send an RPC request
 * @task: RPC task with an RPC message in rq_snd_buf
 *
662 663
 * Caller holds the transport's write lock.
 *
664 665 666
 * Returns:
 *	%0 if the RPC message has been sent
 *	%-ENOTCONN if the caller should reconnect and call again
667 668
 *	%-EAGAIN if the caller should call again
 *	%-ENOBUFS if the caller should call again after a delay
669 670
 *	%-EIO if a permanent error occurred and the request was not
 *		sent. Do not try to send this message again.
671 672 673 674 675
 */
static int
xprt_rdma_send_request(struct rpc_task *task)
{
	struct rpc_rqst *rqst = task->tk_rqstp;
676
	struct rpc_xprt *xprt = rqst->rq_xprt;
677 678
	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
679
	int rc = 0;
680

681 682 683 684 685
#if defined(CONFIG_SUNRPC_BACKCHANNEL)
	if (unlikely(!rqst->rq_buffer))
		return xprt_rdma_bc_send_reply(rqst);
#endif	/* CONFIG_SUNRPC_BACKCHANNEL */

686 687 688
	if (!xprt_connected(xprt))
		goto drop_connection;

689
	rc = rpcrdma_marshal_req(r_xprt, rqst);
690 691
	if (rc < 0)
		goto failed_marshal;
692 693 694 695

	if (req->rl_reply == NULL) 		/* e.g. reconnection */
		rpcrdma_recv_buffer_get(req);

696
	/* Must suppress retransmit to maintain credits */
697
	if (rqst->rq_connect_cookie == xprt->connect_cookie)
698 699
		goto drop_connection;

700
	__set_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags);
701 702
	if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
		goto drop_connection;
703

704
	rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
705
	rqst->rq_bytes_sent = 0;
C
Chuck Lever 已提交
706 707 708 709 710 711

	/* An RPC with no reply will throw off credit accounting,
	 * so drop the connection to reset the credit grant.
	 */
	if (!rpc_reply_expected(task))
		goto drop_connection;
712
	return 0;
713

714
failed_marshal:
715 716
	if (rc != -ENOTCONN)
		return rc;
717 718 719
drop_connection:
	xprt_disconnect_done(xprt);
	return -ENOTCONN;	/* implies disconnect */
720 721
}

722
void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
723 724 725 726 727 728 729
{
	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
	long idle_time = 0;

	if (xprt_connected(xprt))
		idle_time = (long)(jiffies - xprt->last_used) / HZ;

730 731 732 733 734 735 736 737 738 739 740 741
	seq_puts(seq, "\txprt:\trdma ");
	seq_printf(seq, "%u %lu %lu %lu %ld %lu %lu %lu %llu %llu ",
		   0,	/* need a local port? */
		   xprt->stat.bind_count,
		   xprt->stat.connect_count,
		   xprt->stat.connect_time,
		   idle_time,
		   xprt->stat.sends,
		   xprt->stat.recvs,
		   xprt->stat.bad_xids,
		   xprt->stat.req_u,
		   xprt->stat.bklog_u);
742
	seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %lu %lu %lu %lu ",
743 744 745 746 747 748 749 750 751
		   r_xprt->rx_stats.read_chunk_count,
		   r_xprt->rx_stats.write_chunk_count,
		   r_xprt->rx_stats.reply_chunk_count,
		   r_xprt->rx_stats.total_rdma_request,
		   r_xprt->rx_stats.total_rdma_reply,
		   r_xprt->rx_stats.pullup_copy_count,
		   r_xprt->rx_stats.fixup_copy_count,
		   r_xprt->rx_stats.hardway_register_count,
		   r_xprt->rx_stats.failed_marshal_count,
752 753
		   r_xprt->rx_stats.bad_reply_count,
		   r_xprt->rx_stats.nomsg_call_count);
754
	seq_printf(seq, "%lu %lu %lu %lu %lu %lu\n",
755
		   r_xprt->rx_stats.mrs_recovered,
C
Chuck Lever 已提交
756
		   r_xprt->rx_stats.mrs_orphaned,
757
		   r_xprt->rx_stats.mrs_allocated,
758
		   r_xprt->rx_stats.local_inv_needed,
759 760
		   r_xprt->rx_stats.empty_sendctx_q,
		   r_xprt->rx_stats.reply_waits_for_send);
761 762
}

763 764 765
static int
xprt_rdma_enable_swap(struct rpc_xprt *xprt)
{
C
Chuck Lever 已提交
766
	return 0;
767 768 769 770 771 772 773
}

static void
xprt_rdma_disable_swap(struct rpc_xprt *xprt)
{
}

774 775 776 777
/*
 * Plumbing for rpc transport switch and kernel module
 */

778
static const struct rpc_xprt_ops xprt_rdma_procs = {
779
	.reserve_xprt		= xprt_reserve_xprt_cong,
780
	.release_xprt		= xprt_release_xprt_cong, /* sunrpc/xprt.c */
781
	.alloc_slot		= xprt_alloc_slot,
782 783
	.release_request	= xprt_release_rqst_cong,       /* ditto */
	.set_retrans_timeout	= xprt_set_retrans_timeout_def, /* ditto */
784
	.timer			= xprt_rdma_timer,
785 786 787 788 789 790 791 792
	.rpcbind		= rpcb_getport_async,	/* sunrpc/rpcb_clnt.c */
	.set_port		= xprt_rdma_set_port,
	.connect		= xprt_rdma_connect,
	.buf_alloc		= xprt_rdma_allocate,
	.buf_free		= xprt_rdma_free,
	.send_request		= xprt_rdma_send_request,
	.close			= xprt_rdma_close,
	.destroy		= xprt_rdma_destroy,
793 794 795
	.print_stats		= xprt_rdma_print_stats,
	.enable_swap		= xprt_rdma_enable_swap,
	.disable_swap		= xprt_rdma_disable_swap,
796 797 798
	.inject_disconnect	= xprt_rdma_inject_disconnect,
#if defined(CONFIG_SUNRPC_BACKCHANNEL)
	.bc_setup		= xprt_rdma_bc_setup,
799
	.bc_up			= xprt_rdma_bc_up,
800
	.bc_maxpayload		= xprt_rdma_bc_maxpayload,
801 802 803
	.bc_free_rqst		= xprt_rdma_bc_free_rqst,
	.bc_destroy		= xprt_rdma_bc_destroy,
#endif
804 805 806 807 808 809 810 811 812 813
};

static struct xprt_class xprt_rdma = {
	.list			= LIST_HEAD_INIT(xprt_rdma.list),
	.name			= "rdma",
	.owner			= THIS_MODULE,
	.ident			= XPRT_TRANSPORT_RDMA,
	.setup			= xprt_setup_rdma,
};

814
void xprt_rdma_cleanup(void)
815 816 817
{
	int rc;

818
	dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
J
Jeff Layton 已提交
819
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
820 821 822 823 824 825 826 827 828
	if (sunrpc_table_header) {
		unregister_sysctl_table(sunrpc_table_header);
		sunrpc_table_header = NULL;
	}
#endif
	rc = xprt_unregister_transport(&xprt_rdma);
	if (rc)
		dprintk("RPC:       %s: xprt_unregister returned %i\n",
			__func__, rc);
829

830
	rpcrdma_destroy_wq();
831 832 833 834 835

	rc = xprt_unregister_transport(&xprt_rdma_bc);
	if (rc)
		dprintk("RPC:       %s: xprt_unregister(bc) returned %i\n",
			__func__, rc);
836 837
}

838
int xprt_rdma_init(void)
839 840 841
{
	int rc;

842
	rc = rpcrdma_alloc_wq();
843
	if (rc)
844 845
		return rc;

846 847
	rc = xprt_register_transport(&xprt_rdma);
	if (rc) {
848
		rpcrdma_destroy_wq();
849 850 851
		return rc;
	}

852 853 854 855 856 857 858
	rc = xprt_register_transport(&xprt_rdma_bc);
	if (rc) {
		xprt_unregister_transport(&xprt_rdma);
		rpcrdma_destroy_wq();
		return rc;
	}

859
	dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
860

861 862
	dprintk("Defaults:\n");
	dprintk("\tSlots %d\n"
863 864 865
		"\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
		xprt_rdma_slot_table_entries,
		xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
866
	dprintk("\tPadding 0\n\tMemreg %d\n", xprt_rdma_memreg_strategy);
867

J
Jeff Layton 已提交
868
#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
869 870 871 872 873
	if (!sunrpc_table_header)
		sunrpc_table_header = register_sysctl_table(sunrpc_table);
#endif
	return 0;
}