net: pkt: Add net_pkt_rx_clone() function

Add a function which allows to clone a packet, using RX packet poll to
create the clone.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2022-03-23 15:05:58 +01:00 committed by Carles Cufí
parent 1dd92ec865
commit 22f970b97f
2 changed files with 28 additions and 4 deletions

View file

@ -1887,7 +1887,8 @@ int net_pkt_copy(struct net_pkt *pkt_dst,
size_t length);
/**
* @brief Clone pkt and its buffer.
* @brief Clone pkt and its buffer. The cloned packet will be allocated on
* the same pool as the original one.
*
* @param pkt Original pkt to be cloned
* @param timeout Timeout to wait for free buffer
@ -1896,6 +1897,17 @@ int net_pkt_copy(struct net_pkt *pkt_dst,
*/
struct net_pkt *net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout);
/**
* @brief Clone pkt and its buffer. The cloned packet will be allocated on
* the RX packet poll.
*
* @param pkt Original pkt to be cloned
* @param timeout Timeout to wait for free buffer
*
* @return NULL if error, cloned packet otherwise.
*/
struct net_pkt *net_pkt_rx_clone(struct net_pkt *pkt, k_timeout_t timeout);
/**
* @brief Clone pkt and increase the refcount of its buffer.
*

View file

@ -1808,19 +1808,21 @@ static void clone_pkt_attributes(struct net_pkt *pkt, struct net_pkt *clone_pkt)
#endif
}
struct net_pkt *net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout)
static struct net_pkt *net_pkt_clone_internal(struct net_pkt *pkt,
struct k_mem_slab *slab,
k_timeout_t timeout)
{
size_t cursor_offset = net_pkt_get_current_offset(pkt);
struct net_pkt *clone_pkt;
struct net_pkt_cursor backup;
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG
clone_pkt = pkt_alloc_with_buffer(pkt->slab, net_pkt_iface(pkt),
clone_pkt = pkt_alloc_with_buffer(slab, net_pkt_iface(pkt),
net_pkt_get_len(pkt),
AF_UNSPEC, 0, timeout,
__func__, __LINE__);
#else
clone_pkt = pkt_alloc_with_buffer(pkt->slab, net_pkt_iface(pkt),
clone_pkt = pkt_alloc_with_buffer(slab, net_pkt_iface(pkt),
net_pkt_get_len(pkt),
AF_UNSPEC, 0, timeout);
#endif
@ -1864,6 +1866,16 @@ struct net_pkt *net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout)
return clone_pkt;
}
struct net_pkt *net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout)
{
return net_pkt_clone_internal(pkt, pkt->slab, timeout);
}
struct net_pkt *net_pkt_rx_clone(struct net_pkt *pkt, k_timeout_t timeout)
{
return net_pkt_clone_internal(pkt, &rx_pkts, timeout);
}
struct net_pkt *net_pkt_shallow_clone(struct net_pkt *pkt, k_timeout_t timeout)
{
struct net_pkt *clone_pkt;