linux/drivers/infiniband/ulp/rtrs/rtrs.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * RDMA Transport Layer
   4 *
   5 * Copyright (c) 2014 - 2018 ProfitBricks GmbH. All rights reserved.
   6 * Copyright (c) 2018 - 2019 1&1 IONOS Cloud GmbH. All rights reserved.
   7 * Copyright (c) 2019 - 2020 1&1 IONOS SE. All rights reserved.
   8 */
   9#undef pr_fmt
  10#define pr_fmt(fmt) KBUILD_MODNAME " L" __stringify(__LINE__) ": " fmt
  11
  12#include <linux/module.h>
  13#include <linux/inet.h>
  14
  15#include "rtrs-pri.h"
  16#include "rtrs-log.h"
  17
  18MODULE_DESCRIPTION("RDMA Transport Core");
  19MODULE_LICENSE("GPL");
  20
  21struct rtrs_iu *rtrs_iu_alloc(u32 queue_size, size_t size, gfp_t gfp_mask,
  22                              struct ib_device *dma_dev,
  23                              enum dma_data_direction dir,
  24                              void (*done)(struct ib_cq *cq, struct ib_wc *wc))
  25{
  26        struct rtrs_iu *ius, *iu;
  27        int i;
  28
  29        ius = kcalloc(queue_size, sizeof(*ius), gfp_mask);
  30        if (!ius)
  31                return NULL;
  32        for (i = 0; i < queue_size; i++) {
  33                iu = &ius[i];
  34                iu->direction = dir;
  35                iu->buf = kzalloc(size, gfp_mask);
  36                if (!iu->buf)
  37                        goto err;
  38
  39                iu->dma_addr = ib_dma_map_single(dma_dev, iu->buf, size, dir);
  40                if (ib_dma_mapping_error(dma_dev, iu->dma_addr))
  41                        goto err;
  42
  43                iu->cqe.done  = done;
  44                iu->size      = size;
  45        }
  46        return ius;
  47err:
  48        rtrs_iu_free(ius, dma_dev, i);
  49        return NULL;
  50}
  51EXPORT_SYMBOL_GPL(rtrs_iu_alloc);
  52
  53void rtrs_iu_free(struct rtrs_iu *ius, struct ib_device *ibdev, u32 queue_size)
  54{
  55        struct rtrs_iu *iu;
  56        int i;
  57
  58        if (!ius)
  59                return;
  60
  61        for (i = 0; i < queue_size; i++) {
  62                iu = &ius[i];
  63                ib_dma_unmap_single(ibdev, iu->dma_addr, iu->size, iu->direction);
  64                kfree(iu->buf);
  65        }
  66        kfree(ius);
  67}
  68EXPORT_SYMBOL_GPL(rtrs_iu_free);
  69
  70int rtrs_iu_post_recv(struct rtrs_con *con, struct rtrs_iu *iu)
  71{
  72        struct rtrs_sess *sess = con->sess;
  73        struct ib_recv_wr wr;
  74        struct ib_sge list;
  75
  76        list.addr   = iu->dma_addr;
  77        list.length = iu->size;
  78        list.lkey   = sess->dev->ib_pd->local_dma_lkey;
  79
  80        if (list.length == 0) {
  81                rtrs_wrn(con->sess,
  82                          "Posting receive work request failed, sg list is empty\n");
  83                return -EINVAL;
  84        }
  85        wr = (struct ib_recv_wr) {
  86                .wr_cqe  = &iu->cqe,
  87                .sg_list = &list,
  88                .num_sge = 1,
  89        };
  90
  91        return ib_post_recv(con->qp, &wr, NULL);
  92}
  93EXPORT_SYMBOL_GPL(rtrs_iu_post_recv);
  94
  95int rtrs_post_recv_empty(struct rtrs_con *con, struct ib_cqe *cqe)
  96{
  97        struct ib_recv_wr wr;
  98
  99        wr = (struct ib_recv_wr) {
 100                .wr_cqe  = cqe,
 101        };
 102
 103        return ib_post_recv(con->qp, &wr, NULL);
 104}
 105EXPORT_SYMBOL_GPL(rtrs_post_recv_empty);
 106
 107static int rtrs_post_send(struct ib_qp *qp, struct ib_send_wr *head,
 108                             struct ib_send_wr *wr)
 109{
 110        if (head) {
 111                struct ib_send_wr *tail = head;
 112
 113                while (tail->next)
 114                        tail = tail->next;
 115                tail->next = wr;
 116        } else {
 117                head = wr;
 118        }
 119
 120        return ib_post_send(qp, head, NULL);
 121}
 122
 123int rtrs_iu_post_send(struct rtrs_con *con, struct rtrs_iu *iu, size_t size,
 124                       struct ib_send_wr *head)
 125{
 126        struct rtrs_sess *sess = con->sess;
 127        struct ib_send_wr wr;
 128        struct ib_sge list;
 129
 130        if (WARN_ON(size == 0))
 131                return -EINVAL;
 132
 133        list.addr   = iu->dma_addr;
 134        list.length = size;
 135        list.lkey   = sess->dev->ib_pd->local_dma_lkey;
 136
 137        wr = (struct ib_send_wr) {
 138                .wr_cqe     = &iu->cqe,
 139                .sg_list    = &list,
 140                .num_sge    = 1,
 141                .opcode     = IB_WR_SEND,
 142                .send_flags = IB_SEND_SIGNALED,
 143        };
 144
 145        return rtrs_post_send(con->qp, head, &wr);
 146}
 147EXPORT_SYMBOL_GPL(rtrs_iu_post_send);
 148
 149int rtrs_iu_post_rdma_write_imm(struct rtrs_con *con, struct rtrs_iu *iu,
 150                                 struct ib_sge *sge, unsigned int num_sge,
 151                                 u32 rkey, u64 rdma_addr, u32 imm_data,
 152                                 enum ib_send_flags flags,
 153                                 struct ib_send_wr *head)
 154{
 155        struct ib_rdma_wr wr;
 156        int i;
 157
 158        wr = (struct ib_rdma_wr) {
 159                .wr.wr_cqe        = &iu->cqe,
 160                .wr.sg_list       = sge,
 161                .wr.num_sge       = num_sge,
 162                .rkey             = rkey,
 163                .remote_addr      = rdma_addr,
 164                .wr.opcode        = IB_WR_RDMA_WRITE_WITH_IMM,
 165                .wr.ex.imm_data = cpu_to_be32(imm_data),
 166                .wr.send_flags  = flags,
 167        };
 168
 169        /*
 170         * If one of the sges has 0 size, the operation will fail with a
 171         * length error
 172         */
 173        for (i = 0; i < num_sge; i++)
 174                if (WARN_ON(sge[i].length == 0))
 175                        return -EINVAL;
 176
 177        return rtrs_post_send(con->qp, head, &wr.wr);
 178}
 179EXPORT_SYMBOL_GPL(rtrs_iu_post_rdma_write_imm);
 180
 181int rtrs_post_rdma_write_imm_empty(struct rtrs_con *con, struct ib_cqe *cqe,
 182                                    u32 imm_data, enum ib_send_flags flags,
 183                                    struct ib_send_wr *head)
 184{
 185        struct ib_rdma_wr wr;
 186
 187        wr = (struct ib_rdma_wr) {
 188                .wr.wr_cqe      = cqe,
 189                .wr.send_flags  = flags,
 190                .wr.opcode      = IB_WR_RDMA_WRITE_WITH_IMM,
 191                .wr.ex.imm_data = cpu_to_be32(imm_data),
 192        };
 193
 194        return rtrs_post_send(con->qp, head, &wr.wr);
 195}
 196EXPORT_SYMBOL_GPL(rtrs_post_rdma_write_imm_empty);
 197
 198static void qp_event_handler(struct ib_event *ev, void *ctx)
 199{
 200        struct rtrs_con *con = ctx;
 201
 202        switch (ev->event) {
 203        case IB_EVENT_COMM_EST:
 204                rtrs_info(con->sess, "QP event %s (%d) received\n",
 205                           ib_event_msg(ev->event), ev->event);
 206                rdma_notify(con->cm_id, IB_EVENT_COMM_EST);
 207                break;
 208        default:
 209                rtrs_info(con->sess, "Unhandled QP event %s (%d) received\n",
 210                           ib_event_msg(ev->event), ev->event);
 211                break;
 212        }
 213}
 214
 215static int create_cq(struct rtrs_con *con, int cq_vector, u16 cq_size,
 216                     enum ib_poll_context poll_ctx)
 217{
 218        struct rdma_cm_id *cm_id = con->cm_id;
 219        struct ib_cq *cq;
 220
 221        cq = ib_alloc_cq(cm_id->device, con, cq_size,
 222                         cq_vector, poll_ctx);
 223        if (IS_ERR(cq)) {
 224                rtrs_err(con->sess, "Creating completion queue failed, errno: %ld\n",
 225                          PTR_ERR(cq));
 226                return PTR_ERR(cq);
 227        }
 228        con->cq = cq;
 229
 230        return 0;
 231}
 232
 233static int create_qp(struct rtrs_con *con, struct ib_pd *pd,
 234                     u32 max_send_wr, u32 max_recv_wr, u32 max_sge)
 235{
 236        struct ib_qp_init_attr init_attr = {NULL};
 237        struct rdma_cm_id *cm_id = con->cm_id;
 238        int ret;
 239
 240        init_attr.cap.max_send_wr = max_send_wr;
 241        init_attr.cap.max_recv_wr = max_recv_wr;
 242        init_attr.cap.max_recv_sge = 1;
 243        init_attr.event_handler = qp_event_handler;
 244        init_attr.qp_context = con;
 245        init_attr.cap.max_send_sge = max_sge;
 246
 247        init_attr.qp_type = IB_QPT_RC;
 248        init_attr.send_cq = con->cq;
 249        init_attr.recv_cq = con->cq;
 250        init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
 251
 252        ret = rdma_create_qp(cm_id, pd, &init_attr);
 253        if (ret) {
 254                rtrs_err(con->sess, "Creating QP failed, err: %d\n", ret);
 255                return ret;
 256        }
 257        con->qp = cm_id->qp;
 258
 259        return ret;
 260}
 261
 262int rtrs_cq_qp_create(struct rtrs_sess *sess, struct rtrs_con *con,
 263                       u32 max_send_sge, int cq_vector, int cq_size,
 264                       u32 max_send_wr, u32 max_recv_wr,
 265                       enum ib_poll_context poll_ctx)
 266{
 267        int err;
 268
 269        err = create_cq(con, cq_vector, cq_size, poll_ctx);
 270        if (err)
 271                return err;
 272
 273        err = create_qp(con, sess->dev->ib_pd, max_send_wr, max_recv_wr,
 274                        max_send_sge);
 275        if (err) {
 276                ib_free_cq(con->cq);
 277                con->cq = NULL;
 278                return err;
 279        }
 280        con->sess = sess;
 281
 282        return 0;
 283}
 284EXPORT_SYMBOL_GPL(rtrs_cq_qp_create);
 285
 286void rtrs_cq_qp_destroy(struct rtrs_con *con)
 287{
 288        if (con->qp) {
 289                rdma_destroy_qp(con->cm_id);
 290                con->qp = NULL;
 291        }
 292        if (con->cq) {
 293                ib_free_cq(con->cq);
 294                con->cq = NULL;
 295        }
 296}
 297EXPORT_SYMBOL_GPL(rtrs_cq_qp_destroy);
 298
 299static void schedule_hb(struct rtrs_sess *sess)
 300{
 301        queue_delayed_work(sess->hb_wq, &sess->hb_dwork,
 302                           msecs_to_jiffies(sess->hb_interval_ms));
 303}
 304
 305void rtrs_send_hb_ack(struct rtrs_sess *sess)
 306{
 307        struct rtrs_con *usr_con = sess->con[0];
 308        u32 imm;
 309        int err;
 310
 311        imm = rtrs_to_imm(RTRS_HB_ACK_IMM, 0);
 312        err = rtrs_post_rdma_write_imm_empty(usr_con, sess->hb_cqe, imm,
 313                                             0, NULL);
 314        if (err) {
 315                sess->hb_err_handler(usr_con);
 316                return;
 317        }
 318}
 319EXPORT_SYMBOL_GPL(rtrs_send_hb_ack);
 320
 321static void hb_work(struct work_struct *work)
 322{
 323        struct rtrs_con *usr_con;
 324        struct rtrs_sess *sess;
 325        u32 imm;
 326        int err;
 327
 328        sess = container_of(to_delayed_work(work), typeof(*sess), hb_dwork);
 329        usr_con = sess->con[0];
 330
 331        if (sess->hb_missed_cnt > sess->hb_missed_max) {
 332                sess->hb_err_handler(usr_con);
 333                return;
 334        }
 335        if (sess->hb_missed_cnt++) {
 336                /* Reschedule work without sending hb */
 337                schedule_hb(sess);
 338                return;
 339        }
 340        imm = rtrs_to_imm(RTRS_HB_MSG_IMM, 0);
 341        err = rtrs_post_rdma_write_imm_empty(usr_con, sess->hb_cqe, imm,
 342                                             0, NULL);
 343        if (err) {
 344                sess->hb_err_handler(usr_con);
 345                return;
 346        }
 347
 348        schedule_hb(sess);
 349}
 350
 351void rtrs_init_hb(struct rtrs_sess *sess, struct ib_cqe *cqe,
 352                  unsigned int interval_ms, unsigned int missed_max,
 353                  void (*err_handler)(struct rtrs_con *con),
 354                  struct workqueue_struct *wq)
 355{
 356        sess->hb_cqe = cqe;
 357        sess->hb_interval_ms = interval_ms;
 358        sess->hb_err_handler = err_handler;
 359        sess->hb_wq = wq;
 360        sess->hb_missed_max = missed_max;
 361        sess->hb_missed_cnt = 0;
 362        INIT_DELAYED_WORK(&sess->hb_dwork, hb_work);
 363}
 364EXPORT_SYMBOL_GPL(rtrs_init_hb);
 365
 366void rtrs_start_hb(struct rtrs_sess *sess)
 367{
 368        schedule_hb(sess);
 369}
 370EXPORT_SYMBOL_GPL(rtrs_start_hb);
 371
 372void rtrs_stop_hb(struct rtrs_sess *sess)
 373{
 374        cancel_delayed_work_sync(&sess->hb_dwork);
 375        sess->hb_missed_cnt = 0;
 376        sess->hb_missed_max = 0;
 377}
 378EXPORT_SYMBOL_GPL(rtrs_stop_hb);
 379
 380static int rtrs_str_gid_to_sockaddr(const char *addr, size_t len,
 381                                     short port, struct sockaddr_storage *dst)
 382{
 383        struct sockaddr_ib *dst_ib = (struct sockaddr_ib *)dst;
 384        int ret;
 385
 386        /*
 387         * We can use some of the IPv6 functions since GID is a valid
 388         * IPv6 address format
 389         */
 390        ret = in6_pton(addr, len, dst_ib->sib_addr.sib_raw, '\0', NULL);
 391        if (ret == 0)
 392                return -EINVAL;
 393
 394        dst_ib->sib_family = AF_IB;
 395        /*
 396         * Use the same TCP server port number as the IB service ID
 397         * on the IB port space range
 398         */
 399        dst_ib->sib_sid = cpu_to_be64(RDMA_IB_IP_PS_IB | port);
 400        dst_ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
 401        dst_ib->sib_pkey = cpu_to_be16(0xffff);
 402
 403        return 0;
 404}
 405
 406/**
 407 * rtrs_str_to_sockaddr() - Convert rtrs address string to sockaddr
 408 * @addr:       String representation of an addr (IPv4, IPv6 or IB GID):
 409 *              - "ip:192.168.1.1"
 410 *              - "ip:fe80::200:5aee:feaa:20a2"
 411 *              - "gid:fe80::200:5aee:feaa:20a2"
 412 * @len:        String address length
 413 * @port:       Destination port
 414 * @dst:        Destination sockaddr structure
 415 *
 416 * Returns 0 if conversion successful. Non-zero on error.
 417 */
 418static int rtrs_str_to_sockaddr(const char *addr, size_t len,
 419                                u16 port, struct sockaddr_storage *dst)
 420{
 421        if (strncmp(addr, "gid:", 4) == 0) {
 422                return rtrs_str_gid_to_sockaddr(addr + 4, len - 4, port, dst);
 423        } else if (strncmp(addr, "ip:", 3) == 0) {
 424                char port_str[8];
 425                char *cpy;
 426                int err;
 427
 428                snprintf(port_str, sizeof(port_str), "%u", port);
 429                cpy = kstrndup(addr + 3, len - 3, GFP_KERNEL);
 430                err = cpy ? inet_pton_with_scope(&init_net, AF_UNSPEC,
 431                                                 cpy, port_str, dst) : -ENOMEM;
 432                kfree(cpy);
 433
 434                return err;
 435        }
 436        return -EPROTONOSUPPORT;
 437}
 438
 439/**
 440 * sockaddr_to_str() - convert sockaddr to a string.
 441 * @addr:       the sockadddr structure to be converted.
 442 * @buf:        string containing socket addr.
 443 * @len:        string length.
 444 *
 445 * The return value is the number of characters written into buf not
 446 * including the trailing '\0'. If len is == 0 the function returns 0..
 447 */
 448int sockaddr_to_str(const struct sockaddr *addr, char *buf, size_t len)
 449{
 450
 451        switch (addr->sa_family) {
 452        case AF_IB:
 453                return scnprintf(buf, len, "gid:%pI6",
 454                        &((struct sockaddr_ib *)addr)->sib_addr.sib_raw);
 455        case AF_INET:
 456                return scnprintf(buf, len, "ip:%pI4",
 457                        &((struct sockaddr_in *)addr)->sin_addr);
 458        case AF_INET6:
 459                return scnprintf(buf, len, "ip:%pI6c",
 460                          &((struct sockaddr_in6 *)addr)->sin6_addr);
 461        }
 462        return scnprintf(buf, len, "<invalid address family>");
 463}
 464EXPORT_SYMBOL(sockaddr_to_str);
 465
 466/**
 467 * rtrs_addr_to_sockaddr() - convert path string "src,dst" or "src@dst"
 468 * to sockaddreses
 469 * @str:        string containing source and destination addr of a path
 470 *              separated by ',' or '@' I.e. "ip:1.1.1.1,ip:1.1.1.2" or
 471 *              "ip:1.1.1.1@ip:1.1.1.2". If str contains only one address it's
 472 *              considered to be destination.
 473 * @len:        string length
 474 * @port:       Destination port number.
 475 * @addr:       will be set to the source/destination address or to NULL
 476 *              if str doesn't contain any source address.
 477 *
 478 * Returns zero if conversion successful. Non-zero otherwise.
 479 */
 480int rtrs_addr_to_sockaddr(const char *str, size_t len, u16 port,
 481                          struct rtrs_addr *addr)
 482{
 483        const char *d;
 484
 485        d = strchr(str, ',');
 486        if (!d)
 487                d = strchr(str, '@');
 488        if (d) {
 489                if (rtrs_str_to_sockaddr(str, d - str, 0, addr->src))
 490                        return -EINVAL;
 491                d += 1;
 492                len -= d - str;
 493                str  = d;
 494
 495        } else {
 496                addr->src = NULL;
 497        }
 498        return rtrs_str_to_sockaddr(str, len, port, addr->dst);
 499}
 500EXPORT_SYMBOL(rtrs_addr_to_sockaddr);
 501
 502void rtrs_rdma_dev_pd_init(enum ib_pd_flags pd_flags,
 503                            struct rtrs_rdma_dev_pd *pool)
 504{
 505        WARN_ON(pool->ops && (!pool->ops->alloc ^ !pool->ops->free));
 506        INIT_LIST_HEAD(&pool->list);
 507        mutex_init(&pool->mutex);
 508        pool->pd_flags = pd_flags;
 509}
 510EXPORT_SYMBOL(rtrs_rdma_dev_pd_init);
 511
 512void rtrs_rdma_dev_pd_deinit(struct rtrs_rdma_dev_pd *pool)
 513{
 514        mutex_destroy(&pool->mutex);
 515        WARN_ON(!list_empty(&pool->list));
 516}
 517EXPORT_SYMBOL(rtrs_rdma_dev_pd_deinit);
 518
 519static void dev_free(struct kref *ref)
 520{
 521        struct rtrs_rdma_dev_pd *pool;
 522        struct rtrs_ib_dev *dev;
 523
 524        dev = container_of(ref, typeof(*dev), ref);
 525        pool = dev->pool;
 526
 527        mutex_lock(&pool->mutex);
 528        list_del(&dev->entry);
 529        mutex_unlock(&pool->mutex);
 530
 531        if (pool->ops && pool->ops->deinit)
 532                pool->ops->deinit(dev);
 533
 534        ib_dealloc_pd(dev->ib_pd);
 535
 536        if (pool->ops && pool->ops->free)
 537                pool->ops->free(dev);
 538        else
 539                kfree(dev);
 540}
 541
 542int rtrs_ib_dev_put(struct rtrs_ib_dev *dev)
 543{
 544        return kref_put(&dev->ref, dev_free);
 545}
 546EXPORT_SYMBOL(rtrs_ib_dev_put);
 547
 548static int rtrs_ib_dev_get(struct rtrs_ib_dev *dev)
 549{
 550        return kref_get_unless_zero(&dev->ref);
 551}
 552
 553struct rtrs_ib_dev *
 554rtrs_ib_dev_find_or_add(struct ib_device *ib_dev,
 555                         struct rtrs_rdma_dev_pd *pool)
 556{
 557        struct rtrs_ib_dev *dev;
 558
 559        mutex_lock(&pool->mutex);
 560        list_for_each_entry(dev, &pool->list, entry) {
 561                if (dev->ib_dev->node_guid == ib_dev->node_guid &&
 562                    rtrs_ib_dev_get(dev))
 563                        goto out_unlock;
 564        }
 565        mutex_unlock(&pool->mutex);
 566        if (pool->ops && pool->ops->alloc)
 567                dev = pool->ops->alloc();
 568        else
 569                dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 570        if (IS_ERR_OR_NULL(dev))
 571                goto out_err;
 572
 573        kref_init(&dev->ref);
 574        dev->pool = pool;
 575        dev->ib_dev = ib_dev;
 576        dev->ib_pd = ib_alloc_pd(ib_dev, pool->pd_flags);
 577        if (IS_ERR(dev->ib_pd))
 578                goto out_free_dev;
 579
 580        if (pool->ops && pool->ops->init && pool->ops->init(dev))
 581                goto out_free_pd;
 582
 583        mutex_lock(&pool->mutex);
 584        list_add(&dev->entry, &pool->list);
 585out_unlock:
 586        mutex_unlock(&pool->mutex);
 587        return dev;
 588
 589out_free_pd:
 590        ib_dealloc_pd(dev->ib_pd);
 591out_free_dev:
 592        if (pool->ops && pool->ops->free)
 593                pool->ops->free(dev);
 594        else
 595                kfree(dev);
 596out_err:
 597        return NULL;
 598}
 599EXPORT_SYMBOL(rtrs_ib_dev_find_or_add);
 600