Commit 6c52fdc2 authored by Bernard Metzler's avatar Bernard Metzler Committed by Jason Gunthorpe
Browse files

rdma/siw: connection management



Broken up commit to add the Soft iWarp RDMA driver.
Signed-off-by: default avatarBernard Metzler <bmt@zurich.ibm.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
parent bdcf26bf
// SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
/* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
/* Fredy Neeser */
/* Greg Joyce <greg@opengridcomputing.com> */
/* Copyright (c) 2008-2019, IBM Corporation */
/* Copyright (c) 2017, Open Grid Computing, Inc. */
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/net.h>
#include <linux/inetdevice.h>
#include <net/addrconf.h>
#include <linux/workqueue.h>
#include <net/sock.h>
#include <net/tcp.h>
#include <linux/inet.h>
#include <linux/tcp.h>
#include <rdma/iw_cm.h>
#include <rdma/ib_verbs.h>
#include <rdma/ib_user_verbs.h>
#include "siw.h"
#include "siw_cm.h"
/*
* Set to any combination of
* MPA_V2_RDMA_NO_RTR, MPA_V2_RDMA_READ_RTR, MPA_V2_RDMA_WRITE_RTR
*/
static __be16 rtr_type = MPA_V2_RDMA_READ_RTR | MPA_V2_RDMA_WRITE_RTR;
static const bool relaxed_ird_negotiation = 1;
static void siw_cm_llp_state_change(struct sock *s);
static void siw_cm_llp_data_ready(struct sock *s);
static void siw_cm_llp_write_space(struct sock *s);
static void siw_cm_llp_error_report(struct sock *s);
static int siw_cm_upcall(struct siw_cep *cep, enum iw_cm_event_type reason,
int status);
static void siw_sk_assign_cm_upcalls(struct sock *sk)
{
write_lock_bh(&sk->sk_callback_lock);
sk->sk_state_change = siw_cm_llp_state_change;
sk->sk_data_ready = siw_cm_llp_data_ready;
sk->sk_write_space = siw_cm_llp_write_space;
sk->sk_error_report = siw_cm_llp_error_report;
write_unlock_bh(&sk->sk_callback_lock);
}
static void siw_sk_save_upcalls(struct sock *sk)
{
struct siw_cep *cep = sk_to_cep(sk);
write_lock_bh(&sk->sk_callback_lock);
cep->sk_state_change = sk->sk_state_change;
cep->sk_data_ready = sk->sk_data_ready;
cep->sk_write_space = sk->sk_write_space;
cep->sk_error_report = sk->sk_error_report;
write_unlock_bh(&sk->sk_callback_lock);
}
static void siw_sk_restore_upcalls(struct sock *sk, struct siw_cep *cep)
{
sk->sk_state_change = cep->sk_state_change;
sk->sk_data_ready = cep->sk_data_ready;
sk->sk_write_space = cep->sk_write_space;
sk->sk_error_report = cep->sk_error_report;
sk->sk_user_data = NULL;
}
static void siw_qp_socket_assoc(struct siw_cep *cep, struct siw_qp *qp)
{
struct socket *s = cep->sock;
struct sock *sk = s->sk;
write_lock_bh(&sk->sk_callback_lock);
qp->attrs.sk = s;
sk->sk_data_ready = siw_qp_llp_data_ready;
sk->sk_write_space = siw_qp_llp_write_space;
write_unlock_bh(&sk->sk_callback_lock);
}
static void siw_socket_disassoc(struct socket *s)
{
struct sock *sk = s->sk;
struct siw_cep *cep;
if (sk) {
write_lock_bh(&sk->sk_callback_lock);
cep = sk_to_cep(sk);
if (cep) {
siw_sk_restore_upcalls(sk, cep);
siw_cep_put(cep);
} else {
pr_warn("siw: cannot restore sk callbacks: no ep\n");
}
write_unlock_bh(&sk->sk_callback_lock);
} else {
pr_warn("siw: cannot restore sk callbacks: no sk\n");
}
}
static void siw_rtr_data_ready(struct sock *sk)
{
struct siw_cep *cep;
struct siw_qp *qp = NULL;
read_descriptor_t rd_desc;
read_lock(&sk->sk_callback_lock);
cep = sk_to_cep(sk);
if (!cep) {
WARN(1, "No connection endpoint\n");
goto out;
}
qp = sk_to_qp(sk);
memset(&rd_desc, 0, sizeof(rd_desc));
rd_desc.arg.data = qp;
rd_desc.count = 1;
tcp_read_sock(sk, &rd_desc, siw_tcp_rx_data);
/*
* Check if first frame was successfully processed.
* Signal connection full establishment if yes.
* Failed data processing would have already scheduled
* connection drop.
*/
if (!qp->rx_stream.rx_suspend)
siw_cm_upcall(cep, IW_CM_EVENT_ESTABLISHED, 0);
out:
read_unlock(&sk->sk_callback_lock);
if (qp)
siw_qp_socket_assoc(cep, qp);
}
static void siw_sk_assign_rtr_upcalls(struct siw_cep *cep)
{
struct sock *sk = cep->sock->sk;
write_lock_bh(&sk->sk_callback_lock);
sk->sk_data_ready = siw_rtr_data_ready;
sk->sk_write_space = siw_qp_llp_write_space;
write_unlock_bh(&sk->sk_callback_lock);
}
static void siw_cep_socket_assoc(struct siw_cep *cep, struct socket *s)
{
cep->sock = s;
siw_cep_get(cep);
s->sk->sk_user_data = cep;
siw_sk_save_upcalls(s->sk);
siw_sk_assign_cm_upcalls(s->sk);
}
static struct siw_cep *siw_cep_alloc(struct siw_device *sdev)
{
struct siw_cep *cep = kzalloc(sizeof(*cep), GFP_KERNEL);
unsigned long flags;
if (!cep)
return NULL;
INIT_LIST_HEAD(&cep->listenq);
INIT_LIST_HEAD(&cep->devq);
INIT_LIST_HEAD(&cep->work_freelist);
kref_init(&cep->ref);
cep->state = SIW_EPSTATE_IDLE;
init_waitqueue_head(&cep->waitq);
spin_lock_init(&cep->lock);
cep->sdev = sdev;
cep->enhanced_rdma_conn_est = false;
spin_lock_irqsave(&sdev->lock, flags);
list_add_tail(&cep->devq, &sdev->cep_list);
spin_unlock_irqrestore(&sdev->lock, flags);
siw_dbg_cep(cep, "new endpoint\n");
return cep;
}
static void siw_cm_free_work(struct siw_cep *cep)
{
struct list_head *w, *tmp;
struct siw_cm_work *work;
list_for_each_safe(w, tmp, &cep->work_freelist) {
work = list_entry(w, struct siw_cm_work, list);
list_del(&work->list);
kfree(work);
}
}
static void siw_cancel_mpatimer(struct siw_cep *cep)
{
spin_lock_bh(&cep->lock);
if (cep->mpa_timer) {
if (cancel_delayed_work(&cep->mpa_timer->work)) {
siw_cep_put(cep);
kfree(cep->mpa_timer); /* not needed again */
}
cep->mpa_timer = NULL;
}
spin_unlock_bh(&cep->lock);
}
static void siw_put_work(struct siw_cm_work *work)
{
INIT_LIST_HEAD(&work->list);
spin_lock_bh(&work->cep->lock);
list_add(&work->list, &work->cep->work_freelist);
spin_unlock_bh(&work->cep->lock);
}
static void siw_cep_set_inuse(struct siw_cep *cep)
{
unsigned long flags;
int rv;
retry:
spin_lock_irqsave(&cep->lock, flags);
if (cep->in_use) {
spin_unlock_irqrestore(&cep->lock, flags);
rv = wait_event_interruptible(cep->waitq, !cep->in_use);
if (signal_pending(current))
flush_signals(current);
goto retry;
} else {
cep->in_use = 1;
spin_unlock_irqrestore(&cep->lock, flags);
}
}
static void siw_cep_set_free(struct siw_cep *cep)
{
unsigned long flags;
spin_lock_irqsave(&cep->lock, flags);
cep->in_use = 0;
spin_unlock_irqrestore(&cep->lock, flags);
wake_up(&cep->waitq);
}
static void __siw_cep_dealloc(struct kref *ref)
{
struct siw_cep *cep = container_of(ref, struct siw_cep, ref);
struct siw_device *sdev = cep->sdev;
unsigned long flags;
WARN_ON(cep->listen_cep);
/* kfree(NULL) is safe */
kfree(cep->mpa.pdata);
spin_lock_bh(&cep->lock);
if (!list_empty(&cep->work_freelist))
siw_cm_free_work(cep);
spin_unlock_bh(&cep->lock);
spin_lock_irqsave(&sdev->lock, flags);
list_del(&cep->devq);
spin_unlock_irqrestore(&sdev->lock, flags);
siw_dbg_cep(cep, "free endpoint\n");
kfree(cep);
}
static struct siw_cm_work *siw_get_work(struct siw_cep *cep)
{
struct siw_cm_work *work = NULL;
spin_lock_bh(&cep->lock);
if (!list_empty(&cep->work_freelist)) {
work = list_entry(cep->work_freelist.next, struct siw_cm_work,
list);
list_del_init(&work->list);
}
spin_unlock_bh(&cep->lock);
return work;
}
static int siw_cm_alloc_work(struct siw_cep *cep, int num)
{
struct siw_cm_work *work;
while (num--) {
work = kmalloc(sizeof(*work), GFP_KERNEL);
if (!work) {
if (!(list_empty(&cep->work_freelist)))
siw_cm_free_work(cep);
return -ENOMEM;
}
work->cep = cep;
INIT_LIST_HEAD(&work->list);
list_add(&work->list, &cep->work_freelist);
}
return 0;
}
/*
* siw_cm_upcall()
*
* Upcall to IWCM to inform about async connection events
*/
static int siw_cm_upcall(struct siw_cep *cep, enum iw_cm_event_type reason,
int status)
{
struct iw_cm_event event;
struct iw_cm_id *id;
memset(&event, 0, sizeof(event));
event.status = status;
event.event = reason;
if (reason == IW_CM_EVENT_CONNECT_REQUEST) {
event.provider_data = cep;
id = cep->listen_cep->cm_id;
} else {
id = cep->cm_id;
}
/* Signal IRD and ORD */
if (reason == IW_CM_EVENT_ESTABLISHED ||
reason == IW_CM_EVENT_CONNECT_REPLY) {
/* Signal negotiated IRD/ORD values we will use */
event.ird = cep->ird;
event.ord = cep->ord;
} else if (reason == IW_CM_EVENT_CONNECT_REQUEST) {
event.ird = cep->ord;
event.ord = cep->ird;
}
/* Signal private data and address information */
if (reason == IW_CM_EVENT_CONNECT_REQUEST ||
reason == IW_CM_EVENT_CONNECT_REPLY) {
u16 pd_len = be16_to_cpu(cep->mpa.hdr.params.pd_len);
if (pd_len) {
/*
* hand over MPA private data
*/
event.private_data_len = pd_len;
event.private_data = cep->mpa.pdata;
/* Hide MPA V2 IRD/ORD control */
if (cep->enhanced_rdma_conn_est) {
event.private_data_len -=
sizeof(struct mpa_v2_data);
event.private_data +=
sizeof(struct mpa_v2_data);
}
}
getname_local(cep->sock, &event.local_addr);
getname_peer(cep->sock, &event.remote_addr);
}
siw_dbg_cep(cep, "[QP %u]: id 0x%p, reason=%d, status=%d\n",
cep->qp ? qp_id(cep->qp) : -1, id, reason, status);
return id->event_handler(id, &event);
}
/*
* siw_qp_cm_drop()
*
* Drops established LLP connection if present and not already
* scheduled for dropping. Called from user context, SQ workqueue
* or receive IRQ. Caller signals if socket can be immediately
* closed (basically, if not in IRQ).
*/
void siw_qp_cm_drop(struct siw_qp *qp, int schedule)
{
struct siw_cep *cep = qp->cep;
qp->rx_stream.rx_suspend = 1;
qp->tx_ctx.tx_suspend = 1;
if (!qp->cep)
return;
if (schedule) {
siw_cm_queue_work(cep, SIW_CM_WORK_CLOSE_LLP);
} else {
siw_cep_set_inuse(cep);
if (cep->state == SIW_EPSTATE_CLOSED) {
siw_dbg_cep(cep, "already closed\n");
goto out;
}
siw_dbg_cep(cep, "immediate close, state %d\n", cep->state);
if (qp->term_info.valid)
siw_send_terminate(qp);
if (cep->cm_id) {
switch (cep->state) {
case SIW_EPSTATE_AWAIT_MPAREP:
siw_cm_upcall(cep, IW_CM_EVENT_CONNECT_REPLY,
-EINVAL);
break;
case SIW_EPSTATE_RDMA_MODE:
siw_cm_upcall(cep, IW_CM_EVENT_CLOSE, 0);
break;
case SIW_EPSTATE_IDLE:
case SIW_EPSTATE_LISTENING:
case SIW_EPSTATE_CONNECTING:
case SIW_EPSTATE_AWAIT_MPAREQ:
case SIW_EPSTATE_RECVD_MPAREQ:
case SIW_EPSTATE_CLOSED:
default:
break;
}
cep->cm_id->rem_ref(cep->cm_id);
cep->cm_id = NULL;
siw_cep_put(cep);
}
cep->state = SIW_EPSTATE_CLOSED;
if (cep->sock) {
siw_socket_disassoc(cep->sock);
/*
* Immediately close socket
*/
sock_release(cep->sock);
cep->sock = NULL;
}
if (cep->qp) {
cep->qp = NULL;
siw_qp_put(qp);
}
out:
siw_cep_set_free(cep);
}
}
void siw_cep_put(struct siw_cep *cep)
{
WARN_ON(kref_read(&cep->ref) < 1);
kref_put(&cep->ref, __siw_cep_dealloc);
}
void siw_cep_get(struct siw_cep *cep)
{
kref_get(&cep->ref);
}
/*
* Expects params->pd_len in host byte order
*/
static int siw_send_mpareqrep(struct siw_cep *cep, const void *pdata, u8 pd_len)
{
struct socket *s = cep->sock;
struct mpa_rr *rr = &cep->mpa.hdr;
struct kvec iov[3];
struct msghdr msg;
int rv;
int iovec_num = 0;
int mpa_len;
memset(&msg, 0, sizeof(msg));
iov[iovec_num].iov_base = rr;
iov[iovec_num].iov_len = sizeof(*rr);
mpa_len = sizeof(*rr);
if (cep->enhanced_rdma_conn_est) {
iovec_num++;
iov[iovec_num].iov_base = &cep->mpa.v2_ctrl;
iov[iovec_num].iov_len = sizeof(cep->mpa.v2_ctrl);
mpa_len += sizeof(cep->mpa.v2_ctrl);
}
if (pd_len) {
iovec_num++;
iov[iovec_num].iov_base = (char *)pdata;
iov[iovec_num].iov_len = pd_len;
mpa_len += pd_len;
}
if (cep->enhanced_rdma_conn_est)
pd_len += sizeof(cep->mpa.v2_ctrl);
rr->params.pd_len = cpu_to_be16(pd_len);
rv = kernel_sendmsg(s, &msg, iov, iovec_num + 1, mpa_len);
return rv < 0 ? rv : 0;
}
/*
* Receive MPA Request/Reply header.
*
* Returns 0 if complete MPA Request/Reply header including
* eventual private data was received. Returns -EAGAIN if
* header was partially received or negative error code otherwise.
*
* Context: May be called in process context only
*/
static int siw_recv_mpa_rr(struct siw_cep *cep)
{
struct mpa_rr *hdr = &cep->mpa.hdr;
struct socket *s = cep->sock;
u16 pd_len;
int rcvd, to_rcv;
if (cep->mpa.bytes_rcvd < sizeof(struct mpa_rr)) {
rcvd = ksock_recv(s, (char *)hdr + cep->mpa.bytes_rcvd,
sizeof(struct mpa_rr) - cep->mpa.bytes_rcvd,
0);
if (rcvd <= 0)
return -ECONNABORTED;
cep->mpa.bytes_rcvd += rcvd;
if (cep->mpa.bytes_rcvd < sizeof(struct mpa_rr))
return -EAGAIN;
if (be16_to_cpu(hdr->params.pd_len) > MPA_MAX_PRIVDATA)
return -EPROTO;
}
pd_len = be16_to_cpu(hdr->params.pd_len);
/*
* At least the MPA Request/Reply header (frame not including
* private data) has been received.
* Receive (or continue receiving) any private data.
*/
to_rcv = pd_len - (cep->mpa.bytes_rcvd - sizeof(struct mpa_rr));
if (!to_rcv) {
/*
* We must have hdr->params.pd_len == 0 and thus received a
* complete MPA Request/Reply frame.
* Check against peer protocol violation.
*/
u32 word;
rcvd = ksock_recv(s, (char *)&word, sizeof(word), MSG_DONTWAIT);
if (rcvd == -EAGAIN)
return 0;
if (rcvd == 0) {
siw_dbg_cep(cep, "peer EOF\n");
return -EPIPE;
}
if (rcvd < 0) {
siw_dbg_cep(cep, "error: %d\n", rcvd);
return rcvd;
}
siw_dbg_cep(cep, "peer sent extra data: %d\n", rcvd);
return -EPROTO;
}
/*
* At this point, we must have hdr->params.pd_len != 0.
* A private data buffer gets allocated if hdr->params.pd_len != 0.
*/
if (!cep->mpa.pdata) {
cep->mpa.pdata = kmalloc(pd_len + 4, GFP_KERNEL);
if (!cep->mpa.pdata)
return -ENOMEM;
}
rcvd = ksock_recv(
s, cep->mpa.pdata + cep->mpa.bytes_rcvd - sizeof(struct mpa_rr),
to_rcv + 4, MSG_DONTWAIT);
if (rcvd < 0)
return rcvd;
if (rcvd > to_rcv)
return -EPROTO;
cep->mpa.bytes_rcvd += rcvd;
if (to_rcv == rcvd) {
siw_dbg_cep(cep, "%d bytes private data received\n", pd_len);
return 0;
}
return -EAGAIN;
}
/*
* siw_proc_mpareq()
*
* Read MPA Request from socket and signal new connection to IWCM
* if success. Caller must hold lock on corresponding listening CEP.
*/
static int siw_proc_mpareq(struct siw_cep *cep)
{
struct mpa_rr *req;
int version, rv;
u16 pd_len;
rv = siw_recv_mpa_rr(cep);
if (rv)
return rv;
req = &cep->mpa.hdr;
version = __mpa_rr_revision(req->params.bits);
pd_len = be16_to_cpu(req->params.pd_len);
if (version > MPA_REVISION_2)
/* allow for 0, 1, and 2 only */
return -EPROTO;
if (memcmp(req->key, MPA_KEY_REQ, 16))
return -EPROTO;
/* Prepare for sending MPA reply */
memcpy(req->key, MPA_KEY_REP, 16);
if (version == MPA_REVISION_2 &&
(req->params.bits & MPA_RR_FLAG_ENHANCED</