-
Notifications
You must be signed in to change notification settings - Fork 245
Add nftables RAII library + unit tests #3127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dxapd
wants to merge
4
commits into
google:main
Choose a base branch
from
dxapd:nftables-lib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
985dc67
Add nftables RAII library + unit tests
dxapd 791af50
switch mock to fake entirely + address several review comments
dxapd 74fb59e
clarify SearchForNft comment + restrict the nftables RAII lib to linux
dxapd fc8f65a
last round of review fixes for fake_nftables
dxapd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "allocd/net/nft_rule.h" | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
|
|
||
| #include "absl/log/log.h" | ||
| #include "absl/strings/str_cat.h" | ||
|
|
||
| #include "allocd/net/nftables.h" | ||
| #include "cuttlefish/result/result.h" | ||
|
|
||
| namespace cuttlefish { | ||
| namespace { | ||
|
|
||
| // Namespace prefix applied to every dynamic cvdalloc rule comment. | ||
| constexpr std::string_view kCvdallocCommentPrefix = "cvdalloc-"; | ||
|
|
||
| } // namespace | ||
|
|
||
| Result<NftRule> NftRule::Create(Nftables& nft, std::string_view family, | ||
| std::string_view table, std::string_view chain, | ||
| std::string_view content, | ||
| std::string_view tag) { | ||
| std::string comment = absl::StrCat(kCvdallocCommentPrefix, tag); | ||
| CF_EXPECT(nft.AddRule(family, table, chain, content, comment)); | ||
| return NftRule(&nft, family, table, chain, std::move(comment)); | ||
| } | ||
|
|
||
| NftRule::NftRule(Nftables* nft, std::string_view family, std::string_view table, | ||
| std::string_view chain, std::string comment) | ||
| : nft_(nft), | ||
| family_(family), | ||
| table_(table), | ||
| chain_(chain), | ||
| comment_(std::move(comment)) {} | ||
|
|
||
| NftRule::NftRule(NftRule&& r) noexcept | ||
| : nft_(std::exchange(r.nft_, nullptr)), | ||
| family_(std::move(r.family_)), | ||
| table_(std::move(r.table_)), | ||
| chain_(std::move(r.chain_)), | ||
| comment_(std::move(r.comment_)) {} | ||
|
|
||
| NftRule::~NftRule() { | ||
| // nft_ is nulled on move, so a moved-from rule is inert and never deletes. | ||
| if (nft_ != nullptr) { | ||
| auto res = nft_->DeleteRulesByComment(family_, table_, chain_, comment_); | ||
| if (!res.has_value()) { | ||
| LOG(ERROR) << "Failed to delete nft rule(s) in NftRule destructor: " | ||
| << res.error(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace cuttlefish |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef ALLOCD_NET_NFT_RULE_H_ | ||
| #define ALLOCD_NET_NFT_RULE_H_ | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "allocd/net/nftables.h" | ||
| #include "cuttlefish/result/result.h" | ||
|
|
||
| namespace cuttlefish { | ||
|
|
||
| class NftRule { | ||
| public: | ||
| // `tag` is a caller-supplied unique identifier (e.g. an interface name). | ||
| static Result<NftRule> Create(Nftables& nft, std::string_view family, | ||
| std::string_view table, std::string_view chain, | ||
| std::string_view content, std::string_view tag); | ||
|
|
||
| ~NftRule(); | ||
|
|
||
| NftRule(NftRule&& r) noexcept; | ||
| NftRule& operator=(NftRule&& r) = delete; | ||
| NftRule(const NftRule& r) = delete; | ||
| NftRule& operator=(const NftRule& r) = delete; | ||
|
|
||
| private: | ||
| NftRule(Nftables* nft, std::string_view family, std::string_view table, | ||
| std::string_view chain, std::string comment); | ||
|
|
||
| Nftables* nft_ = nullptr; | ||
| std::string family_; | ||
| std::string table_; | ||
| std::string chain_; | ||
| std::string comment_; | ||
| }; | ||
|
|
||
| } // namespace cuttlefish | ||
|
|
||
| #endif // ALLOCD_NET_NFT_RULE_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "allocd/net/nft_rule.h" | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <utility> | ||
|
|
||
| #include "allocd/test/fake_nftables.h" | ||
| #include "cuttlefish/result/result.h" | ||
| #include "cuttlefish/result/result_matchers.h" | ||
|
|
||
| namespace cuttlefish { | ||
| namespace { | ||
|
|
||
| class NftRuleTest : public ::testing::Test { | ||
| protected: | ||
| void SetUp() override { | ||
| ASSERT_THAT(fake_.EnsureTable("ip", "table1"), IsOk()); | ||
| ASSERT_THAT(fake_.EnsureChain("ip", "table1", "chain1", ""), IsOk()); | ||
| } | ||
|
|
||
| FakeNftables fake_; | ||
| }; | ||
|
|
||
| TEST_F(NftRuleTest, CreateAddsRuleWithPrefixedComment) { | ||
| Result<NftRule> rule = | ||
| NftRule::Create(fake_, "ip", "table1", "chain1", "content1", "tag1"); | ||
| ASSERT_THAT(rule, IsOk()); | ||
| EXPECT_TRUE( | ||
| fake_.HasRuleWithComment("ip", "table1", "chain1", "cvdalloc-tag1")); | ||
| } | ||
|
|
||
| TEST_F(NftRuleTest, DeletesRuleOnDestruction) { | ||
| { | ||
| Result<NftRule> rule = | ||
| NftRule::Create(fake_, "ip", "table1", "chain1", "content1", "tag1"); | ||
| ASSERT_THAT(rule, IsOk()); | ||
| EXPECT_TRUE( | ||
| fake_.HasRuleWithComment("ip", "table1", "chain1", "cvdalloc-tag1")); | ||
| } | ||
| EXPECT_FALSE( | ||
| fake_.HasRuleWithComment("ip", "table1", "chain1", "cvdalloc-tag1")); | ||
| EXPECT_EQ(fake_.RuleCount("ip", "table1", "chain1"), 0); | ||
| } | ||
|
|
||
| TEST_F(NftRuleTest, MoveConstructorTransfersOwnership) { | ||
| { | ||
| Result<NftRule> rule1 = | ||
| NftRule::Create(fake_, "ip", "table1", "chain1", "content1", "tag1"); | ||
| ASSERT_THAT(rule1, IsOk()); | ||
|
|
||
| NftRule rule2(std::move(*rule1)); | ||
| EXPECT_TRUE( | ||
| fake_.HasRuleWithComment("ip", "table1", "chain1", "cvdalloc-tag1")); | ||
| } | ||
| EXPECT_FALSE( | ||
| fake_.HasRuleWithComment("ip", "table1", "chain1", "cvdalloc-tag1")); | ||
| EXPECT_EQ(fake_.RuleCount("ip", "table1", "chain1"), 0); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace cuttlefish |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef ALLOCD_NET_NFTABLES_H_ | ||
| #define ALLOCD_NET_NFTABLES_H_ | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #include <string_view> | ||
|
|
||
| #include "cuttlefish/result/result.h" | ||
|
|
||
| namespace cuttlefish { | ||
|
|
||
| class Nftables { | ||
| public: | ||
| virtual ~Nftables() = default; | ||
|
|
||
| virtual Result<void> EnsureTable(std::string_view family, | ||
| std::string_view table) = 0; | ||
| virtual Result<void> DeleteTable(std::string_view family, | ||
| std::string_view table) = 0; | ||
| virtual Result<void> EnsureChain(std::string_view family, | ||
| std::string_view table, | ||
| std::string_view chain, | ||
| std::string_view content) = 0; | ||
| virtual Result<uint64_t> AddRule(std::string_view family, | ||
| std::string_view table, | ||
| std::string_view chain, | ||
| std::string_view content, | ||
| std::string_view comment) = 0; | ||
| virtual Result<void> DeleteRule(std::string_view family, | ||
| std::string_view table, | ||
| std::string_view chain, uint64_t handle) = 0; | ||
| virtual Result<void> DeleteRulesByComment(std::string_view family, | ||
| std::string_view table, | ||
| std::string_view chain, | ||
| std::string_view comment) = 0; | ||
| }; | ||
|
|
||
| } // namespace cuttlefish | ||
|
|
||
| #endif // ALLOCD_NET_NFTABLES_H_ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.