summaryrefslogtreecommitdiffstats
path: root/agents/ocf/ifspeed.in
blob: 8c07c3d7bac5f4bca60abc3d813649838b187af4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!@BASH_PATH@
#
# ocf:pacemaker:ifspeed resource agent
#
# Copyright 2011-2023 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This source code is licensed under the GNU General Public License version 2
# or later (GPLv2+) WITHOUT ANY WARRANTY.
#

#
# Record speed of a network interface as a node attribute, based on the sum of
# speeds of its active (up, link detected, not blocked) underlying interfaces.
#
# Originally based on ocf:pacemaker:ping agent
#

: ${OCF_FUNCTIONS:="${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs"}

# If these aren't available, we can still show help,
# which is all that is needed to build the man pages.
[ -r "${OCF_FUNCTIONS}" ] && . "${OCF_FUNCTIONS}"
[ -r "${OCF_FUNCTIONS_DIR}/findif.sh" ] && . "${OCF_FUNCTIONS_DIR}/findif.sh"
: ${OCF_SUCCESS:=0}

: ${__OCF_ACTION:=$1}

FINDIF=findif

# Defaults
OCF_RESKEY_name_default="ifspeed"
OCF_RESKEY_bridge_ports_default="detect"
OCF_RESKEY_weight_base_default=1000
OCF_RESKEY_dampen_default=5

# Explicitly list all environment variables used, to make static analysis happy
: ${OCF_RESKEY_name:=${OCF_RESKEY_name_default}}
: ${OCF_RESKEY_bridge_ports:=${OCF_RESKEY_bridge_ports_default}}
: ${OCF_RESKEY_weight_base:=${OCF_RESKEY_weight_base_default}}
: ${OCF_RESKEY_dampen:=${OCF_RESKEY_dampen_default}}
: ${OCF_RESKEY_iface:=""}
: ${OCF_RESKEY_ip:=""}
: ${OCF_RESKEY_debug:="false"}

meta_data() {
        cat <<END
<?xml version="1.0"?>
<resource-agent name="ifspeed" version="@VERSION@">
<version>1.1</version>

<longdesc lang="en">
This agent's monitor action records the speed of a specified network interface
as a node attribute. The attribute can be used in rules to prefer nodes based
on network speeds.

This agent can monitor physical interfaces, bonded interfaces, bridges, VLANs,
or any combination thereof. For example:

*) Bridge on top of one 10Gbps interface (eth2) and 802.3ad bonding (bond0) built
   on two 1Gbps interfaces (eth0 and eth1).
*) Active-backup bonding built on top of one physical interface and one VLAN on
   another interface.

For STP-enabled bridges, this agent tries to determine the network topology, and
by default looks only on ports which are connected to an upstream switch. This
can be overridden by 'bridge_ports' parameter. Active interfaces in this case
are those in "forwarding" state.

For balancing bonded interfaces, this agent uses 80% of the sum of the speeds of
underlying "up" ports.

For non-balancing bonded interfaces ("active-backup" and probably "broadcast"),
only the speed of the currently active port is considered.
</longdesc>
<shortdesc lang="en">Network interface speed monitor</shortdesc>

<parameters>

<parameter name="name" unique-group="name">
<longdesc lang="en">
Name of the node attribute to set
</longdesc>
<shortdesc lang="en">Attribute name</shortdesc>
<content type="string" default="${OCF_RESKEY_name_default}"/>
</parameter>

<parameter name="iface" unique-group="iface">
<longdesc lang="en">
If this is set, monitor this network interface. One of iface or ip must be set.
</longdesc>
<shortdesc lang="en">Network interface</shortdesc>
<content type="string" default=""/>
</parameter>

<parameter name="ip" unique-group="ip">
<longdesc lang="en">
If this is set instead of iface, monitor the interface that holds this IP
address. The address may be specified in dotted-quad notation for IPv4 (for
example, 192.168.1.1) or hexadecimal notation for IPv6 (for example,
2001:db8:DC28:0:0:FC57:D4C8:1FFF). One of iface or ip must be set.
</longdesc>
<shortdesc lang="en">IPv4 or IPv6 address</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="bridge_ports" unique-group="iface">
<longdesc lang="en">
If set and iface is a bridge, consider these bridge ports (by default, all ports
which have designated_bridge=root_id)
</longdesc>
<shortdesc lang="en">Bridge ports</shortdesc>
<content type="string" default="${OCF_RESKEY_bridge_ports_default}"/>
</parameter>

<parameter name="weight_base">
<longdesc lang="en">
Relative weight of 1Gbps in interface speed.
Can be used to tune how big attribute value will be.
</longdesc>
<shortdesc lang="en">Weight of 1Gbps</shortdesc>
<content type="integer" default="${OCF_RESKEY_weight_base_default}"/>
</parameter>

<parameter name="dampen" reloadable="1">
<longdesc lang="en">
The time to wait (dampening) for further changes to occur.
</longdesc>
<shortdesc lang="en">Dampening interval</shortdesc>
<content type="integer" default="${OCF_RESKEY_dampen_default}"/>
</parameter>

<parameter name="debug">
<longdesc lang="en">
Log more verbosely.
</longdesc>
<shortdesc lang="en">Verbose logging</shortdesc>
<content type="string" default="false"/>
</parameter>

</parameters>

<actions>
<action name="start"   timeout="30s" />
<action name="stop"    timeout="30s" />
<action name="monitor" depth="0"  timeout="30s" interval="10s"/>
<action name="meta-data"  timeout="5s" />
<action name="validate-all"  timeout="30s" depth="0" />
<action name="reload-agent"  timeout="20s" />
</actions>
</resource-agent>
END
}

usage() {
    cat <<END
Usage: $0 {start|stop|monitor|validate-all|meta-data|reload-agent}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

start() {
    monitor
    if [ $? -eq $OCF_SUCCESS ]; then
        return $OCF_SUCCESS
    fi
    ha_pseudo_resource ${ha_pseudo_resource_name} start
    update
    return $?
}

stop() {
    ha_pseudo_resource "${ha_pseudo_resource_name}" stop
    attrd_updater -D -n "${OCF_RESKEY_name}" ${attrd_options}
    return $OCF_SUCCESS
}

monitor() {
    local ret

    ha_pseudo_resource "${ha_pseudo_resource_name}" monitor
    ret=$?
    if [ ${ret} -eq $OCF_SUCCESS ] ; then
        update
    fi
    return ${ret}
}

get_nic_name_by_ip() {
    # $FINDIF takes its parameters from the environment.
    # Its output is as follows:
    # [NIC_NAME] netmask [NETMASK] broadcast [BROADCAST}
    NICINFO=$( "${FINDIF}" )
    rc=$?
    if [ $rc -eq 0 ];then
        # Get NIC_NAME part of findif function output.
        echo "${NICINFO%% *}"
    else
        echo ""
    fi
}

validate() {
    if [ -z "${OCF_RESKEY_iface}" ]; then
        if [ -z "${OCF_RESKEY_ip}" ]; then
            ocf_log err "Must specify either an interface name or valid IP address"
            ocf_exit_reason "no interface or IP address specified"
            exit $OCF_ERR_CONFIGURED
        else
            ipcheck_ipv4 "${OCF_RESKEY_ip}"
            if [ $? -eq 1 ] ; then
                ipcheck_ipv6 "${OCF_RESKEY_ip}"
                if [ $? -eq 1 ] ; then
                  ocf_exit_reason "'${OCF_RESKEY_ip}' is not a valid IP"
                  exit $OCF_ERR_CONFIGURED
                fi
            fi
        fi
    fi

    # Host-specific checks
    if [ "$1" = "10" ]; then
        if [ "$(uname)" != "Linux" ] ; then
            ocf_log err "This resource agent works only on Linux"
            ocf_exit_reason "not Linux"
            exit $OCF_ERR_INSTALLED
        fi
    fi
    return $OCF_SUCCESS
}

iface_get_speed() {
    local iface="$1"
    local operstate
    local carrier
    local speed

    if [ ! -e "/sys/class/net/${iface}" ] ; then
        echo "0"
    elif iface_is_bridge "${iface}" ; then # bridges do not have operstate
        read carrier < "/sys/class/net/${iface}/carrier"

        if [ "${carrier}" != "1" ] ; then
            echo "0"
        else
            bridge_get_speed "${iface}"
        fi
    else
        read operstate < "/sys/class/net/${iface}/operstate"
        read carrier < "/sys/class/net/${iface}/carrier"

        if [ "${operstate}" != "up" ] || [ "${carrier}" != "1" ] ; then
            echo "0"
        elif iface_is_bond "${iface}" ; then
            bond_get_speed "${iface}"
        elif iface_is_vlan "${iface}" ; then
            iface_get_speed "$(vlan_get_phy "${iface}")"
        elif iface_is_hfi1 "${iface}" ; then
            hfi1_get_speed "${iface}"
        else
            read speed < "/sys/class/net/${iface}/speed"
            echo "${speed}"
        fi
    fi
}

iface_is_vlan() {
    local iface="$1"

    [ -e "/proc/net/vlan/${iface}" ] && return 0 || return 1
}

iface_is_bridge() {
    local iface="$1"

    [ -e "/sys/class/net/${iface}/bridge" ] && return 0 || return 1
}

iface_is_bond() {
    local iface="$1"

    [ -e "/sys/class/net/${iface}/bonding" ] && return 0 || return 1
}

iface_is_hfi1() {
    local iface="$1"

    driver=$(readlink "/sys/class/net/${iface}/device/driver")
    [[ $(basename "${driver}") =~ "hfi1" ]] && return 0 || return 1
}

vlan_get_phy() {
    local iface="$1"

    sed -ne "s/^${iface} .*| *//p" < /proc/net/vlan/config
}

bridge_is_stp_enabled() {
    local iface="$1"
    local stp

    read stp < "/sys/class/net/${iface}/bridge/stp_state"
    [ "${stp}" = "1" ] && return 0 || return 1
}

bridge_get_root_ports() {
    local bridge="$1"
    local root_id
    local root_ports=""
    local bridge_id

    read root_id < "/sys/class/net/${bridge}/bridge/root_id"

    for port in /sys/class/net/${bridge}/brif/* ; do
        read bridge_id < "${port}/designated_bridge"
        if [ "${bridge_id}" = "${root_id}" ] ; then
            root_ports="${root_ports} ${port##*/}"
        fi
    done

    root_ports=${root_ports# }

    if [ -n "$2" ] ; then # Record value in specified var. This expects we were called not in a sub-shell.
        eval "$2=\${root_ports}"
    else # Expect sub-shell
        echo ${root_ports}
    fi
}

# From /usr/include/linux/if_bridge.h:
#define BR_STATE_DISABLED 0
#define BR_STATE_LISTENING 1
#define BR_STATE_LEARNING 2
#define BR_STATE_FORWARDING 3
#define BR_STATE_BLOCKING 4

bridge_get_active_ports() {
    local bridge="$1"
    shift 1
    local ports="$*"
    local active_ports=""
    local port_state
    local stp_state
    local warn=0

    bridge_is_stp_enabled "${bridge}"
    stp_state=$?

    if [ -z "${ports}" ] || [ "${ports}" = "detect" ] ; then
        bridge_get_root_ports "${bridge}" ports
    fi

    for port in $ports ; do
        if [ ! -e "/sys/class/net/${bridge}/brif/${port}" ] ; then
            ocf_log warning "Port ${port} doesn't belong to bridge ${bridge}"
            continue
        fi
        read port_state < "/sys/class/net/${bridge}/brif/${port}/state"
        if [ "${port_state}" = "3" ] ; then
            if [ -n "${active_ports}" ] && ${stp_state} ; then
                warn=1
            fi
            active_ports="${active_ports} ${port}"
        fi
    done
    if [ ${warn} -eq 1 ] ; then
        ocf_log warning "More then one upstream port in bridge '${bridge}' is in forwarding state while STP is enabled: ${active_ports}"
    fi
    echo "${active_ports# }"
}

bridge_get_speed() {
    local iface="$1"
    local aggregate_speed=0

    if ! iface_is_bridge "${iface}" ; then
        echo 0
        return
    fi

    BGS_PORTS=$( bridge_get_active_ports "${iface}" "${OCF_RESKEY_bridge_ports}" )
    for port in ${BGS_PORTS} ; do
        : $(( aggregate_speed += $( iface_get_speed "${port}" ) ))
    done
    if [ -n "$2" ] ; then # Record value in specified var. This expects we were called not in a sub-shell.
        eval "$2=\${aggregate_speed}"
    else # Expect sub-shell
        echo ${aggregate_speed}
    fi
}

hfi1_get_speed() {
    local iface="$1"
    local hfi1_speed
    local hfi1_value
    local hfi1_desc

    # At least as of 9/14/2017 Intel Omni Path v10.5.0.0.155, Intel doesn't have
    # dual- or multiple-port Host Channel Adapters, and it's safe to use this
    # method to get the speed. Example output:
    # [root@es-host0 ~]# cat /sys/class/net/ib0/device/infiniband/*/ports/*/rate
    # 100 Gb/sec (4X EDR)
    read hfi1_speed hfi1_value hfi1_desc < "/sys/class/net/${iface}/device/infiniband"/*/ports/*/rate
    ocf_is_true "${OCF_RESKEY_debug}" && ocf_log debug "Detected speed $hfi1_speed $hfi1_value $hfi1_desc"

    # hfi1_value always in Gb/sec, so we need to convert hfi1_speed in Mb/sec
    echo $(( hfi1_speed * 1000 ))
}

bond_get_ports() {
    local iface="$1"
    local ports

    read ports < "/sys/class/net/${iface}/bonding/slaves"
    if [ -n "$2" ] ; then # Record value in specified var. This expects we were called not in a sub-shell.
        eval "$2=\${ports}"
    else # Expect sub-shell
        echo ${ports}
    fi
}

bond_get_active_iface() {
    local iface="$1"
    local active

    read active < "/sys/class/net/${iface}/bonding/active_slave"
    if [ -n "$2" ] ; then # Record value in specified var. This expects we were called not in a sub-shell.
        eval "$2=\${active}"
    else # Expect sub-shell
        echo ${active}
    fi
}

bond_is_balancing() {
    local iface="$1"
    read mode mode_index < "/sys/class/net/${iface}/bonding/mode"
    ocf_is_true "${OCF_RESKEY_debug}" && ocf_log debug "Detected balancing $mode $mode_index"
    case "${mode}" in
        "balance-rr"|"balance-xor"|"802.3ad"|"balance-tlb"|"balance-alb")
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

bond_get_speed() {
    local iface="$1"
    local aggregate_speed=0
    local active_iface
    local bond_ports

    if ! iface_is_bond "${iface}" ; then
        echo 0
        return
    fi

    bond_get_ports "${iface}" bond_ports

    if bond_is_balancing "${iface}" ; then
        for port in ${bond_ports} ; do
            : $(( aggregate_speed += $( iface_get_speed "${port}" ) ))
        done
        # Bonding is unable to get speed*n
        : $(( aggregate_speed = aggregate_speed * 8 / 10 ))
    else
        bond_get_active_iface "${iface}" "active_iface"
        aggregate_speed=$( iface_get_speed "$active_iface" )
    fi
    if [ -n "$2" ] ; then # Record value in specified var. This expects we were called not in a sub-shell.
        eval "$2=\${aggregate_speed}"
    else # Expect sub-shell
        echo ${aggregate_speed}
    fi
}

update() {
    local speed;
    local nic="${OCF_RESKEY_iface}";

    if [ -z "${OCF_RESKEY_iface}" ]; then
        nic=$( get_nic_name_by_ip )
        if [ -z "${nic}" ];then
            ocf_log err "Could not determine network interface name from IP address (${OCF_RESKEY_ip})"
            ocf_exit_reason "unable to determine interface name"
            exit $OCF_ERR_GENERIC
        fi
    fi
    speed=$( iface_get_speed "${nic}" )

    : $(( score = speed * ${OCF_RESKEY_weight_base} / 1000 ))
    attrd_updater -n "${OCF_RESKEY_name}" -B "${score}" -d "${OCF_RESKEY_dampen}" ${attrd_options}
    rc=$?
    case ${rc} in
        0)
            ocf_is_true "${OCF_RESKEY_debug}" && ocf_log debug "Updated ${OCF_RESKEY_name} = ${score}"
            ;;
        *)
            ocf_log warn "Could not update ${OCF_RESKEY_name} = ${score}: rc=${rc}"
            ;;
    esac
    return ${rc}
}

reload_agent() {
    return $OCF_SUCCESS
}

case $__OCF_ACTION in
    meta-data)
        meta_data
        exit $OCF_SUCCESS
        ;;
    usage|help)
        usage
        exit $OCF_SUCCESS
        ;;
esac

: ${ha_pseudo_resource_name:="ifspeed-${OCF_RESOURCE_INSTANCE}"}

attrd_options=''
if ocf_is_true "${OCF_RESKEY_debug}" ; then
    attrd_options='-VV'
fi

case "$__OCF_ACTION" in
    start)
        validate 10
        start
        ;;
    stop)
        validate 10
        stop
        ;;
    monitor)
        validate 10
        monitor
        ;;
    validate-all)
        validate "$OCF_CHECK_LEVEL"
        ;;
    reload-agent)
        reload_agent
        ;;
    *)
        usage
        exit $OCF_ERR_UNIMPLEMENTED
        ;;
esac

exit $?

# vim: set filetype=sh expandtab tabstop=4 softtabstop=4 shiftwidth=4 textwidth=80: