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 ###############################################################################
27 for device
in $
(list_directory
${SYS_CLASS_NET}); do
28 list_append_one devices
"${device}"
32 list_append devices $
(phy_list
)
34 # Add all serial devices
35 list_append devices $
(serial_list
)
37 # Return a sorted result
41 # Check if the device exists
45 # If device name was not found, exit.
46 [ -n "${device}" ] ||
return ${EXIT_ERROR}
48 # Check for a normal network device.
49 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
51 # If the check above did not find a result,
53 phy_exists "${device}" && return ${EXIT_OK}
55 # If the check above did not find a result,
56 # we check for serial devices.
57 serial_exists ${device}
60 device_matches_pattern() {
67 pattern="^
${pattern//N/[[:digit:]]+}$
"
69 [[ ${device} =~ ${pattern} ]] \
70 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
77 # Nothing to do, it device does not exist.
78 device_exists ${device} || return ${EXIT_OK}
81 cmd_quiet ip link delete ${device}
84 if [ ${ret} -ne ${EXIT_OK} ]; then
85 log ERROR "device
: Could not delete device
'${device}': ${ret}"
96 local flags=$(__device_get_file ${device} flags)
98 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
105 # Check if the device is up
109 device_exists ${device} || return ${EXIT_ERROR}
111 device_has_flag ${device} 0x1
114 device_ifindex_to_name() {
118 local device device_idx
119 for device in $(list_directory "${SYS_CLASS_NET}"); do
120 device_idx=$(device_get_ifindex ${device})
122 if [ "${device_idx}" = "${idx}" ]; then
131 device_get_ifindex() {
135 local path="${SYS_CLASS_NET}/${1}/ifindex
"
137 # Check if file can be read.
138 [ -r "${path}" ] || return ${EXIT_ERROR}
143 # Check if the device is a batman-adv bridge
144 device_is_batman_adv() {
145 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
148 # Check if the device is a batman-adv slave port
149 device_is_batman_adv_slave() {
152 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
153 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
168 # Check if the device is a bonding device
169 device_is_bonding() {
170 [ -d "/sys
/class
/net
/${1}/bonding
" ]
173 # Check if the device bonded in a bonding device
177 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
180 # Check if the device is a bridge
182 [ -d "/sys
/class
/net
/${1}/bridge
" ]
185 device_is_bridge_attached() {
187 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
190 device_is_wireless_monitor() {
194 device_is_wireless "${device}" && \
195 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
198 device_is_wireless_adhoc() {
202 device_is_wireless "${device}" && \
203 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_ADHOC}"
206 device_get_bridge() {
210 # Check if device is attached to a bridge.
211 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
213 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
214 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
216 local ifindex=$(<${ifindex_path})
219 device_ifindex_to_name ${ifindex}
222 # Check if the device is a vlan device
227 [ -e "${PROC_NET_VLAN}/${device}" ]
230 # Check if the device has vlan devices
235 if device_is_vlan ${device}; then
239 local vlans=$(device_get_vlans ${device})
240 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
247 # If no 8021q module has been loaded into the kernel,
248 # we cannot do anything.
249 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
251 local dev spacer1 id spacer2 parent
252 while read dev spacer1 id spacer2 parent
; do
253 [ "${parent}" = "${device}" ] ||
continue
256 done < ${PROC_NET_VLAN_CONFIG}
259 # Check if the device is a ppp device
263 local type=$
(__device_get_file
${device} type)
265 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
268 # Check if the device is a pointopoint device.
272 device_has_flag ${device} 0x10
275 # Check if the device is a loopback device
276 device_is_loopback() {
279 [ "${device}" = "lo
" ]
282 # Check if the device is a dummy device
283 # This is the worst possible check, but all I could come up with
287 [[ ${device} =~ ^dummy[0-9]+$ ]]
293 [[ ${device} =~ ^ipsec\- ]]
296 # Check if the device is a wireless device
297 device_is_wireless() {
300 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
306 local type=$(__device_get_file ${device} type)
308 [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
314 if device_is_wireless
"${device}"; then
315 print
"$(<${SYS_CLASS_NET}/${device}/phy80211/name)"
330 # Returns true if a device is a tun device
334 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
337 # Check if the device is a physical network interface
338 device_is_ethernet
() {
341 device_is_ethernet_compatible
"${device}" || \
344 device_is_loopback
${device} && \
347 device_is_bonding
${device} && \
350 device_is_bridge
${device} && \
353 device_is_ppp
${device} && \
356 device_is_vlan
${device} && \
359 device_is_dummy
${device} && \
362 device_is_tun
${device} && \
368 # Get the device type
372 # If the device does not exist (happens on udev remove events),
373 # we do not bother to run all checks.
374 if ! device_exists
"${device}"; then
377 elif device_is_vlan
${device}; then
380 elif device_is_bonding
${device}; then
383 elif device_is_bridge
${device}; then
386 elif device_is_ppp
${device}; then
389 elif device_is_batman_adv
${device}; then
392 elif device_is_loopback
${device}; then
395 elif device_is_wireless_adhoc
${device}; then
396 echo "wireless-adhoc"
398 elif device_is_wireless
${device}; then
401 elif device_is_dummy
${device}; then
404 elif device_is_tun
${device}; then
407 elif device_is_ethernet
${device}; then
410 elif device_is_serial
${device}; then
413 elif device_is_phy
${device}; then
416 elif device_is_vti
${device}; then
424 device_is_ethernet_compatible
() {
427 # /sys/class/net/*/type must equal 1 for ethernet compatible devices
428 local type="$(__device_get_file "${device}" "type")"
429 [[ "${type}" = "1" ]]
432 device_get_status
() {
436 local status
=${STATUS_DOWN}
438 if device_is_up
${device}; then
441 if ! device_has_carrier
${device}; then
442 status
=${STATUS_NOCARRIER}
449 device_get_address
() {
452 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
455 device_set_address
() {
461 if ! device_exists
"${device}"; then
462 error
"Device '${device}' does not exist."
466 # Do nothing if the address has not changed
467 local old_addr
="$(device_get_address "${device}")"
468 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
472 log DEBUG
"Setting address of '${device}' from '${old_addr}' to '${addr}'"
475 if device_is_up
"${device}"; then
476 device_set_down
"${device}"
480 ip link
set "${device}" address
"${addr}"
483 if [ "${up}" = "1" ]; then
484 device_set_up
"${device}"
487 if [ "${ret}" != "0" ]; then
488 error_log
"Could not set address '${addr}' on device '${device}'"
496 for device
in $
(list_directory
"${SYS_CLASS_NET}"); do
497 # bonding_masters is no device
498 [ "${device}" = "bonding_masters" ] && continue
506 # Check if a device has a cable plugged in
507 device_has_carrier
() {
511 local carrier
=$
(__device_get_file
${device} carrier
)
512 [ "${carrier}" = "1" ]
515 device_is_promisc
() {
518 device_has_flag
${device} 0x200
521 device_set_promisc
() {
525 assert device_exists
${device}
527 assert isoneof state on off
529 ip link
set ${device} promisc
${state}
532 # Check if the device is free
534 ! device_is_used
"$@"
537 # Check if the device is used
541 device_has_vlans
${device} && \
543 device_is_bonded
${device} && \
545 device_is_bridge_attached
${device} && \
551 # Give the device a new name
554 local destination
=${2}
556 # Check if devices exists
557 if ! device_exists
${source} || device_exists
${destination}; then
562 if device_is_up
${source}; then
563 ip link
set ${source} down
567 ip link
set ${source} name
${destination}
569 if [ "${up}" = "1" ]; then
570 ip link
set ${destination} up
574 device_set_master
() {
581 if ! cmd ip link
set "${device}" master
"${master}"; then
582 log ERROR
"Could not set master ${master} for device ${device}"
589 device_remove_master
() {
593 if ! cmd ip link
set "${device}" nomaster
; then
594 log ERROR
"Could not remove master for device ${device}"
607 # Do nothing if device is already up
608 device_is_up
${device} && return ${EXIT_OK}
610 log INFO
"Bringing up ${device}"
612 device_set_parent_up
${device}
613 if ! cmd ip link
set ${device} up
; then
618 if interrupt_use_smp_affinity
; then
619 device_auto_configure_smp_affinity
${device}
625 device_set_parent_up
() {
629 if device_is_vlan
${device}; then
630 parent
=$
(vlan_get_parent
${device})
632 device_is_up
${parent} && return ${EXIT_OK}
634 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
636 device_set_up
${parent}
650 if device_is_up
${device}; then
651 log INFO
"Bringing down ${device}"
653 cmd ip link
set ${device} down
657 device_set_parent_down
${device}
662 device_set_parent_down
() {
666 if device_is_vlan
${device}; then
667 parent
=$
(vlan_get_parent
${device})
669 device_is_up
${parent} ||
return ${EXIT_OK}
671 if device_is_free
${parent}; then
672 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
674 device_set_down
${parent}
684 # Return an error if the device does not exist
685 device_exists
${device} ||
return ${EXIT_ERROR}
687 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
690 # Set mtu to a device
695 assert device_exists
${device}
697 # Handle bridges differently
698 if device_is_bridge
${device}; then
700 for port
in $
(bridge_get_members
${device}); do
701 device_set_mtu
${port} ${mtu}
705 log INFO
"Setting MTU of ${device} to ${mtu}"
708 if device_is_up
${device}; then
709 device_set_down
${device}
714 if ! cmd ip link
set ${device} mtu
${mtu}; then
717 log ERROR
"Could not set MTU ${mtu} on ${device}"
720 if [ "${up}" = "1" ]; then
721 device_set_up
${device}
727 device_adjust_mtu
() {
731 local other_device
="${2}"
733 local mtu
="$(device_get_mtu "${other_device}")"
734 device_set_mtu
"${device}" "${mtu}"
740 log INFO
"Running discovery process on device '${device}'."
743 for hook
in $
(hook_zone_get_all
); do
744 hook_zone_exec
${hook} discover
${device}
753 # Flash for ten seconds by default
757 local background
="false"
766 seconds
="$(cli_get_val "${arg}")"
769 done <<< "$(args "$@
")"
771 assert isinteger seconds
773 if ! device_exists
"${device}"; then
774 log ERROR
"Cannot identify device ${device}: Does not exist"
778 if ! device_is_ethernet
"${device}"; then
779 log DEBUG
"Cannot identify device ${device}: Not an ethernet device"
780 return ${EXIT_NOT_SUPPORTED}
783 log DEBUG
"Identifying device ${device}"
785 local command="ethtool --identify ${device} ${seconds}"
788 if enabled background
; then
789 cmd_background
"${command}"
791 cmd_quiet
"${command}"
803 assert device_exists
${device}
805 # IPv6 addresses must be fully imploded
806 local protocol
=$
(ip_detect_protocol
${addr})
807 case "${protocol}" in
809 addr
=$
(ipv6_format
"${addr}")
813 list_match
${addr} $
(device_get_addresses
${device})
816 device_get_addresses
() {
819 assert device_exists
${device}
824 ip addr show
${device} | \
825 while read prot addr line
; do
826 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
830 __device_get_file
() {
834 fread
"${SYS_CLASS_NET}/${device}/${file}"
837 __device_set_file
() {
844 fappend
"${SYS_CLASS_NET}/${device}/${file}" "${value}"
847 device_get_rx_bytes
() {
850 __device_get_file
${device} statistics
/rx_bytes
853 device_get_tx_bytes
() {
856 __device_get_file
${device} statistics
/tx_bytes
859 device_get_rx_packets
() {
862 __device_get_file
${device} statistics
/rx_packets
865 device_get_tx_packets
() {
868 __device_get_file
${device} statistics
/tx_packets
871 device_get_rx_errors
() {
874 __device_get_file
${device} statistics
/rx_errors
877 device_get_tx_errors
() {
880 __device_get_file
${device} statistics
/tx_errors
886 local speed
=$
(__device_get_file
${device} speed
)
888 # Exit for no output (i.e. no link detected)
889 isset speed ||
return ${EXIT_ERROR}
891 # Don't return anything for negative values
892 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
897 device_get_duplex
() {
900 local duplex
=$
(__device_get_file
${device} duplex
)
912 device_get_link_string
() {
918 local speed
="$(device_get_speed "${device}")"
920 list_append s
"${speed} MBit/s"
923 local duplex
="$(device_get_duplex "${device}")"
924 if isset duplex
; then
925 list_append s
"${duplex} duplex"
931 device_auto_configure_smp_affinity
() {
936 if lock_acquire
"smp-affinity" 60; then
937 device_set_smp_affinity
${device} auto
939 lock_release
"smp-affinity"
943 device_set_smp_affinity
() {
949 # mode can be auto which will automatically try to find
950 # the least busy processor, or an integer for the desired
951 # processor that should handle this device
953 local num_processors
=$
(system_get_processors
)
955 if [ "${mode}" = "auto" ]; then
956 local processor
=$
(interrupt_choose_least_busy_processor
)
958 assert isinteger mode
959 local processor
=${mode}
961 if [ ${processor} -gt ${num_processors} ]; then
962 log ERROR
"Processor ${processor} does not exist"
967 local interrupts
=$
(interrupts_for_device
${device})
968 if ! isset interrupts
; then
969 log DEBUG
"${device} has no interrupts. Not changing SMP affinity"
975 for interrupt
in ${interrupts}; do
976 interrupt_set_smp_affinity
${interrupt} ${processor}
979 # Find all queues and assign them to the next processor
981 for queue
in $
(device_get_queues
${device}); do
983 # Only handle receive queues
985 for interrupt
in $
(interrupts_for_device_queue
${device} ${queue}); do
986 interrupt_set_smp_affinity
${interrupt} ${processor}
989 device_queue_set_smp_affinity
${device} ${queue} ${processor}
998 # Get the next available processor if in auto mode
999 [ "${mode}" = "auto" ] && processor
=$
(system_get_next_processor
${processor})
1005 device_get_queues
() {
1010 list_directory
"${SYS_CLASS_NET}/${device}/queues"
1013 device_supports_multiqueue
() {
1016 local num_queues
=$
(device_num_queues
${device})
1018 if isset num_queues
&& [ ${num_queues} -gt 2 ]; then
1022 return ${EXIT_FALSE}
1025 device_num_queues
() {
1029 isset
type && assert isoneof
type rx tx
1034 for q
in $
(device_get_queues
${device}); do
1035 case "${type},${q}" in
1051 device_queue_get_smp_affinity
() {
1057 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}"
1061 path
="${path}/rps_cpus"
1064 path
="${path}/xps_cpus"
1067 assert
[ -r "${path}" ]
1069 __bitmap_to_processor_ids $
(<${path})
1072 device_queue_set_smp_affinity
() {
1077 local processor
=${3}
1079 local path
="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1080 assert
[ -w "${path}" ]
1082 log DEBUG
"Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1084 __processor_id_to_bitmap
${processor} > ${path}