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