1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*cfb92d14SAndroid Build Coastguard Worker# 3*cfb92d14SAndroid Build Coastguard Worker# Copyright (c) 2022, The OpenThread Authors. 4*cfb92d14SAndroid Build Coastguard Worker# All rights reserved. 5*cfb92d14SAndroid Build Coastguard Worker# 6*cfb92d14SAndroid Build Coastguard Worker# Redistribution and use in source and binary forms, with or without 7*cfb92d14SAndroid Build Coastguard Worker# modification, are permitted provided that the following conditions are met: 8*cfb92d14SAndroid Build Coastguard Worker# 1. Redistributions of source code must retain the above copyright 9*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer. 10*cfb92d14SAndroid Build Coastguard Worker# 2. Redistributions in binary form must reproduce the above copyright 11*cfb92d14SAndroid Build Coastguard Worker# notice, this list of conditions and the following disclaimer in the 12*cfb92d14SAndroid Build Coastguard Worker# documentation and/or other materials provided with the distribution. 13*cfb92d14SAndroid Build Coastguard Worker# 3. Neither the name of the copyright holder nor the 14*cfb92d14SAndroid Build Coastguard Worker# names of its contributors may be used to endorse or promote products 15*cfb92d14SAndroid Build Coastguard Worker# derived from this software without specific prior written permission. 16*cfb92d14SAndroid Build Coastguard Worker# 17*cfb92d14SAndroid Build Coastguard Worker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18*cfb92d14SAndroid Build Coastguard Worker# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*cfb92d14SAndroid Build Coastguard Worker# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*cfb92d14SAndroid Build Coastguard Worker# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21*cfb92d14SAndroid Build Coastguard Worker# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22*cfb92d14SAndroid Build Coastguard Worker# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23*cfb92d14SAndroid Build Coastguard Worker# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*cfb92d14SAndroid Build Coastguard Worker# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25*cfb92d14SAndroid Build Coastguard Worker# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26*cfb92d14SAndroid Build Coastguard Worker# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27*cfb92d14SAndroid Build Coastguard Worker# POSSIBILITY OF SUCH DAMAGE. 28*cfb92d14SAndroid Build Coastguard Worker 29*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify 30*cfb92d14SAndroid Build Coastguard Workerfrom cli import verify_within 31*cfb92d14SAndroid Build Coastguard Workerimport cli 32*cfb92d14SAndroid Build Coastguard Workerimport time 33*cfb92d14SAndroid Build Coastguard Worker 34*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 35*cfb92d14SAndroid Build Coastguard Worker# Test description: Adding off-mesh routes (on routers and FEDs) and traffic flow to off-mesh addresses. 36*cfb92d14SAndroid Build Coastguard Worker# 37*cfb92d14SAndroid Build Coastguard Worker# Test topology: 38*cfb92d14SAndroid Build Coastguard Worker# 39*cfb92d14SAndroid Build Coastguard Worker# r1---- r2 ---- r3 ---- r4 40*cfb92d14SAndroid Build Coastguard Worker# | | 41*cfb92d14SAndroid Build Coastguard Worker# | | 42*cfb92d14SAndroid Build Coastguard Worker# fed1 med3 43*cfb92d14SAndroid Build Coastguard Worker# 44*cfb92d14SAndroid Build Coastguard Worker# The off-mesh-routes are added as follows: 45*cfb92d14SAndroid Build Coastguard Worker# - `r1` adds fd00:1:1::/48 high 46*cfb92d14SAndroid Build Coastguard Worker# - `r2` adds fd00:1:1:2::64 med 47*cfb92d14SAndroid Build Coastguard Worker# - `r3` adds fd00:3::/64 med & fed00:4::/32 med 48*cfb92d14SAndroid Build Coastguard Worker# - 'r4' adds fd00:1:1::/48 low 49*cfb92d14SAndroid Build Coastguard Worker# - `fed1` adds fd00:4::/32 med 50*cfb92d14SAndroid Build Coastguard Worker# 51*cfb92d14SAndroid Build Coastguard Worker 52*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__ 53*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120) 54*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name)) 55*cfb92d14SAndroid Build Coastguard Worker 56*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 57*cfb92d14SAndroid Build Coastguard Worker# Creating `cli.Nodes` instances 58*cfb92d14SAndroid Build Coastguard Worker 59*cfb92d14SAndroid Build Coastguard Workerspeedup = 10 60*cfb92d14SAndroid Build Coastguard Workercli.Node.set_time_speedup_factor(speedup) 61*cfb92d14SAndroid Build Coastguard Worker 62*cfb92d14SAndroid Build Coastguard Workerr1 = cli.Node() 63*cfb92d14SAndroid Build Coastguard Workerr2 = cli.Node() 64*cfb92d14SAndroid Build Coastguard Workerr3 = cli.Node() 65*cfb92d14SAndroid Build Coastguard Workerr4 = cli.Node() 66*cfb92d14SAndroid Build Coastguard Workerfed1 = cli.Node() 67*cfb92d14SAndroid Build Coastguard Workermed3 = cli.Node() 68*cfb92d14SAndroid Build Coastguard Worker 69*cfb92d14SAndroid Build Coastguard Workernodes = [r1, r2, r3, r4, fed1, med3] 70*cfb92d14SAndroid Build Coastguard Worker 71*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 72*cfb92d14SAndroid Build Coastguard Worker# Form topology 73*cfb92d14SAndroid Build Coastguard Worker 74*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2) 75*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(fed1) 76*cfb92d14SAndroid Build Coastguard Worker 77*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1) 78*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r3) 79*cfb92d14SAndroid Build Coastguard Worker 80*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r2) 81*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(r4) 82*cfb92d14SAndroid Build Coastguard Workerr3.allowlist_node(med3) 83*cfb92d14SAndroid Build Coastguard Worker 84*cfb92d14SAndroid Build Coastguard Workerr4.allowlist_node(r3) 85*cfb92d14SAndroid Build Coastguard Worker 86*cfb92d14SAndroid Build Coastguard Workerfed1.allowlist_node(r1) 87*cfb92d14SAndroid Build Coastguard Workermed3.allowlist_node(r3) 88*cfb92d14SAndroid Build Coastguard Worker 89*cfb92d14SAndroid Build Coastguard Workerr1.form("off-mesh") 90*cfb92d14SAndroid Build Coastguard Workerr2.join(r1) 91*cfb92d14SAndroid Build Coastguard Workerr3.join(r2) 92*cfb92d14SAndroid Build Coastguard Workerr4.join(r3) 93*cfb92d14SAndroid Build Coastguard Workerfed1.join(r1, cli.JOIN_TYPE_REED) 94*cfb92d14SAndroid Build Coastguard Workermed3.join(r3, cli.JOIN_TYPE_END_DEVICE) 95*cfb92d14SAndroid Build Coastguard Worker 96*cfb92d14SAndroid Build Coastguard Workerverify(r1.get_state() == 'leader') 97*cfb92d14SAndroid Build Coastguard Workerverify(r2.get_state() == 'router') 98*cfb92d14SAndroid Build Coastguard Workerverify(r3.get_state() == 'router') 99*cfb92d14SAndroid Build Coastguard Workerverify(r4.get_state() == 'router') 100*cfb92d14SAndroid Build Coastguard Workerverify(fed1.get_state() == 'child') 101*cfb92d14SAndroid Build Coastguard Workerverify(med3.get_state() == 'child') 102*cfb92d14SAndroid Build Coastguard Worker 103*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 104*cfb92d14SAndroid Build Coastguard Worker# Test Implementation 105*cfb92d14SAndroid Build Coastguard Worker 106*cfb92d14SAndroid Build Coastguard Worker# Wait till first router has either established a link or 107*cfb92d14SAndroid Build Coastguard Worker# has a valid "next hop" towards all other routers. 108*cfb92d14SAndroid Build Coastguard Worker 109*cfb92d14SAndroid Build Coastguard Workerr1_rloc16 = int(r1.get_rloc16(), 16) 110*cfb92d14SAndroid Build Coastguard Worker 111*cfb92d14SAndroid Build Coastguard Worker 112*cfb92d14SAndroid Build Coastguard Workerdef check_r1_router_table(): 113*cfb92d14SAndroid Build Coastguard Worker table = r1.get_router_table() 114*cfb92d14SAndroid Build Coastguard Worker verify(len(table) == 4) 115*cfb92d14SAndroid Build Coastguard Worker for entry in table: 116*cfb92d14SAndroid Build Coastguard Worker verify(int(entry['RLOC16'], 0) == r1_rloc16 or int(entry['Link']) == 1 or int(entry['Next Hop']) != 63) 117*cfb92d14SAndroid Build Coastguard Worker 118*cfb92d14SAndroid Build Coastguard Worker 119*cfb92d14SAndroid Build Coastguard Workerverify_within(check_r1_router_table, 120) 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard Worker# Add an on-mesh prefix on r1 and different off-mesh routes 122*cfb92d14SAndroid Build Coastguard Worker# on different nodes and make sure they are seen on all nodes 123*cfb92d14SAndroid Build Coastguard Worker 124*cfb92d14SAndroid Build Coastguard Workerr1.add_prefix('fd00:abba::/64', 'paros', 'med') 125*cfb92d14SAndroid Build Coastguard Workerr1.add_route('fd00:1:1::/48', 's', 'high') 126*cfb92d14SAndroid Build Coastguard Workerr1.register_netdata() 127*cfb92d14SAndroid Build Coastguard Worker 128*cfb92d14SAndroid Build Coastguard Workerr2.add_route('fd00:1:1:2::/64', 's', 'med') 129*cfb92d14SAndroid Build Coastguard Workerr2.register_netdata() 130*cfb92d14SAndroid Build Coastguard Worker 131*cfb92d14SAndroid Build Coastguard Workerr3.add_route('fd00:3::/64', 's', 'med') 132*cfb92d14SAndroid Build Coastguard Workerr3.add_route('fd00:4::/32', 'med') 133*cfb92d14SAndroid Build Coastguard Workerr3.register_netdata() 134*cfb92d14SAndroid Build Coastguard Worker 135*cfb92d14SAndroid Build Coastguard Workerr4.add_route('fd00:1:1::/48', 's', 'med') 136*cfb92d14SAndroid Build Coastguard Worker 137*cfb92d14SAndroid Build Coastguard Workerfed1.add_route('fd00:4::/32', 'med') 138*cfb92d14SAndroid Build Coastguard Workerfed1.register_netdata() 139*cfb92d14SAndroid Build Coastguard Worker 140*cfb92d14SAndroid Build Coastguard Worker 141*cfb92d14SAndroid Build Coastguard Workerdef check_netdata_on_all_nodes(): 142*cfb92d14SAndroid Build Coastguard Worker for node in nodes: 143*cfb92d14SAndroid Build Coastguard Worker netdata = node.get_netdata() 144*cfb92d14SAndroid Build Coastguard Worker verify(len(netdata['prefixes']) == 1) 145*cfb92d14SAndroid Build Coastguard Worker verify(len(netdata['routes']) == 6) 146*cfb92d14SAndroid Build Coastguard Worker 147*cfb92d14SAndroid Build Coastguard Worker 148*cfb92d14SAndroid Build Coastguard Workerverify_within(check_netdata_on_all_nodes, 5) 149*cfb92d14SAndroid Build Coastguard Worker 150*cfb92d14SAndroid Build Coastguard Worker# Send from `med3` to an address matching `fd00:1:1:2::/64` added 151*cfb92d14SAndroid Build Coastguard Worker# by`r2` and verify that it is received on `r2` and not `r1`, since 152*cfb92d14SAndroid Build Coastguard Worker# it is better prefix match with `fd00:1:1:2/64` on r2. 153*cfb92d14SAndroid Build Coastguard Worker 154*cfb92d14SAndroid Build Coastguard Workerr1_counter = r1.get_br_counter_unicast_outbound_packets() 155*cfb92d14SAndroid Build Coastguard Workerr2_counter = r2.get_br_counter_unicast_outbound_packets() 156*cfb92d14SAndroid Build Coastguard Worker 157*cfb92d14SAndroid Build Coastguard Workermed3.ping('fd00:1:1:2::1', verify_success=False) 158*cfb92d14SAndroid Build Coastguard Worker 159*cfb92d14SAndroid Build Coastguard Workerverify(r1_counter == r1.get_br_counter_unicast_outbound_packets()) 160*cfb92d14SAndroid Build Coastguard Workerverify(r2_counter + 1 == r2.get_br_counter_unicast_outbound_packets()) 161*cfb92d14SAndroid Build Coastguard Worker 162*cfb92d14SAndroid Build Coastguard Worker# Send from`r3` to an address matching `fd00:1:1::/48` which is 163*cfb92d14SAndroid Build Coastguard Worker# added by both `r1` and `r4` and verify that it is received on 164*cfb92d14SAndroid Build Coastguard Worker# `r1` since it adds it with higher preference. 165*cfb92d14SAndroid Build Coastguard Worker 166*cfb92d14SAndroid Build Coastguard Workerr1_counter = r1.get_br_counter_unicast_outbound_packets() 167*cfb92d14SAndroid Build Coastguard Workerr4_counter = r4.get_br_counter_unicast_outbound_packets() 168*cfb92d14SAndroid Build Coastguard Worker 169*cfb92d14SAndroid Build Coastguard Workerr3.ping('fd00:1:1::2', count=3, verify_success=False) 170*cfb92d14SAndroid Build Coastguard Worker 171*cfb92d14SAndroid Build Coastguard Workerverify(r1_counter + 3 == r1.get_br_counter_unicast_outbound_packets()) 172*cfb92d14SAndroid Build Coastguard Workerverify(r4_counter == r4.get_br_counter_unicast_outbound_packets()) 173*cfb92d14SAndroid Build Coastguard Worker 174*cfb92d14SAndroid Build Coastguard Worker# TRy the same address from `r4` itself, it should again end up 175*cfb92d14SAndroid Build Coastguard Worker# going to `r1` due to it adding it at high preference. 176*cfb92d14SAndroid Build Coastguard Worker 177*cfb92d14SAndroid Build Coastguard Workerr1_counter = r1.get_br_counter_unicast_outbound_packets() 178*cfb92d14SAndroid Build Coastguard Workerr4_counter = r4.get_br_counter_unicast_outbound_packets() 179*cfb92d14SAndroid Build Coastguard Worker 180*cfb92d14SAndroid Build Coastguard Workerr4.ping('fd00:1:1::2', count=2, verify_success=False) 181*cfb92d14SAndroid Build Coastguard Worker 182*cfb92d14SAndroid Build Coastguard Workerverify(r1_counter + 2 == r1.get_br_counter_unicast_outbound_packets()) 183*cfb92d14SAndroid Build Coastguard Workerverify(r4_counter == r4.get_br_counter_unicast_outbound_packets()) 184*cfb92d14SAndroid Build Coastguard Worker 185*cfb92d14SAndroid Build Coastguard Worker# Send from `fed1` to an address matching `fd00::3::/64` (from `r3`) 186*cfb92d14SAndroid Build Coastguard Worker# and verify that it is received on `r3`. 187*cfb92d14SAndroid Build Coastguard Worker 188*cfb92d14SAndroid Build Coastguard Workerr3_counter = r3.get_br_counter_unicast_outbound_packets() 189*cfb92d14SAndroid Build Coastguard Worker 190*cfb92d14SAndroid Build Coastguard Workerfed1.ping('fd00:3::3', count=2, verify_success=False) 191*cfb92d14SAndroid Build Coastguard Worker 192*cfb92d14SAndroid Build Coastguard Workerverify(r3_counter + 2 == r3.get_br_counter_unicast_outbound_packets()) 193*cfb92d14SAndroid Build Coastguard Worker 194*cfb92d14SAndroid Build Coastguard Worker# Send from `r1` to an address matching `fd00::4::/32` which is added 195*cfb92d14SAndroid Build Coastguard Worker# by both `fed1` and `r3` with same preference. Verify that it is 196*cfb92d14SAndroid Build Coastguard Worker# received on `fed1` since it is closer to `r1` and we would have a 197*cfb92d14SAndroid Build Coastguard Worker# smaller path cost from `r1` to `fed1`. 198*cfb92d14SAndroid Build Coastguard Worker 199*cfb92d14SAndroid Build Coastguard Workerfed1_counter = fed1.get_br_counter_unicast_outbound_packets() 200*cfb92d14SAndroid Build Coastguard Workerr3_counter = r3.get_br_counter_unicast_outbound_packets() 201*cfb92d14SAndroid Build Coastguard Worker 202*cfb92d14SAndroid Build Coastguard Workerr1.ping('fd00:4::4', count=1, verify_success=False) 203*cfb92d14SAndroid Build Coastguard Worker 204*cfb92d14SAndroid Build Coastguard Workerverify(fed1_counter + 1 == fed1.get_br_counter_unicast_outbound_packets()) 205*cfb92d14SAndroid Build Coastguard Workerverify(r3_counter == r3.get_br_counter_unicast_outbound_packets()) 206*cfb92d14SAndroid Build Coastguard Worker 207*cfb92d14SAndroid Build Coastguard Worker# Try the same, but now send from `fed1` and make sure it selects 208*cfb92d14SAndroid Build Coastguard Worker# itself as destination. 209*cfb92d14SAndroid Build Coastguard Worker 210*cfb92d14SAndroid Build Coastguard Workerfed1_counter = fed1.get_br_counter_unicast_outbound_packets() 211*cfb92d14SAndroid Build Coastguard Workerr3_counter = r3.get_br_counter_unicast_outbound_packets() 212*cfb92d14SAndroid Build Coastguard Worker 213*cfb92d14SAndroid Build Coastguard Workerfed1.ping('fd00:4::5', count=2, verify_success=False) 214*cfb92d14SAndroid Build Coastguard Worker 215*cfb92d14SAndroid Build Coastguard Workerverify(fed1_counter + 2 == fed1.get_br_counter_unicast_outbound_packets()) 216*cfb92d14SAndroid Build Coastguard Workerverify(r3_counter == r3.get_br_counter_unicast_outbound_packets()) 217*cfb92d14SAndroid Build Coastguard Worker 218*cfb92d14SAndroid Build Coastguard Worker# Try the same but now send from `r2`. Now the `r3` would be closer 219*cfb92d14SAndroid Build Coastguard Worker# and should be selected as destination. 220*cfb92d14SAndroid Build Coastguard Worker 221*cfb92d14SAndroid Build Coastguard Workerfed1_counter = fed1.get_br_counter_unicast_outbound_packets() 222*cfb92d14SAndroid Build Coastguard Workerr3_counter = r3.get_br_counter_unicast_outbound_packets() 223*cfb92d14SAndroid Build Coastguard Worker 224*cfb92d14SAndroid Build Coastguard Workerr2.ping('fd00:4::6', count=3, verify_success=False) 225*cfb92d14SAndroid Build Coastguard Worker 226*cfb92d14SAndroid Build Coastguard Workerverify(fed1_counter == fed1.get_br_counter_unicast_outbound_packets()) 227*cfb92d14SAndroid Build Coastguard Workerverify(r3_counter + 3 == r3.get_br_counter_unicast_outbound_packets()) 228*cfb92d14SAndroid Build Coastguard Worker 229*cfb92d14SAndroid Build Coastguard Worker# Again try same but send from `med1` and make sure its parent 230*cfb92d14SAndroid Build Coastguard Worker# `r3` receives. 231*cfb92d14SAndroid Build Coastguard Worker 232*cfb92d14SAndroid Build Coastguard Workerfed1_counter = fed1.get_br_counter_unicast_outbound_packets() 233*cfb92d14SAndroid Build Coastguard Workerr3_counter = r3.get_br_counter_unicast_outbound_packets() 234*cfb92d14SAndroid Build Coastguard Worker 235*cfb92d14SAndroid Build Coastguard Workermed3.ping('fd00:4::7', count=1, verify_success=False) 236*cfb92d14SAndroid Build Coastguard Worker 237*cfb92d14SAndroid Build Coastguard Workerverify(fed1_counter == fed1.get_br_counter_unicast_outbound_packets()) 238*cfb92d14SAndroid Build Coastguard Workerverify(r3_counter + 1 == r3.get_br_counter_unicast_outbound_packets()) 239*cfb92d14SAndroid Build Coastguard Worker 240*cfb92d14SAndroid Build Coastguard Worker# ----------------------------------------------------------------------------------------------------------------------- 241*cfb92d14SAndroid Build Coastguard Worker# Test finished 242*cfb92d14SAndroid Build Coastguard Worker 243*cfb92d14SAndroid Build Coastguard Workercli.Node.finalize_all_nodes() 244*cfb92d14SAndroid Build Coastguard Worker 245*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name)) 246