]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/Tcp4Dxe/Socket.h
1. Enable Network stack to pass SCT, currently MNP, ARP, IP4, TCP4 and DHCP4 have...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Tcp4Dxe / Socket.h
CommitLineData
8a67d61d 1/** @file
2
3Copyright (c) 2005 - 2006, Intel Corporation
4All rights reserved. This program and the accompanying materials
5are licensed and made available under the terms and conditions of the BSD License
6which accompanies this distribution. The full text of the license may be found at
7http://opensource.org/licenses/bsd-license.php
8
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12Module Name:
13
14 Socket.h
15
16Abstract:
17
18
19**/
20
21#ifndef _SOCKET_H_
22#define _SOCKET_H_
23
24#include <PiDxe.h>\r
25\r
26#include <Protocol/IP4.h>\r
27#include <Protocol/Tcp4.h>
28#include <Protocol/Udp4.h>\r
29
30#include <Library/NetLib.h>\r
31#include <Library/DebugLib.h>\r
32#include <Library/UefiRuntimeServicesTableLib.h>\r
33#include <Library/UefiDriverEntryPoint.h>\r
34#include <Library/UefiBootServicesTableLib.h>\r
35#include <Library/BaseLib.h>\r
36#include <Library/UefiLib.h>
37#include <Library/MemoryAllocationLib.h>
38#include <Library/BaseMemoryLib.h>
39
40#define SOCK_SND_BUF 0
41#define SOCK_RCV_BUF 1
42
43#define SOCK_BUFF_LOW_WATER 2 * 1024
44#define SOCK_RCV_BUFF_SIZE 8 * 1024
45#define SOCK_SND_BUFF_SIZE 8 * 1024
46#define SOCK_BACKLOG 5
47
48#define PROTO_RESERVED_LEN 20
49
50#define SO_NO_MORE_DATA 0x0001
51
52//
53//
54//
55// When a socket is created it enters into SO_UNCONFIGURED,
56// no actions can be taken on this socket, only after calling
57// SockConfigure. The state transition diagram of socket is
58// as following:
59//
60// SO_UNCONFIGURED --- SO_CONFIGURED --- SO_CONNECTING
61// ^ | |
62// | ---> SO_LISTENING |
63// | |
64// |------------------SO_DISCONNECTING<-- SO_CONNECTED
65//
66// A passive socket can only go into SO_LISTENING and
67// SO_UNCONFIGURED state. SO_XXXING state is a middle state
68// when a socket is undergoing a protocol procedure such
69// as requesting a TCP connection.
70//
71//
72//
73typedef enum {
74 SO_CLOSED = 0,
75 SO_LISTENING,
76 SO_CONNECTING,
77 SO_CONNECTED,
78 SO_DISCONNECTING
79} SOCK_STATE;
80
81typedef enum {
82 SO_UNCONFIGURED = 0,
83 SO_CONFIGURED_ACTIVE,
84 SO_CONFIGURED_PASSIVE,
85 SO_NO_MAPPING
86} SOCK_CONFIGURE_STATE;
87
88#define SOCK_NO_MORE_DATA(Sock) ((Sock)->Flag |= SO_NO_MORE_DATA)
89
90#define SOCK_IS_UNCONFIGURED(Sock) ((Sock)->ConfigureState == SO_UNCONFIGURED)
91
92#define SOCK_IS_CONFIGURED(Sock) \
93 (((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE) || \
94 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE))
95
96#define SOCK_IS_CONFIGURED_ACTIVE(Sock) \
97 ((Sock)->ConfigureState == SO_CONFIGURED_ACTIVE)
98
99#define SOCK_IS_CONNECTED_PASSIVE(Sock) \
100 ((Sock)->ConfigureState == SO_CONFIGURED_PASSIVE)
101
102#define SOCK_IS_NO_MAPPING(Sock) \
103 ((Sock)->ConfigureState == SO_NO_MAPPING)
104
105#define SOCK_IS_CLOSED(Sock) ((Sock)->State == SO_CLOSED)
106
107#define SOCK_IS_LISTENING(Sock) ((Sock)->State == SO_LISTENING)
108
109#define SOCK_IS_CONNECTING(Sock) ((Sock)->State == SO_CONNECTING)
110
111#define SOCK_IS_CONNECTED(Sock) ((Sock)->State == SO_CONNECTED)
112
113#define SOCK_IS_DISCONNECTING(Sock) ((Sock)->State == SO_DISCONNECTING)
114
115#define SOCK_IS_NO_MORE_DATA(Sock) (0 != ((Sock)->Flag & SO_NO_MORE_DATA))
116
117#define SOCK_SIGNATURE EFI_SIGNATURE_32 ('S', 'O', 'C', 'K')
118
119#define SOCK_FROM_THIS(a) CR ((a), SOCKET, NetProtocol, SOCK_SIGNATURE)
120
121#define SET_RCV_BUFFSIZE(Sock, Size) ((Sock)->RcvBuffer.HighWater = (Size))
122
123#define GET_RCV_BUFFSIZE(Sock) ((Sock)->RcvBuffer.HighWater)
124
125#define GET_RCV_DATASIZE(Sock) (((Sock)->RcvBuffer.DataQueue)->BufSize)
126
127#define SET_SND_BUFFSIZE(Sock, Size) ((Sock)->SndBuffer.HighWater = (Size))
128
129#define GET_SND_BUFFSIZE(Sock) ((Sock)->SndBuffer.HighWater)
130
131#define GET_SND_DATASIZE(Sock) (((Sock)->SndBuffer.DataQueue)->BufSize)
132
133#define SET_BACKLOG(Sock, Value) ((Sock)->BackLog = (Value))
134
135#define GET_BACKLOG(Sock) ((Sock)->BackLog)
136
137#define SOCK_ERROR(Sock, Error) ((Sock)->SockError = (Error))
138
139#define SND_BUF_HDR_LEN(Sock) \
140 ((SockBufFirst (&((Sock)->SndBuffer)))->TotalSize)
141
142#define RCV_BUF_HDR_LEN(Sock) \
143 ((SockBufFirst (&((Sock)->RcvBuffer)))->TotalSize)
144
145#define SOCK_FROM_TOKEN(Token) (((SOCK_TOKEN *) (Token))->Sock)
146
147#define PROTO_TOKEN_FORM_SOCK(SockToken, Type) \
148 ((Type *) (((SOCK_TOKEN *) (SockToken))->Token))
149
150typedef struct _SOCKET SOCKET;
151
152typedef struct _SOCK_COMPLETION_TOKEN {
153 EFI_EVENT Event;
154 EFI_STATUS Status;
155} SOCK_COMPLETION_TOKEN;
156
157typedef struct _SOCK_IO_TOKEN {
158 SOCK_COMPLETION_TOKEN Token;
159 union {
160 VOID *RxData;
161 VOID *TxData;
162 } Packet;
163} SOCK_IO_TOKEN;
164
165//
166// the request issued from socket layer to protocol layer
167//
168typedef enum {
169 SOCK_ATTACH, // attach current socket to a new PCB
170 SOCK_DETACH, // detach current socket from the PCB
171 SOCK_CONFIGURE, // configure attached PCB
172 SOCK_FLUSH, // flush attached PCB
173 SOCK_SND, // need protocol to send something
174 SOCK_SNDPUSH, // need protocol to send pushed data
175 SOCK_SNDURG, // need protocol to send urgent data
176 SOCK_CONSUMED, // application has retrieved data from socket
177 SOCK_CONNECT, // need to connect to a peer
178 SOCK_CLOSE, // need to close the protocol process
179 SOCK_ABORT, // need to reset the protocol process
180 SOCK_POLL, // need to poll to the protocol layer
181 SOCK_ROUTE, // need to add a route information
182 SOCK_MODE, // need to get the mode data of the protocol
183 SOCK_GROUP // need to join a mcast group
184} SOCK_REQUEST;
185
186//
187// the socket type
188//
189typedef enum {
190 SOCK_DGRAM, // this socket providing datagram service
191 SOCK_STREAM // this socket providing stream service
192} SOCK_TYPE;
193
194//
195// the handler of protocol for request from socket
196//
197typedef
198EFI_STATUS
199(*SOCK_PROTO_HANDLER) (
200 IN SOCKET * Socket, // the socket issuing the request to protocol
201 IN SOCK_REQUEST Request, // the request issued by socket
202 IN VOID *RequestData // the request related data
203 );
204
205//
206// the buffer structure of rcvd data and send data used by socket
207//
208typedef struct _SOCK_BUFFER {
209 UINT32 HighWater; // the buffersize upper limit of sock_buffer
210 UINT32 LowWater; // the low warter mark of sock_buffer
211 NET_BUF_QUEUE *DataQueue; // the queue to buffer data
212} SOCK_BUFFER;
213
214//
215// the initialize data for create a new socket
216//
217typedef struct _SOCK_INIT_DATA {
218 SOCK_TYPE Type;
219 SOCK_STATE State;
220
221 SOCKET *Parent; // the parent of this socket
222 UINT32 BackLog; // the connection limit for listening socket
223 UINT32 SndBufferSize; // the high warter mark of send buffer
224 UINT32 RcvBufferSize; // the high warter mark of receive buffer
225 VOID *Protocol; // the pointer to protocol function template
226 // wanted to install on socket
227
228 SOCK_PROTO_HANDLER ProtoHandler;
229
230 EFI_HANDLE DriverBinding; // the driver binding handle
231} SOCK_INIT_DATA;
232
233//
234// socket provided oprerations for low layer protocol
235//
236
237//
238// socket provided operations for user interface
239//
240VOID
241SockSetState (
242 IN SOCKET *Sock,
243 IN SOCK_STATE State
244 );
245
246//
247// when the connection establishment process for a Sock
248// is finished low layer protocol calling this function
249// to notify socket layer
250//
251VOID
252SockConnEstablished (
253 IN SOCKET *Sock
254 );
255
256VOID
257SockConnClosed (
258 IN SOCKET *Sock
259 );
260
261//
262// called by low layer protocol to trim send buffer of
263// Sock, when Count data is sent out completely
264//
265VOID
266SockDataSent (
267 IN SOCKET *Sock,
268 IN UINT32 Count
269 );
270
271//
272// called by low layer protocol to get Len of data from
273// socket to send and copy it in Dest
274//
275UINT32
276SockGetDataToSend (
277 IN SOCKET *Sock,
278 IN UINT32 Offset,
279 IN UINT32 Len,
280 IN UINT8 *Dest
281 );
282
283//
284// called by low layer protocol to notify socket no more data can be
285// received
286//
287VOID
288SockNoMoreData (
289 IN SOCKET *Sock
290 );
291
292//
293// called by low layer protocol to append a NetBuffer
294// to rcv buffer of sock
295//
296VOID
297SockDataRcvd (
298 IN SOCKET *Sock,
299 IN NET_BUF *NetBuffer,
300 IN UINT32 UrgLen
301 );
302
303UINT32
304SockGetFreeSpace (
305 IN SOCKET *Sock,
306 IN UINT32 Which
307 );
308
309SOCKET *
310SockClone (
311 IN SOCKET *Sock
312 );
313
314VOID
315SockRcvdErr (
316 IN SOCKET *Sock,
317 IN EFI_STATUS Error
318 );
319
320//
321// the socket structure representing a network service access point
322//
4eb65aff 323struct _SOCKET {
8a67d61d 324
325 //
326 // socket description information
327 //
328 UINT32 Signature;
329 EFI_HANDLE SockHandle; // the virtual handle of the socket
330 EFI_HANDLE DriverBinding; // socket't driver binding protocol
331 SOCK_CONFIGURE_STATE ConfigureState;
332 SOCK_TYPE Type;
333 SOCK_STATE State;
334 UINT16 Flag;
335 NET_LOCK Lock; // the lock of socket
336 SOCK_BUFFER SndBuffer; // send buffer of application's data
337 SOCK_BUFFER RcvBuffer; // receive buffer of received data
338 EFI_STATUS SockError; // the error returned by low layer protocol
339 BOOLEAN IsDestroyed;
340
341 //
342 // fields used to manage the connection request
343 //
344 UINT32 BackLog; // the limit of connection to this socket
345 UINT32 ConnCnt; // the current count of connections to it
346 SOCKET *Parent; // listening parent that accept the connection
347 NET_LIST_ENTRY ConnectionList; // the connections maintained by this socket
348 //
349 // the queue to buffer application's asynchronous token
350 //
351 NET_LIST_ENTRY ListenTokenList;
352 NET_LIST_ENTRY RcvTokenList;
353 NET_LIST_ENTRY SndTokenList;
354 NET_LIST_ENTRY ProcessingSndTokenList;
355
356 SOCK_COMPLETION_TOKEN *ConnectionToken; // app's token to signal if connected
357 SOCK_COMPLETION_TOKEN *CloseToken; // app's token to signal if closed
358
359 //
360 // interface for low level protocol
361 //
362 SOCK_PROTO_HANDLER ProtoHandler; // the request handler of protocol
363 UINT8 ProtoReserved[PROTO_RESERVED_LEN]; // Data fields reserved for protocol
364 union {
365 EFI_TCP4_PROTOCOL TcpProtocol;
366 EFI_UDP4_PROTOCOL UdpProtocol;
367 } NetProtocol;
4eb65aff 368};
8a67d61d 369
370//
371// the token structure buffered in socket layer
372//
373typedef struct _SOCK_TOKEN {
374 NET_LIST_ENTRY TokenList; // the entry to add in the token list
375 SOCK_COMPLETION_TOKEN *Token; // The application's token
376 UINT32 RemainDataLen; // unprocessed data length
377 SOCKET *Sock; // the poninter to the socket this token
378 // belongs to
379} SOCK_TOKEN;
380
381//
382// reserved data to access the NET_BUF delivered by UDP driver
383//
384typedef struct _UDP_RSV_DATA {
385 EFI_TIME TimeStamp;
386 EFI_UDP4_SESSION_DATA Session;
387} UDP_RSV_DATA;
388
389//
390// reserved data to access the NET_BUF delivered by TCP driver
391//
392typedef struct _TCP_RSV_DATA {
393 UINT32 UrgLen;
394} TCP_RSV_DATA;
395
396//
397// call it to creat a socket and attach it to a PCB
398//
399SOCKET *
400SockCreateChild (
401 IN SOCK_INIT_DATA *SockInitData,
402 IN VOID *ProtoData,
403 IN UINT32 Len
404 );
405
406//
407// call it to destroy a socket and its related PCB
408//
409EFI_STATUS
410SockDestroyChild (
411 IN SOCKET *Sock
412 );
413
414//
415// call it to configure a socket and its related PCB
416//
417EFI_STATUS
418SockConfigure (
419 IN SOCKET *Sock,
420 IN VOID *ConfigData
421 );
422
423//
424// call it to connect a socket to the peer
425//
426EFI_STATUS
427SockConnect (
428 IN SOCKET *Sock,
429 IN VOID *Token
430 );
431
432//
433// call it to issue an asynchronous listen token to the socket
434//
435EFI_STATUS
436SockAccept (
437 IN SOCKET *Sock,
438 IN VOID *Token
439 );
440
441//
442// Call it to send data using this socket
443//
444EFI_STATUS
445SockSend (
446 IN SOCKET *Sock,
447 IN VOID *Token
448 );
449
450//
451// Call it to receive data from this socket
452//
453EFI_STATUS
454SockRcv (
455 IN SOCKET *Sock,
456 IN VOID *Token
457 );
458
459//
460// Call it to flush a socket
461//
462EFI_STATUS
463SockFlush (
464 IN SOCKET *Sock
465 );
466
467//
468// Call it to close a socket in the light of policy in Token
469//
470EFI_STATUS
471SockClose (
472 IN SOCKET *Sock,
473 IN VOID *Token,
474 IN BOOLEAN OnAbort
475 );
476
477//
478// Call it to get the mode data of low layer protocol
479//
480EFI_STATUS
481SockGetMode (
482 IN SOCKET *Sock,
483 IN VOID *Mode
484 );
485
486//
487// call it to add this socket instance into a group
488//
489EFI_STATUS
490SockGroup (
491 IN SOCKET *Sock,
492 IN VOID *GroupInfo
493 );
494
495//
496// call it to add a route entry for this socket instance
497//
498EFI_STATUS
499SockRoute (
500 IN SOCKET *Sock,
501 IN VOID *RouteInfo
502 );
503
504//
505// Supporting function to operate on socket buffer
506//
507NET_BUF *
508SockBufFirst (
509 IN SOCK_BUFFER *Sockbuf
510 );
511
512NET_BUF *
513SockBufNext (
514 IN SOCK_BUFFER *Sockbuf,
515 IN NET_BUF *SockEntry
516 );
517
518#endif