Skip to content

Commit c5b6993

Browse files
author
Mika Tervonen
committed
refactored packet ingress
made the ingress affect other interfaces than RF Moved the functionality to ns_monitor
1 parent a05605e commit c5b6993

File tree

15 files changed

+109
-62
lines changed

15 files changed

+109
-62
lines changed

source/Core/include/ns_monitor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,10 @@ void ns_monitor_timer(uint16_t seconds);
3535

3636
int ns_monitor_heap_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critical);
3737

38+
int ns_monitor_packet_ingress_rate_limit_by_memory(uint8_t free_heap_percentage);
39+
40+
bool ns_monitor_packet_allocation_allowed(void);
41+
42+
3843
#endif // _NS_MONITOR_H
3944

source/Core/ns_monitor.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ typedef struct ns_monitor__s {
6262

6363
static ns_monitor_t *ns_monitor_ptr = NULL;
6464

65+
static uint8_t ns_dyn_mem_rate_limiting_threshold_percentage = 0; // Percentage of free memory required to allow routing
66+
6567
typedef void (ns_maintenance_gc_cb)(bool full_gc);
6668

6769
/*
@@ -189,3 +191,31 @@ int ns_monitor_heap_gc_threshold_set(uint8_t percentage_high, uint8_t percentage
189191

190192
return -1;
191193
}
194+
195+
int ns_monitor_packet_ingress_rate_limit_by_memory(uint8_t free_heap_percentage)
196+
{
197+
if (free_heap_percentage < 100) {
198+
ns_dyn_mem_rate_limiting_threshold_percentage = free_heap_percentage;
199+
return 0;
200+
}
201+
202+
return -1;
203+
}
204+
205+
bool ns_monitor_packet_allocation_allowed(void)
206+
{
207+
// If there is no packets to forward this should not be blocked.
208+
// There should be cleanup routine enabled that will remove unneeded memory to prevent locks
209+
// this could trigger a function to clean packets from routing and allow newest packets
210+
211+
const mem_stat_t *ns_dyn_mem_stat = ns_dyn_mem_get_mem_stat();
212+
213+
if (ns_dyn_mem_stat && ns_dyn_mem_rate_limiting_threshold_percentage) {
214+
if (ns_dyn_mem_stat->heap_sector_allocated_bytes > ns_dyn_mem_stat->heap_sector_size / 100 * (100 - ns_dyn_mem_rate_limiting_threshold_percentage)) {
215+
// Packet allocation not allowed as memory is running low.
216+
return false;
217+
}
218+
}
219+
return true;
220+
}
221+

source/MAC/IEEE802_15_4/mac_mcps_sap.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "fhss_api.h"
3535
#include "platform/arm_hal_interrupt.h"
3636
#include "common_functions.h"
37+
#include "Core/include/ns_monitor.h"
3738

3839
#include "MAC/IEEE802_15_4/sw_mac_internal.h"
3940
#include "MAC/IEEE802_15_4/mac_defines.h"
@@ -75,8 +76,6 @@ static void mac_pd_data_confirm_failure_handle(protocol_interface_rf_mac_setup_s
7576

7677
static int8_t mac_tasklet_event_handler = -1;
7778

78-
static uint8_t ns_dyn_mem_rate_limiting_threshold_percentage = 0;
79-
8079
/**
8180
* Get PHY time stamp.
8281
*
@@ -2131,13 +2130,9 @@ void mcps_sap_pre_parsed_frame_buffer_free(mac_pre_parsed_frame_t *buf)
21312130
mac_pre_parsed_frame_t *mcps_sap_pre_parsed_frame_buffer_get(const uint8_t *data_ptr, uint16_t frame_length)
21322131
{
21332132
// check that system has enough space to handle the new packet
2134-
const mem_stat_t *ns_dyn_mem_stat = ns_dyn_mem_get_mem_stat();
2135-
2136-
if (ns_dyn_mem_stat && ns_dyn_mem_rate_limiting_threshold_percentage) {
2137-
if (ns_dyn_mem_stat->heap_sector_allocated_bytes > ns_dyn_mem_stat->heap_sector_size / 100 * (100 - ns_dyn_mem_rate_limiting_threshold_percentage)) {
2138-
// Packet is dropped from radio interface because there is not enough memory to handle incoming packets.
2139-
return NULL;
2140-
}
2133+
if (!ns_monitor_packet_allocation_allowed()) {
2134+
// stack can not handle new packets for routing
2135+
return NULL;
21412136
}
21422137

21432138
mac_pre_parsed_frame_t *buffer = ns_dyn_mem_temporary_alloc(sizeof(mac_pre_parsed_frame_t) + frame_length);
@@ -2391,16 +2386,6 @@ uint8_t mcps_sap_purge_reg_handler(protocol_interface_rf_mac_setup_s *rf_mac_set
23912386
return confirmation.status;
23922387
}
23932388

2394-
int mcps_packet_ingress_rate_limit_by_memory(uint8_t free_heap_percentage)
2395-
{
2396-
if (free_heap_percentage < 100) {
2397-
ns_dyn_mem_rate_limiting_threshold_percentage = free_heap_percentage;
2398-
return 0;
2399-
}
2400-
2401-
return -1;
2402-
}
2403-
24042389
void mcps_pending_packet_counter_update_check(protocol_interface_rf_mac_setup_s *rf_mac_setup, mac_pre_build_frame_t *buffer)
24052390
{
24062391
if (buffer->fcf_dsn.securityEnabled) {

source/MAC/ethernet/ethernet_mac_api.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "nsdynmemLIB.h"
2323
#include "common_functions.h"
2424
#include "MAC/rf_driver_storage.h"
25+
#include "Core/include/ns_monitor.h"
2526

2627
typedef struct eth_mac_internal_s {
2728
eth_mac_api_t *mac_api;
@@ -269,6 +270,11 @@ static int8_t eth_mac_net_phy_rx(const uint8_t *data_ptr, uint16_t data_len, uin
269270
return -1;
270271
}
271272

273+
if (!ns_monitor_packet_allocation_allowed()) {
274+
// stack can not handle new packets for routing
275+
return -1;
276+
}
277+
272278
eth_data_ind_t *data_ind = ns_dyn_mem_temporary_alloc(sizeof(eth_data_ind_t));
273279
if (!data_ind) {
274280
return -1;

source/MAC/serial/serial_mac_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "common_functions.h"
2424
#include "ns_trace.h"
2525
#include "MAC/rf_driver_storage.h"
26+
#include "Core/include/ns_monitor.h"
2627

2728
#define TRACE_GROUP "seMa"
2829

@@ -317,10 +318,16 @@ static int8_t serial_mac_net_phy_rx(const uint8_t *data_ptr, uint16_t data_len,
317318
(void)link_quality;
318319
(void) dbm;
319320

321+
if (!ns_monitor_packet_allocation_allowed()) {
322+
// stack can not handle new packets for routing
323+
return -1;
324+
}
325+
320326
serial_data_ind_t *data_ind = ns_dyn_mem_temporary_alloc(sizeof(serial_data_ind_t));
321327
if (!data_ind) {
322328
return -1;
323329
}
330+
324331
data_ind->msdu = ns_dyn_mem_temporary_alloc(data_len);
325332
if (!data_ind->msdu) {
326333
ns_dyn_mem_free(data_ind);

source/Service_Libs/utils/ns_conf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ int ns_conf_gc_threshold_set(uint8_t percentage_high, uint8_t percentage_critica
2929

3030
int ns_conf_packet_ingress_rate_limit_by_mem(uint8_t free_heap_percentage)
3131
{
32-
return mcps_packet_ingress_rate_limit_by_memory(free_heap_percentage);
32+
return ns_monitor_packet_ingress_rate_limit_by_memory(free_heap_percentage);
3333
}

test/nanostack/unittest/mac/ethernet_mac_api/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ TEST_SRC_FILES = \
1010
main.cpp \
1111
ethernet_mac_apitest.cpp \
1212
test_ethernet_mac_api.c \
13+
../../stub/ns_monitor_stub.c \
1314
../../stub/common_functions_stub.c \
1415
../../stub/nsdynmemLIB_stub.c \
1516
../../stub/event_stub.c \

test/nanostack/unittest/mac/mac_mcps_sap/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TEST_SRC_FILES = \
1616
../../stub/mac_header_helper_functions_stub.c \
1717
../../stub/event_stub.c \
1818
../../stub/ns_timer_stub.c \
19+
../../stub/ns_monitor_stub.c \
1920
../../stub/sw_mac_stub.c \
2021
../../stub/mac_mlme_stub.c \
2122
../../stub/mac_timer_stub.c \

test/nanostack/unittest/mac/mac_mcps_sap/mac_mcps_saptest.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,3 @@ TEST(mac_mcps_sap, test_mcps_sap_data_req_handler_ext)
127127
{
128128
CHECK(test_mcps_sap_data_req_handler_ext());
129129
}
130-
131-
TEST(mac_mcps_sap, test_mcps_packet_ingress_rate_limit_by_memory)
132-
{
133-
CHECK(test_mcps_packet_ingress_rate_limit_by_memory());
134-
}
135-

test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "mac_filter_stub.h"
5959
#include "mac_mlme_stub.h"
6060
#include "fhss_config_stub.h"
61+
#include "ns_monitor_stub.h"
6162
#include <string.h>
6263

6364
static uint8_t tes_mac_mcps_mac64[8];
@@ -2196,20 +2197,18 @@ bool test_mcps_sap_pre_parsed_frame_buffer_get()
21962197
ns_dyn_mem_free(buf);
21972198

21982199
// Test that if dynamic memory is low, then allocation fails
2199-
// Update memory stats: heap size = 1000, allocated 600
22002200
nsdynmemlib_stub.returnCounter = 1;
2201-
nsdynmemlib_mem_stats_stub.heap_sector_size = 1000;
2202-
nsdynmemlib_mem_stats_stub.heap_sector_allocated_bytes = 600;
2203-
// Set ingress limit to 50% = 500 bytes
2204-
mcps_packet_ingress_rate_limit_by_memory(50);
2201+
ns_monitor_stub.return_bool = false;
2202+
22052203
buf = mcps_sap_pre_parsed_frame_buffer_get(buffer, 8);
22062204
if (buf) {
22072205
return false;
22082206
}
22092207

22102208
// Test that allocation succeeds when there is enough heap
22112209
nsdynmemlib_stub.returnCounter = 1;
2212-
nsdynmemlib_mem_stats_stub.heap_sector_allocated_bytes = 400;
2210+
ns_monitor_stub.return_bool = true;
2211+
22132212
buf = mcps_sap_pre_parsed_frame_buffer_get(buffer, 8);
22142213
if (!buf || buf->frameLength != 8) {
22152214
return false;
@@ -3227,25 +3226,3 @@ bool test_mcps_sap_data_req_handler_ext()
32273226
test_mac_rf_mac_class_free(rf_mac_setup);
32283227
return true;
32293228
}
3230-
3231-
bool test_mcps_packet_ingress_rate_limit_by_memory()
3232-
{
3233-
3234-
int ret_val;
3235-
3236-
nsdynmemlib_mem_stats_stub.heap_sector_size = 1000;
3237-
3238-
// Too big percentage
3239-
ret_val = mcps_packet_ingress_rate_limit_by_memory(101);
3240-
if (ret_val == 0) {
3241-
return false;
3242-
}
3243-
3244-
// OK case
3245-
ret_val = mcps_packet_ingress_rate_limit_by_memory(50);
3246-
if (ret_val != 0) {
3247-
return false;
3248-
}
3249-
3250-
return true;
3251-
}

test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ bool test_mac_mcps_data_confirmation();
6363

6464
bool test_mcps_sap_data_req_handler_ext();
6565

66-
bool test_mcps_packet_ingress_rate_limit_by_memory();
67-
6866
#ifdef __cplusplus
6967
}
7068
#endif

test/nanostack/unittest/mac/serial_mac_api/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TEST_SRC_FILES = \
1212
test_serial_mac_api.c \
1313
../../stub/common_functions_stub.c \
1414
../../stub/ns_list_stub.c \
15+
../../stub/ns_monitor_stub.c \
1516
../../stub/nsdynmemLIB_stub.c \
1617
../../stub/mbed_trace_stub.c \
1718
../../stub/event_stub.c \

test/nanostack/unittest/stub/mac_mcps_sap_stub.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,6 @@ uint8_t mcps_sap_purge_reg_handler(protocol_interface_rf_mac_setup_s *rf_mac_set
152152
return 0;
153153
}
154154

155-
int mcps_packet_ingress_rate_limit_by_memory(uint8_t free_heap_percentage)
156-
{
157-
return (int)mac_mcps_sap_stub.int8_value;
158-
}
159-
160155
int mac_convert_frame_type_to_fhss(uint8_t frame_type)
161156
{
162157
return 0;

test/nanostack/unittest/stub/ns_monitor_stub.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#include <string.h>
1919
#include "ns_types.h"
2020
#include "nsdynmemLIB.h"
21-
#include "ns_file_system.h"
21+
#include "ns_monitor_stub.h"
22+
23+
24+
ns_monitor_stub_def ns_monitor_stub = {true};
25+
2226

2327
void ns_monitor_init(void)
2428
{
@@ -38,4 +42,12 @@ int ns_monitor_heap_gc_threshold_set(uint8_t percentage_high, uint8_t percentage
3842
return 0;
3943
}
4044

45+
int ns_monitor_packet_ingress_rate_limit_by_memory(uint8_t free_heap_percentage)
46+
{
47+
return 0;
48+
}
4149

50+
bool ns_monitor_packet_allocation_allowed(void)
51+
{
52+
return ns_monitor_stub.return_bool;
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2015-2017, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef __NS_MONITOR_STUB_H__
18+
#define __NS_MONITOR_STUB_H__
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
typedef struct {
25+
bool return_bool;
26+
} ns_monitor_stub_def;
27+
28+
extern ns_monitor_stub_def ns_monitor_stub;
29+
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
35+
#endif // __NS_MONITOR_STUB_H__

0 commit comments

Comments
 (0)