Skip to content

Commit a0c5575

Browse files
P4 Infra Teamcopybara-github
authored andcommitted
Process native ecmp and load balance type
PiperOrigin-RevId: 795643224
1 parent 5be3cfd commit a0c5575

File tree

9 files changed

+1115
-1095
lines changed

9 files changed

+1115
-1095
lines changed

p4_pdpi/ir.cc

Lines changed: 348 additions & 344 deletions
Large diffs are not rendered by default.

p4_pdpi/ir.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,26 @@ message IrActionSetInvocation {
447447
string watch_port = 3;
448448
}
449449

450+
enum BucketSelectionMode {
451+
DEFAULT_BUCKET_DETERMINED_BY_ACTION_SELECTOR_MODE = 0;
452+
HASH_MODE = 1;
453+
RANDOM_MODE = 2;
454+
}
455+
456+
// Describes the size semantics of an action set.
457+
enum ResourceAllocationMode {
458+
DEFAULT_ALLOCATION_DETERMINED_BY_ACTION_SELECTOR_MODE = 0;
459+
SUM_OF_WEIGHTS_MODE = 1;
460+
SUM_OF_MEMBERS_MODE = 2;
461+
}
462+
450463
// Describes an action profile action set (for WCMP tables).
451464
message IrActionSet {
452465
// Required. The set of actions.
453466
repeated IrActionSetInvocation actions = 1;
467+
468+
BucketSelectionMode bucket_selection_mode = 2;
469+
ResourceAllocationMode resource_accounting_type = 3;
454470
}
455471

456472
// Describes a table entry (matches, priority and action, plus some metadata).

p4_pdpi/ir_utils.cc

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ using ::pdpi::Format;
6666
using ::pdpi::IrValue;
6767

6868
absl::StatusOr<std::string> ArbitraryToNormalizedByteString(
69-
const std::string &bytes, int expected_bitwidth) {
69+
const std::string& bytes, int expected_bitwidth) {
7070
// If the bytestring length is zero, the server always rejects the string.
7171
// https://p4.org/p4-spec/p4runtime/main/P4Runtime-Spec.html#sec-bytestrings
7272
if (bytes.empty()) {
@@ -87,15 +87,15 @@ absl::StatusOr<std::string> ArbitraryToNormalizedByteString(
8787
canonical_string);
8888
}
8989

90-
absl::StatusOr<uint64_t> ArbitraryByteStringToUint(const std::string &bytes,
90+
absl::StatusOr<uint64_t> ArbitraryByteStringToUint(const std::string& bytes,
9191
int bitwidth) {
9292
if (bitwidth > 64) {
9393
return absl::Status(absl::StatusCode::kInvalidArgument,
9494
absl::StrCat("Cannot convert value with "
9595
"bitwidth ",
9696
bitwidth, " to uint."));
9797
}
98-
ASSIGN_OR_RETURN(const auto &stripped_value,
98+
ASSIGN_OR_RETURN(const auto& stripped_value,
9999
ArbitraryToNormalizedByteString(bytes, bitwidth));
100100
uint64_t nb_value; // network byte order
101101
char value[sizeof(nb_value)];
@@ -121,19 +121,19 @@ absl::StatusOr<std::string> UintToNormalizedByteString(uint64_t value,
121121
std::string bytes = "";
122122
if (bitwidth <= 8) {
123123
uint8_t tmp = static_cast<uint8_t>(value);
124-
bytes.assign(reinterpret_cast<char *>(&tmp), sizeof(uint8_t));
124+
bytes.assign(reinterpret_cast<char*>(&tmp), sizeof(uint8_t));
125125
} else if (bitwidth <= 16) {
126126
uint16_t tmp = htons(static_cast<uint16_t>(value));
127-
bytes.assign(reinterpret_cast<char *>(&tmp), sizeof(uint16_t));
127+
bytes.assign(reinterpret_cast<char*>(&tmp), sizeof(uint16_t));
128128
} else if (bitwidth <= 32) {
129129
uint32_t tmp = htonl(static_cast<uint32_t>(value));
130-
bytes.assign(reinterpret_cast<char *>(&tmp), sizeof(uint32_t));
130+
bytes.assign(reinterpret_cast<char*>(&tmp), sizeof(uint32_t));
131131
} else {
132132
uint64_t tmp =
133133
(htonl(1) == 1)
134134
? value
135135
: (static_cast<uint64_t>(htonl(value)) << 32) | htonl(value >> 32);
136-
bytes.assign(reinterpret_cast<char *>(&tmp), sizeof(uint64_t));
136+
bytes.assign(reinterpret_cast<char*>(&tmp), sizeof(uint64_t));
137137
}
138138

139139
ASSIGN_OR_RETURN(auto normalized_str,
@@ -148,13 +148,13 @@ std::string ArbitraryToCanonicalByteString(std::string bytes) {
148148
return bytes;
149149
}
150150

151-
absl::StatusOr<Format> GetFormat(const std::vector<std::string> &annotations,
151+
absl::StatusOr<Format> GetFormat(const std::vector<std::string>& annotations,
152152
const int bitwidth, bool is_sdn_string) {
153153
Format format = Format::HEX_STRING;
154154
if (is_sdn_string) {
155155
format = Format::STRING;
156156
}
157-
for (const std::string &annotation : annotations) {
157+
for (const std::string& annotation : annotations) {
158158
if (absl::StartsWith(annotation, "@format(")) {
159159
if (format != Format::HEX_STRING) {
160160
return gutil::InvalidArgumentErrorBuilder()
@@ -189,7 +189,7 @@ absl::StatusOr<Format> GetFormat(const std::vector<std::string> &annotations,
189189

190190
absl::StatusOr<IrValue> ArbitraryByteStringToIrValue(Format format,
191191
const int bitwidth,
192-
const std::string &bytes) {
192+
const std::string& bytes) {
193193
// If the bytestring length is zero, the server always rejects the string.
194194
// https://p4.org/p4-spec/p4runtime/main/P4Runtime-Spec.html#sec-bytestrings
195195
if (bytes.empty()) {
@@ -247,9 +247,9 @@ absl::StatusOr<IrValue> ArbitraryByteStringToIrValue(Format format,
247247
}
248248
}
249249

250-
absl::Status ValidateIrValueFormat(const IrValue &ir_value, Format format,
251-
const TranslationOptions &options) {
252-
const auto &format_case = ir_value.format_case();
250+
absl::Status ValidateIrValueFormat(const IrValue& ir_value, Format format,
251+
const TranslationOptions& options) {
252+
const auto& format_case = ir_value.format_case();
253253
ASSIGN_OR_RETURN(const std::string format_case_name,
254254
gutil::GetOneOfFieldName(ir_value, std::string("format")));
255255
switch (format) {
@@ -307,7 +307,7 @@ absl::Status ValidateIrValueFormat(const IrValue &ir_value, Format format,
307307
}
308308

309309
absl::StatusOr<std::string> IrValueToNormalizedByteString(
310-
const IrValue &ir_value, const int bitwidth) {
310+
const IrValue& ir_value, const int bitwidth) {
311311
switch (ir_value.format_case()) {
312312
case IrValue::kMac: {
313313
ASSIGN_OR_RETURN(MacAddress mac, MacAddress::OfString(ir_value.mac()));
@@ -341,7 +341,7 @@ absl::StatusOr<std::string> IrValueToNormalizedByteString(
341341
break;
342342
}
343343
case IrValue::kHexStr: {
344-
const std::string &hex_str = ir_value.hex_str();
344+
const std::string& hex_str = ir_value.hex_str();
345345
const int expected_num_hex_chars =
346346
bitwidth / 4 + (bitwidth % 4 != 0 ? 1 : 0);
347347
if (!absl::StartsWith(hex_str, "0x")) {
@@ -377,7 +377,7 @@ absl::StatusOr<std::string> IrValueToNormalizedByteString(
377377
<< "Uninitialized value: " << ir_value.DebugString();
378378
}
379379

380-
absl::StatusOr<IrValue> FormattedStringToIrValue(const std::string &value,
380+
absl::StatusOr<IrValue> FormattedStringToIrValue(const std::string& value,
381381
Format format) {
382382
IrValue result;
383383
switch (format) {
@@ -403,7 +403,7 @@ absl::StatusOr<IrValue> FormattedStringToIrValue(const std::string &value,
403403
return result;
404404
}
405405

406-
std::string IrValueString(const IrValue &value) {
406+
std::string IrValueString(const IrValue& value) {
407407
switch (value.format_case()) {
408408
case IrValue::FormatCase::kMac:
409409
return value.mac();
@@ -421,9 +421,9 @@ std::string IrValueString(const IrValue &value) {
421421
return "";
422422
}
423423

424-
bool IsAllZeros(const std::string &s) {
424+
bool IsAllZeros(const std::string& s) {
425425
bool has_non_zero_value = false;
426-
for (const auto &c : s) {
426+
for (const auto& c : s) {
427427
if (c != '\x00') {
428428
has_non_zero_value = true;
429429
break;
@@ -433,8 +433,8 @@ bool IsAllZeros(const std::string &s) {
433433
return has_non_zero_value == false;
434434
}
435435

436-
absl::StatusOr<std::string> Intersection(const std::string &left,
437-
const std::string &right) {
436+
absl::StatusOr<std::string> Intersection(const std::string& left,
437+
const std::string& right) {
438438
if (left.size() != right.size()) {
439439
return gutil::InvalidArgumentErrorBuilder()
440440
<< "Cannot find intersection. '" << absl::CEscape(left) << "'("
@@ -488,7 +488,7 @@ absl::Status IsGoogleRpcCode(int rpc_code) {
488488
}
489489

490490
absl::Status ValidateGenericUpdateStatus(google::rpc::Code code,
491-
const std::string &message) {
491+
const std::string& message) {
492492
if (code == google::rpc::OK && !message.empty()) {
493493
return absl::InvalidArgumentError(
494494
"OK status should not contain error message.");
@@ -497,11 +497,11 @@ absl::Status ValidateGenericUpdateStatus(google::rpc::Code code,
497497
}
498498

499499
std::string IrWriteResponseToReadableMessage(
500-
const IrWriteResponse &ir_write_response) {
500+
const IrWriteResponse& ir_write_response) {
501501
std::string readable_message;
502502
absl::StrAppend(&readable_message, "Batch failed, individual results:\n");
503503
int i = 1;
504-
for (const auto &ir_update_status : ir_write_response.statuses()) {
504+
for (const auto& ir_update_status : ir_write_response.statuses()) {
505505
absl::StrAppend(&readable_message, "#", i, ": ",
506506
absl::StatusCodeToString(static_cast<absl::StatusCode>(
507507
ir_update_status.code())));
@@ -559,7 +559,7 @@ std::string MetadataName(absl::string_view metadata_name) {
559559
}
560560

561561
bool IsElementDeprecated(
562-
const google::protobuf::RepeatedPtrField<std::string> &annotations) {
562+
const google::protobuf::RepeatedPtrField<std::string>& annotations) {
563563
return absl::c_any_of(annotations, [](absl::string_view annotation) {
564564
return absl::StartsWith(annotation, "@deprecated");
565565
});
@@ -568,7 +568,7 @@ bool IsElementDeprecated(
568568
namespace {
569569
// Compress and return a match field into a unique, descriptive short-form
570570
// string.
571-
std::string MatchFieldShortDescription(const IrMatch &match) {
571+
std::string MatchFieldShortDescription(const IrMatch& match) {
572572
switch (match.match_value_case()) {
573573
case IrMatch::MatchValueCase::kExact:
574574
return absl::Substitute("$0=$1", match.name(),
@@ -592,10 +592,10 @@ std::string MatchFieldShortDescription(const IrMatch &match) {
592592

593593
// Compress and return an action invocation into a unique, descriptive
594594
// short-form string.
595-
std::string ActionInvocationShortDescription(const IrActionInvocation &action) {
595+
std::string ActionInvocationShortDescription(const IrActionInvocation& action) {
596596
if (action.params().empty()) return action.name();
597597
absl::btree_set<std::string> action_params;
598-
for (const IrActionInvocation::IrActionParam &param : action.params()) {
598+
for (const IrActionInvocation::IrActionParam& param : action.params()) {
599599
action_params.insert(
600600
absl::Substitute("$0=$1", param.name(), IrValueString(param.value())));
601601
}
@@ -605,9 +605,9 @@ std::string ActionInvocationShortDescription(const IrActionInvocation &action) {
605605

606606
// Compress and return an action set into a unique, descriptive short-form
607607
// string.
608-
std::string ActionSetShortDescription(const IrActionSet &action_set) {
608+
std::string ActionSetShortDescription(const IrActionSet& action_set) {
609609
absl::btree_set<std::string> actions;
610-
for (const IrActionSetInvocation &invocation : action_set.actions()) {
610+
for (const IrActionSetInvocation& invocation : action_set.actions()) {
611611
actions.insert(absl::Substitute(
612612
"$0$1[$2]",
613613
invocation.watch_port().empty()
@@ -620,9 +620,9 @@ std::string ActionSetShortDescription(const IrActionSet &action_set) {
620620
}
621621
} // namespace
622622

623-
std::string ShortDescription(const IrTableEntry &entry) {
623+
std::string ShortDescription(const IrTableEntry& entry) {
624624
absl::btree_set<std::string> match_fields;
625-
for (const IrMatch &match : entry.matches()) {
625+
for (const IrMatch& match : entry.matches()) {
626626
match_fields.insert(MatchFieldShortDescription(match));
627627
}
628628

p4_pdpi/ir_utils.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,33 @@ constexpr absl::string_view kIndent = " ";
6262

6363
// Returns the format for value, given the annotations on it, it's bitwidth
6464
// and named type (if any).
65-
absl::StatusOr<Format> GetFormat(const std::vector<std::string> &annotations,
65+
absl::StatusOr<Format> GetFormat(const std::vector<std::string>& annotations,
6666
const int bitwidth, bool is_sdn_string);
6767

6868
// Checks if the IrValue in the IR table entry is in the same format as
6969
// specified in the P4Info. If "allow_arbitrary_format" in the translation
7070
// options is true, any format is accepted.
71-
absl::Status ValidateIrValueFormat(const IrValue &ir_value, Format format,
72-
const TranslationOptions &options);
71+
absl::Status ValidateIrValueFormat(const IrValue& ir_value, Format format,
72+
const TranslationOptions& options);
7373

7474
// Converts the IR value to a PI byte string and returns it.
7575
absl::StatusOr<std::string> IrValueToNormalizedByteString(
76-
const IrValue &ir_value, const int bitwidth);
76+
const IrValue& ir_value, const int bitwidth);
7777

7878
// Converts the PI value to an IR value and returns it.
7979
absl::StatusOr<IrValue> ArbitraryByteStringToIrValue(Format format,
8080
const int bitwidth,
81-
const std::string &bytes);
81+
const std::string& bytes);
8282

8383
// Returns an IrValue based on a string value and a format. The value is
8484
// expected to already be formatted correctly, and is just copied to the correct
8585
// oneof field.
86-
absl::StatusOr<IrValue> FormattedStringToIrValue(const std::string &value,
86+
absl::StatusOr<IrValue> FormattedStringToIrValue(const std::string& value,
8787
Format format);
8888

8989
// Returns the string contents of an IrValue for the populated format (or "" if
9090
// there is no data).
91-
std::string IrValueString(const IrValue &value);
91+
std::string IrValueString(const IrValue& value);
9292

9393
// Return a short-form representation of an IrTableEntry.
9494
// This is useful for creating a unique, short description of the table entry
@@ -97,30 +97,30 @@ std::string IrValueString(const IrValue &value);
9797
// names.
9898
//
9999
// Does not include metadata, meter, and counters.
100-
std::string ShortDescription(const IrTableEntry &entry);
100+
std::string ShortDescription(const IrTableEntry& entry);
101101

102102
// Returns a string of length ceil(expected_bitwidth/8).
103103
absl::StatusOr<std::string> ArbitraryToNormalizedByteString(
104-
const std::string &bytes, int expected_bitwidth);
104+
const std::string& bytes, int expected_bitwidth);
105105

106106
// Convert an arbitrary byte string to its canonical form.
107107
// TODO: smolkaj - Move to byte_string.h and rename appropriately.
108108
std::string ArbitraryToCanonicalByteString(std::string bytes);
109109

110110
// Convert the given byte string into a uint value.
111-
absl::StatusOr<uint64_t> ArbitraryByteStringToUint(const std::string &bytes,
111+
absl::StatusOr<uint64_t> ArbitraryByteStringToUint(const std::string& bytes,
112112
int bitwidth);
113113

114114
// Convert the given uint to byte string.
115115
absl::StatusOr<std::string> UintToNormalizedByteString(uint64_t value,
116116
int bitwidth);
117117

118118
// Returns if a (normalized) byte string is all zeros.
119-
bool IsAllZeros(const std::string &s);
119+
bool IsAllZeros(const std::string& s);
120120

121121
// Returns the intersection of two (normalized) byte strings.
122-
absl::StatusOr<std::string> Intersection(const std::string &left,
123-
const std::string &right);
122+
absl::StatusOr<std::string> Intersection(const std::string& left,
123+
const std::string& right);
124124

125125
// Returns the (normalized) mask for a given prefix length.
126126
absl::StatusOr<std::string> PrefixLenToMask(int prefix_len, int bitwidth);
@@ -130,10 +130,10 @@ absl::Status IsGoogleRpcCode(int rpc_code);
130130
// 1: If `code` is ok, `message` should be empty.
131131
// 2: If `code` is not ok, `message` should not be empty.
132132
absl::Status ValidateGenericUpdateStatus(google::rpc::Code code,
133-
const std::string &message);
133+
const std::string& message);
134134
// Parses IrUpdateStatus inside of `ir_write_response`` into string.
135135
std::string IrWriteResponseToReadableMessage(
136-
const IrWriteResponse &ir_write_response);
136+
const IrWriteResponse& ir_write_response);
137137

138138
// Returns a formatted error message that can be inserted directly into a
139139
// status.
@@ -161,7 +161,7 @@ std::string MetadataName(absl::string_view metadata_name);
161161

162162
// Checks for an "@deprecated" annotation in the argument.
163163
bool IsElementDeprecated(
164-
const google::protobuf::RepeatedPtrField<std::string> &annotations);
164+
const google::protobuf::RepeatedPtrField<std::string>& annotations);
165165

166166
} // namespace pdpi
167167

p4_pdpi/p4info_union_lib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace pdpi {
3333
// - Tables, which will have their match fields, action refs,
3434
// direct resource ids and preambles unioned.
3535
absl::StatusOr<::p4::config::v1::P4Info> UnionP4info(
36-
const std::vector<::p4::config::v1::P4Info> &infos);
36+
const std::vector<::p4::config::v1::P4Info>& infos);
3737

3838
} // namespace pdpi
3939

0 commit comments

Comments
 (0)