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 int cb_cnt; 59 spdk_sock_cb cb_fn; 60 void *cb_arg; 61 struct spdk_sock_group_impl *group_impl; 62 TAILQ_ENTRY(spdk_sock) link; 63 64 int max_iovcnt; 65 TAILQ_HEAD(, spdk_sock_request) queued_reqs; 66 TAILQ_HEAD(, spdk_sock_request) pending_reqs; 67 int queued_iovcnt; 68 int placement_id; 69 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 #ifdef __cplusplus 227 } 228 #endif 229 230 #endif /* SPDK_INTERNAL_SOCK_H */ 231