Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions netkat/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ Policy Modify(absl::string_view field, int new_value);
// Sequence({Sequence({Sequence({p0, p1}), p2}), p3})
//
// Semantically this behaves like a function composition in which, for some
// list p0...pn, we feed the preceeding program inputs into p0 and forward each
// list p0...pn, we feed the preceding program inputs into p0 and forward each
// of p0's outputs into p1, we then forward each of p1's outputs into p2, etc.
//
// Note that this means Sequence(p0, p1) and Sequence(p1, p0) may be
Expand Down Expand Up @@ -281,7 +281,7 @@ Policy Sequence(T&&... policies) {
Policy Union(std::vector<Policy> policies);

// Allows callers to Union policies without wrapping them in a list. Prefer
// this overload when reasonble. For example, instead of
// this overload when reasonable. For example, instead of
//
// Union({p0, p1, p2, p3})
//
Expand Down Expand Up @@ -365,8 +365,8 @@ inline Predicate Match(absl::string_view field, TernaryField<N> value) {
for (int i = 0; i < N; ++i) {
if (!value.mask[i]) continue;
const int bit_val = value.value[i] ? 1 : 0;
predicate =
std::move(predicate) && Match(absl::StrCat(field, "_b", i), bit_val);
predicate = std::move(predicate) &&
Match(absl::StrCat(field, "{", i, "}"), bit_val);
}
return predicate;
}
Expand All @@ -383,7 +383,7 @@ inline Policy Modify(absl::string_view field, TernaryField<N> new_value) {
if (!new_value.mask[i]) continue;
const int value = new_value.value[i] ? 1 : 0;
policy = Sequence(
{std::move(policy), Modify(absl::StrCat(field, "_b", i), value)});
{std::move(policy), Modify(absl::StrCat(field, "{", i, "}"), value)});
}
return policy;
}
Expand Down
6 changes: 3 additions & 3 deletions netkat/frontend_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@ TEST(FrontEndTest, ValueReflectedWhenMasked) {
UlongTernaryField ternary = {.value = {0b0010}, .mask = {0b0011}};
EXPECT_THAT(
Match("f", ternary).ToProto(),
EqualsProto((Predicate::True() && Match("f_b0", 0) && Match("f_b1", 1))
EqualsProto((Predicate::True() && Match("f{0}", 0) && Match("f{1}", 1))
.ToProto()));
EXPECT_THAT(Modify("f", ternary).ToProto(),
EqualsProto(SequenceProto(
SequenceProto(AcceptProto(), ModificationProto("f_b0", 0)),
ModificationProto("f_b1", 1))));
SequenceProto(AcceptProto(), ModificationProto("f{0}", 0)),
ModificationProto("f{1}", 1))));
}

} // namespace
Expand Down
16 changes: 9 additions & 7 deletions netkat/packet_transformer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -828,12 +828,15 @@ std::string PacketTransformerManager::ToDot(
absl::flat_hash_set<PacketTransformerHandle> visited = {transformer};

auto dotify_map =
[&](absl::string_view field, absl::string_view old_value, int node_index,
[&](absl::string_view field, std::optional<int> old_value, int node_index,
const absl::btree_map<int, PacketTransformerHandle>& map) {
for (const auto& [new_value, branch] : map) {
absl::StrAppendFormat(
&result, " %d -> %d [label=\"%s==%s; %s:=%d\"]\n", node_index,
branch.node_index_, field, old_value, field, new_value);
absl::StrAppendFormat(&result, " %d -> %d [label=\"", node_index,
branch.node_index_);
if (old_value.has_value()) {
absl::StrAppendFormat(&result, "%s==%d; ", field, *old_value);
}
absl::StrAppendFormat(&result, "%s:=%d\"]\n", field, new_value);
if (IsAccept(branch) || IsDeny(branch)) continue;
bool new_branch = visited.insert(branch).second;
if (new_branch) work_list.push(branch);
Expand All @@ -852,10 +855,9 @@ std::string PacketTransformerManager::ToDot(
absl::StrAppendFormat(&result, " %d [label=\"%s\"]\n",
transformer.node_index_, field);
for (const auto& [value, modify_map] : node.modify_branch_by_field_match) {
dotify_map(field, absl::StrCat(value), transformer.node_index_,
modify_map);
dotify_map(field, value, transformer.node_index_, modify_map);
}
dotify_map(field, "*", transformer.node_index_,
dotify_map(field, /*old_value=*/std::nullopt, transformer.node_index_,
node.default_branch_by_field_modification);
PacketTransformerHandle fallthrough = node.default_branch;
absl::StrAppendFormat(&result, " %d -> %d [style=dashed]\n",
Expand Down
20 changes: 10 additions & 10 deletions netkat/packet_transformer_test.expected
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ digraph {
7 -> 5 [label="a==5; a:=5"]
7 -> 6 [style=dashed]
5 [label="b"]
5 -> 4294967294 [label="b==*; b:=1"]
5 -> 4294967294 [label="b:=1"]
5 -> 4 [style=dashed]
6 [label="b"]
6 -> 4294967294 [label="b==2; b:=1"]
Expand Down Expand Up @@ -79,23 +79,23 @@ digraph {
4294967295 [label="F" shape=box]
16 [label="a"]
16 -> 13 [label="a==1; a:=1"]
16 -> 3 [label="a==*; a:=1"]
16 -> 3 [label="a:=1"]
16 -> 15 [style=dashed]
13 [label="b"]
13 -> 12 [label="b==1; b:=1"]
13 -> 4294967294 [label="b==*; b:=1"]
13 -> 4294967294 [label="b:=1"]
13 -> 9 [style=dashed]
3 [label="b"]
3 -> 4294967294 [label="b==*; b:=1"]
3 -> 4294967294 [label="b:=1"]
3 -> 4294967295 [style=dashed]
15 [label="b"]
15 -> 12 [label="b==1; b:=1"]
15 -> 9 [style=dashed]
12 [label="c"]
12 -> 4294967294 [label="c==*; c:=4"]
12 -> 4294967294 [label="c:=4"]
12 -> 4294967294 [style=dashed]
9 [label="c"]
9 -> 4294967294 [label="c==*; c:=4"]
9 -> 4294967294 [label="c:=4"]
9 -> 4294967295 [style=dashed]
}
================================================================================
Expand Down Expand Up @@ -147,23 +147,23 @@ digraph {
21 -> 18 [label="a==1; a:=1"]
21 -> 3 [label="a==5; a:=1"]
21 -> 20 [label="a==5; a:=5"]
21 -> 19 [label="a==*; a:=1"]
21 -> 19 [label="a:=1"]
21 -> 18 [style=dashed]
18 [label="b"]
18 -> 12 [label="b==2; b:=1"]
18 -> 17 [label="b==2; b:=2"]
18 -> 4294967295 [style=dashed]
3 [label="b"]
3 -> 4294967294 [label="b==*; b:=1"]
3 -> 4294967294 [label="b:=1"]
3 -> 4294967295 [style=dashed]
20 [label="b"]
20 -> 12 [label="b==*; b:=1"]
20 -> 12 [label="b:=1"]
20 -> 17 [style=dashed]
19 [label="b"]
19 -> 4294967294 [label="b==2; b:=1"]
19 -> 4294967295 [style=dashed]
12 [label="c"]
12 -> 4294967294 [label="c==*; c:=4"]
12 -> 4294967294 [label="c:=4"]
12 -> 4294967294 [style=dashed]
17 [label="c"]
17 -> 4294967294 [label="c==5; c:=4"]
Expand Down