diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c index a1f3997010563..a97f3202b3c63 100644 --- a/drivers/net/netdev_upperhalf.c +++ b/drivers/net/netdev_upperhalf.c @@ -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; diff --git a/include/nuttx/net/netdev_lowerhalf.h b/include/nuttx/net/netdev_lowerhalf.h index 009d47f23e53e..bfe4089225cbd 100644 --- a/include/nuttx/net/netdev_lowerhalf.h +++ b/include/nuttx/net/netdev_lowerhalf.h @@ -69,6 +69,7 @@ */ #define NETPKT_BUFLEN CONFIG_IOB_BUFSIZE +#define NETPKT_BUFNUM CONFIG_IOB_NBUFFERS /**************************************************************************** * Public Types