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