Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#include "nx_packet.h"
#include "nx_tcp.h"
#include "nx_ipv6.h"
...
...
_nx_tcp_socket_state_ack_check(NX_TCP_SOCKET *, NX_TCP_HEADER *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocommon/src/nx_tcp_socket_state_ack_check.c
 
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Component */ /** */ /** Transmission Control Protocol (TCP) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_ip.h" #include "nx_packet.h" #include "nx_tcp.h" #ifdef FEATURE_NX_IPV6 #include "nx_ipv6.h" #endif ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_tcp_socket_state_ack_check PORTABLE C */ /* 6.1.10 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function checks for ACK conditions in various states of the */ /* TCP socket. ACK messages are examined against the queued transmit */ /* packets in order to see if one or more transmit packets may be */ /* removed from the socket's transmit queue. */ /* */ /* INPUT */ /* */ /* socket_ptr Pointer to owning socket */ /* tcp_header_ptr Pointer to packet header */ /* */ /* OUTPUT */ /* */ /* NX_TRUE The ACK number is acceptable */ /* NX_FALSE The ACK number is unacceptable*/ /* */ /* CALLS */ /* */ /* _nx_tcp_packet_send_ack Send ACK message */ /* _nx_packet_release Packet release function */ /* _nx_tcp_socket_retransmit Retransmit packet */ /* */ /* CALLED BY */ /* */ /* _nx_tcp_socket_packet_process Process TCP packet for socket */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), and */ /* corrected the calculation of*/ /* ending packet sequence, */ /* resulting in version 6.1 */ /* 06-02-2021 Yuxin Zhou Modified comment(s), */ /* fixed compiler warnings, */ /* resulting in version 6.1.7 */ /* 10-15-2021 Yuxin Zhou Modified comment(s), */ /* fixed the bug of race */ /* condition, removed useless */ /* code, */ /* resulting in version 6.1.9 */ /* 01-31-2022 Yuxin Zhou Modified comment(s), and */ /* fixed unsigned integers */ /* comparison, */ /* resulting in version 6.1.10 */ /* */... /**************************************************************************/ UINT _nx_tcp_socket_state_ack_check(NX_TCP_SOCKET *socket_ptr, NX_TCP_HEADER *tcp_header_ptr) { TX_INTERRUPT_SAVE_AREA NX_TCP_HEADER *search_header_ptr = NX_NULL; NX_PACKET *search_ptr; NX_PACKET *previous_ptr; ULONG header_length; ULONG search_sequence; ULONG temp; ULONG packet_release_count; ULONG ending_packet_sequence; ULONG starting_tx_sequence; ULONG ending_tx_sequence; ULONG ending_rx_sequence; ULONG acked_bytes; ULONG tcp_payload_length; UINT wrapped_flag = NX_FALSE; /* Determine if the header has an ACK bit set. This is an acknowledgement of a previous transmission. *//* ... */ if (tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) { /* Initialize tx sequence. */ if (socket_ptr -> nx_tcp_socket_zero_window_probe_has_data) { ending_tx_sequence = socket_ptr -> nx_tcp_socket_tx_sequence + 1; }if (socket_ptr -> nx_tcp_socket_zero_window_probe_has_data) { ... } else { ending_tx_sequence = socket_ptr -> nx_tcp_socket_tx_sequence; }else { ... } starting_tx_sequence = socket_ptr -> nx_tcp_socket_tx_sequence - socket_ptr -> nx_tcp_socket_tx_outstanding_bytes; /* Initialize ending rx sequence. */ if (socket_ptr -> nx_tcp_socket_receive_queue_tail) { search_ptr = socket_ptr -> nx_tcp_socket_receive_queue_tail; /* Setup a pointer to header of this packet in the sent list. */ #ifndef NX_DISABLE_IPV4 if (search_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4) { /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ search_header_ptr = (NX_TCP_HEADER *)(search_ptr -> nx_packet_ip_header + sizeof(NX_IPV4_HEADER)); }if (search_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4) { ... } else/* ... */ #endif /* NX_DISABLE_IPV4 */ #ifdef FEATURE_NX_IPV6 if (search_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) { /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ search_header_ptr = (NX_TCP_HEADER *)(search_ptr -> nx_packet_ip_header + sizeof(NX_IPV6_HEADER)); }else if (search_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) { ... } else #endif /* FEATURE_NX_IPV6 */ { return(NX_FALSE); }else { ... } /* Determine the size of the TCP header. */ temp = search_header_ptr -> nx_tcp_header_word_3; header_length = (temp >> NX_TCP_HEADER_SHIFT) * (ULONG)sizeof(ULONG); /* Determine the sequence number in the TCP header. */ search_sequence = search_header_ptr -> nx_tcp_sequence_number; /* Calculate the payload length of TCP. */ tcp_payload_length = (search_ptr -> nx_packet_length - (header_length + (ULONG)((ALIGN_TYPE)search_header_ptr - (ALIGN_TYPE)search_ptr -> nx_packet_prepend_ptr))); /* Calculate the ending packet sequence. */ ending_rx_sequence = search_sequence + tcp_payload_length; }if (socket_ptr -> nx_tcp_socket_receive_queue_tail) { ... } else { ending_rx_sequence = socket_ptr -> nx_tcp_socket_rx_sequence; }else { ... } #ifdef NX_ENABLE_TCP_KEEPALIVE /* Determine if the socket is in the established state. */ if (socket_ptr -> nx_tcp_socket_state == NX_TCP_ESTABLISHED) { /* Is the keepalive feature enabled on this socket? */ if (socket_ptr -> nx_tcp_socket_keepalive_enabled) { /* Yes, reset the TCP Keepalive timer to initial values. */ socket_ptr -> nx_tcp_socket_keepalive_timeout = NX_TCP_KEEPALIVE_INITIAL; socket_ptr -> nx_tcp_socket_keepalive_retries = 0; /* Determine if we have received a Keepalive ACK request from the other side of the connection. *//* ... */ if (tcp_header_ptr -> nx_tcp_sequence_number == (socket_ptr -> nx_tcp_socket_rx_sequence - 1)) { /* Yes, a Keepalive ACK probe is present. Respond with an ACK to let the other side of the connection know that we are still alive. *//* ... */ _nx_tcp_packet_send_ack(socket_ptr, ending_tx_sequence); }if (tcp_header_ptr -> nx_tcp_sequence_number == (socket_ptr -> nx_tcp_socket_rx_sequence - 1)) { ... } }if (socket_ptr -> nx_tcp_socket_keepalive_enabled) { ... } }if (socket_ptr -> nx_tcp_socket_state == NX_TCP_ESTABLISHED) { ... } /* ... */#endif /* First, determine if incoming ACK matches our transmit sequence. */ /*lint -e{923} suppress cast of pointer to ULONG. */ if (tcp_header_ptr -> nx_tcp_acknowledgment_number == ending_tx_sequence) { /* In this case, everything on the transmit list is acknowledged. Simply set the packet release count to the number of packets in the transmit queue. *//* ... */ packet_release_count = socket_ptr -> nx_tcp_socket_transmit_sent_count; /* Set the previous pointer to the socket transmit tail pointer. */ previous_ptr = socket_ptr -> nx_tcp_socket_transmit_sent_tail; /* Is this ACK to FIN? */ if (socket_ptr -> nx_tcp_socket_state >= NX_TCP_FIN_WAIT_1) { /* Yes it is. */ socket_ptr -> nx_tcp_socket_fin_acked = NX_TRUE; }if (socket_ptr -> nx_tcp_socket_state >= NX_TCP_FIN_WAIT_1) { ... } }if (tcp_header_ptr -> nx_tcp_acknowledgment_number == ending_tx_sequence) { ... } else { /* Calculate the start and end of the transmit sequence. */ /* Pickup the head of the transmit queue. */ search_ptr = socket_ptr -> nx_tcp_socket_transmit_sent_head; /* Determine if there is a packet on the transmit queue... and determine if the packet has been transmitted. *//* ... */ /*lint -e{923} suppress cast of ULONG to pointer. */ if ((search_ptr) && (search_ptr -> nx_packet_queue_next == ((NX_PACKET *)NX_DRIVER_TX_DONE))) { /* Determine if the incoming ACK matches the front of our transmit queue. */ if (tcp_header_ptr -> nx_tcp_acknowledgment_number == starting_tx_sequence) { /* Handle duplicated ACK packet. */ socket_ptr -> nx_tcp_socket_duplicated_ack_received++; if (socket_ptr -> nx_tcp_socket_duplicated_ack_received == 3) { if ((INT)((tcp_header_ptr -> nx_tcp_acknowledgment_number - 1) - socket_ptr -> nx_tcp_socket_tx_sequence_recover) > 0) { /* Cumulative acknowledge covers more than recover. */ /* Section 3.2, Page 5, RFC6582. */ /* Retransmit packet immediately. */ _nx_tcp_socket_retransmit(socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, NX_TRUE); }if ((INT)((tcp_header_ptr -> nx_tcp_acknowledgment_number - 1) - socket_ptr -> nx_tcp_socket_tx_sequence_recover) > 0) { ... } else if ((socket_ptr -> nx_tcp_socket_tx_window_congestion > socket_ptr -> nx_tcp_socket_connect_mss) && ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - (socket_ptr -> nx_tcp_socket_previous_highest_ack + (socket_ptr -> nx_tcp_socket_connect_mss << 2))) < 0)) { /* Congestion window is greater than SMSS bytes and the difference between highest_ack and prev_highest_ack is at most 4*SMSS bytes.*//* ... */ /* Section 4.1, Page 5, RFC6582. */ /* Retransmit packet immediately. */ _nx_tcp_socket_retransmit(socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, NX_TRUE); }else if ((socket_ptr -> nx_tcp_socket_tx_window_congestion > socket_ptr -> nx_tcp_socket_connect_mss) && ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - (socket_ptr -> nx_tcp_socket_previous_highest_ack + (socket_ptr -> nx_tcp_socket_connect_mss << 2))) < 0)) { ... } }if (socket_ptr -> nx_tcp_socket_duplicated_ack_received == 3) { ... } else if ((socket_ptr -> nx_tcp_socket_duplicated_ack_received > 3) && (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE)) { /* CWND += MSS */ socket_ptr -> nx_tcp_socket_tx_window_congestion += socket_ptr -> nx_tcp_socket_connect_mss; }else if ((socket_ptr -> nx_tcp_socket_duplicated_ack_received > 3) && (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE)) { ... } }if (tcp_header_ptr -> nx_tcp_acknowledgment_number == starting_tx_sequence) { ... } /* Determine if the transmit queue has wrapped. */ if (ending_tx_sequence > starting_tx_sequence) { /* Clear the wrapped flag. */ wrapped_flag = NX_FALSE; }if (ending_tx_sequence > starting_tx_sequence) { ... } else { /* Set the wrapped flag. */ wrapped_flag = NX_TRUE; }else { ... } }if ((search_ptr) && (search_ptr -> nx_packet_queue_next == ((NX_PACKET *)NX_DRIVER_TX_DONE))) { ... } /* Initialize the packet release count. */ packet_release_count = 0; /* See if we can find the sequence number in the sent queue for this socket. *//* ... */ previous_ptr = NX_NULL; while (search_ptr) { /* Determine if the packet has been transmitted. */ /*lint -e{923} suppress cast of ULONG to pointer. */ if (search_ptr -> nx_packet_queue_next != ((NX_PACKET *)NX_DRIVER_TX_DONE)) { /* Setup a pointer to header of this packet in the sent list. */ search_header_ptr = (NX_TCP_HEADER *)(search_ptr -> nx_packet_ip_header + search_ptr -> nx_packet_ip_header_length); }if (search_ptr -> nx_packet_queue_next != ((NX_PACKET *)NX_DRIVER_TX_DONE)) { ... } else { /* Setup a pointer to header of this packet in the sent list. */ /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ search_header_ptr = (NX_TCP_HEADER *)search_ptr -> nx_packet_prepend_ptr; }else { ... } /* Determine the size of the TCP header. */ temp = search_header_ptr -> nx_tcp_header_word_3; NX_CHANGE_ULONG_ENDIAN(temp); header_length = (temp >> NX_TCP_HEADER_SHIFT) * (ULONG)sizeof(ULONG); /* Determine the sequence number in the TCP header. */ search_sequence = search_header_ptr -> nx_tcp_sequence_number; NX_CHANGE_ULONG_ENDIAN(search_sequence); /* Calculate the payload length of TCP. */ tcp_payload_length = (search_ptr -> nx_packet_length - (header_length + (ULONG)((ALIGN_TYPE)search_header_ptr - (ALIGN_TYPE)search_ptr -> nx_packet_prepend_ptr))); /* Calculate the ending packet sequence. */ ending_packet_sequence = search_sequence + tcp_payload_length; /* Determine if the transmit window is wrapped. */ if (wrapped_flag == NX_FALSE) { /* No, the transmit window is not wrapped. Perform a simple compare to determine if the ACK covers the current search packet. *//* ... */ /* Is this ACK before the current search packet or after the transmit sequence? */ if ((tcp_header_ptr -> nx_tcp_acknowledgment_number < ending_packet_sequence) || (tcp_header_ptr -> nx_tcp_acknowledgment_number > ending_tx_sequence)) { /* The ACK is less than the current packet, just get out of the loop! */ break; }if ((tcp_header_ptr -> nx_tcp_acknowledgment_number < ending_packet_sequence) || (tcp_header_ptr -> nx_tcp_acknowledgment_number > ending_tx_sequence)) { ... } }if (wrapped_flag == NX_FALSE) { ... } else { /* Yes, the transmit window has wrapped. We need to now check for all the wrap conditions to determine if ACK covers the current search packet. *//* ... */ /* Is the search packet's ending sequence number in the wrapped part of the window. */ if (ending_packet_sequence < starting_tx_sequence) { /* The search packet ends in the wrapped portion of the window. Determine if the ACK sequence in the wrapped portion as well. *//* ... */ if (tcp_header_ptr -> nx_tcp_acknowledgment_number < starting_tx_sequence) { /* Yes, the ACK sequence is in the wrapped portion as well. Simply compare the ACK sequence with the search packet sequence. *//* ... */ if (tcp_header_ptr -> nx_tcp_acknowledgment_number < ending_packet_sequence) { /* ACK does not cover the search packet. Break out of the loop. */ break; }if (tcp_header_ptr -> nx_tcp_acknowledgment_number < ending_packet_sequence) { ... } }if (tcp_header_ptr -> nx_tcp_acknowledgment_number < starting_tx_sequence) { ... } else { /* The ACK sequence is in the non-wrapped portion of the window and the ending sequence of the search packet is in the wrapped portion - so the ACK doesn't cover the search packet. Break out of the loop! *//* ... */ break; }else { ... } }if (ending_packet_sequence < starting_tx_sequence) { ... } else { /* The search packet is in the non-wrapped portion of the window. Determine if the ACK sequence is in the non-wrapped portion as well. *//* ... */ if (tcp_header_ptr -> nx_tcp_acknowledgment_number >= starting_tx_sequence) { /* Yes, the ACK sequence is in the non-wrapped portion of the window. Simply compare the ACK sequence with the search packet sequence. *//* ... */ if (tcp_header_ptr -> nx_tcp_acknowledgment_number < ending_packet_sequence) { /* ACK does not cover the search packet. Break out of the loop. */ break; }if (tcp_header_ptr -> nx_tcp_acknowledgment_number < ending_packet_sequence) { ... } }if (tcp_header_ptr -> nx_tcp_acknowledgment_number >= starting_tx_sequence) { ... } }else { ... } }else { ... } /* At this point we know that the ACK received covers the search packet. */ /* Increase the packet release count. */ packet_release_count++; /* Move the search and previous pointers forward. */ previous_ptr = search_ptr; search_ptr = search_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next; /* Determine if we are at the end of the TCP queue. */ /*lint -e{923} suppress cast of ULONG to pointer. */ if (search_ptr == ((NX_PACKET *)NX_PACKET_ENQUEUED)) { /* Yes, set the search pointer to NULL. */ search_ptr = NX_NULL; }if (search_ptr == ((NX_PACKET *)NX_PACKET_ENQUEUED)) { ... } }while (search_ptr) { ... } }else { ... } /* Determine if anything needs to be released. */ if (!packet_release_count) { /* No, check and see if the ACK is valid. */ /* If the ACK acks something not yet sent (SEG.ACK > SND.NXT) then send an ACK, drop the segment */ /* Page 72, section 3.9, RFC 793.*/ if (tcp_header_ptr -> nx_tcp_acknowledgment_number != ending_tx_sequence) { /* If the ACK is a duplicate, it can be ignored. */ if ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - ending_tx_sequence) > 0) { /* The ACK sequence is invalid. Respond with an ACK to let the other side of the connection figure out if everything is still okay. *//* ... */ _nx_tcp_packet_send_ack(socket_ptr, ending_tx_sequence); return(NX_FALSE); }if ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - ending_tx_sequence) > 0) { ... } }if (tcp_header_ptr -> nx_tcp_acknowledgment_number != ending_tx_sequence) { ... } else if ((socket_ptr -> nx_tcp_socket_rx_window_current == 0) && (tcp_header_ptr -> nx_tcp_sequence_number == socket_ptr -> nx_tcp_socket_rx_sequence)) { /* Response to zero window probe. */ _nx_tcp_packet_send_ack(socket_ptr, ending_tx_sequence); }else if ((socket_ptr -> nx_tcp_socket_rx_window_current == 0) && (tcp_header_ptr -> nx_tcp_sequence_number == socket_ptr -> nx_tcp_socket_rx_sequence)) { ... } }if (!packet_release_count) { ... } else { /* Congestion window adjustment during slow start and congestion avoidance is executed on every incoming ACK that acknowledges new data. RFC5681, Section3.1, Page4-8. *//* ... */ /* Check whether the socket is in fast recovery procedure. */ if (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE) { /* Yes. */ if ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - socket_ptr -> nx_tcp_socket_tx_sequence_recover) > 0) { /* All packets sent before entering fast recovery are ACKed. */ /* Exit fast recovery procedure. */ socket_ptr -> nx_tcp_socket_fast_recovery = NX_FALSE; socket_ptr -> nx_tcp_socket_tx_window_congestion = socket_ptr -> nx_tcp_socket_tx_slow_start_threshold; }if ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - socket_ptr -> nx_tcp_socket_tx_sequence_recover) > 0) { ... } }if (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE) { ... } if ((INT)(socket_ptr -> nx_tcp_socket_tx_sequence_recover - (tcp_header_ptr -> nx_tcp_acknowledgment_number - 2)) < 0) { /* Update the transmit sequence that entered fast transmit. */ socket_ptr -> nx_tcp_socket_tx_sequence_recover = tcp_header_ptr -> nx_tcp_acknowledgment_number - 2; }if ((INT)(socket_ptr -> nx_tcp_socket_tx_sequence_recover - (tcp_header_ptr -> nx_tcp_acknowledgment_number - 2)) < 0) { ... } /* Reset the duplicated ACK counter. */ socket_ptr -> nx_tcp_socket_duplicated_ack_received = 0; /* Set previous cumulative acknowlesgement. */ socket_ptr -> nx_tcp_socket_previous_highest_ack = starting_tx_sequence; /* Calculate ACKed length. */ acked_bytes = tcp_header_ptr -> nx_tcp_acknowledgment_number - starting_tx_sequence; if (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE) { /* Process cwnd in fast recovery procedure. */ socket_ptr -> nx_tcp_socket_tx_window_congestion -= acked_bytes; if (acked_bytes > socket_ptr -> nx_tcp_socket_connect_mss) { socket_ptr -> nx_tcp_socket_tx_window_congestion += socket_ptr -> nx_tcp_socket_connect_mss; }if (acked_bytes > socket_ptr -> nx_tcp_socket_connect_mss) { ... } }if (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE) { ... } else { /* Adjust the transmit window. In slow start phase, the transmit window is incremented for every ACK. In Congestion Avoidance phase, the window is incremented for every RTT. Section 3.1, Page 4-7, RFC5681. *//* ... */ if (socket_ptr -> nx_tcp_socket_tx_window_congestion >= socket_ptr -> nx_tcp_socket_tx_slow_start_threshold) { /* In Congestion avoidance phase, for every ACK it receives, increase the window size using the following approximation: cwnd = cwnd + MSS * MSS / cwnd; *//* ... */ temp = socket_ptr -> nx_tcp_socket_connect_mss2 / socket_ptr -> nx_tcp_socket_tx_window_congestion; /* If the above formula yields 0, the result SHOULD be rounded up to 1 byte. */ if (temp == 0) { temp = 1; }if (temp == 0) { ... } socket_ptr -> nx_tcp_socket_tx_window_congestion = socket_ptr -> nx_tcp_socket_tx_window_congestion + temp; }if (socket_ptr -> nx_tcp_socket_tx_window_congestion >= socket_ptr -> nx_tcp_socket_tx_slow_start_threshold) { ... } else { /* In Slow start phase: cwnd += min (N, SMSS), where N is the number of ACKed bytes. *//* ... */ if (acked_bytes < socket_ptr -> nx_tcp_socket_connect_mss) { /* In Slow start phase. Increase the cwnd by acked bytes.*/ socket_ptr -> nx_tcp_socket_tx_window_congestion += acked_bytes; }if (acked_bytes < socket_ptr -> nx_tcp_socket_connect_mss) { ... } else { /* In Slow start phase. Increase the cwnd by full MSS for every ack.*/ socket_ptr -> nx_tcp_socket_tx_window_congestion += socket_ptr -> nx_tcp_socket_connect_mss; }else { ... } }else { ... } }else { ... } }else { ... } /* Update the window only when * 1. SND.UNA < SEG.ACK =< SND.NXT or * 2. SND.WL1 < SEG.SEQ or * 3. SND.WL1 = SEG.SEQ and SND.WL2 =< SEG.ACK * RFC793, Section 3.9, Page72. *//* ... */ if ((((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - starting_tx_sequence) > 0) && ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - ending_tx_sequence) <= 0)) || ((INT)(tcp_header_ptr -> nx_tcp_sequence_number - ending_rx_sequence) > 0) || (((INT)(tcp_header_ptr -> nx_tcp_sequence_number == ending_rx_sequence)) && ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - starting_tx_sequence) >= 0))) { /* Update this socket's transmit window with the advertised window size in the ACK message. */ socket_ptr -> nx_tcp_socket_tx_window_advertised = (tcp_header_ptr -> nx_tcp_header_word_3) & NX_LOWER_16_MASK; #ifdef NX_ENABLE_TCP_WINDOW_SCALING socket_ptr -> nx_tcp_socket_tx_window_advertised <<= socket_ptr -> nx_tcp_snd_win_scale_value; #endif /* NX_ENABLE_TCP_WINDOW_SCALING */ }if ((((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - starting_tx_sequence) > 0) && ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - ending_tx_sequence) <= 0)) || ((INT)(tcp_header_ptr -> nx_tcp_sequence_number - ending_rx_sequence) > 0) || (((INT)(tcp_header_ptr -> nx_tcp_sequence_number == ending_rx_sequence)) && ((INT)(tcp_header_ptr -> nx_tcp_acknowledgment_number - starting_tx_sequence) >= 0))) { ... } /* Check advertised window. */ if ((socket_ptr -> nx_tcp_socket_tx_window_advertised <= socket_ptr -> nx_tcp_socket_tx_outstanding_bytes) && (tcp_header_ptr -> nx_tcp_acknowledgment_number >= socket_ptr -> nx_tcp_socket_zero_window_probe_sequence)) { /* It is an ACK to Zero Window Probe. Reset the zero window probe failure. */ socket_ptr -> nx_tcp_socket_zero_window_probe_failure = 0; }if ((socket_ptr -> nx_tcp_socket_tx_window_advertised <= socket_ptr -> nx_tcp_socket_tx_outstanding_bytes) && (tcp_header_ptr -> nx_tcp_acknowledgment_number >= socket_ptr -> nx_tcp_socket_zero_window_probe_sequence)) { ... } if (!packet_release_count) { /* Done, return to caller. */ return(NX_TRUE); }if (!packet_release_count) { ... } /* Once we get this line, packet_release_count is non-zero, which indicates the transmit_sent_count value will be reduced. *//* ... */ #ifdef NX_ENABLE_TCP_QUEUE_DEPTH_UPDATE_NOTIFY /* Is NetX Duo set up with a transmit socket queue depth update? */ if (socket_ptr -> nx_tcp_socket_queue_depth_notify) { /* Yes it is. Verify that the packets on the transmit queue exceed the max queue depth, and that the packets to release bring that queue size below the max queue depth. *//* ... */ if ((socket_ptr -> nx_tcp_socket_transmit_sent_count >= socket_ptr -> nx_tcp_socket_transmit_queue_maximum) && ((socket_ptr -> nx_tcp_socket_transmit_sent_count - packet_release_count) < socket_ptr -> nx_tcp_socket_transmit_queue_maximum)) { /* Yes to both; Call the queue depth notify callback. */ (socket_ptr -> nx_tcp_socket_queue_depth_notify)(socket_ptr); }if ((socket_ptr -> nx_tcp_socket_transmit_sent_count >= socket_ptr -> nx_tcp_socket_transmit_queue_maximum) && ((socket_ptr -> nx_tcp_socket_transmit_sent_count - packet_release_count) < socket_ptr -> nx_tcp_socket_transmit_queue_maximum)) { ... } }if (socket_ptr -> nx_tcp_socket_queue_depth_notify) { ... } /* ... */#endif /* Save the front of the of the transmit queue. */ search_ptr = socket_ptr -> nx_tcp_socket_transmit_sent_head; /* Okay so now the packet after the previous pointer needs to be the front of the queue. *//* ... */ if (previous_ptr != socket_ptr -> nx_tcp_socket_transmit_sent_tail) { /* Just update the head pointer. */ socket_ptr -> nx_tcp_socket_transmit_sent_head = previous_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next; /* And decrease the transmit queue count accordingly. */ socket_ptr -> nx_tcp_socket_transmit_sent_count = socket_ptr -> nx_tcp_socket_transmit_sent_count - packet_release_count; /* Setup a new transmit timeout. */ socket_ptr -> nx_tcp_socket_timeout = socket_ptr -> nx_tcp_socket_timeout_rate; socket_ptr -> nx_tcp_socket_timeout_retries = 0; }if (previous_ptr != socket_ptr -> nx_tcp_socket_transmit_sent_tail) { ... } else { /* The transmit list is now cleared, just set the head and tail pointers to NULL. *//* ... */ socket_ptr -> nx_tcp_socket_transmit_sent_head = NX_NULL; socket_ptr -> nx_tcp_socket_transmit_sent_tail = NX_NULL; /* Clear the transmit queue count. */ socket_ptr -> nx_tcp_socket_transmit_sent_count = 0; /* Determine if a disconnect FIN has been sent from this side of the connection. */ if ((socket_ptr -> nx_tcp_socket_state == NX_TCP_FIN_WAIT_1) || (socket_ptr -> nx_tcp_socket_state == NX_TCP_CLOSING) || (socket_ptr -> nx_tcp_socket_state == NX_TCP_LAST_ACK)) { /* Yes, setup timeout such that the FIN can be retried if it is lost. */ socket_ptr -> nx_tcp_socket_timeout = socket_ptr -> nx_tcp_socket_timeout_rate; socket_ptr -> nx_tcp_socket_timeout_retries = 0; }if ((socket_ptr -> nx_tcp_socket_state == NX_TCP_FIN_WAIT_1) || (socket_ptr -> nx_tcp_socket_state == NX_TCP_CLOSING) || (socket_ptr -> nx_tcp_socket_state == NX_TCP_LAST_ACK)) { ... } else if (socket_ptr -> nx_tcp_socket_tx_window_advertised != 0) { /* Otherwise, a FIN has not been sent, simply clear the transmit timeout. */ socket_ptr -> nx_tcp_socket_timeout = 0; }else if (socket_ptr -> nx_tcp_socket_tx_window_advertised != 0) { ... } }else { ... } /* Now walk through the packets to release and set them free. *//* ... */ while (packet_release_count--) { /* Use the previous pointer as the release pointer. */ previous_ptr = search_ptr; /* Move to the next packet in the queue before we clip the next pointer. *//* ... */ search_ptr = search_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next; /* Disable interrupts temporarily. */ TX_DISABLE /* Set the packet to allocated to indicate it is no longer part of the TCP queue. */ /*lint -e{923} suppress cast of ULONG to pointer. */ previous_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next = ((NX_PACKET *)NX_PACKET_ALLOCATED); /* Has the packet been transmitted? This is only pertinent if a retransmit of the packet occurred prior to receiving the ACK. If so, the packet could be in an ARP queue or in a driver queue waiting for transmission so we can't release it directly at this point. The driver or the ARP processing will release it when finished. *//* ... */ /*lint -e{923} suppress cast of ULONG to pointer. */ if (previous_ptr -> nx_packet_queue_next == ((NX_PACKET *)NX_DRIVER_TX_DONE)) { /* Restore interrupts. */ TX_RESTORE /* Yes, the driver has already released the packet. */ /* Open up the transmit window. */ /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ search_header_ptr = (NX_TCP_HEADER *)previous_ptr -> nx_packet_prepend_ptr; temp = search_header_ptr -> nx_tcp_header_word_3; NX_CHANGE_ULONG_ENDIAN(temp); header_length = (temp >> NX_TCP_HEADER_SHIFT) * (ULONG)sizeof(ULONG); if (socket_ptr -> nx_tcp_socket_tx_outstanding_bytes > (previous_ptr -> nx_packet_length - header_length)) { socket_ptr -> nx_tcp_socket_tx_outstanding_bytes -= previous_ptr -> nx_packet_length - header_length; }if (socket_ptr -> nx_tcp_socket_tx_outstanding_bytes > (previous_ptr -> nx_packet_length - header_length)) { ... } else { socket_ptr -> nx_tcp_socket_tx_outstanding_bytes = 0; }else { ... } /* Release the packet. */ _nx_packet_release(previous_ptr); }if (previous_ptr -> nx_packet_queue_next == ((NX_PACKET *)NX_DRIVER_TX_DONE)) { ... } else { /* No, the driver has not released the packet. */ /* Open up the transmit window. */ search_header_ptr = (NX_TCP_HEADER *)(previous_ptr -> nx_packet_ip_header + previous_ptr -> nx_packet_ip_header_length); temp = search_header_ptr -> nx_tcp_header_word_3; NX_CHANGE_ULONG_ENDIAN(temp); header_length = (temp >> NX_TCP_HEADER_SHIFT) * (ULONG)sizeof(ULONG); tcp_payload_length = (previous_ptr -> nx_packet_length - (header_length + (ULONG)((ALIGN_TYPE)search_header_ptr - (ALIGN_TYPE)(previous_ptr -> nx_packet_prepend_ptr)))); if (socket_ptr -> nx_tcp_socket_tx_outstanding_bytes > tcp_payload_length) { socket_ptr -> nx_tcp_socket_tx_outstanding_bytes -= tcp_payload_length; }if (socket_ptr -> nx_tcp_socket_tx_outstanding_bytes > tcp_payload_length) { ... } else { socket_ptr -> nx_tcp_socket_tx_outstanding_bytes = 0; }else { ... } /* Restore interrupts. */ TX_RESTORE }else { ... } }while (packet_release_count--) { ... } if (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE) { /* Only partial data are ACKed. Retransmit packet immediately. */ _nx_tcp_socket_retransmit(socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, NX_FALSE); }if (socket_ptr -> nx_tcp_socket_fast_recovery == NX_TRUE) { ... } return(NX_TRUE); }if (tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT) { ... } else { /* The ACK bit is off drop the segment and return. */ /* RFC793, Section3.9, Page72. */ return(NX_FALSE); }else { ... } }{ ... }