Commit 91ecc783 authored by Italo Cunha's avatar Italo Cunha Committed by Peter V. Saveliev

Add sort_addresses option to IPDB

This commit adds a sort_addresses parameter to IPDB.  If set to
True, then IPDB will use SortedIPaddrSet instead of IPaddrSet to
keep addresses sorted.  Sorting IP addresses is optional because
SortedIPaddrSet incurs CPU overhead as its iterators are not
parent 7727cc9e
......@@ -18,7 +18,6 @@ from pyroute2.ipdb.transactional import Transactional
from pyroute2.ipdb.transactional import with_transaction
from pyroute2.ipdb.transactional import SYNC_TIMEOUT
from pyroute2.ipdb.linkedset import LinkedSet
from pyroute2.ipdb.linkedset import IPaddrSet
from pyroute2.ipdb.exceptions import CreateException
from pyroute2.ipdb.exceptions import CommitException
from pyroute2.ipdb.exceptions import PartialCommitException
......@@ -120,7 +119,7 @@ class Interface(Transactional):
with self._direct_state:
for i in ('change', 'mask'):
del self[i]
self['ipaddr'] = IPaddrSet()
self['ipaddr'] = self.ipdb.init_ipaddr_set()
self['ports'] = LinkedSet()
self['vlans'] = LinkedSet()
self['ipdb_priority'] = 0
......@@ -1083,7 +1082,7 @@ class InterfacesDict(TransactionalBase):
device = self[ifname] = self[index]
if index not in self.ipdb.ipaddr:
self.ipdb.ipaddr[index] = IPaddrSet()
self.ipdb.ipaddr[index] = self.ipdb.init_ipaddr_set()
if index not in self.ipdb.neighbours:
self.ipdb.neighbours[index] = LinkedSet()
......@@ -1147,7 +1146,7 @@ class AddressesDict(dict):
# Reload addresses from the kernel.
# (This is a workaround to reorder primary and secondary addresses.)
for k in self.keys():
self[k] = IPaddrSet()
self[k] = self.ipdb.init_ipaddr_set()
for msg in
for idx in self.keys():
......@@ -536,6 +536,7 @@ from pyroute2.netlink.rtnl.ifinfmsg import ifinfmsg
from pyroute2.ipdb import rule
from pyroute2.ipdb import route
from pyroute2.ipdb import interface
from pyroute2.ipdb.linkedset import IPaddrSet, SortedIPaddrSet
from pyroute2.ipdb.transactional import SYNC_TIMEOUT
log = logging.getLogger(__name__)
......@@ -580,8 +581,10 @@ class IPDB(object):
def __init__(self, nl=None, mode='implicit',
restart_on_error=None, nl_async=None,
ignore_rtables=None, callbacks=None):
ignore_rtables=None, callbacks=None,
self.mode = mode
self.sort_addresses = sort_addresses
self._event_map = {}
self._deferred = {}
self._loaded = set()
......@@ -990,3 +993,9 @@ class IPDB(object):
if len(self._cb_threads.get(cuid, ())) == 0:
del self._cb_threads[cuid]
def init_ipaddr_set(self):
if self.sort_addresses:
return SortedIPaddrSet()
return IPaddrSet()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment