Skip to content

Commit

Permalink
Remove redundant nonterminating actions except the last one
Browse files Browse the repository at this point in the history
Currently, this applies only to "state-timeout", but may extend to more
actions in the future.

This logic supports progressive refinement, such as initial rules setting a
general TCP timeout and subsequent rules refining specifics.
  • Loading branch information
ol-imorozko committed Sep 3, 2024
1 parent 7bb6529 commit 1afe262
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
23 changes: 23 additions & 0 deletions controlplane/acl_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ void value_t::ensure_termination(IntermediateActions& actions)
}
}

void value_t::remove_redundant_actions(IntermediateActions& actions)
{
constexpr std::array<size_t, 1> affected_nonterminating_rules = {
common::variant::get_index<common::StateTimeoutAction, common::RawAction>::value};

for (size_t index : affected_nonterminating_rules)
{
if (actions.action_counts[index] <= 1)
continue;

size_t count_to_remove = actions.action_counts[index] - 1;

auto pred = [index, &count_to_remove](const common::Action& action) {
return action.raw_action.index() == index && count_to_remove-- > 0;
};

auto new_end = std::remove_if(actions.path.begin(), actions.path.end(), pred);

actions.path.erase(new_end, actions.path.end());
}
}

void value_t::finalize_actions(IntermediateActions&& actions)
{
if (actions.check_state_index.has_value())
Expand All @@ -60,6 +82,7 @@ void value_t::compile()
for (auto& actions : intermediate_vector)
{
ensure_termination(actions);
remove_redundant_actions(actions);
finalize_actions(std::move(actions));
}
}
10 changes: 9 additions & 1 deletion controlplane/acl_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class value_t
*/
void ensure_termination(IntermediateActions& actions);

/**
* Removes all but the last occurrence of some non-terminating actions
*
* Affected non-terminating actions: StateTimeout
*/
void remove_redundant_actions(IntermediateActions& actions);

/**
* Finalizes the intermediate actions into a vector of BaseActions objects.
*/
Expand Down Expand Up @@ -52,7 +59,8 @@ class value_t
* The compile process involves:
* 1. Ensuring that the last action in the path is terminating.
* If not, a default "drop" rule is added.
* 2. Finalizing the actions into a vector of BaseActions objects
* 2. Removing all but the last occurrence of some non-terminating actions
* 3. Finalizing the actions into a vector of BaseActions objects
*/
void compile();

Expand Down

0 comments on commit 1afe262

Please sign in to comment.