2 This library provides basic function for UEFI network stack.
4 Copyright (c) 2005 - 2008, Intel Corporation
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Protocol/DriverBinding.h>
21 #include <Protocol/ComponentName.h>
22 #include <Protocol/DriverConfiguration.h>
23 #include <Protocol/DriverDiagnostics.h>
24 #include <Protocol/Dpc.h>
26 typedef UINT32 IP4_ADDR
;
27 typedef UINT32 TCP_SEQNO
;
28 typedef UINT16 TCP_PORTNO
;
31 NET_ETHER_ADDR_LEN
= 6,
32 NET_IFTYPE_ETHERNET
= 0x01,
34 EFI_IP_PROTO_UDP
= 0x11,
35 EFI_IP_PROTO_TCP
= 0x06,
36 EFI_IP_PROTO_ICMP
= 0x01,
39 // The address classification
53 // Ethernet head definition
56 UINT8 DstMac
[NET_ETHER_ADDR_LEN
];
57 UINT8 SrcMac
[NET_ETHER_ADDR_LEN
];
63 // The EFI_IP4_HEADER is hard to use because the source and
64 // destination address are defined as EFI_IPv4_ADDRESS, which
65 // is a structure. Two structures can't be compared or masked
66 // directly. This is why there is an internal representation.
84 // ICMP head definition. ICMP message is categoried as either an error
85 // message or query message. Two message types have their own head format.
95 UINT32 Fourth
; // 4th filed of the head, it depends on Type.
97 } IP4_ICMP_ERROR_HEAD
;
103 } IP4_ICMP_QUERY_HEAD
;
107 // UDP header definition
118 // TCP header definition
135 #define NET_MAC_EQUAL(pMac1, pMac2, Len) \
136 (CompareMem ((pMac1), (pMac2), Len) == 0)
138 #define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \
139 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))
141 #define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \
142 (((UINT32) (x) & 0xff00) << 8) | \
143 (((UINT32) (x) & 0xff0000) >> 8) | \
144 (((UINT32) (x) & 0xff000000) >> 24))
146 #define HTONL(x) NTOHL(x)
148 #define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \
149 (((UINT16) (x) & 0xff00) >> 8))
151 #define HTONS(x) NTOHS(x)
154 // Test the IP's attribute, All the IPs are in host byte order.
156 #define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)
157 #define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)
158 #define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))
159 #define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)
162 // Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.
164 #define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))
165 #define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))
166 #define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)
169 Return the length of the mask. If the mask is invalid,
170 return the invalid length 33, which is IP4_MASK_NUM.
171 NetMask is in the host byte order.
173 @param[in] NetMask The netmask to get the length from.
175 @return The length of the netmask, IP4_MASK_NUM if the mask isn't.
185 Return the class of the address, such as class a, b, c.
186 Addr is in host byte order.
188 @param[in] Addr The address to get the class from.
190 @return IP address class, such as IP4_ADDR_CLASSA.
200 Check whether the IP is a valid unicast address according to
201 the netmask. If NetMask is zero, use the IP address's class to
202 get the default mask.
204 @param[in] Ip The IP to check against.
205 @param[in] NetMask The mask of the IP.
207 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.
217 extern IP4_ADDR gIp4AllMasks
[IP4_MASK_NUM
];
220 extern EFI_IPv4_ADDRESS mZeroIp4Addr
;
222 #define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))
223 #define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))
224 #define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))
225 #define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))
227 #define TICKS_PER_MS 10000U
228 #define TICKS_PER_SECOND 10000000U
230 #define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)
233 Extract a UINT32 from a byte stream, then convert it to host
234 byte order. Use this function to avoid alignment error.
236 @param[in] Buf The buffer to extract the UINT32.
238 @return The UINT32 extracted.
248 Put a UINT32 to the byte stream. Convert it from host byte order
249 to network byte order before putting.
251 @param[in, out] Buf The buffer to put the UINT32.
252 @param[in] Data The data to put.
263 Initialize a random seed using current time.
265 @return The random seed initialized with current time.
275 #define NET_LIST_USER_STRUCT(Entry, Type, Field) \
276 BASE_CR(Entry, Type, Field)
278 #define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \
279 CR(Entry, Type, Field, Sig)
282 // Iterate through the doule linked list. It is NOT delete safe
284 #define NET_LIST_FOR_EACH(Entry, ListHead) \
285 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)
288 // Iterate through the doule linked list. This is delete-safe.
289 // Don't touch NextEntry. Also, don't use this macro if list
290 // entries other than the Entry may be deleted when processing
291 // the current Entry.
293 #define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \
294 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \
295 Entry != (ListHead); \
296 Entry = NextEntry, NextEntry = Entry->ForwardLink \
300 // Make sure the list isn't empty before get the frist/last record.
302 #define NET_LIST_HEAD(ListHead, Type, Field) \
303 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)
305 #define NET_LIST_TAIL(ListHead, Type, Field) \
306 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)
310 Remove the first entry on the list.
312 @param[in, out] Head The list header.
314 @return The entry that is removed from the list, NULL if the list is empty.
320 IN OUT LIST_ENTRY
*Head
324 Remove the last entry on the list.
326 @param[in, out] Head The list head.
328 @return The entry that is removed from the list, NULL if the list is empty.
334 IN OUT LIST_ENTRY
*Head
338 Insert the NewEntry after the PrevEntry.
340 @param[in, out] PrevEntry The previous entry to insert after.
341 @param[in, out] NewEntry The new entry to insert.
347 IN OUT LIST_ENTRY
*PrevEntry
,
348 IN OUT LIST_ENTRY
*NewEntry
352 Insert the NewEntry before the PostEntry.
354 @param[in, out] PostEntry The entry to insert before.
355 @param[in, out] NewEntry The new entry to insert.
360 NetListInsertBefore (
361 IN OUT LIST_ENTRY
*PostEntry
,
362 IN OUT LIST_ENTRY
*NewEntry
367 // Object container: EFI network stack spec defines various kinds of
368 // tokens. The drivers can share code to manage those objects.
382 #define NET_MAP_INCREAMENT 64
385 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.
387 @param[in, out] Map The netmap to initialize.
397 To clean up the netmap, that is, release allocated memories.
399 @param[in, out] Map The netmap to clean up.
409 Test whether the netmap is empty.
411 @param[in] Map The net map to test.
413 @return TRUE if the netmap is empty, otherwise FALSE.
423 Return the number of the <Key, Value> pairs in the netmap.
425 @param[in] Map The netmap to get the entry number.
427 @return The entry number in the netmap.
437 Allocate an item to save the <Key, Value> pair to the head of the netmap.
439 @param[in, out] Map The netmap to insert into.
440 @param[in] Key The user's key.
441 @param[in] Value The user's value for the key.
443 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
444 @retval EFI_SUCCESS The item is inserted to the head.
452 IN VOID
*Value OPTIONAL
456 Allocate an item to save the <Key, Value> pair to the tail of the netmap.
458 @param[in, out] Map The netmap to insert into.
459 @param[in] Key The user's key.
460 @param[in] Value The user's value for the key.
462 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.
463 @retval EFI_SUCCESS The item is inserted to the tail.
471 IN VOID
*Value OPTIONAL
475 Find the key in the netmap.
477 @param[in] Map The netmap to search within.
478 @param[in] Key The key to search.
480 @return The point to the item contains the Key, or NULL if Key isn't in the map.
491 Remove the item from the netmap.
493 @param[in, out] Map The netmap to remove the item from.
494 @param[in, out] Item The item to remove.
495 @param[out] Value The variable to receive the value if not NULL.
497 @return The key of the removed item.
504 IN OUT NET_MAP_ITEM
*Item
,
505 OUT VOID
**Value OPTIONAL
509 Remove the first entry on the netmap.
511 @param[in, out] Map The netmap to remove the head from.
512 @param[out] Value The variable to receive the value if not NULL.
514 @return The key of the item removed.
521 OUT VOID
**Value OPTIONAL
525 Remove the last entry on the netmap.
527 @param[in, out] Map The netmap to remove the tail from.
528 @param[out] Value The variable to receive the value if not NULL.
530 @return The key of the item removed.
537 OUT VOID
**Value OPTIONAL
542 (*NET_MAP_CALLBACK
) (
544 IN NET_MAP_ITEM
*Item
,
549 Iterate through the netmap and call CallBack for each item. It will
550 contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break
551 from the loop. It returns the CallBack's last return value. This
552 function is delete safe for the current item.
554 @param[in] Map The Map to iterate through.
555 @param[in] CallBack The callback function to call for each item.
556 @param[in] Arg The opaque parameter to the callback.
558 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item
560 @retval Others It returns the CallBack's last return value.
567 IN NET_MAP_CALLBACK CallBack
,
573 // Helper functions to implement driver binding and service binding protocols.
576 Create a child of the service that is identified by ServiceBindingGuid.
578 @param[in] Controller The controller which has the service installed.
579 @param[in] Image The image handle used to open service.
580 @param[in] ServiceBindingGuid The service's Guid.
581 @param[in, out] ChildHandle The handle to receive the create child
583 @retval EFI_SUCCESS The child is successfully created.
584 @retval Others Failed to create the child.
589 NetLibCreateServiceChild (
590 IN EFI_HANDLE Controller
,
592 IN EFI_GUID
*ServiceBindingGuid
,
593 IN OUT EFI_HANDLE
*ChildHandle
597 Destory a child of the service that is identified by ServiceBindingGuid.
599 @param[in] Controller The controller which has the service installed.
600 @param[in] Image The image handle used to open service.
601 @param[in] ServiceBindingGuid The service's Guid.
602 @param[in] ChildHandle The child to destory
604 @retval EFI_SUCCESS The child is successfully destoried.
605 @retval Others Failed to destory the child.
610 NetLibDestroyServiceChild (
611 IN EFI_HANDLE Controller
,
613 IN EFI_GUID
*ServiceBindingGuid
,
614 IN EFI_HANDLE ChildHandle
618 Convert the mac address of the simple network protocol installed on
619 SnpHandle to a unicode string. Callers are responsible for freeing the
622 @param[in] SnpHandle The handle where the simple network protocol is
624 @param[in] ImageHandle The image handle used to act as the agent handle to
625 get the simple network protocol.
626 @param[out] MacString The pointer to store the address of the string
627 representation of the mac address.
629 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.
630 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.
631 @retval Others Failed to open the simple network protocol.
637 IN EFI_HANDLE SnpHandle
,
638 IN EFI_HANDLE ImageHandle
,
639 OUT CHAR16
**MacString
643 Create an IPv4 device path node.
645 @param[in, out] Node Pointer to the IPv4 device path node.
646 @param[in] Controller The handle where the NIC IP4 config protocol resides.
647 @param[in] LocalIp The local IPv4 address.
648 @param[in] LocalPort The local port.
649 @param[in] RemoteIp The remote IPv4 address.
650 @param[in] RemotePort The remote port.
651 @param[in] Protocol The protocol type in the IP header.
652 @param[in] UseDefaultAddress Whether this instance is using default address or not.
657 NetLibCreateIPv4DPathNode (
658 IN OUT IPv4_DEVICE_PATH
*Node
,
659 IN EFI_HANDLE Controller
,
662 IN IP4_ADDR RemoteIp
,
663 IN UINT16 RemotePort
,
665 IN BOOLEAN UseDefaultAddress
669 Find the UNDI/SNP handle from controller and protocol GUID.
670 For example, IP will open a MNP child to transmit/receive
671 packets, when MNP is stopped, IP should also be stopped. IP
672 needs to find its own private data which is related the IP's
673 service binding instance that is install on UNDI/SNP handle.
674 Now, the controller is either a MNP or ARP child handle. But
675 IP opens these handle BY_DRIVER, use that info, we can get the
678 @param[in] Controller Then protocol handle to check.
679 @param[in] ProtocolGuid The protocol that is related with the handle.
681 @return The UNDI/SNP handle or NULL for errors.
687 IN EFI_HANDLE Controller
,
688 IN EFI_GUID
*ProtocolGuid
692 Add a Deferred Procedure Call to the end of the DPC queue.
694 @param[in] DpcTpl The EFI_TPL that the DPC should be invoked.
695 @param[in] DpcProcedure Pointer to the DPC's function.
696 @param[in] DpcContext Pointer to the DPC's context. Passed to DpcProcedure
697 when DpcProcedure is invoked.
699 @retval EFI_SUCCESS The DPC was queued.
700 @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL, or DpcProcedure
702 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
703 add the DPC to the queue.
710 IN EFI_DPC_PROCEDURE DpcProcedure
,
711 IN VOID
*DpcContext OPTIONAL
715 Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl
716 value greater than or equal to the current TPL are invoked in the order that
717 they were queued. DPCs with higher DpcTpl values are invoked before DPCs with
720 @retval EFI_SUCCESS One or more DPCs were invoked.
721 @retval EFI_NOT_FOUND No DPCs were invoked.
731 This is the default unload handle for all the network drivers.
733 @param[in] ImageHandle The drivers' driver image.
735 @retval EFI_SUCCESS The image is unloaded.
736 @retval Others Failed to unload the image.
741 NetLibDefaultUnload (
742 IN EFI_HANDLE ImageHandle
749 NET_BUF_SIGNATURE
= SIGNATURE_32 ('n', 'b', 'u', 'f'),
750 NET_VECTOR_SIGNATURE
= SIGNATURE_32 ('n', 'v', 'e', 'c'),
751 NET_QUE_SIGNATURE
= SIGNATURE_32 ('n', 'b', 'q', 'u'),
754 NET_PROTO_DATA
= 64, // Opaque buffer for protocols
755 NET_BUF_HEAD
= 1, // Trim or allocate space from head
756 NET_BUF_TAIL
= 0, // Trim or allocate space from tail
757 NET_VECTOR_OWN_FIRST
= 0x01 // We allocated the 1st block in the vector
758 } NET_SIGNATURE_TYPE
;
760 #define NET_CHECK_SIGNATURE(PData, SIGNATURE) \
761 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))
763 #define NET_SWAP_SHORT(Value) \
764 ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))
767 // Single memory block in the vector.
770 UINT32 Len
; // The block's length
771 UINT8
*Bulk
; // The block's Data
774 typedef VOID (*NET_VECTOR_EXT_FREE
) (VOID
*Arg
);
777 //NET_VECTOR contains several blocks to hold all packet's
778 //fragments and other house-keeping stuff for sharing. It
779 //doesn't specify the where actual packet fragment begins.
783 INTN RefCnt
; // Reference count to share NET_VECTOR.
784 NET_VECTOR_EXT_FREE Free
; // external function to free NET_VECTOR
785 VOID
*Arg
; // opeque argument to Free
786 UINT32 Flag
; // Flags, NET_VECTOR_OWN_FIRST
787 UINT32 Len
; // Total length of the assocated BLOCKs
794 //NET_BLOCK_OP operate on the NET_BLOCK, It specifies
795 //where the actual fragment begins and where it ends
798 UINT8
*BlockHead
; // Block's head, or the smallest valid Head
799 UINT8
*BlockTail
; // Block's tail. BlockTail-BlockHead=block length
800 UINT8
*Head
; // 1st byte of the data in the block
801 UINT8
*Tail
; // Tail of the data in the block, Tail-Head=Size
802 UINT32 Size
; // The size of the data
807 //NET_BUF is the buffer manage structure used by the
808 //network stack. Every network packet may be fragmented,
809 //and contains multiple fragments. The Vector points to
810 //memory blocks used by the each fragment, and BlockOp
811 //specifies where each fragment begins and ends.
813 //It also contains a opaque area for protocol to store
814 //per-packet informations. Protocol must be caution not
815 //to overwrite the members after that.
820 LIST_ENTRY List
; // The List this NET_BUF is on
822 IP4_HEAD
*Ip
; // Network layer header, for fast access
823 TCP_HEAD
*Tcp
; // Transport layer header, for fast access
824 UINT8 ProtoData
[NET_PROTO_DATA
]; //Protocol specific data
826 NET_VECTOR
*Vector
; // The vector containing the packet
828 UINT32 BlockOpNum
; // Total number of BlockOp in the buffer
829 UINT32 TotalSize
; // Total size of the actual packet
830 NET_BLOCK_OP BlockOp
[1]; // Specify the position of actual packet
835 //A queue of NET_BUFs, It is just a thin extension of
841 LIST_ENTRY List
; // The List this buffer queue is on
843 LIST_ENTRY BufList
; // list of queued buffers
844 UINT32 BufSize
; // total length of DATA in the buffers
845 UINT32 BufNum
; // total number of buffers on the chain
849 // Pseudo header for TCP and UDP checksum
862 // The fragment entry table used in network interfaces. This is
863 // the same as NET_BLOCK now. Use two different to distinguish
864 // the two in case that NET_BLOCK be enhanced later.
871 #define NET_GET_REF(PData) ((PData)->RefCnt++)
872 #define NET_PUT_REF(PData) ((PData)->RefCnt--)
873 #define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData)
875 #define NET_BUF_SHARED(Buf) \
876 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))
878 #define NET_VECTOR_SIZE(BlockNum) \
879 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))
881 #define NET_BUF_SIZE(BlockOpNum) \
882 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))
884 #define NET_HEADSPACE(BlockOp) \
885 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)
887 #define NET_TAILSPACE(BlockOp) \
888 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)
891 Allocate a single block NET_BUF. Upon allocation, all the
892 free space is in the tail room.
894 @param[in] Len The length of the block.
896 @return Pointer to the allocated NET_BUF, or NULL if the
897 allocation failed due to resource limit.
907 Free the net buffer and its associated NET_VECTOR.
909 Decrease the reference count of the net buffer by one. Free the associated net
910 vector and itself if the reference count of the net buffer is decreased to 0.
911 The net vector free operation just decrease the reference count of the net
912 vector by one and do the real resource free operation when the reference count
913 of the net vector is 0.
915 @param[in] Nbuf Pointer to the NET_BUF to be freed.
925 Get the index of NET_BLOCK_OP that contains the byte at Offset in the net
928 This can be used to, for example, retrieve the IP header in the packet. It
929 also can be used to get the fragment that contains the byte which is used
930 mainly by the library implementation itself.
932 @param[in] Nbuf Pointer to the net buffer.
933 @param[in] Offset The offset of the byte.
934 @param[out] Index Index of the NET_BLOCK_OP that contains the byte at
937 @return Pointer to the Offset'th byte of data in the net buffer, or NULL
938 if there is no such data in the net buffer.
946 OUT UINT32
*Index OPTIONAL
950 Create a copy of the net buffer that shares the associated net vector.
952 The reference count of the newly created net buffer is set to 1. The reference
953 count of the associated net vector is increased by one.
955 @param[in] Nbuf Pointer to the net buffer to be cloned.
957 @return Pointer to the cloned net buffer, or NULL if the
958 allocation failed due to resource limit.
968 Create a duplicated copy of the net buffer with data copied and HeadSpace
969 bytes of head space reserved.
971 The duplicated net buffer will allocate its own memory to hold the data of the
974 @param[in] Nbuf Pointer to the net buffer to be duplicated from.
975 @param[in, out] Duplicate Pointer to the net buffer to duplicate to, if
976 NULL a new net buffer is allocated.
977 @param[in] HeadSpace Length of the head space to reserve.
979 @return Pointer to the duplicated net buffer, or NULL if
980 the allocation failed due to resource limit.
987 IN OUT NET_BUF
*Duplicate OPTIONAL
,
992 Create a NET_BUF structure which contains Len byte data of Nbuf starting from
995 A new NET_BUF structure will be created but the associated data in NET_VECTOR
996 is shared. This function exists to do IP packet fragmentation.
998 @param[in] Nbuf Pointer to the net buffer to be extracted.
999 @param[in] Offset Starting point of the data to be included in the new
1001 @param[in] Len Bytes of data to be included in the new net buffer.
1002 @param[in] HeadSpace Bytes of head space to reserve for protocol header.
1004 @return Pointer to the cloned net buffer, or NULL if the
1005 allocation failed due to resource limit.
1018 Reserve some space in the header room of the net buffer.
1020 Upon allocation, all the space are in the tail room of the buffer. Call this
1021 function to move some space to the header room. This function is quite limited
1022 in that it can only reserve space from the first block of an empty NET_BUF not
1023 built from the external. But it should be enough for the network stack.
1025 @param[in, out] Nbuf Pointer to the net buffer.
1026 @param[in] Len The length of buffer to be reserved from the header.
1032 IN OUT NET_BUF
*Nbuf
,
1037 Allocate Len bytes of space from the header or tail of the buffer.
1039 @param[in, out] Nbuf Pointer to the net buffer.
1040 @param[in] Len The length of the buffer to be allocated.
1041 @param[in] FromHead The flag to indicate whether reserve the data
1042 from head (TRUE) or tail (FALSE).
1044 @return Pointer to the first byte of the allocated buffer,
1045 or NULL if there is no sufficient space.
1051 IN OUT NET_BUF
*Nbuf
,
1057 Trim Len bytes from the header or tail of the net buffer.
1059 @param[in, out] Nbuf Pointer to the net buffer.
1060 @param[in] Len The length of the data to be trimmed.
1061 @param[in] FromHead The flag to indicate whether trim data from head
1062 (TRUE) or tail (FALSE).
1064 @return Length of the actually trimmed data, which is possible to be less
1065 than Len because the TotalSize of Nbuf is less than Len.
1071 IN OUT NET_BUF
*Nbuf
,
1077 Copy Len bytes of data from the specific offset of the net buffer to the
1080 The Len bytes of data may cross the several fragments of the net buffer.
1082 @param[in] Nbuf Pointer to the net buffer.
1083 @param[in] Offset The sequence number of the first byte to copy.
1084 @param[in] Len Length of the data to copy.
1085 @param[in] Dest The destination of the data to copy to.
1087 @return The length of the actual copied data, or 0 if the offset
1088 specified exceeds exceeds the total size of net buffer.
1101 Build a NET_BUF from external blocks.
1103 A new NET_BUF structure will be created from external blocks. Additional block
1104 of memory will be allocated to hold reserved HeadSpace bytes of header room
1105 and existing HeadLen bytes of header but the external blocks are shared by the
1106 net buffer to avoid data copying.
1108 @param[in] ExtFragment Pointer to the data block.
1109 @param[in] ExtNum The number of the data blocks.
1110 @param[in] HeadSpace The head space to be reserved.
1111 @param[in] HeadLen The length of the protocol header, This function
1112 will pull that number of data into a linear block.
1113 @param[in] ExtFree Pointer to the caller provided free function.
1114 @param[in] Arg The argument passed to ExtFree when ExtFree is
1117 @return Pointer to the net buffer built from the data blocks,
1118 or NULL if the allocation failed due to resource
1125 IN NET_FRAGMENT
*ExtFragment
,
1127 IN UINT32 HeadSpace
,
1129 IN NET_VECTOR_EXT_FREE ExtFree
,
1130 IN VOID
*Arg OPTIONAL
1134 Build a fragment table to contain the fragments in the net buffer. This is the
1135 opposite operation of the NetbufFromExt.
1137 @param[in] Nbuf Point to the net buffer.
1138 @param[in, out] ExtFragment Pointer to the data block.
1139 @param[in, out] ExtNum The number of the data blocks.
1141 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than
1143 @retval EFI_SUCCESS Fragment table is built successfully.
1150 IN OUT NET_FRAGMENT
*ExtFragment
,
1151 IN OUT UINT32
*ExtNum
1155 Build a net buffer from a list of net buffers.
1157 All the fragments will be collected from the list of NEW_BUF and then a new
1158 net buffer will be created through NetbufFromExt.
1160 @param[in] BufList A List of the net buffer.
1161 @param[in] HeadSpace The head space to be reserved.
1162 @param[in] HeaderLen The length of the protocol header, This function
1163 will pull that number of data into a linear block.
1164 @param[in] ExtFree Pointer to the caller provided free function.
1165 @param[in] Arg The argument passed to ExtFree when ExtFree is called.
1167 @return Pointer to the net buffer built from the list of net
1174 IN LIST_ENTRY
*BufList
,
1175 IN UINT32 HeadSpace
,
1176 IN UINT32 HeaderLen
,
1177 IN NET_VECTOR_EXT_FREE ExtFree
,
1178 IN VOID
*Arg OPTIONAL
1182 Free a list of net buffers.
1184 @param[in, out] Head Pointer to the head of linked net buffers.
1190 IN OUT LIST_ENTRY
*Head
1194 Initiate the net buffer queue.
1196 @param[in, out] NbufQue Pointer to the net buffer queue to be initialized.
1202 IN OUT NET_BUF_QUEUE
*NbufQue
1206 Allocate and initialize a net buffer queue.
1208 @return Pointer to the allocated net buffer queue, or NULL if the
1209 allocation failed due to resource limit.
1219 Free a net buffer queue.
1221 Decrease the reference count of the net buffer queue by one. The real resource
1222 free operation isn't performed until the reference count of the net buffer
1223 queue is decreased to 0.
1225 @param[in] NbufQue Pointer to the net buffer queue to be freed.
1231 IN NET_BUF_QUEUE
*NbufQue
1235 Remove a net buffer from the head in the specific queue and return it.
1237 @param[in, out] NbufQue Pointer to the net buffer queue.
1239 @return Pointer to the net buffer removed from the specific queue,
1240 or NULL if there is no net buffer in the specific queue.
1246 IN OUT NET_BUF_QUEUE
*NbufQue
1250 Append a net buffer to the net buffer queue.
1252 @param[in, out] NbufQue Pointer to the net buffer queue.
1253 @param[in, out] Nbuf Pointer to the net buffer to be appended.
1259 IN OUT NET_BUF_QUEUE
*NbufQue
,
1260 IN OUT NET_BUF
*Nbuf
1264 Copy Len bytes of data from the net buffer queue at the specific offset to the
1267 The copying operation is the same as NetbufCopy but applies to the net buffer
1268 queue instead of the net buffer.
1270 @param[in] NbufQue Pointer to the net buffer queue.
1271 @param[in] Offset The sequence number of the first byte to copy.
1272 @param[in] Len Length of the data to copy.
1273 @param[out] Dest The destination of the data to copy to.
1275 @return The length of the actual copied data, or 0 if the offset
1276 specified exceeds the total size of net buffer queue.
1282 IN NET_BUF_QUEUE
*NbufQue
,
1289 Trim Len bytes of data from the queue header, release any of the net buffer
1290 whom is trimmed wholely.
1292 The trimming operation is the same as NetbufTrim but applies to the net buffer
1293 queue instead of the net buffer.
1295 @param[in, out] NbufQue Pointer to the net buffer queue.
1296 @param[in] Len Length of the data to trim.
1298 @return The actual length of the data trimmed.
1304 IN OUT NET_BUF_QUEUE
*NbufQue
,
1310 Flush the net buffer queue.
1312 @param[in, out] NbufQue Pointer to the queue to be flushed.
1318 IN OUT NET_BUF_QUEUE
*NbufQue
1322 Compute the checksum for a bulk of data.
1324 @param[in] Bulk Pointer to the data.
1325 @param[in] Len Length of the data, in bytes.
1327 @return The computed checksum.
1340 @param[in] Checksum1 The first checksum to be added.
1341 @param[in] Checksum2 The second checksum to be added.
1343 @return The new checksum.
1349 IN UINT16 Checksum1
,
1354 Compute the checksum for a NET_BUF.
1356 @param[in] Nbuf Pointer to the net buffer.
1358 @return The computed checksum.
1368 Compute the checksum for TCP/UDP pseudo header.
1370 Src and Dst are in network byte order, and Len is in host byte order.
1372 @param[in] Src The source address of the packet.
1373 @param[in] Dst The destination address of the packet.
1374 @param[in] Proto The protocol type of the packet.
1375 @param[in] Len The length of the packet.
1377 @return The computed checksum.
1382 NetPseudoHeadChecksum (