Skip to content

Use Round Robin in Address Ranges #7053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/AddressRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class AddressRange
* - IP
* - ULA_PREFIX
* - GLOBAL_PREFIX
* - NEXT_INDEX*
*
* The following can be defined to override VNET values:
* - BRIDGE
Expand Down Expand Up @@ -437,7 +438,7 @@ class AddressRange
* Base constructor it cannot be called directly but from the
* AddressRange factory constructor.
*/
AddressRange(unsigned int _id):id(_id) {};
AddressRange(unsigned int _id):id(_id), next(0) {};

/* ---------------------------------------------------------------------- */
/* Address/AR helper functions to build/parse driver messages */
Expand Down Expand Up @@ -557,6 +558,11 @@ class AddressRange
*/
std::map<unsigned int, long long> allocated;

/**
* Lookup index for the next free address lease
*/
unsigned int next;

private:
/* ---------------------------------------------------------------------- */
/* String to binary conversion functions for different address types */
Expand Down
9 changes: 2 additions & 7 deletions include/AddressRangeInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class VectorAttribute;
class AddressRangeInternal : public AddressRange
{
public:
AddressRangeInternal(unsigned int _id):AddressRange(_id), next(0) {};
AddressRangeInternal(unsigned int _id):AddressRange(_id) {};

virtual ~AddressRangeInternal() {};

Expand Down Expand Up @@ -90,11 +90,6 @@ class AddressRangeInternal : public AddressRange
};

private:
/**
* Lookup index for the next free address lease
*/
unsigned int next;

/**
* Get a free lease
* @param index of the free lease, undefined if error
Expand All @@ -110,7 +105,7 @@ class AddressRangeInternal : public AddressRange
* @param msg with error description if any
* @return 0 on success -1 otherwise
*/
int get_range_addr(unsigned int& index, unsigned int sz, std::string& msg) const;
int get_range_addr(unsigned int& index, unsigned int sz, std::string& msg);
};

#endif
3 changes: 2 additions & 1 deletion share/doc/xsd/vnet_pool.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<xs:element name="MAC" type="xs:string"/>
<xs:element name="PARENT_NETWORK_AR_ID" type="xs:string" minOccurs="0"/>
<xs:element name="SIZE" type="xs:integer"/>
<xs:element name="NEXT_INDEX" type="xs:integer"/>
<xs:element name="TYPE" type="xs:string"/>
<xs:element name="ULA_PREFIX" type="xs:string" minOccurs="0"/>
<xs:element name="VN_MAD" type="xs:string" minOccurs="0"/>
Expand All @@ -112,4 +113,4 @@
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</xs:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type AR struct {
MAC string `xml:"MAC,omitempty"`
ParentNetworkARID string `xml:"PARENT_NETWORK_AR_ID,omitempty"` // minOccurs=0
Size int `xml:"SIZE"`
NextIndex int `xml:"NEXT_INDEX,omitempty"`
Type string `xml:"TYPE"`
ULAPrefix string `xml:"ULA_PREFIX,omitempty"` // minOccurs=0
VNMAD string `xml:"VN_MAD,omitempty"` // minOccurs=0
Expand Down
24 changes: 23 additions & 1 deletion src/vnm/AddressRange.cc
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ int AddressRange::from_attr(VectorAttribute *vattr, string& error_msg)
return -1;
}

/* ------------------------- Next Index -------------------------------- */

if ( vattr->vector_value("NEXT_INDEX", next) != 0 )
{
next = 0;
}

/* ------------------------- Security Groups ---------------------------- */

value = vattr->vector_value("SECURITY_GROUPS");
Expand Down Expand Up @@ -488,6 +495,8 @@ int AddressRange::update_attributes(

vup->replace("SIZE", size);

vup->replace("NEXT_INDEX", next);

string value = vup->vector_value("SECURITY_GROUPS");

security_groups.clear();
Expand Down Expand Up @@ -562,6 +571,11 @@ int AddressRange::from_vattr_db(VectorAttribute *vattr)

rc += vattr->vector_value("SIZE", size);

if ( vattr->vector_value("NEXT_INDEX", next) != 0 )
{
next = 0;
}

rc += mac_to_i(vattr->vector_value("MAC"), mac);

if (is_ipv4())
Expand Down Expand Up @@ -737,6 +751,7 @@ void AddressRange::to_xml(ostringstream &oss) const
}

oss << "<USED_LEASES>" << get_used_addr() << "</USED_LEASES>";
oss << "<NEXT_INDEX>" << next << "</NEXT_INDEX>";
oss << "</AR>";
}

Expand Down Expand Up @@ -907,7 +922,7 @@ void AddressRange::to_xml(ostringstream &oss, const vector<int>& vms,

oss << "</LEASES>";
}

oss << "<NEXT_INDEX>" << next << "</NEXT_INDEX>";
oss << "</AR>";
}

Expand Down Expand Up @@ -1429,6 +1444,8 @@ void AddressRange::allocated_to_attr()
}

attr->replace("ALLOCATED", oss.str());

attr->replace("NEXT_INDEX", next);
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -1854,6 +1871,7 @@ const char * AddressRange::SG_RULE_ATTRIBUTES[] =
"AR_ID",
"TYPE",
"SIZE",
"NEXT_INDEX",
"MAC",
"IP",
"IP6"
Expand Down Expand Up @@ -2005,6 +2023,8 @@ int AddressRange::reserve_addr(int vid, unsigned int rsize, AddressRange *rar)

new_ar->replace("SIZE", rsize);

new_ar->replace("NEXT_INDEX", next);

new_ar->remove("IPAM_MAD");

rar->from_vattr(new_ar, errmsg);
Expand Down Expand Up @@ -2064,6 +2084,8 @@ int AddressRange::reserve_addr_by_index(int vid, unsigned int rsize,

new_ar->replace("SIZE", rsize);

new_ar->replace("NEXT_INDEX", next);

new_ar->remove("IPAM_MAD");

rar->from_vattr(new_ar, errmsg);
Expand Down
18 changes: 12 additions & 6 deletions src/vnm/AddressRangeInternal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,42 @@
int AddressRangeInternal::get_single_addr(unsigned int& index, std::string& msg)
{
unsigned int ar_size = get_size();
unsigned int next_original = next;

for ( unsigned int i=0; i<ar_size; i++, next = (next+1)%ar_size )
{
if ( allocated.count(next) == 0 )
{
index = next;
next = (next+1)%ar_size;
return 0;
}
}

next = next_original;
msg = "Not free addresses available";
return -1;
}

int AddressRangeInternal::get_range_addr(unsigned int& index,
unsigned int rsize, std::string& msg) const
unsigned int rsize, std::string& msg)
{
unsigned int ar_size = get_size();
unsigned int next_original = next;
bool valid;

for (unsigned int i=0; i< ar_size; i++)
for (unsigned int i=0; i< ar_size; i++, next = (next+1)%ar_size )
{
if ( allocated.count(i) != 0 )
if ( allocated.count(next) != 0 )
{
continue;
}

valid = true;

for (unsigned int j=0; j<rsize; j++, i++)
for (unsigned int j=0; j<rsize; j++, i++, next++)
{
if ( allocated.count(i) != 0 || i >= ar_size )
if ( allocated.count(next) != 0 || i >= ar_size || next >= ar_size )
{
valid = false;
break;
Expand All @@ -59,11 +63,13 @@ int AddressRangeInternal::get_range_addr(unsigned int& index,

if (valid == true)
{
index = i - rsize;
index = next - rsize;
next = next%ar_size;
return 0;
}
}

next = next_original;
msg = "There isn't a continuous range big enough";
return -1;
}
Expand Down