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 #include "spdk/likely.h" 45 46 #ifdef __cplusplus 47 extern "C" { 48 #endif 49 50 #define MAX_EVENTS_PER_POLL 32 51 #define DEFAULT_SOCK_PRIORITY 0 52 #define MIN_SOCK_PIPE_SIZE 1024 53 #define MIN_SO_RCVBUF_SIZE (2 * 1024 * 1024) 54 #define MIN_SO_SNDBUF_SIZE (2 * 1024 * 1024) 55 #define IOV_BATCH_SIZE 64 56 57 struct spdk_sock { 58 struct spdk_net_impl *net_impl; 59 struct spdk_sock_opts opts; 60 struct spdk_sock_group_impl *group_impl; 61 TAILQ_ENTRY(spdk_sock) link; 62 63 TAILQ_HEAD(, spdk_sock_request) queued_reqs; 64 TAILQ_HEAD(, spdk_sock_request) pending_reqs; 65 int queued_iovcnt; 66 int cb_cnt; 67 spdk_sock_cb cb_fn; 68 void *cb_arg; 69 int placement_id; 70 struct { 71 uint8_t closed : 1; 72 uint8_t reserved : 7; 73 } flags; 74 }; 75 76 struct spdk_sock_group { 77 STAILQ_HEAD(, spdk_sock_group_impl) group_impls; 78 void *ctx; 79 }; 80 81 struct spdk_sock_group_impl { 82 struct spdk_net_impl *net_impl; 83 TAILQ_HEAD(, spdk_sock) socks; 84 STAILQ_ENTRY(spdk_sock_group_impl) link; 85 /* List of removed sockets. refreshed each time we poll the sock group. */ 86 int num_removed_socks; 87 /* Unfortunately, we can't just keep a tailq of the sockets in case they are freed 88 * or added to another poll group later. 89 */ 90 uintptr_t removed_socks[MAX_EVENTS_PER_POLL]; 91 }; 92 93 struct spdk_net_impl { 94 const char *name; 95 int priority; 96 97 int (*getaddr)(struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport, char *caddr, 98 int clen, uint16_t *cport); 99 struct spdk_sock *(*connect)(const char *ip, int port, struct spdk_sock_opts *opts); 100 struct spdk_sock *(*listen)(const char *ip, int port, struct spdk_sock_opts *opts); 101 struct spdk_sock *(*accept)(struct spdk_sock *sock); 102 int (*close)(struct spdk_sock *sock); 103 ssize_t (*recv)(struct spdk_sock *sock, void *buf, size_t len); 104 ssize_t (*readv)(struct spdk_sock *sock, struct iovec *iov, int iovcnt); 105 ssize_t (*writev)(struct spdk_sock *sock, struct iovec *iov, int iovcnt); 106 107 void (*writev_async)(struct spdk_sock *sock, struct spdk_sock_request *req); 108 int (*flush)(struct spdk_sock *sock); 109 110 int (*set_recvlowat)(struct spdk_sock *sock, int nbytes); 111 int (*set_recvbuf)(struct spdk_sock *sock, int sz); 112 int (*set_sendbuf)(struct spdk_sock *sock, int sz); 113 114 bool (*is_ipv6)(struct spdk_sock *sock); 115 bool (*is_ipv4)(struct spdk_sock *sock); 116 bool (*is_connected)(struct spdk_sock *sock); 117 118 int (*get_placement_id)(struct spdk_sock *sock, int *placement_id); 119 struct spdk_sock_group_impl *(*group_impl_create)(void); 120 int (*group_impl_add_sock)(struct spdk_sock_group_impl *group, struct spdk_sock *sock); 121 int (*group_impl_remove_sock)(struct spdk_sock_group_impl *group, struct spdk_sock *sock); 122 int (*group_impl_poll)(struct spdk_sock_group_impl *group, int max_events, 123 struct spdk_sock **socks); 124 int (*group_impl_close)(struct spdk_sock_group_impl *group); 125 126 int (*get_opts)(struct spdk_sock_impl_opts *opts, size_t *len); 127 int (*set_opts)(const struct spdk_sock_impl_opts *opts, size_t len); 128 129 STAILQ_ENTRY(spdk_net_impl) link; 130 }; 131 132 void spdk_net_impl_register(struct spdk_net_impl *impl, int priority); 133 134 #define SPDK_NET_IMPL_REGISTER(name, impl, priority) \ 135 static void __attribute__((constructor)) net_impl_register_##name(void) \ 136 { \ 137 spdk_net_impl_register(impl, priority); \ 138 } 139 140 static inline void 141 spdk_sock_request_queue(struct spdk_sock *sock, struct spdk_sock_request *req) 142 { 143 TAILQ_INSERT_TAIL(&sock->queued_reqs, req, internal.link); 144 sock->queued_iovcnt += req->iovcnt; 145 } 146 147 static inline void 148 spdk_sock_request_pend(struct spdk_sock *sock, struct spdk_sock_request *req) 149 { 150 TAILQ_REMOVE(&sock->queued_reqs, req, internal.link); 151 assert(sock->queued_iovcnt >= req->iovcnt); 152 sock->queued_iovcnt -= req->iovcnt; 153 TAILQ_INSERT_TAIL(&sock->pending_reqs, req, internal.link); 154 } 155 156 static inline int 157 spdk_sock_request_put(struct spdk_sock *sock, struct spdk_sock_request *req, int err) 158 { 159 bool closed; 160 int rc = 0; 161 162 TAILQ_REMOVE(&sock->pending_reqs, req, internal.link); 163 164 req->internal.offset = 0; 165 166 closed = sock->flags.closed; 167 sock->cb_cnt++; 168 req->cb_fn(req->cb_arg, err); 169 assert(sock->cb_cnt > 0); 170 sock->cb_cnt--; 171 172 if (sock->cb_cnt == 0 && !closed && sock->flags.closed) { 173 /* The user closed the socket in response to a callback above. */ 174 rc = -1; 175 spdk_sock_close(&sock); 176 } 177 178 return rc; 179 } 180 181 static inline int 182 spdk_sock_abort_requests(struct spdk_sock *sock) 183 { 184 struct spdk_sock_request *req; 185 bool closed; 186 int rc = 0; 187 188 closed = sock->flags.closed; 189 sock->cb_cnt++; 190 191 req = TAILQ_FIRST(&sock->pending_reqs); 192 while (req) { 193 TAILQ_REMOVE(&sock->pending_reqs, req, internal.link); 194 195 req->cb_fn(req->cb_arg, -ECANCELED); 196 197 req = TAILQ_FIRST(&sock->pending_reqs); 198 } 199 200 req = TAILQ_FIRST(&sock->queued_reqs); 201 while (req) { 202 TAILQ_REMOVE(&sock->queued_reqs, req, internal.link); 203 204 assert(sock->queued_iovcnt >= req->iovcnt); 205 sock->queued_iovcnt -= req->iovcnt; 206 207 req->cb_fn(req->cb_arg, -ECANCELED); 208 209 req = TAILQ_FIRST(&sock->queued_reqs); 210 } 211 assert(sock->cb_cnt > 0); 212 sock->cb_cnt--; 213 214 assert(TAILQ_EMPTY(&sock->queued_reqs)); 215 assert(TAILQ_EMPTY(&sock->pending_reqs)); 216 217 if (sock->cb_cnt == 0 && !closed && sock->flags.closed) { 218 /* The user closed the socket in response to a callback above. */ 219 rc = -1; 220 spdk_sock_close(&sock); 221 } 222 223 return rc; 224 } 225 226 static inline int 227 spdk_sock_prep_reqs(struct spdk_sock *_sock, struct iovec *iovs, int index, 228 struct spdk_sock_request **last_req) 229 { 230 int iovcnt, i; 231 struct spdk_sock_request *req; 232 unsigned int offset; 233 234 /* Gather an iov */ 235 iovcnt = index; 236 if (spdk_unlikely(iovcnt >= IOV_BATCH_SIZE)) { 237 goto end; 238 } 239 240 if (last_req != NULL && *last_req != NULL) { 241 req = TAILQ_NEXT(*last_req, internal.link); 242 } else { 243 req = TAILQ_FIRST(&_sock->queued_reqs); 244 } 245 246 while (req) { 247 offset = req->internal.offset; 248 249 for (i = 0; i < req->iovcnt; i++) { 250 /* Consume any offset first */ 251 if (offset >= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len) { 252 offset -= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len; 253 continue; 254 } 255 256 iovs[iovcnt].iov_base = SPDK_SOCK_REQUEST_IOV(req, i)->iov_base + offset; 257 iovs[iovcnt].iov_len = SPDK_SOCK_REQUEST_IOV(req, i)->iov_len - offset; 258 iovcnt++; 259 260 offset = 0; 261 262 if (iovcnt >= IOV_BATCH_SIZE) { 263 break; 264 } 265 } 266 if (iovcnt >= IOV_BATCH_SIZE) { 267 break; 268 } 269 270 if (last_req != NULL) { 271 *last_req = req; 272 } 273 req = TAILQ_NEXT(req, internal.link); 274 } 275 276 end: 277 return iovcnt; 278 } 279 280 #ifdef __cplusplus 281 } 282 #endif 283 284 #endif /* SPDK_INTERNAL_SOCK_H */ 285