2 * net/dccp/packet_history.c
4 * Copyright (c) 2007 The University of Aberdeen, Scotland, UK
5 * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
7 * An implementation of the DCCP protocol
9 * This code has been developed by the University of Waikato WAND
10 * research group. For further information please see http://www.wand.net.nz/
11 * or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz
13 * This code also uses code from Lulea University, rereleased as GPL by its
15 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
17 * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
18 * and to make it work as a loadable module in the DCCP stack written by
19 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
21 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #include <linux/string.h>
39 #include "packet_history.h"
42 * tfrc_tx_hist_entry - Simple singly-linked TX history list
43 * @next: next oldest entry (LIFO order)
44 * @seqno: sequence number of this entry
45 * @stamp: send time of packet with sequence number @seqno
47 struct tfrc_tx_hist_entry {
48 struct tfrc_tx_hist_entry *next;
54 * Transmitter History Routines
56 static struct kmem_cache *tfrc_tx_hist_slab;
58 static struct tfrc_tx_hist_entry *
59 tfrc_tx_hist_find_entry(struct tfrc_tx_hist_entry *head, u64 seqno)
61 while (head != NULL && head->seqno != seqno)
67 int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)
69 struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());
74 entry->stamp = ktime_get_real();
79 EXPORT_SYMBOL_GPL(tfrc_tx_hist_add);
81 void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)
83 struct tfrc_tx_hist_entry *head = *headp;
85 while (head != NULL) {
86 struct tfrc_tx_hist_entry *next = head->next;
88 kmem_cache_free(tfrc_tx_hist_slab, head);
94 EXPORT_SYMBOL_GPL(tfrc_tx_hist_purge);
96 u32 tfrc_tx_hist_rtt(struct tfrc_tx_hist_entry *head, const u64 seqno,
100 struct tfrc_tx_hist_entry *packet = tfrc_tx_hist_find_entry(head, seqno);
102 if (packet != NULL) {
103 rtt = ktime_us_delta(now, packet->stamp);
105 * Garbage-collect older (irrelevant) entries:
107 tfrc_tx_hist_purge(&packet->next);
112 EXPORT_SYMBOL_GPL(tfrc_tx_hist_rtt);
115 * Receiver History Routines
117 static struct kmem_cache *tfrc_rx_hist_slab;
119 struct dccp_rx_hist_entry *dccp_rx_hist_entry_new(const u32 ndp,
120 const struct sk_buff *skb,
123 struct dccp_rx_hist_entry *entry = kmem_cache_alloc(tfrc_rx_hist_slab,
127 const struct dccp_hdr *dh = dccp_hdr(skb);
129 entry->dccphrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;
130 entry->dccphrx_ccval = dh->dccph_ccval;
131 entry->dccphrx_type = dh->dccph_type;
132 entry->dccphrx_ndp = ndp;
133 entry->dccphrx_tstamp = ktime_get_real();
138 EXPORT_SYMBOL_GPL(dccp_rx_hist_entry_new);
140 static inline void dccp_rx_hist_entry_delete(struct dccp_rx_hist_entry *entry)
142 kmem_cache_free(tfrc_rx_hist_slab, entry);
145 int dccp_rx_hist_find_entry(const struct list_head *list, const u64 seq,
148 struct dccp_rx_hist_entry *packet = NULL, *entry;
150 list_for_each_entry(entry, list, dccphrx_node)
151 if (entry->dccphrx_seqno == seq) {
157 *ccval = packet->dccphrx_ccval;
159 return packet != NULL;
162 EXPORT_SYMBOL_GPL(dccp_rx_hist_find_entry);
163 struct dccp_rx_hist_entry *
164 dccp_rx_hist_find_data_packet(const struct list_head *list)
166 struct dccp_rx_hist_entry *entry, *packet = NULL;
168 list_for_each_entry(entry, list, dccphrx_node)
169 if (entry->dccphrx_type == DCCP_PKT_DATA ||
170 entry->dccphrx_type == DCCP_PKT_DATAACK) {
178 EXPORT_SYMBOL_GPL(dccp_rx_hist_find_data_packet);
180 void dccp_rx_hist_add_packet(struct list_head *rx_list,
181 struct list_head *li_list,
182 struct dccp_rx_hist_entry *packet,
185 struct dccp_rx_hist_entry *entry, *next;
188 list_add(&packet->dccphrx_node, rx_list);
190 num_later = TFRC_RECV_NUM_LATE_LOSS + 1;
192 if (!list_empty(li_list)) {
193 list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
194 if (num_later == 0) {
195 if (after48(nonloss_seqno,
196 entry->dccphrx_seqno)) {
197 list_del_init(&entry->dccphrx_node);
198 dccp_rx_hist_entry_delete(entry);
200 } else if (dccp_rx_hist_entry_data_packet(entry))
205 u8 win_count = 0; /* Not needed, but lets shut up gcc */
208 * We have no loss interval history so we need at least one
209 * rtt:s of data packets to approximate rtt.
211 list_for_each_entry_safe(entry, next, rx_list, dccphrx_node) {
212 if (num_later == 0) {
216 /* OK, find next data packet */
221 /* OK, find next data packet */
223 win_count = entry->dccphrx_ccval;
226 tmp = win_count - entry->dccphrx_ccval;
228 tmp += TFRC_WIN_COUNT_LIMIT;
229 if (tmp > TFRC_WIN_COUNT_PER_RTT + 1) {
231 * We have found a packet older
232 * than one rtt remove the rest
235 } else /* OK, find next data packet */
239 list_del_init(&entry->dccphrx_node);
240 dccp_rx_hist_entry_delete(entry);
243 } else if (dccp_rx_hist_entry_data_packet(entry))
249 EXPORT_SYMBOL_GPL(dccp_rx_hist_add_packet);
251 void dccp_rx_hist_purge(struct list_head *list)
253 struct dccp_rx_hist_entry *entry, *next;
255 list_for_each_entry_safe(entry, next, list, dccphrx_node) {
256 list_del_init(&entry->dccphrx_node);
257 dccp_rx_hist_entry_delete(entry);
261 EXPORT_SYMBOL_GPL(dccp_rx_hist_purge);
263 __init int packet_history_init(void)
265 tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",
266 sizeof(struct tfrc_tx_hist_entry), 0,
267 SLAB_HWCACHE_ALIGN, NULL);
268 if (tfrc_tx_hist_slab == NULL)
271 tfrc_rx_hist_slab = kmem_cache_create("tfrc_rx_hist",
272 sizeof(struct dccp_rx_hist_entry), 0,
273 SLAB_HWCACHE_ALIGN, NULL);
274 if (tfrc_rx_hist_slab == NULL)
280 kmem_cache_destroy(tfrc_tx_hist_slab);
281 tfrc_tx_hist_slab = NULL;
286 void packet_history_exit(void)
288 if (tfrc_tx_hist_slab != NULL) {
289 kmem_cache_destroy(tfrc_tx_hist_slab);
290 tfrc_tx_hist_slab = NULL;
293 if (tfrc_rx_hist_slab != NULL) {
294 kmem_cache_destroy(tfrc_rx_hist_slab);
295 tfrc_rx_hist_slab = NULL;