P4语法(4)Control block

Control block

Control block之中用于放置设计好的Table和Action。

可以把control block认为是pipeline的一个模板,之前用的v1model中就是ingress和egress。 不同的架构中的control block格式不同。 其主要的功能就是知道每一个封包经过的table顺序以及采用的规则(触发条件等)。还能放置一些其他的功能部件,例如计数器counter等。


对于一个match-action pipeline通过control block的执行流程描述如下:

  • At runtime, statements within a block are executed in the order they appear in the control block.
  • Execution of the return statement causes immediate termination of the execution of the current control block, and a return to the caller.
  • Execution of the exit statement causes the immediate termination of the execution of the current control block and of all the enclosing caller control blocks.
  • Applying a table executes the corresponding match-action unit, as described above.

简单来说就是return退出并返回,exit退出,或者继续执行table中的规则。


p4中允许control调用其他control的服务,但是必须得在当前的control中进行实例化申明。

1 control Callee(inout IPv4 ipv4) { ...}
2 control Caller(inout Headers h) {
3      Callee() instance;  // instance of callee
4      apply {
5           instance.apply(h.ipv4);  // invoke control
6      }
7 }

 

v1model

1 control Ingress_name(inout H hdr,
2                      inout M meta,
3                      inout standard_metadata_t stand_metadata);

 

从参数中可以看出,需要包含定义好的header和metadata。且包含架构中提供的stand_metadata。

样例

ipv4转发:

 1 control MyIngress(inout headers hdr,
 2                   inout metadata meta,
 3                   inout standard_metadata_t standard_metadata)
 4 {
 5     action drop(){
 6         mark_to_drop();
 7     }
 8     
 9     action ipv4_forward(macAddr_t dstAddr,egressSpec_t port){
10         standard_metadata.egress_spec = port;
11         hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
12         hdr.ethernet.dstAddr = dstAddr;
13         hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
14     }
15     
16     table ipv4_lpm{
17         key = {
18             hdr.ipv4.dstAddr: lpm;
19         }
20         
21         actions = {
22             ipv4_forward;
23             drop;
24             NoAction;
25         }
26         
27         size = 1024;
28         
29         default_action = drop();
30     }
31     
32     apply{
33         if(hdr.ipv4.isValid()){
34             ipv4_lpm.apply();
35         }
36     }
37 }

 

从code的apply中看出,当封包是ipv4协议的时候,执行ipv4_lpm的table。

posted @ 2019-03-17 12:52  水喵桑  阅读(1389)  评论(0编辑  收藏  举报