Package
对于package这个概念,类似于将一个框架中各组成部件以一个规律进行打包,以正常运转。
基于一个架构去编写一个新的pipeline的时候,需要先了解初始化的时候需要提供那些东西,parser,ingress或一些校验的模块。
v1model的package
1 package V1Switch(Parser p, 2 VerifyChecksum vr 3 Ingress ig, 4 Egress eg, 5 ComputeChecksum ck, 6 Deparser dep 7 )
从package的定义中可以看出v1model需要的东西,这些东西在框架的设计中也有相对应的定义。
就如之前control中提到的ingress,其就需要包含header,metadata,standard metadata。
编写一个最基本的p4程序,官方建议使用core.p4这个标准函数库以及官方提供的v1model架构,其导入的方式类似于c语言以#include<库名>的方式导入。
1 #include <core.p4> 2 #include <v1model.p4> 3 4 #include "include/headers.p4" 5 #include "include/custom_headers.p4" 6 #include "include/defines.p4" 7 #include "include/parsers.p4" 8 #include "include/actions.p4" 9 #include "include/port_counters.p4" 10 #include "include/port_meters.p4" 11 #include "include/checksums.p4" 12 #include "include/packet_io.p4" 13 #include "include/table0.p4" 14 #include "include/host_meter_table.p4" 15 #include "include/wcmp.p4" 16 17 //------------------------------------------------------------------------------ 18 // INGRESS PIPELINE 19 //------------------------------------------------------------------------------ 20 21 control ingress(inout headers_t hdr, 22 inout local_metadata_t local_metadata, 23 inout standard_metadata_t standard_metadata) { 24 25 apply { 26 port_counters_ingress.apply(hdr, standard_metadata); 27 port_meters_ingress.apply(hdr, standard_metadata); 28 packetio_ingress.apply(hdr, standard_metadata); 29 table0_control.apply(hdr, local_metadata, standard_metadata); 30 host_meter_control.apply(hdr, local_metadata, standard_metadata); 31 wcmp_control.apply(hdr, local_metadata, standard_metadata); 32 } 33 } 34 35 //------------------------------------------------------------------------------ 36 // EGRESS PIPELINE 37 //------------------------------------------------------------------------------ 38 39 control egress(inout headers_t hdr, 40 inout local_metadata_t local_metadata, 41 inout standard_metadata_t standard_metadata) { 42 43 apply { 44 port_counters_egress.apply(hdr, standard_metadata); 45 port_meters_egress.apply(hdr, standard_metadata); 46 packetio_egress.apply(hdr, standard_metadata); 47 } 48 } 49 50 //------------------------------------------------------------------------------ 51 // SWITCH INSTANTIATION 52 //------------------------------------------------------------------------------ 53 54 V1Switch(parser_impl(), 55 verify_checksum_control(), 56 ingress(), 57 egress(), 58 compute_checksum_control(), 59 deparser()) main;
一个p4程序的main文件,最后的一部分就是整个框架组成,然后main作为程序的入口使用,总体很类似于c语言。p4也可以自定义框架。