Merge "Openstack: Using VSPERF to Test on Openstack."
[vswitchperf.git] / tools / md-testvnf / scripts / deploycentostools.sh
1 #!/usr/bin/env bash
2 ##
3 ## Copyright (c) 2010-2020 Intel Corporation
4 ##
5 ## Licensed under the Apache License, Version 2.0 (the "License");
6 ## you may not use this file except in compliance with the License.
7 ## You may obtain a copy of the License at
8 ##
9 ##     http://www.apache.org/licenses/LICENSE-2.0
10 ##
11 ## Unless required by applicable law or agreed to in writing, software
12 ## distributed under the License is distributed on an "AS IS" BASIS,
13 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ## See the License for the specific language governing permissions and
15 ## limitations under the License.
16 ##
17
18 # Directory for package build
19 BUILD_DIR="/opt/rapid"
20 TREX_DIR="/opt/trex"
21 RAMSPEED_DIR="/opt/ramspeed"
22 STRESSNG_DIR="/opt/stressng"
23 UNIXBENCH_DIR="/opt/unixbench"
24 DPDK_VERSION="20.05"
25 PROX_COMMIT="80dfeb5c734cc4d681f467e853a541a8a91fe1cf"
26 PROX_CHECKOUT="git checkout ${PROX_COMMIT}"
27 ## Next line is overruling the PROX_COMMIT and will replace the version with a very specific patch. Should be commented out
28 ##      if you want to use a committed version of PROX with the COMMIT ID specified above
29 ## As an example: Following line has the commit for testing IMIX, IPV6, ... It is the merge of all PROX commits on May 27th 2020
30 #PROX_CHECKOUT="git fetch \"https://gerrit.opnfv.org/gerrit/samplevnf\" refs/changes/23/70223/1 && git checkout FETCH_HEAD"
31 MULTI_BUFFER_LIB_VER="0.52"
32 export RTE_SDK="${BUILD_DIR}/dpdk-${DPDK_VERSION}"
33 export RTE_TARGET="x86_64-native-linuxapp-gcc"
34
35 # By default, do not update OS
36 OS_UPDATE="n"
37 # By default, asumming that we are in the VM
38 K8S_ENV="n"
39
40 # If already running from root, no need for sudo
41 SUDO=""
42 [ $(id -u) -ne 0 ] && SUDO="sudo"
43
44 function os_pkgs_install()
45 {
46         ${SUDO} yum install -y deltarpm yum-utils
47
48         # NASM repository for AESNI MB library
49         #${SUDO} yum-config-manager --add-repo http://www.nasm.us/nasm.repo
50
51         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
52         ${SUDO} yum install -y git wget gcc unzip libpcap-devel ncurses-devel \
53                          libedit-devel lua-devel kernel-devel iperf3 pciutils \
54                          numactl-devel vim tuna openssl-devel wireshark \
55                          make driverctl
56
57         ${SUDO} wget https://www.nasm.us/pub/nasm/releasebuilds/2.14.02/linux/nasm-2.14.02-0.fc27.x86_64.rpm
58         ${SUDO} rpm -ivh nasm-2.14.02-0.fc27.x86_64.rpm
59 }
60
61 function k8s_os_pkgs_runtime_install()
62 {
63         [ "${OS_UPDATE}" == "y" ] && ${SUDO} yum update -y
64
65         # Install required dynamically linked libraries + required packages
66         ${SUDO} yum install -y numactl-libs libpcap openssh openssh-server \
67                   openssh-clients sudo
68 }
69
70 function os_cfg()
71 {
72         # huge pages to be used by DPDK
73         ${SUDO} sh -c '(echo "vm.nr_hugepages = 1024") > /etc/sysctl.conf'
74
75         ${SUDO} sh -c '(echo "options vfio enable_unsafe_noiommu_mode=1") > /etc/modprobe.d/vfio.conf'
76         ${SUDO} sh -c '(echo "vfio") > /etc/modules-load.d/vfio.conf'
77         ${SUDO} sh -c '(echo "vfio-pci") > /etc/modules-load.d/vfio.conf'
78         # Enabling tuned with the realtime-virtual-guest profile
79         pushd ${BUILD_DIR} > /dev/null 2>&1
80         wget http://linuxsoft.cern.ch/cern/centos/7/rt/x86_64/Packages/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm
81         wget http://linuxsoft.cern.ch/cern/centos/7/rt/x86_64/Packages/tuned-profiles-nfv-guest-2.8.0-5.el7_4.2.noarch.rpm
82         # Install with --nodeps. The latest CentOS cloud images come with a tuned version higher than 2.8. These 2 packages however
83         # do not depend on v2.8 and also work with tuned 2.9. Need to be careful in the future
84         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-realtime-2.8.0-5.el7_4.2.noarch.rpm --nodeps
85         ${SUDO} rpm -ivh ${BUILD_DIR}/tuned-profiles-nfv-guest-2.8.0-5.el7_4.2.noarch.rpm --nodeps
86         # Although we do no know how many cores the VM will have when begin deployed for real testing, we already put a number for the
87         # isolated CPUs so we can start the realtime-virtual-guest profile. If we don't, that command will fail.
88         # When the VM will be instantiated, the check_kernel_params service will check for the real number of cores available to this VM 
89         # and update the realtime-virtual-guest-variables.conf accordingly.
90         echo "isolated_cores=1-3" | ${SUDO} tee -a /etc/tuned/realtime-virtual-guest-variables.conf
91         ${SUDO} tuned-adm profile realtime-virtual-guest
92
93         # Install the check_tuned_params service to make sure that the grub cmd line has the right cpus in isolcpu. The actual number of cpu's
94         # assigned to this VM depends on the flavor used. We don't know at this time what that will be.
95         ${SUDO} chmod +x ${BUILD_DIR}/check_prox_system_setup.sh
96         ${SUDO} mv ${BUILD_DIR}/check_prox_system_setup.sh /usr/local/libexec/
97         ${SUDO} mv ${BUILD_DIR}/check-prox-system-setup.service /etc/systemd/system/
98         ${SUDO} systemctl daemon-reload
99         ${SUDO} systemctl enable check-prox-system-setup.service
100         popd > /dev/null 2>&1
101 }
102
103 function k8s_os_cfg()
104 {
105         [ ! -f /etc/ssh/ssh_host_rsa_key ] && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
106         [ ! -f /etc/ssh/ssh_host_ecdsa_key ] && ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
107         [ ! -f /etc/ssh/ssh_host_ed25519_key ] && ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
108
109         [ ! -d /var/run/sshd ] && mkdir -p /var/run/sshd
110
111         USER_NAME="centos"
112         USER_PWD="centos"
113
114         useradd -m -d /home/${USER_NAME} -s /bin/bash -U ${USER_NAME}
115         echo "${USER_NAME}:${USER_PWD}" | chpasswd
116         usermod -aG wheel ${USER_NAME}
117
118         echo "%wheel ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wheelnopass
119 }
120
121 function mblib_install()
122 {
123         export AESNI_MULTI_BUFFER_LIB_PATH="${BUILD_DIR}/intel-ipsec-mb-${MULTI_BUFFER_LIB_VER}"
124
125         # Downloading the Multi-buffer library. Note that the version to download is linked to the DPDK version being used
126         pushd ${BUILD_DIR} > /dev/null 2>&1
127         wget https://github.com/01org/intel-ipsec-mb/archive/v${MULTI_BUFFER_LIB_VER}.zip
128         unzip v${MULTI_BUFFER_LIB_VER}.zip
129         pushd ${AESNI_MULTI_BUFFER_LIB_PATH}
130         make -j`getconf _NPROCESSORS_ONLN`
131         ${SUDO} make install
132         popd > /dev/null 2>&1
133         popd > /dev/null 2>&1
134 }
135
136 function trex_install()
137 {
138         pushd ${TREX_DIR} > /dev/null 2>&1
139   wget --no-cache https://trex-tgen.cisco.com/trex/release/latest
140   tar -xzvf latest
141   popd > /dev/null 2>&1
142 }
143
144 function unixbench_install()
145 {
146         pushd ${UNIXBENCH_DIR} > /dev/null 2>&1
147   git clone https://github.com/kdlucas/byte-unixbench
148   popd > /dev/null 2>&1
149 }
150
151 function ramspeed_install()
152 {
153   RAMSPEED_BUILD_DIR = "${RAMSPEED_DIR}/ramspeed-smp"
154   pushd ${RAMSPEED_DIR} > /dev/null 2>&1
155   git clone https://github.com/cruvolo/ramspeed-smp
156   pushd ${RAMSPEED_BUILD_DIR} > /dev/null 2>&1
157   chmod 766 build.sh
158   source build.sh
159   popd > /dev/null 2>&1
160   popd > /dev/null 2>&1
161 }
162
163 function collectd_install()
164 {
165   ${SUDO} yum -y install collectd
166 }
167
168 function fio_install()
169 {
170   ${SUDO} yum -y install fio
171 }
172
173 function stressng_install()
174 {
175   STRESSNG_BUILD_DIR = "${STRESSNG_DIR}/stress-ng"
176   pushd ${STRESSNG_DIR} > /dev/null 2>&1
177   git clone https://github.com/ColinIanKing/stress-ng
178   ${SUDO} yum -y install libaio-devel libbsd-devel libcap-devel libattr-devel libgcrypt-devel
179   ${SUDO} yum -y install Judy-devel keyutils-libs-devel lksctp-tools-devel libatomic zlib-devel
180   pushd ${STRESSNG_BUILD_DIR} > /dev/null 2>&1
181   make clean
182   make
183   ${SUDO} make install
184         popd > /dev/null 2>&1
185         popd > /dev/null 2>&1
186 }
187
188 function dpdk_install()
189 {
190         # Build DPDK for the latest kernel installed
191         LATEST_KERNEL_INSTALLED=`ls -v1 /lib/modules/ | tail -1`
192         export RTE_KERNELDIR="/lib/modules/${LATEST_KERNEL_INSTALLED}/build"
193
194         # Get and compile DPDK
195         pushd ${BUILD_DIR} > /dev/null 2>&1
196         wget http://fast.dpdk.org/rel/dpdk-${DPDK_VERSION}.tar.xz
197         tar -xf ./dpdk-${DPDK_VERSION}.tar.xz
198         popd > /dev/null 2>&1
199
200         ${SUDO} ln -s ${RTE_SDK} ${BUILD_DIR}/dpdk
201
202         pushd ${RTE_SDK} > /dev/null 2>&1
203         make config T=${RTE_TARGET}
204         # Starting from DPDK 20.05, the IGB_UIO driver is not compiled by default.
205     # Uncomment the sed command to enable the driver compilation
206     #${SUDO} sed -i 's/CONFIG_RTE_EAL_IGB_UIO=n/c\/CONFIG_RTE_EAL_IGB_UIO=y' ${RTE_SDK}/build/.config
207
208         # For Kubernetes environment we use host vfio module
209         if [ "${K8S_ENV}" == "y" ]; then
210                 sed -i 's/CONFIG_RTE_EAL_IGB_UIO=y/CONFIG_RTE_EAL_IGB_UIO=n/g' ${RTE_SDK}/build/.config
211                 sed -i 's/CONFIG_RTE_LIBRTE_KNI=y/CONFIG_RTE_LIBRTE_KNI=n/g' ${RTE_SDK}/build/.config
212                 sed -i 's/CONFIG_RTE_KNI_KMOD=y/CONFIG_RTE_KNI_KMOD=n/g' ${RTE_SDK}/build/.config
213     fi
214
215         # Compile with MB library
216         sed -i '/CONFIG_RTE_LIBRTE_PMD_AESNI_MB=n/c\CONFIG_RTE_LIBRTE_PMD_AESNI_MB=y' ${RTE_SDK}/build/.config
217         make -j`getconf _NPROCESSORS_ONLN`
218         ln -s ${RTE_SDK}/build ${RTE_SDK}/${RTE_TARGET}
219         popd > /dev/null 2>&1
220 }
221
222 function prox_compile()
223 {
224         # Compile PROX
225         pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX
226         make -j`getconf _NPROCESSORS_ONLN`
227         ${SUDO} cp ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX/build/app/prox ${BUILD_DIR}/prox
228         popd > /dev/null 2>&1
229 }
230
231 function prox_install()
232 {
233         # Clone and compile PROX
234         pushd ${BUILD_DIR} > /dev/null 2>&1
235         git clone https://git.opnfv.org/samplevnf
236         pushd ${BUILD_DIR}/samplevnf/VNFs/DPPD-PROX > /dev/null 2>&1
237         bash -c "${PROX_CHECKOUT}"
238         popd > /dev/null 2>&1
239         prox_compile
240         popd > /dev/null 2>&1
241 }
242
243 function port_info_build()
244 {
245         [ ! -d ${BUILD_DIR}/port_info ] && echo "Skipping port_info compilation..." && return
246
247         pushd ${BUILD_DIR}/port_info > /dev/null 2>&1
248         make
249         ${SUDO} cp ${BUILD_DIR}/port_info/build/app/port_info_app ${BUILD_DIR}/port_info_app
250         popd > /dev/null 2>&1
251 }
252
253 function create_minimal_install()
254 {
255         ldd ${BUILD_DIR}/prox | awk '{ if ($(NF-1) != "=>") print $(NF-1) }' >> ${BUILD_DIR}/list_of_install_components
256
257         echo "${BUILD_DIR}/prox" >> ${BUILD_DIR}/list_of_install_components
258         echo "${BUILD_DIR}/port_info_app" >> ${BUILD_DIR}/list_of_install_components
259
260         tar -czvhf ${BUILD_DIR}/install_components.tgz -T ${BUILD_DIR}/list_of_install_components
261 }
262
263 function cleanup()
264 {
265         ${SUDO} yum autoremove -y
266         ${SUDO} yum clean all
267         ${SUDO} rm -rf /var/cache/yum
268 }
269
270 function k8s_runtime_image()
271 {
272         k8s_os_pkgs_runtime_install
273         k8s_os_cfg
274         cleanup
275
276         pushd / > /dev/null 2>&1
277         tar -xvf ${BUILD_DIR}/install_components.tgz --skip-old-files
278         popd > /dev/null 2>&1
279
280         ldconfig
281
282         #rm -rf ${BUILD_DIR}/install_components.tgz
283 }
284
285 function print_usage()
286 {
287         echo "Usage: ${0} [OPTIONS] [COMMAND]"
288         echo "Options:"
289         echo "   -u, --update     Full OS update"
290         echo "   -k, --kubernetes Build for Kubernetes environment"
291         echo "Commands:"
292         echo "   deploy           Run through all deployment steps"
293         echo "   compile          PROX compile only"
294         echo "   runtime_image    Apply runtime configuration only"
295 }
296
297 COMMAND=""
298 # Parse options and comman
299 for opt in "$@"; do
300         case ${opt} in
301                 -u|--update)
302                 echo 'Full OS update will be done!'
303                 OS_UPDATE="y"
304                 ;;
305                 -k|--kubernetes)
306                 echo "Kubernetes environment is set!"
307                 K8S_ENV="y"
308                 ;;
309                 compile)
310                 COMMAND="compile"
311                 ;;
312                 runtime_image)
313                 COMMAND="runtime_image"
314                 ;;
315                 deploy)
316                 COMMAND="deploy"
317                 ;;
318                 *)
319                 echo "Unknown option/command ${opt}"
320                 print_usage
321                 exit 1
322                 ;;
323         esac
324 done
325
326 if [ "${COMMAND}" == "compile" ]; then
327         echo "PROX compile only..."
328         prox_compile
329 elif [ "${COMMAND}" == "runtime_image" ]; then
330         echo "Runtime image intallation and configuration..."
331         k8s_runtime_image
332 elif [ "${COMMAND}" == "deploy" ]; then
333         [ ! -d ${BUILD_DIR} ] && ${SUDO} mkdir -p ${BUILD_DIR}
334         ${SUDO} chmod 0777 ${BUILD_DIR}
335
336         os_pkgs_install
337
338         if [ "${K8S_ENV}" == "y" ]; then
339                 k8s_os_cfg
340         else
341                 os_cfg
342         fi
343
344         mblib_install
345         dpdk_install
346         prox_install
347   trex_install
348   collectd_install
349   stressng_install
350   fio_install
351   unixbench_install
352   ramspeed_install
353
354
355
356         if [ "${K8S_ENV}" == "y" ]; then
357                 port_info_build
358                 create_minimal_install
359         fi
360
361         cleanup
362 else
363         print_usage
364 fi