]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Tcp4Dxe/Socket.h
Use Mde library and definition instead of some native definitions in NetLib, to simpl...
[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 //
216 // socket provided oprerations for low layer protocol
217 //
218
219 //
220 // socket provided operations for user interface
221 //
222 VOID
223 SockSetState (
224 IN SOCKET *Sock,
225 IN SOCK_STATE State
226 );
227
228 //
229 // when the connection establishment process for a Sock
230 // is finished low layer protocol calling this function
231 // to notify socket layer
232 //
233 VOID
234 SockConnEstablished (
235 IN SOCKET *Sock
236 );
237
238 VOID
239 SockConnClosed (
240 IN SOCKET *Sock
241 );
242
243 //
244 // called by low layer protocol to trim send buffer of
245 // Sock, when Count data is sent out completely
246 //
247 VOID
248 SockDataSent (
249 IN SOCKET *Sock,
250 IN UINT32 Count
251 );
252
253 //
254 // called by low layer protocol to get Len of data from
255 // socket to send and copy it in Dest
256 //
257 UINT32
258 SockGetDataToSend (
259 IN SOCKET *Sock,
260 IN UINT32 Offset,
261 IN UINT32 Len,
262 IN UINT8 *Dest
263 );
264
265 //
266 // called by low layer protocol to notify socket no more data can be
267 // received
268 //
269 VOID
270 SockNoMoreData (
271 IN SOCKET *Sock
272 );
273
274 //
275 // called by low layer protocol to append a NetBuffer
276 // to rcv buffer of sock
277 //
278 VOID
279 SockDataRcvd (
280 IN SOCKET *Sock,
281 IN NET_BUF *NetBuffer,
282 IN UINT32 UrgLen
283 );
284
285 UINT32
286 SockGetFreeSpace (
287 IN SOCKET *Sock,
288 IN UINT32 Which
289 );
290
291 SOCKET *
292 SockClone (
293 IN SOCKET *Sock
294 );
295
296 VOID
297 SockRcvdErr (
298 IN SOCKET *Sock,
299 IN EFI_STATUS Error
300 );
301
302 typedef
303 EFI_STATUS
304 (*SOCK_CREATE_CALLBACK) (
305 IN SOCKET *This,
306 IN VOID *Context
307 );
308
309 typedef
310 VOID
311 (*SOCK_DESTROY_CALLBACK) (
312 IN SOCKET *This,
313 IN VOID *Context
314 );
315
316 //
317 // the initialize data for create a new socket
318 //
319 typedef struct _SOCK_INIT_DATA {
320 SOCK_TYPE Type;
321 SOCK_STATE State;
322
323 SOCKET *Parent; // the parent of this socket
324 UINT32 BackLog; // the connection limit for listening socket
325 UINT32 SndBufferSize; // the high warter mark of send buffer
326 UINT32 RcvBufferSize; // the high warter mark of receive buffer
327 VOID *Protocol; // the pointer to protocol function template
328 // wanted to install on socket
329
330 //
331 // Callbacks after socket is created and before socket is to be destroyed.
332 //
333 SOCK_CREATE_CALLBACK CreateCallback;
334 SOCK_DESTROY_CALLBACK DestroyCallback;
335 VOID *Context;
336
337 //
338 // Opaque protocol data.
339 //
340 VOID *ProtoData;
341 UINT32 DataSize;
342
343 SOCK_PROTO_HANDLER ProtoHandler;
344
345 EFI_HANDLE DriverBinding; // the driver binding handle
346 } SOCK_INIT_DATA;
347 //
348 // the socket structure representing a network service access point
349 //
350 struct _SOCKET {
351
352 //
353 // socket description information
354 //
355 UINT32 Signature;
356 EFI_HANDLE SockHandle; // the virtual handle of the socket
357 EFI_HANDLE DriverBinding; // socket't driver binding protocol
358 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
359 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
360 LIST_ENTRY Link;
361 SOCK_CONFIGURE_STATE ConfigureState;
362 SOCK_TYPE Type;
363 SOCK_STATE State;
364 UINT16 Flag;
365 EFI_LOCK Lock; // the lock of socket
366 SOCK_BUFFER SndBuffer; // send buffer of application's data
367 SOCK_BUFFER RcvBuffer; // receive buffer of received data
368 EFI_STATUS SockError; // the error returned by low layer protocol
369 BOOLEAN IsDestroyed;
370
371 //
372 // fields used to manage the connection request
373 //
374 UINT32 BackLog; // the limit of connection to this socket
375 UINT32 ConnCnt; // the current count of connections to it
376 SOCKET *Parent; // listening parent that accept the connection
377 LIST_ENTRY ConnectionList; // the connections maintained by this socket
378 //
379 // the queue to buffer application's asynchronous token
380 //
381 LIST_ENTRY ListenTokenList;
382 LIST_ENTRY RcvTokenList;
383 LIST_ENTRY SndTokenList;
384 LIST_ENTRY ProcessingSndTokenList;
385
386 SOCK_COMPLETION_TOKEN *ConnectionToken; // app's token to signal if connected
387 SOCK_COMPLETION_TOKEN *CloseToken; // app's token to signal if closed
388
389 //
390 // interface for low level protocol
391 //
392 SOCK_PROTO_HANDLER ProtoHandler; // the request handler of protocol
393 UINT8 ProtoReserved[PROTO_RESERVED_LEN]; // Data fields reserved for protocol
394 union {
395 EFI_TCP4_PROTOCOL TcpProtocol;
396 EFI_UDP4_PROTOCOL UdpProtocol;
397 } NetProtocol;
398
399 //
400 // Callbacks.
401 //
402 SOCK_CREATE_CALLBACK CreateCallback;
403 SOCK_DESTROY_CALLBACK DestroyCallback;
404 VOID *Context;
405 };
406
407 //
408 // the token structure buffered in socket layer
409 //
410 typedef struct _SOCK_TOKEN {
411 LIST_ENTRY TokenList; // the entry to add in the token list
412 SOCK_COMPLETION_TOKEN *Token; // The application's token
413 UINT32 RemainDataLen; // unprocessed data length
414 SOCKET *Sock; // the poninter to the socket this token
415 // belongs to
416 } SOCK_TOKEN;
417
418 //
419 // reserved data to access the NET_BUF delivered by UDP driver
420 //
421 typedef struct _UDP_RSV_DATA {
422 EFI_TIME TimeStamp;
423 EFI_UDP4_SESSION_DATA Session;
424 } UDP_RSV_DATA;
425
426 //
427 // reserved data to access the NET_BUF delivered by TCP driver
428 //
429 typedef struct _TCP_RSV_DATA {
430 UINT32 UrgLen;
431 } TCP_RSV_DATA;
432
433 //
434 // call it to creat a socket and attach it to a PCB
435 //
436 SOCKET *
437 SockCreateChild (
438 IN SOCK_INIT_DATA *SockInitData
439 );
440
441 //
442 // call it to destroy a socket and its related PCB
443 //
444 EFI_STATUS
445 SockDestroyChild (
446 IN SOCKET *Sock
447 );
448
449 //
450 // call it to configure a socket and its related PCB
451 //
452 EFI_STATUS
453 SockConfigure (
454 IN SOCKET *Sock,
455 IN VOID *ConfigData
456 );
457
458 //
459 // call it to connect a socket to the peer
460 //
461 EFI_STATUS
462 SockConnect (
463 IN SOCKET *Sock,
464 IN VOID *Token
465 );
466
467 //
468 // call it to issue an asynchronous listen token to the socket
469 //
470 EFI_STATUS
471 SockAccept (
472 IN SOCKET *Sock,
473 IN VOID *Token
474 );
475
476 //
477 // Call it to send data using this socket
478 //
479 EFI_STATUS
480 SockSend (
481 IN SOCKET *Sock,
482 IN VOID *Token
483 );
484
485 //
486 // Call it to receive data from this socket
487 //
488 EFI_STATUS
489 SockRcv (
490 IN SOCKET *Sock,
491 IN VOID *Token
492 );
493
494 //
495 // Call it to flush a socket
496 //
497 EFI_STATUS
498 SockFlush (
499 IN SOCKET *Sock
500 );
501
502 //
503 // Call it to close a socket in the light of policy in Token
504 //
505 EFI_STATUS
506 SockClose (
507 IN SOCKET *Sock,
508 IN VOID *Token,
509 IN BOOLEAN OnAbort
510 );
511
512 //
513 // Call it to get the mode data of low layer protocol
514 //
515 EFI_STATUS
516 SockGetMode (
517 IN SOCKET *Sock,
518 IN VOID *Mode
519 );
520
521 //
522 // call it to add this socket instance into a group
523 //
524 EFI_STATUS
525 SockGroup (
526 IN SOCKET *Sock,
527 IN VOID *GroupInfo
528 );
529
530 //
531 // call it to add a route entry for this socket instance
532 //
533 EFI_STATUS
534 SockRoute (
535 IN SOCKET *Sock,
536 IN VOID *RouteInfo
537 );
538
539 //
540 // Supporting function to operate on socket buffer
541 //
542 NET_BUF *
543 SockBufFirst (
544 IN SOCK_BUFFER *Sockbuf
545 );
546
547 NET_BUF *
548 SockBufNext (
549 IN SOCK_BUFFER *Sockbuf,
550 IN NET_BUF *SockEntry
551 );
552
553 #endif