TCP/IP Offload
TCP/IP offload APIs to seamlessly integrate and leverage TCP Offload Engine (TOE)
with the existing network infrastructure. In general, we recommend using LD_PRELOAD
which hooks standard POSIX socket APIs to support non-modified applications (e.g., iPerf, Nginx).
However, if you want to develop more optimizd TCP applications, these API will be the best option.
There are three types of handles for TCP APIs:
IPCIPC server to orchestrate requests and responses.TCPPer-device TCP connection management.SocketPer-session socket buffer management.
Example
Here is an example of using TCP offload APIs.
#include <libmango.h>
int main() {
// open an IPC server
mango_ipc_h ipc;
mango_ipc_open ("localhost:32847", false, &ipc);
// create TCP connected with IPC server
mango_tcp_h tcp;
mango_tcp_create (ipc, &tcp);
mango_tcp_set_ipc (tcp, ipc);
// create TCP socket;
mango_socket_h sock;
mango_socket_create (tcp, &sock);
mango_socket_open (sock, NULL);
// connect to the remote server
struct sockaddr_in addr = { ... };
mango_socket_connect (sock, (struct sockaddr *) &addr, sizeof (addr));
// send data to the remote server
char buf[128];
while (1) {
mango_socket_send (sock, buf, 128, NULL);
...
}
return 0;
}
Datatypes
mango_ipc_h
typedef void * mango_ipc_h;
A handle of mango ipc.
mango_ipc_msg_e
typedef enum _mango_ipc_msg mango_ipc_msg_e;
Define available IPC message type.
Values
MANGO_IPC_MSG_HEALTH_CHECK | Health check message |
MANGO_IPC_MSG_SET_CONFIG | Set configuration message |
MANGO_IPC_MSG_GET_CONFIG | Get configuration message |
MANGO_IPC_MSG_TCP_LISTEN | TCP listen message |
MANGO_IPC_MSG_TCP_ACCEPT | TCP accept message |
MANGO_IPC_MSG_TCP_CONNECT | TCP connect message |
MANGO_IPC_MSG_TCP_CLOSE | TCP close message |
MANGO_IPC_MSG_TCP_LISTEN_CLOSE | TCP listen close message |
MANGO_IPC_MSG_TCP_ABORT | TCP abort message |
mango_ipc_prot_e
typedef enum _mango_ipc_prot mango_ipc_prot_e;
Define available IPC protocol type.
Values
MANGO_IPC_PROT_GRPC | gRPC IPC protocol |
MANGO_IPC_PROT_ICEORYX | Iceoryx IPC protocol |
mango_socket_h
typedef void * mango_socket_h;
A handle of mango socket.
mango_tcp_h
typedef void * mango_tcp_h;
A handle of mango tcp.
mango_tcp_reset_fn
typedef void(mango_tcp_h, mango_context_h, void *) mango_tcp_reset_fn;
Custom TCP reset function.
Functions
mango_ipc_close
mango_status_e mango_ipc_close(mango_ipc_h ipc)
Close The IPC connection.
Parameters
- in
ipcThe IPC created handle.
Returns
0 on success, Otherwise, a negative error value.
mango_ipc_get_config
mango_status_e mango_ipc_get_config(mango_ipc_h ipc,
const char *field,
char **value)
Get a config value from the ipc server.
Parameters
- in
ipcThe context handle. - in
fieldThe config field. - out
valueThe config value newly allocated.
Returns
0 on success, Otherwise, a negative error value.
The caller should free the option value using free().
mango_ipc_get_server_path
mango_status_e mango_ipc_get_server_path(mango_ipc_h ipc, const char **path)
Get the IPC server path.
Parameters
- in
ipcThe ipc handle. - out
pathThe ipc server path.
Returns
0 on success, Otherwise, a negative error value.
The path is statically allocated. No need to free.
mango_ipc_open
mango_status_e mango_ipc_open(const char *path, bool owner, mango_ipc_h *ipc)
Open a IPC channel.
Parameters
- in
pathThe socket path. - in
ownerThe socket owner. - out
ipcThe IPC handle.
Returns
0 on success, Otherwise, a negative error value.
mango_ipc_set_config
mango_status_e mango_ipc_set_config(mango_ipc_h ipc,
const char *field,
const char *value)
Set a config value to the ipc server.
Parameters
- in
ipcThe context handle. - in
fieldThe config field. - in
valueThe config value.
Returns
0 on success, Otherwise, a negative error value.
mango_ipc_set_context
mango_status_e mango_ipc_set_context(mango_ipc_h ipc, mango_context_h ctx)
Set a context handle to enable context message handling.
Parameters
- in
ipcThe ipc handle. - in
ctxThe context handle.
Returns
0 on success, Otherwise, a negative error value.
mango_ipc_set_index
mango_status_e mango_ipc_set_index(mango_ipc_h ipc, int idx)
Set DPU index to the IPC.
Parameters
- in
ipcThe ipc handle. - in
idxThe DPU index.
Returns
0 on success, Otherwise, a negative error value.
mango_ipc_set_tcp
mango_status_e mango_ipc_set_tcp(mango_ipc_h ipc, mango_tcp_h tcp)
Set a TCP handle to enable TCP message handling.
Parameters
- in
ipcThe ipc handle. - in
tcpThe tcp handle.
Returns
0 on success, Otherwise, a negative error value.
mango_ipc_shutdown
mango_status_e mango_ipc_shutdown(mango_ipc_h ipc)
Shutdown the IPC server.
Parameters
- in
ipcThe IPC created handle.
Returns
0 on success, Otherwise, a negative error value.
mango_ipc_wait
mango_status_e mango_ipc_wait(mango_ipc_h ipc)
Wait until the IPC server is shutdown.
Parameters
- in
ipcThe created IPC handle.
Returns
0 on success, Otherwise, a negative error value.
It's not supported for non-server IPC handles.
mango_socket_abort
mango_status_e mango_socket_abort(mango_socket_h socket)
Abort the TCP socket.
Parameters
- in
socketThe socket handle.
Returns
0 on success, Otherwise, a negative error value.
It does not close the socket.
mango_socket_accept
mango_status_e mango_socket_accept(mango_socket_h socket,
struct sockaddr *addr,
socklen_t *addrlen,
mango_socket_h *client)
Accept a TCP socket connection.
Parameters
- in
socketThe tcp socket handle. - out
addr(Optional) The sockaddr structure pointer. - inout
addrlen(Optional) The sockaddr structure size. - out
clientThe client socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_bind
mango_status_e mango_socket_bind(mango_socket_h socket,
const struct sockaddr *addr,
socklen_t addrlen)
Bind address info. to the TCP socket.
Parameters
- in
socketThe tcp socket handle. - in
addrThe sockaddr structure pointer. - in
addrlenThe sockaddr structure size.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_close
mango_status_e mango_socket_close(mango_socket_h socket)
Close the TCP socket.
Parameters
- in
socketThe socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_connect
mango_status_e mango_socket_connect(mango_socket_h socket,
const struct sockaddr *addr,
socklen_t addrlen)
Initiate a TCP socket connection.
Parameters
- in
socketThe tcp socket handle. - in
addrThe sockaddr structure pointer. - in
addrlenThe sockaddr structure size.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_create
mango_status_e mango_socket_create(mango_tcp_h tcp, mango_socket_h *socket)
Create a TCP socket.
Parameters
- in
tcpThe tcp handle. - out
socketThe output socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_destroy
mango_status_e mango_socket_destroy(mango_socket_h socket)
Destroy the TCP socket.
Parameters
- in
socketThe created socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_detach
mango_status_e mango_socket_detach(mango_socket_h socket)
Detach the TCP socket and keep its file desriptor.
Parameters
- in
socketThe socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_find
mango_status_e mango_socket_find(int fd, mango_socket_h *socket)
Find the socket handle using a file descriptor.
Parameters
- in
fdThe socket file descriptor. - out
socket(Optional) The output socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_get_fd
mango_status_e mango_socket_get_fd(mango_socket_h socket, int *fd)
Get the file descriptor of the TCP socket.
Parameters
- in
socketThe socket handle. - out
fdThe file descriptor referring to the socket.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_get_name
mango_status_e mango_socket_get_name(mango_socket_h socket,
struct sockaddr *addr,
socklen_t *addrlen)
Get the address name of the TCP socket.
Parameters
- in
socketThe socket handle. - out
addrThe sockaddr structure pointer. - out
addrlenThe sockaddr structure size.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_get_num_avail
mango_status_e mango_socket_get_num_avail(mango_socket_h socket, int *nbytes)
Get a number of recv data bytes immediately availble.
Parameters
- in
socketThe socket handle. - out
nbytesThe number of recv bytes.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_get_option
mango_status_e mango_socket_get_option(mango_socket_h socket,
int level,
int optname,
void *optval,
socklen_t *optlen)
Get TCP socket option.
Parameters
- in
socketThe socket handle. - in
levelThe protocol level. - in
optnameThe option name. - out
optvalThe option value. - out
optlenThe option length.
Returns
0 on success, Otehrwise, a negative error value.
mango_socket_get_peername
mango_status_e mango_socket_get_peername(mango_socket_h socket,
struct sockaddr *addr,
socklen_t *addrlen)
Get the peer address name of the TCP socket.
Parameters
- in
socketThe socket handle. - out
addrThe sockaddr structure pointer. - out
addrlenThe sockaddr structure size.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_get_sequence
mango_status_e mango_socket_get_sequence(mango_socket_h socket,
uint32_t *tx,
uint32_t *rx)
Get the sequence numbers of the TCP socket.
Parameters
- in
socketThe socket handle. - out
txThe TX sequence number of the socket. - out
rxThe RX sequence number of the socket.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_get_session
mango_status_e mango_socket_get_session(mango_socket_h socket, int *session)
Get the session ID of the TCP socket.
Parameters
- in
socketThe socket handle. - out
sessionThe session ID referring to the socket.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_listen
mango_status_e mango_socket_listen(mango_socket_h socket, int backlog)
Mark the socket as a passive one to be used to accept incoming connections.
Parameters
- in
socketThe tcp socket handle. - in
backlogThe maximum number of pending connections.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_open
mango_status_e mango_socket_open(mango_socket_h socket, int *fd)
Open the TCP socket.
Parameters
- in
socketThe socket handle. - out
fdThe file descriptor referring to the socket.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_recv
mango_status_e mango_socket_recv(mango_socket_h socket,
void *buf,
size_t len,
size_t *received)
Receive the TCP packet on the socket.
Parameters
- in
socketThe socket handle - in
bufThe buffer to receive - in
lenThe buffer size - out
receivedThe number of bytes received
Returns
0 on success, Otherwise, a negative error value.
mango_socket_recv_iov
mango_status_e mango_socket_recv_iov(mango_socket_h socket,
const struct iovec *iov,
int iovcnt,
size_t *received)
Receive the TCP packet using I/O vector.
Parameters
- in
socketThe socket handle - in
iovThe pointer of I/O vector - in
iovcntThe number of I/O vector entries - out
receivedThe number of bytes received
Returns
0 on success, Otherwise, a negative error value.
mango_socket_recv_tls
mango_status_e mango_socket_recv_tls(mango_socket_h socket,
void *buf,
size_t len,
size_t *received)
Receive a TLS packet on the socket.
Parameters
- in
socketThe socket handle - in
bufThe buffer to receive - in
lenThe buffer size - out
receivedThe number of bytes received
Returns
0 on success, Otherwise, a negative error value.
the socket should have its SSL data already assigned.
mango_socket_reset
mango_status_e mango_socket_reset(mango_socket_h socket)
Reset the TCP socket.
Parameters
- in
socketThe socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_reset_blocking
mango_status_e mango_socket_reset_blocking(mango_socket_h socket,
useconds_t usec)
Reset the TCP socket, and wait for finishing it.
Parameters
- in
socketThe socket handle. - in
usecThe microseconds to wait.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_send
mango_status_e mango_socket_send(mango_socket_h socket,
const void *buf,
size_t len,
size_t *sent)
Send a TCP packet on the socket.
Parameters
- in
socketThe socket handle - in
bufThe buffer to be sent - in
lenThe buffer size - out
sentThe number of bytes sent
Returns
0 on success, Otherwise, a negative error value.
mango_socket_send_iov
mango_status_e mango_socket_send_iov(mango_socket_h socket,
const struct iovec *iov,
int iovcnt,
size_t *sent)
Send a TCP packet using I/O vector.
Parameters
- in
socketThe socket handle - in
iovThe pointer of I/O vector - in
iovcntThe number of I/O vector entries - out
sentThe number of bytes sent
Returns
0 on success, Otherwise, a negative error value.
mango_socket_send_tls
mango_status_e mango_socket_send_tls(mango_socket_h socket,
const void *buf,
size_t len,
size_t *sent)
Send a TLS packet on the socket.
Parameters
- in
socketThe socket handle - in
bufThe buffer to be sent - in
lenThe buffer size - out
sentThe number of bytes sent
Returns
0 on success, Otherwise, a negative error value.
the socket should have its SSL data already assigned.
mango_socket_set_nonblocking
mango_status_e mango_socket_set_nonblocking(mango_socket_h socket, bool enable)
Set non-blocking socket.
Parameters
- in
socketThe socket handle. - in
enableFlag to enable a non-blocking mode.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_set_option
mango_status_e mango_socket_set_option(mango_socket_h socket,
int level,
int optname,
const void *optval,
socklen_t optlen)
Set TCP socket option.
Parameters
- in
socketThe socket handle. - in
levelThe protocol level. - in
optnameThe option name. - in
optvalThe option value. - in
optlenThe option length.
Returns
0 on success, Otehrwise, a negative error value.
mango_socket_set_ssl
mango_status_e mango_socket_set_ssl(mango_socket_h socket, void *ssl)
Set the SSL data to the TCP socket.
Parameters
- in
socketThe socket handle. - in
sslThe SSL data.
Returns
0 on success, Otherwise, a negative error value.
mango_socket_shutdown
mango_status_e mango_socket_shutdown(mango_socket_h socket, int how)
Shut down part of a full-duplex connection.
Parameters
- in
socketThe socket handle. - in
howThe shutdown direction (in sys/socket.h).
Returns
0 on success, Otherwise, a negative error value.
mango_socket_wait
mango_status_e mango_socket_wait(mango_socket_h socket)
Wait completions of previous send requests.
Parameters
- in
socketThe socket handle.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_abort
mango_status_e mango_tcp_abort(mango_tcp_h tcp, int session)
Abort the TCP connection.
Parameters
- in
tcpThe tcp handle. - in
sessionThe session id.
Returns
0 on success, Otherwise, a negative error value.
It does not close the socket.
mango_tcp_accept
mango_status_e mango_tcp_accept(mango_tcp_h tcp,
struct sockaddr_in *saddr,
struct sockaddr_in *caddr,
bool non_blocking,
int *session)
Accept a TCP connection.
Parameters
- in
tcpThe tcp handle. - inout
saddrThe server sockaddr structure pointer. - out
caddr(Optional) The client sockaddr structure pointer. - in
non_blockingIndicates non blocking mode. - out
sessionThe output session id.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_close
mango_status_e mango_tcp_close(mango_tcp_h tcp, int session)
Close the TCP connection.
Parameters
- in
tcpThe tcp handle. - in
sessionThe session id.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_connect
mango_status_e mango_tcp_connect(mango_tcp_h tcp,
const struct sockaddr_in *saddr,
struct sockaddr_in *caddr,
int *session)
Initiate a TCP connection.
Parameters
- in
tcpThe tcp handle. - in
saddrThe server sockaddr structure pointer. - out
caddrThe client sockaddr structure pointer. - out
sessionThe output session id.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_create
mango_status_e mango_tcp_create(mango_tcp_h *tcp)
Create an tcp handle.
Parameters
- out
tcpThe output tcp handle.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_destroy
mango_status_e mango_tcp_destroy(mango_tcp_h tcp)
Destroy the tcp handle.
Parameters
- in
tcpThe tcp handle to be destroyed.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_epoll_check
mango_status_e mango_tcp_epoll_check(mango_tcp_h tcp, int fd)
Check whether a fd is an epoll instance of a tcp instance.
Parameters
- in
tcpThe tcp handle. - in
fdThe file descriptor.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_epoll_close
mango_status_e mango_tcp_epoll_close(mango_tcp_h tcp, int epfd)
Close an epoll instance.
Parameters
- in
tcpThe tcp handle. - in
epfdThe epoll file descriptor.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_epoll_create
mango_status_e mango_tcp_epoll_create(mango_tcp_h tcp, int epfd)
Create a new epoll instance.
Parameters
- in
tcpThe tcp handle. - in
epfdThe file descriptor referring to the epoll instance.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_epoll_ctl
mango_status_e mango_tcp_epoll_ctl(mango_tcp_h tcp,
int epfd,
int op,
int fd,
struct epoll_event *event)
Register epoll operations.
Parameters
- in
tcpThe tcp handle. - in
epfdThe epoll file descriptor. - in
opThe epoll opcode. - in
fdThe file descriptor - in
eventThe epoll event.
Returns
0 on success, Otherwise, a negative error value.
A negative session id indicates the TCP socket itself.
mango_tcp_epoll_wait
mango_status_e mango_tcp_epoll_wait(mango_tcp_h tcp,
int epfd,
struct epoll_event *event,
int max_events,
int timeout,
int *num_events)
Wait epoll events.
Parameters
- in
tcpThe tcp handle. - in
epfdThe epoll file descriptor. - in
eventThe epoll event - in
max_eventsThe number of maximum events - in
timeoutThe epoll timeout - out
num_eventsThe number of detected events
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_get_address
mango_status_e mango_tcp_get_address(mango_tcp_h tcp,
struct in_addr *ipaddr,
struct in_addr *subnet,
struct ether_addr *macaddr)
Get addresses from the tcp.
Parameters
- in
tcpthe tcp handle. - out
ipaddr(Optional) the IP address. - out
subnet(Optional) the subnet mask. - out
macaddr(Optional) the MAC address.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_get_ip_address
mango_status_e mango_tcp_get_ip_address(mango_tcp_h tcp, const char **ipaddr)
Get IP addresses from the tcp.
Parameters
- in
tcpthe tcp handle. - out
ipaddrthe IP address.
Returns
0 on success, Otherwise, a negative error value.
this API uses inet_ntoa() internally to be rewritten.
mango_tcp_listen
mango_status_e mango_tcp_listen(mango_tcp_h tcp, struct sockaddr_in *addr)
Listen for incoming connections.
Parameters
- in
tcpThe tcp handle. - in
addrThe sockaddr structure pointer.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_listen_close
mango_status_e mango_tcp_listen_close(mango_tcp_h tcp, struct sockaddr_in *addr)
Stop listening to incoming connections.
Parameters
- in
tcpThe tcp handle. - in
addrThe sockaddr structure pointer.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_poll
mango_status_e mango_tcp_poll(mango_tcp_h tcp,
struct pollfd *fds,
nfds_t nfds,
int timeout,
int *num_events)
wait for events on TCP sockets.
Parameters
- in
tcpThe tcp handle. - in
fdsThe array of poll file descriptors. - in
nfdsThe number of poll file descriptors. - in
timeoutThe poll timeout - out
num_eventsThe number of detected events
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_register_reset
mango_status_e mango_tcp_register_reset(mango_tcp_h tcp,
mango_tcp_reset_fn *reset,
void *param)
Register a reset function for the tcp handle.
Parameters
- in
tcpThe tcp handle to be reset. - in
resetA new reset function. - in
paramA parameter forwarded to the reset function.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_reset
mango_status_e mango_tcp_reset(mango_tcp_h tcp, bool preserved)
Reset the tcp handle asynchronously.
Parameters
- in
tcpThe tcp handle to be reset. - in
preservedPreserve internal states (e.g., IP/MAC address)
Returns
0 on success, Otherwise, a negative error value.
this api is asynchronous; if it returns MANGO_STATUS_IN_PROGRESS, it should be called again.
mango_tcp_session_reset
mango_status_e mango_tcp_session_reset(mango_tcp_h tcp,
int session,
suseconds_t timeout)
Reset the TCP connection.
Parameters
- in
tcpThe tcp handle. - in
sessionThe session id. - in
timeoutThe reset timeout in usec. (-1: block indefinitely, 0: return immediately)
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_set_address
mango_status_e mango_tcp_set_address(mango_tcp_h tcp,
const char *ipaddr,
const char *macaddr)
Set ethernet addresses to the tcp.
Parameters
- in
tcpthe tcp handle. - in
ipaddrthe IP address. - in
macaddrthe MAC address.
Returns
0 on success, Otherwise, a negative error value.
mango_tcp_set_context
mango_status_e mango_tcp_set_context(mango_tcp_h tcp, mango_context_h context)
Set the DPU context to the tcp.
Parameters
- in
tcpthe tcp handle. - in
contextthe context handle.
Returns
0 on success, Otherwise, a negative error value.
Context and IPC should be set exclusively.
mango_tcp_set_ipc
mango_status_e mango_tcp_set_ipc(mango_tcp_h tcp, mango_ipc_h ipc)
Set the IPC channel to the tcp.
Parameters
- in
tcpthe tcp handle. - in
ipcthe ipc handle.
Returns
0 on success, Otherwise, a negative error value.
In TCP, context and ipc should be set exclusively.
mango_tcp_wait
mango_status_e mango_tcp_wait(mango_tcp_h tcp, int session)
Wait completions of previous send requests.
Parameters
- in
tcpThe tcp handle. - in
sessionThe session id.
Returns
0 on success, Otherwise, a negative error value.