162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright (C) B.A.T.M.A.N. contributors:
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Edo Monticelli, Antonio Quartulli
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include "tp_meter.h"
862306a36Sopenharmony_ci#include "main.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/atomic.h>
1162306a36Sopenharmony_ci#include <linux/build_bug.h>
1262306a36Sopenharmony_ci#include <linux/byteorder/generic.h>
1362306a36Sopenharmony_ci#include <linux/cache.h>
1462306a36Sopenharmony_ci#include <linux/compiler.h>
1562306a36Sopenharmony_ci#include <linux/container_of.h>
1662306a36Sopenharmony_ci#include <linux/err.h>
1762306a36Sopenharmony_ci#include <linux/etherdevice.h>
1862306a36Sopenharmony_ci#include <linux/gfp.h>
1962306a36Sopenharmony_ci#include <linux/if_ether.h>
2062306a36Sopenharmony_ci#include <linux/init.h>
2162306a36Sopenharmony_ci#include <linux/jiffies.h>
2262306a36Sopenharmony_ci#include <linux/kref.h>
2362306a36Sopenharmony_ci#include <linux/kthread.h>
2462306a36Sopenharmony_ci#include <linux/limits.h>
2562306a36Sopenharmony_ci#include <linux/list.h>
2662306a36Sopenharmony_ci#include <linux/minmax.h>
2762306a36Sopenharmony_ci#include <linux/netdevice.h>
2862306a36Sopenharmony_ci#include <linux/param.h>
2962306a36Sopenharmony_ci#include <linux/printk.h>
3062306a36Sopenharmony_ci#include <linux/random.h>
3162306a36Sopenharmony_ci#include <linux/rculist.h>
3262306a36Sopenharmony_ci#include <linux/rcupdate.h>
3362306a36Sopenharmony_ci#include <linux/sched.h>
3462306a36Sopenharmony_ci#include <linux/skbuff.h>
3562306a36Sopenharmony_ci#include <linux/slab.h>
3662306a36Sopenharmony_ci#include <linux/spinlock.h>
3762306a36Sopenharmony_ci#include <linux/stddef.h>
3862306a36Sopenharmony_ci#include <linux/string.h>
3962306a36Sopenharmony_ci#include <linux/timer.h>
4062306a36Sopenharmony_ci#include <linux/wait.h>
4162306a36Sopenharmony_ci#include <linux/workqueue.h>
4262306a36Sopenharmony_ci#include <uapi/linux/batadv_packet.h>
4362306a36Sopenharmony_ci#include <uapi/linux/batman_adv.h>
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#include "hard-interface.h"
4662306a36Sopenharmony_ci#include "log.h"
4762306a36Sopenharmony_ci#include "netlink.h"
4862306a36Sopenharmony_ci#include "originator.h"
4962306a36Sopenharmony_ci#include "send.h"
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/**
5262306a36Sopenharmony_ci * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
5362306a36Sopenharmony_ci *  in milliseconds
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_ci#define BATADV_TP_DEF_TEST_LENGTH 10000
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/**
5862306a36Sopenharmony_ci * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_ci#define BATADV_TP_AWND 0x20000000
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/**
6362306a36Sopenharmony_ci * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
6462306a36Sopenharmony_ci *  get anything for such amount of milliseconds, the connection is killed
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_ci#define BATADV_TP_RECV_TIMEOUT 1000
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/**
6962306a36Sopenharmony_ci * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
7062306a36Sopenharmony_ci * such amount of milliseconds, the receiver is considered unreachable and the
7162306a36Sopenharmony_ci * connection is killed
7262306a36Sopenharmony_ci */
7362306a36Sopenharmony_ci#define BATADV_TP_MAX_RTO 30000
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/**
7662306a36Sopenharmony_ci * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
7762306a36Sopenharmony_ci *  in order to immediately trigger a wrap around (test purposes)
7862306a36Sopenharmony_ci */
7962306a36Sopenharmony_ci#define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/**
8262306a36Sopenharmony_ci * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
8362306a36Sopenharmony_ci *  to simulate
8462306a36Sopenharmony_ci */
8562306a36Sopenharmony_ci#define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
8662306a36Sopenharmony_ci			sizeof(struct batadv_unicast_packet))
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic u8 batadv_tp_prerandom[4096] __read_mostly;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci/**
9162306a36Sopenharmony_ci * batadv_tp_session_cookie() - generate session cookie based on session ids
9262306a36Sopenharmony_ci * @session: TP session identifier
9362306a36Sopenharmony_ci * @icmp_uid: icmp pseudo uid of the tp session
9462306a36Sopenharmony_ci *
9562306a36Sopenharmony_ci * Return: 32 bit tp_meter session cookie
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_cistatic u32 batadv_tp_session_cookie(const u8 session[2], u8 icmp_uid)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	u32 cookie;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	cookie = icmp_uid << 16;
10262306a36Sopenharmony_ci	cookie |= session[0] << 8;
10362306a36Sopenharmony_ci	cookie |= session[1];
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	return cookie;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/**
10962306a36Sopenharmony_ci * batadv_tp_cwnd() - compute the new cwnd size
11062306a36Sopenharmony_ci * @base: base cwnd size value
11162306a36Sopenharmony_ci * @increment: the value to add to base to get the new size
11262306a36Sopenharmony_ci * @min: minimum cwnd value (usually MSS)
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * Return the new cwnd size and ensure it does not exceed the Advertised
11562306a36Sopenharmony_ci * Receiver Window size. It is wrapped around safely.
11662306a36Sopenharmony_ci * For details refer to Section 3.1 of RFC5681
11762306a36Sopenharmony_ci *
11862306a36Sopenharmony_ci * Return: new congestion window size in bytes
11962306a36Sopenharmony_ci */
12062306a36Sopenharmony_cistatic u32 batadv_tp_cwnd(u32 base, u32 increment, u32 min)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci	u32 new_size = base + increment;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/* check for wrap-around */
12562306a36Sopenharmony_ci	if (new_size < base)
12662306a36Sopenharmony_ci		new_size = (u32)ULONG_MAX;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	new_size = min_t(u32, new_size, BATADV_TP_AWND);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	return max_t(u32, new_size, min);
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci/**
13462306a36Sopenharmony_ci * batadv_tp_update_cwnd() - update the Congestion Windows
13562306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
13662306a36Sopenharmony_ci * @mss: maximum segment size of transmission
13762306a36Sopenharmony_ci *
13862306a36Sopenharmony_ci * 1) if the session is in Slow Start, the CWND has to be increased by 1
13962306a36Sopenharmony_ci * MSS every unique received ACK
14062306a36Sopenharmony_ci * 2) if the session is in Congestion Avoidance, the CWND has to be
14162306a36Sopenharmony_ci * increased by MSS * MSS / CWND for every unique received ACK
14262306a36Sopenharmony_ci */
14362306a36Sopenharmony_cistatic void batadv_tp_update_cwnd(struct batadv_tp_vars *tp_vars, u32 mss)
14462306a36Sopenharmony_ci{
14562306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->cwnd_lock);
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	/* slow start... */
14862306a36Sopenharmony_ci	if (tp_vars->cwnd <= tp_vars->ss_threshold) {
14962306a36Sopenharmony_ci		tp_vars->dec_cwnd = 0;
15062306a36Sopenharmony_ci		tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
15162306a36Sopenharmony_ci		spin_unlock_bh(&tp_vars->cwnd_lock);
15262306a36Sopenharmony_ci		return;
15362306a36Sopenharmony_ci	}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	/* increment CWND at least of 1 (section 3.1 of RFC5681) */
15662306a36Sopenharmony_ci	tp_vars->dec_cwnd += max_t(u32, 1U << 3,
15762306a36Sopenharmony_ci				   ((mss * mss) << 6) / (tp_vars->cwnd << 3));
15862306a36Sopenharmony_ci	if (tp_vars->dec_cwnd < (mss << 3)) {
15962306a36Sopenharmony_ci		spin_unlock_bh(&tp_vars->cwnd_lock);
16062306a36Sopenharmony_ci		return;
16162306a36Sopenharmony_ci	}
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
16462306a36Sopenharmony_ci	tp_vars->dec_cwnd = 0;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->cwnd_lock);
16762306a36Sopenharmony_ci}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/**
17062306a36Sopenharmony_ci * batadv_tp_update_rto() - calculate new retransmission timeout
17162306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
17262306a36Sopenharmony_ci * @new_rtt: new roundtrip time in msec
17362306a36Sopenharmony_ci */
17462306a36Sopenharmony_cistatic void batadv_tp_update_rto(struct batadv_tp_vars *tp_vars,
17562306a36Sopenharmony_ci				 u32 new_rtt)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	long m = new_rtt;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	/* RTT update
18062306a36Sopenharmony_ci	 * Details in Section 2.2 and 2.3 of RFC6298
18162306a36Sopenharmony_ci	 *
18262306a36Sopenharmony_ci	 * It's tricky to understand. Don't lose hair please.
18362306a36Sopenharmony_ci	 * Inspired by tcp_rtt_estimator() tcp_input.c
18462306a36Sopenharmony_ci	 */
18562306a36Sopenharmony_ci	if (tp_vars->srtt != 0) {
18662306a36Sopenharmony_ci		m -= (tp_vars->srtt >> 3); /* m is now error in rtt est */
18762306a36Sopenharmony_ci		tp_vars->srtt += m; /* rtt = 7/8 srtt + 1/8 new */
18862306a36Sopenharmony_ci		if (m < 0)
18962306a36Sopenharmony_ci			m = -m;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci		m -= (tp_vars->rttvar >> 2);
19262306a36Sopenharmony_ci		tp_vars->rttvar += m; /* mdev ~= 3/4 rttvar + 1/4 new */
19362306a36Sopenharmony_ci	} else {
19462306a36Sopenharmony_ci		/* first measure getting in */
19562306a36Sopenharmony_ci		tp_vars->srtt = m << 3;	/* take the measured time to be srtt */
19662306a36Sopenharmony_ci		tp_vars->rttvar = m << 1; /* new_rtt / 2 */
19762306a36Sopenharmony_ci	}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	/* rto = srtt + 4 * rttvar.
20062306a36Sopenharmony_ci	 * rttvar is scaled by 4, therefore doesn't need to be multiplied
20162306a36Sopenharmony_ci	 */
20262306a36Sopenharmony_ci	tp_vars->rto = (tp_vars->srtt >> 3) + tp_vars->rttvar;
20362306a36Sopenharmony_ci}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci/**
20662306a36Sopenharmony_ci * batadv_tp_batctl_notify() - send client status result to client
20762306a36Sopenharmony_ci * @reason: reason for tp meter session stop
20862306a36Sopenharmony_ci * @dst: destination of tp_meter session
20962306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
21062306a36Sopenharmony_ci * @start_time: start of transmission in jiffies
21162306a36Sopenharmony_ci * @total_sent: bytes acked to the receiver
21262306a36Sopenharmony_ci * @cookie: cookie of tp_meter session
21362306a36Sopenharmony_ci */
21462306a36Sopenharmony_cistatic void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason,
21562306a36Sopenharmony_ci				    const u8 *dst, struct batadv_priv *bat_priv,
21662306a36Sopenharmony_ci				    unsigned long start_time, u64 total_sent,
21762306a36Sopenharmony_ci				    u32 cookie)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	u32 test_time;
22062306a36Sopenharmony_ci	u8 result;
22162306a36Sopenharmony_ci	u32 total_bytes;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	if (!batadv_tp_is_error(reason)) {
22462306a36Sopenharmony_ci		result = BATADV_TP_REASON_COMPLETE;
22562306a36Sopenharmony_ci		test_time = jiffies_to_msecs(jiffies - start_time);
22662306a36Sopenharmony_ci		total_bytes = total_sent;
22762306a36Sopenharmony_ci	} else {
22862306a36Sopenharmony_ci		result = reason;
22962306a36Sopenharmony_ci		test_time = 0;
23062306a36Sopenharmony_ci		total_bytes = 0;
23162306a36Sopenharmony_ci	}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	batadv_netlink_tpmeter_notify(bat_priv, dst, result, test_time,
23462306a36Sopenharmony_ci				      total_bytes, cookie);
23562306a36Sopenharmony_ci}
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci/**
23862306a36Sopenharmony_ci * batadv_tp_batctl_error_notify() - send client error result to client
23962306a36Sopenharmony_ci * @reason: reason for tp meter session stop
24062306a36Sopenharmony_ci * @dst: destination of tp_meter session
24162306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
24262306a36Sopenharmony_ci * @cookie: cookie of tp_meter session
24362306a36Sopenharmony_ci */
24462306a36Sopenharmony_cistatic void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason,
24562306a36Sopenharmony_ci					  const u8 *dst,
24662306a36Sopenharmony_ci					  struct batadv_priv *bat_priv,
24762306a36Sopenharmony_ci					  u32 cookie)
24862306a36Sopenharmony_ci{
24962306a36Sopenharmony_ci	batadv_tp_batctl_notify(reason, dst, bat_priv, 0, 0, cookie);
25062306a36Sopenharmony_ci}
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci/**
25362306a36Sopenharmony_ci * batadv_tp_list_find() - find a tp_vars object in the global list
25462306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
25562306a36Sopenharmony_ci * @dst: the other endpoint MAC address to look for
25662306a36Sopenharmony_ci *
25762306a36Sopenharmony_ci * Look for a tp_vars object matching dst as end_point and return it after
25862306a36Sopenharmony_ci * having increment the refcounter. Return NULL is not found
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci * Return: matching tp_vars or NULL when no tp_vars with @dst was found
26162306a36Sopenharmony_ci */
26262306a36Sopenharmony_cistatic struct batadv_tp_vars *batadv_tp_list_find(struct batadv_priv *bat_priv,
26362306a36Sopenharmony_ci						  const u8 *dst)
26462306a36Sopenharmony_ci{
26562306a36Sopenharmony_ci	struct batadv_tp_vars *pos, *tp_vars = NULL;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	rcu_read_lock();
26862306a36Sopenharmony_ci	hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
26962306a36Sopenharmony_ci		if (!batadv_compare_eth(pos->other_end, dst))
27062306a36Sopenharmony_ci			continue;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci		/* most of the time this function is invoked during the normal
27362306a36Sopenharmony_ci		 * process..it makes sens to pay more when the session is
27462306a36Sopenharmony_ci		 * finished and to speed the process up during the measurement
27562306a36Sopenharmony_ci		 */
27662306a36Sopenharmony_ci		if (unlikely(!kref_get_unless_zero(&pos->refcount)))
27762306a36Sopenharmony_ci			continue;
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci		tp_vars = pos;
28062306a36Sopenharmony_ci		break;
28162306a36Sopenharmony_ci	}
28262306a36Sopenharmony_ci	rcu_read_unlock();
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	return tp_vars;
28562306a36Sopenharmony_ci}
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci/**
28862306a36Sopenharmony_ci * batadv_tp_list_find_session() - find tp_vars session object in the global
28962306a36Sopenharmony_ci *  list
29062306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
29162306a36Sopenharmony_ci * @dst: the other endpoint MAC address to look for
29262306a36Sopenharmony_ci * @session: session identifier
29362306a36Sopenharmony_ci *
29462306a36Sopenharmony_ci * Look for a tp_vars object matching dst as end_point, session as tp meter
29562306a36Sopenharmony_ci * session and return it after having increment the refcounter. Return NULL
29662306a36Sopenharmony_ci * is not found
29762306a36Sopenharmony_ci *
29862306a36Sopenharmony_ci * Return: matching tp_vars or NULL when no tp_vars was found
29962306a36Sopenharmony_ci */
30062306a36Sopenharmony_cistatic struct batadv_tp_vars *
30162306a36Sopenharmony_cibatadv_tp_list_find_session(struct batadv_priv *bat_priv, const u8 *dst,
30262306a36Sopenharmony_ci			    const u8 *session)
30362306a36Sopenharmony_ci{
30462306a36Sopenharmony_ci	struct batadv_tp_vars *pos, *tp_vars = NULL;
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	rcu_read_lock();
30762306a36Sopenharmony_ci	hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
30862306a36Sopenharmony_ci		if (!batadv_compare_eth(pos->other_end, dst))
30962306a36Sopenharmony_ci			continue;
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci		if (memcmp(pos->session, session, sizeof(pos->session)) != 0)
31262306a36Sopenharmony_ci			continue;
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci		/* most of the time this function is invoked during the normal
31562306a36Sopenharmony_ci		 * process..it makes sense to pay more when the session is
31662306a36Sopenharmony_ci		 * finished and to speed the process up during the measurement
31762306a36Sopenharmony_ci		 */
31862306a36Sopenharmony_ci		if (unlikely(!kref_get_unless_zero(&pos->refcount)))
31962306a36Sopenharmony_ci			continue;
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci		tp_vars = pos;
32262306a36Sopenharmony_ci		break;
32362306a36Sopenharmony_ci	}
32462306a36Sopenharmony_ci	rcu_read_unlock();
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	return tp_vars;
32762306a36Sopenharmony_ci}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci/**
33062306a36Sopenharmony_ci * batadv_tp_vars_release() - release batadv_tp_vars from lists and queue for
33162306a36Sopenharmony_ci *  free after rcu grace period
33262306a36Sopenharmony_ci * @ref: kref pointer of the batadv_tp_vars
33362306a36Sopenharmony_ci */
33462306a36Sopenharmony_cistatic void batadv_tp_vars_release(struct kref *ref)
33562306a36Sopenharmony_ci{
33662306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
33762306a36Sopenharmony_ci	struct batadv_tp_unacked *un, *safe;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	tp_vars = container_of(ref, struct batadv_tp_vars, refcount);
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	/* lock should not be needed because this object is now out of any
34262306a36Sopenharmony_ci	 * context!
34362306a36Sopenharmony_ci	 */
34462306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
34562306a36Sopenharmony_ci	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
34662306a36Sopenharmony_ci		list_del(&un->list);
34762306a36Sopenharmony_ci		kfree(un);
34862306a36Sopenharmony_ci	}
34962306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	kfree_rcu(tp_vars, rcu);
35262306a36Sopenharmony_ci}
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci/**
35562306a36Sopenharmony_ci * batadv_tp_vars_put() - decrement the batadv_tp_vars refcounter and possibly
35662306a36Sopenharmony_ci *  release it
35762306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session to be free'd
35862306a36Sopenharmony_ci */
35962306a36Sopenharmony_cistatic void batadv_tp_vars_put(struct batadv_tp_vars *tp_vars)
36062306a36Sopenharmony_ci{
36162306a36Sopenharmony_ci	if (!tp_vars)
36262306a36Sopenharmony_ci		return;
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	kref_put(&tp_vars->refcount, batadv_tp_vars_release);
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci/**
36862306a36Sopenharmony_ci * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer
36962306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
37062306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session to cleanup
37162306a36Sopenharmony_ci */
37262306a36Sopenharmony_cistatic void batadv_tp_sender_cleanup(struct batadv_priv *bat_priv,
37362306a36Sopenharmony_ci				     struct batadv_tp_vars *tp_vars)
37462306a36Sopenharmony_ci{
37562306a36Sopenharmony_ci	cancel_delayed_work(&tp_vars->finish_work);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
37862306a36Sopenharmony_ci	hlist_del_rcu(&tp_vars->list);
37962306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	/* drop list reference */
38262306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	atomic_dec(&tp_vars->bat_priv->tp_num);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	/* kill the timer and remove its reference */
38762306a36Sopenharmony_ci	del_timer_sync(&tp_vars->timer);
38862306a36Sopenharmony_ci	/* the worker might have rearmed itself therefore we kill it again. Note
38962306a36Sopenharmony_ci	 * that if the worker should run again before invoking the following
39062306a36Sopenharmony_ci	 * del_timer(), it would not re-arm itself once again because the status
39162306a36Sopenharmony_ci	 * is OFF now
39262306a36Sopenharmony_ci	 */
39362306a36Sopenharmony_ci	del_timer(&tp_vars->timer);
39462306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
39562306a36Sopenharmony_ci}
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci/**
39862306a36Sopenharmony_ci * batadv_tp_sender_end() - print info about ended session and inform client
39962306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
40062306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
40162306a36Sopenharmony_ci */
40262306a36Sopenharmony_cistatic void batadv_tp_sender_end(struct batadv_priv *bat_priv,
40362306a36Sopenharmony_ci				 struct batadv_tp_vars *tp_vars)
40462306a36Sopenharmony_ci{
40562306a36Sopenharmony_ci	u32 session_cookie;
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
40862306a36Sopenharmony_ci		   "Test towards %pM finished..shutting down (reason=%d)\n",
40962306a36Sopenharmony_ci		   tp_vars->other_end, tp_vars->reason);
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
41262306a36Sopenharmony_ci		   "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
41362306a36Sopenharmony_ci		   tp_vars->srtt >> 3, tp_vars->rttvar >> 2, tp_vars->rto);
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
41662306a36Sopenharmony_ci		   "Final values: cwnd=%u ss_threshold=%u\n",
41762306a36Sopenharmony_ci		   tp_vars->cwnd, tp_vars->ss_threshold);
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	session_cookie = batadv_tp_session_cookie(tp_vars->session,
42062306a36Sopenharmony_ci						  tp_vars->icmp_uid);
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci	batadv_tp_batctl_notify(tp_vars->reason,
42362306a36Sopenharmony_ci				tp_vars->other_end,
42462306a36Sopenharmony_ci				bat_priv,
42562306a36Sopenharmony_ci				tp_vars->start_time,
42662306a36Sopenharmony_ci				atomic64_read(&tp_vars->tot_sent),
42762306a36Sopenharmony_ci				session_cookie);
42862306a36Sopenharmony_ci}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci/**
43162306a36Sopenharmony_ci * batadv_tp_sender_shutdown() - let sender thread/timer stop gracefully
43262306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
43362306a36Sopenharmony_ci * @reason: reason for tp meter session stop
43462306a36Sopenharmony_ci */
43562306a36Sopenharmony_cistatic void batadv_tp_sender_shutdown(struct batadv_tp_vars *tp_vars,
43662306a36Sopenharmony_ci				      enum batadv_tp_meter_reason reason)
43762306a36Sopenharmony_ci{
43862306a36Sopenharmony_ci	if (!atomic_dec_and_test(&tp_vars->sending))
43962306a36Sopenharmony_ci		return;
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci	tp_vars->reason = reason;
44262306a36Sopenharmony_ci}
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci/**
44562306a36Sopenharmony_ci * batadv_tp_sender_finish() - stop sender session after test_length was reached
44662306a36Sopenharmony_ci * @work: delayed work reference of the related tp_vars
44762306a36Sopenharmony_ci */
44862306a36Sopenharmony_cistatic void batadv_tp_sender_finish(struct work_struct *work)
44962306a36Sopenharmony_ci{
45062306a36Sopenharmony_ci	struct delayed_work *delayed_work;
45162306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	delayed_work = to_delayed_work(work);
45462306a36Sopenharmony_ci	tp_vars = container_of(delayed_work, struct batadv_tp_vars,
45562306a36Sopenharmony_ci			       finish_work);
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	batadv_tp_sender_shutdown(tp_vars, BATADV_TP_REASON_COMPLETE);
45862306a36Sopenharmony_ci}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci/**
46162306a36Sopenharmony_ci * batadv_tp_reset_sender_timer() - reschedule the sender timer
46262306a36Sopenharmony_ci * @tp_vars: the private TP meter data for this session
46362306a36Sopenharmony_ci *
46462306a36Sopenharmony_ci * Reschedule the timer using tp_vars->rto as delay
46562306a36Sopenharmony_ci */
46662306a36Sopenharmony_cistatic void batadv_tp_reset_sender_timer(struct batadv_tp_vars *tp_vars)
46762306a36Sopenharmony_ci{
46862306a36Sopenharmony_ci	/* most of the time this function is invoked while normal packet
46962306a36Sopenharmony_ci	 * reception...
47062306a36Sopenharmony_ci	 */
47162306a36Sopenharmony_ci	if (unlikely(atomic_read(&tp_vars->sending) == 0))
47262306a36Sopenharmony_ci		/* timer ref will be dropped in batadv_tp_sender_cleanup */
47362306a36Sopenharmony_ci		return;
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	mod_timer(&tp_vars->timer, jiffies + msecs_to_jiffies(tp_vars->rto));
47662306a36Sopenharmony_ci}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci/**
47962306a36Sopenharmony_ci * batadv_tp_sender_timeout() - timer that fires in case of packet loss
48062306a36Sopenharmony_ci * @t: address to timer_list inside tp_vars
48162306a36Sopenharmony_ci *
48262306a36Sopenharmony_ci * If fired it means that there was packet loss.
48362306a36Sopenharmony_ci * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
48462306a36Sopenharmony_ci * reset the cwnd to 3*MSS
48562306a36Sopenharmony_ci */
48662306a36Sopenharmony_cistatic void batadv_tp_sender_timeout(struct timer_list *t)
48762306a36Sopenharmony_ci{
48862306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
48962306a36Sopenharmony_ci	struct batadv_priv *bat_priv = tp_vars->bat_priv;
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	if (atomic_read(&tp_vars->sending) == 0)
49262306a36Sopenharmony_ci		return;
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	/* if the user waited long enough...shutdown the test */
49562306a36Sopenharmony_ci	if (unlikely(tp_vars->rto >= BATADV_TP_MAX_RTO)) {
49662306a36Sopenharmony_ci		batadv_tp_sender_shutdown(tp_vars,
49762306a36Sopenharmony_ci					  BATADV_TP_REASON_DST_UNREACHABLE);
49862306a36Sopenharmony_ci		return;
49962306a36Sopenharmony_ci	}
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci	/* RTO exponential backoff
50262306a36Sopenharmony_ci	 * Details in Section 5.5 of RFC6298
50362306a36Sopenharmony_ci	 */
50462306a36Sopenharmony_ci	tp_vars->rto <<= 1;
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->cwnd_lock);
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	tp_vars->ss_threshold = tp_vars->cwnd >> 1;
50962306a36Sopenharmony_ci	if (tp_vars->ss_threshold < BATADV_TP_PLEN * 2)
51062306a36Sopenharmony_ci		tp_vars->ss_threshold = BATADV_TP_PLEN * 2;
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
51362306a36Sopenharmony_ci		   "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
51462306a36Sopenharmony_ci		   tp_vars->other_end, tp_vars->cwnd, tp_vars->ss_threshold,
51562306a36Sopenharmony_ci		   atomic_read(&tp_vars->last_acked));
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	tp_vars->cwnd = BATADV_TP_PLEN * 3;
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->cwnd_lock);
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	/* resend the non-ACKed packets.. */
52262306a36Sopenharmony_ci	tp_vars->last_sent = atomic_read(&tp_vars->last_acked);
52362306a36Sopenharmony_ci	wake_up(&tp_vars->more_bytes);
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	batadv_tp_reset_sender_timer(tp_vars);
52662306a36Sopenharmony_ci}
52762306a36Sopenharmony_ci
52862306a36Sopenharmony_ci/**
52962306a36Sopenharmony_ci * batadv_tp_fill_prerandom() - Fill buffer with prefetched random bytes
53062306a36Sopenharmony_ci * @tp_vars: the private TP meter data for this session
53162306a36Sopenharmony_ci * @buf: Buffer to fill with bytes
53262306a36Sopenharmony_ci * @nbytes: amount of pseudorandom bytes
53362306a36Sopenharmony_ci */
53462306a36Sopenharmony_cistatic void batadv_tp_fill_prerandom(struct batadv_tp_vars *tp_vars,
53562306a36Sopenharmony_ci				     u8 *buf, size_t nbytes)
53662306a36Sopenharmony_ci{
53762306a36Sopenharmony_ci	u32 local_offset;
53862306a36Sopenharmony_ci	size_t bytes_inbuf;
53962306a36Sopenharmony_ci	size_t to_copy;
54062306a36Sopenharmony_ci	size_t pos = 0;
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->prerandom_lock);
54362306a36Sopenharmony_ci	local_offset = tp_vars->prerandom_offset;
54462306a36Sopenharmony_ci	tp_vars->prerandom_offset += nbytes;
54562306a36Sopenharmony_ci	tp_vars->prerandom_offset %= sizeof(batadv_tp_prerandom);
54662306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->prerandom_lock);
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci	while (nbytes) {
54962306a36Sopenharmony_ci		local_offset %= sizeof(batadv_tp_prerandom);
55062306a36Sopenharmony_ci		bytes_inbuf = sizeof(batadv_tp_prerandom) - local_offset;
55162306a36Sopenharmony_ci		to_copy = min(nbytes, bytes_inbuf);
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci		memcpy(&buf[pos], &batadv_tp_prerandom[local_offset], to_copy);
55462306a36Sopenharmony_ci		pos += to_copy;
55562306a36Sopenharmony_ci		nbytes -= to_copy;
55662306a36Sopenharmony_ci		local_offset = 0;
55762306a36Sopenharmony_ci	}
55862306a36Sopenharmony_ci}
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci/**
56162306a36Sopenharmony_ci * batadv_tp_send_msg() - send a single message
56262306a36Sopenharmony_ci * @tp_vars: the private TP meter data for this session
56362306a36Sopenharmony_ci * @src: source mac address
56462306a36Sopenharmony_ci * @orig_node: the originator of the destination
56562306a36Sopenharmony_ci * @seqno: sequence number of this packet
56662306a36Sopenharmony_ci * @len: length of the entire packet
56762306a36Sopenharmony_ci * @session: session identifier
56862306a36Sopenharmony_ci * @uid: local ICMP "socket" index
56962306a36Sopenharmony_ci * @timestamp: timestamp in jiffies which is replied in ack
57062306a36Sopenharmony_ci *
57162306a36Sopenharmony_ci * Create and send a single TP Meter message.
57262306a36Sopenharmony_ci *
57362306a36Sopenharmony_ci * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
57462306a36Sopenharmony_ci * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
57562306a36Sopenharmony_ci * allocated
57662306a36Sopenharmony_ci */
57762306a36Sopenharmony_cistatic int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src,
57862306a36Sopenharmony_ci			      struct batadv_orig_node *orig_node,
57962306a36Sopenharmony_ci			      u32 seqno, size_t len, const u8 *session,
58062306a36Sopenharmony_ci			      int uid, u32 timestamp)
58162306a36Sopenharmony_ci{
58262306a36Sopenharmony_ci	struct batadv_icmp_tp_packet *icmp;
58362306a36Sopenharmony_ci	struct sk_buff *skb;
58462306a36Sopenharmony_ci	int r;
58562306a36Sopenharmony_ci	u8 *data;
58662306a36Sopenharmony_ci	size_t data_len;
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
58962306a36Sopenharmony_ci	if (unlikely(!skb))
59062306a36Sopenharmony_ci		return BATADV_TP_REASON_MEMORY_ERROR;
59162306a36Sopenharmony_ci
59262306a36Sopenharmony_ci	skb_reserve(skb, ETH_HLEN);
59362306a36Sopenharmony_ci	icmp = skb_put(skb, sizeof(*icmp));
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci	/* fill the icmp header */
59662306a36Sopenharmony_ci	ether_addr_copy(icmp->dst, orig_node->orig);
59762306a36Sopenharmony_ci	ether_addr_copy(icmp->orig, src);
59862306a36Sopenharmony_ci	icmp->version = BATADV_COMPAT_VERSION;
59962306a36Sopenharmony_ci	icmp->packet_type = BATADV_ICMP;
60062306a36Sopenharmony_ci	icmp->ttl = BATADV_TTL;
60162306a36Sopenharmony_ci	icmp->msg_type = BATADV_TP;
60262306a36Sopenharmony_ci	icmp->uid = uid;
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci	icmp->subtype = BATADV_TP_MSG;
60562306a36Sopenharmony_ci	memcpy(icmp->session, session, sizeof(icmp->session));
60662306a36Sopenharmony_ci	icmp->seqno = htonl(seqno);
60762306a36Sopenharmony_ci	icmp->timestamp = htonl(timestamp);
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci	data_len = len - sizeof(*icmp);
61062306a36Sopenharmony_ci	data = skb_put(skb, data_len);
61162306a36Sopenharmony_ci	batadv_tp_fill_prerandom(tp_vars, data, data_len);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci	r = batadv_send_skb_to_orig(skb, orig_node, NULL);
61462306a36Sopenharmony_ci	if (r == NET_XMIT_SUCCESS)
61562306a36Sopenharmony_ci		return 0;
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_ci	return BATADV_TP_REASON_CANT_SEND;
61862306a36Sopenharmony_ci}
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci/**
62162306a36Sopenharmony_ci * batadv_tp_recv_ack() - ACK receiving function
62262306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
62362306a36Sopenharmony_ci * @skb: the buffer containing the received packet
62462306a36Sopenharmony_ci *
62562306a36Sopenharmony_ci * Process a received TP ACK packet
62662306a36Sopenharmony_ci */
62762306a36Sopenharmony_cistatic void batadv_tp_recv_ack(struct batadv_priv *bat_priv,
62862306a36Sopenharmony_ci			       const struct sk_buff *skb)
62962306a36Sopenharmony_ci{
63062306a36Sopenharmony_ci	struct batadv_hard_iface *primary_if = NULL;
63162306a36Sopenharmony_ci	struct batadv_orig_node *orig_node = NULL;
63262306a36Sopenharmony_ci	const struct batadv_icmp_tp_packet *icmp;
63362306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
63462306a36Sopenharmony_ci	const unsigned char *dev_addr;
63562306a36Sopenharmony_ci	size_t packet_len, mss;
63662306a36Sopenharmony_ci	u32 rtt, recv_ack, cwnd;
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	packet_len = BATADV_TP_PLEN;
63962306a36Sopenharmony_ci	mss = BATADV_TP_PLEN;
64062306a36Sopenharmony_ci	packet_len += sizeof(struct batadv_unicast_packet);
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci	/* find the tp_vars */
64562306a36Sopenharmony_ci	tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
64662306a36Sopenharmony_ci					      icmp->session);
64762306a36Sopenharmony_ci	if (unlikely(!tp_vars))
64862306a36Sopenharmony_ci		return;
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ci	if (unlikely(atomic_read(&tp_vars->sending) == 0))
65162306a36Sopenharmony_ci		goto out;
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ci	/* old ACK? silently drop it.. */
65462306a36Sopenharmony_ci	if (batadv_seq_before(ntohl(icmp->seqno),
65562306a36Sopenharmony_ci			      (u32)atomic_read(&tp_vars->last_acked)))
65662306a36Sopenharmony_ci		goto out;
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
65962306a36Sopenharmony_ci	if (unlikely(!primary_if))
66062306a36Sopenharmony_ci		goto out;
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, icmp->orig);
66362306a36Sopenharmony_ci	if (unlikely(!orig_node))
66462306a36Sopenharmony_ci		goto out;
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ci	/* update RTO with the new sampled RTT, if any */
66762306a36Sopenharmony_ci	rtt = jiffies_to_msecs(jiffies) - ntohl(icmp->timestamp);
66862306a36Sopenharmony_ci	if (icmp->timestamp && rtt)
66962306a36Sopenharmony_ci		batadv_tp_update_rto(tp_vars, rtt);
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci	/* ACK for new data... reset the timer */
67262306a36Sopenharmony_ci	batadv_tp_reset_sender_timer(tp_vars);
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_ci	recv_ack = ntohl(icmp->seqno);
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci	/* check if this ACK is a duplicate */
67762306a36Sopenharmony_ci	if (atomic_read(&tp_vars->last_acked) == recv_ack) {
67862306a36Sopenharmony_ci		atomic_inc(&tp_vars->dup_acks);
67962306a36Sopenharmony_ci		if (atomic_read(&tp_vars->dup_acks) != 3)
68062306a36Sopenharmony_ci			goto out;
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci		if (recv_ack >= tp_vars->recover)
68362306a36Sopenharmony_ci			goto out;
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci		/* if this is the third duplicate ACK do Fast Retransmit */
68662306a36Sopenharmony_ci		batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
68762306a36Sopenharmony_ci				   orig_node, recv_ack, packet_len,
68862306a36Sopenharmony_ci				   icmp->session, icmp->uid,
68962306a36Sopenharmony_ci				   jiffies_to_msecs(jiffies));
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci		spin_lock_bh(&tp_vars->cwnd_lock);
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci		/* Fast Recovery */
69462306a36Sopenharmony_ci		tp_vars->fast_recovery = true;
69562306a36Sopenharmony_ci		/* Set recover to the last outstanding seqno when Fast Recovery
69662306a36Sopenharmony_ci		 * is entered. RFC6582, Section 3.2, step 1
69762306a36Sopenharmony_ci		 */
69862306a36Sopenharmony_ci		tp_vars->recover = tp_vars->last_sent;
69962306a36Sopenharmony_ci		tp_vars->ss_threshold = tp_vars->cwnd >> 1;
70062306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
70162306a36Sopenharmony_ci			   "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
70262306a36Sopenharmony_ci			   tp_vars->cwnd, tp_vars->ss_threshold,
70362306a36Sopenharmony_ci			   tp_vars->last_sent, recv_ack);
70462306a36Sopenharmony_ci		tp_vars->cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 3 * mss,
70562306a36Sopenharmony_ci					       mss);
70662306a36Sopenharmony_ci		tp_vars->dec_cwnd = 0;
70762306a36Sopenharmony_ci		tp_vars->last_sent = recv_ack;
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci		spin_unlock_bh(&tp_vars->cwnd_lock);
71062306a36Sopenharmony_ci	} else {
71162306a36Sopenharmony_ci		/* count the acked data */
71262306a36Sopenharmony_ci		atomic64_add(recv_ack - atomic_read(&tp_vars->last_acked),
71362306a36Sopenharmony_ci			     &tp_vars->tot_sent);
71462306a36Sopenharmony_ci		/* reset the duplicate ACKs counter */
71562306a36Sopenharmony_ci		atomic_set(&tp_vars->dup_acks, 0);
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci		if (tp_vars->fast_recovery) {
71862306a36Sopenharmony_ci			/* partial ACK */
71962306a36Sopenharmony_ci			if (batadv_seq_before(recv_ack, tp_vars->recover)) {
72062306a36Sopenharmony_ci				/* this is another hole in the window. React
72162306a36Sopenharmony_ci				 * immediately as specified by NewReno (see
72262306a36Sopenharmony_ci				 * Section 3.2 of RFC6582 for details)
72362306a36Sopenharmony_ci				 */
72462306a36Sopenharmony_ci				dev_addr = primary_if->net_dev->dev_addr;
72562306a36Sopenharmony_ci				batadv_tp_send_msg(tp_vars, dev_addr,
72662306a36Sopenharmony_ci						   orig_node, recv_ack,
72762306a36Sopenharmony_ci						   packet_len, icmp->session,
72862306a36Sopenharmony_ci						   icmp->uid,
72962306a36Sopenharmony_ci						   jiffies_to_msecs(jiffies));
73062306a36Sopenharmony_ci				tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd,
73162306a36Sopenharmony_ci							       mss, mss);
73262306a36Sopenharmony_ci			} else {
73362306a36Sopenharmony_ci				tp_vars->fast_recovery = false;
73462306a36Sopenharmony_ci				/* set cwnd to the value of ss_threshold at the
73562306a36Sopenharmony_ci				 * moment that Fast Recovery was entered.
73662306a36Sopenharmony_ci				 * RFC6582, Section 3.2, step 3
73762306a36Sopenharmony_ci				 */
73862306a36Sopenharmony_ci				cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 0,
73962306a36Sopenharmony_ci						      mss);
74062306a36Sopenharmony_ci				tp_vars->cwnd = cwnd;
74162306a36Sopenharmony_ci			}
74262306a36Sopenharmony_ci			goto move_twnd;
74362306a36Sopenharmony_ci		}
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci		if (recv_ack - atomic_read(&tp_vars->last_acked) >= mss)
74662306a36Sopenharmony_ci			batadv_tp_update_cwnd(tp_vars, mss);
74762306a36Sopenharmony_cimove_twnd:
74862306a36Sopenharmony_ci		/* move the Transmit Window */
74962306a36Sopenharmony_ci		atomic_set(&tp_vars->last_acked, recv_ack);
75062306a36Sopenharmony_ci	}
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci	wake_up(&tp_vars->more_bytes);
75362306a36Sopenharmony_ciout:
75462306a36Sopenharmony_ci	batadv_hardif_put(primary_if);
75562306a36Sopenharmony_ci	batadv_orig_node_put(orig_node);
75662306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
75762306a36Sopenharmony_ci}
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci/**
76062306a36Sopenharmony_ci * batadv_tp_avail() - check if congestion window is not full
76162306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
76262306a36Sopenharmony_ci * @payload_len: size of the payload of a single message
76362306a36Sopenharmony_ci *
76462306a36Sopenharmony_ci * Return: true when congestion window is not full, false otherwise
76562306a36Sopenharmony_ci */
76662306a36Sopenharmony_cistatic bool batadv_tp_avail(struct batadv_tp_vars *tp_vars,
76762306a36Sopenharmony_ci			    size_t payload_len)
76862306a36Sopenharmony_ci{
76962306a36Sopenharmony_ci	u32 win_left, win_limit;
77062306a36Sopenharmony_ci
77162306a36Sopenharmony_ci	win_limit = atomic_read(&tp_vars->last_acked) + tp_vars->cwnd;
77262306a36Sopenharmony_ci	win_left = win_limit - tp_vars->last_sent;
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci	return win_left >= payload_len;
77562306a36Sopenharmony_ci}
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ci/**
77862306a36Sopenharmony_ci * batadv_tp_wait_available() - wait until congestion window becomes free or
77962306a36Sopenharmony_ci *  timeout is reached
78062306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
78162306a36Sopenharmony_ci * @plen: size of the payload of a single message
78262306a36Sopenharmony_ci *
78362306a36Sopenharmony_ci * Return: 0 if the condition evaluated to false after the timeout elapsed,
78462306a36Sopenharmony_ci *  1 if the condition evaluated to true after the timeout elapsed, the
78562306a36Sopenharmony_ci *  remaining jiffies (at least 1) if the condition evaluated to true before
78662306a36Sopenharmony_ci *  the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
78762306a36Sopenharmony_ci */
78862306a36Sopenharmony_cistatic int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
78962306a36Sopenharmony_ci{
79062306a36Sopenharmony_ci	int ret;
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci	ret = wait_event_interruptible_timeout(tp_vars->more_bytes,
79362306a36Sopenharmony_ci					       batadv_tp_avail(tp_vars, plen),
79462306a36Sopenharmony_ci					       HZ / 10);
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ci	return ret;
79762306a36Sopenharmony_ci}
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_ci/**
80062306a36Sopenharmony_ci * batadv_tp_send() - main sending thread of a tp meter session
80162306a36Sopenharmony_ci * @arg: address of the related tp_vars
80262306a36Sopenharmony_ci *
80362306a36Sopenharmony_ci * Return: nothing, this function never returns
80462306a36Sopenharmony_ci */
80562306a36Sopenharmony_cistatic int batadv_tp_send(void *arg)
80662306a36Sopenharmony_ci{
80762306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars = arg;
80862306a36Sopenharmony_ci	struct batadv_priv *bat_priv = tp_vars->bat_priv;
80962306a36Sopenharmony_ci	struct batadv_hard_iface *primary_if = NULL;
81062306a36Sopenharmony_ci	struct batadv_orig_node *orig_node = NULL;
81162306a36Sopenharmony_ci	size_t payload_len, packet_len;
81262306a36Sopenharmony_ci	int err = 0;
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_ci	if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
81562306a36Sopenharmony_ci		err = BATADV_TP_REASON_DST_UNREACHABLE;
81662306a36Sopenharmony_ci		tp_vars->reason = err;
81762306a36Sopenharmony_ci		goto out;
81862306a36Sopenharmony_ci	}
81962306a36Sopenharmony_ci
82062306a36Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, tp_vars->other_end);
82162306a36Sopenharmony_ci	if (unlikely(!orig_node)) {
82262306a36Sopenharmony_ci		err = BATADV_TP_REASON_DST_UNREACHABLE;
82362306a36Sopenharmony_ci		tp_vars->reason = err;
82462306a36Sopenharmony_ci		goto out;
82562306a36Sopenharmony_ci	}
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
82862306a36Sopenharmony_ci	if (unlikely(!primary_if)) {
82962306a36Sopenharmony_ci		err = BATADV_TP_REASON_DST_UNREACHABLE;
83062306a36Sopenharmony_ci		tp_vars->reason = err;
83162306a36Sopenharmony_ci		goto out;
83262306a36Sopenharmony_ci	}
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_ci	/* assume that all the hard_interfaces have a correctly
83562306a36Sopenharmony_ci	 * configured MTU, so use the soft_iface MTU as MSS.
83662306a36Sopenharmony_ci	 * This might not be true and in that case the fragmentation
83762306a36Sopenharmony_ci	 * should be used.
83862306a36Sopenharmony_ci	 * Now, try to send the packet as it is
83962306a36Sopenharmony_ci	 */
84062306a36Sopenharmony_ci	payload_len = BATADV_TP_PLEN;
84162306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet) > BATADV_TP_PLEN);
84262306a36Sopenharmony_ci
84362306a36Sopenharmony_ci	batadv_tp_reset_sender_timer(tp_vars);
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ci	/* queue the worker in charge of terminating the test */
84662306a36Sopenharmony_ci	queue_delayed_work(batadv_event_workqueue, &tp_vars->finish_work,
84762306a36Sopenharmony_ci			   msecs_to_jiffies(tp_vars->test_length));
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci	while (atomic_read(&tp_vars->sending) != 0) {
85062306a36Sopenharmony_ci		if (unlikely(!batadv_tp_avail(tp_vars, payload_len))) {
85162306a36Sopenharmony_ci			batadv_tp_wait_available(tp_vars, payload_len);
85262306a36Sopenharmony_ci			continue;
85362306a36Sopenharmony_ci		}
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci		/* to emulate normal unicast traffic, add to the payload len
85662306a36Sopenharmony_ci		 * the size of the unicast header
85762306a36Sopenharmony_ci		 */
85862306a36Sopenharmony_ci		packet_len = payload_len + sizeof(struct batadv_unicast_packet);
85962306a36Sopenharmony_ci
86062306a36Sopenharmony_ci		err = batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
86162306a36Sopenharmony_ci					 orig_node, tp_vars->last_sent,
86262306a36Sopenharmony_ci					 packet_len,
86362306a36Sopenharmony_ci					 tp_vars->session, tp_vars->icmp_uid,
86462306a36Sopenharmony_ci					 jiffies_to_msecs(jiffies));
86562306a36Sopenharmony_ci
86662306a36Sopenharmony_ci		/* something went wrong during the preparation/transmission */
86762306a36Sopenharmony_ci		if (unlikely(err && err != BATADV_TP_REASON_CANT_SEND)) {
86862306a36Sopenharmony_ci			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
86962306a36Sopenharmony_ci				   "Meter: %s() cannot send packets (%d)\n",
87062306a36Sopenharmony_ci				   __func__, err);
87162306a36Sopenharmony_ci			/* ensure nobody else tries to stop the thread now */
87262306a36Sopenharmony_ci			if (atomic_dec_and_test(&tp_vars->sending))
87362306a36Sopenharmony_ci				tp_vars->reason = err;
87462306a36Sopenharmony_ci			break;
87562306a36Sopenharmony_ci		}
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_ci		/* right-shift the TWND */
87862306a36Sopenharmony_ci		if (!err)
87962306a36Sopenharmony_ci			tp_vars->last_sent += payload_len;
88062306a36Sopenharmony_ci
88162306a36Sopenharmony_ci		cond_resched();
88262306a36Sopenharmony_ci	}
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ciout:
88562306a36Sopenharmony_ci	batadv_hardif_put(primary_if);
88662306a36Sopenharmony_ci	batadv_orig_node_put(orig_node);
88762306a36Sopenharmony_ci
88862306a36Sopenharmony_ci	batadv_tp_sender_end(bat_priv, tp_vars);
88962306a36Sopenharmony_ci	batadv_tp_sender_cleanup(bat_priv, tp_vars);
89062306a36Sopenharmony_ci
89162306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
89262306a36Sopenharmony_ci
89362306a36Sopenharmony_ci	return 0;
89462306a36Sopenharmony_ci}
89562306a36Sopenharmony_ci
89662306a36Sopenharmony_ci/**
89762306a36Sopenharmony_ci * batadv_tp_start_kthread() - start new thread which manages the tp meter
89862306a36Sopenharmony_ci *  sender
89962306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
90062306a36Sopenharmony_ci */
90162306a36Sopenharmony_cistatic void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
90262306a36Sopenharmony_ci{
90362306a36Sopenharmony_ci	struct task_struct *kthread;
90462306a36Sopenharmony_ci	struct batadv_priv *bat_priv = tp_vars->bat_priv;
90562306a36Sopenharmony_ci	u32 session_cookie;
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	kref_get(&tp_vars->refcount);
90862306a36Sopenharmony_ci	kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
90962306a36Sopenharmony_ci	if (IS_ERR(kthread)) {
91062306a36Sopenharmony_ci		session_cookie = batadv_tp_session_cookie(tp_vars->session,
91162306a36Sopenharmony_ci							  tp_vars->icmp_uid);
91262306a36Sopenharmony_ci		pr_err("batadv: cannot create tp meter kthread\n");
91362306a36Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
91462306a36Sopenharmony_ci					      tp_vars->other_end,
91562306a36Sopenharmony_ci					      bat_priv, session_cookie);
91662306a36Sopenharmony_ci
91762306a36Sopenharmony_ci		/* drop reserved reference for kthread */
91862306a36Sopenharmony_ci		batadv_tp_vars_put(tp_vars);
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_ci		/* cleanup of failed tp meter variables */
92162306a36Sopenharmony_ci		batadv_tp_sender_cleanup(bat_priv, tp_vars);
92262306a36Sopenharmony_ci		return;
92362306a36Sopenharmony_ci	}
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ci	wake_up_process(kthread);
92662306a36Sopenharmony_ci}
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci/**
92962306a36Sopenharmony_ci * batadv_tp_start() - start a new tp meter session
93062306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
93162306a36Sopenharmony_ci * @dst: the receiver MAC address
93262306a36Sopenharmony_ci * @test_length: test length in milliseconds
93362306a36Sopenharmony_ci * @cookie: session cookie
93462306a36Sopenharmony_ci */
93562306a36Sopenharmony_civoid batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
93662306a36Sopenharmony_ci		     u32 test_length, u32 *cookie)
93762306a36Sopenharmony_ci{
93862306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
93962306a36Sopenharmony_ci	u8 session_id[2];
94062306a36Sopenharmony_ci	u8 icmp_uid;
94162306a36Sopenharmony_ci	u32 session_cookie;
94262306a36Sopenharmony_ci
94362306a36Sopenharmony_ci	get_random_bytes(session_id, sizeof(session_id));
94462306a36Sopenharmony_ci	get_random_bytes(&icmp_uid, 1);
94562306a36Sopenharmony_ci	session_cookie = batadv_tp_session_cookie(session_id, icmp_uid);
94662306a36Sopenharmony_ci	*cookie = session_cookie;
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci	/* look for an already existing test towards this node */
94962306a36Sopenharmony_ci	spin_lock_bh(&bat_priv->tp_list_lock);
95062306a36Sopenharmony_ci	tp_vars = batadv_tp_list_find(bat_priv, dst);
95162306a36Sopenharmony_ci	if (tp_vars) {
95262306a36Sopenharmony_ci		spin_unlock_bh(&bat_priv->tp_list_lock);
95362306a36Sopenharmony_ci		batadv_tp_vars_put(tp_vars);
95462306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
95562306a36Sopenharmony_ci			   "Meter: test to or from the same node already ongoing, aborting\n");
95662306a36Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING,
95762306a36Sopenharmony_ci					      dst, bat_priv, session_cookie);
95862306a36Sopenharmony_ci		return;
95962306a36Sopenharmony_ci	}
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci	if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
96262306a36Sopenharmony_ci		spin_unlock_bh(&bat_priv->tp_list_lock);
96362306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
96462306a36Sopenharmony_ci			   "Meter: too many ongoing sessions, aborting (SEND)\n");
96562306a36Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY, dst,
96662306a36Sopenharmony_ci					      bat_priv, session_cookie);
96762306a36Sopenharmony_ci		return;
96862306a36Sopenharmony_ci	}
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci	tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
97162306a36Sopenharmony_ci	if (!tp_vars) {
97262306a36Sopenharmony_ci		spin_unlock_bh(&bat_priv->tp_list_lock);
97362306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
97462306a36Sopenharmony_ci			   "Meter: %s cannot allocate list elements\n",
97562306a36Sopenharmony_ci			   __func__);
97662306a36Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
97762306a36Sopenharmony_ci					      dst, bat_priv, session_cookie);
97862306a36Sopenharmony_ci		return;
97962306a36Sopenharmony_ci	}
98062306a36Sopenharmony_ci
98162306a36Sopenharmony_ci	/* initialize tp_vars */
98262306a36Sopenharmony_ci	ether_addr_copy(tp_vars->other_end, dst);
98362306a36Sopenharmony_ci	kref_init(&tp_vars->refcount);
98462306a36Sopenharmony_ci	tp_vars->role = BATADV_TP_SENDER;
98562306a36Sopenharmony_ci	atomic_set(&tp_vars->sending, 1);
98662306a36Sopenharmony_ci	memcpy(tp_vars->session, session_id, sizeof(session_id));
98762306a36Sopenharmony_ci	tp_vars->icmp_uid = icmp_uid;
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci	tp_vars->last_sent = BATADV_TP_FIRST_SEQ;
99062306a36Sopenharmony_ci	atomic_set(&tp_vars->last_acked, BATADV_TP_FIRST_SEQ);
99162306a36Sopenharmony_ci	tp_vars->fast_recovery = false;
99262306a36Sopenharmony_ci	tp_vars->recover = BATADV_TP_FIRST_SEQ;
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci	/* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
99562306a36Sopenharmony_ci	 * For batman-adv the MSS is the size of the payload received by the
99662306a36Sopenharmony_ci	 * soft_interface, hence its MTU
99762306a36Sopenharmony_ci	 */
99862306a36Sopenharmony_ci	tp_vars->cwnd = BATADV_TP_PLEN * 3;
99962306a36Sopenharmony_ci	/* at the beginning initialise the SS threshold to the biggest possible
100062306a36Sopenharmony_ci	 * window size, hence the AWND size
100162306a36Sopenharmony_ci	 */
100262306a36Sopenharmony_ci	tp_vars->ss_threshold = BATADV_TP_AWND;
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci	/* RTO initial value is 3 seconds.
100562306a36Sopenharmony_ci	 * Details in Section 2.1 of RFC6298
100662306a36Sopenharmony_ci	 */
100762306a36Sopenharmony_ci	tp_vars->rto = 1000;
100862306a36Sopenharmony_ci	tp_vars->srtt = 0;
100962306a36Sopenharmony_ci	tp_vars->rttvar = 0;
101062306a36Sopenharmony_ci
101162306a36Sopenharmony_ci	atomic64_set(&tp_vars->tot_sent, 0);
101262306a36Sopenharmony_ci
101362306a36Sopenharmony_ci	kref_get(&tp_vars->refcount);
101462306a36Sopenharmony_ci	timer_setup(&tp_vars->timer, batadv_tp_sender_timeout, 0);
101562306a36Sopenharmony_ci
101662306a36Sopenharmony_ci	tp_vars->bat_priv = bat_priv;
101762306a36Sopenharmony_ci	tp_vars->start_time = jiffies;
101862306a36Sopenharmony_ci
101962306a36Sopenharmony_ci	init_waitqueue_head(&tp_vars->more_bytes);
102062306a36Sopenharmony_ci
102162306a36Sopenharmony_ci	spin_lock_init(&tp_vars->unacked_lock);
102262306a36Sopenharmony_ci	INIT_LIST_HEAD(&tp_vars->unacked_list);
102362306a36Sopenharmony_ci
102462306a36Sopenharmony_ci	spin_lock_init(&tp_vars->cwnd_lock);
102562306a36Sopenharmony_ci
102662306a36Sopenharmony_ci	tp_vars->prerandom_offset = 0;
102762306a36Sopenharmony_ci	spin_lock_init(&tp_vars->prerandom_lock);
102862306a36Sopenharmony_ci
102962306a36Sopenharmony_ci	kref_get(&tp_vars->refcount);
103062306a36Sopenharmony_ci	hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
103162306a36Sopenharmony_ci	spin_unlock_bh(&bat_priv->tp_list_lock);
103262306a36Sopenharmony_ci
103362306a36Sopenharmony_ci	tp_vars->test_length = test_length;
103462306a36Sopenharmony_ci	if (!tp_vars->test_length)
103562306a36Sopenharmony_ci		tp_vars->test_length = BATADV_TP_DEF_TEST_LENGTH;
103662306a36Sopenharmony_ci
103762306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
103862306a36Sopenharmony_ci		   "Meter: starting throughput meter towards %pM (length=%ums)\n",
103962306a36Sopenharmony_ci		   dst, test_length);
104062306a36Sopenharmony_ci
104162306a36Sopenharmony_ci	/* init work item for finished tp tests */
104262306a36Sopenharmony_ci	INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	/* start tp kthread. This way the write() call issued from userspace can
104562306a36Sopenharmony_ci	 * happily return and avoid to block
104662306a36Sopenharmony_ci	 */
104762306a36Sopenharmony_ci	batadv_tp_start_kthread(tp_vars);
104862306a36Sopenharmony_ci
104962306a36Sopenharmony_ci	/* don't return reference to new tp_vars */
105062306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
105162306a36Sopenharmony_ci}
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci/**
105462306a36Sopenharmony_ci * batadv_tp_stop() - stop currently running tp meter session
105562306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
105662306a36Sopenharmony_ci * @dst: the receiver MAC address
105762306a36Sopenharmony_ci * @return_value: reason for tp meter session stop
105862306a36Sopenharmony_ci */
105962306a36Sopenharmony_civoid batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
106062306a36Sopenharmony_ci		    u8 return_value)
106162306a36Sopenharmony_ci{
106262306a36Sopenharmony_ci	struct batadv_orig_node *orig_node;
106362306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
106462306a36Sopenharmony_ci
106562306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
106662306a36Sopenharmony_ci		   "Meter: stopping test towards %pM\n", dst);
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, dst);
106962306a36Sopenharmony_ci	if (!orig_node)
107062306a36Sopenharmony_ci		return;
107162306a36Sopenharmony_ci
107262306a36Sopenharmony_ci	tp_vars = batadv_tp_list_find(bat_priv, orig_node->orig);
107362306a36Sopenharmony_ci	if (!tp_vars) {
107462306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
107562306a36Sopenharmony_ci			   "Meter: trying to interrupt an already over connection\n");
107662306a36Sopenharmony_ci		goto out;
107762306a36Sopenharmony_ci	}
107862306a36Sopenharmony_ci
107962306a36Sopenharmony_ci	batadv_tp_sender_shutdown(tp_vars, return_value);
108062306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
108162306a36Sopenharmony_ciout:
108262306a36Sopenharmony_ci	batadv_orig_node_put(orig_node);
108362306a36Sopenharmony_ci}
108462306a36Sopenharmony_ci
108562306a36Sopenharmony_ci/**
108662306a36Sopenharmony_ci * batadv_tp_reset_receiver_timer() - reset the receiver shutdown timer
108762306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
108862306a36Sopenharmony_ci *
108962306a36Sopenharmony_ci * start the receiver shutdown timer or reset it if already started
109062306a36Sopenharmony_ci */
109162306a36Sopenharmony_cistatic void batadv_tp_reset_receiver_timer(struct batadv_tp_vars *tp_vars)
109262306a36Sopenharmony_ci{
109362306a36Sopenharmony_ci	mod_timer(&tp_vars->timer,
109462306a36Sopenharmony_ci		  jiffies + msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT));
109562306a36Sopenharmony_ci}
109662306a36Sopenharmony_ci
109762306a36Sopenharmony_ci/**
109862306a36Sopenharmony_ci * batadv_tp_receiver_shutdown() - stop a tp meter receiver when timeout is
109962306a36Sopenharmony_ci *  reached without received ack
110062306a36Sopenharmony_ci * @t: address to timer_list inside tp_vars
110162306a36Sopenharmony_ci */
110262306a36Sopenharmony_cistatic void batadv_tp_receiver_shutdown(struct timer_list *t)
110362306a36Sopenharmony_ci{
110462306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
110562306a36Sopenharmony_ci	struct batadv_tp_unacked *un, *safe;
110662306a36Sopenharmony_ci	struct batadv_priv *bat_priv;
110762306a36Sopenharmony_ci
110862306a36Sopenharmony_ci	bat_priv = tp_vars->bat_priv;
110962306a36Sopenharmony_ci
111062306a36Sopenharmony_ci	/* if there is recent activity rearm the timer */
111162306a36Sopenharmony_ci	if (!batadv_has_timed_out(tp_vars->last_recv_time,
111262306a36Sopenharmony_ci				  BATADV_TP_RECV_TIMEOUT)) {
111362306a36Sopenharmony_ci		/* reset the receiver shutdown timer */
111462306a36Sopenharmony_ci		batadv_tp_reset_receiver_timer(tp_vars);
111562306a36Sopenharmony_ci		return;
111662306a36Sopenharmony_ci	}
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
111962306a36Sopenharmony_ci		   "Shutting down for inactivity (more than %dms) from %pM\n",
112062306a36Sopenharmony_ci		   BATADV_TP_RECV_TIMEOUT, tp_vars->other_end);
112162306a36Sopenharmony_ci
112262306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
112362306a36Sopenharmony_ci	hlist_del_rcu(&tp_vars->list);
112462306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
112562306a36Sopenharmony_ci
112662306a36Sopenharmony_ci	/* drop list reference */
112762306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
112862306a36Sopenharmony_ci
112962306a36Sopenharmony_ci	atomic_dec(&bat_priv->tp_num);
113062306a36Sopenharmony_ci
113162306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
113262306a36Sopenharmony_ci	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
113362306a36Sopenharmony_ci		list_del(&un->list);
113462306a36Sopenharmony_ci		kfree(un);
113562306a36Sopenharmony_ci	}
113662306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
113762306a36Sopenharmony_ci
113862306a36Sopenharmony_ci	/* drop reference of timer */
113962306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
114062306a36Sopenharmony_ci}
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci/**
114362306a36Sopenharmony_ci * batadv_tp_send_ack() - send an ACK packet
114462306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
114562306a36Sopenharmony_ci * @dst: the mac address of the destination originator
114662306a36Sopenharmony_ci * @seq: the sequence number to ACK
114762306a36Sopenharmony_ci * @timestamp: the timestamp to echo back in the ACK
114862306a36Sopenharmony_ci * @session: session identifier
114962306a36Sopenharmony_ci * @socket_index: local ICMP socket identifier
115062306a36Sopenharmony_ci *
115162306a36Sopenharmony_ci * Return: 0 on success, a positive integer representing the reason of the
115262306a36Sopenharmony_ci * failure otherwise
115362306a36Sopenharmony_ci */
115462306a36Sopenharmony_cistatic int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst,
115562306a36Sopenharmony_ci			      u32 seq, __be32 timestamp, const u8 *session,
115662306a36Sopenharmony_ci			      int socket_index)
115762306a36Sopenharmony_ci{
115862306a36Sopenharmony_ci	struct batadv_hard_iface *primary_if = NULL;
115962306a36Sopenharmony_ci	struct batadv_orig_node *orig_node;
116062306a36Sopenharmony_ci	struct batadv_icmp_tp_packet *icmp;
116162306a36Sopenharmony_ci	struct sk_buff *skb;
116262306a36Sopenharmony_ci	int r, ret;
116362306a36Sopenharmony_ci
116462306a36Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, dst);
116562306a36Sopenharmony_ci	if (unlikely(!orig_node)) {
116662306a36Sopenharmony_ci		ret = BATADV_TP_REASON_DST_UNREACHABLE;
116762306a36Sopenharmony_ci		goto out;
116862306a36Sopenharmony_ci	}
116962306a36Sopenharmony_ci
117062306a36Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
117162306a36Sopenharmony_ci	if (unlikely(!primary_if)) {
117262306a36Sopenharmony_ci		ret = BATADV_TP_REASON_DST_UNREACHABLE;
117362306a36Sopenharmony_ci		goto out;
117462306a36Sopenharmony_ci	}
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_ci	skb = netdev_alloc_skb_ip_align(NULL, sizeof(*icmp) + ETH_HLEN);
117762306a36Sopenharmony_ci	if (unlikely(!skb)) {
117862306a36Sopenharmony_ci		ret = BATADV_TP_REASON_MEMORY_ERROR;
117962306a36Sopenharmony_ci		goto out;
118062306a36Sopenharmony_ci	}
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci	skb_reserve(skb, ETH_HLEN);
118362306a36Sopenharmony_ci	icmp = skb_put(skb, sizeof(*icmp));
118462306a36Sopenharmony_ci	icmp->packet_type = BATADV_ICMP;
118562306a36Sopenharmony_ci	icmp->version = BATADV_COMPAT_VERSION;
118662306a36Sopenharmony_ci	icmp->ttl = BATADV_TTL;
118762306a36Sopenharmony_ci	icmp->msg_type = BATADV_TP;
118862306a36Sopenharmony_ci	ether_addr_copy(icmp->dst, orig_node->orig);
118962306a36Sopenharmony_ci	ether_addr_copy(icmp->orig, primary_if->net_dev->dev_addr);
119062306a36Sopenharmony_ci	icmp->uid = socket_index;
119162306a36Sopenharmony_ci
119262306a36Sopenharmony_ci	icmp->subtype = BATADV_TP_ACK;
119362306a36Sopenharmony_ci	memcpy(icmp->session, session, sizeof(icmp->session));
119462306a36Sopenharmony_ci	icmp->seqno = htonl(seq);
119562306a36Sopenharmony_ci	icmp->timestamp = timestamp;
119662306a36Sopenharmony_ci
119762306a36Sopenharmony_ci	/* send the ack */
119862306a36Sopenharmony_ci	r = batadv_send_skb_to_orig(skb, orig_node, NULL);
119962306a36Sopenharmony_ci	if (unlikely(r < 0) || r == NET_XMIT_DROP) {
120062306a36Sopenharmony_ci		ret = BATADV_TP_REASON_DST_UNREACHABLE;
120162306a36Sopenharmony_ci		goto out;
120262306a36Sopenharmony_ci	}
120362306a36Sopenharmony_ci	ret = 0;
120462306a36Sopenharmony_ci
120562306a36Sopenharmony_ciout:
120662306a36Sopenharmony_ci	batadv_orig_node_put(orig_node);
120762306a36Sopenharmony_ci	batadv_hardif_put(primary_if);
120862306a36Sopenharmony_ci
120962306a36Sopenharmony_ci	return ret;
121062306a36Sopenharmony_ci}
121162306a36Sopenharmony_ci
121262306a36Sopenharmony_ci/**
121362306a36Sopenharmony_ci * batadv_tp_handle_out_of_order() - store an out of order packet
121462306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
121562306a36Sopenharmony_ci * @skb: the buffer containing the received packet
121662306a36Sopenharmony_ci *
121762306a36Sopenharmony_ci * Store the out of order packet in the unacked list for late processing. This
121862306a36Sopenharmony_ci * packets are kept in this list so that they can be ACKed at once as soon as
121962306a36Sopenharmony_ci * all the previous packets have been received
122062306a36Sopenharmony_ci *
122162306a36Sopenharmony_ci * Return: true if the packed has been successfully processed, false otherwise
122262306a36Sopenharmony_ci */
122362306a36Sopenharmony_cistatic bool batadv_tp_handle_out_of_order(struct batadv_tp_vars *tp_vars,
122462306a36Sopenharmony_ci					  const struct sk_buff *skb)
122562306a36Sopenharmony_ci{
122662306a36Sopenharmony_ci	const struct batadv_icmp_tp_packet *icmp;
122762306a36Sopenharmony_ci	struct batadv_tp_unacked *un, *new;
122862306a36Sopenharmony_ci	u32 payload_len;
122962306a36Sopenharmony_ci	bool added = false;
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci	new = kmalloc(sizeof(*new), GFP_ATOMIC);
123262306a36Sopenharmony_ci	if (unlikely(!new))
123362306a36Sopenharmony_ci		return false;
123462306a36Sopenharmony_ci
123562306a36Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
123662306a36Sopenharmony_ci
123762306a36Sopenharmony_ci	new->seqno = ntohl(icmp->seqno);
123862306a36Sopenharmony_ci	payload_len = skb->len - sizeof(struct batadv_unicast_packet);
123962306a36Sopenharmony_ci	new->len = payload_len;
124062306a36Sopenharmony_ci
124162306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
124262306a36Sopenharmony_ci	/* if the list is empty immediately attach this new object */
124362306a36Sopenharmony_ci	if (list_empty(&tp_vars->unacked_list)) {
124462306a36Sopenharmony_ci		list_add(&new->list, &tp_vars->unacked_list);
124562306a36Sopenharmony_ci		goto out;
124662306a36Sopenharmony_ci	}
124762306a36Sopenharmony_ci
124862306a36Sopenharmony_ci	/* otherwise loop over the list and either drop the packet because this
124962306a36Sopenharmony_ci	 * is a duplicate or store it at the right position.
125062306a36Sopenharmony_ci	 *
125162306a36Sopenharmony_ci	 * The iteration is done in the reverse way because it is likely that
125262306a36Sopenharmony_ci	 * the last received packet (the one being processed now) has a bigger
125362306a36Sopenharmony_ci	 * seqno than all the others already stored.
125462306a36Sopenharmony_ci	 */
125562306a36Sopenharmony_ci	list_for_each_entry_reverse(un, &tp_vars->unacked_list, list) {
125662306a36Sopenharmony_ci		/* check for duplicates */
125762306a36Sopenharmony_ci		if (new->seqno == un->seqno) {
125862306a36Sopenharmony_ci			if (new->len > un->len)
125962306a36Sopenharmony_ci				un->len = new->len;
126062306a36Sopenharmony_ci			kfree(new);
126162306a36Sopenharmony_ci			added = true;
126262306a36Sopenharmony_ci			break;
126362306a36Sopenharmony_ci		}
126462306a36Sopenharmony_ci
126562306a36Sopenharmony_ci		/* look for the right position */
126662306a36Sopenharmony_ci		if (batadv_seq_before(new->seqno, un->seqno))
126762306a36Sopenharmony_ci			continue;
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_ci		/* as soon as an entry having a bigger seqno is found, the new
127062306a36Sopenharmony_ci		 * one is attached _after_ it. In this way the list is kept in
127162306a36Sopenharmony_ci		 * ascending order
127262306a36Sopenharmony_ci		 */
127362306a36Sopenharmony_ci		list_add_tail(&new->list, &un->list);
127462306a36Sopenharmony_ci		added = true;
127562306a36Sopenharmony_ci		break;
127662306a36Sopenharmony_ci	}
127762306a36Sopenharmony_ci
127862306a36Sopenharmony_ci	/* received packet with smallest seqno out of order; add it to front */
127962306a36Sopenharmony_ci	if (!added)
128062306a36Sopenharmony_ci		list_add(&new->list, &tp_vars->unacked_list);
128162306a36Sopenharmony_ci
128262306a36Sopenharmony_ciout:
128362306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
128462306a36Sopenharmony_ci
128562306a36Sopenharmony_ci	return true;
128662306a36Sopenharmony_ci}
128762306a36Sopenharmony_ci
128862306a36Sopenharmony_ci/**
128962306a36Sopenharmony_ci * batadv_tp_ack_unordered() - update number received bytes in current stream
129062306a36Sopenharmony_ci *  without gaps
129162306a36Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
129262306a36Sopenharmony_ci */
129362306a36Sopenharmony_cistatic void batadv_tp_ack_unordered(struct batadv_tp_vars *tp_vars)
129462306a36Sopenharmony_ci{
129562306a36Sopenharmony_ci	struct batadv_tp_unacked *un, *safe;
129662306a36Sopenharmony_ci	u32 to_ack;
129762306a36Sopenharmony_ci
129862306a36Sopenharmony_ci	/* go through the unacked packet list and possibly ACK them as
129962306a36Sopenharmony_ci	 * well
130062306a36Sopenharmony_ci	 */
130162306a36Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
130262306a36Sopenharmony_ci	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
130362306a36Sopenharmony_ci		/* the list is ordered, therefore it is possible to stop as soon
130462306a36Sopenharmony_ci		 * there is a gap between the last acked seqno and the seqno of
130562306a36Sopenharmony_ci		 * the packet under inspection
130662306a36Sopenharmony_ci		 */
130762306a36Sopenharmony_ci		if (batadv_seq_before(tp_vars->last_recv, un->seqno))
130862306a36Sopenharmony_ci			break;
130962306a36Sopenharmony_ci
131062306a36Sopenharmony_ci		to_ack = un->seqno + un->len - tp_vars->last_recv;
131162306a36Sopenharmony_ci
131262306a36Sopenharmony_ci		if (batadv_seq_before(tp_vars->last_recv, un->seqno + un->len))
131362306a36Sopenharmony_ci			tp_vars->last_recv += to_ack;
131462306a36Sopenharmony_ci
131562306a36Sopenharmony_ci		list_del(&un->list);
131662306a36Sopenharmony_ci		kfree(un);
131762306a36Sopenharmony_ci	}
131862306a36Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
131962306a36Sopenharmony_ci}
132062306a36Sopenharmony_ci
132162306a36Sopenharmony_ci/**
132262306a36Sopenharmony_ci * batadv_tp_init_recv() - return matching or create new receiver tp_vars
132362306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
132462306a36Sopenharmony_ci * @icmp: received icmp tp msg
132562306a36Sopenharmony_ci *
132662306a36Sopenharmony_ci * Return: corresponding tp_vars or NULL on errors
132762306a36Sopenharmony_ci */
132862306a36Sopenharmony_cistatic struct batadv_tp_vars *
132962306a36Sopenharmony_cibatadv_tp_init_recv(struct batadv_priv *bat_priv,
133062306a36Sopenharmony_ci		    const struct batadv_icmp_tp_packet *icmp)
133162306a36Sopenharmony_ci{
133262306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
133362306a36Sopenharmony_ci
133462306a36Sopenharmony_ci	spin_lock_bh(&bat_priv->tp_list_lock);
133562306a36Sopenharmony_ci	tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
133662306a36Sopenharmony_ci					      icmp->session);
133762306a36Sopenharmony_ci	if (tp_vars)
133862306a36Sopenharmony_ci		goto out_unlock;
133962306a36Sopenharmony_ci
134062306a36Sopenharmony_ci	if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
134162306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
134262306a36Sopenharmony_ci			   "Meter: too many ongoing sessions, aborting (RECV)\n");
134362306a36Sopenharmony_ci		goto out_unlock;
134462306a36Sopenharmony_ci	}
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci	tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
134762306a36Sopenharmony_ci	if (!tp_vars)
134862306a36Sopenharmony_ci		goto out_unlock;
134962306a36Sopenharmony_ci
135062306a36Sopenharmony_ci	ether_addr_copy(tp_vars->other_end, icmp->orig);
135162306a36Sopenharmony_ci	tp_vars->role = BATADV_TP_RECEIVER;
135262306a36Sopenharmony_ci	memcpy(tp_vars->session, icmp->session, sizeof(tp_vars->session));
135362306a36Sopenharmony_ci	tp_vars->last_recv = BATADV_TP_FIRST_SEQ;
135462306a36Sopenharmony_ci	tp_vars->bat_priv = bat_priv;
135562306a36Sopenharmony_ci	kref_init(&tp_vars->refcount);
135662306a36Sopenharmony_ci
135762306a36Sopenharmony_ci	spin_lock_init(&tp_vars->unacked_lock);
135862306a36Sopenharmony_ci	INIT_LIST_HEAD(&tp_vars->unacked_list);
135962306a36Sopenharmony_ci
136062306a36Sopenharmony_ci	kref_get(&tp_vars->refcount);
136162306a36Sopenharmony_ci	hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
136262306a36Sopenharmony_ci
136362306a36Sopenharmony_ci	kref_get(&tp_vars->refcount);
136462306a36Sopenharmony_ci	timer_setup(&tp_vars->timer, batadv_tp_receiver_shutdown, 0);
136562306a36Sopenharmony_ci
136662306a36Sopenharmony_ci	batadv_tp_reset_receiver_timer(tp_vars);
136762306a36Sopenharmony_ci
136862306a36Sopenharmony_ciout_unlock:
136962306a36Sopenharmony_ci	spin_unlock_bh(&bat_priv->tp_list_lock);
137062306a36Sopenharmony_ci
137162306a36Sopenharmony_ci	return tp_vars;
137262306a36Sopenharmony_ci}
137362306a36Sopenharmony_ci
137462306a36Sopenharmony_ci/**
137562306a36Sopenharmony_ci * batadv_tp_recv_msg() - process a single data message
137662306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
137762306a36Sopenharmony_ci * @skb: the buffer containing the received packet
137862306a36Sopenharmony_ci *
137962306a36Sopenharmony_ci * Process a received TP MSG packet
138062306a36Sopenharmony_ci */
138162306a36Sopenharmony_cistatic void batadv_tp_recv_msg(struct batadv_priv *bat_priv,
138262306a36Sopenharmony_ci			       const struct sk_buff *skb)
138362306a36Sopenharmony_ci{
138462306a36Sopenharmony_ci	const struct batadv_icmp_tp_packet *icmp;
138562306a36Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
138662306a36Sopenharmony_ci	size_t packet_size;
138762306a36Sopenharmony_ci	u32 seqno;
138862306a36Sopenharmony_ci
138962306a36Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
139062306a36Sopenharmony_ci
139162306a36Sopenharmony_ci	seqno = ntohl(icmp->seqno);
139262306a36Sopenharmony_ci	/* check if this is the first seqno. This means that if the
139362306a36Sopenharmony_ci	 * first packet is lost, the tp meter does not work anymore!
139462306a36Sopenharmony_ci	 */
139562306a36Sopenharmony_ci	if (seqno == BATADV_TP_FIRST_SEQ) {
139662306a36Sopenharmony_ci		tp_vars = batadv_tp_init_recv(bat_priv, icmp);
139762306a36Sopenharmony_ci		if (!tp_vars) {
139862306a36Sopenharmony_ci			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
139962306a36Sopenharmony_ci				   "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
140062306a36Sopenharmony_ci			goto out;
140162306a36Sopenharmony_ci		}
140262306a36Sopenharmony_ci	} else {
140362306a36Sopenharmony_ci		tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
140462306a36Sopenharmony_ci						      icmp->session);
140562306a36Sopenharmony_ci		if (!tp_vars) {
140662306a36Sopenharmony_ci			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
140762306a36Sopenharmony_ci				   "Unexpected packet from %pM!\n",
140862306a36Sopenharmony_ci				   icmp->orig);
140962306a36Sopenharmony_ci			goto out;
141062306a36Sopenharmony_ci		}
141162306a36Sopenharmony_ci	}
141262306a36Sopenharmony_ci
141362306a36Sopenharmony_ci	if (unlikely(tp_vars->role != BATADV_TP_RECEIVER)) {
141462306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
141562306a36Sopenharmony_ci			   "Meter: dropping packet: not expected (role=%u)\n",
141662306a36Sopenharmony_ci			   tp_vars->role);
141762306a36Sopenharmony_ci		goto out;
141862306a36Sopenharmony_ci	}
141962306a36Sopenharmony_ci
142062306a36Sopenharmony_ci	tp_vars->last_recv_time = jiffies;
142162306a36Sopenharmony_ci
142262306a36Sopenharmony_ci	/* if the packet is a duplicate, it may be the case that an ACK has been
142362306a36Sopenharmony_ci	 * lost. Resend the ACK
142462306a36Sopenharmony_ci	 */
142562306a36Sopenharmony_ci	if (batadv_seq_before(seqno, tp_vars->last_recv))
142662306a36Sopenharmony_ci		goto send_ack;
142762306a36Sopenharmony_ci
142862306a36Sopenharmony_ci	/* if the packet is out of order enqueue it */
142962306a36Sopenharmony_ci	if (ntohl(icmp->seqno) != tp_vars->last_recv) {
143062306a36Sopenharmony_ci		/* exit immediately (and do not send any ACK) if the packet has
143162306a36Sopenharmony_ci		 * not been enqueued correctly
143262306a36Sopenharmony_ci		 */
143362306a36Sopenharmony_ci		if (!batadv_tp_handle_out_of_order(tp_vars, skb))
143462306a36Sopenharmony_ci			goto out;
143562306a36Sopenharmony_ci
143662306a36Sopenharmony_ci		/* send a duplicate ACK */
143762306a36Sopenharmony_ci		goto send_ack;
143862306a36Sopenharmony_ci	}
143962306a36Sopenharmony_ci
144062306a36Sopenharmony_ci	/* if everything was fine count the ACKed bytes */
144162306a36Sopenharmony_ci	packet_size = skb->len - sizeof(struct batadv_unicast_packet);
144262306a36Sopenharmony_ci	tp_vars->last_recv += packet_size;
144362306a36Sopenharmony_ci
144462306a36Sopenharmony_ci	/* check if this ordered message filled a gap.... */
144562306a36Sopenharmony_ci	batadv_tp_ack_unordered(tp_vars);
144662306a36Sopenharmony_ci
144762306a36Sopenharmony_cisend_ack:
144862306a36Sopenharmony_ci	/* send the ACK. If the received packet was out of order, the ACK that
144962306a36Sopenharmony_ci	 * is going to be sent is a duplicate (the sender will count them and
145062306a36Sopenharmony_ci	 * possibly enter Fast Retransmit as soon as it has reached 3)
145162306a36Sopenharmony_ci	 */
145262306a36Sopenharmony_ci	batadv_tp_send_ack(bat_priv, icmp->orig, tp_vars->last_recv,
145362306a36Sopenharmony_ci			   icmp->timestamp, icmp->session, icmp->uid);
145462306a36Sopenharmony_ciout:
145562306a36Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
145662306a36Sopenharmony_ci}
145762306a36Sopenharmony_ci
145862306a36Sopenharmony_ci/**
145962306a36Sopenharmony_ci * batadv_tp_meter_recv() - main TP Meter receiving function
146062306a36Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
146162306a36Sopenharmony_ci * @skb: the buffer containing the received packet
146262306a36Sopenharmony_ci */
146362306a36Sopenharmony_civoid batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
146462306a36Sopenharmony_ci{
146562306a36Sopenharmony_ci	struct batadv_icmp_tp_packet *icmp;
146662306a36Sopenharmony_ci
146762306a36Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
146862306a36Sopenharmony_ci
146962306a36Sopenharmony_ci	switch (icmp->subtype) {
147062306a36Sopenharmony_ci	case BATADV_TP_MSG:
147162306a36Sopenharmony_ci		batadv_tp_recv_msg(bat_priv, skb);
147262306a36Sopenharmony_ci		break;
147362306a36Sopenharmony_ci	case BATADV_TP_ACK:
147462306a36Sopenharmony_ci		batadv_tp_recv_ack(bat_priv, skb);
147562306a36Sopenharmony_ci		break;
147662306a36Sopenharmony_ci	default:
147762306a36Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
147862306a36Sopenharmony_ci			   "Received unknown TP Metric packet type %u\n",
147962306a36Sopenharmony_ci			   icmp->subtype);
148062306a36Sopenharmony_ci	}
148162306a36Sopenharmony_ci	consume_skb(skb);
148262306a36Sopenharmony_ci}
148362306a36Sopenharmony_ci
148462306a36Sopenharmony_ci/**
148562306a36Sopenharmony_ci * batadv_tp_meter_init() - initialize global tp_meter structures
148662306a36Sopenharmony_ci */
148762306a36Sopenharmony_civoid __init batadv_tp_meter_init(void)
148862306a36Sopenharmony_ci{
148962306a36Sopenharmony_ci	get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
149062306a36Sopenharmony_ci}
1491