Skip to content

Commit 8e67db1

Browse files
committed
Use Round Robin in Address Ranges
Signed-off-by: Jukka Jokiniva <[email protected]>
1 parent 48e1c85 commit 8e67db1

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

include/AddressRange.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class AddressRange
120120
* - IP
121121
* - ULA_PREFIX
122122
* - GLOBAL_PREFIX
123+
* - NEXT_INDEX*
123124
*
124125
* The following can be defined to override VNET values:
125126
* - BRIDGE
@@ -437,7 +438,7 @@ class AddressRange
437438
* Base constructor it cannot be called directly but from the
438439
* AddressRange factory constructor.
439440
*/
440-
AddressRange(unsigned int _id):id(_id) {};
441+
AddressRange(unsigned int _id):id(_id), next(0) {};
441442

442443
/* ---------------------------------------------------------------------- */
443444
/* Address/AR helper functions to build/parse driver messages */
@@ -557,6 +558,11 @@ class AddressRange
557558
*/
558559
std::map<unsigned int, long long> allocated;
559560

561+
/**
562+
* Lookup index for the next free address lease
563+
*/
564+
unsigned int next;
565+
560566
private:
561567
/* ---------------------------------------------------------------------- */
562568
/* String to binary conversion functions for different address types */

include/AddressRangeInternal.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class VectorAttribute;
2626
class AddressRangeInternal : public AddressRange
2727
{
2828
public:
29-
AddressRangeInternal(unsigned int _id):AddressRange(_id), next(0) {};
29+
AddressRangeInternal(unsigned int _id):AddressRange(_id) {};
3030

3131
virtual ~AddressRangeInternal() {};
3232

@@ -90,11 +90,6 @@ class AddressRangeInternal : public AddressRange
9090
};
9191

9292
private:
93-
/**
94-
* Lookup index for the next free address lease
95-
*/
96-
unsigned int next;
97-
9893
/**
9994
* Get a free lease
10095
* @param index of the free lease, undefined if error
@@ -110,7 +105,7 @@ class AddressRangeInternal : public AddressRange
110105
* @param msg with error description if any
111106
* @return 0 on success -1 otherwise
112107
*/
113-
int get_range_addr(unsigned int& index, unsigned int sz, std::string& msg) const;
108+
int get_range_addr(unsigned int& index, unsigned int sz, std::string& msg);
114109
};
115110

116111
#endif

share/doc/xsd/vnet_pool.xsd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<xs:element name="MAC" type="xs:string"/>
9898
<xs:element name="PARENT_NETWORK_AR_ID" type="xs:string" minOccurs="0"/>
9999
<xs:element name="SIZE" type="xs:integer"/>
100+
<xs:element name="NEXT_INDEX" type="xs:integer"/>
100101
<xs:element name="TYPE" type="xs:string"/>
101102
<xs:element name="ULA_PREFIX" type="xs:string" minOccurs="0"/>
102103
<xs:element name="VN_MAD" type="xs:string" minOccurs="0"/>
@@ -112,4 +113,4 @@
112113
</xs:sequence>
113114
</xs:complexType>
114115
</xs:element>
115-
</xs:schema>
116+
</xs:schema>

src/oca/go/src/goca/schemas/virtualnetwork/virtualnetwork.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type AR struct {
6767
MAC string `xml:"MAC,omitempty"`
6868
ParentNetworkARID string `xml:"PARENT_NETWORK_AR_ID,omitempty"` // minOccurs=0
6969
Size int `xml:"SIZE"`
70+
NextIndex int `xml:"NEXT_INDEX,omitempty"`
7071
Type string `xml:"TYPE"`
7172
ULAPrefix string `xml:"ULA_PREFIX,omitempty"` // minOccurs=0
7273
VNMAD string `xml:"VN_MAD,omitempty"` // minOccurs=0

src/vnm/AddressRange.cc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ int AddressRange::from_attr(VectorAttribute *vattr, string& error_msg)
263263
return -1;
264264
}
265265

266+
/* ------------------------- Next Index -------------------------------- */
267+
268+
if ( vattr->vector_value("NEXT_INDEX", next) != 0 )
269+
{
270+
next = 0;
271+
}
272+
266273
/* ------------------------- Security Groups ---------------------------- */
267274

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

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

498+
vup->replace("NEXT_INDEX", next);
499+
491500
string value = vup->vector_value("SECURITY_GROUPS");
492501

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

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

574+
if ( vattr->vector_value("NEXT_INDEX", next) != 0 )
575+
{
576+
next = 0;
577+
}
578+
565579
rc += mac_to_i(vattr->vector_value("MAC"), mac);
566580

567581
if (is_ipv4())
@@ -737,6 +751,7 @@ void AddressRange::to_xml(ostringstream &oss) const
737751
}
738752

739753
oss << "<USED_LEASES>" << get_used_addr() << "</USED_LEASES>";
754+
oss << "<NEXT_INDEX>" << next << "</NEXT_INDEX>";
740755
oss << "</AR>";
741756
}
742757

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

908923
oss << "</LEASES>";
909924
}
910-
925+
oss << "<NEXT_INDEX>" << next << "</NEXT_INDEX>";
911926
oss << "</AR>";
912927
}
913928

@@ -1429,6 +1444,8 @@ void AddressRange::allocated_to_attr()
14291444
}
14301445

14311446
attr->replace("ALLOCATED", oss.str());
1447+
1448+
attr->replace("NEXT_INDEX", next);
14321449
}
14331450

14341451
/* -------------------------------------------------------------------------- */
@@ -1854,6 +1871,7 @@ const char * AddressRange::SG_RULE_ATTRIBUTES[] =
18541871
"AR_ID",
18551872
"TYPE",
18561873
"SIZE",
1874+
"NEXT_INDEX",
18571875
"MAC",
18581876
"IP",
18591877
"IP6"
@@ -2005,6 +2023,8 @@ int AddressRange::reserve_addr(int vid, unsigned int rsize, AddressRange *rar)
20052023

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

2026+
new_ar->replace("NEXT_INDEX", next);
2027+
20082028
new_ar->remove("IPAM_MAD");
20092029

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

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

2087+
new_ar->replace("NEXT_INDEX", next);
2088+
20672089
new_ar->remove("IPAM_MAD");
20682090

20692091
rar->from_vattr(new_ar, errmsg);

src/vnm/AddressRangeInternal.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,42 @@
1919
int AddressRangeInternal::get_single_addr(unsigned int& index, std::string& msg)
2020
{
2121
unsigned int ar_size = get_size();
22+
unsigned int next_original = next;
2223

2324
for ( unsigned int i=0; i<ar_size; i++, next = (next+1)%ar_size )
2425
{
2526
if ( allocated.count(next) == 0 )
2627
{
2728
index = next;
29+
next = (next+1)%ar_size;
2830
return 0;
2931
}
3032
}
3133

34+
next = next_original;
3235
msg = "Not free addresses available";
3336
return -1;
3437
}
3538

3639
int AddressRangeInternal::get_range_addr(unsigned int& index,
37-
unsigned int rsize, std::string& msg) const
40+
unsigned int rsize, std::string& msg)
3841
{
3942
unsigned int ar_size = get_size();
43+
unsigned int next_original = next;
4044
bool valid;
4145

42-
for (unsigned int i=0; i< ar_size; i++)
46+
for (unsigned int i=0; i< ar_size; i++, next = (next+1)%ar_size )
4347
{
44-
if ( allocated.count(i) != 0 )
48+
if ( allocated.count(next) != 0 )
4549
{
4650
continue;
4751
}
4852

4953
valid = true;
5054

51-
for (unsigned int j=0; j<rsize; j++, i++)
55+
for (unsigned int j=0; j<rsize; j++, i++, next++)
5256
{
53-
if ( allocated.count(i) != 0 || i >= ar_size )
57+
if ( allocated.count(next) != 0 || i >= ar_size || next >= ar_size )
5458
{
5559
valid = false;
5660
break;
@@ -59,11 +63,13 @@ int AddressRangeInternal::get_range_addr(unsigned int& index,
5963

6064
if (valid == true)
6165
{
62-
index = i - rsize;
66+
index = next - rsize;
67+
next = next%ar_size;
6368
return 0;
6469
}
6570
}
6671

72+
next = next_original;
6773
msg = "There isn't a continuous range big enough";
6874
return -1;
6975
}

0 commit comments

Comments
 (0)