1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2014-2018 Oracle and/or its affiliates. All Rights Reserved. 4# Copyright (c) 2018 Petr Vorel <pvorel@suse.cz> 5# Author: Alexey Kodanev alexey.kodanev@oracle.com 6 7TST_SETUP="dhcp_lib_setup" 8TST_CLEANUP="dhcp_lib_cleanup" 9TST_TESTFUNC="test01" 10TST_NEEDS_TMPDIR=1 11TST_NEEDS_ROOT=1 12TST_NEEDS_CMDS="cat $dhcp_name awk ip pgrep pkill dhclient" 13 14. tst_net.sh 15. daemonlib.sh 16 17iface0="ltp_veth0" 18iface1="ltp_veth1" 19 20stop_dhcp() 21{ 22 [ "$(pgrep -x $dhcp_name)" ] || return 0 23 24 tst_res TINFO "stopping $dhcp_name" 25 local count=0 26 while [ $count -le 10 ]; do 27 pkill -x $dhcp_name 28 [ "$(pgrep -x $dhcp_name)" ] || return 0 29 tst_sleep 100ms 30 count=$((count + 1)) 31 done 32 33 pkill -9 -x $dhcp_name 34 tst_sleep 100ms 35 [ "$(pgrep -x $dhcp_name)" ] && return 1 || return 0 36} 37 38dhcp_lib_setup() 39{ 40 if [ $HAVE_SYSTEMCTL -eq 1 ] && \ 41 systemctl --no-pager -p Id show network.service | grep -q Id=wicked.service; then 42 [ $TST_IPV6 ] && tst_brk TCONF "wicked not supported on IPv6" 43 is_wicked=1 44 fi 45 46 [ -z "$log" ] && log="$PWD/$(basename $0 '.sh').log" 47 48 if [ $TST_IPV6 ]; then 49 ip_addr="fd00:1:1:2::12/64" 50 ip_addr_check_noprefix="fd00:1:1:2::100" 51 ip_addr_check="$ip_addr_check_noprefix/128" 52 else 53 ip_addr="10.1.1.12/24" 54 ip_addr_check_noprefix="10.1.1.100" 55 ip_addr_check="$ip_addr_check_noprefix/24" 56 fi 57 58 lsmod | grep -q '^veth ' && veth_loaded=yes || veth_loaded=no 59 60 tst_res TINFO "create veth interfaces" 61 ip link add $iface0 type veth peer name $iface1 || \ 62 tst_brk TBROK "failed to add veth $iface0" 63 64 veth_added=1 65 ip link set up $iface0 || tst_brk TBROK "failed to bring $iface0 up" 66 ip link set up $iface1 || tst_brk TBROK "failed to bring $iface1 up" 67 68 stop_dhcp || tst_brk TBROK "Failed to stop dhcp server" 69 70 dhclient_lease="/var/lib/dhclient/dhclient${TST_IPV6}.leases" 71 [ -f $dhclient_lease ] || dhclient_lease="/var/lib/dhcp/dhclient${TST_IPV6}.leases" 72 if [ -f $dhclient_lease ]; then 73 tst_res TINFO "backup dhclient${TST_IPV6}.leases" 74 mv $dhclient_lease . 75 fi 76 77 tst_res TINFO "add $ip_addr to $iface0" 78 ip addr add $ip_addr dev $iface0 || \ 79 tst_brk TBROK "failed to add ip address" 80 81 if [ ! -d "$lease_dir" ]; then 82 mkdir -p $lease_dir 83 lease_dir_added=1 84 fi 85} 86 87dhcp_lib_cleanup() 88{ 89 [ -z "$veth_loaded" ] && return 90 91 [ "$lease_dir_added" = 1 ] && rm -rf $lease_dir 92 rm -f $lease_file 93 94 stop_dhcp 95 96 pkill -f "dhclient -$TST_IPVER $iface1" 97 98 cleanup_dhcp 99 100 # restore dhclient leases 101 [ $dhclient_lease ] && rm -f $dhclient_lease 102 [ -f "dhclient${TST_IPV6}.leases" ] && \ 103 mv dhclient${TST_IPV6}.leases $dhclient_lease 104 105 [ $veth_added ] && ip link del $iface0 106 107 [ "$veth_loaded" = "no" ] && lsmod | grep -q '^veth ' && rmmod veth 108} 109 110print_dhcp_log() 111{ 112 [ -f "$log" ] && cat $log 113} 114 115test01() 116{ 117 local wicked_cfg="/etc/sysconfig/network/ifcfg-$iface1" 118 local wicked_cleanup 119 120 tst_res TINFO "testing DHCP server $dhcp_name: $(print_dhcp_version)" 121 tst_res TINFO "using DHCP client: $(dhclient --version 2>&1)" 122 123 tst_res TINFO "starting DHCPv$TST_IPVER server on $iface0" 124 125 start_dhcp$TST_IPV6 126 if [ $? -ne 0 ]; then 127 print_dhcp_log 128 tst_brk TBROK "Failed to start $dhcp_name" 129 fi 130 131 sleep 1 132 133 if [ "$(pgrep '$dhcp_name')" ]; then 134 print_dhcp_log 135 tst_brk TBROK "Failed to start $dhcp_name" 136 fi 137 138 if [ "$is_wicked" ]; then 139 tst_res TINFO "wicked is running, don't start dhclient" 140 if [ ! -f "$wicked_cfg" ]; then 141 cat <<EOF > $wicked_cfg 142BOOTPROTO='dhcp' 143NAME='LTP card' 144STARTMODE='auto' 145USERCONTROL='no' 146EOF 147 wicked_cleanup=1 148 else 149 tst_res TINFO "wicked config file $wicked_cfg already exist" 150 fi 151 152 tst_res TINFO "restarting wicked" 153 systemctl restart wicked 154 else 155 tst_res TINFO "starting dhclient -$TST_IPVER $iface1" 156 dhclient -$TST_IPVER $iface1 || tst_brk TBROK "dhclient failed" 157 fi 158 159 # check that we get configured ip address 160 ip addr show $iface1 | grep -q $ip_addr_check 161 if [ $? -eq 0 ]; then 162 tst_res TPASS "'$ip_addr_check' configured by DHCPv$TST_IPVER" 163 else 164 if ip addr show $iface1 | grep -q $ip_addr_check_noprefix; then 165 tst_res TFAIL "'$ip_addr_check_noprefix' configured but has wrong prefix, expect '$ip_addr_check'" 166 ip addr show $iface1 167 else 168 tst_res TFAIL "'$ip_addr_check' not configured by DHCPv$TST_IPVER" 169 print_dhcp_log 170 fi 171 fi 172 173 [ "$wicked_cleanup" ] && rm -f $wicked_cfg 174 175 stop_dhcp 176} 177