]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/ports/bonding
bonding: Rewrite the port hook and fix various errors
[people/stevee/network.git] / src / hooks / ports / bonding
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 . /usr/lib/network/header-port
23
24 HOOK_SETTINGS="HOOK ADDRESS MIIMON MODE SLAVES"
25
26 ADDRESS=$(mac_generate)
27 SLAVES=""
28 MIIMON=100
29 MODE="balance-rr"
30
31 function hook_check() {
32 assert isset ADDRESS
33 assert ismac ADDRESS
34
35 #assert isset SLAVES
36 assert isinteger MIIMON
37 }
38
39 function hook_create() {
40 hook_edit $@
41 }
42
43 function hook_edit() {
44 local port=${1}
45 assert isset port
46 shift
47
48 while [ $# -gt 0 ]; do
49 case "${1}" in
50 --address=*)
51 ADDRESS=$(cli_get_val ${1})
52 ;;
53 --miimon=*)
54 MIIMON=$(cli_get_val ${1})
55 ;;
56 --mode=*)
57 MODE=$(cli_get_val ${1})
58 ;;
59 --slave=*)
60 slave=$(cli_get_val ${1})
61 SLAVES="${SLAVES} ${slave}"
62 ;;
63 *)
64 warning "Unknown argument '${1}'"
65 ;;
66 esac
67 shift
68 done
69
70 DEVICE=${port}
71
72 # XXX think this must move to _check()
73 if ! isset DEVICE; then
74 error "You must set a device name."
75 exit ${EXIT_ERROR}
76 fi
77
78 if ! isset SLAVES; then
79 error "You need to specify at least one slave port (e.g. --slave=port0)."
80 exit ${EXIT_ERROR}
81 fi
82
83 local slave
84 for slave in $(unquote ${SLAVES}); do
85 if ! device_is_ethernet ${slave}; then
86 error "Slave device '${slave}' is not an ethernet device."
87 exit ${EXIT_ERROR}
88 fi
89 done
90
91 # Remove any whitespace
92 SLAVES=$(echo ${SLAVES})
93
94 port_settings_write "${port}" ${HOOK_SETTINGS}
95
96 exit ${EXIT_OK}
97 }
98
99 function hook_up() {
100 local device="${1}"
101 assert isset device
102
103 port_settings_read "${device}" ${HOOK_SETTINGS}
104
105 if ! device_exists ${device}; then
106 bonding_create "${device}" \
107 --address="${ADDRESS}" \
108 --mode="${MODE}" || exit ${EXIT_ERROR}
109 fi
110
111 device_set_address "${device}" "${ADDRESS}"
112 bonding_set_miimon "${device}" "${MIIMON}"
113 device_set_up "${device}"
114
115 local slave
116 for slave in $(unquote ${SLAVES}); do
117 if ! device_exists ${slave}; then
118 log WARNING "Cannot enslave '${slave}' to '${device}' as it is not available"
119 continue
120 fi
121
122 bonding_enslave_device "${device}" "${slave}"
123 done
124
125 # Bring up the device.
126 device_set_up "${device}"
127
128 exit ${EXIT_OK}
129 }
130
131 function hook_down() {
132 local device="${1}"
133
134 bonding_remove "${device}"
135
136 local slave
137 for slave in ${SLAVES}; do
138 device_set_down "${slave}"
139 done
140
141 exit ${EXIT_OK}
142 }