b63eaa0106273ae6bac4d6bd05d068b58733391a
[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 NET_LIST_ENTRY Link;
334 SOCK_CONFIGURE_STATE ConfigureState;
335 SOCK_TYPE Type;
336 SOCK_STATE State;
337 UINT16 Flag;
338 NET_LOCK Lock; // the lock of socket
339 SOCK_BUFFER SndBuffer; // send buffer of application's data
340 SOCK_BUFFER RcvBuffer; // receive buffer of received data
341 EFI_STATUS SockError; // the error returned by low layer protocol
342 BOOLEAN IsDestroyed;
343
344 //
345 // fields used to manage the connection request
346 //
347 UINT32 BackLog; // the limit of connection to this socket
348 UINT32 ConnCnt; // the current count of connections to it
349 SOCKET *Parent; // listening parent that accept the connection
350 NET_LIST_ENTRY ConnectionList; // the connections maintained by this socket
351 //
352 // the queue to buffer application's asynchronous token
353 //
354 NET_LIST_ENTRY ListenTokenList;
355 NET_LIST_ENTRY RcvTokenList;
356 NET_LIST_ENTRY SndTokenList;
357 NET_LIST_ENTRY ProcessingSndTokenList;
358
359 SOCK_COMPLETION_TOKEN *ConnectionToken; // app's token to signal if connected
360 SOCK_COMPLETION_TOKEN *CloseToken; // app's token to signal if closed
361
362 //
363 // interface for low level protocol
364 //
365 SOCK_PROTO_HANDLER ProtoHandler; // the request handler of protocol
366 UINT8 ProtoReserved[PROTO_RESERVED_LEN]; // Data fields reserved for protocol
367 union {
368 EFI_TCP4_PROTOCOL TcpProtocol;
369 EFI_UDP4_PROTOCOL UdpProtocol;
370 } NetProtocol;
371 };
372
373 //
374 // the token structure buffered in socket layer
375 //
376 typedef struct _SOCK_TOKEN {
377 NET_LIST_ENTRY TokenList; // the entry to add in the token list
378 SOCK_COMPLETION_TOKEN *Token; // The application's token
379 UINT32 RemainDataLen; // unprocessed data length
380 SOCKET *Sock; // the poninter to the socket this token
381 // belongs to
382 } SOCK_TOKEN;
383
384 //
385 // reserved data to access the NET_BUF delivered by UDP driver
386 //
387 typedef struct _UDP_RSV_DATA {
388 EFI_TIME TimeStamp;
389 EFI_UDP4_SESSION_DATA Session;
390 } UDP_RSV_DATA;
391
392 //
393 // reserved data to access the NET_BUF delivered by TCP driver
394 //
395 typedef struct _TCP_RSV_DATA {
396 UINT32 UrgLen;
397 } TCP_RSV_DATA;
398
399 //
400 // call it to creat a socket and attach it to a PCB
401 //
402 SOCKET *
403 SockCreateChild (
404 IN SOCK_INIT_DATA *SockInitData,
405 IN VOID *ProtoData,
406 IN UINT32 Len
407 );
408
409 //
410 // call it to destroy a socket and its related PCB
411 //
412 EFI_STATUS
413 SockDestroyChild (
414 IN SOCKET *Sock
415 );
416
417 //
418 // call it to configure a socket and its related PCB
419 //
420 EFI_STATUS
421 SockConfigure (
422 IN SOCKET *Sock,
423 IN VOID *ConfigData
424 );
425
426 //
427 // call it to connect a socket to the peer
428 //
429 EFI_STATUS
430 SockConnect (
431 IN SOCKET *Sock,
432 IN VOID *Token
433 );
434
435 //
436 // call it to issue an asynchronous listen token to the socket
437 //
438 EFI_STATUS
439 SockAccept (
440 IN SOCKET *Sock,
441 IN VOID *Token
442 );
443
444 //
445 // Call it to send data using this socket
446 //
447 EFI_STATUS
448 SockSend (
449 IN SOCKET *Sock,
450 IN VOID *Token
451 );
452
453 //
454 // Call it to receive data from this socket
455 //
456 EFI_STATUS
457 SockRcv (
458 IN SOCKET *Sock,
459 IN VOID *Token
460 );
461
462 //
463 // Call it to flush a socket
464 //
465 EFI_STATUS
466 SockFlush (
467 IN SOCKET *Sock
468 );
469
470 //
471 // Call it to close a socket in the light of policy in Token
472 //
473 EFI_STATUS
474 SockClose (
475 IN SOCKET *Sock,
476 IN VOID *Token,
477 IN BOOLEAN OnAbort
478 );
479
480 //
481 // Call it to get the mode data of low layer protocol
482 //
483 EFI_STATUS
484 SockGetMode (
485 IN SOCKET *Sock,
486 IN VOID *Mode
487 );
488
489 //
490 // call it to add this socket instance into a group
491 //
492 EFI_STATUS
493 SockGroup (
494 IN SOCKET *Sock,
495 IN VOID *GroupInfo
496 );
497
498 //
499 // call it to add a route entry for this socket instance
500 //
501 EFI_STATUS
502 SockRoute (
503 IN SOCKET *Sock,
504 IN VOID *RouteInfo
505 );
506
507 //
508 // Supporting function to operate on socket buffer
509 //
510 NET_BUF *
511 SockBufFirst (
512 IN SOCK_BUFFER *Sockbuf
513 );
514
515 NET_BUF *
516 SockBufNext (
517 IN SOCK_BUFFER *Sockbuf,
518 IN NET_BUF *SockEntry
519 );
520
521 #endif