#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025 Maxime Leroy, Free Mobile

. $(dirname $0)/_init_frr.sh

create_vrf gr-vrf1
create_vrf gr-vrf2

create_interface p0 vrf gr-vrf1
create_interface p1 vrf gr-vrf2

for n in 0 1; do
	p=x-p$n
	ns=n$n
	netns_add $ns
	move_to_netns $p $ns
	ip -n $ns addr add 172.16.$n.2/24 dev $p
	ip -n $ns addr add 16.$n.0.1/16 dev lo
	ip -n $ns route add default via 172.16.$n.1
done

set_ip_address p0 172.16.0.1/24
set_ip_address p1 172.16.1.1/24

# from 16.0.0.1 to 16.1.0.1, only one route lookup is done
set_ip_route 16.1.0.0/16 172.16.1.2 gr-vrf1 gr-vrf2
set_ip_route 16.1.0.0/16 172.16.1.2 gr-vrf2 gr-vrf2 # required for ARP resolution

# from 16.1.0.1 to 16.0.0.1, two route lookup are done
set_ip_route 16.0.0.0/16 gr-vrf1 gr-vrf2 gr-vrf1
set_ip_route 16.0.0.0/16 172.16.0.2 gr-vrf1

ip netns exec n0 ping -i0.01 -c3 -I 16.0.0.1 -n 16.1.0.1
ip netns exec n1 ping -i0.01 -c3 -I 16.1.0.1 -n 16.0.0.1
