| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 1 | #!/bin/bash | 
|  | 2 | # | 
|  | 3 | #    Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 4 | #    not use this file except in compliance with the License. You may obtain | 
|  | 5 | #    a copy of the License at | 
|  | 6 | # | 
|  | 7 | #         http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | # | 
|  | 9 | #    Unless required by applicable law or agreed to in writing, software | 
|  | 10 | #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 11 | #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | 
|  | 12 | #    License for the specific language governing permissions and limitations | 
|  | 13 | #    under the License. | 
|  | 14 |  | 
|  | 15 | # Defaults | 
|  | 16 | # -------- | 
| Slawek Kaplonski | 4185358 | 2021-07-06 12:05:31 +0200 | [diff] [blame] | 17 | Q_BUILD_OVS_FROM_GIT=$(trueorfalse False Q_BUILD_OVS_FROM_GIT) | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 18 |  | 
|  | 19 | # Set variables for building OVS from source | 
|  | 20 | OVS_REPO=${OVS_REPO:-https://github.com/openvswitch/ovs.git} | 
|  | 21 | OVS_REPO_NAME=$(basename ${OVS_REPO} | cut -f1 -d'.') | 
|  | 22 | OVS_REPO_NAME=${OVS_REPO_NAME:-ovs} | 
|  | 23 | OVS_BRANCH=${OVS_BRANCH:-0047ca3a0290f1ef954f2c76b31477cf4b9755f5} | 
|  | 24 |  | 
|  | 25 | # Functions | 
|  | 26 |  | 
|  | 27 | # load_module() - Load module using modprobe module given by argument and dies | 
|  | 28 | #                 on failure | 
|  | 29 | #               - fatal argument is optional and says whether function should | 
|  | 30 | #                 exit if module can't be loaded | 
|  | 31 | function load_module { | 
|  | 32 | local module=$1 | 
|  | 33 | local fatal=$2 | 
|  | 34 |  | 
|  | 35 | if [ "$(trueorfalse True fatal)" == "True" ]; then | 
| Slawek Kaplonski | 5e7afb7 | 2022-10-24 12:17:48 +0200 | [diff] [blame] | 36 | sudo modprobe $module || (sudo dmesg && die $LINENO "FAILED TO LOAD $module") | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 37 | else | 
| Slawek Kaplonski | 5e7afb7 | 2022-10-24 12:17:48 +0200 | [diff] [blame] | 38 | sudo modprobe $module || (echo "FAILED TO LOAD $module" && sudo dmesg) | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 39 | fi | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | # prepare_for_compilation() - Fetch ovs git repository and install packages needed for | 
|  | 43 | #                             compilation. | 
|  | 44 | function prepare_for_ovs_compilation { | 
|  | 45 | local build_modules=${1:-False} | 
|  | 46 | OVS_DIR=$DEST/$OVS_REPO_NAME | 
|  | 47 |  | 
|  | 48 | if [ ! -d $OVS_DIR ] ; then | 
|  | 49 | # We can't use git_clone here because we want to ignore ERROR_ON_CLONE | 
|  | 50 | git_timed clone $OVS_REPO $OVS_DIR | 
|  | 51 | cd $OVS_DIR | 
|  | 52 | git checkout $OVS_BRANCH | 
|  | 53 | else | 
|  | 54 | # Even though the directory already exists, call git_clone to update it | 
|  | 55 | # if needed based on the RECLONE option | 
|  | 56 | git_clone $OVS_REPO $OVS_DIR $OVS_BRANCH | 
|  | 57 | cd $OVS_DIR | 
|  | 58 | fi | 
|  | 59 |  | 
|  | 60 | # TODO: Can you create package list files like you can inside devstack? | 
|  | 61 | install_package autoconf automake libtool gcc patch make | 
|  | 62 |  | 
|  | 63 | # If build_modules is False, we don't need to install the kernel-* | 
|  | 64 | # packages. Just return. | 
|  | 65 | if [[ "$build_modules" == "False" ]]; then | 
|  | 66 | return | 
|  | 67 | fi | 
|  | 68 |  | 
|  | 69 | KERNEL_VERSION=`uname -r` | 
|  | 70 | if is_fedora ; then | 
|  | 71 | # is_fedora covers Fedora, RHEL, CentOS, etc... | 
|  | 72 | if [[ "$os_VENDOR" == "Fedora" ]]; then | 
|  | 73 | install_package elfutils-libelf-devel | 
|  | 74 | KERNEL_VERSION=`echo $KERNEL_VERSION | cut --delimiter='-' --field 1` | 
|  | 75 | elif [[ ${KERNEL_VERSION:0:2} != "3." ]]; then | 
|  | 76 | # dash is illegal character in rpm version so replace | 
|  | 77 | # them with underscore like it is done in the kernel | 
|  | 78 | # https://github.com/torvalds/linux/blob/master/scripts/package/mkspec#L25 | 
|  | 79 | # but only for latest series of the kernel, not 3.x | 
|  | 80 |  | 
|  | 81 | KERNEL_VERSION=`echo $KERNEL_VERSION | tr - _` | 
|  | 82 | fi | 
|  | 83 |  | 
|  | 84 | echo NOTE: if kernel-devel-$KERNEL_VERSION or kernel-headers-$KERNEL_VERSION installation | 
|  | 85 | echo failed, please, provide a repository with the package, or yum update / reboot | 
|  | 86 | echo your machine to get the latest kernel. | 
|  | 87 |  | 
|  | 88 | install_package kernel-devel-$KERNEL_VERSION | 
|  | 89 | install_package kernel-headers-$KERNEL_VERSION | 
| Slawek Kaplonski | 3de92db | 2022-08-26 12:58:29 +0200 | [diff] [blame] | 90 | if is_service_enabled tls-proxy; then | 
|  | 91 | install_package openssl-devel | 
|  | 92 | fi | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 93 |  | 
|  | 94 | elif is_ubuntu ; then | 
|  | 95 | install_package linux-headers-$KERNEL_VERSION | 
| Slawek Kaplonski | 3de92db | 2022-08-26 12:58:29 +0200 | [diff] [blame] | 96 | if is_service_enabled tls-proxy; then | 
|  | 97 | install_package libssl-dev | 
|  | 98 | fi | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 99 | fi | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | # load_ovs_kernel_modules() - load openvswitch kernel module | 
|  | 103 | function load_ovs_kernel_modules { | 
|  | 104 | load_module openvswitch | 
|  | 105 | load_module vport-geneve False | 
| Slawek Kaplonski | 5e7afb7 | 2022-10-24 12:17:48 +0200 | [diff] [blame] | 106 | sudo dmesg | tail | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
|  | 109 | # reload_ovs_kernel_modules() - reload openvswitch kernel module | 
|  | 110 | function reload_ovs_kernel_modules { | 
|  | 111 | set +e | 
|  | 112 | ovs_system=$(sudo ovs-dpctl dump-dps | grep ovs-system) | 
|  | 113 | if [ -n "$ovs_system" ]; then | 
|  | 114 | sudo ovs-dpctl del-dp ovs-system | 
|  | 115 | fi | 
|  | 116 | set -e | 
|  | 117 | sudo modprobe -r vport_geneve | 
|  | 118 | sudo modprobe -r openvswitch | 
|  | 119 | load_ovs_kernel_modules | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | # compile_ovs() - Compile OVS from source and load needed modules. | 
|  | 123 | #                 Accepts two parameters: | 
|  | 124 | #                   - first one is False by default and means that modules are not built and installed. | 
|  | 125 | #                   - second optional parameter defines prefix for ovs compilation | 
|  | 126 | #                   - third optional parameter defines localstatedir for ovs single machine runtime | 
|  | 127 | #                 Env variables OVS_REPO_NAME, OVS_REPO and OVS_BRANCH must be set | 
|  | 128 | function compile_ovs { | 
|  | 129 | local _pwd=$PWD | 
|  | 130 | local build_modules=${1:-False} | 
|  | 131 | local prefix=$2 | 
|  | 132 | local localstatedir=$3 | 
|  | 133 |  | 
|  | 134 | if [ -n "$prefix" ]; then | 
|  | 135 | prefix="--prefix=$prefix" | 
|  | 136 | fi | 
|  | 137 |  | 
|  | 138 | if [ -n "$localstatedir" ]; then | 
|  | 139 | localstatedir="--localstatedir=$localstatedir" | 
|  | 140 | fi | 
|  | 141 |  | 
|  | 142 | prepare_for_ovs_compilation $build_modules | 
|  | 143 |  | 
|  | 144 | KERNEL_VERSION=$(uname -r) | 
|  | 145 | major_version=$(echo "${KERNEL_VERSION}" | cut -d '.' -f1) | 
|  | 146 | patch_level=$(echo "${KERNEL_VERSION}" | cut -d '.' -f2) | 
|  | 147 | if [ "${major_version}" -gt 5 ] || [ "${major_version}" == 5 ] && [ "${patch_level}" -gt 5 ]; then | 
|  | 148 | echo "NOTE: KERNEL VERSION is ${KERNEL_VERSION} and OVS doesn't support compiling " | 
|  | 149 | echo "Kernel module for version higher than 5.5. Skipping module compilation..." | 
|  | 150 | build_modules="False" | 
|  | 151 | fi | 
|  | 152 |  | 
|  | 153 | if [ ! -f configure ] ; then | 
|  | 154 | ./boot.sh | 
|  | 155 | fi | 
|  | 156 | if [ ! -f config.status ] || [ configure -nt config.status ] ; then | 
|  | 157 | if [[ "$build_modules" == "True" ]]; then | 
|  | 158 | ./configure $prefix $localstatedir --with-linux=/lib/modules/$(uname -r)/build | 
|  | 159 | else | 
|  | 160 | ./configure $prefix $localstatedir | 
|  | 161 | fi | 
|  | 162 | fi | 
|  | 163 | make -j$(($(nproc) + 1)) | 
|  | 164 | sudo make install | 
|  | 165 | if [[ "$build_modules" == "True" ]]; then | 
|  | 166 | sudo make INSTALL_MOD_DIR=kernel/net/openvswitch modules_install | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 167 | fi | 
| yatinkarel | 4251796 | 2023-04-14 19:06:03 +0530 | [diff] [blame] | 168 | reload_ovs_kernel_modules | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 169 |  | 
|  | 170 | cd $_pwd | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | # action_service - call an action over openvswitch service | 
|  | 174 | #                  Accepts one parameter that can be either | 
|  | 175 | #                  'start', 'restart' and 'stop'. | 
|  | 176 | function action_openvswitch { | 
|  | 177 | local action=$1 | 
|  | 178 |  | 
|  | 179 | if is_ubuntu; then | 
|  | 180 | ${action}_service openvswitch-switch | 
|  | 181 | elif is_fedora; then | 
|  | 182 | ${action}_service openvswitch | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 183 | fi | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | # start_new_ovs() - removes old ovs database, creates a new one and starts ovs | 
|  | 187 | function start_new_ovs { | 
|  | 188 | sudo rm -f /etc/openvswitch/conf.db /etc/openvswitch/.conf.db~lock~ | 
| Rodolfo Alonso Hernandez | 8c67103 | 2022-02-09 18:01:46 +0000 | [diff] [blame] | 189 | sudo /usr/local/share/openvswitch/scripts/ovs-ctl start | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
|  | 192 | # stop_new_ovs() - stops ovs | 
|  | 193 | function stop_new_ovs { | 
| Rodolfo Alonso Hernandez | 8c67103 | 2022-02-09 18:01:46 +0000 | [diff] [blame] | 194 | local ovs_ctl='/usr/local/share/openvswitch/scripts/ovs-ctl' | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 195 |  | 
|  | 196 | if [ -x $ovs_ctl ] ; then | 
|  | 197 | sudo $ovs_ctl stop | 
|  | 198 | fi | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | # remove_ovs_packages() - removes old ovs packages from the system | 
|  | 202 | function remove_ovs_packages { | 
|  | 203 | for package in openvswitch openvswitch-switch openvswitch-common; do | 
|  | 204 | if is_package_installed $package; then | 
|  | 205 | uninstall_package $package | 
|  | 206 | fi | 
|  | 207 | done | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 |  | 
|  | 211 | # load_conntrack_gre_module() - loads nf_conntrack_proto_gre kernel module | 
|  | 212 | function load_conntrack_gre_module { | 
| Slawek Kaplonski | b4e683e | 2021-10-05 20:44:57 +0200 | [diff] [blame] | 213 | load_module nf_conntrack_proto_gre False | 
| Ian Wienand | 7783563 | 2021-05-13 13:14:42 +1000 | [diff] [blame] | 214 | } |