create_netns()                                                                                        
{                                                                                                     
        [ -z "$1" ] && { echo "$0 ns_name, no name is specified"; return 1; }                         
        local ns_name=$1                                                                              
        ip netns del $ns_name                                                                         
        ip netns add $ns_name                                                                         
        ip netns exec $ns_name ip link set lo up                                                      
        return 0                                                                                      
}                                                                                                     
                                                                                                      
# netns_attach_if $ns_name $if_name $mac $ip4 $ip6 $ip4_route $ip6_route                              
# attach specified if into specified netns, and set specified address for the if(mac, ip, etc.)       
netns_attach_if()                                                                                     
{
        [ $# -lt 2 ] && { echo "usage:$0 ns_name if_name [mac] [ip4] [ip6] [ip4_route] [ip6_route]"; return 1; }
        local ns_name=$1
        local if_name=$2
        ip netns exec $ns_name ip link show &> /dev/null || { echo "$ns_name doesn't exist"; return 2; }
        ip link set $if_name netns $ns_name || { echo "fail to attach"; return 3; }
        [ -n "$3" ] && ip netns exec $ns_name ip link set $if_name address $3
        [ -n "$4" ] && ip netns exec $ns_name ip addr add $4 dev $if_name
        [ -n "$5" ] && ip netns exec $ns_name ip addr add $5 dev $if_name
        ip netns exec $ns_name ip link set $if_name up
        [ -n "$6" ] && ip netns exec $ns_name ip route add default via $6
        [ -n "$7" ] && ip netns exec $ns_name ip -6 route add default via $7
        return 0

} 

systemctl start openvswitch                                                                                     
systemctl start ovn-northd                                                                                      
ovn-nbctl set-connection ptcp:6641                                                                              
ovn-sbctl set-connection ptcp:6642                                                                              
ovs-vsctl set open . external_ids:system-id=hv1 external_ids:ovn-remote=tcp:20.0.86.25:6642 external_ids:ovn-encap-type=geneve external_ids:ovn-encap-ip=20.0.86.25
systemctl restart ovn-controller
hv=1

ip1=172.16.2.1
ip2=172.16.2.2
mac1="00:00:00:00:02:01"
mac2="00:00:00:00:02:02"
ip1_v6="172:16:2::1"
ip2_v6="172:16:2::2"

			# add logical switch
ovn-nbctl ls-add ls2 -- add Logical_Switch ls2 other_config subnet=172.16.2.0/24

			# create logical switch port
			lsp_name=ls2p1
ovn-nbctl lsp-add ls2 $lsp_name
ovn-nbctl lsp-set-addresses $lsp_name "$mac1 $ip1 $ip1_v6"

lsp_name=ls2p2
ovn-nbctl lsp-add ls2 $lsp_name
ovn-nbctl lsp-set-addresses $lsp_name "$mac2 $ip2 $ip2_v6"

			# create logical router lr2
ovn-nbctl lr-add lr2
ovn-nbctl lrp-add lr2 lr2-ls2 00:00:00:00:02:00 172.16.2.254/24 172:16:2::a/64

			# connect lr2 and ls2
ovn-nbctl lsp-add ls2 ls2-lr2
ovn-nbctl lsp-set-type ls2-lr2 router
ovn-nbctl lsp-set-addresses ls2-lr2 00:00:00:00:02:00
ovn-nbctl lsp-set-options ls2-lr2 router-port=lr2-ls2

			# get address
ovs-vsctl add-port br-int ls2p1 -- set interface ls2p1 type=internal external_ids:iface-id=ls2p1
create_netns ls2p1
netns_attach_if ls2p1 ls2p1 $mac1 $ip1/24 $ip1_v6/64 172.16.2.254 172:16:2::a

ovs-vsctl add-port br-int ls2p2 -- set interface ls2p2 type=internal external_ids:iface-id=ls2p2
create_netns ls2p2
netns_attach_if ls2p2 ls2p2 $mac2 $ip2/24 $ip2_v6/64 172.16.2.254 172:16:2::a
			# ping in ovn deployment
ip netns exec ls2p1 ping $ip2 -c 2
ip netns exec ls2p1 ping6 $ip2_v6 -c 2

			#alias ovn-ctl="/usr/share/ovn/scripts/ovn-ctl"
/usr/share/ovn/scripts/ovn-ctl --db-ic-nb-create-insecure-remote=yes \
	--db-ic-sb-create-insecure-remote=yes start_ic_ovsdb

			# add transit switch
ovn-ic-nbctl ts-add ts1

			# set az name
ovn-nbctl set NB_Global . name=az2

			# connecto to global ovsdb
/usr/share/ovn/scripts/ovn-ctl --ovn-ic-nb-db=tcp:20.0.86.25:6645 --ovn-ic-sb-db=tcp:20.0.86.25:6646 --ovn-northd-nb-db=tcp:20.0.86.25:6641 --ovn-northd-sb-db=tcp:20.0.86.25:6642 start_ic

			# enable me as gateway chassis
ovs-vsctl set open_vswitch . external_ids:ovn-is-interconn=true

			# connect gateway router to transit switch
ovn-nbctl lrp-add lr2 lr2-ts1 aa:aa:aa:aa:aa:02 169.254.100.2/24
ovn-nbctl lsp-add ts1 ts1-lr2 -- \
				lsp-set-addresses ts1-lr2 router -- \
				lsp-set-type ts1-lr2 router -- \
				lsp-set-options ts1-lr2 router-port=lr2-ts1

			# setup distributed gateway port
ovn-nbctl lrp-set-gateway-chassis lr2-ts1 hv$hv


			#set multicast flood
ovn-nbctl --wait=hv set nb_global . options:ic-route-learn=true
ovn-nbctl --wait=hv set nb_global . options:ic-route-adv=true
ovn-nbctl set logical_switch ls2 other_config:mcast_snoop='true'
ovn-nbctl set logical_router lr2 options:mcast_relay='true'
ovn-nbctl set Logical_Switch_Port ls2-lr2 options:mcast_flood_reports="true"
ovn-nbctl set logical_router_port lr2-ts1 options:mcast_flood='true'
ovn-nbctl set logical_switch ts1 other_config:mcast_snoop="true"
ovn-nbctl set logical_switch_port ts1-lr1 options:mcast_flood_reports="true"
ovn-nbctl set logical_switch_port ts1-lr2 options:mcast_flood_reports="true"

ip netns exec ls2p1 join_group -f 4 -g 239.0.1.68 -i ls2p1 &

