主要来自对https://github.com/jafingerhut/p4-guide/blob/master/README-p4lang-repos.md 的学习

文档与博客

官方文档

博客

p4 lang组织下面的仓库

p4lang organization 是github的一个组织。以下是该组织2019-3.31的所有仓库,并按类别进行了归类:

规范文件

  • p4-spec - Containsspecification documents for the P4_14 language, the P4_16 language,and Portable Switch Architecture (PSA).
  • p4runtime - Specification documents for the P4Runtime control-plane API
  • p4-applications - P4 Applications WG repo

文档、研究论文和教程

P4 编译器

some only front end, some front end plus back end for one or more P4 targets

  • p4c - P4_16 prototype compiler (also compiles P4_14 programs)
  • p4c-bm - Generates the JSON configuration for the behavioral-model (bmv2), as well as the C/C++ PD code
  • p4-hlir - P4_14 compiler, written in Python, which stops at generating an intermediate representation, from which one can start in writing a back end compiler
  • p4c-behavioral - P4 compiler for the behavioral model. Deprecated.

P4 行为模型,用于在通用计算机上运行 P4 程序

  • behavioral-model - Rewrite of the behavioral model as a C++ project without
    auto-generated code. Also known as bmv2.
  • p4c-behavioral - P4 compiler for the behavioral model. Deprecated.

P4Runtime API 规范和一些实现代码,包括客户端和服务器代码

  • p4runtime - Specification documents for the P4Runtime control-plane API
  • PI - An implementation framework for a P4Runtime server
  • grpc - grpc - (forked from grpc/grpc) The C based gRPC (C++, Python, Ruby, Objective-C, PHP,C#) (forked from grpc/grpc)
  • protobuf - Protocol Buffers Google’s data interchange format (forked from
    protocolbuffers/protobuf)

由 p4.org 以外的组织创建的开源工具,供一个或多个p4lang存储库使用

  • grpc - grpc - (forked from grpc/grpc) The C based gRPC (C++, Python, Ruby, Objective-C, PHP,C#) (forked from grpc/grpc)
  • protobuf - Protocol Buffers Google’s data interchange format (forked from
    protocolbuffers/protobuf)
  • rules_protobuf - Bazel rules for building protocol buffers and gRPC services (java, c++,go, …) (forked from pubref/rules_protobuf)
  • mininet - Emulator for rapid prototyping of Software Defined Networks http://mininet.org (forked from mininet/mininet)
  • SAI - Switch Abstraction Interface (forked from opencomputeproject/SAI)
  • scapy-vxlan - A scapy clone, with support for additional packet headers
  • third-party - Third-party dependencies for p4lang software
  • thrift - Mirror of Apache Thrift (forked from
    apache/thrift)

用于创建和运行自动化测试

可能不再维护的部分

  • p4-build - Infrastructure needed to generate, build and install the PD library for a given P4 program
  • p4app - (No description)
  • p4factory - Compile P4 and run the P4 behavioral simulator. Deprecated.
  • p4ofagent - Openflow agent on a P4 dataplane
  • switch - Consolidated switch repo (API, SAI and Netlink)

编译命令

Sample command lines to compile P4 source file foo.p4 to bmv2 JSON configuration file:

# foo.p4 is P4_14 source code
p4c-bmv2 --json foo.json foo.p4
p4c --target bmv2 --arch v1model --std p4-14 foo.p4

# foo.p4 is P4_16 source code
p4c --target bmv2 --arch v1model foo.p4

Sample command line for converting P4_14 source code to P4_16 source code:

p4test --std p4-14 --pp foo-translated-to-p4-16.p4 foo-in-p4-14.p4

P4依赖关系图

可以参考https://github.com/jafingerhut/p4-guide/blob/master/dependencies.pdf

p4语言概览

  • 相对于C
  • P4删除了 loops, recursive calls, pointers, malloc, and free
    • 删除 loops, recursive calls,对于每一个包的工作可以限制在一个特定的时间,更加适合高性能的P4可编程平台
    • 删除 pointers, malloc, 以及自用的通用数据结构,从而不可能又任意大小的数据结构,但是我们可以实现具有固定深度的树
  • 增加了解析器parser, 重点关注将接收到的数据包的内容解析为一系列报头时最需要的功能
    • 解析器被定义为有限状态机,其中的状态必须命名并定义它们之间可能的转换。实际上,允许在解析器有限状态机中有循环,但最高性能目标通常会将您限制为可展开到编译时已知最大迭代次数的循环,例如,用于解析长度最多为5个报头的MPLS报头序列(其中5或其他值是您在源代码中选择的数字)。
    • P4专注于数据包报头处理。对于P4程序而言,数据包中任何未解析为某个报头的部分都被视为数据包的“有效载荷”,通常在处理完数据包后,数据包会随数据包一起携带,未经修改。您可以随意修改标题字段
  • 增加了tables这样的数据结构,每一个表都有一个搜索的键值,并且可以定义一个或者多个操作
  • 不同的交换机体系结构可能还有一些另外的库,比如PSA( Portable Switch Architecture )里面的packet/byte counters等等
  • 字段以及变量可以是任意位宽的整数(有最大位数限制),P4不支持浮点数运算,同时P4实现哦那个杨不需要实现乘法除法模运算,因为大多数包处理应用程序通常用不到
  • 没有浮点数咋整相关的文章integer arithmetic is sufficient for implementing fixed point operations

P4_14 和 P4_16的比较

16相对于14的好处

  • 更加像C++等语言的语法
  • p4_14没有参数和返回值,需要操作全局变量;p4_16没有全局变量,但是有in,out,inout 等具有方向性的参数
  • tables 以及 externs 的设计;

16相对于14的缺点

18年的时候16的支持不如14,但是时代变了;

bmv2使用