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