src: update make install for DPDK
[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 = settings.getValue('OVS_VAR_DIR')
35 _OVS_ETC_DIR = settings.getValue('OVS_ETC_DIR')
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     _ovsdb_pidfile_path = os.path.join(settings.getValue('LOG_DIR'), "ovsdb_pidfile.pid")
48     _proc_name = 'ovs-vswitchd'
49
50     def __init__(self, timeout=30, vswitchd_args=None, expected_cmd=None):
51         """Initialise the wrapper with a specific start timeout and extra
52         parameters.
53
54         :param timeout: Timeout to wait for application to start.
55         :param vswitchd_args: Command line parameters for vswitchd.
56
57         :returns: None
58         """
59         self._logger = logging.getLogger(__name__)
60         self._timeout = timeout
61         self._expect = expected_cmd
62         vswitchd_args = vswitchd_args or []
63         self._cmd = ['sudo', '-E', _OVS_VSWITCHD_BIN] + vswitchd_args
64
65     # startup/shutdown
66
67     def start(self):
68         """ Start ``ovsdb-server`` and ``ovs-vswitchd`` instance.
69
70         :returns: None
71         :raises: pexpect.EOF, pexpect.TIMEOUT
72         """
73
74         self._reset_ovsdb()
75         self._start_ovsdb()  # this has to be started first
76
77         try:
78             super(VSwitchd, self).start()
79             self.relinquish()
80         except (pexpect.EOF, pexpect.TIMEOUT) as exc:
81             logging.error("Exception during VSwitch start.")
82             self._kill_ovsdb()
83             raise exc
84
85     def kill(self, signal='-15', sleep=2):
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(signal, sleep)
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         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              '--pidfile=' + self._ovsdb_pidfile_path, '--overwrite-pidfile'],
133             self._logger,
134             'Starting ovsdb-server...')
135
136     def _kill_ovsdb(self):
137         """Kill ``ovsdb-server`` instance.
138
139         :returns: None
140         """
141         with open(self._ovsdb_pidfile_path, "r") as pidfile:
142             ovsdb_pid = pidfile.read().strip()
143
144         self._logger.info("Killing ovsdb with pid: " + ovsdb_pid)
145
146         if ovsdb_pid:
147             tasks.run_task(['sudo', 'kill', '-15', str(ovsdb_pid)],
148                            self._logger, 'Killing ovsdb-server...')
149
150     @staticmethod
151     def get_db_sock_path():
152         """Method returns location of db.sock file
153
154         :returns: path to db.sock file.
155         """
156         return os.path.join(_OVS_VAR_DIR, 'db.sock')