Skip to content

Commit

Permalink
net: pkt: Add function net_pkt_get_contiguous_len()
Browse files Browse the repository at this point in the history
This returns the available contingous space in the packet starting from
the current cursor position.

Signed-off-by: Christian Taedcke <[email protected]>
  • Loading branch information
chrta authored and jukkar committed Feb 24, 2021
1 parent 5de3f85 commit c467149
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
10 changes: 10 additions & 0 deletions include/net/net_pkt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,16 @@ uint16_t net_pkt_get_current_offset(struct net_pkt *pkt);
*/
bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);

/**
* Get the contiguous buffer space
*
* @param pkt Network packet
*
* @return The available contiguous buffer space in bytes starting from the
* current cursor position. 0 in case of an error.
*/
size_t net_pkt_get_contiguous_len(struct net_pkt *pkt);

struct net_pkt_data_access {
#if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
void *data;
Expand Down
13 changes: 9 additions & 4 deletions subsys/net/ip/net_pkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,13 @@ uint16_t net_pkt_get_current_offset(struct net_pkt *pkt)
}

bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size)
{
size_t len = net_pkt_get_contiguous_len(pkt);

return len >= size;
}

size_t net_pkt_get_contiguous_len(struct net_pkt *pkt)
{
pkt_cursor_advance(pkt, !net_pkt_is_being_overwritten(pkt));

Expand All @@ -1967,12 +1974,10 @@ bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size)
len = net_pkt_is_being_overwritten(pkt) ?
pkt->cursor.buf->len : pkt->cursor.buf->size;
len -= pkt->cursor.pos - pkt->cursor.buf->data;
if (len >= size) {
return true;
}
return len;
}

return false;
return 0;
}

void *net_pkt_get_data(struct net_pkt *pkt,
Expand Down
65 changes: 64 additions & 1 deletion tests/net/net_pkt/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,68 @@ void test_net_pkt_headroom_copy(void)
net_pkt_unref(pkt_src);
}

static void test_net_pkt_get_contiguous_len(void)
{
size_t cont_len;
int res;
/* Allocate pkt with 2 fragments */
struct net_pkt *pkt = net_pkt_rx_alloc_with_buffer(
NULL, CONFIG_NET_BUF_DATA_SIZE * 2,
AF_UNSPEC, 0, K_NO_WAIT);

zassert_not_null(pkt, "Pkt not allocated");

net_pkt_cursor_init(pkt);

cont_len = net_pkt_get_contiguous_len(pkt);
zassert_equal(CONFIG_NET_BUF_DATA_SIZE, cont_len,
"Expected one complete available net_buf");

net_pkt_set_overwrite(pkt, false);

/* now write 3 byte into the pkt */
for (int i = 0; i < 3; ++i) {
res = net_pkt_write_u8(pkt, 0xAA);
zassert_equal(0, res, "Write packet failed");
}

cont_len = net_pkt_get_contiguous_len(pkt);
zassert_equal(CONFIG_NET_BUF_DATA_SIZE - 3, cont_len,
"Expected a three byte reduction");

/* Fill the first fragment up until only 3 bytes are free */
for (int i = 0; i < CONFIG_NET_BUF_DATA_SIZE - 6; ++i) {
res = net_pkt_write_u8(pkt, 0xAA);
zassert_equal(0, res, "Write packet failed");
}

cont_len = net_pkt_get_contiguous_len(pkt);
zassert_equal(3, cont_len, "Expected only three bytes are available");

/* Fill the complete first fragment, so the cursor points to the second
* fragment.
*/
for (int i = 0; i < 3; ++i) {
res = net_pkt_write_u8(pkt, 0xAA);
zassert_equal(0, res, "Write packet failed");
}

cont_len = net_pkt_get_contiguous_len(pkt);
zassert_equal(CONFIG_NET_BUF_DATA_SIZE, cont_len,
"Expected next full net_buf is available");

/* Fill the last fragment */
for (int i = 0; i < CONFIG_NET_BUF_DATA_SIZE; ++i) {
res = net_pkt_write_u8(pkt, 0xAA);
zassert_equal(0, res, "Write packet failed");
}

cont_len = net_pkt_get_contiguous_len(pkt);
zassert_equal(0, cont_len, "Expected no available space");

net_pkt_unref(pkt);
}

void test_main(void)
{
eth_if = net_if_get_default();
Expand All @@ -926,7 +988,8 @@ void test_main(void)
ztest_unit_test(test_net_pkt_pull),
ztest_unit_test(test_net_pkt_clone),
ztest_unit_test(test_net_pkt_headroom),
ztest_unit_test(test_net_pkt_headroom_copy)
ztest_unit_test(test_net_pkt_headroom_copy),
ztest_unit_test(test_net_pkt_get_contiguous_len)
);

ztest_run_test_suite(net_pkt_tests);
Expand Down

0 comments on commit c467149

Please sign in to comment.