]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.device
device: add new function device_is_vti6
[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
cdcce259
MT
80 # Shut down device before we delete it
81 device_set_down "${device}"
82
99be6026
MT
83 # Delete the device.
84 cmd_quiet ip link delete ${device}
85 local ret=$?
86
87 if [ ${ret} -ne ${EXIT_OK} ]; then
88 log ERROR "device: Could not delete device '${device}': ${ret}"
89 return ${EXIT_ERROR}
90 fi
91
92 return ${ret}
93}
94
1c6a4e30 95device_has_flag() {
e369be1a
MT
96 local device=${1}
97 local flag=${2}
98
99 local flags=$(__device_get_file ${device} flags)
100
101 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
102 return ${EXIT_FALSE}
103 else
104 return ${EXIT_TRUE}
105 fi
106}
107
1848564d 108# Check if the device is up
1c6a4e30 109device_is_up() {
1848564d
MT
110 local device=${1}
111
112 device_exists ${device} || return ${EXIT_ERROR}
113
e369be1a 114 device_has_flag ${device} 0x1
1848564d
MT
115}
116
1c6a4e30 117device_ifindex_to_name() {
99be6026
MT
118 local idx=${1}
119 assert isset idx
120
121 local device device_idx
60b1f378 122 for device in $(list_directory "${SYS_CLASS_NET}"); do
99be6026
MT
123 device_idx=$(device_get_ifindex ${device})
124
125 if [ "${device_idx}" = "${idx}" ]; then
126 print "${device}"
127 return ${EXIT_OK}
128 fi
129 done
130
131 return ${EXIT_ERROR}
132}
133
1c6a4e30 134device_get_ifindex() {
99be6026
MT
135 local device=${1}
136 assert isset device
137
138 local path="${SYS_CLASS_NET}/${1}/ifindex"
139
140 # Check if file can be read.
141 [ -r "${path}" ] || return ${EXIT_ERROR}
142
143 print "$(<${path})"
144}
145
1848564d 146# Check if the device is a bonding device
1c6a4e30 147device_is_bonding() {
1848564d
MT
148 [ -d "/sys/class/net/${1}/bonding" ]
149}
150
151# Check if the device bonded in a bonding device
1c6a4e30 152device_is_bonded() {
711ffac1 153 local device=${1}
1848564d 154
0959482b 155 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ]
1848564d
MT
156}
157
158# Check if the device is a bridge
1c6a4e30 159device_is_bridge() {
1848564d
MT
160 [ -d "/sys/class/net/${1}/bridge" ]
161}
162
1c6a4e30 163device_is_bridge_attached() {
81ed640c 164 local device=${1}
81ed640c
MT
165 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
166}
167
1c6a4e30 168device_is_wireless_monitor() {
a23fdc0e
MT
169 local device="${1}"
170 assert isset device
171
172 device_is_wireless "${device}" && \
173 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
174}
175
1c6a4e30 176device_is_wireless_adhoc() {
b8026986
MT
177 local device="${1}"
178 assert isset device
179
180 device_is_wireless "${device}" && \
181 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
182}
183
1c6a4e30 184device_get_bridge() {
99be6026
MT
185 local device=${1}
186 assert isset device
187
188 # Check if device is attached to a bridge.
189 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
190
191 local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
192 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
193
194 local ifindex=$(<${ifindex_path})
195 assert isset ifindex
196
197 device_ifindex_to_name ${ifindex}
198}
199
7951525a 200# Check if the device is a vlan device
1c6a4e30 201device_is_vlan() {
1848564d 202 local device=${1}
7951525a 203 assert isset device
1848564d 204
7951525a 205 [ -e "${PROC_NET_VLAN}/${device}" ]
1848564d
MT
206}
207
7951525a 208# Check if the device has vlan devices
1c6a4e30 209device_has_vlans() {
fb02e543 210 local device=${1}
7951525a 211 assert isset device
fb02e543 212
7951525a 213 if device_is_vlan ${device}; then
ec63256a 214 return ${EXIT_FALSE}
fb02e543
MT
215 fi
216
7951525a
MT
217 local vlans=$(device_get_vlans ${device})
218 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
ec63256a
MT
219}
220
1c6a4e30 221device_get_vlans() {
ec63256a 222 local device=${1}
7951525a 223 assert isset device
ec63256a 224
8357a7ff
MT
225 # If no 8021q module has been loaded into the kernel,
226 # we cannot do anything.
7951525a 227 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
8357a7ff 228
ec63256a
MT
229 local dev spacer1 id spacer2 parent
230 while read dev spacer1 id spacer2 parent; do
7951525a
MT
231 [ "${parent}" = "${device}" ] || continue
232
233 print "${dev}"
234 done < ${PROC_NET_VLAN_CONFIG}
1848564d
MT
235}
236
1848564d 237# Check if the device is a ppp device
1c6a4e30 238device_is_ppp() {
1848564d
MT
239 local device=${1}
240
55b802cc 241 local type=$(__device_get_file ${device} type)
28f0b4ab 242
e369be1a
MT
243 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
244}
55b802cc 245
e369be1a 246# Check if the device is a pointopoint device.
1c6a4e30 247device_is_ptp() {
e369be1a
MT
248 local device=${1}
249
250 device_has_flag ${device} 0x10
1848564d
MT
251}
252
253# Check if the device is a loopback device
1c6a4e30 254device_is_loopback() {
5bb2429a
MT
255 local device=${1}
256
1848564d
MT
257 [ "${device}" = "lo" ]
258}
259
0067696a
MT
260# Check if the device is a dummy device
261# This is the worst possible check, but all I could come up with
1c6a4e30 262device_is_dummy() {
0067696a
MT
263 local device="${1}"
264
265 [[ ${device} =~ ^dummy[0-9]+$ ]]
266}
267
82fac748
MT
268device_is_ipsec() {
269 local device="${1}"
270
271 [[ ${device} =~ ^ipsec\- ]]
272}
273
a508c27e 274# Check if the device is a wireless device
1c6a4e30 275device_is_wireless() {
a508c27e
MT
276 local device=${1}
277
278 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
279}
280
1a02da59
MT
281device_is_vti() {
282 local device=${1}
283
284 local type=$(__device_get_file ${device} type)
285
286 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
287}
288
bcba5d27
JS
289device_is_vti6() {
290 local device=${1}
291
292 local type=$(__device_get_file ${device} type)
293
294 [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
295}
296
1c6a4e30 297device_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 308device_is_phy() {
2212045f 309 phy_exists "$@"
f90dd58c
MT
310}
311
1c6a4e30 312device_is_serial() {
2212045f 313 serial_exists "$@"
6c74a64c
MT
314}
315
2d6dab20
MT
316# Returns true if a device is a tun device
317device_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 324device_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 355device_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
375 elif device_is_loopback ${device}; then
376 echo "loopback"
377
b8026986
MT
378 elif device_is_wireless_adhoc ${device}; then
379 echo "wireless-adhoc"
380
a508c27e
MT
381 elif device_is_wireless ${device}; then
382 echo "wireless"
383
0067696a
MT
384 elif device_is_dummy ${device}; then
385 echo "dummy"
386
2d6dab20
MT
387 elif device_is_tun ${device}; then
388 echo "tun"
389
ec63256a
MT
390 elif device_is_ethernet ${device}; then
391 echo "ethernet"
1848564d 392
6c74a64c
MT
393 elif device_is_serial ${device}; then
394 echo "serial"
395
f90dd58c
MT
396 elif device_is_phy ${device}; then
397 echo "phy"
398
1a02da59
MT
399 elif device_is_vti ${device}; then
400 echo "vti"
401
1848564d
MT
402 else
403 echo "unknown"
404 fi
405}
406
1c6a4e30 407device_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 415device_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 432device_get_address() {
1848564d
MT
433 local device=${1}
434
435 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
436}
437
1c6a4e30 438device_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 477device_get() {
2ae0fb8d 478 local device
60b1f378 479 for device in $(list_directory "${SYS_CLASS_NET}"); do
2ae0fb8d
MT
480 # bonding_masters is no device
481 [ "${device}" = "bonding_masters" ] && continue
482
60b1f378 483 echo "${device}"
2ae0fb8d 484 done
711ffac1 485
711ffac1
MT
486 return ${EXIT_OK}
487}
488
1848564d 489# Check if a device has a cable plugged in
1c6a4e30 490device_has_carrier() {
5bb2429a
MT
491 local device=${1}
492 assert isset device
493
ec63256a
MT
494 local carrier=$(__device_get_file ${device} carrier)
495 [ "${carrier}" = "1" ]
1848564d
MT
496}
497
1c6a4e30 498device_is_promisc() {
1e4c26a4
MT
499 local device=${1}
500
e369be1a 501 device_has_flag ${device} 0x200
1e4c26a4
MT
502}
503
1c6a4e30 504device_set_promisc() {
cf6e4606
MT
505 local device=${1}
506 local state=${2}
507
508 assert device_exists ${device}
509 assert isset state
510 assert isoneof state on off
511
512 ip link set ${device} promisc ${state}
513}
514
1848564d 515# Check if the device is free
1c6a4e30 516device_is_free() {
2212045f 517 ! device_is_used "$@"
1848564d
MT
518}
519
520# Check if the device is used
1c6a4e30 521device_is_used() {
5bb2429a 522 local device=${1}
1848564d 523
7951525a 524 device_has_vlans ${device} && \
fb02e543 525 return ${EXIT_OK}
1848564d 526 device_is_bonded ${device} && \
fb02e543 527 return ${EXIT_OK}
81ed640c
MT
528 device_is_bridge_attached ${device} && \
529 return ${EXIT_OK}
1848564d 530
fb02e543 531 return ${EXIT_ERROR}
1848564d
MT
532}
533
1b7a1578 534# Give the device a new name
1c6a4e30 535device_set_name() {
1848564d 536 local source=$1
1578dae9 537 local destination=${2}
1848564d
MT
538
539 # Check if devices exists
540 if ! device_exists ${source} || device_exists ${destination}; then
541 return 4
542 fi
543
544 local up
545 if device_is_up ${source}; then
546 ip link set ${source} down
547 up=1
548 fi
549
550 ip link set ${source} name ${destination}
551
552 if [ "${up}" = "1" ]; then
553 ip link set ${destination} up
554 fi
555}
556
0e523702
MT
557device_set_master() {
558 local device="${1}"
559 assert isset device
560
561 local master="${2}"
562 assert isset master
563
564 if ! cmd ip link set "${device}" master "${master}"; then
565 log ERROR "Could not set master ${master} for device ${device}"
566 return ${EXIT_ERROR}
567 fi
568
569 return ${EXIT_OK}
570}
571
572device_remove_master() {
573 local device="${1}"
574 assert isset device
575
576 if ! cmd ip link set "${device}" nomaster; then
577 log ERROR "Could not remove master for device ${device}"
578 return ${EXIT_ERROR}
579 fi
580
581 return ${EXIT_OK}
582}
583
1848564d 584# Set device up
1c6a4e30 585device_set_up() {
45546be1 586 assert [ $# -eq 1 ]
1848564d 587
45546be1 588 local device=${1}
711ffac1 589
1848564d
MT
590 # Do nothing if device is already up
591 device_is_up ${device} && return ${EXIT_OK}
592
f18ee3d4 593 log INFO "Bringing up ${device}"
81ed640c 594
f18ee3d4 595 device_set_parent_up ${device}
45546be1
MT
596 if ! cmd ip link set ${device} up; then
597 return ${EXIT_ERROR}
598 fi
de72bd91
MT
599
600 # Set SMP affinity
601 if interrupt_use_smp_affinity; then
505ead5d 602 device_auto_configure_smp_affinity ${device}
de72bd91
MT
603 fi
604
605 return ${EXIT_OK}
1848564d
MT
606}
607
1c6a4e30 608device_set_parent_up() {
81ed640c
MT
609 local device=${1}
610 local parent
611
7951525a
MT
612 if device_is_vlan ${device}; then
613 parent=$(vlan_get_parent ${device})
81ed640c
MT
614
615 device_is_up ${parent} && return ${EXIT_OK}
616
617 log DEBUG "Setting up parent device '${parent}' of '${device}'"
618
619 device_set_up ${parent}
620 return $?
621 fi
622
623 return ${EXIT_OK}
624}
625
1848564d 626# Set device down
1c6a4e30 627device_set_down() {
45546be1 628 assert [ $# -eq 1 ]
1848564d 629
45546be1 630 local device=${1}
81ed640c
MT
631 local ret=${EXIT_OK}
632
633 if device_is_up ${device}; then
f18ee3d4 634 log INFO "Bringing down ${device}"
81ed640c 635
45546be1 636 cmd ip link set ${device} down
81ed640c
MT
637 ret=$?
638 fi
639
640 device_set_parent_down ${device}
1848564d 641
81ed640c
MT
642 return ${ret}
643}
644
1c6a4e30 645device_set_parent_down() {
81ed640c
MT
646 local device=${1}
647 local parent
648
7951525a
MT
649 if device_is_vlan ${device}; then
650 parent=$(vlan_get_parent ${device})
81ed640c
MT
651
652 device_is_up ${parent} || return ${EXIT_OK}
653
654 if device_is_free ${parent}; then
655 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
656
657 device_set_down ${parent}
658 fi
659 fi
660
661 return ${EXIT_OK}
1848564d
MT
662}
663
1c6a4e30 664device_get_mtu() {
1848564d
MT
665 local device=${1}
666
491e4f92
MT
667 # Return an error if the device does not exist
668 device_exists ${device} || return ${EXIT_ERROR}
1848564d 669
f3e6fe50 670 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
671}
672
673# Set mtu to a device
1c6a4e30 674device_set_mtu() {
1b7a1578 675 local device=${1}
1848564d
MT
676 local mtu=${2}
677
491e4f92 678 assert device_exists ${device}
1b7a1578 679
3464bc89
AF
680 # Handle bridges differently
681 if device_is_bridge ${device}; then
682 local port
683 for port in $(bridge_get_members ${device}); do
684 device_set_mtu ${port} ${mtu}
685 done
686 fi
687
491e4f92 688 log INFO "Setting MTU of ${device} to ${mtu}"
1b7a1578 689
1848564d 690 local up
1b7a1578
MT
691 if device_is_up ${device}; then
692 device_set_down ${device}
1848564d
MT
693 up=1
694 fi
695
491e4f92
MT
696 local ret=${EXIT_OK}
697 if ! cmd ip link set ${device} mtu ${mtu}; then
698 ret=${EXIT_ERROR}
1848564d 699
491e4f92 700 log ERROR "Could not set MTU ${mtu} on ${device}"
1b7a1578
MT
701 fi
702
491e4f92
MT
703 if [ "${up}" = "1" ]; then
704 device_set_up ${device}
1848564d
MT
705 fi
706
707 return ${ret}
708}
709
1c6a4e30 710device_adjust_mtu() {
3ee5ccb1
MT
711 assert [ $# -eq 2 ]
712
713 local device="${1}"
714 local other_device="${2}"
715
716 local mtu="$(device_get_mtu "${other_device}")"
717 device_set_mtu "${device}" "${mtu}"
718}
719
1c6a4e30 720device_discover() {
1848564d
MT
721 local device=${1}
722
1b7a1578
MT
723 log INFO "Running discovery process on device '${device}'."
724
1848564d 725 local hook
d61a01d4
MT
726 for hook in $(hook_zone_get_all); do
727 hook_zone_exec ${hook} discover ${device}
1848564d
MT
728 done
729}
730
f5ee091e
MT
731device_identify() {
732 assert [ $# -ge 1 ]
733
734 local device="${1}"
735
736 # Flash for ten seconds by default
737 local seconds="10"
738
739 # Run in background?
740 local background="false"
741
742 local arg
743 while read arg; do
744 case "${arg}" in
745 --background)
746 background="true"
747 ;;
748 --seconds=*)
749 seconds="$(cli_get_val "${arg}")"
750 ;;
751 esac
2212045f 752 done <<< "$(args "$@")"
f5ee091e
MT
753
754 assert isinteger seconds
755
756 if ! device_exists "${device}"; then
757 log ERROR "Cannot identify device ${device}: Does not exist"
758 return ${EXIT_ERROR}
759 fi
760
761 if ! device_is_ethernet "${device}"; then
762 log DEBUG "Cannot identify device ${device}: Not an ethernet device"
763 return ${EXIT_NOT_SUPPORTED}
764 fi
765
766 log DEBUG "Identifying device ${device}"
767
768 local command="ethtool --identify ${device} ${seconds}"
769 local ret=0
770
771 if enabled background; then
772 cmd_background "${command}"
773 else
774 cmd_quiet "${command}"
775 ret=$?
776 fi
777
778 return ${ret}
779}
780
1c6a4e30 781device_has_ip() {
1848564d
MT
782 local device=${1}
783 local addr=${2}
784
38f61548
MT
785 assert isset addr
786 assert device_exists ${device}
787
788 # IPv6 addresses must be fully imploded
789 local protocol=$(ip_detect_protocol ${addr})
790 case "${protocol}" in
791 ipv6)
13a6e69f 792 addr=$(ipv6_format "${addr}")
38f61548
MT
793 ;;
794 esac
1848564d 795
8c9205b1 796 list_match ${addr} $(device_get_addresses ${device})
1848564d 797}
4231f419 798
1c6a4e30 799device_get_addresses() {
4231f419 800 local device=${1}
4231f419 801
38f61548 802 assert device_exists ${device}
4231f419 803
38f61548
MT
804 local prot
805 local addr
806 local line
807 ip addr show ${device} | \
808 while read prot addr line; do
809 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
810 done
4231f419 811}
711ffac1 812
1c6a4e30 813__device_get_file() {
711ffac1
MT
814 local device=${1}
815 local file=${2}
816
750aae10 817 fread "${SYS_CLASS_NET}/${device}/${file}"
711ffac1
MT
818}
819
1c6a4e30 820__device_set_file() {
2c083d57
MT
821 assert [ $# -eq 3 ]
822
823 local device="${1}"
824 local file="${2}"
825 local value="${3}"
826
644d3bb8 827 fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
2c083d57
MT
828}
829
1c6a4e30 830device_get_rx_bytes() {
711ffac1
MT
831 local device=${1}
832
833 __device_get_file ${device} statistics/rx_bytes
834}
835
1c6a4e30 836device_get_tx_bytes() {
711ffac1
MT
837 local device=${1}
838
839 __device_get_file ${device} statistics/tx_bytes
840}
841
1c6a4e30 842device_get_rx_packets() {
711ffac1
MT
843 local device=${1}
844
845 __device_get_file ${device} statistics/rx_packets
846}
847
1c6a4e30 848device_get_tx_packets() {
711ffac1
MT
849 local device=${1}
850
851 __device_get_file ${device} statistics/tx_packets
852}
853
1c6a4e30 854device_get_rx_errors() {
711ffac1
MT
855 local device=${1}
856
857 __device_get_file ${device} statistics/rx_errors
858}
859
1c6a4e30 860device_get_tx_errors() {
711ffac1
MT
861 local device=${1}
862
863 __device_get_file ${device} statistics/tx_errors
864}
ec63256a 865
1c6a4e30 866device_get_speed() {
ec63256a
MT
867 local device=${1}
868
0d2d02da
MT
869 local speed=$(__device_get_file ${device} speed)
870
f887607c
MT
871 # Exit for no output (i.e. no link detected)
872 isset speed || return ${EXIT_ERROR}
873
0d2d02da
MT
874 # Don't return anything for negative values
875 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
876
877 print "${speed}"
ec63256a
MT
878}
879
1c6a4e30 880device_get_duplex() {
ec63256a
MT
881 local device=${1}
882
cd0f7e51
MT
883 local duplex=$(__device_get_file ${device} duplex)
884
885 case "${duplex}" in
886 unknown)
887 return ${EXIT_ERROR}
888 ;;
889 *)
890 print "${duplex}"
891 ;;
892 esac
ec63256a 893}
657540d8 894
1c6a4e30 895device_get_link_string() {
657540d8
MT
896 local device="${1}"
897 assert isset device
898
899 local s
900
901 local speed="$(device_get_speed "${device}")"
902 if isset speed; then
903 list_append s "${speed} MBit/s"
904 fi
905
906 local duplex="$(device_get_duplex "${device}")"
907 if isset duplex; then
908 list_append s "${duplex} duplex"
909 fi
910
911 print "${s}"
912}
de72bd91
MT
913
914device_auto_configure_smp_affinity() {
915 assert [ $# -eq 1 ]
916
917 local device=${1}
918
c73dc5dc 919 if lock_acquire "smp-affinity" 60; then
505ead5d 920 device_set_smp_affinity ${device} auto
de72bd91
MT
921
922 lock_release "smp-affinity"
923 fi
924}
925
926device_set_smp_affinity() {
927 assert [ $# -eq 2 ]
928
929 local device=${1}
930 local mode=${2}
931
932 # mode can be auto which will automatically try to find
933 # the least busy processor, or an integer for the desired
934 # processor that should handle this device
935
936 local num_processors=$(system_get_processors)
937
938 if [ "${mode}" = "auto" ]; then
939 local processor=$(interrupt_choose_least_busy_processor)
940 else
941 assert isinteger mode
942 local processor=${mode}
943
944 if [ ${processor} -gt ${num_processors} ]; then
945 log ERROR "Processor ${processor} does not exist"
946 return ${EXIT_ERROR}
947 fi
948 fi
949
950 local interrupts=$(interrupts_for_device ${device})
951 if ! isset interrupts; then
952 log DEBUG "${device} has no interrupts. Not changing SMP affinity"
953 return ${EXIT_OK}
954 fi
955
956 # Set SMP affinity
957 local interrupt
958 for interrupt in ${interrupts}; do
959 interrupt_set_smp_affinity ${interrupt} ${processor}
960 done
961
962 # Find all queues and assign them to the next processor
963 local queue
964 for queue in $(device_get_queues ${device}); do
965 case "${queue}" in
966 # Only handle receive queues
967 rx-*)
968 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
969 interrupt_set_smp_affinity ${interrupt} ${processor}
970 done
971
972 device_queue_set_smp_affinity ${device} ${queue} ${processor}
973 ;;
974
975 # Ignore the rest
976 *)
977 continue
978 ;;
979 esac
980
981 # Get the next available processor if in auto mode
982 [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
983 done
984
985 return ${EXIT_OK}
986}
987
988device_get_queues() {
989 assert [ $# -eq 1 ]
990
991 local device=${1}
992
60b1f378 993 list_directory "${SYS_CLASS_NET}/${device}/queues"
de72bd91
MT
994}
995
6529dfaa
MT
996device_supports_multiqueue() {
997 local device=${1}
998
999 local num_queues=$(device_num_queues ${device})
1000
1001 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1002 return ${EXIT_TRUE}
1003 fi
1004
1005 return ${EXIT_FALSE}
1006}
1007
1008device_num_queues() {
1009 local device=${1}
1010 local type=${2}
1011
e396e74e 1012 isset type && assert isoneof type rx tx
6529dfaa
MT
1013
1014 local i=0
1015
1016 local q
1017 for q in $(device_get_queues ${device}); do
1018 case "${type},${q}" in
1019 rx,rx-*)
1020 (( i++ ))
1021 ;;
1022 tx,tx-*)
1023 (( i++ ))
1024 ;;
1025 *,*)
1026 (( i++ ))
1027 ;;
1028 esac
1029 done
1030
1031 print ${i}
1032}
1033
1034device_queue_get_smp_affinity() {
1035 assert [ $# -eq 2 ]
1036
1037 local device=${1}
1038 local queue=${2}
1039
e396e74e
MT
1040 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1041
1042 case "${queue}" in
1043 rx-*)
1044 path="${path}/rps_cpus"
1045 ;;
1046 tx-*)
1047 path="${path}/xps_cpus"
1048 ;;
1049 esac
6529dfaa
MT
1050 assert [ -r "${path}" ]
1051
1052 __bitmap_to_processor_ids $(<${path})
1053}
1054
de72bd91
MT
1055device_queue_set_smp_affinity() {
1056 assert [ $# -eq 3 ]
1057
1058 local device=${1}
1059 local queue=${2}
1060 local processor=${3}
1061
1062 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1063 assert [ -w "${path}" ]
1064
1065 log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1066
1067 __processor_id_to_bitmap ${processor} > ${path}
1068}
905b13b3
JS
1069
1070# Tries to find a device which has the given IP address assigned
1071device_get_by_assigned_ip_address() {
1072 local ip=${1}
1073
1074 assert isset ip
1075
1076 local device
1077
1078 # Read the first line of ip addr show to
1079 read -r device <<< $(ip addr show to "${ip}")
1080
1081 # If we did not found a device we return with ${EXIT_ERROR}
1082 if ! isset device; then
1083 return ${EXIT_ERROR}
1084 fi
1085
1086 # We get something like:
1087 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1088 # and we want upl0 so we take the second word and removing the :
1089 device=(${device})
1090 device=${device[1]}
1091 device=${device%:}
1092
1093 print "${device}"
1094 return ${EXIT_OK}
1095}