当前位置:网站首页>初识P4语言
初识P4语言
2022-07-02 03:55:00 【不胖的小胖芊】
P4学习之路
记录自己进行P4实验过程中所了解到的一些简单的P4知识
概念
- P4的全称是Programming Protocol-independent Packet Processor,即对协议无关的包处理器进行编程的语言
- 所谓对协议无关,即根据用户的需求实现一个全新的网络协议
- 定义自己想要的数据面,然后通过南向协议(openFlow)添加流表项
流程
- P4的流程如下图所示,当数据包进入交换机,首先解析数据包包头,然后ingress进行处理,最后封包转发走
- P4写的就是数据包怎么处理
包解析
- 包解析就是 提取出数据报文中我们要的数据这一步骤
PS:(工程师提醒我)提取的是按照报文的顺序往后提取的,有一个指针指向了报文已经提取的位置 - 已经提取的部分,再提取就不对
比如,我本来已经提取数据包IPv4的头部,包含了目的IP地址,我想将其作为四个8比特字符提取出来,不能再写一个提取目的地址,我就犯过这个错误
举例如下,需求:将目的IP地址解析为四个8比特字符
代码如下:
// 修改IPv4的header
header ipv4_h {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> total_len;
bit<16> identification;
bit<3> flags;
bit<13> frag_offset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdr_checksum;
ipv4_addr_t src_addr;
// ipv4_addr_t dst_addr;
bit<8> ch1;
bit<8> ch2;
bit<8> ch3;
bit<8> ch4;
}
// 包解析逻辑部分
parser SwitchIngressParser(
packet_in pkt,
out header_t hdr,
out metadata_t ig_md,
out ingress_intrinsic_metadata_t ig_intr_md) {
// 解析器从start状态开始执行到结束,其中reject或accept表示已达到结束状态
// transition表示转换到其他状态
state start {
pkt.extract(ig_intr_md);
pkt.advance(PORT_METADATA_SIZE);
transition parse_ethernet;
}
state parse_ethernet {
pkt.extract(hdr.ethernet);
transition parse_ipv4;
}
// 解析器状态的最后一条语句是可选的transition语句,将控制权转移到另一个状态,可能accept 或者reject
state parse_ipv4 {
pkt.extract(hdr.ipv4);
transition accept;
}
}
写表查表
- 写表,表包括以下几个部分
- 表名
- key值与匹配类型,匹配类型有exact、ternary、lpm三种
- actions,包含所有可能的action
- 定义默认的动作
- 表的大小
table tableName {
key = {
a : exact; // 关键字a是确切匹配
b : ternary; // 关键字b是ternary类型
}
actions = {
get_value; // 包含所有可能的动作
}
size=1024; //表的大小
}
- 查表
- 一个表只能查一次,一个表对应一个match-action unit(匹配动作单元)
- 若要对一个表多次查找,则需要被实例化多次,即写多个名字不同的表
- 查表在apply里面进行
apply {
// 进行操作
// 查表
tableName.apply();
}
- actions
(个人感觉只能进行赋值操作…)- 只允许straight-line代码
- 没有条件语句或表达式
包封装
暂未进行了解…
其它
- switch
对于switch语句,表达式必须是t.apply().action_run(),所有标签必须是表的动作t,或者是default - select
选择表达式select将提取的标头字段与一组常量比较
参考
边栏推荐
- Three ways for programmers to learn PHP easily and put chaos out of order
- 【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
- Blue Bridge Cup single chip microcomputer sixth temperature recorder
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Learn more about materialapp and common attribute parsing in fluent
- The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
- go 包的使用
- [yolo3d]: real time detection of end-to-end 3D point cloud input
- XSS prevention
- regular expression
猜你喜欢
【IBDFE】基于IBDFE的频域均衡matlab仿真
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
Visual slam Lecture 3 -- Lie groups and Lie Algebras
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
Go语言介绍
JVM知识点
[wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
随机推荐
NLog使用
Basic syntax of unity script (6) - specific folder
5G时代全面到来,浅谈移动通信的前世今生
[untitled]
Basic operations of MySQL database (based on tables)
MD5 of Oracle
JVM knowledge points
The 6th Blue Bridge Cup single chip microcomputer provincial competition
"No war on the Western Front" we just began to love life, but we had to shoot at everything
Monkey test
The 11th Blue Bridge Cup single chip microcomputer provincial competition
蓝桥杯单片机省赛第七届
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
NLog use
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
Introduction to Robotics II. Forward kinematics, MDH method
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
【小技巧】使用matlab GUI以对话框模式读取文件
手撕——排序
蓝桥杯单片机省赛第九届