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