1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2019 B.A.T.M.A.N. contributors:
4 * Edo Monticelli, Antonio Quartulli
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>
44 #include "hard-interface.h"
47 #include "originator.h"
51 * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
54 #define BATADV_TP_DEF_TEST_LENGTH 10000
57 * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
59 #define BATADV_TP_AWND 0x20000000
62 * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
63 * get anything for such amount of milliseconds, the connection is killed
65 #define BATADV_TP_RECV_TIMEOUT 1000
68 * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
69 * such amound of milliseconds, the receiver is considered unreachable and the
70 * connection is killed
72 #define BATADV_TP_MAX_RTO 30000
75 * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
76 * in order to immediately trigger a wrap around (test purposes)
78 #define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
81 * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
84 #define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
85 sizeof(struct batadv_unicast_packet))
87 static u8 batadv_tp_prerandom
[4096] __read_mostly
;
90 * batadv_tp_session_cookie() - generate session cookie based on session ids
91 * @session: TP session identifier
92 * @icmp_uid: icmp pseudo uid of the tp session
94 * Return: 32 bit tp_meter session cookie
96 static u32
batadv_tp_session_cookie(const u8 session
[2], u8 icmp_uid
)
100 cookie
= icmp_uid
<< 16;
101 cookie
|= session
[0] << 8;
102 cookie
|= session
[1];
108 * batadv_tp_cwnd() - compute the new cwnd size
109 * @base: base cwnd size value
110 * @increment: the value to add to base to get the new size
111 * @min: minumim cwnd value (usually MSS)
113 * Return the new cwnd size and ensures it does not exceed the Advertised
114 * Receiver Window size. It is wrap around safe.
115 * For details refer to Section 3.1 of RFC5681
117 * Return: new congestion window size in bytes
119 static u32
batadv_tp_cwnd(u32 base
, u32 increment
, u32 min
)
121 u32 new_size
= base
+ increment
;
123 /* check for wrap-around */
125 new_size
= (u32
)ULONG_MAX
;
127 new_size
= min_t(u32
, new_size
, BATADV_TP_AWND
);
129 return max_t(u32
, new_size
, min
);
133 * batadv_tp_updated_cwnd() - update the Congestion Windows
134 * @tp_vars: the private data of the current TP meter session
135 * @mss: maximum segment size of transmission
137 * 1) if the session is in Slow Start, the CWND has to be increased by 1
138 * MSS every unique received ACK
139 * 2) if the session is in Congestion Avoidance, the CWND has to be
140 * increased by MSS * MSS / CWND for every unique received ACK
142 static void batadv_tp_update_cwnd(struct batadv_tp_vars
*tp_vars
, u32 mss
)
144 spin_lock_bh(&tp_vars
->cwnd_lock
);
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
);
154 /* increment CWND at least of 1 (section 3.1 of RFC5681) */
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
);
162 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
, mss
, mss
);
163 tp_vars
->dec_cwnd
= 0;
165 spin_unlock_bh(&tp_vars
->cwnd_lock
);
169 * batadv_tp_update_rto() - calculate new retransmission timeout
170 * @tp_vars: the private data of the current TP meter session
171 * @new_rtt: new roundtrip time in msec
173 static void batadv_tp_update_rto(struct batadv_tp_vars
*tp_vars
,
179 * Details in Section 2.2 and 2.3 of RFC6298
181 * It's tricky to understand. Don't lose hair please.
182 * Inspired by tcp_rtt_estimator() tcp_input.c
184 if (tp_vars
->srtt
!= 0) {
185 m
-= (tp_vars
->srtt
>> 3); /* m is now error in rtt est */
186 tp_vars
->srtt
+= m
; /* rtt = 7/8 srtt + 1/8 new */
190 m
-= (tp_vars
->rttvar
>> 2);
191 tp_vars
->rttvar
+= m
; /* mdev ~= 3/4 rttvar + 1/4 new */
193 /* first measure getting in */
194 tp_vars
->srtt
= m
<< 3; /* take the measured time to be srtt */
195 tp_vars
->rttvar
= m
<< 1; /* new_rtt / 2 */
198 /* rto = srtt + 4 * rttvar.
199 * rttvar is scaled by 4, therefore doesn't need to be multiplied
201 tp_vars
->rto
= (tp_vars
->srtt
>> 3) + tp_vars
->rttvar
;
205 * batadv_tp_batctl_notify() - send client status result to client
206 * @reason: reason for tp meter session stop
207 * @dst: destination of tp_meter session
208 * @bat_priv: the bat priv with all the soft interface information
209 * @start_time: start of transmission in jiffies
210 * @total_sent: bytes acked to the receiver
211 * @cookie: cookie of tp_meter session
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
,
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
;
232 batadv_netlink_tpmeter_notify(bat_priv
, dst
, result
, test_time
,
233 total_bytes
, cookie
);
237 * batadv_tp_batctl_error_notify() - send client error result to client
238 * @reason: reason for tp meter session stop
239 * @dst: destination of tp_meter session
240 * @bat_priv: the bat priv with all the soft interface information
241 * @cookie: cookie of tp_meter session
243 static void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason
,
245 struct batadv_priv
*bat_priv
,
248 batadv_tp_batctl_notify(reason
, dst
, bat_priv
, 0, 0, cookie
);
252 * batadv_tp_list_find() - find a tp_vars object in the global list
253 * @bat_priv: the bat priv with all the soft interface information
254 * @dst: the other endpoint MAC address to look for
256 * Look for a tp_vars object matching dst as end_point and return it after
257 * having incremented the refcounter. Return NULL is not found
259 * Return: matching tp_vars or NULL when no tp_vars with @dst was found
261 static struct batadv_tp_vars
*batadv_tp_list_find(struct batadv_priv
*bat_priv
,
264 struct batadv_tp_vars
*pos
, *tp_vars
= NULL
;
267 hlist_for_each_entry_rcu(pos
, &bat_priv
->tp_list
, list
) {
268 if (!batadv_compare_eth(pos
->other_end
, dst
))
271 /* most of the time this function is invoked during the normal
272 * process..it makes sens to pay more when the session is
273 * finished and to speed the process up during the measurement
275 if (unlikely(!kref_get_unless_zero(&pos
->refcount
)))
287 * batadv_tp_list_find_session() - find tp_vars session object in the global
289 * @bat_priv: the bat priv with all the soft interface information
290 * @dst: the other endpoint MAC address to look for
291 * @session: session identifier
293 * Look for a tp_vars object matching dst as end_point, session as tp meter
294 * session and return it after having incremented the refcounter. Return NULL
297 * Return: matching tp_vars or NULL when no tp_vars was found
299 static struct batadv_tp_vars
*
300 batadv_tp_list_find_session(struct batadv_priv
*bat_priv
, const u8
*dst
,
303 struct batadv_tp_vars
*pos
, *tp_vars
= NULL
;
306 hlist_for_each_entry_rcu(pos
, &bat_priv
->tp_list
, list
) {
307 if (!batadv_compare_eth(pos
->other_end
, dst
))
310 if (memcmp(pos
->session
, session
, sizeof(pos
->session
)) != 0)
313 /* most of the time this function is invoked during the normal
314 * process..it makes sense to pay more when the session is
315 * finished and to speed the process up during the measurement
317 if (unlikely(!kref_get_unless_zero(&pos
->refcount
)))
329 * batadv_tp_vars_release() - release batadv_tp_vars from lists and queue for
330 * free after rcu grace period
331 * @ref: kref pointer of the batadv_tp_vars
333 static void batadv_tp_vars_release(struct kref
*ref
)
335 struct batadv_tp_vars
*tp_vars
;
336 struct batadv_tp_unacked
*un
, *safe
;
338 tp_vars
= container_of(ref
, struct batadv_tp_vars
, refcount
);
340 /* lock should not be needed because this object is now out of any
343 spin_lock_bh(&tp_vars
->unacked_lock
);
344 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
348 spin_unlock_bh(&tp_vars
->unacked_lock
);
350 kfree_rcu(tp_vars
, rcu
);
354 * batadv_tp_vars_put() - decrement the batadv_tp_vars refcounter and possibly
356 * @tp_vars: the private data of the current TP meter session to be free'd
358 static void batadv_tp_vars_put(struct batadv_tp_vars
*tp_vars
)
360 kref_put(&tp_vars
->refcount
, batadv_tp_vars_release
);
364 * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer
365 * @bat_priv: the bat priv with all the soft interface information
366 * @tp_vars: the private data of the current TP meter session to cleanup
368 static void batadv_tp_sender_cleanup(struct batadv_priv
*bat_priv
,
369 struct batadv_tp_vars
*tp_vars
)
371 cancel_delayed_work(&tp_vars
->finish_work
);
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
);
377 /* drop list reference */
378 batadv_tp_vars_put(tp_vars
);
380 atomic_dec(&tp_vars
->bat_priv
->tp_num
);
382 /* kill the timer and remove its reference */
383 del_timer_sync(&tp_vars
->timer
);
384 /* the worker might have rearmed itself therefore we kill it again. Note
385 * that if the worker should run again before invoking the following
386 * del_timer(), it would not re-arm itself once again because the status
389 del_timer(&tp_vars
->timer
);
390 batadv_tp_vars_put(tp_vars
);
394 * batadv_tp_sender_end() - print info about ended session and inform client
395 * @bat_priv: the bat priv with all the soft interface information
396 * @tp_vars: the private data of the current TP meter session
398 static void batadv_tp_sender_end(struct batadv_priv
*bat_priv
,
399 struct batadv_tp_vars
*tp_vars
)
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
);
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
);
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
);
415 session_cookie
= batadv_tp_session_cookie(tp_vars
->session
,
418 batadv_tp_batctl_notify(tp_vars
->reason
,
422 atomic64_read(&tp_vars
->tot_sent
),
427 * batadv_tp_sender_shutdown() - let sender thread/timer stop gracefully
428 * @tp_vars: the private data of the current TP meter session
429 * @reason: reason for tp meter session stop
431 static void batadv_tp_sender_shutdown(struct batadv_tp_vars
*tp_vars
,
432 enum batadv_tp_meter_reason reason
)
434 if (!atomic_dec_and_test(&tp_vars
->sending
))
437 tp_vars
->reason
= reason
;
441 * batadv_tp_sender_finish() - stop sender session after test_length was reached
442 * @work: delayed work reference of the related tp_vars
444 static void batadv_tp_sender_finish(struct work_struct
*work
)
446 struct delayed_work
*delayed_work
;
447 struct batadv_tp_vars
*tp_vars
;
449 delayed_work
= to_delayed_work(work
);
450 tp_vars
= container_of(delayed_work
, struct batadv_tp_vars
,
453 batadv_tp_sender_shutdown(tp_vars
, BATADV_TP_REASON_COMPLETE
);
457 * batadv_tp_reset_sender_timer() - reschedule the sender timer
458 * @tp_vars: the private TP meter data for this session
460 * Reschedule the timer using tp_vars->rto as delay
462 static void batadv_tp_reset_sender_timer(struct batadv_tp_vars
*tp_vars
)
464 /* most of the time this function is invoked while normal packet
467 if (unlikely(atomic_read(&tp_vars
->sending
) == 0))
468 /* timer ref will be dropped in batadv_tp_sender_cleanup */
471 mod_timer(&tp_vars
->timer
, jiffies
+ msecs_to_jiffies(tp_vars
->rto
));
475 * batadv_tp_sender_timeout() - timer that fires in case of packet loss
476 * @t: address to timer_list inside tp_vars
478 * If fired it means that there was packet loss.
479 * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
480 * reset the cwnd to 3*MSS
482 static void batadv_tp_sender_timeout(struct timer_list
*t
)
484 struct batadv_tp_vars
*tp_vars
= from_timer(tp_vars
, t
, timer
);
485 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
487 if (atomic_read(&tp_vars
->sending
) == 0)
490 /* if the user waited long enough...shutdown the test */
491 if (unlikely(tp_vars
->rto
>= BATADV_TP_MAX_RTO
)) {
492 batadv_tp_sender_shutdown(tp_vars
,
493 BATADV_TP_REASON_DST_UNREACHABLE
);
497 /* RTO exponential backoff
498 * Details in Section 5.5 of RFC6298
502 spin_lock_bh(&tp_vars
->cwnd_lock
);
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;
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
));
513 tp_vars
->cwnd
= BATADV_TP_PLEN
* 3;
515 spin_unlock_bh(&tp_vars
->cwnd_lock
);
517 /* resend the non-ACKed packets.. */
518 tp_vars
->last_sent
= atomic_read(&tp_vars
->last_acked
);
519 wake_up(&tp_vars
->more_bytes
);
521 batadv_tp_reset_sender_timer(tp_vars
);
525 * batadv_tp_fill_prerandom() - Fill buffer with prefetched random bytes
526 * @tp_vars: the private TP meter data for this session
527 * @buf: Buffer to fill with bytes
528 * @nbytes: amount of pseudorandom bytes
530 static void batadv_tp_fill_prerandom(struct batadv_tp_vars
*tp_vars
,
531 u8
*buf
, size_t nbytes
)
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
);
545 local_offset
%= sizeof(batadv_tp_prerandom
);
546 bytes_inbuf
= sizeof(batadv_tp_prerandom
) - local_offset
;
547 to_copy
= min(nbytes
, bytes_inbuf
);
549 memcpy(&buf
[pos
], &batadv_tp_prerandom
[local_offset
], to_copy
);
557 * batadv_tp_send_msg() - send a single message
558 * @tp_vars: the private TP meter data for this session
559 * @src: source mac address
560 * @orig_node: the originator of the destination
561 * @seqno: sequence number of this packet
562 * @len: length of the entire packet
563 * @session: session identifier
564 * @uid: local ICMP "socket" index
565 * @timestamp: timestamp in jiffies which is replied in ack
567 * Create and send a single TP Meter message.
569 * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
570 * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
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
)
578 struct batadv_icmp_tp_packet
*icmp
;
584 skb
= netdev_alloc_skb_ip_align(NULL
, len
+ ETH_HLEN
);
586 return BATADV_TP_REASON_MEMORY_ERROR
;
588 skb_reserve(skb
, ETH_HLEN
);
589 icmp
= skb_put(skb
, sizeof(*icmp
));
591 /* fill the icmp header */
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
;
600 icmp
->subtype
= BATADV_TP_MSG
;
601 memcpy(icmp
->session
, session
, sizeof(icmp
->session
));
602 icmp
->seqno
= htonl(seqno
);
603 icmp
->timestamp
= htonl(timestamp
);
605 data_len
= len
- sizeof(*icmp
);
606 data
= skb_put(skb
, data_len
);
607 batadv_tp_fill_prerandom(tp_vars
, data
, data_len
);
609 r
= batadv_send_skb_to_orig(skb
, orig_node
, NULL
);
610 if (r
== NET_XMIT_SUCCESS
)
613 return BATADV_TP_REASON_CANT_SEND
;
617 * batadv_tp_recv_ack() - ACK receiving function
618 * @bat_priv: the bat priv with all the soft interface information
619 * @skb: the buffer containing the received packet
621 * Process a received TP ACK packet
623 static void batadv_tp_recv_ack(struct batadv_priv
*bat_priv
,
624 const struct sk_buff
*skb
)
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
;
634 packet_len
= BATADV_TP_PLEN
;
635 mss
= BATADV_TP_PLEN
;
636 packet_len
+= sizeof(struct batadv_unicast_packet
);
638 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
640 /* find the tp_vars */
641 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
643 if (unlikely(!tp_vars
))
646 if (unlikely(atomic_read(&tp_vars
->sending
) == 0))
649 /* old ACK? silently drop it.. */
650 if (batadv_seq_before(ntohl(icmp
->seqno
),
651 (u32
)atomic_read(&tp_vars
->last_acked
)))
654 primary_if
= batadv_primary_if_get_selected(bat_priv
);
655 if (unlikely(!primary_if
))
658 orig_node
= batadv_orig_hash_find(bat_priv
, icmp
->orig
);
659 if (unlikely(!orig_node
))
662 /* update RTO with the new sampled RTT, if any */
663 rtt
= jiffies_to_msecs(jiffies
) - ntohl(icmp
->timestamp
);
664 if (icmp
->timestamp
&& rtt
)
665 batadv_tp_update_rto(tp_vars
, rtt
);
667 /* ACK for new data... reset the timer */
668 batadv_tp_reset_sender_timer(tp_vars
);
670 recv_ack
= ntohl(icmp
->seqno
);
672 /* check if this ACK is a duplicate */
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)
678 if (recv_ack
>= tp_vars
->recover
)
681 /* if this is the third duplicate ACK do Fast Retransmit */
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
));
687 spin_lock_bh(&tp_vars
->cwnd_lock
);
690 tp_vars
->fast_recovery
= true;
691 /* Set recover to the last outstanding seqno when Fast Recovery
692 * is entered. RFC6582, Section 3.2, step 1
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
,
702 tp_vars
->dec_cwnd
= 0;
703 tp_vars
->last_sent
= recv_ack
;
705 spin_unlock_bh(&tp_vars
->cwnd_lock
);
707 /* count the acked data */
708 atomic64_add(recv_ack
- atomic_read(&tp_vars
->last_acked
),
710 /* reset the duplicate ACKs counter */
711 atomic_set(&tp_vars
->dup_acks
, 0);
713 if (tp_vars
->fast_recovery
) {
715 if (batadv_seq_before(recv_ack
, tp_vars
->recover
)) {
716 /* this is another hole in the window. React
717 * immediately as specified by NewReno (see
718 * Section 3.2 of RFC6582 for details)
720 dev_addr
= primary_if
->net_dev
->dev_addr
;
721 batadv_tp_send_msg(tp_vars
, dev_addr
,
723 packet_len
, icmp
->session
,
725 jiffies_to_msecs(jiffies
));
726 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
,
729 tp_vars
->fast_recovery
= false;
730 /* set cwnd to the value of ss_threshold at the
731 * moment that Fast Recovery was entered.
732 * RFC6582, Section 3.2, step 3
734 cwnd
= batadv_tp_cwnd(tp_vars
->ss_threshold
, 0,
736 tp_vars
->cwnd
= cwnd
;
741 if (recv_ack
- atomic_read(&tp_vars
->last_acked
) >= mss
)
742 batadv_tp_update_cwnd(tp_vars
, mss
);
744 /* move the Transmit Window */
745 atomic_set(&tp_vars
->last_acked
, recv_ack
);
748 wake_up(&tp_vars
->more_bytes
);
750 if (likely(primary_if
))
751 batadv_hardif_put(primary_if
);
752 if (likely(orig_node
))
753 batadv_orig_node_put(orig_node
);
755 batadv_tp_vars_put(tp_vars
);
759 * batadv_tp_avail() - check if congestion window is not full
760 * @tp_vars: the private data of the current TP meter session
761 * @payload_len: size of the payload of a single message
763 * Return: true when congestion window is not full, false otherwise
765 static bool batadv_tp_avail(struct batadv_tp_vars
*tp_vars
,
768 u32 win_left
, win_limit
;
770 win_limit
= atomic_read(&tp_vars
->last_acked
) + tp_vars
->cwnd
;
771 win_left
= win_limit
- tp_vars
->last_sent
;
773 return win_left
>= payload_len
;
777 * batadv_tp_wait_available() - wait until congestion window becomes free or
779 * @tp_vars: the private data of the current TP meter session
780 * @plen: size of the payload of a single message
782 * Return: 0 if the condition evaluated to false after the timeout elapsed,
783 * 1 if the condition evaluated to true after the timeout elapsed, the
784 * remaining jiffies (at least 1) if the condition evaluated to true before
785 * the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
787 static int batadv_tp_wait_available(struct batadv_tp_vars
*tp_vars
, size_t plen
)
791 ret
= wait_event_interruptible_timeout(tp_vars
->more_bytes
,
792 batadv_tp_avail(tp_vars
, plen
),
799 * batadv_tp_send() - main sending thread of a tp meter session
800 * @arg: address of the related tp_vars
802 * Return: nothing, this function never returns
804 static int batadv_tp_send(void *arg
)
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
;
813 if (unlikely(tp_vars
->role
!= BATADV_TP_SENDER
)) {
814 err
= BATADV_TP_REASON_DST_UNREACHABLE
;
815 tp_vars
->reason
= err
;
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
;
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
;
833 /* assume that all the hard_interfaces have a correctly
834 * configured MTU, so use the soft_iface MTU as MSS.
835 * This might not be true and in that case the fragmentation
837 * Now, try to send the packet as it is
839 payload_len
= BATADV_TP_PLEN
;
840 BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet
) > BATADV_TP_PLEN
);
842 batadv_tp_reset_sender_timer(tp_vars
);
844 /* queue the worker in charge of terminating the test */
845 queue_delayed_work(batadv_event_workqueue
, &tp_vars
->finish_work
,
846 msecs_to_jiffies(tp_vars
->test_length
));
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
);
854 /* to emulate normal unicast traffic, add to the payload len
855 * the size of the unicast header
857 packet_len
= payload_len
+ sizeof(struct batadv_unicast_packet
);
859 err
= batadv_tp_send_msg(tp_vars
, primary_if
->net_dev
->dev_addr
,
860 orig_node
, tp_vars
->last_sent
,
862 tp_vars
->session
, tp_vars
->icmp_uid
,
863 jiffies_to_msecs(jiffies
));
865 /* something went wrong during the preparation/transmission */
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",
870 /* ensure nobody else tries to stop the thread now */
871 if (atomic_dec_and_test(&tp_vars
->sending
))
872 tp_vars
->reason
= err
;
876 /* right-shift the TWND */
878 tp_vars
->last_sent
+= payload_len
;
884 if (likely(primary_if
))
885 batadv_hardif_put(primary_if
);
886 if (likely(orig_node
))
887 batadv_orig_node_put(orig_node
);
889 batadv_tp_sender_end(bat_priv
, tp_vars
);
890 batadv_tp_sender_cleanup(bat_priv
, tp_vars
);
892 batadv_tp_vars_put(tp_vars
);
898 * batadv_tp_start_kthread() - start new thread which manages the tp meter
900 * @tp_vars: the private data of the current TP meter session
902 static void batadv_tp_start_kthread(struct batadv_tp_vars
*tp_vars
)
904 struct task_struct
*kthread
;
905 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
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
,
913 pr_err("batadv: cannot create tp meter kthread\n");
914 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR
,
916 bat_priv
, session_cookie
);
918 /* drop reserved reference for kthread */
919 batadv_tp_vars_put(tp_vars
);
921 /* cleanup of failed tp meter variables */
922 batadv_tp_sender_cleanup(bat_priv
, tp_vars
);
926 wake_up_process(kthread
);
930 * batadv_tp_start() - start a new tp meter session
931 * @bat_priv: the bat priv with all the soft interface information
932 * @dst: the receiver MAC address
933 * @test_length: test length in milliseconds
934 * @cookie: session cookie
936 void batadv_tp_start(struct batadv_priv
*bat_priv
, const u8
*dst
,
937 u32 test_length
, u32
*cookie
)
939 struct batadv_tp_vars
*tp_vars
;
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
;
949 /* look for an already existing test towards this node */
950 spin_lock_bh(&bat_priv
->tp_list_lock
);
951 tp_vars
= batadv_tp_list_find(bat_priv
, dst
);
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
);
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
);
971 tp_vars
= kmalloc(sizeof(*tp_vars
), GFP_ATOMIC
);
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",
977 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR
,
978 dst
, bat_priv
, session_cookie
);
982 /* initialize tp_vars */
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
;
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
;
995 /* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
996 * For batman-adv the MSS is the size of the payload received by the
997 * soft_interface, hence its MTU
999 tp_vars
->cwnd
= BATADV_TP_PLEN
* 3;
1000 /* at the beginning initialise the SS threshold to the biggest possible
1001 * window size, hence the AWND size
1003 tp_vars
->ss_threshold
= BATADV_TP_AWND
;
1005 /* RTO initial value is 3 seconds.
1006 * Details in Section 2.1 of RFC6298
1008 tp_vars
->rto
= 1000;
1010 tp_vars
->rttvar
= 0;
1012 atomic64_set(&tp_vars
->tot_sent
, 0);
1014 kref_get(&tp_vars
->refcount
);
1015 timer_setup(&tp_vars
->timer
, batadv_tp_sender_timeout
, 0);
1017 tp_vars
->bat_priv
= bat_priv
;
1018 tp_vars
->start_time
= jiffies
;
1020 init_waitqueue_head(&tp_vars
->more_bytes
);
1022 spin_lock_init(&tp_vars
->unacked_lock
);
1023 INIT_LIST_HEAD(&tp_vars
->unacked_list
);
1025 spin_lock_init(&tp_vars
->cwnd_lock
);
1027 tp_vars
->prerandom_offset
= 0;
1028 spin_lock_init(&tp_vars
->prerandom_lock
);
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
);
1034 tp_vars
->test_length
= test_length
;
1035 if (!tp_vars
->test_length
)
1036 tp_vars
->test_length
= BATADV_TP_DEF_TEST_LENGTH
;
1038 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1039 "Meter: starting throughput meter towards %pM (length=%ums)\n",
1042 /* init work item for finished tp tests */
1043 INIT_DELAYED_WORK(&tp_vars
->finish_work
, batadv_tp_sender_finish
);
1045 /* start tp kthread. This way the write() call issued from userspace can
1046 * happily return and avoid to block
1048 batadv_tp_start_kthread(tp_vars
);
1050 /* don't return reference to new tp_vars */
1051 batadv_tp_vars_put(tp_vars
);
1055 * batadv_tp_stop() - stop currently running tp meter session
1056 * @bat_priv: the bat priv with all the soft interface information
1057 * @dst: the receiver MAC address
1058 * @return_value: reason for tp meter session stop
1060 void batadv_tp_stop(struct batadv_priv
*bat_priv
, const u8
*dst
,
1063 struct batadv_orig_node
*orig_node
;
1064 struct batadv_tp_vars
*tp_vars
;
1066 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1067 "Meter: stopping test towards %pM\n", dst
);
1069 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
1073 tp_vars
= batadv_tp_list_find(bat_priv
, orig_node
->orig
);
1075 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1076 "Meter: trying to interrupt an already over connection\n");
1080 batadv_tp_sender_shutdown(tp_vars
, return_value
);
1081 batadv_tp_vars_put(tp_vars
);
1083 batadv_orig_node_put(orig_node
);
1087 * batadv_tp_reset_receiver_timer() - reset the receiver shutdown timer
1088 * @tp_vars: the private data of the current TP meter session
1090 * start the receiver shutdown timer or reset it if already started
1092 static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars
*tp_vars
)
1094 mod_timer(&tp_vars
->timer
,
1095 jiffies
+ msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT
));
1099 * batadv_tp_receiver_shutdown() - stop a tp meter receiver when timeout is
1100 * reached without received ack
1101 * @t: address to timer_list inside tp_vars
1103 static void batadv_tp_receiver_shutdown(struct timer_list
*t
)
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
;
1109 bat_priv
= tp_vars
->bat_priv
;
1111 /* if there is recent activity rearm the timer */
1112 if (!batadv_has_timed_out(tp_vars
->last_recv_time
,
1113 BATADV_TP_RECV_TIMEOUT
)) {
1114 /* reset the receiver shutdown timer */
1115 batadv_tp_reset_receiver_timer(tp_vars
);
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
);
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
);
1127 /* drop list reference */
1128 batadv_tp_vars_put(tp_vars
);
1130 atomic_dec(&bat_priv
->tp_num
);
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
);
1137 spin_unlock_bh(&tp_vars
->unacked_lock
);
1139 /* drop reference of timer */
1140 batadv_tp_vars_put(tp_vars
);
1144 * batadv_tp_send_ack() - send an ACK packet
1145 * @bat_priv: the bat priv with all the soft interface information
1146 * @dst: the mac address of the destination originator
1147 * @seq: the sequence number to ACK
1148 * @timestamp: the timestamp to echo back in the ACK
1149 * @session: session identifier
1150 * @socket_index: local ICMP socket identifier
1152 * Return: 0 on success, a positive integer representing the reason of the
1155 static int batadv_tp_send_ack(struct batadv_priv
*bat_priv
, const u8
*dst
,
1156 u32 seq
, __be32 timestamp
, const u8
*session
,
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
;
1165 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
1166 if (unlikely(!orig_node
)) {
1167 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1171 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1172 if (unlikely(!primary_if
)) {
1173 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1177 skb
= netdev_alloc_skb_ip_align(NULL
, sizeof(*icmp
) + ETH_HLEN
);
1178 if (unlikely(!skb
)) {
1179 ret
= BATADV_TP_REASON_MEMORY_ERROR
;
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
;
1193 icmp
->subtype
= BATADV_TP_ACK
;
1194 memcpy(icmp
->session
, session
, sizeof(icmp
->session
));
1195 icmp
->seqno
= htonl(seq
);
1196 icmp
->timestamp
= timestamp
;
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
;
1207 if (likely(orig_node
))
1208 batadv_orig_node_put(orig_node
);
1209 if (likely(primary_if
))
1210 batadv_hardif_put(primary_if
);
1216 * batadv_tp_handle_out_of_order() - store an out of order packet
1217 * @tp_vars: the private data of the current TP meter session
1218 * @skb: the buffer containing the received packet
1220 * Store the out of order packet in the unacked list for late processing. This
1221 * packets are kept in this list so that they can be ACKed at once as soon as
1222 * all the previous packets have been received
1224 * Return: true if the packed has been successfully processed, false otherwise
1226 static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars
*tp_vars
,
1227 const struct sk_buff
*skb
)
1229 const struct batadv_icmp_tp_packet
*icmp
;
1230 struct batadv_tp_unacked
*un
, *new;
1234 new = kmalloc(sizeof(*new), GFP_ATOMIC
);
1238 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1240 new->seqno
= ntohl(icmp
->seqno
);
1241 payload_len
= skb
->len
- sizeof(struct batadv_unicast_packet
);
1242 new->len
= payload_len
;
1244 spin_lock_bh(&tp_vars
->unacked_lock
);
1245 /* if the list is empty immediately attach this new object */
1246 if (list_empty(&tp_vars
->unacked_list
)) {
1247 list_add(&new->list
, &tp_vars
->unacked_list
);
1251 /* otherwise loop over the list and either drop the packet because this
1252 * is a duplicate or store it at the right position.
1254 * The iteration is done in the reverse way because it is likely that
1255 * the last received packet (the one being processed now) has a bigger
1256 * seqno than all the others already stored.
1258 list_for_each_entry_reverse(un
, &tp_vars
->unacked_list
, list
) {
1259 /* check for duplicates */
1260 if (new->seqno
== un
->seqno
) {
1261 if (new->len
> un
->len
)
1268 /* look for the right position */
1269 if (batadv_seq_before(new->seqno
, un
->seqno
))
1272 /* as soon as an entry having a bigger seqno is found, the new
1273 * one is attached _after_ it. In this way the list is kept in
1276 list_add_tail(&new->list
, &un
->list
);
1281 /* received packet with smallest seqno out of order; add it to front */
1283 list_add(&new->list
, &tp_vars
->unacked_list
);
1286 spin_unlock_bh(&tp_vars
->unacked_lock
);
1292 * batadv_tp_ack_unordered() - update number received bytes in current stream
1294 * @tp_vars: the private data of the current TP meter session
1296 static void batadv_tp_ack_unordered(struct batadv_tp_vars
*tp_vars
)
1298 struct batadv_tp_unacked
*un
, *safe
;
1301 /* go through the unacked packet list and possibly ACK them as
1304 spin_lock_bh(&tp_vars
->unacked_lock
);
1305 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
1306 /* the list is ordered, therefore it is possible to stop as soon
1307 * there is a gap between the last acked seqno and the seqno of
1308 * the packet under inspection
1310 if (batadv_seq_before(tp_vars
->last_recv
, un
->seqno
))
1313 to_ack
= un
->seqno
+ un
->len
- tp_vars
->last_recv
;
1315 if (batadv_seq_before(tp_vars
->last_recv
, un
->seqno
+ un
->len
))
1316 tp_vars
->last_recv
+= to_ack
;
1318 list_del(&un
->list
);
1321 spin_unlock_bh(&tp_vars
->unacked_lock
);
1325 * batadv_tp_init_recv() - return matching or create new receiver tp_vars
1326 * @bat_priv: the bat priv with all the soft interface information
1327 * @icmp: received icmp tp msg
1329 * Return: corresponding tp_vars or NULL on errors
1331 static struct batadv_tp_vars
*
1332 batadv_tp_init_recv(struct batadv_priv
*bat_priv
,
1333 const struct batadv_icmp_tp_packet
*icmp
)
1335 struct batadv_tp_vars
*tp_vars
;
1337 spin_lock_bh(&bat_priv
->tp_list_lock
);
1338 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
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");
1349 tp_vars
= kmalloc(sizeof(*tp_vars
), GFP_ATOMIC
);
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
);
1360 spin_lock_init(&tp_vars
->unacked_lock
);
1361 INIT_LIST_HEAD(&tp_vars
->unacked_list
);
1363 kref_get(&tp_vars
->refcount
);
1364 hlist_add_head_rcu(&tp_vars
->list
, &bat_priv
->tp_list
);
1366 kref_get(&tp_vars
->refcount
);
1367 timer_setup(&tp_vars
->timer
, batadv_tp_receiver_shutdown
, 0);
1369 batadv_tp_reset_receiver_timer(tp_vars
);
1372 spin_unlock_bh(&bat_priv
->tp_list_lock
);
1378 * batadv_tp_recv_msg() - process a single data message
1379 * @bat_priv: the bat priv with all the soft interface information
1380 * @skb: the buffer containing the received packet
1382 * Process a received TP MSG packet
1384 static void batadv_tp_recv_msg(struct batadv_priv
*bat_priv
,
1385 const struct sk_buff
*skb
)
1387 const struct batadv_icmp_tp_packet
*icmp
;
1388 struct batadv_tp_vars
*tp_vars
;
1392 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1394 seqno
= ntohl(icmp
->seqno
);
1395 /* check if this is the first seqno. This means that if the
1396 * first packet is lost, the tp meter does not work anymore!
1398 if (seqno
== BATADV_TP_FIRST_SEQ
) {
1399 tp_vars
= batadv_tp_init_recv(bat_priv
, icmp
);
1401 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1402 "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
1406 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
1409 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1410 "Unexpected packet from %pM!\n",
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",
1423 tp_vars
->last_recv_time
= jiffies
;
1425 /* if the packet is a duplicate, it may be the case that an ACK has been
1426 * lost. Resend the ACK
1428 if (batadv_seq_before(seqno
, tp_vars
->last_recv
))
1431 /* if the packet is out of order enqueue it */
1432 if (ntohl(icmp
->seqno
) != tp_vars
->last_recv
) {
1433 /* exit immediately (and do not send any ACK) if the packet has
1434 * not been enqueued correctly
1436 if (!batadv_tp_handle_out_of_order(tp_vars
, skb
))
1439 /* send a duplicate ACK */
1443 /* if everything was fine count the ACKed bytes */
1444 packet_size
= skb
->len
- sizeof(struct batadv_unicast_packet
);
1445 tp_vars
->last_recv
+= packet_size
;
1447 /* check if this ordered message filled a gap.... */
1448 batadv_tp_ack_unordered(tp_vars
);
1451 /* send the ACK. If the received packet was out of order, the ACK that
1452 * is going to be sent is a duplicate (the sender will count them and
1453 * possibly enter Fast Retransmit as soon as it has reached 3)
1455 batadv_tp_send_ack(bat_priv
, icmp
->orig
, tp_vars
->last_recv
,
1456 icmp
->timestamp
, icmp
->session
, icmp
->uid
);
1458 if (likely(tp_vars
))
1459 batadv_tp_vars_put(tp_vars
);
1463 * batadv_tp_meter_recv() - main TP Meter receiving function
1464 * @bat_priv: the bat priv with all the soft interface information
1465 * @skb: the buffer containing the received packet
1467 void batadv_tp_meter_recv(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
)
1469 struct batadv_icmp_tp_packet
*icmp
;
1471 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1473 switch (icmp
->subtype
) {
1475 batadv_tp_recv_msg(bat_priv
, skb
);
1478 batadv_tp_recv_ack(bat_priv
, skb
);
1481 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1482 "Received unknown TP Metric packet type %u\n",
1489 * batadv_tp_meter_init() - initialize global tp_meter structures
1491 void __init
batadv_tp_meter_init(void)
1493 get_random_bytes(batadv_tp_prerandom
, sizeof(batadv_tp_prerandom
));