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