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