]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.bonding
Use "ip link set X master" where ever we can
[people/ms/network.git] / src / functions / functions.bonding
CommitLineData
d61a01d4
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
d1e71061
MT
22BONDING_ALLOWED_MODES="balance-rr active-backup balance-xor broadcast 802.3ad \
23 balance-tlb balance-alb"
24BONDING_MASTERS="/sys/class/net/bonding_masters"
a6fcc369 25BONDING_PORT_PATTERN="bN"
d1e71061 26
1c6a4e30 27bonding_init() {
e72eb5c8 28 module_load "bonding"
711ffac1
MT
29}
30
1c6a4e30 31bonding_create() {
d61a01d4 32 local device=${1}
d1e71061
MT
33 assert isset device
34 shift
35
36 local address
37 local mode="balance-rr"
38
39 while [ $# -gt 0 ]; do
40 case "${1}" in
41 --address=*)
42 address=$(cli_get_val ${1})
43 ;;
44 --mode=*)
45 mode=$(cli_get_val ${1})
46 ;;
47 *)
48 error "Unrecognized argument: ${1}"
49 return ${EXIT_ERROR}
50 ;;
51 esac
52 shift
53 done
d61a01d4 54
d1e71061
MT
55 if isset address; then
56 if ! ismac address; then
57 log ERROR "Invalid mac address: ${address}"
58 return ${EXIT_ERROR}
59 fi
60 fi
61
62 if ! list_match "${mode}" ${BONDING_ALLOWED_MODES}; then
63 log ERROR "Bonding mode is not supported: ${mode}"
64 log ERROR "Valid modes are: ${BONDING_ALLOWED_MODES}"
65 return ${EXIT_ERROR}
66 fi
d61a01d4 67
065a1bd1
MT
68 # Initialize the bonding driver just
69 # when we need it.
70 bonding_init
71
a6fcc369
MT
72 # Create the bonding device
73 if cmd ip link add "${device}" address "${address}" \
74 type bond mode "${mode}"; then
d1e71061
MT
75 log DEBUG "Successfully created bonding device '${device}'"
76 else
77 log ERROR "Could not create bonding device '${device}'"
78 return ${EXIT_ERROR}
79 fi
80
d1e71061 81 return ${EXIT_OK}
d61a01d4
MT
82}
83
1c6a4e30 84bonding_remove() {
5bb2429a 85 local device=${1}
711ffac1
MT
86 assert isset device
87
d1e71061 88 # Remove the device.
a6fcc369 89 if device_delete "${device}"; then
d1e71061
MT
90 log DEBUG "Successfully removed bonding device '${device}'"
91 else
92 log ERROR "Could not remove bonding device '${device}'"
93 return ${EXIT_ERROR}
94 fi
d61a01d4 95
d1e71061 96 return ${EXIT_OK}
d61a01d4
MT
97}
98
1c6a4e30 99bonding_get_mode() {
711ffac1 100 local device=${1}
d1e71061 101 assert isset device
711ffac1
MT
102
103 local mode mode_num
104 read mode mode_num < ${SYS_CLASS_NET}/${device}/bonding/mode
d1e71061 105 print "${mode}"
711ffac1
MT
106}
107
1c6a4e30 108bonding_set_mode() {
e72eb5c8
MT
109 assert [ $# -eq 2 ]
110
111 local device="${1}"
112 local mode="${2}"
113
644d3bb8 114 if fappend "${SYS_CLASS_NET}/${device}/bonding/mode" "${mode}"; then
e72eb5c8
MT
115 log DEBUG "Set mode of bond '${device}' to '${mode}'"
116 else
117 log ERROR "Could not set mode of bond '${device}' to '${mode}'"
118 return ${EXIT_ERROR}
119 fi
120
121 return ${EXIT_OK}
122}
123
1c6a4e30 124bonding_enslave_device() {
5bb2429a 125 local device=${1}
d1e71061
MT
126 assert isset device
127
5bb2429a 128 local slave=${2}
d1e71061
MT
129 assert isset slave
130
d61a01d4
MT
131 shift 2
132
a6fcc369
MT
133 local slaves="$(bonding_get_slaves "${device}")"
134 if list_match "${slave}" ${slaves}; then
135 log DEBUG "${slave} is already enslaved in ${device}"
136 return ${EXIT_OK}
137 fi
d1e71061 138
a6fcc369
MT
139 # Slave must be down to be enslaved.
140 if ! device_set_down "${slave}"; then
d1e71061
MT
141 log ERROR "Cannot enslave '${slave}' because it cannot be set down."
142 return ${EXIT_ERROR}
143 fi
711ffac1 144
a6fcc369 145 # Add it
0e523702 146 if ! device_set_master "${slave}" "${device}"; then
e72eb5c8 147 log ERROR "Could not enslave '${slave}' to '${device}'"
d1e71061
MT
148 return ${EXIT_ERROR}
149 fi
150
0e523702 151 log DEBUG "Successfully enslaved '${slave}' to '${device}'"
d1e71061 152 return ${EXIT_OK}
d61a01d4 153}
711ffac1 154
1c6a4e30 155bonding_get_slaves() {
711ffac1 156 local device=${1}
d1e71061
MT
157 assert isset device
158 shift
159
160 local file="slaves"
161 while [ $# -gt 0 ]; do
162 case "${1}" in
163 --active)
164 file="active_slave"
165 ;;
166 *)
167 error "Unrecognized argument: ${1}"
168 return ${EXIT_ERROR}
169 ;;
170 esac
171 shift
172 done
711ffac1 173
d1e71061
MT
174 fread ${SYS_CLASS_NET}/${device}/bonding/${file}
175
176 return ${EXIT_OK}
711ffac1
MT
177}
178
1c6a4e30 179bonding_get_lacp_rate() {
711ffac1 180 local device=${1}
d1e71061 181 assert isset device
711ffac1 182
d1e71061
MT
183 local rate rateno
184 read -r rate rateno \
185 < ${SYS_CLASS_NET}/${device}/bonding/lacp_rate
711ffac1 186
d1e71061
MT
187 print "${rate}"
188 return ${EXIT_OK}
189}
711ffac1 190
1c6a4e30 191bonding_get_miimon() {
711ffac1 192 local device=${1}
d1e71061 193 assert isset device
711ffac1 194
d1e71061 195 fread ${SYS_CLASS_NET}/${device}/bonding/miimon
711ffac1
MT
196}
197
1c6a4e30 198bonding_set_miimon() {
711ffac1 199 local device=${1}
d1e71061 200 assert isset device
711ffac1 201
d1e71061
MT
202 local miimon=${2}
203 assert isset miimon
711ffac1 204
d1e71061 205 print "${miimon}" > ${SYS_CLASS_NET}/${device}/bonding/miimon
711ffac1
MT
206}
207
1c6a4e30 208bonding_slave_get_master() {
711ffac1 209 local slave=${1}
711ffac1 210 assert isset slave
711ffac1 211
d1e71061
MT
212 device_is_bonded ${slave} || return ${EXIT_ERROR}
213
214 local master=$(fread ${SYS_CLASS_NET}/${slave}/master/ifindex)
215 if isset master; then
216 device_ifindex_to_name ${master}
217 return ${EXIT_OK}
218 fi
711ffac1
MT
219
220 return ${EXIT_ERROR}
221}