Skip to content

Commit

Permalink
Handle pna_main_parser_input_metadata_t fields in the tc backend's pa…
Browse files Browse the repository at this point in the history
…rser (#4955)

* Handle pna_main_parser_input_metadata_t fields in the tc backend's parser

As of now, referencing a field of pna_main_parser_input_metadata_t will
cause the it to be generated in eBPF without any translation.

For example:

parser Ingress_Parser(
        packet_in pkt,
        out   my_ingress_headers_t  hdr,
        inout my_ingress_metadata_t meta,
        in    pna_main_parser_input_metadata_t istd)
{
    state mystate {
        ...
        meta->ingress_port = istd.input_port;
        ...
    }
    ...
}

Will generate something like:

mystate: {
    ...
    meta->ingress_port = istd.input_port;
    ...
}

This will cause a compilation error because istd is not defined in the ebpf
code. This commit fixes this by either translating the fields to something
ebpf understands or simply throwing an error if the field is unsupported.

Signed-off-by: Victor Nogueira <[email protected]>

* Add example to illustrate accessing parser metadata

Signed-off-by: Victor Nogueira <[email protected]>

---------

Signed-off-by: Victor Nogueira <[email protected]>
  • Loading branch information
vbnogueira authored Oct 21, 2024
1 parent 738c038 commit da04995
Show file tree
Hide file tree
Showing 10 changed files with 898 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backends/tc/ebpfCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,24 @@ void PnaStateTranslationVisitor::compileExtractField(const IR::Expression *expr,
builder->newline();
}

bool PnaStateTranslationVisitor::preorder(const IR::Member *m) {
if ((m->expr != nullptr) && (m->expr->type != nullptr)) {
if (auto st = m->expr->type->to<IR::Type_Struct>()) {
if (st->name == "pna_main_parser_input_metadata_t") {
if (m->member.name == "input_port") {
builder->append("skb->ifindex");
return false;
} else {
::P4::error(ErrorType::ERR_UNSUPPORTED_ON_TARGET,
"%1%: this metadata field is not supported", m);
}
}
}
}

return EBPF::PsaStateTranslationVisitor::preorder(m);
}

void PnaStateTranslationVisitor::compileLookahead(const IR::Expression *destination) {
cstring msgStr = absl::StrFormat("Parser: lookahead for %s %s",
state->parser->typeMap->getType(destination)->toString(),
Expand Down
2 changes: 2 additions & 0 deletions backends/tc/ebpfCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class PnaStateTranslationVisitor : public EBPF::PsaStateTranslationVisitor {
EBPF::EBPFPsaParser *prsr)
: EBPF::PsaStateTranslationVisitor(refMap, typeMap, prsr) {}

bool preorder(const IR::Member *expression) override;

protected:
void compileExtractField(const IR::Expression *expr, const IR::StructField *field,
unsigned hdrOffsetBits, EBPF::EBPFType *type) override;
Expand Down
1 change: 1 addition & 0 deletions p4include/tc/pna.p4
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ enum PNA_Source_t {
struct pna_main_parser_input_metadata_t {
// common fields initialized for all packets that are input to main
// parser, regardless of direction.
// Unsupported as of now.
bool recirculated;
// If this packet has FROM_NET source, input_port contains
// the id of the network port on which the packet arrived.
Expand Down
144 changes: 144 additions & 0 deletions testdata/p4tc_samples/digest_parser_meta.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* -*- P4_16 -*- */

#include <core.p4>
#include <tc/pna.p4>

struct my_ingress_metadata_t {
@tc_type("dev") PortId_t ingress_port;
}

/*
* CONST VALUES FOR TYPES
*/
const bit<8> IP_PROTO_TCP = 0x06;
const bit<16> ETHERTYPE_IPV4 = 0x0800;

/*
* Standard ethernet header
*/
header ethernet_t {
@tc_type("macaddr") bit<48> dstAddr;
@tc_type("macaddr") 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;
@tc_type("ipv4") bit<32> srcAddr;
@tc_type("ipv4") bit<32> dstAddr;
}

struct my_ingress_headers_t {
ethernet_t ethernet;
ipv4_t ipv4;
}

/*********************** P A R S E R **************************/
parser Ingress_Parser(
packet_in pkt,
out my_ingress_headers_t hdr,
inout my_ingress_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.ipv4);
meta.ingress_port = istd.input_port;
transition accept;
}
}

#define L3_TABLE_SIZE 2048

struct ipv4_learn_digest_t {
@tc_type("ipv4") bit<32> srcAddr;
@tc_type("dev") PortId_t ingress_port;
};

/***************** M A T C H - A C T I O N *********************/

control ingress(
inout my_ingress_headers_t hdr,
inout my_ingress_metadata_t meta,
in pna_main_input_metadata_t istd,
inout pna_main_output_metadata_t ostd
)
{
action send_nh(@tc_type("dev") PortId_t port, @tc_type("macaddr") bit<48> srcMac, @tc_type("macaddr") bit<48> dstMac) {
hdr.ethernet.srcAddr = srcMac;
hdr.ethernet.dstAddr = dstMac;
send_to_port(port);
}

action drop() {
drop_packet();
}

table nh_table {
key = {
hdr.ipv4.dstAddr : exact @tc_type("ipv4") @name("dstAddr");
}
actions = {
send_nh;
drop;
}
size = L3_TABLE_SIZE;
const default_action = drop;
}

apply {
if (hdr.ipv4.isValid()) {
nh_table.apply();
}
}
}

/********************* D E P A R S E R ************************/

control Ingress_Deparser(
packet_out pkt,
inout my_ingress_headers_t hdr,
in my_ingress_metadata_t meta,
in pna_main_output_metadata_t ostd)
{
Digest<ipv4_learn_digest_t>() digest_inst;

apply {
ipv4_learn_digest_t ipv4_learn_digest;

pkt.emit(hdr.ethernet);
pkt.emit(hdr.ipv4);

ipv4_learn_digest.srcAddr = hdr.ipv4.srcAddr;
ipv4_learn_digest.ingress_port = meta.ingress_port;
digest_inst.pack(ipv4_learn_digest);
}
}

/************ F I N A L P A C K A G E ******************************/

PNA_NIC(
Ingress_Parser(),
ingress(),
Ingress_Deparser()
) main;
98 changes: 98 additions & 0 deletions testdata/p4tc_samples_outputs/digest_parser_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"schema_version" : "1.0.0",
"pipeline_name" : "digest_parser_meta",
"externs" : [
{
"name" : "Digest",
"id" : "0x05000000",
"permissions" : "0x19b6",
"instances" : [
{
"inst_name" : "Ingress_Deparser.digest_inst",
"inst_id" : 1,
"params" : [
{
"id" : 1,
"name" : "index",
"type" : "bit32",
"attr" : "tc_key",
"bitwidth" : 32
},
{
"id" : 2,
"name" : "srcAddr",
"type" : "ipv4",
"attr" : "param",
"bitwidth" : 32
},
{
"id" : 3,
"name" : "ingress_port",
"type" : "dev",
"attr" : "param",
"bitwidth" : 32
}
]
}
]
}
],
"tables" : [
{
"name" : "ingress/nh_table",
"id" : 1,
"tentries" : 2048,
"permissions" : "0x3da4",
"nummask" : 8,
"keysize" : 32,
"keyfields" : [
{
"id" : 1,
"name" : "dstAddr",
"type" : "ipv4",
"match_type" : "exact",
"bitwidth" : 32
}
],
"actions" : [
{
"id" : 1,
"name" : "ingress/send_nh",
"action_scope" : "TableAndDefault",
"annotations" : [],
"params" : [
{
"id" : 1,
"name" : "port",
"type" : "dev",
"bitwidth" : 32
},
{
"id" : 2,
"name" : "srcMac",
"type" : "macaddr",
"bitwidth" : 48
},
{
"id" : 3,
"name" : "dstMac",
"type" : "macaddr",
"bitwidth" : 48
}
],
"default_hit_action" : false,
"default_miss_action" : false
},
{
"id" : 2,
"name" : "ingress/drop",
"action_scope" : "TableAndDefault",
"annotations" : [],
"params" : [],
"default_hit_action" : false,
"default_miss_action" : true
}
]
}
]
}
Empty file.
30 changes: 30 additions & 0 deletions testdata/p4tc_samples_outputs/digest_parser_meta.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash -x

set -e

: "${TC:="tc"}"
$TC p4template create pipeline/digest_parser_meta numtables 1

$TC p4template create action/digest_parser_meta/ingress/send_nh actid 1 \
param port type dev \
param srcMac type macaddr \
param dstMac type macaddr
$TC p4template update action/digest_parser_meta/ingress/send_nh state active

$TC p4template create action/digest_parser_meta/ingress/drop actid 2
$TC p4template update action/digest_parser_meta/ingress/drop state active

$TC p4template create extern/root/Digest extid 0x05000000 numinstances 1 tc_acl 0x19b6 has_exec_method

$TC p4template create extern_inst/digest_parser_meta/Digest/Ingress_Deparser.digest_inst instid 1 \
tc_numel 0 \
control_path tc_key index ptype bit32 id 1 param srcAddr ptype ipv4 id 2 param ingress_port ptype dev id 3

$TC p4template create table/digest_parser_meta/ingress/nh_table \
tblid 1 \
type exact \
keysz 32 permissions 0x3da4 tentries 2048 nummasks 1 \
table_acts act name digest_parser_meta/ingress/send_nh \
act name digest_parser_meta/ingress/drop
$TC p4template update table/digest_parser_meta/ingress/nh_table default_miss_action permissions 0x1024 action digest_parser_meta/ingress/drop
$TC p4template update pipeline/digest_parser_meta state ready
Loading

0 comments on commit da04995

Please sign in to comment.