]> git.ipfire.org Git - people/stevee/network.git/blame - hooks/ports/bonding
Add quote and unquote functions.
[people/stevee/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
987dfeb4 22. /usr/lib/network/header-port
711ffac1
MT
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})
fcbf6823 160 # XXX STATUS_COLOUR AND STATUS_TEXT DO NOT EXIST ANYMORE
711ffac1
MT
161 printf "${DEVICE_PRINT_LINE1}" "Status:" "$(echo -ne ${STATUS_COLOUR[${status}]}${STATUS_TEXT[${status}]}${COLOUR_NORMAL})"
162
163 cli_headline " Ethernet information:"
164 printf "${DEVICE_PRINT_LINE1}" "Address:" $(device_get_address ${port})
165 printf "${DEVICE_PRINT_LINE1}" "MTU:" $(device_get_mtu ${port})
166 printf "${DEVICE_PRINT_LINE1}" "Promisc mode:" $(device_is_promisc ${port} && echo "yes" || echo "no")
167
168 if device_is_bonded ${port}; then
169 cli_headline " Bonding information:"
170
171 local master=$(bonding_slave_get_master ${port})
172 printf "${DEVICE_PRINT_LINE1}" "Master:" "${master}"
173
174 local active
175 if [ "$(bonding_get_active_slave ${master})" = "${port}" ]; then
176 active="yes"
177 else
178 active="no"
179 fi
180 printf "${DEVICE_PRINT_LINE1}" "Active slave:" "${active}"
181 fi
182
183 if device_is_bonding ${port}; then
184 cli_headline " Bonding information:"
185
186 printf "${DEVICE_PRINT_LINE1}" "Mode:" "$(bonding_get_mode ${port})"
187 # XXX lacp rate
188 echo
189
190 local slave
191 local slave_active=$(bonding_get_active_slave ${port})
192 for slave in $(bonding_get_slaves ${port}); do
193 printf "${DEVICE_PRINT_LINE1}" "Slave$([ "${slave}" = "${slave_active}" ] && echo " (active)"):" "${slave}"
194 done
195 fi
196
197 cli_headline " Statistics:"
198 printf "${DEVICE_PRINT_LINE1}" "Received:" \
199 "$(beautify_bytes $(device_get_rx_bytes ${port})) ($(device_get_rx_packets ${port}) packets)"
200 printf "${DEVICE_PRINT_LINE1}" "Sent:" \
201 "$(beautify_bytes $(device_get_tx_bytes ${port})) ($(device_get_tx_packets ${port}) packets)"
202
203 echo
204
205 exit ${EXIT_OK}
206}