Skip to content

Named Parameters

Calin Cascaval edited this page Mar 4, 2018 · 8 revisions

Named parameters

The goal of this proposal is to extend the syntax of P4_16 to support named parameters to controls, tables, actions, externs, and packages. The main motivation is to enable optional parameters to enable reducing the overhead of listing all parameters and limit the ordering constraints.

Status quo

The p4c compiler currently supports optional parameters as an experimental feature. This is enabled by the @optional annotation followed by an ordered list of arguments. That is, if an optional argument is present, all preceeding arguments (non-optional and optional) must be present. Optional arguments apply to extern methods.

Design

We propose the following changes:

  • Calls specify parameters by name. By default, all parameters must be specified.
  • Optional parameters are specified by assigning a default parameter value: param_modifier param_type name = default_parameter_value
  • We allow specifying _ as the default value and the compiler will initialize the optional parameter with an appropriate value.
  • Mandatory parameters must preceed all optional parameters and all parameters must be called in the order in which they were declared. This is similar to C++, except that preceeding optional parameters can be fully omitted (as in Python).
  • We only allow optional parameters for some kinds of functions:
    • Constructors, including extern constructors and package constructors, controls, tables, and parsers
    • Extern methods
    • Actions
  • Optional parameters must be either in or directionless

Example

// Example of parser declaration in PSA
parser IngressParser<H, M, RESUBM, RECIRCM>(
    packet_in buffer,
    out H parsed_hdr,
    inout M user_meta,
    in psa_ingress_parser_input_metadata_t istd,
    in RESUBM resubmit_meta = _,
    in RECIRCM recirculate_meta = _);

// Program implementation of the parser (from psa-example-resubmit.p4)
parser IngressParserImpl(
    packet_in buffer,
    out headers parsed_hdr,
    inout metadata user_meta,
    in psa_ingress_parser_input_metadata_t istd,
    in resubmit_metadata_t resub_meta)
{
    CommonParser() cp;

    state start {
        transition select(istd.packet_path) {
           PSA_PacketPath_t.RESUBMIT: copy_resubmit_meta;
           PSA_PacketPath_t.NORMAL: packet_in_parsing;
        }
    }

    state copy_resubmit_meta {
        user_meta.resubmit_meta = resub_meta;
        transition packet_in_parsing;
    }

    state packet_in_parsing {
        cp.apply(buffer, parsed_hdr, user_meta);
        transition accept;
    }
}

Clone this wiki locally