net/can: Add SO_RCVBUF option for can socket
If the CAN stack receiving packets fast, but the application layer reading packets slow. Then `conn->readahead` will continue to grow, leading to memory leaks. Finally CAN stack potentially starve out all IOB buffers. To prevent memory leaks, users can restrict can socket buffer length. Signed-off-by: gaohedong <gaohedong@xiaomi.com>
This commit is contained in:
parent
894d8a2c52
commit
dc651e090e
@ -456,6 +456,7 @@ void iob_free_queue_qentry(FAR struct iob_s *iob,
|
|||||||
|
|
||||||
#if CONFIG_IOB_NCHAINS > 0
|
#if CONFIG_IOB_NCHAINS > 0
|
||||||
unsigned int iob_get_queue_size(FAR struct iob_queue_s *queue);
|
unsigned int iob_get_queue_size(FAR struct iob_queue_s *queue);
|
||||||
|
unsigned int iob_get_queue_entry_count(FAR struct iob_queue_s *queue);
|
||||||
#endif /* CONFIG_IOB_NCHAINS > 0 */
|
#endif /* CONFIG_IOB_NCHAINS > 0 */
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
@ -44,7 +44,7 @@ if(CONFIG_MM_IOB)
|
|||||||
iob_navail.c
|
iob_navail.c
|
||||||
iob_free_queue_qentry.c
|
iob_free_queue_qentry.c
|
||||||
iob_tailroom.c
|
iob_tailroom.c
|
||||||
iob_get_queue_size.c
|
iob_get_queue_info.c
|
||||||
iob_reserve.c
|
iob_reserve.c
|
||||||
iob_update_pktlen.c
|
iob_update_pktlen.c
|
||||||
iob_count.c)
|
iob_count.c)
|
||||||
|
@ -28,7 +28,7 @@ CSRCS += iob_free_chain.c iob_free_qentry.c iob_free_queue.c
|
|||||||
CSRCS += iob_initialize.c iob_pack.c iob_peek_queue.c iob_remove_queue.c
|
CSRCS += iob_initialize.c iob_pack.c iob_peek_queue.c iob_remove_queue.c
|
||||||
CSRCS += iob_statistics.c iob_trimhead.c iob_trimhead_queue.c iob_trimtail.c
|
CSRCS += iob_statistics.c iob_trimhead.c iob_trimhead_queue.c iob_trimtail.c
|
||||||
CSRCS += iob_navail.c iob_free_queue_qentry.c iob_tailroom.c
|
CSRCS += iob_navail.c iob_free_queue_qentry.c iob_tailroom.c
|
||||||
CSRCS += iob_get_queue_size.c iob_reserve.c iob_update_pktlen.c
|
CSRCS += iob_get_queue_info.c iob_reserve.c iob_update_pktlen.c
|
||||||
CSRCS += iob_count.c
|
CSRCS += iob_count.c
|
||||||
|
|
||||||
ifeq ($(CONFIG_IOB_NOTIFIER),y)
|
ifeq ($(CONFIG_IOB_NOTIFIER),y)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* mm/iob/iob_get_queue_size.c
|
* mm/iob/iob_get_queue_info.c
|
||||||
*
|
*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
@ -55,4 +55,23 @@ unsigned int iob_get_queue_size(FAR struct iob_queue_s *queue)
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: iob_get_queue_entry_count
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Queue helper for get the iob queue entry count.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
unsigned int iob_get_queue_entry_count(FAR struct iob_queue_s *queue)
|
||||||
|
{
|
||||||
|
FAR struct iob_qentry_s *iobq;
|
||||||
|
unsigned int count;
|
||||||
|
|
||||||
|
for (iobq = queue->qh_head, count = 0; iobq != NULL;
|
||||||
|
iobq = iobq->qe_flink, count++);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* CONFIG_IOB_NCHAINS > 0 */
|
#endif /* CONFIG_IOB_NCHAINS > 0 */
|
@ -88,6 +88,10 @@ struct can_conn_s
|
|||||||
|
|
||||||
struct iob_queue_s readahead; /* remove Read-ahead buffering */
|
struct iob_queue_s readahead; /* remove Read-ahead buffering */
|
||||||
|
|
||||||
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
||||||
|
int32_t recv_buffnum; /* Recv buffer number */
|
||||||
|
#endif
|
||||||
|
|
||||||
/* CAN-specific content follows */
|
/* CAN-specific content follows */
|
||||||
|
|
||||||
int16_t crefs; /* Reference count */
|
int16_t crefs; /* Reference count */
|
||||||
|
@ -199,7 +199,19 @@ uint16_t can_datahandler(FAR struct net_driver_s *dev,
|
|||||||
FAR struct can_conn_s *conn)
|
FAR struct can_conn_s *conn)
|
||||||
{
|
{
|
||||||
FAR struct iob_s *iob = dev->d_iob;
|
FAR struct iob_s *iob = dev->d_iob;
|
||||||
int ret;
|
int ret = 0;
|
||||||
|
|
||||||
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
||||||
|
/* Check the frame count pending on conn->readahead */
|
||||||
|
|
||||||
|
if (iob_get_queue_entry_count(&conn->readahead) >= conn->recv_buffnum)
|
||||||
|
{
|
||||||
|
nwarn("WARNNING: There are no free recive buffer to retain the data. "
|
||||||
|
"Recive buffer number:%"PRId32", recived frames:%"PRIuPTR" \n",
|
||||||
|
conn->recv_buffnum, iob_get_queue_entry_count(&conn->readahead));
|
||||||
|
goto errout;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Concat the iob to readahead */
|
/* Concat the iob to readahead */
|
||||||
|
|
||||||
@ -222,10 +234,14 @@ uint16_t can_datahandler(FAR struct net_driver_s *dev,
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
||||||
netdev_iob_release(dev);
|
goto errout;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
errout:
|
||||||
|
netdev_iob_release(dev);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* CONFIG_NET && CONFIG_NET_CAN */
|
#endif /* CONFIG_NET && CONFIG_NET_CAN */
|
||||||
|
@ -234,6 +234,22 @@ int can_getsockopt(FAR struct socket *psock, int level, int option,
|
|||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
||||||
|
case SO_RCVBUF:
|
||||||
|
/* Verify that option is the size of an 'int'. Should also check
|
||||||
|
* that 'value' is properly aligned for an 'int'
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (*value_len != sizeof(int))
|
||||||
|
{
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
*(FAR int *)value = conn->recv_buffnum * CONFIG_IOB_BUFSIZE;
|
||||||
|
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
nerr("ERROR: Unrecognized RAW CAN socket option: %d\n", option);
|
nerr("ERROR: Unrecognized RAW CAN socket option: %d\n", option);
|
||||||
ret = -ENOPROTOOPT;
|
ret = -ENOPROTOOPT;
|
||||||
|
@ -210,6 +210,39 @@ int can_setsockopt(FAR struct socket *psock, int level, int option,
|
|||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
||||||
|
case SO_RCVBUF:
|
||||||
|
{
|
||||||
|
int buffersize;
|
||||||
|
|
||||||
|
/* Verify that option is the size of an 'int'. Should also check
|
||||||
|
* that 'value' is properly aligned for an 'int'
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (value_len != sizeof(int))
|
||||||
|
{
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the value. Is the option being set or cleared? */
|
||||||
|
|
||||||
|
buffersize = *(FAR int *)value;
|
||||||
|
if (buffersize < 0)
|
||||||
|
{
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if CONFIG_NET_MAX_RECV_BUFSIZE > 0
|
||||||
|
buffersize = MIN(buffersize, CONFIG_NET_MAX_RECV_BUFSIZE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
conn->recv_buffnum = (buffersize + CONFIG_IOB_BUFSIZE - 1)
|
||||||
|
/ CONFIG_IOB_BUFSIZE;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
nerr("ERROR: Unrecognized CAN option: %d\n", option);
|
nerr("ERROR: Unrecognized CAN option: %d\n", option);
|
||||||
ret = -ENOPROTOOPT;
|
ret = -ENOPROTOOPT;
|
||||||
|
@ -220,6 +220,16 @@ static int can_setup(FAR struct socket *psock)
|
|||||||
|
|
||||||
conn->crefs = 1;
|
conn->crefs = 1;
|
||||||
|
|
||||||
|
/* If Can Socket Stack recive can frame and pending on the readahead,
|
||||||
|
* but the application layer did not read the frame. This will cause
|
||||||
|
* a memory leak, and it is necessary to limit the readahead.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if CONFIG_NET_RECV_BUFSIZE > 0
|
||||||
|
conn->recv_buffnum = (CONFIG_NET_RECV_BUFSIZE + CONFIG_IOB_BUFSIZE - 1)
|
||||||
|
/ CONFIG_IOB_BUFSIZE;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Attach the connection instance to the socket */
|
/* Attach the connection instance to the socket */
|
||||||
|
|
||||||
psock->s_conn = conn;
|
psock->s_conn = conn;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user