xref: /aosp_15_r20/external/openthread/tests/toranj/cli/test-006-traffic-multi-hop.py (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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:
36#
37# Traffic over multi-hop in a network with chain topology
38#
39#       r1 ----- r2 ---- r3 ----- r4
40#       /\                        /\
41#      /  \                      /  \
42#    fed1 sed1                sed4 fed4
43#
44#
45# Traffic flow:
46#  - From first router to last router
47#  - From SED child of last router to SED child of first router
48#  - From FED child of first router to FED child of last router
49#
50# The test verifies the following:
51# - Verifies Address Query process to routers and FEDs.
52# - Verifies Mesh Header frame forwarding over multiple routers.
53# - Verifies forwarding of large IPv6 messages (1000 bytes) requiring lowpan fragmentation.
54
55test_name = __file__[:-3] if __file__.endswith('.py') else __file__
56print('-' * 120)
57print('Starting \'{}\''.format(test_name))
58
59# -----------------------------------------------------------------------------------------------------------------------
60# Creating `cli.Nodes` instances
61
62speedup = 10
63cli.Node.set_time_speedup_factor(speedup)
64
65r1 = cli.Node()
66r2 = cli.Node()
67r3 = cli.Node()
68r4 = cli.Node()
69fed1 = cli.Node()
70sed1 = cli.Node()
71fed4 = cli.Node()
72sed4 = cli.Node()
73
74# -----------------------------------------------------------------------------------------------------------------------
75# Form topology
76
77r1.allowlist_node(r2)
78r1.allowlist_node(fed1)
79r1.allowlist_node(sed1)
80
81r2.allowlist_node(r1)
82r2.allowlist_node(r3)
83
84r3.allowlist_node(r2)
85r3.allowlist_node(r4)
86
87r4.allowlist_node(r3)
88r4.allowlist_node(sed4)
89r4.allowlist_node(fed4)
90
91fed1.allowlist_node(r1)
92sed1.allowlist_node(r1)
93
94fed4.allowlist_node(r4)
95sed4.allowlist_node(r4)
96
97r1.form("pan")
98r2.join(r1)
99r3.join(r2)
100r4.join(r3)
101fed1.join(r1, cli.JOIN_TYPE_REED)
102sed1.join(r1, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
103fed4.join(r4, cli.JOIN_TYPE_REED)
104sed4.join(r4, cli.JOIN_TYPE_SLEEPY_END_DEVICE)
105
106verify(r1.get_state() == 'leader')
107verify(r2.get_state() == 'router')
108verify(r3.get_state() == 'router')
109verify(r4.get_state() == 'router')
110verify(fed1.get_state() == 'child')
111verify(fed4.get_state() == 'child')
112verify(sed1.get_state() == 'child')
113verify(sed4.get_state() == 'child')
114
115sed1.set_pollperiod(200)
116sed4.set_pollperiod(200)
117verify(int(sed1.get_pollperiod()) == 200)
118verify(int(sed4.get_pollperiod()) == 200)
119
120# -----------------------------------------------------------------------------------------------------------------------
121# Test Implementation
122
123# Wait till first router has either established a link or
124# has a valid "next hop" towards all other routers.
125
126r1_rloc16 = int(r1.get_rloc16(), 16)
127
128
129def check_r1_router_table():
130    table = r1.get_router_table()
131    verify(len(table) == 4)
132    for entry in table:
133        verify(int(entry['RLOC16'], 0) == r1_rloc16 or int(entry['Link']) == 1 or int(entry['Next Hop']) != 63)
134
135
136verify_within(check_r1_router_table, 160)
137
138sizes = [0, 80, 500, 1000]
139
140# Traffic from the first router in the chain to the last one.
141
142r4_mleid = r4.get_mleid_ip_addr()
143
144for size in sizes:
145    r1.ping(r4_mleid, size=size)
146
147# Send from the SED child of the last router to the SED child of the first
148# router.
149
150sed1_mleid = sed1.get_mleid_ip_addr()
151
152for size in sizes:
153    sed4.ping(sed1_mleid, size=size)
154
155# Send from the FED child of the first router to the FED child of the last
156# router.
157
158fed4_mleid = fed4.get_mleid_ip_addr()
159
160for size in sizes:
161    fed1.ping(fed4_mleid, size=size)
162
163# -----------------------------------------------------------------------------------------------------------------------
164# Test finished
165
166cli.Node.finalize_all_nodes()
167
168print('\'{}\' passed.'.format(test_name))
169