Commit 8ebee8b13 for llama.cpp

commit 8ebee8b13e59826b2285b7bafb5c7c6f91a1d38c
Author: Radoslav Gerganov <rgerganov@gmail.com>
Date:   Sat Dec 5 12:37:25 2026 +0200

    rpc : add rdma_cm setup path for IB and iWARP (#28494)

    The RDMA probe only matches IP-mapped GIDs, which exist on RoCE but not on
    native InfiniBand or iWARP, so RPC silently used TCP there. iWARP also cannot
    reach RTS with a manual QP transition. Fall back to rdma_cm when the GID probe
    fails; the data path is unchanged.

    The IB CM sets min_rnr_timer to 0 (655 ms), which stalled IB transfers for
    the full timeout on every RNR NAK. Set it to 1 after the connection is
    established, matching the manual verbs path. iWARP has no RNR timer.

    Assisted-by: Claude Opus 5.5

diff --git a/ggml/src/ggml-rpc/CMakeLists.txt b/ggml/src/ggml-rpc/CMakeLists.txt
index 897665123..2ba92447b 100644
--- a/ggml/src/ggml-rpc/CMakeLists.txt
+++ b/ggml/src/ggml-rpc/CMakeLists.txt
@@ -10,7 +10,7 @@ if (WIN32)
     target_link_libraries(ggml-rpc PRIVATE ws2_32)
 endif()

-# RDMA auto-detection: Linux RoCE/IB via libibverbs, Apple RDMA-over-Thunderbolt via librdma
+# RDMA auto-detection: Linux RoCE/IB/iWARP via libibverbs and librdmacm, Apple RDMA-over-Thunderbolt via librdma
 if (APPLE)
     set(RDMA_LIB_NAME rdma)
     set(RDMA_DESC "Apple RDMA-over-Thunderbolt, UC")
@@ -44,7 +44,9 @@ if (GGML_RPC_RDMA)
         target_compile_definitions(ggml-rpc PRIVATE GGML_RPC_RDMA_APPLE)
         target_sources(ggml-rpc PRIVATE transport-apple.cpp)
     else()
-        target_link_libraries(ggml-rpc PRIVATE ${RDMA_LIB})
+        # librdmacm brings up the QP on IB and iWARP, where the manual RoCE GID setup does not apply
+        find_library(RDMACM_LIB rdmacm REQUIRED)
+        target_link_libraries(ggml-rpc PRIVATE ${RDMA_LIB} ${RDMACM_LIB})
     endif()
     message(STATUS "  RDMA transport enabled (${RDMA_DESC})")
 else()
diff --git a/ggml/src/ggml-rpc/transport.cpp b/ggml/src/ggml-rpc/transport.cpp
index c7d9f5321..1fb17482e 100644
--- a/ggml/src/ggml-rpc/transport.cpp
+++ b/ggml/src/ggml-rpc/transport.cpp
@@ -33,6 +33,9 @@
 #  endif
 #  ifdef GGML_RPC_RDMA_APPLE
 #    include "transport-apple.h"
+#  else
+#    include <rdma/rdma_cma.h>
+#    include <cerrno>
 #  endif
 #endif // GGML_RPC_RDMA

@@ -54,6 +57,10 @@ static constexpr int    RDMA_RX_DEPTH = 24;            // pre-posted recv ring:
 // keep polling the CQ for this long after the last activity, then sleep until the next completion
 static constexpr auto   RDMA_SPIN_TIME = std::chrono::milliseconds(100);

+static constexpr uint32_t RDMA_CM_MAGIC      = 0x4d435752; // tags caps as rdma_cm, not a manual QPN/GID setup
+static constexpr int      RDMA_CM_TIMEOUT_MS = 10000;
+static constexpr uint32_t RDMA_CM_MAX_INLINE = 64;         // irdma rejects 128 and above
+
 struct rdma_conn {
     struct ibv_context * ctx = nullptr;
     struct ibv_pd * pd  = nullptr;
@@ -78,6 +85,11 @@ struct rdma_conn {

     uint32_t        max_inline = 0;

+    // set when the QP comes from rdma_cm; the cm id then owns ctx, pd and qp
+    struct rdma_event_channel * cm_ch        = nullptr;
+    struct rdma_cm_id         * cm_id        = nullptr;
+    struct rdma_cm_id         * cm_listen_id = nullptr;
+
     uint8_t * rx_slot(int i) const {
         return static_cast<uint8_t *>(rx_buf) + static_cast<size_t>(i) * RDMA_CHUNK;
     }
@@ -94,15 +106,66 @@ struct rdma_conn {
         return ibv_post_recv(qp, &wr, &bad) == 0;
     }

+    // separate send and recv CQs on one completion channel; irdma rejects a QP that shares a CQ
+    bool create_cqs() {
+        // without a completion channel rdma_poll() spins all the time, as before
+        ch  = ibv_create_comp_channel(ctx);
+        scq = ibv_create_cq(ctx, 16, nullptr, ch, 0);
+        rcq = ibv_create_cq(ctx, RDMA_RX_DEPTH + 4, nullptr, ch, 0);
+        return scq && rcq;
+    }
+
+    ibv_qp_init_attr qp_init_attr(uint32_t inline_size) const {
+        ibv_qp_init_attr qia = {};
+        qia.send_cq = scq;
+        qia.recv_cq = rcq;
+        qia.qp_type = IBV_QPT_RC;
+        qia.cap.max_send_wr     = 4;
+        qia.cap.max_recv_wr     = RDMA_RX_DEPTH + 4;
+        qia.cap.max_send_sge    = 1;
+        qia.cap.max_recv_sge    = 1;
+        qia.cap.max_inline_data = inline_size;
+        return qia;
+    }
+
+    bool alloc_bufs() {
+        tx_buf = aligned_alloc(4096, RDMA_CHUNK);
+        rx_buf = aligned_alloc(4096, static_cast<size_t>(RDMA_RX_DEPTH) * RDMA_CHUNK);
+        if (!tx_buf || !rx_buf) return false;
+        tx_mr = ibv_reg_mr(pd, tx_buf, RDMA_CHUNK, IBV_ACCESS_LOCAL_WRITE);
+        rx_mr = ibv_reg_mr(pd, rx_buf, static_cast<size_t>(RDMA_RX_DEPTH) * RDMA_CHUNK,
+                           IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
+        return tx_mr && rx_mr;
+    }
+
+    bool post_rx_all() {
+        for (int i = 0; i < RDMA_RX_DEPTH; i++) {
+            if (!post_rx(i)) return false;
+        }
+        return true;
+    }
+
     ~rdma_conn() {
         if (tx_mr) ibv_dereg_mr(tx_mr);
         if (rx_mr) ibv_dereg_mr(rx_mr);
         free(tx_buf);
         free(rx_buf);
-        if (qp)  ibv_destroy_qp(qp);
+        if (cm_id && cm_id->qp) {
+            rdma_disconnect(cm_id);
+            rdma_destroy_qp(cm_id);
+        } else if (qp) {
+            ibv_destroy_qp(qp);
+        }
         if (scq) ibv_destroy_cq(scq);
         if (rcq) ibv_destroy_cq(rcq);
         if (ch)  ibv_destroy_comp_channel(ch);
+        if (cm_ch) {
+            // ctx and pd belong to the cm id
+            if (cm_id)        rdma_destroy_id(cm_id);
+            if (cm_listen_id) rdma_destroy_id(cm_listen_id);
+            rdma_destroy_event_channel(cm_ch);
+            return;
+        }
         if (pd)  ibv_dealloc_pd(pd);
         if (ctx) ibv_close_device(ctx);
     }
@@ -127,10 +190,23 @@ struct rdma_caps {

 static_assert(sizeof(rdma_caps) == RPC_CONN_CAPS_SIZE, "rdma_caps must match conn_caps size");

+// Native IB and iWARP have no IP-mapped GIDs, so the RoCE probe cannot match them
+// (and iWARP cannot reach RTS with a manual transition anyway). For those, the
+// listening peer advertises the port of an rdma_cm listener instead of a
+// QPN/GID, and rdma_cm resolves the path. The connecting side advertises
+// port 0: it dials, it does not listen.
+struct rdma_cm_caps {
+    uint32_t magic;
+    uint16_t port;
+    uint8_t  pad[18];
+};
+
+static_assert(sizeof(rdma_cm_caps) == RPC_CONN_CAPS_SIZE, "rdma_cm_caps must match conn_caps size");
+
 #endif // GGML_RPC_RDMA && !GGML_RPC_RDMA_APPLE

 struct socket_t::impl {
-    impl(sockfd_t fd) : use_rdma(false), fd(fd) {}
+    impl(sockfd_t fd, bool listen_side = false) : use_rdma(false), listen_side(listen_side), fd(fd) {}
     ~impl();
     bool send_data(const void * data, size_t size);
     bool recv_data(void * data, size_t size);
@@ -152,11 +228,20 @@ struct socket_t::impl {
     bool rdma_poll(struct ibv_cq * cq, struct ibv_wc * wc);
     bool rdma_wait_event();

+    bool rdma_cm_probe();
+    bool rdma_cm_activate(uint16_t remote_port);
+    bool rdma_cm_setup_qp(struct rdma_cm_id * id);
+
+    enum rdma_mode { RDMA_MODE_NONE, RDMA_MODE_ROCE, RDMA_MODE_CM };
+
     std::unique_ptr<rdma_conn> rdma;
     rdma_local_info            rdma_local = {};
+    rdma_mode                  mode         = RDMA_MODE_NONE;
+    uint16_t                   rdma_cm_port = 0;
 #  endif
 #endif // GGML_RPC_RDMA
     bool     use_rdma;
+    bool     listen_side;
     sockfd_t fd;
 };

@@ -300,34 +385,14 @@ bool socket_t::impl::rdma_probe() {
     rdma->pd = ibv_alloc_pd(ibctx);
     if (!rdma->pd) return false;

-    // without a completion channel rdma_poll() spins all the time, as before
-    rdma->ch  = ibv_create_comp_channel(ibctx);
-    rdma->scq = ibv_create_cq(ibctx, 16, nullptr, rdma->ch, 0);
-    rdma->rcq = ibv_create_cq(ibctx, RDMA_RX_DEPTH + 4, nullptr, rdma->ch, 0);
-    if (!rdma->scq || !rdma->rcq) return false;
-
-    ibv_qp_init_attr qia = {};
-    qia.send_cq = rdma->scq;
-    qia.recv_cq = rdma->rcq;
-    qia.qp_type = IBV_QPT_RC;
-    qia.cap.max_send_wr     = 4;
-    qia.cap.max_recv_wr     = RDMA_RX_DEPTH + 4;
-    qia.cap.max_send_sge    = 1;
-    qia.cap.max_recv_sge    = 1;
-    qia.cap.max_inline_data = 256;
+    if (!rdma->create_cqs()) return false;

+    ibv_qp_init_attr qia = rdma->qp_init_attr(256);
     rdma->qp = ibv_create_qp(rdma->pd, &qia);
     if (!rdma->qp) return false;
     rdma->max_inline = qia.cap.max_inline_data;

-    rdma->tx_buf = aligned_alloc(4096, RDMA_CHUNK);
-    rdma->rx_buf = aligned_alloc(4096, static_cast<size_t>(RDMA_RX_DEPTH) * RDMA_CHUNK);
-    if (!rdma->tx_buf || !rdma->rx_buf) return false;
-
-    rdma->tx_mr = ibv_reg_mr(rdma->pd, rdma->tx_buf, RDMA_CHUNK, IBV_ACCESS_LOCAL_WRITE);
-    rdma->rx_mr = ibv_reg_mr(rdma->pd, rdma->rx_buf, static_cast<size_t>(RDMA_RX_DEPTH) * RDMA_CHUNK,
-                           IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
-    if (!rdma->tx_mr || !rdma->rx_mr) return false;
+    if (!rdma->alloc_bufs()) return false;

     ibv_gid local_gid;
     if (ibv_query_gid(ibctx, ib_port, gid_idx, &local_gid) != 0) return false;
@@ -363,9 +428,7 @@ bool socket_t::impl::rdma_activate(uint32_t remote_qpn, uint32_t remote_psn, con
         }
     }

-    for (int i = 0; i < RDMA_RX_DEPTH; i++) {
-        if (!rdma->post_rx(i)) return false;
-    }
+    if (!rdma->post_rx_all()) return false;

     // INIT -> RTR
     {
@@ -444,7 +507,10 @@ bool socket_t::impl::rdma_poll(struct ibv_cq * cq, struct ibv_wc * wc) {
         int n = ibv_poll_cq(cq, 1, wc);
         if (n > 0) {
             c->last_active = std::chrono::steady_clock::now();
-            if (wc->status != IBV_WC_SUCCESS) {
+            if (wc->status == IBV_WC_WR_FLUSH_ERR) {
+                // the peer went away and the QP flushed the pre-posted recv ring
+                LOG_DBG("[%s] work request flushed\n", __func__);
+            } else if (wc->status != IBV_WC_SUCCESS) {
                 LOG_ERROR("RDMA CQ wc error: status=%d (%s) vendor_err=0x%x\n",
                     wc->status, ibv_wc_status_str(wc->status), wc->vendor_err);
             }
@@ -542,6 +608,195 @@ bool socket_t::impl::rdma_recv(void * data, size_t size) {
     return true;
 }

+// Copy of the socket's local or peer address with the port replaced. rdma_cm
+// resolves the device from the address, so this keeps RDMA on the same
+// interface the TCP connection already uses.
+static bool rdma_cm_addr(sockfd_t fd, bool peer, uint16_t port, sockaddr_storage & out) {
+    socklen_t len = sizeof(out);
+    memset(&out, 0, sizeof(out));
+    int r = peer ? getpeername(fd, reinterpret_cast<sockaddr *>(&out), &len)
+                 : getsockname(fd, reinterpret_cast<sockaddr *>(&out), &len);
+    if (r != 0) {
+        return false;
+    }
+    if (out.ss_family == AF_INET) {
+        reinterpret_cast<sockaddr_in *>(&out)->sin_port = htons(port);
+        return true;
+    }
+    if (out.ss_family == AF_INET6) {
+        reinterpret_cast<sockaddr_in6 *>(&out)->sin6_port = htons(port);
+        return true;
+    }
+    return false;
+}
+
+static bool rdma_cm_wait(struct rdma_event_channel * ch, enum rdma_cm_event_type want,
+                         struct rdma_cm_id ** out_id) {
+    struct pollfd pfd = { ch->fd, POLLIN, 0 };
+    if (poll(&pfd, 1, RDMA_CM_TIMEOUT_MS) <= 0) {
+        LOG_ERROR("RDMA CM: timed out waiting for %s\n", rdma_event_str(want));
+        return false;
+    }
+    struct rdma_cm_event * ev = nullptr;
+    if (rdma_get_cm_event(ch, &ev) != 0) {
+        return false;
+    }
+    bool ok = ev->event == want;
+    if (!ok) {
+        LOG_ERROR("RDMA CM: got %s, expected %s\n", rdma_event_str(ev->event), rdma_event_str(want));
+    } else if (out_id) {
+        *out_id = ev->id;
+    }
+    rdma_ack_cm_event(ev);
+    return ok;
+}
+
+// The listening side binds an rdma_cm listener to an ephemeral port and
+// advertises it. The connecting side only checks that its local address has an
+// RDMA device, so it never makes the peer wait for a connection it cannot make.
+bool socket_t::impl::rdma_cm_probe() {
+    rdma.reset();
+
+    sockaddr_storage local = {};
+    if (!rdma_cm_addr(fd, false, 0, local)) {
+        return false;
+    }
+
+    if (!listen_side) {
+        struct rdma_event_channel * ch = rdma_create_event_channel();
+        if (!ch) {
+            return false;
+        }
+        struct rdma_cm_id * id = nullptr;
+        bool ok = rdma_create_id(ch, &id, nullptr, RDMA_PS_TCP) == 0 &&
+                  rdma_bind_addr(id, reinterpret_cast<sockaddr *>(&local)) == 0;
+        if (id) {
+            rdma_destroy_id(id);
+        }
+        rdma_destroy_event_channel(ch);
+        if (!ok) {
+            return false;
+        }
+        rdma_cm_port = 0;
+        LOG_DBG("[%s] rdma_cm device available for this address\n", __func__);
+        return true;
+    }
+
+    rdma = std::make_unique<rdma_conn>();
+    rdma->cm_ch = rdma_create_event_channel();
+    if (!rdma->cm_ch) {
+        return false;
+    }
+    if (rdma_create_id(rdma->cm_ch, &rdma->cm_listen_id, nullptr, RDMA_PS_TCP) != 0) {
+        return false;
+    }
+    if (rdma_bind_addr(rdma->cm_listen_id, reinterpret_cast<sockaddr *>(&local)) != 0) {
+        return false;
+    }
+    if (rdma_listen(rdma->cm_listen_id, 1) != 0) {
+        return false;
+    }
+    rdma_cm_port = ntohs(rdma_get_src_port(rdma->cm_listen_id));
+    LOG_INFO("RDMA CM probed: listening on port %u\n", rdma_cm_port);
+    return true;
+}
+
+bool socket_t::impl::rdma_cm_setup_qp(struct rdma_cm_id * id) {
+    rdma->cm_id = id;
+    rdma->ctx   = id->verbs;
+    rdma->pd    = id->pd;
+
+    if (!rdma->create_cqs()) {
+        return false;
+    }
+
+    ibv_qp_init_attr qia = rdma->qp_init_attr(RDMA_CM_MAX_INLINE);
+    if (rdma_create_qp(id, id->pd, &qia) != 0) {
+        LOG_ERROR("RDMA CM: rdma_create_qp failed: %s\n", strerror(errno));
+        return false;
+    }
+    rdma->qp = id->qp;
+    rdma->max_inline = qia.cap.max_inline_data;
+
+    return rdma->alloc_bufs() && rdma->post_rx_all();
+}
+
+bool socket_t::impl::rdma_cm_activate(uint16_t remote_port) {
+    if (listen_side) {
+        if (!rdma || !rdma->cm_listen_id) {
+            return false;
+        }
+        struct rdma_cm_id * id = nullptr;
+        if (!rdma_cm_wait(rdma->cm_ch, RDMA_CM_EVENT_CONNECT_REQUEST, &id)) {
+            return false;
+        }
+        if (!rdma_cm_setup_qp(id)) {
+            return false;
+        }
+        if (rdma_accept(id, nullptr) != 0) {
+            LOG_ERROR("RDMA CM: rdma_accept failed: %s\n", strerror(errno));
+            return false;
+        }
+    } else {
+        if (remote_port == 0) {
+            return false;
+        }
+        sockaddr_storage src = {};
+        sockaddr_storage dst = {};
+        if (!rdma_cm_addr(fd, false, 0, src) || !rdma_cm_addr(fd, true, remote_port, dst)) {
+            return false;
+        }
+
+        rdma = std::make_unique<rdma_conn>();
+        rdma->cm_ch = rdma_create_event_channel();
+        if (!rdma->cm_ch) {
+            return false;
+        }
+        if (rdma_create_id(rdma->cm_ch, &rdma->cm_id, nullptr, RDMA_PS_TCP) != 0) {
+            return false;
+        }
+        struct rdma_cm_id * id = rdma->cm_id;
+        if (rdma_resolve_addr(id, reinterpret_cast<sockaddr *>(&src),
+                              reinterpret_cast<sockaddr *>(&dst), RDMA_CM_TIMEOUT_MS) != 0) {
+            return false;
+        }
+        if (!rdma_cm_wait(rdma->cm_ch, RDMA_CM_EVENT_ADDR_RESOLVED, nullptr)) {
+            return false;
+        }
+        if (rdma_resolve_route(id, RDMA_CM_TIMEOUT_MS) != 0) {
+            return false;
+        }
+        if (!rdma_cm_wait(rdma->cm_ch, RDMA_CM_EVENT_ROUTE_RESOLVED, nullptr)) {
+            return false;
+        }
+        if (!rdma_cm_setup_qp(id)) {
+            return false;
+        }
+        if (rdma_connect(id, nullptr) != 0) {
+            LOG_ERROR("RDMA CM: rdma_connect failed: %s\n", strerror(errno));
+            return false;
+        }
+    }
+    if (!rdma_cm_wait(rdma->cm_ch, RDMA_CM_EVENT_ESTABLISHED, nullptr)) {
+        return false;
+    }
+    // The IB CM sets min_rnr_timer to 0, which means a 655 ms wait after every RNR NAK.
+    // Use the same 0.01 ms as the manual verbs path. iWARP has no RNR timer, so skip it there.
+    if (rdma->ctx->device->transport_type == IBV_TRANSPORT_IB) {
+        struct ibv_qp_attr a = {};
+        a.qp_state      = IBV_QPS_RTS;
+        a.min_rnr_timer = 1;
+        if (ibv_modify_qp(rdma->qp, &a, IBV_QP_STATE | IBV_QP_MIN_RNR_TIMER) != 0) {
+            LOG_WARN("RDMA CM: failed to set min_rnr_timer: %s\n", strerror(errno));
+        }
+    }
+    rdma->last_active = std::chrono::steady_clock::now();
+
+    LOG_INFO("RDMA CM activated: qpn=%u inline=%u rx_depth=%d\n",
+             rdma->qp->qp_num, rdma->max_inline, RDMA_RX_DEPTH);
+    return true;
+}
+
 #endif // !GGML_RPC_RDMA_APPLE (Linux RC transport)

 #endif // GGML_RPC_RDMA
@@ -616,12 +871,20 @@ void socket_t::impl::get_caps(uint8_t * local_caps) {
     }
 #  else
     rdma_local = {};
+    mode = RDMA_MODE_NONE;
     if (rdma_probe()) {
+        mode = RDMA_MODE_ROCE;
         rdma_caps rc = {};
         rc.qpn = rdma_local.qpn;
         rc.psn = rdma_local.psn;
         memcpy(rc.gid, rdma_local.gid, RDMA_GID_SIZE);
         memcpy(local_caps, &rc, sizeof(rc));
+    } else if (rdma_cm_probe()) {
+        mode = RDMA_MODE_CM;
+        rdma_cm_caps cc = {};
+        cc.magic = RDMA_CM_MAGIC;
+        cc.port  = rdma_cm_port;
+        memcpy(local_caps, &cc, sizeof(cc));
     } else {
         rdma.reset();
         LOG_DBG2("[%s] RDMA probe failed, continuing with TCP\n", __func__);
@@ -638,8 +901,13 @@ void socket_t::impl::update_caps(const uint8_t * remote_caps) {
     for (size_t i = 0; i < RPC_CONN_CAPS_SIZE; i++) {
         remote_rdma |= remote_caps[i] != 0;
     }
-    if (!rdma || !remote_rdma) {
-        if (rdma && !remote_rdma) {
+#  ifdef GGML_RPC_RDMA_APPLE
+    bool local_rdma = rdma != nullptr;
+#  else
+    bool local_rdma = mode != RDMA_MODE_NONE;
+#  endif
+    if (!local_rdma || !remote_rdma) {
+        if (local_rdma && !remote_rdma) {
             LOG_WARN("[%s] peer does not support RDMA, continuing with TCP\n", __func__);
         }
         rdma.reset();
@@ -648,9 +916,22 @@ void socket_t::impl::update_caps(const uint8_t * remote_caps) {
 #  ifdef GGML_RPC_RDMA_APPLE
     bool activated = rdma->activate(remote_caps);
 #  else
-    rdma_caps rc = {};
-    memcpy(&rc, remote_caps, sizeof(rc));
-    bool activated = rdma_activate(rc.qpn, rc.psn, rc.gid);
+    rdma_cm_caps cc = {};
+    memcpy(&cc, remote_caps, sizeof(cc));
+    bool remote_cm = cc.magic == RDMA_CM_MAGIC;
+    if (remote_cm != (mode == RDMA_MODE_CM)) {
+        LOG_INFO("RDMA: peer uses a different setup path, staying on TCP\n");
+        rdma.reset();
+        return;
+    }
+    bool activated;
+    if (remote_cm) {
+        activated = rdma_cm_activate(cc.port);
+    } else {
+        rdma_caps rc = {};
+        memcpy(&rc, remote_caps, sizeof(rc));
+        activated = rdma_activate(rc.qpn, rc.psn, rc.gid);
+    }
 #  endif
     if (activated) {
         use_rdma = true;
@@ -733,7 +1014,7 @@ socket_ptr socket_t::accept() {
         return nullptr;
     }
     LOG_DBG("[%s] accepted client connection\n", __func__);
-    return socket_ptr(new socket_t(std::make_unique<impl>(client_socket_fd)));
+    return socket_ptr(new socket_t(std::make_unique<impl>(client_socket_fd, true)));
 }

 socket_ptr socket_t::create_server(const char * host, int port) {