From bffe12b4fc25b1f66d48bcf3a1ad62e261c8fbb1 Mon Sep 17 00:00:00 2001 From: Quentin Smith Date: Tue, 29 May 2018 19:20:26 -0400 Subject: [PATCH] Remove quadratic netifaces.interfaces() call --- debian/changelog | 6 ++++++ invirt-dhcpserver | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/debian/changelog b/debian/changelog index 463c04b..e0c4b23 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +invirt-dhcp (0.0.10) unstable; urgency=low + + * Remove quadratic netifaces.interfaces() call + + -- Quentin Smith Tue, 29 May 2018 19:20:20 -0400 + invirt-dhcp (0.0.9) precise; urgency=low * Fix routes that the boot process failed to install. diff --git a/invirt-dhcpserver b/invirt-dhcpserver index 5c96f13..6ffcbda 100755 --- a/invirt-dhcpserver +++ b/invirt-dhcpserver @@ -1,4 +1,5 @@ #!/usr/bin/python +import os.path import sys import pydhcplib import pydhcplib.dhcp_network @@ -24,11 +25,32 @@ from invirt.config import structs as config dhcp_options = {'domain_name_server': ','.join(config.dhcp.dns), 'ip_address_lease_time': config.dhcp.get('leasetime', 60*60*24)} +class Interfaces(object): + @staticmethod + def primary_ip(name): + """primary_ip returns an interface's primary IP address. + + This is the first IPv4 address returned by "ip addr show $name" + """ + # TODO(quentin): The netifaces module is a pile of crappy C. + # Figure out a way to do this in native Python. + return ni.ifaddresses(name)[ni.AF_INET][0]['addr'] + + @staticmethod + def exists(name): + """exists checks if an interface exists. + + Args: + name: Interface name + """ + return os.path.exists("/sys/class/net/"+name) + + class DhcpBackend: def __init__(self, queue): database.connect() self.queue = queue - self.main_ip = ni.ifaddresses(config.xen.iface)[ni.AF_INET][0]['addr'] + self.main_ip = Interfaces.primary_ip(config.xen.iface) def add_route_and_arp(self, ip, intf, gateway): try: p = Popen(['ip', 'route', 'add', ip, 'dev', intf, 'src', self.main_ip, 'metric', '2' if intf.startswith('vif') else '1'], stdout=PIPE, stderr=PIPE) @@ -77,7 +99,7 @@ class DhcpBackend: # in use. for viftype in ('tap', 'vif'): vif = '%s%s.%s' % (viftype, domid, vifnum) - if vif in ni.interfaces(): + if Interface.exists(vif): self.add_route_and_arp(nic.ip, vif, nic.gateway) xsc.close() return vif -- 1.7.9.5