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