Add initial meta-stx to support StarlingX build
[pti/rtp.git] / meta-stx / recipes-daemons / lldpd / files / i40e-lldp-configure.sh
1 #!/bin/bash
2 ################################################################################
3 # Copyright (c) 2016 Wind River Systems, Inc.
4 #
5 # SPDX-License-Identifier: Apache-2.0
6 #
7 ################################################################################
8
9 # Certain i40e network devices (XL710 Fortville) have an internal firmware LLDP
10 # agent enabled by default. This can prevent LLDP PDUs from being processed by
11 # the driver and any upper layer agents.
12 #
13 # This script allows a user to enable and disable the internal LLDP agent.
14 #
15 # Note: debugfs must be enabled in the kernel
16 #
17 # To enable:
18 # ./i40e-lldp-configure.sh start
19 #
20 # To disable:
21 # ./i40e-lldp-configure.sh stop
22
23 PROGNAME=$(basename $0)
24 DEBUGFS_PATH=/sys/kernel/debug
25 DEBUGFS_I40_DEVICES_PATH=$DEBUGFS_PATH/i40e
26 LLDP_COMMAND=lldp
27
28 function log {
29     local MSG="${PROGNAME}: $1"
30     logger -p notice "${MSG}"
31 }
32
33 function err {
34     local MSG="${PROGNAME}: $1"
35     logger -p error "${MSG}"
36 }
37
38 function configure_device {
39     local DEVICE=$1
40     local ACTION=$2
41     local DEVICE_PATH=${DEBUGFS_I40_DEVICES}/${DEVICE}
42
43     if [ ! -d ${DEVICE_PATH} ]; then
44         return 1
45     fi
46
47     echo "${LLDP_COMMAND} ${ACTION}" > ${DEVICE_PATH}/command
48     RET=$?
49
50     if [ ${RET} -ne 0 ]; then
51         err "Failed to ${ACTION} internal LLDP agent for device ${DEVICE}"
52         return ${RET}
53     fi
54
55     log "${ACTION} internal LLDP agent for device ${DEVICE}"
56     return ${RET}
57 }
58
59 function is_debugfs_mounted {
60     if grep -qs "${DEBUGFS_PATH}" /proc/mounts; then
61     return 0
62     fi
63     return 1
64 }
65
66 function mount_debugfs {
67     mount -t debugfs none ${DEBUGFS_PATH}
68 }
69
70 function unmount_debugfs {
71     umount ${DEBUGFS_PATH}
72 }
73
74 function scan_devices {
75     local ACTION=$1
76     local DEBUGFS_MOUNTED="false"
77     local DEVICES=${DEBUGFS_I40_DEVICES_PATH}/*
78
79     if is_debugfs_mounted; then
80         DEBUGFS_MOUNTED="true"
81     fi
82
83     if [ ${DEBUGFS_MOUNTED} = "false" ]; then
84         mount_debugfs
85         RET=$?
86         if [ ${RET} -ne 0 ]; then
87             err "Failed to mount debugfs"
88             return ${RET}
89         fi
90         log "Mounted debugfs"
91     fi
92
93     for DEVICE in $DEVICES; do
94         configure_device ${DEVICE} ${ACTION}
95     done
96
97     if [ ${DEBUGFS_MOUNTED} = "false" ]; then
98         unmount_debugfs
99         RET=$?
100         if [ ${RET} -ne 0 ]; then
101             err "Failed to unmount debugfs"
102             return ${RET}
103         fi
104         log "Unmounted debugfs"
105     fi
106
107     return 0
108 }
109
110 function start {
111     scan_devices start
112     return $?
113 }
114
115 function stop {
116     scan_devices stop
117     return $?
118 }
119
120 function status {
121     return 0
122 }
123
124 case "$1" in
125     start)
126         start
127         ;;
128     stop)
129         stop
130         ;;
131     restart)
132         stop
133         start
134         ;;
135     status)
136         status
137         ;;
138     *)
139         echo "Usage: $0 {start|stop|restart|status}"
140         exit 1
141 esac