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