Skip to content

Commit c8ae2e4

Browse files
Make EMAC memory test smarter. It now disables pool allocations when disabling input memory, and errors out if the MAC was working when it shouldn't've been.
1 parent 6bc2ef3 commit c8ae2e4

File tree

5 files changed

+89
-26
lines changed

5 files changed

+89
-26
lines changed

connectivity/netsocket/tests/TESTS/network/emac/emac_test_memory.cpp

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,65 +30,104 @@ using namespace utest::v1;
3030
void test_emac_memory_cb(int opt)
3131
{
3232
static bool send_request = true;
33-
static bool echo_should_work = true;
33+
static bool echo_may_fail = false;
34+
static bool echo_may_succeed = true;
3435
static int no_response_cnt = 0;
3536
static int retries = 0;
3637
static int msg_len = 0;
3738
static int test_step = 0;
3839
static bool next_step = true;
3940
static int options = CTP_OPT_NON_ALIGNED;
4041

42+
static int successful_pkts_this_step = 0;
43+
static int failed_pkts_this_step = 0;
44+
4145
// Defines test step
4246
if (next_step) {
4347
next_step = false;
48+
49+
// Verify that, if this was a test step where allocations were supposed to fail, they actually did fail.
50+
// We can accept a couple of successful packets due to buffers that had been allocated earlier, but the
51+
// *majority* of the packets should have failed.
52+
if(test_step > 0 && !echo_may_succeed)
53+
{
54+
if(successful_pkts_this_step >= failed_pkts_this_step)
55+
{
56+
printf("Too many successful packets (%d) vs failed packets (%d). This was supposed to be a failure test!\r\n",
57+
successful_pkts_this_step, failed_pkts_this_step);
58+
SET_ERROR_FLAGS(TEST_FAILED);
59+
END_TEST_LOOP;
60+
}
61+
}
62+
successful_pkts_this_step = 0;
63+
failed_pkts_this_step = 0;
64+
4465
switch (test_step) {
4566
case 0:
4667
printf("STEP 0: memory available\r\n\r\n");
4768
emac_if_set_output_memory(true);
4869
emac_if_set_input_memory(true);
49-
echo_should_work = true;
70+
emac_if_check_memory(false);
71+
echo_may_fail = false;
72+
echo_may_succeed = true;
5073
break;
5174

5275
case 1:
5376
printf("STEP 1: no input memory buffer memory available\r\n\r\n");
5477
emac_if_set_output_memory(true);
5578
emac_if_set_input_memory(false);
56-
echo_should_work = false;
79+
emac_if_check_memory(false);
80+
echo_may_fail = true;
81+
echo_may_succeed = false;
5782
break;
5883

5984
case 2:
6085
printf("STEP 2: memory available\r\n\r\n");
6186
emac_if_set_output_memory(true);
6287
emac_if_set_input_memory(true);
63-
echo_should_work = true;
88+
emac_if_check_memory(false);
89+
echo_may_fail = false;
90+
echo_may_succeed = true;
6491
break;
6592

6693
case 3:
6794
printf("STEP 3: no output memory buffer memory available\r\n\r\n");
6895
emac_if_set_output_memory(false);
6996
emac_if_set_input_memory(true);
70-
echo_should_work = false;
97+
emac_if_check_memory(false);
98+
99+
// For this test, zero-copy implementations can still successfully work because
100+
// they don't need to allocate anything to send a packet.
101+
// So we allow either success or failure.
102+
echo_may_fail = true;
103+
echo_may_succeed = true;
71104
break;
72105

73106
case 4:
74107
printf("STEP 4: memory available\r\n\r\n");
75108
emac_if_set_output_memory(true);
76109
emac_if_set_input_memory(true);
77-
echo_should_work = true;
110+
emac_if_check_memory(false);
111+
echo_may_fail = false;
112+
echo_may_succeed = true;
78113
break;
79114

80115
case 5:
81116
printf("STEP 5: no output or input memory buffer memory available\r\n\r\n");
82117
emac_if_set_output_memory(false);
83118
emac_if_set_input_memory(false);
84-
echo_should_work = false;
119+
emac_if_check_memory(false);
120+
echo_may_fail = true;
121+
echo_may_succeed = false;
85122
break;
86123

87124
case 6:
88125
printf("STEP 6: memory available\r\n\r\n");
89126
emac_if_set_output_memory(true);
90127
emac_if_set_input_memory(true);
91-
echo_should_work = true;
128+
emac_if_check_memory(false);
129+
echo_may_fail = false;
130+
echo_may_succeed = true;
92131
break;
93132

94133
case 7:
@@ -107,8 +146,9 @@ void test_emac_memory_cb(int opt)
107146
} else if (opt == TIMEOUT) {
108147
if (++no_response_cnt > 3) {
109148
if (++retries > 1) {
149+
failed_pkts_this_step += 1;
110150
// If echo replies should be received fails the test case
111-
if (echo_should_work) {
151+
if (!echo_may_fail) {
112152
printf("too many retries\r\n\r\n");
113153
SET_ERROR_FLAGS(TEST_FAILED);
114154
END_TEST_LOOP;
@@ -124,6 +164,11 @@ void test_emac_memory_cb(int opt)
124164
}
125165
}
126166

167+
if(opt == INPUT)
168+
{
169+
successful_pkts_this_step += 1;
170+
}
171+
127172
// Echo response received or retry count exceeded for memory buffers are not available
128173
if (opt == INPUT || next_len) {
129174
if (msg_len == ETH_MAX_LEN) {

connectivity/netsocket/tests/emac_test_utils/EmacTestMemoryManager.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ EmacTestMemoryManager::EmacTestMemoryManager()
7373
: m_mem_mutex(),
7474
m_mem_buffers(),
7575
m_alloc_unit(MBED_CONF_NSAPI_EMAC_RX_POOL_BUF_SIZE),
76-
m_memory_available(true)
76+
m_memory_available(true),
77+
m_pool_memory_available(true)
7778
{
7879
#ifdef ETHMEM_SECTION
7980
static bool ns_heap_init = false;
@@ -171,7 +172,7 @@ emac_mem_buf_t *EmacTestMemoryManager::alloc_pool(uint32_t size, uint32_t align,
171172

172173
check_align(align);
173174

174-
if ((opt & MEM_CHECK) && !m_memory_available) {
175+
if ((opt & MEM_CHECK) && (!m_memory_available || !m_pool_memory_available)) {
175176
return NULL;
176177
}
177178

@@ -507,6 +508,16 @@ void EmacTestMemoryManager::set_memory_available(bool memory)
507508
}
508509
}
509510

511+
void EmacTestMemoryManager::set_pool_memory_available(bool memory)
512+
{
513+
m_pool_memory_available = memory;
514+
515+
// Poke the EMAC in case it can allocate buffers
516+
if (m_pool_memory_available && m_memory_available && onPoolSpaceAvailCallback) {
517+
onPoolSpaceAvailCallback();
518+
}
519+
}
520+
510521
void EmacTestMemoryManager::get_memory_statistics(int *buffers, int *memory)
511522
{
512523
if (!buffers || !memory) {

connectivity/netsocket/tests/emac_test_utils/EmacTestMemoryManager.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,23 @@ class EmacTestMemoryManager : public EMACMemoryManager {
108108
virtual void set_alloc_unit(uint32_t alloc_unit);
109109

110110
/**
111-
* Sets whether memory is available
111+
* Sets whether memory (heap or pool) is available
112112
*
113113
* Can be used to disable memory allocation request from emac.
114114
*
115115
* @param memory True if memory is available
116116
*/
117117
void set_memory_available(bool memory);
118118

119+
/**
120+
* Sets whether pool memory is available.
121+
*
122+
* Can be used to disable memory allocation request for the pool from emac.
123+
*
124+
* @param memory True if memory is available
125+
*/
126+
void set_pool_memory_available(bool memory);
127+
119128
/**
120129
* Gets memory statistics
121130
*
@@ -136,6 +145,7 @@ class EmacTestMemoryManager : public EMACMemoryManager {
136145
unsigned int m_alloc_unit;
137146
size_t m_pool_bufs_used = 0;
138147
bool m_memory_available;
148+
bool m_pool_memory_available;
139149
};
140150

141151
#endif /* EMAC_TEST_MEMORY_MANAGER_H */

connectivity/netsocket/tests/emac_test_utils/emac_util.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -384,25 +384,15 @@ void emac_if_set_output_memory(bool memory)
384384
void emac_if_set_input_memory(bool memory)
385385
{
386386
allow_input_memory_allocs = memory;
387-
388-
emac_if_set_memory(memory);
387+
EmacTestMemoryManager::get_instance().set_pool_memory_available(allow_input_memory_allocs);
389388
}
390389

391390
void emac_if_check_memory(bool output)
392391
{
393392
if (output) {
394-
emac_if_set_memory(allow_output_memory_allocs);
393+
EmacTestMemoryManager::get_instance().set_memory_available(allow_output_memory_allocs);
395394
} else {
396-
emac_if_set_memory(allow_input_memory_allocs);
397-
}
398-
}
399-
400-
void emac_if_set_memory(bool memory)
401-
{
402-
static bool memory_value = true;
403-
if (memory_value != memory) {
404-
memory_value = memory;
405-
EmacTestMemoryManager::get_instance().set_memory_available(memory);
395+
EmacTestMemoryManager::get_instance().set_memory_available(allow_input_memory_allocs);
406396
}
407397
}
408398

connectivity/netsocket/tests/emac_test_utils/emac_util.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,17 @@ void emac_if_set_all_multicast(bool all);
113113
void emac_if_add_multicast_group(uint8_t *address);
114114
void emac_if_remove_multicast_group(uint8_t *address);
115115

116+
/// If called with false as the arg, disables memory allocation
117+
/// (a) from the pool at all times, and
118+
/// (b) from the heap when not actively transmitting a packet
116119
void emac_if_set_output_memory(bool memory);
120+
121+
/// If called with true as the arg, disables memory allocation from the pool and heap during packet Tx.
117122
void emac_if_set_input_memory(bool memory);
123+
124+
/// Switches between input (false) and output (true) mode for the memory allocation disable.
125+
/// Called by the CTP code when it's sending a packet.
118126
void emac_if_check_memory(bool output);
119-
void emac_if_set_memory(bool memory);
120127

121128
void emac_if_set_ctp_server_enabled(bool enabled);
122129

0 commit comments

Comments
 (0)