test_spec: Add LTD.MemoryBandwidth.RFC2544.0PacketLoss.Scalability
[vswitchperf.git] / src / ovs / daemon.py
1 # Copyright 2015 Intel 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 """Class wrapper for controlling an OVS instance.
16
17 Wraps a pair of ``ovs-vswitchd`` and ``ovsdb-server`` processes.
18 """
19
20 import os
21 import logging
22 import pexpect
23
24 from conf import settings
25 from tools import tasks
26
27 _OVS_VSWITCHD_BIN = os.path.join(
28     settings.getValue('OVS_DIR'), 'vswitchd', 'ovs-vswitchd')
29 _OVSDB_TOOL_BIN = os.path.join(
30     settings.getValue('OVS_DIR'), 'ovsdb', 'ovsdb-tool')
31 _OVSDB_SERVER_BIN = os.path.join(
32     settings.getValue('OVS_DIR'), 'ovsdb', 'ovsdb-server')
33
34 _OVS_VAR_DIR = '/usr/local/var/run/openvswitch/'
35 _OVS_ETC_DIR = '/usr/local/etc/openvswitch/'
36
37 _LOG_FILE_VSWITCHD = os.path.join(
38     settings.getValue('LOG_DIR'), settings.getValue('LOG_FILE_VSWITCHD'))
39
40 class VSwitchd(tasks.Process):
41     """Class wrapper for controlling an OVS instance.
42
43     Wraps a pair of ``ovs-vswitchd`` and ``ovsdb-server`` processes.
44     """
45     _ovsdb_pid = None
46     _logfile = _LOG_FILE_VSWITCHD
47
48
49     _expect = r'EAL: Master l*core \d+ is ready'
50     _proc_name = 'ovs-vswitchd'
51
52     def __init__(self, timeout=30, vswitchd_args=None):
53         """Initialise the wrapper with a specific start timeout and extra
54         parameters.
55
56         :param timeout: Timeout to wait for application to start.
57         :param vswitchd_args: Command line parameters for vswitchd.
58
59         :returns: None
60         """
61         self._logger = logging.getLogger(__name__)
62         self._timeout = timeout
63         vswitchd_args = vswitchd_args or []
64
65         self._cmd = ['sudo', '-E', _OVS_VSWITCHD_BIN] + vswitchd_args
66
67     # startup/shutdown
68
69     def start(self):
70         """ Start ``ovsdb-server`` and ``ovs-vswitchd`` instance.
71
72         :returns: None
73         :raises: pexpect.EOF, pexpect.TIMEOUT
74         """
75         self._reset_ovsdb()
76         self._start_ovsdb()  # this has to be started first
77
78         try:
79             super(VSwitchd, self).start()
80             self.relinquish()
81         except (pexpect.EOF, pexpect.TIMEOUT) as exc:
82             self._kill_ovsdb()
83             raise exc
84
85     def kill(self):
86         """Kill ``ovs-vswitchd`` instance if it is alive.
87
88         :returns: None
89         """
90         self._logger.info('Killing ovs-vswitchd...')
91
92         self._kill_ovsdb()
93
94         super(VSwitchd, self).kill()
95
96     # helper functions
97
98     def _reset_ovsdb(self):
99         """Reset system for 'ovsdb'.
100
101         :returns: None
102         """
103         self._logger.info('Resetting system after last run...')
104
105         tasks.run_task(['sudo', 'rm', '-rf', _OVS_VAR_DIR], self._logger)
106         tasks.run_task(['sudo', 'mkdir', '-p', _OVS_VAR_DIR], self._logger)
107         tasks.run_task(['sudo', 'rm', '-rf', _OVS_ETC_DIR], self._logger)
108         tasks.run_task(['sudo', 'mkdir', '-p', _OVS_ETC_DIR], self._logger)
109
110         tasks.run_task(['sudo', 'rm', '-f',
111                         os.path.join(_OVS_ETC_DIR, 'conf.db')],
112                        self._logger)
113
114         self._logger.info('System reset after last run.')
115
116     def _start_ovsdb(self):
117         """Start ``ovsdb-server`` instance.
118
119         :returns: None
120         """
121         tasks.run_task(['sudo', _OVSDB_TOOL_BIN, 'create',
122                         os.path.join(_OVS_ETC_DIR, 'conf.db'),
123                         os.path.join(settings.getValue('OVS_DIR'), 'vswitchd',
124                                      'vswitch.ovsschema')],
125                        self._logger,
126                        'Creating ovsdb configuration database...')
127
128         self._ovsdb_pid = tasks.run_background_task(
129             ['sudo', _OVSDB_SERVER_BIN,
130              '--remote=punix:%s' % os.path.join(_OVS_VAR_DIR, 'db.sock'),
131              '--remote=db:Open_vSwitch,Open_vSwitch,manager_options'],
132             self._logger,
133             'Starting ovsdb-server...')
134
135     def _kill_ovsdb(self):
136         """Kill ``ovsdb-server`` instance.
137
138         :returns: None
139         """
140         if self._ovsdb_pid:
141             tasks.run_task(['sudo', 'kill', '-15', str(self._ovsdb_pid)],
142                            self._logger, 'Killing ovsdb-server...')