From ab38321508d886f0acd535f0f5f07a3d44e29591 Mon Sep 17 00:00:00 2001 From: "Enrico Loparco (eloparco)" Date: Wed, 14 Sep 2022 08:58:55 +0000 Subject: feat(hicn-ping): allow usage of random prefixes/suffixes in hicn-ping Ref: HICN-783 Signed-off-by: Enrico Loparco (eloparco) Change-Id: I41c804dd639ee15aee9619732f55e39a3baf1385 --- libtransport/src/core/interest.cc | 10 +- libtransport/src/core/name.cc | 7 ++ libtransport/src/test/CMakeLists.txt | 1 + libtransport/src/test/test_traffic_generator.cc | 119 ++++++++++++++++++++++++ libtransport/src/utils/CMakeLists.txt | 1 + libtransport/src/utils/traffic_generator.cc | 87 +++++++++++++++++ 6 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 libtransport/src/test/test_traffic_generator.cc create mode 100644 libtransport/src/utils/traffic_generator.cc (limited to 'libtransport/src') diff --git a/libtransport/src/core/interest.cc b/libtransport/src/core/interest.cc index 6348a8d3e..c3eb7c379 100644 --- a/libtransport/src/core/interest.cc +++ b/libtransport/src/core/interest.cc @@ -170,9 +170,6 @@ void Interest::encodeSuffixes() { (interest_manifest_header_t *)(writableData() + headerSize()); interest_manifest_init(int_manifest_header, name_.getSuffix()); - memset(int_manifest_header->request_bitmap, 0xFFFFFFFF, - BITMAP_SIZE * sizeof(hicn_uword)); - for (auto it = suffix_set_.begin(); it != suffix_set_.end(); it++) { interest_manifest_add_suffix(int_manifest_header, *it); } @@ -237,6 +234,13 @@ hicn_uword *Interest::getRequestBitmap() { return header->request_bitmap; } +interest_manifest_header_t *Interest::getIntManifestHeader() { + if (!hasManifest()) return nullptr; + + auto header = (interest_manifest_header_t *)(writableData() + headerSize()); + return header; +}; + void Interest::setRequestBitmap(const uint32_t *request_bitmap) { if (!hasManifest()) return; diff --git a/libtransport/src/core/name.cc b/libtransport/src/core/name.cc index 02cc79be6..4f8ba7873 100644 --- a/libtransport/src/core/name.cc +++ b/libtransport/src/core/name.cc @@ -168,6 +168,13 @@ hicn_ip_prefix_t Name::toIpAddress() const { return ret; } +std::string Name::getPrefix() const { + char prefix[MAXSZ_HICN_NAME]; + hicn_name_no_suffix_snprintf(prefix, MAXSZ_HICN_NAME, &name_); + + return std::string(prefix); +} + int Name::getAddressFamily() const { int ret = 0; diff --git a/libtransport/src/test/CMakeLists.txt b/libtransport/src/test/CMakeLists.txt index 356ee0067..864006e5d 100644 --- a/libtransport/src/test/CMakeLists.txt +++ b/libtransport/src/test/CMakeLists.txt @@ -33,6 +33,7 @@ list(APPEND TESTS_SRC test_thread_pool.cc test_quadloop.cc test_prefix.cc + test_traffic_generator.cc ) if (ENABLE_RELY) diff --git a/libtransport/src/test/test_traffic_generator.cc b/libtransport/src/test/test_traffic_generator.cc new file mode 100644 index 000000000..88cb2de75 --- /dev/null +++ b/libtransport/src/test/test_traffic_generator.cc @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * 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 +#include +#include + +static constexpr int NUM_PINGS = 10; +static constexpr char PREFIX[] = "b001:1:2:3::"; +static constexpr uint32_t FIRST_SUFFIX = 5; + +namespace utils { + +using transport::IncrSuffixTrafficGenerator; +using transport::RandomTrafficGenerator; + +class TrafficGeneratorTest : public ::testing::Test { + protected: + TrafficGeneratorTest() {} + virtual ~TrafficGeneratorTest() {} +}; + +TEST_F(TrafficGeneratorTest, IncrSuffixGetPrefixAndSuffix) { + auto traffic_generator_ = std::make_unique( + PREFIX, FIRST_SUFFIX, NUM_PINGS); + + std::string prefix = traffic_generator_->getPrefix(); + EXPECT_EQ(prefix, PREFIX); + uint32_t suffix = traffic_generator_->getSuffix(); + EXPECT_EQ(suffix, FIRST_SUFFIX); +} + +TEST_F(TrafficGeneratorTest, IncrSuffixGetMultipleSuffixes) { + auto traffic_generator_ = std::make_unique( + PREFIX, FIRST_SUFFIX, NUM_PINGS); + + std::string prefix = traffic_generator_->getPrefix(); + EXPECT_EQ(prefix, PREFIX); + EXPECT_EQ(prefix, traffic_generator_->getPrefix()); + + for (int i = 0; i < NUM_PINGS; i++) + EXPECT_EQ(traffic_generator_->getSuffix(), FIRST_SUFFIX + i); +} + +TEST_F(TrafficGeneratorTest, IncrSuffixReset) { + auto traffic_generator_ = std::make_unique( + PREFIX, FIRST_SUFFIX, NUM_PINGS); + + for (int i = 0; i < NUM_PINGS; i++) traffic_generator_->getSuffix(); + + traffic_generator_->reset(); + EXPECT_EQ(traffic_generator_->getPrefix(), PREFIX); + EXPECT_EQ(traffic_generator_->getSuffix(), FIRST_SUFFIX); + EXPECT_EQ(traffic_generator_->getSuffix(), FIRST_SUFFIX + 1); +} + +TEST_F(TrafficGeneratorTest, IncrSuffixRequestTooManySuffixes) { + auto traffic_generator_ = std::make_unique( + PREFIX, FIRST_SUFFIX, NUM_PINGS); + + for (int i = 0; i < NUM_PINGS; i++) traffic_generator_->getSuffix(); + EXPECT_THROW(traffic_generator_->getSuffix(), std::runtime_error); +} + +TEST_F(TrafficGeneratorTest, IncrSuffixGetPrefixAndSuffixTogether) { + auto traffic_generator_ = std::make_unique( + PREFIX, FIRST_SUFFIX, NUM_PINGS); + + auto [prefix, suffix] = traffic_generator_->getPrefixAndSuffix(); + EXPECT_EQ(prefix, PREFIX); + EXPECT_EQ(suffix, FIRST_SUFFIX); +} + +TEST_F(TrafficGeneratorTest, IncrSuffixCheckSentCount) { + auto traffic_generator_ = std::make_unique( + PREFIX, FIRST_SUFFIX, NUM_PINGS); + + for (int i = 0; i < NUM_PINGS; i++) { + EXPECT_EQ(traffic_generator_->getSentCount(), i); + EXPECT_FALSE(traffic_generator_->hasFinished()); + traffic_generator_->getSuffix(); + } + EXPECT_TRUE(traffic_generator_->hasFinished()); +} + +TEST_F(TrafficGeneratorTest, RandomGetPrefixAndSuffix) { + auto traffic_generator_ = std::make_unique(NUM_PINGS); + + std::string prefix1 = traffic_generator_->getPrefix(); + std::string prefix2 = traffic_generator_->getPrefix(); + EXPECT_NE(prefix1, prefix2); + + uint32_t suffix1 = traffic_generator_->getSuffix(); + uint32_t suffix2 = traffic_generator_->getSuffix(); + EXPECT_NE(suffix1, suffix2); +} + +TEST_F(TrafficGeneratorTest, RandomGetPrefixAndSuffixWithNetPrefix) { + auto traffic_generator_ = std::make_unique( + NUM_PINGS, std::string(PREFIX) + "/64"); + + for (int i = 0; i < NUM_PINGS; i++) + EXPECT_THAT(traffic_generator_->getPrefix(), + testing::StartsWith(std::string(PREFIX))); +} + +} // namespace utils \ No newline at end of file diff --git a/libtransport/src/utils/CMakeLists.txt b/libtransport/src/utils/CMakeLists.txt index 5bb76303a..e7c1c03ea 100644 --- a/libtransport/src/utils/CMakeLists.txt +++ b/libtransport/src/utils/CMakeLists.txt @@ -17,6 +17,7 @@ list(APPEND SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/log.cc ${CMAKE_CURRENT_SOURCE_DIR}/membuf.cc ${CMAKE_CURRENT_SOURCE_DIR}/content_store.cc + ${CMAKE_CURRENT_SOURCE_DIR}/traffic_generator.cc ) diff --git a/libtransport/src/utils/traffic_generator.cc b/libtransport/src/utils/traffic_generator.cc new file mode 100644 index 000000000..a617e3dc9 --- /dev/null +++ b/libtransport/src/utils/traffic_generator.cc @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * 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 +#include + +#include + +namespace transport { + +/* TrafficGenerator */ + +TrafficGenerator::TrafficGenerator(uint32_t count) : count_(count), sent_(0) {} + +bool TrafficGenerator::hasFinished() { return sent_ >= count_; } + +uint32_t TrafficGenerator::getSentCount() { return sent_; } + +std::pair TrafficGenerator::getPrefixAndSuffix() { + return std::make_pair(getPrefix(), getSuffix()); +} + +void TrafficGenerator::reset() { sent_ = 0; }; + +void TrafficGenerator::onSuffixGenerated() { + if (hasFinished()) throw std::runtime_error("Too many pings"); + sent_++; +}; + +/* IncrSuffixTrafficGenerator */ + +IncrSuffixTrafficGenerator::IncrSuffixTrafficGenerator(std::string prefix, + uint32_t suffix, + uint32_t count) + : TrafficGenerator(count), + prefix_(prefix), + suffix_(suffix), + initial_suffix_(suffix) {} + +std::string IncrSuffixTrafficGenerator::getPrefix() { return prefix_; } + +uint32_t IncrSuffixTrafficGenerator::getSuffix() { + TrafficGenerator::onSuffixGenerated(); + return suffix_++; +} + +void IncrSuffixTrafficGenerator::reset() { + TrafficGenerator::reset(); + suffix_ = initial_suffix_; +}; + +/* RandomTrafficGenerator */ + +RandomTrafficGenerator::RandomTrafficGenerator(uint32_t count, + std::string net_prefix) + : TrafficGenerator(count), + net_prefix_(net_prefix), + rand_engine_((std::random_device())()), + uniform_distribution_(0, std::numeric_limits::max()) {} + +std::string RandomTrafficGenerator::getPrefix() { + // Generate random prefix + core::Prefix prefix(net_prefix_); + core::Name name = prefix.makeRandomName(); + return name.getPrefix(); +} + +uint32_t RandomTrafficGenerator::getSuffix() { + TrafficGenerator::onSuffixGenerated(); + + // Generate random suffix + return uniform_distribution_(rand_engine_); +} + +} // namespace transport \ No newline at end of file -- cgit 1.2.3-korg