]>
Commit | Line | Data |
---|---|---|
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 | 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 | ||
26 | function bonding_init() { | |
27 | module_load "bonding" | |
28 | } | |
29 | ||
30 | function bonding_create() { | |
31 | local device=${1} | |
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 | |
53 | ||
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 | |
66 | ||
67 | # Initialize the bonding driver just | |
68 | # when we need it. | |
69 | bonding_init | |
70 | ||
71 | # Create the bonding device. | |
72 | fwrite "${BONDING_MASTERS}" "+${device}" | |
73 | local ret=$? | |
74 | ||
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. | |
83 | bonding_set_mode "${device}" "${mode}" | |
84 | ||
85 | if isset address; then | |
86 | device_set_address "${device}" "${address}" | |
87 | fi | |
88 | ||
89 | return ${EXIT_OK} | |
90 | } | |
91 | ||
92 | function bonding_remove() { | |
93 | local device=${1} | |
94 | assert isset device | |
95 | ||
96 | if ! device_exists ${device}; then | |
97 | return ${EXIT_ERROR} | |
98 | fi | |
99 | ||
100 | # Set device down if not already done. | |
101 | device_set_down ${device} | |
102 | ||
103 | # Remove the device. | |
104 | fwrite "${BONDING_MASTERS}" "-${device}" | |
105 | local ret=$? | |
106 | ||
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 | |
113 | ||
114 | return ${EXIT_OK} | |
115 | } | |
116 | ||
117 | function bonding_get_mode() { | |
118 | local device=${1} | |
119 | assert isset device | |
120 | ||
121 | local mode mode_num | |
122 | read mode mode_num < ${SYS_CLASS_NET}/${device}/bonding/mode | |
123 | print "${mode}" | |
124 | } | |
125 | ||
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 | ||
142 | function bonding_enslave_device() { | |
143 | local device=${1} | |
144 | assert isset device | |
145 | ||
146 | local slave=${2} | |
147 | assert isset slave | |
148 | ||
149 | shift 2 | |
150 | ||
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 | |
158 | ||
159 | # Add it. | |
160 | fwrite "${SYS_CLASS_NET}/${device}/bonding/slaves" "+${slave}" | |
161 | local ret=$? | |
162 | ||
163 | if [ ${ret} -eq ${EXIT_OK} ]; then | |
164 | log DEBUG "Successfully enslaved '${slave}' to '${device}'" | |
165 | else | |
166 | log ERROR "Could not enslave '${slave}' to '${device}'" | |
167 | return ${EXIT_ERROR} | |
168 | fi | |
169 | ||
170 | return ${EXIT_OK} | |
171 | } | |
172 | ||
173 | function bonding_get_slaves() { | |
174 | local device=${1} | |
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 | |
191 | ||
192 | fread ${SYS_CLASS_NET}/${device}/bonding/${file} | |
193 | ||
194 | return ${EXIT_OK} | |
195 | } | |
196 | ||
197 | function bonding_get_lacp_rate() { | |
198 | local device=${1} | |
199 | assert isset device | |
200 | ||
201 | local rate rateno | |
202 | read -r rate rateno \ | |
203 | < ${SYS_CLASS_NET}/${device}/bonding/lacp_rate | |
204 | ||
205 | print "${rate}" | |
206 | return ${EXIT_OK} | |
207 | } | |
208 | ||
209 | function bonding_get_miimon() { | |
210 | local device=${1} | |
211 | assert isset device | |
212 | ||
213 | fread ${SYS_CLASS_NET}/${device}/bonding/miimon | |
214 | } | |
215 | ||
216 | function bonding_set_miimon() { | |
217 | local device=${1} | |
218 | assert isset device | |
219 | ||
220 | local miimon=${2} | |
221 | assert isset miimon | |
222 | ||
223 | print "${miimon}" > ${SYS_CLASS_NET}/${device}/bonding/miimon | |
224 | } | |
225 | ||
226 | function bonding_slave_get_master() { | |
227 | local slave=${1} | |
228 | assert isset slave | |
229 | ||
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 | |
237 | ||
238 | return ${EXIT_ERROR} | |
239 | } |