diff --git a/drivers/console/telnet_console.c b/drivers/console/telnet_console.c index 5aa58e6f140..f4945c9980e 100644 --- a/drivers/console/telnet_console.c +++ b/drivers/console/telnet_console.c @@ -80,7 +80,7 @@ static K_TIMER_DEFINE(send_timer, telnet_send_prematurely, NULL); /* For now we handle a unique telnet client connection */ 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 struct k_fifo *avail_queue; @@ -116,17 +116,17 @@ static void telnet_end_client_connection(void) net_context_put(client_cnx); client_cnx = NULL; - if (out_buf) { - net_buf_unref(out_buf); + if (out_pkt) { + net_pkt_unref(out_pkt); } 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); - if (!out_buf) { + out_pkt = net_pkt_get_tx(client, K_FOREVER); + if (!out_pkt) { /* Cannot happen atm, net_pkt waits indefinitely */ return -ENOBUFS; } @@ -225,7 +225,7 @@ static void telnet_sent_cb(struct net_context *client, { if (status) { 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(); 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 */ 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) || - telnet_setup_out_buf(client_cnx)) { + telnet_setup_out_pkt(client_cnx)) { 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) { - 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); - telnet_setup_out_buf(client_cnx); + telnet_setup_out_pkt(client_cnx); } static inline void telnet_reply_ay_command(void) @@ -326,10 +326,10 @@ out: #define telnet_reply_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 *)net_pkt_appdata(buf); + (struct telnet_simple_command *)net_pkt_appdata(pkt); if (cmd->iac != NVT_CMD_IAC) { return false; @@ -351,17 +351,17 @@ static inline bool telnet_handle_command(struct net_buf *buf) 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; 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) { return; } - if (telnet_handle_command(buf)) { + if (telnet_handle_command(pkt)) { return; } @@ -374,8 +374,8 @@ static inline void telnet_handle_input(struct net_buf *buf) return; } - offset = net_buf_frags_len(buf) - len; - net_pkt_read(buf->frags, offset, &pos, len, input->line); + offset = net_pkt_get_len(pkt) - len; + net_frag_read(pkt->frags, offset, &pos, len, input->line); /* LF/CR will be removed if only the line is not NUL terminated */ 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, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { - if (!buf || status) { + if (!pkt || status) { telnet_end_client_connection(); SYS_LOG_DBG("Telnet client dropped (AF_INET%s) status %d", @@ -405,9 +405,9 @@ static void telnet_recv(struct net_context *client, 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 */ @@ -446,7 +446,7 @@ static void telnet_accept(struct net_context *client, goto error; } - if (telnet_setup_out_buf(client)) { + if (telnet_setup_out_pkt(client)) { goto error; } diff --git a/drivers/ethernet/eth_enc28j60.c b/drivers/ethernet/eth_enc28j60.c index 95ad145f3c7..c0321c85644 100644 --- a/drivers/ethernet/eth_enc28j60.c +++ b/drivers/ethernet/eth_enc28j60.c @@ -424,7 +424,7 @@ static int eth_enc28j60_init(struct device *dev) 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) { 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; 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; uint16_t data_len; if (first_frag) { - data_ptr = net_pkt_ll(buf); - data_len = net_pkt_ll_reserve(buf) + frag->len; + data_ptr = net_pkt_ll(pkt); + data_len = net_pkt_ll_reserve(pkt) + frag->len; first_frag = false; } else { 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); do { - struct net_buf *last_frag; struct net_buf *pkt_buf = NULL; uint16_t frm_len = 0; - struct net_buf *buf; + struct net_pkt *pkt; uint16_t next_packet; uint8_t np[2]; @@ -552,32 +551,29 @@ static int eth_enc28j60_rx(struct device *dev) lengthfr = frm_len; /* Get the frame from the buffer */ - buf = net_pkt_get_reserve_rx(0, config->timeout); - if (!buf) { + pkt = net_pkt_get_reserve_rx(0, config->timeout); + if (!pkt) { SYS_LOG_ERR("Could not allocate rx buffer"); goto done; } - last_frag = buf; - do { size_t frag_len; uint8_t *data_ptr; size_t spi_frame_len; /* 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) { SYS_LOG_ERR("Could not allocate data buffer"); - net_buf_unref(buf); + net_pkt_unref(pkt); goto done; } - net_buf_frag_insert(last_frag, pkt_buf); - data_ptr = pkt_buf->data; + net_pkt_frag_insert(pkt, pkt_buf); - last_frag = pkt_buf; + data_ptr = pkt_buf->data; /* Review the space available for the new frag */ 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 */ SYS_LOG_DBG("Received packet of length %u", lengthfr); - net_recv_data(context->iface, buf); + net_recv_data(context->iface, pkt); done: /* Free buffer memory and decrement rx counter */ 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; - 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) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return ret; diff --git a/drivers/ethernet/eth_mcux.c b/drivers/ethernet/eth_mcux.c index d269d58e78c..428219f02d2 100644 --- a/drivers/ethernet/eth_mcux.c +++ b/drivers/ethernet/eth_mcux.c @@ -306,7 +306,7 @@ static void eth_mcux_delayed_phy_work(struct k_work *item) 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; const struct net_buf *frag; @@ -314,7 +314,7 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf) status_t status; 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); @@ -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. */ dst = context->frame_buf; - memcpy(dst, net_pkt_ll(buf), - net_pkt_ll_reserve(buf) + buf->frags->len); - dst += net_pkt_ll_reserve(buf) + buf->frags->len; + memcpy(dst, net_pkt_ll(pkt), + net_pkt_ll_reserve(pkt) + pkt->frags->len); + dst += net_pkt_ll_reserve(pkt) + pkt->frags->len; /* Continue with the rest of fragments (which contain only data) */ - frag = buf->frags->frags; + frag = pkt->frags->frags; while (frag) { memcpy(dst, frag->data, frag->len); dst += frag->len; @@ -351,14 +351,14 @@ static int eth_tx(struct net_if *iface, struct net_buf *buf) return -1; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return 0; } static void eth_rx(struct device *iface) { struct eth_context *context = iface->driver_data; - struct net_buf *buf, *prev_frag; + struct net_pkt *pkt; const uint8_t *src; uint32_t frame_length = 0; status_t status; @@ -381,8 +381,8 @@ static void eth_rx(struct device *iface) return; } - buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); - if (!buf) { + pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT); + if (!pkt) { /* We failed to get a receive buffer. We don't add * any further logging here because the allocator * 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) { 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); assert(status == kStatus_Success); return; @@ -414,27 +414,25 @@ static void eth_rx(struct device *iface) if (status) { irq_unlock(imask); SYS_LOG_ERR("ENET_ReadFrame failed: %d\n", status); - net_pkt_unref(buf); + net_pkt_unref(pkt); return; } src = context->frame_buf; - prev_frag = buf; do { struct net_buf *pkt_buf; 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) { irq_unlock(imask); SYS_LOG_ERR("Failed to get fragment buf\n"); - net_pkt_unref(buf); + net_pkt_unref(pkt); assert(status == kStatus_Success); return; } - net_buf_frag_insert(prev_frag, pkt_buf); - prev_frag = pkt_buf; + net_pkt_frag_insert(pkt, pkt_buf); frag_len = net_buf_tailroom(pkt_buf); if (frag_len > frame_length) { frag_len = frame_length; @@ -448,8 +446,8 @@ static void eth_rx(struct device *iface) irq_unlock(imask); - if (net_recv_data(context->iface, buf) < 0) { - net_pkt_unref(buf); + if (net_recv_data(context->iface, pkt) < 0) { + net_pkt_unref(pkt); } } diff --git a/drivers/ethernet/eth_sam_gmac.c b/drivers/ethernet/eth_sam_gmac.c index cb92343b7d1..7df79931607 100644 --- a/drivers/ethernet/eth_sam_gmac.c +++ b/drivers/ethernet/eth_sam_gmac.c @@ -77,7 +77,7 @@ static struct gmac_desc tx_desc_que12[PRIORITY_QUEUE_DESC_COUNT] /* RX buffer accounting list */ static struct net_buf *rx_buf_list_que0[MAIN_QUEUE_RX_DESC_COUNT]; /* 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; } @@ -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 *tx_desc; - struct net_buf *buf; + struct net_pkt *pkt; __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"); @@ -232,9 +232,9 @@ static void tx_completed(Gmac *gmac, struct gmac_queue *queue) if (tx_desc->w1 & GMAC_TXW1_LASTBUFFER) { /* Release net buffer to the buffer pool */ - buf = UINT_TO_POINTER(ring_buf_get(&queue->tx_frames)); - net_buf_unref(buf); - SYS_LOG_DBG("Dropping buf %p", buf); + pkt = UINT_TO_POINTER(ring_buf_get(&queue->tx_frames)); + net_pkt_unref(pkt); + SYS_LOG_DBG("Dropping pkt %p", pkt); 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) { - struct net_buf *buf; + struct net_pkt *pkt; struct ring_buf *tx_frames = &queue->tx_frames; 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 */ while (tx_frames->tail != tx_frames->head) { /* Release net buffer to the buffer pool */ - buf = UINT_TO_POINTER(tx_frames->buf[tx_frames->tail]); - net_buf_unref(buf); - SYS_LOG_DBG("Dropping buf %p", buf); + pkt = UINT_TO_POINTER(tx_frames->buf[tx_frames->tail]); + net_pkt_unref(pkt); + SYS_LOG_DBG("Dropping pkt %p", pkt); 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 *rx_desc; struct ring_buf *rx_pkt_list = &queue->rx_pkt_list; - struct net_buf *rx_frame; + struct net_pkt *rx_frame; bool frame_is_complete; - struct net_buf *prev_frag; struct net_buf *frag; struct net_buf *new_frag; 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); /* Process a frame */ - prev_frag = rx_frame; tail = rx_desc_list->tail; rx_desc = &rx_desc_list->buf[tail]; 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); if (new_frag == NULL) { queue->err_rx_frames_dropped++; - net_buf_unref(rx_frame); + net_pkt_unref(rx_frame); rx_frame = NULL; } else { net_buf_add(frag, frag_len); - net_buf_frag_insert(prev_frag, frag); - prev_frag = frag; + net_pkt_frag_insert(rx_frame, frag); frag = new_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 = 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 * 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); 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; unsigned int key; - __ASSERT(buf, "buf pointer is NULL"); - __ASSERT(buf->frags, "Frame data missing"); + __ASSERT(pkt, "buf pointer is NULL"); + __ASSERT(pkt->frags, "Frame data missing"); 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 the beginning of the buffer. */ - net_buf_push(buf->frags, net_pkt_ll_reserve(buf)); - - frag = buf->frags; + net_buf_push(pkt->frags, net_pkt_ll_reserve(pkt)); + frag = pkt->frags; while (frag) { frag_data = frag->data; 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; /* 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); diff --git a/drivers/ieee802154/ieee802154_cc2520.c b/drivers/ieee802154/ieee802154_cc2520.c index b7af098d028..a6eef8c775c 100644 --- a/drivers/ieee802154/ieee802154_cc2520.c +++ b/drivers/ieee802154/ieee802154_cc2520.c @@ -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, - struct net_buf *buf, uint8_t len) + struct net_buf *frag, uint8_t len) { uint8_t data[128+1]; @@ -535,14 +535,14 @@ static inline bool read_rxfifo_content(struct cc2520_spi *spi, return false; } - memcpy(buf->data, &data[1], len); - net_buf_add(buf, len); + memcpy(frag->data, &data[1], len); + net_buf_add(frag, len); return true; } 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[1] = 0; @@ -559,7 +559,7 @@ static inline bool verify_crc(struct cc2520_context *cc2520, 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. @@ -597,12 +597,12 @@ static void cc2520_rx(int arg) { struct device *dev = INT_TO_POINTER(arg); struct cc2520_context *cc2520 = dev->driver_data; - struct net_buf *pkt_buf = NULL; - struct net_buf *buf; + struct net_buf *pkt_frag = NULL; + struct net_pkt *pkt; uint8_t pkt_len; while (1) { - buf = NULL; + pkt = NULL; k_sem_take(&cc2520->rx_lock, K_FOREVER); @@ -619,9 +619,9 @@ static void cc2520_rx(int arg) goto flush; } - buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); - if (!buf) { - SYS_LOG_ERR("No buf available"); + pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT); + if (!pkt) { + SYS_LOG_ERR("No pkt available"); goto flush; } @@ -629,27 +629,27 @@ static void cc2520_rx(int arg) /** * Reserve 1 byte for length */ - net_pkt_set_ll_reserve(buf, 1); + net_pkt_set_ll_reserve(pkt, 1); #endif - pkt_buf = net_pkt_get_frag(buf, K_NO_WAIT); - if (!pkt_buf) { - SYS_LOG_ERR("No pkt_buf available"); + pkt_frag = net_pkt_get_frag(pkt, K_NO_WAIT); + if (!pkt_frag) { + SYS_LOG_ERR("No pkt_frag available"); goto flush; } - net_buf_frag_insert(buf, pkt_buf); + net_pkt_frag_insert(pkt, pkt_frag); #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"); 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; } - cc2520->lqi = pkt_buf->data[pkt_len - 1] & + cc2520->lqi = pkt_frag->data[pkt_len - 1] & CC2520_FCS_CORRELATION; if (cc2520->lqi <= 50) { cc2520->lqi = 0; @@ -659,30 +659,30 @@ static void cc2520_rx(int arg) cc2520->lqi = (cc2520->lqi - 50) << 2; } #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"); goto flush; } - if (!verify_crc(cc2520, buf)) { + if (!verify_crc(cc2520, pkt)) { SYS_LOG_ERR("Bad packet CRC"); goto out; } #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"); goto out; } #if defined(CONFIG_IEEE802154_CC2520_RAW) - net_buf_add_u8(pkt_buf, cc2520->lqi); + net_buf_add_u8(pkt_frag, cc2520->lqi); #endif SYS_LOG_DBG("Caught a packet (%u) (LQI: %u)", 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"); goto out; } @@ -696,8 +696,8 @@ flush: _cc2520_print_errors(cc2520); flush_rxfifo(cc2520); out: - if (buf) { - net_buf_unref(buf); + if (pkt) { + net_pkt_unref(pkt); } } } @@ -837,11 +837,11 @@ error: } static int cc2520_tx(struct device *dev, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { - uint8_t *frame = frag->data - net_pkt_ll_reserve(buf); - uint8_t len = net_pkt_ll_reserve(buf) + frag->len; + uint8_t *frame = frag->data - net_pkt_ll_reserve(pkt); + uint8_t len = net_pkt_ll_reserve(pkt) + frag->len; struct cc2520_context *cc2520 = dev->driver_data; uint8_t retry = 2; bool status; diff --git a/drivers/ieee802154/ieee802154_mcr20a.c b/drivers/ieee802154/ieee802154_mcr20a.c index b5b42146216..fbac7b863ef 100644 --- a/drivers/ieee802154/ieee802154_mcr20a.c +++ b/drivers/ieee802154/ieee802154_mcr20a.c @@ -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) { - struct net_buf *pkt_buf = NULL; - struct net_buf *buf; + struct net_pkt *pkt = NULL; + struct net_buf *frag; uint8_t pkt_len; - buf = NULL; - pkt_len = len - MCR20A_FCS_LENGTH; - buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); - if (!buf) { + pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT); + if (!pkt) { SYS_LOG_ERR("No buf available"); goto out; } @@ -558,22 +556,22 @@ static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len) /** * Reserve 1 byte for length */ - net_pkt_set_ll_reserve(buf, 1); + net_pkt_set_ll_reserve(pkt, 1); #endif - pkt_buf = net_pkt_get_frag(buf, K_NO_WAIT); - if (!pkt_buf) { - SYS_LOG_ERR("No pkt_buf available"); + frag = net_pkt_get_frag(pkt, K_NO_WAIT); + if (!frag) { + SYS_LOG_ERR("No frag available"); 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"); 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"); goto out; } @@ -584,10 +582,10 @@ static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len) mcr20a_get_rssi(mcr20a->lqi)); #if defined(CONFIG_IEEE802154_MCR20A_RAW) - net_buf_add_u8(pkt_buf, mcr20a->lqi); + net_buf_add_u8(frag, mcr20a->lqi); #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"); goto out; } @@ -597,8 +595,8 @@ static inline void mcr20a_rx(struct mcr20a_context *mcr20a, uint8_t len) CONFIG_IEEE802154_MCR20A_RX_STACK_SIZE); return; out: - if (buf) { - net_buf_unref(buf); + if (pkt) { + net_pkt_unref(pkt); } } @@ -1031,12 +1029,12 @@ error: } static inline bool write_txfifo_content(struct mcr20a_spi *spi, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { uint8_t cmd[2 + MCR20A_PSDU_LENGTH]; - uint8_t payload_len = net_pkt_ll_reserve(buf) + frag->len; - uint8_t *payload = frag->data - net_pkt_ll_reserve(buf); + uint8_t payload_len = net_pkt_ll_reserve(pkt) + frag->len; + uint8_t *payload = frag->data - net_pkt_ll_reserve(pkt); bool retval; 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, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { 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); 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)) { SYS_LOG_ERR("Failed to mask IRQ_B"); @@ -1089,7 +1087,7 @@ static int mcr20a_tx(struct device *dev, 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"); goto error; } diff --git a/drivers/ieee802154/ieee802154_nrf5.c b/drivers/ieee802154/ieee802154_nrf5.c index 59f46da5e43..28cf151990a 100644 --- a/drivers/ieee802154/ieee802154_nrf5.c +++ b/drivers/ieee802154/ieee802154_nrf5.c @@ -55,25 +55,25 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3) { struct device *dev = (struct device *)arg1; 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; - struct net_buf *buf; + struct net_pkt *pkt; uint8_t pkt_len; ARG_UNUSED(arg2); ARG_UNUSED(arg3); while (1) { - buf = NULL; + pkt = NULL; SYS_LOG_DBG("Waiting for frame"); k_sem_take(&nrf5_radio->rx_wait, K_FOREVER); SYS_LOG_DBG("Frame received"); - buf = net_pkt_get_reserve_rx(0, K_NO_WAIT); - if (!buf) { - SYS_LOG_ERR("No buf available"); + pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT); + if (!pkt) { + SYS_LOG_ERR("No pkt available"); goto out; } @@ -81,16 +81,16 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3) /** * Reserve 1 byte for length */ - net_pkt_set_ll_reserve(buf, 1); + net_pkt_set_ll_reserve(pkt, 1); #endif - pkt_buf = net_pkt_get_frag(buf, K_NO_WAIT); - if (!pkt_buf) { - SYS_LOG_ERR("No pkt_buf available"); + frag = net_pkt_get_frag(pkt, K_NO_WAIT); + if (!frag) { + SYS_LOG_ERR("No frag available"); goto out; } - net_buf_frag_insert(buf, pkt_buf); + net_pkt_frag_insert(pkt, frag); /* rx_mpdu contains length, psdu, fcs|lqi * 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 /* Skip length (first byte) and copy the payload */ - memcpy(pkt_buf->data, nrf5_radio->rx_psdu + 1, pkt_len); - net_buf_add(pkt_buf, pkt_len); + memcpy(frag->data, nrf5_radio->rx_psdu + 1, pkt_len); + net_buf_add(frag, pkt_len); nrf_drv_radio802154_buffer_free(nrf5_radio->rx_psdu); ack_result = ieee802154_radio_handle_ack(nrf5_radio->iface, - buf); + pkt); if (ack_result == NET_OK) { SYS_LOG_DBG("ACK packet handled"); 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)", 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"); goto out; } @@ -129,8 +129,8 @@ static void nrf5_rx_thread(void *arg1, void *arg2, void *arg3) continue; out: - if (buf) { - net_buf_unref(buf); + if (pkt) { + 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, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { struct nrf5_802154_data *nrf5_radio = NRF5_802154_DATA(dev); - uint8_t payload_len = net_pkt_ll_reserve(buf) + frag->len; - uint8_t *payload = frag->data - net_pkt_ll_reserve(buf); + uint8_t payload_len = net_pkt_ll_reserve(pkt) + frag->len; + uint8_t *payload = frag->data - net_pkt_ll_reserve(pkt); SYS_LOG_DBG("%p (%u)", payload, payload_len); diff --git a/drivers/ieee802154/ieee802154_uart_pipe.c b/drivers/ieee802154/ieee802154_uart_pipe.c index 9402830f500..31f50f9c6d3 100644 --- a/drivers/ieee802154/ieee802154_uart_pipe.c +++ b/drivers/ieee802154/ieee802154_uart_pipe.c @@ -30,8 +30,8 @@ static struct device *upipe_dev; static uint8_t *upipe_rx(uint8_t *buf, size_t *off) { struct upipe_context *upipe = upipe_dev->driver_data; - struct net_buf *pkt_buf = NULL; - struct net_buf *pkt = NULL; + struct net_pkt *pkt = NULL; + struct net_buf *frag = NULL; if (!upipe_dev) { goto done; @@ -56,20 +56,20 @@ static uint8_t *upipe_rx(uint8_t *buf, size_t *off) if (upipe->rx_len == upipe->rx_off) { pkt = net_pkt_get_reserve_rx(0, K_NO_WAIT); if (!pkt) { - SYS_LOG_DBG("No buf available"); + SYS_LOG_DBG("No pkt available"); goto flush; } - pkt_buf = net_pkt_get_frag(pkt, K_NO_WAIT); - if (!pkt_buf) { + frag = net_pkt_get_frag(pkt, K_NO_WAIT); + if (!frag) { SYS_LOG_DBG("No fragment available"); 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); - net_buf_add(pkt_buf, upipe->rx_len - 2); + memcpy(frag->data, upipe->rx_buf, upipe->rx_len - 2); + net_buf_add(frag, upipe->rx_len - 2); if (ieee802154_radio_handle_ack(upipe->iface, pkt) == NET_OK) { SYS_LOG_DBG("ACK packet handled"); @@ -93,7 +93,7 @@ flush: done: *off = 0; - return buf; + return pkt; } 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, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { - uint8_t *pkt_buf = frag->data - net_pkt_ll_reserve(buf); - uint8_t len = net_pkt_ll_reserve(buf) + frag->len; + uint8_t *pkt_buf = frag->data - net_pkt_ll_reserve(pkt); + uint8_t len = net_pkt_ll_reserve(pkt) + frag->len; struct upipe_context *upipe = dev->driver_data; uint8_t i, data; diff --git a/drivers/slip/slip.c b/drivers/slip/slip.c index 033b0177b80..376e6bd101d 100644 --- a/drivers/slip/slip.c +++ b/drivers/slip/slip.c @@ -45,9 +45,9 @@ struct slip_context { * driver initialization or SLIP_END byte. */ 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 */ - uint8_t *ptr; /* Where in net_buf to add data */ + uint8_t *ptr; /* Where in net_pkt to add data */ struct net_if *iface; uint8_t state; @@ -125,25 +125,25 @@ static inline void slip_writeb(unsigned char c) 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; #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; #endif uint8_t *ptr; uint16_t i; uint8_t c; - if (!buf->frags) { + if (!pkt->frags) { /* No data? */ return -ENODATA; } 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) int frag_count = 0; #endif @@ -201,26 +201,26 @@ static int slip_send(struct net_if *iface, struct net_buf *buf) #if defined(CONFIG_SLIP_DEBUG) 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) { char msg[8 + 1]; snprintf(msg, sizeof(msg), "len + net_pkt_ll_reserve(buf), - net_pkt_ll_reserve(buf)); + hexdump(msg, net_pkt_ll(pkt), + frag->len + net_pkt_ll_reserve(pkt), + net_pkt_ll_reserve(pkt)); } #endif } - net_pkt_unref(buf); + net_pkt_unref(pkt); slip_writeb(SLIP_END); 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) { 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) { - struct net_buf *buf; + struct net_pkt *pkt; - buf = slip_poll_handler(slip); - if (!buf || !buf->frags) { + pkt = slip_poll_handler(slip); + if (!pkt || !pkt->frags) { return; } - if (net_recv_data(slip->iface, buf) < 0) { - net_pkt_unref(buf); + if (net_recv_data(slip->iface, pkt) < 0) { + 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, @@ -306,7 +307,7 @@ static inline int slip_input_byte(struct slip_context *slip, 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); } diff --git a/include/net/arp.h b/include/net/arp.h index a746675ec60..e31a97e6bd8 100644 --- a/include/net/arp.h +++ b/include/net/arp.h @@ -17,7 +17,7 @@ #include -#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 { uint16_t hwtype; /* HTYPE */ @@ -36,8 +36,8 @@ struct net_arp_hdr { #define NET_ARP_REQUEST 1 #define NET_ARP_REPLY 2 -struct net_buf *net_arp_prepare(struct net_buf *buf); -enum net_verdict net_arp_input(struct net_buf *buf); +struct net_pkt *net_arp_prepare(struct net_pkt *pkt); +enum net_verdict net_arp_input(struct net_pkt *pkt); void net_arp_init(void); diff --git a/include/net/buf.h b/include/net/buf.h index 55db785a2ea..7a65e9752ec 100644 --- a/include/net/buf.h +++ b/include/net/buf.h @@ -401,11 +401,6 @@ struct net_buf { 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. */ uint8_t ref; diff --git a/include/net/ethernet.h b/include/net/ethernet.h index ea1e6444565..2d46c1f935c 100644 --- a/include/net/ethernet.h +++ b/include/net/ethernet.h @@ -20,7 +20,7 @@ #include #include -#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_IP 0x0800 diff --git a/include/net/ieee802154_radio.h b/include/net/ieee802154_radio.h index ee7966eccfc..6d29ccd0c43 100644 --- a/include/net/ieee802154_radio.h +++ b/include/net/ieee802154_radio.h @@ -42,9 +42,9 @@ struct ieee802154_radio_api { /** Set TX power level in dbm */ int (*set_txpower)(struct device *dev, int16_t dbm); - /** Transmit a buffer fragment */ + /** Transmit a packet fragment */ int (*tx)(struct device *dev, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag); /** 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. * * @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 */ 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 @@ -77,12 +77,12 @@ extern int ieee802154_radio_send(struct net_if *iface, * 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 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 */ 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 diff --git a/include/net/mqtt.h b/include/net/mqtt.h index d7d55e63671..4dd1cd60785 100644 --- a/include/net/mqtt.h +++ b/include/net/mqtt.h @@ -148,7 +148,7 @@ struct mqtt_ctx { void (*malformed)(struct mqtt_ctx *ctx, uint16_t pkt_type); /* 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 */ uint8_t app_type; diff --git a/include/net/net_context.h b/include/net/net_context.h index 6d020540239..927a12a12cf 100644 --- a/include/net/net_context.h +++ b/include/net/net_context.h @@ -70,16 +70,16 @@ struct net_context; * received. * * @param context The context to use. - * @param buf Network buffer that is received. If the buf is not NULL, - * then the callback will own the buffer and it needs to to unref the buf - * as soon as it has finished working with it. On EOF, buf will be 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 pkt + * 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 * 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. */ typedef void (*net_context_recv_cb_t)(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data); @@ -139,6 +139,19 @@ typedef void (*net_context_connect_cb_t)(struct net_context *context, int status, 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 * dependency between net_pkt.h and net_context.h */ @@ -200,7 +213,7 @@ struct net_context { #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) /** 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. */ @@ -611,7 +624,7 @@ int net_context_accept(struct net_context *context, * net_context_connect(). * 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 timeout Timeout for the connection. Possible values * 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 */ -int net_context_send(struct net_buf *buf, +int net_context_send(struct net_pkt *pkt, net_context_send_cb_t cb, int32_t timeout, void *token, @@ -641,7 +654,7 @@ int net_context_send(struct net_buf *buf, * timeout expires. * 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 * already set in network buffer. * @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 */ -int net_context_sendto(struct net_buf *buf, +int net_context_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr, socklen_t addrlen, 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 tx_pool Pointer to the function that will return TX pool * 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 * to the caller. The DATA pool is used to store data that is sent to * the network. */ #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) 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_ASSERT(context); - NET_ASSERT(tx_pool); + NET_ASSERT(tx_slab); NET_ASSERT(data_pool); - context->tx_pool = tx_pool; + context->tx_slab = tx_slab; context->data_pool = data_pool; } #else diff --git a/include/net/net_core.h b/include/net/net_core.h index 8f7beca931b..f074e34b36e 100644 --- a/include/net/net_core.h +++ b/include/net/net_core.h @@ -57,6 +57,7 @@ extern "C" { #include struct net_buf; +struct net_pkt; struct net_context; struct net_if; @@ -71,21 +72,21 @@ enum net_verdict { }; /* 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. * * @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. * - * @param buf Network buffer. + * @param pkt Network packet. * * @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 { char *stack; diff --git a/include/net/net_if.h b/include/net/net_if.h index 02046c5d866..eddffa87e33 100644 --- a/include/net/net_if.h +++ b/include/net/net_if.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -315,27 +314,27 @@ struct net_if { } __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 buf Pointer on a net buffer to send + * @param pkt Pointer on a net packet to send * * 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 buf Pointer on a net buffer to input + * @param pkt Pointer on a net packet to input * * @return verdict about the packet */ 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 * * @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) @@ -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 * has been sent. * @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 * where the network packet was sent. * @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 { 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) diff --git a/include/net/net_l2.h b/include/net/net_l2.h index 236cf71192f..7a73422ad21 100644 --- a/include/net/net_l2.h +++ b/include/net/net_l2.h @@ -25,18 +25,18 @@ struct net_l2 { * This function is used by net core to get iface's L2 layer parsing * 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 - * (interface's L2), which in turn might work on the buffer relevantly. + * This function is used by net core to push a packet to lower layer + * (interface's L2), which in turn might work on the packet relevantly. * (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 - * 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. */ uint16_t (*reserve)(struct net_if *iface, void *data); diff --git a/include/net/net_offload.h b/include/net/net_offload.h index d50793a513f..c957f29527b 100644 --- a/include/net/net_offload.h +++ b/include/net/net_offload.h @@ -70,7 +70,7 @@ struct net_offload { /** * 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, int32_t timeout, void *token, @@ -79,7 +79,7 @@ struct net_offload { /** * 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, socklen_t addrlen, 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 * 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 - * will wait until the network buffer is sent. Timeout value > 0 will - * wait as many ms. After the network buffer is sent, + * will wait until the network packet is sent. Timeout value > 0 will + * wait as many ms. After the network packet is sent, * a caller supplied callback is 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 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 * reached. - * @param buf The network buffer to send. + * @param pkt The network packet to send. * @param cb Caller supplied callback function. * @param timeout Timeout for the connection. Possible values * 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 */ static inline int net_offload_send(struct net_if *iface, - struct net_buf *buf, + struct net_pkt *pkt, net_context_send_cb_t cb, int32_t timeout, void *token, @@ -303,18 +303,18 @@ static inline int net_offload_send(struct net_if *iface, NET_ASSERT(iface->offload); 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 * specified by address. This variant can only be used for datagram * 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, - * the function will wait until the network buffer is sent. Timeout - * value > 0 will wait as many ms. After the network buffer + * the function will wait until the network packet is sent. Timeout + * value > 0 will wait as many ms. After the network packet * is sent, a caller supplied callback is called. 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 @@ -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 * 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 - * already set in network buffer. + * already set in network packet. * @param addrlen Length of the address. * @param cb Caller supplied callback function. * @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 */ static inline int net_offload_sendto(struct net_if *iface, - struct net_buf *buf, + struct net_pkt *pkt, const struct sockaddr *dst_addr, socklen_t addrlen, 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->sendto); - return iface->offload->sendto(buf, dst_addr, addrlen, cb, + return iface->offload->sendto(pkt, dst_addr, addrlen, cb, timeout, token, user_data); } @@ -364,8 +364,8 @@ static inline int net_offload_sendto(struct net_if *iface, * multiple times to register new values. * 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 - * network buffer is received. Timeout value > 0 will wait as many ms. - * After the network buffer is received, a caller supplied callback is + * network packet is received. Timeout value > 0 will wait as many ms. + * After the network packet is received, a caller supplied callback is * 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 not called if the timeout expires. The timeout functionality diff --git a/include/net/net_pkt.h b/include/net/net_pkt.h index 877af2b83b9..c33c5a9d27f 100644 --- a/include/net/net_pkt.h +++ b/include/net/net_pkt.h @@ -42,6 +42,15 @@ enum net_dir { }; struct net_pkt { + /** FIFO uses first 4 bytes itself, reserve space */ + int _unused; + + /** Slab pointer from where it belongs to */ + struct k_mem_slab *slab; + + /** List of buffer fragments holding the packet */ + struct net_buf *frags; + /** Network connection context */ struct net_context *context; @@ -91,387 +100,367 @@ struct net_pkt { uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */ uint8_t ext_len; /* length of extension headers */ uint8_t ext_bitmap; - uint8_t net_dir; /* is this RX or TX buf */ + uint8_t net_dir; /* is this RX or TX pkt */ #if defined(CONFIG_NET_IPV6) uint8_t ext_opt_len; /* IPv6 ND option length */ #endif #if defined(CONFIG_NET_TCP) - bool buf_sent; /* Is this net_buf sent or not */ + sys_snode_t sent_list; + bool sent; /* Is this net_pkt sent or not */ #endif #if defined(CONFIG_NET_ROUTE) - bool forwarding; /* Are we forwarding this buf */ + bool forwarding; /* Are we forwarding this pkt */ #endif #if defined(CONFIG_NET_L2_IEEE802154) uint8_t ieee802154_rssi; #endif /* @endcond */ + + /** Reference counter */ + uint8_t ref; }; /** @cond ignore */ /* The interface real ll address */ -static inline struct net_linkaddr *net_pkt_ll_if(struct net_buf *buf) +static inline struct net_linkaddr *net_pkt_ll_if(struct net_pkt *pkt) { - return net_if_get_link_addr( - ((struct net_pkt *)net_buf_user_data(buf))->iface); + return net_if_get_link_addr(pkt->iface); } -static inline struct net_context *net_pkt_context(struct net_buf *buf) +static inline struct net_context *net_pkt_context(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->context; + return pkt->context; } -static inline void net_pkt_set_context(struct net_buf *buf, +static inline void net_pkt_set_context(struct net_pkt *pkt, struct net_context *ctx) { - ((struct net_pkt *)net_buf_user_data(buf))->context = ctx; + pkt->context = ctx; } -static inline void *net_pkt_token(struct net_buf *buf) +static inline void *net_pkt_token(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->token; + return pkt->token; } -static inline void net_pkt_set_token(struct net_buf *buf, void *token) +static inline void net_pkt_set_token(struct net_pkt *pkt, void *token) { - ((struct net_pkt *)net_buf_user_data(buf))->token = token; + pkt->token = token; } -static inline struct net_if *net_pkt_iface(struct net_buf *buf) +static inline struct net_if *net_pkt_iface(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->iface; + return pkt->iface; } -static inline void net_pkt_set_iface(struct net_buf *buf, struct net_if *iface) +static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface) { - ((struct net_pkt *)net_buf_user_data(buf))->iface = iface; + pkt->iface = iface; /* If the network interface is set in pkt, then also set the type of * the network address that is stored in pkt. This is done here so * that the address type is properly set and is not forgotten. */ - ((struct net_pkt *)net_buf_user_data(buf))->lladdr_src.type = - iface->link_addr.type; - ((struct net_pkt *)net_buf_user_data(buf))->lladdr_dst.type = - iface->link_addr.type; + pkt->lladdr_src.type = iface->link_addr.type; + pkt->lladdr_dst.type = iface->link_addr.type; } -static inline uint8_t net_pkt_family(struct net_buf *buf) +static inline uint8_t net_pkt_family(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->family; + return pkt->family; } -static inline void net_pkt_set_family(struct net_buf *buf, uint8_t family) +static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family) { - ((struct net_pkt *)net_buf_user_data(buf))->family = family; + pkt->family = family; } -static inline uint8_t net_pkt_ip_hdr_len(struct net_buf *buf) +static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt) { - return ((struct net_pkt *) net_buf_user_data(buf))->ip_hdr_len; + return pkt->ip_hdr_len; } -static inline void net_pkt_set_ip_hdr_len(struct net_buf *buf, uint8_t len) +static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len) { - ((struct net_pkt *) net_buf_user_data(buf))->ip_hdr_len = len; + pkt->ip_hdr_len = len; } -static inline uint8_t net_pkt_ext_len(struct net_buf *buf) +static inline uint8_t net_pkt_ext_len(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->ext_len; + return pkt->ext_len; } -static inline void net_pkt_set_ext_len(struct net_buf *buf, uint8_t len) +static inline void net_pkt_set_ext_len(struct net_pkt *pkt, uint8_t len) { - ((struct net_pkt *)net_buf_user_data(buf))->ext_len = len; + pkt->ext_len = len; } -static inline uint8_t net_pkt_ext_bitmap(struct net_buf *buf) +static inline uint8_t net_pkt_ext_bitmap(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->ext_bitmap; + return pkt->ext_bitmap; } -static inline void net_pkt_set_ext_bitmap(struct net_buf *buf, uint8_t bm) +static inline void net_pkt_set_ext_bitmap(struct net_pkt *pkt, uint8_t bm) { - ((struct net_pkt *)net_buf_user_data(buf))->ext_bitmap = bm; + pkt->ext_bitmap = bm; } -static inline void net_pkt_add_ext_bitmap(struct net_buf *buf, uint8_t bm) +static inline void net_pkt_add_ext_bitmap(struct net_pkt *pkt, uint8_t bm) { - ((struct net_pkt *)net_buf_user_data(buf))->ext_bitmap |= bm; + pkt->ext_bitmap |= bm; } -static inline uint8_t net_pkt_dir(struct net_buf *buf) +static inline uint8_t net_pkt_dir(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->net_dir; + return pkt->net_dir; } -static inline void net_pkt_set_dir(struct net_buf *buf, enum net_dir dir) +static inline void net_pkt_set_dir(struct net_pkt *pkt, enum net_dir dir) { - ((struct net_pkt *)net_buf_user_data(buf))->net_dir = dir; + pkt->net_dir = dir; } -static inline uint8_t *net_pkt_next_hdr(struct net_buf *buf) +static inline uint8_t *net_pkt_next_hdr(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->next_hdr; + return pkt->next_hdr; } -static inline void net_pkt_set_next_hdr(struct net_buf *buf, uint8_t *hdr) +static inline void net_pkt_set_next_hdr(struct net_pkt *pkt, uint8_t *hdr) { - ((struct net_pkt *)net_buf_user_data(buf))->next_hdr = hdr; + pkt->next_hdr = hdr; } #if defined(CONFIG_NET_IPV6) -static inline uint8_t net_pkt_ext_opt_len(struct net_buf *buf) +static inline uint8_t net_pkt_ext_opt_len(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->ext_opt_len; + return pkt->ext_opt_len; } -static inline void net_pkt_set_ext_opt_len(struct net_buf *buf, uint8_t len) +static inline void net_pkt_set_ext_opt_len(struct net_pkt *pkt, uint8_t len) { - ((struct net_pkt *)net_buf_user_data(buf))->ext_opt_len = len; + pkt->ext_opt_len = len; } #endif #if defined(CONFIG_NET_TCP) -static inline uint8_t net_pkt_buf_sent(struct net_buf *buf) +static inline uint8_t net_pkt_sent(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->buf_sent; + return pkt->sent; } -static inline void net_pkt_set_buf_sent(struct net_buf *buf, bool sent) +static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent) { - ((struct net_pkt *)net_buf_user_data(buf))->buf_sent = sent; + pkt->sent = sent; } #endif #if defined(CONFIG_NET_ROUTE) -static inline bool net_pkt_forwarding(struct net_buf *buf) +static inline bool net_pkt_forwarding(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->forwarding; + return pkt->forwarding; } -static inline void net_pkt_set_forwarding(struct net_buf *buf, bool forward) +static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward) { - ((struct net_pkt *)net_buf_user_data(buf))->forwarding = forward; + pkt->forwarding = forward; } #else -static inline bool net_pkt_forwarding(struct net_buf *buf) +static inline bool net_pkt_forwarding(struct net_pkt *pkt) { return false; } #endif -static inline uint16_t net_pkt_get_len(struct net_buf *buf) +static inline size_t net_pkt_get_len(struct net_pkt *pkt) { - return buf->len; + return net_buf_frags_len(pkt->frags); } -static inline void net_pkt_set_len(struct net_buf *buf, uint16_t len) +static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt) { - buf->len = len; + return pkt->frags->data; } -static inline uint8_t *net_pkt_ip_data(struct net_buf *buf) +static inline uint8_t *net_pkt_udp_data(struct net_pkt *pkt) { - return buf->frags->data; + return &pkt->frags->data[net_pkt_ip_hdr_len(pkt) + + net_pkt_ext_len(pkt)]; } -static inline uint8_t *net_pkt_udp_data(struct net_buf *buf) +static inline uint8_t *net_pkt_tcp_data(struct net_pkt *pkt) { - return &buf->frags->data[net_pkt_ip_hdr_len(buf) + - net_pkt_ext_len(buf)]; + return &pkt->frags->data[net_pkt_ip_hdr_len(pkt) + + net_pkt_ext_len(pkt)]; } -static inline uint8_t *net_pkt_tcp_data(struct net_buf *buf) +static inline uint8_t *net_pkt_icmp_data(struct net_pkt *pkt) { - return &buf->frags->data[net_pkt_ip_hdr_len(buf) + - net_pkt_ext_len(buf)]; + return &pkt->frags->data[net_pkt_ip_hdr_len(pkt) + + net_pkt_ext_len(pkt)]; } -static inline uint8_t *net_pkt_icmp_data(struct net_buf *buf) +static inline uint8_t *net_pkt_appdata(struct net_pkt *pkt) { - return &buf->frags->data[net_pkt_ip_hdr_len(buf) + - net_pkt_ext_len(buf)]; + return pkt->appdata; } -static inline uint8_t *net_pkt_appdata(struct net_buf *buf) +static inline void net_pkt_set_appdata(struct net_pkt *pkt, uint8_t *data) { - return ((struct net_pkt *)net_buf_user_data(buf))->appdata; + pkt->appdata = data; } -static inline void net_pkt_set_appdata(struct net_buf *buf, uint8_t *data) +static inline uint16_t net_pkt_appdatalen(struct net_pkt *pkt) { - ((struct net_pkt *)net_buf_user_data(buf))->appdata = data; + return pkt->appdatalen; } -static inline uint16_t net_pkt_appdatalen(struct net_buf *buf) +static inline void net_pkt_set_appdatalen(struct net_pkt *pkt, uint16_t len) { - return ((struct net_pkt *)net_buf_user_data(buf))->appdatalen; + pkt->appdatalen = len; } -static inline void net_pkt_set_appdatalen(struct net_buf *buf, uint16_t len) +static inline uint8_t net_pkt_ll_reserve(struct net_pkt *pkt) { - ((struct net_pkt *)net_buf_user_data(buf))->appdatalen = len; + return pkt->ll_reserve; } -static inline uint8_t net_pkt_ll_reserve(struct net_buf *buf) +static inline void net_pkt_set_ll_reserve(struct net_pkt *pkt, uint8_t len) { - return ((struct net_pkt *) net_buf_user_data(buf))->ll_reserve; + pkt->ll_reserve = len; } -static inline void net_pkt_set_ll_reserve(struct net_buf *buf, uint8_t len) +static inline uint8_t *net_pkt_ll(struct net_pkt *pkt) { - ((struct net_pkt *) net_buf_user_data(buf))->ll_reserve = len; + return net_pkt_ip_data(pkt) - net_pkt_ll_reserve(pkt); } -static inline uint8_t *net_pkt_ll(struct net_buf *buf) +static inline struct net_linkaddr *net_pkt_ll_src(struct net_pkt *pkt) { - return net_pkt_ip_data(buf) - net_pkt_ll_reserve(buf); + return &pkt->lladdr_src; } -static inline struct net_linkaddr *net_pkt_ll_src(struct net_buf *buf) +static inline struct net_linkaddr *net_pkt_ll_dst(struct net_pkt *pkt) { - return &((struct net_pkt *)net_buf_user_data(buf))->lladdr_src; + return &pkt->lladdr_dst; } -static inline struct net_linkaddr *net_pkt_ll_dst(struct net_buf *buf) +static inline void net_pkt_ll_clear(struct net_pkt *pkt) { - return &((struct net_pkt *)net_buf_user_data(buf))->lladdr_dst; + memset(net_pkt_ll(pkt), 0, net_pkt_ll_reserve(pkt)); + net_pkt_ll_src(pkt)->addr = NULL; + net_pkt_ll_src(pkt)->len = 0; } -static inline void net_pkt_ll_clear(struct net_buf *buf) +static inline void net_pkt_ll_swap(struct net_pkt *pkt) { - memset(net_pkt_ll(buf), 0, net_pkt_ll_reserve(buf)); - net_pkt_ll_src(buf)->addr = NULL; - net_pkt_ll_src(buf)->len = 0; -} + uint8_t *addr = net_pkt_ll_src(pkt)->addr; -static inline void net_pkt_ll_swap(struct net_buf *buf) -{ - uint8_t *addr = net_pkt_ll_src(buf)->addr; - - net_pkt_ll_src(buf)->addr = net_pkt_ll_dst(buf)->addr; - net_pkt_ll_dst(buf)->addr = addr; -} - -static inline void net_pkt_copy_user_data(struct net_buf *new, - struct net_buf *orig) -{ - memcpy((struct net_pkt *)net_buf_user_data(new), - (struct net_pkt *)net_buf_user_data(orig), - sizeof(struct net_pkt)); + net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(pkt)->addr; + net_pkt_ll_dst(pkt)->addr = addr; } #if defined(CONFIG_NET_IPV6) -static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_buf *buf) +static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt) { - return ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_prev_hdr_start; + return pkt->ipv6_prev_hdr_start; } -static inline void net_pkt_set_ipv6_hdr_prev(struct net_buf *buf, +static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt, uint16_t offset) { - ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_prev_hdr_start = offset; + pkt->ipv6_prev_hdr_start = offset; } -static inline uint8_t net_pkt_ipv6_hop_limit(struct net_buf *buf) +static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt) { - return ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_hop_limit; + return pkt->ipv6_hop_limit; } -static inline void net_pkt_set_ipv6_hop_limit(struct net_buf *buf, +static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt, uint8_t hop_limit) { - ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_hop_limit = hop_limit; + pkt->ipv6_hop_limit = hop_limit; } #endif #if defined(CONFIG_NET_IPV6_FRAGMENT) -static inline uint8_t *net_pkt_ipv6_fragment_start(struct net_buf *buf) +static inline uint8_t *net_pkt_ipv6_fragment_start(struct net_pkt *pkt) { - return ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_frag_hdr_start; + return pkt->ipv6_frag_hdr_start; } -static inline void net_pkt_set_ipv6_fragment_start(struct net_buf *buf, +static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt, uint8_t *start) { - ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_frag_hdr_start = start; + pkt->ipv6_frag_hdr_start = start; } -static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_buf *buf) +static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt) { - return ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_fragment_offset; + return pkt->ipv6_fragment_offset; } -static inline void net_pkt_set_ipv6_fragment_offset(struct net_buf *buf, +static inline void net_pkt_set_ipv6_fragment_offset(struct net_pkt *pkt, uint16_t offset) { - ((struct net_pkt *) - net_buf_user_data(buf))->ipv6_fragment_offset = offset; + pkt->ipv6_fragment_offset = offset; } -static inline uint32_t net_pkt_ipv6_fragment_id(struct net_buf *buf) +static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->ipv6_fragment_id; + return pkt->ipv6_fragment_id; } -static inline void net_pkt_set_ipv6_fragment_id(struct net_buf *buf, +static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt, uint32_t id) { - ((struct net_pkt *)net_buf_user_data(buf))->ipv6_fragment_id = id; + pkt->ipv6_fragment_id = id; } #endif #if defined(CONFIG_NET_L2_IEEE802154) -static inline uint8_t net_pkt_ieee802154_rssi(struct net_buf *buf) +static inline uint8_t net_pkt_ieee802154_rssi(struct net_pkt *pkt) { - return ((struct net_pkt *)net_buf_user_data(buf))->ieee802154_rssi; + return pkt->ieee802154_rssi; } -static inline void net_pkt_set_ieee802154_rssi(struct net_buf *buf, +static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt, uint8_t rssi) { - ((struct net_pkt *)net_buf_user_data(buf))->ieee802154_rssi = rssi; + pkt->ieee802154_rssi = rssi; } #endif -#define NET_IPV6_BUF(buf) ((struct net_ipv6_hdr *)net_pkt_ip_data(buf)) -#define NET_IPV4_BUF(buf) ((struct net_ipv4_hdr *)net_pkt_ip_data(buf)) -#define NET_ICMP_BUF(buf) ((struct net_icmp_hdr *)net_pkt_icmp_data(buf)) -#define NET_UDP_BUF(buf) ((struct net_udp_hdr *)(net_pkt_udp_data(buf))) -#define NET_TCP_BUF(buf) ((struct net_tcp_hdr *)(net_pkt_tcp_data(buf))) +#define NET_IPV6_BUF(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt)) +#define NET_IPV4_BUF(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt)) +#define NET_ICMP_BUF(pkt) ((struct net_icmp_hdr *)net_pkt_icmp_data(pkt)) +#define NET_UDP_BUF(pkt) ((struct net_udp_hdr *)(net_pkt_udp_data(pkt))) +#define NET_TCP_BUF(pkt) ((struct net_tcp_hdr *)(net_pkt_tcp_data(pkt))) -static inline void net_pkt_set_src_ipv6_addr(struct net_buf *buf) +static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt) { net_if_ipv6_select_src_addr(net_context_get_iface( - net_pkt_context(buf)), - &NET_IPV6_BUF(buf)->src); + net_pkt_context(pkt)), + &NET_IPV6_BUF(pkt)->src); } /* @endcond */ /** - * @brief Create a TX net_buf pool that is used when sending user + * @brief Create a TX net_pkt slab that is used when sending user * specified data to network. * * @param name Name of the pool. - * @param count Number of net_buf in this pool. + * @param count Number of net_pkt in this slab. */ -#define NET_PKT_TX_POOL_DEFINE(name, count) \ - NET_BUF_POOL_DEFINE(name, count, 0, sizeof(struct net_pkt), NULL) +#define NET_PKT_TX_SLAB_DEFINE(name, count) \ + K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4) /** * @brief Create a DATA net_buf pool that is used when sending user @@ -490,22 +479,32 @@ static inline void net_pkt_set_src_ipv6_addr(struct net_buf *buf) * buffer usage. */ -struct net_buf *net_pkt_get_reserve_debug(struct net_buf_pool *pool, +struct net_pkt *net_pkt_get_reserve_debug(struct k_mem_slab *slab, uint16_t reserve_head, int32_t timeout, const char *caller, int line); -#define net_pkt_get_reserve(pool, reserve_head, timeout) \ +#define net_pkt_get_reserve(slab, reserve_head, timeout) \ + net_pkt_get_reserve_debug(slab, reserve_head, timeout, \ + __func__, __LINE__) + +struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool, + uint16_t reserve_head, + int32_t timeout, + const char *caller, + int line); + +#define net_pkt_get_reserve_data(pool, reserve_head, timeout) \ net_pkt_get_reserve_debug(pool, reserve_head, timeout, \ __func__, __LINE__) -struct net_buf *net_pkt_get_rx_debug(struct net_context *context, +struct net_pkt *net_pkt_get_rx_debug(struct net_context *context, int32_t timeout, const char *caller, int line); #define net_pkt_get_rx(context, timeout) \ net_pkt_get_rx_debug(context, timeout, __func__, __LINE__) -struct net_buf *net_pkt_get_tx_debug(struct net_context *context, +struct net_pkt *net_pkt_get_tx_debug(struct net_context *context, int32_t timeout, const char *caller, int line); #define net_pkt_get_tx(context, timeout) \ @@ -517,13 +516,13 @@ struct net_buf *net_pkt_get_data_debug(struct net_context *context, #define net_pkt_get_data(context, timeout) \ net_pkt_get_data_debug(context, timeout, __func__, __LINE__) -struct net_buf *net_pkt_get_reserve_rx_debug(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_rx_debug(uint16_t reserve_head, int32_t timeout, const char *caller, int line); #define net_pkt_get_reserve_rx(res, timeout) \ net_pkt_get_reserve_rx_debug(res, timeout, __func__, __LINE__) -struct net_buf *net_pkt_get_reserve_tx_debug(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_tx_debug(uint16_t reserve_head, int32_t timeout, const char *caller, int line); #define net_pkt_get_reserve_tx(res, timeout) \ @@ -543,91 +542,109 @@ struct net_buf *net_pkt_get_reserve_tx_data_debug(uint16_t reserve_head, #define net_pkt_get_reserve_tx_data(res, timeout) \ net_pkt_get_reserve_tx_data_debug(res, timeout, __func__, __LINE__) -struct net_buf *net_pkt_get_frag_debug(struct net_buf *buf, +struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt, int32_t timeout, const char *caller, int line); -#define net_pkt_get_frag(buf, timeout) \ - net_pkt_get_frag_debug(buf, timeout, __func__, __LINE__) +#define net_pkt_get_frag(pkt, timeout) \ + net_pkt_get_frag_debug(pkt, timeout, __func__, __LINE__) -void net_pkt_unref_debug(struct net_buf *buf, const char *caller, int line); -#define net_pkt_unref(buf) net_pkt_unref_debug(buf, __func__, __LINE__) +void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line); +#define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__) -struct net_buf *net_pkt_ref_debug(struct net_buf *buf, const char *caller, +struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller, int line); -#define net_pkt_ref(buf) net_pkt_ref_debug(buf, __func__, __LINE__) +#define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__) -struct net_buf *net_pkt_frag_del_debug(struct net_buf *parent, +struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag, + const char *caller, int line); +#define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__) + +void net_pkt_frag_unref_debug(struct net_buf *frag, + const char *caller, int line); +#define net_pkt_frag_unref(frag) \ + net_pkt_frag_unref_debug(frag, __func__, __LINE__) + +struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt, + struct net_buf *parent, struct net_buf *frag, const char *caller, int line); -#define net_pkt_frag_del(parent, frag) \ - net_pkt_frag_del_debug(parent, frag, __func__, __LINE__) +#define net_pkt_frag_del(pkt, parent, frag) \ + net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__) + +void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag, + const char *caller, int line); +#define net_pkt_frag_add(pkt, frag) \ + net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__) + +void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag, + const char *caller, int line); +#define net_pkt_frag_insert(pkt, frag) \ + net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__) /** * @brief Print fragment list and the fragment sizes * * @details Only available if debugging is activated. * - * @param buf Network buffer fragment. This should be the first fragment (data) - * in the fragment list. + * @param pkt Network pkt. */ -void net_pkt_print_frags(struct net_buf *buf); +void net_pkt_print_frags(struct net_pkt *pkt); #else /* CONFIG_NET_DEBUG_NET_PKT */ #define net_pkt_print_frags(...) /** - * @brief Get buffer from the given buffer pool. + * @brief Get packet from the given packet slab. * - * @details Get network buffer from the specific buffer pool. + * @details Get network packet from the specific packet slab. * - * @param pool Network buffer pool. + * @param slab Network packet slab. * @param reserve_head How many bytes to reserve for headroom. - * @param timeout Affects the action taken should the net buf pool be empty. + * @param timeout Affects the action taken should the net pkt slab be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * - * @return Network buffer if successful, NULL otherwise. + * @return Network packet if successful, NULL otherwise. */ -struct net_buf *net_pkt_get_reserve(struct net_buf_pool *pool, +struct net_pkt *net_pkt_get_reserve(struct k_mem_slab *slab, uint16_t reserve_head, int32_t timeout); /** - * @brief Get buffer from the RX buffers pool. + * @brief Get packet from the RX packet slab. * - * @details Get network buffer from RX buffer pool. You must have + * @details Get network packet from RX packet slab. You must have * network context before able to use this function. * - * @param context Network context that will be related to - * this buffer. - * @param timeout Affects the action taken should the net buf pool be empty. + * @param context Network context that will be related to this packet. + * @param timeout Affects the action taken should the net pkt slab be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * - * @return Network buffer if successful, NULL otherwise. + * @return Network packet if successful, NULL otherwise. */ -struct net_buf *net_pkt_get_rx(struct net_context *context, +struct net_pkt *net_pkt_get_rx(struct net_context *context, int32_t timeout); /** - * @brief Get buffer from the TX buffers pool. + * @brief Get packet from the TX packets slab. * - * @details Get network buffer from TX buffer pool. You must have + * @details Get network packet from TX packet slab. You must have * network context before able to use this function. * * @param context Network context that will be related to - * this buffer. - * @param timeout Affects the action taken should the net buf pool be empty. + * this packet. + * @param timeout Affects the action taken should the net pkt slab be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * - * @return Network buffer if successful, NULL otherwise. + * @return Network packet if successful, NULL otherwise. */ -struct net_buf *net_pkt_get_tx(struct net_context *context, +struct net_pkt *net_pkt_get_tx(struct net_context *context, int32_t timeout); /** @@ -649,39 +666,39 @@ struct net_buf *net_pkt_get_data(struct net_context *context, int32_t timeout); /** - * @brief Get RX buffer from pool but also reserve headroom for + * @brief Get RX packet from slab but also reserve headroom for * potential headers. * * @details Normally this version is not useful for applications * but is mainly used by network fragmentation code. * * @param reserve_head How many bytes to reserve for headroom. - * @param timeout Affects the action taken should the net buf pool be empty. + * @param timeout Affects the action taken should the net pkt slab be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * - * @return Network buffer if successful, NULL otherwise. + * @return Network packet if successful, NULL otherwise. */ -struct net_buf *net_pkt_get_reserve_rx(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_rx(uint16_t reserve_head, int32_t timeout); /** - * @brief Get TX buffer from pool but also reserve headroom for + * @brief Get TX packet from slab but also reserve headroom for * potential headers. * * @details Normally this version is not useful for applications * but is mainly used by network fragmentation code. * * @param reserve_head How many bytes to reserve for headroom. - * @param timeout Affects the action taken should the net buf pool be empty. + * @param timeout Affects the action taken should the net pkt slab be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * - * @return Network buffer if successful, NULL otherwise. + * @return Network packet if successful, NULL otherwise. */ -struct net_buf *net_pkt_get_reserve_tx(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_tx(uint16_t reserve_head, int32_t timeout); /** @@ -724,8 +741,7 @@ struct net_buf *net_pkt_get_reserve_tx_data(uint16_t reserve_head, * @brief Get a data fragment that might be from user specific * buffer pool or from global DATA pool. * - * @param buf Network buffer. This must be the first buffer of the - * buffer chain with user data part in it. + * @param pkt Network packet. * @param timeout Affects the action taken should the net buf pool be empty. * If K_NO_WAIT, then return immediately. If K_FOREVER, then * wait as long as necessary. Otherwise, wait up to the specified @@ -733,48 +749,85 @@ struct net_buf *net_pkt_get_reserve_tx_data(uint16_t reserve_head, * * @return Network buffer if successful, NULL otherwise. */ -struct net_buf *net_pkt_get_frag(struct net_buf *buf, int32_t timeout); +struct net_buf *net_pkt_get_frag(struct net_pkt *pkt, int32_t timeout); /** - * @brief Place buffer back into the available buffers pool. + * @brief Place packet back into the available packets slab * - * @details Releases the buffer to other use. This needs to be - * called by application after it has finished with - * the buffer. + * @details Releases the packet to other use. This needs to be + * called by application after it has finished with the packet. * - * @param buf Network buffer to release. + * @param pkt Network packet to release. * */ -void net_pkt_unref(struct net_buf *buf); +void net_pkt_unref(struct net_pkt *pkt); /** - * @brief Increase the ref count + * @brief Increase the packet ref count * - * @details Mark the buffer to be used still. + * @details Mark the packet to be used still. * - * @param buf Network buffer to ref. + * @param pkt Network packet to ref. * - * @return Network buffer if successful, NULL otherwise. + * @return Network packet if successful, NULL otherwise. */ -struct net_buf *net_pkt_ref(struct net_buf *buf); +struct net_pkt *net_pkt_ref(struct net_pkt *pkt); -/** @brief Delete existing fragment from a chain of bufs. +/** + * @brief Increase the packet fragment ref count * - * @param parent Parent buffer/fragment, or NULL if there is no parent. - * @param frag Fragment to delete. + * @details Mark the fragment to be used still. * - * @return Pointer to the buffer following the fragment, or NULL if it - * had no further fragments. + * @param frag Network fragment to ref. + * + * @return a pointer on the referenced Network fragment. */ -struct net_buf *net_pkt_frag_del(struct net_buf *parent, struct net_buf *frag); +struct net_buf *net_pkt_frag_ref(struct net_buf *frag); + +/** + * @brief Decrease the packet fragment ref count + * + * @param frag Network fragment to unref. + */ +void net_pkt_frag_unref(struct net_buf *frag); + +/** + * @brief Delete existing fragment from a packet + * + * @param pkt Network packet from which frag belongs to. + * @param parent parent fragment of frag, or NULL if none. + * @param frag Fragment to delete. + * + * @return Pointer to the following fragment, or NULL if it had no + * further fragments. + */ +struct net_buf *net_pkt_frag_del(struct net_pkt *pkt, + struct net_buf *parent, + struct net_buf *frag); + +/** + * @brief Add a fragment to a packet at the end of its fragment list + * + * @param pkt pkt Network packet where to add the fragment + * @param frag Fragment to add + */ +void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag); + +/** + * @brief Insert a fragment to a packet at the beginning of its framgment list + * + * @param pkt pkt Network packet where to insert the fragment + * @param frag Fragment to insert + */ +void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag); #endif /* CONFIG_NET_DEBUG_NET_PKT */ /** - * @brief Copy a buffer with fragments while reserving some extra space + * @brief Copy a packet fragment list while reserving some extra space * in destination buffer before a copy. * - * @param buf Network buffer. This should be the head of the buffer chain. + * @param pkt Network packet. * @param amount Max amount of data to be copied. * @param reserve Amount of extra data (this is not link layer header) in the * first data fragment that is returned. The function will copy the original @@ -786,15 +839,14 @@ struct net_buf *net_pkt_frag_del(struct net_buf *parent, struct net_buf *frag); * * @return New fragment list if successful, NULL otherwise. */ -struct net_buf *net_pkt_copy(struct net_buf *buf, size_t amount, +struct net_buf *net_pkt_copy(struct net_pkt *pkt, size_t amount, size_t reserve, int32_t timeout); /** - * @brief Copy a buffer with fragments while reserving some extra space + * @brief Copy a packet fragment list while reserving some extra space * in destination buffer before a copy. * - * @param buf Network buffer fragment. This should be the head of the buffer - * chain. + * @param pkt Network packet. * @param reserve Amount of extra data (this is not link layer header) in the * first data fragment that is returned. The function will copy the original * buffer right after the reserved bytes in the first destination fragment. @@ -805,11 +857,12 @@ struct net_buf *net_pkt_copy(struct net_buf *buf, size_t amount, * * @return New fragment list if successful, NULL otherwise. */ -static inline struct net_buf *net_pkt_copy_all(struct net_buf *buf, +static inline struct net_buf *net_pkt_copy_all(struct net_pkt *pkt, size_t reserve, int32_t timeout) { - return net_pkt_copy(buf, net_buf_frags_len(buf), reserve, timeout); + return net_pkt_copy(pkt, net_buf_frags_len(pkt->frags), + reserve, timeout); } /** @@ -825,56 +878,27 @@ static inline struct net_buf *net_pkt_copy_all(struct net_buf *buf, * @return 0 on success * @return -ENOMEM on error */ -int net_pkt_linear_copy(struct net_buf *dst, struct net_buf *src, - uint16_t offset, uint16_t len); +int net_frag_linear_copy(struct net_buf *dst, struct net_buf *src, + uint16_t offset, uint16_t len); /** - * @brief Compact the fragment list. + * @brief Compact the fragment list of a packet. * * @details After this there is no more any free space in individual fragments. - * @param buf Network buffer fragment. This should be the Tx/Rx buffer. - * - * @return True if compact success, False otherwise. (Note that it fails only - * when input is data fragment) + * @param pkt Network packet. * + * @return True if compact success, False otherwise. */ -bool net_pkt_compact(struct net_buf *buf); +bool net_pkt_compact(struct net_pkt *pkt); /** - * @brief Check if the buffer chain is compact or not. - * - * @details The compact here means that is there any free space in the - * fragments. Only the last fragment can have some free space if the fragment - * list is compact. - * - * @param buf Network buffer. - * - * @return True if there is no free space in the fragment list, - * false otherwise. - */ -bool net_pkt_is_compact(struct net_buf *buf); - -/** - * @brief Remove given amount of data from the beginning of fragment list. - * This is similar thing to do as in net_buf_pull() but this function changes - * the fragment list instead of one fragment. - * - * @param buf Network buffer fragment list. - * @param amount Max amount of data to be remove. - * - * @return Pointer to start of the fragment list if successful. NULL can be - * returned if all fragments were removed from the list. - */ -struct net_buf *net_pkt_pull(struct net_buf *buf, size_t amount); - -/** - * @brief Append data to last fragment in fragment list + * @brief Append data to last fragment in fragment list of a packet * * @details Append data to last fragment. If there is not enough space in * last fragment then new data fragment will be created and will be added to * fragment list. Caller has to take care of endianness if needed. * - * @param buf Network buffer fragment list. + * @param pkt Network packet. * @param len Total length of input data * @param data Data to be added * @param timeout Affects the action taken should the net buf pool be empty. @@ -883,72 +907,72 @@ struct net_buf *net_pkt_pull(struct net_buf *buf, size_t amount); * number of milliseconds before timing out. * * @return True if all the data is placed at end of fragment list, - * False otherwise (In-case of false buf might contain input + * False otherwise (In-case of false pkt might contain input * data in the process of placing into fragments). */ -bool net_pkt_append(struct net_buf *buf, uint16_t len, const uint8_t *data, +bool net_pkt_append(struct net_pkt *pkt, uint16_t len, const uint8_t *data, int32_t timeout); /** - * @brief Append uint8_t data to last fragment in fragment list + * @brief Append uint8_t data to last fragment in fragment list of a packet * * @details Append data to last fragment. If there is not enough space in last * fragment then new data fragment will be created and will be added to * fragment list. Caller has to take care of endianness if needed. * - * @param buf Network buffer fragment list. + * @param pkt Network packet. * @param data Data to be added * * @return True if all the data is placed at end of fragment list, - * False otherwise (In-case of false buf might contain input + * False otherwise (In-case of false pkt might contain input * data in the process of placing into fragments). */ -static inline bool net_pkt_append_u8(struct net_buf *buf, uint8_t data) +static inline bool net_pkt_append_u8(struct net_pkt *pkt, uint8_t data) { - return net_pkt_append(buf, 1, &data, K_FOREVER); + return net_pkt_append(pkt, 1, &data, K_FOREVER); } /** - * @brief Append uint16_t data to last fragment in fragment list + * @brief Append uint16_t data to last fragment in fragment list of a packet * * @details Append data to last fragment. If there is not enough space in last * fragment then new data fragment will be created and will be added to * fragment list. Caller has to take care of endianness if needed. * - * @param buf Network buffer fragment list. + * @param pkt Network packet. * @param data Data to be added * * @return True if all the data is placed at end of fragment list, - * False otherwise (In-case of false buf might contain input data + * False otherwise (In-case of false pkt might contain input data * in the process of placing into fragments). */ -static inline bool net_pkt_append_be16(struct net_buf *buf, uint16_t data) +static inline bool net_pkt_append_be16(struct net_pkt *pkt, uint16_t data) { uint16_t value = sys_cpu_to_be16(data); - return net_pkt_append(buf, sizeof(uint16_t), (uint8_t *)&value, + return net_pkt_append(pkt, sizeof(uint16_t), (uint8_t *)&value, K_FOREVER); } /** - * @brief Append uint32_t data to last fragment in fragment list + * @brief Append uint32_t data to last fragment in fragment list of a packet * * @details Append data to last fragment. If there is not enough space in last * fragment then new data fragment will be created and will be added to * fragment list. Caller has to take care of endianness if needed. * - * @param buf Network buffer fragment list. + * @param pkt Network packet. * @param data Data to be added * * @return True if all the data is placed at end of fragment list, - * False otherwise (In-case of false buf might contain input data + * False otherwise (In-case of false pkt might contain input data * in the process of placing into fragments). */ -static inline bool net_pkt_append_be32(struct net_buf *buf, uint32_t data) +static inline bool net_pkt_append_be32(struct net_pkt *pkt, uint32_t data) { uint32_t value = sys_cpu_to_be32(data); - return net_pkt_append(buf, sizeof(uint32_t), (uint8_t *)&value, + return net_pkt_append(pkt, sizeof(uint32_t), (uint8_t *)&value, K_FOREVER); } @@ -959,18 +983,18 @@ static inline bool net_pkt_append_be32(struct net_buf *buf, uint32_t data) * fragment then new data fragment will be created and will be added to * fragment list. Convert data to LE. * - * @param buf Network buffer fragment list. + * @param pkt Network packet fragment list. * @param data Data to be added * * @return True if all the data is placed at end of fragment list, - * False otherwise (In-case of false buf might contain input data + * False otherwise (In-case of false pkt might contain input data * in the process of placing into fragments). */ -static inline bool net_pkt_append_le32(struct net_buf *buf, uint32_t data) +static inline bool net_pkt_append_le32(struct net_pkt *pkt, uint32_t data) { uint32_t value = sys_cpu_to_le32(data); - return net_pkt_append(buf, sizeof(uint32_t), (uint8_t *)&value, + return net_pkt_append(pkt, sizeof(uint32_t), (uint8_t *)&value, K_FOREVER); } @@ -982,7 +1006,7 @@ static inline bool net_pkt_append_le32(struct net_buf *buf, uint32_t data) * all fragments until it reaches N number of bytes. Caller has to take care of * endianness if needed. * - * @param buf Network buffer fragment. + * @param frag Network buffer fragment. * @param offset Offset of input buffer. * @param pos Pointer to position of offset after reading n number of bytes, * this is with respect to return buffer(fragment). @@ -993,8 +1017,8 @@ static inline bool net_pkt_append_le32(struct net_buf *buf, uint32_t data) * NULL and pos is 0 after successful read, * NULL and pos is 0xffff otherwise. */ -struct net_buf *net_pkt_read(struct net_buf *buf, uint16_t offset, - uint16_t *pos, uint16_t len, uint8_t *data); +struct net_buf *net_frag_read(struct net_buf *frag, uint16_t offset, + uint16_t *pos, uint16_t len, uint8_t *data); /** * @brief Skip N number of bytes while reading buffer @@ -1005,7 +1029,7 @@ struct net_buf *net_pkt_read(struct net_buf *buf, uint16_t offset, * when unwanted data (e.g. reserved or not supported data in message) is part * of fragment and want to skip it. * - * @param buf Network buffer fragment. + * @param frag Network buffer fragment. * @param offset Offset of input buffer. * @param pos Pointer to position of offset after reading n number of bytes, * this is with respect to return buffer(fragment). @@ -1015,17 +1039,17 @@ struct net_buf *net_pkt_read(struct net_buf *buf, uint16_t offset, * NULL and pos is 0 after successful skip, * NULL and pos is 0xffff otherwise. */ -static inline struct net_buf *net_pkt_skip(struct net_buf *buf, - uint16_t offset, - uint16_t *pos, uint16_t len) +static inline struct net_buf *net_frag_skip(struct net_buf *frag, + uint16_t offset, + uint16_t *pos, uint16_t len) { - return net_pkt_read(buf, offset, pos, len, NULL); + return net_frag_read(frag, offset, pos, len, NULL); } /** * @brief Get a byte value from fragmented buffer * - * @param buf Network buffer fragment. + * @param frag Network buffer fragment. * @param offset Offset of input buffer. * @param pos Pointer to position of offset after reading 2 bytes, * this is with respect to return buffer(fragment). @@ -1034,18 +1058,18 @@ static inline struct net_buf *net_pkt_skip(struct net_buf *buf, * @return Pointer to fragment after successful read, * NULL otherwise (if pos is 0, NULL is not a failure case). */ -static inline struct net_buf *net_pkt_read_u8(struct net_buf *buf, +static inline struct net_buf *net_frag_read_u8(struct net_buf *frag, uint16_t offset, uint16_t *pos, uint8_t *value) { - return net_pkt_read(buf, offset, pos, 1, value); + return net_frag_read(frag, offset, pos, 1, value); } /** * @brief Get 16 bit big endian value from fragmented buffer * - * @param buf Network buffer fragment. + * @param frag Network buffer fragment. * @param offset Offset of input buffer. * @param pos Pointer to position of offset after reading 2 bytes, * this is with respect to return buffer(fragment). @@ -1054,13 +1078,13 @@ static inline struct net_buf *net_pkt_read_u8(struct net_buf *buf, * @return Pointer to fragment after successful read, * NULL otherwise (if pos is 0, NULL is not a failure case). */ -struct net_buf *net_pkt_read_be16(struct net_buf *buf, uint16_t offset, - uint16_t *pos, uint16_t *value); +struct net_buf *net_frag_read_be16(struct net_buf *frag, uint16_t offset, + uint16_t *pos, uint16_t *value); /** * @brief Get 32 bit big endian value from fragmented buffer * - * @param buf Network buffer fragment. + * @param frag Network buffer fragment. * @param offset Offset of input buffer. * @param pos Pointer to position of offset after reading 4 bytes, * this is with respect to return buffer(fragment). @@ -1069,11 +1093,11 @@ struct net_buf *net_pkt_read_be16(struct net_buf *buf, uint16_t offset, * @return Pointer to fragment after successful read, * NULL otherwise (if pos is 0, NULL is not a failure case). */ -struct net_buf *net_pkt_read_be32(struct net_buf *buf, uint16_t offset, - uint16_t *pos, uint32_t *value); +struct net_buf *net_frag_read_be32(struct net_buf *frag, uint16_t offset, + uint16_t *pos, uint32_t *value); /** - * @brief Write data to an arbitrary offset in a series of fragments. + * @brief Write data to an arbitrary offset in fragments list of a packet. * * @details Write data to an arbitrary offset in a series of fragments. * Offset is based on fragment 'size' and calculates from input fragment @@ -1090,26 +1114,26 @@ struct net_buf *net_pkt_read_be32(struct net_buf *buf, uint16_t offset, * 'overwritten'. Use net_pkt_insert() api if you don't want to overwrite. * * Offset is calculated from starting point of data area in input fragment. - * e.g. Buf(Tx/Rx) - Frag1 - Frag2 - Frag3 - Frag4 + * e.g. Pkt(Tx/Rx) - Frag1 - Frag2 - Frag3 - Frag4 * (Assume FRAG DATA SIZE is 100 bytes after link layer header) * - * 1) net_pkt_write(buf, frag2, 20, &pos, 20, data, K_FOREVER) + * 1) net_pkt_write(pkt, frag2, 20, &pos, 20, data, K_FOREVER) * In this case write starts from "frag2->data + 20", * returns frag2, pos = 40 * - * 2) net_pkt_write(buf, frag1, 150, &pos, 60, data, K_FOREVER) + * 2) net_pkt_write(pkt, frag1, 150, &pos, 60, data, K_FOREVER) * In this case write starts from "frag2->data + 50" * returns frag3, pos = 10 * - * 3) net_pkt_write(buf, frag1, 350, &pos, 30, data, K_FOREVER) + * 3) net_pkt_write(pkt, frag1, 350, &pos, 30, data, K_FOREVER) * In this case write starts from "frag4->data + 50" * returns frag4, pos = 80 * - * 4) net_pkt_write(buf, frag2, 110, &pos, 90, data, K_FOREVER) + * 4) net_pkt_write(pkt, frag2, 110, &pos, 90, data, K_FOREVER) * In this case write starts from "frag3->data + 10" * returns frag4, pos = 0 * - * 5) net_pkt_write(buf, frag4, 110, &pos, 20, data, K_FOREVER) + * 5) net_pkt_write(pkt, frag4, 110, &pos, 20, data, K_FOREVER) * In this case write creates new data fragment and starts from * "frag5->data + 10" * returns frag5, pos = 30 @@ -1117,7 +1141,7 @@ struct net_buf *net_pkt_read_be32(struct net_buf *buf, uint16_t offset, * If input argument frag is NULL, it will create new data fragment * and append at the end of fragment list. * - * @param buf Network buffer fragment list. + * @param pkt Network packet. * @param frag Network buffer fragment. * @param offset Offset * @param pos Position of offset after write completed (this will be @@ -1132,23 +1156,23 @@ struct net_buf *net_pkt_read_be32(struct net_buf *buf, uint16_t offset, * @return Pointer to the fragment and position (*pos) where write ended, * NULL and pos is 0xffff otherwise. */ -struct net_buf *net_pkt_write(struct net_buf *buf, struct net_buf *frag, +struct net_buf *net_pkt_write(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t *pos, uint16_t len, uint8_t *data, int32_t timeout); /* Write uint8_t data to an arbitrary offset in fragment. */ -static inline struct net_buf *net_pkt_write_u8(struct net_buf *buf, +static inline struct net_buf *net_pkt_write_u8(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t *pos, uint8_t data) { - return net_pkt_write(buf, frag, offset, pos, sizeof(uint8_t), + return net_pkt_write(pkt, frag, offset, pos, sizeof(uint8_t), &data, K_FOREVER); } /* Write uint16_t big endian value to an arbitrary offset in fragment. */ -static inline struct net_buf *net_pkt_write_be16(struct net_buf *buf, +static inline struct net_buf *net_pkt_write_be16(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t *pos, @@ -1156,12 +1180,12 @@ static inline struct net_buf *net_pkt_write_be16(struct net_buf *buf, { uint16_t value = htons(data); - return net_pkt_write(buf, frag, offset, pos, sizeof(uint16_t), + return net_pkt_write(pkt, frag, offset, pos, sizeof(uint16_t), (uint8_t *)&value, K_FOREVER); } /* Write uint32_t big endian value to an arbitrary offset in fragment. */ -static inline struct net_buf *net_pkt_write_be32(struct net_buf *buf, +static inline struct net_buf *net_pkt_write_be32(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t *pos, @@ -1169,7 +1193,7 @@ static inline struct net_buf *net_pkt_write_be32(struct net_buf *buf, { uint32_t value = htonl(data); - return net_pkt_write(buf, frag, offset, pos, sizeof(uint32_t), + return net_pkt_write(pkt, frag, offset, pos, sizeof(uint32_t), (uint8_t *)&value, K_FOREVER); } @@ -1185,7 +1209,7 @@ static inline struct net_buf *net_pkt_write_be32(struct net_buf *buf, * If the offset is more than already allocated fragments length then it is an * error case. * - * @param buf Network buffer fragment list. + * @param pkt Network packet. * @param frag Network buffer fragment. * @param offset Offset of fragment where insertion will start. * @param len Length of the data to be inserted. @@ -1195,48 +1219,47 @@ static inline struct net_buf *net_pkt_write_be32(struct net_buf *buf, * wait as long as necessary. Otherwise, wait up to the specified * number of milliseconds before timing out. * - * @return True on success, - * False otherwise. + * @return True on success, False otherwise. */ -bool net_pkt_insert(struct net_buf *buf, struct net_buf *frag, +bool net_pkt_insert(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t len, uint8_t *data, int32_t timeout); /* Insert uint8_t data at an arbitrary offset in a series of fragments. */ -static inline bool net_pkt_insert_u8(struct net_buf *buf, +static inline bool net_pkt_insert_u8(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint8_t data) { - return net_pkt_insert(buf, frag, offset, sizeof(uint8_t), &data, + return net_pkt_insert(pkt, frag, offset, sizeof(uint8_t), &data, K_FOREVER); } /* Insert uint16_t big endian value at an arbitrary offset in a series of * fragments. */ -static inline bool net_pkt_insert_be16(struct net_buf *buf, +static inline bool net_pkt_insert_be16(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t data) { uint16_t value = htons(data); - return net_pkt_insert(buf, frag, offset, sizeof(uint16_t), + return net_pkt_insert(pkt, frag, offset, sizeof(uint16_t), (uint8_t *)&value, K_FOREVER); } /* Insert uint32_t big endian value at an arbitrary offset in a series of * fragments. */ -static inline bool net_pkt_insert_be32(struct net_buf *buf, +static inline bool net_pkt_insert_be32(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint32_t data) { uint32_t value = htonl(data); - return net_pkt_insert(buf, frag, offset, sizeof(uint32_t), + return net_pkt_insert(pkt, frag, offset, sizeof(uint32_t), (uint8_t *)&value, K_FOREVER); } @@ -1246,11 +1269,11 @@ static inline bool net_pkt_insert_be32(struct net_buf *buf, * @details This will generate two new fragments (fragA and fragB) from * one (orig_frag). The original fragment is not modified but two new * fragments are allocated and returned to the caller. The original fragment - * must be part of the chain pointed by the buf parameter. If the len parameter + * must be part of the packet pointed by the pkt parameter. If the len parameter * is larger than the amount of data in the orig fragment, then the fragA will * contain all the data and fragB will be empty. * - * @param buf Head of the network buffer fragment list. + * @param pkt Network packet * @param orig_frag Original network buffer fragment which is to be split. * @param len Amount of data in the first returned fragment. * @param fragA A fragment is returned. This will contain len bytes that @@ -1265,7 +1288,7 @@ static inline bool net_pkt_insert_be32(struct net_buf *buf, * * @return 0 on success, <0 otherwise. */ -int net_pkt_split(struct net_buf *buf, struct net_buf *orig_frag, +int net_pkt_split(struct net_pkt *pkt, struct net_buf *orig_frag, uint16_t len, struct net_buf **fragA, struct net_buf **fragB, int32_t timeout); @@ -1277,8 +1300,8 @@ int net_pkt_split(struct net_buf *buf, struct net_buf *orig_frag, * @param rx_data Pointer to RX DATA pool is returned. * @param tx_data Pointer to TX DATA pool is returned. */ -void net_pkt_get_info(struct net_buf_pool **rx, - struct net_buf_pool **tx, +void net_pkt_get_info(struct k_mem_slab **rx, + struct k_mem_slab **tx, struct net_buf_pool **rx_data, struct net_buf_pool **tx_data); @@ -1288,7 +1311,8 @@ void net_pkt_get_info(struct net_buf_pool **rx, */ void net_pkt_print(void); -typedef void (*net_pkt_allocs_cb_t)(struct net_buf *buf, +typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt, + struct net_buf *buf, const char *func_alloc, int line_alloc, const char *func_free, @@ -1298,6 +1322,7 @@ typedef void (*net_pkt_allocs_cb_t)(struct net_buf *buf, void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data); +const char *net_pkt_slab2str(struct k_mem_slab *slab); const char *net_pkt_pool2str(struct net_buf_pool *pool); #else diff --git a/include/net/zoap.h b/include/net/zoap.h index 472143f2f99..9cc0953308a 100644 --- a/include/net/zoap.h +++ b/include/net/zoap.h @@ -193,7 +193,7 @@ struct zoap_observer { * @brief Representation of a CoAP packet. */ struct zoap_packet { - struct net_buf *buf; + struct net_pkt *pkt; uint8_t *start; /* Start of the payload */ 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). */ struct zoap_pending { - struct net_buf *buf; + struct net_pkt *pkt; struct sockaddr addr; int32_t timeout; uint16_t id; @@ -309,35 +309,35 @@ struct zoap_option { }; /** - * @brief Parses the CoAP packet in @a buf, validating it and - * initializing @a pkt. @a buf must remain valid while @a pkt is used. + * @brief Parses the CoAP packet in @a pkt, validating it and + * initializing @a zpkt. @a pkt must remain valid while @a zpkt is used. * - * @param pkt Packet to be initialized from received @a buf. - * @param buf Buffer containing a CoAP packet, its @a data pointer is + * @param zpkt Packet to be initialized from received @a pkt. + * @param pkt Network Packet containing a CoAP packet, its @a data pointer is * positioned on the start of the CoAP packet. * * @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 - * valid while @a pkt is used. + * @brief Creates a new CoAP packet from a net_pkt. @a pkt must remain + * valid while @a zpkt is used. * - * @param pkt New packet to be initialized using the storage from @a - * buf. - * @param buf Buffer that will contain a CoAP packet + * @param zpkt New packet to be initialized using the storage from @a + * pkt. + * @param pkt Network Packet that will contain a CoAP packet * * @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. * * 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 - * 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 * 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 * the matching resources. * - * @param pkt Packet received + * @param zpkt Packet received * @param resources Array of known resources * @param from Address from which the packet was received * * @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, 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 * 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 * * @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. @@ -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 * 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 * * @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. * * 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 value Pointer to the value of the option, will be copied to the packet * @param len Size of the data to be added * * @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); /** @@ -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 * 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 val Integer value to be added * * @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); /** * @brief Return the values associated with the option of value @a * code. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * @param code Option number to look for * @param options Array of #zoap_option where to store the value * 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, * 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); /** @@ -624,61 +624,61 @@ int zoap_block_transfer_init(struct zoap_block_context *ctx, /** * @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 * information for the Block1 option * * @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); /** * @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 * information for the Block2 option * * @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); /** * @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 * information for the Size1 option * * @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); /** * @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 * information for the Size2 option * * @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); /** - * @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. * - * @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 * * @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); /** @@ -696,93 +696,93 @@ size_t zoap_next_block(struct zoap_block_context *ctx); /** * @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 */ -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. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * * @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. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * @param len Where to store the length of the token * * @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); /** * @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 */ -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. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * * @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. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * @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. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * @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. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * @param token Token to set in the packet, will be copied * @param tokenlen Size of the token to be set, 8 bytes maximum * * @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); /** * @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 */ -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. * - * @param pkt CoAP packet representation + * @param zpkt CoAP packet representation * @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 diff --git a/samples/bluetooth/ipsp/src/main.c b/samples/bluetooth/ipsp/src/main.c index 124d6c6bb67..df24d5cc422 100644 --- a/samples/bluetooth/ipsp/src/main.c +++ b/samples/bluetooth/ipsp/src/main.c @@ -150,28 +150,29 @@ static inline bool get_context(struct net_context **udp_recv6, 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_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; 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; - /* Remove frag link so original buf can be unrefed */ - buf->frags = NULL; + tmp = pkt->frags; + /* Remove frag link so original pkt can be unrefed */ + pkt->frags = NULL; /* First fragment will contain IP header so move the data * 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 * data. @@ -179,14 +180,14 @@ static struct net_buf *build_reply_buf(const char *name, net_buf_pull(tmp, header_len); /* 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, reply_len); - return reply_buf; + return reply_pkt; } 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, - struct net_buf *buf, + struct net_pkt *pkt, struct sockaddr *dst_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_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, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { - struct net_buf *reply_buf; + struct net_pkt *reply_pkt; 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]; int ret; snprintf(dbg, MAX_DBG_PRINT, "UDP IPv%c", 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 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in), pkt_sent, 0, - UINT_TO_POINTER(net_buf_frags_len(reply_buf)), + UINT_TO_POINTER(net_pkt_get_len(reply_pkt)), user_data); if (ret < 0) { 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, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { static char dbg[MAX_DBG_PRINT + 1]; - sa_family_t family = net_pkt_family(buf); - struct net_buf *reply_buf; + sa_family_t family = net_pkt_family(pkt); + struct net_pkt *reply_pkt; int ret; snprintf(dbg, MAX_DBG_PRINT, "TCP IPv%c", 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, - UINT_TO_POINTER(net_buf_frags_len(reply_buf)), + ret = net_context_send(reply_pkt, pkt_sent, K_NO_WAIT, + UINT_TO_POINTER(net_pkt_get_len(reply_pkt)), NULL); if (ret < 0) { printk("Cannot send data to peer (%d)", ret); - net_pkt_unref(reply_buf); + net_pkt_unref(reply_pkt); quit(); } diff --git a/samples/net/coaps_client/src/coaps_client.c b/samples/net/coaps_client/src/coaps_client.c index d840ae24e0d..49119cf9ad2 100644 --- a/samples/net/coaps_client/src/coaps_client.c +++ b/samples/net/coaps_client/src/coaps_client.c @@ -70,7 +70,7 @@ struct zoap_reply replies[NUM_REPLIES]; #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); 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) { - struct net_buf *frag = response->buf->frags; + struct net_buf *frag = response->pkt->frags; while (frag) { msg_dump("reply", frag->data, frag->len); @@ -184,9 +184,10 @@ void dtls_client(void) int ret; struct udp_context ctx; struct dtls_timing_context timer; - struct zoap_packet request, pkt; + struct zoap_packet request, zkt; struct zoap_reply *reply; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint8_t observe = 0; const char *const *p; uint16_t len; @@ -281,8 +282,8 @@ void dtls_client(void) /* Write to server */ retry: - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { goto exit; } @@ -291,9 +292,9 @@ retry: 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) { goto exit; } @@ -336,7 +337,7 @@ retry: } while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE); - net_buf_unref(buf); + net_pkt_unref(pkt); if (ret <= 0) { mbedtls_printf("mbedtls_ssl_write failed returned 0x%x\n", @@ -344,9 +345,9 @@ retry: goto exit; } - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - mbedtls_printf("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + mbedtls_printf("Could not get packet from pool\n"); goto exit; } @@ -356,7 +357,7 @@ retry: goto exit; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = ZOAP_BUF_SIZE - 1; memset(frag->data, 0, ZOAP_BUF_SIZE); @@ -366,7 +367,7 @@ retry: ret == MBEDTLS_ERR_SSL_WANT_WRITE); if (ret <= 0) { - net_buf_unref(buf); + net_pkt_unref(pkt); switch (ret) { case MBEDTLS_ERR_SSL_TIMEOUT: @@ -388,18 +389,18 @@ retry: len = ret; frag->len = len; - ret = zoap_packet_parse(&pkt, buf); + ret = zoap_packet_parse(&zpkt, pkt); if (ret) { mbedtls_printf("Could not parse packet\n"); goto exit; } - reply = zoap_response_received(&pkt, NULL, replies, NUM_REPLIES); + reply = zoap_response_received(&zpkt, NULL, replies, NUM_REPLIES); if (!reply) { mbedtls_printf("No handler for response (%d)\n", ret); } - net_buf_unref(buf); + net_pkt_unref(pkt); mbedtls_ssl_close_notify(&ssl); exit: diff --git a/samples/net/coaps_client/src/udp.c b/samples/net/coaps_client/src/udp.c index a243b6f6511..ce53debb080 100644 --- a/samples/net/coaps_client/src/udp.c +++ b/samples/net/coaps_client/src/udp.c @@ -37,14 +37,14 @@ static void set_destination(struct sockaddr *addr) } 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; ARG_UNUSED(context); ARG_UNUSED(status); - ctx->rx_pkt = buf; + ctx->rx_pkt = pkt; 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 net_context *udp_ctx; - struct net_buf *send_buf; + struct net_pkt *send_pkt; struct sockaddr dst_addr; int rc, len; udp_ctx = ctx->net_ctx; - send_buf = net_pkt_get_tx(udp_ctx, K_FOREVER); - if (!send_buf) { + send_pkt = net_pkt_get_tx(udp_ctx, K_FOREVER); + if (!send_pkt) { 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) { return MBEDTLS_ERR_SSL_INTERNAL_ERROR; } set_destination(&dst_addr); - len = net_buf_frags_len(send_buf); + len = net_pkt_frags_len(send_pkt); 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); if (rc < 0) { - net_pkt_unref(send_buf); + net_pkt_unref(send_pkt); return MBEDTLS_ERR_SSL_INTERNAL_ERROR; } else { return len; diff --git a/samples/net/coaps_client/src/udp.h b/samples/net/coaps_client/src/udp.h index ad7885e5cb7..bd3b66a036f 100644 --- a/samples/net/coaps_client/src/udp.h +++ b/samples/net/coaps_client/src/udp.h @@ -11,7 +11,7 @@ struct udp_context { struct net_context *net_ctx; - struct net_buf *rx_pkt; + struct net_pkt *rx_pkt; struct k_sem rx_sem; int remaining; }; diff --git a/samples/net/coaps_server/src/coaps_server.c b/samples/net/coaps_server/src/coaps_server.c index 6c8eff30059..1bd11554033 100644 --- a/samples/net/coaps_server/src/coaps_server.c +++ b/samples/net/coaps_server/src/coaps_server.c @@ -60,7 +60,7 @@ static unsigned char heap[8192]; #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); /* @@ -78,7 +78,8 @@ static mbedtls_ssl_context *curr_ctx; 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; uint8_t code, type; 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("*******\n"); - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { return -ENOMEM; } @@ -102,9 +103,9 @@ static int send_response(struct zoap_packet *request, uint8_t response_code) 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) { return -EINVAL; } @@ -123,7 +124,7 @@ static int send_response(struct zoap_packet *request, uint8_t response_code) r = 0; } - net_buf_unref(buf); + net_pkt_unref(pkt); return r; } @@ -150,7 +151,8 @@ static int piggyback_get(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; uint8_t *payload, code, type; 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("*******\n"); - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { return -ENOMEM; } @@ -174,9 +176,9 @@ static int piggyback_get(struct zoap_resource *resource, 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) { return -EINVAL; } @@ -212,7 +214,7 @@ static int piggyback_get(struct zoap_resource *resource, r = 0; } - net_buf_unref(buf); + net_pkt_unref(pkt); return r; } @@ -221,7 +223,8 @@ static int query_get(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { struct zoap_option options[4]; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; uint8_t *payload, code, type; uint16_t len, id; @@ -258,8 +261,8 @@ static int query_get(struct zoap_resource *resource, printk("*******\n"); - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { return -ENOMEM; } @@ -268,9 +271,9 @@ static int query_get(struct zoap_resource *resource, 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) { return -EINVAL; } @@ -307,7 +310,7 @@ static int query_get(struct zoap_resource *resource, r = 0; } - net_buf_unref(buf); + net_pkt_unref(pkt); return r; } @@ -416,8 +419,9 @@ void dtls_server(void) int len, ret = 0; struct udp_context ctx; struct dtls_timing_context timer; - struct zoap_packet pkt; - struct net_buf *buf, *frag; + struct zoap_packet zpkt; + struct net_pkt *pkt; + struct net_buf *frag; mbedtls_ssl_cookie_ctx cookie_ctx; mbedtls_entropy_context entropy; @@ -549,9 +553,9 @@ reset: do { /* Read the request */ - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - mbedtls_printf("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + mbedtls_printf("Could not get packet from slab\n"); goto exit; } @@ -561,7 +565,7 @@ reset: goto exit; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = ZOAP_BUF_SIZE - 1; memset(frag->data, 0, ZOAP_BUF_SIZE); @@ -572,7 +576,7 @@ reset: } if (ret <= 0) { - net_buf_unref(buf); + net_pkt_unref(pkt); switch (ret) { case MBEDTLS_ERR_SSL_TIMEOUT: @@ -594,20 +598,20 @@ reset: len = ret; frag->len = len; - ret = zoap_packet_parse(&pkt, buf); + ret = zoap_packet_parse(&zpkt, pkt); if (ret) { mbedtls_printf("Could not parse packet\n"); goto exit; } - ret = zoap_handle_request(&pkt, resources, + ret = zoap_handle_request(&zpkt, resources, (const struct sockaddr *)&ssl); if (ret < 0) { mbedtls_printf("No handler for such request (%d)\n", ret); } - net_buf_unref(buf); + net_pkt_unref(pkt); } while (1); diff --git a/samples/net/coaps_server/src/udp.c b/samples/net/coaps_server/src/udp.c index 033b114f0b8..293fa39d5ab 100644 --- a/samples/net/coaps_server/src/udp.c +++ b/samples/net/coaps_server/src/udp.c @@ -18,22 +18,22 @@ 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_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, - struct net_buf *buf, int status, void *user_data) + struct net_pkt *pkt, int status, void *user_data) { struct udp_context *ctx = user_data; ARG_UNUSED(context); ARG_UNUSED(status); - ctx->rx_pkt = buf; + ctx->rx_pkt = pkt; 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 net_context *net_ctx; - struct net_buf *send_buf; + struct net_pkt *send_pkt; int rc, len; net_ctx = ctx->net_ctx; - send_buf = net_pkt_get_tx(net_ctx, K_FOREVER); - if (!send_buf) { - printk("cannot create buf\n"); + send_pkt = net_pkt_get_tx(net_ctx, K_FOREVER); + if (!send_pkt) { + printk("cannot create pkt\n"); 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) { printk("cannot write buf\n"); 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); if (rc < 0) { printk("Cannot send data to peer (%d)\n", rc); - net_pkt_unref(send_buf); + net_pkt_unref(send_pkt); return -EIO; } else { return len; @@ -77,7 +77,8 @@ int udp_rx(void *context, unsigned char *buf, size_t size) { struct udp_context *ctx = context; 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; uint8_t *ptr; int pos; @@ -91,12 +92,12 @@ int udp_rx(void *context, unsigned char *buf, size_t size) 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); - rx_buf = rx_buf->frags; + ptr = net_pkt_appdata(rx_pkt); + rx_buf = rx_pkt->frags; len = rx_buf->len - (ptr - rx_buf->data); pos = 0; diff --git a/samples/net/coaps_server/src/udp.h b/samples/net/coaps_server/src/udp.h index 9bfc2ad5e35..02814b8ea08 100644 --- a/samples/net/coaps_server/src/udp.h +++ b/samples/net/coaps_server/src/udp.h @@ -11,7 +11,7 @@ struct udp_context { struct net_context *net_ctx; - struct net_buf *rx_pkt; + struct net_pkt *rx_pkt; struct k_sem rx_sem; int remaining; char client_id; diff --git a/samples/net/echo_client/src/echo-client.c b/samples/net/echo_client/src/echo-client.c index 21f1df9d50c..e72fd0cd75a 100644 --- a/samples/net/echo_client/src/echo-client.c +++ b/samples/net/echo_client/src/echo-client.c @@ -77,10 +77,10 @@ static int ipsum_len; */ #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #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); -static struct net_buf_pool *tx_tcp_pool(void) +static struct k_mem_slab *tx_tcp_slab(void) { return &echo_tx_tcp; } @@ -92,10 +92,10 @@ static struct net_buf_pool *data_tcp_pool(void) #endif #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); -static struct net_buf_pool *tx_udp_pool(void) +static struct k_mem_slab *tx_udp_slab(void) { return &echo_tx_udp; } @@ -294,7 +294,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, sizeof(struct sockaddr_in6)); @@ -313,7 +313,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, sizeof(struct sockaddr_in)); @@ -334,7 +334,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, (struct sockaddr *)&my_addr6, @@ -356,7 +356,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, (struct sockaddr *)&my_addr4, @@ -389,25 +389,25 @@ static inline bool wait_reply(const char *name, 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, int expecting_len) { - struct net_buf *send_buf; + struct net_pkt *send_pkt; 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, - K_FOREVER); + status = net_pkt_append(send_pkt, expecting_len, lorem_ipsum, + K_FOREVER); if (!status) { - NET_ERR("%s: cannot create send buf", name); + NET_ERR("%s: cannot create send pkt", name); return NULL; } - return send_buf; + return send_pkt; } 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, - struct net_buf *buf, + struct net_pkt *pkt, struct sockaddr *dst_addr) { - ARG_UNUSED(buf); + ARG_UNUSED(pkt); #if defined(CONFIG_NET_IPV6) if (family == AF_INET6) { @@ -453,37 +453,38 @@ static inline void set_dst_addr(sa_family_t family, } #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 len; - /* Buf will now point to first fragment with IP header + /* frag will now point to first fragment with IP header * in it. */ - buf = buf->frags; + frag = pkt->frags; /* Do not include the protocol headers in the first fragment. * The remaining fragments contain only data so the user data * 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)) { NET_DBG("Invalid data received"); return false; } else { pos += len; - buf = buf->frags; - if (!buf) { + frag = frag->frags; + if (!frag) { break; } - ptr = buf->data; - len = buf->len; + ptr = frag->data; + len = frag->len; } } @@ -509,7 +510,7 @@ static bool send_udp_data(struct net_context *udp, struct data *data) { bool status = false; - struct net_buf *send_buf; + struct net_pkt *send_pkt; struct sockaddr dst_addr; socklen_t addrlen; size_t len; @@ -517,18 +518,18 @@ static bool send_udp_data(struct net_context *udp, data->expecting_udp = sys_rand32_get() % ipsum_len; - send_buf = prepare_send_buf(proto, udp, data->expecting_udp); - if (!send_buf) { + send_pkt = prepare_send_pkt(proto, udp, data->expecting_udp); + if (!send_pkt) { goto out; } - len = net_buf_frags_len(send_buf); + len = net_pkt_get_len(send_pkt); NET_ASSERT_INFO(data->expecting_udp == len, "Data to send %d bytes, real len %zu", data->expecting_udp, len); - set_dst_addr(family, send_buf, &dst_addr); + set_dst_addr(family, send_pkt, &dst_addr); if (family == AF_INET6) { addrlen = sizeof(struct sockaddr_in6); @@ -536,13 +537,13 @@ static bool send_udp_data(struct net_context *udp, 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, UINT_TO_POINTER(len), proto); if (ret < 0) { NET_ERR("Cannot send %s data to peer (%d)", proto, ret); - net_pkt_unref(send_buf); + net_pkt_unref(send_pkt); } else { status = true; } @@ -552,11 +553,11 @@ out: } static void udp_received(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, 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 k_sem *recv; @@ -569,16 +570,16 @@ static void udp_received(struct net_context *context, 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", - 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_pkt_unref(buf); + net_pkt_unref(pkt); k_sem_give(recv); } @@ -613,17 +614,17 @@ static void send_udp(struct net_context *udp, #endif /* CONFIG_NET_UDP */ #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) { - uint8_t *ptr = net_pkt_appdata(buf), *start; + uint8_t *ptr = net_pkt_appdata(pkt), *start; int pos = 0; struct net_buf *frag; int len; /* 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. * 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; } - NET_DBG("Compared %d bytes, all ok", net_pkt_appdatalen(buf)); + NET_DBG("Compared %d bytes, all ok", net_pkt_appdatalen(pkt)); return true; } static void tcp_received(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -665,27 +666,27 @@ static void tcp_received(struct net_context *context, ARG_UNUSED(status); - if (!buf || net_pkt_appdatalen(buf) == 0) { - if (buf) { - net_pkt_unref(buf); + if (!pkt || net_pkt_appdatalen(pkt) == 0) { + if (pkt) { + net_pkt_unref(pkt); } return; } - if (net_pkt_family(buf) == AF_INET6) { + if (net_pkt_family(pkt) == AF_INET6) { proto = "IPv6"; } else { proto = "IPv4"; } 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"); } else { - data->received_tcp += net_pkt_appdatalen(buf); + data->received_tcp += net_pkt_appdatalen(pkt); } 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); } - net_pkt_unref(buf); + net_pkt_unref(pkt); } static void setup_tcp_recv(struct net_context *tcp, @@ -729,7 +730,7 @@ static bool send_tcp_data(struct net_context *ctx, char *proto, struct data *data) { - struct net_buf *send_buf; + struct net_pkt *send_pkt; bool status = false; size_t len; int ret; @@ -737,22 +738,22 @@ static bool send_tcp_data(struct net_context *ctx, data->expecting_tcp = sys_rand32_get() % ipsum_len; data->received_tcp = 0; - send_buf = prepare_send_buf(proto, ctx, data->expecting_tcp); - if (!send_buf) { + send_pkt = prepare_send_pkt(proto, ctx, data->expecting_tcp); + if (!send_pkt) { goto out; } - len = net_buf_frags_len(send_buf); + len = net_pkt_get_len(send_pkt); NET_ASSERT_INFO(data->expecting_tcp == len, "%s data to send %d bytes, real len %zu", 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); if (ret < 0) { NET_ERR("Cannot send %s data to peer (%d)", proto, ret); - net_pkt_unref(send_buf); + net_pkt_unref(send_pkt); } else { status = true; } diff --git a/samples/net/echo_server/src/echo-server.c b/samples/net/echo_server/src/echo-server.c index 38860635726..f94b5805b55 100644 --- a/samples/net/echo_server/src/echo-server.c +++ b/samples/net/echo_server/src/echo-server.c @@ -56,10 +56,10 @@ static struct in_addr in4addr_my = MY_IP4ADDR; */ #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) #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); -static struct net_buf_pool *tx_tcp_pool(void) +static struct k_mem_slab *tx_tcp_slab(void) { return &echo_tx_tcp; } @@ -71,10 +71,10 @@ static struct net_buf_pool *data_tcp_pool(void) #endif #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); -static struct net_buf_pool *tx_udp_pool(void) +static struct k_mem_slab *tx_udp_slab(void) { return &echo_tx_udp; } @@ -184,7 +184,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, sizeof(struct sockaddr_in6)); @@ -203,7 +203,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, sizeof(struct sockaddr_in)); @@ -224,7 +224,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, (struct sockaddr *)&my_addr6, @@ -252,7 +252,7 @@ static inline bool get_context(struct net_context **udp_recv4, 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, (struct sockaddr *)&my_addr4, @@ -274,32 +274,33 @@ static inline bool get_context(struct net_context **udp_recv4, 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_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; 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; } - 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 * 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); @@ -326,25 +327,25 @@ static struct net_buf *build_reply_buf(const char *name, * in sending side we add the link layer * 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); 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, "Received %d bytes, sending %d bytes", recv_len - header_len, reply_len); - return reply_buf; + return reply_pkt; } 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) static inline void set_dst_addr(sa_family_t family, - struct net_buf *buf, + struct net_pkt *pkt, struct sockaddr *dst_addr) { #if defined(CONFIG_NET_IPV6) if (family == AF_INET6) { 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_port = NET_UDP_BUF(buf)->src_port; + net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(pkt)->src_port; } #endif /* CONFIG_NET_IPV6) */ #if defined(CONFIG_NET_IPV4) if (family == AF_INET) { 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_port = NET_UDP_BUF(buf)->src_port; + net_sin(dst_addr)->sin_port = NET_UDP_BUF(pkt)->src_port; } #endif /* CONFIG_NET_IPV6) */ } static void udp_received(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { - struct net_buf *reply_buf; + struct net_pkt *reply_pkt; 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]; int ret; snprintk(dbg, MAX_DBG_PRINT, "UDP IPv%c", 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; } - ret = net_context_sendto(reply_buf, &dst_addr, + ret = net_context_sendto(reply_pkt, &dst_addr, family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in), pkt_sent, 0, - UINT_TO_POINTER(net_buf_frags_len(reply_buf)), + UINT_TO_POINTER(net_pkt_get_len(reply_pkt)), user_data); if (ret < 0) { 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) static void tcp_received(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { static char dbg[MAX_DBG_PRINT + 1]; - struct net_buf *reply_buf; + struct net_pkt *reply_pkt; sa_family_t family; int ret; - if (!buf) { + if (!pkt) { /* EOF condition */ return; } - family = net_pkt_family(buf); + family = net_pkt_family(pkt); snprintk(dbg, MAX_DBG_PRINT, "TCP IPv%c", 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; } - ret = net_context_send(reply_buf, pkt_sent, K_NO_WAIT, - UINT_TO_POINTER(net_buf_frags_len(reply_buf)), + ret = net_context_send(reply_pkt, pkt_sent, K_NO_WAIT, + UINT_TO_POINTER(net_pkt_get_len(reply_pkt)), NULL); if (ret < 0) { NET_ERR("Cannot send data to peer (%d)", ret); - net_pkt_unref(reply_buf); + net_pkt_unref(reply_pkt); quit(); } diff --git a/samples/net/http_client/src/http_client_rcv.c b/samples/net/http_client/src/http_client_rcv.c index 3478d439c5b..c9bd0314ae9 100644 --- a/samples/net/http_client/src/http_client_rcv.c +++ b/samples/net/http_client/src/http_client_rcv.c @@ -14,7 +14,7 @@ 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 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); - 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) { rc = -ENOMEM; goto lb_exit; @@ -50,15 +50,15 @@ void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx) lb_exit: net_buf_unref(data_buf); - net_buf_unref(rx); + net_pkt_unref(rx); } #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 net_buf *buf = rx; + struct net_buf *frag = rx->frags; uint16_t offset; 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); - offset = net_buf_frags_len(buf) - net_pkt_appdatalen(buf); + offset = net_pkt_frags_len(rx) - net_pkt_appdatalen(rx); /* find the fragment */ - while (buf && offset >= buf->len) { - offset -= buf->len; - buf = buf->frags; + while (frag && offset >= frag->len) { + offset -= frag->len; + frag = frag->frags; } - while (buf) { + while (frag) { (void)http_parser_execute(&http_ctx->parser, &http_ctx->settings, - buf->data + offset, - buf->len - offset); + frag->data + offset, + frag->len - offset); /* after the first iteration, we set offset to 0 */ offset = 0; @@ -91,11 +91,11 @@ void http_receive_cb(struct tcp_client_ctx *tcp_ctx, struct net_buf *rx) goto lb_exit; } - buf = buf->frags; + frag = frag->frags; } lb_exit: - net_buf_unref(rx); + net_pkt_unref(rx); } #endif diff --git a/samples/net/http_client/src/http_client_rcv.h b/samples/net/http_client/src/http_client_rcv.h index d75d9708e04..5bdca8ddd28 100644 --- a/samples/net/http_client/src/http_client_rcv.h +++ b/samples/net/http_client/src/http_client_rcv.h @@ -10,6 +10,6 @@ #include "tcp_client.h" /* 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 diff --git a/samples/net/http_client/src/tcp_client.c b/samples/net/http_client/src/tcp_client.c index 652a8532bdf..15506206829 100644 --- a/samples/net/http_client/src/tcp_client.c +++ b/samples/net/http_client/src/tcp_client.c @@ -78,7 +78,7 @@ lb_exit: } 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) { 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; } - /* receive_cb must take ownership of the rx buffer */ + /* receive_cb must take ownership of the rx packet */ if (ctx->receive_cb) { ctx->receive_cb(ctx, rx); return; } lb_exit: - net_buf_unref(rx); + net_pkt_unref(rx); } int tcp_connect(struct tcp_client_ctx *ctx, const char *server_addr, diff --git a/samples/net/http_client/src/tcp_client.h b/samples/net/http_client/src/tcp_client.h index 581321c3533..011a09ff77c 100644 --- a/samples/net/http_client/src/tcp_client.h +++ b/samples/net/http_client/src/tcp_client.h @@ -18,7 +18,7 @@ struct tcp_client_ctx { /* Network timeout */ int32_t timeout; /* 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); diff --git a/samples/net/http_server/src/http_server.c b/samples/net/http_server/src/http_server.c index 78ab60fe76e..2908c448f86 100644 --- a/samples/net/http_server/src/http_server.c +++ b/samples/net/http_server/src/http_server.c @@ -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); -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) { 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; } - offset = net_buf_frags_len(rx) - rcv_len; - rc = net_pkt_linear_copy(data, rx, offset, rcv_len); + offset = net_pkt_get_len(rx) - rcv_len; + rc = net_frag_linear_copy(data, rx->frags, offset, rcv_len); if (rc != 0) { printf("[%s:%d] Linear copy error\n", __func__, __LINE__); goto lb_exit; @@ -151,8 +151,8 @@ void http_rx_tx(struct net_context *net_ctx, struct net_buf *rx, int status, } lb_exit: - net_buf_unref(data); - net_buf_unref(rx); + net_pkt_frag_unref(data); + net_pkt_unref(rx); http_ctx_release(http_ctx); } diff --git a/samples/net/http_server/src/http_server.h b/samples/net/http_server/src/http_server.h index 437d025ce01..3169fff116e 100644 --- a/samples/net/http_server/src/http_server.h +++ b/samples/net/http_server/src/http_server.h @@ -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 * header fields or an error message * @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 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); /** diff --git a/samples/net/irc_bot/src/irc-bot.c b/samples/net/irc_bot/src/irc-bot.c index 252d02266d9..d8d75e8e74d 100644 --- a/samples/net/irc_bot/src/irc-bot.c +++ b/samples/net/irc_bot/src/irc-bot.c @@ -138,18 +138,18 @@ panic(const char *msg) static int 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); - if (!send_buf) { + send_pkt = net_pkt_get_tx(ctx, K_FOREVER); + if (!send_pkt) { 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 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 @@ -271,7 +271,7 @@ process_command(struct zirc *irc, char *cmd, size_t len) #undef CMD 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) { struct zirc *irc = data; @@ -280,7 +280,7 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf, size_t len; uint16_t pos = 0, cmd_len = 0; - if (!buf) { + if (!pkt) { /* TODO: notify of disconnection, maybe reconnect? */ NET_ERR("Disconnected\n"); return; @@ -289,14 +289,14 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf, if (status) { /* TODO: handle connection error */ NET_ERR("Connection error: %d\n", -status); - net_pkt_unref(buf); + net_pkt_unref(pkt); return; } /* tmp points to fragment containing IP header */ - tmp = buf->frags; + tmp = pkt->frags; /* skip pos to the first TCP payload */ - pos = net_pkt_appdata(buf) - tmp->data; + pos = net_pkt_appdata(pkt) - tmp->data; while (tmp) { len = tmp->len - pos; @@ -314,13 +314,13 @@ on_context_recv(struct net_context *ctx, struct net_buf *buf, 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; if (end_of_line) { /* skip the /n char after /r */ 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'; @@ -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? */ } diff --git a/samples/net/leds_demo/src/leds-demo.c b/samples/net/leds_demo/src/leds-demo.c index 03b6965b256..aa2742577f8 100644 --- a/samples/net/leds_demo/src/leds-demo.c +++ b/samples/net/leds_demo/src/leds-demo.c @@ -95,7 +95,8 @@ static int led_get(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; const char *str; uint8_t *payload; @@ -104,8 +105,8 @@ static int led_get(struct zoap_resource *resource, id = zoap_header_get_id(request); - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { return -ENOMEM; } @@ -114,9 +115,9 @@ static int led_get(struct zoap_resource *resource, 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) { return -EINVAL; } @@ -147,10 +148,10 @@ static int led_get(struct zoap_resource *resource, 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); if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return r; @@ -160,7 +161,8 @@ static int led_post(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; const char *str; uint8_t *payload; @@ -176,8 +178,8 @@ static int led_post(struct zoap_resource *resource, id = zoap_header_get_id(request); - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { return -ENOMEM; } @@ -186,9 +188,9 @@ static int led_post(struct zoap_resource *resource, 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) { return -EINVAL; } @@ -225,10 +227,10 @@ static int led_post(struct zoap_resource *resource, 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); if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return r; @@ -238,7 +240,8 @@ static int led_put(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; const char *str; uint8_t *payload; @@ -259,8 +262,8 @@ static int led_put(struct zoap_resource *resource, id = zoap_header_get_id(request); - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { return -ENOMEM; } @@ -269,9 +272,9 @@ static int led_put(struct zoap_resource *resource, 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) { return -EINVAL; } @@ -304,10 +307,10 @@ static int led_put(struct zoap_resource *resource, 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); if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return r; @@ -318,7 +321,8 @@ static int dummy_get(struct zoap_resource *resource, const struct sockaddr *from) { 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; uint8_t *payload; uint16_t len, id; @@ -326,8 +330,8 @@ static int dummy_get(struct zoap_resource *resource, id = zoap_header_get_id(request); - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { return -ENOMEM; } @@ -336,9 +340,9 @@ static int dummy_get(struct zoap_resource *resource, 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) { return -EINVAL; } @@ -361,10 +365,10 @@ static int dummy_get(struct zoap_resource *resource, 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); if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return r; @@ -402,7 +406,7 @@ static struct zoap_resource resources[] = { }; static void udp_receive(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -410,28 +414,28 @@ static void udp_receive(struct net_context *context, struct sockaddr_in6 from; int r, header_len; - net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(buf)->src); - from.sin6_port = NET_UDP_BUF(buf)->src_port; + net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(pkt)->src); + from.sin6_port = NET_UDP_BUF(pkt)->src_port; from.sin6_family = AF_INET6; /* * zoap expects that buffer->data starts at the * beginning of the CoAP header */ - header_len = net_pkt_appdata(buf) - buf->frags->data; - net_buf_pull(buf->frags, header_len); + header_len = net_pkt_appdata(pkt) - pkt->frags->data; + net_buf_pull(pkt->frags, header_len); - r = zoap_packet_parse(&request, buf); + r = zoap_packet_parse(&request, pkt); if (r < 0) { NET_ERR("Invalid data received (%d)\n", r); - net_pkt_unref(buf); + net_pkt_unref(pkt); return; } r = zoap_handle_request(&request, resources, (const struct sockaddr *) &from); - net_pkt_unref(buf); + net_pkt_unref(pkt); if (r < 0) { NET_ERR("No handler for such request (%d)\n", r); diff --git a/samples/net/mbedtls_sslclient/src/tcp.c b/samples/net/mbedtls_sslclient/src/tcp.c index 65f382ffd00..e24acf53f32 100644 --- a/samples/net/mbedtls_sslclient/src/tcp.c +++ b/samples/net/mbedtls_sslclient/src/tcp.c @@ -32,42 +32,42 @@ NET_BUF_POOL_DEFINE(tcp_msg_pool, TCP_BUF_CTR, TCP_BUF_SIZE, 0, NULL); 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); 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) { struct tcp_context *ctx = context; struct net_context *tcp_ctx; - struct net_buf *send_buf; + struct net_pkt *send_pkt; int rc, len; tcp_ctx = ctx->net_ctx; - send_buf = net_pkt_get_tx(tcp_ctx, K_FOREVER); - if (!send_buf) { - printk("cannot create buf\n"); + send_pkt = net_pkt_get_tx(tcp_ctx, K_FOREVER); + if (!send_pkt) { + printk("cannot create pkt\n"); 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) { printk("cannot write buf\n"); 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) { printk("Cannot send IPv4 data to peer (%d)\n", rc); - net_pkt_unref(send_buf); + net_pkt_unref(send_pkt); return -EIO; } else { return len; @@ -97,13 +97,13 @@ int tcp_rx(void *context, unsigned char *buf, size_t size) return -EIO; } - offset = net_buf_frags_len(ctx->rx_pkt) - read_bytes; - rc = net_pkt_linear_copy(data, ctx->rx_pkt, offset, read_bytes); - ptr = net_pkt_appdata(data); + offset = net_pkt_get_len(ctx->rx_pkt) - read_bytes; + rc = net_frag_linear_copy(data, ctx->rx_pkt->frags, offset, read_bytes); + ptr = net_pkt_appdata(ctx->rx_pkt); memcpy(buf, ptr, read_bytes); net_pkt_unref(ctx->rx_pkt); - net_pkt_unref(data); + net_buf_unref(data); return read_bytes; } diff --git a/samples/net/mbedtls_sslclient/src/tcp.h b/samples/net/mbedtls_sslclient/src/tcp.h index c48b09fb5b6..0ff77e12a37 100644 --- a/samples/net/mbedtls_sslclient/src/tcp.h +++ b/samples/net/mbedtls_sslclient/src/tcp.h @@ -9,12 +9,12 @@ #include #include -#include +#include struct tcp_context { struct net_context *net_ctx; struct sockaddr local_sock; - struct net_buf *rx_pkt; + struct net_pkt *rx_pkt; int32_t timeout; }; diff --git a/samples/net/nats/src/nats.c b/samples/net/nats/src/nats.c index 84c1fa5c44a..dbff8f246a8 100644 --- a/samples/net/nats/src/nats.c +++ b/samples/net/nats/src/nats.c @@ -98,23 +98,23 @@ static bool is_sid_valid(const char *sid, size_t len) static int transmitv(struct net_context *conn, int iovcnt, struct io_vec *iov) { - struct net_buf *buf; + struct net_pkt *pkt; int i; - buf = net_pkt_get_tx(conn, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_tx(conn, K_FOREVER); + if (!pkt) { return -ENOMEM; } for (i = 0; i < iovcnt; i++) { - if (!net_pkt_append(buf, iov[i].len, iov[i].base, K_FOREVER)) { - net_pkt_unref(buf); + if (!net_pkt_append(pkt, iov[i].len, iov[i].base, K_FOREVER)) { + net_pkt_unref(pkt); 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[], @@ -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) { 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; uint8_t *end_of_line; - if (!buf) { + if (!pkt) { /* FIXME: How to handle disconnection? */ return; } if (status) { /* FIXME: How to handle connectio error? */ - net_buf_unref(buf); + net_pkt_unref(pkt); return; } - tmp = buf->frags; - pos = net_pkt_appdata(buf) - tmp->data; + tmp = pkt->frags; + pos = net_pkt_appdata(pkt) - tmp->data; while (tmp) { len = tmp->len - pos; @@ -567,14 +567,14 @@ static void receive_cb(struct net_context *ctx, struct net_buf *buf, int status, 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; if (end_of_line) { int ret; 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'; @@ -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) diff --git a/samples/net/wpan_serial/src/main.c b/samples/net/wpan_serial/src/main.c index 522e51cb4e7..fdfab17c01d 100644 --- a/samples/net/wpan_serial/src/main.c +++ b/samples/net/wpan_serial/src/main.c @@ -63,7 +63,7 @@ static struct device *uart_dev; /* SLIP state machine */ static uint8_t slip_state = STATE_OK; -static struct net_buf *pkt_curr; +static struct net_pkt *pkt_curr; /* General helpers */ @@ -154,9 +154,9 @@ static int slip_process_byte(unsigned char c) net_pkt_unref(pkt_curr); return 0; } - net_buf_frag_insert(pkt_curr, buf); + net_pkt_frag_insert(pkt_curr, buf); } else { - buf = net_buf_frag_last(pkt_curr); + buf = net_buf_frag_last(pkt_curr->frags); } if (!net_buf_tailroom(buf)) { @@ -207,7 +207,7 @@ static void interrupt_handler(struct device *dev) 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; } } @@ -218,11 +218,12 @@ static void interrupt_handler(struct device *dev) /* Allocate and send data to USB Host */ 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); if (!pkt) { - SYS_LOG_DBG("No buf available"); + SYS_LOG_DBG("No pkt available"); return; } @@ -233,7 +234,7 @@ static void send_data(uint8_t *cfg, uint8_t *data, size_t len) 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); @@ -247,7 +248,7 @@ static void send_data(uint8_t *cfg, uint8_t *data, size_t len) /* simulate FCS */ net_buf_add(buf, 2); - net_buf_put(&tx_queue, pkt); + k_fifo_put(&tx_queue, pkt); } 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)); } -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; int ret, i; @@ -333,9 +334,9 @@ static void set_channel(uint8_t 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); SYS_LOG_DBG("Process config %c", cmd); @@ -357,11 +358,12 @@ static void rx_thread(void) SYS_LOG_INF("RX thread started"); while (1) { - struct net_buf *pkt, *buf; + struct net_pkt *pkt; + struct net_buf *buf; uint8_t specifier; - pkt = net_buf_get(&rx_queue, K_FOREVER); - buf = net_buf_frag_last(pkt); + pkt = k_fifo_get(&rx_queue, K_FOREVER); + buf = net_buf_frag_last(pkt->frags); SYS_LOG_DBG("Got pkt %p buf %p", pkt, buf); @@ -431,14 +433,15 @@ static void tx_thread(void) k_sem_give(&tx_sem); while (1) { - struct net_buf *pkt, *buf; + struct net_pkt *pkt; + struct net_buf *buf; size_t len; k_sem_take(&tx_sem, K_FOREVER); - pkt = net_buf_get(&tx_queue, K_FOREVER); - buf = net_buf_frag_last(pkt); - len = net_buf_frags_len(pkt); + pkt = k_fifo_get(&tx_queue, K_FOREVER); + buf = net_buf_frag_last(pkt->frags); + len = net_pkt_get_len(pkt); 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(""); } -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", - pkt, pkt->len, net_buf_frags_len(pkt)); + SYS_LOG_DBG("Got data, pkt %p, frags->len %d", + pkt, net_pkt_get_len(pkt)); - net_buf_put(&tx_queue, pkt); + k_fifo_put(&tx_queue, pkt); return 0; } extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface, - struct net_buf *buf) + struct net_pkt *pkt) { SYS_LOG_DBG(""); @@ -635,7 +638,7 @@ extern enum net_verdict ieee802154_radio_handle_ack(struct net_if *iface, 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(""); diff --git a/samples/net/wpanusb/src/wpanusb.c b/samples/net/wpanusb/src/wpanusb.c index 20340532b2b..d258a1bab36 100644 --- a/samples/net/wpanusb/src/wpanusb.c +++ b/samples/net/wpanusb/src/wpanusb.c @@ -70,7 +70,7 @@ static struct device *wpanusb_dev; static struct ieee802154_radio_api *radio_api; static struct device *ieee802154_dev; -static struct nano_fifo tx_queue; +static struct k_fifo tx_queue; /** * Stack for the tx thread. @@ -349,9 +349,9 @@ static int stop(void) 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); int retries = 3; int ret; @@ -380,7 +380,8 @@ static int tx(struct net_buf *pkt) static int wpanusb_vendor_handler(struct usb_setup_packet *setup, 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); if (!pkt) { @@ -393,7 +394,7 @@ static int wpanusb_vendor_handler(struct usb_setup_packet *setup, return -ENOMEM; } - net_buf_frag_insert(pkt, buf); + net_pkt_frag_insert(pkt, buf); 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); - net_buf_put(&tx_queue, pkt); + k_fifo_put(&tx_queue, pkt); return 0; } @@ -417,10 +418,11 @@ static void tx_thread(void) while (1) { uint8_t cmd; - struct net_buf *pkt, *buf; + struct net_pkt *pkt; + struct net_buf *buf; - pkt = net_buf_get(&tx_queue, K_FOREVER); - buf = net_buf_frag_last(pkt); + pkt = k_fifo_get(&tx_queue, K_FOREVER); + buf = net_buf_frag_last(pkt->frags); cmd = net_buf_pull_u8(buf); 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, - struct net_buf *buf) + struct net_pkt *pkt) { /* parse on higher layer */ 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(""); @@ -551,25 +553,25 @@ void ieee802154_init(struct net_if *iface) 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; - SYS_LOG_DBG("Got data, buf %p, len %d frags->len %d", - buf, buf->len, net_buf_frags_len(buf)); + SYS_LOG_DBG("Got data, pkt %p, frags->len %d", + 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 */ - 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; } diff --git a/samples/net/zoap_client/src/zoap-client.c b/samples/net/zoap_client/src/zoap-client.c index 4521bc60c3c..47ff3c554b7 100644 --- a/samples/net/zoap_client/src/zoap-client.c +++ b/samples/net/zoap_client/src/zoap-client.c @@ -60,15 +60,15 @@ static int resource_reply_cb(const struct zoap_packet *response, struct zoap_reply *reply, 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; } static void udp_receive(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -82,10 +82,10 @@ static void udp_receive(struct net_context *context, * zoap expects that buffer->data starts at the * beginning of the CoAP header */ - header_len = net_pkt_appdata(buf) - buf->frags->data; - net_buf_pull(buf->frags, header_len); + header_len = net_pkt_appdata(pkt) - pkt->frags->data; + net_buf_pull(pkt->frags, header_len); - r = zoap_packet_parse(&response, buf); + r = zoap_packet_parse(&response, pkt); if (r < 0) { printk("Invalid data received (%d)\n", r); return; @@ -97,8 +97,8 @@ static void udp_receive(struct net_context *context, /* If necessary cancel retransmissions */ } - net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(buf)->src); - from.sin6_port = NET_UDP_BUF(buf)->src_port; + net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(pkt)->src); + from.sin6_port = NET_UDP_BUF(pkt)->src_port; reply = zoap_response_received(&response, (const struct sockaddr *) &from, @@ -119,7 +119,7 @@ static void retransmit_request(struct k_work *work) 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); if (r < 0) { return; @@ -142,7 +142,8 @@ static void event_iface_up(struct net_mgmt_event_callback *cb, struct zoap_pending *pending; struct zoap_reply *reply; const char * const *p; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; int r; 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); - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { - printk("Unable to get TX buffer, not enough memory.\n"); + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { + printk("Unable to get TX packet, not enough memory.\n"); return; } @@ -179,9 +180,9 @@ static void event_iface_up(struct net_mgmt_event_callback *cb, 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) { return; } @@ -233,7 +234,7 @@ static void event_iface_up(struct net_mgmt_event_callback *cb, zoap_reply_init(reply, &request); 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), NULL, 0, NULL, NULL); if (r < 0) { diff --git a/samples/net/zoap_server/src/zoap-server.c b/samples/net/zoap_server/src/zoap-server.c index dee077b6616..7951f5fdb5c 100644 --- a/samples/net/zoap_server/src/zoap-server.c +++ b/samples/net/zoap_server/src/zoap-server.c @@ -72,7 +72,8 @@ static int test_del(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; uint8_t tkl, code, type; 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("*******\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); - 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) { return -EINVAL; } @@ -111,7 +112,7 @@ static int test_del(struct zoap_resource *resource, zoap_header_set_id(&response, id); 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); } @@ -119,7 +120,8 @@ static int test_put(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; uint8_t *payload, code, type, tkl; 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("*******\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); - 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) { return -EINVAL; } @@ -163,7 +165,7 @@ static int test_put(struct zoap_resource *resource, zoap_header_set_id(&response, id); 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); } @@ -176,7 +178,8 @@ static int test_post(struct zoap_resource *resource, "location3", NULL }; const char * const *p; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; uint8_t *payload, code, type, tkl; 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("*******\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); - 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) { return -EINVAL; } @@ -226,7 +229,7 @@ static int test_post(struct zoap_resource *resource, *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); } @@ -238,7 +241,8 @@ static int location_query_post(struct zoap_resource *resource, "second=2", NULL }; const char * const *p; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; uint8_t *payload, code, type, tkl; 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("*******\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); - 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) { return -EINVAL; } @@ -288,7 +292,7 @@ static int location_query_post(struct zoap_resource *resource, *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); } @@ -296,7 +300,8 @@ static int piggyback_get(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; const uint8_t *token; 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("*******\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); - 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) { return -EINVAL; } @@ -360,7 +365,7 @@ static int piggyback_get(struct zoap_resource *resource, 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); } @@ -369,7 +374,8 @@ static int query_get(struct zoap_resource *resource, const struct sockaddr *from) { struct zoap_option options[4]; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; uint8_t *payload, code, type, tkl; const uint8_t *token; @@ -408,12 +414,12 @@ static int query_get(struct zoap_resource *resource, 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); - 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) { return -EINVAL; } @@ -448,7 +454,7 @@ static int query_get(struct zoap_resource *resource, 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); } @@ -456,7 +462,8 @@ static int separate_get(struct zoap_resource *resource, struct zoap_packet *request, const struct sockaddr *from) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; struct zoap_pending *pending; uint8_t *payload, code, type, tkl; @@ -477,12 +484,12 @@ static int separate_get(struct zoap_resource *resource, 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); - 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) { return -EINVAL; } @@ -494,19 +501,19 @@ static int separate_get(struct zoap_resource *resource, zoap_header_set_id(&response, id); 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); if (r < 0) { return -EINVAL; } 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); - 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) { return -EINVAL; } @@ -564,7 +571,7 @@ done: 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); } @@ -573,7 +580,8 @@ static int large_get(struct zoap_resource *resource, const struct sockaddr *from) { static struct zoap_block_context ctx; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; const uint8_t *token; 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("*******\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); - 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) { return -EINVAL; } @@ -653,7 +661,7 @@ static int large_get(struct zoap_resource *resource, 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); } @@ -662,7 +670,8 @@ static int large_update_put(struct zoap_resource *resource, const struct sockaddr *from) { static struct zoap_block_context ctx; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; const uint8_t *token; uint8_t *payload, code, type; @@ -703,12 +712,12 @@ static int large_update_put(struct zoap_resource *resource, /* 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); - 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) { return -EINVAL; } @@ -732,7 +741,7 @@ static int large_update_put(struct zoap_resource *resource, 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); } @@ -741,7 +750,8 @@ static int large_create_post(struct zoap_resource *resource, const struct sockaddr *from) { static struct zoap_block_context ctx; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_packet response; const uint8_t *token; 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("*******\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); - 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) { return -EINVAL; } @@ -802,7 +812,7 @@ static int large_create_post(struct zoap_resource *resource, 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); } @@ -824,17 +834,18 @@ static int send_notification_packet(const struct sockaddr *addr, uint16_t age, { struct zoap_packet response; struct zoap_pending *pending; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint8_t *payload, type = ZOAP_TYPE_CON; uint16_t len; 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); - 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) { 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); } - 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, @@ -957,7 +968,8 @@ static int core_get(struct zoap_resource *resource, const struct sockaddr *from) { 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; uint8_t *payload, tkl; const uint8_t *token; @@ -967,12 +979,12 @@ static int core_get(struct zoap_resource *resource, id = zoap_header_get_id(request); 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); - 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) { return -EINVAL; } @@ -996,7 +1008,7 @@ static int core_get(struct zoap_resource *resource, 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); } @@ -1096,7 +1108,7 @@ static struct zoap_resource *find_resouce_by_observer( } static void udp_receive(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -1105,28 +1117,28 @@ static void udp_receive(struct net_context *context, struct sockaddr_in6 from; int r, header_len; - net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(buf)->src); - from.sin6_port = NET_UDP_BUF(buf)->src_port; + net_ipaddr_copy(&from.sin6_addr, &NET_IPV6_BUF(pkt)->src); + from.sin6_port = NET_UDP_BUF(pkt)->src_port; from.sin6_family = AF_INET6; /* * zoap expects that buffer->data starts at the * beginning of the CoAP header */ - header_len = net_pkt_appdata(buf) - buf->frags->data; - net_buf_pull(buf->frags, header_len); + header_len = net_pkt_appdata(pkt) - pkt->frags->data; + net_buf_pull(pkt->frags, header_len); - r = zoap_packet_parse(&request, buf); + r = zoap_packet_parse(&request, pkt); if (r < 0) { NET_ERR("Invalid data received (%d)\n", r); - net_pkt_unref(buf); + net_pkt_unref(pkt); return; } pending = zoap_pending_received(&request, pendings, NUM_PENDINGS); if (pending) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return; } @@ -1152,7 +1164,7 @@ not_found: r = zoap_handle_request(&request, resources, (const struct sockaddr *) &from); - net_pkt_unref(buf); + net_pkt_unref(pkt); if (r < 0) { NET_ERR("No handler for such request (%d)\n", r); @@ -1208,7 +1220,7 @@ static void retransmit_request(struct k_work *work) return; } - r = net_context_sendto(pending->buf, &pending->addr, + r = net_context_sendto(pending->pkt, &pending->addr, sizeof(struct sockaddr_in6), NULL, 0, NULL, NULL); if (r < 0) { diff --git a/samples/net/zperf/src/zperf_session.c b/samples/net/zperf/src/zperf_session.c index b7429ce01da..1d870b1fc60 100644 --- a/samples/net/zperf/src/zperf_session.c +++ b/samples/net/zperf/src/zperf_session.c @@ -15,7 +15,7 @@ static struct session sessions[SESSION_PROTO_END][SESSION_MAX]; /* 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 *free = NULL; @@ -24,8 +24,8 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto) int i = 0; uint16_t port; - if (!buf) { - printk("Error! null buf detected.\n"); + if (!pkt) { + printk("Error! null pkt detected.\n"); return NULL; } @@ -35,15 +35,15 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto) } /* 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) { - net_ipaddr_copy(&ipv6, &NET_IPV6_BUF(buf)->src); - } else if (net_pkt_family(buf) == AF_INET) { - net_ipaddr_copy(&ipv4, &NET_IPV4_BUF(buf)->src); + if (net_pkt_family(pkt) == AF_INET6) { + net_ipaddr_copy(&ipv6, &NET_IPV6_BUF(pkt)->src); + } else if (net_pkt_family(pkt) == AF_INET) { + net_ipaddr_copy(&ipv4, &NET_IPV4_BUF(pkt)->src); } else { printk("Error! unsupported protocol %d\n", - net_pkt_family(buf)); + net_pkt_family(pkt)); return NULL; } @@ -53,7 +53,7 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto) #if defined(CONFIG_NET_IPV4) 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)) { /* We found an active session */ active = ptr; @@ -61,7 +61,7 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto) #endif #if defined(CONFIG_NET_IPV6) 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)) { /* We found an active session */ active = ptr; @@ -82,12 +82,12 @@ struct session *get_session(struct net_buf *buf, enum session_proto proto) active->port = port; #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); } #endif #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); } #endif diff --git a/samples/net/zperf/src/zperf_session.h b/samples/net/zperf/src/zperf_session.h index 72133b5f94f..c1a0f6fc791 100644 --- a/samples/net/zperf/src/zperf_session.h +++ b/samples/net/zperf/src/zperf_session.h @@ -56,7 +56,7 @@ struct session { 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_reset_session_stats(struct session *session); diff --git a/samples/net/zperf/src/zperf_tcp_receiver.c b/samples/net/zperf/src/zperf_tcp_receiver.c index 63010d910e2..d22c6bc144b 100644 --- a/samples/net/zperf/src/zperf_tcp_receiver.c +++ b/samples/net/zperf/src/zperf_tcp_receiver.c @@ -38,20 +38,20 @@ static struct sockaddr_in *in4_addr_my; #endif static void tcp_received(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { struct session *session; uint32_t time; - if (!buf) { + if (!pkt) { return; } time = k_cycle_get_32(); - session = get_session(buf, SESSION_TCP); + session = get_session(pkt, SESSION_TCP); if (!session) { printk(TAG "ERROR! cannot get a session!\n"); return; @@ -68,11 +68,11 @@ static void tcp_received(struct net_context *context, case STATE_ONGOING: session->counter++; - if (buf) { - session->length += net_pkt_appdatalen(buf); + if (pkt) { + session->length += net_pkt_appdatalen(pkt); } - if (!buf && status == 0) { /* EOF */ + if (!pkt && status == 0) { /* EOF */ uint32_t rate_in_kbps; uint32_t duration = HW_CYCLES_TO_USEC( time_delta(session->start_time, time)); @@ -107,7 +107,7 @@ static void tcp_received(struct net_context *context, printk(TAG "Error! Unsupported case\n"); } - net_pkt_unref(buf); + net_pkt_unref(pkt); } static void tcp_accepted(struct net_context *context, diff --git a/samples/net/zperf/src/zperf_tcp_uploader.c b/samples/net/zperf/src/zperf_tcp_uploader.c index dfb7416eea1..56d544e2f28 100644 --- a/samples/net/zperf/src/zperf_tcp_uploader.c +++ b/samples/net/zperf/src/zperf_tcp_uploader.c @@ -46,7 +46,8 @@ void zperf_tcp_upload(struct net_context *ctx, do { int ret = 0; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint32_t loop_time; bool st; @@ -54,39 +55,39 @@ void zperf_tcp_upload(struct net_context *ctx, loop_time = k_cycle_get_32(); last_loop_time = loop_time; - buf = net_pkt_get_tx(ctx, K_FOREVER); - if (!buf) { - printk(TAG "ERROR! Failed to retrieve a buffer\n"); + pkt = net_pkt_get_tx(ctx, K_FOREVER); + if (!pkt) { + printk(TAG "ERROR! Failed to retrieve a packet\n"); break; } frag = net_pkt_get_data(ctx, K_FOREVER); if (!frag) { - net_pkt_unref(buf); + net_pkt_unref(pkt); printk(TAG "ERROR! Failed to retrieve a fragment\n"); break; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* 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); if (!st) { printk(TAG "ERROR! Failed to fill packet\n"); - net_pkt_unref(buf); + net_pkt_unref(pkt); nb_errors++; break; } /* 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) { - printk(TAG "ERROR! Failed to send the buffer (%d)\n", + printk(TAG "ERROR! Failed to send the packet (%d)\n", ret); - net_pkt_unref(buf); + net_pkt_unref(pkt); nb_errors++; break; } else { diff --git a/samples/net/zperf/src/zperf_udp_receiver.c b/samples/net/zperf/src/zperf_udp_receiver.c index 8b28b8405ef..6bc98581f9c 100644 --- a/samples/net/zperf/src/zperf_udp_receiver.c +++ b/samples/net/zperf/src/zperf_udp_receiver.c @@ -36,95 +36,96 @@ static struct sockaddr_in *in4_addr_my; #define MAX_DBG_PRINT 64 static inline void set_dst_addr(sa_family_t family, - struct net_buf *buf, + struct net_pkt *pkt, struct sockaddr *dst_addr) { #if defined(CONFIG_NET_IPV6) if (family == AF_INET6) { 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_port = NET_UDP_BUF(buf)->src_port; + net_sin6(dst_addr)->sin6_port = NET_UDP_BUF(pkt)->src_port; } #endif /* CONFIG_NET_IPV6 */ #if defined(CONFIG_NET_IPV4) if (family == AF_INET) { 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_port = NET_UDP_BUF(buf)->src_port; + net_sin(dst_addr)->sin_port = NET_UDP_BUF(pkt)->src_port; } #endif /* CONFIG_NET_IPV4 */ } -static inline struct net_buf *build_reply_buf(struct net_context *context, - struct net_buf *buf, +static inline struct net_pkt *build_reply_pkt(struct net_context *context, + struct net_pkt *pkt, struct zperf_udp_datagram *hdr, 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); - 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_buf, hdr->tv_sec); - net_pkt_append_be32(reply_buf, hdr->tv_usec); + net_pkt_append_be32(reply_pkt, hdr->id); + net_pkt_append_be32(reply_pkt, hdr->tv_sec); + net_pkt_append_be32(reply_pkt, hdr->tv_usec); - net_pkt_append_be32(reply_buf, stat->flags); - net_pkt_append_be32(reply_buf, stat->total_len1); - net_pkt_append_be32(reply_buf, stat->total_len2); - net_pkt_append_be32(reply_buf, stat->stop_sec); - net_pkt_append_be32(reply_buf, stat->stop_usec); - net_pkt_append_be32(reply_buf, stat->error_cnt); - net_pkt_append_be32(reply_buf, stat->outorder_cnt); - net_pkt_append_be32(reply_buf, stat->datagrams); - net_pkt_append_be32(reply_buf, stat->jitter1); - net_pkt_append_be32(reply_buf, stat->jitter2); + net_pkt_append_be32(reply_pkt, stat->flags); + net_pkt_append_be32(reply_pkt, stat->total_len1); + net_pkt_append_be32(reply_pkt, stat->total_len2); + net_pkt_append_be32(reply_pkt, stat->stop_sec); + net_pkt_append_be32(reply_pkt, stat->stop_usec); + net_pkt_append_be32(reply_pkt, stat->error_cnt); + net_pkt_append_be32(reply_pkt, stat->outorder_cnt); + net_pkt_append_be32(reply_pkt, stat->datagrams); + net_pkt_append_be32(reply_pkt, stat->jitter1); + net_pkt_append_be32(reply_pkt, stat->jitter2); - return reply_buf; + return reply_pkt; } /* Send statistics to the remote client */ 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_server_hdr *stat) { - struct net_buf *reply_buf; + struct net_pkt *reply_pkt; struct sockaddr dst_addr; 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, - net_pkt_family(buf) == AF_INET6 ? + ret = net_context_sendto(reply_pkt, &dst_addr, + net_pkt_family(pkt) == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in), NULL, 0, NULL, NULL); if (ret < 0) { printk(TAG " Cannot send data to peer (%d)", ret); - net_pkt_unref(reply_buf); + net_pkt_unref(reply_pkt); } return ret; } static void udp_received(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { - struct net_buf *frag = buf->frags; + struct net_buf *frag = pkt->frags; struct zperf_udp_datagram hdr; struct session *session; uint16_t offset, pos; @@ -132,28 +133,28 @@ static void udp_received(struct net_context *context, uint32_t time; int32_t id; - if (!buf) { + if (!pkt) { return; } - if (net_pkt_appdatalen(buf) < sizeof(struct zperf_udp_datagram)) { - net_pkt_unref(buf); + if (net_pkt_appdatalen(pkt) < sizeof(struct zperf_udp_datagram)) { + net_pkt_unref(pkt); return; } time = k_cycle_get_32(); - session = get_session(buf, SESSION_UDP); + session = get_session(pkt, SESSION_UDP); if (!session) { printk(TAG "ERROR! cannot get a session!\n"); 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_pkt_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, offset, &pos, (uint32_t *)&hdr.id); + frag = net_frag_read_be32(frag, pos, &pos, &hdr.tv_sec); + frag = net_frag_read_be32(frag, pos, &pos, &hdr.tv_usec); id = hdr.id; @@ -162,15 +163,15 @@ static void udp_received(struct net_context *context, printk(TAG "End of session!\n"); if (session->state == STATE_COMPLETED) { - /* Session is already completed: Resend the stat buffer + /* Session is already completed: Resend the stat packet * and continue */ - if (zperf_receiver_send_stat(context, buf, &hdr, + if (zperf_receiver_send_stat(context, pkt, &hdr, &session->stat) < 0) { printk(TAG "ERROR! Failed to send the " - "buffer\n"); + "packet\n"); - net_pkt_unref(buf); + net_pkt_unref(pkt); } } else { session->state = STATE_LAST_PACKET_RECEIVED; @@ -200,7 +201,7 @@ static void udp_received(struct net_context *context, /* Update counter */ session->counter++; - session->length += net_pkt_appdatalen(buf); + session->length += net_pkt_appdatalen(pkt); /* Compute jitter */ 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.jitter2 = session->jitter; - if (zperf_receiver_send_stat(context, buf, &hdr, + if (zperf_receiver_send_stat(context, pkt, &hdr, &session->stat) < 0) { printk(TAG "ERROR! Failed to send the " - "buffer\n"); + "packet\n"); - net_pkt_unref(buf); + net_pkt_unref(pkt); } printk(TAG " duration:\t\t"); @@ -277,7 +278,7 @@ static void udp_received(struct net_context *context, printk("\n"); } } else { - net_pkt_unref(buf); + net_pkt_unref(pkt); } } diff --git a/samples/net/zperf/src/zperf_udp_uploader.c b/samples/net/zperf/src/zperf_udp_uploader.c index a0608e6f5a0..336a81689dc 100644 --- a/samples/net/zperf/src/zperf_udp_uploader.c +++ b/samples/net/zperf/src/zperf_udp_uploader.c @@ -19,40 +19,40 @@ 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 net_buf *frag = buf->frags; + struct net_buf *frag = pkt->frags; struct zperf_server_hdr hdr; uint16_t offset; 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) + sizeof(struct zperf_udp_datagram); /* Decode stat */ - if (!buf) { + if (!pkt) { printk(TAG "ERROR! Failed to receive statistic\n"); return; - } else if (net_pkt_appdatalen(buf) < + } else if (net_pkt_appdatalen(pkt) < (sizeof(struct zperf_server_hdr) + sizeof(struct zperf_udp_datagram))) { printk(TAG "ERROR! Statistics too small\n"); return; } - frag = net_pkt_read_be32(frag, offset, &pos, (uint32_t *)&hdr.flags); - frag = net_pkt_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_pkt_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_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.error_cnt); - frag = net_pkt_read_be32(frag, pos, &pos, + frag = net_frag_read_be32(frag, offset, &pos, (uint32_t *)&hdr.flags); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.total_len1); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.total_len2); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.stop_sec); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.stop_usec); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.error_cnt); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.outorder_cnt); - frag = net_pkt_read_be32(frag, pos, &pos, (uint32_t *)&hdr.datagrams); - frag = net_pkt_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.datagrams); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.jitter1); + frag = net_frag_read_be32(frag, pos, &pos, (uint32_t *)&hdr.jitter2); results->nb_packets_rcvd = hdr.datagrams; 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, - struct net_buf *buf, + struct net_pkt *pkt, int status, 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, @@ -78,18 +78,19 @@ static inline void zperf_upload_fin(struct net_context *context, uint32_t packet_size, struct zperf_results *results) { - struct net_buf *stat = NULL; + struct net_pkt *stat = NULL; struct zperf_udp_datagram datagram; int loop = 2; int ret; while (!stat && loop-- > 0) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; bool status; - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { - printk(TAG "ERROR! Failed to retrieve a buffer\n"); + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { + printk(TAG "ERROR! Failed to retrieve a packet\n"); continue; } @@ -99,7 +100,7 @@ static inline void zperf_upload_fin(struct net_context *context, continue; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* Fill the packet header */ 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) % USEC_PER_SEC); - status = net_pkt_append(buf, sizeof(datagram), - (uint8_t *)&datagram, K_FOREVER); + status = net_pkt_append(pkt, sizeof(datagram), + (uint8_t *)&datagram, K_FOREVER); if (!status) { printk(TAG "ERROR! Cannot append datagram data\n"); break; @@ -120,19 +121,19 @@ static inline void zperf_upload_fin(struct net_context *context, sizeof(struct zperf_udp_datagram); 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), - &pos, size, - (uint8_t *)sample_packet, - K_FOREVER); + &pos, size, + (uint8_t *)sample_packet, + K_FOREVER); } /* 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) { - printk(TAG "ERROR! Failed to send the buffer (%d)\n", + printk(TAG "ERROR! Failed to send the packet (%d)\n", ret); - net_pkt_unref(buf); + net_pkt_unref(pkt); continue; } @@ -205,7 +206,8 @@ void zperf_udp_upload(struct net_context *context, do { struct zperf_udp_datagram datagram; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint32_t loop_time; int32_t adjust; bool status; @@ -232,9 +234,9 @@ void zperf_udp_upload(struct net_context *context, last_loop_time = loop_time; - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { - printk(TAG "ERROR! Failed to retrieve a buffer\n"); + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { + printk(TAG "ERROR! Failed to retrieve a packet\n"); continue; } @@ -244,7 +246,7 @@ void zperf_udp_upload(struct net_context *context, continue; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* Fill the packet header */ datagram.id = htonl(nb_packets); @@ -252,8 +254,8 @@ void zperf_udp_upload(struct net_context *context, datagram.tv_usec = htonl(HW_CYCLES_TO_USEC(loop_time) % USEC_PER_SEC); - status = net_pkt_append(buf, sizeof(datagram), - (uint8_t *)&datagram, K_FOREVER); + status = net_pkt_append(pkt, sizeof(datagram), + (uint8_t *)&datagram, K_FOREVER); if (!status) { printk(TAG "ERROR! Cannot append datagram data\n"); break; @@ -265,19 +267,19 @@ void zperf_udp_upload(struct net_context *context, sizeof(struct zperf_udp_datagram); 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), - &pos, size, sample_packet, - K_FOREVER); + &pos, size, sample_packet, + K_FOREVER); } /* 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) { - printk(TAG "ERROR! Failed to send the buffer (%d)\n", + printk(TAG "ERROR! Failed to send the packet (%d)\n", ret); - net_pkt_unref(buf); + net_pkt_unref(pkt); break; } else { nb_packets++; diff --git a/subsys/net/ip/6lo.c b/subsys/net/ip/6lo.c index 118761adad3..70ef43f9ea2 100644 --- a/subsys/net/ip/6lo.c +++ b/subsys/net/ip/6lo.c @@ -285,7 +285,7 @@ static inline uint8_t compress_nh(struct net_ipv6_hdr *ipv6, /* Helpers to compress Source Address */ static inline uint8_t compress_sa(struct net_ipv6_hdr *ipv6, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag, 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); offset += 2; } else { - if (!net_pkt_ll_src(buf)) { + if (!net_pkt_ll_src(pkt)) { NET_ERR("Invalid src ll address"); return 0; } 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"); /* 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) static inline uint8_t compress_sa_ctx(struct net_ipv6_hdr *ipv6, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag, uint8_t offset, struct net_6lo_context *src) { if (!src) { - return compress_sa(ipv6, buf, frag, offset); + return compress_sa(ipv6, pkt, frag, offset); } 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); offset += 2; } 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"); /* 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 */ static inline uint8_t compress_da_mcast(struct net_ipv6_hdr *ipv6, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag, 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, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag, uint8_t offset) { /* If destination address is multicast */ 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 */ @@ -466,13 +466,13 @@ static inline uint8_t compress_da(struct net_ipv6_hdr *ipv6, memcpy(&IPHC[offset], &ipv6->dst.s6_addr[14], 2); offset += 2; } else { - if (!net_pkt_ll_dst(buf)) { + if (!net_pkt_ll_dst(pkt)) { NET_ERR("Invalid dst ll address"); return 0; } 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"); /* 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) static inline uint8_t compress_da_ctx(struct net_ipv6_hdr *ipv6, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag, uint8_t offset, struct net_6lo_context *dst) { if (!dst) { - return compress_da(ipv6, buf, frag, offset); + return compress_da(ipv6, pkt, frag, offset); } 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; } else { 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"); /* 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) 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_6lo_context **src, struct net_6lo_context **dst) { /* 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)) { *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)) { *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 | * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ */ -static inline bool compress_IPHC_header(struct net_buf *buf, +static inline bool compress_IPHC_header(struct net_pkt *pkt, fragment_handler_t fragment) { #if defined(CONFIG_NET_6LO_CONTEXT) struct net_6lo_context *src = NULL; struct net_6lo_context *dst = NULL; #endif - struct net_ipv6_hdr *ipv6 = NET_IPV6_BUF(buf); + struct net_ipv6_hdr *ipv6 = NET_IPV6_BUF(pkt); uint8_t offset = 0; struct net_udp_hdr *udp; struct net_buf *frag; uint8_t compressed; - if (buf->frags->len < NET_IPV6H_LEN) { + if (pkt->frags->len < NET_IPV6H_LEN) { NET_ERR("Invalid length %d, min %d", - buf->frags->len, NET_IPV6H_LEN); + pkt->frags->len, NET_IPV6H_LEN); return false; } if (ipv6->nexthdr == IPPROTO_UDP && - buf->frags->len < NET_IPV6UDPH_LEN) { + pkt->frags->len < NET_IPV6UDPH_LEN) { NET_ERR("Invalid length %d, min %d", - buf->frags->len, NET_IPV6UDPH_LEN); + pkt->frags->len, NET_IPV6UDPH_LEN); 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++] = 0; #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++; } #endif @@ -723,24 +723,24 @@ static inline bool compress_IPHC_header(struct net_buf *buf, /* Source Address Compression */ #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 - offset = compress_sa(ipv6, buf, frag, offset); + offset = compress_sa(ipv6, pkt, frag, offset); #endif if (!offset) { - net_pkt_unref(frag); + net_pkt_frag_unref(frag); return false; } /* Destination Address Compression */ #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 - offset = compress_da(ipv6, buf, frag, offset); + offset = compress_da(ipv6, pkt, frag, offset); #endif if (!offset) { - net_pkt_unref(frag); + net_pkt_frag_unref(frag); return false; } @@ -752,7 +752,7 @@ static inline bool compress_IPHC_header(struct net_buf *buf, } /* UDP header compression */ - udp = NET_UDP_BUF(buf); + udp = NET_UDP_BUF(pkt); IPHC[offset] = NET_6LO_NHC_UDP_BARE; offset = compress_nh_udp(udp, frag, offset); @@ -762,28 +762,28 @@ end: net_buf_add(frag, offset); /* Copy the rest of the data to compressed fragment */ - memcpy(&IPHC[offset], buf->frags->data + compressed, - buf->frags->len - compressed); - net_buf_add(frag, buf->frags->len - compressed); + memcpy(&IPHC[offset], pkt->frags->data + compressed, + pkt->frags->len - compressed); + net_buf_add(frag, pkt->frags->len - compressed); /* Delete uncompressed(original) header fragment */ - net_pkt_frag_del(buf, buf->frags); + net_pkt_frag_del(pkt, NULL, pkt->frags); /* 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 */ - net_pkt_compact(buf); + net_pkt_compact(pkt); if (fragment) { - return fragment(buf, compressed - offset); + return fragment(pkt, compressed - offset); } return true; } /* 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, uint8_t offset) { @@ -835,7 +835,7 @@ static inline uint8_t uncompress_tfl(struct net_buf *buf, } /* 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, uint8_t offset) { @@ -858,7 +858,7 @@ static inline uint8_t uncompress_hoplimit(struct net_buf *buf, } /* 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, uint8_t offset) { @@ -900,7 +900,7 @@ static inline uint8_t uncompress_sa(struct net_buf *buf, case NET_6LO_IPHC_SAM_11: 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; } @@ -908,13 +908,13 @@ static inline uint8_t uncompress_sa(struct net_buf *buf, } #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, uint8_t offset, struct net_6lo_context *ctx) { if (!ctx) { - return uncompress_sa(buf, ipv6, offset); + return uncompress_sa(pkt, ipv6, offset); } 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. * (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 * as link local prefix. @@ -965,7 +965,7 @@ static inline uint8_t uncompress_sa_ctx(struct net_buf *buf, #endif /* 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, uint8_t offset) { @@ -1023,12 +1023,12 @@ static inline uint8_t uncompress_da_mcast(struct net_buf *buf, } /* 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, uint8_t offset) { 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) { @@ -1068,7 +1068,7 @@ static inline uint8_t uncompress_da(struct net_buf *buf, case NET_6LO_IPHC_DAM_11: 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; } @@ -1076,17 +1076,17 @@ static inline uint8_t uncompress_da(struct net_buf *buf, } #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, uint8_t offset, struct net_6lo_context *ctx) { if (!ctx) { - return uncompress_da(buf, ipv6, offset); + return uncompress_da(pkt, ipv6, offset); } 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)) { @@ -1130,7 +1130,7 @@ static inline uint8_t uncompress_da_ctx(struct net_buf *buf, * the encapsulating header. * (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 * as link local prefix. @@ -1145,7 +1145,7 @@ static inline uint8_t uncompress_da_ctx(struct net_buf *buf, #endif /* 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, uint8_t offset) { @@ -1204,7 +1204,7 @@ static inline uint8_t uncompress_nh_udp(struct net_buf *buf, #if defined(CONFIG_NET_6LO_CONTEXT) /* 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 **dst) { @@ -1214,13 +1214,13 @@ static inline bool uncompress_cid(struct net_buf *buf, * Either src or dest address is context based or both. */ 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)) { NET_DBG("Unknown src cid %d", cid); } 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)) { NET_DBG("Unknown dst cid %d", cid); } @@ -1238,7 +1238,7 @@ static inline bool uncompress_cid(struct net_buf *buf, } #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; 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 defined(CONFIG_NET_6LO_CONTEXT) - if (!uncompress_cid(buf, &src, &dst)) { + if (!uncompress_cid(pkt, &src, &dst)) { return false; } @@ -1264,7 +1264,7 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf) #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) { return false; } @@ -1273,10 +1273,10 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf) /* Version is always 6 */ 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 */ - offset = uncompress_tfl(buf, ipv6, offset); + offset = uncompress_tfl(pkt, ipv6, offset); if (!(CIPHC[0] & NET_6LO_IPHC_NH_1)) { ipv6->nexthdr = CIPHC[offset]; @@ -1284,7 +1284,7 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf) } /* Uncompress Hoplimit */ - offset = uncompress_hoplimit(buf, ipv6, offset); + offset = uncompress_hoplimit(pkt, ipv6, offset); /* First set to zero and copy relevant bits */ 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 */ #if defined(CONFIG_NET_6LO_CONTEXT) - offset = uncompress_sa_ctx(buf, ipv6, offset, src); + offset = uncompress_sa_ctx(pkt, ipv6, offset, src); if (!offset) { goto fail; } #else - offset = uncompress_sa(buf, ipv6, offset); + offset = uncompress_sa(pkt, ipv6, offset); #endif /* Uncompress Destination Address */ #if defined(CONFIG_NET_6LO_CONTEXT) - offset = uncompress_da_ctx(buf, ipv6, offset, dst); + offset = uncompress_da_ctx(pkt, ipv6, offset, dst); if (!offset) { goto fail; } #else - offset = uncompress_da(buf, ipv6, offset); + offset = uncompress_da(pkt, ipv6, offset); if (!offset) { 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); 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) { memcpy(&udp->chksum, &CIPHC[offset], 2); @@ -1345,22 +1345,22 @@ static inline bool uncompress_IPHC_header(struct net_buf *buf) end: /* Move the data to beginning, no need for headers now */ NET_DBG("Removing %u bytes of compressed hdr", offset); - memmove(buf->frags->data, buf->frags->data + offset, - buf->frags->len - offset); - buf->frags->len -= offset; + memmove(pkt->frags->data, pkt->frags->data + offset, + pkt->frags->len - offset); + pkt->frags->len -= offset; /* Copying ll part, if any */ - if (net_pkt_ll_reserve(buf)) { - memcpy(frag->data - net_pkt_ll_reserve(buf), - net_pkt_ll(buf), net_pkt_ll_reserve(buf)); + if (net_pkt_ll_reserve(pkt)) { + memcpy(frag->data - net_pkt_ll_reserve(pkt), + net_pkt_ll(pkt), net_pkt_ll_reserve(pkt)); } /* Insert the fragment (this one holds uncompressed headers) */ - net_buf_frag_insert(buf, frag); - net_pkt_compact(buf); + net_pkt_frag_insert(pkt, frag); + net_pkt_compact(pkt); /* 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[1] = (uint8_t)len; @@ -1368,43 +1368,43 @@ end: udp->len = htons(len); if (chksum) { - udp->chksum = ~net_calc_chksum_udp(buf); + udp->chksum = ~net_calc_chksum_udp(pkt); } } return true; fail: - net_pkt_unref(frag); + net_pkt_frag_unref(frag); return false; } /* 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) { 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; 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 */ - net_pkt_compact(buf); + net_pkt_compact(pkt); if (fragment) { - return fragment(buf, -1); + return fragment(pkt, -1); } 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 */ 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; } -bool net_6lo_compress(struct net_buf *buf, bool iphc, +bool net_6lo_compress(struct net_pkt *pkt, bool iphc, fragment_handler_t fragment) { if (iphc) { - return compress_IPHC_header(buf, fragment); + return compress_IPHC_header(pkt, fragment); } 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) { /* 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) { /* Uncompress IPv6 header, it has only IPv6 dispatch in the * 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; } diff --git a/subsys/net/ip/6lo.h b/subsys/net/ip/6lo.h index a35cf487902..9a99080fcd6 100644 --- a/subsys/net/ip/6lo.h +++ b/subsys/net/ip/6lo.h @@ -19,7 +19,7 @@ #include #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 @@ -28,13 +28,13 @@ typedef bool (*fragment_handler_t)(struct net_buf *, int); * are compressed as per RFC 6282. After header compression data * 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 Pointer to fragment function * * @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); /** @@ -44,11 +44,11 @@ bool net_6lo_compress(struct net_buf *buf, bool iphc, * are uncompressed as per RFC 6282. After header uncompression data * 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 */ -bool net_6lo_uncompress(struct net_buf *buf); +bool net_6lo_uncompress(struct net_pkt *pkt); /** * @brief Set 6lowpan context information diff --git a/subsys/net/ip/6lo_private.h b/subsys/net/ip/6lo_private.h index 7d6278d77ef..ebc77ff9141 100644 --- a/subsys/net/ip/6lo_private.h +++ b/subsys/net/ip/6lo_private.h @@ -85,7 +85,7 @@ #define NET_6LO_NHC_UDP_4_BIT_PORT 0xF0B #define IPHC ((frag)->data) -#define CIPHC ((buf->frags)->data) +#define CIPHC ((pkt->frags)->data) #define NET_6LO_FRAG1_HDR_LEN 4 #define NET_6LO_FRAGN_HDR_LEN 5 diff --git a/subsys/net/ip/connection.c b/subsys/net/ip/connection.c index e6629cb3e2b..fd6816d17a6 100644 --- a/subsys/net/ip/connection.c +++ b/subsys/net/ip/connection.c @@ -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 * 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) @@ -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, sa_family_t family, - struct net_buf *buf, + struct net_pkt *pkt, uint32_t *cache_value) { #if defined(CONFIG_NET_IPV4) if (family == AF_INET) { return check_hash(proto, family, - &NET_IPV4_BUF(buf)->src, - &NET_IPV4_BUF(buf)->dst, - NET_UDP_BUF(buf)->src_port, - NET_UDP_BUF(buf)->dst_port, + &NET_IPV4_BUF(pkt)->src, + &NET_IPV4_BUF(pkt)->dst, + NET_UDP_BUF(pkt)->src_port, + NET_UDP_BUF(pkt)->dst_port, cache_value); } #endif @@ -227,10 +227,10 @@ static inline int32_t get_conn(enum net_ip_protocol proto, #if defined(CONFIG_NET_IPV6) if (family == AF_INET6) { return check_hash(proto, family, - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst, - NET_UDP_BUF(buf)->src_port, - NET_UDP_BUF(buf)->dst_port, + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst, + NET_UDP_BUF(pkt)->src_port, + NET_UDP_BUF(pkt)->dst_port, cache_value); } #endif @@ -280,11 +280,11 @@ static void cache_clear(void) } static inline enum net_verdict cache_check(enum net_ip_protocol proto, - struct net_buf *buf, + struct net_pkt *pkt, uint32_t *cache_value, 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 (conn_cache[*pos].idx >= 0) { /* 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]; - 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", - net_proto2str(proto), buf, - ntohs(NET_CONN_BUF(buf)->src_port), - ntohs(NET_CONN_BUF(buf)->dst_port), - net_pkt_family(buf), *pos, + net_proto2str(proto), pkt, + ntohs(NET_CONN_BUF(pkt)->src_port), + ntohs(NET_CONN_BUF(pkt)->dst_port), + net_pkt_family(pkt), *pos, 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) { if (cache_check_neg(*cache_value)) { @@ -557,22 +557,22 @@ int net_conn_register(enum net_ip_protocol proto, return -ENOENT; } -static bool check_addr(struct net_buf *buf, +static bool check_addr(struct net_pkt *pkt, struct sockaddr *addr, bool is_remote) { - if (addr->family != net_pkt_family(buf)) { + if (addr->family != net_pkt_family(pkt)) { return false; } #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; if (is_remote) { - addr6 = &NET_IPV6_BUF(buf)->src; + addr6 = &NET_IPV6_BUF(pkt)->src; } else { - addr6 = &NET_IPV6_BUF(buf)->dst; + addr6 = &NET_IPV6_BUF(pkt)->dst; } if (!net_is_ipv6_addr_unspecified( @@ -588,13 +588,13 @@ static bool check_addr(struct net_buf *buf, #endif /* CONFIG_NET_IPV6 */ #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; if (is_remote) { - addr4 = &NET_IPV4_BUF(buf)->src; + addr4 = &NET_IPV4_BUF(pkt)->src; } else { - addr4 = &NET_IPV4_BUF(buf)->dst; + addr4 = &NET_IPV4_BUF(pkt)->dst; } if (net_sin(addr)->sin_addr.s_addr[0]) { @@ -609,24 +609,24 @@ static bool check_addr(struct net_buf *buf, 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) - 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); #endif /* CONFIG_NET_IPV6 */ } else { #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); #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; 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; int32_t pos; - verdict = cache_check(proto, buf, &cache_value, &pos); + verdict = cache_check(proto, pkt, &cache_value, &pos); if (verdict != NET_CONTINUE) { return verdict; } @@ -646,16 +646,16 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf) uint16_t chksum; if (proto == IPPROTO_TCP) { - chksum = NET_TCP_BUF(buf)->chksum; + chksum = NET_TCP_BUF(pkt)->chksum; } 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 " - "family %d chksum 0x%04x", net_proto2str(proto), buf, - ntohs(NET_CONN_BUF(buf)->src_port), - ntohs(NET_CONN_BUF(buf)->dst_port), - net_pkt_family(buf), ntohs(chksum)); + NET_DBG("Check %s listener for pkt %p src port %u dst port %u " + "family %d chksum 0x%04x", net_proto2str(proto), pkt, + ntohs(NET_CONN_BUF(pkt)->src_port), + ntohs(NET_CONN_BUF(pkt)->dst_port), + net_pkt_family(pkt), ntohs(chksum)); } 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 != - NET_CONN_BUF(buf)->src_port) { + NET_CONN_BUF(pkt)->src_port) { continue; } } 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; } } 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; } } 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; } } @@ -728,7 +728,7 @@ enum net_verdict net_conn_input(enum net_ip_protocol proto, struct net_buf *buf) conns[best_match].rank); #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) { 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, * we do not send ICMP error as that makes no sense. */ - if (net_pkt_family(buf) == AF_INET6 && - net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { + if (net_pkt_family(pkt) == AF_INET6 && + net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) { ; } else #endif #if defined(CONFIG_NET_IPV4) - if (net_pkt_family(buf) == AF_INET && - net_is_ipv4_addr_mcast(&NET_IPV4_BUF(buf)->dst)) { + if (net_pkt_family(pkt) == AF_INET && + net_is_ipv4_addr_mcast(&NET_IPV4_BUF(pkt)->dst)) { ; } else #endif { - send_icmp_error(buf); + send_icmp_error(pkt); } drop: diff --git a/subsys/net/ip/connection.h b/subsys/net/ip/connection.h index e7499cd2209..75072498cc6 100644 --- a/subsys/net/ip/connection.h +++ b/subsys/net/ip/connection.h @@ -35,7 +35,7 @@ struct net_conn_handle; * and port. */ typedef enum net_verdict (*net_conn_cb_t)(struct net_conn *conn, - struct net_buf *buf, + struct net_pkt *pkt, 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. * - * @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 * 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) enum net_verdict net_conn_input(enum net_ip_protocol proto, - struct net_buf *buf); + struct net_pkt *pkt); #else static inline enum net_verdict net_conn_input(enum net_ip_protocol proto, - struct net_buf *buf) + struct net_pkt *pkt) { return NET_DROP; } diff --git a/subsys/net/ip/dhcpv4.c b/subsys/net/ip/dhcpv4.c index 0c82ae7830c..1d2eef9ab81 100644 --- a/subsys/net/ip/dhcpv4.c +++ b/subsys/net/ip/dhcpv4.c @@ -155,25 +155,25 @@ net_dhcpv4_msg_type_name(enum dhcpv4_msg_type msg_type) } /* 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, - K_FOREVER); + return net_pkt_append(pkt, sizeof(magic_cookie), + magic_cookie, K_FOREVER); } /* 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) { - if (!net_pkt_append_u8(buf, option)) { + if (!net_pkt_append_u8(pkt, option)) { return false; } - if (!net_pkt_append_u8(buf, size)) { + if (!net_pkt_append_u8(pkt, size)) { return false; } - if (!net_pkt_append(buf, size, value, K_FOREVER)) { + if (!net_pkt_append(pkt, size, value, K_FOREVER)) { return false; } @@ -181,15 +181,15 @@ static bool add_option_length_value(struct net_buf *buf, uint8_t option, } /* 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. * 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, 3, /* Length */ @@ -197,34 +197,34 @@ static bool add_req_options(struct net_buf *buf) DHCPV4_OPTIONS_ROUTER, 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); } -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); } /* 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 */ -static inline bool add_file(struct net_buf *buf) +static inline bool add_file(struct net_pkt *pkt) { uint8_t len = SIZE_OF_FILE; while (len-- > 0) { - if (!net_pkt_append_u8(buf, 0)) { + if (!net_pkt_append_u8(pkt, 0)) { return false; } } @@ -233,12 +233,12 @@ static inline bool add_file(struct net_buf *buf) } /* 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; while (len-- > 0) { - if (!net_pkt_append_u8(buf, 0)) { + if (!net_pkt_append_u8(pkt, 0)) { return false; } } @@ -247,16 +247,16 @@ static inline bool add_sname(struct net_buf *buf) } /* 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_udp_hdr *udp; uint16_t len; - ipv4 = NET_IPV4_BUF(buf); - udp = NET_UDP_BUF(buf); + ipv4 = NET_IPV4_BUF(pkt); + udp = NET_UDP_BUF(pkt); - len = net_buf_frags_len(buf->frags); + len = net_pkt_get_len(pkt); /* Setup IPv4 header */ 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->len[0] = len >> 8; 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); @@ -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->len = htons(len); 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 */ -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) { - struct net_buf *buf; + struct net_pkt *pkt; struct net_buf *frag; 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); - 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_family(buf, AF_INET); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET); + 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 */ - 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)) { 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)); - if (!add_sname(buf) || - !add_file(buf) || - !add_cookie(buf) || - !add_msg_type(buf, type)) { + if (!add_sname(pkt) || + !add_file(pkt) || + !add_cookie(pkt) || + !add_msg_type(pkt, type)) { goto fail; } - return buf; + return pkt; fail: - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } /* Prepare DHCPv4 Message request and send it to peer */ static void send_request(struct net_if *iface) { - struct net_buf *buf; + struct net_pkt *pkt; uint32_t timeout; const struct in_addr *server_addr = net_ipv4_broadcast_address(); const struct in_addr *ciaddr = NULL; @@ -388,27 +388,27 @@ static void send_request(struct net_if *iface) break; } - buf = prepare_message(iface, DHCPV4_MSG_TYPE_REQUEST, ciaddr); - if (!buf) { + pkt = prepare_message(iface, DHCPV4_MSG_TYPE_REQUEST, ciaddr); + if (!pkt) { 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; } if (with_requested_ip - && !add_req_ipaddr(buf, &iface->dhcpv4.requested_ip)) { + && !add_req_ipaddr(pkt, &iface->dhcpv4.requested_ip)) { goto fail; } - if (!add_end(buf)) { + if (!add_end(pkt)) { 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; } @@ -441,32 +441,32 @@ static void send_request(struct net_if *iface) fail: NET_DBG("Message preparation failed"); - if (!buf) { - net_pkt_unref(buf); + if (!pkt) { + net_pkt_unref(pkt); } } /* Prepare DHCPv4 Discover message and broadcast it */ static void send_discover(struct net_if *iface) { - struct net_buf *buf; + struct net_pkt *pkt; uint32_t timeout; iface->dhcpv4.xid++; - buf = prepare_message(iface, DHCPV4_MSG_TYPE_DISCOVER, NULL); - if (!buf) { + pkt = prepare_message(iface, DHCPV4_MSG_TYPE_DISCOVER, NULL); + if (!pkt) { goto fail; } - if (!add_req_options(buf) || - !add_end(buf)) { + if (!add_req_options(pkt) || + !add_end(pkt)) { 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; } @@ -484,8 +484,8 @@ static void send_discover(struct net_if *iface) fail: NET_DBG("Message preparation failed"); - if (!buf) { - net_pkt_unref(buf); + if (!pkt) { + net_pkt_unref(pkt); } } @@ -662,18 +662,18 @@ static void dhcpv4_timeout(struct k_work *work) /* Parse DHCPv4 options and retrieve relavant information * 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, enum dhcpv4_msg_type *msg_type) { - struct net_buf *frag; uint8_t cookie[4]; uint8_t length; uint8_t type; uint16_t pos; - frag = net_pkt_read(buf, offset, &pos, sizeof(magic_cookie), - (uint8_t *)cookie); + frag = net_frag_read(frag, offset, &pos, sizeof(magic_cookie), + (uint8_t *)cookie); if (!frag || memcmp(magic_cookie, cookie, sizeof(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) { - frag = net_pkt_read_u8(frag, pos, &pos, &type); + frag = net_frag_read_u8(frag, pos, &pos, &type); if (type == DHCPV4_OPTIONS_END) { NET_DBG("options_end"); return NET_OK; } - frag = net_pkt_read_u8(frag, pos, &pos, &length); + frag = net_frag_read_u8(frag, pos, &pos, &length); if (!frag) { NET_ERR("option parsing, bad length"); return NET_DROP; @@ -703,8 +703,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf, return NET_DROP; } - frag = net_pkt_read(frag, pos, &pos, length, - netmask.s4_addr); + frag = net_frag_read(frag, pos, &pos, length, + netmask.s4_addr); if (!frag && pos) { NET_ERR("options_subnet_mask, short packet"); return NET_DROP; @@ -728,8 +728,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf, return NET_DROP; } - frag = net_pkt_read(frag, pos, &pos, 4, router.s4_addr); - frag = net_pkt_skip(frag, pos, &pos, length - 4); + frag = net_frag_read(frag, pos, &pos, 4, router.s4_addr); + frag = net_frag_skip(frag, pos, &pos, length - 4); if (!frag && pos) { NET_ERR("options_router, short packet"); return NET_DROP; @@ -746,8 +746,8 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf, return NET_DROP; } - frag = net_pkt_read_be32(frag, pos, &pos, - &iface->dhcpv4.lease_time); + frag = net_frag_read_be32(frag, pos, &pos, + &iface->dhcpv4.lease_time); NET_DBG("options_lease_time: %u", 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; } - frag = net_pkt_read_be32(frag, pos, &pos, - &iface->dhcpv4.renewal_time); + frag = net_frag_read_be32(frag, pos, &pos, + &iface->dhcpv4.renewal_time); NET_DBG("options_renewal: %u", 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; } - frag = net_pkt_read_be32(frag, pos, &pos, - &iface->dhcpv4.rebinding_time); + frag = net_frag_read_be32(frag, pos, &pos, + &iface->dhcpv4.rebinding_time); NET_DBG("options_rebinding: %u", 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; } - frag = net_pkt_read(frag, pos, &pos, length, - iface->dhcpv4.server_id.s4_addr); + frag = net_frag_read(frag, pos, &pos, length, + iface->dhcpv4.server_id.s4_addr); NET_DBG("options_server_id: %s", net_sprint_ipv4_addr(&iface->dhcpv4.server_id)); break; @@ -804,13 +804,13 @@ static enum net_verdict parse_options(struct net_if *iface, struct net_buf *buf, return NET_DROP; } - frag = net_pkt_read_u8(frag, pos, &pos, &v); + frag = net_frag_read_u8(frag, pos, &pos, &v); *msg_type = v; break; } default: NET_DBG("option unknown: %d", type); - frag = net_pkt_skip(frag, pos, &pos, length); + frag = net_frag_skip(frag, pos, &pos, length); 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, - struct net_buf *buf, + struct net_pkt *pkt, void *user_data) { struct dhcp_msg *msg; @@ -933,24 +933,24 @@ static enum net_verdict net_dhcpv4_input(struct net_conn *conn, return NET_DROP; } - if (!buf || !buf->frags) { - NET_DBG("Invalid buffer, no fragments"); + if (!pkt || !pkt->frags) { + NET_DBG("Invalid packet, no fragments"); return NET_DROP; } - iface = net_pkt_iface(buf); + iface = net_pkt_iface(pkt); if (!iface) { NET_DBG("no iface"); return NET_DROP; } - frag = buf->frags; + frag = pkt->frags; min = NET_IPV4UDPH_LEN + sizeof(struct dhcp_msg); /* If the message is not DHCP then continue passing to * 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"); return NET_CONTINUE; @@ -983,7 +983,7 @@ static enum net_verdict net_dhcpv4_input(struct net_conn *conn, sizeof(msg->yiaddr)); /* 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) { NET_DBG("short packet while skipping sname"); goto drop; @@ -994,7 +994,7 @@ static enum net_verdict net_dhcpv4_input(struct net_conn *conn, goto drop; } - net_pkt_unref(buf); + net_pkt_unref(pkt); handle_dhcpv4_reply(iface, msg_type); diff --git a/subsys/net/ip/icmpv4.c b/subsys/net/ip/icmpv4.c index f0fec031ac3..2c004a4f19b 100644 --- a/subsys/net/ip/icmpv4.c +++ b/subsys/net/ip/icmpv4.c @@ -22,13 +22,13 @@ #include "icmpv4.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 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. */ 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")]; 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_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out); + net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src), out); #endif /* CONFIG_NET_DEBUG_ICMPV4 */ - net_ipaddr_copy(&addr, &NET_IPV4_BUF(buf)->src); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, - &NET_IPV4_BUF(buf)->dst); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, &addr); + net_ipaddr_copy(&addr, &NET_IPV4_BUF(pkt)->src); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, + &NET_IPV4_BUF(pkt)->dst); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, &addr); - NET_ICMP_BUF(buf)->type = NET_ICMPV4_ECHO_REPLY; - NET_ICMP_BUF(buf)->code = 0; - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf); + NET_ICMP_BUF(pkt)->type = NET_ICMPV4_ECHO_REPLY; + NET_ICMP_BUF(pkt)->code = 0; + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv4(pkt); #if defined(CONFIG_NET_DEBUG_ICMPV4) 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_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out); + net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src), out); #endif /* CONFIG_NET_DEBUG_ICMPV4 */ - if (net_send_data(buf) < 0) { + if (net_send_data(pkt) < 0) { net_stats_update_icmp_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 -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 icmp_code) { - NET_IPV4_BUF(buf)->vhl = 0x45; - NET_IPV4_BUF(buf)->tos = 0x00; - NET_IPV4_BUF(buf)->len[0] = 0; - NET_IPV4_BUF(buf)->len[1] = sizeof(struct net_ipv4_hdr) + + NET_IPV4_BUF(pkt)->vhl = 0x45; + NET_IPV4_BUF(pkt)->tos = 0x00; + NET_IPV4_BUF(pkt)->len[0] = 0; + NET_IPV4_BUF(pkt)->len[1] = sizeof(struct net_ipv4_hdr) + NET_ICMPH_LEN + extra_len + NET_ICMPV4_UNUSED_LEN; - NET_IPV4_BUF(buf)->proto = IPPROTO_ICMP; - NET_IPV4_BUF(buf)->ttl = ttl; - NET_IPV4_BUF(buf)->offset[0] = NET_IPV4_BUF(buf)->offset[1] = 0; - NET_IPV4_BUF(buf)->id[0] = NET_IPV4_BUF(buf)->id[1] = 0; + NET_IPV4_BUF(pkt)->proto = IPPROTO_ICMP; + NET_IPV4_BUF(pkt)->ttl = ttl; + NET_IPV4_BUF(pkt)->offset[0] = NET_IPV4_BUF(pkt)->offset[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(buf)->chksum = ~net_calc_chksum_ipv4(buf); + NET_IPV4_BUF(pkt)->chksum = 0; + NET_IPV4_BUF(pkt)->chksum = ~net_calc_chksum_ipv4(pkt); - NET_ICMP_BUF(buf)->type = icmp_type; - NET_ICMP_BUF(buf)->code = icmp_code; + NET_ICMP_BUF(pkt)->type = icmp_type; + 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); } @@ -104,7 +104,8 @@ int net_icmpv4_send_echo_request(struct net_if *iface, uint16_t sequence) { 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 */ 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 * 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), 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_set_family(buf, AF_INET); - net_pkt_set_iface(buf, iface); + net_pkt_frag_add(pkt, frag); + net_pkt_set_family(pkt, AF_INET); + 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_ipaddr_copy(&NET_IPV4_BUF(buf)->src, src); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, dst); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, src); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, dst); - NET_ICMPV4_ECHO_REQ_BUF(buf)->identifier = htons(identifier); - NET_ICMPV4_ECHO_REQ_BUF(buf)->sequence = htons(sequence); + NET_ICMPV4_ECHO_REQ_BUF(pkt)->identifier = htons(identifier); + NET_ICMPV4_ECHO_REQ_BUF(pkt)->sequence = htons(sequence); - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv4(pkt); #if defined(CONFIG_NET_DEBUG_ICMPV4) do { char out[NET_IPV4_ADDR_LEN]; 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" " 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); #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_icmpv4_echo_req)); - if (net_send_data(buf) >= 0) { + if (net_send_data(pkt) >= 0) { net_stats_update_icmp_sent(); return 0; } - net_pkt_unref(buf); + net_pkt_unref(pkt); net_stats_update_icmp_drop(); 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); size_t extra_len, reserve; 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) { /* We must not send ICMP errors back */ err = -EINVAL; - goto drop_no_buf; + goto drop_no_pkt; } } iface = net_pkt_iface(orig); - buf = net_pkt_get_reserve_tx(0, BUF_WAIT_TIME); - if (!buf) { + pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME); + if (!pkt) { err = -ENOMEM; - goto drop_no_buf; + goto drop_no_pkt; } 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 - * 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; 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. * 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) { err = -ENOMEM; goto drop; } - net_buf_frag_add(buf, frag); - net_pkt_set_family(buf, AF_INET); - net_pkt_set_iface(buf, iface); - net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); + net_pkt_frag_add(pkt, frag); + net_pkt_set_family(pkt, AF_INET); + net_pkt_set_iface(pkt, iface); + 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); net_ipaddr_copy(&addr, src); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, dst); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, &addr); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, dst); + 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(buf)->len = net_pkt_ll_dst(orig)->len; - net_pkt_ll_dst(buf)->addr = net_pkt_ll_src(orig)->addr; - net_pkt_ll_dst(buf)->len = net_pkt_ll_src(orig)->len; + net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr; + net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len; + net_pkt_ll_dst(pkt)->addr = net_pkt_ll_src(orig)->addr; + net_pkt_ll_dst(pkt)->len = net_pkt_ll_src(orig)->len; - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv4(pkt); #if defined(CONFIG_NET_DEBUG_ICMPV4) do { char out[sizeof("xxx.xxx.xxx.xxx")]; 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 " "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); #endif /* CONFIG_NET_DEBUG_ICMPV4 */ - if (net_send_data(buf) >= 0) { + if (net_send_data(pkt) >= 0) { net_stats_update_icmp_sent(); return 0; } drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); -drop_no_buf: +drop_no_pkt: net_stats_update_icmp_drop(); return err; @@ -278,7 +280,7 @@ void net_icmpv4_unregister_handler(struct net_icmpv4_handler *handler) 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) { 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) { if (cb->type == type && (cb->code == code || cb->code == 0)) { - return cb->handler(buf); + return cb->handler(pkt); } } diff --git a/subsys/net/ip/icmpv4.h b/subsys/net/ip/icmpv4.h index 2938ad72000..8e9db9e9408 100644 --- a/subsys/net/ip/icmpv4.h +++ b/subsys/net/ip/icmpv4.h @@ -30,11 +30,11 @@ struct net_icmpv4_echo_req { uint16_t sequence; } __packed; -#define NET_ICMPV4_ECHO_REQ_BUF(buf) \ - ((struct net_icmpv4_echo_req *)(net_pkt_icmp_data(buf) + \ +#define NET_ICMPV4_ECHO_REQ_BUF(pkt) \ + ((struct net_icmpv4_echo_req *)(net_pkt_icmp_data(pkt) + \ 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 { sys_snode_t node; @@ -45,12 +45,12 @@ struct net_icmpv4_handler { /** * @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 code Code of the type of the error message. * @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. @@ -73,7 +73,7 @@ void net_icmpv4_register_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); #if defined(CONFIG_NET_IPV4) diff --git a/subsys/net/ip/icmpv6.c b/subsys/net/ip/icmpv6.c index 2897f2dd015..5992fcb9280 100644 --- a/subsys/net/ip/icmpv6.c +++ b/subsys/net/ip/icmpv6.c @@ -28,7 +28,7 @@ #include "rpl.h" #endif -#define BUF_WAIT_TIME K_SECONDS(1) +#define PKT_WAIT_TIME K_SECONDS(1) 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); } -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 icmp_code) { - NET_IPV6_BUF(buf)->vtc = 0x60; - NET_IPV6_BUF(buf)->tcflow = 0; - NET_IPV6_BUF(buf)->flow = 0; + NET_IPV6_BUF(pkt)->vtc = 0x60; + NET_IPV6_BUF(pkt)->tcflow = 0; + NET_IPV6_BUF(pkt)->flow = 0; 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(buf)->hop_limit = hop_limit; + NET_IPV6_BUF(pkt)->nexthdr = IPPROTO_ICMPV6; + 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(buf)->code = icmp_code; + NET_ICMP_BUF(pkt)->type = icmp_type; + NET_ICMP_BUF(pkt)->code = icmp_code; /* 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); } #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]; 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_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]; 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_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src), out); + net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src), out); } #else -#define echo_request_debug(buf) -#define echo_reply_debug(buf) +#define echo_request_debug(pkt) +#define echo_reply_debug(pkt) #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_if *iface; uint16_t payload_len; @@ -135,83 +135,83 @@ static enum net_verdict handle_echo_request(struct net_buf *orig) iface = net_pkt_iface(orig); - buf = net_pkt_get_reserve_tx(0, BUF_WAIT_TIME); - if (!buf) { - goto drop_no_buf; + pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME); + if (!pkt) { + goto drop_no_pkt; } payload_len = sys_get_be16(NET_IPV6_BUF(orig)->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) { goto drop; } - net_buf_frag_add(buf, frag); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_iface(buf, iface); - net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_frag_add(pkt, frag); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_iface(pkt, iface); + net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag)); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); 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 { - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ext_len(pkt, 0); } /* Set up IPv6 Header fields */ - NET_IPV6_BUF(buf)->vtc = 0x60; - NET_IPV6_BUF(buf)->tcflow = 0; - NET_IPV6_BUF(buf)->flow = 0; - NET_IPV6_BUF(buf)->hop_limit = net_if_ipv6_get_hop_limit(iface); + NET_IPV6_BUF(pkt)->vtc = 0x60; + NET_IPV6_BUF(pkt)->tcflow = 0; + NET_IPV6_BUF(pkt)->flow = 0; + NET_IPV6_BUF(pkt)->hop_limit = net_if_ipv6_get_hop_limit(iface); /* ICMPv6 fields */ - NET_ICMP_BUF(buf)->type = NET_ICMPV6_ECHO_REPLY; - NET_ICMP_BUF(buf)->code = 0; - NET_ICMP_BUF(buf)->chksum = 0; + NET_ICMP_BUF(pkt)->type = NET_ICMPV6_ECHO_REPLY; + NET_ICMP_BUF(pkt)->code = 0; + NET_ICMP_BUF(pkt)->chksum = 0; - if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, + if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) { + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, &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_IPV6_BUF(orig)->dst)); } else { struct in6_addr addr; 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_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) 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 */ goto drop; } #endif } - net_pkt_ll_src(buf)->addr = net_pkt_ll_dst(orig)->addr; - net_pkt_ll_src(buf)->len = net_pkt_ll_dst(orig)->len; + net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr; + net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len; /* We must not set the destination ll address here but trust * 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(buf)->chksum = ~net_calc_chksum_icmpv6(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + 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; } @@ -221,18 +221,19 @@ static enum net_verdict handle_echo_request(struct net_buf *orig) return NET_OK; drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); -drop_no_buf: +drop_no_pkt: net_stats_update_icmp_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) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_if *iface; size_t extra_len, reserve; 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) { /* We must not send ICMP errors back */ err = -EINVAL; - goto drop_no_buf; + goto drop_no_pkt; } } iface = net_pkt_iface(orig); - buf = net_pkt_get_reserve_tx(0, BUF_WAIT_TIME); - if (!buf) { + pkt = net_pkt_get_reserve_tx(0, PKT_WAIT_TIME); + if (!pkt) { err = -ENOMEM; - goto drop_no_buf; + goto drop_no_pkt; } /* 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. * 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) { err = -ENOMEM; goto drop; } - net_buf_frag_add(buf, frag); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_iface(buf, iface); - net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); - net_pkt_set_ext_len(buf, 0); + net_pkt_frag_add(pkt, frag); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_iface(pkt, iface); + net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag)); + 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); /* Depending on error option, we store the param into the ICMP message. */ 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)); } 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_ipaddr_copy(&NET_IPV6_BUF(buf)->src, + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, net_if_ipv6_select_src_addr(iface, &NET_IPV6_BUF(orig)->dst)); } else { struct in6_addr addr; 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_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(buf)->len = net_pkt_ll_dst(orig)->len; - net_pkt_ll_dst(buf)->addr = net_pkt_ll_src(orig)->addr; - net_pkt_ll_dst(buf)->len = net_pkt_ll_src(orig)->len; + net_pkt_ll_src(pkt)->addr = net_pkt_ll_dst(orig)->addr; + net_pkt_ll_src(pkt)->len = net_pkt_ll_dst(orig)->len; + net_pkt_ll_dst(pkt)->addr = net_pkt_ll_src(orig)->addr; + net_pkt_ll_dst(pkt)->len = net_pkt_ll_src(orig)->len; - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt); #if defined(CONFIG_NET_DEBUG_ICMPV6) do { char out[NET_IPV6_ADDR_LEN]; 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" " 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); #endif /* CONFIG_NET_DEBUG_ICMPV6 */ - if (net_send_data(buf) >= 0) { + if (net_send_data(pkt) >= 0) { net_stats_update_icmp_sent(); return 0; } drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); -drop_no_buf: +drop_no_pkt: net_stats_update_icmp_drop(); return err; @@ -357,31 +358,31 @@ int net_icmpv6_send_echo_request(struct net_if *iface, uint16_t sequence) { const struct in6_addr *src; - struct net_buf *buf; + struct net_pkt *pkt; 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); - 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_iface(buf, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_iface(pkt, iface); - net_pkt_append_u8(buf, NET_ICMPV6_ECHO_REQUEST); - net_pkt_append_u8(buf, 0); /* code */ - net_pkt_append_be16(buf, 0); /* checksum */ - net_pkt_append_be16(buf, identifier); - net_pkt_append_be16(buf, sequence); + net_pkt_append_u8(pkt, NET_ICMPV6_ECHO_REQUEST); + net_pkt_append_u8(pkt, 0); /* code */ + net_pkt_append_be16(pkt, 0); /* checksum */ + net_pkt_append_be16(pkt, identifier); + net_pkt_append_be16(pkt, sequence); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, src); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, dst); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, dst); - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + 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; } @@ -390,26 +391,26 @@ int net_icmpv6_send_echo_request(struct net_if *iface, char out[NET_IPV6_ADDR_LEN]; 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" " 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); #endif /* CONFIG_NET_DEBUG_ICMPV6 */ - if (net_send_data(buf) >= 0) { + if (net_send_data(pkt) >= 0) { net_stats_update_icmp_sent(); return 0; } drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); net_stats_update_icmp_drop(); 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) { 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) { if (cb->type == type && (cb->code == code || cb->code == 0)) { - return cb->handler(buf); + return cb->handler(pkt); } } diff --git a/subsys/net/ip/icmpv6.h b/subsys/net/ip/icmpv6.h index a47aa273f4a..c4d723f0562 100644 --- a/subsys/net/ip/icmpv6.h +++ b/subsys/net/ip/icmpv6.h @@ -75,25 +75,25 @@ struct net_icmpv6_nd_opt_6co { struct in6_addr prefix; } __packed; -#define NET_ICMPV6_NS_BUF(buf) \ - ((struct net_icmpv6_ns_hdr *)(net_pkt_icmp_data(buf) + \ +#define NET_ICMPV6_NS_BUF(pkt) \ + ((struct net_icmpv6_ns_hdr *)(net_pkt_icmp_data(pkt) + \ sizeof(struct net_icmp_hdr))) -#define NET_ICMPV6_ND_OPT_HDR_BUF(buf) \ - ((struct net_icmpv6_nd_opt_hdr *)(net_pkt_icmp_data(buf) + \ +#define NET_ICMPV6_ND_OPT_HDR_BUF(pkt) \ + ((struct net_icmpv6_nd_opt_hdr *)(net_pkt_icmp_data(pkt) + \ sizeof(struct net_icmp_hdr) + \ - net_pkt_ext_opt_len(buf))) + net_pkt_ext_opt_len(pkt))) -#define NET_ICMPV6_NA_BUF(buf) \ - ((struct net_icmpv6_na_hdr *)(net_pkt_icmp_data(buf) + \ +#define NET_ICMPV6_NA_BUF(pkt) \ + ((struct net_icmpv6_na_hdr *)(net_pkt_icmp_data(pkt) + \ sizeof(struct net_icmp_hdr))) -#define NET_ICMPV6_RS_BUF(buf) \ - ((struct net_icmpv6_rs_hdr *)(net_pkt_icmp_data(buf) + \ +#define NET_ICMPV6_RS_BUF(pkt) \ + ((struct net_icmpv6_rs_hdr *)(net_pkt_icmp_data(pkt) + \ sizeof(struct net_icmp_hdr))) -#define NET_ICMPV6_RA_BUF(buf) \ - ((struct net_icmpv6_ra_hdr *)(net_pkt_icmp_data(buf) + \ +#define NET_ICMPV6_RA_BUF(pkt) \ + ((struct net_icmpv6_ra_hdr *)(net_pkt_icmp_data(pkt) + \ sizeof(struct net_icmp_hdr))) #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 */ #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); @@ -161,7 +161,7 @@ struct net_icmpv6_handler { /** * @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 code Code of the type of the error message. * @param param Optional parameter value for this error. Depending on type @@ -169,7 +169,7 @@ struct net_icmpv6_handler { * what value to use. * @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); /** @@ -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_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); #if defined(CONFIG_NET_IPV6) void net_icmpv6_init(void); diff --git a/subsys/net/ip/ipv4.c b/subsys/net/ip/ipv4.c index 40094bd4dcf..e67d7ec3378 100644 --- a/subsys/net/ip/ipv4.c +++ b/subsys/net/ip/ipv4.c @@ -24,7 +24,7 @@ #include "icmpv4.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 *dst, struct net_if *iface, @@ -32,33 +32,33 @@ struct net_buf *net_ipv4_create_raw(struct net_buf *buf, { 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(buf)->tos = 0x00; - NET_IPV4_BUF(buf)->proto = 0; + NET_IPV4_BUF(pkt)->vhl = 0x45; + NET_IPV4_BUF(pkt)->tos = 0x00; + NET_IPV4_BUF(pkt)->proto = 0; - NET_IPV4_BUF(buf)->ttl = net_if_ipv4_get_ttl(iface); - NET_IPV4_BUF(buf)->offset[0] = NET_IPV4_BUF(buf)->offset[1] = 0; - NET_IPV4_BUF(buf)->id[0] = NET_IPV4_BUF(buf)->id[1] = 0; + NET_IPV4_BUF(pkt)->ttl = net_if_ipv4_get_ttl(iface); + NET_IPV4_BUF(pkt)->offset[0] = NET_IPV4_BUF(pkt)->offset[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(buf)->src, src); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, dst); + 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_family(buf, AF_INET); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr)); + net_pkt_set_family(pkt, AF_INET); 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_buf *buf, +struct net_pkt *net_ipv4_create(struct net_context *context, + struct net_pkt *pkt, const struct in_addr *src, 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) || 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, dst, net_context_get_iface(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 */ 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(buf)->len[1] = total_len - NET_IPV4_BUF(buf)->len[0] * 256; + NET_IPV4_BUF(pkt)->len[0] = total_len / 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(buf)->chksum = ~net_calc_chksum_ipv4(buf); + NET_IPV4_BUF(pkt)->chksum = 0; + NET_IPV4_BUF(pkt)->chksum = ~net_calc_chksum_ipv4(pkt); #if defined(CONFIG_NET_UDP) if (next_header == IPPROTO_UDP) { - NET_UDP_BUF(buf)->chksum = 0; - NET_UDP_BUF(buf)->chksum = ~net_calc_chksum_udp(buf); + NET_UDP_BUF(pkt)->chksum = 0; + NET_UDP_BUF(pkt)->chksum = ~net_calc_chksum_udp(pkt); } #endif #if defined(CONFIG_NET_TCP) if (next_header == IPPROTO_TCP) { - NET_TCP_BUF(buf)->chksum = 0; - NET_TCP_BUF(buf)->chksum = ~net_calc_chksum_tcp(buf); + NET_TCP_BUF(pkt)->chksum = 0; + NET_TCP_BUF(pkt)->chksum = ~net_calc_chksum_tcp(pkt); } #endif 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)); } @@ -131,26 +131,26 @@ const struct in_addr *net_ipv4_broadcast_address(void) 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_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", 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); - int real_len = net_buf_frags_len(buf); + struct net_ipv4_hdr *hdr = NET_IPV4_BUF(pkt); + int real_len = net_pkt_get_len(pkt); int pkt_len = (hdr->len[0] << 8) + hdr->len[1]; enum net_verdict verdict = NET_DROP; 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; } @@ -165,7 +165,7 @@ enum net_verdict net_ipv4_process_pkt(struct net_buf *buf) } while (0); #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 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_broadcast_address())) { - verdict = net_conn_input(IPPROTO_UDP, buf); + verdict = net_conn_input(IPPROTO_UDP, pkt); if (verdict != NET_DROP) { return verdict; } } #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; } switch (hdr->proto) { case IPPROTO_ICMP: - verdict = process_icmpv4_pkt(buf, hdr); + verdict = process_icmpv4_pkt(pkt, hdr); break; case IPPROTO_UDP: - verdict = net_conn_input(IPPROTO_UDP, buf); + verdict = net_conn_input(IPPROTO_UDP, pkt); break; case IPPROTO_TCP: - verdict = net_conn_input(IPPROTO_TCP, buf); + verdict = net_conn_input(IPPROTO_TCP, pkt); break; } diff --git a/subsys/net/ip/ipv4.h b/subsys/net/ip/ipv4.h index ad5c3b98dc3..ce298f56cc8 100644 --- a/subsys/net/ip/ipv4.h +++ b/subsys/net/ip/ipv4.h @@ -23,34 +23,34 @@ #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 dst Destination IPv4 address * @param iface Network interface * @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 *dst, struct net_if *iface, 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 buf Network buffer + * @param pkt Network packet * @param src_addr Source address, or NULL to choose a default * @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_buf *buf, +struct net_pkt *net_ipv4_create(struct net_context *context, + struct net_pkt *pkt, const struct in_addr *src_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 * 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. * * @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 @@ -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. * * @param context Network context for a connection - * @param buf Network buffer + * @param pkt Network packet * * @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 */ diff --git a/subsys/net/ip/ipv6.c b/subsys/net/ip/ipv6.c index bd4015dea88..a7d14e26e03 100644 --- a/subsys/net/ip/ipv6.c +++ b/subsys/net/ip/ipv6.c @@ -91,19 +91,19 @@ static void ipv6_nbr_set_state(struct net_nbr *nbr, net_ipv6_nbr_data(nbr)->state = new_state; } -static inline bool net_is_solicited(struct net_buf *buf) +static inline bool net_is_solicited(struct net_pkt *pkt) { - return NET_ICMPV6_NA_BUF(buf)->flags & NET_ICMPV6_NA_FLAG_SOLICITED; + return NET_ICMPV6_NA_BUF(pkt)->flags & NET_ICMPV6_NA_FLAG_SOLICITED; } -static inline bool net_is_router(struct net_buf *buf) +static inline bool net_is_router(struct net_pkt *pkt) { - return NET_ICMPV6_NA_BUF(buf)->flags & NET_ICMPV6_NA_FLAG_ROUTER; + return NET_ICMPV6_NA_BUF(pkt)->flags & NET_ICMPV6_NA_FLAG_ROUTER; } -static inline bool net_is_override(struct net_buf *buf) +static inline bool net_is_override(struct net_pkt *pkt) { - return NET_ICMPV6_NA_BUF(buf)->flags & NET_ICMPV6_NA_FLAG_OVERRIDE; + return NET_ICMPV6_NA_BUF(pkt)->flags & NET_ICMPV6_NA_FLAG_OVERRIDE; } static inline struct net_nbr *get_nbr(int idx) @@ -272,7 +272,7 @@ static void ns_reply_timeout(struct k_work *work) /* To unref when pending variable was set */ net_pkt_unref(data->pending); - /* To unref the original buf allocation */ + /* To unref the original pkt allocation */ net_pkt_unref(data->pending); data->pending = NULL; @@ -449,12 +449,12 @@ struct net_nbr *net_ipv6_nbr_add(struct net_if *iface, return nbr; } -static inline struct net_nbr *nbr_add(struct net_buf *buf, +static inline struct net_nbr *nbr_add(struct net_pkt *pkt, struct net_linkaddr *lladdr, bool is_router, enum net_ipv6_nbr_state state) { - return net_ipv6_nbr_add(net_pkt_iface(buf), &NET_IPV6_BUF(buf)->src, + return net_ipv6_nbr_add(net_pkt_iface(pkt), &NET_IPV6_BUF(pkt)->src, lladdr, is_router, state); } @@ -499,10 +499,10 @@ struct in6_addr *net_ipv6_nbr_lookup_by_index(struct net_if *iface, } #endif /* CONFIG_NET_IPV6_NBR_CACHE */ -int net_ipv6_find_last_ext_hdr(struct net_buf *buf) +int net_ipv6_find_last_ext_hdr(struct net_pkt *pkt) { - struct net_ipv6_hdr *hdr = NET_IPV6_BUF(buf); - struct net_buf *frag = buf->frags; + struct net_ipv6_hdr *hdr = NET_IPV6_BUF(pkt); + struct net_buf *frag = pkt->frags; int pos = 6; /* Initial value if no extension fragments were found */ uint16_t offset; uint8_t next_hdr; @@ -513,12 +513,12 @@ int net_ipv6_find_last_ext_hdr(struct net_buf *buf) next = hdr->nexthdr; while (frag) { - frag = net_pkt_read_u8(frag, offset, &offset, &next_hdr); - if (frag != buf->frags) { + frag = net_frag_read_u8(frag, offset, &offset, &next_hdr); + if (frag != pkt->frags) { break; } - frag = net_pkt_read_u8(frag, offset, &offset, &length); + frag = net_frag_read_u8(frag, offset, &offset, &length); if (!frag && offset == 0xffff) { pos = -EINVAL; goto fail; @@ -569,7 +569,7 @@ const struct in6_addr *net_ipv6_unspecified_address(void) return &addr; } -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 *dst, struct net_if *iface, @@ -577,39 +577,39 @@ struct net_buf *net_ipv6_create_raw(struct net_buf *buf, { 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_IPV6_BUF(buf)->vtc = 0x60; - NET_IPV6_BUF(buf)->tcflow = 0; - NET_IPV6_BUF(buf)->flow = 0; + NET_IPV6_BUF(pkt)->vtc = 0x60; + NET_IPV6_BUF(pkt)->tcflow = 0; + NET_IPV6_BUF(pkt)->flow = 0; - NET_IPV6_BUF(buf)->nexthdr = 0; + NET_IPV6_BUF(pkt)->nexthdr = 0; /* User can tweak the default hop limit if needed */ - NET_IPV6_BUF(buf)->hop_limit = net_pkt_ipv6_hop_limit(buf); - if (NET_IPV6_BUF(buf)->hop_limit == 0) { - NET_IPV6_BUF(buf)->hop_limit = + NET_IPV6_BUF(pkt)->hop_limit = net_pkt_ipv6_hop_limit(pkt); + if (NET_IPV6_BUF(pkt)->hop_limit == 0) { + NET_IPV6_BUF(pkt)->hop_limit = net_if_ipv6_get_hop_limit(iface); } - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, dst); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, dst); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, src); - net_pkt_set_ext_len(buf, 0); - NET_IPV6_BUF(buf)->nexthdr = next_header; + net_pkt_set_ext_len(pkt, 0); + NET_IPV6_BUF(pkt)->nexthdr = next_header; - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); - net_pkt_set_family(buf, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); + net_pkt_set_family(pkt, AF_INET6); net_buf_add(header, sizeof(struct net_ipv6_hdr)); - return buf; + return pkt; } -struct net_buf *net_ipv6_create(struct net_context *context, - struct net_buf *buf, +struct net_pkt *net_ipv6_create(struct net_context *context, + struct net_pkt *pkt, const struct in6_addr *src, const struct in6_addr *dst) { @@ -621,18 +621,18 @@ struct net_buf *net_ipv6_create(struct net_context *context, if (net_is_ipv6_addr_unspecified(src) || net_is_ipv6_addr_mcast(src)) { - src = net_if_ipv6_select_src_addr(net_pkt_iface(buf), + src = net_if_ipv6_select_src_addr(net_pkt_iface(pkt), (struct in6_addr *)dst); } - return net_ipv6_create_raw(buf, + return net_ipv6_create_raw(pkt, src, dst, net_context_get_iface(context), net_context_get_ip_proto(context)); } -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) { /* Set the length of the IPv6 header */ size_t total_len; @@ -640,48 +640,48 @@ int net_ipv6_finalize_raw(struct net_buf *buf, uint8_t next_header) #if defined(CONFIG_NET_UDP) && defined(CONFIG_NET_RPL_INSERT_HBH_OPTION) if (next_header != IPPROTO_TCP && next_header != IPPROTO_ICMPV6) { /* Check if we need to add RPL header to sent UDP packet. */ - if (net_rpl_insert_header(buf) < 0) { + if (net_rpl_insert_header(pkt) < 0) { NET_DBG("RPL HBHO insert failed"); return -EINVAL; } } #endif - net_pkt_compact(buf); + net_pkt_compact(pkt); - total_len = net_buf_frags_len(buf->frags); + total_len = net_pkt_get_len(pkt); total_len -= sizeof(struct net_ipv6_hdr); - NET_IPV6_BUF(buf)->len[0] = total_len / 256; - NET_IPV6_BUF(buf)->len[1] = total_len - NET_IPV6_BUF(buf)->len[0] * 256; + NET_IPV6_BUF(pkt)->len[0] = total_len / 256; + NET_IPV6_BUF(pkt)->len[1] = total_len - NET_IPV6_BUF(pkt)->len[0] * 256; #if defined(CONFIG_NET_UDP) if (next_header == IPPROTO_UDP) { - NET_UDP_BUF(buf)->chksum = 0; - NET_UDP_BUF(buf)->chksum = ~net_calc_chksum_udp(buf); + NET_UDP_BUF(pkt)->chksum = 0; + NET_UDP_BUF(pkt)->chksum = ~net_calc_chksum_udp(pkt); } else #endif #if defined(CONFIG_NET_TCP) if (next_header == IPPROTO_TCP) { - NET_TCP_BUF(buf)->chksum = 0; - NET_TCP_BUF(buf)->chksum = ~net_calc_chksum_tcp(buf); + NET_TCP_BUF(pkt)->chksum = 0; + NET_TCP_BUF(pkt)->chksum = ~net_calc_chksum_tcp(pkt); } else #endif if (next_header == IPPROTO_ICMPV6) { - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum(buf, + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum(pkt, IPPROTO_ICMPV6); } return 0; } -int net_ipv6_finalize(struct net_context *context, struct net_buf *buf) +int net_ipv6_finalize(struct net_context *context, struct net_pkt *pkt) { - return net_ipv6_finalize_raw(buf, net_context_get_ip_proto(context)); + return net_ipv6_finalize_raw(pkt, net_context_get_ip_proto(context)); } #if defined(CONFIG_NET_IPV6_DAD) @@ -712,7 +712,7 @@ static inline bool dad_failed(struct net_if *iface, struct in6_addr *addr) * Thus we need to check it here. Note that this cannot happen for IPv4 * as 802.15.4 supports IPv6 only. */ -static struct net_buf *update_ll_reserve(struct net_buf *buf, +static struct net_pkt *update_ll_reserve(struct net_pkt *pkt, struct in6_addr *addr) { /* We need to go through all the fragments and adjust the @@ -725,33 +725,33 @@ static struct net_buf *update_ll_reserve(struct net_buf *buf, * as we already know everything about the destination of * the packet. */ - if (net_pkt_forwarding(buf)) { - return buf; + if (net_pkt_forwarding(pkt)) { + return pkt; } - reserve = net_if_get_ll_reserve(net_pkt_iface(buf), addr); - if (reserve == net_pkt_ll_reserve(buf)) { - return buf; + reserve = net_if_get_ll_reserve(net_pkt_iface(pkt), addr); + if (reserve == net_pkt_ll_reserve(pkt)) { + return pkt; } NET_DBG("Adjust reserve old %d new %d", - net_pkt_ll_reserve(buf), reserve); + net_pkt_ll_reserve(pkt), reserve); - net_pkt_set_ll_reserve(buf, reserve); + net_pkt_set_ll_reserve(pkt, reserve); - orig_frag = buf->frags; + orig_frag = pkt->frags; copy_len = orig_frag->len; pos = 0; - buf->frags = NULL; + pkt->frags = NULL; room_len = 0; frag = NULL; while (orig_frag) { if (!room_len) { - 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); room_len = net_buf_tailroom(frag); } @@ -777,7 +777,7 @@ static struct net_buf *update_ll_reserve(struct net_buf *buf, orig_frag = orig_frag->frags; tmp->frags = NULL; - net_pkt_unref(tmp); + net_pkt_frag_unref(tmp); if (!orig_frag) { break; @@ -788,31 +788,31 @@ static struct net_buf *update_ll_reserve(struct net_buf *buf, } } - return buf; + return pkt; } -struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) +struct net_pkt *net_ipv6_prepare_for_send(struct net_pkt *pkt) { struct in6_addr *nexthop = NULL; struct net_if *iface = NULL; struct net_nbr *nbr; - NET_ASSERT(buf && buf->frags); + NET_ASSERT(pkt && pkt->frags); #if defined(CONFIG_NET_IPV6_FRAGMENT) /* If we have already fragmented the packet, the fragment id will * contain a proper value and we can skip other checks. */ - if (net_pkt_ipv6_fragment_id(buf) == 0) { - size_t pkt_len = net_buf_frags_len(buf); + if (net_pkt_ipv6_fragment_id(pkt) == 0) { + size_t pkt_len = net_pkt_get_len(pkt); if (pkt_len > NET_IPV6_MTU) { int ret; - ret = net_ipv6_send_fragmented_pkt(net_pkt_iface(buf), - buf, pkt_len); + ret = net_ipv6_send_fragmented_pkt(net_pkt_iface(pkt), + pkt, pkt_len); if (ret < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } /* No need to continue with the sending as the packet @@ -827,19 +827,19 @@ struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) /* Workaround Linux bug, see: * https://jira.zephyrproject.org/browse/ZEP-1656 */ - if (atomic_test_bit(net_pkt_iface(buf)->flags, NET_IF_POINTOPOINT)) { - return buf; + if (atomic_test_bit(net_pkt_iface(pkt)->flags, NET_IF_POINTOPOINT)) { + return pkt; } - if (net_pkt_ll_dst(buf)->addr || - net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { - return update_ll_reserve(buf, &NET_IPV6_BUF(buf)->dst); + if (net_pkt_ll_dst(pkt)->addr || + net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) { + return update_ll_reserve(pkt, &NET_IPV6_BUF(pkt)->dst); } if (net_if_ipv6_addr_onlink(&iface, - &NET_IPV6_BUF(buf)->dst)) { - nexthop = &NET_IPV6_BUF(buf)->dst; - net_pkt_set_iface(buf, iface); + &NET_IPV6_BUF(pkt)->dst)) { + nexthop = &NET_IPV6_BUF(pkt)->dst; + net_pkt_set_iface(pkt, iface); } else { /* We need to figure out where the destination * host is located. @@ -847,7 +847,7 @@ struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) struct net_route_entry *route; struct net_if_router *router; - route = net_route_lookup(NULL, &NET_IPV6_BUF(buf)->dst); + route = net_route_lookup(NULL, &NET_IPV6_BUF(pkt)->dst); if (route) { nexthop = net_route_get_nexthop(route); if (!nexthop) { @@ -857,9 +857,9 @@ struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) NET_DBG("No route to host %s", net_sprint_ipv6_addr( - &NET_IPV6_BUF(buf)->dst)); + &NET_IPV6_BUF(pkt)->dst)); - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } } else { @@ -867,14 +867,14 @@ struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) * route instead. */ router = net_if_ipv6_router_find_default(NULL, - &NET_IPV6_BUF(buf)->dst); + &NET_IPV6_BUF(pkt)->dst); if (!router) { NET_DBG("No default route to %s", net_sprint_ipv6_addr( - &NET_IPV6_BUF(buf)->dst)); + &NET_IPV6_BUF(pkt)->dst)); /* Try to send the packet anyway */ - nexthop = &NET_IPV6_BUF(buf)->dst; + nexthop = &NET_IPV6_BUF(pkt)->dst; goto try_send; } @@ -882,8 +882,8 @@ struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) } } - if (net_rpl_update_header(buf, nexthop) < 0) { - net_pkt_unref(buf); + if (net_rpl_update_header(pkt, nexthop) < 0) { + net_pkt_unref(pkt); return NULL; } @@ -892,7 +892,7 @@ struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) * figure out the interface using nexthop instead. */ if (net_if_ipv6_addr_onlink(&iface, nexthop)) { - net_pkt_set_iface(buf, iface); + net_pkt_set_iface(pkt, iface); } /* If the above check returns null, we try to send @@ -901,11 +901,11 @@ struct net_buf *net_ipv6_prepare_for_send(struct net_buf *buf) } try_send: - nbr = nbr_lookup(&net_neighbor.table, net_pkt_iface(buf), nexthop); + nbr = nbr_lookup(&net_neighbor.table, net_pkt_iface(pkt), nexthop); NET_DBG("Neighbor lookup %p (%d) iface %p addr %s state %s", nbr, nbr ? nbr->idx : NET_NBR_LLADDR_UNKNOWN, - net_pkt_iface(buf), + net_pkt_iface(pkt), net_sprint_ipv6_addr(nexthop), nbr ? net_ipv6_nbr_state2str(net_ipv6_nbr_data(nbr)->state) : "-"); @@ -915,8 +915,8 @@ try_send: lladdr = net_nbr_get_lladdr(nbr->idx); - net_pkt_ll_dst(buf)->addr = lladdr->addr; - net_pkt_ll_dst(buf)->len = lladdr->len; + net_pkt_ll_dst(pkt)->addr = lladdr->addr; + net_pkt_ll_dst(pkt)->len = lladdr->len; NET_DBG("Neighbor %p addr %s", nbr, net_sprint_ll_addr(lladdr->addr, lladdr->len)); @@ -934,29 +934,29 @@ try_send: } #endif - return update_ll_reserve(buf, nexthop); + return update_ll_reserve(pkt, nexthop); } #if defined(CONFIG_NET_IPV6_ND) /* We need to send NS and wait for NA before sending the packet. */ - if (net_ipv6_send_ns(net_pkt_iface(buf), - buf, - &NET_IPV6_BUF(buf)->src, + if (net_ipv6_send_ns(net_pkt_iface(pkt), + pkt, + &NET_IPV6_BUF(pkt)->src, NULL, nexthop, false) < 0) { /* In case of an error, the NS send function will unref - * the buf. + * the pkt. */ return NULL; } - NET_DBG("Buf %p (frag %p) will be sent later", buf, buf->frags); + NET_DBG("Pkt %p (frag %p) will be sent later", pkt, pkt->frags); #else - NET_DBG("Buf %p (frag %p) cannot be sent, dropping it.", buf, - buf->frags); + NET_DBG("Pkt %p (frag %p) cannot be sent, dropping it.", pkt, + pkt->frags); - net_pkt_unref(buf); + net_pkt_unref(pkt); #endif /* CONFIG_NET_IPV6_ND */ return NULL; @@ -1020,23 +1020,23 @@ static inline void set_llao(struct net_linkaddr *lladdr, llao_len - lladdr->len - 2); } -static void setup_headers(struct net_buf *buf, uint8_t nd6_len, +static void setup_headers(struct net_pkt *pkt, uint8_t nd6_len, uint8_t icmp_type) { - NET_IPV6_BUF(buf)->vtc = 0x60; - NET_IPV6_BUF(buf)->tcflow = 0; - NET_IPV6_BUF(buf)->flow = 0; - NET_IPV6_BUF(buf)->len[0] = 0; - NET_IPV6_BUF(buf)->len[1] = NET_ICMPH_LEN + nd6_len; + NET_IPV6_BUF(pkt)->vtc = 0x60; + NET_IPV6_BUF(pkt)->tcflow = 0; + NET_IPV6_BUF(pkt)->flow = 0; + NET_IPV6_BUF(pkt)->len[0] = 0; + NET_IPV6_BUF(pkt)->len[1] = NET_ICMPH_LEN + nd6_len; - NET_IPV6_BUF(buf)->nexthdr = IPPROTO_ICMPV6; - NET_IPV6_BUF(buf)->hop_limit = NET_IPV6_ND_HOP_LIMIT; + NET_IPV6_BUF(pkt)->nexthdr = IPPROTO_ICMPV6; + NET_IPV6_BUF(pkt)->hop_limit = NET_IPV6_ND_HOP_LIMIT; - NET_ICMP_BUF(buf)->type = icmp_type; - NET_ICMP_BUF(buf)->code = 0; + NET_ICMP_BUF(pkt)->type = icmp_type; + NET_ICMP_BUF(pkt)->code = 0; } -static inline void handle_ns_neighbor(struct net_buf *buf, +static inline void handle_ns_neighbor(struct net_pkt *pkt, struct net_icmpv6_nd_opt_hdr *hdr) { struct net_linkaddr lladdr = { @@ -1049,68 +1049,68 @@ static inline void handle_ns_neighbor(struct net_buf *buf, * 2 * 8 bytes - 2 - padding. * The formula above needs to be adjusted. */ - if (net_pkt_ll_src(buf)->len < lladdr.len) { - lladdr.len = net_pkt_ll_src(buf)->len; + if (net_pkt_ll_src(pkt)->len < lladdr.len) { + lladdr.len = net_pkt_ll_src(pkt)->len; } - nbr_add(buf, &lladdr, false, NET_IPV6_NBR_STATE_INCOMPLETE); + nbr_add(pkt, &lladdr, false, NET_IPV6_NBR_STATE_INCOMPLETE); } int net_ipv6_send_na(struct net_if *iface, struct in6_addr *src, struct in6_addr *dst, struct in6_addr *tgt, uint8_t flags) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint8_t llao_len; - 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); - NET_ASSERT_INFO(buf, "Out of TX buffers"); + NET_ASSERT_INFO(pkt, "Out of TX packets"); - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); NET_ASSERT_INFO(frag, "Out of DATA buffers"); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_pkt_set_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_ll_clear(buf); + net_pkt_ll_clear(pkt); llao_len = get_llao_len(iface); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ext_len(pkt, 0); - setup_headers(buf, sizeof(struct net_icmpv6_na_hdr) + llao_len, + setup_headers(pkt, sizeof(struct net_icmpv6_na_hdr) + llao_len, NET_ICMPV6_NA); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, src); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, dst); - net_ipaddr_copy(&NET_ICMPV6_NA_BUF(buf)->tgt, tgt); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, dst); + net_ipaddr_copy(&NET_ICMPV6_NA_BUF(pkt)->tgt, tgt); - set_llao(&net_pkt_iface(buf)->link_addr, - net_pkt_icmp_data(buf) + sizeof(struct net_icmp_hdr) + + set_llao(&net_pkt_iface(pkt)->link_addr, + net_pkt_icmp_data(pkt) + sizeof(struct net_icmp_hdr) + sizeof(struct net_icmpv6_na_hdr), llao_len, NET_ICMPV6_ND_OPT_TLLAO); - NET_ICMPV6_NA_BUF(buf)->flags = flags; + NET_ICMPV6_NA_BUF(pkt)->flags = flags; - net_pkt_set_len(buf->frags, NET_IPV6ICMPH_LEN + - sizeof(struct net_icmpv6_na_hdr) + - llao_len); + pkt->frags->len = NET_IPV6ICMPH_LEN + + sizeof(struct net_icmpv6_na_hdr) + llao_len; - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt); dbg_addr_sent_tgt("Neighbor Advertisement", - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst, - &NET_ICMPV6_NS_BUF(buf)->tgt); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst, + &NET_ICMPV6_NS_BUF(pkt)->tgt); - if (net_send_data(buf) < 0) { + if (net_send_data(pkt) < 0) { goto drop; } @@ -1119,15 +1119,15 @@ int net_ipv6_send_na(struct net_if *iface, struct in6_addr *src, return 0; drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); net_stats_update_ipv6_nd_drop(); return -EINVAL; } -static enum net_verdict handle_ns_input(struct net_buf *buf) +static enum net_verdict handle_ns_input(struct net_pkt *pkt) { - uint16_t total_len = net_buf_frags_len(buf); + uint16_t total_len = net_pkt_get_len(pkt); struct net_icmpv6_nd_opt_hdr *hdr; struct net_if_addr *ifaddr; uint8_t flags = 0, prev_opt_len = 0; @@ -1135,42 +1135,42 @@ static enum net_verdict handle_ns_input(struct net_buf *buf) size_t left_len; dbg_addr_recv_tgt("Neighbor Solicitation", - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst, - &NET_ICMPV6_NS_BUF(buf)->tgt); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst, + &NET_ICMPV6_NS_BUF(pkt)->tgt); net_stats_update_ipv6_nd_recv(); if ((total_len < (sizeof(struct net_ipv6_hdr) + sizeof(struct net_icmp_hdr) + sizeof(struct net_icmpv6_ns_hdr))) || - (NET_ICMP_BUF(buf)->code != 0) || - (NET_IPV6_BUF(buf)->hop_limit != NET_IPV6_ND_HOP_LIMIT) || - net_is_ipv6_addr_mcast(&NET_ICMPV6_NS_BUF(buf)->tgt)) { + (NET_ICMP_BUF(pkt)->code != 0) || + (NET_IPV6_BUF(pkt)->hop_limit != NET_IPV6_ND_HOP_LIMIT) || + net_is_ipv6_addr_mcast(&NET_ICMPV6_NS_BUF(pkt)->tgt)) { NET_DBG("Preliminary check failed %u/%zu, code %u, hop %u", total_len, (sizeof(struct net_ipv6_hdr) + sizeof(struct net_icmp_hdr) + sizeof(struct net_icmpv6_ns_hdr)), - NET_ICMP_BUF(buf)->code, NET_IPV6_BUF(buf)->hop_limit); + NET_ICMP_BUF(pkt)->code, NET_IPV6_BUF(pkt)->hop_limit); goto drop; } - net_pkt_set_ext_opt_len(buf, sizeof(struct net_icmpv6_ns_hdr)); - hdr = NET_ICMPV6_ND_OPT_HDR_BUF(buf); + net_pkt_set_ext_opt_len(pkt, sizeof(struct net_icmpv6_ns_hdr)); + hdr = NET_ICMPV6_ND_OPT_HDR_BUF(pkt); /* The parsing gets tricky if the ND struct is split * between two fragments. FIXME later. */ - if (buf->frags->len < ((uint8_t *)hdr - buf->frags->data)) { + if (pkt->frags->len < ((uint8_t *)hdr - pkt->frags->data)) { NET_DBG("NS struct split between fragments"); goto drop; } - left_len = buf->frags->len - (sizeof(struct net_ipv6_hdr) + + left_len = pkt->frags->len - (sizeof(struct net_ipv6_hdr) + sizeof(struct net_icmp_hdr)); - while (net_pkt_ext_opt_len(buf) < left_len && - left_len < buf->frags->len) { + while (net_pkt_ext_opt_len(pkt) < left_len && + left_len < pkt->frags->len) { if (!hdr->len) { break; @@ -1179,11 +1179,11 @@ static enum net_verdict handle_ns_input(struct net_buf *buf) switch (hdr->type) { case NET_ICMPV6_ND_OPT_SLLAO: if (net_is_ipv6_addr_unspecified( - &NET_IPV6_BUF(buf)->src)) { + &NET_IPV6_BUF(pkt)->src)) { goto drop; } - handle_ns_neighbor(buf, hdr); + handle_ns_neighbor(pkt, hdr); break; default: @@ -1191,87 +1191,87 @@ static enum net_verdict handle_ns_input(struct net_buf *buf) break; } - prev_opt_len = net_pkt_ext_opt_len(buf); + prev_opt_len = net_pkt_ext_opt_len(pkt); - net_pkt_set_ext_opt_len(buf, net_pkt_ext_opt_len(buf) + + net_pkt_set_ext_opt_len(pkt, net_pkt_ext_opt_len(pkt) + (hdr->len << 3)); - if (prev_opt_len == net_pkt_ext_opt_len(buf)) { + if (prev_opt_len == net_pkt_ext_opt_len(pkt)) { NET_ERR("Corrupted NS message"); goto drop; } - hdr = NET_ICMPV6_ND_OPT_HDR_BUF(buf); + hdr = NET_ICMPV6_ND_OPT_HDR_BUF(pkt); } - ifaddr = net_if_ipv6_addr_lookup_by_iface(net_pkt_iface(buf), - &NET_ICMPV6_NS_BUF(buf)->tgt); + ifaddr = net_if_ipv6_addr_lookup_by_iface(net_pkt_iface(pkt), + &NET_ICMPV6_NS_BUF(pkt)->tgt); if (!ifaddr) { NET_DBG("No such interface address %s", - net_sprint_ipv6_addr(&NET_ICMPV6_NS_BUF(buf)->tgt)); + net_sprint_ipv6_addr(&NET_ICMPV6_NS_BUF(pkt)->tgt)); goto drop; } #if !defined(CONFIG_NET_IPV6_DAD) - if (net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(buf)->src)) { + if (net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(pkt)->src)) { goto drop; } #else /* CONFIG_NET_IPV6_DAD */ /* Do DAD */ - if (net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(buf)->src)) { + if (net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(pkt)->src)) { - if (!net_is_ipv6_addr_solicited_node(&NET_IPV6_BUF(buf)->dst)) { + if (!net_is_ipv6_addr_solicited_node(&NET_IPV6_BUF(pkt)->dst)) { NET_DBG("Not solicited node addr %s", - net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst)); + net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst)); goto drop; } if (ifaddr->addr_state == NET_ADDR_TENTATIVE) { NET_DBG("DAD failed for %s iface %p", net_sprint_ipv6_addr(&ifaddr->address.in6_addr), - net_pkt_iface(buf)); + net_pkt_iface(pkt)); - dad_failed(net_pkt_iface(buf), + dad_failed(net_pkt_iface(pkt), &ifaddr->address.in6_addr); goto drop; } - /* We reuse the received buffer to send the NA */ - net_ipv6_addr_create_ll_allnodes_mcast(&NET_IPV6_BUF(buf)->dst); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, - net_if_ipv6_select_src_addr(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->dst)); + /* We reuse the received packet to send the NA */ + net_ipv6_addr_create_ll_allnodes_mcast(&NET_IPV6_BUF(pkt)->dst); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, + net_if_ipv6_select_src_addr(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->dst)); flags = NET_ICMPV6_NA_FLAG_OVERRIDE; goto send_na; } #endif /* CONFIG_NET_IPV6_DAD */ - if (net_is_my_ipv6_addr(&NET_IPV6_BUF(buf)->src)) { + if (net_is_my_ipv6_addr(&NET_IPV6_BUF(pkt)->src)) { NET_DBG("Duplicate IPv6 %s address", - net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src)); + net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src)); goto drop; } /* Address resolution */ - if (net_is_ipv6_addr_solicited_node(&NET_IPV6_BUF(buf)->dst)) { - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, - &NET_IPV6_BUF(buf)->src); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, - &NET_ICMPV6_NS_BUF(buf)->tgt); + if (net_is_ipv6_addr_solicited_node(&NET_IPV6_BUF(pkt)->dst)) { + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, + &NET_IPV6_BUF(pkt)->src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, + &NET_ICMPV6_NS_BUF(pkt)->tgt); flags = NET_ICMPV6_NA_FLAG_SOLICITED | NET_ICMPV6_NA_FLAG_OVERRIDE; goto send_na; } /* Neighbor Unreachability Detection (NUD) */ - if (net_if_ipv6_addr_lookup_by_iface(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->dst)) { - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, - &NET_IPV6_BUF(buf)->src); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, - &NET_ICMPV6_NS_BUF(buf)->tgt); + if (net_if_ipv6_addr_lookup_by_iface(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->dst)) { + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, + &NET_IPV6_BUF(pkt)->src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, + &NET_ICMPV6_NS_BUF(pkt)->tgt); flags = NET_ICMPV6_NA_FLAG_SOLICITED | NET_ICMPV6_NA_FLAG_OVERRIDE; goto send_na; @@ -1281,13 +1281,13 @@ static enum net_verdict handle_ns_input(struct net_buf *buf) } send_na: - ret = net_ipv6_send_na(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst, + ret = net_ipv6_send_na(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst, &ifaddr->address.in6_addr, flags); if (!ret) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } @@ -1400,23 +1400,23 @@ void net_ipv6_nbr_set_reachable_timer(struct net_if *iface, struct net_nbr *nbr) #endif /* CONFIG_NET_IPV6_ND */ #if defined(CONFIG_NET_IPV6_NBR_CACHE) -static inline bool handle_na_neighbor(struct net_buf *buf, +static inline bool handle_na_neighbor(struct net_pkt *pkt, struct net_icmpv6_nd_opt_hdr *hdr, uint8_t *tllao) { bool lladdr_changed = false; struct net_nbr *nbr; struct net_linkaddr_storage *cached_lladdr; - struct net_buf *pending; + struct net_pkt *pending; ARG_UNUSED(hdr); - nbr = nbr_lookup(&net_neighbor.table, net_pkt_iface(buf), - &NET_ICMPV6_NS_BUF(buf)->tgt); + nbr = nbr_lookup(&net_neighbor.table, net_pkt_iface(pkt), + &NET_ICMPV6_NS_BUF(pkt)->tgt); NET_DBG("Neighbor lookup %p iface %p addr %s", nbr, - net_pkt_iface(buf), - net_sprint_ipv6_addr(&NET_ICMPV6_NS_BUF(buf)->tgt)); + net_pkt_iface(pkt), + net_sprint_ipv6_addr(&NET_ICMPV6_NS_BUF(pkt)->tgt)); if (!nbr) { nbr_print(); @@ -1433,17 +1433,17 @@ static inline bool handle_na_neighbor(struct net_buf *buf, return false; } - lladdr.len = net_pkt_iface(buf)->link_addr.len; + lladdr.len = net_pkt_iface(pkt)->link_addr.len; lladdr.addr = &tllao[NET_ICMPV6_OPT_DATA_OFFSET]; - if (net_nbr_link(nbr, net_pkt_iface(buf), &lladdr)) { + if (net_nbr_link(nbr, net_pkt_iface(pkt), &lladdr)) { nbr_free(nbr); return false; } NET_DBG("[%d] nbr %p state %d IPv6 %s ll %s", nbr->idx, nbr, net_ipv6_nbr_data(nbr)->state, - net_sprint_ipv6_addr(&NET_ICMPV6_NS_BUF(buf)->tgt), + net_sprint_ipv6_addr(&NET_ICMPV6_NS_BUF(pkt)->tgt), net_sprint_ll_addr(lladdr.addr, lladdr.len)); } @@ -1469,14 +1469,14 @@ static inline bool handle_na_neighbor(struct net_buf *buf, dbg_update_neighbor_lladdr_raw( &tllao[NET_ICMPV6_OPT_DATA_OFFSET], cached_lladdr, - &NET_ICMPV6_NS_BUF(buf)->tgt); + &NET_ICMPV6_NS_BUF(pkt)->tgt); net_linkaddr_set(cached_lladdr, &tllao[NET_ICMPV6_OPT_DATA_OFFSET], cached_lladdr->len); } - if (net_is_solicited(buf)) { + if (net_is_solicited(pkt)) { ipv6_nbr_set_state(nbr, NET_IPV6_NBR_STATE_REACHABLE); net_ipv6_nbr_data(nbr)->ns_count = 0; @@ -1484,13 +1484,13 @@ static inline bool handle_na_neighbor(struct net_buf *buf, k_delayed_work_cancel( &net_ipv6_nbr_data(nbr)->reachable); - net_ipv6_nbr_set_reachable_timer(net_pkt_iface(buf), + net_ipv6_nbr_set_reachable_timer(net_pkt_iface(pkt), nbr); } else { ipv6_nbr_set_state(nbr, NET_IPV6_NBR_STATE_STALE); } - net_ipv6_nbr_data(nbr)->is_router = net_is_router(buf); + net_ipv6_nbr_data(nbr)->is_router = net_is_router(pkt); goto send_pending; } @@ -1498,7 +1498,7 @@ static inline bool handle_na_neighbor(struct net_buf *buf, /* We do not update the address if override bit is not set * and we have a valid address in the cache. */ - if (!net_is_override(buf) && lladdr_changed) { + if (!net_is_override(pkt) && lladdr_changed) { if (net_ipv6_nbr_data(nbr)->state == NET_IPV6_NBR_STATE_REACHABLE) { ipv6_nbr_set_state(nbr, NET_IPV6_NBR_STATE_STALE); @@ -1507,28 +1507,28 @@ static inline bool handle_na_neighbor(struct net_buf *buf, return false; } - if (net_is_override(buf) || - (!net_is_override(buf) && tllao && !lladdr_changed)) { + if (net_is_override(pkt) || + (!net_is_override(pkt) && tllao && !lladdr_changed)) { if (lladdr_changed) { dbg_update_neighbor_lladdr_raw( &tllao[NET_ICMPV6_OPT_DATA_OFFSET], cached_lladdr, - &NET_ICMPV6_NS_BUF(buf)->tgt); + &NET_ICMPV6_NS_BUF(pkt)->tgt); net_linkaddr_set(cached_lladdr, &tllao[NET_ICMPV6_OPT_DATA_OFFSET], cached_lladdr->len); } - if (net_is_solicited(buf)) { + if (net_is_solicited(pkt)) { ipv6_nbr_set_state(nbr, NET_IPV6_NBR_STATE_REACHABLE); /* We might have active timer from PROBE */ k_delayed_work_cancel( &net_ipv6_nbr_data(nbr)->reachable); - net_ipv6_nbr_set_reachable_timer(net_pkt_iface(buf), + net_ipv6_nbr_set_reachable_timer(net_pkt_iface(pkt), nbr); } else { if (lladdr_changed) { @@ -1538,14 +1538,14 @@ static inline bool handle_na_neighbor(struct net_buf *buf, } } - if (net_ipv6_nbr_data(nbr)->is_router && !net_is_router(buf)) { + if (net_ipv6_nbr_data(nbr)->is_router && !net_is_router(pkt)) { /* Update the routing if the peer is no longer * a router. */ /* FIXME */ } - net_ipv6_nbr_data(nbr)->is_router = net_is_router(buf); + net_ipv6_nbr_data(nbr)->is_router = net_is_router(pkt); send_pending: /* Next send any pending messages to the peer. */ @@ -1569,9 +1569,9 @@ send_pending: return true; } -static enum net_verdict handle_na_input(struct net_buf *buf) +static enum net_verdict handle_na_input(struct net_pkt *pkt) { - uint16_t total_len = net_buf_frags_len(buf); + uint16_t total_len = net_pkt_get_len(pkt); struct net_icmpv6_nd_opt_hdr *hdr; struct net_if_addr *ifaddr; uint8_t *tllao = NULL; @@ -1579,9 +1579,9 @@ static enum net_verdict handle_na_input(struct net_buf *buf) size_t left_len; dbg_addr_recv_tgt("Neighbor Advertisement", - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst, - &NET_ICMPV6_NS_BUF(buf)->tgt); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst, + &NET_ICMPV6_NS_BUF(pkt)->tgt); net_stats_update_ipv6_nd_recv(); @@ -1589,30 +1589,30 @@ static enum net_verdict handle_na_input(struct net_buf *buf) sizeof(struct net_icmp_hdr) + sizeof(struct net_icmpv6_na_hdr) + sizeof(struct net_icmpv6_nd_opt_hdr))) || - (NET_ICMP_BUF(buf)->code != 0) || - (NET_IPV6_BUF(buf)->hop_limit != NET_IPV6_ND_HOP_LIMIT) || - net_is_ipv6_addr_mcast(&NET_ICMPV6_NS_BUF(buf)->tgt) || - (net_is_solicited(buf) && - net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst))) { + (NET_ICMP_BUF(pkt)->code != 0) || + (NET_IPV6_BUF(pkt)->hop_limit != NET_IPV6_ND_HOP_LIMIT) || + net_is_ipv6_addr_mcast(&NET_ICMPV6_NS_BUF(pkt)->tgt) || + (net_is_solicited(pkt) && + net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst))) { goto drop; } - net_pkt_set_ext_opt_len(buf, sizeof(struct net_icmpv6_na_hdr)); - hdr = NET_ICMPV6_ND_OPT_HDR_BUF(buf); + net_pkt_set_ext_opt_len(pkt, sizeof(struct net_icmpv6_na_hdr)); + hdr = NET_ICMPV6_ND_OPT_HDR_BUF(pkt); /* The parsing gets tricky if the ND struct is split * between two fragments. FIXME later. */ - if (buf->frags->len < ((uint8_t *)hdr - buf->frags->data)) { + if (pkt->frags->len < ((uint8_t *)hdr - pkt->frags->data)) { NET_DBG("NA struct split between fragments"); goto drop; } - left_len = buf->frags->len - (sizeof(struct net_ipv6_hdr) + + left_len = pkt->frags->len - (sizeof(struct net_ipv6_hdr) + sizeof(struct net_icmp_hdr)); - while (net_pkt_ext_opt_len(buf) < left_len && - left_len < buf->frags->len) { + while (net_pkt_ext_opt_len(pkt) < left_len && + left_len < pkt->frags->len) { if (!hdr->len) { break; @@ -1628,41 +1628,41 @@ static enum net_verdict handle_na_input(struct net_buf *buf) break; } - prev_opt_len = net_pkt_ext_opt_len(buf); + prev_opt_len = net_pkt_ext_opt_len(pkt); - net_pkt_set_ext_opt_len(buf, net_pkt_ext_opt_len(buf) + + net_pkt_set_ext_opt_len(pkt, net_pkt_ext_opt_len(pkt) + (hdr->len << 3)); - if (prev_opt_len == net_pkt_ext_opt_len(buf)) { + if (prev_opt_len == net_pkt_ext_opt_len(pkt)) { NET_ERR("Corrupted NA message"); goto drop; } - hdr = NET_ICMPV6_ND_OPT_HDR_BUF(buf); + hdr = NET_ICMPV6_ND_OPT_HDR_BUF(pkt); } - ifaddr = net_if_ipv6_addr_lookup_by_iface(net_pkt_iface(buf), - &NET_ICMPV6_NA_BUF(buf)->tgt); + ifaddr = net_if_ipv6_addr_lookup_by_iface(net_pkt_iface(pkt), + &NET_ICMPV6_NA_BUF(pkt)->tgt); if (ifaddr) { NET_DBG("Interface %p already has address %s", - net_pkt_iface(buf), - net_sprint_ipv6_addr(&NET_ICMPV6_NA_BUF(buf)->tgt)); + net_pkt_iface(pkt), + net_sprint_ipv6_addr(&NET_ICMPV6_NA_BUF(pkt)->tgt)); #if defined(CONFIG_NET_IPV6_DAD) if (ifaddr->addr_state == NET_ADDR_TENTATIVE) { - dad_failed(net_pkt_iface(buf), - &NET_ICMPV6_NA_BUF(buf)->tgt); + dad_failed(net_pkt_iface(pkt), + &NET_ICMPV6_NA_BUF(pkt)->tgt); } #endif /* CONFIG_NET_IPV6_DAD */ goto drop; } - if (!handle_na_neighbor(buf, hdr, tllao)) { + if (!handle_na_neighbor(pkt, hdr, tllao)) { goto drop; } - net_pkt_unref(buf); + net_pkt_unref(pkt); net_stats_update_ipv6_nd_sent(); @@ -1675,55 +1675,56 @@ drop: } int net_ipv6_send_ns(struct net_if *iface, - struct net_buf *pending, + struct net_pkt *pending, struct in6_addr *src, struct in6_addr *dst, struct in6_addr *tgt, bool is_my_address) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_nbr *nbr; uint8_t llao_len; - 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); - NET_ASSERT_INFO(buf, "Out of TX buffers"); + NET_ASSERT_INFO(pkt, "Out of TX packets"); - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); NET_ASSERT_INFO(frag, "Out of DATA buffers"); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_pkt_set_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_ll_clear(buf); + net_pkt_ll_clear(pkt); - llao_len = get_llao_len(net_pkt_iface(buf)); + llao_len = get_llao_len(net_pkt_iface(pkt)); - setup_headers(buf, sizeof(struct net_icmpv6_ns_hdr) + llao_len, + setup_headers(pkt, sizeof(struct net_icmpv6_ns_hdr) + llao_len, NET_ICMPV6_NS); if (!dst) { net_ipv6_addr_create_solicited_node(tgt, - &NET_IPV6_BUF(buf)->dst); + &NET_IPV6_BUF(pkt)->dst); } else { - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, dst); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, dst); } - NET_ICMPV6_NS_BUF(buf)->reserved = 0; + NET_ICMPV6_NS_BUF(pkt)->reserved = 0; - net_ipaddr_copy(&NET_ICMPV6_NS_BUF(buf)->tgt, tgt); + net_ipaddr_copy(&NET_ICMPV6_NS_BUF(pkt)->tgt, tgt); if (is_my_address) { /* DAD */ - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, net_ipv6_unspecified_address()); - NET_IPV6_BUF(buf)->len[1] -= llao_len; + NET_IPV6_BUF(pkt)->len[1] -= llao_len; net_buf_add(frag, sizeof(struct net_ipv6_hdr) + @@ -1731,21 +1732,21 @@ int net_ipv6_send_ns(struct net_if *iface, sizeof(struct net_icmpv6_ns_hdr)); } else { if (src) { - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, src); } else { - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, net_if_ipv6_select_src_addr( - net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->dst)); + net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->dst)); } - if (net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(buf)->src)) { + if (net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(pkt)->src)) { NET_DBG("No source address for NS"); goto drop; } - set_llao(&net_pkt_iface(buf)->link_addr, - net_pkt_icmp_data(buf) + + set_llao(&net_pkt_iface(pkt)->link_addr, + net_pkt_icmp_data(pkt) + sizeof(struct net_icmp_hdr) + sizeof(struct net_icmpv6_ns_hdr), llao_len, NET_ICMPV6_ND_OPT_SLLAO); @@ -1756,21 +1757,21 @@ int net_ipv6_send_ns(struct net_if *iface, sizeof(struct net_icmpv6_ns_hdr) + llao_len); } - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt); - nbr = nbr_lookup(&net_neighbor.table, net_pkt_iface(buf), - &NET_ICMPV6_NS_BUF(buf)->tgt); + nbr = nbr_lookup(&net_neighbor.table, net_pkt_iface(pkt), + &NET_ICMPV6_NS_BUF(pkt)->tgt); if (!nbr) { nbr_print(); - nbr = nbr_new(net_pkt_iface(buf), - &NET_ICMPV6_NS_BUF(buf)->tgt, false, + nbr = nbr_new(net_pkt_iface(pkt), + &NET_ICMPV6_NS_BUF(pkt)->tgt, false, NET_IPV6_NBR_STATE_INCOMPLETE); if (!nbr) { NET_DBG("Could not create new neighbor %s", net_sprint_ipv6_addr( - &NET_ICMPV6_NS_BUF(buf)->tgt)); + &NET_ICMPV6_NS_BUF(pkt)->tgt)); goto drop; } } @@ -1779,9 +1780,9 @@ int net_ipv6_send_ns(struct net_if *iface, if (!net_ipv6_nbr_data(nbr)->pending) { net_ipv6_nbr_data(nbr)->pending = net_pkt_ref(pending); } else { - NET_DBG("Buffer %p already pending for " - "operation. Discarding pending %p and buf %p", - net_ipv6_nbr_data(nbr)->pending, pending, buf); + NET_DBG("Packet %p already pending for " + "operation. Discarding pending %p and pkt %p", + net_ipv6_nbr_data(nbr)->pending, pending, pkt); net_pkt_unref(pending); goto drop; } @@ -1793,12 +1794,12 @@ int net_ipv6_send_ns(struct net_if *iface, } dbg_addr_sent_tgt("Neighbor Solicitation", - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst, - &NET_ICMPV6_NS_BUF(buf)->tgt); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst, + &NET_ICMPV6_NS_BUF(pkt)->tgt); - if (net_send_data(buf) < 0) { - NET_DBG("Cannot send NS %p (pending %p)", buf, pending); + if (net_send_data(pkt) < 0) { + NET_DBG("Cannot send NS %p (pending %p)", pkt, pending); if (pending) { nbr_clear_ns_pending(net_ipv6_nbr_data(nbr)); @@ -1812,7 +1813,7 @@ int net_ipv6_send_ns(struct net_if *iface, return 0; drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); net_stats_update_ipv6_nd_drop(); return -EINVAL; @@ -1822,40 +1823,41 @@ drop: #if defined(CONFIG_NET_IPV6_ND) int net_ipv6_send_rs(struct net_if *iface) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; bool unspec_src; uint8_t llao_len = 0; - 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); - 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_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_ll_clear(buf); + net_pkt_ll_clear(pkt); - net_ipv6_addr_create_ll_allnodes_mcast(&NET_IPV6_BUF(buf)->dst); + net_ipv6_addr_create_ll_allnodes_mcast(&NET_IPV6_BUF(pkt)->dst); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, net_if_ipv6_select_src_addr(iface, - &NET_IPV6_BUF(buf)->dst)); + &NET_IPV6_BUF(pkt)->dst)); - unspec_src = net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(buf)->src); + unspec_src = net_is_ipv6_addr_unspecified(&NET_IPV6_BUF(pkt)->src); if (!unspec_src) { - llao_len = get_llao_len(net_pkt_iface(buf)); + llao_len = get_llao_len(net_pkt_iface(pkt)); } - setup_headers(buf, sizeof(struct net_icmpv6_rs_hdr) + llao_len, + setup_headers(pkt, sizeof(struct net_icmpv6_rs_hdr) + llao_len, NET_ICMPV6_RS); if (!unspec_src) { - set_llao(&net_pkt_iface(buf)->link_addr, - net_pkt_icmp_data(buf) + + set_llao(&net_pkt_iface(pkt)->link_addr, + net_pkt_icmp_data(pkt) + sizeof(struct net_icmp_hdr) + sizeof(struct net_icmpv6_rs_hdr), llao_len, NET_ICMPV6_ND_OPT_SLLAO); @@ -1870,14 +1872,14 @@ int net_ipv6_send_rs(struct net_if *iface) sizeof(struct net_icmpv6_rs_hdr)); } - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt); dbg_addr_sent("Router Solicitation", - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst); - if (net_send_data(buf) < 0) { + if (net_send_data(pkt) < 0) { goto drop; } @@ -1886,7 +1888,7 @@ int net_ipv6_send_rs(struct net_if *iface) return 0; drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); net_stats_update_ipv6_nd_drop(); return -EINVAL; @@ -1897,7 +1899,7 @@ int net_ipv6_start_rs(struct net_if *iface) return net_ipv6_send_rs(iface); } -static inline struct net_buf *handle_ra_neighbor(struct net_buf *buf, +static inline struct net_buf *handle_ra_neighbor(struct net_pkt *pkt, struct net_buf *frag, uint8_t len, uint16_t offset, uint16_t *pos, @@ -1915,34 +1917,34 @@ static inline struct net_buf *handle_ra_neighbor(struct net_buf *buf, llstorage.len = NET_LINK_ADDR_MAX_LENGTH; lladdr.len = NET_LINK_ADDR_MAX_LENGTH; lladdr.addr = llstorage.addr; - if (net_pkt_ll_src(buf)->len < lladdr.len) { - lladdr.len = net_pkt_ll_src(buf)->len; + if (net_pkt_ll_src(pkt)->len < lladdr.len) { + lladdr.len = net_pkt_ll_src(pkt)->len; } - frag = net_pkt_read(frag, offset, pos, lladdr.len, lladdr.addr); + frag = net_frag_read(frag, offset, pos, lladdr.len, lladdr.addr); if (!frag && offset) { return NULL; } padding = len * 8 - 2 - lladdr.len; if (padding) { - frag = net_pkt_read(frag, *pos, pos, padding, NULL); + frag = net_frag_read(frag, *pos, pos, padding, NULL); if (!frag && *pos) { return NULL; } } - *nbr = nbr_add(buf, &lladdr, true, NET_IPV6_NBR_STATE_STALE); + *nbr = nbr_add(pkt, &lladdr, true, NET_IPV6_NBR_STATE_STALE); return frag; } -static inline void handle_prefix_onlink(struct net_buf *buf, +static inline void handle_prefix_onlink(struct net_pkt *pkt, struct net_icmpv6_nd_opt_prefix_info *prefix_info) { struct net_if_ipv6_prefix *prefix; - prefix = net_if_ipv6_prefix_lookup(net_pkt_iface(buf), + prefix = net_if_ipv6_prefix_lookup(net_pkt_iface(pkt), &prefix_info->prefix, prefix_info->prefix_len); if (!prefix) { @@ -1950,13 +1952,13 @@ static inline void handle_prefix_onlink(struct net_buf *buf, return; } - prefix = net_if_ipv6_prefix_add(net_pkt_iface(buf), + prefix = net_if_ipv6_prefix_add(net_pkt_iface(pkt), &prefix_info->prefix, prefix_info->prefix_len, prefix_info->valid_lifetime); if (prefix) { NET_DBG("Interface %p add prefix %s/%d lifetime %u", - net_pkt_iface(buf), + net_pkt_iface(pkt), net_sprint_ipv6_addr(&prefix_info->prefix), prefix_info->prefix_len, prefix_info->valid_lifetime); @@ -1964,7 +1966,7 @@ static inline void handle_prefix_onlink(struct net_buf *buf, NET_ERR("Prefix %s/%d could not be added to iface %p", net_sprint_ipv6_addr(&prefix_info->prefix), prefix_info->prefix_len, - net_pkt_iface(buf)); + net_pkt_iface(pkt)); return; } @@ -1973,18 +1975,18 @@ static inline void handle_prefix_onlink(struct net_buf *buf, switch (prefix_info->valid_lifetime) { case 0: NET_DBG("Interface %p delete prefix %s/%d", - net_pkt_iface(buf), + net_pkt_iface(pkt), net_sprint_ipv6_addr(&prefix_info->prefix), prefix_info->prefix_len); - net_if_ipv6_prefix_rm(net_pkt_iface(buf), + net_if_ipv6_prefix_rm(net_pkt_iface(pkt), &prefix->prefix, prefix->len); break; case NET_IPV6_ND_INFINITE_LIFETIME: NET_DBG("Interface %p prefix %s/%d infinite", - net_pkt_iface(buf), + net_pkt_iface(pkt), net_sprint_ipv6_addr(&prefix->prefix), prefix->len); @@ -1993,7 +1995,7 @@ static inline void handle_prefix_onlink(struct net_buf *buf, default: NET_DBG("Interface %p update prefix %s/%u lifetime %u", - net_pkt_iface(buf), + net_pkt_iface(pkt), net_sprint_ipv6_addr(&prefix_info->prefix), prefix_info->prefix_len, prefix_info->valid_lifetime); @@ -2012,7 +2014,7 @@ static inline uint32_t remaining(struct k_delayed_work *work) return k_delayed_work_remaining_get(work) / MSEC_PER_SEC; } -static inline void handle_prefix_autonomous(struct net_buf *buf, +static inline void handle_prefix_autonomous(struct net_pkt *pkt, struct net_icmpv6_nd_opt_prefix_info *prefix_info) { struct in6_addr addr = { }; @@ -2023,7 +2025,7 @@ static inline void handle_prefix_autonomous(struct net_buf *buf, * bytes of that address. */ net_ipv6_addr_create_iid(&addr, - net_if_get_link_addr(net_pkt_iface(buf))); + net_if_get_link_addr(net_pkt_iface(pkt))); memcpy(&addr, &prefix_info->prefix, sizeof(struct in6_addr) / 2); ifaddr = net_if_ipv6_addr_lookup(&addr, NULL); @@ -2057,17 +2059,17 @@ static inline void handle_prefix_autonomous(struct net_buf *buf, } else { if (prefix_info->valid_lifetime == NET_IPV6_ND_INFINITE_LIFETIME) { - net_if_ipv6_addr_add(net_pkt_iface(buf), + net_if_ipv6_addr_add(net_pkt_iface(pkt), &addr, NET_ADDR_AUTOCONF, 0); } else { - net_if_ipv6_addr_add(net_pkt_iface(buf), + net_if_ipv6_addr_add(net_pkt_iface(pkt), &addr, NET_ADDR_AUTOCONF, prefix_info->valid_lifetime); } } } -static inline struct net_buf *handle_ra_prefix(struct net_buf *buf, +static inline struct net_buf *handle_ra_prefix(struct net_pkt *pkt, struct net_buf *frag, uint8_t len, uint16_t offset, uint16_t *pos) @@ -2077,14 +2079,14 @@ static inline struct net_buf *handle_ra_prefix(struct net_buf *buf, prefix_info.type = NET_ICMPV6_ND_OPT_PREFIX_INFO; prefix_info.len = len * 8 - 2; - frag = net_pkt_read(frag, offset, pos, 1, &prefix_info.prefix_len); - frag = net_pkt_read(frag, *pos, pos, 1, &prefix_info.flags); - frag = net_pkt_read_be32(frag, *pos, pos, &prefix_info.valid_lifetime); - frag = net_pkt_read_be32(frag, *pos, pos, + frag = net_frag_read(frag, offset, pos, 1, &prefix_info.prefix_len); + frag = net_frag_read(frag, *pos, pos, 1, &prefix_info.flags); + frag = net_frag_read_be32(frag, *pos, pos, &prefix_info.valid_lifetime); + frag = net_frag_read_be32(frag, *pos, pos, &prefix_info.preferred_lifetime); /* Skip reserved bytes */ - frag = net_pkt_skip(frag, *pos, pos, 4); - frag = net_pkt_read(frag, *pos, pos, sizeof(struct in6_addr), + frag = net_frag_skip(frag, *pos, pos, 4); + frag = net_frag_read(frag, *pos, pos, sizeof(struct in6_addr), prefix_info.prefix.s6_addr); if (!frag && *pos) { return NULL; @@ -2094,13 +2096,13 @@ static inline struct net_buf *handle_ra_prefix(struct net_buf *buf, !net_is_ipv6_ll_addr(&prefix_info.prefix)) { if (prefix_info.flags & NET_ICMPV6_RA_FLAG_ONLINK) { - handle_prefix_onlink(buf, &prefix_info); + handle_prefix_onlink(pkt, &prefix_info); } if ((prefix_info.flags & NET_ICMPV6_RA_FLAG_AUTONOMOUS) && prefix_info.valid_lifetime && (prefix_info.prefix_len == NET_IPV6_DEFAULT_PREFIX_LEN)) { - handle_prefix_autonomous(buf, &prefix_info); + handle_prefix_autonomous(pkt, &prefix_info); } } @@ -2109,7 +2111,7 @@ static inline struct net_buf *handle_ra_prefix(struct net_buf *buf, #if defined(CONFIG_NET_6LO_CONTEXT) /* 6lowpan Context Option RFC 6775, 4.2 */ -static inline struct net_buf *handle_ra_6co(struct net_buf *buf, +static inline struct net_buf *handle_ra_6co(struct net_pkt *pkt, struct net_buf *frag, uint8_t len, uint16_t offset, uint16_t *pos) @@ -2119,7 +2121,7 @@ static inline struct net_buf *handle_ra_6co(struct net_buf *buf, context.type = NET_ICMPV6_ND_OPT_6CO; context.len = len * 8 - 2; - frag = net_pkt_read_u8(frag, offset, pos, &context.context_len); + frag = net_frag_read_u8(frag, offset, pos, &context.context_len); /* RFC 6775, 4.2 * Context Length: 8-bit unsigned integer. The number of leading @@ -2135,23 +2137,23 @@ static inline struct net_buf *handle_ra_6co(struct net_buf *buf, } context.context_len = context.context_len / 8; - frag = net_pkt_read_u8(frag, *pos, pos, &context.flag); + frag = net_frag_read_u8(frag, *pos, pos, &context.flag); /* Skip reserved bytes */ - frag = net_pkt_skip(frag, *pos, pos, 2); - frag = net_pkt_read_be16(frag, *pos, pos, &context.lifetime); + frag = net_frag_skip(frag, *pos, pos, 2); + frag = net_frag_read_be16(frag, *pos, pos, &context.lifetime); /* RFC 6775, 4.2 (Length field). Length can be 2 or 3 depending * on the length of context prefix field. */ if (len == 3) { - frag = net_pkt_read(frag, *pos, pos, sizeof(struct in6_addr), + frag = net_frag_read(frag, *pos, pos, sizeof(struct in6_addr), context.prefix.s6_addr); } else if (len == 2) { /* If length is 2 means only 64 bits of context prefix * is available, rest set to zeros. */ - frag = net_pkt_read(frag, *pos, pos, 8, + frag = net_frag_read(frag, *pos, pos, 8, context.prefix.s6_addr); } @@ -2167,15 +2169,15 @@ static inline struct net_buf *handle_ra_6co(struct net_buf *buf, sizeof(struct in6_addr) - context.context_len); } - net_6lo_set_context(net_pkt_iface(buf), &context); + net_6lo_set_context(net_pkt_iface(pkt), &context); return frag; } #endif -static enum net_verdict handle_ra_input(struct net_buf *buf) +static enum net_verdict handle_ra_input(struct net_pkt *pkt) { - uint16_t total_len = net_buf_frags_len(buf); + uint16_t total_len = net_pkt_get_len(pkt); struct net_nbr *nbr = NULL; struct net_if_router *router; struct net_buf *frag; @@ -2189,8 +2191,8 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) uint32_t mtu; dbg_addr_recv("Router Advertisement", - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst); net_stats_update_ipv6_nd_recv(); @@ -2198,59 +2200,59 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) sizeof(struct net_icmp_hdr) + sizeof(struct net_icmpv6_ra_hdr) + sizeof(struct net_icmpv6_nd_opt_hdr))) || - (NET_ICMP_BUF(buf)->code != 0) || - (NET_IPV6_BUF(buf)->hop_limit != NET_IPV6_ND_HOP_LIMIT) || - !net_is_ipv6_ll_addr(&NET_IPV6_BUF(buf)->src)) { + (NET_ICMP_BUF(pkt)->code != 0) || + (NET_IPV6_BUF(pkt)->hop_limit != NET_IPV6_ND_HOP_LIMIT) || + !net_is_ipv6_ll_addr(&NET_IPV6_BUF(pkt)->src)) { goto drop; } - frag = buf->frags; - offset = sizeof(struct net_ipv6_hdr) + net_pkt_ext_len(buf) + + frag = pkt->frags; + offset = sizeof(struct net_ipv6_hdr) + net_pkt_ext_len(pkt) + sizeof(struct net_icmp_hdr); - frag = net_pkt_read_u8(frag, offset, &offset, &hop_limit); - frag = net_pkt_skip(frag, offset, &offset, 1); /* flags */ + frag = net_frag_read_u8(frag, offset, &offset, &hop_limit); + frag = net_frag_skip(frag, offset, &offset, 1); /* flags */ if (!frag) { goto drop; } if (hop_limit) { - net_ipv6_set_hop_limit(net_pkt_iface(buf), hop_limit); + net_ipv6_set_hop_limit(net_pkt_iface(pkt), hop_limit); NET_DBG("New hop limit %d", - net_if_ipv6_get_hop_limit(net_pkt_iface(buf))); + net_if_ipv6_get_hop_limit(net_pkt_iface(pkt))); } - frag = net_pkt_read_be16(frag, offset, &offset, &router_lifetime); - frag = net_pkt_read_be32(frag, offset, &offset, &reachable_time); - frag = net_pkt_read_be32(frag, offset, &offset, &retrans_timer); + frag = net_frag_read_be16(frag, offset, &offset, &router_lifetime); + frag = net_frag_read_be32(frag, offset, &offset, &reachable_time); + frag = net_frag_read_be32(frag, offset, &offset, &retrans_timer); if (!frag) { goto drop; } if (reachable_time && - (net_if_ipv6_get_reachable_time(net_pkt_iface(buf)) != - NET_ICMPV6_RA_BUF(buf)->reachable_time)) { - net_if_ipv6_set_base_reachable_time(net_pkt_iface(buf), + (net_if_ipv6_get_reachable_time(net_pkt_iface(pkt)) != + NET_ICMPV6_RA_BUF(pkt)->reachable_time)) { + net_if_ipv6_set_base_reachable_time(net_pkt_iface(pkt), reachable_time); - net_if_ipv6_set_reachable_time(net_pkt_iface(buf)); + net_if_ipv6_set_reachable_time(net_pkt_iface(pkt)); } if (retrans_timer) { - net_if_ipv6_set_retrans_timer(net_pkt_iface(buf), + net_if_ipv6_set_retrans_timer(net_pkt_iface(pkt), retrans_timer); } while (frag) { - frag = net_pkt_read(frag, offset, &offset, 1, &type); - frag = net_pkt_read(frag, offset, &offset, 1, &length); + frag = net_frag_read(frag, offset, &offset, 1, &type); + frag = net_frag_read(frag, offset, &offset, 1, &length); if (!frag) { goto drop; } switch (type) { case NET_ICMPV6_ND_OPT_SLLAO: - frag = handle_ra_neighbor(buf, frag, length, offset, + frag = handle_ra_neighbor(pkt, frag, length, offset, &offset, &nbr); if (!frag && offset) { goto drop; @@ -2259,13 +2261,13 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) break; case NET_ICMPV6_ND_OPT_MTU: /* MTU has reserved 2 bytes, so skip it. */ - frag = net_pkt_skip(frag, offset, &offset, 2); - frag = net_pkt_read_be32(frag, offset, &offset, &mtu); + frag = net_frag_skip(frag, offset, &offset, 2); + frag = net_frag_read_be32(frag, offset, &offset, &mtu); if (!frag && offset) { goto drop; } - net_if_set_mtu(net_pkt_iface(buf), mtu); + net_if_set_mtu(net_pkt_iface(pkt), mtu); if (mtu > 0xffff) { /* TODO: discard packet? */ @@ -2274,7 +2276,7 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) break; case NET_ICMPV6_ND_OPT_PREFIX_INFO: - frag = handle_ra_prefix(buf, frag, length, offset, + frag = handle_ra_prefix(pkt, frag, length, offset, &offset); if (!frag && offset) { goto drop; @@ -2289,7 +2291,7 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) goto drop; } - frag = handle_ra_6co(buf, frag, length, offset, + frag = handle_ra_6co(pkt, frag, length, offset, &offset); if (!frag && offset) { goto drop; @@ -2314,7 +2316,7 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) default: NET_DBG("Unknown ND option 0x%x", type); skip: - frag = net_pkt_skip(frag, offset, &offset, + frag = net_frag_skip(frag, offset, &offset, length * 8 - 2); if (!frag && offset) { goto drop; @@ -2324,8 +2326,8 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) } } - router = net_if_ipv6_router_lookup(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->src); + router = net_if_ipv6_router_lookup(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->src); if (router) { if (!router_lifetime) { /*TODO: Start rs_timer on iface if no routers @@ -2341,13 +2343,13 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) router_lifetime); } } else { - net_if_ipv6_router_add(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->src, + net_if_ipv6_router_add(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->src, router_lifetime); } if (nbr && net_ipv6_nbr_data(nbr)->pending) { - NET_DBG("Sending pending buf %p to %s", + NET_DBG("Sending pending pkt %p to %s", net_ipv6_nbr_data(nbr)->pending, net_sprint_ipv6_addr(&NET_IPV6_BUF( net_ipv6_nbr_data(nbr)->pending)->dst)); @@ -2360,9 +2362,9 @@ static enum net_verdict handle_ra_input(struct net_buf *buf) } /* Cancel the RS timer on iface */ - k_delayed_work_cancel(&net_pkt_iface(buf)->ipv6.rs_timer); + k_delayed_work_cancel(&net_pkt_iface(pkt)->ipv6.rs_timer); - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; @@ -2376,30 +2378,30 @@ drop: #if defined(CONFIG_NET_IPV6_MLD) #define MLDv2_LEN (2 + 1 + 1 + 2 + sizeof(struct in6_addr) * 2) -static struct net_buf *create_mldv2(struct net_buf *buf, +static struct net_pkt *create_mldv2(struct net_pkt *pkt, const struct in6_addr *addr, uint16_t record_type, uint8_t num_sources) { - net_pkt_append_u8(buf, record_type); - net_pkt_append_u8(buf, 0); /* aux data len */ - net_pkt_append_be16(buf, num_sources); /* number of addresses */ - net_pkt_append(buf, sizeof(struct in6_addr), addr->s6_addr, + net_pkt_append_u8(pkt, record_type); + net_pkt_append_u8(pkt, 0); /* aux data len */ + net_pkt_append_be16(pkt, num_sources); /* number of addresses */ + net_pkt_append(pkt, sizeof(struct in6_addr), addr->s6_addr, K_FOREVER); if (num_sources > 0) { /* All source addresses, RFC 3810 ch 3 */ - net_pkt_append(buf, sizeof(struct in6_addr), + net_pkt_append(pkt, sizeof(struct in6_addr), net_ipv6_unspecified_address()->s6_addr, K_FOREVER); } - return buf; + return pkt; } static int send_mldv2_raw(struct net_if *iface, struct net_buf *frags) { - struct net_buf *buf; + struct net_pkt *pkt; struct in6_addr dst; uint16_t pos; int ret; @@ -2407,57 +2409,57 @@ static int send_mldv2_raw(struct net_if *iface, struct net_buf *frags) /* Sent to all MLDv2-capable routers */ net_ipv6_addr_create(&dst, 0xff02, 0, 0, 0, 0, 0, 0, 0x0016); - buf = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, &dst), - K_FOREVER); + pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, &dst), + K_FOREVER); - buf = net_ipv6_create_raw(buf, + pkt = net_ipv6_create_raw(pkt, net_if_ipv6_select_src_addr(iface, &dst), &dst, iface, NET_IPV6_NEXTHDR_HBHO); - NET_IPV6_BUF(buf)->hop_limit = 1; /* RFC 3810 ch 7.4 */ + NET_IPV6_BUF(pkt)->hop_limit = 1; /* RFC 3810 ch 7.4 */ - net_pkt_set_ipv6_hdr_prev(buf, buf->len); + net_pkt_set_ipv6_hdr_prev(pkt, pkt->frags->len); /* Add hop-by-hop option and router alert option, RFC 3810 ch 5. */ - net_pkt_append_u8(buf, IPPROTO_ICMPV6); - net_pkt_append_u8(buf, 0); /* length (0 means 8 bytes) */ + net_pkt_append_u8(pkt, IPPROTO_ICMPV6); + net_pkt_append_u8(pkt, 0); /* length (0 means 8 bytes) */ #define ROUTER_ALERT_LEN 8 /* IPv6 router alert option is described in RFC 2711. */ - net_pkt_append_be16(buf, 0x0502); /* RFC 2711 ch 2.1 */ - net_pkt_append_be16(buf, 0); /* pkt contains MLD msg */ + net_pkt_append_be16(pkt, 0x0502); /* RFC 2711 ch 2.1 */ + net_pkt_append_be16(pkt, 0); /* pkt contains MLD msg */ - net_pkt_append_u8(buf, 0); /* padding */ - net_pkt_append_u8(buf, 0); /* padding */ + net_pkt_append_u8(pkt, 0); /* padding */ + net_pkt_append_u8(pkt, 0); /* padding */ /* ICMPv6 header */ - net_pkt_append_u8(buf, NET_ICMPV6_MLDv2); /* type */ - net_pkt_append_u8(buf, 0); /* code */ - net_pkt_append_be16(buf, 0); /* chksum */ + net_pkt_append_u8(pkt, NET_ICMPV6_MLDv2); /* type */ + net_pkt_append_u8(pkt, 0); /* code */ + net_pkt_append_be16(pkt, 0); /* chksum */ - net_pkt_set_len(buf->frags, NET_IPV6ICMPH_LEN + ROUTER_ALERT_LEN); - net_pkt_set_iface(buf, iface); + pkt->frags->len = NET_IPV6ICMPH_LEN + ROUTER_ALERT_LEN; + net_pkt_set_iface(pkt, iface); - net_pkt_append_be16(buf, 0); /* reserved field */ + net_pkt_append_be16(pkt, 0); /* reserved field */ /* Insert the actual multicast record(s) here */ - net_buf_frag_add(buf, frags); + net_pkt_frag_add(pkt, frags); - ret = net_ipv6_finalize_raw(buf, NET_IPV6_NEXTHDR_HBHO); + ret = net_ipv6_finalize_raw(pkt, NET_IPV6_NEXTHDR_HBHO); if (ret < 0) { goto drop; } - net_pkt_set_ext_len(buf, ROUTER_ALERT_LEN); + net_pkt_set_ext_len(pkt, ROUTER_ALERT_LEN); - net_pkt_write_be16(buf, buf->frags, - NET_IPV6H_LEN + ROUTER_ALERT_LEN + 2, - &pos, ntohs(~net_calc_chksum_icmpv6(buf))); + net_pkt_write_be16(pkt, pkt->frags, + NET_IPV6H_LEN + ROUTER_ALERT_LEN + 2, + &pos, ntohs(~net_calc_chksum_icmpv6(pkt))); - ret = net_send_data(buf); + ret = net_send_data(pkt); if (ret < 0) { goto drop; } @@ -2468,7 +2470,7 @@ static int send_mldv2_raw(struct net_if *iface, struct net_buf *frags) return 0; drop: - net_pkt_unref(buf); + net_pkt_unref(pkt); net_stats_update_icmp_drop(); net_stats_update_ipv6_mld_drop(); @@ -2478,21 +2480,21 @@ drop: static int send_mldv2(struct net_if *iface, const struct in6_addr *addr, uint8_t mode) { - struct net_buf *buf; + struct net_pkt *pkt; int ret; - 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); - net_pkt_append_be16(buf, 1); /* number of records */ + net_pkt_append_be16(pkt, 1); /* number of records */ - buf = create_mldv2(buf, addr, mode, 1); + pkt = create_mldv2(pkt, addr, mode, 1); - ret = send_mldv2_raw(iface, buf->frags); + ret = send_mldv2_raw(iface, pkt->frags); - buf->frags = NULL; + pkt->frags = NULL; - net_pkt_unref(buf); + net_pkt_unref(pkt); return ret; } @@ -2546,13 +2548,13 @@ int net_ipv6_mld_leave(struct net_if *iface, const struct in6_addr *addr) static void send_mld_report(struct net_if *iface) { - struct net_buf *buf; + struct net_pkt *pkt; int i, count = 0; - 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); - net_pkt_append_u8(buf, 0); /* This will be the record count */ + net_pkt_append_u8(pkt, 0); /* This will be the record count */ for (i = 0; i < NET_IF_MAX_IPV6_MADDR; i++) { if (!iface->ipv6.mcast[i].is_used || @@ -2560,7 +2562,7 @@ static void send_mld_report(struct net_if *iface) continue; } - buf = create_mldv2(buf, &iface->ipv6.mcast[i].address.in6_addr, + pkt = create_mldv2(pkt, &iface->ipv6.mcast[i].address.in6_addr, NET_IPV6_MLDv2_MODE_IS_EXCLUDE, 0); count++; } @@ -2569,53 +2571,53 @@ static void send_mld_report(struct net_if *iface) uint16_t pos; /* Write back the record count */ - net_pkt_write_u8(buf, buf->frags, 0, &pos, count); + net_pkt_write_u8(pkt, pkt->frags, 0, &pos, count); - send_mldv2_raw(iface, buf->frags); + send_mldv2_raw(iface, pkt->frags); - buf->frags = NULL; + pkt->frags = NULL; } - net_pkt_unref(buf); + net_pkt_unref(pkt); } -static enum net_verdict handle_mld_query(struct net_buf *buf) +static enum net_verdict handle_mld_query(struct net_pkt *pkt) { - uint16_t total_len = net_buf_frags_len(buf); + uint16_t total_len = net_pkt_get_len(pkt); struct in6_addr mcast; uint16_t max_rsp_code, num_src, pkt_len; uint16_t offset, pos; struct net_buf *frag; dbg_addr_recv("Multicast Listener Query", - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst); net_stats_update_ipv6_mld_recv(); /* offset tells now where the ICMPv6 header is starting */ - offset = net_pkt_icmp_data(buf) - net_pkt_ip_data(buf); + offset = net_pkt_icmp_data(pkt) - net_pkt_ip_data(pkt); offset += sizeof(struct net_icmp_hdr); - frag = net_pkt_read_be16(buf->frags, offset, &pos, &max_rsp_code); - frag = net_pkt_skip(frag, pos, &pos, 2); /* two reserved bytes */ - frag = net_pkt_read(frag, pos, &pos, sizeof(mcast), mcast.s6_addr); - frag = net_pkt_skip(frag, pos, &pos, 2); /* skip S, QRV & QQIC */ - frag = net_pkt_read_be16(buf->frags, pos, &pos, &num_src); + frag = net_frag_read_be16(pkt->frags, offset, &pos, &max_rsp_code); + frag = net_frag_skip(frag, pos, &pos, 2); /* two reserved bytes */ + frag = net_frag_read(frag, pos, &pos, sizeof(mcast), mcast.s6_addr); + frag = net_frag_skip(frag, pos, &pos, 2); /* skip S, QRV & QQIC */ + frag = net_frag_read_be16(pkt->frags, pos, &pos, &num_src); if (!frag && pos == 0xffff) { goto drop; } - pkt_len = sizeof(struct net_ipv6_hdr) + net_pkt_ext_len(buf) + + pkt_len = sizeof(struct net_ipv6_hdr) + net_pkt_ext_len(pkt) + sizeof(struct net_icmp_hdr) + (2 + 2 + 16 + 2 + 2) + sizeof(struct in6_addr) * num_src; if ((total_len < pkt_len || pkt_len > NET_IPV6_MTU || - (NET_ICMP_BUF(buf)->code != 0) || - (NET_IPV6_BUF(buf)->hop_limit != 1))) { + (NET_ICMP_BUF(pkt)->code != 0) || + (NET_IPV6_BUF(pkt)->hop_limit != 1))) { NET_DBG("Preliminary check failed %u/%u, code %u, hop %u", total_len, pkt_len, - NET_ICMP_BUF(buf)->code, NET_IPV6_BUF(buf)->hop_limit); + NET_ICMP_BUF(pkt)->code, NET_IPV6_BUF(pkt)->hop_limit); goto drop; } @@ -2626,7 +2628,7 @@ static enum net_verdict handle_mld_query(struct net_buf *buf) goto drop; } - send_mld_report(net_pkt_iface(buf)); + send_mld_report(net_pkt_iface(pkt)); drop: net_stats_update_ipv6_mld_drop(); @@ -2748,17 +2750,17 @@ static bool reassembly_cancel(uint32_t id, reassembly[i].id = 0; - for (j = 0; j < NET_IPV6_FRAGMENTS_MAX_BUF; j++) { - if (!reassembly[i].buf[j]) { + for (j = 0; j < NET_IPV6_FRAGMENTS_MAX_PKT; j++) { + if (!reassembly[i].pkt[j]) { continue; } - NET_DBG("IPv6 reassembly buf %p %zd bytes data", - reassembly[i].buf[j], - net_buf_frags_len(reassembly[i].buf[j])); + NET_DBG("IPv6 reassembly pkt %p %zd bytes data", + reassembly[i].pkt[j], + net_pkt_get_len(reassembly[i].pkt[j])); - net_pkt_unref(reassembly[i].buf[j]); - reassembly[i].buf[j] = NULL; + net_pkt_unref(reassembly[i].pkt[j]); + reassembly[i].pkt[j] = NULL; } return true; @@ -2774,8 +2776,8 @@ static void reassembly_info(char *str, struct net_ipv6_reassembly *reass) snprintk(out, sizeof(out), "%s", net_sprint_ipv6_addr(&reass->dst)); - for (i = 0, len = 0; i < NET_IPV6_FRAGMENTS_MAX_BUF; i++) { - len += net_buf_frags_len(reass->buf[i]); + for (i = 0, len = 0; i < NET_IPV6_FRAGMENTS_MAX_PKT; i++) { + len += net_pkt_get_len(reass->pkt[i]); } NET_DBG("%s id 0x%x src %s dst %s remain %d ms len %d", @@ -2795,94 +2797,95 @@ static void reassembly_timeout(struct k_work *work) static void reassemble_packet(struct net_ipv6_reassembly *reass) { - struct net_buf *buf, *last; + struct net_pkt *pkt; + struct net_buf *last; uint8_t next_hdr; int i, len; uint16_t pos; k_delayed_work_cancel(&reass->timer); - NET_ASSERT(reass->buf[0]); + NET_ASSERT(reass->pkt[0]); - last = net_buf_frag_last(reass->buf[0]->frags); + last = net_buf_frag_last(reass->pkt[0]->frags); /* We start from 2nd packet which is then appended to * the first one. */ - for (i = 1; i < NET_IPV6_FRAGMENTS_MAX_BUF; i++) { + for (i = 1; i < NET_IPV6_FRAGMENTS_MAX_PKT; i++) { int removed_len; - buf = reass->buf[i]; + pkt = reass->pkt[i]; /* Get rid of IPv6 and fragment header which are at * the beginning of the fragment. */ - removed_len = net_pkt_ipv6_fragment_start(buf) + + removed_len = net_pkt_ipv6_fragment_start(pkt) + sizeof(struct net_ipv6_frag_hdr) - - buf->frags->data; + pkt->frags->data; - NET_DBG("Removing %d bytes from start of buf %p", - removed_len, buf->frags); + NET_DBG("Removing %d bytes from start of pkt %p", + removed_len, pkt->frags); NET_ASSERT(removed_len >= (sizeof(struct net_ipv6_hdr) + sizeof(struct net_ipv6_frag_hdr))); - net_buf_pull(buf->frags, removed_len); + net_buf_pull(pkt->frags, removed_len); - /* Attach the data to previous buf */ - last->frags = buf->frags; - last = net_buf_frag_last(buf->frags); + /* Attach the data to previous pkt */ + last->frags = pkt->frags; + last = net_buf_frag_last(pkt->frags); - buf->frags = NULL; - reass->buf[i] = NULL; + pkt->frags = NULL; + reass->pkt[i] = NULL; - net_pkt_unref(buf); + net_pkt_unref(pkt); } - buf = reass->buf[0]; + pkt = reass->pkt[0]; /* Next we need to strip away the fragment header from the first packet - * and set the various pointers and values in buffer metadata. + * and set the various pointers and values in packet. */ - next_hdr = net_pkt_ipv6_fragment_start(buf)[0]; + next_hdr = net_pkt_ipv6_fragment_start(pkt)[0]; /* How much data we need to move in order to get rid of the * fragmentation header. */ - len = buf->frags->len - sizeof(struct net_ipv6_frag_hdr) - - (net_pkt_ipv6_fragment_start(buf) - buf->frags->data); + len = pkt->frags->len - sizeof(struct net_ipv6_frag_hdr) - + (net_pkt_ipv6_fragment_start(pkt) - pkt->frags->data); - memmove(net_pkt_ipv6_fragment_start(buf), - net_pkt_ipv6_fragment_start(buf) + + memmove(net_pkt_ipv6_fragment_start(pkt), + net_pkt_ipv6_fragment_start(pkt) + sizeof(struct net_ipv6_frag_hdr), len); /* This one updates the previous header's nexthdr value */ - net_pkt_write_u8(buf, buf->frags, net_pkt_ipv6_hdr_prev(buf), + net_pkt_write_u8(pkt, pkt->frags, net_pkt_ipv6_hdr_prev(pkt), &pos, next_hdr); - buf->frags->len -= sizeof(struct net_ipv6_frag_hdr); + pkt->frags->len -= sizeof(struct net_ipv6_frag_hdr); - if (!net_pkt_compact(buf)) { - NET_ERR("Cannot compact reassembly buffer %p", buf); + if (!net_pkt_compact(pkt)) { + NET_ERR("Cannot compact reassembly packet %p", pkt); reassembly_cancel(reass->id, &reass->src, &reass->dst); return; } /* Fix the total length of the IPv6 packet. */ - len = net_pkt_ext_len(buf); + len = net_pkt_ext_len(pkt); if (len > 0) { - NET_DBG("Old buf %p IPv6 ext len is %d bytes", buf, len); - net_pkt_set_ext_len(buf, + NET_DBG("Old pkt %p IPv6 ext len is %d bytes", pkt, len); + net_pkt_set_ext_len(pkt, len - sizeof(struct net_ipv6_frag_hdr)); } - len = net_buf_frags_len(buf) - sizeof(struct net_ipv6_hdr); + len = net_pkt_get_len(pkt) - sizeof(struct net_ipv6_hdr); - NET_IPV6_BUF(buf)->len[0] = len / 256; - NET_IPV6_BUF(buf)->len[1] = len - NET_IPV6_BUF(buf)->len[0] * 256; + NET_IPV6_BUF(pkt)->len[0] = len / 256; + NET_IPV6_BUF(pkt)->len[1] = len - NET_IPV6_BUF(pkt)->len[0] * 256; - NET_DBG("New buf %p IPv6 len is %d bytes", buf, len); + NET_DBG("New pkt %p IPv6 len is %d bytes", pkt, len); /* We need to use the queue when feeding the packet back into the * IP stack as we might run out of stack if we call processing_data() @@ -2890,16 +2893,16 @@ static void reassemble_packet(struct net_ipv6_reassembly *reass) * MUST NOT pass it to L2 so there will be a special check for that * in process_data() when handling the packet. */ - net_recv_data(net_pkt_iface(buf), buf); + net_recv_data(net_pkt_iface(pkt), pkt); /* Make room for new packet that can be reassembled */ k_delayed_work_cancel(&reass->timer); - /* We do not need to unref the net_buf as that will be handled + /* We do not need to unref the net_pkt as that will be handled * by the receiving code in upper part of the IP stack. */ - for (i = 0; i < NET_IPV6_FRAGMENTS_MAX_BUF; i++) { - reass->buf[i] = NULL; + for (i = 0; i < NET_IPV6_FRAGMENTS_MAX_PKT; i++) { + reass->pkt[i] = NULL; } } @@ -2923,19 +2926,19 @@ static bool fragment_verify(struct net_ipv6_reassembly *reass) uint16_t offset; int i, prev_len; - prev_len = net_buf_frags_len(reass->buf[0]); - offset = net_pkt_ipv6_fragment_offset(reass->buf[0]); + prev_len = net_pkt_get_len(reass->pkt[0]); + offset = net_pkt_ipv6_fragment_offset(reass->pkt[0]); - NET_DBG("buf %p offset %u", reass->buf[0], offset); + NET_DBG("pkt %p offset %u", reass->pkt[0], offset); if (offset != 0) { return false; } - for (i = 1; i < NET_IPV6_FRAGMENTS_MAX_BUF; i++) { - offset = net_pkt_ipv6_fragment_offset(reass->buf[i]); + for (i = 1; i < NET_IPV6_FRAGMENTS_MAX_PKT; i++) { + offset = net_pkt_ipv6_fragment_offset(reass->pkt[i]); - NET_DBG("buf %p offset %u prev_len %d", reass->buf[i], + NET_DBG("pkt %p offset %u prev_len %d", reass->pkt[i], offset, prev_len); if (prev_len < offset) { @@ -2943,13 +2946,13 @@ static bool fragment_verify(struct net_ipv6_reassembly *reass) return false; } - prev_len = net_buf_frags_len(reass->buf[i]); + prev_len = net_pkt_get_len(reass->pkt[i]); } return true; } -static enum net_verdict handle_fragment_hdr(struct net_buf *buf, +static enum net_verdict handle_fragment_hdr(struct net_pkt *pkt, struct net_buf *frag, int total_len, uint16_t buf_offset) @@ -2964,32 +2967,32 @@ static enum net_verdict handle_fragment_hdr(struct net_buf *buf, bool found; int i; - net_pkt_set_ipv6_fragment_start(buf, frag->data + buf_offset); + net_pkt_set_ipv6_fragment_start(pkt, frag->data + buf_offset); /* Each fragment has a fragment header. */ - frag = net_pkt_read_u8(frag, buf_offset, &loc, &nexthdr); - frag = net_pkt_skip(frag, loc, &loc, 1); /* reserved */ - frag = net_pkt_read_be16(frag, loc, &loc, &flag); - frag = net_pkt_read_be32(frag, loc, &loc, &id); + frag = net_frag_read_u8(frag, buf_offset, &loc, &nexthdr); + frag = net_frag_skip(frag, loc, &loc, 1); /* reserved */ + frag = net_frag_read_be16(frag, loc, &loc, &flag); + frag = net_frag_read_be32(frag, loc, &loc, &id); if (!frag && loc == 0xffff) { goto drop; } - reass = reassembly_get(id, &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst); + reass = reassembly_get(id, &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst); if (!reass) { - NET_DBG("Cannot get reassembly slot, dropping buf %p", buf); + NET_DBG("Cannot get reassembly slot, dropping pkt %p", pkt); goto drop; } offset = flag & 0xfff8; more = flag & 0x01; - net_pkt_set_ipv6_fragment_offset(buf, offset); + net_pkt_set_ipv6_fragment_offset(pkt, offset); - if (!reass->buf[0]) { - NET_DBG("Storing buf %p to slot %d", buf, 0); - reass->buf[0] = buf; + if (!reass->pkt[0]) { + NET_DBG("Storing pkt %p to slot %d", pkt, 0); + reass->pkt[0] = pkt; reassembly_info("Reassembly 1st pkt", reass); @@ -3000,39 +3003,39 @@ static enum net_verdict handle_fragment_hdr(struct net_buf *buf, /* The fragments might come in wrong order so place them * in reassembly chain in correct order. */ - for (i = 0, found = false; i < NET_IPV6_FRAGMENTS_MAX_BUF; i++) { + for (i = 0, found = false; i < NET_IPV6_FRAGMENTS_MAX_PKT; i++) { bool move_done = false; int j; - if (!reass->buf[i]) { - NET_DBG("Storing buf %p to slot %d", buf, i); - reass->buf[i] = buf; + if (!reass->pkt[i]) { + NET_DBG("Storing pkt %p to slot %d", pkt, i); + reass->pkt[i] = pkt; found = true; break; } - if (net_pkt_ipv6_fragment_offset(reass->buf[i]) < + if (net_pkt_ipv6_fragment_offset(reass->pkt[i]) < offset) { continue; } - for (j = i + 1; j < NET_IPV6_FRAGMENTS_MAX_BUF; j++) { - if (!reass->buf[j]) { - memmove(reass->buf[j], reass->buf[i], + for (j = i + 1; j < NET_IPV6_FRAGMENTS_MAX_PKT; j++) { + if (!reass->pkt[j]) { + memmove(reass->pkt[j], reass->pkt[i], sizeof(void *)); move_done = true; break; } } - /* If we do not have any free space in the buf array, + /* If we do not have any free space in the pkt array, * then the fragment needs to be discarded. */ if (!move_done) { break; } - reass->buf[i] = buf; + reass->pkt[i] = pkt; found = true; break; } @@ -3046,11 +3049,11 @@ static enum net_verdict handle_fragment_hdr(struct net_buf *buf, } if (more) { - if (net_buf_frags_len(buf) % 8) { + if (net_pkt_get_len(pkt) % 8) { /* Fragment length is not multiple of 8, discard * the packet and send parameter problem error. */ - net_icmpv6_send_error(buf, NET_ICMPV6_PARAM_PROBLEM, + net_icmpv6_send_error(pkt, NET_ICMPV6_PARAM_PROBLEM, NET_ICMPV6_PARAM_PROB_OPTION, 0); goto drop; } @@ -3081,7 +3084,7 @@ drop: } static int send_ipv6_fragment(struct net_if *iface, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *orig, struct net_buf *prev, struct net_buf *frag, @@ -3090,20 +3093,21 @@ static int send_ipv6_fragment(struct net_if *iface, int len, bool final) { - struct net_buf *ipv6, *rest = NULL, *end = NULL, *orig_copy = NULL; + struct net_pkt *ipv6; + struct net_buf *rest = NULL, *end = NULL, *orig_copy = NULL; struct net_ipv6_frag_hdr hdr; uint16_t pos, ext_len; uint8_t prev_hdr; int ret; - /* Prepare the head buf so that the IPv6 packet will be sent properly + /* Prepare the pkt so that the IPv6 packet will be sent properly * to the device driver. */ - if (net_pkt_context(buf)) { - ipv6 = net_pkt_get_tx(net_pkt_context(buf), FRAG_BUF_WAIT); + if (net_pkt_context(pkt)) { + ipv6 = net_pkt_get_tx(net_pkt_context(pkt), FRAG_BUF_WAIT); } else { ipv6 = net_pkt_get_reserve_tx( - net_if_get_ll_reserve(iface, &NET_IPV6_BUF(buf)->dst), + net_if_get_ll_reserve(iface, &NET_IPV6_BUF(pkt)->dst), FRAG_BUF_WAIT); } @@ -3120,10 +3124,10 @@ static int send_ipv6_fragment(struct net_if *iface, ipv6_len), "len %u, frag->len %d", len, frag->len); - ret = net_pkt_split(buf, frag, len, &end, &rest, + ret = net_pkt_split(pkt, frag, len, &end, &rest, FRAG_BUF_WAIT); if (ret < 0) { - goto free_bufs; + goto free_pkts; } } @@ -3138,17 +3142,17 @@ static int send_ipv6_fragment(struct net_if *iface, if (rest) { rest->frags = frag->frags; frag->frags = NULL; - net_pkt_unref(frag); + net_pkt_frag_unref(frag); } if (prev) { prev->frags = end; } else { - buf->frags = end; + pkt->frags = end; } end->frags = NULL; - net_pkt_copy_user_data(ipv6, buf); + memcpy(ipv6, pkt, sizeof(struct net_pkt)); /* Update the extension length metadata so that upper layer checksum * will be calculated properly by net_ipv6_finalize_raw(). @@ -3159,11 +3163,11 @@ static int send_ipv6_fragment(struct net_if *iface, orig_copy = net_buf_clone(orig, FRAG_BUF_WAIT); if (!orig_copy) { ret = -ENOMEM; - goto free_bufs; + goto free_pkts; } /* Then add the IPv6 header into the packet. */ - net_buf_frag_insert(ipv6, orig_copy); + net_pkt_frag_insert(ipv6, orig_copy); /* We need to fix the next header value so find out where * is the last IPv6 extension header. The returned value is offset @@ -3173,14 +3177,14 @@ static int send_ipv6_fragment(struct net_if *iface, prev_hdr = net_ipv6_find_last_ext_hdr(ipv6); if (prev_hdr < 0) { ret = -EINVAL; - goto free_bufs; + goto free_pkts; } /* We need to update the next header of the packet. */ - net_pkt_read_u8(ipv6->frags, prev_hdr, &pos, &hdr.nexthdr); + net_frag_read_u8(ipv6->frags, prev_hdr, &pos, &hdr.nexthdr); hdr.reserved = 0; - hdr.id = net_pkt_ipv6_fragment_id(buf); + hdr.id = net_pkt_ipv6_fragment_id(pkt); hdr.offset = htons((offset & 0xfff8) | final); /* And we need to update the last header in the IPv6 packet to point to @@ -3194,56 +3198,56 @@ static int send_ipv6_fragment(struct net_if *iface, FRAG_BUF_WAIT); if (!ret) { ret = -ENOMEM; - goto free_bufs; + goto free_pkts; } /* Tie all the fragments together to form an IPv6 packet. Then * update the length of the packet and optionally the checksum. */ - net_buf_frag_add(ipv6, buf->frags); + net_pkt_frag_add(ipv6, pkt->frags); ret = net_ipv6_finalize_raw(ipv6, hdr.nexthdr); if (ret < 0) { NET_DBG("Cannot create IPv6 packet (%d)", ret); - goto free_bufs; + goto free_pkts; } - NET_DBG("Sending fragment len %zd", net_buf_frags_len(ipv6)); + NET_DBG("Sending fragment len %zd", net_pkt_get_len(ipv6)); /* If everything has been ok so far, we can send the packet. * Note that we cannot send this re-constructed packet directly * as the link layer headers will not be properly set (because * we recreated the packet). So pass this packet back to TX - * so that the buf is going back to L2 for setup. + * so that the pkt is going back to L2 for setup. */ ret = net_send_data(ipv6); if (ret < 0) { - goto free_bufs; + goto free_pkts; } /* Then process the rest of the fragments */ - buf->frags = rest; + pkt->frags = rest; return 0; -free_bufs: +free_pkts: net_pkt_unref(ipv6); if (rest) { - net_pkt_unref(rest); + net_pkt_frag_unref(rest); } if (orig_copy) { - net_pkt_unref(orig_copy); + net_pkt_frag_unref(orig_copy); } return ret; } -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) { - struct net_buf *frag = buf->frags; + struct net_buf *frag = pkt->frags; struct net_buf *prev = NULL; struct net_buf *orig_ipv6, *rest; int curr_len = 0; @@ -3257,8 +3261,8 @@ int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_buf *buf, * header which is copied into each fragment together with * fragmentation header. */ - ret = net_pkt_split(buf, frag, - net_pkt_ip_hdr_len(buf) + net_pkt_ext_len(buf), + ret = net_pkt_split(pkt, frag, + net_pkt_ip_hdr_len(pkt) + net_pkt_ext_len(pkt), &orig_ipv6, &rest, FRAG_BUF_WAIT); if (ret < 0) { return -ENOMEM; @@ -3269,15 +3273,15 @@ int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_buf *buf, /* We do not need the first fragment any more. The "rest" will not * have IPv6 header but it will contain the rest of the original data. */ - rest->frags = buf->frags->frags; - buf->frags = rest; + rest->frags = pkt->frags->frags; + pkt->frags = rest; frag->frags = NULL; - net_pkt_unref(frag); + net_pkt_frag_unref(frag); - frag = buf->frags; + frag = pkt->frags; - net_pkt_set_ipv6_fragment_id(buf, id); + net_pkt_set_ipv6_fragment_id(pkt, id); /* Go through the fragment list, and create suitable IPv6 packet * from the data. @@ -3293,7 +3297,7 @@ int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_buf *buf, sizeof(struct net_ipv6_frag_hdr) - ipv6_len - (curr_len - frag->len); - status = send_ipv6_fragment(iface, buf, orig_ipv6, + status = send_ipv6_fragment(iface, pkt, orig_ipv6, prev, frag, ipv6_len, offset, fit_len, false); if (status < 0) { @@ -3303,7 +3307,7 @@ int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_buf *buf, offset += curr_len; prev = NULL; - frag = buf; + frag = pkt->frags; curr_len = 0; } @@ -3311,30 +3315,30 @@ int net_ipv6_send_fragmented_pkt(struct net_if *iface, struct net_buf *buf, frag = frag->frags; } - status = send_ipv6_fragment(iface, buf, orig_ipv6, prev, prev, + status = send_ipv6_fragment(iface, pkt, orig_ipv6, prev, prev, ipv6_len, offset, 0, true); - net_pkt_unref(buf); + net_pkt_unref(pkt); out: - net_pkt_unref(orig_ipv6); + net_pkt_frag_unref(orig_ipv6); return status; } #endif /* CONFIG_NET_IPV6_FRAGMENT */ -static inline enum net_verdict process_icmpv6_pkt(struct net_buf *buf, +static inline enum net_verdict process_icmpv6_pkt(struct net_pkt *pkt, struct net_ipv6_hdr *ipv6) { - struct net_icmp_hdr *hdr = NET_ICMP_BUF(buf); + struct net_icmp_hdr *hdr = NET_ICMP_BUF(pkt); NET_DBG("ICMPv6 %s received type %d code %d", net_icmpv6_type2str(hdr->type), hdr->type, hdr->code); - return net_icmpv6_input(buf, hdr->type, hdr->code); + return net_icmpv6_input(pkt, hdr->type, hdr->code); } -static inline struct net_buf *check_unknown_option(struct net_buf *buf, +static inline struct net_pkt *check_unknown_option(struct net_pkt *pkt, uint8_t opt_type, uint16_t length) { @@ -3360,21 +3364,21 @@ static inline struct net_buf *check_unknown_option(struct net_buf *buf, case 0x40: return NULL; case 0xc0: - if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { + if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) { return NULL; } /* passthrough */ case 0x80: - net_icmpv6_send_error(buf, NET_ICMPV6_PARAM_PROBLEM, + net_icmpv6_send_error(pkt, NET_ICMPV6_PARAM_PROBLEM, NET_ICMPV6_PARAM_PROB_OPTION, (uint32_t)length); return NULL; } - return buf; + return pkt; } -static inline struct net_buf *handle_ext_hdr_options(struct net_buf *buf, +static inline struct net_buf *handle_ext_hdr_options(struct net_pkt *pkt, struct net_buf *frag, int total_len, uint16_t len, @@ -3398,8 +3402,8 @@ static inline struct net_buf *handle_ext_hdr_options(struct net_buf *buf, length += 2; /* Each extension option has type and length */ - frag = net_pkt_read_u8(frag, offset, &loc, &opt_type); - frag = net_pkt_read_u8(frag, loc, &loc, &opt_len); + frag = net_frag_read_u8(frag, offset, &loc, &opt_type); + frag = net_frag_read_u8(frag, loc, &loc, &opt_len); if (!frag && loc == 0xffff) { goto drop; } @@ -3419,7 +3423,7 @@ static inline struct net_buf *handle_ext_hdr_options(struct net_buf *buf, #if defined(CONFIG_NET_RPL) case NET_IPV6_EXT_HDR_OPT_RPL: NET_DBG("Processing RPL option"); - frag = net_rpl_verify_header(buf, frag, loc, &loc, + frag = net_rpl_verify_header(pkt, frag, loc, &loc, &result); if (!result) { NET_DBG("RPL option error, packet dropped"); @@ -3434,7 +3438,7 @@ static inline struct net_buf *handle_ext_hdr_options(struct net_buf *buf, return frag; #endif default: - if (!check_unknown_option(buf, opt_type, length)) { + if (!check_unknown_option(pkt, opt_type, length)) { goto drop; } @@ -3452,8 +3456,8 @@ static inline struct net_buf *handle_ext_hdr_options(struct net_buf *buf, break; } - frag = net_pkt_read_u8(frag, loc, &loc, &opt_type); - frag = net_pkt_read_u8(frag, loc, &loc, &opt_len); + frag = net_frag_read_u8(frag, loc, &loc, &opt_type); + frag = net_frag_read_u8(frag, loc, &loc, &opt_len); if (!frag && loc == 0xffff) { goto drop; } @@ -3479,10 +3483,10 @@ static inline bool is_upper_layer_protocol_header(uint8_t proto) proto == IPPROTO_TCP); } -enum net_verdict net_ipv6_process_pkt(struct net_buf *buf) +enum net_verdict net_ipv6_process_pkt(struct net_pkt *pkt) { - struct net_ipv6_hdr *hdr = NET_IPV6_BUF(buf); - int real_len = net_buf_frags_len(buf); + struct net_ipv6_hdr *hdr = NET_IPV6_BUF(pkt); + int real_len = net_pkt_get_len(pkt); int pkt_len = (hdr->len[0] << 8) + hdr->len[1] + sizeof(*hdr); struct net_buf *frag; uint8_t start_of_ext, prev_hdr; @@ -3491,7 +3495,7 @@ enum net_verdict net_ipv6_process_pkt(struct net_buf *buf) uint16_t offset, total_len = 0; if (real_len != pkt_len) { - NET_DBG("IPv6 packet size %d buf len %d", pkt_len, real_len); + NET_DBG("IPv6 packet size %d pkt len %d", pkt_len, real_len); net_stats_update_ipv6_drop(); goto drop; } @@ -3522,18 +3526,18 @@ enum net_verdict net_ipv6_process_pkt(struct net_buf *buf) struct in6_addr *nexthop; /* Check if the packet can be routed */ - if (net_route_get_info(net_pkt_iface(buf), &hdr->dst, &route, + if (net_route_get_info(net_pkt_iface(pkt), &hdr->dst, &route, &nexthop)) { int ret; if (route) { - net_pkt_set_iface(buf, route->iface); + net_pkt_set_iface(pkt, route->iface); } - ret = net_route_packet(buf, nexthop); + ret = net_route_packet(pkt, nexthop); if (ret < 0) { - NET_DBG("Cannot re-route buf %p via %s (%d)", - buf, net_sprint_ipv6_addr(nexthop), + NET_DBG("Cannot re-route pkt %p via %s (%d)", + pkt, net_sprint_ipv6_addr(nexthop), ret); } else { return NET_OK; @@ -3542,7 +3546,7 @@ enum net_verdict net_ipv6_process_pkt(struct net_buf *buf) #endif /* CONFIG_NET_ROUTE */ { - NET_DBG("IPv6 packet in buf %p not for me", buf); + NET_DBG("IPv6 packet in pkt %p not for me", pkt); } net_stats_update_ipv6_drop(); @@ -3550,41 +3554,41 @@ enum net_verdict net_ipv6_process_pkt(struct net_buf *buf) } /* Check extension headers */ - net_pkt_set_next_hdr(buf, &hdr->nexthdr); - net_pkt_set_ext_len(buf, 0); - net_pkt_set_ext_bitmap(buf, 0); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_next_hdr(pkt, &hdr->nexthdr); + net_pkt_set_ext_len(pkt, 0); + net_pkt_set_ext_bitmap(pkt, 0); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); /* Fast path for main upper layer protocols. The handling of extension * headers can be slow so do this checking here. There cannot * be any extension headers after the upper layer protocol header. */ - next = *(net_pkt_next_hdr(buf)); + next = *(net_pkt_next_hdr(pkt)); if (is_upper_layer_protocol_header(next)) { goto upper_proto; } /* Go through the extensions */ - frag = buf->frags; + frag = pkt->frags; next = hdr->nexthdr; first_option = next; offset = sizeof(struct net_ipv6_hdr); - prev_hdr = &NET_IPV6_BUF(buf)->nexthdr - &NET_IPV6_BUF(buf)->vtc; + prev_hdr = &NET_IPV6_BUF(pkt)->nexthdr - &NET_IPV6_BUF(pkt)->vtc; while (frag) { enum net_verdict verdict; if (is_upper_layer_protocol_header(next)) { NET_DBG("IPv6 next header %d", next); - net_pkt_set_ext_len(buf, offset - - sizeof(struct net_ipv6_hdr)); + net_pkt_set_ext_len(pkt, offset - + sizeof(struct net_ipv6_hdr)); goto upper_proto; } start_of_ext = offset; - frag = net_pkt_read_u8(frag, offset, &offset, &next_hdr); - frag = net_pkt_read_u8(frag, offset, &offset, &length); + frag = net_frag_read_u8(frag, offset, &offset, &next_hdr); + frag = net_frag_read_u8(frag, offset, &offset, &length); if (!frag && offset == 0xffff) { goto drop; } @@ -3617,28 +3621,28 @@ enum net_verdict net_ipv6_process_pkt(struct net_buf *buf) } /* Hop by hop option */ - if (net_pkt_ext_bitmap(buf) & + if (net_pkt_ext_bitmap(pkt) & NET_IPV6_EXT_HDR_BITMAP_HBHO) { goto bad_hdr; } - net_pkt_add_ext_bitmap(buf, + net_pkt_add_ext_bitmap(pkt, NET_IPV6_EXT_HDR_BITMAP_HBHO); - frag = handle_ext_hdr_options(buf, frag, real_len, + frag = handle_ext_hdr_options(pkt, frag, real_len, length, offset, &offset, &verdict); break; #if defined(CONFIG_NET_IPV6_FRAGMENT) case NET_IPV6_NEXTHDR_FRAG: - net_pkt_set_ipv6_hdr_prev(buf, prev_hdr); + net_pkt_set_ipv6_hdr_prev(pkt, prev_hdr); /* The fragment header does not have length field so * we need to step back two bytes and start from the * beginning of the fragment header. */ - return handle_fragment_hdr(buf, frag, real_len, + return handle_fragment_hdr(pkt, frag, real_len, offset - 2); #endif default: @@ -3656,21 +3660,21 @@ enum net_verdict net_ipv6_process_pkt(struct net_buf *buf) upper_proto: if (total_len > 0) { NET_DBG("Extension len %d", total_len); - net_pkt_set_ext_len(buf, total_len); + net_pkt_set_ext_len(pkt, total_len); } switch (next) { case IPPROTO_ICMPV6: - return process_icmpv6_pkt(buf, hdr); + return process_icmpv6_pkt(pkt, hdr); case IPPROTO_UDP: #if defined(CONFIG_NET_UDP) - return net_conn_input(IPPROTO_UDP, buf); + return net_conn_input(IPPROTO_UDP, pkt); #else return NET_DROP; #endif case IPPROTO_TCP: #if defined(CONFIG_NET_TCP) - return net_conn_input(IPPROTO_TCP, buf); + return net_conn_input(IPPROTO_TCP, pkt); #else return NET_DROP; #endif @@ -3682,7 +3686,7 @@ drop: bad_hdr: /* Send error message about parameter problem (RFC 2460) */ - net_icmpv6_send_error(buf, NET_ICMPV6_PARAM_PROBLEM, + net_icmpv6_send_error(pkt, NET_ICMPV6_PARAM_PROBLEM, NET_ICMPV6_PARAM_PROB_NEXTHEADER, offset - 1); diff --git a/subsys/net/ip/ipv6.h b/subsys/net/ip/ipv6.h index 2515c9623bb..e6a8d09c6b2 100644 --- a/subsys/net/ip/ipv6.h +++ b/subsys/net/ip/ipv6.h @@ -78,8 +78,8 @@ const char *net_ipv6_nbr_state2str(enum net_ipv6_nbr_state state); * @brief IPv6 neighbor information. */ struct net_ipv6_nbr_data { - /** Any pending buffer waiting ND to finish. */ - struct net_buf *pending; + /** Any pending packet waiting ND to finish. */ + struct net_pkt *pending; /** IPv6 address. */ 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); #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 *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); /** - * @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 dst Destination IPv6 address * @param iface Network interface * @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 *dst, struct net_if *iface, 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 buf Network buffer + * @param pkt Network packet * @param src_addr Source address, or NULL to choose a default from context * @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_buf *buf, +struct net_pkt *net_ipv6_create(struct net_context *context, + struct net_pkt *pkt, const struct in6_addr *src_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 * 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. * * @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 @@ -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. * * @param context Network context for a connection - * @param buf Network buffer + * @param pkt Network packet * * @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) /** @@ -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 * 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 @@ -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); #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, @@ -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. * 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. */ struct net_ipv6_reassembly { @@ -390,7 +390,7 @@ struct net_ipv6_reassembly { struct k_delayed_work timer; /** 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 */ 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. * - * @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. */ -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) void net_ipv6_init(void); diff --git a/subsys/net/ip/l2/arp.c b/subsys/net/ip/l2/arp.c index 18aa08de721..20e232d98e8 100644 --- a/subsys/net/ip/l2/arp.c +++ b/subsys/net/ip/l2/arp.c @@ -24,7 +24,7 @@ struct arp_entry { uint32_t time; /* FIXME - implement timeout functionality */ struct net_if *iface; - struct net_buf *pending; + struct net_pkt *pending; struct in_addr ip; struct net_eth_addr eth; }; @@ -97,41 +97,42 @@ static inline struct in_addr *if_get_addr(struct net_if *iface) 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 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_eth_hdr *eth; struct in_addr *my_addr; - buf = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); + if (!pkt) { goto fail; } - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { goto fail; } - net_buf_frag_add(buf, frag); - net_pkt_set_iface(buf, iface); - net_pkt_set_family(buf, AF_INET); + net_pkt_frag_add(pkt, frag); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET); - hdr = NET_ARP_BUF(buf); - eth = NET_ETH_BUF(buf); + hdr = NET_ARP_BUF(pkt); + eth = NET_ETH_BUF(pkt); /* 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 * request and we want to send it again. */ if (entry) { 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); @@ -174,15 +175,15 @@ static inline struct net_buf *prepare_arp(struct net_if *iface, net_buf_add(frag, sizeof(struct net_arp_hdr)); - return buf; + return pkt; fail: - net_pkt_unref(buf); + net_pkt_unref(pkt); net_pkt_unref(pending); 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 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 in_addr *addr; - if (!buf || !buf->frags) { + if (!pkt || !pkt->frags) { 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. */ struct net_buf *header; 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 - - net_pkt_ll_reserve(buf)); + net_pkt_ll_reserve(pkt)); hdr->type = htons(NET_ETH_PTYPE_IP); - ll = net_pkt_ll_dst(buf); + ll = net_pkt_ll_dst(pkt); if (ll->addr) { memcpy(&hdr->dst.addr, ll->addr, sizeof(struct net_eth_addr)); } - ll = net_pkt_ll_src(buf); + ll = net_pkt_ll_src(pkt); if (ll->addr) { memcpy(&hdr->src.addr, ll->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 * the gateway address. */ - if (!net_if_ipv4_addr_mask_cmp(net_pkt_iface(buf), - &NET_IPV4_BUF(buf)->dst)) { - addr = &net_pkt_iface(buf)->ipv4.gw; + if (!net_if_ipv4_addr_mask_cmp(net_pkt_iface(pkt), + &NET_IPV4_BUF(pkt)->dst)) { + addr = &net_pkt_iface(pkt)->ipv4.gw; } else { - addr = &NET_IPV4_BUF(buf)->dst; + addr = &NET_IPV4_BUF(pkt)->dst; } /* If the destination address is already known, we do not need * to send any ARP packet. */ - entry = find_entry(net_pkt_iface(buf), + entry = find_entry(net_pkt_iface(pkt), addr, &free_entry, &non_pending); if (!entry) { if (!free_entry) { @@ -253,13 +254,13 @@ struct net_buf *net_arp_prepare(struct net_buf *buf) * pending query to this IP address, * so this packet must be discarded. */ - struct net_buf *req; + struct net_pkt *req; - req = prepare_arp(net_pkt_iface(buf), - addr, NULL, buf); + req = prepare_arp(net_pkt_iface(pkt), + addr, NULL, pkt); NET_DBG("Resending ARP %p", req); - net_pkt_unref(buf); + net_pkt_unref(pkt); return req; } @@ -267,16 +268,16 @@ struct net_buf *net_arp_prepare(struct net_buf *buf) 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); NET_DBG("ARP using ll %s for IP %s", 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) { /* If there is no room for link layer header, then * 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 - - net_pkt_ll_reserve(buf)); + net_pkt_ll_reserve(pkt)); hdr->type = htons(NET_ETH_PTYPE_IP); memcpy(&hdr->src.addr, ll->addr, @@ -298,25 +299,25 @@ struct net_buf *net_arp_prepare(struct net_buf *buf) 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_sprint_ipv4_addr(&NET_IPV4_BUF(pending)->dst), pending, pending->frags); - *buf = NULL; + *pkt = NULL; if (net_if_send_data(iface, pending) == NET_DROP) { /* This is to unref the original ref */ 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 * unref it now when it is removed from * 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, - struct net_buf *req) +static inline struct net_pkt *prepare_arp_reply(struct net_if *iface, + 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_eth_hdr *eth, *eth_query; - buf = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); + if (!pkt) { goto fail; } - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { goto fail; } - net_buf_frag_add(buf, frag); - net_pkt_set_iface(buf, iface); - net_pkt_set_family(buf, AF_INET); + net_pkt_frag_add(pkt, frag); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET); - hdr = NET_ARP_BUF(buf); - eth = NET_ETH_BUF(buf); + hdr = NET_ARP_BUF(pkt); + eth = NET_ETH_BUF(pkt); query = NET_ARP_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)); - return buf; + return pkt; fail: - net_pkt_unref(buf); + net_pkt_unref(pkt); 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_buf *reply; + struct net_pkt *reply; - if (net_buf_frags_len(buf) < (sizeof(struct net_arp_hdr) - - net_pkt_ll_reserve(buf))) { + if (net_pkt_get_len(pkt) < (sizeof(struct net_arp_hdr) - + net_pkt_ll_reserve(pkt))) { 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) - - net_pkt_ll_reserve(buf)); + net_pkt_ll_reserve(pkt)); return NET_DROP; } - arp_hdr = NET_ARP_BUF(buf); + arp_hdr = NET_ARP_BUF(pkt); switch (ntohs(arp_hdr->opcode)) { case NET_ARP_REQUEST: /* Someone wants to know our ll address */ 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 */ return NET_DROP; } @@ -461,7 +463,7 @@ enum net_verdict net_arp_input(struct net_buf *buf) #endif /* CONFIG_NET_DEBUG_ARP */ /* Send reply */ - reply = prepare_arp_reply(net_pkt_iface(buf), buf); + reply = prepare_arp_reply(net_pkt_iface(pkt), pkt); if (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: 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); } break; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } diff --git a/subsys/net/ip/l2/bluetooth.c b/subsys/net/ip/l2/bluetooth.c index a5efddd25b4..083cb825990 100644 --- a/subsys/net/ip/l2/bluetooth.c +++ b/subsys/net/ip/l2/bluetooth.c @@ -50,11 +50,11 @@ struct bt_context { 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"); 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; } -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; - 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 */ - if (net_pkt_family(buf) != AF_INET6) { + if (net_pkt_family(pkt) != AF_INET6) { return NET_DROP; } - if (!net_6lo_compress(buf, true, NULL)) { + if (!net_6lo_compress(pkt, true, NULL)) { NET_DBG("Packet compression failed"); return NET_DROP; } - net_if_queue_tx(ctxt->iface, buf); + net_if_queue_tx(ctxt->iface, pkt); 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) { 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_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); /* 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 * 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) { 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, }; -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; 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) { return ret; } diff --git a/subsys/net/ip/l2/dummy.c b/subsys/net/ip/l2/dummy.c index 59de85fe6dc..6b488e178f2 100644 --- a/subsys/net/ip/l2/dummy.c +++ b/subsys/net/ip/l2/dummy.c @@ -10,22 +10,22 @@ #include 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(buf)->len = 0; - net_pkt_ll_src(buf)->type = NET_LINK_DUMMY; - net_pkt_ll_dst(buf)->addr = NULL; - net_pkt_ll_dst(buf)->len = 0; - net_pkt_ll_dst(buf)->type = NET_LINK_DUMMY; + net_pkt_ll_src(pkt)->addr = NULL; + net_pkt_ll_src(pkt)->len = 0; + net_pkt_ll_src(pkt)->type = NET_LINK_DUMMY; + net_pkt_ll_dst(pkt)->addr = NULL; + net_pkt_ll_dst(pkt)->len = 0; + net_pkt_ll_dst(pkt)->type = NET_LINK_DUMMY; return NET_CONTINUE; } 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; } diff --git a/subsys/net/ip/l2/ethernet.c b/subsys/net/ip/l2/ethernet.c index 1a28ae981dc..9d945eddab3 100644 --- a/subsys/net/ip/l2/ethernet.c +++ b/subsys/net/ip/l2/ethernet.c @@ -32,16 +32,16 @@ const struct net_eth_addr *net_eth_broadcast_addr(void) } #if defined(CONFIG_NET_DEBUG_L2_ETHERNET) -#define print_ll_addrs(buf, type, len) \ +#define print_ll_addrs(pkt, type, len) \ do { \ char out[sizeof("xx:xx:xx:xx:xx:xx")]; \ \ 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))); \ \ 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)), \ type, (size_t)len); \ } while (0) @@ -50,7 +50,7 @@ const struct net_eth_addr *net_eth_broadcast_addr(void) #endif /* CONFIG_NET_DEBUG_L2_ETHERNET */ static inline void ethernet_update_length(struct net_if *iface, - struct net_buf *buf) + struct net_pkt *pkt) { 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. */ - if (net_pkt_family(buf) == AF_INET) { - len = ((NET_IPV4_BUF(buf)->len[0] << 8) + - NET_IPV4_BUF(buf)->len[1]); + if (net_pkt_family(pkt) == AF_INET) { + len = ((NET_IPV4_BUF(pkt)->len[0] << 8) + + NET_IPV4_BUF(pkt)->len[1]); } else { - len = ((NET_IPV6_BUF(buf)->len[0] << 8) + - NET_IPV6_BUF(buf)->len[1]) + + len = ((NET_IPV6_BUF(pkt)->len[0] << 8) + + NET_IPV6_BUF(pkt)->len[1]) + NET_IPV6H_LEN; } if (len < NET_ETH_MINIMAL_FRAME_SIZE - sizeof(struct net_eth_hdr)) { struct net_buf *frag; - for (frag = buf->frags; frag; frag = frag->frags) { + for (frag = pkt->frags; frag; frag = frag->frags) { if (frag->len < len) { len -= frag->len; } 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, - 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; sa_family_t family; switch (ntohs(hdr->type)) { case NET_ETH_PTYPE_IP: case NET_ETH_PTYPE_ARP: - net_pkt_set_family(buf, AF_INET); + net_pkt_set_family(pkt, AF_INET); family = AF_INET; break; case NET_ETH_PTYPE_IPV6: - net_pkt_set_family(buf, AF_INET6); + net_pkt_set_family(pkt, AF_INET6); family = AF_INET6; break; default: @@ -106,17 +106,17 @@ static enum net_verdict ethernet_recv(struct net_if *iface, } /* Set the pointers to ll src and dst addresses */ - lladdr = net_pkt_ll_src(buf); - lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(buf))->src.addr; + lladdr = net_pkt_ll_src(pkt); + lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(pkt))->src.addr; lladdr->len = sizeof(struct net_eth_addr); lladdr->type = NET_LINK_ETHERNET; - lladdr = net_pkt_ll_dst(buf); - lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(buf))->dst.addr; + lladdr = net_pkt_ll_dst(pkt); + lladdr->addr = ((struct net_eth_hdr *)net_pkt_ll(pkt))->dst.addr; lladdr->len = sizeof(struct net_eth_addr); 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) && !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; } - net_pkt_set_ll_reserve(buf, sizeof(struct net_eth_hdr)); - net_buf_pull(buf->frags, net_pkt_ll_reserve(buf)); + net_pkt_set_ll_reserve(pkt, sizeof(struct net_eth_hdr)); + net_buf_pull(pkt->frags, net_pkt_ll_reserve(pkt)); #ifdef CONFIG_NET_ARP if (family == AF_INET && hdr->type == htons(NET_ETH_PTYPE_ARP)) { NET_DBG("ARP packet from %s received", net_sprint_ll_addr((uint8_t *)hdr->src.addr, sizeof(struct net_eth_addr))); - return net_arp_input(buf); + return net_arp_input(pkt); } #endif - ethernet_update_length(iface, buf); + ethernet_update_length(iface, pkt); return NET_CONTINUE; } 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())) { /* Broadcast address */ - net_pkt_ll_dst(buf)->addr = (uint8_t *)broadcast_eth_addr.addr; - net_pkt_ll_dst(buf)->len = sizeof(struct net_eth_addr); - net_pkt_ll_src(buf)->addr = net_if_get_link_addr(iface)->addr; - net_pkt_ll_src(buf)->len = sizeof(struct net_eth_addr); + net_pkt_ll_dst(pkt)->addr = (uint8_t *)broadcast_eth_addr.addr; + net_pkt_ll_dst(pkt)->len = sizeof(struct net_eth_addr); + net_pkt_ll_src(pkt)->addr = net_if_get_link_addr(iface)->addr; + net_pkt_ll_src(pkt)->len = sizeof(struct net_eth_addr); 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 */ hdr->dst.addr[0] = 0x01; hdr->dst.addr[1] = 0x00; hdr->dst.addr[2] = 0x5e; - hdr->dst.addr[3] = NET_IPV4_BUF(buf)->dst.s4_addr[1]; - hdr->dst.addr[4] = NET_IPV4_BUF(buf)->dst.s4_addr[2]; - hdr->dst.addr[5] = NET_IPV4_BUF(buf)->dst.s4_addr[3]; + hdr->dst.addr[3] = NET_IPV4_BUF(pkt)->dst.s4_addr[1]; + hdr->dst.addr[4] = NET_IPV4_BUF(pkt)->dst.s4_addr[2]; + 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_src(buf)->addr = net_if_get_link_addr(iface)->addr; - net_pkt_ll_src(buf)->len = sizeof(struct net_eth_addr); + net_pkt_ll_dst(pkt)->len = sizeof(struct net_eth_addr); + net_pkt_ll_src(pkt)->addr = net_if_get_link_addr(iface)->addr; + net_pkt_ll_src(pkt)->len = sizeof(struct net_eth_addr); 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, - 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; uint16_t ptype; #ifdef CONFIG_NET_ARP - if (net_pkt_family(buf) == AF_INET) { - struct net_buf *arp_buf; + if (net_pkt_family(pkt) == AF_INET) { + 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; } - arp_buf = net_arp_prepare(buf); - if (!arp_buf) { + arp_pkt = net_arp_prepare(pkt); + if (!arp_pkt) { return NET_DROP; } - NET_DBG("Sending arp buf %p (orig %p) to iface %p", - arp_buf, buf, iface); + NET_DBG("Sending arp pkt %p (orig %p) to iface %p", + 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(buf)->len = sizeof(struct net_eth_addr); - net_pkt_ll_dst(buf)->addr = (uint8_t *)&NET_ETH_BUF(buf)->dst; - net_pkt_ll_dst(buf)->len = sizeof(struct net_eth_addr); + net_pkt_ll_src(pkt)->addr = (uint8_t *)&NET_ETH_BUF(pkt)->src; + net_pkt_ll_src(pkt)->len = sizeof(struct net_eth_addr); + net_pkt_ll_dst(pkt)->addr = (uint8_t *)&NET_ETH_BUF(pkt)->dst; + net_pkt_ll_dst(pkt)->len = sizeof(struct net_eth_addr); /* 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 @@ -217,7 +217,7 @@ static enum net_verdict ethernet_send(struct net_if *iface, goto send; } #else - NET_DBG("Sending buf %p to iface %p", buf, iface); + NET_DBG("Sending pkt %p to iface %p", pkt, iface); #endif /* 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. */ 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_pkt_ll_src(buf)->addr)) { - net_pkt_ll_src(buf)->addr = net_pkt_ll_if(buf)->addr; - net_pkt_ll_src(buf)->len = net_pkt_ll_if(buf)->len; + net_pkt_ll_src(pkt)->addr)) { + net_pkt_ll_src(pkt)->addr = net_pkt_ll_if(pkt)->addr; + net_pkt_ll_src(pkt)->len = net_pkt_ll_if(pkt)->len; } /* If the destination address is not set, then use broadcast * or multicast address. */ - if (!net_pkt_ll_dst(buf)->addr) { + if (!net_pkt_ll_dst(pkt)->addr) { #if defined(CONFIG_NET_IPV6) - if (net_pkt_family(buf) == AF_INET6 && - net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { - struct net_eth_addr *dst = &NET_ETH_BUF(buf)->dst; + if (net_pkt_family(pkt) == AF_INET6 && + net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) { + struct net_eth_addr *dst = &NET_ETH_BUF(pkt)->dst; memcpy(dst, (uint8_t *)multicast_eth_addr.addr, sizeof(struct net_eth_addr) - 4); 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); - net_pkt_ll_dst(buf)->addr = (uint8_t *)dst->addr; + net_pkt_ll_dst(pkt)->addr = (uint8_t *)dst->addr; } else #endif { - net_pkt_ll_dst(buf)->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_DBG("Destination address was not set, using %s", - net_sprint_ll_addr(net_pkt_ll_dst(buf)->addr, - net_pkt_ll_dst(buf)->len)); + net_sprint_ll_addr(net_pkt_ll_dst(pkt)->addr, + net_pkt_ll_dst(pkt)->len)); } setup_hdr: __unused; - if (net_pkt_family(buf) == AF_INET) { + if (net_pkt_family(pkt) == AF_INET) { ptype = htons(NET_ETH_PTYPE_IP); } else { ptype = htons(NET_ETH_PTYPE_IPV6); @@ -274,7 +274,7 @@ setup_hdr: /* Then go through the fragments and set the ethernet header. */ - frag = buf->frags; + frag = pkt->frags; NET_ASSERT_INFO(frag, "No data!"); @@ -282,13 +282,13 @@ setup_hdr: NET_ASSERT(net_buf_headroom(frag) > sizeof(struct net_eth_addr)); hdr = (struct net_eth_hdr *)(frag->data - - net_pkt_ll_reserve(buf)); - memcpy(&hdr->dst, net_pkt_ll_dst(buf)->addr, + net_pkt_ll_reserve(pkt)); + memcpy(&hdr->dst, net_pkt_ll_dst(pkt)->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)); hdr->type = ptype; - print_ll_addrs(buf, ntohs(hdr->type), frag->len); + print_ll_addrs(pkt, ntohs(hdr->type), frag->len); frag = frag->frags; } @@ -297,7 +297,7 @@ setup_hdr: send: #endif /* CONFIG_NET_ARP */ - net_if_queue_tx(iface, buf); + net_if_queue_tx(iface, pkt); return NET_OK; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154.c b/subsys/net/ip/l2/ieee802154/ieee802154.c index 69efd6ea5b3..f59869b5702 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154.c @@ -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; printk("IEEE 802.15.4 packet content:\n"); - frag = buf->frags; + frag = pkt->frags; while (frag) { hexdump(each_frag_reserve ? 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, struct ieee802154_mpdu *mpdu) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; if (!mpdu->mhr.fs->fc.ar) { return; } - buf = net_pkt_get_reserve_tx(IEEE802154_ACK_PKT_LENGTH, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(IEEE802154_ACK_PKT_LENGTH, K_FOREVER); + if (!pkt) { 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 = iface->dev->driver_api; 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; } @@ -119,7 +120,7 @@ static inline void ieee802154_acknowledge(struct net_if *iface, #define ieee802154_acknowledge(...) #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, 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 static inline -enum net_verdict ieee802154_manage_recv_buffer(struct net_if *iface, - struct net_buf *buf) +enum net_verdict ieee802154_manage_recv_packet(struct net_if *iface, + struct net_pkt *pkt) { enum net_verdict verdict = NET_CONTINUE; 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 * big endian format so we must swap it here. */ - if (net_pkt_ll_src(buf)->addr && - net_pkt_ll_src(buf)->len == IEEE802154_EXT_ADDR_LENGTH) { - sys_mem_swap(net_pkt_ll_src(buf)->addr, - net_pkt_ll_src(buf)->len); + if (net_pkt_ll_src(pkt)->addr && + net_pkt_ll_src(pkt)->len == IEEE802154_EXT_ADDR_LENGTH) { + sys_mem_swap(net_pkt_ll_src(pkt)->addr, + net_pkt_ll_src(pkt)->len); } - if (net_pkt_ll_dst(buf)->addr && - net_pkt_ll_dst(buf)->len == IEEE802154_EXT_ADDR_LENGTH) { - sys_mem_swap(net_pkt_ll_dst(buf)->addr, - net_pkt_ll_dst(buf)->len); + if (net_pkt_ll_dst(pkt)->addr && + net_pkt_ll_dst(pkt)->len == IEEE802154_EXT_ADDR_LENGTH) { + sys_mem_swap(net_pkt_ll_dst(pkt)->addr, + 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. */ - src = net_pkt_ll_src(buf)->addr ? - net_pkt_ll_src(buf)->addr - net_pkt_ll(buf) : 0; - dst = net_pkt_ll_dst(buf)->addr ? - net_pkt_ll_dst(buf)->addr - net_pkt_ll(buf) : 0; + src = net_pkt_ll_src(pkt)->addr ? + net_pkt_ll_src(pkt)->addr - net_pkt_ll(pkt) : 0; + dst = net_pkt_ll_dst(pkt)->addr ? + net_pkt_ll_dst(pkt)->addr - net_pkt_ll(pkt) : 0; #ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT - verdict = ieee802154_reassemble(buf); + verdict = ieee802154_reassemble(pkt); if (verdict == NET_DROP) { goto out; } #else - if (!net_6lo_uncompress(buf)) { + if (!net_6lo_uncompress(pkt)) { NET_DBG("Packet decompression failed"); verdict = NET_DROP; goto out; } #endif - net_pkt_ll_src(buf)->addr = src ? net_pkt_ll(buf) + src : NULL; - net_pkt_ll_dst(buf)->addr = dst ? net_pkt_ll(buf) + dst : NULL; + net_pkt_ll_src(pkt)->addr = src ? net_pkt_ll(pkt) + src : NULL; + net_pkt_ll_dst(pkt)->addr = dst ? net_pkt_ll(pkt) + dst : NULL; - pkt_hexdump(buf, false); + pkt_hexdump(pkt, false); out: return verdict; } -static inline bool ieee802154_manage_send_buffer(struct net_if *iface, - struct net_buf *buf) +static inline bool ieee802154_manage_send_packet(struct net_if *iface, + struct net_pkt *pkt) { bool ret; - pkt_hexdump(buf, false); + pkt_hexdump(pkt, false); #ifdef CONFIG_NET_L2_IEEE802154_FRAGMENT - ret = net_6lo_compress(buf, true, ieee802154_fragment); + ret = net_6lo_compress(pkt, true, ieee802154_fragment); #else - ret = net_6lo_compress(buf, true, NULL); + ret = net_6lo_compress(pkt, true, NULL); #endif - pkt_hexdump(buf, false); + pkt_hexdump(pkt, false); return ret; } #else /* CONFIG_NET_6LO */ -#define ieee802154_manage_recv_buffer(...) NET_CONTINUE -#define ieee802154_manage_send_buffer(...) true +#define ieee802154_manage_recv_packet(...) NET_CONTINUE +#define ieee802154_manage_send_packet(...) true #endif /* CONFIG_NET_6LO */ static enum net_verdict ieee802154_recv(struct net_if *iface, - struct net_buf *buf) + struct net_pkt *pkt) { struct ieee802154_mpdu mpdu; - if (!ieee802154_validate_frame(net_pkt_ll(buf), - net_buf_frags_len(buf), &mpdu)) { + if (!ieee802154_validate_frame(net_pkt_ll(pkt), + net_pkt_get_len(pkt), &mpdu)) { return NET_DROP; } @@ -247,40 +248,40 @@ static enum net_verdict ieee802154_recv(struct net_if *iface, ieee802154_acknowledge(iface, &mpdu); - net_pkt_set_ll_reserve(buf, mpdu.payload - (void *)net_pkt_ll(buf)); - net_buf_pull(buf->frags, net_pkt_ll_reserve(buf)); + net_pkt_set_ll_reserve(pkt, mpdu.payload - (void *)net_pkt_ll(pkt)); + 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); - 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); - if (!ieee802154_decipher_data_frame(iface, buf, &mpdu)) { + if (!ieee802154_decipher_data_frame(iface, pkt, &mpdu)) { 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, - struct net_buf *buf) + struct net_pkt *pkt) { 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; - if (net_pkt_family(buf) != AF_INET6) { + if (net_pkt_family(pkt) != AF_INET6) { return NET_DROP; } - if (!ieee802154_manage_send_buffer(iface, buf)) { + if (!ieee802154_manage_send_packet(iface, pkt)) { return NET_DROP; } - frag = buf->frags; + frag = pkt->frags; while (frag) { if (frag->len > IEEE802154_MTU) { 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; } - 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)) { return NET_DROP; } @@ -296,9 +297,9 @@ static enum net_verdict ieee802154_send(struct net_if *iface, 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; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_fragment.c b/subsys/net/ip/l2/ieee802154/ieee802154_fragment.c index 5deaf284adc..2554a29b6d1 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_fragment.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_fragment.c @@ -37,7 +37,7 @@ static uint16_t datagram_tag; */ struct frag_cache { 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 tag; /* Datagram tag */ 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) { struct net_buf *frag; - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { 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_frag_add(buf, frag); + net_pkt_frag_add(pkt, 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, uint8_t offset) { 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; 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 | * * 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). * * 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 - * 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 *next; @@ -233,17 +233,17 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff) uint8_t max; bool first; - if (!buf || !buf->frags) { + if (!pkt || !pkt->frags) { return false; } /* If it is a single fragment do not add fragmentation header */ - if (!buf->frags->frags) { + if (!pkt->frags->frags) { return true; } /* Datagram_size: total length before compression */ - size = net_buf_frags_len(buf) + hdr_diff; + size = net_pkt_get_len(pkt) + hdr_diff; room = 0; offset = 0; @@ -251,8 +251,8 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff) first = true; datagram_tag++; - next = buf->frags; - buf->frags = NULL; + next = pkt->frags; + pkt->frags = NULL; /* First fragment has compressed header, but SIZE and OFFSET * values in fragmentation header are based on uncompressed @@ -261,7 +261,7 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff) while (1) { if (!room) { /* Prepare new fragment based on offset */ - frag = prepare_new_fragment(buf, offset); + frag = prepare_new_fragment(pkt, offset); if (!frag) { return false; } @@ -270,7 +270,7 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff) set_up_frag_hdr(frag, size, offset); /* 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 */ processed += max; @@ -286,7 +286,7 @@ bool ieee802154_fragment(struct net_buf *buf, int hdr_diff) compact_frag(next, move); if (!next->len) { - next = net_pkt_frag_del(NULL, next); + next = net_pkt_frag_del(pkt, NULL, next); if (!next) { break; } @@ -312,15 +312,15 @@ static inline void remove_frag_header(struct net_buf *frag, uint8_t 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(buf)->len[1] = (uint8_t) (size - NET_IPV6H_LEN); + NET_IPV6_BUF(pkt)->len[0] = (size - NET_IPV6H_LEN) >> 8; + NET_IPV6_BUF(pkt)->len[1] = (uint8_t) (size - NET_IPV6H_LEN); - if (NET_IPV6_BUF(buf)->nexthdr == IPPROTO_UDP) { - NET_UDP_BUF(buf)->len = htons(size - NET_IPV6H_LEN); + if (NET_IPV6_BUF(pkt)->nexthdr == IPPROTO_UDP) { + 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; } - if (cache[i].buf) { - net_pkt_unref(cache[i].buf); + if (cache[i].pkt) { + net_pkt_unref(cache[i].pkt); } - cache[i].buf = NULL; + cache[i].pkt = NULL; cache[i].size = 0; cache[i].tag = 0; 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); - if (cache->buf) { - net_pkt_unref(cache->buf); + if (cache->pkt) { + net_pkt_unref(cache->pkt); } - cache->buf = NULL; + cache->pkt = NULL; cache->size = 0; cache->tag = 0; 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 * 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) { int i; @@ -378,7 +378,7 @@ static inline struct frag_cache *set_reass_cache(struct net_buf *buf, continue; } - cache[i].buf = buf; + cache[i].pkt = pkt; cache[i].size = size; cache[i].tag = tag; cache[i].used = true; @@ -411,8 +411,8 @@ static inline struct frag_cache *get_reass_cache(uint16_t size, uint16_t tag) return NULL; } -/* Helper function to write fragment data to Rx buffer based on offset. */ -static inline bool copy_frag(struct net_buf *buf, +/* Helper function to write fragment data to Rx packet based on offset. */ +static inline bool copy_frag(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset) { @@ -420,17 +420,17 @@ static inline bool copy_frag(struct net_buf *buf, struct net_buf *write; uint16_t pos = offset; - write = buf->frags; + write = pkt->frags; 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); if (!write && pos == 0xffff) { /* Free the new bufs we tried to get, we need to discard * the whole fragment chain. */ - net_pkt_unref(buf->frags); - buf->frags = NULL; + net_pkt_frag_unref(pkt->frags); + pkt->frags = NULL; return false; } @@ -438,7 +438,7 @@ static inline bool copy_frag(struct net_buf *buf, input = input->frags; } - net_pkt_unref(frag); + net_pkt_frag_unref(frag); 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. * Cache Rx part of fragment along with data buf for the first fragment * 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) { 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; /* 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; /* 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; if (!first) { - offset = ((uint16_t)buf->frags->data[pos]) << 3; + offset = ((uint16_t)pkt->frags->data[pos]) << 3; pos++; } /* Remove frag header and update data */ - remove_frag_header(buf->frags, pos); + remove_frag_header(pkt->frags, pos); /* 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"); 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 - * 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. * (Detach data fragment from incoming Rx and copy that data). */ - frag = buf->frags; - buf->frags = NULL; + frag = pkt->frags; + pkt->frags = NULL; cache = get_reass_cache(size, tag); if (!cache) { - cache = set_reass_cache(buf, size, tag); + cache = set_reass_cache(pkt, size, tag); if (!cache) { NET_ERR("Could not get a cache entry"); - buf->frags = frag; + pkt->frags = frag; 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. */ - if (!copy_frag(cache->buf, frag, offset)) { - buf->frags = frag; + if (!copy_frag(cache->pkt, frag, offset)) { + pkt->frags = frag; /* Initialize to NULL to prevent duble free. It's only * needed here because this is the first fragment. */ - cache->buf = NULL; + cache->pkt = NULL; 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; } - NET_DBG("buffer inserted into cache"); + NET_DBG("packet inserted into cache"); return NET_OK; } - /* Add data buffer to reassembly buffer */ - if (!copy_frag(cache->buf, frag, offset)) { - buf->frags = frag; + /* Add data packet to reassembly packet */ + if (!copy_frag(cache->pkt, frag, offset)) { + pkt->frags = frag; 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 */ - if (net_buf_frags_len(cache->buf->frags) == size) { - /* Assign frags back to input buffer. */ - buf->frags = cache->buf->frags; - cache->buf->frags = NULL; + if (net_pkt_get_len(cache->pkt) == size) { + /* Assign frags back to input packet. */ + pkt->frags = cache->pkt->frags; + cache->pkt->frags = NULL; /* 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. */ 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; } - /* Unref Rx part of original buffer */ - net_pkt_unref(buf); + /* Unref Rx part of original packet */ + net_pkt_unref(pkt); 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"); return NET_DROP; } - switch (buf->frags->data[0] & 0xF0) { + switch (pkt->frags->data[0] & 0xF0) { case NET_6LO_DISPATCH_FRAG1: /* First fragment with IP headers */ - return add_frag_to_cache(buf, true); + return add_frag_to_cache(pkt, true); case NET_6LO_DISPATCH_FRAGN: /* Further fragments */ - return add_frag_to_cache(buf, false); + return add_frag_to_cache(pkt, false); 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 */ - if (net_6lo_uncompress(buf)) { + if (net_6lo_uncompress(pkt)) { return NET_CONTINUE; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_fragment.h b/subsys/net/ip/l2/ieee802154/ieee802154_fragment.h index 11f33e95082..9318f9dedcd 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_fragment.h +++ b/subsys/net/ip/l2/ieee802154/ieee802154_fragment.h @@ -25,12 +25,12 @@ * needs to be fragmented. Every fragment will have fragmentation header * 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 * * @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 @@ -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 * 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_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__ */ diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_frame.c b/subsys/net/ip/l2/ieee802154/ieee802154_frame.c index 5f2f81b6300..8f8e44a9a9e 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_frame.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_frame.c @@ -472,7 +472,7 @@ uint16_t ieee802154_compute_header_size(struct net_if *iface, 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 * 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 @@ -808,27 +808,28 @@ static inline uint8_t mac_command_length(enum ieee802154_cfi cfi) return reserve; } -struct net_buf * +struct net_pkt * ieee802154_create_mac_cmd_frame(struct ieee802154_context *ctx, enum ieee802154_cfi type, struct ieee802154_frame_params *params) { struct ieee802154_fcf_seq *fs; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint8_t *p_buf; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); + if (!pkt) { return NULL; } - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { goto error; } - net_buf_frag_add(buf, frag); - p_buf = net_pkt_ll(buf); + net_pkt_frag_add(pkt, frag); + p_buf = net_pkt_ll(pkt); fs = generate_fcf_grounds(&p_buf, 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 * command space, in order to fill-in its content. */ - net_pkt_set_ll_reserve(buf, p_buf - net_pkt_ll(buf)); - net_buf_pull(frag, net_pkt_ll_reserve(buf)); + net_pkt_set_ll_reserve(pkt, p_buf - net_pkt_ll(pkt)); + net_buf_pull(frag, net_pkt_ll_reserve(pkt)); /* Thus setting the right MAC command length * Now up to the caller to fill-in this space relevantly. * See ieee802154_mac_command() helper. */ - net_pkt_set_len(frag, mac_command_length(type)); + frag->len = mac_command_length(type); dbg_print_fs(fs); - return buf; + return pkt; error: - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } @@ -872,9 +873,9 @@ error: #ifdef CONFIG_NET_L2_IEEE802154_ACK_REPLY 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; if (!p_buf) { @@ -894,7 +895,7 @@ bool ieee802154_create_ack_frame(struct net_if *iface, #endif /* CONFIG_NET_L2_IEEE802154_ACK_REPLY */ #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_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 * in order to get the extended address related to it */ - if (!ieee802154_decrypt_auth(&ctx->sec_ctx, net_pkt_ll(buf), - net_pkt_ll_reserve(buf), - net_buf_frags_len(buf), - net_pkt_ll_src(buf)->addr, + if (!ieee802154_decrypt_auth(&ctx->sec_ctx, net_pkt_ll(pkt), + net_pkt_ll_reserve(pkt), + net_pkt_get_len(pkt), + net_pkt_ll_src(pkt)->addr, sys_le32_to_cpu( mpdu->mhr.aux_sec->frame_counter))) { 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 */ - buf->frags->len -= level_2_tag_size[level]; + pkt->frags->len -= level_2_tag_size[level]; return true; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_frame.h b/subsys/net/ip/l2/ieee802154/ieee802154_frame.h index bb6c3f203f4..3134cf7c49d 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_frame.h +++ b/subsys/net/ip/l2/ieee802154/ieee802154_frame.h @@ -464,32 +464,32 @@ bool ieee802154_create_data_frame(struct ieee802154_context *ctx, struct net_buf *frag, uint8_t reserved_len); -struct net_buf * +struct net_pkt * ieee802154_create_mac_cmd_frame(struct ieee802154_context *ctx, enum ieee802154_cfi type, struct ieee802154_frame_params *params); 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 bool ieee802154_create_ack_frame(struct net_if *iface, - struct net_buf *buf, uint8_t seq); + struct net_pkt *pkt, uint8_t seq); #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 *)net_pkt_ll(buf); + (struct ieee802154_fcf_seq *)net_pkt_ll(pkt); return fs->fc.ar; } #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); #else #define ieee802154_decipher_data_frame(...) true diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c b/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c index 162db50d00f..68418d00bb4 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c @@ -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_req_params *scan = (struct ieee802154_req_params *)data; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; uint8_t channel; 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.pan_id = IEEE802154_BROADCAST_PAN_ID; - buf = ieee802154_create_mac_cmd_frame( + pkt = ieee802154_create_mac_cmd_frame( ctx, IEEE802154_CFI_BEACON_REQUEST, ¶ms); - if (!buf) { + if (!pkt) { NET_DBG("Could not create Beacon Request"); return -ENOBUFS; } @@ -138,14 +138,14 @@ static int ieee802154_scan(uint32_t mgmt_request, struct net_if *iface, /* Active scan sends a beacon request */ if (mgmt_request == NET_REQUEST_IEEE802154_ACTIVE_SCAN) { - net_pkt_ref(buf); - net_pkt_ref(buf->frags); + net_pkt_ref(pkt); + net_pkt_frag_ref(pkt->frags); - ret = ieee802154_radio_send(iface, buf); + ret = ieee802154_radio_send(iface, pkt); if (ret) { NET_DBG("Could not send Beacon Request (%d)", ret); - net_pkt_unref(buf); + net_pkt_unref(pkt); break; } @@ -168,8 +168,8 @@ static int ieee802154_scan(uint32_t mgmt_request, struct net_if *iface, out: ctx->scan_ctx = NULL; - if (buf) { - net_pkt_unref(buf); + if (pkt) { + net_pkt_unref(pkt); } 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_frame_params params; struct ieee802154_command *cmd; - struct net_buf *buf; + struct net_pkt *pkt; int ret = 0; 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; } - buf = ieee802154_create_mac_cmd_frame( + pkt = ieee802154_create_mac_cmd_frame( ctx, IEEE802154_CFI_ASSOCIATION_REQUEST, ¶ms); - if (!buf) { + if (!pkt) { ret = -ENOBUFS; 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.power_src = 0; /* ToDo: set right power source */ 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; - if (net_if_send_data(iface, buf)) { - net_pkt_unref(buf); + if (net_if_send_data(iface, pkt)) { + net_pkt_unref(pkt); ret = -EIO; 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_frame_params params; struct ieee802154_command *cmd; - struct net_buf *buf; + struct net_pkt *pkt; if (!ctx->associated) { return -EALREADY; @@ -321,17 +321,17 @@ static int ieee802154_disassociate(uint32_t mgmt_request, struct net_if *iface, params.pan_id = ctx->pan_id; - buf = ieee802154_create_mac_cmd_frame( + pkt = ieee802154_create_mac_cmd_frame( ctx, IEEE802154_CFI_DISASSOCIATION_NOTIFICATION, ¶ms); - if (!buf) { + if (!pkt) { return -ENOBUFS; } - cmd = ieee802154_get_mac_command(buf); + cmd = ieee802154_get_mac_command(pkt); cmd->disassoc_note.reason = IEEE802154_DRF_DEVICE_WISH; - if (net_if_send_data(iface, buf)) { - net_pkt_unref(buf); + if (net_if_send_data(iface, pkt)) { + net_pkt_unref(pkt); return -EIO; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c b/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c index c519c25e216..b0366db4ad3 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c @@ -20,12 +20,12 @@ #include "ieee802154_radio_utils.h" static inline int aloha_tx_fragment(struct net_if *iface, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { uint8_t retries = CONFIG_NET_L2_IEEE802154_RADIO_TX_RETRIES; 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; int ret = -EIO; @@ -34,7 +34,7 @@ static inline int aloha_tx_fragment(struct net_if *iface, while (retries) { retries--; - ret = radio->tx(iface->dev, buf, frag); + ret = radio->tx(iface->dev, pkt, frag); if (ret) { continue; } @@ -48,19 +48,19 @@ static inline int aloha_tx_fragment(struct net_if *iface, 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, - struct net_buf *buf) + struct net_pkt *pkt) { 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 */ diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c b/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c index 600e8812670..bc2e79c79a5 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c @@ -23,7 +23,7 @@ #include "ieee802154_radio_utils.h" static inline int csma_ca_tx_fragment(struct net_if *iface, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { 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; struct ieee802154_context *ctx = net_if_l2_data(iface); 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 nb = 0; int ret = -EIO; @@ -61,7 +61,7 @@ loop: } } - ret = radio->tx(iface->dev, buf, frag); + ret = radio->tx(iface->dev, pkt, frag); if (ret) { continue; } @@ -75,19 +75,19 @@ loop: 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, - struct net_buf *buf) + struct net_pkt *pkt) { 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 */ diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h b/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h index 49c2c092d4d..a4f835ab72e 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h +++ b/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h @@ -8,13 +8,13 @@ #define __IEEE802154_RADIO_UTILS_H__ typedef int (ieee802154_radio_tx_frag_t)(struct net_if *iface, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag); 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; 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, - 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; k_sem_give(&ctx->ack_lock); @@ -55,16 +55,16 @@ static inline int handle_ack(struct ieee802154_context *ctx, return NET_CONTINUE; } -static inline int tx_buffer_fragments(struct net_if *iface, - struct net_buf *buf, +static inline int tx_packet_fragments(struct net_if *iface, + struct net_pkt *pkt, ieee802154_radio_tx_frag_t *tx_func) { int ret = 0; struct net_buf *frag; - frag = buf->frags; + frag = pkt->frags; while (frag) { - ret = tx_func(iface, buf, frag); + ret = tx_func(iface, pkt, frag); if (ret) { break; } @@ -73,7 +73,7 @@ static inline int tx_buffer_fragments(struct net_if *iface, } if (!ret) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return ret; diff --git a/subsys/net/ip/net_context.c b/subsys/net/ip/net_context.c index 5d1450ad5d6..c1b49d135f1 100644 --- a/subsys/net/ip/net_context.c +++ b/subsys/net/ip/net_context.c @@ -43,21 +43,21 @@ */ #define NET_CONN_CB(name) \ static enum net_verdict _##name(struct net_conn *conn, \ - struct net_buf *buf, \ + struct net_pkt *pkt, \ void *user_data); \ static enum net_verdict name(struct net_conn *conn, \ - struct net_buf *buf, \ + struct net_pkt *pkt, \ void *user_data) \ { \ enum net_verdict result; \ \ net_context_ref(user_data); \ - result = _##name(conn, buf, user_data); \ + result = _##name(conn, pkt, user_data); \ net_context_unref(user_data); \ return result; \ } \ static enum net_verdict _##name(struct net_conn *conn, \ - struct net_buf *buf, \ + struct net_pkt *pkt, \ void *user_data) \ @@ -69,29 +69,29 @@ static struct net_context contexts[NET_MAX_CONTEXT]; static struct k_sem contexts_lock; static enum net_verdict packet_received(struct net_conn *conn, - struct net_buf *buf, + struct net_pkt *pkt, 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) -static struct sockaddr *create_sockaddr(struct net_buf *buf, +static struct sockaddr *create_sockaddr(struct net_pkt *pkt, struct sockaddr *addr) { #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_IPV6_BUF(buf)->src); - net_sin6(addr)->sin6_port = NET_TCP_BUF(buf)->src_port; + &NET_IPV6_BUF(pkt)->src); + net_sin6(addr)->sin6_port = NET_TCP_BUF(pkt)->src_port; net_sin6(addr)->sin6_family = AF_INET6; } else #endif #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_IPV4_BUF(buf)->src); - net_sin(addr)->sin_port = NET_TCP_BUF(buf)->src_port; + &NET_IPV4_BUF(pkt)->src); + net_sin(addr)->sin_port = NET_TCP_BUF(pkt)->src_port; net_sin(addr)->sin_family = AF_INET; } else #endif @@ -344,20 +344,20 @@ int net_context_get(sa_family_t family, #if defined(CONFIG_NET_TCP) static void queue_fin(struct net_context *ctx) { - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(ctx->tcp, NET_TCP_FIN, NULL, 0, - NULL, &ctx->remote, &buf); - if (ret || !buf) { + NULL, &ctx->remote, &pkt); + if (ret || !pkt) { return; } ctx->tcp->fin_queued = 1; - ret = net_tcp_send_buf(buf); + ret = net_tcp_send_pkt(pkt); 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_DEBUG_CONTEXT) -#define net_tcp_print_recv_info(str, buf, port) \ +#define net_tcp_print_recv_info(str, pkt, port) \ do { \ if (net_context_get_family(context) == AF_INET6) { \ 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)); \ } else if (net_context_get_family(context) == AF_INET) {\ 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)); \ } \ } while (0) -#define net_tcp_print_send_info(str, buf, port) \ +#define net_tcp_print_send_info(str, pkt, port) \ do { \ if (net_context_get_family(context) == AF_INET6) { \ 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)); \ } else if (net_context_get_family(context) == AF_INET) {\ 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)); \ } \ } while (0) @@ -698,21 +698,21 @@ static inline int send_control_segment(struct net_context *context, const struct sockaddr *remote, int flags, const char *msg) { - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(context->tcp, flags, NULL, 0, - local, remote, &buf); + local, remote, &pkt); if (ret) { return ret; } - ret = net_send_data(buf); + ret = net_send_data(pkt); 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; } @@ -737,7 +737,7 @@ static inline int send_syn_ack(struct net_context *context, static inline int send_ack(struct net_context *context, struct sockaddr *remote) { - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; /* Something (e.g. a data transmission under the user @@ -747,16 +747,16 @@ static inline int send_ack(struct net_context *context, return 0; } - ret = net_tcp_prepare_ack(context->tcp, remote, &buf); + ret = net_tcp_prepare_ack(context->tcp, remote, &pkt); if (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) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return ret; @@ -765,28 +765,28 @@ static inline int send_ack(struct net_context *context, static int send_reset(struct net_context *context, struct sockaddr *remote) { - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; - ret = net_tcp_prepare_reset(context->tcp, remote, &buf); + ret = net_tcp_prepare_reset(context->tcp, remote, &pkt); if (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) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } 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 */ - 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); } @@ -808,15 +808,15 @@ NET_CONN_CB(tcp_established) 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) { 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 * match the next segment exactly, drop and wait for * retransmit @@ -824,10 +824,10 @@ NET_CONN_CB(tcp_established) return NET_DROP; } - set_appdata_values(buf, IPPROTO_TCP); - context->tcp->send_ack += net_pkt_appdatalen(buf); + set_appdata_values(pkt, IPPROTO_TCP); + 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) { /* 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)) { case NET_TCP_SYN_SENT: - net_context_set_iface(context, net_pkt_iface(buf)); + net_context_set_iface(context, net_pkt_iface(pkt)); break; default: NET_DBG("Context %p in wrong state %d", @@ -876,11 +876,11 @@ NET_CONN_CB(tcp_synack_received) 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) { context->connect_cb(context, -ECONNREFUSED, context->user_data); @@ -889,15 +889,15 @@ NET_CONN_CB(tcp_synack_received) return NET_DROP; } - if (NET_TCP_FLAGS(buf) & NET_TCP_SYN) { + if (NET_TCP_FLAGS(pkt) & NET_TCP_SYN) { 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; } /* * 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 *raddr; @@ -911,39 +911,39 @@ NET_CONN_CB(tcp_synack_received) #endif #if defined(CONFIG_NET_IPV6) - if (net_pkt_family(buf) == AF_INET6) { + if (net_pkt_family(pkt) == AF_INET6) { laddr = (struct sockaddr *)&l6addr; raddr = (struct sockaddr *)&r6addr; 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_IPV6_BUF(buf)->src); + &NET_IPV6_BUF(pkt)->src); 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_IPV6_BUF(buf)->dst); + &NET_IPV6_BUF(pkt)->dst); } else #endif #if defined(CONFIG_NET_IPV4) - if (net_pkt_family(buf) == AF_INET) { + if (net_pkt_family(pkt) == AF_INET) { laddr = (struct sockaddr *)&l4addr; raddr = (struct sockaddr *)&r4addr; 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_IPV4_BUF(buf)->src); + &NET_IPV4_BUF(pkt)->src); 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_IPV4_BUF(buf)->dst); + &NET_IPV4_BUF(pkt)->dst); } else #endif { - NET_DBG("Invalid family (%d)", net_pkt_family(buf)); + NET_DBG("Invalid family (%d)", net_pkt_family(pkt)); return NET_DROP; } @@ -954,8 +954,8 @@ NET_CONN_CB(tcp_synack_received) ret = net_tcp_register(raddr, laddr, - ntohs(NET_TCP_BUF(buf)->src_port), - ntohs(NET_TCP_BUF(buf)->dst_port), + ntohs(NET_TCP_BUF(pkt)->src_port), + ntohs(NET_TCP_BUF(pkt)->dst_port), tcp_established, context, &context->conn_handler); @@ -1185,7 +1185,7 @@ static void ack_timeout(struct k_work *work) 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) { 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); addr4->sin_family = AF_INET; - addr4->sin_port = NET_TCP_BUF(buf)->dst_port; - addr4->sin_addr = &NET_IPV4_BUF(buf)->dst; + addr4->sin_port = NET_TCP_BUF(pkt)->dst_port; + addr4->sin_addr = &NET_IPV4_BUF(pkt)->dst; } #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); addr6->sin6_family = AF_INET6; - addr6->sin6_port = NET_TCP_BUF(buf)->dst_port; - addr6->sin6_addr = &NET_IPV6_BUF(buf)->dst; + addr6->sin6_port = NET_TCP_BUF(pkt)->dst_port; + addr6->sin6_addr = &NET_IPV6_BUF(pkt)->dst; } #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, 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; } #else @@ -1231,7 +1231,7 @@ NET_CONN_CB(tcp_syn_rcvd) { struct net_context *context = (struct net_context *)user_data; struct net_tcp *tcp; - struct sockaddr_ptr buf_src_addr; + struct sockaddr_ptr pkt_src_addr; NET_ASSERT(context && context->tcp); @@ -1239,10 +1239,10 @@ NET_CONN_CB(tcp_syn_rcvd) switch (net_tcp_get_state(tcp)) { case NET_TCP_LISTEN: - net_context_set_iface(context, net_pkt_iface(buf)); + net_context_set_iface(context, net_pkt_iface(pkt)); break; 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; } break; @@ -1252,30 +1252,30 @@ NET_CONN_CB(tcp_syn_rcvd) 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 (NET_TCP_FLAGS(buf) == NET_TCP_SYN) { + if (NET_TCP_FLAGS(pkt) == NET_TCP_SYN) { 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); - remote = create_sockaddr(buf, &peer); + remote = create_sockaddr(pkt, &peer); /* FIXME: Is this the correct place to set 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; - buf_get_sockaddr(net_context_get_family(context), - buf, &buf_src_addr); - send_syn_ack(context, &buf_src_addr, remote); + pkt_get_sockaddr(net_context_get_family(context), + pkt, &pkt_src_addr); + send_syn_ack(context, &pkt_src_addr, remote); /* We might be entering this section multiple times * 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 (NET_TCP_FLAGS(buf) == NET_TCP_RST) { + if (NET_TCP_FLAGS(pkt) == NET_TCP_RST) { 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); @@ -1304,7 +1304,7 @@ NET_CONN_CB(tcp_syn_rcvd) /* * 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 sockaddr local_addr; struct sockaddr remote_addr; @@ -1321,7 +1321,7 @@ NET_CONN_CB(tcp_syn_rcvd) 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) { 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. */ - ret = net_context_get(net_pkt_family(buf), + ret = net_context_get(net_pkt_family(pkt), SOCK_STREAM, IPPROTO_TCP, &new_context); if (ret < 0) { @@ -1353,13 +1353,13 @@ NET_CONN_CB(tcp_syn_rcvd) remote_addr6->sin6_family = AF_INET6; local_addr6->sin6_family = AF_INET6; - local_addr6->sin6_port = NET_TCP_BUF(buf)->dst_port; - remote_addr6->sin6_port = NET_TCP_BUF(buf)->src_port; + local_addr6->sin6_port = NET_TCP_BUF(pkt)->dst_port; + remote_addr6->sin6_port = NET_TCP_BUF(pkt)->src_port; 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_IPV6_BUF(buf)->src); + &NET_IPV6_BUF(pkt)->src); addrlen = sizeof(struct sockaddr_in6); } else #endif /* CONFIG_NET_IPV6 */ @@ -1374,13 +1374,13 @@ NET_CONN_CB(tcp_syn_rcvd) remote_addr4->sin_family = AF_INET; local_addr4->sin_family = AF_INET; - local_addr4->sin_port = NET_TCP_BUF(buf)->dst_port; - remote_addr4->sin_port = NET_TCP_BUF(buf)->src_port; + local_addr4->sin_port = NET_TCP_BUF(pkt)->dst_port; + remote_addr4->sin_port = NET_TCP_BUF(pkt)->src_port; 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_IPV4_BUF(buf)->src); + &NET_IPV4_BUF(pkt)->src); addrlen = sizeof(struct sockaddr_in); } else #endif /* CONFIG_NET_IPV4 */ @@ -1453,7 +1453,7 @@ reset: { struct sockaddr peer; - send_reset(tcp->context, create_sockaddr(buf, &peer)); + send_reset(tcp->context, create_sockaddr(pkt, &peer)); } return NET_DROP; @@ -1562,7 +1562,7 @@ int net_context_accept(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, int32_t timeout, void *token, @@ -1570,10 +1570,10 @@ static int send_data(struct net_context *context, { context->send_cb = cb; 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) { - return net_send_data(buf); + return net_send_data(pkt); } #if defined(CONFIG_NET_TCP) @@ -1599,42 +1599,42 @@ static int send_data(struct net_context *context, #if defined(CONFIG_NET_UDP) static int create_udp_packet(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, const struct sockaddr *dst_addr, - struct net_buf **out_buf) + struct net_pkt **out_pkt) { int r = 0; #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; - buf = net_ipv6_create(context, buf, NULL, &addr6->sin6_addr); - buf = net_udp_append(context, buf, ntohs(addr6->sin6_port)); - r = net_ipv6_finalize(context, buf); + pkt = net_ipv6_create(context, pkt, NULL, &addr6->sin6_addr); + pkt = net_udp_append(context, pkt, ntohs(addr6->sin6_port)); + r = net_ipv6_finalize(context, pkt); } else #endif /* CONFIG_NET_IPV6 */ #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; - buf = net_ipv4_create(context, buf, NULL, &addr4->sin_addr); - buf = net_udp_append(context, buf, ntohs(addr4->sin_port)); - r = net_ipv4_finalize(context, buf); + pkt = net_ipv4_create(context, pkt, NULL, &addr4->sin_addr); + pkt = net_udp_append(context, pkt, ntohs(addr4->sin_port)); + r = net_ipv4_finalize(context, pkt); } else #endif /* CONFIG_NET_IPV4 */ { return -EPROTONOSUPPORT; } - *out_buf = buf; + *out_pkt = pkt; return r; } #endif /* CONFIG_NET_UDP */ -static int sendto(struct net_buf *buf, +static int sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr, socklen_t addrlen, net_context_send_cb_t cb, @@ -1642,7 +1642,7 @@ static int sendto(struct net_buf *buf, void *token, void *user_data) { - struct net_context *context = net_pkt_context(buf); + struct net_context *context = net_pkt_context(pkt); int ret; if (!net_context_is_used(context)) { @@ -1667,16 +1667,16 @@ static int sendto(struct net_buf *buf, } #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( - net_pkt_iface(buf), - buf, dst_addr, addrlen, + net_pkt_iface(pkt), + pkt, dst_addr, addrlen, cb, timeout, token, user_data); } #endif /* CONFIG_NET_OFFLOAD */ #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; if (addrlen < sizeof(struct sockaddr_in6)) { @@ -1690,7 +1690,7 @@ static int sendto(struct net_buf *buf, #endif /* CONFIG_NET_IPV6 */ #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; if (addrlen < sizeof(struct sockaddr_in)) { @@ -1703,19 +1703,19 @@ static int sendto(struct net_buf *buf, } else #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; } #if defined(CONFIG_NET_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 #endif /* CONFIG_NET_UDP */ #if defined(CONFIG_NET_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 #endif /* CONFIG_NET_TCP */ { @@ -1729,25 +1729,25 @@ static int sendto(struct net_buf *buf, 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, int32_t timeout, void *token, void *user_data) { - struct net_context *context = net_pkt_context(buf); + struct net_context *context = net_pkt_context(pkt); socklen_t addrlen; NET_ASSERT(PART_OF_ARRAY(contexts, context)); #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( - net_pkt_iface(buf), - buf, cb, timeout, + net_pkt_iface(pkt), + pkt, cb, timeout, token, user_data); } #endif /* CONFIG_NET_OFFLOAD */ @@ -1758,13 +1758,13 @@ int net_context_send(struct net_buf *buf, } #if defined(CONFIG_NET_IPV6) - if (net_pkt_family(buf) == AF_INET6) { + if (net_pkt_family(pkt) == AF_INET6) { addrlen = sizeof(struct sockaddr_in6); } else #endif /* CONFIG_NET_IPV6 */ #if defined(CONFIG_NET_IPV4) - if (net_pkt_family(buf) == AF_INET) { + if (net_pkt_family(pkt) == AF_INET) { addrlen = sizeof(struct sockaddr_in); } else #endif /* CONFIG_NET_IPV4 */ @@ -1772,11 +1772,11 @@ int net_context_send(struct net_buf *buf, addrlen = 0; } - return sendto(buf, &context->remote, addrlen, cb, timeout, token, + return sendto(pkt, &context->remote, addrlen, cb, timeout, token, user_data); } -int net_context_sendto(struct net_buf *buf, +int net_context_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr, socklen_t addrlen, net_context_send_cb_t cb, @@ -1785,61 +1785,61 @@ int net_context_sendto(struct net_buf *buf, void *user_data) { #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)); if (net_context_get_ip_proto(context) == IPPROTO_TCP) { /* 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 */ - 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 (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)); } else #endif /* CONFIG_NET_UDP */ #if defined(CONFIG_NET_TCP) if (proto == IPPROTO_TCP) { - net_pkt_set_appdata(buf, net_pkt_udp_data(buf) + - tcp_hdr_len(buf)); + net_pkt_set_appdata(pkt, net_pkt_udp_data(pkt) + + tcp_hdr_len(pkt)); } else #endif /* CONFIG_NET_TCP */ { - net_pkt_set_appdata(buf, net_pkt_ip_data(buf) + - net_pkt_ip_hdr_len(buf)); + net_pkt_set_appdata(pkt, net_pkt_ip_data(pkt) + + net_pkt_ip_hdr_len(pkt)); } - net_pkt_set_appdatalen(buf, total_len - - (net_pkt_appdata(buf) - - net_pkt_ip_data(buf))); + net_pkt_set_appdatalen(pkt, total_len - + (net_pkt_appdata(pkt) - + 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", - net_pkt_appdatalen(buf), total_len); + net_pkt_appdatalen(pkt), total_len); } static enum net_verdict packet_received(struct net_conn *conn, - struct net_buf *buf, - void *user_data) + struct net_pkt *pkt, + void *user_data) { struct net_context *context = find_context(conn); 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_pkt_set_context(buf, context); + net_context_set_iface(context, net_pkt_iface(pkt)); + net_pkt_set_context(pkt, context); /* If there is no callback registered, then we can only drop * 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) { /* 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) else if (net_context_get_type(context) == SOCK_STREAM) { - if (net_pkt_appdatalen(buf) == 0) { - net_pkt_unref(buf); + if (net_pkt_appdatalen(pkt) == 0) { + net_pkt_unref(pkt); return NET_OK; } } #endif /* CONFIG_NET_TCP */ NET_DBG("Set appdata %p to len %u (total %zu)", - net_pkt_appdata(buf), net_pkt_appdatalen(buf), - net_buf_frags_len(buf)); + net_pkt_appdata(pkt), net_pkt_appdatalen(pkt), + 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) k_sem_give(&context->recv_data_wait); diff --git a/subsys/net/ip/net_core.c b/subsys/net/ip/net_core.c index 8a9eacdd62f..6004657df3f 100644 --- a/subsys/net/ip/net_core.c +++ b/subsys/net/ip/net_core.c @@ -64,7 +64,7 @@ static struct k_fifo rx_queue; static k_tid_t rx_tid; 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) { 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 * not have link layer headers in it. */ - if (net_pkt_ipv6_fragment_start(buf)) { + if (net_pkt_ipv6_fragment_start(pkt)) { locally_routed = true; } #endif - /* If there is no data, then drop the packet. Also if - * the buffer is wrong type, then also drop the packet. - * The first buffer needs to have user data part that - * 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); + /* If there is no data, then drop the packet. */ + if (!pkt->frags) { + NET_DBG("Corrupted packet (frags %p)", pkt->frags); net_stats_update_processing_error(); return NET_DROP; } 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_DROP) { - NET_DBG("Buffer %p discarded by L2", buf); + NET_DBG("Packet %p discarded by L2", pkt); 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. */ - switch (NET_IPV6_BUF(buf)->vtc & 0xf0) { + switch (NET_IPV6_BUF(pkt)->vtc & 0xf0) { #if defined(CONFIG_NET_IPV6) case 0x60: net_stats_update_ipv6_recv(); - net_pkt_set_family(buf, PF_INET6); - return net_ipv6_process_pkt(buf); + net_pkt_set_family(pkt, PF_INET6); + return net_ipv6_process_pkt(pkt); #endif #if defined(CONFIG_NET_IPV4) case 0x40: net_stats_update_ipv4_recv(); - net_pkt_set_family(buf, PF_INET); - return net_ipv4_process_pkt(buf); + net_pkt_set_family(pkt, PF_INET); + return net_ipv4_process_pkt(pkt); #endif } 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_vhlerr(); 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: - NET_DBG("Consumed buf %p", buf); + NET_DBG("Consumed pkt %p", pkt); break; case NET_DROP: default: - NET_DBG("Dropping buf %p", buf); - net_pkt_unref(buf); + NET_DBG("Dropping pkt %p", pkt); + net_pkt_unref(pkt); break; } } 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)); @@ -168,18 +162,18 @@ static void net_rx_thread(void) size_t pkt_len; #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)); #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 - 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); - processing_data(buf, false); + processing_data(pkt, false); net_print_statistics(); 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, * 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 (net_pkt_family(buf) == AF_INET6) { - if (net_ipv6_addr_cmp(&NET_IPV6_BUF(buf)->dst, + if (net_pkt_family(pkt) == AF_INET6) { + if (net_ipv6_addr_cmp(&NET_IPV6_BUF(pkt)->dst, net_ipv6_unspecified_address())) { NET_DBG("IPv6 dst address missing"); 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 * back to us. */ - if (net_is_ipv6_addr_loopback(&NET_IPV6_BUF(buf)->dst) || - net_is_my_ipv6_addr(&NET_IPV6_BUF(buf)->dst)) { + if (net_is_ipv6_addr_loopback(&NET_IPV6_BUF(pkt)->dst) || + net_is_my_ipv6_addr(&NET_IPV6_BUF(pkt)->dst)) { struct in6_addr addr; /* Swap the addresses so that in receiving side * the packet is accepted. */ - net_ipaddr_copy(&addr, &NET_IPV6_BUF(buf)->src); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, &addr); + net_ipaddr_copy(&addr, &NET_IPV6_BUF(pkt)->src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, &addr); 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 * 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"); return -EADDRNOTAVAIL; } @@ -241,8 +235,8 @@ static inline int check_ip_addr(struct net_buf *buf) #endif /* CONFIG_NET_IPV6 */ #if defined(CONFIG_NET_IPV4) - if (net_pkt_family(buf) == AF_INET) { - if (net_ipv4_addr_cmp(&NET_IPV4_BUF(buf)->dst, + if (net_pkt_family(pkt) == AF_INET) { + if (net_ipv4_addr_cmp(&NET_IPV4_BUF(pkt)->dst, net_ipv4_unspecified_address())) { 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 * back to us. */ - if (net_is_ipv4_addr_loopback(&NET_IPV4_BUF(buf)->dst) || - net_is_my_ipv4_addr(&NET_IPV4_BUF(buf)->dst)) { + if (net_is_ipv4_addr_loopback(&NET_IPV4_BUF(pkt)->dst) || + net_is_my_ipv4_addr(&NET_IPV4_BUF(pkt)->dst)) { struct in_addr addr; /* Swap the addresses so that in receiving side * the packet is accepted. */ - net_ipaddr_copy(&addr, &NET_IPV4_BUF(buf)->src); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, - &NET_IPV4_BUF(buf)->dst); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, &addr); + net_ipaddr_copy(&addr, &NET_IPV4_BUF(pkt)->src); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, + &NET_IPV4_BUF(pkt)->dst); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, &addr); 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 * 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"); return -EADDRNOTAVAIL; } @@ -283,24 +277,24 @@ static inline int check_ip_addr(struct net_buf *buf) return 0; } #else -#define check_ip_addr(buf) 0 +#define check_ip_addr(pkt) 0 #endif /* 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; - if (!buf || !buf->frags) { + if (!pkt || !pkt->frags) { return -ENODATA; } - if (!net_pkt_iface(buf)) { + if (!net_pkt_iface(pkt)) { return -EINVAL; } #if defined(CONFIG_NET_STATISTICS) - switch (net_pkt_family(buf)) { + switch (net_pkt_family(pkt)) { case AF_INET: net_stats_update_ipv4_sent(); break; @@ -310,18 +304,18 @@ int net_send_data(struct net_buf *buf) } #endif - status = check_ip_addr(buf); + status = check_ip_addr(pkt); if (status < 0) { return status; } else if (status > 0) { /* Packet is destined back to us so send it directly * to RX processing. */ - processing_data(buf, true); + processing_data(pkt, true); 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; } @@ -329,12 +323,12 @@ int net_send_data(struct net_buf *buf) } /* 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); - if (!buf->frags) { + if (!pkt->frags) { return -ENODATA; } @@ -342,12 +336,12 @@ int net_recv_data(struct net_if *iface, struct net_buf *buf) return -ENETDOWN; } - NET_DBG("fifo %p iface %p buf %p len %zu", &rx_queue, iface, buf, - net_buf_frags_len(buf)); + NET_DBG("fifo %p iface %p pkt %p len %zu", &rx_queue, iface, pkt, + 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; } diff --git a/subsys/net/ip/net_if.c b/subsys/net/ip/net_if.c index fd2fa4e3971..a0d467f5a0e 100644 --- a/subsys/net/ip/net_if.c +++ b/subsys/net/ip/net_if.c @@ -46,14 +46,14 @@ NET_STACK_DEFINE(TX, tx_stack, CONFIG_NET_TX_STACK_SIZE, CONFIG_NET_TX_STACK_SIZE); #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", \ - buf, len); \ + NET_DBG("Processing (pkt %p, data len %zu) network packet", \ + pkt, len); \ \ - NET_ASSERT(buf->frags && len); \ + NET_ASSERT(pkt->frags && len); \ } while (0) #else #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; struct net_linkaddr *dst; struct net_context *context; - struct net_buf *buf; + struct net_pkt *pkt; void *context_token; int status; #if defined(CONFIG_NET_STATISTICS) size_t pkt_len; #endif - buf = net_buf_get(&iface->tx_queue, K_NO_WAIT); - if (!buf) { + pkt = k_fifo_get(&iface->tx_queue, K_NO_WAIT); + if (!pkt) { return false; } - debug_check_packet(buf); + debug_check_packet(pkt); - dst = net_pkt_ll_dst(buf); - context = net_pkt_context(buf); - context_token = net_pkt_token(buf); + dst = net_pkt_ll_dst(pkt); + context = net_pkt_context(pkt); + context_token = net_pkt_token(pkt); if (atomic_test_bit(iface->flags, NET_IF_UP)) { #if defined(CONFIG_NET_STATISTICS) - pkt_len = net_buf_frags_len(buf); + pkt_len = net_pkt_get_len(pkt); #endif - status = api->send(iface, buf); + status = api->send(iface, pkt); } else { /* Drop packet if interface is not up */ NET_WARN("iface %p is down", iface); @@ -108,7 +108,7 @@ static bool net_if_tx(struct net_if *iface) } if (status < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } else { net_stats_update_bytes_sent(pkt_len); } @@ -133,8 +133,8 @@ static void net_if_flush_tx(struct net_if *iface) return; } - /* Without this, the net_buf_get() can return a buf which - * has buf->frags set to NULL. This is not allowed as we + /* Without this, the k_fifo_get() can return a pkt which + * has pkt->frags set to NULL. This is not allowed as we * cannot send a packet that has no data in it. * The k_yield() fixes the issue and packets are flushed * correctly. @@ -222,11 +222,11 @@ static inline void init_iface(struct net_if *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_linkaddr *dst = net_pkt_ll_dst(buf); - void *token = net_pkt_token(buf); + struct net_context *context = net_pkt_context(pkt); + struct net_linkaddr *dst = net_pkt_ll_dst(pkt); + void *token = net_pkt_token(pkt); enum net_verdict verdict; 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 */ if (!atomic_test_bit(iface->flags, NET_IF_POINTOPOINT) && - !net_pkt_ll_src(buf)->addr) { - net_pkt_ll_src(buf)->addr = net_pkt_ll_if(buf)->addr; - net_pkt_ll_src(buf)->len = net_pkt_ll_if(buf)->len; + !net_pkt_ll_src(pkt)->addr) { + net_pkt_ll_src(pkt)->addr = net_pkt_ll_if(pkt)->addr; + net_pkt_ll_src(pkt)->len = net_pkt_ll_if(pkt)->len; } #if defined(CONFIG_NET_IPV6) /* If the ll dst address is not set check if it is present in the nbr * cache. */ - if (net_pkt_family(buf) == AF_INET6) { - buf = net_ipv6_prepare_for_send(buf); - if (!buf) { + if (net_pkt_family(pkt) == AF_INET6) { + pkt = net_ipv6_prepare_for_send(pkt); + if (!pkt) { verdict = NET_CONTINUE; goto done; } } #endif - verdict = iface->l2->send(iface, buf); + verdict = iface->l2->send(iface, pkt); done: /* The L2 send() function can return diff --git a/subsys/net/ip/net_pkt.c b/subsys/net/ip/net_pkt.c index a0e7e7c8f2e..de6dfceee1d 100644 --- a/subsys/net/ip/net_pkt.c +++ b/subsys/net/ip/net_pkt.c @@ -78,10 +78,8 @@ #error "Too small net_buf fragment size" #endif -NET_BUF_POOL_DEFINE(rx_pkts, NET_PKT_RX_COUNT, 0, sizeof(struct net_pkt), - NULL); -NET_BUF_POOL_DEFINE(tx_pkts, NET_PKT_TX_COUNT, 0, sizeof(struct net_pkt), - NULL); +K_MEM_SLAB_DEFINE(rx_pkts, sizeof(struct net_pkt), NET_PKT_RX_COUNT, 4); +K_MEM_SLAB_DEFINE(tx_pkts, sizeof(struct net_pkt), NET_PKT_TX_COUNT, 4); /* The data fragment pool is for storing network data. */ NET_BUF_POOL_DEFINE(rx_bufs, NET_BUF_RX_COUNT, NET_BUF_DATA_LEN, @@ -92,52 +90,26 @@ NET_BUF_POOL_DEFINE(tx_bufs, NET_BUF_TX_COUNT, NET_BUF_DATA_LEN, #if defined(CONFIG_NET_DEBUG_NET_PKT) -#define NET_BUF_CHECK_IF_IN_USE(buf, ref) \ - do { \ - if (ref) { \ - NET_ERR("**ERROR** buf %p in use (%s:%s():%d)", \ - buf, __FILE__, __func__, __LINE__); \ - } \ - } while (0) - -#define NET_BUF_CHECK_IF_NOT_IN_USE(buf, ref) \ +#define NET_FRAG_CHECK_IF_NOT_IN_USE(frag, ref) \ do { \ if (!(ref)) { \ - NET_ERR("**ERROR** buf %p not in use (%s:%s():%d)", \ - buf, __FILE__, __func__, __LINE__); \ + NET_ERR("**ERROR** frag %p not in use (%s:%s():%d)", \ + frag, __FILE__, __func__, __LINE__); \ } \ } while (0) -#else /* CONFIG_NET_DEBUG_NET_PKT */ -#define NET_BUF_CHECK_IF_IN_USE(buf, ref) -#define NET_BUF_CHECK_IF_NOT_IN_USE(buf, ref) -#endif /* CONFIG_NET_DEBUG_NET_PKT */ - -static inline bool is_data_pool(struct net_buf_pool *pool) -{ - /* The user data can only be found in TX/RX pool and it - * is always the size of struct net_pkt. - */ - if (pool->user_data_size != sizeof(struct net_pkt)) { - return true; - } - - return false; -} - -static inline bool is_from_data_pool(struct net_buf *buf) -{ - return is_data_pool(buf->pool); -} - -#if defined(CONFIG_NET_DEBUG_NET_PKT) struct net_pkt_alloc { - struct net_buf *buf; + union { + struct net_pkt *pkt; + struct net_buf *buf; + void *alloc_data; + }; const char *func_alloc; const char *func_free; uint16_t line_alloc; uint16_t line_free; uint8_t in_use; + bool is_pkt; }; #define MAX_NET_PKT_ALLOCS (NET_PKT_RX_COUNT + NET_PKT_TX_COUNT + \ @@ -146,9 +118,8 @@ struct net_pkt_alloc { static struct net_pkt_alloc net_pkt_allocs[MAX_NET_PKT_ALLOCS]; -static bool net_pkt_alloc_add(struct net_buf *buf, - const char *func, - int line) +static bool net_pkt_alloc_add(void *alloc_data, bool is_pkt, + const char *func, int line) { int i; @@ -158,7 +129,8 @@ static bool net_pkt_alloc_add(struct net_buf *buf, } net_pkt_allocs[i].in_use = true; - net_pkt_allocs[i].buf = buf; + net_pkt_allocs[i].is_pkt = is_pkt; + net_pkt_allocs[i].alloc_data = alloc_data; net_pkt_allocs[i].func_alloc = func; net_pkt_allocs[i].line_alloc = line; @@ -168,14 +140,13 @@ static bool net_pkt_alloc_add(struct net_buf *buf, return false; } -static bool net_pkt_alloc_del(struct net_buf *buf, - const char *func, - int line) +static bool net_pkt_alloc_del(void *alloc_data, const char *func, int line) { int i; for (i = 0; i < MAX_NET_PKT_ALLOCS; i++) { - if (net_pkt_allocs[i].in_use && net_pkt_allocs[i].buf == buf) { + if (net_pkt_allocs[i].in_use && + net_pkt_allocs[i].alloc_data == alloc_data) { net_pkt_allocs[i].func_free = func; net_pkt_allocs[i].line_free = line; net_pkt_allocs[i].in_use = false; @@ -187,14 +158,15 @@ static bool net_pkt_alloc_del(struct net_buf *buf, return false; } -static bool net_pkt_alloc_find(struct net_buf *buf, - const char **func_free, - int *line_free) +static bool net_pkt_alloc_find(void *alloc_data, + const char **func_free, + int *line_free) { int i; for (i = 0; i < MAX_NET_PKT_ALLOCS; i++) { - if (!net_pkt_allocs[i].in_use && net_pkt_allocs[i].buf == buf) { + if (!net_pkt_allocs[i].in_use && + net_pkt_allocs[i].alloc_data == alloc_data) { *func_free = net_pkt_allocs[i].func_free; *line_free = net_pkt_allocs[i].line_free; @@ -211,7 +183,10 @@ void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data) for (i = 0; i < MAX_NET_PKT_ALLOCS; i++) { if (net_pkt_allocs[i].in_use) { - cb(net_pkt_allocs[i].buf, + cb(net_pkt_allocs[i].is_pkt ? + net_pkt_allocs[i].pkt : NULL, + net_pkt_allocs[i].is_pkt ? + NULL : net_pkt_allocs[i].buf, net_pkt_allocs[i].func_alloc, net_pkt_allocs[i].line_alloc, net_pkt_allocs[i].func_free, @@ -223,7 +198,10 @@ void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data) for (i = 0; i < MAX_NET_PKT_ALLOCS; i++) { if (!net_pkt_allocs[i].in_use) { - cb(net_pkt_allocs[i].buf, + cb(net_pkt_allocs[i].is_pkt ? + net_pkt_allocs[i].pkt : NULL, + net_pkt_allocs[i].is_pkt ? + NULL : net_pkt_allocs[i].buf, net_pkt_allocs[i].func_alloc, net_pkt_allocs[i].line_alloc, net_pkt_allocs[i].func_free, @@ -234,85 +212,59 @@ void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data) } } -const char *net_pkt_pool2str(struct net_buf_pool *pool) +const char *net_pkt_slab2str(struct k_mem_slab *slab) { - if (pool == &rx_pkts) { + if (slab == &rx_pkts) { return "RX"; - } else if (pool == &tx_pkts) { + } else if (slab == &tx_pkts) { return "TX"; - } else if (pool == &rx_bufs) { - return "RDATA"; - } else if (pool == &tx_bufs) { - return "TDATA"; - } else if (is_data_pool(pool)) { - return "EDATA"; } return "EXT"; } +const char *net_pkt_pool2str(struct net_buf_pool *pool) +{ + if (pool == &rx_bufs) { + return "RDATA"; + } else if (pool == &tx_bufs) { + return "TDATA"; + } + + return "EDATA"; +} + static inline int16_t get_frees(struct net_buf_pool *pool) { return pool->avail_count; } -#endif -#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) -static inline struct net_buf_pool *get_tx_pool(struct net_context *context) +static inline const char *slab2str(struct k_mem_slab *slab) { - if (context->tx_pool) { - return context->tx_pool(); - } - - return NULL; + return net_pkt_slab2str(slab); } -static inline struct net_buf_pool *get_data_pool(struct net_context *context) -{ - if (context->data_pool) { - return context->data_pool(); - } - - return NULL; -} -#else -#define get_tx_pool(context) NULL -#define get_data_pool(context) NULL -#endif /* CONFIG_NET_CONTEXT_PKT_POOL */ - -static inline bool is_external_pool(struct net_buf_pool *pool) -{ - if (pool != &rx_pkts && pool != &tx_pkts && - pool != &rx_bufs && pool != &tx_bufs) { - return true; - } - - return false; -} - -#if defined(CONFIG_NET_DEBUG_NET_PKT) static inline const char *pool2str(struct net_buf_pool *pool) { return net_pkt_pool2str(pool); } -void net_pkt_print_frags(struct net_buf *buf) +void net_pkt_print_frags(struct net_pkt *pkt) { struct net_buf *frag; size_t total = 0; int count = -1, frag_size = 0, ll_overhead = 0; - if (!buf) { - NET_INFO("Buf %p", buf); + if (!pkt) { + NET_INFO("Pkt %p", pkt); return; } - NET_INFO("Buf %p frags %p", buf, buf->frags); + NET_INFO("Pkt %p frags %p", pkt, pkt->frags); - NET_ASSERT(buf->frags); - - frag = buf; + NET_ASSERT(pkt->frags); + frag = pkt->frags; while (frag) { total += frag->len; @@ -336,18 +288,64 @@ void net_pkt_print_frags(struct net_buf *buf) count * ll_overhead, (total * 100) / (count * frag_size)); } -struct net_buf *net_pkt_get_reserve_debug(struct net_buf_pool *pool, +struct net_pkt *net_pkt_get_reserve_debug(struct k_mem_slab *slab, uint16_t reserve_head, int32_t timeout, const char *caller, int line) #else /* CONFIG_NET_DEBUG_NET_PKT */ -struct net_buf *net_pkt_get_reserve(struct net_buf_pool *pool, +struct net_pkt *net_pkt_get_reserve(struct k_mem_slab *slab, uint16_t reserve_head, int32_t timeout) #endif /* CONFIG_NET_DEBUG_NET_PKT */ { - struct net_buf *buf; + struct net_pkt *pkt; + int ret; + + if (k_is_in_isr()) { + ret = k_mem_slab_alloc(slab, (void **)&pkt, K_NO_WAIT); + } else { + ret = k_mem_slab_alloc(slab, (void **)&pkt, timeout); + } + + if (ret) { + return NULL; + } + + memset(pkt, 0, sizeof(struct net_pkt)); + + net_pkt_set_ll_reserve(pkt, reserve_head); + + if (slab == &rx_pkts) { + net_pkt_set_dir(pkt, NET_RX); + } + + pkt->ref = 1; + pkt->slab = slab; + +#if defined(CONFIG_NET_DEBUG_NET_PKT) + net_pkt_alloc_add(pkt, true, caller, line); + + NET_DBG("%s [%u] pkt %p reserve %u ref %d (%s():%d)", + slab2str(slab), k_mem_slab_num_free_get(slab), + pkt, reserve_head, pkt->ref, caller, line); +#endif + return pkt; +} + +#if defined(CONFIG_NET_DEBUG_NET_PKT) +struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool, + uint16_t reserve_head, + int32_t timeout, + const char *caller, + int line) +#else /* CONFIG_NET_DEBUG_NET_PKT */ +struct net_buf *net_pkt_get_reserve_data(struct net_buf_pool *pool, + uint16_t reserve_head, + int32_t timeout) +#endif /* CONFIG_NET_DEBUG_NET_PKT */ +{ + struct net_buf *frag; /* * The reserve_head variable in the function will tell @@ -355,104 +353,80 @@ struct net_buf *net_pkt_get_reserve(struct net_buf_pool *pool, */ if (k_is_in_isr()) { - buf = net_buf_alloc(pool, K_NO_WAIT); + frag = net_buf_alloc(pool, K_NO_WAIT); } else { - buf = net_buf_alloc(pool, timeout); + frag = net_buf_alloc(pool, timeout); } - if (!buf) { + if (!frag) { return NULL; } - if (is_data_pool(pool)) { - /* The buf->data will point to the start of the L3 - * header (like IPv4 or IPv6 packet header). - */ - net_buf_reserve(buf, reserve_head); - } else { - memset(net_buf_user_data(buf), 0, sizeof(struct net_pkt)); - - net_pkt_set_ll_reserve(buf, reserve_head); - - /* Remember the RX vs. TX so that the fragments can be - * allocated from correct DATA pool. - */ - if (pool == &rx_pkts) { - net_pkt_set_dir(buf, NET_RX); - } - } - - NET_BUF_CHECK_IF_NOT_IN_USE(buf, buf->ref + 1); + net_buf_reserve(frag, reserve_head); #if defined(CONFIG_NET_DEBUG_NET_PKT) - net_pkt_alloc_add(buf, caller, line); + NET_FRAG_CHECK_IF_NOT_IN_USE(frag, frag->ref + 1); - NET_DBG("%s (%s) [%d] pkt %p reserve %u ref %d (%s():%d)", + net_pkt_alloc_add(frag, false, caller, line); + + NET_DBG("%s (%s) [%d] frag %p reserve %u ref %d (%s():%d)", pool2str(pool), pool->name, get_frees(pool), - buf, reserve_head, buf->ref, caller, line); + frag, reserve_head, frag->ref, caller, line); #endif - return buf; + return frag; } /* Get a fragment, try to figure out the pool from where to get * the data. */ #if defined(CONFIG_NET_DEBUG_NET_PKT) -struct net_buf *net_pkt_get_frag_debug(struct net_buf *buf, +struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt, int32_t timeout, const char *caller, int line) #else -struct net_buf *net_pkt_get_frag(struct net_buf *buf, +struct net_buf *net_pkt_get_frag(struct net_pkt *pkt, int32_t timeout) #endif { #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) struct net_context *context; -#endif - if (is_from_data_pool(buf)) { - /* We cannot know the correct data pool in this case (because - * we do not know the context). Return error to the caller. - */ - return NULL; - } - -#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) - context = net_pkt_context(buf); + context = net_pkt_context(pkt); if (context && context->data_pool) { #if defined(CONFIG_NET_DEBUG_NET_PKT) - return net_pkt_get_reserve_debug(context->data_pool(), - net_pkt_ll_reserve(buf), - timeout, caller, line); + return net_pkt_get_reserve_data_debug(context->data_pool(), + net_pkt_ll_reserve(pkt), + timeout, caller, line); #else - return net_pkt_get_reserve(context->data_pool(), - net_pkt_ll_reserve(buf), timeout); + return net_pkt_get_reserve_data(context->data_pool(), + net_pkt_ll_reserve(pkt), + timeout); #endif /* CONFIG_NET_DEBUG_NET_PKT */ } #endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */ - if (net_pkt_dir(buf) == NET_RX) { + if (net_pkt_dir(pkt) == NET_RX) { #if defined(CONFIG_NET_DEBUG_NET_PKT) return net_pkt_get_reserve_rx_data_debug( - net_pkt_ll_reserve(buf), timeout, caller, line); + net_pkt_ll_reserve(pkt), timeout, caller, line); #else - return net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + return net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), timeout); #endif } #if defined(CONFIG_NET_DEBUG_NET_PKT) - return net_pkt_get_reserve_tx_data_debug(net_pkt_ll_reserve(buf), + return net_pkt_get_reserve_tx_data_debug(net_pkt_ll_reserve(pkt), timeout, caller, line); #else - return net_pkt_get_reserve_tx_data(net_pkt_ll_reserve(buf), + return net_pkt_get_reserve_tx_data(net_pkt_ll_reserve(pkt), timeout); #endif } #if defined(CONFIG_NET_DEBUG_NET_PKT) -struct net_buf *net_pkt_get_reserve_rx_debug(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_rx_debug(uint16_t reserve_head, int32_t timeout, const char *caller, int line) { @@ -460,7 +434,7 @@ struct net_buf *net_pkt_get_reserve_rx_debug(uint16_t reserve_head, caller, line); } -struct net_buf *net_pkt_get_reserve_tx_debug(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_tx_debug(uint16_t reserve_head, int32_t timeout, const char *caller, int line) { @@ -472,27 +446,27 @@ struct net_buf *net_pkt_get_reserve_rx_data_debug(uint16_t reserve_head, int32_t timeout, const char *caller, int line) { - return net_pkt_get_reserve_debug(&rx_bufs, reserve_head, - timeout, caller, line); + return net_pkt_get_reserve_data_debug(&rx_bufs, reserve_head, + timeout, caller, line); } struct net_buf *net_pkt_get_reserve_tx_data_debug(uint16_t reserve_head, int32_t timeout, const char *caller, int line) { - return net_pkt_get_reserve_debug(&tx_bufs, reserve_head, - timeout, caller, line); + return net_pkt_get_reserve_data_debug(&tx_bufs, reserve_head, + timeout, caller, line); } #else /* CONFIG_NET_DEBUG_NET_PKT */ -struct net_buf *net_pkt_get_reserve_rx(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_rx(uint16_t reserve_head, int32_t timeout) { return net_pkt_get_reserve(&rx_pkts, reserve_head, timeout); } -struct net_buf *net_pkt_get_reserve_tx(uint16_t reserve_head, +struct net_pkt *net_pkt_get_reserve_tx(uint16_t reserve_head, int32_t timeout) { return net_pkt_get_reserve(&tx_pkts, reserve_head, timeout); @@ -501,32 +475,32 @@ struct net_buf *net_pkt_get_reserve_tx(uint16_t reserve_head, struct net_buf *net_pkt_get_reserve_rx_data(uint16_t reserve_head, int32_t timeout) { - return net_pkt_get_reserve(&rx_bufs, reserve_head, timeout); + return net_pkt_get_reserve_data(&rx_bufs, reserve_head, timeout); } struct net_buf *net_pkt_get_reserve_tx_data(uint16_t reserve_head, int32_t timeout) { - return net_pkt_get_reserve(&tx_bufs, reserve_head, timeout); + return net_pkt_get_reserve_data(&tx_bufs, reserve_head, timeout); } #endif /* CONFIG_NET_DEBUG_NET_PKT */ #if defined(CONFIG_NET_DEBUG_NET_PKT) -static struct net_buf *net_pkt_get_debug(struct net_buf_pool *pool, +static struct net_pkt *net_pkt_get_debug(struct k_mem_slab *slab, struct net_context *context, int32_t timeout, const char *caller, int line) #else -static struct net_buf *net_pkt_get(struct net_buf_pool *pool, +static struct net_pkt *net_pkt_get(struct k_mem_slab *slab, struct net_context *context, int32_t timeout) #endif /* CONFIG_NET_DEBUG_NET_PKT */ { struct in6_addr *addr6 = NULL; struct net_if *iface; - struct net_buf *buf; + struct net_pkt *pkt; if (!context) { return NULL; @@ -541,45 +515,105 @@ static struct net_buf *net_pkt_get(struct net_buf_pool *pool, } #if defined(CONFIG_NET_DEBUG_NET_PKT) - buf = net_pkt_get_reserve_debug(pool, + pkt = net_pkt_get_reserve_debug(slab, net_if_get_ll_reserve(iface, addr6), timeout, caller, line); #else - buf = net_pkt_get_reserve(pool, net_if_get_ll_reserve(iface, addr6), + pkt = net_pkt_get_reserve(slab, net_if_get_ll_reserve(iface, addr6), timeout); #endif - if (!buf) { - return buf; - } - - if (!is_data_pool(pool)) { - net_pkt_set_context(buf, context); - net_pkt_set_iface(buf, iface); + if (pkt) { + net_pkt_set_context(pkt, context); + net_pkt_set_iface(pkt, iface); if (context) { - net_pkt_set_family(buf, + net_pkt_set_family(pkt, net_context_get_family(context)); } } - return buf; + return pkt; } #if defined(CONFIG_NET_DEBUG_NET_PKT) -struct net_buf *net_pkt_get_rx_debug(struct net_context *context, +static struct net_buf *_pkt_get_data_debug(struct net_buf_pool *pool, + struct net_context *context, + int32_t timeout, + const char *caller, int line) +#else +static struct net_buf *_pkt_get_data(struct net_buf_pool *pool, + struct net_context *context, + int32_t timeout) +#endif /* CONFIG_NET_DEBUG_NET_PKT */ +{ + struct in6_addr *addr6 = NULL; + struct net_if *iface; + struct net_buf *frag; + + if (!context) { + return NULL; + } + + iface = net_context_get_iface(context); + + NET_ASSERT(iface); + + if (net_context_get_family(context) == AF_INET6) { + addr6 = &((struct sockaddr_in6 *) &context->remote)->sin6_addr; + } + +#if defined(CONFIG_NET_DEBUG_NET_PKT) + frag = net_pkt_get_reserve_data_debug(pool, + net_if_get_ll_reserve(iface, + addr6), + timeout, caller, line); +#else + frag = net_pkt_get_reserve_data(pool, + net_if_get_ll_reserve(iface, addr6), + timeout); +#endif + return frag; +} + + +#if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) +static inline struct k_mem_slab *get_tx_slab(struct net_context *context) +{ + if (context->tx_slab) { + return context->tx_slab(); + } + + return NULL; +} + +static inline struct net_buf_pool *get_data_pool(struct net_context *context) +{ + if (context->data_pool) { + return context->data_pool(); + } + + return NULL; +} +#else +#define get_tx_slab(...) NULL +#define get_data_pool(...) NULL +#endif /* CONFIG_NET_CONTEXT_NET_PKT_POOL */ + +#if defined(CONFIG_NET_DEBUG_NET_PKT) +struct net_pkt *net_pkt_get_rx_debug(struct net_context *context, int32_t timeout, const char *caller, int line) { return net_pkt_get_debug(&rx_pkts, context, timeout, caller, line); } -struct net_buf *net_pkt_get_tx_debug(struct net_context *context, +struct net_pkt *net_pkt_get_tx_debug(struct net_context *context, int32_t timeout, const char *caller, int line) { - struct net_buf_pool *pool = get_tx_pool(context); + struct k_mem_slab *slab = get_tx_slab(context); - return net_pkt_get_debug(pool ? pool : &tx_pkts, context, + return net_pkt_get_debug(slab ? slab : &tx_pkts, context, timeout, caller, line); } @@ -589,28 +623,28 @@ struct net_buf *net_pkt_get_data_debug(struct net_context *context, { struct net_buf_pool *pool = get_data_pool(context); - return net_pkt_get_debug(pool ? pool : &tx_bufs, context, - timeout, caller, line); + return _pkt_get_data_debug(pool ? pool : &tx_bufs, context, + timeout, caller, line); } #else /* CONFIG_NET_DEBUG_NET_PKT */ -struct net_buf *net_pkt_get_rx(struct net_context *context, int32_t timeout) +struct net_pkt *net_pkt_get_rx(struct net_context *context, int32_t timeout) { NET_ASSERT_INFO(context, "RX context not set"); return net_pkt_get(&rx_pkts, context, timeout); } -struct net_buf *net_pkt_get_tx(struct net_context *context, int32_t timeout) +struct net_pkt *net_pkt_get_tx(struct net_context *context, int32_t timeout) { - struct net_buf_pool *pool; + struct k_mem_slab *slab; NET_ASSERT_INFO(context, "TX context not set"); - pool = get_tx_pool(context); + slab = get_tx_slab(context); - return net_pkt_get(pool ? pool : &tx_pkts, context, timeout); + return net_pkt_get(slab ? slab : &tx_pkts, context, timeout); } struct net_buf *net_pkt_get_data(struct net_context *context, int32_t timeout) @@ -624,55 +658,55 @@ struct net_buf *net_pkt_get_data(struct net_context *context, int32_t timeout) /* The context is not known in RX path so we can only have TX * data here. */ - return net_pkt_get(pool ? pool : &tx_bufs, context, timeout); + return _pkt_get_data(pool ? pool : &tx_bufs, context, timeout); } #endif /* CONFIG_NET_DEBUG_NET_PKT */ #if defined(CONFIG_NET_DEBUG_NET_PKT) -void net_pkt_unref_debug(struct net_buf *buf, const char *caller, int line) +void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line) { struct net_buf *frag; #else -void net_pkt_unref(struct net_buf *buf) +void net_pkt_unref(struct net_pkt *pkt) { #endif /* CONFIG_NET_DEBUG_NET_PKT */ - if (!buf) { - NET_DBG("*** ERROR *** buf %p (%s():%d)", buf, caller, line); + if (!pkt) { + NET_ERR("*** ERROR *** pkt %p (%s():%d)", pkt, caller, line); return; } - if (!buf->ref) { + if (!pkt->ref) { #if defined(CONFIG_NET_DEBUG_NET_PKT) const char *func_freed; int line_freed; - if (net_pkt_alloc_find(buf, &func_freed, &line_freed)) { - NET_DBG("*** ERROR *** buf %p is freed already by " + if (net_pkt_alloc_find(pkt, &func_freed, &line_freed)) { + NET_ERR("*** ERROR *** pkt %p is freed already by " "%s():%d (%s():%d)", - buf, func_freed, line_freed, caller, line); + pkt, func_freed, line_freed, caller, line); } else { - NET_DBG("*** ERROR *** buf %p is freed already " - "(%s():%d)", buf, caller, line); + NET_ERR("*** ERROR *** pkt %p is freed already " + "(%s():%d)", pkt, caller, line); } #endif return; } #if defined(CONFIG_NET_DEBUG_NET_PKT) - NET_DBG("%s (%s) [%d] buf %p ref %d frags %p (%s():%d)", - pool2str(buf->pool), buf->pool->name, get_frees(buf->pool), - buf, buf->ref - 1, buf->frags, caller, line); + NET_DBG("%s [%d] pkt %p ref %d frags %p (%s():%d)", + slab2str(pkt->slab), k_mem_slab_num_free_get(pkt->slab), + pkt, pkt->ref - 1, pkt->frags, caller, line); - if (buf->ref > 1) { + if (pkt->ref > 1) { goto done; } - frag = buf->frags; + frag = pkt->frags; while (frag) { - NET_DBG("%s (%s) [%d] buf %p ref %d frags %p (%s():%d)", + NET_DBG("%s (%s) [%d] frag %p ref %d frags %p (%s():%d)", pool2str(frag->pool), frag->pool->name, get_frees(frag->pool), frag, frag->ref - 1, frag->frags, caller, line); @@ -683,12 +717,12 @@ void net_pkt_unref(struct net_buf *buf) if (net_pkt_alloc_find(frag, &func_freed, &line_freed)) { - NET_DBG("*** ERROR *** frag %p is freed " + NET_ERR("*** ERROR *** frag %p is freed " "already by %s():%d (%s():%d)", frag, func_freed, line_freed, caller, line); } else { - NET_DBG("*** ERROR *** frag %p is freed " + NET_ERR("*** ERROR *** frag %p is freed " "already (%s():%d)", frag, caller, line); } @@ -699,68 +733,165 @@ void net_pkt_unref(struct net_buf *buf) frag = frag->frags; } - net_pkt_alloc_del(buf, caller, line); + net_pkt_alloc_del(pkt, caller, line); done: #endif /* CONFIG_NET_DEBUG_NET_PKT */ - net_buf_unref(buf); + if (--pkt->ref > 0) { + return; + } + + if (pkt->frags) { + net_pkt_frag_unref(pkt->frags); + } + + k_mem_slab_free(pkt->slab, (void **)&pkt); } - #if defined(CONFIG_NET_DEBUG_NET_PKT) -struct net_buf *net_pkt_ref_debug(struct net_buf *buf, const char *caller, +struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller, int line) #else -struct net_buf *net_pkt_ref(struct net_buf *buf) +struct net_pkt *net_pkt_ref(struct net_pkt *pkt) #endif /* CONFIG_NET_DEBUG_NET_PKT */ { - if (!buf) { - NET_DBG("*** ERROR *** buf %p (%s():%d)", buf, caller, line); + if (!pkt) { + NET_ERR("*** ERROR *** pkt %p (%s():%d)", pkt, caller, line); return NULL; } #if defined(CONFIG_NET_DEBUG_NET_PKT) - NET_DBG("%s (%s) [%d] buf %p ref %d (%s():%d)", - pool2str(buf->pool), buf->pool->name, get_frees(buf->pool), - buf, buf->ref + 1, caller, line); + NET_DBG("%s [%d] pkt %p ref %d (%s():%d)", + slab2str(pkt->slab), k_mem_slab_num_free_get(pkt->slab), + pkt, pkt->ref + 1, caller, line); #endif - return net_buf_ref(buf); + pkt->ref++; + + return pkt; } #if defined(CONFIG_NET_DEBUG_NET_PKT) -struct net_buf *net_pkt_frag_del_debug(struct net_buf *parent, +struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag, + const char *caller, int line) +#else +struct net_buf *net_pkt_frag_ref(struct net_buf *frag) +#endif /* CONFIG_NET_DEBUG_NET_PKT */ +{ + if (!frag) { + NET_ERR("*** ERROR *** frag %p (%s():%d)", frag, caller, line); + return NULL; + } + +#if defined(CONFIG_NET_DEBUG_NET_PKT) + NET_DBG("%s (%s) [%d] frag %p ref %d (%s():%d)", + pool2str(frag->pool), frag->pool->name, get_frees(frag->pool), + frag, frag->ref + 1, caller, line); +#endif + + return net_buf_ref(frag); +} + + +#if defined(CONFIG_NET_DEBUG_NET_PKT) +void net_pkt_frag_unref_debug(struct net_buf *frag, + const char *caller, int line) +#else +void net_pkt_frag_unref(struct net_buf *frag) +#endif /* CONFIG_NET_DEBUG_NET_PKT */ +{ + if (!frag) { + NET_ERR("*** ERROR *** frag %p (%s():%d)", frag, caller, line); + return; + } + +#if defined(CONFIG_NET_DEBUG_NET_PKT) + NET_DBG("%s (%s) [%d] frag %p ref %d (%s():%d)", + pool2str(frag->pool), frag->pool->name, get_frees(frag->pool), + frag, frag->ref - 1, caller, line); + + if (frag->ref == 1) { + net_pkt_alloc_del(frag, caller, line); + } +#endif + net_buf_unref(frag); +} + +#if defined(CONFIG_NET_DEBUG_NET_PKT) +struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt, + struct net_buf *parent, struct net_buf *frag, const char *caller, int line) #else -struct net_buf *net_pkt_frag_del(struct net_buf *parent, +struct net_buf *net_pkt_frag_del(struct net_pkt *pkt, + struct net_buf *parent, struct net_buf *frag) #endif { #if defined(CONFIG_NET_DEBUG_NET_PKT) + NET_DBG("Pkt %p parent %p frag %p ref %u (%s:%d)", + pkt, parent, frag, frag->ref, caller, line); + if (frag->ref == 1) { net_pkt_alloc_del(frag, caller, line); } #endif + if (pkt->frags == frag && !parent) { + struct net_buf *tmp; + + tmp = net_buf_frag_del(NULL, frag); + pkt->frags = tmp; + + return tmp; + } + return net_buf_frag_del(parent, frag); } -struct net_buf *net_pkt_copy(struct net_buf *buf, size_t amount, +#if defined(CONFIG_NET_DEBUG_NET_PKT) +void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag, + const char *caller, int line) +#else +void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag) +#endif +{ + NET_DBG("Pkt %p frag %p (%s:%d)", pkt, frag, caller, line); + + /* We do not use net_buf_frag_add() as this one will refcount + * the frag once more if !pkt->frags + */ + if (!pkt->frags) { + pkt->frags = frag; + return; + } + + net_buf_frag_insert(net_buf_frag_last(pkt->frags), frag); +} + +#if defined(CONFIG_NET_DEBUG_NET_PKT) +void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag, + const char *caller, int line) +#else +void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag) +#endif +{ + NET_DBG("Pkt %p frag %p (%s:%d)", pkt, frag, caller, line); + + net_buf_frag_last(frag)->frags = pkt->frags; + pkt->frags = frag; +} + +struct net_buf *net_pkt_copy(struct net_pkt *pkt, size_t amount, size_t reserve, int32_t timeout) { struct net_buf *frag, *first, *orig; uint8_t *orig_data; size_t orig_len; - if (is_from_data_pool(buf)) { - NET_ERR("Buffer %p should not be a data fragment", buf); - return NULL; - } + orig = pkt->frags; - orig = buf->frags; - - frag = net_pkt_get_frag(buf, timeout); + frag = net_pkt_get_frag(pkt, timeout); if (!frag) { return NULL; } @@ -768,7 +899,7 @@ struct net_buf *net_pkt_copy(struct net_buf *buf, size_t amount, if (reserve > net_buf_tailroom(frag)) { NET_ERR("Reserve %zu is too long, max is %zu", reserve, net_buf_tailroom(frag)); - net_pkt_unref(frag); + net_pkt_frag_unref(frag); return NULL; } @@ -781,7 +912,7 @@ struct net_buf *net_pkt_copy(struct net_buf *buf, size_t amount, if (!orig->len) { /* No data in the first fragment in the original message */ - NET_DBG("Original buffer empty!"); + NET_DBG("Original fragment empty!"); return frag; } @@ -814,9 +945,9 @@ struct net_buf *net_pkt_copy(struct net_buf *buf, size_t amount, * We must allocate a new one. */ struct net_buf *new_frag = - net_pkt_get_frag(buf, timeout); + net_pkt_get_frag(pkt, timeout); if (!new_frag) { - net_pkt_unref(first); + net_pkt_frag_unref(first); return NULL; } @@ -849,8 +980,8 @@ struct net_buf *net_pkt_copy(struct net_buf *buf, size_t amount, return first; } -int net_pkt_linear_copy(struct net_buf *dst, struct net_buf *src, - uint16_t offset, uint16_t len) +int net_frag_linear_copy(struct net_buf *dst, struct net_buf *src, + uint16_t offset, uint16_t len) { uint16_t to_copy; uint16_t copied; @@ -888,183 +1019,75 @@ int net_pkt_linear_copy(struct net_buf *dst, struct net_buf *src, return 0; } -bool net_pkt_is_compact(struct net_buf *buf) +bool net_pkt_compact(struct net_pkt *pkt) { - struct net_buf *last; - size_t total = 0, calc; - int count = 0; + struct net_buf *frag, *prev; - last = NULL; + NET_DBG("Compacting data in pkt %p", pkt); - if (!is_from_data_pool(buf)) { - /* Skip the first element that does not contain any data. - */ - buf = buf->frags; - } - - while (buf) { - total += buf->len; - count++; - - last = buf; - buf = buf->frags; - } - - NET_ASSERT(last); - - if (!last) { - return false; - } - - calc = count * last->size - net_buf_tailroom(last) - - count * net_buf_headroom(last); - - if (total == calc) { - return true; - } - - NET_DBG("Not compacted total %zu real %zu", total, calc); - - return false; -} - -bool net_pkt_compact(struct net_buf *buf) -{ - struct net_buf *prev; - - if (is_from_data_pool(buf)) { - NET_DBG("Buffer %p is a data fragment", buf); - return false; - } - - NET_DBG("Compacting data to buf %p", buf); - - buf = buf->frags; + frag = pkt->frags; prev = NULL; - while (buf) { - if (buf->frags) { + while (frag) { + if (frag->frags) { /* Copy amount of data from next fragment to this * fragment. */ size_t copy_len; - copy_len = buf->frags->len; - if (copy_len > net_buf_tailroom(buf)) { - copy_len = net_buf_tailroom(buf); + copy_len = frag->frags->len; + if (copy_len > net_buf_tailroom(frag)) { + copy_len = net_buf_tailroom(frag); } - memcpy(net_buf_tail(buf), buf->frags->data, copy_len); - net_buf_add(buf, copy_len); + memcpy(net_buf_tail(frag), frag->frags->data, copy_len); + net_buf_add(frag, copy_len); - memmove(buf->frags->data, - buf->frags->data + copy_len, - buf->frags->len - copy_len); + memmove(frag->frags->data, + frag->frags->data + copy_len, + frag->frags->len - copy_len); - buf->frags->len -= copy_len; + frag->frags->len -= copy_len; /* Is there any more space in this fragment */ - if (net_buf_tailroom(buf)) { + if (net_buf_tailroom(frag)) { /* There is. This also means that the next * fragment is empty as otherwise we could * not have copied all data. Remove next * fragment as there is no data in it any more. */ - net_pkt_frag_del(buf, buf->frags); + net_pkt_frag_del(pkt, frag, frag->frags); /* Then check next fragment */ continue; } } else { - if (!buf->len) { + if (!frag->len) { /* Remove the last fragment because there is no * data in it. */ - net_pkt_frag_del(prev, buf); + net_pkt_frag_del(pkt, prev, frag); break; } } - prev = buf; - buf = buf->frags; + prev = frag; + frag = frag->frags; } return true; } -struct net_buf *net_pkt_pull(struct net_buf *buf, size_t amount) -{ - struct net_buf *first; - ssize_t count = amount; - - if (amount == 0) { - NET_DBG("No data to remove."); - return buf; - } - - first = buf; - - if (!is_from_data_pool(buf)) { - NET_DBG("Buffer %p is not a data fragment", buf); - buf = buf->frags; - } - - NET_DBG("Removing first %zd bytes from the fragments (%zu bytes)", - count, net_buf_frags_len(buf)); - - while (buf && count > 0) { - if (count < buf->len) { - /* We can remove the data from this single fragment */ - net_buf_pull(buf, count); - return first; - } - - if (buf->len == count) { - if (buf == first) { - struct net_buf tmp; - - tmp.frags = buf; - first = buf->frags; - net_pkt_frag_del(&tmp, buf); - } else { - net_pkt_frag_del(first, buf); - } - - return first; - } - - count -= buf->len; - - if (buf == first) { - struct net_buf tmp; - - tmp.frags = buf; - first = buf->frags; - net_pkt_frag_del(&tmp, buf); - buf = first; - } else { - net_pkt_frag_del(first, buf); - buf = first->frags; - } - } - - if (count > 0) { - NET_ERR("Not enough data in the fragments"); - } - - return first; -} - /* This helper routine will append multiple bytes, if there is no place for * the data in current fragment then create new fragment and add it to * the buffer. It assumes that the buffer has at least one fragment. */ -static inline bool net_pkt_append_bytes(struct net_buf *buf, +static inline bool net_pkt_append_bytes(struct net_pkt *pkt, const uint8_t *value, uint16_t len, int32_t timeout) { - struct net_buf *frag = net_buf_frag_last(buf); + struct net_buf *frag = net_buf_frag_last(pkt->frags); do { uint16_t count = min(len, net_buf_tailroom(frag)); @@ -1078,95 +1101,86 @@ static inline bool net_pkt_append_bytes(struct net_buf *buf, return true; } - frag = net_pkt_get_frag(buf, timeout); + frag = net_pkt_get_frag(pkt, timeout); if (!frag) { return false; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); } while (1); return false; } -bool net_pkt_append(struct net_buf *buf, uint16_t len, const uint8_t *data, +bool net_pkt_append(struct net_pkt *pkt, uint16_t len, const uint8_t *data, int32_t timeout) { struct net_buf *frag; - if (!buf || !data) { + if (!pkt || !data) { return false; } - if (is_from_data_pool(buf)) { - /* The buf needs to be a net_buf that has user data - * part. Otherwise we cannot use the net_pkt_ll_reserve() - * function to figure out the reserve amount. - */ - NET_DBG("Buffer %p is a data fragment", buf); - return false; - } - - if (!buf->frags) { - frag = net_pkt_get_frag(buf, timeout); + if (!pkt->frags) { + frag = net_pkt_get_frag(pkt, timeout); if (!frag) { return false; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); } - return net_pkt_append_bytes(buf, data, len, timeout); + return net_pkt_append_bytes(pkt, data, len, timeout); } /* Helper routine to retrieve single byte from fragment and move * offset. If required byte is last byte in framgent then return * next fragment and set offset = 0. */ -static inline struct net_buf *net_pkt_read_byte(struct net_buf *buf, - uint16_t offset, - uint16_t *pos, - uint8_t *data) +static inline struct net_buf *net_frag_read_byte(struct net_buf *frag, + uint16_t offset, + uint16_t *pos, + uint8_t *data) { if (data) { - *data = buf->data[offset]; + *data = frag->data[offset]; } *pos = offset + 1; - if (*pos >= buf->len) { + if (*pos >= frag->len) { *pos = 0; - return buf->frags; + return frag->frags; } - return buf; + return frag; } -/* Helper function to adjust offset in net_pkt_read() call +/* Helper function to adjust offset in net_frag_read() call * if given offset is more than current fragment length. */ -static inline struct net_buf *adjust_offset(struct net_buf *buf, +static inline struct net_buf *adjust_offset(struct net_buf *frag, uint16_t offset, uint16_t *pos) { - if (!buf || !is_from_data_pool(buf)) { - NET_ERR("Invalid buffer or buffer is not a fragment"); + if (!frag) { + NET_ERR("Invalid fragment"); return NULL; } - while (buf) { - if (offset == buf->len) { + while (frag) { + if (offset == frag->len) { *pos = 0; - return buf->frags; - } else if (offset < buf->len) { + return frag->frags; + } else if (offset < frag->len) { *pos = offset; - return buf; + return frag; } - offset -= buf->len; - buf = buf->frags; + offset -= frag->len; + frag = frag->frags; } NET_ERR("Invalid offset (%u), failed to adjust", offset); @@ -1174,31 +1188,32 @@ static inline struct net_buf *adjust_offset(struct net_buf *buf, return NULL; } -struct net_buf *net_pkt_read(struct net_buf *buf, uint16_t offset, +struct net_buf *net_frag_read(struct net_buf *frag, uint16_t offset, uint16_t *pos, uint16_t len, uint8_t *data) { uint16_t copy = 0; - buf = adjust_offset(buf, offset, pos); - if (!buf) { + frag = adjust_offset(frag, offset, pos); + if (!frag) { goto error; } - while (len-- > 0 && buf) { + while (len-- > 0 && frag) { if (data) { - buf = net_pkt_read_byte(buf, *pos, pos, data + copy++); + frag = net_frag_read_byte(frag, *pos, + pos, data + copy++); } else { - buf = net_pkt_read_byte(buf, *pos, pos, NULL); + frag = net_frag_read_byte(frag, *pos, pos, NULL); } /* Error: Still reamining length to be read, but no data. */ - if (!buf && len) { + if (!frag && len) { NET_ERR("Not enough data to read"); goto error; } } - return buf; + return frag; error: *pos = 0xffff; @@ -1206,33 +1221,33 @@ error: return NULL; } -struct net_buf *net_pkt_read_be16(struct net_buf *buf, uint16_t offset, - uint16_t *pos, uint16_t *value) +struct net_buf *net_frag_read_be16(struct net_buf *frag, uint16_t offset, + uint16_t *pos, uint16_t *value) { - struct net_buf *retbuf; + struct net_buf *ret_frag; uint8_t v16[2]; - retbuf = net_pkt_read(buf, offset, pos, sizeof(uint16_t), v16); + ret_frag = net_frag_read(frag, offset, pos, sizeof(uint16_t), v16); *value = v16[0] << 8 | v16[1]; - return retbuf; + return ret_frag; } -struct net_buf *net_pkt_read_be32(struct net_buf *buf, uint16_t offset, - uint16_t *pos, uint32_t *value) +struct net_buf *net_frag_read_be32(struct net_buf *frag, uint16_t offset, + uint16_t *pos, uint32_t *value) { - struct net_buf *retbuf; + struct net_buf *ret_frag; uint8_t v32[4]; - retbuf = net_pkt_read(buf, offset, pos, sizeof(uint32_t), v32); + ret_frag = net_frag_read(frag, offset, pos, sizeof(uint32_t), v32); *value = v32[0] << 24 | v32[1] << 16 | v32[2] << 8 | v32[3]; - return retbuf; + return ret_frag; } -static inline struct net_buf *check_and_create_data(struct net_buf *buf, +static inline struct net_buf *check_and_create_data(struct net_pkt *pkt, struct net_buf *data, int32_t timeout) { @@ -1242,17 +1257,17 @@ static inline struct net_buf *check_and_create_data(struct net_buf *buf, return data; } - frag = net_pkt_get_frag(buf, timeout); + frag = net_pkt_get_frag(pkt, timeout); if (!frag) { return NULL; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); return frag; } -static inline struct net_buf *adjust_write_offset(struct net_buf *buf, +static inline struct net_buf *adjust_write_offset(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t *pos, @@ -1261,7 +1276,7 @@ static inline struct net_buf *adjust_write_offset(struct net_buf *buf, uint16_t tailroom; do { - frag = check_and_create_data(buf, frag, timeout); + frag = check_and_create_data(pkt, frag, timeout); if (!frag) { return NULL; } @@ -1288,7 +1303,7 @@ static inline struct net_buf *adjust_write_offset(struct net_buf *buf, *pos = 0; - return check_and_create_data(buf, frag->frags, + return check_and_create_data(pkt, frag->frags, timeout); } @@ -1317,7 +1332,7 @@ static inline struct net_buf *adjust_write_offset(struct net_buf *buf, *pos = 0; - return check_and_create_data(buf, + return check_and_create_data(pkt, frag->frags, timeout); } @@ -1327,7 +1342,7 @@ static inline struct net_buf *adjust_write_offset(struct net_buf *buf, net_buf_add(frag, tailroom); offset -= tailroom; - frag = check_and_create_data(buf, + frag = check_and_create_data(pkt, frag->frags, timeout); } @@ -1338,17 +1353,17 @@ static inline struct net_buf *adjust_write_offset(struct net_buf *buf, return NULL; } -struct net_buf *net_pkt_write(struct net_buf *buf, struct net_buf *frag, +struct net_buf *net_pkt_write(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t *pos, uint16_t len, uint8_t *data, int32_t timeout) { - if (!buf || is_from_data_pool(buf)) { - NET_ERR("Invalid buffer or it is data fragment"); + if (!pkt) { + NET_ERR("Invalid packet"); goto error; } - frag = adjust_write_offset(buf, frag, offset, &offset, timeout); + frag = adjust_write_offset(pkt, frag, offset, &offset, timeout); if (!frag) { NET_DBG("Failed to adjust offset (%u)", offset); goto error; @@ -1381,12 +1396,12 @@ struct net_buf *net_pkt_write(struct net_buf *buf, struct net_buf *frag, frag = frag->frags; if (!frag) { - frag = net_pkt_get_frag(buf, timeout); + frag = net_pkt_get_frag(pkt, timeout); if (!frag) { goto error; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); } } while (1); @@ -1396,7 +1411,7 @@ error: return NULL; } -static inline bool insert_data(struct net_buf *buf, struct net_buf *frag, +static inline bool insert_data(struct net_pkt *pkt, struct net_buf *frag, struct net_buf *temp, uint16_t offset, uint16_t len, uint8_t *data, int32_t timeout) @@ -1423,7 +1438,7 @@ static inline bool insert_data(struct net_buf *buf, struct net_buf *frag, /* As we are creating temporary buffers to cache, * compact the fragments to save space. */ - net_pkt_compact(buf->frags); + net_pkt_compact(pkt); return true; } @@ -1431,7 +1446,7 @@ static inline bool insert_data(struct net_buf *buf, struct net_buf *frag, data += count; offset = 0; - insert = net_pkt_get_frag(buf, timeout); + insert = net_pkt_get_frag(pkt, timeout); if (!insert) { return false; } @@ -1444,33 +1459,32 @@ static inline bool insert_data(struct net_buf *buf, struct net_buf *frag, return false; } -static inline struct net_buf *adjust_insert_offset(struct net_buf *buf, +static inline struct net_buf *adjust_insert_offset(struct net_buf *frag, uint16_t offset, uint16_t *pos) { - if (!buf || !is_from_data_pool(buf)) { - NET_ERR("Invalid buffer or buffer is not a fragment"); - + if (!frag) { + NET_ERR("Invalid fragment"); return NULL; } - while (buf) { - if (offset == buf->len) { + while (frag) { + if (offset == frag->len) { *pos = 0; - return buf->frags; + return frag->frags; } - if (offset < buf->len) { + if (offset < frag->len) { *pos = offset; - return buf; + return frag; } - if (offset > buf->len) { - if (buf->frags) { - offset -= buf->len; - buf = buf->frags; + if (offset > frag->len) { + if (frag->frags) { + offset -= frag->len; + frag = frag->frags; } else { return NULL; } @@ -1482,14 +1496,14 @@ static inline struct net_buf *adjust_insert_offset(struct net_buf *buf, return NULL; } -bool net_pkt_insert(struct net_buf *buf, struct net_buf *frag, +bool net_pkt_insert(struct net_pkt *pkt, struct net_buf *frag, uint16_t offset, uint16_t len, uint8_t *data, int32_t timeout) { struct net_buf *temp = NULL; uint16_t bytes; - if (!buf || is_from_data_pool(buf)) { + if (!pkt) { return false; } @@ -1503,7 +1517,7 @@ bool net_pkt_insert(struct net_buf *buf, struct net_buf *frag, */ bytes = frag->len - offset; if (bytes) { - temp = net_pkt_get_frag(buf, timeout); + temp = net_pkt_get_frag(pkt, timeout); if (!temp) { return false; } @@ -1514,14 +1528,14 @@ bool net_pkt_insert(struct net_buf *buf, struct net_buf *frag, } /* Insert data into current(frag) fragment from "offset". */ - return insert_data(buf, frag, temp, offset, len, data, timeout); + return insert_data(pkt, frag, temp, offset, len, data, timeout); } -int net_pkt_split(struct net_buf *buf, struct net_buf *orig_frag, +int net_pkt_split(struct net_pkt *pkt, struct net_buf *orig_frag, uint16_t len, struct net_buf **fragA, struct net_buf **fragB, int32_t timeout) { - if (!buf || !orig_frag || is_from_data_pool(buf)) { + if (!pkt || !orig_frag) { return -EINVAL; } @@ -1540,7 +1554,7 @@ int net_pkt_split(struct net_buf *buf, struct net_buf *orig_frag, } if (len > orig_frag->len) { - *fragA = net_pkt_get_frag(buf, timeout); + *fragA = net_pkt_get_frag(pkt, timeout); if (!*fragA) { return -ENOMEM; } @@ -1552,14 +1566,14 @@ int net_pkt_split(struct net_buf *buf, struct net_buf *orig_frag, return 0; } - *fragA = net_pkt_get_frag(buf, timeout); + *fragA = net_pkt_get_frag(pkt, timeout); if (!*fragA) { return -ENOMEM; } - *fragB = net_pkt_get_frag(buf, timeout); + *fragB = net_pkt_get_frag(pkt, timeout); if (!*fragB) { - net_pkt_unref(*fragA); + net_pkt_frag_unref(*fragA); return -ENOMEM; } @@ -1570,8 +1584,8 @@ int net_pkt_split(struct net_buf *buf, struct net_buf *orig_frag, return 0; } -void net_pkt_get_info(struct net_buf_pool **rx, - struct net_buf_pool **tx, +void net_pkt_get_info(struct k_mem_slab **rx, + struct k_mem_slab **tx, struct net_buf_pool **rx_data, struct net_buf_pool **tx_data) { @@ -1595,18 +1609,23 @@ void net_pkt_get_info(struct net_buf_pool **rx, #if defined(CONFIG_NET_DEBUG_NET_PKT) void net_pkt_print(void) { - NET_DBG("TX %d RX %d RDATA %d TDATA %d", tx_bufs.avail_count, - rx_bufs.avail_count, rx_bufs.avail_count, - tx_bufs.avail_count); + NET_DBG("TX %u RX %u RDATA %d TDATA %d", + k_mem_slab_num_free_get(&tx_pkts), + k_mem_slab_num_free_get(&rx_pkts), + rx_bufs.avail_count, tx_bufs.avail_count); } #endif /* CONFIG_NET_DEBUG_NET_PKT */ void net_pkt_init(void) { - NET_DBG("Allocating %d RX (%u bytes), %d TX (%u bytes), " + NET_DBG("Allocating %u RX (%zu bytes), %u TX (%zu bytes), " "%d RX data (%u bytes) and %d TX data (%u bytes) buffers", - get_frees(&rx_pkts), rx_pkts.pool_size, - get_frees(&tx_pkts), tx_pkts.pool_size, + k_mem_slab_num_free_get(&rx_pkts), + (size_t)(k_mem_slab_num_free_get(&rx_pkts) * + sizeof(struct net_pkt)), + k_mem_slab_num_free_get(&tx_pkts), + (size_t)(k_mem_slab_num_free_get(&tx_pkts) * + sizeof(struct net_pkt)), get_frees(&rx_bufs), rx_bufs.pool_size, get_frees(&tx_bufs), tx_bufs.pool_size); } diff --git a/subsys/net/ip/net_private.h b/subsys/net/ip/net_private.h index 423c7da6d01..93e846b888c 100644 --- a/subsys/net/ip/net_private.h +++ b/subsys/net/ip/net_private.h @@ -19,12 +19,12 @@ extern void net_pkt_init(void); extern void net_if_init(struct k_sem *startup_sync); extern void net_if_post_init(void); extern void net_context_init(void); -enum net_verdict net_ipv4_process_pkt(struct net_buf *buf); -enum net_verdict net_ipv6_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_pkt *pkt); extern void net_ipv6_init(void); #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); #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_sprint_ll_addr_buf(const uint8_t *ll, uint8_t ll_len, 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) -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 */ -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 @@ -143,26 +143,26 @@ static inline void net_hexdump(const char *str, const uint8_t *packet, } /* 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) { - net_hexdump(str, frag->data, net_pkt_get_len(frag)); + net_hexdump(str, frag->data, frag->len); frag = frag->frags; } } /* 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) { printk("%s", str); } - printk("%p[%d]", buf, buf->ref); + printk("%p[%d]", pkt, pkt->ref); if (frag) { printk("->"); diff --git a/subsys/net/ip/net_shell.c b/subsys/net/ip/net_shell.c index 61e482b895d..3296f80b3d7 100644 --- a/subsys/net/ip/net_shell.c +++ b/subsys/net/ip/net_shell.c @@ -509,7 +509,8 @@ static void ipv6_frag_cb(struct net_ipv6_reassembly *reass, #endif /* CONFIG_NET_IPV6_FRAGMENT */ #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, int line_alloc, 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 (in_use) { 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); #if defined(CONFIG_NET_DEBUG_NET_PKT) - printk("Network buffer allocations\n\n"); - printk("net_buf\t\tStatus\tPool\tFunction alloc -> freed\n"); + printk("Network memory allocations\n\n"); + printk("memory\t\tStatus\tPool\tFunction alloc -> freed\n"); net_pkt_allocs_foreach(allocs_cb, NULL); #else 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 { int pos; 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]; }; #if defined(CONFIG_NET_CONTEXT_NET_PKT_POOL) -static bool pool_found_already(struct ctx_info *info, - struct net_buf_pool *pool) +static bool slab_pool_found_already(struct ctx_info *info, + struct k_mem_slab *slab, + struct net_buf_pool *pool) { int i; for (i = 0; i < CONFIG_NET_MAX_CONTEXTS; i++) { - if (info->tx_pools[i] == pool || - info->data_pools[i] == pool) { - return true; + if (slab) { + if (info->tx_slabs[i] == slab) { + 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) struct ctx_info *info = user_data; + struct k_mem_slab *slab; struct net_buf_pool *pool; if (!net_context_is_used(context)) { return; } - if (context->tx_pool) { - pool = context->tx_pool(); + if (context->tx_slab) { + slab = context->tx_slab(); - if (pool_found_already(info, pool)) { + if (slab_pool_found_already(info, slab, NULL)) { return; } #if defined(CONFIG_NET_DEBUG_NET_PKT) - printk("ETX (%s)\t%d\t%d\t%d\t%p\n", - pool->name, pool->pool_size, pool->buf_count, - pool->avail_count, pool); + printk("ETX\t%zu\t%u\t%u\t%p\n", + slab->num_blocks * slab->block_size, + slab->num_blocks, k_mem_slab_num_free_get(slab), slab); #else - printk("ETX \t%d\t%p\n", pool->buf_count, pool); + printk("ETX \t%d\t%p\n", slab->num_blocks, slab); #endif info->are_external_pools = true; - info->tx_pools[info->pos] = pool; + info->tx_slabs[info->pos] = slab; } if (context->data_pool) { pool = context->data_pool(); - if (pool_found_already(info, pool)) { + if (slab_pool_found_already(info, NULL, pool)) { 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[]) { - 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(argv); @@ -893,11 +920,13 @@ static int shell_cmd_mem(int argc, char *argv[]) #if defined(CONFIG_NET_DEBUG_NET_PKT) printk("Name\t\t\tSize\tCount\tAvail\tAddress\n"); - printk("RX (%s)\t\t%d\t%d\t%d\t%p\n", - rx->name, rx->pool_size, rx->buf_count, rx->avail_count, rx); + printk("RX\t\t%zu\t%d\t%u\t%p\n", + 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", - tx->name, tx->pool_size, tx->buf_count, tx->avail_count, tx); + printk("TX\t\t%zu\t%d\t%u\t%p\n", + 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", 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 printk("Name \tCount\tAddress\n"); - printk("RX \t%d\t%p\n", rx->buf_count, rx); - printk("TX \t%d\t%p\n", tx->buf_count, tx); + printk("RX \t%d\t%p\n", rx->num_blocks, rx); + 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("TX DATA \t%d\t%p\n", tx_data->buf_count, tx_data); #endif /* CONFIG_NET_DEBUG_NET_PKT */ @@ -1018,7 +1047,7 @@ K_SEM_DEFINE(ping_timeout, 0, 1); #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 = { .type = NET_ICMPV6_ECHO_REPLY, @@ -1031,15 +1060,15 @@ static inline void _remove_ipv6_ping_handler(void) 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]; 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", - 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); _remove_ipv6_ping_handler(); @@ -1077,7 +1106,7 @@ static int _ping_ipv6(char *host) #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 = { .type = NET_ICMPV4_ECHO_REPLY, @@ -1090,15 +1119,15 @@ static inline void _remove_ipv4_ping_handler(void) 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]; 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", - 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); _remove_ipv4_ping_handler(); @@ -1470,7 +1499,7 @@ static int shell_cmd_tcp(int argc, char *argv[]) if (!strcmp(argv[arg], "send")) { /* tcp send */ - struct net_buf *buf; + struct net_pkt *pkt; if (!tcp_ctx || !net_context_is_used(tcp_ctx)) { printk("Not connected\n"); @@ -1482,25 +1511,25 @@ static int shell_cmd_tcp(int argc, char *argv[]) return 0; } - buf = net_pkt_get_tx(tcp_ctx, TCP_TIMEOUT); - if (!buf) { - printk("Out of bufs, msg cannot be sent.\n"); + pkt = net_pkt_get_tx(tcp_ctx, TCP_TIMEOUT); + if (!pkt) { + printk("Out of pkts, msg cannot be sent.\n"); return 0; } - ret = net_pkt_append(buf, strlen(argv[arg]), + ret = net_pkt_append(pkt, strlen(argv[arg]), argv[arg], TCP_TIMEOUT); if (!ret) { - printk("Cannot build msg (out of bufs)\n"); - net_pkt_unref(buf); + printk("Cannot build msg (out of pkts)\n"); + net_pkt_unref(pkt); return 0; } - ret = net_context_send(buf, tcp_sent_cb, TCP_TIMEOUT, + ret = net_context_send(pkt, tcp_sent_cb, TCP_TIMEOUT, NULL, NULL); if (ret < 0) { printk("Cannot send msg (%d)\n", ret); - net_pkt_unref(buf); + net_pkt_unref(pkt); return 0; } @@ -1550,14 +1579,14 @@ static int shell_cmd_help(int argc, char *argv[]) ARG_UNUSED(argv); /* 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 dns\n\tShow how DNS is configured\n"); printk("net dns cancel\n\tCancel all pending requests\n"); printk("net dns [A or AAAA]\n\tQuery IPv4 address (default)" " or IPv6 address for a host name\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 rm \n\tRemove neighbor from cache\n"); printk("net ping \n\tPing a network host\n"); diff --git a/subsys/net/ip/route.c b/subsys/net/ip/route.c index 446ffe808d5..e188c0425c6 100644 --- a/subsys/net/ip/route.c +++ b/subsys/net/ip/route.c @@ -716,12 +716,12 @@ bool net_route_get_info(struct net_if *iface, 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_nbr *nbr; - nbr = net_ipv6_nbr_lookup(net_pkt_iface(buf), nexthop); + nbr = net_ipv6_nbr_lookup(net_pkt_iface(pkt), nexthop); if (!nbr) { NET_DBG("Cannot find %s neighbor.", 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, * 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"); return -EINVAL; } - net_pkt_set_forwarding(buf, true); + net_pkt_set_forwarding(pkt, true); /* Set the destination and source ll address in the packet. * 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(buf)->type = net_pkt_ll_if(buf)->type; - net_pkt_ll_src(buf)->len = net_pkt_ll_if(buf)->len; + net_pkt_ll_src(pkt)->addr = net_pkt_ll_if(pkt)->addr; + net_pkt_ll_src(pkt)->type = net_pkt_ll_if(pkt)->type; + net_pkt_ll_src(pkt)->len = net_pkt_ll_if(pkt)->len; - net_pkt_ll_dst(buf)->addr = lladdr->addr; - net_pkt_ll_dst(buf)->type = lladdr->type; - net_pkt_ll_dst(buf)->len = lladdr->len; + net_pkt_ll_dst(pkt)->addr = lladdr->addr; + net_pkt_ll_dst(pkt)->type = lladdr->type; + net_pkt_ll_dst(pkt)->len = lladdr->len; - return net_send_data(buf); + return net_send_data(pkt); } void net_route_init(void) diff --git a/subsys/net/ip/route.h b/subsys/net/ip/route.h index 7b440750e9f..498500b7b07 100644 --- a/subsys/net/ip/route.h +++ b/subsys/net/ip/route.h @@ -251,12 +251,12 @@ bool net_route_get_info(struct net_if *iface, /** * @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. * * @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 */ #define net_route_init(...) diff --git a/subsys/net/ip/rpl.c b/subsys/net/ip/rpl.c index 7b1133fcdb1..4578c2334c2 100644 --- a/subsys/net/ip/rpl.c +++ b/subsys/net/ip/rpl.c @@ -216,17 +216,17 @@ NET_NBR_TABLE_INIT(NET_NBR_LOCAL, rpl_parents, net_rpl_neighbor_pool, net_rpl_neighbor_table_clear); #if defined(CONFIG_NET_DEBUG_RPL) -#define net_rpl_info(buf, req) \ +#define net_rpl_info(pkt, req) \ do { \ char out[NET_IPV6_ADDR_LEN]; \ \ 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 %s from %s to %s", req, \ - net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src), out); \ + net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src), out); \ } while (0) -#define net_rpl_dao_info(buf, src, dst, prefix) \ +#define net_rpl_dao_info(pkt, src, dst, prefix) \ do { \ char out[NET_IPV6_ADDR_LEN]; \ char prf[NET_IPV6_ADDR_LEN]; \ @@ -239,7 +239,7 @@ NET_NBR_TABLE_INIT(NET_NBR_LOCAL, rpl_parents, net_rpl_neighbor_pool, prf, net_sprint_ipv6_addr(src), out); \ } while (0) -#define net_rpl_dao_ack_info(buf, src, dst, id, seq) \ +#define net_rpl_dao_ack_info(pkt, src, dst, id, seq) \ do { \ char out[NET_IPV6_ADDR_LEN]; \ \ @@ -418,12 +418,12 @@ struct net_route_entry *net_rpl_add_route(struct net_rpl_dag *dag, return route; } -static inline void setup_icmpv6_hdr(struct net_buf *buf, uint8_t type, +static inline void setup_icmpv6_hdr(struct net_pkt *pkt, uint8_t type, uint8_t code) { - net_pkt_append_u8(buf, type); - net_pkt_append_u8(buf, code); - net_pkt_append_be16(buf, 0); /* checksum */ + net_pkt_append_u8(pkt, type); + net_pkt_append_u8(pkt, code); + net_pkt_append_be16(pkt, 0); /* checksum */ } int net_rpl_dio_send(struct net_if *iface, @@ -433,13 +433,13 @@ int net_rpl_dio_send(struct net_if *iface, { struct net_rpl_dag *dag = instance->current_dag; struct in6_addr addr, *dst_addr; - struct net_buf *buf; + struct net_pkt *pkt; uint16_t value; int ret; - 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); - if (!buf) { + if (!pkt) { return -ENOMEM; } @@ -450,92 +450,92 @@ int net_rpl_dio_send(struct net_if *iface, dst_addr = dst; } - buf = net_ipv6_create_raw(buf, src, dst_addr, iface, IPPROTO_ICMPV6); + pkt = net_ipv6_create_raw(pkt, src, dst_addr, iface, IPPROTO_ICMPV6); - net_pkt_set_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - setup_icmpv6_hdr(buf, NET_ICMPV6_RPL, NET_RPL_DODAG_INFO_OBJ); + setup_icmpv6_hdr(pkt, NET_ICMPV6_RPL, NET_RPL_DODAG_INFO_OBJ); - net_pkt_append_u8(buf, instance->instance_id); - net_pkt_append_u8(buf, dag->version); - net_pkt_append_be16(buf, dag->rank); + net_pkt_append_u8(pkt, instance->instance_id); + net_pkt_append_u8(pkt, dag->version); + net_pkt_append_be16(pkt, dag->rank); value = net_rpl_dag_is_grounded(dag) << 8; value |= instance->mop << NET_RPL_DIO_MOP_SHIFT; value |= net_rpl_dag_get_preference(dag) & NET_RPL_DIO_PREFERENCE_MASK; - net_pkt_append_u8(buf, value); - net_pkt_append_u8(buf, instance->dtsn); + net_pkt_append_u8(pkt, value); + net_pkt_append_u8(pkt, instance->dtsn); if (!dst) { net_rpl_lollipop_increment(&instance->dtsn); } /* Flags and reserved are set to 0 */ - net_pkt_append_be16(buf, 0); + net_pkt_append_be16(pkt, 0); - net_pkt_append(buf, sizeof(struct in6_addr), dag->dag_id.s6_addr, - K_FOREVER); + net_pkt_append(pkt, sizeof(struct in6_addr), dag->dag_id.s6_addr, + K_FOREVER); if (instance->mc.type != NET_RPL_MC_NONE) { net_rpl_of_update_mc(instance); - net_pkt_append_u8(buf, NET_RPL_OPTION_DAG_METRIC_CONTAINER); - net_pkt_append_u8(buf, 6); - net_pkt_append_u8(buf, instance->mc.type); - net_pkt_append_u8(buf, instance->mc.flags >> 1); + net_pkt_append_u8(pkt, NET_RPL_OPTION_DAG_METRIC_CONTAINER); + net_pkt_append_u8(pkt, 6); + net_pkt_append_u8(pkt, instance->mc.type); + net_pkt_append_u8(pkt, instance->mc.flags >> 1); value = (instance->mc.flags & 1) << 7; - net_pkt_append_u8(buf, value | - (instance->mc.aggregated << 4) | - instance->mc.precedence); + net_pkt_append_u8(pkt, value | + (instance->mc.aggregated << 4) | + instance->mc.precedence); if (instance->mc.type == NET_RPL_MC_ETX) { - net_pkt_append_u8(buf, 2); - net_pkt_append_be16(buf, instance->mc.obj.etx); + net_pkt_append_u8(pkt, 2); + net_pkt_append_be16(pkt, instance->mc.obj.etx); } else if (instance->mc.type == NET_RPL_MC_ENERGY) { - net_pkt_append_u8(buf, 2); - net_pkt_append_u8(buf, instance->mc.obj.energy.flags); - net_pkt_append_u8(buf, + net_pkt_append_u8(pkt, 2); + net_pkt_append_u8(pkt, instance->mc.obj.energy.flags); + net_pkt_append_u8(pkt, instance->mc.obj.energy.estimation); } else { NET_DBG("Cannot send DIO, unknown DAG MC type %u", instance->mc.type); - net_pkt_unref(buf); + net_pkt_unref(pkt); return -EINVAL; } } - net_pkt_append_u8(buf, NET_RPL_OPTION_DAG_CONF); - net_pkt_append_u8(buf, 14); - net_pkt_append_u8(buf, 0); /* No Auth */ - net_pkt_append_u8(buf, instance->dio_interval_doublings); - net_pkt_append_u8(buf, instance->dio_interval_min); - net_pkt_append_u8(buf, instance->dio_redundancy); - net_pkt_append_be16(buf, instance->max_rank_inc); - net_pkt_append_be16(buf, instance->min_hop_rank_inc); + net_pkt_append_u8(pkt, NET_RPL_OPTION_DAG_CONF); + net_pkt_append_u8(pkt, 14); + net_pkt_append_u8(pkt, 0); /* No Auth */ + net_pkt_append_u8(pkt, instance->dio_interval_doublings); + net_pkt_append_u8(pkt, instance->dio_interval_min); + net_pkt_append_u8(pkt, instance->dio_redundancy); + net_pkt_append_be16(pkt, instance->max_rank_inc); + net_pkt_append_be16(pkt, instance->min_hop_rank_inc); - net_pkt_append_be16(buf, instance->ocp); - net_pkt_append_u8(buf, 0); /* Reserved */ - net_pkt_append_u8(buf, instance->default_lifetime); - net_pkt_append_be16(buf, instance->lifetime_unit); + net_pkt_append_be16(pkt, instance->ocp); + net_pkt_append_u8(pkt, 0); /* Reserved */ + net_pkt_append_u8(pkt, instance->default_lifetime); + net_pkt_append_be16(pkt, instance->lifetime_unit); if (dag->prefix_info.length > 0) { - net_pkt_append_u8(buf, NET_RPL_OPTION_PREFIX_INFO); - net_pkt_append_u8(buf, 30); /* length */ - net_pkt_append_u8(buf, dag->prefix_info.length); - net_pkt_append_u8(buf, dag->prefix_info.flags); + net_pkt_append_u8(pkt, NET_RPL_OPTION_PREFIX_INFO); + net_pkt_append_u8(pkt, 30); /* length */ + net_pkt_append_u8(pkt, dag->prefix_info.length); + net_pkt_append_u8(pkt, dag->prefix_info.flags); /* First valid lifetime and the second one is * preferred lifetime. */ - net_pkt_append_be32(buf, dag->prefix_info.lifetime); - net_pkt_append_be32(buf, dag->prefix_info.lifetime); + net_pkt_append_be32(pkt, dag->prefix_info.lifetime); + net_pkt_append_be32(pkt, dag->prefix_info.lifetime); - net_pkt_append_be32(buf, 0); /* reserved */ - net_pkt_append(buf, sizeof(struct in6_addr), - dag->prefix_info.prefix.s6_addr, - K_FOREVER); + net_pkt_append_be32(pkt, 0); /* reserved */ + net_pkt_append(pkt, sizeof(struct in6_addr), + dag->prefix_info.prefix.s6_addr, + K_FOREVER); NET_DBG("Sending prefix info in DIO for %s", net_sprint_ipv6_addr(&dag->prefix_info.prefix)); @@ -544,13 +544,13 @@ int net_rpl_dio_send(struct net_if *iface, dag->prefix_info.length); } - ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6); + ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6); if (ret < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return ret; } - ret = net_send_data(buf); + ret = net_send_data(pkt); if (ret >= 0) { if (!dst) { NET_DBG("Sent a multicast DIO with rank %d", @@ -567,7 +567,7 @@ int net_rpl_dio_send(struct net_if *iface, return 0; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return ret; } @@ -703,7 +703,7 @@ int net_rpl_dis_send(struct in6_addr *dst, struct net_if *iface) { struct in6_addr addr, *dst_addr; const struct in6_addr *src; - struct net_buf *buf; + struct net_pkt *pkt; uint16_t pos; int ret; @@ -721,34 +721,34 @@ int net_rpl_dis_send(struct in6_addr *dst, struct net_if *iface) dst_addr = dst; } - buf = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, dst_addr), + pkt = net_pkt_get_reserve_tx(net_if_get_ll_reserve(iface, dst_addr), K_FOREVER); - if (!buf) { + if (!pkt) { return -ENOMEM; } src = net_if_ipv6_select_src_addr(iface, dst_addr); - buf = net_ipv6_create_raw(buf, src, dst_addr, iface, IPPROTO_ICMPV6); + pkt = net_ipv6_create_raw(pkt, src, dst_addr, iface, IPPROTO_ICMPV6); - net_pkt_set_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - setup_icmpv6_hdr(buf, NET_ICMPV6_RPL, NET_RPL_DODAG_SOLICIT); + setup_icmpv6_hdr(pkt, NET_ICMPV6_RPL, NET_RPL_DODAG_SOLICIT); /* Add flags and reserved fields */ - net_pkt_write_u8(buf, buf->frags, - sizeof(struct net_ipv6_hdr) + - sizeof(struct net_icmp_hdr), - &pos, 0); - net_pkt_write_u8(buf, buf->frags, pos, &pos, 0); + net_pkt_write_u8(pkt, pkt->frags, + sizeof(struct net_ipv6_hdr) + + sizeof(struct net_icmp_hdr), + &pos, 0); + net_pkt_write_u8(pkt, pkt->frags, pos, &pos, 0); - ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6); + ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6); if (ret < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return ret; } - ret = net_send_data(buf); + ret = net_send_data(pkt); if (ret >= 0) { NET_DBG("Sent a %s DIS to %s", dst ? "unicast" : "multicast", net_sprint_ipv6_addr(dst_addr)); @@ -756,17 +756,17 @@ int net_rpl_dis_send(struct in6_addr *dst, struct net_if *iface) net_stats_update_icmp_sent(); net_stats_update_rpl_dis_sent(); } else { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return ret; } -static enum net_verdict handle_dis(struct net_buf *buf) +static enum net_verdict handle_dis(struct net_pkt *pkt) { struct net_rpl_instance *instance; - net_rpl_info(buf, "DODAG Information Solicitation"); + net_rpl_info(pkt, "DODAG Information Solicitation"); for (instance = &rpl_instances[0]; instance < &rpl_instances[0] + CONFIG_NET_RPL_MAX_INSTANCES; @@ -775,17 +775,17 @@ static enum net_verdict handle_dis(struct net_buf *buf) continue; } - if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { + if (net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) { net_rpl_dio_reset_timer(instance); } else { - net_rpl_dio_send(net_pkt_iface(buf), + net_rpl_dio_send(net_pkt_iface(pkt), instance, - &NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst); + &NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst); } } - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } @@ -2710,7 +2710,7 @@ static void net_rpl_process_dio(struct net_if *iface, parent->dtsn = dio->dtsn; } -static enum net_verdict handle_dio(struct net_buf *buf) +static enum net_verdict handle_dio(struct net_pkt *pkt) { struct net_rpl_dio dio = { 0 }; struct net_buf *frag; @@ -2719,7 +2719,7 @@ static enum net_verdict handle_dio(struct net_buf *buf) uint8_t subopt_type; uint8_t flags, len, tmp; - net_rpl_info(buf, "DODAG Information Object"); + net_rpl_info(pkt, "DODAG Information Object"); /* Default values can be overwritten by DIO config option. */ @@ -2732,15 +2732,15 @@ static enum net_verdict handle_dio(struct net_buf *buf) dio.default_lifetime = CONFIG_NET_RPL_DEFAULT_LIFETIME; dio.lifetime_unit = CONFIG_NET_RPL_DEFAULT_LIFETIME_UNIT; - nbr = net_ipv6_nbr_lookup(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->src); + nbr = net_ipv6_nbr_lookup(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->src); if (!nbr) { - NET_ASSERT_INFO(net_pkt_ll_src(buf)->len, + NET_ASSERT_INFO(net_pkt_ll_src(pkt)->len, "Link layer address not set"); - nbr = net_ipv6_nbr_add(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->src, - net_pkt_ll_src(buf), + nbr = net_ipv6_nbr_add(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->src, + net_pkt_ll_src(pkt), 0, NET_IPV6_NBR_STATE_REACHABLE); if (!nbr) { @@ -2748,35 +2748,35 @@ static enum net_verdict handle_dio(struct net_buf *buf) goto out; } - net_ipv6_nbr_set_reachable_timer(net_pkt_iface(buf), nbr); + net_ipv6_nbr_set_reachable_timer(net_pkt_iface(pkt), nbr); } /* offset tells now where the ICMPv6 header is starting */ - offset = net_pkt_icmp_data(buf) - net_pkt_ip_data(buf); + offset = net_pkt_icmp_data(pkt) - net_pkt_ip_data(pkt); offset += sizeof(struct net_icmp_hdr); /* First the DIO option. */ - frag = net_pkt_read_u8(buf->frags, offset, &pos, &dio.instance_id); - frag = net_pkt_read_u8(frag, pos, &pos, &dio.version); - frag = net_pkt_read_be16(frag, pos, &pos, &dio.rank); + frag = net_frag_read_u8(pkt->frags, offset, &pos, &dio.instance_id); + frag = net_frag_read_u8(frag, pos, &pos, &dio.version); + frag = net_frag_read_be16(frag, pos, &pos, &dio.rank); NET_DBG("Incoming DIO len %zu id %d ver %d rank %d", - net_buf_frags_len(buf) - offset, + net_pkt_get_len(pkt) - offset, dio.instance_id, dio.version, dio.rank); - frag = net_pkt_read_u8(frag, pos, &pos, &flags); + frag = net_frag_read_u8(frag, pos, &pos, &flags); dio.grounded = flags & NET_RPL_DIO_GROUNDED; dio.mop = (flags & NET_RPL_DIO_MOP_MASK) >> NET_RPL_DIO_MOP_SHIFT; dio.preference = flags & NET_RPL_DIO_PREFERENCE_MASK; - frag = net_pkt_read_u8(frag, pos, &pos, &dio.dtsn); + frag = net_frag_read_u8(frag, pos, &pos, &dio.dtsn); /* two reserved bytes */ - frag = net_pkt_skip(frag, pos, &pos, 2); + frag = net_frag_skip(frag, pos, &pos, 2); - frag = net_pkt_read(frag, pos, &pos, sizeof(dio.dag_id), + frag = net_frag_read(frag, pos, &pos, sizeof(dio.dag_id), dio.dag_id.s6_addr); NET_DBG("Incoming DIO dag_id %s pref %d", @@ -2785,7 +2785,7 @@ static enum net_verdict handle_dio(struct net_buf *buf) /* Handle any DIO suboptions */ while (frag) { len = 0; - frag = net_pkt_read_u8(frag, pos, &pos, &subopt_type); + frag = net_frag_read_u8(frag, pos, &pos, &subopt_type); if (!frag && pos == 0) { /* We are at the end of the message */ break; @@ -2797,7 +2797,7 @@ static enum net_verdict handle_dio(struct net_buf *buf) if (subopt_type != NET_RPL_OPTION_PAD1) { /* Suboption with a two-byte header + payload */ - frag = net_pkt_read_u8(frag, pos, &pos, &len); + frag = net_frag_read_u8(frag, pos, &pos, &len); len += 2; } @@ -2810,7 +2810,7 @@ static enum net_verdict handle_dio(struct net_buf *buf) break; case NET_RPL_OPTION_PADN: /* Skip padding bytes */ - frag = net_pkt_skip(frag, pos, &pos, len - 2); + frag = net_frag_skip(frag, pos, &pos, len - 2); break; case NET_RPL_OPTION_DAG_METRIC_CONTAINER: if (len < 6) { @@ -2819,19 +2819,19 @@ static enum net_verdict handle_dio(struct net_buf *buf) goto out; } - frag = net_pkt_read_u8(frag, pos, &pos, &dio.mc.type); - frag = net_pkt_read_u8(frag, pos, &pos, &tmp); + frag = net_frag_read_u8(frag, pos, &pos, &dio.mc.type); + frag = net_frag_read_u8(frag, pos, &pos, &tmp); dio.mc.flags = tmp << 1; - frag = net_pkt_read_u8(frag, pos, &pos, &tmp); + frag = net_frag_read_u8(frag, pos, &pos, &tmp); dio.mc.flags |= tmp >> 7; dio.mc.aggregated = (tmp >> 4) & 0x3; dio.mc.precedence = tmp & 0xf; - frag = net_pkt_read_u8(frag, pos, &pos, + frag = net_frag_read_u8(frag, pos, &pos, &dio.mc.length); if (dio.mc.type == NET_RPL_MC_ETX) { - frag = net_pkt_read_be16(frag, pos, &pos, - &dio.mc.obj.etx); + frag = net_frag_read_be16(frag, pos, &pos, + &dio.mc.obj.etx); NET_DBG("DAG MC type %d flags %d aggr %d " "prec %d length %d ETX %d", @@ -2841,9 +2841,9 @@ static enum net_verdict handle_dio(struct net_buf *buf) dio.mc.obj.etx); } else if (dio.mc.type == NET_RPL_MC_ENERGY) { - frag = net_pkt_read_u8(frag, pos, &pos, - &dio.mc.obj.energy.flags); - frag = net_pkt_read_u8(frag, pos, &pos, + frag = net_frag_read_u8(frag, pos, &pos, + &dio.mc.obj.energy.flags); + frag = net_frag_read_u8(frag, pos, &pos, &dio.mc.obj.energy.estimation); } else { NET_DBG("Unhandled DAG MC type %d", @@ -2860,16 +2860,16 @@ static enum net_verdict handle_dio(struct net_buf *buf) goto out; } - frag = net_pkt_read_u8(frag, pos, &pos, + frag = net_frag_read_u8(frag, pos, &pos, &dio.destination_prefix.length); - frag = net_pkt_read_u8(frag, pos, &pos, + frag = net_frag_read_u8(frag, pos, &pos, &dio.destination_prefix.flags); - frag = net_pkt_read_be32(frag, pos, &pos, + frag = net_frag_read_be32(frag, pos, &pos, &dio.destination_prefix.lifetime); if (((dio.destination_prefix.length + 7) / 8) + 8 <= len && dio.destination_prefix.length <= 128) { - frag = net_pkt_read(frag, pos, &pos, + frag = net_frag_read(frag, pos, &pos, (dio.destination_prefix.length + 7) / 8, dio.destination_prefix.prefix.s6_addr); @@ -2894,28 +2894,28 @@ static enum net_verdict handle_dio(struct net_buf *buf) } /* Path control field not yet implemented (1 byte) */ - frag = net_pkt_skip(frag, pos, &pos, 1); + frag = net_frag_skip(frag, pos, &pos, 1); - frag = net_pkt_read_u8(frag, pos, &pos, - &dio.dag_interval_doublings); - frag = net_pkt_read_u8(frag, pos, &pos, - &dio.dag_interval_min); - frag = net_pkt_read_u8(frag, pos, &pos, - &dio.dag_redundancy); - frag = net_pkt_read_be16(frag, pos, &pos, - &dio.max_rank_inc); - frag = net_pkt_read_be16(frag, pos, &pos, - &dio.min_hop_rank_inc); - frag = net_pkt_read_be16(frag, pos, &pos, - &dio.ocp); + frag = net_frag_read_u8(frag, pos, &pos, + &dio.dag_interval_doublings); + frag = net_frag_read_u8(frag, pos, &pos, + &dio.dag_interval_min); + frag = net_frag_read_u8(frag, pos, &pos, + &dio.dag_redundancy); + frag = net_frag_read_be16(frag, pos, &pos, + &dio.max_rank_inc); + frag = net_frag_read_be16(frag, pos, &pos, + &dio.min_hop_rank_inc); + frag = net_frag_read_be16(frag, pos, &pos, + &dio.ocp); /* one reserved byte */ - frag = net_pkt_skip(frag, pos, &pos, 1); + frag = net_frag_skip(frag, pos, &pos, 1); - frag = net_pkt_read_u8(frag, pos, &pos, - &dio.default_lifetime); - frag = net_pkt_read_be16(frag, pos, &pos, - &dio.lifetime_unit); + frag = net_frag_read_u8(frag, pos, &pos, + &dio.default_lifetime); + frag = net_frag_read_be16(frag, pos, &pos, + &dio.lifetime_unit); NET_DBG("DAG conf dbl %d min %d red %d maxinc %d " "mininc %d ocp %d d_l %d l_u %d", @@ -2934,24 +2934,24 @@ static enum net_verdict handle_dio(struct net_buf *buf) goto out; } - frag = net_pkt_read_u8(frag, pos, &pos, - &dio.prefix_info.length); - frag = net_pkt_read_u8(frag, pos, &pos, - &dio.prefix_info.flags); + frag = net_frag_read_u8(frag, pos, &pos, + &dio.prefix_info.length); + frag = net_frag_read_u8(frag, pos, &pos, + &dio.prefix_info.flags); /* skip valid lifetime atm */ - frag = net_pkt_skip(frag, pos, &pos, 4); + frag = net_frag_skip(frag, pos, &pos, 4); /* preferred lifetime stored in lifetime */ - frag = net_pkt_read_be32(frag, pos, &pos, - &dio.prefix_info.lifetime); + frag = net_frag_read_be32(frag, pos, &pos, + &dio.prefix_info.lifetime); /* 32-bit reserved */ - frag = net_pkt_skip(frag, pos, &pos, 4); + frag = net_frag_skip(frag, pos, &pos, 4); - frag = net_pkt_read(frag, pos, &pos, - sizeof(struct in6_addr), - dio.prefix_info.prefix.s6_addr); + frag = net_frag_read(frag, pos, &pos, + sizeof(struct in6_addr), + dio.prefix_info.prefix.s6_addr); NET_DBG("Prefix %s/%d", net_sprint_ipv6_addr(&dio.prefix_info.prefix), @@ -2961,14 +2961,14 @@ static enum net_verdict handle_dio(struct net_buf *buf) default: NET_DBG("Unsupported suboption type in DIO %d", subopt_type); - frag = net_pkt_skip(frag, pos, &pos, len - 2); + frag = net_frag_skip(frag, pos, &pos, len - 2); break; } } NET_ASSERT_INFO(!pos && !frag, "DIO reading failure"); - net_rpl_process_dio(net_pkt_iface(buf), &NET_IPV6_BUF(buf)->src, &dio); + net_rpl_process_dio(net_pkt_iface(pkt), &NET_IPV6_BUF(pkt)->src, &dio); out: return NET_DROP; @@ -2984,7 +2984,7 @@ int net_rpl_dao_send(struct net_if *iface, const struct in6_addr *src; struct net_rpl_dag *dag; struct in6_addr *dst; - struct net_buf *buf; + struct net_pkt *pkt; uint8_t prefix_bytes; uint8_t prefixlen; int ret; @@ -3022,21 +3022,21 @@ int net_rpl_dao_send(struct net_if *iface, return -EINVAL; } - 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); - if (!buf) { + if (!pkt) { return -ENOMEM; } - 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_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - setup_icmpv6_hdr(buf, NET_ICMPV6_RPL, NET_RPL_DEST_ADV_OBJ); + setup_icmpv6_hdr(pkt, NET_ICMPV6_RPL, NET_RPL_DEST_ADV_OBJ); net_rpl_lollipop_increment(&rpl_dao_sequence); - net_pkt_append_u8(buf, instance->instance_id); + net_pkt_append_u8(pkt, instance->instance_id); #if defined(CONFIG_NET_RPL_DAO_SPECIFY_DAG) value |= NET_RPL_DAO_D_FLAG; @@ -3045,45 +3045,45 @@ int net_rpl_dao_send(struct net_if *iface, #if defined(CONFIG_NET_RPL_DAO_ACK) value |= NET_RPL_DAO_K_FLAG; #endif - net_pkt_append_u8(buf, value); - net_pkt_append_u8(buf, 0); /* reserved */ - net_pkt_append_u8(buf, rpl_dao_sequence); + net_pkt_append_u8(pkt, value); + net_pkt_append_u8(pkt, 0); /* reserved */ + net_pkt_append_u8(pkt, rpl_dao_sequence); #if defined(CONFIG_NET_RPL_DAO_SPECIFY_DAG) - net_pkt_append(buf, sizeof(dag->dag_id), dag->dag_id.s6_addr, + net_pkt_append(pkt, sizeof(dag->dag_id), dag->dag_id.s6_addr, K_FOREVER); #endif prefixlen = sizeof(*prefix) * CHAR_BIT; prefix_bytes = (prefixlen + 7) / CHAR_BIT; - net_pkt_append_u8(buf, NET_RPL_OPTION_TARGET); - net_pkt_append_u8(buf, 2 + prefix_bytes); - net_pkt_append_u8(buf, 0); /* reserved */ - net_pkt_append_u8(buf, prefixlen); - net_pkt_append(buf, prefix_bytes, prefix->s6_addr, K_FOREVER); + net_pkt_append_u8(pkt, NET_RPL_OPTION_TARGET); + net_pkt_append_u8(pkt, 2 + prefix_bytes); + net_pkt_append_u8(pkt, 0); /* reserved */ + net_pkt_append_u8(pkt, prefixlen); + net_pkt_append(pkt, prefix_bytes, prefix->s6_addr, K_FOREVER); - net_pkt_append_u8(buf, NET_RPL_OPTION_TRANSIT); - net_pkt_append_u8(buf, 4); /* length */ - net_pkt_append_u8(buf, 0); /* flags */ - net_pkt_append_u8(buf, 0); /* path control */ - net_pkt_append_u8(buf, 0); /* path seq */ - net_pkt_append_u8(buf, lifetime); + net_pkt_append_u8(pkt, NET_RPL_OPTION_TRANSIT); + net_pkt_append_u8(pkt, 4); /* length */ + net_pkt_append_u8(pkt, 0); /* flags */ + net_pkt_append_u8(pkt, 0); /* path control */ + net_pkt_append_u8(pkt, 0); /* path seq */ + net_pkt_append_u8(pkt, lifetime); - ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6); + ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6); if (ret < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return ret; } - ret = net_send_data(buf); + ret = net_send_data(pkt); if (ret >= 0) { - net_rpl_dao_info(buf, src, dst, prefix); + net_rpl_dao_info(pkt, src, dst, prefix); net_stats_update_icmp_sent(); net_stats_update_rpl_dao_sent(); } else { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return ret; @@ -3107,37 +3107,37 @@ static int dao_send(struct net_rpl_parent *parent, } static inline int dao_forward(struct net_if *iface, - struct net_buf *orig, + struct net_pkt *orig, struct in6_addr *dst) { - struct net_buf *buf; + struct net_pkt *pkt; int ret; - 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); - if (!buf) { + if (!pkt) { return -ENOMEM; } /* Steal the fragment chain */ - buf->frags = orig->frags; + pkt->frags = orig->frags; orig->frags = NULL; - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, dst); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, dst); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_iface(buf, iface); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_iface(pkt, iface); - NET_ICMP_BUF(buf)->chksum = 0; - NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv6(buf); + NET_ICMP_BUF(pkt)->chksum = 0; + NET_ICMP_BUF(pkt)->chksum = ~net_calc_chksum_icmpv6(pkt); - ret = net_send_data(buf); + ret = net_send_data(pkt); if (ret >= 0) { net_stats_update_icmp_sent(); net_stats_update_rpl_dao_forwarded(); } else { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return ret; @@ -3150,44 +3150,44 @@ static int dao_ack_send(struct in6_addr *src, uint8_t sequence, uint8_t status) { - struct net_buf *buf; + struct net_pkt *pkt; int ret; NET_DBG("Sending a DAO ACK with sequence number %d to %s", sequence, net_sprint_ipv6_addr(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); - if (!buf) { + if (!pkt) { return -ENOMEM; } - 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_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - setup_icmpv6_hdr(buf, NET_ICMPV6_RPL, NET_RPL_DEST_ADV_OBJ_ACK); + setup_icmpv6_hdr(pkt, NET_ICMPV6_RPL, NET_RPL_DEST_ADV_OBJ_ACK); - net_pkt_append_u8(buf, instance->instance_id); - net_pkt_append_u8(buf, 0); /* reserved */ - net_pkt_append_u8(buf, sequence); - net_pkt_append_u8(buf, status); /* status */ + net_pkt_append_u8(pkt, instance->instance_id); + net_pkt_append_u8(pkt, 0); /* reserved */ + net_pkt_append_u8(pkt, sequence); + net_pkt_append_u8(pkt, status); /* status */ - ret = net_ipv6_finalize_raw(buf, IPPROTO_ICMPV6); + ret = net_ipv6_finalize_raw(pkt, IPPROTO_ICMPV6); if (ret < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return ret; } - ret = net_send_data(buf); + ret = net_send_data(pkt); if (ret >= 0) { - net_rpl_dao_ack_info(buf, src, dst, instance->instance_id, + net_rpl_dao_ack_info(pkt, src, dst, instance->instance_id, sequence); net_stats_update_icmp_sent(); net_stats_update_rpl_dao_ack_sent(); } else { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return 0; @@ -3195,7 +3195,7 @@ static int dao_ack_send(struct in6_addr *src, static int forwarding_dao(struct net_rpl_instance *instance, struct net_rpl_dag *dag, - struct net_buf *buf, + struct net_pkt *pkt, uint8_t sequence, uint8_t flags, char *str) @@ -3210,10 +3210,10 @@ static int forwarding_dao(struct net_rpl_instance *instance, if (paddr) { NET_DBG("%s %s", str, net_sprint_ipv6_addr(paddr)); - net_ipaddr_copy(&src, &NET_IPV6_BUF(buf)->src); - net_ipaddr_copy(&dst, &NET_IPV6_BUF(buf)->dst); + net_ipaddr_copy(&src, &NET_IPV6_BUF(pkt)->src); + net_ipaddr_copy(&dst, &NET_IPV6_BUF(pkt)->dst); - r = dao_forward(dag->instance->iface, buf, paddr); + r = dao_forward(dag->instance->iface, pkt, paddr); if (r >= 0) { return r; } @@ -3222,7 +3222,7 @@ static int forwarding_dao(struct net_rpl_instance *instance, * original DAO message. */ if (flags & NET_RPL_DAO_K_FLAG) { - r = dao_ack_send(&dst, &src, net_pkt_iface(buf), + r = dao_ack_send(&dst, &src, net_pkt_iface(pkt), instance, sequence, 128); } } @@ -3230,9 +3230,9 @@ static int forwarding_dao(struct net_rpl_instance *instance, return r; } -static enum net_verdict handle_dao(struct net_buf *buf) +static enum net_verdict handle_dao(struct net_pkt *pkt) { - struct in6_addr *dao_sender = &NET_IPV6_BUF(buf)->src; + struct in6_addr *dao_sender = &NET_IPV6_BUF(pkt)->src; struct net_rpl_route_entry *extra = NULL; struct net_rpl_parent *parent = NULL; enum net_rpl_route_source learned_from; @@ -3253,14 +3253,14 @@ static enum net_verdict handle_dao(struct net_buf *buf) uint8_t len; int r = -EINVAL; - net_rpl_info(buf, "Destination Advertisement Object"); + net_rpl_info(pkt, "Destination Advertisement Object"); /* offset tells now where the ICMPv6 header is starting */ - offset = net_pkt_icmp_data(buf) - net_pkt_ip_data(buf); + offset = net_pkt_icmp_data(pkt) - net_pkt_ip_data(pkt); offset += sizeof(struct net_icmp_hdr); - frag = net_pkt_read_u8(buf->frags, offset, &pos, &instance_id); + frag = net_frag_read_u8(pkt->frags, offset, &pos, &instance_id); instance = net_rpl_get_instance(instance_id); if (!instance) { @@ -3271,15 +3271,15 @@ static enum net_verdict handle_dao(struct net_buf *buf) lifetime = instance->default_lifetime; - frag = net_pkt_read_u8(frag, pos, &pos, &flags); - frag = net_pkt_skip(frag, pos, &pos, 1); /* reserved */ - frag = net_pkt_read_u8(frag, pos, &pos, &sequence); + frag = net_frag_read_u8(frag, pos, &pos, &flags); + frag = net_frag_skip(frag, pos, &pos, 1); /* reserved */ + frag = net_frag_read_u8(frag, pos, &pos, &sequence); dag = instance->current_dag; /* Is the DAG ID present? */ if (flags & NET_RPL_DAO_D_FLAG) { - frag = net_pkt_read(frag, pos, &pos, sizeof(addr), + frag = net_frag_read(frag, pos, &pos, sizeof(addr), addr.s6_addr); if (memcmp(&dag->dag_id, &addr, sizeof(dag->dag_id))) { @@ -3330,7 +3330,7 @@ static enum net_verdict handle_dao(struct net_buf *buf) /* Handle any DAO suboptions */ while (frag) { len = 0; - frag = net_pkt_read_u8(frag, pos, &pos, &subopt_type); + frag = net_frag_read_u8(frag, pos, &pos, &subopt_type); if (!frag && pos == 0) { /* We are at the end of the message */ break; @@ -3343,7 +3343,7 @@ static enum net_verdict handle_dao(struct net_buf *buf) if (subopt_type != NET_RPL_OPTION_PAD1) { /* Suboption with a two-byte header + payload */ - frag = net_pkt_read_u8(frag, pos, &pos, &len); + frag = net_frag_read_u8(frag, pos, &pos, &len); len += 2; } @@ -3356,23 +3356,23 @@ static enum net_verdict handle_dao(struct net_buf *buf) break; case NET_RPL_OPTION_PADN: /* Skip padding bytes */ - frag = net_pkt_skip(frag, pos, &pos, len - 2); + frag = net_frag_skip(frag, pos, &pos, len - 2); break; case NET_RPL_OPTION_TARGET: - frag = net_pkt_skip(frag, pos, &pos, 1); /* reserved */ - frag = net_pkt_read_u8(frag, pos, &pos, &target_len); - frag = net_pkt_read(frag, pos, &pos, + frag = net_frag_skip(frag, pos, &pos, 1); /* reserved */ + frag = net_frag_read_u8(frag, pos, &pos, &target_len); + frag = net_frag_read(frag, pos, &pos, (target_len + 7) / 8, addr.s6_addr); break; case NET_RPL_OPTION_TRANSIT: /* The flags, path sequence and control are ignored. */ - frag = net_pkt_skip(frag, pos, &pos, 3); - frag = net_pkt_read_u8(frag, pos, &pos, &lifetime); + frag = net_frag_skip(frag, pos, &pos, 3); + frag = net_frag_read_u8(frag, pos, &pos, &lifetime); break; default: /* Skip unknown sub options */ - frag = net_pkt_skip(frag, pos, &pos, len - 2); + frag = net_frag_skip(frag, pos, &pos, len - 2); break; } } @@ -3384,7 +3384,7 @@ static enum net_verdict handle_dao(struct net_buf *buf) if (net_is_ipv6_addr_mcast_global(&addr)) { struct net_route_entry_mcast *mcast_group; - mcast_group = net_route_mcast_add(net_pkt_iface(buf), &addr); + mcast_group = net_route_mcast_add(net_pkt_iface(pkt), &addr); if (mcast_group) { mcast_group->data = (void *)dag; mcast_group->lifetime = net_rpl_lifetime(instance, @@ -3400,7 +3400,7 @@ static enum net_verdict handle_dao(struct net_buf *buf) NET_DBG("No-Path DAO received"); - route = net_route_lookup(net_pkt_iface(buf), &addr); + route = net_route_lookup(net_pkt_iface(pkt), &addr); nbr = net_route_get_nbr(route); extra = net_nbr_extra_data(nbr); nexthop = net_route_get_nexthop(route); @@ -3420,7 +3420,7 @@ static enum net_verdict handle_dao(struct net_buf *buf) */ if (dag->preferred_parent) { r = forwarding_dao(instance, dag, - buf, sequence, flags, + pkt, sequence, flags, #if defined(CONFIG_NET_DEBUG_RPL) "Forwarding no-path DAO to " "parent" @@ -3429,7 +3429,7 @@ static enum net_verdict handle_dao(struct net_buf *buf) #endif ); if (r >= 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } } @@ -3440,35 +3440,35 @@ static enum net_verdict handle_dao(struct net_buf *buf) NET_DBG("Adding DAO route"); - nbr = net_ipv6_nbr_lookup(net_pkt_iface(buf), dao_sender); + nbr = net_ipv6_nbr_lookup(net_pkt_iface(pkt), dao_sender); if (!nbr) { - nbr = net_ipv6_nbr_add(net_pkt_iface(buf), dao_sender, - net_pkt_ll_src(buf), false, + nbr = net_ipv6_nbr_add(net_pkt_iface(pkt), dao_sender, + net_pkt_ll_src(pkt), false, NET_IPV6_NBR_STATE_REACHABLE); if (nbr) { /* Set reachable timer */ - net_ipv6_nbr_set_reachable_timer(net_pkt_iface(buf), + net_ipv6_nbr_set_reachable_timer(net_pkt_iface(pkt), nbr); NET_DBG("Neighbor %s [%s] added to neighbor cache", net_sprint_ipv6_addr(dao_sender), - net_sprint_ll_addr(net_pkt_ll_src(buf)->addr, - net_pkt_ll_src(buf)->len)); + net_sprint_ll_addr(net_pkt_ll_src(pkt)->addr, + net_pkt_ll_src(pkt)->len)); } else { NET_DBG("Out of memory, dropping DAO from %s [%s]", net_sprint_ipv6_addr(dao_sender), - net_sprint_ll_addr(net_pkt_ll_src(buf)->addr, - net_pkt_ll_src(buf)->len)); + net_sprint_ll_addr(net_pkt_ll_src(pkt)->addr, + net_pkt_ll_src(pkt)->len)); return NET_DROP; } } else { NET_DBG("Neighbor %s [%s] already in neighbor cache", net_sprint_ipv6_addr(dao_sender), - net_sprint_ll_addr(net_pkt_ll_src(buf)->addr, - net_pkt_ll_src(buf)->len)); + net_sprint_ll_addr(net_pkt_ll_src(pkt)->addr, + net_pkt_ll_src(pkt)->len)); } - route = net_rpl_add_route(dag, net_pkt_iface(buf), + route = net_rpl_add_route(dag, net_pkt_iface(pkt), &addr, target_len, dao_sender); if (!route) { net_stats_update_rpl_mem_overflows(); @@ -3490,7 +3490,7 @@ fwd_dao: if (learned_from == NET_RPL_ROUTE_UNICAST_DAO) { if (dag->preferred_parent) { r = forwarding_dao(instance, dag, - buf, sequence, flags, + pkt, sequence, flags, #if defined(CONFIG_NET_DEBUG_RPL) "Forwarding DAO to parent" #else @@ -3498,7 +3498,7 @@ fwd_dao: #endif ); if (r >= 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } } @@ -3507,7 +3507,7 @@ fwd_dao: return NET_DROP; } -static enum net_verdict handle_dao_ack(struct net_buf *buf) +static enum net_verdict handle_dao_ack(struct net_pkt *pkt) { struct net_rpl_instance *instance; struct net_buf *frag; @@ -3517,14 +3517,14 @@ static enum net_verdict handle_dao_ack(struct net_buf *buf) uint8_t sequence; uint8_t status; - net_rpl_info(buf, "Destination Advertisement Object Ack"); + net_rpl_info(pkt, "Destination Advertisement Object Ack"); /* offset tells now where the ICMPv6 header is starting */ - offset = net_pkt_icmp_data(buf) - net_pkt_ip_data(buf); + offset = net_pkt_icmp_data(pkt) - net_pkt_ip_data(pkt); offset += sizeof(struct net_icmp_hdr); - frag = net_pkt_read_u8(buf->frags, offset, &pos, &instance_id); + frag = net_frag_read_u8(pkt->frags, offset, &pos, &instance_id); if (!frag && pos == 0xffff) { /* Read error */ return NET_DROP; @@ -3538,16 +3538,16 @@ static enum net_verdict handle_dao_ack(struct net_buf *buf) } /* Skip reserved byte */ - frag = net_pkt_skip(frag, pos, &pos, 1); - frag = net_pkt_read_u8(frag, pos, &pos, &sequence); - frag = net_pkt_read_u8(frag, pos, &pos, &status); + frag = net_frag_skip(frag, pos, &pos, 1); + frag = net_frag_read_u8(frag, pos, &pos, &sequence); + frag = net_frag_read_u8(frag, pos, &pos, &status); if (!frag && pos == 0xffff) { return NET_DROP; } NET_DBG("Received a DAO ACK with seq number %d(%d) status %d from %s", sequence, rpl_dao_sequence, status, - net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src)); + net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src)); if (sequence == rpl_dao_sequence) { NET_DBG("Status %s", status < 128 ? "ACK" : "NACK"); @@ -3560,7 +3560,7 @@ static enum net_verdict handle_dao_ack(struct net_buf *buf) * to act as a parent. * Trigger a local repair since we can not get our DAO. */ - net_rpl_local_repair(net_pkt_iface(buf), instance); + net_rpl_local_repair(net_pkt_iface(pkt), instance); return NET_DROP; } } else { @@ -3570,7 +3570,7 @@ static enum net_verdict handle_dao_ack(struct net_buf *buf) net_stats_update_rpl_dao_ack_recv(); - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } @@ -3599,7 +3599,7 @@ static struct net_icmpv6_handler dao_ack_handler = { .handler = handle_dao_ack, }; -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) { uint16_t pos = 0; struct net_rpl_parent *parent; @@ -3609,18 +3609,18 @@ int net_rpl_update_header(struct net_buf *buf, struct in6_addr *addr) uint8_t opt; uint8_t len; - if (NET_IPV6_BUF(buf)->nexthdr != NET_IPV6_NEXTHDR_HBHO) { + if (NET_IPV6_BUF(pkt)->nexthdr != NET_IPV6_NEXTHDR_HBHO) { return 0; } - frag = buf->frags; + frag = pkt->frags; /* The HBHO will start right after IPv6 header also skip * next header. */ pos = sizeof(struct net_ipv6_hdr) + 1; - frag = net_pkt_read(frag, pos, &pos, 1, &len); /* HBH length */ - frag = net_pkt_read(frag, pos, &pos, 1, &opt); /* Opt type */ + frag = net_frag_read(frag, pos, &pos, 1, &len); /* HBH length */ + frag = net_frag_read(frag, pos, &pos, 1, &opt); /* Opt type */ if (!frag && pos) { /* Not enough data in the message */ return -EMSGSIZE; @@ -3636,15 +3636,15 @@ int net_rpl_update_header(struct net_buf *buf, struct in6_addr *addr) return 0; } - frag = net_pkt_skip(frag, pos, &pos, 1); /* opt len */ + frag = net_frag_skip(frag, pos, &pos, 1); /* opt len */ /* Where the flags is located in the packet, that info is * need few lines below. */ offset = pos; - frag = net_pkt_skip(frag, pos, &pos, 1); /* flags */ - frag = net_pkt_skip(frag, pos, &pos, 1); /* instance */ - frag = net_pkt_read(frag, pos, &pos, 2, (uint8_t *)&sender_rank); + frag = net_frag_skip(frag, pos, &pos, 1); /* flags */ + frag = net_frag_skip(frag, pos, &pos, 1); /* instance */ + frag = net_frag_read(frag, pos, &pos, 2, (uint8_t *)&sender_rank); if (!frag && pos) { return -EMSGSIZE; } @@ -3661,23 +3661,23 @@ int net_rpl_update_header(struct net_buf *buf, struct in6_addr *addr) return -EINVAL; } - parent = find_parent(net_pkt_iface(buf), + parent = find_parent(net_pkt_iface(pkt), rpl_default_instance->current_dag, addr); if (!parent || parent != parent->dag->preferred_parent) { - net_pkt_write_u8(buf, buf->frags, offset, &pos, + net_pkt_write_u8(pkt, pkt->frags, offset, &pos, NET_RPL_HDR_OPT_DOWN); } offset++; - net_pkt_write_u8(buf, buf->frags, offset, &pos, + net_pkt_write_u8(pkt, pkt->frags, offset, &pos, rpl_default_instance->instance_id); - net_pkt_write_be16(buf, buf->frags, pos, &pos, + net_pkt_write_be16(pkt, pkt->frags, pos, &pos, htons(rpl_default_instance-> current_dag->rank)); return 0; } -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, bool *result) { @@ -3686,9 +3686,9 @@ struct net_buf *net_rpl_verify_header(struct net_buf *buf, struct net_buf *frag, uint8_t instance_id, flags; bool down, sender_closer; - frag = net_pkt_read_u8(frag, offset, pos, &flags); - frag = net_pkt_read_u8(frag, *pos, pos, &instance_id); - frag = net_pkt_read_be16(frag, *pos, pos, &sender_rank); + frag = net_frag_read_u8(frag, offset, pos, &flags); + frag = net_frag_read_u8(frag, *pos, pos, &instance_id); + frag = net_frag_read_be16(frag, *pos, pos, &sender_rank); if (!frag && *pos == 0xffff) { *result = false; @@ -3712,8 +3712,8 @@ struct net_buf *net_rpl_verify_header(struct net_buf *buf, struct net_buf *frag, */ NET_DBG("Forward error!"); - route = net_route_lookup(net_pkt_iface(buf), - &NET_IPV6_BUF(buf)->dst); + route = net_route_lookup(net_pkt_iface(pkt), + &NET_IPV6_BUF(pkt)->dst); if (route) { net_route_del(route); } @@ -3781,71 +3781,71 @@ struct net_buf *net_rpl_verify_header(struct net_buf *buf, struct net_buf *frag, return frag; } -static inline int add_rpl_opt(struct net_buf *buf, uint16_t offset) +static inline int add_rpl_opt(struct net_pkt *pkt, uint16_t offset) { - int ext_len = net_pkt_ext_len(buf); + int ext_len = net_pkt_ext_len(pkt); bool ret; /* next header */ - net_pkt_set_ipv6_hdr_prev(buf, offset); + net_pkt_set_ipv6_hdr_prev(pkt, offset); - ret = net_pkt_insert_u8(buf, buf->frags, offset++, - NET_IPV6_BUF(buf)->nexthdr); + ret = net_pkt_insert_u8(pkt, pkt->frags, offset++, + NET_IPV6_BUF(pkt)->nexthdr); if (!ret) { return -EINVAL; } /* Option len */ - ret = net_pkt_insert_u8(buf, buf->frags, offset++, + ret = net_pkt_insert_u8(pkt, pkt->frags, offset++, NET_RPL_HOP_BY_HOP_LEN - 8); if (!ret) { return -EINVAL; } /* Sub-option type */ - ret = net_pkt_insert_u8(buf, buf->frags, offset++, + ret = net_pkt_insert_u8(pkt, pkt->frags, offset++, NET_IPV6_EXT_HDR_OPT_RPL); if (!ret) { return -EINVAL; } /* Sub-option length */ - ret = net_pkt_insert_u8(buf, buf->frags, offset++, + ret = net_pkt_insert_u8(pkt, pkt->frags, offset++, NET_RPL_HDR_OPT_LEN); if (!ret) { return -EINVAL; } /* RPL option flags */ - ret = net_pkt_insert_u8(buf, buf->frags, offset++, 0); + ret = net_pkt_insert_u8(pkt, pkt->frags, offset++, 0); if (!ret) { return -EINVAL; } /* RPL Instance id */ - ret = net_pkt_insert_u8(buf, buf->frags, offset++, 0); + ret = net_pkt_insert_u8(pkt, pkt->frags, offset++, 0); if (!ret) { return -EINVAL; } /* RPL sender rank */ - ret = net_pkt_insert_be16(buf, buf->frags, offset++, 0); + ret = net_pkt_insert_be16(pkt, pkt->frags, offset++, 0); if (!ret) { return -EINVAL; } - NET_IPV6_BUF(buf)->nexthdr = NET_IPV6_NEXTHDR_HBHO; + NET_IPV6_BUF(pkt)->nexthdr = NET_IPV6_NEXTHDR_HBHO; - net_pkt_set_ext_len(buf, ext_len + NET_RPL_HOP_BY_HOP_LEN); + net_pkt_set_ext_len(pkt, ext_len + NET_RPL_HOP_BY_HOP_LEN); return 0; } -static int net_rpl_update_header_empty(struct net_buf *buf) +static int net_rpl_update_header_empty(struct net_pkt *pkt) { uint16_t offset = sizeof(struct net_ipv6_hdr); - uint8_t next = NET_IPV6_BUF(buf)->nexthdr; - struct net_buf *frag = buf->frags; + uint8_t next = NET_IPV6_BUF(pkt)->nexthdr; + struct net_buf *frag = pkt->frags; struct net_rpl_instance *instance; struct net_rpl_parent *parent; struct net_route_entry *route; @@ -3859,7 +3859,7 @@ static int net_rpl_update_header_empty(struct net_buf *buf) if (next != NET_IPV6_NEXTHDR_HBHO) { NET_DBG("No hop-by-hop option found, creating it"); - if (add_rpl_opt(buf, offset) < 0) { + if (add_rpl_opt(pkt, offset) < 0) { NET_DBG("Cannot add RPL options"); return -EINVAL; } @@ -3867,10 +3867,10 @@ static int net_rpl_update_header_empty(struct net_buf *buf) return 0; } - net_pkt_set_ipv6_hdr_prev(buf, offset); + net_pkt_set_ipv6_hdr_prev(pkt, offset); - frag = net_pkt_read_u8(frag, offset, &offset, &next_hdr); - frag = net_pkt_read_u8(frag, offset, &offset, &len); + frag = net_frag_read_u8(frag, offset, &offset, &next_hdr); + frag = net_frag_read_u8(frag, offset, &offset, &len); if (!frag) { return 0; } @@ -3888,8 +3888,8 @@ static int net_rpl_update_header_empty(struct net_buf *buf) length += 2; /* Each extension option has type and length */ - frag = net_pkt_read_u8(frag, offset, &offset, &opt_type); - frag = net_pkt_read_u8(frag, offset, &offset, &opt_len); + frag = net_frag_read_u8(frag, offset, &offset, &opt_type); + frag = net_frag_read_u8(frag, offset, &offset, &opt_len); if (opt_type != NET_IPV6_EXT_HDR_OPT_RPL) { /* FIXME: go through all the options instead */ @@ -3903,8 +3903,8 @@ static int net_rpl_update_header_empty(struct net_buf *buf) return 0; } - frag = net_pkt_read_u8(buf, offset, &offset, &flags); - frag = net_pkt_read_u8(frag, offset, &offset, &instance_id); + frag = net_frag_read_u8(pkt->frags, offset, &offset, &flags); + frag = net_frag_read_u8(frag, offset, &offset, &instance_id); instance = net_rpl_get_instance(instance_id); if (!instance || !instance->is_used || @@ -3922,12 +3922,12 @@ static int net_rpl_update_header_empty(struct net_buf *buf) NET_DBG("Updating RPL option"); /* The offset should point to "rank" right now */ - net_pkt_write_be16(buf, frag, offset, &pos, - instance->current_dag->rank); + net_pkt_write_be16(pkt, frag, offset, &pos, + instance->current_dag->rank); offset -= 2; /* move back to flags */ - route = net_route_lookup(net_pkt_iface(buf), &NET_IPV6_BUF(buf)->dst); + route = net_route_lookup(net_pkt_iface(pkt), &NET_IPV6_BUF(pkt)->dst); /* * Check the direction of the down flag, as per @@ -3939,8 +3939,8 @@ static int net_rpl_update_header_empty(struct net_buf *buf) struct net_nbr *nbr; if (!route) { - net_pkt_write_u8(buf, frag, offset, &pos, - flags |= NET_RPL_HDR_OPT_FWD_ERR); + net_pkt_write_u8(pkt, frag, offset, &pos, + flags |= NET_RPL_HDR_OPT_FWD_ERR); NET_DBG("RPL forwarding error"); @@ -3952,14 +3952,14 @@ static int net_rpl_update_header_empty(struct net_buf *buf) NET_DBG("RPL generate No-Path DAO"); nbr = net_nbr_lookup(&net_rpl_parents.table, - net_pkt_iface(buf), - net_pkt_ll_src(buf)); + net_pkt_iface(pkt), + net_pkt_ll_src(pkt)); parent = nbr_data(nbr); if (parent) { - net_rpl_dao_send(net_pkt_iface(buf), + net_rpl_dao_send(net_pkt_iface(pkt), parent, - &NET_IPV6_BUF(buf)->dst, + &NET_IPV6_BUF(pkt)->dst, NET_RPL_ZERO_LIFETIME); } @@ -3981,16 +3981,16 @@ static int net_rpl_update_header_empty(struct net_buf *buf) * towards the RPL root. If so, we should not * set the down flag. */ - net_pkt_write_u8(buf, frag, offset, &pos, - flags &= ~NET_RPL_HDR_OPT_DOWN); + net_pkt_write_u8(pkt, frag, offset, &pos, + flags &= ~NET_RPL_HDR_OPT_DOWN); NET_DBG("RPL option going up"); } else { /* A DAO route was found so we set the down * flag. */ - net_pkt_write_u8(buf, frag, offset, &pos, - flags |= NET_RPL_HDR_OPT_DOWN); + net_pkt_write_u8(pkt, frag, offset, &pos, + flags |= NET_RPL_HDR_OPT_DOWN); NET_DBG("RPL option going down"); } @@ -3998,7 +3998,7 @@ static int net_rpl_update_header_empty(struct net_buf *buf) return 0; } -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) { struct net_rpl_instance *instance; struct net_buf *frag; @@ -4010,17 +4010,17 @@ int net_rpl_revert_header(struct net_buf *buf, uint16_t offset, uint16_t *pos) uint8_t opt; /* Skip HBHO next header and length */ - frag = net_pkt_skip(buf->frags, offset, pos, 2); - frag = net_pkt_read_u8(frag, *pos, pos, &opt); - frag = net_pkt_read_u8(frag, *pos, pos, &opt_len); + frag = net_frag_skip(pkt->frags, offset, pos, 2); + frag = net_frag_read_u8(frag, *pos, pos, &opt); + frag = net_frag_read_u8(frag, *pos, pos, &opt_len); if (opt != NET_IPV6_EXT_HDR_OPT_RPL) { return -EINVAL; } revert_pos = *pos; - frag = net_pkt_read_u8(frag, *pos, pos, &flags); - frag = net_pkt_read_u8(frag, *pos, pos, &instance_id); + frag = net_frag_read_u8(frag, *pos, pos, &flags); + frag = net_frag_read_u8(frag, *pos, pos, &instance_id); if (!frag && *pos == 0xffff) { return -EINVAL; } @@ -4045,9 +4045,9 @@ int net_rpl_revert_header(struct net_buf *buf, uint16_t offset, uint16_t *pos) *pos = revert_pos; /* Update flags, instance id, sender rank */ - frag = net_pkt_write_u8(buf, frag, *pos, pos, flags); - frag = net_pkt_write_u8(buf, frag, *pos, pos, instance_id); - frag = net_pkt_write_be16(buf, frag, *pos, pos, sender_rank); + frag = net_pkt_write_u8(pkt, frag, *pos, pos, flags); + frag = net_pkt_write_u8(pkt, frag, *pos, pos, instance_id); + frag = net_pkt_write_be16(pkt, frag, *pos, pos, sender_rank); if (!frag && *pos == 0xffff) { return -EINVAL; } @@ -4055,12 +4055,12 @@ int net_rpl_revert_header(struct net_buf *buf, uint16_t offset, uint16_t *pos) return 0; } -int net_rpl_insert_header(struct net_buf *buf) +int net_rpl_insert_header(struct net_pkt *pkt) { #if defined(CONFIG_NET_RPL_INSERT_HBH_OPTION) if (rpl_default_instance && - !net_is_ipv6_addr_mcast(&NET_IPV6_BUF(buf)->dst)) { - return net_rpl_update_header_empty(buf); + !net_is_ipv6_addr_mcast(&NET_IPV6_BUF(pkt)->dst)) { + return net_rpl_update_header_empty(pkt); } #endif diff --git a/subsys/net/ip/rpl.h b/subsys/net/ip/rpl.h index bc0daa0e1c1..b6e4b384489 100644 --- a/subsys/net/ip/rpl.h +++ b/subsys/net/ip/rpl.h @@ -909,17 +909,17 @@ void net_rpl_global_repair(struct net_route_entry *route); /** * @brief Update RPL headers in IPv6 packet. * - * @param buf Network buffer. + * @param pkt Network packet. * @param addr IPv6 address of next hop host. * * @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. * - * @param buf Network buffer fragment list. + * @param pkt Network packet fragment list. * @param frag Current network buffer fragment. * @param offset Where the RPL header starts in the packet * @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. */ -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, bool *result); /** * @brief Insert RPL extension header to IPv6 packet. * - * @param buf Network buffer. + * @param pkt Network packet. * * @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. * 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 pos How long the RPL header was, this is returned to the caller. * * @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. diff --git a/subsys/net/ip/tcp.c b/subsys/net/ip/tcp.c index cc58c395b83..c5bbec42ac7 100644 --- a/subsys/net/ip/tcp.c +++ b/subsys/net/ip/tcp.c @@ -72,12 +72,12 @@ static char upper_if_set(char chr, bool set) 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; - ack = sys_get_be32(NET_TCP_BUF(buf)->ack); + ack = sys_get_be32(NET_TCP_BUF(pkt)->ack); if (!tcp->sent_ack) { 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; } - 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", - buf, - ntohs(NET_TCP_BUF(buf)->src_port), - ntohs(NET_TCP_BUF(buf)->dst_port), - sys_get_be32(NET_TCP_BUF(buf)->seq), + pkt, + ntohs(NET_TCP_BUF(pkt)->src_port), + ntohs(NET_TCP_BUF(pkt)->dst_port), + sys_get_be32(NET_TCP_BUF(pkt)->seq), ack, /* This tells how many bytes we are acking now */ 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('s', flags & NET_TCP_SYN), upper_if_set('f', flags & NET_TCP_FIN), - sys_get_be16(NET_TCP_BUF(buf)->wnd), - ntohs(NET_TCP_BUF(buf)->chksum)); + sys_get_be16(NET_TCP_BUF(pkt)->wnd), + ntohs(NET_TCP_BUF(pkt)->chksum)); } #else #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; } -#define is_6lo_technology(buf) \ - (IS_ENABLED(CONFIG_NET_IPV6) && net_pkt_family(buf) == AF_INET6 && \ +#define is_6lo_technology(pkt) \ + (IS_ENABLED(CONFIG_NET_IPV6) && net_pkt_family(pkt) == AF_INET6 && \ ((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) && \ - 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 - * 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 * 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)) { - buf = net_pkt_ref(buf); + if (!is_6lo_technology(pkt)) { + pkt = net_pkt_ref(pkt); } } static void tcp_retry_expired(struct k_timer *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 * 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++; k_timer_start(&tcp->retry_timer, retry_timeout(tcp), 0); - buf = CONTAINER_OF(sys_slist_peek_head(&tcp->sent_list), - struct net_buf, sent_list); + pkt = CONTAINER_OF(sys_slist_peek_head(&tcp->sent_list), + struct net_pkt, sent_list); - do_ref_if_needed(buf); - if (net_tcp_send_buf(buf) < 0 && !is_6lo_technology(buf)) { - net_pkt_unref(buf); + do_ref_if_needed(pkt); + if (net_tcp_send_pkt(pkt) < 0 && !is_6lo_technology(pkt)) { + net_pkt_unref(pkt); } } else if (IS_ENABLED(CONFIG_NET_TCP_TIME_WAIT)) { 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) { - struct net_buf *buf; - struct net_buf *tmp; + struct net_pkt *pkt; + struct net_pkt *tmp; int key; if (!PART_OF_ARRAY(tcp_context, tcp)) { 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) { - sys_slist_remove(&tcp->sent_list, NULL, &buf->sent_list); - net_pkt_unref(buf); + sys_slist_remove(&tcp->sent_list, NULL, &pkt->sent_list); + net_pkt_unref(pkt); } 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, - void *data) + void *data) { uint8_t optlen; @@ -247,16 +247,16 @@ static inline uint8_t net_tcp_add_options(struct net_buf *header, size_t len, 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 (net_pkt_family(buf) == AF_INET) { - return net_ipv4_finalize(context, buf); + if (net_pkt_family(pkt) == AF_INET) { + return net_ipv4_finalize(context, pkt); } else #endif #if defined(CONFIG_NET_IPV6) - if (net_pkt_family(buf) == AF_INET6) { - return net_ipv6_finalize(context, buf); + if (net_pkt_family(pkt) == AF_INET6) { + return net_ipv6_finalize(context, pkt); } #endif { @@ -265,9 +265,9 @@ static int finalize_segment(struct net_context *context, struct net_buf *buf) 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 net_buf *buf) + struct net_pkt *pkt) { struct net_buf *header, *tail = NULL; struct net_tcp_hdr *tcphdr; @@ -277,49 +277,49 @@ static struct net_buf *prepare_segment(struct net_tcp *tcp, NET_ASSERT(context); - if (buf) { + if (pkt) { /* TCP transmit data comes in with a pre-allocated * net_pkt at the head (so that net_context_send can find * the context), and the data after. Rejigger so we * can insert a TCP header cleanly */ - tail = buf->frags; - buf->frags = NULL; + tail = pkt->frags; + pkt->frags = NULL; } else { - buf = net_pkt_get_tx(context, K_FOREVER); + pkt = net_pkt_get_tx(context, K_FOREVER); } #if defined(CONFIG_NET_IPV4) - if (net_pkt_family(buf) == AF_INET) { - net_ipv4_create(context, buf, + if (net_pkt_family(pkt) == AF_INET) { + net_ipv4_create(context, pkt, net_sin_ptr(segment->src_addr)->sin_addr, &(net_sin(segment->dst_addr)->sin_addr)); dst_port = net_sin(segment->dst_addr)->sin_port; src_port = ((struct sockaddr_in_ptr *)&context->local)-> sin_port; - NET_IPV4_BUF(buf)->proto = IPPROTO_TCP; + NET_IPV4_BUF(pkt)->proto = IPPROTO_TCP; } else #endif #if defined(CONFIG_NET_IPV6) - if (net_pkt_family(buf) == AF_INET6) { - net_ipv6_create(tcp->context, buf, + if (net_pkt_family(pkt) == AF_INET6) { + net_ipv6_create(tcp->context, pkt, net_sin6_ptr(segment->src_addr)->sin6_addr, &(net_sin6(segment->dst_addr)->sin6_addr)); dst_port = net_sin6(segment->dst_addr)->sin6_port; src_port = ((struct sockaddr_in6_ptr *)&context->local)-> sin6_port; - NET_IPV6_BUF(buf)->nexthdr = IPPROTO_TCP; + NET_IPV6_BUF(pkt)->nexthdr = IPPROTO_TCP; } else #endif { NET_DBG("Protocol family %d not supported", - net_pkt_family(buf)); - net_pkt_unref(buf); + net_pkt_family(pkt)); + net_pkt_unref(pkt); return NULL; } 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); @@ -340,17 +340,17 @@ static struct net_buf *prepare_segment(struct net_tcp *tcp, tcphdr->urg[1] = 0; if (tail) { - net_buf_frag_add(buf, tail); + net_pkt_frag_add(pkt, tail); } - if (finalize_segment(context, buf) < 0) { - net_pkt_unref(buf); + if (finalize_segment(context, pkt) < 0) { + net_pkt_unref(pkt); 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) @@ -379,7 +379,7 @@ int net_tcp_prepare_segment(struct net_tcp *tcp, uint8_t flags, void *options, size_t optlen, const struct sockaddr_ptr *local, const struct sockaddr *remote, - struct net_buf **send_buf) + struct net_pkt **send_pkt) { uint32_t seq; uint16_t wnd; @@ -437,8 +437,8 @@ int net_tcp_prepare_segment(struct net_tcp *tcp, uint8_t flags, segment.options = options; segment.optlen = optlen; - *send_buf = prepare_segment(tcp, &segment, *send_buf); - if (!*send_buf) { + *send_pkt = prepare_segment(tcp, &segment, *send_pkt); + if (!*send_pkt) { 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, - struct net_buf **buf) + struct net_pkt **pkt) { uint8_t options[NET_TCP_MAX_OPT_SIZE]; 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, options, optionlen, NULL, remote, - buf); + pkt); case NET_TCP_FIN_WAIT_1: case NET_TCP_LAST_ACK: /* 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--; return net_tcp_prepare_segment(tcp, NET_TCP_FIN | NET_TCP_ACK, - 0, 0, NULL, remote, buf); + 0, 0, NULL, remote, pkt); default: return net_tcp_prepare_segment(tcp, NET_TCP_ACK, 0, 0, NULL, - remote, buf); + remote, pkt); } 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, const struct sockaddr *remote, - struct net_buf **buf) + struct net_pkt **pkt) { struct tcp_segment segment = { 0 }; @@ -590,7 +590,7 @@ int net_tcp_prepare_reset(struct net_tcp *tcp, segment.options = NULL; segment.optlen = 0; - *buf = prepare_segment(tcp, &segment, NULL); + *pkt = prepare_segment(tcp, &segment, NULL); } return 0; @@ -630,10 +630,10 @@ const char * const net_tcp_state_str(enum net_tcp_state state) 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; - size_t data_len = net_buf_frags_len(buf); + size_t data_len = net_pkt_get_len(pkt); int ret; /* 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. */ 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) { return ret; } 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. */ 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); } - do_ref_if_needed(buf); + do_ref_if_needed(pkt); 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_tcp_hdr *tcphdr = NET_TCP_BUF(buf); + struct net_context *ctx = net_pkt_context(pkt); + struct net_tcp_hdr *tcphdr = NET_TCP_BUF(pkt); 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; - net_pkt_set_buf_sent(buf, true); + net_pkt_set_sent(pkt, true); /* We must have special handling for some network technologies that * tweak the IP protocol headers during packet sending. This happens * 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. * For Bluetooth, its fragmentation code will even mangle the data * part of the message so we need to copy those too. */ - if (is_6lo_technology(buf)) { - struct net_buf *new_buf, *check_buf; + if (is_6lo_technology(pkt)) { + struct net_pkt *new_pkt, *check_pkt; 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, * we should avoid the extra 6lowpan specific buffer copy * below. */ SYS_SLIST_FOR_EACH_CONTAINER(&ctx->tcp->sent_list, - check_buf, sent_list) { - if (check_buf == buf) { - buf_in_slist = true; + check_pkt, sent_list) { + if (check_pkt == pkt) { + pkt_in_slist = true; break; } } - if (buf_in_slist) { - new_buf = net_pkt_get_tx(ctx, K_FOREVER); + if (pkt_in_slist) { + new_pkt = net_pkt_get_tx(ctx, K_FOREVER); - new_buf->frags = net_pkt_copy_all(buf, 0, K_FOREVER); - net_pkt_copy_user_data(new_buf, buf); + memcpy(new_pkt, pkt, sizeof(struct net_pkt)); + new_pkt->frags = net_pkt_copy_all(pkt, 0, K_FOREVER); 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 - * return < 0, the caller will unref the original buf. - * This would leak the new_buf so remove it here. + * return < 0, the caller will unref the original pkt. + * 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) { - net_pkt_unref(new_buf); + net_pkt_unref(new_pkt); } return ret; } } - return net_send_data(buf); + return net_send_data(pkt); } 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) { - struct net_buf *buf; + struct net_pkt *pkt; /* For now, just send all queued data synchronously. Need to * add window handling and retry/ACK logic. */ - SYS_SLIST_FOR_EACH_CONTAINER(&context->tcp->sent_list, buf, sent_list) { - if (!net_pkt_buf_sent(buf)) { - if (net_tcp_send_buf(buf) < 0 && - !is_6lo_technology(buf)) { - net_pkt_unref(buf); + SYS_SLIST_FOR_EACH_CONTAINER(&context->tcp->sent_list, pkt, sent_list) { + if (!net_pkt_sent(pkt)) { + if (net_tcp_send_pkt(pkt) < 0 && + !is_6lo_technology(pkt)) { + 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; sys_slist_t *list = &ctx->tcp->sent_list; sys_snode_t *head; - struct net_buf *buf; + struct net_pkt *pkt; struct net_tcp_hdr *tcphdr; uint32_t seq; bool valid_ack = false; while (!sys_slist_is_empty(list)) { head = sys_slist_peek_head(list); - buf = CONTAINER_OF(head, struct net_buf, sent_list); - tcphdr = NET_TCP_BUF(buf); + pkt = CONTAINER_OF(head, struct net_pkt, sent_list); + 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)) { break; @@ -808,7 +808,7 @@ void net_tcp_ack_received(struct net_context *ctx, uint32_t ack) } sys_slist_remove(list, NULL, head); - net_pkt_unref(buf); + net_pkt_unref(pkt); valid_ack = true; } @@ -827,13 +827,13 @@ void net_tcp_ack_received(struct net_context *ctx, uint32_t ack) * pipe is uncorked again. */ 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) { - if (net_pkt_buf_sent(buf)) { - do_ref_if_needed(buf); - net_pkt_set_buf_sent(buf, false); + if (net_pkt_sent(pkt)) { + do_ref_if_needed(pkt); + net_pkt_set_sent(pkt, false); } } diff --git a/subsys/net/ip/tcp.h b/subsys/net/ip/tcp.h index 8c8ee184f90..60f94909a74 100644 --- a/subsys/net/ip/tcp.h +++ b/subsys/net/ip/tcp.h @@ -234,7 +234,7 @@ int net_tcp_release(struct net_tcp *tcp); * @param local Source address, or NULL to use the local address of * the TCP context * @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 */ @@ -242,31 +242,31 @@ int net_tcp_prepare_segment(struct net_tcp *tcp, uint8_t flags, void *options, size_t optlen, const struct sockaddr_ptr *local, 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. * * @param tcp TCP context * @param remote Peer address - * @param buf Network buffer + * @param pkt Network buffer * * @return 0 if ok, < 0 if error */ 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. * * @param tcp TCP context * @param remote Peer address - * @param buf Network buffer + * @param pkt Network buffer * * @return 0 if ok, < 0 if error */ 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); @@ -292,19 +292,19 @@ int net_tcp_send_data(struct net_context *context); * @brief Enqueue a single packet for transmission * * @param context TCP context - * @param buf Packet + * @param pkt Packet * * @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_*() * 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 diff --git a/subsys/net/ip/udp.h b/subsys/net/ip/udp.h index 92db38d4c30..5c68c184523 100644 --- a/subsys/net/ip/udp.h +++ b/subsys/net/ip/udp.h @@ -28,53 +28,53 @@ extern "C" { #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 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 dst_port) { - NET_UDP_BUF(buf)->src_port = htons(src_port); - NET_UDP_BUF(buf)->dst_port = htons(dst_port); + NET_UDP_BUF(pkt)->src_port = htons(src_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_pkt_ip_hdr_len(buf)); + NET_UDP_BUF(pkt)->len = htons(net_pkt_get_len(pkt) - + 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)); - 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 buf Network buffer + * @param pkt Network packet * @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, - struct net_buf *buf, +static inline struct net_pkt *net_udp_append(struct net_context *context, + struct net_pkt *pkt, uint16_t port) { - return net_udp_append_raw(buf, + return net_udp_append_raw(pkt, ntohs(net_sin((struct sockaddr *) &context->local)->sin_port), port); } #else -#define net_udp_append_raw(buf, src_port, dst_port) (buf) -#define net_udp_append(context, buf, port) (buf) +#define net_udp_append_raw(pkt, src_port, dst_port) (pkt) +#define net_udp_append(context, pkt, port) (pkt) #endif /* CONFIG_NET_UDP */ /** diff --git a/subsys/net/ip/utils.c b/subsys/net/ip/utils.c index 491bbfead79..96eeccd641b 100644 --- a/subsys/net/ip/utils.c +++ b/subsys/net/ip/utils.c @@ -400,12 +400,12 @@ static uint16_t calc_chksum(uint16_t sum, const uint8_t *ptr, uint16_t len) 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) { - struct net_buf *frag = buf->frags; - uint16_t proto_len = net_pkt_ip_hdr_len(buf) + - net_pkt_ext_len(buf); + struct net_buf *frag = pkt->frags; + uint16_t proto_len = net_pkt_ip_hdr_len(pkt) + + net_pkt_ext_len(pkt); int16_t len = frag->len - 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; } -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 sum; - switch (net_pkt_family(buf)) { + switch (net_pkt_family(pkt)) { #if defined(CONFIG_NET_IPV4) case AF_INET: - upper_layer_len = (NET_IPV4_BUF(buf)->len[0] << 8) + - NET_IPV4_BUF(buf)->len[1] - - net_pkt_ext_len(buf) - - net_pkt_ip_hdr_len(buf); + upper_layer_len = (NET_IPV4_BUF(pkt)->len[0] << 8) + + NET_IPV4_BUF(pkt)->len[1] - + net_pkt_ext_len(pkt) - + net_pkt_ip_hdr_len(pkt); if (proto == IPPROTO_ICMP) { - return htons(calc_chksum(0, net_pkt_ip_data(buf) + - net_pkt_ip_hdr_len(buf), + return htons(calc_chksum(0, net_pkt_ip_data(pkt) + + net_pkt_ip_hdr_len(pkt), upper_layer_len)); } else { 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)); } break; #endif #if defined(CONFIG_NET_IPV6) case AF_INET6: - upper_layer_len = (NET_IPV6_BUF(buf)->len[0] << 8) + - NET_IPV6_BUF(buf)->len[1] - net_pkt_ext_len(buf); + upper_layer_len = (NET_IPV6_BUF(pkt)->len[0] << 8) + + NET_IPV6_BUF(pkt)->len[1] - net_pkt_ext_len(pkt); 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)); break; #endif default: - NET_DBG("Unknown protocol family %d", net_pkt_family(buf)); + NET_DBG("Unknown protocol family %d", net_pkt_family(pkt)); 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); @@ -489,11 +489,11 @@ uint16_t net_calc_chksum(struct net_buf *buf, uint8_t proto) } #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; - 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); diff --git a/subsys/net/lib/dns/resolve.c b/subsys/net/lib/dns/resolve.c index da4efc477b2..c26a36bcd1e 100644 --- a/subsys/net/lib/dns/resolve.c +++ b/subsys/net/lib/dns/resolve.c @@ -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, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *dns_data, uint16_t *dns_id, struct net_buf *dns_cname, @@ -377,12 +377,12 @@ static int dns_read(struct dns_resolve_context *ctx, int ret; int server_idx, query_idx; - data_len = min(net_pkt_appdatalen(buf), DNS_RESOLVER_MAX_BUF_SIZE); - offset = net_buf_frags_len(buf) - data_len; + data_len = min(net_pkt_appdatalen(pkt), DNS_RESOLVER_MAX_BUF_SIZE); + 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) { ret = DNS_EAI_MEMORY; goto quit; @@ -526,7 +526,7 @@ static int dns_read(struct dns_resolve_context *ctx, ctx->queries[query_idx].cb = NULL; - net_pkt_unref(buf); + net_pkt_unref(pkt); return 0; @@ -534,13 +534,13 @@ finished: dns_resolve_cancel(ctx, *dns_id); quit: - net_pkt_unref(buf); + net_pkt_unref(pkt); return ret; } static void cb_recv(struct net_context *net_ctx, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -570,7 +570,7 @@ static void cb_recv(struct net_context *net_ctx, 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) { /* We called the callback already in dns_read() if there * was no errors. @@ -643,7 +643,7 @@ static int dns_write(struct dns_resolve_context *ctx, enum dns_query_type query_type; struct net_context *net_ctx; struct sockaddr *server; - struct net_buf *buf; + struct net_pkt *pkt; int server_addr_len; uint16_t dns_id; int ret; @@ -661,13 +661,13 @@ static int dns_write(struct dns_resolve_context *ctx, goto quit; } - buf = net_pkt_get_tx(net_ctx, ctx->buf_timeout); - if (!buf) { + pkt = net_pkt_get_tx(net_ctx, ctx->buf_timeout); + if (!pkt) { ret = -ENOMEM; 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); if (ret < 0) { 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); - 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); if (ret < 0) { NET_DBG("Cannot send query (%d)", ret); - net_pkt_unref(buf); + net_pkt_unref(pkt); goto quit; } diff --git a/subsys/net/lib/http/http_client.c b/subsys/net/lib/http/http_client.c index 71d3642e405..cbf574f12b7 100644 --- a/subsys/net/lib/http/http_client.c +++ b/subsys/net/lib/http/http_client.c @@ -19,7 +19,7 @@ int http_request(struct net_context *net_ctx, int32_t timeout, struct http_client_request *req) { - struct net_buf *tx; + struct net_pkt *tx; int rc = -ENOMEM; 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); lb_exit: - net_buf_unref(tx); + net_pkt_unref(tx); return rc; } diff --git a/subsys/net/lib/http/http_server.c b/subsys/net/lib/http/http_server.c index 18589e3dcbc..7c3630b4952 100644 --- a/subsys/net/lib/http/http_server.c +++ b/subsys/net/lib/http/http_server.c @@ -32,7 +32,7 @@ static inline uint16_t http_strlen(const char *str) 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)) { return 0; @@ -41,7 +41,7 @@ static int http_add_header(struct net_buf *tx, int32_t timeout, const char *str) 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 *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, const char *html_payload) { - struct net_buf *tx; + struct net_pkt *tx; int rc = -EINVAL; tx = net_pkt_get_tx(ctx->net_ctx, ctx->timeout); if (!tx) { - goto exit_routine; + return rc; } 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; exit_routine: - /* unref can handle NULL buffers, so we are covered */ net_pkt_unref(tx); return rc; diff --git a/subsys/net/lib/mqtt/mqtt.c b/subsys/net/lib/mqtt/mqtt.c index 86da072a960..9a4f6966a39 100644 --- a/subsys/net/lib/mqtt/mqtt.c +++ b/subsys/net/lib/mqtt/mqtt.c @@ -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) { struct net_buf *data = NULL; - struct net_buf *tx = NULL; + struct net_pkt *tx = NULL; int rc; 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; } - net_buf_frag_add(tx, data); + net_pkt_frag_add(tx, data); data = 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; exit_connect: - net_pkt_unref(data); + net_pkt_frag_unref(data); net_pkt_unref(tx); return rc; @@ -69,7 +69,7 @@ exit_connect: 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 */ uint8_t msg[2]; uint16_t len; @@ -121,14 +121,14 @@ exit_disconnect: * * @retval 0 on success * @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 */ static int mqtt_tx_pub_msgs(struct mqtt_ctx *ctx, uint16_t id, enum mqtt_packet pkt_type) { - struct net_buf *tx = NULL; + struct net_pkt *tx = NULL; uint8_t msg[4]; uint16_t len; 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) { struct net_buf *data = NULL; - struct net_buf *tx = NULL; + struct net_pkt *tx = NULL; int rc; 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; } - net_buf_frag_add(tx, data); + net_pkt_frag_add(tx, data); data = 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; exit_publish: - net_pkt_unref(data); + net_pkt_frag_unref(data); net_pkt_unref(tx); return rc; @@ -244,7 +244,7 @@ exit_publish: int mqtt_tx_pingreq(struct mqtt_ctx *ctx) { - struct net_buf *tx = NULL; + struct net_pkt *tx = NULL; uint8_t msg[2]; uint16_t len; 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[]) { struct net_buf *data = NULL; - struct net_buf *tx = NULL; + struct net_pkt *tx = NULL; int rc; 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, pkt_id, items, topics, qos); if (rc != 0) { - net_pkt_unref(data); + net_pkt_frag_unref(data); rc = -EINVAL; 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; } - net_buf_frag_add(tx, data); + net_pkt_frag_add(tx, data); data = 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; exit_subs: - net_pkt_unref(data); + net_pkt_frag_unref(data); net_pkt_unref(tx); return rc; @@ -330,7 +330,7 @@ int mqtt_tx_unsubscribe(struct mqtt_ctx *ctx, uint16_t pkt_id, uint8_t items, const char *topics[]) { struct net_buf *data = NULL; - struct net_buf *tx = NULL; + struct net_pkt *tx = NULL; int rc; 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; } - net_buf_frag_add(tx, data); + net_pkt_frag_add(tx, data); data = 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; exit_unsub: - net_buf_unref(data); - net_buf_unref(tx); + net_pkt_frag_unref(data); + net_pkt_unref(tx); 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. @@ -429,7 +429,7 @@ exit_connect: * corresponding MQTT PUB msg. * * @param ctx MQTT context - * @param rx RX buffer + * @param rx RX packet * @param type MQTT Packet type * * @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] 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 - * rx buffer is shorter than the expected msg size + * rx packet is shorter than the expected msg size * * @retval Data buffer * @retval NULL on error */ 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) { 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; /* 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. */ 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; } - offset = net_buf_frags_len(rx) - data_len; - rc = net_pkt_linear_copy(data, rx, offset, data_len); + offset = net_pkt_get_len(rx) - data_len; + rc = net_frag_linear_copy(data, rx->frags, offset, data_len); if (rc != 0) { goto exit_error; } @@ -671,7 +671,7 @@ struct net_buf *mqtt_linearize_buffer(struct mqtt_ctx *ctx, struct net_buf *rx, return data; exit_error: - net_pkt_unref(data); + net_pkt_frag_unref(data); return NULL; } @@ -683,7 +683,7 @@ exit_error: * (if defined) * * @param ctx MQTT context - * @param rx RX buffer + * @param rx RX packet * * @retval 0 on success * @retval -EINVAL if an unknown message is received @@ -692,13 +692,13 @@ exit_error: * and mqtt_rx_pingresp return codes */ 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; struct net_buf *data = NULL; 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) { rc = -ENOMEM; goto exit_parser; @@ -736,7 +736,7 @@ exit_parser: ctx->malformed(ctx, pkt_type); } - net_pkt_unref(data); + net_pkt_frag_unref(data); return rc; } @@ -749,7 +749,7 @@ exit_parser: * (if defined) * * @param ctx MQTT context - * @param rx RX buffer + * @param rx RX packet * * @retval 0 on success * @retval -EINVAL if an unknown message is received @@ -758,13 +758,13 @@ exit_parser: * return codes */ 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; struct net_buf *data = NULL; 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) { rc = -EINVAL; goto exit_parser; @@ -803,13 +803,13 @@ exit_parser: ctx->malformed(ctx, pkt_type); } - net_pkt_unref(data); + net_pkt_frag_unref(data); return rc; } 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) { 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 */ ARG_UNUSED(net_ctx); - if (status || !buf) { + if (status || !pkt) { return; } - if (net_pkt_appdatalen(buf) == 0) { + if (net_pkt_appdatalen(pkt) == 0) { goto lb_exit; } - mqtt->rcv(mqtt, buf); + mqtt->rcv(mqtt, pkt); lb_exit: - net_pkt_unref(buf); + net_pkt_unref(pkt); } int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type) diff --git a/subsys/net/lib/zoap/zoap.c b/subsys/net/lib/zoap/zoap.c index 396866b2357..f0282a1c854 100644 --- a/subsys/net/lib/zoap/zoap.c +++ b/subsys/net/lib/zoap/zoap.c @@ -82,7 +82,7 @@ static int decode_delta(int num, const uint8_t *buf, int16_t buflen, 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, uint8_t **value, uint16_t *vlen) { @@ -141,16 +141,16 @@ static int coap_parse_option(const struct zoap_packet *pkt, 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 = { .delta = 0, .used = 0, - .buflen = pkt->buf->frags->len - offset, - .buf = &pkt->buf->frags->data[offset] }; + .buflen = zpkt->pkt->frags->len - offset, + .buf = &zpkt->pkt->frags->data[offset] }; while (true) { - int r = coap_parse_option(pkt, &context, NULL, NULL); + int r = coap_parse_option(zpkt, &context, NULL, NULL); if (r < 0) { return -EINVAL; @@ -163,68 +163,68 @@ static int coap_parse_options(struct zoap_packet *pkt, unsigned int offset) 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; uint8_t tkl; hdrlen = BASIC_HEADER_SIZE; - if (pkt->buf->frags->len < hdrlen) { + if (zpkt->pkt->frags->len < hdrlen) { return -EINVAL; } - tkl = coap_header_get_tkl(pkt); + tkl = coap_header_get_tkl(zpkt); /* Token lenghts 9-15 are reserved. */ if (tkl > 8) { return -EINVAL; } - if (pkt->buf->frags->len < hdrlen + tkl) { + if (zpkt->pkt->frags->len < hdrlen + tkl) { return -EINVAL; } 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; - if (!buf || !buf->frags) { + if (!pkt || !pkt->frags) { return -EINVAL; } - memset(pkt, 0, sizeof(*pkt)); - pkt->buf = buf; + memset(zpkt, 0, sizeof(*zpkt)); + zpkt->pkt = pkt; - hdrlen = coap_get_header_len(pkt); + hdrlen = coap_get_header_len(zpkt); if (hdrlen < 0) { return -EINVAL; } - optlen = coap_parse_options(pkt, hdrlen); + optlen = coap_parse_options(zpkt, hdrlen); if (optlen < 0) { return -EINVAL; } - if (buf->frags->len < hdrlen + optlen) { + if (pkt->frags->len < hdrlen + optlen) { return -EINVAL; } - if (buf->frags->len <= hdrlen + optlen + 1) { - pkt->start = NULL; + if (pkt->frags->len <= hdrlen + optlen + 1) { + zpkt->start = NULL; return 0; } - pkt->start = buf->frags->data + hdrlen + optlen + 1; - pkt->total_size = buf->frags->len; + zpkt->start = pkt->frags->data + hdrlen + optlen + 1; + zpkt->total_size = pkt->frags->len; return 0; } @@ -298,30 +298,30 @@ static int coap_option_encode(struct option_context *context, uint16_t code, return offset + len; } -int zoap_packet_init(struct zoap_packet *pkt, - struct net_buf *buf) +int zoap_packet_init(struct zoap_packet *zpkt, + struct net_pkt *pkt) { void *data; - if (!buf || !buf->frags) { + if (!pkt || !pkt->frags) { return -EINVAL; } - if (net_buf_tailroom(buf->frags) < BASIC_HEADER_SIZE) { + if (net_buf_tailroom(pkt->frags) < BASIC_HEADER_SIZE) { return -ENOMEM; } - memset(pkt, 0, sizeof(*pkt)); - pkt->total_size = net_buf_tailroom(buf->frags); + memset(zpkt, 0, sizeof(*zpkt)); + 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 * the header to be sure. */ memset(data, 0, BASIC_HEADER_SIZE); - pkt->buf = buf; + zpkt->pkt = pkt; return 0; } @@ -335,7 +335,7 @@ int zoap_pending_init(struct zoap_pending *pending, memcpy(&pending->addr, addr, sizeof(*addr)); /* Will increase the reference count when the pending is cycled */ - pending->buf = request->buf; + pending->pkt = request->pkt; return 0; } @@ -347,7 +347,7 @@ struct zoap_pending *zoap_pending_next_unused( size_t i; for (i = 0, p = pendings; i < len; i++, p++) { - if (p->timeout == 0 && !p->buf) { + if (p->timeout == 0 && !p->pkt) { return p; } } @@ -472,7 +472,7 @@ bool zoap_pending_cycle(struct zoap_pending *pending) * When it it is the last retransmission, the buffer * will be destroyed when it is transmitted. */ - net_pkt_ref(pending->buf); + net_pkt_ref(pending->pkt); } return cont; @@ -481,18 +481,18 @@ bool zoap_pending_cycle(struct zoap_pending *pending) void zoap_pending_clear(struct zoap_pending *pending) { pending->timeout = 0; - net_pkt_unref(pending->buf); - pending->buf = NULL; + net_pkt_unref(pending->pkt); + 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) { struct zoap_option options[16]; uint16_t count = 16; 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) { 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); } -int zoap_handle_request(struct zoap_packet *pkt, +int zoap_handle_request(struct zoap_packet *zpkt, struct zoap_resource *resources, const struct sockaddr *from) { struct zoap_resource *resource; - if (!is_request(pkt)) { + if (!is_request(zpkt)) { return 0; } @@ -555,18 +555,18 @@ int zoap_handle_request(struct zoap_packet *pkt, uint8_t code; /* FIXME: deal with hierarchical resources */ - if (!uri_path_eq(pkt, resource->path)) { + if (!uri_path_eq(zpkt, resource->path)) { continue; } - code = zoap_header_get_code(pkt); + code = zoap_header_get_code(zpkt); method = method_from_code(resource, code); if (!method) { return 0; } - return method(resource, pkt, from); + return method(resource, zpkt, from); } return -ENOENT; @@ -594,13 +594,13 @@ unsigned int zoap_option_value_to_int(const struct zoap_option *option) 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 = {}; uint16_t count = 1; 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) { return -ENOENT; } @@ -797,56 +797,56 @@ struct zoap_observer *zoap_find_observer_by_addr( 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; - uint16_t appdatalen = pkt->buf->frags->len; + uint8_t *appdata = zpkt->pkt->frags->data; + uint16_t appdatalen = zpkt->pkt->frags->len; - if (!pkt || !len) { + if (!zpkt || !len) { return NULL; } - if (!pkt->start) { - if (appdatalen + 1 >= pkt->total_size) { + if (!zpkt->start) { + if (appdatalen + 1 >= zpkt->total_size) { return NULL; } 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) > - net_buf_tailroom(pkt->buf->frags)) { + if ((zpkt->pkt->frags->len + len) > + net_buf_tailroom(zpkt->pkt->frags)) { return -ENOMEM; } - pkt->buf->frags->len += len; + zpkt->pkt->frags->len += len; 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) { - struct net_buf *frag = pkt->buf->frags; + struct net_buf *frag = zpkt->pkt->frags; struct option_context context = { .delta = 0, .used = 0 }; int r, offset; - if (pkt->start) { + if (zpkt->start) { return -EINVAL; } - offset = coap_get_header_len(pkt); + offset = coap_get_header_len(zpkt); if (offset < 0) { return -EINVAL; } @@ -856,7 +856,7 @@ int zoap_add_option(struct zoap_packet *pkt, uint16_t code, context.buf = frag->data + offset; while (context.delta <= code) { - r = coap_parse_option(pkt, &context, NULL, NULL); + r = coap_parse_option(zpkt, &context, NULL, NULL); if (r < 0) { return -ENOENT; } @@ -883,7 +883,7 @@ int zoap_add_option(struct zoap_packet *pkt, uint16_t code, 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) { uint8_t data[4], len; @@ -906,19 +906,19 @@ int zoap_add_option_int(struct zoap_packet *pkt, uint16_t code, 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 net_buf *frag = pkt->buf->frags; + struct net_buf *frag = zpkt->pkt->frags; struct option_context context = { .delta = 0, .used = 0 }; int hdrlen, count = 0; uint16_t len; - hdrlen = coap_get_header_len(pkt); + hdrlen = coap_get_header_len(zpkt); if (hdrlen < 0) { 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; 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, &len); options[count].len = len; @@ -949,26 +949,26 @@ int zoap_find_options(const struct zoap_packet *pkt, uint16_t code, 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) { - struct net_buf *frag = pkt->buf->frags; - uint8_t tkl = coap_header_get_tkl(pkt); + struct net_buf *frag = zpkt->pkt->frags; + uint8_t tkl = coap_header_get_tkl(zpkt); if (len) { *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; } -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) { /* 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) { - struct net_buf *frag = pkt->buf->frags; + struct net_buf *frag = zpkt->pkt->frags; uint8_t *appdata = frag->data; 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; } -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, @@ -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_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) { uint16_t bytes = zoap_block_size_to_bytes(ctx->block_size); unsigned int val = 0; int r; - if (is_request(pkt)) { + if (is_request(zpkt)) { SET_BLOCK_SIZE(val, ctx->block_size); SET_MORE(val, ctx->current + bytes < ctx->total_size); 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); } - r = zoap_add_option_int(pkt, ZOAP_OPTION_BLOCK1, val); + r = zoap_add_option_int(zpkt, ZOAP_OPTION_BLOCK1, val); 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) { int r, val = 0; 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_NUM(val, ctx->current / bytes); } else { @@ -1128,21 +1128,21 @@ int zoap_add_block2_option(struct zoap_packet *pkt, 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; } -int zoap_add_size1_option(struct zoap_packet *pkt, +int zoap_add_size1_option(struct zoap_packet *zpkt, 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) { - 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 { @@ -1151,13 +1151,13 @@ struct block_transfer { 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; unsigned int val; int count = 1; - count = zoap_find_options(pkt, code, &option, count); + count = zoap_find_options(zpkt, code, &option, count); if (count <= 0) { return -ENOENT; } @@ -1243,20 +1243,20 @@ static int update_control_block2(struct zoap_block_context *ctx, 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) { int r, block1, block2, size1, size2; - block1 = get_block_option(pkt, ZOAP_OPTION_BLOCK1); - block2 = get_block_option(pkt, ZOAP_OPTION_BLOCK2); - size1 = get_block_option(pkt, ZOAP_OPTION_SIZE1); - size2 = get_block_option(pkt, ZOAP_OPTION_SIZE2); + block1 = get_block_option(zpkt, ZOAP_OPTION_BLOCK1); + block2 = get_block_option(zpkt, ZOAP_OPTION_BLOCK2); + size1 = get_block_option(zpkt, ZOAP_OPTION_SIZE1); + size2 = get_block_option(zpkt, ZOAP_OPTION_SIZE2); size1 = size1 == -ENOENT ? 0 : size1; size2 = size2 == -ENOENT ? 0 : size2; - if (is_request(pkt)) { + if (is_request(zpkt)) { r = update_control_block2(ctx, block2, size2); if (r) { return r; diff --git a/subsys/net/lib/zoap/zoap_link_format.c b/subsys/net/lib/zoap/zoap_link_format.c index ce845832ca9..daa2e842e89 100644 --- a/subsys/net/lib/zoap/zoap_link_format.c +++ b/subsys/net/lib/zoap/zoap_link_format.c @@ -223,30 +223,31 @@ static int send_error_response(struct zoap_resource *resource, { struct net_context *context; struct zoap_packet response; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint16_t id; int r; 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); - if (!buf) { + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { return -ENOMEM; } frag = net_pkt_get_data(context, K_FOREVER); if (!frag) { - net_pkt_unref(buf); + net_pkt_unref(pkt); 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) { - net_pkt_unref(buf); + net_pkt_unref(pkt); 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_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); if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return r; @@ -272,7 +273,8 @@ int _zoap_well_known_core_get(struct zoap_resource *resource, struct net_context *context; struct zoap_packet response; struct zoap_option query; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; const uint8_t *token; unsigned int num_queries; uint16_t id; @@ -293,22 +295,22 @@ int _zoap_well_known_core_get(struct zoap_resource *resource, num_queries = r; - context = net_pkt_context(request->buf); + context = net_pkt_context(request->pkt); - buf = net_pkt_get_tx(context, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_tx(context, K_FOREVER); + if (!pkt) { return -ENOMEM; } frag = net_pkt_get_data(context, K_FOREVER); if (!frag) { - net_pkt_unref(buf); + net_pkt_unref(pkt); 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) { 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, &format, sizeof(format)); if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return -EINVAL; } @@ -342,18 +344,18 @@ int _zoap_well_known_core_get(struct zoap_resource *resource, } if (!response.start) { - temp = response.buf->frags; + temp = response.pkt->frags; str = net_buf_add(temp, 1); *str = 0xFF; response.start = str + 1; } else { temp = net_pkt_get_data(context, K_FOREVER); if (!temp) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return -ENOMEM; } - net_buf_frag_add(buf, temp); + net_pkt_frag_add(pkt, 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: if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); 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); if (r < 0) { - net_pkt_unref(buf); + net_pkt_unref(pkt); } return r; diff --git a/tests/net/6lo/prj.conf b/tests/net/6lo/prj.conf index 6dda3ddcf6c..d7fdfebd6fa 100644 --- a/tests/net/6lo/prj.conf +++ b/tests/net/6lo/prj.conf @@ -14,11 +14,12 @@ CONFIG_NET_PKT_TX_COUNT=1 CONFIG_NET_BUF_RX_COUNT=3 CONFIG_NET_BUF_TX_COUNT=3 CONFIG_NET_LOG=y +CONFIG_SYS_LOG_NET_LEVEL=2 CONFIG_SYS_LOG_SHOW_COLOR=y CONFIG_NET_DEBUG_IF=y CONFIG_NET_DEBUG_CORE=y -CONFIG_NET_DEBUG_6LO=n -CONFIG_NET_DEBUG_NET_PKT=n +CONFIG_NET_DEBUG_6LO=y +CONFIG_NET_DEBUG_NET_PKT=y CONFIG_NET_6LO_CONTEXT=y #Before modifying this value, add respective code in src/main.c diff --git a/tests/net/6lo/src/main.c b/tests/net/6lo/src/main.c index 4b52257cb72..5edadbd5baf 100644 --- a/tests/net/6lo/src/main.c +++ b/tests/net/6lo/src/main.c @@ -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); } -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; } @@ -214,7 +214,7 @@ NET_DEVICE_INIT(net_6lo_test, "net_6lo_test", CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &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; 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; if (data->nh_udp) { - if (net_buf_frags_len(buf->frags) != + if (net_pkt_get_len(pkt) != (NET_IPV6UDPH_LEN + remaining)) { TC_PRINT("mismatch lengths, expected %d received %zu\n", NET_IPV6UDPH_LEN + remaining, - net_buf_frags_len(buf->frags)); + net_pkt_get_len(pkt)); return false; } } else if (data->nh_icmp) { - if (net_buf_frags_len(buf->frags) != + if (net_pkt_get_len(pkt) != (NET_IPV6ICMPH_LEN + remaining)) { TC_PRINT("mismatch lengths, expected %d received %zu\n", NET_IPV6ICMPH_LEN + remaining, - net_buf_frags_len(buf->frags)); + net_pkt_get_len(pkt)); return false; } } else { - if (net_buf_frags_len(buf->frags) != + if (net_pkt_get_len(pkt) != (NET_IPV6H_LEN + remaining)) { TC_PRINT("mismatch lengths, expected %d received %zu\n", NET_IPV6H_LEN + remaining, - net_buf_frags_len(buf->frags)); + net_pkt_get_len(pkt)); return false; } } - frag = buf->frags; + frag = pkt->frags; if (data->nh_udp) { 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; } -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; uint16_t len; int remaining; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); + if (!pkt) { return NULL; } - net_pkt_set_iface(buf, net_if_get_default()); - net_pkt_set_ip_hdr_len(buf, NET_IPV6H_LEN); + net_pkt_set_iface(pkt, net_if_get_default()); + net_pkt_set_ip_hdr_len(pkt, NET_IPV6H_LEN); - net_pkt_ll_src(buf)->addr = src_mac; - net_pkt_ll_src(buf)->len = 8; + net_pkt_ll_src(pkt)->addr = src_mac; + net_pkt_ll_src(pkt)->len = 8; - net_pkt_ll_dst(buf)->addr = dst_mac; - net_pkt_ll_dst(buf)->len = 8; + net_pkt_ll_dst(pkt)->addr = dst_mac; + 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) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } @@ -377,18 +378,18 @@ static struct net_buf *create_buf(struct net_6lo_data *data) remaining -= bytes; if (net_buf_tailroom(frag) - (bytes - copy)) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); 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 = { @@ -798,49 +799,49 @@ static struct net_6lo_data test_data_22 = { static int test_6lo(struct net_6lo_data *data) { - struct net_buf *buf; + struct net_pkt *pkt; int result = TC_FAIL; - buf = create_buf(data); - if (!buf) { + pkt = create_pkt(data); + if (!pkt) { TC_PRINT("%s: failed to create buffer\n", __func__); goto end; } #if DEBUG > 0 TC_PRINT("length before compression %zu\n", - net_buf_frags_len(buf->frags)); - net_hexdump_frags("before-compression", buf); + net_pkt_get_len(pkt)); + net_hexdump_frags("before-compression", pkt); #endif - if (!net_6lo_compress(buf, data->iphc, NULL)) { + if (!net_6lo_compress(pkt, data->iphc, NULL)) { TC_PRINT("compression failed\n"); goto end; } #if DEBUG > 0 TC_PRINT("length after compression %zu\n", - net_buf_frags_len(buf->frags)); - net_hexdump_frags("after-compression", buf); + net_pkt_get_len(pkt)); + net_hexdump_frags("after-compression", pkt); #endif - if (!net_6lo_uncompress(buf)) { + if (!net_6lo_uncompress(pkt)) { TC_PRINT("uncompression failed\n"); goto end; } #if DEBUG > 0 TC_PRINT("length after uncompression %zu\n", - net_buf_frags_len(buf->frags)); - net_hexdump_frags("after-uncompression", buf); + net_pkt_get_len(pkt)); + net_hexdump_frags("after-uncompression", pkt); #endif - if (compare_data(buf, data)) { + if (compare_data(pkt, data)) { result = TC_PASS; } end: - net_pkt_unref(buf); + net_pkt_unref(pkt); return result; } diff --git a/tests/net/arp/src/main.c b/tests/net/arp/src/main.c index 7a402626d72..1fa2bdb0d1f 100644 --- a/tests/net/arp/src/main.c +++ b/tests/net/arp/src/main.c @@ -67,38 +67,38 @@ static void net_arp_iface_init(struct net_if *iface) net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET); } -static struct net_buf *pending_buf; +static struct net_pkt *pending_pkt; static struct net_eth_addr hwaddr = { { 0x42, 0x11, 0x69, 0xde, 0xfa, 0xec } }; static int send_status = -EINVAL; -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { struct net_eth_hdr *hdr; - if (!buf->frags) { + if (!pkt->frags) { printk("No data to send!\n"); return -ENODATA; } - if (net_pkt_ll_reserve(buf) != sizeof(struct net_eth_hdr)) { - printk("No ethernet header in buf %p", buf); + if (net_pkt_ll_reserve(pkt) != sizeof(struct net_eth_hdr)) { + printk("No ethernet header in pkt %p", pkt); send_status = -EINVAL; return send_status; } - hdr = (struct net_eth_hdr *)net_pkt_ll(buf); + hdr = (struct net_eth_hdr *)net_pkt_ll(pkt); if (ntohs(hdr->type) == NET_ETH_PTYPE_ARP) { - struct net_arp_hdr *arp_hdr = NET_ARP_BUF(buf); + struct net_arp_hdr *arp_hdr = NET_ARP_BUF(pkt); if (ntohs(arp_hdr->opcode) == NET_ARP_REPLY) { - if (!req_test && buf != pending_buf) { + if (!req_test && pkt != pending_pkt) { printk("Pending data but to be sent is wrong, " "expecting %p but got %p\n", - pending_buf, buf); + pending_pkt, pkt); return -EINVAL; } @@ -139,7 +139,7 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) printk("Data was sent successfully\n"); - net_pkt_unref(buf); + net_pkt_unref(pkt); send_status = 0; @@ -161,29 +161,30 @@ static inline struct in_addr *if_get_addr(struct net_if *iface) return NULL; } -static inline struct net_buf *prepare_arp_reply(struct net_if *iface, - struct net_buf *req, +static inline struct net_pkt *prepare_arp_reply(struct net_if *iface, + struct net_pkt *req, struct net_eth_addr *addr) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_arp_hdr *hdr; struct net_eth_hdr *eth; - buf = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); + if (!pkt) { goto fail; } - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { goto fail; } - net_buf_frag_add(buf, frag); - net_pkt_set_iface(buf, iface); + net_pkt_frag_add(pkt, frag); + net_pkt_set_iface(pkt, iface); - hdr = NET_ARP_BUF(buf); - eth = NET_ETH_BUF(buf); + hdr = NET_ARP_BUF(pkt); + eth = NET_ETH_BUF(pkt); eth->type = htons(NET_ETH_PTYPE_ARP); @@ -207,37 +208,38 @@ static inline struct net_buf *prepare_arp_reply(struct net_if *iface, net_buf_add(frag, sizeof(struct net_arp_hdr)); - return buf; + return pkt; fail: - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } -static inline struct net_buf *prepare_arp_request(struct net_if *iface, - struct net_buf *req, +static inline struct net_pkt *prepare_arp_request(struct net_if *iface, + struct net_pkt *req, struct net_eth_addr *addr) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_arp_hdr *hdr, *req_hdr; struct net_eth_hdr *eth, *eth_req; - buf = net_pkt_get_reserve_rx(sizeof(struct net_eth_hdr), + pkt = net_pkt_get_reserve_rx(sizeof(struct net_eth_hdr), K_FOREVER); - if (!buf) { + if (!pkt) { goto fail; } - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { goto fail; } - net_buf_frag_add(buf, frag); - net_pkt_set_iface(buf, iface); + net_pkt_frag_add(pkt, frag); + net_pkt_set_iface(pkt, iface); - hdr = NET_ARP_BUF(buf); - eth = NET_ETH_BUF(buf); + hdr = NET_ARP_BUF(pkt); + eth = NET_ETH_BUF(pkt); req_hdr = NET_ARP_BUF(req); eth_req = NET_ETH_BUF(req); @@ -260,17 +262,17 @@ static inline struct net_buf *prepare_arp_request(struct net_if *iface, net_buf_add(frag, sizeof(struct net_arp_hdr)); - return buf; + return pkt; fail: - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } -static void setup_eth_header(struct net_if *iface, struct net_buf *buf, +static void setup_eth_header(struct net_if *iface, struct net_pkt *pkt, struct net_eth_addr *hwaddr, uint16_t type) { - struct net_eth_hdr *hdr = (struct net_eth_hdr *)net_pkt_ll(buf); + struct net_eth_hdr *hdr = (struct net_eth_hdr *)net_pkt_ll(pkt); memcpy(&hdr->dst.addr, hwaddr, sizeof(struct net_eth_addr)); memcpy(&hdr->src.addr, net_if_get_link_addr(iface)->addr, @@ -301,7 +303,7 @@ NET_DEVICE_INIT(net_arp_test, "net_arp_test", static bool run_tests(void) { - struct net_buf *buf, *buf2; + struct net_pkt *pkt, *pkt2; struct net_buf *frag; struct net_if *iface; struct net_if_addr *ifaddr; @@ -332,30 +334,30 @@ static bool run_tests(void) ifaddr->addr_state = NET_ADDR_PREFERRED; /* Application data for testing */ - buf = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(sizeof(struct net_eth_hdr), K_FOREVER); + if (!pkt) { printk("Out of mem TX\n"); return false; } - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { printk("Out of mem DATA\n"); return false; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_pkt_set_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - setup_eth_header(iface, buf, &hwaddr, NET_ETH_PTYPE_IP); + setup_eth_header(iface, pkt, &hwaddr, NET_ETH_PTYPE_IP); len = strlen(app_data); - if (net_pkt_ll_reserve(buf) != sizeof(struct net_eth_hdr)) { + if (net_pkt_ll_reserve(pkt) != sizeof(struct net_eth_hdr)) { printk("LL reserve invalid, should be %zd was %d\n", sizeof(struct net_eth_hdr), - net_pkt_ll_reserve(buf)); + net_pkt_ll_reserve(pkt)); return false; } @@ -366,38 +368,38 @@ static bool run_tests(void) memcpy(net_buf_add(frag, len), app_data, len); - buf2 = net_arp_prepare(buf); + pkt2 = net_arp_prepare(pkt); - /* buf2 is the ARP buffer and buf is the IPv4 buffer and it was + /* pkt2 is the ARP packet and pkt is the IPv4 packet and it was * stored in ARP table. */ - if (buf2 == buf) { - /* The buffers cannot be the same as the ARP cache has - * still room for the buf. + if (pkt2 == pkt) { + /* The packets cannot be the same as the ARP cache has + * still room for the pkt. */ printk("ARP cache should still have free space\n"); return false; } - if (!buf2) { - printk("ARP buf is empty\n"); + if (!pkt2) { + printk("ARP pkt is empty\n"); return false; } - /* The ARP cache should now have a link to pending net_buf + /* The ARP cache should now have a link to pending net_pkt * that is to be sent after we have got an ARP reply. */ - if (!buf->frags) { - printk("Pending buf fragment is NULL\n"); + if (!pkt->frags) { + printk("Pending pkt fragment is NULL\n"); return false; } - pending_buf = buf; + pending_pkt = pkt; - /* buf2 should contain the arp header, verify it */ - if (memcmp(net_pkt_ll(buf2), net_eth_broadcast_addr(), + /* pkt2 should contain the arp header, verify it */ + if (memcmp(net_pkt_ll(pkt2), net_eth_broadcast_addr(), sizeof(struct net_eth_addr))) { printk("ARP ETH dest address invalid\n"); - net_hexdump("ETH dest wrong ", net_pkt_ll(buf2), + net_hexdump("ETH dest wrong ", net_pkt_ll(pkt2), sizeof(struct net_eth_addr)); net_hexdump("ETH dest correct", (uint8_t *)net_eth_broadcast_addr(), @@ -405,7 +407,7 @@ static bool run_tests(void) return false; } - if (memcmp(net_pkt_ll(buf2) + sizeof(struct net_eth_addr), + if (memcmp(net_pkt_ll(pkt2) + sizeof(struct net_eth_addr), iface->link_addr.addr, sizeof(struct net_eth_addr))) { printk("ARP ETH source address invalid\n"); @@ -413,13 +415,13 @@ static bool run_tests(void) iface->link_addr.addr, sizeof(struct net_eth_addr)); net_hexdump("ETH src wrong ", - net_pkt_ll(buf2) + sizeof(struct net_eth_addr), + net_pkt_ll(pkt2) + sizeof(struct net_eth_addr), sizeof(struct net_eth_addr)); return false; } - arp_hdr = NET_ARP_BUF(buf2); - eth_hdr = NET_ETH_BUF(buf2); + arp_hdr = NET_ARP_BUF(pkt2); + eth_hdr = NET_ETH_BUF(pkt2); if (eth_hdr->type != htons(NET_ETH_PTYPE_ARP)) { printk("ETH type 0x%x, should be 0x%x\n", @@ -458,51 +460,51 @@ static bool run_tests(void) } if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr, - &NET_IPV4_BUF(buf)->dst)) { + &NET_IPV4_BUF(pkt)->dst)) { char out[sizeof("xxx.xxx.xxx.xxx")]; snprintk(out, sizeof(out), "%s", net_sprint_ipv4_addr(&arp_hdr->dst_ipaddr)); printk("ARP IP dest invalid %s, should be %s", out, - net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst)); + net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst)); return false; } if (!net_ipv4_addr_cmp(&arp_hdr->src_ipaddr, - &NET_IPV4_BUF(buf)->src)) { + &NET_IPV4_BUF(pkt)->src)) { char out[sizeof("xxx.xxx.xxx.xxx")]; snprintk(out, sizeof(out), "%s", net_sprint_ipv4_addr(&arp_hdr->src_ipaddr)); printk("ARP IP src invalid %s, should be %s", out, - net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src)); + net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src)); return false; } /* We could have send the new ARP request but for this test we * just free it. */ - net_pkt_unref(buf2); + net_pkt_unref(pkt2); - if (buf->ref != 2) { - printk("ARP cache should own the original buffer\n"); + if (pkt->ref != 2) { + printk("ARP cache should own the original packet\n"); return false; } /* Then a case where target is not in the same subnet */ net_ipaddr_copy(&ipv4->dst, &dst_far); - buf2 = net_arp_prepare(buf); + pkt2 = net_arp_prepare(pkt); - if (buf2 == buf) { + if (pkt2 == pkt) { printk("ARP cache should not find anything\n"); return false; } - if (!buf2) { - printk("ARP buf2 is empty\n"); + if (!pkt2) { + printk("ARP pkt2 is empty\n"); return false; } - arp_hdr = NET_ARP_BUF(buf2); + arp_hdr = NET_ARP_BUF(pkt2); if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr, &iface->ipv4.gw)) { char out[sizeof("xxx.xxx.xxx.xxx")]; @@ -513,38 +515,38 @@ static bool run_tests(void) return false; } - net_pkt_unref(buf2); + net_pkt_unref(pkt2); /* Try to find the same destination again, this should fail as there * is a pending request in ARP cache. */ net_ipaddr_copy(&ipv4->dst, &dst_far); - /* Make sure prepare will not free the buf because it will be + /* Make sure prepare will not free the pkt because it will be * needed in the later test case. */ - net_buf_ref(buf); + net_pkt_ref(pkt); - buf2 = net_arp_prepare(buf); - if (!buf2) { + pkt2 = net_arp_prepare(pkt); + if (!pkt2) { printk("ARP cache is not sending the request again\n"); return false; } - net_pkt_unref(buf2); + net_pkt_unref(pkt2); /* Try to find the different destination, this should fail too * as the cache table should be full. */ net_ipaddr_copy(&ipv4->dst, &dst_far2); - /* Make sure prepare will not free the buf because it will be + /* Make sure prepare will not free the pkt because it will be * needed in the next test case. */ - net_buf_ref(buf); + net_pkt_ref(pkt); - buf2 = net_arp_prepare(buf); - if (!buf2) { + pkt2 = net_arp_prepare(pkt); + if (!pkt2) { printk("ARP cache did not send a req\n"); return false; } @@ -555,40 +557,40 @@ static bool run_tests(void) net_ipaddr_copy(&ipv4->dst, &dst); /* The arp request packet is now verified, create an arp reply. - * The previous value of buf is stored in arp table and is not lost. + * The previous value of pkt is stored in arp table and is not lost. */ - buf = net_pkt_get_reserve_rx(sizeof(struct net_eth_hdr), K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_rx(sizeof(struct net_eth_hdr), K_FOREVER); + if (!pkt) { printk("Out of mem RX reply\n"); return false; } - printk("%d buf %p\n", __LINE__, buf); + printk("%d pkt %p\n", __LINE__, pkt); - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { printk("Out of mem DATA reply\n"); return false; } printk("%d frag %p\n", __LINE__, frag); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_pkt_set_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - arp_hdr = NET_ARP_BUF(buf); + arp_hdr = NET_ARP_BUF(pkt); net_buf_add(frag, sizeof(struct net_arp_hdr)); net_ipaddr_copy(&arp_hdr->dst_ipaddr, &dst); net_ipaddr_copy(&arp_hdr->src_ipaddr, &src); - buf2 = prepare_arp_reply(iface, buf, &hwaddr); - if (!buf2) { + pkt2 = prepare_arp_reply(iface, pkt, &hwaddr); + if (!pkt2) { printk("ARP reply generation failed."); return false; } /* The pending packet should now be sent */ - switch (net_arp_input(buf2)) { + switch (net_arp_input(pkt2)) { case NET_OK: case NET_CONTINUE: break; @@ -604,47 +606,47 @@ static bool run_tests(void) return false; } - if (buf->ref != 1) { - printk("ARP cache should no longer own the original buffer\n"); + if (pkt->ref != 1) { + printk("ARP cache should no longer own the original packet\n"); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); /* Then feed in ARP request */ - buf = net_pkt_get_reserve_rx(sizeof(struct net_eth_hdr), K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_rx(sizeof(struct net_eth_hdr), K_FOREVER); + if (!pkt) { printk("Out of mem RX request\n"); return false; } - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { printk("Out of mem DATA request\n"); return false; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_pkt_set_iface(buf, iface); + net_pkt_set_iface(pkt, iface); send_status = -EINVAL; - arp_hdr = NET_ARP_BUF(buf); + arp_hdr = NET_ARP_BUF(pkt); net_buf_add(frag, sizeof(struct net_arp_hdr)); net_ipaddr_copy(&arp_hdr->dst_ipaddr, &src); net_ipaddr_copy(&arp_hdr->src_ipaddr, &dst); - setup_eth_header(iface, buf, &hwaddr, NET_ETH_PTYPE_ARP); + setup_eth_header(iface, pkt, &hwaddr, NET_ETH_PTYPE_ARP); - buf2 = prepare_arp_request(iface, buf, &hwaddr); - if (!buf2) { + pkt2 = prepare_arp_request(iface, pkt, &hwaddr); + if (!pkt2) { printk("ARP request generation failed."); return false; } req_test = true; - switch (net_arp_input(buf2)) { + switch (net_arp_input(pkt2)) { case NET_OK: case NET_CONTINUE: break; @@ -660,7 +662,7 @@ static bool run_tests(void) return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); printk("Network ARP checks passed\n"); diff --git a/tests/net/context/src/main.c b/tests/net/context/src/main.c index 7451eb3b323..43ab37f5afe 100644 --- a/tests/net/context/src/main.c +++ b/tests/net/context/src/main.c @@ -497,22 +497,23 @@ static void send_cb(struct net_context *context, int status, static bool net_ctx_send_v6(void) { int ret, len; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; - buf = net_pkt_get_tx(udp_v6_ctx, K_FOREVER); + pkt = net_pkt_get_tx(udp_v6_ctx, K_FOREVER); frag = net_pkt_get_data(udp_v6_ctx, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = strlen(test_data); memcpy(net_buf_add(frag, len), test_data, len); - net_pkt_set_appdatalen(buf, len); + net_pkt_set_appdatalen(pkt, len); test_token = SENDING; - ret = net_context_send(buf, send_cb, 0, INT_TO_POINTER(test_token), + ret = net_context_send(pkt, send_cb, 0, INT_TO_POINTER(test_token), INT_TO_POINTER(AF_INET6)); if (ret || cb_failure) { TC_ERROR("Context send IPv6 UDP test failed (%d)\n", ret); @@ -525,22 +526,23 @@ static bool net_ctx_send_v6(void) static bool net_ctx_send_v4(void) { int ret, len; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; - buf = net_pkt_get_tx(udp_v4_ctx, K_FOREVER); + pkt = net_pkt_get_tx(udp_v4_ctx, K_FOREVER); frag = net_pkt_get_data(udp_v4_ctx, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = strlen(test_data); memcpy(net_buf_add(frag, len), test_data, len); - net_pkt_set_appdatalen(buf, len); + net_pkt_set_appdatalen(pkt, len); test_token = SENDING; - ret = net_context_send(buf, send_cb, 0, INT_TO_POINTER(test_token), + ret = net_context_send(pkt, send_cb, 0, INT_TO_POINTER(test_token), INT_TO_POINTER(AF_INET)); if (ret || cb_failure) { TC_ERROR("Context send IPv4 UDP test failed (%d)\n", ret); @@ -553,7 +555,8 @@ static bool net_ctx_send_v4(void) static bool net_ctx_sendto_v6(void) { int ret, len; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct sockaddr_in6 addr = { .sin6_family = AF_INET6, .sin6_port = htons(PEER_PORT), @@ -561,20 +564,20 @@ static bool net_ctx_sendto_v6(void) 0, 0, 0, 0, 0, 0, 0, 0x2 } } }, }; - buf = net_pkt_get_tx(udp_v6_ctx, K_FOREVER); + pkt = net_pkt_get_tx(udp_v6_ctx, K_FOREVER); frag = net_pkt_get_data(udp_v6_ctx, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = strlen(test_data); memcpy(net_buf_add(frag, len), test_data, len); - net_pkt_set_appdatalen(buf, len); + net_pkt_set_appdatalen(pkt, len); test_token = SENDING; - ret = net_context_sendto(buf, (struct sockaddr *)&addr, + ret = net_context_sendto(pkt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6), send_cb, 0, INT_TO_POINTER(test_token), @@ -590,27 +593,28 @@ static bool net_ctx_sendto_v6(void) static bool net_ctx_sendto_v4(void) { int ret, len; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(PEER_PORT), .sin_addr = { { { 192, 0, 2, 2 } } }, }; - buf = net_pkt_get_tx(udp_v4_ctx, K_FOREVER); + pkt = net_pkt_get_tx(udp_v4_ctx, K_FOREVER); frag = net_pkt_get_data(udp_v4_ctx, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = strlen(test_data); memcpy(net_buf_add(frag, len), test_data, len); - net_pkt_set_appdatalen(buf, len); + net_pkt_set_appdatalen(pkt, len); test_token = SENDING; - ret = net_context_sendto(buf, (struct sockaddr *)&addr, + ret = net_context_sendto(pkt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in), send_cb, 0, INT_TO_POINTER(test_token), @@ -624,7 +628,7 @@ static bool net_ctx_sendto_v4(void) } static void recv_cb(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -687,7 +691,8 @@ static bool net_ctx_recv_v4(void) static bool net_ctx_sendto_v6_wrong_src(void) { int ret, len; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct sockaddr_in6 addr = { .sin6_family = AF_INET6, .sin6_port = htons(PEER_PORT), @@ -695,20 +700,20 @@ static bool net_ctx_sendto_v6_wrong_src(void) 0, 0, 0, 0, 0, 0, 0, 0x3 } } }, }; - buf = net_pkt_get_tx(udp_v6_ctx, K_FOREVER); + pkt = net_pkt_get_tx(udp_v6_ctx, K_FOREVER); frag = net_pkt_get_data(udp_v6_ctx, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = strlen(test_data); memcpy(net_buf_add(frag, len), test_data, len); - net_pkt_set_appdatalen(buf, len); + net_pkt_set_appdatalen(pkt, len); test_token = SENDING; - ret = net_context_sendto(buf, (struct sockaddr *)&addr, + ret = net_context_sendto(pkt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in6), send_cb, 0, INT_TO_POINTER(test_token), @@ -745,27 +750,28 @@ static bool net_ctx_recv_v6_fail(void) static bool net_ctx_sendto_v4_wrong_src(void) { int ret, len; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct sockaddr_in addr = { .sin_family = AF_INET, .sin_port = htons(PEER_PORT), .sin_addr = { { { 192, 0, 2, 3 } } }, }; - buf = net_pkt_get_tx(udp_v4_ctx, K_FOREVER); + pkt = net_pkt_get_tx(udp_v4_ctx, K_FOREVER); frag = net_pkt_get_data(udp_v4_ctx, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = strlen(test_data); memcpy(net_buf_add(frag, len), test_data, len); - net_pkt_set_appdatalen(buf, len); + net_pkt_set_appdatalen(pkt, len); test_token = SENDING; - ret = net_context_sendto(buf, (struct sockaddr *)&addr, + ret = net_context_sendto(pkt, (struct sockaddr *)&addr, sizeof(struct sockaddr_in), send_cb, 0, INT_TO_POINTER(test_token), @@ -833,7 +839,7 @@ static bool net_ctx_recv_v4_again(void) } static void recv_cb_another(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -901,7 +907,7 @@ static bool net_ctx_recv_v4_reconfig(void) char __noinit __stack thread_stack[STACKSIZE]; static void recv_cb_timeout(struct net_context *context, - struct net_buf *buf, + struct net_pkt *pkt, int status, void *user_data) { @@ -1054,9 +1060,9 @@ static void net_context_iface_init(struct net_if *iface) NET_LINK_ETHERNET); } -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { TC_ERROR("No data to send!\n"); return -ENODATA; } @@ -1076,27 +1082,27 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) */ uint16_t port; - if (net_pkt_family(buf) == AF_INET6) { + if (net_pkt_family(pkt) == AF_INET6) { struct in6_addr addr; - net_ipaddr_copy(&addr, &NET_IPV6_BUF(buf)->src); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, - &NET_IPV6_BUF(buf)->dst); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, &addr); + net_ipaddr_copy(&addr, &NET_IPV6_BUF(pkt)->src); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, + &NET_IPV6_BUF(pkt)->dst); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, &addr); } else { struct in_addr addr; - net_ipaddr_copy(&addr, &NET_IPV4_BUF(buf)->src); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, - &NET_IPV4_BUF(buf)->dst); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, &addr); + net_ipaddr_copy(&addr, &NET_IPV4_BUF(pkt)->src); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, + &NET_IPV4_BUF(pkt)->dst); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, &addr); } - port = NET_UDP_BUF(buf)->src_port; - NET_UDP_BUF(buf)->src_port = NET_UDP_BUF(buf)->dst_port; - NET_UDP_BUF(buf)->dst_port = port; + port = NET_UDP_BUF(pkt)->src_port; + NET_UDP_BUF(pkt)->src_port = NET_UDP_BUF(pkt)->dst_port; + NET_UDP_BUF(pkt)->dst_port = port; - if (net_recv_data(iface, buf) < 0) { + if (net_recv_data(iface, pkt) < 0) { TC_ERROR("Data receive failed."); goto out; } @@ -1107,7 +1113,7 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) } out: - net_pkt_unref(buf); + net_pkt_unref(pkt); if (data_failure) { test_failed = true; diff --git a/tests/net/dhcpv4/src/main.c b/tests/net/dhcpv4/src/main.c index dec2897ac46..8c7e1d66bd3 100644 --- a/tests/net/dhcpv4/src/main.c +++ b/tests/net/dhcpv4/src/main.c @@ -199,19 +199,19 @@ static void net_dhcpv4_iface_init(struct net_if *iface) net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET); } -static struct net_buf *pkt_get_data(struct net_buf *buf, struct net_if *iface) +static struct net_buf *pkt_get_data(struct net_pkt *pkt, struct net_if *iface) { struct net_buf *frag; struct net_eth_hdr *hdr; - net_pkt_set_ll_reserve(buf, net_if_get_ll_reserve(iface, NULL)); + net_pkt_set_ll_reserve(pkt, net_if_get_ll_reserve(iface, NULL)); - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { return NULL; } - hdr = (struct net_eth_hdr *)net_pkt_ll(frag); + hdr = (struct net_eth_hdr *)(frag->data - net_pkt_ll_reserve(pkt)); hdr->type = htons(NET_ETH_PTYPE_IP); net_ipaddr_copy(&hdr->dst, &src_addr); @@ -220,12 +220,12 @@ static struct net_buf *pkt_get_data(struct net_buf *buf, struct net_if *iface) return frag; } -static void set_ipv4_header(struct net_buf *buf) +static void set_ipv4_header(struct net_pkt *pkt) { struct net_ipv4_hdr *ipv4; uint16_t length; - ipv4 = NET_IPV4_BUF(buf); + ipv4 = NET_IPV4_BUF(pkt); ipv4->vhl = 0x45; /* IP version and header length */ ipv4->tos = 0x00; @@ -245,12 +245,12 @@ static void set_ipv4_header(struct net_buf *buf) net_ipaddr_copy(&ipv4->dst, &client_addr); } -static void set_udp_header(struct net_buf *buf) +static void set_udp_header(struct net_pkt *pkt) { struct net_udp_hdr *udp; uint16_t length; - udp = NET_UDP_BUF(buf); + udp = NET_UDP_BUF(pkt); udp->src_port = htons(SERVER_PORT); udp->dst_port = htons(CLIENT_PORT); @@ -259,35 +259,36 @@ static void set_udp_header(struct net_buf *buf) udp->chksum = 0; } -struct net_buf *prepare_dhcp_offer(struct net_if *iface, uint32_t xid) +struct net_pkt *prepare_dhcp_offer(struct net_if *iface, uint32_t xid) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; int bytes, remaining = sizeof(offer), pos = 0; uint16_t offset; - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + if (!pkt) { return NULL; } - frag = pkt_get_data(buf, iface); + frag = pkt_get_data(pkt, iface); if (!frag) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } - net_pkt_set_iface(buf, iface); - net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); - net_pkt_set_family(buf, AF_INET); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag)); + net_pkt_set_family(pkt, AF_INET); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr)); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* Place the IPv4 header */ - set_ipv4_header(buf); + set_ipv4_header(pkt); /* Place the UDP header */ - set_udp_header(buf); + set_udp_header(pkt); net_buf_add(frag, NET_IPV4UDPH_LEN); offset = NET_IPV4UDPH_LEN; @@ -306,57 +307,58 @@ struct net_buf *prepare_dhcp_offer(struct net_if *iface, uint32_t xid) remaining -= bytes; if (remaining > 0) { - frag = pkt_get_data(buf, iface); + frag = pkt_get_data(pkt, iface); if (!frag) { goto fail; } offset = 0; - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); } } /* Now fixup the expect XID */ - frag = net_pkt_write_be32(buf, buf->frags, + frag = net_pkt_write_be32(pkt, pkt->frags, (sizeof(struct net_ipv4_hdr) + sizeof(struct net_udp_hdr)) + 4, &offset, xid); - return buf; + return pkt; fail: - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } -struct net_buf *prepare_dhcp_ack(struct net_if *iface, uint32_t xid) +struct net_pkt *prepare_dhcp_ack(struct net_if *iface, uint32_t xid) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; int bytes, remaining = sizeof(ack), pos = 0; uint16_t offset; - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + if (!pkt) { return NULL; } - frag = pkt_get_data(buf, iface); + frag = pkt_get_data(pkt, iface); if (!frag) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } - net_pkt_set_iface(buf, iface); - net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); - net_pkt_set_family(buf, AF_INET); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag)); + net_pkt_set_family(pkt, AF_INET); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr)); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* Place the IPv4 header */ - set_ipv4_header(buf); + set_ipv4_header(pkt); /* Place the UDP header */ - set_udp_header(buf); + set_udp_header(pkt); net_buf_add(frag, NET_IPV4UDPH_LEN); offset = NET_IPV4UDPH_LEN; @@ -375,35 +377,35 @@ struct net_buf *prepare_dhcp_ack(struct net_if *iface, uint32_t xid) remaining -= bytes; if (remaining > 0) { - frag = pkt_get_data(buf, iface); + frag = pkt_get_data(pkt, iface); if (!frag) { goto fail; } offset = 0; - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); } } /* Now fixup the expect XID */ - frag = net_pkt_write_be32(buf, buf->frags, + frag = net_pkt_write_be32(pkt, pkt->frags, (sizeof(struct net_ipv4_hdr) + sizeof(struct net_udp_hdr)) + 4, &offset, xid); - return buf; + return pkt; fail: - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } -static int parse_dhcp_message(struct net_buf *buf, struct dhcp_msg *msg) +static int parse_dhcp_message(struct net_pkt *pkt, struct dhcp_msg *msg) { - struct net_buf *frag = buf->frags; + struct net_buf *frag = pkt->frags; uint8_t type; uint16_t offset; - frag = net_pkt_skip(frag, 0, &offset, + frag = net_frag_skip(frag, 0, &offset, /* size of op, htype, hlen, hops */ (sizeof(struct net_ipv4_hdr) + sizeof(struct net_udp_hdr)) + 4); @@ -411,12 +413,12 @@ static int parse_dhcp_message(struct net_buf *buf, struct dhcp_msg *msg) return 0; } - frag = net_pkt_read_be32(frag, offset, &offset, &msg->xid); + frag = net_frag_read_be32(frag, offset, &offset, &msg->xid); if (!frag) { return 0; } - frag = net_pkt_skip(frag, offset, &offset, + frag = net_frag_skip(frag, offset, &offset, /* size of op, htype ... cookie */ (36 + 64 + 128 + 4)); if (!frag) { @@ -426,18 +428,18 @@ static int parse_dhcp_message(struct net_buf *buf, struct dhcp_msg *msg) while (frag) { uint8_t length; - frag = net_pkt_read_u8(frag, offset, &offset, &type); + frag = net_frag_read_u8(frag, offset, &offset, &type); if (!frag) { return 0; } if (type == MSG_TYPE) { - frag = net_pkt_skip(frag, offset, &offset, 1); + frag = net_frag_skip(frag, offset, &offset, 1); if (!frag) { return 0; } - frag = net_pkt_read_u8(frag, offset, &offset, + frag = net_frag_read_u8(frag, offset, &offset, &msg->type); if (!frag) { return 0; @@ -446,9 +448,9 @@ static int parse_dhcp_message(struct net_buf *buf, struct dhcp_msg *msg) return 1; } - frag = net_pkt_read_u8(frag, offset, &offset, &length); + frag = net_frag_read_u8(frag, offset, &offset, &length); if (frag) { - frag = net_pkt_skip(frag, offset, &offset, length); + frag = net_frag_skip(frag, offset, &offset, length); if (!frag) { return 0; } @@ -458,32 +460,32 @@ static int parse_dhcp_message(struct net_buf *buf, struct dhcp_msg *msg) return 0; } -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - struct net_buf *rbuf; + struct net_pkt *rpkt; struct dhcp_msg msg; memset(&msg, 0, sizeof(msg)); - if (!buf->frags) { + if (!pkt->frags) { TC_PRINT("No data to send!\n"); return -ENODATA; } - parse_dhcp_message(buf, &msg); - net_pkt_unref(buf); + parse_dhcp_message(pkt, &msg); + net_pkt_unref(pkt); if (msg.type == DISCOVER) { /* Reply with DHCPv4 offer message */ - rbuf = prepare_dhcp_offer(iface, msg.xid); - if (!rbuf) { + rpkt = prepare_dhcp_offer(iface, msg.xid); + if (!rpkt) { return -EINVAL; } } else if (msg.type == REQUEST) { /* Reply with DHCPv4 ACK message */ - rbuf = prepare_dhcp_ack(iface, msg.xid); - if (!rbuf) { + rpkt = prepare_dhcp_ack(iface, msg.xid); + if (!rpkt) { return -EINVAL; } @@ -492,8 +494,8 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) return -EINVAL; } - if (net_recv_data(iface, rbuf)) { - net_pkt_unref(rbuf); + if (net_recv_data(iface, rpkt)) { + net_pkt_unref(rpkt); return -EINVAL; } diff --git a/tests/net/icmpv6/src/main.c b/tests/net/icmpv6/src/main.c index 4d0289ba6b1..7e1089ce602 100644 --- a/tests/net/icmpv6/src/main.c +++ b/tests/net/icmpv6/src/main.c @@ -20,27 +20,21 @@ #include "icmpv6.h" static int handler_called; - -struct header { - int status; -}; - -#define buf_status(buf) (((struct header *)net_buf_user_data((buf)))->status) +static int handler_status; #define TEST_MSG "foobar devnull" -NET_BUF_POOL_DEFINE(bufs_pool, 2, 0, sizeof(struct header), NULL); - +NET_PKT_TX_SLAB_DEFINE(pkts_slab, 2); NET_BUF_POOL_DEFINE(data_pool, 2, 128, 0, NULL); -static enum net_verdict handle_test_msg(struct net_buf *buf) +static enum net_verdict handle_test_msg(struct net_pkt *pkt) { - struct net_buf *last = net_buf_frag_last(buf); + struct net_buf *last = net_buf_frag_last(pkt->frags); if (last->len != (strlen(TEST_MSG) + 1)) { - buf_status(buf) = -EINVAL; + handler_status = -EINVAL; } else { - buf_status(buf) = 0; + handler_status = 0; } handler_called++; @@ -62,40 +56,45 @@ static struct net_icmpv6_handler test_handler2 = { static bool run_tests(void) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; int ret; net_icmpv6_register_handler(&test_handler1); net_icmpv6_register_handler(&test_handler2); - buf = net_buf_alloc(&bufs_pool, K_FOREVER); + pkt = net_pkt_get_reserve(&pkts_slab, 0, K_FOREVER); frag = net_buf_alloc(&data_pool, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, sizeof(TEST_MSG)), TEST_MSG, sizeof(TEST_MSG)); - ret = net_icmpv6_input(buf, 0, 0); + ret = net_icmpv6_input(pkt, 0, 0); if (!ret) { printk("%d: Callback not called properly\n", __LINE__); return false; } - ret = net_icmpv6_input(buf, NET_ICMPV6_ECHO_REPLY, 0); - if (ret < 0 || buf_status(buf) != 0) { + handler_status = -1; + + ret = net_icmpv6_input(pkt, NET_ICMPV6_ECHO_REPLY, 0); + if (ret < 0 || handler_status != 0) { printk("%d: Callback not called properly\n", __LINE__); return false; } - ret = net_icmpv6_input(buf, 1, 0); + ret = net_icmpv6_input(pkt, 1, 0); if (!ret) { printk("%d: Callback not called properly\n", __LINE__); return false; } - ret = net_icmpv6_input(buf, NET_ICMPV6_ECHO_REQUEST, 0); - if (ret < 0 || buf_status(buf) != 0) { + handler_status = -1; + + ret = net_icmpv6_input(pkt, NET_ICMPV6_ECHO_REQUEST, 0); + if (ret < 0 || handler_status != 0) { printk("%d: Callback not called properly\n", __LINE__); return false; } diff --git a/tests/net/ieee802154/fragment/src/main.c b/tests/net/ieee802154/fragment/src/main.c index cc02d10fff5..8f89ad5f51b 100644 --- a/tests/net/ieee802154/fragment/src/main.c +++ b/tests/net/ieee802154/fragment/src/main.c @@ -155,9 +155,9 @@ static void net_fragment_iface_init(struct net_if *iface) net_if_set_link_addr(iface, 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; } @@ -172,20 +172,20 @@ NET_DEVICE_INIT(net_fragment_test, "net_fragment_test", &net_fragment_if_api, DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2), 127); -static bool compare_data(struct net_buf *buf, struct net_fragment_data *data) +static bool compare_data(struct net_pkt *pkt, struct net_fragment_data *data) { struct net_buf *frag; uint8_t bytes, pos, compare, offset = 0; int remaining = data->len; - if (net_buf_frags_len(buf->frags) != (NET_IPV6UDPH_LEN + remaining)) { + if (net_pkt_get_len(pkt) != (NET_IPV6UDPH_LEN + remaining)) { printk("mismatch lengths, expected %d received %zd\n", NET_IPV6UDPH_LEN + remaining, - net_buf_frags_len(buf->frags)); + net_pkt_get_len(pkt)); return false; } - frag = buf->frags; + frag = pkt->frags; if (memcmp(frag->data, (uint8_t *)data, NET_IPV6UDPH_LEN)) { printk("mismatch headers\n"); @@ -214,25 +214,26 @@ static bool compare_data(struct net_buf *buf, struct net_fragment_data *data) return true; } -static struct net_buf *create_buf(struct net_fragment_data *data) +static struct net_pkt *create_pkt(struct net_fragment_data *data) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; uint8_t bytes, pos; uint16_t len; int remaining; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); - if (!buf) { + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); + if (!pkt) { return NULL; } - net_pkt_set_ll_reserve(buf, 0); - net_pkt_set_iface(buf, net_if_get_default()); - net_pkt_set_ip_hdr_len(buf, NET_IPV6H_LEN); + net_pkt_set_ll_reserve(pkt, 0); + net_pkt_set_iface(pkt, net_if_get_default()); + net_pkt_set_ip_hdr_len(pkt, NET_IPV6H_LEN); - frag = net_pkt_get_frag(buf, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); if (!frag) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } @@ -264,18 +265,18 @@ static struct net_buf *create_buf(struct net_fragment_data *data) remaining -= bytes; if (net_buf_tailroom(frag) - (bytes - copy)) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NULL; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); 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_fragment_data test_data_1 = { @@ -416,22 +417,23 @@ static struct net_fragment_data test_data_8 = { static int test_fragment(struct net_fragment_data *data) { - struct net_buf *rxbuf = NULL; + struct net_pkt *rxpkt = NULL; int result = TC_FAIL; - struct net_buf *buf, *frag, *dfrag; + struct net_pkt *pkt; + struct net_buf *frag, *dfrag; - buf = create_buf(data); - if (!buf) { + pkt = create_pkt(data); + if (!pkt) { TC_PRINT("%s: failed to create buffer\n", __func__); goto end; } #if DEBUG > 0 - printk("length before compression %zd\n", net_buf_frags_len(buf->frags)); - net_hexdump_frags("before-compression", buf); + printk("length before compression %zd\n", net_pkt_get_len(pkt)); + net_hexdump_frags("before-compression", pkt); #endif - if (!net_6lo_compress(buf, data->iphc, + if (!net_6lo_compress(pkt, data->iphc, ieee802154_fragment)) { TC_PRINT("compression failed\n"); goto end; @@ -439,21 +441,21 @@ static int test_fragment(struct net_fragment_data *data) #if DEBUG > 0 printk("length after compression and fragmentation %zd\n", - net_buf_frags_len(buf->frags)); - net_hexdump_frags("after-compression", buf); + net_pkt_get_len(pkt)); + net_hexdump_frags("after-compression", pkt); #endif - frag = buf->frags; + frag = pkt->frags; while (frag) { - rxbuf = net_pkt_get_reserve_rx(0, K_FOREVER); - if (!rxbuf) { + rxpkt = net_pkt_get_reserve_rx(0, K_FOREVER); + if (!rxpkt) { goto end; } - net_pkt_set_ll_reserve(rxbuf, 0); + net_pkt_set_ll_reserve(rxpkt, 0); - dfrag = net_pkt_get_frag(rxbuf, K_FOREVER); + dfrag = net_pkt_get_frag(rxpkt, K_FOREVER); if (!dfrag) { goto end; } @@ -461,16 +463,16 @@ static int test_fragment(struct net_fragment_data *data) memcpy(dfrag->data, frag->data, frag->len); dfrag->len = frag->len; - net_buf_frag_add(rxbuf, dfrag); + net_pkt_frag_add(rxpkt, dfrag); - switch (ieee802154_reassemble(rxbuf)) { + switch (ieee802154_reassemble(rxpkt)) { case NET_OK: frag = frag->frags; break; case NET_CONTINUE: goto compare; case NET_DROP: - net_pkt_unref(rxbuf); + net_pkt_unref(rxpkt); goto end; } } @@ -478,17 +480,17 @@ static int test_fragment(struct net_fragment_data *data) compare: #if DEBUG > 0 printk("length after reassembly and uncompression %zd\n", - net_buf_frags_len(rxbuf->frags)); - net_hexdump_frags("after-uncompression", rxbuf); + net_pkt_get_len(rxpkt)); + net_hexdump_frags("after-uncompression", rxpkt); #endif - if (compare_data(rxbuf, data)) { + if (compare_data(rxpkt, data)) { result = TC_PASS; } end: - net_pkt_unref(rxbuf); - net_pkt_unref(buf); + net_pkt_unref(rxpkt); + net_pkt_unref(pkt); return result; } diff --git a/tests/net/ieee802154/l2/src/ieee802154_fake_driver.c b/tests/net/ieee802154/l2/src/ieee802154_fake_driver.c index 05b9597008e..cc762f02243 100644 --- a/tests/net/ieee802154/l2/src/ieee802154_fake_driver.c +++ b/tests/net/ieee802154/l2/src/ieee802154_fake_driver.c @@ -16,7 +16,7 @@ /** FAKE ieee802.15.4 driver **/ #include -extern struct net_buf *current_buf; +extern struct net_pkt *current_pkt; extern struct k_sem driver_lock; static int fake_cca(struct device *dev) @@ -61,34 +61,34 @@ static int fake_set_txpower(struct device *dev, int16_t dbm) return 0; } -static inline void insert_frag_dummy_way(struct net_buf *buf) +static inline void insert_frag_dummy_way(struct net_pkt *pkt) { - if (current_buf->frags) { + if (current_pkt->frags) { struct net_buf *frag, *prev_frag = NULL; - frag = current_buf->frags; + frag = current_pkt->frags; while (frag) { prev_frag = frag; frag = frag->frags; } - prev_frag->frags = net_buf_ref(buf->frags); + prev_frag->frags = net_buf_ref(pkt->frags); } else { - current_buf->frags = net_buf_ref(buf->frags); + current_pkt->frags = net_buf_ref(pkt->frags); } } static int fake_tx(struct device *dev, - struct net_buf *buf, + struct net_pkt *pkt, struct net_buf *frag) { - NET_INFO("Sending buffer %p - length %zu\n", - buf, net_buf_frags_len(buf)); + NET_INFO("Sending packet %p - length %zu\n", + pkt, net_pkt_get_len(pkt)); - net_pkt_set_ll_reserve(current_buf, net_pkt_ll_reserve(buf)); + net_pkt_set_ll_reserve(current_pkt, net_pkt_ll_reserve(pkt)); - insert_frag_dummy_way(buf); + insert_frag_dummy_way(pkt); k_sem_give(&driver_lock); diff --git a/tests/net/ieee802154/l2/src/ieee802154_test.c b/tests/net/ieee802154/l2/src/ieee802154_test.c index 2f96da89b5f..1fed0bc8ae7 100644 --- a/tests/net/ieee802154/l2/src/ieee802154_test.c +++ b/tests/net/ieee802154/l2/src/ieee802154_test.c @@ -100,7 +100,7 @@ struct ieee802154_pkt_test test_sec_data_pkt = { (struct ieee802154_address_field *)(sec_data_pkt + 7), }; -struct net_buf *current_buf; +struct net_pkt *current_pkt; struct k_sem driver_lock; struct net_if *iface; @@ -177,23 +177,23 @@ static bool test_ns_sending(struct ieee802154_pkt_test *t) k_sem_take(&driver_lock, 10); - if (!current_buf->frags) { + if (!current_pkt->frags) { NET_ERR("*** Could not send IPv6 NS packet\n"); return false; } - pkt_hexdump(net_pkt_ll(current_buf), net_buf_frags_len(current_buf)); + pkt_hexdump(net_pkt_ll(current_pkt), net_pkt_get_len(current_pkt)); - if (!ieee802154_validate_frame(net_pkt_ll(current_buf), - net_buf_frags_len(current_buf), &mpdu)) { + if (!ieee802154_validate_frame(net_pkt_ll(current_pkt), + net_pkt_get_len(current_pkt), &mpdu)) { NET_ERR("*** Sent packet is not valid\n"); - net_pkt_unref(current_buf); + net_pkt_unref(current_pkt); return false; } - net_pkt_unref(current_buf->frags); - current_buf->frags = NULL; + net_pkt_frag_unref(current_pkt->frags); + current_pkt->frags = NULL; return true; } @@ -212,32 +212,33 @@ static bool test_ack_reply(struct ieee802154_pkt_test *t) 0xff, 0x16, 0xf0, 0x00, 0xff, 0x16, 0xf0, 0x00, 0xff, 0x16 }; struct ieee802154_mpdu mpdu; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; NET_INFO("- Sending ACK reply to a data packet\n"); - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - frag = net_pkt_get_frag(buf, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); memcpy(frag->data, data_pkt, sizeof(data_pkt)); frag->len = sizeof(data_pkt); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_recv_data(iface, buf); + net_recv_data(iface, pkt); k_sem_take(&driver_lock, 20); - /* an ACK packet should be in current_buf */ - if (!current_buf->frags) { + /* an ACK packet should be in current_pkt */ + if (!current_pkt->frags) { NET_ERR("*** No ACK reply sent\n"); return false; } - pkt_hexdump(net_pkt_ll(current_buf), net_buf_frags_len(current_buf)); + pkt_hexdump(net_pkt_ll(current_pkt), net_pkt_get_len(current_pkt)); - if (!ieee802154_validate_frame(net_pkt_ll(current_buf), - net_buf_frags_len(current_buf), &mpdu)) { + if (!ieee802154_validate_frame(net_pkt_ll(current_pkt), + net_pkt_get_len(current_pkt), &mpdu)) { NET_ERR("*** ACK Reply is invalid\n"); return false; } @@ -248,8 +249,8 @@ static bool test_ack_reply(struct ieee802154_pkt_test *t) return false; } - net_pkt_unref(current_buf->frags); - current_buf->frags = NULL; + net_pkt_frag_unref(current_pkt->frags); + current_pkt->frags = NULL; return true; } @@ -260,8 +261,8 @@ static bool initialize_test_environment(void) k_sem_init(&driver_lock, 0, UINT_MAX); - current_buf = net_pkt_get_reserve_rx(0, K_FOREVER); - if (!current_buf) { + current_pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + if (!current_pkt) { NET_ERR("*** No buffer to allocate\n"); return false; } diff --git a/tests/net/iface/src/main.c b/tests/net/iface/src/main.c index c099d13de7c..fef9d07ca34 100644 --- a/tests/net/iface/src/main.c +++ b/tests/net/iface/src/main.c @@ -99,9 +99,9 @@ static void net_iface_init(struct net_if *iface) NET_LINK_ETHERNET); } -static int sender_iface(struct net_if *iface, struct net_buf *buf) +static int sender_iface(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { DBG("No data to send!\n"); return -ENODATA; } @@ -112,9 +112,9 @@ static int sender_iface(struct net_if *iface, struct net_buf *buf) DBG("Sending at iface %d %p\n", net_if_get_by_iface(iface), iface); - if (net_pkt_iface(buf) != iface) { + if (net_pkt_iface(pkt) != iface) { DBG("Invalid interface %p, expecting %p\n", - net_pkt_iface(buf), iface); + net_pkt_iface(pkt), iface); test_failed = true; } @@ -125,7 +125,7 @@ static int sender_iface(struct net_if *iface, struct net_buf *buf) } } - net_pkt_unref(buf); + net_pkt_unref(pkt); k_sem_give(&wait_data); @@ -281,17 +281,17 @@ static void iface_setup(void) static bool send_iface(struct net_if *iface, int val, bool expect_fail) { static uint8_t data[] = { 't', 'e', 's', 't', '\0' }; - struct net_buf *buf; + struct net_pkt *pkt; int ret; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); - net_pkt_set_iface(buf, iface); + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); + net_pkt_set_iface(pkt, iface); - net_pkt_append(buf, sizeof(data), data, K_FOREVER); + net_pkt_append(pkt, sizeof(data), data, K_FOREVER); - ret = net_send_data(buf); + ret = net_send_data(pkt); if (!expect_fail && ret < 0) { - DBG("Cannot send test buffer (%d)\n", ret); + DBG("Cannot send test packet (%d)\n", ret); return false; } diff --git a/tests/net/ip-addr/src/main.c b/tests/net/ip-addr/src/main.c index 099c12cac57..614778f06fa 100644 --- a/tests/net/ip-addr/src/main.c +++ b/tests/net/ip-addr/src/main.c @@ -138,9 +138,9 @@ static void net_test_iface_init(struct net_if *iface) net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET); } -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 0; } diff --git a/tests/net/ipv6/src/main.c b/tests/net/ipv6/src/main.c index f324b36f34e..2d97c6dc49a 100644 --- a/tests/net/ipv6/src/main.c +++ b/tests/net/ipv6/src/main.c @@ -163,51 +163,52 @@ static void net_test_iface_init(struct net_if *iface) NET_LINK_ETHERNET); } -static struct net_buf *prepare_ra_message(void) +static struct net_pkt *prepare_ra_message(void) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_if *iface; iface = net_if_get_default(); - buf = net_pkt_get_reserve_rx(net_if_get_ll_reserve(iface, NULL), + pkt = net_pkt_get_reserve_rx(net_if_get_ll_reserve(iface, NULL), K_FOREVER); - NET_ASSERT_INFO(buf, "Out of RX buffers"); + NET_ASSERT_INFO(pkt, "Out of RX packets"); - 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_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_ll_clear(buf); + net_pkt_ll_clear(pkt); memcpy(net_buf_add(frag, sizeof(icmpv6_ra)), icmpv6_ra, sizeof(icmpv6_ra)); - return buf; + return pkt; } -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - struct net_icmp_hdr *icmp = NET_ICMP_BUF(buf); + struct net_icmp_hdr *icmp = NET_ICMP_BUF(pkt); - if (!buf->frags) { + if (!pkt->frags) { TC_ERROR("No data to send!\n"); return -ENODATA; } /* Reply with RA messge */ if (icmp->type == NET_ICMPV6_RS) { - net_pkt_unref(buf); - buf = prepare_ra_message(); + net_pkt_unref(pkt); + pkt = prepare_ra_message(); } /* Feed this data back to us */ - if (net_recv_data(iface, buf) < 0) { + if (net_recv_data(iface, pkt) < 0) { TC_ERROR("Data receive failed."); goto out; } @@ -215,7 +216,7 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) return 0; out: - net_pkt_unref(buf); + net_pkt_unref(pkt); test_failed = true; return 0; @@ -407,30 +408,31 @@ static bool net_test_nbr_lookup_ok(void) static bool net_test_send_ns_extra_options(void) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_if *iface; iface = net_if_get_default(); - 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); - NET_ASSERT_INFO(buf, "Out of TX buffers"); + NET_ASSERT_INFO(pkt, "Out of TX packets"); - 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_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_ll_clear(buf); + net_pkt_ll_clear(pkt); memcpy(net_buf_add(frag, sizeof(icmpv6_ns_invalid)), icmpv6_ns_invalid, sizeof(icmpv6_ns_invalid)); - if (net_recv_data(iface, buf) < 0) { + if (net_recv_data(iface, pkt) < 0) { TC_ERROR("Data receive for invalid NS failed."); return false; } @@ -440,30 +442,31 @@ static bool net_test_send_ns_extra_options(void) static bool net_test_send_ns_no_options(void) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_if *iface; iface = net_if_get_default(); - 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); - NET_ASSERT_INFO(buf, "Out of TX buffers"); + NET_ASSERT_INFO(pkt, "Out of TX packets"); - 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_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_ll_clear(buf); + net_pkt_ll_clear(pkt); memcpy(net_buf_add(frag, sizeof(icmpv6_ns_no_sllao)), icmpv6_ns_no_sllao, sizeof(icmpv6_ns_no_sllao)); - if (net_recv_data(iface, buf) < 0) { + if (net_recv_data(iface, pkt) < 0) { TC_ERROR("Data receive for invalid NS failed."); return false; } @@ -557,30 +560,31 @@ static bool net_test_ra_message(void) static bool net_test_hbho_message(void) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct net_if *iface; iface = net_if_get_default(); - 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); - NET_ASSERT_INFO(buf, "Out of TX buffers"); + NET_ASSERT_INFO(pkt, "Out of TX packets"); - 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_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); - net_pkt_ll_clear(buf); + net_pkt_ll_clear(pkt); memcpy(net_buf_add(frag, sizeof(ipv6_hbho)), ipv6_hbho, sizeof(ipv6_hbho)); - if (net_recv_data(iface, buf) < 0) { + if (net_recv_data(iface, pkt) < 0) { TC_ERROR("Data receive for HBHO failed."); return false; } @@ -593,7 +597,8 @@ static bool net_test_change_ll_addr(void) uint8_t new_mac[] = { 00, 01, 02, 03, 04, 05 }; struct net_linkaddr_storage *ll; struct net_linkaddr *ll_iface; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct in6_addr dst; struct net_if *iface; struct net_nbr *nbr; @@ -604,18 +609,18 @@ static bool net_test_change_ll_addr(void) iface = net_if_get_default(); - 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); - NET_ASSERT_INFO(buf, "Out of TX buffers"); + NET_ASSERT_INFO(pkt, "Out of TX packets"); - 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_iface(buf, iface); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); flags = NET_ICMPV6_NA_FLAG_ROUTER | NET_ICMPV6_NA_FLAG_OVERRIDE; diff --git a/tests/net/lib/dns_resolve/src/main.c b/tests/net/lib/dns_resolve/src/main.c index e2c0c6f839f..ef1053bb01d 100644 --- a/tests/net/lib/dns_resolve/src/main.c +++ b/tests/net/lib/dns_resolve/src/main.c @@ -115,9 +115,9 @@ static inline int get_slot_by_id(struct dns_resolve_context *ctx, return -1; } -static int sender_iface(struct net_if *iface, struct net_buf *buf) +static int sender_iface(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { DBG("No data to send!\n"); return -ENODATA; } @@ -130,9 +130,9 @@ static int sender_iface(struct net_if *iface, struct net_buf *buf) DBG("Sending at iface %d %p\n", net_if_get_by_iface(iface), iface); - if (net_pkt_iface(buf) != iface) { + if (net_pkt_iface(pkt) != iface) { DBG("Invalid interface %p, expecting %p\n", - net_pkt_iface(buf), iface); + net_pkt_iface(pkt), iface); test_failed = true; } @@ -170,7 +170,7 @@ static int sender_iface(struct net_if *iface, struct net_buf *buf) } out: - net_pkt_unref(buf); + net_pkt_unref(pkt); return 0; } diff --git a/tests/net/lib/zoap/prj.conf b/tests/net/lib/zoap/prj.conf index 7e89eb1e150..39085e54ad3 100644 --- a/tests/net/lib/zoap/prj.conf +++ b/tests/net/lib/zoap/prj.conf @@ -1,5 +1,7 @@ +CONFIG_NET_BUF_LOG=y +CONFIG_SYS_LOG_NET_BUF_LEVEL=2 CONFIG_NETWORKING=y CONFIG_NETWORKING_WITH_IPV6=y CONFIG_RANDOM_GENERATOR=y CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_ZOAP=y \ No newline at end of file +CONFIG_ZOAP=y diff --git a/tests/net/lib/zoap/src/main.c b/tests/net/lib/zoap/src/main.c index 997ed986060..cf3655514ee 100644 --- a/tests/net/lib/zoap/src/main.c +++ b/tests/net/lib/zoap/src/main.c @@ -29,7 +29,7 @@ #define NUM_OBSERVERS 3 #define NUM_REPLIES 3 -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); @@ -63,35 +63,36 @@ static struct sockaddr_in6 dummy_addr = { static int test_build_empty_pdu(void) { uint8_t result_pdu[] = { 0x40, 0x01, 0x0, 0x0 }; - struct zoap_packet pkt; - struct net_buf *buf, *frag; + struct zoap_packet zpkt; + struct net_pkt *pkt; + struct net_buf *frag; int result = TC_FAIL; int r; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - r = zoap_packet_init(&pkt, buf); + r = zoap_packet_init(&zpkt, pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; } - zoap_header_set_version(&pkt, 1); - zoap_header_set_type(&pkt, ZOAP_TYPE_CON); - zoap_header_set_code(&pkt, ZOAP_METHOD_GET); - zoap_header_set_id(&pkt, 0); + zoap_header_set_version(&zpkt, 1); + zoap_header_set_type(&zpkt, ZOAP_TYPE_CON); + zoap_header_set_code(&zpkt, ZOAP_METHOD_GET); + zoap_header_set_id(&zpkt, 0); if (frag->len != sizeof(result_pdu)) { TC_PRINT("Different size from the reference packet\n"); @@ -106,7 +107,7 @@ static int test_build_empty_pdu(void) result = TC_PASS; done: - net_buf_unref(buf); + net_pkt_unref(pkt); TC_END_RESULT(result); @@ -118,8 +119,9 @@ static int test_build_simple_pdu(void) uint8_t result_pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0xC1, 0x00, 0xFF, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; - struct zoap_packet pkt; - struct net_buf *buf, *frag; + struct zoap_packet zpkt; + struct net_pkt *pkt; + struct net_buf *frag; const char token[] = "token"; uint8_t *appdata, payload[] = "payload"; uint16_t buflen; @@ -127,47 +129,47 @@ static int test_build_simple_pdu(void) int result = TC_FAIL; int r; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - r = zoap_packet_init(&pkt, buf); + r = zoap_packet_init(&zpkt, pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; } - zoap_header_set_version(&pkt, 1); - zoap_header_set_type(&pkt, ZOAP_TYPE_NON_CON); - zoap_header_set_code(&pkt, + zoap_header_set_version(&zpkt, 1); + zoap_header_set_type(&zpkt, ZOAP_TYPE_NON_CON); + zoap_header_set_code(&zpkt, ZOAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); - zoap_header_set_id(&pkt, 0x1234); + zoap_header_set_id(&zpkt, 0x1234); - r = zoap_header_set_token(&pkt, (uint8_t *)token, strlen(token)); + r = zoap_header_set_token(&zpkt, (uint8_t *)token, strlen(token)); if (r) { TC_PRINT("Could not set token\n"); goto done; } - r = zoap_add_option(&pkt, ZOAP_OPTION_CONTENT_FORMAT, + r = zoap_add_option(&zpkt, ZOAP_OPTION_CONTENT_FORMAT, &format, sizeof(format)); if (r) { TC_PRINT("Could not add option\n"); goto done; } - appdata = zoap_packet_get_payload(&pkt, &buflen); - if (!buf || buflen <= sizeof(payload)) { + appdata = zoap_packet_get_payload(&zpkt, &buflen); + if (!appdata || buflen <= sizeof(payload)) { TC_PRINT("Not enough space to insert payload\n"); goto done; } @@ -186,7 +188,7 @@ static int test_build_simple_pdu(void) memcpy(appdata, payload, sizeof(payload)); - r = zoap_packet_set_used(&pkt, sizeof(payload)); + r = zoap_packet_set_used(&zpkt, sizeof(payload)); if (r) { TC_PRINT("Failed to set the amount of bytes used\n"); goto done; @@ -205,7 +207,7 @@ static int test_build_simple_pdu(void) result = TC_PASS; done: - net_buf_unref(buf); + net_pkt_unref(pkt); TC_END_RESULT(result); @@ -214,47 +216,48 @@ done: static int test_build_no_size_for_options(void) { - struct zoap_packet pkt; - struct net_buf *buf, *frag; + struct zoap_packet zpkt; + struct net_pkt *pkt; + struct net_buf *frag; const char token[] = "token"; uint8_t format = 0; int result = TC_FAIL; int r; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_limited_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - r = zoap_packet_init(&pkt, buf); + r = zoap_packet_init(&zpkt, pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; } - zoap_header_set_version(&pkt, 1); - zoap_header_set_type(&pkt, ZOAP_TYPE_NON_CON); - zoap_header_set_code(&pkt, + zoap_header_set_version(&zpkt, 1); + zoap_header_set_type(&zpkt, ZOAP_TYPE_NON_CON); + zoap_header_set_code(&zpkt, ZOAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); - zoap_header_set_id(&pkt, 0x1234); + zoap_header_set_id(&zpkt, 0x1234); - r = zoap_header_set_token(&pkt, (uint8_t *)token, strlen(token)); + r = zoap_header_set_token(&zpkt, (uint8_t *)token, strlen(token)); if (r) { TC_PRINT("Could not set token\n"); goto done; } /* There won't be enough space for the option value */ - r = zoap_add_option(&pkt, ZOAP_OPTION_CONTENT_FORMAT, + r = zoap_add_option(&zpkt, ZOAP_OPTION_CONTENT_FORMAT, &format, sizeof(format)); if (!r) { TC_PRINT("Shouldn't have added the option, not enough space\n"); @@ -264,7 +267,7 @@ static int test_build_no_size_for_options(void) result = TC_PASS; done: - net_buf_unref(buf); + net_pkt_unref(pkt); TC_END_RESULT(result); @@ -274,40 +277,41 @@ done: static int test_parse_empty_pdu(void) { uint8_t pdu[] = { 0x40, 0x01, 0, 0 }; - struct net_buf *buf, *frag; - struct zoap_packet pkt; + struct net_pkt *pkt; + struct net_buf *frag; + struct zoap_packet zpkt; uint8_t ver, type, code; uint16_t id; int result = TC_FAIL; int r; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(frag->data, pdu, sizeof(pdu)); frag->len = sizeof(pdu); - r = zoap_packet_parse(&pkt, buf); + r = zoap_packet_parse(&zpkt, pkt); if (r) { TC_PRINT("Could not parse packet\n"); goto done; } - ver = zoap_header_get_version(&pkt); - type = zoap_header_get_type(&pkt); - code = zoap_header_get_code(&pkt); - id = zoap_header_get_id(&pkt); + ver = zoap_header_get_version(&zpkt); + type = zoap_header_get_type(&zpkt); + code = zoap_header_get_code(&zpkt); + id = zoap_header_get_id(&zpkt); if (ver != 1) { TC_PRINT("Invalid version for parsed packet\n"); @@ -332,7 +336,7 @@ static int test_parse_empty_pdu(void) result = TC_PASS; done: - net_buf_unref(buf); + net_pkt_unref(pkt); TC_END_RESULT(result); @@ -344,8 +348,9 @@ static int test_parse_simple_pdu(void) uint8_t pdu[] = { 0x55, 0xA5, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0x00, 0xc1, 0x00, 0xff, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0x00 }; - struct zoap_packet pkt; - struct net_buf *buf, *frag; + struct zoap_packet zpkt; + struct net_pkt *pkt; + struct net_buf *frag; struct zoap_option options[16]; uint8_t ver, type, code, tkl; const uint8_t *token, *payload; @@ -353,33 +358,33 @@ static int test_parse_simple_pdu(void) int result = TC_FAIL; int r, count = 16; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(frag->data, pdu, sizeof(pdu)); frag->len = sizeof(pdu); - r = zoap_packet_parse(&pkt, buf); + r = zoap_packet_parse(&zpkt, pkt); if (r) { TC_PRINT("Could not parse packet\n"); goto done; } - ver = zoap_header_get_version(&pkt); - type = zoap_header_get_type(&pkt); - code = zoap_header_get_code(&pkt); - id = zoap_header_get_id(&pkt); + ver = zoap_header_get_version(&zpkt); + type = zoap_header_get_type(&zpkt); + code = zoap_header_get_code(&zpkt); + id = zoap_header_get_id(&zpkt); if (ver != 1) { TC_PRINT("Invalid version for parsed packet\n"); @@ -401,7 +406,7 @@ static int test_parse_simple_pdu(void) goto done; } - token = zoap_header_get_token(&pkt, &tkl); + token = zoap_header_get_token(&zpkt, &tkl); if (!token) { TC_PRINT("Couldn't extract token from packet\n"); @@ -418,7 +423,7 @@ static int test_parse_simple_pdu(void) goto done; } - count = zoap_find_options(&pkt, ZOAP_OPTION_CONTENT_FORMAT, + count = zoap_find_options(&zpkt, ZOAP_OPTION_CONTENT_FORMAT, options, count); if (count != 1) { TC_PRINT("Unexpected number of options in the packet\n"); @@ -436,13 +441,13 @@ static int test_parse_simple_pdu(void) } /* Not existent */ - count = zoap_find_options(&pkt, ZOAP_OPTION_ETAG, options, count); + count = zoap_find_options(&zpkt, ZOAP_OPTION_ETAG, options, count); if (count) { TC_PRINT("There shouldn't be any ETAG option in the packet\n"); goto done; } - payload = zoap_packet_get_payload(&pkt, &len); + payload = zoap_packet_get_payload(&zpkt, &len); if (!payload || !len) { TC_PRINT("There should be a payload in the packet\n"); goto done; @@ -456,7 +461,7 @@ static int test_parse_simple_pdu(void) result = TC_PASS; done: - net_buf_unref(buf); + net_pkt_unref(pkt); TC_END_RESULT(result); @@ -465,28 +470,29 @@ done: static int test_retransmit_second_round(void) { - struct zoap_packet pkt, resp; + struct zoap_packet zpkt, resp; struct zoap_pending *pending, *resp_pending; - struct net_buf *buf, *frag, *resp_buf = NULL; + struct net_pkt *pkt, *resp_pkt = NULL; + struct net_buf *frag; int result = TC_FAIL; int r; uint16_t id; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - r = zoap_packet_init(&pkt, buf); + r = zoap_packet_init(&zpkt, pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; @@ -494,10 +500,10 @@ static int test_retransmit_second_round(void) id = zoap_next_id(); - zoap_header_set_version(&pkt, 1); - zoap_header_set_type(&pkt, ZOAP_TYPE_CON); - zoap_header_set_code(&pkt, ZOAP_METHOD_GET); - zoap_header_set_id(&pkt, id); + zoap_header_set_version(&zpkt, 1); + zoap_header_set_type(&zpkt, ZOAP_TYPE_CON); + zoap_header_set_code(&zpkt, ZOAP_METHOD_GET); + zoap_header_set_id(&zpkt, id); pending = zoap_pending_next_unused(pendings, NUM_PENDINGS); if (!pending) { @@ -505,7 +511,7 @@ static int test_retransmit_second_round(void) goto done; } - r = zoap_pending_init(pending, &pkt, (struct sockaddr *) &dummy_addr); + r = zoap_pending_init(pending, &zpkt, (struct sockaddr *) &dummy_addr); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; @@ -524,21 +530,21 @@ static int test_retransmit_second_round(void) goto done; } - resp_buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!resp_buf) { - TC_PRINT("Could not get buffer from pool\n"); + resp_pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!resp_pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(resp_buf, frag); + net_pkt_frag_add(resp_pkt, frag); - r = zoap_packet_init(&resp, resp_buf); + r = zoap_packet_init(&resp, resp_pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; @@ -565,9 +571,9 @@ static int test_retransmit_second_round(void) result = TC_PASS; done: - net_buf_unref(buf); - if (resp_buf) { - net_buf_unref(resp_buf); + net_pkt_unref(pkt); + if (resp_pkt) { + net_pkt_unref(resp_pkt); } TC_END_RESULT(result); @@ -612,7 +618,8 @@ static int server_resource_1_get(struct zoap_resource *resource, { struct zoap_packet response; struct zoap_observer *observer; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; char payload[] = "This is the payload"; const uint8_t *token; uint8_t tkl, *p; @@ -637,21 +644,21 @@ static int server_resource_1_get(struct zoap_resource *resource, zoap_register_observer(resource, observer); - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); return -ENOMEM; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); 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) { TC_PRINT("Unable to initialize packet.\n"); return -EINVAL; @@ -675,7 +682,7 @@ static int server_resource_1_get(struct zoap_resource *resource, return -EINVAL; } - resource->user_data = buf; + resource->user_data = pkt; return 0; } @@ -695,28 +702,29 @@ static int test_observer_server(void) 0x51, 's', 0x01, '2', /* path */ }; struct zoap_packet req; - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; int result = TC_FAIL; int r; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(frag->data, valid_request_pdu, sizeof(valid_request_pdu)); frag->len = sizeof(valid_request_pdu); - r = zoap_packet_parse(&req, buf); + r = zoap_packet_parse(&req, pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; @@ -737,27 +745,27 @@ static int test_observer_server(void) goto done; } - net_pkt_unref(buf); + net_pkt_unref(pkt); - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(frag->data, not_found_request_pdu, sizeof(not_found_request_pdu)); frag->len = sizeof(not_found_request_pdu); - r = zoap_packet_parse(&req, buf); + r = zoap_packet_parse(&req, pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; @@ -773,7 +781,7 @@ static int test_observer_server(void) result = TC_PASS; done: - net_buf_unref(buf); + net_pkt_unref(pkt); TC_END_RESULT(result); @@ -793,28 +801,29 @@ static int test_observer_client(void) { struct zoap_packet req, rsp; struct zoap_reply *reply; - struct net_buf *buf, *frag, *rsp_buf = NULL; + struct net_pkt *pkt, *rsp_pkt = NULL; + struct net_buf *frag; const char token[] = "rndtoken"; const char * const *p; int observe = 0; int result = TC_FAIL; int r; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - r = zoap_packet_init(&req, buf); + r = zoap_packet_init(&req, pkt); if (r < 0) { TC_PRINT("Unable to initialize request\n"); goto done; @@ -838,7 +847,7 @@ static int test_observer_client(void) for (p = server_resource_1_path; p && *p; p++) { r = zoap_add_option(&req, ZOAP_OPTION_URI_PATH, - *p, strlen(*p)); + *p, strlen(*p)); if (r < 0) { TC_PRINT("Unable to add option to request.\n"); goto done; @@ -855,7 +864,7 @@ static int test_observer_client(void) reply->reply = resource_reply_cb; /* Server side, not interesting for this test */ - r = zoap_packet_parse(&req, buf); + r = zoap_packet_parse(&req, pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; @@ -869,13 +878,13 @@ static int test_observer_client(void) } /* We cheat, and communicate using the resource's user_data */ - rsp_buf = server_resources[0].user_data; + rsp_pkt = server_resources[0].user_data; /* The uninteresting part ends here */ - /* 'rsp_buf' contains the response now */ + /* 'rsp_pkt' contains the response now */ - r = zoap_packet_parse(&rsp, rsp_buf); + r = zoap_packet_parse(&rsp, rsp_pkt); if (r) { TC_PRINT("Could not initialize packet\n"); goto done; @@ -892,9 +901,9 @@ static int test_observer_client(void) result = TC_PASS; done: - net_buf_unref(buf); - if (rsp_buf) { - net_buf_unref(buf); + net_pkt_unref(pkt); + if (rsp_pkt) { + net_pkt_unref(pkt); } TC_END_RESULT(result); @@ -906,28 +915,29 @@ static int test_block_size(void) { struct zoap_block_context req_ctx, rsp_ctx; struct zoap_packet req; - struct net_buf *frag, *buf = NULL; + struct net_pkt *pkt = NULL; + struct net_buf *frag; const char token[] = "rndtoken"; uint8_t *payload; uint16_t len; int result = TC_FAIL; int r; - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - r = zoap_packet_init(&req, buf); + r = zoap_packet_init(&req, pkt); if (r < 0) { TC_PRINT("Unable to initialize request\n"); goto done; @@ -979,27 +989,27 @@ static int test_block_size(void) goto done; } - /* Suppose that buf was sent */ - net_buf_unref(buf); + /* Suppose that pkt was sent */ + net_pkt_unref(pkt); /* Let's try the second packet */ zoap_next_block(&req_ctx); - buf = net_buf_alloc(&zoap_pkt_pool, K_NO_WAIT); - if (!buf) { - TC_PRINT("Could not get buffer from pool\n"); + pkt = net_pkt_get_reserve(&zoap_pkt_slab, 0, K_NO_WAIT); + if (!pkt) { + TC_PRINT("Could not get packet from pool\n"); goto done; } frag = net_buf_alloc(&zoap_data_pool, K_NO_WAIT); - if (!buf) { + if (!frag) { TC_PRINT("Could not get buffer from pool\n"); goto done; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - r = zoap_packet_init(&req, buf); + r = zoap_packet_init(&req, pkt); if (r < 0) { TC_PRINT("Unable to initialize request\n"); goto done; @@ -1043,7 +1053,7 @@ static int test_block_size(void) result = TC_PASS; done: - net_buf_unref(buf); + net_pkt_unref(pkt); TC_END_RESULT(result); diff --git a/tests/net/mgmt/src/mgmt.c b/tests/net/mgmt/src/mgmt.c index f50c39aacce..ef2008f3e8c 100644 --- a/tests/net/mgmt/src/mgmt.c +++ b/tests/net/mgmt/src/mgmt.c @@ -64,9 +64,9 @@ static void fake_iface_init(struct net_if *iface) net_if_set_link_addr(iface, mac, 8, NET_LINK_DUMMY); } -static int fake_iface_send(struct net_if *iface, struct net_buf *buf) +static int fake_iface_send(struct net_if *iface, struct net_pkt *pkt) { - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } diff --git a/tests/net/mld/src/main.c b/tests/net/mld/src/main.c index 34007a91b28..4ac781f5c00 100644 --- a/tests/net/mld/src/main.c +++ b/tests/net/mld/src/main.c @@ -91,11 +91,11 @@ static void net_test_iface_init(struct net_if *iface) NET_LINK_ETHERNET); } -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - struct net_icmp_hdr *icmp = NET_ICMP_BUF(buf); + struct net_icmp_hdr *icmp = NET_ICMP_BUF(pkt); - if (!buf->frags) { + if (!pkt->frags) { TC_ERROR("No data to send!\n"); return -ENODATA; } @@ -111,7 +111,7 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) k_sem_give(&wait_data); } - net_pkt_unref(buf); + net_pkt_unref(pkt); return 0; } @@ -286,65 +286,65 @@ static void verify_leave_group(void) static void send_query(struct net_if *iface) { - struct net_buf *buf; + struct net_pkt *pkt; struct in6_addr dst; uint16_t pos; /* Sent to all MLDv2-capable routers */ net_ipv6_addr_create(&dst, 0xff02, 0, 0, 0, 0, 0, 0, 0x0016); - 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); - buf = net_ipv6_create_raw(buf, + pkt = net_ipv6_create_raw(pkt, &peer_addr, &dst, iface, NET_IPV6_NEXTHDR_HBHO); - NET_IPV6_BUF(buf)->hop_limit = 1; /* RFC 3810 ch 7.4 */ + NET_IPV6_BUF(pkt)->hop_limit = 1; /* RFC 3810 ch 7.4 */ /* Add hop-by-hop option and router alert option, RFC 3810 ch 5. */ - net_pkt_append_u8(buf, IPPROTO_ICMPV6); - net_pkt_append_u8(buf, 0); /* length (0 means 8 bytes) */ + net_pkt_append_u8(pkt, IPPROTO_ICMPV6); + net_pkt_append_u8(pkt, 0); /* length (0 means 8 bytes) */ #define ROUTER_ALERT_LEN 8 /* IPv6 router alert option is described in RFC 2711. */ - net_pkt_append_be16(buf, 0x0502); /* RFC 2711 ch 2.1 */ - net_pkt_append_be16(buf, 0); /* pkt contains MLD msg */ + net_pkt_append_be16(pkt, 0x0502); /* RFC 2711 ch 2.1 */ + net_pkt_append_be16(pkt, 0); /* pkt contains MLD msg */ - net_pkt_append_u8(buf, 1); /* padn */ - net_pkt_append_u8(buf, 0); /* padn len */ + net_pkt_append_u8(pkt, 1); /* padn */ + net_pkt_append_u8(pkt, 0); /* padn len */ /* ICMPv6 header */ - net_pkt_append_u8(buf, NET_ICMPV6_MLD_QUERY); /* type */ - net_pkt_append_u8(buf, 0); /* code */ - net_pkt_append_be16(buf, 0); /* chksum */ + net_pkt_append_u8(pkt, NET_ICMPV6_MLD_QUERY); /* type */ + net_pkt_append_u8(pkt, 0); /* code */ + net_pkt_append_be16(pkt, 0); /* chksum */ - net_pkt_append_be16(buf, 3); /* maximum response code */ - net_pkt_append_be16(buf, 0); /* reserved field */ + net_pkt_append_be16(pkt, 3); /* maximum response code */ + net_pkt_append_be16(pkt, 0); /* reserved field */ - net_pkt_append(buf, sizeof(struct in6_addr), + net_pkt_append(pkt, sizeof(struct in6_addr), (const uint8_t *)net_ipv6_unspecified_address(), K_FOREVER); /* multicast address */ - net_pkt_append_be16(buf, 0); /* Resv, S, QRV and QQIC */ - net_pkt_append_be16(buf, 0); /* number of addresses */ + net_pkt_append_be16(pkt, 0); /* Resv, S, QRV and QQIC */ + net_pkt_append_be16(pkt, 0); /* number of addresses */ - net_ipv6_finalize_raw(buf, NET_IPV6_NEXTHDR_HBHO); + net_ipv6_finalize_raw(pkt, NET_IPV6_NEXTHDR_HBHO); - net_pkt_set_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - net_pkt_write_be16(buf, buf->frags, + net_pkt_write_be16(pkt, pkt->frags, NET_IPV6H_LEN + ROUTER_ALERT_LEN + 2, - &pos, ntohs(~net_calc_chksum_icmpv6(buf))); + &pos, ntohs(~net_calc_chksum_icmpv6(pkt))); - net_recv_data(iface, buf); + net_recv_data(iface, pkt); } /* We are not really interested to parse the query at this point */ -static enum net_verdict handle_mld_query(struct net_buf *buf) +static enum net_verdict handle_mld_query(struct net_pkt *pkt) { is_query_received = true; diff --git a/tests/net/net_pkt/prj.conf b/tests/net/net_pkt/prj.conf index 317027eecec..d9409c929e8 100644 --- a/tests/net/net_pkt/prj.conf +++ b/tests/net/net_pkt/prj.conf @@ -1,8 +1,8 @@ CONFIG_NETWORKING=y CONFIG_NET_IPV6=y CONFIG_NET_BUF=y -#CONFIG_NET_BUF_LOG=y -#CONFIG_SYS_LOG_NET_BUF_LEVEL=4 +CONFIG_NET_BUF_LOG=y +CONFIG_SYS_LOG_NET_BUF_LEVEL=2 CONFIG_MAIN_STACK_SIZE=2048 CONFIG_NET_PKT_RX_COUNT=4 CONFIG_NET_PKT_TX_COUNT=4 @@ -12,6 +12,7 @@ CONFIG_NET_BUF_TX_COUNT=34 # it without fixing the tests. CONFIG_NET_BUF_DATA_SIZE=100 CONFIG_NET_LOG=y +CONFIG_SYS_LOG_NET_LEVEL=1 CONFIG_SYS_LOG_SHOW_COLOR=y CONFIG_NET_DEBUG_NET_PKT=y CONFIG_RANDOM_GENERATOR=y diff --git a/tests/net/net_pkt/src/main.c b/tests/net/net_pkt/src/main.c index 2624a3c8f24..179634eada8 100644 --- a/tests/net/net_pkt/src/main.c +++ b/tests/net/net_pkt/src/main.c @@ -21,6 +21,7 @@ #include "net_private.h" #define LL_RESERVE 28 +#define FRAG_COUNT 7 struct ipv6_hdr { uint8_t vtc; @@ -56,13 +57,14 @@ static const char example_data[] = static int test_ipv6_multi_frags(void) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; struct ipv6_hdr *ipv6; struct udp_hdr *udp; int bytes, remaining = strlen(example_data), pos = 0; /* Example of multi fragment scenario with IPv6 */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(LL_RESERVE, K_FOREVER); /* Place the IP + UDP header in the first fragment */ @@ -84,11 +86,11 @@ static int test_ipv6_multi_frags(void) return -EINVAL; } - net_pkt_set_appdata(buf, (void *)udp + sizeof(*udp)); - net_pkt_set_appdatalen(buf, 0); + net_pkt_set_appdata(pkt, (void *)udp + sizeof(*udp)); + net_pkt_set_appdatalen(pkt, 0); } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* Put some data to rest of the fragments */ frag = net_pkt_get_reserve_rx_data(LL_RESERVE, K_FOREVER); @@ -128,14 +130,14 @@ static int test_ipv6_multi_frags(void) return -EINVAL; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); if (remaining > 0) { frag = net_pkt_get_reserve_rx_data(LL_RESERVE, K_FOREVER); } } - bytes = net_buf_frags_len(buf->frags); + bytes = net_pkt_get_len(pkt); if (bytes != strlen(example_data)) { printk("Invalid number of bytes in message, %zd vs %d\n", strlen(example_data), bytes); @@ -143,17 +145,17 @@ static int test_ipv6_multi_frags(void) } /* Normally one should not unref the fragment list like this - * because it will leave the buf->frags pointing to already + * because it will leave the pkt->frags pointing to already * freed fragment. */ - net_pkt_unref(buf->frags); - if (!buf->frags) { + net_pkt_frag_unref(pkt->frags); + if (!pkt->frags) { printk("Fragment list should not be empty.\n"); return -EINVAL; } - buf->frags = NULL; /* to prevent double free */ + pkt->frags = NULL; /* to prevent double free */ - net_pkt_unref(buf); + net_pkt_unref(pkt); return 0; } @@ -161,31 +163,33 @@ static int test_ipv6_multi_frags(void) static char buf_orig[200]; static char buf_copy[200]; -static void linearize(struct net_buf *buf, char *buffer, int len) +static void linearize(struct net_pkt *pkt, char *buffer, int len) { + struct net_buf *frag; char *ptr = buffer; - buf = buf->frags; + frag = pkt->frags; - while (buf && len > 0) { + while (frag && len > 0) { - memcpy(ptr, buf->data, buf->len); - ptr += buf->len; - len -= buf->len; + memcpy(ptr, frag->data, frag->len); + ptr += frag->len; + len -= frag->len; - buf = buf->frags; + frag = frag->frags; } } static int test_fragment_copy(void) { - struct net_buf *buf, *frag, *new_buf, *new_frag; + struct net_pkt *pkt, *new_pkt; + struct net_buf *frag, *new_frag; struct ipv6_hdr *ipv6; struct udp_hdr *udp; size_t orig_len, reserve; int pos; - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(LL_RESERVE, K_FOREVER); /* Place the IP + UDP header in the first fragment */ @@ -211,56 +215,56 @@ static int test_fragment_copy(void) memcpy(net_buf_add(frag, 15), example_data, 15); - net_pkt_set_appdata(buf, (void *)udp + sizeof(*udp) + 15); - net_pkt_set_appdatalen(buf, 0); + net_pkt_set_appdata(pkt, (void *)udp + sizeof(*udp) + 15); + net_pkt_set_appdatalen(pkt, 0); } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - orig_len = net_buf_frags_len(buf); + orig_len = net_pkt_get_len(pkt); printk("Total copy data len %zd\n", orig_len); - linearize(buf, buf_orig, orig_len); + linearize(pkt, buf_orig, orig_len); /* Then copy a fragment list to a new fragment list. * Reserve some space in front of the buffers. */ reserve = sizeof(struct ipv6_hdr) + sizeof(struct icmp_hdr); - new_frag = net_pkt_copy_all(buf, reserve, K_FOREVER); + new_frag = net_pkt_copy_all(pkt, reserve, K_FOREVER); if (!new_frag) { printk("Cannot copy fragment list.\n"); return -EINVAL; } - new_buf = net_pkt_get_reserve_tx(0, K_FOREVER); - net_buf_frag_add(new_buf, new_frag); + new_pkt = net_pkt_get_reserve_tx(0, K_FOREVER); + new_pkt->frags = net_buf_frag_add(new_pkt->frags, new_frag); - printk("Total new data len %zd\n", net_buf_frags_len(new_buf)); + printk("Total new data len %zd\n", net_pkt_get_len(new_pkt)); - if ((net_buf_frags_len(buf) + reserve) != net_buf_frags_len(new_buf)) { + if ((net_pkt_get_len(pkt) + reserve) != net_pkt_get_len(new_pkt)) { int diff; - diff = net_buf_frags_len(new_buf) - reserve - - net_buf_frags_len(buf); + diff = net_pkt_get_len(new_pkt) - reserve - + net_pkt_get_len(pkt); printk("Fragment list missing data, %d bytes not copied " "(%zd vs %zd)\n", diff, - net_buf_frags_len(buf) + reserve, - net_buf_frags_len(new_buf)); + net_pkt_get_len(pkt) + reserve, + net_pkt_get_len(new_pkt)); return -EINVAL; } - if (net_buf_frags_len(new_buf) != (orig_len + sizeof(struct ipv6_hdr) + + if (net_pkt_get_len(new_pkt) != (orig_len + sizeof(struct ipv6_hdr) + sizeof(struct icmp_hdr))) { - printk("Fragment list missing data, new buf len %zd " - "should be %zd\n", net_buf_frags_len(new_buf), + printk("Fragment list missing data, new pkt len %zd " + "should be %zd\n", net_pkt_get_len(new_pkt), orig_len + sizeof(struct ipv6_hdr) + sizeof(struct icmp_hdr)); return -EINVAL; } - linearize(new_buf, buf_copy, sizeof(buf_copy)); + linearize(new_pkt, buf_copy, sizeof(buf_copy)); if (!memcmp(buf_orig, buf_copy, sizeof(buf_orig))) { printk("Buffer copy failed, buffers are same!\n"); @@ -314,126 +318,6 @@ static void hexdump(const char *str, const uint8_t *packet, size_t length) } #endif -static int test_fragment_pull(void) -{ -#define FRAG_COUNT 7 - struct net_buf *buf, *newbuf, *frags[FRAG_COUNT], *frag; - int i, bytes_before, bytes_after, amount = 10, bytes_before2; - - buf = net_pkt_get_reserve_tx(0, K_FOREVER); - frag = NULL; - - for (i = 0; i < FRAG_COUNT; i++) { - frags[i] = net_pkt_get_reserve_tx_data(12, K_FOREVER); - - if (frag) { - net_buf_frag_add(frag, frags[i]); - } - - frag = frags[i]; - - /* Copy character test data in front of the fragment */ - memcpy(net_buf_add(frags[i], sizeof(test_data)), - test_data, sizeof(test_data)); - } - - net_buf_frag_add(buf, frags[0]); - - bytes_before = net_buf_frags_len(buf); - - newbuf = net_pkt_pull(buf, amount / 2); - if (newbuf != buf) { - printk("First fragment is wrong\n"); - return -1; - } - - bytes_after = net_buf_frags_len(buf); - if (bytes_before != (bytes_after + amount / 2)) { - printk("Wrong amount of data in fragments, should be %d " - "but was %d\n", bytes_before - amount / 2, bytes_after); - return -1; - } - - newbuf = net_pkt_pull(buf, amount); - if (newbuf != buf) { - printk("First fragment is wrong\n"); - return -1; - } - - newbuf = net_pkt_pull(buf, amount * 100); - if (newbuf != buf) { - printk("First fragment is wrong\n"); - return -1; - } - - bytes_after = net_buf_frags_len(buf); - if (bytes_after != 0) { - printk("Fragment list should be empty (left %d bytes)\n", - bytes_after); - return -1; - } - - net_pkt_unref(buf); - - /* Trying without TX or RX buf as a first element */ - frags[0] = net_pkt_get_reserve_tx_data(12, K_FOREVER); - frag = frags[0]; - memcpy(net_buf_add(frags[0], sizeof(test_data)), - test_data, sizeof(test_data)); - - for (i = 1; i < FRAG_COUNT; i++) { - frags[i] = net_pkt_get_reserve_tx_data(12, K_FOREVER); - - if (frag) { - net_buf_frag_add(frag, frags[i]); - } - - frag = frags[i]; - - memcpy(net_buf_add(frags[i], sizeof(test_data)), - test_data, sizeof(test_data)); - } - - buf = frags[0]; - - bytes_before2 = net_buf_frags_len(buf); - - if (bytes_before != bytes_before2) { - printk("Invalid number of bytes in fragments (%d vs %d)\n", - bytes_before, bytes_before2); - return -1; - } - - bytes_before = net_buf_frags_len(buf); - - newbuf = net_pkt_pull(buf, amount / 2); - if (newbuf != buf) { - printk("First fragment is wrong\n"); - return -1; - } - - bytes_after = net_buf_frags_len(buf); - if (bytes_before != (bytes_after + amount / 2)) { - printk("Wrong amount of data in fragments2, should be %d " - "but was %d\n", bytes_before - amount / 2, bytes_after); - return -1; - } - - newbuf = net_pkt_pull(buf, amount); - if (newbuf == buf || newbuf != frags[1]) { - printk("First fragment2 is wrong\n"); - return -1; - } - - newbuf = net_pkt_pull(buf, amount * 100); - if (newbuf == buf || newbuf != NULL) { - printk("First fragment2 is not correct\n"); - return -1; - } - - return 0; -} - static const char sample_data[] = "abcdefghijklmnopqrstuvxyz" "abcdefghijklmnopqrstuvxyz" @@ -466,7 +350,7 @@ static int test_pkt_read_append(void) int remaining = strlen(sample_data); uint8_t verify_rw_short[sizeof(test_rw_short)]; uint8_t verify_rw_long[sizeof(test_rw_long)]; - struct net_buf *buf; + struct net_pkt *pkt; struct net_buf *frag; struct net_buf *tfrag; struct ipv6_hdr *ipv6; @@ -479,7 +363,7 @@ static int test_pkt_read_append(void) uint16_t fail_pos; /* Example of multi fragment read, append and skip APS's */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(LL_RESERVE, K_FOREVER); /* Place the IP + UDP header in the first fragment */ @@ -501,11 +385,11 @@ static int test_pkt_read_append(void) return -EINVAL; } - net_pkt_set_appdata(buf, (void *)udp + sizeof(*udp)); - net_pkt_set_appdatalen(buf, 0); + net_pkt_set_appdata(pkt, (void *)udp + sizeof(*udp)); + net_pkt_set_appdatalen(pkt, 0); } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* Put some data to rest of the fragments */ frag = net_pkt_get_reserve_rx_data(LL_RESERVE, K_FOREVER); @@ -545,14 +429,14 @@ static int test_pkt_read_append(void) return -EINVAL; } - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); if (remaining > 0) { frag = net_pkt_get_reserve_rx_data(LL_RESERVE, K_FOREVER); } } - bytes = net_buf_frags_len(buf->frags); + bytes = net_pkt_get_len(pkt); if (bytes != strlen(sample_data)) { printk("Invalid number of bytes in message, %zd vs %d\n", strlen(sample_data), bytes); @@ -561,15 +445,15 @@ static int test_pkt_read_append(void) /* Failure cases */ /* Invalid buffer */ - tfrag = net_pkt_skip(NULL, 10, &fail_pos, 10); + tfrag = net_frag_skip(NULL, 10, &fail_pos, 10); if (!(!tfrag && fail_pos == 0xffff)) { printk("Invalid case NULL buffer\n"); return -EINVAL; } /* Invalid: Skip more than a buffer length.*/ - tfrag = net_buf_frag_last(buf->frags); - tfrag = net_pkt_skip(tfrag, tfrag->len - 1, &fail_pos, tfrag->len + 2); + tfrag = net_buf_frag_last(pkt->frags); + tfrag = net_frag_skip(tfrag, tfrag->len - 1, &fail_pos, tfrag->len + 2); if (!(!tfrag && fail_pos == 0xffff)) { printk("Invalid case offset %d length to skip %d," "frag length %d\n", @@ -578,8 +462,8 @@ static int test_pkt_read_append(void) } /* Invalid offset */ - tfrag = net_buf_frag_last(buf->frags); - tfrag = net_pkt_skip(tfrag, tfrag->len + 10, &fail_pos, 10); + tfrag = net_buf_frag_last(pkt->frags); + tfrag = net_frag_skip(tfrag, tfrag->len + 10, &fail_pos, 10); if (!(!tfrag && fail_pos == 0xffff)) { printk("Invalid case offset %d length to skip %d," "frag length %d\n", @@ -591,10 +475,10 @@ static int test_pkt_read_append(void) /* Offset is more than single fragment length */ /* Get the first data fragment */ - tfrag = buf->frags; + tfrag = pkt->frags; tfrag = tfrag->frags; off = tfrag->len; - tfrag = net_pkt_read(tfrag, off + 10, &tpos, 10, data); + tfrag = net_frag_read(tfrag, off + 10, &tpos, 10, data); if (!tfrag || memcmp(sample_data + off + 10, data, 10)) { printk("Failed to read from offset %d, frag length %d" @@ -605,9 +489,9 @@ static int test_pkt_read_append(void) /* Skip till end of all fragments */ /* Get the first data fragment */ - tfrag = buf->frags; + tfrag = pkt->frags; tfrag = tfrag->frags; - tfrag = net_pkt_skip(tfrag, 0, &tpos, strlen(sample_data)); + tfrag = net_frag_skip(tfrag, 0, &tpos, strlen(sample_data)); if (!(!tfrag && tpos == 0)) { printk("Invalid skip till end of all fragments"); return -EINVAL; @@ -621,33 +505,33 @@ static int test_pkt_read_append(void) * 4) Skip first short data from cached frag or offset * 5) Read short data and compare */ - tfrag = net_buf_frag_last(buf->frags); + tfrag = net_buf_frag_last(pkt->frags); off = tfrag->len; - if (!net_pkt_append(buf, (uint16_t)sizeof(test_rw_short), + if (!net_pkt_append(pkt, (uint16_t)sizeof(test_rw_short), test_rw_short, K_FOREVER)) { printk("net_pkt_append failed\n"); return -EINVAL; } - if (!net_pkt_append(buf, (uint16_t)sizeof(test_rw_short), + if (!net_pkt_append(pkt, (uint16_t)sizeof(test_rw_short), test_rw_short, K_FOREVER)) { printk("net_pkt_append failed\n"); return -EINVAL; } - tfrag = net_pkt_skip(tfrag, off, &tpos, + tfrag = net_frag_skip(tfrag, off, &tpos, (uint16_t)sizeof(test_rw_short)); if (!tfrag) { - printk("net_pkt_skip failed\n"); + printk("net_frag_skip failed\n"); return -EINVAL; } - tfrag = net_pkt_read(tfrag, tpos, &tpos, + tfrag = net_frag_read(tfrag, tpos, &tpos, (uint16_t)sizeof(test_rw_short), verify_rw_short); if (memcmp(test_rw_short, verify_rw_short, sizeof(test_rw_short))) { - printk("net_pkt_read failed with mismatch data"); + printk("net_frag_read failed with mismatch data"); return -EINVAL; } @@ -659,46 +543,39 @@ static int test_pkt_read_append(void) * 4) Skip first long data from cached frag or offset * 5) Read long data and compare */ - tfrag = net_buf_frag_last(buf->frags); + tfrag = net_buf_frag_last(pkt->frags); off = tfrag->len; - if (!net_pkt_append(buf, (uint16_t)sizeof(test_rw_long), test_rw_long, + if (!net_pkt_append(pkt, (uint16_t)sizeof(test_rw_long), test_rw_long, K_FOREVER)) { printk("net_pkt_append failed\n"); return -EINVAL; } - if (!net_pkt_append(buf, (uint16_t)sizeof(test_rw_long), test_rw_long, + if (!net_pkt_append(pkt, (uint16_t)sizeof(test_rw_long), test_rw_long, K_FOREVER)) { printk("net_pkt_append failed\n"); return -EINVAL; } - /* Try to pass fragment to net_pkt_append(), this should fail - * as we always need to pass the first buf into it. - */ - if (net_pkt_append(buf->frags, (uint16_t)sizeof(test_rw_short), - test_rw_short, K_FOREVER)) { - printk("net_pkt_append succeed but should have failed\n"); - return -EINVAL; - } - - tfrag = net_pkt_skip(tfrag, off, &tpos, + tfrag = net_frag_skip(tfrag, off, &tpos, (uint16_t)sizeof(test_rw_long)); if (!tfrag) { - printk("net_pkt_skip failed\n"); + printk("net_frag_skip failed\n"); return -EINVAL; } - tfrag = net_pkt_read(tfrag, tpos, &tpos, + tfrag = net_frag_read(tfrag, tpos, &tpos, (uint16_t)sizeof(test_rw_long), verify_rw_long); if (memcmp(test_rw_long, verify_rw_long, sizeof(test_rw_long))) { - printk("net_pkt_read failed with mismatch data"); + printk("net_frag_read failed with mismatch data"); return -EINVAL; } - net_pkt_unref(buf); + net_pkt_unref(pkt); + + printk("test_pkt_read_append passed\n"); return 0; } @@ -707,7 +584,7 @@ static int test_pkt_read_write_insert(void) { struct net_buf *read_frag; struct net_buf *temp_frag; - struct net_buf *buf; + struct net_pkt *pkt; struct net_buf *frag; uint8_t read_data[100]; uint16_t read_pos; @@ -715,26 +592,26 @@ static int test_pkt_read_write_insert(void) uint16_t pos; /* Example of multi fragment read, append and skip APS's */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, LL_RESERVE); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + net_pkt_set_ll_reserve(pkt, LL_RESERVE); - frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* 1) Offset is with in input fragment. * Write app data after IPv6 and UDP header. (If the offset is after * IPv6 + UDP header size, api will create empty space till offset * and write data). */ - frag = net_pkt_write(buf, frag, NET_IPV6UDPH_LEN, &pos, 10, + frag = net_pkt_write(pkt, frag, NET_IPV6UDPH_LEN, &pos, 10, (uint8_t *)sample_data, K_FOREVER); if (!frag || pos != 58) { printk("Usecase 1: Write failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, NET_IPV6UDPH_LEN, &read_pos, 10, + read_frag = net_frag_read(frag, NET_IPV6UDPH_LEN, &read_pos, 10, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 1: Read failed\n"); @@ -750,14 +627,14 @@ static int test_pkt_read_write_insert(void) * already in Usecase 1, just need to fill the header, at this point * there shouldn't be any length change). */ - frag = net_pkt_write(buf, frag, 0, &pos, NET_IPV6UDPH_LEN, + frag = net_pkt_write(pkt, frag, 0, &pos, NET_IPV6UDPH_LEN, (uint8_t *)sample_data, K_FOREVER); if (!frag || pos != 48) { printk("Usecase 2: Write failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, 0, &read_pos, NET_IPV6UDPH_LEN, + read_frag = net_frag_read(frag, 0, &read_pos, NET_IPV6UDPH_LEN, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 2: Read failed\n"); @@ -770,22 +647,22 @@ static int test_pkt_read_write_insert(void) } /* Unref */ - net_pkt_unref(buf); + net_pkt_unref(pkt); - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, LL_RESERVE); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + net_pkt_set_ll_reserve(pkt, LL_RESERVE); /* 3) Offset is in next to next fragment. * Write app data after 2 fragments. (If the offset far away, api will * create empty fragments(space) till offset and write data). */ - frag = net_pkt_write(buf, buf->frags, 200, &pos, 10, + frag = net_pkt_write(pkt, pkt->frags, 200, &pos, 10, (uint8_t *)sample_data + 10, K_FOREVER); if (!frag) { printk("Usecase 3: Write failed"); } - read_frag = net_pkt_read(frag, pos - 10, &read_pos, 10, + read_frag = net_frag_read(frag, pos - 10, &read_pos, 10, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 3: Read failed\n"); @@ -802,14 +679,14 @@ static int test_pkt_read_write_insert(void) * Usecase 3, this scenatio doesn't create any space, it just overwrites * the existing data. */ - frag = net_pkt_write(buf, buf->frags, 190, &pos, 10, + frag = net_pkt_write(pkt, pkt->frags, 190, &pos, 10, (uint8_t *)sample_data, K_FOREVER); if (!frag) { printk("Usecase 4: Write failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, pos - 10, &read_pos, 20, + read_frag = net_frag_read(frag, pos - 10, &read_pos, 20, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 4: Read failed\n"); @@ -822,30 +699,30 @@ static int test_pkt_read_write_insert(void) } /* Unref */ - net_pkt_unref(buf); + net_pkt_unref(pkt); /* 5) Write 20 bytes in fragment which has only 10 bytes space. * API should overwrite on first 10 bytes and create extra 10 bytes * and write there. */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, LL_RESERVE); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + net_pkt_set_ll_reserve(pkt, LL_RESERVE); - frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); /* Create 10 bytes space. */ net_buf_add(frag, 10); - frag = net_pkt_write(buf, frag, 0, &pos, 20, (uint8_t *)sample_data, + frag = net_pkt_write(pkt, frag, 0, &pos, 20, (uint8_t *)sample_data, K_FOREVER); if (!frag && pos != 20) { printk("Usecase 5: Write failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, 0, &read_pos, 20, read_data); + read_frag = net_frag_read(frag, 0, &read_pos, 20, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 5: Read failed\n"); return -EINVAL; @@ -857,7 +734,7 @@ static int test_pkt_read_write_insert(void) } /* Unref */ - net_pkt_unref(buf); + net_pkt_unref(pkt); /* 6) First fragment is full, second fragment has 10 bytes tail room, * third fragment has 5 bytes. @@ -867,21 +744,21 @@ static int test_pkt_read_write_insert(void) * bytes and write data. Third fragment 5 bytes overwritten and space * for 5 bytes created. */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, LL_RESERVE); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + net_pkt_set_ll_reserve(pkt, LL_RESERVE); /* First fragment make it fully occupied. */ - frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = net_buf_tailroom(frag); net_buf_add(frag, len); /* 2nd fragment last 10 bytes tailroom, rest occupied */ - frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); len = net_buf_tailroom(frag); net_buf_add(frag, len - 10); @@ -890,19 +767,19 @@ static int test_pkt_read_write_insert(void) read_pos = frag->len - 10; /* 3rd fragment, only 5 bytes occupied */ - frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); net_buf_add(frag, 5); - temp_frag = net_pkt_write(buf, temp_frag, temp_frag->len - 10, &pos, + temp_frag = net_pkt_write(pkt, temp_frag, temp_frag->len - 10, &pos, 30, (uint8_t *) sample_data, K_FOREVER); if (!temp_frag) { printk("Use case 6: Write failed\n"); return -EINVAL; } - read_frag = net_pkt_read(read_frag, read_pos, &read_pos, 30, + read_frag = net_frag_read(read_frag, read_pos, &read_pos, 30, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 6: Read failed\n"); @@ -915,7 +792,7 @@ static int test_pkt_read_write_insert(void) } /* Unref */ - net_pkt_unref(buf); + net_pkt_unref(pkt); /* 7) Offset is with in input fragment. * Write app data after IPv6 and UDP header. (If the offset is after @@ -924,22 +801,22 @@ static int test_pkt_read_write_insert(void) * before first set of app data. */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, LL_RESERVE); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + net_pkt_set_ll_reserve(pkt, LL_RESERVE); /* First fragment make it fully occupied. */ - frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - frag = net_pkt_write(buf, frag, NET_IPV6UDPH_LEN, &pos, 10, + frag = net_pkt_write(pkt, frag, NET_IPV6UDPH_LEN, &pos, 10, (uint8_t *)sample_data + 10, K_FOREVER); if (!frag || pos != 58) { printk("Usecase 7: Write failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, NET_IPV6UDPH_LEN, &read_pos, 10, + read_frag = net_frag_read(frag, NET_IPV6UDPH_LEN, &read_pos, 10, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 7: Read failed\n"); @@ -951,13 +828,13 @@ static int test_pkt_read_write_insert(void) return -EINVAL; } - if (!net_pkt_insert(buf, frag, NET_IPV6UDPH_LEN, 10, + if (!net_pkt_insert(pkt, frag, NET_IPV6UDPH_LEN, 10, (uint8_t *)sample_data, K_FOREVER)) { printk("Usecase 7: Insert failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, NET_IPV6UDPH_LEN, &read_pos, 20, + read_frag = net_frag_read(frag, NET_IPV6UDPH_LEN, &read_pos, 20, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 7: Read after failed\n"); @@ -970,14 +847,14 @@ static int test_pkt_read_write_insert(void) } /* Insert data outside input fragment length, error case. */ - if (net_pkt_insert(buf, frag, 70, 10, (uint8_t *)sample_data, + if (net_pkt_insert(pkt, frag, 70, 10, (uint8_t *)sample_data, K_FOREVER)) { printk("Usecase 7: False insert failed\n"); return -EINVAL; } /* Unref */ - net_pkt_unref(buf); + net_pkt_unref(pkt); /* 8) Offset is with in input fragment. * Write app data after IPv6 and UDP header. (If the offset is after @@ -986,22 +863,22 @@ static int test_pkt_read_write_insert(void) * before first set of app data. Insertion data is long which will * take two fragments. */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, LL_RESERVE); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); + net_pkt_set_ll_reserve(pkt, LL_RESERVE); /* First fragment make it fully occupied. */ - frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(buf), + frag = net_pkt_get_reserve_rx_data(net_pkt_ll_reserve(pkt), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - frag = net_pkt_write(buf, frag, NET_IPV6UDPH_LEN, &pos, 10, + frag = net_pkt_write(pkt, frag, NET_IPV6UDPH_LEN, &pos, 10, (uint8_t *)sample_data + 60, K_FOREVER); if (!frag || pos != 58) { printk("Usecase 8: Write failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, NET_IPV6UDPH_LEN, &read_pos, 10, + read_frag = net_frag_read(frag, NET_IPV6UDPH_LEN, &read_pos, 10, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 8: Read failed\n"); @@ -1013,13 +890,13 @@ static int test_pkt_read_write_insert(void) return -EINVAL; } - if (!net_pkt_insert(buf, frag, NET_IPV6UDPH_LEN, 60, + if (!net_pkt_insert(pkt, frag, NET_IPV6UDPH_LEN, 60, (uint8_t *)sample_data, K_FOREVER)) { printk("Usecase 8: Insert failed\n"); return -EINVAL; } - read_frag = net_pkt_read(frag, NET_IPV6UDPH_LEN, &read_pos, 70, + read_frag = net_frag_read(frag, NET_IPV6UDPH_LEN, &read_pos, 70, read_data); if (!read_frag && read_pos == 0xffff) { printk("Usecase 8: Read after failed\n"); @@ -1032,29 +909,68 @@ static int test_pkt_read_write_insert(void) } /* Unref */ - net_pkt_unref(buf); + net_pkt_unref(pkt); + + printk("test_pkt_read_write_insert passed\n"); return 0; } -static int calc_fragments(struct net_buf *buf) +static int calc_fragments(struct net_pkt *pkt) { + struct net_buf *frag = pkt->frags; int count = 0; - while (buf) { - buf = buf->frags; + while (frag) { + frag = frag->frags; count++; } return count; } +static bool net_pkt_is_compact(struct net_pkt *pkt) +{ + struct net_buf *frag, *last; + size_t total = 0, calc; + int count = 0; + + last = NULL; + frag = pkt->frags; + + while (frag) { + total += frag->len; + count++; + + last = frag; + frag = frag->frags; + } + + NET_ASSERT(last); + + if (!last) { + return false; + } + + calc = count * (last->size) - net_buf_tailroom(last) - + count * (net_buf_headroom(last)); + + if (total == calc) { + return true; + } + + NET_DBG("Not compacted total %zu real %zu", total, calc); + + return false; +} + static int test_fragment_compact(void) { - struct net_buf *buf, *frags[FRAG_COUNT], *frag; + struct net_pkt *pkt; + struct net_buf *frags[FRAG_COUNT], *frag; int i, bytes, total, count; - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = NULL; for (i = 0, total = 0; i < FRAG_COUNT; i++) { @@ -1083,9 +999,11 @@ static int test_fragment_compact(void) return -1; } - net_buf_frag_add(buf, frags[0]); + printk("step 1\n"); - bytes = net_buf_frags_len(buf); + pkt->frags = net_buf_frag_add(pkt->frags, frags[0]); + + bytes = net_pkt_get_len(pkt); if (bytes != FRAG_COUNT * sizeof(test_data) * 2) { printk("Compact test failed, fragments had %d bytes but " "should have had %zd\n", bytes, @@ -1093,45 +1011,46 @@ static int test_fragment_compact(void) return -1; } - if (net_pkt_is_compact(buf->frags)) { - printk("The buf->frags is not compact. Test fails\n"); + if (net_pkt_is_compact(pkt)) { + printk("The pkt is definitely not compact. Test fails\n"); return -1; } - if (net_pkt_is_compact(buf)) { - printk("The buf is definitely not compact. Test fails\n"); + printk("step 2\n"); + + net_pkt_compact(pkt); + + if (!net_pkt_is_compact(pkt)) { + printk("The pkt should be in compact form. Test fails\n"); return -1; } - net_pkt_compact(buf); - - if (!net_pkt_is_compact(buf)) { - printk("The buf should be in compact form. Test fails\n"); - return -1; - } + printk("step 3\n"); /* Try compacting again, nothing should happen */ - net_pkt_compact(buf); + net_pkt_compact(pkt); - if (!net_pkt_is_compact(buf)) { - printk("The buf should be compacted now. Test fails\n"); + if (!net_pkt_is_compact(pkt)) { + printk("The pkt should be compacted now. Test fails\n"); return -1; } - total = calc_fragments(buf); + total = calc_fragments(pkt); /* Add empty fragment at the end and compact, the last fragment * should be removed. */ frag = net_pkt_get_reserve_rx_data(0, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - count = calc_fragments(buf); + count = calc_fragments(pkt); - net_pkt_compact(buf); + printk("step 4\n"); - i = calc_fragments(buf); + net_pkt_compact(pkt); + + i = calc_fragments(pkt); if (count != (i + 1)) { printk("Last fragment removal failed, chain should have %d " @@ -1150,17 +1069,19 @@ static int test_fragment_compact(void) */ frag = net_pkt_get_reserve_rx_data(0, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); frag = net_pkt_get_reserve_rx_data(0, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - count = calc_fragments(buf); + count = calc_fragments(pkt); - net_pkt_compact(buf); + printk("step 5\n"); - i = calc_fragments(buf); + net_pkt_compact(pkt); + + i = calc_fragments(pkt); if (count != (i + 2)) { printk("Last two fragment removal failed, chain should have " @@ -1179,17 +1100,19 @@ static int test_fragment_compact(void) */ frag = net_pkt_get_reserve_rx_data(0, K_FOREVER); - net_buf_frag_insert(buf, frag); + net_pkt_frag_insert(pkt, frag); frag = net_pkt_get_reserve_rx_data(0, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - count = calc_fragments(buf); + count = calc_fragments(pkt); - net_pkt_compact(buf); + printk("step 6\n"); - i = calc_fragments(buf); + net_pkt_compact(pkt); + + i = calc_fragments(pkt); if (count != (i + 2)) { printk("Two fragment removal failed, chain should have " @@ -1203,6 +1126,8 @@ static int test_fragment_compact(void) return -1; } + printk("test_fragment_compact passed\n"); + return 0; } @@ -1213,11 +1138,12 @@ static int test_fragment_split(void) #define TEST_FRAG_COUNT (FRAG_COUNT - 2) #define FRAGA (FRAG_COUNT - 2) #define FRAGB (FRAG_COUNT - 1) - struct net_buf *buf, *frags[FRAG_COUNT], *frag, *fragA, *fragB; + struct net_pkt *pkt; + struct net_buf *frags[FRAG_COUNT], *frag, *fragA, *fragB; int i, total, splitA, splitB; int ret; - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = NULL; for (i = 0, total = 0; i < TEST_FRAG_COUNT; i++) { @@ -1242,7 +1168,7 @@ static int test_fragment_split(void) return -1; } - net_buf_frag_add(buf, frags[0]); + net_pkt_frag_add(pkt, frags[0]); fragA = frags[FRAGA]; fragB = frags[FRAGB]; @@ -1257,13 +1183,13 @@ static int test_fragment_split(void) return -1; } - ret = net_pkt_split(buf, buf->frags, 1024, &fragA, &fragB, K_NO_WAIT); + ret = net_pkt_split(pkt, pkt->frags, 1024, &fragA, &fragB, K_NO_WAIT); if (!ret) { printk("Too long frag length %d\n", 1024); return -1; } - ret = net_pkt_split(buf, buf->frags, CONFIG_NET_BUF_DATA_SIZE + 1, + ret = net_pkt_split(pkt, pkt->frags, CONFIG_NET_BUF_DATA_SIZE + 1, &fragA, &fragB, K_NO_WAIT); if (!ret) { printk("Too long frag size %d vs %d\n", @@ -1272,7 +1198,7 @@ static int test_fragment_split(void) return -1; } - ret = net_pkt_split(buf, buf->frags, splitA, + ret = net_pkt_split(pkt, pkt->frags, splitA, &fragA, &fragB, K_NO_WAIT); if (ret < 0) { printk("Cannot split into %d and %d parts\n", splitA, splitB); @@ -1289,12 +1215,12 @@ static int test_fragment_split(void) return -1; } - if (memcmp(buf->frags->data, fragA->data, splitA)) { + if (memcmp(pkt->frags->data, fragA->data, splitA)) { printk("Frag A data mismatch\n"); return -1; } - if (memcmp(buf->frags->data + splitA, fragB->data, splitB)) { + if (memcmp(pkt->frags->data + splitA, fragB->data, splitB)) { printk("Frag B data mismatch\n"); return -1; } @@ -1312,10 +1238,6 @@ void main(void) goto fail; } - if (test_fragment_pull() < 0) { - goto fail; - } - if (test_pkt_read_append() < 0) { goto fail; } diff --git a/tests/net/route/src/main.c b/tests/net/route/src/main.c index 5f6ba9cdb4b..cf0b76c1128 100644 --- a/tests/net/route/src/main.c +++ b/tests/net/route/src/main.c @@ -120,9 +120,9 @@ static void net_route_iface_init(struct net_if *iface) NET_LINK_ETHERNET); } -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { TC_ERROR("No data to send!\n"); return -ENODATA; } @@ -134,9 +134,9 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) DBG("Received at iface %p and feeding it into iface %p\n", iface, recipient); - if (net_recv_data(recipient, buf) < 0) { + if (net_recv_data(recipient, pkt) < 0) { TC_ERROR("Data receive failed."); - net_pkt_unref(buf); + net_pkt_unref(pkt); test_failed = true; } @@ -145,9 +145,9 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) return 0; } - DBG("Buf %p to be sent len %lu\n", buf, net_buf_frags_len(buf)); + DBG("Pkt %p to be sent len %lu\n", pkt, net_pkt_get_len(pkt)); - net_pkt_unref(buf); + net_pkt_unref(pkt); if (data_failure) { test_failed = true; @@ -160,9 +160,9 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) return 0; } -static int tester_send_peer(struct net_if *iface, struct net_buf *buf) +static int tester_send_peer(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { TC_ERROR("No data to send!\n"); return -ENODATA; } @@ -174,9 +174,9 @@ static int tester_send_peer(struct net_if *iface, struct net_buf *buf) DBG("Received at iface %p and feeding it into iface %p\n", iface, recipient); - if (net_recv_data(recipient, buf) < 0) { + if (net_recv_data(recipient, pkt) < 0) { TC_ERROR("Data receive failed."); - net_pkt_unref(buf); + net_pkt_unref(pkt); test_failed = true; } @@ -185,9 +185,9 @@ static int tester_send_peer(struct net_if *iface, struct net_buf *buf) return 0; } - DBG("Buf %p to be sent len %lu\n", buf, net_buf_frags_len(buf)); + DBG("Pkt %p to be sent len %lu\n", pkt, net_pkt_get_len(pkt)); - net_pkt_unref(buf); + net_pkt_unref(pkt); if (data_failure) { test_failed = true; @@ -313,43 +313,6 @@ static bool net_ctx_create(void) return true; } -static inline uint8_t get_llao_len(struct net_if *iface) -{ - if (iface->link_addr.len == 6) { - return 8; - } else if (iface->link_addr.len == 8) { - return 16; - } - - /* What else could it be? */ - NET_ASSERT_INFO(0, "Invalid link address length %d", - iface->link_addr.len); - - return 0; -} - -static inline void set_llao(struct net_linkaddr *lladdr, - uint8_t *llao, uint8_t llao_len, uint8_t type) -{ - llao[NET_ICMPV6_OPT_TYPE_OFFSET] = type; - llao[NET_ICMPV6_OPT_LEN_OFFSET] = llao_len >> 3; - - memcpy(&llao[NET_ICMPV6_OPT_DATA_OFFSET], lladdr->addr, lladdr->len); - - memset(&llao[NET_ICMPV6_OPT_DATA_OFFSET + lladdr->len], 0, - llao_len - lladdr->len - 2); -} - -static inline void setup_icmpv6_hdr(struct net_buf *buf, uint8_t type, - uint8_t code) -{ - net_buf_add_u8(buf, type); - net_buf_add_u8(buf, code); - - memset(net_buf_add(buf, NET_ICMPV6_UNUSED_LEN), 0, - NET_ICMPV6_UNUSED_LEN); -} - static bool net_test_send_ns(struct net_if *iface, struct in6_addr *addr) { diff --git a/tests/net/rpl/src/main.c b/tests/net/rpl/src/main.c index 59b5c2bf5e5..676d30abfd4 100644 --- a/tests/net/rpl/src/main.c +++ b/tests/net/rpl/src/main.c @@ -105,12 +105,12 @@ static void net_rpl_iface_init(struct net_if *iface) NET_LINK_ETHERNET); } -static void set_buf_ll_addr(struct device *dev, struct net_buf *buf) +static void set_pkt_ll_addr(struct device *dev, struct net_pkt *pkt) { struct net_rpl_test *rpl = dev->driver_data; - struct net_linkaddr *src = net_pkt_ll_src(buf); - struct net_linkaddr *dst = net_pkt_ll_dst(buf); + struct net_linkaddr *src = net_pkt_ll_src(pkt); + struct net_linkaddr *dst = net_pkt_ll_dst(pkt); dst->len = lladdr_src.len; dst->addr = lladdr_src.addr; @@ -119,24 +119,24 @@ static void set_buf_ll_addr(struct device *dev, struct net_buf *buf) src->addr = rpl->mac_addr; } -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { TC_ERROR("No data to send!\n"); return -ENODATA; } - set_buf_ll_addr(iface->dev, buf); + set_pkt_ll_addr(iface->dev, pkt); /* By default we assume that the test is ok */ data_failure = false; if (feed_data) { - net_pkt_ll_swap(buf); + net_pkt_ll_swap(pkt); - if (net_recv_data(iface, buf) < 0) { + if (net_recv_data(iface, pkt) < 0) { TC_ERROR("Data receive failed."); - net_pkt_unref(buf); + net_pkt_unref(pkt); test_failed = true; } @@ -145,15 +145,15 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) return 0; } - DBG("Buf %p to be sent len %lu\n", buf, net_buf_frags_len(buf)); + DBG("Pkt %p to be sent len %lu\n", pkt, net_pkt_get_len(pkt)); #if 0 - net_hexdump_frags("recv", buf); + net_hexdump_frags("recv", pkt); #endif - if (NET_ICMP_BUF(buf)->type != expected_icmpv6) { + if (NET_ICMP_BUF(pkt)->type != expected_icmpv6) { DBG("ICMPv6 type %d, expected %d\n", - NET_ICMP_BUF(buf)->type, expected_icmpv6); + NET_ICMP_BUF(pkt)->type, expected_icmpv6); data_failure = true; } @@ -161,17 +161,17 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) /* If we are not sending what is expected, then mark it as a failure */ if (msg_sending) { - if (msg_sending != NET_ICMP_BUF(buf)->code) { + if (msg_sending != NET_ICMP_BUF(pkt)->code) { DBG("Received code %d, expected %d\n", - NET_ICMP_BUF(buf)->code, msg_sending); + NET_ICMP_BUF(pkt)->code, msg_sending); data_failure = true; } else { /* Pass sent DIO message back to us */ if (msg_sending == NET_RPL_DODAG_INFO_OBJ) { - net_pkt_ll_swap(buf); + net_pkt_ll_swap(pkt); - if (!net_recv_data(iface, buf)) { + if (!net_recv_data(iface, pkt)) { /* We must not unref the msg, * as it should be unfreed by * the upper stack. @@ -182,7 +182,7 @@ static int tester_send(struct net_if *iface, struct net_buf *buf) } } - net_pkt_unref(buf); + net_pkt_unref(pkt); out: if (data_failure) { @@ -314,19 +314,20 @@ static bool test_rpl_mcast_addr(void) static bool test_dio_dummy_input(void) { - struct net_buf *buf, *frag; + struct net_pkt *pkt; + struct net_buf *frag; int ret; - buf = net_pkt_get_tx(udp_ctx, K_FOREVER); + pkt = net_pkt_get_tx(udp_ctx, K_FOREVER); frag = net_pkt_get_data(udp_ctx, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); msg_sending = NET_RPL_DODAG_INFO_OBJ; - set_buf_ll_addr(net_if_get_default()->dev, buf); + set_pkt_ll_addr(net_if_get_default()->dev, pkt); - ret = net_icmpv6_input(buf, NET_ICMPV6_RPL, msg_sending); + ret = net_icmpv6_input(pkt, NET_ICMPV6_RPL, msg_sending); if (!ret) { TC_ERROR("%d: Callback in %s not called properly\n", __LINE__, __func__); diff --git a/tests/net/tcp/src/main.c b/tests/net/tcp/src/main.c index 3639e6af8a1..424ebba9ad6 100644 --- a/tests/net/tcp/src/main.c +++ b/tests/net/tcp/src/main.c @@ -113,9 +113,9 @@ static void net_tcp_iface_init(struct net_if *iface) net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET); } -static void v6_send_syn_ack(struct net_if *iface, struct net_buf *req) +static void v6_send_syn_ack(struct net_if *iface, struct net_pkt *req) { - struct net_buf *rsp = NULL; + struct net_pkt *rsp = NULL; int ret; ret = net_tcp_prepare_segment(reply_v6_ctx->tcp, @@ -156,37 +156,37 @@ static void v6_send_syn_ack(struct net_if *iface, struct net_buf *req) static int send_status = -EINVAL; -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { DBG("No data to send!\n"); return -ENODATA; } - if (syn_v6_sent && net_pkt_family(buf) == AF_INET6) { + if (syn_v6_sent && net_pkt_family(pkt) == AF_INET6) { DBG("v6 SYN was sent successfully\n"); syn_v6_sent = false; - v6_send_syn_ack(iface, buf); + v6_send_syn_ack(iface, pkt); } else { DBG("Data was sent successfully\n"); } - net_pkt_unref(buf); + net_pkt_unref(pkt); send_status = 0; return 0; } -static int tester_send_peer(struct net_if *iface, struct net_buf *buf) +static int tester_send_peer(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { DBG("No data to send!\n"); return -ENODATA; } DBG("Peer data was sent successfully\n"); - net_pkt_unref(buf); + net_pkt_unref(pkt); return 0; } @@ -218,7 +218,7 @@ struct ud { static struct ud *returned_ud; static enum net_verdict test_ok(struct net_conn *conn, - struct net_buf *buf, + struct net_pkt *pkt, void *user_data) { struct ud *ud = (struct ud *)user_data; @@ -237,13 +237,13 @@ static enum net_verdict test_ok(struct net_conn *conn, returned_ud = user_data; - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } static enum net_verdict test_fail(struct net_conn *conn, - struct net_buf *buf, + struct net_pkt *pkt, void *user_data) { /* This function should never be called as there should not @@ -254,60 +254,60 @@ static enum net_verdict test_fail(struct net_conn *conn, return NET_DROP; } -static void setup_ipv6_tcp(struct net_buf *buf, +static void setup_ipv6_tcp(struct net_pkt *pkt, struct in6_addr *remote_addr, struct in6_addr *local_addr, uint16_t remote_port, uint16_t local_port) { - NET_IPV6_BUF(buf)->vtc = 0x60; - NET_IPV6_BUF(buf)->tcflow = 0; - NET_IPV6_BUF(buf)->flow = 0; - NET_IPV6_BUF(buf)->len[0] = 0; - NET_IPV6_BUF(buf)->len[1] = NET_TCPH_LEN; + NET_IPV6_BUF(pkt)->vtc = 0x60; + NET_IPV6_BUF(pkt)->tcflow = 0; + NET_IPV6_BUF(pkt)->flow = 0; + NET_IPV6_BUF(pkt)->len[0] = 0; + NET_IPV6_BUF(pkt)->len[1] = NET_TCPH_LEN; - NET_IPV6_BUF(buf)->nexthdr = IPPROTO_TCP; - NET_IPV6_BUF(buf)->hop_limit = 255; + NET_IPV6_BUF(pkt)->nexthdr = IPPROTO_TCP; + NET_IPV6_BUF(pkt)->hop_limit = 255; - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, remote_addr); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, local_addr); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, remote_addr); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, local_addr); - 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_TCP_BUF(buf)->src_port = htons(remote_port); - NET_TCP_BUF(buf)->dst_port = htons(local_port); + NET_TCP_BUF(pkt)->src_port = htons(remote_port); + NET_TCP_BUF(pkt)->dst_port = htons(local_port); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ext_len(pkt, 0); - net_buf_add(buf->frags, net_pkt_ip_hdr_len(buf) + + net_buf_add(pkt->frags, net_pkt_ip_hdr_len(pkt) + sizeof(struct net_tcp_hdr)); } -static void setup_ipv4_tcp(struct net_buf *buf, +static void setup_ipv4_tcp(struct net_pkt *pkt, struct in_addr *remote_addr, struct in_addr *local_addr, uint16_t remote_port, uint16_t local_port) { - NET_IPV4_BUF(buf)->vhl = 0x45; - NET_IPV4_BUF(buf)->tos = 0; - NET_IPV4_BUF(buf)->len[0] = 0; - NET_IPV4_BUF(buf)->len[1] = NET_TCPH_LEN + + NET_IPV4_BUF(pkt)->vhl = 0x45; + NET_IPV4_BUF(pkt)->tos = 0; + NET_IPV4_BUF(pkt)->len[0] = 0; + NET_IPV4_BUF(pkt)->len[1] = NET_TCPH_LEN + sizeof(struct net_ipv4_hdr); - NET_IPV4_BUF(buf)->proto = IPPROTO_TCP; + NET_IPV4_BUF(pkt)->proto = IPPROTO_TCP; - net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, remote_addr); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, local_addr); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, remote_addr); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, local_addr); - 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_TCP_BUF(buf)->src_port = htons(remote_port); - NET_TCP_BUF(buf)->dst_port = htons(local_port); + NET_TCP_BUF(pkt)->src_port = htons(remote_port); + NET_TCP_BUF(pkt)->dst_port = htons(local_port); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ext_len(pkt, 0); - net_buf_add(buf->frags, net_pkt_ip_hdr_len(buf) + + net_buf_add(pkt->frags, net_pkt_ip_hdr_len(pkt) + sizeof(struct net_tcp_hdr)); } @@ -321,25 +321,25 @@ static bool send_ipv6_tcp_msg(struct net_if *iface, struct ud *ud, bool expect_failure) { - struct net_buf *buf; + struct net_pkt *pkt; struct net_buf *frag; int ret; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, 0); + net_pkt_set_ll_reserve(pkt, 0); - 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_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - setup_ipv6_tcp(buf, src, dst, src_port, dst_port); + setup_ipv6_tcp(pkt, src, dst, src_port, dst_port); - ret = net_recv_data(iface, buf); + ret = net_recv_data(iface, pkt); if (ret < 0) { - printk("Cannot recv buf %p, ret %d\n", buf, ret); + printk("Cannot recv pkt %p, ret %d\n", pkt, ret); return false; } @@ -372,25 +372,25 @@ static bool send_ipv4_tcp_msg(struct net_if *iface, struct ud *ud, bool expect_failure) { - struct net_buf *buf; + struct net_pkt *pkt; struct net_buf *frag; int ret; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); - net_pkt_set_ll_reserve(buf, 0); + net_pkt_set_ll_reserve(pkt, 0); - 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_iface(buf, iface); + net_pkt_set_iface(pkt, iface); - setup_ipv4_tcp(buf, src, dst, src_port, dst_port); + setup_ipv4_tcp(pkt, src, dst, src_port, dst_port); - ret = net_recv_data(iface, buf); + ret = net_recv_data(iface, pkt); if (ret < 0) { - printk("Cannot recv buf %p, ret %d\n", buf, ret); + printk("Cannot recv pkt %p, ret %d\n", pkt, ret); return false; } @@ -677,39 +677,39 @@ static bool test_register(void) return true; } -static bool v6_check_port_and_address(char *test_str, struct net_buf *buf, +static bool v6_check_port_and_address(char *test_str, struct net_pkt *pkt, const struct in6_addr *expected_dst_addr, uint16_t expected_dst_port) { - if (!net_ipv6_addr_cmp(&NET_IPV6_BUF(buf)->src, + if (!net_ipv6_addr_cmp(&NET_IPV6_BUF(pkt)->src, &my_v6_addr.sin6_addr)) { printk("%s: IPv6 source address mismatch, should be %s ", test_str, net_sprint_ipv6_addr(&my_v6_addr.sin6_addr)); printk("was %s\n", - net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->src)); + net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->src)); return false; } - if (NET_TCP_BUF(buf)->src_port != my_v6_addr.sin6_port) { + if (NET_TCP_BUF(pkt)->src_port != my_v6_addr.sin6_port) { printk("%s: IPv6 source port mismatch, %d vs %d\n", - test_str, ntohs(NET_TCP_BUF(buf)->src_port), + test_str, ntohs(NET_TCP_BUF(pkt)->src_port), ntohs(my_v6_addr.sin6_port)); return false; } - if (!net_ipv6_addr_cmp(expected_dst_addr, &NET_IPV6_BUF(buf)->dst)) { + if (!net_ipv6_addr_cmp(expected_dst_addr, &NET_IPV6_BUF(pkt)->dst)) { printk("%s: IPv6 destination address mismatch, should be %s ", test_str, net_sprint_ipv6_addr(expected_dst_addr)); printk("was %s\n", - net_sprint_ipv6_addr(&NET_IPV6_BUF(buf)->dst)); + net_sprint_ipv6_addr(&NET_IPV6_BUF(pkt)->dst)); return false; } - if (NET_TCP_BUF(buf)->dst_port != htons(expected_dst_port)) { + if (NET_TCP_BUF(pkt)->dst_port != htons(expected_dst_port)) { printk("%s: IPv6 destination port mismatch, %d vs %d\n", - test_str, ntohs(NET_TCP_BUF(buf)->dst_port), + test_str, ntohs(NET_TCP_BUF(pkt)->dst_port), expected_dst_port); return false; } @@ -717,39 +717,39 @@ static bool v6_check_port_and_address(char *test_str, struct net_buf *buf, return true; } -static bool v4_check_port_and_address(char *test_str, struct net_buf *buf, +static bool v4_check_port_and_address(char *test_str, struct net_pkt *pkt, const struct in_addr *expected_dst_addr, uint16_t expected_dst_port) { - if (!net_ipv4_addr_cmp(&NET_IPV4_BUF(buf)->src, + if (!net_ipv4_addr_cmp(&NET_IPV4_BUF(pkt)->src, &my_v4_addr.sin_addr)) { printk("%s: IPv4 source address mismatch, should be %s ", test_str, net_sprint_ipv4_addr(&my_v4_addr.sin_addr)); printk("was %s\n", - net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src)); + net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->src)); return false; } - if (NET_TCP_BUF(buf)->src_port != my_v4_addr.sin_port) { + if (NET_TCP_BUF(pkt)->src_port != my_v4_addr.sin_port) { printk("%s: IPv4 source port mismatch, %d vs %d\n", - test_str, ntohs(NET_TCP_BUF(buf)->src_port), + test_str, ntohs(NET_TCP_BUF(pkt)->src_port), ntohs(my_v4_addr.sin_port)); return false; } - if (!net_ipv4_addr_cmp(expected_dst_addr, &NET_IPV4_BUF(buf)->dst)) { + if (!net_ipv4_addr_cmp(expected_dst_addr, &NET_IPV4_BUF(pkt)->dst)) { printk("%s: IPv4 destination address mismatch, should be %s ", test_str, net_sprint_ipv4_addr(expected_dst_addr)); printk("was %s\n", - net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst)); + net_sprint_ipv4_addr(&NET_IPV4_BUF(pkt)->dst)); return false; } - if (NET_TCP_BUF(buf)->dst_port != htons(expected_dst_port)) { + if (NET_TCP_BUF(pkt)->dst_port != htons(expected_dst_port)) { printk("%s: IPv4 destination port mismatch, %d vs %d\n", - test_str, ntohs(NET_TCP_BUF(buf)->dst_port), + test_str, ntohs(NET_TCP_BUF(pkt)->dst_port), expected_dst_port); return false; } @@ -761,29 +761,29 @@ static bool test_create_v6_reset_packet(void) { struct net_tcp *tcp = v6_ctx->tcp; uint8_t flags = NET_TCP_RST; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v6_addr, &buf); + (struct sockaddr *)&peer_v6_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv6", buf); + net_hexdump_frags("TCPv6", pkt); - if (!(NET_TCP_FLAGS(buf) & NET_TCP_RST)) { + if (!(NET_TCP_FLAGS(pkt) & NET_TCP_RST)) { printk("Reset flag not set\n"); return false; } - if (!v6_check_port_and_address("TCP reset", buf, + if (!v6_check_port_and_address("TCP reset", pkt, &peer_v6_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -792,29 +792,29 @@ static bool test_create_v4_reset_packet(void) { struct net_tcp *tcp = v4_ctx->tcp; uint8_t flags = NET_TCP_RST; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v4_addr, &buf); + (struct sockaddr *)&peer_v4_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv4", buf); + net_hexdump_frags("TCPv4", pkt); - if (!(NET_TCP_FLAGS(buf) & NET_TCP_RST)) { + if (!(NET_TCP_FLAGS(pkt) & NET_TCP_RST)) { printk("Reset flag not set\n"); return false; } - if (!v4_check_port_and_address("TCP reset", buf, + if (!v4_check_port_and_address("TCP reset", pkt, &peer_v4_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -823,29 +823,29 @@ static bool test_create_v6_syn_packet(void) { struct net_tcp *tcp = v6_ctx->tcp; uint8_t flags = NET_TCP_SYN; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v6_addr, &buf); + (struct sockaddr *)&peer_v6_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv6", buf); + net_hexdump_frags("TCPv6", pkt); - if (!(NET_TCP_FLAGS(buf) & NET_TCP_SYN)) { + if (!(NET_TCP_FLAGS(pkt) & NET_TCP_SYN)) { printk("SYN flag not set\n"); return false; } - if (!v6_check_port_and_address("TCP syn", buf, + if (!v6_check_port_and_address("TCP syn", pkt, &peer_v6_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -854,29 +854,29 @@ static bool test_create_v4_syn_packet(void) { struct net_tcp *tcp = v4_ctx->tcp; uint8_t flags = NET_TCP_SYN; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v4_addr, &buf); + (struct sockaddr *)&peer_v4_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv4", buf); + net_hexdump_frags("TCPv4", pkt); - if (!(NET_TCP_FLAGS(buf) & NET_TCP_SYN)) { + if (!(NET_TCP_FLAGS(pkt) & NET_TCP_SYN)) { printk("Reset flag not set\n"); return false; } - if (!v4_check_port_and_address("TCP syn", buf, + if (!v4_check_port_and_address("TCP syn", pkt, &peer_v4_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -885,30 +885,30 @@ static bool test_create_v6_synack_packet(void) { struct net_tcp *tcp = v6_ctx->tcp; uint8_t flags = NET_TCP_SYN | NET_TCP_ACK; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v6_addr, &buf); + (struct sockaddr *)&peer_v6_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv6", buf); + net_hexdump_frags("TCPv6", pkt); - if (!((NET_TCP_FLAGS(buf) & NET_TCP_SYN) && - (NET_TCP_FLAGS(buf) & NET_TCP_ACK))) { + if (!((NET_TCP_FLAGS(pkt) & NET_TCP_SYN) && + (NET_TCP_FLAGS(pkt) & NET_TCP_ACK))) { printk("SYN|ACK flag not set\n"); return false; } - if (!v6_check_port_and_address("TCP synack", buf, + if (!v6_check_port_and_address("TCP synack", pkt, &peer_v6_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -917,30 +917,30 @@ static bool test_create_v4_synack_packet(void) { struct net_tcp *tcp = v4_ctx->tcp; uint8_t flags = NET_TCP_SYN | NET_TCP_ACK; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v4_addr, &buf); + (struct sockaddr *)&peer_v4_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv4", buf); + net_hexdump_frags("TCPv4", pkt); - if (!((NET_TCP_FLAGS(buf) & NET_TCP_SYN) && - (NET_TCP_FLAGS(buf) & NET_TCP_ACK))) { + if (!((NET_TCP_FLAGS(pkt) & NET_TCP_SYN) && + (NET_TCP_FLAGS(pkt) & NET_TCP_ACK))) { printk("SYN|ACK flag not set\n"); return false; } - if (!v4_check_port_and_address("TCP synack", buf, + if (!v4_check_port_and_address("TCP synack", pkt, &peer_v4_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -949,29 +949,29 @@ static bool test_create_v6_fin_packet(void) { struct net_tcp *tcp = v6_ctx->tcp; uint8_t flags = NET_TCP_FIN; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v6_addr, &buf); + (struct sockaddr *)&peer_v6_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv6", buf); + net_hexdump_frags("TCPv6", pkt); - if (!(NET_TCP_FLAGS(buf) & NET_TCP_FIN)) { + if (!(NET_TCP_FLAGS(pkt) & NET_TCP_FIN)) { printk("FIN flag not set\n"); return false; } - if (!v6_check_port_and_address("TCP fin", buf, + if (!v6_check_port_and_address("TCP fin", pkt, &peer_v6_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -980,29 +980,29 @@ static bool test_create_v4_fin_packet(void) { struct net_tcp *tcp = v4_ctx->tcp; uint8_t flags = NET_TCP_FIN; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v4_addr, &buf); + (struct sockaddr *)&peer_v4_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv4", buf); + net_hexdump_frags("TCPv4", pkt); - if (!(NET_TCP_FLAGS(buf) & NET_TCP_FIN)) { + if (!(NET_TCP_FLAGS(pkt) & NET_TCP_FIN)) { printk("FIN flag not set\n"); return false; } - if (!v4_check_port_and_address("TCP fin", buf, + if (!v4_check_port_and_address("TCP fin", pkt, &peer_v4_inaddr, PEER_TCP_PORT)) { return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -1011,30 +1011,30 @@ static bool test_v6_seq_check(void) { struct net_tcp *tcp = v6_ctx->tcp; uint8_t flags = NET_TCP_SYN; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; uint32_t seq; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v6_addr, &buf); + (struct sockaddr *)&peer_v6_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv6", buf); + net_hexdump_frags("TCPv6", pkt); - seq = NET_TCP_BUF(buf)->seq[0] << 24 | - NET_TCP_BUF(buf)->seq[1] << 16 | - NET_TCP_BUF(buf)->seq[2] << 8 | - NET_TCP_BUF(buf)->seq[3]; + seq = NET_TCP_BUF(pkt)->seq[0] << 24 | + NET_TCP_BUF(pkt)->seq[1] << 16 | + NET_TCP_BUF(pkt)->seq[2] << 8 | + NET_TCP_BUF(pkt)->seq[3]; if (seq != (tcp->send_seq - 1)) { printk("Seq does not match (%u vs %u)\n", seq + 1, tcp->send_seq); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } @@ -1043,30 +1043,30 @@ static bool test_v4_seq_check(void) { struct net_tcp *tcp = v4_ctx->tcp; uint8_t flags = NET_TCP_SYN; - struct net_buf *buf = NULL; + struct net_pkt *pkt = NULL; uint32_t seq; int ret; ret = net_tcp_prepare_segment(tcp, flags, NULL, 0, NULL, - (struct sockaddr *)&peer_v4_addr, &buf); + (struct sockaddr *)&peer_v4_addr, &pkt); if (ret) { printk("Prepare segment failed (%d)\n", ret); return false; } - net_hexdump_frags("TCPv4", buf); + net_hexdump_frags("TCPv4", pkt); - seq = NET_TCP_BUF(buf)->seq[0] << 24 | - NET_TCP_BUF(buf)->seq[1] << 16 | - NET_TCP_BUF(buf)->seq[2] << 8 | - NET_TCP_BUF(buf)->seq[3]; + seq = NET_TCP_BUF(pkt)->seq[0] << 24 | + NET_TCP_BUF(pkt)->seq[1] << 16 | + NET_TCP_BUF(pkt)->seq[2] << 8 | + NET_TCP_BUF(pkt)->seq[3]; if (seq != (tcp->send_seq - 1)) { printk("Seq does not match (%u vs %u)\n", seq + 1, tcp->send_seq); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; } diff --git a/tests/net/udp/src/main.c b/tests/net/udp/src/main.c index 31379f33331..48755e92834 100644 --- a/tests/net/udp/src/main.c +++ b/tests/net/udp/src/main.c @@ -77,16 +77,16 @@ static void net_udp_iface_init(struct net_if *iface) static int send_status = -EINVAL; -static int tester_send(struct net_if *iface, struct net_buf *buf) +static int tester_send(struct net_if *iface, struct net_pkt *pkt) { - if (!buf->frags) { + if (!pkt->frags) { DBG("No data to send!\n"); return -ENODATA; } DBG("Data was sent successfully\n"); - net_pkt_unref(buf); + net_pkt_unref(pkt); send_status = 0; @@ -135,7 +135,7 @@ struct ud { static struct ud *returned_ud; static enum net_verdict test_ok(struct net_conn *conn, - struct net_buf *buf, + struct net_pkt *pkt, void *user_data) { struct ud *ud = (struct ud *)user_data; @@ -154,13 +154,13 @@ static enum net_verdict test_ok(struct net_conn *conn, returned_ud = user_data; - net_pkt_unref(buf); + net_pkt_unref(pkt); return NET_OK; } static enum net_verdict test_fail(struct net_conn *conn, - struct net_buf *buf, + struct net_pkt *pkt, void *user_data) { /* This function should never be called as there should not @@ -171,60 +171,60 @@ static enum net_verdict test_fail(struct net_conn *conn, return NET_DROP; } -static void setup_ipv6_udp(struct net_buf *buf, +static void setup_ipv6_udp(struct net_pkt *pkt, struct in6_addr *remote_addr, struct in6_addr *local_addr, uint16_t remote_port, uint16_t local_port) { - NET_IPV6_BUF(buf)->vtc = 0x60; - NET_IPV6_BUF(buf)->tcflow = 0; - NET_IPV6_BUF(buf)->flow = 0; - NET_IPV6_BUF(buf)->len[0] = 0; - NET_IPV6_BUF(buf)->len[1] = NET_UDPH_LEN; + NET_IPV6_BUF(pkt)->vtc = 0x60; + NET_IPV6_BUF(pkt)->tcflow = 0; + NET_IPV6_BUF(pkt)->flow = 0; + NET_IPV6_BUF(pkt)->len[0] = 0; + NET_IPV6_BUF(pkt)->len[1] = NET_UDPH_LEN; - NET_IPV6_BUF(buf)->nexthdr = IPPROTO_UDP; - NET_IPV6_BUF(buf)->hop_limit = 255; + NET_IPV6_BUF(pkt)->nexthdr = IPPROTO_UDP; + NET_IPV6_BUF(pkt)->hop_limit = 255; - net_ipaddr_copy(&NET_IPV6_BUF(buf)->src, remote_addr); - net_ipaddr_copy(&NET_IPV6_BUF(buf)->dst, local_addr); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->src, remote_addr); + net_ipaddr_copy(&NET_IPV6_BUF(pkt)->dst, local_addr); - 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_UDP_BUF(buf)->src_port = htons(remote_port); - NET_UDP_BUF(buf)->dst_port = htons(local_port); + NET_UDP_BUF(pkt)->src_port = htons(remote_port); + NET_UDP_BUF(pkt)->dst_port = htons(local_port); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ext_len(pkt, 0); - net_buf_add(buf->frags, net_pkt_ip_hdr_len(buf) + + net_buf_add(pkt->frags, net_pkt_ip_hdr_len(pkt) + sizeof(struct net_udp_hdr)); } -static void setup_ipv4_udp(struct net_buf *buf, +static void setup_ipv4_udp(struct net_pkt *pkt, struct in_addr *remote_addr, struct in_addr *local_addr, uint16_t remote_port, uint16_t local_port) { - NET_IPV4_BUF(buf)->vhl = 0x45; - NET_IPV4_BUF(buf)->tos = 0; - NET_IPV4_BUF(buf)->len[0] = 0; - NET_IPV4_BUF(buf)->len[1] = NET_UDPH_LEN + + NET_IPV4_BUF(pkt)->vhl = 0x45; + NET_IPV4_BUF(pkt)->tos = 0; + NET_IPV4_BUF(pkt)->len[0] = 0; + NET_IPV4_BUF(pkt)->len[1] = NET_UDPH_LEN + sizeof(struct net_ipv4_hdr); - NET_IPV4_BUF(buf)->proto = IPPROTO_UDP; + NET_IPV4_BUF(pkt)->proto = IPPROTO_UDP; - net_ipaddr_copy(&NET_IPV4_BUF(buf)->src, remote_addr); - net_ipaddr_copy(&NET_IPV4_BUF(buf)->dst, local_addr); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->src, remote_addr); + net_ipaddr_copy(&NET_IPV4_BUF(pkt)->dst, local_addr); - 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_UDP_BUF(buf)->src_port = htons(remote_port); - NET_UDP_BUF(buf)->dst_port = htons(local_port); + NET_UDP_BUF(pkt)->src_port = htons(remote_port); + NET_UDP_BUF(pkt)->dst_port = htons(local_port); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ext_len(pkt, 0); - net_buf_add(buf->frags, net_pkt_ip_hdr_len(buf) + + net_buf_add(pkt->frags, net_pkt_ip_hdr_len(pkt) + sizeof(struct net_udp_hdr)); } @@ -238,22 +238,22 @@ static bool send_ipv6_udp_msg(struct net_if *iface, struct ud *ud, bool expect_failure) { - struct net_buf *buf; + struct net_pkt *pkt; struct net_buf *frag; int ret; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); - frag = net_pkt_get_frag(buf, K_FOREVER); - net_buf_frag_add(buf, frag); + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); + net_pkt_frag_add(pkt, frag); - net_pkt_set_iface(buf, iface); - net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag)); - setup_ipv6_udp(buf, src, dst, src_port, dst_port); + setup_ipv6_udp(pkt, src, dst, src_port, dst_port); - ret = net_recv_data(iface, buf); + ret = net_recv_data(iface, pkt); if (ret < 0) { - printk("Cannot recv buf %p, ret %d\n", buf, ret); + printk("Cannot recv pkt %p, ret %d\n", pkt, ret); return false; } @@ -286,22 +286,22 @@ static bool send_ipv4_udp_msg(struct net_if *iface, struct ud *ud, bool expect_failure) { - struct net_buf *buf; + struct net_pkt *pkt; struct net_buf *frag; int ret; - buf = net_pkt_get_reserve_tx(0, K_FOREVER); - frag = net_pkt_get_frag(buf, K_FOREVER); - net_buf_frag_add(buf, frag); + pkt = net_pkt_get_reserve_tx(0, K_FOREVER); + frag = net_pkt_get_frag(pkt, K_FOREVER); + net_pkt_frag_add(pkt, frag); - net_pkt_set_iface(buf, iface); - net_pkt_set_ll_reserve(buf, net_buf_headroom(frag)); + net_pkt_set_iface(pkt, iface); + net_pkt_set_ll_reserve(pkt, net_buf_headroom(frag)); - setup_ipv4_udp(buf, src, dst, src_port, dst_port); + setup_ipv4_udp(pkt, src, dst, src_port, dst_port); - ret = net_recv_data(iface, buf); + ret = net_recv_data(iface, pkt); if (ret < 0) { - printk("Cannot recv buf %p, ret %d\n", buf, ret); + printk("Cannot recv pkt %p, ret %d\n", pkt, ret); return false; } diff --git a/tests/net/utils/src/main.c b/tests/net/utils/src/main.c index 8958808c196..359bd5aaa7f 100644 --- a/tests/net/utils/src/main.c +++ b/tests/net/utils/src/main.c @@ -149,14 +149,15 @@ static const unsigned char pkt5[98] = { static bool run_tests(void) { - struct net_buf *frag, *buf; + struct net_pkt *pkt; + struct net_buf *frag; uint16_t chksum, orig_chksum; int hdr_len, i, chunk, datalen, total = 0; /* Packet fits to one fragment */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, sizeof(pkt1)), pkt1, sizeof(pkt1)); if (frag->len != sizeof(pkt1)) { @@ -165,94 +166,94 @@ static bool run_tests(void) return false; } - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ext_len(pkt, 0); /* We need to zero the ICMP checksum */ - hdr_len = net_pkt_ip_hdr_len(buf); + hdr_len = net_pkt_ip_hdr_len(pkt); orig_chksum = (frag->data[hdr_len + 2] << 8) + frag->data[hdr_len + 3]; frag->data[hdr_len + 2] = 0; frag->data[hdr_len + 3] = 0; - chksum = ntohs(~net_calc_chksum(buf, IPPROTO_ICMPV6)); + chksum = ntohs(~net_calc_chksum(pkt, IPPROTO_ICMPV6)); if (chksum != orig_chksum) { printk("Invalid chksum 0x%x in pkt1, should be 0x%x\n", chksum, orig_chksum); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); /* Then a case where there will be two fragments */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, sizeof(pkt2) / 2), pkt2, sizeof(pkt2) / 2); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ext_len(pkt, 0); - hdr_len = net_pkt_ip_hdr_len(buf); + hdr_len = net_pkt_ip_hdr_len(pkt); orig_chksum = (frag->data[hdr_len + 2] << 8) + frag->data[hdr_len + 3]; frag->data[hdr_len + 2] = 0; frag->data[hdr_len + 3] = 0; frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, sizeof(pkt2) - sizeof(pkt2) / 2), pkt2 + sizeof(pkt2) / 2, sizeof(pkt2) - sizeof(pkt2) / 2); - chksum = ntohs(~net_calc_chksum(buf, IPPROTO_ICMPV6)); + chksum = ntohs(~net_calc_chksum(pkt, IPPROTO_ICMPV6)); if (chksum != orig_chksum) { printk("Invalid chksum 0x%x in pkt2, should be 0x%x\n", chksum, orig_chksum); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); /* Then a case where there will be two fragments but odd data size */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, sizeof(pkt3) / 2), pkt3, sizeof(pkt3) / 2); printk("First fragment will have %zd bytes\n", sizeof(pkt3) / 2); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ext_len(pkt, 0); - hdr_len = net_pkt_ip_hdr_len(buf); + hdr_len = net_pkt_ip_hdr_len(pkt); orig_chksum = (frag->data[hdr_len + 2] << 8) + frag->data[hdr_len + 3]; frag->data[hdr_len + 2] = 0; frag->data[hdr_len + 3] = 0; frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, sizeof(pkt3) - sizeof(pkt3) / 2), pkt3 + sizeof(pkt3) / 2, sizeof(pkt3) - sizeof(pkt3) / 2); printk("Second fragment will have %zd bytes\n", sizeof(pkt3) - sizeof(pkt3) / 2); - chksum = ntohs(~net_calc_chksum(buf, IPPROTO_ICMPV6)); + chksum = ntohs(~net_calc_chksum(pkt, IPPROTO_ICMPV6)); if (chksum != orig_chksum) { printk("Invalid chksum 0x%x in pkt3, should be 0x%x\n", chksum, orig_chksum); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); /* Then a case where there will be several fragments */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, sizeof(struct net_ipv6_hdr)), pkt3, sizeof(struct net_ipv6_hdr)); printk("[0] IPv6 fragment will have %d bytes\n", frag->len); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv6_hdr)); - net_pkt_set_family(buf, AF_INET6); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr)); + net_pkt_set_family(pkt, AF_INET6); + net_pkt_set_ext_len(pkt, 0); chunk = 29; datalen = sizeof(pkt3) - sizeof(struct net_ipv6_hdr); @@ -260,7 +261,7 @@ static bool run_tests(void) for (i = 0; i < datalen/chunk; i++) { /* Next fragments will contain the data in odd sizes */ frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, chunk), pkt3 + sizeof(struct net_ipv6_hdr) + i * chunk, chunk); total += chunk; @@ -279,7 +280,7 @@ static bool run_tests(void) } if ((datalen - total) > 0) { frag = net_pkt_get_reserve_rx_data(10, K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); memcpy(net_buf_add(frag, datalen - total), pkt3 + sizeof(struct net_ipv6_hdr) + i * chunk, datalen - total); @@ -290,79 +291,79 @@ static bool run_tests(void) if ((total + sizeof(struct net_ipv6_hdr)) != sizeof(pkt3) || (total + sizeof(struct net_ipv6_hdr)) != - net_buf_frags_len(buf->frags)) { + net_pkt_get_len(pkt)) { printk("pkt3 size differs from fragment sizes, " "pkt3 size %zd frags size %zd calc total %zd\n", - sizeof(pkt3), net_buf_frags_len(buf->frags), + sizeof(pkt3), net_pkt_get_len(pkt), total + sizeof(struct net_ipv6_hdr)); return false; } - chksum = ntohs(~net_calc_chksum(buf, IPPROTO_ICMPV6)); + chksum = ntohs(~net_calc_chksum(pkt, IPPROTO_ICMPV6)); if (chksum != orig_chksum) { printk("Invalid chksum 0x%x in pkt3, should be 0x%x\n", chksum, orig_chksum); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); /* Another packet that fits to one fragment. * This one has ethernet header before IPv4 data. */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(sizeof(struct net_eth_hdr), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_pkt_set_ll_reserve(buf, sizeof(struct net_eth_hdr)); - memcpy(net_pkt_ll(buf), pkt4, sizeof(pkt4)); + net_pkt_set_ll_reserve(pkt, sizeof(struct net_eth_hdr)); + memcpy(net_pkt_ll(pkt), pkt4, sizeof(pkt4)); net_buf_add(frag, sizeof(pkt4) - sizeof(struct net_eth_hdr)); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); - net_pkt_set_family(buf, AF_INET); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr)); + net_pkt_set_family(pkt, AF_INET); + net_pkt_set_ext_len(pkt, 0); - hdr_len = net_pkt_ip_hdr_len(buf); + hdr_len = net_pkt_ip_hdr_len(pkt); orig_chksum = (frag->data[hdr_len + 2] << 8) + frag->data[hdr_len + 3]; frag->data[hdr_len + 2] = 0; frag->data[hdr_len + 3] = 0; - chksum = ntohs(~net_calc_chksum(buf, IPPROTO_ICMP)); + chksum = ntohs(~net_calc_chksum(pkt, IPPROTO_ICMP)); if (chksum != orig_chksum) { printk("Invalid chksum 0x%x in pkt4, should be 0x%x\n", chksum, orig_chksum); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); /* Another packet that fits to one fragment and which has correct * checksum. This one has ethernet header before IPv4 data. */ - buf = net_pkt_get_reserve_rx(0, K_FOREVER); + pkt = net_pkt_get_reserve_rx(0, K_FOREVER); frag = net_pkt_get_reserve_rx_data(sizeof(struct net_eth_hdr), K_FOREVER); - net_buf_frag_add(buf, frag); + net_pkt_frag_add(pkt, frag); - net_pkt_set_ll_reserve(buf, sizeof(struct net_eth_hdr)); - memcpy(net_pkt_ll(buf), pkt5, sizeof(pkt5)); + net_pkt_set_ll_reserve(pkt, sizeof(struct net_eth_hdr)); + memcpy(net_pkt_ll(pkt), pkt5, sizeof(pkt5)); net_buf_add(frag, sizeof(pkt5) - sizeof(struct net_eth_hdr)); - net_pkt_set_ip_hdr_len(buf, sizeof(struct net_ipv4_hdr)); - net_pkt_set_family(buf, AF_INET); - net_pkt_set_ext_len(buf, 0); + net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr)); + net_pkt_set_family(pkt, AF_INET); + net_pkt_set_ext_len(pkt, 0); - hdr_len = net_pkt_ip_hdr_len(buf); + hdr_len = net_pkt_ip_hdr_len(pkt); orig_chksum = (frag->data[hdr_len + 2] << 8) + frag->data[hdr_len + 3]; frag->data[hdr_len + 2] = 0; frag->data[hdr_len + 3] = 0; - chksum = ntohs(~net_calc_chksum(buf, IPPROTO_ICMP)); + chksum = ntohs(~net_calc_chksum(pkt, IPPROTO_ICMP)); if (chksum != orig_chksum) { printk("Invalid chksum 0x%x in pkt5, should be 0x%x\n", chksum, orig_chksum); return false; } - net_pkt_unref(buf); + net_pkt_unref(pkt); return true; }