Golang编程语言知识介绍


  • 首页

  • todo

  • 思考

  • life

  • food

  • OS

  • lua

  • redis

  • Golang

  • C

  • TCP/IP

  • ebpf

  • p4

  • OpenVPN

  • IPSec

  • L2TP

  • DNS

  • distributed

  • web

  • OpenWRT

  • 运维

  • Git

  • 鸟哥的私房菜

  • IT杂谈

  • 投资

  • About Me

  • 友情链接

  • FTP

  • 搜索
close

P4语法(4)Control block 转载系列 5

时间: 2023-09-12   |   分类: p4     |   阅读: 620 字 ~2分钟

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中进行实例化申明。

control Callee(inout IPv4 ipv4) { ...}
control Caller(inout Headers h) {
     Callee() instance;  // instance of callee
     apply {
          instance.apply(h.ipv4);  // invoke control
     }
}

v1model

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

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

样例

ipv4转发:

control MyIngress(inout headers hdr,
                  inout metadata meta,
                  inout standard_metadata_t standard_metadata)
{
    action drop(){
        mark_to_drop();
    }
    
    action ipv4_forward(macAddr_t dstAddr,egressSpec_t port){
        standard_metadata.egress_spec = port;
        hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
        hdr.ethernet.dstAddr = dstAddr;
        hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
    }
    
    table ipv4_lpm{
        key = {
            hdr.ipv4.dstAddr: lpm;
        }
        
        actions = {
            ipv4_forward;
            drop;
            NoAction;
        }
        
        size = 1024;
        
        default_action = drop();
    }
    
    apply{
        if(hdr.ipv4.isValid()){
            ipv4_lpm.apply();
        }
    }
}

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

以上内容转载自本链接

#p4#
P4语法(5) Package 系列转载 6
P4语法(3)Table,Action 转载系列 4
shankusu2017@gmail.com

shankusu2017@gmail.com

日志
分类
标签
GitHub
© 2009 - 2025
粤ICP备2021068940号-1 粤公网安备44011302003059
0%