sock.h File Reference

TCP socket abstraction layer. More...

Data Structures

struct  spdk_sock_request
 Anywhere this struct is used, an iovec array is assumed to immediately follow the last member in memory, without any padding. More...
 
struct  spdk_sock_request::__sock_request_internal
 These fields are used by the socket layer and should not be modified. More...
 
struct  spdk_sock_opts
 Spdk socket initialization options. More...
 

Macros

#define SPDK_SOCK_REQUEST_IOV(req, i)   ((struct iovec *)(((uint8_t *)req + sizeof(struct spdk_sock_request)) + (sizeof(struct iovec) * i)))
 

Typedefs

typedef void(* spdk_sock_cb) (void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)
 Callback function for spdk_sock_group_add_sock(). More...
 

Functions

void spdk_sock_get_default_opts (struct spdk_sock_opts *opts)
 Initialize the default value of opts. More...
 
int spdk_sock_getaddr (struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport, char *caddr, int clen, uint16_t *cport)
 Get client and server addresses of the given socket. More...
 
struct spdk_sock * spdk_sock_connect (const char *ip, int port, char *impl_name)
 Create a socket using the specific sock implementation, connect the socket to the specified address and port (of the server), and then return the socket. More...
 
struct spdk_sock * spdk_sock_connect_ext (const char *ip, int port, char *impl_name, struct spdk_sock_opts *opts)
 Create a socket using the specific sock implementation, connect the socket to the specified address and port (of the server), and then return the socket. More...
 
struct spdk_sock * spdk_sock_listen (const char *ip, int port, char *impl_name)
 Create a socket using the specific sock implementation, bind the socket to the specified address and port and listen on the socket, and then return the socket. More...
 
struct spdk_sock * spdk_sock_listen_ext (const char *ip, int port, char *impl_name, struct spdk_sock_opts *opts)
 Create a socket using the specific sock implementation, bind the socket to the specified address and port and listen on the socket, and then return the socket. More...
 
struct spdk_sock * spdk_sock_accept (struct spdk_sock *sock)
 Accept a new connection from a client on the specified socket and return a socket structure which holds the connection. More...
 
int spdk_sock_close (struct spdk_sock **sock)
 Close a socket. More...
 
int spdk_sock_flush (struct spdk_sock *sock)
 Flush a socket from data gathered in previous writev_async calls. More...
 
ssize_t spdk_sock_recv (struct spdk_sock *sock, void *buf, size_t len)
 Receive a message from the given socket. More...
 
ssize_t spdk_sock_writev (struct spdk_sock *sock, struct iovec *iov, int iovcnt)
 Write message to the given socket from the I/O vector array. More...
 
void spdk_sock_writev_async (struct spdk_sock *sock, struct spdk_sock_request *req)
 Write data to the given socket asynchronously, calling the provided callback when the data has been written. More...
 
ssize_t spdk_sock_readv (struct spdk_sock *sock, struct iovec *iov, int iovcnt)
 Read message from the given socket to the I/O vector array. More...
 
int spdk_sock_set_recvlowat (struct spdk_sock *sock, int nbytes)
 Set the value used to specify the low water mark (in bytes) for this socket. More...
 
int spdk_sock_set_recvbuf (struct spdk_sock *sock, int sz)
 Set receive buffer size for the given socket. More...
 
int spdk_sock_set_sendbuf (struct spdk_sock *sock, int sz)
 Set send buffer size for the given socket. More...
 
bool spdk_sock_is_ipv6 (struct spdk_sock *sock)
 Check whether the address of socket is ipv6. More...
 
bool spdk_sock_is_ipv4 (struct spdk_sock *sock)
 Check whether the address of socket is ipv4. More...
 
bool spdk_sock_is_connected (struct spdk_sock *sock)
 Check whether the socket is currently connected. More...
 
struct spdk_sock_group * spdk_sock_group_create (void *ctx)
 Create a new socket group with user provided pointer. More...
 
void * spdk_sock_group_get_ctx (struct spdk_sock_group *sock_group)
 Get the ctx of the sock group. More...
 
int spdk_sock_group_add_sock (struct spdk_sock_group *group, struct spdk_sock *sock, spdk_sock_cb cb_fn, void *cb_arg)
 Add a socket to the group. More...
 
int spdk_sock_group_remove_sock (struct spdk_sock_group *group, struct spdk_sock *sock)
 Remove a socket from the group. More...
 
int spdk_sock_group_poll (struct spdk_sock_group *group)
 Poll incoming events for each registered socket. More...
 
int spdk_sock_group_poll_count (struct spdk_sock_group *group, int max_events)
 Poll incoming events up to max_events for each registered socket. More...
 
int spdk_sock_group_close (struct spdk_sock_group **group)
 Close all registered sockets of the group and then remove the group. More...
 
int spdk_sock_get_optimal_sock_group (struct spdk_sock *sock, struct spdk_sock_group **group)
 Get the optimal sock group for this sock. More...
 

Detailed Description

TCP socket abstraction layer.

Typedef Documentation

◆ spdk_sock_cb

typedef void(* spdk_sock_cb) (void *arg, struct spdk_sock_group *group, struct spdk_sock *sock)

Callback function for spdk_sock_group_add_sock().

Parameters
argArgument for the callback function.
groupSocket group.
sockSocket.

Function Documentation

◆ spdk_sock_accept()

struct spdk_sock* spdk_sock_accept ( struct spdk_sock *  sock)

Accept a new connection from a client on the specified socket and return a socket structure which holds the connection.

Parameters
sockListening socket.
Returns
a pointer to the accepted socket on success, or NULL on failure.

◆ spdk_sock_close()

int spdk_sock_close ( struct spdk_sock **  sock)

Close a socket.

Parameters
sockSocket to close.
Returns
0 on success, -1 on failure.

◆ spdk_sock_connect()

struct spdk_sock* spdk_sock_connect ( const char *  ip,
int  port,
char *  impl_name 
)

Create a socket using the specific sock implementation, connect the socket to the specified address and port (of the server), and then return the socket.

This function is used by client.

Parameters
ipIP address of the server.
portPort number of the server.
impl_nameThe sock_implementation to use, such as "posix". If impl_name is specified, it will only try to connect on that impl. If it is NULL, it will try all the sock implementations in order and uses the first sock implementation which can connect. For example, it may try vpp first, then fall back to posix.
Returns
a pointer to the connected socket on success, or NULL on failure.

◆ spdk_sock_connect_ext()

struct spdk_sock* spdk_sock_connect_ext ( const char *  ip,
int  port,
char *  impl_name,
struct spdk_sock_opts opts 
)

Create a socket using the specific sock implementation, connect the socket to the specified address and port (of the server), and then return the socket.

This function is used by client.

Parameters
ipIP address of the server.
portPort number of the server.
impl_nameThe sock_implementation to use, such as "posix". If impl_name is specified, it will only try to connect on that impl. If it is NULL, it will try all the sock implementations in order and uses the first sock implementation which can connect. For example, it may try vpp first, then fall back to posix.
optsThe sock option pointer provided by the user which should not be NULL pointer.
Returns
a pointer to the connected socket on success, or NULL on failure.

◆ spdk_sock_flush()

int spdk_sock_flush ( struct spdk_sock *  sock)

Flush a socket from data gathered in previous writev_async calls.

Parameters
sockSocket to flush.
Returns
0 on success, -1 on failure.

◆ spdk_sock_get_default_opts()

void spdk_sock_get_default_opts ( struct spdk_sock_opts opts)

Initialize the default value of opts.

Parameters
optsData structure where SPDK will initialize the default sock options. Users must set opts_size to sizeof(struct spdk_sock_opts). This will ensure that the libraryonly tries to fill as many fields as allocated by the caller. This allows ABI compatibility with future versions of this library that may extend the spdk_sock_opts structure.

◆ spdk_sock_get_optimal_sock_group()

int spdk_sock_get_optimal_sock_group ( struct spdk_sock *  sock,
struct spdk_sock_group **  group 
)

Get the optimal sock group for this sock.

Parameters
sockThe socket
groupReturns the optimal sock group. If there is no optimal sock group, returns NULL.
Returns
0 on success. Negated errno on failure.

◆ spdk_sock_getaddr()

int spdk_sock_getaddr ( struct spdk_sock *  sock,
char *  saddr,
int  slen,
uint16_t *  sport,
char *  caddr,
int  clen,
uint16_t *  cport 
)

Get client and server addresses of the given socket.

Parameters
sockSocket to get address.
saddrA pointer to the buffer to hold the address of server.
slenLength of the buffer 'saddr'.
sportA pointer(May be NULL) to the buffer to hold the port info of server.
caddrA pointer to the buffer to hold the address of client.
clenLength of the buffer 'caddr'.
cportA pointer(May be NULL) to the buffer to hold the port info of server.
Returns
0 on success, -1 on failure.

◆ spdk_sock_group_add_sock()

int spdk_sock_group_add_sock ( struct spdk_sock_group *  group,
struct spdk_sock *  sock,
spdk_sock_cb  cb_fn,
void *  cb_arg 
)

Add a socket to the group.

Parameters
groupSocket group.
sockSocket to add.
cb_fnCalled when the operation completes.
cb_argArgument passed to the callback function.
Returns
0 on success, -1 on failure.

◆ spdk_sock_group_close()

int spdk_sock_group_close ( struct spdk_sock_group **  group)

Close all registered sockets of the group and then remove the group.

Parameters
groupGroup to close.
Returns
0 on success, -1 on failure.

◆ spdk_sock_group_create()

struct spdk_sock_group* spdk_sock_group_create ( void *  ctx)

Create a new socket group with user provided pointer.

Parameters
ctxthe context provided by user.
Returns
a pointer to the created group on success, or NULL on failure.

◆ spdk_sock_group_get_ctx()

void* spdk_sock_group_get_ctx ( struct spdk_sock_group *  sock_group)

Get the ctx of the sock group.

Parameters
sock_groupSocket group.
Returns
a pointer which is ctx of the sock_group.

◆ spdk_sock_group_poll()

int spdk_sock_group_poll ( struct spdk_sock_group *  group)

Poll incoming events for each registered socket.

Parameters
groupGroup to poll.
Returns
the number of events on success, -1 on failure.

◆ spdk_sock_group_poll_count()

int spdk_sock_group_poll_count ( struct spdk_sock_group *  group,
int  max_events 
)

Poll incoming events up to max_events for each registered socket.

Parameters
groupGroup to poll.
max_eventsNumber of maximum events to poll for each socket.
Returns
the number of events on success, -1 on failure.

◆ spdk_sock_group_remove_sock()

int spdk_sock_group_remove_sock ( struct spdk_sock_group *  group,
struct spdk_sock *  sock 
)

Remove a socket from the group.

Parameters
groupSocket group.
sockSocket to remove.
Returns
0 on success, -1 on failure.

◆ spdk_sock_is_connected()

bool spdk_sock_is_connected ( struct spdk_sock *  sock)

Check whether the socket is currently connected.

Parameters
sockSocket to check
Returns
true if the socket is connected or false otherwise.

◆ spdk_sock_is_ipv4()

bool spdk_sock_is_ipv4 ( struct spdk_sock *  sock)

Check whether the address of socket is ipv4.

Parameters
sockSocket to check.
Returns
true if the address of socket is ipv4, or false otherwise.

◆ spdk_sock_is_ipv6()

bool spdk_sock_is_ipv6 ( struct spdk_sock *  sock)

Check whether the address of socket is ipv6.

Parameters
sockSocket to check.
Returns
true if the address of socket is ipv6, or false otherwise.

◆ spdk_sock_listen()

struct spdk_sock* spdk_sock_listen ( const char *  ip,
int  port,
char *  impl_name 
)

Create a socket using the specific sock implementation, bind the socket to the specified address and port and listen on the socket, and then return the socket.

This function is used by server.

Parameters
ipIP address to listen on.
portPort number.
impl_nameThe sock_implementation to use, such as "posix". If impl_name is specified, it will only try to listen on that impl. If it is NULL, it will try all the sock implementations in order and uses the first sock implementation which can listen. For example, it may try vpp first, then fall back to posix.
Returns
a pointer to the listened socket on success, or NULL on failure.

◆ spdk_sock_listen_ext()

struct spdk_sock* spdk_sock_listen_ext ( const char *  ip,
int  port,
char *  impl_name,
struct spdk_sock_opts opts 
)

Create a socket using the specific sock implementation, bind the socket to the specified address and port and listen on the socket, and then return the socket.

This function is used by server.

Parameters
ipIP address to listen on.
portPort number.
impl_nameThe sock_implementation to use, such as "posix". If impl_name is specified, it will only try to listen on that impl. If it is NULL, it will try all the sock implementations in order and uses the first sock implementation which can listen. For example, it may try vpp first, then fall back to posix.
optsThe sock option pointer provided by the user, which should not be NULL pointer.
Returns
a pointer to the listened socket on success, or NULL on failure.

◆ spdk_sock_readv()

ssize_t spdk_sock_readv ( struct spdk_sock *  sock,
struct iovec *  iov,
int  iovcnt 
)

Read message from the given socket to the I/O vector array.

Parameters
sockSocket to receive message.
iovI/O vector.
iovcntNumber of I/O vectors in the array.
Returns
the length of the received message on success, -1 on failure.

◆ spdk_sock_recv()

ssize_t spdk_sock_recv ( struct spdk_sock *  sock,
void *  buf,
size_t  len 
)

Receive a message from the given socket.

Parameters
sockSocket to receive message.
bufPointer to a buffer to hold the data.
lenLength of the buffer.
Returns
the length of the received message on success, -1 on failure.

◆ spdk_sock_set_recvbuf()

int spdk_sock_set_recvbuf ( struct spdk_sock *  sock,
int  sz 
)

Set receive buffer size for the given socket.

Parameters
sockSocket to set buffer size for.
szBuffer size in bytes.
Returns
0 on success, -1 on failure.

◆ spdk_sock_set_recvlowat()

int spdk_sock_set_recvlowat ( struct spdk_sock *  sock,
int  nbytes 
)

Set the value used to specify the low water mark (in bytes) for this socket.

Parameters
sockSocket to set for.
nbytesValue for recvlowat.
Returns
0 on success, -1 on failure.

◆ spdk_sock_set_sendbuf()

int spdk_sock_set_sendbuf ( struct spdk_sock *  sock,
int  sz 
)

Set send buffer size for the given socket.

Parameters
sockSocket to set buffer size for.
szBuffer size in bytes.
Returns
0 on success, -1 on failure.

◆ spdk_sock_writev()

ssize_t spdk_sock_writev ( struct spdk_sock *  sock,
struct iovec *  iov,
int  iovcnt 
)

Write message to the given socket from the I/O vector array.

Parameters
sockSocket to write to.
iovI/O vector.
iovcntNumber of I/O vectors in the array.
Returns
the length of written message on success, -1 on failure.

◆ spdk_sock_writev_async()

void spdk_sock_writev_async ( struct spdk_sock *  sock,
struct spdk_sock_request req 
)

Write data to the given socket asynchronously, calling the provided callback when the data has been written.

Parameters
sockSocket to write to.
reqThe write request to submit.