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 | |
| 23 | import os, sys, types |
| 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 | |
| 30 | import drivers |
| 31 | from config.config import VlanConfig |
| 32 | from db.db import VlanDB |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 33 | from ipc.ipc import VlanIpc |
| 34 | from errors import CriticalError, InputError, ConfigError, SocketError |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 35 | from util import VlanUtil |
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() |
| 41 | state.version = "0.0.0-DEV" |
| 42 | state.banner = "Linaro VLANd version %s" % state.version |
| 43 | state.starttime = time.time() |
| 44 | |
| 45 | print '%s' % state.banner |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 46 | |
| 47 | print 'Parsing Config...' |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 48 | state.config = VlanConfig(filenames=('./vland.cfg',)) |
| 49 | print ' Config knows about %d switches' % len(state.config.switches) |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 50 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 51 | util = VlanUtil() |
| 52 | |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 53 | print 'Connecting to DB...' |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 54 | state.db = VlanDB(db_name=state.config.database.dbname, |
| 55 | username=state.config.database.username) |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 56 | |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 57 | switches = state.db.all_switches() |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 58 | print ' DB knows about %d switches' % len(switches) |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 59 | ports = state.db.all_ports() |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 60 | print ' DB knows about %d ports' % len(ports) |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 61 | vlans = state.db.all_vlans() |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 62 | print ' DB knows about %d vlans' % len(vlans) |
| 63 | |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 64 | # Initial startup sanity chacking |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 65 | |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 66 | # For sanity, we need to know the vlan_id for the default vlan (tag |
| 67 | # 1). Make sure we know that before anybody attempts to create things |
| 68 | # that depend on it. |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 69 | state.default_vlan_id = state.db.get_vlan_id_by_tag(state.config.vland.default_vlan_tag) |
| 70 | if state.default_vlan_id is None: |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 71 | # It doesn't exist - create it and try again |
Steve McIntyre | 283d5f2 | 2014-12-17 13:12:04 +0000 | [diff] [blame] | 72 | state.default_vlan_id = state.db.create_vlan("DEFAULT", |
| 73 | state.config.vland.default_vlan_tag, |
| 74 | True) |
Steve McIntyre | c12f631 | 2014-12-15 15:00:12 +0000 | [diff] [blame] | 75 | |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 76 | if len(switches) != len(state.config.switches): |
| 77 | 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] | 78 | print 'but have %d switch(es) registered in your database.' % len(switches) |
Steve McIntyre | ae28619 | 2014-12-12 22:10:43 +0000 | [diff] [blame] | 79 | print 'You must fix this difference for VLANd to work sensibly.' |
| 80 | print 'HINT: Running admin.py --auto-import-switch <switch_name>' |
| 81 | print 'for each of your switches will probably be useful!' |
| 82 | print |
Steve McIntyre | e5043dd | 2014-12-10 16:49:28 +0000 | [diff] [blame] | 83 | |
Steve McIntyre | 3b33097 | 2014-12-12 22:08:29 +0000 | [diff] [blame] | 84 | #for switch in sorted(config.switches): |
| 85 | # print "Found switch %s:" % (switch) |
| 86 | # |
| 87 | # print " Probing:" |
| 88 | # |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 89 | # s = util.get_switch_driver(switch, config) |
Steve McIntyre | 3b33097 | 2014-12-12 22:08:29 +0000 | [diff] [blame] | 90 | # s.switch_connect(config.switches[switch].username, config.switches[switch].password) |
| 91 | # print " Found details of switch:" |
| 92 | # s._dump_list(s._systemdata) |
| 93 | # print " Switch has %d ports:" % len(s.switch_get_port_names()) |
| 94 | # for port in s.switch_get_port_names(): |
| 95 | # print " %s" % port |
| 96 | # if 0 == 1: |
| 97 | # mode = s.port_get_mode(port) |
| 98 | # if mode == "trunk": |
| 99 | # print " port %s is in trunk mode, VLAN(s):" % port |
| 100 | # vlans = s.port_get_trunk_vlan_list(port) |
| 101 | # for vlan in vlans: |
| 102 | # name = s.vlan_get_name(vlan) |
| 103 | # print " %d (%s)" % (vlan, name) |
| 104 | # else: |
| 105 | # vlan = s.port_get_access_vlan(port) |
| 106 | # name = s.vlan_get_name(vlan) |
| 107 | # print " port %s is in access mode, VLAN %d (%s):" % (port, vlan, name) |
| 108 | # |
| 109 | # s.switch_disconnect() |
| 110 | # del(s) |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 111 | |
| 112 | # Now start up the core loop. Listen for command connections and |
| 113 | # process them |
| 114 | ipc = VlanIpc() |
Steve McIntyre | 231354b | 2014-12-16 19:22:12 +0000 | [diff] [blame] | 115 | ipc.server_init('localhost', state.config.vland.port) |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 116 | while True: |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 117 | try: |
| 118 | ipc.server_listen() |
| 119 | json_data = ipc.server_recv() |
| 120 | except SocketError as e: |
| 121 | print e |
| 122 | print 'Caught IPC error, ignoring' |
| 123 | continue |
| 124 | pass |
| 125 | except: |
| 126 | ipc.server_close() |
| 127 | raise |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 128 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 129 | print "client %s sent us:" % json_data['client_name'] |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 130 | print json_data |
| 131 | |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 132 | response = {} |
| 133 | |
Steve McIntyre | 0ebf583 | 2014-12-16 19:23:10 +0000 | [diff] [blame] | 134 | # Several types of IPC message here, with potentially different |
| 135 | # access control and safety |
| 136 | |
| 137 | # First - simple queries to the database only. Should be safe! |
| 138 | if json_data['type'] == 'db_query': |
| 139 | response['type'] = 'response' |
Steve McIntyre | f1c04f9 | 2014-12-16 18:23:15 +0000 | [diff] [blame] | 140 | try: |
Steve McIntyre | 208241a | 2014-12-17 13:12:26 +0000 | [diff] [blame^] | 141 | 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] | 142 | response['response'] = 'ACK' |
| 143 | except InputError as e: |
| 144 | print e |
| 145 | response['response'] = 'ERROR' |
| 146 | response['error'] = e.__str__() |
| 147 | print 'FOO?' |
| 148 | pass |
| 149 | |
| 150 | # Next - simple queries about daemon state only. Should be safe! |
| 151 | if json_data['type'] == 'daemon_query': |
| 152 | response['type'] = 'response' |
| 153 | try: |
| 154 | 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] | 155 | response['response'] = 'ACK' |
| 156 | except InputError as e: |
| 157 | print e |
| 158 | response['response'] = 'ERROR' |
| 159 | response['error'] = e.__str__() |
| 160 | print 'FOO?' |
| 161 | pass |
| 162 | |
Steve McIntyre | 6c44fb2 | 2014-12-12 22:11:45 +0000 | [diff] [blame] | 163 | print "sending reply:" |
| 164 | print response |
| 165 | |
| 166 | ipc.server_reply(response) |
| 167 | |