]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/sock.h
4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * TCP socket abstraction layer
41 #include "spdk/stdinc.h"
48 struct spdk_sock_group
;
51 * Get client and server addresses of the given socket.
53 * \param sock Socket to get address.
54 * \param saddr A pointer to the buffer to hold the address of server.
55 * \param slen Length of the buffer 'saddr'.
56 * \param sport A pointer(May be NULL) to the buffer to hold the port info of server.
57 * \param caddr A pointer to the buffer to hold the address of client.
58 * \param clen Length of the buffer 'caddr'.
59 * \param cport A pointer(May be NULL) to the buffer to hold the port info of server.
61 * \return 0 on success, -1 on failure.
63 int spdk_sock_getaddr(struct spdk_sock
*sock
, char *saddr
, int slen
, uint16_t *sport
,
64 char *caddr
, int clen
, uint16_t *cport
);
67 * Create a socket, connect the socket to the specified address and port (of the
68 * server), and then return the socket. This function is used by client.
70 * \param ip IP address of the server.
71 * \param port Port number of the server.
73 * \return a pointer to the connected socket on success, or NULL on failure.
75 struct spdk_sock
*spdk_sock_connect(const char *ip
, int port
);
78 * Create a socket, bind the socket to the specified address and port and listen
79 * on the socket, and then return the socket. This function is used by server.
81 * \param ip IP address to listen on.
82 * \param port Port number.
84 * \return a pointer to the listened socket on success, or NULL on failure.
86 struct spdk_sock
*spdk_sock_listen(const char *ip
, int port
);
89 * Accept a new connection from a client on the specified socket and return a
90 * socket structure which holds the connection.
92 * \param sock Listening socket.
94 * \return a pointer to the accepted socket on success, or NULL on failure.
96 struct spdk_sock
*spdk_sock_accept(struct spdk_sock
*sock
);
101 * \param sock Socket to close.
103 * \return 0 on success, -1 on failure.
105 int spdk_sock_close(struct spdk_sock
**sock
);
108 * Receive a message from the given socket.
110 * \param sock Socket to receive message.
111 * \param buf Pointer to a buffer to hold the data.
112 * \param len Length of the buffer.
114 * \return the length of the received message on success, -1 on failure.
116 ssize_t
spdk_sock_recv(struct spdk_sock
*sock
, void *buf
, size_t len
);
119 * Write message to the given socket from the I/O vector array.
121 * \param sock Socket to write to.
122 * \param iov I/O vector.
123 * \param iovcnt Number of I/O vectors in the array.
125 * \return the length of written message on success, -1 on failure.
127 ssize_t
spdk_sock_writev(struct spdk_sock
*sock
, struct iovec
*iov
, int iovcnt
);
130 * Set the value used to specify the low water mark (in bytes) for this socket.
132 * \param sock Socket to set for.
133 * \param nbytes Value for recvlowat.
135 * \return 0 on success, -1 on failure.
137 int spdk_sock_set_recvlowat(struct spdk_sock
*sock
, int nbytes
);
140 * Set receive buffer size for the given socket.
142 * \param sock Socket to set buffer size for.
143 * \param sz Buffer size in bytes.
145 * \return 0 on success, -1 on failure.
147 int spdk_sock_set_recvbuf(struct spdk_sock
*sock
, int sz
);
150 * Set send buffer size for the given socket.
152 * \param sock Socket to set buffer size for.
153 * \param sz Buffer size in bytes.
155 * \return 0 on success, -1 on failure.
157 int spdk_sock_set_sendbuf(struct spdk_sock
*sock
, int sz
);
160 * Check whether the address of socket is ipv6.
162 * \param sock Socket to check.
164 * \return true if the address of socket is ipv6, or false otherwise.
166 bool spdk_sock_is_ipv6(struct spdk_sock
*sock
);
169 * Check whether the address of socket is ipv4.
171 * \param sock Socket to check.
173 * \return true if the address of socket is ipv4, or false otherwise.
175 bool spdk_sock_is_ipv4(struct spdk_sock
*sock
);
178 * Callback function for spdk_sock_group_add_sock().
180 * \param arg Argument for the callback function.
181 * \param group Socket group.
182 * \param sock Socket.
184 typedef void (*spdk_sock_cb
)(void *arg
, struct spdk_sock_group
*group
, struct spdk_sock
*sock
);
187 * Create a new socket group.
189 * \return a pointer to the created group on success, or NULL on failure.
191 struct spdk_sock_group
*spdk_sock_group_create(void);
194 * Add a socket to the group.
196 * \param group Socket group.
197 * \param sock Socket to add.
198 * \param cb_fn Called when the operation completes.
199 * \param cb_arg Argument passed to the callback function.
201 * \return 0 on success, -1 on failure.
203 int spdk_sock_group_add_sock(struct spdk_sock_group
*group
, struct spdk_sock
*sock
,
204 spdk_sock_cb cb_fn
, void *cb_arg
);
207 * Remove a socket from the group.
209 * \param group Socket group.
210 * \param sock Socket to remove.
212 * \return 0 on success, -1 on failure.
214 int spdk_sock_group_remove_sock(struct spdk_sock_group
*group
, struct spdk_sock
*sock
);
217 * Poll incoming events for each registered socket.
219 * \param group Group to poll.
221 * \return 0 on success, -1 on failure.
223 int spdk_sock_group_poll(struct spdk_sock_group
*group
);
226 * Poll incoming events up to max_events for each registered socket.
228 * \param group Group to poll.
229 * \param max_events Number of maximum events to poll for each socket.
231 * \return the number of events on success, -1 on failure.
233 int spdk_sock_group_poll_count(struct spdk_sock_group
*group
, int max_events
);
236 * Close all registered sockets of the group and then remove the group.
238 * \param group Group to close.
240 * \return 0 on success, -1 on failure.
242 int spdk_sock_group_close(struct spdk_sock_group
**group
);
248 #endif /* SPDK_SOCK_H */