Skip to content

Commit

Permalink
[fixes] updates based on tesing on Oxide p4 programs
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanlakhani committed Dec 10, 2024
1 parent 12f4d2b commit 91478f6
Show file tree
Hide file tree
Showing 20 changed files with 19,328 additions and 12,606 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# tree-sitter-p4
# tree-sitter-p4: P4 grammar for tree-sitter

P4 grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter).

## Setup

To build the parser, you need to have `tree-sitter` installed. You can install
it using most OS package managers, from source, or through `npm`:

```sh
npm install tree-sitter-cli
```

or with `cargo`:

```sh
cargo install tree-sitter-cli
```

## Contributing

This project is still in its early stages, so there are many things that can be
improved, as we aim toward capturing more of the [`P4_16` specification][p4-16-spec].

To contribute, just open a pull request!

[p4-16-spec]: https://p4.org/p4-spec/docs/P4-16-v1.0.0-spec.html
13 changes: 13 additions & 0 deletions examples/action-toplevel.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
action multicast_inbound() {
hdr.sidecar.sc_code = 4;
hdr.sidecar.sc_ingress = (bit<16>) meta.in_port;
hdr.sidecar.sc_egress = (bit<16>) ig_tm_md.ucast_egress_port;
hdr.sidecar.sc_ether_type = hdr.ethernet.ether_type;
hdr.sidecar.sc_payload = 0;
hdr.sidecar.setValid();
hdr.ethernet.ether_type = ETHERTYPE_SIDECAR;
meta.routed = false;
meta.service_routed = true;
ig_tm_md.ucast_egress_port = USER_SPACE_SERVICE_PORT;
meta.multicast = true;
}
8 changes: 8 additions & 0 deletions examples/control_counters.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
control Services(
inout sidecar_headers_t hdr,
inout sidecar_ingress_meta_t meta,
inout ingress_intrinsic_metadata_for_deparser_t ig_dprsr_md,
inout ingress_intrinsic_metadata_for_tm_t ig_tm_md)
{
Counter<bit<32>, bit<8>>(SVC_COUNTER_MAX, CounterType_t.PACKETS) service_ctr;
}
72 changes: 72 additions & 0 deletions examples/controller_multiple_instantiation.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <core.p4>
#include <softnpu.p4>

SoftNPU(
parse(),
ingress(),
egress()
) main;

struct headers_t { }

parser parse(
packet_in pkt,
out headers_t headers,
inout ingress_metadata_t ingress,
){
state start { transition accept; }
}

control resolver(
in bit<32> x,
out bool resolved,
) {
action resolve() {
resolved = true;
}
table arp {
key = { x: exact; }
actions = { resolve; }
default_action = NoAction;
}
apply { arp.apply(); }
}

control foo() {
resolver() resolver;
apply {
//bool resolved;
resolver.apply(32w47, resolved);
}
}

control bar() {
// resolver() resolver;
// resolver() taco;
// apply {
// bool resolved;
// resolver.apply(32w1701, resolved);
// }
}

control ingress(
inout headers_t hdr,
inout ingress_metadata_t ingress,
inout egress_metadata_t egress,
) {
foo() taco;
bar() pizza;

apply {
taco.apply();
pizza.apply();
}
}

control egress(
inout headers_t hdr,
inout ingress_metadata_t ingress,
inout egress_metadata_t egress,
) {

}
14 changes: 14 additions & 0 deletions examples/core.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This is core.p4
*/
extern packet_in {
void extract<T>(out T headerLvalue);
void extract<T>(out T variableSizeHeader, in bit<32> varFieldSizeBits);
T lookahead<T>();
bit<32> length(); // This method may be unavailable in some architectures
void advance(bit<32> bits);
}

extern packet_out {
void emit<T>(in T hdr);
}
Loading

0 comments on commit 91478f6

Please sign in to comment.