2022-11-28 06:39:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* mm/iob/iob_update_pktlen.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/mm/iob.h>
|
|
|
|
|
|
|
|
#include "iob.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_update_pktlen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function will update packet length of the iob, it will be
|
2023-05-17 10:11:21 +02:00
|
|
|
* trimmed if the current length of the iob chain is greater than the
|
|
|
|
* new length, and will be grown if less than new length.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The new effective iob packet length, or a negated errno value on error.
|
2022-11-28 06:39:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-17 10:11:21 +02:00
|
|
|
int iob_update_pktlen(FAR struct iob_s *iob, unsigned int pktlen,
|
|
|
|
bool throttled)
|
2022-11-28 06:39:05 +01:00
|
|
|
{
|
|
|
|
FAR struct iob_s *penultimate;
|
|
|
|
FAR struct iob_s *next;
|
2024-04-01 08:09:10 +02:00
|
|
|
int remain = pktlen;
|
2022-11-28 06:39:05 +01:00
|
|
|
int ninqueue = 0;
|
2024-04-01 08:09:10 +02:00
|
|
|
int nrequire = 0;
|
2022-11-28 06:39:05 +01:00
|
|
|
uint16_t len;
|
|
|
|
|
|
|
|
/* The data offset must be less than CONFIG_IOB_BUFSIZE */
|
|
|
|
|
|
|
|
if (iob == NULL)
|
|
|
|
{
|
2023-05-17 10:11:21 +02:00
|
|
|
return -EINVAL;
|
2022-11-28 06:39:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the total entries of the data in the I/O buffer chain */
|
|
|
|
|
|
|
|
next = iob;
|
|
|
|
while (next != NULL)
|
|
|
|
{
|
|
|
|
ninqueue++;
|
2023-05-17 10:11:21 +02:00
|
|
|
penultimate = next;
|
2024-04-01 08:09:10 +02:00
|
|
|
if (remain > 0)
|
|
|
|
{
|
|
|
|
nrequire++;
|
|
|
|
remain -= IOB_BUFSIZE(next) - next->io_offset;
|
|
|
|
}
|
|
|
|
|
2023-05-17 10:11:21 +02:00
|
|
|
next = next->io_flink;
|
2022-11-28 06:39:05 +01:00
|
|
|
}
|
|
|
|
|
2024-04-01 08:09:10 +02:00
|
|
|
if (remain > 0)
|
|
|
|
{
|
|
|
|
nrequire += (remain + CONFIG_IOB_BUFSIZE - 1) / CONFIG_IOB_BUFSIZE;
|
|
|
|
}
|
2022-11-28 06:39:05 +01:00
|
|
|
|
|
|
|
if (nrequire == 0)
|
|
|
|
{
|
|
|
|
nrequire = 1;
|
|
|
|
}
|
|
|
|
|
2024-04-01 08:09:10 +02:00
|
|
|
/* Trim inqueue entries if needed */
|
|
|
|
|
2022-11-28 06:39:05 +01:00
|
|
|
if (nrequire < ninqueue)
|
|
|
|
{
|
|
|
|
/* Loop until complete the trim */
|
|
|
|
|
|
|
|
next = iob;
|
|
|
|
penultimate = NULL;
|
|
|
|
|
|
|
|
while (next != NULL)
|
|
|
|
{
|
|
|
|
if (nrequire-- <= 0)
|
|
|
|
{
|
|
|
|
if (penultimate != NULL)
|
|
|
|
{
|
|
|
|
penultimate->io_flink = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
iob_free_chain(next);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
penultimate = next;
|
|
|
|
next = next->io_flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-17 10:11:21 +02:00
|
|
|
else if (nrequire > ninqueue)
|
|
|
|
{
|
|
|
|
/* Start from the last IOB */
|
|
|
|
|
|
|
|
next = penultimate;
|
|
|
|
|
|
|
|
/* Loop to extend the link */
|
|
|
|
|
|
|
|
while (next != NULL && nrequire > ninqueue)
|
|
|
|
{
|
|
|
|
next->io_flink = iob_tryalloc(throttled);
|
|
|
|
next = next->io_flink;
|
|
|
|
ninqueue++;
|
|
|
|
}
|
|
|
|
}
|
2022-11-28 06:39:05 +01:00
|
|
|
|
|
|
|
iob->io_pktlen = pktlen;
|
|
|
|
|
|
|
|
/* Update size of each iob */
|
|
|
|
|
|
|
|
next = iob;
|
|
|
|
while (next != NULL && pktlen > 0)
|
|
|
|
{
|
2024-04-23 05:01:02 +02:00
|
|
|
if (pktlen + next->io_offset > IOB_BUFSIZE(next))
|
2022-11-28 06:39:05 +01:00
|
|
|
{
|
2024-04-23 05:01:02 +02:00
|
|
|
len = IOB_BUFSIZE(next) - next->io_offset;
|
2022-11-28 06:39:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = pktlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
next->io_len = len;
|
|
|
|
pktlen -= len;
|
|
|
|
next = next->io_flink;
|
|
|
|
}
|
2023-05-17 10:11:21 +02:00
|
|
|
|
|
|
|
/* Adjust final pktlen if it's not fully increased (e.g. alloc fail) */
|
|
|
|
|
|
|
|
iob->io_pktlen -= pktlen;
|
|
|
|
|
|
|
|
return iob->io_pktlen;
|
2022-11-28 06:39:05 +01:00
|
|
|
}
|