1 /* Copyright (C) 2012-2017 B.A.T.M.A.N. contributors:
3 * Edo Monticelli, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <linux/atomic.h>
22 #include <linux/bug.h>
23 #include <linux/byteorder/generic.h>
24 #include <linux/cache.h>
25 #include <linux/compiler.h>
26 #include <linux/err.h>
27 #include <linux/etherdevice.h>
29 #include <linux/if_ether.h>
30 #include <linux/init.h>
31 #include <linux/jiffies.h>
32 #include <linux/kernel.h>
33 #include <linux/kref.h>
34 #include <linux/kthread.h>
35 #include <linux/list.h>
36 #include <linux/netdevice.h>
37 #include <linux/param.h>
38 #include <linux/printk.h>
39 #include <linux/random.h>
40 #include <linux/rculist.h>
41 #include <linux/rcupdate.h>
42 #include <linux/sched.h>
43 #include <linux/skbuff.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
46 #include <linux/stddef.h>
47 #include <linux/string.h>
48 #include <linux/timer.h>
49 #include <linux/wait.h>
50 #include <linux/workqueue.h>
51 #include <uapi/linux/batman_adv.h>
53 #include "hard-interface.h"
56 #include "originator.h"
61 * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
64 #define BATADV_TP_DEF_TEST_LENGTH 10000
67 * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
69 #define BATADV_TP_AWND 0x20000000
72 * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
73 * get anything for such amount of milliseconds, the connection is killed
75 #define BATADV_TP_RECV_TIMEOUT 1000
78 * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
79 * such amound of milliseconds, the receiver is considered unreachable and the
80 * connection is killed
82 #define BATADV_TP_MAX_RTO 30000
85 * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
86 * in order to immediately trigger a wrap around (test purposes)
88 #define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
91 * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
94 #define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
95 sizeof(struct batadv_unicast_packet))
97 static u8 batadv_tp_prerandom
[4096] __read_mostly
;
100 * batadv_tp_session_cookie - generate session cookie based on session ids
101 * @session: TP session identifier
102 * @icmp_uid: icmp pseudo uid of the tp session
104 * Return: 32 bit tp_meter session cookie
106 static u32
batadv_tp_session_cookie(const u8 session
[2], u8 icmp_uid
)
110 cookie
= icmp_uid
<< 16;
111 cookie
|= session
[0] << 8;
112 cookie
|= session
[1];
118 * batadv_tp_cwnd - compute the new cwnd size
119 * @base: base cwnd size value
120 * @increment: the value to add to base to get the new size
121 * @min: minumim cwnd value (usually MSS)
123 * Return the new cwnd size and ensures it does not exceed the Advertised
124 * Receiver Window size. It is wrap around safe.
125 * For details refer to Section 3.1 of RFC5681
127 * Return: new congestion window size in bytes
129 static u32
batadv_tp_cwnd(u32 base
, u32 increment
, u32 min
)
131 u32 new_size
= base
+ increment
;
133 /* check for wrap-around */
135 new_size
= (u32
)ULONG_MAX
;
137 new_size
= min_t(u32
, new_size
, BATADV_TP_AWND
);
139 return max_t(u32
, new_size
, min
);
143 * batadv_tp_updated_cwnd - update the Congestion Windows
144 * @tp_vars: the private data of the current TP meter session
145 * @mss: maximum segment size of transmission
147 * 1) if the session is in Slow Start, the CWND has to be increased by 1
148 * MSS every unique received ACK
149 * 2) if the session is in Congestion Avoidance, the CWND has to be
150 * increased by MSS * MSS / CWND for every unique received ACK
152 static void batadv_tp_update_cwnd(struct batadv_tp_vars
*tp_vars
, u32 mss
)
154 spin_lock_bh(&tp_vars
->cwnd_lock
);
157 if (tp_vars
->cwnd
<= tp_vars
->ss_threshold
) {
158 tp_vars
->dec_cwnd
= 0;
159 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
, mss
, mss
);
160 spin_unlock_bh(&tp_vars
->cwnd_lock
);
164 /* increment CWND at least of 1 (section 3.1 of RFC5681) */
165 tp_vars
->dec_cwnd
+= max_t(u32
, 1U << 3,
166 ((mss
* mss
) << 6) / (tp_vars
->cwnd
<< 3));
167 if (tp_vars
->dec_cwnd
< (mss
<< 3)) {
168 spin_unlock_bh(&tp_vars
->cwnd_lock
);
172 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
, mss
, mss
);
173 tp_vars
->dec_cwnd
= 0;
175 spin_unlock_bh(&tp_vars
->cwnd_lock
);
179 * batadv_tp_update_rto - calculate new retransmission timeout
180 * @tp_vars: the private data of the current TP meter session
181 * @new_rtt: new roundtrip time in msec
183 static void batadv_tp_update_rto(struct batadv_tp_vars
*tp_vars
,
189 * Details in Section 2.2 and 2.3 of RFC6298
191 * It's tricky to understand. Don't lose hair please.
192 * Inspired by tcp_rtt_estimator() tcp_input.c
194 if (tp_vars
->srtt
!= 0) {
195 m
-= (tp_vars
->srtt
>> 3); /* m is now error in rtt est */
196 tp_vars
->srtt
+= m
; /* rtt = 7/8 srtt + 1/8 new */
200 m
-= (tp_vars
->rttvar
>> 2);
201 tp_vars
->rttvar
+= m
; /* mdev ~= 3/4 rttvar + 1/4 new */
203 /* first measure getting in */
204 tp_vars
->srtt
= m
<< 3; /* take the measured time to be srtt */
205 tp_vars
->rttvar
= m
<< 1; /* new_rtt / 2 */
208 /* rto = srtt + 4 * rttvar.
209 * rttvar is scaled by 4, therefore doesn't need to be multiplied
211 tp_vars
->rto
= (tp_vars
->srtt
>> 3) + tp_vars
->rttvar
;
215 * batadv_tp_batctl_notify - send client status result to client
216 * @reason: reason for tp meter session stop
217 * @dst: destination of tp_meter session
218 * @bat_priv: the bat priv with all the soft interface information
219 * @start_time: start of transmission in jiffies
220 * @total_sent: bytes acked to the receiver
221 * @cookie: cookie of tp_meter session
223 static void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason
,
224 const u8
*dst
, struct batadv_priv
*bat_priv
,
225 unsigned long start_time
, u64 total_sent
,
232 if (!batadv_tp_is_error(reason
)) {
233 result
= BATADV_TP_REASON_COMPLETE
;
234 test_time
= jiffies_to_msecs(jiffies
- start_time
);
235 total_bytes
= total_sent
;
242 batadv_netlink_tpmeter_notify(bat_priv
, dst
, result
, test_time
,
243 total_bytes
, cookie
);
247 * batadv_tp_batctl_error_notify - send client error result to client
248 * @reason: reason for tp meter session stop
249 * @dst: destination of tp_meter session
250 * @bat_priv: the bat priv with all the soft interface information
251 * @cookie: cookie of tp_meter session
253 static void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason
,
255 struct batadv_priv
*bat_priv
,
258 batadv_tp_batctl_notify(reason
, dst
, bat_priv
, 0, 0, cookie
);
262 * batadv_tp_list_find - find a tp_vars object in the global list
263 * @bat_priv: the bat priv with all the soft interface information
264 * @dst: the other endpoint MAC address to look for
266 * Look for a tp_vars object matching dst as end_point and return it after
267 * having incremented the refcounter. Return NULL is not found
269 * Return: matching tp_vars or NULL when no tp_vars with @dst was found
271 static struct batadv_tp_vars
*batadv_tp_list_find(struct batadv_priv
*bat_priv
,
274 struct batadv_tp_vars
*pos
, *tp_vars
= NULL
;
277 hlist_for_each_entry_rcu(pos
, &bat_priv
->tp_list
, list
) {
278 if (!batadv_compare_eth(pos
->other_end
, dst
))
281 /* most of the time this function is invoked during the normal
282 * process..it makes sens to pay more when the session is
283 * finished and to speed the process up during the measurement
285 if (unlikely(!kref_get_unless_zero(&pos
->refcount
)))
297 * batadv_tp_list_find_session - find tp_vars session object in the global list
298 * @bat_priv: the bat priv with all the soft interface information
299 * @dst: the other endpoint MAC address to look for
300 * @session: session identifier
302 * Look for a tp_vars object matching dst as end_point, session as tp meter
303 * session and return it after having incremented the refcounter. Return NULL
306 * Return: matching tp_vars or NULL when no tp_vars was found
308 static struct batadv_tp_vars
*
309 batadv_tp_list_find_session(struct batadv_priv
*bat_priv
, const u8
*dst
,
312 struct batadv_tp_vars
*pos
, *tp_vars
= NULL
;
315 hlist_for_each_entry_rcu(pos
, &bat_priv
->tp_list
, list
) {
316 if (!batadv_compare_eth(pos
->other_end
, dst
))
319 if (memcmp(pos
->session
, session
, sizeof(pos
->session
)) != 0)
322 /* most of the time this function is invoked during the normal
323 * process..it makes sense to pay more when the session is
324 * finished and to speed the process up during the measurement
326 if (unlikely(!kref_get_unless_zero(&pos
->refcount
)))
338 * batadv_tp_vars_release - release batadv_tp_vars from lists and queue for
339 * free after rcu grace period
340 * @ref: kref pointer of the batadv_tp_vars
342 static void batadv_tp_vars_release(struct kref
*ref
)
344 struct batadv_tp_vars
*tp_vars
;
345 struct batadv_tp_unacked
*un
, *safe
;
347 tp_vars
= container_of(ref
, struct batadv_tp_vars
, refcount
);
349 /* lock should not be needed because this object is now out of any
352 spin_lock_bh(&tp_vars
->unacked_lock
);
353 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
357 spin_unlock_bh(&tp_vars
->unacked_lock
);
359 kfree_rcu(tp_vars
, rcu
);
363 * batadv_tp_vars_put - decrement the batadv_tp_vars refcounter and possibly
365 * @tp_vars: the private data of the current TP meter session to be free'd
367 static void batadv_tp_vars_put(struct batadv_tp_vars
*tp_vars
)
369 kref_put(&tp_vars
->refcount
, batadv_tp_vars_release
);
373 * batadv_tp_sender_cleanup - cleanup sender data and drop and timer
374 * @bat_priv: the bat priv with all the soft interface information
375 * @tp_vars: the private data of the current TP meter session to cleanup
377 static void batadv_tp_sender_cleanup(struct batadv_priv
*bat_priv
,
378 struct batadv_tp_vars
*tp_vars
)
380 cancel_delayed_work(&tp_vars
->finish_work
);
382 spin_lock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
383 hlist_del_rcu(&tp_vars
->list
);
384 spin_unlock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
386 /* drop list reference */
387 batadv_tp_vars_put(tp_vars
);
389 atomic_dec(&tp_vars
->bat_priv
->tp_num
);
391 /* kill the timer and remove its reference */
392 del_timer_sync(&tp_vars
->timer
);
393 /* the worker might have rearmed itself therefore we kill it again. Note
394 * that if the worker should run again before invoking the following
395 * del_timer(), it would not re-arm itself once again because the status
398 del_timer(&tp_vars
->timer
);
399 batadv_tp_vars_put(tp_vars
);
403 * batadv_tp_sender_end - print info about ended session and inform client
404 * @bat_priv: the bat priv with all the soft interface information
405 * @tp_vars: the private data of the current TP meter session
407 static void batadv_tp_sender_end(struct batadv_priv
*bat_priv
,
408 struct batadv_tp_vars
*tp_vars
)
412 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
413 "Test towards %pM finished..shutting down (reason=%d)\n",
414 tp_vars
->other_end
, tp_vars
->reason
);
416 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
417 "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
418 tp_vars
->srtt
>> 3, tp_vars
->rttvar
>> 2, tp_vars
->rto
);
420 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
421 "Final values: cwnd=%u ss_threshold=%u\n",
422 tp_vars
->cwnd
, tp_vars
->ss_threshold
);
424 session_cookie
= batadv_tp_session_cookie(tp_vars
->session
,
427 batadv_tp_batctl_notify(tp_vars
->reason
,
431 atomic64_read(&tp_vars
->tot_sent
),
436 * batadv_tp_sender_shutdown - let sender thread/timer stop gracefully
437 * @tp_vars: the private data of the current TP meter session
438 * @reason: reason for tp meter session stop
440 static void batadv_tp_sender_shutdown(struct batadv_tp_vars
*tp_vars
,
441 enum batadv_tp_meter_reason reason
)
443 if (!atomic_dec_and_test(&tp_vars
->sending
))
446 tp_vars
->reason
= reason
;
450 * batadv_tp_sender_finish - stop sender session after test_length was reached
451 * @work: delayed work reference of the related tp_vars
453 static void batadv_tp_sender_finish(struct work_struct
*work
)
455 struct delayed_work
*delayed_work
;
456 struct batadv_tp_vars
*tp_vars
;
458 delayed_work
= to_delayed_work(work
);
459 tp_vars
= container_of(delayed_work
, struct batadv_tp_vars
,
462 batadv_tp_sender_shutdown(tp_vars
, BATADV_TP_REASON_COMPLETE
);
466 * batadv_tp_reset_sender_timer - reschedule the sender timer
467 * @tp_vars: the private TP meter data for this session
469 * Reschedule the timer using tp_vars->rto as delay
471 static void batadv_tp_reset_sender_timer(struct batadv_tp_vars
*tp_vars
)
473 /* most of the time this function is invoked while normal packet
476 if (unlikely(atomic_read(&tp_vars
->sending
) == 0))
477 /* timer ref will be dropped in batadv_tp_sender_cleanup */
480 mod_timer(&tp_vars
->timer
, jiffies
+ msecs_to_jiffies(tp_vars
->rto
));
484 * batadv_tp_sender_timeout - timer that fires in case of packet loss
485 * @t: address to timer_list inside tp_vars
487 * If fired it means that there was packet loss.
488 * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
489 * reset the cwnd to 3*MSS
491 static void batadv_tp_sender_timeout(struct timer_list
*t
)
493 struct batadv_tp_vars
*tp_vars
= from_timer(tp_vars
, t
, timer
);
494 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
496 if (atomic_read(&tp_vars
->sending
) == 0)
499 /* if the user waited long enough...shutdown the test */
500 if (unlikely(tp_vars
->rto
>= BATADV_TP_MAX_RTO
)) {
501 batadv_tp_sender_shutdown(tp_vars
,
502 BATADV_TP_REASON_DST_UNREACHABLE
);
506 /* RTO exponential backoff
507 * Details in Section 5.5 of RFC6298
511 spin_lock_bh(&tp_vars
->cwnd_lock
);
513 tp_vars
->ss_threshold
= tp_vars
->cwnd
>> 1;
514 if (tp_vars
->ss_threshold
< BATADV_TP_PLEN
* 2)
515 tp_vars
->ss_threshold
= BATADV_TP_PLEN
* 2;
517 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
518 "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
519 tp_vars
->other_end
, tp_vars
->cwnd
, tp_vars
->ss_threshold
,
520 atomic_read(&tp_vars
->last_acked
));
522 tp_vars
->cwnd
= BATADV_TP_PLEN
* 3;
524 spin_unlock_bh(&tp_vars
->cwnd_lock
);
526 /* resend the non-ACKed packets.. */
527 tp_vars
->last_sent
= atomic_read(&tp_vars
->last_acked
);
528 wake_up(&tp_vars
->more_bytes
);
530 batadv_tp_reset_sender_timer(tp_vars
);
534 * batadv_tp_fill_prerandom - Fill buffer with prefetched random bytes
535 * @tp_vars: the private TP meter data for this session
536 * @buf: Buffer to fill with bytes
537 * @nbytes: amount of pseudorandom bytes
539 static void batadv_tp_fill_prerandom(struct batadv_tp_vars
*tp_vars
,
540 u8
*buf
, size_t nbytes
)
547 spin_lock_bh(&tp_vars
->prerandom_lock
);
548 local_offset
= tp_vars
->prerandom_offset
;
549 tp_vars
->prerandom_offset
+= nbytes
;
550 tp_vars
->prerandom_offset
%= sizeof(batadv_tp_prerandom
);
551 spin_unlock_bh(&tp_vars
->prerandom_lock
);
554 local_offset
%= sizeof(batadv_tp_prerandom
);
555 bytes_inbuf
= sizeof(batadv_tp_prerandom
) - local_offset
;
556 to_copy
= min(nbytes
, bytes_inbuf
);
558 memcpy(&buf
[pos
], &batadv_tp_prerandom
[local_offset
], to_copy
);
566 * batadv_tp_send_msg - send a single message
567 * @tp_vars: the private TP meter data for this session
568 * @src: source mac address
569 * @orig_node: the originator of the destination
570 * @seqno: sequence number of this packet
571 * @len: length of the entire packet
572 * @session: session identifier
573 * @uid: local ICMP "socket" index
574 * @timestamp: timestamp in jiffies which is replied in ack
576 * Create and send a single TP Meter message.
578 * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
579 * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
582 static int batadv_tp_send_msg(struct batadv_tp_vars
*tp_vars
, const u8
*src
,
583 struct batadv_orig_node
*orig_node
,
584 u32 seqno
, size_t len
, const u8
*session
,
585 int uid
, u32 timestamp
)
587 struct batadv_icmp_tp_packet
*icmp
;
593 skb
= netdev_alloc_skb_ip_align(NULL
, len
+ ETH_HLEN
);
595 return BATADV_TP_REASON_MEMORY_ERROR
;
597 skb_reserve(skb
, ETH_HLEN
);
598 icmp
= skb_put(skb
, sizeof(*icmp
));
600 /* fill the icmp header */
601 ether_addr_copy(icmp
->dst
, orig_node
->orig
);
602 ether_addr_copy(icmp
->orig
, src
);
603 icmp
->version
= BATADV_COMPAT_VERSION
;
604 icmp
->packet_type
= BATADV_ICMP
;
605 icmp
->ttl
= BATADV_TTL
;
606 icmp
->msg_type
= BATADV_TP
;
609 icmp
->subtype
= BATADV_TP_MSG
;
610 memcpy(icmp
->session
, session
, sizeof(icmp
->session
));
611 icmp
->seqno
= htonl(seqno
);
612 icmp
->timestamp
= htonl(timestamp
);
614 data_len
= len
- sizeof(*icmp
);
615 data
= skb_put(skb
, data_len
);
616 batadv_tp_fill_prerandom(tp_vars
, data
, data_len
);
618 r
= batadv_send_skb_to_orig(skb
, orig_node
, NULL
);
619 if (r
== NET_XMIT_SUCCESS
)
622 return BATADV_TP_REASON_CANT_SEND
;
626 * batadv_tp_recv_ack - ACK receiving function
627 * @bat_priv: the bat priv with all the soft interface information
628 * @skb: the buffer containing the received packet
630 * Process a received TP ACK packet
632 static void batadv_tp_recv_ack(struct batadv_priv
*bat_priv
,
633 const struct sk_buff
*skb
)
635 struct batadv_hard_iface
*primary_if
= NULL
;
636 struct batadv_orig_node
*orig_node
= NULL
;
637 const struct batadv_icmp_tp_packet
*icmp
;
638 struct batadv_tp_vars
*tp_vars
;
639 size_t packet_len
, mss
;
640 u32 rtt
, recv_ack
, cwnd
;
641 unsigned char *dev_addr
;
643 packet_len
= BATADV_TP_PLEN
;
644 mss
= BATADV_TP_PLEN
;
645 packet_len
+= sizeof(struct batadv_unicast_packet
);
647 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
649 /* find the tp_vars */
650 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
652 if (unlikely(!tp_vars
))
655 if (unlikely(atomic_read(&tp_vars
->sending
) == 0))
658 /* old ACK? silently drop it.. */
659 if (batadv_seq_before(ntohl(icmp
->seqno
),
660 (u32
)atomic_read(&tp_vars
->last_acked
)))
663 primary_if
= batadv_primary_if_get_selected(bat_priv
);
664 if (unlikely(!primary_if
))
667 orig_node
= batadv_orig_hash_find(bat_priv
, icmp
->orig
);
668 if (unlikely(!orig_node
))
671 /* update RTO with the new sampled RTT, if any */
672 rtt
= jiffies_to_msecs(jiffies
) - ntohl(icmp
->timestamp
);
673 if (icmp
->timestamp
&& rtt
)
674 batadv_tp_update_rto(tp_vars
, rtt
);
676 /* ACK for new data... reset the timer */
677 batadv_tp_reset_sender_timer(tp_vars
);
679 recv_ack
= ntohl(icmp
->seqno
);
681 /* check if this ACK is a duplicate */
682 if (atomic_read(&tp_vars
->last_acked
) == recv_ack
) {
683 atomic_inc(&tp_vars
->dup_acks
);
684 if (atomic_read(&tp_vars
->dup_acks
) != 3)
687 if (recv_ack
>= tp_vars
->recover
)
690 /* if this is the third duplicate ACK do Fast Retransmit */
691 batadv_tp_send_msg(tp_vars
, primary_if
->net_dev
->dev_addr
,
692 orig_node
, recv_ack
, packet_len
,
693 icmp
->session
, icmp
->uid
,
694 jiffies_to_msecs(jiffies
));
696 spin_lock_bh(&tp_vars
->cwnd_lock
);
699 tp_vars
->fast_recovery
= true;
700 /* Set recover to the last outstanding seqno when Fast Recovery
701 * is entered. RFC6582, Section 3.2, step 1
703 tp_vars
->recover
= tp_vars
->last_sent
;
704 tp_vars
->ss_threshold
= tp_vars
->cwnd
>> 1;
705 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
706 "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
707 tp_vars
->cwnd
, tp_vars
->ss_threshold
,
708 tp_vars
->last_sent
, recv_ack
);
709 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->ss_threshold
, 3 * mss
,
711 tp_vars
->dec_cwnd
= 0;
712 tp_vars
->last_sent
= recv_ack
;
714 spin_unlock_bh(&tp_vars
->cwnd_lock
);
716 /* count the acked data */
717 atomic64_add(recv_ack
- atomic_read(&tp_vars
->last_acked
),
719 /* reset the duplicate ACKs counter */
720 atomic_set(&tp_vars
->dup_acks
, 0);
722 if (tp_vars
->fast_recovery
) {
724 if (batadv_seq_before(recv_ack
, tp_vars
->recover
)) {
725 /* this is another hole in the window. React
726 * immediately as specified by NewReno (see
727 * Section 3.2 of RFC6582 for details)
729 dev_addr
= primary_if
->net_dev
->dev_addr
;
730 batadv_tp_send_msg(tp_vars
, dev_addr
,
732 packet_len
, icmp
->session
,
734 jiffies_to_msecs(jiffies
));
735 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
,
738 tp_vars
->fast_recovery
= false;
739 /* set cwnd to the value of ss_threshold at the
740 * moment that Fast Recovery was entered.
741 * RFC6582, Section 3.2, step 3
743 cwnd
= batadv_tp_cwnd(tp_vars
->ss_threshold
, 0,
745 tp_vars
->cwnd
= cwnd
;
750 if (recv_ack
- atomic_read(&tp_vars
->last_acked
) >= mss
)
751 batadv_tp_update_cwnd(tp_vars
, mss
);
753 /* move the Transmit Window */
754 atomic_set(&tp_vars
->last_acked
, recv_ack
);
757 wake_up(&tp_vars
->more_bytes
);
759 if (likely(primary_if
))
760 batadv_hardif_put(primary_if
);
761 if (likely(orig_node
))
762 batadv_orig_node_put(orig_node
);
764 batadv_tp_vars_put(tp_vars
);
768 * batadv_tp_avail - check if congestion window is not full
769 * @tp_vars: the private data of the current TP meter session
770 * @payload_len: size of the payload of a single message
772 * Return: true when congestion window is not full, false otherwise
774 static bool batadv_tp_avail(struct batadv_tp_vars
*tp_vars
,
777 u32 win_left
, win_limit
;
779 win_limit
= atomic_read(&tp_vars
->last_acked
) + tp_vars
->cwnd
;
780 win_left
= win_limit
- tp_vars
->last_sent
;
782 return win_left
>= payload_len
;
786 * batadv_tp_wait_available - wait until congestion window becomes free or
788 * @tp_vars: the private data of the current TP meter session
789 * @plen: size of the payload of a single message
791 * Return: 0 if the condition evaluated to false after the timeout elapsed,
792 * 1 if the condition evaluated to true after the timeout elapsed, the
793 * remaining jiffies (at least 1) if the condition evaluated to true before
794 * the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
796 static int batadv_tp_wait_available(struct batadv_tp_vars
*tp_vars
, size_t plen
)
800 ret
= wait_event_interruptible_timeout(tp_vars
->more_bytes
,
801 batadv_tp_avail(tp_vars
, plen
),
808 * batadv_tp_send - main sending thread of a tp meter session
809 * @arg: address of the related tp_vars
811 * Return: nothing, this function never returns
813 static int batadv_tp_send(void *arg
)
815 struct batadv_tp_vars
*tp_vars
= arg
;
816 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
817 struct batadv_hard_iface
*primary_if
= NULL
;
818 struct batadv_orig_node
*orig_node
= NULL
;
819 size_t payload_len
, packet_len
;
822 if (unlikely(tp_vars
->role
!= BATADV_TP_SENDER
)) {
823 err
= BATADV_TP_REASON_DST_UNREACHABLE
;
824 tp_vars
->reason
= err
;
828 orig_node
= batadv_orig_hash_find(bat_priv
, tp_vars
->other_end
);
829 if (unlikely(!orig_node
)) {
830 err
= BATADV_TP_REASON_DST_UNREACHABLE
;
831 tp_vars
->reason
= err
;
835 primary_if
= batadv_primary_if_get_selected(bat_priv
);
836 if (unlikely(!primary_if
)) {
837 err
= BATADV_TP_REASON_DST_UNREACHABLE
;
838 tp_vars
->reason
= err
;
842 /* assume that all the hard_interfaces have a correctly
843 * configured MTU, so use the soft_iface MTU as MSS.
844 * This might not be true and in that case the fragmentation
846 * Now, try to send the packet as it is
848 payload_len
= BATADV_TP_PLEN
;
849 BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet
) > BATADV_TP_PLEN
);
851 batadv_tp_reset_sender_timer(tp_vars
);
853 /* queue the worker in charge of terminating the test */
854 queue_delayed_work(batadv_event_workqueue
, &tp_vars
->finish_work
,
855 msecs_to_jiffies(tp_vars
->test_length
));
857 while (atomic_read(&tp_vars
->sending
) != 0) {
858 if (unlikely(!batadv_tp_avail(tp_vars
, payload_len
))) {
859 batadv_tp_wait_available(tp_vars
, payload_len
);
863 /* to emulate normal unicast traffic, add to the payload len
864 * the size of the unicast header
866 packet_len
= payload_len
+ sizeof(struct batadv_unicast_packet
);
868 err
= batadv_tp_send_msg(tp_vars
, primary_if
->net_dev
->dev_addr
,
869 orig_node
, tp_vars
->last_sent
,
871 tp_vars
->session
, tp_vars
->icmp_uid
,
872 jiffies_to_msecs(jiffies
));
874 /* something went wrong during the preparation/transmission */
875 if (unlikely(err
&& err
!= BATADV_TP_REASON_CANT_SEND
)) {
876 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
877 "Meter: %s() cannot send packets (%d)\n",
879 /* ensure nobody else tries to stop the thread now */
880 if (atomic_dec_and_test(&tp_vars
->sending
))
881 tp_vars
->reason
= err
;
885 /* right-shift the TWND */
887 tp_vars
->last_sent
+= payload_len
;
893 if (likely(primary_if
))
894 batadv_hardif_put(primary_if
);
895 if (likely(orig_node
))
896 batadv_orig_node_put(orig_node
);
898 batadv_tp_sender_end(bat_priv
, tp_vars
);
899 batadv_tp_sender_cleanup(bat_priv
, tp_vars
);
901 batadv_tp_vars_put(tp_vars
);
907 * batadv_tp_start_kthread - start new thread which manages the tp meter sender
908 * @tp_vars: the private data of the current TP meter session
910 static void batadv_tp_start_kthread(struct batadv_tp_vars
*tp_vars
)
912 struct task_struct
*kthread
;
913 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
916 kref_get(&tp_vars
->refcount
);
917 kthread
= kthread_create(batadv_tp_send
, tp_vars
, "kbatadv_tp_meter");
918 if (IS_ERR(kthread
)) {
919 session_cookie
= batadv_tp_session_cookie(tp_vars
->session
,
921 pr_err("batadv: cannot create tp meter kthread\n");
922 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR
,
924 bat_priv
, session_cookie
);
926 /* drop reserved reference for kthread */
927 batadv_tp_vars_put(tp_vars
);
929 /* cleanup of failed tp meter variables */
930 batadv_tp_sender_cleanup(bat_priv
, tp_vars
);
934 wake_up_process(kthread
);
938 * batadv_tp_start - start a new tp meter session
939 * @bat_priv: the bat priv with all the soft interface information
940 * @dst: the receiver MAC address
941 * @test_length: test length in milliseconds
942 * @cookie: session cookie
944 void batadv_tp_start(struct batadv_priv
*bat_priv
, const u8
*dst
,
945 u32 test_length
, u32
*cookie
)
947 struct batadv_tp_vars
*tp_vars
;
952 get_random_bytes(session_id
, sizeof(session_id
));
953 get_random_bytes(&icmp_uid
, 1);
954 session_cookie
= batadv_tp_session_cookie(session_id
, icmp_uid
);
955 *cookie
= session_cookie
;
957 /* look for an already existing test towards this node */
958 spin_lock_bh(&bat_priv
->tp_list_lock
);
959 tp_vars
= batadv_tp_list_find(bat_priv
, dst
);
961 spin_unlock_bh(&bat_priv
->tp_list_lock
);
962 batadv_tp_vars_put(tp_vars
);
963 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
964 "Meter: test to or from the same node already ongoing, aborting\n");
965 batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING
,
966 dst
, bat_priv
, session_cookie
);
970 if (!atomic_add_unless(&bat_priv
->tp_num
, 1, BATADV_TP_MAX_NUM
)) {
971 spin_unlock_bh(&bat_priv
->tp_list_lock
);
972 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
973 "Meter: too many ongoing sessions, aborting (SEND)\n");
974 batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY
, dst
,
975 bat_priv
, session_cookie
);
979 tp_vars
= kmalloc(sizeof(*tp_vars
), GFP_ATOMIC
);
981 spin_unlock_bh(&bat_priv
->tp_list_lock
);
982 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
983 "Meter: %s cannot allocate list elements\n",
985 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR
,
986 dst
, bat_priv
, session_cookie
);
990 /* initialize tp_vars */
991 ether_addr_copy(tp_vars
->other_end
, dst
);
992 kref_init(&tp_vars
->refcount
);
993 tp_vars
->role
= BATADV_TP_SENDER
;
994 atomic_set(&tp_vars
->sending
, 1);
995 memcpy(tp_vars
->session
, session_id
, sizeof(session_id
));
996 tp_vars
->icmp_uid
= icmp_uid
;
998 tp_vars
->last_sent
= BATADV_TP_FIRST_SEQ
;
999 atomic_set(&tp_vars
->last_acked
, BATADV_TP_FIRST_SEQ
);
1000 tp_vars
->fast_recovery
= false;
1001 tp_vars
->recover
= BATADV_TP_FIRST_SEQ
;
1003 /* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
1004 * For batman-adv the MSS is the size of the payload received by the
1005 * soft_interface, hence its MTU
1007 tp_vars
->cwnd
= BATADV_TP_PLEN
* 3;
1008 /* at the beginning initialise the SS threshold to the biggest possible
1009 * window size, hence the AWND size
1011 tp_vars
->ss_threshold
= BATADV_TP_AWND
;
1013 /* RTO initial value is 3 seconds.
1014 * Details in Section 2.1 of RFC6298
1016 tp_vars
->rto
= 1000;
1018 tp_vars
->rttvar
= 0;
1020 atomic64_set(&tp_vars
->tot_sent
, 0);
1022 kref_get(&tp_vars
->refcount
);
1023 timer_setup(&tp_vars
->timer
, batadv_tp_sender_timeout
, 0);
1025 tp_vars
->bat_priv
= bat_priv
;
1026 tp_vars
->start_time
= jiffies
;
1028 init_waitqueue_head(&tp_vars
->more_bytes
);
1030 spin_lock_init(&tp_vars
->unacked_lock
);
1031 INIT_LIST_HEAD(&tp_vars
->unacked_list
);
1033 spin_lock_init(&tp_vars
->cwnd_lock
);
1035 tp_vars
->prerandom_offset
= 0;
1036 spin_lock_init(&tp_vars
->prerandom_lock
);
1038 kref_get(&tp_vars
->refcount
);
1039 hlist_add_head_rcu(&tp_vars
->list
, &bat_priv
->tp_list
);
1040 spin_unlock_bh(&bat_priv
->tp_list_lock
);
1042 tp_vars
->test_length
= test_length
;
1043 if (!tp_vars
->test_length
)
1044 tp_vars
->test_length
= BATADV_TP_DEF_TEST_LENGTH
;
1046 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1047 "Meter: starting throughput meter towards %pM (length=%ums)\n",
1050 /* init work item for finished tp tests */
1051 INIT_DELAYED_WORK(&tp_vars
->finish_work
, batadv_tp_sender_finish
);
1053 /* start tp kthread. This way the write() call issued from userspace can
1054 * happily return and avoid to block
1056 batadv_tp_start_kthread(tp_vars
);
1058 /* don't return reference to new tp_vars */
1059 batadv_tp_vars_put(tp_vars
);
1063 * batadv_tp_stop - stop currently running tp meter session
1064 * @bat_priv: the bat priv with all the soft interface information
1065 * @dst: the receiver MAC address
1066 * @return_value: reason for tp meter session stop
1068 void batadv_tp_stop(struct batadv_priv
*bat_priv
, const u8
*dst
,
1071 struct batadv_orig_node
*orig_node
;
1072 struct batadv_tp_vars
*tp_vars
;
1074 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1075 "Meter: stopping test towards %pM\n", dst
);
1077 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
1081 tp_vars
= batadv_tp_list_find(bat_priv
, orig_node
->orig
);
1083 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1084 "Meter: trying to interrupt an already over connection\n");
1088 batadv_tp_sender_shutdown(tp_vars
, return_value
);
1089 batadv_tp_vars_put(tp_vars
);
1091 batadv_orig_node_put(orig_node
);
1095 * batadv_tp_reset_receiver_timer - reset the receiver shutdown timer
1096 * @tp_vars: the private data of the current TP meter session
1098 * start the receiver shutdown timer or reset it if already started
1100 static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars
*tp_vars
)
1102 mod_timer(&tp_vars
->timer
,
1103 jiffies
+ msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT
));
1107 * batadv_tp_receiver_shutdown - stop a tp meter receiver when timeout is
1108 * reached without received ack
1109 * @t: address to timer_list inside tp_vars
1111 static void batadv_tp_receiver_shutdown(struct timer_list
*t
)
1113 struct batadv_tp_vars
*tp_vars
= from_timer(tp_vars
, t
, timer
);
1114 struct batadv_tp_unacked
*un
, *safe
;
1115 struct batadv_priv
*bat_priv
;
1117 bat_priv
= tp_vars
->bat_priv
;
1119 /* if there is recent activity rearm the timer */
1120 if (!batadv_has_timed_out(tp_vars
->last_recv_time
,
1121 BATADV_TP_RECV_TIMEOUT
)) {
1122 /* reset the receiver shutdown timer */
1123 batadv_tp_reset_receiver_timer(tp_vars
);
1127 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1128 "Shutting down for inactivity (more than %dms) from %pM\n",
1129 BATADV_TP_RECV_TIMEOUT
, tp_vars
->other_end
);
1131 spin_lock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
1132 hlist_del_rcu(&tp_vars
->list
);
1133 spin_unlock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
1135 /* drop list reference */
1136 batadv_tp_vars_put(tp_vars
);
1138 atomic_dec(&bat_priv
->tp_num
);
1140 spin_lock_bh(&tp_vars
->unacked_lock
);
1141 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
1142 list_del(&un
->list
);
1145 spin_unlock_bh(&tp_vars
->unacked_lock
);
1147 /* drop reference of timer */
1148 batadv_tp_vars_put(tp_vars
);
1152 * batadv_tp_send_ack - send an ACK packet
1153 * @bat_priv: the bat priv with all the soft interface information
1154 * @dst: the mac address of the destination originator
1155 * @seq: the sequence number to ACK
1156 * @timestamp: the timestamp to echo back in the ACK
1157 * @session: session identifier
1158 * @socket_index: local ICMP socket identifier
1160 * Return: 0 on success, a positive integer representing the reason of the
1163 static int batadv_tp_send_ack(struct batadv_priv
*bat_priv
, const u8
*dst
,
1164 u32 seq
, __be32 timestamp
, const u8
*session
,
1167 struct batadv_hard_iface
*primary_if
= NULL
;
1168 struct batadv_orig_node
*orig_node
;
1169 struct batadv_icmp_tp_packet
*icmp
;
1170 struct sk_buff
*skb
;
1173 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
1174 if (unlikely(!orig_node
)) {
1175 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1179 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1180 if (unlikely(!primary_if
)) {
1181 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1185 skb
= netdev_alloc_skb_ip_align(NULL
, sizeof(*icmp
) + ETH_HLEN
);
1186 if (unlikely(!skb
)) {
1187 ret
= BATADV_TP_REASON_MEMORY_ERROR
;
1191 skb_reserve(skb
, ETH_HLEN
);
1192 icmp
= skb_put(skb
, sizeof(*icmp
));
1193 icmp
->packet_type
= BATADV_ICMP
;
1194 icmp
->version
= BATADV_COMPAT_VERSION
;
1195 icmp
->ttl
= BATADV_TTL
;
1196 icmp
->msg_type
= BATADV_TP
;
1197 ether_addr_copy(icmp
->dst
, orig_node
->orig
);
1198 ether_addr_copy(icmp
->orig
, primary_if
->net_dev
->dev_addr
);
1199 icmp
->uid
= socket_index
;
1201 icmp
->subtype
= BATADV_TP_ACK
;
1202 memcpy(icmp
->session
, session
, sizeof(icmp
->session
));
1203 icmp
->seqno
= htonl(seq
);
1204 icmp
->timestamp
= timestamp
;
1207 r
= batadv_send_skb_to_orig(skb
, orig_node
, NULL
);
1208 if (unlikely(r
< 0) || r
== NET_XMIT_DROP
) {
1209 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1215 if (likely(orig_node
))
1216 batadv_orig_node_put(orig_node
);
1217 if (likely(primary_if
))
1218 batadv_hardif_put(primary_if
);
1224 * batadv_tp_handle_out_of_order - store an out of order packet
1225 * @tp_vars: the private data of the current TP meter session
1226 * @skb: the buffer containing the received packet
1228 * Store the out of order packet in the unacked list for late processing. This
1229 * packets are kept in this list so that they can be ACKed at once as soon as
1230 * all the previous packets have been received
1232 * Return: true if the packed has been successfully processed, false otherwise
1234 static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars
*tp_vars
,
1235 const struct sk_buff
*skb
)
1237 const struct batadv_icmp_tp_packet
*icmp
;
1238 struct batadv_tp_unacked
*un
, *new;
1242 new = kmalloc(sizeof(*new), GFP_ATOMIC
);
1246 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1248 new->seqno
= ntohl(icmp
->seqno
);
1249 payload_len
= skb
->len
- sizeof(struct batadv_unicast_packet
);
1250 new->len
= payload_len
;
1252 spin_lock_bh(&tp_vars
->unacked_lock
);
1253 /* if the list is empty immediately attach this new object */
1254 if (list_empty(&tp_vars
->unacked_list
)) {
1255 list_add(&new->list
, &tp_vars
->unacked_list
);
1259 /* otherwise loop over the list and either drop the packet because this
1260 * is a duplicate or store it at the right position.
1262 * The iteration is done in the reverse way because it is likely that
1263 * the last received packet (the one being processed now) has a bigger
1264 * seqno than all the others already stored.
1266 list_for_each_entry_reverse(un
, &tp_vars
->unacked_list
, list
) {
1267 /* check for duplicates */
1268 if (new->seqno
== un
->seqno
) {
1269 if (new->len
> un
->len
)
1276 /* look for the right position */
1277 if (batadv_seq_before(new->seqno
, un
->seqno
))
1280 /* as soon as an entry having a bigger seqno is found, the new
1281 * one is attached _after_ it. In this way the list is kept in
1284 list_add_tail(&new->list
, &un
->list
);
1289 /* received packet with smallest seqno out of order; add it to front */
1291 list_add(&new->list
, &tp_vars
->unacked_list
);
1294 spin_unlock_bh(&tp_vars
->unacked_lock
);
1300 * batadv_tp_ack_unordered - update number received bytes in current stream
1302 * @tp_vars: the private data of the current TP meter session
1304 static void batadv_tp_ack_unordered(struct batadv_tp_vars
*tp_vars
)
1306 struct batadv_tp_unacked
*un
, *safe
;
1309 /* go through the unacked packet list and possibly ACK them as
1312 spin_lock_bh(&tp_vars
->unacked_lock
);
1313 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
1314 /* the list is ordered, therefore it is possible to stop as soon
1315 * there is a gap between the last acked seqno and the seqno of
1316 * the packet under inspection
1318 if (batadv_seq_before(tp_vars
->last_recv
, un
->seqno
))
1321 to_ack
= un
->seqno
+ un
->len
- tp_vars
->last_recv
;
1323 if (batadv_seq_before(tp_vars
->last_recv
, un
->seqno
+ un
->len
))
1324 tp_vars
->last_recv
+= to_ack
;
1326 list_del(&un
->list
);
1329 spin_unlock_bh(&tp_vars
->unacked_lock
);
1333 * batadv_tp_init_recv - return matching or create new receiver tp_vars
1334 * @bat_priv: the bat priv with all the soft interface information
1335 * @icmp: received icmp tp msg
1337 * Return: corresponding tp_vars or NULL on errors
1339 static struct batadv_tp_vars
*
1340 batadv_tp_init_recv(struct batadv_priv
*bat_priv
,
1341 const struct batadv_icmp_tp_packet
*icmp
)
1343 struct batadv_tp_vars
*tp_vars
;
1345 spin_lock_bh(&bat_priv
->tp_list_lock
);
1346 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
1351 if (!atomic_add_unless(&bat_priv
->tp_num
, 1, BATADV_TP_MAX_NUM
)) {
1352 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1353 "Meter: too many ongoing sessions, aborting (RECV)\n");
1357 tp_vars
= kmalloc(sizeof(*tp_vars
), GFP_ATOMIC
);
1361 ether_addr_copy(tp_vars
->other_end
, icmp
->orig
);
1362 tp_vars
->role
= BATADV_TP_RECEIVER
;
1363 memcpy(tp_vars
->session
, icmp
->session
, sizeof(tp_vars
->session
));
1364 tp_vars
->last_recv
= BATADV_TP_FIRST_SEQ
;
1365 tp_vars
->bat_priv
= bat_priv
;
1366 kref_init(&tp_vars
->refcount
);
1368 spin_lock_init(&tp_vars
->unacked_lock
);
1369 INIT_LIST_HEAD(&tp_vars
->unacked_list
);
1371 kref_get(&tp_vars
->refcount
);
1372 hlist_add_head_rcu(&tp_vars
->list
, &bat_priv
->tp_list
);
1374 kref_get(&tp_vars
->refcount
);
1375 timer_setup(&tp_vars
->timer
, batadv_tp_receiver_shutdown
, 0);
1377 batadv_tp_reset_receiver_timer(tp_vars
);
1380 spin_unlock_bh(&bat_priv
->tp_list_lock
);
1386 * batadv_tp_recv_msg - process a single data message
1387 * @bat_priv: the bat priv with all the soft interface information
1388 * @skb: the buffer containing the received packet
1390 * Process a received TP MSG packet
1392 static void batadv_tp_recv_msg(struct batadv_priv
*bat_priv
,
1393 const struct sk_buff
*skb
)
1395 const struct batadv_icmp_tp_packet
*icmp
;
1396 struct batadv_tp_vars
*tp_vars
;
1400 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1402 seqno
= ntohl(icmp
->seqno
);
1403 /* check if this is the first seqno. This means that if the
1404 * first packet is lost, the tp meter does not work anymore!
1406 if (seqno
== BATADV_TP_FIRST_SEQ
) {
1407 tp_vars
= batadv_tp_init_recv(bat_priv
, icmp
);
1409 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1410 "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
1414 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
1417 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1418 "Unexpected packet from %pM!\n",
1424 if (unlikely(tp_vars
->role
!= BATADV_TP_RECEIVER
)) {
1425 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1426 "Meter: dropping packet: not expected (role=%u)\n",
1431 tp_vars
->last_recv_time
= jiffies
;
1433 /* if the packet is a duplicate, it may be the case that an ACK has been
1434 * lost. Resend the ACK
1436 if (batadv_seq_before(seqno
, tp_vars
->last_recv
))
1439 /* if the packet is out of order enqueue it */
1440 if (ntohl(icmp
->seqno
) != tp_vars
->last_recv
) {
1441 /* exit immediately (and do not send any ACK) if the packet has
1442 * not been enqueued correctly
1444 if (!batadv_tp_handle_out_of_order(tp_vars
, skb
))
1447 /* send a duplicate ACK */
1451 /* if everything was fine count the ACKed bytes */
1452 packet_size
= skb
->len
- sizeof(struct batadv_unicast_packet
);
1453 tp_vars
->last_recv
+= packet_size
;
1455 /* check if this ordered message filled a gap.... */
1456 batadv_tp_ack_unordered(tp_vars
);
1459 /* send the ACK. If the received packet was out of order, the ACK that
1460 * is going to be sent is a duplicate (the sender will count them and
1461 * possibly enter Fast Retransmit as soon as it has reached 3)
1463 batadv_tp_send_ack(bat_priv
, icmp
->orig
, tp_vars
->last_recv
,
1464 icmp
->timestamp
, icmp
->session
, icmp
->uid
);
1466 if (likely(tp_vars
))
1467 batadv_tp_vars_put(tp_vars
);
1471 * batadv_tp_meter_recv - main TP Meter receiving function
1472 * @bat_priv: the bat priv with all the soft interface information
1473 * @skb: the buffer containing the received packet
1475 void batadv_tp_meter_recv(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
)
1477 struct batadv_icmp_tp_packet
*icmp
;
1479 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1481 switch (icmp
->subtype
) {
1483 batadv_tp_recv_msg(bat_priv
, skb
);
1486 batadv_tp_recv_ack(bat_priv
, skb
);
1489 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1490 "Received unknown TP Metric packet type %u\n",
1497 * batadv_tp_meter_init - initialize global tp_meter structures
1499 void __init
batadv_tp_meter_init(void)
1501 get_random_bytes(batadv_tp_prerandom
, sizeof(batadv_tp_prerandom
));