aboutsummaryrefslogtreecommitdiffstats
path: root/vicn/resource/linux/veth_pair.py
diff options
context:
space:
mode:
Diffstat (limited to 'vicn/resource/linux/veth_pair.py')
-rw-r--r--vicn/resource/linux/veth_pair.py94
1 files changed, 64 insertions, 30 deletions
diff --git a/vicn/resource/linux/veth_pair.py b/vicn/resource/linux/veth_pair.py
index 53fa9bf8..52050074 100644
--- a/vicn/resource/linux/veth_pair.py
+++ b/vicn/resource/linux/veth_pair.py
@@ -15,48 +15,82 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-import random
-import string
-from vicn.resource.linux.net_device import SlaveBaseNetDevice
-from vicn.core.task import BashTask, get_attributes_task
-
-# ip link add veth0 type veth peer name veth1
+from netmodel.model.key import Key
+from netmodel.model.type import String, Bool
+from vicn.core.attribute import Attribute, Reference
+from vicn.core.task import BashTask, inline_task, get_attributes_task
+from vicn.resource.linux.net_device import NetDevice, NetDeviceName
+from vicn.resource.node import Node
+from vicn.resource.symmetric_channel import SymmetricChannel
CMD_CREATE='''
# Create veth pair in the host node
-ip link add name {interface.host.device_name} type veth peer name {tmp_name}
+ip link add name {self.device_name} type veth peer name {self.peer_device_name}
+'''
+
+DEPRECATED_CMD_UP='''
# The host interface will always be up...
ip link set dev {interface.host.device_name} up
+
# Move interface into container and rename it
ip link set dev {tmp_name} netns {pid} name {interface.device_name}
-'''
-CMD_UP='''
ip link set dev {interface.device_name} up
'''
-# see:
-# http://stackoverflow.com/questions/22780927/lxc-linux-containers-add-new-network-interface-without-restarting
+# Forward declaration
+class VethPair(SymmetricChannel):
+ pass
+
+class VethNetDevice(NetDevice):
+ parent = Attribute(VethPair, mandatory = True, ro = True)
-class VethPair(SlaveBaseNetDevice):
- # Do not need the parent attribute...
+ __get__ = None
+ __create__ = None
+ __delete__ = None
- def __init__(self, *args, **kwargs):
- super().__init__(self, *args, **kwargs)
- self.prefix = 'veth'
- self.netdevice_type = 'veth'
+class VethPair(SymmetricChannel):
+ # Mimics NetDevice for using its __get__ and __delete__ functions
+ node = Attribute(Node)
+ device_name = Attribute(NetDeviceName)
+ peer_device_name = Attribute(NetDeviceName)
+ capacity = Attribute(String)
+ src = Attribute(ro = True, mandatory = False)
+ dst = Attribute(ro = True, mandatory = False)
+ auto_commit = Attribute(Bool, description = 'Auto commit interfaces')
+
+ __key1__ = Key(node, device_name)
+ __key2__ = Key(node, peer_device_name)
+
+ @inline_task
+ def _commit(self):
+ if self.auto_commit:
+ manager = self._state.manager
+
+ manager.commit_resource(self.src)
+ manager.commit_resource(self.dst)
+
+ def __initialize__(self):
+ # XXX owner prevents the resource to be committed
+ self.src = VethNetDevice(node = self.node,
+ parent = self,
+ device_name = self.device_name,
+ channel = self,
+ capacity = Reference(self, 'capacity'),
+ owner = self)
+ self.dst = VethNetDevice(node = self.node,
+ parent = self,
+ device_name = self.peer_device_name,
+ channel = self,
+ capacity = Reference(self, 'capacity'),
+ owner = self)
def __create__(self):
- assert self.node.__class__.__name__ == 'LxcContainer'
- host = self.node.node
- pid = get_attributes_task(self.node, ['pid'])
- tmp_name = 'tmp-veth-' + ''.join(random.choice(string.ascii_uppercase \
- + string.digits) for _ in range(5))
- create = BashTask(host, CMD_CREATE, {'tmp_name': tmp_name,
- 'interface': self})
- up = BashTask(self.node, CMD_UP, {'interface': self})
- bridge = host.bridge_manager.add_interface(host.bridge.device_name,
- self.host.device_name)
- return ((pid @ create) > up) > bridge
-
- # ... IP and UP missing...
+ veth = BashTask(self.node, CMD_CREATE, {'self': self})
+ return (veth > super().__create__()) > self._commit()
+
+ def __get__(self):
+ return NetDevice.__get__(self) > self._commit()
+
+ def __delete__(self):
+ return NetDevice.__delete__(self)