| 1 |
#! /usr/bin/env python |
| 2 |
""" |
| 3 |
Team daemon example. Implements very basic active backup functionality. |
| 4 |
Note that this application spawns its own team device instead of connecting |
| 5 |
to an existing one. |
| 6 |
|
| 7 |
Copyright (C) 2011-2015 Jiri Pirko <[email protected]> |
| 8 |
|
| 9 |
This library is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 2.1 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
This library is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with this library; if not, write to the Free Software |
| 21 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 |
""" |
| 23 |
|
| 24 |
__author__ = """ |
| 25 |
[email protected] (Jiri Pirko) |
| 26 |
""" |
| 27 |
|
| 28 |
import sys |
| 29 |
import getopt |
| 30 |
import team |
| 31 |
import signal |
| 32 |
|
| 33 |
def usage(): |
| 34 |
""" |
| 35 |
Print usage of this app |
| 36 |
""" |
| 37 |
print "Usage: team_daemon.py [OPTION...]" |
| 38 |
print "" |
| 39 |
print " -h, --help print this message" |
| 40 |
print " -t, --team-name=NAME name of team device" |
| 41 |
print " -p, --port=NETDEV port device (can be defined multiple times)" |
| 42 |
print " -m, --mode=MODENAME name of team mode" |
| 43 |
sys.exit() |
| 44 |
|
| 45 |
class TeamDaemon(object): |
| 46 |
def __init__(self): |
| 47 |
def_team_name = "teamtest0" |
| 48 |
self._team_name = def_team_name |
| 49 |
def_mode_name = "roundrobin" |
| 50 |
self._mode_name = def_mode_name |
| 51 |
self._port_names = [] |
| 52 |
self._old_active_hwaddr = None |
| 53 |
|
| 54 |
self._parse_cmdline() |
| 55 |
|
| 56 |
if self._team_name == def_team_name: |
| 57 |
print "Using default team name \"%s\"" % def_team_name |
| 58 |
|
| 59 |
if self._mode_name == def_mode_name: |
| 60 |
print "Using default mode \"%s\"" % def_mode_name |
| 61 |
|
| 62 |
print "Team name is \"%s\"" % self._team_name |
| 63 |
self._t = team.Team(self._team_name, create=True, destroy=True) |
| 64 |
|
| 65 |
self._t.set_mode_name(self._mode_name) |
| 66 |
for port_name in self._port_names: |
| 67 |
self._t.port_add(port_name) |
| 68 |
|
| 69 |
if self._mode_name == "activebackup": |
| 70 |
self._port_change_handler_ab = team.TeamChangeHandler( |
| 71 |
self._port_change_handler_ab_func, None, |
| 72 |
team.TEAM_PORT_CHANGE) |
| 73 |
self._t.change_handler_register(self._port_change_handler_ab) |
| 74 |
|
| 75 |
self._port_change_handler = team.TeamChangeHandler( |
| 76 |
self._port_change_handler_func, None, |
| 77 |
team.TEAM_PORT_CHANGE) |
| 78 |
self._t.change_handler_register(self._port_change_handler) |
| 79 |
|
| 80 |
self._option_change_handler = team.TeamChangeHandler( |
| 81 |
self._option_change_handler_func, None, |
| 82 |
team.TEAM_OPTION_CHANGE) |
| 83 |
self._t.change_handler_register(self._option_change_handler) |
| 84 |
|
| 85 |
def _parse_cmdline(self): |
| 86 |
try: |
| 87 |
opts, args = getopt.getopt( |
| 88 |
sys.argv[1:], |
| 89 |
"ht:p:m:", |
| 90 |
["help", "team-name=", "port=", "mode="] |
| 91 |
) |
| 92 |
except getopt.GetoptError, err: |
| 93 |
print str(err) |
| 94 |
usage() |
| 95 |
|
| 96 |
for opt, arg in opts: |
| 97 |
if opt in ("-h", "--help"): |
| 98 |
usage() |
| 99 |
elif opt in ("-t", "--team-name"): |
| 100 |
self._team_name = arg |
| 101 |
elif opt in ("-p", "--port"): |
| 102 |
self._port_names.append(arg) |
| 103 |
elif opt in ("-m", "--mode"): |
| 104 |
self._mode_name = arg |
| 105 |
|
| 106 |
def _port_change_handler_func(self, unused): |
| 107 |
print "------------------\nport change\n\tport list:" |
| 108 |
for port in self._t.port_list(): |
| 109 |
print ("\tifname %s, linkup %d, changed %d, speed %d, duplex %d, removed %d" % |
| 110 |
(port.ifname, port.linkup, port.changed, |
| 111 |
port.speed, port.duplex, port.removed)) |
| 112 |
return 0 |
| 113 |
|
| 114 |
def _option_change_handler_func(self, unused): |
| 115 |
print "------------------\noption change\n\toption list:" |
| 116 |
for option in self._t.option_list(): |
| 117 |
print ("\t%s = %s (changed %d)" % (option.name, option.value, |
| 118 |
option.changed)) |
| 119 |
return 0 |
| 120 |
|
| 121 |
def _change_active_port(self, old_active, new_active): |
| 122 |
if old_active: |
| 123 |
old_active.set_hwaddr(self._old_active_hwaddr) |
| 124 |
|
| 125 |
self._t.set_active_port(new_active) |
| 126 |
mac = self._t.get_hwaddr() |
| 127 |
self._old_active_hwaddr = new_active.get_hwaddr() |
| 128 |
new_active.set_hwaddr(mac) |
| 129 |
print "*** new active port \"%s\" selected" % new_active |
| 130 |
|
| 131 |
def _port_change_handler_ab_func(self, unused): |
| 132 |
try: |
| 133 |
active = self._t.get_active_port() |
| 134 |
if active: |
| 135 |
print "*** current active port is \"%s\"" % active |
| 136 |
if active.linkup: |
| 137 |
return |
| 138 |
elif active.changed: |
| 139 |
print "*** active port \"%s\" lost link" % active |
| 140 |
except KeyError: |
| 141 |
active = None |
| 142 |
print "*** no current active port set" |
| 143 |
|
| 144 |
best = None |
| 145 |
for port in self._t.port_list(): |
| 146 |
if port.linkup: |
| 147 |
if (not best or |
| 148 |
port.speed > best.speed or |
| 149 |
(port.speed == best.speed and port.duplex > best.duplex)): |
| 150 |
best = port |
| 151 |
if best: |
| 152 |
self._change_active_port(active, best) |
| 153 |
|
| 154 |
def close(self): |
| 155 |
self._t.close() |
| 156 |
|
| 157 |
def loop_forever(self): |
| 158 |
self._t.loop_forever() |
| 159 |
|
| 160 |
def kill_loop(self): |
| 161 |
self._t.kill_loop() |
| 162 |
|
| 163 |
def main(): |
| 164 |
team_daemon = TeamDaemon() |
| 165 |
|
| 166 |
def kill_loop(signum, frame): |
| 167 |
team_daemon.kill_loop() |
| 168 |
|
| 169 |
signal.signal(signal.SIGINT, kill_loop) |
| 170 |
signal.signal(signal.SIGHUP, kill_loop) |
| 171 |
signal.signal(signal.SIGTERM, kill_loop) |
| 172 |
|
| 173 |
team_daemon.loop_forever() |
| 174 |
team_daemon.close() |
| 175 |
|
| 176 |
if __name__ == "__main__": |
| 177 |
main() |