This source file includes following definitions.
- batadv_tp_session_cookie
- batadv_tp_cwnd
- batadv_tp_update_cwnd
- batadv_tp_update_rto
- batadv_tp_batctl_notify
- batadv_tp_batctl_error_notify
- batadv_tp_list_find
- batadv_tp_list_find_session
- batadv_tp_vars_release
- batadv_tp_vars_put
- batadv_tp_sender_cleanup
- batadv_tp_sender_end
- batadv_tp_sender_shutdown
- batadv_tp_sender_finish
- batadv_tp_reset_sender_timer
- batadv_tp_sender_timeout
- batadv_tp_fill_prerandom
- batadv_tp_send_msg
- batadv_tp_recv_ack
- batadv_tp_avail
- batadv_tp_wait_available
- batadv_tp_send
- batadv_tp_start_kthread
- batadv_tp_start
- batadv_tp_stop
- batadv_tp_reset_receiver_timer
- batadv_tp_receiver_shutdown
- batadv_tp_send_ack
- batadv_tp_handle_out_of_order
- batadv_tp_ack_unordered
- batadv_tp_init_recv
- batadv_tp_recv_msg
- batadv_tp_meter_recv
- batadv_tp_meter_init
1
2
3
4
5
6
7 #include "tp_meter.h"
8 #include "main.h"
9
10 #include <linux/atomic.h>
11 #include <linux/build_bug.h>
12 #include <linux/byteorder/generic.h>
13 #include <linux/cache.h>
14 #include <linux/compiler.h>
15 #include <linux/err.h>
16 #include <linux/etherdevice.h>
17 #include <linux/gfp.h>
18 #include <linux/if_ether.h>
19 #include <linux/init.h>
20 #include <linux/jiffies.h>
21 #include <linux/kernel.h>
22 #include <linux/kref.h>
23 #include <linux/kthread.h>
24 #include <linux/limits.h>
25 #include <linux/list.h>
26 #include <linux/netdevice.h>
27 #include <linux/param.h>
28 #include <linux/printk.h>
29 #include <linux/random.h>
30 #include <linux/rculist.h>
31 #include <linux/rcupdate.h>
32 #include <linux/sched.h>
33 #include <linux/skbuff.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/timer.h>
39 #include <linux/wait.h>
40 #include <linux/workqueue.h>
41 #include <uapi/linux/batadv_packet.h>
42 #include <uapi/linux/batman_adv.h>
43
44 #include "hard-interface.h"
45 #include "log.h"
46 #include "netlink.h"
47 #include "originator.h"
48 #include "send.h"
49
50
51
52
53
54 #define BATADV_TP_DEF_TEST_LENGTH 10000
55
56
57
58
59 #define BATADV_TP_AWND 0x20000000
60
61
62
63
64
65 #define BATADV_TP_RECV_TIMEOUT 1000
66
67
68
69
70
71
72 #define BATADV_TP_MAX_RTO 30000
73
74
75
76
77
78 #define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
79
80
81
82
83
84 #define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
85 sizeof(struct batadv_unicast_packet))
86
87 static u8 batadv_tp_prerandom[4096] __read_mostly;
88
89
90
91
92
93
94
95
96 static u32 batadv_tp_session_cookie(const u8 session[2], u8 icmp_uid)
97 {
98 u32 cookie;
99
100 cookie = icmp_uid << 16;
101 cookie |= session[0] << 8;
102 cookie |= session[1];
103
104 return cookie;
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119 static u32 batadv_tp_cwnd(u32 base, u32 increment, u32 min)
120 {
121 u32 new_size = base + increment;
122
123
124 if (new_size < base)
125 new_size = (u32)ULONG_MAX;
126
127 new_size = min_t(u32, new_size, BATADV_TP_AWND);
128
129 return max_t(u32, new_size, min);
130 }
131
132
133
134
135
136
137
138
139
140
141
142 static void batadv_tp_update_cwnd(struct batadv_tp_vars *tp_vars, u32 mss)
143 {
144 spin_lock_bh(&tp_vars->cwnd_lock);
145
146
147 if (tp_vars->cwnd <= tp_vars->ss_threshold) {
148 tp_vars->dec_cwnd = 0;
149 tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
150 spin_unlock_bh(&tp_vars->cwnd_lock);
151 return;
152 }
153
154
155 tp_vars->dec_cwnd += max_t(u32, 1U << 3,
156 ((mss * mss) << 6) / (tp_vars->cwnd << 3));
157 if (tp_vars->dec_cwnd < (mss << 3)) {
158 spin_unlock_bh(&tp_vars->cwnd_lock);
159 return;
160 }
161
162 tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
163 tp_vars->dec_cwnd = 0;
164
165 spin_unlock_bh(&tp_vars->cwnd_lock);
166 }
167
168
169
170
171
172
173 static void batadv_tp_update_rto(struct batadv_tp_vars *tp_vars,
174 u32 new_rtt)
175 {
176 long m = new_rtt;
177
178
179
180
181
182
183
184 if (tp_vars->srtt != 0) {
185 m -= (tp_vars->srtt >> 3);
186 tp_vars->srtt += m;
187 if (m < 0)
188 m = -m;
189
190 m -= (tp_vars->rttvar >> 2);
191 tp_vars->rttvar += m;
192 } else {
193
194 tp_vars->srtt = m << 3;
195 tp_vars->rttvar = m << 1;
196 }
197
198
199
200
201 tp_vars->rto = (tp_vars->srtt >> 3) + tp_vars->rttvar;
202 }
203
204
205
206
207
208
209
210
211
212
213 static void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason,
214 const u8 *dst, struct batadv_priv *bat_priv,
215 unsigned long start_time, u64 total_sent,
216 u32 cookie)
217 {
218 u32 test_time;
219 u8 result;
220 u32 total_bytes;
221
222 if (!batadv_tp_is_error(reason)) {
223 result = BATADV_TP_REASON_COMPLETE;
224 test_time = jiffies_to_msecs(jiffies - start_time);
225 total_bytes = total_sent;
226 } else {
227 result = reason;
228 test_time = 0;
229 total_bytes = 0;
230 }
231
232 batadv_netlink_tpmeter_notify(bat_priv, dst, result, test_time,
233 total_bytes, cookie);
234 }
235
236
237
238
239
240
241
242
243 static void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason,
244 const u8 *dst,
245 struct batadv_priv *bat_priv,
246 u32 cookie)
247 {
248 batadv_tp_batctl_notify(reason, dst, bat_priv, 0, 0, cookie);
249 }
250
251
252
253
254
255
256
257
258
259
260
261 static struct batadv_tp_vars *batadv_tp_list_find(struct batadv_priv *bat_priv,
262 const u8 *dst)
263 {
264 struct batadv_tp_vars *pos, *tp_vars = NULL;
265
266 rcu_read_lock();
267 hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
268 if (!batadv_compare_eth(pos->other_end, dst))
269 continue;
270
271
272
273
274
275 if (unlikely(!kref_get_unless_zero(&pos->refcount)))
276 continue;
277
278 tp_vars = pos;
279 break;
280 }
281 rcu_read_unlock();
282
283 return tp_vars;
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299 static struct batadv_tp_vars *
300 batadv_tp_list_find_session(struct batadv_priv *bat_priv, const u8 *dst,
301 const u8 *session)
302 {
303 struct batadv_tp_vars *pos, *tp_vars = NULL;
304
305 rcu_read_lock();
306 hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
307 if (!batadv_compare_eth(pos->other_end, dst))
308 continue;
309
310 if (memcmp(pos->session, session, sizeof(pos->session)) != 0)
311 continue;
312
313
314
315
316
317 if (unlikely(!kref_get_unless_zero(&pos->refcount)))
318 continue;
319
320 tp_vars = pos;
321 break;
322 }
323 rcu_read_unlock();
324
325 return tp_vars;
326 }
327
328
329
330
331
332
333 static void batadv_tp_vars_release(struct kref *ref)
334 {
335 struct batadv_tp_vars *tp_vars;
336 struct batadv_tp_unacked *un, *safe;
337
338 tp_vars = container_of(ref, struct batadv_tp_vars, refcount);
339
340
341
342
343 spin_lock_bh(&tp_vars->unacked_lock);
344 list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
345 list_del(&un->list);
346 kfree(un);
347 }
348 spin_unlock_bh(&tp_vars->unacked_lock);
349
350 kfree_rcu(tp_vars, rcu);
351 }
352
353
354
355
356
357
358 static void batadv_tp_vars_put(struct batadv_tp_vars *tp_vars)
359 {
360 kref_put(&tp_vars->refcount, batadv_tp_vars_release);
361 }
362
363
364
365
366
367
368 static void batadv_tp_sender_cleanup(struct batadv_priv *bat_priv,
369 struct batadv_tp_vars *tp_vars)
370 {
371 cancel_delayed_work(&tp_vars->finish_work);
372
373 spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
374 hlist_del_rcu(&tp_vars->list);
375 spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
376
377
378 batadv_tp_vars_put(tp_vars);
379
380 atomic_dec(&tp_vars->bat_priv->tp_num);
381
382
383 del_timer_sync(&tp_vars->timer);
384
385
386
387
388
389 del_timer(&tp_vars->timer);
390 batadv_tp_vars_put(tp_vars);
391 }
392
393
394
395
396
397
398 static void batadv_tp_sender_end(struct batadv_priv *bat_priv,
399 struct batadv_tp_vars *tp_vars)
400 {
401 u32 session_cookie;
402
403 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
404 "Test towards %pM finished..shutting down (reason=%d)\n",
405 tp_vars->other_end, tp_vars->reason);
406
407 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
408 "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
409 tp_vars->srtt >> 3, tp_vars->rttvar >> 2, tp_vars->rto);
410
411 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
412 "Final values: cwnd=%u ss_threshold=%u\n",
413 tp_vars->cwnd, tp_vars->ss_threshold);
414
415 session_cookie = batadv_tp_session_cookie(tp_vars->session,
416 tp_vars->icmp_uid);
417
418 batadv_tp_batctl_notify(tp_vars->reason,
419 tp_vars->other_end,
420 bat_priv,
421 tp_vars->start_time,
422 atomic64_read(&tp_vars->tot_sent),
423 session_cookie);
424 }
425
426
427
428
429
430
431 static void batadv_tp_sender_shutdown(struct batadv_tp_vars *tp_vars,
432 enum batadv_tp_meter_reason reason)
433 {
434 if (!atomic_dec_and_test(&tp_vars->sending))
435 return;
436
437 tp_vars->reason = reason;
438 }
439
440
441
442
443
444 static void batadv_tp_sender_finish(struct work_struct *work)
445 {
446 struct delayed_work *delayed_work;
447 struct batadv_tp_vars *tp_vars;
448
449 delayed_work = to_delayed_work(work);
450 tp_vars = container_of(delayed_work, struct batadv_tp_vars,
451 finish_work);
452
453 batadv_tp_sender_shutdown(tp_vars, BATADV_TP_REASON_COMPLETE);
454 }
455
456
457
458
459
460
461
462 static void batadv_tp_reset_sender_timer(struct batadv_tp_vars *tp_vars)
463 {
464
465
466
467 if (unlikely(atomic_read(&tp_vars->sending) == 0))
468
469 return;
470
471 mod_timer(&tp_vars->timer, jiffies + msecs_to_jiffies(tp_vars->rto));
472 }
473
474
475
476
477
478
479
480
481
482 static void batadv_tp_sender_timeout(struct timer_list *t)
483 {
484 struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
485 struct batadv_priv *bat_priv = tp_vars->bat_priv;
486
487 if (atomic_read(&tp_vars->sending) == 0)
488 return;
489
490
491 if (unlikely(tp_vars->rto >= BATADV_TP_MAX_RTO)) {
492 batadv_tp_sender_shutdown(tp_vars,
493 BATADV_TP_REASON_DST_UNREACHABLE);
494 return;
495 }
496
497
498
499
500 tp_vars->rto <<= 1;
501
502 spin_lock_bh(&tp_vars->cwnd_lock);
503
504 tp_vars->ss_threshold = tp_vars->cwnd >> 1;
505 if (tp_vars->ss_threshold < BATADV_TP_PLEN * 2)
506 tp_vars->ss_threshold = BATADV_TP_PLEN * 2;
507
508 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
509 "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
510 tp_vars->other_end, tp_vars->cwnd, tp_vars->ss_threshold,
511 atomic_read(&tp_vars->last_acked));
512
513 tp_vars->cwnd = BATADV_TP_PLEN * 3;
514
515 spin_unlock_bh(&tp_vars->cwnd_lock);
516
517
518 tp_vars->last_sent = atomic_read(&tp_vars->last_acked);
519 wake_up(&tp_vars->more_bytes);
520
521 batadv_tp_reset_sender_timer(tp_vars);
522 }
523
524
525
526
527
528
529
530 static void batadv_tp_fill_prerandom(struct batadv_tp_vars *tp_vars,
531 u8 *buf, size_t nbytes)
532 {
533 u32 local_offset;
534 size_t bytes_inbuf;
535 size_t to_copy;
536 size_t pos = 0;
537
538 spin_lock_bh(&tp_vars->prerandom_lock);
539 local_offset = tp_vars->prerandom_offset;
540 tp_vars->prerandom_offset += nbytes;
541 tp_vars->prerandom_offset %= sizeof(batadv_tp_prerandom);
542 spin_unlock_bh(&tp_vars->prerandom_lock);
543
544 while (nbytes) {
545 local_offset %= sizeof(batadv_tp_prerandom);
546 bytes_inbuf = sizeof(batadv_tp_prerandom) - local_offset;
547 to_copy = min(nbytes, bytes_inbuf);
548
549 memcpy(&buf[pos], &batadv_tp_prerandom[local_offset], to_copy);
550 pos += to_copy;
551 nbytes -= to_copy;
552 local_offset = 0;
553 }
554 }
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573 static int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src,
574 struct batadv_orig_node *orig_node,
575 u32 seqno, size_t len, const u8 *session,
576 int uid, u32 timestamp)
577 {
578 struct batadv_icmp_tp_packet *icmp;
579 struct sk_buff *skb;
580 int r;
581 u8 *data;
582 size_t data_len;
583
584 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
585 if (unlikely(!skb))
586 return BATADV_TP_REASON_MEMORY_ERROR;
587
588 skb_reserve(skb, ETH_HLEN);
589 icmp = skb_put(skb, sizeof(*icmp));
590
591
592 ether_addr_copy(icmp->dst, orig_node->orig);
593 ether_addr_copy(icmp->orig, src);
594 icmp->version = BATADV_COMPAT_VERSION;
595 icmp->packet_type = BATADV_ICMP;
596 icmp->ttl = BATADV_TTL;
597 icmp->msg_type = BATADV_TP;
598 icmp->uid = uid;
599
600 icmp->subtype = BATADV_TP_MSG;
601 memcpy(icmp->session, session, sizeof(icmp->session));
602 icmp->seqno = htonl(seqno);
603 icmp->timestamp = htonl(timestamp);
604
605 data_len = len - sizeof(*icmp);
606 data = skb_put(skb, data_len);
607 batadv_tp_fill_prerandom(tp_vars, data, data_len);
608
609 r = batadv_send_skb_to_orig(skb, orig_node, NULL);
610 if (r == NET_XMIT_SUCCESS)
611 return 0;
612
613 return BATADV_TP_REASON_CANT_SEND;
614 }
615
616
617
618
619
620
621
622
623 static void batadv_tp_recv_ack(struct batadv_priv *bat_priv,
624 const struct sk_buff *skb)
625 {
626 struct batadv_hard_iface *primary_if = NULL;
627 struct batadv_orig_node *orig_node = NULL;
628 const struct batadv_icmp_tp_packet *icmp;
629 struct batadv_tp_vars *tp_vars;
630 size_t packet_len, mss;
631 u32 rtt, recv_ack, cwnd;
632 unsigned char *dev_addr;
633
634 packet_len = BATADV_TP_PLEN;
635 mss = BATADV_TP_PLEN;
636 packet_len += sizeof(struct batadv_unicast_packet);
637
638 icmp = (struct batadv_icmp_tp_packet *)skb->data;
639
640
641 tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
642 icmp->session);
643 if (unlikely(!tp_vars))
644 return;
645
646 if (unlikely(atomic_read(&tp_vars->sending) == 0))
647 goto out;
648
649
650 if (batadv_seq_before(ntohl(icmp->seqno),
651 (u32)atomic_read(&tp_vars->last_acked)))
652 goto out;
653
654 primary_if = batadv_primary_if_get_selected(bat_priv);
655 if (unlikely(!primary_if))
656 goto out;
657
658 orig_node = batadv_orig_hash_find(bat_priv, icmp->orig);
659 if (unlikely(!orig_node))
660 goto out;
661
662
663 rtt = jiffies_to_msecs(jiffies) - ntohl(icmp->timestamp);
664 if (icmp->timestamp && rtt)
665 batadv_tp_update_rto(tp_vars, rtt);
666
667
668 batadv_tp_reset_sender_timer(tp_vars);
669
670 recv_ack = ntohl(icmp->seqno);
671
672
673 if (atomic_read(&tp_vars->last_acked) == recv_ack) {
674 atomic_inc(&tp_vars->dup_acks);
675 if (atomic_read(&tp_vars->dup_acks) != 3)
676 goto out;
677
678 if (recv_ack >= tp_vars->recover)
679 goto out;
680
681
682 batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
683 orig_node, recv_ack, packet_len,
684 icmp->session, icmp->uid,
685 jiffies_to_msecs(jiffies));
686
687 spin_lock_bh(&tp_vars->cwnd_lock);
688
689
690 tp_vars->fast_recovery = true;
691
692
693
694 tp_vars->recover = tp_vars->last_sent;
695 tp_vars->ss_threshold = tp_vars->cwnd >> 1;
696 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
697 "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
698 tp_vars->cwnd, tp_vars->ss_threshold,
699 tp_vars->last_sent, recv_ack);
700 tp_vars->cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 3 * mss,
701 mss);
702 tp_vars->dec_cwnd = 0;
703 tp_vars->last_sent = recv_ack;
704
705 spin_unlock_bh(&tp_vars->cwnd_lock);
706 } else {
707
708 atomic64_add(recv_ack - atomic_read(&tp_vars->last_acked),
709 &tp_vars->tot_sent);
710
711 atomic_set(&tp_vars->dup_acks, 0);
712
713 if (tp_vars->fast_recovery) {
714
715 if (batadv_seq_before(recv_ack, tp_vars->recover)) {
716
717
718
719
720 dev_addr = primary_if->net_dev->dev_addr;
721 batadv_tp_send_msg(tp_vars, dev_addr,
722 orig_node, recv_ack,
723 packet_len, icmp->session,
724 icmp->uid,
725 jiffies_to_msecs(jiffies));
726 tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd,
727 mss, mss);
728 } else {
729 tp_vars->fast_recovery = false;
730
731
732
733
734 cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 0,
735 mss);
736 tp_vars->cwnd = cwnd;
737 }
738 goto move_twnd;
739 }
740
741 if (recv_ack - atomic_read(&tp_vars->last_acked) >= mss)
742 batadv_tp_update_cwnd(tp_vars, mss);
743 move_twnd:
744
745 atomic_set(&tp_vars->last_acked, recv_ack);
746 }
747
748 wake_up(&tp_vars->more_bytes);
749 out:
750 if (likely(primary_if))
751 batadv_hardif_put(primary_if);
752 if (likely(orig_node))
753 batadv_orig_node_put(orig_node);
754 if (likely(tp_vars))
755 batadv_tp_vars_put(tp_vars);
756 }
757
758
759
760
761
762
763
764
765 static bool batadv_tp_avail(struct batadv_tp_vars *tp_vars,
766 size_t payload_len)
767 {
768 u32 win_left, win_limit;
769
770 win_limit = atomic_read(&tp_vars->last_acked) + tp_vars->cwnd;
771 win_left = win_limit - tp_vars->last_sent;
772
773 return win_left >= payload_len;
774 }
775
776
777
778
779
780
781
782
783
784
785
786
787 static int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
788 {
789 int ret;
790
791 ret = wait_event_interruptible_timeout(tp_vars->more_bytes,
792 batadv_tp_avail(tp_vars, plen),
793 HZ / 10);
794
795 return ret;
796 }
797
798
799
800
801
802
803
804 static int batadv_tp_send(void *arg)
805 {
806 struct batadv_tp_vars *tp_vars = arg;
807 struct batadv_priv *bat_priv = tp_vars->bat_priv;
808 struct batadv_hard_iface *primary_if = NULL;
809 struct batadv_orig_node *orig_node = NULL;
810 size_t payload_len, packet_len;
811 int err = 0;
812
813 if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
814 err = BATADV_TP_REASON_DST_UNREACHABLE;
815 tp_vars->reason = err;
816 goto out;
817 }
818
819 orig_node = batadv_orig_hash_find(bat_priv, tp_vars->other_end);
820 if (unlikely(!orig_node)) {
821 err = BATADV_TP_REASON_DST_UNREACHABLE;
822 tp_vars->reason = err;
823 goto out;
824 }
825
826 primary_if = batadv_primary_if_get_selected(bat_priv);
827 if (unlikely(!primary_if)) {
828 err = BATADV_TP_REASON_DST_UNREACHABLE;
829 tp_vars->reason = err;
830 goto out;
831 }
832
833
834
835
836
837
838
839 payload_len = BATADV_TP_PLEN;
840 BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet) > BATADV_TP_PLEN);
841
842 batadv_tp_reset_sender_timer(tp_vars);
843
844
845 queue_delayed_work(batadv_event_workqueue, &tp_vars->finish_work,
846 msecs_to_jiffies(tp_vars->test_length));
847
848 while (atomic_read(&tp_vars->sending) != 0) {
849 if (unlikely(!batadv_tp_avail(tp_vars, payload_len))) {
850 batadv_tp_wait_available(tp_vars, payload_len);
851 continue;
852 }
853
854
855
856
857 packet_len = payload_len + sizeof(struct batadv_unicast_packet);
858
859 err = batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
860 orig_node, tp_vars->last_sent,
861 packet_len,
862 tp_vars->session, tp_vars->icmp_uid,
863 jiffies_to_msecs(jiffies));
864
865
866 if (unlikely(err && err != BATADV_TP_REASON_CANT_SEND)) {
867 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
868 "Meter: %s() cannot send packets (%d)\n",
869 __func__, err);
870
871 if (atomic_dec_and_test(&tp_vars->sending))
872 tp_vars->reason = err;
873 break;
874 }
875
876
877 if (!err)
878 tp_vars->last_sent += payload_len;
879
880 cond_resched();
881 }
882
883 out:
884 if (likely(primary_if))
885 batadv_hardif_put(primary_if);
886 if (likely(orig_node))
887 batadv_orig_node_put(orig_node);
888
889 batadv_tp_sender_end(bat_priv, tp_vars);
890 batadv_tp_sender_cleanup(bat_priv, tp_vars);
891
892 batadv_tp_vars_put(tp_vars);
893
894 do_exit(0);
895 }
896
897
898
899
900
901
902 static void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
903 {
904 struct task_struct *kthread;
905 struct batadv_priv *bat_priv = tp_vars->bat_priv;
906 u32 session_cookie;
907
908 kref_get(&tp_vars->refcount);
909 kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
910 if (IS_ERR(kthread)) {
911 session_cookie = batadv_tp_session_cookie(tp_vars->session,
912 tp_vars->icmp_uid);
913 pr_err("batadv: cannot create tp meter kthread\n");
914 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
915 tp_vars->other_end,
916 bat_priv, session_cookie);
917
918
919 batadv_tp_vars_put(tp_vars);
920
921
922 batadv_tp_sender_cleanup(bat_priv, tp_vars);
923 return;
924 }
925
926 wake_up_process(kthread);
927 }
928
929
930
931
932
933
934
935
936 void batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
937 u32 test_length, u32 *cookie)
938 {
939 struct batadv_tp_vars *tp_vars;
940 u8 session_id[2];
941 u8 icmp_uid;
942 u32 session_cookie;
943
944 get_random_bytes(session_id, sizeof(session_id));
945 get_random_bytes(&icmp_uid, 1);
946 session_cookie = batadv_tp_session_cookie(session_id, icmp_uid);
947 *cookie = session_cookie;
948
949
950 spin_lock_bh(&bat_priv->tp_list_lock);
951 tp_vars = batadv_tp_list_find(bat_priv, dst);
952 if (tp_vars) {
953 spin_unlock_bh(&bat_priv->tp_list_lock);
954 batadv_tp_vars_put(tp_vars);
955 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
956 "Meter: test to or from the same node already ongoing, aborting\n");
957 batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING,
958 dst, bat_priv, session_cookie);
959 return;
960 }
961
962 if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
963 spin_unlock_bh(&bat_priv->tp_list_lock);
964 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
965 "Meter: too many ongoing sessions, aborting (SEND)\n");
966 batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY, dst,
967 bat_priv, session_cookie);
968 return;
969 }
970
971 tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
972 if (!tp_vars) {
973 spin_unlock_bh(&bat_priv->tp_list_lock);
974 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
975 "Meter: %s cannot allocate list elements\n",
976 __func__);
977 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
978 dst, bat_priv, session_cookie);
979 return;
980 }
981
982
983 ether_addr_copy(tp_vars->other_end, dst);
984 kref_init(&tp_vars->refcount);
985 tp_vars->role = BATADV_TP_SENDER;
986 atomic_set(&tp_vars->sending, 1);
987 memcpy(tp_vars->session, session_id, sizeof(session_id));
988 tp_vars->icmp_uid = icmp_uid;
989
990 tp_vars->last_sent = BATADV_TP_FIRST_SEQ;
991 atomic_set(&tp_vars->last_acked, BATADV_TP_FIRST_SEQ);
992 tp_vars->fast_recovery = false;
993 tp_vars->recover = BATADV_TP_FIRST_SEQ;
994
995
996
997
998
999 tp_vars->cwnd = BATADV_TP_PLEN * 3;
1000
1001
1002
1003 tp_vars->ss_threshold = BATADV_TP_AWND;
1004
1005
1006
1007
1008 tp_vars->rto = 1000;
1009 tp_vars->srtt = 0;
1010 tp_vars->rttvar = 0;
1011
1012 atomic64_set(&tp_vars->tot_sent, 0);
1013
1014 kref_get(&tp_vars->refcount);
1015 timer_setup(&tp_vars->timer, batadv_tp_sender_timeout, 0);
1016
1017 tp_vars->bat_priv = bat_priv;
1018 tp_vars->start_time = jiffies;
1019
1020 init_waitqueue_head(&tp_vars->more_bytes);
1021
1022 spin_lock_init(&tp_vars->unacked_lock);
1023 INIT_LIST_HEAD(&tp_vars->unacked_list);
1024
1025 spin_lock_init(&tp_vars->cwnd_lock);
1026
1027 tp_vars->prerandom_offset = 0;
1028 spin_lock_init(&tp_vars->prerandom_lock);
1029
1030 kref_get(&tp_vars->refcount);
1031 hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
1032 spin_unlock_bh(&bat_priv->tp_list_lock);
1033
1034 tp_vars->test_length = test_length;
1035 if (!tp_vars->test_length)
1036 tp_vars->test_length = BATADV_TP_DEF_TEST_LENGTH;
1037
1038 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1039 "Meter: starting throughput meter towards %pM (length=%ums)\n",
1040 dst, test_length);
1041
1042
1043 INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
1044
1045
1046
1047
1048 batadv_tp_start_kthread(tp_vars);
1049
1050
1051 batadv_tp_vars_put(tp_vars);
1052 }
1053
1054
1055
1056
1057
1058
1059
1060 void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
1061 u8 return_value)
1062 {
1063 struct batadv_orig_node *orig_node;
1064 struct batadv_tp_vars *tp_vars;
1065
1066 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1067 "Meter: stopping test towards %pM\n", dst);
1068
1069 orig_node = batadv_orig_hash_find(bat_priv, dst);
1070 if (!orig_node)
1071 return;
1072
1073 tp_vars = batadv_tp_list_find(bat_priv, orig_node->orig);
1074 if (!tp_vars) {
1075 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1076 "Meter: trying to interrupt an already over connection\n");
1077 goto out;
1078 }
1079
1080 batadv_tp_sender_shutdown(tp_vars, return_value);
1081 batadv_tp_vars_put(tp_vars);
1082 out:
1083 batadv_orig_node_put(orig_node);
1084 }
1085
1086
1087
1088
1089
1090
1091
1092 static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars *tp_vars)
1093 {
1094 mod_timer(&tp_vars->timer,
1095 jiffies + msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT));
1096 }
1097
1098
1099
1100
1101
1102
1103 static void batadv_tp_receiver_shutdown(struct timer_list *t)
1104 {
1105 struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
1106 struct batadv_tp_unacked *un, *safe;
1107 struct batadv_priv *bat_priv;
1108
1109 bat_priv = tp_vars->bat_priv;
1110
1111
1112 if (!batadv_has_timed_out(tp_vars->last_recv_time,
1113 BATADV_TP_RECV_TIMEOUT)) {
1114
1115 batadv_tp_reset_receiver_timer(tp_vars);
1116 return;
1117 }
1118
1119 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1120 "Shutting down for inactivity (more than %dms) from %pM\n",
1121 BATADV_TP_RECV_TIMEOUT, tp_vars->other_end);
1122
1123 spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
1124 hlist_del_rcu(&tp_vars->list);
1125 spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
1126
1127
1128 batadv_tp_vars_put(tp_vars);
1129
1130 atomic_dec(&bat_priv->tp_num);
1131
1132 spin_lock_bh(&tp_vars->unacked_lock);
1133 list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
1134 list_del(&un->list);
1135 kfree(un);
1136 }
1137 spin_unlock_bh(&tp_vars->unacked_lock);
1138
1139
1140 batadv_tp_vars_put(tp_vars);
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155 static int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst,
1156 u32 seq, __be32 timestamp, const u8 *session,
1157 int socket_index)
1158 {
1159 struct batadv_hard_iface *primary_if = NULL;
1160 struct batadv_orig_node *orig_node;
1161 struct batadv_icmp_tp_packet *icmp;
1162 struct sk_buff *skb;
1163 int r, ret;
1164
1165 orig_node = batadv_orig_hash_find(bat_priv, dst);
1166 if (unlikely(!orig_node)) {
1167 ret = BATADV_TP_REASON_DST_UNREACHABLE;
1168 goto out;
1169 }
1170
1171 primary_if = batadv_primary_if_get_selected(bat_priv);
1172 if (unlikely(!primary_if)) {
1173 ret = BATADV_TP_REASON_DST_UNREACHABLE;
1174 goto out;
1175 }
1176
1177 skb = netdev_alloc_skb_ip_align(NULL, sizeof(*icmp) + ETH_HLEN);
1178 if (unlikely(!skb)) {
1179 ret = BATADV_TP_REASON_MEMORY_ERROR;
1180 goto out;
1181 }
1182
1183 skb_reserve(skb, ETH_HLEN);
1184 icmp = skb_put(skb, sizeof(*icmp));
1185 icmp->packet_type = BATADV_ICMP;
1186 icmp->version = BATADV_COMPAT_VERSION;
1187 icmp->ttl = BATADV_TTL;
1188 icmp->msg_type = BATADV_TP;
1189 ether_addr_copy(icmp->dst, orig_node->orig);
1190 ether_addr_copy(icmp->orig, primary_if->net_dev->dev_addr);
1191 icmp->uid = socket_index;
1192
1193 icmp->subtype = BATADV_TP_ACK;
1194 memcpy(icmp->session, session, sizeof(icmp->session));
1195 icmp->seqno = htonl(seq);
1196 icmp->timestamp = timestamp;
1197
1198
1199 r = batadv_send_skb_to_orig(skb, orig_node, NULL);
1200 if (unlikely(r < 0) || r == NET_XMIT_DROP) {
1201 ret = BATADV_TP_REASON_DST_UNREACHABLE;
1202 goto out;
1203 }
1204 ret = 0;
1205
1206 out:
1207 if (likely(orig_node))
1208 batadv_orig_node_put(orig_node);
1209 if (likely(primary_if))
1210 batadv_hardif_put(primary_if);
1211
1212 return ret;
1213 }
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226 static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars *tp_vars,
1227 const struct sk_buff *skb)
1228 {
1229 const struct batadv_icmp_tp_packet *icmp;
1230 struct batadv_tp_unacked *un, *new;
1231 u32 payload_len;
1232 bool added = false;
1233
1234 new = kmalloc(sizeof(*new), GFP_ATOMIC);
1235 if (unlikely(!new))
1236 return false;
1237
1238 icmp = (struct batadv_icmp_tp_packet *)skb->data;
1239
1240 new->seqno = ntohl(icmp->seqno);
1241 payload_len = skb->len - sizeof(struct batadv_unicast_packet);
1242 new->len = payload_len;
1243
1244 spin_lock_bh(&tp_vars->unacked_lock);
1245
1246 if (list_empty(&tp_vars->unacked_list)) {
1247 list_add(&new->list, &tp_vars->unacked_list);
1248 goto out;
1249 }
1250
1251
1252
1253
1254
1255
1256
1257
1258 list_for_each_entry_reverse(un, &tp_vars->unacked_list, list) {
1259
1260 if (new->seqno == un->seqno) {
1261 if (new->len > un->len)
1262 un->len = new->len;
1263 kfree(new);
1264 added = true;
1265 break;
1266 }
1267
1268
1269 if (batadv_seq_before(new->seqno, un->seqno))
1270 continue;
1271
1272
1273
1274
1275
1276 list_add_tail(&new->list, &un->list);
1277 added = true;
1278 break;
1279 }
1280
1281
1282 if (!added)
1283 list_add(&new->list, &tp_vars->unacked_list);
1284
1285 out:
1286 spin_unlock_bh(&tp_vars->unacked_lock);
1287
1288 return true;
1289 }
1290
1291
1292
1293
1294
1295
1296 static void batadv_tp_ack_unordered(struct batadv_tp_vars *tp_vars)
1297 {
1298 struct batadv_tp_unacked *un, *safe;
1299 u32 to_ack;
1300
1301
1302
1303
1304 spin_lock_bh(&tp_vars->unacked_lock);
1305 list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
1306
1307
1308
1309
1310 if (batadv_seq_before(tp_vars->last_recv, un->seqno))
1311 break;
1312
1313 to_ack = un->seqno + un->len - tp_vars->last_recv;
1314
1315 if (batadv_seq_before(tp_vars->last_recv, un->seqno + un->len))
1316 tp_vars->last_recv += to_ack;
1317
1318 list_del(&un->list);
1319 kfree(un);
1320 }
1321 spin_unlock_bh(&tp_vars->unacked_lock);
1322 }
1323
1324
1325
1326
1327
1328
1329
1330
1331 static struct batadv_tp_vars *
1332 batadv_tp_init_recv(struct batadv_priv *bat_priv,
1333 const struct batadv_icmp_tp_packet *icmp)
1334 {
1335 struct batadv_tp_vars *tp_vars;
1336
1337 spin_lock_bh(&bat_priv->tp_list_lock);
1338 tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
1339 icmp->session);
1340 if (tp_vars)
1341 goto out_unlock;
1342
1343 if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
1344 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1345 "Meter: too many ongoing sessions, aborting (RECV)\n");
1346 goto out_unlock;
1347 }
1348
1349 tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
1350 if (!tp_vars)
1351 goto out_unlock;
1352
1353 ether_addr_copy(tp_vars->other_end, icmp->orig);
1354 tp_vars->role = BATADV_TP_RECEIVER;
1355 memcpy(tp_vars->session, icmp->session, sizeof(tp_vars->session));
1356 tp_vars->last_recv = BATADV_TP_FIRST_SEQ;
1357 tp_vars->bat_priv = bat_priv;
1358 kref_init(&tp_vars->refcount);
1359
1360 spin_lock_init(&tp_vars->unacked_lock);
1361 INIT_LIST_HEAD(&tp_vars->unacked_list);
1362
1363 kref_get(&tp_vars->refcount);
1364 hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
1365
1366 kref_get(&tp_vars->refcount);
1367 timer_setup(&tp_vars->timer, batadv_tp_receiver_shutdown, 0);
1368
1369 batadv_tp_reset_receiver_timer(tp_vars);
1370
1371 out_unlock:
1372 spin_unlock_bh(&bat_priv->tp_list_lock);
1373
1374 return tp_vars;
1375 }
1376
1377
1378
1379
1380
1381
1382
1383
1384 static void batadv_tp_recv_msg(struct batadv_priv *bat_priv,
1385 const struct sk_buff *skb)
1386 {
1387 const struct batadv_icmp_tp_packet *icmp;
1388 struct batadv_tp_vars *tp_vars;
1389 size_t packet_size;
1390 u32 seqno;
1391
1392 icmp = (struct batadv_icmp_tp_packet *)skb->data;
1393
1394 seqno = ntohl(icmp->seqno);
1395
1396
1397
1398 if (seqno == BATADV_TP_FIRST_SEQ) {
1399 tp_vars = batadv_tp_init_recv(bat_priv, icmp);
1400 if (!tp_vars) {
1401 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1402 "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
1403 goto out;
1404 }
1405 } else {
1406 tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
1407 icmp->session);
1408 if (!tp_vars) {
1409 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1410 "Unexpected packet from %pM!\n",
1411 icmp->orig);
1412 goto out;
1413 }
1414 }
1415
1416 if (unlikely(tp_vars->role != BATADV_TP_RECEIVER)) {
1417 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1418 "Meter: dropping packet: not expected (role=%u)\n",
1419 tp_vars->role);
1420 goto out;
1421 }
1422
1423 tp_vars->last_recv_time = jiffies;
1424
1425
1426
1427
1428 if (batadv_seq_before(seqno, tp_vars->last_recv))
1429 goto send_ack;
1430
1431
1432 if (ntohl(icmp->seqno) != tp_vars->last_recv) {
1433
1434
1435
1436 if (!batadv_tp_handle_out_of_order(tp_vars, skb))
1437 goto out;
1438
1439
1440 goto send_ack;
1441 }
1442
1443
1444 packet_size = skb->len - sizeof(struct batadv_unicast_packet);
1445 tp_vars->last_recv += packet_size;
1446
1447
1448 batadv_tp_ack_unordered(tp_vars);
1449
1450 send_ack:
1451
1452
1453
1454
1455 batadv_tp_send_ack(bat_priv, icmp->orig, tp_vars->last_recv,
1456 icmp->timestamp, icmp->session, icmp->uid);
1457 out:
1458 if (likely(tp_vars))
1459 batadv_tp_vars_put(tp_vars);
1460 }
1461
1462
1463
1464
1465
1466
1467 void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
1468 {
1469 struct batadv_icmp_tp_packet *icmp;
1470
1471 icmp = (struct batadv_icmp_tp_packet *)skb->data;
1472
1473 switch (icmp->subtype) {
1474 case BATADV_TP_MSG:
1475 batadv_tp_recv_msg(bat_priv, skb);
1476 break;
1477 case BATADV_TP_ACK:
1478 batadv_tp_recv_ack(bat_priv, skb);
1479 break;
1480 default:
1481 batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
1482 "Received unknown TP Metric packet type %u\n",
1483 icmp->subtype);
1484 }
1485 consume_skb(skb);
1486 }
1487
1488
1489
1490
1491 void __init batadv_tp_meter_init(void)
1492 {
1493 get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
1494 }