2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
26 list_append devices $
(devices_get_all
)
29 list_append devices $
(phy_list
)
31 # Add all serial devices
32 list_append devices $
(serial_list
)
34 # Return a sorted result
38 # Check if the device exists
42 # If device name was not found, exit.
43 [ -n "${device}" ] ||
return ${EXIT_ERROR}
45 # Check for a normal network device.
46 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
48 # If the check above did not find a result,
50 phy_exists "${device}" && return ${EXIT_OK}
52 # If the check above did not find a result,
53 # we check for serial devices.
54 serial_exists ${device}
57 device_matches_pattern() {
64 pattern="^
${pattern//N/[[:digit:]]+}$
"
66 [[ ${device} =~ ${pattern} ]] \
67 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
74 # Nothing to do, it device does not exist.
75 device_exists ${device} || return ${EXIT_OK}
78 cmd_quiet ip link delete ${device}
81 if [ ${ret} -ne ${EXIT_OK} ]; then
82 log ERROR "device
: Could not delete device
'${device}': ${ret}"
93 local flags=$(__device_get_file ${device} flags)
95 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
102 # Check if the device is up
106 device_exists ${device} || return ${EXIT_ERROR}
108 device_has_flag ${device} 0x1
111 device_ifindex_to_name() {
115 local device device_idx
116 for device in $(list_directory "${SYS_CLASS_NET}"); do
117 device_idx=$(device_get_ifindex ${device})
119 if [ "${device_idx}" = "${idx}" ]; then
128 device_get_ifindex() {
132 local path="${SYS_CLASS_NET}/${1}/ifindex
"
134 # Check if file can be read.
135 [ -r "${path}" ] || return ${EXIT_ERROR}
140 # Check if the device is a batman-adv bridge
141 device_is_batman_adv() {
142 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
145 # Check if the device is a batman-adv slave port
146 device_is_batman_adv_slave() {
149 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
150 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
165 # Check if the device is a bonding device
166 device_is_bonding() {
167 [ -d "/sys
/class
/net
/${1}/bonding
" ]
170 # Check if the device bonded in a bonding device
174 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
177 # Check if the device is a bridge
179 [ -d "/sys
/class
/net
/${1}/bridge
" ]
182 device_is_bridge_attached() {
184 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
187 device_is_wireless_monitor() {
191 device_is_wireless "${device}" && \
192 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
195 device_is_wireless_adhoc() {
199 device_is_wireless "${device}" && \
200 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
203 device_get_bridge() {
207 # Check if device is attached to a bridge.
208 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
210 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
211 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
213 local ifindex=$(<${ifindex_path})
216 device_ifindex_to_name ${ifindex}
219 # Check if the device is a vlan device
224 [ -e "${PROC_NET_VLAN}/${device}" ]
227 # Check if the device has vlan devices
232 if device_is_vlan ${device}; then
236 local vlans=$(device_get_vlans ${device})
237 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
244 # If no 8021q module has been loaded into the kernel,
245 # we cannot do anything.
246 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
248 local dev spacer1 id spacer2 parent
249 while read dev spacer1 id spacer2 parent
; do
250 [ "${parent}" = "${device}" ] ||
continue
253 done < ${PROC_NET_VLAN_CONFIG}
256 # Check if the device is a ppp device
260 local type=$
(__device_get_file
${device} type)
262 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
265 # Check if the device is a pointopoint device.
269 device_has_flag ${device} 0x10
272 # Check if the device is a loopback device
273 device_is_loopback() {
276 [ "${device}" = "lo
" ]
279 # Check if the device is a dummy device
280 # This is the worst possible check, but all I could come up with
284 [[ ${device} =~ ^dummy[0-9]+$ ]]
290 [[ ${device} =~ ^ipsec\- ]]
293 # Check if the device is a wireless device
294 device_is_wireless() {
297 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
303 local type=$(__device_get_file ${device} type)
305 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
311 if device_is_wireless
"${device}"; then
312 print
"$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
327 # Returns true if a device is a tun device
331 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
334 # Check if the device is a physical network interface
335 device_is_ethernet
() {
338 device_is_ethernet_compatible
"${device}" || \
341 device_is_loopback
${device} && \
344 device_is_bonding
${device} && \
347 device_is_bridge
${device} && \
350 device_is_ppp
${device} && \
353 device_is_vlan
${device} && \
356 device_is_dummy
${device} && \
359 device_is_tun
${device} && \
365 # Get the device type
369 # If the device does not exist (happens on udev remove events),
370 # we do not bother to run all checks.
371 if ! device_exists
"${device}"; then
374 elif device_is_vlan
${device}; then
377 elif device_is_bonding
${device}; then
380 elif device_is_bridge
${device}; then
383 elif device_is_ppp
${device}; then
386 elif device_is_batman_adv
${device}; then
389 elif device_is_loopback
${device}; then
392 elif device_is_wireless_adhoc
${device}; then
393 echo "wireless-adhoc"
395 elif device_is_wireless
${device}; then
398 elif device_is_dummy
${device}; then
401 elif device_is_tun
${device}; then
404 elif device_is_ethernet
${device}; then
407 elif device_is_serial
${device}; then
410 elif device_is_phy
${device}; then
413 elif device_is_vti
${device}; then
421 device_is_ethernet_compatible
() {
424 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
425 local type="$(__device_get_file "${device}" "type")"
426 [[ "${type}" = "1" ]]
429 device_get_status
() {
433 local status
=${STATUS_DOWN}
435 if device_is_up
${device}; then
438 if ! device_has_carrier
${device}; then
439 status
=${STATUS_NOCARRIER}
446 device_get_address
() {
449 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
452 device_set_address
() {
458 if ! device_exists
"${device}"; then
459 error
"Device '${device}' does not exist."
463 # Do nothing if the address has not changed
464 local old_addr
="$(device_get_address "${device}")"
465 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
469 log DEBUG
"Setting address of '${device}' from '${old_addr}' to '${addr}'"
472 if device_is_up
"${device}"; then
473 device_set_down
"${device}"
477 ip link
set "${device}" address
"${addr}"
480 if [ "${up}" = "1" ]; then
481 device_set_up
"${device}"
484 if [ "${ret}" != "0" ]; then
485 error_log
"Could not set address '${addr}' on device '${device}'"
493 for device
in $
(list_directory
"${SYS_CLASS_NET}"); do
494 # bonding_masters is no device
495 [ "${device}" = "bonding_masters" ] && continue
507 # Check if a device has a cable plugged in
508 device_has_carrier
() {
512 local carrier
=$
(__device_get_file
${device} carrier
)
513 [ "${carrier}" = "1" ]
516 device_is_promisc
() {
519 device_has_flag
${device} 0x200
522 device_set_promisc
() {
526 assert device_exists
${device}
528 assert isoneof state on off
530 ip link
set ${device} promisc
${state}
533 # Check if the device is free
535 ! device_is_used
"$@"
538 # Check if the device is used
542 device_has_vlans
${device} && \
544 device_is_bonded
${device} && \
546 device_is_bridge_attached
${device} && \
552 # Give the device a new name
555 local destination
=${2}
557 # Check if devices exists
558 if ! device_exists
${source} || device_exists
${destination}; then
563 if device_is_up
${source}; then
564 ip link
set ${source} down
568 ip link
set ${source} name
${destination}
570 if [ "${up}" = "1" ]; then
571 ip link
set ${destination} up
575 device_set_master
() {
582 if ! cmd ip link
set "${device}" master
"${master}"; then
583 log ERROR
"Could not set master ${master} for device ${device}"
590 device_remove_master
() {
594 if ! cmd ip link
set "${device}" nomaster
; then
595 log ERROR
"Could not remove master for device ${device}"
608 # Do nothing if device is already up
609 device_is_up
${device} && return ${EXIT_OK}
611 log INFO
"Bringing up ${device}"
613 device_set_parent_up
${device}
614 if ! cmd ip link
set ${device} up
; then
619 if interrupt_use_smp_affinity
; then
620 device_auto_configure_smp_affinity
${device}
626 device_set_parent_up
() {
630 if device_is_vlan
${device}; then
631 parent
=$
(vlan_get_parent
${device})
633 device_is_up
${parent} && return ${EXIT_OK}
635 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
637 device_set_up
${parent}
651 if device_is_up
${device}; then
652 log INFO
"Bringing down ${device}"
654 cmd ip link
set ${device} down
658 device_set_parent_down
${device}
663 device_set_parent_down
() {
667 if device_is_vlan
${device}; then
668 parent
=$
(vlan_get_parent
${device})
670 device_is_up
${parent} ||
return ${EXIT_OK}
672 if device_is_free
${parent}; then
673 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
675 device_set_down
${parent}
685 # Return an error if the device does not exist
686 device_exists
${device} ||
return ${EXIT_ERROR}
688 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
691 # Set mtu to a device
696 assert device_exists
${device}
698 # Handle bridges differently
699 if device_is_bridge
${device}; then
701 for port
in $
(bridge_get_members
${device}); do
702 device_set_mtu
${port} ${mtu}
706 log INFO
"Setting MTU of ${device} to ${mtu}"
709 if device_is_up
${device}; then
710 device_set_down
${device}
715 if ! cmd ip link
set ${device} mtu
${mtu}; then
718 log ERROR
"Could not set MTU ${mtu} on ${device}"
721 if [ "${up}" = "1" ]; then
722 device_set_up
${device}
728 device_adjust_mtu
() {
732 local other_device
="${2}"
734 local mtu
="$(device_get_mtu "${other_device}")"
735 device_set_mtu
"${device}" "${mtu}"
741 log INFO
"Running discovery process on device '${device}'."
744 for hook
in $
(hook_zone_get_all
); do
745 hook_zone_exec
${hook} discover
${device}
754 # Flash for ten seconds by default
758 local background
="false"
767 seconds
="$(cli_get_val "${arg}")"
770 done <<< "$(args "$@
")"
772 assert isinteger seconds
774 if ! device_exists
"${device}"; then
775 log ERROR
"Cannot identify device ${device}: Does not exist"
779 if ! device_is_ethernet
"${device}"; then
780 log DEBUG
"Cannot identify device ${device}: Not an ethernet device"
781 return ${EXIT_NOT_SUPPORTED}
784 log DEBUG
"Identifying device ${device}"
786 local command="ethtool --identify ${device} ${seconds}"
789 if enabled background
; then
790 cmd_background
"${command}"
792 cmd_quiet
"${command}"
804 assert device_exists
${device}
806 # IPv6 addresses must be fully imploded
807 local protocol
=$
(ip_detect_protocol
${addr})
808 case "${protocol}" in
810 addr
=$
(ipv6_format
"${addr}")
814 list_match
${addr} $
(device_get_addresses
${device})
817 device_get_addresses
() {
820 assert device_exists
${device}
825 ip addr show
${device} | \
826 while read prot addr line
; do
827 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
831 __device_get_file
() {
835 fread
"${SYS_CLASS_NET}/${device}/${file}"
838 __device_set_file
() {
845 fappend
"${SYS_CLASS_NET}/${device}/${file}" "${value}"
848 device_get_rx_bytes
() {
851 __device_get_file
${device} statistics
/rx_bytes
854 device_get_tx_bytes
() {
857 __device_get_file
${device} statistics
/tx_bytes
860 device_get_rx_packets
() {
863 __device_get_file
${device} statistics
/rx_packets
866 device_get_tx_packets
() {
869 __device_get_file
${device} statistics
/tx_packets
872 device_get_rx_errors
() {
875 __device_get_file
${device} statistics
/rx_errors
878 device_get_tx_errors
() {
881 __device_get_file
${device} statistics
/tx_errors
887 local speed
=$
(__device_get_file
${device} speed
)
889 # Exit for no output (i.e. no link detected)
890 isset speed ||
return ${EXIT_ERROR}
892 # Don't return anything for negative values
893 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
898 device_get_duplex
() {
901 local duplex
=$
(__device_get_file
${device} duplex
)
913 device_get_link_string
() {
919 local speed
="$(device_get_speed "${device}")"
921 list_append s
"${speed} MBit/s"
924 local duplex
="$(device_get_duplex "${device}")"
925 if isset duplex
; then
926 list_append s
"${duplex} duplex"
932 device_auto_configure_smp_affinity
() {
937 if lock_acquire
"smp-affinity" 60; then
938 device_set_smp_affinity
${device} auto
940 lock_release
"smp-affinity"
944 device_set_smp_affinity
() {
950 # mode can be auto which will automatically try to find
951 # the least busy processor, or an integer for the desired
952 # processor that should handle this device
954 local num_processors
=$
(system_get_processors
)
956 if [ "${mode}" = "auto" ]; then
957 local processor
=$
(interrupt_choose_least_busy_processor
)
959 assert isinteger mode
960 local processor
=${mode}
962 if [ ${processor} -gt ${num_processors} ]; then
963 log ERROR
"Processor ${processor} does not exist"
968 local interrupts
=$
(interrupts_for_device
${device})
969 if ! isset interrupts
; then
970 log DEBUG
"${device} has no interrupts. Not changing SMP affinity"
976 for interrupt
in ${interrupts}; do
977 interrupt_set_smp_affinity
${interrupt} ${processor}
980 # Find all queues and assign them to the next processor
982 for queue
in $
(device_get_queues
${device}); do
984 # Only handle receive queues
986 for interrupt
in $
(interrupts_for_device_queue
${device} ${queue}); do
987 interrupt_set_smp_affinity
${interrupt} ${processor}
990 device_queue_set_smp_affinity
${device} ${queue} ${processor}
999 # Get the next available processor if in auto mode
1000 [ "${mode}" = "auto" ] && processor
=$
(system_get_next_processor
${processor})
1006 device_get_queues
() {
1011 list_directory
"${SYS_CLASS_NET}/${device}/queues"
1014 device_supports_multiqueue
() {
1017 local num_queues
=$
(device_num_queues
${device})
1019 if isset num_queues
&& [ ${num_queues} -gt 2 ]; then
1023 return ${EXIT_FALSE}
1026 device_num_queues
() {
1030 isset
type && assert isoneof
type rx tx
1035 for q
in $
(device_get_queues
${device}); do
1036 case "${type},${q}" in
1052 device_queue_get_smp_affinity
() {
1058 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}"
1062 path
="${path}/rps_cpus"
1065 path
="${path}/xps_cpus"
1068 assert
[ -r "${path}" ]
1070 __bitmap_to_processor_ids $
(<${path})
1073 device_queue_set_smp_affinity
() {
1078 local processor
=${3}
1080 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1081 assert
[ -w "${path}" ]
1083 log DEBUG
"Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1085 __processor_id_to_bitmap
${processor} > ${path}