Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: add devmem TCP TX documentation

Add documentation outlining the usage and details of the devmem TCP TX
API.

Signed-off-by: Mina Almasry <almasrymina@google.com>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://patch.msgid.link/20250508004830.4100853-6-almasrymina@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Mina Almasry and committed by
Paolo Abeni
17af8cc0 bd618489

+146 -4
+146 -4
Documentation/networking/devmem.rst
··· 62 62 https://lore.kernel.org/netdev/20240831004313.3713467-1-almasrymina@google.com/ 63 63 64 64 65 - Interface 66 - ========= 65 + RX Interface 66 + ============ 67 67 68 68 69 69 Example 70 70 ------- 71 71 72 - tools/testing/selftests/net/ncdevmem.c:do_server shows an example of setting up 73 - the RX path of this API. 72 + ./tools/testing/selftests/drivers/net/hw/ncdevmem:do_server shows an example of 73 + setting up the RX path of this API. 74 74 75 75 76 76 NIC Setup ··· 234 234 235 235 (a) an internal kernel leak bug. 236 236 (b) the user passed more than 1024 frags. 237 + 238 + TX Interface 239 + ============ 240 + 241 + 242 + Example 243 + ------- 244 + 245 + ./tools/testing/selftests/drivers/net/hw/ncdevmem:do_client shows an example of 246 + setting up the TX path of this API. 247 + 248 + 249 + NIC Setup 250 + --------- 251 + 252 + The user must bind a TX dmabuf to a given NIC using the netlink API:: 253 + 254 + struct netdev_bind_tx_req *req = NULL; 255 + struct netdev_bind_tx_rsp *rsp = NULL; 256 + struct ynl_error yerr; 257 + 258 + *ys = ynl_sock_create(&ynl_netdev_family, &yerr); 259 + 260 + req = netdev_bind_tx_req_alloc(); 261 + netdev_bind_tx_req_set_ifindex(req, ifindex); 262 + netdev_bind_tx_req_set_fd(req, dmabuf_fd); 263 + 264 + rsp = netdev_bind_tx(*ys, req); 265 + 266 + tx_dmabuf_id = rsp->id; 267 + 268 + 269 + The netlink API returns a dmabuf_id: a unique ID that refers to this dmabuf 270 + that has been bound. 271 + 272 + The user can unbind the dmabuf from the netdevice by closing the netlink socket 273 + that established the binding. We do this so that the binding is automatically 274 + unbound even if the userspace process crashes. 275 + 276 + Note that any reasonably well-behaved dmabuf from any exporter should work with 277 + devmem TCP, even if the dmabuf is not actually backed by devmem. An example of 278 + this is udmabuf, which wraps user memory (non-devmem) in a dmabuf. 279 + 280 + Socket Setup 281 + ------------ 282 + 283 + The user application must use MSG_ZEROCOPY flag when sending devmem TCP. Devmem 284 + cannot be copied by the kernel, so the semantics of the devmem TX are similar 285 + to the semantics of MSG_ZEROCOPY:: 286 + 287 + setsockopt(socket_fd, SOL_SOCKET, SO_ZEROCOPY, &opt, sizeof(opt)); 288 + 289 + It is also recommended that the user binds the TX socket to the same interface 290 + the dma-buf has been bound to via SO_BINDTODEVICE:: 291 + 292 + setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname) + 1); 293 + 294 + 295 + Sending data 296 + ------------ 297 + 298 + Devmem data is sent using the SCM_DEVMEM_DMABUF cmsg. 299 + 300 + The user should create a msghdr where, 301 + 302 + * iov_base is set to the offset into the dmabuf to start sending from 303 + * iov_len is set to the number of bytes to be sent from the dmabuf 304 + 305 + The user passes the dma-buf id to send from via the dmabuf_tx_cmsg.dmabuf_id. 306 + 307 + The example below sends 1024 bytes from offset 100 into the dmabuf, and 2048 308 + from offset 2000 into the dmabuf. The dmabuf to send from is tx_dmabuf_id:: 309 + 310 + char ctrl_data[CMSG_SPACE(sizeof(struct dmabuf_tx_cmsg))]; 311 + struct dmabuf_tx_cmsg ddmabuf; 312 + struct msghdr msg = {}; 313 + struct cmsghdr *cmsg; 314 + struct iovec iov[2]; 315 + 316 + iov[0].iov_base = (void*)100; 317 + iov[0].iov_len = 1024; 318 + iov[1].iov_base = (void*)2000; 319 + iov[1].iov_len = 2048; 320 + 321 + msg.msg_iov = iov; 322 + msg.msg_iovlen = 2; 323 + 324 + msg.msg_control = ctrl_data; 325 + msg.msg_controllen = sizeof(ctrl_data); 326 + 327 + cmsg = CMSG_FIRSTHDR(&msg); 328 + cmsg->cmsg_level = SOL_SOCKET; 329 + cmsg->cmsg_type = SCM_DEVMEM_DMABUF; 330 + cmsg->cmsg_len = CMSG_LEN(sizeof(struct dmabuf_tx_cmsg)); 331 + 332 + ddmabuf.dmabuf_id = tx_dmabuf_id; 333 + 334 + *((struct dmabuf_tx_cmsg *)CMSG_DATA(cmsg)) = ddmabuf; 335 + 336 + sendmsg(socket_fd, &msg, MSG_ZEROCOPY); 337 + 338 + 339 + Reusing TX dmabufs 340 + ------------------ 341 + 342 + Similar to MSG_ZEROCOPY with regular memory, the user should not modify the 343 + contents of the dma-buf while a send operation is in progress. This is because 344 + the kernel does not keep a copy of the dmabuf contents. Instead, the kernel 345 + will pin and send data from the buffer available to the userspace. 346 + 347 + Just as in MSG_ZEROCOPY, the kernel notifies the userspace of send completions 348 + using MSG_ERRQUEUE:: 349 + 350 + int64_t tstop = gettimeofday_ms() + waittime_ms; 351 + char control[CMSG_SPACE(100)] = {}; 352 + struct sock_extended_err *serr; 353 + struct msghdr msg = {}; 354 + struct cmsghdr *cm; 355 + int retries = 10; 356 + __u32 hi, lo; 357 + 358 + msg.msg_control = control; 359 + msg.msg_controllen = sizeof(control); 360 + 361 + while (gettimeofday_ms() < tstop) { 362 + if (!do_poll(fd)) continue; 363 + 364 + ret = recvmsg(fd, &msg, MSG_ERRQUEUE); 365 + 366 + for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm)) { 367 + serr = (void *)CMSG_DATA(cm); 368 + 369 + hi = serr->ee_data; 370 + lo = serr->ee_info; 371 + 372 + fprintf(stdout, "tx complete [%d,%d]\n", lo, hi); 373 + } 374 + } 375 + 376 + After the associated sendmsg has been completed, the dmabuf can be reused by 377 + the userspace. 378 + 237 379 238 380 Implementation & Caveats 239 381 ========================