XLIO
The NVIDIA® Accelerated IO
xlio_extra.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
3  * Copyright (c) 2021-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4  * SPDX-License-Identifier: GPL-2.0-only or BSD-2-Clause
5  */
6 
16 #ifndef XLIO_EXTRA_H
17 #define XLIO_EXTRA_H
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <sys/socket.h>
22 
23 #include "xlio_types.h"
24 
25 /* Magic value for xlio_get_api (NVDAXLIO) */
26 #define XLIO_MAGIC_NUMBER (0x4f494c584144564eULL)
27 
28 /* Forward declaration. */
29 struct ibv_pd;
30 
31 /*
32  * XLIO Extended Socket API
33  */
34 
35 enum {
36  XLIO_EXTRA_API_REGISTER_RECV_CALLBACK = (1 << 0),
37  XLIO_EXTRA_API_ADD_CONF_RULE = (1 << 3),
38  XLIO_EXTRA_API_THREAD_OFFLOAD = (1 << 4),
39  XLIO_EXTRA_API_DUMP_FD_STATS = (1 << 11),
40  XLIO_EXTRA_API_XLIO_ULTRA = (1 << 13),
41 };
42 
43 struct __attribute__((packed)) xlio_api_t {
44 
45  /*
46  * Used to verify that API structure returned from xlio_get_api call is
47  * compatible with current XLIO library version.
48  */
49  uint64_t magic;
50 
51  /*
52  * Used to identify which methods were initialized by XLIO as part of xlio_get_api().
53  * The value content is based on cap_mask bit field.
54  * Order of fields in this structure should not be changed to keep abi compatibility.
55  */
56  uint64_t cap_mask;
57 
58  /*
59  * Add a libxlio.conf rule to the top of the list.
60  * This rule will not apply to existing sockets which already considered the conf rules.
61  * (around connect/listen/send/recv ..)
62  * @param config_line A char buffer with the exact format as defined in libxlio.conf, and should
63  * end with '\0'.
64  * @return 0 on success, or error code on failure.
65  */
66  int (*add_conf_rule)(const char *config_line);
67 
68  /*
69  * Create sockets on pthread tid as offloaded/not-offloaded.
70  * This does not affect existing sockets.
71  * Offloaded sockets are still subject to libxlio.conf rules.
72  * @param offload 1 for offloaded, 0 for not-offloaded.
73  * @return 0 on success, or error code on failure.
74  */
75  int (*thread_offload)(int offload, pthread_t tid);
76 
77  /*
78  * Dump fd statistics using the library logger.
79  * @param fd to dump, 0 for all open fds.
80  * @param log_level dumping level corresponding vlog_levels_t enum (vlogger.h).
81  * @return 0 on success, or error code on failure.
82  */
83  int (*dump_fd_stats)(int fd, int log_level);
84 
85  /*
86  * Register a received packet notification callback.
87  *
88  * @param s Socket file descriptor.
89  * @param callback Callback function.
90  * @param context user context for callback function.
91  * @return 0 - success, -1 - error
92  *
93  * errno is set to: EINVAL - not offloaded socket
94  */
95  int (*register_recv_callback)(int s, xlio_recv_callback_t callback, void *context);
96 
97  /*
98  * XLIO Ultra API.
99  */
100  int (*xlio_init_ex)(const struct xlio_init_attr *attr);
101  int (*xlio_exit)(void);
102  int (*xlio_poll_group_create)(const struct xlio_poll_group_attr *attr,
103  xlio_poll_group_t *group_out);
106  int (*xlio_socket_create)(const struct xlio_socket_attr *attr, xlio_socket_t *sock_out);
107  int (*xlio_socket_destroy)(xlio_socket_t sock);
108  int (*xlio_socket_update)(xlio_socket_t sock, unsigned flags, uintptr_t userdata_sq);
109  int (*xlio_socket_setsockopt)(xlio_socket_t sock, int level, int optname, const void *optval,
110  socklen_t optlen);
111  int (*xlio_socket_getsockname)(xlio_socket_t sock, struct sockaddr *addr, socklen_t *addrlen);
112  int (*xlio_socket_getpeername)(xlio_socket_t sock, struct sockaddr *addr, socklen_t *addrlen);
113  int (*xlio_socket_bind)(xlio_socket_t sock, const struct sockaddr *addr, socklen_t addrlen);
114  int (*xlio_socket_connect)(xlio_socket_t sock, const struct sockaddr *to, socklen_t tolen);
115  int (*xlio_socket_listen)(xlio_socket_t sock);
116  struct ibv_pd *(*xlio_socket_get_pd)(xlio_socket_t sock);
119  int (*xlio_socket_send)(xlio_socket_t sock, const void *data, size_t len,
120  const struct xlio_socket_send_attr *attr);
121  int (*xlio_socket_sendv)(xlio_socket_t sock, const struct iovec *iov, unsigned iovcnt,
122  const struct xlio_socket_send_attr *attr);
124  void (*xlio_socket_flush)(xlio_socket_t sock);
125  void (*xlio_socket_buf_free)(xlio_socket_t sock, struct xlio_buf *buf);
126  void (*xlio_poll_group_buf_free)(xlio_poll_group_t group, struct xlio_buf *buf);
127 };
128 
129 /*
130  * Retrieve XLIO extended API.
131  * This function can be called as an alternative to getsockopt() call
132  * when library is preloaded using LD_PRELOAD
133  * getsockopt() call should be used in case application loads library
134  * using dlopen()/dlsym().
135  *
136  * @return Pointer to the XLIO Extended Socket API, or NULL if XLIO not found.
137  */
138 static inline struct xlio_api_t *xlio_get_api()
139 {
140  struct xlio_api_t *api_ptr = NULL;
141  socklen_t len = sizeof(api_ptr);
142 
143  /* coverity[negative_returns] */
144  int err = getsockopt(-2, SOL_SOCKET, SO_XLIO_GET_API, &api_ptr, &len);
145  if (err < 0) {
146  return NULL;
147  }
148  if (len < sizeof(struct xlio_api_t *) || api_ptr == NULL ||
149  api_ptr->magic != XLIO_MAGIC_NUMBER) {
150  return NULL;
151  }
152  return api_ptr;
153 }
154 
155 #endif /* XLIO_EXTRA_H */
int xlio_exit(void)
Finalize XLIO.
int xlio_init_ex(const struct xlio_init_attr *attr)
Initialize the XLIO Ultra API.
int xlio_poll_group_destroy(xlio_poll_group_t group)
Destroy a polling group.
int xlio_poll_group_create(const struct xlio_poll_group_attr *attr, xlio_poll_group_t *group_out)
Create a new polling group.
uintptr_t xlio_poll_group_t
Polling group handle.
Definition: xlio_types.h:164
void xlio_poll_group_poll(xlio_poll_group_t group)
Poll for events on a polling group.
void xlio_socket_buf_free(xlio_socket_t sock, struct xlio_buf *buf)
Free a receive buffer (socket-specific)
void xlio_poll_group_buf_free(xlio_poll_group_t group, struct xlio_buf *buf)
Free a receive buffer (group-specific)
int xlio_socket_attach_group(xlio_socket_t sock, xlio_poll_group_t group)
Attach socket to polling group.
int xlio_socket_create(const struct xlio_socket_attr *attr, xlio_socket_t *sock_out)
Create a new XLIO socket.
int xlio_socket_update(xlio_socket_t sock, unsigned flags, uintptr_t userdata_sq)
Update socket attributes.
uintptr_t xlio_socket_t
Socket handle.
Definition: xlio_types.h:172
int xlio_socket_listen(xlio_socket_t sock)
Listen for incoming connections.
int xlio_socket_getsockname(xlio_socket_t sock, struct sockaddr *addr, socklen_t *addrlen)
Get socket name.
int xlio_socket_destroy(xlio_socket_t sock)
Destroy an XLIO socket.
int xlio_socket_getpeername(xlio_socket_t sock, struct sockaddr *addr, socklen_t *addrlen)
Get peer name.
int xlio_socket_detach_group(xlio_socket_t sock)
Detach socket from polling group.
int xlio_socket_setsockopt(xlio_socket_t sock, int level, int optname, const void *optval, socklen_t optlen)
Set socket options.
int xlio_socket_bind(xlio_socket_t sock, const struct sockaddr *addr, socklen_t addrlen)
Bind socket to address.
int xlio_socket_connect(xlio_socket_t sock, const struct sockaddr *to, socklen_t tolen)
Connect socket to remote address.
void xlio_poll_group_flush(xlio_poll_group_t group)
Flush all dirty sockets in a polling group.
int xlio_socket_sendv(xlio_socket_t sock, const struct iovec *iov, unsigned iovcnt, const struct xlio_socket_send_attr *attr)
Send vectored data on a socket.
int xlio_socket_send(xlio_socket_t sock, const void *data, size_t len, const struct xlio_socket_send_attr *attr)
Send data on a socket.
void xlio_socket_flush(xlio_socket_t sock)
Flush pending data on a socket.
Buffer descriptor.
Definition: xlio_types.h:198
XLIO initialization attributes.
Definition: xlio_types.h:371
Polling group attributes.
Definition: xlio_types.h:414
Socket creation attributes.
Definition: xlio_types.h:450
Send operation attributes.
Definition: xlio_types.h:489
XLIO API type definitions and structures.