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