jafingerhut / p4-guide

Guide to p4lang repositories and some other public info about P4
534 stars 156 forks source link

Question of tcp and udp on deparser #73

Closed git-liusen closed 5 months ago

git-liusen commented 5 months ago

control MyDeparser(packet_out packet, in headers hdr) { apply { packet.emit(hdr.ethernet); if (hdr.ipv4.isValid()) { packet.emit(hdr.ipv4); } else if (hdr.ipv6.isValid()) { packet.emit(hdr.ipv6); } if (hdr.tcp.isValid()){ packet.emit(hdr.tcp); } if (hdr.udp.isValid()) { packet.emit(hdr.udp); } } }

I want to implement it in a p4 program, but the condition is not support on deparser, how should i do to implement that. which can suport ipv4, ipv6, tcp, udp on a p4 program. Thanks for your response!

jafingerhut commented 5 months ago

The BMV2 software switch does not support if statements in the deparser as you have shown in your code example.

The more common way to write a deparser like this is just a series of emit statements, like this:

control MyDeparser(packet_out packet, in headers hdr) {
    apply {
        packet.emit(hdr.ethernet);
        packet.emit(hdr.ipv4);
        packet.emit(hdr.ipv6);
        packet.emit(hdr.tcp);
        packet.emit(hdr.udp);
    }
}

Note that the definition of the behavior of the emit method is "if the given header is valid, append the contents of the header to the output packet being processed now. Otherwise, do nothing."

git-liusen commented 5 months ago

Thanks!