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