mirror of
https://github.com/espressif/openthread.git
synced 2026-07-28 22:57:47 +00:00
[tmf] add AnycastLocator module (#6513)
This commit adds a new class `AnycastLocator` which can be used to
locate the closest destination of an anycast IPv6 address (i.e., find
the related mesh local EID and RLOC16). The closest destination is
determined based on the the current routing table and path costs
within the Thread mesh.
The implementation uses a CoAP confirmable post request to a newly
added URI path ("a/yl"). The destination IPv6 address of such as
request message is set to the anycast address to be located. The
receiver of the request message sends a CoAP response which includes
the "Mesh Local EID" and "Thread RLOC16" TLVs.
This commit also adds support this new feature in CLI (adding a new
`locate <anycast-addr>` command).
Finally this commit adds `test_anycast_locator.py` to test behavior of
the new feature.
This commit is contained in:
@@ -152,6 +152,7 @@ EXTRA_DIST = \
|
||||
sniffer.py \
|
||||
sniffer_transport.py \
|
||||
test_anycast.py \
|
||||
test_anycast_locator.py \
|
||||
test_coap.py \
|
||||
test_coap_block.py \
|
||||
test_coap_observe.py \
|
||||
@@ -222,6 +223,7 @@ check_PROGRAMS = \
|
||||
|
||||
check_SCRIPTS = \
|
||||
test_anycast.py \
|
||||
test_anycast_locator.py \
|
||||
test_coap.py \
|
||||
test_coap_block.py \
|
||||
test_coap_observe.py \
|
||||
|
||||
@@ -1082,6 +1082,22 @@ class NodeImpl:
|
||||
values = [key_value[1].strip('"') for key_value in key_values]
|
||||
return dict(zip(keys, values))
|
||||
|
||||
def locate(self, anycast_addr):
|
||||
cmd = 'locate ' + anycast_addr
|
||||
self.send_command(cmd)
|
||||
self.simulator.go(5)
|
||||
return self._parse_locate_result(self._expect_command_output(cmd)[0])
|
||||
|
||||
def _parse_locate_result(self, line: str):
|
||||
"""Parse anycast locate result as list of ml-eid and rloc16.
|
||||
|
||||
Example output for input
|
||||
'fd00:db8:0:0:acf9:9d0:7f3c:b06e 0xa800'
|
||||
|
||||
[ 'fd00:db8:0:0:acf9:9d0:7f3c:b06e', '0xa800' ]
|
||||
"""
|
||||
return line.split(' ')
|
||||
|
||||
def enable_backbone_router(self):
|
||||
cmd = 'bbr enable'
|
||||
self.send_command(cmd)
|
||||
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# Copyright (c) 2021, The OpenThread Authors.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# 3. Neither the name of the copyright holder nor the
|
||||
# names of its contributors may be used to endorse or promote products
|
||||
# derived from this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
|
||||
import config
|
||||
import ipaddress
|
||||
import unittest
|
||||
|
||||
import command
|
||||
import thread_cert
|
||||
|
||||
# Test description:
|
||||
# This test verifies Anycast Locator functionality
|
||||
#
|
||||
# Topology:
|
||||
#
|
||||
# LEADER -- ROUTER1 -- ROUTER2 -- ROUTER3 -- ROUTER4
|
||||
#
|
||||
|
||||
LEADER = 1
|
||||
ROUTER1 = 2
|
||||
ROUTER2 = 3
|
||||
ROUTER3 = 4
|
||||
ROUTER4 = 5
|
||||
|
||||
SRV_ENT_NUMBER = '44970'
|
||||
SRV_SERVICE_DATA = '571234'
|
||||
SRV_SERVER_DATA = '00'
|
||||
|
||||
|
||||
class AnycastLocator(thread_cert.TestCase):
|
||||
USE_MESSAGE_FACTORY = False
|
||||
SUPPORT_NCP = False
|
||||
|
||||
TOPOLOGY = {
|
||||
LEADER: {
|
||||
'name': 'LEADER',
|
||||
'mode': 'rdn',
|
||||
'allowlist': [ROUTER1]
|
||||
},
|
||||
ROUTER1: {
|
||||
'name': 'ROUTER1',
|
||||
'mode': 'rdn',
|
||||
'allowlist': [LEADER, ROUTER2]
|
||||
},
|
||||
ROUTER2: {
|
||||
'name': 'ROUTER2',
|
||||
'mode': 'rdn',
|
||||
'allowlist': [ROUTER1, ROUTER3]
|
||||
},
|
||||
ROUTER3: {
|
||||
'name': 'ROUTER3',
|
||||
'mode': 'rdn',
|
||||
'allowlist': [ROUTER2, ROUTER4]
|
||||
},
|
||||
ROUTER4: {
|
||||
'name': 'ROUTER4',
|
||||
'mode': 'rdn',
|
||||
'allowlist': [ROUTER3]
|
||||
},
|
||||
}
|
||||
|
||||
def test(self):
|
||||
leader = self.nodes[LEADER]
|
||||
router1 = self.nodes[ROUTER1]
|
||||
router2 = self.nodes[ROUTER2]
|
||||
router3 = self.nodes[ROUTER3]
|
||||
router4 = self.nodes[ROUTER4]
|
||||
nodes = [leader, router1, router2, router3, router4]
|
||||
|
||||
#
|
||||
# 0. Start the leader and routers
|
||||
#
|
||||
|
||||
leader.start()
|
||||
self.simulator.go(5)
|
||||
self.assertEqual(leader.get_state(), 'leader')
|
||||
|
||||
for node in nodes[1:]:
|
||||
node.start()
|
||||
self.simulator.go(5)
|
||||
self.assertEqual(node.get_state(), 'router')
|
||||
|
||||
#
|
||||
# 1. Locate leader ALOC from all nodes
|
||||
#
|
||||
|
||||
leader_aloc = leader.get_addr_leader_aloc()
|
||||
|
||||
for node in nodes:
|
||||
result = node.locate(leader_aloc)
|
||||
self.assertEqual(result[0], leader.get_mleid())
|
||||
self.assertEqual(int(result[1], 0), leader.get_addr16())
|
||||
|
||||
#
|
||||
# 2. Add a service on router4 and locate its ALOC
|
||||
#
|
||||
|
||||
router4.add_service(SRV_ENT_NUMBER, SRV_SERVICE_DATA, SRV_SERVER_DATA)
|
||||
router4.register_netdata()
|
||||
self.simulator.go(5)
|
||||
services = leader.get_services()
|
||||
self.assertEqual(len(services), 1)
|
||||
|
||||
service_aloc = router4.get_ip6_address(config.ADDRESS_TYPE.ALOC)[0]
|
||||
|
||||
for node in nodes:
|
||||
result = node.locate(service_aloc)
|
||||
self.assertEqual(result[0], router4.get_mleid())
|
||||
self.assertEqual(int(result[1], 0), router4.get_addr16())
|
||||
|
||||
#
|
||||
# 3. Add same service on leader and ensure we locate the closest ALOC destination
|
||||
#
|
||||
|
||||
leader.add_service(SRV_ENT_NUMBER, SRV_SERVICE_DATA, SRV_SERVER_DATA)
|
||||
leader.register_netdata()
|
||||
self.simulator.go(5)
|
||||
services = leader.get_services()
|
||||
self.assertEqual(len(services), 2)
|
||||
|
||||
# leader and router1 should locate leader as closest service ALOC,
|
||||
# router3 and router4 should locate router4. router2 is in middle
|
||||
# and can locate either one.
|
||||
for node in [leader, router1]:
|
||||
result = node.locate(service_aloc)
|
||||
self.assertEqual(result[0], leader.get_mleid())
|
||||
self.assertEqual(int(result[1], 0), leader.get_addr16())
|
||||
|
||||
for node in [router3, router4]:
|
||||
result = node.locate(service_aloc)
|
||||
self.assertEqual(result[0], router4.get_mleid())
|
||||
self.assertEqual(int(result[1], 0), router4.get_addr16())
|
||||
|
||||
result = router2.locate(service_aloc)
|
||||
self.assertTrue(result[0] in [leader.get_mleid(), router4.get_mleid()])
|
||||
self.assertTrue(int(result[1], 0) in [leader.get_addr16(), router4.get_addr16()])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user