Skip to content

Commit

Permalink
Convert deparser header to 'inout' and address casting for functions …
Browse files Browse the repository at this point in the history
…with control block parameters (p4lang#4338)

* Convert deparser header to 'inout'.
Function with control block parameters generate correct cast
* Remove commented code.
* Fix failing issue
  • Loading branch information
komaljai authored Jan 19, 2024
1 parent 4c547a3 commit ef756ee
Show file tree
Hide file tree
Showing 41 changed files with 993 additions and 34 deletions.
12 changes: 11 additions & 1 deletion backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,17 @@ void IngressDeparserPNA::emitPreDeparser(EBPF::CodeBuilder *builder) {
void IngressDeparserPNA::emit(EBPF::CodeBuilder *builder) {
codeGen->setBuilder(builder);

for (auto a : controlBlock->container->controlLocals) emitDeclaration(builder, a);
for (auto a : controlBlock->container->controlLocals) {
if (a->is<IR::Declaration_Variable>()) {
auto vd = a->to<IR::Declaration_Variable>();
if (vd->type->toString() == headers->type->toString() ||
vd->type->toString() == user_metadata->type->toString()) {
codeGen->isPointerVariable(a->name.name);
codeGen->useAsPointerVariable(vd->name);
}
}
emitDeclaration(builder, a);
}

emitDeparserExternCalls(builder);
builder->newline();
Expand Down
2 changes: 1 addition & 1 deletion p4include/tc/pna.p4
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ control MainControlT<MH, MM>(

control MainDeparserT<MH, MM>(
packet_out pkt,
in MH main_hdr,
inout MH main_hdr,
in MM main_user_meta,
in pna_main_output_metadata_t ostd);

Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/default_action_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/default_hit_const_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/default_hit_const_repeat_errors.p4
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/default_hit_repeat_errors.p4
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/drop_packet_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/global_action_example_01.p4
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ control ingress(

control Ingress_Deparser(
packet_out pkt,
in my_ingress_headers_t hdr,
inout my_ingress_headers_t hdr,
in my_ingress_metadata_t meta,
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/global_action_example_02.p4
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ control ingress(

control Ingress_Deparser(
packet_out pkt,
in my_ingress_headers_t hdr,
inout my_ingress_headers_t hdr,
in my_ingress_metadata_t meta,
in pna_main_output_metadata_t ostd)
{
Expand Down
156 changes: 156 additions & 0 deletions testdata/p4tc_samples/ipip.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include <core.p4>
#include <tc/pna.p4>

struct metadata_t {
bit<32> src;
bit<32> dst;
bool push;
}

header ethernet_t {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}

header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
bit<32> srcAddr;
bit<32> dstAddr;
}

struct headers_t {
ethernet_t ethernet;
ipv4_t outer;
ipv4_t inner;
}

#define ETHERTYPE_IPV4 0x0800
#define IPPROTO_IPV4 0x4

/*********************** P A R S E R **************************/
parser Parser(
packet_in pkt,
out headers_t hdr,
inout metadata_t meta,
in pna_main_parser_input_metadata_t istd)
{
state start {
transition parse_ethernet;
}

state parse_ethernet {
pkt.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
ETHERTYPE_IPV4: parse_ipv4;
default: reject;
}
}

state parse_ipv4 {
pkt.extract(hdr.outer);
transition select(hdr.outer.protocol) {
IPPROTO_IPV4: parse_ipv4_inner;
default: accept;
}
}

state parse_ipv4_inner {
pkt.extract(hdr.inner);
transition accept;
}
}

void ipip_push(inout headers_t hdr, in metadata_t meta)
{
hdr.inner = hdr.outer;
hdr.outer.srcAddr = meta.src;
hdr.outer.dstAddr = meta.dst;
hdr.outer.ttl = 64;
hdr.outer.protocol = 4; /* IPIP */
/* Assume MTU can accomodate +20 bytes */
hdr.outer.totalLen = hdr.outer.totalLen + 20;
hdr.outer.hdrChecksum = 0;
}

/***************** M A T C H - A C T I O N *********************/
control Main(
inout headers_t hdr,
inout metadata_t meta,
in pna_main_input_metadata_t istd,
inout pna_main_output_metadata_t ostd
)
{
action set_ipip(@tc_type("ipv4") bit<32> src, @tc_type("ipv4") bit<32> dst, @tc_type("dev") PortId_t port) {
meta.src = src;
meta.dst = dst;
meta.push = true;
send_to_port(port);
}

action set_nh(@tc_type("macaddr") bit<48> dmac, @tc_type("dev") PortId_t port) {
hdr.ethernet.dstAddr = dmac;
send_to_port(port);
}

action drop() {
drop_packet();
}

table fwd_table {
key = {
istd.input_port : exact @tc_type("dev") @name("port");
}
actions = {
set_ipip;
set_nh;
drop;
}
default_action = drop;
}

apply {
if (hdr.outer.isValid()) { /* applies to both ipip and plain ip */
fwd_table.apply(); /* lookup based on incoming netdev */
if (hdr.inner.isValid()) { /* incoming packet ipip */
/* Pop the ipip header by invalidating outer header */
hdr.outer.setInvalid();
}
}
}
}

/********************* D E P A R S E R ************************/
control Deparser(
packet_out pkt,
inout headers_t hdr,
in metadata_t meta,
in pna_main_output_metadata_t ostd)
{

apply {
pkt.emit(hdr.ethernet);
if (meta.push && hdr.outer.isValid()) {
/* Push the ipip header */
ipip_push(hdr, meta);
}
pkt.emit(hdr.outer);
pkt.emit(hdr.inner);
}
}

/************ F I N A L P A C K A G E ******************************/
PNA_NIC(
Parser(),
Main(),
Deparser()
) main;
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/matchtype.p4
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/mix_matchtype_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/multiple_tables_example_01.p4
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/multiple_tables_example_02.p4
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/name_annotation_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/no_table_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/noaction_example_01.p4
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/noaction_example_02.p4
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/nummask_annotation_errors.p4
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/nummask_annotation_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in metadata_t meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/send_to_port_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/set_entry_timer_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ control MainControlImpl(

control MainDeparserImpl(
packet_out pkt,
in headers_t hdr, // from main control
inout headers_t hdr, // from main control
in main_metadata_t user_meta, // from main control
in pna_main_output_metadata_t ostd)
{
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4tc_samples/simple_exact_example.p4
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ control ingress(

control Ingress_Deparser(
packet_out pkt,
in my_ingress_headers_t hdr,
inout my_ingress_headers_t hdr,
in my_ingress_metadata_t meta,
in pna_main_output_metadata_t ostd)
{
Expand Down
Loading

0 comments on commit ef756ee

Please sign in to comment.