Merge branch 'upstream-16.11-stable' into 16.11.x
[deb_dpdk.git] / debian / tests / test-initscripts
1 #!/bin/sh
2 set -e
3
4 basedir=$(dirname "$0")
5 . "${basedir}"/check-dpdk-supported-arch.sh
6
7 # Overall that could require up to 1.2G for hugepages in the test environment
8 EXPECT2MHP=10
9 # Some page sizes like e.g. 1G might not be available in all test environments
10 # The test still configures 1 page of 1G size.
11 # One of two things will happen, depending on the test environment:
12 # - has 1G huge page size => they will tried to be allocated (usually env is
13 #   too small, but we want to see it fail gracefully for that)
14 #   We will not check for the 1G alloc, as we know it often fails in small adt's
15 # - has no 1G huge page size (HW feature) => we check if it fails gracefully
16 EXPECT1GHP=1
17 EXPECT16MHP=2
18
19 DPDK_CONF="/etc/dpdk/dpdk.conf"
20 DPDK_INTERF="/etc/dpdk/interfaces"
21
22 checkhp() {
23     MMDIR="/sys/kernel/mm/hugepages/${1}"
24     EXPECTHP="${2}"
25     if [ -d "$MMDIR" -a -r "$MMDIR/nr_hugepages" ]; then
26         hpcount=$(cat "$MMDIR/nr_hugepages")
27         if [ "${hpcount}" -ne "${EXPECTHP}" ]; then
28             echo "Hugepages (${hpcount}) not as expected (${EXPECTHP})"
29             exit 1
30         else
31             echo "Hugepages ok (${hpcount})"
32         fi
33     fi
34 }
35
36 checkstatus() {
37     MARK=${1}
38     EXPMPCOUNT=${2}
39     PRE=${3}
40     POST=${4}
41     EXPECTEDSTATUS=${5}
42     echo "Status after ${MARK}"
43     echo "Status of the Service"
44     ${PRE} status "${POST}" || true
45
46     GOTSTATUS=$(${PRE} status "${POST}" | awk '/^ *Active: / { print $2 }')
47     if [ "${GOTSTATUS}" != "${EXPECTEDSTATUS}" ]; then
48         echo "Service status (${GOTSTATUS}) not as expected (${EXPECTEDSTATUS})"
49         exit 1
50     else
51         echo "Service status (${GOTSTATUS}) as expected"
52     fi
53
54     echo "Status of hugetlbfs mount points"
55     # this section is ok to create bad RCs when no mounts are available
56     set +e
57     grep hugetlbfs < /proc/mounts
58     htlbfscount=$(grep -c hugetlbfs < /proc/mounts)
59     set -e
60
61     # we have to reduce the expected mountpoint count in case some sizes are
62     # not supported by the current kernel/environment
63     if [ ${EXPMPCOUNT} -gt 0 ]; then
64         if [ ! -d /sys/kernel/mm/hugepages/hugepages-2048kB ]; then
65             EXPMPCOUNT=$((EXPMPCOUNT-1))
66         fi
67         if [ ! -d /sys/kernel/mm/hugepages/hugepages-16384kB ]; then
68             EXPMPCOUNT=$((EXPMPCOUNT-1))
69         fi
70         if [ ! -d /sys/kernel/mm/hugepages/hugepages-1048576kB ]; then
71             EXPMPCOUNT=$((EXPMPCOUNT-1))
72         fi
73     fi
74
75     if [ "${htlbfscount}" -eq "${EXPMPCOUNT}" ]; then
76         echo "MP Count (${htlbfscount}) as expected (${EXPMPCOUNT})"
77     else
78         echo "MP Count (${htlbfscount}) not as expected (${EXPMPCOUNT})"
79         exit 1
80     fi
81
82     # check if setting HP worked
83     if [ "${EXPMPCOUNT}" -ne "0" ]; then
84         checkhp "hugepages-2048kB" "${EXPECT2MHP}"
85         checkhp "hugepages-16384kB" "${EXPECT16MHP}"
86         # We do not check 1G/16M alloc, as it is known to be often not available
87     fi
88 }
89
90 resetservice() {
91     # help a bit with memory fragmentation regarding huge page allocation
92     sync
93     echo 3 > /proc/sys/vm/drop_caches
94
95     # stopping and resetting Service
96     systemctl stop dpdk.service || /bin/true
97     systemctl reset-failed dpdk.service || /bin/true
98
99     echo "Unmounting all potential hugetlbfs mounts"
100     awk '/hugetlbfs/ {print $2}' /proc/mounts | while read hugetlbmount; do
101         umount -v "$hugetlbmount"
102     done
103 }
104
105 checkinitstyle() {
106     # We want to verify that
107     # - initially our environment has no hugetlbfs mount
108     # - a system without hugetlbfs mount gets it mounted
109     # - a restart of the service does neither drop nor duplicate the mount
110     PRE=${1}
111     POST=${2}
112     TYPE=${3}
113     printf "\n\n### Checking Type %s ###\n" "${TYPE}"
114     resetservice
115     checkstatus "${TYPE}-BEGIN" 0 "${PRE}" "${POST}" "inactive"
116     echo "### Starting Service ###"
117     ${PRE} start "${POST}"
118     checkstatus "${TYPE}-START" 3 "${PRE}" "${POST}" "active"
119     echo "### Restarting Service ###"
120     ${PRE} restart "${POST}"
121     checkstatus "${TYPE}-RESTART" 3 "${PRE}" "${POST}" "active"
122 }
123
124 echo "NR_2M_PAGES=$EXPECT2MHP" >> ${DPDK_CONF}
125 echo "NR_1G_PAGES=$EXPECT1GHP" >> ${DPDK_CONF}
126 echo "NR_16M_PAGES=$EXPECT16MHP" >> ${DPDK_CONF}
127
128 # We can't rely on any real device for DPDK tests in adt-* environments. But
129 # we can expect all kind of broken configuration not to break it (would be
130 # detected via set -e).
131 # So add all kind of known-to-be-broken definitions and expect it not to fail.
132 cat <<EOF > ${DPDK_INTERF}
133 # wrong bus
134 pTi 0000:04:00.0 uio-pci-generic
135 # not enough parms
136 0000:04:00.0 uio-pci-generic
137 # empty line
138
139 # non existing device
140 pci 1234:56:78.9 uio-pci-generic
141 EOF
142
143 # some had issues in the past caused by different init systems, so we test all
144 # Direct Calls
145 checkinitstyle "/etc/init.d/dpdk" "" "Direct"
146 # System V style init
147 checkinitstyle "service dpdk" "" "SysV"
148 # SystemD style init
149 checkinitstyle "systemctl" "dpdk.service" "SystemD"