Skip to content

Commit

Permalink
Update README with improved API and action constraints.
Browse files Browse the repository at this point in the history
  • Loading branch information
smolkaj committed May 6, 2024
1 parent 5371b09 commit c9e6b4d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,44 @@ should not require general ternary matches on the destination address; the
constraint documents this intend and let's us catch accidental ternary matches
installed by the control plane at runtime.

## Example - Action Restrictions

An *action restriction* is similar to an entry restriction, but is placed on a
P4 action:
```p4
// Disallow mulitcast group ID 0, since it indicates "no mulitcast" in
// `v1model.p4`.
@action_restriction("multicast_group_id != 0")
action multicast(bit<16> multicast_group_id) {
standard_metadata.mcast_grp = multicast_group_id;
}
```

## API

At a high level, p4-constraint's API consists of only two functions:
one function for parsing constraints and one function for checking them.
```C++
/* p4_constraints/backend/constraint_info.h */

// Translates P4Info to ConstraintInfo.
// Translates `P4Info` to `ConstraintInfo`.
//
// Parses all tables and their constraint annotations into an in-memory
// representation suitable for constraint checking. Returns parsed
// representation, or a nonempty list of error statuses if parsing fails.
absl::variant<ConstraintInfo, std::vector<absl::Status>> P4ToConstraintInfo(
const p4::config::v1::P4Info& p4info);
// Parses all tables and actions and their p4-constraints annotations into an
// in-memory representation suitable for constraint checking. Returns parsed
// representation, or an error status if parsing fails.
absl::StatusOr<ConstraintInfo> P4ToConstraintInfo(
const p4::config::v1::P4Info &p4info);
```
```C++
/* p4_constraints/backend/interpreter.h */
// Checks if a given table entry satisfies the entry/action(s) constraint(s)
// attached to its associated table/action(s). Returns the empty string if this
// is the case or a human-readable nonempty string explaining why it is not the
// case otherwise. Returns an InvalidArgument Status if the entry/action(s)
// don't belong in ConstraintInfo or is inconsistent with the table/action(s)
// definition in ConstraintInfo.
// Checks if a given table entry satisfies the constraints attached to its
// associated table/action.
//
// Returns the empty string if this is the case, or a human-readable nonempty
// string explaining why it is not the case otherwise. Returns an
// `InvalidArgument` if the entry's table or action is not defined in
// `ConstraintInfo`, or if `entry` is inconsistent with these definitions.
absl::StatusOr<std::string> ReasonEntryViolatesConstraint(
const p4::v1::TableEntry& entry, const ConstraintInfo& constraint_info);
```
Expand Down
8 changes: 4 additions & 4 deletions p4_constraints/backend/constraint_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ struct ConstraintInfo {
absl::flat_hash_map<uint32_t, TableInfo> table_info_by_id;
};

// Translates P4Info to ConstraintInfo.
// Translates `P4Info` to `ConstraintInfo`.
//
// Parses all tables and their constraint annotations into an in-memory
// representation suitable for constraint checking. Returns parsed
// representation, or an error statuses if parsing fails.
// Parses all tables and actions and their p4-constraints annotations into an
// in-memory representation suitable for constraint checking. Returns parsed
// representation, or an error status if parsing fails.
absl::StatusOr<ConstraintInfo> P4ToConstraintInfo(
const p4::config::v1::P4Info& p4info);

Expand Down
13 changes: 7 additions & 6 deletions p4_constraints/backend/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@

namespace p4_constraints {

// Checks if a given table entry satisfies the entry/action(s) constraint(s)
// attached to its associated table/action(s). Returns the empty string if this
// is the case or a human-readable nonempty string explaining why it is not the
// case otherwise. Returns an InvalidArgument Status if the entry/action(s)
// don't belong in ConstraintInfo or is inconsistent with the table/action(s)
// definition in ConstraintInfo.
// Checks if a given table entry satisfies the constraints attached to its
// associated table/action.
//
// Returns the empty string if this is the case, or a human-readable nonempty
// string explaining why it is not the case otherwise. Returns an
// `InvalidArgument` if the entry's table or action is not defined in
// `ConstraintInfo`, or if `entry` is inconsistent with these definitions.
absl::StatusOr<std::string> ReasonEntryViolatesConstraint(
const p4::v1::TableEntry& entry, const ConstraintInfo& constraint_info);

Expand Down

0 comments on commit c9e6b4d

Please sign in to comment.