]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Include/Library/NetLib.h
temporary roll back changing on gEfiHotPlugDevice.
[mirror_edk2.git] / MdeModulePkg / Include / Library / NetLib.h
CommitLineData
97b38d4e 1/** @file\r
2 This library provides basic function for UEFI network stack.\r
3\r
4Copyright (c) 2005 - 2008, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#ifndef _NET_LIB_H_\r
16#define _NET_LIB_H_\r
17\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20#include <Protocol/DriverBinding.h>\r
21#include <Protocol/ComponentName.h>\r
22#include <Protocol/DriverConfiguration.h>\r
23#include <Protocol/DriverDiagnostics.h>\r
24#include <Protocol/Dpc.h>\r
25\r
26typedef UINT32 IP4_ADDR;\r
27typedef UINT32 TCP_SEQNO;\r
28typedef UINT16 TCP_PORTNO;\r
29\r
30typedef enum {\r
31 NET_ETHER_ADDR_LEN = 6,\r
32 NET_IFTYPE_ETHERNET = 0x01,\r
33\r
34 EFI_IP_PROTO_UDP = 0x11,\r
35 EFI_IP_PROTO_TCP = 0x06,\r
36 EFI_IP_PROTO_ICMP = 0x01,\r
37\r
38 //\r
d7db0902 39 // The address classification\r
97b38d4e 40 //\r
41 IP4_ADDR_CLASSA = 1,\r
42 IP4_ADDR_CLASSB,\r
43 IP4_ADDR_CLASSC,\r
44 IP4_ADDR_CLASSD,\r
45 IP4_ADDR_CLASSE,\r
46\r
47 IP4_MASK_NUM = 33\r
48} IP4_CLASS_TYPE;\r
49\r
50#pragma pack(1)\r
51\r
52//\r
53// Ethernet head definition\r
54//\r
55typedef struct {\r
56 UINT8 DstMac [NET_ETHER_ADDR_LEN];\r
57 UINT8 SrcMac [NET_ETHER_ADDR_LEN];\r
58 UINT16 EtherType;\r
59} ETHER_HEAD;\r
60\r
61\r
62//\r
63// The EFI_IP4_HEADER is hard to use because the source and\r
64// destination address are defined as EFI_IPv4_ADDRESS, which\r
65// is a structure. Two structures can't be compared or masked\r
66// directly. This is why there is an internal representation.\r
67//\r
68typedef struct {\r
69 UINT8 HeadLen : 4;\r
70 UINT8 Ver : 4;\r
71 UINT8 Tos;\r
72 UINT16 TotalLen;\r
73 UINT16 Id;\r
74 UINT16 Fragment;\r
75 UINT8 Ttl;\r
76 UINT8 Protocol;\r
77 UINT16 Checksum;\r
78 IP4_ADDR Src;\r
79 IP4_ADDR Dst;\r
80} IP4_HEAD;\r
81\r
82\r
83//\r
84// ICMP head definition. ICMP message is categoried as either an error\r
85// message or query message. Two message types have their own head format.\r
86//\r
87typedef struct {\r
88 UINT8 Type;\r
89 UINT8 Code;\r
90 UINT16 Checksum;\r
91} IP4_ICMP_HEAD;\r
92\r
93typedef struct {\r
94 IP4_ICMP_HEAD Head;\r
95 UINT32 Fourth; // 4th filed of the head, it depends on Type.\r
96 IP4_HEAD IpHead;\r
97} IP4_ICMP_ERROR_HEAD;\r
98\r
99typedef struct {\r
100 IP4_ICMP_HEAD Head;\r
101 UINT16 Id;\r
102 UINT16 Seq;\r
103} IP4_ICMP_QUERY_HEAD;\r
104\r
105\r
106//\r
107// UDP header definition\r
108//\r
109typedef struct {\r
110 UINT16 SrcPort;\r
111 UINT16 DstPort;\r
112 UINT16 Length;\r
113 UINT16 Checksum;\r
114} EFI_UDP4_HEADER;\r
115\r
116\r
117//\r
118// TCP header definition\r
119//\r
120typedef struct {\r
121 TCP_PORTNO SrcPort;\r
122 TCP_PORTNO DstPort;\r
123 TCP_SEQNO Seq;\r
124 TCP_SEQNO Ack;\r
125 UINT8 Res : 4;\r
126 UINT8 HeadLen : 4;\r
127 UINT8 Flag;\r
128 UINT16 Wnd;\r
129 UINT16 Checksum;\r
130 UINT16 Urg;\r
131} TCP_HEAD;\r
132\r
133#pragma pack()\r
134\r
135#define NET_MAC_EQUAL(pMac1, pMac2, Len) \\r
136 (CompareMem ((pMac1), (pMac2), Len) == 0)\r
137\r
138#define NET_MAC_IS_MULTICAST(Mac, BMac, Len) \\r
139 (((*((UINT8 *) Mac) & 0x01) == 0x01) && (!NET_MAC_EQUAL (Mac, BMac, Len)))\r
140\r
141#define NTOHL(x) (UINT32)((((UINT32) (x) & 0xff) << 24) | \\r
142 (((UINT32) (x) & 0xff00) << 8) | \\r
143 (((UINT32) (x) & 0xff0000) >> 8) | \\r
144 (((UINT32) (x) & 0xff000000) >> 24))\r
145\r
146#define HTONL(x) NTOHL(x)\r
147\r
148#define NTOHS(x) (UINT16)((((UINT16) (x) & 0xff) << 8) | \\r
149 (((UINT16) (x) & 0xff00) >> 8))\r
150\r
151#define HTONS(x) NTOHS(x)\r
152\r
153//\r
154// Test the IP's attribute, All the IPs are in host byte order.\r
155//\r
156#define IP4_IS_MULTICAST(Ip) (((Ip) & 0xF0000000) == 0xE0000000)\r
157#define IP4_IS_LOCAL_BROADCAST(Ip) ((Ip) == 0xFFFFFFFF)\r
158#define IP4_NET_EQUAL(Ip1, Ip2, NetMask) (((Ip1) & (NetMask)) == ((Ip2) & (NetMask)))\r
159#define IP4_IS_VALID_NETMASK(Ip) (NetGetMaskLength (Ip) != IP4_MASK_NUM)\r
160\r
161//\r
162// Convert the EFI_IP4_ADDRESS to plain UINT32 IP4 address.\r
163//\r
164#define EFI_IP4(EfiIpAddr) (*(IP4_ADDR *) ((EfiIpAddr).Addr))\r
165#define EFI_NTOHL(EfiIp) (NTOHL (EFI_IP4 ((EfiIp))))\r
166#define EFI_IP4_EQUAL(Ip1, Ip2) (CompareMem ((Ip1), (Ip2), sizeof (EFI_IPv4_ADDRESS)) == 0)\r
167\r
168/**\r
3a1ab4bc 169 Return the length of the mask. \r
170 \r
171 Return the length of the mask, the correct value is from 0 to 32.\r
172 If the mask is invalid, return the invalid length 33, which is IP4_MASK_NUM.\r
97b38d4e 173 NetMask is in the host byte order.\r
174\r
ae213b7d 175 @param[in] NetMask The netmask to get the length from.\r
97b38d4e 176\r
3a1ab4bc 177 @return The length of the netmask, IP4_MASK_NUM if the mask is invalid.\r
ae213b7d 178 \r
97b38d4e 179**/\r
180INTN\r
181EFIAPI\r
182NetGetMaskLength (\r
ae213b7d 183 IN IP4_ADDR NetMask\r
97b38d4e 184 );\r
185\r
186/**\r
3a1ab4bc 187 Return the class of the IP address, such as class A, B, C.\r
97b38d4e 188 Addr is in host byte order.\r
3a1ab4bc 189 \r
190 The address of class A starts with 0.\r
191 If the address belong to class A, return IP4_ADDR_CLASSA.\r
192 The address of class B starts with 10. \r
193 If the address belong to class B, return IP4_ADDR_CLASSB.\r
194 The address of class C starts with 110. \r
195 If the address belong to class C, return IP4_ADDR_CLASSC.\r
196 The address of class D starts with 1110. \r
197 If the address belong to class D, return IP4_ADDR_CLASSD.\r
198 The address of class E starts with 1111.\r
199 If the address belong to class E, return IP4_ADDR_CLASSE.\r
97b38d4e 200\r
3a1ab4bc 201 \r
ae213b7d 202 @param[in] Addr The address to get the class from.\r
97b38d4e 203\r
ae213b7d 204 @return IP address class, such as IP4_ADDR_CLASSA.\r
97b38d4e 205\r
206**/\r
207INTN\r
208EFIAPI\r
209NetGetIpClass (\r
210 IN IP4_ADDR Addr\r
211 );\r
212\r
213/**\r
214 Check whether the IP is a valid unicast address according to\r
3a1ab4bc 215 the netmask. If NetMask is zero, use the IP address's class to get the default mask.\r
216 \r
217 If Ip is 0, IP is not a valid unicast address.\r
218 Class D address is used for multicasting and class E address is reserved for future. If Ip\r
219 belongs to class D or class E, IP is not a valid unicast address. \r
220 If all bits of the host address of IP are 0 or 1, IP is also not a valid unicast address.\r
97b38d4e 221\r
ae213b7d 222 @param[in] Ip The IP to check against.\r
223 @param[in] NetMask The mask of the IP.\r
97b38d4e 224\r
ae213b7d 225 @return TRUE if IP is a valid unicast address on the network, otherwise FALSE.\r
97b38d4e 226\r
227**/\r
228BOOLEAN\r
ae213b7d 229EFIAPI\r
97b38d4e 230Ip4IsUnicast (\r
231 IN IP4_ADDR Ip,\r
232 IN IP4_ADDR NetMask\r
233 );\r
234\r
235extern IP4_ADDR gIp4AllMasks [IP4_MASK_NUM];\r
236\r
237\r
238extern EFI_IPv4_ADDRESS mZeroIp4Addr;\r
239\r
240#define NET_IS_DIGIT(Ch) (('0' <= (Ch)) && ((Ch) <= '9'))\r
241#define NET_ROUNDUP(size, unit) (((size) + (unit) - 1) & (~((unit) - 1)))\r
242#define NET_IS_LOWER_CASE_CHAR(Ch) (('a' <= (Ch)) && ((Ch) <= 'z'))\r
243#define NET_IS_UPPER_CASE_CHAR(Ch) (('A' <= (Ch)) && ((Ch) <= 'Z'))\r
244\r
245#define TICKS_PER_MS 10000U\r
246#define TICKS_PER_SECOND 10000000U\r
247\r
248#define NET_RANDOM(Seed) ((UINT32) ((UINT32) (Seed) * 1103515245UL + 12345) % 4294967295UL)\r
249\r
250/**\r
3a1ab4bc 251 Extract a UINT32 from a byte stream.\r
252 \r
253 Copy a UINT32 from a byte stream, then converts it from Network \r
254 byte order to host byte order. Use this function to avoid alignment error.\r
97b38d4e 255\r
ae213b7d 256 @param[in] Buf The buffer to extract the UINT32.\r
97b38d4e 257\r
258 @return The UINT32 extracted.\r
259\r
260**/\r
261UINT32\r
262EFIAPI\r
263NetGetUint32 (\r
264 IN UINT8 *Buf\r
265 );\r
266\r
267/**\r
3a1ab4bc 268 Put a UINT32 to the byte stream in network byte order. \r
269 \r
270 Converts a UINT32 from host byte order to network byte order. Then copy it to the \r
271 byte stream.\r
97b38d4e 272\r
ae213b7d 273 @param[in, out] Buf The buffer to put the UINT32.\r
274 @param[in] Data The data to put.\r
275 \r
97b38d4e 276**/\r
277VOID\r
278EFIAPI\r
279NetPutUint32 (\r
ae213b7d 280 IN OUT UINT8 *Buf,\r
281 IN UINT32 Data\r
97b38d4e 282 );\r
283\r
284/**\r
285 Initialize a random seed using current time.\r
3a1ab4bc 286 \r
287 Get current time first. Then initialize a random seed based on some basic \r
288 mathematics operation on the hour, day, minute, second, nanosecond and year \r
289 of the current time.\r
290 \r
97b38d4e 291 @return The random seed initialized with current time.\r
292\r
293**/\r
294UINT32\r
295EFIAPI\r
296NetRandomInitSeed (\r
297 VOID\r
298 );\r
299\r
300\r
301#define NET_LIST_USER_STRUCT(Entry, Type, Field) \\r
50d7ebad 302 BASE_CR(Entry, Type, Field)\r
97b38d4e 303\r
304#define NET_LIST_USER_STRUCT_S(Entry, Type, Field, Sig) \\r
305 CR(Entry, Type, Field, Sig)\r
306\r
307//\r
308// Iterate through the doule linked list. It is NOT delete safe\r
309//\r
310#define NET_LIST_FOR_EACH(Entry, ListHead) \\r
311 for(Entry = (ListHead)->ForwardLink; Entry != (ListHead); Entry = Entry->ForwardLink)\r
312\r
313//\r
314// Iterate through the doule linked list. This is delete-safe.\r
315// Don't touch NextEntry. Also, don't use this macro if list\r
316// entries other than the Entry may be deleted when processing\r
317// the current Entry.\r
318//\r
319#define NET_LIST_FOR_EACH_SAFE(Entry, NextEntry, ListHead) \\r
320 for(Entry = (ListHead)->ForwardLink, NextEntry = Entry->ForwardLink; \\r
321 Entry != (ListHead); \\r
322 Entry = NextEntry, NextEntry = Entry->ForwardLink \\r
323 )\r
324\r
325//\r
326// Make sure the list isn't empty before get the frist/last record.\r
327//\r
328#define NET_LIST_HEAD(ListHead, Type, Field) \\r
329 NET_LIST_USER_STRUCT((ListHead)->ForwardLink, Type, Field)\r
330\r
331#define NET_LIST_TAIL(ListHead, Type, Field) \\r
332 NET_LIST_USER_STRUCT((ListHead)->BackLink, Type, Field)\r
333\r
334\r
335/**\r
3a1ab4bc 336 Remove the first node entry on the list, and return the removed node entry.\r
337 \r
338 Removes the first node Entry from a doubly linked list. It is up to the caller of\r
339 this function to release the memory used by the first node if that is required. On\r
340 exit, the removed node is returned. \r
341\r
342 If Head is NULL, then ASSERT().\r
343 If Head was not initialized, then ASSERT().\r
344 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
345 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
346 then ASSERT(). \r
97b38d4e 347\r
ae213b7d 348 @param[in, out] Head The list header.\r
97b38d4e 349\r
3a1ab4bc 350 @return The first node entry that is removed from the list, NULL if the list is empty.\r
97b38d4e 351\r
352**/\r
353LIST_ENTRY *\r
354EFIAPI\r
355NetListRemoveHead (\r
ae213b7d 356 IN OUT LIST_ENTRY *Head\r
97b38d4e 357 );\r
358\r
359/**\r
3a1ab4bc 360 Remove the last node entry on the list and and return the removed node entry.\r
361\r
362 Removes the last node entry from a doubly linked list. It is up to the caller of\r
363 this function to release the memory used by the first node if that is required. On\r
364 exit, the removed node is returned. \r
97b38d4e 365\r
3a1ab4bc 366 If Head is NULL, then ASSERT().\r
367 If Head was not initialized, then ASSERT().\r
368 If PcdMaximumLinkedListLength is not zero, and the number of nodes in the\r
369 linked list including the head node is greater than or equal to PcdMaximumLinkedListLength,\r
370 then ASSERT(). \r
371 \r
ae213b7d 372 @param[in, out] Head The list head.\r
97b38d4e 373\r
3a1ab4bc 374 @return The last node entry that is removed from the list, NULL if the list is empty.\r
97b38d4e 375\r
376**/\r
377LIST_ENTRY *\r
378EFIAPI\r
379NetListRemoveTail (\r
ae213b7d 380 IN OUT LIST_ENTRY *Head\r
97b38d4e 381 );\r
382\r
383/**\r
3a1ab4bc 384 Insert a new node entry after a designated node entry of a doubly linked list.\r
385 \r
386 Inserts a new node entry donated by NewEntry after the node entry donated by PrevEntry\r
387 of the doubly linked list.\r
388 \r
ae213b7d 389 @param[in, out] PrevEntry The previous entry to insert after.\r
390 @param[in, out] NewEntry The new entry to insert.\r
97b38d4e 391\r
392**/\r
393VOID\r
394EFIAPI\r
395NetListInsertAfter (\r
ae213b7d 396 IN OUT LIST_ENTRY *PrevEntry,\r
397 IN OUT LIST_ENTRY *NewEntry\r
97b38d4e 398 );\r
399\r
400/**\r
3a1ab4bc 401 Insert a new node entry before a designated node entry of a doubly linked list.\r
402 \r
403 Inserts a new node entry donated by NewEntry after the node entry donated by PostEntry\r
404 of the doubly linked list.\r
405 \r
ae213b7d 406 @param[in, out] PostEntry The entry to insert before.\r
407 @param[in, out] NewEntry The new entry to insert.\r
97b38d4e 408\r
409**/\r
410VOID\r
411EFIAPI\r
412NetListInsertBefore (\r
ae213b7d 413 IN OUT LIST_ENTRY *PostEntry,\r
414 IN OUT LIST_ENTRY *NewEntry\r
97b38d4e 415 );\r
416\r
417\r
418//\r
419// Object container: EFI network stack spec defines various kinds of\r
420// tokens. The drivers can share code to manage those objects.\r
421//\r
422typedef struct {\r
423 LIST_ENTRY Link;\r
424 VOID *Key;\r
425 VOID *Value;\r
426} NET_MAP_ITEM;\r
427\r
428typedef struct {\r
429 LIST_ENTRY Used;\r
430 LIST_ENTRY Recycled;\r
431 UINTN Count;\r
432} NET_MAP;\r
433\r
434#define NET_MAP_INCREAMENT 64\r
435\r
436/**\r
437 Initialize the netmap. Netmap is a reposity to keep the <Key, Value> pairs.\r
3a1ab4bc 438 \r
439 Initialize the forward and backward links of two head nodes donated by Map->Used \r
440 and Map->Recycled of two doubly linked lists.\r
441 Initializes the count of the <Key, Value> pairs in the netmap to zero.\r
442 \r
443 If Map is NULL, then ASSERT().\r
444 If the address of Map->Used is NULL, then ASSERT().\r
445 If the address of Map->Recycled is NULl, then ASSERT().\r
446 \r
ae213b7d 447 @param[in, out] Map The netmap to initialize.\r
97b38d4e 448\r
449**/\r
450VOID\r
451EFIAPI\r
452NetMapInit (\r
ae213b7d 453 IN OUT NET_MAP *Map\r
97b38d4e 454 );\r
455\r
456/**\r
457 To clean up the netmap, that is, release allocated memories.\r
3a1ab4bc 458 \r
459 Removes all nodes of the Used doubly linked list and free memory of all related netmap items.\r
460 Removes all nodes of the Recycled doubly linked list and free memory of all related netmap items.\r
461 The number of the <Key, Value> pairs in the netmap is set to be zero.\r
462 \r
463 If Map is NULL, then ASSERT().\r
464 \r
ae213b7d 465 @param[in, out] Map The netmap to clean up.\r
97b38d4e 466\r
467**/\r
468VOID\r
469EFIAPI\r
470NetMapClean (\r
ae213b7d 471 IN OUT NET_MAP *Map\r
97b38d4e 472 );\r
473\r
474/**\r
3a1ab4bc 475 Test whether the netmap is empty and return true if it is.\r
476 \r
477 If the number of the <Key, Value> pairs in the netmap is zero, return TRUE.\r
478 \r
479 If Map is NULL, then ASSERT().\r
480 \r
481 \r
ae213b7d 482 @param[in] Map The net map to test.\r
97b38d4e 483\r
484 @return TRUE if the netmap is empty, otherwise FALSE.\r
485\r
486**/\r
487BOOLEAN\r
488EFIAPI\r
489NetMapIsEmpty (\r
490 IN NET_MAP *Map\r
491 );\r
492\r
493/**\r
494 Return the number of the <Key, Value> pairs in the netmap.\r
495\r
ae213b7d 496 @param[in] Map The netmap to get the entry number.\r
97b38d4e 497\r
498 @return The entry number in the netmap.\r
499\r
500**/\r
501UINTN\r
502EFIAPI\r
503NetMapGetCount (\r
504 IN NET_MAP *Map\r
505 );\r
506\r
507/**\r
508 Allocate an item to save the <Key, Value> pair to the head of the netmap.\r
3a1ab4bc 509 \r
510 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
511 to the beginning of the Used doubly linked list. The number of the <Key, Value> \r
512 pairs in the netmap increase by 1.\r
97b38d4e 513\r
3a1ab4bc 514 If Map is NULL, then ASSERT().\r
515 \r
ae213b7d 516 @param[in, out] Map The netmap to insert into.\r
517 @param[in] Key The user's key.\r
518 @param[in] Value The user's value for the key.\r
97b38d4e 519\r
ae213b7d 520 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
521 @retval EFI_SUCCESS The item is inserted to the head.\r
97b38d4e 522\r
523**/\r
524EFI_STATUS\r
525EFIAPI\r
526NetMapInsertHead (\r
ae213b7d 527 IN OUT NET_MAP *Map,\r
97b38d4e 528 IN VOID *Key,\r
529 IN VOID *Value OPTIONAL\r
530 );\r
531\r
532/**\r
533 Allocate an item to save the <Key, Value> pair to the tail of the netmap.\r
534\r
3a1ab4bc 535 Allocate an item to save the <Key, Value> pair and add corresponding node entry\r
536 to the tail of the Used doubly linked list. The number of the <Key, Value> \r
537 pairs in the netmap increase by 1.\r
538\r
539 If Map is NULL, then ASSERT().\r
540 \r
ae213b7d 541 @param[in, out] Map The netmap to insert into.\r
542 @param[in] Key The user's key.\r
543 @param[in] Value The user's value for the key.\r
97b38d4e 544\r
ae213b7d 545 @retval EFI_OUT_OF_RESOURCES Failed to allocate the memory for the item.\r
546 @retval EFI_SUCCESS The item is inserted to the tail.\r
97b38d4e 547\r
548**/\r
549EFI_STATUS\r
550EFIAPI\r
551NetMapInsertTail (\r
ae213b7d 552 IN OUT NET_MAP *Map,\r
97b38d4e 553 IN VOID *Key,\r
554 IN VOID *Value OPTIONAL\r
555 );\r
556\r
557/**\r
3a1ab4bc 558 Find the key in the netmap and returns the point to the item contains the Key.\r
559 \r
560 Iterate the Used doubly linked list of the netmap to get every item. Compare the key of every \r
561 item with the key to search. It returns the point to the item contains the Key if found.\r
97b38d4e 562\r
3a1ab4bc 563 If Map is NULL, then ASSERT().\r
564 \r
ae213b7d 565 @param[in] Map The netmap to search within.\r
566 @param[in] Key The key to search.\r
97b38d4e 567\r
568 @return The point to the item contains the Key, or NULL if Key isn't in the map.\r
569\r
570**/\r
ae213b7d 571NET_MAP_ITEM *\r
97b38d4e 572EFIAPI\r
573NetMapFindKey (\r
574 IN NET_MAP *Map,\r
575 IN VOID *Key\r
576 );\r
577\r
578/**\r
3a1ab4bc 579 Remove the node entry of the item from the netmap and return the key of the removed item.\r
580 \r
581 Remove the node entry of the item from the Used doubly linked list of the netmap. \r
582 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
583 entry of the item to the Recycled doubly linked list of the netmap. If Value is not NULL,\r
584 Value will point to the value of the item. It returns the key of the removed item.\r
585 \r
586 If Map is NULL, then ASSERT().\r
587 If Item is NULL, then ASSERT().\r
588 if item in not in the netmap, then ASSERT().\r
589 \r
ae213b7d 590 @param[in, out] Map The netmap to remove the item from.\r
591 @param[in, out] Item The item to remove.\r
592 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 593\r
ae213b7d 594 @return The key of the removed item.\r
97b38d4e 595\r
596**/\r
597VOID *\r
598EFIAPI\r
599NetMapRemoveItem (\r
ae213b7d 600 IN OUT NET_MAP *Map,\r
601 IN OUT NET_MAP_ITEM *Item,\r
602 OUT VOID **Value OPTIONAL\r
97b38d4e 603 );\r
604\r
605/**\r
3a1ab4bc 606 Remove the first node entry on the netmap and return the key of the removed item.\r
97b38d4e 607\r
3a1ab4bc 608 Remove the first node entry from the Used doubly linked list of the netmap. \r
609 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
610 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
611 parameter Value will point to the value of the item. It returns the key of the removed item.\r
612 \r
613 If Map is NULL, then ASSERT().\r
614 If the Used doubly linked list is empty, then ASSERT().\r
615 \r
ae213b7d 616 @param[in, out] Map The netmap to remove the head from.\r
617 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 618\r
ae213b7d 619 @return The key of the item removed.\r
97b38d4e 620\r
621**/\r
622VOID *\r
623EFIAPI\r
624NetMapRemoveHead (\r
ae213b7d 625 IN OUT NET_MAP *Map,\r
626 OUT VOID **Value OPTIONAL\r
97b38d4e 627 );\r
628\r
629/**\r
3a1ab4bc 630 Remove the last node entry on the netmap and return the key of the removed item.\r
97b38d4e 631\r
3a1ab4bc 632 Remove the last node entry from the Used doubly linked list of the netmap. \r
633 The number of the <Key, Value> pairs in the netmap decrease by 1. Then add the node \r
634 entry to the Recycled doubly linked list of the netmap. If parameter Value is not NULL,\r
635 parameter Value will point to the value of the item. It returns the key of the removed item.\r
636 \r
637 If Map is NULL, then ASSERT().\r
638 If the Used doubly linked list is empty, then ASSERT().\r
639 \r
ae213b7d 640 @param[in, out] Map The netmap to remove the tail from.\r
641 @param[out] Value The variable to receive the value if not NULL.\r
97b38d4e 642\r
ae213b7d 643 @return The key of the item removed.\r
97b38d4e 644\r
645**/\r
646VOID *\r
647EFIAPI\r
648NetMapRemoveTail (\r
ae213b7d 649 IN OUT NET_MAP *Map,\r
650 OUT VOID **Value OPTIONAL\r
97b38d4e 651 );\r
652\r
653typedef\r
654EFI_STATUS\r
655(*NET_MAP_CALLBACK) (\r
656 IN NET_MAP *Map,\r
657 IN NET_MAP_ITEM *Item,\r
658 IN VOID *Arg\r
659 );\r
660\r
661/**\r
3a1ab4bc 662 Iterate through the netmap and call CallBack for each item.\r
663 \r
664 It will contiue the traverse if CallBack returns EFI_SUCCESS, otherwise, break\r
665 from the loop. It returns the CallBack's last return value. This function is \r
666 delete safe for the current item.\r
97b38d4e 667\r
3a1ab4bc 668 If Map is NULL, then ASSERT().\r
669 If CallBack is NULL, then ASSERT().\r
670 \r
ae213b7d 671 @param[in] Map The Map to iterate through.\r
672 @param[in] CallBack The callback function to call for each item.\r
673 @param[in] Arg The opaque parameter to the callback.\r
97b38d4e 674\r
ae213b7d 675 @retval EFI_SUCCESS There is no item in the netmap or CallBack for each item\r
676 return EFI_SUCCESS.\r
677 @retval Others It returns the CallBack's last return value.\r
97b38d4e 678\r
679**/\r
680EFI_STATUS\r
681EFIAPI\r
682NetMapIterate (\r
683 IN NET_MAP *Map,\r
684 IN NET_MAP_CALLBACK CallBack,\r
ae213b7d 685 IN VOID *Arg\r
97b38d4e 686 );\r
687\r
688\r
689//\r
690// Helper functions to implement driver binding and service binding protocols.\r
691//\r
692/**\r
693 Create a child of the service that is identified by ServiceBindingGuid.\r
3a1ab4bc 694 \r
695 Get the ServiceBinding Protocol first, then use it to create a child.\r
97b38d4e 696\r
3a1ab4bc 697 If ServiceBindingGuid is NULL, then ASSERT().\r
698 If ChildHandle is NULL, then ASSERT().\r
699 \r
ae213b7d 700 @param[in] Controller The controller which has the service installed.\r
701 @param[in] Image The image handle used to open service.\r
702 @param[in] ServiceBindingGuid The service's Guid.\r
3a1ab4bc 703 @param[in, out] ChildHandle The handle to receive the create child.\r
97b38d4e 704\r
705 @retval EFI_SUCCESS The child is successfully created.\r
706 @retval Others Failed to create the child.\r
707\r
708**/\r
709EFI_STATUS\r
710EFIAPI\r
711NetLibCreateServiceChild (\r
ae213b7d 712 IN EFI_HANDLE Controller,\r
713 IN EFI_HANDLE Image,\r
97b38d4e 714 IN EFI_GUID *ServiceBindingGuid,\r
ae213b7d 715 IN OUT EFI_HANDLE *ChildHandle\r
97b38d4e 716 );\r
717\r
718/**\r
719 Destory a child of the service that is identified by ServiceBindingGuid.\r
3a1ab4bc 720 \r
721 Get the ServiceBinding Protocol first, then use it to destroy a child.\r
722 \r
723 If ServiceBindingGuid is NULL, then ASSERT().\r
724 \r
ae213b7d 725 @param[in] Controller The controller which has the service installed.\r
726 @param[in] Image The image handle used to open service.\r
727 @param[in] ServiceBindingGuid The service's Guid.\r
3a1ab4bc 728 @param[in] ChildHandle The child to destory.\r
97b38d4e 729\r
730 @retval EFI_SUCCESS The child is successfully destoried.\r
731 @retval Others Failed to destory the child.\r
732\r
733**/\r
734EFI_STATUS\r
735EFIAPI\r
736NetLibDestroyServiceChild (\r
ae213b7d 737 IN EFI_HANDLE Controller,\r
738 IN EFI_HANDLE Image,\r
97b38d4e 739 IN EFI_GUID *ServiceBindingGuid,\r
740 IN EFI_HANDLE ChildHandle\r
741 );\r
742\r
743/**\r
744 Convert the mac address of the simple network protocol installed on\r
745 SnpHandle to a unicode string. Callers are responsible for freeing the\r
746 string storage.\r
747\r
3a1ab4bc 748 Get the mac address of the Simple Network protocol from the SnpHandle. Then convert\r
749 the mac address into a unicode string. It takes 2 unicode characters to represent \r
750 a 1 byte binary buffer. Plus one unicode character for the null-terminator.\r
751\r
752\r
ae213b7d 753 @param[in] SnpHandle The handle where the simple network protocol is\r
754 installed on.\r
755 @param[in] ImageHandle The image handle used to act as the agent handle to\r
756 get the simple network protocol.\r
757 @param[out] MacString The pointer to store the address of the string\r
758 representation of the mac address.\r
759 \r
760 @retval EFI_SUCCESS Convert the mac address a unicode string successfully.\r
97b38d4e 761 @retval EFI_OUT_OF_RESOURCES There are not enough memory resource.\r
ae213b7d 762 @retval Others Failed to open the simple network protocol.\r
97b38d4e 763\r
764**/\r
765EFI_STATUS\r
766EFIAPI\r
767NetLibGetMacString (\r
ae213b7d 768 IN EFI_HANDLE SnpHandle,\r
769 IN EFI_HANDLE ImageHandle,\r
770 OUT CHAR16 **MacString\r
97b38d4e 771 );\r
772\r
773/**\r
774 Create an IPv4 device path node.\r
3a1ab4bc 775 \r
776 The header type of IPv4 device path node is MESSAGING_DEVICE_PATH.\r
777 The header subtype of IPv4 device path node is MSG_IPv4_DP.\r
778 The length of the IPv4 device path node in bytes is 19.\r
779 Get other info from parameters to make up the whole IPv4 device path node.\r
97b38d4e 780\r
ae213b7d 781 @param[in, out] Node Pointer to the IPv4 device path node.\r
782 @param[in] Controller The handle where the NIC IP4 config protocol resides.\r
783 @param[in] LocalIp The local IPv4 address.\r
784 @param[in] LocalPort The local port.\r
785 @param[in] RemoteIp The remote IPv4 address.\r
786 @param[in] RemotePort The remote port.\r
787 @param[in] Protocol The protocol type in the IP header.\r
788 @param[in] UseDefaultAddress Whether this instance is using default address or not.\r
97b38d4e 789\r
97b38d4e 790**/\r
791VOID\r
792EFIAPI\r
793NetLibCreateIPv4DPathNode (\r
794 IN OUT IPv4_DEVICE_PATH *Node,\r
795 IN EFI_HANDLE Controller,\r
796 IN IP4_ADDR LocalIp,\r
797 IN UINT16 LocalPort,\r
798 IN IP4_ADDR RemoteIp,\r
799 IN UINT16 RemotePort,\r
800 IN UINT16 Protocol,\r
801 IN BOOLEAN UseDefaultAddress\r
802 );\r
803\r
804/**\r
805 Find the UNDI/SNP handle from controller and protocol GUID.\r
3a1ab4bc 806 \r
97b38d4e 807 For example, IP will open a MNP child to transmit/receive\r
808 packets, when MNP is stopped, IP should also be stopped. IP\r
809 needs to find its own private data which is related the IP's\r
810 service binding instance that is install on UNDI/SNP handle.\r
811 Now, the controller is either a MNP or ARP child handle. But\r
812 IP opens these handle BY_DRIVER, use that info, we can get the\r
813 UNDI/SNP handle.\r
814\r
ae213b7d 815 @param[in] Controller Then protocol handle to check.\r
816 @param[in] ProtocolGuid The protocol that is related with the handle.\r
97b38d4e 817\r
ae213b7d 818 @return The UNDI/SNP handle or NULL for errors.\r
97b38d4e 819\r
820**/\r
821EFI_HANDLE\r
822EFIAPI\r
823NetLibGetNicHandle (\r
824 IN EFI_HANDLE Controller,\r
825 IN EFI_GUID *ProtocolGuid\r
826 );\r
827\r
828/**\r
829 Add a Deferred Procedure Call to the end of the DPC queue.\r
830\r
ae213b7d 831 @param[in] DpcTpl The EFI_TPL that the DPC should be invoked.\r
832 @param[in] DpcProcedure Pointer to the DPC's function.\r
833 @param[in] DpcContext Pointer to the DPC's context. Passed to DpcProcedure\r
834 when DpcProcedure is invoked.\r
97b38d4e 835\r
836 @retval EFI_SUCCESS The DPC was queued.\r
ae213b7d 837 @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL, or DpcProcedure\r
838 is NULL.\r
97b38d4e 839 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
840 add the DPC to the queue.\r
841\r
842**/\r
843EFI_STATUS\r
844EFIAPI\r
845NetLibQueueDpc (\r
846 IN EFI_TPL DpcTpl,\r
847 IN EFI_DPC_PROCEDURE DpcProcedure,\r
848 IN VOID *DpcContext OPTIONAL\r
849 );\r
850\r
851/**\r
ae213b7d 852 Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl\r
853 value greater than or equal to the current TPL are invoked in the order that\r
854 they were queued. DPCs with higher DpcTpl values are invoked before DPCs with\r
855 lower DpcTpl values.\r
97b38d4e 856\r
857 @retval EFI_SUCCESS One or more DPCs were invoked.\r
858 @retval EFI_NOT_FOUND No DPCs were invoked.\r
859\r
860**/\r
861EFI_STATUS\r
862EFIAPI\r
863NetLibDispatchDpc (\r
864 VOID\r
865 );\r
866\r
867/**\r
868 This is the default unload handle for all the network drivers.\r
869\r
3a1ab4bc 870 Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
871 Uninstall all the protocols installed in the driver entry point.\r
872 \r
ae213b7d 873 @param[in] ImageHandle The drivers' driver image.\r
97b38d4e 874\r
875 @retval EFI_SUCCESS The image is unloaded.\r
876 @retval Others Failed to unload the image.\r
877\r
878**/\r
879EFI_STATUS\r
880EFIAPI\r
881NetLibDefaultUnload (\r
882 IN EFI_HANDLE ImageHandle\r
883 );\r
884\r
885typedef enum {\r
886 //\r
887 //Various signatures\r
888 //\r
f3f2e05d 889 NET_BUF_SIGNATURE = SIGNATURE_32 ('n', 'b', 'u', 'f'),\r
890 NET_VECTOR_SIGNATURE = SIGNATURE_32 ('n', 'v', 'e', 'c'),\r
891 NET_QUE_SIGNATURE = SIGNATURE_32 ('n', 'b', 'q', 'u'),\r
97b38d4e 892\r
893\r
894 NET_PROTO_DATA = 64, // Opaque buffer for protocols\r
895 NET_BUF_HEAD = 1, // Trim or allocate space from head\r
896 NET_BUF_TAIL = 0, // Trim or allocate space from tail\r
897 NET_VECTOR_OWN_FIRST = 0x01 // We allocated the 1st block in the vector\r
898} NET_SIGNATURE_TYPE;\r
899\r
900#define NET_CHECK_SIGNATURE(PData, SIGNATURE) \\r
901 ASSERT (((PData) != NULL) && ((PData)->Signature == (SIGNATURE)))\r
902\r
903#define NET_SWAP_SHORT(Value) \\r
904 ((((Value) & 0xff) << 8) | (((Value) >> 8) & 0xff))\r
905\r
906//\r
907// Single memory block in the vector.\r
908//\r
909typedef struct {\r
910 UINT32 Len; // The block's length\r
911 UINT8 *Bulk; // The block's Data\r
912} NET_BLOCK;\r
913\r
914typedef VOID (*NET_VECTOR_EXT_FREE) (VOID *Arg);\r
915\r
916//\r
917//NET_VECTOR contains several blocks to hold all packet's\r
918//fragments and other house-keeping stuff for sharing. It\r
919//doesn't specify the where actual packet fragment begins.\r
920//\r
921typedef struct {\r
922 UINT32 Signature;\r
923 INTN RefCnt; // Reference count to share NET_VECTOR.\r
924 NET_VECTOR_EXT_FREE Free; // external function to free NET_VECTOR\r
925 VOID *Arg; // opeque argument to Free\r
926 UINT32 Flag; // Flags, NET_VECTOR_OWN_FIRST\r
927 UINT32 Len; // Total length of the assocated BLOCKs\r
928\r
929 UINT32 BlockNum;\r
930 NET_BLOCK Block[1];\r
931} NET_VECTOR;\r
932\r
933//\r
934//NET_BLOCK_OP operate on the NET_BLOCK, It specifies\r
935//where the actual fragment begins and where it ends\r
936//\r
937typedef struct {\r
938 UINT8 *BlockHead; // Block's head, or the smallest valid Head\r
939 UINT8 *BlockTail; // Block's tail. BlockTail-BlockHead=block length\r
940 UINT8 *Head; // 1st byte of the data in the block\r
941 UINT8 *Tail; // Tail of the data in the block, Tail-Head=Size\r
942 UINT32 Size; // The size of the data\r
943} NET_BLOCK_OP;\r
944\r
945\r
946//\r
947//NET_BUF is the buffer manage structure used by the\r
948//network stack. Every network packet may be fragmented,\r
949//and contains multiple fragments. The Vector points to\r
950//memory blocks used by the each fragment, and BlockOp\r
951//specifies where each fragment begins and ends.\r
952//\r
953//It also contains a opaque area for protocol to store\r
954//per-packet informations. Protocol must be caution not\r
955//to overwrite the members after that.\r
956//\r
957typedef struct {\r
958 UINT32 Signature;\r
959 INTN RefCnt;\r
960 LIST_ENTRY List; // The List this NET_BUF is on\r
961\r
962 IP4_HEAD *Ip; // Network layer header, for fast access\r
963 TCP_HEAD *Tcp; // Transport layer header, for fast access\r
964 UINT8 ProtoData [NET_PROTO_DATA]; //Protocol specific data\r
965\r
966 NET_VECTOR *Vector; // The vector containing the packet\r
967\r
968 UINT32 BlockOpNum; // Total number of BlockOp in the buffer\r
969 UINT32 TotalSize; // Total size of the actual packet\r
970 NET_BLOCK_OP BlockOp[1]; // Specify the position of actual packet\r
971} NET_BUF;\r
972\r
973\r
974//\r
975//A queue of NET_BUFs, It is just a thin extension of\r
976//NET_BUF functions.\r
977//\r
978typedef struct {\r
979 UINT32 Signature;\r
980 INTN RefCnt;\r
981 LIST_ENTRY List; // The List this buffer queue is on\r
982\r
983 LIST_ENTRY BufList; // list of queued buffers\r
984 UINT32 BufSize; // total length of DATA in the buffers\r
985 UINT32 BufNum; // total number of buffers on the chain\r
986} NET_BUF_QUEUE;\r
987\r
988//\r
989// Pseudo header for TCP and UDP checksum\r
990//\r
991#pragma pack(1)\r
992typedef struct {\r
993 IP4_ADDR SrcIp;\r
994 IP4_ADDR DstIp;\r
995 UINT8 Reserved;\r
996 UINT8 Protocol;\r
997 UINT16 Len;\r
998} NET_PSEUDO_HDR;\r
999#pragma pack()\r
1000\r
1001//\r
1002// The fragment entry table used in network interfaces. This is\r
1003// the same as NET_BLOCK now. Use two different to distinguish\r
1004// the two in case that NET_BLOCK be enhanced later.\r
1005//\r
1006typedef struct {\r
1007 UINT32 Len;\r
1008 UINT8 *Bulk;\r
1009} NET_FRAGMENT;\r
1010\r
1011#define NET_GET_REF(PData) ((PData)->RefCnt++)\r
1012#define NET_PUT_REF(PData) ((PData)->RefCnt--)\r
50d7ebad 1013#define NETBUF_FROM_PROTODATA(Info) BASE_CR((Info), NET_BUF, ProtoData)\r
97b38d4e 1014\r
1015#define NET_BUF_SHARED(Buf) \\r
1016 (((Buf)->RefCnt > 1) || ((Buf)->Vector->RefCnt > 1))\r
1017\r
1018#define NET_VECTOR_SIZE(BlockNum) \\r
1019 (sizeof (NET_VECTOR) + ((BlockNum) - 1) * sizeof (NET_BLOCK))\r
1020\r
1021#define NET_BUF_SIZE(BlockOpNum) \\r
1022 (sizeof (NET_BUF) + ((BlockOpNum) - 1) * sizeof (NET_BLOCK_OP))\r
1023\r
1024#define NET_HEADSPACE(BlockOp) \\r
1025 (UINTN)((BlockOp)->Head - (BlockOp)->BlockHead)\r
1026\r
1027#define NET_TAILSPACE(BlockOp) \\r
1028 (UINTN)((BlockOp)->BlockTail - (BlockOp)->Tail)\r
1029\r
1030/**\r
1031 Allocate a single block NET_BUF. Upon allocation, all the\r
1032 free space is in the tail room.\r
1033\r
ae213b7d 1034 @param[in] Len The length of the block.\r
97b38d4e 1035\r
7557df4d 1036 @return Pointer to the allocated NET_BUF, or NULL if the \r
1037 allocation failed due to resource limit.\r
97b38d4e 1038\r
1039**/\r
1040NET_BUF *\r
1041EFIAPI\r
1042NetbufAlloc (\r
1043 IN UINT32 Len\r
1044 );\r
1045\r
1046/**\r
7557df4d 1047 Free the net buffer and its associated NET_VECTOR.\r
1048 \r
1049 Decrease the reference count of the net buffer by one. Free the associated net\r
1050 vector and itself if the reference count of the net buffer is decreased to 0. \r
1051 The net vector free operation just decrease the reference count of the net \r
1052 vector by one and do the real resource free operation when the reference count\r
1053 of the net vector is 0. \r
1054 \r
ae213b7d 1055 @param[in] Nbuf Pointer to the NET_BUF to be freed.\r
97b38d4e 1056\r
1057**/\r
1058VOID\r
1059EFIAPI\r
1060NetbufFree (\r
1061 IN NET_BUF *Nbuf\r
1062 );\r
1063\r
1064/**\r
7557df4d 1065 Get the index of NET_BLOCK_OP that contains the byte at Offset in the net \r
1066 buffer. \r
1067 \r
1068 This can be used to, for example, retrieve the IP header in the packet. It \r
1069 also can be used to get the fragment that contains the byte which is used \r
1070 mainly by the library implementation itself. \r
97b38d4e 1071\r
7557df4d 1072 @param[in] Nbuf Pointer to the net buffer.\r
1073 @param[in] Offset The offset of the byte.\r
1074 @param[out] Index Index of the NET_BLOCK_OP that contains the byte at \r
1075 Offset.\r
97b38d4e 1076\r
7557df4d 1077 @return Pointer to the Offset'th byte of data in the net buffer, or NULL\r
1078 if there is no such data in the net buffer.\r
97b38d4e 1079\r
1080**/\r
1081UINT8 *\r
1082EFIAPI\r
1083NetbufGetByte (\r
1084 IN NET_BUF *Nbuf,\r
1085 IN UINT32 Offset,\r
ae213b7d 1086 OUT UINT32 *Index OPTIONAL\r
97b38d4e 1087 );\r
1088\r
1089/**\r
7557df4d 1090 Create a copy of the net buffer that shares the associated net vector. \r
1091 \r
1092 The reference count of the newly created net buffer is set to 1. The reference \r
1093 count of the associated net vector is increased by one. \r
97b38d4e 1094\r
ae213b7d 1095 @param[in] Nbuf Pointer to the net buffer to be cloned.\r
97b38d4e 1096\r
7557df4d 1097 @return Pointer to the cloned net buffer, or NULL if the\r
ae213b7d 1098 allocation failed due to resource limit.\r
97b38d4e 1099\r
1100**/\r
7557df4d 1101NET_BUF *\r
97b38d4e 1102EFIAPI\r
1103NetbufClone (\r
1104 IN NET_BUF *Nbuf\r
1105 );\r
1106\r
1107/**\r
7557df4d 1108 Create a duplicated copy of the net buffer with data copied and HeadSpace\r
1109 bytes of head space reserved.\r
1110 \r
1111 The duplicated net buffer will allocate its own memory to hold the data of the\r
1112 source net buffer.\r
1113 \r
1114 @param[in] Nbuf Pointer to the net buffer to be duplicated from.\r
1115 @param[in, out] Duplicate Pointer to the net buffer to duplicate to, if \r
1116 NULL a new net buffer is allocated.\r
1117 @param[in] HeadSpace Length of the head space to reserve.\r
1118\r
1119 @return Pointer to the duplicated net buffer, or NULL if\r
1120 the allocation failed due to resource limit.\r
97b38d4e 1121\r
1122**/\r
1123NET_BUF *\r
1124EFIAPI\r
1125NetbufDuplicate (\r
1126 IN NET_BUF *Nbuf,\r
ae213b7d 1127 IN OUT NET_BUF *Duplicate OPTIONAL,\r
97b38d4e 1128 IN UINT32 HeadSpace\r
1129 );\r
1130\r
1131/**\r
7557df4d 1132 Create a NET_BUF structure which contains Len byte data of Nbuf starting from \r
1133 Offset. \r
1134 \r
1135 A new NET_BUF structure will be created but the associated data in NET_VECTOR \r
1136 is shared. This function exists to do IP packet fragmentation. \r
1137\r
1138 @param[in] Nbuf Pointer to the net buffer to be extracted.\r
1139 @param[in] Offset Starting point of the data to be included in the new \r
1140 net buffer.\r
1141 @param[in] Len Bytes of data to be included in the new net buffer. \r
1142 @param[in] HeadSpace Bytes of head space to reserve for protocol header. \r
1143\r
1144 @return Pointer to the cloned net buffer, or NULL if the \r
1145 allocation failed due to resource limit.\r
97b38d4e 1146\r
1147**/\r
1148NET_BUF *\r
1149EFIAPI\r
1150NetbufGetFragment (\r
1151 IN NET_BUF *Nbuf,\r
1152 IN UINT32 Offset,\r
1153 IN UINT32 Len,\r
1154 IN UINT32 HeadSpace\r
1155 );\r
1156\r
1157/**\r
7557df4d 1158 Reserve some space in the header room of the net buffer.\r
1159\r
1160 Upon allocation, all the space are in the tail room of the buffer. Call this \r
1161 function to move some space to the header room. This function is quite limited\r
1162 in that it can only reserve space from the first block of an empty NET_BUF not \r
1163 built from the external. But it should be enough for the network stack. \r
97b38d4e 1164\r
7557df4d 1165 @param[in, out] Nbuf Pointer to the net buffer.\r
1166 @param[in] Len The length of buffer to be reserved from the header.\r
97b38d4e 1167\r
1168**/\r
1169VOID\r
1170EFIAPI\r
1171NetbufReserve (\r
ae213b7d 1172 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1173 IN UINT32 Len\r
1174 );\r
1175\r
1176/**\r
7557df4d 1177 Allocate Len bytes of space from the header or tail of the buffer. \r
97b38d4e 1178\r
7557df4d 1179 @param[in, out] Nbuf Pointer to the net buffer.\r
1180 @param[in] Len The length of the buffer to be allocated.\r
1181 @param[in] FromHead The flag to indicate whether reserve the data \r
1182 from head (TRUE) or tail (FALSE).\r
97b38d4e 1183\r
7557df4d 1184 @return Pointer to the first byte of the allocated buffer, \r
1185 or NULL if there is no sufficient space.\r
97b38d4e 1186\r
1187**/\r
7557df4d 1188UINT8*\r
97b38d4e 1189EFIAPI\r
1190NetbufAllocSpace (\r
ae213b7d 1191 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1192 IN UINT32 Len,\r
1193 IN BOOLEAN FromHead\r
1194 );\r
1195\r
1196/**\r
7557df4d 1197 Trim Len bytes from the header or tail of the net buffer. \r
97b38d4e 1198\r
7557df4d 1199 @param[in, out] Nbuf Pointer to the net buffer.\r
1200 @param[in] Len The length of the data to be trimmed.\r
1201 @param[in] FromHead The flag to indicate whether trim data from head \r
1202 (TRUE) or tail (FALSE).\r
97b38d4e 1203\r
7557df4d 1204 @return Length of the actually trimmed data, which is possible to be less \r
1205 than Len because the TotalSize of Nbuf is less than Len.\r
97b38d4e 1206\r
1207**/\r
1208UINT32\r
1209EFIAPI\r
1210NetbufTrim (\r
ae213b7d 1211 IN OUT NET_BUF *Nbuf,\r
97b38d4e 1212 IN UINT32 Len,\r
1213 IN BOOLEAN FromHead\r
1214 );\r
1215\r
1216/**\r
7557df4d 1217 Copy Len bytes of data from the specific offset of the net buffer to the \r
1218 destination memory.\r
1219 \r
1220 The Len bytes of data may cross the several fragments of the net buffer.\r
1221 \r
1222 @param[in] Nbuf Pointer to the net buffer.\r
1223 @param[in] Offset The sequence number of the first byte to copy.\r
1224 @param[in] Len Length of the data to copy.\r
1225 @param[in] Dest The destination of the data to copy to.\r
1226\r
1227 @return The length of the actual copied data, or 0 if the offset\r
361468ed 1228 specified exceeds the total size of net buffer.\r
97b38d4e 1229\r
1230**/\r
1231UINT32\r
1232EFIAPI\r
1233NetbufCopy (\r
1234 IN NET_BUF *Nbuf,\r
1235 IN UINT32 Offset,\r
1236 IN UINT32 Len,\r
1237 IN UINT8 *Dest\r
1238 );\r
1239\r
1240/**\r
7557df4d 1241 Build a NET_BUF from external blocks. \r
1242 \r
1243 A new NET_BUF structure will be created from external blocks. Additional block\r
1244 of memory will be allocated to hold reserved HeadSpace bytes of header room\r
1245 and existing HeadLen bytes of header but the external blocks are shared by the\r
1246 net buffer to avoid data copying.\r
97b38d4e 1247\r
ae213b7d 1248 @param[in] ExtFragment Pointer to the data block.\r
7557df4d 1249 @param[in] ExtNum The number of the data blocks.\r
ae213b7d 1250 @param[in] HeadSpace The head space to be reserved.\r
1251 @param[in] HeadLen The length of the protocol header, This function\r
1252 will pull that number of data into a linear block.\r
1253 @param[in] ExtFree Pointer to the caller provided free function.\r
1254 @param[in] Arg The argument passed to ExtFree when ExtFree is\r
1255 called.\r
97b38d4e 1256\r
7557df4d 1257 @return Pointer to the net buffer built from the data blocks, \r
1258 or NULL if the allocation failed due to resource\r
1259 limit.\r
97b38d4e 1260\r
1261**/\r
1262NET_BUF *\r
1263EFIAPI\r
1264NetbufFromExt (\r
1265 IN NET_FRAGMENT *ExtFragment,\r
1266 IN UINT32 ExtNum,\r
1267 IN UINT32 HeadSpace,\r
1268 IN UINT32 HeadLen,\r
1269 IN NET_VECTOR_EXT_FREE ExtFree,\r
1270 IN VOID *Arg OPTIONAL\r
1271 );\r
1272\r
1273/**\r
7557df4d 1274 Build a fragment table to contain the fragments in the net buffer. This is the\r
1275 opposite operation of the NetbufFromExt. \r
1276 \r
ae213b7d 1277 @param[in] Nbuf Point to the net buffer.\r
1278 @param[in, out] ExtFragment Pointer to the data block.\r
7557df4d 1279 @param[in, out] ExtNum The number of the data blocks.\r
97b38d4e 1280\r
7557df4d 1281 @retval EFI_BUFFER_TOO_SMALL The number of non-empty block is bigger than \r
1282 ExtNum.\r
1283 @retval EFI_SUCCESS Fragment table is built successfully.\r
97b38d4e 1284\r
1285**/\r
1286EFI_STATUS\r
1287EFIAPI\r
1288NetbufBuildExt (\r
1289 IN NET_BUF *Nbuf,\r
ae213b7d 1290 IN OUT NET_FRAGMENT *ExtFragment,\r
1291 IN OUT UINT32 *ExtNum\r
97b38d4e 1292 );\r
1293\r
1294/**\r
7557df4d 1295 Build a net buffer from a list of net buffers.\r
1296 \r
1297 All the fragments will be collected from the list of NEW_BUF and then a new \r
1298 net buffer will be created through NetbufFromExt. \r
1299 \r
1300 @param[in] BufList A List of the net buffer.\r
1301 @param[in] HeadSpace The head space to be reserved.\r
1302 @param[in] HeaderLen The length of the protocol header, This function\r
1303 will pull that number of data into a linear block.\r
1304 @param[in] ExtFree Pointer to the caller provided free function.\r
1305 @param[in] Arg The argument passed to ExtFree when ExtFree is called.\r
1306\r
1307 @return Pointer to the net buffer built from the list of net \r
1308 buffers.\r
97b38d4e 1309\r
1310**/\r
1311NET_BUF *\r
1312EFIAPI\r
1313NetbufFromBufList (\r
1314 IN LIST_ENTRY *BufList,\r
1315 IN UINT32 HeadSpace,\r
1316 IN UINT32 HeaderLen,\r
1317 IN NET_VECTOR_EXT_FREE ExtFree,\r
ae213b7d 1318 IN VOID *Arg OPTIONAL\r
97b38d4e 1319 );\r
1320\r
1321/**\r
1322 Free a list of net buffers.\r
1323\r
ae213b7d 1324 @param[in, out] Head Pointer to the head of linked net buffers.\r
97b38d4e 1325\r
1326**/\r
1327VOID\r
1328EFIAPI\r
1329NetbufFreeList (\r
ae213b7d 1330 IN OUT LIST_ENTRY *Head\r
97b38d4e 1331 );\r
1332\r
1333/**\r
1334 Initiate the net buffer queue.\r
1335\r
7557df4d 1336 @param[in, out] NbufQue Pointer to the net buffer queue to be initialized.\r
97b38d4e 1337\r
1338**/\r
1339VOID\r
1340EFIAPI\r
1341NetbufQueInit (\r
ae213b7d 1342 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1343 );\r
1344\r
1345/**\r
7557df4d 1346 Allocate and initialize a net buffer queue.\r
97b38d4e 1347\r
7557df4d 1348 @return Pointer to the allocated net buffer queue, or NULL if the\r
1349 allocation failed due to resource limit.\r
97b38d4e 1350\r
1351**/\r
1352NET_BUF_QUEUE *\r
1353EFIAPI\r
1354NetbufQueAlloc (\r
1355 VOID\r
1356 );\r
1357\r
1358/**\r
7557df4d 1359 Free a net buffer queue. \r
1360 \r
1361 Decrease the reference count of the net buffer queue by one. The real resource\r
1362 free operation isn't performed until the reference count of the net buffer \r
1363 queue is decreased to 0.\r
97b38d4e 1364\r
7557df4d 1365 @param[in] NbufQue Pointer to the net buffer queue to be freed.\r
97b38d4e 1366\r
1367**/\r
1368VOID\r
1369EFIAPI\r
1370NetbufQueFree (\r
1371 IN NET_BUF_QUEUE *NbufQue\r
1372 );\r
1373\r
1374/**\r
7557df4d 1375 Remove a net buffer from the head in the specific queue and return it.\r
97b38d4e 1376\r
ae213b7d 1377 @param[in, out] NbufQue Pointer to the net buffer queue.\r
97b38d4e 1378\r
7557df4d 1379 @return Pointer to the net buffer removed from the specific queue, \r
1380 or NULL if there is no net buffer in the specific queue.\r
97b38d4e 1381\r
1382**/\r
1383NET_BUF *\r
1384EFIAPI\r
1385NetbufQueRemove (\r
ae213b7d 1386 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1387 );\r
1388\r
1389/**\r
7557df4d 1390 Append a net buffer to the net buffer queue.\r
97b38d4e 1391\r
7557df4d 1392 @param[in, out] NbufQue Pointer to the net buffer queue.\r
1393 @param[in, out] Nbuf Pointer to the net buffer to be appended.\r
97b38d4e 1394\r
1395**/\r
1396VOID\r
1397EFIAPI\r
1398NetbufQueAppend (\r
ae213b7d 1399 IN OUT NET_BUF_QUEUE *NbufQue,\r
1400 IN OUT NET_BUF *Nbuf\r
97b38d4e 1401 );\r
1402\r
1403/**\r
7557df4d 1404 Copy Len bytes of data from the net buffer queue at the specific offset to the\r
1405 destination memory.\r
1406 \r
1407 The copying operation is the same as NetbufCopy but applies to the net buffer\r
1408 queue instead of the net buffer.\r
1409 \r
1410 @param[in] NbufQue Pointer to the net buffer queue.\r
1411 @param[in] Offset The sequence number of the first byte to copy.\r
1412 @param[in] Len Length of the data to copy.\r
1413 @param[out] Dest The destination of the data to copy to.\r
1414\r
1415 @return The length of the actual copied data, or 0 if the offset \r
1416 specified exceeds the total size of net buffer queue.\r
97b38d4e 1417\r
1418**/\r
1419UINT32\r
1420EFIAPI\r
1421NetbufQueCopy (\r
1422 IN NET_BUF_QUEUE *NbufQue,\r
1423 IN UINT32 Offset,\r
1424 IN UINT32 Len,\r
ae213b7d 1425 OUT UINT8 *Dest\r
97b38d4e 1426 );\r
1427\r
1428/**\r
7557df4d 1429 Trim Len bytes of data from the queue header, release any of the net buffer \r
1430 whom is trimmed wholely.\r
1431 \r
1432 The trimming operation is the same as NetbufTrim but applies to the net buffer\r
1433 queue instead of the net buffer.\r
97b38d4e 1434\r
ae213b7d 1435 @param[in, out] NbufQue Pointer to the net buffer queue.\r
1436 @param[in] Len Length of the data to trim.\r
97b38d4e 1437\r
7557df4d 1438 @return The actual length of the data trimmed.\r
97b38d4e 1439\r
1440**/\r
1441UINT32\r
1442EFIAPI\r
1443NetbufQueTrim (\r
ae213b7d 1444 IN OUT NET_BUF_QUEUE *NbufQue,\r
97b38d4e 1445 IN UINT32 Len\r
1446 );\r
1447\r
1448\r
1449/**\r
1450 Flush the net buffer queue.\r
1451\r
ae213b7d 1452 @param[in, out] NbufQue Pointer to the queue to be flushed.\r
97b38d4e 1453\r
1454**/\r
1455VOID\r
1456EFIAPI\r
1457NetbufQueFlush (\r
ae213b7d 1458 IN OUT NET_BUF_QUEUE *NbufQue\r
97b38d4e 1459 );\r
1460\r
1461/**\r
7557df4d 1462 Compute the checksum for a bulk of data.\r
97b38d4e 1463\r
ae213b7d 1464 @param[in] Bulk Pointer to the data.\r
1465 @param[in] Len Length of the data, in bytes.\r
97b38d4e 1466\r
ae213b7d 1467 @return The computed checksum.\r
97b38d4e 1468\r
1469**/\r
1470UINT16\r
1471EFIAPI\r
1472NetblockChecksum (\r
1473 IN UINT8 *Bulk,\r
1474 IN UINT32 Len\r
1475 );\r
1476\r
1477/**\r
1478 Add two checksums.\r
1479\r
ae213b7d 1480 @param[in] Checksum1 The first checksum to be added.\r
1481 @param[in] Checksum2 The second checksum to be added.\r
97b38d4e 1482\r
ae213b7d 1483 @return The new checksum.\r
97b38d4e 1484\r
1485**/\r
1486UINT16\r
1487EFIAPI\r
1488NetAddChecksum (\r
1489 IN UINT16 Checksum1,\r
1490 IN UINT16 Checksum2\r
1491 );\r
1492\r
1493/**\r
1494 Compute the checksum for a NET_BUF.\r
1495\r
ae213b7d 1496 @param[in] Nbuf Pointer to the net buffer.\r
97b38d4e 1497\r
ae213b7d 1498 @return The computed checksum.\r
97b38d4e 1499\r
1500**/\r
1501UINT16\r
1502EFIAPI\r
1503NetbufChecksum (\r
1504 IN NET_BUF *Nbuf\r
1505 );\r
1506\r
1507/**\r
7557df4d 1508 Compute the checksum for TCP/UDP pseudo header. \r
1509 \r
1510 Src and Dst are in network byte order, and Len is in host byte order.\r
97b38d4e 1511\r
ae213b7d 1512 @param[in] Src The source address of the packet.\r
1513 @param[in] Dst The destination address of the packet.\r
1514 @param[in] Proto The protocol type of the packet.\r
1515 @param[in] Len The length of the packet.\r
97b38d4e 1516\r
ae213b7d 1517 @return The computed checksum.\r
97b38d4e 1518\r
1519**/\r
1520UINT16\r
1521EFIAPI\r
1522NetPseudoHeadChecksum (\r
1523 IN IP4_ADDR Src,\r
1524 IN IP4_ADDR Dst,\r
1525 IN UINT8 Proto,\r
1526 IN UINT16 Len\r
1527 );\r
1528\r
1529#endif\r