From 6aa8ca091b8a18f75d9256662a8df9b9875066d0 Mon Sep 17 00:00:00 2001 From: Xavi Leon Date: Fri, 2 Dec 2011 15:47:02 -0500 Subject: [PATCH] Refactor code. Put ebtables stuff at sliver creation time (start/stop) on sliver_libvirt.py --- bwlimit.py | 14 ++------------ sliver_libvirt.py | 14 +++++++++++++- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/bwlimit.py b/bwlimit.py index 7ac3def..e310762 100644 --- a/bwlimit.py +++ b/bwlimit.py @@ -594,18 +594,8 @@ def on(xid, dev = dev, share = None, minrate = None, maxrate = None, minexemptra # Setup a filter rule to the root class so each packet originated by a # container interface is classified to it corresponding class # The handle number is a mark created by ebtables with the xid - tc("filter replace dev %s parent 1:1 protocol ip prio 1 handle %d fw flowid 1:%x" % \ - (dev, default_minor | xid, default_minor | xid)) - - # Create the ebtables rule to mark the packets going out from the virtual - # interface to the actual device so the filter canmatch against the mark - # We remove and readd the rule because this method is called each time the - # bandwidth limit is changed - ebtables("-D INPUT -i veth%d -j mark --set-mark %d" % \ - (xid, default_minor | xid)) - ebtables("-A INPUT -i veth%d -j mark --set-mark %d" % \ - (xid, default_minor | xid)) - + tc("filter replace dev %s parent 1: protocol ip prio 1 handle %d fw flowid 1:%x" % \ + (dev, xid, default_minor | xid)) def set(xid, share = None, minrate = None, maxrate = None, minexemptrate = None, maxexemptrate = None, dev = dev ): on(xid = xid, dev = dev, share = share, diff --git a/sliver_libvirt.py b/sliver_libvirt.py index 83b5ad8..990695e 100644 --- a/sliver_libvirt.py +++ b/sliver_libvirt.py @@ -10,6 +10,7 @@ import os.path import libvirt import sys import shutil +import bwlimit from string import Template @@ -58,6 +59,7 @@ class Sliver_Libvirt(accounts.Account): self.slice_id = rec['slice_id'] self.enabled = True self.conn = getConnection(rec['type']) + self.xid = bwlimit.get_xid(self.name) try: self.dom = self.conn.lookupByName(self.name) @@ -74,12 +76,22 @@ class Sliver_Libvirt(accounts.Account): if not self.is_running(): self.dom.create() else: - logger.verbose('sliver_libvirt: sliver %s already started'%(dom.name())) + logger.verbose('sliver_libvirt: sliver %s already started'%(self.name)) + + # After the VM is started... we can play with the virtual interface + # Create the ebtables rule to mark the packets going out from the virtual + # interface to the actual device so the filter canmatch against the mark + bwlimit.ebtables("-A INPUT -i veth%d -j mark --set-mark %d" % \ + (self.xid, self.xid)) def stop(self): logger.verbose('sliver_libvirt: %s stop'%(self.name)) + # Remove the ebtables rule before stopping + bwlimit.ebtables("-D INPUT -i veth%d -j mark --set-mark %d" % \ + (self.xid, self.xid)) + try: self.dom.destroy() except: -- 2.43.0