diff options
Diffstat (limited to 'libtransport/includes')
80 files changed, 833 insertions, 547 deletions
diff --git a/libtransport/includes/hicn/transport/CMakeLists.txt b/libtransport/includes/hicn/transport/CMakeLists.txt index eb339fb5a..8b9f907b9 100644 --- a/libtransport/includes/hicn/transport/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2020 Cisco and/or its affiliates. +# Copyright (c) 2021-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: @@ -23,14 +23,21 @@ add_subdirectory(portability) add_subdirectory(auth) add_subdirectory(utils) -set(LIBTRANSPORT_INCLUDE_DIRS - ${CMAKE_CURRENT_SOURCE_DIR}/../.. "" + +############################################################## +# Public headers directory +############################################################## +set(Libhicntransport_INCLUDE_DIRS + ${CMAKE_CURRENT_SOURCE_DIR}/../.. CACHE INTERNAL "" FORCE ) + +############################################################## +# Header files to install +############################################################## set(LIBHICNTRANSPORT_TO_INSTALL_HEADER_FILES - ${HEADER_FILES} "" - CACHE INTERNAL - "" FORCE + ${HEADER_FILES} + PARENT_SCOPE ) diff --git a/libtransport/includes/hicn/transport/auth/CMakeLists.txt b/libtransport/includes/hicn/transport/auth/CMakeLists.txt index 1e9fe4698..0b5ae1836 100644 --- a/libtransport/includes/hicn/transport/auth/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/auth/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 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: @@ -15,7 +15,6 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/common.h ${CMAKE_CURRENT_SOURCE_DIR}/crypto_hash.h ${CMAKE_CURRENT_SOURCE_DIR}/crypto_suite.h - ${CMAKE_CURRENT_SOURCE_DIR}/identity.h ${CMAKE_CURRENT_SOURCE_DIR}/key_id.h ${CMAKE_CURRENT_SOURCE_DIR}/policies.h ${CMAKE_CURRENT_SOURCE_DIR}/signer.h diff --git a/libtransport/includes/hicn/transport/auth/common.h b/libtransport/includes/hicn/transport/auth/common.h index fb0e82eb7..d2282436e 100644 --- a/libtransport/includes/hicn/transport/auth/common.h +++ b/libtransport/includes/hicn/transport/auth/common.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/auth/crypto_hash.h b/libtransport/includes/hicn/transport/auth/crypto_hash.h index 90f1627e9..3c734fee3 100644 --- a/libtransport/includes/hicn/transport/auth/crypto_hash.h +++ b/libtransport/includes/hicn/transport/auth/crypto_hash.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -89,7 +89,7 @@ class CryptoHash { static std::size_t getSize(CryptoHashType hash_type); // Compare two raw buffers - static bool compareDigest(const uint8_t *h1, const uint8_t *h2, + static bool compareDigest(const uint8_t *digest1, const uint8_t *digest2, CryptoHashType hash_type); private: diff --git a/libtransport/includes/hicn/transport/auth/crypto_suite.h b/libtransport/includes/hicn/transport/auth/crypto_suite.h index d0f1de395..ed21abb91 100644 --- a/libtransport/includes/hicn/transport/auth/crypto_suite.h +++ b/libtransport/includes/hicn/transport/auth/crypto_suite.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -47,6 +47,9 @@ enum class CryptoSuite : uint8_t { // Return the suite associated to the given NID CryptoSuite getSuite(int nid); +// Return the string representation of given suite +std::string getStringSuite(CryptoSuite suite); + // Return the hash type associated to the given suite CryptoHashType getHashType(CryptoSuite suite); diff --git a/libtransport/includes/hicn/transport/auth/identity.h b/libtransport/includes/hicn/transport/auth/identity.h deleted file mode 100644 index be072f5d3..000000000 --- a/libtransport/includes/hicn/transport/auth/identity.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2017-2021 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. - */ - -#pragma once - -#include <errno.h> -#include <fcntl.h> -#include <hicn/transport/auth/signer.h> -#include <unistd.h> - -extern "C" { -#include <openssl/pkcs12.h> -#include <openssl/rand.h> -#include <openssl/x509.h> -#include <openssl/x509v3.h> -} - -namespace transport { -namespace auth { - -class Identity { - // This class holds several information about a client, including its public - // key. - public: - // Generate a new identity from the given parameters. The identity will be - // saved in 'keystore_path' and encrypted using 'keystore_pwd'. - Identity(const std::string &keystore_path, const std::string &keystore_pwd, - CryptoSuite suite, unsigned int signature_len, - unsigned int validity_days, const std::string &subject_name); - - // Create an identity from an already existing keystore path. - Identity(std::string &keystore_path, std::string &keystore_pwd, - CryptoHashType hash_type); - - Identity(const Identity &other); - Identity(Identity &&other); - ~Identity(); - - // Return the asymmetric signer object created from the public key. - std::shared_ptr<AsymmetricSigner> getSigner() const; - - // Return the key store filename. - std::string getFilename() const; - - // Return the key store password. - std::string getPassword() const; - - std::shared_ptr<X509> getCertificate() const; - - std::shared_ptr<EVP_PKEY> getPrivateKey() const; - - // Generate a new random identity. - static Identity generateIdentity(const std::string &subject_name = ""); - - private: - static void free_key(EVP_PKEY *T) { EVP_PKEY_free(T); } - - std::string pwd_; - std::string filename_; - std::shared_ptr<AsymmetricSigner> signer_; - std::shared_ptr<X509> cert_; -}; - -} // namespace auth -} // namespace transport diff --git a/libtransport/includes/hicn/transport/auth/key_id.h b/libtransport/includes/hicn/transport/auth/key_id.h index 3aa09336f..8723ae698 100644 --- a/libtransport/includes/hicn/transport/auth/key_id.h +++ b/libtransport/includes/hicn/transport/auth/key_id.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/auth/policies.h b/libtransport/includes/hicn/transport/auth/policies.h index 00464d54b..b9755595c 100644 --- a/libtransport/includes/hicn/transport/auth/policies.h +++ b/libtransport/includes/hicn/transport/auth/policies.h @@ -23,10 +23,10 @@ namespace auth { * perform after verification. */ enum class VerificationPolicy { + UNKNOWN, ABORT, - ACCEPT, DROP, - UNKNOWN, + ACCEPT, }; } // namespace auth diff --git a/libtransport/includes/hicn/transport/auth/signer.h b/libtransport/includes/hicn/transport/auth/signer.h index 405dd83cf..5a7598991 100644 --- a/libtransport/includes/hicn/transport/auth/signer.h +++ b/libtransport/includes/hicn/transport/auth/signer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2021 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -21,18 +21,19 @@ #include <hicn/transport/errors/errors.h> #include <hicn/transport/utils/membuf.h> +#include <memory> extern "C" { #include <openssl/evp.h> #include <openssl/hmac.h> +#include <openssl/pkcs12.h> +#include <openssl/x509.h> } namespace transport { namespace auth { -class Identity; class Signer { // The base class from which all signer classes derive. - friend class Identity; public: Signer(); @@ -47,6 +48,9 @@ class Signer { // Return the signature. std::vector<uint8_t> getSignature() const; + // Return the signature as a string + std::string getStringSignature() const; + // Return the signature size in bytes. virtual std::size_t getSignatureSize() const; @@ -61,6 +65,9 @@ class Signer { // Return the hash algorithm associated to the signer. CryptoHashType getHashType() const; + // Print signature to stdout + void display(); + protected: CryptoSuite suite_; std::vector<uint8_t> signature_; @@ -84,10 +91,17 @@ class AsymmetricSigner : public Signer { public: AsymmetricSigner() = default; - // Construct an AsymmetricSigner from a key store and a given crypto suite. + // Create an AsymmetricSigner from a keystore file (.p12). + AsymmetricSigner(std::string keystore_path, std::string password); + + // Construct an AsymmetricSigner from a key store and a given crypto + // suite. AsymmetricSigner(CryptoSuite suite, std::shared_ptr<EVP_PKEY> key, std::shared_ptr<EVP_PKEY> pub_key); + void setKey(CryptoSuite suite, std::shared_ptr<EVP_PKEY> key, + std::shared_ptr<EVP_PKEY> pub_key); + std::size_t getSignatureFieldSize() const override; }; diff --git a/libtransport/includes/hicn/transport/auth/verifier.h b/libtransport/includes/hicn/transport/auth/verifier.h index 6321d4ed5..677a1efe4 100644 --- a/libtransport/includes/hicn/transport/auth/verifier.h +++ b/libtransport/includes/hicn/transport/auth/verifier.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2021 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -40,9 +40,9 @@ class Verifier { // The VerificationFailedCallback will be called by the transport if a // data packet (either a manifest or a content object) was not validated. // The application decides what to do then by returning a - // VerificationPolicy object. + // new VerificationPolicy. using VerificationFailedCallback = std::function<auth::VerificationPolicy( - const core::ContentObject &content_object, std::error_code ec)>; + Suffix suffix, VerificationPolicy policy)>; // The list of VerificationPolicy that will trigger the // VerificationFailedCallback. @@ -96,13 +96,13 @@ class Verifier { void getVerificationFailedCallback( VerificationFailedCallback **verification_failed_cb); + // Call VerificationFailedCallback if it is set and update the packet policy. + void callVerificationFailedCallback(Suffix suffix, + VerificationPolicy &policy); + protected: VerificationFailedCallback verification_failed_cb_; std::vector<VerificationPolicy> failed_policies_; - - // Call VerificationFailedCallback if it is set and update the packet policy. - void callVerificationFailedCallback(PacketPtr packet, - VerificationPolicy &policy); }; class VoidVerifier : public Verifier { diff --git a/libtransport/includes/hicn/transport/core/CMakeLists.txt b/libtransport/includes/hicn/transport/core/CMakeLists.txt index 14c795a7a..34048d93a 100644 --- a/libtransport/includes/hicn/transport/core/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/core/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/core/connector.h b/libtransport/includes/hicn/transport/core/connector.h index dcf38cdc8..b671a7d89 100644 --- a/libtransport/includes/hicn/transport/core/connector.h +++ b/libtransport/includes/hicn/transport/core/connector.h @@ -15,6 +15,7 @@ #pragma once +#include <glog/logging.h> #include <hicn/transport/core/connector_stats.h> #include <hicn/transport/core/content_object.h> #include <hicn/transport/core/endpoint.h> @@ -50,22 +51,21 @@ class Connector : public std::enable_shared_from_this<Connector> { enum class Role : std::uint8_t { CONSUMER, PRODUCER }; - public: static constexpr std::size_t queue_size = 4096; static constexpr std::uint32_t invalid_connector = ~0; - -#ifdef LINUX + static constexpr std::uint32_t max_reconnection_reattempts = 5; static constexpr std::uint16_t max_burst = 256; -#endif using Ptr = std::shared_ptr<Connector>; - using PacketQueue = std::deque<Packet::Ptr>; - using PacketReceivedCallback = std::function<void( - Connector *, utils::MemBuf &, const std::error_code &)>; + using PacketQueue = std::deque<utils::MemBuf::Ptr>; + using PacketReceivedCallback = + std::function<void(Connector *, const std::vector<utils::MemBuf::Ptr> &, + const std::error_code &)>; using PacketSentCallback = std::function<void(Connector *, const std::error_code &)>; using OnCloseCallback = std::function<void(Connector *)>; - using OnReconnectCallback = std::function<void(Connector *)>; + using OnReconnectCallback = + std::function<void(Connector *, const std::error_code &)>; using Id = std::uint64_t; template <typename ReceiveCallback, typename SentCallback, typename OnClose, @@ -77,7 +77,8 @@ class Connector : public std::enable_shared_from_this<Connector> { on_close_callback_(std::forward<OnClose &&>(close_callback)), on_reconnect_callback_(std::forward<OnReconnect &&>(on_reconnect)), state_(State::CLOSED), - connector_id_(invalid_connector) {} + connector_id_(invalid_connector), + connection_reattempts_(0) {} virtual ~Connector(){}; @@ -115,7 +116,7 @@ class Connector : public std::enable_shared_from_this<Connector> { virtual void send(Packet &packet) = 0; - virtual void send(const uint8_t *packet, std::size_t len) = 0; + virtual void send(const utils::MemBuf::Ptr &buffer) = 0; virtual void close() = 0; @@ -206,6 +207,9 @@ class Connector : public std::enable_shared_from_this<Connector> { // Stats AtomicConnectorStats stats_; + + // Connection attempts + std::uint32_t connection_reattempts_; }; } // namespace core diff --git a/libtransport/includes/hicn/transport/core/content_object.h b/libtransport/includes/hicn/transport/core/content_object.h index 38baafc69..a8df1e8e3 100644 --- a/libtransport/includes/hicn/transport/core/content_object.h +++ b/libtransport/includes/hicn/transport/core/content_object.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -31,10 +31,9 @@ class ContentObject : public Packet { using Ptr = std::shared_ptr<ContentObject>; using HICNContentObject = hicn_header_t; - ContentObject(Packet::Format format = HF_INET6_TCP, - std::size_t additional_header_size = 0); + ContentObject(Packet::Format format, std::size_t additional_header_size = 0); - ContentObject(const Name &name, Packet::Format format = HF_INET6_TCP, + ContentObject(const Name &name, Packet::Format format, std::size_t additional_header_size = 0); ContentObject(const Name &name, hicn_format_t format, @@ -42,7 +41,7 @@ class ContentObject : public Packet { std::size_t payload_size); template <typename... Args> - ContentObject(CopyBufferOp op, Args &&... args) + ContentObject(CopyBufferOp op, Args &&...args) : Packet(op, std::forward<Args>(args)...) { if (hicn_data_get_name(format_, packet_start_, name_.getStructReference()) < 0) { @@ -51,7 +50,7 @@ class ContentObject : public Packet { } template <typename... Args> - ContentObject(WrapBufferOp op, Args &&... args) + ContentObject(WrapBufferOp op, Args &&...args) : Packet(op, std::forward<Args>(args)...) { if (hicn_data_get_name(format_, packet_start_, name_.getStructReference()) < 0) { @@ -60,8 +59,12 @@ class ContentObject : public Packet { } template <typename... Args> - ContentObject(CreateOp op, Args &&... args) + ContentObject(CreateOp op, Args &&...args) : Packet(op, std::forward<Args>(args)...) { + if (hicn_packet_set_data(format_, packet_start_) < 0) { + throw errors::MalformedPacketException(); + } + if (hicn_data_get_name(format_, packet_start_, name_.getStructReference()) < 0) { throw errors::MalformedPacketException(); @@ -96,6 +99,10 @@ class ContentObject : public Packet { auto shared_from_this() { return utils::shared_from(this); } + bool isLast() const; + + void setLast(); + private: void resetForHash() override; }; diff --git a/libtransport/includes/hicn/transport/core/endpoint.h b/libtransport/includes/hicn/transport/core/endpoint.h index cb6b0f562..e1fa193d7 100644 --- a/libtransport/includes/hicn/transport/core/endpoint.h +++ b/libtransport/includes/hicn/transport/core/endpoint.h @@ -42,14 +42,20 @@ class Endpoint { ~Endpoint() = default; Endpoint &operator=(const Endpoint &other) { - address_ = other.address_; - port_ = other.port_; + if (this != &other) { + address_ = other.address_; + port_ = other.port_; + } + return *this; } Endpoint &operator=(Endpoint &&other) { - address_ = std::move(other.address_); - port_ = std::move(other.port_); + if (this != &other) { + address_ = std::move(other.address_); + port_ = std::move(other.port_); + } + return *this; } diff --git a/libtransport/includes/hicn/transport/core/global_object_pool.h b/libtransport/includes/hicn/transport/core/global_object_pool.h index d7f3a9e41..f98df521f 100644 --- a/libtransport/includes/hicn/transport/core/global_object_pool.h +++ b/libtransport/includes/hicn/transport/core/global_object_pool.h @@ -27,9 +27,10 @@ namespace transport { namespace core { template <std::size_t packet_pool_size = 1024, std::size_t chunk_size = 2048> -class PacketManager - : public utils::Singleton<PacketManager<packet_pool_size, chunk_size>> { - friend class utils::Singleton<PacketManager<packet_pool_size, chunk_size>>; +class PacketManager : public utils::ThreadLocalSingleton< + PacketManager<packet_pool_size, chunk_size>> { + friend class utils::ThreadLocalSingleton< + PacketManager<packet_pool_size, chunk_size>>; public: using MemoryPool = utils::FixedBlockAllocator<chunk_size, packet_pool_size>; @@ -71,7 +72,10 @@ class PacketManager template < typename PacketType, typename... Args, typename = std::enable_if_t<std::is_base_of<Packet, PacketType>::value>> - typename PacketType::Ptr getPacket(Args &&... args) { + typename PacketType::Ptr getPacket(Args &&...args) { + static_assert(sizeof(PacketType) + sizeof(std::shared_ptr<PacketType>) + + sizeof(std::max_align_t) <= + sizeof(PacketStorage::packet_and_shared_ptr)); PacketType *memory = nullptr; memory = reinterpret_cast<PacketType *>(memory_pool_.allocateBlock()); @@ -98,7 +102,7 @@ class PacketManager template <typename PacketType, typename... Args> typename PacketType::Ptr getPacketFromExistingBuffer(uint8_t *buffer, std::size_t length, - Args &&... args) { + Args &&...args) { auto offset = offsetof(PacketStorage, align); auto memory = reinterpret_cast<PacketType *>(buffer - offset); utils::STLAllocator<PacketType, MemoryPool> allocator(memory, diff --git a/libtransport/includes/hicn/transport/core/interest.h b/libtransport/includes/hicn/transport/core/interest.h index a5b9cf375..b7ce3c3a0 100644 --- a/libtransport/includes/hicn/transport/core/interest.h +++ b/libtransport/includes/hicn/transport/core/interest.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -40,16 +40,15 @@ class Interest public: using Ptr = std::shared_ptr<Interest>; - Interest(Packet::Format format = HF_INET6_TCP, - std::size_t additional_header_size = 0); + Interest(Packet::Format format, std::size_t additional_header_size = 0); - Interest(const Name &interest_name, Packet::Format format = HF_INET6_TCP, + Interest(const Name &interest_name, Packet::Format format, std::size_t additional_header_size = 0); Interest(MemBuf &&buffer); template <typename... Args> - Interest(CopyBufferOp op, Args &&... args) + Interest(CopyBufferOp op, Args &&...args) : Packet(op, std::forward<Args>(args)...) { if (hicn_interest_get_name(format_, packet_start_, name_.getStructReference()) < 0) { @@ -58,7 +57,7 @@ class Interest } template <typename... Args> - Interest(WrapBufferOp op, Args &&... args) + Interest(WrapBufferOp op, Args &&...args) : Packet(op, std::forward<Args>(args)...) { if (hicn_interest_get_name(format_, packet_start_, name_.getStructReference()) < 0) { @@ -67,8 +66,12 @@ class Interest } template <typename... Args> - Interest(CreateOp op, Args &&... args) - : Packet(op, std::forward<Args>(args)...) {} + Interest(CreateOp op, Args &&...args) + : Packet(op, std::forward<Args>(args)...) { + if (hicn_packet_set_interest(format_, packet_start_) < 0) { + throw errors::MalformedPacketException(); + } + } /* Move constructor */ Interest(Interest &&other_interest); diff --git a/libtransport/includes/hicn/transport/core/io_module.h b/libtransport/includes/hicn/transport/core/io_module.h index ea3cf4e16..817d96d00 100644 --- a/libtransport/includes/hicn/transport/core/io_module.h +++ b/libtransport/includes/hicn/transport/core/io_module.h @@ -40,7 +40,7 @@ typedef struct { class Connector; -class IoModule { +class IoModule : utils::NonCopyable { protected: IoModule() : inet_address_({}), @@ -64,15 +64,20 @@ class IoModule { virtual bool isConnected() = 0; virtual void init(Connector::PacketReceivedCallback &&receive_callback, + Connector::PacketSentCallback &&sent_callback, Connector::OnReconnectCallback &&reconnect_callback, asio::io_service &io_service, const std::string &app_name = "Libtransport") = 0; virtual void registerRoute(const Prefix &prefix) = 0; + virtual void sendMapme() {} + + virtual void setForwardingStrategy(const Prefix &prefix, + std::string &strategy){}; virtual std::uint32_t getMtu() = 0; - virtual bool isControlMessage(const uint8_t *message) = 0; + virtual bool isControlMessage(utils::MemBuf &packet_buffer) = 0; virtual void processControlMessageReply(utils::MemBuf &packet_buffer) = 0; @@ -89,7 +94,7 @@ class IoModule { } } - virtual void send(const uint8_t *packet, std::size_t len) = 0; + virtual void send(const utils::MemBuf::Ptr &buffer) = 0; void setContentStoreSize(uint32_t cs_size) { content_store_reserved_ = cs_size; diff --git a/libtransport/includes/hicn/transport/core/name.h b/libtransport/includes/hicn/transport/core/name.h index 033582289..5cb4efd10 100644 --- a/libtransport/includes/hicn/transport/core/name.h +++ b/libtransport/includes/hicn/transport/core/name.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -51,7 +51,7 @@ class Name { public: using NameStruct = hicn_name_t; - using Type = hicn_name_type_t; + enum class Type { UNDEFINED, V4, V6 }; Name(); @@ -69,6 +69,8 @@ class Name { Name(const Name &name); + ~Name(); + Name &operator=(const Name &name); bool operator==(const Name &name) const; @@ -91,14 +93,11 @@ class Name { uint32_t getSuffix() const; - std::shared_ptr<Sockaddr> getAddress() const; - Name &setSuffix(uint32_t seq_number); ip_prefix_t toIpAddress() const; - void copyToDestination(uint8_t *destination, - bool include_suffix = false) const; + void copyPrefixToDestination(uint8_t *destination) const; int getAddressFamily() const; diff --git a/libtransport/includes/hicn/transport/core/packet.h b/libtransport/includes/hicn/transport/core/packet.h index 269a1571a..059430f1d 100644 --- a/libtransport/includes/hicn/transport/core/packet.h +++ b/libtransport/includes/hicn/transport/core/packet.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -30,11 +30,7 @@ namespace transport { namespace auth { class Signer; -class AsymmetricSigner; -class SymmetricSigner; class Verifier; -class AsymmetricVerifier; -class SymmetricVerifier; } // namespace auth namespace core { @@ -51,16 +47,13 @@ namespace core { class Packet : public utils::MemBuf, public std::enable_shared_from_this<Packet> { friend class auth::Signer; - friend class auth::SymmetricSigner; - friend class auth::AsymmetricSigner; friend class auth::Verifier; - friend class auth::AsymmetricVerifier; - friend class auth::SymmetricVerifier; public: using Ptr = std::shared_ptr<Packet>; using MemBufPtr = std::shared_ptr<utils::MemBuf>; using Format = hicn_format_t; + static constexpr size_t default_mtu = 1500; /** @@ -68,146 +61,73 @@ class Packet : public utils::MemBuf, * the eventual payload will be added by prepending the payload buffer * to the buffer chain whose the fist buffer is the header itself. */ - Packet(Format format = HF_INET6_TCP, std::size_t additional_header_size = 0); - - /** - * Create new IP packet using raw buffer. - */ - + Packet(Format format, std::size_t additional_header_size = 0); /* Copy buffer */ Packet(CopyBufferOp, const uint8_t *buffer, std::size_t size); /* Wrap buffer */ Packet(WrapBufferOp, uint8_t *buffer, std::size_t length, std::size_t size); /* Create new using pre-allocated buffer */ Packet(CreateOp, uint8_t *buffer, std::size_t length, std::size_t size, - Format format = HF_INET6_TCP, std::size_t additional_header_size = 0); - /* Move MemBuf */ - Packet(MemBuf &&buffer); + Format format, std::size_t additional_header_size = 0); + Packet(MemBuf &&buffer); Packet(Packet &&other); - - /* - * Copy constructor and assignemnt operators. - */ Packet(const Packet &other); - Packet &operator=(const Packet &other); - - friend bool operator==(const Packet &l_packet, const Packet &r_packet); + // Destructor virtual ~Packet(); - static std::size_t getHeaderSizeFromFormat(Format format, - std::size_t signature_size = 0) { - std::size_t header_length; - hicn_packet_get_header_length_from_format(format, &header_length); - int is_ah = _is_ah(format); - return is_ah * (header_length + signature_size) + (!is_ah) * header_length; - } - - static std::size_t getHeaderSizeFromBuffer(Format format, - const uint8_t *buffer); - - static std::size_t getPayloadSizeFromBuffer(Format format, - const uint8_t *buffer); - - static bool isInterest(const uint8_t *buffer); - - bool isInterest(); - - static Format getFormatFromBuffer(const uint8_t *buffer, std::size_t length) { - Format format = HF_UNSPEC; - hicn_packet_get_format((const hicn_header_t *)buffer, &format); - return format; - } - - void reset() { - clear(); - packet_start_ = reinterpret_cast<hicn_header_t *>(writableData()); - header_offset_ = 0; - format_ = HF_UNSPEC; - payload_type_ = PayloadType::UNSPECIFIED; - name_.clear(); - - if (isChained()) { - separateChain(next(), prev()); - } - } - - void setFormat(Packet::Format format = HF_INET6_TCP, - std::size_t additional_header_size = 0); - - std::size_t payloadSize() const; - - std::size_t headerSize() const; + // Operators + Packet &operator=(const Packet &other); + friend bool operator==(const Packet &l_packet, const Packet &r_packet); + // Cast to MemBuf std::shared_ptr<utils::MemBuf> acquireMemBufReference(); - virtual const Name &getName() const = 0; + // Format + Format getFormat() const; + void setFormat(Packet::Format format, std::size_t additional_header_size = 0); + // Name + virtual const Name &getName() const = 0; virtual Name &getWritableName() = 0; - virtual void setName(const Name &name) = 0; + // Lifetime virtual void setLifetime(uint32_t lifetime) = 0; - virtual uint32_t getLifetime() const = 0; - Packet &appendPayload(const uint8_t *buffer, std::size_t length); - - Packet &appendPayload(std::unique_ptr<utils::MemBuf> &&payload); - - std::unique_ptr<utils::MemBuf> getPayload() const; - - Packet &updateLength(std::size_t length = 0); - - PayloadType getPayloadType() const; - - Packet &setPayloadType(PayloadType payload_type); - - Format getFormat() const; - - void dump() const; - - static void dump(uint8_t *buffer, std::size_t length); - + // Locator virtual void setLocator(const ip_address_t &locator) = 0; - virtual ip_address_t getLocator() const = 0; - /** - * @brief Set signature timestamp, in milliseconds. - */ - void setSignatureTimestamp(const uint64_t ×tamp_milliseconds); - - uint64_t getSignatureTimestamp() const; - - void setValidationAlgorithm(const auth::CryptoSuite &validation_algorithm); - - auth::CryptoSuite getValidationAlgorithm() const; - - void setKeyId(const auth::KeyId &key_id); + // Payload type + PayloadType getPayloadType() const; + Packet &setPayloadType(PayloadType payload_type); - auth::KeyId getKeyId() const; + // Payload + std::unique_ptr<utils::MemBuf> getPayload() const; + Packet &appendPayload(std::unique_ptr<utils::MemBuf> &&payload); + Packet &appendPayload(const uint8_t *buffer, std::size_t length); - virtual auth::CryptoHash computeDigest(auth::CryptoHashType algorithm) const; + // Sizes + std::size_t headerSize() const; + std::size_t payloadSize() const; - void setChecksum() { - uint16_t partial_csum = - csum(data() + HICN_V6_TCP_HDRLEN, length() - HICN_V6_TCP_HDRLEN, 0); + // Digest + auth::CryptoHash computeDigest(auth::CryptoHashType algorithm) const; - for (utils::MemBuf *current = next(); current != this; - current = current->next()) { - partial_csum = csum(current->data(), current->length(), ~partial_csum); - } + // Reset packet + void reset(); - if (hicn_packet_compute_header_checksum(format_, packet_start_, - partial_csum) < 0) { - throw errors::MalformedPacketException(); - } - } + // Utils + bool isInterest(); + Packet &updateLength(std::size_t length = 0); + void dump() const; + // TCP methods + void setChecksum(); bool checkIntegrity() const; - Packet &setSyn(); Packet &resetSyn(); bool testSyn() const; @@ -222,53 +142,45 @@ class Packet : public utils::MemBuf, bool testFin() const; Packet &resetFlags(); std::string printFlags() const; - Packet &setSrcPort(uint16_t srcPort); Packet &setDstPort(uint16_t dstPort); uint16_t getSrcPort() const; uint16_t getDstPort() const; - Packet &setTTL(uint8_t hops); uint8_t getTTL() const; + // Authentication Header methods + bool hasAH() const; + std::vector<uint8_t> getSignature() const; + std::size_t getSignatureFieldSize() const; + std::size_t getSignatureSize() const; + uint64_t getSignatureTimestamp() const; + auth::KeyId getKeyId() const; + auth::CryptoSuite getValidationAlgorithm() const; + void setSignature(const std::vector<uint8_t> &signature); + void setSignatureFieldSize(std::size_t size); + void setSignatureSize(std::size_t size); + void setSignatureTimestamp(const uint64_t ×tamp_ms); + void setKeyId(const auth::KeyId &key_id); + void setValidationAlgorithm(const auth::CryptoSuite &algo); + + // Static methods + static Format toAHFormat(const Format &format); + static Format getFormatFromBuffer(const uint8_t *buffer, std::size_t length); + static std::size_t getHeaderSizeFromFormat(Format format, + std::size_t signature_size = 0); + static std::size_t getHeaderSizeFromBuffer(Format format, + const uint8_t *buffer); + static std::size_t getPayloadSizeFromBuffer(Format format, + const uint8_t *buffer); + static bool isInterest(const uint8_t *buffer, + Format format = Format::HF_UNSPEC); + static void dump(uint8_t *buffer, std::size_t length); + private: virtual void resetForHash() = 0; - void setSignatureSize(std::size_t size_bytes); - void setSignatureSizeGap(std::size_t size_bytes); void prependPayload(const uint8_t **buffer, std::size_t *size); - bool authenticationHeader() const { return _is_ah(format_); } - - std::size_t getSignatureSize() const { - size_t size_bytes; - int ret = - hicn_packet_get_signature_size(format_, packet_start_, &size_bytes); - - if (ret < 0) { - throw errors::RuntimeException("Packet without Authentication Header."); - } - - return size_bytes; - } - - std::size_t getSignatureSizeGap() const { - uint8_t size_bytes; - int ret = - hicn_packet_get_signature_gap(format_, packet_start_, &size_bytes); - - if (ret < 0) { - throw errors::RuntimeException("Packet without Authentication Header."); - } - - return (size_t)size_bytes; - } - - std::size_t getSignatureSizeReal() const { - return getSignatureSize() - getSignatureSizeGap(); - } - - uint8_t *getSignature() const; - protected: hicn_header_t *packet_start_; std::size_t header_offset_; diff --git a/libtransport/includes/hicn/transport/core/payload_type.h b/libtransport/includes/hicn/transport/core/payload_type.h index 8c918f792..3a682e177 100644 --- a/libtransport/includes/hicn/transport/core/payload_type.h +++ b/libtransport/includes/hicn/transport/core/payload_type.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/core/prefix.h b/libtransport/includes/hicn/transport/core/prefix.h index 7ef667bc8..13401e1a8 100644 --- a/libtransport/includes/hicn/transport/core/prefix.h +++ b/libtransport/includes/hicn/transport/core/prefix.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -35,6 +35,10 @@ class Prefix { Prefix(const core::Name &content_name, uint16_t prefix_length); + bool operator<(const Prefix &prefix) const; + + bool operator==(const Prefix &prefix) const; + std::unique_ptr<Sockaddr> toSockaddr() const; uint16_t getPrefixLength() const; diff --git a/libtransport/includes/hicn/transport/errors/CMakeLists.txt b/libtransport/includes/hicn/transport/errors/CMakeLists.txt index 1d35c5b34..c8642b51b 100644 --- a/libtransport/includes/hicn/transport/errors/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/errors/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/errors.h b/libtransport/includes/hicn/transport/errors/errors.h index b659820fa..09ce97221 100644 --- a/libtransport/includes/hicn/transport/errors/errors.h +++ b/libtransport/includes/hicn/transport/errors/errors.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/indexing_exception.h b/libtransport/includes/hicn/transport/errors/indexing_exception.h index 731314f0e..bdcc39aef 100644 --- a/libtransport/includes/hicn/transport/errors/indexing_exception.h +++ b/libtransport/includes/hicn/transport/errors/indexing_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/invalid_ip_address_exception.h b/libtransport/includes/hicn/transport/errors/invalid_ip_address_exception.h index 60226f576..26e0f98af 100644 --- a/libtransport/includes/hicn/transport/errors/invalid_ip_address_exception.h +++ b/libtransport/includes/hicn/transport/errors/invalid_ip_address_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/malformed_ahpacket_exception.h b/libtransport/includes/hicn/transport/errors/malformed_ahpacket_exception.h index f0cfe0b82..9b3ec7466 100644 --- a/libtransport/includes/hicn/transport/errors/malformed_ahpacket_exception.h +++ b/libtransport/includes/hicn/transport/errors/malformed_ahpacket_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/malformed_name_exception.h b/libtransport/includes/hicn/transport/errors/malformed_name_exception.h index 4ef45d2e8..57412cf7f 100644 --- a/libtransport/includes/hicn/transport/errors/malformed_name_exception.h +++ b/libtransport/includes/hicn/transport/errors/malformed_name_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/malformed_packet_exception.h b/libtransport/includes/hicn/transport/errors/malformed_packet_exception.h index ec5c97e6e..996d6a4ab 100644 --- a/libtransport/includes/hicn/transport/errors/malformed_packet_exception.h +++ b/libtransport/includes/hicn/transport/errors/malformed_packet_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/not_implemented_exception.h b/libtransport/includes/hicn/transport/errors/not_implemented_exception.h index e9869163d..3291fa425 100644 --- a/libtransport/includes/hicn/transport/errors/not_implemented_exception.h +++ b/libtransport/includes/hicn/transport/errors/not_implemented_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/null_pointer_exception.h b/libtransport/includes/hicn/transport/errors/null_pointer_exception.h index bd06485ed..7edb619b8 100644 --- a/libtransport/includes/hicn/transport/errors/null_pointer_exception.h +++ b/libtransport/includes/hicn/transport/errors/null_pointer_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/runtime_exception.h b/libtransport/includes/hicn/transport/errors/runtime_exception.h index ba5128a7e..498c41a7c 100644 --- a/libtransport/includes/hicn/transport/errors/runtime_exception.h +++ b/libtransport/includes/hicn/transport/errors/runtime_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/tokenizer_exception.h b/libtransport/includes/hicn/transport/errors/tokenizer_exception.h index 76eda838e..31f79644a 100644 --- a/libtransport/includes/hicn/transport/errors/tokenizer_exception.h +++ b/libtransport/includes/hicn/transport/errors/tokenizer_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/errors/unexpected_manifest_exception.h b/libtransport/includes/hicn/transport/errors/unexpected_manifest_exception.h index 6f71471e4..c8e4eadd4 100644 --- a/libtransport/includes/hicn/transport/errors/unexpected_manifest_exception.h +++ b/libtransport/includes/hicn/transport/errors/unexpected_manifest_exception.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/http/CMakeLists.txt b/libtransport/includes/hicn/transport/http/CMakeLists.txt index 9f4cdaf39..36a2ca5ce 100644 --- a/libtransport/includes/hicn/transport/http/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/http/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2020 Cisco and/or its affiliates. +# Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/http/client_connection.h b/libtransport/includes/hicn/transport/http/client_connection.h index 7e78e9c59..d5edf6cd0 100644 --- a/libtransport/includes/hicn/transport/http/client_connection.h +++ b/libtransport/includes/hicn/transport/http/client_connection.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -31,7 +31,7 @@ namespace http { using namespace interface; using namespace core; -class HTTPClientConnection { +class HTTPClientConnection : private utils::NonCopyable { static constexpr uint32_t max_buffer_capacity = 64 * 1024; public: @@ -39,7 +39,7 @@ class HTTPClientConnection { public: virtual void onBytesReceived(std::unique_ptr<utils::MemBuf> &&buffer) = 0; virtual void onSuccess(std::size_t bytes) = 0; - virtual void onError(const std::error_code ec) = 0; + virtual void onError(const std::error_code &ec) = 0; }; enum class RC : uint32_t { DOWNLOAD_FAILED, DOWNLOAD_SUCCESS }; diff --git a/libtransport/includes/hicn/transport/http/default_values.h b/libtransport/includes/hicn/transport/http/default_values.h index 2d5a6b821..8cf247e19 100644 --- a/libtransport/includes/hicn/transport/http/default_values.h +++ b/libtransport/includes/hicn/transport/http/default_values.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/http/facade.h b/libtransport/includes/hicn/transport/http/facade.h index 8a465ce94..c886a7588 100644 --- a/libtransport/includes/hicn/transport/http/facade.h +++ b/libtransport/includes/hicn/transport/http/facade.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/http/message.h b/libtransport/includes/hicn/transport/http/message.h index b8756224f..a07941413 100644 --- a/libtransport/includes/hicn/transport/http/message.h +++ b/libtransport/includes/hicn/transport/http/message.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/http/request.h b/libtransport/includes/hicn/transport/http/request.h index b62f5b061..9939fbcaf 100644 --- a/libtransport/includes/hicn/transport/http/request.h +++ b/libtransport/includes/hicn/transport/http/request.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/http/response.h b/libtransport/includes/hicn/transport/http/response.h index bab41acb8..79def27cd 100644 --- a/libtransport/includes/hicn/transport/http/response.h +++ b/libtransport/includes/hicn/transport/http/response.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt b/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt index 40623dfe1..1acaadcf0 100644 --- a/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/interfaces/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 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: @@ -21,6 +21,7 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/global_conf_interface.h ${CMAKE_CURRENT_SOURCE_DIR}/statistics.h ${CMAKE_CURRENT_SOURCE_DIR}/portal.h + ${CMAKE_CURRENT_SOURCE_DIR}/notification.h ) if (${OPENSSL_VERSION} VERSION_EQUAL "1.1.1a" OR ${OPENSSL_VERSION} VERSION_GREATER "1.1.1a") diff --git a/libtransport/includes/hicn/transport/interfaces/callbacks.h b/libtransport/includes/hicn/transport/interfaces/callbacks.h index 22e111799..41e4fba3b 100644 --- a/libtransport/includes/hicn/transport/interfaces/callbacks.h +++ b/libtransport/includes/hicn/transport/interfaces/callbacks.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -16,6 +16,7 @@ #pragma once #include <hicn/transport/auth/policies.h> +#include <hicn/transport/interfaces/notification.h> #include <hicn/transport/interfaces/statistics.h> #include <functional> @@ -60,6 +61,12 @@ using ConsumerTimerCallback = std::function<void(ConsumerSocket &, const TransportStatistics &stats)>; /** + * The ConsumerTimerCallback is called when the forwarding/recovery stategy is + * changes. + */ +using StrategyCallback = std::function<void(notification::Strategy strategy)>; + +/** * The ProducerContentCallback will be called by the producer socket right after * a content has been segmented and published. */ diff --git a/libtransport/includes/hicn/transport/interfaces/notification.h b/libtransport/includes/hicn/transport/interfaces/notification.h new file mode 100644 index 000000000..a0945c3f0 --- /dev/null +++ b/libtransport/includes/hicn/transport/interfaces/notification.h @@ -0,0 +1,31 @@ +/* + * 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. + */ + +#pragma once + +#include <variant> + +namespace transport { +namespace interface { +namespace notification { + +enum class ForwardingStrategy { BEST_PATH, REPLICATION, BOTH, NONE }; +enum class RecoveryStrategy { RECOVERY_OFF, RTX_ONLY, FEC_ONLY, RTX_AND_FEC }; + +using Strategy = std::variant<ForwardingStrategy, RecoveryStrategy>; + +} // namespace notification +} // namespace interface +} // namespace transport
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_consumer.h b/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_consumer.h index 224493f00..a67634c1e 100644 --- a/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_consumer.h +++ b/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_consumer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_producer.h b/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_producer.h index d86744514..e197a3658 100644 --- a/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_producer.h +++ b/libtransport/includes/hicn/transport/interfaces/p2psecure_socket_producer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -15,7 +15,6 @@ #pragma once -#include <hicn/transport/auth/identity.h> #include <hicn/transport/interfaces/socket_producer.h> namespace transport { @@ -25,8 +24,8 @@ namespace interface { class P2PSecureProducerSocket : public ProducerSocket { public: P2PSecureProducerSocket(); - P2PSecureProducerSocket(bool rtc, - const std::shared_ptr<auth::Identity> &identity); + P2PSecureProducerSocket(bool rtc, std::string &keystore_path, + std::string &keystore_pwd); ~P2PSecureProducerSocket() = default; }; diff --git a/libtransport/includes/hicn/transport/interfaces/portal.h b/libtransport/includes/hicn/transport/interfaces/portal.h index 66fc84256..1a22b1f1d 100644 --- a/libtransport/includes/hicn/transport/interfaces/portal.h +++ b/libtransport/includes/hicn/transport/interfaces/portal.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -19,6 +19,8 @@ #include <hicn/transport/core/content_object.h> #include <hicn/transport/core/interest.h> #include <hicn/transport/core/prefix.h> +#include <hicn/transport/utils/event_thread.h> +#include <hicn/transport/utils/noncopyable.h> #include <functional> @@ -28,58 +30,18 @@ namespace transport { namespace interface { -template <typename PrefixType> -class BasicBindConfig { - static_assert(std::is_same<core::Prefix, PrefixType>::value, - "Prefix must be a Prefix type."); - - const uint32_t standard_cs_reserved = 5000; - - public: - template <typename T> - BasicBindConfig(T &&prefix) - : prefix_(std::forward<T &&>(prefix)), - content_store_reserved_(standard_cs_reserved) {} - - template <typename T> - BasicBindConfig(T &&prefix, uint32_t cs_reserved) - : prefix_(std::forward<T &&>(prefix)), - content_store_reserved_(cs_reserved) {} - - TRANSPORT_ALWAYS_INLINE const PrefixType &prefix() const { return prefix_; } - - TRANSPORT_ALWAYS_INLINE uint32_t csReserved() const { - return content_store_reserved_; - } - - private: - PrefixType prefix_; - uint32_t content_store_reserved_; -}; - -using BindConfig = BasicBindConfig<core::Prefix>; - -class Portal { +class Portal : private utils::NonCopyable { public: /** - * Consumer callback is an abstract class containing two methods to be - * implemented by a consumer application. + * Transport callback is an abstract class containing two methods to be + * implemented by a consumer/producer application. */ - class ConsumerCallback { + class TransportCallback { public: + virtual void onInterest(core::Interest &i) = 0; virtual void onContentObject(core::Interest &i, core::ContentObject &c) = 0; virtual void onTimeout(core::Interest::Ptr &i, const core::Name &n) = 0; - virtual void onError(std::error_code ec) = 0; - }; - - /** - * Producer callback is an abstract class containing two methods to be - * implemented by a producer application. - */ - class ProducerCallback { - public: - virtual void onInterest(core::Interest &i) = 0; - virtual void onError(std::error_code ec) = 0; + virtual void onError(const std::error_code &ec) = 0; }; using OnContentObjectCallback = @@ -89,21 +51,14 @@ class Portal { Portal(); - Portal(asio::io_service &io_service); + Portal(::utils::EventThread &worker); /** - * Set the consumer callback. - * - * @param consumer_callback - The pointer to the ConsumerCallback object. - */ - void setConsumerCallback(ConsumerCallback *consumer_callback); - - /** - * Set the producer callback. + * Set the transport protocl callback. * * @param producer_callback - The pointer to the ProducerCallback object. */ - void setProducerCallback(ProducerCallback *producer_callback); + void registerTransportCallback(TransportCallback *transport_callback); /** * Connect the transport to the local hicn forwarder. @@ -146,34 +101,12 @@ class Portal { UNSET_CALLBACK); /** - * Register a producer name to the local forwarder and optionally set the - * content store size in a per-face manner. - * - * @param config - The configuration for the local forwarder binding. - */ - void bind(const BindConfig &config); - - void runEventsLoop(); - - /** - * Run one event and return. - */ - void runOneEvent(); - - /** * Send a data packet to the local forwarder. As opposite to sendInterest, the * ownership of the content object is not transferred to the portal. * * @param content_object - The data packet. */ void sendContentObject(core::ContentObject &content_object); - /** - * Stop the event loop, canceling all the pending events in the event queue. - * - * Beware that stopping the event loop DOES NOT disconnect the transport from - * the local forwarder, the connector underneath will stay connected. - */ - void stopEventsLoop(); /** * Disconnect the transport from the local forwarder. @@ -188,15 +121,26 @@ class Portal { /** * Get a reference to the io_service object. */ - asio::io_service &getIoService(); + utils::EventThread &getThread(); /** * Register a route to the local forwarder. */ void registerRoute(core::Prefix &prefix); + /** + * Send a MAP-Me command to traverse NATs. + */ + void sendMapme(); + + /** + * Set forwarding strategy + */ + void setForwardingStrategy(core::Prefix &prefix, std::string &strategy); + private: - void *implementation_; + class Impl; + Impl *implementation_; }; } // namespace interface diff --git a/libtransport/includes/hicn/transport/interfaces/publication_options.h b/libtransport/includes/hicn/transport/interfaces/publication_options.h index 6910e5371..03a0763f5 100644 --- a/libtransport/includes/hicn/transport/interfaces/publication_options.h +++ b/libtransport/includes/hicn/transport/interfaces/publication_options.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/interfaces/socket_consumer.h b/libtransport/includes/hicn/transport/interfaces/socket_consumer.h index 5e0e81b9f..e26fe5a85 100644 --- a/libtransport/includes/hicn/transport/interfaces/socket_consumer.h +++ b/libtransport/includes/hicn/transport/interfaces/socket_consumer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -23,6 +23,8 @@ #include <hicn/transport/interfaces/callbacks.h> #include <hicn/transport/interfaces/socket_options_default_values.h> #include <hicn/transport/interfaces/socket_options_keys.h> +#include <hicn/transport/utils/event_thread.h> +#include <hicn/transport/utils/noncopyable.h> #define CONSUMER_FINISHED 0 #define CONSUMER_BUSY 1 @@ -45,7 +47,7 @@ using namespace core; * It allows to retrieve an application data from one/many producers, by * hiding all the complexity of the transport protocol used underneath. */ -class ConsumerSocket { +class ConsumerSocket : private utils::NonCopyable { public: /** * The ReadCallback is a class which can be used by the transport for both @@ -124,7 +126,7 @@ class ConsumerSocket { * * @param ec - An error code describing the error. */ - virtual void readError(const std::error_code ec) noexcept = 0; + virtual void readError(const std::error_code &ec) noexcept = 0; /** * This callback will be invoked when the whole content is retrieved. The @@ -166,7 +168,12 @@ class ConsumerSocket { * 110, 104-117 * - RTC: Real time communication */ - explicit ConsumerSocket(int protocol, asio::io_service &io_service); + explicit ConsumerSocket(int protocol, ::utils::EventThread &worker); + + /** + * @brief Move contructor + */ + ConsumerSocket(ConsumerSocket &&other) noexcept; /** * @brief Destroy the consumer socket. @@ -200,12 +207,10 @@ class ConsumerSocket { * content retrieval succeeded. This information can be obtained from the * error code in CONTENT_RETRIEVED callback. */ - int consume(const Name &name); - int asyncConsume(const Name &name); + int consume(const Name &name, bool blocking = false); /** - * Stops the consumer socket. If several downloads are queued (using - * asyncConsume), this call stops just the current one. + * Stops the consumer socket. */ void stop(); @@ -255,6 +260,12 @@ class ConsumerSocket { int setSocketOption(int socket_option_key, ConsumerTimerCallback socket_option_value); + int setSocketOption(int socket_option_key, + StrategyCallback socket_option_value); + + int setSocketOption(int socket_option_key, + Packet::Format socket_option_value); + int getSocketOption(int socket_option_key, double &socket_option_value); int getSocketOption(int socket_option_key, uint32_t &socket_option_value); @@ -280,8 +291,14 @@ class ConsumerSocket { ConsumerTimerCallback **socket_option_value); int getSocketOption(int socket_option_key, + StrategyCallback **socket_option_value); + + int getSocketOption(int socket_option_key, interface::TransportStatistics **socket_option_value); + int getSocketOption(int socket_option_key, + Packet::Format &socket_option_value); + protected: ConsumerSocket(); std::unique_ptr<implementation::ConsumerSocket> socket_; diff --git a/libtransport/includes/hicn/transport/interfaces/socket_options_default_values.h b/libtransport/includes/hicn/transport/interfaces/socket_options_default_values.h index f4945ac8a..04454852b 100644 --- a/libtransport/includes/hicn/transport/interfaces/socket_options_default_values.h +++ b/libtransport/includes/hicn/transport/interfaces/socket_options_default_values.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -15,7 +15,10 @@ #pragma once +extern "C" { #include <hicn/base.h> +#include <hicn/compat.h> +} #include <chrono> #include <cstdint> @@ -26,6 +29,16 @@ namespace interface { namespace default_values { +// Packet format +// #define NEW_PACKET_FORMAT +static constexpr hicn_format_t packet_format = +#ifdef NEW_PACKET_FORMAT + HF_INET6_UDP; +#else + HF_INET6_TCP; +#endif + +// Parameters static constexpr uint32_t interest_lifetime = 1001; // milliseconds static constexpr uint32_t never_expire_time = HICN_MAX_LIFETIME; static constexpr uint32_t content_object_expiry_time = @@ -40,6 +53,8 @@ static constexpr uint32_t key_locator_size = 60; // bytes static constexpr uint32_t limit_guard = 80; // bytes static constexpr uint32_t digest_size = 34; // bytes static constexpr uint32_t max_out_of_order_segments = 3; // content object +static constexpr uint32_t max_unverified_delay = 2001; // milliseconds +static constexpr uint32_t manifest_capacity = 30; // RAAQM static constexpr int sample_number = 30; diff --git a/libtransport/includes/hicn/transport/interfaces/socket_options_keys.h b/libtransport/includes/hicn/transport/interfaces/socket_options_keys.h index 00cd44075..90f218770 100644 --- a/libtransport/includes/hicn/transport/interfaces/socket_options_keys.h +++ b/libtransport/includes/hicn/transport/interfaces/socket_options_keys.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -26,15 +26,27 @@ namespace transport { namespace interface { typedef enum { - RAAQM = 0, - CBR = 1, - RTC = 2, + UNKNOWN = 0, + BYTE_STREAM = 1, + RTC_PROD = 2, +} ProductionProtocolAlgorithms; + +typedef enum { + RAAQM = 10, + CBR = 11, + RTC = 12, } TransportProtocolAlgorithms; typedef enum { - BYTE_STREAM = 10, - RTC_PROD = 11, -} ProductionProtocolAlgorithms; + RECOVERY_OFF = 20, + RTX_ONLY = 21, + FEC_ONLY = 22, + DELAY_BASED = 23, + LOW_RATE = 24, + LOW_RATE_AND_BESTPATH = 25, + LOW_RATE_AND_REPLICATION = 26, + LOW_RATE_AND_ALL_FWD_STRATEGIES = 27, +} RtcTransportRecoveryStrategies; typedef enum { INPUT_BUFFER_SIZE = 101, @@ -45,6 +57,8 @@ typedef enum { DATA_PACKET_SIZE = 106, INTEREST_LIFETIME = 107, CONTENT_OBJECT_EXPIRY_TIME = 108, + MAX_SEGMENT_SIZE = 109, + MAX_UNVERIFIED_TIME = 110, MIN_WINDOW_SIZE = 111, MAX_WINDOW_SIZE = 112, CURRENT_WINDOW_SIZE = 113, @@ -57,7 +71,9 @@ typedef enum { SIGNER = 121, VERIFIER = 122, STATS_INTERVAL = 125, - SUFFIX_STRATEGY = 126 + SUFFIX_STRATEGY = 126, + PACKET_FORMAT = 127, + FEC_TYPE = 128, } GeneralTransportOptions; typedef enum { @@ -87,7 +103,9 @@ typedef enum { CONTENT_OBJECT_TO_VERIFY = 413, VERIFICATION_FAILED = 414, READ_CALLBACK = 415, - STATS_SUMMARY = 416 + STATS_SUMMARY = 416, + FWD_STRATEGY_CHANGE = 417, + REC_STRATEGY_CHANGE = 418, } ConsumerCallbacksOptions; typedef enum { @@ -100,7 +118,8 @@ typedef enum { CONTENT_OBJECT_READY = 510, CONTENT_OBJECT_OUTPUT = 511, CONTENT_PRODUCED = 512, - CONTENT_OBJECT_TO_SIGN = 513 + CONTENT_OBJECT_TO_SIGN = 513, + PRODUCER_CALLBACK = 514, } ProducerCallbacksOptions; typedef enum { OUTPUT_INTERFACE = 601 } DataLinkOptions; @@ -116,6 +135,11 @@ typedef enum { RSA_256 = 802, } SignatureType; +typedef enum { + RECOVERY_STRATEGY = 901, + AGGREGATED_DATA = 902, +} RtcTransportOptions; + } // namespace interface } // end namespace transport diff --git a/libtransport/includes/hicn/transport/interfaces/socket_producer.h b/libtransport/includes/hicn/transport/interfaces/socket_producer.h index 27b603dfe..77b89742a 100644 --- a/libtransport/includes/hicn/transport/interfaces/socket_producer.h +++ b/libtransport/includes/hicn/transport/interfaces/socket_producer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -23,6 +23,8 @@ #include <hicn/transport/interfaces/callbacks.h> #include <hicn/transport/interfaces/socket_options_default_values.h> #include <hicn/transport/interfaces/socket_options_keys.h> +#include <hicn/transport/utils/event_thread.h> +#include <hicn/transport/utils/noncopyable.h> namespace transport { @@ -34,12 +36,28 @@ namespace interface { using namespace core; -class ProducerSocket { +class ProducerSocket : private utils::NonCopyable { public: + /** + * @brief This class is used by the transport to notify events to the + * application. + */ + class Callback { + public: + /** + * @brief This will invoked in an error occurred in the production protocol. + * + * @param ec - An error code describing the error. + */ + virtual void produceError(const std::error_code &ec) noexcept = 0; + }; + explicit ProducerSocket( int protocol = ProductionProtocolAlgorithms::BYTE_STREAM); - explicit ProducerSocket(int protocol, asio::io_service &io_service); + explicit ProducerSocket(int protocol, ::utils::EventThread &worker); + + ProducerSocket(ProducerSocket &&other) noexcept; virtual ~ProducerSocket(); @@ -63,21 +81,18 @@ class ProducerSocket { uint32_t produceDatagram(const Name &content_name, std::unique_ptr<utils::MemBuf> &&buffer); - void asyncProduce(const Name &suffix, const uint8_t *buf, size_t buffer_size, - bool is_last = true, uint32_t *start_offset = nullptr); - - void asyncProduce(Name content_name, std::unique_ptr<utils::MemBuf> &&buffer, - bool is_last, uint32_t offset, - uint32_t **last_segment = nullptr); - void produce(ContentObject &content_object); - void serveForever(); + void sendMapme(); void stop(); + void start(); + asio::io_service &getIoService(); + int setSocketOption(int socket_option_key, Callback *socket_option_value); + int setSocketOption(int socket_option_key, uint32_t socket_option_value); int setSocketOption(int socket_option_key, @@ -111,6 +126,9 @@ class ProducerSocket { int setSocketOption(int socket_option_key, const std::string &socket_option_value); + int setSocketOption(int socket_option_key, + Packet::Format socket_option_value); + int getSocketOption(int socket_option_key, uint32_t &socket_option_value); int getSocketOption(int socket_option_key, bool &socket_option_value); @@ -138,6 +156,9 @@ class ProducerSocket { int getSocketOption(int socket_option_key, std::string &socket_option_value); + int getSocketOption(int socket_option_key, + Packet::Format &socket_option_value); + protected: ProducerSocket(bool); std::unique_ptr<implementation::ProducerSocket> socket_; diff --git a/libtransport/includes/hicn/transport/interfaces/statistics.h b/libtransport/includes/hicn/transport/interfaces/statistics.h index 1ff6f3edd..4d9e1bfe2 100644 --- a/libtransport/includes/hicn/transport/interfaces/statistics.h +++ b/libtransport/includes/hicn/transport/interfaces/statistics.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -16,6 +16,7 @@ #pragma once #include <hicn/transport/portability/c_portability.h> +#include <hicn/transport/utils/chrono_typedefs.h> #include <cstdint> @@ -37,6 +38,8 @@ class TransportStatistics { static constexpr double default_alpha = 0.7; public: + enum class statsAlerts : uint8_t { CONGESTION, LATENCY, LOSSES }; + TransportStatistics(double alpha = default_alpha) : retx_count_(0), bytes_received_(0), @@ -51,11 +54,15 @@ class TransportStatistics { lost_data_(0), definitely_lost_data_(0), recovered_data_(0), - status_(-1), + status_(0), // avg_data_rtt_(0), avg_pending_pkt_(0.0), received_nacks_(0), - received_fec_(0) {} + received_fec_(0), + in_congestion_(false), + residual_loss_rate_(0.0), + quality_score_(5), + alerts_(0) {} TRANSPORT_ALWAYS_INLINE void updateRetxCount(uint64_t retx) { retx_count_ += retx; @@ -65,8 +72,11 @@ class TransportStatistics { bytes_received_ += bytes; } - TRANSPORT_ALWAYS_INLINE void updateAverageRtt(uint64_t rtt) { - average_rtt_ = (alpha_ * average_rtt_) + ((1. - alpha_) * double(rtt)); + TRANSPORT_ALWAYS_INLINE void updateAverageRtt( + const utils::SteadyTime::Milliseconds &rtt) { + auto rtt_milliseconds = rtt.count(); + average_rtt_ = + (alpha_ * average_rtt_) + ((1. - alpha_) * double(rtt_milliseconds)); } TRANSPORT_ALWAYS_INLINE void updateAverageWindowSize(double current_window) { @@ -120,6 +130,26 @@ class TransportStatistics { received_fec_ += pkt; } + TRANSPORT_ALWAYS_INLINE void updateResidualLossRate(double val) { + residual_loss_rate_ = val; + } + + TRANSPORT_ALWAYS_INLINE void updateQualityScore(uint8_t val) { + quality_score_ = val; + } + + TRANSPORT_ALWAYS_INLINE void updateCongestionState(bool state) { + in_congestion_ = state; + } + + TRANSPORT_ALWAYS_INLINE void setAlert(statsAlerts x) { + alerts_ |= 1UL << (uint32_t)x; + } + + TRANSPORT_ALWAYS_INLINE void clearAlert(statsAlerts x) { + alerts_ &= ~(1UL << (uint32_t)x); + } + TRANSPORT_ALWAYS_INLINE uint64_t getRetxCount() const { return retx_count_; } TRANSPORT_ALWAYS_INLINE uint64_t getBytesRecv() const { @@ -174,6 +204,18 @@ class TransportStatistics { return received_fec_; } + TRANSPORT_ALWAYS_INLINE double getResidualLossRate() const { + return residual_loss_rate_; + } + + TRANSPORT_ALWAYS_INLINE uint8_t getQualityScore() const { + return quality_score_; + } + + TRANSPORT_ALWAYS_INLINE bool isCongested() const { return in_congestion_; } + + TRANSPORT_ALWAYS_INLINE uint32_t getAlerts() const { return alerts_; } + TRANSPORT_ALWAYS_INLINE void setAlpha(double val) { alpha_ = val; } TRANSPORT_ALWAYS_INLINE void reset() { @@ -193,6 +235,8 @@ class TransportStatistics { avg_pending_pkt_ = 0; received_nacks_ = 0; received_fec_ = 0; + in_congestion_ = false; + quality_score_ = 5; } private: @@ -213,6 +257,14 @@ class TransportStatistics { double avg_pending_pkt_; uint32_t received_nacks_; uint32_t received_fec_; + bool in_congestion_; + double residual_loss_rate_; + uint8_t quality_score_; + + // alerts is a bit vector used to signal to the upper layer that + // something bad is appening in the network, the encode is done accoding to + // the enum alerts; + uint32_t alerts_; }; } // namespace interface diff --git a/libtransport/includes/hicn/transport/portability/CMakeLists.txt b/libtransport/includes/hicn/transport/portability/CMakeLists.txt index 8094c0661..7a688b1f1 100644 --- a/libtransport/includes/hicn/transport/portability/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/portability/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 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: @@ -14,6 +14,7 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/c_portability.h ${CMAKE_CURRENT_SOURCE_DIR}/portability.h + ${CMAKE_CURRENT_SOURCE_DIR}/cpu.h ) list(APPEND SOURCE_FILES diff --git a/libtransport/includes/hicn/transport/portability/c_portability.h b/libtransport/includes/hicn/transport/portability/c_portability.h index 2675de000..bc697d8b1 100644 --- a/libtransport/includes/hicn/transport/portability/c_portability.h +++ b/libtransport/includes/hicn/transport/portability/c_portability.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/libtransport/includes/hicn/transport/portability/cpu.h b/libtransport/includes/hicn/transport/portability/cpu.h new file mode 100644 index 000000000..036bf9cd9 --- /dev/null +++ b/libtransport/includes/hicn/transport/portability/cpu.h @@ -0,0 +1,40 @@ +/* + * 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. + */ + +#pragma once + +#if defined(__aarch64__) && defined(__ARM_NEON) || defined(__i686__) +#define TRANSPORT_HAVE_VEC128 +#endif + +#if defined(__SSE4_2__) && __GNUC__ >= 4 +#define TRANSPORT_HAVE_VEC128 +#endif + +#if defined(__ALTIVEC__) +#define TRANSPORT_HAVE_VEC128 +#endif + +#if defined(__AVX2__) +#define TRANSPORT_HAVE_VEC256 +#if defined(__clang__) && __clang_major__ < 4 +#undef TRANSPORT_HAVE_VEC256 +#endif +#endif + +#if defined(__AVX512BITALG__) +#define TRANSPORT_HAVE_VEC512 +#endif diff --git a/libtransport/includes/hicn/transport/portability/portability.h b/libtransport/includes/hicn/transport/portability/portability.h index 24ef012f7..b093f8892 100644 --- a/libtransport/includes/hicn/transport/portability/portability.h +++ b/libtransport/includes/hicn/transport/portability/portability.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,9 +28,6 @@ namespace portability { -constexpr bool little_endian_arch = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__; -constexpr bool big_endian_arch = !little_endian_arch; - // Generalize warning push/pop. #if defined(__GNUC__) || defined(__clang__) // Clang & GCC diff --git a/libtransport/includes/hicn/transport/portability/win_portability.h b/libtransport/includes/hicn/transport/portability/win_portability.h index 24c7a932a..81aa828cd 100644 --- a/libtransport/includes/hicn/transport/portability/win_portability.h +++ b/libtransport/includes/hicn/transport/portability/win_portability.h @@ -1,44 +1,44 @@ -/*
- * Copyright (c) 2017-2019 Cisco and/or its affiliates.
- * Copyright 2017 Facebook, Inc.
- *
- * 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.
- */
-
-#pragma once
-#define WIN32_LEAN_AND_MEAN
-#ifndef NOMINMAX
-#define NOMINMAX
-#endif
-#include <fcntl.h>
-#include <io.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <time.h>
-#include <windows.h>
-#include <winsock2.h>
-#include <ws2ipdef.h>
-#include <ws2tcpip.h>
-
-#include <algorithm>
-
-#define __ORDER_LITTLE_ENDIAN__ 0x41424344UL
-#define __ORDER_BIG_ENDIAN__ 0x44434241UL
-#define __BYTE_ORDER__ ('ABCD')
-#undef DELETE
-
-#define HAVE_STRUCT_TIMESPEC
+/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Copyright 2017 Facebook, Inc. + * + * 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. + */ + +#pragma once +#define WIN32_LEAN_AND_MEAN +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include <fcntl.h> +#include <io.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <time.h> +#include <windows.h> +#include <winsock2.h> +#include <ws2ipdef.h> +#include <ws2tcpip.h> + +#include <algorithm> + +#define __ORDER_LITTLE_ENDIAN__ 0x41424344UL +#define __ORDER_BIG_ENDIAN__ 0x44434241UL +#define __BYTE_ORDER__ ('ABCD') +#undef DELETE + +#define HAVE_STRUCT_TIMESPEC #include <pthread.h>
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/CMakeLists.txt b/libtransport/includes/hicn/transport/utils/CMakeLists.txt index 75f727f03..bbd149e09 100644 --- a/libtransport/includes/hicn/transport/utils/CMakeLists.txt +++ b/libtransport/includes/hicn/transport/utils/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2019 Cisco and/or its affiliates. +# Copyright (c) 2021 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: @@ -19,6 +19,7 @@ list(APPEND HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/chrono_typedefs.h ${CMAKE_CURRENT_SOURCE_DIR}/branch_prediction.h ${CMAKE_CURRENT_SOURCE_DIR}/ring_buffer.h + ${CMAKE_CURRENT_SOURCE_DIR}/rtc_quality_score.h ${CMAKE_CURRENT_SOURCE_DIR}/literals.h ${CMAKE_CURRENT_SOURCE_DIR}/conversions.h ${CMAKE_CURRENT_SOURCE_DIR}/linux.h diff --git a/libtransport/includes/hicn/transport/utils/array.h b/libtransport/includes/hicn/transport/utils/array.h index 7c0ed65d8..d57bfabaf 100644 --- a/libtransport/includes/hicn/transport/utils/array.h +++ b/libtransport/includes/hicn/transport/utils/array.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/branch_prediction.h b/libtransport/includes/hicn/transport/utils/branch_prediction.h index 8cbfaca76..626cb1573 100644 --- a/libtransport/includes/hicn/transport/utils/branch_prediction.h +++ b/libtransport/includes/hicn/transport/utils/branch_prediction.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/chrono_typedefs.h b/libtransport/includes/hicn/transport/utils/chrono_typedefs.h index 8f28e763c..ddfbd00cd 100644 --- a/libtransport/includes/hicn/transport/utils/chrono_typedefs.h +++ b/libtransport/includes/hicn/transport/utils/chrono_typedefs.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -17,11 +17,102 @@ #include <chrono> +namespace std { +namespace chrono { +namespace detail { + +template <typename From, typename To> +struct posix_duration_cast; + +// chrono -> timespec caster +template <typename Rep, typename Period> +struct posix_duration_cast<std::chrono::duration<Rep, Period>, + struct timespec> { + static struct timespec cast(std::chrono::duration<Rep, Period> const &d) { + struct timespec tv; + + std::chrono::seconds const sec = + std::chrono::duration_cast<std::chrono::seconds>(d); + + tv.tv_sec = sec.count(); + tv.tv_nsec = + std::chrono::duration_cast<std::chrono::nanoseconds>(d - sec).count(); + + return tv; + } +}; + +// timespec -> chrono caster +template <typename Rep, typename Period> +struct posix_duration_cast<struct timespec, + std::chrono::duration<Rep, Period>> { + static std::chrono::duration<Rep, Period> cast(struct timespec const &tv) { + return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>( + std::chrono::seconds(tv.tv_sec) + std::chrono::nanoseconds(tv.tv_nsec)); + } +}; + +} // namespace detail + +// chrono -> timespec +template <typename T, typename Rep, typename Period> +auto duration_cast(std::chrono::duration<Rep, Period> const &d) -> + typename std::enable_if<std::is_same<T, struct timespec>::value, + struct timespec>::type { + return detail::posix_duration_cast<std::chrono::duration<Rep, Period>, + timespec>::cast(d); +} + +// timespec -> chrono +template <typename Duration> +Duration duration_cast(struct timespec const &tv) { + return detail::posix_duration_cast<struct timespec, Duration>::cast(tv); +} + +} // namespace chrono +} // namespace std + namespace utils { -using SteadyClock = std::chrono::steady_clock; -using TimePoint = SteadyClock::time_point; -using Milliseconds = std::chrono::milliseconds; -using Microseconds = std::chrono::microseconds; +template <typename T> +class Time { + public: + using Clock = T; + using TimePoint = typename Clock::time_point; + using Rep = double; + using Seconds = std::chrono::duration<Rep>; + using Milliseconds = std::chrono::duration<Rep, std::milli>; + using Microseconds = std::chrono::duration<Rep, std::micro>; + + static auto now() { return Clock::now(); } + + // From epochs + static auto nowMs() { + return std::chrono::duration_cast<Milliseconds>(now().time_since_epoch()); + } + + // From epoch + static auto nowUs() { + return std::chrono::duration_cast<Microseconds>(now().time_since_epoch()); + } + + template <typename D> + static auto getDuration(const TimePoint &start, const TimePoint &end) { + return std::chrono::duration_cast<D>(end - start); + } + + static auto getDurationS(const TimePoint &start, const TimePoint &end) { + return getDuration<Seconds>(start, end); + } + static auto getDurationMs(const TimePoint &start, const TimePoint &end) { + return getDuration<Milliseconds>(start, end); + } + static auto getDurationUs(const TimePoint &start, const TimePoint &end) { + return getDuration<Microseconds>(start, end); + } +}; + +using SteadyTime = Time<std::chrono::steady_clock>; +using SystemTime = Time<std::chrono::system_clock>; } // namespace utils diff --git a/libtransport/includes/hicn/transport/utils/conversions.h b/libtransport/includes/hicn/transport/utils/conversions.h index 52d3e3168..812803175 100644 --- a/libtransport/includes/hicn/transport/utils/conversions.h +++ b/libtransport/includes/hicn/transport/utils/conversions.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/daemonizator.h b/libtransport/includes/hicn/transport/utils/daemonizator.h index 028d74865..1d0a0b309 100644 --- a/libtransport/includes/hicn/transport/utils/daemonizator.h +++ b/libtransport/includes/hicn/transport/utils/daemonizator.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/event_thread.h b/libtransport/includes/hicn/transport/utils/event_thread.h index 15ec1d62c..2cd2f3aca 100644 --- a/libtransport/includes/hicn/transport/utils/event_thread.h +++ b/libtransport/includes/hicn/transport/utils/event_thread.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -97,6 +97,19 @@ class EventThread { io_service_.get().dispatch(std::forward<Func&&>(f)); } + template <typename Func> + void addAndWaitForExecution(Func&& f) { + auto promise = std::promise<void>(); + auto future = promise.get_future(); + + asio::dispatch(io_service_.get(), [&promise, f = std::forward<Func>(f)]() { + f(); + promise.set_value(); + }); + + future.wait(); + } + void stop() { work_.reset(); diff --git a/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h b/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h index 298b6f9d1..19b52b37e 100644 --- a/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h +++ b/libtransport/includes/hicn/transport/utils/fixed_block_allocator.h @@ -1,23 +1,28 @@ /* - * Copyright (c) 2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. */ #pragma once #include <hicn/transport/portability/c_portability.h> +#include <hicn/transport/utils/branch_prediction.h> #include <hicn/transport/utils/singleton.h> #include <hicn/transport/utils/spinlock.h> #include <stdint.h> #include <cassert> #include <cstdlib> +#include <list> #include <memory> namespace utils { template <std::size_t SIZE = 512, std::size_t OBJECTS = 4096> class FixedBlockAllocator - : public utils::Singleton<FixedBlockAllocator<SIZE, OBJECTS>> { - friend class utils::Singleton<FixedBlockAllocator<SIZE, OBJECTS>>; + : public utils::ThreadLocalSingleton<FixedBlockAllocator<SIZE, OBJECTS>> { + friend class utils::ThreadLocalSingleton<FixedBlockAllocator<SIZE, OBJECTS>>; + + static inline const std::size_t BLOCK_SIZE = SIZE; + static inline const std::size_t BLOCKS_PER_POOL = OBJECTS; public: ~FixedBlockAllocator() { @@ -26,19 +31,19 @@ class FixedBlockAllocator } } - void* allocateBlock(size_t size = SIZE) { - assert(size <= SIZE); + void* allocateBlock() { uint32_t index; - SpinLock::Acquire locked(lock_); void* p_block = pop(); if (!p_block) { - if (TRANSPORT_EXPECT_FALSE(current_pool_index_ >= max_objects_)) { + if (TRANSPORT_EXPECT_FALSE(current_pool_index_ >= BLOCKS_PER_POOL)) { // Allocate new memory block p_pools_.emplace_front( - new typename std::aligned_storage<SIZE>::type[max_objects_]); + new typename std::aligned_storage<SIZE>::type[BLOCKS_PER_POOL]); // reset current_pool_index_ current_pool_index_ = 0; + // Increase total block count + block_count_ += BLOCKS_PER_POOL; } auto& latest = p_pools_.front(); @@ -59,7 +64,7 @@ class FixedBlockAllocator } public: - std::size_t blockSize() { return block_size_; } + std::size_t blockSize() { return BLOCK_SIZE; } uint32_t blockCount() { return block_count_; } @@ -69,20 +74,32 @@ class FixedBlockAllocator uint32_t deallocations() { return deallocations_; } + void reset() { + p_head_ = nullptr; + blocks_in_use_ = 0; + allocations_ = 0; + deallocations_ = 0; + current_pool_index_ = 0; + block_count_ = BLOCKS_PER_POOL; + + // Delete all memory pools but the first one + for (auto it = std::next(p_pools_.begin()); it != p_pools_.end();) { + delete[] * it; + it = p_pools_.erase(it); + } + } + private: FixedBlockAllocator() - : block_size_(SIZE), - object_size_(SIZE), - max_objects_(OBJECTS), - p_head_(NULL), + : p_head_(NULL), current_pool_index_(0), - block_count_(0), + block_count_(BLOCKS_PER_POOL), blocks_in_use_(0), allocations_(0), deallocations_(0) { static_assert(SIZE >= sizeof(long*), "SIZE must be at least 8 bytes"); p_pools_.emplace_front( - new typename std::aligned_storage<SIZE>::type[max_objects_]); + new typename std::aligned_storage<SIZE>::type[BLOCKS_PER_POOL]); } void push(void* p_memory) { @@ -106,12 +123,6 @@ class FixedBlockAllocator Block* p_next; }; - static std::unique_ptr<FixedBlockAllocator> instance_; - - const std::size_t block_size_; - const std::size_t object_size_; - const std::size_t max_objects_; - Block* p_head_; uint32_t current_pool_index_; std::list<typename std::aligned_storage<SIZE>::type*> p_pools_; @@ -123,10 +134,6 @@ class FixedBlockAllocator SpinLock lock_; }; -template <std::size_t A, std::size_t B> -std::unique_ptr<FixedBlockAllocator<A, B>> - FixedBlockAllocator<A, B>::instance_ = nullptr; - /** * STL Allocator trait to be used with allocate_shared. */ diff --git a/libtransport/includes/hicn/transport/utils/hash.h b/libtransport/includes/hicn/transport/utils/hash.h index 6815ca4bf..7943596e6 100644 --- a/libtransport/includes/hicn/transport/utils/hash.h +++ b/libtransport/includes/hicn/transport/utils/hash.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/libtransport/includes/hicn/transport/utils/linux.h b/libtransport/includes/hicn/transport/utils/linux.h index 4fbf5f01e..03d29c1db 100644 --- a/libtransport/includes/hicn/transport/utils/linux.h +++ b/libtransport/includes/hicn/transport/utils/linux.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/literals.h b/libtransport/includes/hicn/transport/utils/literals.h index bd00e0a58..531f67362 100644 --- a/libtransport/includes/hicn/transport/utils/literals.h +++ b/libtransport/includes/hicn/transport/utils/literals.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/log.h b/libtransport/includes/hicn/transport/utils/log.h index 0947b755e..f4d39b6b1 100644 --- a/libtransport/includes/hicn/transport/utils/log.h +++ b/libtransport/includes/hicn/transport/utils/log.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/move_wrapper.h b/libtransport/includes/hicn/transport/utils/move_wrapper.h index 5dc3b461d..73389948d 100644 --- a/libtransport/includes/hicn/transport/utils/move_wrapper.h +++ b/libtransport/includes/hicn/transport/utils/move_wrapper.h @@ -23,6 +23,7 @@ namespace utils { template <typename F> struct MoveWrapper : F { MoveWrapper(F&& f) : F(std::move(f)) {} + ~MoveWrapper() = default; MoveWrapper(MoveWrapper&&) = default; MoveWrapper& operator=(MoveWrapper&&) = default; diff --git a/libtransport/includes/hicn/transport/utils/noncopyable.h b/libtransport/includes/hicn/transport/utils/noncopyable.h index 83923e647..0c54d24a3 100644 --- a/libtransport/includes/hicn/transport/utils/noncopyable.h +++ b/libtransport/includes/hicn/transport/utils/noncopyable.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/object_pool.h b/libtransport/includes/hicn/transport/utils/object_pool.h index d9b28e663..63288c655 100644 --- a/libtransport/includes/hicn/transport/utils/object_pool.h +++ b/libtransport/includes/hicn/transport/utils/object_pool.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -16,6 +16,7 @@ #pragma once #include <hicn/transport/utils/branch_prediction.h> +#include <hicn/transport/utils/noncopyable.h> #include <hicn/transport/utils/spinlock.h> #include <deque> @@ -25,7 +26,7 @@ namespace utils { template <typename T> -class ObjectPool { +class ObjectPool : private utils::NonCopyable { class ObjectDeleter { public: ObjectDeleter(ObjectPool<T> *pool = nullptr) : pool_(pool) {} @@ -47,8 +48,20 @@ class ObjectPool { ObjectPool() : destructor_(false) {} - // No copies - ObjectPool(const ObjectPool &other) = delete; + ObjectPool(ObjectPool &&other) + : object_pool_lock_(std::move(other.object_pool_lock_)), + object_pool_(std::move(other.object_pool_)), + destructor_(other.destructor_) {} + + ObjectPool &operator=(ObjectPool &&other) { + if (this != &other) { + object_pool_lock_ = std::move(other.object_pool_lock_); + object_pool_ = std::move(other.object_pool_); + destructor_ = other.destructor_; + } + + return *this; + } ~ObjectPool() { destructor_ = true; diff --git a/libtransport/includes/hicn/transport/utils/ring_buffer.h b/libtransport/includes/hicn/transport/utils/ring_buffer.h index 52629b82b..3032032c9 100644 --- a/libtransport/includes/hicn/transport/utils/ring_buffer.h +++ b/libtransport/includes/hicn/transport/utils/ring_buffer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/rtc_quality_score.h b/libtransport/includes/hicn/transport/utils/rtc_quality_score.h new file mode 100644 index 000000000..2e8ca97d3 --- /dev/null +++ b/libtransport/includes/hicn/transport/utils/rtc_quality_score.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021 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. + */ + +#pragma once + +namespace transport { + +namespace protocol { + +namespace rtc { + +class RTCQualityScore { + public: + RTCQualityScore(){}; + ~RTCQualityScore(){}; + + uint8_t getQualityScore(uint64_t RTT, uint32_t loss_rate) { + uint32_t delay_index = getDelay(RTT); + uint32_t loss_index = getLossRate(loss_rate); + return quality_score_[loss_index][delay_index]; + } + + private: + // see quality score map + uint8_t quality_score_[7][6]{{5, 5, 5, 4, 3, 1}, {5, 4, 3, 2, 1, 1}, + {5, 3, 2, 1, 1, 1}, {5, 2, 1, 1, 1, 1}, + {4, 1, 1, 1, 1, 1}, {3, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1}}; + + uint8_t getDelay(uint64_t RTT) { + uint64_t OWD = RTT / 2; + // we should never get a OWD of 0. so we take the first col if OWD is < 5ms + if (OWD < 5) return 0; + if (OWD < 50) return 1; + if (OWD < 100) return 2; + if (OWD < 200) return 3; + if (OWD < 300) return 4; + return 5; + } + + uint8_t getLossRate(uint32_t loss_rate) { + // we use 3% as mean loss rate + if (loss_rate < 3) return 0; + if (loss_rate < 10) return 1; + if (loss_rate < 20) return 2; + if (loss_rate < 30) return 3; + if (loss_rate < 40) return 4; + if (loss_rate < 50) return 5; + return 6; + } +}; + +} // namespace rtc + +} // namespace protocol + +} // namespace transport diff --git a/libtransport/includes/hicn/transport/utils/singleton.h b/libtransport/includes/hicn/transport/utils/singleton.h index 4b7b19c0a..cdd8b03bf 100644 --- a/libtransport/includes/hicn/transport/utils/singleton.h +++ b/libtransport/includes/hicn/transport/utils/singleton.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: @@ -32,4 +32,17 @@ class Singleton : NonCopyable { ~Singleton() {} }; +template <typename T> +class ThreadLocalSingleton : NonCopyable { + public: + static T& getInstance() { + static thread_local T instance; + return instance; + } + + protected: + ThreadLocalSingleton() {} + ~ThreadLocalSingleton() {} +}; + } // namespace utils
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/spinlock.h b/libtransport/includes/hicn/transport/utils/spinlock.h index 009a94454..40fc48de3 100644 --- a/libtransport/includes/hicn/transport/utils/spinlock.h +++ b/libtransport/includes/hicn/transport/utils/spinlock.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/string_tokenizer.h b/libtransport/includes/hicn/transport/utils/string_tokenizer.h index 36630eb58..2b2b893a0 100644 --- a/libtransport/includes/hicn/transport/utils/string_tokenizer.h +++ b/libtransport/includes/hicn/transport/utils/string_tokenizer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/string_utils.h b/libtransport/includes/hicn/transport/utils/string_utils.h index 313c28cc6..5f9cca13b 100644 --- a/libtransport/includes/hicn/transport/utils/string_utils.h +++ b/libtransport/includes/hicn/transport/utils/string_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: diff --git a/libtransport/includes/hicn/transport/utils/thread_pool.h b/libtransport/includes/hicn/transport/utils/thread_pool.h new file mode 100644 index 000000000..e4e47209c --- /dev/null +++ b/libtransport/includes/hicn/transport/utils/thread_pool.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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. + */ + +#pragma once + +#include <hicn/transport/utils/event_thread.h> +#include <hicn/transport/utils/noncopyable.h> + +#include <thread> +#include <vector> + +namespace utils { + +class ThreadPool : public NonCopyable { + public: + explicit ThreadPool( + std::size_t n_threads = std::thread::hardware_concurrency()) + : workers_(n_threads > 0 ? n_threads : 1) {} + + std::size_t getNThreads() const { return workers_.size(); } + EventThread &getWorker(std::size_t i) { return workers_.at(i); } + + private: + std::vector<EventThread> workers_; +}; + +} // namespace utils
\ No newline at end of file diff --git a/libtransport/includes/hicn/transport/utils/uri.h b/libtransport/includes/hicn/transport/utils/uri.h index 7c28e8552..30675e880 100644 --- a/libtransport/includes/hicn/transport/utils/uri.h +++ b/libtransport/includes/hicn/transport/utils/uri.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2019 Cisco and/or its affiliates. + * Copyright (c) 2021 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: |