]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/include/spdk/sock.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / include / spdk / sock.h
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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.
32 */
33
34 /** \file
35 * TCP socket abstraction layer
36 */
37
38 #ifndef SPDK_SOCK_H
39 #define SPDK_SOCK_H
40
41 #include "spdk/stdinc.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 struct spdk_sock;
48 struct spdk_sock_group;
49
50 /**
51 * Get client and server addresses of the given socket.
52 *
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.
60 *
61 * \return 0 on success, -1 on failure.
62 */
63 int spdk_sock_getaddr(struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport,
64 char *caddr, int clen, uint16_t *cport);
65
66 /**
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.
69 *
70 * \param ip IP address of the server.
71 * \param port Port number of the server.
72 *
73 * \return a pointer to the connected socket on success, or NULL on failure.
74 */
75 struct spdk_sock *spdk_sock_connect(const char *ip, int port);
76
77 /**
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.
80 *
81 * \param ip IP address to listen on.
82 * \param port Port number.
83 *
84 * \return a pointer to the listened socket on success, or NULL on failure.
85 */
86 struct spdk_sock *spdk_sock_listen(const char *ip, int port);
87
88 /**
89 * Accept a new connection from a client on the specified socket and return a
90 * socket structure which holds the connection.
91 *
92 * \param sock Listening socket.
93 *
94 * \return a pointer to the accepted socket on success, or NULL on failure.
95 */
96 struct spdk_sock *spdk_sock_accept(struct spdk_sock *sock);
97
98 /**
99 * Close a socket.
100 *
101 * \param sock Socket to close.
102 *
103 * \return 0 on success, -1 on failure.
104 */
105 int spdk_sock_close(struct spdk_sock **sock);
106
107 /**
108 * Receive a message from the given socket.
109 *
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.
113 *
114 * \return the length of the received message on success, -1 on failure.
115 */
116 ssize_t spdk_sock_recv(struct spdk_sock *sock, void *buf, size_t len);
117
118 /**
119 * Write message to the given socket from the I/O vector array.
120 *
121 * \param sock Socket to write to.
122 * \param iov I/O vector.
123 * \param iovcnt Number of I/O vectors in the array.
124 *
125 * \return the length of written message on success, -1 on failure.
126 */
127 ssize_t spdk_sock_writev(struct spdk_sock *sock, struct iovec *iov, int iovcnt);
128
129 /**
130 * Set the value used to specify the low water mark (in bytes) for this socket.
131 *
132 * \param sock Socket to set for.
133 * \param nbytes Value for recvlowat.
134 *
135 * \return 0 on success, -1 on failure.
136 */
137 int spdk_sock_set_recvlowat(struct spdk_sock *sock, int nbytes);
138
139 /**
140 * Set receive buffer size for the given socket.
141 *
142 * \param sock Socket to set buffer size for.
143 * \param sz Buffer size in bytes.
144 *
145 * \return 0 on success, -1 on failure.
146 */
147 int spdk_sock_set_recvbuf(struct spdk_sock *sock, int sz);
148
149 /**
150 * Set send buffer size for the given socket.
151 *
152 * \param sock Socket to set buffer size for.
153 * \param sz Buffer size in bytes.
154 *
155 * \return 0 on success, -1 on failure.
156 */
157 int spdk_sock_set_sendbuf(struct spdk_sock *sock, int sz);
158
159 /**
160 * Check whether the address of socket is ipv6.
161 *
162 * \param sock Socket to check.
163 *
164 * \return true if the address of socket is ipv6, or false otherwise.
165 */
166 bool spdk_sock_is_ipv6(struct spdk_sock *sock);
167
168 /**
169 * Check whether the address of socket is ipv4.
170 *
171 * \param sock Socket to check.
172 *
173 * \return true if the address of socket is ipv4, or false otherwise.
174 */
175 bool spdk_sock_is_ipv4(struct spdk_sock *sock);
176
177 /**
178 * Callback function for spdk_sock_group_add_sock().
179 *
180 * \param arg Argument for the callback function.
181 * \param group Socket group.
182 * \param sock Socket.
183 */
184 typedef void (*spdk_sock_cb)(void *arg, struct spdk_sock_group *group, struct spdk_sock *sock);
185
186 /**
187 * Create a new socket group.
188 *
189 * \return a pointer to the created group on success, or NULL on failure.
190 */
191 struct spdk_sock_group *spdk_sock_group_create(void);
192
193 /**
194 * Add a socket to the group.
195 *
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.
200 *
201 * \return 0 on success, -1 on failure.
202 */
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);
205
206 /**
207 * Remove a socket from the group.
208 *
209 * \param group Socket group.
210 * \param sock Socket to remove.
211 *
212 * \return 0 on success, -1 on failure.
213 */
214 int spdk_sock_group_remove_sock(struct spdk_sock_group *group, struct spdk_sock *sock);
215
216 /**
217 * Poll incoming events for each registered socket.
218 *
219 * \param group Group to poll.
220 *
221 * \return 0 on success, -1 on failure.
222 */
223 int spdk_sock_group_poll(struct spdk_sock_group *group);
224
225 /**
226 * Poll incoming events up to max_events for each registered socket.
227 *
228 * \param group Group to poll.
229 * \param max_events Number of maximum events to poll for each socket.
230 *
231 * \return the number of events on success, -1 on failure.
232 */
233 int spdk_sock_group_poll_count(struct spdk_sock_group *group, int max_events);
234
235 /**
236 * Close all registered sockets of the group and then remove the group.
237 *
238 * \param group Group to close.
239 *
240 * \return 0 on success, -1 on failure.
241 */
242 int spdk_sock_group_close(struct spdk_sock_group **group);
243
244 #ifdef __cplusplus
245 }
246 #endif
247
248 #endif /* SPDK_SOCK_H */