]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.device
897f812d7d7bb8a827bc19be43e3663bef434d58
[people/stevee/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 list_append devices $(devices_get_all)
27
28 # Add all PHYs
29 list_append devices $(phy_list)
30
31 # Add all serial devices
32 list_append devices $(serial_list)
33
34 # Return a sorted result
35 list_sort ${devices}
36 }
37
38 # Check if the device exists
39 device_exists() {
40 local device=${1}
41
42 # If device name was not found, exit.
43 [ -n "${device}" ] || return ${EXIT_ERROR}
44
45 # Check for a normal network device.
46 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
47
48 # If the check above did not find a result,
49 # we check for PHYs.
50 phy_exists "${device}" && return ${EXIT_OK}
51
52 # If the check above did not find a result,
53 # we check for serial devices.
54 serial_exists ${device}
55 }
56
57 device_matches_pattern() {
58 local device="${1}"
59 assert isset device
60
61 local pattern="${2}"
62 assert isset pattern
63
64 pattern="^${pattern//N/[[:digit:]]+}$"
65
66 [[ ${device} =~ ${pattern} ]] \
67 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
68 }
69
70 device_delete() {
71 local device=${1}
72 assert isset device
73
74 # Nothing to do, it device does not exist.
75 device_exists ${device} || return ${EXIT_OK}
76
77 # Delete the device.
78 cmd_quiet ip link delete ${device}
79 local ret=$?
80
81 if [ ${ret} -ne ${EXIT_OK} ]; then
82 log ERROR "device: Could not delete device '${device}': ${ret}"
83 return ${EXIT_ERROR}
84 fi
85
86 return ${ret}
87 }
88
89 device_has_flag() {
90 local device=${1}
91 local flag=${2}
92
93 local flags=$(__device_get_file ${device} flags)
94
95 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
96 return ${EXIT_FALSE}
97 else
98 return ${EXIT_TRUE}
99 fi
100 }
101
102 # Check if the device is up
103 device_is_up() {
104 local device=${1}
105
106 device_exists ${device} || return ${EXIT_ERROR}
107
108 device_has_flag ${device} 0x1
109 }
110
111 device_ifindex_to_name() {
112 local idx=${1}
113 assert isset idx
114
115 local device device_idx
116 for device in ${SYS_CLASS_NET}/*; do
117 device=$(basename ${device})
118 device_exists ${device} || continue
119
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 local devices
497
498 for device in ${SYS_CLASS_NET}/*; do
499 device=$(basename ${device})
500
501 # bonding_masters is no device
502 [ "${device}" = "bonding_masters" ] && continue
503
504 devices="${devices} ${device}"
505 done
506
507 echo ${devices}
508 return ${EXIT_OK}
509 }
510
511 devices_get_all() {
512 device_get
513 }
514
515 # Check if a device has a cable plugged in
516 device_has_carrier() {
517 local device=${1}
518 assert isset device
519
520 local carrier=$(__device_get_file ${device} carrier)
521 [ "${carrier}" = "1" ]
522 }
523
524 device_is_promisc() {
525 local device=${1}
526
527 device_has_flag ${device} 0x200
528 }
529
530 device_set_promisc() {
531 local device=${1}
532 local state=${2}
533
534 assert device_exists ${device}
535 assert isset state
536 assert isoneof state on off
537
538 ip link set ${device} promisc ${state}
539 }
540
541 # Check if the device is free
542 device_is_free() {
543 ! device_is_used $@
544 }
545
546 # Check if the device is used
547 device_is_used() {
548 local device=${1}
549
550 device_has_vlans ${device} && \
551 return ${EXIT_OK}
552 device_is_bonded ${device} && \
553 return ${EXIT_OK}
554 device_is_bridge_attached ${device} && \
555 return ${EXIT_OK}
556
557 return ${EXIT_ERROR}
558 }
559
560 # Give the device a new name
561 device_set_name() {
562 local source=$1
563 local destination=${2}
564
565 # Check if devices exists
566 if ! device_exists ${source} || device_exists ${destination}; then
567 return 4
568 fi
569
570 local up
571 if device_is_up ${source}; then
572 ip link set ${source} down
573 up=1
574 fi
575
576 ip link set ${source} name ${destination}
577
578 if [ "${up}" = "1" ]; then
579 ip link set ${destination} up
580 fi
581 }
582
583 device_set_master() {
584 local device="${1}"
585 assert isset device
586
587 local master="${2}"
588 assert isset master
589
590 if ! cmd ip link set "${device}" master "${master}"; then
591 log ERROR "Could not set master ${master} for device ${device}"
592 return ${EXIT_ERROR}
593 fi
594
595 return ${EXIT_OK}
596 }
597
598 device_remove_master() {
599 local device="${1}"
600 assert isset device
601
602 if ! cmd ip link set "${device}" nomaster; then
603 log ERROR "Could not remove master for device ${device}"
604 return ${EXIT_ERROR}
605 fi
606
607 return ${EXIT_OK}
608 }
609
610 # Set device up
611 device_set_up() {
612 assert [ $# -eq 1 ]
613
614 local device=${1}
615
616 # Do nothing if device is already up
617 device_is_up ${device} && return ${EXIT_OK}
618
619 log INFO "Bringing up ${device}"
620
621 device_set_parent_up ${device}
622 if ! cmd ip link set ${device} up; then
623 return ${EXIT_ERROR}
624 fi
625
626 # Set SMP affinity
627 if interrupt_use_smp_affinity; then
628 device_auto_configure_smp_affinity ${device}
629 fi
630
631 return ${EXIT_OK}
632 }
633
634 device_set_parent_up() {
635 local device=${1}
636 local parent
637
638 if device_is_vlan ${device}; then
639 parent=$(vlan_get_parent ${device})
640
641 device_is_up ${parent} && return ${EXIT_OK}
642
643 log DEBUG "Setting up parent device '${parent}' of '${device}'"
644
645 device_set_up ${parent}
646 return $?
647 fi
648
649 return ${EXIT_OK}
650 }
651
652 # Set device down
653 device_set_down() {
654 assert [ $# -eq 1 ]
655
656 local device=${1}
657 local ret=${EXIT_OK}
658
659 if device_is_up ${device}; then
660 log INFO "Bringing down ${device}"
661
662 cmd ip link set ${device} down
663 ret=$?
664 fi
665
666 device_set_parent_down ${device}
667
668 return ${ret}
669 }
670
671 device_set_parent_down() {
672 local device=${1}
673 local parent
674
675 if device_is_vlan ${device}; then
676 parent=$(vlan_get_parent ${device})
677
678 device_is_up ${parent} || return ${EXIT_OK}
679
680 if device_is_free ${parent}; then
681 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
682
683 device_set_down ${parent}
684 fi
685 fi
686
687 return ${EXIT_OK}
688 }
689
690 device_get_mtu() {
691 local device=${1}
692
693 # Return an error if the device does not exist
694 device_exists ${device} || return ${EXIT_ERROR}
695
696 echo $(<${SYS_CLASS_NET}/${device}/mtu)
697 }
698
699 # Set mtu to a device
700 device_set_mtu() {
701 local device=${1}
702 local mtu=${2}
703
704 assert device_exists ${device}
705
706 # Handle bridges differently
707 if device_is_bridge ${device}; then
708 local port
709 for port in $(bridge_get_members ${device}); do
710 device_set_mtu ${port} ${mtu}
711 done
712 fi
713
714 log INFO "Setting MTU of ${device} to ${mtu}"
715
716 local up
717 if device_is_up ${device}; then
718 device_set_down ${device}
719 up=1
720 fi
721
722 local ret=${EXIT_OK}
723 if ! cmd ip link set ${device} mtu ${mtu}; then
724 ret=${EXIT_ERROR}
725
726 log ERROR "Could not set MTU ${mtu} on ${device}"
727 fi
728
729 if [ "${up}" = "1" ]; then
730 device_set_up ${device}
731 fi
732
733 return ${ret}
734 }
735
736 device_adjust_mtu() {
737 assert [ $# -eq 2 ]
738
739 local device="${1}"
740 local other_device="${2}"
741
742 local mtu="$(device_get_mtu "${other_device}")"
743 device_set_mtu "${device}" "${mtu}"
744 }
745
746 device_discover() {
747 local device=${1}
748
749 log INFO "Running discovery process on device '${device}'."
750
751 local hook
752 for hook in $(hook_zone_get_all); do
753 hook_zone_exec ${hook} discover ${device}
754 done
755 }
756
757 device_identify() {
758 assert [ $# -ge 1 ]
759
760 local device="${1}"
761
762 # Flash for ten seconds by default
763 local seconds="10"
764
765 # Run in background?
766 local background="false"
767
768 local arg
769 while read arg; do
770 case "${arg}" in
771 --background)
772 background="true"
773 ;;
774 --seconds=*)
775 seconds="$(cli_get_val "${arg}")"
776 ;;
777 esac
778 done <<< "$(args $@)"
779
780 assert isinteger seconds
781
782 if ! device_exists "${device}"; then
783 log ERROR "Cannot identify device ${device}: Does not exist"
784 return ${EXIT_ERROR}
785 fi
786
787 if ! device_is_ethernet "${device}"; then
788 log DEBUG "Cannot identify device ${device}: Not an ethernet device"
789 return ${EXIT_NOT_SUPPORTED}
790 fi
791
792 log DEBUG "Identifying device ${device}"
793
794 local command="ethtool --identify ${device} ${seconds}"
795 local ret=0
796
797 if enabled background; then
798 cmd_background "${command}"
799 else
800 cmd_quiet "${command}"
801 ret=$?
802 fi
803
804 return ${ret}
805 }
806
807 device_has_ip() {
808 local device=${1}
809 local addr=${2}
810
811 assert isset addr
812 assert device_exists ${device}
813
814 # IPv6 addresses must be fully imploded
815 local protocol=$(ip_detect_protocol ${addr})
816 case "${protocol}" in
817 ipv6)
818 addr=$(ipv6_format "${addr}")
819 ;;
820 esac
821
822 list_match ${addr} $(device_get_addresses ${device})
823 }
824
825 device_get_addresses() {
826 local device=${1}
827
828 assert device_exists ${device}
829
830 local prot
831 local addr
832 local line
833 ip addr show ${device} | \
834 while read prot addr line; do
835 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
836 done
837 }
838
839 __device_get_file() {
840 local device=${1}
841 local file=${2}
842
843 fread "${SYS_CLASS_NET}/${device}/${file}"
844 }
845
846 __device_set_file() {
847 assert [ $# -eq 3 ]
848
849 local device="${1}"
850 local file="${2}"
851 local value="${3}"
852
853 fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
854 }
855
856 device_get_rx_bytes() {
857 local device=${1}
858
859 __device_get_file ${device} statistics/rx_bytes
860 }
861
862 device_get_tx_bytes() {
863 local device=${1}
864
865 __device_get_file ${device} statistics/tx_bytes
866 }
867
868 device_get_rx_packets() {
869 local device=${1}
870
871 __device_get_file ${device} statistics/rx_packets
872 }
873
874 device_get_tx_packets() {
875 local device=${1}
876
877 __device_get_file ${device} statistics/tx_packets
878 }
879
880 device_get_rx_errors() {
881 local device=${1}
882
883 __device_get_file ${device} statistics/rx_errors
884 }
885
886 device_get_tx_errors() {
887 local device=${1}
888
889 __device_get_file ${device} statistics/tx_errors
890 }
891
892 device_get_speed() {
893 local device=${1}
894
895 local speed=$(__device_get_file ${device} speed)
896
897 # Exit for no output (i.e. no link detected)
898 isset speed || return ${EXIT_ERROR}
899
900 # Don't return anything for negative values
901 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
902
903 print "${speed}"
904 }
905
906 device_get_duplex() {
907 local device=${1}
908
909 local duplex=$(__device_get_file ${device} duplex)
910
911 case "${duplex}" in
912 unknown)
913 return ${EXIT_ERROR}
914 ;;
915 *)
916 print "${duplex}"
917 ;;
918 esac
919 }
920
921 device_get_link_string() {
922 local device="${1}"
923 assert isset device
924
925 local s
926
927 local speed="$(device_get_speed "${device}")"
928 if isset speed; then
929 list_append s "${speed} MBit/s"
930 fi
931
932 local duplex="$(device_get_duplex "${device}")"
933 if isset duplex; then
934 list_append s "${duplex} duplex"
935 fi
936
937 print "${s}"
938 }
939
940 device_auto_configure_smp_affinity() {
941 assert [ $# -eq 1 ]
942
943 local device=${1}
944
945 if lock_acquire "smp-affinity" 60; then
946 device_set_smp_affinity ${device} auto
947
948 lock_release "smp-affinity"
949 fi
950 }
951
952 device_set_smp_affinity() {
953 assert [ $# -eq 2 ]
954
955 local device=${1}
956 local mode=${2}
957
958 # mode can be auto which will automatically try to find
959 # the least busy processor, or an integer for the desired
960 # processor that should handle this device
961
962 local num_processors=$(system_get_processors)
963
964 if [ "${mode}" = "auto" ]; then
965 local processor=$(interrupt_choose_least_busy_processor)
966 else
967 assert isinteger mode
968 local processor=${mode}
969
970 if [ ${processor} -gt ${num_processors} ]; then
971 log ERROR "Processor ${processor} does not exist"
972 return ${EXIT_ERROR}
973 fi
974 fi
975
976 local interrupts=$(interrupts_for_device ${device})
977 if ! isset interrupts; then
978 log DEBUG "${device} has no interrupts. Not changing SMP affinity"
979 return ${EXIT_OK}
980 fi
981
982 # Set SMP affinity
983 local interrupt
984 for interrupt in ${interrupts}; do
985 interrupt_set_smp_affinity ${interrupt} ${processor}
986 done
987
988 # Find all queues and assign them to the next processor
989 local queue
990 for queue in $(device_get_queues ${device}); do
991 case "${queue}" in
992 # Only handle receive queues
993 rx-*)
994 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
995 interrupt_set_smp_affinity ${interrupt} ${processor}
996 done
997
998 device_queue_set_smp_affinity ${device} ${queue} ${processor}
999 ;;
1000
1001 # Ignore the rest
1002 *)
1003 continue
1004 ;;
1005 esac
1006
1007 # Get the next available processor if in auto mode
1008 [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
1009 done
1010
1011 return ${EXIT_OK}
1012 }
1013
1014 device_get_queues() {
1015 assert [ $# -eq 1 ]
1016
1017 local device=${1}
1018
1019 local queue
1020 for queue in ${SYS_CLASS_NET}/${device}/queues/*; do
1021 [ -d "${queue}" ] || continue
1022
1023 basename "${queue}"
1024 done
1025 }
1026
1027 device_supports_multiqueue() {
1028 local device=${1}
1029
1030 local num_queues=$(device_num_queues ${device})
1031
1032 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1033 return ${EXIT_TRUE}
1034 fi
1035
1036 return ${EXIT_FALSE}
1037 }
1038
1039 device_num_queues() {
1040 local device=${1}
1041 local type=${2}
1042
1043 isset type && assert isoneof type rx tx
1044
1045 local i=0
1046
1047 local q
1048 for q in $(device_get_queues ${device}); do
1049 case "${type},${q}" in
1050 rx,rx-*)
1051 (( i++ ))
1052 ;;
1053 tx,tx-*)
1054 (( i++ ))
1055 ;;
1056 *,*)
1057 (( i++ ))
1058 ;;
1059 esac
1060 done
1061
1062 print ${i}
1063 }
1064
1065 device_queue_get_smp_affinity() {
1066 assert [ $# -eq 2 ]
1067
1068 local device=${1}
1069 local queue=${2}
1070
1071 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1072
1073 case "${queue}" in
1074 rx-*)
1075 path="${path}/rps_cpus"
1076 ;;
1077 tx-*)
1078 path="${path}/xps_cpus"
1079 ;;
1080 esac
1081 assert [ -r "${path}" ]
1082
1083 __bitmap_to_processor_ids $(<${path})
1084 }
1085
1086 device_queue_set_smp_affinity() {
1087 assert [ $# -eq 3 ]
1088
1089 local device=${1}
1090 local queue=${2}
1091 local processor=${3}
1092
1093 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1094 assert [ -w "${path}" ]
1095
1096 log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1097
1098 __processor_id_to_bitmap ${processor} > ${path}
1099 }