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