]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.device
device: Refactor check for device type
[people/ms/network.git] / src / functions / functions.device
CommitLineData
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
5b1fd814
MT
22declare -A DEVICE_LINK_SPEEDS=(
23 [10BaseT-Half]=0x1
24 [10BaseT-Full]=0x2
25 [100BaseT-Half]=0x4
26 [100BaseT-Full]=0x8
27 [1000BaseT-Half]=0x10
28 [1000BaseT-Full]=0x20
29 [10000BaseT-Full]=0x1000
30)
31
1c6a4e30 32device_list() {
f90dd58c 33 # Add all interfaces
91aeb1be
MT
34 local device
35 for device in $(list_directory ${SYS_CLASS_NET}); do
c1678cea
MT
36 if device_exists "${device}"; then
37 print "${device}"
38 fi
91aeb1be 39 done
f90dd58c 40
38d28e62
MT
41 # List all PHYs
42 phy_list
f90dd58c 43
38d28e62
MT
44 # List all serial devices
45 serial_list
f90dd58c
MT
46}
47
1848564d 48# Check if the device exists
1c6a4e30 49device_exists() {
1848564d
MT
50 local device=${1}
51
52 # If device name was not found, exit.
53 [ -n "${device}" ] || return ${EXIT_ERROR}
54
6c74a64c
MT
55 # Check for a normal network device.
56 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
57
f90dd58c
MT
58 # If the check above did not find a result,
59 # we check for PHYs.
60 phy_exists "${device}" && return ${EXIT_OK}
61
62 # If the check above did not find a result,
6c74a64c
MT
63 # we check for serial devices.
64 serial_exists ${device}
1848564d
MT
65}
66
1c6a4e30 67device_matches_pattern() {
a23fdc0e
MT
68 local device="${1}"
69 assert isset device
70
71 local pattern="${2}"
72 assert isset pattern
73
74 pattern="^${pattern//N/[[:digit:]]+}$"
75
76 [[ ${device} =~ ${pattern} ]] \
77 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
78}
79
1c6a4e30 80device_delete() {
99be6026
MT
81 local device=${1}
82 assert isset device
83
84 # Nothing to do, it device does not exist.
85 device_exists ${device} || return ${EXIT_OK}
86
cdcce259
MT
87 # Shut down device before we delete it
88 device_set_down "${device}"
89
99be6026
MT
90 # Delete the device.
91 cmd_quiet ip link delete ${device}
92 local ret=$?
93
94 if [ ${ret} -ne ${EXIT_OK} ]; then
95 log ERROR "device: Could not delete device '${device}': ${ret}"
96 return ${EXIT_ERROR}
97 fi
98
99 return ${ret}
100}
101
1c6a4e30 102device_has_flag() {
e369be1a
MT
103 local device=${1}
104 local flag=${2}
105
106 local flags=$(__device_get_file ${device} flags)
107
108 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
109 return ${EXIT_FALSE}
110 else
111 return ${EXIT_TRUE}
112 fi
113}
114
1848564d 115# Check if the device is up
1c6a4e30 116device_is_up() {
1848564d
MT
117 local device=${1}
118
119 device_exists ${device} || return ${EXIT_ERROR}
120
e369be1a 121 device_has_flag ${device} 0x1
1848564d
MT
122}
123
1c6a4e30 124device_ifindex_to_name() {
99be6026
MT
125 local idx=${1}
126 assert isset idx
127
128 local device device_idx
60b1f378 129 for device in $(list_directory "${SYS_CLASS_NET}"); do
99be6026
MT
130 device_idx=$(device_get_ifindex ${device})
131
132 if [ "${device_idx}" = "${idx}" ]; then
133 print "${device}"
134 return ${EXIT_OK}
135 fi
136 done
137
138 return ${EXIT_ERROR}
139}
140
1c6a4e30 141device_get_ifindex() {
99be6026
MT
142 local device=${1}
143 assert isset device
144
145 local path="${SYS_CLASS_NET}/${1}/ifindex"
146
147 # Check if file can be read.
148 [ -r "${path}" ] || return ${EXIT_ERROR}
149
150 print "$(<${path})"
151}
152
4ad5981f
MT
153device_get_driver() {
154 local device="${1}"
155 assert isset device
156
157 get_driver_from_path "${SYS_CLASS_NET}/${device}/device/driver/module"
158}
159
1848564d 160# Check if the device is a bonding device
1c6a4e30 161device_is_bonding() {
1848564d
MT
162 [ -d "/sys/class/net/${1}/bonding" ]
163}
164
165# Check if the device bonded in a bonding device
1c6a4e30 166device_is_bonded() {
711ffac1 167 local device=${1}
1848564d 168
0959482b 169 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ]
1848564d
MT
170}
171
172# Check if the device is a bridge
1c6a4e30 173device_is_bridge() {
1848564d
MT
174 [ -d "/sys/class/net/${1}/bridge" ]
175}
176
1c6a4e30 177device_is_bridge_attached() {
81ed640c 178 local device=${1}
81ed640c
MT
179 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
180}
181
1c6a4e30 182device_is_wireless_monitor() {
a23fdc0e
MT
183 local device="${1}"
184 assert isset device
185
186 device_is_wireless "${device}" && \
187 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
188}
189
1c6a4e30 190device_is_wireless_adhoc() {
b8026986
MT
191 local device="${1}"
192 assert isset device
193
194 device_is_wireless "${device}" && \
195 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
196}
197
1c6a4e30 198device_get_bridge() {
99be6026
MT
199 local device=${1}
200 assert isset device
201
202 # Check if device is attached to a bridge.
203 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
204
205 local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
206 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
207
208 local ifindex=$(<${ifindex_path})
209 assert isset ifindex
210
211 device_ifindex_to_name ${ifindex}
212}
213
7951525a 214# Check if the device is a vlan device
1c6a4e30 215device_is_vlan() {
1848564d 216 local device=${1}
7951525a 217 assert isset device
1848564d 218
7951525a 219 [ -e "${PROC_NET_VLAN}/${device}" ]
1848564d
MT
220}
221
7951525a 222# Check if the device has vlan devices
1c6a4e30 223device_has_vlans() {
fb02e543 224 local device=${1}
7951525a 225 assert isset device
fb02e543 226
7951525a 227 if device_is_vlan ${device}; then
ec63256a 228 return ${EXIT_FALSE}
fb02e543
MT
229 fi
230
7951525a
MT
231 local vlans=$(device_get_vlans ${device})
232 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
ec63256a
MT
233}
234
1c6a4e30 235device_get_vlans() {
ec63256a 236 local device=${1}
7951525a 237 assert isset device
ec63256a 238
8357a7ff
MT
239 # If no 8021q module has been loaded into the kernel,
240 # we cannot do anything.
7951525a 241 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
8357a7ff 242
ec63256a
MT
243 local dev spacer1 id spacer2 parent
244 while read dev spacer1 id spacer2 parent; do
7951525a
MT
245 [ "${parent}" = "${device}" ] || continue
246
247 print "${dev}"
248 done < ${PROC_NET_VLAN_CONFIG}
1848564d
MT
249}
250
5ee61d86
MT
251__device_type_matches() {
252 local device="${1}"
253 local type="${2}"
254
255 local _type="$(__device_get_file "${device}" "type")"
256
257 if [ "${type}" = "${_type}" ]; then
258 return ${EXIT_TRUE}
259 fi
260
261 return ${EXIT_FALSE}
262}
263
1848564d 264# Check if the device is a ppp device
1c6a4e30 265device_is_ppp() {
5ee61d86
MT
266 local device="${1}"
267 assert isset device
28f0b4ab 268
5ee61d86 269 __device_type_matches "${device}" 512
e369be1a 270}
55b802cc 271
e369be1a 272# Check if the device is a pointopoint device.
1c6a4e30 273device_is_ptp() {
e369be1a
MT
274 local device=${1}
275
276 device_has_flag ${device} 0x10
1848564d
MT
277}
278
279# Check if the device is a loopback device
1c6a4e30 280device_is_loopback() {
5bb2429a
MT
281 local device=${1}
282
1848564d
MT
283 [ "${device}" = "lo" ]
284}
285
0067696a
MT
286# Check if the device is a dummy device
287# This is the worst possible check, but all I could come up with
1c6a4e30 288device_is_dummy() {
0067696a
MT
289 local device="${1}"
290
291 [[ ${device} =~ ^dummy[0-9]+$ ]]
292}
293
82fac748
MT
294device_is_ipsec() {
295 local device="${1}"
296
297 [[ ${device} =~ ^ipsec\- ]]
298}
299
a508c27e 300# Check if the device is a wireless device
1c6a4e30 301device_is_wireless() {
a508c27e
MT
302 local device=${1}
303
304 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
305}
306
1a02da59 307device_is_vti() {
5ee61d86
MT
308 local device="${1}"
309 assert isset device
1a02da59 310
5ee61d86 311 __device_type_matches "${device}" 768
1a02da59
MT
312}
313
bcba5d27 314device_is_vti6() {
5ee61d86
MT
315 local device="${1}"
316 assert isset device
bcba5d27 317
5ee61d86 318 __device_type_matches "${device}" 769
bcba5d27
JS
319}
320
1c6a4e30 321device_get_phy() {
4733a336
MT
322 local device="${1}"
323
324 if device_is_wireless "${device}"; then
325 print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
326 return ${EXIT_OK}
327 fi
328
329 return ${EXIT_ERROR}
330}
331
1c6a4e30 332device_is_phy() {
2212045f 333 phy_exists "$@"
f90dd58c
MT
334}
335
1c6a4e30 336device_is_serial() {
2212045f 337 serial_exists "$@"
6c74a64c
MT
338}
339
2d6dab20
MT
340# Returns true if a device is a tun device
341device_is_tun() {
342 local device="${1}"
343
344 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
345}
346
1848564d 347# Check if the device is a physical network interface
1c6a4e30 348device_is_ethernet() {
1848564d
MT
349 local device=${1}
350
0067696a
MT
351 device_is_ethernet_compatible "${device}" || \
352 return ${EXIT_ERROR}
353
1848564d
MT
354 device_is_loopback ${device} && \
355 return ${EXIT_ERROR}
356
357 device_is_bonding ${device} && \
358 return ${EXIT_ERROR}
359
360 device_is_bridge ${device} && \
361 return ${EXIT_ERROR}
362
363 device_is_ppp ${device} && \
364 return ${EXIT_ERROR}
365
7951525a 366 device_is_vlan ${device} && \
1848564d
MT
367 return ${EXIT_ERROR}
368
0067696a 369 device_is_dummy ${device} && \
419b4cd0
MT
370 return ${EXIT_ERROR}
371
2d6dab20
MT
372 device_is_tun ${device} && \
373 return ${EXIT_ERROR}
374
1848564d
MT
375 return ${EXIT_OK}
376}
377
378# Get the device type
1c6a4e30 379device_get_type() {
5bb2429a 380 local device=${1}
1848564d 381
b9f27baf
MT
382 # If the device does not exist (happens on udev remove events),
383 # we do not bother to run all checks.
384 if ! device_exists "${device}"; then
385 echo "unknown"
386
387 elif device_is_vlan ${device}; then
1848564d
MT
388 echo "vlan"
389
390 elif device_is_bonding ${device}; then
391 echo "bonding"
392
393 elif device_is_bridge ${device}; then
394 echo "bridge"
395
396 elif device_is_ppp ${device}; then
397 echo "ppp"
398
399 elif device_is_loopback ${device}; then
400 echo "loopback"
401
b8026986
MT
402 elif device_is_wireless_adhoc ${device}; then
403 echo "wireless-adhoc"
404
a508c27e
MT
405 elif device_is_wireless ${device}; then
406 echo "wireless"
407
0067696a
MT
408 elif device_is_dummy ${device}; then
409 echo "dummy"
410
2d6dab20
MT
411 elif device_is_tun ${device}; then
412 echo "tun"
413
ec63256a
MT
414 elif device_is_ethernet ${device}; then
415 echo "ethernet"
1848564d 416
6c74a64c
MT
417 elif device_is_serial ${device}; then
418 echo "serial"
419
f90dd58c
MT
420 elif device_is_phy ${device}; then
421 echo "phy"
422
e173e612
JS
423 else
424 echo "$(device_tunnel_get_type "${device}")"
425 fi
426}
427
428# This function just checks the types a ip-tunnel device usually have
429# so when we know that the device is an ip-tunnel device we save time
430device_tunnel_get_type() {
431 local device=${1}
432
433 # If the device does not exist (happens on udev remove events),
434 # we do not bother to run all checks.
435 if ! device_exists "${device}"; then
436 echo "unknown"
437
1a02da59
MT
438 elif device_is_vti ${device}; then
439 echo "vti"
440
e173e612
JS
441 elif device_is_vti6 ${device}; then
442 echo "vti6"
443
1848564d
MT
444 else
445 echo "unknown"
446 fi
447}
448
1c6a4e30 449device_is_ethernet_compatible() {
a4f7ad26
MT
450 local device="${1}"
451
452 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
453 local type="$(__device_get_file "${device}" "type")"
454 [[ "${type}" = "1" ]]
455}
456
1c6a4e30 457device_get_status() {
711ffac1 458 local device=${1}
711ffac1
MT
459 assert isset device
460
3cb2fc42 461 local status=${STATUS_DOWN}
711ffac1 462
3cb2fc42 463 if device_is_up ${device}; then
711ffac1 464 status=${STATUS_UP}
711ffac1 465
3cb2fc42
MT
466 if ! device_has_carrier ${device}; then
467 status=${STATUS_NOCARRIER}
468 fi
469 fi
711ffac1
MT
470
471 echo "${status}"
472}
473
1c6a4e30 474device_get_address() {
1848564d
MT
475 local device=${1}
476
477 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
478}
479
1c6a4e30 480device_set_address() {
08c5b789
MT
481 assert [ $# -eq 2 ]
482
483 local device="${1}"
484 local addr="${2}"
1b7a1578 485
08c5b789 486 if ! device_exists "${device}"; then
1b7a1578
MT
487 error "Device '${device}' does not exist."
488 return ${EXIT_ERROR}
489 fi
490
08c5b789
MT
491 # Do nothing if the address has not changed
492 local old_addr="$(device_get_address "${device}")"
493 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
494 return ${EXIT_OK}
495 fi
496
497 log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'"
1b7a1578
MT
498
499 local up
08c5b789
MT
500 if device_is_up "${device}"; then
501 device_set_down "${device}"
1b7a1578
MT
502 up=1
503 fi
504
08c5b789 505 ip link set "${device}" address "${addr}"
1b7a1578
MT
506 local ret=$?
507
508 if [ "${up}" = "1" ]; then
08c5b789 509 device_set_up "${device}"
1b7a1578
MT
510 fi
511
512 if [ "${ret}" != "0" ]; then
08c5b789 513 error_log "Could not set address '${addr}' on device '${device}'"
1b7a1578
MT
514 fi
515
516 return ${ret}
1848564d
MT
517}
518
1c6a4e30 519device_get() {
2ae0fb8d 520 local device
60b1f378 521 for device in $(list_directory "${SYS_CLASS_NET}"); do
2ae0fb8d
MT
522 # bonding_masters is no device
523 [ "${device}" = "bonding_masters" ] && continue
524
60b1f378 525 echo "${device}"
2ae0fb8d 526 done
711ffac1 527
711ffac1
MT
528 return ${EXIT_OK}
529}
530
1848564d 531# Check if a device has a cable plugged in
1c6a4e30 532device_has_carrier() {
5bb2429a
MT
533 local device=${1}
534 assert isset device
535
ec63256a
MT
536 local carrier=$(__device_get_file ${device} carrier)
537 [ "${carrier}" = "1" ]
1848564d
MT
538}
539
1c6a4e30 540device_is_promisc() {
1e4c26a4
MT
541 local device=${1}
542
e369be1a 543 device_has_flag ${device} 0x200
1e4c26a4
MT
544}
545
1c6a4e30 546device_set_promisc() {
cf6e4606
MT
547 local device=${1}
548 local state=${2}
549
550 assert device_exists ${device}
551 assert isset state
552 assert isoneof state on off
553
554 ip link set ${device} promisc ${state}
555}
556
1848564d 557# Check if the device is free
1c6a4e30 558device_is_free() {
2212045f 559 ! device_is_used "$@"
1848564d
MT
560}
561
562# Check if the device is used
1c6a4e30 563device_is_used() {
5bb2429a 564 local device=${1}
1848564d 565
7951525a 566 device_has_vlans ${device} && \
fb02e543 567 return ${EXIT_OK}
1848564d 568 device_is_bonded ${device} && \
fb02e543 569 return ${EXIT_OK}
81ed640c
MT
570 device_is_bridge_attached ${device} && \
571 return ${EXIT_OK}
1848564d 572
fb02e543 573 return ${EXIT_ERROR}
1848564d
MT
574}
575
1b7a1578 576# Give the device a new name
1c6a4e30 577device_set_name() {
1848564d 578 local source=$1
1578dae9 579 local destination=${2}
1848564d
MT
580
581 # Check if devices exists
582 if ! device_exists ${source} || device_exists ${destination}; then
583 return 4
584 fi
585
586 local up
587 if device_is_up ${source}; then
588 ip link set ${source} down
589 up=1
590 fi
591
592 ip link set ${source} name ${destination}
593
594 if [ "${up}" = "1" ]; then
595 ip link set ${destination} up
596 fi
597}
598
0e523702
MT
599device_set_master() {
600 local device="${1}"
601 assert isset device
602
603 local master="${2}"
604 assert isset master
605
606 if ! cmd ip link set "${device}" master "${master}"; then
607 log ERROR "Could not set master ${master} for device ${device}"
608 return ${EXIT_ERROR}
609 fi
610
611 return ${EXIT_OK}
612}
613
614device_remove_master() {
615 local device="${1}"
616 assert isset device
617
618 if ! cmd ip link set "${device}" nomaster; then
619 log ERROR "Could not remove master for device ${device}"
620 return ${EXIT_ERROR}
621 fi
622
623 return ${EXIT_OK}
624}
625
1848564d 626# Set device up
1c6a4e30 627device_set_up() {
45546be1 628 assert [ $# -eq 1 ]
1848564d 629
45546be1 630 local device=${1}
711ffac1 631
1848564d
MT
632 # Do nothing if device is already up
633 device_is_up ${device} && return ${EXIT_OK}
634
f18ee3d4 635 log INFO "Bringing up ${device}"
81ed640c 636
f18ee3d4 637 device_set_parent_up ${device}
45546be1
MT
638 if ! cmd ip link set ${device} up; then
639 return ${EXIT_ERROR}
640 fi
de72bd91
MT
641
642 # Set SMP affinity
643 if interrupt_use_smp_affinity; then
505ead5d 644 device_auto_configure_smp_affinity ${device}
de72bd91
MT
645 fi
646
647 return ${EXIT_OK}
1848564d
MT
648}
649
1c6a4e30 650device_set_parent_up() {
81ed640c
MT
651 local device=${1}
652 local parent
653
7951525a
MT
654 if device_is_vlan ${device}; then
655 parent=$(vlan_get_parent ${device})
81ed640c
MT
656
657 device_is_up ${parent} && return ${EXIT_OK}
658
659 log DEBUG "Setting up parent device '${parent}' of '${device}'"
660
661 device_set_up ${parent}
662 return $?
663 fi
664
665 return ${EXIT_OK}
666}
667
1848564d 668# Set device down
1c6a4e30 669device_set_down() {
45546be1 670 assert [ $# -eq 1 ]
1848564d 671
45546be1 672 local device=${1}
81ed640c
MT
673 local ret=${EXIT_OK}
674
675 if device_is_up ${device}; then
f18ee3d4 676 log INFO "Bringing down ${device}"
81ed640c 677
45546be1 678 cmd ip link set ${device} down
81ed640c
MT
679 ret=$?
680 fi
681
682 device_set_parent_down ${device}
1848564d 683
81ed640c
MT
684 return ${ret}
685}
686
1c6a4e30 687device_set_parent_down() {
81ed640c
MT
688 local device=${1}
689 local parent
690
7951525a
MT
691 if device_is_vlan ${device}; then
692 parent=$(vlan_get_parent ${device})
81ed640c
MT
693
694 device_is_up ${parent} || return ${EXIT_OK}
695
696 if device_is_free ${parent}; then
697 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
698
699 device_set_down ${parent}
700 fi
701 fi
702
703 return ${EXIT_OK}
1848564d
MT
704}
705
1c6a4e30 706device_get_mtu() {
1848564d
MT
707 local device=${1}
708
491e4f92
MT
709 # Return an error if the device does not exist
710 device_exists ${device} || return ${EXIT_ERROR}
1848564d 711
f3e6fe50 712 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
713}
714
715# Set mtu to a device
1c6a4e30 716device_set_mtu() {
1b7a1578 717 local device=${1}
1848564d
MT
718 local mtu=${2}
719
491e4f92 720 assert device_exists ${device}
1b7a1578 721
3464bc89
AF
722 # Handle bridges differently
723 if device_is_bridge ${device}; then
724 local port
725 for port in $(bridge_get_members ${device}); do
726 device_set_mtu ${port} ${mtu}
727 done
728 fi
729
491e4f92 730 log INFO "Setting MTU of ${device} to ${mtu}"
1b7a1578 731
1848564d 732 local up
1b7a1578
MT
733 if device_is_up ${device}; then
734 device_set_down ${device}
1848564d
MT
735 up=1
736 fi
737
491e4f92
MT
738 local ret=${EXIT_OK}
739 if ! cmd ip link set ${device} mtu ${mtu}; then
740 ret=${EXIT_ERROR}
1848564d 741
491e4f92 742 log ERROR "Could not set MTU ${mtu} on ${device}"
1b7a1578
MT
743 fi
744
491e4f92
MT
745 if [ "${up}" = "1" ]; then
746 device_set_up ${device}
1848564d
MT
747 fi
748
749 return ${ret}
750}
751
1c6a4e30 752device_adjust_mtu() {
3ee5ccb1
MT
753 assert [ $# -eq 2 ]
754
755 local device="${1}"
756 local other_device="${2}"
757
758 local mtu="$(device_get_mtu "${other_device}")"
759 device_set_mtu "${device}" "${mtu}"
760}
761
1c6a4e30 762device_discover() {
1848564d
MT
763 local device=${1}
764
1b7a1578
MT
765 log INFO "Running discovery process on device '${device}'."
766
1848564d 767 local hook
d61a01d4
MT
768 for hook in $(hook_zone_get_all); do
769 hook_zone_exec ${hook} discover ${device}
1848564d
MT
770 done
771}
772
f5ee091e
MT
773device_identify() {
774 assert [ $# -ge 1 ]
775
776 local device="${1}"
777
778 # Flash for ten seconds by default
779 local seconds="10"
780
781 # Run in background?
782 local background="false"
783
784 local arg
785 while read arg; do
786 case "${arg}" in
787 --background)
788 background="true"
789 ;;
790 --seconds=*)
791 seconds="$(cli_get_val "${arg}")"
792 ;;
793 esac
2212045f 794 done <<< "$(args "$@")"
f5ee091e
MT
795
796 assert isinteger seconds
797
798 if ! device_exists "${device}"; then
799 log ERROR "Cannot identify device ${device}: Does not exist"
800 return ${EXIT_ERROR}
801 fi
802
803 if ! device_is_ethernet "${device}"; then
804 log DEBUG "Cannot identify device ${device}: Not an ethernet device"
805 return ${EXIT_NOT_SUPPORTED}
806 fi
807
808 log DEBUG "Identifying device ${device}"
809
810 local command="ethtool --identify ${device} ${seconds}"
811 local ret=0
812
813 if enabled background; then
814 cmd_background "${command}"
815 else
816 cmd_quiet "${command}"
817 ret=$?
818 fi
819
820 return ${ret}
821}
822
1c6a4e30 823device_has_ip() {
1848564d
MT
824 local device=${1}
825 local addr=${2}
826
38f61548
MT
827 assert isset addr
828 assert device_exists ${device}
829
830 # IPv6 addresses must be fully imploded
831 local protocol=$(ip_detect_protocol ${addr})
832 case "${protocol}" in
833 ipv6)
13a6e69f 834 addr=$(ipv6_format "${addr}")
38f61548
MT
835 ;;
836 esac
1848564d 837
8c9205b1 838 list_match ${addr} $(device_get_addresses ${device})
1848564d 839}
4231f419 840
1c6a4e30 841device_get_addresses() {
4231f419 842 local device=${1}
4231f419 843
38f61548 844 assert device_exists ${device}
4231f419 845
38f61548
MT
846 local prot
847 local addr
848 local line
849 ip addr show ${device} | \
850 while read prot addr line; do
851 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
852 done
4231f419 853}
711ffac1 854
1c6a4e30 855__device_get_file() {
711ffac1
MT
856 local device=${1}
857 local file=${2}
858
750aae10 859 fread "${SYS_CLASS_NET}/${device}/${file}"
711ffac1
MT
860}
861
1c6a4e30 862__device_set_file() {
2c083d57
MT
863 assert [ $# -eq 3 ]
864
865 local device="${1}"
866 local file="${2}"
867 local value="${3}"
868
644d3bb8 869 fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
2c083d57
MT
870}
871
1c6a4e30 872device_get_rx_bytes() {
711ffac1
MT
873 local device=${1}
874
875 __device_get_file ${device} statistics/rx_bytes
876}
877
1c6a4e30 878device_get_tx_bytes() {
711ffac1
MT
879 local device=${1}
880
881 __device_get_file ${device} statistics/tx_bytes
882}
883
1c6a4e30 884device_get_rx_packets() {
711ffac1
MT
885 local device=${1}
886
887 __device_get_file ${device} statistics/rx_packets
888}
889
1c6a4e30 890device_get_tx_packets() {
711ffac1
MT
891 local device=${1}
892
893 __device_get_file ${device} statistics/tx_packets
894}
895
1c6a4e30 896device_get_rx_errors() {
711ffac1
MT
897 local device=${1}
898
899 __device_get_file ${device} statistics/rx_errors
900}
901
1c6a4e30 902device_get_tx_errors() {
711ffac1
MT
903 local device=${1}
904
905 __device_get_file ${device} statistics/tx_errors
906}
ec63256a 907
5b1fd814
MT
908device_advertise_link_speeds() {
909 local device="${1}"
910 shift
911
912 assert isset device
913
914 # Advertised modes in hex
915 local advertise=0
916
917 local mode
918 for mode in $@; do
919 local m="${DEVICE_LINK_SPEEDS[${mode}]}"
920 if isset m; then
921 advertise="$(( advertise | m ))"
922 fi
923 done
924
925 # If nothing was selected, we reset and enable everything
926 if [ ${advertise} -eq 0 ]; then
927 advertise=0xffffff
928 fi
929
930 # Enable auto-negotiation
931 cmd_quiet ethtool --change "${device}" autoneg on
932
933 # Set advertised link speeds
934 if ! cmd_quiet ethtool --change "${device}" advertise "0x$(hex "${advertise}")"; then
935 log ERROR "Could not set link modes of ${device}: $@"
936 return ${EXIT_ERROR}
937 fi
938
939 log DEBUG "Set device link modes of ${device} to $@"
940 return ${EXIT_ERROR}
941}
942
1c6a4e30 943device_get_speed() {
ec63256a
MT
944 local device=${1}
945
0d2d02da
MT
946 local speed=$(__device_get_file ${device} speed)
947
f887607c
MT
948 # Exit for no output (i.e. no link detected)
949 isset speed || return ${EXIT_ERROR}
950
0d2d02da
MT
951 # Don't return anything for negative values
952 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
953
954 print "${speed}"
ec63256a
MT
955}
956
1c6a4e30 957device_get_duplex() {
ec63256a
MT
958 local device=${1}
959
cd0f7e51
MT
960 local duplex=$(__device_get_file ${device} duplex)
961
962 case "${duplex}" in
963 unknown)
964 return ${EXIT_ERROR}
965 ;;
966 *)
967 print "${duplex}"
968 ;;
969 esac
ec63256a 970}
657540d8 971
1c6a4e30 972device_get_link_string() {
657540d8
MT
973 local device="${1}"
974 assert isset device
975
976 local s
977
978 local speed="$(device_get_speed "${device}")"
979 if isset speed; then
980 list_append s "${speed} MBit/s"
981 fi
982
983 local duplex="$(device_get_duplex "${device}")"
984 if isset duplex; then
985 list_append s "${duplex} duplex"
986 fi
987
988 print "${s}"
989}
de72bd91
MT
990
991device_auto_configure_smp_affinity() {
992 assert [ $# -eq 1 ]
993
994 local device=${1}
995
c73dc5dc 996 if lock_acquire "smp-affinity" 60; then
505ead5d 997 device_set_smp_affinity ${device} auto
de72bd91
MT
998
999 lock_release "smp-affinity"
1000 fi
1001}
1002
1003device_set_smp_affinity() {
1004 assert [ $# -eq 2 ]
1005
1006 local device=${1}
1007 local mode=${2}
1008
1009 # mode can be auto which will automatically try to find
1010 # the least busy processor, or an integer for the desired
1011 # processor that should handle this device
1012
1013 local num_processors=$(system_get_processors)
1014
1015 if [ "${mode}" = "auto" ]; then
1016 local processor=$(interrupt_choose_least_busy_processor)
1017 else
1018 assert isinteger mode
1019 local processor=${mode}
1020
1021 if [ ${processor} -gt ${num_processors} ]; then
1022 log ERROR "Processor ${processor} does not exist"
1023 return ${EXIT_ERROR}
1024 fi
1025 fi
1026
1027 local interrupts=$(interrupts_for_device ${device})
1028 if ! isset interrupts; then
1029 log DEBUG "${device} has no interrupts. Not changing SMP affinity"
1030 return ${EXIT_OK}
1031 fi
1032
1033 # Set SMP affinity
1034 local interrupt
1035 for interrupt in ${interrupts}; do
1036 interrupt_set_smp_affinity ${interrupt} ${processor}
1037 done
1038
1039 # Find all queues and assign them to the next processor
1040 local queue
1041 for queue in $(device_get_queues ${device}); do
1042 case "${queue}" in
1043 # Only handle receive queues
1044 rx-*)
1045 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
1046 interrupt_set_smp_affinity ${interrupt} ${processor}
1047 done
1048
1049 device_queue_set_smp_affinity ${device} ${queue} ${processor}
1050 ;;
1051
1052 # Ignore the rest
1053 *)
1054 continue
1055 ;;
1056 esac
1057
1058 # Get the next available processor if in auto mode
1059 [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
1060 done
1061
1062 return ${EXIT_OK}
1063}
1064
1065device_get_queues() {
1066 assert [ $# -eq 1 ]
1067
1068 local device=${1}
1069
60b1f378 1070 list_directory "${SYS_CLASS_NET}/${device}/queues"
de72bd91
MT
1071}
1072
6529dfaa
MT
1073device_supports_multiqueue() {
1074 local device=${1}
1075
1076 local num_queues=$(device_num_queues ${device})
1077
1078 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1079 return ${EXIT_TRUE}
1080 fi
1081
1082 return ${EXIT_FALSE}
1083}
1084
1085device_num_queues() {
1086 local device=${1}
1087 local type=${2}
1088
e396e74e 1089 isset type && assert isoneof type rx tx
6529dfaa
MT
1090
1091 local i=0
1092
1093 local q
1094 for q in $(device_get_queues ${device}); do
1095 case "${type},${q}" in
1096 rx,rx-*)
1097 (( i++ ))
1098 ;;
1099 tx,tx-*)
1100 (( i++ ))
1101 ;;
1102 *,*)
1103 (( i++ ))
1104 ;;
1105 esac
1106 done
1107
1108 print ${i}
1109}
1110
1111device_queue_get_smp_affinity() {
1112 assert [ $# -eq 2 ]
1113
1114 local device=${1}
1115 local queue=${2}
1116
e396e74e
MT
1117 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1118
1119 case "${queue}" in
1120 rx-*)
1121 path="${path}/rps_cpus"
1122 ;;
1123 tx-*)
1124 path="${path}/xps_cpus"
1125 ;;
1126 esac
6529dfaa
MT
1127 assert [ -r "${path}" ]
1128
1129 __bitmap_to_processor_ids $(<${path})
1130}
1131
de72bd91
MT
1132device_queue_set_smp_affinity() {
1133 assert [ $# -eq 3 ]
1134
1135 local device=${1}
1136 local queue=${2}
1137 local processor=${3}
1138
1139 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1140 assert [ -w "${path}" ]
1141
1142 log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1143
1144 __processor_id_to_bitmap ${processor} > ${path}
1145}
905b13b3
JS
1146
1147# Tries to find a device which has the given IP address assigned
1148device_get_by_assigned_ip_address() {
1149 local ip=${1}
1150
1151 assert isset ip
1152
1153 local device
1154
1155 # Read the first line of ip addr show to
1156 read -r device <<< $(ip addr show to "${ip}")
1157
1158 # If we did not found a device we return with ${EXIT_ERROR}
1159 if ! isset device; then
1160 return ${EXIT_ERROR}
1161 fi
1162
1163 # We get something like:
1164 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1165 # and we want upl0 so we take the second word and removing the :
1166 device=(${device})
1167 device=${device[1]}
1168 device=${device%:}
1169
1170 print "${device}"
1171 return ${EXIT_OK}
1172}
e8253eba
JS
1173
1174device_get_by_mac_address() {
1175 local mac=${1}
1176
1177 assert isset mac
1178
1179 local device
1180
1181 for device in $(device_list); do
1182 if [ "${mac}" = "$(device_get_address ${device})" ]; then
1183 print "${device}"
1184 return ${EXIT_OK}
1185 fi
1186 done
1187
1188 # We could not found a port to the given mac address so we return exit error
1189 return ${EXIT_ERROR}
1190}