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