net/netdev: Check quota when registering lower-half devices

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 <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2024-05-13 17:18:58 +08:00 committed by Alan Carvalho de Assis
parent 6c1b900e82
commit c234fac910
2 changed files with 36 additions and 1 deletions

View File

@ -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
*
@ -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;

View File

@ -69,6 +69,7 @@
*/
#define NETPKT_BUFLEN CONFIG_IOB_BUFSIZE
#define NETPKT_BUFNUM CONFIG_IOB_NBUFFERS
/****************************************************************************
* Public Types