1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
import string
from netmodel.model.type import Integer
from vicn.core.attribute import Attribute
from vicn.core.exception import ResourceNotFound
from vicn.core.resource import Resource
from vicn.core.state import ResourceState, AttributeState
from vicn.core.task import BashTask, get_attributes_task
from vicn.core.task import async_task, task, inline_task
from vicn.core.task import run_task
from vicn.resource.interface import Interface
from vicn.resource.node import Node
from vicn.resource.linux.net_device import NonTapBaseNetDevice
from vicn.resource.linux.link import CMD_DELETE_IF_EXISTS
from vicn.resource.linux.link import CMD_UP
CMD_CREATE='''
# Create veth pair in the host node
ip link add name {tmp_side1} type veth peer name {tmp_side2}
ip link set dev {tmp_side1} netns {pid[0]} name {side1_device_name}
ip link set dev {tmp_side2} netns {pid[1]} name {side2_device_name}
'''
class SymVethPair(Resource):
"""
Resource: SymVethPair
This resource is used in VPPBridge. The main difference with the Link
resource is that is it not a channel.
"""
node1 = Attribute(Node,
description = 'Node on which one side of the veth will sit',
mandatory = True)
node2 = Attribute(Node,
description = 'Node on which the other side of the veth will sit',
mandatory = True)
capacity = Attribute(Integer,
description = 'Capacity of the veth pair (Mb/s)')
side1 = Attribute(Interface, description = 'Source interface')
side2 = Attribute(Interface, description = 'Destination interface')
#--------------------------------------------------------------------------
# Internal methods
#--------------------------------------------------------------------------
async def _commit(self):
# see link.py for explanations
manager = self._state.manager
await manager._set_resource_state(self.side1,
ResourceState.INITIALIZED)
await manager._set_resource_state(self.side2,
ResourceState.INITIALIZED)
await manager._set_resource_state(self.side1, ResourceState.CREATED)
await manager._set_resource_state(self.side2, ResourceState.CREATED)
await manager._set_attribute_state(self, 'side1', AttributeState.CLEAN)
await manager._set_attribute_state(self, 'side2', AttributeState.CLEAN)
manager.commit_resource(self.side1)
manager.commit_resource(self.side2)
#--------------------------------------------------------------------------
# Resource lifecycle
#--------------------------------------------------------------------------
@inline_task
def __initialize__(self):
self.side1 = NonTapBaseNetDevice(node = self.node1,
device_name = self.node2.name,
capacity = self.capacity,
owner = self.owner)
self.side2 = NonTapBaseNetDevice(node = self.node2,
device_name = self.node1.name,
capacity = self.capacity,
owner = self.owner)
self.side1.remote = self.side2
self.side2.remote = self.side1
@async_task
async def __get__(self):
manager = self._state.manager
try:
await run_task(self.side1.__get__(), manager)
await run_task(self.side2.__get__(), manager)
except ResourceNotFound:
raise ResourceNotFound
await self._commit()
def __create__(self):
assert self.node1.get_type() == 'lxccontainer'
assert self.node2.get_type() == 'lxccontainer'
node1_host = self.node1.node
node2_host = self.node2.node
assert node1_host == node2_host
host = node1_host
# Sometimes a down interface persists on one side
delif_side1 = BashTask(self.node1, CMD_DELETE_IF_EXISTS,
{'interface': self.side1})
delif_side2 = BashTask(self.node2, CMD_DELETE_IF_EXISTS,
{'interface': self.side2})
pid_node1 = get_attributes_task(self.node1, ['pid'])
pid_node2 = get_attributes_task(self.node2, ['pid'])
tmp_side1 = 'tmp-veth-' + ''.join(random.choice(
string.ascii_uppercase + string.digits) for _ in range(5))
tmp_side2 = 'tmp-veth-' + ''.join(random.choice(
string.ascii_uppercase + string.digits) for _ in range(5))
create = BashTask(host, CMD_CREATE,
{'side1_device_name': self.side1.device_name,
'side2_device_name': self.side2.device_name,
'tmp_side1': tmp_side1, 'tmp_side2': tmp_side2})
up_side1 = BashTask(self.node1, CMD_UP, {'interface': self.side1})
up_side2 = BashTask(self.node2, CMD_UP, {'interface': self.side2})
@async_task
async def set_state():
await self._commit()
delif = delif_side1 | delif_side2
up = up_side1 | up_side2
pid = pid_node1 | pid_node2
return ((delif > (pid @ create)) > up) > set_state()
def __delete__(self):
raise NotImplementedError
|