| With the rapid development of network technology,making the data plane have the same programmability as the control plane is the key to the development of software-defined networks(SDN).The programming protocol-independent packet processor(P4)gives the data plane the ability to be programmable,which can be controlled by software programming,so that the process of packet parsing and forwarding is no longer bound to hardware.At the same time,with the exponential increase of network bandwidth,the processing capacity of network equipment has gradually developed into the bottleneck of network bandwidth.It is difficult for traditional network equipment to meet the requirements of high performance and high flexibility at the same time.Programmable network processor unit(NPU)can achieve a balance between flexibility and high performance,and is an ideal solution.For this purpose,this thesis designs and implements a P4 language compiler based on NPU,which has important application significance for network equipment such as switches,network cards,and firewalls.Based on the principles of compilation technology,this thesis divides the compiler system into five modules: lexical analysis,syntax analysis,semantic analysis,intermediate code generation and target code generation.It adopts a highly modular design and conducts a detailed study on the design and implementation process of each module.The main contents are as follows.Firstly,the lexical analysis and syntax analysis functions are realized by using the automatic compiler tool PLY,which not only shortens the development cycle of the compiler,but also reduces the complexity of the development of lexical analysis module and the syntax analysis module,and ensures the quality of the two modules.Secondly,according to the characteristics of P4 language,data structures such as syntax tree and symbol table are designed to store semantic information and semantic checking process is designed to find semantic errors in the program,which further improves the correctness of the compiler system.Thirdly,a high-level intermediate representation data structure is designed to store intermediate code in the form of Python object structure,which is independent of the target platform,thus improving the portability of the compiler system.Finally,the data structure related to the target platform is designed,and the corresponding instructions are generated according to the mapping rules between the source language and the target language,and the matching and translation functions of the instructions are realized.The results of testing and verifying the P4 language compiler based on NPU show that each function and performance meet the design specifications.The compiler system can realize the function of each module,generate the code that runs on the target platform correctly,print the compilation process and give error messages.The performance test results show that it takes 0.5s to compile 100 lines of code and 4s to compile 1000 lines of code.The compilation time can be controlled within an acceptable range.Compiling 1000 lines of code takes up 1.5MB of physical memory in the system,the throughput of generated target code is 100 Gbps,and the PPS is 52 Mpps.The work of this thesis,on the one hand,expands the application field of P4,on the other hand,greatly improves the flexibility and programmability of NPU equipment,which has important engineering application value. |