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