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