Fix routes that the boot process failed to install.
authorMitchell E Berger <mitchb@mit.edu>
Tue, 29 May 2018 22:14:14 +0000 (18:14 -0400)
committerMitchell E Berger <mitchb@mit.edu>
Tue, 29 May 2018 22:14:14 +0000 (18:14 -0400)
debian/changelog
invirt-dhcpserver

index 9acc35e..463c04b 100644 (file)
@@ -1,3 +1,9 @@
+invirt-dhcp (0.0.9) precise; urgency=low
+
+  * Fix routes that the boot process failed to install.
+
+ -- Mitchell Berger <mitchb@mit.edu>  Tue, 29 May 2018 18:15:00 -0400
+
 invirt-dhcp (0.0.8) precise; urgency=low
 
   * Populate subnet_mask and router options from individual NIC parameters
index e803ecd..5c96f13 100755 (executable)
@@ -12,6 +12,8 @@ from Queue import Queue
 from threading import Thread
 from subprocess import PIPE, Popen
 import netifaces as ni
+sys.path.append('/usr/lib/xen-default/lib/python/')
+from xen.lowlevel import xs
 
 import syslog as s
 
@@ -26,6 +28,18 @@ class DhcpBackend:
     def __init__(self, queue):
         database.connect()
         self.queue = queue
+        self.main_ip = ni.ifaddresses(config.xen.iface)[ni.AF_INET][0]['addr']
+    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)
+            (out, err) = p.communicate()
+            if p.returncode == 0:
+                s.syslog(s.LOG_INFO, "Added route for IP %s to interface %s" % (ip, intf))
+                self.queue.put((ip, gateway))
+            sys.stderr.write(err)
+            sys.stdout.write(out)
+        except Exception as e:
+            s.syslog(s.LOG_ERR, "Could not add route for IP %s: %s" % (ip, e))
     def findNIC(self, mac):
         database.clear_cache()
         return database.NIC.query.filter_by(mac_addr=mac).first()
@@ -42,6 +56,38 @@ class DhcpBackend:
             if parts[1] == ipstr:
                 s.syslog(s.LOG_DEBUG, "find_interface found "+str(nic.ip)+" on "+parts[0])
                 return parts[0]
+        # Either the machine isn't running, or the route is missing.  We can
+        # fix the latter.
+        try:
+            xsc = xs.xs()
+            domid = xsc.read('', '/vm/%s/device/vif/0/frontend-id' % (nic.machine.uuid))
+            # If we didn't find the domid, the machine is either off or the
+            # UUID in xenstore isn't right.  Try slightly harder.
+            if not domid:
+                for uuid in xsc.ls('', '/vm'):
+                    if xsc.read('', '/vm/%s/name' % (uuid)) == 'd_' + nic.machine.name:
+                        domid = xsc.read('', '/vm/%s/device/vif/0/frontend-id' % (uuid))
+            if not domid:
+                xsc.close()
+                return None
+            for vifnum in xsc.ls('', '/local/domain/0/backend/vif/%s' % (domid)):
+                if xsc.read('', '/local/domain/0/backend/vif/%s/%s/mac' % (domid, vifnum)) == nic.mac_addr:
+                    # Prefer the tap if it exists; paravirtualized HVMs will
+                    # have already unplugged it, so if it's there, it's the one
+                    # in use.
+                    for viftype in ('tap', 'vif'):
+                        vif = '%s%s.%s' % (viftype, domid, vifnum)
+                        if vif in ni.interfaces():
+                            self.add_route_and_arp(nic.ip, vif, nic.gateway)
+                            xsc.close()
+                            return vif
+            xsc.close()
+        except Exception as e:
+            try:
+                xsc.close()
+            except Exception as e2:
+                s.syslog(s.LOG_ERR, "Could not close connection to xenstore: %s" % (e2))
+            s.syslog(s.LOG_ERR, "Could not find interface and add missing route: %s" % (e))
         return None
                             
     def getParameters(self, **extra):
@@ -149,7 +195,6 @@ class DhcpBackend:
             s.syslog(s.LOG_INFO,"Ack ip "+str(yiaddr)+" for "+str(chaddr))
             n = self.findNIC(str(chaddr))
             intf = self.find_interface_by_nic(n)
-            main_ip = ni.ifaddresses(config.xen.iface)[ni.AF_INET][0]['addr']
             s.syslog(s.LOG_ERR, "Interface is %s" % (intf))
             # Don't perform "other" actions if the machine isn't running
             other_action = n.other_action if n.other_action and intf else ''
@@ -171,16 +216,7 @@ class DhcpBackend:
                 # IP needs to be added.  Just try adding both of them, and
                 # arp for whichever of them turns out to be new.
                 for parms in [(n.ip, n.gateway), (n.other_ip, n.other_gateway)]:
-                    try:
-                        p = Popen(['ip', 'route', 'add', parms[0], 'dev', intf, 'src', main_ip, 'metric', '2' if intf.startswith('vif') else '1'], stdout=PIPE, stderr=PIPE)
-                        (out, err) = p.communicate()
-                        if p.returncode == 0:
-                            s.syslog(s.LOG_INFO, "Added route for IP %s to interface %s" % (parms[0], intf))
-                            self.queue.put(parms)
-                        sys.stderr.write(err)
-                        sys.stdout.write(out)
-                    except Exception as e:
-                        s.syslog(s.LOG_ERR, "Could not add route for IP %s: %s" % (parms[0], e))
+                    self.add_route_and_arp(parms[0], intf, parms[1])
                 try:
                     # iptables will let you add the same rule again and again;
                     # let's not do that.