xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-013-off-mesh-route-traffic.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*cfb92d14SAndroid Build Coastguard Worker#
3*cfb92d14SAndroid Build Coastguard Worker#  Copyright (c) 2018, 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 Workerimport time
30*cfb92d14SAndroid Build Coastguard Workerimport wpan
31*cfb92d14SAndroid Build Coastguard Workerfrom wpan import verify
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
34*cfb92d14SAndroid Build Coastguard Worker# Test description: Adding off-mesh routes (on routers and FEDs) and traffic flow to off-mesh addresses.
35*cfb92d14SAndroid Build Coastguard Worker#
36*cfb92d14SAndroid Build Coastguard Worker# Test topology:
37*cfb92d14SAndroid Build Coastguard Worker#
38*cfb92d14SAndroid Build Coastguard Worker#     r1 ---- r2
39*cfb92d14SAndroid Build Coastguard Worker#     |       |
40*cfb92d14SAndroid Build Coastguard Worker#     |       |
41*cfb92d14SAndroid Build Coastguard Worker#    fed1    sed2
42*cfb92d14SAndroid Build Coastguard Worker#
43*cfb92d14SAndroid Build Coastguard Worker# The off-mesh-routes are added as follows:
44*cfb92d14SAndroid Build Coastguard Worker# - `r1`   adds `OFF_MESH_ROUTE_1`,
45*cfb92d14SAndroid Build Coastguard Worker# - `r2`   adds `OFF_MESH_ROUTE_2`,
46*cfb92d14SAndroid Build Coastguard Worker# - `fed1` adds `OFF_MESH_ROUTE_3`.
47*cfb92d14SAndroid Build Coastguard Worker#
48*cfb92d14SAndroid Build Coastguard Worker# Traffic flow:
49*cfb92d14SAndroid Build Coastguard Worker# - From `sed2` to an address matching `OFF_MESH_ROUTE_1` (verify it is received on `r1`),
50*cfb92d14SAndroid Build Coastguard Worker# - From `r1`   to an address matching `OFF_MESH_ROUTE_2` (verify it is received on `r2`),
51*cfb92d14SAndroid Build Coastguard Worker# - From `r2`   to an address matching `OFF_MESH_ROUTE_3` (verify it is received on `fed1`)
52*cfb92d14SAndroid Build Coastguard Worker#
53*cfb92d14SAndroid Build Coastguard Worker
54*cfb92d14SAndroid Build Coastguard Workertest_name = __file__[:-3] if __file__.endswith('.py') else __file__
55*cfb92d14SAndroid Build Coastguard Workerprint('-' * 120)
56*cfb92d14SAndroid Build Coastguard Workerprint('Starting \'{}\''.format(test_name))
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
59*cfb92d14SAndroid Build Coastguard Worker# Creating `wpan.Nodes` instances
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Workerspeedup = 4
62*cfb92d14SAndroid Build Coastguard Workerwpan.Node.set_time_speedup_factor(speedup)
63*cfb92d14SAndroid Build Coastguard Worker
64*cfb92d14SAndroid Build Coastguard Workerr1 = wpan.Node()
65*cfb92d14SAndroid Build Coastguard Workerfed1 = wpan.Node()
66*cfb92d14SAndroid Build Coastguard Workerr2 = wpan.Node()
67*cfb92d14SAndroid Build Coastguard Workersed2 = wpan.Node()
68*cfb92d14SAndroid Build Coastguard Worker
69*cfb92d14SAndroid Build Coastguard Workerall_nodes = [r1, fed1, r2, sed2]
70*cfb92d14SAndroid Build Coastguard Worker
71*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
72*cfb92d14SAndroid Build Coastguard Worker# Init all nodes
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Workerwpan.Node.init_all_nodes()
75*cfb92d14SAndroid Build Coastguard Workerfor node in all_nodes:
76*cfb92d14SAndroid Build Coastguard Worker    # Disable `AutoUpdateInterfaceAddrsOnNCP` feature on wpantund
77*cfb92d14SAndroid Build Coastguard Worker    # for all nodes. This ensures that added IPv6 address (on linux
78*cfb92d14SAndroid Build Coastguard Worker    # interface) are not pushed to NCP (and therefore are not
79*cfb92d14SAndroid Build Coastguard Worker    # on-mesh).
80*cfb92d14SAndroid Build Coastguard Worker    node.set("Daemon:IPv6:AutoUpdateInterfaceAddrsOnNCP", '0')
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
83*cfb92d14SAndroid Build Coastguard Worker# Build network topology
84*cfb92d14SAndroid Build Coastguard Worker#
85*cfb92d14SAndroid Build Coastguard Worker#   r1 ---- r2
86*cfb92d14SAndroid Build Coastguard Worker#   |       |
87*cfb92d14SAndroid Build Coastguard Worker#   |       |
88*cfb92d14SAndroid Build Coastguard Worker#  fed1    sed2
89*cfb92d14SAndroid Build Coastguard Worker
90*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(r2)
91*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(r1)
92*cfb92d14SAndroid Build Coastguard Worker
93*cfb92d14SAndroid Build Coastguard Workerr1.allowlist_node(fed1)
94*cfb92d14SAndroid Build Coastguard Workerfed1.allowlist_node(r1)
95*cfb92d14SAndroid Build Coastguard Worker
96*cfb92d14SAndroid Build Coastguard Workerr2.allowlist_node(sed2)
97*cfb92d14SAndroid Build Coastguard Workersed2.allowlist_node(r2)
98*cfb92d14SAndroid Build Coastguard Worker
99*cfb92d14SAndroid Build Coastguard Workerr1.form("off-mesh")
100*cfb92d14SAndroid Build Coastguard Workerr2.join_node(r1, wpan.JOIN_TYPE_ROUTER)
101*cfb92d14SAndroid Build Coastguard Worker
102*cfb92d14SAndroid Build Coastguard Workerfed1.join_node(r1, wpan.JOIN_TYPE_END_DEVICE)
103*cfb92d14SAndroid Build Coastguard Workersed2.join_node(r2, wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
104*cfb92d14SAndroid Build Coastguard Worker
105*cfb92d14SAndroid Build Coastguard Workersed2.set(wpan.WPAN_POLL_INTERVAL, '1500')
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
108*cfb92d14SAndroid Build Coastguard Worker# Test implementation
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard WorkerWAIT_TIME = 10
111*cfb92d14SAndroid Build Coastguard WorkerNUM_ROUTES = 3
112*cfb92d14SAndroid Build Coastguard WorkerNUM_ROUTES_LOCAL = 1
113*cfb92d14SAndroid Build Coastguard WorkerON_MESH_PREFIX = "fd00:1234::"
114*cfb92d14SAndroid Build Coastguard WorkerOFF_MESH_ROUTE_1 = "fd00:abba::"
115*cfb92d14SAndroid Build Coastguard WorkerOFF_MESH_ROUTE_2 = "fd00:cafe::"
116*cfb92d14SAndroid Build Coastguard WorkerOFF_MESH_ROUTE_3 = "fd00:baba::"
117*cfb92d14SAndroid Build Coastguard WorkerOFF_MESH_ADDR_1 = OFF_MESH_ROUTE_1 + "1"
118*cfb92d14SAndroid Build Coastguard WorkerOFF_MESH_ADDR_2 = OFF_MESH_ROUTE_2 + "2"
119*cfb92d14SAndroid Build Coastguard WorkerOFF_MESH_ADDR_3 = OFF_MESH_ROUTE_3 + "3"
120*cfb92d14SAndroid Build Coastguard Worker
121*cfb92d14SAndroid Build Coastguard Worker# Add on-mesh prefix
122*cfb92d14SAndroid Build Coastguard Workerr1.config_gateway(ON_MESH_PREFIX)
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Worker# The off-mesh-routes are added as follows:
125*cfb92d14SAndroid Build Coastguard Worker# - `r1` adds OFF_MESH_ROUTE_1,
126*cfb92d14SAndroid Build Coastguard Worker# - `r2` adds OFF_MESH_ROUTE_2,
127*cfb92d14SAndroid Build Coastguard Worker# - `fed1` adds OFF_MESH_ROUTE_3.
128*cfb92d14SAndroid Build Coastguard Worker
129*cfb92d14SAndroid Build Coastguard Workerr1.add_route(OFF_MESH_ROUTE_1)
130*cfb92d14SAndroid Build Coastguard Workerr1.add_ip6_address_on_interface(OFF_MESH_ADDR_1)
131*cfb92d14SAndroid Build Coastguard Worker
132*cfb92d14SAndroid Build Coastguard Workerr2.add_route(OFF_MESH_ROUTE_2)
133*cfb92d14SAndroid Build Coastguard Workerr2.add_ip6_address_on_interface(OFF_MESH_ADDR_2)
134*cfb92d14SAndroid Build Coastguard Worker
135*cfb92d14SAndroid Build Coastguard Workerfed1.add_route(OFF_MESH_ROUTE_3)
136*cfb92d14SAndroid Build Coastguard Workerfed1.add_ip6_address_on_interface(OFF_MESH_ADDR_3)
137*cfb92d14SAndroid Build Coastguard Worker
138*cfb92d14SAndroid Build Coastguard Worker# Wait till network data is updated on r1, r2, and sed2 and they all see all
139*cfb92d14SAndroid Build Coastguard Worker# the added off-mesh routes.
140*cfb92d14SAndroid Build Coastguard Worker
141*cfb92d14SAndroid Build Coastguard Worker
142*cfb92d14SAndroid Build Coastguard Workerdef check_off_mesh_routes():
143*cfb92d14SAndroid Build Coastguard Worker    # If a node itself adds a route, the route entry will be seen twice in
144*cfb92d14SAndroid Build Coastguard Worker    # its WPAN_THREAD_OFF_MESH_ROUTES list (one time as part of network-wide
145*cfb92d14SAndroid Build Coastguard Worker    # network data and again as part of the local network data). Note that
146*cfb92d14SAndroid Build Coastguard Worker    # `r1 and `r2` each add a route, while `sed2` does not.
147*cfb92d14SAndroid Build Coastguard Worker    verify(len(wpan.parse_list(r1.get(wpan.WPAN_THREAD_OFF_MESH_ROUTES))) == NUM_ROUTES + NUM_ROUTES_LOCAL)
148*cfb92d14SAndroid Build Coastguard Worker    verify(len(wpan.parse_list(r2.get(wpan.WPAN_THREAD_OFF_MESH_ROUTES))) == NUM_ROUTES + NUM_ROUTES_LOCAL)
149*cfb92d14SAndroid Build Coastguard Worker    verify(len(wpan.parse_list(sed2.get(wpan.WPAN_THREAD_OFF_MESH_ROUTES))) == NUM_ROUTES)
150*cfb92d14SAndroid Build Coastguard Worker
151*cfb92d14SAndroid Build Coastguard Worker
152*cfb92d14SAndroid Build Coastguard Workerwpan.verify_within(check_off_mesh_routes, WAIT_TIME)
153*cfb92d14SAndroid Build Coastguard Worker
154*cfb92d14SAndroid Build Coastguard Worker# Traffic from `sed2` to `OFF_MESH_ADDR_1` (verify that it is received on
155*cfb92d14SAndroid Build Coastguard Worker# `r1`).
156*cfb92d14SAndroid Build Coastguard Worker
157*cfb92d14SAndroid Build Coastguard Workersrc = sed2.find_ip6_address_with_prefix(ON_MESH_PREFIX)
158*cfb92d14SAndroid Build Coastguard Workersender = sed2.prepare_tx(src, OFF_MESH_ADDR_1, "Hello Route1")
159*cfb92d14SAndroid Build Coastguard Workerrecver = r1.prepare_rx(sender)
160*cfb92d14SAndroid Build Coastguard Workerwpan.Node.perform_async_tx_rx()
161*cfb92d14SAndroid Build Coastguard Workerverify(sender.was_successful)
162*cfb92d14SAndroid Build Coastguard Workerverify(recver.was_successful)
163*cfb92d14SAndroid Build Coastguard Worker
164*cfb92d14SAndroid Build Coastguard Worker# Traffic from `r1` to `OFF_MESH_ADDR_2` (verify that it is received on `r2`),
165*cfb92d14SAndroid Build Coastguard Worker
166*cfb92d14SAndroid Build Coastguard Workersrc = r1.find_ip6_address_with_prefix(ON_MESH_PREFIX)
167*cfb92d14SAndroid Build Coastguard Workersender = r1.prepare_tx(src, OFF_MESH_ADDR_2, "Hello Route2")
168*cfb92d14SAndroid Build Coastguard Workerrecver = r2.prepare_rx(sender)
169*cfb92d14SAndroid Build Coastguard Workerwpan.Node.perform_async_tx_rx()
170*cfb92d14SAndroid Build Coastguard Workerverify(sender.was_successful)
171*cfb92d14SAndroid Build Coastguard Workerverify(recver.was_successful)
172*cfb92d14SAndroid Build Coastguard Worker
173*cfb92d14SAndroid Build Coastguard Worker# Traffic from `r2` to `OFF_MESH_ADDR_3` (verify that it is received on `fed1`)
174*cfb92d14SAndroid Build Coastguard Worker
175*cfb92d14SAndroid Build Coastguard Workersrc = r2.find_ip6_address_with_prefix(ON_MESH_PREFIX)
176*cfb92d14SAndroid Build Coastguard Workersender = r2.prepare_tx(src, OFF_MESH_ADDR_3, "Hello Route3")
177*cfb92d14SAndroid Build Coastguard Workerrecver = fed1.prepare_rx(sender)
178*cfb92d14SAndroid Build Coastguard Workerwpan.Node.perform_async_tx_rx()
179*cfb92d14SAndroid Build Coastguard Workerverify(sender.was_successful)
180*cfb92d14SAndroid Build Coastguard Workerverify(recver.was_successful)
181*cfb92d14SAndroid Build Coastguard Worker
182*cfb92d14SAndroid Build Coastguard Worker# -----------------------------------------------------------------------------------------------------------------------
183*cfb92d14SAndroid Build Coastguard Worker# Test finished
184*cfb92d14SAndroid Build Coastguard Worker
185*cfb92d14SAndroid Build Coastguard Workerwpan.Node.finalize_all_nodes()
186*cfb92d14SAndroid Build Coastguard Worker
187*cfb92d14SAndroid Build Coastguard Workerprint('\'{}\' passed.'.format(test_name))
188