summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/iScsi/IScsiTcp4Io.c
blob: b5b85a7b8a493f09e4d330376b9b46c4b92d2d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*++

Copyright (c)  2007 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.

Module Name:

  IScsiTcp4Io.c

Abstract:

--*/

#include "IScsiImpl.h"

VOID
EFIAPI
Tcp4IoCommonNotify (
  IN EFI_EVENT  Event,
  IN VOID       *Context
  )
/*++

Routine Description:

  The common notify function associated with various Tcp4Io events. 

Arguments:

  Event   - The event signaled.
  Contect - The context.

Returns:

  None.

--*/
{
  *((BOOLEAN *) Context) = TRUE;
}

EFI_STATUS
Tcp4IoCreateSocket (
  IN EFI_HANDLE           Image,
  IN EFI_HANDLE           Controller,
  IN TCP4_IO_CONFIG_DATA  *ConfigData,
  IN TCP4_IO              *Tcp4Io
  )
/*++

Routine Description:

  Create a TCP socket with the specified configuration data. 

Arguments:

  Image      - The handle of the driver image.
  Controller - The handle of the controller.
  ConfigData - The Tcp4 configuration data.
  Tcp4Io     - The Tcp4Io.

Returns:

  EFI_SUCCESS - The TCP socket is created and configured.
  other       - Failed to create the TCP socket or configure it.

--*/
{
  EFI_STATUS            Status;
  EFI_TCP4_PROTOCOL     *Tcp4;
  EFI_TCP4_CONFIG_DATA  Tcp4ConfigData;
  EFI_TCP4_OPTION       ControlOption;
  EFI_TCP4_ACCESS_POINT *AccessPoint;

  Tcp4Io->Handle = NULL;
  Tcp4Io->ConnToken.CompletionToken.Event = NULL;
  Tcp4Io->TxToken.CompletionToken.Event = NULL;
  Tcp4Io->RxToken.CompletionToken.Event = NULL;
  Tcp4Io->CloseToken.CompletionToken.Event = NULL;
  Tcp4 = NULL;

  //
  // Create the TCP4 child instance and get the TCP4 protocol.
  //
  Status = NetLibCreateServiceChild (
            Controller,
            Image,
            &gEfiTcp4ServiceBindingProtocolGuid,
            &Tcp4Io->Handle
            );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->OpenProtocol (
                  Tcp4Io->Handle,
                  &gEfiTcp4ProtocolGuid,
                  (VOID **)&Tcp4Io->Tcp4,
                  Image,
                  Controller,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  Tcp4Io->Image       = Image;
  Tcp4Io->Controller  = Controller;
  Tcp4                = Tcp4Io->Tcp4;

  //
  // Set the configuration parameters.
  //
  ControlOption.ReceiveBufferSize       = 0x200000;
  ControlOption.SendBufferSize          = 0x200000;
  ControlOption.MaxSynBackLog           = 0;
  ControlOption.ConnectionTimeout       = 0;
  ControlOption.DataRetries             = 6;
  ControlOption.FinTimeout              = 0;
  ControlOption.TimeWaitTimeout         = 0;
  ControlOption.KeepAliveProbes         = 4;
  ControlOption.KeepAliveTime           = 0;
  ControlOption.KeepAliveInterval       = 0;
  ControlOption.EnableNagle             = FALSE;
  ControlOption.EnableTimeStamp         = FALSE;
  ControlOption.EnableWindowScaling     = TRUE;
  ControlOption.EnableSelectiveAck      = FALSE;
  ControlOption.EnablePathMtuDiscovery  = FALSE;

  Tcp4ConfigData.TypeOfService          = 8;
  Tcp4ConfigData.TimeToLive             = 255;
  Tcp4ConfigData.ControlOption          = &ControlOption;

  AccessPoint = &Tcp4ConfigData.AccessPoint;

  AccessPoint->UseDefaultAddress = FALSE;
  AccessPoint->StationPort = 0;
  AccessPoint->RemotePort = ConfigData->RemotePort;
  AccessPoint->ActiveFlag = TRUE;

  NetCopyMem (&AccessPoint->StationAddress, &ConfigData->LocalIp, sizeof (EFI_IPv4_ADDRESS));
  NetCopyMem (&AccessPoint->SubnetMask, &ConfigData->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  NetCopyMem (&AccessPoint->RemoteAddress, &ConfigData->RemoteIp, sizeof (EFI_IPv4_ADDRESS));

  //
  // Configure the TCP4 protocol.
  //
  Status = Tcp4->Configure (Tcp4, &Tcp4ConfigData);
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  if (!EFI_IP4_EQUAL (&ConfigData->Gateway, &mZeroIp4Addr)) {
    //
    // the gateway is not zero, add the default route by hand
    //
    Status = Tcp4->Routes (Tcp4, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, &ConfigData->Gateway);
    if (EFI_ERROR (Status)) {
      goto ON_ERROR;
    }
  }
  //
  // Create events for variuos asynchronous operations.
  //
  Status = gBS->CreateEvent (
                  EFI_EVENT_NOTIFY_SIGNAL,
                  NET_TPL_EVENT,
                  Tcp4IoCommonNotify,
                  &Tcp4Io->IsConnDone,
                  &Tcp4Io->ConnToken.CompletionToken.Event
                  );
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  Status = gBS->CreateEvent (
                  EFI_EVENT_NOTIFY_SIGNAL,
                  NET_TPL_EVENT,
                  Tcp4IoCommonNotify,
                  &Tcp4Io->IsTxDone,
                  &Tcp4Io->TxToken.CompletionToken.Event
                  );
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  Status = gBS->CreateEvent (
                  EFI_EVENT_NOTIFY_SIGNAL,
                  NET_TPL_EVENT,
                  Tcp4IoCommonNotify,
                  &Tcp4Io->IsRxDone,
                  &Tcp4Io->RxToken.CompletionToken.Event
                  );
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  Status = gBS->CreateEvent (
                  EFI_EVENT_NOTIFY_SIGNAL,
                  NET_TPL_EVENT,
                  Tcp4IoCommonNotify,
                  &Tcp4Io->IsCloseDone,
                  &Tcp4Io->CloseToken.CompletionToken.Event
                  );
  if (EFI_ERROR (Status)) {
    goto ON_ERROR;
  }

  Tcp4Io->IsTxDone  = FALSE;
  Tcp4Io->IsRxDone  = FALSE;

  return EFI_SUCCESS;

ON_ERROR:

  if (Tcp4Io->RxToken.CompletionToken.Event != NULL) {
    gBS->CloseEvent (Tcp4Io->RxToken.CompletionToken.Event);
  }

  if (Tcp4Io->TxToken.CompletionToken.Event != NULL) {
    gBS->CloseEvent (Tcp4Io->TxToken.CompletionToken.Event);
  }

  if (Tcp4Io->ConnToken.CompletionToken.Event != NULL) {
    gBS->CloseEvent (Tcp4Io->ConnToken.CompletionToken.Event);
  }

  if (Tcp4 != NULL) {
    Tcp4->Configure (Tcp4, NULL);

    gBS->CloseProtocol (
          Tcp4Io->Handle,
          &gEfiTcp4ProtocolGuid,
          Image,
          Controller
          );
  }

  NetLibDestroyServiceChild (
    Controller,
    Image,
    &gEfiTcp4ServiceBindingProtocolGuid,
    Tcp4Io->Handle
    );

  return Status;
}

VOID
Tcp4IoDestroySocket (
  IN TCP4_IO  *Tcp4Io
  )
/*++

Routine Description:

  Destroy the socket. 

Arguments:

  Tcp4Io - The Tcp4Io which wraps the socket to be destroyeds.

Returns:

  None.

--*/
{
  EFI_TCP4_PROTOCOL *Tcp4;

  Tcp4 = Tcp4Io->Tcp4;

  Tcp4->Configure (Tcp4, NULL);

  gBS->CloseEvent (Tcp4Io->TxToken.CompletionToken.Event);
  gBS->CloseEvent (Tcp4Io->RxToken.CompletionToken.Event);
  gBS->CloseEvent (Tcp4Io->ConnToken.CompletionToken.Event);

  gBS->CloseProtocol (
        Tcp4Io->Handle,
        &gEfiTcp4ProtocolGuid,
        Tcp4Io->Image,
        Tcp4Io->Controller
        );

  NetLibDestroyServiceChild (
    Tcp4Io->Controller,
    Tcp4Io->Image,
    &gEfiTcp4ServiceBindingProtocolGuid,
    Tcp4Io->Handle
    );
}

EFI_STATUS
Tcp4IoConnect (
  IN TCP4_IO    *Tcp4Io,
  IN EFI_EVENT  Timeout
  )
/*++

Routine Description:

  Connect to the other endpoint of the TCP socket.

Arguments:

  Tcp4Io  - The Tcp4Io wrapping the TCP socket.
  Timeout - The time to wait for connection done.

Returns:

  None.

--*/
{
  EFI_TCP4_PROTOCOL *Tcp4;
  EFI_STATUS        Status;

  Tcp4Io->IsConnDone  = FALSE;
  Tcp4                = Tcp4Io->Tcp4;
  Status              = Tcp4->Connect (Tcp4, &Tcp4Io->ConnToken);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  while (!Tcp4Io->IsConnDone && EFI_ERROR (gBS->CheckEvent (Timeout))) {
    Tcp4->Poll (Tcp4);
  }

  if (!Tcp4Io->IsConnDone) {
    Status = EFI_TIMEOUT;
  } else {
    Status = Tcp4Io->ConnToken.CompletionToken.Status;
  }

  return Status;
}

VOID
Tcp4IoReset (
  IN TCP4_IO  *Tcp4Io
  )
/*++

Routine Description:

  Reset the socket.

Arguments:

  Tcp4Io - The Tcp4Io wrapping the TCP socket.

Returns:

  None.

--*/
{
  EFI_STATUS        Status;
  EFI_TCP4_PROTOCOL *Tcp4;

  Tcp4Io->CloseToken.AbortOnClose = TRUE;
  Tcp4Io->IsCloseDone             = FALSE;

  Tcp4 = Tcp4Io->Tcp4;
  Status = Tcp4->Close (Tcp4, &Tcp4Io->CloseToken);
  if (EFI_ERROR (Status)) {
    return ;
  }

  while (!Tcp4Io->IsCloseDone) {
    Tcp4->Poll (Tcp4);
  }
}

EFI_STATUS
Tcp4IoTransmit (
  IN TCP4_IO  *Tcp4Io,
  IN NET_BUF  *Packet
  )
/*++

Routine Description:

  Transmit the Packet to the other endpoint of the socket.

Arguments:

  Tcp4Io - The Tcp4Io wrapping the TCP socket.
  Packet - The packet to transmit

Returns:

  EFI_SUCCESS          - The packet is trasmitted.
  EFI_OUT_OF_RESOURCES - Failed to allocate memory.

--*/
{
  EFI_TCP4_TRANSMIT_DATA  *TxData;
  EFI_TCP4_PROTOCOL       *Tcp4;
  EFI_STATUS              Status;

  TxData = NetAllocatePool (sizeof (EFI_TCP4_TRANSMIT_DATA) + (Packet->BlockOpNum - 1) * sizeof (EFI_TCP4_FRAGMENT_DATA));
  if (TxData == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  TxData->Push        = TRUE;
  TxData->Urgent      = FALSE;
  TxData->DataLength  = Packet->TotalSize;

  //
  // Build the fragment table.
  //
  TxData->FragmentCount = Packet->BlockOpNum;
  NetbufBuildExt (Packet, (NET_FRAGMENT *) &TxData->FragmentTable[0], &TxData->FragmentCount);

  Tcp4Io->TxToken.Packet.TxData = TxData;

  //
  // Trasnmit the packet.
  //
  Tcp4    = Tcp4Io->Tcp4;
  Status  = Tcp4->Transmit (Tcp4, &Tcp4Io->TxToken);
  if (EFI_ERROR (Status)) {
    goto ON_EXIT;
  }

  while (!Tcp4Io->IsTxDone) {
    Tcp4->Poll (Tcp4);
  }

  Tcp4Io->IsTxDone  = FALSE;

  Status            = Tcp4Io->TxToken.CompletionToken.Status;

ON_EXIT:

  NetFreePool (TxData);

  return Status;
}

EFI_STATUS
Tcp4IoReceive (
  IN TCP4_IO    *Tcp4Io,
  IN NET_BUF    *Packet,
  IN BOOLEAN    AsyncMode,
  IN EFI_EVENT  Timeout
  )
/*++

Routine Description:

  Receive data from the socket.

Arguments:

  Tcp4Io    - The Tcp4Io which wraps the socket to be destroyeds.
  Packet    - The buffer to hold the data copy from the soket rx buffer.
  AsyncMode - Is this receive asyncronous or not.
  Timeout   - The time to wait for receiving the amount of data the Packet
              can hold.

Returns:

  EFI_SUCCESS          - The required amount of data is received from the socket.
  EFI_OUT_OF_RESOURCES - Failed to allocate momery.
  EFI_TIMEOUT          - Failed to receive the required amount of data in the
                         specified time period.

--*/
{
  EFI_TCP4_PROTOCOL     *Tcp4;
  EFI_TCP4_RECEIVE_DATA RxData;
  EFI_STATUS            Status;
  NET_FRAGMENT          *Fragment;
  UINT32                FragmentCount;
  UINT32                CurrentFragment;

  FragmentCount = Packet->BlockOpNum;
  Fragment      = NetAllocatePool (FragmentCount * sizeof (NET_FRAGMENT));
  if (Fragment == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }
  //
  // Build the fragment table.
  //
  NetbufBuildExt (Packet, Fragment, &FragmentCount);

  RxData.FragmentCount          = 1;
  Tcp4Io->RxToken.Packet.RxData = &RxData;
  CurrentFragment               = 0;
  Tcp4                          = Tcp4Io->Tcp4;
  Status                        = EFI_SUCCESS;

  while (CurrentFragment < FragmentCount) {
    RxData.DataLength                       = Fragment[CurrentFragment].Len;
    RxData.FragmentTable[0].FragmentLength  = Fragment[CurrentFragment].Len;
    RxData.FragmentTable[0].FragmentBuffer  = Fragment[CurrentFragment].Bulk;

    Status = Tcp4->Receive (Tcp4, &Tcp4Io->RxToken);
    if (EFI_ERROR (Status)) {
      goto ON_EXIT;
    }

    while (!Tcp4Io->IsRxDone && ((Timeout == NULL) || EFI_ERROR (gBS->CheckEvent (Timeout)))) {
      //
      // Poll until some data is received or something error happens.
      //
      Tcp4->Poll (Tcp4);
    }

    if (!Tcp4Io->IsRxDone) {
      //
      // Timeout occurs, cancel the receive request.
      //
      Tcp4->Cancel (Tcp4, &Tcp4Io->RxToken.CompletionToken);

      Status = EFI_TIMEOUT;
      goto ON_EXIT;
    } else {
      Tcp4Io->IsRxDone = FALSE;
    }

    if (EFI_ERROR (Tcp4Io->RxToken.CompletionToken.Status)) {
      Status = Tcp4Io->RxToken.CompletionToken.Status;
      goto ON_EXIT;
    }

    Fragment[CurrentFragment].Len -= RxData.FragmentTable[0].FragmentLength;
    if (Fragment[CurrentFragment].Len == 0) {
      CurrentFragment++;
    } else {
      Fragment[CurrentFragment].Bulk += RxData.FragmentTable[0].FragmentLength;
    }
  }

ON_EXIT:

  NetFreePool (Fragment);

  return Status;
}