#define RTRS_CONNECT_TIMEOUT_MS 30000 /* * Wait a bit before trying to reconnect after a failure * in order to give server time to finish clean up which * leads to "false positives" failed reconnect attempts
*/ #define RTRS_RECONNECT_BACKOFF 1000 /* * Wait for additional random time between 0 and 8 seconds * before starting to reconnect to avoid clients reconnecting * all at once in case of a major network outage
*/ #define RTRS_RECONNECT_SEED 8
#define FIRST_CONN 0x01 /* limit to 128 * 4k = 512k max IO */ #define RTRS_MAX_SEGMENTS 128
MODULE_DESCRIPTION("RDMA Transport Client");
MODULE_LICENSE("GPL");
/* * Adapted from null_blk get_tag(). Callers from different cpus may * grab the same bit, since find_first_zero_bit is not atomic. * But then the test_and_set_bit_lock will fail for all the * callers but one, so that they will loop again. * This way an explicit spinlock is not required.
*/ do {
bit = find_first_zero_bit(clt->permits_map, max_depth); if (bit >= max_depth) return NULL;
} while (test_and_set_bit_lock(bit, clt->permits_map));
/** * rtrs_clt_get_permit() - allocates permit for future RDMA operation * @clt: Current session * @con_type: Type of connection to use with the permit * @can_wait: Wait type * * Description: * Allocates permit for the following RDMA operation. Permit is used * to preallocate all resources and to propagate memory pressure * up earlier. * * Context: * Can sleep if @wait == RTRS_PERMIT_WAIT
*/ struct rtrs_permit *rtrs_clt_get_permit(struct rtrs_clt_sess *clt, enum rtrs_clt_con_type con_type, enum wait_type can_wait)
{ struct rtrs_permit *permit;
DEFINE_WAIT(wait);
permit = __rtrs_get_permit(clt, con_type); if (permit || !can_wait) return permit;
do {
prepare_to_wait(&clt->permits_wait, &wait,
TASK_UNINTERRUPTIBLE);
permit = __rtrs_get_permit(clt, con_type); if (permit) break;
/** * rtrs_clt_put_permit() - puts allocated permit * @clt: Current session * @permit: Permit to be freed * * Context: * Does not matter
*/ void rtrs_clt_put_permit(struct rtrs_clt_sess *clt, struct rtrs_permit *permit)
{ if (WARN_ON(!test_bit(permit->mem_id, clt->permits_map))) return;
__rtrs_put_permit(clt, permit);
/* * rtrs_clt_get_permit() adds itself to the &clt->permits_wait list * before calling schedule(). So if rtrs_clt_get_permit() is sleeping * it must have added itself to &clt->permits_wait before * __rtrs_put_permit() finished. * Hence it is safe to guard wake_up() with a waitqueue_active() test.
*/ if (waitqueue_active(&clt->permits_wait))
wake_up(&clt->permits_wait);
}
EXPORT_SYMBOL(rtrs_clt_put_permit);
/** * rtrs_permit_to_clt_con() - returns RDMA connection pointer by the permit * @clt_path: client path pointer * @permit: permit for the allocation of the RDMA buffer * Note: * IO connection starts from 1. * 0 connection is for user messages.
*/ static struct rtrs_clt_con *rtrs_permit_to_clt_con(struct rtrs_clt_path *clt_path, struct rtrs_permit *permit)
{ int id = 0;
if (permit->con_type == RTRS_IO_CON)
id = (permit->cpu_id % (clt_path->s.irq_con_num - 1)) + 1;
return to_clt_con(clt_path->s.con[id]);
}
/** * rtrs_clt_change_state() - change the session state through session state * machine. * * @clt_path: client path to change the state of. * @new_state: state to change to. * * returns true if sess's state is changed to new state, otherwise return false. * * Locks: * state_wq lock must be hold.
*/ staticbool rtrs_clt_change_state(struct rtrs_clt_path *clt_path, enum rtrs_clt_state new_state)
{ enum rtrs_clt_state old_state; bool changed = false;
lockdep_assert_held(&clt_path->state_wq.lock);
old_state = clt_path->state; switch (new_state) { case RTRS_CLT_CONNECTING: switch (old_state) { case RTRS_CLT_RECONNECTING:
changed = true;
fallthrough; default: break;
} break; case RTRS_CLT_RECONNECTING: switch (old_state) { case RTRS_CLT_CONNECTED: case RTRS_CLT_CONNECTING_ERR: case RTRS_CLT_CLOSED:
changed = true;
fallthrough; default: break;
} break; case RTRS_CLT_CONNECTED: switch (old_state) { case RTRS_CLT_CONNECTING:
changed = true;
fallthrough; default: break;
} break; case RTRS_CLT_CONNECTING_ERR: switch (old_state) { case RTRS_CLT_CONNECTING:
changed = true;
fallthrough; default: break;
} break; case RTRS_CLT_CLOSING: switch (old_state) { case RTRS_CLT_CONNECTING: case RTRS_CLT_CONNECTING_ERR: case RTRS_CLT_RECONNECTING: case RTRS_CLT_CONNECTED:
changed = true;
fallthrough; default: break;
} break; case RTRS_CLT_CLOSED: switch (old_state) { case RTRS_CLT_CLOSING:
changed = true;
fallthrough; default: break;
} break; case RTRS_CLT_DEAD: switch (old_state) { case RTRS_CLT_CLOSED:
changed = true;
fallthrough; default: break;
} break; default: break;
} if (changed) {
clt_path->state = new_state;
wake_up_locked(&clt_path->state_wq);
}
if (rtrs_clt_change_state_from_to(clt_path,
RTRS_CLT_CONNECTED,
RTRS_CLT_RECONNECTING)) {
queue_work(rtrs_wq, &clt_path->err_recovery_work);
} else { /* * Error can happen just on establishing new connection, * so notify waiter with error state, waiter is responsible * for cleaning the rest and reconnect if needed.
*/
rtrs_clt_change_state_from_to(clt_path,
RTRS_CLT_CONNECTING,
RTRS_CLT_CONNECTING_ERR);
}
}
if (!req->in_use) return; if (WARN_ON(!req->con)) return;
clt_path = to_clt_path(con->c.path);
if (req->sg_cnt) { if (req->mr->need_inval) { /* * We are here to invalidate read/write requests * ourselves. In normal scenario server should * send INV for all read requests, we do local * invalidate for write requests ourselves, but * we are here, thus three things could happen: * * 1. this is failover, when errno != 0 * and can_wait == 1, * * 2. something totally bad happened and * server forgot to send INV, so we * should do that ourselves. * * 3. write request finishes, we need to do local * invalidate
*/
if (can_wait) {
req->need_inv_comp = true;
} else { /* This should be IO path, so always notify */
WARN_ON(!notify); /* Save errno for INV callback */
req->inv_errno = errno;
}
refcount_inc(&req->ref);
err = rtrs_inv_rkey(req); if (err) {
rtrs_err_rl(con->c.path, "Send INV WR key=%#x: %d\n",
req->mr->rkey, err);
} elseif (can_wait) {
wait_for_completion(&req->inv_comp);
} if (!refcount_dec_and_test(&req->ref)) return;
}
ib_dma_unmap_sg(clt_path->s.dev->ib_dev, req->sglist,
req->sg_cnt, req->dir);
} if (!refcount_dec_and_test(&req->ref)) return; if (req->mp_policy == MP_POLICY_MIN_INFLIGHT)
atomic_dec(&clt_path->stats->inflight);
if (!req->sg_size) {
rtrs_wrn(con->c.path, "Doing RDMA Write failed, no data supplied\n"); return -EINVAL;
}
/* user data and user message in the first list element */
sge.addr = req->iu->dma_addr;
sge.length = req->sg_size;
sge.lkey = clt_path->s.dev->ib_pd->local_dma_lkey;
/* * From time to time we have to post signalled sends, * or send queue will fill up and only QP reset can help.
*/
flags = atomic_inc_return(&con->c.wr_cnt) % clt_path->s.signal_interval ?
0 : IB_SEND_SIGNALED;
if (WARN_ON(msg_id >= clt_path->queue_depth)) return;
req = &clt_path->reqs[msg_id]; /* Drop need_inv if server responded with send with invalidation */
req->mr->need_inval &= !w_inval;
complete_rdma_req(req, errno, true, false);
}
/* * Post x2 empty WRs: first is for this RDMA with IMM, * second is for RECV with INV, which happened earlier.
*/ staticint rtrs_post_recv_empty_x2(struct rtrs_con *con, struct ib_cqe *cqe)
{ struct ib_recv_wr wr_arr[2], *wr; int i;
memset(wr_arr, 0, sizeof(wr_arr)); for (i = 0; i < ARRAY_SIZE(wr_arr); i++) {
wr = &wr_arr[i];
wr->wr_cqe = cqe; if (i) /* Chain backwards */
wr->next = &wr_arr[i - 1];
}
/* * rtrs_clt_get_next_path_or_null - get clt path from the list or return NULL * @head: the head for the list. * @clt_path: The element to take the next clt_path from. * * Next clt path returned in round-robin fashion, i.e. head will be skipped, * but if list is observed as empty, NULL will be returned. * * This function may safely run concurrently with the _rcu list-mutation * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
*/ staticinlinestruct rtrs_clt_path *
rtrs_clt_get_next_path_or_null(struct list_head *head, struct rtrs_clt_path *clt_path)
{ return list_next_or_null_rcu(head, &clt_path->s.entry, typeof(*clt_path), s.entry) ?:
list_next_or_null_rcu(head,
READ_ONCE((&clt_path->s.entry)->next),
typeof(*clt_path), s.entry);
}
/** * get_next_path_rr() - Returns path in round-robin fashion. * @it: the path pointer * * Related to @MP_POLICY_RR * * Locks: * rcu_read_lock() must be held.
*/ staticstruct rtrs_clt_path *get_next_path_rr(struct path_it *it)
{ struct rtrs_clt_path __rcu **ppcpu_path; struct rtrs_clt_path *path; struct rtrs_clt_sess *clt;
/* * Assert that rcu lock must be held
*/
RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu read lock held");
clt = it->clt;
/* * Here we use two RCU objects: @paths_list and @pcpu_path * pointer. See rtrs_clt_remove_path_from_arr() for details * how that is handled.
*/
/* * add the path to the skip list, so that next time we can get * a different one
*/ if (min_path)
list_add(raw_cpu_ptr(min_path->mp_skip_entry), &it->skip_list);
return min_path;
}
/** * get_next_path_min_latency() - Returns path with minimal latency. * @it: the path pointer * * Return: a path with the lowest latency or NULL if all paths are tried * * Locks: * rcu_read_lock() must be hold. * * Related to @MP_POLICY_MIN_LATENCY * * This DOES skip an already-tried path. * There is a skip-list to skip a path if the path has tried but failed. * It will try the minimum latency path and then the second minimum latency * path and so on. Finally it will return NULL if all paths are tried. * Therefore the caller MUST check the returned * path is NULL and trigger the IO error.
*/ staticstruct rtrs_clt_path *get_next_path_min_latency(struct path_it *it)
{ struct rtrs_clt_path *min_path = NULL; struct rtrs_clt_sess *clt = it->clt; struct rtrs_clt_path *clt_path;
ktime_t min_latency = KTIME_MAX;
ktime_t latency;
list_for_each_entry_rcu(clt_path, &clt->paths_list, s.entry) { if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED) continue;
if (!list_empty(raw_cpu_ptr(clt_path->mp_skip_entry))) continue;
/* * add the path to the skip list, so that next time we can get * a different one
*/ if (min_path)
list_add(raw_cpu_ptr(min_path->mp_skip_entry), &it->skip_list);
staticinlinevoid path_it_deinit(struct path_it *it)
{ struct list_head *skip, *tmp; /* * The skip_list is used only for the MIN_INFLIGHT and MIN_LATENCY policies. * We need to remove paths from it, so that next IO can insert * paths (->mp_skip_entry) into a skip_list again.
*/
list_for_each_safe(skip, tmp, &it->skip_list)
list_del_init(skip);
}
/** * rtrs_clt_init_req() - Initialize an rtrs_clt_io_req holding information * about an inflight IO. * The user buffer holding user control message (not data) is copied into * the corresponding buffer of rtrs_iu (req->iu->buf), which later on will * also hold the control message of rtrs. * @req: an io request holding information about IO. * @clt_path: client path * @conf: conformation callback function to notify upper layer. * @permit: permit for allocation of RDMA remote buffer * @priv: private pointer * @vec: kernel vector containing control message * @usr_len: length of the user message * @sg: scater list for IO data * @sg_cnt: number of scater list entries * @data_len: length of the IO data * @dir: direction of the IO.
*/ staticvoid rtrs_clt_init_req(struct rtrs_clt_io_req *req, struct rtrs_clt_path *clt_path, void (*conf)(void *priv, int errno), struct rtrs_permit *permit, void *priv, conststruct kvec *vec, size_t usr_len, struct scatterlist *sg, size_t sg_cnt,
size_t data_len, int dir)
{ struct iov_iter iter;
size_t len;
/* * From time to time we have to post signalled sends, * or send queue will fill up and only QP reset can help.
*/
flags = atomic_inc_return(&con->c.wr_cnt) % clt_path->s.signal_interval ?
0 : IB_SEND_SIGNALED;
staticint rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count)
{ int nr;
/* Align the MR to a 4K page size to match the block virt boundary */
nr = ib_map_mr_sg(req->mr, req->sglist, count, NULL, SZ_4K); if (nr != count) return nr < 0 ? nr : -EINVAL;
ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
if (tsize > clt_path->chunk_size) {
rtrs_wrn(s, "Write request failed, size too big %zu > %d\n",
tsize, clt_path->chunk_size); return -EMSGSIZE;
} if (req->sg_cnt) {
count = ib_dma_map_sg(clt_path->s.dev->ib_dev, req->sglist,
req->sg_cnt, req->dir); if (!count) {
rtrs_wrn(s, "Write request failed, map failed\n"); return -EINVAL;
}
} /* put rtrs msg after sg and user message */
msg = req->iu->buf + req->usr_len;
msg->type = cpu_to_le16(RTRS_MSG_WRITE);
msg->usr_len = cpu_to_le16(req->usr_len);
/* rtrs message on server side will be after user data and message */
imm = req->permit->mem_off + req->data_len + req->usr_len;
imm = rtrs_to_io_req_imm(imm);
buf_id = req->permit->mem_id;
req->sg_size = tsize;
rbuf = &clt_path->rbufs[buf_id];
if (count) {
ret = rtrs_map_sg_fr(req, count); if (ret < 0) {
rtrs_err_rl(s, "Write request failed, failed to map fast reg. data, err: %d\n",
ret);
ib_dma_unmap_sg(clt_path->s.dev->ib_dev, req->sglist,
req->sg_cnt, req->dir); return ret;
}
rwr = (struct ib_reg_wr) {
.wr.opcode = IB_WR_REG_MR,
.wr.wr_cqe = &fast_reg_cqe,
.mr = req->mr,
.key = req->mr->rkey,
.access = (IB_ACCESS_LOCAL_WRITE),
};
wr = &rwr.wr;
fr_en = true;
req->mr->need_inval = true;
} /* * Update stats now, after request is successfully sent it is not * safe anymore to touch it.
*/
rtrs_clt_update_all_stats(req, WRITE);
ret = rtrs_post_rdma_write_sg(req->con, req, rbuf, fr_en, count,
req->usr_len + sizeof(*msg),
imm, wr, NULL); if (ret) {
rtrs_err_rl(s, "Write request failed: error=%d path=%s [%s:%u]\n",
ret, kobject_name(&clt_path->kobj), clt_path->hca_name,
clt_path->hca_port); if (req->mp_policy == MP_POLICY_MIN_INFLIGHT)
atomic_dec(&clt_path->stats->inflight); if (req->mr->need_inval) {
req->mr->need_inval = false;
refcount_dec(&req->ref);
} if (req->sg_cnt)
ib_dma_unmap_sg(clt_path->s.dev->ib_dev, req->sglist,
req->sg_cnt, req->dir);
}
/* Further invalidation is required */
req->mr->need_inval = !!RTRS_MSG_NEED_INVAL_F;
} else {
msg->sg_cnt = 0;
msg->flags = 0;
} /* * rtrs message will be after the space reserved for disk data and * user message
*/
imm = req->permit->mem_off + req->data_len + req->usr_len;
imm = rtrs_to_io_req_imm(imm);
buf_id = req->permit->mem_id;
staticvoid fail_all_outstanding_reqs(struct rtrs_clt_path *clt_path)
{ struct rtrs_clt_sess *clt = clt_path->clt; struct rtrs_clt_io_req *req; int i, err;
if (!clt_path->reqs) return; for (i = 0; i < clt_path->queue_depth; ++i) {
req = &clt_path->reqs[i]; if (!req->in_use) continue;
/* * Safely (without notification) complete failed request. * After completion this request is still useble and can * be failovered to another path.
*/
complete_rdma_req(req, -ECONNABORTED, false, true);
/* * Use the smallest page size supported by the HCA, down to a * minimum of 4096 bytes. We're unlikely to build large sglists * out of smaller entries.
*/
mr_page_shift = max(12, ffs(ib_dev->attrs.page_size_cap) - 1);
max_pages_per_mr = ib_dev->attrs.max_mr_size;
do_div(max_pages_per_mr, (1ull << mr_page_shift));
clt_path->max_pages_per_mr =
min3(clt_path->max_pages_per_mr, (u32)max_pages_per_mr,
ib_dev->attrs.max_fast_reg_page_list_len);
clt_path->clt->max_segments =
min(clt_path->max_pages_per_mr, clt_path->clt->max_segments);
}
/* * rdma_resolve_addr() passes src_addr to cma_bind_addr, which * checks the sa_family to be non-zero. If user passed src_addr=NULL * the sess->src_addr will contain only zeros, which is then fine.
*/ if (path->src)
memcpy(&clt_path->s.src_addr, path->src,
rdma_addr_size((struct sockaddr *)path->src));
strscpy(clt_path->s.sessname, clt->sessname, sizeof(clt_path->s.sessname));
clt_path->clt = clt;
clt_path->max_pages_per_mr = RTRS_MAX_SEGMENTS;
init_waitqueue_head(&clt_path->state_wq);
clt_path->state = RTRS_CLT_CONNECTING;
atomic_set(&clt_path->connected_cnt, 0);
INIT_WORK(&clt_path->close_work, rtrs_clt_close_work);
INIT_WORK(&clt_path->err_recovery_work, rtrs_clt_err_recovery_work);
INIT_DELAYED_WORK(&clt_path->reconnect_dwork, rtrs_clt_reconnect_work);
rtrs_clt_init_hb(clt_path);
clt_path->mp_skip_entry = alloc_percpu(typeof(*clt_path->mp_skip_entry)); if (!clt_path->mp_skip_entry) goto err_free_stats;
con = kzalloc(sizeof(*con), GFP_KERNEL); if (!con) return -ENOMEM;
/* Map first two connections to the first CPU */
con->cpu = (cid ? cid - 1 : 0) % nr_cpu_ids;
con->c.cid = cid;
con->c.path = &clt_path->s; /* Align with srv, init as 1 */
atomic_set(&con->c.wr_cnt, 1);
mutex_init(&con->con_mutex);
lockdep_assert_held(&con->con_mutex); if (con->c.cid == 0) {
max_send_sge = 1; /* We must be the first here */ if (WARN_ON(clt_path->s.dev)) return -EINVAL;
/* * The whole session uses device from user connection. * Be careful not to close user connection before ib dev * is gracefully put.
*/
clt_path->s.dev = rtrs_ib_dev_find_or_add(con->c.cm_id->device,
&dev_pd); if (!clt_path->s.dev) {
rtrs_wrn(clt_path->clt, "rtrs_ib_dev_find_get_or_add(): no memory\n"); return -ENOMEM;
}
clt_path->s.dev_ref = 1;
query_fast_reg_mode(clt_path);
wr_limit = clt_path->s.dev->ib_dev->attrs.max_qp_wr; /* * Two (request + registration) completion for send * Two for recv if always_invalidate is set on server * or one for recv. * + 2 for drain and heartbeat * in case qp gets into error state.
*/
max_send_wr =
min_t(int, wr_limit, SERVICE_CON_QUEUE_DEPTH * 2 + 2);
max_recv_wr = max_send_wr;
} else { /* * Here we assume that session members are correctly set. * This is always true if user connection (cid == 0) is * established first.
*/ if (WARN_ON(!clt_path->s.dev)) return -EINVAL; if (WARN_ON(!clt_path->queue_depth)) return -EINVAL;
wr_limit = clt_path->s.dev->ib_dev->attrs.max_qp_wr; /* Shared between connections */
clt_path->s.dev_ref++;
max_send_wr = min_t(int, wr_limit, /* QD * (REQ + RSP + FR REGS or INVS) + drain */
clt_path->queue_depth * 4 + 1);
max_recv_wr = min_t(int, wr_limit,
clt_path->queue_depth * 3 + 1);
max_send_sge = 2;
}
atomic_set(&con->c.sq_wr_avail, max_send_wr);
cq_num = max_send_wr + max_recv_wr; /* alloc iu to recv new rkey reply when server reports flags set */ if (clt_path->flags & RTRS_MSG_NEW_RKEY_F || con->c.cid == 0) {
con->rsp_ius = rtrs_iu_alloc(cq_num, sizeof(*rsp),
GFP_KERNEL,
clt_path->s.dev->ib_dev,
DMA_FROM_DEVICE,
rtrs_clt_rdma_done); if (!con->rsp_ius) return -ENOMEM;
con->queue_num = cq_num;
}
cq_vector = con->cpu % clt_path->s.dev->ib_dev->num_comp_vectors; if (con->c.cid >= clt_path->s.irq_con_num)
err = rtrs_cq_qp_create(&clt_path->s, &con->c, max_send_sge,
cq_vector, cq_num, max_send_wr,
max_recv_wr, IB_POLL_DIRECT); else
err = rtrs_cq_qp_create(&clt_path->s, &con->c, max_send_sge,
cq_vector, cq_num, max_send_wr,
max_recv_wr, IB_POLL_SOFTIRQ); /* * In case of error we do not bother to clean previous allocations, * since destroy_con_cq_qp() must be called.
*/ return err;
}
/* * Stop any more reconnection attempts
*/
clt_path->reconnect_attempts = -1;
rtrs_err(clt, "Disabling auto-reconnect. Trigger a manual reconnect after issue is resolved\n"); return -ECONNRESET;
}
/* * Global IO size is always a minimum. * If while a reconnection server sends us a value a bit * higher - client does not care and uses cached minimum. * * Since we can have several sessions (paths) restablishing * connections in parallel, use lock.
*/
mutex_lock(&clt->paths_mutex);
clt->queue_depth = clt_path->queue_depth;
clt->max_io_size = min_not_zero(clt_path->max_io_size,
clt->max_io_size);
mutex_unlock(&clt->paths_mutex);
/* * Cache the hca_port and hca_name for sysfs
*/
clt_path->hca_port = con->c.cm_id->port_num;
scnprintf(clt_path->hca_name, sizeof(clt_path->hca_name),
clt_path->s.dev->ib_dev->name);
clt_path->s.src_addr = con->c.cm_id->route.addr.src_addr; /* set for_new_clt, to allow future reconnect on any path */
clt_path->for_new_clt = 1;
}
if (rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CLOSING, NULL))
queue_work(rtrs_wq, &clt_path->close_work); if (wait)
flush_work(&clt_path->close_work);
}
staticinlinevoid flag_error_on_conn(struct rtrs_clt_con *con, int cm_err)
{ if (con->cm_err == 1) { struct rtrs_clt_path *clt_path;
clt_path = to_clt_path(con->c.path); if (atomic_dec_and_test(&clt_path->connected_cnt))
switch (ev->event) { case RDMA_CM_EVENT_ADDR_RESOLVED:
cm_err = rtrs_rdma_addr_resolved(con); break; case RDMA_CM_EVENT_ROUTE_RESOLVED:
cm_err = rtrs_rdma_route_resolved(con); break; case RDMA_CM_EVENT_ESTABLISHED:
cm_err = rtrs_rdma_conn_established(con, ev); if (!cm_err) { /* * Report success and wake up. Here we abuse state_wq, * i.e. wake up without state change, but we set cm_err.
*/
flag_success_on_conn(con);
wake_up(&clt_path->state_wq); return 0;
} break; case RDMA_CM_EVENT_REJECTED:
cm_err = rtrs_rdma_conn_rejected(con, ev); break; case RDMA_CM_EVENT_DISCONNECTED: /* No message for disconnecting */
cm_err = -ECONNRESET; break; case RDMA_CM_EVENT_CONNECT_ERROR: case RDMA_CM_EVENT_UNREACHABLE: case RDMA_CM_EVENT_ADDR_CHANGE: case RDMA_CM_EVENT_TIMEWAIT_EXIT:
rtrs_wrn(s, "CM error (CM event: %s, err: %d)\n",
rdma_event_msg(ev->event), ev->status);
cm_err = -ECONNRESET; break; case RDMA_CM_EVENT_ADDR_ERROR: case RDMA_CM_EVENT_ROUTE_ERROR:
rtrs_wrn(s, "CM error (CM event: %s, err: %d)\n",
rdma_event_msg(ev->event), ev->status);
cm_err = -EHOSTUNREACH; break; case RDMA_CM_EVENT_DEVICE_REMOVAL: /* * Device removal is a special case. Queue close and return 0.
*/
rtrs_wrn_rl(s, "CM event: %s, status: %d\n", rdma_event_msg(ev->event),
ev->status);
rtrs_clt_close_conns(clt_path, false); return 0; default:
rtrs_err(s, "Unexpected RDMA CM error (CM event: %s, err: %d)\n",
rdma_event_msg(ev->event), ev->status);
cm_err = -ECONNRESET; break;
}
if (cm_err) { /* * cm error makes sense only on connection establishing, * in other cases we rely on normal procedure of reconnecting.
*/
flag_error_on_conn(con, cm_err);
rtrs_rdma_error_recovery(con);
}
return 0;
}
/* The caller should do the cleanup in case of error */ staticint create_cm(struct rtrs_clt_con *con)
{ struct rtrs_path *s = con->c.path; struct rtrs_clt_path *clt_path = to_clt_path(s); struct rdma_cm_id *cm_id; int err;
cm_id = rdma_create_id(&init_net, rtrs_clt_rdma_cm_handler, con,
clt_path->s.dst_addr.ss_family == AF_IB ?
RDMA_PS_IB : RDMA_PS_TCP, IB_QPT_RC); if (IS_ERR(cm_id)) {
rtrs_err(s, "Failed to create CM ID, err: %pe\n", cm_id); return PTR_ERR(cm_id);
}
con->c.cm_id = cm_id;
con->cm_err = 0; /* allow the port to be reused */
err = rdma_set_reuseaddr(cm_id, 1); if (err != 0) {
rtrs_err(s, "Set address reuse failed, err: %d\n", err); return err;
}
err = rdma_resolve_addr(cm_id, (struct sockaddr *)&clt_path->s.src_addr,
(struct sockaddr *)&clt_path->s.dst_addr,
RTRS_CONNECT_TIMEOUT_MS); if (err) {
rtrs_err(s, "Failed to resolve address, err: %d\n", err); return err;
} /* * Combine connection status and session events. This is needed * for waiting two possible cases: cm_err has something meaningful * or session state was really changed to error by device removal.
*/
err = wait_event_interruptible_timeout(
clt_path->state_wq,
con->cm_err || clt_path->state != RTRS_CLT_CONNECTING,
msecs_to_jiffies(RTRS_CONNECT_TIMEOUT_MS)); if (err == 0 || err == -ERESTARTSYS) { if (err == 0)
err = -ETIMEDOUT; /* Timedout or interrupted */ return err;
} if (con->cm_err < 0) return con->cm_err; if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTING) /* Device removal */ return -ECONNABORTED;
/* * We can fire RECONNECTED event only when all paths were * connected on rtrs_clt_open(), then each was disconnected * and the first one connected again. That's why this nasty * game with counter value.
*/
mutex_lock(&clt->paths_ev_mutex);
up = ++clt->paths_up; /* * Here it is safe to access paths num directly since up counter * is greater than MAX_PATHS_NUM only while rtrs_clt_open() is * in progress, thus paths removals are impossible.
*/ if (up > MAX_PATHS_NUM && up == MAX_PATHS_NUM + clt->paths_num)
clt->paths_up = clt->paths_num; elseif (up == 1)
clt->link_ev(clt->priv, RTRS_CLT_LINK_EV_RECONNECTED);
mutex_unlock(&clt->paths_ev_mutex);
/* Mark session as established */
clt_path->established = true;
clt_path->reconnect_attempts = 0;
clt_path->stats->reconnects.successful_cnt++;
}
/* * Possible race with rtrs_clt_open(), when DEVICE_REMOVAL comes * exactly in between. Start destroying after it finishes.
*/
mutex_lock(&clt_path->init_mutex);
mutex_unlock(&clt_path->init_mutex);
/* * All IO paths must observe !CONNECTED state before we * free everything.
*/
synchronize_rcu();
rtrs_stop_hb(&clt_path->s);
/* * The order it utterly crucial: firstly disconnect and complete all * rdma requests with error (thus set in_use=false for requests), * then fail outstanding requests checking in_use for each, and * eventually notify upper layer about session disconnection.
*/
for (cid = 0; cid < clt_path->s.con_num; cid++) { if (!clt_path->s.con[cid]) break;
con = to_clt_con(clt_path->s.con[cid]);
stop_cm(con);
}
fail_all_outstanding_reqs(clt_path);
free_path_reqs(clt_path);
rtrs_clt_path_down(clt_path);
/* * Wait for graceful shutdown, namely when peer side invokes * rdma_disconnect(). 'connected_cnt' is decremented only on * CM events, thus if other side had crashed and hb has detected * something is wrong, here we will stuck for exactly timeout ms, * since CM does not fire anything. That is fine, we are not in * hurry.
*/
wait_event_timeout(clt_path->state_wq,
!atomic_read(&clt_path->connected_cnt),
msecs_to_jiffies(RTRS_CONNECT_TIMEOUT_MS));
for (cid = 0; cid < clt_path->s.con_num; cid++) { if (!clt_path->s.con[cid]) break;
con = to_clt_con(clt_path->s.con[cid]);
mutex_lock(&con->con_mutex);
destroy_con_cq_qp(con);
mutex_unlock(&con->con_mutex);
destroy_cm(con);
destroy_con(con);
}
}
/* Make sure everybody observes path removal. */
synchronize_rcu();
/* * At this point nobody sees @sess in the list, but still we have * dangling pointer @pcpu_path which _can_ point to @sess. Since * nobody can observe @sess in the list, we guarantee that IO path * will not assign @sess to @pcpu_path, i.e. @pcpu_path can be equal * to @sess, but can never again become @sess.
*/
/* * Decrement paths number only after grace period, because * caller of do_each_path() must firstly observe list without * path and only then decremented paths number. * * Otherwise there can be the following situation: * o Two paths exist and IO is coming. * o One path is removed: * CPU#0 CPU#1 * do_each_path(): rtrs_clt_remove_path_from_arr(): * path = get_next_path() * ^^^ list_del_rcu(path) * [!CONNECTED path] clt->paths_num-- * ^^^^^^^^^ * load clt->paths_num from 2 to 1 * ^^^^^^^^^ * sees 1 * * path is observed as !CONNECTED, but do_each_path() loop * ends, because expression i < clt->paths_num is false.
*/
clt->paths_num--;
/* * Get @next connection from current @sess which is going to be * removed. If @sess is the last element, then @next is NULL.
*/
rcu_read_lock();
next = rtrs_clt_get_next_path_or_null(&clt->paths_list, clt_path);
rcu_read_unlock();
/* * @pcpu paths can still point to the path which is going to be * removed, so change the pointer manually.
*/
for_each_possible_cpu(cpu) { struct rtrs_clt_path __rcu **ppcpu_path;
ppcpu_path = per_cpu_ptr(clt->pcpu_path, cpu); if (rcu_dereference_protected(*ppcpu_path,
lockdep_is_held(&clt->paths_mutex)) != clt_path) /* * synchronize_rcu() was called just after deleting * entry from the list, thus IO code path cannot * change pointer back to the pointer which is going * to be removed, we are safe here.
*/ continue;
/* * We race with IO code path, which also changes pointer, * thus we have to be careful not to overwrite it.
*/ if (try_cmpxchg((struct rtrs_clt_path **)ppcpu_path, &clt_path,
next)) /* * @ppcpu_path was successfully replaced with @next, * that means that someone could also pick up the * @sess and dereferencing it right now, so wait for * a grace period is required.
*/
wait_for_grace = true;
} if (wait_for_grace)
synchronize_rcu();
staticint init_conns(struct rtrs_clt_path *clt_path)
{ unsignedint cid; int err, i;
/* * On every new session connections increase reconnect counter * to avoid clashes with previous sessions not yet closed * sessions on a server side.
*/
clt_path->s.recon_cnt++;
/* Establish all RDMA connections */ for (cid = 0; cid < clt_path->s.con_num; cid++) {
err = create_con(clt_path, cid); if (err) goto destroy;
err = create_cm(to_clt_con(clt_path->s.con[cid])); if (err) goto destroy;
}
/* * Set the cid to con_num - 1, since if we fail later, we want to stay in bounds.
*/
cid = clt_path->s.con_num - 1;
err = alloc_path_reqs(clt_path); if (err) goto destroy;
return 0;
destroy: /* Make sure we do the cleanup in the order they are created */ for (i = 0; i <= cid; i++) { struct rtrs_clt_con *con;
if (!clt_path->s.con[i]) break;
con = to_clt_con(clt_path->s.con[i]); if (con->c.cm_id) {
stop_cm(con);
mutex_lock(&con->con_mutex);
destroy_con_cq_qp(con);
mutex_unlock(&con->con_mutex);
destroy_cm(con);
}
destroy_con(con);
} /* * If we've never taken async path and got an error, say, * doing rdma_resolve_addr(), switch to CONNECTION_ERR state * manually to keep reconnecting.
*/
rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CONNECTING_ERR, NULL);
/* Wait for state change */
wait_event_interruptible_timeout(clt_path->state_wq,
clt_path->state != RTRS_CLT_CONNECTING,
msecs_to_jiffies(
RTRS_CONNECT_TIMEOUT_MS)); if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED) { if (READ_ONCE(clt_path->state) == RTRS_CLT_CONNECTING_ERR)
err = -ECONNRESET; else
err = -ETIMEDOUT;
}
out: if (tx_iu)
rtrs_iu_free(tx_iu, clt_path->s.dev->ib_dev, 1); if (rx_iu)
rtrs_iu_free(rx_iu, clt_path->s.dev->ib_dev, 1); if (err) /* If we've never taken async path because of malloc problems */
rtrs_clt_change_state_get_old(clt_path,
RTRS_CLT_CONNECTING_ERR, NULL);
return err;
}
/** * init_path() - establishes all path connections and does handshake * @clt_path: client path. * In case of error full close or reconnect procedure should be taken, * because reconnect or close async works can be started.
*/ staticint init_path(struct rtrs_clt_path *clt_path)
{ int err; char str[NAME_MAX]; struct rtrs_addr path = {
.src = &clt_path->s.src_addr,
.dst = &clt_path->s.dst_addr,
};
if (READ_ONCE(clt_path->state) != RTRS_CLT_RECONNECTING) return;
if (clt_path->reconnect_attempts >= clt->max_reconnect_attempts) { /* Close a path completely if max attempts is reached */
rtrs_clt_close_conns(clt_path, false); return;
}
clt_path->reconnect_attempts++;
msleep(RTRS_RECONNECT_BACKOFF); if (rtrs_clt_change_state_get_old(clt_path, RTRS_CLT_CONNECTING, NULL)) {
err = init_path(clt_path); if (err) goto reconnect_again;
}
err = dev_set_name(&clt->dev, "%s", sessname); if (err) goto err_put;
/* * Suppress user space notification until * sysfs files are created
*/
dev_set_uevent_suppress(&clt->dev, true);
err = device_add(&clt->dev); if (err) goto err_put;
/* * release callback will free clt and destroy mutexes in last put
*/
device_unregister(&clt->dev);
}
/** * rtrs_clt_open() - Open a path to an RTRS server * @ops: holds the link event callback and the private pointer. * @pathname: name of the path to an RTRS server * @paths: Paths to be established defined by their src and dst addresses * @paths_num: Number of elements in the @paths array * @port: port to be used by the RTRS session * @pdu_sz: Size of extra payload which can be accessed after permit allocation. * @reconnect_delay_sec: time between reconnect tries * @max_reconnect_attempts: Number of times to reconnect on error before giving * up, 0 for * disabled, -1 for forever * @nr_poll_queues: number of polling mode connection using IB_POLL_DIRECT flag * * Starts session establishment with the rtrs_server. The function can block * up to ~2000ms before it returns. * * Return a valid pointer on success otherwise PTR_ERR.
*/ struct rtrs_clt_sess *rtrs_clt_open(struct rtrs_clt_ops *ops, constchar *pathname, conststruct rtrs_addr *paths,
size_t paths_num, u16 port,
size_t pdu_sz, u8 reconnect_delay_sec,
s16 max_reconnect_attempts, u32 nr_poll_queues)
{ struct rtrs_clt_path *clt_path, *tmp; struct rtrs_clt_sess *clt; int err, i;
if (strchr(pathname, '/') || strchr(pathname, '.')) {
pr_err("pathname cannot contain / and .\n");
err = -EINVAL; goto out;
}
clt = alloc_clt(pathname, paths_num, port, pdu_sz, ops->priv,
ops->link_ev,
reconnect_delay_sec,
max_reconnect_attempts); if (IS_ERR(clt)) {
err = PTR_ERR(clt); goto out;
} for (i = 0; i < paths_num; i++) { struct rtrs_clt_path *clt_path;
/* Now it is safe to iterate over all paths without locks */
list_for_each_entry_safe(clt_path, tmp, &clt->paths_list, s.entry) {
rtrs_clt_close_conns(clt_path, true);
rtrs_clt_destroy_path_files(clt_path, NULL);
kobject_put(&clt_path->kobj);
}
free_permits(clt);
free_clt(clt);
}
EXPORT_SYMBOL(rtrs_clt_close);
int rtrs_clt_reconnect_from_sysfs(struct rtrs_clt_path *clt_path)
{ enum rtrs_clt_state old_state; int err = -EBUSY; bool changed;
changed = rtrs_clt_change_state_get_old(clt_path,
RTRS_CLT_RECONNECTING,
&old_state); if (changed) {
clt_path->reconnect_attempts = 0;
rtrs_clt_stop_and_destroy_conns(clt_path);
queue_delayed_work(rtrs_wq, &clt_path->reconnect_dwork, 0);
} if (changed || old_state == RTRS_CLT_RECONNECTING) { /* * flush_delayed_work() queues pending work for immediate * execution, so do the flush if we have queued something * right now or work is pending.
*/
flush_delayed_work(&clt_path->reconnect_dwork);
err = (READ_ONCE(clt_path->state) ==
RTRS_CLT_CONNECTED ? 0 : -ENOTCONN);
}
/* * Continue stopping path till state was changed to DEAD or * state was observed as DEAD: * 1. State was changed to DEAD - we were fast and nobody * invoked rtrs_clt_reconnect(), which can again start * reconnecting. * 2. State was observed as DEAD - we have someone in parallel * removing the path.
*/ do {
rtrs_clt_close_conns(clt_path, true);
changed = rtrs_clt_change_state_get_old(clt_path,
RTRS_CLT_DEAD,
&old_state);
} while (!changed && old_state != RTRS_CLT_DEAD);
if (changed) {
rtrs_clt_remove_path_from_arr(clt_path);
rtrs_clt_destroy_path_files(clt_path, sysfs_self);
kobject_put(&clt_path->kobj);
}
int rtrs_clt_get_max_reconnect_attempts(conststruct rtrs_clt_sess *clt)
{ return (int)clt->max_reconnect_attempts;
}
/** * rtrs_clt_request() - Request data transfer to/from server via RDMA. * * @dir: READ/WRITE * @ops: callback function to be called as confirmation, and the pointer. * @clt: Session * @permit: Preallocated permit * @vec: Message that is sent to server together with the request. * Sum of len of all @vec elements limited to <= IO_MSG_SIZE. * Since the msg is copied internally it can be allocated on stack. * @nr: Number of elements in @vec. * @data_len: length of data sent to/from server * @sg: Pages to be sent/received to/from server. * @sg_cnt: Number of elements in the @sg * * Return: * 0: Success * <0: Error * * On dir=READ rtrs client will request a data transfer from Server to client. * The data that the server will respond with will be stored in @sg when * the user receives an %RTRS_CLT_RDMA_EV_RDMA_REQUEST_WRITE_COMPL event. * On dir=WRITE rtrs client will rdma write data in sg to server side.
*/ int rtrs_clt_request(int dir, struct rtrs_clt_req_ops *ops, struct rtrs_clt_sess *clt, struct rtrs_permit *permit, conststruct kvec *vec, size_t nr, size_t data_len, struct scatterlist *sg, unsignedint sg_cnt)
{ struct rtrs_clt_io_req *req; struct rtrs_clt_path *clt_path;
rcu_read_lock(); for (path_it_init(&it, clt);
(clt_path = it.next_path(&it)) && it.i < it.clt->paths_num; it.i++) { if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED) continue;
if (usr_len + hdr_len > clt_path->max_hdr_size) {
rtrs_wrn_rl(clt_path->clt, "%s request failed, user message size is %zu and header length %zu, but max size is %u\n",
dir == READ ? "Read" : "Write",
usr_len, hdr_len, clt_path->max_hdr_size);
err = -EMSGSIZE; break;
}
req = rtrs_clt_get_req(clt_path, ops->conf_fn, permit, ops->priv,
vec, usr_len, sg, sg_cnt, data_len,
dma_dir); if (dir == READ)
err = rtrs_clt_read_req(req); else
err = rtrs_clt_write_req(req); if (err) {
req->in_use = false; continue;
} /* Success path */ break;
}
path_it_deinit(&it);
rcu_read_unlock();
return err;
}
EXPORT_SYMBOL(rtrs_clt_request);
int rtrs_clt_rdma_cq_direct(struct rtrs_clt_sess *clt, unsignedint index)
{ /* If no path, return -1 for block layer not to try again */ int cnt = -1; struct rtrs_con *con; struct rtrs_clt_path *clt_path; struct path_it it;
rcu_read_lock(); for (path_it_init(&it, clt);
(clt_path = it.next_path(&it)) && it.i < it.clt->paths_num; it.i++) { if (READ_ONCE(clt_path->state) != RTRS_CLT_CONNECTED) continue;
con = clt_path->s.con[index + 1];
cnt = ib_process_cq_direct(con->cq, -1); if (cnt) break;
}
path_it_deinit(&it);
rcu_read_unlock();
/** * rtrs_clt_query() - queries RTRS session attributes *@clt: session pointer *@attr: query results for session attributes. * Returns: * 0 on success * -ECOMM no connection to the server
*/ int rtrs_clt_query(struct rtrs_clt_sess *clt, struct rtrs_attrs *attr)
{ if (!rtrs_clt_is_connected(clt)) return -ECOMM;
attr->queue_depth = clt->queue_depth;
attr->max_segments = clt->max_segments; /* Cap max_io_size to min of remote buffer size and the fr pages */
attr->max_io_size = min_t(int, clt->max_io_size,
clt->max_segments * SZ_4K);
return 0;
}
EXPORT_SYMBOL(rtrs_clt_query);
int rtrs_clt_create_path_from_sysfs(struct rtrs_clt_sess *clt, struct rtrs_addr *addr)
{ struct rtrs_clt_path *clt_path; int err;
clt_path = alloc_path(clt, addr, nr_cpu_ids, 0); if (IS_ERR(clt_path)) return PTR_ERR(clt_path);
mutex_lock(&clt->paths_mutex); if (clt->paths_num == 0) { /* * When all the paths are removed for a session, * the addition of the first path is like a new session for * the storage server
*/
clt_path->for_new_clt = 1;
}
mutex_unlock(&clt->paths_mutex);
/* * It is totally safe to add path in CONNECTING state: coming * IO will never grab it. Also it is very important to add * path before init, since init fires LINK_CONNECTED event.
*/
rtrs_clt_add_path_to_arr(clt_path);
err = init_path(clt_path); if (err) goto close_path;
err = rtrs_clt_create_path_files(clt_path); if (err) goto close_path;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.