Merge "mrg_buff_doc: Add documentation on mergable buffer option"
[vswitchperf.git] / tools / veth.py
1 # Copyright 2016 Red Hat Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 veth port emulation
17 """
18
19 import logging
20 import os
21
22 from tools import tasks
23
24 _LOGGER = logging.getLogger(__name__)
25
26
27 def add_veth_port(port, peer_port):
28     """
29     Add a veth port
30     :param port:port name for the first port
31     :param peer_port: port name for the peer port
32     :return: None
33     """
34     # touch some files in a tmp area so we can track them. This allows us to
35     # track VSPerf created veth ports so they can be cleaned up if needed.
36     if not os.path.isdir('/tmp/veth'):
37         try:
38             os.mkdir('/tmp/veth')
39         except os.error:
40             # OK don't crash but cleanup may be an issue
41             _LOGGER.error('Unable to create veth temp folder.')
42             _LOGGER.error(
43                 'Veth ports may not be removed on testcase completion')
44     if os.path.isdir('/tmp/veth'):
45         with open('/tmp/veth/{}-{}'.format(port, peer_port), 'a'):
46             os.utime('/tmp/veth/{}-{}'.format(port, peer_port), None)
47     tasks.run_task(['sudo', 'ip', 'link', 'add',
48                     port, 'type', 'veth', 'peer', 'name', peer_port],
49                    _LOGGER, 'Adding veth port {} with peer port {}...'.format(
50                        port, peer_port), False)
51
52
53 def bring_up_eth_port(eth_port, namespace=None):
54     """
55     Bring up an eth port
56     :param eth_port: string of eth port to bring up
57     :param namespace: Namespace eth port it located if needed
58     :return: None
59     """
60     if namespace:
61         tasks.run_task(['sudo', 'ip', 'netns', 'exec', namespace,
62                         'ip', 'link', 'set', eth_port, 'up'],
63                        _LOGGER,
64                        'Bringing up port {} in namespace {}...'.format(
65                            eth_port, namespace), False)
66     else:
67         tasks.run_task(['sudo', 'ip', 'link', 'set', eth_port, 'up'],
68                        _LOGGER, 'Bringing up port...', False)
69
70
71 def del_veth_port(port, peer_port):
72     """
73     Delete the veth ports, the peer will automatically be deleted on deletion
74     of the first port param
75     :param port: port name to delete
76     :param port: peer port name
77     :return: None
78     """
79     # delete the file if it exists in the temp area
80     if os.path.exists('/tmp/veth/{}-{}'.format(port, peer_port)):
81         os.remove('/tmp/veth/{}-{}'.format(port, peer_port))
82     tasks.run_task(['sudo', 'ip', 'link', 'del', port],
83                    _LOGGER, 'Deleting veth port {} with peer {}...'.format(
84                        port, peer_port), False)
85
86
87 # pylint: disable=unused-argument
88 def validate_add_veth_port(result, port, peer_port):
89     """
90     Validation function for integration testcases
91     """
92     devs = os.listdir('/sys/class/net')
93     return all([port in devs, peer_port in devs])
94
95
96 def validate_bring_up_eth_port(result, eth_port, namespace=None):
97     """
98     Validation function for integration testcases
99     """
100     command = list()
101     if namespace:
102         command += ['ip', 'netns', 'exec', namespace]
103     command += ['cat', '/sys/class/net/{}/operstate'.format(eth_port)]
104     out = tasks.run_task(command, _LOGGER, 'Validating port up...', False)
105
106     # since different types of ports may report different status the best way
107     # we can do this for now is to just make sure it doesn't say down
108     if 'down' in out:
109         return False
110     return True
111
112
113 def validate_del_veth_port(result, port, peer_port):
114     """
115     Validation function for integration testcases
116     """
117     devs = os.listdir('/sys/class/net')
118     return not any([port in devs, peer_port in devs])