AT_BANNER([tunnel_push_pop])

AT_SETUP([tunnel_push_pop - erspan])
OVS_VSWITCHD_START([add-port br0 p0 -- set Interface p0 type=dummy ofport_request=1 other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy], [0])
AT_CHECK([ovs-vsctl add-port int-br t1 -- set Interface t1 type=erspan \
                       options:remote_ip=1.1.2.92 options:key=123 options:erspan_ver=1 \
                       options:erspan_idx=3 ofport_request=2 \
                    -- add-port int-br t2 -- set Interface t2 type=erspan \
                       options:remote_ip=1.1.2.92 options:key=567 options:erspan_ver=2 \
                       options:erspan_dir=1 options:erspan_hwid=0x7 ofport_request=3\
                    -- add-port int-br t3 -- set Interface t3 type=erspan \
                       options:remote_ip=flow options:erspan_ver=2 options:key=456 \
                       options:erspan_hwid=flow options:erspan_dir=flow ofport_request=4\
                    -- add-port int-br t4 -- set Interface t4 type=erspan \
                       options:remote_ip=flow options:erspan_ver=2 options:key=56 \
                       options:erspan_ver=flow ofport_request=5\
                       ], [0])

AT_CHECK([ovs-appctl dpif/show], [0], [dnl
dummy@ovs-dummy: hit:0 missed:0
  br0:
    br0 65534/100: (dummy-internal)
    p0 1/1: (dummy)
  int-br:
    int-br 65534/2: (dummy-internal)
    t1 2/3: (erspan: erspan_idx=0x3, erspan_ver=1, key=123, remote_ip=1.1.2.92)
    t2 3/3: (erspan: erspan_dir=1, erspan_hwid=0x7, erspan_ver=2, key=567, remote_ip=1.1.2.92)
    t3 4/3: (erspan: erspan_dir=flow, erspan_hwid=flow, erspan_ver=2, key=456, remote_ip=flow)
    t4 5/3: (erspan: erspan_dir=flow, erspan_hwid=flow, erspan_idx=flow, erspan_ver=flow, key=56, remote_ip=flow)
])

dnl Setup dummy interface IP addresses.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
AT_CHECK([ovs-appctl netdev-dummy/ip6addr br0 2001:cafe::88/24], [0], [OK
])
dnl Checking that a local routes for added IPs were successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached | sort], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
Cached: 2001:ca00::/24 dev br0 SRC 2001:cafe::88 local
])

AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl Check ARP request
AT_CHECK([ovs-vsctl -- set Interface p0 options:pcap=p0.pcap])

AT_CHECK([ovs-appctl netdev-dummy/receive int-br 'in_port(2),eth(src=aa:55:aa:55:00:00,dst=f8:bc:12:ff:ff:ff),eth_type(0x0800),ipv4(src=1.1.3.92,dst=1.1.3.88,proto=1,tos=0,ttl=64,frag=no),icmp(type=0,code=0)'])

dnl Wait for the two ARP requests to be sent. Sometimes the system
dnl can be slow (e.g. under valgrind)
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | sort | uniq | wc -l` -ge 1])

AT_CHECK([ovs-pcap p0.pcap > p0.pcap.txt 2>&1])

AT_CHECK([cat p0.pcap.txt | grep 101025 | uniq], [0], [dnl
ffffffffffffaa55aa55000008060001080006040001aa55aa550000010102580000000000000101025c
])


dnl Check ARP Snoop
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(100),eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(100),eth(src=f8:bc:12:44:34:b7,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.93,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b7,tha=00:00:00:00:00:00)'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(100),eth(src=f8:bc:12:44:34:b8,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.94,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b8,tha=00:00:00:00:00:00)'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | tail -n+3 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b6   br0
1.1.2.93                                      f8:bc:12:44:34:b7   br0
1.1.2.94                                      f8:bc:12:44:34:b8   br0
])

AT_CHECK([ovs-appctl tnl/ports/show |sort], [0], [dnl
Listening ports:
erspan_sys (3) ref_cnt=4
])

dnl Check ERSPAN v1 tunnel push
AT_CHECK([ovs-vsctl -- set Interface br0 options:pcap=br0.pcap])
AT_CHECK([ovs-ofctl add-flow int-br action=2])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=50,type=107,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=1,sid=0x7b,idx=0x3)),out_port(100)),1
])

dnl Check ERSPAN v2 tunnel push
AT_CHECK([ovs-ofctl mod-flows int-br action=3])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=54,type=107,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=2,sid=0x237,dir=1,hwid=0x7)),out_port(100)),1
])

dnl Check ERSPAN v2 flow-based tunnel push
AT_CHECK([ovs-ofctl mod-flows int-br "action=set_field:1.1.2.94->tun_dst,set_field:1->tun_erspan_dir,set_field:0x1->tun_erspan_hwid,4"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=54,type=107,eth(dst=f8:bc:12:44:34:b8,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.94,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=2,sid=0x1c8,dir=1,hwid=0x1)),out_port(100)),1
])

dnl Check ERSPAN v2 flow-based tunnel push, erspan_ver=flow
dnl Dynamically set erspan v2
AT_CHECK([ovs-ofctl mod-flows int-br "action=set_field:1.1.2.94->tun_dst,set_field:2->tun_erspan_ver,set_field:1->tun_erspan_dir,set_field:0x1->tun_erspan_hwid,5"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=54,type=107,eth(dst=f8:bc:12:44:34:b8,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.94,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=2,sid=0x38,dir=1,hwid=0x1)),out_port(100)),1
])

dnl Dynamically set erspan v1
AT_CHECK([ovs-ofctl mod-flows int-br "action=set_field:1.1.2.94->tun_dst,set_field:1->tun_erspan_ver,set_field:1->tun_erspan_idx,5"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=50,type=107,eth(dst=f8:bc:12:44:34:b8,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.94,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=1,sid=0x38,idx=0x1)),out_port(100)),1
])

dnl Check ERSPAN v2 flow-based tunnel push
AT_CHECK([ovs-ofctl mod-flows int-br "action=set_field:1.1.2.94->tun_dst,set_field:1->tun_erspan_dir,set_field:0x1->tun_erspan_hwid,4"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=54,type=107,eth(dst=f8:bc:12:44:34:b8,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.94,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=2,sid=0x1c8,dir=1,hwid=0x1)),out_port(100)),1
])

dnl Check ERSPAN v2 flow-based tunnel push, erspan_ver=flow
dnl Dynamically set erspan v2
AT_CHECK([ovs-ofctl mod-flows int-br "action=set_field:1.1.2.94->tun_dst,set_field:2->tun_erspan_ver,set_field:1->tun_erspan_dir,set_field:0x1->tun_erspan_hwid,5"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=54,type=107,eth(dst=f8:bc:12:44:34:b8,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.94,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=2,sid=0x38,dir=1,hwid=0x1)),out_port(100)),1
])

dnl Dynamically set erspan v1
AT_CHECK([ovs-ofctl mod-flows int-br "action=set_field:1.1.2.94->tun_dst,set_field:1->tun_erspan_ver,set_field:1->tun_erspan_idx,5"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=50,type=107,eth(dst=f8:bc:12:44:34:b8,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.94,proto=47,tos=0,ttl=64,frag=0x4000),erspan(ver=1,sid=0x38,idx=0x1)),out_port(100)),1
])

dnl Check ERSPAN tunnel pop
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(1),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.11.93,dst=1.1.2.88,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_pop(3)
])

AT_CHECK([ovs-ofctl del-flows int-br])
AT_CHECK([ovs-appctl revalidator/wait])

dnl Check decapsulation of ERSPAN v1
dnl Hex dump: GRE:(100088be)
dnl ERSPAN: v1, session id = 0x7b (1000007b), index=3 (00000003)
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007e79464000402fba550101025c01010258100088be000000011000007b00000003fe71d883724fbeb6f4e1494a080045000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])

AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-ofctl dump-ports int-br | grep 'port  2'], [0], [dnl
  port  2: rx pkts=1, bytes=98, drop=?, errs=?, frame=?, over=?, crc=?
])

dnl Check decapsulation ERSPAN v2
dnl Hex dump: GRE:(100022eb)
dnl ERSPAN: v2, session id = 0x237 (20000237), hwid =7, dir = 1 (00000078)
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007e79464000402fba550101025c01010258100022eb000000012000023710abcd0100000078fe71d883724fbeb6f4e1494a080045000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])

AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-ofctl dump-ports int-br | grep 'port  3'], [0], [dnl
  port  3: rx pkts=1, bytes=98, drop=?, errs=?, frame=?, over=?, crc=?
])

dnl Check ERSPAN encap in pcap file
dnl This ARP reply from p0 has two effects:
dnl 1. The ARP cache will learn that 1.1.2.92 is at f8:bc:12:44:34:b6.
dnl 2. The br0 mac learning will learn that f8:bc:12:44:34:b6 is on p0.
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(2),eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'])

AT_CHECK([ovs-vsctl -- set Interface p0 options:tx_pcap=p0.pcap])

dnl Output to tunnel from a int-br internal port
AT_CHECK([ovs-ofctl add-flow int-br "in_port=LOCAL,actions=output:2"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-appctl netdev-dummy/receive int-br '50540000000a5054000000091234'])
dnl 100088be: GRE with ERSPANv1, 00000001: Seqno, 1000007b: v1 with 0x7b session ID (key)
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep 100088be000000011000007b | wc -l` -ge 1])

AT_CHECK([ovs-ofctl mod-flows int-br "in_port=LOCAL,actions=output:3"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-appctl netdev-dummy/receive int-br '50540000000a5054000000091235'])
dnl 100022eb: GRE with ERSPANv2, 00000001: Seqno, 20000237: v2 with 0x237 session ID (key)
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep 100022eb0000000120000237 | wc -l` -ge 1])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - action])

OVS_VSWITCHD_START([add-port br0 p0 -- set Interface p0 type=dummy ofport_request=1 other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy], [0])
AT_CHECK([ovs-vsctl add-port int-br t2 -- set Interface t2 type=vxlan \
                       options:remote_ip=1.1.2.92 options:key=123 ofport_request=2\
                    -- add-port int-br t1 -- set Interface t1 type=gre \
                       options:remote_ip=1.1.2.92 options:key=456 ofport_request=3\
                    -- add-port int-br t3 -- set Interface t3 type=vxlan \
                       options:remote_ip=1.1.2.93 options:out_key=flow options:csum=true ofport_request=4\
                    -- add-port int-br t4 -- set Interface t4 type=geneve \
                       options:remote_ip=flow options:key=123 ofport_request=5\
                    -- add-port int-br t5 -- set Interface t5 type=geneve \
                       options:remote_ip=1.1.2.93 options:out_key=flow options:egress_pkt_mark=1234 ofport_request=6\
                    -- add-port int-br t6 -- set Interface t6 type=gre \
                       options:remote_ip=1.1.2.92 options:key=456 options:packet_type=legacy_l3 ofport_request=7\
                    -- add-port int-br t7 -- set Interface t7 type=vxlan \
                       options:remote_ip=1.1.2.92 options:key=345 options:exts=gpe ofport_request=8\
                    -- add-port int-br t8 -- set Interface t8 type=gtpu \
                       options:remote_ip=1.1.2.92 options:key=123 ofport_request=9\
                       ], [0])

AT_CHECK([ovs-appctl dpif/show], [0], [dnl
dummy@ovs-dummy: hit:0 missed:0
  br0:
    br0 65534/100: (dummy-internal)
    p0 1/1: (dummy)
  int-br:
    int-br 65534/2: (dummy-internal)
    t1 3/3: (gre: key=456, remote_ip=1.1.2.92)
    t2 2/4789: (vxlan: key=123, remote_ip=1.1.2.92)
    t3 4/4789: (vxlan: csum=true, out_key=flow, remote_ip=1.1.2.93)
    t4 5/6081: (geneve: key=123, remote_ip=flow)
    t5 6/6081: (geneve: egress_pkt_mark=1234, out_key=flow, remote_ip=1.1.2.93)
    t6 7/3: (gre: key=456, packet_type=legacy_l3, remote_ip=1.1.2.92)
    t7 8/4789: (vxlan: key=345, remote_ip=1.1.2.92)
    t8 9/2152: (gtpu: key=123, remote_ip=1.1.2.92)
])

dnl Setup dummy interface IP addresses.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
AT_CHECK([ovs-appctl netdev-dummy/ip6addr br0 2001:cafe::88/24], [0], [OK
])
dnl Add a static route with a mark.
AT_CHECK([ovs-appctl ovs/route/add 1.1.2.92/24 br0 pkt_mark=1234], [0], [OK
])
dnl Checking that local routes for added IPs and the static route with a mark
dnl were successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep br0 | sort], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
Cached: 2001:ca00::/24 dev br0 SRC 2001:cafe::88 local
User: 1.1.2.0/24 MARK 1234 dev br0 SRC 1.1.2.88
])

AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl Check ARP request
AT_CHECK([ovs-vsctl -- set Interface p0 options:pcap=p0.pcap])

AT_CHECK([ovs-appctl netdev-dummy/receive int-br 'in_port(2),eth(src=aa:55:aa:55:00:00,dst=f8:bc:12:ff:ff:ff),eth_type(0x0800),ipv4(src=1.1.3.92,dst=1.1.3.88,proto=1,tos=0,ttl=64,frag=no),icmp(type=0,code=0)'])

dnl Wait for the two ARP requests to be sent. Sometimes the system
dnl can be slow (e.g. under valgrind)
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | sort | uniq | wc -l` -ge 2])

AT_CHECK([ovs-pcap p0.pcap > p0.pcap.txt 2>&1])

AT_CHECK([cat p0.pcap.txt | grep 101025c | uniq], [0], [dnl
ffffffffffffaa55aa55000008060001080006040001aa55aa550000010102580000000000000101025c
])
AT_CHECK([cat p0.pcap.txt | grep 101025d | uniq], [0], [dnl
ffffffffffffaa55aa55000008060001080006040001aa55aa550000010102580000000000000101025d
])

dnl Check input range
AT_CHECK([ovs-appctl tnl/neigh/aging 0], [2], [], [dnl
bad aging value
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl tnl/neigh/aging 3601], [2], [], [dnl
bad aging value
ovs-appctl: ovs-vswitchd: server returned an error
])

AT_CHECK([ovs-appctl tnl/neigh/aging 1], [0], [OK
])

AT_CHECK([ovs-appctl tnl/neigh/aging 3600], [0], [OK
])

dnl Set the aging time to 5 seconds
AT_CHECK([ovs-appctl tnl/neigh/aging 5], [0], [OK
])

dnl Read the current aging time
AT_CHECK([ovs-appctl tnl/neigh/aging], [0], [5
])

dnl Add an entry
AT_CHECK([ovs-appctl tnl/neigh/set br0 1.1.2.92 aa:bb:cc:00:00:01], [0], [OK
])

AT_CHECK([ovs-appctl tnl/neigh/show | grep br0 | sort], [0], [dnl
1.1.2.92                                      aa:bb:cc:00:00:01   br0
])

ovs-appctl time/warp 5000

dnl Check the entry has been removed
AT_CHECK([ovs-appctl tnl/neigh/show | grep br0 | sort], [0], [dnl
])

dnl Restore the aging time to 900s (default)
AT_CHECK([ovs-appctl tnl/neigh/aging 900], [0], [OK
])

dnl Read the current aging time
AT_CHECK([ovs-appctl tnl/neigh/aging], [0], [900
])

dnl Check ARP Snoop
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:c8,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:c8,tha=00:00:00:00:00:00)'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | grep br0 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:c8   br0
])

dnl Receiving ARP reply with incorrect 'tip' should not alter tunnel neighbor cache
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:b8,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.90,op=2,sha=f8:bc:12:44:34:b8,tha=00:00:00:00:00:00)'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | grep br0 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:c8   br0
])

dnl Receiving ARP reply with incorrect VLAN id should not alter tunnel neighbor cache
AT_CHECK([ovs-vsctl set port br0 tag=10])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x8100),vlan(vid=99,pcp=7),encap(eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00))'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | grep br0 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:c8   br0
])

dnl Receiving ARP reply with correct VLAN id should alter tunnel neighbor cache
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x8100),vlan(vid=10,pcp=7),encap(eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00))'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | grep br0 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b6   br0
])

dnl Receiving ARP reply in overlay bridge should not alter tunnel neighbor cache
AT_CHECK([ovs-vsctl add-port int-br p1 -- set interface p1 type=dummy ofport_request=200 other-config:hwaddr=aa:55:aa:55:00:99])
AT_CHECK([ovs-appctl netdev-dummy/receive p1 'recirc_id(0),in_port(200),eth(src=f8:bc:12:44:34:c8,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:c8,tha=00:00:00:00:00:00)'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | grep br | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b6   br0
])

dnl Receiving Gratuitous ARP request with correct VLAN id should alter tunnel neighbor cache
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:c8,dst=ff:ff:ff:ff:ff:ff),eth_type(0x8100),vlan(vid=10,pcp=7),encap(eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.92,op=1,sha=f8:bc:12:44:34:c8,tha=00:00:00:00:00:00))'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | grep br | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:c8   br0
])

dnl Receiving Gratuitous ARP reply with correct VLAN id should alter tunnel neighbor cache
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:b2,dst=ff:ff:ff:ff:ff:ff),eth_type(0x8100),vlan(vid=10,pcp=7),encap(eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.92,op=2,sha=f8:bc:12:44:34:b2,tha=f8:bc:12:44:34:b2))'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | grep br | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b2   br0
])

dnl Receive ARP reply without VLAN header
AT_CHECK([ovs-vsctl set port br0 tag=0])
AT_CHECK([ovs-appctl tnl/neigh/flush], [0], [OK
])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:b7,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.93,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b7,tha=00:00:00:00:00:00)'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-appctl tnl/neigh/show | tail -n+3 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b6   br0
1.1.2.93                                      f8:bc:12:44:34:b7   br0
])

AT_CHECK([ovs-appctl tnl/ports/show |sort], [0], [dnl
Listening ports:
genev_sys_6081 (6081) ref_cnt=2
gre_sys (3) ref_cnt=2
gtpu_sys_2152 (2152) ref_cnt=1
vxlan_sys_4789 (4789) ref_cnt=3
])

dnl Check VXLAN tunnel pop
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(1),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.2.92,dst=1.1.2.88,proto=17,tos=0,ttl=64,frag=no),udp(src=51283,dst=4789)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_pop(4789)
])

dnl Check GRE tunnel pop
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(1),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.2.92,dst=1.1.2.88,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_pop(3)
])

dnl Check Geneve tunnel pop
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(1),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.2.92,dst=1.1.2.88,proto=17,tos=0,ttl=64,frag=no),udp(src=51283,dst=6081)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_pop(6081)
])

dnl Check Geneve tunnel (t6) pop
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(1),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.2.96,dst=1.1.2.88,proto=17,tos=0,ttl=64,frag=no),udp(src=51283,dst=6081)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_pop(6081)
])

dnl Check GTP-U tunnel pop
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(1),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.2.92,dst=1.1.2.88,proto=17,tos=0,ttl=64,frag=no),udp(src=51283,dst=2152)'],
[0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_pop(2152)
])

dnl Check VXLAN tunnel push
AT_CHECK([ovs-ofctl add-flow int-br action=2])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(4789),header(size=50,type=4,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=4789,csum=0x0),vxlan(flags=0x8000000,vni=0x7b)),out_port(100)),1
])

dnl Check VXLAN GPE tunnel push
AT_CHECK([ovs-ofctl add-flow int-br action=8])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:01),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(4789),header(size=50,type=4,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=4789,csum=0x0),vxlan(flags=0xc000003,vni=0x159)),out_port(100)),1
])

dnl Check VXLAN tunnel push set tunnel id by flow and checksum
AT_CHECK([ovs-ofctl add-flow int-br "actions=set_tunnel:124,4"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(4789),header(size=50,type=4,eth(dst=f8:bc:12:44:34:b7,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.93,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=4789,csum=0xffff),vxlan(flags=0x8000000,vni=0x7c)),out_port(100)),1
])

dnl Check GRE tunnel push
AT_CHECK([ovs-ofctl add-flow int-br action=3])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=42,type=3,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=47,tos=0,ttl=64,frag=0x4000),gre((flags=0x2000,proto=0x6558),key=0x1c8)),out_port(100)),1
])

dnl Check L3GRE tunnel push
AT_CHECK([ovs-ofctl add-flow int-br action=7])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:01),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: pop_eth,tnl_push(tnl_port(3),header(size=42,type=3,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=47,tos=0,ttl=64,frag=0x4000),gre((flags=0x2000,proto=0x800),key=0x1c8)),out_port(100)),1
])

dnl Check Geneve tunnel push
AT_CHECK([ovs-ofctl add-flow int-br "actions=set_field:1.1.2.92->tun_dst,5"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(6081),header(size=50,type=5,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=6081,csum=0x0),geneve(vni=0x7b)),out_port(100)),1
])

dnl Check Geneve tunnel push with pkt-mark
AT_CHECK([ovs-ofctl add-flow int-br "actions=set_tunnel:234,6"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: set(skb_mark(0x4d2)),tnl_push(tnl_port(6081),header(size=50,type=5,eth(dst=f8:bc:12:44:34:b7,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.93,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=6081,csum=0x0),geneve(vni=0xea)),out_port(100)),1
])

dnl Check Geneve tunnel push with options
AT_CHECK([ovs-ofctl add-tlv-map int-br "{class=0xffff,type=0x80,len=4}->tun_metadata0"])
AT_CHECK([ovs-ofctl add-flow int-br "actions=set_field:1.1.2.92->tun_dst,set_field:0xa->tun_metadata0,5"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(6081),header(size=58,type=5,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=6081,csum=0x0),geneve(crit,vni=0x7b,options({class=0xffff,type=0x80,len=4,0xa}))),out_port(100)),1
])

dnl Check GTP-U tunnel push
AT_CHECK([ovs-ofctl add-flow int-br "actions=9"])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'],
[0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: pop_eth,tnl_push(tnl_port(2152),header(size=50,type=110,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=2152,csum=0x0),gtpu(flags=0x30,msgtype=255,teid=0x7b)),out_port(100)),1
])
AT_CHECK([ovs-ofctl del-flows int-br])
AT_CHECK([ovs-appctl revalidator/wait])

dnl Check decapsulation of VXLAN packet
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a00000800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a02320800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a202e0c00000300015900fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
ovs-appctl time/warp 1000
AT_CHECK([ovs-ofctl dump-ports int-br | grep -E 'port  [[248]]:' | sort], [0], [dnl
  port  2: rx pkts=2, bytes=84, drop=?, errs=?, frame=?, over=?, crc=?
  port  4: rx pkts=0, bytes=0, drop=?, errs=?, frame=?, over=?, crc=?
  port  8: rx pkts=1, bytes=42, drop=?, errs=?, frame=?, over=?, crc=?
])

dnl Idem, with Rx cksum good
AT_CHECK([ovs-vsctl set interface p0 options:ol_ip_rx_csum_set_good=true])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a00000800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a02320800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a202e0c00000300015900fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-vsctl set interface p0 options:ol_ip_rx_csum_set_good=false])
ovs-appctl time/warp 1000
AT_CHECK([ovs-ofctl dump-ports int-br | grep -E 'port  [[248]]:' | sort], [0], [dnl
  port  2: rx pkts=4, bytes=168, drop=?, errs=?, frame=?, over=?, crc=?
  port  4: rx pkts=0, bytes=0, drop=?, errs=?, frame=?, over=?, crc=?
  port  8: rx pkts=2, bytes=84, drop=?, errs=?, frame=?, over=?, crc=?
])

dnl Check decapsulation of GRE packet
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007e79464000402fba550101025c0101025820006558000001c8fe71d883724fbeb6f4e1494a080045000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007e79464000402fba550101025c0101025820006558000001c8fe71d883724fbeb6f4e1494a080045000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007e79464000402fba550101025c0101025820006558000001c8fe71d883724fbeb6f4e1494a080045000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
ovs-appctl time/warp 1000

AT_CHECK([ovs-ofctl dump-ports int-br | grep 'port  3'], [0], [dnl
  port  3: rx pkts=3, bytes=294, drop=?, errs=?, frame=?, over=?, crc=?
])

dnl Check decapsulation of L3GRE packet
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-ofctl dump-ports int-br | grep 'port  7'], [0], [dnl
  port  7: rx pkts=3, bytes=252, drop=?, errs=?, frame=?, over=?, crc=?
])

dnl No drop so far.
AT_CHECK([ovs-appctl coverage/read-counter datapath_drop_tunnel_pop_error], [0], [0
])
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l3csum_err], [0], [0
])
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l4csum_err], [0], [0
])
dnl Yet csum validation happened on all previous packets.
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l3csum_checked], [0], [12
])
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l4csum_checked], [0], [4
])

dnl Send various incorrect bad IP checksum packets.
dnl GRE
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fDEAD0101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
dnl VxLAN
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e000100004011DEAD0101025c0101025812b512b5003a00000800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
dnl VxLAN-GPE
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e000100004011DEAD0101025c0101025812b512b5003a202e0c00000300015900fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])

dnl Idem, with Rx cksum bad but with a valid and an invalid packet.
AT_CHECK([ovs-vsctl set interface p0 options:ol_ip_rx_csum_set_bad=true])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a02320800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fDEAD0101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
AT_CHECK([ovs-vsctl set interface p0 options:ol_ip_rx_csum_set_bad=false])

dnl Send various incorrect bad UDP checksum packets.
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003aDEAD0800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])

dnl Idem, with Rx cksum bad but with a valid and an invalid packet.
AT_CHECK([ovs-vsctl set interface p0 options:ol_l4_rx_csum_set_bad=true])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003a02320800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500004e00010000401173e90101025c0101025812b512b5003aDEAD0800000000007b00fe71d883724fbeb6f4e1494a08004500001c0001000040013ede1e0000011e0000020000ffff00000000'])
AT_CHECK([ovs-vsctl set interface p0 options:ol_l4_rx_csum_set_bad=false])

ovs-appctl time/warp 5000

AT_CHECK([ovs-appctl coverage/read-counter datapath_drop_tunnel_pop_error], [0], [8
])
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l3csum_err], [0], [5
])
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l4csum_err], [0], [3
])
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l3csum_checked], [0], [18
])
AT_CHECK([ovs-appctl coverage/read-counter native_tnl_l4csum_checked], [0], [5
])

AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004503007079464000402fba600101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])

ovs-appctl time/warp 5000

AT_CHECK([
ovs-appctl coverage/read-counter drop_action_congestion
], [0], [dnl
1
])


dnl Check GREL3 only accepts non-fragmented packets?
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007e79464000402fba550101025c0101025820000800000001c8fe71d883724fbeb6f4e1494a080045000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

AT_CHECK([ovs-ofctl dump-ports int-br | grep 'port  [[37]]' | sort], [0], [dnl
  port  3: rx pkts=3, bytes=294, drop=?, errs=?, frame=?, over=?, crc=?
  port  7: rx pkts=5, bytes=434, drop=?, errs=?, frame=?, over=?, crc=?
])

dnl Send out packets received from L3GRE tunnel back to L3GRE tunnel
AT_CHECK([ovs-ofctl del-flows int-br])
AT_CHECK([ovs-ofctl add-flow int-br "in_port=7,actions=set_field:3->in_port,7"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-vsctl -- set Interface br0 options:pcap=br0.pcap])

AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])

ovs-appctl time/warp 1000

AT_CHECK([ovs-pcap p0.pcap > p0.pcap.txt 2>&1])
AT_CHECK([tail -6 p0.pcap.txt], [0], [dnl
aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
001b213cab64aa55aa55000008004500007000004000402f33aa010102580101025c20000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
001b213cab64aa55aa55000008004500007000004000402f33aa010102580101025c20000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
aa55aa550000001b213cab6408004500007079464000402fba630101025c0101025820000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
001b213cab64aa55aa55000008004500007000004000402f33aa010102580101025c20000800000001c845000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
])


dnl Check decapsulation of Geneve packet with options
AT_CAPTURE_FILE([ofctl_monitor.log])
AT_CHECK([ovs-ofctl monitor int-br 65534 --detach --no-chdir --pidfile 2> ofctl_monitor.log])

AT_CHECK([ovs-ofctl del-flows int-br])
AT_CHECK([ovs-ofctl add-flow int-br "tun_metadata0=0xa/0xf,actions=5,controller"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000001b213cab64080045000096794640004011ba5b0101025c01010258308817c1008200000400655800007b00ffff80010000000affff00010000000bfe71d883724fbeb6f4e1494a080045000054ba200000400184861e0000011e00000200004227e75400030af3195500000000f265010000000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637'])

OVS_WAIT_UNTIL([test `wc -l < ofctl_monitor.log` -ge 2])
OVS_APP_EXIT_AND_WAIT(ovs-ofctl)

AT_CHECK([cat ofctl_monitor.log], [0], [dnl
NXT_PACKET_IN2 (xid=0x0): cookie=0x0 total_len=98 tun_id=0x7b,tun_src=1.1.2.92,tun_dst=1.1.2.88,tun_metadata0=0xa,in_port=5 (via action) data_len=98 (unbuffered)
icmp,vlan_tci=0x0000,dl_src=be:b6:f4:e1:49:4a,dl_dst=fe:71:d8:83:72:4f,nw_src=30.0.0.1,nw_dst=30.0.0.2,nw_tos=0,nw_ecn=0,nw_ttl=64,nw_frag=no,icmp_type=0,icmp_code=0 icmp_csum:4227
])

AT_CHECK([ovs-ofctl dump-ports int-br | grep 'port  5'], [0], [dnl
  port  5: rx pkts=1, bytes=98, drop=?, errs=?, frame=?, over=?, crc=?
])
AT_CHECK([ovs-appctl dpif/dump-flows int-br | grep 'in_port(6081)' | sed -e 's/recirc_id=[[0-9]]*/recirc_id=<cleared>/g'], [0], [dnl
recirc_id(0),tunnel(tun_id=0x7b,src=1.1.2.92,dst=1.1.2.88,geneve({class=0xffff,type=0x80,len=4,0xa/0xf}{class=0xffff,type=0,len=4}),flags(+df-csum+key)),in_port(6081),packet_type(ns=0,id=0),eth_type(0x0800),ipv4(frag=no), packets:0, bytes:0, used:never, actions:userspace(pid=0,controller(reason=1,dont_send=0,continuation=0,recirc_id=<cleared>,rule_cookie=0,controller_id=0,max_len=65535))
])

dnl Receive VXLAN with different MAC and verify that the neigh cache gets updated
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000f8bc1244cafe08004500004e00010000401173e90101025c01010258c85312b5003a8cd40c00000300007b00ffffffffffff00000000000008004500001c0001000040117cce7f0000017f0000010035003500080172'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

dnl Check VXLAN tunnel push
AT_CHECK([ovs-ofctl add-flow int-br action=2])
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=36:b1:ee:7c:01:01,dst=36:b1:ee:7c:01:02),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(4789),header(size=50,type=4,eth(dst=f8:bc:12:44:ca:fe,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=4789,csum=0x0),vxlan(flags=0x8000000,vni=0x7b)),out_port(100)),1
])

AT_CHECK([ovs-appctl tnl/neigh/show | tail -n+3 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:ca:fe   br0
1.1.2.93                                      f8:bc:12:44:34:b7   br0
])

dnl Restore and check the cache entries
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'aa55aa550000f8bc124434b608004500004e00010000401173e90101025c01010258c85312b5003a8cd40c00000300007b00ffffffffffff00000000000008004500001c0001000040117cce7f0000017f0000010035003500080172'])

ovs-appctl time/warp 1000
ovs-appctl time/warp 1000

dnl Check VXLAN tunnel push
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=36:b1:ee:7c:01:01,dst=36:b1:ee:7c:01:02),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=47,tos=0,ttl=64,frag=no)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(4789),header(size=50,type=4,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),udp(src=0,dst=4789,csum=0x0),vxlan(flags=0x8000000,vni=0x7b)),out_port(100)),1
])

AT_CHECK([ovs-appctl tnl/neigh/show | tail -n+3 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b6   br0
1.1.2.93                                      f8:bc:12:44:34:b7   br0
])

ovs-appctl time/warp 10000

AT_CHECK([ovs-vsctl del-port int-br t3 \
                    -- del-port int-br t5 \
                    -- set Interface t1 type=vxlan \
                    -- set Interface t2 options:dst_port=4790 \
                       ], [0])

dnl Check tunnel lookup entries after deleting/reconfiguring some ports
AT_CHECK([ovs-appctl tnl/ports/show |sort], [0], [dnl
Listening ports:
genev_sys_6081 (6081) ref_cnt=1
gre_sys (3) ref_cnt=1
gtpu_sys_2152 (2152) ref_cnt=1
vxlan_sys_4789 (4789) ref_cnt=2
vxlan_sys_4790 (4790) ref_cnt=1
])

AT_CHECK([ovs-vsctl del-port int-br t1 \
                    -- del-port int-br t2 \
                    -- del-port int-br t4 \
                    -- del-port int-br t6 \
                    -- del-port int-br t7 \
                    -- del-port int-br t8 \
                       ], [0])

dnl Check tunnel lookup entries after deleting all remaining tunnel ports
AT_CHECK([ovs-appctl tnl/ports/show |sort], [0], [dnl
Listening ports:
])

OVS_VSWITCHD_STOP(["/dropping tunnel packet marked ECN CE but is not ECN capable/d
/ip packet has invalid checksum/d"])
AT_CLEANUP

AT_SETUP([tunnel_push_pop - packet_out])

OVS_VSWITCHD_START([add-port br0 p0 -- set Interface p0 type=dummy ofport_request=1 other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-appctl vlog/set dpif_netdev:dbg])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy])
AT_CHECK([ovs-vsctl add-port int-br t2 -- set Interface t2 type=geneve \
                       options:remote_ip=1.1.2.92 options:key=123 ofport_request=2 \
                       ])

dnl Setup dummy interface IP address.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
])

AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl This ARP reply from p0 has two effects:
dnl 1. The ARP cache will learn that 1.1.2.92 is at f8:bc:12:44:34:b6.
dnl 2. The br0 mac learning will learn that f8:bc:12:44:34:b6 is on p0.
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(2),eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'])

AT_CHECK([ovs-vsctl -- set Interface p0 options:tx_pcap=p0.pcap])

dnl Output to tunnel from a int-br internal port
AT_CHECK([ovs-ofctl add-flow int-br "in_port=LOCAL,actions=output:2"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-appctl netdev-dummy/receive int-br '50540000000a5054000000091234'])
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep 50540000000a5054000000091234 | wc -l` -ge 1])

dnl Output to tunnel from the controller
AT_CHECK([ovs-ofctl -O OpenFlow13 packet-out int-br CONTROLLER "output:2" '50540000000a5054000000091235'])
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep 50540000000a5054000000091235 | wc -l` -ge 1])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - packet_out debug_slow])

OVS_VSWITCHD_START(
    [add-port br0 p0 dnl
     -- set Interface p0 type=dummy ofport_request=1 dnl
                         other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-appctl vlog/set dpif_netdev:dbg])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy])
AT_CHECK([ovs-vsctl add-port int-br t2 dnl
          -- set Interface t2 type=geneve options:remote_ip=1.1.2.92 dnl
                              options:key=123 ofport_request=2])

dnl Setup dummy interface IP address.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
])
AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl This ARP reply from p0 has two effects:
dnl 1. The ARP cache will learn that 1.1.2.92 is at f8:bc:12:44:34:b6.
dnl 2. The br0 mac learning will learn that f8:bc:12:44:34:b6 is on p0.
AT_CHECK([
  ovs-appctl netdev-dummy/receive p0 dnl
      'recirc_id(0),in_port(2),dnl
       eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
       arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'
])

AT_CHECK([ovs-vsctl -- set Interface p0 options:tx_pcap=p0.pcap])

packet=50540000000a505400000009123
dnl Source port is based on a packet hash, so it may differ depending on the
dnl compiler flags and CPU type.  Masked with '....'.
encap=f8bc124434b6aa55aa5500000800450000320000400040113406010102580101025c....17c1001e00000000655800007b00

dnl Output to tunnel from a int-br internal port.
dnl Checking that the packet arrived and it was correctly encapsulated.
AT_CHECK([ovs-ofctl add-flow int-br "in_port=LOCAL,actions=debug_slow,output:2"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-appctl netdev-dummy/receive int-br "${packet}4"])
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep -E "${encap}${packet}4" | wc -l` -ge 1])
dnl Sending again to exercise the non-miss upcall path.
AT_CHECK([ovs-appctl netdev-dummy/receive int-br "${packet}4"])
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep -E "${encap}${packet}4" | wc -l` -ge 2])

dnl Send two more packets at the same time to make sure they are distinct
dnl memory buffers.
AT_CHECK([ovs-appctl netdev-dummy/receive int-br "${packet}4" "${packet}4"])
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep -E "${encap}${packet}4" | wc -l` -ge 4])

dnl Make sure all the packets are the same, i.e. have the same source port.
AT_CHECK([ovs-pcap p0.pcap | sed 's/.$//' | sort | uniq \
            | grep -E -c "${encap}${packet}"], [0], [1
])

dnl Output to tunnel from the controller.
AT_CHECK([ovs-ofctl -O OpenFlow13 packet-out int-br CONTROLLER "debug_slow,output:2" "${packet}5"])
OVS_WAIT_UNTIL([test `ovs-pcap p0.pcap | grep -E "${encap}${packet}5" | wc -l` -ge 1])

dnl Datapath actions should not have tunnel push action.
AT_CHECK([ovs-appctl dpctl/dump-flows | grep -q tnl_push], [1])
dnl There should be slow_path action instead.
AT_CHECK([ovs-appctl dpctl/dump-flows | grep -q 'slow_path(action)'], [0])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - local_ip configuration])

OVS_VSWITCHD_START(
    [add-port br0 p0 \
     -- set Interface p0 type=dummy ofport_request=1 \
                         other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-appctl vlog/set dpif_netdev:dbg])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy])
AT_CHECK([ovs-vsctl add-port int-br t2 \
          -- set Interface t2 type=geneve \
                              options:local_ip=2.2.2.88 \
                              options:remote_ip=1.1.2.92 \
                              options:key=123 ofport_request=2])

dnl Setup multiple IP addresses.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 2.2.2.88/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached | sort], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
Cached: 2.2.2.0/24 dev br0 SRC 2.2.2.88 local
])
AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-ofctl add-flow int-br action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl This ARP reply from p0 has two effects:
dnl 1. The ARP cache will learn that 1.1.2.92 is at f8:bc:12:44:34:b6.
dnl 2. The br0 mac learning will learn that f8:bc:12:44:34:b6 is on p0.
AT_CHECK([ovs-appctl netdev-dummy/receive p0 dnl
 'recirc_id(0),in_port(1),dnl
  eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
  arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'
])

dnl Check that local_ip is used for encapsulation in the trace.
AT_CHECK([ovs-appctl ofproto/trace int-br in_port=LOCAL \
                | grep -E 'tunnel|actions'], [0], [dnl
     -> output to native tunnel
     -> tunneling to 1.1.2.92 via br0
     -> tunneling from aa:55:aa:55:00:00 2.2.2.88 to f8:bc:12:44:34:b6 1.1.2.92
Datapath actions: tnl_push(tnl_port(6081),header(size=50,type=5,dnl
eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),dnl
ipv4(src=2.2.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),dnl
udp(src=0,dst=6081,csum=0x0),geneve(vni=0x7b)),out_port(100)),1
])

dnl Now check that the packet actually has the local_ip in the header.
AT_CHECK([ovs-vsctl -- set Interface p0 options:tx_pcap=p0.pcap])

packet=50540000000a5054000000091234
eth=f8bc124434b6aa55aa5500000800
ip4=450000320000400040113305020202580101025c
dnl Source port is based on a packet hash, so it may differ depending on the
dnl compiler flags and CPU type.  Masked with '....'.
udp=....17c1001e0000
geneve=0000655800007b00
encap=${eth}${ip4}${udp}${geneve}
dnl Output to tunnel from a int-br internal port.
dnl Checking that the packet arrived and it was correctly encapsulated.
AT_CHECK([ovs-appctl netdev-dummy/receive int-br "${packet}"])
OVS_WAIT_UNTIL([test $(ovs-pcap p0.pcap | grep -c "${encap}${packet}") -eq 1])
dnl Sending again to exercise the non-miss upcall path.
AT_CHECK([ovs-appctl netdev-dummy/receive int-br "${packet}"])
OVS_WAIT_UNTIL([test $(ovs-pcap p0.pcap | grep -c "${encap}${packet}") -eq 2])

dnl Finally, checking that the datapath flow also has a local_ip.
AT_CHECK([ovs-appctl dpctl/dump-flows | grep tnl_push \
            | strip_ufid | strip_used], [0], [dnl
recirc_id(0),in_port(2),packet_type(ns=0,id=0),dnl
eth(src=50:54:00:00:00:09,dst=50:54:00:00:00:0a),eth_type(0x1234), dnl
packets:1, bytes:14, used:0.0s, dnl
actions:tnl_push(tnl_port(6081),header(size=50,type=5,dnl
eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),dnl
ipv4(src=2.2.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),dnl
udp(src=0,dst=6081,csum=0x0),geneve(vni=0x7b)),out_port(100)),1
])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - underlay bridge match])

OVS_VSWITCHD_START([add-port br0 p0 -- set Interface p0 type=dummy ofport_request=1 other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy], [0])
AT_CHECK([ovs-vsctl add-port int-br t1 -- set Interface t1 type=gre \
                       options:remote_ip=1.1.2.92 options:key=456 options:seq=true ofport_request=3], [0])

AT_CHECK([ovs-appctl dpif/show], [0], [dnl
dummy@ovs-dummy: hit:0 missed:0
  br0:
    br0 65534/100: (dummy-internal)
    p0 1/1: (dummy)
  int-br:
    int-br 65534/2: (dummy-internal)
    t1 3/3: (gre: key=456, remote_ip=1.1.2.92, seq=true)
])

AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
])

AT_CHECK([ovs-ofctl add-flow br0 'arp,priority=1,action=normal'])
AT_CHECK([ovs-appctl revalidator/wait])

dnl Use arp reply to achieve tunnel next hop mac binding
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0806),arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'])

AT_CHECK([ovs-appctl tnl/neigh/show | tail -n+3 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b6   br0
])

AT_CHECK([ovs-ofctl add-flow br0 'ip,ip_proto=47,nw_tos=0,eth_src=aa:55:aa:55:00:00,eth_dst=f8:bc:12:44:34:b6,ip_src=1.1.2.88,ip_dst=1.1.2.92,priority=99,action=normal'])

dnl Direct traffic from the integration bridge to the GRE tunnel
AT_CHECK([ovs-ofctl add-flow int-br action=3])
AT_CHECK([ovs-appctl revalidator/wait])

AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(2),eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:00),eth_type(0x0800),ipv4(src=1.1.3.88,dst=1.1.3.112,proto=17,tos=0,ttl=64,frag=no),udp(src=51283,dst=4789)'], [0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(3),header(size=46,type=3,eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),ipv4(src=1.1.2.88,dst=1.1.2.92,proto=47,tos=0,ttl=64,frag=0x4000),gre((flags=0x3000,proto=0x6558),key=0x1c8,seq=0x0)),out_port(100)),1
])

dnl Verify outer L2 and L3 header flow fields can be matched in the underlay bridge
AT_CHECK([ovs-appctl netdev-dummy/receive int-br '50540000000a5054000000091234'])
AT_CHECK([ovs-ofctl dump-flows br0 | ofctl_strip | sort], [0], [dnl)
 n_packets=1, n_bytes=42, priority=1,arp actions=NORMAL
 n_packets=1, n_bytes=60, priority=99,ip,dl_src=aa:55:aa:55:00:00,dl_dst=f8:bc:12:44:34:b6,nw_src=1.1.2.88,nw_dst=1.1.2.92,nw_proto=47,nw_tos=0 actions=NORMAL
NXST_FLOW reply:
])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - flow translation on unrelated bridges])

OVS_VSWITCHD_START(
    [add-port br0 p0 dnl
     -- set Interface p0 type=dummy ofport_request=1 dnl
                         other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-appctl vlog/set dpif_netdev:dbg])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy])
AT_CHECK([ovs-vsctl add-port int-br t2 dnl
          -- set Interface t2 type=geneve options:remote_ip=1.1.2.92 dnl
                              options:key=123 ofport_request=2])

dnl Setup dummy interface IP address.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
])
AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl This ARP reply from p0 has two effects:
dnl 1. The ARP cache will learn that 1.1.2.92 is at f8:bc:12:44:34:b6.
dnl 2. The br0 mac learning will learn that f8:bc:12:44:34:b6 is on p0.
AT_CHECK([
  ovs-appctl netdev-dummy/receive p0 dnl
      'recirc_id(0),in_port(2),dnl
       eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
       arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'
])

dnl Creating a separate bridge that is completely unrelated to a tunnel
dnl configuration.  Ports in this bridge cannot be tunnel endpoints.
AT_CHECK([ovs-vsctl add-br br-non-tunnel dnl
          -- set bridge br-non-tunnel datapath_type=dummy fail-mode=secure])
add_of_ports br-non-tunnel 7 8
AT_CHECK([ovs-ofctl del-flows br-non-tunnel])
AT_CHECK([ovs-ofctl add-flow br-non-tunnel in_port=p7,action=p8])
AT_CHECK([ovs-ofctl add-flow br-non-tunnel in_port=p8,action=p7])

dnl Checking that tunnel configuration doesn't impact flow translation
dnl on this bridge (Megaflow should contain a bare minimum of fields
dnl according to installed OF rules).
AT_CHECK([ovs-appctl ofproto/trace br-non-tunnel in_port=p7], [0], [stdout])
AT_CHECK([tail -2 stdout], [0], [dnl
Megaflow: recirc_id=0,eth,in_port=7,dl_type=0x0000
Datapath actions: 8
])

AT_CHECK([ovs-appctl ofproto/trace br-non-tunnel in_port=p8], [0], [stdout])
AT_CHECK([tail -2 stdout], [0], [dnl
Megaflow: recirc_id=0,eth,in_port=8,dl_type=0x0000
Datapath actions: 7
])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - VXLAN access port])

dnl Create bridge that has a MAC address.
OVS_VSWITCHD_START([set bridge br0 datapath_type=dummy dnl
                    -- set Interface br0 other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-vsctl add-port br0 p8 dnl
                    -- set Interface p8 type=dummy ofport_request=8])

dnl Create another bridge.
AT_CHECK([ovs-vsctl add-br ovs-tun0 -- set bridge ovs-tun0 datapath_type=dummy])

dnl Add VXLAN port to this bridge.
AT_CHECK([ovs-vsctl add-port ovs-tun0 tun0 dnl
            -- set int tun0 type=vxlan options:remote_ip=10.0.0.11 dnl
            -- add-port ovs-tun0 p7 dnl
            -- set interface p7 type=dummy ofport_request=7])

dnl Set VLAN tags, so that br0 and its port p8 have the same tag,
dnl but ovs-tun0's port p7 has a different tag.
AT_CHECK([ovs-vsctl set port p8  tag=42 dnl
                 -- set port br0 tag=42 dnl
                 -- set port p7  tag=200])

dnl Set an IP address for br0.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 10.0.0.2/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached], [0], [dnl
Cached: 10.0.0.0/24 dev br0 SRC 10.0.0.2 local
])

dnl Send an ARP reply to port b8 on br0, so that packets will be forwarded
dnl to learned port.
AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

AT_CHECK([ovs-appctl netdev-dummy/receive p8 'in_port(8),dnl
   eth(src=aa:55:aa:66:00:00,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
   arp(sip=10.0.0.11,tip=10.0.0.2,op=2,sha=aa:55:aa:66:00:00,tha=00:00:00:00:00:00)'])

AT_CHECK([ovs-appctl ofproto/trace ovs-tun0 in_port=p7], [0], [stdout])
AT_CHECK([tail -2 stdout], [0], [dnl
Megaflow: recirc_id=0,eth,in_port=7,dl_src=00:00:00:00:00:00,dnl
dl_dst=00:00:00:00:00:00,dl_type=0x0000
Datapath actions: push_vlan(vid=200,pcp=0),1,tnl_push(tnl_port(4789),dnl
header(size=50,type=4,eth(dst=aa:55:aa:66:00:00,src=aa:55:aa:55:00:00,dnl
dl_type=0x0800),ipv4(src=10.0.0.2,dst=10.0.0.11,proto=17,tos=0,ttl=64,dnl
frag=0x4000),udp(src=0,dst=4789,csum=0x0),vxlan(flags=0x8000000,vni=0x0)),dnl
out_port(100)),8
])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - no clone when is_last_action])

dnl Create bridge that has a MAC address.
OVS_VSWITCHD_START([set bridge br0 datapath_type=dummy dnl
                    -- set Interface br0 other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-vsctl add-port br0 p8 dnl
          -- set Interface p8 type=dummy ofport_request=8])

dnl Create another bridge.
AT_CHECK([ovs-vsctl add-br ovs-tun0 -- set bridge ovs-tun0 datapath_type=dummy])

dnl Add VXLAN port to this bridge.
AT_CHECK([ovs-vsctl add-port ovs-tun0 tun0 dnl
          -- set int tun0 type=vxlan options:remote_ip=10.0.0.11 dnl
          -- add-port ovs-tun0 p7 dnl
          -- set interface p7 type=dummy ofport_request=7])

dnl Set an IP address for br0.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 10.0.0.2/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached], [0], [dnl
Cached: 10.0.0.0/24 dev br0 SRC 10.0.0.2 local
])

dnl Send an ARP reply to port b8 on br0, so that packets will be forwarded
dnl to learned port.
AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-ofctl del-flows ovs-tun0])
AT_CHECK([ovs-ofctl add-flow ovs-tun0 "in_port=p7,actions=tun0"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-appctl netdev-dummy/receive p8 'in_port(8),dnl
   eth(src=aa:55:aa:66:00:00,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
   arp(sip=10.0.0.11,tip=10.0.0.2,op=2,sha=aa:55:aa:66:00:00,tha=00:00:00:00:00:00)'])
AT_CHECK([ovs-appctl ofproto/trace ovs-tun0 in_port=p7], [0], [stdout])
AT_CHECK([tail -2 stdout], [0], [dnl
Megaflow: recirc_id=0,eth,in_port=7,dl_type=0x0000
Datapath actions: tnl_push(tnl_port(4789),header(size=50,type=4,dnl
eth(dst=aa:55:aa:66:00:00,src=aa:55:aa:55:00:00,dl_type=0x0800),dnl
ipv4(src=10.0.0.2,dst=10.0.0.11,proto=17,tos=0,ttl=64,frag=0x4000),dnl
udp(src=0,dst=4789,csum=0x0),vxlan(flags=0x8000000,vni=0x0)),out_port(100)),8
])

dnl Perform a negative check to make sure clone is still present.
AT_CHECK([ovs-ofctl del-flows ovs-tun0])
AT_CHECK([ovs-ofctl add-flow ovs-tun0 "in_port=p7,actions=tun0,in_port"])
AT_CHECK([ovs-appctl revalidator/wait])
AT_CHECK([ovs-appctl netdev-dummy/receive p8 'in_port(8),dnl
   eth(src=aa:55:aa:66:00:00,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
   arp(sip=10.0.0.11,tip=10.0.0.2,op=2,sha=aa:55:aa:66:00:00,tha=00:00:00:00:00:00)'])
AT_CHECK([ovs-appctl ofproto/trace ovs-tun0 in_port=p7], [0], [stdout])
AT_CHECK([tail -2 stdout], [0], [dnl
Megaflow: recirc_id=0,eth,in_port=7,dl_type=0x0000
Datapath actions: clone(tnl_push(tnl_port(4789),header(size=50,dnl
type=4,eth(dst=aa:55:aa:66:00:00,src=aa:55:aa:55:00:00,dl_type=0x0800),dnl
ipv4(src=10.0.0.2,dst=10.0.0.11,proto=17,tos=0,ttl=64,frag=0x4000),dnl
udp(src=0,dst=4789,csum=0x0),vxlan(flags=0x8000000,vni=0x0)),out_port(100)),8),7
])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - use non-local port as tunnel endpoint])

OVS_VSWITCHD_START([add-port br0 p0 \
                    -- set Interface p0 type=dummy ofport_request=1])

dnl Adding another port separately to ensure that it gets an
dnl aa:55:aa:55:00:03 MAC address (dummy port number 3).
AT_CHECK([ovs-vsctl add-port br0 vtep0 \
            -- set interface vtep0 type=dummy ofport_request=2])
AT_CHECK([ovs-vsctl \
          -- add-br int-br \
          -- set bridge int-br datapath_type=dummy \
          -- set Interface int-br ofport_request=3])
AT_CHECK([ovs-vsctl \
          -- add-port int-br t1 \
          -- set Interface t1 type=gre ofport_request=4 \
                              options:remote_ip=1.1.2.92
])

AT_CHECK([ovs-appctl dpif/show], [0], [dnl
dummy@ovs-dummy: hit:0 missed:0
  br0:
    br0 65534/100: (dummy-internal)
    p0 1/1: (dummy)
    vtep0 2/2: (dummy)
  int-br:
    int-br 65534/3: (dummy-internal)
    t1 4/4: (gre: remote_ip=1.1.2.92)
])

AT_CHECK([ovs-appctl netdev-dummy/ip4addr vtep0 1.1.2.88/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached], [0], [dnl
Cached: 1.1.2.0/24 dev vtep0 SRC 1.1.2.88 local
])

AT_CHECK([ovs-ofctl add-flow br0 action=normal])
AT_CHECK([ovs-ofctl add-flow int-br action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl Use arp request and reply to achieve tunnel next hop mac binding.
dnl By default, vtep0's MAC address is aa:55:aa:55:00:03.
AT_CHECK([ovs-appctl netdev-dummy/receive vtep0 'recirc_id(0),in_port(2),dnl
  eth(dst=ff:ff:ff:ff:ff:ff,src=aa:55:aa:55:00:03),eth_type(0x0806),dnl
  arp(tip=1.1.2.92,sip=1.1.2.88,op=1,sha=aa:55:aa:55:00:03,tha=00:00:00:00:00:00)'])
AT_CHECK([ovs-appctl netdev-dummy/receive p0 'recirc_id(0),in_port(1),dnl
  eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:03),eth_type(0x0806),dnl
  arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=aa:55:aa:55:00:03)'])

AT_CHECK([ovs-appctl tnl/neigh/show | tail -n+3 | sort], [0], [dnl
1.1.2.92                                      f8:bc:12:44:34:b6   br0
])

dnl Check GRE tunnel pop.
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(1),dnl
  eth(src=f8:bc:12:44:34:b6,dst=aa:55:aa:55:00:03),eth_type(0x0800),dnl
  ipv4(src=1.1.2.92,dst=1.1.2.88,proto=47,tos=0,ttl=64,frag=no)'],
[0], [stdout])

AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_pop(4)
])

dnl Check GRE tunnel push.
AT_CHECK([ovs-appctl ofproto/trace ovs-dummy 'in_port(3),dnl
  eth(dst=f9:bc:12:44:34:b6,src=af:55:aa:55:00:03),eth_type(0x0800),dnl
  ipv4(src=1.1.3.88,dst=1.1.3.92,proto=1,tos=0,ttl=64,frag=no)'],
[0], [stdout])
AT_CHECK([tail -1 stdout], [0],
  [Datapath actions: tnl_push(tnl_port(4),header(size=38,type=3,dnl
eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:03,dl_type=0x0800),dnl
ipv4(src=1.1.2.88,dst=1.1.2.92,proto=47,tos=0,ttl=64,frag=0x4000),dnl
gre((flags=0x0,proto=0x6558))),out_port(2)),1
])

OVS_VSWITCHD_STOP
AT_CLEANUP

dnl This is a regression test for outer header checksum offloading
dnl with recirculation.
AT_SETUP([tunnel_push_pop - recirculation after encapsulation])

OVS_VSWITCHD_START(
    [add-port br0 p0 \
     -- set Interface p0 type=dummy ofport_request=1 \
                         other-config:hwaddr=aa:55:aa:55:00:00])
AT_CHECK([ovs-appctl vlog/set dpif_netdev:dbg])
AT_CHECK([ovs-vsctl add-br int-br -- set bridge int-br datapath_type=dummy])
AT_CHECK([ovs-vsctl add-port int-br t2 \
          -- set Interface t2 type=geneve \
                              options:remote_ip=1.1.2.92 \
                              options:key=123 ofport_request=2])

dnl Setup an IP address.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br0 1.1.2.88/24], [0], [OK
])
dnl Checking that a local route for added IP was successfully installed.
AT_CHECK([ovs-appctl ovs/route/show | grep Cached | sort], [0], [dnl
Cached: 1.1.2.0/24 dev br0 SRC 1.1.2.88 local
])

dnl Add a dp-hash selection group.
AT_CHECK([ovs-ofctl add-group br0 \
    'group_id=1234,type=select,selection_method=dp_hash,bucket=weight=1,output:p0'])
AT_CHECK([ovs-ofctl add-flow br0 in_port=br0,action=group:1234])
AT_CHECK([ovs-ofctl add-flow br0 in_port=p0,action=normal])

AT_CHECK([ovs-ofctl add-flow int-br action=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl This ARP reply from p0 has two effects:
dnl 1. The ARP cache will learn that 1.1.2.92 is at f8:bc:12:44:34:b6.
dnl 2. The br0 mac learning will learn that f8:bc:12:44:34:b6 is on p0.
AT_CHECK([ovs-appctl netdev-dummy/receive p0 dnl
 'recirc_id(0),in_port(1),dnl
  eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
  arp(sip=1.1.2.92,tip=1.1.2.88,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'
])

dnl Check that selection group is used in the trace.
AT_CHECK([ovs-appctl ofproto/trace int-br in_port=LOCAL \
                | grep -E 'tunnel|actions'], [0], [dnl
     -> output to native tunnel
     -> tunneling to 1.1.2.92 via br0
     -> tunneling from aa:55:aa:55:00:00 1.1.2.88 to f8:bc:12:44:34:b6 1.1.2.92
Datapath actions: tnl_push(tnl_port(6081),header(size=50,type=5,dnl
eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),dnl
ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),dnl
udp(src=0,dst=6081,csum=0x0),geneve(vni=0x7b)),out_port(100)),dnl
hash(l4(0)),recirc(0x1)
])

dnl Now check that the packet is actually encapsulated and delivered.
AT_CHECK([ovs-vsctl -- set Interface p0 options:tx_pcap=p0.pcap])

packet=50540000000a5054000000091234
eth=f8bc124434b6aa55aa5500000800
ip4=450000320000400040113406010102580101025c
dnl Source port is based on a packet hash, so it may differ depending on the
dnl compiler flags and CPU type.  Masked with '....'.
udp=....17c1001e0000
geneve=0000655800007b00
encap=${eth}${ip4}${udp}${geneve}
dnl Output to tunnel from a int-br internal port.
dnl Checking that the packet arrived and it was correctly encapsulated.
AT_CHECK([ovs-appctl netdev-dummy/receive int-br "${packet}"])
OVS_WAIT_UNTIL([test $(ovs-pcap p0.pcap | grep -c "${encap}${packet}") -eq 1])

dnl Sending again to exercise the non-miss upcall path.
AT_CHECK([ovs-appctl netdev-dummy/receive int-br "${packet}"])
OVS_WAIT_UNTIL([test $(ovs-pcap p0.pcap | grep -c "${encap}${packet}") -eq 2])

dnl Finally, checking that the datapath flow is also correct.
AT_CHECK([ovs-appctl dpctl/dump-flows | grep tnl_push \
            | strip_ufid | strip_used], [0], [dnl
recirc_id(0),in_port(2),packet_type(ns=0,id=0),dnl
eth(src=50:54:00:00:00:09,dst=50:54:00:00:00:0a),eth_type(0x1234), dnl
packets:1, bytes:14, used:0.0s, dnl
actions:tnl_push(tnl_port(6081),header(size=50,type=5,dnl
eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800),dnl
ipv4(src=1.1.2.88,dst=1.1.2.92,proto=17,tos=0,ttl=64,frag=0x4000),dnl
udp(src=0,dst=6081,csum=0x0),geneve(vni=0x7b)),out_port(100)),dnl
hash(l4(0)),recirc(0x2)
])

OVS_VSWITCHD_STOP
AT_CLEANUP

AT_SETUP([tunnel_push_pop - Mirror over tunnels])
OVS_VSWITCHD_START([dnl
    add-br br-ext -- set bridge br-ext datapath_type=dummy \
        other-config:hwaddr=aa:55:aa:55:00:00 \
    -- add-port br0 t1 -- set Interface t1 type=geneve \
        options:remote_ip=1.1.1.1 \
    -- add-port br0 t2 -- set Interface t2 type=erspan \
        options:remote_ip=1.1.1.2 options:key=flow options:erspan_ver=1 \
        options:erspan_idx=flow \
    -- add-port br0 p0 -- set Interface p0 type=dummy \
    -- add-port br0 p1 -- set Interface p1 type=dummy \
    -- add-port br-ext p-ext -- set Interface p-ext type=dummy \
        options:pcap=ext.pcap])

dnl Configure mirroring over the UDP and ERSPAN tunnels.
AT_CHECK([dnl
    ovs-vsctl \
        set Bridge br0 mirrors=@m1,@m2 -- \
        --id=@t1 get Port t1 -- \
        --id=@t2 get Port t2 -- \
        --id=@m1 create Mirror name=vxlan select_all=true output_port=@t1 -- \
        --id=@m2 create Mirror name=erspan select_all=true output_port=@t2],
    [0], [stdout])

AT_CHECK([ovs-ofctl add-flow br-ext actions=normal])
AT_CHECK([ovs-ofctl add-flow br0 actions=normal])
AT_CHECK([ovs-appctl revalidator/wait])

dnl Make sure ephemeral ports stay static across tests.
AT_CHECK([ovs-appctl tnl/egress_port_range 35190 35190], [0], [OK
])

dnl Setup an IP address for the local side of the tunnel.
AT_CHECK([ovs-appctl netdev-dummy/ip4addr br-ext 1.1.1.3/24], [0], [OK
])

dnl Send two arp replies to populate arp table with tunnel remote endpoints.
AT_CHECK([ovs-appctl netdev-dummy/receive p-ext dnl
 'eth(src=f8:bc:12:44:34:b6,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
  arp(sip=1.1.1.1,tip=1.1.1.3,op=2,sha=f8:bc:12:44:34:b6,tha=00:00:00:00:00:00)'
])
AT_CHECK([ovs-appctl netdev-dummy/receive p-ext dnl
 'eth(src=f8:bc:12:44:34:b3,dst=ff:ff:ff:ff:ff:ff),eth_type(0x0806),dnl
  arp(sip=1.1.1.2,tip=1.1.1.3,op=2,sha=f8:bc:12:44:34:b3,tha=00:00:00:00:00:00)'
])

m4_define([FLOW], [m4_join([,],
  [in_port(p1)],
  [eth(src=50:54:00:00:00:05,dst=50:54:00:00:00:07),eth_type(0x0800)],
  [ipv4(src=192.168.0.1,dst=192.168.0.2,proto=1,tos=0,ttl=128,frag=no)],
  [icmp(type=8,code=0)])])

m4_define([ERSPAN_ACT], [m4_join([,],
  [clone(tnl_push(tnl_port(erspan_sys)],
           [header(size=50,type=107],
                  [eth(dst=f8:bc:12:44:34:b3,src=aa:55:aa:55:00:00,dl_type=0x0800)],
                  [ipv4(src=1.1.1.3,dst=1.1.1.2,proto=47,tos=0,ttl=64,frag=0x4000)],
                  [erspan(ver=1,sid=0x0,idx=0x0))],
           [out_port(br-ext))],
         [p-ext)])])

m4_define([GENEVE_ACT], [m4_join([,],
  [clone(tnl_push(tnl_port(genev_sys_6081)],
           [header(size=50,type=5],
                   [eth(dst=f8:bc:12:44:34:b6,src=aa:55:aa:55:00:00,dl_type=0x0800)],
                   [ipv4(src=1.1.1.3,dst=1.1.1.1,proto=17,tos=0,ttl=64,frag=0x4000)],
                   [udp(src=0,dst=6081,csum=0x0)],
                   [geneve(vni=0x0))],
           [out_port(br-ext))],
         [p-ext)])])

dnl Verify packet is mirrored to both tunnels.  Tunnel actions may happen
dnl in any order.
AT_CHECK([ovs-appctl ofproto/trace --names ovs-dummy "FLOW"], [0], [stdout])
AT_CHECK([grep -q "ERSPAN_ACT" stdout])
AT_CHECK([grep -q "GENEVE_ACT" stdout])

OVS_VSWITCHD_STOP
AT_CLEANUP
