]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.bonding
network: Add some initialization handlers.
[people/arne_f/network.git] / functions.bonding
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 function bonding_init() {
23 if ! grep -q "^bonding" /proc/modules; then
24 modprobe bonding
25
26 bonding_remove bond0
27 fi
28 }
29
30 function bonding_create() {
31 local device=${1}
32 local mac=${2}
33
34 # Make sure the bonding is initialized
35 bonding_init
36
37 [ -z "${mac}" ] && mac=$(mac_generate)
38
39 log INFO "Creating bonding device '${device}' (${mac})."
40
41 echo "+${device}" > /sys/class/net/bonding_masters
42 device_set_address ${device} ${mac}
43 device_set_up ${device}
44 }
45
46 function bonding_remove() {
47 local device=$(devicify ${1})
48
49 assert isset device
50
51 log INFO "Remove bonding device '${device}'."
52
53 device_set_down ${device}
54 echo "-${device}" > /sys/class/net/bonding_masters
55 }
56
57 function bonding_set_mode() {
58 local device=${1}
59 local mode=${2}
60
61 log INFO "Setting bonding mode on '${device}' '${mode}'."
62
63 echo "${mode}" > /sys/class/net/${device}/bonding/mode
64 }
65
66 function bonding_get_mode() {
67 local device=${1}
68
69 local mode mode_num
70 read mode mode_num < ${SYS_CLASS_NET}/${device}/bonding/mode
71 echo "${mode}"
72 }
73
74 function bonding_enslave_device() {
75 local device=$(devicify ${1})
76 local slave=$(devicify ${2})
77 shift 2
78
79 assert isset device
80 assert isset slave
81
82 log INFO "Enslaving slave '${slave}' to '${device}'."
83
84 device_set_down ${slave}
85 echo "+${slave}" > /sys/class/net/${device}/bonding/slaves
86 }
87
88 function bonding_get_slaves() {
89 local device=${1}
90
91 cat ${SYS_CLASS_NET}/${device}/bonding/slaves
92 }
93
94 function bonding_get_active_slave() {
95 local device=${1}
96
97 cat ${SYS_CLASS_NET}/${device}/bonding/active_slave
98 }
99
100 # XXX function bonding_get_lacp_rate?
101
102 function bonding_get_miimon() {
103 local device=${1}
104
105 cat ${SYS_CLASS_NET}/${device}/bonding/miimon
106 }
107
108 function bonding_set_miimon() {
109 local device=${1}
110 local miimon=${2}
111
112 echo "${miimon}" > ${SYS_CLASS_NET}/${device}/bonding/miimon
113 }
114
115 function bonding_device_print() {
116 local device=${1}
117
118 ethernet_device_print ${device}
119
120 echo # Empty line
121
122 printf "${DEVICE_PRINT_LINE1}" "Mode:" "$(bonding_get_mode ${device})"
123 printf "${DEVICE_PRINT_LINE1}" "Slaves:" "$(bonding_get_slaves ${device})"
124 }
125
126 function bonding_slave_get_master() {
127 local slave=${1}
128
129 assert isset slave
130 assert device_is_bonded ${slave}
131
132 local device
133 for device in $(devices_get_all); do
134 if device_is_bonding ${device} && listmatch ${slave} $(bonding_get_slaves ${device}); then
135 echo "${device}"
136 return ${EXIT_OK}
137 fi
138 done
139
140 return ${EXIT_ERROR}
141 }