net/net_pkt: Fully separate struct net_pkt from struct net_buf

- net_pkt becomes a stand-alone structure with network packet meta
  information.
- network packet data is still managed through net_buf, mostly named
  'frag'.
- net_pkt memory management is done through k_mem_slab
- function got introduced or relevantly renamed to target eithe net_pkt
  or net_buf fragments.
- net_buf's sent_list ends up in net_pkt now, and thus helps to save
  memory when TCP is enabled.

Change-Id: Ibd5c17df4f75891dec79db723a4c9fc704eb843d
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
This commit is contained in:
Tomasz Bursztyka 2017-04-05 08:37:44 +02:00 committed by Jukka Rissanen
commit db11fcd174
122 changed files with 5510 additions and 5480 deletions

View file

@ -80,7 +80,7 @@ static K_TIMER_DEFINE(send_timer, telnet_send_prematurely, NULL);
/* For now we handle a unique telnet client connection */ /* For now we handle a unique telnet client connection */
static struct net_context *client_cnx; static struct net_context *client_cnx;
static struct net_buf *out_buf; static struct net_pkt *out_pkt;
static int (*orig_printk_hook)(int); static int (*orig_printk_hook)(int);
static struct k_fifo *avail_queue; static struct k_fifo *avail_queue;
@ -116,17 +116,17 @@ static void telnet_end_client_connection(void)
net_context_put(client_cnx); net_context_put(client_cnx);
client_cnx = NULL; client_cnx = NULL;
if (out_buf) { if (out_pkt) {
net_buf_unref(out_buf); net_pkt_unref(out_pkt);
} }
telnet_rb_init(); telnet_rb_init();
} }
static int telnet_setup_out_buf(struct net_context *client) static int telnet_setup_out_pkt(struct net_context *client)
{ {
out_buf = net_pkt_get_tx(client, K_FOREVER); out_pkt = net_pkt_get_tx(client, K_FOREVER);
if (!out_buf) { if (!out_pkt) {
/* Cannot happen atm, net_pkt waits indefinitely */ /* Cannot happen atm, net_pkt waits indefinitely */
return -ENOBUFS; return -ENOBUFS;
} }
@ -225,7 +225,7 @@ static void telnet_sent_cb(struct net_context *client,
{ {
if (status) { if (status) {
telnet_end_client_connection(); telnet_end_client_connection();
SYS_LOG_ERR("Could not sent last buffer"); SYS_LOG_ERR("Could not sent last packet");
} }
} }
@ -234,14 +234,14 @@ static inline bool telnet_send(void)
struct line_buf *lb = telnet_rb_get_line_out(); struct line_buf *lb = telnet_rb_get_line_out();
if (lb) { if (lb) {
net_pkt_append(out_buf, lb->len, lb->buf, K_FOREVER); net_pkt_append(out_pkt, lb->len, lb->buf, K_FOREVER);
/* We reinitialize the line buffer */ /* We reinitialize the line buffer */
lb->len = 0; lb->len = 0;
if (net_context_send(out_buf, telnet_sent_cb, if (net_context_send(out_pkt, telnet_sent_cb,
K_NO_WAIT, NULL, NULL) || K_NO_WAIT, NULL, NULL) ||
telnet_setup_out_buf(client_cnx)) { telnet_setup_out_pkt(client_cnx)) {
return false; return false;
} }
} }
@ -258,12 +258,12 @@ static int telnet_console_out_nothing(int c)
static inline void telnet_command_send_reply(uint8_t *msg, uint16_t len) static inline void telnet_command_send_reply(uint8_t *msg, uint16_t len)
{ {
net_pkt_append(out_buf, len, msg, K_FOREVER); net_pkt_append(out_pkt, len, msg, K_FOREVER);
net_context_send(out_buf, telnet_sent_cb, net_context_send(out_pkt, telnet_sent_cb,
K_NO_WAIT, NULL, NULL); K_NO_WAIT, NULL, NULL);
telnet_setup_out_buf(client_cnx); telnet_setup_out_pkt(client_cnx);
} }
static inline void telnet_reply_ay_command(void) static inline void telnet_reply_ay_command(void)
@ -326,10 +326,10 @@ out:
#define telnet_reply_command() #define telnet_reply_command()
#endif /* CONFIG_TELNET_CONSOLE_SUPPORT_COMMAND */ #endif /* CONFIG_TELNET_CONSOLE_SUPPORT_COMMAND */
static inline bool telnet_handle_command(struct net_buf *buf) static inline bool telnet_handle_command(struct net_pkt *pkt)
{ {
struct telnet_simple_command *cmd = struct telnet_simple_command *cmd =
(struct telnet_simple_command *)net_pkt_appdata(buf); (struct telnet_simple_command *)net_pkt_appdata(pkt);
if (cmd->iac != NVT_CMD_IAC) { if (cmd->iac != NVT_CMD_IAC) {
return false; return false;
@ -351,17 +351,17 @@ static inline bool telnet_handle_command(struct net_buf *buf)
return true; return true;
} }
static inline void telnet_handle_input(struct net_buf *buf) static inline void telnet_handle_input(struct net_pkt *pkt)
{ {
struct console_input *input; struct console_input *input;
uint16_t len, offset, pos; uint16_t len, offset, pos;
len = net_pkt_appdatalen(buf); len = net_pkt_appdatalen(pkt);
if (len > CONSOLE_MAX_LINE_LEN || len < TELNET_MIN_MSG) { if (len > CONSOLE_MAX_LINE_LEN || len < TELNET_MIN_MSG) {
return; return;
} }
if (telnet_handle_command(buf)) { if (telnet_handle_command(pkt)) {
return; return;
} }
@ -374,8 +374,8 @@ static inline void telnet_handle_input(struct net_buf *buf)
return; return;
} }
offset = net_buf_frags_len(buf) - len; offset = net_pkt_get_len(pkt) - len;
net_pkt_read(buf->frags, offset, &pos, len, input->line); net_frag_read(pkt->frags, offset, &pos, len, input->line);
/* LF/CR will be removed if only the line is not NUL terminated */ /* LF/CR will be removed if only the line is not NUL terminated */
if (input->line[len-1] != NVT_NUL) { if (input->line[len-1] != NVT_NUL) {
@ -392,11 +392,11 @@ static inline void telnet_handle_input(struct net_buf *buf)
} }
static void telnet_recv(struct net_context *client, static void telnet_recv(struct net_context *client,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
if (!buf || status) { if (!pkt || status) {
telnet_end_client_connection(); telnet_end_client_connection();
SYS_LOG_DBG("Telnet client dropped (AF_INET%s) status %d", SYS_LOG_DBG("Telnet client dropped (AF_INET%s) status %d",
@ -405,9 +405,9 @@ static void telnet_recv(struct net_context *client,
return; return;
} }
telnet_handle_input(buf); telnet_handle_input(pkt);
net_buf_unref(buf); net_pkt_unref(pkt);
} }
/* Telnet server loop, used to send buffered output in the RB */ /* Telnet server loop, used to send buffered output in the RB */
@ -446,7 +446,7 @@ static void telnet_accept(struct net_context *client,
goto error; goto error;
} }
if (telnet_setup_out_buf(client)) { if (telnet_setup_out_pkt(client)) {
goto error; goto error;
} }

View file

@ -424,7 +424,7 @@ static int eth_enc28j60_init(struct device *dev)
return 0; return 0;
} }
static int eth_enc28j60_tx(struct device *dev, struct net_buf *buf, static int eth_enc28j60_tx(struct device *dev, struct net_pkt *pkt,
uint16_t len) uint16_t len)
{ {
struct eth_enc28j60_runtime *context = dev->driver_data; struct eth_enc28j60_runtime *context = dev->driver_data;
@ -461,13 +461,13 @@ static int eth_enc28j60_tx(struct device *dev, struct net_buf *buf,
per_packet_control = ENC28J60_PPCTL_BYTE; per_packet_control = ENC28J60_PPCTL_BYTE;
eth_enc28j60_write_mem(dev, &per_packet_control, 1); eth_enc28j60_write_mem(dev, &per_packet_control, 1);
for (frag = buf->frags; frag; frag = frag->frags) { for (frag = pkt->frags; frag; frag = frag->frags) {
uint8_t *data_ptr; uint8_t *data_ptr;
uint16_t data_len; uint16_t data_len;
if (first_frag) { if (first_frag) {
data_ptr = net_pkt_ll(buf); data_ptr = net_pkt_ll(pkt);
data_len = net_pkt_ll_reserve(buf) + frag->len; data_len = net_pkt_ll_reserve(pkt) + frag->len;
first_frag = false; first_frag = false;
} else { } else {
data_ptr = frag->data; data_ptr = frag->data;
@ -522,10 +522,9 @@ static int eth_enc28j60_rx(struct device *dev)
k_sem_take(&context->tx_rx_sem, K_FOREVER); k_sem_take(&context->tx_rx_sem, K_FOREVER);
do { do {
struct net_buf *last_frag;
struct net_buf *pkt_buf = NULL; struct net_buf *pkt_buf = NULL;
uint16_t frm_len = 0; uint16_t frm_len = 0;
struct net_buf *buf; struct net_pkt *pkt;
uint16_t next_packet; uint16_t next_packet;
uint8_t np[2]; uint8_t np[2];
@ -552,32 +551,29 @@ static int eth_enc28j60_rx(struct device *dev)
lengthfr = frm_len; lengthfr = frm_len;
/* Get the frame from the buffer */ /* Get the frame from the buffer */
buf = net_pkt_get_reserve_rx(0, config->timeout); pkt = net_pkt_get_reserve_rx(0, config->timeout);
if (!buf) { if (!pkt) {
SYS_LOG_ERR("Could not allocate rx buffer"); SYS_LOG_ERR("Could not allocate rx buffer");
goto done; goto done;
} }
last_frag = buf;
do { do {
size_t frag_len; size_t frag_len;
uint8_t *data_ptr; uint8_t *data_ptr;
size_t spi_frame_len; size_t spi_frame_len;
/* Reserve a data frag to receive the frame */ /* Reserve a data frag to receive the frame */
pkt_buf = net_pkt_get_frag(buf, config->timeout); pkt_buf = net_pkt_get_frag(pkt, config->timeout);
if (!pkt_buf) { if (!pkt_buf) {
SYS_LOG_ERR("Could not allocate data buffer"); SYS_LOG_ERR("Could not allocate data buffer");
net_buf_unref(buf); net_pkt_unref(pkt);
goto done; goto done;
} }
net_buf_frag_insert(last_frag, pkt_buf); net_pkt_frag_insert(pkt, pkt_buf);
data_ptr = pkt_buf->data;
last_frag = pkt_buf; data_ptr = pkt_buf->data;
/* Review the space available for the new frag */ /* Review the space available for the new frag */
frag_len = net_buf_tailroom(pkt_buf); frag_len = net_buf_tailroom(pkt_buf);
@ -608,7 +604,7 @@ static int eth_enc28j60_rx(struct device *dev)
/* Feed buffer frame to IP stack */ /* Feed buffer frame to IP stack */
SYS_LOG_DBG("Received packet of length %u", lengthfr); SYS_LOG_DBG("Received packet of length %u", lengthfr);
net_recv_data(context->iface, buf); net_recv_data(context->iface, pkt);
done: done:
/* Free buffer memory and decrement rx counter */ /* Free buffer memory and decrement rx counter */
eth_enc28j60_set_bank(dev, ENC28J60_REG_ERXRDPTL); eth_enc28j60_set_bank(dev, ENC28J60_REG_ERXRDPTL);
@ -654,16 +650,16 @@ static void enc28j60_thread_main(void *arg1, void *unused1, void *unused2)
} }
} }
static int eth_net_tx(struct net_if *iface, struct net_buf *buf) static int eth_net_tx(struct net_if *iface, struct net_pkt *pkt)
{ {
uint16_t len = net_pkt_ll_reserve(buf) + net_buf_frags_len(buf); uint16_t len = net_pkt_ll_reserve(pkt) + net_pkt_get_len(pkt);
int ret; int ret;
SYS_LOG_DBG("buf %p (len %u)", buf, len); SYS_LOG_DBG("pkt %p (len %u)", pkt, len);
ret = eth_enc28j60_tx(iface->dev, buf, len); ret = eth_enc28j60_tx(iface->dev, pkt, len);
if (ret == 0) { if (ret == 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return ret; return ret;

View file

@ -306,7 +306,7 @@ static void eth_mcux_delayed_phy_work(struct k_work *item)
eth_mcux_phy_event(context); eth_mcux_phy_event(context);
} }
static int eth_tx(struct net_if *iface, struct net_buf *buf) static int eth_tx(struct net_if *iface, struct net_pkt *pkt)
{ {
struct eth_context *context = iface->dev->driver_data; struct eth_context *context = iface->dev->driver_data;
const struct net_buf *frag; const struct net_buf *frag;
@ -314,7 +314,7 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf)
status_t status; status_t status;
unsigned int imask; unsigned int imask;
uint16_t total_len = net_pkt_ll_reserve(buf) + net_buf_frags_len(buf); uint16_t total_len = net_pkt_ll_reserve(pkt) + net_pkt_get_len(pkt);
k_sem_take(&context->tx_buf_sem, K_FOREVER); k_sem_take(&context->tx_buf_sem, K_FOREVER);
@ -329,12 +329,12 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf)
* in our case) headers and must be treated specially. * in our case) headers and must be treated specially.
*/ */
dst = context->frame_buf; dst = context->frame_buf;
memcpy(dst, net_pkt_ll(buf), memcpy(dst, net_pkt_ll(pkt),
net_pkt_ll_reserve(buf) + buf->frags->len); net_pkt_ll_reserve(pkt) + pkt->frags->len);
dst += net_pkt_ll_reserve(buf) + buf->frags->len; dst += net_pkt_ll_reserve(pkt) + pkt->frags->len;
/* Continue with the rest of fragments (which contain only data) */ /* Continue with the rest of fragments (which contain only data) */
frag = buf->frags->frags; frag = pkt->frags->frags;
while (frag) { while (frag) {
memcpy(dst, frag->data, frag->len); memcpy(dst, frag->data, frag->len);
dst += frag->len; dst += frag->len;
@ -351,14 +351,14 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf)
return -1; return -1;
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
return 0; return 0;
} }
static void eth_rx(struct device *iface) static void eth_rx(struct device *iface)
{ {
struct eth_context *context = iface->driver_data; struct eth_context *context = iface->driver_data;
struct net_buf *buf, *prev_frag; struct net_pkt *pkt;
const uint8_t *src; const uint8_t *src;
uint32_t frame_length = 0; uint32_t frame_length = 0;
status_t status; status_t status;
@ -381,8 +381,8 @@ static void eth_rx(struct device *iface)
return; return;
} }
buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT);
if (!buf) { if (!pkt) {
/* We failed to get a receive buffer. We don't add /* We failed to get a receive buffer. We don't add
* any further logging here because the allocator * any further logging here because the allocator
* issued a diagnostic when it failed to allocate. * issued a diagnostic when it failed to allocate.
@ -398,7 +398,7 @@ static void eth_rx(struct device *iface)
if (sizeof(context->frame_buf) < frame_length) { if (sizeof(context->frame_buf) < frame_length) {
SYS_LOG_ERR("frame too large (%d)\n", frame_length); SYS_LOG_ERR("frame too large (%d)\n", frame_length);
net_pkt_unref(buf); net_pkt_unref(pkt);
status = ENET_ReadFrame(ENET, &context->enet_handle, NULL, 0); status = ENET_ReadFrame(ENET, &context->enet_handle, NULL, 0);
assert(status == kStatus_Success); assert(status == kStatus_Success);
return; return;
@ -414,27 +414,25 @@ static void eth_rx(struct device *iface)
if (status) { if (status) {
irq_unlock(imask); irq_unlock(imask);
SYS_LOG_ERR("ENET_ReadFrame failed: %d\n", status); SYS_LOG_ERR("ENET_ReadFrame failed: %d\n", status);
net_pkt_unref(buf); net_pkt_unref(pkt);
return; return;
} }
src = context->frame_buf; src = context->frame_buf;
prev_frag = buf;
do { do {
struct net_buf *pkt_buf; struct net_buf *pkt_buf;
size_t frag_len; size_t frag_len;
pkt_buf = net_pkt_get_frag(buf, K_NO_WAIT); pkt_buf = net_pkt_get_frag(pkt, K_NO_WAIT);
if (!pkt_buf) { if (!pkt_buf) {
irq_unlock(imask); irq_unlock(imask);
SYS_LOG_ERR("Failed to get fragment buf\n"); SYS_LOG_ERR("Failed to get fragment buf\n");
net_pkt_unref(buf); net_pkt_unref(pkt);
assert(status == kStatus_Success); assert(status == kStatus_Success);
return; return;
} }
net_buf_frag_insert(prev_frag, pkt_buf); net_pkt_frag_insert(pkt, pkt_buf);
prev_frag = pkt_buf;
frag_len = net_buf_tailroom(pkt_buf); frag_len = net_buf_tailroom(pkt_buf);
if (frag_len > frame_length) { if (frag_len > frame_length) {
frag_len = frame_length; frag_len = frame_length;
@ -448,8 +446,8 @@ static void eth_rx(struct device *iface)
irq_unlock(imask); irq_unlock(imask);
if (net_recv_data(context->iface, buf) < 0) { if (net_recv_data(context->iface, pkt) < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} }

View file

@ -77,7 +77,7 @@ static struct gmac_desc tx_desc_que12[PRIORITY_QUEUE_DESC_COUNT]
/* RX buffer accounting list */ /* RX buffer accounting list */
static struct net_buf *rx_buf_list_que0[MAIN_QUEUE_RX_DESC_COUNT]; static struct net_buf *rx_buf_list_que0[MAIN_QUEUE_RX_DESC_COUNT];
/* TX frames accounting list */ /* TX frames accounting list */
static struct net_buf *tx_frame_list_que0[CONFIG_NET_PKT_TX_COUNT + 1]; static struct net_pkt *tx_frame_list_que0[CONFIG_NET_PKT_TX_COUNT + 1];
#define MODULO_INC(val, max) {val = (++val < max) ? val : 0; } #define MODULO_INC(val, max) {val = (++val < max) ? val : 0; }
@ -219,7 +219,7 @@ static void tx_completed(Gmac *gmac, struct gmac_queue *queue)
{ {
struct gmac_desc_list *tx_desc_list = &queue->tx_desc_list; struct gmac_desc_list *tx_desc_list = &queue->tx_desc_list;
struct gmac_desc *tx_desc; struct gmac_desc *tx_desc;
struct net_buf *buf; struct net_pkt *pkt;
__ASSERT(tx_desc_list->buf[tx_desc_list->tail].w1 & GMAC_TXW1_USED, __ASSERT(tx_desc_list->buf[tx_desc_list->tail].w1 & GMAC_TXW1_USED,
"first buffer of a frame is not marked as own by GMAC"); "first buffer of a frame is not marked as own by GMAC");
@ -232,9 +232,9 @@ static void tx_completed(Gmac *gmac, struct gmac_queue *queue)
if (tx_desc->w1 & GMAC_TXW1_LASTBUFFER) { if (tx_desc->w1 & GMAC_TXW1_LASTBUFFER) {
/* Release net buffer to the buffer pool */ /* Release net buffer to the buffer pool */
buf = UINT_TO_POINTER(ring_buf_get(&queue->tx_frames)); pkt = UINT_TO_POINTER(ring_buf_get(&queue->tx_frames));
net_buf_unref(buf); net_pkt_unref(pkt);
SYS_LOG_DBG("Dropping buf %p", buf); SYS_LOG_DBG("Dropping pkt %p", pkt);
break; break;
} }
@ -246,7 +246,7 @@ static void tx_completed(Gmac *gmac, struct gmac_queue *queue)
*/ */
static void tx_error_handler(Gmac *gmac, struct gmac_queue *queue) static void tx_error_handler(Gmac *gmac, struct gmac_queue *queue)
{ {
struct net_buf *buf; struct net_pkt *pkt;
struct ring_buf *tx_frames = &queue->tx_frames; struct ring_buf *tx_frames = &queue->tx_frames;
queue->err_tx_flushed_count++; queue->err_tx_flushed_count++;
@ -257,9 +257,9 @@ static void tx_error_handler(Gmac *gmac, struct gmac_queue *queue)
/* Free all pkt resources in the TX path */ /* Free all pkt resources in the TX path */
while (tx_frames->tail != tx_frames->head) { while (tx_frames->tail != tx_frames->head) {
/* Release net buffer to the buffer pool */ /* Release net buffer to the buffer pool */
buf = UINT_TO_POINTER(tx_frames->buf[tx_frames->tail]); pkt = UINT_TO_POINTER(tx_frames->buf[tx_frames->tail]);
net_buf_unref(buf); net_pkt_unref(pkt);
SYS_LOG_DBG("Dropping buf %p", buf); SYS_LOG_DBG("Dropping pkt %p", pkt);
MODULO_INC(tx_frames->tail, tx_frames->len); MODULO_INC(tx_frames->tail, tx_frames->len);
} }
@ -472,9 +472,8 @@ static struct net_buf *frame_get(struct gmac_queue *queue)
struct gmac_desc_list *rx_desc_list = &queue->rx_desc_list; struct gmac_desc_list *rx_desc_list = &queue->rx_desc_list;
struct gmac_desc *rx_desc; struct gmac_desc *rx_desc;
struct ring_buf *rx_pkt_list = &queue->rx_pkt_list; struct ring_buf *rx_pkt_list = &queue->rx_pkt_list;
struct net_buf *rx_frame; struct net_pkt *rx_frame;
bool frame_is_complete; bool frame_is_complete;
struct net_buf *prev_frag;
struct net_buf *frag; struct net_buf *frag;
struct net_buf *new_frag; struct net_buf *new_frag;
uint8_t *frag_data; uint8_t *frag_data;
@ -501,7 +500,6 @@ static struct net_buf *frame_get(struct gmac_queue *queue)
rx_frame = net_pkt_get_reserve_rx(0, K_NO_WAIT); rx_frame = net_pkt_get_reserve_rx(0, K_NO_WAIT);
/* Process a frame */ /* Process a frame */
prev_frag = rx_frame;
tail = rx_desc_list->tail; tail = rx_desc_list->tail;
rx_desc = &rx_desc_list->buf[tail]; rx_desc = &rx_desc_list->buf[tail];
frame_is_complete = false; frame_is_complete = false;
@ -540,12 +538,11 @@ static struct net_buf *frame_get(struct gmac_queue *queue)
new_frag = net_pkt_get_frag(rx_frame, K_NO_WAIT); new_frag = net_pkt_get_frag(rx_frame, K_NO_WAIT);
if (new_frag == NULL) { if (new_frag == NULL) {
queue->err_rx_frames_dropped++; queue->err_rx_frames_dropped++;
net_buf_unref(rx_frame); net_pkt_unref(rx_frame);
rx_frame = NULL; rx_frame = NULL;
} else { } else {
net_buf_add(frag, frag_len); net_buf_add(frag, frag_len);
net_buf_frag_insert(prev_frag, frag); net_pkt_frag_insert(rx_frame, frag);
prev_frag = frag;
frag = new_frag; frag = new_frag;
rx_pkt_list->buf[tail] = (uint32_t)frag; rx_pkt_list->buf[tail] = (uint32_t)frag;
} }
@ -577,7 +574,7 @@ static void eth_rx(struct gmac_queue *queue)
{ {
struct eth_sam_dev_data *dev_data = struct eth_sam_dev_data *dev_data =
CONTAINER_OF(queue, struct eth_sam_dev_data, queue_list); CONTAINER_OF(queue, struct eth_sam_dev_data, queue_list);
struct net_buf *rx_frame; struct net_pkt *rx_frame;
/* More than one frame could have been received by GMAC, get all /* More than one frame could have been received by GMAC, get all
* complete frames stored in the GMAC RX descriptor list. * complete frames stored in the GMAC RX descriptor list.
@ -594,7 +591,7 @@ static void eth_rx(struct gmac_queue *queue)
} }
} }
static int eth_tx(struct net_if *iface, struct net_buf *buf) static int eth_tx(struct net_if *iface, struct net_pkt *pkt)
{ {
struct device *const dev = net_if_get_device(iface); struct device *const dev = net_if_get_device(iface);
const struct eth_sam_dev_cfg *const cfg = DEV_CFG(dev); const struct eth_sam_dev_cfg *const cfg = DEV_CFG(dev);
@ -609,8 +606,8 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf)
uint32_t err_tx_flushed_count_at_entry = queue->err_tx_flushed_count; uint32_t err_tx_flushed_count_at_entry = queue->err_tx_flushed_count;
unsigned int key; unsigned int key;
__ASSERT(buf, "buf pointer is NULL"); __ASSERT(pkt, "buf pointer is NULL");
__ASSERT(buf->frags, "Frame data missing"); __ASSERT(pkt->frags, "Frame data missing");
SYS_LOG_DBG("ETH tx"); SYS_LOG_DBG("ETH tx");
@ -618,10 +615,9 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf)
* in our case) header. Modify the data pointer to account for more data * in our case) header. Modify the data pointer to account for more data
* in the beginning of the buffer. * in the beginning of the buffer.
*/ */
net_buf_push(buf->frags, net_pkt_ll_reserve(buf)); net_buf_push(pkt->frags, net_pkt_ll_reserve(pkt));
frag = buf->frags;
frag = pkt->frags;
while (frag) { while (frag) {
frag_data = frag->data; frag_data = frag->data;
frag_len = frag->len; frag_len = frag->len;
@ -683,7 +679,7 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf)
tx_desc->w1 |= GMAC_TXW1_USED; tx_desc->w1 |= GMAC_TXW1_USED;
/* Account for a sent frame */ /* Account for a sent frame */
ring_buf_put(&queue->tx_frames, POINTER_TO_UINT(buf)); ring_buf_put(&queue->tx_frames, POINTER_TO_UINT(pkt));
irq_unlock(key); irq_unlock(key);

View file

@ -517,7 +517,7 @@ static inline uint8_t read_rxfifo_length(struct cc2520_spi *spi)
} }
static inline bool read_rxfifo_content(struct cc2520_spi *spi, static inline bool read_rxfifo_content(struct cc2520_spi *spi,
struct net_buf *buf, uint8_t len) struct net_buf *frag, uint8_t len)
{ {
uint8_t data[128+1]; uint8_t data[128+1];
@ -535,14 +535,14 @@ static inline bool read_rxfifo_content(struct cc2520_spi *spi,
return false; return false;
} }
memcpy(buf->data, &data[1], len); memcpy(frag->data, &data[1], len);
net_buf_add(buf, len); net_buf_add(frag, len);
return true; return true;
} }
static inline bool verify_crc(struct cc2520_context *cc2520, static inline bool verify_crc(struct cc2520_context *cc2520,
struct net_buf *buf) struct net_pkt *pkt)
{ {
cc2520->spi.cmd_buf[0] = CC2520_INS_RXBUF; cc2520->spi.cmd_buf[0] = CC2520_INS_RXBUF;
cc2520->spi.cmd_buf[1] = 0; cc2520->spi.cmd_buf[1] = 0;
@ -559,7 +559,7 @@ static inline bool verify_crc(struct cc2520_context *cc2520,
return false; return false;
} }
net_pkt_set_ieee802154_rssi(buf, cc2520->spi.cmd_buf[1]); net_pkt_set_ieee802154_rssi(pkt, cc2520->spi.cmd_buf[1]);
/** /**
* CC2520 does not provide an LQI but a correlation factor. * CC2520 does not provide an LQI but a correlation factor.
@ -597,12 +597,12 @@ static void cc2520_rx(int arg)
{ {
struct device *dev = INT_TO_POINTER(arg); struct device *dev = INT_TO_POINTER(arg);
struct cc2520_context *cc2520 = dev->driver_data; struct cc2520_context *cc2520 = dev->driver_data;
struct net_buf *pkt_buf = NULL; struct net_buf *pkt_frag = NULL;
struct net_buf *buf; struct net_pkt *pkt;
uint8_t pkt_len; uint8_t pkt_len;
while (1) { while (1) {
buf = NULL; pkt = NULL;
k_sem_take(&cc2520->rx_lock, K_FOREVER); k_sem_take(&cc2520->rx_lock, K_FOREVER);
@ -619,9 +619,9 @@ static void cc2520_rx(int arg)
goto flush; goto flush;
} }
buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT);
if (!buf) { if (!pkt) {
SYS_LOG_ERR("No buf available"); SYS_LOG_ERR("No pkt available");
goto flush; goto flush;
} }
@ -629,27 +629,27 @@ static void cc2520_rx(int arg)
/** /**
* Reserve 1 byte for length * Reserve 1 byte for length
*/ */
net_pkt_set_ll_reserve(buf, 1); net_pkt_set_ll_reserve(pkt, 1);
#endif #endif
pkt_buf = net_pkt_get_frag(buf, K_NO_WAIT); pkt_frag = net_pkt_get_frag(pkt, K_NO_WAIT);
if (!pkt_buf) { if (!pkt_frag) {
SYS_LOG_ERR("No pkt_buf available"); SYS_LOG_ERR("No pkt_frag available");
goto flush; goto flush;
} }
net_buf_frag_insert(buf, pkt_buf); net_pkt_frag_insert(pkt, pkt_frag);
#if defined(CONFIG_IEEE802154_CC2520_RAW) #if defined(CONFIG_IEEE802154_CC2520_RAW)
if (!read_rxfifo_content(&cc2520->spi, pkt_buf, pkt_len)) { if (!read_rxfifo_content(&cc2520->spi, pkt_frag, pkt_len)) {
SYS_LOG_ERR("No content read"); SYS_LOG_ERR("No content read");
goto flush; goto flush;
} }
if (!(pkt_buf->data[pkt_len - 1] & CC2520_FCS_CRC_OK)) { if (!(pkt_frag->data[pkt_len - 1] & CC2520_FCS_CRC_OK)) {
goto out; goto out;
} }
cc2520->lqi = pkt_buf->data[pkt_len - 1] & cc2520->lqi = pkt_frag->data[pkt_len - 1] &
CC2520_FCS_CORRELATION; CC2520_FCS_CORRELATION;
if (cc2520->lqi <= 50) { if (cc2520->lqi <= 50) {
cc2520->lqi = 0; cc2520->lqi = 0;
@ -659,30 +659,30 @@ static void cc2520_rx(int arg)
cc2520->lqi = (cc2520->lqi - 50) << 2; cc2520->lqi = (cc2520->lqi - 50) << 2;
} }
#else #else
if (!read_rxfifo_content(&cc2520->spi, pkt_buf, pkt_len - 2)) { if (!read_rxfifo_content(&cc2520->spi, pkt_frag, pkt_len - 2)) {
SYS_LOG_ERR("No content read"); SYS_LOG_ERR("No content read");
goto flush; goto flush;
} }
if (!verify_crc(cc2520, buf)) { if (!verify_crc(cc2520, pkt)) {
SYS_LOG_ERR("Bad packet CRC"); SYS_LOG_ERR("Bad packet CRC");
goto out; goto out;
} }
#endif #endif
if (ieee802154_radio_handle_ack(cc2520->iface, buf) == NET_OK) { if (ieee802154_radio_handle_ack(cc2520->iface, pkt) == NET_OK) {
SYS_LOG_DBG("ACK packet handled"); SYS_LOG_DBG("ACK packet handled");
goto out; goto out;
} }
#if defined(CONFIG_IEEE802154_CC2520_RAW) #if defined(CONFIG_IEEE802154_CC2520_RAW)
net_buf_add_u8(pkt_buf, cc2520->lqi); net_buf_add_u8(pkt_frag, cc2520->lqi);
#endif #endif
SYS_LOG_DBG("Caught a packet (%u) (LQI: %u)", SYS_LOG_DBG("Caught a packet (%u) (LQI: %u)",
pkt_len, cc2520->lqi); pkt_len, cc2520->lqi);
if (net_recv_data(cc2520->iface, buf) < 0) { if (net_recv_data(cc2520->iface, pkt) < 0) {
SYS_LOG_DBG("Packet dropped by NET stack"); SYS_LOG_DBG("Packet dropped by NET stack");
goto out; goto out;
} }
@ -696,8 +696,8 @@ flush:
_cc2520_print_errors(cc2520); _cc2520_print_errors(cc2520);
flush_rxfifo(cc2520); flush_rxfifo(cc2520);
out: out:
if (buf) { if (pkt) {
net_buf_unref(buf); net_pkt_unref(pkt);
} }
} }
} }
@ -837,11 +837,11 @@ error:
} }
static int cc2520_tx(struct device *dev, static int cc2520_tx(struct device *dev,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag) struct net_buf *frag)
{ {
uint8_t *frame = frag->data - net_pkt_ll_reserve(buf); uint8_t *frame = frag->data - net_pkt_ll_reserve(pkt);
uint8_t len = net_pkt_ll_reserve(buf) + frag->len; uint8_t len = net_pkt_ll_reserve(pkt) + frag->len;
struct cc2520_context *cc2520 = dev->driver_data; struct cc2520_context *cc2520 = dev->driver_data;
uint8_t retry = 2; uint8_t retry = 2;
bool status; bool status;

View file

@ -539,16 +539,14 @@ static inline bool read_rxfifo_content(struct mcr20a_spi *spi,
static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len) static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len)
{ {
struct net_buf *pkt_buf = NULL; struct net_pkt *pkt = NULL;
struct net_buf *buf; struct net_buf *frag;
uint8_t pkt_len; uint8_t pkt_len;
buf = NULL;
pkt_len = len - MCR20A_FCS_LENGTH; pkt_len = len - MCR20A_FCS_LENGTH;
buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT);
if (!buf) { if (!pkt) {
SYS_LOG_ERR("No buf available"); SYS_LOG_ERR("No buf available");
goto out; goto out;
} }
@ -558,22 +556,22 @@ static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len)
/** /**
* Reserve 1 byte for length * Reserve 1 byte for length
*/ */
net_pkt_set_ll_reserve(buf, 1); net_pkt_set_ll_reserve(pkt, 1);
#endif #endif
pkt_buf = net_pkt_get_frag(buf, K_NO_WAIT); frag = net_pkt_get_frag(pkt, K_NO_WAIT);
if (!pkt_buf) { if (!frag) {
SYS_LOG_ERR("No pkt_buf available"); SYS_LOG_ERR("No frag available");
goto out; goto out;
} }
net_buf_frag_insert(buf, pkt_buf); net_pkt_frag_insert(pkt, frag);
if (!read_rxfifo_content(&mcr20a->spi, pkt_buf, pkt_len)) { if (!read_rxfifo_content(&mcr20a->spi, frag, pkt_len)) {
SYS_LOG_ERR("No content read"); SYS_LOG_ERR("No content read");
goto out; goto out;
} }
if (ieee802154_radio_handle_ack(mcr20a->iface, buf) == NET_OK) { if (ieee802154_radio_handle_ack(mcr20a->iface, pkt) == NET_OK) {
SYS_LOG_DBG("ACK packet handled"); SYS_LOG_DBG("ACK packet handled");
goto out; goto out;
} }
@ -584,10 +582,10 @@ static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len)
mcr20a_get_rssi(mcr20a->lqi)); mcr20a_get_rssi(mcr20a->lqi));
#if defined(CONFIG_IEEE802154_MCR20A_RAW) #if defined(CONFIG_IEEE802154_MCR20A_RAW)
net_buf_add_u8(pkt_buf, mcr20a->lqi); net_buf_add_u8(frag, mcr20a->lqi);
#endif #endif
if (net_recv_data(mcr20a->iface, buf) < 0) { if (net_recv_data(mcr20a->iface, pkt) < 0) {
SYS_LOG_DBG("Packet dropped by NET stack"); SYS_LOG_DBG("Packet dropped by NET stack");
goto out; goto out;
} }
@ -597,8 +595,8 @@ static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len)
CONFIG_IEEE802154_MCR20A_RX_STACK_SIZE); CONFIG_IEEE802154_MCR20A_RX_STACK_SIZE);
return; return;
out: out:
if (buf) { if (pkt) {
net_buf_unref(buf); net_pkt_unref(pkt);
} }
} }
@ -1031,12 +1029,12 @@ error:
} }
static inline bool write_txfifo_content(struct mcr20a_spi *spi, static inline bool write_txfifo_content(struct mcr20a_spi *spi,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag) struct net_buf *frag)
{ {
uint8_t cmd[2 + MCR20A_PSDU_LENGTH]; uint8_t cmd[2 + MCR20A_PSDU_LENGTH];
uint8_t payload_len = net_pkt_ll_reserve(buf) + frag->len; uint8_t payload_len = net_pkt_ll_reserve(pkt) + frag->len;
uint8_t *payload = frag->data - net_pkt_ll_reserve(buf); uint8_t *payload = frag->data - net_pkt_ll_reserve(pkt);
bool retval; bool retval;
k_sem_take(&spi->spi_sem, K_FOREVER); k_sem_take(&spi->spi_sem, K_FOREVER);
@ -1067,7 +1065,7 @@ static inline bool write_txfifo_content(struct mcr20a_spi *spi,
} }
static int mcr20a_tx(struct device *dev, static int mcr20a_tx(struct device *dev,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag) struct net_buf *frag)
{ {
struct mcr20a_context *mcr20a = dev->driver_data; struct mcr20a_context *mcr20a = dev->driver_data;
@ -1077,7 +1075,7 @@ static int mcr20a_tx(struct device *dev,
k_mutex_lock(&mcr20a->phy_mutex, K_FOREVER); k_mutex_lock(&mcr20a->phy_mutex, K_FOREVER);
SYS_LOG_DBG("%p (%u)", SYS_LOG_DBG("%p (%u)",
frag, net_pkt_ll_reserve(buf) + frag->len); frag, net_pkt_ll_reserve(pkt) + frag->len);
if (!mcr20a_mask_irqb(mcr20a, true)) { if (!mcr20a_mask_irqb(mcr20a, true)) {
SYS_LOG_ERR("Failed to mask IRQ_B"); SYS_LOG_ERR("Failed to mask IRQ_B");
@ -1089,7 +1087,7 @@ static int mcr20a_tx(struct device *dev,
goto error; goto error;
} }
if (!write_txfifo_content(&mcr20a->spi, buf, frag)) { if (!write_txfifo_content(&mcr20a->spi, pkt, frag)) {
SYS_LOG_ERR("Did not write properly into TX FIFO"); SYS_LOG_ERR("Did not write properly into TX FIFO");
goto error; goto error;
} }

View file

@ -55,25 +55,25 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
{ {
struct device *dev = (struct device *)arg1; struct device *dev = (struct device *)arg1;
struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev); struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
struct net_buf *pkt_buf = NULL; struct net_buf *frag = NULL;
enum net_verdict ack_result; enum net_verdict ack_result;
struct net_buf *buf; struct net_pkt *pkt;
uint8_t pkt_len; uint8_t pkt_len;
ARG_UNUSED(arg2); ARG_UNUSED(arg2);
ARG_UNUSED(arg3); ARG_UNUSED(arg3);
while (1) { while (1) {
buf = NULL; pkt = NULL;
SYS_LOG_DBG("Waiting for frame"); SYS_LOG_DBG("Waiting for frame");
k_sem_take(&nrf5_radio->rx_wait, K_FOREVER); k_sem_take(&nrf5_radio->rx_wait, K_FOREVER);
SYS_LOG_DBG("Frame received"); SYS_LOG_DBG("Frame received");
buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT);
if (!buf) { if (!pkt) {
SYS_LOG_ERR("No buf available"); SYS_LOG_ERR("No pkt available");
goto out; goto out;
} }
@ -81,16 +81,16 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
/** /**
* Reserve 1 byte for length * Reserve 1 byte for length
*/ */
net_pkt_set_ll_reserve(buf, 1); net_pkt_set_ll_reserve(pkt, 1);
#endif #endif
pkt_buf = net_pkt_get_frag(buf, K_NO_WAIT); frag = net_pkt_get_frag(pkt, K_NO_WAIT);
if (!pkt_buf) { if (!frag) {
SYS_LOG_ERR("No pkt_buf available"); SYS_LOG_ERR("No frag available");
goto out; goto out;
} }
net_buf_frag_insert(buf, pkt_buf); net_pkt_frag_insert(pkt, frag);
/* rx_mpdu contains length, psdu, fcs|lqi /* rx_mpdu contains length, psdu, fcs|lqi
* The last 2 bytes contain LQI or FCS, depending if * The last 2 bytes contain LQI or FCS, depending if
@ -103,13 +103,13 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
#endif #endif
/* Skip length (first byte) and copy the payload */ /* Skip length (first byte) and copy the payload */
memcpy(pkt_buf->data, nrf5_radio->rx_psdu + 1, pkt_len); memcpy(frag->data, nrf5_radio->rx_psdu + 1, pkt_len);
net_buf_add(pkt_buf, pkt_len); net_buf_add(frag, pkt_len);
nrf_drv_radio802154_buffer_free(nrf5_radio->rx_psdu); nrf_drv_radio802154_buffer_free(nrf5_radio->rx_psdu);
ack_result = ieee802154_radio_handle_ack(nrf5_radio->iface, ack_result = ieee802154_radio_handle_ack(nrf5_radio->iface,
buf); pkt);
if (ack_result == NET_OK) { if (ack_result == NET_OK) {
SYS_LOG_DBG("ACK packet handled"); SYS_LOG_DBG("ACK packet handled");
goto out; goto out;
@ -118,7 +118,7 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
SYS_LOG_DBG("Caught a packet (%u) (LQI: %u)", SYS_LOG_DBG("Caught a packet (%u) (LQI: %u)",
pkt_len, nrf5_radio->lqi); pkt_len, nrf5_radio->lqi);
if (net_recv_data(nrf5_radio->iface, buf) < 0) { if (net_recv_data(nrf5_radio->iface, pkt) < 0) {
SYS_LOG_DBG("Packet dropped by NET stack"); SYS_LOG_DBG("Packet dropped by NET stack");
goto out; goto out;
} }
@ -129,8 +129,8 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3)
continue; continue;
out: out:
if (buf) { if (pkt) {
net_buf_unref(buf); net_pkt_unref(pkt);
} }
} }
} }
@ -230,12 +230,12 @@ static int nrf5_set_txpower(struct device *dev, int16_t dbm)
} }
static int nrf5_tx(struct device *dev, static int nrf5_tx(struct device *dev,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag) struct net_buf *frag)
{ {
struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev); struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev);
uint8_t payload_len = net_pkt_ll_reserve(buf) + frag->len; uint8_t payload_len = net_pkt_ll_reserve(pkt) + frag->len;
uint8_t *payload = frag->data - net_pkt_ll_reserve(buf); uint8_t *payload = frag->data - net_pkt_ll_reserve(pkt);
SYS_LOG_DBG("%p (%u)", payload, payload_len); SYS_LOG_DBG("%p (%u)", payload, payload_len);

View file

@ -30,8 +30,8 @@ static struct device *upipe_dev;
static uint8_t *upipe_rx(uint8_t *buf, size_t *off) static uint8_t *upipe_rx(uint8_t *buf, size_t *off)
{ {
struct upipe_context *upipe = upipe_dev->driver_data; struct upipe_context *upipe = upipe_dev->driver_data;
struct net_buf *pkt_buf = NULL; struct net_pkt *pkt = NULL;
struct net_buf *pkt = NULL; struct net_buf *frag = NULL;
if (!upipe_dev) { if (!upipe_dev) {
goto done; goto done;
@ -56,20 +56,20 @@ static uint8_t *upipe_rx(uint8_t *buf, size_t *off)
if (upipe->rx_len == upipe->rx_off) { if (upipe->rx_len == upipe->rx_off) {
pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT); pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT);
if (!pkt) { if (!pkt) {
SYS_LOG_DBG("No buf available"); SYS_LOG_DBG("No pkt available");
goto flush; goto flush;
} }
pkt_buf = net_pkt_get_frag(pkt, K_NO_WAIT); frag = net_pkt_get_frag(pkt, K_NO_WAIT);
if (!pkt_buf) { if (!frag) {
SYS_LOG_DBG("No fragment available"); SYS_LOG_DBG("No fragment available");
goto out; goto out;
} }
net_buf_frag_insert(pkt, pkt_buf); net_pkt_frag_insert(pkt, frag);
memcpy(pkt_buf->data, upipe->rx_buf, upipe->rx_len - 2); memcpy(frag->data, upipe->rx_buf, upipe->rx_len - 2);
net_buf_add(pkt_buf, upipe->rx_len - 2); net_buf_add(frag, upipe->rx_len - 2);
if (ieee802154_radio_handle_ack(upipe->iface, pkt) == NET_OK) { if (ieee802154_radio_handle_ack(upipe->iface, pkt) == NET_OK) {
SYS_LOG_DBG("ACK packet handled"); SYS_LOG_DBG("ACK packet handled");
@ -93,7 +93,7 @@ flush:
done: done:
*off = 0; *off = 0;
return buf; return pkt;
} }
static int upipe_cca(struct device *dev) static int upipe_cca(struct device *dev)
@ -163,11 +163,11 @@ static int upipe_set_txpower(struct device *dev, int16_t dbm)
} }
static int upipe_tx(struct device *dev, static int upipe_tx(struct device *dev,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag) struct net_buf *frag)
{ {
uint8_t *pkt_buf = frag->data - net_pkt_ll_reserve(buf); uint8_t *pkt_buf = frag->data - net_pkt_ll_reserve(pkt);
uint8_t len = net_pkt_ll_reserve(buf) + frag->len; uint8_t len = net_pkt_ll_reserve(pkt) + frag->len;
struct upipe_context *upipe = dev->driver_data; struct upipe_context *upipe = dev->driver_data;
uint8_t i, data; uint8_t i, data;

View file

@ -45,9 +45,9 @@ struct slip_context {
* driver initialization or SLIP_END byte. * driver initialization or SLIP_END byte.
*/ */
uint8_t buf[1]; /* SLIP data is read into this buf */ uint8_t buf[1]; /* SLIP data is read into this buf */
struct net_buf *rx; /* and then placed into this net_buf */ struct net_pkt *rx; /* and then placed into this net_pkt */
struct net_buf *last; /* Pointer to last fragment in the list */ struct net_buf *last; /* Pointer to last fragment in the list */
uint8_t *ptr; /* Where in net_buf to add data */ uint8_t *ptr; /* Where in net_pkt to add data */
struct net_if *iface; struct net_if *iface;
uint8_t state; uint8_t state;
@ -125,25 +125,25 @@ static inline void slip_writeb(unsigned char c)
uart_pipe_send(&buf[0], 1); uart_pipe_send(&buf[0], 1);
} }
static int slip_send(struct net_if *iface, struct net_buf *buf) static int slip_send(struct net_if *iface, struct net_pkt *pkt)
{ {
struct net_buf *frag; struct net_buf *frag;
#if defined(CONFIG_SLIP_TAP) #if defined(CONFIG_SLIP_TAP)
uint16_t ll_reserve = net_pkt_ll_reserve(buf); uint16_t ll_reserve = net_pkt_ll_reserve(pkt);
bool send_header_once = false; bool send_header_once = false;
#endif #endif
uint8_t *ptr; uint8_t *ptr;
uint16_t i; uint16_t i;
uint8_t c; uint8_t c;
if (!buf->frags) { if (!pkt->frags) {
/* No data? */ /* No data? */
return -ENODATA; return -ENODATA;
} }
slip_writeb(SLIP_END); slip_writeb(SLIP_END);
for (frag = buf->frags; frag; frag = frag->frags) { for (frag = pkt->frags; frag; frag = frag->frags) {
#if defined(CONFIG_SLIP_DEBUG) #if defined(CONFIG_SLIP_DEBUG)
int frag_count = 0; int frag_count = 0;
#endif #endif
@ -201,26 +201,26 @@ static int slip_send(struct net_if *iface, struct net_buf *buf)
#if defined(CONFIG_SLIP_DEBUG) #if defined(CONFIG_SLIP_DEBUG)
SYS_LOG_DBG("sent data %d bytes", SYS_LOG_DBG("sent data %d bytes",
frag->len + net_pkt_ll_reserve(buf)); frag->len + net_pkt_ll_reserve(pkt));
if (frag->len + ll_reserve) { if (frag->len + ll_reserve) {
char msg[8 + 1]; char msg[8 + 1];
snprintf(msg, sizeof(msg), "<slip %2d", frag_count++); snprintf(msg, sizeof(msg), "<slip %2d", frag_count++);
hexdump(msg, net_pkt_ll(buf), hexdump(msg, net_pkt_ll(pkt),
frag->len + net_pkt_ll_reserve(buf), frag->len + net_pkt_ll_reserve(pkt),
net_pkt_ll_reserve(buf)); net_pkt_ll_reserve(pkt));
} }
#endif #endif
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
slip_writeb(SLIP_END); slip_writeb(SLIP_END);
return 0; return 0;
} }
static struct net_buf *slip_poll_handler(struct slip_context *slip) static struct net_pkt *slip_poll_handler(struct slip_context *slip)
{ {
if (slip->last && slip->last->len) { if (slip->last && slip->last->len) {
return slip->rx; return slip->rx;
@ -231,18 +231,19 @@ static struct net_buf *slip_poll_handler(struct slip_context *slip)
static void process_msg(struct slip_context *slip) static void process_msg(struct slip_context *slip)
{ {
struct net_buf *buf; struct net_pkt *pkt;
buf = slip_poll_handler(slip); pkt = slip_poll_handler(slip);
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
return; return;
} }
if (net_recv_data(slip->iface, buf) < 0) { if (net_recv_data(slip->iface, pkt) < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
slip->rx = slip->last = NULL; slip->rx = NULL;
slip->last = NULL;
} }
static inline int slip_input_byte(struct slip_context *slip, static inline int slip_input_byte(struct slip_context *slip,
@ -306,7 +307,7 @@ static inline int slip_input_byte(struct slip_context *slip,
return 0; return 0;
} }
net_buf_frag_add(slip->rx, slip->last); net_pkt_frag_add(slip->rx, slip->last);
slip->ptr = net_pkt_ip_data(slip->rx); slip->ptr = net_pkt_ip_data(slip->rx);
} }

View file

@ -17,7 +17,7 @@
#include <net/ethernet.h> #include <net/ethernet.h>
#define NET_ARP_BUF(buf) ((struct net_arp_hdr *)net_pkt_ip_data(buf)) #define NET_ARP_BUF(pkt) ((struct net_arp_hdr *)net_pkt_ip_data(pkt))
struct net_arp_hdr { struct net_arp_hdr {
uint16_t hwtype; /* HTYPE */ uint16_t hwtype; /* HTYPE */
@ -36,8 +36,8 @@ struct net_arp_hdr {
#define NET_ARP_REQUEST 1 #define NET_ARP_REQUEST 1
#define NET_ARP_REPLY 2 #define NET_ARP_REPLY 2
struct net_buf *net_arp_prepare(struct net_buf *buf); struct net_pkt *net_arp_prepare(struct net_pkt *pkt);
enum net_verdict net_arp_input(struct net_buf *buf); enum net_verdict net_arp_input(struct net_pkt *pkt);
void net_arp_init(void); void net_arp_init(void);

View file

@ -401,11 +401,6 @@ struct net_buf {
struct net_buf *frags; struct net_buf *frags;
}; };
#if defined(CONFIG_NET_TCP)
/** List pointer used for TCP retransmit buffering */
sys_snode_t sent_list;
#endif /* CONFIG_NET_TCP */
/** Reference count. */ /** Reference count. */
uint8_t ref; uint8_t ref;

View file

@ -20,7 +20,7 @@
#include <net/net_pkt.h> #include <net/net_pkt.h>
#include <misc/util.h> #include <misc/util.h>
#define NET_ETH_BUF(buf) ((struct net_eth_hdr *)net_pkt_ll(buf)) #define NET_ETH_BUF(pkt) ((struct net_eth_hdr *)net_pkt_ll(pkt))
#define NET_ETH_PTYPE_ARP 0x0806 #define NET_ETH_PTYPE_ARP 0x0806
#define NET_ETH_PTYPE_IP 0x0800 #define NET_ETH_PTYPE_IP 0x0800

View file

@ -42,9 +42,9 @@ struct ieee802154_radio_api {
/** Set TX power level in dbm */ /** Set TX power level in dbm */
int (*set_txpower)(struct device *dev, int16_t dbm); int (*set_txpower)(struct device *dev, int16_t dbm);
/** Transmit a buffer fragment */ /** Transmit a packet fragment */
int (*tx)(struct device *dev, int (*tx)(struct device *dev,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag); struct net_buf *frag);
/** Start the device */ /** Start the device */
@ -63,12 +63,12 @@ struct ieee802154_radio_api {
* @details This function should be used to fill in struct net_if's send pointer. * @details This function should be used to fill in struct net_if's send pointer.
* *
* @param iface A valid pointer on a network interface to send from * @param iface A valid pointer on a network interface to send from
* @param buf A valid pointer on a buffer to send * @param pkt A valid pointer on a packet to send
* *
* @return 0 on success, negative value otherwise * @return 0 on success, negative value otherwise
*/ */
extern int ieee802154_radio_send(struct net_if *iface, extern int ieee802154_radio_send(struct net_if *iface,
struct net_buf *buf); struct net_pkt *pkt);
/** /**
* @brief Radio driver ACK handling function that hw drivers should use * @brief Radio driver ACK handling function that hw drivers should use
@ -77,12 +77,12 @@ extern int ieee802154_radio_send(struct net_if *iface,
* helps to hook direcly the hw drivers to the radio driver. * helps to hook direcly the hw drivers to the radio driver.
* *
* @param iface A valid pointer on a network interface that received the packet * @param iface A valid pointer on a network interface that received the packet
* @param buf A valid pointer on a buffer to check * @param pkt A valid pointer on a packet to check
* *
* @return NET_OK if it was handled, NET_CONTINUE otherwise * @return NET_OK if it was handled, NET_CONTINUE otherwise
*/ */
extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface, extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface,
struct net_buf *buf); struct net_pkt *pkt);
/** /**
* @brief Initialize L2 stack for a given interface * @brief Initialize L2 stack for a given interface

View file

@ -148,7 +148,7 @@ struct mqtt_ctx {
void (*malformed)(struct mqtt_ctx *ctx, uint16_t pkt_type); void (*malformed)(struct mqtt_ctx *ctx, uint16_t pkt_type);
/* Internal use only */ /* Internal use only */
int (*rcv)(struct mqtt_ctx *ctx, struct net_buf *); int (*rcv)(struct mqtt_ctx *ctx, struct net_pkt *);
/** Application type, see: enum mqtt_app */ /** Application type, see: enum mqtt_app */
uint8_t app_type; uint8_t app_type;

View file

@ -70,16 +70,16 @@ struct net_context;
* received. * received.
* *
* @param context The context to use. * @param context The context to use.
* @param buf Network buffer that is received. If the buf is not NULL, * @param pkt Network buffer that is received. If the pkt is not NULL,
* then the callback will own the buffer and it needs to to unref the buf * then the callback will own the buffer and it needs to to unref the pkt
* as soon as it has finished working with it. On EOF, buf will be NULL. * as soon as it has finished working with it. On EOF, pkt will be NULL.
* @param status Value is set to 0 if some data or the connection is * @param status Value is set to 0 if some data or the connection is
* at EOF, <0 if there was an error receiving data, in this case the * at EOF, <0 if there was an error receiving data, in this case the
* buf parameter is set to NULL. * pkt parameter is set to NULL.
* @param user_data The user data given in net_recv() call. * @param user_data The user data given in net_recv() call.
*/ */
typedef void (*net_context_recv_cb_t)(struct net_context *context, typedef void (*net_context_recv_cb_t)(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data); void *user_data);
@ -139,6 +139,19 @@ typedef void (*net_context_connect_cb_t)(struct net_context *context,
int status, int status,
void *user_data); void *user_data);
/* The net_pkt_get_slab_func_t is here in order to avoid circular
* dependency between net_pkt.h and net_context.h
*/
/**
* @typedef net_pkt_get_slab_func_t
*
* @brief Function that is called to get the slab that is used
* for net_pkt allocations.
*
* @return Pointer to valid struct k_mem_slab instance.
*/
typedef struct k_mem_slab *(*net_pkt_get_slab_func_t)(void);
/* The net_pkt_get_pool_func_t is here in order to avoid circular /* The net_pkt_get_pool_func_t is here in order to avoid circular
* dependency between net_pkt.h and net_context.h * dependency between net_pkt.h and net_context.h
*/ */
@ -200,7 +213,7 @@ struct net_context {
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
/** Get TX net_buf pool for this context. /** Get TX net_buf pool for this context.
*/ */
net_pkt_get_pool_func_t tx_pool; net_pkt_get_slab_func_t tx_slab;
/** Get DATA net_buf pool for this context. /** Get DATA net_buf pool for this context.
*/ */
@ -611,7 +624,7 @@ int net_context_accept(struct net_context *context,
* net_context_connect(). * net_context_connect().
* This is similar as BSD send() function. * This is similar as BSD send() function.
* *
* @param buf The network buffer to send. * @param pkt The network buffer to send.
* @param cb Caller supplied callback function. * @param cb Caller supplied callback function.
* @param timeout Timeout for the connection. Possible values * @param timeout Timeout for the connection. Possible values
* are K_FOREVER, K_NO_WAIT, >0. * are K_FOREVER, K_NO_WAIT, >0.
@ -620,7 +633,7 @@ int net_context_accept(struct net_context *context,
* *
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
int net_context_send(struct net_buf *buf, int net_context_send(struct net_pkt *pkt,
net_context_send_cb_t cb, net_context_send_cb_t cb,
int32_t timeout, int32_t timeout,
void *token, void *token,
@ -641,7 +654,7 @@ int net_context_send(struct net_buf *buf,
* timeout expires. * timeout expires.
* This is similar as BSD sendto() function. * This is similar as BSD sendto() function.
* *
* @param buf The network buffer to send. * @param pkt The network buffer to send.
* @param dst_addr Destination address. This will override the address * @param dst_addr Destination address. This will override the address
* already set in network buffer. * already set in network buffer.
* @param addrlen Length of the address. * @param addrlen Length of the address.
@ -653,7 +666,7 @@ int net_context_send(struct net_buf *buf,
* *
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
int net_context_sendto(struct net_buf *buf, int net_context_sendto(struct net_pkt *pkt,
const struct sockaddr *dst_addr, const struct sockaddr *dst_addr,
socklen_t addrlen, socklen_t addrlen,
net_context_send_cb_t cb, net_context_send_cb_t cb,
@ -728,21 +741,21 @@ void net_context_foreach(net_context_cb_t cb, void *user_data);
* @param context Context that will use the given net_buf pools. * @param context Context that will use the given net_buf pools.
* @param tx_pool Pointer to the function that will return TX pool * @param tx_pool Pointer to the function that will return TX pool
* to the caller. The TX pool is used when sending data to network. * to the caller. The TX pool is used when sending data to network.
* There is one TX net_buf for each network packet that is sent. * There is one TX net_pkt for each network packet that is sent.
* @param data_pool Pointer to the function that will return DATA pool * @param data_pool Pointer to the function that will return DATA pool
* to the caller. The DATA pool is used to store data that is sent to * to the caller. The DATA pool is used to store data that is sent to
* the network. * the network.
*/ */
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
static inline void net_context_setup_pools(struct net_context *context, static inline void net_context_setup_pools(struct net_context *context,
net_pkt_get_pool_func_t tx_pool, net_pkt_get_slab_func_t tx_slab,
net_pkt_get_pool_func_t data_pool) net_pkt_get_pool_func_t data_pool)
{ {
NET_ASSERT(context); NET_ASSERT(context);
NET_ASSERT(tx_pool); NET_ASSERT(tx_slab);
NET_ASSERT(data_pool); NET_ASSERT(data_pool);
context->tx_pool = tx_pool; context->tx_slab = tx_slab;
context->data_pool = data_pool; context->data_pool = data_pool;
} }
#else #else

View file

@ -57,6 +57,7 @@ extern "C" {
#include <kernel.h> #include <kernel.h>
struct net_buf; struct net_buf;
struct net_pkt;
struct net_context; struct net_context;
struct net_if; struct net_if;
@ -71,21 +72,21 @@ enum net_verdict {
}; };
/* Called by lower network stack when a network packet has been received */ /* Called by lower network stack when a network packet has been received */
int net_recv_data(struct net_if *iface, struct net_buf *buf); int net_recv_data(struct net_if *iface, struct net_pkt *pkt);
/** /**
* @brief Send data to network. * @brief Send data to network.
* *
* @details Send data to network. This should not be used normally by * @details Send data to network. This should not be used normally by
* applications as it requires that the buffer and fragments are properly * applications as it requires that the pktfer and fragments are properly
* constructed. * constructed.
* *
* @param buf Network buffer. * @param pkt Network packet.
* *
* @return 0 if ok, <0 if error. If <0 is returned, then the caller needs * @return 0 if ok, <0 if error. If <0 is returned, then the caller needs
* to unref the buf in order to avoid buffer leak. * to unref the pkt in order to avoid memory leak.
*/ */
int net_send_data(struct net_buf *buf); int net_send_data(struct net_pkt *pkt);
struct net_stack_info { struct net_stack_info {
char *stack; char *stack;

View file

@ -22,7 +22,6 @@
#include <misc/slist.h> #include <misc/slist.h>
#include <net/net_core.h> #include <net/net_core.h>
#include <net/buf.h>
#include <net/net_linkaddr.h> #include <net/net_linkaddr.h>
#include <net/net_ip.h> #include <net/net_ip.h>
#include <net/net_l2.h> #include <net/net_l2.h>
@ -315,27 +314,27 @@ struct net_if {
} __net_if_align; } __net_if_align;
/** /**
* @brief Send a buffer through a net iface * @brief Send a packet through a net iface
* *
* @param iface Pointer to a network interface structure * @param iface Pointer to a network interface structure
* @param buf Pointer on a net buffer to send * @param pkt Pointer on a net packet to send
* *
* return verdict about the packet * return verdict about the packet
*/ */
enum net_verdict net_if_send_data(struct net_if *iface, struct net_buf *buf); enum net_verdict net_if_send_data(struct net_if *iface, struct net_pkt *pkt);
/** /**
* @brief Input a buffer through a net iface * @brief Input a packet through a net iface
* *
* @param iface Pointer to a network interface structure * @param iface Pointer to a network interface structure
* @param buf Pointer on a net buffer to input * @param pkt Pointer on a net packet to input
* *
* @return verdict about the packet * @return verdict about the packet
*/ */
static inline enum net_verdict net_if_recv_data(struct net_if *iface, static inline enum net_verdict net_if_recv_data(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
return iface->l2->recv(iface, buf); return iface->l2->recv(iface, pkt);
} }
/** /**
@ -380,11 +379,11 @@ static inline struct device *net_if_get_device(struct net_if *iface)
* @brief Queue a packet into net if's TX queue * @brief Queue a packet into net if's TX queue
* *
* @param iface Pointer to a network interface structure * @param iface Pointer to a network interface structure
* @param buf Pointer on a net buffer to queue * @param pkt Pointer on a net pktfer to queue
*/ */
static inline void net_if_queue_tx(struct net_if *iface, struct net_buf *buf) static inline void net_if_queue_tx(struct net_if *iface, struct net_pkt *pkt)
{ {
net_buf_put(&iface->tx_queue, buf); k_fifo_put(&iface->tx_queue, pkt);
} }
#if defined(CONFIG_NET_OFFLOAD) #if defined(CONFIG_NET_OFFLOAD)
@ -1087,7 +1086,7 @@ static inline void net_if_ipv4_set_gw(struct net_if *iface,
* @brief Define callback that is called after a network packet * @brief Define callback that is called after a network packet
* has been sent. * has been sent.
* @param "struct net_if *iface" A pointer on a struct net_if to which the * @param "struct net_if *iface" A pointer on a struct net_if to which the
* the net_buf was sent to. * the net_pkt was sent to.
* @param "struct net_linkaddr *dst" Link layer address of the destination * @param "struct net_linkaddr *dst" Link layer address of the destination
* where the network packet was sent. * where the network packet was sent.
* @param "int status" Send status, 0 is ok, < 0 error. * @param "int status" Send status, 0 is ok, < 0 error.
@ -1194,7 +1193,7 @@ int net_if_down(struct net_if *iface);
struct net_if_api { struct net_if_api {
void (*init)(struct net_if *iface); void (*init)(struct net_if *iface);
int (*send)(struct net_if *iface, struct net_buf *buf); int (*send)(struct net_if *iface, struct net_pkt *pkt);
}; };
#if defined(CONFIG_NET_DHCPV4) #if defined(CONFIG_NET_DHCPV4)

View file

@ -25,18 +25,18 @@ struct net_l2 {
* This function is used by net core to get iface's L2 layer parsing * This function is used by net core to get iface's L2 layer parsing
* what's relevant to itself. * what's relevant to itself.
*/ */
enum net_verdict (*recv)(struct net_if *iface, struct net_buf *buf); enum net_verdict (*recv)(struct net_if *iface, struct net_pkt *pkt);
/** /**
* This function is used by net core to push a buffer to lower layer * This function is used by net core to push a packet to lower layer
* (interface's L2), which in turn might work on the buffer relevantly. * (interface's L2), which in turn might work on the packet relevantly.
* (adding proper header etc...) * (adding proper header etc...)
*/ */
enum net_verdict (*send)(struct net_if *iface, struct net_buf *buf); enum net_verdict (*send)(struct net_if *iface, struct net_pkt *pkt);
/** /**
* This function is used to get the amount of bytes the net core should * This function is used to get the amount of bytes the net core should
* reserve as headroom in a net buffer. Such space is relevant to L2 * reserve as headroom in a net packet. Such space is relevant to L2
* layer only. * layer only.
*/ */
uint16_t (*reserve)(struct net_if *iface, void *data); uint16_t (*reserve)(struct net_if *iface, void *data);

View file

@ -70,7 +70,7 @@ struct net_offload {
/** /**
* This function is called when user wants to send data to peer host. * This function is called when user wants to send data to peer host.
*/ */
int (*send)(struct net_buf *buf, int (*send)(struct net_pkt *pkt,
net_context_send_cb_t cb, net_context_send_cb_t cb,
int32_t timeout, int32_t timeout,
void *token, void *token,
@ -79,7 +79,7 @@ struct net_offload {
/** /**
* This function is called when user wants to send data to peer host. * This function is called when user wants to send data to peer host.
*/ */
int (*sendto)(struct net_buf *buf, int (*sendto)(struct net_pkt *pkt,
const struct sockaddr *dst_addr, const struct sockaddr *dst_addr,
socklen_t addrlen, socklen_t addrlen,
net_context_send_cb_t cb, net_context_send_cb_t cb,
@ -266,13 +266,13 @@ static inline int net_offload_accept(struct net_if *iface,
} }
/** /**
* @brief Send a network buffer to a peer. * @brief Send a network packet to a peer.
* *
* @details This function can be used to send network data to a peer * @details This function can be used to send network data to a peer
* connection. This function will return immediately if the timeout * connection. This function will return immediately if the timeout
* is set to K_NO_WAIT. If the timeout is set to K_FOREVER, the function * is set to K_NO_WAIT. If the timeout is set to K_FOREVER, the function
* will wait until the network buffer is sent. Timeout value > 0 will * will wait until the network packet is sent. Timeout value > 0 will
* wait as many ms. After the network buffer is sent, * wait as many ms. After the network packet is sent,
* a caller supplied callback is called. The callback is called even * a caller supplied callback is called. The callback is called even
* if timeout was set to K_FOREVER, the callback is called * if timeout was set to K_FOREVER, the callback is called
* before this function will return in this case. The callback is not * before this function will return in this case. The callback is not
@ -283,7 +283,7 @@ static inline int net_offload_accept(struct net_if *iface,
* *
* @param iface Network interface where the offloaded IP stack can be * @param iface Network interface where the offloaded IP stack can be
* reached. * reached.
* @param buf The network buffer to send. * @param pkt The network packet to send.
* @param cb Caller supplied callback function. * @param cb Caller supplied callback function.
* @param timeout Timeout for the connection. Possible values * @param timeout Timeout for the connection. Possible values
* are K_FOREVER, K_NO_WAIT, >0. * are K_FOREVER, K_NO_WAIT, >0.
@ -293,7 +293,7 @@ static inline int net_offload_accept(struct net_if *iface,
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
static inline int net_offload_send(struct net_if *iface, static inline int net_offload_send(struct net_if *iface,
struct net_buf *buf, struct net_pkt *pkt,
net_context_send_cb_t cb, net_context_send_cb_t cb,
int32_t timeout, int32_t timeout,
void *token, void *token,
@ -303,18 +303,18 @@ static inline int net_offload_send(struct net_if *iface,
NET_ASSERT(iface->offload); NET_ASSERT(iface->offload);
NET_ASSERT(iface->offload->send); NET_ASSERT(iface->offload->send);
return iface->offload->send(buf, cb, timeout, token, user_data); return iface->offload->send(pkt, cb, timeout, token, user_data);
} }
/** /**
* @brief Send a network buffer to a peer specified by address. * @brief Send a network packet to a peer specified by address.
* *
* @details This function can be used to send network data to a peer * @details This function can be used to send network data to a peer
* specified by address. This variant can only be used for datagram * specified by address. This variant can only be used for datagram
* connections of type SOCK_DGRAM. This function will return immediately * connections of type SOCK_DGRAM. This function will return immediately
* if the timeout is set to K_NO_WAIT. If the timeout is set to K_FOREVER, * if the timeout is set to K_NO_WAIT. If the timeout is set to K_FOREVER,
* the function will wait until the network buffer is sent. Timeout * the function will wait until the network packet is sent. Timeout
* value > 0 will wait as many ms. After the network buffer * value > 0 will wait as many ms. After the network packet
* is sent, a caller supplied callback is called. The callback is called * is sent, a caller supplied callback is called. The callback is called
* even if timeout was set to K_FOREVER, the callback is called * even if timeout was set to K_FOREVER, the callback is called
* before this function will return. The callback is not called if the * before this function will return. The callback is not called if the
@ -323,9 +323,9 @@ static inline int net_offload_send(struct net_if *iface,
* *
* @param iface Network interface where the offloaded IP stack can be * @param iface Network interface where the offloaded IP stack can be
* reached. * reached.
* @param buf The network buffer to send. * @param pkt The network packet to send.
* @param dst_addr Destination address. This will override the address * @param dst_addr Destination address. This will override the address
* already set in network buffer. * already set in network packet.
* @param addrlen Length of the address. * @param addrlen Length of the address.
* @param cb Caller supplied callback function. * @param cb Caller supplied callback function.
* @param timeout Timeout for the connection. Possible values * @param timeout Timeout for the connection. Possible values
@ -336,7 +336,7 @@ static inline int net_offload_send(struct net_if *iface,
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
static inline int net_offload_sendto(struct net_if *iface, static inline int net_offload_sendto(struct net_if *iface,
struct net_buf *buf, struct net_pkt *pkt,
const struct sockaddr *dst_addr, const struct sockaddr *dst_addr,
socklen_t addrlen, socklen_t addrlen,
net_context_send_cb_t cb, net_context_send_cb_t cb,
@ -348,7 +348,7 @@ static inline int net_offload_sendto(struct net_if *iface,
NET_ASSERT(iface->offload); NET_ASSERT(iface->offload);
NET_ASSERT(iface->offload->sendto); NET_ASSERT(iface->offload->sendto);
return iface->offload->sendto(buf, dst_addr, addrlen, cb, return iface->offload->sendto(pkt, dst_addr, addrlen, cb,
timeout, token, user_data); timeout, token, user_data);
} }
@ -364,8 +364,8 @@ static inline int net_offload_sendto(struct net_if *iface,
* multiple times to register new values. * multiple times to register new values.
* This function will return immediately if the timeout is set to K_NO_WAIT. * This function will return immediately if the timeout is set to K_NO_WAIT.
* If the timeout is set to K_FOREVER, the function will wait until the * If the timeout is set to K_FOREVER, the function will wait until the
* network buffer is received. Timeout value > 0 will wait as many ms. * network packet is received. Timeout value > 0 will wait as many ms.
* After the network buffer is received, a caller supplied callback is * After the network packet is received, a caller supplied callback is
* called. The callback is called even if timeout was set to K_FOREVER, * called. The callback is called even if timeout was set to K_FOREVER,
* the callback is called before this function will return in this case. * the callback is called before this function will return in this case.
* The callback is not called if the timeout expires. The timeout functionality * The callback is not called if the timeout expires. The timeout functionality

File diff suppressed because it is too large Load diff

View file

@ -193,7 +193,7 @@ struct zoap_observer {
* @brief Representation of a CoAP packet. * @brief Representation of a CoAP packet.
*/ */
struct zoap_packet { struct zoap_packet {
struct net_buf *buf; struct net_pkt *pkt;
uint8_t *start; /* Start of the payload */ uint8_t *start; /* Start of the payload */
uint16_t total_size; uint16_t total_size;
}; };
@ -211,7 +211,7 @@ typedef int (*zoap_reply_t)(const struct zoap_packet *response,
* @brief Represents a request awaiting for an acknowledgment (ACK). * @brief Represents a request awaiting for an acknowledgment (ACK).
*/ */
struct zoap_pending { struct zoap_pending {
struct net_buf *buf; struct net_pkt *pkt;
struct sockaddr addr; struct sockaddr addr;
int32_t timeout; int32_t timeout;
uint16_t id; uint16_t id;
@ -309,35 +309,35 @@ struct zoap_option {
}; };
/** /**
* @brief Parses the CoAP packet in @a buf, validating it and * @brief Parses the CoAP packet in @a pkt, validating it and
* initializing @a pkt. @a buf must remain valid while @a pkt is used. * initializing @a zpkt. @a pkt must remain valid while @a zpkt is used.
* *
* @param pkt Packet to be initialized from received @a buf. * @param zpkt Packet to be initialized from received @a pkt.
* @param buf Buffer containing a CoAP packet, its @a data pointer is * @param pkt Network Packet containing a CoAP packet, its @a data pointer is
* positioned on the start of the CoAP packet. * positioned on the start of the CoAP packet.
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_packet_parse(struct zoap_packet *pkt, struct net_buf *buf); int zoap_packet_parse(struct zoap_packet *zpkt, struct net_pkt *pkt);
/** /**
* @brief Creates a new CoAP packet from a net_buf. @a buf must remain * @brief Creates a new CoAP packet from a net_pkt. @a pkt must remain
* valid while @a pkt is used. * valid while @a zpkt is used.
* *
* @param pkt New packet to be initialized using the storage from @a * @param zpkt New packet to be initialized using the storage from @a
* buf. * pkt.
* @param buf Buffer that will contain a CoAP packet * @param pkt Network Packet that will contain a CoAP packet
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_packet_init(struct zoap_packet *pkt, struct net_buf *buf); int zoap_packet_init(struct zoap_packet *zpkt, struct net_pkt *pkt);
/** /**
* @brief Initialize a pending request with a request. * @brief Initialize a pending request with a request.
* *
* The request's fields are copied into the pending struct, so @a * The request's fields are copied into the pending struct, so @a
* request doesn't have to live for as long as the pending struct * request doesn't have to live for as long as the pending struct
* lives, but net_buf needs to live for at least that long. * lives, but net_pkt needs to live for at least that long.
* *
* @param pending Structure representing the waiting for a * @param pending Structure representing the waiting for a
* confirmation message, initialized with data from @a request * confirmation message, initialized with data from @a request
@ -451,13 +451,13 @@ void zoap_reply_clear(struct zoap_reply *reply);
* @brief When a request is received, call the appropriate methods of * @brief When a request is received, call the appropriate methods of
* the matching resources. * the matching resources.
* *
* @param pkt Packet received * @param zpkt Packet received
* @param resources Array of known resources * @param resources Array of known resources
* @param from Address from which the packet was received * @param from Address from which the packet was received
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_handle_request(struct zoap_packet *pkt, int zoap_handle_request(struct zoap_packet *zpkt,
struct zoap_resource *resources, struct zoap_resource *resources,
const struct sockaddr *from); const struct sockaddr *from);
@ -487,12 +487,12 @@ bool zoap_request_is_observe(const struct zoap_packet *request);
* It will insert the COAP_MARKER (0xFF), if its not set, and return the * It will insert the COAP_MARKER (0xFF), if its not set, and return the
* available size for the payload. * available size for the payload.
* *
* @param pkt Packet to get (or insert) the payload * @param zpkt Packet to get (or insert) the payload
* @param len Amount of space for the payload * @param len Amount of space for the payload
* *
* @return pointer to the start of the payload, NULL in case of error. * @return pointer to the start of the payload, NULL in case of error.
*/ */
uint8_t *zoap_packet_get_payload(struct zoap_packet *pkt, uint16_t *len); uint8_t *zoap_packet_get_payload(struct zoap_packet *zpkt, uint16_t *len);
/** /**
* @brief Sets how much space was used by the payload. * @brief Sets how much space was used by the payload.
@ -501,26 +501,26 @@ uint8_t *zoap_packet_get_payload(struct zoap_packet *pkt, uint16_t *len);
* update the internal representation with the amount of data that was * update the internal representation with the amount of data that was
* added to the packet. * added to the packet.
* *
* @param pkt Packet to be updated * @param zpkt Packet to be updated
* @param len Amount of data that was added to the payload * @param len Amount of data that was added to the payload
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_packet_set_used(struct zoap_packet *pkt, uint16_t len); int zoap_packet_set_used(struct zoap_packet *zpkt, uint16_t len);
/** /**
* @brief Adds an option to the packet. * @brief Adds an option to the packet.
* *
* Note: ptions must be added in numeric order of their codes. * Note: ptions must be added in numeric order of their codes.
* *
* @param pkt Packet to be updated * @param zpkt Packet to be updated
* @param code Option code to add to the packet, see #zoap_option_num * @param code Option code to add to the packet, see #zoap_option_num
* @param value Pointer to the value of the option, will be copied to the packet * @param value Pointer to the value of the option, will be copied to the packet
* @param len Size of the data to be added * @param len Size of the data to be added
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_add_option(struct zoap_packet *pkt, uint16_t code, int zoap_add_option(struct zoap_packet *zpkt, uint16_t code,
const void *value, uint16_t len); const void *value, uint16_t len);
/** /**
@ -542,20 +542,20 @@ unsigned int zoap_option_value_to_int(const struct zoap_option *option);
* The option must be added in numeric order of their codes, and the * The option must be added in numeric order of their codes, and the
* least amount of bytes will be used to encode the value. * least amount of bytes will be used to encode the value.
* *
* @param pkt Packet to be updated * @param zpkt Packet to be updated
* @param code Option code to add to the packet, see #zoap_option_num * @param code Option code to add to the packet, see #zoap_option_num
* @param val Integer value to be added * @param val Integer value to be added
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_add_option_int(struct zoap_packet *pkt, uint16_t code, int zoap_add_option_int(struct zoap_packet *zpkt, uint16_t code,
unsigned int val); unsigned int val);
/** /**
* @brief Return the values associated with the option of value @a * @brief Return the values associated with the option of value @a
* code. * code.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* @param code Option number to look for * @param code Option number to look for
* @param options Array of #zoap_option where to store the value * @param options Array of #zoap_option where to store the value
* of the options found * of the options found
@ -564,7 +564,7 @@ int zoap_add_option_int(struct zoap_packet *pkt, uint16_t code,
* @return The number of options found in packet matching code, * @return The number of options found in packet matching code,
* negative on error. * negative on error.
*/ */
int zoap_find_options(const struct zoap_packet *pkt, uint16_t code, int zoap_find_options(const struct zoap_packet *zpkt, uint16_t code,
struct zoap_option *options, uint16_t veclen); struct zoap_option *options, uint16_t veclen);
/** /**
@ -624,61 +624,61 @@ int zoap_block_transfer_init(struct zoap_block_context *ctx,
/** /**
* @brief Add BLOCK1 option to the packet. * @brief Add BLOCK1 option to the packet.
* *
* @param pkt Packet to be updated * @param zpkt Packet to be updated
* @param ctx Block context from which to retrieve the * @param ctx Block context from which to retrieve the
* information for the Block1 option * information for the Block1 option
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_add_block1_option(struct zoap_packet *pkt, int zoap_add_block1_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx); struct zoap_block_context *ctx);
/** /**
* @brief Add BLOCK2 option to the packet. * @brief Add BLOCK2 option to the packet.
* *
* @param pkt Packet to be updated * @param zpkt Packet to be updated
* @param ctx Block context from which to retrieve the * @param ctx Block context from which to retrieve the
* information for the Block2 option * information for the Block2 option
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_add_block2_option(struct zoap_packet *pkt, int zoap_add_block2_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx); struct zoap_block_context *ctx);
/** /**
* @brief Add SIZE1 option to the packet. * @brief Add SIZE1 option to the packet.
* *
* @param pkt Packet to be updated * @param zpkt Packet to be updated
* @param ctx Block context from which to retrieve the * @param ctx Block context from which to retrieve the
* information for the Size1 option * information for the Size1 option
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_add_size1_option(struct zoap_packet *pkt, int zoap_add_size1_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx); struct zoap_block_context *ctx);
/** /**
* @brief Add SIZE2 option to the packet. * @brief Add SIZE2 option to the packet.
* *
* @param pkt Packet to be updated * @param zpkt Packet to be updated
* @param ctx Block context from which to retrieve the * @param ctx Block context from which to retrieve the
* information for the Size2 option * information for the Size2 option
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_add_size2_option(struct zoap_packet *pkt, int zoap_add_size2_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx); struct zoap_block_context *ctx);
/** /**
* @brief Retrieves BLOCK{1,2} and SIZE{1,2} from @a pkt and updates * @brief Retrieves BLOCK{1,2} and SIZE{1,2} from @a zpkt and updates
* @a ctx accordingly. * @a ctx accordingly.
* *
* @param pkt Packet in which to look for block-wise transfers options * @param zpkt Packet in which to look for block-wise transfers options
* @param ctx Block context to be updated * @param ctx Block context to be updated
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_update_from_block(const struct zoap_packet *pkt, int zoap_update_from_block(const struct zoap_packet *zpkt,
struct zoap_block_context *ctx); struct zoap_block_context *ctx);
/** /**
@ -696,93 +696,93 @@ size_t zoap_next_block(struct zoap_block_context *ctx);
/** /**
* @brief Returns the version present in a CoAP packet. * @brief Returns the version present in a CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* *
* @return the CoAP version in packet * @return the CoAP version in packet
*/ */
uint8_t zoap_header_get_version(const struct zoap_packet *pkt); uint8_t zoap_header_get_version(const struct zoap_packet *zpkt);
/** /**
* @brief Returns the type of the CoAP packet. * @brief Returns the type of the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* *
* @return the type of the packet * @return the type of the packet
*/ */
uint8_t zoap_header_get_type(const struct zoap_packet *pkt); uint8_t zoap_header_get_type(const struct zoap_packet *zpkt);
/** /**
* @brief Returns the token (if any) in the CoAP packet. * @brief Returns the token (if any) in the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* @param len Where to store the length of the token * @param len Where to store the length of the token
* *
* @return pointer to the start of the token in the CoAP packet. * @return pointer to the start of the token in the CoAP packet.
*/ */
const uint8_t *zoap_header_get_token(const struct zoap_packet *pkt, const uint8_t *zoap_header_get_token(const struct zoap_packet *zpkt,
uint8_t *len); uint8_t *len);
/** /**
* @brief Returns the code of the CoAP packet. * @brief Returns the code of the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* *
* @return the code present in the packet * @return the code present in the packet
*/ */
uint8_t zoap_header_get_code(const struct zoap_packet *pkt); uint8_t zoap_header_get_code(const struct zoap_packet *zpkt);
/** /**
* @brief Returns the message id associated with the CoAP packet. * @brief Returns the message id associated with the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* *
* @return the message id present in the packet * @return the message id present in the packet
*/ */
uint16_t zoap_header_get_id(const struct zoap_packet *pkt); uint16_t zoap_header_get_id(const struct zoap_packet *zpkt);
/** /**
* @brief Sets the version of the CoAP packet. * @brief Sets the version of the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* @param ver The CoAP version to set in the packet * @param ver The CoAP version to set in the packet
*/ */
void zoap_header_set_version(struct zoap_packet *pkt, uint8_t ver); void zoap_header_set_version(struct zoap_packet *zpkt, uint8_t ver);
/** /**
* @brief Sets the type of the CoAP packet. * @brief Sets the type of the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* @param type The packet type to set * @param type The packet type to set
*/ */
void zoap_header_set_type(struct zoap_packet *pkt, uint8_t type); void zoap_header_set_type(struct zoap_packet *zpkt, uint8_t type);
/** /**
* @brief Sets the token in the CoAP packet. * @brief Sets the token in the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* @param token Token to set in the packet, will be copied * @param token Token to set in the packet, will be copied
* @param tokenlen Size of the token to be set, 8 bytes maximum * @param tokenlen Size of the token to be set, 8 bytes maximum
* *
* @return 0 in case of success or negative in case of error. * @return 0 in case of success or negative in case of error.
*/ */
int zoap_header_set_token(struct zoap_packet *pkt, const uint8_t *token, int zoap_header_set_token(struct zoap_packet *zpkt, const uint8_t *token,
uint8_t tokenlen); uint8_t tokenlen);
/** /**
* @brief Sets the code present in the CoAP packet. * @brief Sets the code present in the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* @param code The code set in the packet * @param code The code set in the packet
*/ */
void zoap_header_set_code(struct zoap_packet *pkt, uint8_t code); void zoap_header_set_code(struct zoap_packet *zpkt, uint8_t code);
/** /**
* @brief Sets the message id present in the CoAP packet. * @brief Sets the message id present in the CoAP packet.
* *
* @param pkt CoAP packet representation * @param zpkt CoAP packet representation
* @param id The message id to set in the packet * @param id The message id to set in the packet
*/ */
void zoap_header_set_id(struct zoap_packet *pkt, uint16_t id); void zoap_header_set_id(struct zoap_packet *zpkt, uint16_t id);
/** /**
* @brief Helper to generate message ids * @brief Helper to generate message ids

View file

@ -150,28 +150,29 @@ static inline bool get_context(struct net_context **udp_recv6,
return true; return true;
} }
static struct net_buf *build_reply_buf(const char *name, static struct net_pkt *build_reply_pkt(const char *name,
struct net_context *context, struct net_context *context,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct net_buf *reply_buf, *tmp; struct net_pkt *reply_pkt;
struct net_buf *tmp;
int header_len, recv_len, reply_len; int header_len, recv_len, reply_len;
printk("%s received %d bytes", name, printk("%s received %d bytes", name,
net_pkt_appdatalen(buf)); net_pkt_appdatalen(pkt));
reply_buf = net_pkt_get_tx(context, K_FOREVER); reply_pkt = net_pkt_get_tx(context, K_FOREVER);
recv_len = net_buf_frags_len(buf->frags); recv_len = net_pkt_get_len(pkt);
tmp = buf->frags; tmp = pkt->frags;
/* Remove frag link so original buf can be unrefed */ /* Remove frag link so original pkt can be unrefed */
buf->frags = NULL; pkt->frags = NULL;
/* First fragment will contain IP header so move the data /* First fragment will contain IP header so move the data
* down in order to get rid of it. * down in order to get rid of it.
*/ */
header_len = net_pkt_appdata(buf) - tmp->data; header_len = net_pkt_appdata(pkt) - tmp->data;
/* After this pull, the tmp->data points directly to application /* After this pull, the tmp->data points directly to application
* data. * data.
@ -179,14 +180,14 @@ static struct net_buf *build_reply_buf(const char *name,
net_buf_pull(tmp, header_len); net_buf_pull(tmp, header_len);
/* Add the entire chain into reply */ /* Add the entire chain into reply */
net_buf_frag_add(reply_buf, tmp); net_pkt_frag_add(reply_pkt, tmp);
reply_len = net_buf_frags_len(reply_buf->frags); reply_len = net_pkt_get_len(reply_pkt);
printk("Received %d bytes, sending %d bytes", recv_len - header_len, printk("Received %d bytes, sending %d bytes", recv_len - header_len,
reply_len); reply_len);
return reply_buf; return reply_pkt;
} }
static inline void pkt_sent(struct net_context *context, static inline void pkt_sent(struct net_context *context,
@ -200,45 +201,45 @@ static inline void pkt_sent(struct net_context *context,
} }
static inline void set_dst_addr(sa_family_t family, static inline void set_dst_addr(sa_family_t family,
struct net_buf *buf, struct net_pkt *pkt,
struct sockaddr *dst_addr) struct sockaddr *dst_addr)
{ {
net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr, net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr,
&NET_IPV6_BUF(buf)->src); &NET_IPV6_BUF(pkt)->src);
net_sin6(dst_addr)->sin6_family = AF_INET6; net_sin6(dst_addr)->sin6_family = AF_INET6;
net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(buf)->src_port; net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(pkt)->src_port;
} }
static void udp_received(struct net_context *context, static void udp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
struct net_buf *reply_buf; struct net_pkt *reply_pkt;
struct sockaddr dst_addr; struct sockaddr dst_addr;
sa_family_t family = net_pkt_family(buf); sa_family_t family = net_pkt_family(pkt);
static char dbg[MAX_DBG_PRINT + 1]; static char dbg[MAX_DBG_PRINT + 1];
int ret; int ret;
snprintf(dbg, MAX_DBG_PRINT, "UDP IPv%c", snprintf(dbg, MAX_DBG_PRINT, "UDP IPv%c",
family == AF_INET6 ? '6' : '4'); family == AF_INET6 ? '6' : '4');
set_dst_addr(family, buf, &dst_addr); set_dst_addr(family, pkt, &dst_addr);
reply_buf = build_reply_buf(dbg, context, buf); reply_pkt = build_reply_pkt(dbg, context, pkt);
net_pkt_unref(buf); net_pkt_unref(pkt);
ret = net_context_sendto(reply_buf, &dst_addr, ret = net_context_sendto(reply_pkt, &dst_addr,
family == AF_INET6 ? family == AF_INET6 ?
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in6) :
sizeof(struct sockaddr_in), sizeof(struct sockaddr_in),
pkt_sent, 0, pkt_sent, 0,
UINT_TO_POINTER(net_buf_frags_len(reply_buf)), UINT_TO_POINTER(net_pkt_get_len(reply_pkt)),
user_data); user_data);
if (ret < 0) { if (ret < 0) {
printk("Cannot send data to peer (%d)", ret); printk("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_buf); net_pkt_unref(reply_pkt);
} }
} }
@ -253,28 +254,28 @@ static void setup_udp_recv(struct net_context *udp_recv6)
} }
static void tcp_received(struct net_context *context, static void tcp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
static char dbg[MAX_DBG_PRINT + 1]; static char dbg[MAX_DBG_PRINT + 1];
sa_family_t family = net_pkt_family(buf); sa_family_t family = net_pkt_family(pkt);
struct net_buf *reply_buf; struct net_pkt *reply_pkt;
int ret; int ret;
snprintf(dbg, MAX_DBG_PRINT, "TCP IPv%c", snprintf(dbg, MAX_DBG_PRINT, "TCP IPv%c",
family == AF_INET6 ? '6' : '4'); family == AF_INET6 ? '6' : '4');
reply_buf = build_reply_buf(dbg, context, buf); reply_pkt = build_reply_pkt(dbg, context, pkt);
net_buf_unref(buf); net_pkt_unref(pkt);
ret = net_context_send(reply_buf, pkt_sent, K_NO_WAIT, ret = net_context_send(reply_pkt, pkt_sent, K_NO_WAIT,
UINT_TO_POINTER(net_buf_frags_len(reply_buf)), UINT_TO_POINTER(net_pkt_get_len(reply_pkt)),
NULL); NULL);
if (ret < 0) { if (ret < 0) {
printk("Cannot send data to peer (%d)", ret); printk("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_buf); net_pkt_unref(reply_pkt);
quit(); quit();
} }

View file

@ -70,7 +70,7 @@ struct zoap_reply replies[NUM_REPLIES];
#define ZOAP_BUF_SIZE 128 #define ZOAP_BUF_SIZE 128
NET_BUF_POOL_DEFINE(zoap_pkt_pool, 4, 0, sizeof(struct net_pkt), NULL); NET_PKT_TX_SLAB_DEFINE(zoap_pkt_slab, 4);
NET_BUF_POOL_DEFINE(zoap_data_pool, 4, ZOAP_BUF_SIZE, 0, NULL); NET_BUF_POOL_DEFINE(zoap_data_pool, 4, ZOAP_BUF_SIZE, 0, NULL);
static const char *const test_path[] = { "test", NULL }; static const char *const test_path[] = { "test", NULL };
@ -100,7 +100,7 @@ static int resource_reply_cb(const struct zoap_packet *response,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *frag = response->buf->frags; struct net_buf *frag = response->pkt->frags;
while (frag) { while (frag) {
msg_dump("reply", frag->data, frag->len); msg_dump("reply", frag->data, frag->len);
@ -184,9 +184,10 @@ void dtls_client(void)
int ret; int ret;
struct udp_context ctx; struct udp_context ctx;
struct dtls_timing_context timer; struct dtls_timing_context timer;
struct zoap_packet request, pkt; struct zoap_packet request, zkt;
struct zoap_reply *reply; struct zoap_reply *reply;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
uint8_t observe = 0; uint8_t observe = 0;
const char *const *p; const char *const *p;
uint16_t len; uint16_t len;
@ -281,8 +282,8 @@ void dtls_client(void)
/* Write to server */ /* Write to server */
retry: retry:
buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT);
if (!buf) { if (!pkt) {
goto exit; goto exit;
} }
@ -291,9 +292,9 @@ retry:
goto exit; goto exit;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
ret = zoap_packet_init(&request, buf); ret = zoap_packet_init(&request, pkt);
if (ret < 0) { if (ret < 0) {
goto exit; goto exit;
} }
@ -336,7 +337,7 @@ retry:
} while (ret == MBEDTLS_ERR_SSL_WANT_READ || } while (ret == MBEDTLS_ERR_SSL_WANT_READ ||
ret == MBEDTLS_ERR_SSL_WANT_WRITE); ret == MBEDTLS_ERR_SSL_WANT_WRITE);
net_buf_unref(buf); net_pkt_unref(pkt);
if (ret <= 0) { if (ret <= 0) {
mbedtls_printf("mbedtls_ssl_write failed returned 0x%x\n", mbedtls_printf("mbedtls_ssl_write failed returned 0x%x\n",
@ -344,9 +345,9 @@ retry:
goto exit; goto exit;
} }
buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT);
if (!buf) { if (!pkt) {
mbedtls_printf("Could not get buffer from pool\n"); mbedtls_printf("Could not get packet from pool\n");
goto exit; goto exit;
} }
@ -356,7 +357,7 @@ retry:
goto exit; goto exit;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
len = ZOAP_BUF_SIZE - 1; len = ZOAP_BUF_SIZE - 1;
memset(frag->data, 0, ZOAP_BUF_SIZE); memset(frag->data, 0, ZOAP_BUF_SIZE);
@ -366,7 +367,7 @@ retry:
ret == MBEDTLS_ERR_SSL_WANT_WRITE); ret == MBEDTLS_ERR_SSL_WANT_WRITE);
if (ret <= 0) { if (ret <= 0) {
net_buf_unref(buf); net_pkt_unref(pkt);
switch (ret) { switch (ret) {
case MBEDTLS_ERR_SSL_TIMEOUT: case MBEDTLS_ERR_SSL_TIMEOUT:
@ -388,18 +389,18 @@ retry:
len = ret; len = ret;
frag->len = len; frag->len = len;
ret = zoap_packet_parse(&pkt, buf); ret = zoap_packet_parse(&zpkt, pkt);
if (ret) { if (ret) {
mbedtls_printf("Could not parse packet\n"); mbedtls_printf("Could not parse packet\n");
goto exit; goto exit;
} }
reply = zoap_response_received(&pkt, NULL, replies, NUM_REPLIES); reply = zoap_response_received(&zpkt, NULL, replies, NUM_REPLIES);
if (!reply) { if (!reply) {
mbedtls_printf("No handler for response (%d)\n", ret); mbedtls_printf("No handler for response (%d)\n", ret);
} }
net_buf_unref(buf); net_pkt_unref(pkt);
mbedtls_ssl_close_notify(&ssl); mbedtls_ssl_close_notify(&ssl);
exit: exit:

View file

@ -37,14 +37,14 @@ static void set_destination(struct sockaddr *addr)
} }
static void udp_received(struct net_context *context, static void udp_received(struct net_context *context,
struct net_buf *buf, int status, void *user_data) struct net_pkt *pkt, int status, void *user_data)
{ {
struct udp_context *ctx = user_data; struct udp_context *ctx = user_data;
ARG_UNUSED(context); ARG_UNUSED(context);
ARG_UNUSED(status); ARG_UNUSED(status);
ctx->rx_pkt = buf; ctx->rx_pkt = pkt;
k_sem_give(&ctx->rx_sem); k_sem_give(&ctx->rx_sem);
} }
@ -52,30 +52,30 @@ int udp_tx(void *context, const unsigned char *buf, size_t size)
{ {
struct udp_context *ctx = context; struct udp_context *ctx = context;
struct net_context *udp_ctx; struct net_context *udp_ctx;
struct net_buf *send_buf; struct net_pkt *send_pkt;
struct sockaddr dst_addr; struct sockaddr dst_addr;
int rc, len; int rc, len;
udp_ctx = ctx->net_ctx; udp_ctx = ctx->net_ctx;
send_buf = net_pkt_get_tx(udp_ctx, K_FOREVER); send_pkt = net_pkt_get_tx(udp_ctx, K_FOREVER);
if (!send_buf) { if (!send_pkt) {
return MBEDTLS_ERR_SSL_ALLOC_FAILED; return MBEDTLS_ERR_SSL_ALLOC_FAILED;
} }
rc = net_pkt_append(send_buf, size, (uint8_t *) buf, K_FOREVER); rc = net_pkt_append(send_pkt, size, (uint8_t *) buf, K_FOREVER);
if (!rc) { if (!rc) {
return MBEDTLS_ERR_SSL_INTERNAL_ERROR; return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
} }
set_destination(&dst_addr); set_destination(&dst_addr);
len = net_buf_frags_len(send_buf); len = net_pkt_frags_len(send_pkt);
k_sleep(UDP_TX_TIMEOUT); k_sleep(UDP_TX_TIMEOUT);
rc = net_context_sendto(send_buf, &dst_addr, rc = net_context_sendto(send_pkt, &dst_addr,
addrlen, NULL, K_FOREVER, NULL, NULL); addrlen, NULL, K_FOREVER, NULL, NULL);
if (rc < 0) { if (rc < 0) {
net_pkt_unref(send_buf); net_pkt_unref(send_pkt);
return MBEDTLS_ERR_SSL_INTERNAL_ERROR; return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
} else { } else {
return len; return len;

View file

@ -11,7 +11,7 @@
struct udp_context { struct udp_context {
struct net_context *net_ctx; struct net_context *net_ctx;
struct net_buf *rx_pkt; struct net_pkt *rx_pkt;
struct k_sem rx_sem; struct k_sem rx_sem;
int remaining; int remaining;
}; };

View file

@ -60,7 +60,7 @@ static unsigned char heap[8192];
#define ZOAP_BUF_SIZE 128 #define ZOAP_BUF_SIZE 128
NET_BUF_POOL_DEFINE(zoap_pkt_pool, 4, 0, sizeof(struct net_pkt), NULL); NET_PKT_TX_SLAB_DEFINE(zoap_pkt_slab, 4);
NET_BUF_POOL_DEFINE(zoap_data_pool, 4, ZOAP_BUF_SIZE, 0, NULL); NET_BUF_POOL_DEFINE(zoap_data_pool, 4, ZOAP_BUF_SIZE, 0, NULL);
/* /*
@ -78,7 +78,8 @@ static mbedtls_ssl_context *curr_ctx;
static int send_response(struct zoap_packet *request, uint8_t response_code) static int send_response(struct zoap_packet *request, uint8_t response_code)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t code, type; uint8_t code, type;
uint16_t id; uint16_t id;
@ -92,8 +93,8 @@ static int send_response(struct zoap_packet *request, uint8_t response_code)
printk("type: %u code %u id %u\n", type, code, id); printk("type: %u code %u id %u\n", type, code, id);
printk("*******\n"); printk("*******\n");
buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
@ -102,9 +103,9 @@ static int send_response(struct zoap_packet *request, uint8_t response_code)
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -123,7 +124,7 @@ static int send_response(struct zoap_packet *request, uint8_t response_code)
r = 0; r = 0;
} }
net_buf_unref(buf); net_pkt_unref(pkt);
return r; return r;
} }
@ -150,7 +151,8 @@ static int piggyback_get(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload, code, type; uint8_t *payload, code, type;
uint16_t len, id; uint16_t len, id;
@ -164,8 +166,8 @@ static int piggyback_get(struct zoap_resource *resource,
printk("type: %u code %u id %u\n", type, code, id); printk("type: %u code %u id %u\n", type, code, id);
printk("*******\n"); printk("*******\n");
buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
@ -174,9 +176,9 @@ static int piggyback_get(struct zoap_resource *resource,
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -212,7 +214,7 @@ static int piggyback_get(struct zoap_resource *resource,
r = 0; r = 0;
} }
net_buf_unref(buf); net_pkt_unref(pkt);
return r; return r;
} }
@ -221,7 +223,8 @@ static int query_get(struct zoap_resource *resource,
struct zoap_packet *request, const struct sockaddr *from) struct zoap_packet *request, const struct sockaddr *from)
{ {
struct zoap_option options[4]; struct zoap_option options[4];
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload, code, type; uint8_t *payload, code, type;
uint16_t len, id; uint16_t len, id;
@ -258,8 +261,8 @@ static int query_get(struct zoap_resource *resource,
printk("*******\n"); printk("*******\n");
buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
@ -268,9 +271,9 @@ static int query_get(struct zoap_resource *resource,
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -307,7 +310,7 @@ static int query_get(struct zoap_resource *resource,
r = 0; r = 0;
} }
net_buf_unref(buf); net_pkt_unref(pkt);
return r; return r;
} }
@ -416,8 +419,9 @@ void dtls_server(void)
int len, ret = 0; int len, ret = 0;
struct udp_context ctx; struct udp_context ctx;
struct dtls_timing_context timer; struct dtls_timing_context timer;
struct zoap_packet pkt; struct zoap_packet zpkt;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
mbedtls_ssl_cookie_ctx cookie_ctx; mbedtls_ssl_cookie_ctx cookie_ctx;
mbedtls_entropy_context entropy; mbedtls_entropy_context entropy;
@ -549,9 +553,9 @@ reset:
do { do {
/* Read the request */ /* Read the request */
buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT);
if (!buf) { if (!pkt) {
mbedtls_printf("Could not get buffer from pool\n"); mbedtls_printf("Could not get packet from slab\n");
goto exit; goto exit;
} }
@ -561,7 +565,7 @@ reset:
goto exit; goto exit;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
len = ZOAP_BUF_SIZE - 1; len = ZOAP_BUF_SIZE - 1;
memset(frag->data, 0, ZOAP_BUF_SIZE); memset(frag->data, 0, ZOAP_BUF_SIZE);
@ -572,7 +576,7 @@ reset:
} }
if (ret <= 0) { if (ret <= 0) {
net_buf_unref(buf); net_pkt_unref(pkt);
switch (ret) { switch (ret) {
case MBEDTLS_ERR_SSL_TIMEOUT: case MBEDTLS_ERR_SSL_TIMEOUT:
@ -594,20 +598,20 @@ reset:
len = ret; len = ret;
frag->len = len; frag->len = len;
ret = zoap_packet_parse(&pkt, buf); ret = zoap_packet_parse(&zpkt, pkt);
if (ret) { if (ret) {
mbedtls_printf("Could not parse packet\n"); mbedtls_printf("Could not parse packet\n");
goto exit; goto exit;
} }
ret = zoap_handle_request(&pkt, resources, ret = zoap_handle_request(&zpkt, resources,
(const struct sockaddr *)&ssl); (const struct sockaddr *)&ssl);
if (ret < 0) { if (ret < 0) {
mbedtls_printf("No handler for such request (%d)\n", mbedtls_printf("No handler for such request (%d)\n",
ret); ret);
} }
net_buf_unref(buf); net_pkt_unref(pkt);
} while (1); } while (1);

View file

@ -18,22 +18,22 @@
static const socklen_t addrlen = sizeof(struct sockaddr_in6); static const socklen_t addrlen = sizeof(struct sockaddr_in6);
static void set_client_address(struct sockaddr *addr, struct net_buf *rx_buf) static void set_client_address(struct sockaddr *addr, struct net_pkt *rx_pkt)
{ {
net_ipaddr_copy(&net_sin6(addr)->sin6_addr, &NET_IPV6_BUF(rx_buf)->src); net_ipaddr_copy(&net_sin6(addr)->sin6_addr, &NET_IPV6_BUF(rx_pkt)->src);
net_sin6(addr)->sin6_family = AF_INET6; net_sin6(addr)->sin6_family = AF_INET6;
net_sin6(addr)->sin6_port = NET_UDP_BUF(rx_buf)->src_port; net_sin6(addr)->sin6_port = NET_UDP_BUF(rx_pkt)->src_port;
} }
static void udp_received(struct net_context *context, static void udp_received(struct net_context *context,
struct net_buf *buf, int status, void *user_data) struct net_pkt *pkt, int status, void *user_data)
{ {
struct udp_context *ctx = user_data; struct udp_context *ctx = user_data;
ARG_UNUSED(context); ARG_UNUSED(context);
ARG_UNUSED(status); ARG_UNUSED(status);
ctx->rx_pkt = buf; ctx->rx_pkt = pkt;
k_sem_give(&ctx->rx_sem); k_sem_give(&ctx->rx_sem);
} }
@ -41,32 +41,32 @@ int udp_tx(void *context, const unsigned char *buf, size_t size)
{ {
struct udp_context *ctx = context; struct udp_context *ctx = context;
struct net_context *net_ctx; struct net_context *net_ctx;
struct net_buf *send_buf; struct net_pkt *send_pkt;
int rc, len; int rc, len;
net_ctx = ctx->net_ctx; net_ctx = ctx->net_ctx;
send_buf = net_pkt_get_tx(net_ctx, K_FOREVER); send_pkt = net_pkt_get_tx(net_ctx, K_FOREVER);
if (!send_buf) { if (!send_pkt) {
printk("cannot create buf\n"); printk("cannot create pkt\n");
return -EIO; return -EIO;
} }
rc = net_pkt_append(send_buf, size, (uint8_t *) buf, K_FOREVER); rc = net_pkt_append(send_pkt, size, (uint8_t *) buf, K_FOREVER);
if (!rc) { if (!rc) {
printk("cannot write buf\n"); printk("cannot write buf\n");
return -EIO; return -EIO;
} }
len = net_buf_frags_len(send_buf); len = net_pkt_frags_len(send_pkt);
rc = net_context_sendto(send_buf, &net_ctx->remote, rc = net_context_sendto(send_pkt, &net_ctx->remote,
addrlen, NULL, K_FOREVER, NULL, NULL); addrlen, NULL, K_FOREVER, NULL, NULL);
if (rc < 0) { if (rc < 0) {
printk("Cannot send data to peer (%d)\n", rc); printk("Cannot send data to peer (%d)\n", rc);
net_pkt_unref(send_buf); net_pkt_unref(send_pkt);
return -EIO; return -EIO;
} else { } else {
return len; return len;
@ -77,7 +77,8 @@ int udp_rx(void *context, unsigned char *buf, size_t size)
{ {
struct udp_context *ctx = context; struct udp_context *ctx = context;
struct net_context *net_ctx = ctx->net_ctx; struct net_context *net_ctx = ctx->net_ctx;
struct net_buf *rx_buf = NULL; struct net_pkt *rx_pkt = NULL;
struct net_buf *rx_buf;
uint16_t read_bytes; uint16_t read_bytes;
uint8_t *ptr; uint8_t *ptr;
int pos; int pos;
@ -91,12 +92,12 @@ int udp_rx(void *context, unsigned char *buf, size_t size)
return -ENOMEM; return -ENOMEM;
} }
rx_buf = ctx->rx_pkt; rx_pkt = ctx->rx_pkt;
set_client_address(&net_ctx->remote, rx_buf); set_client_address(&net_ctx->remote, rx_pkt);
ptr = net_pkt_appdata(rx_buf); ptr = net_pkt_appdata(rx_pkt);
rx_buf = rx_buf->frags; rx_buf = rx_pkt->frags;
len = rx_buf->len - (ptr - rx_buf->data); len = rx_buf->len - (ptr - rx_buf->data);
pos = 0; pos = 0;

View file

@ -11,7 +11,7 @@
struct udp_context { struct udp_context {
struct net_context *net_ctx; struct net_context *net_ctx;
struct net_buf *rx_pkt; struct net_pkt *rx_pkt;
struct k_sem rx_sem; struct k_sem rx_sem;
int remaining; int remaining;
char client_id; char client_id;

View file

@ -77,10 +77,10 @@ static int ipsum_len;
*/ */
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
NET_PKT_TX_POOL_DEFINE(echo_tx_tcp, 15); NET_PKT_TX_SLAB_DEFINE(echo_tx_tcp, 15);
NET_PKT_DATA_POOL_DEFINE(echo_data_tcp, 30); NET_PKT_DATA_POOL_DEFINE(echo_data_tcp, 30);
static struct net_buf_pool *tx_tcp_pool(void) static struct k_mem_slab *tx_tcp_slab(void)
{ {
return &echo_tx_tcp; return &echo_tx_tcp;
} }
@ -92,10 +92,10 @@ static struct net_buf_pool *data_tcp_pool(void)
#endif #endif
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
NET_PKT_TX_POOL_DEFINE(echo_tx_udp, 5); NET_PKT_TX_SLAB_DEFINE(echo_tx_udp, 5);
NET_PKT_DATA_POOL_DEFINE(echo_data_udp, 20); NET_PKT_DATA_POOL_DEFINE(echo_data_udp, 20);
static struct net_buf_pool *tx_udp_pool(void) static struct k_mem_slab *tx_udp_slab(void)
{ {
return &echo_tx_udp; return &echo_tx_udp;
} }
@ -294,7 +294,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*udp_recv6, tx_udp_pool, data_udp_pool); net_context_setup_pools(*udp_recv6, tx_udp_slab, data_udp_pool);
ret = net_context_bind(*udp_recv6, (struct sockaddr *)&my_addr6, ret = net_context_bind(*udp_recv6, (struct sockaddr *)&my_addr6,
sizeof(struct sockaddr_in6)); sizeof(struct sockaddr_in6));
@ -313,7 +313,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*udp_recv4, tx_udp_pool, data_udp_pool); net_context_setup_pools(*udp_recv4, tx_udp_slab, data_udp_pool);
ret = net_context_bind(*udp_recv4, (struct sockaddr *)&my_addr4, ret = net_context_bind(*udp_recv4, (struct sockaddr *)&my_addr4,
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
@ -334,7 +334,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*tcp_recv6, tx_tcp_pool, data_tcp_pool); net_context_setup_pools(*tcp_recv6, tx_tcp_slab, data_tcp_pool);
ret = net_context_bind(*tcp_recv6, ret = net_context_bind(*tcp_recv6,
(struct sockaddr *)&my_addr6, (struct sockaddr *)&my_addr6,
@ -356,7 +356,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*tcp_recv4, tx_tcp_pool, data_tcp_pool); net_context_setup_pools(*tcp_recv4, tx_tcp_slab, data_tcp_pool);
ret = net_context_bind(*tcp_recv4, ret = net_context_bind(*tcp_recv4,
(struct sockaddr *)&my_addr4, (struct sockaddr *)&my_addr4,
@ -389,25 +389,25 @@ static inline bool wait_reply(const char *name,
return false; return false;
} }
static struct net_buf *prepare_send_buf(const char *name, static struct net_pkt *prepare_send_pkt(const char *name,
struct net_context *context, struct net_context *context,
int expecting_len) int expecting_len)
{ {
struct net_buf *send_buf; struct net_pkt *send_pkt;
bool status; bool status;
send_buf = net_pkt_get_tx(context, K_FOREVER); send_pkt = net_pkt_get_tx(context, K_FOREVER);
NET_ASSERT(send_buf); NET_ASSERT(send_pkt);
status = net_pkt_append(send_buf, expecting_len, lorem_ipsum, status = net_pkt_append(send_pkt, expecting_len, lorem_ipsum,
K_FOREVER); K_FOREVER);
if (!status) { if (!status) {
NET_ERR("%s: cannot create send buf", name); NET_ERR("%s: cannot create send pkt", name);
return NULL; return NULL;
} }
return send_buf; return send_pkt;
} }
static inline void udp_sent(struct net_context *context, static inline void udp_sent(struct net_context *context,
@ -424,10 +424,10 @@ static inline void udp_sent(struct net_context *context,
} }
static inline void set_dst_addr(sa_family_t family, static inline void set_dst_addr(sa_family_t family,
struct net_buf *buf, struct net_pkt *pkt,
struct sockaddr *dst_addr) struct sockaddr *dst_addr)
{ {
ARG_UNUSED(buf); ARG_UNUSED(pkt);
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (family == AF_INET6) { if (family == AF_INET6) {
@ -453,37 +453,38 @@ static inline void set_dst_addr(sa_family_t family,
} }
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
static bool compare_udp_data(struct net_buf *buf, int expecting_len) static bool compare_udp_data(struct net_pkt *pkt, int expecting_len)
{ {
uint8_t *ptr = net_pkt_appdata(buf); uint8_t *ptr = net_pkt_appdata(pkt);
struct net_buf *frag;
int pos = 0; int pos = 0;
int len; int len;
/* Buf will now point to first fragment with IP header /* frag will now point to first fragment with IP header
* in it. * in it.
*/ */
buf = buf->frags; frag = pkt->frags;
/* Do not include the protocol headers in the first fragment. /* Do not include the protocol headers in the first fragment.
* The remaining fragments contain only data so the user data * The remaining fragments contain only data so the user data
* length is directly the fragment len. * length is directly the fragment len.
*/ */
len = buf->len - (ptr - buf->data); len = frag->len - (ptr - frag->data);
while (buf) { while (frag) {
if (memcmp(ptr, lorem_ipsum + pos, len)) { if (memcmp(ptr, lorem_ipsum + pos, len)) {
NET_DBG("Invalid data received"); NET_DBG("Invalid data received");
return false; return false;
} else { } else {
pos += len; pos += len;
buf = buf->frags; frag = frag->frags;
if (!buf) { if (!frag) {
break; break;
} }
ptr = buf->data; ptr = frag->data;
len = buf->len; len = frag->len;
} }
} }
@ -509,7 +510,7 @@ static bool send_udp_data(struct net_context *udp,
struct data *data) struct data *data)
{ {
bool status = false; bool status = false;
struct net_buf *send_buf; struct net_pkt *send_pkt;
struct sockaddr dst_addr; struct sockaddr dst_addr;
socklen_t addrlen; socklen_t addrlen;
size_t len; size_t len;
@ -517,18 +518,18 @@ static bool send_udp_data(struct net_context *udp,
data->expecting_udp = sys_rand32_get() % ipsum_len; data->expecting_udp = sys_rand32_get() % ipsum_len;
send_buf = prepare_send_buf(proto, udp, data->expecting_udp); send_pkt = prepare_send_pkt(proto, udp, data->expecting_udp);
if (!send_buf) { if (!send_pkt) {
goto out; goto out;
} }
len = net_buf_frags_len(send_buf); len = net_pkt_get_len(send_pkt);
NET_ASSERT_INFO(data->expecting_udp == len, NET_ASSERT_INFO(data->expecting_udp == len,
"Data to send %d bytes, real len %zu", "Data to send %d bytes, real len %zu",
data->expecting_udp, len); data->expecting_udp, len);
set_dst_addr(family, send_buf, &dst_addr); set_dst_addr(family, send_pkt, &dst_addr);
if (family == AF_INET6) { if (family == AF_INET6) {
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
@ -536,13 +537,13 @@ static bool send_udp_data(struct net_context *udp,
addrlen = sizeof(struct sockaddr_in); addrlen = sizeof(struct sockaddr_in);
} }
ret = net_context_sendto(send_buf, &dst_addr, ret = net_context_sendto(send_pkt, &dst_addr,
addrlen, udp_sent, 0, addrlen, udp_sent, 0,
UINT_TO_POINTER(len), UINT_TO_POINTER(len),
proto); proto);
if (ret < 0) { if (ret < 0) {
NET_ERR("Cannot send %s data to peer (%d)", proto, ret); NET_ERR("Cannot send %s data to peer (%d)", proto, ret);
net_pkt_unref(send_buf); net_pkt_unref(send_pkt);
} else { } else {
status = true; status = true;
} }
@ -552,11 +553,11 @@ out:
} }
static void udp_received(struct net_context *context, static void udp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
sa_family_t family = net_pkt_family(buf); sa_family_t family = net_pkt_family(pkt);
struct data *data = user_data; struct data *data = user_data;
struct k_sem *recv; struct k_sem *recv;
@ -569,16 +570,16 @@ static void udp_received(struct net_context *context,
recv = &conf.recv_ipv6; recv = &conf.recv_ipv6;
} }
if (data->expecting_udp != net_pkt_appdatalen(buf)) { if (data->expecting_udp != net_pkt_appdatalen(pkt)) {
NET_ERR("Sent %d bytes, received %u bytes", NET_ERR("Sent %d bytes, received %u bytes",
data->expecting_udp, net_pkt_appdatalen(buf)); data->expecting_udp, net_pkt_appdatalen(pkt));
} }
if (!compare_udp_data(buf, data->expecting_udp)) { if (!compare_udp_data(pkt, data->expecting_udp)) {
NET_DBG("Data mismatch"); NET_DBG("Data mismatch");
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
k_sem_give(recv); k_sem_give(recv);
} }
@ -613,17 +614,17 @@ static void send_udp(struct net_context *udp,
#endif /* CONFIG_NET_UDP */ #endif /* CONFIG_NET_UDP */
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
static bool compare_tcp_data(struct net_buf *buf, int expecting_len, static bool compare_tcp_data(struct net_pkt *pkt, int expecting_len,
int received_len) int received_len)
{ {
uint8_t *ptr = net_pkt_appdata(buf), *start; uint8_t *ptr = net_pkt_appdata(pkt), *start;
int pos = 0; int pos = 0;
struct net_buf *frag; struct net_buf *frag;
int len; int len;
/* frag will point to first fragment with IP header in it. /* frag will point to first fragment with IP header in it.
*/ */
frag = buf->frags; frag = pkt->frags;
/* Do not include the protocol headers for the first fragment. /* Do not include the protocol headers for the first fragment.
* The remaining fragments contain only data so the user data * The remaining fragments contain only data so the user data
@ -650,13 +651,13 @@ static bool compare_tcp_data(struct net_buf *buf, int expecting_len,
len = frag->len; len = frag->len;
} }
NET_DBG("Compared %d bytes, all ok", net_pkt_appdatalen(buf)); NET_DBG("Compared %d bytes, all ok", net_pkt_appdatalen(pkt));
return true; return true;
} }
static void tcp_received(struct net_context *context, static void tcp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
@ -665,27 +666,27 @@ static void tcp_received(struct net_context *context,
ARG_UNUSED(status); ARG_UNUSED(status);
if (!buf || net_pkt_appdatalen(buf) == 0) { if (!pkt || net_pkt_appdatalen(pkt) == 0) {
if (buf) { if (pkt) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return; return;
} }
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
proto = "IPv6"; proto = "IPv6";
} else { } else {
proto = "IPv4"; proto = "IPv4";
} }
NET_DBG("Sent %d bytes, received %u bytes", NET_DBG("Sent %d bytes, received %u bytes",
data->expecting_tcp, net_pkt_appdatalen(buf)); data->expecting_tcp, net_pkt_appdatalen(pkt));
if (!compare_tcp_data(buf, data->expecting_tcp, data->received_tcp)) { if (!compare_tcp_data(pkt, data->expecting_tcp, data->received_tcp)) {
NET_DBG("Data mismatch"); NET_DBG("Data mismatch");
} else { } else {
data->received_tcp += net_pkt_appdatalen(buf); data->received_tcp += net_pkt_appdatalen(pkt);
} }
if (data->expecting_tcp <= data->received_tcp) { if (data->expecting_tcp <= data->received_tcp) {
@ -693,7 +694,7 @@ static void tcp_received(struct net_context *context,
send_tcp_data(context, proto, data); send_tcp_data(context, proto, data);
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
static void setup_tcp_recv(struct net_context *tcp, static void setup_tcp_recv(struct net_context *tcp,
@ -729,7 +730,7 @@ static bool send_tcp_data(struct net_context *ctx,
char *proto, char *proto,
struct data *data) struct data *data)
{ {
struct net_buf *send_buf; struct net_pkt *send_pkt;
bool status = false; bool status = false;
size_t len; size_t len;
int ret; int ret;
@ -737,22 +738,22 @@ static bool send_tcp_data(struct net_context *ctx,
data->expecting_tcp = sys_rand32_get() % ipsum_len; data->expecting_tcp = sys_rand32_get() % ipsum_len;
data->received_tcp = 0; data->received_tcp = 0;
send_buf = prepare_send_buf(proto, ctx, data->expecting_tcp); send_pkt = prepare_send_pkt(proto, ctx, data->expecting_tcp);
if (!send_buf) { if (!send_pkt) {
goto out; goto out;
} }
len = net_buf_frags_len(send_buf); len = net_pkt_get_len(send_pkt);
NET_ASSERT_INFO(data->expecting_tcp == len, NET_ASSERT_INFO(data->expecting_tcp == len,
"%s data to send %d bytes, real len %zu", "%s data to send %d bytes, real len %zu",
proto, data->expecting_tcp, len); proto, data->expecting_tcp, len);
ret = net_context_send(send_buf, tcp_sent, 0, ret = net_context_send(send_pkt, tcp_sent, 0,
UINT_TO_POINTER(len), proto); UINT_TO_POINTER(len), proto);
if (ret < 0) { if (ret < 0) {
NET_ERR("Cannot send %s data to peer (%d)", proto, ret); NET_ERR("Cannot send %s data to peer (%d)", proto, ret);
net_pkt_unref(send_buf); net_pkt_unref(send_pkt);
} else { } else {
status = true; status = true;
} }

View file

@ -56,10 +56,10 @@ static struct in_addr in4addr_my = MY_IP4ADDR;
*/ */
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
NET_PKT_TX_POOL_DEFINE(echo_tx_tcp, 15); NET_PKT_TX_SLAB_DEFINE(echo_tx_tcp, 15);
NET_PKT_DATA_POOL_DEFINE(echo_data_tcp, 30); NET_PKT_DATA_POOL_DEFINE(echo_data_tcp, 30);
static struct net_buf_pool *tx_tcp_pool(void) static struct k_mem_slab *tx_tcp_slab(void)
{ {
return &echo_tx_tcp; return &echo_tx_tcp;
} }
@ -71,10 +71,10 @@ static struct net_buf_pool *data_tcp_pool(void)
#endif #endif
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
NET_PKT_TX_POOL_DEFINE(echo_tx_udp, 5); NET_PKT_TX_SLAB_DEFINE(echo_tx_udp, 5);
NET_PKT_DATA_POOL_DEFINE(echo_data_udp, 20); NET_PKT_DATA_POOL_DEFINE(echo_data_udp, 20);
static struct net_buf_pool *tx_udp_pool(void) static struct k_mem_slab *tx_udp_slab(void)
{ {
return &echo_tx_udp; return &echo_tx_udp;
} }
@ -184,7 +184,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*udp_recv6, tx_udp_pool, data_udp_pool); net_context_setup_pools(*udp_recv6, tx_udp_slab, data_udp_pool);
ret = net_context_bind(*udp_recv6, (struct sockaddr *)&my_addr6, ret = net_context_bind(*udp_recv6, (struct sockaddr *)&my_addr6,
sizeof(struct sockaddr_in6)); sizeof(struct sockaddr_in6));
@ -203,7 +203,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*udp_recv4, tx_udp_pool, data_udp_pool); net_context_setup_pools(*udp_recv4, tx_udp_slab, data_udp_pool);
ret = net_context_bind(*udp_recv4, (struct sockaddr *)&my_addr4, ret = net_context_bind(*udp_recv4, (struct sockaddr *)&my_addr4,
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
@ -224,7 +224,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*tcp_recv6, tx_tcp_pool, data_tcp_pool); net_context_setup_pools(*tcp_recv6, tx_tcp_slab, data_tcp_pool);
ret = net_context_bind(*tcp_recv6, ret = net_context_bind(*tcp_recv6,
(struct sockaddr *)&my_addr6, (struct sockaddr *)&my_addr6,
@ -252,7 +252,7 @@ static inline bool get_context(struct net_context **udp_recv4,
return false; return false;
} }
net_context_setup_pools(*tcp_recv4, tx_tcp_pool, data_tcp_pool); net_context_setup_pools(*tcp_recv4, tx_tcp_slab, data_tcp_pool);
ret = net_context_bind(*tcp_recv4, ret = net_context_bind(*tcp_recv4,
(struct sockaddr *)&my_addr4, (struct sockaddr *)&my_addr4,
@ -274,32 +274,33 @@ static inline bool get_context(struct net_context **udp_recv4,
return true; return true;
} }
static struct net_buf *build_reply_buf(const char *name, static struct net_pkt *build_reply_pkt(const char *name,
struct net_context *context, struct net_context *context,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct net_buf *reply_buf, *frag, *tmp; struct net_pkt *reply_pkt;
struct net_buf *frag, *tmp;
int header_len, recv_len, reply_len; int header_len, recv_len, reply_len;
NET_INFO("%s received %d bytes", name, NET_INFO("%s received %d bytes", name,
net_pkt_appdatalen(buf)); net_pkt_appdatalen(pkt));
if (net_pkt_appdatalen(buf) == 0) { if (net_pkt_appdatalen(pkt) == 0) {
return NULL; return NULL;
} }
reply_buf = net_pkt_get_tx(context, K_FOREVER); reply_pkt = net_pkt_get_tx(context, K_FOREVER);
NET_ASSERT(reply_buf); NET_ASSERT(reply_pkt);
recv_len = net_buf_frags_len(buf->frags); recv_len = net_pkt_get_len(pkt);
tmp = buf->frags; tmp = pkt->frags;
/* First fragment will contain IP header so move the data /* First fragment will contain IP header so move the data
* down in order to get rid of it. * down in order to get rid of it.
*/ */
header_len = net_pkt_appdata(buf) - tmp->data; header_len = net_pkt_appdata(pkt) - tmp->data;
NET_ASSERT(header_len < CONFIG_NET_BUF_DATA_SIZE); NET_ASSERT(header_len < CONFIG_NET_BUF_DATA_SIZE);
@ -326,25 +327,25 @@ static struct net_buf *build_reply_buf(const char *name,
* in sending side we add the link layer * in sending side we add the link layer
* header if needed. * header if needed.
*/ */
net_pkt_set_ll_reserve(reply_buf, 0); net_pkt_set_ll_reserve(reply_pkt, 0);
} }
NET_ASSERT(net_buf_tailroom(frag) >= tmp->len); NET_ASSERT(net_buf_tailroom(frag) >= tmp->len);
memcpy(net_buf_add(frag, tmp->len), tmp->data, tmp->len); memcpy(net_buf_add(frag, tmp->len), tmp->data, tmp->len);
net_buf_frag_add(reply_buf, frag); net_pkt_frag_add(reply_pkt, frag);
tmp = net_pkt_frag_del(buf, tmp); tmp = net_pkt_frag_del(pkt, NULL, tmp);
} }
reply_len = net_buf_frags_len(reply_buf->frags); reply_len = net_pkt_get_len(reply_pkt);
NET_ASSERT_INFO((recv_len - header_len) == reply_len, NET_ASSERT_INFO((recv_len - header_len) == reply_len,
"Received %d bytes, sending %d bytes", "Received %d bytes, sending %d bytes",
recv_len - header_len, reply_len); recv_len - header_len, reply_len);
return reply_buf; return reply_pkt;
} }
static inline void pkt_sent(struct net_context *context, static inline void pkt_sent(struct net_context *context,
@ -359,62 +360,62 @@ static inline void pkt_sent(struct net_context *context,
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
static inline void set_dst_addr(sa_family_t family, static inline void set_dst_addr(sa_family_t family,
struct net_buf *buf, struct net_pkt *pkt,
struct sockaddr *dst_addr) struct sockaddr *dst_addr)
{ {
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (family == AF_INET6) { if (family == AF_INET6) {
net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr, net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr,
&NET_IPV6_BUF(buf)->src); &NET_IPV6_BUF(pkt)->src);
net_sin6(dst_addr)->sin6_family = AF_INET6; net_sin6(dst_addr)->sin6_family = AF_INET6;
net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(buf)->src_port; net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(pkt)->src_port;
} }
#endif /* CONFIG_NET_IPV6) */ #endif /* CONFIG_NET_IPV6) */
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (family == AF_INET) { if (family == AF_INET) {
net_ipaddr_copy(&net_sin(dst_addr)->sin_addr, net_ipaddr_copy(&net_sin(dst_addr)->sin_addr,
&NET_IPV4_BUF(buf)->src); &NET_IPV4_BUF(pkt)->src);
net_sin(dst_addr)->sin_family = AF_INET; net_sin(dst_addr)->sin_family = AF_INET;
net_sin(dst_addr)->sin_port = NET_UDP_BUF(buf)->src_port; net_sin(dst_addr)->sin_port = NET_UDP_BUF(pkt)->src_port;
} }
#endif /* CONFIG_NET_IPV6) */ #endif /* CONFIG_NET_IPV6) */
} }
static void udp_received(struct net_context *context, static void udp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
struct net_buf *reply_buf; struct net_pkt *reply_pkt;
struct sockaddr dst_addr; struct sockaddr dst_addr;
sa_family_t family = net_pkt_family(buf); sa_family_t family = net_pkt_family(pkt);
static char dbg[MAX_DBG_PRINT + 1]; static char dbg[MAX_DBG_PRINT + 1];
int ret; int ret;
snprintk(dbg, MAX_DBG_PRINT, "UDP IPv%c", snprintk(dbg, MAX_DBG_PRINT, "UDP IPv%c",
family == AF_INET6 ? '6' : '4'); family == AF_INET6 ? '6' : '4');
set_dst_addr(family, buf, &dst_addr); set_dst_addr(family, pkt, &dst_addr);
reply_buf = build_reply_buf(dbg, context, buf); reply_pkt = build_reply_pkt(dbg, context, pkt);
net_pkt_unref(buf); net_pkt_unref(pkt);
if (!reply_buf) { if (!reply_pkt) {
return; return;
} }
ret = net_context_sendto(reply_buf, &dst_addr, ret = net_context_sendto(reply_pkt, &dst_addr,
family == AF_INET6 ? family == AF_INET6 ?
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in6) :
sizeof(struct sockaddr_in), sizeof(struct sockaddr_in),
pkt_sent, 0, pkt_sent, 0,
UINT_TO_POINTER(net_buf_frags_len(reply_buf)), UINT_TO_POINTER(net_pkt_get_len(reply_pkt)),
user_data); user_data);
if (ret < 0) { if (ret < 0) {
NET_ERR("Cannot send data to peer (%d)", ret); NET_ERR("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_buf); net_pkt_unref(reply_pkt);
} }
} }
@ -441,39 +442,39 @@ static void setup_udp_recv(struct net_context *udp_recv4,
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
static void tcp_received(struct net_context *context, static void tcp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
static char dbg[MAX_DBG_PRINT + 1]; static char dbg[MAX_DBG_PRINT + 1];
struct net_buf *reply_buf; struct net_pkt *reply_pkt;
sa_family_t family; sa_family_t family;
int ret; int ret;
if (!buf) { if (!pkt) {
/* EOF condition */ /* EOF condition */
return; return;
} }
family = net_pkt_family(buf); family = net_pkt_family(pkt);
snprintk(dbg, MAX_DBG_PRINT, "TCP IPv%c", snprintk(dbg, MAX_DBG_PRINT, "TCP IPv%c",
family == AF_INET6 ? '6' : '4'); family == AF_INET6 ? '6' : '4');
reply_buf = build_reply_buf(dbg, context, buf); reply_pkt = build_reply_pkt(dbg, context, pkt);
net_pkt_unref(buf); net_pkt_unref(pkt);
if (!reply_buf) { if (!reply_pkt) {
return; return;
} }
ret = net_context_send(reply_buf, pkt_sent, K_NO_WAIT, ret = net_context_send(reply_pkt, pkt_sent, K_NO_WAIT,
UINT_TO_POINTER(net_buf_frags_len(reply_buf)), UINT_TO_POINTER(net_pkt_get_len(reply_pkt)),
NULL); NULL);
if (ret < 0) { if (ret < 0) {
NET_ERR("Cannot send data to peer (%d)", ret); NET_ERR("Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_buf); net_pkt_unref(reply_pkt);
quit(); quit();
} }

View file

@ -14,7 +14,7 @@
NET_BUF_POOL_DEFINE(http_pool, HTTP_POOL_BUF_CTR, HTTP_POOL_BUF_SIZE, 0, NULL); NET_BUF_POOL_DEFINE(http_pool, HTTP_POOL_BUF_CTR, HTTP_POOL_BUF_SIZE, 0, NULL);
void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx) void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_pkt *rx)
{ {
struct http_client_ctx *http_ctx; struct http_client_ctx *http_ctx;
struct net_buf *data_buf = NULL; struct net_buf *data_buf = NULL;
@ -32,9 +32,9 @@ void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx)
} }
data_len = min(net_pkt_appdatalen(rx), HTTP_POOL_BUF_SIZE); data_len = min(net_pkt_appdatalen(rx), HTTP_POOL_BUF_SIZE);
offset = net_buf_frags_len(rx) - data_len; offset = net_pkt_get_len(rx) - data_len;
rc = net_pkt_linear_copy(data_buf, rx, offset, data_len); rc = net_frag_linear_copy(data_buf, rx->frags, offset, data_len);
if (rc != 0) { if (rc != 0) {
rc = -ENOMEM; rc = -ENOMEM;
goto lb_exit; goto lb_exit;
@ -50,15 +50,15 @@ void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx)
lb_exit: lb_exit:
net_buf_unref(data_buf); net_buf_unref(data_buf);
net_buf_unref(rx); net_pkt_unref(rx);
} }
#else #else
void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx) void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_pkt *rx)
{ {
struct http_client_ctx *http_ctx; struct http_client_ctx *http_ctx;
struct net_buf *buf = rx; struct net_buf *frag = rx->frags;
uint16_t offset; uint16_t offset;
if (!rx) { if (!rx) {
@ -67,19 +67,19 @@ void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx)
http_ctx = CONTAINER_OF(tcp_ctx, struct http_client_ctx, tcp_ctx); http_ctx = CONTAINER_OF(tcp_ctx, struct http_client_ctx, tcp_ctx);
offset = net_buf_frags_len(buf) - net_pkt_appdatalen(buf); offset = net_pkt_frags_len(rx) - net_pkt_appdatalen(rx);
/* find the fragment */ /* find the fragment */
while (buf && offset >= buf->len) { while (frag && offset >= frag->len) {
offset -= buf->len; offset -= frag->len;
buf = buf->frags; frag = frag->frags;
} }
while (buf) { while (frag) {
(void)http_parser_execute(&http_ctx->parser, (void)http_parser_execute(&http_ctx->parser,
&http_ctx->settings, &http_ctx->settings,
buf->data + offset, frag->data + offset,
buf->len - offset); frag->len - offset);
/* after the first iteration, we set offset to 0 */ /* after the first iteration, we set offset to 0 */
offset = 0; offset = 0;
@ -91,11 +91,11 @@ void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx)
goto lb_exit; goto lb_exit;
} }
buf = buf->frags; frag = frag->frags;
} }
lb_exit: lb_exit:
net_buf_unref(rx); net_pkt_unref(rx);
} }
#endif #endif

View file

@ -10,6 +10,6 @@
#include "tcp_client.h" #include "tcp_client.h"
/* HTTP reception callback */ /* HTTP reception callback */
void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx); void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_pkt *rx);
#endif #endif

View file

@ -78,7 +78,7 @@ lb_exit:
} }
static static
void recv_cb(struct net_context *net_ctx, struct net_buf *rx, int status, void recv_cb(struct net_context *net_ctx, struct net_pkt *rx, int status,
void *data) void *data)
{ {
struct tcp_client_ctx *ctx = (struct tcp_client_ctx *)data; struct tcp_client_ctx *ctx = (struct tcp_client_ctx *)data;
@ -93,14 +93,14 @@ void recv_cb(struct net_context *net_ctx, struct net_buf *rx, int status,
goto lb_exit; goto lb_exit;
} }
/* receive_cb must take ownership of the rx buffer */ /* receive_cb must take ownership of the rx packet */
if (ctx->receive_cb) { if (ctx->receive_cb) {
ctx->receive_cb(ctx, rx); ctx->receive_cb(ctx, rx);
return; return;
} }
lb_exit: lb_exit:
net_buf_unref(rx); net_pkt_unref(rx);
} }
int tcp_connect(struct tcp_client_ctx *ctx, const char *server_addr, int tcp_connect(struct tcp_client_ctx *ctx, const char *server_addr,

View file

@ -18,7 +18,7 @@ struct tcp_client_ctx {
/* Network timeout */ /* Network timeout */
int32_t timeout; int32_t timeout;
/* User defined call back*/ /* User defined call back*/
void (*receive_cb)(struct tcp_client_ctx *ctx, struct net_buf *rx); void (*receive_cb)(struct tcp_client_ctx *ctx, struct net_pkt *rx);
}; };
int tcp_set_local_addr(struct tcp_client_ctx *ctx, const char *local_addr); int tcp_set_local_addr(struct tcp_client_ctx *ctx, const char *local_addr);

View file

@ -82,7 +82,7 @@ static int http_url_cmp(const char *url, uint16_t url_len,
static void http_tx(struct http_server_ctx *http_ctx); static void http_tx(struct http_server_ctx *http_ctx);
void http_rx_tx(struct net_context *net_ctx, struct net_buf *rx, int status, void http_rx_tx(struct net_context *net_ctx, struct net_pkt *rx, int status,
void *user_data) void *user_data)
{ {
struct http_server_ctx *http_ctx = NULL; struct http_server_ctx *http_ctx = NULL;
@ -128,8 +128,8 @@ void http_rx_tx(struct net_context *net_ctx, struct net_buf *rx, int status,
goto lb_exit; goto lb_exit;
} }
offset = net_buf_frags_len(rx) - rcv_len; offset = net_pkt_get_len(rx) - rcv_len;
rc = net_pkt_linear_copy(data, rx, offset, rcv_len); rc = net_frag_linear_copy(data, rx->frags, offset, rcv_len);
if (rc != 0) { if (rc != 0) {
printf("[%s:%d] Linear copy error\n", __func__, __LINE__); printf("[%s:%d] Linear copy error\n", __func__, __LINE__);
goto lb_exit; goto lb_exit;
@ -151,8 +151,8 @@ void http_rx_tx(struct net_context *net_ctx, struct net_buf *rx, int status,
} }
lb_exit: lb_exit:
net_buf_unref(data); net_pkt_frag_unref(data);
net_buf_unref(rx); net_pkt_unref(rx);
http_ctx_release(http_ctx); http_ctx_release(http_ctx);
} }

View file

@ -18,11 +18,11 @@ void http_accept_cb(struct net_context *net_ctx, struct sockaddr *addr,
* and writes an HTTP 1.1 200 OK response with client * and writes an HTTP 1.1 200 OK response with client
* header fields or an error message * header fields or an error message
* @param ctx The network context * @param ctx The network context
* @param rx The reception buffer * @param rx The received packet
* @param status Connection status, see `net_context_recv_cb_t` * @param status Connection status, see `net_context_recv_cb_t`
* @param user_data User-provided data * @param user_data User-provided data
*/ */
void http_rx_tx(struct net_context *ctx, struct net_buf *rx, int status, void http_rx_tx(struct net_context *ctx, struct net_pkt *rx, int status,
void *user_data); void *user_data);
/** /**

View file

@ -138,18 +138,18 @@ panic(const char *msg)
static int static int
transmit(struct net_context *ctx, char buffer[], size_t len) transmit(struct net_context *ctx, char buffer[], size_t len)
{ {
struct net_buf *send_buf; struct net_pkt *send_pkt;
send_buf = net_pkt_get_tx(ctx, K_FOREVER); send_pkt = net_pkt_get_tx(ctx, K_FOREVER);
if (!send_buf) { if (!send_pkt) {
return -ENOMEM; return -ENOMEM;
} }
if (!net_pkt_append(send_buf, len, buffer, K_FOREVER)) { if (!net_pkt_append(send_pkt, len, buffer, K_FOREVER)) {
return -EINVAL; return -EINVAL;
} }
return net_context_send(send_buf, NULL, K_NO_WAIT, NULL, NULL); return net_context_send(send_pkt, NULL, K_NO_WAIT, NULL, NULL);
} }
static void static void
@ -271,7 +271,7 @@ process_command(struct zirc *irc, char *cmd, size_t len)
#undef CMD #undef CMD
static void static void
on_context_recv(struct net_context *ctx, struct net_buf *buf, on_context_recv(struct net_context *ctx, struct net_pkt *pkt,
int status, void *data) int status, void *data)
{ {
struct zirc *irc = data; struct zirc *irc = data;
@ -280,7 +280,7 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf,
size_t len; size_t len;
uint16_t pos = 0, cmd_len = 0; uint16_t pos = 0, cmd_len = 0;
if (!buf) { if (!pkt) {
/* TODO: notify of disconnection, maybe reconnect? */ /* TODO: notify of disconnection, maybe reconnect? */
NET_ERR("Disconnected\n"); NET_ERR("Disconnected\n");
return; return;
@ -289,14 +289,14 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf,
if (status) { if (status) {
/* TODO: handle connection error */ /* TODO: handle connection error */
NET_ERR("Connection error: %d\n", -status); NET_ERR("Connection error: %d\n", -status);
net_pkt_unref(buf); net_pkt_unref(pkt);
return; return;
} }
/* tmp points to fragment containing IP header */ /* tmp points to fragment containing IP header */
tmp = buf->frags; tmp = pkt->frags;
/* skip pos to the first TCP payload */ /* skip pos to the first TCP payload */
pos = net_pkt_appdata(buf) - tmp->data; pos = net_pkt_appdata(pkt) - tmp->data;
while (tmp) { while (tmp) {
len = tmp->len - pos; len = tmp->len - pos;
@ -314,13 +314,13 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf,
break; break;
} }
tmp = net_pkt_read(tmp, pos, &pos, len, cmd_buf + cmd_len); tmp = net_frag_read(tmp, pos, &pos, len, cmd_buf + cmd_len);
cmd_len += len; cmd_len += len;
if (end_of_line) { if (end_of_line) {
/* skip the /n char after /r */ /* skip the /n char after /r */
if (tmp) { if (tmp) {
tmp = net_pkt_read(tmp, pos, &pos, 1, NULL); tmp = net_frag_read(tmp, pos, &pos, 1, NULL);
} }
cmd_buf[cmd_len] = '\0'; cmd_buf[cmd_len] = '\0';
@ -329,7 +329,7 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf,
} }
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
/* TODO: handle messages that spans multiple packets? */ /* TODO: handle messages that spans multiple packets? */
} }

View file

@ -95,7 +95,8 @@ static int led_get(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
const char *str; const char *str;
uint8_t *payload; uint8_t *payload;
@ -104,8 +105,8 @@ static int led_get(struct zoap_resource *resource,
id = zoap_header_get_id(request); id = zoap_header_get_id(request);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
@ -114,9 +115,9 @@ static int led_get(struct zoap_resource *resource,
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -147,10 +148,10 @@ static int led_get(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
r = net_context_sendto(buf, from, sizeof(struct sockaddr_in6), r = net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return r; return r;
@ -160,7 +161,8 @@ static int led_post(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
const char *str; const char *str;
uint8_t *payload; uint8_t *payload;
@ -176,8 +178,8 @@ static int led_post(struct zoap_resource *resource,
id = zoap_header_get_id(request); id = zoap_header_get_id(request);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
@ -186,9 +188,9 @@ static int led_post(struct zoap_resource *resource,
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -225,10 +227,10 @@ static int led_post(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
r = net_context_sendto(buf, from, sizeof(struct sockaddr_in6), r = net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return r; return r;
@ -238,7 +240,8 @@ static int led_put(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
const char *str; const char *str;
uint8_t *payload; uint8_t *payload;
@ -259,8 +262,8 @@ static int led_put(struct zoap_resource *resource,
id = zoap_header_get_id(request); id = zoap_header_get_id(request);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
@ -269,9 +272,9 @@ static int led_put(struct zoap_resource *resource,
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -304,10 +307,10 @@ static int led_put(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
r = net_context_sendto(buf, from, sizeof(struct sockaddr_in6), r = net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return r; return r;
@ -318,7 +321,8 @@ static int dummy_get(struct zoap_resource *resource,
const struct sockaddr *from) const struct sockaddr *from)
{ {
static const char dummy_str[] = "Just a test\n"; static const char dummy_str[] = "Just a test\n";
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload; uint8_t *payload;
uint16_t len, id; uint16_t len, id;
@ -326,8 +330,8 @@ static int dummy_get(struct zoap_resource *resource,
id = zoap_header_get_id(request); id = zoap_header_get_id(request);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
@ -336,9 +340,9 @@ static int dummy_get(struct zoap_resource *resource,
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -361,10 +365,10 @@ static int dummy_get(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
r = net_context_sendto(buf, from, sizeof(struct sockaddr_in6), r = net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return r; return r;
@ -402,7 +406,7 @@ static struct zoap_resource resources[] = {
}; };
static void udp_receive(struct net_context *context, static void udp_receive(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
@ -410,28 +414,28 @@ static void udp_receive(struct net_context *context,
struct sockaddr_in6 from; struct sockaddr_in6 from;
int r, header_len; int r, header_len;
net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(buf)->src); net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(pkt)->src);
from.sin6_port = NET_UDP_BUF(buf)->src_port; from.sin6_port = NET_UDP_BUF(pkt)->src_port;
from.sin6_family = AF_INET6; from.sin6_family = AF_INET6;
/* /*
* zoap expects that buffer->data starts at the * zoap expects that buffer->data starts at the
* beginning of the CoAP header * beginning of the CoAP header
*/ */
header_len = net_pkt_appdata(buf) - buf->frags->data; header_len = net_pkt_appdata(pkt) - pkt->frags->data;
net_buf_pull(buf->frags, header_len); net_buf_pull(pkt->frags, header_len);
r = zoap_packet_parse(&request, buf); r = zoap_packet_parse(&request, pkt);
if (r < 0) { if (r < 0) {
NET_ERR("Invalid data received (%d)\n", r); NET_ERR("Invalid data received (%d)\n", r);
net_pkt_unref(buf); net_pkt_unref(pkt);
return; return;
} }
r = zoap_handle_request(&request, resources, r = zoap_handle_request(&request, resources,
(const struct sockaddr *) &from); (const struct sockaddr *) &from);
net_pkt_unref(buf); net_pkt_unref(pkt);
if (r < 0) { if (r < 0) {
NET_ERR("No handler for such request (%d)\n", r); NET_ERR("No handler for such request (%d)\n", r);

View file

@ -32,42 +32,42 @@
NET_BUF_POOL_DEFINE(tcp_msg_pool, TCP_BUF_CTR, TCP_BUF_SIZE, 0, NULL); NET_BUF_POOL_DEFINE(tcp_msg_pool, TCP_BUF_CTR, TCP_BUF_SIZE, 0, NULL);
static void tcp_received(struct net_context *context, static void tcp_received(struct net_context *context,
struct net_buf *buf, int status, void *user_data) struct net_pkt *pkt, int status, void *user_data)
{ {
ARG_UNUSED(context); ARG_UNUSED(context);
struct tcp_context *ctx = user_data; struct tcp_context *ctx = user_data;
ctx->rx_pkt = buf; ctx->rx_pkt = pkt;
} }
int tcp_tx(void *context, const unsigned char *buf, size_t size) int tcp_tx(void *context, const unsigned char *buf, size_t size)
{ {
struct tcp_context *ctx = context; struct tcp_context *ctx = context;
struct net_context *tcp_ctx; struct net_context *tcp_ctx;
struct net_buf *send_buf; struct net_pkt *send_pkt;
int rc, len; int rc, len;
tcp_ctx = ctx->net_ctx; tcp_ctx = ctx->net_ctx;
send_buf = net_pkt_get_tx(tcp_ctx, K_FOREVER); send_pkt = net_pkt_get_tx(tcp_ctx, K_FOREVER);
if (!send_buf) { if (!send_pkt) {
printk("cannot create buf\n"); printk("cannot create pkt\n");
return -EIO; return -EIO;
} }
rc = net_pkt_append(send_buf, size, (uint8_t *) buf, K_FOREVER); rc = net_pkt_append(send_pkt, size, (uint8_t *) buf, K_FOREVER);
if (!rc) { if (!rc) {
printk("cannot write buf\n"); printk("cannot write buf\n");
return -EIO; return -EIO;
} }
len = net_buf_frags_len(send_buf); len = net_pkt_get_len(send_pkt);
rc = net_context_send(send_buf, NULL, ctx->timeout, NULL, NULL); rc = net_context_send(send_pkt, NULL, ctx->timeout, NULL, NULL);
if (rc < 0) { if (rc < 0) {
printk("Cannot send IPv4 data to peer (%d)\n", rc); printk("Cannot send IPv4 data to peer (%d)\n", rc);
net_pkt_unref(send_buf); net_pkt_unref(send_pkt);
return -EIO; return -EIO;
} else { } else {
return len; return len;
@ -97,13 +97,13 @@ int tcp_rx(void *context, unsigned char *buf, size_t size)
return -EIO; return -EIO;
} }
offset = net_buf_frags_len(ctx->rx_pkt) - read_bytes; offset = net_pkt_get_len(ctx->rx_pkt) - read_bytes;
rc = net_pkt_linear_copy(data, ctx->rx_pkt, offset, read_bytes); rc = net_frag_linear_copy(data, ctx->rx_pkt->frags, offset, read_bytes);
ptr = net_pkt_appdata(data); ptr = net_pkt_appdata(ctx->rx_pkt);
memcpy(buf, ptr, read_bytes); memcpy(buf, ptr, read_bytes);
net_pkt_unref(ctx->rx_pkt); net_pkt_unref(ctx->rx_pkt);
net_pkt_unref(data); net_buf_unref(data);
return read_bytes; return read_bytes;
} }

View file

@ -9,12 +9,12 @@
#include <net/net_context.h> #include <net/net_context.h>
#include <net/net_ip.h> #include <net/net_ip.h>
#include <net/buf.h> #include <net/net_pkt.h>
struct tcp_context { struct tcp_context {
struct net_context *net_ctx; struct net_context *net_ctx;
struct sockaddr local_sock; struct sockaddr local_sock;
struct net_buf *rx_pkt; struct net_pkt *rx_pkt;
int32_t timeout; int32_t timeout;
}; };

View file

@ -98,23 +98,23 @@ static bool is_sid_valid(const char *sid, size_t len)
static int transmitv(struct net_context *conn, int iovcnt, static int transmitv(struct net_context *conn, int iovcnt,
struct io_vec *iov) struct io_vec *iov)
{ {
struct net_buf *buf; struct net_pkt *pkt;
int i; int i;
buf = net_pkt_get_tx(conn, K_FOREVER); pkt = net_pkt_get_tx(conn, K_FOREVER);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
for (i = 0; i < iovcnt; i++) { for (i = 0; i < iovcnt; i++) {
if (!net_pkt_append(buf, iov[i].len, iov[i].base, K_FOREVER)) { if (!net_pkt_append(pkt, iov[i].len, iov[i].base, K_FOREVER)) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return -ENOMEM; return -ENOMEM;
} }
} }
return net_context_send(buf, NULL, K_NO_WAIT, NULL, NULL); return net_context_send(pkt, NULL, K_NO_WAIT, NULL, NULL);
} }
static inline int transmit(struct net_context *conn, const char buffer[], static inline int transmit(struct net_context *conn, const char buffer[],
@ -531,7 +531,7 @@ int nats_publish(const struct nats *nats,
}); });
} }
static void receive_cb(struct net_context *ctx, struct net_buf *buf, int status, static void receive_cb(struct net_context *ctx, struct net_pkt *pkt, int status,
void *user_data) void *user_data)
{ {
struct nats *nats = user_data; struct nats *nats = user_data;
@ -541,19 +541,19 @@ static void receive_cb(struct net_context *ctx, struct net_buf *buf, int status,
size_t len; size_t len;
uint8_t *end_of_line; uint8_t *end_of_line;
if (!buf) { if (!pkt) {
/* FIXME: How to handle disconnection? */ /* FIXME: How to handle disconnection? */
return; return;
} }
if (status) { if (status) {
/* FIXME: How to handle connectio error? */ /* FIXME: How to handle connectio error? */
net_buf_unref(buf); net_pkt_unref(pkt);
return; return;
} }
tmp = buf->frags; tmp = pkt->frags;
pos = net_pkt_appdata(buf) - tmp->data; pos = net_pkt_appdata(pkt) - tmp->data;
while (tmp) { while (tmp) {
len = tmp->len - pos; len = tmp->len - pos;
@ -567,14 +567,14 @@ static void receive_cb(struct net_context *ctx, struct net_buf *buf, int status,
break; break;
} }
tmp = net_pkt_read(tmp, pos, &pos, len, cmd_buf + cmd_len); tmp = net_frag_read(tmp, pos, &pos, len, cmd_buf + cmd_len);
cmd_len += len; cmd_len += len;
if (end_of_line) { if (end_of_line) {
int ret; int ret;
if (tmp) { if (tmp) {
tmp = net_pkt_read(tmp, pos, &pos, 1, NULL); tmp = net_frag_read(tmp, pos, &pos, 1, NULL);
} }
cmd_buf[cmd_len] = '\0'; cmd_buf[cmd_len] = '\0';
@ -589,7 +589,7 @@ static void receive_cb(struct net_context *ctx, struct net_buf *buf, int status,
} }
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
int nats_connect(struct nats *nats, struct sockaddr *addr, socklen_t addrlen) int nats_connect(struct nats *nats, struct sockaddr *addr, socklen_t addrlen)

View file

@ -63,7 +63,7 @@ static struct device *uart_dev;
/* SLIP state machine */ /* SLIP state machine */
static uint8_t slip_state = STATE_OK; static uint8_t slip_state = STATE_OK;
static struct net_buf *pkt_curr; static struct net_pkt *pkt_curr;
/* General helpers */ /* General helpers */
@ -154,9 +154,9 @@ static int slip_process_byte(unsigned char c)
net_pkt_unref(pkt_curr); net_pkt_unref(pkt_curr);
return 0; return 0;
} }
net_buf_frag_insert(pkt_curr, buf); net_pkt_frag_insert(pkt_curr, buf);
} else { } else {
buf = net_buf_frag_last(pkt_curr); buf = net_buf_frag_last(pkt_curr->frags);
} }
if (!net_buf_tailroom(buf)) { if (!net_buf_tailroom(buf)) {
@ -207,7 +207,7 @@ static void interrupt_handler(struct device *dev)
SYS_LOG_DBG("Full packet %p", pkt_curr); SYS_LOG_DBG("Full packet %p", pkt_curr);
net_buf_put(&rx_queue, pkt_curr); k_fifo_put(&rx_queue, pkt_curr);
pkt_curr = NULL; pkt_curr = NULL;
} }
} }
@ -218,11 +218,12 @@ static void interrupt_handler(struct device *dev)
/* Allocate and send data to USB Host */ /* Allocate and send data to USB Host */
static void send_data(uint8_t *cfg, uint8_t *data, size_t len) static void send_data(uint8_t *cfg, uint8_t *data, size_t len)
{ {
struct net_buf *buf, *pkt; struct net_pkt *pkt;
struct net_buf *buf;
pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT); pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT);
if (!pkt) { if (!pkt) {
SYS_LOG_DBG("No buf available"); SYS_LOG_DBG("No pkt available");
return; return;
} }
@ -233,7 +234,7 @@ static void send_data(uint8_t *cfg, uint8_t *data, size_t len)
return; return;
} }
net_buf_frag_insert(pkt, buf); net_pkt_frag_insert(pkt, buf);
SYS_LOG_DBG("queue pkt %p buf %p len %u", pkt, buf, len); SYS_LOG_DBG("queue pkt %p buf %p len %u", pkt, buf, len);
@ -247,7 +248,7 @@ static void send_data(uint8_t *cfg, uint8_t *data, size_t len)
/* simulate FCS */ /* simulate FCS */
net_buf_add(buf, 2); net_buf_add(buf, 2);
net_buf_put(&tx_queue, pkt); k_fifo_put(&tx_queue, pkt);
} }
static void get_ieee_addr(void) static void get_ieee_addr(void)
@ -290,9 +291,9 @@ static void send_pkt_report(uint8_t seq, uint8_t status, uint8_t num_tx)
send_data(cfg, report, sizeof(report)); send_data(cfg, report, sizeof(report));
} }
static void process_data(struct net_buf *pkt) static void process_data(struct net_pkt *pkt)
{ {
struct net_buf *buf = net_buf_frag_last(pkt); struct net_buf *buf = net_buf_frag_last(pkt->frags);
uint8_t seq, num_attr; uint8_t seq, num_attr;
int ret, i; int ret, i;
@ -333,9 +334,9 @@ static void set_channel(uint8_t chan)
radio_api->set_channel(ieee802154_dev, chan); radio_api->set_channel(ieee802154_dev, chan);
} }
static void process_config(struct net_buf *pkt) static void process_config(struct net_pkt *pkt)
{ {
struct net_buf *buf = net_buf_frag_last(pkt); struct net_buf *buf = net_buf_frag_last(pkt->frags);
uint8_t cmd = net_buf_pull_u8(buf); uint8_t cmd = net_buf_pull_u8(buf);
SYS_LOG_DBG("Process config %c", cmd); SYS_LOG_DBG("Process config %c", cmd);
@ -357,11 +358,12 @@ static void rx_thread(void)
SYS_LOG_INF("RX thread started"); SYS_LOG_INF("RX thread started");
while (1) { while (1) {
struct net_buf *pkt, *buf; struct net_pkt *pkt;
struct net_buf *buf;
uint8_t specifier; uint8_t specifier;
pkt = net_buf_get(&rx_queue, K_FOREVER); pkt = k_fifo_get(&rx_queue, K_FOREVER);
buf = net_buf_frag_last(pkt); buf = net_buf_frag_last(pkt->frags);
SYS_LOG_DBG("Got pkt %p buf %p", pkt, buf); SYS_LOG_DBG("Got pkt %p buf %p", pkt, buf);
@ -431,14 +433,15 @@ static void tx_thread(void)
k_sem_give(&tx_sem); k_sem_give(&tx_sem);
while (1) { while (1) {
struct net_buf *pkt, *buf; struct net_pkt *pkt;
struct net_buf *buf;
size_t len; size_t len;
k_sem_take(&tx_sem, K_FOREVER); k_sem_take(&tx_sem, K_FOREVER);
pkt = net_buf_get(&tx_queue, K_FOREVER); pkt = k_fifo_get(&tx_queue, K_FOREVER);
buf = net_buf_frag_last(pkt); buf = net_buf_frag_last(pkt->frags);
len = net_buf_frags_len(pkt); len = net_pkt_get_len(pkt);
SYS_LOG_DBG("Send pkt %p buf %p len %d", pkt, buf, len); SYS_LOG_DBG("Send pkt %p buf %p len %d", pkt, buf, len);
@ -616,18 +619,18 @@ void ieee802154_init(struct net_if *iface)
SYS_LOG_DBG(""); SYS_LOG_DBG("");
} }
int net_recv_data(struct net_if *iface, struct net_buf *pkt) int net_recv_data(struct net_if *iface, struct net_pkt *pkt)
{ {
SYS_LOG_DBG("Got data, buf %p, len %d frags->len %d", SYS_LOG_DBG("Got data, pkt %p, frags->len %d",
pkt, pkt->len, net_buf_frags_len(pkt)); pkt, net_pkt_get_len(pkt));
net_buf_put(&tx_queue, pkt); k_fifo_put(&tx_queue, pkt);
return 0; return 0;
} }
extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface, extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
SYS_LOG_DBG(""); SYS_LOG_DBG("");
@ -635,7 +638,7 @@ extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface,
return NET_CONTINUE; return NET_CONTINUE;
} }
int ieee802154_radio_send(struct net_if *iface, struct net_buf *buf) int ieee802154_radio_send(struct net_if *iface, struct net_pkt *pkt)
{ {
SYS_LOG_DBG(""); SYS_LOG_DBG("");

View file

@ -70,7 +70,7 @@ static struct device *wpanusb_dev;
static struct ieee802154_radio_api *radio_api; static struct ieee802154_radio_api *radio_api;
static struct device *ieee802154_dev; static struct device *ieee802154_dev;
static struct nano_fifo tx_queue; static struct k_fifo tx_queue;
/** /**
* Stack for the tx thread. * Stack for the tx thread.
@ -349,9 +349,9 @@ static int stop(void)
return radio_api->stop(ieee802154_dev); return radio_api->stop(ieee802154_dev);
} }
static int tx(struct net_buf *pkt) static int tx(struct net_pkt *pkt)
{ {
struct net_buf *buf = net_buf_frag_last(pkt); struct net_buf *buf = net_buf_frag_last(pkt->frags);
uint8_t seq = net_buf_pull_u8(buf); uint8_t seq = net_buf_pull_u8(buf);
int retries = 3; int retries = 3;
int ret; int ret;
@ -380,7 +380,8 @@ static int tx(struct net_buf *pkt)
static int wpanusb_vendor_handler(struct usb_setup_packet *setup, static int wpanusb_vendor_handler(struct usb_setup_packet *setup,
int32_t *len, uint8_t **data) int32_t *len, uint8_t **data)
{ {
struct net_buf *pkt, *buf; struct net_pkt *pkt;
struct net_buf *buf;
pkt = net_pkt_get_reserve_tx(0, K_NO_WAIT); pkt = net_pkt_get_reserve_tx(0, K_NO_WAIT);
if (!pkt) { if (!pkt) {
@ -393,7 +394,7 @@ static int wpanusb_vendor_handler(struct usb_setup_packet *setup,
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_insert(pkt, buf); net_pkt_frag_insert(pkt, buf);
net_buf_add_u8(buf, setup->bRequest); net_buf_add_u8(buf, setup->bRequest);
@ -406,7 +407,7 @@ static int wpanusb_vendor_handler(struct usb_setup_packet *setup,
SYS_LOG_DBG("len %u seq %u", *len, setup->wIndex); SYS_LOG_DBG("len %u seq %u", *len, setup->wIndex);
net_buf_put(&tx_queue, pkt); k_fifo_put(&tx_queue, pkt);
return 0; return 0;
} }
@ -417,10 +418,11 @@ static void tx_thread(void)
while (1) { while (1) {
uint8_t cmd; uint8_t cmd;
struct net_buf *pkt, *buf; struct net_pkt *pkt;
struct net_buf *buf;
pkt = net_buf_get(&tx_queue, K_FOREVER); pkt = k_fifo_get(&tx_queue, K_FOREVER);
buf = net_buf_frag_last(pkt); buf = net_buf_frag_last(pkt->frags);
cmd = net_buf_pull_u8(buf); cmd = net_buf_pull_u8(buf);
hexdump(">", buf->data, buf->len); hexdump(">", buf->data, buf->len);
@ -533,13 +535,13 @@ static void init_tx_queue(void)
} }
extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface, extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
/* parse on higher layer */ /* parse on higher layer */
return NET_CONTINUE; return NET_CONTINUE;
} }
int ieee802154_radio_send(struct net_if *iface, struct net_buf *buf) int ieee802154_radio_send(struct net_if *iface, struct net_pkt *pkt)
{ {
SYS_LOG_DBG(""); SYS_LOG_DBG("");
@ -551,25 +553,25 @@ void ieee802154_init(struct net_if *iface)
SYS_LOG_DBG(""); SYS_LOG_DBG("");
} }
int net_recv_data(struct net_if *iface, struct net_buf *buf) int net_recv_data(struct net_if *iface, struct net_pkt *pkt)
{ {
struct net_buf *frag; struct net_buf *frag;
SYS_LOG_DBG("Got data, buf %p, len %d frags->len %d", SYS_LOG_DBG("Got data, pkt %p, frags->len %d",
buf, buf->len, net_buf_frags_len(buf)); pkt, net_pkt_get_len(pkt));
frag = net_buf_frag_last(buf); frag = net_buf_frag_last(pkt->frags);
/** /**
* Add length 1 byte, do not forget to reserve it * Add length 1 byte, do not forget to reserve it
*/ */
net_buf_push_u8(frag, net_buf_frags_len(buf) - 1); net_buf_push_u8(frag, net_pkt_get_len(pkt) - 1);
hexdump("<", frag->data, net_buf_frags_len(buf)); hexdump("<", frag->data, net_pkt_get_len(pkt));
try_write(WPANUSB_ENDP_BULK_IN, frag->data, net_buf_frags_len(buf)); try_write(WPANUSB_ENDP_BULK_IN, frag->data, net_pkt_get_len(pkt));
net_pkt_unref(buf); net_pkt_unref(pkt);
return 0; return 0;
} }

View file

@ -60,15 +60,15 @@ static int resource_reply_cb(const struct zoap_packet *response,
struct zoap_reply *reply, struct zoap_reply *reply,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf = response->buf; struct net_pkt *pkt = response->pkt;
msg_dump("reply", buf->data, buf->len); msg_dump("reply", pkt->frags->data, pkt->frags->len);
return 0; return 0;
} }
static void udp_receive(struct net_context *context, static void udp_receive(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
@ -82,10 +82,10 @@ static void udp_receive(struct net_context *context,
* zoap expects that buffer->data starts at the * zoap expects that buffer->data starts at the
* beginning of the CoAP header * beginning of the CoAP header
*/ */
header_len = net_pkt_appdata(buf) - buf->frags->data; header_len = net_pkt_appdata(pkt) - pkt->frags->data;
net_buf_pull(buf->frags, header_len); net_buf_pull(pkt->frags, header_len);
r = zoap_packet_parse(&response, buf); r = zoap_packet_parse(&response, pkt);
if (r < 0) { if (r < 0) {
printk("Invalid data received (%d)\n", r); printk("Invalid data received (%d)\n", r);
return; return;
@ -97,8 +97,8 @@ static void udp_receive(struct net_context *context,
/* If necessary cancel retransmissions */ /* If necessary cancel retransmissions */
} }
net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(buf)->src); net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(pkt)->src);
from.sin6_port = NET_UDP_BUF(buf)->src_port; from.sin6_port = NET_UDP_BUF(pkt)->src_port;
reply = zoap_response_received(&response, reply = zoap_response_received(&response,
(const struct sockaddr *) &from, (const struct sockaddr *) &from,
@ -119,7 +119,7 @@ static void retransmit_request(struct k_work *work)
return; return;
} }
r = net_context_sendto(pending->buf, (struct sockaddr *) &mcast_addr, r = net_context_sendto(pending->pkt, (struct sockaddr *) &mcast_addr,
sizeof(mcast_addr), NULL, 0, NULL, NULL); sizeof(mcast_addr), NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
return; return;
@ -142,7 +142,8 @@ static void event_iface_up(struct net_mgmt_event_callback *cb,
struct zoap_pending *pending; struct zoap_pending *pending;
struct zoap_reply *reply; struct zoap_reply *reply;
const char * const *p; const char * const *p;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
int r; int r;
uint8_t observe = 0; uint8_t observe = 0;
@ -167,9 +168,9 @@ static void event_iface_up(struct net_mgmt_event_callback *cb,
k_delayed_work_init(&retransmit_work, retransmit_request); k_delayed_work_init(&retransmit_work, retransmit_request);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
printk("Unable to get TX buffer, not enough memory.\n"); printk("Unable to get TX packet, not enough memory.\n");
return; return;
} }
@ -179,9 +180,9 @@ static void event_iface_up(struct net_mgmt_event_callback *cb,
return; return;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&request, frag); r = zoap_packet_init(&request, pkt);
if (r < 0) { if (r < 0) {
return; return;
} }
@ -233,7 +234,7 @@ static void event_iface_up(struct net_mgmt_event_callback *cb,
zoap_reply_init(reply, &request); zoap_reply_init(reply, &request);
reply->reply = resource_reply_cb; reply->reply = resource_reply_cb;
r = net_context_sendto(buf, (struct sockaddr *) &mcast_addr, r = net_context_sendto(pkt, (struct sockaddr *) &mcast_addr,
sizeof(mcast_addr), sizeof(mcast_addr),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {

View file

@ -72,7 +72,8 @@ static int test_del(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t tkl, code, type; uint8_t tkl, code, type;
const uint8_t *token; const uint8_t *token;
@ -88,12 +89,12 @@ static int test_del(struct zoap_resource *resource,
NET_INFO("type: %u code %u id %u\n", type, code, id); NET_INFO("type: %u code %u id %u\n", type, code, id);
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -111,7 +112,7 @@ static int test_del(struct zoap_resource *resource,
zoap_header_set_id(&response, id); zoap_header_set_id(&response, id);
zoap_header_set_token(&response, token, tkl); zoap_header_set_token(&response, token, tkl);
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -119,7 +120,8 @@ static int test_put(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload, code, type, tkl; uint8_t *payload, code, type, tkl;
const uint8_t *token; const uint8_t *token;
@ -140,12 +142,12 @@ static int test_put(struct zoap_resource *resource,
NET_INFO("type: %u code %u id %u\n", type, code, id); NET_INFO("type: %u code %u id %u\n", type, code, id);
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -163,7 +165,7 @@ static int test_put(struct zoap_resource *resource,
zoap_header_set_id(&response, id); zoap_header_set_id(&response, id);
zoap_header_set_token(&response, token, tkl); zoap_header_set_token(&response, token, tkl);
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -176,7 +178,8 @@ static int test_post(struct zoap_resource *resource,
"location3", "location3",
NULL }; NULL };
const char * const *p; const char * const *p;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload, code, type, tkl; uint8_t *payload, code, type, tkl;
const uint8_t *token; const uint8_t *token;
@ -198,12 +201,12 @@ static int test_post(struct zoap_resource *resource,
NET_INFO("type: %u code %u id %u\n", type, code, id); NET_INFO("type: %u code %u id %u\n", type, code, id);
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -226,7 +229,7 @@ static int test_post(struct zoap_resource *resource,
*p, strlen(*p)); *p, strlen(*p));
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -238,7 +241,8 @@ static int location_query_post(struct zoap_resource *resource,
"second=2", "second=2",
NULL }; NULL };
const char * const *p; const char * const *p;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload, code, type, tkl; uint8_t *payload, code, type, tkl;
const uint8_t *token; const uint8_t *token;
@ -260,12 +264,12 @@ static int location_query_post(struct zoap_resource *resource,
NET_INFO("type: %u code %u id %u\n", type, code, id); NET_INFO("type: %u code %u id %u\n", type, code, id);
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -288,7 +292,7 @@ static int location_query_post(struct zoap_resource *resource,
*p, strlen(*p)); *p, strlen(*p));
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -296,7 +300,8 @@ static int piggyback_get(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
const uint8_t *token; const uint8_t *token;
uint8_t *payload, code, type; uint8_t *payload, code, type;
@ -313,12 +318,12 @@ static int piggyback_get(struct zoap_resource *resource,
NET_INFO("type: %u code %u id %u\n", type, code, id); NET_INFO("type: %u code %u id %u\n", type, code, id);
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -360,7 +365,7 @@ static int piggyback_get(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -369,7 +374,8 @@ static int query_get(struct zoap_resource *resource,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct zoap_option options[4]; struct zoap_option options[4];
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload, code, type, tkl; uint8_t *payload, code, type, tkl;
const uint8_t *token; const uint8_t *token;
@ -408,12 +414,12 @@ static int query_get(struct zoap_resource *resource,
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -448,7 +454,7 @@ static int query_get(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -456,7 +462,8 @@ static int separate_get(struct zoap_resource *resource,
struct zoap_packet *request, struct zoap_packet *request,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
struct zoap_pending *pending; struct zoap_pending *pending;
uint8_t *payload, code, type, tkl; uint8_t *payload, code, type, tkl;
@ -477,12 +484,12 @@ static int separate_get(struct zoap_resource *resource,
goto done; goto done;
} }
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -494,19 +501,19 @@ static int separate_get(struct zoap_resource *resource,
zoap_header_set_id(&response, id); zoap_header_set_id(&response, id);
zoap_header_set_token(&response, token, tkl); zoap_header_set_token(&response, token, tkl);
r = net_context_sendto(buf, from, sizeof(struct sockaddr_in6), r = net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
done: done:
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -564,7 +571,7 @@ done:
k_delayed_work_submit(&retransmit_work, pending->timeout); k_delayed_work_submit(&retransmit_work, pending->timeout);
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -573,7 +580,8 @@ static int large_get(struct zoap_resource *resource,
const struct sockaddr *from) const struct sockaddr *from)
{ {
static struct zoap_block_context ctx; static struct zoap_block_context ctx;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
const uint8_t *token; const uint8_t *token;
uint8_t *payload, code, type; uint8_t *payload, code, type;
@ -600,12 +608,12 @@ static int large_get(struct zoap_resource *resource,
NET_INFO("type: %u code %u id %u\n", type, code, id); NET_INFO("type: %u code %u id %u\n", type, code, id);
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -653,7 +661,7 @@ static int large_get(struct zoap_resource *resource,
memset(&ctx, 0, sizeof(ctx)); memset(&ctx, 0, sizeof(ctx));
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -662,7 +670,8 @@ static int large_update_put(struct zoap_resource *resource,
const struct sockaddr *from) const struct sockaddr *from)
{ {
static struct zoap_block_context ctx; static struct zoap_block_context ctx;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
const uint8_t *token; const uint8_t *token;
uint8_t *payload, code, type; uint8_t *payload, code, type;
@ -703,12 +712,12 @@ static int large_update_put(struct zoap_resource *resource,
/* Do something with the payload */ /* Do something with the payload */
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -732,7 +741,7 @@ static int large_update_put(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -741,7 +750,8 @@ static int large_create_post(struct zoap_resource *resource,
const struct sockaddr *from) const struct sockaddr *from)
{ {
static struct zoap_block_context ctx; static struct zoap_block_context ctx;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
const uint8_t *token; const uint8_t *token;
uint8_t *payload, code, type; uint8_t *payload, code, type;
@ -773,12 +783,12 @@ static int large_create_post(struct zoap_resource *resource,
NET_INFO("type: %u code %u id %u\n", type, code, id); NET_INFO("type: %u code %u id %u\n", type, code, id);
NET_INFO("*******\n"); NET_INFO("*******\n");
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -802,7 +812,7 @@ static int large_create_post(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -824,17 +834,18 @@ static int send_notification_packet(const struct sockaddr *addr, uint16_t age,
{ {
struct zoap_packet response; struct zoap_packet response;
struct zoap_pending *pending; struct zoap_pending *pending;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
uint8_t *payload, type = ZOAP_TYPE_CON; uint8_t *payload, type = ZOAP_TYPE_CON;
uint16_t len; uint16_t len;
int r; int r;
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -899,7 +910,7 @@ static int send_notification_packet(const struct sockaddr *addr, uint16_t age,
k_delayed_work_submit(&retransmit_work, pending->timeout); k_delayed_work_submit(&retransmit_work, pending->timeout);
} }
return net_context_sendto(buf, addr, addrlen, NULL, 0, NULL, NULL); return net_context_sendto(pkt, addr, addrlen, NULL, 0, NULL, NULL);
} }
static int obs_get(struct zoap_resource *resource, static int obs_get(struct zoap_resource *resource,
@ -957,7 +968,8 @@ static int core_get(struct zoap_resource *resource,
const struct sockaddr *from) const struct sockaddr *from)
{ {
static const char dummy_str[] = "Just a test\n"; static const char dummy_str[] = "Just a test\n";
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct zoap_packet response; struct zoap_packet response;
uint8_t *payload, tkl; uint8_t *payload, tkl;
const uint8_t *token; const uint8_t *token;
@ -967,12 +979,12 @@ static int core_get(struct zoap_resource *resource,
id = zoap_header_get_id(request); id = zoap_header_get_id(request);
token = zoap_header_get_token(request, &tkl); token = zoap_header_get_token(request, &tkl);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
} }
@ -996,7 +1008,7 @@ static int core_get(struct zoap_resource *resource,
return -EINVAL; return -EINVAL;
} }
return net_context_sendto(buf, from, sizeof(struct sockaddr_in6), return net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
} }
@ -1096,7 +1108,7 @@ static struct zoap_resource *find_resouce_by_observer(
} }
static void udp_receive(struct net_context *context, static void udp_receive(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
@ -1105,28 +1117,28 @@ static void udp_receive(struct net_context *context,
struct sockaddr_in6 from; struct sockaddr_in6 from;
int r, header_len; int r, header_len;
net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(buf)->src); net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(pkt)->src);
from.sin6_port = NET_UDP_BUF(buf)->src_port; from.sin6_port = NET_UDP_BUF(pkt)->src_port;
from.sin6_family = AF_INET6; from.sin6_family = AF_INET6;
/* /*
* zoap expects that buffer->data starts at the * zoap expects that buffer->data starts at the
* beginning of the CoAP header * beginning of the CoAP header
*/ */
header_len = net_pkt_appdata(buf) - buf->frags->data; header_len = net_pkt_appdata(pkt) - pkt->frags->data;
net_buf_pull(buf->frags, header_len); net_buf_pull(pkt->frags, header_len);
r = zoap_packet_parse(&request, buf); r = zoap_packet_parse(&request, pkt);
if (r < 0) { if (r < 0) {
NET_ERR("Invalid data received (%d)\n", r); NET_ERR("Invalid data received (%d)\n", r);
net_pkt_unref(buf); net_pkt_unref(pkt);
return; return;
} }
pending = zoap_pending_received(&request, pendings, pending = zoap_pending_received(&request, pendings,
NUM_PENDINGS); NUM_PENDINGS);
if (pending) { if (pending) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return; return;
} }
@ -1152,7 +1164,7 @@ not_found:
r = zoap_handle_request(&request, resources, r = zoap_handle_request(&request, resources,
(const struct sockaddr *) &from); (const struct sockaddr *) &from);
net_pkt_unref(buf); net_pkt_unref(pkt);
if (r < 0) { if (r < 0) {
NET_ERR("No handler for such request (%d)\n", r); NET_ERR("No handler for such request (%d)\n", r);
@ -1208,7 +1220,7 @@ static void retransmit_request(struct k_work *work)
return; return;
} }
r = net_context_sendto(pending->buf, &pending->addr, r = net_context_sendto(pending->pkt, &pending->addr,
sizeof(struct sockaddr_in6), sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {

View file

@ -15,7 +15,7 @@
static struct session sessions[SESSION_PROTO_END][SESSION_MAX]; static struct session sessions[SESSION_PROTO_END][SESSION_MAX];
/* Get session from a given packet */ /* Get session from a given packet */
struct session *get_session(struct net_buf *buf, enum session_proto proto) struct session *get_session(struct net_pkt *pkt, enum session_proto proto)
{ {
struct session *active = NULL; struct session *active = NULL;
struct session *free = NULL; struct session *free = NULL;
@ -24,8 +24,8 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto)
int i = 0; int i = 0;
uint16_t port; uint16_t port;
if (!buf) { if (!pkt) {
printk("Error! null buf detected.\n"); printk("Error! null pkt detected.\n");
return NULL; return NULL;
} }
@ -35,15 +35,15 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto)
} }
/* Get tuple of the remote connection */ /* Get tuple of the remote connection */
port = NET_UDP_BUF(buf)->src_port; port = NET_UDP_BUF(pkt)->src_port;
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
net_ipaddr_copy(&ipv6, &NET_IPV6_BUF(buf)->src); net_ipaddr_copy(&ipv6, &NET_IPV6_BUF(pkt)->src);
} else if (net_pkt_family(buf) == AF_INET) { } else if (net_pkt_family(pkt) == AF_INET) {
net_ipaddr_copy(&ipv4, &NET_IPV4_BUF(buf)->src); net_ipaddr_copy(&ipv4, &NET_IPV4_BUF(pkt)->src);
} else { } else {
printk("Error! unsupported protocol %d\n", printk("Error! unsupported protocol %d\n",
net_pkt_family(buf)); net_pkt_family(pkt));
return NULL; return NULL;
} }
@ -53,7 +53,7 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto)
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (ptr->port == port && if (ptr->port == port &&
net_pkt_family(buf) == AF_INET && net_pkt_family(pkt) == AF_INET &&
net_ipv4_addr_cmp(&ptr->ip.in_addr, &ipv4)) { net_ipv4_addr_cmp(&ptr->ip.in_addr, &ipv4)) {
/* We found an active session */ /* We found an active session */
active = ptr; active = ptr;
@ -61,7 +61,7 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto)
#endif #endif
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (ptr->port == port && if (ptr->port == port &&
net_pkt_family(buf) == AF_INET6 && net_pkt_family(pkt) == AF_INET6 &&
net_ipv6_addr_cmp(&ptr->ip.in6_addr, &ipv6)) { net_ipv6_addr_cmp(&ptr->ip.in6_addr, &ipv6)) {
/* We found an active session */ /* We found an active session */
active = ptr; active = ptr;
@ -82,12 +82,12 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto)
active->port = port; active->port = port;
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
net_ipaddr_copy(&active->ip.in6_addr, &ipv6); net_ipaddr_copy(&active->ip.in6_addr, &ipv6);
} }
#endif #endif
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
net_ipaddr_copy(&active->ip.in_addr, &ipv4); net_ipaddr_copy(&active->ip.in_addr, &ipv4);
} }
#endif #endif

View file

@ -56,7 +56,7 @@ struct session {
struct zperf_server_hdr stat; struct zperf_server_hdr stat;
}; };
struct session *get_session(struct net_buf *buf, enum session_proto proto); struct session *get_session(struct net_pkt *pkt, enum session_proto proto);
void zperf_session_init(void); void zperf_session_init(void);
void zperf_reset_session_stats(struct session *session); void zperf_reset_session_stats(struct session *session);

View file

@ -38,20 +38,20 @@ static struct sockaddr_in *in4_addr_my;
#endif #endif
static void tcp_received(struct net_context *context, static void tcp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
struct session *session; struct session *session;
uint32_t time; uint32_t time;
if (!buf) { if (!pkt) {
return; return;
} }
time = k_cycle_get_32(); time = k_cycle_get_32();
session = get_session(buf, SESSION_TCP); session = get_session(pkt, SESSION_TCP);
if (!session) { if (!session) {
printk(TAG "ERROR! cannot get a session!\n"); printk(TAG "ERROR! cannot get a session!\n");
return; return;
@ -68,11 +68,11 @@ static void tcp_received(struct net_context *context,
case STATE_ONGOING: case STATE_ONGOING:
session->counter++; session->counter++;
if (buf) { if (pkt) {
session->length += net_pkt_appdatalen(buf); session->length += net_pkt_appdatalen(pkt);
} }
if (!buf && status == 0) { /* EOF */ if (!pkt && status == 0) { /* EOF */
uint32_t rate_in_kbps; uint32_t rate_in_kbps;
uint32_t duration = HW_CYCLES_TO_USEC( uint32_t duration = HW_CYCLES_TO_USEC(
time_delta(session->start_time, time)); time_delta(session->start_time, time));
@ -107,7 +107,7 @@ static void tcp_received(struct net_context *context,
printk(TAG "Error! Unsupported case\n"); printk(TAG "Error! Unsupported case\n");
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
static void tcp_accepted(struct net_context *context, static void tcp_accepted(struct net_context *context,

View file

@ -46,7 +46,8 @@ void zperf_tcp_upload(struct net_context *ctx,
do { do {
int ret = 0; int ret = 0;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
uint32_t loop_time; uint32_t loop_time;
bool st; bool st;
@ -54,39 +55,39 @@ void zperf_tcp_upload(struct net_context *ctx,
loop_time = k_cycle_get_32(); loop_time = k_cycle_get_32();
last_loop_time = loop_time; last_loop_time = loop_time;
buf = net_pkt_get_tx(ctx, K_FOREVER); pkt = net_pkt_get_tx(ctx, K_FOREVER);
if (!buf) { if (!pkt) {
printk(TAG "ERROR! Failed to retrieve a buffer\n"); printk(TAG "ERROR! Failed to retrieve a packet\n");
break; break;
} }
frag = net_pkt_get_data(ctx, K_FOREVER); frag = net_pkt_get_data(ctx, K_FOREVER);
if (!frag) { if (!frag) {
net_pkt_unref(buf); net_pkt_unref(pkt);
printk(TAG "ERROR! Failed to retrieve a fragment\n"); printk(TAG "ERROR! Failed to retrieve a fragment\n");
break; break;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
/* Fill in the TCP payload */ /* Fill in the TCP payload */
st = net_pkt_append(buf, sizeof(sample_packet), st = net_pkt_append(pkt, sizeof(sample_packet),
sample_packet, K_FOREVER); sample_packet, K_FOREVER);
if (!st) { if (!st) {
printk(TAG "ERROR! Failed to fill packet\n"); printk(TAG "ERROR! Failed to fill packet\n");
net_pkt_unref(buf); net_pkt_unref(pkt);
nb_errors++; nb_errors++;
break; break;
} }
/* Send the packet */ /* Send the packet */
ret = net_context_send(buf, NULL, K_NO_WAIT, NULL, NULL); ret = net_context_send(pkt, NULL, K_NO_WAIT, NULL, NULL);
if (ret < 0) { if (ret < 0) {
printk(TAG "ERROR! Failed to send the buffer (%d)\n", printk(TAG "ERROR! Failed to send the packet (%d)\n",
ret); ret);
net_pkt_unref(buf); net_pkt_unref(pkt);
nb_errors++; nb_errors++;
break; break;
} else { } else {

View file

@ -36,95 +36,96 @@ static struct sockaddr_in *in4_addr_my;
#define MAX_DBG_PRINT 64 #define MAX_DBG_PRINT 64
static inline void set_dst_addr(sa_family_t family, static inline void set_dst_addr(sa_family_t family,
struct net_buf *buf, struct net_pkt *pkt,
struct sockaddr *dst_addr) struct sockaddr *dst_addr)
{ {
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (family == AF_INET6) { if (family == AF_INET6) {
net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr, net_ipaddr_copy(&net_sin6(dst_addr)->sin6_addr,
&NET_IPV6_BUF(buf)->src); &NET_IPV6_BUF(pkt)->src);
net_sin6(dst_addr)->sin6_family = AF_INET6; net_sin6(dst_addr)->sin6_family = AF_INET6;
net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(buf)->src_port; net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(pkt)->src_port;
} }
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (family == AF_INET) { if (family == AF_INET) {
net_ipaddr_copy(&net_sin(dst_addr)->sin_addr, net_ipaddr_copy(&net_sin(dst_addr)->sin_addr,
&NET_IPV4_BUF(buf)->src); &NET_IPV4_BUF(pkt)->src);
net_sin(dst_addr)->sin_family = AF_INET; net_sin(dst_addr)->sin_family = AF_INET;
net_sin(dst_addr)->sin_port = NET_UDP_BUF(buf)->src_port; net_sin(dst_addr)->sin_port = NET_UDP_BUF(pkt)->src_port;
} }
#endif /* CONFIG_NET_IPV4 */ #endif /* CONFIG_NET_IPV4 */
} }
static inline struct net_buf *build_reply_buf(struct net_context *context, static inline struct net_pkt *build_reply_pkt(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
struct zperf_udp_datagram *hdr, struct zperf_udp_datagram *hdr,
struct zperf_server_hdr *stat) struct zperf_server_hdr *stat)
{ {
struct net_buf *reply_buf, *frag; struct net_pkt *reply_pkt;
struct net_buf *frag;
printk(TAG "received %d bytes\n", net_pkt_appdatalen(buf)); printk(TAG "received %d bytes\n", net_pkt_appdatalen(pkt));
reply_buf = net_pkt_get_tx(context, K_FOREVER); reply_pkt = net_pkt_get_tx(context, K_FOREVER);
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(reply_buf, frag); net_pkt_frag_add(reply_pkt, frag);
net_pkt_append_be32(reply_buf, hdr->id); net_pkt_append_be32(reply_pkt, hdr->id);
net_pkt_append_be32(reply_buf, hdr->tv_sec); net_pkt_append_be32(reply_pkt, hdr->tv_sec);
net_pkt_append_be32(reply_buf, hdr->tv_usec); net_pkt_append_be32(reply_pkt, hdr->tv_usec);
net_pkt_append_be32(reply_buf, stat->flags); net_pkt_append_be32(reply_pkt, stat->flags);
net_pkt_append_be32(reply_buf, stat->total_len1); net_pkt_append_be32(reply_pkt, stat->total_len1);
net_pkt_append_be32(reply_buf, stat->total_len2); net_pkt_append_be32(reply_pkt, stat->total_len2);
net_pkt_append_be32(reply_buf, stat->stop_sec); net_pkt_append_be32(reply_pkt, stat->stop_sec);
net_pkt_append_be32(reply_buf, stat->stop_usec); net_pkt_append_be32(reply_pkt, stat->stop_usec);
net_pkt_append_be32(reply_buf, stat->error_cnt); net_pkt_append_be32(reply_pkt, stat->error_cnt);
net_pkt_append_be32(reply_buf, stat->outorder_cnt); net_pkt_append_be32(reply_pkt, stat->outorder_cnt);
net_pkt_append_be32(reply_buf, stat->datagrams); net_pkt_append_be32(reply_pkt, stat->datagrams);
net_pkt_append_be32(reply_buf, stat->jitter1); net_pkt_append_be32(reply_pkt, stat->jitter1);
net_pkt_append_be32(reply_buf, stat->jitter2); net_pkt_append_be32(reply_pkt, stat->jitter2);
return reply_buf; return reply_pkt;
} }
/* Send statistics to the remote client */ /* Send statistics to the remote client */
static int zperf_receiver_send_stat(struct net_context *context, static int zperf_receiver_send_stat(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
struct zperf_udp_datagram *hdr, struct zperf_udp_datagram *hdr,
struct zperf_server_hdr *stat) struct zperf_server_hdr *stat)
{ {
struct net_buf *reply_buf; struct net_pkt *reply_pkt;
struct sockaddr dst_addr; struct sockaddr dst_addr;
int ret; int ret;
set_dst_addr(net_pkt_family(buf), buf, &dst_addr); set_dst_addr(net_pkt_family(pkt), pkt, &dst_addr);
reply_buf = build_reply_buf(context, buf, hdr, stat); reply_pkt = build_reply_pkt(context, pkt, hdr, stat);
net_pkt_unref(buf); net_pkt_unref(pkt);
ret = net_context_sendto(reply_buf, &dst_addr, ret = net_context_sendto(reply_pkt, &dst_addr,
net_pkt_family(buf) == AF_INET6 ? net_pkt_family(pkt) == AF_INET6 ?
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in6) :
sizeof(struct sockaddr_in), sizeof(struct sockaddr_in),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (ret < 0) { if (ret < 0) {
printk(TAG " Cannot send data to peer (%d)", ret); printk(TAG " Cannot send data to peer (%d)", ret);
net_pkt_unref(reply_buf); net_pkt_unref(reply_pkt);
} }
return ret; return ret;
} }
static void udp_received(struct net_context *context, static void udp_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
struct net_buf *frag = buf->frags; struct net_buf *frag = pkt->frags;
struct zperf_udp_datagram hdr; struct zperf_udp_datagram hdr;
struct session *session; struct session *session;
uint16_t offset, pos; uint16_t offset, pos;
@ -132,28 +133,28 @@ static void udp_received(struct net_context *context,
uint32_t time; uint32_t time;
int32_t id; int32_t id;
if (!buf) { if (!pkt) {
return; return;
} }
if (net_pkt_appdatalen(buf) < sizeof(struct zperf_udp_datagram)) { if (net_pkt_appdatalen(pkt) < sizeof(struct zperf_udp_datagram)) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return; return;
} }
time = k_cycle_get_32(); time = k_cycle_get_32();
session = get_session(buf, SESSION_UDP); session = get_session(pkt, SESSION_UDP);
if (!session) { if (!session) {
printk(TAG "ERROR! cannot get a session!\n"); printk(TAG "ERROR! cannot get a session!\n");
return; return;
} }
offset = net_pkt_appdata(buf) - net_pkt_ip_data(buf); offset = net_pkt_appdata(pkt) - net_pkt_ip_data(pkt);
frag = net_pkt_read_be32(frag, offset, &pos, (uint32_t *)&hdr.id); frag = net_frag_read_be32(frag, offset, &pos, (uint32_t *)&hdr.id);
frag = net_pkt_read_be32(frag, pos, &pos, &hdr.tv_sec); frag = net_frag_read_be32(frag, pos, &pos, &hdr.tv_sec);
frag = net_pkt_read_be32(frag, pos, &pos, &hdr.tv_usec); frag = net_frag_read_be32(frag, pos, &pos, &hdr.tv_usec);
id = hdr.id; id = hdr.id;
@ -162,15 +163,15 @@ static void udp_received(struct net_context *context,
printk(TAG "End of session!\n"); printk(TAG "End of session!\n");
if (session->state == STATE_COMPLETED) { if (session->state == STATE_COMPLETED) {
/* Session is already completed: Resend the stat buffer /* Session is already completed: Resend the stat packet
* and continue * and continue
*/ */
if (zperf_receiver_send_stat(context, buf, &hdr, if (zperf_receiver_send_stat(context, pkt, &hdr,
&session->stat) < 0) { &session->stat) < 0) {
printk(TAG "ERROR! Failed to send the " printk(TAG "ERROR! Failed to send the "
"buffer\n"); "packet\n");
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} else { } else {
session->state = STATE_LAST_PACKET_RECEIVED; session->state = STATE_LAST_PACKET_RECEIVED;
@ -200,7 +201,7 @@ static void udp_received(struct net_context *context,
/* Update counter */ /* Update counter */
session->counter++; session->counter++;
session->length += net_pkt_appdatalen(buf); session->length += net_pkt_appdatalen(pkt);
/* Compute jitter */ /* Compute jitter */
transit_time = time_delta(HW_CYCLES_TO_USEC(time), transit_time = time_delta(HW_CYCLES_TO_USEC(time),
@ -249,12 +250,12 @@ static void udp_received(struct net_context *context,
session->stat.jitter1 = 0; session->stat.jitter1 = 0;
session->stat.jitter2 = session->jitter; session->stat.jitter2 = session->jitter;
if (zperf_receiver_send_stat(context, buf, &hdr, if (zperf_receiver_send_stat(context, pkt, &hdr,
&session->stat) < 0) { &session->stat) < 0) {
printk(TAG "ERROR! Failed to send the " printk(TAG "ERROR! Failed to send the "
"buffer\n"); "packet\n");
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
printk(TAG " duration:\t\t"); printk(TAG " duration:\t\t");
@ -277,7 +278,7 @@ static void udp_received(struct net_context *context,
printk("\n"); printk("\n");
} }
} else { } else {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} }

View file

@ -19,40 +19,40 @@
static char sample_packet[PACKET_SIZE_MAX]; static char sample_packet[PACKET_SIZE_MAX];
static inline void zperf_upload_decode_stat(struct net_buf *buf, static inline void zperf_upload_decode_stat(struct net_pkt *pkt,
struct zperf_results *results) struct zperf_results *results)
{ {
struct net_buf *frag = buf->frags; struct net_buf *frag = pkt->frags;
struct zperf_server_hdr hdr; struct zperf_server_hdr hdr;
uint16_t offset; uint16_t offset;
uint16_t pos; uint16_t pos;
offset = net_pkt_udp_data(buf) - net_pkt_ip_data(buf); offset = net_pkt_udp_data(pkt) - net_pkt_ip_data(pkt);
offset += sizeof(struct net_udp_hdr) + offset += sizeof(struct net_udp_hdr) +
sizeof(struct zperf_udp_datagram); sizeof(struct zperf_udp_datagram);
/* Decode stat */ /* Decode stat */
if (!buf) { if (!pkt) {
printk(TAG "ERROR! Failed to receive statistic\n"); printk(TAG "ERROR! Failed to receive statistic\n");
return; return;
} else if (net_pkt_appdatalen(buf) < } else if (net_pkt_appdatalen(pkt) <
(sizeof(struct zperf_server_hdr) + (sizeof(struct zperf_server_hdr) +
sizeof(struct zperf_udp_datagram))) { sizeof(struct zperf_udp_datagram))) {
printk(TAG "ERROR! Statistics too small\n"); printk(TAG "ERROR! Statistics too small\n");
return; return;
} }
frag = net_pkt_read_be32(frag, offset, &pos, (uint32_t *)&hdr.flags); frag = net_frag_read_be32(frag, offset, &pos, (uint32_t *)&hdr.flags);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.total_len1); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.total_len1);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.total_len2); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.total_len2);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.stop_sec); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.stop_sec);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.stop_usec); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.stop_usec);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.error_cnt); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.error_cnt);
frag = net_pkt_read_be32(frag, pos, &pos, frag = net_frag_read_be32(frag, pos, &pos,
(uint32_t *)&hdr.outorder_cnt); (uint32_t *)&hdr.outorder_cnt);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.datagrams); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.datagrams);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.jitter1); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.jitter1);
frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.jitter2); frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.jitter2);
results->nb_packets_rcvd = hdr.datagrams; results->nb_packets_rcvd = hdr.datagrams;
results->nb_packets_lost = hdr.error_cnt; results->nb_packets_lost = hdr.error_cnt;
@ -63,13 +63,13 @@ static inline void zperf_upload_decode_stat(struct net_buf *buf,
} }
static void stat_received(struct net_context *context, static void stat_received(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
struct net_buf **stat = user_data; struct net_pkt **stat = user_data;
*stat = buf; *stat = pkt;
} }
static inline void zperf_upload_fin(struct net_context *context, static inline void zperf_upload_fin(struct net_context *context,
@ -78,18 +78,19 @@ static inline void zperf_upload_fin(struct net_context *context,
uint32_t packet_size, uint32_t packet_size,
struct zperf_results *results) struct zperf_results *results)
{ {
struct net_buf *stat = NULL; struct net_pkt *stat = NULL;
struct zperf_udp_datagram datagram; struct zperf_udp_datagram datagram;
int loop = 2; int loop = 2;
int ret; int ret;
while (!stat && loop-- > 0) { while (!stat && loop-- > 0) {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
bool status; bool status;
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
printk(TAG "ERROR! Failed to retrieve a buffer\n"); printk(TAG "ERROR! Failed to retrieve a packet\n");
continue; continue;
} }
@ -99,7 +100,7 @@ static inline void zperf_upload_fin(struct net_context *context,
continue; continue;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
/* Fill the packet header */ /* Fill the packet header */
datagram.id = htonl(-nb_packets); datagram.id = htonl(-nb_packets);
@ -107,8 +108,8 @@ static inline void zperf_upload_fin(struct net_context *context,
datagram.tv_usec = htonl(HW_CYCLES_TO_USEC(end_time) % datagram.tv_usec = htonl(HW_CYCLES_TO_USEC(end_time) %
USEC_PER_SEC); USEC_PER_SEC);
status = net_pkt_append(buf, sizeof(datagram), status = net_pkt_append(pkt, sizeof(datagram),
(uint8_t *)&datagram, K_FOREVER); (uint8_t *)&datagram, K_FOREVER);
if (!status) { if (!status) {
printk(TAG "ERROR! Cannot append datagram data\n"); printk(TAG "ERROR! Cannot append datagram data\n");
break; break;
@ -120,19 +121,19 @@ static inline void zperf_upload_fin(struct net_context *context,
sizeof(struct zperf_udp_datagram); sizeof(struct zperf_udp_datagram);
uint16_t pos; uint16_t pos;
frag = net_pkt_write(buf, net_buf_frag_last(buf), frag = net_pkt_write(pkt, net_buf_frag_last(pkt->frags),
sizeof(struct zperf_udp_datagram), sizeof(struct zperf_udp_datagram),
&pos, size, &pos, size,
(uint8_t *)sample_packet, (uint8_t *)sample_packet,
K_FOREVER); K_FOREVER);
} }
/* Send the packet */ /* Send the packet */
ret = net_context_send(buf, NULL, K_NO_WAIT, NULL, NULL); ret = net_context_send(pkt, NULL, K_NO_WAIT, NULL, NULL);
if (ret < 0) { if (ret < 0) {
printk(TAG "ERROR! Failed to send the buffer (%d)\n", printk(TAG "ERROR! Failed to send the packet (%d)\n",
ret); ret);
net_pkt_unref(buf); net_pkt_unref(pkt);
continue; continue;
} }
@ -205,7 +206,8 @@ void zperf_udp_upload(struct net_context *context,
do { do {
struct zperf_udp_datagram datagram; struct zperf_udp_datagram datagram;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
uint32_t loop_time; uint32_t loop_time;
int32_t adjust; int32_t adjust;
bool status; bool status;
@ -232,9 +234,9 @@ void zperf_udp_upload(struct net_context *context,
last_loop_time = loop_time; last_loop_time = loop_time;
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
printk(TAG "ERROR! Failed to retrieve a buffer\n"); printk(TAG "ERROR! Failed to retrieve a packet\n");
continue; continue;
} }
@ -244,7 +246,7 @@ void zperf_udp_upload(struct net_context *context,
continue; continue;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
/* Fill the packet header */ /* Fill the packet header */
datagram.id = htonl(nb_packets); datagram.id = htonl(nb_packets);
@ -252,8 +254,8 @@ void zperf_udp_upload(struct net_context *context,
datagram.tv_usec = datagram.tv_usec =
htonl(HW_CYCLES_TO_USEC(loop_time) % USEC_PER_SEC); htonl(HW_CYCLES_TO_USEC(loop_time) % USEC_PER_SEC);
status = net_pkt_append(buf, sizeof(datagram), status = net_pkt_append(pkt, sizeof(datagram),
(uint8_t *)&datagram, K_FOREVER); (uint8_t *)&datagram, K_FOREVER);
if (!status) { if (!status) {
printk(TAG "ERROR! Cannot append datagram data\n"); printk(TAG "ERROR! Cannot append datagram data\n");
break; break;
@ -265,19 +267,19 @@ void zperf_udp_upload(struct net_context *context,
sizeof(struct zperf_udp_datagram); sizeof(struct zperf_udp_datagram);
uint16_t pos; uint16_t pos;
frag = net_pkt_write(buf, net_buf_frag_last(buf), frag = net_pkt_write(pkt, net_buf_frag_last(pkt->frags),
sizeof(struct zperf_udp_datagram), sizeof(struct zperf_udp_datagram),
&pos, size, sample_packet, &pos, size, sample_packet,
K_FOREVER); K_FOREVER);
} }
/* Send the packet */ /* Send the packet */
ret = net_context_send(buf, NULL, K_NO_WAIT, NULL, NULL); ret = net_context_send(pkt, NULL, K_NO_WAIT, NULL, NULL);
if (ret < 0) { if (ret < 0) {
printk(TAG "ERROR! Failed to send the buffer (%d)\n", printk(TAG "ERROR! Failed to send the packet (%d)\n",
ret); ret);
net_pkt_unref(buf); net_pkt_unref(pkt);
break; break;
} else { } else {
nb_packets++; nb_packets++;

View file

@ -285,7 +285,7 @@ static inline uint8_t compress_nh(struct net_ipv6_hdr *ipv6,
/* Helpers to compress Source Address */ /* Helpers to compress Source Address */
static inline uint8_t compress_sa(struct net_ipv6_hdr *ipv6, static inline uint8_t compress_sa(struct net_ipv6_hdr *ipv6,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
uint8_t offset) uint8_t offset)
{ {
@ -314,13 +314,13 @@ static inline uint8_t compress_sa(struct net_ipv6_hdr *ipv6,
memcpy(&IPHC[offset], &ipv6->src.s6_addr[14], 2); memcpy(&IPHC[offset], &ipv6->src.s6_addr[14], 2);
offset += 2; offset += 2;
} else { } else {
if (!net_pkt_ll_src(buf)) { if (!net_pkt_ll_src(pkt)) {
NET_ERR("Invalid src ll address"); NET_ERR("Invalid src ll address");
return 0; return 0;
} }
if (net_ipv6_addr_based_on_ll(&ipv6->src, if (net_ipv6_addr_based_on_ll(&ipv6->src,
net_pkt_ll_src(buf))) { net_pkt_ll_src(pkt))) {
NET_DBG("SAM_11 src address is fully elided"); NET_DBG("SAM_11 src address is fully elided");
/* Address is fully elided */ /* Address is fully elided */
@ -350,13 +350,13 @@ static inline uint8_t compress_sa(struct net_ipv6_hdr *ipv6,
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
static inline uint8_t compress_sa_ctx(struct net_ipv6_hdr *ipv6, static inline uint8_t compress_sa_ctx(struct net_ipv6_hdr *ipv6,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
uint8_t offset, uint8_t offset,
struct net_6lo_context *src) struct net_6lo_context *src)
{ {
if (!src) { if (!src) {
return compress_sa(ipv6, buf, frag, offset); return compress_sa(ipv6, pkt, frag, offset);
} }
IPHC[1] |= NET_6LO_IPHC_SAC_1; IPHC[1] |= NET_6LO_IPHC_SAC_1;
@ -370,7 +370,7 @@ static inline uint8_t compress_sa_ctx(struct net_ipv6_hdr *ipv6,
memcpy(&IPHC[offset], &ipv6->src.s6_addr[14], 2); memcpy(&IPHC[offset], &ipv6->src.s6_addr[14], 2);
offset += 2; offset += 2;
} else if (net_ipv6_addr_based_on_ll(&ipv6->src, } else if (net_ipv6_addr_based_on_ll(&ipv6->src,
net_pkt_ll_src(buf))) { net_pkt_ll_src(pkt))) {
NET_DBG("SAM_11 src address is fully elided"); NET_DBG("SAM_11 src address is fully elided");
/* Address is fully elided */ /* Address is fully elided */
@ -391,7 +391,7 @@ static inline uint8_t compress_sa_ctx(struct net_ipv6_hdr *ipv6,
/* Helpers to compress Destination Address */ /* Helpers to compress Destination Address */
static inline uint8_t compress_da_mcast(struct net_ipv6_hdr *ipv6, static inline uint8_t compress_da_mcast(struct net_ipv6_hdr *ipv6,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
uint8_t offset) uint8_t offset)
{ {
@ -442,13 +442,13 @@ static inline uint8_t compress_da_mcast(struct net_ipv6_hdr *ipv6,
} }
static inline uint8_t compress_da(struct net_ipv6_hdr *ipv6, static inline uint8_t compress_da(struct net_ipv6_hdr *ipv6,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
uint8_t offset) uint8_t offset)
{ {
/* If destination address is multicast */ /* If destination address is multicast */
if (net_is_ipv6_addr_mcast(&ipv6->dst)) { if (net_is_ipv6_addr_mcast(&ipv6->dst)) {
return compress_da_mcast(ipv6, buf, frag, offset); return compress_da_mcast(ipv6, pkt, frag, offset);
} }
/* If address is link-local prefix and padded with zeros */ /* If address is link-local prefix and padded with zeros */
@ -466,13 +466,13 @@ static inline uint8_t compress_da(struct net_ipv6_hdr *ipv6,
memcpy(&IPHC[offset], &ipv6->dst.s6_addr[14], 2); memcpy(&IPHC[offset], &ipv6->dst.s6_addr[14], 2);
offset += 2; offset += 2;
} else { } else {
if (!net_pkt_ll_dst(buf)) { if (!net_pkt_ll_dst(pkt)) {
NET_ERR("Invalid dst ll address"); NET_ERR("Invalid dst ll address");
return 0; return 0;
} }
if (net_ipv6_addr_based_on_ll(&ipv6->dst, if (net_ipv6_addr_based_on_ll(&ipv6->dst,
net_pkt_ll_dst(buf))) { net_pkt_ll_dst(pkt))) {
NET_DBG("DAM_11 dst addr fully elided"); NET_DBG("DAM_11 dst addr fully elided");
/* Address is fully elided */ /* Address is fully elided */
@ -500,13 +500,13 @@ static inline uint8_t compress_da(struct net_ipv6_hdr *ipv6,
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
static inline uint8_t compress_da_ctx(struct net_ipv6_hdr *ipv6, static inline uint8_t compress_da_ctx(struct net_ipv6_hdr *ipv6,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
uint8_t offset, uint8_t offset,
struct net_6lo_context *dst) struct net_6lo_context *dst)
{ {
if (!dst) { if (!dst) {
return compress_da(ipv6, buf, frag, offset); return compress_da(ipv6, pkt, frag, offset);
} }
IPHC[1] |= NET_6LO_IPHC_DAC_1; IPHC[1] |= NET_6LO_IPHC_DAC_1;
@ -521,7 +521,7 @@ static inline uint8_t compress_da_ctx(struct net_ipv6_hdr *ipv6,
offset += 2; offset += 2;
} else { } else {
if (net_ipv6_addr_based_on_ll(&ipv6->dst, if (net_ipv6_addr_based_on_ll(&ipv6->dst,
net_pkt_ll_dst(buf))) { net_pkt_ll_dst(pkt))) {
NET_DBG("DAM_11 dst addr fully elided"); NET_DBG("DAM_11 dst addr fully elided");
/* Address is fully elided */ /* Address is fully elided */
@ -627,19 +627,19 @@ static inline uint8_t compress_nh_udp(struct net_udp_hdr *udp,
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
static inline bool is_src_and_dst_addr_ctx_based(struct net_ipv6_hdr *ipv6, static inline bool is_src_and_dst_addr_ctx_based(struct net_ipv6_hdr *ipv6,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
struct net_6lo_context **src, struct net_6lo_context **src,
struct net_6lo_context **dst) struct net_6lo_context **dst)
{ {
/* If compress flag is unset means use only in uncompression. */ /* If compress flag is unset means use only in uncompression. */
*src = get_6lo_context_by_addr(net_pkt_iface(buf), &ipv6->src); *src = get_6lo_context_by_addr(net_pkt_iface(pkt), &ipv6->src);
if (*src && !((*src)->compress)) { if (*src && !((*src)->compress)) {
*src = NULL; *src = NULL;
} }
*dst = get_6lo_context_by_addr(net_pkt_iface(buf), &ipv6->dst); *dst = get_6lo_context_by_addr(net_pkt_iface(pkt), &ipv6->dst);
if (*dst && !((*dst)->compress)) { if (*dst && !((*dst)->compress)) {
*dst = NULL; *dst = NULL;
} }
@ -675,39 +675,39 @@ static inline bool is_src_and_dst_addr_ctx_based(struct net_ipv6_hdr *ipv6,
* | 0 | 1 | 1 | TF |NH | HLIM |CID|SAC| SAM | M |DAC| DAM | * | 0 | 1 | 1 | TF |NH | HLIM |CID|SAC| SAM | M |DAC| DAM |
* +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
*/ */
static inline bool compress_IPHC_header(struct net_buf *buf, static inline bool compress_IPHC_header(struct net_pkt *pkt,
fragment_handler_t fragment) fragment_handler_t fragment)
{ {
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
struct net_6lo_context *src = NULL; struct net_6lo_context *src = NULL;
struct net_6lo_context *dst = NULL; struct net_6lo_context *dst = NULL;
#endif #endif
struct net_ipv6_hdr *ipv6 = NET_IPV6_BUF(buf); struct net_ipv6_hdr *ipv6 = NET_IPV6_BUF(pkt);
uint8_t offset = 0; uint8_t offset = 0;
struct net_udp_hdr *udp; struct net_udp_hdr *udp;
struct net_buf *frag; struct net_buf *frag;
uint8_t compressed; uint8_t compressed;
if (buf->frags->len < NET_IPV6H_LEN) { if (pkt->frags->len < NET_IPV6H_LEN) {
NET_ERR("Invalid length %d, min %d", NET_ERR("Invalid length %d, min %d",
buf->frags->len, NET_IPV6H_LEN); pkt->frags->len, NET_IPV6H_LEN);
return false; return false;
} }
if (ipv6->nexthdr == IPPROTO_UDP && if (ipv6->nexthdr == IPPROTO_UDP &&
buf->frags->len < NET_IPV6UDPH_LEN) { pkt->frags->len < NET_IPV6UDPH_LEN) {
NET_ERR("Invalid length %d, min %d", NET_ERR("Invalid length %d, min %d",
buf->frags->len, NET_IPV6UDPH_LEN); pkt->frags->len, NET_IPV6UDPH_LEN);
return false; return false;
} }
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
IPHC[offset++] = NET_6LO_DISPATCH_IPHC; IPHC[offset++] = NET_6LO_DISPATCH_IPHC;
IPHC[offset++] = 0; IPHC[offset++] = 0;
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
if (is_src_and_dst_addr_ctx_based(ipv6, buf, frag, &src, &dst)) { if (is_src_and_dst_addr_ctx_based(ipv6, pkt, frag, &src, &dst)) {
offset++; offset++;
} }
#endif #endif
@ -723,24 +723,24 @@ static inline bool compress_IPHC_header(struct net_buf *buf,
/* Source Address Compression */ /* Source Address Compression */
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
offset = compress_sa_ctx(ipv6, buf, frag, offset, src); offset = compress_sa_ctx(ipv6, pkt, frag, offset, src);
#else #else
offset = compress_sa(ipv6, buf, frag, offset); offset = compress_sa(ipv6, pkt, frag, offset);
#endif #endif
if (!offset) { if (!offset) {
net_pkt_unref(frag); net_pkt_frag_unref(frag);
return false; return false;
} }
/* Destination Address Compression */ /* Destination Address Compression */
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
offset = compress_da_ctx(ipv6, buf, frag, offset, dst); offset = compress_da_ctx(ipv6, pkt, frag, offset, dst);
#else #else
offset = compress_da(ipv6, buf, frag, offset); offset = compress_da(ipv6, pkt, frag, offset);
#endif #endif
if (!offset) { if (!offset) {
net_pkt_unref(frag); net_pkt_frag_unref(frag);
return false; return false;
} }
@ -752,7 +752,7 @@ static inline bool compress_IPHC_header(struct net_buf *buf,
} }
/* UDP header compression */ /* UDP header compression */
udp = NET_UDP_BUF(buf); udp = NET_UDP_BUF(pkt);
IPHC[offset] = NET_6LO_NHC_UDP_BARE; IPHC[offset] = NET_6LO_NHC_UDP_BARE;
offset = compress_nh_udp(udp, frag, offset); offset = compress_nh_udp(udp, frag, offset);
@ -762,28 +762,28 @@ end:
net_buf_add(frag, offset); net_buf_add(frag, offset);
/* Copy the rest of the data to compressed fragment */ /* Copy the rest of the data to compressed fragment */
memcpy(&IPHC[offset], buf->frags->data + compressed, memcpy(&IPHC[offset], pkt->frags->data + compressed,
buf->frags->len - compressed); pkt->frags->len - compressed);
net_buf_add(frag, buf->frags->len - compressed); net_buf_add(frag, pkt->frags->len - compressed);
/* Delete uncompressed(original) header fragment */ /* Delete uncompressed(original) header fragment */
net_pkt_frag_del(buf, buf->frags); net_pkt_frag_del(pkt, NULL, pkt->frags);
/* Insert compressed header fragment */ /* Insert compressed header fragment */
net_buf_frag_insert(buf, frag); net_pkt_frag_insert(pkt, frag);
/* Compact the fragments, so that gaps will be filled */ /* Compact the fragments, so that gaps will be filled */
net_pkt_compact(buf); net_pkt_compact(pkt);
if (fragment) { if (fragment) {
return fragment(buf, compressed - offset); return fragment(pkt, compressed - offset);
} }
return true; return true;
} }
/* Helper to uncompress Traffic class and Flow label */ /* Helper to uncompress Traffic class and Flow label */
static inline uint8_t uncompress_tfl(struct net_buf *buf, static inline uint8_t uncompress_tfl(struct net_pkt *pkt,
struct net_ipv6_hdr *ipv6, struct net_ipv6_hdr *ipv6,
uint8_t offset) uint8_t offset)
{ {
@ -835,7 +835,7 @@ static inline uint8_t uncompress_tfl(struct net_buf *buf,
} }
/* Helper to uncompress Hoplimit */ /* Helper to uncompress Hoplimit */
static inline uint8_t uncompress_hoplimit(struct net_buf *buf, static inline uint8_t uncompress_hoplimit(struct net_pkt *pkt,
struct net_ipv6_hdr *ipv6, struct net_ipv6_hdr *ipv6,
uint8_t offset) uint8_t offset)
{ {
@ -858,7 +858,7 @@ static inline uint8_t uncompress_hoplimit(struct net_buf *buf,
} }
/* Helper to uncompress Source Address */ /* Helper to uncompress Source Address */
static inline uint8_t uncompress_sa(struct net_buf *buf, static inline uint8_t uncompress_sa(struct net_pkt *pkt,
struct net_ipv6_hdr *ipv6, struct net_ipv6_hdr *ipv6,
uint8_t offset) uint8_t offset)
{ {
@ -900,7 +900,7 @@ static inline uint8_t uncompress_sa(struct net_buf *buf,
case NET_6LO_IPHC_SAM_11: case NET_6LO_IPHC_SAM_11:
NET_DBG("SAM_11 generate src addr from ll"); NET_DBG("SAM_11 generate src addr from ll");
net_ipv6_addr_create_iid(&ipv6->src, net_pkt_ll_src(buf)); net_ipv6_addr_create_iid(&ipv6->src, net_pkt_ll_src(pkt));
break; break;
} }
@ -908,13 +908,13 @@ static inline uint8_t uncompress_sa(struct net_buf *buf,
} }
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
static inline uint8_t uncompress_sa_ctx(struct net_buf *buf, static inline uint8_t uncompress_sa_ctx(struct net_pkt *pkt,
struct net_ipv6_hdr *ipv6, struct net_ipv6_hdr *ipv6,
uint8_t offset, uint8_t offset,
struct net_6lo_context *ctx) struct net_6lo_context *ctx)
{ {
if (!ctx) { if (!ctx) {
return uncompress_sa(buf, ipv6, offset); return uncompress_sa(pkt, ipv6, offset);
} }
switch (CIPHC[1] & NET_6LO_IPHC_SAM_11) { switch (CIPHC[1] & NET_6LO_IPHC_SAM_11) {
@ -950,7 +950,7 @@ static inline uint8_t uncompress_sa_ctx(struct net_buf *buf,
* the encapsulating header. * the encapsulating header.
* (e.g., 802.15.4 or IPv6 source address). * (e.g., 802.15.4 or IPv6 source address).
*/ */
net_ipv6_addr_create_iid(&ipv6->src, net_pkt_ll_src(buf)); net_ipv6_addr_create_iid(&ipv6->src, net_pkt_ll_src(pkt));
/* net_ipv6_addr_create_iid will copy first 8 bytes /* net_ipv6_addr_create_iid will copy first 8 bytes
* as link local prefix. * as link local prefix.
@ -965,7 +965,7 @@ static inline uint8_t uncompress_sa_ctx(struct net_buf *buf,
#endif #endif
/* Helpers to uncompress Destination Address */ /* Helpers to uncompress Destination Address */
static inline uint8_t uncompress_da_mcast(struct net_buf *buf, static inline uint8_t uncompress_da_mcast(struct net_pkt *pkt,
struct net_ipv6_hdr *ipv6, struct net_ipv6_hdr *ipv6,
uint8_t offset) uint8_t offset)
{ {
@ -1023,12 +1023,12 @@ static inline uint8_t uncompress_da_mcast(struct net_buf *buf,
} }
/* Helper to uncompress Destination Address */ /* Helper to uncompress Destination Address */
static inline uint8_t uncompress_da(struct net_buf *buf, static inline uint8_t uncompress_da(struct net_pkt *pkt,
struct net_ipv6_hdr *ipv6, struct net_ipv6_hdr *ipv6,
uint8_t offset) uint8_t offset)
{ {
if (CIPHC[1] & NET_6LO_IPHC_M_1) { if (CIPHC[1] & NET_6LO_IPHC_M_1) {
return uncompress_da_mcast(buf, ipv6, offset); return uncompress_da_mcast(pkt, ipv6, offset);
} }
if (CIPHC[1] & NET_6LO_IPHC_DAC_1) { if (CIPHC[1] & NET_6LO_IPHC_DAC_1) {
@ -1068,7 +1068,7 @@ static inline uint8_t uncompress_da(struct net_buf *buf,
case NET_6LO_IPHC_DAM_11: case NET_6LO_IPHC_DAM_11:
NET_DBG("DAM_11 generate dst addr from ll"); NET_DBG("DAM_11 generate dst addr from ll");
net_ipv6_addr_create_iid(&ipv6->dst, net_pkt_ll_dst(buf)); net_ipv6_addr_create_iid(&ipv6->dst, net_pkt_ll_dst(pkt));
break; break;
} }
@ -1076,17 +1076,17 @@ static inline uint8_t uncompress_da(struct net_buf *buf,
} }
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
static inline uint8_t uncompress_da_ctx(struct net_buf *buf, static inline uint8_t uncompress_da_ctx(struct net_pkt *pkt,
struct net_ipv6_hdr *ipv6, struct net_ipv6_hdr *ipv6,
uint8_t offset, uint8_t offset,
struct net_6lo_context *ctx) struct net_6lo_context *ctx)
{ {
if (!ctx) { if (!ctx) {
return uncompress_da(buf, ipv6, offset); return uncompress_da(pkt, ipv6, offset);
} }
if (CIPHC[1] & NET_6LO_IPHC_M_1) { if (CIPHC[1] & NET_6LO_IPHC_M_1) {
return uncompress_da_mcast(buf, ipv6, offset); return uncompress_da_mcast(pkt, ipv6, offset);
} }
if (!(CIPHC[1] & NET_6LO_IPHC_DAC_1)) { if (!(CIPHC[1] & NET_6LO_IPHC_DAC_1)) {
@ -1130,7 +1130,7 @@ static inline uint8_t uncompress_da_ctx(struct net_buf *buf,
* the encapsulating header. * the encapsulating header.
* (e.g., 802.15.4 or IPv6 source address). * (e.g., 802.15.4 or IPv6 source address).
*/ */
net_ipv6_addr_create_iid(&ipv6->dst, net_pkt_ll_dst(buf)); net_ipv6_addr_create_iid(&ipv6->dst, net_pkt_ll_dst(pkt));
/* net_ipv6_addr_create_iid will copy first 8 bytes /* net_ipv6_addr_create_iid will copy first 8 bytes
* as link local prefix. * as link local prefix.
@ -1145,7 +1145,7 @@ static inline uint8_t uncompress_da_ctx(struct net_buf *buf,
#endif #endif
/* Helper to uncompress NH UDP */ /* Helper to uncompress NH UDP */
static inline uint8_t uncompress_nh_udp(struct net_buf *buf, static inline uint8_t uncompress_nh_udp(struct net_pkt *pkt,
struct net_udp_hdr *udp, struct net_udp_hdr *udp,
uint8_t offset) uint8_t offset)
{ {
@ -1204,7 +1204,7 @@ static inline uint8_t uncompress_nh_udp(struct net_buf *buf,
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
/* Helper function to uncompress src and dst contexts */ /* Helper function to uncompress src and dst contexts */
static inline bool uncompress_cid(struct net_buf *buf, static inline bool uncompress_cid(struct net_pkt *pkt,
struct net_6lo_context **src, struct net_6lo_context **src,
struct net_6lo_context **dst) struct net_6lo_context **dst)
{ {
@ -1214,13 +1214,13 @@ static inline bool uncompress_cid(struct net_buf *buf,
* Either src or dest address is context based or both. * Either src or dest address is context based or both.
*/ */
cid = (CIPHC[2] >> 4) & 0x0F; cid = (CIPHC[2] >> 4) & 0x0F;
*src = get_6lo_context_by_cid(net_pkt_iface(buf), cid); *src = get_6lo_context_by_cid(net_pkt_iface(pkt), cid);
if (!(*src)) { if (!(*src)) {
NET_DBG("Unknown src cid %d", cid); NET_DBG("Unknown src cid %d", cid);
} }
cid = CIPHC[2] & 0x0F; cid = CIPHC[2] & 0x0F;
*dst = get_6lo_context_by_cid(net_pkt_iface(buf), cid); *dst = get_6lo_context_by_cid(net_pkt_iface(pkt), cid);
if (!(*dst)) { if (!(*dst)) {
NET_DBG("Unknown dst cid %d", cid); NET_DBG("Unknown dst cid %d", cid);
} }
@ -1238,7 +1238,7 @@ static inline bool uncompress_cid(struct net_buf *buf,
} }
#endif #endif
static inline bool uncompress_IPHC_header(struct net_buf *buf) static inline bool uncompress_IPHC_header(struct net_pkt *pkt)
{ {
struct net_udp_hdr *udp = NULL; struct net_udp_hdr *udp = NULL;
uint8_t offset = 2; uint8_t offset = 2;
@ -1253,7 +1253,7 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf)
if (CIPHC[1] & NET_6LO_IPHC_CID_1) { if (CIPHC[1] & NET_6LO_IPHC_CID_1) {
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
if (!uncompress_cid(buf, &src, &dst)) { if (!uncompress_cid(pkt, &src, &dst)) {
return false; return false;
} }
@ -1264,7 +1264,7 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf)
#endif #endif
} }
frag = net_pkt_get_frag(buf, NET_6LO_RX_PKT_TIMEOUT); frag = net_pkt_get_frag(pkt, NET_6LO_RX_PKT_TIMEOUT);
if (!frag) { if (!frag) {
return false; return false;
} }
@ -1273,10 +1273,10 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf)
/* Version is always 6 */ /* Version is always 6 */
ipv6->vtc = 0x60; ipv6->vtc = 0x60;
net_pkt_set_ip_hdr_len(buf, NET_IPV6H_LEN); net_pkt_set_ip_hdr_len(pkt, NET_IPV6H_LEN);
/* Uncompress Traffic class and Flow label */ /* Uncompress Traffic class and Flow label */
offset = uncompress_tfl(buf, ipv6, offset); offset = uncompress_tfl(pkt, ipv6, offset);
if (!(CIPHC[0] & NET_6LO_IPHC_NH_1)) { if (!(CIPHC[0] & NET_6LO_IPHC_NH_1)) {
ipv6->nexthdr = CIPHC[offset]; ipv6->nexthdr = CIPHC[offset];
@ -1284,7 +1284,7 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf)
} }
/* Uncompress Hoplimit */ /* Uncompress Hoplimit */
offset = uncompress_hoplimit(buf, ipv6, offset); offset = uncompress_hoplimit(pkt, ipv6, offset);
/* First set to zero and copy relevant bits */ /* First set to zero and copy relevant bits */
memset(&ipv6->src.s6_addr[0], 0, 16); memset(&ipv6->src.s6_addr[0], 0, 16);
@ -1292,22 +1292,22 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf)
/* Uncompress Source Address */ /* Uncompress Source Address */
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
offset = uncompress_sa_ctx(buf, ipv6, offset, src); offset = uncompress_sa_ctx(pkt, ipv6, offset, src);
if (!offset) { if (!offset) {
goto fail; goto fail;
} }
#else #else
offset = uncompress_sa(buf, ipv6, offset); offset = uncompress_sa(pkt, ipv6, offset);
#endif #endif
/* Uncompress Destination Address */ /* Uncompress Destination Address */
#if defined(CONFIG_NET_6LO_CONTEXT) #if defined(CONFIG_NET_6LO_CONTEXT)
offset = uncompress_da_ctx(buf, ipv6, offset, dst); offset = uncompress_da_ctx(pkt, ipv6, offset, dst);
if (!offset) { if (!offset) {
goto fail; goto fail;
} }
#else #else
offset = uncompress_da(buf, ipv6, offset); offset = uncompress_da(pkt, ipv6, offset);
if (!offset) { if (!offset) {
goto fail; goto fail;
} }
@ -1333,7 +1333,7 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf)
udp = (struct net_udp_hdr *)(frag->data + NET_IPV6H_LEN); udp = (struct net_udp_hdr *)(frag->data + NET_IPV6H_LEN);
chksum = CIPHC[offset] & NET_6LO_NHC_UDP_CHKSUM_1; chksum = CIPHC[offset] & NET_6LO_NHC_UDP_CHKSUM_1;
offset = uncompress_nh_udp(buf, udp, offset); offset = uncompress_nh_udp(pkt, udp, offset);
if (!chksum) { if (!chksum) {
memcpy(&udp->chksum, &CIPHC[offset], 2); memcpy(&udp->chksum, &CIPHC[offset], 2);
@ -1345,22 +1345,22 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf)
end: end:
/* Move the data to beginning, no need for headers now */ /* Move the data to beginning, no need for headers now */
NET_DBG("Removing %u bytes of compressed hdr", offset); NET_DBG("Removing %u bytes of compressed hdr", offset);
memmove(buf->frags->data, buf->frags->data + offset, memmove(pkt->frags->data, pkt->frags->data + offset,
buf->frags->len - offset); pkt->frags->len - offset);
buf->frags->len -= offset; pkt->frags->len -= offset;
/* Copying ll part, if any */ /* Copying ll part, if any */
if (net_pkt_ll_reserve(buf)) { if (net_pkt_ll_reserve(pkt)) {
memcpy(frag->data - net_pkt_ll_reserve(buf), memcpy(frag->data - net_pkt_ll_reserve(pkt),
net_pkt_ll(buf), net_pkt_ll_reserve(buf)); net_pkt_ll(pkt), net_pkt_ll_reserve(pkt));
} }
/* Insert the fragment (this one holds uncompressed headers) */ /* Insert the fragment (this one holds uncompressed headers) */
net_buf_frag_insert(buf, frag); net_pkt_frag_insert(pkt, frag);
net_pkt_compact(buf); net_pkt_compact(pkt);
/* Set IPv6 header and UDP (if next header is) length */ /* Set IPv6 header and UDP (if next header is) length */
len = net_buf_frags_len(buf) - NET_IPV6H_LEN; len = net_pkt_get_len(pkt) - NET_IPV6H_LEN;
ipv6->len[0] = len >> 8; ipv6->len[0] = len >> 8;
ipv6->len[1] = (uint8_t)len; ipv6->len[1] = (uint8_t)len;
@ -1368,43 +1368,43 @@ end:
udp->len = htons(len); udp->len = htons(len);
if (chksum) { if (chksum) {
udp->chksum = ~net_calc_chksum_udp(buf); udp->chksum = ~net_calc_chksum_udp(pkt);
} }
} }
return true; return true;
fail: fail:
net_pkt_unref(frag); net_pkt_frag_unref(frag);
return false; return false;
} }
/* Adds IPv6 dispatch as first byte and adjust fragments */ /* Adds IPv6 dispatch as first byte and adjust fragments */
static inline bool compress_ipv6_header(struct net_buf *buf, static inline bool compress_ipv6_header(struct net_pkt *pkt,
fragment_handler_t fragment) fragment_handler_t fragment)
{ {
struct net_buf *frag; struct net_buf *frag;
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
frag->data[0] = NET_6LO_DISPATCH_IPV6; frag->data[0] = NET_6LO_DISPATCH_IPV6;
net_buf_add(frag, 1); net_buf_add(frag, 1);
net_buf_frag_insert(buf, frag); net_pkt_frag_insert(pkt, frag);
/* Compact the fragments, so that gaps will be filled */ /* Compact the fragments, so that gaps will be filled */
net_pkt_compact(buf); net_pkt_compact(pkt);
if (fragment) { if (fragment) {
return fragment(buf, -1); return fragment(pkt, -1);
} }
return true; return true;
} }
static inline bool uncompress_ipv6_header(struct net_buf *buf) static inline bool uncompress_ipv6_header(struct net_pkt *pkt)
{ {
struct net_buf *frag = buf->frags; struct net_buf *frag = pkt->frags;
/* Pull off IPv6 dispatch header and adjust data and length */ /* Pull off IPv6 dispatch header and adjust data and length */
memmove(frag->data, frag->data + 1, frag->len - 1); memmove(frag->data, frag->data + 1, frag->len - 1);
@ -1413,33 +1413,33 @@ static inline bool uncompress_ipv6_header(struct net_buf *buf)
return true; return true;
} }
bool net_6lo_compress(struct net_buf *buf, bool iphc, bool net_6lo_compress(struct net_pkt *pkt, bool iphc,
fragment_handler_t fragment) fragment_handler_t fragment)
{ {
if (iphc) { if (iphc) {
return compress_IPHC_header(buf, fragment); return compress_IPHC_header(pkt, fragment);
} else { } else {
return compress_ipv6_header(buf, fragment); return compress_ipv6_header(pkt, fragment);
} }
} }
bool net_6lo_uncompress(struct net_buf *buf) bool net_6lo_uncompress(struct net_pkt *pkt)
{ {
NET_ASSERT(buf && buf->frags); NET_ASSERT(pkt && pkt->frags);
if ((buf->frags->data[0] & NET_6LO_DISPATCH_IPHC) == if ((pkt->frags->data[0] & NET_6LO_DISPATCH_IPHC) ==
NET_6LO_DISPATCH_IPHC) { NET_6LO_DISPATCH_IPHC) {
/* Uncompress IPHC header */ /* Uncompress IPHC header */
return uncompress_IPHC_header(buf); return uncompress_IPHC_header(pkt);
} else if ((buf->frags->data[0] & NET_6LO_DISPATCH_IPV6) == } else if ((pkt->frags->data[0] & NET_6LO_DISPATCH_IPV6) ==
NET_6LO_DISPATCH_IPV6) { NET_6LO_DISPATCH_IPV6) {
/* Uncompress IPv6 header, it has only IPv6 dispatch in the /* Uncompress IPv6 header, it has only IPv6 dispatch in the
* beginning */ * beginning */
return uncompress_ipv6_header(buf); return uncompress_ipv6_header(pkt);
} }
NET_DBG("Buf is not compressed"); NET_DBG("Pkt is not compressed");
return true; return true;
} }

View file

@ -19,7 +19,7 @@
#include <net/net_pkt.h> #include <net/net_pkt.h>
#include "icmpv6.h" #include "icmpv6.h"
typedef bool (*fragment_handler_t)(struct net_buf *, int); typedef bool (*fragment_handler_t)(struct net_pkt *, int);
/** /**
* @brief Compress IPv6 packet as per RFC 6282 * @brief Compress IPv6 packet as per RFC 6282
@ -28,13 +28,13 @@ typedef bool (*fragment_handler_t)(struct net_buf *, int);
* are compressed as per RFC 6282. After header compression data * are compressed as per RFC 6282. After header compression data
* will be adjusted according to remaining space in fragments. * will be adjusted according to remaining space in fragments.
* *
* @param Pointer to network buffer * @param Pointer to network packet
* @param iphc true for IPHC compression, false for IPv6 dispatch header * @param iphc true for IPHC compression, false for IPv6 dispatch header
* @param Pointer to fragment function * @param Pointer to fragment function
* *
* @return True on success, false otherwise * @return True on success, false otherwise
*/ */
bool net_6lo_compress(struct net_buf *buf, bool iphc, bool net_6lo_compress(struct net_pkt *pkt, bool iphc,
fragment_handler_t fragment); fragment_handler_t fragment);
/** /**
@ -44,11 +44,11 @@ bool net_6lo_compress(struct net_buf *buf, bool iphc,
* are uncompressed as per RFC 6282. After header uncompression data * are uncompressed as per RFC 6282. After header uncompression data
* will be adjusted according to remaining space in fragments. * will be adjusted according to remaining space in fragments.
* *
* @param Pointer to network buffer * @param Pointer to network packet
* *
* @return True on success, false otherwise * @return True on success, false otherwise
*/ */
bool net_6lo_uncompress(struct net_buf *buf); bool net_6lo_uncompress(struct net_pkt *pkt);
/** /**
* @brief Set 6lowpan context information * @brief Set 6lowpan context information

View file

@ -85,7 +85,7 @@
#define NET_6LO_NHC_UDP_4_BIT_PORT 0xF0B #define NET_6LO_NHC_UDP_4_BIT_PORT 0xF0B
#define IPHC ((frag)->data) #define IPHC ((frag)->data)
#define CIPHC ((buf->frags)->data) #define CIPHC ((pkt->frags)->data)
#define NET_6LO_FRAG1_HDR_LEN 4 #define NET_6LO_FRAG1_HDR_LEN 4
#define NET_6LO_FRAGN_HDR_LEN 5 #define NET_6LO_FRAGN_HDR_LEN 5

View file

@ -48,7 +48,7 @@ static struct net_conn conns[CONFIG_NET_MAX_CONN];
* both TCP and UDP header have these in the same location, we can check * both TCP and UDP header have these in the same location, we can check
* them both using the UDP struct. * them both using the UDP struct.
*/ */
#define NET_CONN_BUF(buf) ((struct net_udp_hdr *)(net_pkt_udp_data(buf))) #define NET_CONN_BUF(pkt) ((struct net_udp_hdr *)(net_pkt_udp_data(pkt)))
#if defined(CONFIG_NET_CONN_CACHE) #if defined(CONFIG_NET_CONN_CACHE)
@ -210,16 +210,16 @@ static int32_t check_hash(enum net_ip_protocol proto,
static inline int32_t get_conn(enum net_ip_protocol proto, static inline int32_t get_conn(enum net_ip_protocol proto,
sa_family_t family, sa_family_t family,
struct net_buf *buf, struct net_pkt *pkt,
uint32_t *cache_value) uint32_t *cache_value)
{ {
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (family == AF_INET) { if (family == AF_INET) {
return check_hash(proto, family, return check_hash(proto, family,
&NET_IPV4_BUF(buf)->src, &NET_IPV4_BUF(pkt)->src,
&NET_IPV4_BUF(buf)->dst, &NET_IPV4_BUF(pkt)->dst,
NET_UDP_BUF(buf)->src_port, NET_UDP_BUF(pkt)->src_port,
NET_UDP_BUF(buf)->dst_port, NET_UDP_BUF(pkt)->dst_port,
cache_value); cache_value);
} }
#endif #endif
@ -227,10 +227,10 @@ static inline int32_t get_conn(enum net_ip_protocol proto,
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (family == AF_INET6) { if (family == AF_INET6) {
return check_hash(proto, family, return check_hash(proto, family,
&NET_IPV6_BUF(buf)->src, &NET_IPV6_BUF(pkt)->src,
&NET_IPV6_BUF(buf)->dst, &NET_IPV6_BUF(pkt)->dst,
NET_UDP_BUF(buf)->src_port, NET_UDP_BUF(pkt)->src_port,
NET_UDP_BUF(buf)->dst_port, NET_UDP_BUF(pkt)->dst_port,
cache_value); cache_value);
} }
#endif #endif
@ -280,11 +280,11 @@ static void cache_clear(void)
} }
static inline enum net_verdict cache_check(enum net_ip_protocol proto, static inline enum net_verdict cache_check(enum net_ip_protocol proto,
struct net_buf *buf, struct net_pkt *pkt,
uint32_t *cache_value, uint32_t *cache_value,
int32_t *pos) int32_t *pos)
{ {
*pos = get_conn(proto, net_pkt_family(buf), buf, cache_value); *pos = get_conn(proto, net_pkt_family(pkt), pkt, cache_value);
if (*pos >= 0) { if (*pos >= 0) {
if (conn_cache[*pos].idx >= 0) { if (conn_cache[*pos].idx >= 0) {
/* Connection is in the cache */ /* Connection is in the cache */
@ -292,15 +292,15 @@ static inline enum net_verdict cache_check(enum net_ip_protocol proto,
conn = &conns[conn_cache[*pos].idx]; conn = &conns[conn_cache[*pos].idx];
NET_DBG("Cache %s listener for buf %p src port %u " NET_DBG("Cache %s listener for pkt %p src port %u "
"dst port %u family %d cache[%d] 0x%x", "dst port %u family %d cache[%d] 0x%x",
net_proto2str(proto), buf, net_proto2str(proto), pkt,
ntohs(NET_CONN_BUF(buf)->src_port), ntohs(NET_CONN_BUF(pkt)->src_port),
ntohs(NET_CONN_BUF(buf)->dst_port), ntohs(NET_CONN_BUF(pkt)->dst_port),
net_pkt_family(buf), *pos, net_pkt_family(pkt), *pos,
conn_cache[*pos].value); conn_cache[*pos].value);
return conn->cb(conn, buf, conn->user_data); return conn->cb(conn, pkt, conn->user_data);
} }
} else if (*cache_value > 0) { } else if (*cache_value > 0) {
if (cache_check_neg(*cache_value)) { if (cache_check_neg(*cache_value)) {
@ -557,22 +557,22 @@ int net_conn_register(enum net_ip_protocol proto,
return -ENOENT; return -ENOENT;
} }
static bool check_addr(struct net_buf *buf, static bool check_addr(struct net_pkt *pkt,
struct sockaddr *addr, struct sockaddr *addr,
bool is_remote) bool is_remote)
{ {
if (addr->family != net_pkt_family(buf)) { if (addr->family != net_pkt_family(pkt)) {
return false; return false;
} }
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6 && addr->family == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6 && addr->family == AF_INET6) {
struct in6_addr *addr6; struct in6_addr *addr6;
if (is_remote) { if (is_remote) {
addr6 = &NET_IPV6_BUF(buf)->src; addr6 = &NET_IPV6_BUF(pkt)->src;
} else { } else {
addr6 = &NET_IPV6_BUF(buf)->dst; addr6 = &NET_IPV6_BUF(pkt)->dst;
} }
if (!net_is_ipv6_addr_unspecified( if (!net_is_ipv6_addr_unspecified(
@ -588,13 +588,13 @@ static bool check_addr(struct net_buf *buf,
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET && addr->family == AF_INET) { if (net_pkt_family(pkt) == AF_INET && addr->family == AF_INET) {
struct in_addr *addr4; struct in_addr *addr4;
if (is_remote) { if (is_remote) {
addr4 = &NET_IPV4_BUF(buf)->src; addr4 = &NET_IPV4_BUF(pkt)->src;
} else { } else {
addr4 = &NET_IPV4_BUF(buf)->dst; addr4 = &NET_IPV4_BUF(pkt)->dst;
} }
if (net_sin(addr)->sin_addr.s_addr[0]) { if (net_sin(addr)->sin_addr.s_addr[0]) {
@ -609,24 +609,24 @@ static bool check_addr(struct net_buf *buf,
return true; return true;
} }
static inline void send_icmp_error(struct net_buf *buf) static inline void send_icmp_error(struct net_pkt *pkt)
{ {
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
net_icmpv6_send_error(buf, NET_ICMPV6_DST_UNREACH, net_icmpv6_send_error(pkt, NET_ICMPV6_DST_UNREACH,
NET_ICMPV6_DST_UNREACH_NO_PORT, 0); NET_ICMPV6_DST_UNREACH_NO_PORT, 0);
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
} else { } else {
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
net_icmpv4_send_error(buf, NET_ICMPV4_DST_UNREACH, net_icmpv4_send_error(pkt, NET_ICMPV4_DST_UNREACH,
NET_ICMPV4_DST_UNREACH_NO_PORT); NET_ICMPV4_DST_UNREACH_NO_PORT);
#endif /* CONFIG_NET_IPV4 */ #endif /* CONFIG_NET_IPV4 */
} }
} }
enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf) enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_pkt *pkt)
{ {
int i, best_match = -1; int i, best_match = -1;
int16_t best_rank = -1; int16_t best_rank = -1;
@ -636,7 +636,7 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf)
uint32_t cache_value = 0; uint32_t cache_value = 0;
int32_t pos; int32_t pos;
verdict = cache_check(proto, buf, &cache_value, &pos); verdict = cache_check(proto, pkt, &cache_value, &pos);
if (verdict != NET_CONTINUE) { if (verdict != NET_CONTINUE) {
return verdict; return verdict;
} }
@ -646,16 +646,16 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf)
uint16_t chksum; uint16_t chksum;
if (proto == IPPROTO_TCP) { if (proto == IPPROTO_TCP) {
chksum = NET_TCP_BUF(buf)->chksum; chksum = NET_TCP_BUF(pkt)->chksum;
} else { } else {
chksum = NET_UDP_BUF(buf)->chksum; chksum = NET_UDP_BUF(pkt)->chksum;
} }
NET_DBG("Check %s listener for buf %p src port %u dst port %u " NET_DBG("Check %s listener for pkt %p src port %u dst port %u "
"family %d chksum 0x%04x", net_proto2str(proto), buf, "family %d chksum 0x%04x", net_proto2str(proto), pkt,
ntohs(NET_CONN_BUF(buf)->src_port), ntohs(NET_CONN_BUF(pkt)->src_port),
ntohs(NET_CONN_BUF(buf)->dst_port), ntohs(NET_CONN_BUF(pkt)->dst_port),
net_pkt_family(buf), ntohs(chksum)); net_pkt_family(pkt), ntohs(chksum));
} }
for (i = 0; i < CONFIG_NET_MAX_CONN; i++) { for (i = 0; i < CONFIG_NET_MAX_CONN; i++) {
@ -669,26 +669,26 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf)
if (net_sin(&conns[i].remote_addr)->sin_port) { if (net_sin(&conns[i].remote_addr)->sin_port) {
if (net_sin(&conns[i].remote_addr)->sin_port != if (net_sin(&conns[i].remote_addr)->sin_port !=
NET_CONN_BUF(buf)->src_port) { NET_CONN_BUF(pkt)->src_port) {
continue; continue;
} }
} }
if (net_sin(&conns[i].local_addr)->sin_port) { if (net_sin(&conns[i].local_addr)->sin_port) {
if (net_sin(&conns[i].local_addr)->sin_port != if (net_sin(&conns[i].local_addr)->sin_port !=
NET_CONN_BUF(buf)->dst_port) { NET_CONN_BUF(pkt)->dst_port) {
continue; continue;
} }
} }
if (conns[i].flags & NET_CONN_REMOTE_ADDR_SET) { if (conns[i].flags & NET_CONN_REMOTE_ADDR_SET) {
if (!check_addr(buf, &conns[i].remote_addr, true)) { if (!check_addr(pkt, &conns[i].remote_addr, true)) {
continue; continue;
} }
} }
if (conns[i].flags & NET_CONN_LOCAL_ADDR_SET) { if (conns[i].flags & NET_CONN_LOCAL_ADDR_SET) {
if (!check_addr(buf, &conns[i].local_addr, false)) { if (!check_addr(pkt, &conns[i].local_addr, false)) {
continue; continue;
} }
} }
@ -728,7 +728,7 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf)
conns[best_match].rank); conns[best_match].rank);
#endif /* CONFIG_NET_CONN_CACHE */ #endif /* CONFIG_NET_CONN_CACHE */
if (conns[best_match].cb(&conns[best_match], buf, if (conns[best_match].cb(&conns[best_match], pkt,
conns[best_match].user_data) == NET_DROP) { conns[best_match].user_data) == NET_DROP) {
goto drop; goto drop;
} }
@ -748,19 +748,19 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf)
/* If the destination address is multicast address, /* If the destination address is multicast address,
* we do not send ICMP error as that makes no sense. * we do not send ICMP error as that makes no sense.
*/ */
if (net_pkt_family(buf) == AF_INET6 && if (net_pkt_family(pkt) == AF_INET6 &&
net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) {
; ;
} else } else
#endif #endif
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET && if (net_pkt_family(pkt) == AF_INET &&
net_is_ipv4_addr_mcast(&NET_IPV4_BUF(buf)->dst)) { net_is_ipv4_addr_mcast(&NET_IPV4_BUF(pkt)->dst)) {
; ;
} else } else
#endif #endif
{ {
send_icmp_error(buf); send_icmp_error(pkt);
} }
drop: drop:

View file

@ -35,7 +35,7 @@ struct net_conn_handle;
* and port. * and port.
*/ */
typedef enum net_verdict (*net_conn_cb_t)(struct net_conn *conn, typedef enum net_verdict (*net_conn_cb_t)(struct net_conn *conn,
struct net_buf *buf, struct net_pkt *pkt,
void *user_data); void *user_data);
/** /**
@ -125,7 +125,7 @@ int net_conn_change_callback(struct net_conn_handle *handle,
/** /**
* @brief Called by net_core.c when a network packet is received. * @brief Called by net_core.c when a network packet is received.
* *
* @param buf Network buffer holding received data * @param pkt Network packet holding received data
* *
* @return NET_OK if the packet was consumed, NET_DROP if * @return NET_OK if the packet was consumed, NET_DROP if
* the packet parsing failed and the caller should handle * the packet parsing failed and the caller should handle
@ -134,10 +134,10 @@ int net_conn_change_callback(struct net_conn_handle *handle,
*/ */
#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_TCP)
enum net_verdict net_conn_input(enum net_ip_protocol proto, enum net_verdict net_conn_input(enum net_ip_protocol proto,
struct net_buf *buf); struct net_pkt *pkt);
#else #else
static inline enum net_verdict net_conn_input(enum net_ip_protocol proto, static inline enum net_verdict net_conn_input(enum net_ip_protocol proto,
struct net_buf *buf) struct net_pkt *pkt)
{ {
return NET_DROP; return NET_DROP;
} }

View file

@ -155,25 +155,25 @@ net_dhcpv4_msg_type_name(enum dhcpv4_msg_type msg_type)
} }
/* Add magic cookie to DCHPv4 messages */ /* Add magic cookie to DCHPv4 messages */
static inline bool add_cookie(struct net_buf *buf) static inline bool add_cookie(struct net_pkt *pkt)
{ {
return net_pkt_append(buf, sizeof(magic_cookie), magic_cookie, return net_pkt_append(pkt, sizeof(magic_cookie),
K_FOREVER); magic_cookie, K_FOREVER);
} }
/* Add a an option with the form OPTION LENGTH VALUE. */ /* Add a an option with the form OPTION LENGTH VALUE. */
static bool add_option_length_value(struct net_buf *buf, uint8_t option, static bool add_option_length_value(struct net_pkt *pkt, uint8_t option,
uint8_t size, const uint8_t *value) uint8_t size, const uint8_t *value)
{ {
if (!net_pkt_append_u8(buf, option)) { if (!net_pkt_append_u8(pkt, option)) {
return false; return false;
} }
if (!net_pkt_append_u8(buf, size)) { if (!net_pkt_append_u8(pkt, size)) {
return false; return false;
} }
if (!net_pkt_append(buf, size, value, K_FOREVER)) { if (!net_pkt_append(pkt, size, value, K_FOREVER)) {
return false; return false;
} }
@ -181,15 +181,15 @@ static bool add_option_length_value(struct net_buf *buf, uint8_t option,
} }
/* Add DHCPv4 message type */ /* Add DHCPv4 message type */
static bool add_msg_type(struct net_buf *buf, uint8_t type) static bool add_msg_type(struct net_pkt *pkt, uint8_t type)
{ {
return add_option_length_value(buf, DHCPV4_OPTIONS_MSG_TYPE, 1, &type); return add_option_length_value(pkt, DHCPV4_OPTIONS_MSG_TYPE, 1, &type);
} }
/* Add DHCPv4 minimum required options for server to reply. /* Add DHCPv4 minimum required options for server to reply.
* Can be added more if needed. * Can be added more if needed.
*/ */
static bool add_req_options(struct net_buf *buf) static bool add_req_options(struct net_pkt *pkt)
{ {
static const uint8_t data[5] = { DHCPV4_OPTIONS_REQ_LIST, static const uint8_t data[5] = { DHCPV4_OPTIONS_REQ_LIST,
3, /* Length */ 3, /* Length */
@ -197,34 +197,34 @@ static bool add_req_options(struct net_buf *buf)
DHCPV4_OPTIONS_ROUTER, DHCPV4_OPTIONS_ROUTER,
DHCPV4_OPTIONS_DNS_SERVER }; DHCPV4_OPTIONS_DNS_SERVER };
return net_pkt_append(buf, sizeof(data), data, K_FOREVER); return net_pkt_append(pkt, sizeof(data), data, K_FOREVER);
} }
static bool add_server_id(struct net_buf *buf, const struct in_addr *addr) static bool add_server_id(struct net_pkt *pkt, const struct in_addr *addr)
{ {
return add_option_length_value(buf, DHCPV4_OPTIONS_SERVER_ID, 4, return add_option_length_value(pkt, DHCPV4_OPTIONS_SERVER_ID, 4,
addr->s4_addr); addr->s4_addr);
} }
static bool add_req_ipaddr(struct net_buf *buf, const struct in_addr *addr) static bool add_req_ipaddr(struct net_pkt *pkt, const struct in_addr *addr)
{ {
return add_option_length_value(buf, DHCPV4_OPTIONS_REQ_IPADDR, 4, return add_option_length_value(pkt, DHCPV4_OPTIONS_REQ_IPADDR, 4,
addr->s4_addr); addr->s4_addr);
} }
/* Add DHCPv4 Options end, rest of the message can be padded wit zeros */ /* Add DHCPv4 Options end, rest of the message can be padded wit zeros */
static inline bool add_end(struct net_buf *buf) static inline bool add_end(struct net_pkt *pkt)
{ {
return net_pkt_append_u8(buf, DHCPV4_OPTIONS_END); return net_pkt_append_u8(pkt, DHCPV4_OPTIONS_END);
} }
/* File is empty ATM */ /* File is empty ATM */
static inline bool add_file(struct net_buf *buf) static inline bool add_file(struct net_pkt *pkt)
{ {
uint8_t len = SIZE_OF_FILE; uint8_t len = SIZE_OF_FILE;
while (len-- > 0) { while (len-- > 0) {
if (!net_pkt_append_u8(buf, 0)) { if (!net_pkt_append_u8(pkt, 0)) {
return false; return false;
} }
} }
@ -233,12 +233,12 @@ static inline bool add_file(struct net_buf *buf)
} }
/* SNAME is empty ATM */ /* SNAME is empty ATM */
static inline bool add_sname(struct net_buf *buf) static inline bool add_sname(struct net_pkt *pkt)
{ {
uint8_t len = SIZE_OF_SNAME; uint8_t len = SIZE_OF_SNAME;
while (len-- > 0) { while (len-- > 0) {
if (!net_pkt_append_u8(buf, 0)) { if (!net_pkt_append_u8(pkt, 0)) {
return false; return false;
} }
} }
@ -247,16 +247,16 @@ static inline bool add_sname(struct net_buf *buf)
} }
/* Setup IPv4 + UDP header */ /* Setup IPv4 + UDP header */
static void setup_header(struct net_buf *buf, const struct in_addr *server_addr) static void setup_header(struct net_pkt *pkt, const struct in_addr *server_addr)
{ {
struct net_ipv4_hdr *ipv4; struct net_ipv4_hdr *ipv4;
struct net_udp_hdr *udp; struct net_udp_hdr *udp;
uint16_t len; uint16_t len;
ipv4 = NET_IPV4_BUF(buf); ipv4 = NET_IPV4_BUF(pkt);
udp = NET_UDP_BUF(buf); udp = NET_UDP_BUF(pkt);
len = net_buf_frags_len(buf->frags); len = net_pkt_get_len(pkt);
/* Setup IPv4 header */ /* Setup IPv4 header */
memset(ipv4, 0, sizeof(struct net_ipv4_hdr)); memset(ipv4, 0, sizeof(struct net_ipv4_hdr));
@ -266,7 +266,7 @@ static void setup_header(struct net_buf *buf, const struct in_addr *server_addr)
ipv4->proto = IPPROTO_UDP; ipv4->proto = IPPROTO_UDP;
ipv4->len[0] = len >> 8; ipv4->len[0] = len >> 8;
ipv4->len[1] = (uint8_t)len; ipv4->len[1] = (uint8_t)len;
ipv4->chksum = ~net_calc_chksum_ipv4(buf); ipv4->chksum = ~net_calc_chksum_ipv4(pkt);
net_ipaddr_copy(&ipv4->dst, server_addr); net_ipaddr_copy(&ipv4->dst, server_addr);
@ -276,30 +276,30 @@ static void setup_header(struct net_buf *buf, const struct in_addr *server_addr)
udp->dst_port = htons(DHCPV4_SERVER_PORT); udp->dst_port = htons(DHCPV4_SERVER_PORT);
udp->len = htons(len); udp->len = htons(len);
udp->chksum = 0; udp->chksum = 0;
udp->chksum = ~net_calc_chksum_udp(buf); udp->chksum = ~net_calc_chksum_udp(pkt);
} }
/* Prepare initial DHCPv4 message and add options as per message type */ /* Prepare initial DHCPv4 message and add options as per message type */
static struct net_buf *prepare_message(struct net_if *iface, uint8_t type, static struct net_pkt *prepare_message(struct net_if *iface, uint8_t type,
const struct in_addr *ciaddr) const struct in_addr *ciaddr)
{ {
struct net_buf *buf; struct net_pkt *pkt;
struct net_buf *frag; struct net_buf *frag;
struct dhcp_msg *msg; struct dhcp_msg *msg;
buf = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, NULL), pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, NULL),
K_FOREVER); K_FOREVER);
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_pkt_set_family(buf, AF_INET); net_pkt_set_family(pkt, AF_INET);
net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
/* Leave room for IPv4 + UDP headers */ /* Leave room for IPv4 + UDP headers */
net_buf_add(buf->frags, NET_IPV4UDPH_LEN); net_buf_add(pkt->frags, NET_IPV4UDPH_LEN);
if (net_buf_tailroom(frag) < sizeof(struct dhcp_msg)) { if (net_buf_tailroom(frag) < sizeof(struct dhcp_msg)) {
goto fail; goto fail;
@ -328,24 +328,24 @@ static struct net_buf *prepare_message(struct net_if *iface, uint8_t type,
net_buf_add(frag, sizeof(struct dhcp_msg)); net_buf_add(frag, sizeof(struct dhcp_msg));
if (!add_sname(buf) || if (!add_sname(pkt) ||
!add_file(buf) || !add_file(pkt) ||
!add_cookie(buf) || !add_cookie(pkt) ||
!add_msg_type(buf, type)) { !add_msg_type(pkt, type)) {
goto fail; goto fail;
} }
return buf; return pkt;
fail: fail:
net_pkt_unref(buf); net_pkt_unref(pkt);
return NULL; return NULL;
} }
/* Prepare DHCPv4 Message request and send it to peer */ /* Prepare DHCPv4 Message request and send it to peer */
static void send_request(struct net_if *iface) static void send_request(struct net_if *iface)
{ {
struct net_buf *buf; struct net_pkt *pkt;
uint32_t timeout; uint32_t timeout;
const struct in_addr *server_addr = net_ipv4_broadcast_address(); const struct in_addr *server_addr = net_ipv4_broadcast_address();
const struct in_addr *ciaddr = NULL; const struct in_addr *ciaddr = NULL;
@ -388,27 +388,27 @@ static void send_request(struct net_if *iface)
break; break;
} }
buf = prepare_message(iface, DHCPV4_MSG_TYPE_REQUEST, ciaddr); pkt = prepare_message(iface, DHCPV4_MSG_TYPE_REQUEST, ciaddr);
if (!buf) { if (!pkt) {
goto fail; goto fail;
} }
if (with_server_id && !add_server_id(buf, &iface->dhcpv4.server_id)) { if (with_server_id && !add_server_id(pkt, &iface->dhcpv4.server_id)) {
goto fail; goto fail;
} }
if (with_requested_ip if (with_requested_ip
&& !add_req_ipaddr(buf, &iface->dhcpv4.requested_ip)) { && !add_req_ipaddr(pkt, &iface->dhcpv4.requested_ip)) {
goto fail; goto fail;
} }
if (!add_end(buf)) { if (!add_end(pkt)) {
goto fail; goto fail;
} }
setup_header(buf, server_addr); setup_header(pkt, server_addr);
if (net_send_data(buf) < 0) { if (net_send_data(pkt) < 0) {
goto fail; goto fail;
} }
@ -441,32 +441,32 @@ static void send_request(struct net_if *iface)
fail: fail:
NET_DBG("Message preparation failed"); NET_DBG("Message preparation failed");
if (!buf) { if (!pkt) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} }
/* Prepare DHCPv4 Discover message and broadcast it */ /* Prepare DHCPv4 Discover message and broadcast it */
static void send_discover(struct net_if *iface) static void send_discover(struct net_if *iface)
{ {
struct net_buf *buf; struct net_pkt *pkt;
uint32_t timeout; uint32_t timeout;
iface->dhcpv4.xid++; iface->dhcpv4.xid++;
buf = prepare_message(iface, DHCPV4_MSG_TYPE_DISCOVER, NULL); pkt = prepare_message(iface, DHCPV4_MSG_TYPE_DISCOVER, NULL);
if (!buf) { if (!pkt) {
goto fail; goto fail;
} }
if (!add_req_options(buf) || if (!add_req_options(pkt) ||
!add_end(buf)) { !add_end(pkt)) {
goto fail; goto fail;
} }
setup_header(buf, net_ipv4_broadcast_address()); setup_header(pkt, net_ipv4_broadcast_address());
if (net_send_data(buf) < 0) { if (net_send_data(pkt) < 0) {
goto fail; goto fail;
} }
@ -484,8 +484,8 @@ static void send_discover(struct net_if *iface)
fail: fail:
NET_DBG("Message preparation failed"); NET_DBG("Message preparation failed");
if (!buf) { if (!pkt) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} }
@ -662,18 +662,18 @@ static void dhcpv4_timeout(struct k_work *work)
/* Parse DHCPv4 options and retrieve relavant information /* Parse DHCPv4 options and retrieve relavant information
* as per RFC 2132. * as per RFC 2132.
*/ */
static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf, static enum net_verdict parse_options(struct net_if *iface,
struct net_buf *frag,
uint16_t offset, uint16_t offset,
enum dhcpv4_msg_type *msg_type) enum dhcpv4_msg_type *msg_type)
{ {
struct net_buf *frag;
uint8_t cookie[4]; uint8_t cookie[4];
uint8_t length; uint8_t length;
uint8_t type; uint8_t type;
uint16_t pos; uint16_t pos;
frag = net_pkt_read(buf, offset, &pos, sizeof(magic_cookie), frag = net_frag_read(frag, offset, &pos, sizeof(magic_cookie),
(uint8_t *)cookie); (uint8_t *)cookie);
if (!frag || memcmp(magic_cookie, cookie, sizeof(magic_cookie))) { if (!frag || memcmp(magic_cookie, cookie, sizeof(magic_cookie))) {
NET_DBG("Incorrect magic cookie"); NET_DBG("Incorrect magic cookie");
@ -681,14 +681,14 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
} }
while (frag) { while (frag) {
frag = net_pkt_read_u8(frag, pos, &pos, &type); frag = net_frag_read_u8(frag, pos, &pos, &type);
if (type == DHCPV4_OPTIONS_END) { if (type == DHCPV4_OPTIONS_END) {
NET_DBG("options_end"); NET_DBG("options_end");
return NET_OK; return NET_OK;
} }
frag = net_pkt_read_u8(frag, pos, &pos, &length); frag = net_frag_read_u8(frag, pos, &pos, &length);
if (!frag) { if (!frag) {
NET_ERR("option parsing, bad length"); NET_ERR("option parsing, bad length");
return NET_DROP; return NET_DROP;
@ -703,8 +703,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
frag = net_pkt_read(frag, pos, &pos, length, frag = net_frag_read(frag, pos, &pos, length,
netmask.s4_addr); netmask.s4_addr);
if (!frag && pos) { if (!frag && pos) {
NET_ERR("options_subnet_mask, short packet"); NET_ERR("options_subnet_mask, short packet");
return NET_DROP; return NET_DROP;
@ -728,8 +728,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
frag = net_pkt_read(frag, pos, &pos, 4, router.s4_addr); frag = net_frag_read(frag, pos, &pos, 4, router.s4_addr);
frag = net_pkt_skip(frag, pos, &pos, length - 4); frag = net_frag_skip(frag, pos, &pos, length - 4);
if (!frag && pos) { if (!frag && pos) {
NET_ERR("options_router, short packet"); NET_ERR("options_router, short packet");
return NET_DROP; return NET_DROP;
@ -746,8 +746,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
frag = net_pkt_read_be32(frag, pos, &pos, frag = net_frag_read_be32(frag, pos, &pos,
&iface->dhcpv4.lease_time); &iface->dhcpv4.lease_time);
NET_DBG("options_lease_time: %u", NET_DBG("options_lease_time: %u",
iface->dhcpv4.lease_time); iface->dhcpv4.lease_time);
if (!iface->dhcpv4.lease_time) { if (!iface->dhcpv4.lease_time) {
@ -761,8 +761,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
frag = net_pkt_read_be32(frag, pos, &pos, frag = net_frag_read_be32(frag, pos, &pos,
&iface->dhcpv4.renewal_time); &iface->dhcpv4.renewal_time);
NET_DBG("options_renewal: %u", NET_DBG("options_renewal: %u",
iface->dhcpv4.renewal_time); iface->dhcpv4.renewal_time);
if (!iface->dhcpv4.renewal_time) { if (!iface->dhcpv4.renewal_time) {
@ -776,8 +776,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
frag = net_pkt_read_be32(frag, pos, &pos, frag = net_frag_read_be32(frag, pos, &pos,
&iface->dhcpv4.rebinding_time); &iface->dhcpv4.rebinding_time);
NET_DBG("options_rebinding: %u", NET_DBG("options_rebinding: %u",
iface->dhcpv4.rebinding_time); iface->dhcpv4.rebinding_time);
if (!iface->dhcpv4.rebinding_time) { if (!iface->dhcpv4.rebinding_time) {
@ -791,8 +791,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
frag = net_pkt_read(frag, pos, &pos, length, frag = net_frag_read(frag, pos, &pos, length,
iface->dhcpv4.server_id.s4_addr); iface->dhcpv4.server_id.s4_addr);
NET_DBG("options_server_id: %s", NET_DBG("options_server_id: %s",
net_sprint_ipv4_addr(&iface->dhcpv4.server_id)); net_sprint_ipv4_addr(&iface->dhcpv4.server_id));
break; break;
@ -804,13 +804,13 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
frag = net_pkt_read_u8(frag, pos, &pos, &v); frag = net_frag_read_u8(frag, pos, &pos, &v);
*msg_type = v; *msg_type = v;
break; break;
} }
default: default:
NET_DBG("option unknown: %d", type); NET_DBG("option unknown: %d", type);
frag = net_pkt_skip(frag, pos, &pos, length); frag = net_frag_skip(frag, pos, &pos, length);
break; break;
} }
@ -918,7 +918,7 @@ static void handle_dhcpv4_reply(struct net_if *iface,
} }
static enum net_verdict net_dhcpv4_input(struct net_conn *conn, static enum net_verdict net_dhcpv4_input(struct net_conn *conn,
struct net_buf *buf, struct net_pkt *pkt,
void *user_data) void *user_data)
{ {
struct dhcp_msg *msg; struct dhcp_msg *msg;
@ -933,24 +933,24 @@ static enum net_verdict net_dhcpv4_input(struct net_conn *conn,
return NET_DROP; return NET_DROP;
} }
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
NET_DBG("Invalid buffer, no fragments"); NET_DBG("Invalid packet, no fragments");
return NET_DROP; return NET_DROP;
} }
iface = net_pkt_iface(buf); iface = net_pkt_iface(pkt);
if (!iface) { if (!iface) {
NET_DBG("no iface"); NET_DBG("no iface");
return NET_DROP; return NET_DROP;
} }
frag = buf->frags; frag = pkt->frags;
min = NET_IPV4UDPH_LEN + sizeof(struct dhcp_msg); min = NET_IPV4UDPH_LEN + sizeof(struct dhcp_msg);
/* If the message is not DHCP then continue passing to /* If the message is not DHCP then continue passing to
* related handlers. * related handlers.
*/ */
if (net_buf_frags_len(frag) < min) { if (net_pkt_get_len(pkt) < min) {
NET_DBG("Input msg is not related to DHCPv4"); NET_DBG("Input msg is not related to DHCPv4");
return NET_CONTINUE; return NET_CONTINUE;
@ -983,7 +983,7 @@ static enum net_verdict net_dhcpv4_input(struct net_conn *conn,
sizeof(msg->yiaddr)); sizeof(msg->yiaddr));
/* SNAME, FILE are not used at the moment, skip it */ /* SNAME, FILE are not used at the moment, skip it */
frag = net_pkt_skip(frag, min, &pos, SIZE_OF_SNAME + SIZE_OF_FILE); frag = net_frag_skip(frag, min, &pos, SIZE_OF_SNAME + SIZE_OF_FILE);
if (!frag && pos) { if (!frag && pos) {
NET_DBG("short packet while skipping sname"); NET_DBG("short packet while skipping sname");
goto drop; goto drop;
@ -994,7 +994,7 @@ static enum net_verdict net_dhcpv4_input(struct net_conn *conn,
goto drop; goto drop;
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
handle_dhcpv4_reply(iface, msg_type); handle_dhcpv4_reply(iface, msg_type);

View file

@ -22,13 +22,13 @@
#include "icmpv4.h" #include "icmpv4.h"
#include "net_stats.h" #include "net_stats.h"
#define BUF_WAIT_TIME K_SECONDS(1) #define PKT_WAIT_TIME K_SECONDS(1)
static sys_slist_t handlers; static sys_slist_t handlers;
static inline enum net_verdict handle_echo_request(struct net_buf *buf) static inline enum net_verdict handle_echo_request(struct net_pkt *pkt)
{ {
/* Note that we send the same data buffers back and just swap /* Note that we send the same data packets back and just swap
* the addresses etc. * the addresses etc.
*/ */
struct in_addr addr; struct in_addr addr;
@ -37,29 +37,29 @@ static inline enum net_verdict handle_echo_request(struct net_buf *buf)
char out[sizeof("xxx.xxx.xxx.xxx")]; char out[sizeof("xxx.xxx.xxx.xxx")];
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst)); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst));
NET_DBG("Received Echo Request from %s to %s", NET_DBG("Received Echo Request from %s to %s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src), out);
#endif /* CONFIG_NET_DEBUG_ICMPV4 */ #endif /* CONFIG_NET_DEBUG_ICMPV4 */
net_ipaddr_copy(&addr, &NET_IPV4_BUF(buf)->src); net_ipaddr_copy(&addr, &NET_IPV4_BUF(pkt)->src);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src,
&NET_IPV4_BUF(buf)->dst); &NET_IPV4_BUF(pkt)->dst);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, &addr); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, &addr);
NET_ICMP_BUF(buf)->type = NET_ICMPV4_ECHO_REPLY; NET_ICMP_BUF(pkt)->type = NET_ICMPV4_ECHO_REPLY;
NET_ICMP_BUF(buf)->code = 0; NET_ICMP_BUF(pkt)->code = 0;
NET_ICMP_BUF(buf)->chksum = 0; NET_ICMP_BUF(pkt)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf); NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv4(pkt);
#if defined(CONFIG_NET_DEBUG_ICMPV4) #if defined(CONFIG_NET_DEBUG_ICMPV4)
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst)); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst));
NET_DBG("Sending Echo Reply from %s to %s", NET_DBG("Sending Echo Reply from %s to %s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src), out);
#endif /* CONFIG_NET_DEBUG_ICMPV4 */ #endif /* CONFIG_NET_DEBUG_ICMPV4 */
if (net_send_data(buf) < 0) { if (net_send_data(pkt) < 0) {
net_stats_update_icmp_drop(); net_stats_update_icmp_drop();
return NET_DROP; return NET_DROP;
} }
@ -71,30 +71,30 @@ static inline enum net_verdict handle_echo_request(struct net_buf *buf)
#define NET_ICMPV4_UNUSED_LEN 4 #define NET_ICMPV4_UNUSED_LEN 4
static inline void setup_ipv4_header(struct net_buf *buf, uint8_t extra_len, static inline void setup_ipv4_header(struct net_pkt *pkt, uint8_t extra_len,
uint8_t ttl, uint8_t icmp_type, uint8_t ttl, uint8_t icmp_type,
uint8_t icmp_code) uint8_t icmp_code)
{ {
NET_IPV4_BUF(buf)->vhl = 0x45; NET_IPV4_BUF(pkt)->vhl = 0x45;
NET_IPV4_BUF(buf)->tos = 0x00; NET_IPV4_BUF(pkt)->tos = 0x00;
NET_IPV4_BUF(buf)->len[0] = 0; NET_IPV4_BUF(pkt)->len[0] = 0;
NET_IPV4_BUF(buf)->len[1] = sizeof(struct net_ipv4_hdr) + NET_IPV4_BUF(pkt)->len[1] = sizeof(struct net_ipv4_hdr) +
NET_ICMPH_LEN + extra_len + NET_ICMPV4_UNUSED_LEN; NET_ICMPH_LEN + extra_len + NET_ICMPV4_UNUSED_LEN;
NET_IPV4_BUF(buf)->proto = IPPROTO_ICMP; NET_IPV4_BUF(pkt)->proto = IPPROTO_ICMP;
NET_IPV4_BUF(buf)->ttl = ttl; NET_IPV4_BUF(pkt)->ttl = ttl;
NET_IPV4_BUF(buf)->offset[0] = NET_IPV4_BUF(buf)->offset[1] = 0; NET_IPV4_BUF(pkt)->offset[0] = NET_IPV4_BUF(pkt)->offset[1] = 0;
NET_IPV4_BUF(buf)->id[0] = NET_IPV4_BUF(buf)->id[1] = 0; NET_IPV4_BUF(pkt)->id[0] = NET_IPV4_BUF(pkt)->id[1] = 0;
net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));
NET_IPV4_BUF(buf)->chksum = 0; NET_IPV4_BUF(pkt)->chksum = 0;
NET_IPV4_BUF(buf)->chksum = ~net_calc_chksum_ipv4(buf); NET_IPV4_BUF(pkt)->chksum = ~net_calc_chksum_ipv4(pkt);
NET_ICMP_BUF(buf)->type = icmp_type; NET_ICMP_BUF(pkt)->type = icmp_type;
NET_ICMP_BUF(buf)->code = icmp_code; NET_ICMP_BUF(pkt)->code = icmp_code;
memset(net_pkt_icmp_data(buf) + sizeof(struct net_icmp_hdr), 0, memset(net_pkt_icmp_data(pkt) + sizeof(struct net_icmp_hdr), 0,
NET_ICMPV4_UNUSED_LEN); NET_ICMPV4_UNUSED_LEN);
} }
@ -104,7 +104,8 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
uint16_t sequence) uint16_t sequence)
{ {
const struct in_addr *src; const struct in_addr *src;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
/* Take the first address of the network interface */ /* Take the first address of the network interface */
src = &iface->ipv4.unicast[0].address.in_addr; src = &iface->ipv4.unicast[0].address.in_addr;
@ -113,59 +114,60 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
* as IPv4 cannot be used in 802.15.4 where it is the reserve * as IPv4 cannot be used in 802.15.4 where it is the reserve
* size can change depending on address. * size can change depending on address.
*/ */
buf = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface,
(const struct in6_addr *)dst), (const struct in6_addr *)dst),
K_FOREVER); K_FOREVER);
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
net_pkt_set_family(buf, AF_INET); net_pkt_set_family(pkt, AF_INET);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
setup_ipv4_header(buf, 0, net_if_ipv4_get_ttl(iface), setup_ipv4_header(pkt, 0, net_if_ipv4_get_ttl(iface),
NET_ICMPV4_ECHO_REQUEST, 0); NET_ICMPV4_ECHO_REQUEST, 0);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, src); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, src);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, dst); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, dst);
NET_ICMPV4_ECHO_REQ_BUF(buf)->identifier = htons(identifier); NET_ICMPV4_ECHO_REQ_BUF(pkt)->identifier = htons(identifier);
NET_ICMPV4_ECHO_REQ_BUF(buf)->sequence = htons(sequence); NET_ICMPV4_ECHO_REQ_BUF(pkt)->sequence = htons(sequence);
NET_ICMP_BUF(buf)->chksum = 0; NET_ICMP_BUF(pkt)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf); NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv4(pkt);
#if defined(CONFIG_NET_DEBUG_ICMPV4) #if defined(CONFIG_NET_DEBUG_ICMPV4)
do { do {
char out[NET_IPV4_ADDR_LEN]; char out[NET_IPV4_ADDR_LEN];
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst)); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst));
NET_DBG("Sending ICMPv4 Echo Request type %d" NET_DBG("Sending ICMPv4 Echo Request type %d"
" from %s to %s", NET_ICMPV4_ECHO_REQUEST, " from %s to %s", NET_ICMPV4_ECHO_REQUEST,
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src), out);
} while (0); } while (0);
#endif /* CONFIG_NET_DEBUG_ICMPV4 */ #endif /* CONFIG_NET_DEBUG_ICMPV4 */
net_buf_add(buf->frags, sizeof(struct net_ipv4_hdr) + net_buf_add(pkt->frags, sizeof(struct net_ipv4_hdr) +
sizeof(struct net_icmp_hdr) + sizeof(struct net_icmp_hdr) +
sizeof(struct net_icmpv4_echo_req)); sizeof(struct net_icmpv4_echo_req));
if (net_send_data(buf) >= 0) { if (net_send_data(pkt) >= 0) {
net_stats_update_icmp_sent(); net_stats_update_icmp_sent();
return 0; return 0;
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
net_stats_update_icmp_drop(); net_stats_update_icmp_drop();
return -EIO; return -EIO;
} }
int net_icmpv4_send_error(struct net_buf *orig, uint8_t type, uint8_t code) int net_icmpv4_send_error(struct net_pkt *orig, uint8_t type, uint8_t code)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct net_if *iface = net_pkt_iface(orig); struct net_if *iface = net_pkt_iface(orig);
size_t extra_len, reserve; size_t extra_len, reserve;
struct in_addr addr, *src, *dst; struct in_addr addr, *src, *dst;
@ -175,16 +177,16 @@ int net_icmpv4_send_error(struct net_buf *orig, uint8_t type, uint8_t code)
if (NET_ICMP_BUF(orig)->code < 8) { if (NET_ICMP_BUF(orig)->code < 8) {
/* We must not send ICMP errors back */ /* We must not send ICMP errors back */
err = -EINVAL; err = -EINVAL;
goto drop_no_buf; goto drop_no_pkt;
} }
} }
iface = net_pkt_iface(orig); iface = net_pkt_iface(orig);
buf = net_pkt_get_reserve_tx(0, BUF_WAIT_TIME); pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME);
if (!buf) { if (!pkt) {
err = -ENOMEM; err = -ENOMEM;
goto drop_no_buf; goto drop_no_pkt;
} }
reserve = sizeof(struct net_ipv4_hdr) + sizeof(struct net_icmp_hdr) + reserve = sizeof(struct net_ipv4_hdr) + sizeof(struct net_icmp_hdr) +
@ -208,7 +210,7 @@ int net_icmpv4_send_error(struct net_buf *orig, uint8_t type, uint8_t code)
} }
/* We need to remember the original location of source and destination /* We need to remember the original location of source and destination
* addresses as the net_pkt_copy() will mangle the original buffer. * addresses as the net_pkt_copy() will mangle the original packet.
*/ */
src = &NET_IPV4_BUF(orig)->src; src = &NET_IPV4_BUF(orig)->src;
dst = &NET_IPV4_BUF(orig)->dst; dst = &NET_IPV4_BUF(orig)->dst;
@ -216,53 +218,53 @@ int net_icmpv4_send_error(struct net_buf *orig, uint8_t type, uint8_t code)
/* We only copy minimal IPv4 + next header from original message. /* We only copy minimal IPv4 + next header from original message.
* This is so that the memory pressure is minimized. * This is so that the memory pressure is minimized.
*/ */
frag = net_pkt_copy(orig, extra_len, reserve, BUF_WAIT_TIME); frag = net_pkt_copy(orig, extra_len, reserve, PKT_WAIT_TIME);
if (!frag) { if (!frag) {
err = -ENOMEM; err = -ENOMEM;
goto drop; goto drop;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
net_pkt_set_family(buf, AF_INET); net_pkt_set_family(pkt, AF_INET);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag));
setup_ipv4_header(buf, extra_len, net_if_ipv4_get_ttl(iface), setup_ipv4_header(pkt, extra_len, net_if_ipv4_get_ttl(iface),
type, code); type, code);
net_ipaddr_copy(&addr, src); net_ipaddr_copy(&addr, src);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, dst); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, dst);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, &addr); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, &addr);
net_pkt_ll_src(buf)->addr = net_pkt_ll_dst(orig)->addr; net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr;
net_pkt_ll_src(buf)->len = net_pkt_ll_dst(orig)->len; net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len;
net_pkt_ll_dst(buf)->addr = net_pkt_ll_src(orig)->addr; net_pkt_ll_dst(pkt)->addr = net_pkt_ll_src(orig)->addr;
net_pkt_ll_dst(buf)->len = net_pkt_ll_src(orig)->len; net_pkt_ll_dst(pkt)->len = net_pkt_ll_src(orig)->len;
NET_ICMP_BUF(buf)->chksum = 0; NET_ICMP_BUF(pkt)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf); NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv4(pkt);
#if defined(CONFIG_NET_DEBUG_ICMPV4) #if defined(CONFIG_NET_DEBUG_ICMPV4)
do { do {
char out[sizeof("xxx.xxx.xxx.xxx")]; char out[sizeof("xxx.xxx.xxx.xxx")];
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst)); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst));
NET_DBG("Sending ICMPv4 Error Message type %d code %d " NET_DBG("Sending ICMPv4 Error Message type %d code %d "
"from %s to %s", type, code, "from %s to %s", type, code,
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src), out);
} while (0); } while (0);
#endif /* CONFIG_NET_DEBUG_ICMPV4 */ #endif /* CONFIG_NET_DEBUG_ICMPV4 */
if (net_send_data(buf) >= 0) { if (net_send_data(pkt) >= 0) {
net_stats_update_icmp_sent(); net_stats_update_icmp_sent();
return 0; return 0;
} }
drop: drop:
net_pkt_unref(buf); net_pkt_unref(pkt);
drop_no_buf: drop_no_pkt:
net_stats_update_icmp_drop(); net_stats_update_icmp_drop();
return err; return err;
@ -278,7 +280,7 @@ void net_icmpv4_unregister_handler(struct net_icmpv4_handler *handler)
sys_slist_find_and_remove(&handlers, &handler->node); sys_slist_find_and_remove(&handlers, &handler->node);
} }
enum net_verdict net_icmpv4_input(struct net_buf *buf, enum net_verdict net_icmpv4_input(struct net_pkt *pkt,
uint8_t type, uint8_t code) uint8_t type, uint8_t code)
{ {
struct net_icmpv4_handler *cb; struct net_icmpv4_handler *cb;
@ -287,7 +289,7 @@ enum net_verdict net_icmpv4_input(struct net_buf *buf,
SYS_SLIST_FOR_EACH_CONTAINER(&handlers, cb, node) { SYS_SLIST_FOR_EACH_CONTAINER(&handlers, cb, node) {
if (cb->type == type && (cb->code == code || cb->code == 0)) { if (cb->type == type && (cb->code == code || cb->code == 0)) {
return cb->handler(buf); return cb->handler(pkt);
} }
} }

View file

@ -30,11 +30,11 @@ struct net_icmpv4_echo_req {
uint16_t sequence; uint16_t sequence;
} __packed; } __packed;
#define NET_ICMPV4_ECHO_REQ_BUF(buf) \ #define NET_ICMPV4_ECHO_REQ_BUF(pkt) \
((struct net_icmpv4_echo_req *)(net_pkt_icmp_data(buf) + \ ((struct net_icmpv4_echo_req *)(net_pkt_icmp_data(pkt) + \
sizeof(struct net_icmp_hdr))) sizeof(struct net_icmp_hdr)))
typedef enum net_verdict (*icmpv4_callback_handler_t)(struct net_buf *buf); typedef enum net_verdict (*icmpv4_callback_handler_t)(struct net_pkt *pkt);
struct net_icmpv4_handler { struct net_icmpv4_handler {
sys_snode_t node; sys_snode_t node;
@ -45,12 +45,12 @@ struct net_icmpv4_handler {
/** /**
* @brief Send ICMPv4 error message. * @brief Send ICMPv4 error message.
* @param buf Network buffer that this error is related to. * @param pkt Network packet that this error is related to.
* @param type Type of the error message. * @param type Type of the error message.
* @param code Code of the type of the error message. * @param code Code of the type of the error message.
* @return Return 0 if the sending succeed, <0 otherwise. * @return Return 0 if the sending succeed, <0 otherwise.
*/ */
int net_icmpv4_send_error(struct net_buf *buf, uint8_t type, uint8_t code); int net_icmpv4_send_error(struct net_pkt *pkt, uint8_t type, uint8_t code);
/** /**
* @brief Send ICMPv4 echo request message. * @brief Send ICMPv4 echo request message.
@ -73,7 +73,7 @@ void net_icmpv4_register_handler(struct net_icmpv4_handler *handler);
void net_icmpv4_unregister_handler(struct net_icmpv4_handler *handler); void net_icmpv4_unregister_handler(struct net_icmpv4_handler *handler);
enum net_verdict net_icmpv4_input(struct net_buf *buf, enum net_verdict net_icmpv4_input(struct net_pkt *pkt,
uint8_t type, uint8_t code); uint8_t type, uint8_t code);
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)

View file

@ -28,7 +28,7 @@
#include "rpl.h" #include "rpl.h"
#endif #endif
#define BUF_WAIT_TIME K_SECONDS(1) #define PKT_WAIT_TIME K_SECONDS(1)
static sys_slist_t handlers; static sys_slist_t handlers;
@ -74,59 +74,59 @@ void net_icmpv6_unregister_handler(struct net_icmpv6_handler *handler)
sys_slist_find_and_remove(&handlers, &handler->node); sys_slist_find_and_remove(&handlers, &handler->node);
} }
static inline void setup_ipv6_header(struct net_buf *buf, uint16_t extra_len, static inline void setup_ipv6_header(struct net_pkt *pkt, uint16_t extra_len,
uint8_t hop_limit, uint8_t icmp_type, uint8_t hop_limit, uint8_t icmp_type,
uint8_t icmp_code) uint8_t icmp_code)
{ {
NET_IPV6_BUF(buf)->vtc = 0x60; NET_IPV6_BUF(pkt)->vtc = 0x60;
NET_IPV6_BUF(buf)->tcflow = 0; NET_IPV6_BUF(pkt)->tcflow = 0;
NET_IPV6_BUF(buf)->flow = 0; NET_IPV6_BUF(pkt)->flow = 0;
sys_put_be16(NET_ICMPH_LEN + extra_len + NET_ICMPV6_UNUSED_LEN, sys_put_be16(NET_ICMPH_LEN + extra_len + NET_ICMPV6_UNUSED_LEN,
NET_IPV6_BUF(buf)->len); NET_IPV6_BUF(pkt)->len);
NET_IPV6_BUF(buf)->nexthdr = IPPROTO_ICMPV6; NET_IPV6_BUF(pkt)->nexthdr = IPPROTO_ICMPV6;
NET_IPV6_BUF(buf)->hop_limit = hop_limit; NET_IPV6_BUF(pkt)->hop_limit = hop_limit;
net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr));
NET_ICMP_BUF(buf)->type = icmp_type; NET_ICMP_BUF(pkt)->type = icmp_type;
NET_ICMP_BUF(buf)->code = icmp_code; NET_ICMP_BUF(pkt)->code = icmp_code;
/* ICMPv6 header has 4 unused bytes that must be zero, RFC 4443 ch 3.1 /* ICMPv6 header has 4 unused bytes that must be zero, RFC 4443 ch 3.1
*/ */
memset(net_pkt_icmp_data(buf) + sizeof(struct net_icmp_hdr), 0, memset(net_pkt_icmp_data(pkt) + sizeof(struct net_icmp_hdr), 0,
NET_ICMPV6_UNUSED_LEN); NET_ICMPV6_UNUSED_LEN);
} }
#if defined(CONFIG_NET_DEBUG_ICMPV6) #if defined(CONFIG_NET_DEBUG_ICMPV6)
static inline void echo_request_debug(struct net_buf *buf) static inline void echo_request_debug(struct net_pkt *pkt)
{ {
char out[NET_IPV6_ADDR_LEN]; char out[NET_IPV6_ADDR_LEN];
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst)); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst));
NET_DBG("Received Echo Request from %s to %s", NET_DBG("Received Echo Request from %s to %s",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src), out); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src), out);
} }
static inline void echo_reply_debug(struct net_buf *buf) static inline void echo_reply_debug(struct net_pkt *pkt)
{ {
char out[NET_IPV6_ADDR_LEN]; char out[NET_IPV6_ADDR_LEN];
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst)); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst));
NET_DBG("Sending Echo Reply from %s to %s", NET_DBG("Sending Echo Reply from %s to %s",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src), out); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src), out);
} }
#else #else
#define echo_request_debug(buf) #define echo_request_debug(pkt)
#define echo_reply_debug(buf) #define echo_reply_debug(pkt)
#endif /* CONFIG_NET_DEBUG_ICMPV6 */ #endif /* CONFIG_NET_DEBUG_ICMPV6 */
static enum net_verdict handle_echo_request(struct net_buf *orig) static enum net_verdict handle_echo_request(struct net_pkt *orig)
{ {
struct net_buf *buf; struct net_pkt *pkt;
struct net_buf *frag; struct net_buf *frag;
struct net_if *iface; struct net_if *iface;
uint16_t payload_len; uint16_t payload_len;
@ -135,83 +135,83 @@ static enum net_verdict handle_echo_request(struct net_buf *orig)
iface = net_pkt_iface(orig); iface = net_pkt_iface(orig);
buf = net_pkt_get_reserve_tx(0, BUF_WAIT_TIME); pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME);
if (!buf) { if (!pkt) {
goto drop_no_buf; goto drop_no_pkt;
} }
payload_len = sys_get_be16(NET_IPV6_BUF(orig)->len) - payload_len = sys_get_be16(NET_IPV6_BUF(orig)->len) -
sizeof(NET_ICMPH_LEN) - NET_ICMPV6_UNUSED_LEN; sizeof(NET_ICMPH_LEN) - NET_ICMPV6_UNUSED_LEN;
frag = net_pkt_copy_all(orig, 0, BUF_WAIT_TIME); frag = net_pkt_copy_all(orig, 0, PKT_WAIT_TIME);
if (!frag) { if (!frag) {
goto drop; goto drop;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
net_pkt_set_family(buf, AF_INET6); net_pkt_set_family(pkt, AF_INET6);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag));
net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr));
if (net_pkt_ext_len(orig)) { if (net_pkt_ext_len(orig)) {
net_pkt_set_ext_len(buf, net_pkt_ext_len(orig)); net_pkt_set_ext_len(pkt, net_pkt_ext_len(orig));
} else { } else {
net_pkt_set_ext_len(buf, 0); net_pkt_set_ext_len(pkt, 0);
} }
/* Set up IPv6 Header fields */ /* Set up IPv6 Header fields */
NET_IPV6_BUF(buf)->vtc = 0x60; NET_IPV6_BUF(pkt)->vtc = 0x60;
NET_IPV6_BUF(buf)->tcflow = 0; NET_IPV6_BUF(pkt)->tcflow = 0;
NET_IPV6_BUF(buf)->flow = 0; NET_IPV6_BUF(pkt)->flow = 0;
NET_IPV6_BUF(buf)->hop_limit = net_if_ipv6_get_hop_limit(iface); NET_IPV6_BUF(pkt)->hop_limit = net_if_ipv6_get_hop_limit(iface);
/* ICMPv6 fields */ /* ICMPv6 fields */
NET_ICMP_BUF(buf)->type = NET_ICMPV6_ECHO_REPLY; NET_ICMP_BUF(pkt)->type = NET_ICMPV6_ECHO_REPLY;
NET_ICMP_BUF(buf)->code = 0; NET_ICMP_BUF(pkt)->code = 0;
NET_ICMP_BUF(buf)->chksum = 0; NET_ICMP_BUF(pkt)->chksum = 0;
if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) {
net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst,
&NET_IPV6_BUF(orig)->src); &NET_IPV6_BUF(orig)->src);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src,
net_if_ipv6_select_src_addr(iface, net_if_ipv6_select_src_addr(iface,
&NET_IPV6_BUF(orig)->dst)); &NET_IPV6_BUF(orig)->dst));
} else { } else {
struct in6_addr addr; struct in6_addr addr;
net_ipaddr_copy(&addr, &NET_IPV6_BUF(orig)->src); net_ipaddr_copy(&addr, &NET_IPV6_BUF(orig)->src);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src,
&NET_IPV6_BUF(orig)->dst); &NET_IPV6_BUF(orig)->dst);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, &addr); net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, &addr);
} }
if (NET_IPV6_BUF(buf)->nexthdr == NET_IPV6_NEXTHDR_HBHO) { if (NET_IPV6_BUF(pkt)->nexthdr == NET_IPV6_NEXTHDR_HBHO) {
#if defined(CONFIG_NET_RPL) #if defined(CONFIG_NET_RPL)
uint16_t offset = NET_IPV6H_LEN; uint16_t offset = NET_IPV6H_LEN;
if (net_rpl_revert_header(buf, offset, &offset) < 0) { if (net_rpl_revert_header(pkt, offset, &offset) < 0) {
/* TODO: Handle error cases */ /* TODO: Handle error cases */
goto drop; goto drop;
} }
#endif #endif
} }
net_pkt_ll_src(buf)->addr = net_pkt_ll_dst(orig)->addr; net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr;
net_pkt_ll_src(buf)->len = net_pkt_ll_dst(orig)->len; net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len;
/* We must not set the destination ll address here but trust /* We must not set the destination ll address here but trust
* that it is set properly using a value from neighbor cache. * that it is set properly using a value from neighbor cache.
*/ */
net_pkt_ll_dst(buf)->addr = NULL; net_pkt_ll_dst(pkt)->addr = NULL;
NET_ICMP_BUF(buf)->chksum = 0; NET_ICMP_BUF(pkt)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt);
echo_reply_debug(buf); echo_reply_debug(pkt);
if (net_send_data(buf) < 0) { if (net_send_data(pkt) < 0) {
goto drop; goto drop;
} }
@ -221,18 +221,19 @@ static enum net_verdict handle_echo_request(struct net_buf *orig)
return NET_OK; return NET_OK;
drop: drop:
net_pkt_unref(buf); net_pkt_unref(pkt);
drop_no_buf: drop_no_pkt:
net_stats_update_icmp_drop(); net_stats_update_icmp_drop();
return NET_DROP; return NET_DROP;
} }
int net_icmpv6_send_error(struct net_buf *orig, uint8_t type, uint8_t code, int net_icmpv6_send_error(struct net_pkt *orig, uint8_t type, uint8_t code,
uint32_t param) uint32_t param)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct net_if *iface; struct net_if *iface;
size_t extra_len, reserve; size_t extra_len, reserve;
int err = -EIO; int err = -EIO;
@ -241,16 +242,16 @@ int net_icmpv6_send_error(struct net_buf *orig, uint8_t type, uint8_t code,
if (NET_ICMP_BUF(orig)->code < 128) { if (NET_ICMP_BUF(orig)->code < 128) {
/* We must not send ICMP errors back */ /* We must not send ICMP errors back */
err = -EINVAL; err = -EINVAL;
goto drop_no_buf; goto drop_no_pkt;
} }
} }
iface = net_pkt_iface(orig); iface = net_pkt_iface(orig);
buf = net_pkt_get_reserve_tx(0, BUF_WAIT_TIME); pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME);
if (!buf) { if (!pkt) {
err = -ENOMEM; err = -ENOMEM;
goto drop_no_buf; goto drop_no_pkt;
} }
/* There is unsed part in ICMPv6 error msg header what we might need /* There is unsed part in ICMPv6 error msg header what we might need
@ -280,72 +281,72 @@ int net_icmpv6_send_error(struct net_buf *orig, uint8_t type, uint8_t code,
/* We only copy minimal IPv6 + next header from original message. /* We only copy minimal IPv6 + next header from original message.
* This is so that the memory pressure is minimized. * This is so that the memory pressure is minimized.
*/ */
frag = net_pkt_copy(orig, extra_len, reserve, BUF_WAIT_TIME); frag = net_pkt_copy(orig, extra_len, reserve, PKT_WAIT_TIME);
if (!frag) { if (!frag) {
err = -ENOMEM; err = -ENOMEM;
goto drop; goto drop;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
net_pkt_set_family(buf, AF_INET6); net_pkt_set_family(pkt, AF_INET6);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag));
net_pkt_set_ext_len(buf, 0); net_pkt_set_ext_len(pkt, 0);
setup_ipv6_header(buf, extra_len, net_if_ipv6_get_hop_limit(iface), setup_ipv6_header(pkt, extra_len, net_if_ipv6_get_hop_limit(iface),
type, code); type, code);
/* Depending on error option, we store the param into the ICMP message. /* Depending on error option, we store the param into the ICMP message.
*/ */
if (type == NET_ICMPV6_PARAM_PROBLEM) { if (type == NET_ICMPV6_PARAM_PROBLEM) {
sys_put_be32(param, net_pkt_icmp_data(buf) + sys_put_be32(param, net_pkt_icmp_data(pkt) +
sizeof(struct net_icmp_hdr)); sizeof(struct net_icmp_hdr));
} }
if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(orig)->dst)) { if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(orig)->dst)) {
net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst,
&NET_IPV6_BUF(orig)->src); &NET_IPV6_BUF(orig)->src);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src,
net_if_ipv6_select_src_addr(iface, net_if_ipv6_select_src_addr(iface,
&NET_IPV6_BUF(orig)->dst)); &NET_IPV6_BUF(orig)->dst));
} else { } else {
struct in6_addr addr; struct in6_addr addr;
net_ipaddr_copy(&addr, &NET_IPV6_BUF(orig)->src); net_ipaddr_copy(&addr, &NET_IPV6_BUF(orig)->src);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src,
&NET_IPV6_BUF(orig)->dst); &NET_IPV6_BUF(orig)->dst);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, &addr); net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, &addr);
} }
net_pkt_ll_src(buf)->addr = net_pkt_ll_dst(orig)->addr; net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr;
net_pkt_ll_src(buf)->len = net_pkt_ll_dst(orig)->len; net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len;
net_pkt_ll_dst(buf)->addr = net_pkt_ll_src(orig)->addr; net_pkt_ll_dst(pkt)->addr = net_pkt_ll_src(orig)->addr;
net_pkt_ll_dst(buf)->len = net_pkt_ll_src(orig)->len; net_pkt_ll_dst(pkt)->len = net_pkt_ll_src(orig)->len;
NET_ICMP_BUF(buf)->chksum = 0; NET_ICMP_BUF(pkt)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt);
#if defined(CONFIG_NET_DEBUG_ICMPV6) #if defined(CONFIG_NET_DEBUG_ICMPV6)
do { do {
char out[NET_IPV6_ADDR_LEN]; char out[NET_IPV6_ADDR_LEN];
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst)); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst));
NET_DBG("Sending ICMPv6 Error Message type %d code %d param %d" NET_DBG("Sending ICMPv6 Error Message type %d code %d param %d"
" from %s to %s", type, code, param, " from %s to %s", type, code, param,
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src), out); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src), out);
} while (0); } while (0);
#endif /* CONFIG_NET_DEBUG_ICMPV6 */ #endif /* CONFIG_NET_DEBUG_ICMPV6 */
if (net_send_data(buf) >= 0) { if (net_send_data(pkt) >= 0) {
net_stats_update_icmp_sent(); net_stats_update_icmp_sent();
return 0; return 0;
} }
drop: drop:
net_pkt_unref(buf); net_pkt_unref(pkt);
drop_no_buf: drop_no_pkt:
net_stats_update_icmp_drop(); net_stats_update_icmp_drop();
return err; return err;
@ -357,31 +358,31 @@ int net_icmpv6_send_echo_request(struct net_if *iface,
uint16_t sequence) uint16_t sequence)
{ {
const struct in6_addr *src; const struct in6_addr *src;
struct net_buf *buf; struct net_pkt *pkt;
src = net_if_ipv6_select_src_addr(iface, dst); src = net_if_ipv6_select_src_addr(iface, dst);
buf = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, dst), pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, dst),
K_FOREVER); K_FOREVER);
buf = net_ipv6_create_raw(buf, src, dst, iface, IPPROTO_ICMPV6); pkt = net_ipv6_create_raw(pkt, src, dst, iface, IPPROTO_ICMPV6);
net_pkt_set_family(buf, AF_INET6); net_pkt_set_family(pkt, AF_INET6);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_pkt_append_u8(buf, NET_ICMPV6_ECHO_REQUEST); net_pkt_append_u8(pkt, NET_ICMPV6_ECHO_REQUEST);
net_pkt_append_u8(buf, 0); /* code */ net_pkt_append_u8(pkt, 0); /* code */
net_pkt_append_be16(buf, 0); /* checksum */ net_pkt_append_be16(pkt, 0); /* checksum */
net_pkt_append_be16(buf, identifier); net_pkt_append_be16(pkt, identifier);
net_pkt_append_be16(buf, sequence); net_pkt_append_be16(pkt, sequence);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, src); net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, src);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, dst); net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, dst);
NET_ICMP_BUF(buf)->chksum = 0; NET_ICMP_BUF(pkt)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt);
if (net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6) < 0) { if (net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6) < 0) {
goto drop; goto drop;
} }
@ -390,26 +391,26 @@ int net_icmpv6_send_echo_request(struct net_if *iface,
char out[NET_IPV6_ADDR_LEN]; char out[NET_IPV6_ADDR_LEN];
snprintk(out, sizeof(out), "%s", snprintk(out, sizeof(out), "%s",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst)); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst));
NET_DBG("Sending ICMPv6 Echo Request type %d" NET_DBG("Sending ICMPv6 Echo Request type %d"
" from %s to %s", NET_ICMPV6_ECHO_REQUEST, " from %s to %s", NET_ICMPV6_ECHO_REQUEST,
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src), out); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src), out);
} while (0); } while (0);
#endif /* CONFIG_NET_DEBUG_ICMPV6 */ #endif /* CONFIG_NET_DEBUG_ICMPV6 */
if (net_send_data(buf) >= 0) { if (net_send_data(pkt) >= 0) {
net_stats_update_icmp_sent(); net_stats_update_icmp_sent();
return 0; return 0;
} }
drop: drop:
net_pkt_unref(buf); net_pkt_unref(pkt);
net_stats_update_icmp_drop(); net_stats_update_icmp_drop();
return -EIO; return -EIO;
} }
enum net_verdict net_icmpv6_input(struct net_buf *buf, enum net_verdict net_icmpv6_input(struct net_pkt *pkt,
uint8_t type, uint8_t code) uint8_t type, uint8_t code)
{ {
struct net_icmpv6_handler *cb; struct net_icmpv6_handler *cb;
@ -418,7 +419,7 @@ enum net_verdict net_icmpv6_input(struct net_buf *buf,
SYS_SLIST_FOR_EACH_CONTAINER(&handlers, cb, node) { SYS_SLIST_FOR_EACH_CONTAINER(&handlers, cb, node) {
if (cb->type == type && (cb->code == code || cb->code == 0)) { if (cb->type == type && (cb->code == code || cb->code == 0)) {
return cb->handler(buf); return cb->handler(pkt);
} }
} }

View file

@ -75,25 +75,25 @@ struct net_icmpv6_nd_opt_6co {
struct in6_addr prefix; struct in6_addr prefix;
} __packed; } __packed;
#define NET_ICMPV6_NS_BUF(buf) \ #define NET_ICMPV6_NS_BUF(pkt) \
((struct net_icmpv6_ns_hdr *)(net_pkt_icmp_data(buf) + \ ((struct net_icmpv6_ns_hdr *)(net_pkt_icmp_data(pkt) + \
sizeof(struct net_icmp_hdr))) sizeof(struct net_icmp_hdr)))
#define NET_ICMPV6_ND_OPT_HDR_BUF(buf) \ #define NET_ICMPV6_ND_OPT_HDR_BUF(pkt) \
((struct net_icmpv6_nd_opt_hdr *)(net_pkt_icmp_data(buf) + \ ((struct net_icmpv6_nd_opt_hdr *)(net_pkt_icmp_data(pkt) + \
sizeof(struct net_icmp_hdr) + \ sizeof(struct net_icmp_hdr) + \
net_pkt_ext_opt_len(buf))) net_pkt_ext_opt_len(pkt)))
#define NET_ICMPV6_NA_BUF(buf) \ #define NET_ICMPV6_NA_BUF(pkt) \
((struct net_icmpv6_na_hdr *)(net_pkt_icmp_data(buf) + \ ((struct net_icmpv6_na_hdr *)(net_pkt_icmp_data(pkt) + \
sizeof(struct net_icmp_hdr))) sizeof(struct net_icmp_hdr)))
#define NET_ICMPV6_RS_BUF(buf) \ #define NET_ICMPV6_RS_BUF(pkt) \
((struct net_icmpv6_rs_hdr *)(net_pkt_icmp_data(buf) + \ ((struct net_icmpv6_rs_hdr *)(net_pkt_icmp_data(pkt) + \
sizeof(struct net_icmp_hdr))) sizeof(struct net_icmp_hdr)))
#define NET_ICMPV6_RA_BUF(buf) \ #define NET_ICMPV6_RA_BUF(pkt) \
((struct net_icmpv6_ra_hdr *)(net_pkt_icmp_data(buf) + \ ((struct net_icmpv6_ra_hdr *)(net_pkt_icmp_data(pkt) + \
sizeof(struct net_icmp_hdr))) sizeof(struct net_icmp_hdr)))
#define NET_ICMPV6_ND_O_FLAG(flag) ((flag) & 0x40) #define NET_ICMPV6_ND_O_FLAG(flag) ((flag) & 0x40)
@ -148,7 +148,7 @@ struct net_icmpv6_nd_opt_6co {
/* ICMPv6 header has 4 unused bytes that must be zero, RFC 4443 ch 3.1 */ /* ICMPv6 header has 4 unused bytes that must be zero, RFC 4443 ch 3.1 */
#define NET_ICMPV6_UNUSED_LEN 4 #define NET_ICMPV6_UNUSED_LEN 4
typedef enum net_verdict (*icmpv6_callback_handler_t)(struct net_buf *buf); typedef enum net_verdict (*icmpv6_callback_handler_t)(struct net_pkt *pkt);
const char *net_icmpv6_type2str(int icmpv6_type); const char *net_icmpv6_type2str(int icmpv6_type);
@ -161,7 +161,7 @@ struct net_icmpv6_handler {
/** /**
* @brief Send ICMPv6 error message. * @brief Send ICMPv6 error message.
* @param buf Network buffer that this error is related to. * @param pkt Network packet that this error is related to.
* @param type Type of the error message. * @param type Type of the error message.
* @param code Code of the type of the error message. * @param code Code of the type of the error message.
* @param param Optional parameter value for this error. Depending on type * @param param Optional parameter value for this error. Depending on type
@ -169,7 +169,7 @@ struct net_icmpv6_handler {
* what value to use. * what value to use.
* @return Return 0 if the sending succeed, <0 otherwise. * @return Return 0 if the sending succeed, <0 otherwise.
*/ */
int net_icmpv6_send_error(struct net_buf *buf, uint8_t type, uint8_t code, int net_icmpv6_send_error(struct net_pkt *pkt, uint8_t type, uint8_t code,
uint32_t param); uint32_t param);
/** /**
@ -191,7 +191,7 @@ int net_icmpv6_send_echo_request(struct net_if *iface,
void net_icmpv6_register_handler(struct net_icmpv6_handler *handler); void net_icmpv6_register_handler(struct net_icmpv6_handler *handler);
void net_icmpv6_unregister_handler(struct net_icmpv6_handler *handler); void net_icmpv6_unregister_handler(struct net_icmpv6_handler *handler);
enum net_verdict net_icmpv6_input(struct net_buf *buf, enum net_verdict net_icmpv6_input(struct net_pkt *pkt,
uint8_t type, uint8_t code); uint8_t type, uint8_t code);
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
void net_icmpv6_init(void); void net_icmpv6_init(void);

View file

@ -24,7 +24,7 @@
#include "icmpv4.h" #include "icmpv4.h"
#include "ipv4.h" #include "ipv4.h"
struct net_buf *net_ipv4_create_raw(struct net_buf *buf, struct net_pkt *net_ipv4_create_raw(struct net_pkt *pkt,
const struct in_addr *src, const struct in_addr *src,
const struct in_addr *dst, const struct in_addr *dst,
struct net_if *iface, struct net_if *iface,
@ -32,33 +32,33 @@ struct net_buf *net_ipv4_create_raw(struct net_buf *buf,
{ {
struct net_buf *header; struct net_buf *header;
header = net_pkt_get_frag(buf, K_FOREVER); header = net_pkt_get_frag(pkt, K_FOREVER);
net_buf_frag_insert(buf, header); net_pkt_frag_insert(pkt, header);
NET_IPV4_BUF(buf)->vhl = 0x45; NET_IPV4_BUF(pkt)->vhl = 0x45;
NET_IPV4_BUF(buf)->tos = 0x00; NET_IPV4_BUF(pkt)->tos = 0x00;
NET_IPV4_BUF(buf)->proto = 0; NET_IPV4_BUF(pkt)->proto = 0;
NET_IPV4_BUF(buf)->ttl = net_if_ipv4_get_ttl(iface); NET_IPV4_BUF(pkt)->ttl = net_if_ipv4_get_ttl(iface);
NET_IPV4_BUF(buf)->offset[0] = NET_IPV4_BUF(buf)->offset[1] = 0; NET_IPV4_BUF(pkt)->offset[0] = NET_IPV4_BUF(pkt)->offset[1] = 0;
NET_IPV4_BUF(buf)->id[0] = NET_IPV4_BUF(buf)->id[1] = 0; NET_IPV4_BUF(pkt)->id[0] = NET_IPV4_BUF(pkt)->id[1] = 0;
net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, dst); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, dst);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, src); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, src);
NET_IPV4_BUF(buf)->proto = next_header; NET_IPV4_BUF(pkt)->proto = next_header;
net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));
net_pkt_set_family(buf, AF_INET); net_pkt_set_family(pkt, AF_INET);
net_buf_add(header, sizeof(struct net_ipv4_hdr)); net_buf_add(header, sizeof(struct net_ipv4_hdr));
return buf; return pkt;
} }
struct net_buf *net_ipv4_create(struct net_context *context, struct net_pkt *net_ipv4_create(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
const struct in_addr *src, const struct in_addr *src,
const struct in_addr *dst) const struct in_addr *dst)
{ {
@ -70,50 +70,50 @@ struct net_buf *net_ipv4_create(struct net_context *context,
if (net_is_ipv4_addr_unspecified(src) if (net_is_ipv4_addr_unspecified(src)
|| net_is_ipv4_addr_mcast(src)) { || net_is_ipv4_addr_mcast(src)) {
src = &net_pkt_iface(buf)->ipv4.unicast[0].address.in_addr; src = &net_pkt_iface(pkt)->ipv4.unicast[0].address.in_addr;
} }
return net_ipv4_create_raw(buf, return net_ipv4_create_raw(pkt,
src, src,
dst, dst,
net_context_get_iface(context), net_context_get_iface(context),
net_context_get_ip_proto(context)); net_context_get_ip_proto(context));
} }
int net_ipv4_finalize_raw(struct net_buf *buf, uint8_t next_header) int net_ipv4_finalize_raw(struct net_pkt *pkt, uint8_t next_header)
{ {
/* Set the length of the IPv4 header */ /* Set the length of the IPv4 header */
size_t total_len; size_t total_len;
net_pkt_compact(buf); net_pkt_compact(pkt);
total_len = net_buf_frags_len(buf->frags); total_len = net_pkt_get_len(pkt);
NET_IPV4_BUF(buf)->len[0] = total_len / 256; NET_IPV4_BUF(pkt)->len[0] = total_len / 256;
NET_IPV4_BUF(buf)->len[1] = total_len - NET_IPV4_BUF(buf)->len[0] * 256; NET_IPV4_BUF(pkt)->len[1] = total_len - NET_IPV4_BUF(pkt)->len[0] * 256;
NET_IPV4_BUF(buf)->chksum = 0; NET_IPV4_BUF(pkt)->chksum = 0;
NET_IPV4_BUF(buf)->chksum = ~net_calc_chksum_ipv4(buf); NET_IPV4_BUF(pkt)->chksum = ~net_calc_chksum_ipv4(pkt);
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
if (next_header == IPPROTO_UDP) { if (next_header == IPPROTO_UDP) {
NET_UDP_BUF(buf)->chksum = 0; NET_UDP_BUF(pkt)->chksum = 0;
NET_UDP_BUF(buf)->chksum = ~net_calc_chksum_udp(buf); NET_UDP_BUF(pkt)->chksum = ~net_calc_chksum_udp(pkt);
} }
#endif #endif
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
if (next_header == IPPROTO_TCP) { if (next_header == IPPROTO_TCP) {
NET_TCP_BUF(buf)->chksum = 0; NET_TCP_BUF(pkt)->chksum = 0;
NET_TCP_BUF(buf)->chksum = ~net_calc_chksum_tcp(buf); NET_TCP_BUF(pkt)->chksum = ~net_calc_chksum_tcp(pkt);
} }
#endif #endif
return 0; return 0;
} }
int net_ipv4_finalize(struct net_context *context, struct net_buf *buf) int net_ipv4_finalize(struct net_context *context, struct net_pkt *pkt)
{ {
return net_ipv4_finalize_raw(buf, return net_ipv4_finalize_raw(pkt,
net_context_get_ip_proto(context)); net_context_get_ip_proto(context));
} }
@ -131,26 +131,26 @@ const struct in_addr *net_ipv4_broadcast_address(void)
return &addr; return &addr;
} }
static inline enum net_verdict process_icmpv4_pkt(struct net_buf *buf, static inline enum net_verdict process_icmpv4_pkt(struct net_pkt *pkt,
struct net_ipv4_hdr *ipv4) struct net_ipv4_hdr *ipv4)
{ {
struct net_icmp_hdr *hdr = NET_ICMP_BUF(buf); struct net_icmp_hdr *hdr = NET_ICMP_BUF(pkt);
NET_DBG("ICMPv4 packet received type %d code %d", NET_DBG("ICMPv4 packet received type %d code %d",
hdr->type, hdr->code); hdr->type, hdr->code);
return net_icmpv4_input(buf, hdr->type, hdr->code); return net_icmpv4_input(pkt, hdr->type, hdr->code);
} }
enum net_verdict net_ipv4_process_pkt(struct net_buf *buf) enum net_verdict net_ipv4_process_pkt(struct net_pkt *pkt)
{ {
struct net_ipv4_hdr *hdr = NET_IPV4_BUF(buf); struct net_ipv4_hdr *hdr = NET_IPV4_BUF(pkt);
int real_len = net_buf_frags_len(buf); int real_len = net_pkt_get_len(pkt);
int pkt_len = (hdr->len[0] << 8) + hdr->len[1]; int pkt_len = (hdr->len[0] << 8) + hdr->len[1];
enum net_verdict verdict = NET_DROP; enum net_verdict verdict = NET_DROP;
if (real_len != pkt_len) { if (real_len != pkt_len) {
NET_DBG("IPv4 packet size %d buf len %d", pkt_len, real_len); NET_DBG("IPv4 packet size %d pkt len %d", pkt_len, real_len);
goto drop; goto drop;
} }
@ -165,7 +165,7 @@ enum net_verdict net_ipv4_process_pkt(struct net_buf *buf)
} while (0); } while (0);
#endif /* CONFIG_NET_DEBUG_IPV4 */ #endif /* CONFIG_NET_DEBUG_IPV4 */
net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));
if (!net_is_my_ipv4_addr(&hdr->dst)) { if (!net_is_my_ipv4_addr(&hdr->dst)) {
#if defined(CONFIG_NET_DHCPV4) #if defined(CONFIG_NET_DHCPV4)
@ -173,25 +173,25 @@ enum net_verdict net_ipv4_process_pkt(struct net_buf *buf)
net_ipv4_addr_cmp(&hdr->dst, net_ipv4_addr_cmp(&hdr->dst,
net_ipv4_broadcast_address())) { net_ipv4_broadcast_address())) {
verdict = net_conn_input(IPPROTO_UDP, buf); verdict = net_conn_input(IPPROTO_UDP, pkt);
if (verdict != NET_DROP) { if (verdict != NET_DROP) {
return verdict; return verdict;
} }
} }
#endif #endif
NET_DBG("IPv4 packet in buf %p not for me", buf); NET_DBG("IPv4 packet in pkt %p not for me", pkt);
goto drop; goto drop;
} }
switch (hdr->proto) { switch (hdr->proto) {
case IPPROTO_ICMP: case IPPROTO_ICMP:
verdict = process_icmpv4_pkt(buf, hdr); verdict = process_icmpv4_pkt(pkt, hdr);
break; break;
case IPPROTO_UDP: case IPPROTO_UDP:
verdict = net_conn_input(IPPROTO_UDP, buf); verdict = net_conn_input(IPPROTO_UDP, pkt);
break; break;
case IPPROTO_TCP: case IPPROTO_TCP:
verdict = net_conn_input(IPPROTO_TCP, buf); verdict = net_conn_input(IPPROTO_TCP, pkt);
break; break;
} }

View file

@ -23,34 +23,34 @@
#include "ipv4.h" #include "ipv4.h"
/** /**
* @brief Create IPv4 packet in provided net_buf. * @brief Create IPv4 packet in provided net_pkt.
* *
* @param buf Network buffer * @param pkt Network packet
* @param src Source IPv4 address * @param src Source IPv4 address
* @param dst Destination IPv4 address * @param dst Destination IPv4 address
* @param iface Network interface * @param iface Network interface
* @param next_header Protocol type of the next header after IPv4 header. * @param next_header Protocol type of the next header after IPv4 header.
* *
* @return Return network buffer that contains the IPv4 packet. * @return Return network packet that contains the IPv4 packet.
*/ */
struct net_buf *net_ipv4_create_raw(struct net_buf *buf, struct net_pkt *net_ipv4_create_raw(struct net_pkt *pkt,
const struct in_addr *src, const struct in_addr *src,
const struct in_addr *dst, const struct in_addr *dst,
struct net_if *iface, struct net_if *iface,
uint8_t next_header); uint8_t next_header);
/** /**
* @brief Create IPv4 packet in provided net_buf. * @brief Create IPv4 packet in provided net_pkt.
* *
* @param context Network context for a connection * @param context Network context for a connection
* @param buf Network buffer * @param pkt Network packet
* @param src_addr Source address, or NULL to choose a default * @param src_addr Source address, or NULL to choose a default
* @param dst_addr Destination IPv4 address * @param dst_addr Destination IPv4 address
* *
* @return Return network buffer that contains the IPv6 packet. * @return Return network packet that contains the IPv6 packet.
*/ */
struct net_buf *net_ipv4_create(struct net_context *context, struct net_pkt *net_ipv4_create(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
const struct in_addr *src_addr, const struct in_addr *src_addr,
const struct in_addr *dst_addr); const struct in_addr *dst_addr);
@ -60,12 +60,12 @@ struct net_buf *net_ipv4_create(struct net_context *context,
* the packet. This function will set the length of the * the packet. This function will set the length of the
* packet and calculate the higher protocol checksum if needed. * packet and calculate the higher protocol checksum if needed.
* *
* @param buf Network buffer * @param pkt Network packet
* @param next_header Protocol type of the next header after IPv4 header. * @param next_header Protocol type of the next header after IPv4 header.
* *
* @return Return 0 on Success, < 0 on Failure. * @return Return 0 on Success, < 0 on Failure.
*/ */
int net_ipv4_finalize_raw(struct net_buf *buf, uint8_t next_header); int net_ipv4_finalize_raw(struct net_pkt *pkt, uint8_t next_header);
/** /**
* @brief Finalize IPv4 packet. It should be called right before * @brief Finalize IPv4 packet. It should be called right before
@ -74,10 +74,10 @@ int net_ipv4_finalize_raw(struct net_buf *buf, uint8_t next_header);
* packet and calculate the higher protocol checksum if needed. * packet and calculate the higher protocol checksum if needed.
* *
* @param context Network context for a connection * @param context Network context for a connection
* @param buf Network buffer * @param pkt Network packet
* *
* @return Return 0 on Success, < 0 on Failure. * @return Return 0 on Success, < 0 on Failure.
*/ */
int net_ipv4_finalize(struct net_context *context, struct net_buf *buf); int net_ipv4_finalize(struct net_context *context, struct net_pkt *pkt);
#endif /* __IPV4_H */ #endif /* __IPV4_H */

File diff suppressed because it is too large Load diff

View file

@ -78,8 +78,8 @@ const char *net_ipv6_nbr_state2str(enum net_ipv6_nbr_state state);
* @brief IPv6 neighbor information. * @brief IPv6 neighbor information.
*/ */
struct net_ipv6_nbr_data { struct net_ipv6_nbr_data {
/** Any pending buffer waiting ND to finish. */ /** Any pending packet waiting ND to finish. */
struct net_buf *pending; struct net_pkt *pending;
/** IPv6 address. */ /** IPv6 address. */
struct in6_addr addr; struct in6_addr addr;
@ -121,7 +121,7 @@ struct net_ipv6_nbr_data *net_ipv6_get_nbr_by_index(uint8_t idx);
int net_ipv6_start_dad(struct net_if *iface, struct net_if_addr *ifaddr); int net_ipv6_start_dad(struct net_if *iface, struct net_if_addr *ifaddr);
#endif #endif
int net_ipv6_send_ns(struct net_if *iface, struct net_buf *pending, int net_ipv6_send_ns(struct net_if *iface, struct net_pkt *pending,
struct in6_addr *src, struct in6_addr *dst, struct in6_addr *src, struct in6_addr *dst,
struct in6_addr *tgt, bool is_my_address); struct in6_addr *tgt, bool is_my_address);
@ -133,34 +133,34 @@ int net_ipv6_send_na(struct net_if *iface, struct in6_addr *src,
uint8_t flags); uint8_t flags);
/** /**
* @brief Create IPv6 packet in provided net_buf. * @brief Create IPv6 packet in provided net_pkt.
* *
* @param buf Network buffer * @param pkt Network packet
* @param src Source IPv6 address * @param src Source IPv6 address
* @param dst Destination IPv6 address * @param dst Destination IPv6 address
* @param iface Network interface * @param iface Network interface
* @param next_header Protocol type of the next header after IPv6 header. * @param next_header Protocol type of the next header after IPv6 header.
* *
* @return Return network buffer that contains the IPv6 packet. * @return Return network packet that contains the IPv6 packet.
*/ */
struct net_buf *net_ipv6_create_raw(struct net_buf *buf, struct net_pkt *net_ipv6_create_raw(struct net_pkt *pkt,
const struct in6_addr *src, const struct in6_addr *src,
const struct in6_addr *dst, const struct in6_addr *dst,
struct net_if *iface, struct net_if *iface,
uint8_t next_header); uint8_t next_header);
/** /**
* @brief Create IPv6 packet in provided net_buf. * @brief Create IPv6 packet in provided net_pkt.
* *
* @param context Network context for a connection * @param context Network context for a connection
* @param buf Network buffer * @param pkt Network packet
* @param src_addr Source address, or NULL to choose a default from context * @param src_addr Source address, or NULL to choose a default from context
* @param dst_addr Destination IPv6 address * @param dst_addr Destination IPv6 address
* *
* @return Return network buffer that contains the IPv6 packet. * @return Return network packet that contains the IPv6 packet.
*/ */
struct net_buf *net_ipv6_create(struct net_context *context, struct net_pkt *net_ipv6_create(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
const struct in6_addr *src_addr, const struct in6_addr *src_addr,
const struct in6_addr *dst_addr); const struct in6_addr *dst_addr);
@ -170,12 +170,12 @@ struct net_buf *net_ipv6_create(struct net_context *context,
* the packet. This function will set the length of the * the packet. This function will set the length of the
* packet and calculate the higher protocol checksum if needed. * packet and calculate the higher protocol checksum if needed.
* *
* @param buf Network buffer * @param pkt Network packet
* @param next_header Protocol type of the next header after IPv6 header. * @param next_header Protocol type of the next header after IPv6 header.
* *
* @return Return 0 on Success, < 0 on Failure. * @return Return 0 on Success, < 0 on Failure.
*/ */
int net_ipv6_finalize_raw(struct net_buf *buf, uint8_t next_header); int net_ipv6_finalize_raw(struct net_pkt *pkt, uint8_t next_header);
/** /**
* @brief Finalize IPv6 packet. It should be called right before * @brief Finalize IPv6 packet. It should be called right before
@ -184,11 +184,11 @@ int net_ipv6_finalize_raw(struct net_buf *buf, uint8_t next_header);
* packet and calculate the higher protocol checksum if needed. * packet and calculate the higher protocol checksum if needed.
* *
* @param context Network context for a connection * @param context Network context for a connection
* @param buf Network buffer * @param pkt Network packet
* *
* @return Return 0 on Success, < 0 on Failure. * @return Return 0 on Success, < 0 on Failure.
*/ */
int net_ipv6_finalize(struct net_context *context, struct net_buf *buf); int net_ipv6_finalize(struct net_context *context, struct net_pkt *pkt);
#if defined(CONFIG_NET_IPV6_MLD) #if defined(CONFIG_NET_IPV6_MLD)
/** /**
@ -233,11 +233,11 @@ typedef void (*net_nbr_cb_t)(struct net_nbr *nbr, void *user_data);
* and the original message is sent after Neighbor Advertisement * and the original message is sent after Neighbor Advertisement
* message is received. * message is received.
* *
* @param buf Network buffer * @param pkt Network packet
* *
* @return Return network buffer to be sent. * @return Return network packet to be sent.
*/ */
struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf); struct net_pkt *net_ipv6_prepare_for_send(struct net_pkt *pkt);
/** /**
* @brief Look for a neighbor from it's address on an iface * @brief Look for a neighbor from it's address on an iface
@ -313,9 +313,9 @@ bool net_ipv6_nbr_rm(struct net_if *iface, struct in6_addr *addr);
void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data); void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data);
#else /* CONFIG_NET_IPV6_NBR_CACHE */ #else /* CONFIG_NET_IPV6_NBR_CACHE */
static inline struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) static inline struct net_pkt *net_ipv6_prepare_for_send(struct net_pkt *pkt)
{ {
return buf; return pkt;
} }
static inline struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface, static inline struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface,
@ -373,7 +373,7 @@ static inline void net_ipv6_nbr_set_reachable_timer(struct net_if *iface,
* This means that we should receive everything within first two fragments. * This means that we should receive everything within first two fragments.
* The first one being 1280 bytes and the second one 220 bytes. * The first one being 1280 bytes and the second one 220 bytes.
*/ */
#define NET_IPV6_FRAGMENTS_MAX_BUF 2 #define NET_IPV6_FRAGMENTS_MAX_PKT 2
/** Store pending IPv6 fragment information that is needed for reassembly. */ /** Store pending IPv6 fragment information that is needed for reassembly. */
struct net_ipv6_reassembly { struct net_ipv6_reassembly {
@ -390,7 +390,7 @@ struct net_ipv6_reassembly {
struct k_delayed_work timer; struct k_delayed_work timer;
/** Pointers to pending fragments */ /** Pointers to pending fragments */
struct net_buf *buf[NET_IPV6_FRAGMENTS_MAX_BUF]; struct net_pkt *pkt[NET_IPV6_FRAGMENTS_MAX_PKT];
/** IPv6 fragment identification */ /** IPv6 fragment identification */
uint32_t id; uint32_t id;
@ -419,12 +419,12 @@ void net_ipv6_frag_foreach(net_ipv6_frag_cb_t cb, void *user_data);
/** /**
* @brief Find the last IPv6 extension header in the network packet. * @brief Find the last IPv6 extension header in the network packet.
* *
* @param buf Network head buffer. * @param pkt Network head packet.
* *
* @return Offset to the extension header within the first fragment of net_buf. * @return Offset to the extension header within the first fragment of net_pkt.
* Return <0 if the packet is malformed. * Return <0 if the packet is malformed.
*/ */
int net_ipv6_find_last_ext_hdr(struct net_buf *buf); int net_ipv6_find_last_ext_hdr(struct net_pkt *pkt);
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
void net_ipv6_init(void); void net_ipv6_init(void);

View file

@ -24,7 +24,7 @@
struct arp_entry { struct arp_entry {
uint32_t time; /* FIXME - implement timeout functionality */ uint32_t time; /* FIXME - implement timeout functionality */
struct net_if *iface; struct net_if *iface;
struct net_buf *pending; struct net_pkt *pending;
struct in_addr ip; struct in_addr ip;
struct net_eth_addr eth; struct net_eth_addr eth;
}; };
@ -97,41 +97,42 @@ static inline struct in_addr *if_get_addr(struct net_if *iface)
return NULL; return NULL;
} }
static inline struct net_buf *prepare_arp(struct net_if *iface, static inline struct net_pkt *prepare_arp(struct net_if *iface,
struct in_addr *next_addr, struct in_addr *next_addr,
struct arp_entry *entry, struct arp_entry *entry,
struct net_buf *pending) struct net_pkt *pending)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct net_arp_hdr *hdr; struct net_arp_hdr *hdr;
struct net_eth_hdr *eth; struct net_eth_hdr *eth;
struct in_addr *my_addr; struct in_addr *my_addr;
buf = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); pkt = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER);
if (!buf) { if (!pkt) {
goto fail; goto fail;
} }
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
if (!frag) { if (!frag) {
goto fail; goto fail;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_pkt_set_family(buf, AF_INET); net_pkt_set_family(pkt, AF_INET);
hdr = NET_ARP_BUF(buf); hdr = NET_ARP_BUF(pkt);
eth = NET_ETH_BUF(buf); eth = NET_ETH_BUF(pkt);
/* If entry is not set, then we are just about to send /* If entry is not set, then we are just about to send
* an ARP request using the data in pending net_buf. * an ARP request using the data in pending net_pkt.
* This can happen if there is already a pending ARP * This can happen if there is already a pending ARP
* request and we want to send it again. * request and we want to send it again.
*/ */
if (entry) { if (entry) {
entry->pending = net_pkt_ref(pending); entry->pending = net_pkt_ref(pending);
entry->iface = net_pkt_iface(buf); entry->iface = net_pkt_iface(pkt);
net_ipaddr_copy(&entry->ip, next_addr); net_ipaddr_copy(&entry->ip, next_addr);
@ -174,15 +175,15 @@ static inline struct net_buf *prepare_arp(struct net_if *iface,
net_buf_add(frag, sizeof(struct net_arp_hdr)); net_buf_add(frag, sizeof(struct net_arp_hdr));
return buf; return pkt;
fail: fail:
net_pkt_unref(buf); net_pkt_unref(pkt);
net_pkt_unref(pending); net_pkt_unref(pending);
return NULL; return NULL;
} }
struct net_buf *net_arp_prepare(struct net_buf *buf) struct net_pkt *net_arp_prepare(struct net_pkt *pkt)
{ {
struct net_buf *frag; struct net_buf *frag;
struct arp_entry *entry, *free_entry = NULL, *non_pending = NULL; struct arp_entry *entry, *free_entry = NULL, *non_pending = NULL;
@ -190,57 +191,57 @@ struct net_buf *net_arp_prepare(struct net_buf *buf)
struct net_eth_hdr *hdr; struct net_eth_hdr *hdr;
struct in_addr *addr; struct in_addr *addr;
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
return NULL; return NULL;
} }
if (net_pkt_ll_reserve(buf) != sizeof(struct net_eth_hdr)) { if (net_pkt_ll_reserve(pkt) != sizeof(struct net_eth_hdr)) {
/* Add the ethernet header if it is missing. */ /* Add the ethernet header if it is missing. */
struct net_buf *header; struct net_buf *header;
struct net_linkaddr *ll; struct net_linkaddr *ll;
net_pkt_set_ll_reserve(buf, sizeof(struct net_eth_hdr)); net_pkt_set_ll_reserve(pkt, sizeof(struct net_eth_hdr));
header = net_pkt_get_frag(buf, K_FOREVER); header = net_pkt_get_frag(pkt, K_FOREVER);
hdr = (struct net_eth_hdr *)(header->data - hdr = (struct net_eth_hdr *)(header->data -
net_pkt_ll_reserve(buf)); net_pkt_ll_reserve(pkt));
hdr->type = htons(NET_ETH_PTYPE_IP); hdr->type = htons(NET_ETH_PTYPE_IP);
ll = net_pkt_ll_dst(buf); ll = net_pkt_ll_dst(pkt);
if (ll->addr) { if (ll->addr) {
memcpy(&hdr->dst.addr, ll->addr, memcpy(&hdr->dst.addr, ll->addr,
sizeof(struct net_eth_addr)); sizeof(struct net_eth_addr));
} }
ll = net_pkt_ll_src(buf); ll = net_pkt_ll_src(pkt);
if (ll->addr) { if (ll->addr) {
memcpy(&hdr->src.addr, ll->addr, memcpy(&hdr->src.addr, ll->addr,
sizeof(struct net_eth_addr)); sizeof(struct net_eth_addr));
} }
net_buf_frag_insert(buf, header); net_pkt_frag_insert(pkt, header);
net_pkt_compact(buf); net_pkt_compact(pkt);
} }
hdr = (struct net_eth_hdr *)net_pkt_ll(buf); hdr = (struct net_eth_hdr *)net_pkt_ll(pkt);
/* Is the destination in the local network, if not route via /* Is the destination in the local network, if not route via
* the gateway address. * the gateway address.
*/ */
if (!net_if_ipv4_addr_mask_cmp(net_pkt_iface(buf), if (!net_if_ipv4_addr_mask_cmp(net_pkt_iface(pkt),
&NET_IPV4_BUF(buf)->dst)) { &NET_IPV4_BUF(pkt)->dst)) {
addr = &net_pkt_iface(buf)->ipv4.gw; addr = &net_pkt_iface(pkt)->ipv4.gw;
} else { } else {
addr = &NET_IPV4_BUF(buf)->dst; addr = &NET_IPV4_BUF(pkt)->dst;
} }
/* If the destination address is already known, we do not need /* If the destination address is already known, we do not need
* to send any ARP packet. * to send any ARP packet.
*/ */
entry = find_entry(net_pkt_iface(buf), entry = find_entry(net_pkt_iface(pkt),
addr, &free_entry, &non_pending); addr, &free_entry, &non_pending);
if (!entry) { if (!entry) {
if (!free_entry) { if (!free_entry) {
@ -253,13 +254,13 @@ struct net_buf *net_arp_prepare(struct net_buf *buf)
* pending query to this IP address, * pending query to this IP address,
* so this packet must be discarded. * so this packet must be discarded.
*/ */
struct net_buf *req; struct net_pkt *req;
req = prepare_arp(net_pkt_iface(buf), req = prepare_arp(net_pkt_iface(pkt),
addr, NULL, buf); addr, NULL, pkt);
NET_DBG("Resending ARP %p", req); NET_DBG("Resending ARP %p", req);
net_pkt_unref(buf); net_pkt_unref(pkt);
return req; return req;
} }
@ -267,16 +268,16 @@ struct net_buf *net_arp_prepare(struct net_buf *buf)
free_entry = non_pending; free_entry = non_pending;
} }
return prepare_arp(net_pkt_iface(buf), addr, free_entry, buf); return prepare_arp(net_pkt_iface(pkt), addr, free_entry, pkt);
} }
ll = net_if_get_link_addr(entry->iface); ll = net_if_get_link_addr(entry->iface);
NET_DBG("ARP using ll %s for IP %s", NET_DBG("ARP using ll %s for IP %s",
net_sprint_ll_addr(ll->addr, sizeof(struct net_eth_addr)), net_sprint_ll_addr(ll->addr, sizeof(struct net_eth_addr)),
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src)); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src));
frag = buf->frags; frag = pkt->frags;
while (frag) { while (frag) {
/* If there is no room for link layer header, then /* If there is no room for link layer header, then
* just send the packet as is. * just send the packet as is.
@ -287,7 +288,7 @@ struct net_buf *net_arp_prepare(struct net_buf *buf)
} }
hdr = (struct net_eth_hdr *)(frag->data - hdr = (struct net_eth_hdr *)(frag->data -
net_pkt_ll_reserve(buf)); net_pkt_ll_reserve(pkt));
hdr->type = htons(NET_ETH_PTYPE_IP); hdr->type = htons(NET_ETH_PTYPE_IP);
memcpy(&hdr->src.addr, ll->addr, memcpy(&hdr->src.addr, ll->addr,
@ -298,25 +299,25 @@ struct net_buf *net_arp_prepare(struct net_buf *buf)
frag = frag->frags; frag = frag->frags;
} }
return buf; return pkt;
} }
static inline void send_pending(struct net_if *iface, struct net_buf **buf) static inline void send_pending(struct net_if *iface, struct net_pkt **pkt)
{ {
struct net_buf *pending = *buf; struct net_pkt *pending = *pkt;
NET_DBG("dst %s pending %p frag %p", NET_DBG("dst %s pending %p frag %p",
net_sprint_ipv4_addr(&NET_IPV4_BUF(pending)->dst), pending, net_sprint_ipv4_addr(&NET_IPV4_BUF(pending)->dst), pending,
pending->frags); pending->frags);
*buf = NULL; *pkt = NULL;
if (net_if_send_data(iface, pending) == NET_DROP) { if (net_if_send_data(iface, pending) == NET_DROP) {
/* This is to unref the original ref */ /* This is to unref the original ref */
net_pkt_unref(pending); net_pkt_unref(pending);
} }
/* The pending buf was referenced when /* The pending pkt was referenced when
* it was added to cache so we need to * it was added to cache so we need to
* unref it now when it is removed from * unref it now when it is removed from
* the cache. * the cache.
@ -365,29 +366,30 @@ static inline void arp_update(struct net_if *iface,
} }
} }
static inline struct net_buf *prepare_arp_reply(struct net_if *iface, static inline struct net_pkt *prepare_arp_reply(struct net_if *iface,
struct net_buf *req) struct net_pkt *req)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
struct net_arp_hdr *hdr, *query; struct net_arp_hdr *hdr, *query;
struct net_eth_hdr *eth, *eth_query; struct net_eth_hdr *eth, *eth_query;
buf = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); pkt = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER);
if (!buf) { if (!pkt) {
goto fail; goto fail;
} }
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
if (!frag) { if (!frag) {
goto fail; goto fail;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_pkt_set_family(buf, AF_INET); net_pkt_set_family(pkt, AF_INET);
hdr = NET_ARP_BUF(buf); hdr = NET_ARP_BUF(pkt);
eth = NET_ETH_BUF(buf); eth = NET_ETH_BUF(pkt);
query = NET_ARP_BUF(req); query = NET_ARP_BUF(req);
eth_query = NET_ETH_BUF(req); eth_query = NET_ETH_BUF(req);
@ -414,34 +416,34 @@ static inline struct net_buf *prepare_arp_reply(struct net_if *iface,
net_buf_add(frag, sizeof(struct net_arp_hdr)); net_buf_add(frag, sizeof(struct net_arp_hdr));
return buf; return pkt;
fail: fail:
net_pkt_unref(buf); net_pkt_unref(pkt);
return NULL; return NULL;
} }
enum net_verdict net_arp_input(struct net_buf *buf) enum net_verdict net_arp_input(struct net_pkt *pkt)
{ {
struct net_arp_hdr *arp_hdr; struct net_arp_hdr *arp_hdr;
struct net_buf *reply; struct net_pkt *reply;
if (net_buf_frags_len(buf) < (sizeof(struct net_arp_hdr) - if (net_pkt_get_len(pkt) < (sizeof(struct net_arp_hdr) -
net_pkt_ll_reserve(buf))) { net_pkt_ll_reserve(pkt))) {
NET_DBG("Invalid ARP header (len %zu, min %zu bytes)", NET_DBG("Invalid ARP header (len %zu, min %zu bytes)",
net_buf_frags_len(buf), net_pkt_get_len(pkt),
sizeof(struct net_arp_hdr) - sizeof(struct net_arp_hdr) -
net_pkt_ll_reserve(buf)); net_pkt_ll_reserve(pkt));
return NET_DROP; return NET_DROP;
} }
arp_hdr = NET_ARP_BUF(buf); arp_hdr = NET_ARP_BUF(pkt);
switch (ntohs(arp_hdr->opcode)) { switch (ntohs(arp_hdr->opcode)) {
case NET_ARP_REQUEST: case NET_ARP_REQUEST:
/* Someone wants to know our ll address */ /* Someone wants to know our ll address */
if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr, if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr,
if_get_addr(net_pkt_iface(buf)))) { if_get_addr(net_pkt_iface(pkt)))) {
/* Not for us so drop the packet silently */ /* Not for us so drop the packet silently */
return NET_DROP; return NET_DROP;
} }
@ -461,7 +463,7 @@ enum net_verdict net_arp_input(struct net_buf *buf)
#endif /* CONFIG_NET_DEBUG_ARP */ #endif /* CONFIG_NET_DEBUG_ARP */
/* Send reply */ /* Send reply */
reply = prepare_arp_reply(net_pkt_iface(buf), buf); reply = prepare_arp_reply(net_pkt_iface(pkt), pkt);
if (reply) { if (reply) {
net_if_queue_tx(net_pkt_iface(reply), reply); net_if_queue_tx(net_pkt_iface(reply), reply);
} }
@ -469,13 +471,13 @@ enum net_verdict net_arp_input(struct net_buf *buf)
case NET_ARP_REPLY: case NET_ARP_REPLY:
if (net_is_my_ipv4_addr(&arp_hdr->dst_ipaddr)) { if (net_is_my_ipv4_addr(&arp_hdr->dst_ipaddr)) {
arp_update(net_pkt_iface(buf), &arp_hdr->src_ipaddr, arp_update(net_pkt_iface(pkt), &arp_hdr->src_ipaddr,
&arp_hdr->src_hwaddr); &arp_hdr->src_hwaddr);
} }
break; break;
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
return NET_OK; return NET_OK;
} }

View file

@ -50,11 +50,11 @@ struct bt_context {
bt_addr_t dst; bt_addr_t dst;
}; };
static enum net_verdict net_bt_recv(struct net_if *iface, struct net_buf *buf) static enum net_verdict net_bt_recv(struct net_if *iface, struct net_pkt *pkt)
{ {
NET_DBG("iface %p buf %p len %zu", iface, buf, net_buf_frags_len(buf)); NET_DBG("iface %p pkt %p len %zu", iface, pkt, net_pkt_get_len(pkt));
if (!net_6lo_uncompress(buf)) { if (!net_6lo_uncompress(pkt)) {
NET_DBG("Packet decompression failed"); NET_DBG("Packet decompression failed");
return NET_DROP; return NET_DROP;
} }
@ -62,23 +62,23 @@ static enum net_verdict net_bt_recv(struct net_if *iface, struct net_buf *buf)
return NET_CONTINUE; return NET_CONTINUE;
} }
static enum net_verdict net_bt_send(struct net_if *iface, struct net_buf *buf) static enum net_verdict net_bt_send(struct net_if *iface, struct net_pkt *pkt)
{ {
struct bt_context *ctxt = net_if_get_device(iface)->driver_data; struct bt_context *ctxt = net_if_get_device(iface)->driver_data;
NET_DBG("iface %p buf %p len %zu", iface, buf, net_buf_frags_len(buf)); NET_DBG("iface %p pkt %p len %zu", iface, pkt, net_pkt_get_len(pkt));
/* Only accept IPv6 packets */ /* Only accept IPv6 packets */
if (net_pkt_family(buf) != AF_INET6) { if (net_pkt_family(pkt) != AF_INET6) {
return NET_DROP; return NET_DROP;
} }
if (!net_6lo_compress(buf, true, NULL)) { if (!net_6lo_compress(pkt, true, NULL)) {
NET_DBG("Packet compression failed"); NET_DBG("Packet compression failed");
return NET_DROP; return NET_DROP;
} }
net_if_queue_tx(ctxt->iface, buf); net_if_queue_tx(ctxt->iface, pkt);
return NET_OK; return NET_OK;
} }
@ -173,12 +173,12 @@ static void ipsp_disconnected(struct bt_l2cap_chan *chan)
static void ipsp_recv(struct bt_l2cap_chan *chan, struct net_buf *buf) static void ipsp_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
{ {
struct bt_context *ctxt = CHAN_CTXT(chan); struct bt_context *ctxt = CHAN_CTXT(chan);
struct net_buf *pkt; struct net_pkt *pkt;
NET_DBG("Incoming data channel %p len %zu", chan, NET_DBG("Incoming data channel %p len %zu", chan,
net_buf_frags_len(buf)); net_buf_frags_len(buf));
/* Get buffer for bearer / protocol related data */ /* Get packet for bearer / protocol related data */
pkt = net_pkt_get_reserve_rx(0, K_FOREVER); pkt = net_pkt_get_reserve_rx(0, K_FOREVER);
/* Set destination address */ /* Set destination address */
@ -194,7 +194,7 @@ static void ipsp_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
/* Add data buffer as fragment of RX buffer, take a reference while /* Add data buffer as fragment of RX buffer, take a reference while
* doing so since L2CAP will unref the buffer after return. * doing so since L2CAP will unref the buffer after return.
*/ */
net_buf_frag_add(pkt, net_pkt_ref(buf)); net_pkt_frag_add(pkt, net_buf_ref(buf));
if (net_recv_data(ctxt->iface, pkt) < 0) { if (net_recv_data(ctxt->iface, pkt) < 0) {
NET_DBG("Packet dropped by NET stack"); NET_DBG("Packet dropped by NET stack");
@ -222,14 +222,14 @@ static struct bt_context bt_context_data = {
.ipsp_chan.rx.mtu = L2CAP_IPSP_MTU, .ipsp_chan.rx.mtu = L2CAP_IPSP_MTU,
}; };
static int bt_iface_send(struct net_if *iface, struct net_buf *buf) static int bt_iface_send(struct net_if *iface, struct net_pkt *pkt)
{ {
struct bt_context *ctxt = net_if_get_device(iface)->driver_data; struct bt_context *ctxt = net_if_get_device(iface)->driver_data;
int ret; int ret;
NET_DBG("iface %p buf %p len %zu", iface, buf, net_buf_frags_len(buf)); NET_DBG("iface %p pkt %p len %zu", iface, pkt, net_pkt_get_len(pkt));
ret = bt_l2cap_chan_send(&ctxt->ipsp_chan.chan, buf); ret = bt_l2cap_chan_send(&ctxt->ipsp_chan.chan, pkt->frags);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }

View file

@ -10,22 +10,22 @@
#include <net/net_pkt.h> #include <net/net_pkt.h>
static inline enum net_verdict dummy_recv(struct net_if *iface, static inline enum net_verdict dummy_recv(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
net_pkt_ll_src(buf)->addr = NULL; net_pkt_ll_src(pkt)->addr = NULL;
net_pkt_ll_src(buf)->len = 0; net_pkt_ll_src(pkt)->len = 0;
net_pkt_ll_src(buf)->type = NET_LINK_DUMMY; net_pkt_ll_src(pkt)->type = NET_LINK_DUMMY;
net_pkt_ll_dst(buf)->addr = NULL; net_pkt_ll_dst(pkt)->addr = NULL;
net_pkt_ll_dst(buf)->len = 0; net_pkt_ll_dst(pkt)->len = 0;
net_pkt_ll_dst(buf)->type = NET_LINK_DUMMY; net_pkt_ll_dst(pkt)->type = NET_LINK_DUMMY;
return NET_CONTINUE; return NET_CONTINUE;
} }
static inline enum net_verdict dummy_send(struct net_if *iface, static inline enum net_verdict dummy_send(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
net_if_queue_tx(iface, buf); net_if_queue_tx(iface, pkt);
return NET_OK; return NET_OK;
} }

View file

@ -32,16 +32,16 @@ const struct net_eth_addr *net_eth_broadcast_addr(void)
} }
#if defined(CONFIG_NET_DEBUG_L2_ETHERNET) #if defined(CONFIG_NET_DEBUG_L2_ETHERNET)
#define print_ll_addrs(buf, type, len) \ #define print_ll_addrs(pkt, type, len) \
do { \ do { \
char out[sizeof("xx:xx:xx:xx:xx:xx")]; \ char out[sizeof("xx:xx:xx:xx:xx:xx")]; \
\ \
snprintk(out, sizeof(out), "%s", \ snprintk(out, sizeof(out), "%s", \
net_sprint_ll_addr(net_pkt_ll_src(buf)->addr, \ net_sprint_ll_addr(net_pkt_ll_src(pkt)->addr, \
sizeof(struct net_eth_addr))); \ sizeof(struct net_eth_addr))); \
\ \
NET_DBG("src %s dst %s type 0x%x len %zu", out, \ NET_DBG("src %s dst %s type 0x%x len %zu", out, \
net_sprint_ll_addr(net_pkt_ll_dst(buf)->addr, \ net_sprint_ll_addr(net_pkt_ll_dst(pkt)->addr, \
sizeof(struct net_eth_addr)), \ sizeof(struct net_eth_addr)), \
type, (size_t)len); \ type, (size_t)len); \
} while (0) } while (0)
@ -50,7 +50,7 @@ const struct net_eth_addr *net_eth_broadcast_addr(void)
#endif /* CONFIG_NET_DEBUG_L2_ETHERNET */ #endif /* CONFIG_NET_DEBUG_L2_ETHERNET */
static inline void ethernet_update_length(struct net_if *iface, static inline void ethernet_update_length(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
uint16_t len; uint16_t len;
@ -60,19 +60,19 @@ static inline void ethernet_update_length(struct net_if *iface,
* frame size of 60 bytes. In that case, we need to get rid of it. * frame size of 60 bytes. In that case, we need to get rid of it.
*/ */
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
len = ((NET_IPV4_BUF(buf)->len[0] << 8) + len = ((NET_IPV4_BUF(pkt)->len[0] << 8) +
NET_IPV4_BUF(buf)->len[1]); NET_IPV4_BUF(pkt)->len[1]);
} else { } else {
len = ((NET_IPV6_BUF(buf)->len[0] << 8) + len = ((NET_IPV6_BUF(pkt)->len[0] << 8) +
NET_IPV6_BUF(buf)->len[1]) + NET_IPV6_BUF(pkt)->len[1]) +
NET_IPV6H_LEN; NET_IPV6H_LEN;
} }
if (len < NET_ETH_MINIMAL_FRAME_SIZE - sizeof(struct net_eth_hdr)) { if (len < NET_ETH_MINIMAL_FRAME_SIZE - sizeof(struct net_eth_hdr)) {
struct net_buf *frag; struct net_buf *frag;
for (frag = buf->frags; frag; frag = frag->frags) { for (frag = pkt->frags; frag; frag = frag->frags) {
if (frag->len < len) { if (frag->len < len) {
len -= frag->len; len -= frag->len;
} else { } else {
@ -84,20 +84,20 @@ static inline void ethernet_update_length(struct net_if *iface,
} }
static enum net_verdict ethernet_recv(struct net_if *iface, static enum net_verdict ethernet_recv(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct net_eth_hdr *hdr = NET_ETH_BUF(buf); struct net_eth_hdr *hdr = NET_ETH_BUF(pkt);
struct net_linkaddr *lladdr; struct net_linkaddr *lladdr;
sa_family_t family; sa_family_t family;
switch (ntohs(hdr->type)) { switch (ntohs(hdr->type)) {
case NET_ETH_PTYPE_IP: case NET_ETH_PTYPE_IP:
case NET_ETH_PTYPE_ARP: case NET_ETH_PTYPE_ARP:
net_pkt_set_family(buf, AF_INET); net_pkt_set_family(pkt, AF_INET);
family = AF_INET; family = AF_INET;
break; break;
case NET_ETH_PTYPE_IPV6: case NET_ETH_PTYPE_IPV6:
net_pkt_set_family(buf, AF_INET6); net_pkt_set_family(pkt, AF_INET6);
family = AF_INET6; family = AF_INET6;
break; break;
default: default:
@ -106,17 +106,17 @@ static enum net_verdict ethernet_recv(struct net_if *iface,
} }
/* Set the pointers to ll src and dst addresses */ /* Set the pointers to ll src and dst addresses */
lladdr = net_pkt_ll_src(buf); lladdr = net_pkt_ll_src(pkt);
lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(buf))->src.addr; lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(pkt))->src.addr;
lladdr->len = sizeof(struct net_eth_addr); lladdr->len = sizeof(struct net_eth_addr);
lladdr->type = NET_LINK_ETHERNET; lladdr->type = NET_LINK_ETHERNET;
lladdr = net_pkt_ll_dst(buf); lladdr = net_pkt_ll_dst(pkt);
lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(buf))->dst.addr; lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(pkt))->dst.addr;
lladdr->len = sizeof(struct net_eth_addr); lladdr->len = sizeof(struct net_eth_addr);
lladdr->type = NET_LINK_ETHERNET; lladdr->type = NET_LINK_ETHERNET;
print_ll_addrs(buf, ntohs(hdr->type), net_buf_frags_len(buf)); print_ll_addrs(pkt, ntohs(hdr->type), net_pkt_get_len(pkt));
if (!net_eth_is_addr_broadcast((struct net_eth_addr *)lladdr->addr) && if (!net_eth_is_addr_broadcast((struct net_eth_addr *)lladdr->addr) &&
!net_eth_is_addr_multicast((struct net_eth_addr *)lladdr->addr) && !net_eth_is_addr_multicast((struct net_eth_addr *)lladdr->addr) &&
@ -131,48 +131,48 @@ static enum net_verdict ethernet_recv(struct net_if *iface,
return NET_DROP; return NET_DROP;
} }
net_pkt_set_ll_reserve(buf, sizeof(struct net_eth_hdr)); net_pkt_set_ll_reserve(pkt, sizeof(struct net_eth_hdr));
net_buf_pull(buf->frags, net_pkt_ll_reserve(buf)); net_buf_pull(pkt->frags, net_pkt_ll_reserve(pkt));
#ifdef CONFIG_NET_ARP #ifdef CONFIG_NET_ARP
if (family == AF_INET && hdr->type == htons(NET_ETH_PTYPE_ARP)) { if (family == AF_INET && hdr->type == htons(NET_ETH_PTYPE_ARP)) {
NET_DBG("ARP packet from %s received", NET_DBG("ARP packet from %s received",
net_sprint_ll_addr((uint8_t *)hdr->src.addr, net_sprint_ll_addr((uint8_t *)hdr->src.addr,
sizeof(struct net_eth_addr))); sizeof(struct net_eth_addr)));
return net_arp_input(buf); return net_arp_input(pkt);
} }
#endif #endif
ethernet_update_length(iface, buf); ethernet_update_length(iface, pkt);
return NET_CONTINUE; return NET_CONTINUE;
} }
static inline bool check_if_dst_is_broadcast_or_mcast(struct net_if *iface, static inline bool check_if_dst_is_broadcast_or_mcast(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct net_eth_hdr *hdr = NET_ETH_BUF(buf); struct net_eth_hdr *hdr = NET_ETH_BUF(pkt);
if (net_ipv4_addr_cmp(&NET_IPV4_BUF(buf)->dst, if (net_ipv4_addr_cmp(&NET_IPV4_BUF(pkt)->dst,
net_ipv4_broadcast_address())) { net_ipv4_broadcast_address())) {
/* Broadcast address */ /* Broadcast address */
net_pkt_ll_dst(buf)->addr = (uint8_t *)broadcast_eth_addr.addr; net_pkt_ll_dst(pkt)->addr = (uint8_t *)broadcast_eth_addr.addr;
net_pkt_ll_dst(buf)->len = sizeof(struct net_eth_addr); net_pkt_ll_dst(pkt)->len = sizeof(struct net_eth_addr);
net_pkt_ll_src(buf)->addr = net_if_get_link_addr(iface)->addr; net_pkt_ll_src(pkt)->addr = net_if_get_link_addr(iface)->addr;
net_pkt_ll_src(buf)->len = sizeof(struct net_eth_addr); net_pkt_ll_src(pkt)->len = sizeof(struct net_eth_addr);
return true; return true;
} else if (NET_IPV4_BUF(buf)->dst.s4_addr[0] == 224) { } else if (NET_IPV4_BUF(pkt)->dst.s4_addr[0] == 224) {
/* Multicast address */ /* Multicast address */
hdr->dst.addr[0] = 0x01; hdr->dst.addr[0] = 0x01;
hdr->dst.addr[1] = 0x00; hdr->dst.addr[1] = 0x00;
hdr->dst.addr[2] = 0x5e; hdr->dst.addr[2] = 0x5e;
hdr->dst.addr[3] = NET_IPV4_BUF(buf)->dst.s4_addr[1]; hdr->dst.addr[3] = NET_IPV4_BUF(pkt)->dst.s4_addr[1];
hdr->dst.addr[4] = NET_IPV4_BUF(buf)->dst.s4_addr[2]; hdr->dst.addr[4] = NET_IPV4_BUF(pkt)->dst.s4_addr[2];
hdr->dst.addr[5] = NET_IPV4_BUF(buf)->dst.s4_addr[3]; hdr->dst.addr[5] = NET_IPV4_BUF(pkt)->dst.s4_addr[3];
net_pkt_ll_dst(buf)->len = sizeof(struct net_eth_addr); net_pkt_ll_dst(pkt)->len = sizeof(struct net_eth_addr);
net_pkt_ll_src(buf)->addr = net_if_get_link_addr(iface)->addr; net_pkt_ll_src(pkt)->addr = net_if_get_link_addr(iface)->addr;
net_pkt_ll_src(buf)->len = sizeof(struct net_eth_addr); net_pkt_ll_src(pkt)->len = sizeof(struct net_eth_addr);
return true; return true;
} }
@ -181,34 +181,34 @@ static inline bool check_if_dst_is_broadcast_or_mcast(struct net_if *iface,
} }
static enum net_verdict ethernet_send(struct net_if *iface, static enum net_verdict ethernet_send(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct net_eth_hdr *hdr = NET_ETH_BUF(buf); struct net_eth_hdr *hdr = NET_ETH_BUF(pkt);
struct net_buf *frag; struct net_buf *frag;
uint16_t ptype; uint16_t ptype;
#ifdef CONFIG_NET_ARP #ifdef CONFIG_NET_ARP
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
struct net_buf *arp_buf; struct net_pkt *arp_pkt;
if (check_if_dst_is_broadcast_or_mcast(iface, buf)) { if (check_if_dst_is_broadcast_or_mcast(iface, pkt)) {
goto setup_hdr; goto setup_hdr;
} }
arp_buf = net_arp_prepare(buf); arp_pkt = net_arp_prepare(pkt);
if (!arp_buf) { if (!arp_pkt) {
return NET_DROP; return NET_DROP;
} }
NET_DBG("Sending arp buf %p (orig %p) to iface %p", NET_DBG("Sending arp pkt %p (orig %p) to iface %p",
arp_buf, buf, iface); arp_pkt, pkt, iface);
buf = arp_buf; pkt = arp_pkt;
net_pkt_ll_src(buf)->addr = (uint8_t *)&NET_ETH_BUF(buf)->src; net_pkt_ll_src(pkt)->addr = (uint8_t *)&NET_ETH_BUF(pkt)->src;
net_pkt_ll_src(buf)->len = sizeof(struct net_eth_addr); net_pkt_ll_src(pkt)->len = sizeof(struct net_eth_addr);
net_pkt_ll_dst(buf)->addr = (uint8_t *)&NET_ETH_BUF(buf)->dst; net_pkt_ll_dst(pkt)->addr = (uint8_t *)&NET_ETH_BUF(pkt)->dst;
net_pkt_ll_dst(buf)->len = sizeof(struct net_eth_addr); net_pkt_ll_dst(pkt)->len = sizeof(struct net_eth_addr);
/* For ARP message, we do not touch the packet further but will /* For ARP message, we do not touch the packet further but will
* send it as it is because the arp.c has prepared the packet * send it as it is because the arp.c has prepared the packet
@ -217,7 +217,7 @@ static enum net_verdict ethernet_send(struct net_if *iface,
goto send; goto send;
} }
#else #else
NET_DBG("Sending buf %p to iface %p", buf, iface); NET_DBG("Sending pkt %p to iface %p", pkt, iface);
#endif #endif
/* If the src ll address is multicast or broadcast, then /* If the src ll address is multicast or broadcast, then
@ -226,47 +226,47 @@ static enum net_verdict ethernet_send(struct net_if *iface,
* substitute the src address using the real ll address. * substitute the src address using the real ll address.
*/ */
if (net_eth_is_addr_broadcast((struct net_eth_addr *) if (net_eth_is_addr_broadcast((struct net_eth_addr *)
net_pkt_ll_src(buf)->addr) || net_pkt_ll_src(pkt)->addr) ||
net_eth_is_addr_multicast((struct net_eth_addr *) net_eth_is_addr_multicast((struct net_eth_addr *)
net_pkt_ll_src(buf)->addr)) { net_pkt_ll_src(pkt)->addr)) {
net_pkt_ll_src(buf)->addr = net_pkt_ll_if(buf)->addr; net_pkt_ll_src(pkt)->addr = net_pkt_ll_if(pkt)->addr;
net_pkt_ll_src(buf)->len = net_pkt_ll_if(buf)->len; net_pkt_ll_src(pkt)->len = net_pkt_ll_if(pkt)->len;
} }
/* If the destination address is not set, then use broadcast /* If the destination address is not set, then use broadcast
* or multicast address. * or multicast address.
*/ */
if (!net_pkt_ll_dst(buf)->addr) { if (!net_pkt_ll_dst(pkt)->addr) {
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6 && if (net_pkt_family(pkt) == AF_INET6 &&
net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) {
struct net_eth_addr *dst = &NET_ETH_BUF(buf)->dst; struct net_eth_addr *dst = &NET_ETH_BUF(pkt)->dst;
memcpy(dst, (uint8_t *)multicast_eth_addr.addr, memcpy(dst, (uint8_t *)multicast_eth_addr.addr,
sizeof(struct net_eth_addr) - 4); sizeof(struct net_eth_addr) - 4);
memcpy((uint8_t *)dst + 2, memcpy((uint8_t *)dst + 2,
(uint8_t *)(&NET_IPV6_BUF(buf)->dst) + 12, (uint8_t *)(&NET_IPV6_BUF(pkt)->dst) + 12,
sizeof(struct net_eth_addr) - 2); sizeof(struct net_eth_addr) - 2);
net_pkt_ll_dst(buf)->addr = (uint8_t *)dst->addr; net_pkt_ll_dst(pkt)->addr = (uint8_t *)dst->addr;
} else } else
#endif #endif
{ {
net_pkt_ll_dst(buf)->addr = net_pkt_ll_dst(pkt)->addr =
(uint8_t *)broadcast_eth_addr.addr; (uint8_t *)broadcast_eth_addr.addr;
} }
net_pkt_ll_dst(buf)->len = sizeof(struct net_eth_addr); net_pkt_ll_dst(pkt)->len = sizeof(struct net_eth_addr);
NET_DBG("Destination address was not set, using %s", NET_DBG("Destination address was not set, using %s",
net_sprint_ll_addr(net_pkt_ll_dst(buf)->addr, net_sprint_ll_addr(net_pkt_ll_dst(pkt)->addr,
net_pkt_ll_dst(buf)->len)); net_pkt_ll_dst(pkt)->len));
} }
setup_hdr: setup_hdr:
__unused; __unused;
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
ptype = htons(NET_ETH_PTYPE_IP); ptype = htons(NET_ETH_PTYPE_IP);
} else { } else {
ptype = htons(NET_ETH_PTYPE_IPV6); ptype = htons(NET_ETH_PTYPE_IPV6);
@ -274,7 +274,7 @@ setup_hdr:
/* Then go through the fragments and set the ethernet header. /* Then go through the fragments and set the ethernet header.
*/ */
frag = buf->frags; frag = pkt->frags;
NET_ASSERT_INFO(frag, "No data!"); NET_ASSERT_INFO(frag, "No data!");
@ -282,13 +282,13 @@ setup_hdr:
NET_ASSERT(net_buf_headroom(frag) > sizeof(struct net_eth_addr)); NET_ASSERT(net_buf_headroom(frag) > sizeof(struct net_eth_addr));
hdr = (struct net_eth_hdr *)(frag->data - hdr = (struct net_eth_hdr *)(frag->data -
net_pkt_ll_reserve(buf)); net_pkt_ll_reserve(pkt));
memcpy(&hdr->dst, net_pkt_ll_dst(buf)->addr, memcpy(&hdr->dst, net_pkt_ll_dst(pkt)->addr,
sizeof(struct net_eth_addr)); sizeof(struct net_eth_addr));
memcpy(&hdr->src, net_pkt_ll_src(buf)->addr, memcpy(&hdr->src, net_pkt_ll_src(pkt)->addr,
sizeof(struct net_eth_addr)); sizeof(struct net_eth_addr));
hdr->type = ptype; hdr->type = ptype;
print_ll_addrs(buf, ntohs(hdr->type), frag->len); print_ll_addrs(pkt, ntohs(hdr->type), frag->len);
frag = frag->frags; frag = frag->frags;
} }
@ -297,7 +297,7 @@ setup_hdr:
send: send:
#endif /* CONFIG_NET_ARP */ #endif /* CONFIG_NET_ARP */
net_if_queue_tx(iface, buf); net_if_queue_tx(iface, pkt);
return NET_OK; return NET_OK;
} }

View file

@ -63,14 +63,14 @@ static inline void hexdump(uint8_t *pkt, uint16_t length, uint8_t reserve)
} }
} }
static void pkt_hexdump(struct net_buf *buf, bool each_frag_reserve) static void pkt_hexdump(struct net_pkt *pkt, bool each_frag_reserve)
{ {
uint16_t reserve = each_frag_reserve ? net_pkt_ll_reserve(buf) : 0; uint16_t reserve = each_frag_reserve ? net_pkt_ll_reserve(pkt) : 0;
struct net_buf *frag; struct net_buf *frag;
printk("IEEE 802.15.4 packet content:\n"); printk("IEEE 802.15.4 packet content:\n");
frag = buf->frags; frag = pkt->frags;
while (frag) { while (frag) {
hexdump(each_frag_reserve ? hexdump(each_frag_reserve ?
frag->data - reserve : frag->data, frag->data - reserve : frag->data,
@ -87,31 +87,32 @@ static void pkt_hexdump(struct net_buf *buf, bool each_frag_reserve)
static inline void ieee802154_acknowledge(struct net_if *iface, static inline void ieee802154_acknowledge(struct net_if *iface,
struct ieee802154_mpdu *mpdu) struct ieee802154_mpdu *mpdu)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
if (!mpdu->mhr.fs->fc.ar) { if (!mpdu->mhr.fs->fc.ar) {
return; return;
} }
buf = net_pkt_get_reserve_tx(IEEE802154_ACK_PKT_LENGTH, K_FOREVER); pkt = net_pkt_get_reserve_tx(IEEE802154_ACK_PKT_LENGTH, K_FOREVER);
if (!buf) { if (!pkt) {
return; return;
} }
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
net_buf_frag_insert(buf, frag); net_pkt_frag_insert(pkt, frag);
if (ieee802154_create_ack_frame(iface, buf, mpdu->mhr.fs->sequence)) { if (ieee802154_create_ack_frame(iface, pkt, mpdu->mhr.fs->sequence)) {
const struct ieee802154_radio_api *radio = const struct ieee802154_radio_api *radio =
iface->dev->driver_api; iface->dev->driver_api;
net_buf_add(frag, IEEE802154_ACK_PKT_LENGTH); net_buf_add(frag, IEEE802154_ACK_PKT_LENGTH);
radio->tx(iface->dev, buf, frag); radio->tx(iface->dev, pkt, frag);
} }
net_pkt_unref(buf); net_pkt_unref(pkt);
return; return;
} }
@ -119,7 +120,7 @@ static inline void ieee802154_acknowledge(struct net_if *iface,
#define ieee802154_acknowledge(...) #define ieee802154_acknowledge(...)
#endif /* CONFIG_NET_L2_IEEE802154_ACK_REPLY */ #endif /* CONFIG_NET_L2_IEEE802154_ACK_REPLY */
static inline void set_buf_ll_addr(struct net_linkaddr *addr, bool comp, static inline void set_pkt_ll_addr(struct net_linkaddr *addr, bool comp,
enum ieee802154_addressing_mode mode, enum ieee802154_addressing_mode mode,
struct ieee802154_address_field *ll) struct ieee802154_address_field *ll)
{ {
@ -146,8 +147,8 @@ static inline void set_buf_ll_addr(struct net_linkaddr *addr, bool comp,
#ifdef CONFIG_NET_6LO #ifdef CONFIG_NET_6LO
static inline static inline
enum net_verdict ieee802154_manage_recv_buffer(struct net_if *iface, enum net_verdict ieee802154_manage_recv_packet(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
enum net_verdict verdict = NET_CONTINUE; enum net_verdict verdict = NET_CONTINUE;
uint32_t src; uint32_t src;
@ -156,78 +157,78 @@ enum net_verdict ieee802154_manage_recv_buffer(struct net_if *iface,
/* Upper IP stack expects the link layer address to be in /* Upper IP stack expects the link layer address to be in
* big endian format so we must swap it here. * big endian format so we must swap it here.
*/ */
if (net_pkt_ll_src(buf)->addr && if (net_pkt_ll_src(pkt)->addr &&
net_pkt_ll_src(buf)->len == IEEE802154_EXT_ADDR_LENGTH) { net_pkt_ll_src(pkt)->len == IEEE802154_EXT_ADDR_LENGTH) {
sys_mem_swap(net_pkt_ll_src(buf)->addr, sys_mem_swap(net_pkt_ll_src(pkt)->addr,
net_pkt_ll_src(buf)->len); net_pkt_ll_src(pkt)->len);
} }
if (net_pkt_ll_dst(buf)->addr && if (net_pkt_ll_dst(pkt)->addr &&
net_pkt_ll_dst(buf)->len == IEEE802154_EXT_ADDR_LENGTH) { net_pkt_ll_dst(pkt)->len == IEEE802154_EXT_ADDR_LENGTH) {
sys_mem_swap(net_pkt_ll_dst(buf)->addr, sys_mem_swap(net_pkt_ll_dst(pkt)->addr,
net_pkt_ll_dst(buf)->len); net_pkt_ll_dst(pkt)->len);
} }
/** Uncompress will drop the current fragment. Buf ll src/dst address /** Uncompress will drop the current fragment. Pkt ll src/dst address
* will then be wrong and must be updated according to the new fragment. * will then be wrong and must be updated according to the new fragment.
*/ */
src = net_pkt_ll_src(buf)->addr ? src = net_pkt_ll_src(pkt)->addr ?
net_pkt_ll_src(buf)->addr - net_pkt_ll(buf) : 0; net_pkt_ll_src(pkt)->addr - net_pkt_ll(pkt) : 0;
dst = net_pkt_ll_dst(buf)->addr ? dst = net_pkt_ll_dst(pkt)->addr ?
net_pkt_ll_dst(buf)->addr - net_pkt_ll(buf) : 0; net_pkt_ll_dst(pkt)->addr - net_pkt_ll(pkt) : 0;
#ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT #ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT
verdict = ieee802154_reassemble(buf); verdict = ieee802154_reassemble(pkt);
if (verdict == NET_DROP) { if (verdict == NET_DROP) {
goto out; goto out;
} }
#else #else
if (!net_6lo_uncompress(buf)) { if (!net_6lo_uncompress(pkt)) {
NET_DBG("Packet decompression failed"); NET_DBG("Packet decompression failed");
verdict = NET_DROP; verdict = NET_DROP;
goto out; goto out;
} }
#endif #endif
net_pkt_ll_src(buf)->addr = src ? net_pkt_ll(buf) + src : NULL; net_pkt_ll_src(pkt)->addr = src ? net_pkt_ll(pkt) + src : NULL;
net_pkt_ll_dst(buf)->addr = dst ? net_pkt_ll(buf) + dst : NULL; net_pkt_ll_dst(pkt)->addr = dst ? net_pkt_ll(pkt) + dst : NULL;
pkt_hexdump(buf, false); pkt_hexdump(pkt, false);
out: out:
return verdict; return verdict;
} }
static inline bool ieee802154_manage_send_buffer(struct net_if *iface, static inline bool ieee802154_manage_send_packet(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
bool ret; bool ret;
pkt_hexdump(buf, false); pkt_hexdump(pkt, false);
#ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT #ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT
ret = net_6lo_compress(buf, true, ieee802154_fragment); ret = net_6lo_compress(pkt, true, ieee802154_fragment);
#else #else
ret = net_6lo_compress(buf, true, NULL); ret = net_6lo_compress(pkt, true, NULL);
#endif #endif
pkt_hexdump(buf, false); pkt_hexdump(pkt, false);
return ret; return ret;
} }
#else /* CONFIG_NET_6LO */ #else /* CONFIG_NET_6LO */
#define ieee802154_manage_recv_buffer(...) NET_CONTINUE #define ieee802154_manage_recv_packet(...) NET_CONTINUE
#define ieee802154_manage_send_buffer(...) true #define ieee802154_manage_send_packet(...) true
#endif /* CONFIG_NET_6LO */ #endif /* CONFIG_NET_6LO */
static enum net_verdict ieee802154_recv(struct net_if *iface, static enum net_verdict ieee802154_recv(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct ieee802154_mpdu mpdu; struct ieee802154_mpdu mpdu;
if (!ieee802154_validate_frame(net_pkt_ll(buf), if (!ieee802154_validate_frame(net_pkt_ll(pkt),
net_buf_frags_len(buf), &mpdu)) { net_pkt_get_len(pkt), &mpdu)) {
return NET_DROP; return NET_DROP;
} }
@ -247,40 +248,40 @@ static enum net_verdict ieee802154_recv(struct net_if *iface,
ieee802154_acknowledge(iface, &mpdu); ieee802154_acknowledge(iface, &mpdu);
net_pkt_set_ll_reserve(buf, mpdu.payload - (void *)net_pkt_ll(buf)); net_pkt_set_ll_reserve(pkt, mpdu.payload - (void *)net_pkt_ll(pkt));
net_buf_pull(buf->frags, net_pkt_ll_reserve(buf)); net_buf_pull(pkt->frags, net_pkt_ll_reserve(pkt));
set_buf_ll_addr(net_pkt_ll_src(buf), mpdu.mhr.fs->fc.pan_id_comp, set_pkt_ll_addr(net_pkt_ll_src(pkt), mpdu.mhr.fs->fc.pan_id_comp,
mpdu.mhr.fs->fc.src_addr_mode, mpdu.mhr.src_addr); mpdu.mhr.fs->fc.src_addr_mode, mpdu.mhr.src_addr);
set_buf_ll_addr(net_pkt_ll_dst(buf), false, set_pkt_ll_addr(net_pkt_ll_dst(pkt), false,
mpdu.mhr.fs->fc.dst_addr_mode, mpdu.mhr.dst_addr); mpdu.mhr.fs->fc.dst_addr_mode, mpdu.mhr.dst_addr);
if (!ieee802154_decipher_data_frame(iface, buf, &mpdu)) { if (!ieee802154_decipher_data_frame(iface, pkt, &mpdu)) {
return NET_DROP; return NET_DROP;
} }
pkt_hexdump(buf, true); pkt_hexdump(pkt, true);
return ieee802154_manage_recv_buffer(iface, buf); return ieee802154_manage_recv_packet(iface, pkt);
} }
static enum net_verdict ieee802154_send(struct net_if *iface, static enum net_verdict ieee802154_send(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
uint8_t reserved_space = net_pkt_ll_reserve(buf); uint8_t reserved_space = net_pkt_ll_reserve(pkt);
struct net_buf *frag; struct net_buf *frag;
if (net_pkt_family(buf) != AF_INET6) { if (net_pkt_family(pkt) != AF_INET6) {
return NET_DROP; return NET_DROP;
} }
if (!ieee802154_manage_send_buffer(iface, buf)) { if (!ieee802154_manage_send_packet(iface, pkt)) {
return NET_DROP; return NET_DROP;
} }
frag = buf->frags; frag = pkt->frags;
while (frag) { while (frag) {
if (frag->len > IEEE802154_MTU) { if (frag->len > IEEE802154_MTU) {
NET_ERR("Frag %p as too big length %u", NET_ERR("Frag %p as too big length %u",
@ -288,7 +289,7 @@ static enum net_verdict ieee802154_send(struct net_if *iface,
return NET_DROP; return NET_DROP;
} }
if (!ieee802154_create_data_frame(ctx, net_pkt_ll_dst(buf), if (!ieee802154_create_data_frame(ctx, net_pkt_ll_dst(pkt),
frag, reserved_space)) { frag, reserved_space)) {
return NET_DROP; return NET_DROP;
} }
@ -296,9 +297,9 @@ static enum net_verdict ieee802154_send(struct net_if *iface,
frag = frag->frags; frag = frag->frags;
} }
pkt_hexdump(buf, true); pkt_hexdump(pkt, true);
net_if_queue_tx(iface, buf); net_if_queue_tx(iface, pkt);
return NET_OK; return NET_OK;
} }

View file

@ -37,7 +37,7 @@ static uint16_t datagram_tag;
*/ */
struct frag_cache { struct frag_cache {
struct k_delayed_work timer; /* Reassemble timer */ struct k_delayed_work timer; /* Reassemble timer */
struct net_buf *buf; /* Reassemble buffer */ struct net_pkt *pkt; /* Reassemble packet */
uint16_t size; /* Datagram size */ uint16_t size; /* Datagram size */
uint16_t tag; /* Datagram tag */ uint16_t tag; /* Datagram tag */
bool used; bool used;
@ -78,12 +78,12 @@ static struct frag_cache cache[REASS_CACHE_SIZE];
* +-+-+-+-+-+-+-+-+ * +-+-+-+-+-+-+-+-+
*/ */
static inline struct net_buf *prepare_new_fragment(struct net_buf *buf, static inline struct net_buf *prepare_new_fragment(struct net_pkt *pkt,
uint8_t offset) uint8_t offset)
{ {
struct net_buf *frag; struct net_buf *frag;
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
if (!frag) { if (!frag) {
return NULL; return NULL;
} }
@ -95,7 +95,7 @@ static inline struct net_buf *prepare_new_fragment(struct net_buf *buf,
net_buf_add(frag, NET_6LO_FRAGN_HDR_LEN); net_buf_add(frag, NET_6LO_FRAGN_HDR_LEN);
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
return frag; return frag;
} }
@ -134,13 +134,13 @@ static inline void set_up_frag_hdr(struct net_buf *frag, uint16_t size,
} }
} }
static inline uint8_t calc_max_payload(struct net_buf *buf, static inline uint8_t calc_max_payload(struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
uint8_t offset) uint8_t offset)
{ {
uint8_t max; uint8_t max;
max = frag->size - net_pkt_ll_reserve(buf); max = frag->size - net_pkt_ll_reserve(pkt);
max -= offset ? NET_6LO_FRAGN_HDR_LEN : NET_6LO_FRAG1_HDR_LEN; max -= offset ? NET_6LO_FRAGN_HDR_LEN : NET_6LO_FRAG1_HDR_LEN;
return (max & 0xF8); return (max & 0xF8);
@ -213,15 +213,15 @@ static inline void compact_frag(struct net_buf *frag, uint8_t moved)
* |fh + p + e | fh + p + e | fh + p + e | fh + p + e | fh + p + e | * |fh + p + e | fh + p + e | fh + p + e | fh + p + e | fh + p + e |
* *
* Space in every fragment is because fragment payload should be multiple * Space in every fragment is because fragment payload should be multiple
* of 8 octets (we have predefined buffers at compile time, data buffer mtu * of 8 octets (we have predefined packets at compile time, data packet mtu
* is set already). * is set already).
* *
* Create the first fragment, add fragmentation header and insert * Create the first fragment, add fragmentation header and insert
* fragment at beginning of buf, move data from next fragments to * fragment at beginning of pkt, move data from next fragments to
* previous one, from here on insert fragmentation header and adjust * previous one, from here on insert fragmentation header and adjust
* data on subsequent buffers. * data on subsequent packets.
*/ */
bool ieee802154_fragment(struct net_buf *buf, int hdr_diff) bool ieee802154_fragment(struct net_pkt *pkt, int hdr_diff)
{ {
struct net_buf *frag; struct net_buf *frag;
struct net_buf *next; struct net_buf *next;
@ -233,17 +233,17 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff)
uint8_t max; uint8_t max;
bool first; bool first;
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
return false; return false;
} }
/* If it is a single fragment do not add fragmentation header */ /* If it is a single fragment do not add fragmentation header */
if (!buf->frags->frags) { if (!pkt->frags->frags) {
return true; return true;
} }
/* Datagram_size: total length before compression */ /* Datagram_size: total length before compression */
size = net_buf_frags_len(buf) + hdr_diff; size = net_pkt_get_len(pkt) + hdr_diff;
room = 0; room = 0;
offset = 0; offset = 0;
@ -251,8 +251,8 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff)
first = true; first = true;
datagram_tag++; datagram_tag++;
next = buf->frags; next = pkt->frags;
buf->frags = NULL; pkt->frags = NULL;
/* First fragment has compressed header, but SIZE and OFFSET /* First fragment has compressed header, but SIZE and OFFSET
* values in fragmentation header are based on uncompressed * values in fragmentation header are based on uncompressed
@ -261,7 +261,7 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff)
while (1) { while (1) {
if (!room) { if (!room) {
/* Prepare new fragment based on offset */ /* Prepare new fragment based on offset */
frag = prepare_new_fragment(buf, offset); frag = prepare_new_fragment(pkt, offset);
if (!frag) { if (!frag) {
return false; return false;
} }
@ -270,7 +270,7 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff)
set_up_frag_hdr(frag, size, offset); set_up_frag_hdr(frag, size, offset);
/* Calculate max payload in multiples of 8 bytes */ /* Calculate max payload in multiples of 8 bytes */
max = calc_max_payload(buf, frag, offset); max = calc_max_payload(pkt, frag, offset);
/* Calculate how much data is processed */ /* Calculate how much data is processed */
processed += max; processed += max;
@ -286,7 +286,7 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff)
compact_frag(next, move); compact_frag(next, move);
if (!next->len) { if (!next->len) {
next = net_pkt_frag_del(NULL, next); next = net_pkt_frag_del(pkt, NULL, next);
if (!next) { if (!next) {
break; break;
} }
@ -312,15 +312,15 @@ static inline void remove_frag_header(struct net_buf *frag, uint8_t hdr_len)
frag->len -= hdr_len; frag->len -= hdr_len;
} }
static void update_protocol_header_lengths(struct net_buf *buf, uint16_t size) static void update_protocol_header_lengths(struct net_pkt *pkt, uint16_t size)
{ {
net_pkt_set_ip_hdr_len(buf, NET_IPV6H_LEN); net_pkt_set_ip_hdr_len(pkt, NET_IPV6H_LEN);
NET_IPV6_BUF(buf)->len[0] = (size - NET_IPV6H_LEN) >> 8; NET_IPV6_BUF(pkt)->len[0] = (size - NET_IPV6H_LEN) >> 8;
NET_IPV6_BUF(buf)->len[1] = (uint8_t) (size - NET_IPV6H_LEN); NET_IPV6_BUF(pkt)->len[1] = (uint8_t) (size - NET_IPV6H_LEN);
if (NET_IPV6_BUF(buf)->nexthdr == IPPROTO_UDP) { if (NET_IPV6_BUF(pkt)->nexthdr == IPPROTO_UDP) {
NET_UDP_BUF(buf)->len = htons(size - NET_IPV6H_LEN); NET_UDP_BUF(pkt)->len = htons(size - NET_IPV6H_LEN);
} }
} }
@ -333,11 +333,11 @@ static inline void clear_reass_cache(uint16_t size, uint16_t tag)
continue; continue;
} }
if (cache[i].buf) { if (cache[i].pkt) {
net_pkt_unref(cache[i].buf); net_pkt_unref(cache[i].pkt);
} }
cache[i].buf = NULL; cache[i].pkt = NULL;
cache[i].size = 0; cache[i].size = 0;
cache[i].tag = 0; cache[i].tag = 0;
cache[i].used = false; cache[i].used = false;
@ -353,11 +353,11 @@ static void reass_timeout(struct k_work *work)
{ {
struct frag_cache *cache = CONTAINER_OF(work, struct frag_cache, timer); struct frag_cache *cache = CONTAINER_OF(work, struct frag_cache, timer);
if (cache->buf) { if (cache->pkt) {
net_pkt_unref(cache->buf); net_pkt_unref(cache->pkt);
} }
cache->buf = NULL; cache->pkt = NULL;
cache->size = 0; cache->size = 0;
cache->tag = 0; cache->tag = 0;
cache->used = false; cache->used = false;
@ -368,7 +368,7 @@ static void reass_timeout(struct k_work *work)
* create a new cache. If number of unused cache are out then * create a new cache. If number of unused cache are out then
* discard the fragments. * discard the fragments.
*/ */
static inline struct frag_cache *set_reass_cache(struct net_buf *buf, static inline struct frag_cache *set_reass_cache(struct net_pkt *pkt,
uint16_t size, uint16_t tag) uint16_t size, uint16_t tag)
{ {
int i; int i;
@ -378,7 +378,7 @@ static inline struct frag_cache *set_reass_cache(struct net_buf *buf,
continue; continue;
} }
cache[i].buf = buf; cache[i].pkt = pkt;
cache[i].size = size; cache[i].size = size;
cache[i].tag = tag; cache[i].tag = tag;
cache[i].used = true; cache[i].used = true;
@ -411,8 +411,8 @@ static inline struct frag_cache *get_reass_cache(uint16_t size, uint16_t tag)
return NULL; return NULL;
} }
/* Helper function to write fragment data to Rx buffer based on offset. */ /* Helper function to write fragment data to Rx packet based on offset. */
static inline bool copy_frag(struct net_buf *buf, static inline bool copy_frag(struct net_pkt *pkt,
struct net_buf *frag, struct net_buf *frag,
uint16_t offset) uint16_t offset)
{ {
@ -420,17 +420,17 @@ static inline bool copy_frag(struct net_buf *buf,
struct net_buf *write; struct net_buf *write;
uint16_t pos = offset; uint16_t pos = offset;
write = buf->frags; write = pkt->frags;
while (input) { while (input) {
write = net_pkt_write(buf, write, pos, &pos, input->len, write = net_pkt_write(pkt, write, pos, &pos, input->len,
input->data, NET_6LO_RX_PKT_TIMEOUT); input->data, NET_6LO_RX_PKT_TIMEOUT);
if (!write && pos == 0xffff) { if (!write && pos == 0xffff) {
/* Free the new bufs we tried to get, we need to discard /* Free the new bufs we tried to get, we need to discard
* the whole fragment chain. * the whole fragment chain.
*/ */
net_pkt_unref(buf->frags); net_pkt_frag_unref(pkt->frags);
buf->frags = NULL; pkt->frags = NULL;
return false; return false;
} }
@ -438,7 +438,7 @@ static inline bool copy_frag(struct net_buf *buf,
input = input->frags; input = input->frags;
} }
net_pkt_unref(frag); net_pkt_frag_unref(frag);
return true; return true;
} }
@ -449,9 +449,9 @@ static inline bool copy_frag(struct net_buf *buf,
* Remove the fragmentation header and uncompress IPv6 and related headers. * Remove the fragmentation header and uncompress IPv6 and related headers.
* Cache Rx part of fragment along with data buf for the first fragment * Cache Rx part of fragment along with data buf for the first fragment
* in the cache, remaining fragments just cache data fragment, unref * in the cache, remaining fragments just cache data fragment, unref
* RX buf. So in both the cases caller can assume buffer is consumed. * RX pkt. So in both the cases caller can assume packet is consumed.
*/ */
static inline enum net_verdict add_frag_to_cache(struct net_buf *buf, static inline enum net_verdict add_frag_to_cache(struct net_pkt *pkt,
bool first) bool first)
{ {
struct frag_cache *cache; struct frag_cache *cache;
@ -462,23 +462,23 @@ static inline enum net_verdict add_frag_to_cache(struct net_buf *buf,
uint8_t pos = 0; uint8_t pos = 0;
/* Parse total size of packet */ /* Parse total size of packet */
size = get_datagram_size(buf->frags->data); size = get_datagram_size(pkt->frags->data);
pos += NET_6LO_FRAG_DATAGRAM_SIZE_LEN; pos += NET_6LO_FRAG_DATAGRAM_SIZE_LEN;
/* Parse the datagram tag */ /* Parse the datagram tag */
tag = get_datagram_tag(buf->frags->data + pos); tag = get_datagram_tag(pkt->frags->data + pos);
pos += NET_6LO_FRAG_DATAGRAM_OFFSET_LEN; pos += NET_6LO_FRAG_DATAGRAM_OFFSET_LEN;
if (!first) { if (!first) {
offset = ((uint16_t)buf->frags->data[pos]) << 3; offset = ((uint16_t)pkt->frags->data[pos]) << 3;
pos++; pos++;
} }
/* Remove frag header and update data */ /* Remove frag header and update data */
remove_frag_header(buf->frags, pos); remove_frag_header(pkt->frags, pos);
/* Uncompress the IP headers */ /* Uncompress the IP headers */
if (first && !net_6lo_uncompress(buf)) { if (first && !net_6lo_uncompress(pkt)) {
NET_ERR("Could not uncompress first frag's 6lo hdr"); NET_ERR("Could not uncompress first frag's 6lo hdr");
clear_reass_cache(size, tag); clear_reass_cache(size, tag);
@ -486,33 +486,33 @@ static inline enum net_verdict add_frag_to_cache(struct net_buf *buf,
} }
/* If there are no fragments in the cache means this frag /* If there are no fragments in the cache means this frag
* is the first one. So cache Rx buf otherwise not. * is the first one. So cache Rx pkt otherwise not.
* Write data fragment data to cached Rx based on offset parameter. * Write data fragment data to cached Rx based on offset parameter.
* (Detach data fragment from incoming Rx and copy that data). * (Detach data fragment from incoming Rx and copy that data).
*/ */
frag = buf->frags; frag = pkt->frags;
buf->frags = NULL; pkt->frags = NULL;
cache = get_reass_cache(size, tag); cache = get_reass_cache(size, tag);
if (!cache) { if (!cache) {
cache = set_reass_cache(buf, size, tag); cache = set_reass_cache(pkt, size, tag);
if (!cache) { if (!cache) {
NET_ERR("Could not get a cache entry"); NET_ERR("Could not get a cache entry");
buf->frags = frag; pkt->frags = frag;
return NET_DROP; return NET_DROP;
} }
/* If write failed, then attach frag back to incoming buffer /* If write failed, then attach frag back to incoming packet
* and return NET_DROP, caller will take care of freeing it. * and return NET_DROP, caller will take care of freeing it.
*/ */
if (!copy_frag(cache->buf, frag, offset)) { if (!copy_frag(cache->pkt, frag, offset)) {
buf->frags = frag; pkt->frags = frag;
/* Initialize to NULL to prevent duble free. It's only /* Initialize to NULL to prevent duble free. It's only
* needed here because this is the first fragment. * needed here because this is the first fragment.
*/ */
cache->buf = NULL; cache->pkt = NULL;
clear_reass_cache(size, tag); clear_reass_cache(size, tag);
@ -521,14 +521,14 @@ static inline enum net_verdict add_frag_to_cache(struct net_buf *buf,
return NET_DROP; return NET_DROP;
} }
NET_DBG("buffer inserted into cache"); NET_DBG("packet inserted into cache");
return NET_OK; return NET_OK;
} }
/* Add data buffer to reassembly buffer */ /* Add data packet to reassembly packet */
if (!copy_frag(cache->buf, frag, offset)) { if (!copy_frag(cache->pkt, frag, offset)) {
buf->frags = frag; pkt->frags = frag;
clear_reass_cache(size, tag); clear_reass_cache(size, tag);
@ -536,13 +536,13 @@ static inline enum net_verdict add_frag_to_cache(struct net_buf *buf,
} }
/* Check if all the fragments are received or not */ /* Check if all the fragments are received or not */
if (net_buf_frags_len(cache->buf->frags) == size) { if (net_pkt_get_len(cache->pkt) == size) {
/* Assign frags back to input buffer. */ /* Assign frags back to input packet. */
buf->frags = cache->buf->frags; pkt->frags = cache->pkt->frags;
cache->buf->frags = NULL; cache->pkt->frags = NULL;
/* Lengths are elided in compression, so calculate it. */ /* Lengths are elided in compression, so calculate it. */
update_protocol_header_lengths(buf, cache->size); update_protocol_header_lengths(pkt, cache->size);
/* Once reassemble is done, cache is no longer needed. */ /* Once reassemble is done, cache is no longer needed. */
clear_reass_cache(size, tag); clear_reass_cache(size, tag);
@ -552,30 +552,30 @@ static inline enum net_verdict add_frag_to_cache(struct net_buf *buf,
return NET_CONTINUE; return NET_CONTINUE;
} }
/* Unref Rx part of original buffer */ /* Unref Rx part of original packet */
net_pkt_unref(buf); net_pkt_unref(pkt);
return NET_OK; return NET_OK;
} }
enum net_verdict ieee802154_reassemble(struct net_buf *buf) enum net_verdict ieee802154_reassemble(struct net_pkt *pkt)
{ {
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
NET_ERR("Nothing to reassemble"); NET_ERR("Nothing to reassemble");
return NET_DROP; return NET_DROP;
} }
switch (buf->frags->data[0] & 0xF0) { switch (pkt->frags->data[0] & 0xF0) {
case NET_6LO_DISPATCH_FRAG1: case NET_6LO_DISPATCH_FRAG1:
/* First fragment with IP headers */ /* First fragment with IP headers */
return add_frag_to_cache(buf, true); return add_frag_to_cache(pkt, true);
case NET_6LO_DISPATCH_FRAGN: case NET_6LO_DISPATCH_FRAGN:
/* Further fragments */ /* Further fragments */
return add_frag_to_cache(buf, false); return add_frag_to_cache(pkt, false);
default: default:
NET_DBG("No frag dispatch (%02x)", buf->frags->data[0]); NET_DBG("No frag dispatch (%02x)", pkt->frags->data[0]);
/* Received unfragmented packet, uncompress */ /* Received unfragmented packet, uncompress */
if (net_6lo_uncompress(buf)) { if (net_6lo_uncompress(pkt)) {
return NET_CONTINUE; return NET_CONTINUE;
} }

View file

@ -25,12 +25,12 @@
* needs to be fragmented. Every fragment will have fragmentation header * needs to be fragmented. Every fragment will have fragmentation header
* data size, data offset, data tag and payload. * data size, data offset, data tag and payload.
* *
* @param Pointer to network buffer * @param Pointer to network packet
* @param Header difference between original IPv6 header and compressed header * @param Header difference between original IPv6 header and compressed header
* *
* @return True in case of success, false otherwise * @return True in case of success, false otherwise
*/ */
bool ieee802154_fragment(struct net_buf *buf, int hdr_diff); bool ieee802154_fragment(struct net_pkt *pkt, int hdr_diff);
/** /**
* @brief Reassemble 802.15.4 fragments as per RFC 6282 * @brief Reassemble 802.15.4 fragments as per RFC 6282
@ -43,11 +43,11 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff);
* @param Pointer to network fragment, which gets updated to full reassembled * @param Pointer to network fragment, which gets updated to full reassembled
* packet when verdict is NET_CONTINUE * packet when verdict is NET_CONTINUE
* *
* @return NET_CONTINUE reassembly done, buf is complete * @return NET_CONTINUE reassembly done, pkt is complete
* NET_OK waiting for other fragments, * NET_OK waiting for other fragments,
* NET_DROP invalid fragment. * NET_DROP invalid fragment.
*/ */
enum net_verdict ieee802154_reassemble(struct net_buf *buf); enum net_verdict ieee802154_reassemble(struct net_pkt *pkt);
#endif /* __NET_IEEE802154_FRAGMENT_H__ */ #endif /* __NET_IEEE802154_FRAGMENT_H__ */

View file

@ -472,7 +472,7 @@ uint16_t ieee802154_compute_header_size(struct net_if *iface,
hdr_len += IEEE8021254_KEY_ID_FIELD_SRC_8_INDEX_LENGTH; hdr_len += IEEE8021254_KEY_ID_FIELD_SRC_8_INDEX_LENGTH;
} }
/* This is a _HACK_: as net buf do not let the possibility to /* This is a _HACK_: as net pkt do not let the possibility to
* reserve tailroom - here for authentication tag - it reserves * reserve tailroom - here for authentication tag - it reserves
* it in headroom so the payload won't occupy all the left space * it in headroom so the payload won't occupy all the left space
* and then when it will come to finalize the data frame it will * and then when it will come to finalize the data frame it will
@ -808,27 +808,28 @@ static inline uint8_t mac_command_length(enum ieee802154_cfi cfi)
return reserve; return reserve;
} }
struct net_buf * struct net_pkt *
ieee802154_create_mac_cmd_frame(struct ieee802154_context *ctx, ieee802154_create_mac_cmd_frame(struct ieee802154_context *ctx,
enum ieee802154_cfi type, enum ieee802154_cfi type,
struct ieee802154_frame_params *params) struct ieee802154_frame_params *params)
{ {
struct ieee802154_fcf_seq *fs; struct ieee802154_fcf_seq *fs;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
uint8_t *p_buf; uint8_t *p_buf;
buf = net_pkt_get_reserve_tx(0, K_FOREVER); pkt = net_pkt_get_reserve_tx(0, K_FOREVER);
if (!buf) { if (!pkt) {
return NULL; return NULL;
} }
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
if (!frag) { if (!frag) {
goto error; goto error;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
p_buf = net_pkt_ll(buf); p_buf = net_pkt_ll(pkt);
fs = generate_fcf_grounds(&p_buf, fs = generate_fcf_grounds(&p_buf,
type == IEEE802154_CFI_BEACON_REQUEST ? type == IEEE802154_CFI_BEACON_REQUEST ?
@ -851,20 +852,20 @@ ieee802154_create_mac_cmd_frame(struct ieee802154_context *ctx,
* to be easy to handle afterwards to point directly to MAC * to be easy to handle afterwards to point directly to MAC
* command space, in order to fill-in its content. * command space, in order to fill-in its content.
*/ */
net_pkt_set_ll_reserve(buf, p_buf - net_pkt_ll(buf)); net_pkt_set_ll_reserve(pkt, p_buf - net_pkt_ll(pkt));
net_buf_pull(frag, net_pkt_ll_reserve(buf)); net_buf_pull(frag, net_pkt_ll_reserve(pkt));
/* Thus setting the right MAC command length /* Thus setting the right MAC command length
* Now up to the caller to fill-in this space relevantly. * Now up to the caller to fill-in this space relevantly.
* See ieee802154_mac_command() helper. * See ieee802154_mac_command() helper.
*/ */
net_pkt_set_len(frag, mac_command_length(type)); frag->len = mac_command_length(type);
dbg_print_fs(fs); dbg_print_fs(fs);
return buf; return pkt;
error: error:
net_pkt_unref(buf); net_pkt_unref(pkt);
return NULL; return NULL;
} }
@ -872,9 +873,9 @@ error:
#ifdef CONFIG_NET_L2_IEEE802154_ACK_REPLY #ifdef CONFIG_NET_L2_IEEE802154_ACK_REPLY
bool ieee802154_create_ack_frame(struct net_if *iface, bool ieee802154_create_ack_frame(struct net_if *iface,
struct net_buf *buf, uint8_t seq) struct net_pkt *pkt, uint8_t seq)
{ {
uint8_t *p_buf = net_pkt_ll(buf); uint8_t *p_buf = net_pkt_ll(pkt);
struct ieee802154_fcf_seq *fs; struct ieee802154_fcf_seq *fs;
if (!p_buf) { if (!p_buf) {
@ -894,7 +895,7 @@ bool ieee802154_create_ack_frame(struct net_if *iface,
#endif /* CONFIG_NET_L2_IEEE802154_ACK_REPLY */ #endif /* CONFIG_NET_L2_IEEE802154_ACK_REPLY */
#ifdef CONFIG_NET_L2_IEEE802154_SECURITY #ifdef CONFIG_NET_L2_IEEE802154_SECURITY
bool ieee802154_decipher_data_frame(struct net_if *iface, struct net_buf *buf, bool ieee802154_decipher_data_frame(struct net_if *iface, struct net_pkt *pkt,
struct ieee802154_mpdu *mpdu) struct ieee802154_mpdu *mpdu)
{ {
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
@ -916,10 +917,10 @@ bool ieee802154_decipher_data_frame(struct net_if *iface, struct net_buf *buf,
* This will require to look up in nbr cache with short addr * This will require to look up in nbr cache with short addr
* in order to get the extended address related to it * in order to get the extended address related to it
*/ */
if (!ieee802154_decrypt_auth(&ctx->sec_ctx, net_pkt_ll(buf), if (!ieee802154_decrypt_auth(&ctx->sec_ctx, net_pkt_ll(pkt),
net_pkt_ll_reserve(buf), net_pkt_ll_reserve(pkt),
net_buf_frags_len(buf), net_pkt_get_len(pkt),
net_pkt_ll_src(buf)->addr, net_pkt_ll_src(pkt)->addr,
sys_le32_to_cpu( sys_le32_to_cpu(
mpdu->mhr.aux_sec->frame_counter))) { mpdu->mhr.aux_sec->frame_counter))) {
NET_ERR("Could not decipher the frame"); NET_ERR("Could not decipher the frame");
@ -932,7 +933,7 @@ bool ieee802154_decipher_data_frame(struct net_if *iface, struct net_buf *buf,
} }
/* We remove tag size from frag's length, it is now useless */ /* We remove tag size from frag's length, it is now useless */
buf->frags->len -= level_2_tag_size[level]; pkt->frags->len -= level_2_tag_size[level];
return true; return true;
} }

View file

@ -464,32 +464,32 @@ bool ieee802154_create_data_frame(struct ieee802154_context *ctx,
struct net_buf *frag, struct net_buf *frag,
uint8_t reserved_len); uint8_t reserved_len);
struct net_buf * struct net_pkt *
ieee802154_create_mac_cmd_frame(struct ieee802154_context *ctx, ieee802154_create_mac_cmd_frame(struct ieee802154_context *ctx,
enum ieee802154_cfi type, enum ieee802154_cfi type,
struct ieee802154_frame_params *params); struct ieee802154_frame_params *params);
static inline static inline
struct ieee802154_command *ieee802154_get_mac_command(struct net_buf *buf) struct ieee802154_command *ieee802154_get_mac_command(struct net_pkt *pkt)
{ {
return (struct ieee802154_command *)net_pkt_ip_data(buf); return (struct ieee802154_command *)net_pkt_ip_data(pkt);
} }
#ifdef CONFIG_NET_L2_IEEE802154_ACK_REPLY #ifdef CONFIG_NET_L2_IEEE802154_ACK_REPLY
bool ieee802154_create_ack_frame(struct net_if *iface, bool ieee802154_create_ack_frame(struct net_if *iface,
struct net_buf *buf, uint8_t seq); struct net_pkt *pkt, uint8_t seq);
#endif #endif
static inline bool ieee802154_ack_required(struct net_buf *buf) static inline bool ieee802154_ack_required(struct net_pkt *pkt)
{ {
struct ieee802154_fcf_seq *fs = struct ieee802154_fcf_seq *fs =
(struct ieee802154_fcf_seq *)net_pkt_ll(buf); (struct ieee802154_fcf_seq *)net_pkt_ll(pkt);
return fs->fc.ar; return fs->fc.ar;
} }
#ifdef CONFIG_NET_L2_IEEE802154_SECURITY #ifdef CONFIG_NET_L2_IEEE802154_SECURITY
bool ieee802154_decipher_data_frame(struct net_if *iface, struct net_buf *buf, bool ieee802154_decipher_data_frame(struct net_if *iface, struct net_pkt *pkt,
struct ieee802154_mpdu *mpdu); struct ieee802154_mpdu *mpdu);
#else #else
#define ieee802154_decipher_data_frame(...) true #define ieee802154_decipher_data_frame(...) true

View file

@ -87,7 +87,7 @@ static int ieee802154_scan(uint32_t mgmt_request, struct net_if *iface,
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
struct ieee802154_req_params *scan = struct ieee802154_req_params *scan =
(struct ieee802154_req_params *)data; (struct ieee802154_req_params *)data;
struct net_buf *buf = NULL; struct net_pkt *pkt = NULL;
uint8_t channel; uint8_t channel;
int ret; int ret;
@ -105,9 +105,9 @@ static int ieee802154_scan(uint32_t mgmt_request, struct net_if *iface,
params.dst.short_addr = IEEE802154_BROADCAST_ADDRESS; params.dst.short_addr = IEEE802154_BROADCAST_ADDRESS;
params.dst.pan_id = IEEE802154_BROADCAST_PAN_ID; params.dst.pan_id = IEEE802154_BROADCAST_PAN_ID;
buf = ieee802154_create_mac_cmd_frame( pkt = ieee802154_create_mac_cmd_frame(
ctx, IEEE802154_CFI_BEACON_REQUEST, &params); ctx, IEEE802154_CFI_BEACON_REQUEST, &params);
if (!buf) { if (!pkt) {
NET_DBG("Could not create Beacon Request"); NET_DBG("Could not create Beacon Request");
return -ENOBUFS; return -ENOBUFS;
} }
@ -138,14 +138,14 @@ static int ieee802154_scan(uint32_t mgmt_request, struct net_if *iface,
/* Active scan sends a beacon request */ /* Active scan sends a beacon request */
if (mgmt_request == NET_REQUEST_IEEE802154_ACTIVE_SCAN) { if (mgmt_request == NET_REQUEST_IEEE802154_ACTIVE_SCAN) {
net_pkt_ref(buf); net_pkt_ref(pkt);
net_pkt_ref(buf->frags); net_pkt_frag_ref(pkt->frags);
ret = ieee802154_radio_send(iface, buf); ret = ieee802154_radio_send(iface, pkt);
if (ret) { if (ret) {
NET_DBG("Could not send Beacon Request (%d)", NET_DBG("Could not send Beacon Request (%d)",
ret); ret);
net_pkt_unref(buf); net_pkt_unref(pkt);
break; break;
} }
@ -168,8 +168,8 @@ static int ieee802154_scan(uint32_t mgmt_request, struct net_if *iface,
out: out:
ctx->scan_ctx = NULL; ctx->scan_ctx = NULL;
if (buf) { if (pkt) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return ret; return ret;
@ -229,7 +229,7 @@ static int ieee802154_associate(uint32_t mgmt_request, struct net_if *iface,
(struct ieee802154_req_params *)data; (struct ieee802154_req_params *)data;
struct ieee802154_frame_params params; struct ieee802154_frame_params params;
struct ieee802154_command *cmd; struct ieee802154_command *cmd;
struct net_buf *buf; struct net_pkt *pkt;
int ret = 0; int ret = 0;
k_sem_take(&ctx->req_lock, K_FOREVER); k_sem_take(&ctx->req_lock, K_FOREVER);
@ -250,14 +250,14 @@ static int ieee802154_associate(uint32_t mgmt_request, struct net_if *iface,
goto out; goto out;
} }
buf = ieee802154_create_mac_cmd_frame( pkt = ieee802154_create_mac_cmd_frame(
ctx, IEEE802154_CFI_ASSOCIATION_REQUEST, &params); ctx, IEEE802154_CFI_ASSOCIATION_REQUEST, &params);
if (!buf) { if (!pkt) {
ret = -ENOBUFS; ret = -ENOBUFS;
goto out; goto out;
} }
cmd = ieee802154_get_mac_command(buf); cmd = ieee802154_get_mac_command(pkt);
cmd->assoc_req.ci.dev_type = 0; /* RFD */ cmd->assoc_req.ci.dev_type = 0; /* RFD */
cmd->assoc_req.ci.power_src = 0; /* ToDo: set right power source */ cmd->assoc_req.ci.power_src = 0; /* ToDo: set right power source */
cmd->assoc_req.ci.rx_on = 1; /* ToDo: that will depends on PM */ cmd->assoc_req.ci.rx_on = 1; /* ToDo: that will depends on PM */
@ -266,8 +266,8 @@ static int ieee802154_associate(uint32_t mgmt_request, struct net_if *iface,
ctx->associated = false; ctx->associated = false;
if (net_if_send_data(iface, buf)) { if (net_if_send_data(iface, pkt)) {
net_pkt_unref(buf); net_pkt_unref(pkt);
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
@ -305,7 +305,7 @@ static int ieee802154_disassociate(uint32_t mgmt_request, struct net_if *iface,
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
struct ieee802154_frame_params params; struct ieee802154_frame_params params;
struct ieee802154_command *cmd; struct ieee802154_command *cmd;
struct net_buf *buf; struct net_pkt *pkt;
if (!ctx->associated) { if (!ctx->associated) {
return -EALREADY; return -EALREADY;
@ -321,17 +321,17 @@ static int ieee802154_disassociate(uint32_t mgmt_request, struct net_if *iface,
params.pan_id = ctx->pan_id; params.pan_id = ctx->pan_id;
buf = ieee802154_create_mac_cmd_frame( pkt = ieee802154_create_mac_cmd_frame(
ctx, IEEE802154_CFI_DISASSOCIATION_NOTIFICATION, &params); ctx, IEEE802154_CFI_DISASSOCIATION_NOTIFICATION, &params);
if (!buf) { if (!pkt) {
return -ENOBUFS; return -ENOBUFS;
} }
cmd = ieee802154_get_mac_command(buf); cmd = ieee802154_get_mac_command(pkt);
cmd->disassoc_note.reason = IEEE802154_DRF_DEVICE_WISH; cmd->disassoc_note.reason = IEEE802154_DRF_DEVICE_WISH;
if (net_if_send_data(iface, buf)) { if (net_if_send_data(iface, pkt)) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return -EIO; return -EIO;
} }

View file

@ -20,12 +20,12 @@
#include "ieee802154_radio_utils.h" #include "ieee802154_radio_utils.h"
static inline int aloha_tx_fragment(struct net_if *iface, static inline int aloha_tx_fragment(struct net_if *iface,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag) struct net_buf *frag)
{ {
uint8_t retries = CONFIG_NET_L2_IEEE802154_RADIO_TX_RETRIES; uint8_t retries = CONFIG_NET_L2_IEEE802154_RADIO_TX_RETRIES;
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
bool ack_required = prepare_for_ack(ctx, buf); bool ack_required = prepare_for_ack(ctx, pkt);
const struct ieee802154_radio_api *radio = iface->dev->driver_api; const struct ieee802154_radio_api *radio = iface->dev->driver_api;
int ret = -EIO; int ret = -EIO;
@ -34,7 +34,7 @@ static inline int aloha_tx_fragment(struct net_if *iface,
while (retries) { while (retries) {
retries--; retries--;
ret = radio->tx(iface->dev, buf, frag); ret = radio->tx(iface->dev, pkt, frag);
if (ret) { if (ret) {
continue; continue;
} }
@ -48,19 +48,19 @@ static inline int aloha_tx_fragment(struct net_if *iface,
return ret; return ret;
} }
static int aloha_radio_send(struct net_if *iface, struct net_buf *buf) static int aloha_radio_send(struct net_if *iface, struct net_pkt *pkt)
{ {
NET_DBG("buf %p (frags %p)", buf, buf->frags); NET_DBG("pkt %p (frags %p)", pkt, pkt->frags);
return tx_buffer_fragments(iface, buf, aloha_tx_fragment); return tx_packet_fragments(iface, pkt, aloha_tx_fragment);
} }
static enum net_verdict aloha_radio_handle_ack(struct net_if *iface, static enum net_verdict aloha_radio_handle_ack(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
return handle_ack(ctx, buf); return handle_ack(ctx, pkt);
} }
/* Declare the public Radio driver function used by the HW drivers */ /* Declare the public Radio driver function used by the HW drivers */

View file

@ -23,7 +23,7 @@
#include "ieee802154_radio_utils.h" #include "ieee802154_radio_utils.h"
static inline int csma_ca_tx_fragment(struct net_if *iface, static inline int csma_ca_tx_fragment(struct net_if *iface,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag) struct net_buf *frag)
{ {
const uint8_t max_bo = CONFIG_NET_L2_IEEE802154_RADIO_CSMA_CA_MAX_BO; const uint8_t max_bo = CONFIG_NET_L2_IEEE802154_RADIO_CSMA_CA_MAX_BO;
@ -31,7 +31,7 @@ static inline int csma_ca_tx_fragment(struct net_if *iface,
uint8_t retries = CONFIG_NET_L2_IEEE802154_RADIO_TX_RETRIES; uint8_t retries = CONFIG_NET_L2_IEEE802154_RADIO_TX_RETRIES;
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
const struct ieee802154_radio_api *radio = iface->dev->driver_api; const struct ieee802154_radio_api *radio = iface->dev->driver_api;
bool ack_required = prepare_for_ack(ctx, buf); bool ack_required = prepare_for_ack(ctx, pkt);
uint8_t be = CONFIG_NET_L2_IEEE802154_RADIO_CSMA_CA_MIN_BE; uint8_t be = CONFIG_NET_L2_IEEE802154_RADIO_CSMA_CA_MIN_BE;
uint8_t nb = 0; uint8_t nb = 0;
int ret = -EIO; int ret = -EIO;
@ -61,7 +61,7 @@ loop:
} }
} }
ret = radio->tx(iface->dev, buf, frag); ret = radio->tx(iface->dev, pkt, frag);
if (ret) { if (ret) {
continue; continue;
} }
@ -75,19 +75,19 @@ loop:
return ret; return ret;
} }
static int csma_ca_radio_send(struct net_if *iface, struct net_buf *buf) static int csma_ca_radio_send(struct net_if *iface, struct net_pkt *pkt)
{ {
NET_DBG("buf %p (frags %p)", buf, buf->frags); NET_DBG("pkt %p (frags %p)", pkt, pkt->frags);
return tx_buffer_fragments(iface, buf, csma_ca_tx_fragment); return tx_packet_fragments(iface, pkt, csma_ca_tx_fragment);
} }
static enum net_verdict csma_ca_radio_handle_ack(struct net_if *iface, static enum net_verdict csma_ca_radio_handle_ack(struct net_if *iface,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_context *ctx = net_if_l2_data(iface);
return handle_ack(ctx, buf); return handle_ack(ctx, pkt);
} }
/* Declare the public Radio driver function used by the HW drivers */ /* Declare the public Radio driver function used by the HW drivers */

View file

@ -8,13 +8,13 @@
#define __IEEE802154_RADIO_UTILS_H__ #define __IEEE802154_RADIO_UTILS_H__
typedef int (ieee802154_radio_tx_frag_t)(struct net_if *iface, typedef int (ieee802154_radio_tx_frag_t)(struct net_if *iface,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *frag); struct net_buf *frag);
static inline bool prepare_for_ack(struct ieee802154_context *ctx, static inline bool prepare_for_ack(struct ieee802154_context *ctx,
struct net_buf *buf) struct net_pkt *pkt)
{ {
if (ieee802154_ack_required(buf)) { if (ieee802154_ack_required(pkt)) {
ctx->ack_received = false; ctx->ack_received = false;
k_sem_init(&ctx->ack_lock, 0, UINT_MAX); k_sem_init(&ctx->ack_lock, 0, UINT_MAX);
@ -43,9 +43,9 @@ static inline int wait_for_ack(struct ieee802154_context *ctx,
} }
static inline int handle_ack(struct ieee802154_context *ctx, static inline int handle_ack(struct ieee802154_context *ctx,
struct net_buf *buf) struct net_pkt *pkt)
{ {
if (buf->len == IEEE802154_ACK_PKT_LENGTH) { if (pkt->frags->len == IEEE802154_ACK_PKT_LENGTH) {
ctx->ack_received = true; ctx->ack_received = true;
k_sem_give(&ctx->ack_lock); k_sem_give(&ctx->ack_lock);
@ -55,16 +55,16 @@ static inline int handle_ack(struct ieee802154_context *ctx,
return NET_CONTINUE; return NET_CONTINUE;
} }
static inline int tx_buffer_fragments(struct net_if *iface, static inline int tx_packet_fragments(struct net_if *iface,
struct net_buf *buf, struct net_pkt *pkt,
ieee802154_radio_tx_frag_t *tx_func) ieee802154_radio_tx_frag_t *tx_func)
{ {
int ret = 0; int ret = 0;
struct net_buf *frag; struct net_buf *frag;
frag = buf->frags; frag = pkt->frags;
while (frag) { while (frag) {
ret = tx_func(iface, buf, frag); ret = tx_func(iface, pkt, frag);
if (ret) { if (ret) {
break; break;
} }
@ -73,7 +73,7 @@ static inline int tx_buffer_fragments(struct net_if *iface,
} }
if (!ret) { if (!ret) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return ret; return ret;

View file

@ -43,21 +43,21 @@
*/ */
#define NET_CONN_CB(name) \ #define NET_CONN_CB(name) \
static enum net_verdict _##name(struct net_conn *conn, \ static enum net_verdict _##name(struct net_conn *conn, \
struct net_buf *buf, \ struct net_pkt *pkt, \
void *user_data); \ void *user_data); \
static enum net_verdict name(struct net_conn *conn, \ static enum net_verdict name(struct net_conn *conn, \
struct net_buf *buf, \ struct net_pkt *pkt, \
void *user_data) \ void *user_data) \
{ \ { \
enum net_verdict result; \ enum net_verdict result; \
\ \
net_context_ref(user_data); \ net_context_ref(user_data); \
result = _##name(conn, buf, user_data); \ result = _##name(conn, pkt, user_data); \
net_context_unref(user_data); \ net_context_unref(user_data); \
return result; \ return result; \
} \ } \
static enum net_verdict _##name(struct net_conn *conn, \ static enum net_verdict _##name(struct net_conn *conn, \
struct net_buf *buf, \ struct net_pkt *pkt, \
void *user_data) \ void *user_data) \
@ -69,29 +69,29 @@ static struct net_context contexts[NET_MAX_CONTEXT];
static struct k_sem contexts_lock; static struct k_sem contexts_lock;
static enum net_verdict packet_received(struct net_conn *conn, static enum net_verdict packet_received(struct net_conn *conn,
struct net_buf *buf, struct net_pkt *pkt,
void *user_data); void *user_data);
static void set_appdata_values(struct net_buf *buf, enum net_ip_protocol proto); static void set_appdata_values(struct net_pkt *pkt, enum net_ip_protocol proto);
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
static struct sockaddr *create_sockaddr(struct net_buf *buf, static struct sockaddr *create_sockaddr(struct net_pkt *pkt,
struct sockaddr *addr) struct sockaddr *addr)
{ {
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
net_ipaddr_copy(&net_sin6(addr)->sin6_addr, net_ipaddr_copy(&net_sin6(addr)->sin6_addr,
&NET_IPV6_BUF(buf)->src); &NET_IPV6_BUF(pkt)->src);
net_sin6(addr)->sin6_port = NET_TCP_BUF(buf)->src_port; net_sin6(addr)->sin6_port = NET_TCP_BUF(pkt)->src_port;
net_sin6(addr)->sin6_family = AF_INET6; net_sin6(addr)->sin6_family = AF_INET6;
} else } else
#endif #endif
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
net_ipaddr_copy(&net_sin(addr)->sin_addr, net_ipaddr_copy(&net_sin(addr)->sin_addr,
&NET_IPV4_BUF(buf)->src); &NET_IPV4_BUF(pkt)->src);
net_sin(addr)->sin_port = NET_TCP_BUF(buf)->src_port; net_sin(addr)->sin_port = NET_TCP_BUF(pkt)->src_port;
net_sin(addr)->sin_family = AF_INET; net_sin(addr)->sin_family = AF_INET;
} else } else
#endif #endif
@ -344,20 +344,20 @@ int net_context_get(sa_family_t family,
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
static void queue_fin(struct net_context *ctx) static void queue_fin(struct net_context *ctx)
{ {
struct net_buf *buf = NULL; struct net_pkt *pkt = NULL;
int ret; int ret;
ret = net_tcp_prepare_segment(ctx->tcp, NET_TCP_FIN, NULL, 0, ret = net_tcp_prepare_segment(ctx->tcp, NET_TCP_FIN, NULL, 0,
NULL, &ctx->remote, &buf); NULL, &ctx->remote, &pkt);
if (ret || !buf) { if (ret || !pkt) {
return; return;
} }
ctx->tcp->fin_queued = 1; ctx->tcp->fin_queued = 1;
ret = net_tcp_send_buf(buf); ret = net_tcp_send_pkt(pkt);
if (ret < 0) { if (ret < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} }
@ -663,28 +663,28 @@ int net_context_listen(struct net_context *context, int backlog)
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
#if defined(CONFIG_NET_DEBUG_CONTEXT) #if defined(CONFIG_NET_DEBUG_CONTEXT)
#define net_tcp_print_recv_info(str, buf, port) \ #define net_tcp_print_recv_info(str, pkt, port) \
do { \ do { \
if (net_context_get_family(context) == AF_INET6) { \ if (net_context_get_family(context) == AF_INET6) { \
NET_DBG("%s received from %s port %d", str, \ NET_DBG("%s received from %s port %d", str, \
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src),\ net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src),\
ntohs(port)); \ ntohs(port)); \
} else if (net_context_get_family(context) == AF_INET) {\ } else if (net_context_get_family(context) == AF_INET) {\
NET_DBG("%s received from %s port %d", str, \ NET_DBG("%s received from %s port %d", str, \
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src),\ net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src),\
ntohs(port)); \ ntohs(port)); \
} \ } \
} while (0) } while (0)
#define net_tcp_print_send_info(str, buf, port) \ #define net_tcp_print_send_info(str, pkt, port) \
do { \ do { \
if (net_context_get_family(context) == AF_INET6) { \ if (net_context_get_family(context) == AF_INET6) { \
NET_DBG("%s sent to %s port %d", str, \ NET_DBG("%s sent to %s port %d", str, \
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst),\ net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst),\
ntohs(port)); \ ntohs(port)); \
} else if (net_context_get_family(context) == AF_INET) {\ } else if (net_context_get_family(context) == AF_INET) {\
NET_DBG("%s sent to %s port %d", str, \ NET_DBG("%s sent to %s port %d", str, \
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst),\ net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst),\
ntohs(port)); \ ntohs(port)); \
} \ } \
} while (0) } while (0)
@ -698,21 +698,21 @@ static inline int send_control_segment(struct net_context *context,
const struct sockaddr *remote, const struct sockaddr *remote,
int flags, const char *msg) int flags, const char *msg)
{ {
struct net_buf *buf = NULL; struct net_pkt *pkt = NULL;
int ret; int ret;
ret = net_tcp_prepare_segment(context->tcp, flags, NULL, 0, ret = net_tcp_prepare_segment(context->tcp, flags, NULL, 0,
local, remote, &buf); local, remote, &pkt);
if (ret) { if (ret) {
return ret; return ret;
} }
ret = net_send_data(buf); ret = net_send_data(pkt);
if (ret < 0) { if (ret < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
net_tcp_print_send_info(msg, buf, NET_TCP_BUF(buf)->dst_port); net_tcp_print_send_info(msg, pkt, NET_TCP_BUF(pkt)->dst_port);
return ret; return ret;
} }
@ -737,7 +737,7 @@ static inline int send_syn_ack(struct net_context *context,
static inline int send_ack(struct net_context *context, static inline int send_ack(struct net_context *context,
struct sockaddr *remote) struct sockaddr *remote)
{ {
struct net_buf *buf = NULL; struct net_pkt *pkt = NULL;
int ret; int ret;
/* Something (e.g. a data transmission under the user /* Something (e.g. a data transmission under the user
@ -747,16 +747,16 @@ static inline int send_ack(struct net_context *context,
return 0; return 0;
} }
ret = net_tcp_prepare_ack(context->tcp, remote, &buf); ret = net_tcp_prepare_ack(context->tcp, remote, &pkt);
if (ret) { if (ret) {
return ret; return ret;
} }
net_tcp_print_send_info("ACK", buf, NET_TCP_BUF(buf)->dst_port); net_tcp_print_send_info("ACK", pkt, NET_TCP_BUF(pkt)->dst_port);
ret = net_tcp_send_buf(buf); ret = net_tcp_send_pkt(pkt);
if (ret < 0) { if (ret < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return ret; return ret;
@ -765,28 +765,28 @@ static inline int send_ack(struct net_context *context,
static int send_reset(struct net_context *context, static int send_reset(struct net_context *context,
struct sockaddr *remote) struct sockaddr *remote)
{ {
struct net_buf *buf = NULL; struct net_pkt *pkt = NULL;
int ret; int ret;
ret = net_tcp_prepare_reset(context->tcp, remote, &buf); ret = net_tcp_prepare_reset(context->tcp, remote, &pkt);
if (ret) { if (ret) {
return ret; return ret;
} }
net_tcp_print_send_info("RST", buf, NET_TCP_BUF(buf)->dst_port); net_tcp_print_send_info("RST", pkt, NET_TCP_BUF(pkt)->dst_port);
ret = net_send_data(buf); ret = net_send_data(pkt);
if (ret < 0) { if (ret < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return ret; return ret;
} }
static int tcp_hdr_len(struct net_buf *buf) static int tcp_hdr_len(struct net_pkt *pkt)
{ {
/* "Offset": 4-bit field in high nibble, units of dwords */ /* "Offset": 4-bit field in high nibble, units of dwords */
struct net_tcp_hdr *hdr = (void *)net_pkt_tcp_data(buf); struct net_tcp_hdr *hdr = (void *)net_pkt_tcp_data(pkt);
return 4 * (hdr->offset >> 4); return 4 * (hdr->offset >> 4);
} }
@ -808,15 +808,15 @@ NET_CONN_CB(tcp_established)
return NET_DROP; return NET_DROP;
} }
net_tcp_print_recv_info("DATA", buf, NET_TCP_BUF(buf)->src_port); net_tcp_print_recv_info("DATA", pkt, NET_TCP_BUF(pkt)->src_port);
tcp_flags = NET_TCP_FLAGS(buf); tcp_flags = NET_TCP_FLAGS(pkt);
if (tcp_flags & NET_TCP_ACK) { if (tcp_flags & NET_TCP_ACK) {
net_tcp_ack_received(context, net_tcp_ack_received(context,
sys_get_be32(NET_TCP_BUF(buf)->ack)); sys_get_be32(NET_TCP_BUF(pkt)->ack));
} }
if (sys_get_be32(NET_TCP_BUF(buf)->seq) - context->tcp->send_ack) { if (sys_get_be32(NET_TCP_BUF(pkt)->seq) - context->tcp->send_ack) {
/* Don't try to reorder packets. If it doesn't /* Don't try to reorder packets. If it doesn't
* match the next segment exactly, drop and wait for * match the next segment exactly, drop and wait for
* retransmit * retransmit
@ -824,10 +824,10 @@ NET_CONN_CB(tcp_established)
return NET_DROP; return NET_DROP;
} }
set_appdata_values(buf, IPPROTO_TCP); set_appdata_values(pkt, IPPROTO_TCP);
context->tcp->send_ack += net_pkt_appdatalen(buf); context->tcp->send_ack += net_pkt_appdatalen(pkt);
ret = packet_received(conn, buf, context->tcp->recv_user_data); ret = packet_received(conn, pkt, context->tcp->recv_user_data);
if (tcp_flags & NET_TCP_FIN) { if (tcp_flags & NET_TCP_FIN) {
/* Sending an ACK in the CLOSE_WAIT state will transition to /* Sending an ACK in the CLOSE_WAIT state will transition to
@ -868,7 +868,7 @@ NET_CONN_CB(tcp_synack_received)
switch (net_tcp_get_state(context->tcp)) { switch (net_tcp_get_state(context->tcp)) {
case NET_TCP_SYN_SENT: case NET_TCP_SYN_SENT:
net_context_set_iface(context, net_pkt_iface(buf)); net_context_set_iface(context, net_pkt_iface(pkt));
break; break;
default: default:
NET_DBG("Context %p in wrong state %d", NET_DBG("Context %p in wrong state %d",
@ -876,11 +876,11 @@ NET_CONN_CB(tcp_synack_received)
return NET_DROP; return NET_DROP;
} }
net_pkt_set_context(buf, context); net_pkt_set_context(pkt, context);
NET_ASSERT(net_pkt_iface(buf)); NET_ASSERT(net_pkt_iface(pkt));
if (NET_TCP_FLAGS(buf) & NET_TCP_RST) { if (NET_TCP_FLAGS(pkt) & NET_TCP_RST) {
if (context->connect_cb) { if (context->connect_cb) {
context->connect_cb(context, -ECONNREFUSED, context->connect_cb(context, -ECONNREFUSED,
context->user_data); context->user_data);
@ -889,15 +889,15 @@ NET_CONN_CB(tcp_synack_received)
return NET_DROP; return NET_DROP;
} }
if (NET_TCP_FLAGS(buf) & NET_TCP_SYN) { if (NET_TCP_FLAGS(pkt) & NET_TCP_SYN) {
context->tcp->send_ack = context->tcp->send_ack =
sys_get_be32(NET_TCP_BUF(buf)->seq) + 1; sys_get_be32(NET_TCP_BUF(pkt)->seq) + 1;
context->tcp->recv_max_ack = context->tcp->send_seq + 1; context->tcp->recv_max_ack = context->tcp->send_seq + 1;
} }
/* /*
* If we receive SYN, we send SYN-ACK and go to SYN_RCVD state. * If we receive SYN, we send SYN-ACK and go to SYN_RCVD state.
*/ */
if (NET_TCP_FLAGS(buf) == (NET_TCP_SYN | NET_TCP_ACK)) { if (NET_TCP_FLAGS(pkt) == (NET_TCP_SYN | NET_TCP_ACK)) {
struct sockaddr *laddr; struct sockaddr *laddr;
struct sockaddr *raddr; struct sockaddr *raddr;
@ -911,39 +911,39 @@ NET_CONN_CB(tcp_synack_received)
#endif #endif
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
laddr = (struct sockaddr *)&l6addr; laddr = (struct sockaddr *)&l6addr;
raddr = (struct sockaddr *)&r6addr; raddr = (struct sockaddr *)&r6addr;
r6addr.sin6_family = AF_INET6; r6addr.sin6_family = AF_INET6;
r6addr.sin6_port = NET_TCP_BUF(buf)->src_port; r6addr.sin6_port = NET_TCP_BUF(pkt)->src_port;
net_ipaddr_copy(&r6addr.sin6_addr, net_ipaddr_copy(&r6addr.sin6_addr,
&NET_IPV6_BUF(buf)->src); &NET_IPV6_BUF(pkt)->src);
l6addr.sin6_family = AF_INET6; l6addr.sin6_family = AF_INET6;
l6addr.sin6_port = NET_TCP_BUF(buf)->dst_port; l6addr.sin6_port = NET_TCP_BUF(pkt)->dst_port;
net_ipaddr_copy(&l6addr.sin6_addr, net_ipaddr_copy(&l6addr.sin6_addr,
&NET_IPV6_BUF(buf)->dst); &NET_IPV6_BUF(pkt)->dst);
} else } else
#endif #endif
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
laddr = (struct sockaddr *)&l4addr; laddr = (struct sockaddr *)&l4addr;
raddr = (struct sockaddr *)&r4addr; raddr = (struct sockaddr *)&r4addr;
r4addr.sin_family = AF_INET; r4addr.sin_family = AF_INET;
r4addr.sin_port = NET_TCP_BUF(buf)->src_port; r4addr.sin_port = NET_TCP_BUF(pkt)->src_port;
net_ipaddr_copy(&r4addr.sin_addr, net_ipaddr_copy(&r4addr.sin_addr,
&NET_IPV4_BUF(buf)->src); &NET_IPV4_BUF(pkt)->src);
l4addr.sin_family = AF_INET; l4addr.sin_family = AF_INET;
l4addr.sin_port = NET_TCP_BUF(buf)->dst_port; l4addr.sin_port = NET_TCP_BUF(pkt)->dst_port;
net_ipaddr_copy(&l4addr.sin_addr, net_ipaddr_copy(&l4addr.sin_addr,
&NET_IPV4_BUF(buf)->dst); &NET_IPV4_BUF(pkt)->dst);
} else } else
#endif #endif
{ {
NET_DBG("Invalid family (%d)", net_pkt_family(buf)); NET_DBG("Invalid family (%d)", net_pkt_family(pkt));
return NET_DROP; return NET_DROP;
} }
@ -954,8 +954,8 @@ NET_CONN_CB(tcp_synack_received)
ret = net_tcp_register(raddr, ret = net_tcp_register(raddr,
laddr, laddr,
ntohs(NET_TCP_BUF(buf)->src_port), ntohs(NET_TCP_BUF(pkt)->src_port),
ntohs(NET_TCP_BUF(buf)->dst_port), ntohs(NET_TCP_BUF(pkt)->dst_port),
tcp_established, tcp_established,
context, context,
&context->conn_handler); &context->conn_handler);
@ -1185,7 +1185,7 @@ static void ack_timeout(struct k_work *work)
net_tcp_change_state(tcp, NET_TCP_LISTEN); net_tcp_change_state(tcp, NET_TCP_LISTEN);
} }
static void buf_get_sockaddr(sa_family_t family, struct net_buf *buf, static void pkt_get_sockaddr(sa_family_t family, struct net_pkt *pkt,
struct sockaddr_ptr *addr) struct sockaddr_ptr *addr)
{ {
memset(addr, 0, sizeof(*addr)); memset(addr, 0, sizeof(*addr));
@ -1195,8 +1195,8 @@ static void buf_get_sockaddr(sa_family_t family, struct net_buf *buf,
struct sockaddr_in_ptr *addr4 = net_sin_ptr(addr); struct sockaddr_in_ptr *addr4 = net_sin_ptr(addr);
addr4->sin_family = AF_INET; addr4->sin_family = AF_INET;
addr4->sin_port = NET_TCP_BUF(buf)->dst_port; addr4->sin_port = NET_TCP_BUF(pkt)->dst_port;
addr4->sin_addr = &NET_IPV4_BUF(buf)->dst; addr4->sin_addr = &NET_IPV4_BUF(pkt)->dst;
} }
#endif #endif
@ -1205,8 +1205,8 @@ static void buf_get_sockaddr(sa_family_t family, struct net_buf *buf,
struct sockaddr_in6_ptr *addr6 = net_sin6_ptr(addr); struct sockaddr_in6_ptr *addr6 = net_sin6_ptr(addr);
addr6->sin6_family = AF_INET6; addr6->sin6_family = AF_INET6;
addr6->sin6_port = NET_TCP_BUF(buf)->dst_port; addr6->sin6_port = NET_TCP_BUF(pkt)->dst_port;
addr6->sin6_addr = &NET_IPV6_BUF(buf)->dst; addr6->sin6_addr = &NET_IPV6_BUF(pkt)->dst;
} }
#endif #endif
} }
@ -1215,7 +1215,7 @@ static void buf_get_sockaddr(sa_family_t family, struct net_buf *buf,
static inline void copy_pool_vars(struct net_context *new_context, static inline void copy_pool_vars(struct net_context *new_context,
struct net_context *listen_context) struct net_context *listen_context)
{ {
new_context->tx_pool = listen_context->tx_pool; new_context->tx_slab = listen_context->tx_slab;
new_context->data_pool = listen_context->data_pool; new_context->data_pool = listen_context->data_pool;
} }
#else #else
@ -1231,7 +1231,7 @@ NET_CONN_CB(tcp_syn_rcvd)
{ {
struct net_context *context = (struct net_context *)user_data; struct net_context *context = (struct net_context *)user_data;
struct net_tcp *tcp; struct net_tcp *tcp;
struct sockaddr_ptr buf_src_addr; struct sockaddr_ptr pkt_src_addr;
NET_ASSERT(context && context->tcp); NET_ASSERT(context && context->tcp);
@ -1239,10 +1239,10 @@ NET_CONN_CB(tcp_syn_rcvd)
switch (net_tcp_get_state(tcp)) { switch (net_tcp_get_state(tcp)) {
case NET_TCP_LISTEN: case NET_TCP_LISTEN:
net_context_set_iface(context, net_pkt_iface(buf)); net_context_set_iface(context, net_pkt_iface(pkt));
break; break;
case NET_TCP_SYN_RCVD: case NET_TCP_SYN_RCVD:
if (net_pkt_iface(buf) != net_context_get_iface(context)) { if (net_pkt_iface(pkt) != net_context_get_iface(context)) {
return NET_DROP; return NET_DROP;
} }
break; break;
@ -1252,30 +1252,30 @@ NET_CONN_CB(tcp_syn_rcvd)
return NET_DROP; return NET_DROP;
} }
net_pkt_set_context(buf, context); net_pkt_set_context(pkt, context);
NET_ASSERT(net_pkt_iface(buf)); NET_ASSERT(net_pkt_iface(pkt));
/* /*
* If we receive SYN, we send SYN-ACK and go to SYN_RCVD state. * If we receive SYN, we send SYN-ACK and go to SYN_RCVD state.
*/ */
if (NET_TCP_FLAGS(buf) == NET_TCP_SYN) { if (NET_TCP_FLAGS(pkt) == NET_TCP_SYN) {
struct sockaddr peer, *remote; struct sockaddr peer, *remote;
net_tcp_print_recv_info("SYN", buf, NET_TCP_BUF(buf)->src_port); net_tcp_print_recv_info("SYN", pkt, NET_TCP_BUF(pkt)->src_port);
net_tcp_change_state(tcp, NET_TCP_SYN_RCVD); net_tcp_change_state(tcp, NET_TCP_SYN_RCVD);
remote = create_sockaddr(buf, &peer); remote = create_sockaddr(pkt, &peer);
/* FIXME: Is this the correct place to set tcp->send_ack? */ /* FIXME: Is this the correct place to set tcp->send_ack? */
context->tcp->send_ack = context->tcp->send_ack =
sys_get_be32(NET_TCP_BUF(buf)->seq) + 1; sys_get_be32(NET_TCP_BUF(pkt)->seq) + 1;
context->tcp->recv_max_ack = context->tcp->send_seq + 1; context->tcp->recv_max_ack = context->tcp->send_seq + 1;
buf_get_sockaddr(net_context_get_family(context), pkt_get_sockaddr(net_context_get_family(context),
buf, &buf_src_addr); pkt, &pkt_src_addr);
send_syn_ack(context, &buf_src_addr, remote); send_syn_ack(context, &pkt_src_addr, remote);
/* We might be entering this section multiple times /* We might be entering this section multiple times
* if the SYN is sent more than once. So we need to cancel * if the SYN is sent more than once. So we need to cancel
@ -1291,10 +1291,10 @@ NET_CONN_CB(tcp_syn_rcvd)
/* /*
* If we receive RST, we go back to LISTEN state. * If we receive RST, we go back to LISTEN state.
*/ */
if (NET_TCP_FLAGS(buf) == NET_TCP_RST) { if (NET_TCP_FLAGS(pkt) == NET_TCP_RST) {
k_delayed_work_cancel(&tcp->ack_timer); k_delayed_work_cancel(&tcp->ack_timer);
net_tcp_print_recv_info("RST", buf, NET_TCP_BUF(buf)->src_port); net_tcp_print_recv_info("RST", pkt, NET_TCP_BUF(pkt)->src_port);
net_tcp_change_state(tcp, NET_TCP_LISTEN); net_tcp_change_state(tcp, NET_TCP_LISTEN);
@ -1304,7 +1304,7 @@ NET_CONN_CB(tcp_syn_rcvd)
/* /*
* If we receive ACK, we go to ESTABLISHED state. * If we receive ACK, we go to ESTABLISHED state.
*/ */
if (NET_TCP_FLAGS(buf) == NET_TCP_ACK) { if (NET_TCP_FLAGS(pkt) == NET_TCP_ACK) {
struct net_context *new_context; struct net_context *new_context;
struct sockaddr local_addr; struct sockaddr local_addr;
struct sockaddr remote_addr; struct sockaddr remote_addr;
@ -1321,7 +1321,7 @@ NET_CONN_CB(tcp_syn_rcvd)
goto reset; goto reset;
} }
net_tcp_print_recv_info("ACK", buf, NET_TCP_BUF(buf)->src_port); net_tcp_print_recv_info("ACK", pkt, NET_TCP_BUF(pkt)->src_port);
if (!context->tcp->accept_cb) { if (!context->tcp->accept_cb) {
NET_DBG("No accept callback, connection reset."); NET_DBG("No accept callback, connection reset.");
@ -1330,7 +1330,7 @@ NET_CONN_CB(tcp_syn_rcvd)
/* We create a new context that starts to wait data. /* We create a new context that starts to wait data.
*/ */
ret = net_context_get(net_pkt_family(buf), ret = net_context_get(net_pkt_family(pkt),
SOCK_STREAM, IPPROTO_TCP, SOCK_STREAM, IPPROTO_TCP,
&new_context); &new_context);
if (ret < 0) { if (ret < 0) {
@ -1353,13 +1353,13 @@ NET_CONN_CB(tcp_syn_rcvd)
remote_addr6->sin6_family = AF_INET6; remote_addr6->sin6_family = AF_INET6;
local_addr6->sin6_family = AF_INET6; local_addr6->sin6_family = AF_INET6;
local_addr6->sin6_port = NET_TCP_BUF(buf)->dst_port; local_addr6->sin6_port = NET_TCP_BUF(pkt)->dst_port;
remote_addr6->sin6_port = NET_TCP_BUF(buf)->src_port; remote_addr6->sin6_port = NET_TCP_BUF(pkt)->src_port;
net_ipaddr_copy(&local_addr6->sin6_addr, net_ipaddr_copy(&local_addr6->sin6_addr,
&NET_IPV6_BUF(buf)->dst); &NET_IPV6_BUF(pkt)->dst);
net_ipaddr_copy(&remote_addr6->sin6_addr, net_ipaddr_copy(&remote_addr6->sin6_addr,
&NET_IPV6_BUF(buf)->src); &NET_IPV6_BUF(pkt)->src);
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
} else } else
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
@ -1374,13 +1374,13 @@ NET_CONN_CB(tcp_syn_rcvd)
remote_addr4->sin_family = AF_INET; remote_addr4->sin_family = AF_INET;
local_addr4->sin_family = AF_INET; local_addr4->sin_family = AF_INET;
local_addr4->sin_port = NET_TCP_BUF(buf)->dst_port; local_addr4->sin_port = NET_TCP_BUF(pkt)->dst_port;
remote_addr4->sin_port = NET_TCP_BUF(buf)->src_port; remote_addr4->sin_port = NET_TCP_BUF(pkt)->src_port;
net_ipaddr_copy(&local_addr4->sin_addr, net_ipaddr_copy(&local_addr4->sin_addr,
&NET_IPV4_BUF(buf)->dst); &NET_IPV4_BUF(pkt)->dst);
net_ipaddr_copy(&remote_addr4->sin_addr, net_ipaddr_copy(&remote_addr4->sin_addr,
&NET_IPV4_BUF(buf)->src); &NET_IPV4_BUF(pkt)->src);
addrlen = sizeof(struct sockaddr_in); addrlen = sizeof(struct sockaddr_in);
} else } else
#endif /* CONFIG_NET_IPV4 */ #endif /* CONFIG_NET_IPV4 */
@ -1453,7 +1453,7 @@ reset:
{ {
struct sockaddr peer; struct sockaddr peer;
send_reset(tcp->context, create_sockaddr(buf, &peer)); send_reset(tcp->context, create_sockaddr(pkt, &peer));
} }
return NET_DROP; return NET_DROP;
@ -1562,7 +1562,7 @@ int net_context_accept(struct net_context *context,
} }
static int send_data(struct net_context *context, static int send_data(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
net_context_send_cb_t cb, net_context_send_cb_t cb,
int32_t timeout, int32_t timeout,
void *token, void *token,
@ -1570,10 +1570,10 @@ static int send_data(struct net_context *context,
{ {
context->send_cb = cb; context->send_cb = cb;
context->user_data = user_data; context->user_data = user_data;
net_pkt_set_token(buf, token); net_pkt_set_token(pkt, token);
if (net_context_get_ip_proto(context) == IPPROTO_UDP) { if (net_context_get_ip_proto(context) == IPPROTO_UDP) {
return net_send_data(buf); return net_send_data(pkt);
} }
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
@ -1599,42 +1599,42 @@ static int send_data(struct net_context *context,
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
static int create_udp_packet(struct net_context *context, static int create_udp_packet(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
const struct sockaddr *dst_addr, const struct sockaddr *dst_addr,
struct net_buf **out_buf) struct net_pkt **out_pkt)
{ {
int r = 0; int r = 0;
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)dst_addr; struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)dst_addr;
buf = net_ipv6_create(context, buf, NULL, &addr6->sin6_addr); pkt = net_ipv6_create(context, pkt, NULL, &addr6->sin6_addr);
buf = net_udp_append(context, buf, ntohs(addr6->sin6_port)); pkt = net_udp_append(context, pkt, ntohs(addr6->sin6_port));
r = net_ipv6_finalize(context, buf); r = net_ipv6_finalize(context, pkt);
} else } else
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)dst_addr; struct sockaddr_in *addr4 = (struct sockaddr_in *)dst_addr;
buf = net_ipv4_create(context, buf, NULL, &addr4->sin_addr); pkt = net_ipv4_create(context, pkt, NULL, &addr4->sin_addr);
buf = net_udp_append(context, buf, ntohs(addr4->sin_port)); pkt = net_udp_append(context, pkt, ntohs(addr4->sin_port));
r = net_ipv4_finalize(context, buf); r = net_ipv4_finalize(context, pkt);
} else } else
#endif /* CONFIG_NET_IPV4 */ #endif /* CONFIG_NET_IPV4 */
{ {
return -EPROTONOSUPPORT; return -EPROTONOSUPPORT;
} }
*out_buf = buf; *out_pkt = pkt;
return r; return r;
} }
#endif /* CONFIG_NET_UDP */ #endif /* CONFIG_NET_UDP */
static int sendto(struct net_buf *buf, static int sendto(struct net_pkt *pkt,
const struct sockaddr *dst_addr, const struct sockaddr *dst_addr,
socklen_t addrlen, socklen_t addrlen,
net_context_send_cb_t cb, net_context_send_cb_t cb,
@ -1642,7 +1642,7 @@ static int sendto(struct net_buf *buf,
void *token, void *token,
void *user_data) void *user_data)
{ {
struct net_context *context = net_pkt_context(buf); struct net_context *context = net_pkt_context(pkt);
int ret; int ret;
if (!net_context_is_used(context)) { if (!net_context_is_used(context)) {
@ -1667,16 +1667,16 @@ static int sendto(struct net_buf *buf,
} }
#if defined(CONFIG_NET_OFFLOAD) #if defined(CONFIG_NET_OFFLOAD)
if (net_if_is_ip_offloaded(net_pkt_iface(buf))) { if (net_if_is_ip_offloaded(net_pkt_iface(pkt))) {
return net_offload_sendto( return net_offload_sendto(
net_pkt_iface(buf), net_pkt_iface(pkt),
buf, dst_addr, addrlen, pkt, dst_addr, addrlen,
cb, timeout, token, user_data); cb, timeout, token, user_data);
} }
#endif /* CONFIG_NET_OFFLOAD */ #endif /* CONFIG_NET_OFFLOAD */
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)dst_addr; struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)dst_addr;
if (addrlen < sizeof(struct sockaddr_in6)) { if (addrlen < sizeof(struct sockaddr_in6)) {
@ -1690,7 +1690,7 @@ static int sendto(struct net_buf *buf,
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
struct sockaddr_in *addr4 = (struct sockaddr_in *)dst_addr; struct sockaddr_in *addr4 = (struct sockaddr_in *)dst_addr;
if (addrlen < sizeof(struct sockaddr_in)) { if (addrlen < sizeof(struct sockaddr_in)) {
@ -1703,19 +1703,19 @@ static int sendto(struct net_buf *buf,
} else } else
#endif /* CONFIG_NET_IPV4 */ #endif /* CONFIG_NET_IPV4 */
{ {
NET_DBG("Invalid protocol family %d", net_pkt_family(buf)); NET_DBG("Invalid protocol family %d", net_pkt_family(pkt));
return -EINVAL; return -EINVAL;
} }
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
if (net_context_get_ip_proto(context) == IPPROTO_UDP) { if (net_context_get_ip_proto(context) == IPPROTO_UDP) {
ret = create_udp_packet(context, buf, dst_addr, &buf); ret = create_udp_packet(context, pkt, dst_addr, &pkt);
} else } else
#endif /* CONFIG_NET_UDP */ #endif /* CONFIG_NET_UDP */
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
if (net_context_get_ip_proto(context) == IPPROTO_TCP) { if (net_context_get_ip_proto(context) == IPPROTO_TCP) {
ret = net_tcp_queue_data(context, buf); ret = net_tcp_queue_data(context, pkt);
} else } else
#endif /* CONFIG_NET_TCP */ #endif /* CONFIG_NET_TCP */
{ {
@ -1729,25 +1729,25 @@ static int sendto(struct net_buf *buf,
return ret; return ret;
} }
return send_data(context, buf, cb, timeout, token, user_data); return send_data(context, pkt, cb, timeout, token, user_data);
} }
int net_context_send(struct net_buf *buf, int net_context_send(struct net_pkt *pkt,
net_context_send_cb_t cb, net_context_send_cb_t cb,
int32_t timeout, int32_t timeout,
void *token, void *token,
void *user_data) void *user_data)
{ {
struct net_context *context = net_pkt_context(buf); struct net_context *context = net_pkt_context(pkt);
socklen_t addrlen; socklen_t addrlen;
NET_ASSERT(PART_OF_ARRAY(contexts, context)); NET_ASSERT(PART_OF_ARRAY(contexts, context));
#if defined(CONFIG_NET_OFFLOAD) #if defined(CONFIG_NET_OFFLOAD)
if (net_if_is_ip_offloaded(net_pkt_iface(buf))) { if (net_if_is_ip_offloaded(net_pkt_iface(pkt))) {
return net_offload_send( return net_offload_send(
net_pkt_iface(buf), net_pkt_iface(pkt),
buf, cb, timeout, pkt, cb, timeout,
token, user_data); token, user_data);
} }
#endif /* CONFIG_NET_OFFLOAD */ #endif /* CONFIG_NET_OFFLOAD */
@ -1758,13 +1758,13 @@ int net_context_send(struct net_buf *buf,
} }
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
addrlen = sizeof(struct sockaddr_in6); addrlen = sizeof(struct sockaddr_in6);
} else } else
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
addrlen = sizeof(struct sockaddr_in); addrlen = sizeof(struct sockaddr_in);
} else } else
#endif /* CONFIG_NET_IPV4 */ #endif /* CONFIG_NET_IPV4 */
@ -1772,11 +1772,11 @@ int net_context_send(struct net_buf *buf,
addrlen = 0; addrlen = 0;
} }
return sendto(buf, &context->remote, addrlen, cb, timeout, token, return sendto(pkt, &context->remote, addrlen, cb, timeout, token,
user_data); user_data);
} }
int net_context_sendto(struct net_buf *buf, int net_context_sendto(struct net_pkt *pkt,
const struct sockaddr *dst_addr, const struct sockaddr *dst_addr,
socklen_t addrlen, socklen_t addrlen,
net_context_send_cb_t cb, net_context_send_cb_t cb,
@ -1785,61 +1785,61 @@ int net_context_sendto(struct net_buf *buf,
void *user_data) void *user_data)
{ {
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
struct net_context *context = net_pkt_context(buf); struct net_context *context = net_pkt_context(pkt);
NET_ASSERT(PART_OF_ARRAY(contexts, context)); NET_ASSERT(PART_OF_ARRAY(contexts, context));
if (net_context_get_ip_proto(context) == IPPROTO_TCP) { if (net_context_get_ip_proto(context) == IPPROTO_TCP) {
/* Match POSIX behavior and ignore dst_address and addrlen */ /* Match POSIX behavior and ignore dst_address and addrlen */
return net_context_send(buf, cb, timeout, token, user_data); return net_context_send(pkt, cb, timeout, token, user_data);
} }
#endif /* CONFIG_NET_TCP */ #endif /* CONFIG_NET_TCP */
return sendto(buf, dst_addr, addrlen, cb, timeout, token, user_data); return sendto(pkt, dst_addr, addrlen, cb, timeout, token, user_data);
} }
static void set_appdata_values(struct net_buf *buf, enum net_ip_protocol proto) static void set_appdata_values(struct net_pkt *pkt, enum net_ip_protocol proto)
{ {
size_t total_len = net_buf_frags_len(buf); size_t total_len = net_pkt_get_len(pkt);
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
if (proto == IPPROTO_UDP) { if (proto == IPPROTO_UDP) {
net_pkt_set_appdata(buf, net_pkt_udp_data(buf) + net_pkt_set_appdata(pkt, net_pkt_udp_data(pkt) +
sizeof(struct net_udp_hdr)); sizeof(struct net_udp_hdr));
} else } else
#endif /* CONFIG_NET_UDP */ #endif /* CONFIG_NET_UDP */
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
if (proto == IPPROTO_TCP) { if (proto == IPPROTO_TCP) {
net_pkt_set_appdata(buf, net_pkt_udp_data(buf) + net_pkt_set_appdata(pkt, net_pkt_udp_data(pkt) +
tcp_hdr_len(buf)); tcp_hdr_len(pkt));
} else } else
#endif /* CONFIG_NET_TCP */ #endif /* CONFIG_NET_TCP */
{ {
net_pkt_set_appdata(buf, net_pkt_ip_data(buf) + net_pkt_set_appdata(pkt, net_pkt_ip_data(pkt) +
net_pkt_ip_hdr_len(buf)); net_pkt_ip_hdr_len(pkt));
} }
net_pkt_set_appdatalen(buf, total_len - net_pkt_set_appdatalen(pkt, total_len -
(net_pkt_appdata(buf) - (net_pkt_appdata(pkt) -
net_pkt_ip_data(buf))); net_pkt_ip_data(pkt)));
NET_ASSERT_INFO(net_pkt_appdatalen(buf) < total_len, NET_ASSERT_INFO(net_pkt_appdatalen(pkt) < total_len,
"Wrong appdatalen %u, total %zu", "Wrong appdatalen %u, total %zu",
net_pkt_appdatalen(buf), total_len); net_pkt_appdatalen(pkt), total_len);
} }
static enum net_verdict packet_received(struct net_conn *conn, static enum net_verdict packet_received(struct net_conn *conn,
struct net_buf *buf, struct net_pkt *pkt,
void *user_data) void *user_data)
{ {
struct net_context *context = find_context(conn); struct net_context *context = find_context(conn);
NET_ASSERT(context); NET_ASSERT(context);
NET_ASSERT(net_pkt_iface(buf)); NET_ASSERT(net_pkt_iface(pkt));
net_context_set_iface(context, net_pkt_iface(buf)); net_context_set_iface(context, net_pkt_iface(pkt));
net_pkt_set_context(buf, context); net_pkt_set_context(pkt, context);
/* If there is no callback registered, then we can only drop /* If there is no callback registered, then we can only drop
* the packet. * the packet.
@ -1851,21 +1851,21 @@ static enum net_verdict packet_received(struct net_conn *conn,
if (net_context_get_ip_proto(context) != IPPROTO_TCP) { if (net_context_get_ip_proto(context) != IPPROTO_TCP) {
/* TCP packets get appdata earlier in tcp_established(). */ /* TCP packets get appdata earlier in tcp_established(). */
set_appdata_values(buf, IPPROTO_UDP); set_appdata_values(pkt, IPPROTO_UDP);
} }
#if defined(CONFIG_NET_TCP) #if defined(CONFIG_NET_TCP)
else if (net_context_get_type(context) == SOCK_STREAM) { else if (net_context_get_type(context) == SOCK_STREAM) {
if (net_pkt_appdatalen(buf) == 0) { if (net_pkt_appdatalen(pkt) == 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return NET_OK; return NET_OK;
} }
} }
#endif /* CONFIG_NET_TCP */ #endif /* CONFIG_NET_TCP */
NET_DBG("Set appdata %p to len %u (total %zu)", NET_DBG("Set appdata %p to len %u (total %zu)",
net_pkt_appdata(buf), net_pkt_appdatalen(buf), net_pkt_appdata(pkt), net_pkt_appdatalen(pkt),
net_buf_frags_len(buf)); net_pkt_get_len(pkt));
context->recv_cb(context, buf, 0, user_data); context->recv_cb(context, pkt, 0, user_data);
#if defined(CONFIG_NET_CONTEXT_SYNC_RECV) #if defined(CONFIG_NET_CONTEXT_SYNC_RECV)
k_sem_give(&context->recv_data_wait); k_sem_give(&context->recv_data_wait);

View file

@ -64,7 +64,7 @@ static struct k_fifo rx_queue;
static k_tid_t rx_tid; static k_tid_t rx_tid;
static K_SEM_DEFINE(startup_sync, 0, UINT_MAX); static K_SEM_DEFINE(startup_sync, 0, UINT_MAX);
static inline enum net_verdict process_data(struct net_buf *buf, static inline enum net_verdict process_data(struct net_pkt *pkt,
bool is_loopback) bool is_loopback)
{ {
int ret; int ret;
@ -75,30 +75,24 @@ static inline enum net_verdict process_data(struct net_buf *buf,
* an IPv6 packet, then do not pass it to L2 as the packet does * an IPv6 packet, then do not pass it to L2 as the packet does
* not have link layer headers in it. * not have link layer headers in it.
*/ */
if (net_pkt_ipv6_fragment_start(buf)) { if (net_pkt_ipv6_fragment_start(pkt)) {
locally_routed = true; locally_routed = true;
} }
#endif #endif
/* If there is no data, then drop the packet. Also if /* If there is no data, then drop the packet. */
* the buffer is wrong type, then also drop the packet. if (!pkt->frags) {
* The first buffer needs to have user data part that NET_DBG("Corrupted packet (frags %p)", pkt->frags);
* contains user data. The rest of the fragments should
* be data fragments without user data.
*/
if (!buf->frags || !buf->pool->user_data_size) {
NET_DBG("Corrupted buffer (frags %p, data size %u)",
buf->frags, buf->pool->user_data_size);
net_stats_update_processing_error(); net_stats_update_processing_error();
return NET_DROP; return NET_DROP;
} }
if (!is_loopback && !locally_routed) { if (!is_loopback && !locally_routed) {
ret = net_if_recv_data(net_pkt_iface(buf), buf); ret = net_if_recv_data(net_pkt_iface(pkt), pkt);
if (ret != NET_CONTINUE) { if (ret != NET_CONTINUE) {
if (ret == NET_DROP) { if (ret == NET_DROP) {
NET_DBG("Buffer %p discarded by L2", buf); NET_DBG("Packet %p discarded by L2", pkt);
net_stats_update_processing_error(); net_stats_update_processing_error();
} }
@ -107,46 +101,46 @@ static inline enum net_verdict process_data(struct net_buf *buf,
} }
/* IP version and header length. */ /* IP version and header length. */
switch (NET_IPV6_BUF(buf)->vtc & 0xf0) { switch (NET_IPV6_BUF(pkt)->vtc & 0xf0) {
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
case 0x60: case 0x60:
net_stats_update_ipv6_recv(); net_stats_update_ipv6_recv();
net_pkt_set_family(buf, PF_INET6); net_pkt_set_family(pkt, PF_INET6);
return net_ipv6_process_pkt(buf); return net_ipv6_process_pkt(pkt);
#endif #endif
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
case 0x40: case 0x40:
net_stats_update_ipv4_recv(); net_stats_update_ipv4_recv();
net_pkt_set_family(buf, PF_INET); net_pkt_set_family(pkt, PF_INET);
return net_ipv4_process_pkt(buf); return net_ipv4_process_pkt(pkt);
#endif #endif
} }
NET_DBG("Unknown IP family packet (0x%x)", NET_DBG("Unknown IP family packet (0x%x)",
NET_IPV6_BUF(buf)->vtc & 0xf0); NET_IPV6_BUF(pkt)->vtc & 0xf0);
net_stats_update_ip_errors_protoerr(); net_stats_update_ip_errors_protoerr();
net_stats_update_ip_errors_vhlerr(); net_stats_update_ip_errors_vhlerr();
return NET_DROP; return NET_DROP;
} }
static void processing_data(struct net_buf *buf, bool is_loopback) static void processing_data(struct net_pkt *pkt, bool is_loopback)
{ {
switch (process_data(buf, is_loopback)) { switch (process_data(pkt, is_loopback)) {
case NET_OK: case NET_OK:
NET_DBG("Consumed buf %p", buf); NET_DBG("Consumed pkt %p", pkt);
break; break;
case NET_DROP: case NET_DROP:
default: default:
NET_DBG("Dropping buf %p", buf); NET_DBG("Dropping pkt %p", pkt);
net_pkt_unref(buf); net_pkt_unref(pkt);
break; break;
} }
} }
static void net_rx_thread(void) static void net_rx_thread(void)
{ {
struct net_buf *buf; struct net_pkt *pkt;
NET_DBG("Starting RX thread (stack %zu bytes)", sizeof(rx_stack)); NET_DBG("Starting RX thread (stack %zu bytes)", sizeof(rx_stack));
@ -168,18 +162,18 @@ static void net_rx_thread(void)
size_t pkt_len; size_t pkt_len;
#endif #endif
buf = net_buf_get(&rx_queue, K_FOREVER); pkt = k_fifo_get(&rx_queue, K_FOREVER);
net_analyze_stack("RX thread", rx_stack, sizeof(rx_stack)); net_analyze_stack("RX thread", rx_stack, sizeof(rx_stack));
#if defined(CONFIG_NET_STATISTICS) || defined(CONFIG_NET_DEBUG_CORE) #if defined(CONFIG_NET_STATISTICS) || defined(CONFIG_NET_DEBUG_CORE)
pkt_len = net_buf_frags_len(buf); pkt_len = net_pkt_get_len(pkt);
#endif #endif
NET_DBG("Received buf %p len %zu", buf, pkt_len); NET_DBG("Received pkt %p len %zu", pkt, pkt_len);
net_stats_update_bytes_recv(pkt_len); net_stats_update_bytes_recv(pkt_len);
processing_data(buf, false); processing_data(pkt, false);
net_print_statistics(); net_print_statistics();
net_pkt_print(); net_pkt_print();
@ -202,11 +196,11 @@ static void init_rx_queue(void)
/* Check if the IPv{4|6} addresses are proper. As this can be expensive, /* Check if the IPv{4|6} addresses are proper. As this can be expensive,
* make this optional. * make this optional.
*/ */
static inline int check_ip_addr(struct net_buf *buf) static inline int check_ip_addr(struct net_pkt *pkt)
{ {
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
if (net_ipv6_addr_cmp(&NET_IPV6_BUF(buf)->dst, if (net_ipv6_addr_cmp(&NET_IPV6_BUF(pkt)->dst,
net_ipv6_unspecified_address())) { net_ipv6_unspecified_address())) {
NET_DBG("IPv6 dst address missing"); NET_DBG("IPv6 dst address missing");
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
@ -215,17 +209,17 @@ static inline int check_ip_addr(struct net_buf *buf)
/* If the destination address is our own, then route it /* If the destination address is our own, then route it
* back to us. * back to us.
*/ */
if (net_is_ipv6_addr_loopback(&NET_IPV6_BUF(buf)->dst) || if (net_is_ipv6_addr_loopback(&NET_IPV6_BUF(pkt)->dst) ||
net_is_my_ipv6_addr(&NET_IPV6_BUF(buf)->dst)) { net_is_my_ipv6_addr(&NET_IPV6_BUF(pkt)->dst)) {
struct in6_addr addr; struct in6_addr addr;
/* Swap the addresses so that in receiving side /* Swap the addresses so that in receiving side
* the packet is accepted. * the packet is accepted.
*/ */
net_ipaddr_copy(&addr, &NET_IPV6_BUF(buf)->src); net_ipaddr_copy(&addr, &NET_IPV6_BUF(pkt)->src);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src,
&NET_IPV6_BUF(buf)->dst); &NET_IPV6_BUF(pkt)->dst);
net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, &addr); net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, &addr);
return 1; return 1;
} }
@ -233,7 +227,7 @@ static inline int check_ip_addr(struct net_buf *buf)
/* The source check must be done after the destination check /* The source check must be done after the destination check
* as having src ::1 is perfectly ok if dst is ::1 too. * as having src ::1 is perfectly ok if dst is ::1 too.
*/ */
if (net_is_ipv6_addr_loopback(&NET_IPV6_BUF(buf)->src)) { if (net_is_ipv6_addr_loopback(&NET_IPV6_BUF(pkt)->src)) {
NET_DBG("IPv6 loopback src address"); NET_DBG("IPv6 loopback src address");
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
} }
@ -241,8 +235,8 @@ static inline int check_ip_addr(struct net_buf *buf)
#endif /* CONFIG_NET_IPV6 */ #endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
if (net_ipv4_addr_cmp(&NET_IPV4_BUF(buf)->dst, if (net_ipv4_addr_cmp(&NET_IPV4_BUF(pkt)->dst,
net_ipv4_unspecified_address())) { net_ipv4_unspecified_address())) {
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
} }
@ -250,17 +244,17 @@ static inline int check_ip_addr(struct net_buf *buf)
/* If the destination address is our own, then route it /* If the destination address is our own, then route it
* back to us. * back to us.
*/ */
if (net_is_ipv4_addr_loopback(&NET_IPV4_BUF(buf)->dst) || if (net_is_ipv4_addr_loopback(&NET_IPV4_BUF(pkt)->dst) ||
net_is_my_ipv4_addr(&NET_IPV4_BUF(buf)->dst)) { net_is_my_ipv4_addr(&NET_IPV4_BUF(pkt)->dst)) {
struct in_addr addr; struct in_addr addr;
/* Swap the addresses so that in receiving side /* Swap the addresses so that in receiving side
* the packet is accepted. * the packet is accepted.
*/ */
net_ipaddr_copy(&addr, &NET_IPV4_BUF(buf)->src); net_ipaddr_copy(&addr, &NET_IPV4_BUF(pkt)->src);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src,
&NET_IPV4_BUF(buf)->dst); &NET_IPV4_BUF(pkt)->dst);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, &addr); net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, &addr);
return 1; return 1;
} }
@ -269,7 +263,7 @@ static inline int check_ip_addr(struct net_buf *buf)
* as having src 127.0.0.0/8 is perfectly ok if dst is in * as having src 127.0.0.0/8 is perfectly ok if dst is in
* localhost subnet too. * localhost subnet too.
*/ */
if (net_is_ipv4_addr_loopback(&NET_IPV4_BUF(buf)->src)) { if (net_is_ipv4_addr_loopback(&NET_IPV4_BUF(pkt)->src)) {
NET_DBG("IPv4 loopback src address"); NET_DBG("IPv4 loopback src address");
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
} }
@ -283,24 +277,24 @@ static inline int check_ip_addr(struct net_buf *buf)
return 0; return 0;
} }
#else #else
#define check_ip_addr(buf) 0 #define check_ip_addr(pkt) 0
#endif #endif
/* Called when data needs to be sent to network */ /* Called when data needs to be sent to network */
int net_send_data(struct net_buf *buf) int net_send_data(struct net_pkt *pkt)
{ {
int status; int status;
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
return -ENODATA; return -ENODATA;
} }
if (!net_pkt_iface(buf)) { if (!net_pkt_iface(pkt)) {
return -EINVAL; return -EINVAL;
} }
#if defined(CONFIG_NET_STATISTICS) #if defined(CONFIG_NET_STATISTICS)
switch (net_pkt_family(buf)) { switch (net_pkt_family(pkt)) {
case AF_INET: case AF_INET:
net_stats_update_ipv4_sent(); net_stats_update_ipv4_sent();
break; break;
@ -310,18 +304,18 @@ int net_send_data(struct net_buf *buf)
} }
#endif #endif
status = check_ip_addr(buf); status = check_ip_addr(pkt);
if (status < 0) { if (status < 0) {
return status; return status;
} else if (status > 0) { } else if (status > 0) {
/* Packet is destined back to us so send it directly /* Packet is destined back to us so send it directly
* to RX processing. * to RX processing.
*/ */
processing_data(buf, true); processing_data(pkt, true);
return 0; return 0;
} }
if (net_if_send_data(net_pkt_iface(buf), buf) == NET_DROP) { if (net_if_send_data(net_pkt_iface(pkt), pkt) == NET_DROP) {
return -EIO; return -EIO;
} }
@ -329,12 +323,12 @@ int net_send_data(struct net_buf *buf)
} }
/* Called by driver when an IP packet has been received */ /* Called by driver when an IP packet has been received */
int net_recv_data(struct net_if *iface, struct net_buf *buf) int net_recv_data(struct net_if *iface, struct net_pkt *pkt)
{ {
NET_ASSERT(buf && buf->frags); NET_ASSERT(pkt && pkt->frags);
NET_ASSERT(iface); NET_ASSERT(iface);
if (!buf->frags) { if (!pkt->frags) {
return -ENODATA; return -ENODATA;
} }
@ -342,12 +336,12 @@ int net_recv_data(struct net_if *iface, struct net_buf *buf)
return -ENETDOWN; return -ENETDOWN;
} }
NET_DBG("fifo %p iface %p buf %p len %zu", &rx_queue, iface, buf, NET_DBG("fifo %p iface %p pkt %p len %zu", &rx_queue, iface, pkt,
net_buf_frags_len(buf)); net_pkt_get_len(pkt));
net_pkt_set_iface(buf, iface); net_pkt_set_iface(pkt, iface);
net_buf_put(&rx_queue, buf); k_fifo_put(&rx_queue, pkt);
return 0; return 0;
} }

View file

@ -46,14 +46,14 @@ NET_STACK_DEFINE(TX, tx_stack, CONFIG_NET_TX_STACK_SIZE,
CONFIG_NET_TX_STACK_SIZE); CONFIG_NET_TX_STACK_SIZE);
#if defined(CONFIG_NET_DEBUG_IF) #if defined(CONFIG_NET_DEBUG_IF)
#define debug_check_packet(buf) \ #define debug_check_packet(pkt) \
{ \ { \
size_t len = net_buf_frags_len(buf->frags); \ size_t len = net_pkt_get_len(pkt); \
\ \
NET_DBG("Processing (buf %p, data len %zu) network packet", \ NET_DBG("Processing (pkt %p, data len %zu) network packet", \
buf, len); \ pkt, len); \
\ \
NET_ASSERT(buf->frags && len); \ NET_ASSERT(pkt->frags && len); \
} while (0) } while (0)
#else #else
#define debug_check_packet(...) #define debug_check_packet(...)
@ -78,29 +78,29 @@ static bool net_if_tx(struct net_if *iface)
const struct net_if_api *api = iface->dev->driver_api; const struct net_if_api *api = iface->dev->driver_api;
struct net_linkaddr *dst; struct net_linkaddr *dst;
struct net_context *context; struct net_context *context;
struct net_buf *buf; struct net_pkt *pkt;
void *context_token; void *context_token;
int status; int status;
#if defined(CONFIG_NET_STATISTICS) #if defined(CONFIG_NET_STATISTICS)
size_t pkt_len; size_t pkt_len;
#endif #endif
buf = net_buf_get(&iface->tx_queue, K_NO_WAIT); pkt = k_fifo_get(&iface->tx_queue, K_NO_WAIT);
if (!buf) { if (!pkt) {
return false; return false;
} }
debug_check_packet(buf); debug_check_packet(pkt);
dst = net_pkt_ll_dst(buf); dst = net_pkt_ll_dst(pkt);
context = net_pkt_context(buf); context = net_pkt_context(pkt);
context_token = net_pkt_token(buf); context_token = net_pkt_token(pkt);
if (atomic_test_bit(iface->flags, NET_IF_UP)) { if (atomic_test_bit(iface->flags, NET_IF_UP)) {
#if defined(CONFIG_NET_STATISTICS) #if defined(CONFIG_NET_STATISTICS)
pkt_len = net_buf_frags_len(buf); pkt_len = net_pkt_get_len(pkt);
#endif #endif
status = api->send(iface, buf); status = api->send(iface, pkt);
} else { } else {
/* Drop packet if interface is not up */ /* Drop packet if interface is not up */
NET_WARN("iface %p is down", iface); NET_WARN("iface %p is down", iface);
@ -108,7 +108,7 @@ static bool net_if_tx(struct net_if *iface)
} }
if (status < 0) { if (status < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} else { } else {
net_stats_update_bytes_sent(pkt_len); net_stats_update_bytes_sent(pkt_len);
} }
@ -133,8 +133,8 @@ static void net_if_flush_tx(struct net_if *iface)
return; return;
} }
/* Without this, the net_buf_get() can return a buf which /* Without this, the k_fifo_get() can return a pkt which
* has buf->frags set to NULL. This is not allowed as we * has pkt->frags set to NULL. This is not allowed as we
* cannot send a packet that has no data in it. * cannot send a packet that has no data in it.
* The k_yield() fixes the issue and packets are flushed * The k_yield() fixes the issue and packets are flushed
* correctly. * correctly.
@ -222,11 +222,11 @@ static inline void init_iface(struct net_if *iface)
api->init(iface); api->init(iface);
} }
enum net_verdict net_if_send_data(struct net_if *iface, struct net_buf *buf) enum net_verdict net_if_send_data(struct net_if *iface, struct net_pkt *pkt)
{ {
struct net_context *context = net_pkt_context(buf); struct net_context *context = net_pkt_context(pkt);
struct net_linkaddr *dst = net_pkt_ll_dst(buf); struct net_linkaddr *dst = net_pkt_ll_dst(pkt);
void *token = net_pkt_token(buf); void *token = net_pkt_token(pkt);
enum net_verdict verdict; enum net_verdict verdict;
int status = -EIO; int status = -EIO;
@ -244,25 +244,25 @@ enum net_verdict net_if_send_data(struct net_if *iface, struct net_buf *buf)
* https://jira.zephyrproject.org/browse/ZEP-1656 * https://jira.zephyrproject.org/browse/ZEP-1656
*/ */
if (!atomic_test_bit(iface->flags, NET_IF_POINTOPOINT) && if (!atomic_test_bit(iface->flags, NET_IF_POINTOPOINT) &&
!net_pkt_ll_src(buf)->addr) { !net_pkt_ll_src(pkt)->addr) {
net_pkt_ll_src(buf)->addr = net_pkt_ll_if(buf)->addr; net_pkt_ll_src(pkt)->addr = net_pkt_ll_if(pkt)->addr;
net_pkt_ll_src(buf)->len = net_pkt_ll_if(buf)->len; net_pkt_ll_src(pkt)->len = net_pkt_ll_if(pkt)->len;
} }
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
/* If the ll dst address is not set check if it is present in the nbr /* If the ll dst address is not set check if it is present in the nbr
* cache. * cache.
*/ */
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
buf = net_ipv6_prepare_for_send(buf); pkt = net_ipv6_prepare_for_send(pkt);
if (!buf) { if (!pkt) {
verdict = NET_CONTINUE; verdict = NET_CONTINUE;
goto done; goto done;
} }
} }
#endif #endif
verdict = iface->l2->send(iface, buf); verdict = iface->l2->send(iface, pkt);
done: done:
/* The L2 send() function can return /* The L2 send() function can return

File diff suppressed because it is too large Load diff

View file

@ -19,12 +19,12 @@ extern void net_pkt_init(void);
extern void net_if_init(struct k_sem *startup_sync); extern void net_if_init(struct k_sem *startup_sync);
extern void net_if_post_init(void); extern void net_if_post_init(void);
extern void net_context_init(void); extern void net_context_init(void);
enum net_verdict net_ipv4_process_pkt(struct net_buf *buf); enum net_verdict net_ipv4_process_pkt(struct net_pkt *pkt);
enum net_verdict net_ipv6_process_pkt(struct net_buf *buf); enum net_verdict net_ipv6_process_pkt(struct net_pkt *pkt);
extern void net_ipv6_init(void); extern void net_ipv6_init(void);
#if defined(CONFIG_NET_IPV6_FRAGMENT) #if defined(CONFIG_NET_IPV6_FRAGMENT)
int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_buf *buf, int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_pkt *pkt,
uint16_t pkt_len); uint16_t pkt_len);
#endif #endif
@ -32,30 +32,30 @@ extern const char *net_proto2str(enum net_ip_protocol proto);
extern char *net_byte_to_hex(char *ptr, uint8_t byte, char base, bool pad); extern char *net_byte_to_hex(char *ptr, uint8_t byte, char base, bool pad);
extern char *net_sprint_ll_addr_buf(const uint8_t *ll, uint8_t ll_len, extern char *net_sprint_ll_addr_buf(const uint8_t *ll, uint8_t ll_len,
char *buf, int buflen); char *buf, int buflen);
extern uint16_t net_calc_chksum(struct net_buf *buf, uint8_t proto); extern uint16_t net_calc_chksum(struct net_pkt *pkt, uint8_t proto);
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
extern uint16_t net_calc_chksum_ipv4(struct net_buf *buf); extern uint16_t net_calc_chksum_ipv4(struct net_pkt *pkt);
#endif /* CONFIG_NET_IPV4 */ #endif /* CONFIG_NET_IPV4 */
static inline uint16_t net_calc_chksum_icmpv6(struct net_buf *buf) static inline uint16_t net_calc_chksum_icmpv6(struct net_pkt *pkt)
{ {
return net_calc_chksum(buf, IPPROTO_ICMPV6); return net_calc_chksum(pkt, IPPROTO_ICMPV6);
} }
static inline uint16_t net_calc_chksum_icmpv4(struct net_buf *buf) static inline uint16_t net_calc_chksum_icmpv4(struct net_pkt *pkt)
{ {
return net_calc_chksum(buf, IPPROTO_ICMP); return net_calc_chksum(pkt, IPPROTO_ICMP);
} }
static inline uint16_t net_calc_chksum_udp(struct net_buf *buf) static inline uint16_t net_calc_chksum_udp(struct net_pkt *pkt)
{ {
return net_calc_chksum(buf, IPPROTO_UDP); return net_calc_chksum(pkt, IPPROTO_UDP);
} }
static inline uint16_t net_calc_chksum_tcp(struct net_buf *buf) static inline uint16_t net_calc_chksum_tcp(struct net_pkt *pkt)
{ {
return net_calc_chksum(buf, IPPROTO_TCP); return net_calc_chksum(pkt, IPPROTO_TCP);
} }
#if NET_LOG_ENABLED > 0 #if NET_LOG_ENABLED > 0
@ -143,26 +143,26 @@ static inline void net_hexdump(const char *str, const uint8_t *packet,
} }
/* Hexdump from all fragments */ /* Hexdump from all fragments */
static inline void net_hexdump_frags(const char *str, struct net_buf *buf) static inline void net_hexdump_frags(const char *str, struct net_pkt *pkt)
{ {
struct net_buf *frag = buf->frags; struct net_buf *frag = pkt->frags;
while (frag) { while (frag) {
net_hexdump(str, frag->data, net_pkt_get_len(frag)); net_hexdump(str, frag->data, frag->len);
frag = frag->frags; frag = frag->frags;
} }
} }
/* Print fragment chain */ /* Print fragment chain */
static inline void net_print_frags(const char *str, struct net_buf *buf) static inline void net_print_frags(const char *str, struct net_pkt *pkt)
{ {
struct net_buf *frag = buf->frags; struct net_buf *frag = pkt->frags;
if (str) { if (str) {
printk("%s", str); printk("%s", str);
} }
printk("%p[%d]", buf, buf->ref); printk("%p[%d]", pkt, pkt->ref);
if (frag) { if (frag) {
printk("->"); printk("->");

View file

@ -509,7 +509,8 @@ static void ipv6_frag_cb(struct net_ipv6_reassembly *reass,
#endif /* CONFIG_NET_IPV6_FRAGMENT */ #endif /* CONFIG_NET_IPV6_FRAGMENT */
#if defined(CONFIG_NET_DEBUG_NET_PKT) #if defined(CONFIG_NET_DEBUG_NET_PKT)
static void allocs_cb(struct net_buf *buf, static void allocs_cb(struct net_pkt *pkt,
struct net_buf *buf,
const char *func_alloc, const char *func_alloc,
int line_alloc, int line_alloc,
const char *func_free, const char *func_free,
@ -529,6 +530,24 @@ static void allocs_cb(struct net_buf *buf,
} }
} }
if (buf) {
goto buf;
}
if (func_alloc) {
if (in_use) {
printk("%p/%d\t%5s\t%5s\t%s():%d\n", pkt, pkt->ref,
str, net_pkt_slab2str(pkt->slab), func_alloc,
line_alloc);
} else {
printk("%p\t%5s\t%5s\t%s():%d -> %s():%d\n", pkt,
str, net_pkt_slab2str(pkt->slab), func_alloc,
line_alloc, func_free, line_free);
}
}
return;
buf:
if (func_alloc) { if (func_alloc) {
if (in_use) { if (in_use) {
printk("%p/%d\t%5s\t%5s\t%s():%d\n", buf, buf->ref, printk("%p/%d\t%5s\t%5s\t%s():%d\n", buf, buf->ref,
@ -551,8 +570,8 @@ static int shell_cmd_allocs(int argc, char *argv[])
ARG_UNUSED(argv); ARG_UNUSED(argv);
#if defined(CONFIG_NET_DEBUG_NET_PKT) #if defined(CONFIG_NET_DEBUG_NET_PKT)
printk("Network buffer allocations\n\n"); printk("Network memory allocations\n\n");
printk("net_buf\t\tStatus\tPool\tFunction alloc -> freed\n"); printk("memory\t\tStatus\tPool\tFunction alloc -> freed\n");
net_pkt_allocs_foreach(allocs_cb, NULL); net_pkt_allocs_foreach(allocs_cb, NULL);
#else #else
printk("Enable CONFIG_NET_DEBUG_NET_PKT to see allocations.\n"); printk("Enable CONFIG_NET_DEBUG_NET_PKT to see allocations.\n");
@ -806,20 +825,26 @@ static int shell_cmd_iface(int argc, char *argv[])
struct ctx_info { struct ctx_info {
int pos; int pos;
bool are_external_pools; bool are_external_pools;
struct net_buf_pool *tx_pools[CONFIG_NET_MAX_CONTEXTS]; struct k_mem_slab *tx_slabs[CONFIG_NET_MAX_CONTEXTS];
struct net_buf_pool *data_pools[CONFIG_NET_MAX_CONTEXTS]; struct net_buf_pool *data_pools[CONFIG_NET_MAX_CONTEXTS];
}; };
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
static bool pool_found_already(struct ctx_info *info, static bool slab_pool_found_already(struct ctx_info *info,
struct net_buf_pool *pool) struct k_mem_slab *slab,
struct net_buf_pool *pool)
{ {
int i; int i;
for (i = 0; i < CONFIG_NET_MAX_CONTEXTS; i++) { for (i = 0; i < CONFIG_NET_MAX_CONTEXTS; i++) {
if (info->tx_pools[i] == pool || if (slab) {
info->data_pools[i] == pool) { if (info->tx_slabs[i] == slab) {
return true; return true;
}
} else {
if (info->data_pools[i] == pool) {
return true;
}
} }
} }
@ -831,34 +856,35 @@ static void context_info(struct net_context *context, void *user_data)
{ {
#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL)
struct ctx_info *info = user_data; struct ctx_info *info = user_data;
struct k_mem_slab *slab;
struct net_buf_pool *pool; struct net_buf_pool *pool;
if (!net_context_is_used(context)) { if (!net_context_is_used(context)) {
return; return;
} }
if (context->tx_pool) { if (context->tx_slab) {
pool = context->tx_pool(); slab = context->tx_slab();
if (pool_found_already(info, pool)) { if (slab_pool_found_already(info, slab, NULL)) {
return; return;
} }
#if defined(CONFIG_NET_DEBUG_NET_PKT) #if defined(CONFIG_NET_DEBUG_NET_PKT)
printk("ETX (%s)\t%d\t%d\t%d\t%p\n", printk("ETX\t%zu\t%u\t%u\t%p\n",
pool->name, pool->pool_size, pool->buf_count, slab->num_blocks * slab->block_size,
pool->avail_count, pool); slab->num_blocks, k_mem_slab_num_free_get(slab), slab);
#else #else
printk("ETX \t%d\t%p\n", pool->buf_count, pool); printk("ETX \t%d\t%p\n", slab->num_blocks, slab);
#endif #endif
info->are_external_pools = true; info->are_external_pools = true;
info->tx_pools[info->pos] = pool; info->tx_slabs[info->pos] = slab;
} }
if (context->data_pool) { if (context->data_pool) {
pool = context->data_pool(); pool = context->data_pool();
if (pool_found_already(info, pool)) { if (slab_pool_found_already(info, NULL, pool)) {
return; return;
} }
@ -879,7 +905,8 @@ static void context_info(struct net_context *context, void *user_data)
static int shell_cmd_mem(int argc, char *argv[]) static int shell_cmd_mem(int argc, char *argv[])
{ {
struct net_buf_pool *tx, *rx, *rx_data, *tx_data; struct k_mem_slab *rx, *tx;
struct net_buf_pool *rx_data, *tx_data;
ARG_UNUSED(argc); ARG_UNUSED(argc);
ARG_UNUSED(argv); ARG_UNUSED(argv);
@ -893,11 +920,13 @@ static int shell_cmd_mem(int argc, char *argv[])
#if defined(CONFIG_NET_DEBUG_NET_PKT) #if defined(CONFIG_NET_DEBUG_NET_PKT)
printk("Name\t\t\tSize\tCount\tAvail\tAddress\n"); printk("Name\t\t\tSize\tCount\tAvail\tAddress\n");
printk("RX (%s)\t\t%d\t%d\t%d\t%p\n", printk("RX\t\t%zu\t%d\t%u\t%p\n",
rx->name, rx->pool_size, rx->buf_count, rx->avail_count, rx); rx->num_blocks * rx->block_size,
rx->num_blocks, k_mem_slab_num_free_get(rx), rx);
printk("TX (%s)\t\t%d\t%d\t%d\t%p\n", printk("TX\t\t%zu\t%d\t%u\t%p\n",
tx->name, tx->pool_size, tx->buf_count, tx->avail_count, tx); tx->num_blocks * tx->block_size,
tx->num_blocks, k_mem_slab_num_free_get(tx), tx);
printk("RX DATA (%s)\t%d\t%d\t%d\t%p\n", printk("RX DATA (%s)\t%d\t%d\t%d\t%p\n",
rx_data->name, rx_data->pool_size, rx_data->buf_count, rx_data->name, rx_data->pool_size, rx_data->buf_count,
@ -909,8 +938,8 @@ static int shell_cmd_mem(int argc, char *argv[])
#else #else
printk("Name \tCount\tAddress\n"); printk("Name \tCount\tAddress\n");
printk("RX \t%d\t%p\n", rx->buf_count, rx); printk("RX \t%d\t%p\n", rx->num_blocks, rx);
printk("TX \t%d\t%p\n", tx->buf_count, tx); printk("TX \t%d\t%p\n", tx->num_blocks, tx);
printk("RX DATA \t%d\t%p\n", rx_data->buf_count, rx_data); printk("RX DATA \t%d\t%p\n", rx_data->buf_count, rx_data);
printk("TX DATA \t%d\t%p\n", tx_data->buf_count, tx_data); printk("TX DATA \t%d\t%p\n", tx_data->buf_count, tx_data);
#endif /* CONFIG_NET_DEBUG_NET_PKT */ #endif /* CONFIG_NET_DEBUG_NET_PKT */
@ -1018,7 +1047,7 @@ K_SEM_DEFINE(ping_timeout, 0, 1);
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
static enum net_verdict _handle_ipv6_echo_reply(struct net_buf *buf); static enum net_verdict _handle_ipv6_echo_reply(struct net_pkt *pkt);
static struct net_icmpv6_handler ping6_handler = { static struct net_icmpv6_handler ping6_handler = {
.type = NET_ICMPV6_ECHO_REPLY, .type = NET_ICMPV6_ECHO_REPLY,
@ -1031,15 +1060,15 @@ static inline void _remove_ipv6_ping_handler(void)
net_icmpv6_unregister_handler(&ping6_handler); net_icmpv6_unregister_handler(&ping6_handler);
} }
static enum net_verdict _handle_ipv6_echo_reply(struct net_buf *buf) static enum net_verdict _handle_ipv6_echo_reply(struct net_pkt *pkt)
{ {
char addr[NET_IPV6_ADDR_LEN]; char addr[NET_IPV6_ADDR_LEN];
snprintk(addr, sizeof(addr), "%s", snprintk(addr, sizeof(addr), "%s",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst)); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst));
printk("Received echo reply from %s to %s\n", printk("Received echo reply from %s to %s\n",
net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src), addr); net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src), addr);
k_sem_give(&ping_timeout); k_sem_give(&ping_timeout);
_remove_ipv6_ping_handler(); _remove_ipv6_ping_handler();
@ -1077,7 +1106,7 @@ static int _ping_ipv6(char *host)
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
static enum net_verdict _handle_ipv4_echo_reply(struct net_buf *buf); static enum net_verdict _handle_ipv4_echo_reply(struct net_pkt *pkt);
static struct net_icmpv4_handler ping4_handler = { static struct net_icmpv4_handler ping4_handler = {
.type = NET_ICMPV4_ECHO_REPLY, .type = NET_ICMPV4_ECHO_REPLY,
@ -1090,15 +1119,15 @@ static inline void _remove_ipv4_ping_handler(void)
net_icmpv4_unregister_handler(&ping4_handler); net_icmpv4_unregister_handler(&ping4_handler);
} }
static enum net_verdict _handle_ipv4_echo_reply(struct net_buf *buf) static enum net_verdict _handle_ipv4_echo_reply(struct net_pkt *pkt)
{ {
char addr[NET_IPV4_ADDR_LEN]; char addr[NET_IPV4_ADDR_LEN];
snprintk(addr, sizeof(addr), "%s", snprintk(addr, sizeof(addr), "%s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst)); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst));
printk("Received echo reply from %s to %s\n", printk("Received echo reply from %s to %s\n",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), addr); net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src), addr);
k_sem_give(&ping_timeout); k_sem_give(&ping_timeout);
_remove_ipv4_ping_handler(); _remove_ipv4_ping_handler();
@ -1470,7 +1499,7 @@ static int shell_cmd_tcp(int argc, char *argv[])
if (!strcmp(argv[arg], "send")) { if (!strcmp(argv[arg], "send")) {
/* tcp send <data> */ /* tcp send <data> */
struct net_buf *buf; struct net_pkt *pkt;
if (!tcp_ctx || !net_context_is_used(tcp_ctx)) { if (!tcp_ctx || !net_context_is_used(tcp_ctx)) {
printk("Not connected\n"); printk("Not connected\n");
@ -1482,25 +1511,25 @@ static int shell_cmd_tcp(int argc, char *argv[])
return 0; return 0;
} }
buf = net_pkt_get_tx(tcp_ctx, TCP_TIMEOUT); pkt = net_pkt_get_tx(tcp_ctx, TCP_TIMEOUT);
if (!buf) { if (!pkt) {
printk("Out of bufs, msg cannot be sent.\n"); printk("Out of pkts, msg cannot be sent.\n");
return 0; return 0;
} }
ret = net_pkt_append(buf, strlen(argv[arg]), ret = net_pkt_append(pkt, strlen(argv[arg]),
argv[arg], TCP_TIMEOUT); argv[arg], TCP_TIMEOUT);
if (!ret) { if (!ret) {
printk("Cannot build msg (out of bufs)\n"); printk("Cannot build msg (out of pkts)\n");
net_pkt_unref(buf); net_pkt_unref(pkt);
return 0; return 0;
} }
ret = net_context_send(buf, tcp_sent_cb, TCP_TIMEOUT, ret = net_context_send(pkt, tcp_sent_cb, TCP_TIMEOUT,
NULL, NULL); NULL, NULL);
if (ret < 0) { if (ret < 0) {
printk("Cannot send msg (%d)\n", ret); printk("Cannot send msg (%d)\n", ret);
net_pkt_unref(buf); net_pkt_unref(pkt);
return 0; return 0;
} }
@ -1550,14 +1579,14 @@ static int shell_cmd_help(int argc, char *argv[])
ARG_UNUSED(argv); ARG_UNUSED(argv);
/* Keep the commands in alphabetical order */ /* Keep the commands in alphabetical order */
printk("net allocs\n\tPrint network buffer allocations\n"); printk("net allocs\n\tPrint network memory allocations\n");
printk("net conn\n\tPrint information about network connections\n"); printk("net conn\n\tPrint information about network connections\n");
printk("net dns\n\tShow how DNS is configured\n"); printk("net dns\n\tShow how DNS is configured\n");
printk("net dns cancel\n\tCancel all pending requests\n"); printk("net dns cancel\n\tCancel all pending requests\n");
printk("net dns <hostname> [A or AAAA]\n\tQuery IPv4 address (default)" printk("net dns <hostname> [A or AAAA]\n\tQuery IPv4 address (default)"
" or IPv6 address for a host name\n"); " or IPv6 address for a host name\n");
printk("net iface\n\tPrint information about network interfaces\n"); printk("net iface\n\tPrint information about network interfaces\n");
printk("net mem\n\tPrint network buffer information\n"); printk("net mem\n\tPrint network memory information\n");
printk("net nbr\n\tPrint neighbor information\n"); printk("net nbr\n\tPrint neighbor information\n");
printk("net nbr rm <IPv6 address>\n\tRemove neighbor from cache\n"); printk("net nbr rm <IPv6 address>\n\tRemove neighbor from cache\n");
printk("net ping <host>\n\tPing a network host\n"); printk("net ping <host>\n\tPing a network host\n");

View file

@ -716,12 +716,12 @@ bool net_route_get_info(struct net_if *iface,
return false; return false;
} }
int net_route_packet(struct net_buf *buf, struct in6_addr *nexthop) int net_route_packet(struct net_pkt *pkt, struct in6_addr *nexthop)
{ {
struct net_linkaddr_storage *lladdr; struct net_linkaddr_storage *lladdr;
struct net_nbr *nbr; struct net_nbr *nbr;
nbr = net_ipv6_nbr_lookup(net_pkt_iface(buf), nexthop); nbr = net_ipv6_nbr_lookup(net_pkt_iface(pkt), nexthop);
if (!nbr) { if (!nbr) {
NET_DBG("Cannot find %s neighbor.", NET_DBG("Cannot find %s neighbor.",
net_sprint_ipv6_addr(nexthop)); net_sprint_ipv6_addr(nexthop));
@ -738,25 +738,25 @@ int net_route_packet(struct net_buf *buf, struct in6_addr *nexthop)
/* Sanitycheck: If src and dst ll addresses are going to be same, /* Sanitycheck: If src and dst ll addresses are going to be same,
* then something went wrong in route lookup. * then something went wrong in route lookup.
*/ */
if (!memcmp(net_pkt_ll_src(buf)->addr, lladdr->addr, lladdr->len)) { if (!memcmp(net_pkt_ll_src(pkt)->addr, lladdr->addr, lladdr->len)) {
NET_ERR("Src ll and Dst ll are same"); NET_ERR("Src ll and Dst ll are same");
return -EINVAL; return -EINVAL;
} }
net_pkt_set_forwarding(buf, true); net_pkt_set_forwarding(pkt, true);
/* Set the destination and source ll address in the packet. /* Set the destination and source ll address in the packet.
* We set the destination address to be the nexthop recipient. * We set the destination address to be the nexthop recipient.
*/ */
net_pkt_ll_src(buf)->addr = net_pkt_ll_if(buf)->addr; net_pkt_ll_src(pkt)->addr = net_pkt_ll_if(pkt)->addr;
net_pkt_ll_src(buf)->type = net_pkt_ll_if(buf)->type; net_pkt_ll_src(pkt)->type = net_pkt_ll_if(pkt)->type;
net_pkt_ll_src(buf)->len = net_pkt_ll_if(buf)->len; net_pkt_ll_src(pkt)->len = net_pkt_ll_if(pkt)->len;
net_pkt_ll_dst(buf)->addr = lladdr->addr; net_pkt_ll_dst(pkt)->addr = lladdr->addr;
net_pkt_ll_dst(buf)->type = lladdr->type; net_pkt_ll_dst(pkt)->type = lladdr->type;
net_pkt_ll_dst(buf)->len = lladdr->len; net_pkt_ll_dst(pkt)->len = lladdr->len;
return net_send_data(buf); return net_send_data(pkt);
} }
void net_route_init(void) void net_route_init(void)

View file

@ -251,12 +251,12 @@ bool net_route_get_info(struct net_if *iface,
/** /**
* @brief Send the network packet to network via some intermediate host. * @brief Send the network packet to network via some intermediate host.
* *
* @param buf Network buffer to send. * @param pkt Network packet to send.
* @param nexthop Next hop neighbor IPv6 address. * @param nexthop Next hop neighbor IPv6 address.
* *
* @return 0 if there was no error, <0 if the packet could not be sent. * @return 0 if there was no error, <0 if the packet could not be sent.
*/ */
int net_route_packet(struct net_buf *buf, struct in6_addr *nexthop); int net_route_packet(struct net_pkt *pkt, struct in6_addr *nexthop);
#else /* CONFIG_NET_ROUTE */ #else /* CONFIG_NET_ROUTE */
#define net_route_init(...) #define net_route_init(...)

File diff suppressed because it is too large Load diff

View file

@ -909,17 +909,17 @@ void net_rpl_global_repair(struct net_route_entry *route);
/** /**
* @brief Update RPL headers in IPv6 packet. * @brief Update RPL headers in IPv6 packet.
* *
* @param buf Network buffer. * @param pkt Network packet.
* @param addr IPv6 address of next hop host. * @param addr IPv6 address of next hop host.
* *
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
int net_rpl_update_header(struct net_buf *buf, struct in6_addr *addr); int net_rpl_update_header(struct net_pkt *pkt, struct in6_addr *addr);
/** /**
* @brief Verify RPL header in IPv6 packet. * @brief Verify RPL header in IPv6 packet.
* *
* @param buf Network buffer fragment list. * @param pkt Network packet fragment list.
* @param frag Current network buffer fragment. * @param frag Current network buffer fragment.
* @param offset Where the RPL header starts in the packet * @param offset Where the RPL header starts in the packet
* @param pos How long the RPL header was, this is returned to the caller. * @param pos How long the RPL header was, this is returned to the caller.
@ -927,30 +927,30 @@ int net_rpl_update_header(struct net_buf *buf, struct in6_addr *addr);
* *
* @return frag Returns the fragment where this call finished reading. * @return frag Returns the fragment where this call finished reading.
*/ */
struct net_buf *net_rpl_verify_header(struct net_buf *buf, struct net_buf *frag, struct net_buf *net_rpl_verify_header(struct net_pkt *pkt, struct net_buf *frag,
uint16_t offset, uint16_t *pos, uint16_t offset, uint16_t *pos,
bool *result); bool *result);
/** /**
* @brief Insert RPL extension header to IPv6 packet. * @brief Insert RPL extension header to IPv6 packet.
* *
* @param buf Network buffer. * @param pkt Network packet.
* *
* @return 0 if ok, <0 if error. * @return 0 if ok, <0 if error.
*/ */
int net_rpl_insert_header(struct net_buf *buf); int net_rpl_insert_header(struct net_pkt *pkt);
/** /**
* @brief Revert RPL extension header to IPv6 packet. * @brief Revert RPL extension header to IPv6 packet.
* Revert flags, instance ID and sender rank in the packet. * Revert flags, instance ID and sender rank in the packet.
* *
* @param buf Network buffer. * @param pkt Network packet.
* @param offset Where the HBH header starts in the packet * @param offset Where the HBH header starts in the packet
* @param pos How long the RPL header was, this is returned to the caller. * @param pos How long the RPL header was, this is returned to the caller.
* *
* @return 0 if ok, <0 if error. * @return 0 if ok, <0 if error.
*/ */
int net_rpl_revert_header(struct net_buf *buf, uint16_t offset, uint16_t *pos); int net_rpl_revert_header(struct net_pkt *pkt, uint16_t offset, uint16_t *pos);
/** /**
* @brief Get parent IPv6 address. * @brief Get parent IPv6 address.

View file

@ -72,12 +72,12 @@ static char upper_if_set(char chr, bool set)
return chr | 0x20; return chr | 0x20;
} }
static void net_tcp_trace(struct net_buf *buf, struct net_tcp *tcp) static void net_tcp_trace(struct net_pkt *pkt, struct net_tcp *tcp)
{ {
uint8_t flags = NET_TCP_FLAGS(buf); uint8_t flags = NET_TCP_FLAGS(pkt);
uint32_t rel_ack, ack; uint32_t rel_ack, ack;
ack = sys_get_be32(NET_TCP_BUF(buf)->ack); ack = sys_get_be32(NET_TCP_BUF(pkt)->ack);
if (!tcp->sent_ack) { if (!tcp->sent_ack) {
rel_ack = 0; rel_ack = 0;
@ -85,12 +85,12 @@ static void net_tcp_trace(struct net_buf *buf, struct net_tcp *tcp)
rel_ack = ack ? ack - tcp->sent_ack : 0; rel_ack = ack ? ack - tcp->sent_ack : 0;
} }
NET_DBG("buf %p src %u dst %u seq 0x%04x ack 0x%04x (%u) " NET_DBG("pkt %p src %u dst %u seq 0x%04x ack 0x%04x (%u) "
"flags %c%c%c%c%c%c win %u chk 0x%04x", "flags %c%c%c%c%c%c win %u chk 0x%04x",
buf, pkt,
ntohs(NET_TCP_BUF(buf)->src_port), ntohs(NET_TCP_BUF(pkt)->src_port),
ntohs(NET_TCP_BUF(buf)->dst_port), ntohs(NET_TCP_BUF(pkt)->dst_port),
sys_get_be32(NET_TCP_BUF(buf)->seq), sys_get_be32(NET_TCP_BUF(pkt)->seq),
ack, ack,
/* This tells how many bytes we are acking now */ /* This tells how many bytes we are acking now */
rel_ack, rel_ack,
@ -100,8 +100,8 @@ static void net_tcp_trace(struct net_buf *buf, struct net_tcp *tcp)
upper_if_set('r', flags & NET_TCP_RST), upper_if_set('r', flags & NET_TCP_RST),
upper_if_set('s', flags & NET_TCP_SYN), upper_if_set('s', flags & NET_TCP_SYN),
upper_if_set('f', flags & NET_TCP_FIN), upper_if_set('f', flags & NET_TCP_FIN),
sys_get_be16(NET_TCP_BUF(buf)->wnd), sys_get_be16(NET_TCP_BUF(pkt)->wnd),
ntohs(NET_TCP_BUF(buf)->chksum)); ntohs(NET_TCP_BUF(pkt)->chksum));
} }
#else #else
#define net_tcp_trace(...) #define net_tcp_trace(...)
@ -118,30 +118,30 @@ static inline uint32_t retry_timeout(const struct net_tcp *tcp)
return ((uint32_t)1 << tcp->retry_timeout_shift) * INIT_RETRY_MS; return ((uint32_t)1 << tcp->retry_timeout_shift) * INIT_RETRY_MS;
} }
#define is_6lo_technology(buf) \ #define is_6lo_technology(pkt) \
(IS_ENABLED(CONFIG_NET_IPV6) && net_pkt_family(buf) == AF_INET6 && \ (IS_ENABLED(CONFIG_NET_IPV6) && net_pkt_family(pkt) == AF_INET6 && \
((IS_ENABLED(CONFIG_NET_L2_BLUETOOTH) && \ ((IS_ENABLED(CONFIG_NET_L2_BLUETOOTH) && \
net_pkt_ll_dst(buf)->type == NET_LINK_BLUETOOTH) || \ net_pkt_ll_dst(pkt)->type == NET_LINK_BLUETOOTH) || \
(IS_ENABLED(CONFIG_NET_L2_IEEE802154) && \ (IS_ENABLED(CONFIG_NET_L2_IEEE802154) && \
net_pkt_ll_dst(buf)->type == NET_LINK_IEEE802154))) net_pkt_ll_dst(pkt)->type == NET_LINK_IEEE802154)))
static inline void do_ref_if_needed(struct net_buf *buf) static inline void do_ref_if_needed(struct net_pkt *pkt)
{ {
/* The ref should not be done for Bluetooth and IEEE 802.15.4 which use /* The ref should not be done for Bluetooth and IEEE 802.15.4 which use
* IPv6 header compression (6lo). For BT and 802.15.4 we copy the buf * IPv6 header compression (6lo). For BT and 802.15.4 we copy the pkt
* chain we are about to send so it is fine if the network driver * chain we are about to send so it is fine if the network driver
* releases it. As we have our own copy of the sent data, we do not * releases it. As we have our own copy of the sent data, we do not
* need to take a reference of it. See also net_tcp_send_buf(). * need to take a reference of it. See also net_tcp_send_pkt().
*/ */
if (!is_6lo_technology(buf)) { if (!is_6lo_technology(pkt)) {
buf = net_pkt_ref(buf); pkt = net_pkt_ref(pkt);
} }
} }
static void tcp_retry_expired(struct k_timer *timer) static void tcp_retry_expired(struct k_timer *timer)
{ {
struct net_tcp *tcp = CONTAINER_OF(timer, struct net_tcp, retry_timer); struct net_tcp *tcp = CONTAINER_OF(timer, struct net_tcp, retry_timer);
struct net_buf *buf; struct net_pkt *pkt;
/* Double the retry period for exponential backoff and resent /* Double the retry period for exponential backoff and resent
* the first (only the first!) unack'd packet. * the first (only the first!) unack'd packet.
@ -150,12 +150,12 @@ static void tcp_retry_expired(struct k_timer *timer)
tcp->retry_timeout_shift++; tcp->retry_timeout_shift++;
k_timer_start(&tcp->retry_timer, retry_timeout(tcp), 0); k_timer_start(&tcp->retry_timer, retry_timeout(tcp), 0);
buf = CONTAINER_OF(sys_slist_peek_head(&tcp->sent_list), pkt = CONTAINER_OF(sys_slist_peek_head(&tcp->sent_list),
struct net_buf, sent_list); struct net_pkt, sent_list);
do_ref_if_needed(buf); do_ref_if_needed(pkt);
if (net_tcp_send_buf(buf) < 0 && !is_6lo_technology(buf)) { if (net_tcp_send_pkt(pkt) < 0 && !is_6lo_technology(pkt)) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} else if (IS_ENABLED(CONFIG_NET_TCP_TIME_WAIT)) { } else if (IS_ENABLED(CONFIG_NET_TCP_TIME_WAIT)) {
if (tcp->fin_sent && tcp->fin_rcvd) { if (tcp->fin_sent && tcp->fin_rcvd) {
@ -200,18 +200,18 @@ struct net_tcp *net_tcp_alloc(struct net_context *context)
int net_tcp_release(struct net_tcp *tcp) int net_tcp_release(struct net_tcp *tcp)
{ {
struct net_buf *buf; struct net_pkt *pkt;
struct net_buf *tmp; struct net_pkt *tmp;
int key; int key;
if (!PART_OF_ARRAY(tcp_context, tcp)) { if (!PART_OF_ARRAY(tcp_context, tcp)) {
return -EINVAL; return -EINVAL;
} }
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&tcp->sent_list, buf, tmp, SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&tcp->sent_list, pkt, tmp,
sent_list) { sent_list) {
sys_slist_remove(&tcp->sent_list, NULL, &buf->sent_list); sys_slist_remove(&tcp->sent_list, NULL, &pkt->sent_list);
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
k_delayed_work_cancel(&tcp->ack_timer); k_delayed_work_cancel(&tcp->ack_timer);
@ -231,7 +231,7 @@ int net_tcp_release(struct net_tcp *tcp)
} }
static inline uint8_t net_tcp_add_options(struct net_buf *header, size_t len, static inline uint8_t net_tcp_add_options(struct net_buf *header, size_t len,
void *data) void *data)
{ {
uint8_t optlen; uint8_t optlen;
@ -247,16 +247,16 @@ static inline uint8_t net_tcp_add_options(struct net_buf *header, size_t len,
return optlen; return optlen;
} }
static int finalize_segment(struct net_context *context, struct net_buf *buf) static int finalize_segment(struct net_context *context, struct net_pkt *pkt)
{ {
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
return net_ipv4_finalize(context, buf); return net_ipv4_finalize(context, pkt);
} else } else
#endif #endif
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
return net_ipv6_finalize(context, buf); return net_ipv6_finalize(context, pkt);
} }
#endif #endif
{ {
@ -265,9 +265,9 @@ static int finalize_segment(struct net_context *context, struct net_buf *buf)
return 0; return 0;
} }
static struct net_buf *prepare_segment(struct net_tcp *tcp, static struct net_pkt *prepare_segment(struct net_tcp *tcp,
struct tcp_segment *segment, struct tcp_segment *segment,
struct net_buf *buf) struct net_pkt *pkt)
{ {
struct net_buf *header, *tail = NULL; struct net_buf *header, *tail = NULL;
struct net_tcp_hdr *tcphdr; struct net_tcp_hdr *tcphdr;
@ -277,49 +277,49 @@ static struct net_buf *prepare_segment(struct net_tcp *tcp,
NET_ASSERT(context); NET_ASSERT(context);
if (buf) { if (pkt) {
/* TCP transmit data comes in with a pre-allocated /* TCP transmit data comes in with a pre-allocated
* net_pkt at the head (so that net_context_send can find * net_pkt at the head (so that net_context_send can find
* the context), and the data after. Rejigger so we * the context), and the data after. Rejigger so we
* can insert a TCP header cleanly * can insert a TCP header cleanly
*/ */
tail = buf->frags; tail = pkt->frags;
buf->frags = NULL; pkt->frags = NULL;
} else { } else {
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
} }
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
if (net_pkt_family(buf) == AF_INET) { if (net_pkt_family(pkt) == AF_INET) {
net_ipv4_create(context, buf, net_ipv4_create(context, pkt,
net_sin_ptr(segment->src_addr)->sin_addr, net_sin_ptr(segment->src_addr)->sin_addr,
&(net_sin(segment->dst_addr)->sin_addr)); &(net_sin(segment->dst_addr)->sin_addr));
dst_port = net_sin(segment->dst_addr)->sin_port; dst_port = net_sin(segment->dst_addr)->sin_port;
src_port = ((struct sockaddr_in_ptr *)&context->local)-> src_port = ((struct sockaddr_in_ptr *)&context->local)->
sin_port; sin_port;
NET_IPV4_BUF(buf)->proto = IPPROTO_TCP; NET_IPV4_BUF(pkt)->proto = IPPROTO_TCP;
} else } else
#endif #endif
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
if (net_pkt_family(buf) == AF_INET6) { if (net_pkt_family(pkt) == AF_INET6) {
net_ipv6_create(tcp->context, buf, net_ipv6_create(tcp->context, pkt,
net_sin6_ptr(segment->src_addr)->sin6_addr, net_sin6_ptr(segment->src_addr)->sin6_addr,
&(net_sin6(segment->dst_addr)->sin6_addr)); &(net_sin6(segment->dst_addr)->sin6_addr));
dst_port = net_sin6(segment->dst_addr)->sin6_port; dst_port = net_sin6(segment->dst_addr)->sin6_port;
src_port = ((struct sockaddr_in6_ptr *)&context->local)-> src_port = ((struct sockaddr_in6_ptr *)&context->local)->
sin6_port; sin6_port;
NET_IPV6_BUF(buf)->nexthdr = IPPROTO_TCP; NET_IPV6_BUF(pkt)->nexthdr = IPPROTO_TCP;
} else } else
#endif #endif
{ {
NET_DBG("Protocol family %d not supported", NET_DBG("Protocol family %d not supported",
net_pkt_family(buf)); net_pkt_family(pkt));
net_pkt_unref(buf); net_pkt_unref(pkt);
return NULL; return NULL;
} }
header = net_pkt_get_data(context, K_FOREVER); header = net_pkt_get_data(context, K_FOREVER);
net_buf_frag_add(buf, header); net_pkt_frag_add(pkt, header);
tcphdr = (struct net_tcp_hdr *)net_buf_add(header, NET_TCPH_LEN); tcphdr = (struct net_tcp_hdr *)net_buf_add(header, NET_TCPH_LEN);
@ -340,17 +340,17 @@ static struct net_buf *prepare_segment(struct net_tcp *tcp,
tcphdr->urg[1] = 0; tcphdr->urg[1] = 0;
if (tail) { if (tail) {
net_buf_frag_add(buf, tail); net_pkt_frag_add(pkt, tail);
} }
if (finalize_segment(context, buf) < 0) { if (finalize_segment(context, pkt) < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return NULL; return NULL;
} }
net_tcp_trace(buf, tcp); net_tcp_trace(pkt, tcp);
return buf; return pkt;
} }
static inline uint32_t get_recv_wnd(struct net_tcp *tcp) static inline uint32_t get_recv_wnd(struct net_tcp *tcp)
@ -379,7 +379,7 @@ int net_tcp_prepare_segment(struct net_tcp *tcp, uint8_t flags,
void *options, size_t optlen, void *options, size_t optlen,
const struct sockaddr_ptr *local, const struct sockaddr_ptr *local,
const struct sockaddr *remote, const struct sockaddr *remote,
struct net_buf **send_buf) struct net_pkt **send_pkt)
{ {
uint32_t seq; uint32_t seq;
uint16_t wnd; uint16_t wnd;
@ -437,8 +437,8 @@ int net_tcp_prepare_segment(struct net_tcp *tcp, uint8_t flags,
segment.options = options; segment.options = options;
segment.optlen = optlen; segment.optlen = optlen;
*send_buf = prepare_segment(tcp, &segment, *send_buf); *send_pkt = prepare_segment(tcp, &segment, *send_pkt);
if (!*send_buf) { if (!*send_pkt) {
return -EINVAL; return -EINVAL;
} }
@ -530,7 +530,7 @@ static void net_tcp_set_syn_opt(struct net_tcp *tcp, uint8_t *options,
} }
int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote, int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote,
struct net_buf **buf) struct net_pkt **pkt)
{ {
uint8_t options[NET_TCP_MAX_OPT_SIZE]; uint8_t options[NET_TCP_MAX_OPT_SIZE];
uint8_t optionlen; uint8_t optionlen;
@ -546,7 +546,7 @@ int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote,
return net_tcp_prepare_segment(tcp, NET_TCP_SYN | NET_TCP_ACK, return net_tcp_prepare_segment(tcp, NET_TCP_SYN | NET_TCP_ACK,
options, optionlen, NULL, remote, options, optionlen, NULL, remote,
buf); pkt);
case NET_TCP_FIN_WAIT_1: case NET_TCP_FIN_WAIT_1:
case NET_TCP_LAST_ACK: case NET_TCP_LAST_ACK:
/* In the FIN_WAIT_1 and LAST_ACK states acknowledgment must /* In the FIN_WAIT_1 and LAST_ACK states acknowledgment must
@ -555,10 +555,10 @@ int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote,
tcp->send_seq--; tcp->send_seq--;
return net_tcp_prepare_segment(tcp, NET_TCP_FIN | NET_TCP_ACK, return net_tcp_prepare_segment(tcp, NET_TCP_FIN | NET_TCP_ACK,
0, 0, NULL, remote, buf); 0, 0, NULL, remote, pkt);
default: default:
return net_tcp_prepare_segment(tcp, NET_TCP_ACK, 0, 0, NULL, return net_tcp_prepare_segment(tcp, NET_TCP_ACK, 0, 0, NULL,
remote, buf); remote, pkt);
} }
return -EINVAL; return -EINVAL;
@ -566,7 +566,7 @@ int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote,
int net_tcp_prepare_reset(struct net_tcp *tcp, int net_tcp_prepare_reset(struct net_tcp *tcp,
const struct sockaddr *remote, const struct sockaddr *remote,
struct net_buf **buf) struct net_pkt **pkt)
{ {
struct tcp_segment segment = { 0 }; struct tcp_segment segment = { 0 };
@ -590,7 +590,7 @@ int net_tcp_prepare_reset(struct net_tcp *tcp,
segment.options = NULL; segment.options = NULL;
segment.optlen = 0; segment.optlen = 0;
*buf = prepare_segment(tcp, &segment, NULL); *pkt = prepare_segment(tcp, &segment, NULL);
} }
return 0; return 0;
@ -630,10 +630,10 @@ const char * const net_tcp_state_str(enum net_tcp_state state)
return ""; return "";
} }
int net_tcp_queue_data(struct net_context *context, struct net_buf *buf) int net_tcp_queue_data(struct net_context *context, struct net_pkt *pkt)
{ {
struct net_conn *conn = (struct net_conn *)context->conn_handler; struct net_conn *conn = (struct net_conn *)context->conn_handler;
size_t data_len = net_buf_frags_len(buf); size_t data_len = net_pkt_get_len(pkt);
int ret; int ret;
/* Set PSH on all packets, our window is so small that there's /* Set PSH on all packets, our window is so small that there's
@ -641,14 +641,14 @@ int net_tcp_queue_data(struct net_context *context, struct net_buf *buf)
* coalesce packets. * coalesce packets.
*/ */
ret = net_tcp_prepare_segment(context->tcp, NET_TCP_PSH | NET_TCP_ACK, ret = net_tcp_prepare_segment(context->tcp, NET_TCP_PSH | NET_TCP_ACK,
NULL, 0, NULL, &conn->remote_addr, &buf); NULL, 0, NULL, &conn->remote_addr, &pkt);
if (ret) { if (ret) {
return ret; return ret;
} }
context->tcp->send_seq += data_len; context->tcp->send_seq += data_len;
sys_slist_append(&context->tcp->sent_list, &buf->sent_list); sys_slist_append(&context->tcp->sent_list, &pkt->sent_list);
/* We need to restart retry_timer if it is stopped. */ /* We need to restart retry_timer if it is stopped. */
if (k_timer_remaining_get(&context->tcp->retry_timer) == 0) { if (k_timer_remaining_get(&context->tcp->retry_timer) == 0) {
@ -656,15 +656,15 @@ int net_tcp_queue_data(struct net_context *context, struct net_buf *buf)
retry_timeout(context->tcp), 0); retry_timeout(context->tcp), 0);
} }
do_ref_if_needed(buf); do_ref_if_needed(pkt);
return 0; return 0;
} }
int net_tcp_send_buf(struct net_buf *buf) int net_tcp_send_pkt(struct net_pkt *pkt)
{ {
struct net_context *ctx = net_pkt_context(buf); struct net_context *ctx = net_pkt_context(pkt);
struct net_tcp_hdr *tcphdr = NET_TCP_BUF(buf); struct net_tcp_hdr *tcphdr = NET_TCP_BUF(pkt);
sys_put_be32(ctx->tcp->send_ack, tcphdr->ack); sys_put_be32(ctx->tcp->send_ack, tcphdr->ack);
@ -683,58 +683,58 @@ int net_tcp_send_buf(struct net_buf *buf)
ctx->tcp->sent_ack = ctx->tcp->send_ack; ctx->tcp->sent_ack = ctx->tcp->send_ack;
net_pkt_set_buf_sent(buf, true); net_pkt_set_sent(pkt, true);
/* We must have special handling for some network technologies that /* We must have special handling for some network technologies that
* tweak the IP protocol headers during packet sending. This happens * tweak the IP protocol headers during packet sending. This happens
* with Bluetooth and IEEE 802.15.4 which use IPv6 header compression * with Bluetooth and IEEE 802.15.4 which use IPv6 header compression
* (6lo) and alter the sent network buffer. So in order to avoid any * (6lo) and alter the sent network packet. So in order to avoid any
* corruption of the original data buffer, we must copy the sent data. * corruption of the original data buffer, we must copy the sent data.
* For Bluetooth, its fragmentation code will even mangle the data * For Bluetooth, its fragmentation code will even mangle the data
* part of the message so we need to copy those too. * part of the message so we need to copy those too.
*/ */
if (is_6lo_technology(buf)) { if (is_6lo_technology(pkt)) {
struct net_buf *new_buf, *check_buf; struct net_pkt *new_pkt, *check_pkt;
int ret; int ret;
bool buf_in_slist = false; bool pkt_in_slist = false;
/* /*
* There are users of this function that don't add buf to TCP * There are users of this function that don't add pkt to TCP
* sent_list. (See send_ack() in net_context.c) In these cases, * sent_list. (See send_ack() in net_context.c) In these cases,
* we should avoid the extra 6lowpan specific buffer copy * we should avoid the extra 6lowpan specific buffer copy
* below. * below.
*/ */
SYS_SLIST_FOR_EACH_CONTAINER(&ctx->tcp->sent_list, SYS_SLIST_FOR_EACH_CONTAINER(&ctx->tcp->sent_list,
check_buf, sent_list) { check_pkt, sent_list) {
if (check_buf == buf) { if (check_pkt == pkt) {
buf_in_slist = true; pkt_in_slist = true;
break; break;
} }
} }
if (buf_in_slist) { if (pkt_in_slist) {
new_buf = net_pkt_get_tx(ctx, K_FOREVER); new_pkt = net_pkt_get_tx(ctx, K_FOREVER);
new_buf->frags = net_pkt_copy_all(buf, 0, K_FOREVER); memcpy(new_pkt, pkt, sizeof(struct net_pkt));
net_pkt_copy_user_data(new_buf, buf); new_pkt->frags = net_pkt_copy_all(pkt, 0, K_FOREVER);
NET_DBG("Copied %zu bytes from %p to %p", NET_DBG("Copied %zu bytes from %p to %p",
net_buf_frags_len(new_buf), buf, new_buf); net_pkt_get_len(new_pkt), pkt, new_pkt);
/* This function is called from net_context.c and if we /* This function is called from net_context.c and if we
* return < 0, the caller will unref the original buf. * return < 0, the caller will unref the original pkt.
* This would leak the new_buf so remove it here. * This would leak the new_pkt so remove it here.
*/ */
ret = net_send_data(new_buf); ret = net_send_data(new_pkt);
if (ret < 0) { if (ret < 0) {
net_pkt_unref(new_buf); net_pkt_unref(new_pkt);
} }
return ret; return ret;
} }
} }
return net_send_data(buf); return net_send_data(pkt);
} }
static void restart_timer(struct net_tcp *tcp) static void restart_timer(struct net_tcp *tcp)
@ -759,16 +759,16 @@ static void restart_timer(struct net_tcp *tcp)
int net_tcp_send_data(struct net_context *context) int net_tcp_send_data(struct net_context *context)
{ {
struct net_buf *buf; struct net_pkt *pkt;
/* For now, just send all queued data synchronously. Need to /* For now, just send all queued data synchronously. Need to
* add window handling and retry/ACK logic. * add window handling and retry/ACK logic.
*/ */
SYS_SLIST_FOR_EACH_CONTAINER(&context->tcp->sent_list, buf, sent_list) { SYS_SLIST_FOR_EACH_CONTAINER(&context->tcp->sent_list, pkt, sent_list) {
if (!net_pkt_buf_sent(buf)) { if (!net_pkt_sent(pkt)) {
if (net_tcp_send_buf(buf) < 0 && if (net_tcp_send_pkt(pkt) < 0 &&
!is_6lo_technology(buf)) { !is_6lo_technology(pkt)) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
} }
} }
@ -781,17 +781,17 @@ void net_tcp_ack_received(struct net_context *ctx, uint32_t ack)
struct net_tcp *tcp = ctx->tcp; struct net_tcp *tcp = ctx->tcp;
sys_slist_t *list = &ctx->tcp->sent_list; sys_slist_t *list = &ctx->tcp->sent_list;
sys_snode_t *head; sys_snode_t *head;
struct net_buf *buf; struct net_pkt *pkt;
struct net_tcp_hdr *tcphdr; struct net_tcp_hdr *tcphdr;
uint32_t seq; uint32_t seq;
bool valid_ack = false; bool valid_ack = false;
while (!sys_slist_is_empty(list)) { while (!sys_slist_is_empty(list)) {
head = sys_slist_peek_head(list); head = sys_slist_peek_head(list);
buf = CONTAINER_OF(head, struct net_buf, sent_list); pkt = CONTAINER_OF(head, struct net_pkt, sent_list);
tcphdr = NET_TCP_BUF(buf); tcphdr = NET_TCP_BUF(pkt);
seq = sys_get_be32(tcphdr->seq) + net_pkt_appdatalen(buf) - 1; seq = sys_get_be32(tcphdr->seq) + net_pkt_appdatalen(pkt) - 1;
if (!seq_greater(ack, seq)) { if (!seq_greater(ack, seq)) {
break; break;
@ -808,7 +808,7 @@ void net_tcp_ack_received(struct net_context *ctx, uint32_t ack)
} }
sys_slist_remove(list, NULL, head); sys_slist_remove(list, NULL, head);
net_pkt_unref(buf); net_pkt_unref(pkt);
valid_ack = true; valid_ack = true;
} }
@ -827,13 +827,13 @@ void net_tcp_ack_received(struct net_context *ctx, uint32_t ack)
* pipe is uncorked again. * pipe is uncorked again.
*/ */
if (ctx->tcp->flags & NET_TCP_RETRYING) { if (ctx->tcp->flags & NET_TCP_RETRYING) {
struct net_buf *buf; struct net_pkt *pkt;
SYS_SLIST_FOR_EACH_CONTAINER(&ctx->tcp->sent_list, buf, SYS_SLIST_FOR_EACH_CONTAINER(&ctx->tcp->sent_list, pkt,
sent_list) { sent_list) {
if (net_pkt_buf_sent(buf)) { if (net_pkt_sent(pkt)) {
do_ref_if_needed(buf); do_ref_if_needed(pkt);
net_pkt_set_buf_sent(buf, false); net_pkt_set_sent(pkt, false);
} }
} }

View file

@ -234,7 +234,7 @@ int net_tcp_release(struct net_tcp *tcp);
* @param local Source address, or NULL to use the local address of * @param local Source address, or NULL to use the local address of
* the TCP context * the TCP context
* @param remote Peer address * @param remote Peer address
* @param send_buf Full IP + TCP header that is to be sent. * @param send_pkt Full IP + TCP header that is to be sent.
* *
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
@ -242,31 +242,31 @@ int net_tcp_prepare_segment(struct net_tcp *tcp, uint8_t flags,
void *options, size_t optlen, void *options, size_t optlen,
const struct sockaddr_ptr *local, const struct sockaddr_ptr *local,
const struct sockaddr *remote, const struct sockaddr *remote,
struct net_buf **send_buf); struct net_pkt **send_pkt);
/** /**
* @brief Prepare a TCP ACK message that can be send to peer. * @brief Prepare a TCP ACK message that can be send to peer.
* *
* @param tcp TCP context * @param tcp TCP context
* @param remote Peer address * @param remote Peer address
* @param buf Network buffer * @param pkt Network buffer
* *
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote, int net_tcp_prepare_ack(struct net_tcp *tcp, const struct sockaddr *remote,
struct net_buf **buf); struct net_pkt **pkt);
/** /**
* @brief Prepare a TCP RST message that can be send to peer. * @brief Prepare a TCP RST message that can be send to peer.
* *
* @param tcp TCP context * @param tcp TCP context
* @param remote Peer address * @param remote Peer address
* @param buf Network buffer * @param pkt Network buffer
* *
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
int net_tcp_prepare_reset(struct net_tcp *tcp, const struct sockaddr *remote, int net_tcp_prepare_reset(struct net_tcp *tcp, const struct sockaddr *remote,
struct net_buf **buf); struct net_pkt **pkt);
typedef void (*net_tcp_cb_t)(struct net_tcp *tcp, void *user_data); typedef void (*net_tcp_cb_t)(struct net_tcp *tcp, void *user_data);
@ -292,19 +292,19 @@ int net_tcp_send_data(struct net_context *context);
* @brief Enqueue a single packet for transmission * @brief Enqueue a single packet for transmission
* *
* @param context TCP context * @param context TCP context
* @param buf Packet * @param pkt Packet
* *
* @return 0 if ok, < 0 if error * @return 0 if ok, < 0 if error
*/ */
int net_tcp_queue_data(struct net_context *context, struct net_buf *buf); int net_tcp_queue_data(struct net_context *context, struct net_pkt *pkt);
/** /**
* @brief Sends one TCP packet initialized with the _prepare_*() * @brief Sends one TCP packet initialized with the _prepare_*()
* family of functions. * family of functions.
* *
* @param buf Packet * @param pkt Packet
*/ */
int net_tcp_send_buf(struct net_buf *buf); int net_tcp_send_pkt(struct net_pkt *pkt);
/** /**
* @brief Handle a received TCP ACK * @brief Handle a received TCP ACK

View file

@ -28,53 +28,53 @@ extern "C" {
#if defined(CONFIG_NET_UDP) #if defined(CONFIG_NET_UDP)
/** /**
* @brief Append UDP packet into net_buf * @brief Append UDP packet into net_pkt
* *
* @param buf Network buffer * @param pkt Network packet
* @param src_port Source port in host byte order. * @param src_port Source port in host byte order.
* @param dst_port Destination port in host byte order. * @param dst_port Destination port in host byte order.
* *
* @return Return network buffer that contains the UDP packet. * @return Return network packet that contains the UDP packet.
*/ */
static inline struct net_buf *net_udp_append_raw(struct net_buf *buf, static inline struct net_pkt *net_udp_append_raw(struct net_pkt *pkt,
uint16_t src_port, uint16_t src_port,
uint16_t dst_port) uint16_t dst_port)
{ {
NET_UDP_BUF(buf)->src_port = htons(src_port); NET_UDP_BUF(pkt)->src_port = htons(src_port);
NET_UDP_BUF(buf)->dst_port = htons(dst_port); NET_UDP_BUF(pkt)->dst_port = htons(dst_port);
net_buf_add(buf->frags, sizeof(struct net_udp_hdr)); net_buf_add(pkt->frags, sizeof(struct net_udp_hdr));
NET_UDP_BUF(buf)->len = htons(net_buf_frags_len(buf) - NET_UDP_BUF(pkt)->len = htons(net_pkt_get_len(pkt) -
net_pkt_ip_hdr_len(buf)); net_pkt_ip_hdr_len(pkt));
net_pkt_set_appdata(buf, net_pkt_udp_data(buf) + net_pkt_set_appdata(pkt, net_pkt_udp_data(pkt) +
sizeof(struct net_udp_hdr)); sizeof(struct net_udp_hdr));
return buf; return pkt;
} }
/** /**
* @brief Append UDP packet into net_buf * @brief Append UDP packet into net_pkt
* *
* @param context Network context for a connection * @param context Network context for a connection
* @param buf Network buffer * @param pkt Network packet
* @param port Destination port in host byte order. * @param port Destination port in host byte order.
* *
* @return Return network buffer that contains the UDP packet. * @return Return network packet that contains the UDP packet.
*/ */
static inline struct net_buf *net_udp_append(struct net_context *context, static inline struct net_pkt *net_udp_append(struct net_context *context,
struct net_buf *buf, struct net_pkt *pkt,
uint16_t port) uint16_t port)
{ {
return net_udp_append_raw(buf, return net_udp_append_raw(pkt,
ntohs(net_sin((struct sockaddr *) ntohs(net_sin((struct sockaddr *)
&context->local)->sin_port), &context->local)->sin_port),
port); port);
} }
#else #else
#define net_udp_append_raw(buf, src_port, dst_port) (buf) #define net_udp_append_raw(pkt, src_port, dst_port) (pkt)
#define net_udp_append(context, buf, port) (buf) #define net_udp_append(context, pkt, port) (pkt)
#endif /* CONFIG_NET_UDP */ #endif /* CONFIG_NET_UDP */
/** /**

View file

@ -400,12 +400,12 @@ static uint16_t calc_chksum(uint16_t sum, const uint8_t *ptr, uint16_t len)
return sum; return sum;
} }
static inline uint16_t calc_chksum_buf(uint16_t sum, struct net_buf *buf, static inline uint16_t calc_chksum_pkt(uint16_t sum, struct net_pkt *pkt,
uint16_t upper_layer_len) uint16_t upper_layer_len)
{ {
struct net_buf *frag = buf->frags; struct net_buf *frag = pkt->frags;
uint16_t proto_len = net_pkt_ip_hdr_len(buf) + uint16_t proto_len = net_pkt_ip_hdr_len(pkt) +
net_pkt_ext_len(buf); net_pkt_ext_len(pkt);
int16_t len = frag->len - proto_len; int16_t len = frag->len - proto_len;
uint8_t *ptr = frag->data + proto_len; uint8_t *ptr = frag->data + proto_len;
@ -443,45 +443,45 @@ static inline uint16_t calc_chksum_buf(uint16_t sum, struct net_buf *buf,
return sum; return sum;
} }
uint16_t net_calc_chksum(struct net_buf *buf, uint8_t proto) uint16_t net_calc_chksum(struct net_pkt *pkt, uint8_t proto)
{ {
uint16_t upper_layer_len; uint16_t upper_layer_len;
uint16_t sum; uint16_t sum;
switch (net_pkt_family(buf)) { switch (net_pkt_family(pkt)) {
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
case AF_INET: case AF_INET:
upper_layer_len = (NET_IPV4_BUF(buf)->len[0] << 8) + upper_layer_len = (NET_IPV4_BUF(pkt)->len[0] << 8) +
NET_IPV4_BUF(buf)->len[1] - NET_IPV4_BUF(pkt)->len[1] -
net_pkt_ext_len(buf) - net_pkt_ext_len(pkt) -
net_pkt_ip_hdr_len(buf); net_pkt_ip_hdr_len(pkt);
if (proto == IPPROTO_ICMP) { if (proto == IPPROTO_ICMP) {
return htons(calc_chksum(0, net_pkt_ip_data(buf) + return htons(calc_chksum(0, net_pkt_ip_data(pkt) +
net_pkt_ip_hdr_len(buf), net_pkt_ip_hdr_len(pkt),
upper_layer_len)); upper_layer_len));
} else { } else {
sum = calc_chksum(upper_layer_len + proto, sum = calc_chksum(upper_layer_len + proto,
(uint8_t *)&NET_IPV4_BUF(buf)->src, (uint8_t *)&NET_IPV4_BUF(pkt)->src,
2 * sizeof(struct in_addr)); 2 * sizeof(struct in_addr));
} }
break; break;
#endif #endif
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
case AF_INET6: case AF_INET6:
upper_layer_len = (NET_IPV6_BUF(buf)->len[0] << 8) + upper_layer_len = (NET_IPV6_BUF(pkt)->len[0] << 8) +
NET_IPV6_BUF(buf)->len[1] - net_pkt_ext_len(buf); NET_IPV6_BUF(pkt)->len[1] - net_pkt_ext_len(pkt);
sum = calc_chksum(upper_layer_len + proto, sum = calc_chksum(upper_layer_len + proto,
(uint8_t *)&NET_IPV6_BUF(buf)->src, (uint8_t *)&NET_IPV6_BUF(pkt)->src,
2 * sizeof(struct in6_addr)); 2 * sizeof(struct in6_addr));
break; break;
#endif #endif
default: default:
NET_DBG("Unknown protocol family %d", net_pkt_family(buf)); NET_DBG("Unknown protocol family %d", net_pkt_family(pkt));
return 0; return 0;
} }
sum = calc_chksum_buf(sum, buf, upper_layer_len); sum = calc_chksum_pkt(sum, pkt, upper_layer_len);
sum = (sum == 0) ? 0xffff : htons(sum); sum = (sum == 0) ? 0xffff : htons(sum);
@ -489,11 +489,11 @@ uint16_t net_calc_chksum(struct net_buf *buf, uint8_t proto)
} }
#if defined(CONFIG_NET_IPV4) #if defined(CONFIG_NET_IPV4)
uint16_t net_calc_chksum_ipv4(struct net_buf *buf) uint16_t net_calc_chksum_ipv4(struct net_pkt *pkt)
{ {
uint16_t sum; uint16_t sum;
sum = calc_chksum(0, (uint8_t *)NET_IPV4_BUF(buf), NET_IPV4H_LEN); sum = calc_chksum(0, (uint8_t *)NET_IPV4_BUF(pkt), NET_IPV4H_LEN);
sum = (sum == 0) ? 0xffff : htons(sum); sum = (sum == 0) ? 0xffff : htons(sum);

View file

@ -358,7 +358,7 @@ static inline int get_slot_by_id(struct dns_resolve_context *ctx,
} }
static int dns_read(struct dns_resolve_context *ctx, static int dns_read(struct dns_resolve_context *ctx,
struct net_buf *buf, struct net_pkt *pkt,
struct net_buf *dns_data, struct net_buf *dns_data,
uint16_t *dns_id, uint16_t *dns_id,
struct net_buf *dns_cname, struct net_buf *dns_cname,
@ -377,12 +377,12 @@ static int dns_read(struct dns_resolve_context *ctx,
int ret; int ret;
int server_idx, query_idx; int server_idx, query_idx;
data_len = min(net_pkt_appdatalen(buf), DNS_RESOLVER_MAX_BUF_SIZE); data_len = min(net_pkt_appdatalen(pkt), DNS_RESOLVER_MAX_BUF_SIZE);
offset = net_buf_frags_len(buf) - data_len; offset = net_pkt_get_len(pkt) - data_len;
/* TODO: Instead of this temporary copy, just use the net_buf directly. /* TODO: Instead of this temporary copy, just use the net_pkt directly.
*/ */
ret = net_pkt_linear_copy(dns_data, buf, offset, data_len); ret = net_frag_linear_copy(dns_data, pkt->frags, offset, data_len);
if (ret < 0) { if (ret < 0) {
ret = DNS_EAI_MEMORY; ret = DNS_EAI_MEMORY;
goto quit; goto quit;
@ -526,7 +526,7 @@ static int dns_read(struct dns_resolve_context *ctx,
ctx->queries[query_idx].cb = NULL; ctx->queries[query_idx].cb = NULL;
net_pkt_unref(buf); net_pkt_unref(pkt);
return 0; return 0;
@ -534,13 +534,13 @@ finished:
dns_resolve_cancel(ctx, *dns_id); dns_resolve_cancel(ctx, *dns_id);
quit: quit:
net_pkt_unref(buf); net_pkt_unref(pkt);
return ret; return ret;
} }
static void cb_recv(struct net_context *net_ctx, static void cb_recv(struct net_context *net_ctx,
struct net_buf *buf, struct net_pkt *pkt,
int status, int status,
void *user_data) void *user_data)
{ {
@ -570,7 +570,7 @@ static void cb_recv(struct net_context *net_ctx,
goto quit; goto quit;
} }
ret = dns_read(ctx, buf, dns_data, &dns_id, dns_cname, &info); ret = dns_read(ctx, pkt, dns_data, &dns_id, dns_cname, &info);
if (!ret) { if (!ret) {
/* We called the callback already in dns_read() if there /* We called the callback already in dns_read() if there
* was no errors. * was no errors.
@ -643,7 +643,7 @@ static int dns_write(struct dns_resolve_context *ctx,
enum dns_query_type query_type; enum dns_query_type query_type;
struct net_context *net_ctx; struct net_context *net_ctx;
struct sockaddr *server; struct sockaddr *server;
struct net_buf *buf; struct net_pkt *pkt;
int server_addr_len; int server_addr_len;
uint16_t dns_id; uint16_t dns_id;
int ret; int ret;
@ -661,13 +661,13 @@ static int dns_write(struct dns_resolve_context *ctx,
goto quit; goto quit;
} }
buf = net_pkt_get_tx(net_ctx, ctx->buf_timeout); pkt = net_pkt_get_tx(net_ctx, ctx->buf_timeout);
if (!buf) { if (!pkt) {
ret = -ENOMEM; ret = -ENOMEM;
goto quit; goto quit;
} }
ret = net_pkt_append(buf, dns_data->len, dns_data->data, ret = net_pkt_append(pkt, dns_data->len, dns_data->data,
ctx->buf_timeout); ctx->buf_timeout);
if (ret < 0) { if (ret < 0) {
ret = -ENOMEM; ret = -ENOMEM;
@ -682,11 +682,11 @@ static int dns_write(struct dns_resolve_context *ctx,
net_context_recv(net_ctx, cb_recv, K_NO_WAIT, ctx); net_context_recv(net_ctx, cb_recv, K_NO_WAIT, ctx);
ret = net_context_sendto(buf, server, server_addr_len, NULL, ret = net_context_sendto(pkt, server, server_addr_len, NULL,
K_NO_WAIT, NULL, NULL); K_NO_WAIT, NULL, NULL);
if (ret < 0) { if (ret < 0) {
NET_DBG("Cannot send query (%d)", ret); NET_DBG("Cannot send query (%d)", ret);
net_pkt_unref(buf); net_pkt_unref(pkt);
goto quit; goto quit;
} }

View file

@ -19,7 +19,7 @@
int http_request(struct net_context *net_ctx, int32_t timeout, int http_request(struct net_context *net_ctx, int32_t timeout,
struct http_client_request *req) struct http_client_request *req)
{ {
struct net_buf *tx; struct net_pkt *tx;
int rc = -ENOMEM; int rc = -ENOMEM;
tx = net_pkt_get_tx(net_ctx, timeout); tx = net_pkt_get_tx(net_ctx, timeout);
@ -97,7 +97,7 @@ int http_request(struct net_context *net_ctx, int32_t timeout,
return net_context_send(tx, NULL, timeout, NULL, NULL); return net_context_send(tx, NULL, timeout, NULL, NULL);
lb_exit: lb_exit:
net_buf_unref(tx); net_pkt_unref(tx);
return rc; return rc;
} }

View file

@ -32,7 +32,7 @@ static inline uint16_t http_strlen(const char *str)
return 0; return 0;
} }
static int http_add_header(struct net_buf *tx, int32_t timeout, const char *str) static int http_add_header(struct net_pkt *tx, int32_t timeout, const char *str)
{ {
if (net_pkt_append(tx, strlen(str), (uint8_t *)str, timeout)) { if (net_pkt_append(tx, strlen(str), (uint8_t *)str, timeout)) {
return 0; return 0;
@ -41,7 +41,7 @@ static int http_add_header(struct net_buf *tx, int32_t timeout, const char *str)
return -ENOMEM; return -ENOMEM;
} }
static int http_add_chunk(struct net_buf *tx, int32_t timeout, const char *str) static int http_add_chunk(struct net_pkt *tx, int32_t timeout, const char *str)
{ {
char chunk_header[16]; char chunk_header[16];
char *rn = "\r\n"; char *rn = "\r\n";
@ -71,12 +71,12 @@ static int http_add_chunk(struct net_buf *tx, int32_t timeout, const char *str)
int http_response(struct http_server_ctx *ctx, const char *http_header, int http_response(struct http_server_ctx *ctx, const char *http_header,
const char *html_payload) const char *html_payload)
{ {
struct net_buf *tx; struct net_pkt *tx;
int rc = -EINVAL; int rc = -EINVAL;
tx = net_pkt_get_tx(ctx->net_ctx, ctx->timeout); tx = net_pkt_get_tx(ctx->net_ctx, ctx->timeout);
if (!tx) { if (!tx) {
goto exit_routine; return rc;
} }
rc = http_add_header(tx, ctx->timeout, http_header); rc = http_add_header(tx, ctx->timeout, http_header);
@ -105,7 +105,6 @@ int http_response(struct http_server_ctx *ctx, const char *http_header,
tx = NULL; tx = NULL;
exit_routine: exit_routine:
/* unref can handle NULL buffers, so we are covered */
net_pkt_unref(tx); net_pkt_unref(tx);
return rc; return rc;

View file

@ -26,7 +26,7 @@ NET_BUF_POOL_DEFINE(mqtt_msg_pool, MQTT_BUF_CTR, MSG_SIZE, 0, NULL);
int mqtt_tx_connect(struct mqtt_ctx *ctx, struct mqtt_connect_msg *msg) int mqtt_tx_connect(struct mqtt_ctx *ctx, struct mqtt_connect_msg *msg)
{ {
struct net_buf *data = NULL; struct net_buf *data = NULL;
struct net_buf *tx = NULL; struct net_pkt *tx = NULL;
int rc; int rc;
data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout); data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout);
@ -49,7 +49,7 @@ int mqtt_tx_connect(struct mqtt_ctx *ctx, struct mqtt_connect_msg *msg)
goto exit_connect; goto exit_connect;
} }
net_buf_frag_add(tx, data); net_pkt_frag_add(tx, data);
data = NULL; data = NULL;
rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL); rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL);
@ -61,7 +61,7 @@ int mqtt_tx_connect(struct mqtt_ctx *ctx, struct mqtt_connect_msg *msg)
tx = NULL; tx = NULL;
exit_connect: exit_connect:
net_pkt_unref(data); net_pkt_frag_unref(data);
net_pkt_unref(tx); net_pkt_unref(tx);
return rc; return rc;
@ -69,7 +69,7 @@ exit_connect:
int mqtt_tx_disconnect(struct mqtt_ctx *ctx) int mqtt_tx_disconnect(struct mqtt_ctx *ctx)
{ {
struct net_buf *tx = NULL; struct net_pkt *tx = NULL;
/* DISCONNECT is a zero length message: 2 bytes required, no payload */ /* DISCONNECT is a zero length message: 2 bytes required, no payload */
uint8_t msg[2]; uint8_t msg[2];
uint16_t len; uint16_t len;
@ -121,14 +121,14 @@ exit_disconnect:
* *
* @retval 0 on success * @retval 0 on success
* @retval -EINVAL * @retval -EINVAL
* @retval -ENOMEM if a tx buffer is not available * @retval -ENOMEM if a tx pktfer is not available
* @retval -EIO on network error * @retval -EIO on network error
*/ */
static static
int mqtt_tx_pub_msgs(struct mqtt_ctx *ctx, uint16_t id, int mqtt_tx_pub_msgs(struct mqtt_ctx *ctx, uint16_t id,
enum mqtt_packet pkt_type) enum mqtt_packet pkt_type)
{ {
struct net_buf *tx = NULL; struct net_pkt *tx = NULL;
uint8_t msg[4]; uint8_t msg[4];
uint16_t len; uint16_t len;
int rc; int rc;
@ -203,7 +203,7 @@ int mqtt_tx_pubrel(struct mqtt_ctx *ctx, uint16_t id)
int mqtt_tx_publish(struct mqtt_ctx *ctx, struct mqtt_publish_msg *msg) int mqtt_tx_publish(struct mqtt_ctx *ctx, struct mqtt_publish_msg *msg)
{ {
struct net_buf *data = NULL; struct net_buf *data = NULL;
struct net_buf *tx = NULL; struct net_pkt *tx = NULL;
int rc; int rc;
data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout); data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout);
@ -224,7 +224,7 @@ int mqtt_tx_publish(struct mqtt_ctx *ctx, struct mqtt_publish_msg *msg)
goto exit_publish; goto exit_publish;
} }
net_buf_frag_add(tx, data); net_pkt_frag_add(tx, data);
data = NULL; data = NULL;
rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL); rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL);
@ -236,7 +236,7 @@ int mqtt_tx_publish(struct mqtt_ctx *ctx, struct mqtt_publish_msg *msg)
tx = NULL; tx = NULL;
exit_publish: exit_publish:
net_pkt_unref(data); net_pkt_frag_unref(data);
net_pkt_unref(tx); net_pkt_unref(tx);
return rc; return rc;
@ -244,7 +244,7 @@ exit_publish:
int mqtt_tx_pingreq(struct mqtt_ctx *ctx) int mqtt_tx_pingreq(struct mqtt_ctx *ctx)
{ {
struct net_buf *tx = NULL; struct net_pkt *tx = NULL;
uint8_t msg[2]; uint8_t msg[2];
uint16_t len; uint16_t len;
int rc; int rc;
@ -285,7 +285,7 @@ int mqtt_tx_subscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items,
const char *topics[], const enum mqtt_qos qos[]) const char *topics[], const enum mqtt_qos qos[])
{ {
struct net_buf *data = NULL; struct net_buf *data = NULL;
struct net_buf *tx = NULL; struct net_pkt *tx = NULL;
int rc; int rc;
data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout); data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout);
@ -297,7 +297,7 @@ int mqtt_tx_subscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items,
rc = mqtt_pack_subscribe(data->data, &data->len, data->size, rc = mqtt_pack_subscribe(data->data, &data->len, data->size,
pkt_id, items, topics, qos); pkt_id, items, topics, qos);
if (rc != 0) { if (rc != 0) {
net_pkt_unref(data); net_pkt_frag_unref(data);
rc = -EINVAL; rc = -EINVAL;
goto exit_subs; goto exit_subs;
} }
@ -308,7 +308,7 @@ int mqtt_tx_subscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items,
goto exit_subs; goto exit_subs;
} }
net_buf_frag_add(tx, data); net_pkt_frag_add(tx, data);
data = NULL; data = NULL;
rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL); rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL);
@ -320,7 +320,7 @@ int mqtt_tx_subscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items,
tx = NULL; tx = NULL;
exit_subs: exit_subs:
net_pkt_unref(data); net_pkt_frag_unref(data);
net_pkt_unref(tx); net_pkt_unref(tx);
return rc; return rc;
@ -330,7 +330,7 @@ int mqtt_tx_unsubscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items,
const char *topics[]) const char *topics[])
{ {
struct net_buf *data = NULL; struct net_buf *data = NULL;
struct net_buf *tx = NULL; struct net_pkt *tx = NULL;
int rc; int rc;
data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout); data = net_buf_alloc(&mqtt_msg_pool, ctx->net_timeout);
@ -352,7 +352,7 @@ int mqtt_tx_unsubscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items,
goto exit_unsub; goto exit_unsub;
} }
net_buf_frag_add(tx, data); net_pkt_frag_add(tx, data);
data = NULL; data = NULL;
rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL); rc = net_context_send(tx, NULL, ctx->net_timeout, NULL, NULL);
@ -364,8 +364,8 @@ int mqtt_tx_unsubscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items,
tx = NULL; tx = NULL;
exit_unsub: exit_unsub:
net_buf_unref(data); net_pkt_frag_unref(data);
net_buf_unref(tx); net_pkt_unref(tx);
return rc; return rc;
} }
@ -421,7 +421,7 @@ exit_connect:
} }
/** /**
* Parses and validates the MQTT PUBxxxx message contained in the rx buffer. * Parses and validates the MQTT PUBxxxx message contained in the rx packet.
* *
* *
* @details It validates against message structure and Packet Identifier. * @details It validates against message structure and Packet Identifier.
@ -429,7 +429,7 @@ exit_connect:
* corresponding MQTT PUB msg. * corresponding MQTT PUB msg.
* *
* @param ctx MQTT context * @param ctx MQTT context
* @param rx RX buffer * @param rx RX packet
* @param type MQTT Packet type * @param type MQTT Packet type
* *
* @retval 0 on success * @retval 0 on success
@ -629,18 +629,18 @@ int mqtt_rx_publish(struct mqtt_ctx *ctx, struct net_buf *rx)
} }
/** /**
* Linearizes an IP fragmented buffer * Linearizes an IP fragmented packet
* *
* @param [in] ctx MQTT context structure * @param [in] ctx MQTT context structure
* @param [in] rx RX IP stack buffer * @param [in] rx RX IP stack packet
* @param [in] min_size Min message size allowed. This allows us to exit if the * @param [in] min_size Min message size allowed. This allows us to exit if the
* rx buffer is shorter than the expected msg size * rx packet is shorter than the expected msg size
* *
* @retval Data buffer * @retval Data buffer
* @retval NULL on error * @retval NULL on error
*/ */
static static
struct net_buf *mqtt_linearize_buffer(struct mqtt_ctx *ctx, struct net_buf *rx, struct net_buf *mqtt_linearize_packet(struct mqtt_ctx *ctx, struct net_pkt *rx,
uint16_t min_size) uint16_t min_size)
{ {
struct net_buf *data = NULL; struct net_buf *data = NULL;
@ -649,7 +649,7 @@ struct net_buf *mqtt_linearize_buffer(struct mqtt_ctx *ctx, struct net_buf *rx,
int rc; int rc;
/* CONFIG_MQTT_MSG_MAX_SIZE is defined via Kconfig. So here it's /* CONFIG_MQTT_MSG_MAX_SIZE is defined via Kconfig. So here it's
* determined if the input buffer could fit our data buffer or if * determined if the input packet could fit our data buffer or if
* it has the expected size. * it has the expected size.
*/ */
data_len = net_pkt_appdatalen(rx); data_len = net_pkt_appdatalen(rx);
@ -662,8 +662,8 @@ struct net_buf *mqtt_linearize_buffer(struct mqtt_ctx *ctx, struct net_buf *rx,
return NULL; return NULL;
} }
offset = net_buf_frags_len(rx) - data_len; offset = net_pkt_get_len(rx) - data_len;
rc = net_pkt_linear_copy(data, rx, offset, data_len); rc = net_frag_linear_copy(data, rx->frags, offset, data_len);
if (rc != 0) { if (rc != 0) {
goto exit_error; goto exit_error;
} }
@ -671,7 +671,7 @@ struct net_buf *mqtt_linearize_buffer(struct mqtt_ctx *ctx, struct net_buf *rx,
return data; return data;
exit_error: exit_error:
net_pkt_unref(data); net_pkt_frag_unref(data);
return NULL; return NULL;
} }
@ -683,7 +683,7 @@ exit_error:
* (if defined) * (if defined)
* *
* @param ctx MQTT context * @param ctx MQTT context
* @param rx RX buffer * @param rx RX packet
* *
* @retval 0 on success * @retval 0 on success
* @retval -EINVAL if an unknown message is received * @retval -EINVAL if an unknown message is received
@ -692,13 +692,13 @@ exit_error:
* and mqtt_rx_pingresp return codes * and mqtt_rx_pingresp return codes
*/ */
static static
int mqtt_publisher_parser(struct mqtt_ctx *ctx, struct net_buf *rx) int mqtt_publisher_parser(struct mqtt_ctx *ctx, struct net_pkt *rx)
{ {
uint16_t pkt_type = MQTT_INVALID; uint16_t pkt_type = MQTT_INVALID;
struct net_buf *data = NULL; struct net_buf *data = NULL;
int rc = -EINVAL; int rc = -EINVAL;
data = mqtt_linearize_buffer(ctx, rx, MQTT_PUBLISHER_MIN_MSG_SIZE); data = mqtt_linearize_packet(ctx, rx, MQTT_PUBLISHER_MIN_MSG_SIZE);
if (!data) { if (!data) {
rc = -ENOMEM; rc = -ENOMEM;
goto exit_parser; goto exit_parser;
@ -736,7 +736,7 @@ exit_parser:
ctx->malformed(ctx, pkt_type); ctx->malformed(ctx, pkt_type);
} }
net_pkt_unref(data); net_pkt_frag_unref(data);
return rc; return rc;
} }
@ -749,7 +749,7 @@ exit_parser:
* (if defined) * (if defined)
* *
* @param ctx MQTT context * @param ctx MQTT context
* @param rx RX buffer * @param rx RX packet
* *
* @retval 0 on success * @retval 0 on success
* @retval -EINVAL if an unknown message is received * @retval -EINVAL if an unknown message is received
@ -758,13 +758,13 @@ exit_parser:
* return codes * return codes
*/ */
static static
int mqtt_subscriber_parser(struct mqtt_ctx *ctx, struct net_buf *rx) int mqtt_subscriber_parser(struct mqtt_ctx *ctx, struct net_pkt *rx)
{ {
uint16_t pkt_type = MQTT_INVALID; uint16_t pkt_type = MQTT_INVALID;
struct net_buf *data = NULL; struct net_buf *data = NULL;
int rc = 0; int rc = 0;
data = mqtt_linearize_buffer(ctx, rx, MQTT_PUBLISHER_MIN_MSG_SIZE); data = mqtt_linearize_packet(ctx, rx, MQTT_PUBLISHER_MIN_MSG_SIZE);
if (!data) { if (!data) {
rc = -EINVAL; rc = -EINVAL;
goto exit_parser; goto exit_parser;
@ -803,13 +803,13 @@ exit_parser:
ctx->malformed(ctx, pkt_type); ctx->malformed(ctx, pkt_type);
} }
net_pkt_unref(data); net_pkt_frag_unref(data);
return rc; return rc;
} }
static static
void mqtt_recv(struct net_context *net_ctx, struct net_buf *buf, int status, void mqtt_recv(struct net_context *net_ctx, struct net_pkt *pkt, int status,
void *data) void *data)
{ {
struct mqtt_ctx *mqtt = (struct mqtt_ctx *)data; struct mqtt_ctx *mqtt = (struct mqtt_ctx *)data;
@ -817,18 +817,18 @@ void mqtt_recv(struct net_context *net_ctx, struct net_buf *buf, int status,
/* net_ctx is already referenced to by the mqtt_ctx struct */ /* net_ctx is already referenced to by the mqtt_ctx struct */
ARG_UNUSED(net_ctx); ARG_UNUSED(net_ctx);
if (status || !buf) { if (status || !pkt) {
return; return;
} }
if (net_pkt_appdatalen(buf) == 0) { if (net_pkt_appdatalen(pkt) == 0) {
goto lb_exit; goto lb_exit;
} }
mqtt->rcv(mqtt, buf); mqtt->rcv(mqtt, pkt);
lb_exit: lb_exit:
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type) int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type)

View file

@ -82,7 +82,7 @@ static int decode_delta(int num, const uint8_t *buf, int16_t buflen,
return hdrlen; return hdrlen;
} }
static int coap_parse_option(const struct zoap_packet *pkt, static int coap_parse_option(const struct zoap_packet *zpkt,
struct option_context *context, struct option_context *context,
uint8_t **value, uint16_t *vlen) uint8_t **value, uint16_t *vlen)
{ {
@ -141,16 +141,16 @@ static int coap_parse_option(const struct zoap_packet *pkt,
return context->used; return context->used;
} }
static int coap_parse_options(struct zoap_packet *pkt, unsigned int offset) static int coap_parse_options(struct zoap_packet *zpkt, unsigned int offset)
{ {
struct option_context context = { struct option_context context = {
.delta = 0, .delta = 0,
.used = 0, .used = 0,
.buflen = pkt->buf->frags->len - offset, .buflen = zpkt->pkt->frags->len - offset,
.buf = &pkt->buf->frags->data[offset] }; .buf = &zpkt->pkt->frags->data[offset] };
while (true) { while (true) {
int r = coap_parse_option(pkt, &context, NULL, NULL); int r = coap_parse_option(zpkt, &context, NULL, NULL);
if (r < 0) { if (r < 0) {
return -EINVAL; return -EINVAL;
@ -163,68 +163,68 @@ static int coap_parse_options(struct zoap_packet *pkt, unsigned int offset)
return context.used; return context.used;
} }
static uint8_t coap_header_get_tkl(const struct zoap_packet *pkt) static uint8_t coap_header_get_tkl(const struct zoap_packet *zpkt)
{ {
return pkt->buf->frags->data[0] & 0xF; return zpkt->pkt->frags->data[0] & 0xF;
} }
static int coap_get_header_len(const struct zoap_packet *pkt) static int coap_get_header_len(const struct zoap_packet *zpkt)
{ {
unsigned int hdrlen; unsigned int hdrlen;
uint8_t tkl; uint8_t tkl;
hdrlen = BASIC_HEADER_SIZE; hdrlen = BASIC_HEADER_SIZE;
if (pkt->buf->frags->len < hdrlen) { if (zpkt->pkt->frags->len < hdrlen) {
return -EINVAL; return -EINVAL;
} }
tkl = coap_header_get_tkl(pkt); tkl = coap_header_get_tkl(zpkt);
/* Token lenghts 9-15 are reserved. */ /* Token lenghts 9-15 are reserved. */
if (tkl > 8) { if (tkl > 8) {
return -EINVAL; return -EINVAL;
} }
if (pkt->buf->frags->len < hdrlen + tkl) { if (zpkt->pkt->frags->len < hdrlen + tkl) {
return -EINVAL; return -EINVAL;
} }
return hdrlen + tkl; return hdrlen + tkl;
} }
int zoap_packet_parse(struct zoap_packet *pkt, struct net_buf *buf) int zoap_packet_parse(struct zoap_packet *zpkt, struct net_pkt *pkt)
{ {
int optlen, hdrlen; int optlen, hdrlen;
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
return -EINVAL; return -EINVAL;
} }
memset(pkt, 0, sizeof(*pkt)); memset(zpkt, 0, sizeof(*zpkt));
pkt->buf = buf; zpkt->pkt = pkt;
hdrlen = coap_get_header_len(pkt); hdrlen = coap_get_header_len(zpkt);
if (hdrlen < 0) { if (hdrlen < 0) {
return -EINVAL; return -EINVAL;
} }
optlen = coap_parse_options(pkt, hdrlen); optlen = coap_parse_options(zpkt, hdrlen);
if (optlen < 0) { if (optlen < 0) {
return -EINVAL; return -EINVAL;
} }
if (buf->frags->len < hdrlen + optlen) { if (pkt->frags->len < hdrlen + optlen) {
return -EINVAL; return -EINVAL;
} }
if (buf->frags->len <= hdrlen + optlen + 1) { if (pkt->frags->len <= hdrlen + optlen + 1) {
pkt->start = NULL; zpkt->start = NULL;
return 0; return 0;
} }
pkt->start = buf->frags->data + hdrlen + optlen + 1; zpkt->start = pkt->frags->data + hdrlen + optlen + 1;
pkt->total_size = buf->frags->len; zpkt->total_size = pkt->frags->len;
return 0; return 0;
} }
@ -298,30 +298,30 @@ static int coap_option_encode(struct option_context *context, uint16_t code,
return offset + len; return offset + len;
} }
int zoap_packet_init(struct zoap_packet *pkt, int zoap_packet_init(struct zoap_packet *zpkt,
struct net_buf *buf) struct net_pkt *pkt)
{ {
void *data; void *data;
if (!buf || !buf->frags) { if (!pkt || !pkt->frags) {
return -EINVAL; return -EINVAL;
} }
if (net_buf_tailroom(buf->frags) < BASIC_HEADER_SIZE) { if (net_buf_tailroom(pkt->frags) < BASIC_HEADER_SIZE) {
return -ENOMEM; return -ENOMEM;
} }
memset(pkt, 0, sizeof(*pkt)); memset(zpkt, 0, sizeof(*zpkt));
pkt->total_size = net_buf_tailroom(buf->frags); zpkt->total_size = net_buf_tailroom(pkt->frags);
data = net_buf_add(buf->frags, BASIC_HEADER_SIZE); data = net_buf_add(pkt->frags, BASIC_HEADER_SIZE);
/* /*
* As some header data is built by OR operations, we zero * As some header data is built by OR operations, we zero
* the header to be sure. * the header to be sure.
*/ */
memset(data, 0, BASIC_HEADER_SIZE); memset(data, 0, BASIC_HEADER_SIZE);
pkt->buf = buf; zpkt->pkt = pkt;
return 0; return 0;
} }
@ -335,7 +335,7 @@ int zoap_pending_init(struct zoap_pending *pending,
memcpy(&pending->addr, addr, sizeof(*addr)); memcpy(&pending->addr, addr, sizeof(*addr));
/* Will increase the reference count when the pending is cycled */ /* Will increase the reference count when the pending is cycled */
pending->buf = request->buf; pending->pkt = request->pkt;
return 0; return 0;
} }
@ -347,7 +347,7 @@ struct zoap_pending *zoap_pending_next_unused(
size_t i; size_t i;
for (i = 0, p = pendings; i < len; i++, p++) { for (i = 0, p = pendings; i < len; i++, p++) {
if (p->timeout == 0 && !p->buf) { if (p->timeout == 0 && !p->pkt) {
return p; return p;
} }
} }
@ -472,7 +472,7 @@ bool zoap_pending_cycle(struct zoap_pending *pending)
* When it it is the last retransmission, the buffer * When it it is the last retransmission, the buffer
* will be destroyed when it is transmitted. * will be destroyed when it is transmitted.
*/ */
net_pkt_ref(pending->buf); net_pkt_ref(pending->pkt);
} }
return cont; return cont;
@ -481,18 +481,18 @@ bool zoap_pending_cycle(struct zoap_pending *pending)
void zoap_pending_clear(struct zoap_pending *pending) void zoap_pending_clear(struct zoap_pending *pending)
{ {
pending->timeout = 0; pending->timeout = 0;
net_pkt_unref(pending->buf); net_pkt_unref(pending->pkt);
pending->buf = NULL; pending->pkt = NULL;
} }
static bool uri_path_eq(const struct zoap_packet *pkt, static bool uri_path_eq(const struct zoap_packet *zpkt,
const char * const *path) const char * const *path)
{ {
struct zoap_option options[16]; struct zoap_option options[16];
uint16_t count = 16; uint16_t count = 16;
int i, r; int i, r;
r = zoap_find_options(pkt, ZOAP_OPTION_URI_PATH, options, count); r = zoap_find_options(zpkt, ZOAP_OPTION_URI_PATH, options, count);
if (r < 0) { if (r < 0) {
return false; return false;
} }
@ -533,20 +533,20 @@ static zoap_method_t method_from_code(const struct zoap_resource *resource,
} }
} }
static bool is_request(const struct zoap_packet *pkt) static bool is_request(const struct zoap_packet *zpkt)
{ {
uint8_t code = zoap_header_get_code(pkt); uint8_t code = zoap_header_get_code(zpkt);
return !(code & ~ZOAP_REQUEST_MASK); return !(code & ~ZOAP_REQUEST_MASK);
} }
int zoap_handle_request(struct zoap_packet *pkt, int zoap_handle_request(struct zoap_packet *zpkt,
struct zoap_resource *resources, struct zoap_resource *resources,
const struct sockaddr *from) const struct sockaddr *from)
{ {
struct zoap_resource *resource; struct zoap_resource *resource;
if (!is_request(pkt)) { if (!is_request(zpkt)) {
return 0; return 0;
} }
@ -555,18 +555,18 @@ int zoap_handle_request(struct zoap_packet *pkt,
uint8_t code; uint8_t code;
/* FIXME: deal with hierarchical resources */ /* FIXME: deal with hierarchical resources */
if (!uri_path_eq(pkt, resource->path)) { if (!uri_path_eq(zpkt, resource->path)) {
continue; continue;
} }
code = zoap_header_get_code(pkt); code = zoap_header_get_code(zpkt);
method = method_from_code(resource, code); method = method_from_code(resource, code);
if (!method) { if (!method) {
return 0; return 0;
} }
return method(resource, pkt, from); return method(resource, zpkt, from);
} }
return -ENOENT; return -ENOENT;
@ -594,13 +594,13 @@ unsigned int zoap_option_value_to_int(const struct zoap_option *option)
return 0; return 0;
} }
static int get_observe_option(const struct zoap_packet *pkt) static int get_observe_option(const struct zoap_packet *zpkt)
{ {
struct zoap_option option = {}; struct zoap_option option = {};
uint16_t count = 1; uint16_t count = 1;
int r; int r;
r = zoap_find_options(pkt, ZOAP_OPTION_OBSERVE, &option, count); r = zoap_find_options(zpkt, ZOAP_OPTION_OBSERVE, &option, count);
if (r <= 0) { if (r <= 0) {
return -ENOENT; return -ENOENT;
} }
@ -797,56 +797,56 @@ struct zoap_observer *zoap_find_observer_by_addr(
return NULL; return NULL;
} }
uint8_t *zoap_packet_get_payload(struct zoap_packet *pkt, uint16_t *len) uint8_t *zoap_packet_get_payload(struct zoap_packet *zpkt, uint16_t *len)
{ {
uint8_t *appdata = pkt->buf->frags->data; uint8_t *appdata = zpkt->pkt->frags->data;
uint16_t appdatalen = pkt->buf->frags->len; uint16_t appdatalen = zpkt->pkt->frags->len;
if (!pkt || !len) { if (!zpkt || !len) {
return NULL; return NULL;
} }
if (!pkt->start) { if (!zpkt->start) {
if (appdatalen + 1 >= pkt->total_size) { if (appdatalen + 1 >= zpkt->total_size) {
return NULL; return NULL;
} }
appdata[appdatalen] = COAP_MARKER; appdata[appdatalen] = COAP_MARKER;
pkt->buf->frags->len += 1; zpkt->pkt->frags->len += 1;
pkt->start = appdata + pkt->buf->frags->len; zpkt->start = appdata + zpkt->pkt->frags->len;
} }
*len = appdata + pkt->total_size - pkt->start; *len = appdata + zpkt->total_size - zpkt->start;
return pkt->start; return zpkt->start;
} }
int zoap_packet_set_used(struct zoap_packet *pkt, uint16_t len) int zoap_packet_set_used(struct zoap_packet *zpkt, uint16_t len)
{ {
if ((pkt->buf->frags->len + len) > if ((zpkt->pkt->frags->len + len) >
net_buf_tailroom(pkt->buf->frags)) { net_buf_tailroom(zpkt->pkt->frags)) {
return -ENOMEM; return -ENOMEM;
} }
pkt->buf->frags->len += len; zpkt->pkt->frags->len += len;
return 0; return 0;
} }
int zoap_add_option(struct zoap_packet *pkt, uint16_t code, int zoap_add_option(struct zoap_packet *zpkt, uint16_t code,
const void *value, uint16_t len) const void *value, uint16_t len)
{ {
struct net_buf *frag = pkt->buf->frags; struct net_buf *frag = zpkt->pkt->frags;
struct option_context context = { .delta = 0, struct option_context context = { .delta = 0,
.used = 0 }; .used = 0 };
int r, offset; int r, offset;
if (pkt->start) { if (zpkt->start) {
return -EINVAL; return -EINVAL;
} }
offset = coap_get_header_len(pkt); offset = coap_get_header_len(zpkt);
if (offset < 0) { if (offset < 0) {
return -EINVAL; return -EINVAL;
} }
@ -856,7 +856,7 @@ int zoap_add_option(struct zoap_packet *pkt, uint16_t code,
context.buf = frag->data + offset; context.buf = frag->data + offset;
while (context.delta <= code) { while (context.delta <= code) {
r = coap_parse_option(pkt, &context, NULL, NULL); r = coap_parse_option(zpkt, &context, NULL, NULL);
if (r < 0) { if (r < 0) {
return -ENOENT; return -ENOENT;
} }
@ -883,7 +883,7 @@ int zoap_add_option(struct zoap_packet *pkt, uint16_t code,
return 0; return 0;
} }
int zoap_add_option_int(struct zoap_packet *pkt, uint16_t code, int zoap_add_option_int(struct zoap_packet *zpkt, uint16_t code,
unsigned int val) unsigned int val)
{ {
uint8_t data[4], len; uint8_t data[4], len;
@ -906,19 +906,19 @@ int zoap_add_option_int(struct zoap_packet *pkt, uint16_t code,
len = 4; len = 4;
} }
return zoap_add_option(pkt, code, data, len); return zoap_add_option(zpkt, code, data, len);
} }
int zoap_find_options(const struct zoap_packet *pkt, uint16_t code, int zoap_find_options(const struct zoap_packet *zpkt, uint16_t code,
struct zoap_option *options, uint16_t veclen) struct zoap_option *options, uint16_t veclen)
{ {
struct net_buf *frag = pkt->buf->frags; struct net_buf *frag = zpkt->pkt->frags;
struct option_context context = { .delta = 0, struct option_context context = { .delta = 0,
.used = 0 }; .used = 0 };
int hdrlen, count = 0; int hdrlen, count = 0;
uint16_t len; uint16_t len;
hdrlen = coap_get_header_len(pkt); hdrlen = coap_get_header_len(zpkt);
if (hdrlen < 0) { if (hdrlen < 0) {
return -EINVAL; return -EINVAL;
} }
@ -927,7 +927,7 @@ int zoap_find_options(const struct zoap_packet *pkt, uint16_t code,
context.buf = (uint8_t *)frag->data + hdrlen; context.buf = (uint8_t *)frag->data + hdrlen;
while (context.delta <= code && count < veclen) { while (context.delta <= code && count < veclen) {
int used = coap_parse_option(pkt, &context, int used = coap_parse_option(zpkt, &context,
(uint8_t **)&options[count].value, (uint8_t **)&options[count].value,
&len); &len);
options[count].len = len; options[count].len = len;
@ -949,26 +949,26 @@ int zoap_find_options(const struct zoap_packet *pkt, uint16_t code,
return count; return count;
} }
uint8_t zoap_header_get_version(const struct zoap_packet *pkt) uint8_t zoap_header_get_version(const struct zoap_packet *zpkt)
{ {
return (pkt->buf->frags->data[0] & 0xC0) >> 6; return (zpkt->pkt->frags->data[0] & 0xC0) >> 6;
} }
uint8_t zoap_header_get_type(const struct zoap_packet *pkt) uint8_t zoap_header_get_type(const struct zoap_packet *zpkt)
{ {
return (pkt->buf->frags->data[0] & 0x30) >> 4; return (zpkt->pkt->frags->data[0] & 0x30) >> 4;
} }
uint8_t coap_header_get_code(const struct zoap_packet *pkt) uint8_t coap_header_get_code(const struct zoap_packet *zpkt)
{ {
return pkt->buf->frags->data[1]; return zpkt->pkt->frags->data[1];
} }
const uint8_t *zoap_header_get_token(const struct zoap_packet *pkt, const uint8_t *zoap_header_get_token(const struct zoap_packet *zpkt,
uint8_t *len) uint8_t *len)
{ {
struct net_buf *frag = pkt->buf->frags; struct net_buf *frag = zpkt->pkt->frags;
uint8_t tkl = coap_header_get_tkl(pkt); uint8_t tkl = coap_header_get_tkl(zpkt);
if (len) { if (len) {
*len = 0; *len = 0;
@ -985,9 +985,9 @@ const uint8_t *zoap_header_get_token(const struct zoap_packet *pkt,
return (uint8_t *)frag->data + BASIC_HEADER_SIZE; return (uint8_t *)frag->data + BASIC_HEADER_SIZE;
} }
uint8_t zoap_header_get_code(const struct zoap_packet *pkt) uint8_t zoap_header_get_code(const struct zoap_packet *zpkt)
{ {
uint8_t code = coap_header_get_code(pkt); uint8_t code = coap_header_get_code(zpkt);
switch (code) { switch (code) {
/* Methods are encoded in the code field too */ /* Methods are encoded in the code field too */
@ -1025,25 +1025,25 @@ uint8_t zoap_header_get_code(const struct zoap_packet *pkt)
} }
} }
uint16_t zoap_header_get_id(const struct zoap_packet *pkt) uint16_t zoap_header_get_id(const struct zoap_packet *zpkt)
{ {
return sys_get_be16(&pkt->buf->frags->data[2]); return sys_get_be16(&zpkt->pkt->frags->data[2]);
} }
void zoap_header_set_version(struct zoap_packet *pkt, uint8_t ver) void zoap_header_set_version(struct zoap_packet *zpkt, uint8_t ver)
{ {
pkt->buf->frags->data[0] |= (ver & 0x3) << 6; zpkt->pkt->frags->data[0] |= (ver & 0x3) << 6;
} }
void zoap_header_set_type(struct zoap_packet *pkt, uint8_t type) void zoap_header_set_type(struct zoap_packet *zpkt, uint8_t type)
{ {
pkt->buf->frags->data[0] |= (type & 0x3) << 4; zpkt->pkt->frags->data[0] |= (type & 0x3) << 4;
} }
int zoap_header_set_token(struct zoap_packet *pkt, const uint8_t *token, int zoap_header_set_token(struct zoap_packet *zpkt, const uint8_t *token,
uint8_t tokenlen) uint8_t tokenlen)
{ {
struct net_buf *frag = pkt->buf->frags; struct net_buf *frag = zpkt->pkt->frags;
uint8_t *appdata = frag->data; uint8_t *appdata = frag->data;
if (net_buf_tailroom(frag) < BASIC_HEADER_SIZE + tokenlen) { if (net_buf_tailroom(frag) < BASIC_HEADER_SIZE + tokenlen) {
@ -1063,14 +1063,14 @@ int zoap_header_set_token(struct zoap_packet *pkt, const uint8_t *token,
return 0; return 0;
} }
void zoap_header_set_code(struct zoap_packet *pkt, uint8_t code) void zoap_header_set_code(struct zoap_packet *zpkt, uint8_t code)
{ {
pkt->buf->frags->data[1] = code; zpkt->pkt->frags->data[1] = code;
} }
void zoap_header_set_id(struct zoap_packet *pkt, uint16_t id) void zoap_header_set_id(struct zoap_packet *zpkt, uint16_t id)
{ {
sys_put_be16(id, &pkt->buf->frags->data[2]); sys_put_be16(id, &zpkt->pkt->frags->data[2]);
} }
int zoap_block_transfer_init(struct zoap_block_context *ctx, int zoap_block_transfer_init(struct zoap_block_context *ctx,
@ -1092,14 +1092,14 @@ int zoap_block_transfer_init(struct zoap_block_context *ctx,
#define SET_MORE(v, m) ((v) |= (m) ? 0x08 : 0x00) #define SET_MORE(v, m) ((v) |= (m) ? 0x08 : 0x00)
#define SET_NUM(v, n) ((v) |= ((n) << 4)) #define SET_NUM(v, n) ((v) |= ((n) << 4))
int zoap_add_block1_option(struct zoap_packet *pkt, int zoap_add_block1_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx) struct zoap_block_context *ctx)
{ {
uint16_t bytes = zoap_block_size_to_bytes(ctx->block_size); uint16_t bytes = zoap_block_size_to_bytes(ctx->block_size);
unsigned int val = 0; unsigned int val = 0;
int r; int r;
if (is_request(pkt)) { if (is_request(zpkt)) {
SET_BLOCK_SIZE(val, ctx->block_size); SET_BLOCK_SIZE(val, ctx->block_size);
SET_MORE(val, ctx->current + bytes < ctx->total_size); SET_MORE(val, ctx->current + bytes < ctx->total_size);
SET_NUM(val, ctx->current / bytes); SET_NUM(val, ctx->current / bytes);
@ -1108,18 +1108,18 @@ int zoap_add_block1_option(struct zoap_packet *pkt,
SET_NUM(val, ctx->current / bytes); SET_NUM(val, ctx->current / bytes);
} }
r = zoap_add_option_int(pkt, ZOAP_OPTION_BLOCK1, val); r = zoap_add_option_int(zpkt, ZOAP_OPTION_BLOCK1, val);
return r; return r;
} }
int zoap_add_block2_option(struct zoap_packet *pkt, int zoap_add_block2_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx) struct zoap_block_context *ctx)
{ {
int r, val = 0; int r, val = 0;
uint16_t bytes = zoap_block_size_to_bytes(ctx->block_size); uint16_t bytes = zoap_block_size_to_bytes(ctx->block_size);
if (is_request(pkt)) { if (is_request(zpkt)) {
SET_BLOCK_SIZE(val, ctx->block_size); SET_BLOCK_SIZE(val, ctx->block_size);
SET_NUM(val, ctx->current / bytes); SET_NUM(val, ctx->current / bytes);
} else { } else {
@ -1128,21 +1128,21 @@ int zoap_add_block2_option(struct zoap_packet *pkt,
SET_NUM(val, ctx->current / bytes); SET_NUM(val, ctx->current / bytes);
} }
r = zoap_add_option_int(pkt, ZOAP_OPTION_BLOCK2, val); r = zoap_add_option_int(zpkt, ZOAP_OPTION_BLOCK2, val);
return r; return r;
} }
int zoap_add_size1_option(struct zoap_packet *pkt, int zoap_add_size1_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx) struct zoap_block_context *ctx)
{ {
return zoap_add_option_int(pkt, ZOAP_OPTION_SIZE1, ctx->total_size); return zoap_add_option_int(zpkt, ZOAP_OPTION_SIZE1, ctx->total_size);
} }
int zoap_add_size2_option(struct zoap_packet *pkt, int zoap_add_size2_option(struct zoap_packet *zpkt,
struct zoap_block_context *ctx) struct zoap_block_context *ctx)
{ {
return zoap_add_option_int(pkt, ZOAP_OPTION_SIZE2, ctx->total_size); return zoap_add_option_int(zpkt, ZOAP_OPTION_SIZE2, ctx->total_size);
} }
struct block_transfer { struct block_transfer {
@ -1151,13 +1151,13 @@ struct block_transfer {
bool more; bool more;
}; };
static int get_block_option(const struct zoap_packet *pkt, uint16_t code) static int get_block_option(const struct zoap_packet *zpkt, uint16_t code)
{ {
struct zoap_option option; struct zoap_option option;
unsigned int val; unsigned int val;
int count = 1; int count = 1;
count = zoap_find_options(pkt, code, &option, count); count = zoap_find_options(zpkt, code, &option, count);
if (count <= 0) { if (count <= 0) {
return -ENOENT; return -ENOENT;
} }
@ -1243,20 +1243,20 @@ static int update_control_block2(struct zoap_block_context *ctx,
return 0; return 0;
} }
int zoap_update_from_block(const struct zoap_packet *pkt, int zoap_update_from_block(const struct zoap_packet *zpkt,
struct zoap_block_context *ctx) struct zoap_block_context *ctx)
{ {
int r, block1, block2, size1, size2; int r, block1, block2, size1, size2;
block1 = get_block_option(pkt, ZOAP_OPTION_BLOCK1); block1 = get_block_option(zpkt, ZOAP_OPTION_BLOCK1);
block2 = get_block_option(pkt, ZOAP_OPTION_BLOCK2); block2 = get_block_option(zpkt, ZOAP_OPTION_BLOCK2);
size1 = get_block_option(pkt, ZOAP_OPTION_SIZE1); size1 = get_block_option(zpkt, ZOAP_OPTION_SIZE1);
size2 = get_block_option(pkt, ZOAP_OPTION_SIZE2); size2 = get_block_option(zpkt, ZOAP_OPTION_SIZE2);
size1 = size1 == -ENOENT ? 0 : size1; size1 = size1 == -ENOENT ? 0 : size1;
size2 = size2 == -ENOENT ? 0 : size2; size2 = size2 == -ENOENT ? 0 : size2;
if (is_request(pkt)) { if (is_request(zpkt)) {
r = update_control_block2(ctx, block2, size2); r = update_control_block2(ctx, block2, size2);
if (r) { if (r) {
return r; return r;

View file

@ -223,30 +223,31 @@ static int send_error_response(struct zoap_resource *resource,
{ {
struct net_context *context; struct net_context *context;
struct zoap_packet response; struct zoap_packet response;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
uint16_t id; uint16_t id;
int r; int r;
id = zoap_header_get_id(request); id = zoap_header_get_id(request);
context = net_pkt_context(request->buf); context = net_pkt_context(request->pkt);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
if (!frag) { if (!frag) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return r; return r;
} }
@ -256,10 +257,10 @@ static int send_error_response(struct zoap_resource *resource,
zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_BAD_REQUEST); zoap_header_set_code(&response, ZOAP_RESPONSE_CODE_BAD_REQUEST);
zoap_header_set_id(&response, id); zoap_header_set_id(&response, id);
r = net_context_sendto(buf, from, sizeof(struct sockaddr_in6), r = net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return r; return r;
@ -272,7 +273,8 @@ int _zoap_well_known_core_get(struct zoap_resource *resource,
struct net_context *context; struct net_context *context;
struct zoap_packet response; struct zoap_packet response;
struct zoap_option query; struct zoap_option query;
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
const uint8_t *token; const uint8_t *token;
unsigned int num_queries; unsigned int num_queries;
uint16_t id; uint16_t id;
@ -293,22 +295,22 @@ int _zoap_well_known_core_get(struct zoap_resource *resource,
num_queries = r; num_queries = r;
context = net_pkt_context(request->buf); context = net_pkt_context(request->pkt);
buf = net_pkt_get_tx(context, K_FOREVER); pkt = net_pkt_get_tx(context, K_FOREVER);
if (!buf) { if (!pkt) {
return -ENOMEM; return -ENOMEM;
} }
frag = net_pkt_get_data(context, K_FOREVER); frag = net_pkt_get_data(context, K_FOREVER);
if (!frag) { if (!frag) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
r = zoap_packet_init(&response, buf); r = zoap_packet_init(&response, pkt);
if (r < 0) { if (r < 0) {
goto done; goto done;
} }
@ -323,7 +325,7 @@ int _zoap_well_known_core_get(struct zoap_resource *resource,
r = zoap_add_option(&response, ZOAP_OPTION_CONTENT_FORMAT, r = zoap_add_option(&response, ZOAP_OPTION_CONTENT_FORMAT,
&format, sizeof(format)); &format, sizeof(format));
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return -EINVAL; return -EINVAL;
} }
@ -342,18 +344,18 @@ int _zoap_well_known_core_get(struct zoap_resource *resource,
} }
if (!response.start) { if (!response.start) {
temp = response.buf->frags; temp = response.pkt->frags;
str = net_buf_add(temp, 1); str = net_buf_add(temp, 1);
*str = 0xFF; *str = 0xFF;
response.start = str + 1; response.start = str + 1;
} else { } else {
temp = net_pkt_get_data(context, K_FOREVER); temp = net_pkt_get_data(context, K_FOREVER);
if (!temp) { if (!temp) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return -ENOMEM; return -ENOMEM;
} }
net_buf_frag_add(buf, temp); net_pkt_frag_add(pkt, temp);
} }
r = format_resource(resource, temp); r = format_resource(resource, temp);
@ -362,18 +364,18 @@ int _zoap_well_known_core_get(struct zoap_resource *resource,
} }
} }
net_pkt_compact(buf); net_pkt_compact(pkt);
done: done:
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return send_error_response(resource, request, from); return send_error_response(resource, request, from);
} }
r = net_context_sendto(buf, from, sizeof(struct sockaddr_in6), r = net_context_sendto(pkt, from, sizeof(struct sockaddr_in6),
NULL, 0, NULL, NULL); NULL, 0, NULL, NULL);
if (r < 0) { if (r < 0) {
net_pkt_unref(buf); net_pkt_unref(pkt);
} }
return r; return r;

View file

@ -14,11 +14,12 @@ CONFIG_NET_PKT_TX_COUNT=1
CONFIG_NET_BUF_RX_COUNT=3 CONFIG_NET_BUF_RX_COUNT=3
CONFIG_NET_BUF_TX_COUNT=3 CONFIG_NET_BUF_TX_COUNT=3
CONFIG_NET_LOG=y CONFIG_NET_LOG=y
CONFIG_SYS_LOG_NET_LEVEL=2
CONFIG_SYS_LOG_SHOW_COLOR=y CONFIG_SYS_LOG_SHOW_COLOR=y
CONFIG_NET_DEBUG_IF=y CONFIG_NET_DEBUG_IF=y
CONFIG_NET_DEBUG_CORE=y CONFIG_NET_DEBUG_CORE=y
CONFIG_NET_DEBUG_6LO=n CONFIG_NET_DEBUG_6LO=y
CONFIG_NET_DEBUG_NET_PKT=n CONFIG_NET_DEBUG_NET_PKT=y
CONFIG_NET_6LO_CONTEXT=y CONFIG_NET_6LO_CONTEXT=y
#Before modifying this value, add respective code in src/main.c #Before modifying this value, add respective code in src/main.c

View file

@ -198,9 +198,9 @@ static void net_6lo_iface_init(struct net_if *iface)
net_if_set_link_addr(iface, src_mac, 8, NET_LINK_IEEE802154); net_if_set_link_addr(iface, src_mac, 8, NET_LINK_IEEE802154);
} }
static int tester_send(struct net_if *iface, struct net_buf *buf) static int tester_send(struct net_if *iface, struct net_pkt *pkt)
{ {
net_pkt_unref(buf); net_pkt_unref(pkt);
return NET_OK; return NET_OK;
} }
@ -214,7 +214,7 @@ NET_DEVICE_INIT(net_6lo_test, "net_6lo_test",
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&net_6lo_if_api, DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2), 127); &net_6lo_if_api, DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2), 127);
static bool compare_data(struct net_buf *buf, struct net_6lo_data *data) static bool compare_data(struct net_pkt *pkt, struct net_6lo_data *data)
{ {
struct net_buf *frag; struct net_buf *frag;
uint8_t bytes; uint8_t bytes;
@ -224,38 +224,38 @@ static bool compare_data(struct net_buf *buf, struct net_6lo_data *data)
int remaining = data->small ? SIZE_OF_SMALL_DATA : SIZE_OF_LARGE_DATA; int remaining = data->small ? SIZE_OF_SMALL_DATA : SIZE_OF_LARGE_DATA;
if (data->nh_udp) { if (data->nh_udp) {
if (net_buf_frags_len(buf->frags) != if (net_pkt_get_len(pkt) !=
(NET_IPV6UDPH_LEN + remaining)) { (NET_IPV6UDPH_LEN + remaining)) {
TC_PRINT("mismatch lengths, expected %d received %zu\n", TC_PRINT("mismatch lengths, expected %d received %zu\n",
NET_IPV6UDPH_LEN + remaining, NET_IPV6UDPH_LEN + remaining,
net_buf_frags_len(buf->frags)); net_pkt_get_len(pkt));
return false; return false;
} }
} else if (data->nh_icmp) { } else if (data->nh_icmp) {
if (net_buf_frags_len(buf->frags) != if (net_pkt_get_len(pkt) !=
(NET_IPV6ICMPH_LEN + remaining)) { (NET_IPV6ICMPH_LEN + remaining)) {
TC_PRINT("mismatch lengths, expected %d received %zu\n", TC_PRINT("mismatch lengths, expected %d received %zu\n",
NET_IPV6ICMPH_LEN + remaining, NET_IPV6ICMPH_LEN + remaining,
net_buf_frags_len(buf->frags)); net_pkt_get_len(pkt));
return false; return false;
} }
} else { } else {
if (net_buf_frags_len(buf->frags) != if (net_pkt_get_len(pkt) !=
(NET_IPV6H_LEN + remaining)) { (NET_IPV6H_LEN + remaining)) {
TC_PRINT("mismatch lengths, expected %d received %zu\n", TC_PRINT("mismatch lengths, expected %d received %zu\n",
NET_IPV6H_LEN + remaining, NET_IPV6H_LEN + remaining,
net_buf_frags_len(buf->frags)); net_pkt_get_len(pkt));
return false; return false;
} }
} }
frag = buf->frags; frag = pkt->frags;
if (data->nh_udp) { if (data->nh_udp) {
if (memcmp(frag->data, (uint8_t *)data, NET_IPV6UDPH_LEN)) { if (memcmp(frag->data, (uint8_t *)data, NET_IPV6UDPH_LEN)) {
@ -301,30 +301,31 @@ static bool compare_data(struct net_buf *buf, struct net_6lo_data *data)
return true; return true;
} }
static struct net_buf *create_buf(struct net_6lo_data *data) static struct net_pkt *create_pkt(struct net_6lo_data *data)
{ {
struct net_buf *buf, *frag; struct net_pkt *pkt;
struct net_buf *frag;
uint8_t bytes, pos; uint8_t bytes, pos;
uint16_t len; uint16_t len;
int remaining; int remaining;
buf = net_pkt_get_reserve_tx(0, K_FOREVER); pkt = net_pkt_get_reserve_tx(0, K_FOREVER);
if (!buf) { if (!pkt) {
return NULL; return NULL;
} }
net_pkt_set_iface(buf, net_if_get_default()); net_pkt_set_iface(pkt, net_if_get_default());
net_pkt_set_ip_hdr_len(buf, NET_IPV6H_LEN); net_pkt_set_ip_hdr_len(pkt, NET_IPV6H_LEN);
net_pkt_ll_src(buf)->addr = src_mac; net_pkt_ll_src(pkt)->addr = src_mac;
net_pkt_ll_src(buf)->len = 8; net_pkt_ll_src(pkt)->len = 8;
net_pkt_ll_dst(buf)->addr = dst_mac; net_pkt_ll_dst(pkt)->addr = dst_mac;
net_pkt_ll_dst(buf)->len = 8; net_pkt_ll_dst(pkt)->len = 8;
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
if (!frag) { if (!frag) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return NULL; return NULL;
} }
@ -377,18 +378,18 @@ static struct net_buf *create_buf(struct net_6lo_data *data)
remaining -= bytes; remaining -= bytes;
if (net_buf_tailroom(frag) - (bytes - copy)) { if (net_buf_tailroom(frag) - (bytes - copy)) {
net_pkt_unref(buf); net_pkt_unref(pkt);
return NULL; return NULL;
} }
net_buf_frag_add(buf, frag); net_pkt_frag_add(pkt, frag);
if (remaining > 0) { if (remaining > 0) {
frag = net_pkt_get_frag(buf, K_FOREVER); frag = net_pkt_get_frag(pkt, K_FOREVER);
} }
} }
return buf; return pkt;
} }
static struct net_6lo_data test_data_1 = { static struct net_6lo_data test_data_1 = {
@ -798,49 +799,49 @@ static struct net_6lo_data test_data_22 = {
static int test_6lo(struct net_6lo_data *data) static int test_6lo(struct net_6lo_data *data)
{ {
struct net_buf *buf; struct net_pkt *pkt;
int result = TC_FAIL; int result = TC_FAIL;
buf = create_buf(data); pkt = create_pkt(data);
if (!buf) { if (!pkt) {
TC_PRINT("%s: failed to create buffer\n", __func__); TC_PRINT("%s: failed to create buffer\n", __func__);
goto end; goto end;
} }
#if DEBUG > 0 #if DEBUG > 0
TC_PRINT("length before compression %zu\n", TC_PRINT("length before compression %zu\n",
net_buf_frags_len(buf->frags)); net_pkt_get_len(pkt));
net_hexdump_frags("before-compression", buf); net_hexdump_frags("before-compression", pkt);
#endif #endif
if (!net_6lo_compress(buf, data->iphc, NULL)) { if (!net_6lo_compress(pkt, data->iphc, NULL)) {
TC_PRINT("compression failed\n"); TC_PRINT("compression failed\n");
goto end; goto end;
} }
#if DEBUG > 0 #if DEBUG > 0
TC_PRINT("length after compression %zu\n", TC_PRINT("length after compression %zu\n",
net_buf_frags_len(buf->frags)); net_pkt_get_len(pkt));
net_hexdump_frags("after-compression", buf); net_hexdump_frags("after-compression", pkt);
#endif #endif
if (!net_6lo_uncompress(buf)) { if (!net_6lo_uncompress(pkt)) {
TC_PRINT("uncompression failed\n"); TC_PRINT("uncompression failed\n");
goto end; goto end;
} }
#if DEBUG > 0 #if DEBUG > 0
TC_PRINT("length after uncompression %zu\n", TC_PRINT("length after uncompression %zu\n",
net_buf_frags_len(buf->frags)); net_pkt_get_len(pkt));
net_hexdump_frags("after-uncompression", buf); net_hexdump_frags("after-uncompression", pkt);
#endif #endif
if (compare_data(buf, data)) { if (compare_data(pkt, data)) {
result = TC_PASS; result = TC_PASS;
} }
end: end:
net_pkt_unref(buf); net_pkt_unref(pkt);
return result; return result;
} }

Some files were not shown because too many files have changed in this diff Show more