Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support if statements in parsers #2386

Merged
merged 1 commit into from
May 20, 2020
Merged

Conversation

mihaibudiu
Copy link
Contributor

@vgurevich : this is actually a two-line change to the compiler.
We should add this to the spec as well; right now it's labeled as experimental.

@hesingh
Copy link
Contributor

hesingh commented May 20, 2020

It would be good to add a packet test to this PR changes, thanks.

Copy link

@vgurevich vgurevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@mihaibudiu
Copy link
Contributor Author

I don't see any reason not to merge this.

@mihaibudiu mihaibudiu merged commit d048acd into p4lang:master May 20, 2020
@mihaibudiu mihaibudiu deleted the ifParser branch May 20, 2020 19:51
@hksoni
Copy link

hksoni commented Jul 7, 2020

I modified the example in 5.3 of the spec to show possible use of transition statement in body of If-stmt.

  1. will transition statements be allowed in if-else body?
  2. Currently states have only one explicit conditional, select expression, that can be only at the end of the state. Having multiple conditional would essential create a DAG within the state. If the transfer of control is outside the state using transition statement, compilers need to do some work to some more work.

look at parse_ipv4 state.

parser TopParser(packet_in b, out Parsed_packet p) {
    Checksum16() ck;  // instantiate checksum unit

    state start {
        b.extract(p.ethernet);
        transition select(p.ethernet.etherType) {
            0x0800: parse_ipv4;
            // no default rule: all other packets rejected
        }
    }

    state parse_ipv4 {
        b.extract(p.ip);
        if (p.ip.version == 4w4) {
            transition  my_error_state;
        }
        verify(p.ip.ihl == 4w5, error.IPv4OptionsNotSupported);
        ck.clear();
        ck.update(p.ip);
        // Verify that packet checksum is zero
        verify(ck.get() == 16w0, error.IPv4ChecksumError);
        transition accept;
    }
    state my_error_state {
        // update user-defined metadata
    }
}

@mihaibudiu
Copy link
Contributor Author

transition is still allowed only as the last statement of a state, and it cannot be conditional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants