Skip to content

Commit

Permalink
Add Z_FEATURE_TCP_NODELAY (#723)
Browse files Browse the repository at this point in the history
* refactor: recopy buffers instead of bytes in siphon

* feat: activate TCP_NO_DELAY
  • Loading branch information
jean-roland authored Oct 9, 2024
1 parent 8f56595 commit 64ae44c
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ set(Z_FEATURE_LINK_UDP_UNICAST 1 CACHE STRING "Toggle UDP unicast links")
set(Z_FEATURE_MULTICAST_TRANSPORT 1 CACHE STRING "Toggle multicast transport")
set(Z_FEATURE_UNICAST_TRANSPORT 1 CACHE STRING "Toggle unicast transport")
set(Z_FEATURE_RAWETH_TRANSPORT 0 CACHE STRING "Toggle raw ethernet transport")
set(Z_FEATURE_TCP_NODELAY 1 CACHE STRING "Toggle TCP_NODELAY")

add_compile_definitions("Z_BUILD_DEBUG=$<CONFIG:Debug>")
message(STATUS "Building with feature confing:\n\
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define Z_FEATURE_UNICAST_TRANSPORT 1
#define Z_FEATURE_FRAGMENTATION 1
#define Z_FEATURE_ENCODING_VALUES 1
#define Z_FEATURE_TCP_NODELAY 1
// End of CMake generation

/*------------------ Runtime configuration properties ------------------*/
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define Z_FEATURE_UNICAST_TRANSPORT @Z_FEATURE_UNICAST_TRANSPORT@
#define Z_FEATURE_FRAGMENTATION @Z_FEATURE_FRAGMENTATION@
#define Z_FEATURE_ENCODING_VALUES @Z_FEATURE_ENCODING_VALUES@
#define Z_FEATURE_TCP_NODELAY @Z_FEATURE_TCP_NODELAY@
// End of CMake generation

/*------------------ Runtime configuration properties ------------------*/
Expand Down
30 changes: 23 additions & 7 deletions src/protocol/iobuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,30 @@ _z_zbuf_t _z_wbuf_to_zbuf(const _z_wbuf_t *wbf) {

z_result_t _z_wbuf_siphon(_z_wbuf_t *dst, _z_wbuf_t *src, size_t length) {
z_result_t ret = _Z_RES_OK;

for (size_t i = 0; i < length; i++) {
ret = _z_wbuf_write(dst, _z_wbuf_read(src));
if (ret != _Z_RES_OK) {
break;
}
size_t llength = length;
_z_iosli_t *wios = _z_wbuf_get_iosli(dst, dst->_w_idx);
size_t writable = _z_iosli_writable(wios);

// Siphon does not work (as of now) on expandable dst buffers
if (writable >= length) {
do {
assert(src->_r_idx <= src->_w_idx);
_z_iosli_t *rios = _z_wbuf_get_iosli(src, src->_r_idx);
size_t readable = _z_iosli_readable(rios);
if (readable > (size_t)0) {
size_t to_read = (readable <= llength) ? readable : llength;
uint8_t *w_pos = _z_ptr_u8_offset(wios->_buf, (ptrdiff_t)wios->_w_pos);
(void)memcpy(w_pos, rios->_buf + rios->_r_pos, to_read);
rios->_r_pos = rios->_r_pos + to_read;
llength -= to_read;
wios->_w_pos += to_read;
} else {
src->_r_idx++;
}
} while (llength > (size_t)0);
} else {
ret = _Z_ERR_TRANSPORT_NO_SPACE;
}

return ret;
}

Expand Down
7 changes: 7 additions & 0 deletions src/system/arduino/esp32/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ z_result_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t re
ret = _Z_ERR_GENERIC;
}

#if Z_FEATURE_TCP_NODELAY == 1
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&optflag, sizeof(optflag)) < 0)) {
ret = _Z_ERR_GENERIC;
}
#endif

#if LWIP_SO_LINGER == 1
struct linger ling;
ling.l_onoff = 1;
Expand Down
7 changes: 7 additions & 0 deletions src/system/espidf/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ z_result_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t re
ret = _Z_ERR_GENERIC;
}

#if Z_FEATURE_TCP_NODELAY == 1
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&optflag, sizeof(optflag)) < 0)) {
ret = _Z_ERR_GENERIC;
}
#endif

#if LWIP_SO_LINGER == 1
struct linger ling;
ling.l_onoff = 1;
Expand Down
8 changes: 7 additions & 1 deletion src/system/unix/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
Expand Down Expand Up @@ -71,7 +72,12 @@ z_result_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t re
(setsockopt(sock->_fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags)) < 0)) {
ret = _Z_ERR_GENERIC;
}

#if Z_FEATURE_TCP_NODELAY == 1
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags)) < 0)) {
ret = _Z_ERR_GENERIC;
}
#endif
struct linger ling;
ling.l_onoff = 1;
ling.l_linger = Z_TRANSPORT_LEASE / 1000;
Expand Down
7 changes: 7 additions & 0 deletions src/system/windows/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ z_result_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t re
ret = _Z_ERR_GENERIC;
}

#if Z_FEATURE_TCP_NODELAY == 1
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_sock._fd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags)) < 0)) {
ret = _Z_ERR_GENERIC;
}
#endif

struct linger ling;
ling.l_onoff = 1;
ling.l_linger = Z_TRANSPORT_LEASE / 1000;
Expand Down
9 changes: 9 additions & 0 deletions src/system/zephyr/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <sys/socket.h>
#include <zephyr/net/net_if.h>

#include "zenoh-pico/collections/string.h"
#include "zenoh-pico/config.h"
Expand Down Expand Up @@ -68,6 +69,14 @@ z_result_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t re
// ret = _Z_ERR_GENERIC;
}

#if Z_FEATURE_TCP_NODELAY == 1
int optflag = 1;
if ((ret == _Z_RES_OK) &&
(setsockopt(sock->_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&optflag, sizeof(optflag)) < 0)) {
ret = _Z_ERR_GENERIC;
}
#endif

#if LWIP_SO_LINGER == 1
struct linger ling;
ling.l_onoff = 1;
Expand Down
2 changes: 2 additions & 0 deletions src/transport/multicast/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ z_result_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t
if (ret == _Z_RES_OK) {
ztm->_transmitted = true; // Mark the session that we have transmitted data
}
} else {
_Z_ERROR("Fragment serialization failed with err %d", ret);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/transport/unicast/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ z_result_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n
if (ret == _Z_RES_OK) {
ztu->_transmitted = true; // Mark the session that we have transmitted data
}
} else {
_Z_ERROR("Fragment serialization failed with err %d", ret);
}
}
}
Expand Down

0 comments on commit 64ae44c

Please sign in to comment.