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