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