Skip to content

Commit a91c69c

Browse files
P4 Infra Teamcopybara-github
authored andcommitted
[DVaaS][Minimization] Enable table-based bisection for minimization.
PiperOrigin-RevId: 786414117
1 parent d3c8e0a commit a91c69c

File tree

5 files changed

+292
-0
lines changed

5 files changed

+292
-0
lines changed

p4_pdpi/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ cc_library(
153153
# Disable default arguments internally. Using them in PDPI itself is very likely a bug.
154154
local_defines = ["PDPI_DISABLE_TRANSLATION_OPTIONS_DEFAULT"],
155155
deps = [
156+
":entity_keys",
156157
":ir_cc_proto",
157158
":names",
158159
":references",
@@ -171,6 +172,7 @@ cc_library(
171172
"@com_google_absl//absl/status",
172173
"@com_google_absl//absl/status:statusor",
173174
"@com_google_absl//absl/strings",
175+
"@com_google_absl//absl/types:optional",
174176
"@com_google_absl//absl/types:span",
175177
"@com_google_gutil//gutil:collections",
176178
"@com_google_gutil//gutil:status",

p4_pdpi/sequencing.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <optional>
2020
#include <queue>
2121
#include <string>
22+
#include <tuple>
2223
#include <utility>
2324
#include <vector>
2425

@@ -30,13 +31,15 @@
3031
#include "absl/status/status.h"
3132
#include "absl/status/statusor.h"
3233
#include "absl/strings/str_cat.h"
34+
#include "absl/types/optional.h"
3335
#include "absl/types/span.h"
3436
#include "boost/graph/adjacency_list.hpp"
3537
#include "google/protobuf/repeated_ptr_field.h"
3638
#include "gutil/collections.h"
3739
#include "gutil/status.h"
3840
#include "p4/config/v1/p4info.pb.h"
3941
#include "p4/v1/p4runtime.pb.h"
42+
#include "p4_pdpi/entity_keys.h"
4043
#include "p4_pdpi/ir.pb.h"
4144
#include "p4_pdpi/names.h"
4245
#include "p4_pdpi/references.h"
@@ -376,6 +379,34 @@ absl::Status StableSortEntities(const IrP4Info& info,
376379
return absl::OkStatus();
377380
}
378381

382+
absl::Status SortEntities(const IrP4Info& info,
383+
std::vector<p4::v1::Entity>& entities) {
384+
absl::Status status;
385+
absl::c_sort(entities,
386+
[&](const p4::v1::Entity& e1, const p4::v1::Entity& e2) {
387+
absl::StatusOr<EntityKey> k1 = EntityKey::MakeEntityKey(e1);
388+
absl::StatusOr<EntityKey> k2 = EntityKey::MakeEntityKey(e2);
389+
status.Update(k1.status());
390+
status.Update(k2.status());
391+
return status.ok() ? *k1 < *k2 : true;
392+
});
393+
RETURN_IF_ERROR(status);
394+
absl::c_stable_sort(
395+
entities, [&](const p4::v1::Entity& a, const p4::v1::Entity& b) {
396+
auto b_may_depend_on_a = GreaterThanInDependencyOrder(info, a, b);
397+
if (!b_may_depend_on_a.ok()) {
398+
LOG(ERROR) << "Failed to compare entities with error: "
399+
<< b_may_depend_on_a.status() << "\nEntities were:\n"
400+
<< a.DebugString() << "\n\n and \n\n"
401+
<< b.DebugString();
402+
return false;
403+
}
404+
return *b_may_depend_on_a;
405+
});
406+
407+
return absl::OkStatus();
408+
}
409+
379410
absl::Status StableSortUpdates(
380411
const IrP4Info& info,
381412
google::protobuf::RepeatedPtrField<p4::v1::Update>& updates,

p4_pdpi/sequencing.expected

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,9 @@ SortTest: Empty input
12121212
--- Sorted entities (output):
12131213
<empty>
12141214

1215+
--- Non-stable sorted entities (output):
1216+
<empty>
1217+
12151218
=========================================================================
12161219
SortTest: EntryWithOneKey{key-a} -> EntryThatRefersTo{key-a}
12171220
=========================================================================
@@ -1260,6 +1263,28 @@ referring_by_action_table_entry {
12601263
}
12611264
}
12621265

1266+
--- Non-stable sorted entities (output):
1267+
one_match_field_table_entry {
1268+
match {
1269+
id: "key-a"
1270+
}
1271+
action {
1272+
do_thing_4 {
1273+
}
1274+
}
1275+
}
1276+
1277+
referring_by_action_table_entry {
1278+
match {
1279+
val: "0x001"
1280+
}
1281+
action {
1282+
referring_to_one_match_field_action {
1283+
referring_id_1: "key-a"
1284+
}
1285+
}
1286+
}
1287+
12631288
=========================================================================
12641289
SortTest: EntryWithOneKey{key-a} -> EntryThatRefersTo{key-a, 0x002}
12651290
=========================================================================
@@ -1310,6 +1335,29 @@ referring_by_action_table_entry {
13101335
}
13111336
}
13121337

1338+
--- Non-stable sorted entities (output):
1339+
one_match_field_table_entry {
1340+
match {
1341+
id: "key-a"
1342+
}
1343+
action {
1344+
do_thing_4 {
1345+
}
1346+
}
1347+
}
1348+
1349+
referring_by_action_table_entry {
1350+
match {
1351+
val: "0x001"
1352+
}
1353+
action {
1354+
referring_to_two_match_fields_action {
1355+
referring_id_1: "key-a"
1356+
referring_id_2: "0x002"
1357+
}
1358+
}
1359+
}
1360+
13131361
=========================================================================
13141362
SortTest: EntryWithTwoKeys{key-a, 0x002} -> EntryThatRefersTo{key-a, 0x002}
13151363
=========================================================================
@@ -1362,6 +1410,30 @@ referring_by_action_table_entry {
13621410
}
13631411
}
13641412

1413+
--- Non-stable sorted entities (output):
1414+
two_match_fields_table_entry {
1415+
match {
1416+
id_1: "key-a"
1417+
id_2: "0x002"
1418+
}
1419+
action {
1420+
do_thing_4 {
1421+
}
1422+
}
1423+
}
1424+
1425+
referring_by_action_table_entry {
1426+
match {
1427+
val: "0x001"
1428+
}
1429+
action {
1430+
referring_to_two_match_fields_action {
1431+
referring_id_1: "key-a"
1432+
referring_id_2: "0x002"
1433+
}
1434+
}
1435+
}
1436+
13651437
=========================================================================
13661438
SortTest: EntryThatRefersTo{key-a, 0x002} -> EntryWithTwoKeys{key-c, 0x004}
13671439
=========================================================================
@@ -1414,6 +1486,30 @@ referring_by_action_table_entry {
14141486
}
14151487
}
14161488

1489+
--- Non-stable sorted entities (output):
1490+
two_match_fields_table_entry {
1491+
match {
1492+
id_1: "key-c"
1493+
id_2: "0x004"
1494+
}
1495+
action {
1496+
do_thing_4 {
1497+
}
1498+
}
1499+
}
1500+
1501+
referring_by_action_table_entry {
1502+
match {
1503+
val: "0x001"
1504+
}
1505+
action {
1506+
referring_to_two_match_fields_action {
1507+
referring_id_1: "key-a"
1508+
referring_id_2: "0x002"
1509+
}
1510+
}
1511+
}
1512+
14171513
=========================================================================
14181514
SortTest: Entries at same rank should maintain the original order.
14191515
=========================================================================
@@ -1470,6 +1566,32 @@ referring_by_action_table_entry {
14701566
}
14711567
}
14721568

1569+
--- Non-stable sorted entities (output):
1570+
packet_count_and_meter_table_entry {
1571+
match {
1572+
ipv4 {
1573+
value: "16.36.50.0"
1574+
prefix_length: 24
1575+
}
1576+
}
1577+
action {
1578+
packet_count_and_meter {
1579+
}
1580+
}
1581+
}
1582+
1583+
referring_by_action_table_entry {
1584+
match {
1585+
val: "0x001"
1586+
}
1587+
action {
1588+
referring_to_two_match_fields_action {
1589+
referring_id_1: "key-a"
1590+
referring_id_2: "0x002"
1591+
}
1592+
}
1593+
}
1594+
14731595
=========================================================================
14741596
SortTest: EntryWithOneKey{key-a} -> EntryThatRefersTo{key-a} , another entry
14751597
=========================================================================
@@ -1544,6 +1666,41 @@ referring_by_action_table_entry {
15441666
}
15451667
}
15461668

1669+
--- Non-stable sorted entities (output):
1670+
lpm2_table_entry {
1671+
match {
1672+
ipv6 {
1673+
value: "ffff::abcd:0:0"
1674+
prefix_length: 96
1675+
}
1676+
}
1677+
action {
1678+
NoAction {
1679+
}
1680+
}
1681+
}
1682+
1683+
one_match_field_table_entry {
1684+
match {
1685+
id: "key-a"
1686+
}
1687+
action {
1688+
do_thing_4 {
1689+
}
1690+
}
1691+
}
1692+
1693+
referring_by_action_table_entry {
1694+
match {
1695+
val: "0x001"
1696+
}
1697+
action {
1698+
referring_to_one_match_field_action {
1699+
referring_id_1: "key-a"
1700+
}
1701+
}
1702+
}
1703+
15471704
=========================================================================
15481705
SortTest: EntryWithOneKey{key-a} -> EntryThatRefersTo{key-a} ,EntryWithOneKey{key-b} -> EntryThatRefersTo{key-b}
15491706
=========================================================================
@@ -1638,6 +1795,51 @@ referring_by_action_table_entry {
16381795
}
16391796
}
16401797

1798+
--- Non-stable sorted entities (output):
1799+
two_match_fields_table_entry {
1800+
match {
1801+
id_1: "key-c"
1802+
id_2: "0x004"
1803+
}
1804+
action {
1805+
do_thing_4 {
1806+
}
1807+
}
1808+
}
1809+
1810+
one_match_field_table_entry {
1811+
match {
1812+
id: "key-a"
1813+
}
1814+
action {
1815+
do_thing_4 {
1816+
}
1817+
}
1818+
}
1819+
1820+
referring_by_action_table_entry {
1821+
match {
1822+
val: "0x001"
1823+
}
1824+
action {
1825+
referring_to_two_match_fields_action {
1826+
referring_id_1: "key-a"
1827+
referring_id_2: "0x002"
1828+
}
1829+
}
1830+
}
1831+
1832+
referring_by_action_table_entry {
1833+
match {
1834+
val: "0x001"
1835+
}
1836+
action {
1837+
referring_to_one_match_field_action {
1838+
referring_id_1: "key-a"
1839+
}
1840+
}
1841+
}
1842+
16411843
=========================================================================
16421844
SortTest: two_match_fields_table_entry -> referring_by_match_field_table_entry
16431845
=========================================================================
@@ -1688,6 +1890,29 @@ referring_by_match_field_table_entry {
16881890
}
16891891
}
16901892

1893+
--- Non-stable sorted entities (output):
1894+
two_match_fields_table_entry {
1895+
match {
1896+
id_1: "key-a"
1897+
id_2: "0x001"
1898+
}
1899+
action {
1900+
do_thing_4 {
1901+
}
1902+
}
1903+
}
1904+
1905+
referring_by_match_field_table_entry {
1906+
match {
1907+
referring_id_1: "key-a"
1908+
referring_id_2: "0x001"
1909+
}
1910+
action {
1911+
do_thing_4 {
1912+
}
1913+
}
1914+
}
1915+
16911916
=========================================================================
16921917
GetEntriesUnreachableFromRootsTest: Empty input generates no garbage.
16931918
=========================================================================

p4_pdpi/sequencing.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ absl::StatusOr<std::vector<std::vector<int>>> SequencePiUpdatesInPlace(
7070
absl::Status StableSortEntities(const IrP4Info& info,
7171
std::vector<p4::v1::Entity>& entities);
7272

73+
// Sorts the entities such that entities that may be depended on come first.
74+
// That is, two entities x and y where x could refer to y will be sorted
75+
// as [y, x]. This is done based on the dependency ranks given in the IrP4Info.
76+
// Any entities with the same dependency rank are sorted by their entity key
77+
// value.
78+
absl::Status SortEntities(const IrP4Info& info,
79+
std::vector<p4::v1::Entity>& entities);
80+
7381
// Same as StableSortEntities but sorts the repeated `Update` message.
7482
absl::Status StableSortUpdates(
7583
const IrP4Info& info,

0 commit comments

Comments
 (0)