Skip to content

Commit

Permalink
net: buf: Use unified k_fifo API for FIFOs
Browse files Browse the repository at this point in the history
Only net_receive() is using non-special value for net_buf_get_timeout
so this change is included here. Other users are using special values
which are already correctly handling ticks vs ms change.

Change-Id: Ib12d34ac5a546b36fa7b35615f082c82a256bd07
Signed-off-by: Szymon Janc <[email protected]>
  • Loading branch information
Szymon Janc authored and Johan Hedberg committed Nov 11, 2016
1 parent 051f97b commit 2f45fab
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 47 deletions.
25 changes: 12 additions & 13 deletions include/net/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

#include <stddef.h>
#include <stdint.h>
#include <toolchain.h>
#include <misc/util.h>
#include <nanokernel.h>
#include <zephyr.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -407,7 +406,7 @@ struct net_buf {
uint8_t flags;

/** Where the buffer should go when freed up. */
struct nano_fifo * const free;
struct k_fifo * const free;

/** Function to be called when the buffer is freed. */
void (*const destroy)(struct net_buf *buf);
Expand Down Expand Up @@ -447,7 +446,7 @@ struct net_buf {
*
* If provided with a custom destroy callback this callback is
* responsible for eventually returning the buffer back to the free
* buffers FIFO through nano_fifo_put(buf->free, buf).
* buffers FIFO through k_fifo_put(buf->free, buf).
*
* @param _name Name of buffer pool.
* @param _count Number of buffers in the pool.
Expand Down Expand Up @@ -483,10 +482,10 @@ struct net_buf {
do { \
int i; \
\
nano_fifo_init(pool[0].buf.free); \
k_fifo_init(pool[0].buf.free); \
\
for (i = 0; i < ARRAY_SIZE(pool); i++) { \
nano_fifo_put(pool[i].buf.free, &pool[i]); \
k_fifo_put(pool[i].buf.free, &pool[i]); \
} \
} while (0)

Expand All @@ -506,9 +505,9 @@ struct net_buf {
* @warning If there are no available buffers and the function is
* called from a task or fiber the call will block until a buffer
* becomes available in the FIFO. If you want to make sure no blocking
* happens use net_buf_get_timeout() instead with TICKS_NONE.
* happens use net_buf_get_timeout() instead with K_NO_WAIT.
*/
struct net_buf *net_buf_get(struct nano_fifo *fifo, size_t reserve_head);
struct net_buf *net_buf_get(struct k_fifo *fifo, size_t reserve_head);

/**
* @brief Get a new buffer from a FIFO.
Expand All @@ -521,13 +520,13 @@ struct net_buf *net_buf_get(struct nano_fifo *fifo, size_t reserve_head);
* @param fifo Which FIFO to take the buffer from.
* @param reserve_head How much headroom to reserve.
* @param timeout Affects the action taken should the FIFO be empty.
* If TICKS_NONE, then return immediately. If TICKS_UNLIMITED, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of ticks before timing out.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then wait as
* long as necessary. Otherwise, wait up to the specified number of
* miliseconds before timing out.
*
* @return New buffer or NULL if out of buffers.
*/
struct net_buf *net_buf_get_timeout(struct nano_fifo *fifo,
struct net_buf *net_buf_get_timeout(struct k_fifo *fifo,
size_t reserve_head, int32_t timeout);

/**
Expand All @@ -551,7 +550,7 @@ void net_buf_reserve(struct net_buf *buf, size_t reserve);
* @param fifo Which FIFO to put the buffer to.
* @param buf Buffer.
*/
void net_buf_put(struct nano_fifo *fifo, struct net_buf *buf);
void net_buf_put(struct k_fifo *fifo, struct net_buf *buf);

/**
* @brief Decrements the reference count of a buffer.
Expand Down
20 changes: 9 additions & 11 deletions net/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* limitations under the License.
*/

#include <nanokernel.h>
#include <toolchain.h>
#include <stdio.h>
#include <errno.h>
#include <stddef.h>
Expand Down Expand Up @@ -50,15 +48,15 @@
#define NET_BUF_ASSERT(cond)
#endif /* CONFIG_NET_BUF_DEBUG */

struct net_buf *net_buf_get_timeout(struct nano_fifo *fifo,
struct net_buf *net_buf_get_timeout(struct k_fifo *fifo,
size_t reserve_head, int32_t timeout)
{
struct net_buf *buf, *frag;

NET_BUF_DBG("fifo %p reserve %u timeout %d", fifo, reserve_head,
timeout);

buf = nano_fifo_get(fifo, timeout);
buf = k_fifo_get(fifo, timeout);
if (!buf) {
NET_BUF_ERR("Failed to get free buffer");
return NULL;
Expand All @@ -82,7 +80,7 @@ struct net_buf *net_buf_get_timeout(struct nano_fifo *fifo,

/* Get any fragments belonging to this buffer */
for (frag = buf; (frag->flags & NET_BUF_FRAGS); frag = frag->frags) {
frag->frags = nano_fifo_get(fifo, TICKS_NONE);
frag->frags = k_fifo_get(fifo, K_NO_WAIT);
NET_BUF_ASSERT(frag->frags);

/* The fragments flag is only for FIFO-internal usage */
Expand All @@ -95,20 +93,20 @@ struct net_buf *net_buf_get_timeout(struct nano_fifo *fifo,
return buf;
}

struct net_buf *net_buf_get(struct nano_fifo *fifo, size_t reserve_head)
struct net_buf *net_buf_get(struct k_fifo *fifo, size_t reserve_head)
{
struct net_buf *buf;

NET_BUF_DBG("fifo %p reserve %u", fifo, reserve_head);

buf = net_buf_get_timeout(fifo, reserve_head, TICKS_NONE);
buf = net_buf_get_timeout(fifo, reserve_head, K_NO_WAIT);
if (buf || k_is_in_isr()) {
return buf;
}

NET_BUF_WARN("Low on buffers. Waiting (fifo %p)", fifo);

return net_buf_get_timeout(fifo, reserve_head, TICKS_UNLIMITED);
return net_buf_get_timeout(fifo, reserve_head, K_FOREVER);
}

void net_buf_reserve(struct net_buf *buf, size_t reserve)
Expand All @@ -119,15 +117,15 @@ void net_buf_reserve(struct net_buf *buf, size_t reserve)
buf->data = buf->__buf + reserve;
}

void net_buf_put(struct nano_fifo *fifo, struct net_buf *buf)
void net_buf_put(struct k_fifo *fifo, struct net_buf *buf)
{
struct net_buf *tail;

for (tail = buf; tail->frags; tail = tail->frags) {
tail->flags |= NET_BUF_FRAGS;
}

nano_fifo_put_list(fifo, buf, tail);
k_fifo_put_list(fifo, buf, tail);
}

void net_buf_unref(struct net_buf *buf)
Expand All @@ -144,7 +142,7 @@ void net_buf_unref(struct net_buf *buf)
if (buf->destroy) {
buf->destroy(buf);
} else {
nano_fifo_put(buf->free, buf);
k_fifo_put(buf->free, buf);
}

buf = frags;
Expand Down
2 changes: 1 addition & 1 deletion net/ip/net_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ struct net_buf *net_receive(struct net_context *context, int32_t timeout)
switch (timeout) {
case TICKS_UNLIMITED:
case TICKS_NONE:
buf = net_buf_get_timeout(rx_queue, 0, timeout);
buf = net_buf_get_timeout(rx_queue, 0, _ticks_to_ms(timeout));
break;
default:
#ifdef CONFIG_NANO_TIMEOUTS
Expand Down
16 changes: 8 additions & 8 deletions samples/testing/unit/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@

#include <net/buf.c>

void nano_fifo_init(struct nano_fifo *fifo) {}
void nano_fifo_put_list(struct nano_fifo *fifo, void *head, void *tail) {}
void k_fifo_init(struct k_fifo *fifo) {}
void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail) {}

int k_is_in_isr(void)
{
return 0;
}

void *nano_fifo_get(struct nano_fifo *fifo, int32_t timeout)
void *k_fifo_get(struct k_fifo *fifo, int32_t timeout)
{
return ztest_get_return_value_ptr();
}

void nano_fifo_put(struct nano_fifo *fifo, void *data)
void k_fifo_put(struct k_fifo *fifo, void *data)
{
ztest_check_expected_value(data);
}

#define BUF_COUNT 1
#define BUF_SIZE 74

static struct nano_fifo bufs_fifo;
static struct k_fifo bufs_fifo;
static NET_BUF_POOL(bufs_pool, BUF_COUNT, BUF_SIZE, &bufs_fifo,
NULL, sizeof(int));

static void init_pool(void)
{
ztest_expect_value(nano_fifo_put, data, &bufs_pool);
ztest_expect_value(k_fifo_put, data, &bufs_pool);
net_buf_pool_init(bufs_pool);
}

Expand All @@ -55,8 +55,8 @@ static void test_get_single_buffer(void)

init_pool();

ztest_returns_value(nano_fifo_get, bufs_pool);
buf = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
ztest_returns_value(k_fifo_get, bufs_pool);
buf = net_buf_get_timeout(&bufs_fifo, 0, K_NO_WAIT);

assert_equal_ptr(buf, &bufs_pool[0], "Returned buffer not from pool");
assert_equal(buf->ref, 1, "Invalid refcount");
Expand Down
12 changes: 6 additions & 6 deletions tests/net/buf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static void net_buf_test_1(void)
int i;

for (i = 0; i < ARRAY_SIZE(bufs_pool); i++) {
buf = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
buf = net_buf_get_timeout(&bufs_fifo, 0, K_NO_WAIT);
assert_not_null(buf, "Failed to get buffer");
bufs[i] = buf;
}
Expand All @@ -128,19 +128,19 @@ static void net_buf_test_2(void)
struct nano_fifo fifo;
int i;

head = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
head = net_buf_get_timeout(&bufs_fifo, 0, K_NO_WAIT);
assert_not_null(head, "Failed to get fragment list head");

frag = head;
for (i = 0; i < ARRAY_SIZE(bufs_pool) - 1; i++) {
frag->frags = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
frag->frags = net_buf_get_timeout(&bufs_fifo, 0, K_NO_WAIT);
assert_not_null(frag->frags, "Failed to get fragment");
frag = frag->frags;
}

nano_fifo_init(&fifo);
net_buf_put(&fifo, head);
head = net_buf_get_timeout(&fifo, 0, TICKS_NONE);
head = net_buf_get_timeout(&fifo, 0, K_NO_WAIT);

destroy_called = 0;
net_buf_unref(head);
Expand Down Expand Up @@ -175,12 +175,12 @@ static void net_buf_test_3(void)
struct nano_sem sema;
int i;

head = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
head = net_buf_get_timeout(&bufs_fifo, 0, K_NO_WAIT);
assert_not_null(head, "Failed to get fragment list head");

frag = head;
for (i = 0; i < ARRAY_SIZE(bufs_pool) - 1; i++) {
frag->frags = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
frag->frags = net_buf_get_timeout(&bufs_fifo, 0, K_NO_WAIT);
assert_not_null(frag->frags, "Failed to get fragment");
frag = frag->frags;
}
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/net/buf/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@

#include <net/buf.c>

void nano_fifo_init(struct nano_fifo *fifo) {}
void nano_fifo_put_list(struct nano_fifo *fifo, void *head, void *tail) {}
void k_fifo_init(struct k_fifo *fifo) {}
void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail) {}

int k_is_in_isr(void)
{
return 0;
}

void *nano_fifo_get(struct nano_fifo *fifo, int32_t timeout)
void *k_fifo_get(struct k_fifo *fifo, int32_t timeout)
{
return ztest_get_return_value_ptr();
}

void nano_fifo_put(struct nano_fifo *fifo, void *data)
void k_fifo_put(struct k_fifo *fifo, void *data)
{
ztest_check_expected_value(data);
}

#define BUF_COUNT 1
#define BUF_SIZE 74

static struct nano_fifo bufs_fifo;
static struct k_fifo bufs_fifo;
static NET_BUF_POOL(bufs_pool, BUF_COUNT, BUF_SIZE, &bufs_fifo,
NULL, sizeof(int));

static void init_pool(void)
{
ztest_expect_value(nano_fifo_put, data, &bufs_pool);
ztest_expect_value(k_fifo_put, data, &bufs_pool);
net_buf_pool_init(bufs_pool);
}

Expand All @@ -55,8 +55,8 @@ static void test_get_single_buffer(void)

init_pool();

ztest_returns_value(nano_fifo_get, bufs_pool);
buf = net_buf_get_timeout(&bufs_fifo, 0, TICKS_NONE);
ztest_returns_value(k_fifo_get, bufs_pool);
buf = net_buf_get_timeout(&bufs_fifo, 0, K_NO_WAIT);

assert_equal_ptr(buf, &bufs_pool[0], "Returned buffer not from pool");
assert_equal(buf->ref, 1, "Invalid refcount");
Expand Down

0 comments on commit 2f45fab

Please sign in to comment.