Skip to content

Commit 2b6ebc0

Browse files
committed
net: tcp: Timeout memory allocations
Instead of waiting forever for a free net_buf, set a timeout to the allocations (500 ms). This way the application will not be blocked by memory exhaustion. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
1 parent bd7d1bd commit 2b6ebc0

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

subsys/net/ip/tcp.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "tcp.h"
3737
#include "net_stats.h"
3838

39+
#define ALLOC_TIMEOUT 500
40+
3941
/*
4042
* Each TCP connection needs to be tracked by net_context, so
4143
* we need to allocate equal number of control structures here.
@@ -315,7 +317,10 @@ static struct net_pkt *prepare_segment(struct net_tcp *tcp,
315317
tail = pkt->frags;
316318
pkt->frags = NULL;
317319
} else {
318-
pkt = net_pkt_get_tx(context, K_FOREVER);
320+
pkt = net_pkt_get_tx(context, ALLOC_TIMEOUT);
321+
if (!pkt) {
322+
return NULL;
323+
}
319324
}
320325

321326
#if defined(CONFIG_NET_IPV4)
@@ -347,7 +352,12 @@ static struct net_pkt *prepare_segment(struct net_tcp *tcp,
347352
return NULL;
348353
}
349354

350-
header = net_pkt_get_data(context, K_FOREVER);
355+
header = net_pkt_get_data(context, ALLOC_TIMEOUT);
356+
if (!header) {
357+
net_pkt_unref(pkt);
358+
return NULL;
359+
}
360+
351361
net_pkt_frag_add(pkt, header);
352362

353363
tcphdr = (struct net_tcp_hdr *)net_buf_add(header, NET_TCPH_LEN);
@@ -740,10 +750,18 @@ int net_tcp_send_pkt(struct net_pkt *pkt)
740750
}
741751

742752
if (pkt_in_slist) {
743-
new_pkt = net_pkt_get_tx(ctx, K_FOREVER);
753+
new_pkt = net_pkt_get_tx(ctx, ALLOC_TIMEOUT);
754+
if (!new_pkt) {
755+
return -ENOMEM;
756+
}
744757

745758
memcpy(new_pkt, pkt, sizeof(struct net_pkt));
746-
new_pkt->frags = net_pkt_copy_all(pkt, 0, K_FOREVER);
759+
new_pkt->frags = net_pkt_copy_all(pkt, 0,
760+
ALLOC_TIMEOUT);
761+
if (!new_pkt->frags) {
762+
net_pkt_unref(new_pkt);
763+
return -ENOMEM;
764+
}
747765

748766
NET_DBG("Copied %zu bytes from %p to %p",
749767
net_pkt_get_len(new_pkt), pkt, new_pkt);

0 commit comments

Comments
 (0)