diff --git a/src/services/pcn-k8sdispatcher/cmake/LoadFileAsVariable.cmake b/src/services/pcn-k8sdispatcher/cmake/LoadFileAsVariable.cmake deleted file mode 100644 index 2b58da763..000000000 --- a/src/services/pcn-k8sdispatcher/cmake/LoadFileAsVariable.cmake +++ /dev/null @@ -1,52 +0,0 @@ -# Loads the contents of a file into a std::string variable -# -# It creates a header file in ${CMAKE_CURRENT_BINARY_DIR}/${file}.h -# that wrapps the contents of the file in a std::string using the raw -# string literal feature of C++11. The user needs to include that file -# into the source code in order to see the variable. -# -# parameters: -# target: target to add a dependency on file -# file: file to be loaded -# variable_name: name variable where the file is loaded -# -# example: -# load_file_as_variable(my-lib resource.c my_resource) -# Creates a resource.h in CMAKE_CURRENT_BINARY_DIR with a string variable -# my_resource with the contents of resource.c -# A dependency in resource.c is added to my-lib - -function(load_file_as_variable target file variable_name) - get_filename_component(file_name ${file} NAME_WE) - get_filename_component(file_dir ${file} DIRECTORY) - - set(new_path ${file_dir}/${file_name}.h) - - add_custom_command( - OUTPUT - ${CMAKE_CURRENT_BINARY_DIR}/${new_path} - COMMAND - mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${file_dir} - COMMAND - echo "#pragma once" > ${CMAKE_CURRENT_BINARY_DIR}/${new_path} - COMMAND - echo "#include " >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path} - COMMAND - echo "const std::string ${variable_name} = R\"POLYCUBE_DP(" >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path} - COMMAND - cat ${CMAKE_CURRENT_SOURCE_DIR}/${file} >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path} - COMMAND - cmake -E echo ")POLYCUBE_DP\";" >> ${CMAKE_CURRENT_BINARY_DIR}/${new_path} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${file} - VERBATIM - ) - - string(REPLACE "/" "-" path_replaced ${new_path}) - - add_custom_target( - generate_${path_replaced} - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${new_path} - ) - - add_dependencies(${target} generate_${path_replaced}) -endfunction() diff --git a/src/services/pcn-k8sdispatcher/datamodel/k8sdispatcher.yang b/src/services/pcn-k8sdispatcher/datamodel/k8sdispatcher.yang index 94d5a022a..e0ad649c5 100644 --- a/src/services/pcn-k8sdispatcher/datamodel/k8sdispatcher.yang +++ b/src/services/pcn-k8sdispatcher/datamodel/k8sdispatcher.yang @@ -1,47 +1,47 @@ module k8sdispatcher { - yang-version 1.1; - namespace "http://polycube.network/k8sdispatcher"; - prefix "k8sdispatcher"; + yang-version 1.1; + namespace "http://polycube.network/k8sdispatcher"; + prefix "k8sdispatcher"; - import polycube-base { prefix "polycube-base"; } - import polycube-standard-base { prefix "polycube-standard-base"; } + import polycube-base { prefix "polycube-base"; } + import polycube-standard-base { prefix "polycube-standard-base"; } - import ietf-inet-types { prefix "inet"; } + import ietf-inet-types { prefix "inet"; } - organization "Polycube open source project"; - description "YANG data model for the Polycube dispatcher for Kubernetes"; + organization "Polycube open source project"; + description "YANG data model for the Polycube K8s Dispatcher"; - polycube-base:service-description "Kubernetes k8sdispatcher Service"; - polycube-base:service-version "2.0.0"; - polycube-base:service-name "k8sdispatcher"; - polycube-base:service-min-kernel-version "4.14.0"; + polycube-base:service-description "K8s Dispatcher Service"; + polycube-base:service-version "2.0.0"; + polycube-base:service-name "k8sdispatcher"; + polycube-base:service-min-kernel-version "4.14.0"; - uses "polycube-standard-base:standard-base-yang-module" { - augment ports { - leaf type { - type enumeration { - enum BACKEND { description "Port connected to cni topology"; } - enum FRONTEND { description "Port connected to NIC"; } + uses "polycube-standard-base:standard-base-yang-module" { + augment ports { + leaf type { + type enumeration { + enum BACKEND { description "Port connected to cni topology"; } + enum FRONTEND { description "Port connected to NIC"; } + } + mandatory true; + description "Type of the k8sdispatcher port (e.g. BACKEND or FRONTEND)"; + } + leaf ip { + type inet:ipv4-address; + description "IP address of the node interface (only for FRONTEND port)"; + polycube-base:cli-example "10.10.1.1"; + } } - mandatory true; - description "Type of the k8sdispatcher port (e.g. BACKEND or FRONTEND)"; - } - leaf ip { + } + + leaf internal-src-ip { type inet:ipv4-address; - description "IP address of the node interface (only for FRONTEND port)"; + description "Internal src ip used for services with externalTrafficPolicy=CLUSTER"; polycube-base:cli-example "10.10.1.1"; - } + mandatory true; } - } - leaf internal-src-ip { - type inet:ipv4-address; - description "Internal src ip used for services with externaltrafficpolicy=cluster"; - polycube-base:cli-example "10.10.1.1"; - mandatory true; - } - - list natting-rule { + list natting-rule { key "internal-src internal-dst internal-sport internal-dport proto"; leaf internal-src { @@ -74,14 +74,14 @@ module k8sdispatcher { } } - list nodeport-rule { - key "nodeport-port proto"; - - leaf internal-src { - type inet:ipv4-address; - description "Source IP address"; - } - leaf nodeport-port { + list nodeport-rule { + key "port proto"; + + leaf name { + type string; + description "An optional name for the NodePort rule"; + } + leaf port { type inet:port-number; description "Destination L4 port number"; } @@ -90,22 +90,20 @@ module k8sdispatcher { description "L4 protocol"; } leaf service-type { - type enumeration { - enum CLUSTER { description "Cluster wide service"; } - enum LOCAL { description "Local service"; } + type enumeration { + enum CLUSTER { description "Cluster wide service"; } + enum LOCAL { description "Local service"; } + } + mandatory true; + description "Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoint"; } - mandatory true; - description "Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoint"; - } } - leaf nodeport-range { - type string; - description "Port range used for NodePort services"; - default "30000-32767"; - polycube-base:cli-example "30000-32767"; - } - - + leaf nodeport-range { + type string; + description "Port range used for NodePort services"; + default "30000-32767"; + polycube-base:cli-example "30000-32767"; + } } diff --git a/src/services/pcn-k8sdispatcher/src/CMakeLists.txt b/src/services/pcn-k8sdispatcher/src/CMakeLists.txt index d25452a66..22c6d621e 100644 --- a/src/services/pcn-k8sdispatcher/src/CMakeLists.txt +++ b/src/services/pcn-k8sdispatcher/src/CMakeLists.txt @@ -7,9 +7,9 @@ aux_source_directory(base BASE_SOURCES) include_directories(serializer) if (NOT DEFINED POLYCUBE_STANDALONE_SERVICE OR POLYCUBE_STANDALONE_SERVICE) - find_package(PkgConfig REQUIRED) - pkg_check_modules(POLYCUBE libpolycube) - include_directories(${POLYCUBE_INCLUDE_DIRS}) + find_package(PkgConfig REQUIRED) + pkg_check_modules(POLYCUBE libpolycube) + include_directories(${POLYCUBE_INCLUDE_DIRS}) endif (NOT DEFINED POLYCUBE_STANDALONE_SERVICE OR POLYCUBE_STANDALONE_SERVICE) # Needed to load files as variables diff --git a/src/services/pcn-k8sdispatcher/src/K8sdispatcher.cpp b/src/services/pcn-k8sdispatcher/src/K8sdispatcher.cpp index 6a9b6cf46..5ec989a05 100644 --- a/src/services/pcn-k8sdispatcher/src/K8sdispatcher.cpp +++ b/src/services/pcn-k8sdispatcher/src/K8sdispatcher.cpp @@ -9,34 +9,32 @@ #include "K8sdispatcher.h" #include "K8sdispatcher_dp.h" +const std::string K8sdispatcher::EBPF_DP_RULES_MAP = "dp_rules" + K8sdispatcher::K8sdispatcher(const std::string name, const K8sdispatcherJsonObject &conf) : Cube(conf.getBase(), {k8sdispatcher_code}, {}), K8sdispatcherBase(name), - nodeport_range_low_{30000}, - nodeport_range_high_{32767}, - natted_ip_{0}, - external_ip_{0} { + nodePortRangeLow_{30000}, + nodePortRangeHigh_{32767}, + nattedIp_{0}, + externalIp_{0} { logger()->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [K8sDispatcher] [%n] [%l] %v"); logger()->info("creating K8sDispatcher instance"); - this->in_building_ = true; - this->need_reload_ = false; + this->inBuilding_ = true; + this->needReload_ = false; this->setNodeportRange(conf.getNodeportRange()); this->setInternalSrcIp(conf.getInternalSrcIp()); - -// this->addNattingRuleList(conf.getNattingRule()); -// this->addNodeportRuleList(conf.getNodeportRule()); this->addPortsList(conf.getPorts()); - if (this->need_reload_) { + if (this->needReload_) { logger()->trace("performing K8sDispatcher code reloading during instance creation"); this->reloadConfig(); logger()->trace("performed K8sDispatcher code reloading during instance creation"); } - - this->in_building_ = false; + this->inBuilding_ = false; logger()->info("created K8sDispatcher instance"); } @@ -81,14 +79,14 @@ void K8sdispatcher::reloadConfig() { } flags += "#define FRONTEND_PORT " + std::to_string(frontend) + "\n"; flags += "#define BACKEND_PORT " + std::to_string(backend) + "\n"; - flags += "#define NODEPORT_RANGE_LOW " + std::to_string(nodeport_range_low_) + "\n"; - flags += "#define NODEPORT_RANGE_HIGH " + std::to_string(nodeport_range_high_) + "\n"; - flags += "#define EXTERNAL_IP " + std::to_string(htonl(external_ip_)) + "\n"; - flags += "#define NATTED_IP " + std::to_string(htonl(natted_ip_)) + "\n"; + flags += "#define NODEPORT_RANGE_LOW " + std::to_string(nodePortRangeLow_) + "\n"; + flags += "#define NODEPORT_RANGE_HIGH " + std::to_string(nodePortRangeHigh_) + "\n"; + flags += "#define EXTERNAL_IP " + std::to_string(htonl(externalIp_)) + "\n"; + flags += "#define NATTED_IP " + std::to_string(htonl(nattedIp_)); logger()->debug("reloading code with the following flags:\n{}", flags); - reload(flags + k8sdispatcher_code); + reload(flags + '\n' + k8sdispatcher_code); logger()->trace("reloaded K8sDispatcher code"); } @@ -116,13 +114,13 @@ void K8sdispatcher::update(const K8sdispatcherJsonObject &conf) { } K8sdispatcher::~K8sdispatcher() { - logger()->info("Destroying K8sdispatcher instance"); + logger()->info("Destroyed K8sdispatcher instance"); } void K8sdispatcher::setExternalIp(const std::string &value) { uint32_t external_ip_subnet; - external_ip_string_ = value; - parse_cidr(value, &external_ip_, &external_ip_subnet); + externalIpString_ = value; + parse_cidr(value, &externalIp_, &external_ip_subnet); } void K8sdispatcher::parse_cidr(const std::string &cidr, uint32_t *subnet, @@ -177,7 +175,7 @@ void K8sdispatcher::addPorts(const std::string &name, const PortsJsonObject &con // setting external IP if port_type == FRONTEND and reload code if needed if (type == PortsTypeEnum::FRONTEND) this->setExternalIp(conf.getIp()); if (get_ports().size() == 2) { - if (this->in_building_) this->need_reload_ = true; + if (this->inBuilding_) this->needReload_ = true; else this->reloadConfig(); }; } catch (std::runtime_error &ex) { @@ -225,13 +223,13 @@ uint8_t K8sdispatcher::proto_from_string_to_int(const std::string &proto) { } std::string K8sdispatcher::getInternalSrcIp() { - return natted_ip_string_; + return nattedIpString_; } void K8sdispatcher::setInternalSrcIp(const std::string &value) { - this->natted_ip_string_ = value; - this->natted_ip_ = ip_to_dec(value); - if (this->in_building_) this->need_reload_ = true; + this->nattedIpString_ = value; + this->nattedIp_ = ip_to_dec(value); + if (this->inBuilding_) this->needReload_ = true; else this->reloadConfig(); } @@ -323,19 +321,19 @@ void K8sdispatcher::delNattingRuleList() { logger()->info("Flushed natting tables"); } -std::shared_ptr K8sdispatcher::getNodeportRule(const uint16_t &nodeportPort, const std::string &proto) { - logger()->debug("Received a request to read a nodeport rule"); - NodeportKey key = NodeportKey(proto, nodeportPort); - if (nodeport_map_.count(key) == 0) { - logger()->error("There are no entries associated with that key"); +std::shared_ptr K8sdispatcher::getNodeportRule(const uint16_t &port, const std::string &proto) { + logger()->debug("received a request to read a NodePort rule"); + NodeportKey key = NodeportKey(proto, port); + if (nodePortMap_.count(key) == 0) { + logger()->error("there are no entries associated with that key"); } - return std::shared_ptr(&nodeport_map_.at(key), [](NodeportRule *) {}); + return std::shared_ptr(&nodePortMap_.at(key), [](NodeportRule *) {}); } std::vector> K8sdispatcher::getNodeportRuleList() { std::vector> services_vect; - for (auto &it: nodeport_map_) { + for (auto &it: nodePortMap_) { NodeportKey key = it.first; services_vect.push_back(getNodeportRule(std::get<1>(key), std::get<0>(key))); } @@ -343,29 +341,27 @@ std::vector> K8sdispatcher::getNodeportRuleList() return services_vect; } -void K8sdispatcher::addNodeportRule(const uint16_t &nodeportPort, const std::string &proto, +void K8sdispatcher::addNodeportRule(const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &conf) { - logger()->info("add nodeportRule"); - NodeportKey key = NodeportKey(proto, nodeportPort); + logger()->info("received a request to add a NodePort rule"); + NodeportKey key = NodeportKey(proto, port); - if (nodeport_map_.count(key) == 1) { - logger()->error("This nodeport rule already exists"); - throw std::runtime_error("This nodeport rule already exists"); + if (nodePortMap_.count(key) == 1) { + logger()->error("the NodePort rule already exists"); + throw std::runtime_error("the NodePort rule already exists"); } NodeportRule rule = NodeportRule(*this, conf); - nodeport_map_.insert(std::make_pair(key, rule)); + nodePortMap_.insert(std::make_pair(key, rule)); - auto dp_rules = get_hash_table( - "dp_rules", 0, ProgramType::INGRESS); + auto dp_rules = get_hash_table(K8sdispatcher::EBPF_DP_RULES_MAP); dp_k key_rule{ - .mask = 56, .external_ip = htonl(external_ip_), .external_port = htons( - nodeportPort), .proto = proto_from_string_to_int(proto), + .external_port = htons(port), + .proto = proto_from_string_to_int(proto), }; dp_v value{ - .internal_ip = htonl(ip_to_dec(rule.getInternalSrc())), - .internal_port = htons(nodeportPort), + .internal_port = htons(port), .entry_type = 4, }; if (rule.getServiceType() == polycube::service::model::NodeportRuleServiceTypeEnum::LOCAL) { @@ -377,43 +373,41 @@ void K8sdispatcher::addNodeportRule(const uint16_t &nodeportPort, const std::str // Basic default implementation, place your extension here (if needed) void K8sdispatcher::addNodeportRuleList(const std::vector &conf) { for (auto &i: conf) { - addNodeportRule(i.getNodeportPort(), i.getProto(), i); + addNodeportRule(i.getPort(), i.getProto(), i); } } // Basic default implementation, place your extension here (if needed) -void K8sdispatcher::replaceNodeportRule(const uint16_t &nodeportPort, const std::string &proto, +void K8sdispatcher::replaceNodeportRule(const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &conf) { - delNodeportRule(nodeportPort, proto); - addNodeportRule(nodeportPort, proto, conf); + delNodeportRule(port, proto); + addNodeportRule(port, proto, conf); } -void K8sdispatcher::delNodeportRule(const uint16_t &nodeportPort, const std::string &proto) { - NodeportKey key = NodeportKey(proto, nodeportPort); - if (nodeport_map_.count(key) == 0) { +void K8sdispatcher::delNodeportRule(const uint16_t &port, const std::string &proto) { + NodeportKey key = NodeportKey(proto, port); + if (nodePortMap_.count(key) == 0) { logger()->error("There are no entries associated with that key"); throw std::runtime_error("There are no entries associated with that key"); } - auto dp_rules = get_hash_table( - "dp_rules", 0, ProgramType::INGRESS); + auto dp_rules = get_hash_table(K8sdispatcher::EBPF_DP_RULES_MAP); dp_k key_rule{ - .mask = 56, .external_ip = htonl(external_ip_), .external_port = htons( - nodeportPort), .proto = proto_from_string_to_int(proto), + .external_port = htons(port), + .proto = proto_from_string_to_int(proto), }; dp_rules.remove(key_rule); - nodeport_map_.erase(key); + nodePortMap_.erase(key); } -// Basic default implementation, place your extension here (if needed) void K8sdispatcher::delNodeportRuleList() { - for (auto &it: nodeport_map_) { + for (auto &it: nodePortMap_) { NodeportKey key = it.first; delNodeportRule(std::get<1>(key), std::get<0>(key)); } } std::string K8sdispatcher::getNodeportRange() { - return this->nodeport_range_; + return this->nodePortRange_; } void K8sdispatcher::setNodeportRange(const std::string &value) { @@ -432,11 +426,11 @@ void K8sdispatcher::setNodeportRange(const std::string &value) { throw std::runtime_error("invalid node port range"); } - this->nodeport_range_low_ = low; - this->nodeport_range_high_ = high; + this->nodePortRangeLow_ = low; + this->nodePortRangeHigh_ = high; - this->nodeport_range_ = value; - if (this->in_building_) this->need_reload_ = true; + this->nodePortRange_ = value; + if (this->inBuilding_) this->needReload_ = true; else this->reloadConfig(); } diff --git a/src/services/pcn-k8sdispatcher/src/K8sdispatcher.h b/src/services/pcn-k8sdispatcher/src/K8sdispatcher.h index 1e4f93db0..9a6d134b5 100644 --- a/src/services/pcn-k8sdispatcher/src/K8sdispatcher.h +++ b/src/services/pcn-k8sdispatcher/src/K8sdispatcher.h @@ -22,21 +22,16 @@ using namespace polycube::service; /* definitions copied from datapath */ struct dp_k { - uint32_t mask; - uint32_t external_ip; uint16_t external_port; uint8_t proto; } __attribute__((packed)); struct dp_v { - uint32_t internal_ip; uint16_t internal_port; uint8_t entry_type; } __attribute__((packed)); class K8sdispatcher : public K8sdispatcherBase { - friend class NodeportRule; - public: K8sdispatcher(const std::string name, const K8sdispatcherJsonObject &conf); @@ -44,18 +39,18 @@ class K8sdispatcher : public K8sdispatcherBase { void packet_in(Ports &port, polycube::service::PacketInMetadata &md, - const std::vector &packet) override; + const std::vector &packet) override; /// /// Entry of the ports table /// - std::shared_ptr getPorts(const std::string &name) override; + std::shared_ptr getPorts(const std::string &name) override; - std::vector> getPortsList() override; + std::vector > getPortsList() override; void addPorts(const std::string &name, const PortsJsonObject &conf) override; - void addPortsList(const std::vector &conf) override; + void addPortsList(const std::vector &conf) override; void replacePorts(const std::string &name, const PortsJsonObject &conf) override; @@ -64,25 +59,26 @@ class K8sdispatcher : public K8sdispatcherBase { void delPortsList() override; /// - /// Internal src ip used for services with externaltrafficpolicy=cluster + /// Internal src ip used for services with externalTrafficPolicy=CLUSTER /// std::string getInternalSrcIp() override; + void setInternalSrcIp(const std::string &value) override; /// /// /// - std::shared_ptr + std::shared_ptr getNattingRule(const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, const uint16_t &internalDport, const std::string &proto) override; - std::vector> getNattingRuleList() override; + std::vector > getNattingRuleList() override; void addNattingRule(const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, const uint16_t &internalDport, const std::string &proto, const NattingRuleJsonObject &conf) override; - void addNattingRuleList(const std::vector &conf) override; + void addNattingRuleList(const std::vector &conf) override; void replaceNattingRule(const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, @@ -97,19 +93,18 @@ class K8sdispatcher : public K8sdispatcherBase { /// /// /// - std::shared_ptr getNodeportRule(const uint16_t &nodeportPort, const std::string &proto) override; + std::shared_ptr getNodeportRule(const uint16_t &port, const std::string &proto) override; - std::vector> getNodeportRuleList() override; + std::vector > getNodeportRuleList() override; - void addNodeportRule(const uint16_t &nodeportPort, const std::string &proto, - const NodeportRuleJsonObject &conf) override; + void addNodeportRule(const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &conf) override; - void addNodeportRuleList(const std::vector &conf) override; + void addNodeportRuleList(const std::vector &conf) override; - void replaceNodeportRule(const uint16_t &nodeportPort, const std::string &proto, - const NodeportRuleJsonObject &conf) override; + void + replaceNodeportRule(const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &conf) override; - void delNodeportRule(const uint16_t &nodeportPort, const std::string &proto) override; + void delNodeportRule(const uint16_t &port, const std::string &proto) override; void delNodeportRuleList() override; @@ -117,13 +112,14 @@ class K8sdispatcher : public K8sdispatcherBase { /// Port range used for NodePort services /// std::string getNodeportRange() override; + void setNodeportRange(const std::string &value) override; void update(const K8sdispatcherJsonObject &conf) override; K8sdispatcherJsonObject toJsonObject() override; - typedef std::tuple NodeportKey; + typedef std::tuple NodeportKey; static uint8_t proto_from_string_to_int(const std::string &proto); @@ -132,20 +128,21 @@ class K8sdispatcher : public K8sdispatcherBase { private: - bool in_building_; - bool need_reload_; + bool inBuilding_; + bool needReload_; - std::string nodeport_range_; - uint16_t nodeport_range_low_; - uint16_t nodeport_range_high_; + std::string nodePortRange_; + uint16_t nodePortRangeLow_; + uint16_t nodePortRangeHigh_; - std::string natted_ip_string_; - uint32_t natted_ip_; + std::string nattedIpString_; + uint32_t nattedIp_; - std::string external_ip_string_; - uint32_t external_ip_; + std::string externalIpString_; + uint32_t externalIp_; - std::unordered_map nodeport_map_; + std::unordered_map nodePortMap_; + static const std::string EBPF_DP_RULES_MAP; void setExternalIp(const std::string &value); @@ -155,7 +152,7 @@ class K8sdispatcher : public K8sdispatcherBase { void reloadConfig(); - std::shared_ptr getFrontendPort(); + std::shared_ptr getFrontendPort(); - std::shared_ptr getBackendPort(); + std::shared_ptr getBackendPort(); }; diff --git a/src/services/pcn-k8sdispatcher/src/K8sdispatcher_dp.c b/src/services/pcn-k8sdispatcher/src/K8sdispatcher_dp.c index 08f136b23..20d4f749a 100644 --- a/src/services/pcn-k8sdispatcher/src/K8sdispatcher_dp.c +++ b/src/services/pcn-k8sdispatcher/src/K8sdispatcher_dp.c @@ -81,8 +81,8 @@ #define IS_PSEUDO 0x10 struct eth_hdr { - __be64 dst : 48; - __be64 src : 48; + __be64 dst: 48; + __be64 src: 48; __be16 proto; } __attribute__((packed)); @@ -107,15 +107,13 @@ NAT_MAP_DIM); // DNAT + PORTFORWARDING rules struct dp_k { u32 mask; - __be32 external_ip; __be16 external_port; uint8_t proto; }; struct dp_v { - __be32 internal_ip; __be16 internal_port; uint8_t entry_type; -}; +} attribute; BPF_F_TABLE("lpm_trie", struct dp_k, struct dp_v, dp_rules, 1024, BPF_F_NO_PREALLOC); @@ -123,7 +121,9 @@ BPF_F_NO_PREALLOC); // Port numbers BPF_TABLE("array", u32, u16, first_free_port, 1); -static inline __be16 get_free_port() { +static inline __be16 + +get_free_port() { u32 i = 0; u16 *new_port_p = first_free_port.lookup(&i); if (!new_port_p) @@ -144,31 +144,23 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { // 4) packet modification // Parse packet - void *data = (void *)(long)ctx->data; - void *data_end = (void *)(long)ctx->data_end; + void *data = (void *) (long) ctx->data; + void *data_end = (void *) (long) ctx->data_end; struct eth_hdr *eth = data; - if (data + sizeof(*eth) > data_end) - goto DROP; + if (data + sizeof(*eth) > data_end) { + pcn_log(ctx, LOG_TRACE, "dropped Ethernet pkt - (in_port: %d) (reason: inconsistent_size)", md->in_port); + return RX_DROP; + } pcn_log( ctx, LOG_TRACE, - "Received new packet: in_port: %d eth_type: 0x%x mac_src: %M mac_dst: %M", + "received new pkt: in_port: %d eth_type: 0x%x mac_src: %M mac_dst: %M", md->in_port, bpf_ntohs(eth->proto), eth->src, eth->dst); - switch (eth->proto) { - case htons(ETH_P_IP): - // Packet is IP - pcn_log(ctx, LOG_DEBUG, "Received IP Packet"); - break; - case htons(ETH_P_ARP): - pcn_log(ctx, LOG_TRACE, "Received ARP packet. Letting it go through from %d",md->in_port); - return RX_OK; - break; - default: - pcn_log(ctx, LOG_TRACE, "Unknown eth proto: %d, send to stack", - bpf_ntohs(eth->proto)); - return RX_OK; + if (eth->proto != ETH_P_IP) { + pcn_log(ctx, LOG_TRACE, "pkt is not an IP pkt: sent to stack - (eth_proto %d)", bpf_ntohs(eth->proto)); + return RX_OK; } // Packet data @@ -187,20 +179,23 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { uint8_t update_session_table = 1; struct iphdr *ip = data + sizeof(*eth); - if (data + sizeof(*eth) + sizeof(*ip) > data_end) - goto DROP; - - pcn_log(ctx, LOG_TRACE, "Processing IP packet: src %I, dst: %I", ip->saddr, - ip->daddr); -// check if ip dest is for this host - if (ip->daddr != EXTERNAL_IP && md->in_port == FRONTEND_PORT) { - pcn_log(ctx, LOG_DEBUG, "Packet is not for host, send to stack"); - return RX_OK; + if (data + sizeof(*eth) + sizeof(*ip) > data_end) { + pcn_log(ctx, LOG_TRACE, "dropped IP pkt - (in_port: %d) (reason: inconsistent_size)", md->in_port); + return RX_DROP; } - if(md->in_port == BACKEND_PORT){ - pcn_log(ctx, LOG_INFO, "Processing IP packet: src %I, dst: %I", ip->saddr, - ip->daddr); + + // check if ip dest is for this host + if (md->in_port == FRONTEND_PORT && ip->daddr != EXTERNAL_IP) { + pcn_log( + ctx, LOG_TRACE, + "pkt coming from FRONTEND port is not for host: sent to stack - (in_port: %d) (dst_ip: %I)", + md->in_port, ip->daddr + ); + return RX_OK; } + + pcn_log(ctx, LOG_TRACE, "pkt is a valid IP pkt: processing... - (in_port: %d)", md->in_port); + srcIp = ip->saddr; dstIp = ip->daddr; proto = ip->protocol; @@ -208,31 +203,35 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { switch (ip->protocol) { case IPPROTO_TCP: { struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip); - if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end) - goto DROP; + if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end) { + pcn_log(ctx, LOG_TRACE, "dropped TCP pkt - (in_port: %d) (reason: inconsistent_size)", md->in_port); + return RX_DROP; + } + pcn_log(ctx, LOG_TRACE, "pkt is a valid TCP pkt: processing... - (in_port: %d)", md->in_port); - pcn_log(ctx, LOG_TRACE, "Packet is TCP: src_port %P, dst_port %P", - tcp->source, tcp->dest); srcPort = tcp->source; dstPort = tcp->dest; break; } case IPPROTO_UDP: { struct udphdr *udp = data + sizeof(*eth) + sizeof(*ip); - if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end) - goto DROP; - pcn_log(ctx, LOG_TRACE, "Packet is UDP: src_port %P, dst_port %P", - udp->source, udp->dest); + if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end) { + pcn_log(ctx, LOG_TRACE, "dropped UDP pkt - (in_port: %d) (reason: inconsistent_size)", md->in_port); + return RX_DROP; + } + pcn_log(ctx, LOG_TRACE, "pkt is a valid UDP pkt: processing... - (in_port: %d)", md->in_port); + srcPort = udp->source; dstPort = udp->dest; break; } case IPPROTO_ICMP: { struct icmphdr *icmp = data + sizeof(*eth) + sizeof(*ip); - if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end) - goto DROP; - pcn_log(ctx, LOG_TRACE, "Packet is ICMP: type %d, id %d", icmp->type, - icmp->un.echo.id); + if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end) { + pcn_log(ctx, LOG_TRACE, "dropped ICMP pkt - (in_port: %d) (reason: inconsistent_size)", md->in_port); + return RX_DROP; + } + pcn_log(ctx, LOG_TRACE, "pkt is a valid ICMP pkt: processing... - (in_port: %d)", md->in_port); // Consider the ICMP ID as a "port" number for easier handling srcPort = icmp->un.echo.id; @@ -240,8 +239,12 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { break; } default: - pcn_log(ctx, LOG_TRACE, "Unknown L4 proto %d, dropping", ip->protocol); - goto DROP; + pcn_log( + ctx, LOG_TRACE, + "dropped IP pkt - (in_port: %d) (reason: unsupported_type) (ip_proto: %d)", + md->in_port, ip->protocol + ); + return RX_DROP; } // Packet parsed, start session table lookup @@ -255,106 +258,140 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { if (md->in_port == BACKEND_PORT) { // Packet is inside -> outside, check egress session table + pcn_log( + ctx, LOG_TRACE, "checking egress session table... - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); value = egress_session_table.lookup(&key); if (value != NULL) { // Session table hit - pcn_log(ctx, LOG_INFO, "Egress session table: hit"); - newIp = value->new_ip; newPort = value->new_port; - pcn_log(ctx, LOG_INFO, "Egress session table: hit %I, %P",newIp, newPort); - if(srcIp == EXTERNAL_IP && ntohs(srcPort)>=NODEPORT_RANGE_LOW && ntohs(srcPort)<=NODEPORT_RANGE_HIGH) - { - pcn_log(ctx, LOG_INFO, "Egress nodeport packet"); + if (srcIp == EXTERNAL_IP && ntohs(srcPort) >= NODEPORT_RANGE_LOW && ntohs(srcPort) <= NODEPORT_RANGE_HIGH) { rule_type = NAT_DST; - } - else - { + pcn_log(ctx, LOG_TRACE, + "egress session table hit for a NodePort pkt: chosen NAT_DST - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + pcn_log(ctx, LOG_TRACE, "NAT_DST - (dst: %I:%P) (new_dst: %I:%P)", dstIp, dstPort, newIp, newPort); + } else { rule_type = NAT_SRC; + pcn_log(ctx, LOG_TRACE, + "egress session table hit for a non NodePort pkt: chosen NAT_SRC - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + pcn_log(ctx, LOG_TRACE, "NAT_SRC - (src: %I:%P) (new_src: %I:%P)", srcIp, srcPort, newIp, newPort); } update_session_table = 0; goto apply_nat; } - pcn_log(ctx, LOG_TRACE, "Egress session table: miss"); + pcn_log( + ctx, LOG_TRACE, "egress session table miss - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); } else { // Packet is outside -> inside, check ingress session table + pcn_log( + ctx, LOG_TRACE, "checking ingress session table... - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); value = ingress_session_table.lookup(&key); if (value != NULL) { // Session table hit - pcn_log(ctx, LOG_INFO, "Ingress session table: hit"); - newIp = value->new_ip; newPort = value->new_port; - pcn_log(ctx, LOG_INFO, "Ingress session table: hit with %I %P",newIp, newPort); - - if(value->originating_rule_type == NODEPORT_CLUSTER){ - pcn_log(ctx, LOG_INFO, "Ingress session table nodeport"); + if (value->originating_rule_type == NODEPORT_CLUSTER) { rule_type = NAT_SRC; - } - else + pcn_log(ctx, LOG_TRACE, + "ingress session table hit for a NodePort (CLUSTER) service: chosen NAT_SRC - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + pcn_log(ctx, LOG_TRACE, "NAT_SRC - (src: %I:%P) (new_src: %I:%P)", srcIp, srcPort, newIp, newPort); + } else { rule_type = NAT_DST; + pcn_log( + ctx, LOG_TRACE, + "ingress session table hit for a non NodePort (CLUSTER) service: chosen NAT_DST - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + pcn_log(ctx, LOG_TRACE, "NAT_DST - (dst: %I:%P) (new_dst: %I:%P)", dstIp, dstPort, newIp, newPort); + } update_session_table = 0; goto apply_nat; } - pcn_log(ctx, LOG_TRACE, "Ingress session table: miss"); + pcn_log( + ctx, LOG_TRACE, "ingress session table miss - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); } // Session table miss, start rule lookup if (md->in_port == BACKEND_PORT) { // Packet is inside -> outside, check SNAT/MASQUERADE rule table - - pcn_log(ctx, LOG_INFO, "--->rule lookup mio"); - if(srcIp == EXTERNAL_IP){ - if(dstIp == NATTED_IP){ - pcn_log(ctx, LOG_INFO, "xxxxx non dovrei essere qui"); - } - } else { - newIp = EXTERNAL_IP; - newPort = get_free_port(); - goto apply_nat; + pcn_log( + ctx, LOG_TRACE, "checking egress rules... - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + if (srcIp == EXTERNAL_IP) { + pcn_log( + ctx, LOG_TRACE, + "no egress rule match: redirected as is to FRONTEND port - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + return pcn_pkt_redirect(ctx, md, FRONTEND_PORT); } + newIp = EXTERNAL_IP; + newPort = get_free_port(); + pcn_log( + ctx, LOG_TRACE, "matched egress rule: chosen NAT_SRC - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + pcn_log(ctx, LOG_TRACE, "NAT_SRC - (src: %I:%P) (new_src: %I:%P)", srcIp, srcPort, newIp, newPort); + } else { // Packet is outside -> inside, check DNAT/PORTFORWARDING rule table - struct dp_k key = {0, 0, 0}; - key.mask = 56; // 32 (IP) + 16 (Port) + 8 (Proto) - key.external_ip = dstIp; + struct dp_k key = {0, 0}; + pcn_log( + ctx, LOG_TRACE, "checking ingress rules... - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); key.external_port = dstPort; key.proto = proto; struct dp_v *value = dp_rules.lookup(&key); - if(ntohs(dstPort)==30000) - pcn_log(ctx, LOG_INFO, "check ingress: %I:%P",dstIp,dstPort); - if (value != NULL) { - pcn_log(ctx, LOG_INFO, "Ingress rule table: hit"); - - newIp = NATTED_IP; - pcn_log(ctx, LOG_INFO, "value ingress rule: %I:%P",newIp,newPort); + if (value == NULL) { + pcn_log( + ctx, LOG_TRACE, "no ingress rule match: redirected as is to stack - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + return RX_OK; + } - rule_type = value->entry_type; - //todo cambia this is local - if(rule_type == NODEPORT_LOCAL){ - goto proceed; - } - else - rule_type = value->entry_type; - newPort = get_free_port(); - goto apply_nat; + rule_type = value->entry_type; + if (rule_type == NODEPORT_LOCAL) { + pcn_log( + ctx, LOG_TRACE, + "matched ingress rule: NodePort (LOCAL) service pkt forwarded as is to BACKEND port - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + return pcn_pkt_redirect(ctx, md, BACKEND_PORT); } - pcn_log(ctx, LOG_TRACE, "Ingress rule table: miss"); - //todo check - return RX_OK; - } - // No matching entry was found in the session tables - // No matching rule was found in the rule tables - // -> Forward packet as it is - goto proceed; + newIp = NATTED_IP; + newPort = get_free_port(); + pcn_log( + ctx, LOG_TRACE, "matched ingress rule: chosen NAT_SRC - (src: %I:%P, dst: %I:%P)", + srcIp, srcPort, dstIp, dstPort + ); + pcn_log(ctx, LOG_TRACE, "NAT_SRC - (src: %I:%P) (new_src: %I:%P)", srcIp, srcPort, newIp, newPort); + + } apply_nat:; if (update_session_table == 1) { @@ -386,14 +423,6 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { // forward_key.src_ip = dstIp; forward_key.dst_ip = newIp; - /* - if (proto == IPPROTO_ICMP) { - // For ICMP session table entries "source port" and "destination port" - // must be the same, equal to the ICMP ID - reverse_key.src_port = newPort; - } else { - reverse_key.src_port = dstPort; - }*/ forward_key.src_port = dstPort; forward_key.dst_port = newPort; forward_key.proto = proto; @@ -408,13 +437,12 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { srcPort, dstIp, dstPort); pcn_log(ctx, LOG_INFO, - "Using ingress value: newIp %I, newPort %P",reverse_value.new_ip,reverse_value.new_port); + "Using ingress value: newIp %I, newPort %P", reverse_value.new_ip, reverse_value.new_port); pcn_log(ctx, LOG_INFO, - "Using egress value: newIp %I, newPort %P",forward_value.new_ip,forward_value.new_port); + "Using egress value: newIp %I, newPort %P", forward_value.new_ip, forward_value.new_port); rule_type = NAT_SRC; - } - else if (rule_type == NAT_SRC) { + } else if (rule_type == NAT_SRC) { // A rule matched in the inside -> outside direction // Session table entry for the outgoing packets (egress table) @@ -503,10 +531,14 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { // todo continua da qui // Modify packet - uint32_t old_ip = - (rule_type == NAT_SRC ) ? srcIp : dstIp; - uint32_t old_port = - (rule_type == NAT_SRC ) ? srcPort : dstPort; + uint32_t old_ip, old_port; + if (rule_type == NAT_SRC) { + old_ip = srcIp; + old_port = srcPort; + } else { + old_ip = dstIp; + old_port = dstPort; + } uint32_t new_ip = newIp; uint32_t new_port = newPort; @@ -516,87 +548,106 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) { switch (proto) { case IPPROTO_TCP: { struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip); - if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end) - goto DROP; + if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end) { + pcn_log(ctx, LOG_TRACE, + "dropped TCP pkt after new checksum evaluation - (in_port: %d) (reason: inconsistent_size)", + md->in_port); + return RX_DROP; + } - if (rule_type == NAT_SRC ) { + if (rule_type == NAT_SRC) { ip->saddr = new_ip; - tcp->source = (__be16)new_port; - pcn_log(ctx, LOG_INFO, "Natted TCP packet: source, %I:%P -> %I:%P", - old_ip, old_port, new_ip, new_port); + tcp->source = (__be16) + new_port; + pcn_log( + ctx, LOG_TRACE, "natted TCP pkt: (old_src: %I:%P) (new_src: %I:%P)", + old_ip, old_port, new_ip, new_port + ); } else { ip->daddr = new_ip; - tcp->dest = (__be16)new_port; - pcn_log(ctx, LOG_INFO, "Natted TCP packet: destination, %I:%P -> %I:%P", - old_ip, old_port, new_ip, new_port); + tcp->dest = (__be16) + new_port; + pcn_log( + ctx, LOG_TRACE, "natted TCP pkt: (old_dst: %I:%P) (new_dst: %I:%P)", + old_ip, old_port, new_ip, new_port + ); } // Update checksums pcn_l4_csum_replace(ctx, TCP_CSUM_OFFSET, 0, l3sum, IS_PSEUDO | 0); pcn_l4_csum_replace(ctx, TCP_CSUM_OFFSET, 0, l4sum, 0); pcn_l3_csum_replace(ctx, IP_CSUM_OFFSET, 0, l3sum, 0); - - goto proceed; + break; } case IPPROTO_UDP: { struct udphdr *udp = data + sizeof(*eth) + sizeof(*ip); - if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end) - goto DROP; + if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end) { + pcn_log(ctx, LOG_TRACE, + "dropped UDP pkt after new checksum evaluation - (in_port: %d) (reason: inconsistent_size)", + md->in_port); + return RX_DROP; + } - if (rule_type == NAT_SRC ) { + if (rule_type == NAT_SRC) { ip->saddr = new_ip; - udp->source = (__be16)new_port; - pcn_log(ctx, LOG_TRACE, "Natted UDP packet: source, %I:%P -> %I:%P", - old_ip, old_port, new_ip, new_port); + udp->source = (__be16) + new_port; + pcn_log( + ctx, LOG_TRACE, "natted UDP pkt: (old_src: %I:%P) (new_src: %I:%P)", + old_ip, old_port, new_ip, new_port + ); } else { ip->daddr = new_ip; - udp->dest = (__be16)new_port; - pcn_log(ctx, LOG_TRACE, "Natted UDP packet: destination, %I:%P -> %I:%P", - old_ip, old_port, new_ip, new_port); + udp->dest = (__be16) + new_port; + pcn_log( + ctx, LOG_TRACE, "natted UDP pkt: (old_dst: %I:%P) (new_dst: %I:%P)", + old_ip, old_port, new_ip, new_port + ); } // Update checksums pcn_l4_csum_replace(ctx, UDP_CSUM_OFFSET, 0, l3sum, IS_PSEUDO | 0); pcn_l4_csum_replace(ctx, UDP_CSUM_OFFSET, 0, l4sum, 0); pcn_l3_csum_replace(ctx, IP_CSUM_OFFSET, 0, l3sum, 0); - - goto proceed; + break; } case IPPROTO_ICMP: { struct icmphdr *icmp = data + sizeof(*eth) + sizeof(*ip); - if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end) - goto DROP; + if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end) { + pcn_log(ctx, LOG_TRACE, + "dropped ICMP pkt after new checksum evaluation - (in_port: %d) (reason: inconsistent_size)", + md->in_port); + return RX_DROP; + } if (rule_type == NAT_SRC) { ip->saddr = new_ip; - icmp->un.echo.id = (__be16)new_port; - pcn_log(ctx, LOG_TRACE, "Natted ICMP packet: source, %I:%P -> %I:%P", - old_ip, old_port, new_ip, new_port); + icmp->un.echo.id = (__be16) + new_port; + pcn_log( + ctx, LOG_TRACE, "natted ICMP pkt: (old_src: %I:%P) (new_src: %I:%P)", + old_ip, old_port, new_ip, new_port + ); } else { ip->daddr = new_ip; - icmp->un.echo.id = (__be16)new_port; - pcn_log(ctx, LOG_TRACE, "Natted ICMP packet: destination, %I:%P -> %I:%P", - old_ip, old_port, new_ip, new_port); + icmp->un.echo.id = (__be16) + new_port; + pcn_log( + ctx, LOG_TRACE, "natted ICMP pkt: (old_dst: %I:%P) (new_dst: %I:%P)", + old_ip, old_port, new_ip, new_port + ); } // Update checksums pcn_l4_csum_replace(ctx, ICMP_CSUM_OFFSET, 0, l4sum, 0); pcn_l3_csum_replace(ctx, IP_CSUM_OFFSET, 0, l3sum, 0); - - goto proceed; + break; } } - proceed:; // Session tables have been updated (if needed) // The packet has been modified (if needed) // Nothing more to do, forward the packet - if (md->in_port == BACKEND_PORT) { - return pcn_pkt_redirect(ctx, md, FRONTEND_PORT); - } else { - return pcn_pkt_redirect(ctx, md, BACKEND_PORT); - } - DROP:; - pcn_log(ctx, LOG_INFO, "Dropping packet"); - return RX_DROP; + return pcn_pkt_redirect(ctx, md, md->in_port == BACKEND_PORT ? FRONTEND_PORT : BACKEND_PORT); } \ No newline at end of file diff --git a/src/services/pcn-k8sdispatcher/src/NattingRule.cpp b/src/services/pcn-k8sdispatcher/src/NattingRule.cpp index 313544fd8..d34a095f7 100644 --- a/src/services/pcn-k8sdispatcher/src/NattingRule.cpp +++ b/src/services/pcn-k8sdispatcher/src/NattingRule.cpp @@ -31,13 +31,13 @@ NattingRule::NattingRule(K8sdispatcher &parent, const std::string srcIp, const std::string newIp, const uint16_t newPort ) : NattingRuleBase(parent) { - this->srcIp = srcIp; - this->dstIp = dstIp; - this->srcPort = srcPort; - this->dstPort = dstPort; - this->newIp = newIp; - this->newPort = newPort; - this->proto = K8sdispatcher::proto_from_int_to_string(proto); + this->srcIp_ = srcIp; + this->dstIp_ = dstIp; + this->srcPort_ = srcPort; + this->dstPort_ = dstPort; + this->proto_ = K8sdispatcher::proto_from_int_to_string(proto); + this->newIp_ = newIp; + this->newPort_ = newPort; } @@ -71,32 +71,32 @@ NattingRuleJsonObject NattingRule::toJsonObject() { std::string NattingRule::getInternalSrc() { // This method retrieves the internalSrc value. - return srcIp; + return srcIp_; } std::string NattingRule::getInternalDst() { // This method retrieves the internalDst value. - return dstIp; + return dstIp_; } uint16_t NattingRule::getInternalSport() { // This method retrieves the internalSport value. - return srcPort; + return srcPort_; } uint16_t NattingRule::getInternalDport() { // This method retrieves the internalDport value. - return dstPort; + return dstPort_; } std::string NattingRule::getProto() { // This method retrieves the proto value. - return proto; + return proto_; } std::string NattingRule::getExternalIp() { // This method retrieves the externalIp value. - return newIp; + return newIp_; } void NattingRule::setExternalIp(const std::string &value) { @@ -106,7 +106,7 @@ void NattingRule::setExternalIp(const std::string &value) { uint16_t NattingRule::getExternalPort() { // This method retrieves the externalPort value. - return newPort; + return newPort_; } void NattingRule::setExternalPort(const uint16_t &value) { diff --git a/src/services/pcn-k8sdispatcher/src/NattingRule.h b/src/services/pcn-k8sdispatcher/src/NattingRule.h index 4e0a423d1..5ed4eb32c 100644 --- a/src/services/pcn-k8sdispatcher/src/NattingRule.h +++ b/src/services/pcn-k8sdispatcher/src/NattingRule.h @@ -84,11 +84,11 @@ class NattingRule : public NattingRuleBase { virtual NattingRuleJsonObject toJsonObject() override; private: - std::string srcIp; - std::string dstIp; - uint16_t srcPort; - uint16_t dstPort; - std::string proto; - std::string newIp; - uint16_t newPort; + std::string srcIp_; + std::string dstIp_; + uint16_t srcPort_; + uint16_t dstPort_; + std::string proto_; + std::string newIp_; + uint16_t newPort_; }; diff --git a/src/services/pcn-k8sdispatcher/src/NodeportRule.cpp b/src/services/pcn-k8sdispatcher/src/NodeportRule.cpp index 78fd83e33..569050ff4 100644 --- a/src/services/pcn-k8sdispatcher/src/NodeportRule.cpp +++ b/src/services/pcn-k8sdispatcher/src/NodeportRule.cpp @@ -9,43 +9,37 @@ #include "K8sdispatcher.h" -NodeportRule::NodeportRule(K8sdispatcher &parent, const NodeportRuleJsonObject &conf) - : NodeportRuleBase(parent), parent_(parent) { - - //todo modifica - - if (conf.internalSrcIsSet()) - internal_ip_ = conf.getInternalSrc(); - proto_ = conf.getProto(); - port_ = conf.getNodeportPort(); - if (conf.serviceTypeIsSet()) - service_type_ = conf.getServiceType(); - else - service_type_ = NodeportRuleServiceTypeEnum::CLUSTER; +NodeportRule::NodeportRule(K8sdispatcher &parent, const NodeportRuleJsonObject &conf): NodeportRuleBase(parent) { + logger()->info("creating NodeportRule"); + this->name_ = conf.nameIsSet() ? conf.getName() : ""; + this->proto_ = conf.getProto(); + this->port_ = conf.getPort(); + this->serviceType_ = conf.serviceTypeIsSet() ? conf.getServiceType() : NodeportRuleServiceTypeEnum::CLUSTER; + logger()->info("created NodeportRule"); } NodeportRule::~NodeportRule() {} -std::string NodeportRule::getInternalSrc() { - return internal_ip_; +std::string NodeportRule::getName() { + return this->name_; } -void NodeportRule::setInternalSrc(const std::string &value) { - throw std::runtime_error("NodeportRule::setInternalSrc: Method not allowed"); +void NodeportRule::setName(const std::string &value) { + throw std::runtime_error("NodeportRule::setName: Method not implemented"); } -uint16_t NodeportRule::getNodeportPort() { - return port_; +uint16_t NodeportRule::getPort() { + return this->port_; } std::string NodeportRule::getProto() { - return proto_; + return this->proto_; } NodeportRuleServiceTypeEnum NodeportRule::getServiceType() { - return service_type_; + return serviceType_; } void NodeportRule::setServiceType(const NodeportRuleServiceTypeEnum &value) { - throw std::runtime_error("NodeportRule::setServiceType: Method not allowed"); + throw std::runtime_error("NodeportRule::setServiceType: Method not implemented"); } \ No newline at end of file diff --git a/src/services/pcn-k8sdispatcher/src/NodeportRule.h b/src/services/pcn-k8sdispatcher/src/NodeportRule.h index 0c5f5ea5f..94e235b2a 100644 --- a/src/services/pcn-k8sdispatcher/src/NodeportRule.h +++ b/src/services/pcn-k8sdispatcher/src/NodeportRule.h @@ -17,39 +17,35 @@ class K8sdispatcher; using namespace polycube::service::model; class NodeportRule : public NodeportRuleBase { -public: - NodeportRule(K8sdispatcher &parent, const NodeportRuleJsonObject &conf); - - virtual ~NodeportRule(); - - /// - /// Source IP address - /// - std::string getInternalSrc() override; - - void setInternalSrc(const std::string &value) override; - - /// - /// Destination L4 port number - /// - uint16_t getNodeportPort() override; - - /// - /// L4 protocol - /// - std::string getProto() override; - - /// - /// Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoint - /// - NodeportRuleServiceTypeEnum getServiceType() override; - - void setServiceType(const NodeportRuleServiceTypeEnum &value) override; + public: + NodeportRule(K8sdispatcher &parent, const NodeportRuleJsonObject &conf); + virtual ~NodeportRule(); + + /// + /// An optional name for the NodePort rule + /// + std::string getName() override; + void setName(const std::string &value) override; + + /// + /// Destination L4 port number + /// + uint16_t getPort() override; + + /// + /// L4 protocol + /// + std::string getProto() override; + + /// + /// Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoint + /// + NodeportRuleServiceTypeEnum getServiceType() override; + void setServiceType(const NodeportRuleServiceTypeEnum &value) override; private: - K8sdispatcher &parent_; + std::string name_; uint16_t port_; std::string proto_; - std::string internal_ip_; - NodeportRuleServiceTypeEnum service_type_; + NodeportRuleServiceTypeEnum serviceType_; }; diff --git a/src/services/pcn-k8sdispatcher/src/Ports.cpp b/src/services/pcn-k8sdispatcher/src/Ports.cpp index afed7a47f..c89b9e005 100644 --- a/src/services/pcn-k8sdispatcher/src/Ports.cpp +++ b/src/services/pcn-k8sdispatcher/src/Ports.cpp @@ -5,21 +5,17 @@ * https://github.com/polycube-network/polycube-codegen */ - -// TODO: Modify these methods with your own implementation - - #include "Ports.h" #include "K8sdispatcher.h" -Ports::Ports(polycube::service::Cube &parent, - std::shared_ptr port, - const PortsJsonObject &conf) - : PortsBase(parent, port) { +Ports::Ports(polycube::service::Cube &parent, + std::shared_ptr port, + const PortsJsonObject &conf) + : PortsBase(parent, port) { auto name = port->name(); logger()->info("creating new port {0}", name); - this->port_type_ = conf.getType(); + this->portType_ = conf.getType(); if (conf.ipIsSet()) { this->ip_ = conf.getIp(); } @@ -29,19 +25,17 @@ Ports::Ports(polycube::service::Cube &parent, Ports::~Ports() {} PortsTypeEnum Ports::getType() { - return this->port_type_; + return this->portType_; } void Ports::setType(const PortsTypeEnum &value) { - throw std::runtime_error("Ports::setType: Method not implemented"); + throw std::runtime_error("Ports::setType: Method not implemented"); } std::string Ports::getIp() { - return this->ip_; + return this->ip_; } void Ports::setIp(const std::string &value) { - throw std::runtime_error("Ports::setIp: Method not implemented"); + throw std::runtime_error("Ports::setIp: Method not implemented"); } - - diff --git a/src/services/pcn-k8sdispatcher/src/Ports.h b/src/services/pcn-k8sdispatcher/src/Ports.h index 0843441b1..1a12eaeb3 100644 --- a/src/services/pcn-k8sdispatcher/src/Ports.h +++ b/src/services/pcn-k8sdispatcher/src/Ports.h @@ -40,5 +40,5 @@ class Ports : public PortsBase { private: std::string ip_; - PortsTypeEnum port_type_; + PortsTypeEnum portType_; }; diff --git a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.cpp b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.cpp index 459af9e0a..6c9243bca 100644 --- a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.cpp +++ b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.cpp @@ -133,10 +133,10 @@ Response create_k8sdispatcher_nodeport_rule_by_id_handler( const char *value) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -155,9 +155,9 @@ Response create_k8sdispatcher_nodeport_rule_by_id_handler( // Getting the body param NodeportRuleJsonObject unique_value { request_body }; - unique_value.setNodeportPort(unique_nodeportPort); + unique_value.setPort(unique_port); unique_value.setProto(unique_proto); - create_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_nodeportPort, unique_proto, unique_value); + create_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_port, unique_proto, unique_value); return { kCreated, nullptr }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; @@ -327,10 +327,10 @@ Response delete_k8sdispatcher_nodeport_rule_by_id_handler( size_t num_keys ) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -345,7 +345,7 @@ Response delete_k8sdispatcher_nodeport_rule_by_id_handler( try { - delete_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_nodeportPort, unique_proto); + delete_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_port, unique_proto); return { kOk, nullptr }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; @@ -666,10 +666,10 @@ Response read_k8sdispatcher_nodeport_rule_by_id_handler( size_t num_keys ) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -685,7 +685,7 @@ Response read_k8sdispatcher_nodeport_rule_by_id_handler( try { - auto x = read_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_nodeportPort, unique_proto); + auto x = read_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_port, unique_proto); nlohmann::json response_body; response_body = x.toJson(); return { kOk, ::strdup(response_body.dump().c_str()) }; @@ -694,52 +694,52 @@ Response read_k8sdispatcher_nodeport_rule_by_id_handler( } } -Response read_k8sdispatcher_nodeport_rule_internal_src_by_id_handler( +Response read_k8sdispatcher_nodeport_rule_list_by_id_handler( const char *name, const Key *keys, size_t num_keys ) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; - for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; - break; - } - } - - std::string unique_proto; - for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "proto")) { - unique_proto = std::string { keys[i].value.string }; - break; - } - } - try { - auto x = read_k8sdispatcher_nodeport_rule_internal_src_by_id(unique_name, unique_nodeportPort, unique_proto); + auto x = read_k8sdispatcher_nodeport_rule_list_by_id(unique_name); nlohmann::json response_body; - response_body = x; + for (auto &i : x) { + response_body += i.toJson(); + } return { kOk, ::strdup(response_body.dump().c_str()) }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; } } -Response read_k8sdispatcher_nodeport_rule_list_by_id_handler( +Response read_k8sdispatcher_nodeport_rule_name_by_id_handler( const char *name, const Key *keys, size_t num_keys ) { // Getting the path params std::string unique_name { name }; + uint16_t unique_port; + for (size_t i = 0; i < num_keys; ++i) { + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; + break; + } + } + + std::string unique_proto; + for (size_t i = 0; i < num_keys; ++i) { + if (!strcmp(keys[i].name, "proto")) { + unique_proto = std::string { keys[i].value.string }; + break; + } + } + try { - auto x = read_k8sdispatcher_nodeport_rule_list_by_id(unique_name); + auto x = read_k8sdispatcher_nodeport_rule_name_by_id(unique_name, unique_port, unique_proto); nlohmann::json response_body; - for (auto &i : x) { - response_body += i.toJson(); - } + response_body = x; return { kOk, ::strdup(response_body.dump().c_str()) }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; @@ -751,10 +751,10 @@ Response read_k8sdispatcher_nodeport_rule_service_type_by_id_handler( size_t num_keys ) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -770,7 +770,7 @@ Response read_k8sdispatcher_nodeport_rule_service_type_by_id_handler( try { - auto x = read_k8sdispatcher_nodeport_rule_service_type_by_id(unique_name, unique_nodeportPort, unique_proto); + auto x = read_k8sdispatcher_nodeport_rule_service_type_by_id(unique_name, unique_port, unique_proto); nlohmann::json response_body; response_body = NodeportRuleJsonObject::NodeportRuleServiceTypeEnum_to_string(x); return { kOk, ::strdup(response_body.dump().c_str()) }; @@ -987,10 +987,10 @@ Response replace_k8sdispatcher_nodeport_rule_by_id_handler( const char *value) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -1009,9 +1009,9 @@ Response replace_k8sdispatcher_nodeport_rule_by_id_handler( // Getting the body param NodeportRuleJsonObject unique_value { request_body }; - unique_value.setNodeportPort(unique_nodeportPort); + unique_value.setPort(unique_port); unique_value.setProto(unique_proto); - replace_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_nodeportPort, unique_proto, unique_value); + replace_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_port, unique_proto, unique_value); return { kOk, nullptr }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; @@ -1383,10 +1383,10 @@ Response update_k8sdispatcher_nodeport_rule_by_id_handler( const char *value) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -1405,25 +1405,49 @@ Response update_k8sdispatcher_nodeport_rule_by_id_handler( // Getting the body param NodeportRuleJsonObject unique_value { request_body }; - unique_value.setNodeportPort(unique_nodeportPort); + unique_value.setPort(unique_port); unique_value.setProto(unique_proto); - update_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_nodeportPort, unique_proto, unique_value); + update_k8sdispatcher_nodeport_rule_by_id(unique_name, unique_port, unique_proto, unique_value); + return { kOk, nullptr }; + } catch(const std::exception &e) { + return { kGenericError, ::strdup(e.what()) }; + } +} + +Response update_k8sdispatcher_nodeport_rule_list_by_id_handler( + const char *name, const Key *keys, + size_t num_keys , + const char *value) { + // Getting the path params + std::string unique_name { name }; + // Getting the body param + std::vector unique_value; + + try { + auto request_body = nlohmann::json::parse(std::string { value }); + // Getting the body param + std::vector unique_value; + for (auto &j : request_body) { + NodeportRuleJsonObject a { j }; + unique_value.push_back(a); + } + update_k8sdispatcher_nodeport_rule_list_by_id(unique_name, unique_value); return { kOk, nullptr }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; } } -Response update_k8sdispatcher_nodeport_rule_internal_src_by_id_handler( +Response update_k8sdispatcher_nodeport_rule_name_by_id_handler( const char *name, const Key *keys, size_t num_keys , const char *value) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -1441,31 +1465,7 @@ Response update_k8sdispatcher_nodeport_rule_internal_src_by_id_handler( auto request_body = nlohmann::json::parse(std::string { value }); // The conversion is done automatically by the json library std::string unique_value = request_body; - update_k8sdispatcher_nodeport_rule_internal_src_by_id(unique_name, unique_nodeportPort, unique_proto, unique_value); - return { kOk, nullptr }; - } catch(const std::exception &e) { - return { kGenericError, ::strdup(e.what()) }; - } -} - -Response update_k8sdispatcher_nodeport_rule_list_by_id_handler( - const char *name, const Key *keys, - size_t num_keys , - const char *value) { - // Getting the path params - std::string unique_name { name }; - // Getting the body param - std::vector unique_value; - - try { - auto request_body = nlohmann::json::parse(std::string { value }); - // Getting the body param - std::vector unique_value; - for (auto &j : request_body) { - NodeportRuleJsonObject a { j }; - unique_value.push_back(a); - } - update_k8sdispatcher_nodeport_rule_list_by_id(unique_name, unique_value); + update_k8sdispatcher_nodeport_rule_name_by_id(unique_name, unique_port, unique_proto, unique_value); return { kOk, nullptr }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; @@ -1478,10 +1478,10 @@ Response update_k8sdispatcher_nodeport_rule_service_type_by_id_handler( const char *value) { // Getting the path params std::string unique_name { name }; - uint16_t unique_nodeportPort; + uint16_t unique_port; for (size_t i = 0; i < num_keys; ++i) { - if (!strcmp(keys[i].name, "nodeport-port")) { - unique_nodeportPort = keys[i].value.uint16; + if (!strcmp(keys[i].name, "port")) { + unique_port = keys[i].value.uint16; break; } } @@ -1498,7 +1498,7 @@ Response update_k8sdispatcher_nodeport_rule_service_type_by_id_handler( try { auto request_body = nlohmann::json::parse(std::string { value }); NodeportRuleServiceTypeEnum unique_value_ = NodeportRuleJsonObject::string_to_NodeportRuleServiceTypeEnum(request_body); - update_k8sdispatcher_nodeport_rule_service_type_by_id(unique_name, unique_nodeportPort, unique_proto, unique_value_); + update_k8sdispatcher_nodeport_rule_service_type_by_id(unique_name, unique_port, unique_proto, unique_value_); return { kOk, nullptr }; } catch(const std::exception &e) { return { kGenericError, ::strdup(e.what()) }; diff --git a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.h b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.h index c343f0a09..4fc3354f3 100644 --- a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.h +++ b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApi.h @@ -55,8 +55,8 @@ Response read_k8sdispatcher_natting_rule_external_port_by_id_handler(const char Response read_k8sdispatcher_natting_rule_list_by_id_handler(const char *name, const Key *keys, size_t num_keys); Response read_k8sdispatcher_nodeport_range_by_id_handler(const char *name, const Key *keys, size_t num_keys); Response read_k8sdispatcher_nodeport_rule_by_id_handler(const char *name, const Key *keys, size_t num_keys); -Response read_k8sdispatcher_nodeport_rule_internal_src_by_id_handler(const char *name, const Key *keys, size_t num_keys); Response read_k8sdispatcher_nodeport_rule_list_by_id_handler(const char *name, const Key *keys, size_t num_keys); +Response read_k8sdispatcher_nodeport_rule_name_by_id_handler(const char *name, const Key *keys, size_t num_keys); Response read_k8sdispatcher_nodeport_rule_service_type_by_id_handler(const char *name, const Key *keys, size_t num_keys); Response read_k8sdispatcher_ports_by_id_handler(const char *name, const Key *keys, size_t num_keys); Response read_k8sdispatcher_ports_ip_by_id_handler(const char *name, const Key *keys, size_t num_keys); @@ -78,8 +78,8 @@ Response update_k8sdispatcher_natting_rule_external_port_by_id_handler(const cha Response update_k8sdispatcher_natting_rule_list_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); Response update_k8sdispatcher_nodeport_range_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); Response update_k8sdispatcher_nodeport_rule_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); -Response update_k8sdispatcher_nodeport_rule_internal_src_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); Response update_k8sdispatcher_nodeport_rule_list_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); +Response update_k8sdispatcher_nodeport_rule_name_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); Response update_k8sdispatcher_nodeport_rule_service_type_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); Response update_k8sdispatcher_ports_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); Response update_k8sdispatcher_ports_ip_by_id_handler(const char *name, const Key *keys, size_t num_keys, const char *value); diff --git a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.cpp b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.cpp index 3debcadd6..226c43bee 100644 --- a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.cpp +++ b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.cpp @@ -131,7 +131,7 @@ create_k8sdispatcher_natting_rule_list_by_id(const std::string &name, const std: * Create operation of resource: nodeport-rule* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port +* @param[in] port ID of port * @param[in] proto ID of proto * @param[in] value nodeport-rulebody object * @@ -139,10 +139,10 @@ create_k8sdispatcher_natting_rule_list_by_id(const std::string &name, const std: * */ void -create_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &value) { +create_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &value) { auto k8sdispatcher = get_cube(name); - return k8sdispatcher->addNodeportRule(nodeportPort, proto, value); + return k8sdispatcher->addNodeportRule(port, proto, value); } /** @@ -242,17 +242,17 @@ delete_k8sdispatcher_natting_rule_list_by_id(const std::string &name) { * Delete operation of resource: nodeport-rule* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port +* @param[in] port ID of port * @param[in] proto ID of proto * * Responses: * */ void -delete_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto) { +delete_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto) { auto k8sdispatcher = get_cube(name); - return k8sdispatcher->delNodeportRule(nodeportPort, proto); + return k8sdispatcher->delNodeportRule(port, proto); } /** @@ -449,36 +449,16 @@ read_k8sdispatcher_nodeport_range_by_id(const std::string &name) { * Read operation of resource: nodeport-rule* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port +* @param[in] port ID of port * @param[in] proto ID of proto * * Responses: * NodeportRuleJsonObject */ NodeportRuleJsonObject -read_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto) { +read_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto) { auto k8sdispatcher = get_cube(name); - return k8sdispatcher->getNodeportRule(nodeportPort, proto)->toJsonObject(); - -} - -/** -* @brief Read internal-src by ID -* -* Read operation of resource: internal-src* -* -* @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port -* @param[in] proto ID of proto -* -* Responses: -* std::string -*/ -std::string -read_k8sdispatcher_nodeport_rule_internal_src_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto) { - auto k8sdispatcher = get_cube(name); - auto nodeportRule = k8sdispatcher->getNodeportRule(nodeportPort, proto); - return nodeportRule->getInternalSrc(); + return k8sdispatcher->getNodeportRule(port, proto)->toJsonObject(); } @@ -502,22 +482,42 @@ read_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name) { return m; } +/** +* @brief Read name by ID +* +* Read operation of resource: name* +* +* @param[in] name ID of name +* @param[in] port ID of port +* @param[in] proto ID of proto +* +* Responses: +* std::string +*/ +std::string +read_k8sdispatcher_nodeport_rule_name_by_id(const std::string &name, const uint16_t &port, const std::string &proto) { + auto k8sdispatcher = get_cube(name); + auto nodeportRule = k8sdispatcher->getNodeportRule(port, proto); + return nodeportRule->getName(); + +} + /** * @brief Read service-type by ID * * Read operation of resource: service-type* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port +* @param[in] port ID of port * @param[in] proto ID of proto * * Responses: * NodeportRuleServiceTypeEnum */ NodeportRuleServiceTypeEnum -read_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto) { +read_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &port, const std::string &proto) { auto k8sdispatcher = get_cube(name); - auto nodeportRule = k8sdispatcher->getNodeportRule(nodeportPort, proto); + auto nodeportRule = k8sdispatcher->getNodeportRule(port, proto); return nodeportRule->getServiceType(); } @@ -643,7 +643,7 @@ replace_k8sdispatcher_natting_rule_list_by_id(const std::string &name, const std * Replace operation of resource: nodeport-rule* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port +* @param[in] port ID of port * @param[in] proto ID of proto * @param[in] value nodeport-rulebody object * @@ -651,10 +651,10 @@ replace_k8sdispatcher_natting_rule_list_by_id(const std::string &name, const std * */ void -replace_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &value) { +replace_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &value) { auto k8sdispatcher = get_cube(name); - return k8sdispatcher->replaceNodeportRule(nodeportPort, proto, value); + return k8sdispatcher->replaceNodeportRule(port, proto, value); } /** @@ -732,7 +732,7 @@ update_k8sdispatcher_by_id(const std::string &name, const K8sdispatcherJsonObjec * Update operation of resource: internal-src-ip* * * @param[in] name ID of name -* @param[in] value Internal src ip used for services with externaltrafficpolicy=cluster +* @param[in] value Internal src ip used for services with externalTrafficPolicy=CLUSTER * * Responses: * @@ -871,7 +871,7 @@ update_k8sdispatcher_nodeport_range_by_id(const std::string &name, const std::st * Update operation of resource: nodeport-rule* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port +* @param[in] port ID of port * @param[in] proto ID of proto * @param[in] value nodeport-rulebody object * @@ -879,48 +879,48 @@ update_k8sdispatcher_nodeport_range_by_id(const std::string &name, const std::st * */ void -update_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &value) { +update_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &value) { auto k8sdispatcher = get_cube(name); - auto nodeportRule = k8sdispatcher->getNodeportRule(nodeportPort, proto); + auto nodeportRule = k8sdispatcher->getNodeportRule(port, proto); return nodeportRule->update(value); } /** -* @brief Update internal-src by ID +* @brief Update nodeport-rule by ID * -* Update operation of resource: internal-src* +* Update operation of resource: nodeport-rule* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port -* @param[in] proto ID of proto -* @param[in] value Source IP address +* @param[in] value nodeport-rulebody object * * Responses: * */ void -update_k8sdispatcher_nodeport_rule_internal_src_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const std::string &value) { - auto k8sdispatcher = get_cube(name); - auto nodeportRule = k8sdispatcher->getNodeportRule(nodeportPort, proto); - - return nodeportRule->setInternalSrc(value); +update_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name, const std::vector &value) { + throw std::runtime_error("Method not supported"); } /** -* @brief Update nodeport-rule by ID +* @brief Update name by ID * -* Update operation of resource: nodeport-rule* +* Update operation of resource: name* * * @param[in] name ID of name -* @param[in] value nodeport-rulebody object +* @param[in] port ID of port +* @param[in] proto ID of proto +* @param[in] value An optional name for the NodePort rule * * Responses: * */ void -update_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name, const std::vector &value) { - throw std::runtime_error("Method not supported"); +update_k8sdispatcher_nodeport_rule_name_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const std::string &value) { + auto k8sdispatcher = get_cube(name); + auto nodeportRule = k8sdispatcher->getNodeportRule(port, proto); + + return nodeportRule->setName(value); } /** @@ -929,7 +929,7 @@ update_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name, const std * Update operation of resource: service-type* * * @param[in] name ID of name -* @param[in] nodeportPort ID of nodeport-port +* @param[in] port ID of port * @param[in] proto ID of proto * @param[in] value Denotes if this Service desires to route external traffic to node-local or cluster-wide endpoint * @@ -937,9 +937,9 @@ update_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name, const std * */ void -update_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleServiceTypeEnum &value) { +update_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleServiceTypeEnum &value) { auto k8sdispatcher = get_cube(name); - auto nodeportRule = k8sdispatcher->getNodeportRule(nodeportPort, proto); + auto nodeportRule = k8sdispatcher->getNodeportRule(port, proto); return nodeportRule->setServiceType(value); } @@ -1053,7 +1053,7 @@ std::vector> read_k8sdispatcher_nod for(auto &i : nodeportRule) { nlohmann::fifo_map keys; - keys["nodeportPort"] = std::to_string(i->getNodeportPort()); + keys["port"] = std::to_string(i->getPort()); keys["proto"] = i->getProto(); r.push_back(keys); diff --git a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.h b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.h index 862ca51f4..52d1ec1da 100644 --- a/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.h +++ b/src/services/pcn-k8sdispatcher/src/api/K8sdispatcherApiImpl.h @@ -38,14 +38,14 @@ namespace K8sdispatcherApiImpl { void create_k8sdispatcher_by_id(const std::string &name, const K8sdispatcherJsonObject &value); void create_k8sdispatcher_natting_rule_by_id(const std::string &name, const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, const uint16_t &internalDport, const std::string &proto, const NattingRuleJsonObject &value); void create_k8sdispatcher_natting_rule_list_by_id(const std::string &name, const std::vector &value); - void create_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &value); + void create_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &value); void create_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name, const std::vector &value); void create_k8sdispatcher_ports_by_id(const std::string &name, const std::string &portsName, const PortsJsonObject &value); void create_k8sdispatcher_ports_list_by_id(const std::string &name, const std::vector &value); void delete_k8sdispatcher_by_id(const std::string &name); void delete_k8sdispatcher_natting_rule_by_id(const std::string &name, const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, const uint16_t &internalDport, const std::string &proto); void delete_k8sdispatcher_natting_rule_list_by_id(const std::string &name); - void delete_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto); + void delete_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto); void delete_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name); void delete_k8sdispatcher_ports_by_id(const std::string &name, const std::string &portsName); void delete_k8sdispatcher_ports_list_by_id(const std::string &name); @@ -57,10 +57,10 @@ namespace K8sdispatcherApiImpl { uint16_t read_k8sdispatcher_natting_rule_external_port_by_id(const std::string &name, const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, const uint16_t &internalDport, const std::string &proto); std::vector read_k8sdispatcher_natting_rule_list_by_id(const std::string &name); std::string read_k8sdispatcher_nodeport_range_by_id(const std::string &name); - NodeportRuleJsonObject read_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto); - std::string read_k8sdispatcher_nodeport_rule_internal_src_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto); + NodeportRuleJsonObject read_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto); std::vector read_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name); - NodeportRuleServiceTypeEnum read_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto); + std::string read_k8sdispatcher_nodeport_rule_name_by_id(const std::string &name, const uint16_t &port, const std::string &proto); + NodeportRuleServiceTypeEnum read_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &port, const std::string &proto); PortsJsonObject read_k8sdispatcher_ports_by_id(const std::string &name, const std::string &portsName); std::string read_k8sdispatcher_ports_ip_by_id(const std::string &name, const std::string &portsName); std::vector read_k8sdispatcher_ports_list_by_id(const std::string &name); @@ -68,7 +68,7 @@ namespace K8sdispatcherApiImpl { void replace_k8sdispatcher_by_id(const std::string &name, const K8sdispatcherJsonObject &value); void replace_k8sdispatcher_natting_rule_by_id(const std::string &name, const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, const uint16_t &internalDport, const std::string &proto, const NattingRuleJsonObject &value); void replace_k8sdispatcher_natting_rule_list_by_id(const std::string &name, const std::vector &value); - void replace_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &value); + void replace_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &value); void replace_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name, const std::vector &value); void replace_k8sdispatcher_ports_by_id(const std::string &name, const std::string &portsName, const PortsJsonObject &value); void replace_k8sdispatcher_ports_list_by_id(const std::string &name, const std::vector &value); @@ -80,10 +80,10 @@ namespace K8sdispatcherApiImpl { void update_k8sdispatcher_natting_rule_external_port_by_id(const std::string &name, const std::string &internalSrc, const std::string &internalDst, const uint16_t &internalSport, const uint16_t &internalDport, const std::string &proto, const uint16_t &value); void update_k8sdispatcher_natting_rule_list_by_id(const std::string &name, const std::vector &value); void update_k8sdispatcher_nodeport_range_by_id(const std::string &name, const std::string &value); - void update_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &value); - void update_k8sdispatcher_nodeport_rule_internal_src_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const std::string &value); + void update_k8sdispatcher_nodeport_rule_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &value); void update_k8sdispatcher_nodeport_rule_list_by_id(const std::string &name, const std::vector &value); - void update_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleServiceTypeEnum &value); + void update_k8sdispatcher_nodeport_rule_name_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const std::string &value); + void update_k8sdispatcher_nodeport_rule_service_type_by_id(const std::string &name, const uint16_t &port, const std::string &proto, const NodeportRuleServiceTypeEnum &value); void update_k8sdispatcher_ports_by_id(const std::string &name, const std::string &portsName, const PortsJsonObject &value); void update_k8sdispatcher_ports_ip_by_id(const std::string &name, const std::string &portsName, const std::string &value); void update_k8sdispatcher_ports_list_by_id(const std::string &name, const std::vector &value); diff --git a/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.cpp b/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.cpp index c3a829457..6437cfc1f 100644 --- a/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.cpp +++ b/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.cpp @@ -45,9 +45,9 @@ void K8sdispatcherBase::update(const K8sdispatcherJsonObject &conf) { } if (conf.nodeportRuleIsSet()) { for (auto &i : conf.getNodeportRule()) { - auto nodeportPort = i.getNodeportPort(); + auto port = i.getPort(); auto proto = i.getProto(); - auto m = getNodeportRule(nodeportPort, proto); + auto m = getNodeportRule(port, proto); m->update(i); } } @@ -145,25 +145,25 @@ void K8sdispatcherBase::delNattingRuleList() { } void K8sdispatcherBase::addNodeportRuleList(const std::vector &conf) { for (auto &i : conf) { - uint16_t nodeportPort_ = i.getNodeportPort(); + uint16_t port_ = i.getPort(); std::string proto_ = i.getProto(); - addNodeportRule(nodeportPort_, proto_, i); + addNodeportRule(port_, proto_, i); } } -void K8sdispatcherBase::replaceNodeportRule(const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &conf) { - delNodeportRule(nodeportPort, proto); - uint16_t nodeportPort_ = conf.getNodeportPort(); +void K8sdispatcherBase::replaceNodeportRule(const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &conf) { + delNodeportRule(port, proto); + uint16_t port_ = conf.getPort(); std::string proto_ = conf.getProto(); - addNodeportRule(nodeportPort_, proto_, conf); + addNodeportRule(port_, proto_, conf); } void K8sdispatcherBase::delNodeportRuleList() { auto elements = getNodeportRuleList(); for (auto &i : elements) { - uint16_t nodeportPort_ = i->getNodeportPort(); + uint16_t port_ = i->getPort(); std::string proto_ = i->getProto(); - delNodeportRule(nodeportPort_, proto_); + delNodeportRule(port_, proto_); } } diff --git a/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.h b/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.h index ff403356f..989476e6f 100644 --- a/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.h +++ b/src/services/pcn-k8sdispatcher/src/base/K8sdispatcherBase.h @@ -55,7 +55,7 @@ class K8sdispatcherBase: public virtual polycube::service::Cube { virtual void delPortsList(); /// - /// Internal src ip used for services with externaltrafficpolicy=cluster + /// Internal src ip used for services with externalTrafficPolicy=CLUSTER /// virtual std::string getInternalSrcIp() = 0; virtual void setInternalSrcIp(const std::string &value) = 0; @@ -74,12 +74,12 @@ class K8sdispatcherBase: public virtual polycube::service::Cube { /// /// /// - virtual std::shared_ptr getNodeportRule(const uint16_t &nodeportPort, const std::string &proto) = 0; + virtual std::shared_ptr getNodeportRule(const uint16_t &port, const std::string &proto) = 0; virtual std::vector> getNodeportRuleList() = 0; - virtual void addNodeportRule(const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &conf) = 0; + virtual void addNodeportRule(const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &conf) = 0; virtual void addNodeportRuleList(const std::vector &conf); - virtual void replaceNodeportRule(const uint16_t &nodeportPort, const std::string &proto, const NodeportRuleJsonObject &conf); - virtual void delNodeportRule(const uint16_t &nodeportPort,const std::string &proto) = 0; + virtual void replaceNodeportRule(const uint16_t &port, const std::string &proto, const NodeportRuleJsonObject &conf); + virtual void delNodeportRule(const uint16_t &port,const std::string &proto) = 0; virtual void delNodeportRuleList(); /// diff --git a/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.cpp b/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.cpp index 5d4aeba15..6171e3302 100644 --- a/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.cpp +++ b/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.cpp @@ -20,8 +20,8 @@ NodeportRuleBase::~NodeportRuleBase() {} void NodeportRuleBase::update(const NodeportRuleJsonObject &conf) { - if (conf.internalSrcIsSet()) { - setInternalSrc(conf.getInternalSrc()); + if (conf.nameIsSet()) { + setName(conf.getName()); } if (conf.serviceTypeIsSet()) { setServiceType(conf.getServiceType()); @@ -31,8 +31,8 @@ void NodeportRuleBase::update(const NodeportRuleJsonObject &conf) { NodeportRuleJsonObject NodeportRuleBase::toJsonObject() { NodeportRuleJsonObject conf; - conf.setInternalSrc(getInternalSrc()); - conf.setNodeportPort(getNodeportPort()); + conf.setName(getName()); + conf.setPort(getPort()); conf.setProto(getProto()); conf.setServiceType(getServiceType()); diff --git a/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.h b/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.h index 698a9c244..965d46289 100644 --- a/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.h +++ b/src/services/pcn-k8sdispatcher/src/base/NodeportRuleBase.h @@ -39,15 +39,15 @@ class NodeportRuleBase { virtual NodeportRuleJsonObject toJsonObject(); /// - /// Source IP address + /// An optional name for the NodePort rule /// - virtual std::string getInternalSrc() = 0; - virtual void setInternalSrc(const std::string &value) = 0; + virtual std::string getName() = 0; + virtual void setName(const std::string &value) = 0; /// /// Destination L4 port number /// - virtual uint16_t getNodeportPort() = 0; + virtual uint16_t getPort() = 0; /// /// L4 protocol diff --git a/src/services/pcn-k8sdispatcher/src/serializer/K8sdispatcherJsonObject.h b/src/services/pcn-k8sdispatcher/src/serializer/K8sdispatcherJsonObject.h index 68c756d11..423877b94 100644 --- a/src/services/pcn-k8sdispatcher/src/serializer/K8sdispatcherJsonObject.h +++ b/src/services/pcn-k8sdispatcher/src/serializer/K8sdispatcherJsonObject.h @@ -57,7 +57,7 @@ class K8sdispatcherJsonObject : public JsonObjectBase { void unsetPorts(); /// - /// Internal src ip used for services with externaltrafficpolicy=cluster + /// Internal src ip used for services with externalTrafficPolicy=CLUSTER /// std::string getInternalSrcIp() const; void setInternalSrcIp(std::string value); diff --git a/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.cpp b/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.cpp index 1a67b042b..5caa39462 100644 --- a/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.cpp +++ b/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.cpp @@ -18,26 +18,26 @@ namespace service { namespace model { NodeportRuleJsonObject::NodeportRuleJsonObject() { - m_internalSrcIsSet = false; - m_nodeportPortIsSet = false; + m_nameIsSet = false; + m_portIsSet = false; m_protoIsSet = false; m_serviceTypeIsSet = false; } NodeportRuleJsonObject::NodeportRuleJsonObject(const nlohmann::json &val) : JsonObjectBase(val) { - m_internalSrcIsSet = false; - m_nodeportPortIsSet = false; + m_nameIsSet = false; + m_portIsSet = false; m_protoIsSet = false; m_serviceTypeIsSet = false; - if (val.count("internal-src")) { - setInternalSrc(val.at("internal-src").get()); + if (val.count("name")) { + setName(val.at("name").get()); } - if (val.count("nodeport-port")) { - setNodeportPort(val.at("nodeport-port").get()); + if (val.count("port")) { + setPort(val.at("port").get()); } if (val.count("proto")) { @@ -55,12 +55,12 @@ nlohmann::json NodeportRuleJsonObject::toJson() const { val.update(getBase()); } - if (m_internalSrcIsSet) { - val["internal-src"] = m_internalSrc; + if (m_nameIsSet) { + val["name"] = m_name; } - if (m_nodeportPortIsSet) { - val["nodeport-port"] = m_nodeportPort; + if (m_portIsSet) { + val["port"] = m_port; } if (m_protoIsSet) { @@ -74,34 +74,34 @@ nlohmann::json NodeportRuleJsonObject::toJson() const { return val; } -std::string NodeportRuleJsonObject::getInternalSrc() const { - return m_internalSrc; +std::string NodeportRuleJsonObject::getName() const { + return m_name; } -void NodeportRuleJsonObject::setInternalSrc(std::string value) { - m_internalSrc = value; - m_internalSrcIsSet = true; +void NodeportRuleJsonObject::setName(std::string value) { + m_name = value; + m_nameIsSet = true; } -bool NodeportRuleJsonObject::internalSrcIsSet() const { - return m_internalSrcIsSet; +bool NodeportRuleJsonObject::nameIsSet() const { + return m_nameIsSet; } -void NodeportRuleJsonObject::unsetInternalSrc() { - m_internalSrcIsSet = false; +void NodeportRuleJsonObject::unsetName() { + m_nameIsSet = false; } -uint16_t NodeportRuleJsonObject::getNodeportPort() const { - return m_nodeportPort; +uint16_t NodeportRuleJsonObject::getPort() const { + return m_port; } -void NodeportRuleJsonObject::setNodeportPort(uint16_t value) { - m_nodeportPort = value; - m_nodeportPortIsSet = true; +void NodeportRuleJsonObject::setPort(uint16_t value) { + m_port = value; + m_portIsSet = true; } -bool NodeportRuleJsonObject::nodeportPortIsSet() const { - return m_nodeportPortIsSet; +bool NodeportRuleJsonObject::portIsSet() const { + return m_portIsSet; } diff --git a/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.h b/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.h index 2f60cf92b..4b9d0d2a4 100644 --- a/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.h +++ b/src/services/pcn-k8sdispatcher/src/serializer/NodeportRuleJsonObject.h @@ -40,19 +40,19 @@ class NodeportRuleJsonObject : public JsonObjectBase { /// - /// Source IP address + /// An optional name for the NodePort rule /// - std::string getInternalSrc() const; - void setInternalSrc(std::string value); - bool internalSrcIsSet() const; - void unsetInternalSrc(); + std::string getName() const; + void setName(std::string value); + bool nameIsSet() const; + void unsetName(); /// /// Destination L4 port number /// - uint16_t getNodeportPort() const; - void setNodeportPort(uint16_t value); - bool nodeportPortIsSet() const; + uint16_t getPort() const; + void setPort(uint16_t value); + bool portIsSet() const; /// /// L4 protocol @@ -71,10 +71,10 @@ class NodeportRuleJsonObject : public JsonObjectBase { static NodeportRuleServiceTypeEnum string_to_NodeportRuleServiceTypeEnum(const std::string &str); private: - std::string m_internalSrc; - bool m_internalSrcIsSet; - uint16_t m_nodeportPort; - bool m_nodeportPortIsSet; + std::string m_name; + bool m_nameIsSet; + uint16_t m_port; + bool m_portIsSet; std::string m_proto; bool m_protoIsSet; NodeportRuleServiceTypeEnum m_serviceType; diff --git a/src/services/pcn-k8slbrp/src/K8slbrp.cpp b/src/services/pcn-k8slbrp/src/K8slbrp.cpp index a86276d0f..c591b5437 100644 --- a/src/services/pcn-k8slbrp/src/K8slbrp.cpp +++ b/src/services/pcn-k8slbrp/src/K8slbrp.cpp @@ -29,12 +29,12 @@ const std::string K8slbrp::EBPF_IP_TO_FRONTEND_PORT_MAP = "ip_to_frontend_port"; K8slbrp::K8slbrp(const std::string name, const K8slbrpJsonObject &conf) : Cube(conf.getBase(), {K8slbrp::buildK8sLbrpCode(k8slbrp_code, conf.getPortMode())}, {}), K8slbrpBase(name), - k8slbrp_code_ {K8slbrp::buildK8sLbrpCode(k8slbrp_code, conf.getPortMode())}, + k8slbrp_code_{K8slbrp::buildK8sLbrpCode(k8slbrp_code, conf.getPortMode())}, port_mode_{conf.getPortMode()} { logger()->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [K8slbrp] [%n] [%l] %v"); logger()->info( - "creating K8slbrp instance in {0} port mode", - K8slbrpJsonObject::K8slbrpPortModeEnum_to_string(port_mode_) + "creating K8slbrp instance in {0} port mode", + K8slbrpJsonObject::K8slbrpPortModeEnum_to_string(port_mode_) ); addServiceList(conf.getService()); @@ -46,7 +46,7 @@ K8slbrp::~K8slbrp() { logger()->info("destroying K8slbrp instance"); } -std::string K8slbrp::buildK8sLbrpCode(std::string const& k8slbrp_code, K8slbrpPortModeEnum port_mode) { +std::string K8slbrp::buildK8sLbrpCode(std::string const &k8slbrp_code, K8slbrpPortModeEnum port_mode) { if (port_mode == K8slbrpPortModeEnum::SINGLE) { return "#define SINGLE_PORT_MODE 1\n" + k8slbrp_code; } @@ -120,8 +120,7 @@ void K8slbrp::reloadCodeWithNewPorts() { for (auto &it: get_ports()) { if (it->getType() == PortsTypeEnum::FRONTEND) { frontend_port = it->index(); - } - else backend_port = it->index(); + } else backend_port = it->index(); } logger()->debug("reloading code with FRONTEND port {0} and BACKEND port {1}", @@ -336,10 +335,11 @@ std::vector> K8slbrp::getServiceList() { void K8slbrp::addService(const std::string &vip, const uint16_t &vport, const ServiceProtoEnum &proto, const ServiceJsonObject &conf) { - logger()->debug("[Service] Received request to create new service entry"); - logger()->debug("[Service] Virtual IP: {0}, virtual port: {1}, protocol: {2}", - vip, vport, - ServiceJsonObject::ServiceProtoEnum_to_string(proto)); + logger()->debug("[Service] received request to create new service entry"); + logger()->debug( + "[Service] virtual IP: {0}, virtual port: {1}, protocol: {2}", vip, vport, + ServiceJsonObject::ServiceProtoEnum_to_string(proto) + ); if (proto == ServiceProtoEnum::ALL) { // Let's create 3 different services for TCP, UDP and ICMP @@ -361,11 +361,10 @@ void K8slbrp::addService(const std::string &vip, const uint16_t &vport, const Se // We completed all task, let's directly return since we do not have to // execute the code below return; - } else if (proto == ServiceProtoEnum::ICMP && - conf.getVport() != Service::ICMP_EBPF_PORT) { + } else if (proto == ServiceProtoEnum::ICMP && conf.getVport() != Service::ICMP_EBPF_PORT) { throw std::runtime_error( - "ICMP Service requires 0 as virtual port. Since this parameter is " - "useless for ICMP services"); + "ICMP Service requires 0 as virtual port. Since this parameter is useless for ICMP services" + ); } Service::ServiceKey key =