]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.device
Move offloading code into an own file
[people/ms/network.git] / src / functions / functions.device
CommitLineData
1848564d
MT
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
5b1fd814
MT
22declare -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
1c6a4e30 32device_list() {
f90dd58c
MT
33 local devices
34
35 # Add all interfaces
91aeb1be
MT
36 local device
37 for device in $(list_directory ${SYS_CLASS_NET}); do
38 list_append_one devices "${device}"
39 done
f90dd58c
MT
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
1848564d 51# Check if the device exists
1c6a4e30 52device_exists() {
1848564d
MT
53 local device=${1}
54
55 # If device name was not found, exit.
56 [ -n "${device}" ] || return ${EXIT_ERROR}
57
6c74a64c
MT
58 # Check for a normal network device.
59 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
60
f90dd58c
MT
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,
6c74a64c
MT
66 # we check for serial devices.
67 serial_exists ${device}
1848564d
MT
68}
69
1c6a4e30 70device_matches_pattern() {
a23fdc0e
MT
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
1c6a4e30 83device_delete() {
99be6026
MT
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
cdcce259
MT
90 # Shut down device before we delete it
91 device_set_down "${device}"
92
99be6026
MT
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
1c6a4e30 105device_has_flag() {
e369be1a
MT
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
1848564d 118# Check if the device is up
1c6a4e30 119device_is_up() {
1848564d
MT
120 local device=${1}
121
122 device_exists ${device} || return ${EXIT_ERROR}
123
e369be1a 124 device_has_flag ${device} 0x1
1848564d
MT
125}
126
1c6a4e30 127device_ifindex_to_name() {
99be6026
MT
128 local idx=${1}
129 assert isset idx
130
131 local device device_idx
60b1f378 132 for device in $(list_directory "${SYS_CLASS_NET}"); do
99be6026
MT
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
1c6a4e30 144device_get_ifindex() {
99be6026
MT
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
1848564d 156# Check if the device is a bonding device
1c6a4e30 157device_is_bonding() {
1848564d
MT
158 [ -d "/sys/class/net/${1}/bonding" ]
159}
160
161# Check if the device bonded in a bonding device
1c6a4e30 162device_is_bonded() {
711ffac1 163 local device=${1}
1848564d 164
0959482b 165 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave" ]
1848564d
MT
166}
167
168# Check if the device is a bridge
1c6a4e30 169device_is_bridge() {
1848564d
MT
170 [ -d "/sys/class/net/${1}/bridge" ]
171}
172
1c6a4e30 173device_is_bridge_attached() {
81ed640c 174 local device=${1}
81ed640c
MT
175 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
176}
177
1c6a4e30 178device_is_wireless_monitor() {
a23fdc0e
MT
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
1c6a4e30 186device_is_wireless_adhoc() {
b8026986
MT
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
1c6a4e30 194device_get_bridge() {
99be6026
MT
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
7951525a 210# Check if the device is a vlan device
1c6a4e30 211device_is_vlan() {
1848564d 212 local device=${1}
7951525a 213 assert isset device
1848564d 214
7951525a 215 [ -e "${PROC_NET_VLAN}/${device}" ]
1848564d
MT
216}
217
7951525a 218# Check if the device has vlan devices
1c6a4e30 219device_has_vlans() {
fb02e543 220 local device=${1}
7951525a 221 assert isset device
fb02e543 222
7951525a 223 if device_is_vlan ${device}; then
ec63256a 224 return ${EXIT_FALSE}
fb02e543
MT
225 fi
226
7951525a
MT
227 local vlans=$(device_get_vlans ${device})
228 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
ec63256a
MT
229}
230
1c6a4e30 231device_get_vlans() {
ec63256a 232 local device=${1}
7951525a 233 assert isset device
ec63256a 234
8357a7ff
MT
235 # If no 8021q module has been loaded into the kernel,
236 # we cannot do anything.
7951525a 237 [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK}
8357a7ff 238
ec63256a
MT
239 local dev spacer1 id spacer2 parent
240 while read dev spacer1 id spacer2 parent; do
7951525a
MT
241 [ "${parent}" = "${device}" ] || continue
242
243 print "${dev}"
244 done < ${PROC_NET_VLAN_CONFIG}
1848564d
MT
245}
246
1848564d 247# Check if the device is a ppp device
1c6a4e30 248device_is_ppp() {
1848564d
MT
249 local device=${1}
250
55b802cc 251 local type=$(__device_get_file ${device} type)
28f0b4ab 252
e369be1a
MT
253 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
254}
55b802cc 255
e369be1a 256# Check if the device is a pointopoint device.
1c6a4e30 257device_is_ptp() {
e369be1a
MT
258 local device=${1}
259
260 device_has_flag ${device} 0x10
1848564d
MT
261}
262
263# Check if the device is a loopback device
1c6a4e30 264device_is_loopback() {
5bb2429a
MT
265 local device=${1}
266
1848564d
MT
267 [ "${device}" = "lo" ]
268}
269
0067696a
MT
270# Check if the device is a dummy device
271# This is the worst possible check, but all I could come up with
1c6a4e30 272device_is_dummy() {
0067696a
MT
273 local device="${1}"
274
275 [[ ${device} =~ ^dummy[0-9]+$ ]]
276}
277
82fac748
MT
278device_is_ipsec() {
279 local device="${1}"
280
281 [[ ${device} =~ ^ipsec\- ]]
282}
283
a508c27e 284# Check if the device is a wireless device
1c6a4e30 285device_is_wireless() {
a508c27e
MT
286 local device=${1}
287
288 [ -d "${SYS_CLASS_NET}/${device}/phy80211" ]
289}
290
1a02da59
MT
291device_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
bcba5d27
JS
299device_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
1c6a4e30 307device_get_phy() {
4733a336
MT
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
1c6a4e30 318device_is_phy() {
2212045f 319 phy_exists "$@"
f90dd58c
MT
320}
321
1c6a4e30 322device_is_serial() {
2212045f 323 serial_exists "$@"
6c74a64c
MT
324}
325
2d6dab20
MT
326# Returns true if a device is a tun device
327device_is_tun() {
328 local device="${1}"
329
330 [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ]
331}
332
1848564d 333# Check if the device is a physical network interface
1c6a4e30 334device_is_ethernet() {
1848564d
MT
335 local device=${1}
336
0067696a
MT
337 device_is_ethernet_compatible "${device}" || \
338 return ${EXIT_ERROR}
339
1848564d
MT
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
7951525a 352 device_is_vlan ${device} && \
1848564d
MT
353 return ${EXIT_ERROR}
354
0067696a 355 device_is_dummy ${device} && \
419b4cd0
MT
356 return ${EXIT_ERROR}
357
2d6dab20
MT
358 device_is_tun ${device} && \
359 return ${EXIT_ERROR}
360
1848564d
MT
361 return ${EXIT_OK}
362}
363
364# Get the device type
1c6a4e30 365device_get_type() {
5bb2429a 366 local device=${1}
1848564d 367
b9f27baf
MT
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
1848564d
MT
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
b8026986
MT
388 elif device_is_wireless_adhoc ${device}; then
389 echo "wireless-adhoc"
390
a508c27e
MT
391 elif device_is_wireless ${device}; then
392 echo "wireless"
393
0067696a
MT
394 elif device_is_dummy ${device}; then
395 echo "dummy"
396
2d6dab20
MT
397 elif device_is_tun ${device}; then
398 echo "tun"
399
ec63256a
MT
400 elif device_is_ethernet ${device}; then
401 echo "ethernet"
1848564d 402
6c74a64c
MT
403 elif device_is_serial ${device}; then
404 echo "serial"
405
f90dd58c
MT
406 elif device_is_phy ${device}; then
407 echo "phy"
408
e173e612
JS
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
416device_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
1a02da59
MT
424 elif device_is_vti ${device}; then
425 echo "vti"
426
e173e612
JS
427 elif device_is_vti6 ${device}; then
428 echo "vti6"
429
1848564d
MT
430 else
431 echo "unknown"
432 fi
433}
434
1c6a4e30 435device_is_ethernet_compatible() {
a4f7ad26
MT
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
1c6a4e30 443device_get_status() {
711ffac1 444 local device=${1}
711ffac1
MT
445 assert isset device
446
3cb2fc42 447 local status=${STATUS_DOWN}
711ffac1 448
3cb2fc42 449 if device_is_up ${device}; then
711ffac1 450 status=${STATUS_UP}
711ffac1 451
3cb2fc42
MT
452 if ! device_has_carrier ${device}; then
453 status=${STATUS_NOCARRIER}
454 fi
455 fi
711ffac1
MT
456
457 echo "${status}"
458}
459
1c6a4e30 460device_get_address() {
1848564d
MT
461 local device=${1}
462
463 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
464}
465
1c6a4e30 466device_set_address() {
08c5b789
MT
467 assert [ $# -eq 2 ]
468
469 local device="${1}"
470 local addr="${2}"
1b7a1578 471
08c5b789 472 if ! device_exists "${device}"; then
1b7a1578
MT
473 error "Device '${device}' does not exist."
474 return ${EXIT_ERROR}
475 fi
476
08c5b789
MT
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}'"
1b7a1578
MT
484
485 local up
08c5b789
MT
486 if device_is_up "${device}"; then
487 device_set_down "${device}"
1b7a1578
MT
488 up=1
489 fi
490
08c5b789 491 ip link set "${device}" address "${addr}"
1b7a1578
MT
492 local ret=$?
493
494 if [ "${up}" = "1" ]; then
08c5b789 495 device_set_up "${device}"
1b7a1578
MT
496 fi
497
498 if [ "${ret}" != "0" ]; then
08c5b789 499 error_log "Could not set address '${addr}' on device '${device}'"
1b7a1578
MT
500 fi
501
502 return ${ret}
1848564d
MT
503}
504
1c6a4e30 505device_get() {
2ae0fb8d 506 local device
60b1f378 507 for device in $(list_directory "${SYS_CLASS_NET}"); do
2ae0fb8d
MT
508 # bonding_masters is no device
509 [ "${device}" = "bonding_masters" ] && continue
510
60b1f378 511 echo "${device}"
2ae0fb8d 512 done
711ffac1 513
711ffac1
MT
514 return ${EXIT_OK}
515}
516
1848564d 517# Check if a device has a cable plugged in
1c6a4e30 518device_has_carrier() {
5bb2429a
MT
519 local device=${1}
520 assert isset device
521
ec63256a
MT
522 local carrier=$(__device_get_file ${device} carrier)
523 [ "${carrier}" = "1" ]
1848564d
MT
524}
525
1c6a4e30 526device_is_promisc() {
1e4c26a4
MT
527 local device=${1}
528
e369be1a 529 device_has_flag ${device} 0x200
1e4c26a4
MT
530}
531
1c6a4e30 532device_set_promisc() {
cf6e4606
MT
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
1848564d 543# Check if the device is free
1c6a4e30 544device_is_free() {
2212045f 545 ! device_is_used "$@"
1848564d
MT
546}
547
548# Check if the device is used
1c6a4e30 549device_is_used() {
5bb2429a 550 local device=${1}
1848564d 551
7951525a 552 device_has_vlans ${device} && \
fb02e543 553 return ${EXIT_OK}
1848564d 554 device_is_bonded ${device} && \
fb02e543 555 return ${EXIT_OK}
81ed640c
MT
556 device_is_bridge_attached ${device} && \
557 return ${EXIT_OK}
1848564d 558
fb02e543 559 return ${EXIT_ERROR}
1848564d
MT
560}
561
1b7a1578 562# Give the device a new name
1c6a4e30 563device_set_name() {
1848564d 564 local source=$1
1578dae9 565 local destination=${2}
1848564d
MT
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
0e523702
MT
585device_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
600device_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
1848564d 612# Set device up
1c6a4e30 613device_set_up() {
45546be1 614 assert [ $# -eq 1 ]
1848564d 615
45546be1 616 local device=${1}
711ffac1 617
1848564d
MT
618 # Do nothing if device is already up
619 device_is_up ${device} && return ${EXIT_OK}
620
f18ee3d4 621 log INFO "Bringing up ${device}"
81ed640c 622
f18ee3d4 623 device_set_parent_up ${device}
45546be1
MT
624 if ! cmd ip link set ${device} up; then
625 return ${EXIT_ERROR}
626 fi
de72bd91
MT
627
628 # Set SMP affinity
629 if interrupt_use_smp_affinity; then
505ead5d 630 device_auto_configure_smp_affinity ${device}
de72bd91
MT
631 fi
632
633 return ${EXIT_OK}
1848564d
MT
634}
635
1c6a4e30 636device_set_parent_up() {
81ed640c
MT
637 local device=${1}
638 local parent
639
7951525a
MT
640 if device_is_vlan ${device}; then
641 parent=$(vlan_get_parent ${device})
81ed640c
MT
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
1848564d 654# Set device down
1c6a4e30 655device_set_down() {
45546be1 656 assert [ $# -eq 1 ]
1848564d 657
45546be1 658 local device=${1}
81ed640c
MT
659 local ret=${EXIT_OK}
660
661 if device_is_up ${device}; then
f18ee3d4 662 log INFO "Bringing down ${device}"
81ed640c 663
45546be1 664 cmd ip link set ${device} down
81ed640c
MT
665 ret=$?
666 fi
667
668 device_set_parent_down ${device}
1848564d 669
81ed640c
MT
670 return ${ret}
671}
672
1c6a4e30 673device_set_parent_down() {
81ed640c
MT
674 local device=${1}
675 local parent
676
7951525a
MT
677 if device_is_vlan ${device}; then
678 parent=$(vlan_get_parent ${device})
81ed640c
MT
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}
1848564d
MT
690}
691
1c6a4e30 692device_get_mtu() {
1848564d
MT
693 local device=${1}
694
491e4f92
MT
695 # Return an error if the device does not exist
696 device_exists ${device} || return ${EXIT_ERROR}
1848564d 697
f3e6fe50 698 echo $(<${SYS_CLASS_NET}/${device}/mtu)
1848564d
MT
699}
700
701# Set mtu to a device
1c6a4e30 702device_set_mtu() {
1b7a1578 703 local device=${1}
1848564d
MT
704 local mtu=${2}
705
491e4f92 706 assert device_exists ${device}
1b7a1578 707
3464bc89
AF
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
491e4f92 716 log INFO "Setting MTU of ${device} to ${mtu}"
1b7a1578 717
1848564d 718 local up
1b7a1578
MT
719 if device_is_up ${device}; then
720 device_set_down ${device}
1848564d
MT
721 up=1
722 fi
723
491e4f92
MT
724 local ret=${EXIT_OK}
725 if ! cmd ip link set ${device} mtu ${mtu}; then
726 ret=${EXIT_ERROR}
1848564d 727
491e4f92 728 log ERROR "Could not set MTU ${mtu} on ${device}"
1b7a1578
MT
729 fi
730
491e4f92
MT
731 if [ "${up}" = "1" ]; then
732 device_set_up ${device}
1848564d
MT
733 fi
734
735 return ${ret}
736}
737
1c6a4e30 738device_adjust_mtu() {
3ee5ccb1
MT
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
1c6a4e30 748device_discover() {
1848564d
MT
749 local device=${1}
750
1b7a1578
MT
751 log INFO "Running discovery process on device '${device}'."
752
1848564d 753 local hook
d61a01d4
MT
754 for hook in $(hook_zone_get_all); do
755 hook_zone_exec ${hook} discover ${device}
1848564d
MT
756 done
757}
758
f5ee091e
MT
759device_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
2212045f 780 done <<< "$(args "$@")"
f5ee091e
MT
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
1c6a4e30 809device_has_ip() {
1848564d
MT
810 local device=${1}
811 local addr=${2}
812
38f61548
MT
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)
13a6e69f 820 addr=$(ipv6_format "${addr}")
38f61548
MT
821 ;;
822 esac
1848564d 823
8c9205b1 824 list_match ${addr} $(device_get_addresses ${device})
1848564d 825}
4231f419 826
1c6a4e30 827device_get_addresses() {
4231f419 828 local device=${1}
4231f419 829
38f61548 830 assert device_exists ${device}
4231f419 831
38f61548
MT
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
4231f419 839}
711ffac1 840
1c6a4e30 841__device_get_file() {
711ffac1
MT
842 local device=${1}
843 local file=${2}
844
750aae10 845 fread "${SYS_CLASS_NET}/${device}/${file}"
711ffac1
MT
846}
847
1c6a4e30 848__device_set_file() {
2c083d57
MT
849 assert [ $# -eq 3 ]
850
851 local device="${1}"
852 local file="${2}"
853 local value="${3}"
854
644d3bb8 855 fappend "${SYS_CLASS_NET}/${device}/${file}" "${value}"
2c083d57
MT
856}
857
1c6a4e30 858device_get_rx_bytes() {
711ffac1
MT
859 local device=${1}
860
861 __device_get_file ${device} statistics/rx_bytes
862}
863
1c6a4e30 864device_get_tx_bytes() {
711ffac1
MT
865 local device=${1}
866
867 __device_get_file ${device} statistics/tx_bytes
868}
869
1c6a4e30 870device_get_rx_packets() {
711ffac1
MT
871 local device=${1}
872
873 __device_get_file ${device} statistics/rx_packets
874}
875
1c6a4e30 876device_get_tx_packets() {
711ffac1
MT
877 local device=${1}
878
879 __device_get_file ${device} statistics/tx_packets
880}
881
1c6a4e30 882device_get_rx_errors() {
711ffac1
MT
883 local device=${1}
884
885 __device_get_file ${device} statistics/rx_errors
886}
887
1c6a4e30 888device_get_tx_errors() {
711ffac1
MT
889 local device=${1}
890
891 __device_get_file ${device} statistics/tx_errors
892}
ec63256a 893
5b1fd814
MT
894device_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
1c6a4e30 929device_get_speed() {
ec63256a
MT
930 local device=${1}
931
0d2d02da
MT
932 local speed=$(__device_get_file ${device} speed)
933
f887607c
MT
934 # Exit for no output (i.e. no link detected)
935 isset speed || return ${EXIT_ERROR}
936
0d2d02da
MT
937 # Don't return anything for negative values
938 [ ${speed} -lt 0 ] && return ${EXIT_ERROR}
939
940 print "${speed}"
ec63256a
MT
941}
942
1c6a4e30 943device_get_duplex() {
ec63256a
MT
944 local device=${1}
945
cd0f7e51
MT
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
ec63256a 956}
657540d8 957
1c6a4e30 958device_get_link_string() {
657540d8
MT
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}
de72bd91
MT
976
977device_auto_configure_smp_affinity() {
978 assert [ $# -eq 1 ]
979
980 local device=${1}
981
c73dc5dc 982 if lock_acquire "smp-affinity" 60; then
505ead5d 983 device_set_smp_affinity ${device} auto
de72bd91
MT
984
985 lock_release "smp-affinity"
986 fi
987}
988
989device_set_smp_affinity() {
990 assert [ $# -eq 2 ]
991
992 local device=${1}
993 local mode=${2}
994
995 # mode can be auto which will automatically try to find
996 # the least busy processor, or an integer for the desired
997 # processor that should handle this device
998
999 local num_processors=$(system_get_processors)
1000
1001 if [ "${mode}" = "auto" ]; then
1002 local processor=$(interrupt_choose_least_busy_processor)
1003 else
1004 assert isinteger mode
1005 local processor=${mode}
1006
1007 if [ ${processor} -gt ${num_processors} ]; then
1008 log ERROR "Processor ${processor} does not exist"
1009 return ${EXIT_ERROR}
1010 fi
1011 fi
1012
1013 local interrupts=$(interrupts_for_device ${device})
1014 if ! isset interrupts; then
1015 log DEBUG "${device} has no interrupts. Not changing SMP affinity"
1016 return ${EXIT_OK}
1017 fi
1018
1019 # Set SMP affinity
1020 local interrupt
1021 for interrupt in ${interrupts}; do
1022 interrupt_set_smp_affinity ${interrupt} ${processor}
1023 done
1024
1025 # Find all queues and assign them to the next processor
1026 local queue
1027 for queue in $(device_get_queues ${device}); do
1028 case "${queue}" in
1029 # Only handle receive queues
1030 rx-*)
1031 for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do
1032 interrupt_set_smp_affinity ${interrupt} ${processor}
1033 done
1034
1035 device_queue_set_smp_affinity ${device} ${queue} ${processor}
1036 ;;
1037
1038 # Ignore the rest
1039 *)
1040 continue
1041 ;;
1042 esac
1043
1044 # Get the next available processor if in auto mode
1045 [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor})
1046 done
1047
1048 return ${EXIT_OK}
1049}
1050
1051device_get_queues() {
1052 assert [ $# -eq 1 ]
1053
1054 local device=${1}
1055
60b1f378 1056 list_directory "${SYS_CLASS_NET}/${device}/queues"
de72bd91
MT
1057}
1058
6529dfaa
MT
1059device_supports_multiqueue() {
1060 local device=${1}
1061
1062 local num_queues=$(device_num_queues ${device})
1063
1064 if isset num_queues && [ ${num_queues} -gt 2 ]; then
1065 return ${EXIT_TRUE}
1066 fi
1067
1068 return ${EXIT_FALSE}
1069}
1070
1071device_num_queues() {
1072 local device=${1}
1073 local type=${2}
1074
e396e74e 1075 isset type && assert isoneof type rx tx
6529dfaa
MT
1076
1077 local i=0
1078
1079 local q
1080 for q in $(device_get_queues ${device}); do
1081 case "${type},${q}" in
1082 rx,rx-*)
1083 (( i++ ))
1084 ;;
1085 tx,tx-*)
1086 (( i++ ))
1087 ;;
1088 *,*)
1089 (( i++ ))
1090 ;;
1091 esac
1092 done
1093
1094 print ${i}
1095}
1096
1097device_queue_get_smp_affinity() {
1098 assert [ $# -eq 2 ]
1099
1100 local device=${1}
1101 local queue=${2}
1102
e396e74e
MT
1103 local path="${SYS_CLASS_NET}/${device}/queues/${queue}"
1104
1105 case "${queue}" in
1106 rx-*)
1107 path="${path}/rps_cpus"
1108 ;;
1109 tx-*)
1110 path="${path}/xps_cpus"
1111 ;;
1112 esac
6529dfaa
MT
1113 assert [ -r "${path}" ]
1114
1115 __bitmap_to_processor_ids $(<${path})
1116}
1117
de72bd91
MT
1118device_queue_set_smp_affinity() {
1119 assert [ $# -eq 3 ]
1120
1121 local device=${1}
1122 local queue=${2}
1123 local processor=${3}
1124
1125 local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus"
1126 assert [ -w "${path}" ]
1127
1128 log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}"
1129
1130 __processor_id_to_bitmap ${processor} > ${path}
1131}
905b13b3
JS
1132
1133# Tries to find a device which has the given IP address assigned
1134device_get_by_assigned_ip_address() {
1135 local ip=${1}
1136
1137 assert isset ip
1138
1139 local device
1140
1141 # Read the first line of ip addr show to
1142 read -r device <<< $(ip addr show to "${ip}")
1143
1144 # If we did not found a device we return with ${EXIT_ERROR}
1145 if ! isset device; then
1146 return ${EXIT_ERROR}
1147 fi
1148
1149 # We get something like:
1150 # 3: upl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
1151 # and we want upl0 so we take the second word and removing the :
1152 device=(${device})
1153 device=${device[1]}
1154 device=${device%:}
1155
1156 print "${device}"
1157 return ${EXIT_OK}
1158}
e8253eba
JS
1159
1160device_get_by_mac_address() {
1161 local mac=${1}
1162
1163 assert isset mac
1164
1165 local device
1166
1167 for device in $(device_list); do
1168 if [ "${mac}" = "$(device_get_address ${device})" ]; then
1169 print "${device}"
1170 return ${EXIT_OK}
1171 fi
1172 done
1173
1174 # We could not found a port to the given mac address so we return exit error
1175 return ${EXIT_ERROR}
1176}