xref: /spdk/include/spdk_internal/sock.h (revision 2172c432cfdaecc5a279d64e37c6b51e794683c1)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2020 Mellanox Technologies LTD. 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 network implementation abstraction layer
36  */
37 
38 #ifndef SPDK_INTERNAL_SOCK_H
39 #define SPDK_INTERNAL_SOCK_H
40 
41 #include "spdk/stdinc.h"
42 #include "spdk/sock.h"
43 #include "spdk/queue.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #define MAX_EVENTS_PER_POLL 32
50 #define DEFAULT_SOCK_PRIORITY 0
51 #define MIN_SOCK_PIPE_SIZE 1024
52 #define MIN_SO_RCVBUF_SIZE (2 * 1024 * 1024)
53 #define MIN_SO_SNDBUF_SIZE (2 * 1024 * 1024)
54 
55 struct spdk_sock {
56 	struct spdk_net_impl		*net_impl;
57 	struct spdk_sock_opts		opts;
58 	struct spdk_sock_group_impl	*group_impl;
59 	TAILQ_ENTRY(spdk_sock)		link;
60 
61 	TAILQ_HEAD(, spdk_sock_request)	queued_reqs;
62 	TAILQ_HEAD(, spdk_sock_request)	pending_reqs;
63 	int				queued_iovcnt;
64 	int				cb_cnt;
65 	spdk_sock_cb			cb_fn;
66 	void				*cb_arg;
67 	int				placement_id;
68 	struct {
69 		uint8_t		closed		: 1;
70 		uint8_t		reserved	: 7;
71 	} flags;
72 };
73 
74 struct spdk_sock_group {
75 	STAILQ_HEAD(, spdk_sock_group_impl)	group_impls;
76 	void					*ctx;
77 };
78 
79 struct spdk_sock_group_impl {
80 	struct spdk_net_impl			*net_impl;
81 	TAILQ_HEAD(, spdk_sock)			socks;
82 	STAILQ_ENTRY(spdk_sock_group_impl)	link;
83 	/* List of removed sockets. refreshed each time we poll the sock group. */
84 	int					num_removed_socks;
85 	/* Unfortunately, we can't just keep a tailq of the sockets in case they are freed
86 	 * or added to another poll group later.
87 	 */
88 	uintptr_t				removed_socks[MAX_EVENTS_PER_POLL];
89 };
90 
91 struct spdk_net_impl {
92 	const char *name;
93 	int priority;
94 
95 	int (*getaddr)(struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport, char *caddr,
96 		       int clen, uint16_t *cport);
97 	struct spdk_sock *(*connect)(const char *ip, int port, struct spdk_sock_opts *opts);
98 	struct spdk_sock *(*listen)(const char *ip, int port, struct spdk_sock_opts *opts);
99 	struct spdk_sock *(*accept)(struct spdk_sock *sock);
100 	int (*close)(struct spdk_sock *sock);
101 	ssize_t (*recv)(struct spdk_sock *sock, void *buf, size_t len);
102 	ssize_t (*readv)(struct spdk_sock *sock, struct iovec *iov, int iovcnt);
103 	ssize_t (*writev)(struct spdk_sock *sock, struct iovec *iov, int iovcnt);
104 
105 	void (*writev_async)(struct spdk_sock *sock, struct spdk_sock_request *req);
106 	int (*flush)(struct spdk_sock *sock);
107 
108 	int (*set_recvlowat)(struct spdk_sock *sock, int nbytes);
109 	int (*set_recvbuf)(struct spdk_sock *sock, int sz);
110 	int (*set_sendbuf)(struct spdk_sock *sock, int sz);
111 
112 	bool (*is_ipv6)(struct spdk_sock *sock);
113 	bool (*is_ipv4)(struct spdk_sock *sock);
114 	bool (*is_connected)(struct spdk_sock *sock);
115 
116 	int (*get_placement_id)(struct spdk_sock *sock, int *placement_id);
117 	struct spdk_sock_group_impl *(*group_impl_create)(void);
118 	int (*group_impl_add_sock)(struct spdk_sock_group_impl *group, struct spdk_sock *sock);
119 	int (*group_impl_remove_sock)(struct spdk_sock_group_impl *group, struct spdk_sock *sock);
120 	int (*group_impl_poll)(struct spdk_sock_group_impl *group, int max_events,
121 			       struct spdk_sock **socks);
122 	int (*group_impl_close)(struct spdk_sock_group_impl *group);
123 
124 	int (*get_opts)(struct spdk_sock_impl_opts *opts, size_t *len);
125 	int (*set_opts)(const struct spdk_sock_impl_opts *opts, size_t len);
126 
127 	STAILQ_ENTRY(spdk_net_impl) link;
128 };
129 
130 void spdk_net_impl_register(struct spdk_net_impl *impl, int priority);
131 
132 #define SPDK_NET_IMPL_REGISTER(name, impl, priority) \
133 static void __attribute__((constructor)) net_impl_register_##name(void) \
134 { \
135 	spdk_net_impl_register(impl, priority); \
136 }
137 
138 static inline void
139 spdk_sock_request_queue(struct spdk_sock *sock, struct spdk_sock_request *req)
140 {
141 	TAILQ_INSERT_TAIL(&sock->queued_reqs, req, internal.link);
142 	sock->queued_iovcnt += req->iovcnt;
143 }
144 
145 static inline void
146 spdk_sock_request_pend(struct spdk_sock *sock, struct spdk_sock_request *req)
147 {
148 	TAILQ_REMOVE(&sock->queued_reqs, req, internal.link);
149 	assert(sock->queued_iovcnt >= req->iovcnt);
150 	sock->queued_iovcnt -= req->iovcnt;
151 	TAILQ_INSERT_TAIL(&sock->pending_reqs, req, internal.link);
152 }
153 
154 static inline int
155 spdk_sock_request_put(struct spdk_sock *sock, struct spdk_sock_request *req, int err)
156 {
157 	bool closed;
158 	int rc = 0;
159 
160 	TAILQ_REMOVE(&sock->pending_reqs, req, internal.link);
161 
162 	req->internal.offset = 0;
163 
164 	closed = sock->flags.closed;
165 	sock->cb_cnt++;
166 	req->cb_fn(req->cb_arg, err);
167 	assert(sock->cb_cnt > 0);
168 	sock->cb_cnt--;
169 
170 	if (sock->cb_cnt == 0 && !closed && sock->flags.closed) {
171 		/* The user closed the socket in response to a callback above. */
172 		rc = -1;
173 		spdk_sock_close(&sock);
174 	}
175 
176 	return rc;
177 }
178 
179 static inline int
180 spdk_sock_abort_requests(struct spdk_sock *sock)
181 {
182 	struct spdk_sock_request *req;
183 	bool closed;
184 	int rc = 0;
185 
186 	closed = sock->flags.closed;
187 	sock->cb_cnt++;
188 
189 	req = TAILQ_FIRST(&sock->pending_reqs);
190 	while (req) {
191 		TAILQ_REMOVE(&sock->pending_reqs, req, internal.link);
192 
193 		req->cb_fn(req->cb_arg, -ECANCELED);
194 
195 		req = TAILQ_FIRST(&sock->pending_reqs);
196 	}
197 
198 	req = TAILQ_FIRST(&sock->queued_reqs);
199 	while (req) {
200 		TAILQ_REMOVE(&sock->queued_reqs, req, internal.link);
201 
202 		assert(sock->queued_iovcnt >= req->iovcnt);
203 		sock->queued_iovcnt -= req->iovcnt;
204 
205 		req->cb_fn(req->cb_arg, -ECANCELED);
206 
207 		req = TAILQ_FIRST(&sock->queued_reqs);
208 	}
209 	assert(sock->cb_cnt > 0);
210 	sock->cb_cnt--;
211 
212 	assert(TAILQ_EMPTY(&sock->queued_reqs));
213 	assert(TAILQ_EMPTY(&sock->pending_reqs));
214 
215 	if (sock->cb_cnt == 0 && !closed && sock->flags.closed) {
216 		/* The user closed the socket in response to a callback above. */
217 		rc = -1;
218 		spdk_sock_close(&sock);
219 	}
220 
221 	return rc;
222 }
223 
224 #ifdef __cplusplus
225 }
226 #endif
227 
228 #endif /* SPDK_INTERNAL_SOCK_H */
229