blob: 7f5431cb03a3e90a44c91f81dd1a693453776021 [file] [log] [blame]
Daniele Morof178b0a2020-12-15 14:13:51 +01001/*
2 * Copyright 2020-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <core.p4>
18#include <v1model.p4>
19
20typedef bit<9> port_t;
21
22@p4runtime_translation("", string)
23type bit<48> mac_addr_t;
24
25@p4runtime_translation("", 32)
26type port_t port_id_bit_t;
27
28@p4runtime_translation("", string)
29type port_t port_id_str_t;
30
31const port_t CPU_PORT = 255;
32
33@controller_header("packet_in")
34header packet_in_header_t {
35 bit<9> ingress_port;
36 bit<7> _padding;
37}
38
39@controller_header("packet_out")
40header packet_out_header_t {
41 bit<9> egress_port;
42 bit<7> _padding;
43}
44
45header ethernet_t {
46 mac_addr_t dstAddr;
47 mac_addr_t srcAddr;
48 bit<16> etherType;
49}
50
51struct headers_t {
52 packet_out_header_t packet_out;
53 packet_in_header_t packet_in;
54 ethernet_t ethernet;
55}
56
57struct local_metadata_t {
58 port_id_bit_t ingress_port;
59}
60
61// Test P4 Program for P4Runtime translation (simplified version of basic.p4).
62
63//------------------------------------------------------------------------------
64// PARSER
65//------------------------------------------------------------------------------
66parser parser_impl(packet_in packet,
67 out headers_t hdr,
68 inout local_metadata_t local_metadata,
69 inout standard_metadata_t standard_metadata) {
70
71 state start {
72 local_metadata.ingress_port = (port_id_bit_t) standard_metadata.ingress_port;
73 transition select(standard_metadata.ingress_port) {
74 CPU_PORT: parse_packet_out;
75 default: parse_ethernet;
76 }
77 }
78
79 state parse_packet_out {
80 packet.extract(hdr.packet_out);
81 transition parse_ethernet;
82 }
83
84 state parse_ethernet {
85 packet.extract(hdr.ethernet);
86 transition accept;
87 }
88}
89
90//------------------------------------------------------------------------------
91// DEPARSER
92//------------------------------------------------------------------------------
93control deparser(packet_out packet, in headers_t hdr) {
94 apply {
95 packet.emit(hdr.packet_in);
96 packet.emit(hdr.ethernet);
97 }
98}
99
100
101//------------------------------------------------------------------------------
102// INGRESS PIPELINE
103//------------------------------------------------------------------------------
104
105control ingress(inout headers_t hdr,
106 inout local_metadata_t local_metadata,
107 inout standard_metadata_t standard_metadata) {
108
109 @name(".send_to_cpu")
110 action send_to_cpu() {
111 standard_metadata.egress_spec = CPU_PORT;
112 }
113
114 @name(".set_egress_port")
115 action set_egress_port(port_id_str_t port) {
116 standard_metadata.egress_spec = (bit<9>) port;
117 }
118 @name(".set_egress_port2")
119 action set_egress_port2(port_id_bit_t port) {
120 standard_metadata.egress_spec = (bit<9>) port;
121 }
122
123 @name(".drop")
124 action drop() {
125 mark_to_drop(standard_metadata);
126 }
127 @name(".table0")
128 table table0 {
129 key = {
130 local_metadata.ingress_port : exact;
131 hdr.ethernet.srcAddr : exact;
132 hdr.ethernet.dstAddr : exact;
Daniele Moroc6f2f7f2020-12-18 10:55:57 +0100133 hdr.ethernet.etherType : optional;
Daniele Morof178b0a2020-12-15 14:13:51 +0100134 }
135 actions = {
136 set_egress_port;
137 set_egress_port2;
138 send_to_cpu;
139 drop;
140 }
141 const default_action = drop();
142 }
143
144 apply {
145 table0.apply();
146 }
147}
148
149//------------------------------------------------------------------------------
150// EGRESS PIPELINE
151//------------------------------------------------------------------------------
152
153control egress(inout headers_t hdr,
154 inout local_metadata_t local_metadata,
155 inout standard_metadata_t standard_metadata) {
156
157 apply {
158 // no-op
159 }
160}
161
162control verify_checksum_control(inout headers_t hdr,
163 inout local_metadata_t local_metadata) {
164 apply {
165 // Assume checksum is always correct.
166 }
167}
168
169control compute_checksum_control(inout headers_t hdr,
170 inout local_metadata_t local_metadata) {
171 apply {
172 // no-op for the test program
173 }
174}
175
176//------------------------------------------------------------------------------
177// SWITCH INSTANTIATION
178//------------------------------------------------------------------------------
179
180V1Switch(parser_impl(),
181 verify_checksum_control(),
182 ingress(),
183 egress(),
184 compute_checksum_control(),
185 deparser()) main;