-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
3459 lines (3372 loc) · 154 KB
/
main.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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright 2023 Kenichi Yasukata
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/* configuration */
#ifndef IIP_CONF_ENDIAN
#define IIP_CONF_ENDIAN (1) /* little 1, big 2 */
#endif
/* TODO: properly configure them */
#ifndef IIP_CONF_IP4_TTL
#define IIP_CONF_IP4_TTL (64)
#endif
#ifndef IIP_CONF_TCP_OPT_WS
#define IIP_CONF_TCP_OPT_WS (7U) /* RFC 7323 : between 0 ~ 14 */ /* TODO: how to determine this ? */
#endif
#ifndef IIP_CONF_TCP_OPT_MSS
#define IIP_CONF_TCP_OPT_MSS (1460U)
#endif
#ifndef IIP_CONF_TCP_OPT_SACK_OK
#define IIP_CONF_TCP_OPT_SACK_OK (1U)
#endif
#ifndef IIP_CONF_TCP_RX_BUF_CNT
#define IIP_CONF_TCP_RX_BUF_CNT (512U) /* should be smaller than 735439 (1GB with 1460 mss) : limit by RFC 7323 */
#endif
#ifndef IIP_CONF_TCP_WIN_INIT
#define IIP_CONF_TCP_WIN_INIT (1)
#endif
#ifndef IIP_CONF_TCP_MSL_SEC
#define IIP_CONF_TCP_MSL_SEC (1) /* maximum segment lifetime, in second : RFC 793 recommends 2 min, but we can choose as we wish */
#endif
#ifndef IIP_CONF_TCP_RETRANS_CNT
#define IIP_CONF_TCP_RETRANS_CNT (4) /* maximum retransmission count */
#endif
#ifndef IIP_CONF_TCP_SSTHRESH_INIT
#define IIP_CONF_TCP_SSTHRESH_INIT (256)
#endif
#ifndef IIP_CONF_TCP_CONN_HT_SIZE
#define IIP_CONF_TCP_CONN_HT_SIZE (829) /* generally bigger is faster at the cost of memory consumption */
#endif
#ifndef IIP_CONF_TCP_TIMESTAMP_ENABLE
#define IIP_CONF_TCP_TIMESTAMP_ENABLE (1)
#endif
#ifndef IIP_CONF_L2ADDR_LEN_MAX
#define IIP_CONF_L2ADDR_LEN_MAX (6)
#endif
/* functions implemented by app and io subsystems */
static void *iip_ops_pkt_alloc(void *);
static void iip_ops_pkt_free(void *, void *);
static void *iip_ops_pkt_get_data(void *, void *);
static uint16_t iip_ops_pkt_get_len(void *, void *);
static void iip_ops_pkt_set_len(void *, uint16_t, void *);
static void iip_ops_pkt_increment_head(void *, uint16_t, void *);
static void iip_ops_pkt_decrement_tail(void *, uint16_t, void *);
static void *iip_ops_pkt_clone(void *, void *); /* assuming the entire packet chain is cloned while reference counts to the payload buffers are also incremented */
static void iip_ops_pkt_scatter_gather_chain_append(void *, void *, void *);
static void *iip_ops_pkt_scatter_gather_chain_get_next(void *, void *);
static uint16_t iip_ops_l2_hdr_len(void *, void *);
static uint8_t *iip_ops_l2_hdr_src_ptr(void *, void *);
static uint8_t *iip_ops_l2_hdr_dst_ptr(void *, void *);
static uint16_t iip_ops_l2_ethertype_be(void *, void *);
static uint16_t iip_ops_l2_addr_len(void *);
static void iip_ops_l2_broadcast_addr(uint8_t [], void *);
static void iip_ops_l2_hdr_craft(void *, uint8_t [], uint8_t [], uint16_t, void *);
static uint8_t iip_ops_l2_skip(void *, void *);
static void iip_ops_l2_flush(void *);
static void iip_ops_l2_push(void *, void *); /* assuming packet object is released by app */
static uint8_t iip_ops_arp_lhw(void *);
static uint8_t iip_ops_arp_lproto(void *);
static void iip_ops_arp_reply(void *, void *, void *);
static void iip_ops_icmp_reply(void *, void *, void *);
static uint8_t iip_ops_tcp_accept(void *, void *, void *);
static void *iip_ops_tcp_accepted(void *, void *, void *, void *);
static void *iip_ops_tcp_connected(void *, void *, void *, void *);
static void iip_ops_tcp_closed(void *, uint8_t [], uint32_t, uint16_t, uint8_t [], uint32_t, uint16_t, void *, void *);
static void iip_ops_tcp_payload(void *, void *, void *, void *, uint16_t, uint16_t, void *);
static void iip_ops_tcp_acked(void *, void *, void *, void *, void *);
static void iip_ops_udp_payload(void *, void *, void *);
static uint8_t iip_ops_nic_feature_offload_tx_scatter_gather(void *);
static uint8_t iip_ops_nic_feature_offload_ip4_rx_checksum(void *);
static uint8_t iip_ops_nic_feature_offload_ip4_tx_checksum(void *);
static uint8_t iip_ops_nic_feature_offload_tcp_rx_checksum(void *);
static uint8_t iip_ops_nic_feature_offload_tcp_tx_checksum(void *);
static uint8_t iip_ops_nic_feature_offload_tcp_tx_tso(void *);
static uint8_t iip_ops_nic_feature_offload_udp_rx_checksum(void *);
static uint8_t iip_ops_nic_feature_offload_udp_tx_checksum(void *);
static uint8_t iip_ops_nic_feature_offload_udp_tx_tso(void *);
static uint8_t iip_ops_nic_offload_ip4_rx_checksum(void *, void *);
static uint8_t iip_ops_nic_offload_udp_rx_checksum(void *, void *);
static uint8_t iip_ops_nic_offload_tcp_rx_checksum(void *, void *);
static void iip_ops_nic_offload_ip4_tx_checksum_mark(void *, void *);
static void iip_ops_nic_offload_tcp_tx_checksum_mark(void *, void *);
static void iip_ops_nic_offload_tcp_tx_tso_mark(void *, void *);
static void iip_ops_nic_offload_udp_tx_checksum_mark(void *, void *);
static void iip_ops_nic_offload_udp_tx_tso_mark(void *, void *);
static void iip_ops_util_now_ns(uint32_t [3], void *);
/* utilities */
#define __iip_bsw16(_n) ((((_n) >> 8) | ((_n) << 8)) & 0xffff)
#define __iip_bsw32(_n) (((((_n) >> 24) & 0xff) | (((_n) >> 8) & 0xff00) | (((_n) << 8) & 0xff0000) | (((_n) << 24) & 0xff000000)) & 0xffffffff)
#if IIP_CONF_ENDIAN == 1
#define __iip_htons(_n) __iip_bsw16(_n)
#define __iip_htonl(_n) __iip_bsw32(_n)
#define __iip_ntohs(_n) __iip_bsw16(_n)
#define __iip_ntohl(_n) __iip_bsw32(_n)
#elif IIP_CONF_ENDIAN == 2
#define __iip_htons(_n) (_n)
#define __iip_htonl(_n) (_n)
#define __iip_ntohs(_n) (_n)
#define __iip_ntohl(_n) (_n)
#else
#error "invalid IIP_CONF_ENDIAN: it should be either 1 (little) or 2 (big)"
#endif
#ifndef __iip_memcpy
#define __iip_memcpy(__dest, __src, __n) \
do { \
uint32_t __i; \
for (__i = 0; __i < (uint32_t)(__n); __i++) \
((uint8_t *) __dest)[__i] = ((uint8_t *) __src)[__i]; \
} while (0)
#endif
#ifndef __iip_memset
#define __iip_memset(__s, __c, __n) \
do { \
uint32_t __i; \
for (__i = 0; __i < (uint32_t)(__n); __i++) \
((uint8_t *) __s)[__i] = __c; \
} while (0)
#endif
#ifndef __iip_memcmp
static uint32_t __iip_memcmp_impl(void *__s1, void *__s2, uint32_t __n)
{
uint32_t __i = 0, ret = 0;
for (__i = 0; __i < (uint32_t)(__n); __i++) {
if (((uint8_t *) __s1)[__i] != ((uint8_t *) __s2)[__i]) {
ret = __i + 1;
break;
}
}
return ret;
}
#define __iip_memcmp(__s1, __s2, __n) __iip_memcmp_impl(__s1, __s2, __n)
#endif
#ifndef __iip_memmove
#define __iip_memmove(__dst, __src, __n) \
do { \
if ((uintptr_t) (__dst) > (uintptr_t) (__src)) { \
uint32_t __i = 0; \
for (__i = (uint32_t)(__n) - 1; __i != (uint32_t) -1; __i--) { \
((uint8_t *) (__dst))[__i] = ((uint8_t *) (__src))[__i]; \
} \
} else if ((uintptr_t) (__dst) < (uintptr_t) (__src)) { \
uint32_t __i = 0; \
for (__i = 0; __i < (uint32_t)(__n); __i++) { \
((uint8_t *) (__dst))[__i] = ((uint8_t *) (__src))[__i]; \
} \
} \
} while (0)
#endif
#ifndef __iip_assert
#define __iip_assert(_cond) \
do { \
if (!(_cond)) { \
IIP_OPS_DEBUG_PRINTF("[%s:%u]: assertion fail\n", __FILE__, __LINE__); \
while (1) ; \
} \
} while (0)
#endif
static uint16_t __iip_netcsum16(uint8_t *__b[], uint16_t __l[], uint16_t __c, uint16_t __m)
{
uint32_t __r = 0; uint16_t __n; uint8_t __k;
for (__n = 0, __k = 0; __n < (__c); __n++) {
uint32_t __i, __o = __k;
for (__i = __k; __i < (__l)[__n]; ) {
if ((__k == 1) || ((__l)[__n] - __i == 1)) {
uint16_t __v = ((uint8_t *)((__b)[__n]))[__i] & 0x00ff;
if (__k == 0) {
__v <<= 8;
__k = 1;
} else {
__k = 0;
}
__r += __v;
__i += 1;
} else {
uint16_t __v = ((uint16_t *)((__b)[__n]))[__i / 2 + __o];
__r += __iip_htons(__v);
__i += 2;
}
}
}
__r -= (__m);
__r = (__r >> 16) + (__r & 0x0000ffff);
__r = (__r >> 16) + __r;
return (uint16_t)~((uint16_t) __r);
}
#define __iip_round_up(_v, _r) ((((_v) / (_r)) + ((_v) % (_r) ? 1 : 0)) * (_r))
#define __iip_dequeue_obj(__queue, __obj, __x) \
do { \
if ((__queue)[0] == (__obj)) \
(__queue)[0] = (__obj)->next[__x]; \
if ((__queue)[1] == (__obj)) \
(__queue)[1] = (__obj)->prev[__x]; \
if ((__obj)->prev[__x]) \
(__obj)->prev[__x]->next[__x] = (__obj)->next[__x]; \
if ((__obj)->next[__x]) \
(__obj)->next[__x]->prev[__x] = (__obj)->prev[__x]; \
(__obj)->prev[__x] = (__obj)->next[__x] = NULL; \
} while (0)
#define __iip_enqueue_obj(__queue, __obj, __x) \
do { \
(__obj)->prev[__x] = (__obj)->next[__x] = NULL; \
if (!((__queue)[0])) { \
(__queue)[0] = (__queue)[1] = (__obj); \
} else { \
(__obj)->next[__x] = NULL; \
(__obj)->prev[__x] = (__queue)[1]; \
(__queue)[1]->next[__x] = (__obj); \
(__queue)[1] = (__obj); \
} \
} while (0)
#define __iip_q_for_each_safe(__queue, _i, _n, __x) \
for ((_i) = (__queue)[0], _n = ((_i) ? _i->next[__x] : (NULL)); (_i); (_i) = _n, _n = ((_i) ? (_i)->next[__x] : (NULL)))
#define __iip_q_for_each_safe_reverse(__queue, _i, _n, __x) \
for ((_i) = (__queue)[1], _n = ((_i) ? _i->prev[__x] : (NULL)); (_i); (_i) = _n, _n = ((_i) ? (_i)->prev[__x] : (NULL)))
/* protocol headers */
struct iip_ip4_hdr {
uint8_t vl;
uint8_t tos;
uint16_t len_be;
uint16_t id_be;
uint16_t off_be;
uint8_t ttl;
uint8_t proto;
uint16_t csum_be;
uint32_t src_be;
uint32_t dst_be;
};
struct iip_arp_hdr {
uint16_t hw_be;
uint16_t proto_be;
uint8_t lhw;
uint8_t lproto;
uint16_t op_be;
};
struct iip_icmp_hdr {
uint8_t type;
uint8_t code;
uint16_t csum_be;
struct {
uint16_t id_be;
uint16_t seq_be;
} echo;
};
struct iip_l4_ip4_pseudo_hdr {
uint32_t ip4_src_be;
uint32_t ip4_dst_be;
uint8_t pad;
uint8_t proto;
uint16_t len_be;
};
struct iip_tcp_hdr {
uint16_t src_be;
uint16_t dst_be;
uint32_t seq_be;
uint32_t ack_seq_be;
uint16_t flags;
uint16_t win_be;
uint16_t csum_be;
uint16_t urg_p_be;
};
struct iip_udp_hdr {
uint16_t src_be;
uint16_t dst_be;
uint16_t len_be;
uint16_t csum_be;
};
#define PB_IP4(__b) ((struct iip_ip4_hdr *)((uintptr_t) (iip_ops_pkt_get_data((__b), opaque)) + iip_ops_l2_hdr_len((__b), opaque)))
#define PB_ARP(__b) ((struct iip_arp_hdr *)(PB_IP4(__b)))
#define PB_ARP_HW_SENDER(__b) ((uint8_t *)((uintptr_t) PB_ARP(__b) + sizeof(struct iip_arp_hdr)))
#define PB_ARP_IP_SENDER(__b) ((uint8_t *)((uintptr_t) PB_ARP_HW_SENDER(__b) + PB_ARP(__b)->lhw))
#define PB_ARP_HW_TARGET(__b) ((uint8_t *)((uintptr_t) PB_ARP_IP_SENDER(__b) + PB_ARP(__b)->lproto))
#define PB_ARP_IP_TARGET(__b) ((uint8_t *)((uintptr_t) PB_ARP_HW_TARGET(__b) + PB_ARP(__b)->lhw))
#define PB_ICMP(__b) ((struct iip_icmp_hdr *)((uintptr_t) PB_IP4(__b) + (PB_IP4(__b)->vl & 0x0f) * 4))
#define PB_ICMP_PAYLOAD(__b) ((uint8_t *)((uintptr_t) PB_ICMP(__b) + sizeof(struct iip_icmp_hdr)))
#define PB_ICMP_PAYLOAD_LEN(__b) ((uint16_t)(__iip_htons(PB_IP4(__b)->len_be) - (PB_IP4(__b)->vl & 0x0f) * 4 - sizeof(struct iip_icmp_hdr)))
#define PB_TCP(__b) ((struct iip_tcp_hdr *)((uintptr_t) PB_IP4(__b) + (PB_IP4(__b)->vl & 0x0f) * 4))
#define PB_TCP_HDR_LEN(__b) ((uint16_t) __iip_ntohs(PB_TCP(__b)->flags) >> 12)
#define PB_TCP_HDR_HAS_FIN(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x01U) ? 1 : 0)
#define PB_TCP_HDR_HAS_SYN(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x02U) ? 1 : 0)
#define PB_TCP_HDR_HAS_RST(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x04U) ? 1 : 0)
#define PB_TCP_HDR_HAS_PSH(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x08U) ? 1 : 0)
#define PB_TCP_HDR_HAS_ACK(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x10U) ? 1 : 0)
#define PB_TCP_HDR_HAS_URG(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x20U) ? 1 : 0)
#define PB_TCP_HDR_HAS_ECE(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x40U) ? 1 : 0)
#define PB_TCP_HDR_HAS_CWR(__b) ((((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)) & 0x80U) ? 1 : 0)
#define PB_TCP_HDR_SET_LEN(__b, __l) do { PB_TCP(__b)->flags = (__iip_htons(((__l) << 12) | ((uint8_t)(__iip_ntohs(PB_TCP(__b)->flags) & 0x3fU)))); } while (0)
#define PB_TCP_HDR_SET_FLAGS(__b, __f) do { PB_TCP(__b)->flags = (PB_TCP(__b)->flags & __iip_htons(~0x3fU)) | __iip_htons(__f); } while (0)
#define PB_TCP_PAYLOAD(__b) ((uint8_t *)((uintptr_t) PB_TCP(__b) + PB_TCP_HDR_LEN(__b) * 4))
#define PB_TCP_PAYLOAD_LEN(__b) ((uint16_t)(__iip_htons(PB_IP4(__b)->len_be) - (PB_IP4(__b)->vl & 0x0f) * 4 - PB_TCP_HDR_LEN(__b) * 4))
#define PB_TCP_OPT(__b) ((uint8_t *)((uintptr_t) PB_TCP(__b) + sizeof(struct iip_tcp_hdr)))
#define PB_TCP_OPTLEN(__b) (PB_TCP_HDR_LEN(__b) * 4 - sizeof(struct iip_tcp_hdr))
#define PB_UDP(__b) ((struct iip_udp_hdr *)((uintptr_t) PB_IP4(__b) + (PB_IP4(__b)->vl & 0x0f) * 4))
#define PB_UDP_PAYLOAD(__b) ((uint8_t *)((uintptr_t) PB_UDP(__b) + sizeof(struct iip_udp_hdr)))
#define PB_UDP_PAYLOAD_LEN(__b) ((uint16_t)(__iip_ntohs(PB_UDP(__b)->len_be)) - sizeof(struct iip_udp_hdr))
/* data structures */
struct pb {
void *pkt;
void *buf;
void *ack_cb_pkt;
uint8_t flags;
#define __IIP_PB_FLAGS_NEED_ACK_CB_PKT_FREE (1U << 2)
#define __IIP_PB_FLAGS_OPT_HAS_TS (1U << 3)
#define __IIP_PB_FLAGS_SACKED (1U << 4)
#define __IIP_PB_FLAGS_SACK_REPLY_SEND_ALL (1U << 5)
void *orig_pkt; /* for no scatter gather mode */
uint32_t ts;
uint32_t a_cnt; /* arrival count */
struct {
uint32_t rto_ms;
uint16_t inc_head;
uint16_t dec_tail;
struct {
uint16_t sack_opt_off;
uint32_t ts[2];
} opt;
} tcp;
struct {
uint16_t to_be_updated;
struct {
uint16_t increment_head;
uint16_t decrement_tail;
} range[0xffff /* max frame size*/ / 512 /* minimum mtu */];
} clone;
struct pb *prev[1];
struct pb *next[1];
};
#define __IIP_TCP_STATE_CLOSED (1)
#define __IIP_TCP_STATE_SYN_SENT (2)
#define __IIP_TCP_STATE_SYN_RECVD (3)
#define __IIP_TCP_STATE_ESTABLISHED (4)
#define __IIP_TCP_STATE_CLOSING (5)
#define __IIP_TCP_STATE_FIN_WAIT1 (6)
#define __IIP_TCP_STATE_FIN_WAIT2 (7)
#define __IIP_TCP_STATE_TIME_WAIT (8)
#define __IIP_TCP_STATE_CLOSE_WAIT (9)
#define __IIP_TCP_STATE_LAST_ACK (10)
struct iip_tcp_conn {
uint8_t state;
uint16_t local_port_be;
uint16_t peer_port_be;
uint32_t local_ip4_be;
uint32_t peer_ip4_be;
uint8_t local_mac[IIP_CONF_L2ADDR_LEN_MAX];
uint8_t peer_mac[IIP_CONF_L2ADDR_LEN_MAX];
uint32_t seq_be;
uint32_t ack_seq_be;
uint16_t win_be;
/* management */
uint32_t acked_seq;
uint32_t ts; /* latest timestamp of peer host */
uint16_t peer_win;
uint32_t ack_seq_sent;
uint32_t seq_next_expected;
uint8_t dup_ack_received;
uint32_t time_wait_ts_ms;
uint32_t retrans_cnt;
uint8_t flags;
#define __IIP_TCP_CONN_FLAGS_PEER_RX_FAILED (1U << 5)
uint32_t do_ack_cnt;
uint32_t sent_seq_when_loss_detected;
uint32_t fin_ack_seq_be;
uint8_t ws;
uint8_t sack_ok;
uint16_t mss;
uint32_t a_cnt; /* arrival count */
struct { /* number of packet buffer (not in byte) for rx */
uint32_t limit; /* we should have 1GB (735439) as the max : 1GB / 1460 (mss) = 735439.6.. */
uint32_t used;
} rx_buf_cnt;
struct {
uint16_t ssthresh;
uint16_t win;
} cc; /* congestion control */
struct {
uint32_t srtt; /* smoothed RTT estimator */
uint32_t rttvar; /* smoothed mean RTT estimator */
} rtt;
void *opaque;
struct pb *tcp_sack_rx[2];
struct pb *head[5][2]; /* 0: rx, 1: tx, 2: tx sent, 3: tx retrans, 4: rx out-of-order */
struct iip_tcp_conn *prev[2];
struct iip_tcp_conn *next[2];
};
struct workspace {
struct {
struct pb *ip4_rx_fragment[2];
} queue;
struct {
uint32_t p_cnt;
struct pb *p[2];
struct iip_tcp_conn *conn[2];
} pool;
struct {
uint32_t prev_fast;
uint32_t prev_slow;
uint32_t prev_very_slow;
} timer;
struct {
uint32_t iss; /* initial send sequence number */
uint32_t pkt_ts;
struct iip_tcp_conn *conns[2];
struct iip_tcp_conn *conns_ht[IIP_CONF_TCP_CONN_HT_SIZE][2];
struct iip_tcp_conn *closed_conns[2];
} tcp;
struct {
uint32_t prev_print_ts;
struct {
uint32_t rx_pkt;
uint32_t rx_pkt_dupack;
uint32_t rx_pkt_keepalive;
uint32_t rx_pkt_winupdate;
uint32_t tx_pkt;
uint32_t tx_pkt_re;
uint32_t fc_stop;
uint32_t cc_stop;
uint32_t th_stop;
} tcp;
} monitor;
};
/* pb allocator */
static void __iip_free_pb(struct workspace *s, struct pb *p, void *opaque)
{
if (p->flags & __IIP_PB_FLAGS_NEED_ACK_CB_PKT_FREE)
iip_ops_pkt_free(p->ack_cb_pkt, opaque);
iip_ops_pkt_free(p->pkt, opaque);
if (p->orig_pkt)
iip_ops_pkt_free(p->orig_pkt, opaque);
__iip_memset(p, 0, sizeof(struct pb));
#define __iip_enqueue_obj_top(__queue, __obj, __x) \
do { \
(__obj)->prev[__x] = (__obj)->next[__x] = NULL; \
if (!((__queue)[0])) { \
(__queue)[0] = (__queue)[1] = (__obj); \
} else { \
(__queue)[0]->prev[__x] = (__obj); \
(__obj)->next[__x] = (__queue)[0]; \
(__queue)[0] = (__obj); \
} \
} while (0)
__iip_enqueue_obj_top(s->pool.p, p, 0);
#undef __iip_enqueue_obj_top
s->pool.p_cnt++;
}
static struct pb *__iip_alloc_pb(struct workspace *s, void *pkt, void *opaque)
{
struct pb *p = s->pool.p[0];
__iip_assert(p);
__iip_assert(pkt);
__iip_dequeue_obj(s->pool.p, p, 0);
p->pkt = pkt;
p->buf = iip_ops_pkt_get_data(p->pkt, opaque);
s->pool.p_cnt--;
return p;
}
static struct pb *__iip_clone_pb(struct workspace *s, struct pb *orig, void *opaque)
{
struct pb *p = s->pool.p[0];
__iip_assert(p);
__iip_dequeue_obj(s->pool.p, p, 0);
p->pkt = iip_ops_pkt_clone(orig->pkt, opaque);
__iip_assert(p->pkt);
if (orig->ack_cb_pkt) {
p->ack_cb_pkt = iip_ops_pkt_clone(orig->ack_cb_pkt, opaque);
__iip_assert(p->ack_cb_pkt);
}
if (orig->orig_pkt) {
p->orig_pkt = iip_ops_pkt_clone(orig->orig_pkt, opaque);
__iip_assert(p->orig_pkt);
}
p->buf = orig->buf;
p->flags = orig->flags;
p->ts = orig->ts;
p->a_cnt = orig->a_cnt;
__iip_memcpy(&p->tcp, &orig->tcp, sizeof(p->tcp));
__iip_memcpy(&p->clone, &orig->clone, sizeof(p->clone));
/* we do not touch queue entity */
s->pool.p_cnt--;
return p;
}
/* exported utilitiy functions */
static uint32_t iip_workspace_size(void)
{
return sizeof(struct workspace);
}
static uint32_t iip_pb_size(void)
{
return sizeof(struct pb);
}
static uint32_t iip_tcp_conn_size(void)
{
return sizeof(struct iip_tcp_conn);
}
static void iip_add_pb(void *_mem, void *_p)
{
__iip_enqueue_obj(((struct workspace *) _mem)->pool.p, (struct pb *) _p, 0);
((struct workspace *) _mem)->pool.p_cnt++;
}
static void iip_add_tcp_conn(void *_mem, void *_conn)
{
__iip_enqueue_obj(((struct workspace *) _mem)->pool.conn, (struct iip_tcp_conn *) _conn, 0);
}
/* protocol stack implementation */
static void iip_arp_request(void *_mem,
uint8_t local_mac[],
uint32_t local_ip4_be,
uint32_t target_ip4_be,
void *opaque)
{
void *out_pkt = iip_ops_pkt_alloc(opaque);
__iip_assert(out_pkt);
{
uint8_t bc_mac[IIP_CONF_L2ADDR_LEN_MAX];
iip_ops_l2_broadcast_addr(bc_mac, opaque);
iip_ops_l2_hdr_craft(out_pkt, local_mac, bc_mac, __iip_htons(0x0806), opaque);
}
{
struct iip_arp_hdr *arph = PB_ARP(out_pkt);
arph->hw_be = __iip_htons(0x0001);
arph->proto_be = __iip_htons(0x0800);
arph->lhw = iip_ops_arp_lhw(opaque);
arph->lproto = iip_ops_arp_lproto(opaque);
arph->op_be = __iip_htons(0x0001);
__iip_memcpy(PB_ARP_HW_SENDER(out_pkt), local_mac, 6);
__iip_memset(PB_ARP_HW_TARGET(out_pkt), 0, 6);
__iip_memcpy(PB_ARP_IP_SENDER(out_pkt), (uint8_t *) &local_ip4_be, 4);
__iip_memcpy(PB_ARP_IP_TARGET(out_pkt), (uint8_t *) &target_ip4_be, 4);
iip_ops_pkt_set_len(out_pkt, iip_ops_l2_hdr_len(out_pkt, opaque) + sizeof(struct iip_arp_hdr) + arph->lhw * 2 + arph->lproto * 2, opaque);
}
iip_ops_l2_push(out_pkt, opaque);
{ /* unused */
(void) _mem;
}
}
static uint16_t __iip_tcp_push(struct workspace *s,
struct iip_tcp_conn *conn, void *_pkt,
uint8_t syn, uint8_t ack, uint8_t fin, uint8_t rst,
uint16_t tcp_flags,
uint8_t *sackbuf, void *opaque)
{
void *pkt;
struct pb *out_p;
uint16_t total_payload_len = (_pkt ? iip_ops_pkt_get_len(_pkt, opaque) : 0), payload_len = total_payload_len, pushed_payload_len = 0, frag_cnt = 0;
again:
frag_cnt++;
pkt = _pkt;
out_p = __iip_alloc_pb(s, iip_ops_pkt_alloc(opaque), opaque);
if (!iip_ops_nic_feature_offload_tcp_tx_tso(opaque)) {
uint16_t l = (conn->mss < 536 ? 536 : conn->mss) - (0 /* size of ip option */ + __iip_round_up((syn ? 4 + 3 + (IIP_CONF_TCP_OPT_SACK_OK ? 2 : 0) : 0) + (sackbuf ? sackbuf[1] : 0) + (IIP_CONF_TCP_TIMESTAMP_ENABLE ? 10 : 0), 4)) /* size of tcp option */;
payload_len = (l < (uint16_t) (total_payload_len - pushed_payload_len) ? l : total_payload_len - pushed_payload_len);
if (payload_len != total_payload_len) {
__iip_assert((pkt = iip_ops_pkt_clone(_pkt, opaque)) != NULL);
iip_ops_pkt_increment_head(pkt, pushed_payload_len, opaque);
iip_ops_pkt_set_len(pkt, payload_len, opaque);
}
}
iip_ops_l2_hdr_craft(out_p->pkt, conn->local_mac, conn->peer_mac, __iip_htons(0x0800), opaque);
{
struct iip_ip4_hdr *ip4h = PB_IP4(out_p->pkt);
ip4h->vl = (4 /* ver ipv4 */ << 4) | (sizeof(struct iip_ip4_hdr) / 4 /* len in octet */);
ip4h->len_be = __iip_htons(sizeof(struct iip_ip4_hdr) + __iip_round_up(sizeof(struct iip_tcp_hdr) + (syn ? 4 + 3 + (IIP_CONF_TCP_OPT_SACK_OK ? 2 : 0) : 0) + (sackbuf ? sackbuf[1] : 0) + (IIP_CONF_TCP_TIMESTAMP_ENABLE ? 10 : 0), 4) + payload_len);
ip4h->tos = 0;
ip4h->id_be = 0; /* no ip4 fragment */
ip4h->off_be = 0; /* no ip4 fragment */
ip4h->ttl = IIP_CONF_IP4_TTL;
ip4h->proto = 6; /* tcp */
ip4h->src_be = conn->local_ip4_be;
ip4h->dst_be = conn->peer_ip4_be;
ip4h->csum_be = 0;
if (!iip_ops_nic_feature_offload_ip4_tx_checksum(opaque)) { /* ip4 csum */
uint8_t *_b[1]; _b[0] = (uint8_t *) ip4h;
{
uint16_t _l[1]; _l[0] = (uint16_t) ((ip4h->vl & 0x0f) * 4);
ip4h->csum_be = __iip_htons(__iip_netcsum16(_b, _l, 1, 0));
}
} else
iip_ops_nic_offload_ip4_tx_checksum_mark(out_p->pkt, opaque);
}
__iip_assert(conn->rx_buf_cnt.limit < (1U << 30) /* 1GB limit : RFC 7323 */ / IIP_CONF_TCP_OPT_MSS);
{
struct iip_tcp_hdr *tcph = PB_TCP(out_p->pkt);
tcph->src_be = conn->local_port_be;
tcph->dst_be = conn->peer_port_be;
tcph->seq_be = conn->seq_be;
tcph->ack_seq_be = conn->ack_seq_be;
tcph->flags = 0;
PB_TCP_HDR_SET_LEN(out_p->pkt, __iip_round_up(sizeof(struct iip_tcp_hdr) + (syn ? 4 + 3 + (IIP_CONF_TCP_OPT_SACK_OK ? 2 : 0) : 0) + (sackbuf ? sackbuf[1] : 0) + (IIP_CONF_TCP_TIMESTAMP_ENABLE ? 10 : 0), 4) / 4);
PB_TCP_HDR_SET_FLAGS(out_p->pkt, (syn ? 0x02U : 0) | (ack ? 0x10U : 0) | (rst ? 0x04U : 0) | (fin ? 0x01U : 0) | tcp_flags);
tcph->win_be = __iip_htons((uint16_t) (((conn->rx_buf_cnt.limit - conn->rx_buf_cnt.used) * IIP_CONF_TCP_OPT_MSS) >> IIP_CONF_TCP_OPT_WS));
tcph->urg_p_be = 0;
tcph->csum_be = 0;
{
uint8_t *optbuf = PB_TCP_OPT(out_p->pkt), optlen = 0;
{
if (syn) { /* mss */
optbuf[optlen + 0] = 2;
optbuf[optlen + 1] = 4;
*((uint16_t *) &(optbuf[optlen + 2])) = __iip_htons(IIP_CONF_TCP_OPT_MSS);
optlen += optbuf[optlen + 1];
}
if (syn) { /* window scale */
optbuf[optlen + 0] = 3;
optbuf[optlen + 1] = 3;
__iip_assert(IIP_CONF_TCP_OPT_WS < 15); /* RFC 7323 */
optbuf[optlen + 2] = IIP_CONF_TCP_OPT_WS;
optlen += optbuf[optlen + 1];
}
if (syn && IIP_CONF_TCP_OPT_SACK_OK) { /* sack ok */
optbuf[optlen + 0] = 4;
optbuf[optlen + 1] = 2;
optlen += optbuf[optlen + 1];
}
if (sackbuf) { /* sack */
__iip_memcpy(&optbuf[optlen], sackbuf, sackbuf[1]);
optlen += sackbuf[1];
}
if (IIP_CONF_TCP_TIMESTAMP_ENABLE) { /* time stamp */
optbuf[optlen + 0] = 8;
optbuf[optlen + 1] = 10;
((uint32_t *) &optbuf[optlen + 2])[0] = __iip_htonl(s->tcp.pkt_ts);
((uint32_t *) &optbuf[optlen + 2])[1] = __iip_htonl(conn->ts);
optlen += optbuf[optlen + 1];
}
__iip_assert(PB_TCP_HDR_LEN(out_p->pkt) == __iip_round_up(sizeof(struct iip_tcp_hdr) + optlen, 4) / 4); /* we already have configured */
}
__iip_memset(&optbuf[optlen], 0, PB_TCP_HDR_LEN(out_p->pkt) * 4 - optlen);
}
if (!iip_ops_nic_feature_offload_tcp_tx_checksum(opaque)) {
struct iip_l4_ip4_pseudo_hdr _pseudo;
_pseudo.ip4_src_be = conn->local_ip4_be;
_pseudo.ip4_dst_be = conn->peer_ip4_be;
_pseudo.pad = 0;
_pseudo.proto = 6;
_pseudo.len_be = __iip_htons(PB_TCP_HDR_LEN(out_p->pkt) * 4 + payload_len);
{
uint8_t *_b[3]; _b[0] = (uint8_t *) &_pseudo; _b[1] = (uint8_t *) tcph; _b[2] = (pkt ? (uint8_t *) iip_ops_pkt_get_data(pkt, opaque) : NULL);
{
uint16_t _l[3]; _l[0] = sizeof(_pseudo); _l[1] = (uint16_t) (PB_TCP_HDR_LEN(out_p->pkt) * 4); _l[2] = payload_len;
tcph->csum_be = __iip_htons(__iip_netcsum16(_b, _l, 3, 0));
}
}
} else
iip_ops_nic_offload_tcp_tx_checksum_mark(out_p->pkt, opaque); /* relies on the value of tcp hdr len on packet buf */
}
if (iip_ops_nic_feature_offload_tx_scatter_gather(opaque)) {
if (pkt) iip_ops_pkt_scatter_gather_chain_append(out_p->pkt, pkt, opaque);
iip_ops_pkt_set_len(out_p->pkt, iip_ops_l2_hdr_len(out_p->pkt, opaque) + (PB_IP4(out_p->pkt)->vl & 0x0f) * 4 + PB_TCP_HDR_LEN(out_p->pkt) * 4, opaque);
} else {
if (pkt) __iip_memcpy(PB_TCP_PAYLOAD(out_p->pkt), iip_ops_pkt_get_data(pkt, opaque), payload_len);
iip_ops_pkt_set_len(out_p->pkt, iip_ops_l2_hdr_len(out_p->pkt, opaque) + (PB_IP4(out_p->pkt)->vl & 0x0f) * 4 + PB_TCP_HDR_LEN(out_p->pkt) * 4 + payload_len, opaque);
if (pkt) out_p->orig_pkt = pkt;
}
conn->seq_be = __iip_htonl(__iip_ntohl(conn->seq_be) + payload_len + syn + fin);
if (ack)
conn->ack_seq_sent = __iip_ntohl(PB_TCP(out_p->pkt)->ack_seq_be);
if (iip_ops_nic_feature_offload_tcp_tx_tso(opaque))
iip_ops_nic_offload_tcp_tx_tso_mark(out_p->pkt, opaque);
__iip_enqueue_obj(conn->head[1], out_p, 0);
pushed_payload_len += payload_len;
if (pushed_payload_len != total_payload_len)
goto again;
else {
out_p->ack_cb_pkt = _pkt;
if (1 < frag_cnt)
out_p->flags |= __IIP_PB_FLAGS_NEED_ACK_CB_PKT_FREE;
}
return 0;
}
static uint16_t iip_tcp_send(void *_mem, void *_handle, void *pkt, uint16_t tcp_flags, void *opaque)
{
struct iip_tcp_conn *conn = (struct iip_tcp_conn *) _handle;
if (conn->state != __IIP_TCP_STATE_ESTABLISHED)
return 0;
return __iip_tcp_push((struct workspace *) _mem, conn, pkt, 0, 1, 0, 0, tcp_flags, NULL, opaque);
}
static uint16_t iip_tcp_close(void *_mem, void *_handle, void *opaque)
{
struct iip_tcp_conn *conn = (struct iip_tcp_conn *) _handle;
if (conn->state == __IIP_TCP_STATE_ESTABLISHED) {
conn->state = __IIP_TCP_STATE_FIN_WAIT1;
IIP_OPS_DEBUG_PRINTF("%p: TCP_STATE_FIN_WAIT1\n", (void *) conn);
{
uint16_t ret = __iip_tcp_push((struct workspace *) _mem, conn, NULL, 0, 1, 1, 0, 0, NULL, opaque);
conn->fin_ack_seq_be = conn->seq_be;
return ret;
}
} else
return 0;
}
static void iip_tcp_rxbuf_consumed(void *_mem, void *_handle, uint16_t cnt, void *opaque)
{
struct iip_tcp_conn *conn = (struct iip_tcp_conn *) _handle;
__iip_assert(cnt <= conn->rx_buf_cnt.used);
conn->rx_buf_cnt.used -= cnt;
{ /* unused */
(void) _mem;
(void) opaque;
}
}
static void __iip_tcp_conn_init(struct workspace *s, struct iip_tcp_conn *conn,
uint8_t local_mac[], uint32_t local_ip4_be, uint16_t local_port_be,
uint8_t peer_mac[], uint32_t peer_ip4_be, uint16_t peer_port_be,
uint8_t state, void *opaque)
{
__iip_memset(conn, 0, sizeof(*conn));
__iip_memcpy(conn->local_mac, local_mac, sizeof(conn->local_mac));
conn->local_ip4_be = local_ip4_be;
conn->local_port_be = local_port_be;
__iip_memcpy(conn->peer_mac, peer_mac, sizeof(conn->peer_mac));
conn->peer_ip4_be = peer_ip4_be;
conn->peer_port_be = peer_port_be;
conn->seq_be = __iip_htonl(s->tcp.iss);
conn->ack_seq_be = 0;
conn->acked_seq = 0xffff; /* to differentiate from ack number for Dup ACK detection */
conn->state = state;
conn->rx_buf_cnt.limit = IIP_CONF_TCP_RX_BUF_CNT;
conn->cc.win = IIP_CONF_TCP_WIN_INIT;
conn->cc.ssthresh = IIP_CONF_TCP_SSTHRESH_INIT;
conn->rtt.srtt = 0;
conn->rtt.rttvar = 24;
__iip_assert(conn->rx_buf_cnt.limit * IIP_CONF_TCP_OPT_MSS < (1U << 30));
conn->win_be = __iip_htons((conn->rx_buf_cnt.limit * IIP_CONF_TCP_OPT_MSS) >> IIP_CONF_TCP_OPT_WS);
__iip_enqueue_obj(s->tcp.conns, conn, 0);
__iip_enqueue_obj(s->tcp.conns_ht[(conn->peer_ip4_be + conn->local_port_be + conn->peer_port_be) % IIP_CONF_TCP_CONN_HT_SIZE], conn, 1);
__iip_assert(conn->rx_buf_cnt.used < conn->rx_buf_cnt.limit);
{ /* unused */
(void) opaque;
}
}
static uint16_t iip_tcp_connect(void *_mem,
uint8_t local_mac[], uint32_t local_ip4_be, uint16_t local_port_be,
uint8_t peer_mac[], uint32_t peer_ip4_be, uint16_t peer_port_be,
void *opaque)
{
struct workspace *s = (struct workspace *) _mem;
struct iip_tcp_conn *conn = s->pool.conn[0];
__iip_assert(conn);
__iip_dequeue_obj(s->pool.conn, conn, 0);
__iip_tcp_conn_init(s, conn, local_mac, local_ip4_be, local_port_be, peer_mac, peer_ip4_be, peer_port_be, __IIP_TCP_STATE_SYN_SENT, opaque);
return __iip_tcp_push(s, conn, NULL, 1, 0, 0, 0, 0, NULL, opaque);
}
static uint16_t iip_udp_send(void *_mem,
uint8_t local_mac[], uint32_t local_ip4_be, uint16_t local_port_be,
uint8_t peer_mac[], uint32_t peer_ip4_be, uint16_t peer_port_be,
void *pkt, void *opaque)
{
void *out_pkt = iip_ops_pkt_alloc(opaque);
uint16_t payload_len = (pkt ? iip_ops_pkt_get_len(pkt, opaque) : 0);
__iip_assert(out_pkt);
iip_ops_l2_hdr_craft(out_pkt, local_mac, peer_mac, __iip_htons(0x0800), opaque);
{
struct iip_ip4_hdr *ip4h = PB_IP4(out_pkt);
ip4h->vl = (4 /* ver ipv4 */ << 4) | (sizeof(struct iip_ip4_hdr) / 4 /* len in octet */);
ip4h->len_be = __iip_htons((ip4h->vl & 0x0f) * 4 + sizeof(struct iip_udp_hdr) + payload_len);
ip4h->tos = 0;
ip4h->id_be = 0; /* no ip4 fragment */
ip4h->off_be = 0; /* no ip4 fragment */
ip4h->ttl = IIP_CONF_IP4_TTL;
ip4h->proto = 17; /* udp */
ip4h->src_be = local_ip4_be;
ip4h->dst_be = peer_ip4_be;
ip4h->csum_be = 0;
if (!iip_ops_nic_feature_offload_ip4_tx_checksum(opaque)) { /* ip4 csum */
uint8_t *_b[1]; _b[0] = (uint8_t *) ip4h;
{
uint16_t _l[1]; _l[0] = (uint16_t) ((ip4h->vl & 0x0f) * 4);
ip4h->csum_be = __iip_htons(__iip_netcsum16(_b, _l, 1, 0));
}
} else
iip_ops_nic_offload_ip4_tx_checksum_mark(out_pkt, opaque);
{
struct iip_udp_hdr *udph = PB_UDP(out_pkt);
udph->src_be = local_port_be;
udph->dst_be = peer_port_be;
udph->len_be = __iip_htons(sizeof(struct iip_udp_hdr) + payload_len);
udph->csum_be = 0;
if (!iip_ops_nic_feature_offload_udp_tx_checksum(opaque)) { /* udp csum */
struct iip_l4_ip4_pseudo_hdr _pseudo;
_pseudo.ip4_src_be = local_ip4_be;
_pseudo.ip4_dst_be = peer_ip4_be;
_pseudo.pad = 0;
_pseudo.proto = 17;
_pseudo.len_be = __iip_htons(sizeof(struct iip_udp_hdr) + payload_len);
{
uint8_t *_b[3]; _b[0] = (uint8_t *) &_pseudo; _b[1] = (uint8_t *) udph; _b[2] = (pkt ? (uint8_t *) iip_ops_pkt_get_data(pkt, opaque) : NULL);
{
uint16_t _l[3]; _l[0] = sizeof(_pseudo); _l[1] = sizeof(struct iip_udp_hdr); _l[2] = payload_len;
udph->csum_be = __iip_htons(__iip_netcsum16(_b, _l, 3, 0));
}
}
} else
iip_ops_nic_offload_udp_tx_checksum_mark(out_pkt, opaque);
if (iip_ops_nic_feature_offload_tx_scatter_gather(opaque)) {
if (pkt) iip_ops_pkt_scatter_gather_chain_append(out_pkt, pkt, opaque);
iip_ops_pkt_set_len(out_pkt, iip_ops_l2_hdr_len(out_pkt, opaque) + (ip4h->vl & 0x0f) * 4 + sizeof(struct iip_udp_hdr), opaque);
} else {
if (pkt) __iip_memcpy(&((uint8_t *) iip_ops_pkt_get_data(out_pkt, opaque))[iip_ops_l2_hdr_len(out_pkt, opaque) + (ip4h->vl & 0x0f) * 4 + sizeof(struct iip_udp_hdr)], iip_ops_pkt_get_data(pkt, opaque), payload_len);
iip_ops_pkt_set_len(out_pkt, iip_ops_l2_hdr_len(out_pkt, opaque) + (ip4h->vl & 0x0f) * 4 + __iip_ntohs(udph->len_be), opaque);
if (pkt) iip_ops_pkt_free(pkt, opaque);
}
}
}
if (iip_ops_nic_feature_offload_udp_tx_tso(opaque))
iip_ops_nic_offload_udp_tx_tso_mark(out_pkt, opaque);
iip_ops_l2_push(out_pkt, opaque);
return 0;
{ /* unused */
(void) _mem;
}
}
static uint16_t iip_run(void *_mem, uint8_t mac[], uint32_t ip4_be, void *pkt[], uint16_t cnt, uint32_t *next_us, void *opaque)
{
struct workspace *s = (struct workspace *) _mem;
uint16_t ret = 0;
uint32_t _next_us = 1000000UL; /* 1 sec */
uint32_t now_ms;
{
uint32_t t[3];
iip_ops_util_now_ns(t, opaque);
now_ms = (t[1] * 1000UL + t[2] / 1000000UL);
}
{ /* periodic timer */
if (200 <= now_ms - s->timer.prev_fast){ /* fast timer every 200 ms */
/* send delayed ack */
{
struct iip_tcp_conn *conn, *_conn_n;
__iip_q_for_each_safe(s->tcp.conns, conn, _conn_n, 0) {
if (conn->state == __IIP_TCP_STATE_ESTABLISHED && !conn->head[3][0]) {
if ((__iip_ntohl(conn->ack_seq_be) != conn->ack_seq_sent)) /* we got payload, but ack is not pushed by the app */
__iip_tcp_push(s, conn, NULL, 0, 1, 0, 0, 0, NULL, opaque);
}
}
}
{ /* incrment tcp packet timestamp counter */
s->tcp.pkt_ts++;
}
s->timer.prev_fast = now_ms;
}
if (500 <= now_ms - s->timer.prev_slow){ /* slow timer every 500 ms */
{ /* incrment initial send sequence number */
s->tcp.iss++; /* while RFC 793 specifies to increment every 4 us */
}
s->timer.prev_slow = now_ms;
}
if (1000 <= now_ms - s->timer.prev_very_slow) { /* slow timer every 1000 ms */
{ /* close connections */
struct iip_tcp_conn *conn, *_conn_n;
__iip_q_for_each_safe(s->tcp.conns, conn, _conn_n, 0) {
switch (conn->state) {
case __IIP_TCP_STATE_TIME_WAIT:
if (IIP_CONF_TCP_MSL_SEC * 1000U * 2 < now_ms - conn->time_wait_ts_ms) {
conn->state = __IIP_TCP_STATE_CLOSED;
__iip_dequeue_obj(s->tcp.conns_ht[(conn->peer_ip4_be + conn->local_port_be + conn->peer_port_be) % IIP_CONF_TCP_CONN_HT_SIZE], conn, 1);
__iip_dequeue_obj(s->tcp.conns, conn, 0);
__iip_enqueue_obj(s->tcp.closed_conns, conn, 0);
IIP_OPS_DEBUG_PRINTF("%p: TCP_STATE_TIME_WAIT - TCP_STATE_CLOSED (%u %u (%u %u))\n", (void *) conn, IIP_CONF_TCP_MSL_SEC * 1000U * 2, (now_ms - conn->time_wait_ts_ms) * 1000U, now_ms, conn->time_wait_ts_ms);
}
break;
default:
break;
}
}
}
s->timer.prev_very_slow = now_ms;
}
{
_next_us = (s->timer.prev_fast + 200U - now_ms < _next_us * 1000U ? (s->timer.prev_fast + 200U - now_ms) * 1000U : _next_us);
_next_us = (s->timer.prev_slow + 500U - now_ms < _next_us * 1000U ? (s->timer.prev_slow + 500U - now_ms) * 1000U : _next_us);
_next_us = (s->timer.prev_very_slow + 1000U - now_ms < _next_us * 1000U ? (s->timer.prev_very_slow + 1000U - now_ms) * 1000U : _next_us);
}
}
{ /* steer packet to an ip4_rx queue or discard after executing callback */
struct pb *ip4_rx[2] = { 0 };
{
uint16_t i;
for (i = 0; i < cnt; i++) {
if (iip_ops_l2_skip(pkt[i], opaque))
iip_ops_pkt_free(pkt[i], opaque);
else {
uint8_t pkt_used = 0;
struct pb *p = __iip_alloc_pb(s, pkt[i], opaque);
switch (__iip_ntohs(iip_ops_l2_ethertype_be(p->pkt, opaque))) {
case 0x0800: /* ip */
if (!__iip_memcmp(mac, iip_ops_l2_hdr_dst_ptr(p->pkt, opaque), iip_ops_l2_addr_len(opaque))) {
/*IIP_OPS_DEBUG_PRINTF("ip4-in : src-ip %u.%u.%u.%u dst-ip %u.%u.%u.%u v %u, l %u, proto %u\n",
(PB_IP4(p->pkt)->src_be >> 0) & 0x0ff,
(PB_IP4(p->pkt)->src_be >> 8) & 0x0ff,
(PB_IP4(p->pkt)->src_be >> 16) & 0x0ff,
(PB_IP4(p->pkt)->src_be >> 24) & 0x0ff,
(PB_IP4(p->pkt)->dst_be >> 0) & 0x0ff,
(PB_IP4(p->pkt)->dst_be >> 8) & 0x0ff,
(PB_IP4(p->pkt)->dst_be >> 16) & 0x0ff,
(PB_IP4(p->pkt)->dst_be >> 24) & 0x0ff,
PB_IP4(p->pkt)->vl >> 4, PB_IP4(p->pkt)->l,
PB_IP4(p->pkt)->proto);*/
if ((PB_IP4(p->pkt)->vl >> 4) != 4) { /* ip version*/