#!/bin/sh

set -e

. ./helpers

ME=$(basename $0)

setup_sandbox

# Every switch VM (internet, spines and leaves) gets rp_filter=0,
# mirroring neutron's BGP CI (tempest-multinode-with-bgp): the
# kernel's reverse path validation (rp_filter >= 1, loose mode
# included) drops the asymmetric return traffic of BGP-2-the-host
# floating IPs whenever the chassis advertising the /32 host route
# differs from the one egressing the reply (the floating IPs of a
# distributed router egress from the VM's compute node, while the
# connected-as-host announce tracks the gateway port's chassis).
run_setup start
assert_eq "ikvswitch-setup start rc" "${SETUP_RC}" "0"
for vm in internet spine1 spine2 leaf1 leaf2 leaf3 leaf4 leaf5 leaf6 ; do
	F=$(ls ${SANDBOX}/out/${vm}/etc/sysctl.d/*rp-filter* 2>/dev/null | head -1)
	if [ -z "${F}" ] ; then
		echo "${ME}: ${vm} has no rp-filter sysctl file"
		exit 1
	fi
	assert_file_contains "${vm} all rp_filter" "${F}" "net.ipv4.conf.all.rp_filter=0"
	assert_file_contains "${vm} default rp_filter" "${F}" "net.ipv4.conf.default.rp_filter=0"

	# The file must sort after Debian's 10-network-security.conf
	# (which sets net.ipv4.conf.default.rp_filter=2): sysctl.d
	# precedence is lexicographic and the latest file wins a key, so
	# the "default" key must end at 0 and propagate to every NIC
	# created afterwards; all=0 caps the rest (the kernel uses the
	# max of the all and the per-interface value).
	if [ "10-network-security.conf" \> "$(basename ${F})" ] ; then
		echo "${ME}: ${vm}: $(basename ${F}) sorts before 10-network-security.conf"
		exit 1
	fi
done

exit 0
