1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2020 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/minmax.h>
27 #include <linux/netdevice.h>
28 #include <linux/param.h>
29 #include <linux/printk.h>
30 #include <linux/random.h>
31 #include <linux/rculist.h>
32 #include <linux/rcupdate.h>
33 #include <linux/sched.h>
34 #include <linux/skbuff.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/stddef.h>
38 #include <linux/string.h>
39 #include <linux/timer.h>
40 #include <linux/wait.h>
41 #include <linux/workqueue.h>
42 #include <uapi/linux/batadv_packet.h>
43 #include <uapi/linux/batman_adv.h>
45 #include "hard-interface.h"
48 #include "originator.h"
52 * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
55 #define BATADV_TP_DEF_TEST_LENGTH 10000
58 * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
60 #define BATADV_TP_AWND 0x20000000
63 * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
64 * get anything for such amount of milliseconds, the connection is killed
66 #define BATADV_TP_RECV_TIMEOUT 1000
69 * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
70 * such amount of milliseconds, the receiver is considered unreachable and the
71 * connection is killed
73 #define BATADV_TP_MAX_RTO 30000
76 * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
77 * in order to immediately trigger a wrap around (test purposes)
79 #define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
82 * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
85 #define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
86 sizeof(struct batadv_unicast_packet))
88 static u8 batadv_tp_prerandom
[4096] __read_mostly
;
91 * batadv_tp_session_cookie() - generate session cookie based on session ids
92 * @session: TP session identifier
93 * @icmp_uid: icmp pseudo uid of the tp session
95 * Return: 32 bit tp_meter session cookie
97 static u32
batadv_tp_session_cookie(const u8 session
[2], u8 icmp_uid
)
101 cookie
= icmp_uid
<< 16;
102 cookie
|= session
[0] << 8;
103 cookie
|= session
[1];
109 * batadv_tp_cwnd() - compute the new cwnd size
110 * @base: base cwnd size value
111 * @increment: the value to add to base to get the new size
112 * @min: minimum cwnd value (usually MSS)
114 * Return the new cwnd size and ensure it does not exceed the Advertised
115 * Receiver Window size. It is wrapped around safely.
116 * For details refer to Section 3.1 of RFC5681
118 * Return: new congestion window size in bytes
120 static u32
batadv_tp_cwnd(u32 base
, u32 increment
, u32 min
)
122 u32 new_size
= base
+ increment
;
124 /* check for wrap-around */
126 new_size
= (u32
)ULONG_MAX
;
128 new_size
= min_t(u32
, new_size
, BATADV_TP_AWND
);
130 return max_t(u32
, new_size
, min
);
134 * batadv_tp_updated_cwnd() - update the Congestion Windows
135 * @tp_vars: the private data of the current TP meter session
136 * @mss: maximum segment size of transmission
138 * 1) if the session is in Slow Start, the CWND has to be increased by 1
139 * MSS every unique received ACK
140 * 2) if the session is in Congestion Avoidance, the CWND has to be
141 * increased by MSS * MSS / CWND for every unique received ACK
143 static void batadv_tp_update_cwnd(struct batadv_tp_vars
*tp_vars
, u32 mss
)
145 spin_lock_bh(&tp_vars
->cwnd_lock
);
148 if (tp_vars
->cwnd
<= tp_vars
->ss_threshold
) {
149 tp_vars
->dec_cwnd
= 0;
150 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
, mss
, mss
);
151 spin_unlock_bh(&tp_vars
->cwnd_lock
);
155 /* increment CWND at least of 1 (section 3.1 of RFC5681) */
156 tp_vars
->dec_cwnd
+= max_t(u32
, 1U << 3,
157 ((mss
* mss
) << 6) / (tp_vars
->cwnd
<< 3));
158 if (tp_vars
->dec_cwnd
< (mss
<< 3)) {
159 spin_unlock_bh(&tp_vars
->cwnd_lock
);
163 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
, mss
, mss
);
164 tp_vars
->dec_cwnd
= 0;
166 spin_unlock_bh(&tp_vars
->cwnd_lock
);
170 * batadv_tp_update_rto() - calculate new retransmission timeout
171 * @tp_vars: the private data of the current TP meter session
172 * @new_rtt: new roundtrip time in msec
174 static void batadv_tp_update_rto(struct batadv_tp_vars
*tp_vars
,
180 * Details in Section 2.2 and 2.3 of RFC6298
182 * It's tricky to understand. Don't lose hair please.
183 * Inspired by tcp_rtt_estimator() tcp_input.c
185 if (tp_vars
->srtt
!= 0) {
186 m
-= (tp_vars
->srtt
>> 3); /* m is now error in rtt est */
187 tp_vars
->srtt
+= m
; /* rtt = 7/8 srtt + 1/8 new */
191 m
-= (tp_vars
->rttvar
>> 2);
192 tp_vars
->rttvar
+= m
; /* mdev ~= 3/4 rttvar + 1/4 new */
194 /* first measure getting in */
195 tp_vars
->srtt
= m
<< 3; /* take the measured time to be srtt */
196 tp_vars
->rttvar
= m
<< 1; /* new_rtt / 2 */
199 /* rto = srtt + 4 * rttvar.
200 * rttvar is scaled by 4, therefore doesn't need to be multiplied
202 tp_vars
->rto
= (tp_vars
->srtt
>> 3) + tp_vars
->rttvar
;
206 * batadv_tp_batctl_notify() - send client status result to client
207 * @reason: reason for tp meter session stop
208 * @dst: destination of tp_meter session
209 * @bat_priv: the bat priv with all the soft interface information
210 * @start_time: start of transmission in jiffies
211 * @total_sent: bytes acked to the receiver
212 * @cookie: cookie of tp_meter session
214 static void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason
,
215 const u8
*dst
, struct batadv_priv
*bat_priv
,
216 unsigned long start_time
, u64 total_sent
,
223 if (!batadv_tp_is_error(reason
)) {
224 result
= BATADV_TP_REASON_COMPLETE
;
225 test_time
= jiffies_to_msecs(jiffies
- start_time
);
226 total_bytes
= total_sent
;
233 batadv_netlink_tpmeter_notify(bat_priv
, dst
, result
, test_time
,
234 total_bytes
, cookie
);
238 * batadv_tp_batctl_error_notify() - send client error result to client
239 * @reason: reason for tp meter session stop
240 * @dst: destination of tp_meter session
241 * @bat_priv: the bat priv with all the soft interface information
242 * @cookie: cookie of tp_meter session
244 static void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason
,
246 struct batadv_priv
*bat_priv
,
249 batadv_tp_batctl_notify(reason
, dst
, bat_priv
, 0, 0, cookie
);
253 * batadv_tp_list_find() - find a tp_vars object in the global list
254 * @bat_priv: the bat priv with all the soft interface information
255 * @dst: the other endpoint MAC address to look for
257 * Look for a tp_vars object matching dst as end_point and return it after
258 * having increment the refcounter. Return NULL is not found
260 * Return: matching tp_vars or NULL when no tp_vars with @dst was found
262 static struct batadv_tp_vars
*batadv_tp_list_find(struct batadv_priv
*bat_priv
,
265 struct batadv_tp_vars
*pos
, *tp_vars
= NULL
;
268 hlist_for_each_entry_rcu(pos
, &bat_priv
->tp_list
, list
) {
269 if (!batadv_compare_eth(pos
->other_end
, dst
))
272 /* most of the time this function is invoked during the normal
273 * process..it makes sens to pay more when the session is
274 * finished and to speed the process up during the measurement
276 if (unlikely(!kref_get_unless_zero(&pos
->refcount
)))
288 * batadv_tp_list_find_session() - find tp_vars session object in the global
290 * @bat_priv: the bat priv with all the soft interface information
291 * @dst: the other endpoint MAC address to look for
292 * @session: session identifier
294 * Look for a tp_vars object matching dst as end_point, session as tp meter
295 * session and return it after having increment the refcounter. Return NULL
298 * Return: matching tp_vars or NULL when no tp_vars was found
300 static struct batadv_tp_vars
*
301 batadv_tp_list_find_session(struct batadv_priv
*bat_priv
, const u8
*dst
,
304 struct batadv_tp_vars
*pos
, *tp_vars
= NULL
;
307 hlist_for_each_entry_rcu(pos
, &bat_priv
->tp_list
, list
) {
308 if (!batadv_compare_eth(pos
->other_end
, dst
))
311 if (memcmp(pos
->session
, session
, sizeof(pos
->session
)) != 0)
314 /* most of the time this function is invoked during the normal
315 * process..it makes sense to pay more when the session is
316 * finished and to speed the process up during the measurement
318 if (unlikely(!kref_get_unless_zero(&pos
->refcount
)))
330 * batadv_tp_vars_release() - release batadv_tp_vars from lists and queue for
331 * free after rcu grace period
332 * @ref: kref pointer of the batadv_tp_vars
334 static void batadv_tp_vars_release(struct kref
*ref
)
336 struct batadv_tp_vars
*tp_vars
;
337 struct batadv_tp_unacked
*un
, *safe
;
339 tp_vars
= container_of(ref
, struct batadv_tp_vars
, refcount
);
341 /* lock should not be needed because this object is now out of any
344 spin_lock_bh(&tp_vars
->unacked_lock
);
345 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
349 spin_unlock_bh(&tp_vars
->unacked_lock
);
351 kfree_rcu(tp_vars
, rcu
);
355 * batadv_tp_vars_put() - decrement the batadv_tp_vars refcounter and possibly
357 * @tp_vars: the private data of the current TP meter session to be free'd
359 static void batadv_tp_vars_put(struct batadv_tp_vars
*tp_vars
)
361 kref_put(&tp_vars
->refcount
, batadv_tp_vars_release
);
365 * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer
366 * @bat_priv: the bat priv with all the soft interface information
367 * @tp_vars: the private data of the current TP meter session to cleanup
369 static void batadv_tp_sender_cleanup(struct batadv_priv
*bat_priv
,
370 struct batadv_tp_vars
*tp_vars
)
372 cancel_delayed_work(&tp_vars
->finish_work
);
374 spin_lock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
375 hlist_del_rcu(&tp_vars
->list
);
376 spin_unlock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
378 /* drop list reference */
379 batadv_tp_vars_put(tp_vars
);
381 atomic_dec(&tp_vars
->bat_priv
->tp_num
);
383 /* kill the timer and remove its reference */
384 del_timer_sync(&tp_vars
->timer
);
385 /* the worker might have rearmed itself therefore we kill it again. Note
386 * that if the worker should run again before invoking the following
387 * del_timer(), it would not re-arm itself once again because the status
390 del_timer(&tp_vars
->timer
);
391 batadv_tp_vars_put(tp_vars
);
395 * batadv_tp_sender_end() - print info about ended session and inform client
396 * @bat_priv: the bat priv with all the soft interface information
397 * @tp_vars: the private data of the current TP meter session
399 static void batadv_tp_sender_end(struct batadv_priv
*bat_priv
,
400 struct batadv_tp_vars
*tp_vars
)
404 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
405 "Test towards %pM finished..shutting down (reason=%d)\n",
406 tp_vars
->other_end
, tp_vars
->reason
);
408 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
409 "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
410 tp_vars
->srtt
>> 3, tp_vars
->rttvar
>> 2, tp_vars
->rto
);
412 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
413 "Final values: cwnd=%u ss_threshold=%u\n",
414 tp_vars
->cwnd
, tp_vars
->ss_threshold
);
416 session_cookie
= batadv_tp_session_cookie(tp_vars
->session
,
419 batadv_tp_batctl_notify(tp_vars
->reason
,
423 atomic64_read(&tp_vars
->tot_sent
),
428 * batadv_tp_sender_shutdown() - let sender thread/timer stop gracefully
429 * @tp_vars: the private data of the current TP meter session
430 * @reason: reason for tp meter session stop
432 static void batadv_tp_sender_shutdown(struct batadv_tp_vars
*tp_vars
,
433 enum batadv_tp_meter_reason reason
)
435 if (!atomic_dec_and_test(&tp_vars
->sending
))
438 tp_vars
->reason
= reason
;
442 * batadv_tp_sender_finish() - stop sender session after test_length was reached
443 * @work: delayed work reference of the related tp_vars
445 static void batadv_tp_sender_finish(struct work_struct
*work
)
447 struct delayed_work
*delayed_work
;
448 struct batadv_tp_vars
*tp_vars
;
450 delayed_work
= to_delayed_work(work
);
451 tp_vars
= container_of(delayed_work
, struct batadv_tp_vars
,
454 batadv_tp_sender_shutdown(tp_vars
, BATADV_TP_REASON_COMPLETE
);
458 * batadv_tp_reset_sender_timer() - reschedule the sender timer
459 * @tp_vars: the private TP meter data for this session
461 * Reschedule the timer using tp_vars->rto as delay
463 static void batadv_tp_reset_sender_timer(struct batadv_tp_vars
*tp_vars
)
465 /* most of the time this function is invoked while normal packet
468 if (unlikely(atomic_read(&tp_vars
->sending
) == 0))
469 /* timer ref will be dropped in batadv_tp_sender_cleanup */
472 mod_timer(&tp_vars
->timer
, jiffies
+ msecs_to_jiffies(tp_vars
->rto
));
476 * batadv_tp_sender_timeout() - timer that fires in case of packet loss
477 * @t: address to timer_list inside tp_vars
479 * If fired it means that there was packet loss.
480 * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
481 * reset the cwnd to 3*MSS
483 static void batadv_tp_sender_timeout(struct timer_list
*t
)
485 struct batadv_tp_vars
*tp_vars
= from_timer(tp_vars
, t
, timer
);
486 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
488 if (atomic_read(&tp_vars
->sending
) == 0)
491 /* if the user waited long enough...shutdown the test */
492 if (unlikely(tp_vars
->rto
>= BATADV_TP_MAX_RTO
)) {
493 batadv_tp_sender_shutdown(tp_vars
,
494 BATADV_TP_REASON_DST_UNREACHABLE
);
498 /* RTO exponential backoff
499 * Details in Section 5.5 of RFC6298
503 spin_lock_bh(&tp_vars
->cwnd_lock
);
505 tp_vars
->ss_threshold
= tp_vars
->cwnd
>> 1;
506 if (tp_vars
->ss_threshold
< BATADV_TP_PLEN
* 2)
507 tp_vars
->ss_threshold
= BATADV_TP_PLEN
* 2;
509 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
510 "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
511 tp_vars
->other_end
, tp_vars
->cwnd
, tp_vars
->ss_threshold
,
512 atomic_read(&tp_vars
->last_acked
));
514 tp_vars
->cwnd
= BATADV_TP_PLEN
* 3;
516 spin_unlock_bh(&tp_vars
->cwnd_lock
);
518 /* resend the non-ACKed packets.. */
519 tp_vars
->last_sent
= atomic_read(&tp_vars
->last_acked
);
520 wake_up(&tp_vars
->more_bytes
);
522 batadv_tp_reset_sender_timer(tp_vars
);
526 * batadv_tp_fill_prerandom() - Fill buffer with prefetched random bytes
527 * @tp_vars: the private TP meter data for this session
528 * @buf: Buffer to fill with bytes
529 * @nbytes: amount of pseudorandom bytes
531 static void batadv_tp_fill_prerandom(struct batadv_tp_vars
*tp_vars
,
532 u8
*buf
, size_t nbytes
)
539 spin_lock_bh(&tp_vars
->prerandom_lock
);
540 local_offset
= tp_vars
->prerandom_offset
;
541 tp_vars
->prerandom_offset
+= nbytes
;
542 tp_vars
->prerandom_offset
%= sizeof(batadv_tp_prerandom
);
543 spin_unlock_bh(&tp_vars
->prerandom_lock
);
546 local_offset
%= sizeof(batadv_tp_prerandom
);
547 bytes_inbuf
= sizeof(batadv_tp_prerandom
) - local_offset
;
548 to_copy
= min(nbytes
, bytes_inbuf
);
550 memcpy(&buf
[pos
], &batadv_tp_prerandom
[local_offset
], to_copy
);
558 * batadv_tp_send_msg() - send a single message
559 * @tp_vars: the private TP meter data for this session
560 * @src: source mac address
561 * @orig_node: the originator of the destination
562 * @seqno: sequence number of this packet
563 * @len: length of the entire packet
564 * @session: session identifier
565 * @uid: local ICMP "socket" index
566 * @timestamp: timestamp in jiffies which is replied in ack
568 * Create and send a single TP Meter message.
570 * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
571 * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
574 static int batadv_tp_send_msg(struct batadv_tp_vars
*tp_vars
, const u8
*src
,
575 struct batadv_orig_node
*orig_node
,
576 u32 seqno
, size_t len
, const u8
*session
,
577 int uid
, u32 timestamp
)
579 struct batadv_icmp_tp_packet
*icmp
;
585 skb
= netdev_alloc_skb_ip_align(NULL
, len
+ ETH_HLEN
);
587 return BATADV_TP_REASON_MEMORY_ERROR
;
589 skb_reserve(skb
, ETH_HLEN
);
590 icmp
= skb_put(skb
, sizeof(*icmp
));
592 /* fill the icmp header */
593 ether_addr_copy(icmp
->dst
, orig_node
->orig
);
594 ether_addr_copy(icmp
->orig
, src
);
595 icmp
->version
= BATADV_COMPAT_VERSION
;
596 icmp
->packet_type
= BATADV_ICMP
;
597 icmp
->ttl
= BATADV_TTL
;
598 icmp
->msg_type
= BATADV_TP
;
601 icmp
->subtype
= BATADV_TP_MSG
;
602 memcpy(icmp
->session
, session
, sizeof(icmp
->session
));
603 icmp
->seqno
= htonl(seqno
);
604 icmp
->timestamp
= htonl(timestamp
);
606 data_len
= len
- sizeof(*icmp
);
607 data
= skb_put(skb
, data_len
);
608 batadv_tp_fill_prerandom(tp_vars
, data
, data_len
);
610 r
= batadv_send_skb_to_orig(skb
, orig_node
, NULL
);
611 if (r
== NET_XMIT_SUCCESS
)
614 return BATADV_TP_REASON_CANT_SEND
;
618 * batadv_tp_recv_ack() - ACK receiving function
619 * @bat_priv: the bat priv with all the soft interface information
620 * @skb: the buffer containing the received packet
622 * Process a received TP ACK packet
624 static void batadv_tp_recv_ack(struct batadv_priv
*bat_priv
,
625 const struct sk_buff
*skb
)
627 struct batadv_hard_iface
*primary_if
= NULL
;
628 struct batadv_orig_node
*orig_node
= NULL
;
629 const struct batadv_icmp_tp_packet
*icmp
;
630 struct batadv_tp_vars
*tp_vars
;
631 size_t packet_len
, mss
;
632 u32 rtt
, recv_ack
, cwnd
;
633 unsigned char *dev_addr
;
635 packet_len
= BATADV_TP_PLEN
;
636 mss
= BATADV_TP_PLEN
;
637 packet_len
+= sizeof(struct batadv_unicast_packet
);
639 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
641 /* find the tp_vars */
642 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
644 if (unlikely(!tp_vars
))
647 if (unlikely(atomic_read(&tp_vars
->sending
) == 0))
650 /* old ACK? silently drop it.. */
651 if (batadv_seq_before(ntohl(icmp
->seqno
),
652 (u32
)atomic_read(&tp_vars
->last_acked
)))
655 primary_if
= batadv_primary_if_get_selected(bat_priv
);
656 if (unlikely(!primary_if
))
659 orig_node
= batadv_orig_hash_find(bat_priv
, icmp
->orig
);
660 if (unlikely(!orig_node
))
663 /* update RTO with the new sampled RTT, if any */
664 rtt
= jiffies_to_msecs(jiffies
) - ntohl(icmp
->timestamp
);
665 if (icmp
->timestamp
&& rtt
)
666 batadv_tp_update_rto(tp_vars
, rtt
);
668 /* ACK for new data... reset the timer */
669 batadv_tp_reset_sender_timer(tp_vars
);
671 recv_ack
= ntohl(icmp
->seqno
);
673 /* check if this ACK is a duplicate */
674 if (atomic_read(&tp_vars
->last_acked
) == recv_ack
) {
675 atomic_inc(&tp_vars
->dup_acks
);
676 if (atomic_read(&tp_vars
->dup_acks
) != 3)
679 if (recv_ack
>= tp_vars
->recover
)
682 /* if this is the third duplicate ACK do Fast Retransmit */
683 batadv_tp_send_msg(tp_vars
, primary_if
->net_dev
->dev_addr
,
684 orig_node
, recv_ack
, packet_len
,
685 icmp
->session
, icmp
->uid
,
686 jiffies_to_msecs(jiffies
));
688 spin_lock_bh(&tp_vars
->cwnd_lock
);
691 tp_vars
->fast_recovery
= true;
692 /* Set recover to the last outstanding seqno when Fast Recovery
693 * is entered. RFC6582, Section 3.2, step 1
695 tp_vars
->recover
= tp_vars
->last_sent
;
696 tp_vars
->ss_threshold
= tp_vars
->cwnd
>> 1;
697 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
698 "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
699 tp_vars
->cwnd
, tp_vars
->ss_threshold
,
700 tp_vars
->last_sent
, recv_ack
);
701 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->ss_threshold
, 3 * mss
,
703 tp_vars
->dec_cwnd
= 0;
704 tp_vars
->last_sent
= recv_ack
;
706 spin_unlock_bh(&tp_vars
->cwnd_lock
);
708 /* count the acked data */
709 atomic64_add(recv_ack
- atomic_read(&tp_vars
->last_acked
),
711 /* reset the duplicate ACKs counter */
712 atomic_set(&tp_vars
->dup_acks
, 0);
714 if (tp_vars
->fast_recovery
) {
716 if (batadv_seq_before(recv_ack
, tp_vars
->recover
)) {
717 /* this is another hole in the window. React
718 * immediately as specified by NewReno (see
719 * Section 3.2 of RFC6582 for details)
721 dev_addr
= primary_if
->net_dev
->dev_addr
;
722 batadv_tp_send_msg(tp_vars
, dev_addr
,
724 packet_len
, icmp
->session
,
726 jiffies_to_msecs(jiffies
));
727 tp_vars
->cwnd
= batadv_tp_cwnd(tp_vars
->cwnd
,
730 tp_vars
->fast_recovery
= false;
731 /* set cwnd to the value of ss_threshold at the
732 * moment that Fast Recovery was entered.
733 * RFC6582, Section 3.2, step 3
735 cwnd
= batadv_tp_cwnd(tp_vars
->ss_threshold
, 0,
737 tp_vars
->cwnd
= cwnd
;
742 if (recv_ack
- atomic_read(&tp_vars
->last_acked
) >= mss
)
743 batadv_tp_update_cwnd(tp_vars
, mss
);
745 /* move the Transmit Window */
746 atomic_set(&tp_vars
->last_acked
, recv_ack
);
749 wake_up(&tp_vars
->more_bytes
);
751 if (likely(primary_if
))
752 batadv_hardif_put(primary_if
);
753 if (likely(orig_node
))
754 batadv_orig_node_put(orig_node
);
756 batadv_tp_vars_put(tp_vars
);
760 * batadv_tp_avail() - check if congestion window is not full
761 * @tp_vars: the private data of the current TP meter session
762 * @payload_len: size of the payload of a single message
764 * Return: true when congestion window is not full, false otherwise
766 static bool batadv_tp_avail(struct batadv_tp_vars
*tp_vars
,
769 u32 win_left
, win_limit
;
771 win_limit
= atomic_read(&tp_vars
->last_acked
) + tp_vars
->cwnd
;
772 win_left
= win_limit
- tp_vars
->last_sent
;
774 return win_left
>= payload_len
;
778 * batadv_tp_wait_available() - wait until congestion window becomes free or
780 * @tp_vars: the private data of the current TP meter session
781 * @plen: size of the payload of a single message
783 * Return: 0 if the condition evaluated to false after the timeout elapsed,
784 * 1 if the condition evaluated to true after the timeout elapsed, the
785 * remaining jiffies (at least 1) if the condition evaluated to true before
786 * the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
788 static int batadv_tp_wait_available(struct batadv_tp_vars
*tp_vars
, size_t plen
)
792 ret
= wait_event_interruptible_timeout(tp_vars
->more_bytes
,
793 batadv_tp_avail(tp_vars
, plen
),
800 * batadv_tp_send() - main sending thread of a tp meter session
801 * @arg: address of the related tp_vars
803 * Return: nothing, this function never returns
805 static int batadv_tp_send(void *arg
)
807 struct batadv_tp_vars
*tp_vars
= arg
;
808 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
809 struct batadv_hard_iface
*primary_if
= NULL
;
810 struct batadv_orig_node
*orig_node
= NULL
;
811 size_t payload_len
, packet_len
;
814 if (unlikely(tp_vars
->role
!= BATADV_TP_SENDER
)) {
815 err
= BATADV_TP_REASON_DST_UNREACHABLE
;
816 tp_vars
->reason
= err
;
820 orig_node
= batadv_orig_hash_find(bat_priv
, tp_vars
->other_end
);
821 if (unlikely(!orig_node
)) {
822 err
= BATADV_TP_REASON_DST_UNREACHABLE
;
823 tp_vars
->reason
= err
;
827 primary_if
= batadv_primary_if_get_selected(bat_priv
);
828 if (unlikely(!primary_if
)) {
829 err
= BATADV_TP_REASON_DST_UNREACHABLE
;
830 tp_vars
->reason
= err
;
834 /* assume that all the hard_interfaces have a correctly
835 * configured MTU, so use the soft_iface MTU as MSS.
836 * This might not be true and in that case the fragmentation
838 * Now, try to send the packet as it is
840 payload_len
= BATADV_TP_PLEN
;
841 BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet
) > BATADV_TP_PLEN
);
843 batadv_tp_reset_sender_timer(tp_vars
);
845 /* queue the worker in charge of terminating the test */
846 queue_delayed_work(batadv_event_workqueue
, &tp_vars
->finish_work
,
847 msecs_to_jiffies(tp_vars
->test_length
));
849 while (atomic_read(&tp_vars
->sending
) != 0) {
850 if (unlikely(!batadv_tp_avail(tp_vars
, payload_len
))) {
851 batadv_tp_wait_available(tp_vars
, payload_len
);
855 /* to emulate normal unicast traffic, add to the payload len
856 * the size of the unicast header
858 packet_len
= payload_len
+ sizeof(struct batadv_unicast_packet
);
860 err
= batadv_tp_send_msg(tp_vars
, primary_if
->net_dev
->dev_addr
,
861 orig_node
, tp_vars
->last_sent
,
863 tp_vars
->session
, tp_vars
->icmp_uid
,
864 jiffies_to_msecs(jiffies
));
866 /* something went wrong during the preparation/transmission */
867 if (unlikely(err
&& err
!= BATADV_TP_REASON_CANT_SEND
)) {
868 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
869 "Meter: %s() cannot send packets (%d)\n",
871 /* ensure nobody else tries to stop the thread now */
872 if (atomic_dec_and_test(&tp_vars
->sending
))
873 tp_vars
->reason
= err
;
877 /* right-shift the TWND */
879 tp_vars
->last_sent
+= payload_len
;
885 if (likely(primary_if
))
886 batadv_hardif_put(primary_if
);
887 if (likely(orig_node
))
888 batadv_orig_node_put(orig_node
);
890 batadv_tp_sender_end(bat_priv
, tp_vars
);
891 batadv_tp_sender_cleanup(bat_priv
, tp_vars
);
893 batadv_tp_vars_put(tp_vars
);
899 * batadv_tp_start_kthread() - start new thread which manages the tp meter
901 * @tp_vars: the private data of the current TP meter session
903 static void batadv_tp_start_kthread(struct batadv_tp_vars
*tp_vars
)
905 struct task_struct
*kthread
;
906 struct batadv_priv
*bat_priv
= tp_vars
->bat_priv
;
909 kref_get(&tp_vars
->refcount
);
910 kthread
= kthread_create(batadv_tp_send
, tp_vars
, "kbatadv_tp_meter");
911 if (IS_ERR(kthread
)) {
912 session_cookie
= batadv_tp_session_cookie(tp_vars
->session
,
914 pr_err("batadv: cannot create tp meter kthread\n");
915 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR
,
917 bat_priv
, session_cookie
);
919 /* drop reserved reference for kthread */
920 batadv_tp_vars_put(tp_vars
);
922 /* cleanup of failed tp meter variables */
923 batadv_tp_sender_cleanup(bat_priv
, tp_vars
);
927 wake_up_process(kthread
);
931 * batadv_tp_start() - start a new tp meter session
932 * @bat_priv: the bat priv with all the soft interface information
933 * @dst: the receiver MAC address
934 * @test_length: test length in milliseconds
935 * @cookie: session cookie
937 void batadv_tp_start(struct batadv_priv
*bat_priv
, const u8
*dst
,
938 u32 test_length
, u32
*cookie
)
940 struct batadv_tp_vars
*tp_vars
;
945 get_random_bytes(session_id
, sizeof(session_id
));
946 get_random_bytes(&icmp_uid
, 1);
947 session_cookie
= batadv_tp_session_cookie(session_id
, icmp_uid
);
948 *cookie
= session_cookie
;
950 /* look for an already existing test towards this node */
951 spin_lock_bh(&bat_priv
->tp_list_lock
);
952 tp_vars
= batadv_tp_list_find(bat_priv
, dst
);
954 spin_unlock_bh(&bat_priv
->tp_list_lock
);
955 batadv_tp_vars_put(tp_vars
);
956 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
957 "Meter: test to or from the same node already ongoing, aborting\n");
958 batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING
,
959 dst
, bat_priv
, session_cookie
);
963 if (!atomic_add_unless(&bat_priv
->tp_num
, 1, BATADV_TP_MAX_NUM
)) {
964 spin_unlock_bh(&bat_priv
->tp_list_lock
);
965 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
966 "Meter: too many ongoing sessions, aborting (SEND)\n");
967 batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY
, dst
,
968 bat_priv
, session_cookie
);
972 tp_vars
= kmalloc(sizeof(*tp_vars
), GFP_ATOMIC
);
974 spin_unlock_bh(&bat_priv
->tp_list_lock
);
975 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
976 "Meter: %s cannot allocate list elements\n",
978 batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR
,
979 dst
, bat_priv
, session_cookie
);
983 /* initialize tp_vars */
984 ether_addr_copy(tp_vars
->other_end
, dst
);
985 kref_init(&tp_vars
->refcount
);
986 tp_vars
->role
= BATADV_TP_SENDER
;
987 atomic_set(&tp_vars
->sending
, 1);
988 memcpy(tp_vars
->session
, session_id
, sizeof(session_id
));
989 tp_vars
->icmp_uid
= icmp_uid
;
991 tp_vars
->last_sent
= BATADV_TP_FIRST_SEQ
;
992 atomic_set(&tp_vars
->last_acked
, BATADV_TP_FIRST_SEQ
);
993 tp_vars
->fast_recovery
= false;
994 tp_vars
->recover
= BATADV_TP_FIRST_SEQ
;
996 /* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
997 * For batman-adv the MSS is the size of the payload received by the
998 * soft_interface, hence its MTU
1000 tp_vars
->cwnd
= BATADV_TP_PLEN
* 3;
1001 /* at the beginning initialise the SS threshold to the biggest possible
1002 * window size, hence the AWND size
1004 tp_vars
->ss_threshold
= BATADV_TP_AWND
;
1006 /* RTO initial value is 3 seconds.
1007 * Details in Section 2.1 of RFC6298
1009 tp_vars
->rto
= 1000;
1011 tp_vars
->rttvar
= 0;
1013 atomic64_set(&tp_vars
->tot_sent
, 0);
1015 kref_get(&tp_vars
->refcount
);
1016 timer_setup(&tp_vars
->timer
, batadv_tp_sender_timeout
, 0);
1018 tp_vars
->bat_priv
= bat_priv
;
1019 tp_vars
->start_time
= jiffies
;
1021 init_waitqueue_head(&tp_vars
->more_bytes
);
1023 spin_lock_init(&tp_vars
->unacked_lock
);
1024 INIT_LIST_HEAD(&tp_vars
->unacked_list
);
1026 spin_lock_init(&tp_vars
->cwnd_lock
);
1028 tp_vars
->prerandom_offset
= 0;
1029 spin_lock_init(&tp_vars
->prerandom_lock
);
1031 kref_get(&tp_vars
->refcount
);
1032 hlist_add_head_rcu(&tp_vars
->list
, &bat_priv
->tp_list
);
1033 spin_unlock_bh(&bat_priv
->tp_list_lock
);
1035 tp_vars
->test_length
= test_length
;
1036 if (!tp_vars
->test_length
)
1037 tp_vars
->test_length
= BATADV_TP_DEF_TEST_LENGTH
;
1039 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1040 "Meter: starting throughput meter towards %pM (length=%ums)\n",
1043 /* init work item for finished tp tests */
1044 INIT_DELAYED_WORK(&tp_vars
->finish_work
, batadv_tp_sender_finish
);
1046 /* start tp kthread. This way the write() call issued from userspace can
1047 * happily return and avoid to block
1049 batadv_tp_start_kthread(tp_vars
);
1051 /* don't return reference to new tp_vars */
1052 batadv_tp_vars_put(tp_vars
);
1056 * batadv_tp_stop() - stop currently running tp meter session
1057 * @bat_priv: the bat priv with all the soft interface information
1058 * @dst: the receiver MAC address
1059 * @return_value: reason for tp meter session stop
1061 void batadv_tp_stop(struct batadv_priv
*bat_priv
, const u8
*dst
,
1064 struct batadv_orig_node
*orig_node
;
1065 struct batadv_tp_vars
*tp_vars
;
1067 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1068 "Meter: stopping test towards %pM\n", dst
);
1070 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
1074 tp_vars
= batadv_tp_list_find(bat_priv
, orig_node
->orig
);
1076 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1077 "Meter: trying to interrupt an already over connection\n");
1081 batadv_tp_sender_shutdown(tp_vars
, return_value
);
1082 batadv_tp_vars_put(tp_vars
);
1084 batadv_orig_node_put(orig_node
);
1088 * batadv_tp_reset_receiver_timer() - reset the receiver shutdown timer
1089 * @tp_vars: the private data of the current TP meter session
1091 * start the receiver shutdown timer or reset it if already started
1093 static void batadv_tp_reset_receiver_timer(struct batadv_tp_vars
*tp_vars
)
1095 mod_timer(&tp_vars
->timer
,
1096 jiffies
+ msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT
));
1100 * batadv_tp_receiver_shutdown() - stop a tp meter receiver when timeout is
1101 * reached without received ack
1102 * @t: address to timer_list inside tp_vars
1104 static void batadv_tp_receiver_shutdown(struct timer_list
*t
)
1106 struct batadv_tp_vars
*tp_vars
= from_timer(tp_vars
, t
, timer
);
1107 struct batadv_tp_unacked
*un
, *safe
;
1108 struct batadv_priv
*bat_priv
;
1110 bat_priv
= tp_vars
->bat_priv
;
1112 /* if there is recent activity rearm the timer */
1113 if (!batadv_has_timed_out(tp_vars
->last_recv_time
,
1114 BATADV_TP_RECV_TIMEOUT
)) {
1115 /* reset the receiver shutdown timer */
1116 batadv_tp_reset_receiver_timer(tp_vars
);
1120 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1121 "Shutting down for inactivity (more than %dms) from %pM\n",
1122 BATADV_TP_RECV_TIMEOUT
, tp_vars
->other_end
);
1124 spin_lock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
1125 hlist_del_rcu(&tp_vars
->list
);
1126 spin_unlock_bh(&tp_vars
->bat_priv
->tp_list_lock
);
1128 /* drop list reference */
1129 batadv_tp_vars_put(tp_vars
);
1131 atomic_dec(&bat_priv
->tp_num
);
1133 spin_lock_bh(&tp_vars
->unacked_lock
);
1134 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
1135 list_del(&un
->list
);
1138 spin_unlock_bh(&tp_vars
->unacked_lock
);
1140 /* drop reference of timer */
1141 batadv_tp_vars_put(tp_vars
);
1145 * batadv_tp_send_ack() - send an ACK packet
1146 * @bat_priv: the bat priv with all the soft interface information
1147 * @dst: the mac address of the destination originator
1148 * @seq: the sequence number to ACK
1149 * @timestamp: the timestamp to echo back in the ACK
1150 * @session: session identifier
1151 * @socket_index: local ICMP socket identifier
1153 * Return: 0 on success, a positive integer representing the reason of the
1156 static int batadv_tp_send_ack(struct batadv_priv
*bat_priv
, const u8
*dst
,
1157 u32 seq
, __be32 timestamp
, const u8
*session
,
1160 struct batadv_hard_iface
*primary_if
= NULL
;
1161 struct batadv_orig_node
*orig_node
;
1162 struct batadv_icmp_tp_packet
*icmp
;
1163 struct sk_buff
*skb
;
1166 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
1167 if (unlikely(!orig_node
)) {
1168 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1172 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1173 if (unlikely(!primary_if
)) {
1174 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1178 skb
= netdev_alloc_skb_ip_align(NULL
, sizeof(*icmp
) + ETH_HLEN
);
1179 if (unlikely(!skb
)) {
1180 ret
= BATADV_TP_REASON_MEMORY_ERROR
;
1184 skb_reserve(skb
, ETH_HLEN
);
1185 icmp
= skb_put(skb
, sizeof(*icmp
));
1186 icmp
->packet_type
= BATADV_ICMP
;
1187 icmp
->version
= BATADV_COMPAT_VERSION
;
1188 icmp
->ttl
= BATADV_TTL
;
1189 icmp
->msg_type
= BATADV_TP
;
1190 ether_addr_copy(icmp
->dst
, orig_node
->orig
);
1191 ether_addr_copy(icmp
->orig
, primary_if
->net_dev
->dev_addr
);
1192 icmp
->uid
= socket_index
;
1194 icmp
->subtype
= BATADV_TP_ACK
;
1195 memcpy(icmp
->session
, session
, sizeof(icmp
->session
));
1196 icmp
->seqno
= htonl(seq
);
1197 icmp
->timestamp
= timestamp
;
1200 r
= batadv_send_skb_to_orig(skb
, orig_node
, NULL
);
1201 if (unlikely(r
< 0) || r
== NET_XMIT_DROP
) {
1202 ret
= BATADV_TP_REASON_DST_UNREACHABLE
;
1208 if (likely(orig_node
))
1209 batadv_orig_node_put(orig_node
);
1210 if (likely(primary_if
))
1211 batadv_hardif_put(primary_if
);
1217 * batadv_tp_handle_out_of_order() - store an out of order packet
1218 * @tp_vars: the private data of the current TP meter session
1219 * @skb: the buffer containing the received packet
1221 * Store the out of order packet in the unacked list for late processing. This
1222 * packets are kept in this list so that they can be ACKed at once as soon as
1223 * all the previous packets have been received
1225 * Return: true if the packed has been successfully processed, false otherwise
1227 static bool batadv_tp_handle_out_of_order(struct batadv_tp_vars
*tp_vars
,
1228 const struct sk_buff
*skb
)
1230 const struct batadv_icmp_tp_packet
*icmp
;
1231 struct batadv_tp_unacked
*un
, *new;
1235 new = kmalloc(sizeof(*new), GFP_ATOMIC
);
1239 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1241 new->seqno
= ntohl(icmp
->seqno
);
1242 payload_len
= skb
->len
- sizeof(struct batadv_unicast_packet
);
1243 new->len
= payload_len
;
1245 spin_lock_bh(&tp_vars
->unacked_lock
);
1246 /* if the list is empty immediately attach this new object */
1247 if (list_empty(&tp_vars
->unacked_list
)) {
1248 list_add(&new->list
, &tp_vars
->unacked_list
);
1252 /* otherwise loop over the list and either drop the packet because this
1253 * is a duplicate or store it at the right position.
1255 * The iteration is done in the reverse way because it is likely that
1256 * the last received packet (the one being processed now) has a bigger
1257 * seqno than all the others already stored.
1259 list_for_each_entry_reverse(un
, &tp_vars
->unacked_list
, list
) {
1260 /* check for duplicates */
1261 if (new->seqno
== un
->seqno
) {
1262 if (new->len
> un
->len
)
1269 /* look for the right position */
1270 if (batadv_seq_before(new->seqno
, un
->seqno
))
1273 /* as soon as an entry having a bigger seqno is found, the new
1274 * one is attached _after_ it. In this way the list is kept in
1277 list_add_tail(&new->list
, &un
->list
);
1282 /* received packet with smallest seqno out of order; add it to front */
1284 list_add(&new->list
, &tp_vars
->unacked_list
);
1287 spin_unlock_bh(&tp_vars
->unacked_lock
);
1293 * batadv_tp_ack_unordered() - update number received bytes in current stream
1295 * @tp_vars: the private data of the current TP meter session
1297 static void batadv_tp_ack_unordered(struct batadv_tp_vars
*tp_vars
)
1299 struct batadv_tp_unacked
*un
, *safe
;
1302 /* go through the unacked packet list and possibly ACK them as
1305 spin_lock_bh(&tp_vars
->unacked_lock
);
1306 list_for_each_entry_safe(un
, safe
, &tp_vars
->unacked_list
, list
) {
1307 /* the list is ordered, therefore it is possible to stop as soon
1308 * there is a gap between the last acked seqno and the seqno of
1309 * the packet under inspection
1311 if (batadv_seq_before(tp_vars
->last_recv
, un
->seqno
))
1314 to_ack
= un
->seqno
+ un
->len
- tp_vars
->last_recv
;
1316 if (batadv_seq_before(tp_vars
->last_recv
, un
->seqno
+ un
->len
))
1317 tp_vars
->last_recv
+= to_ack
;
1319 list_del(&un
->list
);
1322 spin_unlock_bh(&tp_vars
->unacked_lock
);
1326 * batadv_tp_init_recv() - return matching or create new receiver tp_vars
1327 * @bat_priv: the bat priv with all the soft interface information
1328 * @icmp: received icmp tp msg
1330 * Return: corresponding tp_vars or NULL on errors
1332 static struct batadv_tp_vars
*
1333 batadv_tp_init_recv(struct batadv_priv
*bat_priv
,
1334 const struct batadv_icmp_tp_packet
*icmp
)
1336 struct batadv_tp_vars
*tp_vars
;
1338 spin_lock_bh(&bat_priv
->tp_list_lock
);
1339 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
1344 if (!atomic_add_unless(&bat_priv
->tp_num
, 1, BATADV_TP_MAX_NUM
)) {
1345 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1346 "Meter: too many ongoing sessions, aborting (RECV)\n");
1350 tp_vars
= kmalloc(sizeof(*tp_vars
), GFP_ATOMIC
);
1354 ether_addr_copy(tp_vars
->other_end
, icmp
->orig
);
1355 tp_vars
->role
= BATADV_TP_RECEIVER
;
1356 memcpy(tp_vars
->session
, icmp
->session
, sizeof(tp_vars
->session
));
1357 tp_vars
->last_recv
= BATADV_TP_FIRST_SEQ
;
1358 tp_vars
->bat_priv
= bat_priv
;
1359 kref_init(&tp_vars
->refcount
);
1361 spin_lock_init(&tp_vars
->unacked_lock
);
1362 INIT_LIST_HEAD(&tp_vars
->unacked_list
);
1364 kref_get(&tp_vars
->refcount
);
1365 hlist_add_head_rcu(&tp_vars
->list
, &bat_priv
->tp_list
);
1367 kref_get(&tp_vars
->refcount
);
1368 timer_setup(&tp_vars
->timer
, batadv_tp_receiver_shutdown
, 0);
1370 batadv_tp_reset_receiver_timer(tp_vars
);
1373 spin_unlock_bh(&bat_priv
->tp_list_lock
);
1379 * batadv_tp_recv_msg() - process a single data message
1380 * @bat_priv: the bat priv with all the soft interface information
1381 * @skb: the buffer containing the received packet
1383 * Process a received TP MSG packet
1385 static void batadv_tp_recv_msg(struct batadv_priv
*bat_priv
,
1386 const struct sk_buff
*skb
)
1388 const struct batadv_icmp_tp_packet
*icmp
;
1389 struct batadv_tp_vars
*tp_vars
;
1393 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1395 seqno
= ntohl(icmp
->seqno
);
1396 /* check if this is the first seqno. This means that if the
1397 * first packet is lost, the tp meter does not work anymore!
1399 if (seqno
== BATADV_TP_FIRST_SEQ
) {
1400 tp_vars
= batadv_tp_init_recv(bat_priv
, icmp
);
1402 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1403 "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
1407 tp_vars
= batadv_tp_list_find_session(bat_priv
, icmp
->orig
,
1410 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1411 "Unexpected packet from %pM!\n",
1417 if (unlikely(tp_vars
->role
!= BATADV_TP_RECEIVER
)) {
1418 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1419 "Meter: dropping packet: not expected (role=%u)\n",
1424 tp_vars
->last_recv_time
= jiffies
;
1426 /* if the packet is a duplicate, it may be the case that an ACK has been
1427 * lost. Resend the ACK
1429 if (batadv_seq_before(seqno
, tp_vars
->last_recv
))
1432 /* if the packet is out of order enqueue it */
1433 if (ntohl(icmp
->seqno
) != tp_vars
->last_recv
) {
1434 /* exit immediately (and do not send any ACK) if the packet has
1435 * not been enqueued correctly
1437 if (!batadv_tp_handle_out_of_order(tp_vars
, skb
))
1440 /* send a duplicate ACK */
1444 /* if everything was fine count the ACKed bytes */
1445 packet_size
= skb
->len
- sizeof(struct batadv_unicast_packet
);
1446 tp_vars
->last_recv
+= packet_size
;
1448 /* check if this ordered message filled a gap.... */
1449 batadv_tp_ack_unordered(tp_vars
);
1452 /* send the ACK. If the received packet was out of order, the ACK that
1453 * is going to be sent is a duplicate (the sender will count them and
1454 * possibly enter Fast Retransmit as soon as it has reached 3)
1456 batadv_tp_send_ack(bat_priv
, icmp
->orig
, tp_vars
->last_recv
,
1457 icmp
->timestamp
, icmp
->session
, icmp
->uid
);
1459 if (likely(tp_vars
))
1460 batadv_tp_vars_put(tp_vars
);
1464 * batadv_tp_meter_recv() - main TP Meter receiving function
1465 * @bat_priv: the bat priv with all the soft interface information
1466 * @skb: the buffer containing the received packet
1468 void batadv_tp_meter_recv(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
)
1470 struct batadv_icmp_tp_packet
*icmp
;
1472 icmp
= (struct batadv_icmp_tp_packet
*)skb
->data
;
1474 switch (icmp
->subtype
) {
1476 batadv_tp_recv_msg(bat_priv
, skb
);
1479 batadv_tp_recv_ack(bat_priv
, skb
);
1482 batadv_dbg(BATADV_DBG_TP_METER
, bat_priv
,
1483 "Received unknown TP Metric packet type %u\n",
1490 * batadv_tp_meter_init() - initialize global tp_meter structures
1492 void __init
batadv_tp_meter_init(void)
1494 get_random_bytes(batadv_tp_prerandom
, sizeof(batadv_tp_prerandom
));