162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci#ifndef _ACKVEC_H
362306a36Sopenharmony_ci#define _ACKVEC_H
462306a36Sopenharmony_ci/*
562306a36Sopenharmony_ci *  net/dccp/ackvec.h
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *  An implementation of Ack Vectors for the DCCP protocol
862306a36Sopenharmony_ci *  Copyright (c) 2007 University of Aberdeen, Scotland, UK
962306a36Sopenharmony_ci *  Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@mandriva.com>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/dccp.h>
1362306a36Sopenharmony_ci#include <linux/compiler.h>
1462306a36Sopenharmony_ci#include <linux/list.h>
1562306a36Sopenharmony_ci#include <linux/types.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/*
1862306a36Sopenharmony_ci * Ack Vector buffer space is static, in multiples of %DCCP_SINGLE_OPT_MAXLEN,
1962306a36Sopenharmony_ci * the maximum size of a single Ack Vector. Setting %DCCPAV_NUM_ACKVECS to 1
2062306a36Sopenharmony_ci * will be sufficient for most cases of low Ack Ratios, using a value of 2 gives
2162306a36Sopenharmony_ci * more headroom if Ack Ratio is higher or when the sender acknowledges slowly.
2262306a36Sopenharmony_ci * The maximum value is bounded by the u16 types for indices and functions.
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci#define DCCPAV_NUM_ACKVECS	2
2562306a36Sopenharmony_ci#define DCCPAV_MAX_ACKVEC_LEN	(DCCP_SINGLE_OPT_MAXLEN * DCCPAV_NUM_ACKVECS)
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* Estimated minimum average Ack Vector length - used for updating MPS */
2862306a36Sopenharmony_ci#define DCCPAV_MIN_OPTLEN	16
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* Threshold for coping with large bursts of losses */
3162306a36Sopenharmony_ci#define DCCPAV_BURST_THRESH	(DCCPAV_MAX_ACKVEC_LEN / 8)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cienum dccp_ackvec_states {
3462306a36Sopenharmony_ci	DCCPAV_RECEIVED =	0x00,
3562306a36Sopenharmony_ci	DCCPAV_ECN_MARKED =	0x40,
3662306a36Sopenharmony_ci	DCCPAV_RESERVED =	0x80,
3762306a36Sopenharmony_ci	DCCPAV_NOT_RECEIVED =	0xC0
3862306a36Sopenharmony_ci};
3962306a36Sopenharmony_ci#define DCCPAV_MAX_RUNLEN	0x3F
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic inline u8 dccp_ackvec_runlen(const u8 *cell)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	return *cell & DCCPAV_MAX_RUNLEN;
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic inline u8 dccp_ackvec_state(const u8 *cell)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	return *cell & ~DCCPAV_MAX_RUNLEN;
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/**
5262306a36Sopenharmony_ci * struct dccp_ackvec - Ack Vector main data structure
5362306a36Sopenharmony_ci *
5462306a36Sopenharmony_ci * This implements a fixed-size circular buffer within an array and is largely
5562306a36Sopenharmony_ci * based on Appendix A of RFC 4340.
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * @av_buf:	   circular buffer storage area
5862306a36Sopenharmony_ci * @av_buf_head:   head index; begin of live portion in @av_buf
5962306a36Sopenharmony_ci * @av_buf_tail:   tail index; first index _after_ the live portion in @av_buf
6062306a36Sopenharmony_ci * @av_buf_ackno:  highest seqno of acknowledgeable packet recorded in @av_buf
6162306a36Sopenharmony_ci * @av_tail_ackno: lowest  seqno of acknowledgeable packet recorded in @av_buf
6262306a36Sopenharmony_ci * @av_buf_nonce:  ECN nonce sums, each covering subsequent segments of up to
6362306a36Sopenharmony_ci *		   %DCCP_SINGLE_OPT_MAXLEN cells in the live portion of @av_buf
6462306a36Sopenharmony_ci * @av_overflow:   if 1 then buf_head == buf_tail indicates buffer wraparound
6562306a36Sopenharmony_ci * @av_records:	   list of %dccp_ackvec_record (Ack Vectors sent previously)
6662306a36Sopenharmony_ci */
6762306a36Sopenharmony_cistruct dccp_ackvec {
6862306a36Sopenharmony_ci	u8			av_buf[DCCPAV_MAX_ACKVEC_LEN];
6962306a36Sopenharmony_ci	u16			av_buf_head;
7062306a36Sopenharmony_ci	u16			av_buf_tail;
7162306a36Sopenharmony_ci	u64			av_buf_ackno:48;
7262306a36Sopenharmony_ci	u64			av_tail_ackno:48;
7362306a36Sopenharmony_ci	bool			av_buf_nonce[DCCPAV_NUM_ACKVECS];
7462306a36Sopenharmony_ci	u8			av_overflow:1;
7562306a36Sopenharmony_ci	struct list_head	av_records;
7662306a36Sopenharmony_ci};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/**
7962306a36Sopenharmony_ci * struct dccp_ackvec_record - Records information about sent Ack Vectors
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci * These list entries define the additional information which the HC-Receiver
8262306a36Sopenharmony_ci * keeps about recently-sent Ack Vectors; again refer to RFC 4340, Appendix A.
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * @avr_node:	    the list node in @av_records
8562306a36Sopenharmony_ci * @avr_ack_seqno:  sequence number of the packet the Ack Vector was sent on
8662306a36Sopenharmony_ci * @avr_ack_ackno:  the Ack number that this record/Ack Vector refers to
8762306a36Sopenharmony_ci * @avr_ack_ptr:    pointer into @av_buf where this record starts
8862306a36Sopenharmony_ci * @avr_ack_runlen: run length of @avr_ack_ptr at the time of sending
8962306a36Sopenharmony_ci * @avr_ack_nonce:  the sum of @av_buf_nonce's at the time this record was sent
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * The list as a whole is sorted in descending order by @avr_ack_seqno.
9262306a36Sopenharmony_ci */
9362306a36Sopenharmony_cistruct dccp_ackvec_record {
9462306a36Sopenharmony_ci	struct list_head avr_node;
9562306a36Sopenharmony_ci	u64		 avr_ack_seqno:48;
9662306a36Sopenharmony_ci	u64		 avr_ack_ackno:48;
9762306a36Sopenharmony_ci	u16		 avr_ack_ptr;
9862306a36Sopenharmony_ci	u8		 avr_ack_runlen;
9962306a36Sopenharmony_ci	u8		 avr_ack_nonce:1;
10062306a36Sopenharmony_ci};
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciint dccp_ackvec_init(void);
10362306a36Sopenharmony_civoid dccp_ackvec_exit(void);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistruct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority);
10662306a36Sopenharmony_civoid dccp_ackvec_free(struct dccp_ackvec *av);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_civoid dccp_ackvec_input(struct dccp_ackvec *av, struct sk_buff *skb);
10962306a36Sopenharmony_ciint dccp_ackvec_update_records(struct dccp_ackvec *av, u64 seq, u8 sum);
11062306a36Sopenharmony_civoid dccp_ackvec_clear_state(struct dccp_ackvec *av, const u64 ackno);
11162306a36Sopenharmony_ciu16 dccp_ackvec_buflen(const struct dccp_ackvec *av);
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic inline bool dccp_ackvec_is_empty(const struct dccp_ackvec *av)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	return av->av_overflow == 0 && av->av_buf_head == av->av_buf_tail;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/**
11962306a36Sopenharmony_ci * struct dccp_ackvec_parsed  -  Record offsets of Ack Vectors in skb
12062306a36Sopenharmony_ci * @vec:	start of vector (offset into skb)
12162306a36Sopenharmony_ci * @len:	length of @vec
12262306a36Sopenharmony_ci * @nonce:	whether @vec had an ECN nonce of 0 or 1
12362306a36Sopenharmony_ci * @node:	FIFO - arranged in descending order of ack_ackno
12462306a36Sopenharmony_ci *
12562306a36Sopenharmony_ci * This structure is used by CCIDs to access Ack Vectors in a received skb.
12662306a36Sopenharmony_ci */
12762306a36Sopenharmony_cistruct dccp_ackvec_parsed {
12862306a36Sopenharmony_ci	u8		 *vec,
12962306a36Sopenharmony_ci			 len,
13062306a36Sopenharmony_ci			 nonce:1;
13162306a36Sopenharmony_ci	struct list_head node;
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciint dccp_ackvec_parsed_add(struct list_head *head, u8 *vec, u8 len, u8 nonce);
13562306a36Sopenharmony_civoid dccp_ackvec_parsed_cleanup(struct list_head *parsed_chunks);
13662306a36Sopenharmony_ci#endif /* _ACKVEC_H */
137