OpenFlow.p4 源码
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* Openflow Processing
*/
// Openflow features
//#define OPENFLOW_ENABLE_MPLS
//#define OPENFLOW_ENABLE_VLAN
//#define OPENFLOW_ENABLE_L3
// adds some handy stuff from switch.p4 for packet in/out
#define OPENFLOW_PACKET_IN_OUT
#define ingress_input_port standard_metadata.ingress_port
#define ingress_egress_port standard_metadata.egress_spec
#define egress_egress_port standard_metadata.egress_port
#define intrinsic_mcast_grp intrinsic_metadata.mcast_grp
header_type openflow_metadata_t {
fields {
index : 32;
bmap : 32;
group_id : 32;
ofvalid : 1;
}
}
metadata openflow_metadata_t openflow_metadata;
#ifndef CPU_PORT_ID
#define CPU_PORT_ID 64
#endif
#define TRUE 1
#ifdef OPENFLOW_PACKET_IN_OUT
#define ETHERTYPE_BF_FABRIC 0x9000
#define FABRIC_HEADER_TYPE_MULTICAST 2
#define FABRIC_HEADER_TYPE_CPU 5
header_type fabric_header_t {
fields {
packetType : 3;
headerVersion : 2;
packetVersion : 2;
pad1 : 1;
fabricColor : 3;
fabricQos : 5;
dstDevice : 8;
dstPortOrGroup : 16;
}
}
header_type fabric_header_multicast_t {
fields {
routed : 1;
outerRouted : 1;
tunnelTerminate : 1;
ingressTunnelType : 5;
ingressIfindex : 16;
ingressBd : 16;
mcastGrp : 16;
}
}
header_type fabric_header_cpu_t {
fields {
egressQueue : 5;
txBypass : 1;
reserved : 2;
ingressPort: 16;
ingressIfindex : 16;
ingressBd : 16;
reasonCode : 16;
}
}
header_type fabric_payload_header_t {
fields {
etherType : 16;
}
}
header fabric_header_t fabric_header;
header fabric_header_cpu_t fabric_header_cpu;
header fabric_header_multicast_t fabric_header_multicast;
header fabric_payload_header_t fabric_payload_header;
parser parse_fabric_header {
extract(fabric_header);
return select(latest.packetType) {
FABRIC_HEADER_TYPE_MULTICAST : parse_fabric_header_multicast;
FABRIC_HEADER_TYPE_CPU : parse_fabric_header_cpu;
default : ingress;
}
}
parser parse_fabric_header_multicast {
extract(fabric_header_multicast);
return parse_fabric_payload_header;
}
parser parse_fabric_header_cpu {
extract(fabric_header_cpu);
return parse_fabric_payload_header;
}
parser parse_fabric_payload_header {
extract(fabric_payload_header);
return select(latest.etherType) {
// add more ethertypes here if you want
default: ingress;
}
}
action nop () {
}
// remove the comments in "terminate_cpu_packet" and "terminate_fabric_multicast_packet"
// as necessary. I'm just assuming these features aren't used (Except copying the
// ethertype from the fabric payload header, that's necessary but I don't want to
// assume you've named your ethernet header "ethernet" :) )
action terminate_cpu_packet() {
modify_field(ingress_egress_port,
fabric_header.dstPortOrGroup);
// modify_field(egress_metadata.bypass, fabric_header_cpu.txBypass);
modify_field(ethernet.etherType, fabric_payload_header.etherType);
remove_header(fabric_header);
remove_header(fabric_header_cpu);
remove_header(fabric_payload_header);
}
action terminate_fabric_multicast_packet() {
// modify_field(tunnel_metadata.tunnel_terminate,
// fabric_header_multicast.tunnelTerminate);
// modify_field(tunnel_metadata.ingress_tunnel_type,
// fabric_header_multicast.ingressTunnelType);
// modify_field(l3_metadata.nexthop_index, 0);
// modify_field(l3_metadata.routed, fabric_header_multicast.routed);
// modify_field(l3_metadata.outer_routed,
// fabric_header_multicast.outerRouted);
modify_field(intrinsic_mcast_grp,
fabric_header_multicast.mcastGrp);
modify_field(ethernet.etherType, fabric_payload_header.etherType);
remove_header(fabric_header);
remove_header(fabric_header_multicast);
remove_header(fabric_payload_header);
}
table packet_out {
reads {
fabric_header.packetType : exact;
}
actions {
nop;
terminate_cpu_packet;
terminate_fabric_multicast_packet;
}
}
#endif /* OPENFLOW_PACKET_IN_OUT */
/****************************************************************
* Actions common to all openflow tables, sets a bitmap indicating
* which OFPAT to be applied to packets in flow flow_id.
****************************************************************/
action openflow_apply(bmap, index, group_id) {
modify_field(openflow_metadata.bmap, bmap);
modify_field(openflow_metadata.index, index);
modify_field(openflow_metadata.group_id, group_id);
modify_field(openflow_metadata.ofvalid, TRUE);
// modify_field(egress_metadata.bypass, TRUE);
}
action openflow_miss(reason, table_id) {
#ifdef OPENFLOW_PACKET_IN_OUT
add_header(fabric_header);
add_header(fabric_header_cpu);
add_header(fabric_payload_header);
modify_field(fabric_payload_header.etherType, ethernet.etherType);
modify_field(fabric_header_cpu.ingressPort, ingress_input_port);
#endif
modify_field(fabric_header_cpu.reasonCode, reason);
shift_left(fabric_header_cpu.reasonCode, fabric_header_cpu.reasonCode, 8);
bit_or(fabric_header_cpu.reasonCode, fabric_header_cpu.reasonCode, table_id);
modify_field(ingress_egress_port, CPU_PORT_ID);
}
/****************************************************************
* Egress openflow bitmap translation
****************************************************************/
action ofpat_group_egress_update(bmap) {
bit_or (openflow_metadata.bmap, openflow_metadata.bmap, bmap);
}
table ofpat_group_egress {
reads {
openflow_metadata.group_id : exact;
egress_egress_port : exact;
}
actions {
ofpat_group_egress_update;
nop;
}
}
/****************************************************************
* GROUPS
****************************************************************/
action ofpat_group_ingress_uc(ifindex) {
modify_field(ingress_egress_port, ifindex);
}
action ofpat_group_ingress_mc(mcindex) {
modify_field(intrinsic_mcast_grp, mcindex);
}
table ofpat_group_ingress {
reads {
openflow_metadata.group_id : exact;
}
actions {
ofpat_group_ingress_uc;
ofpat_group_ingress_mc;
nop;
}
}
/****************************************************************
* OFPAT_OUTPUT
****************************************************************/
action ofpat_output(egress_port) {
modify_field(ingress_egress_port, egress_port);
// for switch.p4
// modify_field(ingress_metadata.egress_ifindex, 0);
}
table ofpat_output {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
ingress_egress_port : ternary;
}
actions {
ofpat_output;
nop;
}
}
#ifdef OPENFLOW_ENABLE_MPLS
/***************************************************************
* OFPAT_SET_MPLS_TTL
***************************************************************/
action ofpat_set_mpls_ttl(ttl) {
modify_field(mpls[0].ttl, ttl);
}
table ofpat_set_mpls_ttl {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_set_mpls_ttl;
nop;
}
}
/***************************************************************
* OFPAT_DEC_MPLS_TTL
***************************************************************/
action ofpat_dec_mpls_ttl() {
add_to_field(mpls[0].ttl, -1);
}
table ofpat_dec_mpls_ttl {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_dec_mpls_ttl;
nop;
}
}
/****************************************************************
* OFPAT_PUSH_MPLS
****************************************************************/
action ofpat_push_mpls() {
modify_field(ethernet.etherType, 0x8847);
add_header(mpls[0]);
}
table ofpat_push_mpls {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_push_mpls;
nop;
}
}
/***************************************************************
* OFPAT_POP_MPLS
***************************************************************/
action ofpat_pop_mpls() {
remove_header(mpls[0]);
}
table ofpat_pop_mpls {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_pop_mpls;
nop;
}
}
#endif /* OPENFLOW_ENABLE_MPLS */
#ifdef OPENFLOW_ENABLE_VLAN
/***************************************************************
* OFPAT_PUSH_VLAN
***************************************************************/
action ofpat_push_vlan() {
modify_field(ethernet.etherType, 0x8100);
add_header(vlan_tag_[0]);
modify_field(vlan_tag_[0].etherType, 0x0800);
}
table ofpat_push_vlan {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_push_vlan;
nop;
}
}
/***************************************************************
* OFPAT_POP_VLAN
***************************************************************/
action ofpat_pop_vlan() {
modify_field(ethernet.etherType, vlan_tag_[0].etherType);
remove_header(vlan_tag_[0]);
}
table ofpat_pop_vlan {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_pop_vlan;
nop;
}
}
/***************************************************************
* OFPAT_SET_FIELD
***************************************************************/
action ofpat_set_vlan_vid(vid) {
modify_field(vlan_tag_[0].vid, vid);
}
table ofpat_set_field {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_set_vlan_vid;
nop;
}
}
#endif /* OPENFLOW_ENABLE_VLAN */
/****************************************************************
* OFPAT_SET_QUEUE
****************************************************************/
//TODO
#ifdef OPENFLOW_ENABLE_L3
/***************************************************************
* OFPAT_SET_NW_TTL IPV4
***************************************************************/
action ofpat_set_nw_ttl_ipv4(ttl) {
modify_field(ipv4.ttl, ttl);
}
table ofpat_set_nw_ttl_ipv4 {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_set_nw_ttl_ipv4;
nop;
}
}
/***************************************************************
* OFPAT_SET_NW_TTL IPV6
***************************************************************/
action ofpat_set_nw_ttl_ipv6(ttl) {
modify_field(ipv6.hopLimit, ttl);
}
table ofpat_set_nw_ttl_ipv6 {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_set_nw_ttl_ipv6;
nop;
}
}
/***************************************************************
* OFPAT_DEC_NW_TTL IPV4
***************************************************************/
action ofpat_dec_nw_ttl_ipv4() {
add_to_field(ipv4.ttl, -1);
}
table ofpat_dec_nw_ttl_ipv4 {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_dec_nw_ttl_ipv4;
nop;
}
}
/***************************************************************
* OFPAT_DEC_NW_TTL IPV6
***************************************************************/
action ofpat_dec_nw_ttl_ipv6(ttl) {
add_to_field(ipv6.hopLimit, -1);
}
table ofpat_dec_nw_ttl_ipv6 {
reads {
openflow_metadata.index : ternary;
openflow_metadata.group_id : ternary;
egress_egress_port : ternary;
}
actions {
ofpat_dec_nw_ttl_ipv6;
nop;
}
}
#endif /* OPENFLOW_ENABLE_L3 */
/***************************************************************
* Main control block
***************************************************************/
control process_ofpat_ingress {
if (openflow_metadata.bmap & 0x400000 == 0x400000) {
apply(ofpat_group_ingress);
}
if (openflow_metadata.bmap & 0x1 == 1) {
apply(ofpat_output);
}
}
control process_ofpat_egress {
apply(ofpat_group_egress);
#ifdef OPENFLOW_ENABLE_MPLS
if (openflow_metadata.bmap & 0x100000 == 0x100000) {
apply(ofpat_pop_mpls);
}
if (openflow_metadata.bmap & 0x80000 == 0x80000) {
apply(ofpat_push_mpls);
}
if (openflow_metadata.bmap & 0x10000 == 0x10000) {
apply(ofpat_dec_mpls_ttl);
}
if (openflow_metadata.bmap & 0x8000 == 0x8000) {
apply(ofpat_set_mpls_ttl);
}
#endif /* OPENFLOW_ENABLE_MPLS */
#ifdef OPENFLOW_ENABLE_VLAN
if (openflow_metadata.bmap & 0x40000 == 0x40000) {
apply(ofpat_pop_vlan);
}
if (openflow_metadata.bmap & 0x20000 == 0x20000) {
apply(ofpat_push_vlan);
}
if (openflow_metadata.bmap & 0x2000000 == 0x2000000) {
apply(ofpat_set_field);
}
#endif /* OPENFLOW_ENABLE_VLAN */
#ifdef OPENFLOW_ENABLE_L3
if (openflow_metadata.bmap & 0x1000000 == 0x1000000) {
if ((valid(ipv4))) {
apply(ofpat_dec_nw_ttl_ipv4);
} else {
if ((valid(ipv6))) {
apply(ofpat_dec_nw_ttl_ipv6);
}
}
}
if (openflow_metadata.bmap & 0x800000 == 0x800000) {
if (valid(ipv4)) {
apply(ofpat_set_nw_ttl_ipv4);
} else {
if (valid(ipv6)) {
apply(ofpat_set_nw_ttl_ipv6);
}
}
}
#endif /* OPENFLOW_ENABLE_L3 */
// oq (set queue)
}
2016/10/16
OpenFlow.p4 源码的更多相关文章
- switch parser.p4源码
/* Copyright 2013-present Barefoot Networks, Inc. Licensed under the Apache License, Version 2.0 (th ...
- maven 下载 源码和javadoc 命令
摘要:我们在写代码时候,往往是想查看一下源码,看看源码的一些细节内容.一般情况下,在IDE(如eclipse)中近仅仅只需按住ctrl+ 点击对应的方法即可进入对应的源码部分.但是有些时候很多依赖项并 ...
- android源码的目录结构
android源码的目录结构 [以下网络摘抄] |-- Makefile ! l/ a5 n% S% @- `0 d# z# a$ P4 V3 o7 R|-- bionic ...
- JS魔法堂:剖析源码理解Promises/A规范
一.前言 Promises/A是由CommonJS组织制定的异步模式编程规范,有不少库已根据该规范及后来经改进的Promises/A+规范提供了实现 如Q, Bluebird, when, rsvp. ...
- AndFix热修复 —— 实战与源码解析
当你的应用发布后第二天却发现一个重要的bug要修复,头疼的同时你可能想着赶紧修复重新打个包发布出去,让用户收到自动更新重新下载.但是万事皆有可能,万一隔一天又发现一个急需修复的bug呢?难道再次发布打 ...
- socket_server源码剖析、python作用域、IO多路复用
本节内容: 课前准备知识: 函数嵌套函数的使用方法: 我们在使用函数嵌套函数的时候,是学习装饰器的时候,出现过,由一个函数返回值是一个函数体情况. 我们在使用函数嵌套函数的时候,最好也这么写. def ...
- JQuery源码分析(七)
了解jQuery对DOM进行遍历背后的工作机制,这样可以在编写代码时有意识地避免一些不必要的重复操作,从而提升代码的性能. 关于jQuery对象的包装 var $aaron = $("aar ...
- Hadoop源码的编译过程详细解读(各版本)
说在前面的话 重新试多几次.编译过程中会出现下载某个包的时间太久,这是由于连接网站的过程中会出现假死,按ctrl+c,重新运行编译命令. 如果出现缺少了某个文件的情况,则要先清理maven(使用命 ...
- Android中Canvas绘图基础详解(附源码下载) (转)
Android中Canvas绘图基础详解(附源码下载) 原文链接 http://blog.csdn.net/iispring/article/details/49770651 AndroidCa ...
随机推荐
- 删除自带OpenJDK
1:rpm -qa|grep jdk 查看当前的jdk情况. 2:yum -y remove java java-1.7.0-openjdk* 卸载openjdk,这个过程中因为依赖原因可能会卸载一些 ...
- BaseActivity与BaseFragment的封装
这篇博客主要是从BaseActivity与BaseFragment的封装开始,总结我们在实战开发中关于Fragment的注意事项以及心得体会. 先看以下效果图: 这里模拟的是用户登录模块,你可能会说, ...
- 创建型模式之Singleton模式
单例模式大概是最直观的一种设计模式了,尽管直观却不简单. 数学与逻辑学中,singleton定义为“有且仅有一个元素的集合”, 单例模式可以如下定义:“一个类有且仅有一个实例,并且自行实例化向整个系统 ...
- django默认开事务的麻烦事
最近DBA发现总是有大事务报警,最终排查到是因为django默认在查询之前执行了 set autocommit=0 原来,mysql如果开了set autocommit=0,那么所有的语句一定是在一个 ...
- hdu 2795 线段树(纵向)
注意h的范围和n的范围,纵向建立线段树 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子思路:每次找到最大值的位子,然后减去L线段树功能:query:区间求最大值的位子(直接 ...
- PUSHA/PUSHAD
http://124.16.151.186/docs/optimization/VTune(TM) User's Guide/mergedProjects/analyzer_ec/mergedProj ...
- hdu 1269 迷宫城堡 强连通分量
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- volatile的理解
用法解释 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修改了某个变量的值,这新值对其 ...
- 转-CSS3 圆角(border-radius)
CSS3 圆角(border-radius) 前缀 例1 例2:无边框 书写顺序 其它 支持性 值:半径的长度 前缀 -moz(例如 -moz-border-radius)用于Firefox -w ...
- 转 BHO API HOOK Wininet基于IE编程的一些资料
BHO原理:推荐vc base中的文章: 如何使用BHO定制你的Internet Explorer浏览器 API HOOK的基本原理:推荐C++ builder 研究中的文章: API Hook基 ...