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