]> git.ipfire.org Git - network.git/blame - hooks/ports/bonding
Fix "network device" command and document it.
[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
ec63256a 84 if ! device_is_ethernet $(devicify ${slave}); then
711ffac1
MT
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}
b8c614b2 113 assert device_exists ${device}
711ffac1
MT
114
115 if [ -n "${MODE}" ]; then
116 bonding_set_mode ${device} ${MODE}
117 fi
118
119 bonding_set_miimon ${device} ${MIIMON}
b8c614b2 120 device_set_up ${device}
711ffac1
MT
121
122 local slave
123 for slave in ${SLAVES}; do
124 if ! device_exists $(devicify ${slave}); then
125 warning_log "${device}: configured slave '${slave}' is not available."
126 continue
127 fi
128
129 slave=$(devicify ${slave})
130 assert isset slave
131
132 bonding_enslave_device ${device} ${slave}
133 done
134
135 exit ${EXIT_OK}
136}
137
138function _down() {
139 local device=${1}
140
141 bonding_remove ${device}
142
143 local slave
144 for slave in ${SLAVES}; do
145 device_set_down ${slave}
146 done
147
148 exit ${EXIT_OK}
149}
150
151function _status() {
152 local port=${1}
153 shift
154
155 assert isset port
156
157 echo "${port}"
158
159 local status=$(device_get_status ${port})
160 printf "${DEVICE_PRINT_LINE1}" "Status:" "$(echo -ne ${STATUS_COLOUR[${status}]}${STATUS_TEXT[${status}]}${COLOUR_NORMAL})"
161
162 cli_headline " Ethernet information:"
163 printf "${DEVICE_PRINT_LINE1}" "Address:" $(device_get_address ${port})
164 printf "${DEVICE_PRINT_LINE1}" "MTU:" $(device_get_mtu ${port})
165 printf "${DEVICE_PRINT_LINE1}" "Promisc mode:" $(device_is_promisc ${port} && echo "yes" || echo "no")
166
167 if device_is_bonded ${port}; then
168 cli_headline " Bonding information:"
169
170 local master=$(bonding_slave_get_master ${port})
171 printf "${DEVICE_PRINT_LINE1}" "Master:" "${master}"
172
173 local active
174 if [ "$(bonding_get_active_slave ${master})" = "${port}" ]; then
175 active="yes"
176 else
177 active="no"
178 fi
179 printf "${DEVICE_PRINT_LINE1}" "Active slave:" "${active}"
180 fi
181
182 if device_is_bonding ${port}; then
183 cli_headline " Bonding information:"
184
185 printf "${DEVICE_PRINT_LINE1}" "Mode:" "$(bonding_get_mode ${port})"
186 # XXX lacp rate
187 echo
188
189 local slave
190 local slave_active=$(bonding_get_active_slave ${port})
191 for slave in $(bonding_get_slaves ${port}); do
192 printf "${DEVICE_PRINT_LINE1}" "Slave$([ "${slave}" = "${slave_active}" ] && echo " (active)"):" "${slave}"
193 done
194 fi
195
196 cli_headline " Statistics:"
197 printf "${DEVICE_PRINT_LINE1}" "Received:" \
198 "$(beautify_bytes $(device_get_rx_bytes ${port})) ($(device_get_rx_packets ${port}) packets)"
199 printf "${DEVICE_PRINT_LINE1}" "Sent:" \
200 "$(beautify_bytes $(device_get_tx_bytes ${port})) ($(device_get_tx_packets ${port}) packets)"
201
202 echo
203
204 exit ${EXIT_OK}
205}
206
207run $@