/** @file
Network library functions providing net buffer operation support.
Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/MemoryAllocationLib.h>
/**
Allocate and build up the sketch for a NET_BUF.
The net buffer allocated has the BlockOpNum's NET_BLOCK_OP, and its associated
NET_VECTOR has the BlockNum's NET_BLOCK. But all the NET_BLOCK_OP and
NET_BLOCK remain un-initialized.
@param[in] BlockNum The number of NET_BLOCK in the vector of net buffer
@param[in] BlockOpNum The number of NET_BLOCK_OP in the net buffer
@return Pointer to the allocated NET_BUF, or NULL if the
allocation failed due to resource limit.
**/
NET_BUF *
)
{
//
// Allocate three memory blocks.
//
return NULL;
}
if (BlockNum != 0) {
goto FreeNbuf;
}
}
return Nbuf;
return NULL;
}
/**
Allocate a single block NET_BUF. Upon allocation, all the
free space is in the tail room.
@param[in] Len The length of the block.
@return Pointer to the allocated NET_BUF, or NULL if the
allocation failed due to resource limit.
**/
NET_BUF *
)
{
return NULL;
}
goto FreeNBuf;
}
return Nbuf;
return NULL;
}
/**
Free the net vector.
Decrease the reference count of the net vector by one. The real resource free
operation isn't performed until the reference count of the net vector is
decreased to 0.
@param[in] Vector Pointer to the NET_VECTOR to be freed.
**/
)
{
return;
}
//
// Call external free function to free the vector if it
// isn't NULL. If NET_VECTOR_OWN_FIRST is set, release the
// first block since it is allocated by us
//
}
} else {
//
// Free each memory block associated with the Vector
//
}
}
}
/**
Free the net buffer and its associated NET_VECTOR.
Decrease the reference count of the net buffer by one. Free the associated net
vector and itself if the reference count of the net buffer is decreased to 0.
The net vector free operation just decrease the reference count of the net
vector by one and do the real resource free operation when the reference count
of the net vector is 0.
@param[in] Nbuf Pointer to the NET_BUF to be freed.
**/
)
{
//
// Update Vector only when NBuf is to be released. That is,
// all the sharing of Nbuf increse Vector's RefCnt by one
//
}
}
/**
Create a copy of the net buffer that shares the associated net vector.
The reference count of the newly created net buffer is set to 1. The reference
count of the associated net vector is increased by one.
@param[in] Nbuf Pointer to the net buffer to be cloned.
@return Pointer to the cloned net buffer, or NULL if the
allocation failed due to resource limit.
**/
NET_BUF *
)
{
return NULL;
}
return Clone;
}
/**
Create a duplicated copy of the net buffer with data copied and HeadSpace
bytes of head space reserved.
The duplicated net buffer will allocate its own memory to hold the data of the
source net buffer.
@param[in] Nbuf Pointer to the net buffer to be duplicated from.
@param[in, out] Duplicate Pointer to the net buffer to duplicate to, if
NULL a new net buffer is allocated.
@param[in] HeadSpace Length of the head space to reserve.
@return Pointer to the duplicated net buffer, or NULL if
the allocation failed due to resource limit.
**/
NET_BUF *
)
{
}
return NULL;
}
//
// Don't set the IP and TCP head point, since it is most
// like that they are pointing to the memory of Nbuf.
//
return Duplicate;
}
/**
Free a list of net buffers.
@param[in, out] Head Pointer to the head of linked net buffers.
**/
)
{
NetbufFree (Nbuf);
}
}
/**
Get the index of NET_BLOCK_OP that contains the byte at Offset in the net
buffer.
This can be used to, for example, retrieve the IP header in the packet. It
also can be used to get the fragment that contains the byte which is used
mainly by the library implementation itself.
@param[in] Nbuf Pointer to the net buffer.
@param[in] Offset The offset of the byte.
@param[out] Index Index of the NET_BLOCK_OP that contains the byte at
Offset.
@return Pointer to the Offset'th byte of data in the net buffer, or NULL
if there is no such data in the net buffer.
**/
UINT8 *
)
{
return NULL;
}
Len = 0;
continue;
}
}
}
return NULL;
}
/**
Set the NET_BLOCK and corresponding NET_BLOCK_OP in the net buffer and
corresponding net vector according to the bulk pointer and bulk length.
All the pointers in the Index'th NET_BLOCK and NET_BLOCK_OP are set to the
bulk's head and tail respectively. So, this function alone can't be used by
NetbufAlloc.
@param[in, out] Nbuf Pointer to the net buffer.
@param[in] Bulk Pointer to the data.
@param[in] Len Length of the bulk data.
@param[in] Index The data block index in the net buffer the bulk
data should belong to.
**/
)
{
}
/**
Set the NET_BLOCK_OP in the net buffer. The corresponding NET_BLOCK
structure is left untouched.
Some times, there is no 1:1 relationship between NET_BLOCK and NET_BLOCK_OP.
For example, that in NetbufGetFragment.
@param[in, out] Nbuf Pointer to the net buffer.
@param[in] Bulk Pointer to the data.
@param[in] Len Length of the bulk data.
@param[in] Index The data block index in the net buffer the bulk
data should belong to.
**/
)
{
}
/**
Helper function for NetbufGetFragment. NetbufGetFragment may allocate the
first block to reserve HeadSpace bytes header space. So it needs to create a
new net vector for the first block and can avoid copy for the remaining data
by sharing the old net vector.
@param[in] Arg Point to the old NET_VECTOR.
**/
)
{
}
/**
Create a NET_BUF structure which contains Len byte data of Nbuf starting from
Offset.
A new NET_BUF structure will be created but the associated data in NET_VECTOR
is shared. This function exists to do IP packet fragmentation.
@param[in] Nbuf Pointer to the net buffer to be extracted.
@param[in] Offset Starting point of the data to be included in the new
net buffer.
@param[in] Len Bytes of data to be included in the new net buffer.
@param[in] HeadSpace Bytes of head space to reserve for protocol header.
@return Pointer to the cloned net buffer, or NULL if the
allocation failed due to resource limit.
**/
NET_BUF *
)
{
return NULL;
}
//
// First find the first and last BlockOp that contains
// the valid data, and compute the offset of the first
// BlockOp and length of the last BlockOp
//
Cur = 0;
break;
}
}
//
// First is the index of the first BlockOp, FirstSkip is
// the offset of the first byte in the first BlockOp.
//
Last = 0;
LastLen = 0;
Index++;
break;
}
}
} else {
}
CurBlockOp = 0;
if (HeadSpace != 0) {
//
// Allocate an extra block to accomdate the head space.
//
BlockOpNum++;
return NULL;
}
goto FreeChild;
}
//
// Reserve the head space in the first block
//
CurBlockOp++;
} else {
return NULL;
}
}
//
// Set all the BlockOp up, the first and last one are special
// and need special process.
//
);
);
}
);
}
return Child;
return NULL;
}
/**
Build a NET_BUF from external blocks.
A new NET_BUF structure will be created from external blocks. Additional block
of memory will be allocated to hold reserved HeadSpace bytes of header room
and existing HeadLen bytes of header but the external blocks are shared by the
net buffer to avoid data copying.
@param[in] ExtFragment Pointer to the data block.
@param[in] ExtNum The number of the data blocks.
@param[in] HeadSpace The head space to be reserved.
@param[in] HeadLen The length of the protocol header, This function
will pull that number of data into a linear block.
@param[in] ExtFree Pointer to the caller provided free function.
@param[in] Arg The argument passed to ExtFree when ExtFree is
called.
@return Pointer to the net buffer built from the data blocks,
or NULL if the allocation failed due to resource
limit.
**/
NET_BUF *
)
{
SavedFragment.Len = 0;
FirstBlockLen = 0;
FirstBlock = NULL;
Index = 0;
TotalLen = 0;
SavedIndex = 0;
Len = 0;
Copied = 0;
//
// No need to consolidate the header if the first block is
// longer than the header length or there is only one block.
//
HeadLen = 0;
}
//
// Allocate an extra block if we need to:
// 1. Allocate some header space
// 2. aggreate the packet header
//
if (FirstBlock == NULL) {
return NULL;
}
BlockNum++;
}
//
// Copy the header to the first block, reduce the NET_BLOCK
// to allocate by one for each block that is completely covered
// by the first bulk.
//
if (HeadLen != 0) {
BlockNum--;
if (Len == 0) {
//
// Increament the index number to point to the next
// non-empty fragment.
//
Index++;
break;
}
} else {
//
// Adjust the block structure to exclude the data copied,
// So, the left-over block can be processed as other blocks.
// But it must be recovered later. (SavedIndex > 0) always
// holds since we don't aggreate the header if the first block
// is bigger enough that the header is continuous
//
SavedIndex = Index;
break;
}
}
}
goto FreeFirstBlock;
}
//
// Set the first block up which may contain
// some head space and aggregated header
//
CurBlock = 0;
if (FirstBlockLen != 0) {
CurBlock++;
}
CurBlock++;
}
if (SavedIndex != 0) {
}
return Nbuf;
if (FirstBlock != NULL) {
}
return NULL;
}
/**
Build a fragment table to contain the fragments in the net buffer. This is the
opposite operation of the NetbufFromExt.
@param[in] Nbuf Point to the net buffer.
@param[in, out] ExtFragment Pointer to the data block.
@param[in, out] ExtNum The number of the data blocks.
@retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than
ExtNum.
@retval EFI_SUCCESS Fragment table is built successfully.
**/
)
{
Current = 0;
continue;
}
Current++;
} else {
return EFI_BUFFER_TOO_SMALL;
}
}
return EFI_SUCCESS;
}
/**
Build a net buffer from a list of net buffers.
All the fragments will be collected from the list of NEW_BUF and then a new
net buffer will be created through NetbufFromExt.
@param[in] BufList A List of the net buffer.
@param[in] HeadSpace The head space to be reserved.
@param[in] HeaderLen The length of the protocol header, This function
will pull that number of data into a linear block.
@param[in] ExtFree Pointer to the caller provided free function.
@param[in] Arg The argument passed to ExtFree when ExtFree is called.
@return Pointer to the net buffer built from the list of net
buffers.
**/
NET_BUF *
)
{
//
//Compute how many blocks are there
//
FragmentNum = 0;
}
//
//Allocate and copy block points
//
return NULL;
}
Current = 0;
Current++;
}
}
}
return Nbuf;
}
/**
Reserve some space in the header room of the net buffer.
Upon allocation, all the space are in the tail room of the buffer. Call this
function to move some space to the header room. This function is quite limited
in that it can only reserve space from the first block of an empty NET_BUF not
built from the external. But it should be enough for the network stack.
@param[in, out] Nbuf Pointer to the net buffer.
@param[in] Len The length of buffer to be reserved from the header.
**/
)
{
}
/**
Allocate Len bytes of space from the header or tail of the buffer.
@param[in, out] Nbuf Pointer to the net buffer.
@param[in] Len The length of the buffer to be allocated.
@param[in] FromHead The flag to indicate whether reserve the data
from head (TRUE) or tail (FALSE).
@return Pointer to the first byte of the allocated buffer,
or NULL if there is no sufficient space.
**/
)
{
Index = 0;
if (FromHead) {
//
// Allocate some space from head. If the buffer is empty,
// allocate from the first block. If it isn't, allocate
// from the first non-empty block, or the block before that.
//
Index = 0;
} else {
Index--;
}
}
return NULL;
}
} else {
//
// Allocate some space from the tail. If the buffer is empty,
// allocate from the first block. If it isn't, allocate
// from the last non-empty block, or the block after that.
//
Index = 0;
} else {
Index++;
}
}
return NULL;
}
return SavedTail;
}
}
/**
Trim a single NET_BLOCK by Len bytes from the header or tail.
@param[in, out] BlockOp Pointer to the NET_BLOCK.
@param[in] Len The length of the data to be trimmed.
@param[in] FromHead The flag to indicate whether trim data from head
(TRUE) or tail (FALSE).
**/
)
{
if (FromHead) {
} else {
}
}
/**
Trim Len bytes from the header or tail of the net buffer.
@param[in, out] Nbuf Pointer to the net buffer.
@param[in] Len The length of the data to be trimmed.
@param[in] FromHead The flag to indicate whether trim data from head
(TRUE) or tail (FALSE).
@return Length of the actually trimmed data, which is possible to be less
than Len because the TotalSize of Nbuf is less than Len.
**/
)
{
}
//
// If FromTail is true, iterate backward. That
// is, init Index to NBuf->BlockNum - 1, and
// decrease it by 1 during each loop. Otherwise,
// iterate forward. That is, init Index to 0, and
// increase it by 1 during each loop.
//
Trimmed = 0;
for (;;) {
continue;
}
} else {
break;
}
}
return Trimmed;
}
/**
Copy Len bytes of data from the specific offset of the net buffer to the
destination memory.
The Len bytes of data may cross the several fragments of the net buffer.
@param[in] Nbuf Pointer to the net buffer.
@param[in] Offset The sequence number of the first byte to copy.
@param[in] Len Length of the data to copy.
@param[in] Dest The destination of the data to copy to.
@return The length of the actual copied data, or 0 if the offset
specified exceeds the total size of net buffer.
**/
)
{
return 0;
}
}
//
// Skip to the offset. Don't make "Offset-By-One" error here.
// Cur + BLOCK.SIZE is the first sequence number of next block.
// So, (Offset < Cur + BLOCK.SIZE) means that the first byte
// is in the current block. if (Offset == Cur + BLOCK.SIZE), the
// first byte is the next block's first byte.
//
Cur = 0;
continue;
}
break;
}
}
//
// Cur is the sequence number of the first byte in the block
// Offset - Cur is the number of bytes before first byte to
// to copy in the current block.
//
return Len;
}
Index++;
} else {
break;
}
}
return Copied;
}
/**
Initiate the net buffer queue.
@param[in, out] NbufQue Pointer to the net buffer queue to be initialized.
**/
)
{
}
/**
Allocate and initialize a net buffer queue.
@return Pointer to the allocated net buffer queue, or NULL if the
allocation failed due to resource limit.
**/
)
{
return NULL;
}
return NbufQue;
}
/**
Free a net buffer queue.
Decrease the reference count of the net buffer queue by one. The real resource
free operation isn't performed until the reference count of the net buffer
queue is decreased to 0.
@param[in] NbufQue Pointer to the net buffer queue to be freed.
**/
)
{
}
}
/**
Append a net buffer to the net buffer queue.
@param[in, out] NbufQue Pointer to the net buffer queue.
@param[in, out] Nbuf Pointer to the net buffer to be appended.
**/
)
{
}
/**
Remove a net buffer from the head in the specific queue and return it.
@param[in, out] NbufQue Pointer to the net buffer queue.
@return Pointer to the net buffer removed from the specific queue,
or NULL if there is no net buffer in the specific queue.
**/
NET_BUF *
)
{
return NULL;
}
return First;
}
/**
Copy Len bytes of data from the net buffer queue at the specific offset to the
destination memory.
The copying operation is the same as NetbufCopy but applies to the net buffer
queue instead of the net buffer.
@param[in] NbufQue Pointer to the net buffer queue.
@param[in] Offset The sequence number of the first byte to copy.
@param[in] Len Length of the data to copy.
@param[out] Dest The destination of the data to copy to.
@return The length of the actual copied data, or 0 if the offset
specified exceeds the total size of net buffer queue.
**/
)
{
return 0;
}
}
//
// skip to the Offset
//
Cur = 0;
break;
}
}
//
// Copy the data in the first buffer.
//
}
//
// Iterate over the others
//
} else {
break;
}
}
return Copied;
}
/**
Trim Len bytes of data from the buffer queue and free any net buffer
that is completely trimmed.
The trimming operation is the same as NetbufTrim but applies to the net buffer
queue instead of the net buffer.
@param[in, out] NbufQue Pointer to the net buffer queue.
@param[in] Len Length of the data to trim.
@return The actual length of the data trimmed.
**/
)
{
if (Len == 0) {
return 0;
}
}
Trimmed = 0;
NetbufFree (Nbuf);
if (Len == 0) {
break;
}
} else {
break;
}
}
return Trimmed;
}
/**
Flush the net buffer queue.
@param[in, out] NbufQue Pointer to the queue to be flushed.
**/
)
{
}
/**
Compute the checksum for a bulk of data.
@param[in] Bulk Pointer to the data.
@param[in] Len Length of the data, in bytes.
@return The computed checksum.
**/
)
{
Sum = 0;
while (Len > 1) {
Bulk += 2;
Len -= 2;
}
//
// Add left-over byte, if any
//
if (Len > 0) {
}
//
// Fold 32-bit sum to 16 bits
//
while ((Sum >> 16) != 0) {
}
}
/**
Add two checksums.
@param[in] Checksum1 The first checksum to be added.
@param[in] Checksum2 The second checksum to be added.
@return The new checksum.
**/
)
{
//
// two UINT16 can only add up to a carry of 1.
//
if ((Sum >> 16) != 0) {
}
}
/**
Compute the checksum for a NET_BUF.
@param[in] Nbuf Pointer to the net buffer.
@return The computed checksum.
**/
)
{
TotalSum = 0;
Offset = 0;
continue;
}
if ((Offset & 0x01) != 0) {
//
// The checksum starts with an odd byte, swap
// the checksum before added to total checksum
//
}
}
return TotalSum;
}
/**
Src and Dst are in network byte order, and Len is in host byte order.
@param[in] Src The source address of the packet.
@param[in] Dst The destination address of the packet.
@param[in] Proto The protocol type of the packet.
@param[in] Len The length of the packet.
@return The computed checksum.
**/
)
{
//
// Zero the memory to relieve align problems
//
}
/**
Src and Dst are in network byte order, and Len is in host byte order.
@param[in] Src The source address of the packet.
@param[in] Dst The destination address of the packet.
@param[in] NextHeader The protocol type of the packet.
@param[in] Len The length of the packet.
@return The computed checksum.
**/
)
{
//
// Zero the memory to relieve align problems
//
}
/**
The function frees the net buffer which allocated by the IP protocol. It releases
only the net buffer and doesn't call the external free function.
This function should be called after finishing the process of mIpSec->ProcessExt()
for outbound traffic. The (EFI_IPSEC2_PROTOCOL)->ProcessExt() allocates a new
buffer for the ESP, so there needs a function to free the old net buffer.
@param[in] Nbuf The network buffer to be freed.
**/
)
{
//
// Update Vector only when NBuf is to be released. That is,
// all the sharing of Nbuf increse Vector's RefCnt by one
//
return;
}
//
// If NET_VECTOR_OWN_FIRST is set, release the first block since it is
// allocated by us
//
}
}
}