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