18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (C) 2012-2020  B.A.T.M.A.N. contributors:
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Edo Monticelli, Antonio Quartulli
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "tp_meter.h"
88c2ecf20Sopenharmony_ci#include "main.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/atomic.h>
118c2ecf20Sopenharmony_ci#include <linux/build_bug.h>
128c2ecf20Sopenharmony_ci#include <linux/byteorder/generic.h>
138c2ecf20Sopenharmony_ci#include <linux/cache.h>
148c2ecf20Sopenharmony_ci#include <linux/compiler.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
178c2ecf20Sopenharmony_ci#include <linux/gfp.h>
188c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
218c2ecf20Sopenharmony_ci#include <linux/kernel.h>
228c2ecf20Sopenharmony_ci#include <linux/kref.h>
238c2ecf20Sopenharmony_ci#include <linux/kthread.h>
248c2ecf20Sopenharmony_ci#include <linux/limits.h>
258c2ecf20Sopenharmony_ci#include <linux/list.h>
268c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
278c2ecf20Sopenharmony_ci#include <linux/param.h>
288c2ecf20Sopenharmony_ci#include <linux/printk.h>
298c2ecf20Sopenharmony_ci#include <linux/random.h>
308c2ecf20Sopenharmony_ci#include <linux/rculist.h>
318c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
328c2ecf20Sopenharmony_ci#include <linux/sched.h>
338c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
348c2ecf20Sopenharmony_ci#include <linux/slab.h>
358c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
368c2ecf20Sopenharmony_ci#include <linux/stddef.h>
378c2ecf20Sopenharmony_ci#include <linux/string.h>
388c2ecf20Sopenharmony_ci#include <linux/timer.h>
398c2ecf20Sopenharmony_ci#include <linux/wait.h>
408c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
418c2ecf20Sopenharmony_ci#include <uapi/linux/batadv_packet.h>
428c2ecf20Sopenharmony_ci#include <uapi/linux/batman_adv.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "hard-interface.h"
458c2ecf20Sopenharmony_ci#include "log.h"
468c2ecf20Sopenharmony_ci#include "netlink.h"
478c2ecf20Sopenharmony_ci#include "originator.h"
488c2ecf20Sopenharmony_ci#include "send.h"
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/**
518c2ecf20Sopenharmony_ci * BATADV_TP_DEF_TEST_LENGTH - Default test length if not specified by the user
528c2ecf20Sopenharmony_ci *  in milliseconds
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_ci#define BATADV_TP_DEF_TEST_LENGTH 10000
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/**
578c2ecf20Sopenharmony_ci * BATADV_TP_AWND - Advertised window by the receiver (in bytes)
588c2ecf20Sopenharmony_ci */
598c2ecf20Sopenharmony_ci#define BATADV_TP_AWND 0x20000000
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/**
628c2ecf20Sopenharmony_ci * BATADV_TP_RECV_TIMEOUT - Receiver activity timeout. If the receiver does not
638c2ecf20Sopenharmony_ci *  get anything for such amount of milliseconds, the connection is killed
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci#define BATADV_TP_RECV_TIMEOUT 1000
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/**
688c2ecf20Sopenharmony_ci * BATADV_TP_MAX_RTO - Maximum sender timeout. If the sender RTO gets beyond
698c2ecf20Sopenharmony_ci * such amount of milliseconds, the receiver is considered unreachable and the
708c2ecf20Sopenharmony_ci * connection is killed
718c2ecf20Sopenharmony_ci */
728c2ecf20Sopenharmony_ci#define BATADV_TP_MAX_RTO 30000
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * BATADV_TP_FIRST_SEQ - First seqno of each session. The number is rather high
768c2ecf20Sopenharmony_ci *  in order to immediately trigger a wrap around (test purposes)
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_ci#define BATADV_TP_FIRST_SEQ ((u32)-1 - 2000)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/**
818c2ecf20Sopenharmony_ci * BATADV_TP_PLEN - length of the payload (data after the batadv_unicast header)
828c2ecf20Sopenharmony_ci *  to simulate
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_ci#define BATADV_TP_PLEN (BATADV_TP_PACKET_LEN - ETH_HLEN - \
858c2ecf20Sopenharmony_ci			sizeof(struct batadv_unicast_packet))
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic u8 batadv_tp_prerandom[4096] __read_mostly;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/**
908c2ecf20Sopenharmony_ci * batadv_tp_session_cookie() - generate session cookie based on session ids
918c2ecf20Sopenharmony_ci * @session: TP session identifier
928c2ecf20Sopenharmony_ci * @icmp_uid: icmp pseudo uid of the tp session
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * Return: 32 bit tp_meter session cookie
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistatic u32 batadv_tp_session_cookie(const u8 session[2], u8 icmp_uid)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	u32 cookie;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	cookie = icmp_uid << 16;
1018c2ecf20Sopenharmony_ci	cookie |= session[0] << 8;
1028c2ecf20Sopenharmony_ci	cookie |= session[1];
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return cookie;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/**
1088c2ecf20Sopenharmony_ci * batadv_tp_cwnd() - compute the new cwnd size
1098c2ecf20Sopenharmony_ci * @base: base cwnd size value
1108c2ecf20Sopenharmony_ci * @increment: the value to add to base to get the new size
1118c2ecf20Sopenharmony_ci * @min: minimum cwnd value (usually MSS)
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * Return the new cwnd size and ensure it does not exceed the Advertised
1148c2ecf20Sopenharmony_ci * Receiver Window size. It is wrapped around safely.
1158c2ecf20Sopenharmony_ci * For details refer to Section 3.1 of RFC5681
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * Return: new congestion window size in bytes
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_cistatic u32 batadv_tp_cwnd(u32 base, u32 increment, u32 min)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	u32 new_size = base + increment;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* check for wrap-around */
1248c2ecf20Sopenharmony_ci	if (new_size < base)
1258c2ecf20Sopenharmony_ci		new_size = (u32)ULONG_MAX;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	new_size = min_t(u32, new_size, BATADV_TP_AWND);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return max_t(u32, new_size, min);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/**
1338c2ecf20Sopenharmony_ci * batadv_tp_updated_cwnd() - update the Congestion Windows
1348c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
1358c2ecf20Sopenharmony_ci * @mss: maximum segment size of transmission
1368c2ecf20Sopenharmony_ci *
1378c2ecf20Sopenharmony_ci * 1) if the session is in Slow Start, the CWND has to be increased by 1
1388c2ecf20Sopenharmony_ci * MSS every unique received ACK
1398c2ecf20Sopenharmony_ci * 2) if the session is in Congestion Avoidance, the CWND has to be
1408c2ecf20Sopenharmony_ci * increased by MSS * MSS / CWND for every unique received ACK
1418c2ecf20Sopenharmony_ci */
1428c2ecf20Sopenharmony_cistatic void batadv_tp_update_cwnd(struct batadv_tp_vars *tp_vars, u32 mss)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->cwnd_lock);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	/* slow start... */
1478c2ecf20Sopenharmony_ci	if (tp_vars->cwnd <= tp_vars->ss_threshold) {
1488c2ecf20Sopenharmony_ci		tp_vars->dec_cwnd = 0;
1498c2ecf20Sopenharmony_ci		tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
1508c2ecf20Sopenharmony_ci		spin_unlock_bh(&tp_vars->cwnd_lock);
1518c2ecf20Sopenharmony_ci		return;
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* increment CWND at least of 1 (section 3.1 of RFC5681) */
1558c2ecf20Sopenharmony_ci	tp_vars->dec_cwnd += max_t(u32, 1U << 3,
1568c2ecf20Sopenharmony_ci				   ((mss * mss) << 6) / (tp_vars->cwnd << 3));
1578c2ecf20Sopenharmony_ci	if (tp_vars->dec_cwnd < (mss << 3)) {
1588c2ecf20Sopenharmony_ci		spin_unlock_bh(&tp_vars->cwnd_lock);
1598c2ecf20Sopenharmony_ci		return;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd, mss, mss);
1638c2ecf20Sopenharmony_ci	tp_vars->dec_cwnd = 0;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->cwnd_lock);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci/**
1698c2ecf20Sopenharmony_ci * batadv_tp_update_rto() - calculate new retransmission timeout
1708c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
1718c2ecf20Sopenharmony_ci * @new_rtt: new roundtrip time in msec
1728c2ecf20Sopenharmony_ci */
1738c2ecf20Sopenharmony_cistatic void batadv_tp_update_rto(struct batadv_tp_vars *tp_vars,
1748c2ecf20Sopenharmony_ci				 u32 new_rtt)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	long m = new_rtt;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/* RTT update
1798c2ecf20Sopenharmony_ci	 * Details in Section 2.2 and 2.3 of RFC6298
1808c2ecf20Sopenharmony_ci	 *
1818c2ecf20Sopenharmony_ci	 * It's tricky to understand. Don't lose hair please.
1828c2ecf20Sopenharmony_ci	 * Inspired by tcp_rtt_estimator() tcp_input.c
1838c2ecf20Sopenharmony_ci	 */
1848c2ecf20Sopenharmony_ci	if (tp_vars->srtt != 0) {
1858c2ecf20Sopenharmony_ci		m -= (tp_vars->srtt >> 3); /* m is now error in rtt est */
1868c2ecf20Sopenharmony_ci		tp_vars->srtt += m; /* rtt = 7/8 srtt + 1/8 new */
1878c2ecf20Sopenharmony_ci		if (m < 0)
1888c2ecf20Sopenharmony_ci			m = -m;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci		m -= (tp_vars->rttvar >> 2);
1918c2ecf20Sopenharmony_ci		tp_vars->rttvar += m; /* mdev ~= 3/4 rttvar + 1/4 new */
1928c2ecf20Sopenharmony_ci	} else {
1938c2ecf20Sopenharmony_ci		/* first measure getting in */
1948c2ecf20Sopenharmony_ci		tp_vars->srtt = m << 3;	/* take the measured time to be srtt */
1958c2ecf20Sopenharmony_ci		tp_vars->rttvar = m << 1; /* new_rtt / 2 */
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/* rto = srtt + 4 * rttvar.
1998c2ecf20Sopenharmony_ci	 * rttvar is scaled by 4, therefore doesn't need to be multiplied
2008c2ecf20Sopenharmony_ci	 */
2018c2ecf20Sopenharmony_ci	tp_vars->rto = (tp_vars->srtt >> 3) + tp_vars->rttvar;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/**
2058c2ecf20Sopenharmony_ci * batadv_tp_batctl_notify() - send client status result to client
2068c2ecf20Sopenharmony_ci * @reason: reason for tp meter session stop
2078c2ecf20Sopenharmony_ci * @dst: destination of tp_meter session
2088c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
2098c2ecf20Sopenharmony_ci * @start_time: start of transmission in jiffies
2108c2ecf20Sopenharmony_ci * @total_sent: bytes acked to the receiver
2118c2ecf20Sopenharmony_ci * @cookie: cookie of tp_meter session
2128c2ecf20Sopenharmony_ci */
2138c2ecf20Sopenharmony_cistatic void batadv_tp_batctl_notify(enum batadv_tp_meter_reason reason,
2148c2ecf20Sopenharmony_ci				    const u8 *dst, struct batadv_priv *bat_priv,
2158c2ecf20Sopenharmony_ci				    unsigned long start_time, u64 total_sent,
2168c2ecf20Sopenharmony_ci				    u32 cookie)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	u32 test_time;
2198c2ecf20Sopenharmony_ci	u8 result;
2208c2ecf20Sopenharmony_ci	u32 total_bytes;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if (!batadv_tp_is_error(reason)) {
2238c2ecf20Sopenharmony_ci		result = BATADV_TP_REASON_COMPLETE;
2248c2ecf20Sopenharmony_ci		test_time = jiffies_to_msecs(jiffies - start_time);
2258c2ecf20Sopenharmony_ci		total_bytes = total_sent;
2268c2ecf20Sopenharmony_ci	} else {
2278c2ecf20Sopenharmony_ci		result = reason;
2288c2ecf20Sopenharmony_ci		test_time = 0;
2298c2ecf20Sopenharmony_ci		total_bytes = 0;
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	batadv_netlink_tpmeter_notify(bat_priv, dst, result, test_time,
2338c2ecf20Sopenharmony_ci				      total_bytes, cookie);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/**
2378c2ecf20Sopenharmony_ci * batadv_tp_batctl_error_notify() - send client error result to client
2388c2ecf20Sopenharmony_ci * @reason: reason for tp meter session stop
2398c2ecf20Sopenharmony_ci * @dst: destination of tp_meter session
2408c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
2418c2ecf20Sopenharmony_ci * @cookie: cookie of tp_meter session
2428c2ecf20Sopenharmony_ci */
2438c2ecf20Sopenharmony_cistatic void batadv_tp_batctl_error_notify(enum batadv_tp_meter_reason reason,
2448c2ecf20Sopenharmony_ci					  const u8 *dst,
2458c2ecf20Sopenharmony_ci					  struct batadv_priv *bat_priv,
2468c2ecf20Sopenharmony_ci					  u32 cookie)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	batadv_tp_batctl_notify(reason, dst, bat_priv, 0, 0, cookie);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/**
2528c2ecf20Sopenharmony_ci * batadv_tp_list_find() - find a tp_vars object in the global list
2538c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
2548c2ecf20Sopenharmony_ci * @dst: the other endpoint MAC address to look for
2558c2ecf20Sopenharmony_ci *
2568c2ecf20Sopenharmony_ci * Look for a tp_vars object matching dst as end_point and return it after
2578c2ecf20Sopenharmony_ci * having increment the refcounter. Return NULL is not found
2588c2ecf20Sopenharmony_ci *
2598c2ecf20Sopenharmony_ci * Return: matching tp_vars or NULL when no tp_vars with @dst was found
2608c2ecf20Sopenharmony_ci */
2618c2ecf20Sopenharmony_cistatic struct batadv_tp_vars *batadv_tp_list_find(struct batadv_priv *bat_priv,
2628c2ecf20Sopenharmony_ci						  const u8 *dst)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	struct batadv_tp_vars *pos, *tp_vars = NULL;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	rcu_read_lock();
2678c2ecf20Sopenharmony_ci	hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
2688c2ecf20Sopenharmony_ci		if (!batadv_compare_eth(pos->other_end, dst))
2698c2ecf20Sopenharmony_ci			continue;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		/* most of the time this function is invoked during the normal
2728c2ecf20Sopenharmony_ci		 * process..it makes sens to pay more when the session is
2738c2ecf20Sopenharmony_ci		 * finished and to speed the process up during the measurement
2748c2ecf20Sopenharmony_ci		 */
2758c2ecf20Sopenharmony_ci		if (unlikely(!kref_get_unless_zero(&pos->refcount)))
2768c2ecf20Sopenharmony_ci			continue;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci		tp_vars = pos;
2798c2ecf20Sopenharmony_ci		break;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci	rcu_read_unlock();
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	return tp_vars;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci/**
2878c2ecf20Sopenharmony_ci * batadv_tp_list_find_session() - find tp_vars session object in the global
2888c2ecf20Sopenharmony_ci *  list
2898c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
2908c2ecf20Sopenharmony_ci * @dst: the other endpoint MAC address to look for
2918c2ecf20Sopenharmony_ci * @session: session identifier
2928c2ecf20Sopenharmony_ci *
2938c2ecf20Sopenharmony_ci * Look for a tp_vars object matching dst as end_point, session as tp meter
2948c2ecf20Sopenharmony_ci * session and return it after having increment the refcounter. Return NULL
2958c2ecf20Sopenharmony_ci * is not found
2968c2ecf20Sopenharmony_ci *
2978c2ecf20Sopenharmony_ci * Return: matching tp_vars or NULL when no tp_vars was found
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistatic struct batadv_tp_vars *
3008c2ecf20Sopenharmony_cibatadv_tp_list_find_session(struct batadv_priv *bat_priv, const u8 *dst,
3018c2ecf20Sopenharmony_ci			    const u8 *session)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	struct batadv_tp_vars *pos, *tp_vars = NULL;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	rcu_read_lock();
3068c2ecf20Sopenharmony_ci	hlist_for_each_entry_rcu(pos, &bat_priv->tp_list, list) {
3078c2ecf20Sopenharmony_ci		if (!batadv_compare_eth(pos->other_end, dst))
3088c2ecf20Sopenharmony_ci			continue;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci		if (memcmp(pos->session, session, sizeof(pos->session)) != 0)
3118c2ecf20Sopenharmony_ci			continue;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci		/* most of the time this function is invoked during the normal
3148c2ecf20Sopenharmony_ci		 * process..it makes sense to pay more when the session is
3158c2ecf20Sopenharmony_ci		 * finished and to speed the process up during the measurement
3168c2ecf20Sopenharmony_ci		 */
3178c2ecf20Sopenharmony_ci		if (unlikely(!kref_get_unless_zero(&pos->refcount)))
3188c2ecf20Sopenharmony_ci			continue;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci		tp_vars = pos;
3218c2ecf20Sopenharmony_ci		break;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci	rcu_read_unlock();
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	return tp_vars;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci/**
3298c2ecf20Sopenharmony_ci * batadv_tp_vars_release() - release batadv_tp_vars from lists and queue for
3308c2ecf20Sopenharmony_ci *  free after rcu grace period
3318c2ecf20Sopenharmony_ci * @ref: kref pointer of the batadv_tp_vars
3328c2ecf20Sopenharmony_ci */
3338c2ecf20Sopenharmony_cistatic void batadv_tp_vars_release(struct kref *ref)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
3368c2ecf20Sopenharmony_ci	struct batadv_tp_unacked *un, *safe;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	tp_vars = container_of(ref, struct batadv_tp_vars, refcount);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	/* lock should not be needed because this object is now out of any
3418c2ecf20Sopenharmony_ci	 * context!
3428c2ecf20Sopenharmony_ci	 */
3438c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
3448c2ecf20Sopenharmony_ci	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
3458c2ecf20Sopenharmony_ci		list_del(&un->list);
3468c2ecf20Sopenharmony_ci		kfree(un);
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	kfree_rcu(tp_vars, rcu);
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci/**
3548c2ecf20Sopenharmony_ci * batadv_tp_vars_put() - decrement the batadv_tp_vars refcounter and possibly
3558c2ecf20Sopenharmony_ci *  release it
3568c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session to be free'd
3578c2ecf20Sopenharmony_ci */
3588c2ecf20Sopenharmony_cistatic void batadv_tp_vars_put(struct batadv_tp_vars *tp_vars)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	if (!tp_vars)
3618c2ecf20Sopenharmony_ci		return;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	kref_put(&tp_vars->refcount, batadv_tp_vars_release);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci/**
3678c2ecf20Sopenharmony_ci * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer
3688c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
3698c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session to cleanup
3708c2ecf20Sopenharmony_ci */
3718c2ecf20Sopenharmony_cistatic void batadv_tp_sender_cleanup(struct batadv_priv *bat_priv,
3728c2ecf20Sopenharmony_ci				     struct batadv_tp_vars *tp_vars)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	cancel_delayed_work(&tp_vars->finish_work);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
3778c2ecf20Sopenharmony_ci	hlist_del_rcu(&tp_vars->list);
3788c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/* drop list reference */
3818c2ecf20Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	atomic_dec(&tp_vars->bat_priv->tp_num);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/* kill the timer and remove its reference */
3868c2ecf20Sopenharmony_ci	del_timer_sync(&tp_vars->timer);
3878c2ecf20Sopenharmony_ci	/* the worker might have rearmed itself therefore we kill it again. Note
3888c2ecf20Sopenharmony_ci	 * that if the worker should run again before invoking the following
3898c2ecf20Sopenharmony_ci	 * del_timer(), it would not re-arm itself once again because the status
3908c2ecf20Sopenharmony_ci	 * is OFF now
3918c2ecf20Sopenharmony_ci	 */
3928c2ecf20Sopenharmony_ci	del_timer(&tp_vars->timer);
3938c2ecf20Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/**
3978c2ecf20Sopenharmony_ci * batadv_tp_sender_end() - print info about ended session and inform client
3988c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
3998c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
4008c2ecf20Sopenharmony_ci */
4018c2ecf20Sopenharmony_cistatic void batadv_tp_sender_end(struct batadv_priv *bat_priv,
4028c2ecf20Sopenharmony_ci				 struct batadv_tp_vars *tp_vars)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	u32 session_cookie;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
4078c2ecf20Sopenharmony_ci		   "Test towards %pM finished..shutting down (reason=%d)\n",
4088c2ecf20Sopenharmony_ci		   tp_vars->other_end, tp_vars->reason);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
4118c2ecf20Sopenharmony_ci		   "Last timing stats: SRTT=%ums RTTVAR=%ums RTO=%ums\n",
4128c2ecf20Sopenharmony_ci		   tp_vars->srtt >> 3, tp_vars->rttvar >> 2, tp_vars->rto);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
4158c2ecf20Sopenharmony_ci		   "Final values: cwnd=%u ss_threshold=%u\n",
4168c2ecf20Sopenharmony_ci		   tp_vars->cwnd, tp_vars->ss_threshold);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	session_cookie = batadv_tp_session_cookie(tp_vars->session,
4198c2ecf20Sopenharmony_ci						  tp_vars->icmp_uid);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	batadv_tp_batctl_notify(tp_vars->reason,
4228c2ecf20Sopenharmony_ci				tp_vars->other_end,
4238c2ecf20Sopenharmony_ci				bat_priv,
4248c2ecf20Sopenharmony_ci				tp_vars->start_time,
4258c2ecf20Sopenharmony_ci				atomic64_read(&tp_vars->tot_sent),
4268c2ecf20Sopenharmony_ci				session_cookie);
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci/**
4308c2ecf20Sopenharmony_ci * batadv_tp_sender_shutdown() - let sender thread/timer stop gracefully
4318c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
4328c2ecf20Sopenharmony_ci * @reason: reason for tp meter session stop
4338c2ecf20Sopenharmony_ci */
4348c2ecf20Sopenharmony_cistatic void batadv_tp_sender_shutdown(struct batadv_tp_vars *tp_vars,
4358c2ecf20Sopenharmony_ci				      enum batadv_tp_meter_reason reason)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	if (!atomic_dec_and_test(&tp_vars->sending))
4388c2ecf20Sopenharmony_ci		return;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	tp_vars->reason = reason;
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci/**
4448c2ecf20Sopenharmony_ci * batadv_tp_sender_finish() - stop sender session after test_length was reached
4458c2ecf20Sopenharmony_ci * @work: delayed work reference of the related tp_vars
4468c2ecf20Sopenharmony_ci */
4478c2ecf20Sopenharmony_cistatic void batadv_tp_sender_finish(struct work_struct *work)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	struct delayed_work *delayed_work;
4508c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	delayed_work = to_delayed_work(work);
4538c2ecf20Sopenharmony_ci	tp_vars = container_of(delayed_work, struct batadv_tp_vars,
4548c2ecf20Sopenharmony_ci			       finish_work);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	batadv_tp_sender_shutdown(tp_vars, BATADV_TP_REASON_COMPLETE);
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci/**
4608c2ecf20Sopenharmony_ci * batadv_tp_reset_sender_timer() - reschedule the sender timer
4618c2ecf20Sopenharmony_ci * @tp_vars: the private TP meter data for this session
4628c2ecf20Sopenharmony_ci *
4638c2ecf20Sopenharmony_ci * Reschedule the timer using tp_vars->rto as delay
4648c2ecf20Sopenharmony_ci */
4658c2ecf20Sopenharmony_cistatic void batadv_tp_reset_sender_timer(struct batadv_tp_vars *tp_vars)
4668c2ecf20Sopenharmony_ci{
4678c2ecf20Sopenharmony_ci	/* most of the time this function is invoked while normal packet
4688c2ecf20Sopenharmony_ci	 * reception...
4698c2ecf20Sopenharmony_ci	 */
4708c2ecf20Sopenharmony_ci	if (unlikely(atomic_read(&tp_vars->sending) == 0))
4718c2ecf20Sopenharmony_ci		/* timer ref will be dropped in batadv_tp_sender_cleanup */
4728c2ecf20Sopenharmony_ci		return;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	mod_timer(&tp_vars->timer, jiffies + msecs_to_jiffies(tp_vars->rto));
4758c2ecf20Sopenharmony_ci}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci/**
4788c2ecf20Sopenharmony_ci * batadv_tp_sender_timeout() - timer that fires in case of packet loss
4798c2ecf20Sopenharmony_ci * @t: address to timer_list inside tp_vars
4808c2ecf20Sopenharmony_ci *
4818c2ecf20Sopenharmony_ci * If fired it means that there was packet loss.
4828c2ecf20Sopenharmony_ci * Switch to Slow Start, set the ss_threshold to half of the current cwnd and
4838c2ecf20Sopenharmony_ci * reset the cwnd to 3*MSS
4848c2ecf20Sopenharmony_ci */
4858c2ecf20Sopenharmony_cistatic void batadv_tp_sender_timeout(struct timer_list *t)
4868c2ecf20Sopenharmony_ci{
4878c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
4888c2ecf20Sopenharmony_ci	struct batadv_priv *bat_priv = tp_vars->bat_priv;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (atomic_read(&tp_vars->sending) == 0)
4918c2ecf20Sopenharmony_ci		return;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	/* if the user waited long enough...shutdown the test */
4948c2ecf20Sopenharmony_ci	if (unlikely(tp_vars->rto >= BATADV_TP_MAX_RTO)) {
4958c2ecf20Sopenharmony_ci		batadv_tp_sender_shutdown(tp_vars,
4968c2ecf20Sopenharmony_ci					  BATADV_TP_REASON_DST_UNREACHABLE);
4978c2ecf20Sopenharmony_ci		return;
4988c2ecf20Sopenharmony_ci	}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	/* RTO exponential backoff
5018c2ecf20Sopenharmony_ci	 * Details in Section 5.5 of RFC6298
5028c2ecf20Sopenharmony_ci	 */
5038c2ecf20Sopenharmony_ci	tp_vars->rto <<= 1;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->cwnd_lock);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	tp_vars->ss_threshold = tp_vars->cwnd >> 1;
5088c2ecf20Sopenharmony_ci	if (tp_vars->ss_threshold < BATADV_TP_PLEN * 2)
5098c2ecf20Sopenharmony_ci		tp_vars->ss_threshold = BATADV_TP_PLEN * 2;
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
5128c2ecf20Sopenharmony_ci		   "Meter: RTO fired during test towards %pM! cwnd=%u new ss_thr=%u, resetting last_sent to %u\n",
5138c2ecf20Sopenharmony_ci		   tp_vars->other_end, tp_vars->cwnd, tp_vars->ss_threshold,
5148c2ecf20Sopenharmony_ci		   atomic_read(&tp_vars->last_acked));
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	tp_vars->cwnd = BATADV_TP_PLEN * 3;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->cwnd_lock);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	/* resend the non-ACKed packets.. */
5218c2ecf20Sopenharmony_ci	tp_vars->last_sent = atomic_read(&tp_vars->last_acked);
5228c2ecf20Sopenharmony_ci	wake_up(&tp_vars->more_bytes);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	batadv_tp_reset_sender_timer(tp_vars);
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci/**
5288c2ecf20Sopenharmony_ci * batadv_tp_fill_prerandom() - Fill buffer with prefetched random bytes
5298c2ecf20Sopenharmony_ci * @tp_vars: the private TP meter data for this session
5308c2ecf20Sopenharmony_ci * @buf: Buffer to fill with bytes
5318c2ecf20Sopenharmony_ci * @nbytes: amount of pseudorandom bytes
5328c2ecf20Sopenharmony_ci */
5338c2ecf20Sopenharmony_cistatic void batadv_tp_fill_prerandom(struct batadv_tp_vars *tp_vars,
5348c2ecf20Sopenharmony_ci				     u8 *buf, size_t nbytes)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	u32 local_offset;
5378c2ecf20Sopenharmony_ci	size_t bytes_inbuf;
5388c2ecf20Sopenharmony_ci	size_t to_copy;
5398c2ecf20Sopenharmony_ci	size_t pos = 0;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->prerandom_lock);
5428c2ecf20Sopenharmony_ci	local_offset = tp_vars->prerandom_offset;
5438c2ecf20Sopenharmony_ci	tp_vars->prerandom_offset += nbytes;
5448c2ecf20Sopenharmony_ci	tp_vars->prerandom_offset %= sizeof(batadv_tp_prerandom);
5458c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->prerandom_lock);
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	while (nbytes) {
5488c2ecf20Sopenharmony_ci		local_offset %= sizeof(batadv_tp_prerandom);
5498c2ecf20Sopenharmony_ci		bytes_inbuf = sizeof(batadv_tp_prerandom) - local_offset;
5508c2ecf20Sopenharmony_ci		to_copy = min(nbytes, bytes_inbuf);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci		memcpy(&buf[pos], &batadv_tp_prerandom[local_offset], to_copy);
5538c2ecf20Sopenharmony_ci		pos += to_copy;
5548c2ecf20Sopenharmony_ci		nbytes -= to_copy;
5558c2ecf20Sopenharmony_ci		local_offset = 0;
5568c2ecf20Sopenharmony_ci	}
5578c2ecf20Sopenharmony_ci}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci/**
5608c2ecf20Sopenharmony_ci * batadv_tp_send_msg() - send a single message
5618c2ecf20Sopenharmony_ci * @tp_vars: the private TP meter data for this session
5628c2ecf20Sopenharmony_ci * @src: source mac address
5638c2ecf20Sopenharmony_ci * @orig_node: the originator of the destination
5648c2ecf20Sopenharmony_ci * @seqno: sequence number of this packet
5658c2ecf20Sopenharmony_ci * @len: length of the entire packet
5668c2ecf20Sopenharmony_ci * @session: session identifier
5678c2ecf20Sopenharmony_ci * @uid: local ICMP "socket" index
5688c2ecf20Sopenharmony_ci * @timestamp: timestamp in jiffies which is replied in ack
5698c2ecf20Sopenharmony_ci *
5708c2ecf20Sopenharmony_ci * Create and send a single TP Meter message.
5718c2ecf20Sopenharmony_ci *
5728c2ecf20Sopenharmony_ci * Return: 0 on success, BATADV_TP_REASON_DST_UNREACHABLE if the destination is
5738c2ecf20Sopenharmony_ci * not reachable, BATADV_TP_REASON_MEMORY_ERROR if the packet couldn't be
5748c2ecf20Sopenharmony_ci * allocated
5758c2ecf20Sopenharmony_ci */
5768c2ecf20Sopenharmony_cistatic int batadv_tp_send_msg(struct batadv_tp_vars *tp_vars, const u8 *src,
5778c2ecf20Sopenharmony_ci			      struct batadv_orig_node *orig_node,
5788c2ecf20Sopenharmony_ci			      u32 seqno, size_t len, const u8 *session,
5798c2ecf20Sopenharmony_ci			      int uid, u32 timestamp)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	struct batadv_icmp_tp_packet *icmp;
5828c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5838c2ecf20Sopenharmony_ci	int r;
5848c2ecf20Sopenharmony_ci	u8 *data;
5858c2ecf20Sopenharmony_ci	size_t data_len;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
5888c2ecf20Sopenharmony_ci	if (unlikely(!skb))
5898c2ecf20Sopenharmony_ci		return BATADV_TP_REASON_MEMORY_ERROR;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	skb_reserve(skb, ETH_HLEN);
5928c2ecf20Sopenharmony_ci	icmp = skb_put(skb, sizeof(*icmp));
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	/* fill the icmp header */
5958c2ecf20Sopenharmony_ci	ether_addr_copy(icmp->dst, orig_node->orig);
5968c2ecf20Sopenharmony_ci	ether_addr_copy(icmp->orig, src);
5978c2ecf20Sopenharmony_ci	icmp->version = BATADV_COMPAT_VERSION;
5988c2ecf20Sopenharmony_ci	icmp->packet_type = BATADV_ICMP;
5998c2ecf20Sopenharmony_ci	icmp->ttl = BATADV_TTL;
6008c2ecf20Sopenharmony_ci	icmp->msg_type = BATADV_TP;
6018c2ecf20Sopenharmony_ci	icmp->uid = uid;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	icmp->subtype = BATADV_TP_MSG;
6048c2ecf20Sopenharmony_ci	memcpy(icmp->session, session, sizeof(icmp->session));
6058c2ecf20Sopenharmony_ci	icmp->seqno = htonl(seqno);
6068c2ecf20Sopenharmony_ci	icmp->timestamp = htonl(timestamp);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	data_len = len - sizeof(*icmp);
6098c2ecf20Sopenharmony_ci	data = skb_put(skb, data_len);
6108c2ecf20Sopenharmony_ci	batadv_tp_fill_prerandom(tp_vars, data, data_len);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	r = batadv_send_skb_to_orig(skb, orig_node, NULL);
6138c2ecf20Sopenharmony_ci	if (r == NET_XMIT_SUCCESS)
6148c2ecf20Sopenharmony_ci		return 0;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	return BATADV_TP_REASON_CANT_SEND;
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci/**
6208c2ecf20Sopenharmony_ci * batadv_tp_recv_ack() - ACK receiving function
6218c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
6228c2ecf20Sopenharmony_ci * @skb: the buffer containing the received packet
6238c2ecf20Sopenharmony_ci *
6248c2ecf20Sopenharmony_ci * Process a received TP ACK packet
6258c2ecf20Sopenharmony_ci */
6268c2ecf20Sopenharmony_cistatic void batadv_tp_recv_ack(struct batadv_priv *bat_priv,
6278c2ecf20Sopenharmony_ci			       const struct sk_buff *skb)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	struct batadv_hard_iface *primary_if = NULL;
6308c2ecf20Sopenharmony_ci	struct batadv_orig_node *orig_node = NULL;
6318c2ecf20Sopenharmony_ci	const struct batadv_icmp_tp_packet *icmp;
6328c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
6338c2ecf20Sopenharmony_ci	size_t packet_len, mss;
6348c2ecf20Sopenharmony_ci	u32 rtt, recv_ack, cwnd;
6358c2ecf20Sopenharmony_ci	unsigned char *dev_addr;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	packet_len = BATADV_TP_PLEN;
6388c2ecf20Sopenharmony_ci	mss = BATADV_TP_PLEN;
6398c2ecf20Sopenharmony_ci	packet_len += sizeof(struct batadv_unicast_packet);
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	/* find the tp_vars */
6448c2ecf20Sopenharmony_ci	tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
6458c2ecf20Sopenharmony_ci					      icmp->session);
6468c2ecf20Sopenharmony_ci	if (unlikely(!tp_vars))
6478c2ecf20Sopenharmony_ci		return;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	if (unlikely(atomic_read(&tp_vars->sending) == 0))
6508c2ecf20Sopenharmony_ci		goto out;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	/* old ACK? silently drop it.. */
6538c2ecf20Sopenharmony_ci	if (batadv_seq_before(ntohl(icmp->seqno),
6548c2ecf20Sopenharmony_ci			      (u32)atomic_read(&tp_vars->last_acked)))
6558c2ecf20Sopenharmony_ci		goto out;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
6588c2ecf20Sopenharmony_ci	if (unlikely(!primary_if))
6598c2ecf20Sopenharmony_ci		goto out;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, icmp->orig);
6628c2ecf20Sopenharmony_ci	if (unlikely(!orig_node))
6638c2ecf20Sopenharmony_ci		goto out;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	/* update RTO with the new sampled RTT, if any */
6668c2ecf20Sopenharmony_ci	rtt = jiffies_to_msecs(jiffies) - ntohl(icmp->timestamp);
6678c2ecf20Sopenharmony_ci	if (icmp->timestamp && rtt)
6688c2ecf20Sopenharmony_ci		batadv_tp_update_rto(tp_vars, rtt);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	/* ACK for new data... reset the timer */
6718c2ecf20Sopenharmony_ci	batadv_tp_reset_sender_timer(tp_vars);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	recv_ack = ntohl(icmp->seqno);
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	/* check if this ACK is a duplicate */
6768c2ecf20Sopenharmony_ci	if (atomic_read(&tp_vars->last_acked) == recv_ack) {
6778c2ecf20Sopenharmony_ci		atomic_inc(&tp_vars->dup_acks);
6788c2ecf20Sopenharmony_ci		if (atomic_read(&tp_vars->dup_acks) != 3)
6798c2ecf20Sopenharmony_ci			goto out;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		if (recv_ack >= tp_vars->recover)
6828c2ecf20Sopenharmony_ci			goto out;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci		/* if this is the third duplicate ACK do Fast Retransmit */
6858c2ecf20Sopenharmony_ci		batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
6868c2ecf20Sopenharmony_ci				   orig_node, recv_ack, packet_len,
6878c2ecf20Sopenharmony_ci				   icmp->session, icmp->uid,
6888c2ecf20Sopenharmony_ci				   jiffies_to_msecs(jiffies));
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci		spin_lock_bh(&tp_vars->cwnd_lock);
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci		/* Fast Recovery */
6938c2ecf20Sopenharmony_ci		tp_vars->fast_recovery = true;
6948c2ecf20Sopenharmony_ci		/* Set recover to the last outstanding seqno when Fast Recovery
6958c2ecf20Sopenharmony_ci		 * is entered. RFC6582, Section 3.2, step 1
6968c2ecf20Sopenharmony_ci		 */
6978c2ecf20Sopenharmony_ci		tp_vars->recover = tp_vars->last_sent;
6988c2ecf20Sopenharmony_ci		tp_vars->ss_threshold = tp_vars->cwnd >> 1;
6998c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
7008c2ecf20Sopenharmony_ci			   "Meter: Fast Recovery, (cur cwnd=%u) ss_thr=%u last_sent=%u recv_ack=%u\n",
7018c2ecf20Sopenharmony_ci			   tp_vars->cwnd, tp_vars->ss_threshold,
7028c2ecf20Sopenharmony_ci			   tp_vars->last_sent, recv_ack);
7038c2ecf20Sopenharmony_ci		tp_vars->cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 3 * mss,
7048c2ecf20Sopenharmony_ci					       mss);
7058c2ecf20Sopenharmony_ci		tp_vars->dec_cwnd = 0;
7068c2ecf20Sopenharmony_ci		tp_vars->last_sent = recv_ack;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci		spin_unlock_bh(&tp_vars->cwnd_lock);
7098c2ecf20Sopenharmony_ci	} else {
7108c2ecf20Sopenharmony_ci		/* count the acked data */
7118c2ecf20Sopenharmony_ci		atomic64_add(recv_ack - atomic_read(&tp_vars->last_acked),
7128c2ecf20Sopenharmony_ci			     &tp_vars->tot_sent);
7138c2ecf20Sopenharmony_ci		/* reset the duplicate ACKs counter */
7148c2ecf20Sopenharmony_ci		atomic_set(&tp_vars->dup_acks, 0);
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci		if (tp_vars->fast_recovery) {
7178c2ecf20Sopenharmony_ci			/* partial ACK */
7188c2ecf20Sopenharmony_ci			if (batadv_seq_before(recv_ack, tp_vars->recover)) {
7198c2ecf20Sopenharmony_ci				/* this is another hole in the window. React
7208c2ecf20Sopenharmony_ci				 * immediately as specified by NewReno (see
7218c2ecf20Sopenharmony_ci				 * Section 3.2 of RFC6582 for details)
7228c2ecf20Sopenharmony_ci				 */
7238c2ecf20Sopenharmony_ci				dev_addr = primary_if->net_dev->dev_addr;
7248c2ecf20Sopenharmony_ci				batadv_tp_send_msg(tp_vars, dev_addr,
7258c2ecf20Sopenharmony_ci						   orig_node, recv_ack,
7268c2ecf20Sopenharmony_ci						   packet_len, icmp->session,
7278c2ecf20Sopenharmony_ci						   icmp->uid,
7288c2ecf20Sopenharmony_ci						   jiffies_to_msecs(jiffies));
7298c2ecf20Sopenharmony_ci				tp_vars->cwnd = batadv_tp_cwnd(tp_vars->cwnd,
7308c2ecf20Sopenharmony_ci							       mss, mss);
7318c2ecf20Sopenharmony_ci			} else {
7328c2ecf20Sopenharmony_ci				tp_vars->fast_recovery = false;
7338c2ecf20Sopenharmony_ci				/* set cwnd to the value of ss_threshold at the
7348c2ecf20Sopenharmony_ci				 * moment that Fast Recovery was entered.
7358c2ecf20Sopenharmony_ci				 * RFC6582, Section 3.2, step 3
7368c2ecf20Sopenharmony_ci				 */
7378c2ecf20Sopenharmony_ci				cwnd = batadv_tp_cwnd(tp_vars->ss_threshold, 0,
7388c2ecf20Sopenharmony_ci						      mss);
7398c2ecf20Sopenharmony_ci				tp_vars->cwnd = cwnd;
7408c2ecf20Sopenharmony_ci			}
7418c2ecf20Sopenharmony_ci			goto move_twnd;
7428c2ecf20Sopenharmony_ci		}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		if (recv_ack - atomic_read(&tp_vars->last_acked) >= mss)
7458c2ecf20Sopenharmony_ci			batadv_tp_update_cwnd(tp_vars, mss);
7468c2ecf20Sopenharmony_cimove_twnd:
7478c2ecf20Sopenharmony_ci		/* move the Transmit Window */
7488c2ecf20Sopenharmony_ci		atomic_set(&tp_vars->last_acked, recv_ack);
7498c2ecf20Sopenharmony_ci	}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	wake_up(&tp_vars->more_bytes);
7528c2ecf20Sopenharmony_ciout:
7538c2ecf20Sopenharmony_ci	if (likely(primary_if))
7548c2ecf20Sopenharmony_ci		batadv_hardif_put(primary_if);
7558c2ecf20Sopenharmony_ci	if (likely(orig_node))
7568c2ecf20Sopenharmony_ci		batadv_orig_node_put(orig_node);
7578c2ecf20Sopenharmony_ci	if (likely(tp_vars))
7588c2ecf20Sopenharmony_ci		batadv_tp_vars_put(tp_vars);
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci/**
7628c2ecf20Sopenharmony_ci * batadv_tp_avail() - check if congestion window is not full
7638c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
7648c2ecf20Sopenharmony_ci * @payload_len: size of the payload of a single message
7658c2ecf20Sopenharmony_ci *
7668c2ecf20Sopenharmony_ci * Return: true when congestion window is not full, false otherwise
7678c2ecf20Sopenharmony_ci */
7688c2ecf20Sopenharmony_cistatic bool batadv_tp_avail(struct batadv_tp_vars *tp_vars,
7698c2ecf20Sopenharmony_ci			    size_t payload_len)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	u32 win_left, win_limit;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	win_limit = atomic_read(&tp_vars->last_acked) + tp_vars->cwnd;
7748c2ecf20Sopenharmony_ci	win_left = win_limit - tp_vars->last_sent;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	return win_left >= payload_len;
7778c2ecf20Sopenharmony_ci}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci/**
7808c2ecf20Sopenharmony_ci * batadv_tp_wait_available() - wait until congestion window becomes free or
7818c2ecf20Sopenharmony_ci *  timeout is reached
7828c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
7838c2ecf20Sopenharmony_ci * @plen: size of the payload of a single message
7848c2ecf20Sopenharmony_ci *
7858c2ecf20Sopenharmony_ci * Return: 0 if the condition evaluated to false after the timeout elapsed,
7868c2ecf20Sopenharmony_ci *  1 if the condition evaluated to true after the timeout elapsed, the
7878c2ecf20Sopenharmony_ci *  remaining jiffies (at least 1) if the condition evaluated to true before
7888c2ecf20Sopenharmony_ci *  the timeout elapsed, or -ERESTARTSYS if it was interrupted by a signal.
7898c2ecf20Sopenharmony_ci */
7908c2ecf20Sopenharmony_cistatic int batadv_tp_wait_available(struct batadv_tp_vars *tp_vars, size_t plen)
7918c2ecf20Sopenharmony_ci{
7928c2ecf20Sopenharmony_ci	int ret;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	ret = wait_event_interruptible_timeout(tp_vars->more_bytes,
7958c2ecf20Sopenharmony_ci					       batadv_tp_avail(tp_vars, plen),
7968c2ecf20Sopenharmony_ci					       HZ / 10);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return ret;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci/**
8028c2ecf20Sopenharmony_ci * batadv_tp_send() - main sending thread of a tp meter session
8038c2ecf20Sopenharmony_ci * @arg: address of the related tp_vars
8048c2ecf20Sopenharmony_ci *
8058c2ecf20Sopenharmony_ci * Return: nothing, this function never returns
8068c2ecf20Sopenharmony_ci */
8078c2ecf20Sopenharmony_cistatic int batadv_tp_send(void *arg)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars = arg;
8108c2ecf20Sopenharmony_ci	struct batadv_priv *bat_priv = tp_vars->bat_priv;
8118c2ecf20Sopenharmony_ci	struct batadv_hard_iface *primary_if = NULL;
8128c2ecf20Sopenharmony_ci	struct batadv_orig_node *orig_node = NULL;
8138c2ecf20Sopenharmony_ci	size_t payload_len, packet_len;
8148c2ecf20Sopenharmony_ci	int err = 0;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	if (unlikely(tp_vars->role != BATADV_TP_SENDER)) {
8178c2ecf20Sopenharmony_ci		err = BATADV_TP_REASON_DST_UNREACHABLE;
8188c2ecf20Sopenharmony_ci		tp_vars->reason = err;
8198c2ecf20Sopenharmony_ci		goto out;
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, tp_vars->other_end);
8238c2ecf20Sopenharmony_ci	if (unlikely(!orig_node)) {
8248c2ecf20Sopenharmony_ci		err = BATADV_TP_REASON_DST_UNREACHABLE;
8258c2ecf20Sopenharmony_ci		tp_vars->reason = err;
8268c2ecf20Sopenharmony_ci		goto out;
8278c2ecf20Sopenharmony_ci	}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
8308c2ecf20Sopenharmony_ci	if (unlikely(!primary_if)) {
8318c2ecf20Sopenharmony_ci		err = BATADV_TP_REASON_DST_UNREACHABLE;
8328c2ecf20Sopenharmony_ci		tp_vars->reason = err;
8338c2ecf20Sopenharmony_ci		goto out;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	/* assume that all the hard_interfaces have a correctly
8378c2ecf20Sopenharmony_ci	 * configured MTU, so use the soft_iface MTU as MSS.
8388c2ecf20Sopenharmony_ci	 * This might not be true and in that case the fragmentation
8398c2ecf20Sopenharmony_ci	 * should be used.
8408c2ecf20Sopenharmony_ci	 * Now, try to send the packet as it is
8418c2ecf20Sopenharmony_ci	 */
8428c2ecf20Sopenharmony_ci	payload_len = BATADV_TP_PLEN;
8438c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct batadv_icmp_tp_packet) > BATADV_TP_PLEN);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	batadv_tp_reset_sender_timer(tp_vars);
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	/* queue the worker in charge of terminating the test */
8488c2ecf20Sopenharmony_ci	queue_delayed_work(batadv_event_workqueue, &tp_vars->finish_work,
8498c2ecf20Sopenharmony_ci			   msecs_to_jiffies(tp_vars->test_length));
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	while (atomic_read(&tp_vars->sending) != 0) {
8528c2ecf20Sopenharmony_ci		if (unlikely(!batadv_tp_avail(tp_vars, payload_len))) {
8538c2ecf20Sopenharmony_ci			batadv_tp_wait_available(tp_vars, payload_len);
8548c2ecf20Sopenharmony_ci			continue;
8558c2ecf20Sopenharmony_ci		}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci		/* to emulate normal unicast traffic, add to the payload len
8588c2ecf20Sopenharmony_ci		 * the size of the unicast header
8598c2ecf20Sopenharmony_ci		 */
8608c2ecf20Sopenharmony_ci		packet_len = payload_len + sizeof(struct batadv_unicast_packet);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci		err = batadv_tp_send_msg(tp_vars, primary_if->net_dev->dev_addr,
8638c2ecf20Sopenharmony_ci					 orig_node, tp_vars->last_sent,
8648c2ecf20Sopenharmony_ci					 packet_len,
8658c2ecf20Sopenharmony_ci					 tp_vars->session, tp_vars->icmp_uid,
8668c2ecf20Sopenharmony_ci					 jiffies_to_msecs(jiffies));
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci		/* something went wrong during the preparation/transmission */
8698c2ecf20Sopenharmony_ci		if (unlikely(err && err != BATADV_TP_REASON_CANT_SEND)) {
8708c2ecf20Sopenharmony_ci			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
8718c2ecf20Sopenharmony_ci				   "Meter: %s() cannot send packets (%d)\n",
8728c2ecf20Sopenharmony_ci				   __func__, err);
8738c2ecf20Sopenharmony_ci			/* ensure nobody else tries to stop the thread now */
8748c2ecf20Sopenharmony_ci			if (atomic_dec_and_test(&tp_vars->sending))
8758c2ecf20Sopenharmony_ci				tp_vars->reason = err;
8768c2ecf20Sopenharmony_ci			break;
8778c2ecf20Sopenharmony_ci		}
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci		/* right-shift the TWND */
8808c2ecf20Sopenharmony_ci		if (!err)
8818c2ecf20Sopenharmony_ci			tp_vars->last_sent += payload_len;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci		cond_resched();
8848c2ecf20Sopenharmony_ci	}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ciout:
8878c2ecf20Sopenharmony_ci	if (likely(primary_if))
8888c2ecf20Sopenharmony_ci		batadv_hardif_put(primary_if);
8898c2ecf20Sopenharmony_ci	if (likely(orig_node))
8908c2ecf20Sopenharmony_ci		batadv_orig_node_put(orig_node);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	batadv_tp_sender_end(bat_priv, tp_vars);
8938c2ecf20Sopenharmony_ci	batadv_tp_sender_cleanup(bat_priv, tp_vars);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	do_exit(0);
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci/**
9018c2ecf20Sopenharmony_ci * batadv_tp_start_kthread() - start new thread which manages the tp meter
9028c2ecf20Sopenharmony_ci *  sender
9038c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
9048c2ecf20Sopenharmony_ci */
9058c2ecf20Sopenharmony_cistatic void batadv_tp_start_kthread(struct batadv_tp_vars *tp_vars)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	struct task_struct *kthread;
9088c2ecf20Sopenharmony_ci	struct batadv_priv *bat_priv = tp_vars->bat_priv;
9098c2ecf20Sopenharmony_ci	u32 session_cookie;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	kref_get(&tp_vars->refcount);
9128c2ecf20Sopenharmony_ci	kthread = kthread_create(batadv_tp_send, tp_vars, "kbatadv_tp_meter");
9138c2ecf20Sopenharmony_ci	if (IS_ERR(kthread)) {
9148c2ecf20Sopenharmony_ci		session_cookie = batadv_tp_session_cookie(tp_vars->session,
9158c2ecf20Sopenharmony_ci							  tp_vars->icmp_uid);
9168c2ecf20Sopenharmony_ci		pr_err("batadv: cannot create tp meter kthread\n");
9178c2ecf20Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
9188c2ecf20Sopenharmony_ci					      tp_vars->other_end,
9198c2ecf20Sopenharmony_ci					      bat_priv, session_cookie);
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci		/* drop reserved reference for kthread */
9228c2ecf20Sopenharmony_ci		batadv_tp_vars_put(tp_vars);
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci		/* cleanup of failed tp meter variables */
9258c2ecf20Sopenharmony_ci		batadv_tp_sender_cleanup(bat_priv, tp_vars);
9268c2ecf20Sopenharmony_ci		return;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	wake_up_process(kthread);
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci/**
9338c2ecf20Sopenharmony_ci * batadv_tp_start() - start a new tp meter session
9348c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
9358c2ecf20Sopenharmony_ci * @dst: the receiver MAC address
9368c2ecf20Sopenharmony_ci * @test_length: test length in milliseconds
9378c2ecf20Sopenharmony_ci * @cookie: session cookie
9388c2ecf20Sopenharmony_ci */
9398c2ecf20Sopenharmony_civoid batadv_tp_start(struct batadv_priv *bat_priv, const u8 *dst,
9408c2ecf20Sopenharmony_ci		     u32 test_length, u32 *cookie)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
9438c2ecf20Sopenharmony_ci	u8 session_id[2];
9448c2ecf20Sopenharmony_ci	u8 icmp_uid;
9458c2ecf20Sopenharmony_ci	u32 session_cookie;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	get_random_bytes(session_id, sizeof(session_id));
9488c2ecf20Sopenharmony_ci	get_random_bytes(&icmp_uid, 1);
9498c2ecf20Sopenharmony_ci	session_cookie = batadv_tp_session_cookie(session_id, icmp_uid);
9508c2ecf20Sopenharmony_ci	*cookie = session_cookie;
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	/* look for an already existing test towards this node */
9538c2ecf20Sopenharmony_ci	spin_lock_bh(&bat_priv->tp_list_lock);
9548c2ecf20Sopenharmony_ci	tp_vars = batadv_tp_list_find(bat_priv, dst);
9558c2ecf20Sopenharmony_ci	if (tp_vars) {
9568c2ecf20Sopenharmony_ci		spin_unlock_bh(&bat_priv->tp_list_lock);
9578c2ecf20Sopenharmony_ci		batadv_tp_vars_put(tp_vars);
9588c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
9598c2ecf20Sopenharmony_ci			   "Meter: test to or from the same node already ongoing, aborting\n");
9608c2ecf20Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_ALREADY_ONGOING,
9618c2ecf20Sopenharmony_ci					      dst, bat_priv, session_cookie);
9628c2ecf20Sopenharmony_ci		return;
9638c2ecf20Sopenharmony_ci	}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
9668c2ecf20Sopenharmony_ci		spin_unlock_bh(&bat_priv->tp_list_lock);
9678c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
9688c2ecf20Sopenharmony_ci			   "Meter: too many ongoing sessions, aborting (SEND)\n");
9698c2ecf20Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_TOO_MANY, dst,
9708c2ecf20Sopenharmony_ci					      bat_priv, session_cookie);
9718c2ecf20Sopenharmony_ci		return;
9728c2ecf20Sopenharmony_ci	}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
9758c2ecf20Sopenharmony_ci	if (!tp_vars) {
9768c2ecf20Sopenharmony_ci		spin_unlock_bh(&bat_priv->tp_list_lock);
9778c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
9788c2ecf20Sopenharmony_ci			   "Meter: %s cannot allocate list elements\n",
9798c2ecf20Sopenharmony_ci			   __func__);
9808c2ecf20Sopenharmony_ci		batadv_tp_batctl_error_notify(BATADV_TP_REASON_MEMORY_ERROR,
9818c2ecf20Sopenharmony_ci					      dst, bat_priv, session_cookie);
9828c2ecf20Sopenharmony_ci		return;
9838c2ecf20Sopenharmony_ci	}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	/* initialize tp_vars */
9868c2ecf20Sopenharmony_ci	ether_addr_copy(tp_vars->other_end, dst);
9878c2ecf20Sopenharmony_ci	kref_init(&tp_vars->refcount);
9888c2ecf20Sopenharmony_ci	tp_vars->role = BATADV_TP_SENDER;
9898c2ecf20Sopenharmony_ci	atomic_set(&tp_vars->sending, 1);
9908c2ecf20Sopenharmony_ci	memcpy(tp_vars->session, session_id, sizeof(session_id));
9918c2ecf20Sopenharmony_ci	tp_vars->icmp_uid = icmp_uid;
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	tp_vars->last_sent = BATADV_TP_FIRST_SEQ;
9948c2ecf20Sopenharmony_ci	atomic_set(&tp_vars->last_acked, BATADV_TP_FIRST_SEQ);
9958c2ecf20Sopenharmony_ci	tp_vars->fast_recovery = false;
9968c2ecf20Sopenharmony_ci	tp_vars->recover = BATADV_TP_FIRST_SEQ;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	/* initialise the CWND to 3*MSS (Section 3.1 in RFC5681).
9998c2ecf20Sopenharmony_ci	 * For batman-adv the MSS is the size of the payload received by the
10008c2ecf20Sopenharmony_ci	 * soft_interface, hence its MTU
10018c2ecf20Sopenharmony_ci	 */
10028c2ecf20Sopenharmony_ci	tp_vars->cwnd = BATADV_TP_PLEN * 3;
10038c2ecf20Sopenharmony_ci	/* at the beginning initialise the SS threshold to the biggest possible
10048c2ecf20Sopenharmony_ci	 * window size, hence the AWND size
10058c2ecf20Sopenharmony_ci	 */
10068c2ecf20Sopenharmony_ci	tp_vars->ss_threshold = BATADV_TP_AWND;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	/* RTO initial value is 3 seconds.
10098c2ecf20Sopenharmony_ci	 * Details in Section 2.1 of RFC6298
10108c2ecf20Sopenharmony_ci	 */
10118c2ecf20Sopenharmony_ci	tp_vars->rto = 1000;
10128c2ecf20Sopenharmony_ci	tp_vars->srtt = 0;
10138c2ecf20Sopenharmony_ci	tp_vars->rttvar = 0;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	atomic64_set(&tp_vars->tot_sent, 0);
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci	kref_get(&tp_vars->refcount);
10188c2ecf20Sopenharmony_ci	timer_setup(&tp_vars->timer, batadv_tp_sender_timeout, 0);
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	tp_vars->bat_priv = bat_priv;
10218c2ecf20Sopenharmony_ci	tp_vars->start_time = jiffies;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	init_waitqueue_head(&tp_vars->more_bytes);
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	spin_lock_init(&tp_vars->unacked_lock);
10268c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tp_vars->unacked_list);
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	spin_lock_init(&tp_vars->cwnd_lock);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	tp_vars->prerandom_offset = 0;
10318c2ecf20Sopenharmony_ci	spin_lock_init(&tp_vars->prerandom_lock);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	kref_get(&tp_vars->refcount);
10348c2ecf20Sopenharmony_ci	hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
10358c2ecf20Sopenharmony_ci	spin_unlock_bh(&bat_priv->tp_list_lock);
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	tp_vars->test_length = test_length;
10388c2ecf20Sopenharmony_ci	if (!tp_vars->test_length)
10398c2ecf20Sopenharmony_ci		tp_vars->test_length = BATADV_TP_DEF_TEST_LENGTH;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
10428c2ecf20Sopenharmony_ci		   "Meter: starting throughput meter towards %pM (length=%ums)\n",
10438c2ecf20Sopenharmony_ci		   dst, test_length);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	/* init work item for finished tp tests */
10468c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&tp_vars->finish_work, batadv_tp_sender_finish);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	/* start tp kthread. This way the write() call issued from userspace can
10498c2ecf20Sopenharmony_ci	 * happily return and avoid to block
10508c2ecf20Sopenharmony_ci	 */
10518c2ecf20Sopenharmony_ci	batadv_tp_start_kthread(tp_vars);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	/* don't return reference to new tp_vars */
10548c2ecf20Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
10558c2ecf20Sopenharmony_ci}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci/**
10588c2ecf20Sopenharmony_ci * batadv_tp_stop() - stop currently running tp meter session
10598c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
10608c2ecf20Sopenharmony_ci * @dst: the receiver MAC address
10618c2ecf20Sopenharmony_ci * @return_value: reason for tp meter session stop
10628c2ecf20Sopenharmony_ci */
10638c2ecf20Sopenharmony_civoid batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst,
10648c2ecf20Sopenharmony_ci		    u8 return_value)
10658c2ecf20Sopenharmony_ci{
10668c2ecf20Sopenharmony_ci	struct batadv_orig_node *orig_node;
10678c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
10708c2ecf20Sopenharmony_ci		   "Meter: stopping test towards %pM\n", dst);
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, dst);
10738c2ecf20Sopenharmony_ci	if (!orig_node)
10748c2ecf20Sopenharmony_ci		return;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	tp_vars = batadv_tp_list_find(bat_priv, orig_node->orig);
10778c2ecf20Sopenharmony_ci	if (!tp_vars) {
10788c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
10798c2ecf20Sopenharmony_ci			   "Meter: trying to interrupt an already over connection\n");
10808c2ecf20Sopenharmony_ci		goto out;
10818c2ecf20Sopenharmony_ci	}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	batadv_tp_sender_shutdown(tp_vars, return_value);
10848c2ecf20Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
10858c2ecf20Sopenharmony_ciout:
10868c2ecf20Sopenharmony_ci	batadv_orig_node_put(orig_node);
10878c2ecf20Sopenharmony_ci}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci/**
10908c2ecf20Sopenharmony_ci * batadv_tp_reset_receiver_timer() - reset the receiver shutdown timer
10918c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
10928c2ecf20Sopenharmony_ci *
10938c2ecf20Sopenharmony_ci * start the receiver shutdown timer or reset it if already started
10948c2ecf20Sopenharmony_ci */
10958c2ecf20Sopenharmony_cistatic void batadv_tp_reset_receiver_timer(struct batadv_tp_vars *tp_vars)
10968c2ecf20Sopenharmony_ci{
10978c2ecf20Sopenharmony_ci	mod_timer(&tp_vars->timer,
10988c2ecf20Sopenharmony_ci		  jiffies + msecs_to_jiffies(BATADV_TP_RECV_TIMEOUT));
10998c2ecf20Sopenharmony_ci}
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci/**
11028c2ecf20Sopenharmony_ci * batadv_tp_receiver_shutdown() - stop a tp meter receiver when timeout is
11038c2ecf20Sopenharmony_ci *  reached without received ack
11048c2ecf20Sopenharmony_ci * @t: address to timer_list inside tp_vars
11058c2ecf20Sopenharmony_ci */
11068c2ecf20Sopenharmony_cistatic void batadv_tp_receiver_shutdown(struct timer_list *t)
11078c2ecf20Sopenharmony_ci{
11088c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars = from_timer(tp_vars, t, timer);
11098c2ecf20Sopenharmony_ci	struct batadv_tp_unacked *un, *safe;
11108c2ecf20Sopenharmony_ci	struct batadv_priv *bat_priv;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	bat_priv = tp_vars->bat_priv;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	/* if there is recent activity rearm the timer */
11158c2ecf20Sopenharmony_ci	if (!batadv_has_timed_out(tp_vars->last_recv_time,
11168c2ecf20Sopenharmony_ci				  BATADV_TP_RECV_TIMEOUT)) {
11178c2ecf20Sopenharmony_ci		/* reset the receiver shutdown timer */
11188c2ecf20Sopenharmony_ci		batadv_tp_reset_receiver_timer(tp_vars);
11198c2ecf20Sopenharmony_ci		return;
11208c2ecf20Sopenharmony_ci	}
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
11238c2ecf20Sopenharmony_ci		   "Shutting down for inactivity (more than %dms) from %pM\n",
11248c2ecf20Sopenharmony_ci		   BATADV_TP_RECV_TIMEOUT, tp_vars->other_end);
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->bat_priv->tp_list_lock);
11278c2ecf20Sopenharmony_ci	hlist_del_rcu(&tp_vars->list);
11288c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock);
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	/* drop list reference */
11318c2ecf20Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	atomic_dec(&bat_priv->tp_num);
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
11368c2ecf20Sopenharmony_ci	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
11378c2ecf20Sopenharmony_ci		list_del(&un->list);
11388c2ecf20Sopenharmony_ci		kfree(un);
11398c2ecf20Sopenharmony_ci	}
11408c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	/* drop reference of timer */
11438c2ecf20Sopenharmony_ci	batadv_tp_vars_put(tp_vars);
11448c2ecf20Sopenharmony_ci}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci/**
11478c2ecf20Sopenharmony_ci * batadv_tp_send_ack() - send an ACK packet
11488c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
11498c2ecf20Sopenharmony_ci * @dst: the mac address of the destination originator
11508c2ecf20Sopenharmony_ci * @seq: the sequence number to ACK
11518c2ecf20Sopenharmony_ci * @timestamp: the timestamp to echo back in the ACK
11528c2ecf20Sopenharmony_ci * @session: session identifier
11538c2ecf20Sopenharmony_ci * @socket_index: local ICMP socket identifier
11548c2ecf20Sopenharmony_ci *
11558c2ecf20Sopenharmony_ci * Return: 0 on success, a positive integer representing the reason of the
11568c2ecf20Sopenharmony_ci * failure otherwise
11578c2ecf20Sopenharmony_ci */
11588c2ecf20Sopenharmony_cistatic int batadv_tp_send_ack(struct batadv_priv *bat_priv, const u8 *dst,
11598c2ecf20Sopenharmony_ci			      u32 seq, __be32 timestamp, const u8 *session,
11608c2ecf20Sopenharmony_ci			      int socket_index)
11618c2ecf20Sopenharmony_ci{
11628c2ecf20Sopenharmony_ci	struct batadv_hard_iface *primary_if = NULL;
11638c2ecf20Sopenharmony_ci	struct batadv_orig_node *orig_node;
11648c2ecf20Sopenharmony_ci	struct batadv_icmp_tp_packet *icmp;
11658c2ecf20Sopenharmony_ci	struct sk_buff *skb;
11668c2ecf20Sopenharmony_ci	int r, ret;
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	orig_node = batadv_orig_hash_find(bat_priv, dst);
11698c2ecf20Sopenharmony_ci	if (unlikely(!orig_node)) {
11708c2ecf20Sopenharmony_ci		ret = BATADV_TP_REASON_DST_UNREACHABLE;
11718c2ecf20Sopenharmony_ci		goto out;
11728c2ecf20Sopenharmony_ci	}
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	primary_if = batadv_primary_if_get_selected(bat_priv);
11758c2ecf20Sopenharmony_ci	if (unlikely(!primary_if)) {
11768c2ecf20Sopenharmony_ci		ret = BATADV_TP_REASON_DST_UNREACHABLE;
11778c2ecf20Sopenharmony_ci		goto out;
11788c2ecf20Sopenharmony_ci	}
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	skb = netdev_alloc_skb_ip_align(NULL, sizeof(*icmp) + ETH_HLEN);
11818c2ecf20Sopenharmony_ci	if (unlikely(!skb)) {
11828c2ecf20Sopenharmony_ci		ret = BATADV_TP_REASON_MEMORY_ERROR;
11838c2ecf20Sopenharmony_ci		goto out;
11848c2ecf20Sopenharmony_ci	}
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci	skb_reserve(skb, ETH_HLEN);
11878c2ecf20Sopenharmony_ci	icmp = skb_put(skb, sizeof(*icmp));
11888c2ecf20Sopenharmony_ci	icmp->packet_type = BATADV_ICMP;
11898c2ecf20Sopenharmony_ci	icmp->version = BATADV_COMPAT_VERSION;
11908c2ecf20Sopenharmony_ci	icmp->ttl = BATADV_TTL;
11918c2ecf20Sopenharmony_ci	icmp->msg_type = BATADV_TP;
11928c2ecf20Sopenharmony_ci	ether_addr_copy(icmp->dst, orig_node->orig);
11938c2ecf20Sopenharmony_ci	ether_addr_copy(icmp->orig, primary_if->net_dev->dev_addr);
11948c2ecf20Sopenharmony_ci	icmp->uid = socket_index;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	icmp->subtype = BATADV_TP_ACK;
11978c2ecf20Sopenharmony_ci	memcpy(icmp->session, session, sizeof(icmp->session));
11988c2ecf20Sopenharmony_ci	icmp->seqno = htonl(seq);
11998c2ecf20Sopenharmony_ci	icmp->timestamp = timestamp;
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	/* send the ack */
12028c2ecf20Sopenharmony_ci	r = batadv_send_skb_to_orig(skb, orig_node, NULL);
12038c2ecf20Sopenharmony_ci	if (unlikely(r < 0) || r == NET_XMIT_DROP) {
12048c2ecf20Sopenharmony_ci		ret = BATADV_TP_REASON_DST_UNREACHABLE;
12058c2ecf20Sopenharmony_ci		goto out;
12068c2ecf20Sopenharmony_ci	}
12078c2ecf20Sopenharmony_ci	ret = 0;
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ciout:
12108c2ecf20Sopenharmony_ci	if (likely(orig_node))
12118c2ecf20Sopenharmony_ci		batadv_orig_node_put(orig_node);
12128c2ecf20Sopenharmony_ci	if (likely(primary_if))
12138c2ecf20Sopenharmony_ci		batadv_hardif_put(primary_if);
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	return ret;
12168c2ecf20Sopenharmony_ci}
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci/**
12198c2ecf20Sopenharmony_ci * batadv_tp_handle_out_of_order() - store an out of order packet
12208c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
12218c2ecf20Sopenharmony_ci * @skb: the buffer containing the received packet
12228c2ecf20Sopenharmony_ci *
12238c2ecf20Sopenharmony_ci * Store the out of order packet in the unacked list for late processing. This
12248c2ecf20Sopenharmony_ci * packets are kept in this list so that they can be ACKed at once as soon as
12258c2ecf20Sopenharmony_ci * all the previous packets have been received
12268c2ecf20Sopenharmony_ci *
12278c2ecf20Sopenharmony_ci * Return: true if the packed has been successfully processed, false otherwise
12288c2ecf20Sopenharmony_ci */
12298c2ecf20Sopenharmony_cistatic bool batadv_tp_handle_out_of_order(struct batadv_tp_vars *tp_vars,
12308c2ecf20Sopenharmony_ci					  const struct sk_buff *skb)
12318c2ecf20Sopenharmony_ci{
12328c2ecf20Sopenharmony_ci	const struct batadv_icmp_tp_packet *icmp;
12338c2ecf20Sopenharmony_ci	struct batadv_tp_unacked *un, *new;
12348c2ecf20Sopenharmony_ci	u32 payload_len;
12358c2ecf20Sopenharmony_ci	bool added = false;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	new = kmalloc(sizeof(*new), GFP_ATOMIC);
12388c2ecf20Sopenharmony_ci	if (unlikely(!new))
12398c2ecf20Sopenharmony_ci		return false;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	new->seqno = ntohl(icmp->seqno);
12448c2ecf20Sopenharmony_ci	payload_len = skb->len - sizeof(struct batadv_unicast_packet);
12458c2ecf20Sopenharmony_ci	new->len = payload_len;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
12488c2ecf20Sopenharmony_ci	/* if the list is empty immediately attach this new object */
12498c2ecf20Sopenharmony_ci	if (list_empty(&tp_vars->unacked_list)) {
12508c2ecf20Sopenharmony_ci		list_add(&new->list, &tp_vars->unacked_list);
12518c2ecf20Sopenharmony_ci		goto out;
12528c2ecf20Sopenharmony_ci	}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	/* otherwise loop over the list and either drop the packet because this
12558c2ecf20Sopenharmony_ci	 * is a duplicate or store it at the right position.
12568c2ecf20Sopenharmony_ci	 *
12578c2ecf20Sopenharmony_ci	 * The iteration is done in the reverse way because it is likely that
12588c2ecf20Sopenharmony_ci	 * the last received packet (the one being processed now) has a bigger
12598c2ecf20Sopenharmony_ci	 * seqno than all the others already stored.
12608c2ecf20Sopenharmony_ci	 */
12618c2ecf20Sopenharmony_ci	list_for_each_entry_reverse(un, &tp_vars->unacked_list, list) {
12628c2ecf20Sopenharmony_ci		/* check for duplicates */
12638c2ecf20Sopenharmony_ci		if (new->seqno == un->seqno) {
12648c2ecf20Sopenharmony_ci			if (new->len > un->len)
12658c2ecf20Sopenharmony_ci				un->len = new->len;
12668c2ecf20Sopenharmony_ci			kfree(new);
12678c2ecf20Sopenharmony_ci			added = true;
12688c2ecf20Sopenharmony_ci			break;
12698c2ecf20Sopenharmony_ci		}
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci		/* look for the right position */
12728c2ecf20Sopenharmony_ci		if (batadv_seq_before(new->seqno, un->seqno))
12738c2ecf20Sopenharmony_ci			continue;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci		/* as soon as an entry having a bigger seqno is found, the new
12768c2ecf20Sopenharmony_ci		 * one is attached _after_ it. In this way the list is kept in
12778c2ecf20Sopenharmony_ci		 * ascending order
12788c2ecf20Sopenharmony_ci		 */
12798c2ecf20Sopenharmony_ci		list_add_tail(&new->list, &un->list);
12808c2ecf20Sopenharmony_ci		added = true;
12818c2ecf20Sopenharmony_ci		break;
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	/* received packet with smallest seqno out of order; add it to front */
12858c2ecf20Sopenharmony_ci	if (!added)
12868c2ecf20Sopenharmony_ci		list_add(&new->list, &tp_vars->unacked_list);
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ciout:
12898c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	return true;
12928c2ecf20Sopenharmony_ci}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci/**
12958c2ecf20Sopenharmony_ci * batadv_tp_ack_unordered() - update number received bytes in current stream
12968c2ecf20Sopenharmony_ci *  without gaps
12978c2ecf20Sopenharmony_ci * @tp_vars: the private data of the current TP meter session
12988c2ecf20Sopenharmony_ci */
12998c2ecf20Sopenharmony_cistatic void batadv_tp_ack_unordered(struct batadv_tp_vars *tp_vars)
13008c2ecf20Sopenharmony_ci{
13018c2ecf20Sopenharmony_ci	struct batadv_tp_unacked *un, *safe;
13028c2ecf20Sopenharmony_ci	u32 to_ack;
13038c2ecf20Sopenharmony_ci
13048c2ecf20Sopenharmony_ci	/* go through the unacked packet list and possibly ACK them as
13058c2ecf20Sopenharmony_ci	 * well
13068c2ecf20Sopenharmony_ci	 */
13078c2ecf20Sopenharmony_ci	spin_lock_bh(&tp_vars->unacked_lock);
13088c2ecf20Sopenharmony_ci	list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) {
13098c2ecf20Sopenharmony_ci		/* the list is ordered, therefore it is possible to stop as soon
13108c2ecf20Sopenharmony_ci		 * there is a gap between the last acked seqno and the seqno of
13118c2ecf20Sopenharmony_ci		 * the packet under inspection
13128c2ecf20Sopenharmony_ci		 */
13138c2ecf20Sopenharmony_ci		if (batadv_seq_before(tp_vars->last_recv, un->seqno))
13148c2ecf20Sopenharmony_ci			break;
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_ci		to_ack = un->seqno + un->len - tp_vars->last_recv;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci		if (batadv_seq_before(tp_vars->last_recv, un->seqno + un->len))
13198c2ecf20Sopenharmony_ci			tp_vars->last_recv += to_ack;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci		list_del(&un->list);
13228c2ecf20Sopenharmony_ci		kfree(un);
13238c2ecf20Sopenharmony_ci	}
13248c2ecf20Sopenharmony_ci	spin_unlock_bh(&tp_vars->unacked_lock);
13258c2ecf20Sopenharmony_ci}
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci/**
13288c2ecf20Sopenharmony_ci * batadv_tp_init_recv() - return matching or create new receiver tp_vars
13298c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
13308c2ecf20Sopenharmony_ci * @icmp: received icmp tp msg
13318c2ecf20Sopenharmony_ci *
13328c2ecf20Sopenharmony_ci * Return: corresponding tp_vars or NULL on errors
13338c2ecf20Sopenharmony_ci */
13348c2ecf20Sopenharmony_cistatic struct batadv_tp_vars *
13358c2ecf20Sopenharmony_cibatadv_tp_init_recv(struct batadv_priv *bat_priv,
13368c2ecf20Sopenharmony_ci		    const struct batadv_icmp_tp_packet *icmp)
13378c2ecf20Sopenharmony_ci{
13388c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	spin_lock_bh(&bat_priv->tp_list_lock);
13418c2ecf20Sopenharmony_ci	tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
13428c2ecf20Sopenharmony_ci					      icmp->session);
13438c2ecf20Sopenharmony_ci	if (tp_vars)
13448c2ecf20Sopenharmony_ci		goto out_unlock;
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	if (!atomic_add_unless(&bat_priv->tp_num, 1, BATADV_TP_MAX_NUM)) {
13478c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
13488c2ecf20Sopenharmony_ci			   "Meter: too many ongoing sessions, aborting (RECV)\n");
13498c2ecf20Sopenharmony_ci		goto out_unlock;
13508c2ecf20Sopenharmony_ci	}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci	tp_vars = kmalloc(sizeof(*tp_vars), GFP_ATOMIC);
13538c2ecf20Sopenharmony_ci	if (!tp_vars)
13548c2ecf20Sopenharmony_ci		goto out_unlock;
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	ether_addr_copy(tp_vars->other_end, icmp->orig);
13578c2ecf20Sopenharmony_ci	tp_vars->role = BATADV_TP_RECEIVER;
13588c2ecf20Sopenharmony_ci	memcpy(tp_vars->session, icmp->session, sizeof(tp_vars->session));
13598c2ecf20Sopenharmony_ci	tp_vars->last_recv = BATADV_TP_FIRST_SEQ;
13608c2ecf20Sopenharmony_ci	tp_vars->bat_priv = bat_priv;
13618c2ecf20Sopenharmony_ci	kref_init(&tp_vars->refcount);
13628c2ecf20Sopenharmony_ci
13638c2ecf20Sopenharmony_ci	spin_lock_init(&tp_vars->unacked_lock);
13648c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&tp_vars->unacked_list);
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci	kref_get(&tp_vars->refcount);
13678c2ecf20Sopenharmony_ci	hlist_add_head_rcu(&tp_vars->list, &bat_priv->tp_list);
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci	kref_get(&tp_vars->refcount);
13708c2ecf20Sopenharmony_ci	timer_setup(&tp_vars->timer, batadv_tp_receiver_shutdown, 0);
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_ci	batadv_tp_reset_receiver_timer(tp_vars);
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ciout_unlock:
13758c2ecf20Sopenharmony_ci	spin_unlock_bh(&bat_priv->tp_list_lock);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	return tp_vars;
13788c2ecf20Sopenharmony_ci}
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci/**
13818c2ecf20Sopenharmony_ci * batadv_tp_recv_msg() - process a single data message
13828c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
13838c2ecf20Sopenharmony_ci * @skb: the buffer containing the received packet
13848c2ecf20Sopenharmony_ci *
13858c2ecf20Sopenharmony_ci * Process a received TP MSG packet
13868c2ecf20Sopenharmony_ci */
13878c2ecf20Sopenharmony_cistatic void batadv_tp_recv_msg(struct batadv_priv *bat_priv,
13888c2ecf20Sopenharmony_ci			       const struct sk_buff *skb)
13898c2ecf20Sopenharmony_ci{
13908c2ecf20Sopenharmony_ci	const struct batadv_icmp_tp_packet *icmp;
13918c2ecf20Sopenharmony_ci	struct batadv_tp_vars *tp_vars;
13928c2ecf20Sopenharmony_ci	size_t packet_size;
13938c2ecf20Sopenharmony_ci	u32 seqno;
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	seqno = ntohl(icmp->seqno);
13988c2ecf20Sopenharmony_ci	/* check if this is the first seqno. This means that if the
13998c2ecf20Sopenharmony_ci	 * first packet is lost, the tp meter does not work anymore!
14008c2ecf20Sopenharmony_ci	 */
14018c2ecf20Sopenharmony_ci	if (seqno == BATADV_TP_FIRST_SEQ) {
14028c2ecf20Sopenharmony_ci		tp_vars = batadv_tp_init_recv(bat_priv, icmp);
14038c2ecf20Sopenharmony_ci		if (!tp_vars) {
14048c2ecf20Sopenharmony_ci			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
14058c2ecf20Sopenharmony_ci				   "Meter: seqno != BATADV_TP_FIRST_SEQ cannot initiate connection\n");
14068c2ecf20Sopenharmony_ci			goto out;
14078c2ecf20Sopenharmony_ci		}
14088c2ecf20Sopenharmony_ci	} else {
14098c2ecf20Sopenharmony_ci		tp_vars = batadv_tp_list_find_session(bat_priv, icmp->orig,
14108c2ecf20Sopenharmony_ci						      icmp->session);
14118c2ecf20Sopenharmony_ci		if (!tp_vars) {
14128c2ecf20Sopenharmony_ci			batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
14138c2ecf20Sopenharmony_ci				   "Unexpected packet from %pM!\n",
14148c2ecf20Sopenharmony_ci				   icmp->orig);
14158c2ecf20Sopenharmony_ci			goto out;
14168c2ecf20Sopenharmony_ci		}
14178c2ecf20Sopenharmony_ci	}
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	if (unlikely(tp_vars->role != BATADV_TP_RECEIVER)) {
14208c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
14218c2ecf20Sopenharmony_ci			   "Meter: dropping packet: not expected (role=%u)\n",
14228c2ecf20Sopenharmony_ci			   tp_vars->role);
14238c2ecf20Sopenharmony_ci		goto out;
14248c2ecf20Sopenharmony_ci	}
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci	tp_vars->last_recv_time = jiffies;
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci	/* if the packet is a duplicate, it may be the case that an ACK has been
14298c2ecf20Sopenharmony_ci	 * lost. Resend the ACK
14308c2ecf20Sopenharmony_ci	 */
14318c2ecf20Sopenharmony_ci	if (batadv_seq_before(seqno, tp_vars->last_recv))
14328c2ecf20Sopenharmony_ci		goto send_ack;
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	/* if the packet is out of order enqueue it */
14358c2ecf20Sopenharmony_ci	if (ntohl(icmp->seqno) != tp_vars->last_recv) {
14368c2ecf20Sopenharmony_ci		/* exit immediately (and do not send any ACK) if the packet has
14378c2ecf20Sopenharmony_ci		 * not been enqueued correctly
14388c2ecf20Sopenharmony_ci		 */
14398c2ecf20Sopenharmony_ci		if (!batadv_tp_handle_out_of_order(tp_vars, skb))
14408c2ecf20Sopenharmony_ci			goto out;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci		/* send a duplicate ACK */
14438c2ecf20Sopenharmony_ci		goto send_ack;
14448c2ecf20Sopenharmony_ci	}
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	/* if everything was fine count the ACKed bytes */
14478c2ecf20Sopenharmony_ci	packet_size = skb->len - sizeof(struct batadv_unicast_packet);
14488c2ecf20Sopenharmony_ci	tp_vars->last_recv += packet_size;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	/* check if this ordered message filled a gap.... */
14518c2ecf20Sopenharmony_ci	batadv_tp_ack_unordered(tp_vars);
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_cisend_ack:
14548c2ecf20Sopenharmony_ci	/* send the ACK. If the received packet was out of order, the ACK that
14558c2ecf20Sopenharmony_ci	 * is going to be sent is a duplicate (the sender will count them and
14568c2ecf20Sopenharmony_ci	 * possibly enter Fast Retransmit as soon as it has reached 3)
14578c2ecf20Sopenharmony_ci	 */
14588c2ecf20Sopenharmony_ci	batadv_tp_send_ack(bat_priv, icmp->orig, tp_vars->last_recv,
14598c2ecf20Sopenharmony_ci			   icmp->timestamp, icmp->session, icmp->uid);
14608c2ecf20Sopenharmony_ciout:
14618c2ecf20Sopenharmony_ci	if (likely(tp_vars))
14628c2ecf20Sopenharmony_ci		batadv_tp_vars_put(tp_vars);
14638c2ecf20Sopenharmony_ci}
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci/**
14668c2ecf20Sopenharmony_ci * batadv_tp_meter_recv() - main TP Meter receiving function
14678c2ecf20Sopenharmony_ci * @bat_priv: the bat priv with all the soft interface information
14688c2ecf20Sopenharmony_ci * @skb: the buffer containing the received packet
14698c2ecf20Sopenharmony_ci */
14708c2ecf20Sopenharmony_civoid batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb)
14718c2ecf20Sopenharmony_ci{
14728c2ecf20Sopenharmony_ci	struct batadv_icmp_tp_packet *icmp;
14738c2ecf20Sopenharmony_ci
14748c2ecf20Sopenharmony_ci	icmp = (struct batadv_icmp_tp_packet *)skb->data;
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci	switch (icmp->subtype) {
14778c2ecf20Sopenharmony_ci	case BATADV_TP_MSG:
14788c2ecf20Sopenharmony_ci		batadv_tp_recv_msg(bat_priv, skb);
14798c2ecf20Sopenharmony_ci		break;
14808c2ecf20Sopenharmony_ci	case BATADV_TP_ACK:
14818c2ecf20Sopenharmony_ci		batadv_tp_recv_ack(bat_priv, skb);
14828c2ecf20Sopenharmony_ci		break;
14838c2ecf20Sopenharmony_ci	default:
14848c2ecf20Sopenharmony_ci		batadv_dbg(BATADV_DBG_TP_METER, bat_priv,
14858c2ecf20Sopenharmony_ci			   "Received unknown TP Metric packet type %u\n",
14868c2ecf20Sopenharmony_ci			   icmp->subtype);
14878c2ecf20Sopenharmony_ci	}
14888c2ecf20Sopenharmony_ci	consume_skb(skb);
14898c2ecf20Sopenharmony_ci}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci/**
14928c2ecf20Sopenharmony_ci * batadv_tp_meter_init() - initialize global tp_meter structures
14938c2ecf20Sopenharmony_ci */
14948c2ecf20Sopenharmony_civoid __init batadv_tp_meter_init(void)
14958c2ecf20Sopenharmony_ci{
14968c2ecf20Sopenharmony_ci	get_random_bytes(batadv_tp_prerandom, sizeof(batadv_tp_prerandom));
14978c2ecf20Sopenharmony_ci}
1498