]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.device
port: ethernet: Allow setting link speed
[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 # Shut down device before we delete it
81 device_set_down "${device}"
82
83 # Delete the device.
84 cmd_quiet ip link delete ${device}
85 local ret=$?
86
87 if [ ${ret} -ne ${EXIT_OK} ]; then
88 log ERROR "device: Could not delete device '${device}': ${ret}"
89 return ${EXIT_ERROR}
90 fi
91
92 return ${ret}
93 }
94
95 device_has_flag() {
96 local device=${1}
97 local flag=${2}
98
99 local flags=$(__device_get_file ${device} flags)
100
101 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
102 return ${EXIT_FALSE}
103 else
104 return ${EXIT_TRUE}
105 fi
106 }
107
108 # Check if the device is up
109 device_is_up() {
110 local device=${1}
111
112 device_exists ${device} || return ${EXIT_ERROR}
113
114 device_has_flag ${device} 0x1
115 }
116
117 device_ifindex_to_name() {
118 local idx=${1}
119 assert isset idx
120
121 local device device_idx
122 for device in $(list_directory "${SYS_CLASS_NET}"); do
123 device_idx=$(device_get_ifindex ${device})
124
125 if [ "${device_idx}" = "${idx}" ]; then
126 print "${device}"
127 return ${EXIT_OK}
128 fi
129 done
130
131 return ${EXIT_ERROR}
132 }
133
134 device_get_ifindex() {
135 local device=${1}
136 assert isset device
137
138 local path="${SYS_CLASS_NET}/${1}/ifindex"
139
140 # Check if file can be read.
141 [ -r "${path}" ] || return ${EXIT_ERROR}
142
143 print "$(<${path})"
144 }
145
146 # Check if the device is a bonding device
147 device_is_bonding() {
148 [ -d "/sys/class/net/${1}/bonding" ]
149 }
150
151 # Check if the device bonded in a bonding device
152 device_is_bonded() {
153 local device=${1}
154
155 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ]
156 }
157
158 # Check if the device is a bridge
159 device_is_bridge() {
160 [ -d "/sys/class/net/${1}/bridge" ]
161 }
162
163 device_is_bridge_attached() {
164 local device=${1}
165 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
166 }
167
168 device_is_wireless_monitor() {
169 local device="${1}"
170 assert isset device
171
172 device_is_wireless "${device}" && \
173 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
174 }
175
176 device_is_wireless_adhoc() {
177 local device="${1}"
178 assert isset device
179
180 device_is_wireless "${device}" && \
181 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
182 }
183
184 device_get_bridge() {
185 local device=${1}
186 assert isset device
187
188 # Check if device is attached to a bridge.
189 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
190
191 local ifindex_path="${SYS_CLASS_NET}/${device}/brport/bridge/ifindex"
192 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
193
194 local ifindex=$(<${ifindex_path})
195 assert isset ifindex
196
197 device_ifindex_to_name ${ifindex}
198 }
199
200 # Check if the device is a vlan device
201 device_is_vlan() {
202 local device=${1}
203 assert isset device
204
205 [ -e "${PROC_NET_VLAN}/${device}" ]
206 }
207
208 # Check if the device has vlan devices
209 device_has_vlans() {
210 local device=${1}
211 assert isset device
212
213 if device_is_vlan ${device}; then
214 return ${EXIT_FALSE}
215 fi
216
217 local vlans=$(device_get_vlans ${device})
218 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
219 }
220
221 device_get_vlans() {
222 local device=${1}
223 assert isset device
224
225 # If no 8021q module has been loaded into the kernel,
226 # we cannot do anything.
227 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
228
229 local dev spacer1 id spacer2 parent
230 while read dev spacer1 id spacer2 parent; do
231 [ "${parent}" = "${device}" ] || continue
232
233 print "${dev}"
234 done < ${PROC_NET_VLAN_CONFIG}
235 }
236
237 # Check if the device is a ppp device
238 device_is_ppp() {
239 local device=${1}
240
241 local type=$(__device_get_file ${device} type)
242
243 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
244 }
245
246 # Check if the device is a pointopoint device.
247 device_is_ptp() {
248 local device=${1}
249
250 device_has_flag ${device} 0x10
251 }
252
253 # Check if the device is a loopback device
254 device_is_loopback() {
255 local device=${1}
256
257 [ "${device}" = "lo" ]
258 }
259
260 # Check if the device is a dummy device
261 # This is the worst possible check, but all I could come up with
262 device_is_dummy() {
263 local device="${1}"
264
265 [[ ${device} =~ ^dummy[0-9]+$ ]]
266 }
267
268 device_is_ipsec() {
269 local device="${1}"
270
271 [[ ${device} =~ ^ipsec\- ]]
272 }
273
274 # Check if the device is a wireless device
275 device_is_wireless() {
276 local device=${1}
277
278 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
279 }
280
281 device_is_vti() {
282 local device=${1}
283
284 local type=$(__device_get_file ${device} type)
285
286 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
287 }
288
289 device_is_vti6() {
290 local device=${1}
291
292 local type=$(__device_get_file ${device} type)
293
294 [ "${type}" = "769" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
295 }
296
297 device_get_phy() {
298 local device="${1}"
299
300 if device_is_wireless "${device}"; then
301 print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
302 return ${EXIT_OK}
303 fi
304
305 return ${EXIT_ERROR}
306 }
307
308 device_is_phy() {
309 phy_exists "$@"
310 }
311
312 device_is_serial() {
313 serial_exists "$@"
314 }
315
316 # Returns true if a device is a tun device
317 device_is_tun() {
318 local device="${1}"
319
320 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
321 }
322
323 # Check if the device is a physical network interface
324 device_is_ethernet() {
325 local device=${1}
326
327 device_is_ethernet_compatible "${device}" || \
328 return ${EXIT_ERROR}
329
330 device_is_loopback ${device} && \
331 return ${EXIT_ERROR}
332
333 device_is_bonding ${device} && \
334 return ${EXIT_ERROR}
335
336 device_is_bridge ${device} && \
337 return ${EXIT_ERROR}
338
339 device_is_ppp ${device} && \
340 return ${EXIT_ERROR}
341
342 device_is_vlan ${device} && \
343 return ${EXIT_ERROR}
344
345 device_is_dummy ${device} && \
346 return ${EXIT_ERROR}
347
348 device_is_tun ${device} && \
349 return ${EXIT_ERROR}
350
351 return ${EXIT_OK}
352 }
353
354 # Get the device type
355 device_get_type() {
356 local device=${1}
357
358 # If the device does not exist (happens on udev remove events),
359 # we do not bother to run all checks.
360 if ! device_exists "${device}"; then
361 echo "unknown"
362
363 elif device_is_vlan ${device}; then
364 echo "vlan"
365
366 elif device_is_bonding ${device}; then
367 echo "bonding"
368
369 elif device_is_bridge ${device}; then
370 echo "bridge"
371
372 elif device_is_ppp ${device}; then
373 echo "ppp"
374
375 elif device_is_loopback ${device}; then
376 echo "loopback"
377
378 elif device_is_wireless_adhoc ${device}; then
379 echo "wireless-adhoc"
380
381 elif device_is_wireless ${device}; then
382 echo "wireless"
383
384 elif device_is_dummy ${device}; then
385 echo "dummy"
386
387 elif device_is_tun ${device}; then
388 echo "tun"
389
390 elif device_is_ethernet ${device}; then
391 echo "ethernet"
392
393 elif device_is_serial ${device}; then
394 echo "serial"
395
396 elif device_is_phy ${device}; then
397 echo "phy"
398
399 else
400 echo "$(device_tunnel_get_type "${device}")"
401 fi
402 }
403
404 # This function just checks the types a ip-tunnel device usually have
405 # so when we know that the device is an ip-tunnel device we save time
406 device_tunnel_get_type() {
407 local device=${1}
408
409 # If the device does not exist (happens on udev remove events),
410 # we do not bother to run all checks.
411 if ! device_exists "${device}"; then
412 echo "unknown"
413
414 elif device_is_vti ${device}; then
415 echo "vti"
416
417 elif device_is_vti6 ${device}; then
418 echo "vti6"
419
420 else
421 echo "unknown"
422 fi
423 }
424
425 device_is_ethernet_compatible() {
426 local device="${1}"
427
428 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
429 local type="$(__device_get_file "${device}" "type")"
430 [[ "${type}" = "1" ]]
431 }
432
433 device_get_status() {
434 local device=${1}
435 assert isset device
436
437 local status=${STATUS_DOWN}
438
439 if device_is_up ${device}; then
440 status=${STATUS_UP}
441
442 if ! device_has_carrier ${device}; then
443 status=${STATUS_NOCARRIER}
444 fi
445 fi
446
447 echo "${status}"
448 }
449
450 device_get_address() {
451 local device=${1}
452
453 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
454 }
455
456 device_set_address() {
457 assert [ $# -eq 2 ]
458
459 local device="${1}"
460 local addr="${2}"
461
462 if ! device_exists "${device}"; then
463 error "Device '${device}' does not exist."
464 return ${EXIT_ERROR}
465 fi
466
467 # Do nothing if the address has not changed
468 local old_addr="$(device_get_address "${device}")"
469 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
470 return ${EXIT_OK}
471 fi
472
473 log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'"
474
475 local up
476 if device_is_up "${device}"; then
477 device_set_down "${device}"
478 up=1
479 fi
480
481 ip link set "${device}" address "${addr}"
482 local ret=$?
483
484 if [ "${up}" = "1" ]; then
485 device_set_up "${device}"
486 fi
487
488 if [ "${ret}" != "0" ]; then
489 error_log "Could not set address '${addr}' on device '${device}'"
490 fi
491
492 return ${ret}
493 }
494
495 device_get() {
496 local device
497 for device in $(list_directory "${SYS_CLASS_NET}"); do
498 # bonding_masters is no device
499 [ "${device}" = "bonding_masters" ] && continue
500
501 echo "${device}"
502 done
503
504 return ${EXIT_OK}
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_set_speed() {
899 local device="${1}"
900 assert isset device
901
902 local speed="${2}"
903 assert isinteger speed
904
905 if ! cmd_quiet ethtool --change "${device}" speed "${speed}"; then
906 log ERROR "Could not set speed of ${device} to ${speed} MBit/s"
907 return ${EXIT_ERROR}
908 fi
909
910 log DEBUG "Set speed of ${device} to ${speed} MBit/s"
911
912 return ${EXIT_OK}
913 }
914
915 device_get_duplex() {
916 local device=${1}
917
918 local duplex=$(__device_get_file ${device} duplex)
919
920 case "${duplex}" in
921 unknown)
922 return ${EXIT_ERROR}
923 ;;
924 *)
925 print "${duplex}"
926 ;;
927 esac
928 }
929
930 device_get_link_string() {
931 local device="${1}"
932 assert isset device
933
934 local s
935
936 local speed="$(device_get_speed "${device}")"
937 if isset speed; then
938 list_append s "${speed} MBit/s"
939 fi
940
941 local duplex="$(device_get_duplex "${device}")"
942 if isset duplex; then
943 list_append s "${duplex} duplex"
944 fi
945
946 print "${s}"
947 }
948
949 device_auto_configure_smp_affinity() {
950 assert [ $# -eq 1 ]
951
952 local device=${1}
953
954 if lock_acquire "smp-affinity" 60; then
955 device_set_smp_affinity ${device} auto
956
957 lock_release "smp-affinity"
958 fi
959 }
960
961 device_set_smp_affinity() {
962 assert [ $# -eq 2 ]
963
964 local device=${1}
965 local mode=${2}
966
967 # mode can be auto which will automatically try to find
968 # the least busy processor, or an integer for the desired
969 # processor that should handle this device
970
971 local num_processors=$(system_get_processors)
972
973 if [ "${mode}" = "auto" ]; then
974 local processor=$(interrupt_choose_least_busy_processor)
975 else
976 assert isinteger mode
977 local processor=${mode}
978
979 if [ ${processor} -gt ${num_processors} ]; then
980 log ERROR "Processor ${processor} does not exist"
981 return ${EXIT_ERROR}
982 fi
983 fi
984
985 local interrupts=$(interrupts_for_device ${device})
986 if ! isset interrupts; then
987 log DEBUG "${device} has no interrupts. Not changing SMP affinity"
988 return ${EXIT_OK}
989 fi
990
991 # Set SMP affinity
992 local interrupt
993 for interrupt in ${interrupts}; do
994 interrupt_set_smp_affinity ${interrupt} ${processor}
995 done
996
997 # Find all queues and assign them to the next processor
998 local queue
999 for queue in $(device_get_queues ${device}); do
1000 case "${queue}" in
1001 # Only handle receive queues
1002 rx-*)
1003 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
1004 interrupt_set_smp_affinity ${interrupt} ${processor}
1005 done
1006
1007 device_queue_set_smp_affinity ${device} ${queue} ${processor}
1008 ;;
1009
1010 # Ignore the rest
1011 *)
1012 continue
1013 ;;
1014 esac
1015
1016 # Get the next available processor if in auto mode
1017 [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
1018 done
1019
1020 return ${EXIT_OK}
1021 }
1022
1023 device_get_queues() {
1024 assert [ $# -eq 1 ]
1025
1026 local device=${1}
1027
1028 list_directory "${SYS_CLASS_NET}/${device}/queues"
1029 }
1030
1031 device_supports_multiqueue() {
1032 local device=${1}
1033
1034 local num_queues=$(device_num_queues ${device})
1035
1036 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1037 return ${EXIT_TRUE}
1038 fi
1039
1040 return ${EXIT_FALSE}
1041 }
1042
1043 device_num_queues() {
1044 local device=${1}
1045 local type=${2}
1046
1047 isset type && assert isoneof type rx tx
1048
1049 local i=0
1050
1051 local q
1052 for q in $(device_get_queues ${device}); do
1053 case "${type},${q}" in
1054 rx,rx-*)
1055 (( i++ ))
1056 ;;
1057 tx,tx-*)
1058 (( i++ ))
1059 ;;
1060 *,*)
1061 (( i++ ))
1062 ;;
1063 esac
1064 done
1065
1066 print ${i}
1067 }
1068
1069 device_queue_get_smp_affinity() {
1070 assert [ $# -eq 2 ]
1071
1072 local device=${1}
1073 local queue=${2}
1074
1075 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1076
1077 case "${queue}" in
1078 rx-*)
1079 path="${path}/rps_cpus"
1080 ;;
1081 tx-*)
1082 path="${path}/xps_cpus"
1083 ;;
1084 esac
1085 assert [ -r "${path}" ]
1086
1087 __bitmap_to_processor_ids $(<${path})
1088 }
1089
1090 device_queue_set_smp_affinity() {
1091 assert [ $# -eq 3 ]
1092
1093 local device=${1}
1094 local queue=${2}
1095 local processor=${3}
1096
1097 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1098 assert [ -w "${path}" ]
1099
1100 log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1101
1102 __processor_id_to_bitmap ${processor} > ${path}
1103 }
1104
1105 # Tries to find a device which has the given IP address assigned
1106 device_get_by_assigned_ip_address() {
1107 local ip=${1}
1108
1109 assert isset ip
1110
1111 local device
1112
1113 # Read the first line of ip addr show to
1114 read -r device <<< $(ip addr show to "${ip}")
1115
1116 # If we did not found a device we return with ${EXIT_ERROR}
1117 if ! isset device; then
1118 return ${EXIT_ERROR}
1119 fi
1120
1121 # We get something like:
1122 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1123 # and we want upl0 so we take the second word and removing the :
1124 device=(${device})
1125 device=${device[1]}
1126 device=${device%:}
1127
1128 print "${device}"
1129 return ${EXIT_OK}
1130 }
1131
1132 device_get_by_mac_address() {
1133 local mac=${1}
1134
1135 assert isset mac
1136
1137 local device
1138
1139 for device in $(device_list); do
1140 if [ "${mac}" = "$(device_get_address ${device})" ]; then
1141 print "${device}"
1142 return ${EXIT_OK}
1143 fi
1144 done
1145
1146 # We could not found a port to the given mac address so we return exit error
1147 return ${EXIT_ERROR}
1148 }