1*a9643ea8Slogwang# F-Stack API Reference 2*a9643ea8Slogwang 3*a9643ea8SlogwangF-Stack is a high performance network framework based on DPDK. 4*a9643ea8Slogwang 5*a9643ea8SlogwangFF API provides standard Kqueue/Epoll interface, and a micro threading framework (SPP). 6*a9643ea8Slogwang 7*a9643ea8SlogwangIn order to facilitate a variety of services can use F-Stack simpler and faster, F-Stack has integrated Nginx and Redis。 8*a9643ea8Slogwang 9*a9643ea8SlogwangSee 《F-Stack\_Nginx\_APP\_Guide》, 《F-Stack\_Reis\_APP\_Guide》, 《F-Stack\_Microthread\_APP\_Guide》 10*a9643ea8Slogwang 11*a9643ea8Slogwang## FF API 12*a9643ea8Slogwang 13*a9643ea8Slogwang The header file ff_api.h defines the following API, which should be used to replace the system called when using the F-Sstack. 14*a9643ea8Slogwang 15*a9643ea8Slogwang### Initialize 16*a9643ea8Slogwang 17*a9643ea8Slogwang#### ff_init 18*a9643ea8Slogwang 19*a9643ea8Slogwang int ff_init(const char *conf, int argc, char * const argv[]); 20*a9643ea8Slogwang conf:Profile path 21*a9643ea8Slogwang argv:-c <coremask>,the coremask parameters can cover the coremask in configuration file 22*a9643ea8Slogwang 23*a9643ea8Slogwang Initialize F-Stack,include DPDK/FreeBSD network stack, etc. 24*a9643ea8Slogwang 25*a9643ea8Slogwang#### ff_run 26*a9643ea8Slogwang 27*a9643ea8Slogwang void ff_run(loop_func_t loop, void *arg); 28*a9643ea8Slogwangloop is a callbask function,the service logic is implemented by the user, and be called by each poll of F-Stack . 29*a9643ea8Slogwang 30*a9643ea8Slogwang### Control API 31*a9643ea8Slogwang 32*a9643ea8Slogwang#### ff_fcntl 33*a9643ea8Slogwang 34*a9643ea8Slogwang int ff_fcntl(int fd, int cmd, ...); 35*a9643ea8Slogwang 36*a9643ea8Slogwang fcntl() performs one of the operations described below on the open file descriptor fd. The operation is determined by cmd. 37*a9643ea8Slogwangmore info see man fcntl 38*a9643ea8Slogwang 39*a9643ea8Slogwang#### ff_sysctl 40*a9643ea8Slogwang 41*a9643ea8Slogwang int ff_sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp, 42*a9643ea8Slogwang const void *newp, size_t newlen); 43*a9643ea8Slogwang 44*a9643ea8Slogwang ff_sysctl is used to modify kernel parameters at runtime. 45*a9643ea8SlogwangHowever, it is currently only supported before F-Stack is started. 46*a9643ea8Slogwang 47*a9643ea8Slogwang#### ff_ioctl 48*a9643ea8Slogwang 49*a9643ea8Slogwang int ff_ioctl(int fd, unsigned long request, ...); 50*a9643ea8Slogwang 51*a9643ea8Slogwang The ioctl() function manipulates the underlying device parameters of special files. 52*a9643ea8Slogwang more info see man ioctl 53*a9643ea8Slogwang 54*a9643ea8Slogwang### Network API 55*a9643ea8Slogwang 56*a9643ea8Slogwang#### ff_socket 57*a9643ea8Slogwang 58*a9643ea8Slogwang int ff_socket(int domain, int type, int protocol); 59*a9643ea8Slogwang 60*a9643ea8Slogwang creates an endpoint for communication and returns a file descriptor that refers to that endpoint. 61*a9643ea8Slogwang more info see man socket 62*a9643ea8Slogwang 63*a9643ea8Slogwang#### ff_setsockopt & ff_getsockopt 64*a9643ea8Slogwang 65*a9643ea8Slogwang int ff_getsockopt(int s, int level, int optname, void *optval, 66*a9643ea8Slogwang socklen_t *optlen); 67*a9643ea8Slogwang int ff_setsockopt(int s, int level, int optname, const void *optval, 68*a9643ea8Slogwang socklen_t optlen); 69*a9643ea8Slogwang 70*a9643ea8Slogwang getsockopt() and setsockopt() manipulate options for the socket referred to by the file descriptor sockfd. 71*a9643ea8Slogwang more info see man getsockopt and man setsockopt. 72*a9643ea8Slogwang 73*a9643ea8Slogwang#### ff_socketpair 74*a9643ea8Slogwang 75*a9643ea8Slogwang int ff_socketpair(int domain, int type, int protocol, int *sv); 76*a9643ea8Slogwang 77*a9643ea8Slogwang The socketpair() call creates an unnamed pair of connected sockets in the specified domain, of the specified type, and using the optionally specified protocol. 78*a9643ea8Slogwang more info see man socketpair 79*a9643ea8Slogwang 80*a9643ea8Slogwang#### Socket operation function 81*a9643ea8Slogwang 82*a9643ea8Slogwang int ff_listen(int s, int backlog); 83*a9643ea8Slogwang int ff_bind(int s, const struct linux_sockaddr *addr, socklen_t addrlen); 84*a9643ea8Slogwang int ff_accept(int s, struct linux_sockaddr *addr, socklen_t *addrlen); 85*a9643ea8Slogwang int ff_connect(int s, const struct linux_sockaddr *name, socklen_t namelen); 86*a9643ea8Slogwang int ff_close(int fd); 87*a9643ea8Slogwang int ff_shutdown(int s, int how); 88*a9643ea8Slogwang 89*a9643ea8Slogwang Socket operation function, more info see Linux Programmer's Manual. 90*a9643ea8Slogwang 91*a9643ea8Slogwang#### ff_getpeername 92*a9643ea8Slogwang 93*a9643ea8Slogwang int ff_getpeername(int s, struct linux_sockaddr *name, socklen_t *namelen); 94*a9643ea8Slogwang 95*a9643ea8Slogwang ff_getpeername() returns the address of the peer connected to the socket sockfd, in the buffer pointed to by addr. 96*a9643ea8Slogwang more info see man getpeername. 97*a9643ea8Slogwang 98*a9643ea8Slogwang#### ff_getsockname 99*a9643ea8Slogwang 100*a9643ea8Slogwang int ff_getsockname(int s, struct linux_sockaddr *name, 101*a9643ea8Slogwang socklen_t *namelen); 102*a9643ea8Slogwang 103*a9643ea8Slogwang ff_getsockname() returns the current address to which the socket sockfd is bound, in the buffer pointed to by addr. 104*a9643ea8Slogwang more info see man getsockname. 105*a9643ea8Slogwang 106*a9643ea8Slogwang#### ff\_read & ff\_readv 107*a9643ea8Slogwang 108*a9643ea8Slogwang ssize_t ff_read(int d, void *buf, size_t nbytes); 109*a9643ea8Slogwang ssize_t ff_readv(int fd, const struct iovec *iov, int iovcnt); 110*a9643ea8Slogwang 111*a9643ea8Slogwang read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. 112*a9643ea8Slogwang more info see man read and man readv. 113*a9643ea8Slogwang 114*a9643ea8Slogwang#### ff\_write & ff\_writev 115*a9643ea8Slogwang 116*a9643ea8Slogwang ssize_t ff_write(int fd, const void *buf, size_t nbytes); 117*a9643ea8Slogwang ssize_t ff_writev(int fd, const struct iovec *iov, int iovcnt); 118*a9643ea8Slogwang 119*a9643ea8Slogwang write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. 120*a9643ea8Slogwang more info see man write and man readv. 121*a9643ea8Slogwang 122*a9643ea8Slogwang#### ff\_send & ff\_sendto & ff\_sendmsg 123*a9643ea8Slogwang 124*a9643ea8Slogwang ssize_t ff_send(int s, const void *buf, size_t len, int flags); 125*a9643ea8Slogwang ssize_t ff_sendto(int s, const void *buf, size_t len, int flags, const struct linux_sockaddr *to, socklen_t tolen); 126*a9643ea8Slogwang ssize_t ff_sendmsg(int s, const struct msghdr *msg, int flags); 127*a9643ea8Slogwang 128*a9643ea8Slogwang send a message on a socket. 129*a9643ea8Slogwang more info see man send. 130*a9643ea8Slogwang 131*a9643ea8Slogwang#### ff\_recv & ff\_recvfrom & ff\_recvmsg 132*a9643ea8Slogwang 133*a9643ea8Slogwang ssize_t ff_recv(int s, void *buf, size_t len, int flags); 134*a9643ea8Slogwang ssize_t ff_recvfrom(int s, void *buf, size_t len, int flags, struct linux_sockaddr *from, socklen_t *fromlen); 135*a9643ea8Slogwang ssize_t ff_recvmsg(int s, struct msghdr *msg, int flags); 136*a9643ea8Slogwang 137*a9643ea8Slogwang receive a message from a socket. 138*a9643ea8Slogwang more info see man recv. 139*a9643ea8Slogwang 140*a9643ea8Slogwang#### ff_select 141*a9643ea8Slogwang 142*a9643ea8Slogwang int ff_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); 143*a9643ea8Slogwang 144*a9643ea8Slogwang select() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). 145*a9643ea8Slogwang more info see man select. 146*a9643ea8Slogwang 147*a9643ea8Slogwang#### ff_poll 148*a9643ea8Slogwang 149*a9643ea8Slogwang int ff_poll(struct pollfd fds[], nfds_t nfds, int timeout); 150*a9643ea8Slogwang 151*a9643ea8Slogwang wait for some event on a file descriptor. 152*a9643ea8Slogwang more info see man poll. 153*a9643ea8Slogwang 154*a9643ea8Slogwang### Kqueue API 155*a9643ea8Slogwang 156*a9643ea8Slogwang#### ff_kqueue 157*a9643ea8Slogwang 158*a9643ea8Slogwang int ff_kqueue(void); 159*a9643ea8Slogwang int ff_kevent(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents, const struct timespec *timeout); 160*a9643ea8Slogwang 161*a9643ea8Slogwang The kqueue() system call provides a generic method of notifying the user when an event happens or a condition holds, based on the results of small pieces of kernel code termed filters. 162*a9643ea8Slogwang more info see man kqueue on FreeBSD System Calls Manual. 163*a9643ea8Slogwang 164*a9643ea8Slogwang### Epoll API 165*a9643ea8Slogwang 166*a9643ea8Slogwang#### ff\_epoll\_create 167*a9643ea8Slogwang 168*a9643ea8Slogwang int ff_epoll_create(int size); 169*a9643ea8Slogwang 170*a9643ea8Slogwang epoll_create() returns a file descriptor referring to the new epoll instance. 171*a9643ea8Slogwang more info see man epoll_create. 172*a9643ea8Slogwang 173*a9643ea8Slogwang#### ff\_epoll\_ctl 174*a9643ea8Slogwang 175*a9643ea8Slogwang int ff_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); 176*a9643ea8Slogwang 177*a9643ea8Slogwang This system call performs control operations on the epoll(7) instance referred to by the file descriptor epfd. 178*a9643ea8Slogwang more info see man epoll_ctl. 179*a9643ea8Slogwang 180*a9643ea8Slogwang### Micro Thread API `micro_thread/mt_api.h` 181*a9643ea8Slogwang 182*a9643ea8Slogwang In order to develop asynchronous program convenient without complex asynchronous logic processing, reference [SPP's micro thread framework](https://github.com/Tencent/MSEC/tree/master/spp_rpc) F-Stack provides a micro thread framework, synchronous programming can be achieved using the asynchronous call. 183*a9643ea8Slogwang 184*a9643ea8Slogwang#### UDP send/recv interface 185*a9643ea8Slogwang 186*a9643ea8Slogwang int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout); 187*a9643ea8Slogwang 188*a9643ea8Slogwang Use Random socket port to send and recv udp packet. 189*a9643ea8Slogwang 190*a9643ea8Slogwang 191*a9643ea8Slogwang#### tcp send/recv interface 192*a9643ea8Slogwang 193*a9643ea8Slogwang int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen chek_func); 194*a9643ea8Slogwang 195*a9643ea8Slogwang Use connection pool to send and recv tcp packet, keep-alive default are 10 mintues. The parameter of buf can't use `static`. 196*a9643ea8Slogwang 197*a9643ea8Slogwang 198*a9643ea8Slogwang enum MT\_TCP\_CONN\_TYPE 199*a9643ea8Slogwang { 200*a9643ea8Slogwang MT\_TCP\_SHORT = 1, 201*a9643ea8Slogwang MT\_TCP\_LONG = 2, 202*a9643ea8Slogwang MT\_TCP\_SHORT\_SNDONLY = 3, 203*a9643ea8Slogwang MT\_TCP\_LONG\_SNDONLY = 4, 204*a9643ea8Slogwang MT\_TCP\_BUTT 205*a9643ea8Slogwang }; 206*a9643ea8Slogwang 207*a9643ea8Slogwang int mt\_tcpsendrcv\_ex(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int* buf_size, int timeout, MtFuncTcpMsgLen func, MT_TCP_CONN_TYPE type = MT_TCP_LONG); 208*a9643ea8Slogwang 209*a9643ea8Slogwang Tcp send and recv interface, you can choose if the connection is keep-alive or close.The parameter of buf can't use `static`. 210*a9643ea8Slogwang 211*a9643ea8Slogwang 212*a9643ea8Slogwang int mt\_tcpsendrcv\_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL, MT_TCP_CONN_TYPE type = MT_TCP_LONG, bool keep_rcv_buf=false); 213*a9643ea8Slogwang 214*a9643ea8Slogwang Tcp send and recv interface, you can choose if the connection is keep-alive or close.The parameter of buf can't use `static`. 215*a9643ea8Slogwang 216*a9643ea8Slogwang 217*a9643ea8Slogwang int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL, bool keep_rcv_buf=false); 218*a9643ea8Slogwang 219*a9643ea8Slogwang 220*a9643ea8Slogwang Use connection pool to send and recv tcp packet, keep-alive default are 10 mintues. The parameter of buf can't use `static`. 221*a9643ea8Slogwang 222*a9643ea8Slogwang#### Socet API of micro thread 223*a9643ea8Slogwang 224*a9643ea8Slogwang see `micro_thread/mt_api.h`. 225