-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.diff
1144 lines (1110 loc) · 37.2 KB
/
tcp.diff
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
diff --git a/src/example/07_udp/netarch.c b/src/example/07_udp/netarch.c
index 4ad4817..d45b813 100644
--- a/src/example/07_udp/netarch.c
+++ b/src/example/07_udp/netarch.c
@@ -109,9 +109,6 @@ void init_global_ring() {
}
}
-static int udp_process(struct rte_mbuf *udpmbuf);
-static int udp_out(struct rte_mempool *mbuf_pool);
-
// 绑定网卡,初始化dpdk端口
static void ht_init_port(struct rte_mempool *mbuf_pool) {
@@ -302,7 +299,7 @@ void arp_request_timer_callback(__attribute__((unused)) struct rte_timer *tim,
uint32_t dst_ip = (g_local_ip & 0x00FFFFFF) | (0xFF000000 & (i << 24));
char ip_buf[16] = {0};
- printf("arp ---> src: %s ----- %d\n", inet_ntoa2(dst_ip, ip_buf), i);
+ // printf("arp ---> src: %s ----- %d\n", inet_ntoa2(dst_ip, ip_buf), i);
struct rte_mbuf* arp_buf = NULL;
uint8_t *dst_mac = ht_get_dst_macaddr(dst_ip);
@@ -323,10 +320,16 @@ void arp_request_timer_callback(__attribute__((unused)) struct rte_timer *tim,
}
/* end of free arp */
-/* udp socket */
+/* udp and tcp structure*/
+// 用来存储所有的tcp stream 是一个双向链表
+#define DEFAULT_FD_NUM 3
+
+#define MAX_FD_COUNT 1024
+
+static unsigned char fd_table[MAX_FD_COUNT] = {0};
+// udp
struct udp_sock_fd {
int fd;
-
//unsigned int status;
uint32_t localip; // ip --> mac
uint8_t localmac[RTE_ETHER_ADDR_LEN]; // 源mac地址,与ip地址要一一对应
@@ -344,24 +347,151 @@ struct udp_sock_fd {
pthread_mutex_t mutex;
};
-#define DEFAULT_FD_NUM 3
-
static struct udp_sock_fd *lhost = NULL;
-#define DEFAULT_FD_NUM 3
+// 因为arp协议存在知道ip就能查到对应的mac,所以该结构体没有mac字段
+struct offload { // 用来组装udp数据包
+ uint32_t sip; // 源ip
+ uint32_t dip; // 目的ip
+
+ uint16_t sport; // 源端口
+ uint16_t dport; // 目的端口
+
+ int protocol; // 协议
+
+ unsigned char *data; // 数据段
+ uint16_t length; // 长度
+};
+
+// tcp
+#define TCP_OPTION_LENGTH 10
+
+#define TCP_MAX_SEQ 4294967295 // 2的32次方
+
+#define TCP_INITIAL_WINDOW 14600
+
+typedef enum _HT_TCP_STATUS {
+ HT_TCP_STATUS_CLOSED = 0,
+ HT_TCP_STATUS_LISTEN,
+ HT_TCP_STATUS_SYN_RCVD,
+ HT_TCP_STATUS_SYN_SENT,
+ HT_TCP_STATUS_ESTABLISHED,
+ // 6个断开时所需的状态
+ HT_TCP_STATUS_FIN_WAIT_1,
+ HT_TCP_STATUS_FIN_WAIT_2,
+ HT_TCP_STATUS_CLOSING,
+ HT_TCP_STATUS_TIME_WAIT,
+
+ HT_TCP_STATUS_CLOSE_WAIT,
+ HT_TCP_STATUS_LAST_ACK
+} HT_TCP_STATUS;
+
+// tcp每个连接的管理结构体
+struct ht_tcp_stream { // tcp控制块
+ int fd; // 文件描述符
+
+ uint32_t sip; // 源ip
+ uint32_t dip; // 目的ip
+
+ uint16_t sport; // 源端口
+ uint16_t dport; // 目的端口
+
+ uint16_t proto; // 协议
+ // mac地址,发数据的时候装填
+ uint8_t localmac[RTE_ETHER_ADDR_LEN];
+ // ack和syn number
+ uint32_t snd_nxt; // seq number
+ uint32_t rcv_nxt; // ack number
+ // tcp状态
+ HT_TCP_STATUS status;
+
+ // 发送和接收ring
+ struct rte_ring *sndbuf;
+ struct rte_ring *rcvbuf;
+ // tcp控制块前驱和后继
+ struct ht_tcp_stream *prev;
+ struct ht_tcp_stream *next;
+
+ pthread_cond_t cond;
+ pthread_mutex_t mutex;
+};
+
+struct ht_tcp_table {
+ int count;
+ struct ht_tcp_stream *tcb_set;
+};
+
+// tcp包数据帧结构(类似于rte_tcp_hdr)
+struct ht_tcp_fragment {
+ uint16_t sport; // 源端口
+ uint16_t dport; // 目的端口
+ uint32_t seqnum; // seq number
+ uint32_t acknum; // ack number
+ uint8_t hdrlen_off;// 首部长度
+ uint8_t tcp_flags; // tcp标志
+ uint16_t windows; // 窗口大小
+ uint16_t checksum; // 校验和
+ uint16_t tcp_urp; // 紧急指针
+ // tcp option选项处理
+ uint32_t optlen;
+ uint32_t option[TCP_OPTION_LENGTH];
+
+ unsigned char *data;
+ int length;
+};
+
+static struct ht_tcp_table *g_tcp_tb = NULL;
+/* end of structure*/
+
+
+/* udp socket */
// 生成fd
static int get_fd_frombitmap(void) {
int fd = DEFAULT_FD_NUM;
+ for ( ; fd < MAX_FD_COUNT; fd++) {
+ if ((fd_table[fd/8] & (0x1 << (fd % 8))) == 0) {
+ fd_table[fd/8] |= (0x1 << (fd % 8));
+ return fd;
+ }
+ }
return fd;
}
-// 通过socket id找到对应的udp_sock_fd结构体
-static struct udp_sock_fd * get_hostinfo_fromfd(int sockfd) {
+
+static int set_fd_frombitmap(int fd) {
+ if (fd >= MAX_FD_COUNT) return -1;
+ fd_table[fd/8] &= ~(0x1 << (fd % 8));
+ return 0;
+}
+
+static struct ht_tcp_stream *get_accept_tcb(uint16_t dport) {
+ struct ht_tcp_stream *apt;
+ struct ht_tcp_table *table = g_tcp_tb;
+ for (apt = table->tcb_set; apt != NULL;apt = apt->next) {
+ if (dport == apt->dport && apt->fd == -1) {
+ return apt;
+ }
+ }
+
+ return NULL;
+}
+
+// 通过socket id找到对应的socket结构体
+static void * get_hostinfo_fromfd(int sockfd) {
struct udp_sock_fd *host;
for (host = lhost; host != NULL;host = host->next) {
if (sockfd == host->fd) {
return host;
}
}
+
+ // 找完udp,如果没有还需要再找tcp
+ struct ht_tcp_stream *stream = NULL;
+ struct ht_tcp_table *table = g_tcp_tb;
+ for (stream = table->tcb_set; stream != NULL; stream = stream->next) {
+ if (sockfd == stream->fd)
+ return stream;
+ }
+
return NULL;
}
// 通过ip和post找到对应socket去接收或者发送数据包
@@ -375,20 +505,6 @@ static struct udp_sock_fd * get_hostinfo_fromip_port(uint32_t dip, uint16_t port
return NULL;
}
-// 因为arp协议存在知道ip就能查到对应的mac,所以该结构体没有mac字段
-struct offload { // 用来组装udp数据包,理解为udp流(连接)
- uint32_t sip; // 源ip
- uint32_t dip; // 目的ip
-
- uint16_t sport; // 源端口
- uint16_t dport; // 目的端口
-
- int protocol; // 协议
-
- unsigned char *data; // 数据段
- uint16_t length; // 长度
-};
-
// udp包的处理(从旧的pkt_process中剥离出来).只做数据包的解析.
// 1.解析数据,填充offload 2.放入到recv buffer里面
static int udp_process(struct rte_mbuf *udpmbuf) {
@@ -537,68 +653,239 @@ static int udp_out(struct rte_mempool *mbuf_pool) {
return 0;
}
-// 实现的基本udp所需的socket api
+// 实现的基本的socket api
static int nsocket(__attribute__((unused)) int domain, int type, __attribute__((unused)) int protocol) {
int fd = get_fd_frombitmap(); // 1.文件描述符fd生成
- // 2.分配一个host
- struct udp_sock_fd *host = rte_malloc("udp_sock_fd", sizeof(struct udp_sock_fd), 0);
- if (host == NULL) {
- return -1;
- }
- memset(host, 0, sizeof(struct udp_sock_fd));
- // 文件描述符赋值
- host->fd = fd;
+
// 通过type赋值要传输的协议
- if (type == SOCK_DGRAM)
+ // udp socket创建
+ if (type == SOCK_DGRAM) {
+ // 2.分配一个host
+ struct udp_sock_fd *host = rte_malloc("udp_sock_fd", sizeof(struct udp_sock_fd), 0);
+ if (host == NULL) {
+ return -1;
+ }
+ memset(host, 0, sizeof(struct udp_sock_fd));
+ // 文件描述符赋值
+ host->fd = fd;
host->protocol = IPPROTO_UDP;
- /*
- else if (type == SOCK_STREAM)
- host->protocol = IPPROTO_TCP;
- */
-
- // 构建recv buffer和send buffer
- host->rcvbuf = rte_ring_create("recv buffer", RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
- if (host->rcvbuf == NULL) {
+ // 构建recv buffer和send buffer
+ host->rcvbuf = rte_ring_create("recv buffer", RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
+ if (host->rcvbuf == NULL) {
+ rte_free(host);
+ return -1;
+ }
- rte_free(host);
- return -1;
+ host->sndbuf = rte_ring_create("send buffer", RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
+ if (host->sndbuf == NULL) {
+ rte_ring_free(host->rcvbuf);
+ rte_free(host);
+ return -1;
+ }
+ // 用于实现阻塞,在recvfrom里面调用
+ pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
+ rte_memcpy(&host->cond, &blank_cond, sizeof(pthread_cond_t));
+
+ pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;
+ rte_memcpy(&host->mutex, &blank_mutex, sizeof(pthread_mutex_t));
+ // 3.该host(socket)添加到socket链表中
+ LL_ADD(host, lhost);
}
+ else if (type == SOCK_STREAM) {
+ struct ht_tcp_stream *stream = rte_malloc("ht_tcp_stream", sizeof(struct ht_tcp_stream), 0);
+ if (stream == NULL) {
+ return -1;
+ }
+ memset(stream, 0, sizeof(struct ht_tcp_stream));
- host->sndbuf = rte_ring_create("send buffer", RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
- if (host->sndbuf == NULL) {
+ stream->fd = fd;
+ stream->proto = IPPROTO_TCP;
+ stream->next = stream->prev = NULL;
- rte_ring_free(host->rcvbuf);
+ stream->rcvbuf = rte_ring_create("tcp recv buffer", RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
+ if (stream->rcvbuf == NULL) {
+ rte_free(stream);
+ return -1;
+ }
- rte_free(host);
- return -1;
- }
- // 用于实现阻塞,在recvfrom里面调用
- pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
- rte_memcpy(&host->cond, &blank_cond, sizeof(pthread_cond_t));
+ stream->sndbuf = rte_ring_create("tcp send buffer", RING_SIZE, rte_socket_id(), RING_F_SP_ENQ | RING_F_SC_DEQ);
+ if (stream->sndbuf == NULL) {
+ rte_ring_free(stream->rcvbuf);
+ rte_free(stream);
+ return -1;
+ }
- pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;
- rte_memcpy(&host->mutex, &blank_mutex, sizeof(pthread_mutex_t));
- // 3.该host(socket)添加到socket链表中
- LL_ADD(host, lhost);
+ pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
+ rte_memcpy(&stream->cond, &blank_cond, sizeof(pthread_cond_t));
+
+ pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;
+ rte_memcpy(&stream->mutex, &blank_mutex, sizeof(pthread_mutex_t));
+ struct ht_tcp_table *table = g_tcp_tb;
+ LL_ADD(stream, table->tcb_set);
+ }
return fd;
}
// 1.通过socket id找到hostinfo 2. 设置相应的ip地址
static int nbind(int sockfd, const struct sockaddr *addr,
__attribute__((unused)) socklen_t addrlen) {
- struct udp_sock_fd *host = get_hostinfo_fromfd(sockfd);
- if (host == NULL) return -1;
+ struct udp_sock_fd *hostinfo = get_hostinfo_fromfd(sockfd);
+ if (hostinfo == NULL) return -1;
- const struct sockaddr_in *laddr = (const struct sockaddr_in *)addr;
- host->localport = laddr->sin_port;
- rte_memcpy(&host->localip, &laddr->sin_addr.s_addr, sizeof(uint32_t));
- rte_memcpy(host->localmac, g_src_mac, RTE_ETHER_ADDR_LEN);
+ struct udp_sock_fd *host = (struct udp_sock_fd *)hostinfo;
+ if (host->protocol == IPPROTO_UDP) {
+ const struct sockaddr_in *laddr = (const struct sockaddr_in *)addr;
+ host->localport = laddr->sin_port;
+ rte_memcpy(&host->localip, &laddr->sin_addr.s_addr, sizeof(uint32_t));
+ rte_memcpy(host->localmac, g_src_mac, RTE_ETHER_ADDR_LEN);
+ }
+ else {
+ struct ht_tcp_stream *stream = (struct ht_tcp_stream *)hostinfo;
+ const struct sockaddr_in *laddr = (const struct sockaddr_in *)addr;
+ stream->dport = laddr->sin_port;
+ rte_memcpy(&stream->dip, &laddr->sin_addr.s_addr, sizeof(uint32_t));
+ rte_memcpy(stream->localmac, g_src_mac, RTE_ETHER_ADDR_LEN);
+ stream->status = HT_TCP_STATUS_CLOSED; // 初始状态位CLOSED,listen后状态为LISTEN
+ }
return 0;
}
+// only for tcp
+static int nlisten(int sockfd, __attribute__((unused)) int backlog) { //
+ void *hostinfo = get_hostinfo_fromfd(sockfd);
+ if (hostinfo == NULL) return -1;
+
+ struct ht_tcp_stream *stream = (struct ht_tcp_stream *)hostinfo;
+ if (stream->proto == IPPROTO_TCP) {
+ stream->status = HT_TCP_STATUS_LISTEN;
+ }
+
+ return 0;
+}
+
+static int naccept(int sockfd, struct sockaddr *addr, __attribute__((unused)) socklen_t *addrlen) {
+ void *hostinfo = get_hostinfo_fromfd(sockfd);
+ if (hostinfo == NULL) return -1;
+
+ struct ht_tcp_stream *stream = (struct ht_tcp_stream *)hostinfo;
+ if (stream->proto == IPPROTO_TCP) {
+ struct ht_tcp_stream *apt = NULL;
+ pthread_mutex_lock(&stream->mutex);
+ while((apt = get_accept_tcb(stream->dport)) == NULL) {
+ pthread_cond_wait(&stream->cond, &stream->mutex);
+ }
+ pthread_mutex_unlock(&stream->mutex);
+
+ apt->fd = get_fd_frombitmap();
+
+ struct sockaddr_in *saddr = (struct sockaddr_in *)addr;
+ saddr->sin_port = apt->sport;
+ rte_memcpy(&saddr->sin_addr.s_addr, &apt->sip, sizeof(uint32_t));
+
+ return apt->fd;
+ }
+
+ return -1;
+}
+
+
+static ssize_t nsend(int sockfd, const void *buf, size_t len,__attribute__((unused)) int flags) {
+ ssize_t length = 0;
+ void *hostinfo = get_hostinfo_fromfd(sockfd);
+ if (hostinfo == NULL) return -1;
+
+ struct ht_tcp_stream *stream = (struct ht_tcp_stream *)hostinfo;
+ if (stream->proto == IPPROTO_TCP) {
+ struct ht_tcp_fragment *fragment = rte_malloc("ht_tcp_fragment", sizeof(struct ht_tcp_fragment), 0);
+ if (fragment == NULL) {
+ return -2;
+ }
+
+ memset(fragment, 0, sizeof(struct ht_tcp_fragment));
+
+ fragment->dport = stream->sport;
+ fragment->sport = stream->dport;
+
+ fragment->acknum = stream->rcv_nxt;
+ fragment->seqnum = stream->snd_nxt;
+
+ fragment->tcp_flags = RTE_TCP_ACK_FLAG | RTE_TCP_PSH_FLAG;
+ fragment->windows = TCP_INITIAL_WINDOW;
+ fragment->hdrlen_off = 0x50;
+
+ fragment->data = rte_malloc("unsigned char *", len+1, 0);
+ if (fragment->data == NULL) {
+ rte_free(fragment);
+ return -1;
+ }
+ memset(fragment->data, 0, len+1);
+
+ rte_memcpy(fragment->data, buf, len);
+ fragment->length = len;
+ length = fragment->length;
+
+ // int nb_snd = 0;
+ rte_ring_mp_enqueue(stream->sndbuf, fragment);
+ }
+
+ return length;
+}
+
+// recv 32
+// recv
+static ssize_t nrecv(int sockfd, void *buf, size_t len, __attribute__((unused)) int flags) {
+ ssize_t length = 0;
+
+ void *hostinfo = get_hostinfo_fromfd(sockfd);
+ if (hostinfo == NULL) return -1;
+ struct ht_tcp_stream *stream = (struct ht_tcp_stream *)hostinfo;
+ if (stream->proto == IPPROTO_TCP) {
+
+ struct ht_tcp_fragment *fragment = NULL;
+ int nb_rcv = 0;
+
+ printf("rte_ring_mc_dequeue before\n");
+ pthread_mutex_lock(&stream->mutex);
+ while ((nb_rcv = rte_ring_mc_dequeue(stream->rcvbuf, (void **)&fragment)) < 0) {
+ pthread_cond_wait(&stream->cond, &stream->mutex);
+ }
+ pthread_mutex_unlock(&stream->mutex);
+ printf("rte_ring_mc_dequeue after\n");
+
+ if (fragment->length > len) {
+ rte_memcpy(buf, fragment->data, len);
+ uint32_t i = 0;
+ for(i = 0;i < fragment->length-len;i ++) {
+ fragment->data[i] = fragment->data[len+i];
+ }
+ fragment->length = fragment->length-len;
+ length = fragment->length;
+
+ rte_ring_mp_enqueue(stream->rcvbuf, fragment);
+ }
+ else if (fragment->length == 0) {
+ rte_free(fragment);
+ return 0;
+ }
+ else {
+ rte_memcpy(buf, fragment->data, fragment->length);
+ length = fragment->length;
+
+ rte_free(fragment->data);
+ fragment->data = NULL;
+
+ rte_free(fragment);
+ }
+ }
+
+ return length;
+}
+// only for tcp end
+
+// only for udp
static ssize_t nrecvfrom(int sockfd, void *buf, size_t len, __attribute__((unused)) int flags,
struct sockaddr *src_addr, __attribute__((unused)) socklen_t *addrlen) {
// 1.判断host是否存在
@@ -672,21 +959,61 @@ static ssize_t nsendto(int sockfd, const void *buf, size_t len, __attribute__((u
return len;
}
+// end of udp only
static int nclose(int fd) {
+ void *hostinfo = get_hostinfo_fromfd(fd);
+ if (hostinfo == NULL) return -1;
struct udp_sock_fd *host = get_hostinfo_fromfd(fd);
- if (host == NULL) return -1;
- // 链表中移除掉该udp_sock_fd结构
- LL_REMOVE(host, lhost);
- // 释放recv buffer和send buffer
- if (host->rcvbuf) {
- rte_ring_free(host->rcvbuf);
- }
- if (host->sndbuf) {
- rte_ring_free(host->sndbuf);
+
+ if (host->protocol == IPPROTO_UDP) {
+ // 链表中移除掉该udp_sock_fd结构
+ LL_REMOVE(host, lhost);
+ // 释放recv buffer和send buffer
+ if (host->rcvbuf) {
+ rte_ring_free(host->rcvbuf);
+ }
+ if (host->sndbuf) {
+ rte_ring_free(host->sndbuf);
+ }
+
+ rte_free(host);
+
+ set_fd_frombitmap(fd);
}
+ else if (host->protocol == IPPROTO_TCP) {
+ struct ht_tcp_stream *stream = (struct ht_tcp_stream*)hostinfo;
+
+ if (stream->status != HT_TCP_STATUS_LISTEN) {
+
+ struct ht_tcp_fragment *fragment = rte_malloc("ht_tcp_fragment", sizeof(struct ht_tcp_fragment), 0);
+ if (fragment == NULL) return -1;
+
+ printf("nclose --> enter last ack\n");
+ fragment->data = NULL;
+ fragment->length = 0;
+ fragment->sport = stream->dport;
+ fragment->dport = stream->sport;
+
+ fragment->seqnum = stream->snd_nxt;
+ fragment->acknum = stream->rcv_nxt;
+
+ fragment->tcp_flags = RTE_TCP_FIN_FLAG | RTE_TCP_ACK_FLAG;
+ fragment->windows = TCP_INITIAL_WINDOW;
+ fragment->hdrlen_off = 0x50;
- rte_free(host);
+ rte_ring_mp_enqueue(stream->sndbuf, fragment);
+ stream->status = HT_TCP_STATUS_LAST_ACK;
+
+ set_fd_frombitmap(fd);
+ }
+ else { // nsocket
+ struct ht_tcp_table *table = g_tcp_tb;
+ LL_REMOVE(stream, table->tcb_set);
+
+ rte_free(stream);
+ }
+ }
}
#define UDP_APP_RECV_BUFFER_SIZE 128
@@ -728,6 +1055,490 @@ static int udp_server_entry(__attribute__((unused)) void *arg) {
}
/* end of udp socket */
+/* tcp socket */
+// 初始化tcp表
+void init_tcp_table() {
+ if (g_tcp_tb == NULL) {
+ g_tcp_tb = rte_malloc("ht_tcp_table", sizeof(struct ht_tcp_table), 0);
+ memset(g_tcp_tb, 0, sizeof(struct ht_tcp_table));
+ }
+}
+
+// 查找tcp stream(连接)
+struct ht_tcp_stream * ht_tcp_stream_search(uint32_t sip, uint32_t dip, uint16_t sport, uint16_t dport) { // proto就是tcp
+ // 获取tcp table
+ struct ht_tcp_table *table = g_tcp_tb;
+ struct ht_tcp_stream *iter;
+
+ for (iter = table->tcb_set;iter != NULL; iter = iter->next) {
+ // 通过四元组查找到具体连接
+ if (iter->sip == sip && iter->dip == dip &&
+ iter->sport == sport && iter->dport == dport) {
+ return iter;
+ }
+ }
+ // 连接已经建立
+ for (iter = table->tcb_set; iter != NULL; iter->next) {
+ if (iter->dport == dport && iter->status == HT_TCP_STATUS_LISTEN) { // listen
+ return iter;
+ }
+ }
+ return NULL;
+}
+
+struct ht_tcp_stream *ht_tcp_stream_create(uint32_t sip, uint32_t dip, uint16_t sport, uint16_t dport) {
+ // tcp --> status(需要设置初始状态)
+ struct ht_tcp_stream *stream = rte_malloc("ht_tcp_stream", sizeof(struct ht_tcp_stream), 0);
+ if (stream == NULL) return NULL;
+ // 初始化stream
+ stream->sip = sip;
+ stream->dip = dip;
+ stream->sport = sport;
+ stream->dport = dport;
+ stream->proto = IPPROTO_TCP;
+ stream->fd = -1;
+ // 这里状态设置为LISTENE状态.在服务器的时候初始状态为LISTEN.客户端则为CLOSE.这里为了简化没有做区分
+ stream->status = HT_TCP_STATUS_LISTEN;
+
+ printf("ht_tcp_stream_create\n");
+ // 该tcp stream的recv buffer和send buffer初始化
+ stream->sndbuf = rte_ring_create("sndbuf", RING_SIZE, rte_socket_id(), 0);
+ stream->rcvbuf = rte_ring_create("rcvbuf", RING_SIZE, rte_socket_id(), 0);
+ // seq num 该值是一个随机值
+ uint32_t next_seed = time(NULL);
+ stream->snd_nxt = rand_r(&next_seed) % TCP_MAX_SEQ;
+ // 绑定local mac
+ rte_memcpy(stream->localmac, g_src_mac, RTE_ETHER_ADDR_LEN);
+
+ pthread_cond_t blank_cond = PTHREAD_COND_INITIALIZER;
+ rte_memcpy(&stream->cond, &blank_cond, sizeof(pthread_cond_t));
+
+ pthread_mutex_t blank_mutex = PTHREAD_MUTEX_INITIALIZER;
+ rte_memcpy(&stream->mutex, &blank_mutex, sizeof(pthread_mutex_t));
+
+ // listen后再把当前创建的stream加入到tcp table中
+ // struct ht_tcp_table *table = g_tcp_tb;
+ // LL_ADD(stream, table->tcb_set);
+
+ return stream;
+}
+
+static int ht_tcp_handle_listen(struct ht_tcp_stream *stream, struct rte_tcp_hdr *tcphdr, struct rte_ipv4_hdr *iphdr) {
+ if (tcphdr->tcp_flags & RTE_TCP_SYN_FLAG) { // tcp头带有SYN FLAG时,要进行listean状态处理
+ // tcp可能重复发送,仅处理一次
+ if (stream->status == HT_TCP_STATUS_LISTEN) {
+ struct ht_tcp_table *table = g_tcp_tb;
+ struct ht_tcp_stream *stream = ht_tcp_stream_create(iphdr->src_addr, iphdr->dst_addr, tcphdr->src_port, tcphdr->dst_port);
+ LL_ADD(stream, table->tcb_set);
+
+ // 创建一个tcp数据包
+ struct ht_tcp_fragment *fragment = rte_malloc("ht_tcp_fragment", sizeof(struct ht_tcp_fragment), 0);
+ if (fragment == NULL) return -1; // 分配失败
+ memset(fragment, 0, sizeof(struct ht_tcp_fragment));
+ // 填充fragment即填充tcp数据包,服务器和客户端的dst和src是相反的
+ fragment->sport = tcphdr->dst_port;
+ fragment->dport = tcphdr->src_port;
+
+ char src_ip[16] = {0};
+ printf("tcp ---> src: %s:%d ", inet_ntoa2(stream->sip, src_ip), rte_ntohs(tcphdr->src_port));
+
+ char dst_ip[16] = {0};
+ printf(" ---> dst: %s:%d \n", inet_ntoa2(stream->dip, dst_ip), rte_ntohs(tcphdr->dst_port));
+ // seq num等于创建stream里的seq值
+ fragment->seqnum = stream->snd_nxt;
+ fragment->acknum = rte_ntohl(tcphdr->sent_seq) + 1; // 大端序网络字节序要转为小端字节序
+ stream->rcv_nxt = fragment->acknum;
+
+ fragment->tcp_flags = (RTE_TCP_SYN_FLAG | RTE_TCP_ACK_FLAG);
+ fragment->windows = TCP_INITIAL_WINDOW; // 这个值是linux内核中的值
+ fragment->hdrlen_off = 0x50; // 首部长度 20字节
+
+ fragment->data = NULL;
+ fragment->length = 0;
+ // 把tcp数据包放入到该stream里的send buffer里
+ rte_ring_mp_enqueue(stream->sndbuf, fragment);
+ // 状态修改为SYN_RECV
+ stream->status = HT_TCP_STATUS_SYN_RCVD;
+ }
+
+ }
+
+ return 0;
+}
+// syn_recv状态的处理.
+static int ht_tcp_handle_syn_rcvd(struct ht_tcp_stream *stream, struct rte_tcp_hdr *tcphdr) {
+ if (tcphdr->tcp_flags & RTE_TCP_ACK_FLAG) { // tcp标志中存在ACK标志
+ // 避免重复的包
+ if (stream->status == HT_TCP_STATUS_SYN_RCVD) {
+ // 获取收到的ack的值
+ uint32_t acknum = rte_ntohl(tcphdr->recv_ack);
+ if (acknum == stream->snd_nxt + 1) {
+ // to do
+ }
+ stream->status = HT_TCP_STATUS_ESTABLISHED; // 改变tcp状态为ESTABLISHED
+
+ // accept
+ struct ht_tcp_stream *listener = ht_tcp_stream_search(0, 0, 0, stream->dport);
+ if (listener == NULL)
+ rte_exit(EXIT_FAILURE, "ht_tcp_stream_search failed\n");
+
+ pthread_mutex_lock(&listener->mutex);
+ pthread_cond_signal(&listener->cond);
+ pthread_mutex_unlock(&listener->mutex);
+ }
+ }
+ return 0;
+}
+
+static int ht_tcp_enqueue_recvbuffer(struct ht_tcp_stream *stream, struct rte_tcp_hdr *tcphdr, int tcplen) {
+ // recv buffer
+ struct ht_tcp_fragment *rfragment = rte_malloc("ht_tcp_fragment", sizeof(struct ht_tcp_fragment), 0);
+ if (rfragment == NULL) return -1;
+ memset(rfragment, 0, sizeof(struct ht_tcp_fragment));
+
+ rfragment->dport = rte_ntohs(tcphdr->dst_port);
+ rfragment->sport = rte_ntohs(tcphdr->src_port);
+
+ uint8_t hdrlen = tcphdr->data_off >> 4;
+ int payloadlen = tcplen - hdrlen * 4;
+ if (payloadlen > 0) {
+ uint8_t *payload = (uint8_t*)tcphdr + hdrlen * 4;
+
+ rfragment->data = rte_malloc("unsigned char *", payloadlen+1, 0);
+ if (rfragment->data == NULL) {
+ rte_free(rfragment);
+ return -1;
+ }
+ memset(rfragment->data, 0, payloadlen+1);
+
+ rte_memcpy(rfragment->data, payload, payloadlen);
+ rfragment->length = payloadlen;
+ }
+ else if (payloadlen == 0) {
+ rfragment->length = 0;
+ rfragment->data = NULL;
+ }
+ rte_ring_mp_enqueue(stream->rcvbuf, rfragment);
+
+ pthread_mutex_lock(&stream->mutex);
+ pthread_cond_signal(&stream->cond);
+ pthread_mutex_unlock(&stream->mutex);
+
+ return 0;
+}
+
+static int ht_tcp_send_ackpkt(struct ht_tcp_stream *stream, struct rte_tcp_hdr *tcphdr) {
+ struct ht_tcp_fragment *ackfrag = rte_malloc("ht_tcp_fragment", sizeof(struct ht_tcp_fragment), 0);
+ if (ackfrag == NULL) return -1;
+ memset(ackfrag, 0, sizeof(struct ht_tcp_fragment));
+
+ ackfrag->dport = tcphdr->src_port;
+ ackfrag->sport = tcphdr->dst_port;
+
+ // remote
+ printf("ht_tcp_send_ackpkt: %d, %d\n", stream->rcv_nxt, rte_ntohs(tcphdr->sent_seq));
+
+ ackfrag->acknum = stream->rcv_nxt;
+ ackfrag->seqnum = stream->snd_nxt;
+
+ ackfrag->tcp_flags = RTE_TCP_ACK_FLAG;
+ ackfrag->windows = TCP_INITIAL_WINDOW;
+ ackfrag->hdrlen_off = 0x50;
+ ackfrag->data = NULL;
+ ackfrag->length = 0;
+
+ rte_ring_mp_enqueue(stream->sndbuf, ackfrag);
+
+ return 0;
+}
+// 建立连接后处理
+static int ht_tcp_handle_established(struct ht_tcp_stream *stream, struct rte_tcp_hdr *tcphdr, int tcplen) {
+ if (tcphdr->tcp_flags & RTE_TCP_SYN_FLAG) {
+ // todo
+ }
+
+ if (tcphdr->tcp_flags & RTE_TCP_PSH_FLAG) { //
+ // recv buffer
+ ht_tcp_enqueue_recvbuffer(stream, tcphdr, tcplen);
+
+ uint8_t hdrlen = tcphdr->data_off >> 4;
+ int payloadlen = tcplen - hdrlen * 4;
+
+ stream->rcv_nxt = stream->rcv_nxt + payloadlen;
+ stream->snd_nxt = rte_ntohl(tcphdr->recv_ack);
+ // Push状态需要发包
+ ht_tcp_send_ackpkt(stream, tcphdr);
+ }
+ if (tcphdr->tcp_flags & RTE_TCP_ACK_FLAG) {
+ // ack不处理
+ }
+ if (tcphdr->tcp_flags & RTE_TCP_FIN_FLAG) {
+ // fin需要发终止包
+ stream->status = HT_TCP_STATUS_CLOSE_WAIT;
+ ht_tcp_enqueue_recvbuffer(stream, tcphdr, tcphdr->data_off >> 4);
+
+ // send ack ptk
+ stream->rcv_nxt = stream->rcv_nxt + 1;
+ stream->snd_nxt = rte_ntohl(tcphdr->recv_ack);
+
+ ht_tcp_send_ackpkt(stream, tcphdr);
+ }
+
+ return 0;
+}
+
+static int ht_tcp_handle_close_wait(struct ht_tcp_stream *stream, struct rte_tcp_hdr *tcphdr) {
+ if (tcphdr->tcp_flags & RTE_TCP_FIN_FLAG) { //
+ if (stream->status == HT_TCP_STATUS_CLOSE_WAIT) {
+ // todo
+ }
+ }
+
+ return 0;
+}
+
+static int ht_tcp_handle_last_ack(struct ht_tcp_stream *stream, struct rte_tcp_hdr *tcphdr) {
+ if (tcphdr->tcp_flags & RTE_TCP_ACK_FLAG) {
+ if (stream->status == HT_TCP_STATUS_LAST_ACK) {
+ stream->status = HT_TCP_STATUS_CLOSED;
+ printf("ht_tcp_handle_last_ack\n");
+
+ struct ht_tcp_table *table = g_tcp_tb;
+ LL_REMOVE(stream, table->tcb_set);
+
+ rte_ring_free(stream->sndbuf);
+ rte_ring_free(stream->rcvbuf);
+
+ rte_free(stream);
+ }
+ }
+
+ return 0;
+}
+
+static int tcp_process(struct rte_mbuf *tcpmbuf) {
+ struct rte_ipv4_hdr *iphdr = rte_pktmbuf_mtod_offset(tcpmbuf, struct rte_ipv4_hdr *,
+ sizeof(struct rte_ether_hdr));
+ struct rte_tcp_hdr *tcphdr = (struct rte_tcp_hdr *)(iphdr + 1);
+ // 对比校验和
+ uint16_t tcpcksum = tcphdr->cksum;
+ tcphdr->cksum = 0; // 先置0初始化
+ // tcp check sum调用rte_ipv4_udptcp_cksum后无需大端转小端,其内部做了处理
+ uint16_t cksum = rte_ipv4_udptcp_cksum(iphdr, tcphdr);
+ if (cksum != tcpcksum) {
+ printf("cksum: %x, tcp cksum: %x\n", cksum, tcpcksum);
+ return -1;
+ }
+ // 通过源地址,目的地址,源端口,目的端口找到对应的stream(即对应的tcp连接)
+ struct ht_tcp_stream *stream = ht_tcp_stream_search(iphdr->src_addr, iphdr->dst_addr,
+ tcphdr->src_port, tcphdr->dst_port);
+ if (stream == NULL) return -2;
+
+ // 存在该stream,判断当前stream的tcp状态进行对应的处理
+ switch (stream->status) {
+
+ case HT_TCP_STATUS_CLOSED: //client
+ break;
+
+ case HT_TCP_STATUS_LISTEN: // server
+ ht_tcp_handle_listen(stream, tcphdr, iphdr);
+ break;
+
+ case HT_TCP_STATUS_SYN_RCVD: // server
+ ht_tcp_handle_syn_rcvd(stream, tcphdr);
+ break;
+
+ case HT_TCP_STATUS_SYN_SENT: // client
+ break;
+
+ case HT_TCP_STATUS_ESTABLISHED: { // server | client
+ // tcp包的长度等于包的长度减去ipv4头
+ int tcplen = rte_ntohs(iphdr->total_length) - sizeof(struct rte_ipv4_hdr);
+
+ ht_tcp_handle_established(stream, tcphdr, tcplen);
+ break;
+ }
+
+ case HT_TCP_STATUS_FIN_WAIT_1: // ~client
+ break;
+
+ case HT_TCP_STATUS_FIN_WAIT_2: // ~client
+ break;
+
+ case HT_TCP_STATUS_CLOSING: // ~client
+ break;
+
+ case HT_TCP_STATUS_TIME_WAIT: // ~client
+ break;
+
+ case HT_TCP_STATUS_CLOSE_WAIT: // ~server
+ ht_tcp_handle_close_wait(stream, tcphdr);
+ break;
+
+ case HT_TCP_STATUS_LAST_ACK: // ~server
+ ht_tcp_handle_last_ack(stream, tcphdr);
+ break;
+ }
+
+ return 0;
+}
+
+
+static int ht_encode_tcp_apppkt(uint8_t *msg, uint32_t sip, uint32_t dip,
+ uint8_t *srcmac, uint8_t *dstmac, struct ht_tcp_fragment *fragment) {
+
+ // encode
+ const unsigned total_len = fragment->length + sizeof(struct rte_ether_hdr) +
+ sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_tcp_hdr) +
+ fragment->optlen * sizeof(uint32_t);
+
+ // 1 ethhdr
+ struct rte_ether_hdr *eth = (struct rte_ether_hdr *)msg;
+ rte_memcpy(eth->s_addr.addr_bytes, srcmac, RTE_ETHER_ADDR_LEN);
+ rte_memcpy(eth->d_addr.addr_bytes, dstmac, RTE_ETHER_ADDR_LEN);
+ eth->ether_type = rte_htons(RTE_ETHER_TYPE_IPV4);
+
+ // 2 iphdr
+ struct rte_ipv4_hdr *ip = (struct rte_ipv4_hdr *)(msg + sizeof(struct rte_ether_hdr));
+ ip->version_ihl = 0x45;
+ ip->type_of_service = 0;
+ ip->total_length = rte_htons(total_len - sizeof(struct rte_ether_hdr));
+ ip->packet_id = 0;
+ ip->fragment_offset = 0;
+ ip->time_to_live = 64; // ttl = 64
+ ip->next_proto_id = IPPROTO_TCP;
+ ip->src_addr = sip;
+ ip->dst_addr = dip;
+
+ ip->hdr_checksum = 0;
+ ip->hdr_checksum = rte_ipv4_cksum(ip);
+
+ // 3 tcphdr
+ struct rte_tcp_hdr *tcp = (struct rte_tcp_hdr *)(msg + sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr));
+ tcp->src_port = fragment->sport;
+ tcp->dst_port = fragment->dport;
+ tcp->sent_seq = rte_htonl(fragment->seqnum);
+ tcp->recv_ack = rte_htonl(fragment->acknum);
+
+ tcp->data_off = fragment->hdrlen_off;
+ tcp->rx_win = fragment->windows;
+ tcp->tcp_urp = fragment->tcp_urp;
+ tcp->tcp_flags = fragment->tcp_flags;
+ // 如果payload不为NULL则需要拷贝
+ if (fragment->data != NULL) {
+ uint8_t *payload = (uint8_t*)(tcp+1) + fragment->optlen * sizeof(uint32_t);
+ rte_memcpy(payload, fragment->data, fragment->length);
+ }
+
+ tcp->cksum = 0;
+ tcp->cksum = rte_ipv4_udptcp_cksum(ip, tcp);
+
+ return 0;
+}
+
+struct rte_mbuf * ht_tcp_pkt(struct rte_mempool *mbuf_pool, uint32_t sip, uint32_t dip,
+ uint8_t *srcmac, uint8_t *dstmac, struct ht_tcp_fragment *fragment) {
+ // mempool --> mbuf
+ const unsigned total_len = fragment->length + sizeof(struct rte_ether_hdr) +
+ sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_tcp_hdr) +
+ fragment->optlen * sizeof(uint32_t); // tcp头里还包含了option的值
+
+ struct rte_mbuf *mbuf = rte_pktmbuf_alloc(mbuf_pool);
+ if (!mbuf) {
+ rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");
+ }
+ mbuf->pkt_len = total_len;
+ mbuf->data_len = total_len;
+
+ uint8_t *pktdata = rte_pktmbuf_mtod(mbuf, uint8_t*);
+
+ ht_encode_tcp_apppkt(pktdata, sip, dip, srcmac, dstmac, fragment);
+
+ return mbuf;
+}
+
+// tcp发送流程
+int tcp_out(struct rte_mempool *mbuf_pool) {
+ // 获取tcp table
+ struct ht_tcp_table *table = g_tcp_tb;
+ // 遍历所有的连接是否有数据发送
+ struct ht_tcp_stream *stream;
+ for (stream = table->tcb_set; stream != NULL; stream = stream->next) {
+ if (stream->sndbuf == NULL) continue;
+
+ struct ht_tcp_fragment *fragment = NULL;
+ int nb_snd = rte_ring_mc_dequeue(stream->sndbuf, (void**)&fragment); // send buffer中取出一个packet
+ if (nb_snd < 0) continue;
+ // 如果目的ip存在与arp table中
+ uint8_t *dstmac = ht_get_dst_macaddr(stream->sip); // dmac是客户端的源ip
+ if (dstmac == NULL) {
+ // 目的mac为空,发送arp request
+ struct rte_mbuf *arpbuf = ht_send_arp(mbuf_pool, RTE_ARP_OP_REQUEST, g_default_arp_mac,
+ stream->dip, stream->sip);
+ // 发送arp包不直接发送,直接放入到send_ring中即可
+ struct ring_buffer *ring = g_ring;
+ rte_ring_mp_enqueue_burst(g_ring->send_ring, (void **)&arpbuf, 1, NULL);
+ // 把该tcp fragment放入到对应stream的send buffer里
+ rte_ring_mp_enqueue(stream->sndbuf, fragment);