mirror of
https://github.com/espressif/openthread.git
synced 2026-08-04 09:57:47 +00:00
[srp-server] add new otSrpServerAddressMode for faster start up (#11180)
This commit adds a new option in `otSrpServerAddressMode` as `OT_SRP_SERVER_ADDRESS_MODE_UNICAST_FORCE_ADD`. This allows faster SRP server start up by bypassing the Network Data publisher and adding the "SRP/DNS unicast" entry directly to Network Data upon enabling of the SRP server, regardless of how many other similar entries are present. This option is intended for testing and specific situations. A warning is added to indicate that using this option will make the device non-compliant with the Thread specification. The unit test `test_srp_server` is updated with a new test case to validate the new `AddressMode` and its related behavior.
This commit is contained in:
@@ -52,7 +52,7 @@ extern "C" {
|
||||
*
|
||||
* @note This number versions both OpenThread platform and user APIs.
|
||||
*/
|
||||
#define OPENTHREAD_API_VERSION (479)
|
||||
#define OPENTHREAD_API_VERSION (480)
|
||||
|
||||
/**
|
||||
* @addtogroup api-instance
|
||||
|
||||
@@ -84,11 +84,17 @@ typedef enum
|
||||
*
|
||||
* Address mode specifies how the address and port number are determined by the SRP server and how this info is
|
||||
* published in the Thread Network Data.
|
||||
*
|
||||
* @warning Using the `OT_SRP_SERVER_ADDRESS_MODE_UNICAST_FORCE_ADD` option will make the implementation
|
||||
* non-compliant with the Thread specification. This option is intended for testing and specific use-cases.
|
||||
* When selected, the SRP server, upon being enabled, will bypass the Network Data publisher and always add the
|
||||
* "SRP/DNS unicast" entry directly to the Network Data, regardless of how many other similar entries are present.
|
||||
*/
|
||||
typedef enum otSrpServerAddressMode
|
||||
{
|
||||
OT_SRP_SERVER_ADDRESS_MODE_UNICAST = 0, ///< Unicast address mode.
|
||||
OT_SRP_SERVER_ADDRESS_MODE_ANYCAST = 1, ///< Anycast address mode.
|
||||
OT_SRP_SERVER_ADDRESS_MODE_UNICAST = 0, ///< Unicast address mode. Use Network Data publisher.
|
||||
OT_SRP_SERVER_ADDRESS_MODE_ANYCAST = 1, ///< Anycast address mode. Use Network Data publisher
|
||||
OT_SRP_SERVER_ADDRESS_MODE_UNICAST_FORCE_ADD = 2, ///< Unicast address mode. Immediately force add to Network Data.
|
||||
} otSrpServerAddressMode;
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace Cli {
|
||||
* anycast
|
||||
* Done
|
||||
* @endcode
|
||||
* @cparam srp server addrmode [@ca{anycast}|@ca{unicast}]
|
||||
* @cparam srp server addrmode [@ca{anycast}|@ca{unicast}|@ca{unicast-force-add}]
|
||||
* @par
|
||||
* Gets or sets the address mode used by the SRP server.
|
||||
* @par
|
||||
@@ -78,6 +78,10 @@ template <> otError SrpServer::Process<Cmd("addrmode")>(Arg aArgs[])
|
||||
case OT_SRP_SERVER_ADDRESS_MODE_ANYCAST:
|
||||
OutputLine("anycast");
|
||||
break;
|
||||
|
||||
case OT_SRP_SERVER_ADDRESS_MODE_UNICAST_FORCE_ADD:
|
||||
OutputLine("unicast-force-add");
|
||||
break;
|
||||
}
|
||||
|
||||
error = OT_ERROR_NONE;
|
||||
@@ -90,6 +94,10 @@ template <> otError SrpServer::Process<Cmd("addrmode")>(Arg aArgs[])
|
||||
{
|
||||
error = otSrpServerSetAddressMode(GetInstancePtr(), OT_SRP_SERVER_ADDRESS_MODE_ANYCAST);
|
||||
}
|
||||
else if (aArgs[0] == "unicast-force-add")
|
||||
{
|
||||
error = otSrpServerSetAddressMode(GetInstancePtr(), OT_SRP_SERVER_ADDRESS_MODE_UNICAST_FORCE_ADD);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
@@ -144,6 +144,9 @@ void Server::Enable(void)
|
||||
// Thread Network Data based of `mAddressMode`. Then wait for
|
||||
// callback `HandleNetDataPublisherEvent()` from the
|
||||
// `Publisher` to start the SRP server.
|
||||
//
|
||||
// For `kAddressModeUnicastForceAdd`, directly add the entry
|
||||
// in the Network Data and start.
|
||||
|
||||
switch (mAddressMode)
|
||||
{
|
||||
@@ -156,6 +159,14 @@ void Server::Enable(void)
|
||||
mPort = kAnycastAddressModePort;
|
||||
Get<NetworkData::Publisher>().PublishDnsSrpServiceAnycast(mAnycastSequenceNumber, kSrpVersion);
|
||||
break;
|
||||
|
||||
case kAddressModeUnicastForceAdd:
|
||||
SelectPort();
|
||||
SuccessOrExit(Get<NetworkData::Service::Manager>().AddDnsSrpUnicastServiceWithAddrInServerData(
|
||||
Get<Mle::Mle>().GetMeshLocalEid(), mPort, kSrpVersion));
|
||||
Get<NetworkData::Notifier>().HandleServerDataUpdated();
|
||||
Start();
|
||||
break;
|
||||
}
|
||||
|
||||
exit:
|
||||
@@ -165,7 +176,20 @@ exit:
|
||||
void Server::Disable(void)
|
||||
{
|
||||
VerifyOrExit(mState != kStateDisabled);
|
||||
Get<NetworkData::Publisher>().UnpublishDnsSrpService();
|
||||
|
||||
switch (mAddressMode)
|
||||
{
|
||||
case kAddressModeUnicast:
|
||||
case kAddressModeAnycast:
|
||||
Get<NetworkData::Publisher>().UnpublishDnsSrpService();
|
||||
break;
|
||||
|
||||
case kAddressModeUnicastForceAdd:
|
||||
IgnoreError(Get<NetworkData::Service::Manager>().RemoveDnsSrpUnicastServiceWithAddrInServerData());
|
||||
Get<NetworkData::Notifier>().HandleServerDataUpdated();
|
||||
break;
|
||||
}
|
||||
|
||||
Stop();
|
||||
mState = kStateDisabled;
|
||||
|
||||
@@ -551,7 +575,8 @@ void Server::CommitSrpUpdate(Error aError,
|
||||
}
|
||||
|
||||
#if OPENTHREAD_CONFIG_SRP_SERVER_PORT_SWITCH_ENABLE
|
||||
if (!mHasRegisteredAnyService && (mAddressMode == kAddressModeUnicast))
|
||||
if (!mHasRegisteredAnyService &&
|
||||
((mAddressMode == kAddressModeUnicast) || (mAddressMode == kAddressModeUnicastForceAdd)))
|
||||
{
|
||||
Settings::SrpServerInfo info;
|
||||
|
||||
@@ -1714,8 +1739,9 @@ void Server::HandleOutstandingUpdatesTimer(void)
|
||||
const char *Server::AddressModeToString(AddressMode aMode)
|
||||
{
|
||||
static const char *const kAddressModeStrings[] = {
|
||||
"unicast", // (0) kAddressModeUnicast
|
||||
"anycast", // (1) kAddressModeAnycast
|
||||
"unicast", // (0) kAddressModeUnicast
|
||||
"anycast", // (1) kAddressModeAnycast
|
||||
"unicast-force-add", // (2) kAddressModeUnicastForceAdd
|
||||
};
|
||||
|
||||
struct EnumCheck
|
||||
@@ -1723,6 +1749,7 @@ const char *Server::AddressModeToString(AddressMode aMode)
|
||||
InitEnumValidatorCounter();
|
||||
ValidateNextEnum(kAddressModeUnicast);
|
||||
ValidateNextEnum(kAddressModeAnycast);
|
||||
ValidateNextEnum(kAddressModeUnicastForceAdd);
|
||||
};
|
||||
|
||||
return kAddressModeStrings[aMode];
|
||||
|
||||
@@ -159,8 +159,9 @@ public:
|
||||
*/
|
||||
enum AddressMode : uint8_t
|
||||
{
|
||||
kAddressModeUnicast = OT_SRP_SERVER_ADDRESS_MODE_UNICAST, ///< Unicast address mode.
|
||||
kAddressModeAnycast = OT_SRP_SERVER_ADDRESS_MODE_ANYCAST, ///< Anycast address mode.
|
||||
kAddressModeUnicast = OT_SRP_SERVER_ADDRESS_MODE_UNICAST, ///< Unicast mode with publisher.
|
||||
kAddressModeAnycast = OT_SRP_SERVER_ADDRESS_MODE_ANYCAST, ///< Anycast mode with publisher.
|
||||
kAddressModeUnicastForceAdd = OT_SRP_SERVER_ADDRESS_MODE_UNICAST_FORCE_ADD ///< Unicast - force add.
|
||||
};
|
||||
|
||||
class Host;
|
||||
|
||||
@@ -676,7 +676,8 @@ class Node(object):
|
||||
return self._cli_single_output('srp server state', expected_outputs=['disabled', 'running', 'stopped'])
|
||||
|
||||
def srp_server_get_addr_mode(self):
|
||||
return self._cli_single_output('srp server addrmode', expected_outputs=['unicast', 'anycast'])
|
||||
return self._cli_single_output('srp server addrmode',
|
||||
expected_outputs=['unicast', 'anycast', 'unicast-force-add'])
|
||||
|
||||
def srp_server_set_addr_mode(self, mode):
|
||||
self._cli_no_output('srp server addrmode', mode)
|
||||
|
||||
@@ -1207,6 +1207,74 @@ void TestSrpClientDelayedResponse(void)
|
||||
|
||||
#endif // OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
|
||||
|
||||
void TestSrpServerAddressModeForceAdd(void)
|
||||
{
|
||||
Srp::Server *srpServer;
|
||||
Srp::Client *srpClient;
|
||||
uint16_t heapAllocations;
|
||||
|
||||
Log("--------------------------------------------------------------------------------------------");
|
||||
Log("TestSrpServerAddressModeForceAdd");
|
||||
|
||||
InitTest();
|
||||
|
||||
srpServer = &sInstance->Get<Srp::Server>();
|
||||
srpClient = &sInstance->Get<Srp::Client>();
|
||||
|
||||
heapAllocations = sHeapAllocatedPtrs.GetLength();
|
||||
|
||||
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
// Set address mode to `kAddressModeUnicastForceAdd`.
|
||||
|
||||
SuccessOrQuit(srpServer->SetAddressMode(Srp::Server::kAddressModeUnicastForceAdd));
|
||||
VerifyOrQuit(srpServer->GetAddressMode() == Srp::Server::kAddressModeUnicastForceAdd);
|
||||
|
||||
VerifyOrQuit(srpServer->GetState() == Srp::Server::kStateDisabled);
|
||||
|
||||
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
// Start SRP server, ensure it starts quickly.
|
||||
|
||||
srpServer->SetEnabled(true);
|
||||
VerifyOrQuit(srpServer->GetState() != Srp::Server::kStateDisabled);
|
||||
|
||||
AdvanceTime(0);
|
||||
VerifyOrQuit(srpServer->GetState() == Srp::Server::kStateRunning);
|
||||
|
||||
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
// Start SRP client and validate that it discovers server.
|
||||
|
||||
srpClient->SetCallback(HandleSrpClientCallback, sInstance);
|
||||
|
||||
srpClient->EnableAutoStartMode(nullptr, nullptr);
|
||||
VerifyOrQuit(srpClient->IsAutoStartModeEnabled());
|
||||
|
||||
AdvanceTime(2000);
|
||||
VerifyOrQuit(srpClient->IsRunning());
|
||||
|
||||
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
// Disable SRP server. Validate that the NetData entry is removed and
|
||||
// client detects this.
|
||||
|
||||
Log("Disabling SRP server");
|
||||
|
||||
srpServer->SetEnabled(false);
|
||||
AdvanceTime(1);
|
||||
|
||||
VerifyOrQuit(!srpClient->IsRunning());
|
||||
|
||||
VerifyOrQuit(heapAllocations == sHeapAllocatedPtrs.GetLength());
|
||||
|
||||
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
// Finalize OT instance and validate all heap allocations are freed.
|
||||
|
||||
Log("Finalizing OT instance");
|
||||
FinalizeTest();
|
||||
|
||||
VerifyOrQuit(sHeapAllocatedPtrs.IsEmpty());
|
||||
|
||||
Log("End of TestSrpServerAddressModeForceAdd");
|
||||
}
|
||||
|
||||
#endif // ENABLE_SRP_TEST
|
||||
|
||||
} // namespace ot
|
||||
@@ -1223,6 +1291,7 @@ int main(void)
|
||||
ot::TestUpdateLeaseShortVariant();
|
||||
ot::TestSrpClientDelayedResponse();
|
||||
#endif
|
||||
ot::TestSrpServerAddressModeForceAdd();
|
||||
|
||||
printf("All tests passed\n");
|
||||
#else
|
||||
|
||||
Reference in New Issue
Block a user