xref: /f-stack/lib/ff_api.h (revision 8d76b62e)
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[14];
45 };
46 
47 #define AF_INET6_LINUX    10
48 #define PF_INET6_LINUX    AF_INET6_LINUX
49 #define AF_INET6_FREEBSD    28
50 #define PF_INET6_FREEBSD    AF_INET6_FREEBSD
51 
52 typedef int (*loop_func_t)(void *arg);
53 
54 int ff_init(int argc, char * const argv[]);
55 
56 void ff_run(loop_func_t loop, void *arg);
57 
58 /* POSIX-LIKE api begin */
59 
60 int ff_fcntl(int fd, int cmd, ...);
61 
62 int ff_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
63     const void *newp, size_t newlen);
64 
65 int ff_ioctl(int fd, unsigned long request, ...);
66 
67 int ff_socket(int domain, int type, int protocol);
68 
69 int ff_setsockopt(int s, int level, int optname, const void *optval,
70     socklen_t optlen);
71 
72 int ff_getsockopt(int s, int level, int optname, void *optval,
73     socklen_t *optlen);
74 
75 int ff_listen(int s, int backlog);
76 int ff_bind(int s, const struct linux_sockaddr *addr, socklen_t addrlen);
77 int ff_accept(int s, struct linux_sockaddr *addr, socklen_t *addrlen);
78 int ff_connect(int s, const struct linux_sockaddr *name, socklen_t namelen);
79 int ff_close(int fd);
80 int ff_shutdown(int s, int how);
81 
82 int ff_getpeername(int s, struct linux_sockaddr *name,
83     socklen_t *namelen);
84 int ff_getsockname(int s, struct linux_sockaddr *name,
85     socklen_t *namelen);
86 
87 ssize_t ff_read(int d, void *buf, size_t nbytes);
88 ssize_t ff_readv(int fd, const struct iovec *iov, int iovcnt);
89 
90 ssize_t ff_write(int fd, const void *buf, size_t nbytes);
91 ssize_t ff_writev(int fd, const struct iovec *iov, int iovcnt);
92 
93 ssize_t ff_send(int s, const void *buf, size_t len, int flags);
94 ssize_t ff_sendto(int s, const void *buf, size_t len, int flags,
95     const struct linux_sockaddr *to, socklen_t tolen);
96 ssize_t ff_sendmsg(int s, const struct msghdr *msg, int flags);
97 
98 ssize_t ff_recv(int s, void *buf, size_t len, int flags);
99 ssize_t ff_recvfrom(int s, void *buf, size_t len, int flags,
100     struct linux_sockaddr *from, socklen_t *fromlen);
101 ssize_t ff_recvmsg(int s, struct msghdr *msg, int flags);
102 
103 int ff_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
104     struct timeval *timeout);
105 
106 int ff_poll(struct pollfd fds[], nfds_t nfds, int timeout);
107 
108 int ff_kqueue(void);
109 int ff_kevent(int kq, const struct kevent *changelist, int nchanges,
110     struct kevent *eventlist, int nevents, const struct timespec *timeout);
111 int ff_kevent_do_each(int kq, const struct kevent *changelist, int nchanges,
112     void *eventlist, int nevents, const struct timespec *timeout,
113     void (*do_each)(void **, struct kevent *));
114 
115 int ff_gettimeofday(struct timeval *tv, struct timezone *tz);
116 
117 int ff_dup(int oldfd);
118 int ff_dup2(int oldfd, int newfd);
119 
120 /* POSIX-LIKE api end */
121 
122 
123 /* Tests if fd is used by F-Stack */
124 extern int ff_fdisused(int fd);
125 
126 extern int ff_getmaxfd(void);
127 
128 /* route api begin */
129 enum FF_ROUTE_CTL {
130     FF_ROUTE_ADD,
131     FF_ROUTE_DEL,
132     FF_ROUTE_CHANGE,
133 };
134 
135 enum FF_ROUTE_FLAG {
136     FF_RTF_HOST,
137     FF_RTF_GATEWAY,
138 };
139 
140 /*
141  * On success, 0 is returned.
142  * On error, -1 is returned, and errno is set appropriately.
143  */
144 int ff_route_ctl(enum FF_ROUTE_CTL req, enum FF_ROUTE_FLAG flag,
145     struct linux_sockaddr *dst, struct linux_sockaddr *gw,
146     struct linux_sockaddr *netmask);
147 
148 /* route api end */
149 
150 
151 /* dispatch api begin */
152 #define FF_DISPATCH_ERROR (-1)
153 #define FF_DISPATCH_RESPONSE (-2)
154 
155 /*
156  * Packet dispatch callback function.
157  * Implemented by user.
158  *
159  * @param data
160  *   The data pointer of this packet.
161  * @param len
162  *   The length of this packet.
163  * @param queue_id
164  *   Current queue of this packet.
165  * @param nb_queues
166  *   Number of queues to be dispatched.
167  *
168  * @return 0 to (nb_queues - 1)
169  *   The queue id that the packet will be dispatched to.
170  * @return FF_DISPATCH_ERROR (-1)
171  *   Error occurs or packet is handled by user, packet will be freed.
172 * @return FF_DISPATCH_RESPONSE (-2)
173  *   Packet is handled by user, packet will be responsed.
174  *
175  */
176 typedef int (*dispatch_func_t)(void *data, uint16_t *len,
177     uint16_t queue_id, uint16_t nb_queues);
178 
179 /* regist a packet dispath function */
180 void ff_regist_packet_dispatcher(dispatch_func_t func);
181 
182 /* dispatch api end */
183 
184 /* pcb lddr api begin */
185 /*
186  * pcb lddr callback function.
187  * Implemented by user.
188  *
189  * @param family
190  *   The remote server addr, should be AF_INET or AF_INET6.
191  * @param dst_addr
192  *   The remote server addr, should be (in_addr *) or (in6_addr *).
193  * @param dst_port
194  *   The remote server port.
195  * @param src_addr
196  *   Return parameter.
197  *   The local addr, should be (in_addr *) or (in6_addr *).
198  *   If set (INADDR_ANY) or (in6addr_any), the app then will
199  *   call `in_pcbladdr()` to get laddr.
200  *
201  * @return error_no
202  *   0 means success.
203  *
204  */
205 typedef int (*pcblddr_func_t)(uint16_t family, void *dst_addr,
206     uint16_t dst_port, void *src_addr);
207 
208 /* regist a pcb lddr function */
209 void ff_regist_pcblddr_fun(pcblddr_func_t func);
210 
211 /* pcb lddr api end */
212 
213 /* internal api begin */
214 
215 /* FreeBSD style calls. Used for tools. */
216 int ff_ioctl_freebsd(int fd, unsigned long request, ...);
217 int ff_setsockopt_freebsd(int s, int level, int optname,
218     const void *optval, socklen_t optlen);
219 int ff_getsockopt_freebsd(int s, int level, int optname,
220     void *optval, socklen_t *optlen);
221 
222 /*
223  * Handle rtctl.
224  * The data is a pointer to struct rt_msghdr.
225  */
226 int ff_rtioctl(int fib, void *data, unsigned *plen, unsigned maxlen);
227 
228 /*
229  * Handle ngctl.
230  */
231 enum FF_NGCTL_CMD {
232     NGCTL_SOCKET,
233     NGCTL_BIND,
234     NGCTL_CONNECT,
235     NGCTL_SEND,
236     NGCTL_RECV,
237     NGCTL_CLOSE,
238 };
239 
240 int ff_ngctl(int cmd, void *data);
241 
242 /* internal api end */
243 
244 #ifdef __cplusplus
245 }
246 #endif
247 #endif
248 
249