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