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