]>
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 | ||
a508c27e | 290 | # Check if the device is a wireless device |
1c6a4e30 | 291 | device_is_wireless() { |
a508c27e MT |
292 | local device=${1} |
293 | ||
294 | [ -d "${SYS_CLASS_NET}/${device}/phy80211" ] | |
295 | } | |
296 | ||
1a02da59 MT |
297 | device_is_vti() { |
298 | local device=${1} | |
299 | ||
300 | local type=$(__device_get_file ${device} type) | |
301 | ||
302 | [ "${type}" = "768" ] && return ${EXIT_OK} || return ${EXIT_ERROR} | |
303 | } | |
304 | ||
1c6a4e30 | 305 | device_get_phy() { |
4733a336 MT |
306 | local device="${1}" |
307 | ||
308 | if device_is_wireless "${device}"; then | |
309 | print "$(<${SYS_CLASS_NET}/${device}/phy80211/name)" | |
310 | return ${EXIT_OK} | |
311 | fi | |
312 | ||
313 | return ${EXIT_ERROR} | |
314 | } | |
315 | ||
1c6a4e30 | 316 | device_is_phy() { |
f90dd58c MT |
317 | phy_exists $@ |
318 | } | |
319 | ||
1c6a4e30 | 320 | device_is_serial() { |
6c74a64c MT |
321 | serial_exists $@ |
322 | } | |
323 | ||
2d6dab20 MT |
324 | # Returns true if a device is a tun device |
325 | device_is_tun() { | |
326 | local device="${1}" | |
327 | ||
328 | [ -e "${SYS_CLASS_NET}/${device}/tun_flags" ] | |
329 | } | |
330 | ||
1848564d | 331 | # Check if the device is a physical network interface |
1c6a4e30 | 332 | device_is_ethernet() { |
1848564d MT |
333 | local device=${1} |
334 | ||
0067696a MT |
335 | device_is_ethernet_compatible "${device}" || \ |
336 | return ${EXIT_ERROR} | |
337 | ||
1848564d MT |
338 | device_is_loopback ${device} && \ |
339 | return ${EXIT_ERROR} | |
340 | ||
341 | device_is_bonding ${device} && \ | |
342 | return ${EXIT_ERROR} | |
343 | ||
344 | device_is_bridge ${device} && \ | |
345 | return ${EXIT_ERROR} | |
346 | ||
347 | device_is_ppp ${device} && \ | |
348 | return ${EXIT_ERROR} | |
349 | ||
7951525a | 350 | device_is_vlan ${device} && \ |
1848564d MT |
351 | return ${EXIT_ERROR} |
352 | ||
0067696a | 353 | device_is_dummy ${device} && \ |
419b4cd0 MT |
354 | return ${EXIT_ERROR} |
355 | ||
2d6dab20 MT |
356 | device_is_tun ${device} && \ |
357 | return ${EXIT_ERROR} | |
358 | ||
1848564d MT |
359 | return ${EXIT_OK} |
360 | } | |
361 | ||
362 | # Get the device type | |
1c6a4e30 | 363 | device_get_type() { |
5bb2429a | 364 | local device=${1} |
1848564d | 365 | |
b9f27baf MT |
366 | # If the device does not exist (happens on udev remove events), |
367 | # we do not bother to run all checks. | |
368 | if ! device_exists "${device}"; then | |
369 | echo "unknown" | |
370 | ||
371 | elif device_is_vlan ${device}; then | |
1848564d MT |
372 | echo "vlan" |
373 | ||
374 | elif device_is_bonding ${device}; then | |
375 | echo "bonding" | |
376 | ||
377 | elif device_is_bridge ${device}; then | |
378 | echo "bridge" | |
379 | ||
380 | elif device_is_ppp ${device}; then | |
381 | echo "ppp" | |
382 | ||
e6993835 MT |
383 | elif device_is_batman_adv ${device}; then |
384 | echo "batman-adv" | |
385 | ||
1848564d MT |
386 | elif device_is_loopback ${device}; then |
387 | echo "loopback" | |
388 | ||
b8026986 MT |
389 | elif device_is_wireless_adhoc ${device}; then |
390 | echo "wireless-adhoc" | |
391 | ||
a508c27e MT |
392 | elif device_is_wireless ${device}; then |
393 | echo "wireless" | |
394 | ||
0067696a MT |
395 | elif device_is_dummy ${device}; then |
396 | echo "dummy" | |
397 | ||
2d6dab20 MT |
398 | elif device_is_tun ${device}; then |
399 | echo "tun" | |
400 | ||
ec63256a MT |
401 | elif device_is_ethernet ${device}; then |
402 | echo "ethernet" | |
1848564d | 403 | |
6c74a64c MT |
404 | elif device_is_serial ${device}; then |
405 | echo "serial" | |
406 | ||
f90dd58c MT |
407 | elif device_is_phy ${device}; then |
408 | echo "phy" | |
409 | ||
1a02da59 MT |
410 | elif device_is_vti ${device}; then |
411 | echo "vti" | |
412 | ||
1848564d MT |
413 | else |
414 | echo "unknown" | |
415 | fi | |
416 | } | |
417 | ||
1c6a4e30 | 418 | device_is_ethernet_compatible() { |
a4f7ad26 MT |
419 | local device="${1}" |
420 | ||
421 | # /sys/class/net/*/type must equal 1 for ethernet compatible devices | |
422 | local type="$(__device_get_file "${device}" "type")" | |
423 | [[ "${type}" = "1" ]] | |
424 | } | |
425 | ||
1c6a4e30 | 426 | device_get_status() { |
711ffac1 | 427 | local device=${1} |
711ffac1 MT |
428 | assert isset device |
429 | ||
3cb2fc42 | 430 | local status=${STATUS_DOWN} |
711ffac1 | 431 | |
3cb2fc42 | 432 | if device_is_up ${device}; then |
711ffac1 | 433 | status=${STATUS_UP} |
711ffac1 | 434 | |
3cb2fc42 MT |
435 | if ! device_has_carrier ${device}; then |
436 | status=${STATUS_NOCARRIER} | |
437 | fi | |
438 | fi | |
711ffac1 MT |
439 | |
440 | echo "${status}" | |
441 | } | |
442 | ||
1c6a4e30 | 443 | device_get_address() { |
1848564d MT |
444 | local device=${1} |
445 | ||
446 | cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null | |
447 | } | |
448 | ||
1c6a4e30 | 449 | device_set_address() { |
08c5b789 MT |
450 | assert [ $# -eq 2 ] |
451 | ||
452 | local device="${1}" | |
453 | local addr="${2}" | |
1b7a1578 | 454 | |
08c5b789 | 455 | if ! device_exists "${device}"; then |
1b7a1578 MT |
456 | error "Device '${device}' does not exist." |
457 | return ${EXIT_ERROR} | |
458 | fi | |
459 | ||
08c5b789 MT |
460 | # Do nothing if the address has not changed |
461 | local old_addr="$(device_get_address "${device}")" | |
462 | if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then | |
463 | return ${EXIT_OK} | |
464 | fi | |
465 | ||
466 | log DEBUG "Setting address of '${device}' from '${old_addr}' to '${addr}'" | |
1b7a1578 MT |
467 | |
468 | local up | |
08c5b789 MT |
469 | if device_is_up "${device}"; then |
470 | device_set_down "${device}" | |
1b7a1578 MT |
471 | up=1 |
472 | fi | |
473 | ||
08c5b789 | 474 | ip link set "${device}" address "${addr}" |
1b7a1578 MT |
475 | local ret=$? |
476 | ||
477 | if [ "${up}" = "1" ]; then | |
08c5b789 | 478 | device_set_up "${device}" |
1b7a1578 MT |
479 | fi |
480 | ||
481 | if [ "${ret}" != "0" ]; then | |
08c5b789 | 482 | error_log "Could not set address '${addr}' on device '${device}'" |
1b7a1578 MT |
483 | fi |
484 | ||
485 | return ${ret} | |
1848564d MT |
486 | } |
487 | ||
1c6a4e30 | 488 | device_get() { |
2ae0fb8d | 489 | local device |
711ffac1 MT |
490 | local devices |
491 | ||
2ae0fb8d MT |
492 | for device in ${SYS_CLASS_NET}/*; do |
493 | device=$(basename ${device}) | |
711ffac1 | 494 | |
2ae0fb8d MT |
495 | # bonding_masters is no device |
496 | [ "${device}" = "bonding_masters" ] && continue | |
497 | ||
498 | devices="${devices} ${device}" | |
499 | done | |
711ffac1 MT |
500 | |
501 | echo ${devices} | |
502 | return ${EXIT_OK} | |
503 | } | |
504 | ||
1c6a4e30 | 505 | devices_get_all() { |
711ffac1 | 506 | device_get |
1848564d MT |
507 | } |
508 | ||
509 | # Check if a device has a cable plugged in | |
1c6a4e30 | 510 | device_has_carrier() { |
5bb2429a MT |
511 | local device=${1} |
512 | assert isset device | |
513 | ||
ec63256a MT |
514 | local carrier=$(__device_get_file ${device} carrier) |
515 | [ "${carrier}" = "1" ] | |
1848564d MT |
516 | } |
517 | ||
1c6a4e30 | 518 | device_is_promisc() { |
1e4c26a4 MT |
519 | local device=${1} |
520 | ||
e369be1a | 521 | device_has_flag ${device} 0x200 |
1e4c26a4 MT |
522 | } |
523 | ||
1c6a4e30 | 524 | device_set_promisc() { |
cf6e4606 MT |
525 | local device=${1} |
526 | local state=${2} | |
527 | ||
528 | assert device_exists ${device} | |
529 | assert isset state | |
530 | assert isoneof state on off | |
531 | ||
532 | ip link set ${device} promisc ${state} | |
533 | } | |
534 | ||
1848564d | 535 | # Check if the device is free |
1c6a4e30 | 536 | device_is_free() { |
81ed640c | 537 | ! device_is_used $@ |
1848564d MT |
538 | } |
539 | ||
540 | # Check if the device is used | |
1c6a4e30 | 541 | device_is_used() { |
5bb2429a | 542 | local device=${1} |
1848564d | 543 | |
7951525a | 544 | device_has_vlans ${device} && \ |
fb02e543 | 545 | return ${EXIT_OK} |
1848564d | 546 | device_is_bonded ${device} && \ |
fb02e543 | 547 | return ${EXIT_OK} |
81ed640c MT |
548 | device_is_bridge_attached ${device} && \ |
549 | return ${EXIT_OK} | |
1848564d | 550 | |
fb02e543 | 551 | return ${EXIT_ERROR} |
1848564d MT |
552 | } |
553 | ||
1b7a1578 | 554 | # Give the device a new name |
1c6a4e30 | 555 | device_set_name() { |
1848564d | 556 | local source=$1 |
1578dae9 | 557 | local destination=${2} |
1848564d MT |
558 | |
559 | # Check if devices exists | |
560 | if ! device_exists ${source} || device_exists ${destination}; then | |
561 | return 4 | |
562 | fi | |
563 | ||
564 | local up | |
565 | if device_is_up ${source}; then | |
566 | ip link set ${source} down | |
567 | up=1 | |
568 | fi | |
569 | ||
570 | ip link set ${source} name ${destination} | |
571 | ||
572 | if [ "${up}" = "1" ]; then | |
573 | ip link set ${destination} up | |
574 | fi | |
575 | } | |
576 | ||
1848564d | 577 | # Set device up |
1c6a4e30 | 578 | device_set_up() { |
45546be1 | 579 | assert [ $# -eq 1 ] |
1848564d | 580 | |
45546be1 | 581 | local device=${1} |
711ffac1 | 582 | |
1848564d MT |
583 | # Do nothing if device is already up |
584 | device_is_up ${device} && return ${EXIT_OK} | |
585 | ||
f18ee3d4 | 586 | log INFO "Bringing up ${device}" |
81ed640c | 587 | |
f18ee3d4 | 588 | device_set_parent_up ${device} |
45546be1 MT |
589 | if ! cmd ip link set ${device} up; then |
590 | return ${EXIT_ERROR} | |
591 | fi | |
de72bd91 MT |
592 | |
593 | # Set SMP affinity | |
594 | if interrupt_use_smp_affinity; then | |
505ead5d | 595 | device_auto_configure_smp_affinity ${device} |
de72bd91 MT |
596 | fi |
597 | ||
598 | return ${EXIT_OK} | |
1848564d MT |
599 | } |
600 | ||
1c6a4e30 | 601 | device_set_parent_up() { |
81ed640c MT |
602 | local device=${1} |
603 | local parent | |
604 | ||
7951525a MT |
605 | if device_is_vlan ${device}; then |
606 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
607 | |
608 | device_is_up ${parent} && return ${EXIT_OK} | |
609 | ||
610 | log DEBUG "Setting up parent device '${parent}' of '${device}'" | |
611 | ||
612 | device_set_up ${parent} | |
613 | return $? | |
614 | fi | |
615 | ||
616 | return ${EXIT_OK} | |
617 | } | |
618 | ||
1848564d | 619 | # Set device down |
1c6a4e30 | 620 | device_set_down() { |
45546be1 | 621 | assert [ $# -eq 1 ] |
1848564d | 622 | |
45546be1 | 623 | local device=${1} |
81ed640c MT |
624 | local ret=${EXIT_OK} |
625 | ||
626 | if device_is_up ${device}; then | |
f18ee3d4 | 627 | log INFO "Bringing down ${device}" |
81ed640c | 628 | |
45546be1 | 629 | cmd ip link set ${device} down |
81ed640c MT |
630 | ret=$? |
631 | fi | |
632 | ||
633 | device_set_parent_down ${device} | |
1848564d | 634 | |
81ed640c MT |
635 | return ${ret} |
636 | } | |
637 | ||
1c6a4e30 | 638 | device_set_parent_down() { |
81ed640c MT |
639 | local device=${1} |
640 | local parent | |
641 | ||
7951525a MT |
642 | if device_is_vlan ${device}; then |
643 | parent=$(vlan_get_parent ${device}) | |
81ed640c MT |
644 | |
645 | device_is_up ${parent} || return ${EXIT_OK} | |
646 | ||
647 | if device_is_free ${parent}; then | |
648 | log DEBUG "Tearing down parent device '${parent}' of '${device}'" | |
649 | ||
650 | device_set_down ${parent} | |
651 | fi | |
652 | fi | |
653 | ||
654 | return ${EXIT_OK} | |
1848564d MT |
655 | } |
656 | ||
1c6a4e30 | 657 | device_get_mtu() { |
1848564d MT |
658 | local device=${1} |
659 | ||
491e4f92 MT |
660 | # Return an error if the device does not exist |
661 | device_exists ${device} || return ${EXIT_ERROR} | |
1848564d | 662 | |
f3e6fe50 | 663 | echo $(<${SYS_CLASS_NET}/${device}/mtu) |
1848564d MT |
664 | } |
665 | ||
666 | # Set mtu to a device | |
1c6a4e30 | 667 | device_set_mtu() { |
1b7a1578 | 668 | local device=${1} |
1848564d MT |
669 | local mtu=${2} |
670 | ||
491e4f92 | 671 | assert device_exists ${device} |
1b7a1578 | 672 | |
3464bc89 AF |
673 | # Handle bridges differently |
674 | if device_is_bridge ${device}; then | |
675 | local port | |
676 | for port in $(bridge_get_members ${device}); do | |
677 | device_set_mtu ${port} ${mtu} | |
678 | done | |
679 | fi | |
680 | ||
491e4f92 | 681 | log INFO "Setting MTU of ${device} to ${mtu}" |
1b7a1578 | 682 | |
1848564d | 683 | local up |
1b7a1578 MT |
684 | if device_is_up ${device}; then |
685 | device_set_down ${device} | |
1848564d MT |
686 | up=1 |
687 | fi | |
688 | ||
491e4f92 MT |
689 | local ret=${EXIT_OK} |
690 | if ! cmd ip link set ${device} mtu ${mtu}; then | |
691 | ret=${EXIT_ERROR} | |
1848564d | 692 | |
491e4f92 | 693 | log ERROR "Could not set MTU ${mtu} on ${device}" |
1b7a1578 MT |
694 | fi |
695 | ||
491e4f92 MT |
696 | if [ "${up}" = "1" ]; then |
697 | device_set_up ${device} | |
1848564d MT |
698 | fi |
699 | ||
700 | return ${ret} | |
701 | } | |
702 | ||
1c6a4e30 | 703 | device_adjust_mtu() { |
3ee5ccb1 MT |
704 | assert [ $# -eq 2 ] |
705 | ||
706 | local device="${1}" | |
707 | local other_device="${2}" | |
708 | ||
709 | local mtu="$(device_get_mtu "${other_device}")" | |
710 | device_set_mtu "${device}" "${mtu}" | |
711 | } | |
712 | ||
1c6a4e30 | 713 | device_discover() { |
1848564d MT |
714 | local device=${1} |
715 | ||
1b7a1578 MT |
716 | log INFO "Running discovery process on device '${device}'." |
717 | ||
1848564d | 718 | local hook |
d61a01d4 MT |
719 | for hook in $(hook_zone_get_all); do |
720 | hook_zone_exec ${hook} discover ${device} | |
1848564d MT |
721 | done |
722 | } | |
723 | ||
f5ee091e MT |
724 | device_identify() { |
725 | assert [ $# -ge 1 ] | |
726 | ||
727 | local device="${1}" | |
728 | ||
729 | # Flash for ten seconds by default | |
730 | local seconds="10" | |
731 | ||
732 | # Run in background? | |
733 | local background="false" | |
734 | ||
735 | local arg | |
736 | while read arg; do | |
737 | case "${arg}" in | |
738 | --background) | |
739 | background="true" | |
740 | ;; | |
741 | --seconds=*) | |
742 | seconds="$(cli_get_val "${arg}")" | |
743 | ;; | |
744 | esac | |
745 | done <<< "$(args $@)" | |
746 | ||
747 | assert isinteger seconds | |
748 | ||
749 | if ! device_exists "${device}"; then | |
750 | log ERROR "Cannot identify device ${device}: Does not exist" | |
751 | return ${EXIT_ERROR} | |
752 | fi | |
753 | ||
754 | if ! device_is_ethernet "${device}"; then | |
755 | log DEBUG "Cannot identify device ${device}: Not an ethernet device" | |
756 | return ${EXIT_NOT_SUPPORTED} | |
757 | fi | |
758 | ||
759 | log DEBUG "Identifying device ${device}" | |
760 | ||
761 | local command="ethtool --identify ${device} ${seconds}" | |
762 | local ret=0 | |
763 | ||
764 | if enabled background; then | |
765 | cmd_background "${command}" | |
766 | else | |
767 | cmd_quiet "${command}" | |
768 | ret=$? | |
769 | fi | |
770 | ||
771 | return ${ret} | |
772 | } | |
773 | ||
1c6a4e30 | 774 | device_has_ip() { |
1848564d MT |
775 | local device=${1} |
776 | local addr=${2} | |
777 | ||
38f61548 MT |
778 | assert isset addr |
779 | assert device_exists ${device} | |
780 | ||
781 | # IPv6 addresses must be fully imploded | |
782 | local protocol=$(ip_detect_protocol ${addr}) | |
783 | case "${protocol}" in | |
784 | ipv6) | |
13a6e69f | 785 | addr=$(ipv6_format "${addr}") |
38f61548 MT |
786 | ;; |
787 | esac | |
1848564d | 788 | |
8c9205b1 | 789 | list_match ${addr} $(device_get_addresses ${device}) |
1848564d | 790 | } |
4231f419 | 791 | |
1c6a4e30 | 792 | device_get_addresses() { |
4231f419 | 793 | local device=${1} |
4231f419 | 794 | |
38f61548 | 795 | assert device_exists ${device} |
4231f419 | 796 | |
38f61548 MT |
797 | local prot |
798 | local addr | |
799 | local line | |
800 | ip addr show ${device} | \ | |
801 | while read prot addr line; do | |
802 | [ "${prot:0:4}" = "inet" ] && echo "${addr}" | |
803 | done | |
4231f419 | 804 | } |
711ffac1 | 805 | |
1c6a4e30 | 806 | __device_get_file() { |
711ffac1 MT |
807 | local device=${1} |
808 | local file=${2} | |
809 | ||
750aae10 | 810 | fread "${SYS_CLASS_NET}/${device}/${file}" |
711ffac1 MT |
811 | } |
812 | ||
1c6a4e30 | 813 | __device_set_file() { |
2c083d57 MT |
814 | assert [ $# -eq 3 ] |
815 | ||
816 | local device="${1}" | |
817 | local file="${2}" | |
818 | local value="${3}" | |
819 | ||
750aae10 | 820 | fwrite "${SYS_CLASS_NET}/${device}/${file}" "${value}" |
2c083d57 MT |
821 | } |
822 | ||
1c6a4e30 | 823 | device_get_rx_bytes() { |
711ffac1 MT |
824 | local device=${1} |
825 | ||
826 | __device_get_file ${device} statistics/rx_bytes | |
827 | } | |
828 | ||
1c6a4e30 | 829 | device_get_tx_bytes() { |
711ffac1 MT |
830 | local device=${1} |
831 | ||
832 | __device_get_file ${device} statistics/tx_bytes | |
833 | } | |
834 | ||
1c6a4e30 | 835 | device_get_rx_packets() { |
711ffac1 MT |
836 | local device=${1} |
837 | ||
838 | __device_get_file ${device} statistics/rx_packets | |
839 | } | |
840 | ||
1c6a4e30 | 841 | device_get_tx_packets() { |
711ffac1 MT |
842 | local device=${1} |
843 | ||
844 | __device_get_file ${device} statistics/tx_packets | |
845 | } | |
846 | ||
1c6a4e30 | 847 | device_get_rx_errors() { |
711ffac1 MT |
848 | local device=${1} |
849 | ||
850 | __device_get_file ${device} statistics/rx_errors | |
851 | } | |
852 | ||
1c6a4e30 | 853 | device_get_tx_errors() { |
711ffac1 MT |
854 | local device=${1} |
855 | ||
856 | __device_get_file ${device} statistics/tx_errors | |
857 | } | |
ec63256a | 858 | |
1c6a4e30 | 859 | device_get_speed() { |
ec63256a MT |
860 | local device=${1} |
861 | ||
0d2d02da MT |
862 | local speed=$(__device_get_file ${device} speed) |
863 | ||
f887607c MT |
864 | # Exit for no output (i.e. no link detected) |
865 | isset speed || return ${EXIT_ERROR} | |
866 | ||
0d2d02da MT |
867 | # Don't return anything for negative values |
868 | [ ${speed} -lt 0 ] && return ${EXIT_ERROR} | |
869 | ||
870 | print "${speed}" | |
ec63256a MT |
871 | } |
872 | ||
1c6a4e30 | 873 | device_get_duplex() { |
ec63256a MT |
874 | local device=${1} |
875 | ||
cd0f7e51 MT |
876 | local duplex=$(__device_get_file ${device} duplex) |
877 | ||
878 | case "${duplex}" in | |
879 | unknown) | |
880 | return ${EXIT_ERROR} | |
881 | ;; | |
882 | *) | |
883 | print "${duplex}" | |
884 | ;; | |
885 | esac | |
ec63256a | 886 | } |
657540d8 | 887 | |
1c6a4e30 | 888 | device_get_link_string() { |
657540d8 MT |
889 | local device="${1}" |
890 | assert isset device | |
891 | ||
892 | local s | |
893 | ||
894 | local speed="$(device_get_speed "${device}")" | |
895 | if isset speed; then | |
896 | list_append s "${speed} MBit/s" | |
897 | fi | |
898 | ||
899 | local duplex="$(device_get_duplex "${device}")" | |
900 | if isset duplex; then | |
901 | list_append s "${duplex} duplex" | |
902 | fi | |
903 | ||
904 | print "${s}" | |
905 | } | |
de72bd91 MT |
906 | |
907 | device_auto_configure_smp_affinity() { | |
908 | assert [ $# -eq 1 ] | |
909 | ||
910 | local device=${1} | |
911 | ||
c73dc5dc | 912 | if lock_acquire "smp-affinity" 60; then |
505ead5d | 913 | device_set_smp_affinity ${device} auto |
de72bd91 MT |
914 | |
915 | lock_release "smp-affinity" | |
916 | fi | |
917 | } | |
918 | ||
919 | device_set_smp_affinity() { | |
920 | assert [ $# -eq 2 ] | |
921 | ||
922 | local device=${1} | |
923 | local mode=${2} | |
924 | ||
925 | # mode can be auto which will automatically try to find | |
926 | # the least busy processor, or an integer for the desired | |
927 | # processor that should handle this device | |
928 | ||
929 | local num_processors=$(system_get_processors) | |
930 | ||
931 | if [ "${mode}" = "auto" ]; then | |
932 | local processor=$(interrupt_choose_least_busy_processor) | |
933 | else | |
934 | assert isinteger mode | |
935 | local processor=${mode} | |
936 | ||
937 | if [ ${processor} -gt ${num_processors} ]; then | |
938 | log ERROR "Processor ${processor} does not exist" | |
939 | return ${EXIT_ERROR} | |
940 | fi | |
941 | fi | |
942 | ||
943 | local interrupts=$(interrupts_for_device ${device}) | |
944 | if ! isset interrupts; then | |
945 | log DEBUG "${device} has no interrupts. Not changing SMP affinity" | |
946 | return ${EXIT_OK} | |
947 | fi | |
948 | ||
949 | # Set SMP affinity | |
950 | local interrupt | |
951 | for interrupt in ${interrupts}; do | |
952 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
953 | done | |
954 | ||
955 | # Find all queues and assign them to the next processor | |
956 | local queue | |
957 | for queue in $(device_get_queues ${device}); do | |
958 | case "${queue}" in | |
959 | # Only handle receive queues | |
960 | rx-*) | |
961 | for interrupt in $(interrupts_for_device_queue ${device} ${queue}); do | |
962 | interrupt_set_smp_affinity ${interrupt} ${processor} | |
963 | done | |
964 | ||
965 | device_queue_set_smp_affinity ${device} ${queue} ${processor} | |
966 | ;; | |
967 | ||
968 | # Ignore the rest | |
969 | *) | |
970 | continue | |
971 | ;; | |
972 | esac | |
973 | ||
974 | # Get the next available processor if in auto mode | |
975 | [ "${mode}" = "auto" ] && processor=$(system_get_next_processor ${processor}) | |
976 | done | |
977 | ||
978 | return ${EXIT_OK} | |
979 | } | |
980 | ||
981 | device_get_queues() { | |
982 | assert [ $# -eq 1 ] | |
983 | ||
984 | local device=${1} | |
985 | ||
986 | local queue | |
987 | for queue in ${SYS_CLASS_NET}/${device}/queues/*; do | |
89c73b14 MT |
988 | [ -d "${queue}" ] || continue |
989 | ||
de72bd91 MT |
990 | basename "${queue}" |
991 | done | |
992 | } | |
993 | ||
6529dfaa MT |
994 | device_supports_multiqueue() { |
995 | local device=${1} | |
996 | ||
997 | local num_queues=$(device_num_queues ${device}) | |
998 | ||
999 | if isset num_queues && [ ${num_queues} -gt 2 ]; then | |
1000 | return ${EXIT_TRUE} | |
1001 | fi | |
1002 | ||
1003 | return ${EXIT_FALSE} | |
1004 | } | |
1005 | ||
1006 | device_num_queues() { | |
1007 | local device=${1} | |
1008 | local type=${2} | |
1009 | ||
e396e74e | 1010 | isset type && assert isoneof type rx tx |
6529dfaa MT |
1011 | |
1012 | local i=0 | |
1013 | ||
1014 | local q | |
1015 | for q in $(device_get_queues ${device}); do | |
1016 | case "${type},${q}" in | |
1017 | rx,rx-*) | |
1018 | (( i++ )) | |
1019 | ;; | |
1020 | tx,tx-*) | |
1021 | (( i++ )) | |
1022 | ;; | |
1023 | *,*) | |
1024 | (( i++ )) | |
1025 | ;; | |
1026 | esac | |
1027 | done | |
1028 | ||
1029 | print ${i} | |
1030 | } | |
1031 | ||
1032 | device_queue_get_smp_affinity() { | |
1033 | assert [ $# -eq 2 ] | |
1034 | ||
1035 | local device=${1} | |
1036 | local queue=${2} | |
1037 | ||
e396e74e MT |
1038 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}" |
1039 | ||
1040 | case "${queue}" in | |
1041 | rx-*) | |
1042 | path="${path}/rps_cpus" | |
1043 | ;; | |
1044 | tx-*) | |
1045 | path="${path}/xps_cpus" | |
1046 | ;; | |
1047 | esac | |
6529dfaa MT |
1048 | assert [ -r "${path}" ] |
1049 | ||
1050 | __bitmap_to_processor_ids $(<${path}) | |
1051 | } | |
1052 | ||
de72bd91 MT |
1053 | device_queue_set_smp_affinity() { |
1054 | assert [ $# -eq 3 ] | |
1055 | ||
1056 | local device=${1} | |
1057 | local queue=${2} | |
1058 | local processor=${3} | |
1059 | ||
1060 | local path="${SYS_CLASS_NET}/${device}/queues/${queue}/rps_cpus" | |
1061 | assert [ -w "${path}" ] | |
1062 | ||
1063 | log DEBUG "Setting SMP affinity of ${device} (${queue}) to processor ${processor}" | |
1064 | ||
1065 | __processor_id_to_bitmap ${processor} > ${path} | |
1066 | } |