From 2a342d24240625fdf4005147525a90ba5cb847cf Mon Sep 17 00:00:00 2001 From: Zhe Weng Date: Wed, 27 Mar 2024 18:52:54 +0800 Subject: [PATCH] net/netdev: Add netdev_iob_clone helper Signed-off-by: Zhe Weng --- include/nuttx/net/netdev.h | 14 +++++++++++++ net/ipforward/ipv4_forward.c | 13 ++---------- net/ipforward/ipv6_forward.c | 13 ++---------- net/netdev/netdev_iob.c | 38 ++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/include/nuttx/net/netdev.h b/include/nuttx/net/netdev.h index 594e03409a..97cea719b7 100644 --- a/include/nuttx/net/netdev.h +++ b/include/nuttx/net/netdev.h @@ -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 * diff --git a/net/ipforward/ipv4_forward.c b/net/ipforward/ipv4_forward.c index ac608681ed..11ffb88613 100644 --- a/net/ipforward/ipv4_forward.c +++ b/net/ipforward/ipv4_forward.c @@ -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. */ diff --git a/net/ipforward/ipv6_forward.c b/net/ipforward/ipv6_forward.c index 48698a2e7e..769fce8138 100644 --- a/net/ipforward/ipv6_forward.c +++ b/net/ipforward/ipv6_forward.c @@ -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. */ diff --git a/net/netdev/netdev_iob.c b/net/netdev/netdev_iob.c index 1c4886b209..999f7776e3 100644 --- a/net/netdev/netdev_iob.c +++ b/net/netdev/netdev_iob.c @@ -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; +}