Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS manual cleanup mechanism added #11535

Merged
merged 3 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/asynchronous_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int result_exp_timeout;

void ASYNCHRONOUS_DNS()
{
nsapi_dns_reset();
do_asynchronous_gethostbyname(dns_test_hosts, 1, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);

TEST_ASSERT_EQUAL(1, result_ok);
Expand Down
2 changes: 2 additions & 0 deletions TESTS/netsocket/dns/asynchronous_dns_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ void ASYNCHRONOUS_DNS_CACHE()
Ticker ticker;
ticker.attach_us(&test_dns_query_ticker, 100);

nsapi_dns_reset();

for (unsigned int i = 0; i < 5; i++) {
int started_us = ticker_us;

Expand Down
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/asynchronous_dns_cancel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void ASYNCHRONOUS_DNS_CANCEL()
dns_application_data *data = new dns_application_data[MBED_CONF_APP_DNS_TEST_HOSTS_NUM];

int count = 0;
nsapi_dns_reset();

for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
data[i].value_set = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ static nsapi_error_t event_queue_call(int delay, mbed::Callback<void()> func)

void ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE()
{
// Ensures that cache does not contain entries
do_asynchronous_gethostbyname(dns_test_hosts, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

TEST_ASSERT_EQUAL(MBED_CONF_NSAPI_DNS_CACHE_SIZE, result_ok);
TEST_ASSERT_EQUAL(0, result_no_mem);
TEST_ASSERT_EQUAL(0, result_dns_failure);
TEST_ASSERT_EQUAL(0, result_exp_timeout);
nsapi_dns_reset();

// Dispatch event queue
Thread eventThread(osPriorityNormal, EXTERNAL_THREAD_SIZE);
Expand Down
4 changes: 1 addition & 3 deletions TESTS/netsocket/dns/asynchronous_dns_invalid_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ int result_exp_timeout;

void ASYNCHRONOUS_DNS_INVALID_HOST()
{
// Ensures that cache does not contain entries
do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);
nsapi_dns_reset();

char dns_test_hosts_new[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN];
memcpy(dns_test_hosts_new, dns_test_hosts, sizeof(dns_test_hosts_new));
Expand Down
2 changes: 2 additions & 0 deletions TESTS/netsocket/dns/asynchronous_dns_non_async_and_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC()
dns_application_data data;
data.semaphore = &semaphore;

nsapi_dns_reset();

// Initiate
nsapi_error_t err = get_interface()->gethostbyname_async(dns_test_hosts_second[0],
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data));
Expand Down
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/asynchronous_dns_simultaneous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int result_exp_timeout;

void ASYNCHRONOUS_DNS_SIMULTANEOUS()
{
nsapi_dns_reset();
do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

Expand Down
7 changes: 4 additions & 3 deletions TESTS/netsocket/dns/asynchronous_dns_simultaneous_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ int result_exp_timeout;

void ASYNCHRONOUS_DNS_SIMULTANEOUS_CACHE()
{
nsapi_dns_reset();
AnttiKauppila marked this conversation as resolved.
Show resolved Hide resolved
do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

// Addresses were cached on last step, OK for all
TEST_ASSERT_EQUAL(MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, result_ok);
TEST_ASSERT_EQUAL(0, result_no_mem);
// As cache is empty, 6th entry will return out of memory.
TEST_ASSERT_EQUAL(MBED_CONF_APP_DNS_SIMULT_QUERIES, result_ok);
TEST_ASSERT_EQUAL(1, result_no_mem);
TEST_ASSERT_EQUAL(0, result_dns_failure);
TEST_ASSERT_EQUAL(0, result_exp_timeout);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int result_exp_timeout;

void ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT()
{

nsapi_dns_reset();
for (int i = 0; i < 10; i++) {
do_asynchronous_gethostbyname(dns_test_hosts, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);
Expand Down
11 changes: 1 addition & 10 deletions TESTS/netsocket/dns/asynchronous_dns_timeouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include "utest.h"
#include "dns_tests.h"

#include "nsapi_dns.h"

using namespace utest::v1;

namespace {
Expand All @@ -49,14 +47,7 @@ static nsapi_error_t event_queue_call(int delay, mbed::Callback<void()> func)

void ASYNCHRONOUS_DNS_TIMEOUTS()
{
// Ensures that cache does not contain entries
do_asynchronous_gethostbyname(dns_test_hosts, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

TEST_ASSERT_EQUAL(MBED_CONF_NSAPI_DNS_CACHE_SIZE, result_ok);
TEST_ASSERT_EQUAL(0, result_no_mem);
TEST_ASSERT_EQUAL(0, result_dns_failure);
TEST_ASSERT_EQUAL(0, result_exp_timeout);
nsapi_dns_reset();

// Dispatch event queue
Thread eventThread(osPriorityNormal, EXTERNAL_THREAD_SIZE);
Expand Down
2 changes: 2 additions & 0 deletions TESTS/netsocket/dns/dns_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef DNS_TESTS_H
#define DNS_TESTS_H

#include "nsapi_dns.h"

#ifndef MBED_CONF_APP_DNS_SIMULT_QUERIES
#ifdef MBED_CONF_CELLULAR_OFFLOAD_DNS_QUERIES
#define MBED_CONF_APP_DNS_SIMULT_QUERIES MBED_CONF_CELLULAR_OFFLOAD_DNS_QUERIES
Expand Down
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ NetworkInterface *get_interface()

static void net_bringup()
{
nsapi_dns_reset();
MBED_ASSERT(MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_NSAPI_DNS_CACHE_SIZE && MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_APP_DNS_SIMULT_QUERIES + 1);

net = NetworkInterface::get_default_instance();
Expand Down
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/synchronous_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int result_exp_timeout;

void SYNCHRONOUS_DNS()
{
nsapi_dns_reset();
do_gethostbyname(dns_test_hosts, 1, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);

TEST_ASSERT_EQUAL(1, result_ok);
Expand Down
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/synchronous_dns_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static void test_dns_query_ticker(void)

void SYNCHRONOUS_DNS_CACHE()
{
nsapi_dns_reset();
Ticker ticker;
ticker.attach_us(&test_dns_query_ticker, 100);

Expand Down
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/synchronous_dns_invalid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ int result_exp_timeout;
void SYNCHRONOUS_DNS_INVALID()
{
//Ensure that there are no addressess in cache
nsapi_dns_reset();
do_gethostbyname(dns_test_hosts_second, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);

char dns_test_hosts_new[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN];
Expand Down
1 change: 1 addition & 0 deletions TESTS/netsocket/dns/synchronous_dns_multiple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ int result_exp_timeout;

void SYNCHRONOUS_DNS_MULTIPLE()
{
nsapi_dns_reset();
do_gethostbyname(dns_test_hosts, MBED_CONF_APP_DNS_TEST_HOSTS_NUM, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);

TEST_ASSERT_EQUAL(MBED_CONF_APP_DNS_TEST_HOSTS_NUM, result_ok);
Expand Down
2 changes: 1 addition & 1 deletion TESTS/network/multihoming/multihoming_asynchronous_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void MULTIHOMING_ASYNCHRONOUS_DNS()
dns_application_data data;
data.semaphore = &semaphore;


nsapi_dns_reset();

for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {

Expand Down
2 changes: 2 additions & 0 deletions TESTS/network/multihoming/multihoming_synchronous_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void MULTIHOMING_SYNCHRONOUS_DNS()
result_dns_failure = 0;
result_exp_timeout = 0;

nsapi_dns_reset();

for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
SocketAddress address;

Expand Down
1 change: 1 addition & 0 deletions TESTS/network/multihoming/multihoming_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "mbed.h"
#include "nsapi_dns.h"

#ifndef MULTIHOMING_TESTS_H
#define MULTIHOMING_TESTS_H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static void _sigio_handler(osThreadId id)

void MULTIHOMING_UDPSOCKET_ECHOTEST()
{
nsapi_dns_reset();

for (unsigned int interface_index = 0; interface_index < MBED_CONF_MULTIHOMING_MAX_INTERFACES_NUM; interface_index++) {
NetworkInterface *interface = get_interface(interface_index);
Expand Down
5 changes: 5 additions & 0 deletions UNITTESTS/stubs/nsapi_dns_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ extern "C" nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr, const char *int
{
return NSAPI_ERROR_OK;
}

void nsapi_dns_cache_reset()
{

}
22 changes: 22 additions & 0 deletions features/netsocket/nsapi_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct DNS_QUERY {

static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_t ttl);
static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t version, nsapi_addr_t *address);
static void nsapi_dns_cache_reset();

static nsapi_error_t nsapi_dns_get_server_addr(NetworkStack *stack, uint8_t *index, uint8_t *total_attempts, uint8_t *send_success, SocketAddress *dns_addr, const char *interface_name);

Expand Down Expand Up @@ -403,6 +404,22 @@ static nsapi_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t vers
return ret_val;
}

static void nsapi_dns_cache_reset()
{
#if (MBED_CONF_NSAPI_DNS_CACHE_SIZE > 0)
dns_cache_mutex->lock();
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
if (dns_cache[i]) {
delete dns_cache[i]->host;
dns_cache[i]->host = NULL;
delete dns_cache[i];
dns_cache[i] = NULL;
}
}
dns_cache_mutex->unlock();
#endif
}

static nsapi_error_t nsapi_dns_get_server_addr(NetworkStack *stack, uint8_t *index, uint8_t *total_attempts, uint8_t *send_success, SocketAddress *dns_addr, const char *interface_name)
{
bool dns_addr_set = false;
Expand Down Expand Up @@ -618,6 +635,11 @@ void nsapi_dns_call_in_set(call_in_callback_cb_t callback)
*dns_call_in.get() = callback;
}

void nsapi_dns_reset()
{
nsapi_dns_cache_reset();
}

nsapi_error_t nsapi_dns_call_in(call_in_callback_cb_t cb, int delay, mbed::Callback<void()> func)
{
if (*dns_call_in.get()) {
Expand Down
8 changes: 8 additions & 0 deletions features/netsocket/nsapi_dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ nsapi_error_t nsapi_dns_query_async_cancel(nsapi_error_t id);
*/
void nsapi_dns_call_in_set(call_in_callback_cb_t callback);

/**
* @brief nsapi_dns_reset Resets all internal states and frees reserved memory, see NOTE!
* Can be used to clean up system resources when there is no need for network connections.
* NOTE: Does NOT clear asynchronous ongoing operations!
* Currently only cleans up DNS cache (if used)
*/
void nsapi_dns_reset();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in the public API?

I don't see any real use cases for resetting the cache, other than to test it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have, but some application developer might want to use it so why should we prevent that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real solution should be that when last NetworkStack is freed, we should free all DNS related stuff as well. Or if permanent DNS cache is needed, we could flag that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such automatic purging should be optional. Cached information is still valid even if a network interface goes down.

No objection to adding a public API for test and/or application use though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our DNS greentea tests use cache, even though perhaps sometimes they shouldn't. For example, if you run the whole suite, then the _SIMULTANOUS tests which are supposed to run 5 DNS requests at the same time, will in fact take three addresses resolved in previous tests out of cache and only open two more sockets to resolve the non-cached addresses.
With this API in place we can clear cache where we don't want the test to rely on it.


/** Add a domain name server to list of servers to query
*
* @param addr Destination for the host address
Expand Down