netdev/upper: Add netpkt_to_iov() interface

Some driver like virtio-net can offload fragmented IOBs, so an interface
to support this feature is needed.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-05-17 17:30:40 +08:00 committed by Masayuki Ishikawa
parent d44e19d115
commit 318d136320
2 changed files with 73 additions and 1 deletions

View File

@ -1359,3 +1359,42 @@ bool netpkt_is_fragmented(FAR netpkt_t *pkt)
{
return pkt->io_flink != NULL;
}
/****************************************************************************
* Name: netpkt_to_iov
*
* Description:
* Write each piece of data/len into iov array.
*
* Input Parameters:
* dev - The lower half device driver structure
* pkt - The net packet
* iov - The iov array to write
* iovcnt - The number of elements in the iov array
*
* Returned Value:
* The actual written count of iov entries.
*
****************************************************************************/
int netpkt_to_iov(FAR struct netdev_lowerhalf_s *dev, FAR netpkt_t *pkt,
FAR struct iovec *iov, int iovcnt)
{
int i;
for (i = 0; pkt != NULL && i < iovcnt; pkt = pkt->io_flink, i++)
{
if (i == 0)
{
iov[i].iov_base = IOB_DATA(pkt) - NET_LL_HDRLEN(&dev->netdev);
iov[i].iov_len = pkt->io_len + NET_LL_HDRLEN(&dev->netdev);
}
else
{
iov[i].iov_base = IOB_DATA(pkt);
iov[i].iov_len = pkt->io_len;
}
}
return i;
}

View File

@ -47,7 +47,9 @@
* Pre-processor Definitions
****************************************************************************/
/* | <-------------- NETPKT_BUFLEN ---------------> |
/* Layout for net packet:
*
* | <-------------- NETPKT_BUFLEN ---------------> |
* +---------------------+-------------------+------+ +-------------+
* | reserved for driver | data | free | ---> | next netpkt |
* +---------------------+-------------------+------+ +-------------+
@ -55,6 +57,17 @@
* ^base ^data
*/
/* Layout for linked net packet, you can get list of (data, len) by
* netpkt_to_iov() interface:
*
* | <----------- datalen = sum(len) ------------> |
* +----------+-----------+ +-----------+ +-----------+------+
* | reserved | data | --> | data | --> | data | free |
* +----------+-----------+ +-----------+ +-----------+------+
* | | <- len -> | | <- len -> | | <- len -> |
* ^base ^data ^data ^data
*/
#define NETPKT_BUFLEN CONFIG_IOB_BUFSIZE
/****************************************************************************
@ -478,4 +491,24 @@ void netpkt_reset_reserved(FAR struct netdev_lowerhalf_s *dev,
bool netpkt_is_fragmented(FAR netpkt_t *pkt);
/****************************************************************************
* Name: netpkt_to_iov
*
* Description:
* Write each piece of data/len into iov array.
*
* Input Parameters:
* dev - The lower half device driver structure
* pkt - The net packet
* iov - The iov array to write
* iovcnt - The number of elements in the iov array
*
* Returned Value:
* The actual written count of iov entries.
*
****************************************************************************/
int netpkt_to_iov(FAR struct netdev_lowerhalf_s *dev, FAR netpkt_t *pkt,
FAR struct iovec *iov, int iovcnt);
#endif /* __INCLUDE_NUTTX_NET_NETDEV_LOWERHALF_H */