]>
Commit | Line | Data |
---|---|---|
1848564d 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 | ||
1c6a4e30 | 22 | device_list() { |
f90dd58c MT |
23 | local devices |
24 | ||
25 | # Add all interfaces | |
26 | list_append devices $(devices_get_all) | |
27 | ||
28 | # Add all PHYs | |
29 | list_append devices $(phy_list) | |
30 | ||
31 | # Add all serial devices | |
32 | list_append devices $(serial_list) | |
33 | ||
34 | # Return a sorted result | |
35 | list_sort ${devices} | |
36 | } | |
37 | ||
1848564d | 38 | # Check if the device exists |
1c6a4e30 | 39 | device_exists() { |
1848564d MT |
40 | local device=${1} |
41 | ||
42 | # If device name was not found, exit. | |
43 | [ -n "${device}" ] || return ${EXIT_ERROR} | |
44 | ||
6c74a64c MT |
45 | # Check for a normal network device. |
46 | [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK} | |
47 | ||
f90dd58c MT |
48 | # If the check above did not find a result, |
49 | # we check for PHYs. | |
50 | phy_exists "${device}" && return ${EXIT_OK} | |
51 | ||
52 | # If the check above did not find a result, | |
6c74a64c MT |
53 | # we check for serial devices. |
54 | serial_exists ${device} | |
1848564d MT |
55 | } |
56 | ||
1c6a4e30 | 57 | device_matches_pattern() { |
a23fdc0e MT |
58 | local device="${1}" |
59 | assert isset device | |
60 | ||
61 | local pattern="${2}" | |
62 | assert isset pattern | |
63 | ||
64 | pattern="^${pattern//N/[[:digit:]]+}$" | |
65 | ||
66 | [[ ${device} =~ ${pattern} ]] \ | |
67 | && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
68 | } | |
69 | ||
1c6a4e30 | 70 | device_delete() { |
99be6026 MT |
71 | local device=${1} |
72 | assert isset device | |
73 | ||
74 | # Nothing to do, it device does not exist. | |
75 | device_exists ${device} || return ${EXIT_OK} | |
76 | ||
77 | # Delete the device. | |
78 | cmd_quiet ip link delete ${device} | |
79 | local ret=$? | |
80 | ||
81 | if [ ${ret} -ne ${EXIT_OK} ]; then | |
82 | log ERROR "device: Could not delete device '${device}': ${ret}" | |
83 | return ${EXIT_ERROR} | |
84 | fi | |
85 | ||
86 | return ${ret} | |
87 | } | |
88 | ||
1c6a4e30 | 89 | device_has_flag() { |
e369be1a MT |
90 | local device=${1} |
91 | local flag=${2} | |
92 | ||
93 | local flags=$(__device_get_file ${device} flags) | |
94 | ||
95 | if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then | |
96 | return ${EXIT_FALSE} | |
97 | else | |
98 | return ${EXIT_TRUE} | |
99 | fi | |
100 | } | |
101 | ||
1848564d | 102 | # Check if the device is up |
1c6a4e30 | 103 | device_is_up() { |
1848564d MT |
104 | local device=${1} |
105 | ||
106 | device_exists ${device} || return ${EXIT_ERROR} | |
107 | ||
e369be1a | 108 | device_has_flag ${device} 0x1 |
1848564d MT |
109 | } |
110 | ||
1c6a4e30 | 111 | device_ifindex_to_name() { |
99be6026 MT |
112 | local idx=${1} |
113 | assert isset idx | |
114 | ||
115 | local device device_idx | |
116 | for device in ${SYS_CLASS_NET}/*; do | |
117 | device=$(basename ${device}) | |
118 | device_exists ${device} || continue | |
119 | ||
120 | device_idx=$(device_get_ifindex ${device}) | |
121 | ||
122 | if [ "${device_idx}" = "${idx}" ]; then | |
123 | print "${device}" | |
124 | return ${EXIT_OK} | |
125 | fi | |
126 | done | |
127 | ||
128 | return ${EXIT_ERROR} | |
129 | } | |
130 | ||
1c6a4e30 | 131 | device_get_ifindex() { |
99be6026 MT |
132 | local device=${1} |
133 | assert isset device | |
134 | ||
135 | local path="${SYS_CLASS_NET}/${1}/ifindex" | |
136 | ||
137 | # Check if file can be read. | |
138 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
139 | ||
140 | print "$(<${path})" | |
141 | } | |
142 | ||
e6993835 | 143 | # Check if the device is a batman-adv bridge |
1c6a4e30 | 144 | device_is_batman_adv() { |
e6993835 MT |
145 | [ -d "${SYS_CLASS_NET}/${1}/mesh" ] |
146 | } | |
147 | ||
b8026986 | 148 | # Check if the device is a batman-adv slave port |
1c6a4e30 | 149 | device_is_batman_adv_slave() { |
7b192cf4 MT |
150 | local device="${1}" |
151 | ||
152 | if [ -d "${SYS_CLASS_NET}/${device}/batman_adv" ]; then | |
153 | local status="$(<${SYS_CLASS_NET}/${device}/batman_adv/iface_status)" | |
154 | ||
155 | case "${status}" in | |
156 | "active") | |
157 | return ${EXIT_TRUE} | |
158 | ;; | |
159 | *) | |
160 | return ${EXIT_FALSE} | |
161 | ;; | |
162 | esac | |
163 | fi | |
164 | ||
165 | return ${EXIT_FALSE} | |
e6993835 MT |
166 | } |
167 | ||
1848564d | 168 | # Check if the device is a bonding device |
1c6a4e30 | 169 | device_is_bonding() { |
1848564d MT |
170 | [ -d "/sys/class/net/${1}/bonding" ] |
171 | } | |
172 | ||
173 | # Check if the device bonded in a bonding device | |
1c6a4e30 | 174 | device_is_bonded() { |
711ffac1 | 175 | local device=${1} |
1848564d | 176 | |
0959482b | 177 | [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ] |
1848564d MT |
178 | } |
179 | ||
180 | # Check if the device is a bridge | |
1c6a4e30 | 181 | device_is_bridge() { |
1848564d MT |
182 | [ -d "/sys/class/net/${1}/bridge" ] |
183 | } | |
184 | ||
1c6a4e30 | 185 | device_is_bridge_attached() { |
81ed640c | 186 | local device=${1} |
81ed640c MT |
187 | [ -d "${SYS_CLASS_NET}/${device}/brport" ] |
188 | } | |
189 | ||
1c6a4e30 | 190 | device_is_wireless_monitor() { |
a23fdc0e MT |
191 | local device="${1}" |
192 | assert isset device | |
193 | ||
194 | device_is_wireless "${device}" && \ | |
195 | device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}" | |
196 | } | |
197 | ||
1c6a4e30 | 198 | device_is_wireless_adhoc() { |
b8026986 MT |
199 | local device="${1}" |
200 | assert isset device | |
201 | ||
202 | device_is_wireless "${device}" && \ | |
203 | device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}" | |
204 | } | |
205 | ||
1c6a4e30 | 206 | device_get_bridge() { |
99be6026 MT |
207 | local device=${1} |
208 | assert isset device | |
209 | ||
210 | # Check if device is attached to a bridge. | |
211 | device_is_bridge_attached ${device} || return ${EXIT_ERROR} | |
212 | ||
213 | local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex" | |
214 | [ -r "${ifindex_path}" ] || return ${EXIT_ERROR} | |
215 | ||
216 | local ifindex=$(<${ifindex_path}) | |
217 | assert isset ifindex | |
218 | ||
219 | device_ifindex_to_name ${ifindex} | |
220 | } | |
221 | ||
7951525a | 222 | # Check if the device is a vlan device |
1c6a4e30 | 223 | device_is_vlan() { |
1848564d | 224 | local device=${1} |
7951525a | 225 | assert isset device |
1848564d | 226 | |
7951525a | 227 | [ -e "${PROC_NET_VLAN}/${device}" ] |
1848564d MT |
228 | } |
229 | ||
7951525a | 230 | # Check if the device has vlan devices |
1c6a4e30 | 231 | device_has_vlans() { |
fb02e543 | 232 | local device=${1} |
7951525a | 233 | assert isset device |
fb02e543 | 234 | |
7951525a | 235 | if device_is_vlan ${device}; then |
ec63256a | 236 | return ${EXIT_FALSE} |
fb02e543 MT |
237 | fi |
238 | ||
7951525a MT |
239 | local vlans=$(device_get_vlans ${device}) |
240 | [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
ec63256a MT |
241 | } |
242 | ||
1c6a4e30 | 243 | device_get_vlans() { |
ec63256a | 244 | local device=${1} |
7951525a | 245 | assert isset device |
ec63256a | 246 | |
8357a7ff MT |
247 | # If no 8021q module has been loaded into the kernel, |
248 | # we cannot do anything. | |
7951525a | 249 | [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} |
8357a7ff | 250 | |
ec63256a MT |
251 | local dev spacer1 id spacer2 parent |
252 | while read dev spacer1 id spacer2 parent; do | |
7951525a MT |
253 | [ "${parent}" = "${device}" ] || continue |
254 | ||
255 | print "${dev}" | |
256 | done < ${PROC_NET_VLAN_CONFIG} | |
1848564d MT |
257 | } |
258 | ||
1848564d | 259 | # Check if the device is a ppp device |
1c6a4e30 | 260 | device_is_ppp() { |
1848564d MT |
261 | local device=${1} |
262 | ||
55b802cc | 263 | local type=$(__device_get_file ${device} type) |
28f0b4ab | 264 | |
e369be1a MT |
265 | [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR} |
266 | } | |
55b802cc | 267 | |
e369be1a | 268 | # Check if the device is a pointopoint device. |
1c6a4e30 | 269 | device_is_ptp() { |
e369be1a MT |
270 | local device=${1} |
271 | ||
272 | device_has_flag ${device} 0x10 | |
1848564d MT |
273 | } |
274 | ||
275 | # Check if the device is a loopback device | |
1c6a4e30 | 276 | device_is_loopback() { |
5bb2429a MT |
277 | local device=${1} |
278 | ||
1848564d MT |
279 | [ "${device}" = "lo" ] |
280 | } | |
281 | ||
0067696a MT |
282 | # Check if the device is a dummy device |
283 | # This is the worst possible check, but all I could come up with | |
1c6a4e30 | 284 | device_is_dummy() { |
0067696a MT |
285 | local device="${1}" |
286 | ||
287 | [[ ${device} =~ ^dummy[0-9]+$ ]] | |
288 | } | |
289 | ||
a508c27e | 290 | # Check if the device is a wireless device |
1c6a4e30 | 291 | device_is_wireless() { |
a508c27e MT |
292 | local device=${1} |
293 | ||
294 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
295 | } | |
296 | ||
1c6a4e30 | 297 | device_get_phy() { |
4733a336 MT |
298 | local device="${1}" |
299 | ||
300 | if device_is_wireless "${device}"; then | |
301 | print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)" | |
302 | return ${EXIT_OK} | |
303 | fi | |
304 | ||
305 | return ${EXIT_ERROR} | |
306 | } | |
307 | ||
1c6a4e30 | 308 | device_is_phy() { |
f90dd58c MT |
309 | phy_exists $@ |
310 | } | |
311 | ||
1c6a4e30 | 312 | device_is_serial() { |
6c74a64c MT |
313 | serial_exists $@ |
314 | } | |
315 | ||
2d6dab20 MT |
316 | # Returns true if a device is a tun device |
317 | device_is_tun() { | |
318 | local device="${1}" | |
319 | ||
320 | [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ] | |
321 | } | |
322 | ||
1848564d | 323 | # Check if the device is a physical network interface |
1c6a4e30 | 324 | device_is_ethernet() { |
1848564d MT |
325 | local device=${1} |
326 | ||
0067696a MT |
327 | device_is_ethernet_compatible "${device}" || \ |
328 | return ${EXIT_ERROR} | |
329 | ||
1848564d MT |
330 | device_is_loopback ${device} && \ |
331 | return ${EXIT_ERROR} | |
332 | ||
333 | device_is_bonding ${device} && \ | |
334 | return ${EXIT_ERROR} | |
335 | ||
336 | device_is_bridge ${device} && \ | |
337 | return ${EXIT_ERROR} | |
338 | ||
339 | device_is_ppp ${device} && \ | |
340 | return ${EXIT_ERROR} | |
341 | ||
7951525a | 342 | device_is_vlan ${device} && \ |
1848564d MT |
343 | return ${EXIT_ERROR} |
344 | ||
0067696a | 345 | device_is_dummy ${device} && \ |
419b4cd0 MT |
346 | return ${EXIT_ERROR} |
347 | ||
2d6dab20 MT |
348 | device_is_tun ${device} && \ |
349 | return ${EXIT_ERROR} | |
350 | ||
1848564d MT |
351 | return ${EXIT_OK} |
352 | } | |
353 | ||
354 | # Get the device type | |
1c6a4e30 | 355 | device_get_type() { |
5bb2429a | 356 | local device=${1} |
1848564d | 357 | |
b9f27baf MT |
358 | # If the device does not exist (happens on udev remove events), |
359 | # we do not bother to run all checks. | |
360 | if ! device_exists "${device}"; then | |
361 | echo "unknown" | |
362 | ||
363 | elif device_is_vlan ${device}; then | |
1848564d MT |
364 | echo "vlan" |
365 | ||
366 | elif device_is_bonding ${device}; then | |
367 | echo "bonding" | |
368 | ||
369 | elif device_is_bridge ${device}; then | |
370 | echo "bridge" | |
371 | ||
372 | elif device_is_ppp ${device}; then | |
373 | echo "ppp" | |
374 | ||
e6993835 MT |
375 | elif device_is_batman_adv ${device}; then |
376 | echo "batman-adv" | |
377 | ||
1848564d MT |
378 | elif device_is_loopback ${device}; then |
379 | echo "loopback" | |
380 | ||
b8026986 MT |
381 | elif device_is_wireless_adhoc ${device}; then |
382 | echo "wireless-adhoc" | |
383 | ||
a508c27e MT |
384 | elif device_is_wireless ${device}; then |
385 | echo "wireless" | |
386 | ||
0067696a MT |
387 | elif device_is_dummy ${device}; then |
388 | echo "dummy" | |
389 | ||
2d6dab20 MT |
390 | elif device_is_tun ${device}; then |
391 | echo "tun" | |
392 | ||
ec63256a MT |
393 | elif device_is_ethernet ${device}; then |
394 | echo "ethernet" | |
1848564d | 395 | |
6c74a64c MT |
396 | elif device_is_serial ${device}; then |
397 | echo "serial" | |
398 | ||
f90dd58c MT |
399 | elif device_is_phy ${device}; then |
400 | echo "phy" | |
401 | ||
1848564d MT |
402 | else |
403 | echo "unknown" | |
404 | fi | |
405 | } | |
406 | ||
1c6a4e30 | 407 | device_is_ethernet_compatible() { |
a4f7ad26 MT |
408 | local device="${1}" |
409 | ||
410 | # /sys/class/net/*/type must equal 1 for ethernet compatible devices | |
411 | local type="$(__device_get_file "${device}" "type")" | |
412 | [[ "${type}" = "1" ]] | |
413 | } | |
414 | ||
1c6a4e30 | 415 | device_get_status() { |
711ffac1 | 416 | local device=${1} |
711ffac1 MT |
417 | assert isset device |
418 | ||
3cb2fc42 | 419 | local status=${STATUS_DOWN} |
711ffac1 | 420 | |
3cb2fc42 | 421 | if device_is_up ${device}; then |
711ffac1 | 422 | status=${STATUS_UP} |
711ffac1 | 423 | |
3cb2fc42 MT |
424 | if ! device_has_carrier ${device}; then |
425 | status=${STATUS_NOCARRIER} | |
426 | fi | |
427 | fi | |
711ffac1 MT |
428 | |
429 | echo "${status}" | |
430 | } | |
431 | ||
1c6a4e30 | 432 | device_get_address() { |
1848564d MT |
433 | local device=${1} |
434 | ||
435 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
436 | } | |
437 | ||
1c6a4e30 | 438 | device_set_address() { |
08c5b789 MT |
439 | assert [ $# -eq 2 ] |
440 | ||
441 | local device="${1}" | |
442 | local addr="${2}" | |
1b7a1578 | 443 | |
08c5b789 | 444 | if ! device_exists "${device}"; then |
1b7a1578 MT |
445 | error "Device '${device}' does not exist." |
446 | return ${EXIT_ERROR} | |
447 | fi | |
448 | ||
08c5b789 MT |
449 | # Do nothing if the address has not changed |
450 | local old_addr="$(device_get_address "${device}")" | |
451 | if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then | |
452 | return ${EXIT_OK} | |
453 | fi | |
454 | ||
455 | log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" | |
1b7a1578 MT |
456 | |
457 | local up | |
08c5b789 MT |
458 | if device_is_up "${device}"; then |
459 | device_set_down "${device}" | |
1b7a1578 MT |
460 | up=1 |
461 | fi | |
462 | ||
08c5b789 | 463 | ip link set "${device}" address "${addr}" |
1b7a1578 MT |
464 | local ret=$? |
465 | ||
466 | if [ "${up}" = "1" ]; then | |
08c5b789 | 467 | device_set_up "${device}" |
1b7a1578 MT |
468 | fi |
469 | ||
470 | if [ "${ret}" != "0" ]; then | |
08c5b789 | 471 | error_log "Could not set address '${addr}' on device '${device}'" |
1b7a1578 MT |
472 | fi |
473 | ||
474 | return ${ret} | |
1848564d MT |
475 | } |
476 | ||
1c6a4e30 | 477 | device_get() { |
2ae0fb8d | 478 | local device |
711ffac1 MT |
479 | local devices |
480 | ||
2ae0fb8d MT |
481 | for device in ${SYS_CLASS_NET}/*; do |
482 | device=$(basename ${device}) | |
711ffac1 | 483 | |
2ae0fb8d MT |
484 | # bonding_masters is no device |
485 | [ "${device}" = "bonding_masters" ] && continue | |
486 | ||
487 | devices="${devices} ${device}" | |
488 | done | |
711ffac1 MT |
489 | |
490 | echo ${devices} | |
491 | return ${EXIT_OK} | |
492 | } | |
493 | ||
1c6a4e30 | 494 | devices_get_all() { |
711ffac1 | 495 | device_get |
1848564d MT |
496 | } |
497 | ||
498 | # Check if a device has a cable plugged in | |
1c6a4e30 | 499 | device_has_carrier() { |
5bb2429a MT |
500 | local device=${1} |
501 | assert isset device | |
502 | ||
ec63256a MT |
503 | local carrier=$(__device_get_file ${device} carrier) |
504 | [ "${carrier}" = "1" ] | |
1848564d MT |
505 | } |
506 | ||
1c6a4e30 | 507 | device_is_promisc() { |
1e4c26a4 MT |
508 | local device=${1} |
509 | ||
e369be1a | 510 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
511 | } |
512 | ||
1c6a4e30 | 513 | device_set_promisc() { |
cf6e4606 MT |
514 | local device=${1} |
515 | local state=${2} | |
516 | ||
517 | assert device_exists ${device} | |
518 | assert isset state | |
519 | assert isoneof state on off | |
520 | ||
521 | ip link set ${device} promisc ${state} | |
522 | } | |
523 | ||
1848564d | 524 | # Check if the device is free |
1c6a4e30 | 525 | device_is_free() { |
81ed640c | 526 | ! device_is_used $@ |
1848564d MT |
527 | } |
528 | ||
529 | # Check if the device is used | |
1c6a4e30 | 530 | device_is_used() { |
5bb2429a | 531 | local device=${1} |
1848564d | 532 | |
7951525a | 533 | device_has_vlans ${device} && \ |
fb02e543 | 534 | return ${EXIT_OK} |
1848564d | 535 | device_is_bonded ${device} && \ |
fb02e543 | 536 | return ${EXIT_OK} |
81ed640c MT |
537 | device_is_bridge_attached ${device} && \ |
538 | return ${EXIT_OK} | |
1848564d | 539 | |
fb02e543 | 540 | return ${EXIT_ERROR} |
1848564d MT |
541 | } |
542 | ||
1b7a1578 | 543 | # Give the device a new name |
1c6a4e30 | 544 | device_set_name() { |
1848564d | 545 | local source=$1 |
1578dae9 | 546 | local destination=${2} |
1848564d MT |
547 | |
548 | # Check if devices exists | |
549 | if ! device_exists ${source} || device_exists ${destination}; then | |
550 | return 4 | |
551 | fi | |
552 | ||
553 | local up | |
554 | if device_is_up ${source}; then | |
555 | ip link set ${source} down | |
556 | up=1 | |
557 | fi | |
558 | ||
559 | ip link set ${source} name ${destination} | |
560 | ||
561 | if [ "${up}" = "1" ]; then | |
562 | ip link set ${destination} up | |
563 | fi | |
564 | } | |
565 | ||
1848564d | 566 | # Set device up |
1c6a4e30 | 567 | device_set_up() { |
45546be1 | 568 | assert [ $# -eq 1 ] |
1848564d | 569 | |
45546be1 | 570 | local device=${1} |
711ffac1 | 571 | |
1848564d MT |
572 | # Do nothing if device is already up |
573 | device_is_up ${device} && return ${EXIT_OK} | |
574 | ||
f18ee3d4 | 575 | log INFO "Bringing up ${device}" |
81ed640c | 576 | |
f18ee3d4 | 577 | device_set_parent_up ${device} |
45546be1 MT |
578 | if ! cmd ip link set ${device} up; then |
579 | return ${EXIT_ERROR} | |
580 | fi | |
de72bd91 MT |
581 | |
582 | # Set SMP affinity | |
583 | if interrupt_use_smp_affinity; then | |
584 | device_auto_configure_smp_affinity "${port}" | |
585 | fi | |
586 | ||
587 | return ${EXIT_OK} | |
1848564d MT |
588 | } |
589 | ||
1c6a4e30 | 590 | device_set_parent_up() { |
81ed640c MT |
591 | local device=${1} |
592 | local parent | |
593 | ||
7951525a MT |
594 | if device_is_vlan ${device}; then |
595 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
596 | |
597 | device_is_up ${parent} && return ${EXIT_OK} | |
598 | ||
599 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
600 | ||
601 | device_set_up ${parent} | |
602 | return $? | |
603 | fi | |
604 | ||
605 | return ${EXIT_OK} | |
606 | } | |
607 | ||
1848564d | 608 | # Set device down |
1c6a4e30 | 609 | device_set_down() { |
45546be1 | 610 | assert [ $# -eq 1 ] |
1848564d | 611 | |
45546be1 | 612 | local device=${1} |
81ed640c MT |
613 | local ret=${EXIT_OK} |
614 | ||
615 | if device_is_up ${device}; then | |
f18ee3d4 | 616 | log INFO "Bringing down ${device}" |
81ed640c | 617 | |
45546be1 | 618 | cmd ip link set ${device} down |
81ed640c MT |
619 | ret=$? |
620 | fi | |
621 | ||
622 | device_set_parent_down ${device} | |
1848564d | 623 | |
81ed640c MT |
624 | return ${ret} |
625 | } | |
626 | ||
1c6a4e30 | 627 | device_set_parent_down() { |
81ed640c MT |
628 | local device=${1} |
629 | local parent | |
630 | ||
7951525a MT |
631 | if device_is_vlan ${device}; then |
632 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
633 | |
634 | device_is_up ${parent} || return ${EXIT_OK} | |
635 | ||
636 | if device_is_free ${parent}; then | |
637 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
638 | ||
639 | device_set_down ${parent} | |
640 | fi | |
641 | fi | |
642 | ||
643 | return ${EXIT_OK} | |
1848564d MT |
644 | } |
645 | ||
1c6a4e30 | 646 | device_get_mtu() { |
1848564d MT |
647 | local device=${1} |
648 | ||
491e4f92 MT |
649 | # Return an error if the device does not exist |
650 | device_exists ${device} || return ${EXIT_ERROR} | |
1848564d | 651 | |
f3e6fe50 | 652 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
653 | } |
654 | ||
655 | # Set mtu to a device | |
1c6a4e30 | 656 | device_set_mtu() { |
1b7a1578 | 657 | local device=${1} |
1848564d MT |
658 | local mtu=${2} |
659 | ||
491e4f92 | 660 | assert device_exists ${device} |
1b7a1578 | 661 | |
3464bc89 AF |
662 | # Handle bridges differently |
663 | if device_is_bridge ${device}; then | |
664 | local port | |
665 | for port in $(bridge_get_members ${device}); do | |
666 | device_set_mtu ${port} ${mtu} | |
667 | done | |
668 | fi | |
669 | ||
491e4f92 | 670 | log INFO "Setting MTU of ${device} to ${mtu}" |
1b7a1578 | 671 | |
1848564d | 672 | local up |
1b7a1578 MT |
673 | if device_is_up ${device}; then |
674 | device_set_down ${device} | |
1848564d MT |
675 | up=1 |
676 | fi | |
677 | ||
491e4f92 MT |
678 | local ret=${EXIT_OK} |
679 | if ! cmd ip link set ${device} mtu ${mtu}; then | |
680 | ret=${EXIT_ERROR} | |
1848564d | 681 | |
491e4f92 | 682 | log ERROR "Could not set MTU ${mtu} on ${device}" |
1b7a1578 MT |
683 | fi |
684 | ||
491e4f92 MT |
685 | if [ "${up}" = "1" ]; then |
686 | device_set_up ${device} | |
1848564d MT |
687 | fi |
688 | ||
689 | return ${ret} | |
690 | } | |
691 | ||
1c6a4e30 | 692 | device_adjust_mtu() { |
3ee5ccb1 MT |
693 | assert [ $# -eq 2 ] |
694 | ||
695 | local device="${1}" | |
696 | local other_device="${2}" | |
697 | ||
698 | local mtu="$(device_get_mtu "${other_device}")" | |
699 | device_set_mtu "${device}" "${mtu}" | |
700 | } | |
701 | ||
1c6a4e30 | 702 | device_discover() { |
1848564d MT |
703 | local device=${1} |
704 | ||
1b7a1578 MT |
705 | log INFO "Running discovery process on device '${device}'." |
706 | ||
1848564d | 707 | local hook |
d61a01d4 MT |
708 | for hook in $(hook_zone_get_all); do |
709 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
710 | done |
711 | } | |
712 | ||
f5ee091e MT |
713 | device_identify() { |
714 | assert [ $# -ge 1 ] | |
715 | ||
716 | local device="${1}" | |
717 | ||
718 | # Flash for ten seconds by default | |
719 | local seconds="10" | |
720 | ||
721 | # Run in background? | |
722 | local background="false" | |
723 | ||
724 | local arg | |
725 | while read arg; do | |
726 | case "${arg}" in | |
727 | --background) | |
728 | background="true" | |
729 | ;; | |
730 | --seconds=*) | |
731 | seconds="$(cli_get_val "${arg}")" | |
732 | ;; | |
733 | esac | |
734 | done <<< "$(args $@)" | |
735 | ||
736 | assert isinteger seconds | |
737 | ||
738 | if ! device_exists "${device}"; then | |
739 | log ERROR "Cannot identify device ${device}: Does not exist" | |
740 | return ${EXIT_ERROR} | |
741 | fi | |
742 | ||
743 | if ! device_is_ethernet "${device}"; then | |
744 | log DEBUG "Cannot identify device ${device}: Not an ethernet device" | |
745 | return ${EXIT_NOT_SUPPORTED} | |
746 | fi | |
747 | ||
748 | log DEBUG "Identifying device ${device}" | |
749 | ||
750 | local command="ethtool --identify ${device} ${seconds}" | |
751 | local ret=0 | |
752 | ||
753 | if enabled background; then | |
754 | cmd_background "${command}" | |
755 | else | |
756 | cmd_quiet "${command}" | |
757 | ret=$? | |
758 | fi | |
759 | ||
760 | return ${ret} | |
761 | } | |
762 | ||
1c6a4e30 | 763 | device_has_ip() { |
1848564d MT |
764 | local device=${1} |
765 | local addr=${2} | |
766 | ||
38f61548 MT |
767 | assert isset addr |
768 | assert device_exists ${device} | |
769 | ||
770 | # IPv6 addresses must be fully imploded | |
771 | local protocol=$(ip_detect_protocol ${addr}) | |
772 | case "${protocol}" in | |
773 | ipv6) | |
13a6e69f | 774 | addr=$(ipv6_format "${addr}") |
38f61548 MT |
775 | ;; |
776 | esac | |
1848564d | 777 | |
38f61548 | 778 | listmatch ${addr} $(device_get_addresses ${device}) |
1848564d | 779 | } |
4231f419 | 780 | |
1c6a4e30 | 781 | device_get_addresses() { |
4231f419 | 782 | local device=${1} |
4231f419 | 783 | |
38f61548 | 784 | assert device_exists ${device} |
4231f419 | 785 | |
38f61548 MT |
786 | local prot |
787 | local addr | |
788 | local line | |
789 | ip addr show ${device} | \ | |
790 | while read prot addr line; do | |
791 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
792 | done | |
4231f419 | 793 | } |
711ffac1 | 794 | |
1c6a4e30 | 795 | __device_get_file() { |
711ffac1 MT |
796 | local device=${1} |
797 | local file=${2} | |
798 | ||
799 | assert isset device | |
800 | assert isset file | |
801 | ||
e369be1a MT |
802 | local path="${SYS_CLASS_NET}/${device}/${file}" |
803 | [ -r "${path}" ] || return ${EXIT_ERROR} | |
804 | ||
805 | echo "$(<${path})" | |
711ffac1 MT |
806 | } |
807 | ||
1c6a4e30 | 808 | __device_set_file() { |
2c083d57 MT |
809 | assert [ $# -eq 3 ] |
810 | ||
811 | local device="${1}" | |
812 | local file="${2}" | |
813 | local value="${3}" | |
814 | ||
815 | local path="${SYS_CLASS_NET}/${device}/${file}" | |
816 | if [ ! -w "${path}" ]; then | |
817 | log DEBUG "Cannot write to file '${file}' (${value})" | |
818 | return ${EXIT_ERROR} | |
819 | fi | |
820 | ||
821 | echo "${value}" > "${path}" | |
822 | } | |
823 | ||
1c6a4e30 | 824 | device_get_rx_bytes() { |
711ffac1 MT |
825 | local device=${1} |
826 | ||
827 | __device_get_file ${device} statistics/rx_bytes | |
828 | } | |
829 | ||
1c6a4e30 | 830 | device_get_tx_bytes() { |
711ffac1 MT |
831 | local device=${1} |
832 | ||
833 | __device_get_file ${device} statistics/tx_bytes | |
834 | } | |
835 | ||
1c6a4e30 | 836 | device_get_rx_packets() { |
711ffac1 MT |
837 | local device=${1} |
838 | ||
839 | __device_get_file ${device} statistics/rx_packets | |
840 | } | |
841 | ||
1c6a4e30 | 842 | device_get_tx_packets() { |
711ffac1 MT |
843 | local device=${1} |
844 | ||
845 | __device_get_file ${device} statistics/tx_packets | |
846 | } | |
847 | ||
1c6a4e30 | 848 | device_get_rx_errors() { |
711ffac1 MT |
849 | local device=${1} |
850 | ||
851 | __device_get_file ${device} statistics/rx_errors | |
852 | } | |
853 | ||
1c6a4e30 | 854 | device_get_tx_errors() { |
711ffac1 MT |
855 | local device=${1} |
856 | ||
857 | __device_get_file ${device} statistics/tx_errors | |
858 | } | |
ec63256a | 859 | |
1c6a4e30 | 860 | device_get_speed() { |
ec63256a MT |
861 | local device=${1} |
862 | ||
863 | __device_get_file ${device} speed | |
864 | } | |
865 | ||
1c6a4e30 | 866 | device_get_duplex() { |
ec63256a MT |
867 | local device=${1} |
868 | ||
869 | __device_get_file ${device} duplex | |
870 | } | |
657540d8 | 871 | |
1c6a4e30 | 872 | device_get_link_string() { |
657540d8 MT |
873 | local device="${1}" |
874 | assert isset device | |
875 | ||
876 | local s | |
877 | ||
878 | local speed="$(device_get_speed "${device}")" | |
879 | if isset speed; then | |
880 | list_append s "${speed} MBit/s" | |
881 | fi | |
882 | ||
883 | local duplex="$(device_get_duplex "${device}")" | |
884 | if isset duplex; then | |
885 | list_append s "${duplex} duplex" | |
886 | fi | |
887 | ||
888 | print "${s}" | |
889 | } | |
de72bd91 MT |
890 | |
891 | device_auto_configure_smp_affinity() { | |
892 | assert [ $# -eq 1 ] | |
893 | ||
894 | local device=${1} | |
895 | ||
896 | if lock_acquire "smp-affinity"; then | |
897 | device_set_smp_affinity "${port}" auto | |
898 | ||
899 | lock_release "smp-affinity" | |
900 | fi | |
901 | } | |
902 | ||
903 | device_set_smp_affinity() { | |
904 | assert [ $# -eq 2 ] | |
905 | ||
906 | local device=${1} | |
907 | local mode=${2} | |
908 | ||
909 | # mode can be auto which will automatically try to find | |
910 | # the least busy processor, or an integer for the desired | |
911 | # processor that should handle this device | |
912 | ||
913 | local num_processors=$(system_get_processors) | |
914 | ||
915 | if [ "${mode}" = "auto" ]; then | |
916 | local processor=$(interrupt_choose_least_busy_processor) | |
917 | else | |
918 | assert isinteger mode | |
919 | local processor=${mode} | |
920 | ||
921 | if [ ${processor} -gt ${num_processors} ]; then | |
922 | log ERROR "Processor ${processor} does not exist" | |
923 | return ${EXIT_ERROR} | |
924 | fi | |
925 | fi | |
926 | ||
927 | local interrupts=$(interrupts_for_device ${device}) | |
928 | if ! isset interrupts; then | |
929 | log DEBUG "${device} has no interrupts. Not changing SMP affinity" | |
930 | return ${EXIT_OK} | |
931 | fi | |
932 | ||
933 | # Set SMP affinity | |
934 | local interrupt | |
935 | for interrupt in ${interrupts}; do | |
936 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
937 | done | |
938 | ||
939 | # Find all queues and assign them to the next processor | |
940 | local queue | |
941 | for queue in $(device_get_queues ${device}); do | |
942 | case "${queue}" in | |
943 | # Only handle receive queues | |
944 | rx-*) | |
945 | for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do | |
946 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
947 | done | |
948 | ||
949 | device_queue_set_smp_affinity ${device} ${queue} ${processor} | |
950 | ;; | |
951 | ||
952 | # Ignore the rest | |
953 | *) | |
954 | continue | |
955 | ;; | |
956 | esac | |
957 | ||
958 | # Get the next available processor if in auto mode | |
959 | [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor}) | |
960 | done | |
961 | ||
962 | return ${EXIT_OK} | |
963 | } | |
964 | ||
965 | device_get_queues() { | |
966 | assert [ $# -eq 1 ] | |
967 | ||
968 | local device=${1} | |
969 | ||
970 | local queue | |
971 | for queue in ${SYS_CLASS_NET}/${device}/queues/*; do | |
972 | basename "${queue}" | |
973 | done | |
974 | } | |
975 | ||
976 | device_queue_set_smp_affinity() { | |
977 | assert [ $# -eq 3 ] | |
978 | ||
979 | local device=${1} | |
980 | local queue=${2} | |
981 | local processor=${3} | |
982 | ||
983 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus" | |
984 | assert [ -w "${path}" ] | |
985 | ||
986 | log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}" | |
987 | ||
988 | __processor_id_to_bitmap ${processor} > ${path} | |
989 | } |