From 61c3fe95f85fb16d5fef682c7e135d38fe0f6a69 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 2 May 2014 14:11:36 +0200 Subject: [PATCH 01/84] feature #2858: Convert to unsigned int from vector attributes --- include/Attribute.h | 10 ++++++++++ src/common/Attribute.cc | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/Attribute.h b/include/Attribute.h index 174612a6c4..97e4bbaeec 100644 --- a/include/Attribute.h +++ b/include/Attribute.h @@ -265,6 +265,16 @@ public: */ int vector_value(const char *name, int& value) const; + /** + * Returns the unsigned integer value + * + * @param name Name of the attribute + * @param value Integer value + * + * @return 0 on success, -1 otherwise + */ + int vector_value(const char *name, unsigned int& value) const; + /** * Returns the long long value * diff --git a/src/common/Attribute.cc b/src/common/Attribute.cc index edc4a0df87..dcc0cd3837 100644 --- a/src/common/Attribute.cc +++ b/src/common/Attribute.cc @@ -281,6 +281,36 @@ int VectorAttribute::vector_value(const char *name, int & value) const /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int VectorAttribute::vector_value(const char *name, unsigned int & value) const +{ + map::const_iterator it; + + it = attribute_value.find(name); + + if ( it == attribute_value.end() ) + { + return -1; + } + + if ( it->second.empty() ) + { + return -1; + } + + istringstream iss(it->second); + iss >> value; + + if (iss.fail() || !iss.eof()) + { + return -1; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int VectorAttribute::vector_value(const char *name, long long& value) const { map::const_iterator it; From 048b0c050be9359ff1d2c10133471ba51ec6d520 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 2 May 2014 15:22:48 +0200 Subject: [PATCH 02/84] feature 2858: Removed duplicated code --- src/vm/VirtualMachine.cc | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index ba388fdd9a..3db4b0591c 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -716,12 +716,6 @@ int VirtualMachine::parse_context(string& error_str) var.str(""); val.str(""); - var << "ETH" << nic_id << "_GATEWAY"; - val << "$NETWORK[GATEWAY, NETWORK=\"" << name << "\"]"; - context->replace(var.str(), val.str()); - - var.str(""); val.str(""); - var << "ETH" << nic_id << "_DNS"; val << "$NETWORK[DNS, NETWORK=\"" << name << "\"]"; context->replace(var.str(), val.str()); From 198ebfdefdb833111eccc9087fb8a53bb75643cb Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 2 May 2014 17:04:48 +0200 Subject: [PATCH 03/84] feature #2858: Implementation of Address Range class --- include/AddressRange.h | 206 +++++++++++++++ src/vnm/AddressRange.cc | 536 ++++++++++++++++++++++++++++++++++++++++ src/vnm/SConstruct | 1 + 3 files changed, 743 insertions(+) create mode 100644 include/AddressRange.h create mode 100644 src/vnm/AddressRange.cc diff --git a/include/AddressRange.h b/include/AddressRange.h new file mode 100644 index 0000000000..738497a4db --- /dev/null +++ b/include/AddressRange.h @@ -0,0 +1,206 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#ifndef ADDRESS_RANGE_H_ +#define ADDRESS_RANGE_H_ + +#include +#include +#include + +using namespace std; + +class VectorAttribute; + +/** + * The Lease class represents an address lease from a Virtual Network. + */ +class AddressRange +{ +public: + + AddressRange(){}; + + virtual ~AddressRange(){}; + + /** + * Init an Address Range based on a vector attribute the following + * attributes will be parsed (* are optional): + * - TYPE = ETHER | IP4 | IP6 | IP4_6 + * - SIZE + * - MAC_START + * - IP_START + * - ULA_PREFIX + * - GLOBAL_PREFIX + * + * The following can be defined to override VNET values: + * - BRIDGE + * - VLAN + * - VLAN_ID + * - PHYDEV + * + * Any value defined in the INHERIT_VNET_ATTR can be defined as well. + * + * Any value for context can be included in the AR. + * + * Example: + * - AR = [ TYPE = "ETHER", SIZE = 128, MAC_START = "00:02:01:02:03:04"] + * - AR = [ TYPE = "ETHER", SIZE = 128] + * - AR = [ TYPE = IP4, + * SIZE = 256, + * IP_START = 10.0.0.0, + * DNS = 10.0.0.2] + * - AR = [ TYPE = "IP6", + * SIZE = 1024, + * ULA_PREFIX = "fd00:0:0:1::", + * GLOBAL_PREFIX = "2001::"] + */ + int init_address_range(VectorAttribute * attr, string& error_msg); + + /** + * Type of Addresses defined by this address range + */ + enum AddressType + { + ETHER = 0x00000001, /** MAC address type */ + IP4 = 0x00000003, /** IP version 4 address */ + IP6 = 0x00000005, /** IP version 6 address */ + IP4_6 = 0x00000007 /** IP dual stack version 4 & 6 addresses */ + }; + + /** + * Returns an unused address, which becomes used and fills a NIC attribute + * with the configuration parameters from the address range. + * + * @param nic the VM NIC attribute + * @return 0 if success + */ + int allocate_addr(VectorAttribute * nic, const vector &inherit); + + /** + * Frees a previous allocated address, referenced by its MAC address + * + * @param mac the MAC address in string form + */ + void free_addr(const string& mac); + + +private: + /* ---------------------------------------------------------------------- */ + /* String to binary conversion functions for different address types */ + /* ---------------------------------------------------------------------- */ + + /** + * MAC to binary (48 bits) + * @param mac in string form 00:02:01:02:03:04 + * @return 0 on success + */ + int mac_to_i(string mac, unsigned int i_mac[]); + + /** + * IP version 4 to binary (32 bits) + * @param ip in string form 192.168.0.2 + * @return 0 on success + */ + int ip_to_i(const string& _ip, unsigned int& i_ip); + + /** + * IPv6 64bits prefix conversion + * @param prefix in string form 2a00:1bc0:b001:A:: + * @return 0 on success + */ + int prefix6_to_i(const string& prefix, unsigned int ip[]); + + /* ---------------------------------------------------------------------- */ + /* NIC setup functions */ + /* ---------------------------------------------------------------------- */ + + void set_mac(unsigned int addr_index, VectorAttribute * nic); + + void set_ip(unsigned int addr_index, VectorAttribute * nic); + + void set_ip6(unsigned int addr_index, VectorAttribute * nic); + + void set_vnet(VectorAttribute *nic, const vector &inherit); + + /* ---------------------------------------------------------------------- */ + /* Address index set helper functions */ + /* ---------------------------------------------------------------------- */ + + void allocated_to_attr(); + + int attr_to_allocated(); + + void allocate_addr(unsigned int addr_index); + + void free_addr(unsigned int addr_index); + + /* ---------------------------------------------------------------------- */ + /* Address Range data */ + /* ---------------------------------------------------------------------- */ + + /** + * The type of addresses defined in the range + */ + AddressType type; + + /** + * Key-Value representation of the attribute + */ + VectorAttribute * attr; + + /** + * ID for this range, unique within the Virtual Network + */ + unsigned int id; + + /** + * Number of addresses in the range + */ + unsigned int size; + + /** + * First IP4 in the range + */ + unsigned int ip_start; + + /** + * First MAC in the range + */ + unsigned int mac_start[2]; + + /** + * Binary representation of the IPv6 address global unicast prefix + */ + unsigned int global6[2]; + + /** + * Binary representation of the IPv6 address site unicast prefix + */ + unsigned int ula6[2]; + + /** + * Next address to lease + */ + unsigned int next; + + /** + * Set of address leased to a VM or other VNET + */ + set allocated; +}; + +#endif diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc new file mode 100644 index 0000000000..e584ef4dc0 --- /dev/null +++ b/src/vnm/AddressRange.cc @@ -0,0 +1,536 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "AddressRange.h" +#include "Attribute.h" +#include "VirtualNetworkPool.h" + +#include + +using namespace std; + +int AddressRange::init_address_range(VectorAttribute * attr, string& error_msg) +{ + string value; + + /* ---------------------- Address Range type ---------------------------- */ + + value = attr->vector_value("TYPE"); + + if (value == "ETHER") + { + type = ETHER; + } + else if (value == "IP4") + { + type = IP4; + } + else if (value == "IP6") + { + type = IP6; + } + else if (value == "IP4_6") + { + type = IP4_6; + } + else + { + error_msg = "Unknown address range TYPE."; + return -1; + } + + /* ---------------------- Size and Attributes --------------------------- */ + + this->attr = attr; + + if ( attr->vector_value("SIZE", size) == -1 || size <= 0 ) + { + error_msg = "Wrong SIZE for address range"; + return -1; + } + + /* -------------------- MAC & IPv4 start addresses ---------------------- */ + + bool do_mac = false; + + value = attr->vector_value("MAC_START"); + + if (value.empty()) + { + do_mac = true; + mac_start[1] = VirtualNetworkPool::mac_prefix(); + } + else + { + if (mac_to_i(value, mac_start) == -1) + { + error_msg = "Wrong format for MAC_START attribute"; + return -1; + }; + } + + switch(type) + { + case ETHER: + case IP6: + if (do_mac) + { + srand(time(0)); + + mac_start[0] = rand() && 0x0000FFFF; + mac_start[0]+= (rand()<<16) && 0xFFFF0000; + } + break; + + case IP4: + case IP4_6: + value = attr->vector_value("IP_START"); + + if (value.empty() || ip_to_i(value, ip_start) == -1) + { + error_msg = "Wrong or empty IP_START attribute"; + return -1; + } + + if (do_mac) + { + mac_start[0] = ip_start; + } + break; + } + + /* -------------------------- IP6 prefixes ------------------------------ */ + + value = attr->vector_value("GLOBAL_PREFIX"); + + if (value.empty()) + { + global6[1]=global6[0]=0; + } + else if (prefix6_to_i(value, global6) != 0 ) + { + error_msg = "Wrong format for IP6 global address prefix"; + return -1; + } + + value = attr->vector_value("ULA_PREFIX"); + + if (value.empty()) + { + ula6[1]=ula6[0]=0; + } + else if (prefix6_to_i(value, ula6) != 0 ) + { + error_msg = "Wrong format for IP6 unique local address prefix"; + return -1; + } + + /* -------------------------- Internal ------------------------------ */ + + next = 0; + + if (attr_to_allocated() == -1) + { + error_msg = "Wrong format for ALLOCATED array"; + return -1; + } + + return 0; +} + +/* ************************************************************************** */ +/* ************************************************************************** */ + +int AddressRange::mac_to_i(string mac, unsigned int i_mac[]) +{ + istringstream iss; + + size_t pos = 0; + int count = 0; + + unsigned int tmp; + + while ( (pos = mac.find(':')) != string::npos ) + { + mac.replace(pos,1," "); + count++; + } + + if (count != 5) + { + return -1; + } + + iss.str(mac); + + i_mac[1] = 0; + i_mac[0] = 0; + + iss >> hex >> i_mac[1] >> ws >> hex >> tmp >> ws; + i_mac[1] <<= 8; + i_mac[1] += tmp; + + for (int i=0;i<4;i++) + { + iss >> hex >> tmp >> ws; + + i_mac[0] <<= 8; + i_mac[0] += tmp; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::ip_to_i(const string& _ip, unsigned int& i_ip) +{ + istringstream iss; + size_t pos=0; + int count = 0; + unsigned int tmp; + + string ip = _ip; + + while ( (pos = ip.find('.')) != string::npos ) + { + ip.replace(pos,1," "); + count++; + } + + if (count != 3) + { + return -1; + } + + iss.str(ip); + + i_ip = 0; + + for (int i=0;i<4;i++) + { + iss >> dec >> tmp >> ws; + + if ( tmp > 255 ) + { + return -1; + } + + i_ip <<= 8; + i_ip += tmp; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::prefix6_to_i(const string& prefix, unsigned int ip[]) +{ + struct in6_addr s6; + + if (prefix.empty()) + { + ip[1] = ip[0] = 0; + return 0; + } + + int rc = inet_pton(AF_INET6, prefix.c_str(), &s6); + + if ( rc != 1 ) + { + return -1; + } + + ip[1] = ntohl(s6.s6_addr32[0]); + ip[0] = ntohl(s6.s6_addr32[1]); + + return 0; +} + +/* ************************************************************************** */ +/* ************************************************************************** */ + +void AddressRange::set_mac(unsigned int addr_index, VectorAttribute * nic) +{ + ostringstream oss; + unsigned int temp_byte; + + for (int i=5;i>=0;i--) + { + if ( i < 4 ) + { + temp_byte = mac_start[0] + addr_index; + temp_byte >>= i*8; + } + else + { + temp_byte = mac_start[1]; + temp_byte >>= (i%4)*8; + } + + temp_byte &= 255; + + oss.width(2); + oss.fill('0'); + oss << hex << temp_byte; + + if(i!=0) + { + oss << ":"; + } + } + + nic->replace("MAC", oss.str()); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AddressRange::set_ip(unsigned int addr_index, VectorAttribute * nic) +{ + ostringstream oss; + unsigned int temp_byte; + + for (int index=0;index<4;index++) + { + temp_byte = ip_start + addr_index; + temp_byte >>= (24-index*8); + temp_byte &= 255; + + oss << temp_byte; + + if(index!=3) + { + oss << "."; + } + } + + nic->replace("IP", oss.str()); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) +{ + unsigned int eui64[2]; + unsigned int mlow = mac_start[0] + addr_index; + + struct in6_addr ip6; + char dst[INET6_ADDRSTRLEN]; + + eui64[1] = ((mac_start[1]+512)<<16) + ((mlow&0xFF000000)>>16) + 0x000000FF; + eui64[0] = 4261412864 + (mlow & 0x00FFFFFF); + + ip6.s6_addr32[2] = htonl(eui64[1]); + ip6.s6_addr32[3] = htonl(eui64[0]); + + /* Link Local */ + ip6.s6_addr32[0] = htonl(0xfe800000); + ip6.s6_addr32[1] = 0; + + if ( inet_ntop(AF_INET6, &ip6, dst, INET6_ADDRSTRLEN) != 0 ) + { + nic->replace("IP6_LINK", dst); + } + + if (ula6[1] != 0 || ula6[0] != 0 ) /* Unique Local Address*/ + { + ip6.s6_addr32[0] = htonl(ula6[1]); + ip6.s6_addr32[1] = htonl(ula6[0]); + + if ( inet_ntop(AF_INET6, &ip6, dst, INET6_ADDRSTRLEN) != 0 ) + { + nic->replace("IP6_ULA", dst); + } + } + + if (global6[1] != 0 || global6[0] != 0 ) /* Glocal Unicast*/ + { + ip6.s6_addr32[0] = htonl(global6[1]); + ip6.s6_addr32[1] = htonl(global6[0]); + + if ( inet_ntop(AF_INET6, &ip6, dst, INET6_ADDRSTRLEN) != 0 ) + { + nic->replace("IP6_GLOBAL", dst); + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AddressRange::set_vnet(VectorAttribute *nic, const vector &inherit) +{ + string bridge = attr->vector_value("BRIDGE"); + string vlan = attr->vector_value("VLAN"); + string vlanid = attr->vector_value("VLAN_ID"); + string phydev = attr->vector_value("PHYDEV"); + + if (!bridge.empty()) + { + nic->replace("BRIDGE", bridge); + } + + if (!vlan.empty()) + { + nic->replace("VLAN", bridge); + } + + if (!phydev.empty()) + { + nic->replace("PHYDEV", phydev); + } + + if (!vlanid.empty()) + { + nic->replace("VLAN_ID", vlanid); + } + + vector::const_iterator it; + + for (it = inherit.begin(); it != inherit.end(); it++) + { + string inherit_val = attr->vector_value((*it).c_str()); + + if (!inherit_val.empty()) + { + nic->replace((*it).c_str(), inherit_val); + } + } +} + +/* ************************************************************************** */ +/* ************************************************************************** */ + +void AddressRange::allocated_to_attr() +{ + if (allocated.empty()) + { + attr->remove("ALLOCATED"); + return; + } + + set::const_iterator it; + + ostringstream oss; + + for (it = allocated.begin(); it != allocated.end(); it++) + { + oss << " " << (*it); + } + + attr->replace("ALLOCATED", oss.str()); +} + +/* -------------------------------------------------------------------------- */ + +int AddressRange::attr_to_allocated() +{ + string allocated_s = attr->vector_value("ALLOCATED"); + + if (allocated_s.empty()) + { + allocated.clear(); + return 0; + } + + istringstream iss(allocated_s); + unsigned int addr_index; + + while (!iss.eof()) + { + iss >> ws >> addr_index; + + if (!iss.fail()) + { + allocated.insert(addr_index); + return -1; + } + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ + +void AddressRange::allocate_addr(unsigned int addr_index) +{ + allocated.insert(addr_index); + + allocated_to_attr(); +} + +/* -------------------------------------------------------------------------- */ + +void AddressRange::free_addr(unsigned int addr_index) +{ + allocated.erase(addr_index); + + allocated_to_attr(); +} + +/* ************************************************************************** */ +/* ************************************************************************** */ + +int AddressRange::allocate_addr(VectorAttribute * nic, + const vector &inherit) +{ + int rc = -1; + + for ( unsigned int i=0; i= mac_start[0] ) + { + free_addr(mac_i[0] - mac_start[0]); + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ diff --git a/src/vnm/SConstruct b/src/vnm/SConstruct index 17ecc14a63..f3466b8e82 100644 --- a/src/vnm/SConstruct +++ b/src/vnm/SConstruct @@ -27,6 +27,7 @@ source_files=[ 'RangedLeases.cc', 'VirtualNetwork.cc', 'VirtualNetworkPool.cc', + 'AddressRange.cc' ] # Build library From e13cfcf72640de2be772ad999b72f50cc5ee0782 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 5 May 2014 17:49:57 +0200 Subject: [PATCH 04/84] feature #2858: Allocate addresses by MAC or IP --- include/AddressRange.h | 22 +++++++- src/vnm/AddressRange.cc | 108 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 127 insertions(+), 3 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index 738497a4db..ca338d3e2d 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -32,7 +32,7 @@ class AddressRange { public: - AddressRange(){}; + AddressRange(unsigned int _id):id(_id){}; virtual ~AddressRange(){}; @@ -90,6 +90,26 @@ public: */ int allocate_addr(VectorAttribute * nic, const vector &inherit); + /** + * Returns the specific address by mac if is not allocated. The NIC attr + * is filled with the configuration parameters from the address range. + * + * @param nic the VM NIC attribute + * @return 0 if success + */ + int allocate_by_mac(const string& mac, VectorAttribute * nic, + const vector &inherit); + + /** + * Returns the specific address by ip if is not allocated. The NIC attr + * is filled with the configuration parameters from the address range. + * + * @param nic the VM NIC attribute + * @return 0 if success + */ + int allocate_by_ip(const string& ip, VectorAttribute * nic, + const vector &inherit); + /** * Frees a previous allocated address, referenced by its MAC address * diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index e584ef4dc0..62908600cb 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -140,6 +140,17 @@ int AddressRange::init_address_range(VectorAttribute * attr, string& error_msg) /* -------------------------- Internal ------------------------------ */ + unsigned int tmp_id; + + if (attr->vector_value("AR_ID", tmp_id) == 0) + { + id = tmp_id; + } + else //id initialized in constructor missing from address range vector + { + attr->replace("AR_ID", id); + } + next = 0; if (attr_to_allocated() == -1) @@ -378,6 +389,8 @@ void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) void AddressRange::set_vnet(VectorAttribute *nic, const vector &inherit) { + nic->replace("AR_ID", id); + string bridge = attr->vector_value("BRIDGE"); string vlan = attr->vector_value("VLAN"); string vlanid = attr->vector_value("VLAN_ID"); @@ -511,6 +524,8 @@ int AddressRange::allocate_addr(VectorAttribute * nic, } set_vnet(nic, inherit); + + allocate_addr(next); } } @@ -520,15 +535,104 @@ int AddressRange::allocate_addr(VectorAttribute * nic, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int AddressRange::allocate_by_mac(const string& mac, VectorAttribute * nic, + const vector &inherit) +{ + unsigned int mac_i[2]; + + if (mac_to_i(mac, mac_i) == -1) + { + return -1; + } + + if ((mac_i[1] != mac_start[1]) || (mac_i[0] < mac_start[0])) + { + return -1; + } + + unsigned int index = mac_i[0] - mac_start[0]; + + if ((allocated.count(index) != 0) || (index >= size)) + { + return -1; + } + + set_mac(index, nic); + + if (type && 0x00000002 ) + { + set_ip(index, nic); + } + + if (type && 0x00000004) + { + set_ip6(index, nic); + } + + set_vnet(nic, inherit); + + allocate_addr(next); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::allocate_by_ip(const string& ip, VectorAttribute * nic, + const vector &inherit) +{ + unsigned int ip_i; + + if (ip_to_i(ip, ip_i) == -1) + { + return -1; + } + + if (ip_i < ip_start) + { + return -1; + } + + unsigned int index = ip_i - ip_start; + + if (allocated.count(index) != 0 || index >= size ) + { + return -1; + } + + set_mac(index, nic); + + if (type && 0x00000002 ) + { + set_ip(index, nic); + } + + if (type && 0x00000004) + { + set_ip6(index, nic); + } + + set_vnet(nic, inherit); + + allocate_addr(next); + + return 0; +} +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void AddressRange::free_addr(const string& mac) { unsigned int mac_i[2]; mac_to_i(mac, mac_i); - if ( mac_i[0] >= mac_start[0] ) + unsigned int index = mac_i[0] - mac_start[0]; + + if ((mac_i[0] >= mac_start[0]) && (index < size)) { - free_addr(mac_i[0] - mac_start[0]); + free_addr(index); } } From acce1cdaee37602d5e79833563224a5f94709f02 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 May 2014 13:42:28 +0200 Subject: [PATCH 05/84] feature #2858: Work on AddressRange class --- include/AddressRange.h | 121 +++++++++++++++----- include/Attribute.h | 12 ++ src/vnm/AddressRange.cc | 247 ++++++++++++++++++++++++++-------------- 3 files changed, 263 insertions(+), 117 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index ca338d3e2d..ace2eed5b0 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -21,6 +21,8 @@ #include #include +#include "PoolObjectSQL.h" + using namespace std; class VectorAttribute; @@ -32,7 +34,7 @@ class AddressRange { public: - AddressRange(unsigned int _id):id(_id){}; + AddressRange(unsigned int _id):id(_id),next(0){}; virtual ~AddressRange(){}; @@ -41,8 +43,8 @@ public: * attributes will be parsed (* are optional): * - TYPE = ETHER | IP4 | IP6 | IP4_6 * - SIZE - * - MAC_START - * - IP_START + * - MAC + * - IP * - ULA_PREFIX * - GLOBAL_PREFIX * @@ -57,30 +59,45 @@ public: * Any value for context can be included in the AR. * * Example: - * - AR = [ TYPE = "ETHER", SIZE = 128, MAC_START = "00:02:01:02:03:04"] + * - AR = [ TYPE = "ETHER", SIZE = 128, MAC = "00:02:01:02:03:04"] * - AR = [ TYPE = "ETHER", SIZE = 128] - * - AR = [ TYPE = IP4, - * SIZE = 256, - * IP_START = 10.0.0.0, - * DNS = 10.0.0.2] + * - AR = [ TYPE = IP4, + * SIZE = 256, + * IP = 10.0.0.0, + * DNS = 10.0.0.5] * - AR = [ TYPE = "IP6", * SIZE = 1024, * ULA_PREFIX = "fd00:0:0:1::", * GLOBAL_PREFIX = "2001::"] */ - int init_address_range(VectorAttribute * attr, string& error_msg); + int from_vattr(VectorAttribute * attr, string& error_msg); /** * Type of Addresses defined by this address range */ enum AddressType { + NONE = 0x00000000, /** Undefined Address Type */ ETHER = 0x00000001, /** MAC address type */ IP4 = 0x00000003, /** IP version 4 address */ IP6 = 0x00000005, /** IP version 6 address */ IP4_6 = 0x00000007 /** IP dual stack version 4 & 6 addresses */ }; + /** + * Return the string representation of an AddressType + * @param ob the type + * @return the string + */ + static string type_to_str(AddressType ob); + + /** + * Return the string representation of an AddressType + * @param ob the type + * @return the string + */ + static AddressType str_to_type(string& str_type); + /** * Returns an unused address, which becomes used and fills a NIC attribute * with the configuration parameters from the address range. @@ -88,7 +105,8 @@ public: * @param nic the VM NIC attribute * @return 0 if success */ - int allocate_addr(VectorAttribute * nic, const vector &inherit); + int allocate_addr(PoolObjectSQL::ObjectType ot, int obid, + VectorAttribute * nic, const vector &inherit); /** * Returns the specific address by mac if is not allocated. The NIC attr @@ -97,8 +115,8 @@ public: * @param nic the VM NIC attribute * @return 0 if success */ - int allocate_by_mac(const string& mac, VectorAttribute * nic, - const vector &inherit); + int allocate_by_mac(const string& mac, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, const vector &inherit); /** * Returns the specific address by ip if is not allocated. The NIC attr @@ -107,8 +125,8 @@ public: * @param nic the VM NIC attribute * @return 0 if success */ - int allocate_by_ip(const string& ip, VectorAttribute * nic, - const vector &inherit); + int allocate_by_ip(const string& ip, PoolObjectSQL::ObjectType ot, + int obid, VectorAttribute * nic, const vector &inherit); /** * Frees a previous allocated address, referenced by its MAC address @@ -117,7 +135,6 @@ public: */ void free_addr(const string& mac); - private: /* ---------------------------------------------------------------------- */ /* String to binary conversion functions for different address types */ @@ -148,40 +165,63 @@ private: /* NIC setup functions */ /* ---------------------------------------------------------------------- */ + /** + * Writes MAC address to the given NIC attribute + * @param addr_index internal index for the lease + * @param nic attribute of a VMTemplate + */ void set_mac(unsigned int addr_index, VectorAttribute * nic); + /** + * Writes IP address to the given NIC attribute + * @param addr_index internal index for the lease + * @param nic attribute of a VMTemplate + */ void set_ip(unsigned int addr_index, VectorAttribute * nic); + /** + * Writes IPv6 address to the given NIC attribute + * @param addr_index internal index for the lease + * @param nic attribute of a VMTemplate + */ void set_ip6(unsigned int addr_index, VectorAttribute * nic); + /** + * Writes VNET configuration attributes to the given NIC attribute. It + * includes: BRIDGE, VLAN, VLAN_ID, PHYDEV and INHERIT_VNET_ATTR in oned.conf + * @param addr_index internal index for the lease + * @param nic attribute of a VMTemplate + */ void set_vnet(VectorAttribute *nic, const vector &inherit); /* ---------------------------------------------------------------------- */ - /* Address index set helper functions */ + /* Address index map helper functions */ /* ---------------------------------------------------------------------- */ void allocated_to_attr(); - int attr_to_allocated(); + int attr_to_allocated(const string& allocated_s); - void allocate_addr(unsigned int addr_index); + void allocate_addr(PoolObjectSQL::ObjectType ot, int obid, + unsigned int addr_index); void free_addr(unsigned int addr_index); + /* ---------------------------------------------------------------------- */ + /* Restricted Attributes functions */ + /* ---------------------------------------------------------------------- */ + bool check(string& rs_attr); + + static void set_restricted_attributes(vector& rattrs); + /* ---------------------------------------------------------------------- */ /* Address Range data */ /* ---------------------------------------------------------------------- */ - /** * The type of addresses defined in the range */ AddressType type; - /** - * Key-Value representation of the attribute - */ - VectorAttribute * attr; - /** * ID for this range, unique within the Virtual Network */ @@ -195,12 +235,12 @@ private: /** * First IP4 in the range */ - unsigned int ip_start; + unsigned int ip; /** * First MAC in the range */ - unsigned int mac_start[2]; + unsigned int mac[2]; /** * Binary representation of the IPv6 address global unicast prefix @@ -213,14 +253,35 @@ private: unsigned int ula6[2]; /** - * Next address to lease + * The Address Range attributes as a Template VectorAttribute. This is + * used to generate XML or a template representation of the AR. */ - unsigned int next; + VectorAttribute * attr; + + /* ---------------------------------------------------------------------- */ + /* Allocated address & control */ + /* ---------------------------------------------------------------------- */ + /** + * Map to store the allocated address indexed by the address index relative + * to the mac/ip values. It contains also the type and id of the object + * owning the address ObjectType(32bits) | Object ID (32) + */ + map allocated; + + unsigned int next; + + /* ---------------------------------------------------------------------- */ + /* Restricted Attributes */ + /* ---------------------------------------------------------------------- */ + /** + * TRUE if restricted attributes have been defined for Address Ranges + */ + static bool restricted_set; /** - * Set of address leased to a VM or other VNET + * The restricted attributes from oned.conf */ - set allocated; + static set restricted_attributes; }; #endif diff --git a/include/Attribute.h b/include/Attribute.h index 97e4bbaeec..a65b7a828e 100644 --- a/include/Attribute.h +++ b/include/Attribute.h @@ -367,6 +367,18 @@ public: replace(name, oss.str()); } + /** + * Replace the value of the given vector attribute + */ + void replace(const string& name, unsigned int value) + { + ostringstream oss; + + oss << value; + + replace(name, oss.str()); + } + /** * Replace the value of the given vector attribute */ diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 62908600cb..d198048966 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -17,46 +17,74 @@ #include "AddressRange.h" #include "Attribute.h" #include "VirtualNetworkPool.h" +#include "NebulaUtil.h" #include using namespace std; -int AddressRange::init_address_range(VectorAttribute * attr, string& error_msg) +/* ************************************************************************** */ +/* ************************************************************************** */ + +string AddressRange::type_to_str(AddressType ob) { - string value; - - /* ---------------------- Address Range type ---------------------------- */ - - value = attr->vector_value("TYPE"); - - if (value == "ETHER") + switch (ob) { - type = ETHER; + case ETHER: return "ETHER"; break; + case IP4: return "IP4" ; break; + case IP6: return "IP6" ; break; + case IP4_6: return "IP4_6"; break; + default: return ""; } - else if (value == "IP4") +}; + +/* -------------------------------------------------------------------------- */ + +AddressRange::AddressType AddressRange::str_to_type(string& str_type) +{ + one_util::toupper(str_type); + + if (str_type == "ETHER") { - type = IP4; + return ETHER; } - else if (value == "IP6") + else if (str_type == "IP4") { - type = IP6; + return IP4; } - else if (value == "IP4_6") + else if (str_type == "IP6") { - type = IP4_6; + return IP6; + } + else if (str_type == "IP4_6") + { + return IP4_6; } else { - error_msg = "Unknown address range TYPE."; + return NONE; + } +} + +/* ************************************************************************** */ +/* ************************************************************************** */ + +int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) +{ + string value; + + /* ------------------------- AR Type & Size ---------------------------- */ + + value = attr->vector_value("TYPE"); + type = str_to_type(value); + + if (type == NONE) + { + error_msg = "Unknown or missing address range TYPE."; return -1; } - /* ---------------------- Size and Attributes --------------------------- */ - - this->attr = attr; - - if ( attr->vector_value("SIZE", size) == -1 || size <= 0 ) + if ( attr->vector_value("SIZE", size) != 0 || size <= 0 ) { error_msg = "Wrong SIZE for address range"; return -1; @@ -66,16 +94,16 @@ int AddressRange::init_address_range(VectorAttribute * attr, string& error_msg) bool do_mac = false; - value = attr->vector_value("MAC_START"); + value = attr->vector_value("MAC"); if (value.empty()) { - do_mac = true; - mac_start[1] = VirtualNetworkPool::mac_prefix(); + do_mac = true; + mac[1] = VirtualNetworkPool::mac_prefix(); } else { - if (mac_to_i(value, mac_start) == -1) + if (mac_to_i(value, mac) == -1) { error_msg = "Wrong format for MAC_START attribute"; return -1; @@ -90,16 +118,16 @@ int AddressRange::init_address_range(VectorAttribute * attr, string& error_msg) { srand(time(0)); - mac_start[0] = rand() && 0x0000FFFF; - mac_start[0]+= (rand()<<16) && 0xFFFF0000; + mac[0] = rand() && 0x0000FFFF; + mac[0]+= (rand()<<16) && 0xFFFF0000; } break; case IP4: case IP4_6: - value = attr->vector_value("IP_START"); + value = attr->vector_value("IP"); - if (value.empty() || ip_to_i(value, ip_start) == -1) + if (value.empty() || ip_to_i(value, ip) == -1) { error_msg = "Wrong or empty IP_START attribute"; return -1; @@ -107,9 +135,12 @@ int AddressRange::init_address_range(VectorAttribute * attr, string& error_msg) if (do_mac) { - mac_start[0] = ip_start; + mac[0] = ip; } break; + + default: + return -1; } /* -------------------------- IP6 prefixes ------------------------------ */ @@ -138,25 +169,15 @@ int AddressRange::init_address_range(VectorAttribute * attr, string& error_msg) return -1; } - /* -------------------------- Internal ------------------------------ */ + /* ------------------------ AR Internal Data ---------------------------- */ - unsigned int tmp_id; + this->attr = attr; - if (attr->vector_value("AR_ID", tmp_id) == 0) + attr->replace("AR_ID", id); + + if (do_mac) //Need to add MAC_START to the attribute { - id = tmp_id; - } - else //id initialized in constructor missing from address range vector - { - attr->replace("AR_ID", id); - } - - next = 0; - - if (attr_to_allocated() == -1) - { - error_msg = "Wrong format for ALLOCATED array"; - return -1; + set_mac(0, attr); } return 0; @@ -286,12 +307,12 @@ void AddressRange::set_mac(unsigned int addr_index, VectorAttribute * nic) { if ( i < 4 ) { - temp_byte = mac_start[0] + addr_index; + temp_byte = mac[0] + addr_index; temp_byte >>= i*8; } else { - temp_byte = mac_start[1]; + temp_byte = mac[1]; temp_byte >>= (i%4)*8; } @@ -320,7 +341,7 @@ void AddressRange::set_ip(unsigned int addr_index, VectorAttribute * nic) for (int index=0;index<4;index++) { - temp_byte = ip_start + addr_index; + temp_byte = ip + addr_index; temp_byte >>= (24-index*8); temp_byte &= 255; @@ -341,12 +362,12 @@ void AddressRange::set_ip(unsigned int addr_index, VectorAttribute * nic) void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) { unsigned int eui64[2]; - unsigned int mlow = mac_start[0] + addr_index; + unsigned int mlow = mac[0] + addr_index; struct in6_addr ip6; char dst[INET6_ADDRSTRLEN]; - eui64[1] = ((mac_start[1]+512)<<16) + ((mlow&0xFF000000)>>16) + 0x000000FF; + eui64[1] = ((mac[1]+512)<<16) + ((mlow & 0xFF000000)>>16) + 0x000000FF; eui64[0] = 4261412864 + (mlow & 0x00FFFFFF); ip6.s6_addr32[2] = htonl(eui64[1]); @@ -361,7 +382,7 @@ void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) nic->replace("IP6_LINK", dst); } - if (ula6[1] != 0 || ula6[0] != 0 ) /* Unique Local Address*/ + if (ula6[1] != 0 || ula6[0] != 0 ) /* Unique Local Address */ { ip6.s6_addr32[0] = htonl(ula6[1]); ip6.s6_addr32[1] = htonl(ula6[0]); @@ -372,7 +393,7 @@ void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) } } - if (global6[1] != 0 || global6[0] != 0 ) /* Glocal Unicast*/ + if (global6[1] != 0 || global6[0] != 0 ) /* Glocal Unicast */ { ip6.s6_addr32[0] = htonl(global6[1]); ip6.s6_addr32[1] = htonl(global6[0]); @@ -436,17 +457,16 @@ void AddressRange::allocated_to_attr() { if (allocated.empty()) { - attr->remove("ALLOCATED"); return; } - set::const_iterator it; + map::const_iterator it; ostringstream oss; for (it = allocated.begin(); it != allocated.end(); it++) { - oss << " " << (*it); + oss << " " << it->first << " " << it->second; } attr->replace("ALLOCATED", oss.str()); @@ -454,10 +474,8 @@ void AddressRange::allocated_to_attr() /* -------------------------------------------------------------------------- */ -int AddressRange::attr_to_allocated() +int AddressRange::attr_to_allocated(const string& allocated_s) { - string allocated_s = attr->vector_value("ALLOCATED"); - if (allocated_s.empty()) { allocated.clear(); @@ -466,14 +484,15 @@ int AddressRange::attr_to_allocated() istringstream iss(allocated_s); unsigned int addr_index; + long long object_pack; while (!iss.eof()) { - iss >> ws >> addr_index; + iss >> ws >> addr_index >> ws >> object_pack; if (!iss.fail()) { - allocated.insert(addr_index); + allocated.insert(make_pair(addr_index,object_pack)); return -1; } } @@ -483,11 +502,11 @@ int AddressRange::attr_to_allocated() /* -------------------------------------------------------------------------- */ -void AddressRange::allocate_addr(unsigned int addr_index) +void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, + unsigned int addr_index) { - allocated.insert(addr_index); - allocated_to_attr(); + allocated.insert(make_pair(addr_index,ot|obid)); } /* -------------------------------------------------------------------------- */ @@ -495,15 +514,16 @@ void AddressRange::allocate_addr(unsigned int addr_index) void AddressRange::free_addr(unsigned int addr_index) { allocated.erase(addr_index); - - allocated_to_attr(); } /* ************************************************************************** */ /* ************************************************************************** */ -int AddressRange::allocate_addr(VectorAttribute * nic, - const vector &inherit) +int AddressRange::allocate_addr( + PoolObjectSQL::ObjectType ot, + int obid, + VectorAttribute* nic, + const vector& inherit) { int rc = -1; @@ -525,7 +545,7 @@ int AddressRange::allocate_addr(VectorAttribute * nic, set_vnet(nic, inherit); - allocate_addr(next); + allocate_addr(ot, obid, next); } } @@ -535,22 +555,26 @@ int AddressRange::allocate_addr(VectorAttribute * nic, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int AddressRange::allocate_by_mac(const string& mac, VectorAttribute * nic, - const vector &inherit) +int AddressRange::allocate_by_mac( + const string& mac_s, + PoolObjectSQL::ObjectType ot, + int obid, + VectorAttribute* nic, + const vector& inherit) { unsigned int mac_i[2]; - if (mac_to_i(mac, mac_i) == -1) + if (mac_to_i(mac_s, mac_i) == -1) { return -1; } - if ((mac_i[1] != mac_start[1]) || (mac_i[0] < mac_start[0])) + if ((mac_i[1] != mac[1]) || (mac_i[0] < mac[0])) { return -1; } - unsigned int index = mac_i[0] - mac_start[0]; + unsigned int index = mac_i[0] - mac[0]; if ((allocated.count(index) != 0) || (index >= size)) { @@ -571,7 +595,7 @@ int AddressRange::allocate_by_mac(const string& mac, VectorAttribute * nic, set_vnet(nic, inherit); - allocate_addr(next); + allocate_addr(ot, obid, next); return 0; } @@ -579,22 +603,26 @@ int AddressRange::allocate_by_mac(const string& mac, VectorAttribute * nic, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int AddressRange::allocate_by_ip(const string& ip, VectorAttribute * nic, - const vector &inherit) +int AddressRange::allocate_by_ip( + const string& ip_s, + PoolObjectSQL::ObjectType ot, + int obid, + VectorAttribute* nic, + const vector& inherit) { unsigned int ip_i; - if (ip_to_i(ip, ip_i) == -1) + if (ip_to_i(ip_s, ip_i) == -1) { return -1; } - if (ip_i < ip_start) + if (ip_i < ip) { return -1; } - unsigned int index = ip_i - ip_start; + unsigned int index = ip_i - ip; if (allocated.count(index) != 0 || index >= size ) { @@ -615,26 +643,71 @@ int AddressRange::allocate_by_ip(const string& ip, VectorAttribute * nic, set_vnet(nic, inherit); - allocate_addr(next); + allocate_addr(ot, obid, next); return 0; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRange::free_addr(const string& mac) +void AddressRange::free_addr(const string& mac_s) { unsigned int mac_i[2]; - mac_to_i(mac, mac_i); + mac_to_i(mac_s, mac_i); - unsigned int index = mac_i[0] - mac_start[0]; + unsigned int index = mac_i[0] - mac[0]; - if ((mac_i[0] >= mac_start[0]) && (index < size)) + if ((mac_i[0] >= mac[0]) && (index < size)) { free_addr(index); } } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ +/* ************************************************************************** */ +/* ************************************************************************** */ + +bool AddressRange::restricted_set = false; + +set AddressRange::restricted_attributes; + +bool AddressRange::check(string& rs_attr) +{ + if (!restricted_set) + { + return false; + } + + const map& ar_attrs = attr->value(); + map::const_iterator it; + + for (it=ar_attrs.begin(); it != ar_attrs.end(); it++) + { + if (restricted_attributes.count(it->first) > 0) + { + rs_attr = it->first; + return true; + } + } + + return false; +}; + +void AddressRange::set_restricted_attributes( + vector& rattrs) +{ + if (restricted_set) + { + return; + } + + restricted_set = true; + + for (unsigned int i = 0 ; i < rattrs.size() ; i++ ) + { + const SingleAttribute * sattr = static_cast(rattrs[i]); + string attr_s = sattr->value(); + + restricted_attributes.insert(one_util::toupper(attr_s)); + } +}; From 771b41f39f3669439b56380dc8f166f8cdc0cfaf Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 8 May 2014 15:48:16 +0200 Subject: [PATCH 06/84] feature #2858. Clear and to_xml on streams for VectorAttributes --- include/AddressRange.h | 29 +++++++- include/Attribute.h | 13 ++++ src/common/Attribute.cc | 41 ++++++----- src/vnm/AddressRange.cc | 158 +++++++++++++++++++++++++++++++++------- 4 files changed, 197 insertions(+), 44 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index ace2eed5b0..46ddf9bca0 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -72,6 +72,17 @@ public: */ int from_vattr(VectorAttribute * attr, string& error_msg); + /** + * Builds an Address Range from a vector attribute stored in the DB + * @param vattr the VectorAttribute stored in a ADDRESS_RANGE template + */ + int from_vattr_db(VectorAttribute *vattr); + + /** + * Builds an extended XML representation of the AR to send it back to clients + */ + string& to_xml(string& str); + /** * Type of Addresses defined by this address range */ @@ -164,7 +175,6 @@ private: /* ---------------------------------------------------------------------- */ /* NIC setup functions */ /* ---------------------------------------------------------------------- */ - /** * Writes MAC address to the given NIC attribute * @param addr_index internal index for the lease @@ -198,13 +208,30 @@ private: /* Address index map helper functions */ /* ---------------------------------------------------------------------- */ + /** + * This function generates a string representation of the in-memory allocated + * addresses. It'll be stored along side the AR vector attribute in the + * ADDRESS_RANGE template. + */ void allocated_to_attr(); + /** + * Generates a memory map for the addresses. + * @param allocated_s the string representation of the allocated addresses + * generated by allocated_to_attr() + * @return 0 on success + */ int attr_to_allocated(const string& allocated_s); + /** + * Adds a new allocated address to the map. Updates the ALLOCATED attribute + */ void allocate_addr(PoolObjectSQL::ObjectType ot, int obid, unsigned int addr_index); + /** + * Frees an address from the map. Updates the ALLOCATED attribute + */ void free_addr(unsigned int addr_index); /* ---------------------------------------------------------------------- */ diff --git a/include/Attribute.h b/include/Attribute.h index a65b7a828e..23ad9af95d 100644 --- a/include/Attribute.h +++ b/include/Attribute.h @@ -339,6 +339,11 @@ public: */ string * to_xml() const; + /** + * Same as above but the attribute is written in an string stream; + */ + void to_xml(ostringstream &oss) const; + /** * Builds a new attribute from a string of the form: * "VAL_NAME_1=VAL_VALUE_1,...,VAL_NAME_N=VAL_VALUE_N". @@ -442,6 +447,14 @@ public: return new VectorAttribute(*this); }; + /** + * Clear the vector attribute values + */ + void clear() + { + attribute_value.clear(); + } + private: static const char * magic_sep; diff --git a/src/common/Attribute.cc b/src/common/Attribute.cc index dcc0cd3837..ba26c33992 100644 --- a/src/common/Attribute.cc +++ b/src/common/Attribute.cc @@ -68,14 +68,28 @@ string * VectorAttribute::marshall(const char * _sep) const string * VectorAttribute::to_xml() const { - map::const_iterator it; - ostringstream oss; - string * xml; + ostringstream oss; - oss << "<" << name() << ">"; + to_xml(oss); - for (it=attribute_value.begin();it!=attribute_value.end();it++) - { + string * xml = new string; + + *xml = oss.str(); + + return xml; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void VectorAttribute::to_xml(ostringstream &oss) const +{ + map::const_iterator it; + + oss << "<" << name() << ">"; + + for (it=attribute_value.begin();it!=attribute_value.end();it++) + { if ( it->first.empty() ) { continue; @@ -87,21 +101,14 @@ string * VectorAttribute::to_xml() const } else { - oss << "<" << it->first << ">second - << "]]>first << ">"; + oss << "<" << it->first << ">second + << "]]>first << ">"; } - } + } - oss << ""; - - xml = new string; - - *xml = oss.str(); - - return xml; + oss << ""; } - /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index d198048966..98d2159323 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -69,13 +69,15 @@ AddressRange::AddressType AddressRange::str_to_type(string& str_type) /* ************************************************************************** */ /* ************************************************************************** */ -int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) +int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) { string value; + attr = vattr; + /* ------------------------- AR Type & Size ---------------------------- */ - value = attr->vector_value("TYPE"); + value = vattr->vector_value("TYPE"); type = str_to_type(value); if (type == NONE) @@ -84,7 +86,7 @@ int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) return -1; } - if ( attr->vector_value("SIZE", size) != 0 || size <= 0 ) + if ( vattr->vector_value("SIZE", size) != 0 || size <= 0 ) { error_msg = "Wrong SIZE for address range"; return -1; @@ -94,7 +96,7 @@ int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) bool do_mac = false; - value = attr->vector_value("MAC"); + value = vattr->vector_value("MAC"); if (value.empty()) { @@ -125,7 +127,7 @@ int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) case IP4: case IP4_6: - value = attr->vector_value("IP"); + value = vattr->vector_value("IP"); if (value.empty() || ip_to_i(value, ip) == -1) { @@ -145,25 +147,17 @@ int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) /* -------------------------- IP6 prefixes ------------------------------ */ - value = attr->vector_value("GLOBAL_PREFIX"); + value = vattr->vector_value("GLOBAL_PREFIX"); - if (value.empty()) - { - global6[1]=global6[0]=0; - } - else if (prefix6_to_i(value, global6) != 0 ) + if (prefix6_to_i(value, global6) != 0 ) { error_msg = "Wrong format for IP6 global address prefix"; return -1; } - value = attr->vector_value("ULA_PREFIX"); + value = vattr->vector_value("ULA_PREFIX"); - if (value.empty()) - { - ula6[1]=ula6[0]=0; - } - else if (prefix6_to_i(value, ula6) != 0 ) + if (prefix6_to_i(value, ula6) != 0 ) { error_msg = "Wrong format for IP6 unique local address prefix"; return -1; @@ -171,9 +165,7 @@ int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) /* ------------------------ AR Internal Data ---------------------------- */ - this->attr = attr; - - attr->replace("AR_ID", id); + vattr->replace("AR_ID", id); if (do_mac) //Need to add MAC_START to the attribute { @@ -183,6 +175,116 @@ int AddressRange::from_vattr(VectorAttribute *attr, string& error_msg) return 0; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::from_vattr_db(VectorAttribute *vattr) +{ + string value; + int rc = 0; + + this->attr = vattr; + + rc += vattr->vector_value("AR_ID", id); + + value = vattr->vector_value("TYPE"); + type = str_to_type(value); + + rc += vattr->vector_value("SIZE", size); + + rc += mac_to_i(vattr->vector_value("MAC"), mac); + + if (type & 0x00000002) + { + rc += ip_to_i(vattr->vector_value("IP"), ip); + } + + rc += prefix6_to_i(vattr->vector_value("GLOBAL_PREFIX"), global6); + + rc += prefix6_to_i(vattr->vector_value("ULA_PREFIX"), ula6); + + rc += attr_to_allocated(vattr->vector_value("ALLOCATED")); + + if (type == NONE) + { + rc = -1; + } + + return rc; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string& AddressRange::to_xml(string& str) +{ + const map& ar_attrs = attr->value(); + map::const_iterator it; + + ostringstream oss; + + oss << ""; + + for (it=ar_attrs.begin(); it != ar_attrs.end(); it++) + { + if ( it->first == "ALLOCATED" ) + { + continue; + } + + oss << "<" << it->first << ">second + << "]]>first << ">"; + } + + if (allocated.empty()) + { + oss << ""; + } + else + { + map::const_iterator it; + VectorAttribute lease("LEASE"); + + oss << ""; + + for (it = allocated.begin(); it != allocated.end(); it++) + { + lease.clear(); + + set_mac(it->first, &lease); + + if (type & 0x00000002 ) + { + set_ip(it->first, &lease); + } + + if (type & 0x00000004) + { + set_ip6(it->first, &lease); + } + + if (it->second & PoolObjectSQL::VM) + { + lease.replace("VM", it->second && 0x00000000FFFFFFFFLL); + } + else if (it->second & PoolObjectSQL::NET) + { + lease.replace("VNET", it->second && 0x00000000FFFFFFFFLL); + } + + lease.to_xml(oss); + } + + oss << ""; + } + + oss << ""; + + str = oss.str(); + + return str; +} + /* ************************************************************************** */ /* ************************************************************************** */ @@ -507,6 +609,8 @@ void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, { allocated.insert(make_pair(addr_index,ot|obid)); + + allocated_to_attr(); } /* -------------------------------------------------------------------------- */ @@ -514,6 +618,8 @@ void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, void AddressRange::free_addr(unsigned int addr_index) { allocated.erase(addr_index); + + allocated_to_attr(); } /* ************************************************************************** */ @@ -533,12 +639,12 @@ int AddressRange::allocate_addr( { set_mac(next, nic); - if (type && 0x00000002 ) + if (type & 0x00000002 ) { set_ip(next, nic); } - if (type && 0x00000004) + if (type & 0x00000004) { set_ip6(next, nic); } @@ -583,12 +689,12 @@ int AddressRange::allocate_by_mac( set_mac(index, nic); - if (type && 0x00000002 ) + if (type & 0x00000002 ) { set_ip(index, nic); } - if (type && 0x00000004) + if (type & 0x00000004) { set_ip6(index, nic); } @@ -631,12 +737,12 @@ int AddressRange::allocate_by_ip( set_mac(index, nic); - if (type && 0x00000002 ) + if (type & 0x00000002 ) { set_ip(index, nic); } - if (type && 0x00000004) + if (type & 0x00000004) { set_ip6(index, nic); } From c24dfec31a2fcfca5e1379498cfbbe66eb580d8d Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 9 May 2014 21:20:09 +0200 Subject: [PATCH 07/84] feature #2858: First version of AddressRangePool --- include/AddressRange.h | 8 ++ include/AddressRangePool.h | 62 ++++++++++++++ src/rm/RequestManagerVirtualMachine.cc | 4 - src/vnm/AddressRangePool.cc | 113 +++++++++++++++++++++++++ src/vnm/SConstruct | 3 +- 5 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 include/AddressRangePool.h create mode 100644 src/vnm/AddressRangePool.cc diff --git a/include/AddressRange.h b/include/AddressRange.h index 46ddf9bca0..bb2951fdec 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -146,6 +146,14 @@ public: */ void free_addr(const string& mac); + /** + * Return the id for this address range + */ + unsigned int ar_id() + { + return id; + } + private: /* ---------------------------------------------------------------------- */ /* String to binary conversion functions for different address types */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h new file mode 100644 index 0000000000..84d8c452d4 --- /dev/null +++ b/include/AddressRangePool.h @@ -0,0 +1,62 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#ifndef ADDRESS_RANGE_POOL_H_ +#define ADDRESS_RANGE_POOL_H_ + +#include +#include +#include + +#include + +#include "Template.h" +#include "AddressRange.h" + +using namespace std; + +class AddressRangePool +{ + AddressRangePool(); + + virtual ~AddressRangePool(); + + /** + * Builds the address range set from an array of VectorAttributes. This + * function is used to create address ranges. + * @param ars the vector of address ranges + * @param error_msg describing the error + * @return 0 on success + */ + int from_vattr(vector ars, string& error_msg); + + /** + * Builds the address range set from its XML representation. This function + * is used to rebuild the address ranges from the DB. + * @param node xmlNode for the template + * @return 0 on success + */ + int from_xml_node(const xmlNodePtr node); + +private: + Template ar_template; + + unsigned int next_ar; + + map ar_pool; +}; + +#endif diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 3878b1a3e7..36bbdaddad 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -775,10 +775,6 @@ void VirtualMachineDeploy::request_execute(xmlrpc_c::paramList const& paramList, } } - - - - // ------------------------------------------------------------------------ // Add a new history record and deploy the VM // ------------------------------------------------------------------------ diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc new file mode 100644 index 0000000000..1612b1472e --- /dev/null +++ b/src/vnm/AddressRangePool.cc @@ -0,0 +1,113 @@ +/* -------------------------------------------------------------------------- */ +/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ +/* */ +/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ +/* not use this file except in compliance with the License. You may obtain */ +/* a copy of the License at */ +/* */ +/* http://www.apache.org/licenses/LICENSE-2.0 */ +/* */ +/* Unless required by applicable law or agreed to in writing, software */ +/* distributed under the License is distributed on an "AS IS" BASIS, */ +/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +/* See the License for the specific language governing permissions and */ +/* limitations under the License. */ +/* -------------------------------------------------------------------------- */ + +#include "AddressRangePool.h" + +using namespace std; + +AddressRangePool::AddressRangePool():ar_template(false,'=',"AR_POOL"),next_ar(0) +{}; + +AddressRangePool::~AddressRangePool() +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + delete it->second; + } +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::from_vattr(vector ars, string& error_msg) +{ + vector::iterator it; + + /* -------------------- Init the AR pool ---------------------------- */ + + for (it = ars.begin(); it != ars.end(); it++) + { + VectorAttribute * va = static_cast(*it); + + if (va == 0) + { + error_msg = "Wrong AR format"; + return -1; + } + + AddressRange * ar = new AddressRange(next_ar); + + if (ar->from_vattr(va, error_msg) != 0) + { + delete ar; + return -1; + } + + ar_pool.insert(make_pair(next_ar++, ar)); + } + + /* ---------------------- Template ---------------------------------- */ + + for (it = ars.begin(); it != ars.end(); it++) + { + ar_template.set(*it); + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::from_xml_node(const xmlNodePtr node) +{ + int rc = ar_template.from_xml_node(node); + + if (rc != 0) + { + return -1; + } + + vector var; + + int num_ar = ar_template.get("AR", var); + + for (int i = 0; i < num_ar; i++) + { + VectorAttribute * va = static_cast(var[i]); + + AddressRange * ar = new AddressRange(0); + + if (ar->from_vattr_db(va)!= 0) + { + return -1; + } + + ar_pool.insert(make_pair(ar->ar_id(), ar)); + + ar_template.set(va); + + if (ar->ar_id() > next_ar) + { + next_ar = ar->ar_id() + 1; + } + } + + return 0; +} + diff --git a/src/vnm/SConstruct b/src/vnm/SConstruct index f3466b8e82..eb586a69df 100644 --- a/src/vnm/SConstruct +++ b/src/vnm/SConstruct @@ -27,7 +27,8 @@ source_files=[ 'RangedLeases.cc', 'VirtualNetwork.cc', 'VirtualNetworkPool.cc', - 'AddressRange.cc' + 'AddressRange.cc', + 'AddressRangePool.cc' ] # Build library From e4cee7c1ea94d1f6b69e1e8d379f538bca984ff8 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 10 May 2014 01:22:02 +0200 Subject: [PATCH 08/84] feature #2858: Integrate AddressRangePool with VirtualNetwork: persistency & initialization --- include/AddressRange.h | 18 +- include/AddressRangePool.h | 3 + include/PoolObjectSQL.h | 14 ++ include/VirtualNetwork.h | 105 ++------- src/vnm/AddressRange.cc | 28 +-- src/vnm/AddressRangePool.cc | 30 ++- src/vnm/VirtualNetwork.cc | 447 ++++++------------------------------ 7 files changed, 159 insertions(+), 486 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index bb2951fdec..423b6bad77 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -81,7 +81,7 @@ public: /** * Builds an extended XML representation of the AR to send it back to clients */ - string& to_xml(string& str); + void to_xml(ostringstream &oss) const; /** * Type of Addresses defined by this address range @@ -164,21 +164,21 @@ private: * @param mac in string form 00:02:01:02:03:04 * @return 0 on success */ - int mac_to_i(string mac, unsigned int i_mac[]); + int mac_to_i(string mac, unsigned int i_mac[]) const; /** * IP version 4 to binary (32 bits) * @param ip in string form 192.168.0.2 * @return 0 on success */ - int ip_to_i(const string& _ip, unsigned int& i_ip); + int ip_to_i(const string& _ip, unsigned int& i_ip) const; /** * IPv6 64bits prefix conversion * @param prefix in string form 2a00:1bc0:b001:A:: * @return 0 on success */ - int prefix6_to_i(const string& prefix, unsigned int ip[]); + int prefix6_to_i(const string& prefix, unsigned int ip[]) const; /* ---------------------------------------------------------------------- */ /* NIC setup functions */ @@ -188,21 +188,21 @@ private: * @param addr_index internal index for the lease * @param nic attribute of a VMTemplate */ - void set_mac(unsigned int addr_index, VectorAttribute * nic); + void set_mac(unsigned int addr_index, VectorAttribute * nic) const; /** * Writes IP address to the given NIC attribute * @param addr_index internal index for the lease * @param nic attribute of a VMTemplate */ - void set_ip(unsigned int addr_index, VectorAttribute * nic); + void set_ip(unsigned int addr_index, VectorAttribute * nic) const; /** * Writes IPv6 address to the given NIC attribute * @param addr_index internal index for the lease * @param nic attribute of a VMTemplate */ - void set_ip6(unsigned int addr_index, VectorAttribute * nic); + void set_ip6(unsigned int addr_index, VectorAttribute * nic) const; /** * Writes VNET configuration attributes to the given NIC attribute. It @@ -210,7 +210,7 @@ private: * @param addr_index internal index for the lease * @param nic attribute of a VMTemplate */ - void set_vnet(VectorAttribute *nic, const vector &inherit); + void set_vnet(VectorAttribute *nic, const vector &inherit) const; /* ---------------------------------------------------------------------- */ /* Address index map helper functions */ @@ -245,7 +245,7 @@ private: /* ---------------------------------------------------------------------- */ /* Restricted Attributes functions */ /* ---------------------------------------------------------------------- */ - bool check(string& rs_attr); + bool check(string& rs_attr) const; static void set_restricted_attributes(vector& rattrs); diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 84d8c452d4..ca426275b3 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -30,6 +30,7 @@ using namespace std; class AddressRangePool { +public: AddressRangePool(); virtual ~AddressRangePool(); @@ -51,6 +52,8 @@ class AddressRangePool */ int from_xml_node(const xmlNodePtr node); + string& to_xml(string& sstream, bool extended) const; + private: Template ar_template; diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 95df3c94fa..f51fd7e7d3 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -475,6 +475,20 @@ public: return obj_template->replace(name, value); } + /** + * Removes an attribute from the template. The attributes are returned, and + * MUST be freed by the calling funtion + * @param name of the attribute + * @param values a vector containing a pointer to the attributes + * @return the number of attributes removed + */ + int remove_template_attribute( + const string& name, + vector& values) + { + return obj_template->remove(name, values); + } + /** * Generates a XML string for the template of the Object * @param xml the string to store the XML description. diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index c1cd5e08c2..8e38fb13bd 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -22,6 +22,7 @@ #include "Leases.h" #include "VirtualNetworkTemplate.h" #include "Clusterable.h" +#include "AddressRangePool.h" #include #include @@ -44,17 +45,6 @@ class VirtualNetwork : public PoolObjectSQL, public Clusterable { public: - /** - * Possible types of networks - */ - - enum NetworkType - { - UNINITIALIZED = -1, - RANGED = 0, - FIXED = 1 - }; - // ************************************************************************* // Virtual Network Public Methods // ************************************************************************* @@ -117,10 +107,14 @@ public: */ int get_lease(int vid, string& _ip, string& _mac, string& _bridge) { + return 0; + //TODO + /* unsigned int eui64[2]; _bridge = bridge; return leases->get(vid, _ip, _mac, eui64); + */ }; /** @@ -133,10 +127,15 @@ public: */ int set_lease(int vid, const string& _ip, string& _mac, string& _bridge) { + //TODO + /* unsigned int eui64[2]; _bridge = bridge; return leases->set(vid, _ip, _mac, eui64); + */ + + return 0; }; /** @@ -146,7 +145,8 @@ public: */ void release_lease(const string& ip) { - return leases->release(ip); + //TODO + //return leases->release(ip); }; /** @@ -157,7 +157,9 @@ public: */ bool is_owner (const string& ip, int vid) { - return leases->is_owner(ip, vid); + return true; + //TODO + //return leases->is_owner(ip, vid); }; /** @@ -166,7 +168,9 @@ public: */ unsigned int get_size() { - return leases->size; + //TODO + return 0; + //return leases->size; }; /** @@ -175,7 +179,9 @@ public: */ unsigned int get_used() { - return leases->n_used; + //TODO + return 0; + //return leases->n_used; }; /** @@ -262,41 +268,9 @@ private: int vlan; /** - * IPv6 address global unicast prefix + * The Address Range Pool */ - string global; - - /** - * Binary representation of the IPv6 address global unicast prefix - */ - unsigned int global_bin[2]; - - /** - * IPv6 address site unicast prefix - */ - string site; - - /** - * Binary representation of the IPv6 address site unicast prefix - */ - unsigned int site_bin[2]; - - // ------------------------------------------------------------------------- - // Virtual Network Description - // ------------------------------------------------------------------------- - /** - * Holds the type of this network - */ - NetworkType type; - - /** - * Pointer to leases class, can be fixed or ranged. - * Holds information on given (and, optionally, possible) leases - */ - Leases * leases; - - unsigned int ip_start; - unsigned int ip_end; + AddressRangePool ar_pool; // ************************************************************************* // DataBase implementation (Private) @@ -370,30 +344,6 @@ private: static const char * db_bootstrap; - /** - * Reads the Virtual Network (identified with its OID) from the database. - * @param db pointer to the db - * @return 0 on success - */ - int select(SqlDB * db); - - /** - * Reads the Virtual Network (identified with its OID) from the database. - * @param db pointer to the db - * @param name of the network - * @param uid of the owner - * - * @return 0 on success - */ - int select(SqlDB * db, const string& name, int uid); - - /** - * Reads the Virtual Network leases from the database. - * @param db pointer to the db - * @return 0 on success - */ - int select_leases(SqlDB * db); - /** * Writes the Virtual Network and its associated template and leases in the database. * @param db pointer to the db @@ -411,15 +361,6 @@ private: string error_str; return insert_replace(db, true, error_str); } - - /** - * Deletes a VNW from the database and all its associated information: - * - VNW template - * - given leases - * @param db pointer to the db - * @return 0 on success - */ - int drop(SqlDB * db); }; #endif /*VIRTUAL_NETWORK_H_*/ diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 98d2159323..0810c244c4 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -120,8 +120,8 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) { srand(time(0)); - mac[0] = rand() && 0x0000FFFF; - mac[0]+= (rand()<<16) && 0xFFFF0000; + mac[0] = rand() & 0x0000FFFF; + mac[0]+= (rand()<<16) & 0xFFFF0000; } break; @@ -216,13 +216,11 @@ int AddressRange::from_vattr_db(VectorAttribute *vattr) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -string& AddressRange::to_xml(string& str) +void AddressRange::to_xml(ostringstream &oss) const { const map& ar_attrs = attr->value(); map::const_iterator it; - ostringstream oss; - oss << ""; for (it=ar_attrs.begin(); it != ar_attrs.end(); it++) @@ -279,16 +277,12 @@ string& AddressRange::to_xml(string& str) } oss << ""; - - str = oss.str(); - - return str; } /* ************************************************************************** */ /* ************************************************************************** */ -int AddressRange::mac_to_i(string mac, unsigned int i_mac[]) +int AddressRange::mac_to_i(string mac, unsigned int i_mac[]) const { istringstream iss; @@ -331,7 +325,7 @@ int AddressRange::mac_to_i(string mac, unsigned int i_mac[]) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int AddressRange::ip_to_i(const string& _ip, unsigned int& i_ip) +int AddressRange::ip_to_i(const string& _ip, unsigned int& i_ip) const { istringstream iss; size_t pos=0; @@ -374,7 +368,7 @@ int AddressRange::ip_to_i(const string& _ip, unsigned int& i_ip) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int AddressRange::prefix6_to_i(const string& prefix, unsigned int ip[]) +int AddressRange::prefix6_to_i(const string& prefix, unsigned int ip[]) const { struct in6_addr s6; @@ -400,7 +394,7 @@ int AddressRange::prefix6_to_i(const string& prefix, unsigned int ip[]) /* ************************************************************************** */ /* ************************************************************************** */ -void AddressRange::set_mac(unsigned int addr_index, VectorAttribute * nic) +void AddressRange::set_mac(unsigned int addr_index, VectorAttribute * nic) const { ostringstream oss; unsigned int temp_byte; @@ -436,7 +430,7 @@ void AddressRange::set_mac(unsigned int addr_index, VectorAttribute * nic) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRange::set_ip(unsigned int addr_index, VectorAttribute * nic) +void AddressRange::set_ip(unsigned int addr_index, VectorAttribute * nic) const { ostringstream oss; unsigned int temp_byte; @@ -461,7 +455,7 @@ void AddressRange::set_ip(unsigned int addr_index, VectorAttribute * nic) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) +void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) const { unsigned int eui64[2]; unsigned int mlow = mac[0] + addr_index; @@ -510,7 +504,7 @@ void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRange::set_vnet(VectorAttribute *nic, const vector &inherit) +void AddressRange::set_vnet(VectorAttribute *nic, const vector &inherit) const { nic->replace("AR_ID", id); @@ -777,7 +771,7 @@ bool AddressRange::restricted_set = false; set AddressRange::restricted_attributes; -bool AddressRange::check(string& rs_attr) +bool AddressRange::check(string& rs_attr) const { if (!restricted_set) { diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 1612b1472e..a370616f4a 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -100,8 +100,6 @@ int AddressRangePool::from_xml_node(const xmlNodePtr node) ar_pool.insert(make_pair(ar->ar_id(), ar)); - ar_template.set(va); - if (ar->ar_id() > next_ar) { next_ar = ar->ar_id() + 1; @@ -111,3 +109,31 @@ int AddressRangePool::from_xml_node(const xmlNodePtr node) return 0; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +string& AddressRangePool::to_xml(string& sstream, bool extended) const +{ + if (extended) + { + ostringstream oss; + map::const_iterator it; + + oss << ""; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + it->second->to_xml(oss); + } + + oss << ""; + + sstream = oss.str(); + + return sstream; + } + + return ar_template.to_xml(sstream); +} + + diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 90f61e3d24..a014c0458b 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -44,9 +44,7 @@ VirtualNetwork::VirtualNetwork(int _uid, VirtualNetworkTemplate * _vn_template): PoolObjectSQL(-1,NET,"",_uid,_gid,_uname,_gname,table), Clusterable(_cluster_id, _cluster_name), - bridge(""), - type(UNINITIALIZED), - leases(0) + bridge("") { if (_vn_template != 0) { @@ -58,9 +56,6 @@ VirtualNetwork::VirtualNetwork(int _uid, } set_umask(_umask); - - global_bin[0] = global_bin[1] = 0; - site_bin[0] = site_bin[1] = 0; }; /* -------------------------------------------------------------------------- */ @@ -68,7 +63,6 @@ VirtualNetwork::VirtualNetwork(int _uid, VirtualNetwork::~VirtualNetwork() { - delete leases; delete obj_template; } @@ -76,7 +70,7 @@ VirtualNetwork::~VirtualNetwork() /* Virtual Network :: Database Access Functions */ /* ************************************************************************** */ -const char * VirtualNetwork::table = "network_pool"; +const char * VirtualNetwork::table = "network_pool"; const char * VirtualNetwork::db_names = "oid, name, body, uid, gid, owner_u, group_u, other_u, cid"; @@ -90,132 +84,17 @@ const char * VirtualNetwork::db_bootstrap = "CREATE TABLE IF NOT EXISTS" /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetwork::select(SqlDB * db) -{ - int rc; - - rc = PoolObjectSQL::select(db); - - if ( rc != 0 ) - { - return rc; - } - - return select_leases(db); -} - -/* -------------------------------------------------------------------------- */ - -int VirtualNetwork::select(SqlDB * db, const string& name, int uid) -{ - int rc; - - rc = PoolObjectSQL::select(db,name,uid); - - if ( rc != 0 ) - { - return rc; - } - - return select_leases(db); -} - -/* -------------------------------------------------------------------------- */ - -int VirtualNetwork::select_leases(SqlDB * db) -{ - ostringstream oss; - ostringstream ose; - - string network_address; - - unsigned int mac_prefix = VirtualNetworkPool::mac_prefix(); - - //Get the leases - if (type == RANGED) - { - leases = new RangedLeases(db, - oid, - mac_prefix, - global_bin, - site_bin, - ip_start, - ip_end); - } - else if(type == FIXED) - { - leases = new FixedLeases(db, - oid, - mac_prefix, - global_bin, - site_bin); - } - else - { - goto error_type; - } - - if (leases == 0) - { - goto error_leases; - } - - return leases->select(db); - - -error_leases: - ose << "Error getting Virtual Network leases nid: " << oid; - goto error_common; - -error_type: - ose << "Wrong type of Virtual Network: " << type; - goto error_common; - -error_common: - NebulaLog::log("VNM", Log::ERROR, ose); - return -1; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - int VirtualNetwork::insert(SqlDB * db, string& error_str) { - ostringstream ose; - int rc; + vector ars; + ostringstream ose; bool b_vlan; - string s_type; - string ranged_error_str; - - unsigned int mac_prefix = VirtualNetworkPool::mac_prefix(); //-------------------------------------------------------------------------- // VirtualNetwork Attributes from the template //-------------------------------------------------------------------------- - // ------------ TYPE ---------------------- - erase_template_attribute("TYPE",s_type); - - TO_UPPER(s_type); - - if (s_type == "RANGED") - { - type = VirtualNetwork::RANGED; - } - else if ( s_type == "FIXED") - { - type = VirtualNetwork::FIXED; - } - else if ( s_type.empty() ) - { - goto error_type_defined; - } - else - { - goto error_wrong_type; - } - // ------------ NAME ---------------------- erase_template_attribute("NAME",name); @@ -283,87 +162,28 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) add_template_attribute("BRIDGE", bridge); - // ------------ IP6 PREFIX --------------- - - erase_template_attribute("GLOBAL_PREFIX", global); - - if (Leases::Lease::prefix6_to_number(global, global_bin) != 0) - { - goto error_global; - } - - erase_template_attribute("SITE_PREFIX", site); - - if (Leases::Lease::prefix6_to_number(site, site_bin) != 0) - { - goto error_site; - } - //-------------------------------------------------------------------------- - // Get the leases + // Get the Address Ranges //-------------------------------------------------------------------------- - if (type == VirtualNetwork::RANGED) + + remove_template_attribute("AR", ars); + + if (ar_pool.from_vattr(ars, error_str) != 0) { - - int rc; - - rc = RangedLeases::process_template(this, ip_start, ip_end, - ranged_error_str); - - if ( rc != 0 ) - { - goto error_ranged; - } - - leases = new RangedLeases(db, - oid, - mac_prefix, - global_bin, - site_bin, - ip_start, - ip_end); - } - else // VirtualNetwork::FIXED - { - vector vector_leases; - - get_template_attribute("LEASES",vector_leases); - - leases = new FixedLeases(db, - oid, - mac_prefix, - global_bin, - site_bin, - vector_leases); - - remove_template_attribute("LEASES"); - } - - if (leases == 0) - { - goto error_null_leases; + goto error_ar; } //-------------------------------------------------------------------------- // Insert the Virtual Network //-------------------------------------------------------------------------- - rc = insert_replace(db, false, error_str); - if ( rc != 0 ) + if ( insert_replace(db, false, error_str) != 0 ) { - goto error_update; + goto error_db; } return 0; -error_type_defined: - ose << "No TYPE in template for Virtual Network."; - goto error_common; - -error_wrong_type: - ose << "Wrong type \""<< s_type <<"\" in template for Virtual Network."; - goto error_common; - error_name: ose << "No NAME in template for Virtual Network."; goto error_common; @@ -372,26 +192,9 @@ error_bridge: ose << "No BRIDGE in template for Virtual Network."; goto error_common; -error_update: +error_db: +error_ar: ose << error_str; - goto error_common; - -error_ranged: - ose << ranged_error_str; - goto error_common; - -error_global: - ose << global - << " does not contain a character string representing a valid prefix"; - goto error_common; - -error_site: - ose << site - << " does not contain a character string representing a valid prefix"; - goto error_common; - -error_null_leases: - ose << "Error getting Virtual Network leases."; error_common: error_str = ose.str(); @@ -477,7 +280,6 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace, string& error_str) ostringstream oss; int rc; - string xml_body; char * sql_name; @@ -552,23 +354,6 @@ error_common: return -1; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int VirtualNetwork::drop(SqlDB * db) -{ - int rc; - - rc = PoolObjectSQL::drop(db); - - if ( rc == 0 && leases != 0 ) - { - rc += leases->drop(db); - } - - return rc; -} - /* ************************************************************************** */ /* Virtual Network :: Misc */ /* ************************************************************************** */ @@ -617,18 +402,17 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const os << "" << - "" << oid << "" << - "" << uid << "" << - "" << gid << "" << - "" << uname << "" << - "" << gname << "" << - "" << name << "" << - perms_to_xml(perm_str) << - "" << cluster_id << "" << - "" << cluster << "" << - "" << type << "" << - "" << bridge << ""<< - "" << vlan << ""; + "" << oid << "" << + "" << uid << "" << + "" << gid << "" << + "" << uname << "" << + "" << gname << "" << + "" << name << "" << + perms_to_xml(perm_str) << + ""<< cluster_id<< ""<< + "" << cluster << "" << + "" << bridge << "" << + "" << vlan << ""; if (!phydev.empty()) { @@ -648,46 +432,9 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const os << ""; } - if (!global.empty()) - { - os << "" << global << ""; - } - else - { - os << ""; - } + os << obj_template->to_xml(template_xml); - if (!site.empty()) - { - os << "" << site << ""; - } - else - { - os << ""; - } - - if ( type == RANGED ) - { - string st_ip_start; - string st_ip_end; - - Leases::Lease::ip_to_string(ip_start, st_ip_start); - Leases::Lease::ip_to_string(ip_end, st_ip_end); - - os << - "" << - "" << st_ip_start << "" << - "" << st_ip_end << "" << - ""; - } - - os << ""<< leases->n_used << ""<< - obj_template->to_xml(template_xml); - - if (extended) - { - os << "" << leases->to_xml(leases_xml) << ""; - } + os << ar_pool.to_xml(leases_xml, extended); os << ""; @@ -704,40 +451,29 @@ int VirtualNetwork::from_xml(const string &xml_str) vector content; int rc = 0; - int int_type; // Initialize the internal XML object update_from_str(xml_str); // Get class base attributes - rc += xpath(oid, "/VNET/ID", -1); - rc += xpath(uid, "/VNET/UID", -1); - rc += xpath(gid, "/VNET/GID", -1); - rc += xpath(uname, "/VNET/UNAME", "not_found"); - rc += xpath(gname, "/VNET/GNAME", "not_found"); - rc += xpath(name, "/VNET/NAME", "not_found"); - rc += xpath(int_type, "/VNET/TYPE", -1); - rc += xpath(bridge, "/VNET/BRIDGE", "not_found"); - rc += xpath(vlan, "/VNET/VLAN", 0); - - rc += xpath(cluster_id, "/VNET/CLUSTER_ID", -1); - rc += xpath(cluster, "/VNET/CLUSTER", "not_found"); + rc += xpath(oid, "/VNET/ID", -1); + rc += xpath(uid, "/VNET/UID", -1); + rc += xpath(gid, "/VNET/GID", -1); + rc += xpath(uname, "/VNET/UNAME", "not_found"); + rc += xpath(gname, "/VNET/GNAME", "not_found"); + rc += xpath(name, "/VNET/NAME", "not_found"); + rc += xpath(bridge, "/VNET/BRIDGE", "not_found"); + rc += xpath(vlan, "/VNET/VLAN", 0); + rc += xpath(cluster_id,"/VNET/CLUSTER_ID", -1); + rc += xpath(cluster, "/VNET/CLUSTER", "not_found"); // Permissions rc += perms_from_xml(); - xpath(phydev, "/VNET/PHYDEV", ""); - xpath(vlan_id, "/VNET/VLAN_ID",""); + xpath(phydev, "/VNET/PHYDEV", ""); + xpath(vlan_id,"/VNET/VLAN_ID",""); - xpath(global, "/VNET/GLOBAL_PREFIX", ""); - xpath(site, "/VNET/SITE_PREFIX",""); - - Leases::Lease::prefix6_to_number(site, site_bin); - Leases::Lease::prefix6_to_number(global, global_bin); - - type = static_cast(int_type); - - // Get associated classes + // Virtual Network template ObjectXML::get_nodes("/VNET/TEMPLATE", content); if (content.empty()) @@ -745,24 +481,24 @@ int VirtualNetwork::from_xml(const string &xml_str) return -1; } - // Virtual Network template rc += obj_template->from_xml_node(content[0]); ObjectXML::free_nodes(content); + content.clear(); - // Ranged Leases - if (type == RANGED) + // Address Range Pool + ObjectXML::get_nodes("/VNET/AR_POOL", content); + + if (content.empty()) { - string st_ip_start; - string st_ip_end; - - rc += xpath(st_ip_start, "/VNET/RANGE/IP_START", "0"); - rc += xpath(st_ip_end, "/VNET/RANGE/IP_END", "0"); - - Leases::Lease::ip_to_number(st_ip_start, ip_start); - Leases::Lease::ip_to_number(st_ip_end, ip_end); + return -1; } + // Virtual Network template + rc += ar_pool.from_xml_node(content[0]); + + ObjectXML::free_nodes(content); + if (rc != 0) { return -1; @@ -779,65 +515,16 @@ int VirtualNetwork::nic_attribute( int vid, const vector& inherit_attrs) { - int rc; - - string ip; - string mac; - string inherit_val; - - unsigned int eui64[2]; - unsigned int prefix[2] = {0, 0}; - - ostringstream oss; - + string inherit_val; vector::const_iterator it; - ip = nic->vector_value("IP"); - oss << oid; - //-------------------------------------------------------------------------- - // GET NETWORK LEASE - //-------------------------------------------------------------------------- - - if (ip.empty()) - { - rc = leases->get(vid, ip, mac, eui64); - } - else - { - rc = leases->set(vid,ip,mac, eui64); - } - - if ( rc != 0 ) - { - return -1; - } - - //-------------------------------------------------------------------------- - // NEW NIC ATTRIBUTES + // Set default values from the Virtual Network //-------------------------------------------------------------------------- nic->replace("NETWORK", name); - nic->replace("NETWORK_ID",oss.str()); + nic->replace("NETWORK_ID", oid); nic->replace("BRIDGE", bridge); - nic->replace("MAC", mac); - nic->replace("IP", ip); - - Leases::Lease::ip6_to_string(eui64, prefix, ip); - nic->replace("IP6_LINK", ip); - - if (!global.empty()) - { - Leases::Lease::ip6_to_string(eui64, global_bin, ip); - nic->replace("IP6_GLOBAL", ip); - } - - if (!site.empty()) - { - Leases::Lease::ip6_to_string(eui64, site_bin, ip); - nic->replace("IP6_SITE", ip); - } - if ( vlan == 1 ) { @@ -860,10 +547,7 @@ int VirtualNetwork::nic_attribute( if ( get_cluster_id() != ClusterPool::NONE_CLUSTER_ID ) { - oss.str(""); - oss << get_cluster_id(); - - nic->replace("CLUSTER_ID", oss.str()); + nic->replace("CLUSTER_ID", get_cluster_id()); } for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++) @@ -876,6 +560,10 @@ int VirtualNetwork::nic_attribute( } } + //GET THE IP/MAC... AND OVERWRITE FROM AR. + //string ip = nic->vector_value("IP"); + //string mac = nic->vector_value("MAC"); + return 0; } @@ -889,7 +577,9 @@ int VirtualNetwork::add_leases(VirtualNetworkTemplate * leases_template, leases_template->get("LEASES", vector_leases); - return leases->add_leases(vector_leases, error_msg); + //TODO + return 0; + //return leases->add_leases(vector_leases, error_msg); } /* -------------------------------------------------------------------------- */ @@ -902,7 +592,9 @@ int VirtualNetwork::remove_leases(VirtualNetworkTemplate * leases_template, leases_template->get("LEASES", vector_leases); - return leases->remove_leases(vector_leases, error_msg); + //TODO + return 0; + //return leases->remove_leases(vector_leases, error_msg); } /* -------------------------------------------------------------------------- */ @@ -914,8 +606,9 @@ int VirtualNetwork::hold_leases(VirtualNetworkTemplate * leases_template, vector vector_leases; leases_template->get("LEASES", vector_leases); - - return leases->hold_leases(vector_leases, error_msg); + //TODO + return 0; + //return leases->hold_leases(vector_leases, error_msg); } /* -------------------------------------------------------------------------- */ @@ -928,7 +621,9 @@ int VirtualNetwork::free_leases(VirtualNetworkTemplate * leases_template, leases_template->get("LEASES", vector_leases); - return leases->free_leases(vector_leases, error_msg); + //TODO + return 0; + //return leases->free_leases(vector_leases, error_msg); } /* -------------------------------------------------------------------------- */ From cab018fc98ac8f261f2507b5c9c5439730d7463b Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 10 May 2014 19:46:27 +0200 Subject: [PATCH 09/84] feature #2858: Allocate & free functions for the address range pool --- include/AddressRange.h | 11 ++++-- include/AddressRangePool.h | 63 ++++++++++++++++++++++++++++++++ src/vnm/AddressRange.cc | 28 ++++++++++---- src/vnm/AddressRangePool.cc | 73 +++++++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 11 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index 423b6bad77..b10e36db70 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -79,7 +79,8 @@ public: int from_vattr_db(VectorAttribute *vattr); /** - * Builds an extended XML representation of the AR to send it back to clients + * Builds an extended XML representation of the AR to send it back to + * clients */ void to_xml(ostringstream &oss) const; @@ -141,10 +142,11 @@ public: /** * Frees a previous allocated address, referenced by its MAC address - * + * @param ot the object type of the owner of the address + * @param obid the id of the owner of the address * @param mac the MAC address in string form */ - void free_addr(const string& mac); + void free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); /** * Return the id for this address range @@ -240,7 +242,8 @@ private: /** * Frees an address from the map. Updates the ALLOCATED attribute */ - void free_addr(unsigned int addr_index); + void free_addr(PoolObjectSQL::ObjectType ot, int obid, + unsigned int addr_index); /* ---------------------------------------------------------------------- */ /* Restricted Attributes functions */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index ca426275b3..4e7cd9548e 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -26,6 +26,8 @@ #include "Template.h" #include "AddressRange.h" +class VectorAttribute; + using namespace std; class AddressRangePool @@ -52,13 +54,74 @@ public: */ int from_xml_node(const xmlNodePtr node); + /** + * Generate a XML representation of the Address Range Pool + * @param sstream where the ARPool is written + * @param extended true to include lease information + * @return the string with the XML + */ string& to_xml(string& sstream, bool extended) const; + /** + * Allocates an address in a suitable address range from the pool + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + * @param nic the NIC attribute to be filled with lease attributes + * @param inherit attributes to be added to the NIC + * @return 0 if success + */ + int allocate(PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, + const vector &inherit); + + /** + * Allocates an address in a suitable address range from the pool by mac + * @param mac the specific MAC address requested + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + * @param nic the NIC attribute to be filled with lease attributes + * @param inherit attributes to be added to the NIC + * @return 0 if success + */ + int allocate_by_mac(const string &mac, PoolObjectSQL::ObjectType ot, int obid, + VectorAttribute * nic, const vector &inherit); + + /** + * Allocates an address in a suitable address range from the pool by mac + * @param ip the specific IP address requested + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + * @param nic the NIC attribute to be filled with lease attributes + * @param inherit attributes to be added to the NIC + * @return 0 if success + */ + int allocate_by_ip(const string &ip, PoolObjectSQL::ObjectType ot, int obid, + VectorAttribute * nic, const vector &inherit); + + /** + * Frees the given address by MAC + * @param arid the ID of the address range + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + * @param mac the specific MAC address requested + */ + void free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot, int obid, + const string& mac); + private: + /** + * Stores the Address Ranges in a template form. This template is used + * to store the pool in the DB + */ Template ar_template; + /** + * ID for the next Address Range + */ unsigned int next_ar; + /** + * Map to access each range + */ map ar_pool; }; diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 0810c244c4..653d6d72d6 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -167,7 +167,7 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) vattr->replace("AR_ID", id); - if (do_mac) //Need to add MAC_START to the attribute + if (do_mac) //Need to add MAC to the attribute { set_mac(0, attr); } @@ -599,7 +599,7 @@ int AddressRange::attr_to_allocated(const string& allocated_s) /* -------------------------------------------------------------------------- */ void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, - unsigned int addr_index) + unsigned int addr_index) { allocated.insert(make_pair(addr_index,ot|obid)); @@ -609,11 +609,18 @@ void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, /* -------------------------------------------------------------------------- */ -void AddressRange::free_addr(unsigned int addr_index) +void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, + unsigned int addr_index) { - allocated.erase(addr_index); + map::iterator it; - allocated_to_attr(); + it = allocated.find(addr_index); + + if (it != allocated.end() && it->second == (ot|obid)) + { + allocated.erase(it); + allocated_to_attr(); + } } /* ************************************************************************** */ @@ -710,6 +717,11 @@ int AddressRange::allocate_by_ip( VectorAttribute* nic, const vector& inherit) { + if (!(type & 0x00000002))//Not of type IP4 or IP4_6 + { + return -1; + } + unsigned int ip_i; if (ip_to_i(ip_s, ip_i) == -1) @@ -747,10 +759,12 @@ int AddressRange::allocate_by_ip( return 0; } + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRange::free_addr(const string& mac_s) +void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, + const string& mac_s) { unsigned int mac_i[2]; @@ -760,7 +774,7 @@ void AddressRange::free_addr(const string& mac_s) if ((mac_i[0] >= mac[0]) && (index < size)) { - free_addr(index); + free_addr(ot, obid, index); } } diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index a370616f4a..0f236df306 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -136,4 +136,77 @@ string& AddressRangePool::to_xml(string& sstream, bool extended) const return ar_template.to_xml(sstream); } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ +int AddressRangePool::allocate(PoolObjectSQL::ObjectType ot, int obid, + VectorAttribute * nic, const vector &inherit) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if (it->second->allocate_addr(ot, obid, nic, inherit) == 0) + { + return 0; + } + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::allocate_by_mac(const string &mac, + PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, + const vector &inherit) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if (it->second->allocate_by_mac(mac, ot, obid, nic, inherit) == 0) + { + return 0; + } + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::allocate_by_ip(const string &ip, + PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, + const vector &inherit) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if (it->second->allocate_by_ip(ip, ot, obid, nic, inherit) == 0) + { + return 0; + } + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AddressRangePool::free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot, + int obid, const string& mac) +{ + map::iterator it; + + it = ar_pool.find(arid); + + if (it!=ar_pool.end()) + { + it->second->free_addr(ot, obid, mac); + } +} From 72b61e9ec5eb7f6584139feda481f2f6728f6b6a Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 10 May 2014 21:06:59 +0200 Subject: [PATCH 10/84] feature #2858: Integrate Address Ranges with VirtualNetwork and VirtualMachine --- include/AddressRangePool.h | 4 +- include/VirtualNetwork.h | 74 ++++++++++++++++--------------------- src/vm/VirtualMachine.cc | 18 ++++----- src/vnm/AddressRange.cc | 19 +++++----- src/vnm/AddressRangePool.cc | 2 +- src/vnm/VirtualNetwork.cc | 25 +++++++++++-- 6 files changed, 74 insertions(+), 68 deletions(-) diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 4e7cd9548e..c7eb858708 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -70,8 +70,8 @@ public: * @param inherit attributes to be added to the NIC * @return 0 if success */ - int allocate(PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, - const vector &inherit); + int allocate_addr(PoolObjectSQL::ObjectType ot, int obid, + VectorAttribute * nic, const vector &inherit); /** * Allocates an address in a suitable address range from the pool by mac diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 8e38fb13bd..f357a3adcb 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -98,69 +98,57 @@ public: int free_leases(VirtualNetworkTemplate* leases, string& error_msg); /** - * Gets a new lease for a specific VM + * Gets a new address lease for a specific VM * @param vid VM identifier - * @param _ip pointer to string for IP to be stored into - * @param _mac pointer to string for MAC to be stored into - * @param _bridge name of the physical bridge this VN binds to + * @param nic the VM NIC attribute to be filled with the lease info. + * @param inherit attributes from the address range to include in the NIC * @return 0 if success */ - int get_lease(int vid, string& _ip, string& _mac, string& _bridge) + int allocate_addr(int vid, VectorAttribute * nic, + const vector& inherit) { - return 0; - //TODO - /* - unsigned int eui64[2]; - - _bridge = bridge; - return leases->get(vid, _ip, _mac, eui64); - */ - }; + return ar_pool.allocate_addr(PoolObjectSQL::VM, vid, nic, inherit); + } /** - * Asks for an specific lease of the given virtual network + * Gets a new address lease for a specific VM by MAC * @param vid VM identifier - * @param _ip the ip of the requested lease - * @param _mac pointer to string for MAC to be stored into - * @param _bridge name of the physical bridge this VN binds to + * @param mac the MAC address requested + * @param nic the VM NIC attribute to be filled with the lease info. + * @param inherit attributes from the address range to include in the NIC * @return 0 if success */ - int set_lease(int vid, const string& _ip, string& _mac, string& _bridge) + int allocate_by_mac(int vid, const string& mac, VectorAttribute * nic, + const vector& inherit) { - //TODO - /* - unsigned int eui64[2]; - - _bridge = bridge; - return leases->set(vid, _ip, _mac, eui64); - */ - - return 0; - }; + return ar_pool.allocate_by_mac(mac, PoolObjectSQL::VM, vid, nic, inherit); + } /** - * Release previously given lease - * @param _ip IP identifying the lease + * Gets a new address lease for a specific VM by IP + * @param vid VM identifier + * @param ip the IP address requested + * @param nic the VM NIC attribute to be filled with the lease info. + * @param inherit attributes from the address range to include in the NIC * @return 0 if success */ - void release_lease(const string& ip) + int allocate_by_ip(int vid, const string& ip, VectorAttribute * nic, + const vector& inherit) { - //TODO - //return leases->release(ip); - }; + return ar_pool.allocate_by_ip(ip, PoolObjectSQL::VM, vid, nic, inherit); + } /** - * Check if a VM is the owner of the ip - * @param ip of the lease to be checked + * Release previously given address lease + * @param arid of the address range where the address was leased from * @param vid the ID of the VM - * @return true if the ip was already assigned + * @param mac MAC address identifying the lease + * @return 0 if success */ - bool is_owner (const string& ip, int vid) + void free_addr(unsigned int arid, int vid, const string& mac) { - return true; - //TODO - //return leases->is_owner(ip, vid); - }; + ar_pool.free_addr(arid, PoolObjectSQL::VM, vid, mac); + } /** * Gets size of the network (used + free) diff --git a/src/vm/VirtualMachine.cc b/src/vm/VirtualMachine.cc index 3db4b0591c..830092e199 100644 --- a/src/vm/VirtualMachine.cc +++ b/src/vm/VirtualMachine.cc @@ -2595,21 +2595,23 @@ int VirtualMachine::release_network_leases(VectorAttribute const * nic, int vmid VirtualNetwork* vn; int vnid; - string ip; + int ar_id; + string mac; if ( nic == 0 ) { return -1; } - if ( nic->vector_value("NETWORK_ID", vnid) != 0 ) + if (nic->vector_value("NETWORK_ID", vnid) != 0 || + nic->vector_value("AR_ID", ar_id) != 0) { return -1; } - ip = nic->vector_value("IP"); + mac = nic->vector_value("MAC"); - if ( ip.empty() ) + if (mac.empty()) { return -1; } @@ -2621,11 +2623,9 @@ int VirtualMachine::release_network_leases(VectorAttribute const * nic, int vmid return -1; } - if (vn->is_owner(ip,vmid)) - { - vn->release_lease(ip); - vnpool->update(vn); - } + vn->free_addr(ar_id, vmid, mac); + + vnpool->update(vn); vn->unlock(); diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 653d6d72d6..bea8c41862 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -263,11 +263,11 @@ void AddressRange::to_xml(ostringstream &oss) const if (it->second & PoolObjectSQL::VM) { - lease.replace("VM", it->second && 0x00000000FFFFFFFFLL); + lease.replace("VM", it->second & 0x00000000FFFFFFFFLL); } else if (it->second & PoolObjectSQL::NET) { - lease.replace("VNET", it->second && 0x00000000FFFFFFFFLL); + lease.replace("VNET", it->second & 0x00000000FFFFFFFFLL); } lease.to_xml(oss); @@ -586,11 +586,12 @@ int AddressRange::attr_to_allocated(const string& allocated_s) { iss >> ws >> addr_index >> ws >> object_pack; - if (!iss.fail()) + if (iss.fail()) { - allocated.insert(make_pair(addr_index,object_pack)); return -1; } + + allocated.insert(make_pair(addr_index,object_pack)); } return 0; @@ -632,8 +633,6 @@ int AddressRange::allocate_addr( VectorAttribute* nic, const vector& inherit) { - int rc = -1; - for ( unsigned int i=0; i &inherit) { map::iterator it; diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index a014c0458b..771636ddac 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -560,11 +560,28 @@ int VirtualNetwork::nic_attribute( } } - //GET THE IP/MAC... AND OVERWRITE FROM AR. - //string ip = nic->vector_value("IP"); - //string mac = nic->vector_value("MAC"); + //-------------------------------------------------------------------------- + // Get the lease from the Virtual Network + //-------------------------------------------------------------------------- + int rc; - return 0; + string ip = nic->vector_value("IP"); + string mac = nic->vector_value("MAC"); + + if (!ip.empty()) + { + rc = allocate_by_ip(vid, ip, nic, inherit_attrs); + } + else if (!mac.empty()) + { + rc = allocate_by_mac(vid, mac, nic, inherit_attrs); + } + else + { + rc = allocate_addr(vid, nic, inherit_attrs); + } + + return rc; } /* -------------------------------------------------------------------------- */ From f0e64384cf8f7c50df8c42dc5598f932d00c5b67 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 10 May 2014 21:44:39 +0200 Subject: [PATCH 11/84] feature #2858: Adds lease usage information to XML descriptions --- include/AddressRange.h | 21 ++++++++++++++++----- include/AddressRangePool.h | 13 +++++++++++++ include/VirtualNetwork.h | 15 +-------------- src/vnm/AddressRange.cc | 20 +++++++++++++++++--- src/vnm/AddressRangePool.cc | 14 +++++++++++--- src/vnm/VirtualNetwork.cc | 1 + 6 files changed, 59 insertions(+), 25 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index b10e36db70..3d2175c24f 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -34,7 +34,7 @@ class AddressRange { public: - AddressRange(unsigned int _id):id(_id),next(0){}; + AddressRange(unsigned int _id):id(_id),next(0),used_addr(0){}; virtual ~AddressRange(){}; @@ -145,17 +145,26 @@ public: * @param ot the object type of the owner of the address * @param obid the id of the owner of the address * @param mac the MAC address in string form + * @return 0 if the address was freed */ - void free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); + int free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); /** * Return the id for this address range */ - unsigned int ar_id() + unsigned int ar_id() const { return id; } + /** + * Return the number of used addresses + */ + unsigned int get_used_addr() const + { + return used_addr; + } + private: /* ---------------------------------------------------------------------- */ /* String to binary conversion functions for different address types */ @@ -242,7 +251,7 @@ private: /** * Frees an address from the map. Updates the ALLOCATED attribute */ - void free_addr(PoolObjectSQL::ObjectType ot, int obid, + int free_addr(PoolObjectSQL::ObjectType ot, int obid, unsigned int addr_index); /* ---------------------------------------------------------------------- */ @@ -306,7 +315,9 @@ private: */ map allocated; - unsigned int next; + unsigned int next; + + unsigned int used_addr; /* ---------------------------------------------------------------------- */ /* Restricted Attributes */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index c7eb858708..44fcd8f6f9 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -107,6 +107,14 @@ public: void free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot, int obid, const string& mac); + /** + * Return the number of used addresses + */ + unsigned int get_used_addr() const + { + return used_addr; + } + private: /** * Stores the Address Ranges in a template form. This template is used @@ -123,6 +131,11 @@ private: * Map to access each range */ map ar_pool; + + /** + * Used addresses + */ + unsigned int used_addr; }; #endif diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index f357a3adcb..c8c8385513 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -150,26 +150,13 @@ public: ar_pool.free_addr(arid, PoolObjectSQL::VM, vid, mac); } - /** - * Gets size of the network (used + free) - * @return number of hosts that can be fitted in this network - */ - unsigned int get_size() - { - //TODO - return 0; - //return leases->size; - }; - /** * Gets used leases * @return number of network leases in used */ unsigned int get_used() { - //TODO - return 0; - //return leases->n_used; + return ar_pool.get_used_addr(); }; /** diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index bea8c41862..b759a4c8fe 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -276,6 +276,8 @@ void AddressRange::to_xml(ostringstream &oss) const oss << ""; } + oss << "" << used_addr << ""; + oss << ""; } @@ -592,6 +594,8 @@ int AddressRange::attr_to_allocated(const string& allocated_s) } allocated.insert(make_pair(addr_index,object_pack)); + + used_addr++; } return 0; @@ -605,12 +609,14 @@ void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, allocated.insert(make_pair(addr_index,ot|obid)); + used_addr++; + allocated_to_attr(); } /* -------------------------------------------------------------------------- */ -void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, +int AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, unsigned int addr_index) { map::iterator it; @@ -621,7 +627,13 @@ void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, { allocated.erase(it); allocated_to_attr(); + + used_addr--; + + return 0; } + + return -1; } /* ************************************************************************** */ @@ -764,7 +776,7 @@ int AddressRange::allocate_by_ip( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, +int AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac_s) { unsigned int mac_i[2]; @@ -775,8 +787,10 @@ void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, if ((mac_i[0] >= mac[0]) && (index < size)) { - free_addr(ot, obid, index); + return free_addr(ot, obid, index); } + + return -1; } /* ************************************************************************** */ diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 3d21ff218c..e78a3a1c0c 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -18,8 +18,8 @@ using namespace std; -AddressRangePool::AddressRangePool():ar_template(false,'=',"AR_POOL"),next_ar(0) -{}; +AddressRangePool::AddressRangePool():ar_template(false,'=',"AR_POOL"), + next_ar(0), used_addr(0){}; AddressRangePool::~AddressRangePool() { @@ -104,6 +104,8 @@ int AddressRangePool::from_xml_node(const xmlNodePtr node) { next_ar = ar->ar_id() + 1; } + + used_addr += ar->get_used_addr(); } return 0; @@ -148,6 +150,7 @@ int AddressRangePool::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, { if (it->second->allocate_addr(ot, obid, nic, inherit) == 0) { + used_addr++; return 0; } } @@ -168,6 +171,7 @@ int AddressRangePool::allocate_by_mac(const string &mac, { if (it->second->allocate_by_mac(mac, ot, obid, nic, inherit) == 0) { + used_addr++; return 0; } } @@ -188,6 +192,7 @@ int AddressRangePool::allocate_by_ip(const string &ip, { if (it->second->allocate_by_ip(ip, ot, obid, nic, inherit) == 0) { + used_addr++; return 0; } } @@ -207,6 +212,9 @@ void AddressRangePool::free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot if (it!=ar_pool.end()) { - it->second->free_addr(ot, obid, mac); + if ( it->second->free_addr(ot, obid, mac) == 0 ) + { + used_addr--; + } } } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 771636ddac..22302f6bba 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -431,6 +431,7 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const { os << ""; } + os << ""<< ar_pool.get_used_addr() << ""; os << obj_template->to_xml(template_xml); From a99707d8289f185301720747339fce72d4b59e3a Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sun, 11 May 2014 23:01:05 +0200 Subject: [PATCH 12/84] feature #2858: Update onevm and onevnet command line tools --- src/cli/one_helper/onevm_helper.rb | 8 +- src/cli/one_helper/onevnet_helper.rb | 105 ++++++++++++++++++--------- src/vnm/AddressRange.cc | 4 +- src/vnm/VirtualNetwork.cc | 2 +- 4 files changed, 79 insertions(+), 40 deletions(-) diff --git a/src/cli/one_helper/onevm_helper.rb b/src/cli/one_helper/onevm_helper.rb index 7755f6036c..f8086ae136 100644 --- a/src/cli/one_helper/onevm_helper.rb +++ b/src/cli/one_helper/onevm_helper.rb @@ -248,8 +248,8 @@ class OneVMHelper < OpenNebulaHelper::OneHelper puts str % ["HOST", vm['/VM/HISTORY_RECORDS/HISTORY[last()]/HOSTNAME']] if %w{ACTIVE SUSPENDED POWEROFF}.include? vm.state_str - puts str % ["CLUSTER ID", - vm['/VM/HISTORY_RECORDS/HISTORY[last()]/CID'] ] if + puts str % ["CLUSTER ID", + vm['/VM/HISTORY_RECORDS/HISTORY[last()]/CID'] ] if %w{ACTIVE SUSPENDED POWEROFF}.include? vm.state_str puts str % ["START TIME", OpenNebulaHelper.time_to_str(vm['/VM/STIME'])] @@ -380,8 +380,8 @@ class OneVMHelper < OpenNebulaHelper::OneHelper array_id += 1 end - if nic.has_key?("IP6_SITE") - ip6_link = {"IP" => nic.delete("IP6_SITE"), + if nic.has_key?("IP6_ULA") + ip6_link = {"IP" => nic.delete("IP6_ULA"), "CLI_DONE" => true, "DOUBLE_ENTRY" => true} vm_nics.insert(array_id+1,ip6_link) diff --git a/src/cli/one_helper/onevnet_helper.rb b/src/cli/one_helper/onevnet_helper.rb index 84c6b99931..a8db52d5ab 100644 --- a/src/cli/one_helper/onevnet_helper.rb +++ b/src/cli/one_helper/onevnet_helper.rb @@ -57,7 +57,7 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper column :CLUSTER, "Name of the Cluster", :left, :size=>10 do |d| OpenNebulaHelper.cluster_str(d["CLUSTER"]) - end + end column :TYPE, "Type of Virtual Network", :size=>6 do |d| OneVNetHelper.type_to_str(d["TYPE"]) @@ -74,7 +74,7 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper column :LEASES, "Number of this Virtual Network's given leases", :size=>6 do |d| - d["TOTAL_LEASES"] + d["USED_LEASES"] end default :ID, :USER, :GROUP, :NAME, :CLUSTER, :TYPE, :BRIDGE, :LEASES @@ -109,14 +109,11 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper puts str % ["USER", vn['UNAME']] puts str % ["GROUP", vn['GNAME']] puts str % ["CLUSTER", OpenNebulaHelper.cluster_str(vn['CLUSTER'])] - puts str % ["TYPE", vn.type_str] puts str % ["BRIDGE", vn["BRIDGE"]] puts str % ["VLAN", OpenNebulaHelper.boolean_to_str(vn['VLAN'])] puts str % ["PHYSICAL DEVICE", vn["PHYDEV"]] if !vn["PHYDEV"].empty? puts str % ["VLAN ID", vn["VLAN_ID"]] if !vn["VLAN_ID"].empty? - puts str % ["GLOBAL PREFIX", vn["GLOBAL_PREFIX"]] if !vn["GLOBAL_PREFIX"].empty? - puts str % ["SITE PREFIX", vn["SITE_PREFIX"]] if !vn["SITE_PREFIX"].empty? - puts str % ["USED LEASES", vn['TOTAL_LEASES']] + puts str % ["USED LEASES", vn['USED_LEASES']] puts CLIHelper.print_header(str_h1 % "PERMISSIONS",false) @@ -129,45 +126,87 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper puts str % [e, mask] } + puts CLIHelper.print_header(str_h1 % ["VIRTUAL NETWORK TEMPLATE"], false) puts vn.template_str(false) - if vn.type_str == "RANGED" - puts - CLIHelper.print_header(str_h1 % ["RANGE"], false) - puts str % ["IP_START", vn['RANGE/IP_START']] - puts str % ["IP_END", vn['RANGE/IP_END']] - end + puts - lease_types = [ ["LEASES ON HOLD", 'LEASE[USED=1 and VID=-1]'], - ["USED LEASES", 'LEASE[USED=1 and VID>-1]'], - ["FREE LEASES", 'LEASE[USED=0]'] ] + CLIHelper.print_header(str_h1 % ["ADDRESS RANGE POOL"], false) - lease_types.each { |pair| - leases_str = vn.template_like_str('/VNET/LEASES', false, pair[1]) - - if !leases_str.empty? - puts - CLIHelper.print_header(str_h1 % [pair[0]], false) - puts leases_str + CLIHelper::ShowTable.new(nil, self) do + column :AR, "", :size=>3 do |d| + d["AR_ID"] end - } + + column :TYPE, "", :left, :size=>5 do |d| + d["TYPE"] + end + + column :SIZE, "", :size=>6 do |d| + d["SIZE"] + end + + column :LEASES, "", :size=>6 do |d| + d["USED_LEASES"] + end + + column :MAC, "", :size=>17 do |d| + d["MAC"] + end + + column :IP, "", :size=>15 do |d| + d["IP"]||"-" + end + + column :IP6_GLOBAL_PREFIX, "", :right, :size=>22 do |d| + d["GLOBAL_PREFIX"]||"-" + end + + end.show([vn.to_hash['VNET']['AR_POOL']['AR']].flatten, {}) puts - CLIHelper.print_header("VIRTUAL MACHINES", false) - puts + CLIHelper.print_header(str_h1 % ["LEASES"], false) - vms=vn.retrieve_elements("LEASES/LEASE/VID") - - if vms - vms=vms.delete_if {|vm| vm=="-1" } - vms.map!{|e| e.to_i } - onevm_helper=OneVMHelper.new - onevm_helper.client=@client - onevm_helper.list_pool({:ids=>vms}, false) + leases = Array.new + [vn.to_hash['VNET']['AR_POOL']['AR']].flatten.each do |ar| + id = ar['AR_ID'] + if ar['LEASES'] && !ar['LEASES']['LEASE'].nil? + lease = [ar['LEASES']['LEASE']].flatten + lease.each do |l| + l['AR_ID'] = id + end + leases << lease + end end + + CLIHelper::ShowTable.new(nil, self) do + column :AR, "", :left, :size=>3 do |d| + d['AR_ID'] + end + + column :OWNER, "", :left, :size=>10 do |d| + if d['VM'] + "VM : #{d['VM']}" + elsif d['VNET'] + "NET: #{d['VM']}" + end + end + + column :MAC, "", :size=>17 do |d| + d["MAC"] + end + + column :IP, "", :size=>15 do |d| + d["IP"]||"-" + end + + column :IP6_GLOBAL, "", :size=>31 do |d| + d["IP6_GLOBAL"]||"-" + end + end.show(leases.flatten, {}) end end diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index b759a4c8fe..753cad0107 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -234,6 +234,8 @@ void AddressRange::to_xml(ostringstream &oss) const << "]]>first << ">"; } + oss << "" << used_addr << ""; + if (allocated.empty()) { oss << ""; @@ -276,8 +278,6 @@ void AddressRange::to_xml(ostringstream &oss) const oss << ""; } - oss << "" << used_addr << ""; - oss << ""; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 22302f6bba..1091029839 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -431,7 +431,7 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const { os << ""; } - os << ""<< ar_pool.get_used_addr() << ""; + os << ""<< ar_pool.get_used_addr() << ""; os << obj_template->to_xml(template_xml); From 1a4f9e09f083ef8303a11a83b7c7261d1265f7cc Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 17 May 2014 01:17:58 +0200 Subject: [PATCH 13/84] feature #2858: Support for Address Range attributes in context --- include/AddressRange.h | 10 ++++++++++ include/AddressRangePool.h | 2 ++ include/VirtualNetwork.h | 19 ++++++++++++------- src/template/template_parser.c | 2 +- src/vm/vm_var_parser.c | 2 +- src/vm/vm_var_syntax.cc | 16 ++++++++-------- src/vm/vm_var_syntax.h | 1 - src/vm/vm_var_syntax.y | 16 ++++++++-------- src/vnm/AddressRangePool.cc | 18 ++++++++++++++++++ src/vnm/SConstruct | 6 +++--- src/vnm/VirtualNetwork.cc | 12 +++++++++++- src/vnm/VirtualNetworkPool.cc | 2 +- src/xml/expr_parser.c | 2 +- 13 files changed, 76 insertions(+), 32 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index 3d2175c24f..c38136cd36 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -165,6 +165,16 @@ public: return used_addr; } + /** + * Returns the string value of an Address Range Attribute + * @param name of the attribute + * @return the value of the attribute if found, empty otherwise + */ + string get_attribute(const char *name) const + { + return attr->vector_value(name); + } + private: /* ---------------------------------------------------------------------- */ /* String to binary conversion functions for different address types */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 44fcd8f6f9..fb9fe09e6f 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -115,6 +115,8 @@ public: return used_addr; } + void get_attribute(const char * name, string& value, int ar_id) const; + private: /** * Stores the Address Ranges in a template form. This template is used diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index c8c8385513..ed67480245 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -207,6 +207,17 @@ public: */ int replace_template(const string& tmpl_str, string& error); + /** + * Gets a string based attribute (single) from an address range. If the + * attribute is not found in the address range, the VNET template will be + * used + * @param name of the attribute + * @param value of the attribute (a string), will be "" if not defined or + * not a single attribute + * @param ar_id of the address attribute. + */ + void get_template_attribute(const char * name, string& value, int ar_id) const; + private: // ------------------------------------------------------------------------- @@ -266,15 +277,9 @@ private: */ static int bootstrap(SqlDB * db) { - int rc; - ostringstream oss_vnet(VirtualNetwork::db_bootstrap); - ostringstream oss_lease(Leases::db_bootstrap); - rc = db->exec(oss_vnet); - rc += db->exec(oss_lease); - - return rc; + return db->exec(oss_vnet); }; /** diff --git a/src/template/template_parser.c b/src/template/template_parser.c index 9faace2fe3..1836c734f1 100644 --- a/src/template/template_parser.c +++ b/src/template/template_parser.c @@ -28,7 +28,7 @@ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 -#define YY_FLEX_SUBMINOR_VERSION 37 +#define YY_FLEX_SUBMINOR_VERSION 39 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif diff --git a/src/vm/vm_var_parser.c b/src/vm/vm_var_parser.c index f40f383298..a0df65025d 100644 --- a/src/vm/vm_var_parser.c +++ b/src/vm/vm_var_parser.c @@ -28,7 +28,7 @@ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 -#define YY_FLEX_SUBMINOR_VERSION 37 +#define YY_FLEX_SUBMINOR_VERSION 39 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif diff --git a/src/vm/vm_var_syntax.cc b/src/vm/vm_var_syntax.cc index ecba1a1971..2f46a8b51d 100644 --- a/src/vm/vm_var_syntax.cc +++ b/src/vm/vm_var_syntax.cc @@ -223,7 +223,7 @@ void get_network_attribute(VirtualMachine * vm, VirtualNetworkPool * vnpool = nd.get_vnpool(); VirtualNetwork * vn; - int vnet_id = -1; + int ar_id, vnet_id = -1; int num; vector attrs; @@ -253,12 +253,12 @@ void get_network_attribute(VirtualMachine * vm, if ( net->vector_value(net_name.c_str()) == net_value ) { - string vnet_id_str = net->vector_value("NETWORK_ID"); - istringstream iss(vnet_id_str); + if (net->vector_value("NETWORK_ID", vnet_id) != 0) + { + vnet_id = -1; + } - iss >> vnet_id; - - if (iss.fail()) + if (net->vector_value("AR_ID", ar_id) != 0) { vnet_id = -1; } @@ -288,7 +288,7 @@ void get_network_attribute(VirtualMachine * vm, } else { - vn->get_template_attribute(attr_name.c_str(),attr_value); + vn->get_template_attribute(attr_name.c_str(), attr_value, ar_id); } vn->unlock(); @@ -361,7 +361,7 @@ void insert_single(VirtualMachine * vm, parsed << vm->get_name(); } - else + else { vm->get_template_attribute(name.c_str(),value); diff --git a/src/vm/vm_var_syntax.h b/src/vm/vm_var_syntax.h index 7b449b9c17..7558dd0e9e 100644 --- a/src/vm/vm_var_syntax.h +++ b/src/vm/vm_var_syntax.h @@ -87,5 +87,4 @@ struct YYLTYPE # define YYLTYPE_IS_DECLARED 1 # define YYLTYPE_IS_TRIVIAL 1 #endif - #endif /* !YY_VM_VAR_VM_VAR_SYNTAX_HH_INCLUDED */ diff --git a/src/vm/vm_var_syntax.y b/src/vm/vm_var_syntax.y index d3c924160b..1a6bd4ee82 100644 --- a/src/vm/vm_var_syntax.y +++ b/src/vm/vm_var_syntax.y @@ -168,7 +168,7 @@ void get_network_attribute(VirtualMachine * vm, VirtualNetworkPool * vnpool = nd.get_vnpool(); VirtualNetwork * vn; - int vnet_id = -1; + int ar_id, vnet_id = -1; int num; vector attrs; @@ -198,12 +198,12 @@ void get_network_attribute(VirtualMachine * vm, if ( net->vector_value(net_name.c_str()) == net_value ) { - string vnet_id_str = net->vector_value("NETWORK_ID"); - istringstream iss(vnet_id_str); + if (net->vector_value("NETWORK_ID", vnet_id) != 0) + { + vnet_id = -1; + } - iss >> vnet_id; - - if (iss.fail()) + if (net->vector_value("AR_ID", ar_id) != 0) { vnet_id = -1; } @@ -233,7 +233,7 @@ void get_network_attribute(VirtualMachine * vm, } else { - vn->get_template_attribute(attr_name.c_str(),attr_value); + vn->get_template_attribute(attr_name.c_str(), attr_value, ar_id); } vn->unlock(); @@ -306,7 +306,7 @@ void insert_single(VirtualMachine * vm, parsed << vm->get_name(); } - else + else { vm->get_template_attribute(name.c_str(),value); diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index e78a3a1c0c..30a17b4873 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -218,3 +218,21 @@ void AddressRangePool::free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot } } } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AddressRangePool::get_attribute(const char * name, string& value, + int ar_id) const +{ + map::const_iterator it; + + value.clear(); + + it = ar_pool.find(ar_id); + + if (it!=ar_pool.end()) + { + value = it->second->get_attribute(name); + } +} diff --git a/src/vnm/SConstruct b/src/vnm/SConstruct index eb586a69df..f2f18f85b0 100644 --- a/src/vnm/SConstruct +++ b/src/vnm/SConstruct @@ -22,9 +22,9 @@ lib_name='nebula_vnm' # Sources to generate the library source_files=[ - 'Leases.cc', - 'FixedLeases.cc', - 'RangedLeases.cc', +# 'Leases.cc', +# 'FixedLeases.cc', +# 'RangedLeases.cc', 'VirtualNetwork.cc', 'VirtualNetworkPool.cc', 'AddressRange.cc', diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 1091029839..72e49d17fd 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -553,7 +553,7 @@ int VirtualNetwork::nic_attribute( for (it = inherit_attrs.begin(); it != inherit_attrs.end(); it++) { - get_template_attribute((*it).c_str(), inherit_val); + PoolObjectSQL::get_template_attribute((*it).c_str(), inherit_val); if (!inherit_val.empty()) { @@ -646,3 +646,13 @@ int VirtualNetwork::free_leases(VirtualNetworkTemplate * leases_template, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ + +void VirtualNetwork::get_template_attribute(const char * name, string& value, int ar_id) const +{ + ar_pool.get_attribute(name, value, ar_id); + + if (value.empty()) + { + PoolObjectSQL::get_template_attribute(name, value); + } +} diff --git a/src/vnm/VirtualNetworkPool.cc b/src/vnm/VirtualNetworkPool.cc index 8d2666386d..49d6513161 100644 --- a/src/vnm/VirtualNetworkPool.cc +++ b/src/vnm/VirtualNetworkPool.cc @@ -109,7 +109,7 @@ int VirtualNetworkPool::allocate ( cluster_id, cluster_name, vn_template); // Check name - vn->get_template_attribute("NAME", name); + vn->PoolObjectSQL::get_template_attribute("NAME", name); if ( !PoolObjectSQL::name_is_valid(name, error_str) ) { diff --git a/src/xml/expr_parser.c b/src/xml/expr_parser.c index f1326e9a84..3c7a6f6642 100644 --- a/src/xml/expr_parser.c +++ b/src/xml/expr_parser.c @@ -28,7 +28,7 @@ #define FLEX_SCANNER #define YY_FLEX_MAJOR_VERSION 2 #define YY_FLEX_MINOR_VERSION 5 -#define YY_FLEX_SUBMINOR_VERSION 37 +#define YY_FLEX_SUBMINOR_VERSION 39 #if YY_FLEX_SUBMINOR_VERSION > 0 #define FLEX_BETA #endif From 14105efd0d5147b9b2205c9d683d3ba15283635c Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 19 May 2014 00:28:27 +0200 Subject: [PATCH 14/84] feature #2858: Hold first matching lease if no address range is specified --- include/AddressRange.h | 37 ++++++++--- include/AddressRangePool.h | 30 +++++++++ src/vnm/AddressRange.cc | 74 +++++++++++++++++++++- src/vnm/AddressRangePool.cc | 70 +++++++++++++++++++++ src/vnm/VirtualNetwork.cc | 121 ++++++++++++++++++++++++++++++++++-- 5 files changed, 314 insertions(+), 18 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index c38136cd36..02f0d5d151 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -113,9 +113,11 @@ public: /** * Returns an unused address, which becomes used and fills a NIC attribute * with the configuration parameters from the address range. - * - * @param nic the VM NIC attribute - * @return 0 if success + * @param ot the type of the object allocating the address + * @param obid the id of the object + * @param nic the VM NIC attribute + * @param inherit attributes to be added to the NIC attribute + * @return 0 if success */ int allocate_addr(PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, const vector &inherit); @@ -123,9 +125,12 @@ public: /** * Returns the specific address by mac if is not allocated. The NIC attr * is filled with the configuration parameters from the address range. - * - * @param nic the VM NIC attribute - * @return 0 if success + * @param mac the mac address + * @param ot the type of the object allocating the address + * @param obid the id of the object + * @param nic the VM NIC attribute + * @param inherit attributes to be added to the NIC attribute + * @return 0 if success */ int allocate_by_mac(const string& mac, PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, const vector &inherit); @@ -133,13 +138,27 @@ public: /** * Returns the specific address by ip if is not allocated. The NIC attr * is filled with the configuration parameters from the address range. - * - * @param nic the VM NIC attribute - * @return 0 if success + * @param ot the type of the object allocating the address + * @param obid the id of the object + * @param nic the VM NIC attribute + * @param inherit attributes to be added to the NIC attribute + * @return 0 if success */ int allocate_by_ip(const string& ip, PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, const vector &inherit); + /** + * Sets the given ip on hold, the address is associated to a VM of id -1. + * @param ip the ip to hold + */ + int hold_by_ip(const string& ip); + + /** + * Sets the given mac on hold, the address is associated to a VM of id -1. + * @param mac the mac to hold + */ + int hold_by_mac(const string& mac); + /** * Frees a previous allocated address, referenced by its MAC address * @param ot the object type of the owner of the address diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index fb9fe09e6f..bee22ba2ce 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -97,6 +97,36 @@ public: int allocate_by_ip(const string &ip, PoolObjectSQL::ObjectType ot, int obid, VectorAttribute * nic, const vector &inherit); + /** + * Holds an address from the specified address range. + * @param arid of the address range + * @param ip the ip to hold + * @return 0 on success + */ + int hold_by_ip(unsigned int arid, const string& ip); + + /** + * Holds an address from the first address range containing the MAC + * @param mac the mac to hold + * @return 0 on success + */ + int hold_by_ip(const string& ip); + + /** + * Holds an address from the specified address range. + * @param arid of the address range + * @param mac the mac to hold + * @return 0 on success + */ + int hold_by_mac(unsigned int arid, const string& mac); + + /** + * Holds an address from the first address range containing the MAC + * @param mac the mac to hold + * @return 0 on success + */ + int hold_by_mac(const string& mac); + /** * Frees the given address by MAC * @param arid the ID of the address range diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 753cad0107..aa3465001e 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -265,11 +265,15 @@ void AddressRange::to_xml(ostringstream &oss) const if (it->second & PoolObjectSQL::VM) { - lease.replace("VM", it->second & 0x00000000FFFFFFFFLL); + int vmid = it->second & 0x00000000FFFFFFFFLL; + + lease.replace("VM", vmid); } else if (it->second & PoolObjectSQL::NET) { - lease.replace("VNET", it->second & 0x00000000FFFFFFFFLL); + int vnid = it->second & 0x00000000FFFFFFFFLL; + + lease.replace("VNET", vnid); } lease.to_xml(oss); @@ -606,8 +610,9 @@ int AddressRange::attr_to_allocated(const string& allocated_s) void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, unsigned int addr_index) { + long long lobid = obid & 0x00000000FFFFFFFFLL; - allocated.insert(make_pair(addr_index,ot|obid)); + allocated.insert(make_pair(addr_index,ot|lobid)); used_addr++; @@ -793,6 +798,69 @@ int AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, return -1; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::hold_by_ip(const string& ip_s) +{ + if (!(type & 0x00000002))//Not of type IP4 or IP4_6 + { + return -1; + } + + unsigned int ip_i; + + if (ip_to_i(ip_s, ip_i) == -1) + { + return -1; + } + + if (ip_i < ip) + { + return -1; + } + + unsigned int index = ip_i - ip; + + if (allocated.count(index) != 0 || index >= size ) + { + return -1; + } + + allocate_addr(PoolObjectSQL::VM, -1, index); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::hold_by_mac(const string& mac_s) +{ + unsigned int mac_i[2]; + + if (mac_to_i(mac_s, mac_i) == -1) + { + return -1; + } + + if ((mac_i[1] != mac[1]) || (mac_i[0] < mac[0])) + { + return -1; + } + + unsigned int index = mac_i[0] - mac[0]; + + if ((allocated.count(index) != 0) || (index >= size)) + { + return -1; + } + + allocate_addr(PoolObjectSQL::VM, -1, index); + + return 0; +} + /* ************************************************************************** */ /* ************************************************************************** */ diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 30a17b4873..f4a614d1de 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -236,3 +236,73 @@ void AddressRangePool::get_attribute(const char * name, string& value, value = it->second->get_attribute(name); } } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::hold_by_ip(unsigned int ar_id, const string& ip_s) +{ + map::const_iterator it; + + it = ar_pool.find(ar_id); + + if (it == ar_pool.end()) + { + return -1; + } + + return it->second->hold_by_ip(ip_s); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::hold_by_ip(const string& ip_s) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if (it->second->hold_by_ip(ip_s) == 0) + { + return 0; + } + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::hold_by_mac(unsigned int ar_id, const string& mac_s) +{ + map::iterator it; + + it = ar_pool.find(ar_id); + + if (it == ar_pool.end()) + { + return -1; + } + + return it->second->hold_by_mac(mac_s); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::hold_by_mac(const string& mac_s) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if (it->second->hold_by_mac(mac_s) == 0) + { + return 0; + } + } + + return -1; +} diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 72e49d17fd..ec558f5f01 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -621,24 +621,133 @@ int VirtualNetwork::remove_leases(VirtualNetworkTemplate * leases_template, int VirtualNetwork::hold_leases(VirtualNetworkTemplate * leases_template, string& error_msg) { - vector vector_leases; + vector vleases; + const VectorAttribute * lease = 0; - leases_template->get("LEASES", vector_leases); - //TODO - return 0; - //return leases->hold_leases(vector_leases, error_msg); + if (leases_template->get("LEASES", vleases) <= 0) + { + error_msg = "Empty lease description."; + return -1; + } + + lease = dynamic_cast(vleases[0]); + + if ( lease == 0 ) + { + error_msg = "Empty lease description."; + return -1; + } + + int rc = -1; + + unsigned int ar_id; + + string ip = lease->vector_value("IP"); + string mac = lease->vector_value("MAC"); + + if (ip.empty() && mac.empty()) + { + error_msg = "Missing MAC or IP."; + return -1; + } + + if (lease->vector_value("AR_ID", ar_id) != 0) //No AR__ID hold fist address + { + if (!mac.empty()) + { + rc = ar_pool.hold_by_mac(mac); + } + else if (!ip.empty()) + { + rc = ar_pool.hold_by_ip(ip); + } + } + else + { + if (!mac.empty()) + { + rc = ar_pool.hold_by_mac(ar_id, mac); + } + else if (!ip.empty()) + { + rc = ar_pool.hold_by_ip(ar_id, ip); + } + } + + if (rc!=0) + { + error_msg = "Address is not part of the address range or in use."; + } + + return rc; } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ int VirtualNetwork::free_leases(VirtualNetworkTemplate * leases_template, - string& error_msg) + string& error_msg) { + + vector vleases; + const VectorAttribute * lease = 0; + + if (leases_template->get("LEASES", vleases) <= 0) + { + error_msg = "Empty lease description."; + return -1; + } + + lease = dynamic_cast(vleases[0]); + + if ( lease == 0 ) + { + error_msg = "Empty lease description."; + return -1; + } + + int rc; + + unsigned int ar_id; + + string ip = lease->vector_value("IP"); + string mac = lease->vector_value("MAC"); + + if (lease->vector_value("AR_ID", ar_id) != 0) + { + error_msg = "Missing address range ID (AR_ID)."; + return -1; + } + + if (!mac.empty()) + { + rc = ar_pool.hold_by_mac(ar_id, mac); + } + else if (!ip.empty()) + { + rc = ar_pool.hold_by_ip(ar_id, ip); + } + else + { + error_msg = "Missing MAC or IP."; + return -1; + } + + if (rc!=0) + { + error_msg = "Address is not part of the address range or in use."; + } + + return rc; + + + vector vector_leases; leases_template->get("LEASES", vector_leases); + + //TODO return 0; //return leases->free_leases(vector_leases, error_msg); From e6301350d05c7d375f99f8c05c30bc9f3c446a90 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 19 May 2014 17:59:06 +0200 Subject: [PATCH 15/84] feature #2858: Hold and Release for IP/MAC addresses in all Address Ranges. Keep compatibility with 4.6 interface --- include/AddressRange.h | 9 ++++ include/AddressRangePool.h | 32 +++++++++++++- src/vnm/AddressRange.cc | 34 ++++++++++++++- src/vnm/AddressRangePool.cc | 87 +++++++++++++++++++++++++++++++++---- src/vnm/VirtualNetwork.cc | 53 ++++++++++------------ 5 files changed, 173 insertions(+), 42 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index 02f0d5d151..cda667500c 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -168,6 +168,15 @@ public: */ int free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); + /** + * Frees a previous allocated address, referenced by its IP address + * @param ot the object type of the owner of the address + * @param obid the id of the owner of the address + * @param ip the IP address in string form + * @return 0 if the address was freed + */ + int free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const string& ip); + /** * Return the id for this address range */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index bee22ba2ce..80a8497ac0 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -128,7 +128,7 @@ public: int hold_by_mac(const string& mac); /** - * Frees the given address by MAC + * Frees the given address by MAC on the given address range * @param arid the ID of the address range * @param ot the type of the object requesting the address (VM or NET) * @param obid the id of the object requesting the address @@ -137,6 +137,36 @@ public: void free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot, int obid, const string& mac); + /** + * Frees the given address by IP on the given address range + * @param arid the ID of the address range + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + * @param ip the specific IP address requested + */ + void free_addr_by_ip(unsigned int arid, PoolObjectSQL::ObjectType ot, + int obid, const string& ip); + + /** + * Frees the given address by MAC from all address ranges containing + * the MAC + * @param arid the ID of the address range + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + * @param mac the specific MAC address requested + */ + void free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); + + /** + * Frees the given address by IP from all address ranges containing + * the IP + * @param arid the ID of the address range + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + * @param ip the specific IP address requested + */ + void free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const string& ip); + /** * Return the number of used addresses */ diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index aa3465001e..b2cf9cccf1 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -624,11 +624,13 @@ void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid, int AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, unsigned int addr_index) { + long long lobid = obid & 0x00000000FFFFFFFFLL; + map::iterator it; it = allocated.find(addr_index); - if (it != allocated.end() && it->second == (ot|obid)) + if (it != allocated.end() && it->second == (ot|lobid)) { allocated.erase(it); allocated_to_attr(); @@ -790,7 +792,35 @@ int AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, unsigned int index = mac_i[0] - mac[0]; - if ((mac_i[0] >= mac[0]) && (index < size)) + if ((0 <= index) && (index < size)) + { + return free_addr(ot, obid, index); + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::free_addr_by_ip(PoolObjectSQL::ObjectType ot, int obid, + const string& ip_s) +{ + if (!(type & 0x00000002))//Not of type IP4 or IP4_6 + { + return -1; + } + + unsigned int ip_i; + + if (ip_to_i(ip_s, ip_i) == -1) + { + return -1; + } + + unsigned int index = ip_i - ip; + + if ((0 <= index ) && (index < size)) { return free_addr(ot, obid, index); } diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index f4a614d1de..0ffd7a9e3f 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -222,6 +222,59 @@ void AddressRangePool::free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void AddressRangePool::free_addr_by_ip(unsigned int arid, + PoolObjectSQL::ObjectType ot, int obid, const string& ip) +{ + map::iterator it; + + it = ar_pool.find(arid); + + if (it!=ar_pool.end()) + { + if ( it->second->free_addr_by_ip(ot, obid, ip) == 0 ) + { + used_addr--; + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AddressRangePool::free_addr(PoolObjectSQL::ObjectType ot, int obid, + const string& mac_s) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if (it->second->free_addr(ot, obid, mac_s) == 0) + { + used_addr--; + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +void AddressRangePool::free_addr_by_ip(PoolObjectSQL::ObjectType ot, int obid, + const string& ip_s) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if (it->second->free_addr_by_ip(ot, obid, ip_s) == 0) + { + used_addr--; + } + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void AddressRangePool::get_attribute(const char * name, string& value, int ar_id) const { @@ -251,7 +304,14 @@ int AddressRangePool::hold_by_ip(unsigned int ar_id, const string& ip_s) return -1; } - return it->second->hold_by_ip(ip_s); + int rc = it->second->hold_by_ip(ip_s); + + if (rc == 0) + { + used_addr++; + } + + return rc; } /* -------------------------------------------------------------------------- */ @@ -260,16 +320,18 @@ int AddressRangePool::hold_by_ip(unsigned int ar_id, const string& ip_s) int AddressRangePool::hold_by_ip(const string& ip_s) { map::iterator it; + int rc = -1; for (it=ar_pool.begin(); it!=ar_pool.end(); it++) { - if (it->second->hold_by_ip(ip_s) == 0) + if (it->second->hold_by_ip(ip_s) == 0) //At least one AR hold the IP { - return 0; + used_addr++; + rc = 0; } } - return -1; + return rc; } /* -------------------------------------------------------------------------- */ @@ -286,7 +348,14 @@ int AddressRangePool::hold_by_mac(unsigned int ar_id, const string& mac_s) return -1; } - return it->second->hold_by_mac(mac_s); + int rc = it->second->hold_by_mac(mac_s); + + if (rc == 0) + { + used_addr++; + } + + return rc; } /* -------------------------------------------------------------------------- */ @@ -295,14 +364,16 @@ int AddressRangePool::hold_by_mac(unsigned int ar_id, const string& mac_s) int AddressRangePool::hold_by_mac(const string& mac_s) { map::iterator it; + int rc = -1; for (it=ar_pool.begin(); it!=ar_pool.end(); it++) { - if (it->second->hold_by_mac(mac_s) == 0) + if (it->second->hold_by_mac(mac_s) == 0) //At least one AR hold the IP { - return 0; + used_addr++; + rc = 0; } } - return -1; + return rc; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index ec558f5f01..1ac5f07dbd 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -651,7 +651,7 @@ int VirtualNetwork::hold_leases(VirtualNetworkTemplate * leases_template, return -1; } - if (lease->vector_value("AR_ID", ar_id) != 0) //No AR__ID hold fist address + if (lease->vector_value("AR_ID", ar_id) != 0) //No AR_ID hold all addresses { if (!mac.empty()) { @@ -706,51 +706,42 @@ int VirtualNetwork::free_leases(VirtualNetworkTemplate * leases_template, return -1; } - int rc; - unsigned int ar_id; string ip = lease->vector_value("IP"); string mac = lease->vector_value("MAC"); - if (lease->vector_value("AR_ID", ar_id) != 0) - { - error_msg = "Missing address range ID (AR_ID)."; - return -1; - } - if (!mac.empty()) - { - rc = ar_pool.hold_by_mac(ar_id, mac); - } - else if (!ip.empty()) - { - rc = ar_pool.hold_by_ip(ar_id, ip); - } - else + if (ip.empty() && mac.empty()) { error_msg = "Missing MAC or IP."; return -1; } - if (rc!=0) + if (lease->vector_value("AR_ID", ar_id) != 0) //No AR_ID free all addresses { - error_msg = "Address is not part of the address range or in use."; + if (!mac.empty()) + { + ar_pool.free_addr(PoolObjectSQL::VM, -1, mac); + } + else if (!ip.empty()) + { + ar_pool.free_addr_by_ip(PoolObjectSQL::VM, -1, ip); + } + } + else + { + if (!mac.empty()) + { + ar_pool.free_addr(ar_id, PoolObjectSQL::VM, -1, mac); + } + else if (!ip.empty()) + { + ar_pool.free_addr_by_ip(ar_id, PoolObjectSQL::VM, -1, ip); + } } - return rc; - - - - vector vector_leases; - - leases_template->get("LEASES", vector_leases); - - - - //TODO return 0; - //return leases->free_leases(vector_leases, error_msg); } /* -------------------------------------------------------------------------- */ From 48c19b2681720df1c9321522e399cefc2b41e7b5 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 19 May 2014 19:18:56 +0200 Subject: [PATCH 16/84] feature #2858: Updated OCA interface for VirtualNetwork objects. Added option to select an address range to hold/release an IP address --- src/cli/one_helper/onevnet_helper.rb | 26 ++++--- src/cli/onevnet | 25 +++++-- src/oca/ruby/opennebula/virtual_network.rb | 80 ++++++++++++++-------- 3 files changed, 87 insertions(+), 44 deletions(-) diff --git a/src/cli/one_helper/onevnet_helper.rb b/src/cli/one_helper/onevnet_helper.rb index a8db52d5ab..c2bdc898a2 100644 --- a/src/cli/one_helper/onevnet_helper.rb +++ b/src/cli/one_helper/onevnet_helper.rb @@ -18,6 +18,22 @@ require 'one_helper' require 'one_helper/onevm_helper' class OneVNetHelper < OpenNebulaHelper::OneHelper + AR = { + :name => "ar_id", + :short => "-a ar_id", + :large => "--address_range ar_id", + :format => Integer, + :description => "ID of the address range" + } + + MAC = { + :name => "mac", + :short => "-m mac", + :large => "--mac mac", + :format => String, + :description => "MAC address" + } + def self.rname "VNET" end @@ -26,12 +42,6 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper "onevnet.yaml" end - def self.type_to_str(id) - id = id.to_i - type_str = VirtualNetwork::VN_TYPES[id] - return VirtualNetwork::SHORT_VN_TYPES[type_str] - end - def format_pool(options) config_file = self.class.table_conf @@ -59,10 +69,6 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper OpenNebulaHelper.cluster_str(d["CLUSTER"]) end - column :TYPE, "Type of Virtual Network", :size=>6 do |d| - OneVNetHelper.type_to_str(d["TYPE"]) - end - column :SIZE, "Size of the Virtual Network", :size=>5 do |d| d["SIZE"] end diff --git a/src/cli/onevnet b/src/cli/onevnet index 4c5ee447e2..886dcbaf66 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -47,6 +47,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do set :option, CommandParser::OPTIONS+OpenNebulaHelper::CLIENT_OPTIONS CREATE_OPTIONS = [OneClusterHelper::CLUSTER] + STD_OPTIONS = CommandParser::OPTIONS + OpenNebulaHelper::CLIENT_OPTIONS ######################################################################## # Formatters for arguments @@ -127,9 +128,17 @@ cmd=CommandParser::CmdParser.new(ARGV) do Holds a Virtual Network lease, marking it as used EOT - command :hold, hold_desc, :vnetid, :ip do + command :hold, hold_desc, :vnetid, :ip, + :options=>STD_OPTIONS + [OneVNetHelper::MAC, OneVNetHelper::AR] do helper.perform_action(args[0],options,"lease on hold") do |vn| - vn.hold(args[1]) + ar = options[:ar_id] || -1 + mac = options[:mac] || "" + + if mac.empty? + vn.hold(args[1], ar) + else + vn.hold_by_mac(mac, ar) + end end end @@ -137,9 +146,17 @@ cmd=CommandParser::CmdParser.new(ARGV) do Releases a Virtual Network lease on hold EOT - command :release, release_desc, :vnetid, :ip do + command :release, release_desc, :vnetid, :ip, + :options=>STD_OPTIONS + [OneVNetHelper::MAC, OneVNetHelper::AR] do helper.perform_action(args[0],options,"lease released") do |vn| - vn.release(args[1]) + ar = options[:ar_id] || -1 + mac = options[:mac] || "" + + if mac.empty? + vn.release(args[1], ar) + else + vn.release_by_mac(mac, ar) + end end end diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index 11ac90feb8..c7ca4cd36e 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -38,13 +38,6 @@ module OpenNebula :rename => "vn.rename" } - VN_TYPES=%w{RANGED FIXED} - - SHORT_VN_TYPES={ - "RANGED" => "R", - "FIXED" => "F" - } - # Creates a VirtualNetwork description with just its identifier # this method should be used to create plain VirtualNetwork objects. # +id+ the id of the network @@ -142,12 +135,16 @@ module OpenNebula return rc end - # Holds a virtual network Lease as used + # Holds a virtual network address # @param ip [String] IP to hold - def hold(ip) + # @param ar_id [Integer] The address range to hold the lease. If not set + # the lease will be held from all possible address ranges + def hold(ip, ar_id=-1) return Error.new('ID not defined') if !@pe_id - lease_template = "LEASES = [ IP = #{ip} ]" + lease_template = "LEASES = [ IP = #{ip}" + lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 + lease_template << "]" rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template) rc = nil if !OpenNebula.is_error?(rc) @@ -155,12 +152,50 @@ module OpenNebula return rc end - # Releases a virtual network Lease on hold - # @param ip [String] IP to release - def release(ip) + # Holds a virtual network address by its MAC + # @param mac [String] MAC to hold + # @param ar_id [Integer] The address range to hold the lease. If not set + # the lease will be held from all possible address ranges + def hold_by_mac(mac, ar_id=-1) return Error.new('ID not defined') if !@pe_id - lease_template = "LEASES = [ IP = #{ip} ]" + lease_template = "LEASES = [ MAC = #{mac}" + lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 + lease_template << "]" + + rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + + # Releases an address on hold + # @param ip [String] IP to release + # @param ar_id [Integer] The address range to release the lease. If not + # set the lease will be freed from all possible address ranges + def release(ip, ar_id=-1) + return Error.new('ID not defined') if !@pe_id + + lease_template = "LEASES = [ IP = #{ip}" + lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 + lease_template << "]" + + rc = @client.call(VN_METHODS[:release], @pe_id, lease_template) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + + # Releases an addres on hold by its MAC + # @param mac [String] MAC to release + # @param ar_id [Integer] The address range to release the lease. If not + # set the lease will be freed from all possible address ranges + def release_by_mac(mac, ar_id=-1) + return Error.new('ID not defined') if !@pe_id + + lease_template = "LEASES = [ MAC = #{mac}" + lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 + lease_template << "]" rc = @client.call(VN_METHODS[:release], @pe_id, lease_template) rc = nil if !OpenNebula.is_error?(rc) @@ -198,7 +233,7 @@ module OpenNebula super(VN_METHODS[:chmod], owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u, other_m, other_a) end - + # Renames this virtual network # # @param name [String] New name for the virtual network. @@ -219,21 +254,6 @@ module OpenNebula self['GID'].to_i end - # Returns the type of the Virtual Network (numeric value) - def type - self['TYPE'].to_i - end - - # Returns the type of the Virtual Network (string value) - def type_str - VN_TYPES[type] - end - - # Returns the state of the Virtual Network (string value) - def short_type_str - SHORT_VN_TYPES[type_str] - end - def public? if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1" true From c92434e920f5090dc7c6cccb901fa06342b97c63 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 19 May 2014 19:28:11 +0200 Subject: [PATCH 17/84] feature #2858: Add support for MAC addresses at OCA and CLI --- src/cli/one_helper/onevnet_helper.rb | 8 ---- src/cli/onevnet | 18 ++------ src/oca/ruby/opennebula/virtual_network.rb | 50 ++++++---------------- 3 files changed, 18 insertions(+), 58 deletions(-) diff --git a/src/cli/one_helper/onevnet_helper.rb b/src/cli/one_helper/onevnet_helper.rb index c2bdc898a2..51b3821a41 100644 --- a/src/cli/one_helper/onevnet_helper.rb +++ b/src/cli/one_helper/onevnet_helper.rb @@ -26,14 +26,6 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper :description => "ID of the address range" } - MAC = { - :name => "mac", - :short => "-m mac", - :large => "--mac mac", - :format => String, - :description => "MAC address" - } - def self.rname "VNET" end diff --git a/src/cli/onevnet b/src/cli/onevnet index 886dcbaf66..5209e3da5f 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -129,16 +129,11 @@ cmd=CommandParser::CmdParser.new(ARGV) do EOT command :hold, hold_desc, :vnetid, :ip, - :options=>STD_OPTIONS + [OneVNetHelper::MAC, OneVNetHelper::AR] do + :options=>STD_OPTIONS + [OneVNetHelper::AR] do helper.perform_action(args[0],options,"lease on hold") do |vn| ar = options[:ar_id] || -1 - mac = options[:mac] || "" - if mac.empty? - vn.hold(args[1], ar) - else - vn.hold_by_mac(mac, ar) - end + vn.hold(args[1], ar) end end @@ -147,16 +142,11 @@ cmd=CommandParser::CmdParser.new(ARGV) do EOT command :release, release_desc, :vnetid, :ip, - :options=>STD_OPTIONS + [OneVNetHelper::MAC, OneVNetHelper::AR] do + :options=>STD_OPTIONS + [OneVNetHelper::AR] do helper.perform_action(args[0],options,"lease released") do |vn| ar = options[:ar_id] || -1 - mac = options[:mac] || "" - if mac.empty? - vn.release(args[1], ar) - else - vn.release_by_mac(mac, ar) - end + vn.release(args[1], ar) end end diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index c7ca4cd36e..7e914402fe 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -136,30 +136,19 @@ module OpenNebula end # Holds a virtual network address - # @param ip [String] IP to hold + # @param ip [String] address to hold, if contains ":" a MAC address is assumed # @param ar_id [Integer] The address range to hold the lease. If not set # the lease will be held from all possible address ranges def hold(ip, ar_id=-1) return Error.new('ID not defined') if !@pe_id - lease_template = "LEASES = [ IP = #{ip}" - lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 - lease_template << "]" + if ip.include?':' + addr_name = "MAC" + else + addr_name = "IP" + end - rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template) - rc = nil if !OpenNebula.is_error?(rc) - - return rc - end - - # Holds a virtual network address by its MAC - # @param mac [String] MAC to hold - # @param ar_id [Integer] The address range to hold the lease. If not set - # the lease will be held from all possible address ranges - def hold_by_mac(mac, ar_id=-1) - return Error.new('ID not defined') if !@pe_id - - lease_template = "LEASES = [ MAC = #{mac}" + lease_template = "LEASES = [ #{addr_name} = #{ip}" lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 lease_template << "]" @@ -170,30 +159,19 @@ module OpenNebula end # Releases an address on hold - # @param ip [String] IP to release + # @param ip [String] IP to release, if contains ":" a MAC address is assumed # @param ar_id [Integer] The address range to release the lease. If not # set the lease will be freed from all possible address ranges def release(ip, ar_id=-1) return Error.new('ID not defined') if !@pe_id - lease_template = "LEASES = [ IP = #{ip}" - lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 - lease_template << "]" + if ip.include?':' + addr_name = "MAC" + else + addr_name = "IP" + end - rc = @client.call(VN_METHODS[:release], @pe_id, lease_template) - rc = nil if !OpenNebula.is_error?(rc) - - return rc - end - - # Releases an addres on hold by its MAC - # @param mac [String] MAC to release - # @param ar_id [Integer] The address range to release the lease. If not - # set the lease will be freed from all possible address ranges - def release_by_mac(mac, ar_id=-1) - return Error.new('ID not defined') if !@pe_id - - lease_template = "LEASES = [ MAC = #{mac}" + lease_template = "LEASES = [ #{addr_name} = #{ip}" lease_template << ", AR_ID = #{ar_id}" if ar_id != -1 lease_template << "]" From 8c92f9b1a78d97796b2d8168193bfabeeb41d6aa Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 19 May 2014 19:41:48 +0200 Subject: [PATCH 18/84] feature #2858: Remove old code --- include/FixedLeases.h | 159 ---------- include/Leases.h | 382 ----------------------- include/RangedLeases.h | 148 --------- include/VirtualNetwork.h | 1 - src/vnm/FixedLeases.cc | 469 ---------------------------- src/vnm/Leases.cc | 632 -------------------------------------- src/vnm/RangedLeases.cc | 556 --------------------------------- src/vnm/SConstruct | 3 - src/vnm/VirtualNetwork.cc | 2 - 9 files changed, 2352 deletions(-) delete mode 100644 include/FixedLeases.h delete mode 100644 include/Leases.h delete mode 100644 include/RangedLeases.h delete mode 100644 src/vnm/FixedLeases.cc delete mode 100644 src/vnm/Leases.cc delete mode 100644 src/vnm/RangedLeases.cc diff --git a/include/FixedLeases.h b/include/FixedLeases.h deleted file mode 100644 index dae1e947a0..0000000000 --- a/include/FixedLeases.h +++ /dev/null @@ -1,159 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#ifndef FIXED_LEASES_H_ -#define FIXED_LEASES_H_ - -#include "Leases.h" - -using namespace std; - -/** - * The FixedLeases class represents a pool of fixed IP-MAC leases. A lease can - * be either a IP (the MAC is then PREFIX:IP) or the IP and MAC addresses. The - * pool is read from a template file, each lease is in the form: - * LEASE = [ IP = "", MAC = ""] - */ -class FixedLeases : public Leases -{ -public: - /** - * Create a FixedLeases from template - */ - FixedLeases(SqlDB * db, - int _oid, - unsigned int _mac_prefix, - unsigned int _global[], - unsigned int _site[], - vector& vector_leases); - /** - * Create a plain FixedLeases, you can populate the lease pool using - * select() - */ - FixedLeases(SqlDB * db, - int _oid, - unsigned int _mac_prefix, - unsigned int _global[], - unsigned int _site[]): - Leases(db,_oid,0,_mac_prefix,_global,_site), - current(leases.begin()){}; - - ~FixedLeases(){}; - - /** - * Returns an unused lease, which becomes used - * @param vid identifier of the VM getting this lease - * @param ip ip of the returned lease - * @param mac mac of the returned lease - * @return 0 if success - */ - int get(int vid, string& ip, string& mac, unsigned int eui64[]); - - /** - * Ask for a specific lease in the network - * @param vid identifier of the VM getting this lease - * @param ip ip of lease requested - * @param mac mac of the lease - * @return 0 if success - */ - int set(int vid, const string& ip, string& mac, unsigned int eui64[]); - - /** - * Release an used lease, which becomes unused - * @param ip of the lease in use - */ - void release(const string& ip) - { - unset(ip); - } - - /** - * Adds New leases. - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - int add_leases(vector& leases, string& error_msg); - - /** - * Removes leases; if they are not used. - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE. - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - int remove_leases(vector& leases, string& error_msg); - - /** - * Loads the leases from the DB. - */ - int select(SqlDB * db) - { - //Read the leases from the DB - int rc = Leases::select(db); - //Update the size - size = leases.size(); - - return rc; - } - -private: - /** - * Current lease pointer - */ - map::iterator current; - - /** - * Add a lease, from the Lease interface - * @param ip ip of the lease - * @param mac mac of the lease - * @param vid identifier of the VM getting this lease - * @param error_msg If the action fails, this message contains the reason. - * @param used Flag to insert the lease as used. - * @return 0 if success - */ - int add(const string& ip, - const string& mac, - int vid, - string& error_msg, - bool used=true); - - /** - * Remove an existing lease, if it is not in use. - * @param ip ip of the lease - * @param error_msg If the action fails, this message contains the reason - */ - int remove(const string& ip, string& error_msg); - - /** - * Sets a lease as not used, from the Lease interface - * @param ip ip of the lease to be deleted - * @return 0 if success - */ - int unset(const string& ip); - - /** - * Updates the DB entry for this lease - * @param lease Lease to be updated - * @return 0 if success - */ - int update_lease(Lease * lease); -}; - -#endif /*FIXED_LEASES_H_*/ diff --git a/include/Leases.h b/include/Leases.h deleted file mode 100644 index d5760ad112..0000000000 --- a/include/Leases.h +++ /dev/null @@ -1,382 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#ifndef LEASES_H_ -#define LEASES_H_ - -#include "ObjectSQL.h" -#include "ObjectXML.h" -#include "Attribute.h" - -#include -#include -#include - -using namespace std; - -/** - * The Leases class represents all the IP and MAC addresses (lease) that can - * be asigned (or are asigned) in a Virtual Network - */ -class Leases : public ObjectSQL -{ -public: - /** - * Creates a new Lease set for a given Virtual Network (oid) - * @param _db pinter to the DB object used to store the leases - * @param _oid the virtual network unique identifier - * @param _size the max number of leases - */ - Leases(SqlDB * _db, int _oid, unsigned long _size, unsigned int _mac_prefix, - unsigned int _global[], unsigned int _site[]): - ObjectSQL(), oid(_oid), size(_size), n_used(0), mac_prefix(_mac_prefix) - , db(_db) - { - global[1] = _global[1]; - global[0] = _global[0]; - - site[1] = _site[1]; - site[0] = _site[0]; - }; - - virtual ~Leases() - { - map::iterator it; - - for(it=leases.begin();it!=leases.end();it++) - { - delete it->second; - } - }; - - friend ostream& operator<<(ostream& os, Leases& _leases); - - /** - * Returns an unused lease, which becomes used - * @param vid identifier of the VM getting this lease - * @param ip ip of the returned lease - * @param mac mac of the returned lease - * @param eui64 extended unique identifier - * @return 0 if success - */ - virtual int get(int vid, string& ip, string& mac, unsigned int *eui64) = 0; - - /** - * Ask for a specific lease in the network - * @param vid identifier of the VM getting this lease - * @param ip ip of lease requested - * @param mac mac of the lease - * @param eui64 extended unique identifier - * @return 0 if success - */ - virtual int set(int vid, const string& ip, string& mac, unsigned int *eui64) = 0; - - /** - * Release an used lease, which becomes unused - * @param ip of the lease in use - */ - virtual void release(const string& ip) = 0; - - /** - * Adds New leases. (Only implemented for FIXED networks) - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE. - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - virtual int add_leases(vector& vector_leases, - string& error_msg) = 0; - - /** - * Removes leases; if they are not used. (Only available for FIXED networks) - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE. - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - virtual int remove_leases(vector& vector_leases, - string& error_msg) = 0; - - /** - * Holds a Lease, marking it as used - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE. - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - int hold_leases(vector& vector_leases, string& error_msg); - - /** - * Releases a Lease on hold - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE. - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - int free_leases(vector& vector_leases, string& error_msg); - -protected: - /** - * The Lease class, it represents a pair of IP and MAC assigned to - * a Virtual Machine - */ - class Lease : public ObjectXML - { - public: - /** - * Creates a new lease, numeric form. - * @param _ip, the Lease IP in numeric format - * @param _mac, the Lease MAC in numeric format - * @param _vid, the ID of the VM owning the lease - * @param _used, the lease is in use - */ - Lease(unsigned int _ip, - unsigned int _mac[], - int _vid, - bool _used) - :ObjectXML(),ip(_ip), vid(_vid), used(_used) - { - mac[1]=_mac[1]; - mac[0]=_mac[0]; - - mac_to_eui64(mac, eui64); - }; - - /** - * Creates a new empty lease. Method from_xml should be called right - * after this constructor. - */ - Lease():ObjectXML(){}; - - ~Lease(){}; - - /** - * Conversion from string IP to unsigned int IP - * @return 0 if success - */ - static int ip_to_number(const string& ip, unsigned int& i_ip); - - /** - * Conversion from unsigned int IP to string IP - */ - static void ip_to_string(const unsigned int i_ip, string& ip); - - /** - * Generates IPv6 strings based on the modified EUI64 and global and - * site network prefixes. - */ - static void ip6_to_string(const unsigned int eui64[], - const unsigned int prefix[], - string& ip6s); - /** - * Conversion from string MAC to unsigned int[] MAC - * @return 0 if success - */ - static int mac_to_number(const string& mac, unsigned int i_mac[]); - - /** - * Generates the modified extended unique identifier based on MAC - * @param i_mac the MAC address - * @param i_meui64 the modified EUI64 - */ - static void mac_to_eui64(const unsigned int mac[], unsigned int eui64[]); - - /** - * Conversion from string IP to unsigned int IP - */ - static void mac_to_string(const unsigned int i_mac[], string& mac); - - /** - * Conversion from string IP to unsigned int IP - */ - static int prefix6_to_number(const string& prefix, unsigned int ip[]); - - /** - * Function to print the Lease object into a string in - * XML format - * @param xml the resulting XML string - * @return a reference to the generated string - */ - string& to_xml(string& xml, - const unsigned int global[], - const unsigned int site[]) const; - /** - * Function to print the Lease object into a string in - * XML format. The output contains all the internal attributes, - * to be saved in the DB as a blob - * @param xml the resulting XML string - * @return a reference to the generated string - */ - string& to_xml_db(string& xml) const; - - /** - * Rebuilds the object from an xml formatted string - * @param xml_str The xml-formatted string - * - * @return 0 on success, -1 otherwise - */ - int from_xml(const string &xml_str); - - unsigned int ip; - - unsigned int mac[2]; - - unsigned int eui64[2]; - - int vid; - - bool used; - }; - - friend class VirtualNetwork; - friend class VirtualNetworkPool; - - // ------------------------------------------------------------------------- - // Leases fields - // ------------------------------------------------------------------------- - /** - * Leases identifier. Connects it to a Virtual Network - */ - int oid; - - /** - * Number of possible leases (free + assigned) - */ - unsigned int size; - - /** - * Hash of leases, indexed by lease.ip - */ - map leases; - - /** - * Number of used leases - */ - int n_used; - - /** - * The default MAC prefix for the Leases - */ - unsigned int mac_prefix; - - /** - * Global prefix for IPv6 addresses (64 upper bits) - */ - unsigned int global[2]; - - /** - * Global prefix for IPv6 addresses (64 upper bits) - */ - unsigned int site[2]; - - // ------------------------------------------------------------------------- - // DataBase implementation variables - // ------------------------------------------------------------------------- - /** - * Pointer to the DataBase - */ - SqlDB * db; - - static const char * table; - - static const char * db_names; - - static const char * db_bootstrap; - - // ------------------------------------------------------------------------- - // Leases methods - // ------------------------------------------------------------------------- - - /** - * Check if the passed ip corresponds with a given lease - * @param ip of the lease to be checked - * @return true if the ip was already assigned - */ - bool check(const string& ip); - - /** - * Check if the passed ip corresponds with a given lease - * @param ip of the lease to be checked - * @return true if the ip was already assigned - */ - bool check(unsigned int ip); - - /** - * Check if a VM is the owner of the ip - * @param ip of the lease to be checked - * @param vid the ID of the VM - * @return true if the ip was already assigned - */ - bool is_owner(const string& ip, int vid); - - /** - * Reads the leases from the DB, and updates the lease hash table - * @param db pointer to the database. - * @return 0 on success. - */ - virtual int select(SqlDB * db); - - friend ostream& operator<<(ostream& os, Lease& _lease); - - /** - * Function to print the Leases object into a string in - * XML format - * @param xml the resulting XML string - * @return a reference to the generated string - */ - string& to_xml(string& xml) const; - -private: - /** - * Callback function to unmarshall a Lease object (Lease::select) - * @param num the number of columns read from the DB - * @para names the column names - * @para vaues the column values - * @return 0 on success - */ - int select_cb(void *nil, int num, char **values, char **names); - - /** - * This method should not be called, leases are added/removed/updated - * through add/del interface - * @param db pointer to the database. - * @return 0 on success. - */ - int insert(SqlDB * db, string& error_str); - - /** - * Leases are added/removed/updated through add/del interface - * This method is for pool management. - * @param db pointer to the database. - * @return 0 on success. - */ - int drop(SqlDB * db); - - /** - * This method should not be called, leases are added/removed/updated - * through add/del interface - * @param db pointer to the database. - * @return 0 on success. - */ - int update(SqlDB * db); -}; - -#endif /*LEASES_H_*/ - diff --git a/include/RangedLeases.h b/include/RangedLeases.h deleted file mode 100644 index b71dd37770..0000000000 --- a/include/RangedLeases.h +++ /dev/null @@ -1,148 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#ifndef RANGED_LEASES_H_ -#define RANGED_LEASES_H_ - -#include "Leases.h" -#include "VirtualNetwork.h" - -using namespace std; - -class RangedLeases : public Leases -{ -public: - - // ************************************************************************* - // Constructor - // ************************************************************************* - RangedLeases(SqlDB * db, - int _oid, - unsigned int _mac_prefix, - unsigned int _global[], - unsigned int _site[], - unsigned int _ip_start, - unsigned int _ip_end); - - ~RangedLeases(){}; - - /** - * Reads (and clears) the necessary attributes to define a Ranged VNet - * @param vn Virtual Network - * @param ip_start First IP of the range - * @param ip_end Last IP of the range - * @param error_str Error reason, if any - * @return 0 on success, -1 otherwise - */ - static int process_template(VirtualNetwork * vn, - unsigned int& ip_start, unsigned int& ip_end, string& error_str); - - /** - * Returns an unused lease, which becomes used - * @param vid identifier of the VM getting this lease - * @param ip ip of the returned lease - * @param mac mac of the returned lease - * @return 0 if success - */ - int get(int vid, string& ip, string& mac, unsigned int eui64[]); - - /** - * Ask for a specific lease in the network - * @param vid identifier of the VM getting this lease - * @param ip ip of lease requested - * @param mac mac of the lease - * @return 0 if success - */ - int set(int vid, const string& ip, string& mac, unsigned int eui64[]); - - /** - * Release an used lease, which becomes unused - * @param ip of the lease in use - */ - void release(const string& ip) - { - del(ip); - } - - /** - * Adds New leases. - * Only available for FIXED networks. - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE. - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - int add_leases(vector& vector_leases, string& error_msg) - { - error_msg = "Adding new leases is only supported for FIXED networks."; - return -1; - } - - /** - * Removes leases; if they are not used. - * Only available for FIXED networks. - * @param vector_leases vector of VectorAttribute objects. For the - * moment, the vector can only contain one LEASE. - * @param error_msg If the action fails, this message contains - * the reason. - * @return 0 on success - */ - int remove_leases(vector& vector_leases, string& error_msg) - { - error_msg = "Removing leases is only supported for FIXED networks."; - return -1; - } - - /** - * Loads the leases from the DB. - */ - int select(SqlDB * db) - { - //Read the leases from the DB - int rc = Leases::select(db); - - return rc; - } - -private: - - unsigned int ip_start; - unsigned int ip_end; - - unsigned int current; - - /** - * Add a lease, from the Lease interface - * @param ip ip of the lease - * @param mac mac of the lease - * @param vid identifier of the VM getting this lease - * @return 0 if success - */ - int add(unsigned int ip, unsigned int mac[], int vid, bool used=true); - - /** - * Remove a lease, from the Lease interface - * @param db pointer to DB - * @param ip ip of the lease to be deleted - * @return 0 if success - */ - int del(const string& ip); - -}; - -#endif /*RANGED_LEASES_H_*/ - diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index ed67480245..b6e9ca7f62 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -19,7 +19,6 @@ #include "PoolSQL.h" -#include "Leases.h" #include "VirtualNetworkTemplate.h" #include "Clusterable.h" #include "AddressRangePool.h" diff --git a/src/vnm/FixedLeases.cc b/src/vnm/FixedLeases.cc deleted file mode 100644 index bdb01f6f4e..0000000000 --- a/src/vnm/FixedLeases.cc +++ /dev/null @@ -1,469 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - - -#include "FixedLeases.h" -#include "NebulaLog.h" - -#include - -FixedLeases::FixedLeases( - SqlDB * db, - int _oid, - unsigned int _mac_prefix, - unsigned int _global[], - unsigned int _site[], - vector& vector_leases): - Leases(db,_oid,0,_mac_prefix, _global, _site), current(leases.begin()) -{ - const VectorAttribute * single_attr_lease; - string _mac; - string _ip; - string error_msg; - - for (unsigned long i=0; i < vector_leases.size() ;i++) - { - single_attr_lease = dynamic_cast - (vector_leases[i]); - - if( single_attr_lease ) - { - _ip = single_attr_lease->vector_value("IP"); - _mac = single_attr_lease->vector_value("MAC"); - - if( add(_ip,_mac,-1,error_msg,false) != 0 ) - { - NebulaLog::log("VNM", Log::ERROR, error_msg); - } - } - } - - size = leases.size(); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::add(const string& ip, const string& mac, int vid, - string& error_msg, bool used) -{ - ostringstream oss; - unsigned int _ip; - unsigned int _mac [2]; - - Lease * lease; - string xml_body; - char * sql_xml; - - int rc; - - if (ip.empty() && mac.empty()) - { - goto error_no_ip_mac; - } - - //Set IP & MAC addresses if provided - if (!ip.empty()) - { - if ( Leases::Lease::ip_to_number(ip,_ip) ) - { - goto error_ip; - } - } - - if (!mac.empty()) - { - if (Leases::Lease::mac_to_number(mac,_mac)) - { - goto error_mac; - } - } - - //Generate IP from MAC (or viceversa) - if (ip.empty()) - { - _ip = _mac[0]; - } - - if (mac.empty()) - { - _mac[1] = mac_prefix; - _mac[0] = _ip; - } - - //Check for duplicates - if ( leases.count(_ip) > 0 ) - { - goto error_duplicate; - } - - lease = new Lease(_ip,_mac,vid,used); - - sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str()); - - if ( sql_xml == 0 ) - { - goto error_body; - } - - oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES (" - << oid << "," - << _ip << "," - << "'" << sql_xml << "')"; - - db->free_str(sql_xml); - - rc = db->exec(oss); - - if ( rc != 0 ) - { - goto error_db; - } - - leases.insert( make_pair(_ip,lease) ); - - if(lease->used) - { - n_used++; - } - - current = leases.begin(); - - return rc; - -error_no_ip_mac: - oss << "Both IP and MAC cannot be empty"; - goto error_common; - -error_ip: - oss << "Error inserting lease, malformed IP = " << ip; - goto error_common; - -error_mac: - oss << "Error inserting lease, malformed MAC = " << mac; - goto error_common; - -error_duplicate: - oss << "Error inserting lease, IP " << ip << " already exists"; - goto error_common; - -error_body: - oss << "Error inserting lease, marshall error"; - delete lease; - goto error_common; - -error_db: - oss.str(""); - oss << "Error inserting lease in database."; - delete lease; - -error_common: - NebulaLog::log("VNM", Log::ERROR, oss); - error_msg = oss.str(); - return -1; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::remove(const string& ip, string& error_msg) -{ - map::iterator it; - - ostringstream oss; - unsigned int _ip; - - int rc; - - if( Leases::Lease::ip_to_number(ip,_ip) ) - { - goto error_ip; - } - - it = leases.find(_ip); - - if (it == leases.end()) //it does not exist in the net - { - goto error_notfound; - } - - if (it->second->used && it->second->vid != -1) //it is in use by VM - { - goto error_used; - } - - oss << "DELETE FROM " << table << " WHERE (oid=" << oid - << " AND ip=" << _ip << ")"; - - rc = db->exec(oss); - - if ( rc != 0 ) - { - goto error_db; - } - - delete it->second; - - leases.erase(it); - - current = leases.begin(); - - return rc; - - -error_ip: - oss.str(""); - oss << "Error deleting lease, malformed IP = " << ip; - goto error_common; - -error_notfound: - oss.str(""); - oss << "Error deleting lease, IP " << ip << " is not part of NET " << oid; - goto error_common; - -error_used: - oss.str(""); - oss << "Error deleting lease, IP " << ip << " is currently in use"; - goto error_common; - -error_db: - oss.str(""); - oss << "Error inserting lease in database."; - -error_common: - NebulaLog::log("VNM", Log::ERROR, oss); - error_msg = oss.str(); - return -1; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::unset(const string& ip) -{ - unsigned int _ip; - - map::iterator it_ip; - - if ( Leases::Lease::ip_to_number(ip,_ip) ) - { - return 0; //Wrong format, not leased - } - - it_ip = leases.find(_ip); - - if (it_ip == leases.end()) - { - return 0; //Not in the map, not leased - } - - // Flip used flag to false - it_ip->second->used = false; - it_ip->second->vid = -1; - - // Update the lease - return update_lease(it_ip->second); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::get(int vid, string& ip, string& mac, unsigned int eui64[]) -{ - int rc = -1; - - if (leases.empty()) - { - return -1; - } - - for(unsigned int i=0 ;isecond->used == false) - { - ostringstream oss; - - current->second->used = true; - current->second->vid = vid; - - rc = update_lease(current->second); - - Leases::Lease::mac_to_string(current->second->mac, mac); - Leases::Lease::ip_to_string(current->second->ip, ip); - - eui64[0] = current->second->eui64[0]; - eui64[1] = current->second->eui64[1]; - - current++; - break; - } - } - - return rc; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::set(int vid, const string& ip, string& mac, unsigned int eui64[]) -{ - map::iterator it; - - unsigned int num_ip; - int rc; - - rc = Leases::Lease::ip_to_number(ip,num_ip); - - if (rc != 0) - { - return -1; - } - - it=leases.find(num_ip); - - if (it == leases.end()) //it does not exist in the net - { - return -1; - } - else if (it->second->used) //it is in use - { - return -1; - } - - it->second->used = true; - it->second->vid = vid; - - Leases::Lease::mac_to_string(it->second->mac,mac); - - eui64[0] = it->second->eui64[0]; - eui64[1] = it->second->eui64[1]; - - return update_lease(it->second); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::update_lease(Lease * lease) -{ - ostringstream oss; - string xml_body; - char * sql_xml; - - sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str()); - - if ( sql_xml == 0 ) - { - return -1; - } - - if( lease->used ) - { - n_used++; - } - else - { - n_used--; - } - - oss << "UPDATE " << table << " SET body='" << sql_xml << "'" - << " WHERE oid=" << oid << " AND ip='" << lease->ip <<"'"; - - db->free_str(sql_xml); - - return db->exec(oss); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::add_leases(vector& vector_leases, - string& error_msg) -{ - const VectorAttribute * single_attr_lease = 0; - - int rc = -1; - string _mac; - string _ip; - - if ( vector_leases.size() > 0 ) - { - single_attr_lease = - dynamic_cast(vector_leases[0]); - } - - if( single_attr_lease != 0 ) - { - _ip = single_attr_lease->vector_value("IP"); - _mac = single_attr_lease->vector_value("MAC"); - - rc = add(_ip, _mac, -1, error_msg, false); - - if( rc == 0 ) - { - size = leases.size(); - } - } - else - { - error_msg = "Empty lease description."; - } - - return rc; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int FixedLeases::remove_leases(vector& vector_leases, - string& error_msg) -{ - const VectorAttribute * single_attr_lease = 0; - int rc = -1; - string _ip; - - if ( vector_leases.size() > 0 ) - { - single_attr_lease = - dynamic_cast(vector_leases[0]); - } - - if( single_attr_lease != 0 ) - { - _ip = single_attr_lease->vector_value("IP"); - - rc = remove(_ip, error_msg); - - if( rc == 0 ) - { - size = leases.size(); - } - } - else - { - error_msg = "Empty lease description."; - } - - return rc; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ diff --git a/src/vnm/Leases.cc b/src/vnm/Leases.cc deleted file mode 100644 index e000c69599..0000000000 --- a/src/vnm/Leases.cc +++ /dev/null @@ -1,632 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - -#include "Leases.h" -#include "NebulaLog.h" - -#include - -/* ************************************************************************** */ -/* ************************************************************************** */ -/* Lease class */ -/* ************************************************************************** */ -/* ************************************************************************** */ - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::Lease::ip_to_number(const string& _ip, unsigned int& i_ip) -{ - istringstream iss; - size_t pos=0; - int count = 0; - unsigned int tmp; - - string ip = _ip; - - while ( (pos = ip.find('.')) != string::npos ) - { - ip.replace(pos,1," "); - count++; - } - - if (count != 3) - { - return -1; - } - - iss.str(ip); - - i_ip = 0; - - for (int i=0;i<4;i++) - { - iss >> dec >> tmp >> ws; - - if ( tmp > 255 ) - { - return -1; - } - - i_ip <<= 8; - i_ip += tmp; - } - - return 0; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void Leases::Lease::ip_to_string(const unsigned int i_ip, string& ip) -{ - unsigned int temp_byte; - ostringstream oss; - - // Convert the IP from unsigned int to string - - for (int index=0;index<4;index++) - { - temp_byte = i_ip; - temp_byte >>= (24-index*8); - temp_byte &= 255; - - oss << temp_byte; - - if(index!=3) - { - oss << "."; - } - } - - ip = oss.str(); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void Leases::Lease::ip6_to_string(const unsigned int eui64[], - const unsigned int prefix[], - string& ip6s) -{ - struct in6_addr ip6; - char dst[INET6_ADDRSTRLEN]; - - ip6.s6_addr32[2] = htonl(eui64[1]); - ip6.s6_addr32[3] = htonl(eui64[0]); - - // global or site unicast address - if (prefix[1] != 0 || prefix[0] != 0 ) - { - ip6.s6_addr32[0] = htonl(prefix[1]); - ip6.s6_addr32[1] = htonl(prefix[0]); - } - else // link local address - { - - ip6.s6_addr32[0] = htonl(0xfe800000); - ip6.s6_addr32[1] = 0; - } - - if ( inet_ntop(AF_INET6, &ip6, dst, INET6_ADDRSTRLEN) != 0 ) - { - ip6s = dst; - } -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::Lease::mac_to_number(const string& _mac, unsigned int i_mac[]) -{ - istringstream iss; - size_t pos=0; - int count = 0; - unsigned int tmp; - - string mac = _mac; - - while ( (pos = mac.find(':')) != string::npos ) - { - mac.replace(pos,1," "); - count++; - } - - if (count != 5) - { - return -1; - } - - iss.str(mac); - - i_mac[1] = 0; - i_mac[0] = 0; - - iss >> hex >> i_mac[1] >> ws >> hex >> tmp >> ws; - i_mac[1] <<= 8; - i_mac[1] += tmp; - - for (int i=0;i<4;i++) - { - iss >> hex >> tmp >> ws; - - i_mac[0] <<= 8; - i_mac[0] += tmp; - } - - return 0; - -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::Lease::prefix6_to_number(const string& prefix, unsigned int ip[]) -{ - struct in6_addr s6; - - if (prefix.empty()) - { - ip[1] = ip[0] = 0; - return 0; - } - - int rc = inet_pton(AF_INET6, prefix.c_str(), &s6); - - if ( rc != 1 ) - { - return -1; - } - - ip[1] = ntohl(s6.s6_addr32[0]); - ip[0] = ntohl(s6.s6_addr32[1]); - - return 0; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void Leases::Lease::mac_to_eui64(const unsigned int i_mac[], unsigned int eui64[]) -{ - eui64[1] = ((i_mac[1]+512)<<16) + ((i_mac[0] & 0xFF000000)>>16) + 0x000000FF; - eui64[0] = 4261412864 + (i_mac[0] & 0x00FFFFFF); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -void Leases::Lease::mac_to_string(const unsigned int i_mac[], string& mac) -{ - ostringstream oss; - unsigned int temp_byte; - - oss.str(""); - - for (int i=5;i>=0;i--) - { - if ( i < 4 ) - { - temp_byte = i_mac[0]; - temp_byte >>= i*8; - } - else - { - temp_byte = i_mac[1]; - temp_byte >>= (i%4)*8; - } - - temp_byte &= 255; - - oss.width(2); - oss.fill('0'); - oss << hex << temp_byte; - - if(i!=0) - { - oss << ":"; - } - } - - mac = oss.str(); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -string& Leases::Lease::to_xml(string& str, - const unsigned int global[], - const unsigned int site[]) const -{ - string ip_s; - string ipl_s, ips_s, ipg_s; - string mac_s; - - unsigned int tmp_prefix[2] = {0, 0}; - - ostringstream os; - - mac_to_string(mac, mac_s); - - ip_to_string(ip, ip_s); - - ip6_to_string(eui64, tmp_prefix, ipl_s); - - os << - "" << - "" << mac_s << "" << - "" << ip_s << "" << - "" << ipl_s << ""; - - if (site[1] != 0 || site[0] != 0 ) - { - ip6_to_string(eui64, site, ips_s); - os << "" << ips_s << ""; - } - - if (global[1] != 0 || global[0] != 0 ) - { - ip6_to_string(eui64, global, ipg_s); - os << "" << ipg_s << ""; - } - - os << ""<< used << ""<< - "" << vid << "" << - ""; - - str = os.str(); - - return str; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -string& Leases::Lease::to_xml_db(string& str) const -{ - ostringstream os; - - os << - "" << - "" << ip << "" << - "" << mac[1] << "" << - "" << mac[0] << "" << - "" << used << ""<< - "" << vid << "" << - ""; - - str = os.str(); - - return str; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::Lease::from_xml(const string &xml_str) -{ - int rc = 0; - int int_used; - - // Initialize the internal XML object - update_from_str(xml_str); - - rc += xpath(ip, "/LEASE/IP", 0); - - rc += xpath(mac[1], "/LEASE/MAC_PREFIX", 0); - rc += xpath(mac[0], "/LEASE/MAC_SUFFIX", 0); - - rc += xpath(int_used, "/LEASE/USED", 0); - rc += xpath(vid, "/LEASE/VID", 0); - - used = static_cast(int_used); - - if (rc != 0) - { - return -1; - } - - mac_to_eui64(mac, eui64); - - return 0; -} - -/* ************************************************************************** */ -/* ************************************************************************** */ -/* Leases class */ -/* ************************************************************************** */ -/* ************************************************************************** */ - -const char * Leases::table = "leases"; - -const char * Leases::db_names = "oid, ip, body"; - -const char * Leases::db_bootstrap = "CREATE TABLE IF NOT EXISTS leases (" - "oid INTEGER, ip BIGINT, body MEDIUMTEXT, PRIMARY KEY(oid,ip))"; - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::select_cb(void *nil, int num, char **values, char **names) -{ - Lease * lease; - int rc; - - if ( (!values[0]) || (num != 1) ) - { - return -1; - } - - lease = new Lease(); - rc = lease->from_xml(values[0]); - - if (rc != 0) - { - return -1; - } - - leases.insert(make_pair(lease->ip, lease)); - - if(lease->used) - { - n_used++; - } - - return 0; -} - -/* -------------------------------------------------------------------------- */ - -int Leases::select(SqlDB * db) -{ - ostringstream oss; - int rc; - - // Reset the used leases counter - n_used = 0; - - set_callback(static_cast(&Leases::select_cb)); - - oss << "SELECT body FROM " << table << " WHERE oid = " << oid; - - rc = db->exec(oss,this); - - unset_callback(); - - if (rc != 0) - { - goto error_id; - } - - return 0; - -error_id: - oss.str(""); - oss << "Error getting leases for network nid: " << oid; - - NebulaLog::log("VNM", Log::ERROR, oss); - return -1; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::drop(SqlDB * db) -{ - ostringstream oss; - - // Drop all the leases - oss << "DELETE FROM " << table << " WHERE oid=" << oid; - - return db->exec(oss); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::insert(SqlDB * db, string& error_str) -{ - error_str = "Should not access to Leases.insert()."; - NebulaLog::log("VNM", Log::ERROR, error_str); - return -1; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::update(SqlDB * db) -{ - NebulaLog::log("VNM", Log::ERROR, "Should not access to Leases.update()"); - return -1; -} - -/* ************************************************************************** */ -/* Leases :: Interface Methods */ -/* ************************************************************************** */ - -bool Leases::check(const string& ip) -{ - unsigned int _ip; - - Leases::Lease::ip_to_number(ip,_ip); - - return check(_ip); -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -bool Leases::check(unsigned int ip) -{ - map::iterator it; - - it=leases.find(ip); - - if (it!=leases.end()) - { - return it->second->used; - } - else - { - return false; - } -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -bool Leases::is_owner(const string& ip, int vid) -{ - unsigned int _ip; - map::iterator it; - - Leases::Lease::ip_to_number(ip,_ip); - - it = leases.find(_ip); - - if (it!=leases.end()) - { - return (it->second->vid == vid); - } - else - { - return false; - } -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::hold_leases(vector& vector_leases, - string& error_msg) -{ - const VectorAttribute * single_attr_lease = 0; - - int rc; - string ip; - string mac; - - unsigned int tmp[2]; - - if ( vector_leases.size() > 0 ) - { - single_attr_lease = - dynamic_cast(vector_leases[0]); - } - - if ( single_attr_lease == 0 ) - { - error_msg = "Empty lease description."; - return -1; - } - - ip = single_attr_lease->vector_value("IP"); - - if ( check(ip) ) - { - error_msg = "Lease is in use."; - return -1; - } - - rc = set(-1, ip, mac, tmp); - - if ( rc != 0 ) - { - error_msg = "Lease is not part of the NET."; - return -1; - } - - return 0; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int Leases::free_leases(vector& vector_leases, - string& error_msg) -{ - const VectorAttribute * single_attr_lease = 0; - map::iterator it; - - unsigned int i_ip; - string st_ip; - string mac; - - if ( vector_leases.size() > 0 ) - { - single_attr_lease = - dynamic_cast(vector_leases[0]); - } - - if ( single_attr_lease == 0 ) - { - error_msg = "Empty lease description."; - return -1; - } - - st_ip = single_attr_lease->vector_value("IP"); - - if ( Leases::Lease::ip_to_number(st_ip,i_ip) != 0 ) - { - error_msg = "Wrong Lease format."; - return -1; - } - - it = leases.find(i_ip); - - if ( it == leases.end() || !it->second->used || it->second->vid != -1 ) - { - error_msg = "Lease is not on hold."; - return -1; - } - - release(st_ip); - - return 0; -} - -/* ************************************************************************** */ -/* Leases :: Misc */ -/* ************************************************************************** */ - -string& Leases::to_xml(string& xml) const -{ - map::const_iterator it; - ostringstream os; - string lease_xml; - - for(it=leases.begin();it!=leases.end();it++) - { - os << it->second->to_xml(lease_xml, global, site); - } - - xml = os.str(); - - return xml; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -ostream& operator<<(ostream& os, Leases& _leases) -{ - string xml; - - os << _leases.to_xml(xml); - - return os; -}; - diff --git a/src/vnm/RangedLeases.cc b/src/vnm/RangedLeases.cc deleted file mode 100644 index 548d734ff6..0000000000 --- a/src/vnm/RangedLeases.cc +++ /dev/null @@ -1,556 +0,0 @@ -/* -------------------------------------------------------------------------- */ -/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */ -/* */ -/* Licensed under the Apache License, Version 2.0 (the "License"); you may */ -/* not use this file except in compliance with the License. You may obtain */ -/* a copy of the License at */ -/* */ -/* http://www.apache.org/licenses/LICENSE-2.0 */ -/* */ -/* Unless required by applicable law or agreed to in writing, software */ -/* distributed under the License is distributed on an "AS IS" BASIS, */ -/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ -/* See the License for the specific language governing permissions and */ -/* limitations under the License. */ -/* -------------------------------------------------------------------------- */ - - -#include "RangedLeases.h" -#include "Nebula.h" - -#include - -/* ************************************************************************** */ -/* Ranged Leases class */ -/* ************************************************************************** */ - -RangedLeases::RangedLeases( - SqlDB * db, - int _oid, - unsigned int _mac_prefix, - unsigned int _global[], - unsigned int _site[], - unsigned int _ip_start, - unsigned int _ip_end): - Leases(db, _oid, 0, _mac_prefix, _global, _site), - ip_start(_ip_start),ip_end(_ip_end),current(0) -{ - size = ip_end - ip_start + 1; -} - -/* ************************************************************************** */ -/* Ranged Leases :: Methods */ -/* ************************************************************************** */ - -static int get_template_size(VirtualNetwork* vn, - unsigned int& host_bits, - unsigned int& size) -{ - string st_size = ""; - - vn->erase_template_attribute("NETWORK_SIZE", st_size); - - if ( st_size == "C" || st_size == "c" ) - { - host_bits = 8; - size = 256; - } - else if ( st_size == "B" || st_size == "b" ) - { - host_bits = 16; - size = 65536; - } - else if ( st_size == "A" || st_size == "a" ) - { - host_bits = 24; - size = 16777216; - } - else - { - if (!st_size.empty())//Assume it's a number - { - istringstream iss(st_size); - - iss >> size; - - if (iss.fail()) - { - return -1; - } - } - else - { - size = VirtualNetworkPool::default_size(); - } - - host_bits = (int) ceil(log(size+2)/log(2)); - } - - return 0; -} - -/* -------------------------------------------------------------------------- */ - -int RangedLeases::process_template(VirtualNetwork* vn, - unsigned int& ip_start, unsigned int& ip_end, string& error_str) -{ - ostringstream oss; - - string st_addr = ""; - string st_mask = ""; - - string st_ip_start = ""; - string st_ip_end = ""; - string st_mac_start = ""; - - unsigned int host_bits = 0; - unsigned int network_bits; - unsigned int size; - - unsigned int net_addr; - unsigned int net_mask; - size_t pos; - - unsigned int mac[2]; - - // ------------------------------------------------------------------------- - // Pure IPv6 network definition based on MAC_START and NETWORK_SIZE - // ------------------------------------------------------------------------- - - vn->erase_template_attribute("MAC_START", st_mac_start); - - if (!st_mac_start.empty()) - { - if ( Leases::Lease::mac_to_number(st_mac_start, mac) != 0 ) - { - goto error_mac_start; - } - - ip_start = mac[0]; - - if (get_template_size(vn, host_bits, size) != 0) - { - goto error_size; - } - - ip_end = ip_start + size; - - if ( ip_end < ip_start ) - { - goto error_greater_mac; - } - - vn->remove_template_attribute("IP_START"); - vn->remove_template_attribute("IP_END"); - vn->remove_template_attribute("NETWORK_ADDRESS"); - vn->remove_template_attribute("NETWORK_MASK"); - vn->remove_template_attribute("NETWORK_SIZE"); - - return 0; - } - - // ------------------------------------------------------------------------- - // Initialize IP_START and IP_END to limit the IP range - // ------------------------------------------------------------------------- - - ip_start = 0; - ip_end = 0; - - vn->erase_template_attribute("IP_START", st_ip_start); - vn->erase_template_attribute("IP_END", st_ip_end); - - if ( !st_ip_start.empty() ) - { - if ( Leases::Lease::ip_to_number(st_ip_start, ip_start) != 0 ) - { - goto error_ip_start; - } - } - - if ( !st_ip_end.empty() ) - { - if ( Leases::Lease::ip_to_number(st_ip_end, ip_end) != 0 ) - { - goto error_ip_end; - } - } - - // ------------------------------------------------------------------------- - // Check for NETWORK_ADDRESS - // ------------------------------------------------------------------------- - - vn->get_template_attribute("NETWORK_ADDRESS", st_addr); - - if (st_addr.empty()) - { - if ( ip_start != 0 && ip_end != 0 ) //Manually range, exit - { - if ( ip_end < ip_start ) - { - goto error_greater; - } - - return 0; - } - else - { - goto error_addr; - } - } - - // ------------------------------------------------------------------------- - // Check for NETWORK_SIZE or NETWORK_ADDRESS - // ------------------------------------------------------------------------- - - pos = st_addr.find("/"); - - if ( pos != string::npos ) // Check for CIDR network address - { - string st_network_bits; - - st_network_bits = st_addr.substr(pos+1); - st_addr = st_addr.substr(0,pos); - - istringstream iss(st_network_bits); - iss >> network_bits; - - if ( network_bits > 32 ) - { - goto error_prefix; - } - - host_bits = 32 - network_bits; - } - else // Check for network mask in decimal-dot notation - { - vn->erase_template_attribute("NETWORK_MASK", st_mask); - - if ( !st_mask.empty() ) - { - // st_mask is in decimal format, e.g. 255.255.0.0 - // The number of trailing 0s is needed - - if ( Leases::Lease::ip_to_number(st_mask, net_mask) != 0 ) - { - goto error_netmask; - } - - host_bits = 0; - - while ( host_bits < 32 && - ((net_mask >> host_bits) & 1) != 1 ) - { - host_bits++; - } - } - else //No network mask, get NETWORK_SIZE - { - if (get_template_size(vn, host_bits, size) != 0) - { - goto error_size; - } - } - } - - vn->remove_template_attribute("NETWORK_SIZE"); - - // ------------------------------------------------------------------------- - // Generate NETWORK_MASK - // ------------------------------------------------------------------------- - - net_mask = 0xFFFFFFFF << host_bits; - Lease::ip_to_string(net_mask, st_mask); - - vn->replace_template_attribute("NETWORK_MASK", st_mask); - - // ------------------------------------------------------------------------- - // Consistency Checks based on the network address, and range - // ------------------------------------------------------------------------- - if ( Leases::Lease::ip_to_number(st_addr,net_addr) != 0 ) - { - goto error_net_addr; - } - - if (net_addr != (net_mask & net_addr) ) - { - goto error_not_base_addr; - } - - // Set IP start/end - if ( ip_start == 0 ) - { - ip_start = net_addr + 1; - } - - if ( ip_end == 0 ) - { - ip_end = net_addr + (1 << host_bits) - 2; - } - - // Check range restrictions - if ( (ip_start & net_mask) != net_addr ) - { - goto error_range_ip_start; - } - - if ( (ip_end & net_mask) != net_addr ) - { - goto error_range_ip_end; - } - - if ( ip_end < ip_start ) - { - goto error_greater; - } - - return 0; - -error_mac_start: - oss << "MAC_START " << st_mac_start << " is not a valid MAC."; - goto error_common; - -error_ip_start: - oss << "IP_START " << st_ip_start << " is not a valid IP."; - goto error_common; - -error_ip_end: - oss << "IP_END " << st_ip_end << " is not a valid IP."; - goto error_common; - -error_not_base_addr: - oss << "NETWORK_ADDRESS " << st_addr - << " is not a base address for the network mask " << st_mask << "."; - goto error_common; - -error_net_addr: - oss << "NETWORK_ADDRESS " << st_addr << " is not a valid IP."; - goto error_common; - -error_netmask: - oss << "NETWORK_MASK " << st_mask << " is not a valid network mask."; - goto error_common; - -error_prefix: - oss << "A CIDR prefix of " << network_bits << " bits is not valid."; - goto error_common; - -error_addr: - oss << "No NETWORK_ADDRESS in template for Virtual Network."; - goto error_common; - -error_range_ip_start: - oss << "IP_START " << st_ip_start << " is not part of the network " - << st_addr << "/" << 32-host_bits << "."; - goto error_common; - -error_size: - oss << "NETWORK_SIZE is not an integer number."; - goto error_common; - -error_range_ip_end: - oss << "IP_END " << st_ip_end << " is not part of the network " - << st_addr << "/" << 32-host_bits << "."; - goto error_common; - -error_greater: - Leases::Lease::ip_to_string(ip_start, st_ip_start); - Leases::Lease::ip_to_string(ip_end, st_ip_end); - - oss << "IP_START " << st_ip_start << " cannot be greater than the IP_END " - << st_ip_end << "."; - goto error_common; - -error_greater_mac: - oss << "Size too big, it will overflow MAC address space."; - goto error_common; - -error_common: - error_str = oss.str(); - return -1; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int RangedLeases::get(int vid, string& ip, string& mac, unsigned int eui64[]) -{ - unsigned int num_ip; - int rc = -1; - - for ( unsigned int i=0; iescape_str(lease->to_xml_db(xml_body).c_str()); - - if ( sql_xml == 0 ) - { - goto error_body; - } - - oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES (" - << oid << "," - << ip << "," - << "'" << sql_xml << "')"; - - db->free_str(sql_xml); - - rc = db->exec(oss); - - if ( rc != 0 ) - { - goto error_db; - } - - leases.insert( make_pair(ip,lease) ); - - n_used++; - - return rc; - - -error_body: - oss.str(""); - oss << "Error inserting lease, marshall error"; - goto error_common; - -error_db: - oss.str(""); - oss << "Error inserting lease in database."; - -error_common: - NebulaLog::log("VNM", Log::ERROR, oss); - return -1; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int RangedLeases::del(const string& ip) -{ - unsigned int _ip; - ostringstream oss; - int rc; - map::iterator it_ip; - - // Remove lease from leases map - - if ( Lease::ip_to_number(ip,_ip) ) - { - return 0; - } - - it_ip = leases.find(_ip); - - if (it_ip == leases.end()) - { - return 0; //Not in the map, not leased - } - - // Erase it from DB - - oss << "DELETE FROM " << table << " WHERE oid='" << oid - << "' AND ip='" << _ip << "'"; - - rc = db->exec(oss); - - if ( rc == 0 ) - { - n_used--; - - delete it_ip->second; - - leases.erase(it_ip); - } - - return rc; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ diff --git a/src/vnm/SConstruct b/src/vnm/SConstruct index f2f18f85b0..dd7233c8b2 100644 --- a/src/vnm/SConstruct +++ b/src/vnm/SConstruct @@ -22,9 +22,6 @@ lib_name='nebula_vnm' # Sources to generate the library source_files=[ -# 'Leases.cc', -# 'FixedLeases.cc', -# 'RangedLeases.cc', 'VirtualNetwork.cc', 'VirtualNetworkPool.cc', 'AddressRange.cc', diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 1ac5f07dbd..f5d142394f 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -20,8 +20,6 @@ #include "VirtualNetworkTemplate.h" #include "NebulaLog.h" -#include "RangedLeases.h" -#include "FixedLeases.h" #include "AuthManager.h" #include "ClusterPool.h" From df8ab356a14bc7738951f9b12d2c71f5ec2e6dad Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 20 May 2014 12:20:43 +0200 Subject: [PATCH 19/84] feature #2858: onevnet: do not fail on empty address range pool --- src/cli/one_helper/onevnet_helper.rb | 30 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/cli/one_helper/onevnet_helper.rb b/src/cli/one_helper/onevnet_helper.rb index 51b3821a41..9045aed244 100644 --- a/src/cli/one_helper/onevnet_helper.rb +++ b/src/cli/one_helper/onevnet_helper.rb @@ -135,6 +135,10 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper CLIHelper.print_header(str_h1 % ["ADDRESS RANGE POOL"], false) + if !vn.to_hash['VNET']['AR_POOL']['AR'].nil? + arlist = [vn.to_hash['VNET']['AR_POOL']['AR']].flatten + end + CLIHelper::ShowTable.new(nil, self) do column :AR, "", :size=>3 do |d| d["AR_ID"] @@ -164,21 +168,27 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper d["GLOBAL_PREFIX"]||"-" end - end.show([vn.to_hash['VNET']['AR_POOL']['AR']].flatten, {}) + end.show(arlist, {}) puts CLIHelper.print_header(str_h1 % ["LEASES"], false) - leases = Array.new - [vn.to_hash['VNET']['AR_POOL']['AR']].flatten.each do |ar| - id = ar['AR_ID'] - if ar['LEASES'] && !ar['LEASES']['LEASE'].nil? - lease = [ar['LEASES']['LEASE']].flatten - lease.each do |l| - l['AR_ID'] = id + if !vn.to_hash['VNET']['AR_POOL']['AR'].nil? + lease_list = [vn.to_hash['VNET']['AR_POOL']['AR']].flatten + leases = Array.new + + lease_list.each do |ar| + id = ar['AR_ID'] + if ar['LEASES'] && !ar['LEASES']['LEASE'].nil? + lease = [ar['LEASES']['LEASE']].flatten + lease.each do |l| + l['AR_ID'] = id + end + leases << lease end - leases << lease end + + leases.flatten! end CLIHelper::ShowTable.new(nil, self) do @@ -205,6 +215,6 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper column :IP6_GLOBAL, "", :size=>31 do |d| d["IP6_GLOBAL"]||"-" end - end.show(leases.flatten, {}) + end.show(leases, {}) end end From c3adf0b5e392ff398775fb73857c07a5b48a6956 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 21 May 2014 13:16:51 +0200 Subject: [PATCH 20/84] feature #2858: Fix error message --- src/vnm/AddressRange.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index b2cf9cccf1..c225be22b6 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -107,7 +107,7 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) { if (mac_to_i(value, mac) == -1) { - error_msg = "Wrong format for MAC_START attribute"; + error_msg = "Wrong format for MAC attribute"; return -1; }; } @@ -131,7 +131,7 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) if (value.empty() || ip_to_i(value, ip) == -1) { - error_msg = "Wrong or empty IP_START attribute"; + error_msg = "Wrong or empty IP attribute"; return -1; } From f440b6b1a537e251d7f24389c5d351e013d4779c Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 22 May 2014 20:20:33 +0200 Subject: [PATCH 21/84] feature #2858: Add and remove methods for Address Ranges. Core, OCA and CLI --- include/AddressRangePool.h | 14 +++++ include/RequestManagerVirtualNetwork.h | 37 +++++++------- include/VirtualNetwork.h | 19 +++---- src/cli/one_helper/onevnet_helper.rb | 42 ++++++++++++++- src/cli/onevnet | 50 ++++++++++++++---- src/oca/ruby/opennebula/virtual_network.rb | 18 +++---- src/rm/RequestManager.cc | 8 +-- src/rm/RequestManagerVirtualNetwork.cc | 51 +++++++++++++++++-- src/vnm/AddressRangePool.cc | 59 ++++++++++++++++++++++ src/vnm/VirtualNetwork.cc | 32 ++++++------ 10 files changed, 256 insertions(+), 74 deletions(-) diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 80a8497ac0..600f3bb64f 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -54,6 +54,14 @@ public: */ int from_xml_node(const xmlNodePtr node); + /** + * Removes an address range from the pool if it does not contain any used + * leases + * @param arid of the address range to be removed + * @return 0 on success, -1 if not exists or has used addresses + */ + int rm_ar(unsigned int ar_id, string& error_msg); + /** * Generate a XML representation of the Address Range Pool * @param sstream where the ARPool is written @@ -175,6 +183,12 @@ public: return used_addr; } + /** + * Gets an attribute from the Address Range + * @param name of the attribute + * @param value of the attribute + * @param ar_id to get the attribute from + */ void get_attribute(const char * name, string& value, int ar_id) const; private: diff --git a/include/RequestManagerVirtualNetwork.h b/include/RequestManagerVirtualNetwork.h index ad8c6a39d5..1aeb3208fb 100644 --- a/include/RequestManagerVirtualNetwork.h +++ b/include/RequestManagerVirtualNetwork.h @@ -58,39 +58,42 @@ protected: /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -class VirtualNetworkAddLeases : public RequestManagerVirtualNetwork +class VirtualNetworkAddAddressRange: public RequestManagerVirtualNetwork { public: - VirtualNetworkAddLeases(): - RequestManagerVirtualNetwork("VirtualNetworkAddLeases", - "Adds leases to a virtual network"){}; - ~VirtualNetworkAddLeases(){}; + VirtualNetworkAddAddressRange(): + RequestManagerVirtualNetwork("VirtualNetworkAddAddressRange", + "Adds address ranges to a virtual network"){}; + ~VirtualNetworkAddAddressRange(){}; int leases_action(VirtualNetwork * vn, VirtualNetworkTemplate * tmpl, string& error_str) { - return vn->add_leases(tmpl, error_str); + return vn->add_ar(tmpl, error_str); } }; /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ -class VirtualNetworkRemoveLeases : public RequestManagerVirtualNetwork +class VirtualNetworkRmAddressRange : public Request { public: - VirtualNetworkRemoveLeases(): - RequestManagerVirtualNetwork("VirtualNetworkRemoveLeases", - "Removes leases from a virtual network"){}; - ~VirtualNetworkRemoveLeases(){}; - - int leases_action(VirtualNetwork * vn, - VirtualNetworkTemplate * tmpl, - string& error_str) + VirtualNetworkRmAddressRange():Request("VirtualNetworkRmAddressRange", + "A:sii", "Removes an address range from a virtual network") { - return vn->remove_leases(tmpl, error_str); - } + Nebula& nd = Nebula::instance(); + pool = nd.get_vnpool(); + + auth_object = PoolObjectSQL::NET; + auth_op = AuthRequest::MANAGE; + }; + + ~VirtualNetworkRmAddressRange(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); }; /* ------------------------------------------------------------------------- */ diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index b6e9ca7f62..180666bdd5 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -57,25 +57,20 @@ public: } /** - * Adds Leases to the virtual network (Only implemented for FIXED networks) - * @param leases template in the form LEASES = [IP=XX, MAC=XX]. - * MAC is optional. The template can only contain one LEASE - * definition. + * Add an address range to the virtual network + * @param ars_tmpl template in the form AR = [TYPE=...,IP=...,SIZE=...]. * @param error_msg If the action fails, this message contains the reason. * @return 0 on success */ - int add_leases(VirtualNetworkTemplate * leases, string& error_msg); + int add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg); /** - * Removes Leases from the virtual network; if they are not used.(Only - * implemented for FIXED networks) - * @param leases template in the form LEASES = [IP=XX]. - * The template can only contain one LEASE definition. - * @param error_msg If the action fails, this message contains - * the reason. + * Removes an address range from the VNET + * @param ar_id of the address range + * @param error_msg If the action fails, this message contains the reason. * @return 0 on success */ - int remove_leases(VirtualNetworkTemplate* leases, string& error_msg); + int rm_ar(unsigned int ar_id, string& error_msg); /** * Holds a Lease, marking it as used diff --git a/src/cli/one_helper/onevnet_helper.rb b/src/cli/one_helper/onevnet_helper.rb index 9045aed244..748d05276b 100644 --- a/src/cli/one_helper/onevnet_helper.rb +++ b/src/cli/one_helper/onevnet_helper.rb @@ -26,6 +26,46 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper :description => "ID of the address range" } + AR_MAC = { + :name => "mac", + :short => "-m mac", + :large => "--address_range_mac mac", + :format => String, + :description => "First MAC address in : notation" + } + + AR_IP = { + :name => "ip", + :short => "-i ip", + :large => "--address_range_ip ip", + :format => String, + :description => "First IP address in . notation" + } + + AR_SIZE = { + :name => "size", + :short => "-s size", + :large => "--address_range_size size", + :format => String, + :description => "Number of addresses in the range" + } + + AR_IP6_GLOBAL = { + :name => "ip6_global", + :short => "-g ip6_pref", + :large => "--address_range_ip6g ip6_pref", + :format => String, + :description => "IP6 global prefix" + } + + AR_IP6_ULA = { + :name => "ip6_ula", + :short => "-u ip6_pref", + :large => "--address_range_ip6u ip6_pref", + :format => String, + :description => "IP6 ula prefix" + } + def self.rname "VNET" end @@ -165,7 +205,7 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper end column :IP6_GLOBAL_PREFIX, "", :right, :size=>22 do |d| - d["GLOBAL_PREFIX"]||"-" + d["IP6_GLOBAL"]||"-" end end.show(arlist, {}) diff --git a/src/cli/onevnet b/src/cli/onevnet index 5209e3da5f..4ec03e3f1a 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -104,23 +104,55 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end - addleases_desc = <<-EOT.unindent - Adds a lease to the Virtual Network + addar_desc = <<-EOT.unindent + Adds an address range to the Virtual Network EOT - command :addleases, addleases_desc, :vnetid, :ip, [:mac, nil] do + command :addar, addar_desc, :vnetid, :options=>STD_OPTIONS + [OneVNetHelper::AR_SIZE, + OneVNetHelper::AR_MAC, OneVNetHelper::AR_IP, OneVNetHelper::AR_IP6_GLOBAL, + OneVNetHelper::AR_IP6_ULA ] do helper.perform_action(args[0],options,"lease added") do |vn| - vn.addleases(args[1], args[2]) + ar = "AR = [ " + + if options[:ip] + if options[:ip6_global] || options[:ip6_ula] + ar << "TYPE=\"IP4_6\"" + else + ar << "TYPE=\"IP4\"" + end + else + if options[:ip6_global] || options[:ip6_ula] + ar << "TYPE=\"IP6\"" + else + ar << "TYPE=\"ETHER\"" + end + end + + if options[:size] + ar << ", SIZE = " << options[:size] + else + STDERR.puts "Address range needs to specify size (-s size)" + return -1 + end + + ar << ", IP = " << options[:ip] if options[:ip] + ar << ", MAC = " << options[:mac] if options[:mac] + ar << ", IP6_GLOBAL = " << options[:ip6_global] if options[:ip6_global] + ar << ", IP6_ULA = " << options[:ip6_ula] if options[:ip6_ula] + + ar << "]" + + vn.add_ar(ar) end end - rmleases_desc = <<-EOT.unindent - Removes a lease from the Virtual Network + rmar_desc = <<-EOT.unindent + Removes an address range from the Virtual Network EOT - command :rmleases, rmleases_desc, :vnetid, :ip do - helper.perform_action(args[0],options,"lease removed") do |vn| - vn.rmleases(args[1]) + command :rmar, rmar_desc, :vnetid, :ar_id do + helper.perform_action(args[0],options,"address range removed") do |vn| + vn.rm_ar(args[1]) end end diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index 7e914402fe..dfce718701 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -28,8 +28,8 @@ module OpenNebula :info => "vn.info", :allocate => "vn.allocate", :delete => "vn.delete", - :addleases => "vn.addleases", - :rmleases => "vn.rmleases", + :add_ar => "vn.add_ar", + :rm_ar => "vn.rm_ar", :chown => "vn.chown", :chmod => "vn.chmod", :update => "vn.update", @@ -110,26 +110,20 @@ module OpenNebula end # Adds a lease to the VirtualNetwork - def addleases(ip, mac = nil) + def add_ar(ar_template) return Error.new('ID not defined') if !@pe_id - lease_template = "LEASES = [ IP = #{ip}" - lease_template << ", MAC = #{mac}" if mac - lease_template << " ]" - - rc = @client.call(VN_METHODS[:addleases], @pe_id, lease_template) + rc = @client.call(VN_METHODS[:add_ar], @pe_id, ar_template) rc = nil if !OpenNebula.is_error?(rc) return rc end # Removes a lease from the VirtualNetwork - def rmleases(ip) + def rm_ar(ar_id) return Error.new('ID not defined') if !@pe_id - lease_template = "LEASES = [ IP = #{ip} ]" - - rc = @client.call(VN_METHODS[:rmleases], @pe_id, lease_template) + rc = @client.call(VN_METHODS[:rm_ar], @pe_id, ar_id.to_i) rc = nil if !OpenNebula.is_error?(rc) return rc diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 1fd4ab040e..9384444c3a 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -276,8 +276,8 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr vm_pool_monitoring(new VirtualMachinePoolMonitoring()); // VirtualNetwork Methods - xmlrpc_c::methodPtr vn_addleases(new VirtualNetworkAddLeases()); - xmlrpc_c::methodPtr vn_rmleases(new VirtualNetworkRemoveLeases()); + xmlrpc_c::methodPtr vn_add_ar(new VirtualNetworkAddAddressRange()); + xmlrpc_c::methodPtr vn_rm_ar(new VirtualNetworkRmAddressRange()); xmlrpc_c::methodPtr vn_hold(new VirtualNetworkHold()); xmlrpc_c::methodPtr vn_release(new VirtualNetworkRelease()); @@ -485,8 +485,8 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.groupquota.update", group_set_default_quota); /* Network related methods*/ - RequestManagerRegistry.addMethod("one.vn.addleases", vn_addleases); - RequestManagerRegistry.addMethod("one.vn.rmleases", vn_rmleases); + RequestManagerRegistry.addMethod("one.vn.add_ar", vn_add_ar); + RequestManagerRegistry.addMethod("one.vn.rm_ar", vn_rm_ar); RequestManagerRegistry.addMethod("one.vn.hold", vn_hold); RequestManagerRegistry.addMethod("one.vn.release", vn_release); RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate); diff --git a/src/rm/RequestManagerVirtualNetwork.cc b/src/rm/RequestManagerVirtualNetwork.cc index 971d18d6dd..ad8b7b8bc9 100644 --- a/src/rm/RequestManagerVirtualNetwork.cc +++ b/src/rm/RequestManagerVirtualNetwork.cc @@ -68,10 +68,10 @@ void RequestManagerVirtualNetwork:: if ( rc < 0 ) { - failure_response(INTERNAL, + failure_response(INTERNAL, request_error("Error modifying network leases",error_str), att); - + vn->unlock(); return; } @@ -79,7 +79,52 @@ void RequestManagerVirtualNetwork:: pool->update(vn); vn->unlock(); - + success_response(id, att); } +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +void VirtualNetworkRmAddressRange:: + request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + int id = xmlrpc_c::value_int(paramList.getInt(1)); + int ar_id = xmlrpc_c::value_int(paramList.getInt(2)); + + VirtualNetwork * vn; + + string error_str; + int rc; + + if ( basic_authorization(id, att) == false ) + { + return; + } + + vn = static_cast(pool->get(id,true)); + + if ( vn == 0 ) + { + failure_response(NO_EXISTS, get_error(object_name(auth_object),id),att); + return; + } + + if ( vn->rm_ar(ar_id, error_str) < 0 ) + { + failure_response(INTERNAL, + request_error("Error removing address range",error_str), + att); + + vn->unlock(); + + return; + } + + pool->update(vn); + + vn->unlock(); + + success_response(id, att); +} diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 0ffd7a9e3f..9f975f4df7 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -114,6 +114,65 @@ int AddressRangePool::from_xml_node(const xmlNodePtr node) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int AddressRangePool::rm_ar(unsigned int ar_id, string& error_msg) +{ + map::iterator it; + + it = ar_pool.find(ar_id); + + if (it == ar_pool.end()) + { + error_msg = "Address Range does not exist"; + return -1; + } + + if (it->second->get_used_addr() > 0) + { + error_msg = "Address Range has leases in use"; + return -1; + } + + AddressRange * ar_ptr = it->second; + + ar_pool.erase(it); + + delete ar_ptr; + + vector ars; + vector::iterator it_ar; + Attribute * the_ar = 0; + + unsigned int ar_id_templ; + + ar_template.get("AR", ars); + + for (it_ar=ars.begin(); it_ar!=ars.end(); it_ar++) + { + VectorAttribute *ar = static_cast(*it_ar); + + if (ar == 0) + { + continue; + } + + if ((ar->vector_value("AR_ID",ar_id_templ)==0) && (ar_id_templ==ar_id)) + { + the_ar = ar; + break; + } + } + + if (the_ar != 0) + { + delete ar_template.remove(the_ar); + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string& AddressRangePool::to_xml(string& sstream, bool extended) const { if (extended) diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index f5d142394f..0288ac899d 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -586,31 +586,31 @@ int VirtualNetwork::nic_attribute( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetwork::add_leases(VirtualNetworkTemplate * leases_template, - string& error_msg) +int VirtualNetwork::add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg) { - vector vector_leases; + vector tmp_ars; + vector ars; - leases_template->get("LEASES", vector_leases); + vector::iterator it; - //TODO - return 0; - //return leases->add_leases(vector_leases, error_msg); + ars_tmpl->get("AR", tmp_ars); + + for (it=tmp_ars.begin(); it!=tmp_ars.end(); it++) + { + VectorAttribute * var = static_cast(*it); + + ars.push_back(var->clone()); + } + + return ar_pool.from_vattr(ars, error_msg); } /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetwork::remove_leases(VirtualNetworkTemplate * leases_template, - string& error_msg) +int VirtualNetwork::rm_ar(unsigned int ar_id, string& error_msg) { - vector vector_leases; - - leases_template->get("LEASES", vector_leases); - - //TODO - return 0; - //return leases->remove_leases(vector_leases, error_msg); + return ar_pool.rm_ar(ar_id, error_msg); } /* -------------------------------------------------------------------------- */ From 20a3c258436baebeb32d30eb5b8a8dcf6c8e5053 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 23 May 2014 00:24:14 +0200 Subject: [PATCH 22/84] feature #2858: Update ARs. Core, OCA and CLI. Needs to implement attribute removal --- include/AddressRange.h | 8 +++++ include/AddressRangePool.h | 7 +++++ include/RequestManagerVirtualNetwork.h | 23 ++++++++++++++ include/VirtualNetwork.h | 7 +++++ src/cli/onevnet | 19 ++++++++++++ src/oca/ruby/opennebula/virtual_network.rb | 16 ++++++++-- src/rm/RequestManager.cc | 2 ++ src/rm/RequestManagerVirtualNetwork.cc | 1 - src/vnm/AddressRange.cc | 36 ++++++++++++++++++++++ src/vnm/AddressRangePool.cc | 35 +++++++++++++++++++++ src/vnm/VirtualNetwork.cc | 12 ++++++++ 11 files changed, 162 insertions(+), 4 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index cda667500c..e0c6526a64 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -203,6 +203,14 @@ public: return attr->vector_value(name); } + /** + * Updates the Address Range with the attributes provided. The following + * CANNOT be updated: TYPE, SIZE, IP, MAC (plus the internal AR_ID and + * ALLOCATED) + * @param vup the new vector attributes for the address range + */ + void update_attributes(VectorAttribute *vup); + private: /* ---------------------------------------------------------------------- */ /* String to binary conversion functions for different address types */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 600f3bb64f..cdf81b28a7 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -62,6 +62,13 @@ public: */ int rm_ar(unsigned int ar_id, string& error_msg); + /** + * Updates the given address ranges + * @param ars vector of address ranges as VectorAttributes obtined from + * template in the form AR = [...] + */ + void update_ar(vector ars); + /** * Generate a XML representation of the Address Range Pool * @param sstream where the ARPool is written diff --git a/include/RequestManagerVirtualNetwork.h b/include/RequestManagerVirtualNetwork.h index 1aeb3208fb..918e73397b 100644 --- a/include/RequestManagerVirtualNetwork.h +++ b/include/RequestManagerVirtualNetwork.h @@ -99,6 +99,29 @@ public: /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ +class VirtualNetworkUpdateAddressRange: public RequestManagerVirtualNetwork +{ +public: + VirtualNetworkUpdateAddressRange(): + RequestManagerVirtualNetwork("VirtualNetworkUpdateAddressRange", + "Updates address ranges to a virtual network"){}; + ~VirtualNetworkUpdateAddressRange(){}; + + int leases_action(VirtualNetwork * vn, + VirtualNetworkTemplate * tmpl, + string& error_str) + { + error_str.clear(); + + vn->update_ar(tmpl); + + return 0; + }; +}; + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + class VirtualNetworkHold : public RequestManagerVirtualNetwork { public: diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 180666bdd5..f8a1dbb920 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -72,6 +72,13 @@ public: */ int rm_ar(unsigned int ar_id, string& error_msg); + /** + * Update an address range to the virtual network + * @param ars_tmpl template in the form AR = [AR_ID=...]. The address range + * is specified by the AR_ID attribute. + */ + void update_ar(VirtualNetworkTemplate * ars_tmpl); + /** * Holds a Lease, marking it as used * @param leases template in the form LEASES = [IP=XX]. diff --git a/src/cli/onevnet b/src/cli/onevnet index 4ec03e3f1a..225dd9707c 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -251,6 +251,25 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + update_ar_desc = <<-EOT.unindent + Update Address Range variables. SIZE, IP, MAC and TYPE cannot be updated + EOT + + command :updatear, update_ar_desc, :vnetid, :ar_id, [:file, nil] do + helper.perform_action(args[0],options,"modified") do |obj| + + str = OpenNebulaHelper.update_template(args[0], obj, args[2], + "AR_POOL/AR[AR_ID=#{args[1]}]") + + str[-1]='' + str.gsub!(/AR_ID="\d+"\n/,'') + str.gsub!(/^\s*(#|$)/,'') + str.gsub!(/\n/,',') + + obj.update_ar("AR = [AR_ID=#{args[1]},#{str}]") + end + end + rename_desc = <<-EOT.unindent Renames the Virtual Network EOT diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index dfce718701..98a2d9435c 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -23,13 +23,13 @@ module OpenNebula # Constants and Class Methods ####################################################################### - VN_METHODS = { :info => "vn.info", :allocate => "vn.allocate", :delete => "vn.delete", :add_ar => "vn.add_ar", :rm_ar => "vn.rm_ar", + :update_ar => "vn.update_ar", :chown => "vn.chown", :chmod => "vn.chmod", :update => "vn.update", @@ -109,7 +109,7 @@ module OpenNebula super(VN_METHODS[:delete]) end - # Adds a lease to the VirtualNetwork + # Adds Address Ranges to the VirtualNetwork def add_ar(ar_template) return Error.new('ID not defined') if !@pe_id @@ -119,7 +119,7 @@ module OpenNebula return rc end - # Removes a lease from the VirtualNetwork + # Removes an Address Range from the VirtualNetwork def rm_ar(ar_id) return Error.new('ID not defined') if !@pe_id @@ -129,6 +129,16 @@ module OpenNebula return rc end + # Updates Address Ranges from the VirtualNetwork + def update_ar(ar_template) + return Error.new('ID not defined') if !@pe_id + + rc = @client.call(VN_METHODS[:update_ar], @pe_id, ar_template) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + # Holds a virtual network address # @param ip [String] address to hold, if contains ":" a MAC address is assumed # @param ar_id [Integer] The address range to hold the lease. If not set diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 9384444c3a..046f59c745 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -278,6 +278,7 @@ void RequestManager::register_xml_methods() // VirtualNetwork Methods xmlrpc_c::methodPtr vn_add_ar(new VirtualNetworkAddAddressRange()); xmlrpc_c::methodPtr vn_rm_ar(new VirtualNetworkRmAddressRange()); + xmlrpc_c::methodPtr vn_update_ar(new VirtualNetworkUpdateAddressRange()); xmlrpc_c::methodPtr vn_hold(new VirtualNetworkHold()); xmlrpc_c::methodPtr vn_release(new VirtualNetworkRelease()); @@ -487,6 +488,7 @@ void RequestManager::register_xml_methods() /* Network related methods*/ RequestManagerRegistry.addMethod("one.vn.add_ar", vn_add_ar); RequestManagerRegistry.addMethod("one.vn.rm_ar", vn_rm_ar); + RequestManagerRegistry.addMethod("one.vn.update_ar", vn_update_ar); RequestManagerRegistry.addMethod("one.vn.hold", vn_hold); RequestManagerRegistry.addMethod("one.vn.release", vn_release); RequestManagerRegistry.addMethod("one.vn.allocate", vn_allocate); diff --git a/src/rm/RequestManagerVirtualNetwork.cc b/src/rm/RequestManagerVirtualNetwork.cc index ad8b7b8bc9..d49afa7942 100644 --- a/src/rm/RequestManagerVirtualNetwork.cc +++ b/src/rm/RequestManagerVirtualNetwork.cc @@ -96,7 +96,6 @@ void VirtualNetworkRmAddressRange:: VirtualNetwork * vn; string error_str; - int rc; if ( basic_authorization(id, att) == false ) { diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index c225be22b6..8523e3ed11 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -178,6 +178,42 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void AddressRange::update_attributes(VectorAttribute *vup) +{ + /* Remove non-update attributes */ + + vup->remove("TYPE"); + + vup->remove("SIZE"); + + vup->remove("MAC"); + + vup->remove("IP"); + + /* Remove internal attributes */ + + vup->remove("AR_ID"); + + vup->remove("ALLOCATED"); + + vup->remove("USED_LEASES"); + + vup->remove("LEASES"); + + /* Copy the rest attributes to the address range */ + + const map new_attrs = vup->value(); + map ::const_iterator it; + + for (it = new_attrs.begin(); it != new_attrs.end(); it++) + { + attr->replace(it->first, it->second); + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int AddressRange::from_vattr_db(VectorAttribute *vattr) { string value; diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 9f975f4df7..1f4ce78f20 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -74,6 +74,41 @@ int AddressRangePool::from_vattr(vector ars, string& error_msg) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void AddressRangePool::update_ar(vector ars) +{ + vector::iterator it; + map::iterator ar_it; + + unsigned int arid; + + for (it = ars.begin(); it != ars.end(); it++) + { + VectorAttribute * va = static_cast(*it); + + if (va == 0) + { + continue; + } + + if (va->vector_value("AR_ID", arid) != 0) + { + continue; + } + + ar_it = ar_pool.find(arid); + + if (ar_it == ar_pool.end()) + { + continue; + } + + ar_it->second->update_attributes(va); + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int AddressRangePool::from_xml_node(const xmlNodePtr node) { int rc = ar_template.from_xml_node(node); diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 0288ac899d..1d5db4bae6 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -608,6 +608,18 @@ int VirtualNetwork::add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void VirtualNetwork::update_ar(VirtualNetworkTemplate * ars_tmpl) +{ + vector tmp_ars; + + ars_tmpl->get("AR", tmp_ars); + + ar_pool.update_ar(tmp_ars); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int VirtualNetwork::rm_ar(unsigned int ar_id, string& error_msg) { return ar_pool.rm_ar(ar_id, error_msg); From 63e42cd18f128f1ae4bb22c76f279be3e234963e Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 23 May 2014 14:47:33 +0200 Subject: [PATCH 23/84] feature #2858: Better update for Address Range. --- src/cli/one_helper.rb | 12 ++++--- src/cli/one_helper/onevnet_helper.rb | 4 +-- src/cli/onevnet | 13 +++++-- src/vnm/AddressRange.cc | 53 ++++++++++++++++++++-------- 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 0f7c1979c4..c9a2ffebe4 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -821,7 +821,7 @@ EOT return update_template_helper(true, id, resource, path, xpath) end - def OpenNebulaHelper.update_template_helper(append, id, resource, path, xpath) + def OpenNebulaHelper.update_template_helper(append, id, resource, path, xpath, update=true) unless path require 'tempfile' @@ -829,11 +829,13 @@ EOT path = tmp.path if !append - rc = resource.info + if update + rc = resource.info - if OpenNebula.is_error?(rc) - puts rc.message - exit -1 + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 + end end tmp << resource.template_like_str(xpath) diff --git a/src/cli/one_helper/onevnet_helper.rb b/src/cli/one_helper/onevnet_helper.rb index 748d05276b..e834335560 100644 --- a/src/cli/one_helper/onevnet_helper.rb +++ b/src/cli/one_helper/onevnet_helper.rb @@ -204,8 +204,8 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper d["IP"]||"-" end - column :IP6_GLOBAL_PREFIX, "", :right, :size=>22 do |d| - d["IP6_GLOBAL"]||"-" + column :GLOBAL_PREFIX, "", :right, :size=>22 do |d| + d["GLOBAL_PREFIX"]||"-" end end.show(arlist, {}) diff --git a/src/cli/onevnet b/src/cli/onevnet index 225dd9707c..d34381754d 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -257,9 +257,18 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :updatear, update_ar_desc, :vnetid, :ar_id, [:file, nil] do helper.perform_action(args[0],options,"modified") do |obj| + rc = obj.info - str = OpenNebulaHelper.update_template(args[0], obj, args[2], - "AR_POOL/AR[AR_ID=#{args[1]}]") + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 + end + + obj.delete_element("AR_POOL/AR/LEASES") + obj.delete_element("AR_POOL/AR/USED_LEASES") + + str = OpenNebulaHelper.update_template_helper(false, args[0], obj, + args[2], "AR_POOL/AR[AR_ID=#{args[1]}]", false) str[-1]='' str.gsub!(/AR_ID="\d+"\n/,'') diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 8523e3ed11..412df4af74 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -180,35 +180,60 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) void AddressRange::update_attributes(VectorAttribute *vup) { - /* Remove non-update attributes */ + /* --------------- Copy non-update attributes ----------------- */ - vup->remove("TYPE"); + vup->replace("TYPE", attr->vector_value("TYPE")); - vup->remove("SIZE"); + vup->replace("MAC", attr->vector_value("MAC")); - vup->remove("MAC"); + if (type & 0x00000002) + { + vup->replace("IP", attr->vector_value("IP")); + } - vup->remove("IP"); + /* ----------------- Remove internal attributes ----------------- */ - /* Remove internal attributes */ + vup->replace("AR_ID", attr->vector_value("AR_ID")); - vup->remove("AR_ID"); - - vup->remove("ALLOCATED"); + vup->replace("ALLOCATED", attr->vector_value("ALLOCATED")); vup->remove("USED_LEASES"); vup->remove("LEASES"); - /* Copy the rest attributes to the address range */ + /* ----------------- update known attributes ----------------- */ - const map new_attrs = vup->value(); - map ::const_iterator it; + unsigned int new_size; - for (it = new_attrs.begin(); it != new_attrs.end(); it++) + if (vup->vector_value("SIZE", new_size) == 0) { - attr->replace(it->first, it->second); + map itup; + + if (allocated.upper_bound(new_size-1) == allocated.end()) + { + size = new_size; + } } + + vup->replace("SIZE", size); + + string value = vup->vector_value("GLOBAL_PREFIX"); + + if (prefix6_to_i(value, global6) != 0 ) + { + vup->replace("GLOBAL_PREFIX", attr->vector_value("GLOBAL_PREFIX")); + } + + value = vup->vector_value("ULA_PREFIX"); + + if (prefix6_to_i(value, ula6) != 0 ) + { + vup->replace("ULA_PREFIX", attr->vector_value("ULA_PREFIX")); + } + + /* Replace with the new attributes */ + + attr->replace(vup->value()); } /* -------------------------------------------------------------------------- */ From 6048a8c44b23b2b71c1b09fe703347f480e83bc6 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 23 May 2014 17:03:35 +0200 Subject: [PATCH 24/84] feature #2858: Fix minor issues in Address Range management --- src/cli/onevnet | 6 +++--- src/vnm/AddressRange.cc | 4 ++++ src/vnm/AddressRangePool.cc | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cli/onevnet b/src/cli/onevnet index d34381754d..fac9752c18 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -132,13 +132,13 @@ cmd=CommandParser::CmdParser.new(ARGV) do ar << ", SIZE = " << options[:size] else STDERR.puts "Address range needs to specify size (-s size)" - return -1 + exit -1 end ar << ", IP = " << options[:ip] if options[:ip] ar << ", MAC = " << options[:mac] if options[:mac] - ar << ", IP6_GLOBAL = " << options[:ip6_global] if options[:ip6_global] - ar << ", IP6_ULA = " << options[:ip6_ula] if options[:ip6_ula] + ar << ", GLOBAL_PREFIX = " << options[:ip6_global] if options[:ip6_global] + ar << ", ULA_PREFIX = " << options[:ip6_ula] if options[:ip6_ula] ar << "]" diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 412df4af74..4014948341 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -116,6 +116,8 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) { case ETHER: case IP6: + vattr->remove("IP"); + if (do_mac) { srand(time(0)); @@ -186,6 +188,8 @@ void AddressRange::update_attributes(VectorAttribute *vup) vup->replace("MAC", attr->vector_value("MAC")); + vup->remove("IP"); + if (type & 0x00000002) { vup->replace("IP", attr->vector_value("IP")); diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 1f4ce78f20..0c3ea6fd0a 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -135,7 +135,7 @@ int AddressRangePool::from_xml_node(const xmlNodePtr node) ar_pool.insert(make_pair(ar->ar_id(), ar)); - if (ar->ar_id() > next_ar) + if (ar->ar_id() >= next_ar) { next_ar = ar->ar_id() + 1; } From aca69ca418c997cbb0e4d09bf630a7d1c3fc3536 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sun, 25 May 2014 23:53:58 +0200 Subject: [PATCH 25/84] feature #2858: Fix VLAN attribute in address range --- src/vnm/AddressRange.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 4014948341..158a5eeb22 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -591,7 +591,7 @@ void AddressRange::set_vnet(VectorAttribute *nic, const vector &inherit) if (!vlan.empty()) { - nic->replace("VLAN", bridge); + nic->replace("VLAN", vlan); } if (!phydev.empty()) From e0bfd3f66cb4f8aa18b2e302d05d0ec146faeecd Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 26 May 2014 00:14:28 +0200 Subject: [PATCH 26/84] feature #2858: Sanitize VLAN attribute defined in Address Range --- src/vnm/AddressRange.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 158a5eeb22..876f3ce99d 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -165,6 +165,15 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) return -1; } + /* ------------------------- VNET Attributes ---------------------------- */ + + bool b_vlan; + + if ((vattr->vector_value("VLAN", b_vlan) == 0) && b_vlan) + { + vattr->replace("VLAN", "YES"); + } + /* ------------------------ AR Internal Data ---------------------------- */ vattr->replace("AR_ID", id); From 88987631d906147cff0ea85902663cdfa3856ddc Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 27 May 2014 12:58:46 +0200 Subject: [PATCH 27/84] feature #2858: Add umask to Request attributes --- include/Request.h | 8 ++- include/RequestManagerAllocate.h | 88 +++++++++++--------------- include/RequestManagerClone.h | 15 ++--- include/User.h | 7 ++ include/UserPool.h | 13 ++-- src/rm/Request.cc | 13 ++-- src/rm/RequestManagerAllocate.cc | 86 ++++++------------------- src/rm/RequestManagerClone.cc | 22 +------ src/rm/RequestManagerImage.cc | 23 +------ src/rm/RequestManagerVMTemplate.cc | 24 +------ src/rm/RequestManagerVirtualMachine.cc | 23 +------ src/um/User.cc | 16 +++++ src/um/UserPool.cc | 37 ++++++++--- 13 files changed, 144 insertions(+), 231 deletions(-) diff --git a/include/Request.h b/include/Request.h index faea23d7e0..c51ce85968 100644 --- a/include/Request.h +++ b/include/Request.h @@ -73,11 +73,13 @@ protected: string uname; /**< name of the user */ string gname; /**< name of the user's group */ - set group_ids; /**< set of user's group ids */ + set group_ids; /**< set of user's group ids */ string session; /**< Session from ONE XML-RPC API */ int req_id; /**< Request ID for log messages */ + int umask; /**< User umask for new objects */ + xmlrpc_c::value * retval; /**< Return value from libxmlrpc-c */ RequestAttributes(){}; @@ -92,6 +94,8 @@ protected: session = ra.session; retval = ra.retval; + + umask = ra.umask; }; RequestAttributes(int _uid, int _gid, const RequestAttributes& ra) @@ -102,6 +106,8 @@ protected: uname = ""; gname = ""; + umask = 0; + session = ra.session; retval = ra.retval; }; diff --git a/include/RequestManagerAllocate.h b/include/RequestManagerAllocate.h index bd6d91878a..223dadc306 100644 --- a/include/RequestManagerAllocate.h +++ b/include/RequestManagerAllocate.h @@ -61,12 +61,11 @@ protected: virtual Template * get_object_template() { return 0; }; - virtual int pool_allocate(xmlrpc_c::paramList const& _paramList, + virtual int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, - int& id, + int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { return -1; }; @@ -77,10 +76,9 @@ protected: string& error_str, RequestAttributes& att, int cluster_id, - const string& cluster_name, - int umask) + const string& cluster_name) { - return pool_allocate(_paramList, tmpl, id, error_str, att, umask); + return pool_allocate(_paramList, tmpl, id, error_str, att); }; virtual int get_cluster_id(xmlrpc_c::paramList const& paramList) @@ -122,7 +120,7 @@ public: "Allocates a new virtual machine", "A:ssb", true) - { + { Nebula& nd = Nebula::instance(); pool = nd.get_vmpool(); auth_object = PoolObjectSQL::VM; @@ -132,17 +130,16 @@ public: /* --------------------------------------------------------------------- */ - Template * get_object_template() - { - return new VirtualMachineTemplate; + Template * get_object_template() + { + return new VirtualMachineTemplate; }; - int pool_allocate(xmlrpc_c::paramList const& _paramList, + int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, - int& id, + int& id, string& error_str, - RequestAttributes& att, - int umask); + RequestAttributes& att); bool allocate_authorization(Template * obj_template, RequestAttributes& att, @@ -160,7 +157,7 @@ public: "Allocates a new virtual network", "A:ssi", true) - { + { Nebula& nd = Nebula::instance(); pool = nd.get_vnpool(); auth_object = PoolObjectSQL::NET; @@ -170,9 +167,9 @@ public: /* --------------------------------------------------------------------- */ - Template * get_object_template() - { - return new VirtualNetworkTemplate; + Template * get_object_template() + { + return new VirtualNetworkTemplate; }; int pool_allocate(xmlrpc_c::paramList const& _paramList, @@ -181,8 +178,7 @@ public: string& error_str, RequestAttributes& att, int cluster_id, - const string& cluster_name, - int umask); + const string& cluster_name); int get_cluster_id(xmlrpc_c::paramList const& paramList) { @@ -235,7 +231,7 @@ public: "Allocates a new virtual machine template", "A:ss", true) - { + { Nebula& nd = Nebula::instance(); pool = nd.get_tpool(); auth_object = PoolObjectSQL::TEMPLATE; @@ -245,17 +241,16 @@ public: /* --------------------------------------------------------------------- */ - Template * get_object_template() - { - return new VirtualMachineTemplate; + Template * get_object_template() + { + return new VirtualMachineTemplate; }; - int pool_allocate(xmlrpc_c::paramList const& _paramList, + int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, - int& id, + int& id, string& error_str, - RequestAttributes& att, - int umask); + RequestAttributes& att); }; /* ------------------------------------------------------------------------- */ @@ -269,7 +264,7 @@ public: "Allocates a new host", "A:sssssi", false) - { + { Nebula& nd = Nebula::instance(); pool = nd.get_hpool(); auth_object = PoolObjectSQL::HOST; @@ -285,8 +280,7 @@ public: string& error_str, RequestAttributes& att, int cluster_id, - const string& cluster_name, - int umask); + const string& cluster_name); int get_cluster_id(xmlrpc_c::paramList const& paramList) { @@ -314,7 +308,7 @@ public: "Returns user information", "A:ssss", false) - { + { Nebula& nd = Nebula::instance(); pool = nd.get_upool(); auth_object = PoolObjectSQL::USER; @@ -322,12 +316,11 @@ public: ~UserAllocate(){}; - int pool_allocate(xmlrpc_c::paramList const& _paramList, + int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, - int& id, + int& id, string& error_str, - RequestAttributes& att, - int umask); + RequestAttributes& att); void log_xmlrpc_param( const xmlrpc_c::value& v, @@ -346,7 +339,7 @@ public: "Allocates a new group", "A:ss", false) - { + { Nebula& nd = Nebula::instance(); pool = nd.get_gpool(); auth_object = PoolObjectSQL::GROUP; @@ -354,12 +347,11 @@ public: ~GroupAllocate(){}; - int pool_allocate(xmlrpc_c::paramList const& _paramList, + int pool_allocate(xmlrpc_c::paramList const& _paramList, Template * tmpl, - int& id, + int& id, string& error_str, - RequestAttributes& att, - int umask); + RequestAttributes& att); }; /* ------------------------------------------------------------------------- */ @@ -394,8 +386,7 @@ public: string& error_str, RequestAttributes& att, int cluster_id, - const string& cluster_name, - int umask); + const string& cluster_name); int get_cluster_id(xmlrpc_c::paramList const& paramList) { @@ -449,8 +440,7 @@ public: Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask); + RequestAttributes& att); }; /* ------------------------------------------------------------------------- */ @@ -483,8 +473,7 @@ public: Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask); + RequestAttributes& att); }; /* ------------------------------------------------------------------------- */ @@ -520,8 +509,7 @@ public: Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask); + RequestAttributes& att); }; /* -------------------------------------------------------------------------- */ diff --git a/include/RequestManagerClone.h b/include/RequestManagerClone.h index 0cad867228..5107f65db0 100644 --- a/include/RequestManagerClone.h +++ b/include/RequestManagerClone.h @@ -49,8 +49,7 @@ protected: Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) = 0; + RequestAttributes& att) = 0; }; /* ------------------------------------------------------------------------- */ @@ -84,15 +83,14 @@ public: Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { VMTemplatePool * tpool = static_cast(pool); VirtualMachineTemplate * ttmpl = static_cast(tmpl); - return tpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, + return tpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, ttmpl, &id, error_str); }; }; @@ -129,14 +127,13 @@ public: Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { DocumentPool * docpool = static_cast(pool); Document * doc = docpool->get(source_id, true); - return docpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, - doc->get_document_type(), tmpl, &id, error_str); + return docpool->allocate(att.uid, att.gid, att.uname, att.gname, + att.umask, doc->get_document_type(), tmpl, &id, error_str); }; }; diff --git a/include/User.h b/include/User.h index 0d40bf23c0..6e7a1520a9 100644 --- a/include/User.h +++ b/include/User.h @@ -168,6 +168,13 @@ public: */ int get_umask() const; + /** + * Returns the default UMASK attribute (octal) from oned.conf + * + * @return the UMASK to create new objects + */ + static int get_default_umask(); + /** * Returns a copy of the groups for the user */ diff --git a/include/UserPool.h b/include/UserPool.h index 38a8fc5117..5f9cdde3ce 100644 --- a/include/UserPool.h +++ b/include/UserPool.h @@ -140,6 +140,7 @@ public: * @param uname of the user if authN succeeded "" otherwise * @param gname of the group if authN succeeded "" otherwise * @param group_ids the user groups if authN succeeded, is empty otherwise + * @param umask of the user, 0 otherwise * * @return false if authn failed, true otherwise */ @@ -148,7 +149,8 @@ public: int& gid, string& uname, string& gname, - set& group_ids); + set& group_ids, + int& umask); /** * Returns whether the operations described in a authorization request are * authorized ot not. @@ -223,7 +225,8 @@ private: int& group_id, string& uname, string& gname, - set& group_ids); + set& group_ids, + int& umask); /** * Function to authenticate internal users using a server driver @@ -234,7 +237,8 @@ private: int& group_id, string& uname, string& gname, - set& group_ids); + set& group_ids, + int& umask); /** @@ -246,7 +250,8 @@ private: int& group_id, string& uname, string& gname, - set& group_ids); + set& group_ids, + int& umask); /** * Factory method to produce User objects * @return a pointer to the new User diff --git a/src/rm/Request.cc b/src/rm/Request.cc index 40ce02572a..f0f5d4c0f8 100644 --- a/src/rm/Request.cc +++ b/src/rm/Request.cc @@ -36,12 +36,13 @@ void Request::execute( Nebula& nd = Nebula::instance(); UserPool* upool = nd.get_upool(); - bool authenticated = upool->authenticate( att.session, - att.uid, - att.gid, - att.uname, - att.gname, - att.group_ids); + bool authenticated = upool->authenticate(att.session, + att.uid, + att.gid, + att.uname, + att.gname, + att.group_ids, + att.umask); log_method_invoked(att, _paramList); diff --git a/src/rm/RequestManagerAllocate.cc b/src/rm/RequestManagerAllocate.cc index f4188c8aa0..7178ca0c9d 100644 --- a/src/rm/RequestManagerAllocate.cc +++ b/src/rm/RequestManagerAllocate.cc @@ -131,31 +131,13 @@ void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params, Template * tmpl = 0; string error_str; - int rc, id, umask; + int rc, id; Cluster * cluster = 0; int cluster_id = ClusterPool::NONE_CLUSTER_ID; string cluster_name = ClusterPool::NONE_CLUSTER_NAME; PoolObjectAuth cluster_perms; - User * user; - UserPool * upool = Nebula::instance().get_upool(); - - user = upool->get(att.uid, true); - - if ( user == 0 ) - { - failure_response(NO_EXISTS, - get_error(object_name(PoolObjectSQL::USER), att.uid), - att); - - return; - } - - umask = user->get_umask(); - - user->unlock(); - if ( do_template == true ) { string str_tmpl = xmlrpc_c::value_string(params.getString(1)); @@ -197,7 +179,7 @@ void RequestManagerAllocate::request_execute(xmlrpc_c::paramList const& params, return; } - rc = pool_allocate(params, tmpl, id, error_str, att, cluster_id, cluster_name, umask); + rc = pool_allocate(params, tmpl, id, error_str,att,cluster_id,cluster_name); if ( rc < 0 ) { @@ -257,8 +239,7 @@ int VirtualMachineAllocate::pool_allocate( Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { bool on_hold = false; @@ -272,7 +253,7 @@ int VirtualMachineAllocate::pool_allocate( Template tmpl_back(*tmpl); - int rc = vmpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, + int rc = vmpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, ttmpl, &id, error_str, on_hold); if ( rc < 0 ) @@ -294,13 +275,12 @@ int VirtualNetworkAllocate::pool_allocate( string& error_str, RequestAttributes& att, int cluster_id, - const string& cluster_name, - int umask) + const string& cluster_name) { VirtualNetworkPool * vpool = static_cast(pool); VirtualNetworkTemplate * vtmpl=static_cast(tmpl); - return vpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, + return vpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, vtmpl, &id, cluster_id, cluster_name, error_str); } @@ -330,7 +310,6 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params, Nebula& nd = Nebula::instance(); - UserPool * upool = nd.get_upool(); DatastorePool * dspool = nd.get_dspool(); ImagePool * ipool = static_cast(pool); ImageManager * imagem = nd.get_imagem(); @@ -338,32 +317,13 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params, ImageTemplate * tmpl; Template img_usage; - User * user; Datastore * ds; Image::DiskType ds_disk_type; long long avail; - int umask; bool ds_check; - // ------------------------- Get user's umask ------------------------------ - - user = upool->get(att.uid, true); - - if ( user == 0 ) - { - failure_response(NO_EXISTS, - get_error(object_name(PoolObjectSQL::USER), att.uid), - att); - - return; - } - - umask = user->get_umask(); - - user->unlock(); - // ------------------------- Parse image template -------------------------- tmpl = new ImageTemplate; @@ -512,7 +472,7 @@ void ImageAllocate::request_execute(xmlrpc_c::paramList const& params, att.gid, att.uname, att.gname, - umask, + att.umask, tmpl, ds_id, ds_name, @@ -552,15 +512,14 @@ int TemplateAllocate::pool_allocate( Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { VMTemplatePool * tpool = static_cast(pool); VirtualMachineTemplate * ttmpl=static_cast(tmpl); - return tpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, ttmpl, - &id, error_str); + return tpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, + ttmpl, &id, error_str); } /* -------------------------------------------------------------------------- */ @@ -573,8 +532,7 @@ int HostAllocate::pool_allocate( string& error_str, RequestAttributes& att, int cluster_id, - const string& cluster_name, - int umask) + const string& cluster_name) { string host = xmlrpc_c::value_string(paramList.getString(1)); string im_mad = xmlrpc_c::value_string(paramList.getString(2)); @@ -596,8 +554,7 @@ int UserAllocate::pool_allocate( Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { string uname = xmlrpc_c::value_string(paramList.getString(1)); string passwd = xmlrpc_c::value_string(paramList.getString(2)); @@ -647,8 +604,7 @@ int GroupAllocate::pool_allocate( Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { string gname = xmlrpc_c::value_string(paramList.getString(1)); @@ -667,13 +623,12 @@ int DatastoreAllocate::pool_allocate( string& error_str, RequestAttributes& att, int cluster_id, - const string& cluster_name, - int umask) + const string& cluster_name) { DatastorePool * dspool = static_cast(pool); DatastoreTemplate * ds_tmpl = static_cast(tmpl); - return dspool->allocate(att.uid, att.gid, att.uname, att.gname, umask, + return dspool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, ds_tmpl, &id, cluster_id, cluster_name, error_str); } @@ -685,8 +640,7 @@ int ClusterAllocate::pool_allocate( Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { string name = xmlrpc_c::value_string(paramList.getString(1)); @@ -703,14 +657,13 @@ int DocumentAllocate::pool_allocate( Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { int type = xmlrpc_c::value_int(paramList.getInt(2)); DocumentPool * docpool = static_cast(pool); - return docpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, + return docpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, type, tmpl, &id, error_str); } @@ -739,8 +692,7 @@ int ZoneAllocate::pool_allocate( Template * tmpl, int& id, string& error_str, - RequestAttributes& att, - int umask) + RequestAttributes& att) { string name = xmlrpc_c::value_string(paramList.getString(1)); diff --git a/src/rm/RequestManagerClone.cc b/src/rm/RequestManagerClone.cc index 20d1ea6747..2de7c4d24e 100644 --- a/src/rm/RequestManagerClone.cc +++ b/src/rm/RequestManagerClone.cc @@ -28,33 +28,15 @@ void RequestManagerClone::request_execute( int source_id = xmlrpc_c::value_int(paramList.getInt(1)); string name = xmlrpc_c::value_string(paramList.getString(2)); - int rc, new_id, umask; + int rc, new_id; PoolObjectAuth perms; Template * tmpl; PoolObjectSQL * source_obj; - User * user; - - UserPool * upool = Nebula::instance().get_upool(); string error_str; - user = upool->get(att.uid, true); - - if ( user == 0 ) - { - failure_response(NO_EXISTS, - get_error(object_name(PoolObjectSQL::USER), att.uid), - att); - - return; - } - - umask = user->get_umask(); - - user->unlock(); - source_obj = pool->get(source_id, true); if ( source_obj == 0 ) @@ -98,7 +80,7 @@ void RequestManagerClone::request_execute( } } - rc = pool_allocate(source_id, tmpl, new_id, error_str, att, umask); + rc = pool_allocate(source_id, tmpl, new_id, error_str, att); if ( rc < 0 ) { diff --git a/src/rm/RequestManagerImage.cc b/src/rm/RequestManagerImage.cc index 69467d987f..5cd8076015 100644 --- a/src/rm/RequestManagerImage.cc +++ b/src/rm/RequestManagerImage.cc @@ -230,7 +230,7 @@ void ImageClone::request_execute( string name = xmlrpc_c::value_string(paramList.getString(2)); long long avail, size; - int rc, new_id, ds_id, umask; + int rc, new_id, ds_id; string error_str, ds_name, ds_data; bool ds_check; @@ -241,30 +241,11 @@ void ImageClone::request_execute( Template img_usage; Image * img; Datastore * ds; - User * user; Nebula& nd = Nebula::instance(); DatastorePool * dspool = nd.get_dspool(); ImagePool * ipool = static_cast(pool); - UserPool * upool = nd.get_upool(); - - // ------------------------- Get user's umask ------------------------------ - - user = upool->get(att.uid, true); - - if ( user == 0 ) - { - failure_response(NO_EXISTS, - get_error(object_name(PoolObjectSQL::USER), att.uid), - att); - - return; - } - - umask = user->get_umask(); - - user->unlock(); // ------------------------- Get source Image info ------------------------- @@ -391,7 +372,7 @@ void ImageClone::request_execute( att.gid, att.uname, att.gname, - umask, + att.umask, tmpl, ds_id, ds_name, diff --git a/src/rm/RequestManagerVMTemplate.cc b/src/rm/RequestManagerVMTemplate.cc index ce22fbebcc..8569c487c5 100644 --- a/src/rm/RequestManagerVMTemplate.cc +++ b/src/rm/RequestManagerVMTemplate.cc @@ -31,7 +31,6 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList int rc; int vid; - int umask; ostringstream sid; @@ -41,12 +40,10 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList VirtualMachinePool* vmpool = nd.get_vmpool(); VMTemplatePool * tpool = static_cast(pool); - UserPool * upool = nd.get_upool(); VirtualMachineTemplate * tmpl; VirtualMachineTemplate uattrs; VMTemplate * rtmpl; - User * user; string error_str; string aname; @@ -60,25 +57,6 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList str_uattrs = xmlrpc_c::value_string(paramList.getString(4)); } - /* ---------------------------------------------------------------------- */ - /* Get user's umask */ - /* ---------------------------------------------------------------------- */ - - user = upool->get(att.uid, true); - - if ( user == 0 ) - { - failure_response(NO_EXISTS, - get_error(object_name(PoolObjectSQL::USER), att.uid), - att); - - return; - } - - umask = user->get_umask(); - - user->unlock(); - /* ---------------------------------------------------------------------- */ /* Get, check and clone the template */ /* ---------------------------------------------------------------------- */ @@ -212,7 +190,7 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList Template tmpl_back(*tmpl); - rc = vmpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, + rc = vmpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, tmpl, &vid, error_str, on_hold); if ( rc < 0 ) diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index 36bbdaddad..191303e817 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -1025,7 +1025,6 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis ImagePool * ipool = nd.get_ipool(); DatastorePool * dspool = nd.get_dspool(); - UserPool * upool = nd.get_upool(); VMTemplatePool* tpool = nd.get_tpool(); int id = xmlrpc_c::value_int(paramList.getInt(1)); @@ -1057,24 +1056,6 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis string target; string dev_prefix; - // ------------------------------------------------------------------------- - // Get user's umask - // ------------------------------------------------------------------------- - User * user = upool->get(att.uid, true); - - if ( user == 0 ) - { - failure_response(NO_EXISTS, - get_error(object_name(PoolObjectSQL::USER), att.uid), - att); - - return; - } - - int umask = user->get_umask(); - - user->unlock(); - // ------------------------------------------------------------------------- // Prepare and check the VM/DISK to be saved_as // ------------------------------------------------------------------------- @@ -1306,7 +1287,7 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis att.gid, att.uname, att.gname, - umask, + att.umask, itemplate, ds_id, ds_name, @@ -1419,7 +1400,7 @@ void VirtualMachineSaveDisk::request_execute(xmlrpc_c::paramList const& paramLis //Allocate the template - rc = tpool->allocate(att.uid, att.gid, att.uname, att.gname, umask, + rc = tpool->allocate(att.uid, att.gid, att.uname, att.gname, att.umask, tmpl, &ntid, error_str); if (rc < 0) diff --git a/src/um/User.cc b/src/um/User.cc index 2677be0b15..30a4196e9c 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -413,5 +413,21 @@ int User::get_umask() const return (umask & 0777); } +int User::get_default_umask() +{ + string umask_st; + int umask; + + istringstream iss; + + Nebula::instance().get_configuration_attribute("DEFAULT_UMASK",umask_st); + + iss.str(umask_st); + + iss >> oct >> umask; + + return (umask & 0777); +} + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index 923b2b3b3d..8375f7d143 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -419,7 +419,8 @@ bool UserPool::authenticate_internal(User * user, int& group_id, string& uname, string& gname, - set& group_ids) + set& group_ids, + int& umask) { bool result = false; @@ -440,13 +441,15 @@ bool UserPool::authenticate_internal(User * user, group_ids = user->get_groups(); - uname = user->name; - gname = user->gname; + uname = user->name; + gname = user->gname; auth_driver = user->auth_driver; result = user->valid_session(token); + umask = user->get_umask(); + user->unlock(); if (result) @@ -523,6 +526,8 @@ auth_failure: uname = ""; gname = ""; + umask = 0; + return false; } @@ -535,7 +540,8 @@ bool UserPool::authenticate_server(User * user, int& group_id, string& uname, string& gname, - set& group_ids) + set& group_ids, + int& umask) { bool result = false; @@ -585,6 +591,8 @@ bool UserPool::authenticate_server(User * user, result = user->valid_session(second_token); + umask = user->get_umask(); + user->unlock(); if (result) @@ -654,6 +662,8 @@ auth_failure: uname = ""; gname = ""; + umask = 0; + return false; } @@ -666,7 +676,8 @@ bool UserPool::authenticate_external(const string& username, int& group_id, string& uname, string& gname, - set& group_ids) + set& group_ids, + int& umask) { ostringstream oss; istringstream is; @@ -741,6 +752,8 @@ bool UserPool::authenticate_external(const string& username, uname = mad_name; gname = GroupPool::USERS_NAME; + umask = User::get_default_umask(); + return true; auth_failure_user: @@ -770,6 +783,8 @@ auth_failure: uname = ""; gname = ""; + umask = 0; + return false; } @@ -781,7 +796,8 @@ bool UserPool::authenticate(const string& session, int& group_id, string& uname, string& gname, - set& group_ids) + set& group_ids, + int& umask) { User * user = 0; string username; @@ -805,16 +821,19 @@ bool UserPool::authenticate(const string& session, if ( fnmatch(UserPool::SERVER_AUTH, driver.c_str(), 0) == 0 ) { - ar = authenticate_server(user,token,user_id,group_id,uname,gname,group_ids); + ar = authenticate_server(user, token, user_id, group_id, uname, + gname, group_ids, umask); } else { - ar = authenticate_internal(user,token,user_id,group_id,uname,gname,group_ids); + ar = authenticate_internal(user, token, user_id, group_id, uname, + gname, group_ids, umask); } } else { - ar = authenticate_external(username,token,user_id,group_id,uname,gname,group_ids); + ar = authenticate_external(username, token, user_id, group_id, uname, + gname, group_ids, umask); } return ar; From d5deaaf9e48c0eb16364bea62dbdae27219977f5 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 27 May 2014 17:19:36 +0200 Subject: [PATCH 28/84] feature #2858: First version of VNET reservation. VNET is created, addresses allocated. Needs to set free addr methods upon reservation removal. --- include/AddressRange.h | 102 ++++++++++---- include/AddressRangePool.h | 55 +++++++- include/RequestManagerVirtualNetwork.h | 26 ++++ include/VirtualNetwork.h | 86 +++++++++--- src/cli/one_helper/onevnet_helper.rb | 18 ++- src/cli/onevnet | 27 ++++ src/oca/ruby/opennebula/virtual_network.rb | 18 ++- src/rm/RequestManager.cc | 2 + src/rm/RequestManagerVirtualNetwork.cc | 146 ++++++++++++++++++++- src/vnm/AddressRange.cc | 103 +++++++++++++++ src/vnm/AddressRangePool.cc | 46 +++++++ src/vnm/VirtualNetwork.cc | 18 +++ 12 files changed, 596 insertions(+), 51 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index e0c6526a64..dc030e8464 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -38,6 +38,40 @@ public: virtual ~AddressRange(){}; + // ************************************************************************* + // Address Range types + // ************************************************************************* + + /** + * Type of Addresses defined by this address range + */ + enum AddressType + { + NONE = 0x00000000, /** Undefined Address Type */ + ETHER = 0x00000001, /** MAC address type */ + IP4 = 0x00000003, /** IP version 4 address */ + IP6 = 0x00000005, /** IP version 6 address */ + IP4_6 = 0x00000007 /** IP dual stack version 4 & 6 addresses */ + }; + + /** + * Return the string representation of an AddressType + * @param ob the type + * @return the string + */ + static string type_to_str(AddressType ob); + + /** + * Return the string representation of an AddressType + * @param ob the type + * @return the string + */ + static AddressType str_to_type(string& str_type); + + // ************************************************************************* + // Address Range initialization functions + // ************************************************************************* + /** * Init an Address Range based on a vector attribute the following * attributes will be parsed (* are optional): @@ -84,31 +118,9 @@ public: */ void to_xml(ostringstream &oss) const; - /** - * Type of Addresses defined by this address range - */ - enum AddressType - { - NONE = 0x00000000, /** Undefined Address Type */ - ETHER = 0x00000001, /** MAC address type */ - IP4 = 0x00000003, /** IP version 4 address */ - IP6 = 0x00000005, /** IP version 6 address */ - IP4_6 = 0x00000007 /** IP dual stack version 4 & 6 addresses */ - }; - - /** - * Return the string representation of an AddressType - * @param ob the type - * @return the string - */ - static string type_to_str(AddressType ob); - - /** - * Return the string representation of an AddressType - * @param ob the type - * @return the string - */ - static AddressType str_to_type(string& str_type); + // ************************************************************************* + // Address allocation functions + // ************************************************************************* /** * Returns an unused address, which becomes used and fills a NIC attribute @@ -177,6 +189,38 @@ public: */ int free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const string& ip); + /** + * Frees all previous allocated address to the given object + * @param ot the object type of the owner of the address + * @param obid the id of the owner of the address + * @return the number of addresses freed + */ + int free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid); + + // ************************************************************************* + // Address Reservation + // ************************************************************************* + + /** + * Reserve a given number of addresses from this address range + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param size number of addresses to reserve + * @param rar a new address range to place the reservation + * @return 0 on success + */ + int reserve_addr(int pvid, int vid, unsigned int rsize, AddressRange *rar); + + /* + void reserve_addr_by_ip(); + + void reserve_addr_by_mac(); + */ + + // ************************************************************************* + // Helpers + // ************************************************************************* + /** * Return the id for this address range */ @@ -193,6 +237,14 @@ public: return used_addr; } + /** + * Return the number of free addresses + */ + unsigned int get_free_addr() const + { + return size - used_addr; + } + /** * Returns the string value of an Address Range Attribute * @param name of the attribute diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index cdf81b28a7..eb30cbce0a 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -37,6 +37,10 @@ public: virtual ~AddressRangePool(); + // ************************************************************************* + // Inititalization functions + // ************************************************************************* + /** * Builds the address range set from an array of VectorAttributes. This * function is used to create address ranges. @@ -54,6 +58,10 @@ public: */ int from_xml_node(const xmlNodePtr node); + // ************************************************************************* + // Address Range management interface + // ************************************************************************* + /** * Removes an address range from the pool if it does not contain any used * leases @@ -70,12 +78,14 @@ public: void update_ar(vector ars); /** - * Generate a XML representation of the Address Range Pool - * @param sstream where the ARPool is written - * @param extended true to include lease information - * @return the string with the XML + * Allocates a new address range in the pool. + * @return the new address range added to the pool */ - string& to_xml(string& sstream, bool extended) const; + AddressRange * allocate_ar(); + + // ************************************************************************* + // Address allocation interface + // ************************************************************************* /** * Allocates an address in a suitable address range from the pool @@ -182,6 +192,33 @@ public: */ void free_addr_by_ip(PoolObjectSQL::ObjectType ot, int id, const string& ip); + /** + * Frees all the addressed owned by the given object + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + */ + void free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid); + + // ************************************************************************* + // Address reservation + // ************************************************************************* + + /** + * Reserve a given number of addresses from the first address range with + * enough free addresses to allocate the reservation + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param size number of addresses to reserve + * @param rar a new address range to place the reservation + * @return 0 on success + */ + int reserve_addr(int pvid, int vid, unsigned int rsize, AddressRange *rar); + + + // ************************************************************************* + // Helpers & Formatting + // ************************************************************************* + /** * Return the number of used addresses */ @@ -198,6 +235,14 @@ public: */ void get_attribute(const char * name, string& value, int ar_id) const; + /** + * Generate a XML representation of the Address Range Pool + * @param sstream where the ARPool is written + * @param extended true to include lease information + * @return the string with the XML + */ + string& to_xml(string& sstream, bool extended) const; + private: /** * Stores the Address Ranges in a template form. This template is used diff --git a/include/RequestManagerVirtualNetwork.h b/include/RequestManagerVirtualNetwork.h index 918e73397b..29221933c0 100644 --- a/include/RequestManagerVirtualNetwork.h +++ b/include/RequestManagerVirtualNetwork.h @@ -49,6 +49,7 @@ protected: virtual int leases_action(VirtualNetwork * vn, VirtualNetworkTemplate * tmpl, + RequestAttributes& att, string& error_str) = 0; /* -------------------------------------------------------------------- */ @@ -68,6 +69,7 @@ public: int leases_action(VirtualNetwork * vn, VirtualNetworkTemplate * tmpl, + RequestAttributes& att, string& error_str) { return vn->add_ar(tmpl, error_str); @@ -109,6 +111,7 @@ public: int leases_action(VirtualNetwork * vn, VirtualNetworkTemplate * tmpl, + RequestAttributes& att, string& error_str) { error_str.clear(); @@ -132,6 +135,7 @@ public: int leases_action(VirtualNetwork * vn, VirtualNetworkTemplate * tmpl, + RequestAttributes& att, string& error_str) { return vn->hold_leases(tmpl, error_str); @@ -151,12 +155,34 @@ public: int leases_action(VirtualNetwork * vn, VirtualNetworkTemplate * tmpl, + RequestAttributes& att, string& error_str) { return vn->free_leases(tmpl, error_str); } }; +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +class VirtualNetworkReserve: public Request +{ +public: + VirtualNetworkReserve():Request("VirtualNetworkReserve", "A:sis", + "Reserve network addresses") + { + Nebula& nd = Nebula::instance(); + pool = nd.get_vnpool(); + + auth_object = PoolObjectSQL::NET; + auth_op = AuthRequest::USE; + }; + + ~VirtualNetworkReserve(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); +}; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index f8a1dbb920..bfff9fc3ec 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -56,6 +56,10 @@ public: return new VirtualNetworkTemplate; } + // ************************************************************************* + // Address Range management interface + // ************************************************************************* + /** * Add an address range to the virtual network * @param ars_tmpl template in the form AR = [TYPE=...,IP=...,SIZE=...]. @@ -72,6 +76,15 @@ public: */ int rm_ar(unsigned int ar_id, string& error_msg); + /** + * Allocates a new (and empty) address range to the AR pool + * @return pointer to the ar added to the AR pool + */ + AddressRange * allocate_ar() + { + return ar_pool.allocate_ar(); + } + /** * Update an address range to the virtual network * @param ars_tmpl template in the form AR = [AR_ID=...]. The address range @@ -79,6 +92,10 @@ public: */ void update_ar(VirtualNetworkTemplate * ars_tmpl); + // ************************************************************************* + // Address hold/release interface + // ************************************************************************* + /** * Holds a Lease, marking it as used * @param leases template in the form LEASES = [IP=XX]. @@ -98,6 +115,10 @@ public: */ int free_leases(VirtualNetworkTemplate* leases, string& error_msg); + // ************************************************************************* + // Address allocation funtions + // ************************************************************************* + /** * Gets a new address lease for a specific VM * @param vid VM identifier @@ -144,13 +165,51 @@ public: * @param arid of the address range where the address was leased from * @param vid the ID of the VM * @param mac MAC address identifying the lease - * @return 0 if success */ void free_addr(unsigned int arid, int vid, const string& mac) { ar_pool.free_addr(arid, PoolObjectSQL::VM, vid, mac); } + /** + * Release all previously given address leases to the given object + * @param ot the type of the object requesting the address (VM or NET) + * @param obid the id of the object requesting the address + */ + void free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid) + { + ar_pool.free_addr_by_owner(ot, obid); + } + + /** + * Modifies the given nic attribute adding the following attributes: + * * IP: leased from network + * * MAC: leased from network + * * BRIDGE: for this virtual network + * @param nic attribute for the VM template + * @param vid of the VM getting the lease + * @param inherit_attrs Attributes to be inherited from the vnet template + * into the nic + * @return 0 on success + */ + int nic_attribute( + VectorAttribute * nic, + int vid, + const vector& inherit_attrs); + + // ************************************************************************* + // Network Reservation functions + // ************************************************************************* + + int reserve_addr(VirtualNetwork *rvnet, unsigned int rsize, string& error_str); + + + + + // ************************************************************************* + // Formatting & Helper functions + // ************************************************************************* + /** * Gets used leases * @return number of network leases in used @@ -185,22 +244,6 @@ public: */ string& to_xml_extended(string& xml) const; - /** - * Modifies the given nic attribute adding the following attributes: - * * IP: leased from network - * * MAC: leased from network - * * BRIDGE: for this virtual network - * @param nic attribute for the VM template - * @param vid of the VM getting the lease - * @param inherit_attrs Attributes to be inherited from the vnet template - * into the nic - * @return 0 on success - */ - int nic_attribute( - VectorAttribute * nic, - int vid, - const vector& inherit_attrs); - /** * Replace the template of the virtual network it also updates the BRIDGE, * PHY_DEV, VLAN_ID and VLAN attributes. @@ -219,6 +262,15 @@ public: */ void get_template_attribute(const char * name, string& value, int ar_id) const; + /** + * @return A copy of the VNET Template + */ + VirtualNetworkTemplate * clone_template() const + { + return new VirtualNetworkTemplate( + *(static_cast(obj_template))); + }; + private: // ------------------------------------------------------------------------- diff --git a/src/cli/one_helper/onevnet_helper.rb b/src/cli/one_helper/onevnet_helper.rb index e834335560..79a0a702cb 100644 --- a/src/cli/one_helper/onevnet_helper.rb +++ b/src/cli/one_helper/onevnet_helper.rb @@ -66,6 +66,22 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper :description => "IP6 ula prefix" } + R_NAME = { + :name => "rname", + :short => "-n reservation name", + :large => "--name reservation name", + :format => String, + :description => "Name of the address reservation" + } + + R_SIZE = { + :name => "rsize", + :short => "-s reservation size", + :large => "--size reservation size", + :format => String, + :description => "Number of addresses to reserve" + } + def self.rname "VNET" end @@ -240,7 +256,7 @@ class OneVNetHelper < OpenNebulaHelper::OneHelper if d['VM'] "VM : #{d['VM']}" elsif d['VNET'] - "NET: #{d['VM']}" + "NET: #{d['VNET']}" end end diff --git a/src/cli/onevnet b/src/cli/onevnet index fac9752c18..87f831b9e6 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -182,6 +182,33 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + reserve_desc = <<-EOT.unindent + Reserve addresses from the Virtual Network + EOT + + command :reserve, reserve_desc, :vnetid, + :options=>STD_OPTIONS + [OneVNetHelper::AR, OneVNetHelper::R_NAME, + OneVNetHelper::R_SIZE] do + helper.perform_action(args[0],options,"reservation made") do |vn| + ar = options[:ar_id] || -1 + rsize = options[:rsize] || -1 + rname = options[:rname] || -1 + + if rsize == -1 + STDERR.puts "Specify a size (-s size) for the reservation" + exit -1 + end + + if rname == -1 + STDERR.puts "Specify a name (-n name) for the reservation" + exit -1 + end + + vn.reserve(rname, rsize) + end + end + + chgrp_desc = <<-EOT.unindent Changes the Virtual Network group EOT diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index 98a2d9435c..aeb52fc201 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -35,7 +35,8 @@ module OpenNebula :update => "vn.update", :hold => "vn.hold", :release => "vn.release", - :rename => "vn.rename" + :rename => "vn.rename", + :reserve => "vn.reserve" } # Creates a VirtualNetwork description with just its identifier @@ -185,6 +186,21 @@ module OpenNebula return rc end + # Reserve a set of addresses from this virtual network + # @param name [String] of the reservation + # @param rsize[String] number of addresses to reserve + def reserve(rname, rsize) + return Error.new('ID not defined') if !@pe_id + + rtmpl = "NAME = #{rname}\n" + rtmpl << "SIZE = #{rsize}\n" + + rc = @client.call(VN_METHODS[:reserve], @pe_id, rtmpl) + rc = nil if !OpenNebula.is_error?(rc) + + return rc + end + # Changes the owner/group # # @param uid [Integer] the new owner id. Set to -1 to leave the current one diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 046f59c745..7b3dba92b8 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -281,6 +281,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr vn_update_ar(new VirtualNetworkUpdateAddressRange()); xmlrpc_c::methodPtr vn_hold(new VirtualNetworkHold()); xmlrpc_c::methodPtr vn_release(new VirtualNetworkRelease()); + xmlrpc_c::methodPtr vn_reserve(new VirtualNetworkReserve()); // Update Template Methods xmlrpc_c::methodPtr image_update(new ImageUpdateTemplate()); @@ -486,6 +487,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.groupquota.update", group_set_default_quota); /* Network related methods*/ + RequestManagerRegistry.addMethod("one.vn.reserve", vn_reserve); RequestManagerRegistry.addMethod("one.vn.add_ar", vn_add_ar); RequestManagerRegistry.addMethod("one.vn.rm_ar", vn_rm_ar); RequestManagerRegistry.addMethod("one.vn.update_ar", vn_update_ar); diff --git a/src/rm/RequestManagerVirtualNetwork.cc b/src/rm/RequestManagerVirtualNetwork.cc index d49afa7942..a6bec568b1 100644 --- a/src/rm/RequestManagerVirtualNetwork.cc +++ b/src/rm/RequestManagerVirtualNetwork.cc @@ -24,7 +24,7 @@ using namespace std; string RequestManagerVirtualNetwork::leases_error (const string& error) { - return request_error("Error modifying network leases",error); + return request_error("Error modifying network leases", error); } /* ------------------------------------------------------------------------- */ @@ -64,7 +64,7 @@ void RequestManagerVirtualNetwork:: return; } - rc = leases_action(vn,&tmpl,error_str); + rc = leases_action(vn, &tmpl, att, error_str); if ( rc < 0 ) { @@ -127,3 +127,145 @@ void VirtualNetworkRmAddressRange:: success_response(id, att); } + +/* ------------------------------------------------------------------------- */ +/* ------------------------------------------------------------------------- */ + +void VirtualNetworkReserve::request_execute( + xmlrpc_c::paramList const& paramList, RequestAttributes& att) +{ + int id = xmlrpc_c::value_int (paramList.getInt(1)); + string str_tmpl = xmlrpc_c::value_string (paramList.getString(2)); + + VirtualNetworkTemplate tmpl; + VirtualNetwork * vn; + + string error_str; + int rc; + + if ( basic_authorization(id, att) == false ) + { + return; + } + + // ----------------- Process the Reservation Template ------------------- + + rc = tmpl.parse_str_or_xml(str_tmpl, error_str); + + if ( rc != 0 ) + { + failure_response(ACTION, + request_error("Error in reservation request", error_str), att); + + return; + } + + int size; + + if ( !tmpl.get("SIZE", size) || size <= 0 ) + { + failure_response(ACTION, request_error("Error in reservation request", + "Reservation SIZE must be a greater than 0"), att); + + return; + } + + string name; + + tmpl.get("NAME", name); + + if (name.empty()) + { + failure_response(ACTION, request_error("Error in reservation request", + "NAME for reservation has to be set"), att); + + return; + } + + // --------------- Create a new VNET to place the reservation ------------- + + vn = static_cast(pool->get(id,true)); + + if ( vn == 0 ) + { + failure_response(NO_EXISTS, get_error(object_name(auth_object),id),att); + return; + } + + VirtualNetworkTemplate * vtmpl = vn->clone_template(); + + vtmpl->replace("NAME", name); + + int rid; + int cluster_id = vn->get_cluster_id(); + + rc = (static_cast(pool))->allocate(att.uid, att.gid, + att.uname, att.gname, att.umask, vtmpl, &rid, cluster_id, + vn->get_cluster_name(), error_str); + + if (rc < 0) + { + failure_response(INTERNAL, + request_error("Cannot allocate reservation VNET", error_str), att); + + vn->unlock(); + + return; + } + + VirtualNetwork * rvn = static_cast(pool->get(rid,true)); + + if (rvn == 0) + { + failure_response(INTERNAL, + request_error("Cannot allocate reservation VNET",""), att); + + vn->unlock(); + + return; + } + + // -------------- Make address reservation and set it ---------------------- + + if (vn->reserve_addr(rvn, size, error_str) != 0 ) + { + failure_response(ACTION, request_error(error_str,""), att); + + pool->drop(rvn, error_str); + + rvn->unlock(); + + vn->unlock(); + + return; + } + + pool->update(rvn); + + pool->update(vn); + + rvn->unlock(); + + vn->unlock(); + + // -------------- Add the reservation to the cluster ---------------------- + + if ( cluster_id != ClusterPool::NONE_CLUSTER_ID ) + { + Nebula& nd = Nebula::instance(); + + ClusterPool * clpool = nd.get_clpool(); + Cluster * cluster = clpool->get(cluster_id, true); + + if ( cluster != 0 ) + { + cluster->add_vnet(rid, error_str); + + clpool->update(cluster); + + cluster->unlock(); + } + } + + success_response(rid, att); +} diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 876f3ce99d..d8d78bd021 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -178,6 +178,16 @@ int AddressRange::from_vattr(VectorAttribute *vattr, string& error_msg) vattr->replace("AR_ID", id); + vattr->remove("ALLOCATED"); + + vattr->remove("USED_LEASES"); + + vattr->remove("LEASES"); + + vattr->remove("PARENT_NETWORK_AR_ID"); + + vattr->remove("PARENT_NETWORK"); + if (do_mac) //Need to add MAC to the attribute { set_mac(0, attr); @@ -214,6 +224,10 @@ void AddressRange::update_attributes(VectorAttribute *vup) vup->remove("LEASES"); + vup->remove("PARENT_NETWORK_AR_ID"); + + vup->remove("PARENT_NETWORK"); + /* ----------------- update known attributes ----------------- */ unsigned int new_size; @@ -905,6 +919,38 @@ int AddressRange::free_addr_by_ip(PoolObjectSQL::ObjectType ot, int obid, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int AddressRange::free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid) +{ + map::iterator it = allocated.begin(); + + long long obj_pack = ot | (obid & 0x00000000FFFFFFFFLL); + + int freed = 0; + + while (it != allocated.end()) + { + if (it->second == obj_pack) + { + map::iterator prev_it = it++; + + allocated.erase(prev_it); + + used_addr--; + + freed++; + } + else + { + it++; + } + } + + return freed; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + int AddressRange::hold_by_ip(const string& ip_s) { if (!(type & 0x00000002))//Not of type IP4 or IP4_6 @@ -965,6 +1011,63 @@ int AddressRange::hold_by_mac(const string& mac_s) return 0; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::reserve_addr(int pvid, int vid, unsigned int rsize, + AddressRange *rar) +{ + bool isset = false; + unsigned int first_index; + int pnet; + + if (rsize > (size - used_addr)) + { + return -1; //reservation dosen't fit + } + + if ((attr->vector_value("PARENT_NETWORK_ID", pnet) == 0 ) && (pnet > -1)) + { + return -1; //This address range is already a reservation + } + + for ( unsigned int i=0; iclone(); + string errmsg; + + set_mac(first_index, new_ar); + + if (type & 0x00000002 ) + { + set_ip(first_index, new_ar); + } + + new_ar->replace("SIZE",rsize); + + rar->from_vattr(new_ar, errmsg); + + new_ar->replace("PARENT_NETWORK_ID", pvid); + + new_ar->replace("PARENT_NETWORK_AR_ID",id); + + return 0; +} + /* ************************************************************************** */ /* ************************************************************************** */ diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 0c3ea6fd0a..3968ddd5a9 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -74,6 +74,18 @@ int AddressRangePool::from_vattr(vector ars, string& error_msg) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +AddressRange * AddressRangePool::allocate_ar() +{ + AddressRange * ar = new AddressRange(next_ar); + + ar_pool.insert(make_pair(next_ar++, ar)); + + return ar; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void AddressRangePool::update_ar(vector ars) { vector::iterator it; @@ -175,6 +187,7 @@ int AddressRangePool::rm_ar(unsigned int ar_id, string& error_msg) vector ars; vector::iterator it_ar; + Attribute * the_ar = 0; unsigned int ar_id_templ; @@ -369,6 +382,19 @@ void AddressRangePool::free_addr_by_ip(PoolObjectSQL::ObjectType ot, int obid, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void AddressRangePool::free_addr_by_owner(PoolObjectSQL::ObjectType ot, int oid) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + used_addr = used_addr - it->second->free_addr_by_owner(ot, oid); + } +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void AddressRangePool::get_attribute(const char * name, string& value, int ar_id) const { @@ -471,3 +497,23 @@ int AddressRangePool::hold_by_mac(const string& mac_s) return rc; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::reserve_addr(int pvid, int vid, unsigned int rsize, + AddressRange *rar) +{ + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if ((it->second->get_free_addr() >= rsize) && + (it->second->reserve_addr(pvid, vid, rsize, rar) == 0)) + { + used_addr += rsize; + return 0; + } + } + + return -1; +} diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 1d5db4bae6..b1b3696e59 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -766,3 +766,21 @@ void VirtualNetwork::get_template_attribute(const char * name, string& value, in PoolObjectSQL::get_template_attribute(name, value); } } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualNetwork::reserve_addr(VirtualNetwork *rvnet, + unsigned int rsize, string& error_str) +{ + AddressRange *rar = rvnet->allocate_ar(); + + if (ar_pool.reserve_addr(oid, rvnet->get_oid(), rsize, rar) != 0) + { + error_str = "Not enough free addresses in an address range"; + + return -1; + } + + return 0; +} From 28e7faf54ed964b82a74c582391ef2892bbda921 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 28 May 2014 10:59:02 +0200 Subject: [PATCH 29/84] feature #2858: Check quotas for reserve addresses. Update AR on reservation --- include/AddressRange.h | 10 ++++++++++ include/AddressRangePool.h | 5 +++++ include/VirtualNetwork.h | 13 ++++++++++--- src/rm/RequestManagerDelete.cc | 22 +++++++++++++++++++++- src/rm/RequestManagerVirtualNetwork.cc | 25 +++++++++++++++++++++++++ src/vnm/AddressRange.cc | 2 ++ src/vnm/AddressRangePool.cc | 21 +++++++++++++++++++++ 7 files changed, 94 insertions(+), 4 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index dc030e8464..663d88ed07 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -255,6 +255,16 @@ public: return attr->vector_value(name); } + /** + * Returns the int value of an Address Range Attribute + * @param name of the attribute + * @return the value of the attribute if found, empty otherwise + */ + int get_attribute(const char *name, int& value) const + { + return attr->vector_value(name, value); + } + /** * Updates the Address Range with the attributes provided. The following * CANNOT be updated: TYPE, SIZE, IP, MAC (plus the internal AR_ID and diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index eb30cbce0a..4dd2f764b4 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -214,6 +214,11 @@ public: */ int reserve_addr(int pvid, int vid, unsigned int rsize, AddressRange *rar); + /** + * Get the parent vnets of the Address Ranges in this AR POOL + * @param parent_vnets vector with the vnet ids of the parent vnets + */ + unsigned int get_parents(vector& parent_nets); // ************************************************************************* // Helpers & Formatting diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index bfff9fc3ec..f82c8ddc6f 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -204,12 +204,9 @@ public: int reserve_addr(VirtualNetwork *rvnet, unsigned int rsize, string& error_str); - - // ************************************************************************* // Formatting & Helper functions // ************************************************************************* - /** * Gets used leases * @return number of network leases in used @@ -219,6 +216,16 @@ public: return ar_pool.get_used_addr(); }; + /** + * Returns the parent networks used to create this VNET (if any) + * @param parents vector of parents networks if any + * @return the number of parents + */ + unsigned int get_parents(vector& parents) + { + return ar_pool.get_parents(parents); + }; + /** * Function to print the VirtualNetwork object into a string in * XML format diff --git a/src/rm/RequestManagerDelete.cc b/src/rm/RequestManagerDelete.cc index 5e727455c3..b4589893cf 100644 --- a/src/rm/RequestManagerDelete.cc +++ b/src/rm/RequestManagerDelete.cc @@ -323,6 +323,8 @@ int ZoneDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) int VirtualNetworkDelete::drop(int oid, PoolObjectSQL * object, string& error_msg) { + vector parents; + VirtualNetwork * vnet = static_cast(object); if ( vnet->get_used() > 0 ) @@ -334,5 +336,23 @@ int VirtualNetworkDelete::drop(int oid, PoolObjectSQL * object, string& error_ms return -1; } - return RequestManagerDelete::drop(oid, object, error_msg); + vnet->get_parents(parents); + + int rc = RequestManagerDelete::drop(oid, object, error_msg); + + for (vector::iterator it = parents.begin(); it < parents.end(); it++) + { + vnet = static_cast(pool->get(*it, true)); + + if (vnet == 0) + { + continue; + } + + vnet->free_addr_by_owner(PoolObjectSQL::NET, oid); + + vnet->unlock(); + } + + return rc; } diff --git a/src/rm/RequestManagerVirtualNetwork.cc b/src/rm/RequestManagerVirtualNetwork.cc index a6bec568b1..888ada2b57 100644 --- a/src/rm/RequestManagerVirtualNetwork.cc +++ b/src/rm/RequestManagerVirtualNetwork.cc @@ -182,12 +182,31 @@ void VirtualNetworkReserve::request_execute( return; } + // --------------- Check/update quotas on the target VNET ----------------- + + ostringstream qtmpl_s; + Template qtmpl; + + for (int i=0; i< size ; i++) + { + qtmpl_s << "NIC = [ NETWORK_ID = " << id << "]" << endl; + } + + qtmpl.parse_str_or_xml(qtmpl_s.str(), error_str); + + if (quota_authorization(&qtmpl, Quotas::NETWORK, att) == false) + { + return; + } + // --------------- Create a new VNET to place the reservation ------------- vn = static_cast(pool->get(id,true)); if ( vn == 0 ) { + quota_rollback(&qtmpl, Quotas::NETWORK, att); + failure_response(NO_EXISTS, get_error(object_name(auth_object),id),att); return; } @@ -205,6 +224,8 @@ void VirtualNetworkReserve::request_execute( if (rc < 0) { + quota_rollback(&qtmpl, Quotas::NETWORK, att); + failure_response(INTERNAL, request_error("Cannot allocate reservation VNET", error_str), att); @@ -217,6 +238,8 @@ void VirtualNetworkReserve::request_execute( if (rvn == 0) { + quota_rollback(&qtmpl, Quotas::NETWORK, att); + failure_response(INTERNAL, request_error("Cannot allocate reservation VNET",""), att); @@ -229,6 +252,8 @@ void VirtualNetworkReserve::request_execute( if (vn->reserve_addr(rvn, size, error_str) != 0 ) { + quota_rollback(&qtmpl, Quotas::NETWORK, att); + failure_response(ACTION, request_error(error_str,""), att); pool->drop(rvn, error_str); diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index d8d78bd021..62086b0af3 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -945,6 +945,8 @@ int AddressRange::free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid) } } + allocated_to_attr(); + return freed; } diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 3968ddd5a9..c4a1eed80f 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -517,3 +517,24 @@ int AddressRangePool::reserve_addr(int pvid, int vid, unsigned int rsize, return -1; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +unsigned int AddressRangePool::get_parents(vector& parent_nets) +{ + int vid; + int num_parents = 0; + + map::iterator it; + + for (it=ar_pool.begin(); it!=ar_pool.end(); it++) + { + if ((it->second->get_attribute("PARENT_NETWORK_ID", vid) == 0) && + (vid >= 0)) + { + parent_nets.push_back(vid); + num_parents++; + } + } +} From 0ef5132bd2c3a58fe75e0c4cfb675558ccd49e6c Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 28 May 2014 12:38:30 +0200 Subject: [PATCH 30/84] feature #2858: Look for continous address ranges --- src/vnm/AddressRange.cc | 44 +++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 62086b0af3..7ae60d0d74 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -1019,7 +1019,6 @@ int AddressRange::hold_by_mac(const string& mac_s) int AddressRange::reserve_addr(int pvid, int vid, unsigned int rsize, AddressRange *rar) { - bool isset = false; unsigned int first_index; int pnet; @@ -1033,22 +1032,47 @@ int AddressRange::reserve_addr(int pvid, int vid, unsigned int rsize, return -1; //This address range is already a reservation } - for ( unsigned int i=0; iclone(); string errmsg; From a11e130a48c04793d6e62cdc8bd6c29adc3007b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 May 2014 11:04:49 +0200 Subject: [PATCH 31/84] Feature #2858: Start redesing of sunstone vnet tab --- src/sunstone/public/js/plugins/vms-tab.js | 2 +- src/sunstone/public/js/plugins/vnets-tab.js | 183 ++++++++++---------- 2 files changed, 91 insertions(+), 94 deletions(-) diff --git a/src/sunstone/public/js/plugins/vms-tab.js b/src/sunstone/public/js/plugins/vms-tab.js index 74930a8f17..506967d0f6 100644 --- a/src/sunstone/public/js/plugins/vms-tab.js +++ b/src/sunstone/public/js/plugins/vms-tab.js @@ -2320,7 +2320,7 @@ function printNics(vm_info){ \ ' + nic.NIC_ID + '\ ' + nic.NETWORK + '\ - ' + nic.IP + '\ + ' + (nic.IP ? nic.IP : "--") + '\ ' + nic.MAC + '\ ' + (nic.IP6_SITE ? nic.IP6_SITE : "--") +'\ ' + (nic.IP6_GLOBAL ? nic.IP6_GLOBAL : "--") +'\ diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index 34c76a35a9..b801d5540c 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -624,9 +624,9 @@ function vNetworkElementArray(vn_json){ network.GNAME, network.NAME, network.CLUSTER.length ? network.CLUSTER : "-", - parseInt(network.TYPE) ? "FIXED" : "RANGED", + "--", network.BRIDGE, - network.TOTAL_LEASES ]; + network.USED_LEASES ]; } //Callback to update a vnet element after an action on it @@ -738,7 +738,6 @@ function updateVNetworkInfo(request,vn){ // hold, release buttons etc. Listeners in setupLeasesOps() function printLeases(vn_info){ var html ='
'; - html +=''; html += '
\ \ @@ -751,52 +750,81 @@ function printLeases(vn_info){ \ \ \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \
'+tr("IPv6 Global prefix")+''+ (typeof(vn_info.GLOBAL_PREFIX) == "object" ? "--": vn_info.GLOBAL_PREFIX) +'
'+tr("IPv6 Site prefix")+''+ (typeof(vn_info.SITE_PREFIX) == "object" ? "--": vn_info.SITE_PREFIX) +'
'; + // TODO: Add other well known attributes, like DNS, GATEWAY... + html += '
'; + + var ar_list = vn_info.AR_POOL.AR; + + if (!ar_list) //empty + { + // TODO: message there are no address ranges + html += '
'; + return html; + } + else if (ar_list.constructor != Array) //>1 lease + { + ar_list = [ar_list]; + }; + + var ar; + + for (var i=0; i\ + '+tr("Address Range information")+'\ \ '; - if (vn_info.TYPE == "0"){ + function ar_attr(val, txt){ + if(val){ + return '\ + '+txt+'\ + '+val+'\ + ' + } + + return ''; + } + + html += ar_attr(ar.AR_ID, "ID"); + + // TODO: translate ar.TYPE values? + html += ar_attr(ar.TYPE, tr("Type")); + html += ar_attr(ar.MAC, tr("MAC")); + html += ar_attr(ar.IP, tr("IP")); + html += ar_attr(ar.GLOBAL_PREFIX, tr("Global prefix")); + html += ar_attr(ar.ULA_PREFIX, tr("ULA prefix")); + html += ar_attr(ar.SIZE, tr("Size")); + html += ar_attr(ar.USED_LEASES, tr("Used leases")); + + // TODO: add, remove AR + + if (Config.isTabActionEnabled("vnets-tab", "Network.hold_lease")) { html += '\ - '+tr("IP Start")+'\ - '+vn_info.RANGE.IP_START+'\ - \ - \ - '+tr("IP End")+'\ - '+vn_info.RANGE.IP_END+'\ + \ + \ '; + } + + // TODO: extra ar config attributes - if (Config.isTabActionEnabled("vnets-tab", "Network.hold_lease")) { - html += - '\ - \ - \ - '; - } - } else { - if (Config.isTabActionEnabled("vnets-tab", "Network.addleases")) { - html += - '\ - \ - \ - '; - } - }; html +='\ \ @@ -805,23 +833,25 @@ function printLeases(vn_info){ \ \ \ - \ '+tr("IP")+'\ '+tr("MAC")+'\ - '+tr("IPv6 Site")+'\ + '+tr("IPv6 Link")+'\ + '+tr("IPv6 ULA")+'\ '+tr("IPv6 Global")+'\ \ \ - '; + '; - var leases = vn_info.LEASES.LEASE; + var leases = ar.LEASES.LEASE; if (!leases) //empty { html+='\ '+tr("No leases to show")+'\ '; - html += ''; + + html += ''; + return html; } else if (leases.constructor != Array) //>1 lease @@ -830,74 +860,41 @@ function printLeases(vn_info){ }; var lease; - var state=null; for (var i=0; i'; html += ''; - switch (state){ - case 0: //free - html += ' ' - break; - case 1: //used - html += ' ' - break; - case 2: //hold - html += ' ' - break; - }; - html += '' - switch (state){ - case 0: - html += ''; - if (Config.isTabActionEnabled("vnets-tab", "Network.hold_lease")) { - html += ''; - } - html += '' - html += '' - if (Config.isTabActionEnabled("vnets-tab", "Network.remove_lease")) { - html += ''; - } - break; - case 1: - html += '' + tr("VM:") + lease.VID+'' - break; - case 2: + if (lease.VM == "-1") { //hold + html += ''; + html += ''; if (Config.isTabActionEnabled("vnets-tab", "Network.release_lease")) { html += ''; } - html += '' - html += '' - if (Config.isTabActionEnabled("vnets-tab", "Network.remove_lease")) { - html += ''; - } - break; - }; - html += '' + html += ''; + } else { //used + html += '\ + ' + tr("VM:") + lease.VM+''; + } - html += ''+ lease.IP + ''; - - html += '\ - '+lease.MAC+''; - - html += ''+ (lease.IP6_SITE ? lease.IP6_SITE : "--") +''; + html += ''+ (lease.IP ? lease.IP : "--") +''; + html += ''+ (lease.MAC ? lease.MAC : "--") +''; + html += ''+ (lease.IP6_LINK ? lease.IP6_LINK : "--") +''; + html += ''+ (lease.IP6_ULA ? lease.IP6_ULA : "--") +''; html += ''+ (lease.IP6_GLOBAL ? lease.IP6_GLOBAL : "--") +''; html += ''; }; - html += ''; + html += ''; return html; } From 3c540456d2347870381777235955706e360bdf0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 May 2014 15:53:58 +0200 Subject: [PATCH 32/84] Feature #2858: Better placement for vnet leases panel --- src/sunstone/public/js/plugins/vnets-tab.js | 271 +++++++++++--------- 1 file changed, 151 insertions(+), 120 deletions(-) diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index b801d5540c..f1bf86ae74 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -634,9 +634,6 @@ function updateVNetworkElement(request, vn_json){ id = vn_json.VNET.ID; element = vNetworkElementArray(vn_json); updateSingleElement(element,dataTable_vNetworks,'#vnetwork_'+id); - - //we update this too, even if it is not shown. - $('#leases_form').replaceWith(printLeases(vn_json.VNET)); } //Callback to delete a vnet element from the table @@ -648,8 +645,6 @@ function deleteVNetworkElement(req){ function addVNetworkElement(request,vn_json){ var element = vNetworkElementArray(vn_json); addElement(element,dataTable_vNetworks); - //we update this too, even if it is not shown. - $('#leases_form').replaceWith(printLeases(vn_json.VNET)); } //updates the list of virtual networks @@ -730,6 +725,15 @@ function updateVNetworkInfo(request,vn){ Sunstone.popUpInfoPanel("vnet_info_panel", "vnets-tab"); + var leases_dataTable = $("#leases_datatable",$("#vnet_info_panel")).dataTable({ + "bSortClasses" : false, + "bDeferRender": true, +// "sScrollX": "100%", + "aoColumnDefs": [ + { "bSortable": false, "aTargets": [0,1] }, + ] + }); + setPermissionsTable(vn_info,''); } @@ -737,60 +741,56 @@ function updateVNetworkInfo(request,vn){ // It adds the "add lease", "hold lease" fields, and each lease comes with // hold, release buttons etc. Listeners in setupLeasesOps() function printLeases(vn_info){ - var html ='
'; - - html += '
\ - \ - \ - \ - \ - \ - \ - \ - \ - \ - \ -
'+tr("Network information")+'
'+tr("Network mask")+''+( vn_info.TEMPLATE.NETWORK_MASK ? vn_info.TEMPLATE.NETWORK_MASK : "--" )+'
'; - - // TODO: Add other well known attributes, like DNS, GATEWAY... - html += '
'; var ar_list = vn_info.AR_POOL.AR; if (!ar_list) //empty { // TODO: message there are no address ranges - html += '
'; - return html; + ar_list = []; } else if (ar_list.constructor != Array) //>1 lease { ar_list = [ar_list]; }; - var ar; + var html = + '
\ +
'; + + html += + '
\ +
'+tr("Global configuration")+'
'; for (var i=0; i'+tr("Adress Range")+' '+id+''; } - html += ''; + html += + '
\ +
\ +
\ +
\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ +
'+tr("Network global configuration")+'
'+tr("Network mask")+''+( vn_info.TEMPLATE.NETWORK_MASK ? vn_info.TEMPLATE.NETWORK_MASK : "--" )+'
\ +
\ +
'; - return html; -} - -function printAR(ar){ - - var html = ''; - html += '
'; - - html += '
\ - \ - \ - \ - '; + // TODO: Add other well known attributes, like DNS, GATEWAY... function ar_attr(val, txt){ if(val){ @@ -803,98 +803,129 @@ function printAR(ar){ return ''; } - html += ar_attr(ar.AR_ID, "ID"); + for (var i=0; i\ +
\ +
'+tr("Address Range information")+'
\ + \ + \ + \ + '; + + // TODO: translate ar.TYPE values? + html += ar_attr(ar.TYPE, tr("Type")); + html += ar_attr(ar.MAC, tr("MAC")); + html += ar_attr(ar.IP, tr("IP")); + html += ar_attr(ar.GLOBAL_PREFIX, tr("Global prefix")); + html += ar_attr(ar.ULA_PREFIX, tr("ULA prefix")); + html += ar_attr(ar.SIZE, tr("Size")); + html += ar_attr(ar.USED_LEASES, tr("Used leases")); + + html += + '\ +
'+tr("Address Range")+' '+id+'
\ +
\ +
'; + + // TODO: extra ar config attributes + + // TODO: add, remove AR + } + + html += '
\ +
'; - // TODO: add, remove AR if (Config.isTabActionEnabled("vnets-tab", "Network.hold_lease")) { - html += '\ - \ - \ - '; + html += + '
\ +
\ +
\ + \ +
\ +
\ + \ +
\ +
'; + } - // TODO: extra ar config attributes + html += + '
\ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + \ + '; + for (var i=0; i\ - \ - \ - \ - \ - \ - \ - '; + var leases = ar.LEASES.LEASE; - var leases = ar.LEASES.LEASE; - - if (!leases) //empty - { - html+=''; - - html += '
'+tr("IP")+''+tr("MAC")+''+tr("IPv6 Link")+''+tr("IPv6 ULA")+''+tr("IPv6 Global")+''+tr("Address Range")+'
'+tr("MAC")+''+tr("IPv6 Link")+''+tr("IPv6 ULA")+''+tr("IPv6 Global")+'
\ - '+tr("No leases to show")+'\ -
'; - - return html; - } - else if (leases.constructor != Array) //>1 lease - { - leases = [leases]; - }; - - var lease; - - for (var i=0; i'; - - html += ''; - - if (lease.VM == "-1") { //hold - html += ''; - - html += ''; - if (Config.isTabActionEnabled("vnets-tab", "Network.release_lease")) { - html += ''; - } - html += ''; - } else { //used - html += '\ - ' + tr("VM:") + lease.VM+''; + if (!leases) //empty + { + continue; + } + else if (leases.constructor != Array) //>1 lease + { + leases = [leases]; } - html += ''+ (lease.IP ? lease.IP : "--") +''; - html += ''+ (lease.MAC ? lease.MAC : "--") +''; - html += ''+ (lease.IP6_LINK ? lease.IP6_LINK : "--") +''; - html += ''+ (lease.IP6_ULA ? lease.IP6_ULA : "--") +''; - html += ''+ (lease.IP6_GLOBAL ? lease.IP6_GLOBAL : "--") +''; + var lease; - html += ''; - }; + for (var j=0; j'; + + html += ''; + + if (lease.VM == "-1") { //hold + html += ''; + + html += ''; + if (Config.isTabActionEnabled("vnets-tab", "Network.release_lease")) { + html += ''; + } + html += ''; + } else { //used + html += '\ + ' + tr("VM:") + lease.VM+''; + } + + html += ''+ (lease.IP ? lease.IP : "--") +''; + html += ''+ (lease.MAC ? lease.MAC : "--") +''; + html += ''+ (lease.IP6_LINK ? lease.IP6_LINK : "--") +''; + html += ''+ (lease.IP6_ULA ? lease.IP6_ULA : "--") +''; + html += ''+ (lease.IP6_GLOBAL ? lease.IP6_GLOBAL : "--") +''; + html += ''+ id +''; + + html += ''; + } + } + + html += ''; + + html += ''; return html; } From 1f09233b96b9220ac64376a209b99ccb7d6753a7 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 28 May 2014 18:39:57 +0200 Subject: [PATCH 33/84] feature #2858: Update parent vnet on removal --- src/rm/RequestManagerDelete.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rm/RequestManagerDelete.cc b/src/rm/RequestManagerDelete.cc index b4589893cf..1ca7fd03d4 100644 --- a/src/rm/RequestManagerDelete.cc +++ b/src/rm/RequestManagerDelete.cc @@ -351,6 +351,8 @@ int VirtualNetworkDelete::drop(int oid, PoolObjectSQL * object, string& error_ms vnet->free_addr_by_owner(PoolObjectSQL::NET, oid); + pool->update(vnet); + vnet->unlock(); } From 977fc0d9e3c6adb3dda35b94220fe493385fc956 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 May 2014 12:44:41 +0200 Subject: [PATCH 34/84] feature #2858: Reserve by AR_ID, fix DB storage of reservations --- include/AddressRange.h | 7 ++++ include/AddressRangePool.h | 28 +++++++++++-- include/VirtualNetwork.h | 17 +++++++- src/cli/onevnet | 3 +- src/oca/ruby/opennebula/virtual_network.rb | 4 +- src/rm/RequestManagerDelete.cc | 4 +- src/rm/RequestManagerVirtualNetwork.cc | 22 +++++++++- src/vnm/AddressRangePool.cc | 49 ++++++++++++++++++++-- src/vnm/VirtualNetwork.cc | 46 ++++++++++++++++++++ 9 files changed, 167 insertions(+), 13 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index 663d88ed07..bfb6c5a67b 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -22,6 +22,7 @@ #include #include "PoolObjectSQL.h" +#include "AddressRangePool.h" using namespace std; @@ -273,6 +274,12 @@ public: */ void update_attributes(VectorAttribute *vup); + /* + * add_ar from AddressRangePool needs to access the internal representation + * of the AR to include it in the ARPool template. + */ + friend int AddressRangePool::add_ar(AddressRange * ar); + private: /* ---------------------------------------------------------------------- */ /* String to binary conversion functions for different address types */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 4dd2f764b4..eb64b5002f 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -24,9 +24,10 @@ #include #include "Template.h" -#include "AddressRange.h" +#include "PoolObjectSQL.h" class VectorAttribute; +class AddressRange; using namespace std; @@ -78,11 +79,20 @@ public: void update_ar(vector ars); /** - * Allocates a new address range in the pool. - * @return the new address range added to the pool + * Allocates a new *empty* address range. It is not added to the pool as it + * needs to be initialized. Only the AR_ID is set. + * @return the new address range. */ AddressRange * allocate_ar(); + /** + * Adds a new address range to the pool. It should be allocated by the + * allocate_ar() function. + * @param ar the new address range; + * @return 0 on success + */ + int add_ar(AddressRange * ar); + // ************************************************************************* // Address allocation interface // ************************************************************************* @@ -214,6 +224,18 @@ public: */ int reserve_addr(int pvid, int vid, unsigned int rsize, AddressRange *rar); + /** + * Reserve a given number of addresses from the given address range + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param rsize number of addresses to reserve + * @param ar_id the address range to reserve the addresses from + * @param rar a new address range to place the reservation + * @return 0 on success + */ + int reserve_addr(int pvid, int vid, unsigned int rsize, unsigned int ar_id, + AddressRange *rar); + /** * Get the parent vnets of the Address Ranges in this AR POOL * @param parent_vnets vector with the vnet ids of the parent vnets diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index f82c8ddc6f..24baa6fcdd 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -77,14 +77,25 @@ public: int rm_ar(unsigned int ar_id, string& error_msg); /** - * Allocates a new (and empty) address range to the AR pool - * @return pointer to the ar added to the AR pool + * Allocates a new (and empty) address range. It is not added to the + * ar_pool + * @return pointer to the new address range */ AddressRange * allocate_ar() { return ar_pool.allocate_ar(); } + /** + * Adds a previously allocated address range to the AR pool + * @param rar pointer to the address range + * @return 0 on success + */ + int add_ar(AddressRange * rar) + { + return ar_pool.add_ar(rar); + } + /** * Update an address range to the virtual network * @param ars_tmpl template in the form AR = [AR_ID=...]. The address range @@ -203,6 +214,8 @@ public: int reserve_addr(VirtualNetwork *rvnet, unsigned int rsize, string& error_str); + int reserve_addr(VirtualNetwork *rvnet, unsigned int rsize, + unsigned int ar_id, string& error_str); // ************************************************************************* // Formatting & Helper functions diff --git a/src/cli/onevnet b/src/cli/onevnet index 87f831b9e6..da67514572 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -190,7 +190,6 @@ cmd=CommandParser::CmdParser.new(ARGV) do :options=>STD_OPTIONS + [OneVNetHelper::AR, OneVNetHelper::R_NAME, OneVNetHelper::R_SIZE] do helper.perform_action(args[0],options,"reservation made") do |vn| - ar = options[:ar_id] || -1 rsize = options[:rsize] || -1 rname = options[:rname] || -1 @@ -204,7 +203,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do exit -1 end - vn.reserve(rname, rsize) + vn.reserve(rname, rsize, options[:ar_id]) end end diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index aeb52fc201..8aef58a4a0 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -189,11 +189,13 @@ module OpenNebula # Reserve a set of addresses from this virtual network # @param name [String] of the reservation # @param rsize[String] number of addresses to reserve - def reserve(rname, rsize) + # @param ar_id[String] the ar_id to make the reservation + def reserve(rname, rsize, ar_id) return Error.new('ID not defined') if !@pe_id rtmpl = "NAME = #{rname}\n" rtmpl << "SIZE = #{rsize}\n" + rtmpl << "AR_ID= #{ar_id}\n" if !ar_id.nil? rc = @client.call(VN_METHODS[:reserve], @pe_id, rtmpl) rc = nil if !OpenNebula.is_error?(rc) diff --git a/src/rm/RequestManagerDelete.cc b/src/rm/RequestManagerDelete.cc index 1ca7fd03d4..b1b81585b7 100644 --- a/src/rm/RequestManagerDelete.cc +++ b/src/rm/RequestManagerDelete.cc @@ -340,9 +340,11 @@ int VirtualNetworkDelete::drop(int oid, PoolObjectSQL * object, string& error_ms int rc = RequestManagerDelete::drop(oid, object, error_msg); + VirtualNetworkPool *vnpool = static_cast(pool); + for (vector::iterator it = parents.begin(); it < parents.end(); it++) { - vnet = static_cast(pool->get(*it, true)); + vnet = vnpool->get(*it, true); if (vnet == 0) { diff --git a/src/rm/RequestManagerVirtualNetwork.cc b/src/rm/RequestManagerVirtualNetwork.cc index 888ada2b57..0d19fd2198 100644 --- a/src/rm/RequestManagerVirtualNetwork.cc +++ b/src/rm/RequestManagerVirtualNetwork.cc @@ -182,6 +182,17 @@ void VirtualNetworkReserve::request_execute( return; } + int ar_id; + bool with_ar_id = tmpl.get("AR_ID", ar_id); + + if ( with_ar_id && (ar_id < 0)) + { + failure_response(ACTION, request_error("Error in reservation request", + "AR_ID must be equal or greater than 0"), att); + + return; + } + // --------------- Check/update quotas on the target VNET ----------------- ostringstream qtmpl_s; @@ -250,7 +261,16 @@ void VirtualNetworkReserve::request_execute( // -------------- Make address reservation and set it ---------------------- - if (vn->reserve_addr(rvn, size, error_str) != 0 ) + if (with_ar_id) + { + rc = vn->reserve_addr(rvn, size, ar_id, error_str); + } + else + { + rc = vn->reserve_addr(rvn, size, error_str); + } + + if (rc != 0 ) { quota_rollback(&qtmpl, Quotas::NETWORK, att); diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index c4a1eed80f..dc33a56db2 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -15,6 +15,7 @@ /* -------------------------------------------------------------------------- */ #include "AddressRangePool.h" +#include "AddressRange.h" using namespace std; @@ -76,11 +77,25 @@ int AddressRangePool::from_vattr(vector ars, string& error_msg) AddressRange * AddressRangePool::allocate_ar() { - AddressRange * ar = new AddressRange(next_ar); + return new AddressRange(next_ar++); +} - ar_pool.insert(make_pair(next_ar++, ar)); +/* -------------------------------------------------------------------------- */ - return ar; +int AddressRangePool::add_ar(AddressRange * ar) +{ + pair::iterator, bool> rc; + + rc = ar_pool.insert(make_pair(ar->ar_id(), ar)); + + if (rc.second == false) + { + return -1; + } + + ar_template.set(ar->attr); + + return 0; } /* -------------------------------------------------------------------------- */ @@ -497,6 +512,7 @@ int AddressRangePool::hold_by_mac(const string& mac_s) return rc; } + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -521,6 +537,31 @@ int AddressRangePool::reserve_addr(int pvid, int vid, unsigned int rsize, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int AddressRangePool::reserve_addr(int pvid, int vid, unsigned int rsize, + unsigned int ar_id, AddressRange *rar) +{ + map::iterator it; + + it = ar_pool.find(ar_id); + + if (it == ar_pool.end()) + { + return -1; + } + + if ((it->second->get_free_addr() >= rsize) && + (it->second->reserve_addr(pvid, vid, rsize, rar) == 0)) + { + used_addr += rsize; + return 0; + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + unsigned int AddressRangePool::get_parents(vector& parent_nets) { int vid; @@ -537,4 +578,6 @@ unsigned int AddressRangePool::get_parents(vector& parent_nets) num_parents++; } } + + return num_parents; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index b1b3696e59..abd67031e5 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -18,6 +18,7 @@ #include "VirtualNetwork.h" #include "VirtualNetworkPool.h" #include "VirtualNetworkTemplate.h" +#include "AddressRange.h" #include "NebulaLog.h" @@ -779,6 +780,51 @@ int VirtualNetwork::reserve_addr(VirtualNetwork *rvnet, { error_str = "Not enough free addresses in an address range"; + delete rar; + + return -1; + } + + if (rvnet->add_ar(rar) != 0) + { + error_str = "Could not add the address range to the netwok"; + + delete rar; + + return -1; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualNetwork::reserve_addr(VirtualNetwork *rvnet, + unsigned int rsize, unsigned int ar_id, string& error_str) +{ + AddressRange *rar = rvnet->allocate_ar(); + + if (ar_pool.reserve_addr(oid, rvnet->get_oid(), rsize, ar_id, rar) != 0) + { + ostringstream oss; + + oss << "Not enough free addresses in address range " << ar_id + << ", or it does not exist"; + + error_str = oss.str(); + + delete rar; + + return -1; + } + + if (rvnet->add_ar(rar) != 0) + { + error_str = "Could not add the address range to the netwok"; + + delete rar; + return -1; } From ab6de3ae02b34ffd05ce6d6862856df5c47cf48d Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 May 2014 15:36:20 +0200 Subject: [PATCH 35/84] feature #2858: Fix minor memory leaks --- include/AddressRangePool.h | 6 ++--- include/Datastore.h | 2 +- include/VirtualNetwork.h | 10 +++++++- src/datastore/Datastore.cc | 5 ++++ src/nebula/NebulaTemplate.cc | 7 ++++-- src/vnm/AddressRangePool.cc | 34 ++++++--------------------- src/vnm/VirtualNetwork.cc | 45 +++++++++++++++++++++++++----------- 7 files changed, 62 insertions(+), 47 deletions(-) diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index eb64b5002f..6ad8265b83 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -43,13 +43,13 @@ public: // ************************************************************************* /** - * Builds the address range set from an array of VectorAttributes. This - * function is used to create address ranges. + * Builds the address range from a VectorAttribute. This function is used + * to create address ranges. * @param ars the vector of address ranges * @param error_msg describing the error * @return 0 on success */ - int from_vattr(vector ars, string& error_msg); + int from_vattr(VectorAttribute * ar, string& error_msg); /** * Builds the address range set from its XML representation. This function diff --git a/include/Datastore.h b/include/Datastore.h index d18b404dd3..fe2cc717f7 100644 --- a/include/Datastore.h +++ b/include/Datastore.h @@ -265,7 +265,7 @@ private: int cluster_id, const string& cluster_name); - virtual ~Datastore(){}; + virtual ~Datastore(); // ************************************************************************* // DataBase implementation (Private) diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 24baa6fcdd..c5355ec3aa 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -61,13 +61,21 @@ public: // ************************************************************************* /** - * Add an address range to the virtual network + * Add a set of address ranges to the virtual network * @param ars_tmpl template in the form AR = [TYPE=...,IP=...,SIZE=...]. * @param error_msg If the action fails, this message contains the reason. * @return 0 on success */ int add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg); + /** + * Adds a set of address ranges + * @param var a vector of address ranges + * @param error_msg If the action fails, this message contains the reason. + * @return 0 on success + */ + int add_var(vector &var, string& error_msg); + /** * Removes an address range from the VNET * @param ar_id of the address range diff --git a/src/datastore/Datastore.cc b/src/datastore/Datastore.cc index ebcb869414..58018ef2eb 100644 --- a/src/datastore/Datastore.cc +++ b/src/datastore/Datastore.cc @@ -68,6 +68,11 @@ Datastore::Datastore( group_u = 1; } +Datastore::~Datastore() +{ + delete obj_template; +}; + /* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */ diff --git a/src/nebula/NebulaTemplate.cc b/src/nebula/NebulaTemplate.cc index a3a9617730..7d966dd98c 100644 --- a/src/nebula/NebulaTemplate.cc +++ b/src/nebula/NebulaTemplate.cc @@ -29,7 +29,7 @@ int NebulaTemplate::load_configuration() string aname; Attribute * attr; - map::iterator iter, j; + map::iterator iter, j, prev; set_conf_default(); @@ -59,7 +59,10 @@ int NebulaTemplate::load_configuration() else { delete iter->second; - conf_default.erase(iter++); + + prev = iter++; + + conf_default.erase(prev); } } diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index dc33a56db2..0aad359b0f 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -35,39 +35,19 @@ AddressRangePool::~AddressRangePool() /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int AddressRangePool::from_vattr(vector ars, string& error_msg) +int AddressRangePool::from_vattr(VectorAttribute* va, string& error_msg) { - vector::iterator it; + AddressRange * ar = new AddressRange(next_ar); - /* -------------------- Init the AR pool ---------------------------- */ - - for (it = ars.begin(); it != ars.end(); it++) + if (ar->from_vattr(va, error_msg) != 0) { - VectorAttribute * va = static_cast(*it); - - if (va == 0) - { - error_msg = "Wrong AR format"; - return -1; - } - - AddressRange * ar = new AddressRange(next_ar); - - if (ar->from_vattr(va, error_msg) != 0) - { - delete ar; - return -1; - } - - ar_pool.insert(make_pair(next_ar++, ar)); + delete ar; + return -1; } - /* ---------------------- Template ---------------------------------- */ + ar_pool.insert(make_pair(next_ar++, ar)); - for (it = ars.begin(); it != ars.end(); it++) - { - ar_template.set(*it); - } + ar_template.set(va); return 0; } diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index abd67031e5..2b591d42cb 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -167,7 +167,7 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str) remove_template_attribute("AR", ars); - if (ar_pool.from_vattr(ars, error_str) != 0) + if (add_var(ars, error_str) != 0) { goto error_ar; } @@ -587,23 +587,42 @@ int VirtualNetwork::nic_attribute( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -int VirtualNetwork::add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg) +int VirtualNetwork::add_var(vector &var, string& error_msg) { - vector tmp_ars; - vector ars; - - vector::iterator it; - - ars_tmpl->get("AR", tmp_ars); - - for (it=tmp_ars.begin(); it!=tmp_ars.end(); it++) + for (vector::iterator it=var.begin(); it!=var.end(); it++) { - VectorAttribute * var = static_cast(*it); + VectorAttribute * oar = static_cast(*it); - ars.push_back(var->clone()); + if (oar == 0) + { + error_msg = "Invalid format for address range"; + return -1; + } + + VectorAttribute * ar = oar->clone(); + + if (ar_pool.from_vattr(ar, error_msg) != 0) + { + delete ar; + return -1; + } } - return ar_pool.from_vattr(ars, error_msg); + return 0; +} + +/* -------------------------------------------------------------------------- */ + +int VirtualNetwork::add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg) +{ + vector var; + + if (ars_tmpl->get("AR", var) > 0) + { + return add_var(var, error_msg); + } + + return 0; } /* -------------------------------------------------------------------------- */ From 8f752d3dd7177fb4552e2ca8da8c279fddf352a0 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 May 2014 15:57:56 +0200 Subject: [PATCH 36/84] feature #2858: Better check for class types. Single AR addtion to prevent out-of-sync NETs --- src/vnm/AddressRangePool.cc | 4 ++-- src/vnm/VirtualNetwork.cc | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 0aad359b0f..74f3eb82f8 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -90,7 +90,7 @@ void AddressRangePool::update_ar(vector ars) for (it = ars.begin(); it != ars.end(); it++) { - VectorAttribute * va = static_cast(*it); + VectorAttribute * va = dynamic_cast(*it); if (va == 0) { @@ -191,7 +191,7 @@ int AddressRangePool::rm_ar(unsigned int ar_id, string& error_msg) for (it_ar=ars.begin(); it_ar!=ars.end(); it_ar++) { - VectorAttribute *ar = static_cast(*it_ar); + VectorAttribute *ar = dynamic_cast(*it_ar); if (ar == 0) { diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 2b591d42cb..5212c8b08a 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -591,7 +591,7 @@ int VirtualNetwork::add_var(vector &var, string& error_msg) { for (vector::iterator it=var.begin(); it!=var.end(); it++) { - VectorAttribute * oar = static_cast(*it); + VectorAttribute * oar = dynamic_cast(*it); if (oar == 0) { @@ -617,9 +617,25 @@ int VirtualNetwork::add_ar(VirtualNetworkTemplate * ars_tmpl, string& error_msg) { vector var; - if (ars_tmpl->get("AR", var) > 0) + if (ars_tmpl->get("AR", var) <= 0) { - return add_var(var, error_msg); + return 0; + } + + const VectorAttribute * ar = dynamic_cast(var[0]); + + if (ar == 0) + { + error_msg = "Wrong AR definition"; + return -1; + } + + VectorAttribute * nar = ar->clone(); + + if (ar_pool.from_vattr(nar, error_msg) != 0) + { + delete nar; + return -1; } return 0; From 15d096885dc24849517b5f1ebb650b0e55c76ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 May 2014 16:29:10 +0200 Subject: [PATCH 37/84] Feature #2858: rm_ar button in sunstone --- .../OpenNebulaJSON/VirtualNetworkJSON.rb | 5 ++++ src/sunstone/public/js/opennebula.js | 7 +++++ src/sunstone/public/js/plugins/vnets-tab.js | 27 ++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb index bba1722c73..5495f3f001 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb @@ -52,6 +52,7 @@ module OpenNebulaJSON when "hold" then self.hold(action_hash['params']) when "release" then self.release(action_hash['params']) when "rename" then self.rename(action_hash['params']) + when "rm_ar" then self.rm_ar(action_hash['params']) else error_msg = "#{action_hash['perform']} action not " << " available for this resource" @@ -90,5 +91,9 @@ module OpenNebulaJSON def rename(params=Hash.new) super(params['name']) end + + def rm_ar(params=Hash.new) + super(params['ar_id']) + end end end diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index b23ea72645..f2f779284a 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -795,6 +795,13 @@ var OpenNebula = { "release", action_obj); }, + "rm_ar" : function(params){ + var action_obj = params.data.extra_param; + OpenNebula.Action.simple_action(params, + OpenNebula.Network.resource, + "rm_ar", + action_obj); + }, "update": function(params){ var action_obj = {"template_raw" : params.data.extra_param }; OpenNebula.Action.simple_action(params, diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index f1bf86ae74..b8e7f77da4 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -437,6 +437,15 @@ var vnet_actions = { error: onError }, + "Network.rm_ar" : { + type: "single", + call: OpenNebula.Network.rm_ar, + callback: function(req) { + Sunstone.runAction("Network.show",req.request.data[0][0]); + }, + error: onError + }, + "Network.chown" : { type: "multiple", call: OpenNebula.Network.chown, @@ -808,7 +817,7 @@ function printLeases(vn_info){ var id = ar.AR_ID; html += - '
\ + '
\
\ \ \ @@ -828,6 +837,9 @@ function printLeases(vn_info){ html += '\
\ +
\ + \ +
\
\
'; @@ -1378,6 +1390,19 @@ function setupLeasesOps(){ return false; }); } + + if (Config.isTabActionEnabled("vnets-tab", "Network.remove_lease")) { + $('button#rm_ar_button').live("click",function(){ + // TODO: confirm? + var id = $(this).parents('form').attr('vnid'); + var ar_id = $(this).parents('.content').attr('ar_id'); + + var obj = {ar_id: ar_id}; + Sunstone.runAction('Network.rm_ar',id,obj); + + return false; + }); + } } //The DOM is ready and the ready() from sunstone.js From 822ef4d1131ab9c39dc0df8e741750c7b4f86f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 May 2014 18:29:57 +0200 Subject: [PATCH 38/84] Feature #2858: New vnet methods in sunstone api --- src/sunstone/etc/sunstone-views/admin.yaml | 5 +- src/sunstone/etc/sunstone-views/cloud.yaml | 5 +- src/sunstone/etc/sunstone-views/user.yaml | 5 +- src/sunstone/etc/sunstone-views/vdcadmin.yaml | 5 +- .../etc/sunstone-views/vdcadmin46.yaml | 5 +- .../OpenNebulaJSON/VirtualNetworkJSON.rb | 20 ++--- src/sunstone/public/js/opennebula.js | 28 +++---- src/sunstone/public/js/plugins/vnets-tab.js | 79 +++++++------------ 8 files changed, 68 insertions(+), 84 deletions(-) diff --git a/src/sunstone/etc/sunstone-views/admin.yaml b/src/sunstone/etc/sunstone-views/admin.yaml index c4bd59e27d..47cc3fc677 100644 --- a/src/sunstone/etc/sunstone-views/admin.yaml +++ b/src/sunstone/etc/sunstone-views/admin.yaml @@ -341,10 +341,11 @@ tabs: Network.chgrp: true Network.chmod: true Network.delete: true - Network.addleases: true Network.hold_lease: true - Network.remove_lease: true Network.release_lease: true + Network.add_ar: true + Network.remove_ar: true + Network.update_ar: true marketplace-tab: panel_tabs: marketplace_info_tab: true diff --git a/src/sunstone/etc/sunstone-views/cloud.yaml b/src/sunstone/etc/sunstone-views/cloud.yaml index 21a43ba6a4..198c9ff0d1 100644 --- a/src/sunstone/etc/sunstone-views/cloud.yaml +++ b/src/sunstone/etc/sunstone-views/cloud.yaml @@ -338,10 +338,11 @@ tabs: Network.chgrp: false Network.chmod: true Network.delete: false - Network.addleases: false Network.hold_lease: false - Network.remove_lease: false Network.release_lease: false + Network.add_ar: false + Network.remove_ar: false + Network.update_ar: false marketplace-tab: panel_tabs: marketplace_info_tab: true diff --git a/src/sunstone/etc/sunstone-views/user.yaml b/src/sunstone/etc/sunstone-views/user.yaml index 727b203022..9ac1e8292e 100644 --- a/src/sunstone/etc/sunstone-views/user.yaml +++ b/src/sunstone/etc/sunstone-views/user.yaml @@ -341,10 +341,11 @@ tabs: Network.chgrp: false Network.chmod: true Network.delete: false - Network.addleases: false Network.hold_lease: false - Network.remove_lease: false Network.release_lease: false + Network.add_ar: false + Network.remove_ar: false + Network.update_ar: false marketplace-tab: panel_tabs: marketplace_info_tab: true diff --git a/src/sunstone/etc/sunstone-views/vdcadmin.yaml b/src/sunstone/etc/sunstone-views/vdcadmin.yaml index 85aeaa6114..447d7a6cf2 100644 --- a/src/sunstone/etc/sunstone-views/vdcadmin.yaml +++ b/src/sunstone/etc/sunstone-views/vdcadmin.yaml @@ -338,10 +338,11 @@ tabs: Network.chgrp: false Network.chmod: true Network.delete: false - Network.addleases: false Network.hold_lease: false - Network.remove_lease: false Network.release_lease: false + Network.add_ar: false + Network.remove_ar: false + Network.update_ar: false marketplace-tab: panel_tabs: marketplace_info_tab: true diff --git a/src/sunstone/etc/sunstone-views/vdcadmin46.yaml b/src/sunstone/etc/sunstone-views/vdcadmin46.yaml index c095c08ca3..5b8fe086f6 100644 --- a/src/sunstone/etc/sunstone-views/vdcadmin46.yaml +++ b/src/sunstone/etc/sunstone-views/vdcadmin46.yaml @@ -341,10 +341,11 @@ tabs: Network.chgrp: false Network.chmod: false Network.delete: false - Network.addleases: false Network.hold_lease: false - Network.remove_lease: false Network.release_lease: false + Network.add_ar: false + Network.remove_ar: false + Network.update_ar: false marketplace-tab: panel_tabs: marketplace_info_tab: true diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb index 5495f3f001..73d321dd04 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb @@ -42,8 +42,6 @@ module OpenNebulaJSON end rc = case action_hash['perform'] - when "addleases" then self.addleases(action_hash['params']) - when "rmleases" then self.rmleases(action_hash['params']) when "publish" then self.publish when "unpublish" then self.unpublish when "update" then self.update(action_hash['params']) @@ -53,6 +51,8 @@ module OpenNebulaJSON when "release" then self.release(action_hash['params']) when "rename" then self.rename(action_hash['params']) when "rm_ar" then self.rm_ar(action_hash['params']) + when "add_ar" then self.add_ar(action_hash['params']) + when "update_ar" then self.update_ar(action_hash['params']) else error_msg = "#{action_hash['perform']} action not " << " available for this resource" @@ -60,14 +60,6 @@ module OpenNebulaJSON end end - def addleases(params=Hash.new) - super(params['ip'],params['mac']) - end - - def rmleases(params=Hash.new) - super(params['ip']) - end - def update(params=Hash.new) super(params['template_raw']) end @@ -95,5 +87,13 @@ module OpenNebulaJSON def rm_ar(params=Hash.new) super(params['ar_id']) end + + def add_ar(params=Hash.new) + super(params['ar_template_raw']) + end + + def update_ar(params=Hash.new) + super(params['ar_template_raw']) + end end end diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index f2f779284a..82dfdc0e51 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -767,20 +767,6 @@ var OpenNebula = { "unpublish": function(params){ OpenNebula.Action.simple_action(params,OpenNebula.Network.resource,"unpublish"); }, - "addleases" : function(params){ - var action_obj = params.data.extra_param; - OpenNebula.Action.simple_action(params, - OpenNebula.Network.resource, - "addleases", - action_obj); - }, - "rmleases" : function(params){ - var action_obj = params.data.extra_param; - OpenNebula.Action.simple_action(params, - OpenNebula.Network.resource, - "rmleases", - action_obj); - }, "hold" : function(params){ var action_obj = params.data.extra_param; OpenNebula.Action.simple_action(params, @@ -795,6 +781,13 @@ var OpenNebula = { "release", action_obj); }, + "add_ar" : function(params){ + var action_obj = {"ar_template_raw" : params.data.extra_param }; + OpenNebula.Action.simple_action(params, + OpenNebula.Network.resource, + "add_ar", + action_obj); + }, "rm_ar" : function(params){ var action_obj = params.data.extra_param; OpenNebula.Action.simple_action(params, @@ -802,6 +795,13 @@ var OpenNebula = { "rm_ar", action_obj); }, + "update_ar": function(params){ + var action_obj = {"ar_template_raw" : params.data.extra_param }; + OpenNebula.Action.simple_action(params, + OpenNebula.Network.resource, + "update_ar", + action_obj); + }, "update": function(params){ var action_obj = {"template_raw" : params.data.extra_param }; OpenNebula.Action.simple_action(params, diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index b8e7f77da4..d90197e9f9 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -401,24 +401,6 @@ var vnet_actions = { error: onError }, - "Network.addleases" : { - type: "single", - call: OpenNebula.Network.addleases, - callback: function(req) { - Sunstone.runAction("Network.show",req.request.data[0][0]); - }, - error: onError - }, - - "Network.rmleases" : { - type: "single", - call: OpenNebula.Network.rmleases, - callback: function(req) { - Sunstone.runAction("Network.show",req.request.data[0][0]); - }, - error: onError - }, - "Network.hold" : { type: "single", call: OpenNebula.Network.hold, @@ -437,6 +419,15 @@ var vnet_actions = { error: onError }, + "Network.add_ar" : { + type: "single", + call: OpenNebula.Network.add_ar, + callback: function(req) { + Sunstone.runAction("Network.show",req.request.data[0][0]); + }, + error: onError + }, + "Network.rm_ar" : { type: "single", call: OpenNebula.Network.rm_ar, @@ -446,6 +437,15 @@ var vnet_actions = { error: onError }, + "Network.update_ar" : { + type: "single", + call: OpenNebula.Network.update_ar, + callback: function(req) { + Sunstone.runAction("Network.show",req.request.data[0][0]); + }, + error: onError + }, + "Network.chown" : { type: "multiple", call: OpenNebula.Network.chown, @@ -836,11 +836,17 @@ function printLeases(vn_info){ html += '\ - \ -
\ + '; + + if (Config.isTabActionEnabled("vnets-tab", "Network.remove_ar")) { + html += + '
\ \ -
\ -
\ +
'; + } + + html += + '\ '; // TODO: extra ar config attributes @@ -1327,19 +1333,6 @@ function popUpCreateVnetDialog() { // Listeners to the add, hold, release, delete leases operations in the // extended information panel. function setupLeasesOps(){ - if (Config.isTabActionEnabled("vnets-tab", "Network.addleases")) { - $('button#panel_add_lease_button').live("click",function(){ - var lease = $('input#panel_add_lease').val(); - //var mac = $(this).previous().val(); - var id = $(this).parents('form').attr('vnid'); - if (lease.length){ - var obj = {ip: lease}; - Sunstone.runAction('Network.addleases',id,obj); - } - return false; - }); - } - if (Config.isTabActionEnabled("vnets-tab", "Network.hold_lease")) { //ranged networks hold lease $('button#panel_hold_lease_button').live("click",function(){ @@ -1365,20 +1358,6 @@ function setupLeasesOps(){ }); } - if (Config.isTabActionEnabled("vnets-tab", "Network.remove_lease")) { - $('form#leases_form a.delete_lease').live("click",function(){ - var lease = $(this).parents('tr').attr('ip'); - var id = $(this).parents('form').attr('vnid'); - var obj = { ip: lease}; - Sunstone.runAction('Network.rmleases',id,obj); - //Set spinner - $(this).parents('tr').html(''+spinner+''); - return false; - }); - } - - - if (Config.isTabActionEnabled("vnets-tab", "Network.release_lease")) { $('a.release_lease').live("click",function(){ var lease = $(this).parents('tr').attr('ip'); @@ -1391,7 +1370,7 @@ function setupLeasesOps(){ }); } - if (Config.isTabActionEnabled("vnets-tab", "Network.remove_lease")) { + if (Config.isTabActionEnabled("vnets-tab", "Network.remove_ar")) { $('button#rm_ar_button').live("click",function(){ // TODO: confirm? var id = $(this).parents('form').attr('vnid'); From 3dd9f292bb342194e89659e90d06aa5d56dbd443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 29 May 2014 16:18:34 +0200 Subject: [PATCH 39/84] Feature #2858: Add AR creation to vnet wizard --- src/sunstone/public/js/plugins/vnets-tab.js | 507 ++++++++------------ 1 file changed, 213 insertions(+), 294 deletions(-) diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index d90197e9f9..6cac3d453b 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -44,118 +44,6 @@ var create_vn_tmpl = \ \
\ -
\ -
\ -
\ - ' + tr("Type") + '\ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ -
\ -
\ - \ -
\ -
\ -
\ -
\ -
\ -
\ -
\ -
\ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ -
\ -
\
\
\ \ @@ -175,16 +63,14 @@ var create_vn_tmpl =
\
\
\ -
\ +
\ \ \
\ -
\ -
\ -
\ +
\ \ \
\ @@ -200,6 +86,46 @@ var create_vn_tmpl =
\
\
\ +
\ +
\ + \ + \ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ +
\ +
\ + ' + tr("Address Ranges") + '\ +
\ +
\ +
\ + \ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\
\
\
\ @@ -851,7 +777,7 @@ function printLeases(vn_info){ // TODO: extra ar config attributes - // TODO: add, remove AR + // TODO: add AR } html += '
\ @@ -869,7 +795,6 @@ function printLeases(vn_info){ \
\
'; - } html += @@ -957,61 +882,28 @@ function setupCreateVNetDialog() { dialog.addClass("reveal-modal medium").attr("data-reveal", ""); - //Make the tabs look nice for the creation mode - //$('#vn_tabs',dialog).tabs(); - $('div#ranged',dialog).hide(); - $('div#ranged_ipv6',dialog).hide(); + var number_of_ar = 0; - $('input#site_prefix,label[for="site_prefix"]',$create_vn_dialog).hide(); - $('input#global_prefix,label[for="global_prefix"]',$create_vn_dialog).hide(); + // close icon: removing the tab on click + $(dialog).on("click", "i.remove-tab", function() { + var target = $(this).parent().attr("href"); + var dd = $(this).closest('dd'); + var dl = $(this).closest('dl'); + var content = $(target); - $('input[name="ip_version"]',dialog).change(function(){ - var fixed_ranged = $('input[name="fixed_ranged"]:checked',dialog).val(); + dd.remove(); + content.remove(); - if (this.id == 'ipv4_check') { - $('input#net_mask,label[for="net_mask"]',$create_vn_dialog).show(); - $('input#net_address,label[for="net_address"]',$create_vn_dialog).show(); - - $('input#site_prefix,label[for="site_prefix"]',$create_vn_dialog).hide(); - $('input#global_prefix,label[for="global_prefix"]',$create_vn_dialog).hide(); - - if ( fixed_ranged == "fixed" ){ - $('input#leaseip,label[for="leaseip"]',$create_vn_dialog).show(); - } else { - $('div#ranged',$create_vn_dialog).show(); - $('div#ranged_ipv6',$create_vn_dialog).hide(); + if (dd.attr("class") == 'active') { + $('a', dl.children('dd').last()).click(); } - } else { - $('input#net_mask,label[for="net_mask"]',$create_vn_dialog).hide(); - $('input#net_address,label[for="net_address"]',$create_vn_dialog).hide(); - $('input#site_prefix,label[for="site_prefix"]',$create_vn_dialog).show(); - $('input#global_prefix,label[for="global_prefix"]',$create_vn_dialog).show(); - - if ( fixed_ranged == "fixed" ){ - $('input#leaseip,label[for="leaseip"]',$create_vn_dialog).hide(); - } else { - $('div#ranged',$create_vn_dialog).hide(); - $('div#ranged_ipv6',$create_vn_dialog).show(); - } - } + return false; }); - $('input[name="fixed_ranged"]',dialog).change(function(){ - if (this.id == 'fixed_check') { - $('div#fixed',$create_vn_dialog).show(); - $('div#ranged',$create_vn_dialog).hide(); - $('div#ranged_ipv6',$create_vn_dialog).hide(); - } - else { - $('div#fixed',$create_vn_dialog).hide(); - - if ( $('input[name="ip_version"]:checked',dialog).val() == "ipv4" ){ - $('div#ranged',$create_vn_dialog).show(); - } else { - $('div#ranged_ipv6',$create_vn_dialog).show(); - } - } + $("#vnet_wizard_ar_btn").bind("click", function(){ + add_ar_tab(number_of_ar, dialog); + number_of_ar++; }); $('#network_mode',dialog).change(function(){ @@ -1055,55 +947,6 @@ function setupCreateVNetDialog() { //Initialize shown options $('#network_mode',dialog).trigger("change"); - //$('button',dialog).button(); - - - //When we hit the add lease button... - $('#add_lease',dialog).click(function(){ - var create_form = $('#create_vn_form_easy',$create_vn_dialog); //this is our scope - - //Fetch the interesting values - var lease_ip = ""; - - if ( $('input[name="ip_version"]:checked',dialog).val() == "ipv4" ) { - lease_ip = $('#leaseip',create_form).val(); - } - - var lease_mac = $('#leasemac',create_form).val(); - - //We do not add anything to the list if there is nothing to add - if (!lease_ip.length && !lease_mac.length) { - notifyError(tr("Please provide a lease IP or MAC")); - return false; - }; - - //contains the HTML to be included in the select box. - // The space is used later to parse ip and mac - var lease = ''; - - //We append the HTML into the select box. - $('select#leases',$create_vn_dialog).append(lease); - return false; - }); - - $('#remove_lease', dialog).click(function(){ - $('select#leases :selected',$create_vn_dialog).remove(); - return false; - }); - - $('#custom_pool', dialog).change(function(){ - if ($(this).is(':checked')){ - $('#ip_start', $create_vn_dialog).removeAttr('disabled'); - $('#ip_end', $create_vn_dialog).removeAttr('disabled'); - } - else { - $('#ip_start', $create_vn_dialog).attr('disabled','disabled'); - $('#ip_end', $create_vn_dialog).attr('disabled','disabled'); - }; - }); - - $('#add_custom_var_vnet_button', dialog).click( function(){ var name = $('#custom_var_vnet_name',$create_vn_dialog).val(); @@ -1191,98 +1034,46 @@ function setupCreateVNetDialog() { break; }; - var ip_version = $('input[name="ip_version"]:checked',dialog).val(); - - var type = $('input[name="fixed_ranged"]:checked',dialog).val(); - network_json['type']=type; - //TODO: Name and bridge provided?! - var network_addr = $('#net_address',dialog).val(); var network_mask = $('#net_mask',dialog).val(); var network_dns = $('#net_dns',dialog).val(); var network_gateway = $('#net_gateway',dialog).val(); + if (network_addr.length) + network_json["network_address"] = network_addr; + + if (network_mask.length) + network_json["network_mask"] = network_mask; + if (network_dns.length) network_json["dns"] = network_dns; - if (ip_version == "ipv6") { - var site_prefix = $('#site_prefix',dialog).val(); - var global_prefix = $('#global_prefix',dialog).val(); + if (network_gateway.length) + network_json["gateway"] = network_gateway; - if (site_prefix.length) - network_json["site_prefix"] = site_prefix; + // TODO: gateway 6 in separate input +// if (network_gateway.length) +// network_json["gateway6"] = network_gateway; - if (global_prefix.length) - network_json["global_prefix"] = global_prefix; +/* +Attribute Description +NETWORK_ADDRESS Base network address +NETWORK_MASK Network mask +GATEWAY Router for this network, do not set when the network is not routable +DNS Specific DNS for this network +GATEWAY6 IPv6 router for this network +CONTEXT_FORCE_IPV4 When a vnet is IPv6 the IPv4 is not configured unless this attribute is set +*/ - if (network_gateway.length) - network_json["gateway6"] = network_gateway; + network_json["AR"] = []; - } else { - if (network_addr.length) - network_json["network_address"]=network_addr; + $('.ar_tab',dialog).each(function(){ + hash = retrieve_ar_tab_data(this); - if (network_mask.length) - network_json["network_mask"]=network_mask; - - if (network_gateway.length) - network_json["gateway"] = network_gateway; - } - - if (type == "fixed") { - var leases = $('#leases option', dialog); - var leases_obj=[]; - - //for each specified lease we prepare the JSON object - $.each(leases,function(){ - var lease_str = $(this).val().split(" "); - - if (lease_str[0].length && lease_str[1].length) { - leases_obj.push({"ip": lease_str[0], - "mac": lease_str[1]}); - } else if (lease_str[0].length) { - leases_obj.push({"ip": lease_str[0] }); - } else { - leases_obj.push({"mac": lease_str[1] }); - } - }); - - //and construct the final data for the request - network_json["leases"] = leases_obj; - } - else { //type ranged - - if (ip_version == "ipv4") { - var custom = $('#custom_pool',dialog).is(':checked'); - var ip_start = $('#ip_start',dialog).val(); - var ip_end = $('#ip_end',dialog).val(); - - if (!(ip_start.length && ip_end.length) && !network_addr.length){ - notifyError(tr("There are missing network parameters")); - return false; - }; - - if (custom){ - if (ip_start.length) - network_json["ip_start"] = ip_start; - if (ip_end.length) - network_json["ip_end"] = ip_end; - }; - } else { - var mac_start = $('#mac_start',dialog).val(); - var network_size = $('#net_size',dialog).val(); - - if (! mac_start.length){ - notifyError(tr("MAC Start must be specified")); - return false; - }; - - network_json["mac_start"] = mac_start; - - if (network_size.length) - network_json["network_size"] = network_size; + if (!$.isEmptyObject(hash)) { + network_json["AR"].push(hash); } - }; + }); //Time to add custom attributes $('#custom_var_vnet_box option',$create_vn_dialog).each(function(){ @@ -1329,6 +1120,135 @@ function popUpCreateVnetDialog() { $("input#name",$create_vn_dialog).focus(); } +function add_ar_tab(ar_id, dialog) { + var str_ar_tab_id = 'ar' + ar_id; + + var html_tab_content = '
'+ + generate_ar_tab_content(str_ar_tab_id) + + '
' + + // Append the new div containing the tab and add the tab to the list + var a = $("
"+ + tr("Address Range")+"
" + ).appendTo($("dl#vnet_wizard_ar_tabs", dialog)); + + $(html_tab_content).appendTo($("#vnet_wizard_ar_tabs_content", dialog)); + + $("a", a).trigger("click"); + + var ar_section = $('#' + str_ar_tab_id + 'Tab', dialog); + setup_ar_tab_content(ar_section, str_ar_tab_id); +} + +function generate_ar_tab_content(str_ar_tab_id){ + var html = + '
\ +
\ + \ + \ + \ + \ +
\ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ + \ + \ +
\ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ + \ + \ +
\ +
'; + + return html; +} + +function setup_ar_tab_content(ar_section, str_ar_tab_id) { + + $('input[name$="ar_type"]',ar_section).change(function(){ + $('div.ar_input', ar_section).hide(); + + switch($(this).val()){ + case "IP4": + $('div.type_ip4', ar_section).show(); + break; + case "IP4_6": + $('div.type_ip4_6', ar_section).show(); + break; + case "IP6": + $('div.type_ip6', ar_section).show(); + break; + case "ETHER": + $('div.type_ether', ar_section).show(); + break; + } + }); + + $('input#'+str_ar_tab_id+'_ar_type_ip4',ar_section).attr('checked', true); + $('input#'+str_ar_tab_id+'_ar_type_ip4',ar_section).change(); + + setupTips(ar_section); +} + +function retrieve_ar_tab_data(ar_section){ + var data = {}; + + var ar_type = $('input[name$="ar_type"]:checked',ar_section).val(); + data["TYPE"] = ar_type; + + var fields = []; + + switch(ar_type){ + case "IP4": + fields = $('div.type_ip4', ar_section).children("input"); + break; + case "IP4_6": + fields = $('div.type_ip4_6', ar_section).children("input"); + break; + case "IP6": + fields = $('div.type_ip6', ar_section).children("input"); + break; + case "ETHER": + fields = $('div.type_ether', ar_section).children("input"); + break; + } + + fields.each(function(){ + var field=$(this); + + if (field.val() != null && field.val().length){ //if has a length + data[field.attr('name')] = field.val(); + } + + }); + + // TODO MANDATORY INPUTS + /* + if (!(ip_start.length && ip_end.length) && !network_addr.length){ + notifyError(tr("There are missing network parameters")); + return false; + }; + */ + + return data +} // Listeners to the add, hold, release, delete leases operations in the // extended information panel. @@ -1346,7 +1266,6 @@ function setupLeasesOps(){ return false; }); - //fixed networks hold lease $('a.hold_lease').live("click",function(){ var lease = $(this).parents('tr').attr('ip'); var id = $(this).parents('form').attr('vnid'); From bba9a065e81cf72030be0a85be0ea41596387319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 29 May 2014 18:44:49 +0200 Subject: [PATCH 40/84] Feature #2858: Add AR wizard --- .../OpenNebulaJSON/VirtualNetworkJSON.rb | 5 +- src/sunstone/public/js/opennebula.js | 2 +- src/sunstone/public/js/plugins/vnets-tab.js | 81 +++++++++++++++++-- 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb index 73d321dd04..4d5f2f0f13 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualNetworkJSON.rb @@ -89,7 +89,10 @@ module OpenNebulaJSON end def add_ar(params=Hash.new) - super(params['ar_template_raw']) + template_json = params['ar_template'] + template = template_to_str(template_json) + + super(template) end def update_ar(params=Hash.new) diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index 82dfdc0e51..d1b40be2f9 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -782,7 +782,7 @@ var OpenNebula = { action_obj); }, "add_ar" : function(params){ - var action_obj = {"ar_template_raw" : params.data.extra_param }; + var action_obj = {"ar_template" : params.data.extra_param }; OpenNebula.Action.simple_action(params, OpenNebula.Network.resource, "add_ar", diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index 6cac3d453b..b8b13887db 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -721,8 +721,17 @@ function printLeases(vn_info){ \ \ \ - \ -
\ + '; + + if (Config.isTabActionEnabled("vnets-tab", "Network.add_ar")) { + html += + '
\ + \ +
'; + } + + html += + '
\
'; // TODO: Add other well known attributes, like DNS, GATEWAY... @@ -776,8 +785,6 @@ function printLeases(vn_info){
'; // TODO: extra ar config attributes - - // TODO: add AR } html += '\ @@ -981,7 +988,8 @@ function setupCreateVNetDialog() { } var description = $('#DESCRIPTION',dialog).val(); - var network_json = {"name" : name, "description" : description}; + if (network_addr.length) + var network_json = {"name" : name, "description" : description}; var network_mode = $('select#network_mode',dialog).val(); var bridge = $('#bridge',dialog).val(); @@ -1301,8 +1309,69 @@ function setupLeasesOps(){ return false; }); } + + if (Config.isTabActionEnabled("vnets-tab", "Network.add_ar")) { + $('button#add_ar_button').live("click",function(){ + var id = $(this).parents('form').attr('vnid'); + + popUpAddAR(id); + + return false; + }); + } } +function popUpAddAR(id){ + $('#vnet_id',$add_ar_dialog).text(id); + $add_ar_dialog.foundation().foundation('reveal', 'open'); +} + +function setupAddARDialog(){ + dialogs_context.append('
'); + $add_ar_dialog = $('#add_ar_dialog',dialogs_context); + var dialog = $add_ar_dialog; + + dialog.html('
\ +
\ +

'+tr("Add a new Address Range")+'

\ +
\ +
\ +
\ +
\ +
\ +
\ + \ +
\ +
' + + generate_ar_tab_content("add_ar") + + '\ + ×\ +
') + + // TODO: max-height? + + dialog.addClass("reveal-modal large max-height").attr("data-reveal", ""); + setupTips(dialog); + + setup_ar_tab_content(dialog, "add_ar") + + $('#add_ar_form',dialog).submit(function(){ + var vnet_id = $('#vnet_id', this).text(); + var data = retrieve_ar_tab_data(this); + + var obj = {AR: data} + Sunstone.runAction('Network.add_ar', vnet_id, obj); + + $add_ar_dialog.foundation('reveal', 'close') + return false; + }); +}; + //The DOM is ready and the ready() from sunstone.js //has been executed at this point. $(document).ready(function(){ @@ -1333,6 +1402,8 @@ $(document).ready(function(){ setupCreateVNetDialog(); setupLeasesOps(); + setupAddARDialog(); + initCheckAllBoxes(dataTable_vNetworks); tableCheckboxesListener(dataTable_vNetworks); infoListener(dataTable_vNetworks,'Network.show'); From 0fbef4c68c631559d992b0959540c1d2867ead03 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 29 May 2014 23:55:51 +0200 Subject: [PATCH 41/84] feature #2858: Reserve by IP & MAC --- include/AddressRange.h | 38 ++++++- include/AddressRangePool.h | 26 +++++ include/VirtualNetwork.h | 42 +++++++- src/cli/onevnet | 8 +- src/oca/ruby/opennebula/virtual_network.rb | 17 +++- src/rm/RequestManagerVirtualNetwork.cc | 27 ++++- src/vnm/AddressRange.cc | 112 +++++++++++++++++++++ src/vnm/AddressRangePool.cc | 48 +++++++++ src/vnm/VirtualNetwork.cc | 68 +++++++++++++ 9 files changed, 376 insertions(+), 10 deletions(-) diff --git a/include/AddressRange.h b/include/AddressRange.h index bfb6c5a67b..d95b6b3a35 100644 --- a/include/AddressRange.h +++ b/include/AddressRange.h @@ -212,11 +212,29 @@ public: */ int reserve_addr(int pvid, int vid, unsigned int rsize, AddressRange *rar); - /* - void reserve_addr_by_ip(); + /** + * Reserve a given number of addresses from this address range + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param size number of addresses to reserve + * @param rar a new address range to place the reservation + * @param ip the firs ip in the Reservation + * @return 0 on success + */ + int reserve_addr_by_ip(int pvid, int vid, unsigned int rsize, + const string& ip, AddressRange *rar); - void reserve_addr_by_mac(); - */ + /** + * Reserve a given number of addresses from this address range + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param size number of addresses to reserve + * @param rar a new address range to place the reservation + * @param mac the firs mac in the Reservation + * @return 0 on success + */ + int reserve_addr_by_mac(int pvid, int vid, unsigned int rsize, + const string& mac, AddressRange *rar); // ************************************************************************* // Helpers @@ -369,6 +387,18 @@ private: int free_addr(PoolObjectSQL::ObjectType ot, int obid, unsigned int addr_index); + /** + * Reserve a set of addresses from an starting one + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param rsize number of addresses to reserve + * @param sindex the first index to start the reservation + * @param rar a new address range to place the reservation + * @return 0 on success + */ + int reserve_addr_by_index(int pvid, int vid, unsigned int rsize, + unsigned int sindex, AddressRange *rar); + /* ---------------------------------------------------------------------- */ /* Restricted Attributes functions */ /* ---------------------------------------------------------------------- */ diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 6ad8265b83..6237c78e76 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -236,6 +236,32 @@ public: int reserve_addr(int pvid, int vid, unsigned int rsize, unsigned int ar_id, AddressRange *rar); + /** + * Reserve a number of addresses from an address range from a given ip + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param rsize number of addresses to reserve + * @param ar_id the address range to reserve the addresses from + * @param ip the first IP in the reservation + * @param rar a new address range to place the reservation + * @return 0 on success + */ + int reserve_addr_by_ip(int pvid, int vid, unsigned int rsize, + unsigned int ar_id, const string& ip, AddressRange *rar); + + /** + * Reserve a number of addresses from an address range from a given ip + * @param pvid the id of the parent VNET + * @param vid the id of the VNET making the reservation + * @param rsize number of addresses to reserve + * @param ar_id the address range to reserve the addresses from + * @param mac the first IP in the reservation + * @param rar a new address range to place the reservation + * @return 0 on success + */ + int reserve_addr_by_mac(int pvid, int vid, unsigned int rsize, + unsigned int ar_id, const string& mac, AddressRange *rar); + /** * Get the parent vnets of the Address Ranges in this AR POOL * @param parent_vnets vector with the vnet ids of the parent vnets diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index c5355ec3aa..74cc43939a 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -220,11 +220,51 @@ public: // Network Reservation functions // ************************************************************************* - int reserve_addr(VirtualNetwork *rvnet, unsigned int rsize, string& error_str); + /** + * Reserve an address range for this network and add it to the given vnet + * @param rvnet the VNET to store the reserved AR + * @param rsize number of addresses to reserve + * @param error_str error message + * @return 0 on success + */ + int reserve_addr(VirtualNetwork *rvnet, unsigned int rsize, + string& error_str); + /** + * Reserve an address range for this network and add it to the given vnet + * @param rvnet the VNET to store the reserved AR + * @param rsize number of addresses to reserve + * @param ar_id id of the address range to obtain the addresses + * @param error_str error message + * @return 0 on success + */ int reserve_addr(VirtualNetwork *rvnet, unsigned int rsize, unsigned int ar_id, string& error_str); + /** + * Reserve an address range for this network and add it to the given vnet + * @param rvnet the VNET to store the reserved AR + * @param rsize number of addresses to reserve + * @param ar_id id of the address range to obtain the addresses + * @param error_str error message + * @param ip the first ip in the reservations + * @return 0 on success + */ + int reserve_addr_by_ip(VirtualNetwork *rvnet, unsigned int rsize, + unsigned int ar_id, const string& ip, string& error_str); + + /** + * Reserve an address range for this network and add it to the given vnet + * @param rvnet the VNET to store the reserved AR + * @param rsize number of addresses to reserve + * @param ar_id id of the address range to obtain the addresses + * @param mac the first mac in the reservations + * @param error_str error message + * @return 0 on success + */ + int reserve_addr_by_mac(VirtualNetwork *rvnet, unsigned int rsize, + unsigned int ar_id, const string& mac, string& error_str); + // ************************************************************************* // Formatting & Helper functions // ************************************************************************* diff --git a/src/cli/onevnet b/src/cli/onevnet index da67514572..2b7d2c870d 100755 --- a/src/cli/onevnet +++ b/src/cli/onevnet @@ -188,11 +188,15 @@ cmd=CommandParser::CmdParser.new(ARGV) do command :reserve, reserve_desc, :vnetid, :options=>STD_OPTIONS + [OneVNetHelper::AR, OneVNetHelper::R_NAME, - OneVNetHelper::R_SIZE] do + OneVNetHelper::R_SIZE, OneVNetHelper::AR_MAC, OneVNetHelper::AR_IP] do helper.perform_action(args[0],options,"reservation made") do |vn| rsize = options[:rsize] || -1 rname = options[:rname] || -1 + addr = nil + addr = options[:mac] if options[:mac] + addr = options[:ip] if options[:ip] + if rsize == -1 STDERR.puts "Specify a size (-s size) for the reservation" exit -1 @@ -203,7 +207,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do exit -1 end - vn.reserve(rname, rsize, options[:ar_id]) + vn.reserve(rname, rsize, options[:ar_id], addr) end end diff --git a/src/oca/ruby/opennebula/virtual_network.rb b/src/oca/ruby/opennebula/virtual_network.rb index 8aef58a4a0..d2a203dce4 100644 --- a/src/oca/ruby/opennebula/virtual_network.rb +++ b/src/oca/ruby/opennebula/virtual_network.rb @@ -189,14 +189,27 @@ module OpenNebula # Reserve a set of addresses from this virtual network # @param name [String] of the reservation # @param rsize[String] number of addresses to reserve - # @param ar_id[String] the ar_id to make the reservation - def reserve(rname, rsize, ar_id) + # @param ar_id[String] the ar_id to make the reservation. If set to nil + # any address range will be used + # @param addr [String] the first address in the reservation. If set to + # nil the first free address will be used + def reserve(rname, rsize, ar_id, addr) return Error.new('ID not defined') if !@pe_id rtmpl = "NAME = #{rname}\n" rtmpl << "SIZE = #{rsize}\n" rtmpl << "AR_ID= #{ar_id}\n" if !ar_id.nil? + if !addr.nil? + if addr.include?':' + addr_name = "MAC" + else + addr_name = "IP" + end + + rtmpl << "#{addr_name} = #{addr}\n" + end + rc = @client.call(VN_METHODS[:reserve], @pe_id, rtmpl) rc = nil if !OpenNebula.is_error?(rc) diff --git a/src/rm/RequestManagerVirtualNetwork.cc b/src/rm/RequestManagerVirtualNetwork.cc index 0d19fd2198..5bdcdf1575 100644 --- a/src/rm/RequestManagerVirtualNetwork.cc +++ b/src/rm/RequestManagerVirtualNetwork.cc @@ -193,6 +193,20 @@ void VirtualNetworkReserve::request_execute( return; } + string ip, mac; + + tmpl.get("IP", ip); + + tmpl.get("MAC", mac); + + if (!with_ar_id && (!ip.empty()||!mac.empty())) + { + failure_response(ACTION, request_error("Error in reservation request", + "AR_ID must be specified for IP/MAC based reservations"), att); + + return; + } + // --------------- Check/update quotas on the target VNET ----------------- ostringstream qtmpl_s; @@ -263,7 +277,18 @@ void VirtualNetworkReserve::request_execute( if (with_ar_id) { - rc = vn->reserve_addr(rvn, size, ar_id, error_str); + if (!ip.empty()) + { + rc = vn->reserve_addr_by_ip(rvn, size, ar_id, ip, error_str); + } + else if (!mac.empty()) + { + rc = vn->reserve_addr_by_mac(rvn, size, ar_id, mac, error_str); + } + else + { + rc = vn->reserve_addr(rvn, size, ar_id, error_str); + } } else { diff --git a/src/vnm/AddressRange.cc b/src/vnm/AddressRange.cc index 7ae60d0d74..84ab4e3669 100644 --- a/src/vnm/AddressRange.cc +++ b/src/vnm/AddressRange.cc @@ -1094,6 +1094,118 @@ int AddressRange::reserve_addr(int pvid, int vid, unsigned int rsize, return 0; } +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::reserve_addr_by_index(int pvid, int vid, unsigned int rsize, + unsigned int sindex, AddressRange *rar) +{ + int pnet; + + if ((attr->vector_value("PARENT_NETWORK_ID", pnet) == 0 ) && (pnet > -1)) + { + return -1; //This address range is already a reservation + } + + /* ----------------- Allocate the new AR from sindex -------------------- */ + + for (unsigned int j=sindex; j< (sindex+rsize) ; j++) + { + if (allocated.count(j) != 0) + { + return -1; + } + } + + for (unsigned int j=sindex; j< (sindex+rsize); j++) + { + allocate_addr(PoolObjectSQL::NET, vid, j); + } + + /* ------------------------- Initialize the new AR ---------------------- */ + + VectorAttribute * new_ar = attr->clone(); + string errmsg; + + set_mac(sindex, new_ar); + + if (type & 0x00000002 ) + { + set_ip(sindex, new_ar); + } + + new_ar->replace("SIZE",rsize); + + rar->from_vattr(new_ar, errmsg); + + new_ar->replace("PARENT_NETWORK_ID", pvid); + + new_ar->replace("PARENT_NETWORK_AR_ID",id); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::reserve_addr_by_ip(int pvid, int vid, unsigned int rsize, + const string& ip_s, AddressRange *rar) +{ + if (!(type & 0x00000002))//Not of type IP4 or IP4_6 + { + return -1; + } + + unsigned int ip_i; + + if (ip_to_i(ip_s, ip_i) == -1) + { + return -1; + } + + if (ip_i < ip) + { + return -1; + } + + unsigned int sindex = ip_i - ip; + + if (sindex >= size ) + { + return -1; + } + + return reserve_addr_by_index(pvid, vid, rsize, sindex, rar); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRange::reserve_addr_by_mac(int pvid, int vid, unsigned int rsize, + const string& mac_s, AddressRange *rar) +{ + unsigned int mac_i[2]; + + if (mac_to_i(mac_s, mac_i) == -1) + { + return -1; + } + + if ((mac_i[1] != mac[1]) || (mac_i[0] < mac[0])) + { + return -1; + } + + unsigned int sindex = mac_i[0] - mac[0]; + + if (sindex >= size) + { + return -1; + } + + return reserve_addr_by_index(pvid, vid, rsize, sindex, rar); +} + /* ************************************************************************** */ /* ************************************************************************** */ diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index 74f3eb82f8..c8ec02172c 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -542,6 +542,54 @@ int AddressRangePool::reserve_addr(int pvid, int vid, unsigned int rsize, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int AddressRangePool::reserve_addr_by_ip(int pvid, int vid, unsigned int rsize, + unsigned int ar_id, const string& ip, AddressRange *rar) +{ + map::iterator it; + + it = ar_pool.find(ar_id); + + if (it == ar_pool.end()) + { + return -1; + } + + if (it->second->reserve_addr_by_ip(pvid, vid, rsize, ip, rar) == 0) + { + used_addr += rsize; + return 0; + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int AddressRangePool::reserve_addr_by_mac(int pvid, int vid, unsigned int rsize, + unsigned int ar_id, const string& mac, AddressRange *rar) +{ + map::iterator it; + + it = ar_pool.find(ar_id); + + if (it == ar_pool.end()) + { + return -1; + } + + if (it->second->reserve_addr_by_mac(pvid, vid, rsize, mac, rar) == 0) + { + used_addr += rsize; + return 0; + } + + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + unsigned int AddressRangePool::get_parents(vector& parent_nets) { int vid; diff --git a/src/vnm/VirtualNetwork.cc b/src/vnm/VirtualNetwork.cc index 5212c8b08a..1f3451d548 100644 --- a/src/vnm/VirtualNetwork.cc +++ b/src/vnm/VirtualNetwork.cc @@ -865,3 +865,71 @@ int VirtualNetwork::reserve_addr(VirtualNetwork *rvnet, return 0; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualNetwork::reserve_addr_by_ip(VirtualNetwork *rvnet, + unsigned int rsize, unsigned int ar_id, const string& ip, string& error_str) +{ + AddressRange *rar = rvnet->allocate_ar(); + + if (ar_pool.reserve_addr_by_ip(oid,rvnet->get_oid(),rsize,ar_id,ip,rar)!=0) + { + ostringstream oss; + + oss << "Not enough free addresses in address range " << ar_id + << ", or it does not exist"; + + error_str = oss.str(); + + delete rar; + + return -1; + } + + if (rvnet->add_ar(rar) != 0) + { + error_str = "Could not add the address range to the netwok"; + + delete rar; + + return -1; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int VirtualNetwork::reserve_addr_by_mac(VirtualNetwork *rvnet, + unsigned int rsize, unsigned int ar_id, const string& mac, string& error_str) +{ + AddressRange *rar = rvnet->allocate_ar(); + + if (ar_pool.reserve_addr_by_mac(oid,rvnet->get_oid(),rsize,ar_id,mac,rar)!=0) + { + ostringstream oss; + + oss << "Not enough free addresses in address range " << ar_id + << ", or it does not exist"; + + error_str = oss.str(); + + delete rar; + + return -1; + } + + if (rvnet->add_ar(rar) != 0) + { + error_str = "Could not add the address range to the netwok"; + + delete rar; + + return -1; + } + + return 0; +} From ba762c118fdfe3c224e302056f2d25ace53baa8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 30 May 2014 12:12:44 +0200 Subject: [PATCH 42/84] Feature #2858: fix vnet wizard --- src/sunstone/public/js/plugins/vnets-tab.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index b8b13887db..91eb2f57a6 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -987,9 +987,11 @@ function setupCreateVNetDialog() { return false; } + var network_json = {"name" : name}; + var description = $('#DESCRIPTION',dialog).val(); - if (network_addr.length) - var network_json = {"name" : name, "description" : description}; + if (description.length) + network_json['description'] = description; var network_mode = $('select#network_mode',dialog).val(); var bridge = $('#bridge',dialog).val(); From cff23bdf97ce6643d8acfe08c656e55b3c4c4a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 30 May 2014 12:15:51 +0200 Subject: [PATCH 43/84] Feature #2858: fix used leases counter --- src/sunstone/public/js/plugins/vnets-tab.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index 91eb2f57a6..05cdd9b29a 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -550,7 +550,7 @@ function vnElements(){ function vNetworkElementArray(vn_json){ var network = vn_json.VNET; - addresses_vnets = addresses_vnets + parseInt(network.TOTAL_LEASES); + addresses_vnets = addresses_vnets + parseInt(network.USED_LEASES); return [ '', From 41e0412bdcb4573bd3bce3dfd0124f7c29f833d3 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 30 May 2014 12:56:35 +0200 Subject: [PATCH 44/84] feature #2858: free by owner returns the number of addressses freed --- include/AddressRangePool.h | 3 ++- include/VirtualNetwork.h | 5 +++-- src/vnm/AddressRangePool.cc | 7 +++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/AddressRangePool.h b/include/AddressRangePool.h index 6237c78e76..71d2a08c56 100644 --- a/include/AddressRangePool.h +++ b/include/AddressRangePool.h @@ -206,8 +206,9 @@ public: * Frees all the addressed owned by the given object * @param ot the type of the object requesting the address (VM or NET) * @param obid the id of the object requesting the address + * @return the number of addresses freed */ - void free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid); + int free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid); // ************************************************************************* // Address reservation diff --git a/include/VirtualNetwork.h b/include/VirtualNetwork.h index 74cc43939a..dd5a96946c 100644 --- a/include/VirtualNetwork.h +++ b/include/VirtualNetwork.h @@ -194,10 +194,11 @@ public: * Release all previously given address leases to the given object * @param ot the type of the object requesting the address (VM or NET) * @param obid the id of the object requesting the address + * @return the number of addresses freed */ - void free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid) + int free_addr_by_owner(PoolObjectSQL::ObjectType ot, int obid) { - ar_pool.free_addr_by_owner(ot, obid); + return ar_pool.free_addr_by_owner(ot, obid); } /** diff --git a/src/vnm/AddressRangePool.cc b/src/vnm/AddressRangePool.cc index c8ec02172c..314f4691ae 100644 --- a/src/vnm/AddressRangePool.cc +++ b/src/vnm/AddressRangePool.cc @@ -377,14 +377,17 @@ void AddressRangePool::free_addr_by_ip(PoolObjectSQL::ObjectType ot, int obid, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -void AddressRangePool::free_addr_by_owner(PoolObjectSQL::ObjectType ot, int oid) +int AddressRangePool::free_addr_by_owner(PoolObjectSQL::ObjectType ot, int oid) { map::iterator it; + unsigned int used_addr_ini = used_addr; for (it=ar_pool.begin(); it!=ar_pool.end(); it++) { - used_addr = used_addr - it->second->free_addr_by_owner(ot, oid); + used_addr -= it->second->free_addr_by_owner(ot, oid); } + + return used_addr_ini - used_addr; } /* -------------------------------------------------------------------------- */ From 20ed8ad25db0690858f6261160a5211fcdf50b65 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 30 May 2014 15:39:25 +0200 Subject: [PATCH 45/84] feature #2858: Update quotas when freeing reserved IPs --- src/rm/RequestManagerDelete.cc | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/rm/RequestManagerDelete.cc b/src/rm/RequestManagerDelete.cc index b1b81585b7..f8a75119e4 100644 --- a/src/rm/RequestManagerDelete.cc +++ b/src/rm/RequestManagerDelete.cc @@ -338,7 +338,10 @@ int VirtualNetworkDelete::drop(int oid, PoolObjectSQL * object, string& error_ms vnet->get_parents(parents); - int rc = RequestManagerDelete::drop(oid, object, error_msg); + int uid = vnet->get_uid(); + int gid = vnet->get_gid(); + + int rc = RequestManagerDelete::drop(oid, object, error_msg); VirtualNetworkPool *vnpool = static_cast(pool); @@ -351,11 +354,26 @@ int VirtualNetworkDelete::drop(int oid, PoolObjectSQL * object, string& error_ms continue; } - vnet->free_addr_by_owner(PoolObjectSQL::NET, oid); + int freed = vnet->free_addr_by_owner(PoolObjectSQL::NET, oid); + pool->update(vnet); - vnet->unlock(); + + if (freed > 0) + { + ostringstream oss; + Template tmpl; + + for (int i= 0 ; i < freed ; i++) + { + oss << " NIC = [ NETWORK_ID = " << *it << " ]" << endl; + } + + tmpl.parse_str_or_xml(oss.str(), error_msg); + + Quotas::quota_del(Quotas::NETWORK, uid, gid, &tmpl); + } } return rc; From 7993d7a88ce93cf7db40aa184b5a9c28290dbb69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 30 May 2014 13:35:58 +0200 Subject: [PATCH 46/84] Feature #2858: Separate vnet wizard into steps --- src/sunstone/public/js/plugins/vnets-tab.js | 210 +++++++++++--------- 1 file changed, 111 insertions(+), 99 deletions(-) diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index 05cdd9b29a..9f01851d34 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -28,9 +28,18 @@ var create_vn_tmpl = \ \ \ -
\ -
\ -
\ +
\ +
\ + \ +
\ +
\ +

'+tr("General")+'
\ +

'+tr("Configuration")+'
\ +

'+tr("Addresses")+'
\ +

'+tr("Other")+'
\ +
\ +
\ +
\
\
\ \ @@ -44,6 +53,28 @@ var create_vn_tmpl =
\
\
\ +
\ +
\ + \ + \ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\
\
\ \ @@ -85,113 +116,94 @@ var create_vn_tmpl =
\
\
\ -
\ -
\ -
\ - \ - \ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ - \ - \ -
\ -
\ -
\ +
\ +
\
\
\ -
\ - ' + tr("Address Ranges") + '\ -
\ -
\ -
\ - \ -
\ -
\ -
\ +
\ +
\ +
\ + \ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ +
\ + ' + tr("Custom attributes") + '\ +
\ +
\ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ + \ + \ +
\
\
\ - \ -
\ -
\ -
\ -
\ -
\ -
\ - ' + tr("Custom attributes") + '\ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ -
\ -
\ -
\ - \ - \ -
\ +
\ +
\ +
\ + \
\
\ -
\ -
\ -
\ - \ -
\ -
\ -
\ -
\ -
\ +
\ +
\
\
\ -
\ - \ - \ +
\
\ - \
\ -
\ -
\ -
\ -
\ -

'+tr("Write the Virtual Network template here")+'

\ -
\ -
\ -
\ -
\ - \ -
\ -
\ -
\ - \ - \ -
\ -
\ +
\ + \ + \
\ -
\ - ×'; + \ +
\ +
\ +
\ +
\ +
\ +

'+tr("Write the Virtual Network template here")+'

\ +
\ +
\ +
\ +
\ + \ +
\ +
\ +
\ + \ + \ +
\ +
\ +
\ +
\ +×'; var update_vnet_tmpl = '
\ From d41ccaf54db4907a63e64173736d4d1520922dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Fri, 30 May 2014 16:31:09 +0200 Subject: [PATCH 47/84] Feature #2858: Add first AR on pop up --- src/sunstone/public/js/plugins/vnets-tab.js | 24 +++++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/sunstone/public/js/plugins/vnets-tab.js b/src/sunstone/public/js/plugins/vnets-tab.js index 9f01851d34..8849e6c16d 100644 --- a/src/sunstone/public/js/plugins/vnets-tab.js +++ b/src/sunstone/public/js/plugins/vnets-tab.js @@ -41,13 +41,13 @@ var create_vn_tmpl =
\
\
\ -
\ +
\ \ \
\
\
\ -
\ +
\ \