xref: /spdk/include/spdk_internal/sock.h (revision 913f780e1077efada6674c7a14b687ba5ac1b6d0)
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 
51 struct spdk_sock {
52 	struct spdk_net_impl		*net_impl;
53 	spdk_sock_cb			cb_fn;
54 	void				*cb_arg;
55 	struct spdk_sock_group_impl	*group_impl;
56 	TAILQ_ENTRY(spdk_sock)		link;
57 };
58 
59 struct spdk_sock_group {
60 	STAILQ_HEAD(, spdk_sock_group_impl)	group_impls;
61 	void					*ctx;
62 };
63 
64 struct spdk_sock_group_impl {
65 	struct spdk_net_impl			*net_impl;
66 	TAILQ_HEAD(, spdk_sock)			socks;
67 	STAILQ_ENTRY(spdk_sock_group_impl)	link;
68 };
69 
70 struct spdk_net_impl {
71 	const char *name;
72 
73 	int (*getaddr)(struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport, char *caddr,
74 		       int clen, uint16_t *cport);
75 	struct spdk_sock *(*connect)(const char *ip, int port);
76 	struct spdk_sock *(*listen)(const char *ip, int port);
77 	struct spdk_sock *(*accept)(struct spdk_sock *sock);
78 	int (*close)(struct spdk_sock *sock);
79 	ssize_t (*recv)(struct spdk_sock *sock, void *buf, size_t len);
80 	ssize_t (*readv)(struct spdk_sock *sock, struct iovec *iov, int iovcnt);
81 	ssize_t (*writev)(struct spdk_sock *sock, struct iovec *iov, int iovcnt);
82 
83 	int (*set_recvlowat)(struct spdk_sock *sock, int nbytes);
84 	int (*set_recvbuf)(struct spdk_sock *sock, int sz);
85 	int (*set_sendbuf)(struct spdk_sock *sock, int sz);
86 	int (*set_priority)(struct spdk_sock *sock, int priority);
87 
88 	bool (*is_ipv6)(struct spdk_sock *sock);
89 	bool (*is_ipv4)(struct spdk_sock *sock);
90 	bool (*is_connected)(struct spdk_sock *sock);
91 
92 	int (*get_placement_id)(struct spdk_sock *sock, int *placement_id);
93 	struct spdk_sock_group_impl *(*group_impl_create)(void);
94 	int (*group_impl_add_sock)(struct spdk_sock_group_impl *group, struct spdk_sock *sock);
95 	int (*group_impl_remove_sock)(struct spdk_sock_group_impl *group, struct spdk_sock *sock);
96 	int (*group_impl_poll)(struct spdk_sock_group_impl *group, int max_events,
97 			       struct spdk_sock **socks);
98 	int (*group_impl_close)(struct spdk_sock_group_impl *group);
99 
100 	STAILQ_ENTRY(spdk_net_impl) link;
101 };
102 
103 void spdk_net_impl_register(struct spdk_net_impl *impl);
104 
105 #define SPDK_NET_IMPL_REGISTER(name, impl) \
106 static void __attribute__((constructor)) net_impl_register_##name(void) \
107 { \
108 	spdk_net_impl_register(impl); \
109 }
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* SPDK_INTERNAL_SOCK_H */
116