xref: /aosp_15_r20/external/openthread/tests/toranj/ncp/test-012-multi-hop-traffic.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1#!/usr/bin/env python3
2#
3#  Copyright (c) 2018, 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
29import wpan
30from wpan import verify
31
32# -----------------------------------------------------------------------------------------------------------------------
33# Test description:
34#
35# Traffic over multi-hop in a network with chain topology
36#
37#       r1 ----- r2 ---- r3 ----- r4
38#       /\       |       |        /\
39#      /  \      |       |       /  \
40#    fed1 sed1  sed2    sed3   sed4 fed4
41#
42#
43# Traffic flow:
44#  - From first router to last router
45#  - From SED child of last router to SED child of first router
46#  - From FED child of first router to FED child of last router
47#
48# The test verifies the following:
49# - Verifies Address Query process to routers and FEDs.
50# - Verifies Mesh Header frame forwarding over multiple routers.
51# - Verifies forwarding of large IPv6 messages (1000 bytes) requiring lowpan fragmentation.
52
53test_name = __file__[:-3] if __file__.endswith('.py') else __file__
54print('-' * 120)
55print('Starting \'{}\''.format(test_name))
56
57# -----------------------------------------------------------------------------------------------------------------------
58# Creating `wpan.Nodes` instances
59
60speedup = 4
61wpan.Node.set_time_speedup_factor(speedup)
62
63NUM_ROUTERS = 4
64
65routers = []
66sed_children = []
67fed_children = []
68
69for index in range(NUM_ROUTERS):
70    routers.append(wpan.Node())
71    sed_children.append(wpan.Node())
72
73fed_children.append(wpan.Node())
74fed_children.append(wpan.Node())
75
76all_nodes = routers + sed_children + fed_children
77
78# -----------------------------------------------------------------------------------------------------------------------
79# Init all nodes
80
81wpan.Node.init_all_nodes()
82
83# -----------------------------------------------------------------------------------------------------------------------
84# Build network topology
85#
86#       r1 ----- r2 ---- r3 ----- r4
87#       /\       |       |        /\
88#      /  \      |       |       /  \
89#    fed1 sed1  sed2    sed3   sed4 fed4
90
91# Allowlist routers with their corresponding sleepy children
92
93for index in range(0, NUM_ROUTERS):
94    routers[index].allowlist_node(sed_children[index])
95    sed_children[index].allowlist_node(routers[index])
96
97# Allowlist a FED child for the first and last routers
98
99routers[0].allowlist_node(fed_children[0])
100fed_children[0].allowlist_node(routers[0])
101
102routers[-1].allowlist_node(fed_children[-1])
103fed_children[-1].allowlist_node(routers[-1])
104
105# While list routers at [index-1] and [index]
106
107for index in range(1, NUM_ROUTERS):
108    routers[index - 1].allowlist_node(routers[index])
109    routers[index].allowlist_node(routers[index - 1])
110
111routers[0].form("multi-hop")
112sed_children[0].join_node(routers[0], wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
113sed_children[0].set(wpan.WPAN_POLL_INTERVAL, '500')
114
115for index in range(1, NUM_ROUTERS):
116    routers[index].join_node(routers[index - 1], wpan.JOIN_TYPE_ROUTER)
117    sed_children[index].join_node(routers[index], wpan.JOIN_TYPE_SLEEPY_END_DEVICE)
118    sed_children[index].set(wpan.WPAN_POLL_INTERVAL, '500')
119
120fed_children[0].join_node(routers[0], wpan.JOIN_TYPE_END_DEVICE)
121fed_children[-1].join_node(routers[-1], wpan.JOIN_TYPE_END_DEVICE)
122
123# -----------------------------------------------------------------------------------------------------------------------
124# Test implementation
125
126NUM_MSGS = 3
127MSG_LENS = [40, 100, 400, 800, 1000]
128
129# Wait till first router has either established a link or
130# has a valid "next hop" towards all other routers.
131
132ROUTER_TABLE_WAIT_TIME = 60 / speedup + 5
133INVALID_ROUTER_ID = 63
134
135r1_rloc = int(routers[0].get(wpan.WPAN_THREAD_RLOC16), 16)
136
137
138def check_r1_router_table():
139    router_table = wpan.parse_router_table_result(routers[0].get(wpan.WPAN_THREAD_ROUTER_TABLE))
140    verify(len(router_table) == NUM_ROUTERS)
141    for entry in router_table:
142        verify(entry.rloc16 == r1_rloc or entry.is_link_established() or entry.next_hop != INVALID_ROUTER_ID)
143
144
145wpan.verify_within(check_r1_router_table, ROUTER_TABLE_WAIT_TIME)
146
147# Send from the first router in the chain to the last one.
148
149src = routers[0].get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
150dst = routers[-1].get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
151
152for msg_length in MSG_LENS:
153    sender = routers[0].prepare_tx(src, dst, msg_length, NUM_MSGS)
154    recver = routers[-1].prepare_rx(sender)
155    wpan.Node.perform_async_tx_rx()
156    verify(sender.was_successful)
157    verify(recver.was_successful)
158
159# Send from the SED child of the last router to the SED child of the first
160# router.
161
162src = sed_children[-1].get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
163dst = sed_children[0].get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
164
165for msg_length in MSG_LENS:
166    sender = sed_children[-1].prepare_tx(src, dst, msg_length, NUM_MSGS)
167    recver = sed_children[0].prepare_rx(sender)
168    wpan.Node.perform_async_tx_rx()
169    verify(sender.was_successful)
170    verify(recver.was_successful)
171
172# Send from the FED child of the first router to the FED child of the last
173# router.
174
175src = fed_children[0].get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
176dst = fed_children[-1].get(wpan.WPAN_IP6_MESH_LOCAL_ADDRESS)[1:-1]
177
178for msg_length in MSG_LENS:
179    sender = fed_children[0].prepare_tx(src, dst, msg_length, NUM_MSGS)
180    recver = fed_children[-1].prepare_rx(sender)
181    wpan.Node.perform_async_tx_rx()
182    verify(sender.was_successful)
183    verify(recver.was_successful)
184
185# -----------------------------------------------------------------------------------------------------------------------
186# Test finished
187
188wpan.Node.finalize_all_nodes()
189
190print('\'{}\' passed.'.format(test_name))
191