18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Packet RX/TX history data structures and routines for TFRC-based protocols.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 2007   The University of Aberdeen, Scotland, UK
68c2ecf20Sopenharmony_ci *  Copyright (c) 2005-6 The University of Waikato, Hamilton, New Zealand.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  This code has been developed by the University of Waikato WAND
98c2ecf20Sopenharmony_ci *  research group. For further information please see https://www.wand.net.nz/
108c2ecf20Sopenharmony_ci *  or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *  This code also uses code from Lulea University, rereleased as GPL by its
138c2ecf20Sopenharmony_ci *  authors:
148c2ecf20Sopenharmony_ci *  Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *  Changes to meet Linux coding standards, to make it meet latest ccid3 draft
178c2ecf20Sopenharmony_ci *  and to make it work as a loadable module in the DCCP stack written by
188c2ecf20Sopenharmony_ci *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#ifndef _DCCP_PKT_HIST_
248c2ecf20Sopenharmony_ci#define _DCCP_PKT_HIST_
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <linux/list.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci#include "tfrc.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/**
318c2ecf20Sopenharmony_ci *  tfrc_tx_hist_entry  -  Simple singly-linked TX history list
328c2ecf20Sopenharmony_ci *  @next:  next oldest entry (LIFO order)
338c2ecf20Sopenharmony_ci *  @seqno: sequence number of this entry
348c2ecf20Sopenharmony_ci *  @stamp: send time of packet with sequence number @seqno
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_cistruct tfrc_tx_hist_entry {
378c2ecf20Sopenharmony_ci	struct tfrc_tx_hist_entry *next;
388c2ecf20Sopenharmony_ci	u64			  seqno;
398c2ecf20Sopenharmony_ci	ktime_t			  stamp;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic inline struct tfrc_tx_hist_entry *
438c2ecf20Sopenharmony_ci	tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	while (head != NULL && head->seqno != seqno)
468c2ecf20Sopenharmony_ci		head = head->next;
478c2ecf20Sopenharmony_ci	return head;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ciint tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno);
518c2ecf20Sopenharmony_civoid tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* Subtraction a-b modulo-16, respects circular wrap-around */
548c2ecf20Sopenharmony_ci#define SUB16(a, b) (((a) + 16 - (b)) & 0xF)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Number of packets to wait after a missing packet (RFC 4342, 6.1) */
578c2ecf20Sopenharmony_ci#define TFRC_NDUPACK 3
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci * tfrc_rx_hist_entry - Store information about a single received packet
618c2ecf20Sopenharmony_ci * @tfrchrx_seqno:	DCCP packet sequence number
628c2ecf20Sopenharmony_ci * @tfrchrx_ccval:	window counter value of packet (RFC 4342, 8.1)
638c2ecf20Sopenharmony_ci * @tfrchrx_ndp:	the NDP count (if any) of the packet
648c2ecf20Sopenharmony_ci * @tfrchrx_tstamp:	actual receive time of packet
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistruct tfrc_rx_hist_entry {
678c2ecf20Sopenharmony_ci	u64		 tfrchrx_seqno:48,
688c2ecf20Sopenharmony_ci			 tfrchrx_ccval:4,
698c2ecf20Sopenharmony_ci			 tfrchrx_type:4;
708c2ecf20Sopenharmony_ci	u64		 tfrchrx_ndp:48;
718c2ecf20Sopenharmony_ci	ktime_t		 tfrchrx_tstamp;
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * tfrc_rx_hist  -  RX history structure for TFRC-based protocols
768c2ecf20Sopenharmony_ci * @ring:		Packet history for RTT sampling and loss detection
778c2ecf20Sopenharmony_ci * @loss_count:		Number of entries in circular history
788c2ecf20Sopenharmony_ci * @loss_start:		Movable index (for loss detection)
798c2ecf20Sopenharmony_ci * @rtt_sample_prev:	Used during RTT sampling, points to candidate entry
808c2ecf20Sopenharmony_ci */
818c2ecf20Sopenharmony_cistruct tfrc_rx_hist {
828c2ecf20Sopenharmony_ci	struct tfrc_rx_hist_entry *ring[TFRC_NDUPACK + 1];
838c2ecf20Sopenharmony_ci	u8			  loss_count:2,
848c2ecf20Sopenharmony_ci				  loss_start:2;
858c2ecf20Sopenharmony_ci#define rtt_sample_prev		  loss_start
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/**
898c2ecf20Sopenharmony_ci * tfrc_rx_hist_index - index to reach n-th entry after loss_start
908c2ecf20Sopenharmony_ci */
918c2ecf20Sopenharmony_cistatic inline u8 tfrc_rx_hist_index(const struct tfrc_rx_hist *h, const u8 n)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	return (h->loss_start + n) & TFRC_NDUPACK;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/**
978c2ecf20Sopenharmony_ci * tfrc_rx_hist_last_rcv - entry with highest-received-seqno so far
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_cistatic inline struct tfrc_rx_hist_entry *
1008c2ecf20Sopenharmony_ci			tfrc_rx_hist_last_rcv(const struct tfrc_rx_hist *h)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	return h->ring[tfrc_rx_hist_index(h, h->loss_count)];
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/**
1068c2ecf20Sopenharmony_ci * tfrc_rx_hist_entry - return the n-th history entry after loss_start
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_cistatic inline struct tfrc_rx_hist_entry *
1098c2ecf20Sopenharmony_ci			tfrc_rx_hist_entry(const struct tfrc_rx_hist *h, const u8 n)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	return h->ring[tfrc_rx_hist_index(h, n)];
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/**
1158c2ecf20Sopenharmony_ci * tfrc_rx_hist_loss_prev - entry with highest-received-seqno before loss was detected
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_cistatic inline struct tfrc_rx_hist_entry *
1188c2ecf20Sopenharmony_ci			tfrc_rx_hist_loss_prev(const struct tfrc_rx_hist *h)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return h->ring[h->loss_start];
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* indicate whether previously a packet was detected missing */
1248c2ecf20Sopenharmony_cistatic inline bool tfrc_rx_hist_loss_pending(const struct tfrc_rx_hist *h)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	return h->loss_count > 0;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_civoid tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h, const struct sk_buff *skb,
1308c2ecf20Sopenharmony_ci			     const u64 ndp);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ciint tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistruct tfrc_loss_hist;
1358c2ecf20Sopenharmony_ciint tfrc_rx_handle_loss(struct tfrc_rx_hist *h, struct tfrc_loss_hist *lh,
1368c2ecf20Sopenharmony_ci			struct sk_buff *skb, const u64 ndp,
1378c2ecf20Sopenharmony_ci			u32 (*first_li)(struct sock *sk), struct sock *sk);
1388c2ecf20Sopenharmony_ciu32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb);
1398c2ecf20Sopenharmony_ciint tfrc_rx_hist_alloc(struct tfrc_rx_hist *h);
1408c2ecf20Sopenharmony_civoid tfrc_rx_hist_purge(struct tfrc_rx_hist *h);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#endif /* _DCCP_PKT_HIST_ */
143