hc
2024-08-16 62c46c9150c4afde7e5b25436263fddf79d66f0b
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
#include <uapi/linux/if_ether.h>
#include <uapi/linux/tcp.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/in.h>
#include <linux/moduleparam.h>
#include <net/tcp.h>
 
#include "sprdwl.h"
#include "intf_ops.h"
#include "tcp_ack.h"
 
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
static void sprdwl_tcp_ack_timeout(struct timer_list *t)
#else
static void sprdwl_tcp_ack_timeout(unsigned long data)
#endif
{
   struct sprdwl_tcp_ack_info *ack_info;
   struct sprdwl_msg_buf *msg;
   struct sprdwl_tcp_ack_manage *ack_m = NULL;
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
   ack_info = (struct sprdwl_tcp_ack_info *)from_timer(ack_info, t, timer);
#else
   ack_info = (struct sprdwl_tcp_ack_info *)data;
#endif
   ack_m = container_of(ack_info, struct sprdwl_tcp_ack_manage,
                ack_info[ack_info->ack_info_num]);
 
   write_seqlock_bh(&ack_info->seqlock);
   msg = ack_info->msgbuf;
   if (ack_info->busy && msg && !ack_info->in_send_msg) {
       ack_info->msgbuf = NULL;
       ack_info->drop_cnt = 0;
       ack_info->in_send_msg = msg;
       write_sequnlock_bh(&ack_info->seqlock);
       sprdwl_intf_tx(ack_m->priv, msg);
       return;
   }
   write_sequnlock_bh(&ack_info->seqlock);
}
 
void sprdwl_tcp_ack_init(struct sprdwl_priv *priv)
{
   int i;
   struct sprdwl_tcp_ack_info *ack_info;
   struct sprdwl_tcp_ack_manage *ack_m = &priv->ack_m;
 
   memset(ack_m, 0, sizeof(struct sprdwl_tcp_ack_manage));
   ack_m->priv = priv;
   spin_lock_init(&ack_m->lock);
   atomic_set(&ack_m->max_drop_cnt, SPRDWL_TCP_ACK_DROP_CNT);
   ack_m->last_time = jiffies;
   ack_m->timeout = msecs_to_jiffies(SPRDWL_ACK_OLD_TIME);
 
   for (i = 0; i < SPRDWL_TCP_ACK_NUM; i++) {
       ack_info = &ack_m->ack_info[i];
       ack_info->ack_info_num = i;
       seqlock_init(&ack_info->seqlock);
       ack_info->last_time = jiffies;
       ack_info->timeout = msecs_to_jiffies(SPRDWL_ACK_OLD_TIME);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)
       timer_setup(&ack_info->timer, sprdwl_tcp_ack_timeout, 0);
#else
       setup_timer(&ack_info->timer, sprdwl_tcp_ack_timeout,
               (unsigned long)ack_info);
#endif
   }
 
   atomic_set(&ack_m->enable, 1);
   ack_m->ack_winsize = MIN_WIN;
}
 
void sprdwl_tcp_ack_deinit(struct sprdwl_priv *priv)
{
   int i;
   struct sprdwl_tcp_ack_manage *ack_m = &priv->ack_m;
   struct sprdwl_msg_buf *drop_msg = NULL;
 
   atomic_set(&ack_m->enable, 0);
 
   for (i = 0; i < SPRDWL_TCP_ACK_NUM; i++) {
       drop_msg = NULL;
 
       write_seqlock_bh(&ack_m->ack_info[i].seqlock);
       del_timer(&ack_m->ack_info[i].timer);
       drop_msg = ack_m->ack_info[i].msgbuf;
       ack_m->ack_info[i].msgbuf = NULL;
       write_sequnlock_bh(&ack_m->ack_info[i].seqlock);
 
       if (drop_msg)
           sprdwl_intf_tcp_drop_msg(priv, drop_msg);
   }
}
 
static int sprdwl_tcp_check_quick_ack(unsigned char *buf,
                     struct sprdwl_tcp_ack_msg *msg)
{
   int ip_hdr_len;
   unsigned char *temp;
   struct ethhdr *ethhdr;
   struct iphdr *iphdr;
   struct tcphdr *tcphdr;
 
   ethhdr = (struct ethhdr *)buf;
   if (ethhdr->h_proto != htons(ETH_P_IP))
       return 0;
   iphdr = (struct iphdr *)(ethhdr + 1);
   if (iphdr->version != 4 || iphdr->protocol != IPPROTO_TCP)
       return 0;
   ip_hdr_len = iphdr->ihl * 4;
   temp = (unsigned char *)(iphdr) + ip_hdr_len;
   tcphdr = (struct tcphdr *)temp;
   /* TCP_FLAG_ACK */
   if (!(temp[13] & 0x10))
       return 0;
 
   if (temp[13] & 0x8) {
       msg->saddr = iphdr->daddr;
       msg->daddr = iphdr->saddr;
       msg->source = tcphdr->dest;
       msg->dest = tcphdr->source;
       msg->seq = ntohl(tcphdr->seq);
       return 1;
   }
 
   return 0;
}
 
static int is_drop_tcp_ack(struct tcphdr *tcphdr, int tcp_tot_len,
               unsigned short *win_scale)
{
   int drop = 1;
   int len = tcphdr->doff * 4;
   unsigned char *ptr;
 
   if (tcp_tot_len > len) {
       drop = 0;
   } else {
       len -= sizeof(struct tcphdr);
       ptr = (unsigned char *)(tcphdr + 1);
 
       while ((len > 0) && drop) {
           int opcode = *ptr++;
           int opsize;
 
           switch (opcode) {
           case TCPOPT_EOL:
               break;
           case TCPOPT_NOP:
               len--;
               continue;
           default:
               opsize = *ptr++;
               if (opsize < 2)
                   break;
               if (opsize > len)
                   break;
 
               switch (opcode) {
               /* TODO: Add other ignore opt */
               case TCPOPT_TIMESTAMP:
                   break;
               case TCPOPT_WINDOW:
                   if (*ptr < 15)
                       *win_scale = (1 << (*ptr));
                   break;
               default:
                   drop = 2;
               }
 
               ptr += opsize - 2;
               len -= opsize;
           }
       }
   }
 
   return drop;
}
 
/* flag:0 for not tcp ack
 *    1 for ack which can be drop
 *    2 for other ack whith more info
 */
static int sprdwl_tcp_check_ack(unsigned char *buf,
               struct sprdwl_tcp_ack_msg *msg,
               unsigned short *win_scale)
{
   int ret;
   int ip_hdr_len;
   int tcp_tot_len;
   unsigned char *temp;
   struct ethhdr *ethhdr;
   struct iphdr *iphdr;
   struct tcphdr *tcphdr;
 
   ethhdr = (struct ethhdr *)buf;
   if (ethhdr->h_proto != htons(ETH_P_IP))
       return 0;
   iphdr = (struct iphdr *)(ethhdr + 1);
   if (iphdr->version != 4 || iphdr->protocol != IPPROTO_TCP)
       return 0;
   ip_hdr_len = iphdr->ihl * 4;
   temp = (unsigned char *)(iphdr) + ip_hdr_len;
   tcphdr = (struct tcphdr *)temp;
   /* TCP_FLAG_ACK */
   if (!(temp[13] & 0x10))
       return 0;
 
   tcp_tot_len = ntohs(iphdr->tot_len) - ip_hdr_len;
   ret = is_drop_tcp_ack(tcphdr, tcp_tot_len, win_scale);
 
   if (ret > 0) {
       msg->saddr = iphdr->saddr;
       msg->daddr = iphdr->daddr;
       msg->source = tcphdr->source;
       msg->dest = tcphdr->dest;
       msg->seq = ntohl(tcphdr->ack_seq);
       msg->win = ntohs(tcphdr->window);
   }
 
   return ret;
}
 
/* return val: -1 for not match, others for match */
static int sprdwl_tcp_ack_match(struct sprdwl_tcp_ack_manage *ack_m,
               struct sprdwl_tcp_ack_msg *ack_msg)
{
   int i, ret = -1;
   unsigned start;
   struct sprdwl_tcp_ack_info *ack_info;
   struct sprdwl_tcp_ack_msg *ack;
 
   for (i = 0; ((ret < 0) && (i < SPRDWL_TCP_ACK_NUM)); i++) {
       ack_info = &ack_m->ack_info[i];
       do {
           start = read_seqbegin(&ack_info->seqlock);
           ret = -1;
 
           ack = &ack_info->ack_msg;
           if (ack_info->busy &&
               ack->dest == ack_msg->dest &&
               ack->source == ack_msg->source &&
               ack->saddr == ack_msg->saddr &&
               ack->daddr == ack_msg->daddr)
               ret = i;
       } while (read_seqretry(&ack_info->seqlock, start));
   }
 
   return ret;
}
 
static void sprdwl_tcp_ack_update(struct sprdwl_tcp_ack_manage *ack_m)
{
   int i;
   struct sprdwl_tcp_ack_info *ack_info;
 
   if (time_after(jiffies, ack_m->last_time + ack_m->timeout)) {
       spin_lock_bh(&ack_m->lock);
       ack_m->last_time = jiffies;
       for (i = SPRDWL_TCP_ACK_NUM - 1; i >= 0; i--) {
           ack_info = &ack_m->ack_info[i];
           write_seqlock_bh(&ack_info->seqlock);
           if (ack_info->busy &&
               time_after(jiffies, ack_info->last_time +
                      ack_info->timeout)) {
               ack_m->free_index = i;
               ack_m->max_num--;
               ack_info->busy = 0;
           }
           write_sequnlock_bh(&ack_info->seqlock);
       }
       spin_unlock_bh(&ack_m->lock);
   }
}
 
/* return val: -1 for no index, others for index */
static int sprdwl_tcp_ack_alloc_index(struct sprdwl_tcp_ack_manage *ack_m)
{
   int i, ret = -1;
   struct sprdwl_tcp_ack_info *ack_info;
   unsigned start;
 
   spin_lock_bh(&ack_m->lock);
   if (ack_m->max_num == SPRDWL_TCP_ACK_NUM) {
       spin_unlock_bh(&ack_m->lock);
       return -1;
   }
 
   if (ack_m->free_index >= 0) {
       i = ack_m->free_index;
       ack_m->free_index = -1;
       ack_m->max_num++;
       spin_unlock_bh(&ack_m->lock);
       return i;
   }
 
   for (i = 0; ((ret < 0) && (i < SPRDWL_TCP_ACK_NUM)); i++) {
       ack_info = &ack_m->ack_info[i];
       do {
           start = read_seqbegin(&ack_info->seqlock);
           ret = -1;
           if (!ack_info->busy) {
               ack_m->free_index = -1;
               ack_m->max_num++;
               ret = i;
           }
       } while (read_seqretry(&ack_info->seqlock, start));
   }
   spin_unlock_bh(&ack_m->lock);
 
   return ret;
}
 
/* return val: 0 for not handle tx, 1 for handle tx */
int sprdwl_tcp_ack_handle(struct sprdwl_msg_buf *new_msgbuf,
             struct sprdwl_tcp_ack_manage *ack_m,
             struct sprdwl_tcp_ack_info *ack_info,
             struct sprdwl_tcp_ack_msg *ack_msg,
             int type)
{
   int quick_ack = 0;
   struct sprdwl_tcp_ack_msg *ack;
   int ret = 0;
   struct sprdwl_msg_buf *drop_msg = NULL;
 
   write_seqlock_bh(&ack_info->seqlock);
 
   ack_info->last_time = jiffies;
   ack = &ack_info->ack_msg;
 
   if (type == 2) {
       if (SPRDWL_U32_BEFORE(ack->seq, ack_msg->seq)) {
           ack->seq = ack_msg->seq;
           if (ack_info->psh_flag &&
               !SPRDWL_U32_BEFORE(ack_msg->seq,
                          ack_info->psh_seq)) {
               ack_info->psh_flag = 0;
           }
 
           if (ack_info->msgbuf) {
               drop_msg = ack_info->msgbuf;
               ack_info->msgbuf = NULL;
               del_timer(&ack_info->timer);
           }
 
           ack_info->in_send_msg = NULL;
           ack_info->drop_cnt = atomic_read(&ack_m->max_drop_cnt);
       } else {
           wl_err("%s before abnormal ack: %d, %d\n",
                  __func__, ack->seq, ack_msg->seq);
           drop_msg = new_msgbuf;
           ret = 1;
       }
   } else if (SPRDWL_U32_BEFORE(ack->seq, ack_msg->seq)) {
       if (ack_info->msgbuf) {
           drop_msg = ack_info->msgbuf;
           ack_info->msgbuf = NULL;
       }
 
       if (ack_info->psh_flag &&
           !SPRDWL_U32_BEFORE(ack_msg->seq, ack_info->psh_seq)) {
           ack_info->psh_flag = 0;
           quick_ack = 1;
       } else {
           ack_info->drop_cnt++;
       }
 
       ack->seq = ack_msg->seq;
 
       if (quick_ack || (!ack_info->in_send_msg &&
                 (ack_info->drop_cnt >=
                  atomic_read(&ack_m->max_drop_cnt)))) {
           ack_info->drop_cnt = 0;
           ack_info->in_send_msg = new_msgbuf;
           del_timer(&ack_info->timer);
       } else {
           ret = 1;
           ack_info->msgbuf = new_msgbuf;
           if (!timer_pending(&ack_info->timer))
               mod_timer(&ack_info->timer,
                     (jiffies + msecs_to_jiffies(5)));
       }
   } else {
       wl_err("%s before ack: %d, %d\n",
              __func__, ack->seq, ack_msg->seq);
       drop_msg = new_msgbuf;
       ret = 1;
   }
 
   write_sequnlock_bh(&ack_info->seqlock);
 
   if (drop_msg)
       sprdwl_intf_tcp_drop_msg(ack_m->priv, drop_msg);
 
   return ret;
}
 
void sprdwl_filter_rx_tcp_ack(struct sprdwl_priv *priv,
                 unsigned char *buf, unsigned plen)
{
   int index;
   struct sprdwl_tcp_ack_msg ack_msg;
   struct sprdwl_tcp_ack_info *ack_info;
   struct sprdwl_tcp_ack_manage *ack_m = &priv->ack_m;
 
   if (!atomic_read(&ack_m->enable))
       return;
 
   if ((plen > MAX_TCP_ACK) ||
       !sprdwl_tcp_check_quick_ack(buf, &ack_msg))
       return;
 
   index = sprdwl_tcp_ack_match(ack_m, &ack_msg);
   if (index >= 0) {
       ack_info = ack_m->ack_info + index;
       write_seqlock_bh(&ack_info->seqlock);
       ack_info->psh_flag = 1;
       ack_info->psh_seq = ack_msg.seq;
       write_sequnlock_bh(&ack_info->seqlock);
   }
}
 
/* return val: 0 for not filter, 1 for filter */
int sprdwl_filter_send_tcp_ack(struct sprdwl_priv *priv,
                  struct sprdwl_msg_buf *msgbuf,
                  unsigned char *buf, unsigned int plen)
{
   int ret = 0;
   int index, drop;
   unsigned short win_scale = 0;
   unsigned int win = 0;
   struct sprdwl_tcp_ack_msg ack_msg;
   struct sprdwl_tcp_ack_msg *ack;
   struct sprdwl_tcp_ack_info *ack_info;
   struct sprdwl_tcp_ack_manage *ack_m = &priv->ack_m;
 
   if (!atomic_read(&ack_m->enable))
       return 0;
 
   if (plen > MAX_TCP_ACK)
       return 0;
 
   sprdwl_tcp_ack_update(ack_m);
   drop = sprdwl_tcp_check_ack(buf, &ack_msg, &win_scale);
   if (!drop && (0 == win_scale))
       return 0;
 
   index = sprdwl_tcp_ack_match(ack_m, &ack_msg);
   if (index >= 0) {
       ack_info = ack_m->ack_info + index;
       if ((0 != win_scale) &&
           (ack_info->win_scale != win_scale)) {
           write_seqlock_bh(&ack_info->seqlock);
           ack_info->win_scale = win_scale;
           write_sequnlock_bh(&ack_info->seqlock);
       }
 
       if (drop > 0) {
           win = ack_info->win_scale * ack_msg.win;
           if (win < (ack_m->ack_winsize * SIZE_KB))
               drop = 2;
 
           ret = sprdwl_tcp_ack_handle(msgbuf, ack_m, ack_info,
                       &ack_msg, drop);
       }
 
       goto out;
   }
 
   index = sprdwl_tcp_ack_alloc_index(ack_m);
   if (index >= 0) {
       write_seqlock_bh(&ack_m->ack_info[index].seqlock);
       ack_m->ack_info[index].busy = 1;
       ack_m->ack_info[index].psh_flag = 0;
       ack_m->ack_info[index].last_time = jiffies;
       ack_m->ack_info[index].drop_cnt =
           atomic_read(&ack_m->max_drop_cnt);
       ack_m->ack_info[index].win_scale =
           (win_scale != 0) ? win_scale : 1;
 
       ack = &ack_m->ack_info[index].ack_msg;
       ack->dest = ack_msg.dest;
       ack->source = ack_msg.source;
       ack->saddr = ack_msg.saddr;
       ack->daddr = ack_msg.daddr;
       ack->seq = ack_msg.seq;
       write_sequnlock_bh(&ack_m->ack_info[index].seqlock);
   }
 
out:
   return ret;
}
 
void sprdwl_move_tcpack_msg(struct sprdwl_priv *priv,
               struct sprdwl_msg_buf *msg)
{
   struct sprdwl_tcp_ack_info *ack_info;
   struct sprdwl_tcp_ack_manage *ack_m = &priv->ack_m;
   int i = 0;
 
   if (!atomic_read(&ack_m->enable))
       return;
 
   if (msg->len > MAX_TCP_ACK)
       return;
 
   for (i = 0; i < SPRDWL_TCP_ACK_NUM; i++) {
       ack_info = &ack_m->ack_info[i];
       write_seqlock_bh(&ack_info->seqlock);
       if (ack_info->busy && (ack_info->in_send_msg == msg))
           ack_info->in_send_msg = NULL;
       write_sequnlock_bh(&ack_info->seqlock);
   }
}
 
extern struct sprdwl_priv *g_sprdwl_priv;
 
void enable_tcp_ack_delay(char *buf, unsigned char offset)
{
   int enable = buf[offset] - '0';
   int i;
   struct sprdwl_tcp_ack_manage *ack_m = NULL;
   struct sprdwl_msg_buf *drop_msg = NULL;
 
   if (!g_sprdwl_priv)
       return ;
 
   ack_m = &g_sprdwl_priv->ack_m;
 
   if (enable == 0) {
       atomic_set(&ack_m->enable, 0);
       for (i = 0; i < SPRDWL_TCP_ACK_NUM; i++) {
           drop_msg = NULL;
 
           write_seqlock_bh(&ack_m->ack_info[i].seqlock);
           drop_msg = ack_m->ack_info[i].msgbuf;
           ack_m->ack_info[i].msgbuf = NULL;
           del_timer(&ack_m->ack_info[i].timer);
           write_sequnlock_bh(&ack_m->ack_info[i].seqlock);
 
           if (drop_msg)
               sprdwl_intf_tcp_drop_msg(g_sprdwl_priv,
                            drop_msg);
       }
   } else {
       atomic_set(&ack_m->enable, 1);
   }
}
 
void adjust_tcp_ack_delay(char *buf, unsigned char offset)
{
#define MAX_LEN 2
   unsigned int cnt = 0;
   unsigned int i = 0;
   struct sprdwl_tcp_ack_manage *ack_m = NULL;
 
   if (!g_sprdwl_priv)
       return ;
 
   for (i = 0; i < MAX_LEN; (cnt *= 10), i++) {
 
       if ((buf[offset + i] >= '0') &&
          (buf[offset + i] <= '9')) {
           cnt += (buf[offset + i] - '0');
       } else {
           cnt /= 10;
           break;
       }
   }
 
   ack_m = &g_sprdwl_priv->ack_m;
   wl_err("cnt: %d\n", cnt);
 
   if (cnt >= 100)
       cnt = SPRDWL_TCP_ACK_DROP_CNT;
 
   atomic_set(&ack_m->max_drop_cnt, cnt);
   wl_err("drop time: %d, atomic drop time: %d\n", cnt, atomic_read(&ack_m->max_drop_cnt));
#undef MAX_LEN
}
 
void adjust_tcp_ack_delay_win(char *buf, unsigned char offset)
{
   unsigned int value = 0;
   unsigned int i = 0;
   unsigned int len = strlen(buf) - strlen("tcpack_delay_win=");
   struct sprdwl_tcp_ack_manage *ack_m = NULL;
 
   if (!g_sprdwl_priv)
       return;
 
   for (i = 0; i < len; (value *= 10), i++) {
       if ((buf[offset + i] >= '0') &&
          (buf[offset + i] <= '9')) {
           value += (buf[offset + i] - '0');
       } else {
           value /= 10;
           break;
       }
   }
   ack_m = &g_sprdwl_priv->ack_m;
   ack_m->ack_winsize = value;
   wl_err("%s, change tcpack_delay_win to %dKB\n", __func__, value);
}