# FreeRTOS - TCP/IP: UDP socket buffer size?

**URL:** <https://forums.freertos.org/t/freertos-tcp-ip-udp-socket-buffer-size/7230>\
**Category:** Kernel\
**Created:** [May 28, 2018, 1:58pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-udp-socket-buffer-size/7230 "2018-05-28T13:58:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 28, 2018, 1:58pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-udp-socket-buffer-size/7230/1 "2018-05-28T13:58:07Z")

</div>

**[kenchang1](https://sourceforge.net/u/kenchang1/)** wrote on [Monday, May 28, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/56c5de95/#a531):

For TCP sockets I can find “ipconfigTCP\_RX\_BUFFER\_LENGTH” for the receive buffer size.

Is there a similar parameter for UDP sockets? Or is it only this one: ipconfigNUM\_NETWORK\_BUFFER\_DESCRIPTORS ?

regards,

Ken

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [May 28, 2018, 2:12pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-udp-socket-buffer-size/7230/2 "2018-05-28T14:12:19Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Monday, May 28, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/56c5de95/#4ddc):

Interesting question!

The number of UDP messages that you can receive without reading them, is indeed  
limited by `ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS`.

For TCP a circular “stream buffer” is created for each direction. TCP data must be  
buffered, and kept in a buffer until the transmission has been acknowledged by the  
peer.  
This TCP housekeeping of data is complicated. I tried to make clear how it works in 2  
pictures ( attached to this post ).

The implementation of UDP sockets is a lot simpler. It has a list ( `List_t` ) of Network Buffers that were received. Note that `FreeRTOS_recvfrom()` has a zero-copy possibility, so that you can peek directly into a Network Buffer.

There is an important setting for UDP sockets, limiting the maximum number of received packets:

```auto
#ifndef ipconfigUDP_MAX_RX_PACKETS
    /* Make positive to define the maximum number of packets
     * which will be buffered for each UDP socket. Can be
     * overridden with the socket option FREERTOS_SO_UDP_MAX_RX_PACKETS
     */
    #define ipconfigUDP_MAX_RX_PACKETS 0u
#endif

```

Suppose that you get a UDP attack: thousands of packets are fired to your UDP  
port, the attack will not disturb the device because at most `ipconfigUDP_MAX_RX_PACKETS`  
network buffers will be stored.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 28, 2018, 2:46pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-udp-socket-buffer-size/7230/3 "2018-05-28T14:46:31Z")

</div>

**[kenchang1](https://sourceforge.net/u/kenchang1/)** wrote on [Monday, May 28, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/56c5de95/#dea9):

Hi Hein,

Thanks for the answer. I have an additional question. How are these 2 parameters related, ipconfigUDP\_MAX\_RX\_PACKETS and ipconfigNUM\_NETWORK\_BUFFER\_DESCRIPTORS?

When I increase ipconfigUDP\_MAX\_RX\_PACKETS from (3) to (10), I get an Assertion fail:  
“Assertion failed in file …/src/FreeRTOS-Plus-TCP-multi/FreeRTOS\_IP.c, line 1610”.

Line 1610 of FreeRTOS\_IP.c is this part of the code:

```auto
static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
{
EthernetHeader_t *pxEthernetHeader;
volatile eFrameProcessingResult_t eReturned; /* Volatile to prevent complier warnings when ipCONSIDER_FRAME_FOR_PROCESSING just sets it to eProcessBuffer. */

	configASSERT( pxNetworkBuffer );

```

regards,  
Ken

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 5, 2018, 12:12pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-udp-socket-buffer-size/7230/4 "2018-06-05T12:12:28Z")

</div>

**[kenchang1](https://sourceforge.net/u/kenchang1/)** wrote on [Tuesday, June 05, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/56c5de95/#2811):

The Assert fail was caused by 2 globally declared network pointer buffers in x\_emacpsif\_dma.c:

```auto
static NetworkBufferDescriptor_t *ethMsg = NULL;
static NetworkBufferDescriptor_t *ethLast = NULL;

```

Fixed by making them local.

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [June 7, 2018, 2:14pm UTC](https://forums.freertos.org/t/freertos-tcp-ip-udp-socket-buffer-size/7230/5 "2018-06-07T14:14:19Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Thursday, June 07, 2018](https://sourceforge.net/p/freertos/discussion/382005/thread/56c5de95/#8690):

Ken, thanks a lot for your big effort to find the cause of this problem.

The driver `x_emacpsif_dma.c` worked fine as long as it handled a single NIC. You found that when it serves two EMAC’s, corruption may occur. So the problem was only present in the **/multi** version.

My stupid: two tasks were storing temporary data in common variables.

These simple changes were made to avoid mentioned problem:

Around line 320:

```auto
-static NetworkBufferDescriptor_t *ethMsg = NULL;
-static NetworkBufferDescriptor_t *ethLast = NULL;
-
-static void passEthMessages( )
+static void passEthMessages( NetworkBufferDescriptor_t *ethMsg )

```

Around line 346:

```auto
 int emacps_check_rx( xemacpsif_s *xemacpsif )
 {
 NetworkBufferDescriptor_t *pxBuffer, *pxNewBuffer;
 int rx_bytes;
 volatile int msgCount = 0;
 int head = xemacpsif->rxHead;
 BaseType_t bHasDataPacket = pdFALSE;
+NetworkBufferDescriptor_t *ethMsg = NULL;
+NetworkBufferDescriptor_t *ethLast = NULL;
    <snip>
- passEthMessages( );
+ passEthMessages( ethMsg );

```
