xref: /f-stack/lib/ff_api.h (revision 3f18f1a2)
1 /*
2  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #ifndef _FSTACK_API_H
28 #define _FSTACK_API_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 #include <sys/socket.h>
34 #include <sys/select.h>
35 #include <sys/poll.h>
36 #include <netinet/in.h>
37 #include <sys/time.h>
38 
39 #include "ff_event.h"
40 #include "ff_errno.h"
41 
42 struct linux_sockaddr {
43     short sa_family;
44     char sa_data[126];
45 };
46 
47 /* AF_INET6/PF_INET6 is 10 in linux
48  * defined 28 for FreeBSD
49  */
50 #ifdef AF_INET6
51 #undef AF_INET6
52 #endif
53 #ifdef PF_INET6
54 #undef PF_INET6
55 #endif
56 #define AF_INET6    28
57 #define PF_INET6    AF_INET6
58 #define AF_INET6_LINUX    10
59 #define PF_INET6_LINUX    AF_INET6_LINUX
60 
61 typedef int (*loop_func_t)(void *arg);
62 
63 int ff_init(int argc, char * const argv[]);
64 
65 void ff_run(loop_func_t loop, void *arg);
66 
67 /* POSIX-LIKE api begin */
68 
69 int ff_fcntl(int fd, int cmd, ...);
70 
71 int ff_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
72     const void *newp, size_t newlen);
73 
74 int ff_ioctl(int fd, unsigned long request, ...);
75 
76 int ff_socket(int domain, int type, int protocol);
77 
78 int ff_setsockopt(int s, int level, int optname, const void *optval,
79     socklen_t optlen);
80 
81 int ff_getsockopt(int s, int level, int optname, void *optval,
82     socklen_t *optlen);
83 
84 int ff_listen(int s, int backlog);
85 int ff_bind(int s, const struct linux_sockaddr *addr, socklen_t addrlen);
86 int ff_accept(int s, struct linux_sockaddr *addr, socklen_t *addrlen);
87 int ff_connect(int s, const struct linux_sockaddr *name, socklen_t namelen);
88 int ff_close(int fd);
89 int ff_shutdown(int s, int how);
90 
91 int ff_getpeername(int s, struct linux_sockaddr *name,
92     socklen_t *namelen);
93 int ff_getsockname(int s, struct linux_sockaddr *name,
94     socklen_t *namelen);
95 
96 ssize_t ff_read(int d, void *buf, size_t nbytes);
97 ssize_t ff_readv(int fd, const struct iovec *iov, int iovcnt);
98 
99 ssize_t ff_write(int fd, const void *buf, size_t nbytes);
100 ssize_t ff_writev(int fd, const struct iovec *iov, int iovcnt);
101 
102 ssize_t ff_send(int s, const void *buf, size_t len, int flags);
103 ssize_t ff_sendto(int s, const void *buf, size_t len, int flags,
104     const struct linux_sockaddr *to, socklen_t tolen);
105 ssize_t ff_sendmsg(int s, const struct msghdr *msg, int flags);
106 
107 ssize_t ff_recv(int s, void *buf, size_t len, int flags);
108 ssize_t ff_recvfrom(int s, void *buf, size_t len, int flags,
109     struct linux_sockaddr *from, socklen_t *fromlen);
110 ssize_t ff_recvmsg(int s, struct msghdr *msg, int flags);
111 
112 int ff_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
113     struct timeval *timeout);
114 
115 int ff_poll(struct pollfd fds[], nfds_t nfds, int timeout);
116 
117 int ff_kqueue(void);
118 int ff_kevent(int kq, const struct kevent *changelist, int nchanges,
119     struct kevent *eventlist, int nevents, const struct timespec *timeout);
120 int ff_kevent_do_each(int kq, const struct kevent *changelist, int nchanges,
121     void *eventlist, int nevents, const struct timespec *timeout,
122     void (*do_each)(void **, struct kevent *));
123 
124 int ff_gettimeofday(struct timeval *tv, struct timezone *tz);
125 
126 int ff_dup(int oldfd);
127 int ff_dup2(int oldfd, int newfd);
128 
129 /* POSIX-LIKE api end */
130 
131 
132 /* Tests if fd is used by F-Stack */
133 extern int ff_fdisused(int fd);
134 
135 extern int ff_getmaxfd(void);
136 
137 /* route api begin */
138 enum FF_ROUTE_CTL {
139     FF_ROUTE_ADD,
140     FF_ROUTE_DEL,
141     FF_ROUTE_CHANGE,
142 };
143 
144 enum FF_ROUTE_FLAG {
145     FF_RTF_HOST,
146     FF_RTF_GATEWAY,
147 };
148 
149 /*
150  * On success, 0 is returned.
151  * On error, -1 is returned, and errno is set appropriately.
152  */
153 int ff_route_ctl(enum FF_ROUTE_CTL req, enum FF_ROUTE_FLAG flag,
154     struct linux_sockaddr *dst, struct linux_sockaddr *gw,
155     struct linux_sockaddr *netmask);
156 
157 /* route api end */
158 
159 
160 /* dispatch api begin */
161 #define FF_DISPATCH_ERROR (-1)
162 #define FF_DISPATCH_RESPONSE (-2)
163 
164 /*
165  * Packet dispatch callback function.
166  * Implemented by user.
167  *
168  * @param data
169  *   The data pointer of this packet.
170  * @param len
171  *   The length of this packet.
172  * @param queue_id
173  *   Current queue of this packet.
174  * @param nb_queues
175  *   Number of queues to be dispatched.
176  *
177  * @return 0 to (nb_queues - 1)
178  *   The queue id that the packet will be dispatched to.
179  * @return FF_DISPATCH_ERROR (-1)
180  *   Error occurs or packet is handled by user, packet will be freed.
181 * @return FF_DISPATCH_RESPONSE (-2)
182  *   Packet is handled by user, packet will be responsed.
183  *
184  */
185 typedef int (*dispatch_func_t)(void *data, uint16_t *len,
186     uint16_t queue_id, uint16_t nb_queues);
187 
188 /* regist a packet dispath function */
189 void ff_regist_packet_dispatcher(dispatch_func_t func);
190 
191 /* dispatch api end */
192 
193 
194 /* internal api begin */
195 
196 /* FreeBSD style calls. Used for tools. */
197 int ff_ioctl_freebsd(int fd, unsigned long request, ...);
198 int ff_setsockopt_freebsd(int s, int level, int optname,
199     const void *optval, socklen_t optlen);
200 int ff_getsockopt_freebsd(int s, int level, int optname,
201     void *optval, socklen_t *optlen);
202 
203 /*
204  * Handle rtctl.
205  * The data is a pointer to struct rt_msghdr.
206  */
207 int ff_rtioctl(int fib, void *data, unsigned *plen, unsigned maxlen);
208 
209 /*
210  * Handle ngctl.
211  */
212 enum FF_NGCTL_CMD {
213     NGCTL_SOCKET,
214     NGCTL_BIND,
215     NGCTL_CONNECT,
216     NGCTL_SEND,
217     NGCTL_RECV,
218     NGCTL_CLOSE,
219 };
220 
221 int ff_ngctl(int cmd, void *data);
222 
223 /* internal api end */
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 #endif
229 
230