Skip to content

Commit

Permalink
net/netdev: Check quota when registering lower-half devices
Browse files Browse the repository at this point in the history
Some drivers may set too big quota by accident and consume all of our
buffers, so we add a check when registering devices.

Signed-off-by: Zhe Weng <[email protected]>
  • Loading branch information
wengzhe authored and acassis committed May 14, 2024
1 parent 6c1b900 commit c234fac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
36 changes: 35 additions & 1 deletion drivers/net/netdev_upperhalf.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,40 @@ static int quota_fetch_dec(FAR struct netdev_lowerhalf_s *lower,
#endif
}

/****************************************************************************
* Name: quota_is_valid
*
* Description:
* Check if the quota of the lower half is not too big.
*
****************************************************************************/

static bool quota_is_valid(FAR struct netdev_lowerhalf_s *lower)
{
int total = 0;
int type;

for (type = 0; type < NETPKT_TYPENUM; type++)
{
total += netdev_lower_quota_load(lower, type);
}

if (total > NETPKT_BUFNUM)
{
nerr("ERROR: Too big quota when registering device: %d\n", total);
return false;
}

if (total > NETPKT_BUFNUM / 2)
{
nwarn("WARNING: The quota of the registering device may consume more "
"than half of the network buffers, which may hurt performance. "
"Please consider decreasing driver quota or increasing nIOB.\n");
}

return true;
}

/****************************************************************************
* Name: netpkt_get
*
Expand Down Expand Up @@ -1029,7 +1063,7 @@ int netdev_lower_register(FAR struct netdev_lowerhalf_s *dev,
FAR struct netdev_upperhalf_s *upper;
int ret;

if (dev == NULL || dev->ops == NULL ||
if (dev == NULL || quota_is_valid(dev) == false || dev->ops == NULL ||
dev->ops->transmit == NULL || dev->ops->receive == NULL)
{
return -EINVAL;
Expand Down
1 change: 1 addition & 0 deletions include/nuttx/net/netdev_lowerhalf.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
*/

#define NETPKT_BUFLEN CONFIG_IOB_BUFSIZE
#define NETPKT_BUFNUM CONFIG_IOB_NBUFFERS

/****************************************************************************
* Public Types
Expand Down

0 comments on commit c234fac

Please sign in to comment.