1. Sync the latest network stack. Add NetLibCreateIPv4DPathNode () in netlib library.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Tcp4Dxe / Socket.h
1 /** @file
2
3 Copyright (c) 2005 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Socket.h
15
16 Abstract:
17
18
19 **/
20
21 #ifndef _SOCKET_H_
22 #define _SOCKET_H_
23
24 #include <PiDxe.h>
25
26 #include <Protocol/IP4.h>
27 #include <Protocol/Tcp4.h>
28 #include <Protocol/Udp4.h>
29
30 #include <Library/NetLib.h>
31 #include <Library/DebugLib.h>
32 #include <Library/UefiRuntimeServicesTableLib.h>
33 #include <Library/UefiDriverEntryPoint.h>
34 #include <Library/UefiBootServicesTableLib.h>
35 #include <Library/BaseLib.h>
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 //
73 typedef enum {
74 SO_CLOSED = 0,
75 SO_LISTENING,
76 SO_CONNECTING,
77 SO_CONNECTED,
78 SO_DISCONNECTING
79 } SOCK_STATE;
80
81 typedef 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
150 typedef struct _SOCKET SOCKET;
151
152 typedef struct _SOCK_COMPLETION_TOKEN {
153 EFI_EVENT Event;
154 EFI_STATUS Status;
155 } SOCK_COMPLETION_TOKEN;
156
157 typedef 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 //
168 typedef 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 //
189 typedef 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 //
197 typedef
198 EFI_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 //
208 typedef 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 //
217 typedef 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 //
240 VOID
241 SockSetState (
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 //
251 VOID
252 SockConnEstablished (
253 IN SOCKET *Sock
254 );
255
256 VOID
257 SockConnClosed (
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 //
265 VOID
266 SockDataSent (
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 //
275 UINT32
276 SockGetDataToSend (
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 //
287 VOID
288 SockNoMoreData (
289 IN SOCKET *Sock
290 );
291
292 //
293 // called by low layer protocol to append a NetBuffer
294 // to rcv buffer of sock
295 //
296 VOID
297 SockDataRcvd (
298 IN SOCKET *Sock,
299 IN NET_BUF *NetBuffer,
300 IN UINT32 UrgLen
301 );
302
303 UINT32
304 SockGetFreeSpace (
305 IN SOCKET *Sock,
306 IN UINT32 Which
307 );
308
309 SOCKET *
310 SockClone (
311 IN SOCKET *Sock
312 );
313
314 VOID
315 SockRcvdErr (
316 IN SOCKET *Sock,
317 IN EFI_STATUS Error
318 );
319
320 //
321 // the socket structure representing a network service access point
322 //
323 struct _SOCKET {
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 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
332 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
333 SOCK_CONFIGURE_STATE ConfigureState;
334 SOCK_TYPE Type;
335 SOCK_STATE State;
336 UINT16 Flag;
337 NET_LOCK Lock; // the lock of socket
338 SOCK_BUFFER SndBuffer; // send buffer of application's data
339 SOCK_BUFFER RcvBuffer; // receive buffer of received data
340 EFI_STATUS SockError; // the error returned by low layer protocol
341 BOOLEAN IsDestroyed;
342
343 //
344 // fields used to manage the connection request
345 //
346 UINT32 BackLog; // the limit of connection to this socket
347 UINT32 ConnCnt; // the current count of connections to it
348 SOCKET *Parent; // listening parent that accept the connection
349 NET_LIST_ENTRY ConnectionList; // the connections maintained by this socket
350 //
351 // the queue to buffer application's asynchronous token
352 //
353 NET_LIST_ENTRY ListenTokenList;
354 NET_LIST_ENTRY RcvTokenList;
355 NET_LIST_ENTRY SndTokenList;
356 NET_LIST_ENTRY ProcessingSndTokenList;
357
358 SOCK_COMPLETION_TOKEN *ConnectionToken; // app's token to signal if connected
359 SOCK_COMPLETION_TOKEN *CloseToken; // app's token to signal if closed
360
361 //
362 // interface for low level protocol
363 //
364 SOCK_PROTO_HANDLER ProtoHandler; // the request handler of protocol
365 UINT8 ProtoReserved[PROTO_RESERVED_LEN]; // Data fields reserved for protocol
366 union {
367 EFI_TCP4_PROTOCOL TcpProtocol;
368 EFI_UDP4_PROTOCOL UdpProtocol;
369 } NetProtocol;
370 };
371
372 //
373 // the token structure buffered in socket layer
374 //
375 typedef struct _SOCK_TOKEN {
376 NET_LIST_ENTRY TokenList; // the entry to add in the token list
377 SOCK_COMPLETION_TOKEN *Token; // The application's token
378 UINT32 RemainDataLen; // unprocessed data length
379 SOCKET *Sock; // the poninter to the socket this token
380 // belongs to
381 } SOCK_TOKEN;
382
383 //
384 // reserved data to access the NET_BUF delivered by UDP driver
385 //
386 typedef struct _UDP_RSV_DATA {
387 EFI_TIME TimeStamp;
388 EFI_UDP4_SESSION_DATA Session;
389 } UDP_RSV_DATA;
390
391 //
392 // reserved data to access the NET_BUF delivered by TCP driver
393 //
394 typedef struct _TCP_RSV_DATA {
395 UINT32 UrgLen;
396 } TCP_RSV_DATA;
397
398 //
399 // call it to creat a socket and attach it to a PCB
400 //
401 SOCKET *
402 SockCreateChild (
403 IN SOCK_INIT_DATA *SockInitData,
404 IN VOID *ProtoData,
405 IN UINT32 Len
406 );
407
408 //
409 // call it to destroy a socket and its related PCB
410 //
411 EFI_STATUS
412 SockDestroyChild (
413 IN SOCKET *Sock
414 );
415
416 //
417 // call it to configure a socket and its related PCB
418 //
419 EFI_STATUS
420 SockConfigure (
421 IN SOCKET *Sock,
422 IN VOID *ConfigData
423 );
424
425 //
426 // call it to connect a socket to the peer
427 //
428 EFI_STATUS
429 SockConnect (
430 IN SOCKET *Sock,
431 IN VOID *Token
432 );
433
434 //
435 // call it to issue an asynchronous listen token to the socket
436 //
437 EFI_STATUS
438 SockAccept (
439 IN SOCKET *Sock,
440 IN VOID *Token
441 );
442
443 //
444 // Call it to send data using this socket
445 //
446 EFI_STATUS
447 SockSend (
448 IN SOCKET *Sock,
449 IN VOID *Token
450 );
451
452 //
453 // Call it to receive data from this socket
454 //
455 EFI_STATUS
456 SockRcv (
457 IN SOCKET *Sock,
458 IN VOID *Token
459 );
460
461 //
462 // Call it to flush a socket
463 //
464 EFI_STATUS
465 SockFlush (
466 IN SOCKET *Sock
467 );
468
469 //
470 // Call it to close a socket in the light of policy in Token
471 //
472 EFI_STATUS
473 SockClose (
474 IN SOCKET *Sock,
475 IN VOID *Token,
476 IN BOOLEAN OnAbort
477 );
478
479 //
480 // Call it to get the mode data of low layer protocol
481 //
482 EFI_STATUS
483 SockGetMode (
484 IN SOCKET *Sock,
485 IN VOID *Mode
486 );
487
488 //
489 // call it to add this socket instance into a group
490 //
491 EFI_STATUS
492 SockGroup (
493 IN SOCKET *Sock,
494 IN VOID *GroupInfo
495 );
496
497 //
498 // call it to add a route entry for this socket instance
499 //
500 EFI_STATUS
501 SockRoute (
502 IN SOCKET *Sock,
503 IN VOID *RouteInfo
504 );
505
506 //
507 // Supporting function to operate on socket buffer
508 //
509 NET_BUF *
510 SockBufFirst (
511 IN SOCK_BUFFER *Sockbuf
512 );
513
514 NET_BUF *
515 SockBufNext (
516 IN SOCK_BUFFER *Sockbuf,
517 IN NET_BUF *SockEntry
518 );
519
520 #endif