1d181a8f5Slogwang[](https://travis-ci.org/F-Stack/f-stack) 2d181a8f5Slogwang 34ada8c2dSlogwang# F-Stack 4a9643ea8Slogwang 5a9643ea8Slogwang 6a9643ea8Slogwang## Introduction 71a527102Slogwang 89ece0b02Sjbwyatt4With the rapid development of Network Interface Cards the poor performance of data packet processing with the Linux kernel has become the bottleneck in modern network systems. Yet, the increasing demands of the Internet's growth demand a higher performant network processing solution. Kernel bypass has emerged to catch more and more attention. There are various similar technologies such as: DPDK, NETMAP and PF_RING. The main idea of kernel bypass is that Linux is only used to deal with control flow; all data streams are processed in user space. Therefore, kernel bypass can avoid performance bottlenecks caused by kernel packet copying, thread scheduling, system calls, and interrupts. Furthermore, kernel bypass can achieve higher performance with multi-optimizing methods. Within various techniques, DPDK has been widely used because of it's more thorough isolation from kernel scheduling and active community support. 9a9643ea8Slogwang 109ece0b02Sjbwyatt4[F-Stack](http://www.f-stack.org/?from=github) is an open source high performant network framework based on DPDK with the following characteristics: 11a9643ea8Slogwang 129ece0b02Sjbwyatt41. Ultra high network performance which the network card can achieve under full load: 10 million concurrent connections, 5 million RPS, 1 million CPS. 139ece0b02Sjbwyatt42. Transplant FreeBSD 11.01 user space stack, which provides a complete stack function, and cut a great amount of irrelevant features. This greatly enhances network performance. 149ece0b02Sjbwyatt43. Support Nginx, Redis, and other mature applications. Services can easily use F-Stack. 159ece0b02Sjbwyatt44. Easy to extend with multi-process architecture. 169ece0b02Sjbwyatt45. Provides micro thread interface. Various applications with stateful applications can easily use F-Stack to get high performance without processing complex asynchronous logic. 179ece0b02Sjbwyatt46. Provide an Epoll/Kqueue interface that allow many kinds of applications to easily use F-Stack. 18a9643ea8Slogwang 19a9643ea8Slogwang## History 20a9643ea8Slogwang 219ece0b02Sjbwyatt4To deal with the increasingly severe DDoS attacks the authorized DNS server of Tencent Cloud DNSPod switched from Gigabit Ethernet to 10-Gigabit at the end of 2012. We faced several options: one is to continue to use the original network stack in the Linux kernel, another is to use kernel bypass techniques. After several rounds of investigation; we finally chose to develop our next generation of DNS server based on DPDK. The reason is DPDK provides ultra-high performance and can be seamlessly extended to 40G, or even 100G NIC, in the future. 22a9643ea8Slogwang 23a9643ea8SlogwangAfter several months of development and testing, DKDNS, high-performance DNS server based on DPDK officially released in October 2013. It's capable of achieving up to 11 million QPS with a single 10GE port and 18.2 million QPS with two 10GE ports. And then we developed a user-space TCP/IP stack called F-Stack that can process 0.6 million RPS with a single 10GE port. 24a9643ea8Slogwang 25eb6b6fa6Sjbwyatt4With the fast growth of Tencent Cloud more and more of our services needed higher network access performance. Meanwhile, F-Stack was continuing to improve, being driven by our business growth, and, ultimately developed into a general network access framework. But our initial TCP/IP stack couldn't meet the needs of these services. Continuing to develop and maintain a complete high performance network stack would have been too expensive. After evaluating several plans; we finally determined to port FreeBSD's (11.0 stable) TCP/IP stack into F-Stack. Not only does this allow us to stop reinventing the wheel, we can take advantage of the the improvements the FreeBSD community will bring in the future. Thanks to [libplebnet](https://gitorious.org/freebsd/kmm-sandbox/commit/fa8a11970bc0ed092692736f175925766bebf6af?p=freebsd:kmm-sandbox.git;a=tree;f=lib/libplebnet;h=ae446dba0b4f8593b69b339ea667e12d5b709cfb;hb=refs/heads/work/svn_trunk_libplebnet) and [libuinet](https://github.com/pkelsey/libuinet) this work became a lot easier. 26a9643ea8Slogwang 27aa833a4aSjbwyatt4With the rapid development of all kinds of applications, in order to help different APPs quick and easily use F-Stack, F-Stack has integrated Nginx, Redis and other commonly used APPs, and a micro thread framework, and provides a standard Epoll/Kqueue interface. 28a9643ea8Slogwang 29a9643ea8SlogwangCurrently, besides authorized DNS server of DNSPod, there are various products in Tencent Cloud has used the F-Stack, such as HttpDNS (D+), COS access module, CDN access module, etc.. 30a9643ea8Slogwang 31a9643ea8Slogwang## Quick Start 32a9643ea8Slogwang 33a9643ea8Slogwang # clone F-Stack 34ced31bf9Syumm007 mkdir -p /data/f-stack 35a9643ea8Slogwang git clone https://github.com/F-Stack/f-stack.git /data/f-stack 36a9643ea8Slogwang 37a964296aSsoroshsabz # Install libnuma-dev 382bfe3f2eSlogwang yum install numactl-devel # on Centos 392bfe3f2eSlogwang #sudo apt-get install libnuma-dev # on Ubuntu 402bfe3f2eSlogwang 41a9643ea8Slogwang cd f-stack 42a964296aSsoroshsabz # Compile DPDK 438d76b62eSfengbojiang cd dpdk/ 44*6b8a3e40Sjfb8856606 meson -Denable_kmods=true build 458d76b62eSfengbojiang ninja -C build 468d76b62eSfengbojiang ninja -C build install 47a9643ea8Slogwang 48a9643ea8Slogwang # Set hugepage 49a9643ea8Slogwang # single-node system 50a9643ea8Slogwang echo 1024 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages 51a9643ea8Slogwang 52a9643ea8Slogwang # or NUMA 53a9643ea8Slogwang echo 1024 > /sys/devices/system/node/node0/hugepages/hugepages-2048kB/nr_hugepages 54a9643ea8Slogwang echo 1024 > /sys/devices/system/node/node1/hugepages/hugepages-2048kB/nr_hugepages 55a9643ea8Slogwang 56a9643ea8Slogwang # Using Hugepage with the DPDK 57a9643ea8Slogwang mkdir /mnt/huge 58a9643ea8Slogwang mount -t hugetlbfs nodev /mnt/huge 59a9643ea8Slogwang 60a964296aSsoroshsabz # Close ASLR; it is necessary in multiple process 61627097dcSlogwang echo 0 > /proc/sys/kernel/randomize_va_space 62627097dcSlogwang 63a964296aSsoroshsabz # Install python for running DPDK python scripts 64a964296aSsoroshsabz sudo apt install python # On ubuntu 65a964296aSsoroshsabz 66a964296aSsoroshsabz # Offload NIC 67a9643ea8Slogwang modprobe uio 68*6b8a3e40Sjfb8856606 insmod /data/f-stack/dpdk/build/kernel/linux/igb_uio/igb_uio.ko 69*6b8a3e40Sjfb8856606 insmod /data/f-stack/dpdk/build/kernel/linux/kni/rte_kni.ko carrier=on # carrier=on is necessary, otherwise need to be up `veth0` via `echo 1 > /sys/class/net/veth0/carrier` 70a9643ea8Slogwang python dpdk-devbind.py --status 71a9643ea8Slogwang ifconfig eth0 down 72a9643ea8Slogwang python dpdk-devbind.py --bind=igb_uio eth0 # assuming that use 10GE NIC and eth0 73a9643ea8Slogwang 7482aa761eSwhl739 # On Ubuntu, use gawk instead of the default mawk. 7582aa761eSwhl739 #sudo apt-get install gawk # or execute `sudo update-alternatives --config awk` to choose gawk. 7682aa761eSwhl739 77a964296aSsoroshsabz # Install dependencies for F-Stack 78a964296aSsoroshsabz sudo apt install gcc make libssl-dev # On ubuntu 79a964296aSsoroshsabz 808d76b62eSfengbojiang # Upgrade pkg-config while version < 0.28 818d76b62eSfengbojiang #cd /data 828d76b62eSfengbojiang #wget https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz 838d76b62eSfengbojiang #tar xzvf pkg-config-0.29.2.tar.gz 848d76b62eSfengbojiang #cd pkg-config-0.29.2 858d76b62eSfengbojiang #./configure --with-internal-glib 868d76b62eSfengbojiang #make 878d76b62eSfengbojiang #make install 888d76b62eSfengbojiang #mv /usr/bin/pkg-config /usr/bin/pkg-config.bak 898d76b62eSfengbojiang #ln -s /usr/local/bin/pkg-config /usr/bin/pkg-config 908d76b62eSfengbojiang 91a9643ea8Slogwang # Compile F-Stack 92a9643ea8Slogwang export FF_PATH=/data/f-stack 938d76b62eSfengbojiang export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/local/lib64/pkgconfig:/usr/lib/pkgconfig 948d76b62eSfengbojiang cd /data/f-stack/lib/ 953377ed76Slogwang make 96a9643ea8Slogwang 97a964296aSsoroshsabz # Install F-STACK 985af66259Sjfb8856606 # libfstack.a will be installed to /usr/local/lib 995af66259Sjfb8856606 # ff_*.h will be installed to /usr/local/include 1005af66259Sjfb8856606 # start.sh will be installed to /usr/local/bin/ff_start 1015af66259Sjfb8856606 # config.ini will be installed to /etc/f-stack.conf 1025af66259Sjfb8856606 make install 1035af66259Sjfb8856606 104a9643ea8Slogwang#### Nginx 105a9643ea8Slogwang 10628e10b57Sfengbojiang(姜凤波) cd app/nginx-1.16.1 10782aa761eSwhl739 bash ./configure --prefix=/usr/local/nginx_fstack --with-ff_module 108a9643ea8Slogwang make 109a9643ea8Slogwang make install 110a9643ea8Slogwang cd ../.. 11140600211Slogwang /usr/local/nginx_fstack/sbin/nginx 11240600211Slogwang 11340600211Slogwangfor more details, see [nginx guide](https://github.com/F-Stack/f-stack/blob/master/doc/F-Stack_Nginx_APP_Guide.md). 114a9643ea8Slogwang 115a9643ea8Slogwang#### Redis 116a9643ea8Slogwang 11714cef497Sfengbojiang cd app/redis-5.0.5/ 118a9643ea8Slogwang make 119a9643ea8Slogwang make install 120a9643ea8Slogwang 1218e26089aSjohnjiang If KNI is enabled in the configuration file, you should create a virtual NIC after F-Stack started, and set the ipaddr, netmask, mac addr, route table, etc. These addrs must be same with F-Stack. 1228e26089aSjohnjiang 1238e26089aSjohnjiang If you don't have another management port, you should execute a script like this. 1248e26089aSjohnjiang 125d181a8f5Slogwang /usr/local/nginx_fstack/sbin/nginx 12687255e88Sjohnjiang sleep 10 12787255e88Sjohnjiang ifconfig veth0 <ipaddr> netmask <netmask> broadcast <broadcast> hw ether <mac addr> 12887255e88Sjohnjiang route add -net 0.0.0.0 gw <gateway> dev veth0 1293f18f1a2Sroot echo 1 > /sys/class/net/veth0/carrier # if `carrier=on` not set while `insmod rte_kni.ko` 13087255e88Sjohnjiang # route add -net ... # other route rules 131a9643ea8Slogwang 132a9643ea8Slogwang## Nginx Testing Result 133a9643ea8Slogwang 134a9643ea8SlogwangTest environment 135a9643ea8Slogwang 136a9643ea8Slogwang NIC:Intel Corporation Ethernet Controller XL710 for 40GbE QSFP+ 1377544d3afSfengbojiang(姜凤波) CPU:Intel(R) Xeon(R) CPU E5-2670 v3 @ 2.30GHz(NUMA) 138a9643ea8Slogwang Memory:128G 139a9643ea8Slogwang OS:CentOS Linux release 7.2 (Final) 140a9643ea8Slogwang Kernel:3.10.104-1-tlinux2-0041.tl2 141a9643ea8Slogwang 142a9643ea8SlogwangNginx uses linux kernel's default config, all soft interrupts are working in the first CPU core. 143a9643ea8Slogwang 144a9643ea8SlogwangNginx si means modify the smp_affinity of every IRQ, so that the decision to service an interrupt with a particular CPU is made at the hardware level, with no intervention from the kernel. 145a9643ea8Slogwang 146dc5b2a9eSfengbojiangNginx Reuseport means enable "reuseport" in `nginx.conf`. 147dc5b2a9eSfengbojiang 14822efc758SjohnjiangNginx_FStack's 600 cache bytes' body was returned directly in nginx.conf. 14922efc758Sjohnjiang 1507544d3afSfengbojiang(姜凤波)All of these test cases use CPUs' physical cores. 151a9643ea8Slogwang 15222efc758Sjohnjiang 153a9643ea8SlogwangCPS (Connection:close, Small data packet) test result 154dc5b2a9eSfengbojiang 155dc5b2a9eSfengbojiang 156dc5b2a9eSfengbojiangCPS_Reuseport (Connection:close, Small data packet) test result, This test case runs in a different test environment 157dc5b2a9eSfengbojiang 158a9643ea8Slogwang 159a9643ea8SlogwangRPS (Connection:Keep-Alive, Small data packet) test data 160dc5b2a9eSfengbojiang 161a9643ea8Slogwang 162a9643ea8SlogwangBandwidth (Connection:Keep-Alive, 3.7k bytes data packet) test data 163dc5b2a9eSfengbojiang 164a9643ea8Slogwang 165a9643ea8Slogwang## Licenses 166a9643ea8SlogwangSee [LICENSE](LICENSE) 1678c0855abSjohnjiang 1688c0855abSjohnjiang## Join us 1698c0855abSjohnjiang 1708c0855abSjohnjiangTencent Cloud F-Stack team developed F-Stack which is a general network framework based on DPDK and provides ultra high network performance. We are here looking for more and more talented people with great passion on technology to join us. You would have the chance to work with brightest minds on this planet and help Tencent cloud and F-stack continuously evolve. Send us your resume or refer your friend to us if you are interested in joining us. 1718c0855abSjohnjiang 1728c0855abSjohnjiangOpen Positions: Software engineer(C/C++), Web developer, IOS/Android developer, Product Manager, Operating Manager, etc. 1738c0855abSjohnjiangContact: Please send your resume to [us](mailto:[email protected]) 174