]> git.ipfire.org Git - people/arne_f/network.git/blame - hooks/ports/bonding
network: Add some sanity checks when removing a port.
[people/arne_f/network.git] / hooks / ports / bonding
CommitLineData
711ffac1
MT
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. /lib/network/header-port
23
24HOOK_SETTINGS="HOOK DEVICE_MAC MIIMON MODE SLAVES"
25
98f4dae6
MT
26PORT_CHILDREN_VAR="SLAVES"
27
711ffac1
MT
28DEVICE_MAC=$(mac_generate)
29MIIMON=100
30
31function _check() {
32 assert isset DEVICE_MAC
33 assert ismac DEVICE_MAC
34
35 #assert isset SLAVES
36 assert isinteger MIIMON
37}
38
39function _create() {
40 _edit $@
41}
42
43function _edit() {
44 local port=${1}
45 shift
46
47 while [ $# -gt 0 ]; do
48 case "${1}" in
49 --mac=*)
50 DEVICE_MAC=${1#--mac=}
51 ;;
52 --miimon=*)
53 MIIMON=${1#--miimon=}
54 ;;
55 --mode=*)
56 MODE=${1#--mode=}
57 ;;
58 --slave=*)
59 slave=${1#--slave=}
60 SLAVES="${SLAVES} $(macify ${slave})"
61 ;;
62 *)
63 warning "Unknown argument '${1}'"
64 ;;
65 esac
66 shift
67 done
68
69 DEVICE=${port}
70
71 # XXX think this must move to _check()
72 if ! isset DEVICE; then
73 error "You must set a device name."
74 exit ${EXIT_ERROR}
75 fi
76
77 if ! isset SLAVES; then
78 error "You need to specify at least one slave port (e.g. --slave=port0)."
79 exit ${EXIT_ERROR}
80 fi
81
82 local slave
83 for slave in ${SLAVES}; do
84 if ! device_is_real $(devicify ${slave}); then
85 error "Slave device '${slave}' is not an ethernet device."
86 exit ${EXIT_ERROR}
87 fi
88 done
89
90 # Remove any whitespace
91 SLAVES=$(echo ${SLAVES})
92
93 config_write $(port_file ${port}) ${HOOK_SETTINGS}
94
95 exit ${EXIT_OK}
96}
97
98function _up() {
99 local device=${1}
100
101 config_read $(port_file ${device})
102
103 if device_exists ${device}; then
104 log DEBUG "Bonding device '${device}' does already exist."
105
106 device_set_address ${DEVICE_MAC}
107 device_set_up ${device}
108
109 exit ${EXIT_OK}
110 fi
111
112 bonding_create ${device} ${DEVICE_MAC}
113
114 if [ -n "${MODE}" ]; then
115 bonding_set_mode ${device} ${MODE}
116 fi
117
118 bonding_set_miimon ${device} ${MIIMON}
119
120 local slave
121 for slave in ${SLAVES}; do
122 if ! device_exists $(devicify ${slave}); then
123 warning_log "${device}: configured slave '${slave}' is not available."
124 continue
125 fi
126
127 slave=$(devicify ${slave})
128 assert isset slave
129
130 bonding_enslave_device ${device} ${slave}
131 done
132
133 exit ${EXIT_OK}
134}
135
136function _down() {
137 local device=${1}
138
139 bonding_remove ${device}
140
141 local slave
142 for slave in ${SLAVES}; do
143 device_set_down ${slave}
144 done
145
146 exit ${EXIT_OK}
147}
148
149function _status() {
150 local port=${1}
151 shift
152
153 assert isset port
154
155 echo "${port}"
156
157 local status=$(device_get_status ${port})
158 printf "${DEVICE_PRINT_LINE1}" "Status:" "$(echo -ne ${STATUS_COLOUR[${status}]}${STATUS_TEXT[${status}]}${COLOUR_NORMAL})"
159
160 cli_headline " Ethernet information:"
161 printf "${DEVICE_PRINT_LINE1}" "Address:" $(device_get_address ${port})
162 printf "${DEVICE_PRINT_LINE1}" "MTU:" $(device_get_mtu ${port})
163 printf "${DEVICE_PRINT_LINE1}" "Promisc mode:" $(device_is_promisc ${port} && echo "yes" || echo "no")
164
165 if device_is_bonded ${port}; then
166 cli_headline " Bonding information:"
167
168 local master=$(bonding_slave_get_master ${port})
169 printf "${DEVICE_PRINT_LINE1}" "Master:" "${master}"
170
171 local active
172 if [ "$(bonding_get_active_slave ${master})" = "${port}" ]; then
173 active="yes"
174 else
175 active="no"
176 fi
177 printf "${DEVICE_PRINT_LINE1}" "Active slave:" "${active}"
178 fi
179
180 if device_is_bonding ${port}; then
181 cli_headline " Bonding information:"
182
183 printf "${DEVICE_PRINT_LINE1}" "Mode:" "$(bonding_get_mode ${port})"
184 # XXX lacp rate
185 echo
186
187 local slave
188 local slave_active=$(bonding_get_active_slave ${port})
189 for slave in $(bonding_get_slaves ${port}); do
190 printf "${DEVICE_PRINT_LINE1}" "Slave$([ "${slave}" = "${slave_active}" ] && echo " (active)"):" "${slave}"
191 done
192 fi
193
194 cli_headline " Statistics:"
195 printf "${DEVICE_PRINT_LINE1}" "Received:" \
196 "$(beautify_bytes $(device_get_rx_bytes ${port})) ($(device_get_rx_packets ${port}) packets)"
197 printf "${DEVICE_PRINT_LINE1}" "Sent:" \
198 "$(beautify_bytes $(device_get_tx_bytes ${port})) ($(device_get_tx_packets ${port}) packets)"
199
200 echo
201
202 exit ${EXIT_OK}
203}
204
205run $@