net/netdev: Add netdev_iob_clone helper

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2024-03-27 18:52:54 +08:00 committed by Xiang Xiao
parent e87de19322
commit 2a342d2424
4 changed files with 56 additions and 22 deletions

View File

@ -1038,6 +1038,20 @@ void netdev_iob_clear(FAR struct net_driver_s *dev);
void netdev_iob_release(FAR struct net_driver_s *dev);
/****************************************************************************
* Name: netdev_iob_clone
*
* Description:
* Backup the current iob buffer for a given NIC by cloning it.
*
* Assumptions:
* The caller has locked the network.
*
****************************************************************************/
FAR struct iob_s *netdev_iob_clone(FAR struct net_driver_s *dev,
bool throttled);
/****************************************************************************
* Name: netdev_ipv6_add/del
*

View File

@ -364,22 +364,13 @@ static int ipv4_forward_callback(FAR struct net_driver_s *fwddev,
{
/* Backup the forward IP packet */
iob = iob_tryalloc(true);
iob = netdev_iob_clone(dev, true);
if (iob == NULL)
{
nerr("ERROR: iob alloc failed when forward broadcast\n");
nerr("ERROR: IOB clone failed when forwarding broadcast.\n");
return -ENOMEM;
}
iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE);
ret = iob_clone_partial(dev->d_iob, dev->d_iob->io_pktlen, 0,
iob, 0, true, false);
if (ret < 0)
{
iob_free_chain(iob);
return ret;
}
/* Recover the pointer to the IPv4 header in the receiving device's
* d_buf.
*/

View File

@ -491,22 +491,13 @@ static int ipv6_forward_callback(FAR struct net_driver_s *fwddev,
{
/* Backup the forward IP packet */
iob = iob_tryalloc(true);
iob = netdev_iob_clone(dev, true);
if (iob == NULL)
{
nerr("ERROR: iob alloc failed when forward broadcast\n");
nerr("ERROR: IOB clone failed when forwarding broadcast.\n");
return -ENOMEM;
}
iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE);
ret = iob_clone_partial(dev->d_iob, dev->d_iob->io_pktlen, 0,
iob, 0, true, false);
if (ret < 0)
{
iob_free_chain(iob);
return ret;
}
/* Recover the pointer to the IPv6 header in the receiving device's
* d_buf.
*/

View File

@ -153,3 +153,41 @@ void netdev_iob_release(FAR struct net_driver_s *dev)
dev->d_buf = NULL;
}
/****************************************************************************
* Name: netdev_iob_clone
*
* Description:
* Backup the current iob buffer for a given NIC by cloning it.
*
* Assumptions:
* The caller has locked the network.
*
****************************************************************************/
FAR struct iob_s *netdev_iob_clone(FAR struct net_driver_s *dev,
bool throttled)
{
FAR struct iob_s *iob;
int ret;
iob = iob_tryalloc(throttled);
if (iob == NULL)
{
nwarn("WARNING: IOB alloc failed for dev %s!\n", dev->d_ifname);
return NULL;
}
iob_reserve(iob, CONFIG_NET_LL_GUARDSIZE);
ret = iob_clone_partial(dev->d_iob, dev->d_iob->io_pktlen, 0,
iob, 0, throttled, false);
if (ret < 0)
{
iob_free_chain(iob);
nwarn("WARNING: IOB clone failed for dev %s, ret=%d!\n",
dev->d_ifname, ret);
return NULL;
}
return iob;
}