aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv4/tcp_mytcp.c
blob: 56cf935a336965242b2a295a78dbf3b165c3fd7f (plain) (blame)
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
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/inet_diag.h>
//#include </home/tcp/linux-5.7/linux-5.7.10/arch/arm/include/asm/div64.h>
//#include <div64.h>
//#include <math.h>

#include <net/tcp.h>
#include <linux/time.h>    // for gettimeofday()
#include <linux/kernel.h>
#include "/home/player/Desktop/ohos/src/kernel/linux/linux-5.10/include/net/mac80211.h"
#include "/home/player/Desktop/ohos/src/kernel/linux/linux-5.10/drivers/net/wireless/ath/ath10k/core.h"
#include "tcp_vegas.h"

#include "tcp_mytcp.h"

//extern unsigned long mytcp_max_tx_queue;
//extern unsigned long mytcp_actual_max_queue;
//extern unsigned long mytcp_actual_min_queue;
//extern unsigned long mytcp_min_tx_queue;
//extern unsigned long receive_num_in_RTT;
//extern unsigned long mytcp_round_count;
//extern unsigned long mytcp_round_max;

//extern void __skb_tstamp_tx;

//extern int mytcp_flow_num;
//int this_flow_num;

static int gamma = 2;
static int RQ_allow = 10;

module_param(gamma, int, 0644);
MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");


static void mytcp_enable(struct sock *sk)
{
	struct tcp_sock *tp = tcp_sk(sk);
	//struct tcp_sock *tp = tcp_sk(sk);
	struct mytcp *mytcp = inet_csk_ca(sk);

	/* Begin taking mytcp samples next time we send something. */
//	printk(KERN_DEBUG "enable mytcp\n");
	mytcp->doing_mytcp_now = 1;
	tp->mytcp_enable_flag = 1;

	/* Set the beginning of the next send window. */
	
	mytcp->beg_snd_nxt = 0x7fffffff;
//	mytcp->minRTT = 0x7fffffff;
//	mytcp->slow_start_end = 0;
	mytcp->full_flag_1 = 0;
	mytcp->full_flag_2 = 0;
	mytcp->full_flag_3 = 0;
	mytcp->cong_flag = 0;
//	mytcp->bytes_rtt = 0;
//	receive_num_in_RTT = 0;
//	mytcp_round_count = 0;
	tp->mytcp_round_flag = 0;
	mytcp->min_rtt = 1000000;

	mytcp->time_now = 0;
//	mytcp->time_last = 0;

//	mytcp->num_i = 0;


}

/* Stop taking mytcp samples for now. */
static inline void mytcp_disable(struct sock *sk)
{
	struct mytcp *mytcp = inet_csk_ca(sk);
	struct tcp_sock *tp = tcp_sk(sk);

	mytcp->doing_mytcp_now = 0;
	tp->mytcp_enable_flag = 0;
}

void tcp_mytcp_init(struct sock *sk)
{
	struct mytcp *mytcp = inet_csk_ca(sk);
	//struct tcp_sock *tp = tcp_sk(sk);
	mytcp->baseRTT = 0x7fffffff;
	mytcp->max_queue_within_t = 0;
	mytcp->min_queue_within_t = 1000;
//	mytcp_flow_num++;
//	this_flow_num = mytcp_flow_num;
	
	//printk(KERN_DEBUG "init mytcp, the time is %u\n", tcp_jiffies32);
//	printk(KERN_DEBUG "init mytcp\n");
	//printk(KERN_DEBUG "init mytcp, the ssth is %u\n", tp->snd_ssthresh);

	mytcp_enable(sk);
}
EXPORT_SYMBOL_GPL(tcp_mytcp_init);

static const u32 mytcp_interval_ms = 1;

void tcp_mytcp_pkts_acked(struct sock *sk, const struct ack_sample *sample)
{
	struct mytcp *mytcp = inet_csk_ca(sk);
	struct tcp_sock *tp = tcp_sk(sk);
//	int time_interval;
	int buffer_queue;
	//u32 msecstojiffies;
	u32 vrtt;

	if (sample->rtt_us < 0)
		return;
	
	vrtt = sample->rtt_us + 1;
//	mytcp->rtt_of_this_packet = sample->rtt_us;
	tp->my_ip_rtt = sample->rtt_us;//目前的rtt

	buffer_queue = tp->write_seq - tp->snd_una;   //sndbuffer中的当前队列长度,单位为byte
//	printk(KERN_DEBUG "packets inflight are %u\n", tp->packets_out);
	// printk(KERN_DEBUG "the mytcp RTT is %u\n",sample->rtt_us);
	// printk(KERN_DEBUG "in mytcp, the cubic cwnd is %u\n",tp->snd_cwnd);



	/* Filter to find propagation delay: */
	if (vrtt < mytcp->baseRTT)
		mytcp->baseRTT = vrtt;

	/* Find the min RTT during the last RTT to find
	 * the current prop. delay + queuing delay:
	 */
//	mytcp->minRTT = min(mytcp->minRTT, vrtt);
	if (sample->rtt_us < mytcp->min_rtt){
		mytcp->min_rtt = sample->rtt_us;
//		printk(KERN_DEBUG "the min RTT is %u\n",mytcp->min_rtt);
	}

	if (tp->myfast_cal_bdp_start_flag == 1){
		if (tp->myfast_min_rtt == 0){
			tp->myfast_min_rtt = sample->rtt_us;
		}
		else{
			tp->myfast_min_rtt = min(tp->myfast_min_rtt, sample->rtt_us);
		}
	}	


}
EXPORT_SYMBOL_GPL(tcp_mytcp_pkts_acked);

void tcp_mytcp_state(struct sock *sk, u8 ca_state)
{
	struct tcp_sock *tp = tcp_sk(sk);
//	struct mytcp *mytcp = inet_csk_ca(sk);
	if (ca_state == TCP_CA_Open){
//		mytcp_enable(sk);
		tp->snd_cwnd = tp->snd_cwnd;
//		printk(KERN_DEBUG "nothing happens, the cwnd is %u\n", tp->snd_cwnd);
//		printk(KERN_DEBUG "nothing happens, \n");
	}
	else{
//		mytcp_disable(sk);
		tp->snd_cwnd = tp->snd_cwnd;
//		printk(KERN_DEBUG "bad things happen, the cwnd is %u, the snd_ssthresh is %u\n", tp->snd_cwnd, tp->snd_ssthresh);
//		printk(KERN_DEBUG "bad things happens\n");
	}
}
EXPORT_SYMBOL_GPL(tcp_mytcp_state);


void tcp_mytcp_cwnd_event(struct sock *sk, enum tcp_ca_event event)
{
//	struct tcp_sock *tp = tcp_sk(sk);
//	struct mytcp *mytcp = inet_csk_ca(sk);

	if (event == CA_EVENT_CWND_RESTART||
	    event == CA_EVENT_TX_START){
//		printk(KERN_DEBUG "event CA_EVENT_CWND_RESTART happens, init mytcp\n");
		tcp_mytcp_init(sk);
	}

}
EXPORT_SYMBOL_GPL(tcp_mytcp_cwnd_event);


//因为有是基于残余队列进行控制,不再依赖丢包信息,因此与BBR相似即使有丢包也不会将cwnd减半
//由于限制了队列堆积程度,QCC的丢包较少,即使可能由于链路容量突然变小有拥塞丢包,也会检测到RQ的存在而降低cwnd
u32 tcp_mytcp_ssthresh(struct sock *sk)
{
	struct tcp_sock *tp = tcp_sk(sk);
	struct mytcp *mytcp = inet_csk_ca(sk);
	tp->snd_cwnd = tp->snd_cwnd;
//	tp->snd_ssthresh = tp->snd_cwnd/2;
//	printk(KERN_DEBUG "the thresh is %u, cwnd is %u\n", tp->snd_ssthresh, tp->snd_cwnd);
//	mytcp->loss_cnt++;
//	return tcp_sk(sk)->snd_ssthresh;
	return tp->snd_cwnd;
}

static void tcp_mytcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
{
	struct tcp_sock *tp = tcp_sk(sk);
	struct mytcp *mytcp = inet_csk_ca(sk);
	int buffer_queue;
	int min_inflight;

	struct inet_sock *inet2;
	inet2 = inet_sk(sk);



	// if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
	// 	printk(KERN_DEBUG "cubictcp_cong_avoid has receive acked is %u\n", acked);
	// 	printk(KERN_DEBUG "cubictcp_cong_avoid the ack is %u\n", ack);
	// 	printk(KERN_DEBUG "cubictcp_cong_avoid tp->snd_nxt %u\n", tp->snd_nxt);

	// }


	mytcp->inflight_2pre = mytcp->inflight_1pre;
	mytcp->inflight_1pre = mytcp->inflight_now;

	buffer_queue = tp->write_seq - tp->snd_una;   //sndbuffer中的当前队列长度,单位为byte
	mytcp->inflight_now = tp->packets_out;
	if (mytcp->inflight_1pre < mytcp->inflight_2pre && mytcp->inflight_1pre < mytcp->inflight_now){
		min_inflight = mytcp->inflight_1pre;
//		printk(KERN_DEBUG "min inflight is %u\n", min_inflight);
//		printk(KERN_DEBUG "min_queue is %d\n", mytcp->min_queue_within_t);
	}


	mytcp->cwnd_last = tp->snd_cwnd;
//	mytcp->mytcp_max_driver_queue = mytcp_actual_max_queue;
	mytcp->mytcp_max_driver_queue = tp->mytcp_est_max_queue_real;


//	printk(KERN_DEBUG "mytcp_max_driver_queue is %u\n", mytcp->mytcp_max_driver_queue);
//	printk(KERN_DEBUG "max_queue_within_t is %u\n", mytcp->max_queue_within_t);

	if (mytcp->mytcp_max_driver_queue > mytcp->max_queue_within_t){				//mytcp+++++++//
		 mytcp->max_queue_within_t = mytcp->mytcp_max_driver_queue;
	}

//	mytcp->min_queue_within_t = mytcp_min_tx_queue;
//	mytcp->min_queue = mytcp_min_tx_queue;

	mytcp->min_queue_within_t = tp->mytcp_est_rq;


	if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
		// printk(KERN_DEBUG "min_queue_within_t is %u\n", mytcp->min_queue_within_t);
		// printk(KERN_DEBUG "the mytcp RTT is %u\n",sample->rtt_us);
		printk(KERN_DEBUG "in mytcp the cwnd is %u\n", tp->snd_cwnd);

	}

	mytcp->min_queue = tp->mytcp_est_rq;

	if ((mytcp->min_queue_within_t > 0) && (mytcp->max_queue_within_t > mytcp->min_queue_within_t)){
		mytcp->one_full_ronund_send = mytcp->max_queue_within_t - mytcp->min_queue_within_t;
		mytcp->threshold = (mytcp->one_full_ronund_send * 5) / 100;
		// mytcp->threshold = max(mytcp->threshold,RQ_allow);
		if(RQ_allow > mytcp->threshold){
			mytcp->threshold = RQ_allow;
		}
//		mytcp->threshold = max(mytcp->threshold,1);
	}
	else{
		mytcp->threshold = RQ_allow;
	}
//	printk(KERN_DEBUG "min_queue_within_t is %d\n", mytcp->min_queue_within_t);
//	printk(KERN_DEBUG "max_queue_within_t is %d\n", mytcp->max_queue_within_t);
//	printk(KERN_DEBUG "one_full_ronund_send is %d\n", mytcp->one_full_ronund_send);
//	printk(KERN_DEBUG "mytcp->threshold is %u\n", mytcp->threshold);


	if (!mytcp->doing_mytcp_now) {
		tcp_reno_cong_avoid(sk, ack, acked);
//		printk(KERN_DEBUG "in reno_cong_avoid, the cwnd is %u\n", tp->snd_cwnd);
		return;
	}


	if (tcp_in_slow_start(tp) && tp->exit_slow_start_flag != 1) {	
		tcp_slow_start(tp, acked);
		mytcp->cwnd_hold = tp->snd_cwnd;
	}

	if((tp->mytcp_average_rtt != 0) &&((tp->mytcp_average_rtt - mytcp->min_rtt) > 20000)){
		tp->exit_slow_start_flag = 1;
	}

	

//当没有显式拥塞的信息返回时,按照正常的QCC逻辑利用驱动残余队列调整cwnd
	if (!tp->mytcp_ece_flag){

		//与BBR类似,当连续三轮网卡发送检测出超过阈值的堆积时,才判定占满带宽,退出慢启动。
		if(/*mytcp_round_count*/ tp->mytcp_round_flag >= 1 && tcp_in_slow_start(tp) && tp->exit_slow_start_flag != 1){
			//mytcp_round_count = 0;
			tp->mytcp_round_flag = 0;
			if (tcp_in_slow_start(tp)) {
				printk(KERN_DEBUG "qcc in slow start and slow start\n");
				int flag_count = 0;
				mytcp->full_flag_3 = mytcp->full_flag_2;
				mytcp->full_flag_2 = mytcp->full_flag_1;
				mytcp->min_queue_within_t = tp->mytcp_est_rq;
				mytcp->full_flag_1 = mytcp->min_queue_within_t;
				
				if(mytcp->full_flag_1 > mytcp->threshold){
					flag_count ++;
					tp->mytcp_full_pipe_flag = 1;
				}
				else{
					tp->mytcp_full_pipe_flag = 0;
				}
				if(mytcp->full_flag_2 > mytcp->threshold){
					flag_count ++;
				}
				if(mytcp->full_flag_3 > mytcp->threshold){
					flag_count ++;
				}
//				printk(KERN_DEBUG "in slow start, the flag1 is %d, the flag2 is %d, the flag3 is %d\n", mytcp->full_flag_1, mytcp->full_flag_2, mytcp->full_flag_3);
				//判定占满的条件是残余队列连续三轮大于阈值,或者一旦超过50(表明此时堆积很大)
				//50这个参数取的经验值,调试分析发现这一个值可以有一定变化影响也不大,因为于拥塞避免阶段也具有调整能力
				if ( ((tp->mytcp_average_rtt != 0) &&((tp->mytcp_average_rtt - mytcp->min_rtt) > 20000))|| (flag_count >= 3 && /*mytcp_round_max*/tp->mytcp_est_max_queue_real > 50) || (mytcp->full_flag_1 > 80) || (mytcp->loss_cnt > 3))
				{	

					if((tp->mytcp_average_rtt - mytcp->min_rtt) > 20000){
						printk(KERN_DEBUG " mytcp->min_rtt is %u\n",mytcp->min_rtt);	
						printk(KERN_DEBUG " tp->mytcp_average_rtt is %u\n",tp->mytcp_average_rtt);						
						printk(KERN_DEBUG "exit slow start because rtt is too big\n");			
					}
					
	//				mytcp->time_last = ktime_get_real_ns();
	//				mytcp->cwnd_first = tp->snd_cwnd - mytcp->full_flag_1;
					mytcp->cwnd_first = tp->snd_cwnd;

					if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
						printk(KERN_DEBUG "exit slow start flag_count >=3  cwnd_first is  %u\n", mytcp->cwnd_first);
					}	
	//				tp->snd_cwnd = mytcp->cwnd_first;
	//				tp->snd_ssthresh = tcp_mytcp_ssthresh(tp)/2;
					if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
						printk(KERN_DEBUG "in slow start, full, before ssthresh change the snd_cwnd is %d, the ssh is %d\n", mytcp->cwnd_first, tp->snd_ssthresh);
					}
					tp->snd_ssthresh = mytcp->cwnd_first/2;
					mytcp->time_last = ktime_get_real_ns();
					mytcp->beg_snd_nxt  = tp->snd_nxt;
					tp->mytcp_bytes_send = 0;
					tp->mytcp_acked_seq_last = ack;
					tp->exit_slow_start_flag = 1;
					if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
						printk(KERN_DEBUG "dport is %u and sport is %u\n", ntohs(inet2->inet_dport), ntohs(inet2->inet_dport));
						printk(KERN_DEBUG "exit slow start\n");
						printk(KERN_DEBUG "in slow start, full, after ssthresh change the snd_cwnd is %d, the ssh is %d\n", mytcp->cwnd_first, tp->snd_ssthresh);
					}
				}
//				printk(KERN_DEBUG "in slow start, the source port is %d, the snd_cwnd is %d, \n", tp->myfast_source, tp->snd_cwnd);
			}
			
	//		receive_num_in_RTT = 0;
			mytcp->max_queue_within_t = 0;
//			mytcp_actual_max_queue = 0;
//			mytcp_round_max = 0;
			tp->mytcp_est_max_queue_real = 0;
//			mytcp->min_queue_within_t = 1000;
//			mytcp_actual_min_queue = 1000;
			mytcp->cwnd_hold = tp->snd_cwnd;
		}

		else if(/*mytcp_round_count*/ tp->mytcp_round_flag >= 1 && !tcp_in_slow_start(tp)){

			if(mytcp->max_queue_within_t > /*mytcp_round_max*/tp->mytcp_est_max_queue_real){
				mytcp->max_queue_now = mytcp->max_queue_within_t;
			}
			else{
				mytcp->max_queue_now = /*mytcp_round_max*/tp->mytcp_est_max_queue_real;
			}
			//mytcp->max_queue_within_t;
	//		mytcp_round_count = 0;
			tp->mytcp_round_flag = 0;
	//		mytcp->cwnd_last = tp->snd_cwnd;
			if (mytcp->cong_flag == 3){
				printk(KERN_DEBUG "mytcp->cong_flag ==3\n");
				if (tp->exit_slow_start_flag==1 && mytcp->cwnd_first > 0){
					tp->snd_cwnd = mytcp->cwnd_first;
				}
				
				if(mytcp->max_queue_within_t > /*mytcp_round_max*/tp->mytcp_est_max_queue_real){
					mytcp->max_queue_first = mytcp->max_queue_within_t;
				}
				else{
					mytcp->max_queue_first = /*mytcp_round_max*/tp->mytcp_est_max_queue_real;
				}
	//			mytcp->max_queue_first = mytcp_round_max; //mytcp->max_queue_within_t;
				mytcp->queue_now = (mytcp->max_queue_first - mytcp->min_queue_within_t) + 1;
				mytcp->min_last = mytcp->min_queue_within_t;
				mytcp->cong_flag = 99;
	//			mytcp->time_tmp = ktime_get_real_ns();
			}

			else if (mytcp->cong_flag < 3){
				printk(KERN_DEBUG "mytcp->cong_flag = %u the cwnd will be cwnd_first and cwnd is %u\n",mytcp->cong_flag,tp->snd_cwnd);
				printk(KERN_DEBUG "the cwnd_first is %d\n",mytcp->cwnd_first);	
				printk(KERN_DEBUG "the ssthresh is %u\n",tp->snd_ssthresh);	
				if (tp->exit_slow_start_flag==1 && mytcp->cwnd_first > 0){
					tp->snd_cwnd = mytcp->cwnd_first;
				}
				mytcp->cong_flag ++;
				printk(KERN_DEBUG "mytcp->cong_flag < 3, the cwnd is %u\n", tp->snd_cwnd);
			}

			else{

				//////////////

				// if (mytcp->throughput_now  > 0 && (tp->mytcp_average_rtt - mytcp->min_rtt) > 20000){

				// 	printk(KERN_DEBUG "the cwnd_last will change now it is %u\n",mytcp->cwnd_last);
				// 	u32 more_rtt = tp->mytcp_average_rtt - mytcp->min_rtt - 20000;
				
				// 	u32 more_cwnd_in_air = (((mytcp->throughput_now * 2)/1) * more_rtt)/(tp->mss_cache*8);
				// 	printk(KERN_DEBUG "the more_cwnd_in_air is %u\n",more_cwnd_in_air);
				// 	if(mytcp->cwnd_last > more_cwnd_in_air/2)
				// 	{
				// 		mytcp->cwnd_last = max(mytcp->cwnd_last - more_cwnd_in_air/2,mytcp->cwnd_last/2);
				// 	}
				// 	printk(KERN_DEBUG "the cwnd_last has change it is %u\n",mytcp->cwnd_last);
					
				// }

				mytcp->queue_last = mytcp->queue_now;
				if(mytcp->min_last == 0 && mytcp->min_queue_within_t == 0){   //连续n轮最小队列长度为0,则cwnd增长步长变为n*10
	//				mytcp->cwnd_last = mytcp->cwnd_last + mytcp->threshold;
					// mytcp->cwnd_last = mytcp->cwnd_last + RQ_allow;
					mytcp->cwnd_last = mytcp->cwnd_last;

				}

				else {
					mytcp->cwnd_first = mytcp->cwnd_first;
				}

				mytcp->min_last = mytcp->min_queue_within_t;

				if(mytcp->min_queue_within_t > 0 && mytcp->min_queue_within_t < mytcp->threshold){
					if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
						printk(KERN_DEBUG "RQ is suitable cwnd will not change, snd_cwnd is %u\n", tp->snd_cwnd);
					}					
					tp->snd_cwnd = tp->snd_cwnd;
					mytcp->cwnd_first = tp->snd_cwnd;
					mytcp->max_queue_first = mytcp->max_queue_now;
					if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
						printk(KERN_DEBUG "RQ is suitable cwnd not change, snd_cwnd is %u\n", tp->snd_cwnd);
						printk(KERN_DEBUG "cwnd_first is  %u\n", mytcp->cwnd_first);
					}
//					printk(KERN_DEBUG "suitable, snd_cwnd is %u\n", tp->snd_cwnd);
	//				printk(KERN_DEBUG "111111111111111111111111111, into the update, snd_cwnd is %u\n", tp->snd_cwnd);
				}
//				else if(/*mytcp->max_queue_now > 10 && */mytcp->queue_now > 5){   //确保队列中至少有5个包的进出
				else{
					if(mytcp->min_queue_within_t > mytcp->threshold){

						if (mytcp->throughput_now  > 0 && (tp->mytcp_average_rtt - mytcp->min_rtt) > 20000){
							printk(KERN_DEBUG "the cwnd_last will change now it is %u\n",mytcp->cwnd_last);
							u32 more_rtt = tp->mytcp_average_rtt - mytcp->min_rtt - 20000;
							u32 more_cwnd_in_air = (((mytcp->throughput_now * 2)/1) * more_rtt)/(tp->mss_cache*8);
							printk(KERN_DEBUG "the more_cwnd_in_air is %u\n",more_cwnd_in_air);
							if(mytcp->cwnd_last > more_cwnd_in_air/2)
							{
								mytcp->cwnd_last = max(mytcp->cwnd_last - more_cwnd_in_air/2,mytcp->cwnd_last/2);
							}
							printk(KERN_DEBUG "the cwnd_last has change it is %u\n",mytcp->cwnd_last);
						}
						
						if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
							printk(KERN_DEBUG "long,the cwnd will down now the snd_cwnd is %u\n", tp->snd_cwnd);
							printk(KERN_DEBUG "RQ is %u\n", mytcp->min_queue_within_t);
							printk(KERN_DEBUG "mytcp->threshold is %u\n", mytcp->threshold);
							// printk(KERN_DEBUG "cwnd will be mytcp->cwnd_last - (mytcp->min_queue_within_t - mytcp->threshold); is %u\n", mytcp->threshold);
						}
						// u32 more_cwnd = ((mytcp->min_queue_within_t - mytcp->threshold)*(mytcp->min_queue_within_t - mytcp->threshold/2))/mytcp->threshold;//此时残余队列长度减阈值
						u32 more_cwnd = mytcp->min_queue_within_t - mytcp->threshold;
						printk(KERN_DEBUG "the more_cwnd is %u\n", more_cwnd);

						if(more_cwnd < mytcp->cwnd_last){
							// tp->snd_cwnd = mytcp->cwnd_last - (mytcp->min_queue_within_t - mytcp->threshold);
							
							tp->snd_cwnd = mytcp->cwnd_last - more_cwnd;
							if(tp->snd_cwnd < 10){
								// printk(KERN_DEBUG "now cwnd < 10 so make it 10 \n");
								tp->snd_cwnd = 10;
							}
							// printk(KERN_DEBUG "too_long, snd_cwnd is %u\n", tp->snd_cwnd);
						}else{
							tp->snd_cwnd -= 10;
							// printk(KERN_DEBUG "too_long, mytcp->cwnd_last is %u\n", mytcp->cwnd_last);
							// printk(KERN_DEBUG "too_long, mytcp->min_queue_within_t is %u\n", mytcp->min_queue_within_t);
							// printk(KERN_DEBUG "too_long, mytcp->threshold is %u\n", mytcp->threshold);				
							// printk(KERN_DEBUG "too_long, mytcp->min_queue_within_t - mytcp->threshold is %u\n", mytcp->min_queue_within_t - mytcp->threshold);	
							// printk(KERN_DEBUG "too_long, snd_cwnd is %u\n", tp->snd_cwnd);
							// printk(KERN_DEBUG "now cwnd < 0 so make it 10\n");
						}

						if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
							printk(KERN_DEBUG "long,the cwnd has down now the snd_cwnd is %u\n", tp->snd_cwnd);
						}
						// tp->snd_cwnd = mytcp->cwnd_last - (mytcp->min_queue_within_t - mytcp->threshold);
//						printk(KERN_DEBUG "too_long, snd_cwnd is %u\n", tp->snd_cwnd);
					}
					if(mytcp->min_queue_within_t == 0){
						if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
							printk(KERN_DEBUG "RQ is 0 cwnd will change + 步长, snd_cwnd is %u\n", tp->snd_cwnd);
						}	
						tp->snd_cwnd = mytcp->cwnd_last +  mytcp->threshold;
						if(ntohs(inet2->inet_dport) == 5201 || ntohs(inet2->inet_sport) == 5201){
							printk(KERN_DEBUG "RQ is 0 cwnd has change, snd_cwnd is %u\n", tp->snd_cwnd);
						}
//						printk(KERN_DEBUG "empty, snd_cwnd is %u\n", tp->snd_cwnd);
					}
				}
//					else{
//						tp->snd_cwnd = tp->snd_cwnd;
//					}
//				}
//				else{
//					tp->snd_cwnd = tp->snd_cwnd;
					
//				}

			}
			
			// printk(KERN_DEBUG "111111111111111111111111111, into the cong_avoid, tp->snd_cwnd is %u\n", tp->snd_cwnd);
			// printk(KERN_DEBUG "111111111111111111111111111, into the cong_avoid, min_queue_within_t is %d\n", mytcp->min_queue_within_t);
	//		printk(KERN_DEBUG "111111111111111111111111111, into the cong_avoid, queue last is %d\n", mytcp->queue_last);
	//		printk(KERN_DEBUG "111111111111111111111111111, into the cong_avoid, queue now is %d\n", mytcp->queue_now);

		//		mytcp->min_queue_within_t = mytcp->max_queue_within_t;
			mytcp->max_queue_within_t = 0;
//			mytcp_actual_max_queue = 0;
//			mytcp_round_max = 0;
			tp->mytcp_est_max_queue_real = 0;
//			mytcp->min_queue_within_t = 1000;
//			mytcp_actual_min_queue = 1000;
			if (tp->snd_cwnd < 10){
				tp->snd_cwnd = 10;
				printk(KERN_DEBUG "snd_cwnd < 10 so make cwnd is 10\n");	
			}
		//		if (tp->snd_cwnd > 30)
		//			tp->snd_cwnd = 30;
			else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
				tp->snd_cwnd = tp->snd_cwnd_clamp;
	//			tp->snd_ssthresh = tcp_current_ssthresh(sk);
//			printk(KERN_DEBUG "111111111111111111111111111, into the cong_avoid, the last_cwnd is %u\n", mytcp->cwnd_last);
	//		printk(KERN_DEBUG "111111111111111111111111111, into the cong_avoid, the cwnd is %u\n", tp->snd_cwnd);
			mytcp->cwnd_hold = tp->snd_cwnd;
		}

		else{
			tp->snd_cwnd = mytcp->cwnd_hold;
//			printk(KERN_DEBUG "111111111111111111111111111, else, the cwnd is %u\n", tp->snd_cwnd);
		}
	}

// //	每一个rtt统计一次有线瓶颈的场景
// 	if (after(ack, mytcp->beg_snd_nxt) && !tcp_in_slow_start(tp)){    //瓶颈为有线部分时的处理
		
// 		int packets_in_air;
// 		int RQ_wire;
// 		int RQ_wire2;
// 		int extra_cwnd;
// 		int throughput2;

// 		mytcp->beg_snd_nxt  = tp->snd_nxt;
// 		mytcp->time_now = ktime_get_real_ns();

// 		//从确认占满链路后100ms再开始计算吞吐量,避免慢启动发送窗口耗尽对带宽估算的影响。
// //		if ((mytcp->time_now - mytcp->time_last)>(100*1000)){

// 		tp->mytcp_acked_bytes = ack - tp->mytcp_acked_seq_last;	
// 		mytcp->throughput_now = (tp->mytcp_bytes_send * 8) / ((mytcp->time_now - mytcp->time_last)/1000);
// 		throughput2 = (tp->mytcp_acked_bytes * 8) / ((mytcp->time_now - mytcp->time_last)/1000);

// 		if (mytcp->throughput_now  > 0){
// 			packets_in_air = (((mytcp->throughput_now * 2)/1) * mytcp->min_rtt)/(tp->mss_cache*8);
// 			RQ_wire = tp->packets_out - mytcp->min_queue - packets_in_air;
// 		}

// 		else {
// 			RQ_wire = 0;
// 		}

// 		if(RQ_wire < 0){
// 			RQ_wire = 0;
// 		}

// 		if(RQ_wire > mytcp->threshold){
// 			extra_cwnd = RQ_wire - mytcp->threshold;
// //				tp->snd_cwnd = mytcp->cwnd_last - (RQ_wire - mytcp->threshold);
// 		}

// 		else{
// 			extra_cwnd = 0;
// //				tp->snd_cwnd = tp->snd_cwnd;
// 		}

// 		// tp->snd_cwnd = mytcp->cwnd_last - max(tp->mytcp_ece_count, extra_cwnd);
// 		tp->snd_cwnd = mytcp->cwnd_last -extra_cwnd;

// 		if (tp->snd_cwnd > (2*packets_in_air)){
// 			tp->snd_cwnd = (2*packets_in_air);
// 		}

// //			printk(KERN_DEBUG "the bytes ackes is %llu, time interval is %llu\n", tp->bytes_acked, mytcp->time_now - mytcp->time_last);
// 		printk(KERN_DEBUG "the throughput is %u, throughput2 is %u, RQ_wire is %u\n", mytcp->throughput_now, throughput2, RQ_wire);
// 		printk(KERN_DEBUG "wired bottle, the packets out is %u, min queue is %u, packets in air is %d, the RQ wire is %d\n",
// 			tp->packets_out, mytcp->min_queue, packets_in_air, RQ_wire);
// //		printk(KERN_DEBUG "wired bottle, the mytcp_ece_count is %u, extra_cwnd is %u\n",tp->mytcp_ece_count, extra_cwnd);
// 		printk(KERN_DEBUG ",wired bottle, into the cong_avoid, the cwnd is %u\n", tp->snd_cwnd);
// //		}
// 		//将ece计数器置0,重新开始统计下一轮rtt内的ece数量
// 		tp->mytcp_ece_count = 0;	
// 		tp->mytcp_bytes_send = 0;
// 		tp->mytcp_acked_seq_last = ack;
// 		mytcp->time_last = mytcp->time_now;	
// 		mytcp->cwnd_hold = tp->snd_cwnd;
// 	}

// 	else{
// 			tp->snd_cwnd = mytcp->cwnd_hold;
// //			printk(KERN_DEBUG "111111111111111111111111111, else, the cwnd is %u\n", tp->snd_cwnd);
// 	}
//	每一个rtt统计一次有线瓶颈的场景
	if (after(ack, mytcp->beg_snd_nxt)){    //瓶颈为有线部分时的处理
		printk(KERN_DEBUG "此时经历了一轮RTT\n");
		mytcp->beg_snd_nxt  = tp->snd_nxt;
		tp->mytcp_this_rtt_count = 0;
		if(!tcp_in_slow_start(tp)){
			int packets_in_air;
			int RQ_wire;
			int RQ_wire2;
			int extra_cwnd;
			int throughput2;

			
			mytcp->time_now = ktime_get_real_ns();

			//从确认占满链路后100ms再开始计算吞吐量,避免慢启动发送窗口耗尽对带宽估算的影响。
	//		if ((mytcp->time_now - mytcp->time_last)>(100*1000)){

			tp->mytcp_acked_bytes = ack - tp->mytcp_acked_seq_last;	
			mytcp->throughput_now = (tp->mytcp_bytes_send * 8) / ((mytcp->time_now - mytcp->time_last)/1000);
			throughput2 = (tp->mytcp_acked_bytes * 8) / ((mytcp->time_now - mytcp->time_last)/1000);

			if (mytcp->throughput_now  > 0){
				packets_in_air = (((mytcp->throughput_now * 2)/1) * mytcp->min_rtt)/(tp->mss_cache*8);
				printk(KERN_DEBUG "the mytcp->throughput_now is > 0\n");
				RQ_wire = tp->packets_out - mytcp->min_queue - packets_in_air;
			}

			else {
				RQ_wire = 0;
			}

			if(RQ_wire < 0){
				RQ_wire = 0;
			}

			if(RQ_wire > mytcp->threshold){
				extra_cwnd = RQ_wire - mytcp->threshold;
	//				tp->snd_cwnd = mytcp->cwnd_last - (RQ_wire - mytcp->threshold);
			}

			else{
				extra_cwnd = 0;
	//				tp->snd_cwnd = tp->snd_cwnd;
			}

			// tp->snd_cwnd = mytcp->cwnd_last - max(tp->mytcp_ece_count, extra_cwnd);
			tp->snd_cwnd = mytcp->cwnd_last -extra_cwnd;

			tp->snd_cwnd = max(mytcp->cwnd_last - extra_cwnd,tp->snd_cwnd/2);//吕超加的,为了控制保证吞吐量不会过低

			if (tp->snd_cwnd > (2*packets_in_air)){
				tp->snd_cwnd = (2*packets_in_air);
			}
			if(tp->snd_cwnd < 10){
				tp->snd_cwnd = 10;
			}

	//			printk(KERN_DEBUG "the bytes ackes is %llu, time interval is %llu\n", tp->bytes_acked, mytcp->time_now - mytcp->time_last);
			printk(KERN_DEBUG "the throughput is %u, throughput2 is %u, RQ_wire is %u\n", mytcp->throughput_now, throughput2, RQ_wire);
			printk(KERN_DEBUG "wired bottle, the packets out is %u, min queue is %u, packets in air is %d, the RQ wire is %d\n",
				tp->packets_out, mytcp->min_queue, packets_in_air, RQ_wire);
	//		printk(KERN_DEBUG "wired bottle, the mytcp_ece_count is %u, extra_cwnd is %u\n",tp->mytcp_ece_count, extra_cwnd);
			printk(KERN_DEBUG ",wired bottle, into the cong_avoid, the cwnd is %u\n", tp->snd_cwnd);
	//		}
			//将ece计数器置0,重新开始统计下一轮rtt内的ece数量
			tp->mytcp_ece_count = 0;	
			tp->mytcp_bytes_send = 0;
			tp->mytcp_acked_seq_last = ack;
			mytcp->time_last = mytcp->time_now;	
			mytcp->cwnd_hold = tp->snd_cwnd;
		}
		else{
			tp->snd_cwnd = mytcp->cwnd_hold;
		}
	}
	else{//本轮rtt没有结束
			tp->mytcp_average_rtt = ((tp->mytcp_average_rtt)*(tp->mytcp_this_rtt_count)+tp->my_ip_rtt)/(tp->mytcp_this_rtt_count+1);
			printk(KERN_DEBUG "now average RTT is %u\n",tp->mytcp_average_rtt);
			tp->mytcp_this_rtt_count += 1;
			
			tp->snd_cwnd = mytcp->cwnd_hold;
//			printk(KERN_DEBUG "111111111111111111111111111, else, the cwnd is %u\n", tp->snd_cwnd);
	}


	

//	printk(KERN_DEBUG "111111111111111111111111111, into the cong_avoid, the actual cwnd is %u\n", tp->snd_cwnd);

}





/* Extract info for Tcp socket info provided via netlink. */
size_t tcp_mytcp_get_info(struct sock *sk, u32 ext, int *attr,
			  union tcp_cc_info *info)
{
	const struct mytcp *ca = inet_csk_ca(sk);

	if (ext & (1 << (INET_DIAG_MYTCPINFO - 1))) {
		info->mytcp.tcpv_enabled = ca->doing_mytcp_now;
		info->mytcp.tcpv_rttcnt	= ca->cntRTT;
		info->mytcp.tcpv_rtt = ca->baseRTT;
		info->mytcp.tcpv_minrtt = ca->min_rtt;

		*attr = INET_DIAG_MYTCPINFO;
		return sizeof(struct tcpmytcp_info);
	}
	return 0;
}
//EXPORT_SYMBOL_GPL(tcp_mytcp_get_info);

static struct tcp_congestion_ops tcp_mytcp __read_mostly = {
	.init		= tcp_mytcp_init,
	.ssthresh	= tcp_mytcp_ssthresh,
	.undo_cwnd	= tcp_reno_undo_cwnd,
	.cong_avoid	= tcp_mytcp_cong_avoid,
	.pkts_acked	= tcp_mytcp_pkts_acked,
//	.set_state	= tcp_mytcp_state,
//	.cwnd_event	= tcp_mytcp_cwnd_event,
	.get_info	= tcp_mytcp_get_info,

	.owner		= THIS_MODULE,
	.name		= "mytcp",
};

static int __init tcp_mytcp_register(void)
{
	BUILD_BUG_ON(sizeof(struct mytcp) > ICSK_CA_PRIV_SIZE);
	tcp_register_congestion_control(&tcp_mytcp);
	return 0;
}

static void __exit tcp_mytcp_unregister(void)
{
	tcp_unregister_congestion_control(&tcp_mytcp);
}

module_init(tcp_mytcp_register);
module_exit(tcp_mytcp_unregister);

MODULE_AUTHOR("LLG");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("TCP mytcp");