1a9643ea8Slogwang# F-Stack Development Guide
2a9643ea8Slogwang
3a9643ea8SlogwangWith the rapid development of NIC, the poor performance of data packets processing with Linux kernel has become the bottleneck. However the rapid development of the Internet needs high performance of network processing, kernel bypass has caught more and more attention. There are various similar technologies appear, 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 copy, thread scheduling, system calls and interrupt. Further more, kernel bypass can achieve higher performance with multi optimizing methods.  Within various techniques, DPDK has been widely used because of its more thorough isolation from kernel scheduling and active community support.
4a9643ea8Slogwang
5a9643ea8SlogwangF-Stack is an open source network framework with high performance based on DPDK. With follow characteristics
6a9643ea8Slogwang
7a9643ea8Slogwang1. Ultra high network performance which can achieve network card under full load, 10 million concurrent, five million RPS, 1 million CPS.
8a9643ea8Slogwang2. Transplant FreeBSD 11.01 user space stack, provides a complete stack function, cut a great amount of irrelevant features. Therefore greatly enhance the performance.
9a9643ea8Slogwang3. Support Nginx, Redis and other mature applications, service can easily use F-Stack
10a9643ea8Slogwang4. With Multi-process architecture, easy to extend
11a9643ea8Slogwang5. Provide micro thread interface. Various applications with long time consuming can easily use F-Stack to get high performance without processing complex asynchronous logic.
12a9643ea8Slogwang6. Provide Epoll/kqueue interface that allow many kinds of applications easily use F-Stack
13a9643ea8Slogwang
14a9643ea8Slogwang## Structure of F-Stack code
15a9643ea8Slogwang
16a9643ea8Slogwang    ├── app  -- Nginx(1.11.10)/Redis(3.2.8)/Microthread framework
17a9643ea8Slogwang    ├── config.ini
18a9643ea8Slogwang    ├── doc
19a9643ea8Slogwang    ├── dpdk -- Intel DPDK(16.07) directory
20a9643ea8Slogwang    ├── example -- DEMO
21a9643ea8Slogwang    ├── freebsd -- FreeBSD(11.0) Network Stack directory
22a9643ea8Slogwang    ├── lib -- F-Stack lib directory
23a9643ea8Slogwang    ├── mk
24a9643ea8Slogwang    └── start.sh
25a9643ea8Slogwang
26a9643ea8Slogwang
27a9643ea8Slogwang## DPDK initialization
28a9643ea8Slogwang
29a9643ea8Slogwang### PORT & SOCKET
30a9643ea8Slogwang
31a9643ea8SlogwangF-Stack simplify the initialization of the standard DPDK. By setting the NIC port and CPU core mask, you can set binding relationship of the port and CPU and lcore on different socket node. If there is no binding relationship set, port0 and socket node 0 will be set by default.
32a9643ea8Slogwang
33a9643ea8Slogwang### KNI related
34a9643ea8Slogwang
35a9643ea8SlogwangIf the server does not have dedicated port, or all port used for service process, you need to open the KNI in the configuration file, and set the related protocol and port number to decide which packets need to be processed by the F-Stack, remaining packets will be forwarded to kernel by KNI, to support SSH management functions.
36a9643ea8Slogwang
37a9643ea8Slogwang## Revise of FreeBSD Network Stack and DPDK based
38a9643ea8Slogwang
39a9643ea8SlogwangSince DPDK is open source, there are various open source network stacks based on DPDK to support the higher level application in the market. Some are will be packaging Linux network stack into a library, some are porting FreeBSD network stack.
40a9643ea8Slogwang
41a9643ea8SlogwangAt the beginning of this work, F-Stack used a simple TCP/IP stack that developed by ourselves. However, with the growth of various services, this stack couldn't meet the needs of these services while continue to develop and maintain a complete network stack will cost high. So the FreeBSD network stack was ported into F-Stack. The FreeBSD network stack provides complete features and can follow up the improvement from the community. 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 becomes a lot easier.
42a9643ea8Slogwang
43a9643ea8SlogwangIn order to minimize the impact of resource sharing and kernel system (such as scheduling, locks, etc.) on the performance, F-Stack uses a multi-process architecture. Following are the changes to the FreeBSD network stack.
44a9643ea8Slogwang
45a9643ea8Slogwang### Scheduling
46a9643ea8Slogwang
47a9643ea8SlogwangCut kernel thread, interrupt thread, timer thread, sched, wakeup, sleep, etc of FreeBSD Network Stack
48a9643ea8Slogwang
49a9643ea8Slogwang### Lock
50a9643ea8Slogwang
51a9643ea8SlogwangCut lock operations of FreeBSD Network Stack, including mtx、rw、rm、sx、cond, etc.
52a9643ea8Slogwang
53a9643ea8Slogwang### Memory related
54a9643ea8Slogwang
55a9643ea8SlogwangUsing phymem, uma\_page\_slab\_hash, uma initialization, kmem_malloc malloc
56a9643ea8Slogwang
57a9643ea8Slogwang### Global variables
58a9643ea8Slogwang
59a9643ea8Slogwangpcpu curthread proc0 thread0, initialization
60a9643ea8Slogwang
61a9643ea8Slogwang### Environment variable
62a9643ea8Slogwang
63a9643ea8Slogwangsetenv getenv
64a9643ea8Slogwang
65a9643ea8Slogwang### SYS_INIT
66a9643ea8Slogwang
67a9643ea8Slogwangmi_startup
68a9643ea8Slogwang
69a9643ea8Slogwang### Clock
70a9643ea8Slogwang
71a9643ea8Slogwangtimecounter, ticks, hz, timer
72a9643ea8Slogwang
73a9643ea8Slogwang### Other
74a9643ea8Slogwang
75a9643ea8SlogwangLinux and freebsd errno conversion, glue code, Remove unnecessary modules
76a9643ea8Slogwang
77a9643ea8Slogwang## Applications use F-Stack
78a9643ea8Slogwang
79a9643ea8SlogwangF-Stack provides ff API (See  *F-Stack\_API\_Reference*) to support applications. F-Stack also integrates third-party application such as Nginx, Redis, etc and. Micro thread interface is also provided to help original application easily use F-Stack.
80a9643ea8Slogwang
81a9643ea8Slogwang### Web application
82a9643ea8Slogwang
83a9643ea8SlogwangHTTP web application can use F-Stack with Nginx.
84a9643ea8Slogwang
85a9643ea8Slogwang### key-value application
86a9643ea8Slogwang
87a9643ea8Slogwangkey-value db application can use F-Stack with redis, and can start multi Redis instance.
88a9643ea8Slogwang
89a9643ea8Slogwang### Stateful(High latency) applications
90a9643ea8Slogwang
91a9643ea8SlogwangApplications with stateful(high latency) use F-Stack , state need to be stored for a long time, can directly use the F-Stack micro threading framework. Applications only need to focus on with the service logic. And with synchronous programming, high performance asynchronous service server can be achieved.
92a9643ea8Slogwang
93a9643ea8Slogwang## F-Stack configure file reference
94a9643ea8Slogwang
95*40600211Slogwang  DPDK related parameters, including coremask adn NIC ports num.
96*40600211Slogwang  FreeBSD related parameters, similar with original FreeBSD's /boot.config and /etc/sysctl.conf.
97a9643ea8Slogwang
98*40600211Slogwang## Start a F-Stack application
99a9643ea8Slogwang
100*40600211SlogwangSince F-Stack is multi-process architecture, every F-Stack application process should call `ff_init(argc, argv)` to initialize the environments.
101*40600211SlogwangFor example, if `lcore_mask=f` in config.ini, you can start your app like this:
102a9643ea8Slogwang
103*40600211Slogwang    ${bin} --conf config.ini --proc-type=primary --proc-id=0
104*40600211Slogwang    ${bin} --conf config.ini --proc-type=secondary --proc-id=1
105*40600211Slogwang    ${bin} --conf config.ini --proc-type=secondary --proc-id=2
106*40600211Slogwang    ${bin} --conf config.ini --proc-type=secondary --proc-id=3
107a9643ea8Slogwang
108*40600211SlogwangOr you can just use `start.sh` under F-Stack root directory.
109