Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2014 Linaro Limited |
| 4 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation; either version 2 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program; if not, write to the Free Software |
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | # MA 02110-1301, USA. |
| 19 | # |
| 20 | # Main VLANd module |
| 21 | # |
| 22 | |
Steve McIntyre | 1cb0b48 | 2014-12-22 17:18:11 +0000 | [diff] [blame] | 23 | import os, sys |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 24 | import time |
| 25 | import logging |
| 26 | |
| 27 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 28 | sys.path.insert(0, vlandpath) |
| 29 | |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 30 | from config.config import VlanConfig |
| 31 | from db.db import VlanDB |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 32 | from ipc.ipc import VlanIpc |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 33 | from errors import InputError, SocketError |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 34 | from util import VlanUtil |
Steve McIntyre | 71aa0ac | 2015-09-23 18:34:02 +0100 | [diff] [blame^] | 35 | from visualisation.visualisation import Visualisation |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 36 | |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 37 | class DaemonState: |
| 38 | """ Simple container for stuff to make for nicer syntax """ |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 39 | |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 40 | state = DaemonState() |
Steve McIntyre | 77fd71c | 2015-07-31 17:16:01 +0100 | [diff] [blame] | 41 | state.version = "0.4-DEV" |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 42 | state.banner = "Linaro VLANd version %s" % state.version |
| 43 | state.starttime = time.time() |
Steve McIntyre | 71aa0ac | 2015-09-23 18:34:02 +0100 | [diff] [blame^] | 44 | state.vis = None |
| 45 | |
| 46 | os.environ['TZ'] = 'UTC' |
| 47 | time.tzset() |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 48 | |
| 49 | print '%s' % state.banner |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 50 | |
| 51 | print 'Parsing Config...' |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 52 | state.config = VlanConfig(filenames=('./vland.cfg',)) |
| 53 | print ' Config knows about %d switches' % len(state.config.switches) |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 54 | |
Steve McIntyre | 71aa0ac | 2015-09-23 18:34:02 +0100 | [diff] [blame^] | 55 | if state.config.visualisation.enabled: |
| 56 | state.vis = Visualisation(state) |
| 57 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 58 | util = VlanUtil() |
| 59 | |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 60 | print 'Connecting to DB...' |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 61 | state.db = VlanDB(db_name=state.config.database.dbname, |
| 62 | username=state.config.database.username) |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 63 | |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 64 | switches = state.db.all_switches() |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 65 | print ' DB knows about %d switches' % len(switches) |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 66 | ports = state.db.all_ports() |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 67 | print ' DB knows about %d ports' % len(ports) |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 68 | vlans = state.db.all_vlans() |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 69 | print ' DB knows about %d vlans' % len(vlans) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 70 | trunks = state.db.all_trunks() |
| 71 | print ' DB knows about %d trunks' % len(trunks) |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 72 | |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 73 | # Initial startup sanity chacking |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 74 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 75 | # For sanity, we need to know the vlan_id for the default vlan (tag |
| 76 | # 1). Make sure we know that before anybody attempts to create things |
| 77 | # that depend on it. |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 78 | state.default_vlan_id = state.db.get_vlan_id_by_tag(state.config.vland.default_vlan_tag) |
| 79 | if state.default_vlan_id is None: |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 80 | # It doesn't exist - create it and try again |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 81 | state.default_vlan_id = state.db.create_vlan("DEFAULT", |
| 82 | state.config.vland.default_vlan_tag, |
| 83 | True) |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 84 | |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 85 | if len(switches) != len(state.config.switches): |
| 86 | print 'You have configured access details for %d switch(es), ' % len(state.config.switches) |
Steve McIntyre | 96855db | 2014-12-15 16:52:00 +0000 | [diff] [blame] | 87 | print 'but have %d switch(es) registered in your database.' % len(switches) |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 88 | print 'You must fix this difference for VLANd to work sensibly.' |
Steve McIntyre | 8c84fb3 | 2015-04-29 17:39:31 +0100 | [diff] [blame] | 89 | print 'HINT: Running admin.py --auto_import_switch <switch_name>' |
Steve McIntyre | bd7ce37 | 2014-12-17 16:27:41 +0000 | [diff] [blame] | 90 | print 'for each of your switches may help!' |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 91 | print |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 92 | |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 93 | # Set up logging and maybe go quiet... |
| 94 | if state.config.logging.level is None: |
| 95 | state.config.logging.level = "CRITICAL" |
Steve McIntyre | 504ba60 | 2015-02-12 07:51:28 +0000 | [diff] [blame] | 96 | |
| 97 | loglevel = logging.CRITICAL |
| 98 | if state.config.logging.level == "ERROR": |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 99 | loglevel = logging.ERROR |
| 100 | elif state.config.logging.level == "WARNING": |
| 101 | loglevel = logging.WARNING |
| 102 | elif state.config.logging.level == "INFO": |
| 103 | loglevel = logging.INFO |
| 104 | elif state.config.logging.level == "DEBUG": |
| 105 | loglevel = logging.DEBUG |
| 106 | |
Steve McIntyre | 2703983 | 2015-07-13 17:49:40 +0100 | [diff] [blame] | 107 | os.environ['TZ'] = 'UTC' |
| 108 | time.tzset() |
| 109 | |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 110 | # Should we log to stderr? |
| 111 | if state.config.logging.filename is None: |
| 112 | logging.basicConfig(level = loglevel, |
| 113 | format = '%(asctime)s %(levelname)-8s %(message)s') |
| 114 | else: |
Steve McIntyre | 7918f74 | 2015-02-12 08:07:23 +0000 | [diff] [blame] | 115 | print "Logging to %s" % state.config.logging.filename |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 116 | logging.basicConfig(level = loglevel, |
| 117 | format = '%(asctime)s %(levelname)-8s %(message)s', |
Steve McIntyre | 322b0ff | 2015-02-12 08:11:40 +0000 | [diff] [blame] | 118 | datefmt = '%Y-%m-%d %H:%M:%S %Z', |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 119 | filename = state.config.logging.filename, |
| 120 | filemode = 'a') |
Steve McIntyre | c5fc28b | 2015-07-13 17:50:10 +0100 | [diff] [blame] | 121 | logging.info('%s starting up', state.banner) |
| 122 | switches = state.db.all_switches() |
| 123 | logging.info(' DB knows about %d switches', len(switches)) |
| 124 | ports = state.db.all_ports() |
| 125 | logging.info(' DB knows about %d ports', len(ports)) |
| 126 | vlans = state.db.all_vlans() |
| 127 | logging.info(' DB knows about %d vlans', len(vlans)) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 128 | trunks = state.db.all_trunks() |
| 129 | logging.info(' DB knows about %d trunks', len(trunks)) |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 130 | |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 131 | # Now start up the core loop. Listen for command connections and |
| 132 | # process them |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 133 | state.running = True |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 134 | ipc = VlanIpc() |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 135 | ipc.server_init('localhost', state.config.vland.port) |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 136 | while state.running: |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 137 | try: |
| 138 | ipc.server_listen() |
| 139 | json_data = ipc.server_recv() |
| 140 | except SocketError as e: |
| 141 | print e |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 142 | logging.debug('Caught IPC error, ignoring') |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 143 | continue |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 144 | except: |
| 145 | ipc.server_close() |
| 146 | raise |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 147 | |
Steve McIntyre | 5fa2265 | 2015-04-01 18:01:45 +0100 | [diff] [blame] | 148 | logging.debug("client %s sent us:", json_data['client_name']) |
Steve McIntyre | eb0a4b8 | 2015-02-12 07:06:56 +0000 | [diff] [blame] | 149 | logging.debug(json_data) |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 150 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 151 | response = {} |
| 152 | |
Steve McIntyre | 0ebf583 | 2014-12-16 19:23:10 +0000 | [diff] [blame] | 153 | # Several types of IPC message here, with potentially different |
| 154 | # access control and safety |
| 155 | |
| 156 | # First - simple queries to the database only. Should be safe! |
| 157 | if json_data['type'] == 'db_query': |
| 158 | response['type'] = 'response' |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 159 | try: |
Steve McIntyre | 208241a | 2014-12-17 13:12:26 +0000 | [diff] [blame] | 160 | response['data'] = util.perform_db_query(state, json_data['command'], json_data['data']) |
Steve McIntyre | 0ebf583 | 2014-12-16 19:23:10 +0000 | [diff] [blame] | 161 | response['response'] = 'ACK' |
| 162 | except InputError as e: |
| 163 | print e |
| 164 | response['response'] = 'ERROR' |
| 165 | response['error'] = e.__str__() |
Steve McIntyre | 0ebf583 | 2014-12-16 19:23:10 +0000 | [diff] [blame] | 166 | |
| 167 | # Next - simple queries about daemon state only. Should be safe! |
| 168 | if json_data['type'] == 'daemon_query': |
| 169 | response['type'] = 'response' |
| 170 | try: |
| 171 | response['data'] = util.perform_daemon_query(state, json_data['command'], json_data['data']) |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 172 | response['response'] = 'ACK' |
| 173 | except InputError as e: |
| 174 | print e |
| 175 | response['response'] = 'ERROR' |
| 176 | response['error'] = e.__str__() |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 177 | |
Steve McIntyre | 995a10c | 2014-12-17 13:12:58 +0000 | [diff] [blame] | 178 | # Next, calls that manipulate objects in the database only |
| 179 | # (switches and ports). These are safe and don't need actual |
Steve McIntyre | c3b659b | 2014-12-17 16:28:37 +0000 | [diff] [blame] | 180 | # co-ordinating with hardware directly. |
Steve McIntyre | 995a10c | 2014-12-17 13:12:58 +0000 | [diff] [blame] | 181 | # |
| 182 | # As/when/if we add authentication, use of this function will need |
| 183 | # it. |
| 184 | if json_data['type'] == 'db_update': |
| 185 | response['type'] = 'response' |
| 186 | try: |
| 187 | response['data'] = util.perform_db_update(state, json_data['command'], json_data['data']) |
| 188 | response['response'] = 'ACK' |
| 189 | except InputError as e: |
| 190 | print e |
| 191 | response['response'] = 'ERROR' |
| 192 | response['error'] = e.__str__() |
Steve McIntyre | 995a10c | 2014-12-17 13:12:58 +0000 | [diff] [blame] | 193 | |
Steve McIntyre | 5076715 | 2014-12-17 16:29:01 +0000 | [diff] [blame] | 194 | # Next, calls that may manipulate switch state *as well* as state |
| 195 | # in the database - changes to VLAN setup. |
| 196 | # |
| 197 | # As/when/if we add authentication, use of this function will need |
| 198 | # it. |
| 199 | if json_data['type'] == 'vlan_update': |
| 200 | response['type'] = 'response' |
| 201 | try: |
| 202 | response['data'] = util.perform_vlan_update(state, json_data['command'], json_data['data']) |
| 203 | response['response'] = 'ACK' |
| 204 | except InputError as e: |
| 205 | print e |
| 206 | response['response'] = 'ERROR' |
| 207 | response['error'] = e.__str__() |
Steve McIntyre | 5076715 | 2014-12-17 16:29:01 +0000 | [diff] [blame] | 208 | |
| 209 | # Finally, IPC interface for more complex API calls. |
| 210 | # NOT IMPLEMENTED YET |
| 211 | if json_data['type'] == 'vland_api': |
| 212 | response['type'] = 'response' |
| 213 | response['response'] = 'ERROR' |
| 214 | response['error'] = 'VLANd API not yet implemented...' |
| 215 | |
Steve McIntyre | 7cf8098 | 2015-02-12 07:03:40 +0000 | [diff] [blame] | 216 | logging.debug("sending reply:") |
Steve McIntyre | 72f1a5c | 2015-02-12 07:05:39 +0000 | [diff] [blame] | 217 | logging.debug(response) |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 218 | |
| 219 | ipc.server_reply(response) |
| 220 | |
Steve McIntyre | 06fe642 | 2015-01-23 17:55:43 +0000 | [diff] [blame] | 221 | # We've been asked to shut down. Do that as cleanly as we can |
| 222 | ipc.server_close() |
Steve McIntyre | c5fc28b | 2015-07-13 17:50:10 +0100 | [diff] [blame] | 223 | |
Steve McIntyre | 71aa0ac | 2015-09-23 18:34:02 +0100 | [diff] [blame^] | 224 | if state.config.visualisation.enabled: |
| 225 | state.vis.shutdown() |
| 226 | |
Steve McIntyre | c5fc28b | 2015-07-13 17:50:10 +0100 | [diff] [blame] | 227 | logging.info('%s shutting down', state.banner) |
| 228 | switches = state.db.all_switches() |
| 229 | logging.info(' DB knows about %d switches', len(switches)) |
| 230 | ports = state.db.all_ports() |
| 231 | logging.info(' DB knows about %d ports', len(ports)) |
| 232 | vlans = state.db.all_vlans() |
| 233 | logging.info(' DB knows about %d vlans', len(vlans)) |
Steve McIntyre | c489013 | 2015-08-07 15:19:11 +0100 | [diff] [blame] | 234 | trunks = state.db.all_trunks() |
| 235 | logging.info(' DB knows about %d trunks', len(trunks)) |
Steve McIntyre | c5fc28b | 2015-07-13 17:50:10 +0100 | [diff] [blame] | 236 | |
Steve McIntyre | dbe6aa5 | 2015-02-12 07:48:22 +0000 | [diff] [blame] | 237 | logging.shutdown() |