Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 1 | /* |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 2 | * Copyright (c) 2007 The University of Aberdeen, Scotland, UK |
| 3 | * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand. |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 4 | * |
| 5 | * An implementation of the DCCP protocol |
| 6 | * |
| 7 | * This code has been developed by the University of Waikato WAND |
| 8 | * research group. For further information please see http://www.wand.net.nz/ |
Ian McDonald | e6bccd3 | 2006-08-26 19:01:30 -0700 | [diff] [blame] | 9 | * or e-mail Ian McDonald - ian.mcdonald@jandi.co.nz |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 10 | * |
| 11 | * This code also uses code from Lulea University, rereleased as GPL by its |
| 12 | * authors: |
| 13 | * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon |
| 14 | * |
| 15 | * Changes to meet Linux coding standards, to make it meet latest ccid3 draft |
| 16 | * and to make it work as a loadable module in the DCCP stack written by |
| 17 | * Arnaldo Carvalho de Melo <acme@conectiva.com.br>. |
| 18 | * |
| 19 | * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 20 | * |
| 21 | * This program is free software; you can redistribute it and/or modify |
| 22 | * it under the terms of the GNU General Public License as published by |
| 23 | * the Free Software Foundation; either version 2 of the License, or |
| 24 | * (at your option) any later version. |
| 25 | * |
| 26 | * This program is distributed in the hope that it will be useful, |
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | * GNU General Public License for more details. |
| 30 | * |
| 31 | * You should have received a copy of the GNU General Public License |
| 32 | * along with this program; if not, write to the Free Software |
| 33 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 34 | */ |
| 35 | |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 36 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 37 | #include <linux/slab.h> |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 38 | #include "packet_history.h" |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 39 | #include "../../dccp.h" |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 40 | |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 41 | /* |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 42 | * Transmitter History Routines |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 43 | */ |
Arnaldo Carvalho de Melo | e9c8b24a | 2007-12-06 12:27:49 -0200 | [diff] [blame] | 44 | static struct kmem_cache *tfrc_tx_hist_slab; |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 45 | |
Gerrit Renker | df8f83f | 2007-12-12 12:24:49 -0200 | [diff] [blame] | 46 | int __init tfrc_tx_packet_history_init(void) |
| 47 | { |
| 48 | tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist", |
| 49 | sizeof(struct tfrc_tx_hist_entry), |
| 50 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 51 | return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0; |
| 52 | } |
| 53 | |
| 54 | void tfrc_tx_packet_history_exit(void) |
| 55 | { |
| 56 | if (tfrc_tx_hist_slab != NULL) { |
| 57 | kmem_cache_destroy(tfrc_tx_hist_slab); |
| 58 | tfrc_tx_hist_slab = NULL; |
| 59 | } |
| 60 | } |
| 61 | |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 62 | int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno) |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 63 | { |
Arnaldo Carvalho de Melo | e9c8b24a | 2007-12-06 12:27:49 -0200 | [diff] [blame] | 64 | struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any()); |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 65 | |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 66 | if (entry == NULL) |
| 67 | return -ENOBUFS; |
| 68 | entry->seqno = seqno; |
| 69 | entry->stamp = ktime_get_real(); |
| 70 | entry->next = *headp; |
| 71 | *headp = entry; |
| 72 | return 0; |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 73 | } |
| 74 | |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 75 | void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp) |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 76 | { |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 77 | struct tfrc_tx_hist_entry *head = *headp; |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 78 | |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 79 | while (head != NULL) { |
| 80 | struct tfrc_tx_hist_entry *next = head->next; |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 81 | |
Arnaldo Carvalho de Melo | e9c8b24a | 2007-12-06 12:27:49 -0200 | [diff] [blame] | 82 | kmem_cache_free(tfrc_tx_hist_slab, head); |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 83 | head = next; |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 84 | } |
Arnaldo Carvalho de Melo | 276f2ed | 2007-11-28 11:15:40 -0200 | [diff] [blame] | 85 | |
| 86 | *headp = NULL; |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 87 | } |
Gerrit Renker | 7af5af3 | 2006-12-10 00:24:33 -0200 | [diff] [blame] | 88 | |
Gerrit Renker | 78282d2 | 2007-12-08 15:08:08 -0200 | [diff] [blame] | 89 | /* |
Gerrit Renker | aa1b1ff | 2009-09-12 07:47:01 +0000 | [diff] [blame] | 90 | * Receiver History Routines |
Gerrit Renker | 78282d2 | 2007-12-08 15:08:08 -0200 | [diff] [blame] | 91 | */ |
| 92 | static struct kmem_cache *tfrc_rx_hist_slab; |
| 93 | |
Gerrit Renker | df8f83f | 2007-12-12 12:24:49 -0200 | [diff] [blame] | 94 | int __init tfrc_rx_packet_history_init(void) |
| 95 | { |
| 96 | tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache", |
| 97 | sizeof(struct tfrc_rx_hist_entry), |
| 98 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 99 | return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0; |
| 100 | } |
| 101 | |
| 102 | void tfrc_rx_packet_history_exit(void) |
| 103 | { |
| 104 | if (tfrc_rx_hist_slab != NULL) { |
| 105 | kmem_cache_destroy(tfrc_rx_hist_slab); |
| 106 | tfrc_rx_hist_slab = NULL; |
| 107 | } |
| 108 | } |
| 109 | |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 110 | static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry, |
| 111 | const struct sk_buff *skb, |
Gerrit Renker | 5b5d0e7 | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 112 | const u64 ndp) |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 113 | { |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 114 | const struct dccp_hdr *dh = dccp_hdr(skb); |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 115 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 116 | entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq; |
| 117 | entry->tfrchrx_ccval = dh->dccph_ccval; |
| 118 | entry->tfrchrx_type = dh->dccph_type; |
| 119 | entry->tfrchrx_ndp = ndp; |
| 120 | entry->tfrchrx_tstamp = ktime_get_real(); |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 121 | } |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 122 | |
| 123 | void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h, |
| 124 | const struct sk_buff *skb, |
Gerrit Renker | 5b5d0e7 | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 125 | const u64 ndp) |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 126 | { |
| 127 | struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h); |
| 128 | |
| 129 | tfrc_rx_hist_entry_from_skb(entry, skb, ndp); |
| 130 | } |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 131 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 132 | /* has the packet contained in skb been seen before? */ |
| 133 | int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb) |
Arnaldo Carvalho de Melo | 072ab6c | 2005-08-28 01:19:14 -0300 | [diff] [blame] | 134 | { |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 135 | const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq; |
| 136 | int i; |
Arnaldo Carvalho de Melo | 072ab6c | 2005-08-28 01:19:14 -0300 | [diff] [blame] | 137 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 138 | if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0) |
| 139 | return 1; |
Arnaldo Carvalho de Melo | 072ab6c | 2005-08-28 01:19:14 -0300 | [diff] [blame] | 140 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 141 | for (i = 1; i <= h->loss_count; i++) |
| 142 | if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq) |
| 143 | return 1; |
Arnaldo Carvalho de Melo | 072ab6c | 2005-08-28 01:19:14 -0300 | [diff] [blame] | 144 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 145 | return 0; |
| 146 | } |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 147 | |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 148 | static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b) |
| 149 | { |
| 150 | const u8 idx_a = tfrc_rx_hist_index(h, a), |
| 151 | idx_b = tfrc_rx_hist_index(h, b); |
| 152 | struct tfrc_rx_hist_entry *tmp = h->ring[idx_a]; |
| 153 | |
| 154 | h->ring[idx_a] = h->ring[idx_b]; |
| 155 | h->ring[idx_b] = tmp; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Private helper functions for loss detection. |
| 160 | * |
| 161 | * In the descriptions, `Si' refers to the sequence number of entry number i, |
| 162 | * whose NDP count is `Ni' (lower case is used for variables). |
Gerrit Renker | b552c62 | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 163 | * Note: All __xxx_loss functions expect that a test against duplicates has been |
| 164 | * performed already: the seqno of the skb must not be less than the seqno |
| 165 | * of loss_prev; and it must not equal that of any valid history entry. |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 166 | */ |
Gerrit Renker | b552c62 | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 167 | static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1) |
| 168 | { |
| 169 | u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, |
| 170 | s1 = DCCP_SKB_CB(skb)->dccpd_seq; |
| 171 | |
| 172 | if (!dccp_loss_free(s0, s1, n1)) { /* gap between S0 and S1 */ |
| 173 | h->loss_count = 1; |
| 174 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n1); |
| 175 | } |
| 176 | } |
| 177 | |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 178 | static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2) |
| 179 | { |
| 180 | u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, |
| 181 | s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno, |
| 182 | s2 = DCCP_SKB_CB(skb)->dccpd_seq; |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 183 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 184 | if (likely(dccp_delta_seqno(s1, s2) > 0)) { /* S1 < S2 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 185 | h->loss_count = 2; |
| 186 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | /* S0 < S2 < S1 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 191 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 192 | if (dccp_loss_free(s0, s2, n2)) { |
| 193 | u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp; |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 194 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 195 | if (dccp_loss_free(s2, s1, n1)) { |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 196 | /* hole is filled: S0, S2, and S1 are consecutive */ |
| 197 | h->loss_count = 0; |
| 198 | h->loss_start = tfrc_rx_hist_index(h, 1); |
| 199 | } else |
| 200 | /* gap between S2 and S1: just update loss_prev */ |
| 201 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2); |
| 202 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 203 | } else { /* gap between S0 and S2 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 204 | /* |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 205 | * Reorder history to insert S2 between S0 and S1 |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 206 | */ |
| 207 | tfrc_rx_hist_swap(h, 0, 3); |
| 208 | h->loss_start = tfrc_rx_hist_index(h, 3); |
| 209 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2); |
| 210 | h->loss_count = 2; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /* return 1 if a new loss event has been identified */ |
| 215 | static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3) |
| 216 | { |
| 217 | u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, |
| 218 | s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno, |
| 219 | s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno, |
| 220 | s3 = DCCP_SKB_CB(skb)->dccpd_seq; |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 221 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 222 | if (likely(dccp_delta_seqno(s2, s3) > 0)) { /* S2 < S3 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 223 | h->loss_count = 3; |
| 224 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3); |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | /* S3 < S2 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 229 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 230 | if (dccp_delta_seqno(s1, s3) > 0) { /* S1 < S3 < S2 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 231 | /* |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 232 | * Reorder history to insert S3 between S1 and S2 |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 233 | */ |
| 234 | tfrc_rx_hist_swap(h, 2, 3); |
| 235 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3); |
| 236 | h->loss_count = 3; |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | /* S0 < S3 < S1 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 241 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 242 | if (dccp_loss_free(s0, s3, n3)) { |
| 243 | u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp; |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 244 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 245 | if (dccp_loss_free(s3, s1, n1)) { |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 246 | /* hole between S0 and S1 filled by S3 */ |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 247 | u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp; |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 248 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 249 | if (dccp_loss_free(s1, s2, n2)) { |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 250 | /* entire hole filled by S0, S3, S1, S2 */ |
| 251 | h->loss_start = tfrc_rx_hist_index(h, 2); |
| 252 | h->loss_count = 0; |
| 253 | } else { |
| 254 | /* gap remains between S1 and S2 */ |
| 255 | h->loss_start = tfrc_rx_hist_index(h, 1); |
| 256 | h->loss_count = 1; |
| 257 | } |
| 258 | |
| 259 | } else /* gap exists between S3 and S1, loss_count stays at 2 */ |
| 260 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 266 | * The remaining case: S0 < S3 < S1 < S2; gap between S0 and S3 |
| 267 | * Reorder history to insert S3 between S0 and S1. |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 268 | */ |
| 269 | tfrc_rx_hist_swap(h, 0, 3); |
| 270 | h->loss_start = tfrc_rx_hist_index(h, 3); |
| 271 | tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3); |
| 272 | h->loss_count = 3; |
| 273 | |
| 274 | return 1; |
| 275 | } |
| 276 | |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 277 | /* recycle RX history records to continue loss detection if necessary */ |
| 278 | static void __three_after_loss(struct tfrc_rx_hist *h) |
| 279 | { |
| 280 | /* |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 281 | * At this stage we know already that there is a gap between S0 and S1 |
| 282 | * (since S0 was the highest sequence number received before detecting |
| 283 | * the loss). To recycle the loss record, it is thus only necessary to |
| 284 | * check for other possible gaps between S1/S2 and between S2/S3. |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 285 | */ |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 286 | u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno, |
| 287 | s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno, |
| 288 | s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno; |
| 289 | u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp, |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 290 | n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp; |
| 291 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 292 | if (dccp_loss_free(s1, s2, n2)) { |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 293 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 294 | if (dccp_loss_free(s2, s3, n3)) { |
| 295 | /* no gap between S2 and S3: entire hole is filled */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 296 | h->loss_start = tfrc_rx_hist_index(h, 3); |
| 297 | h->loss_count = 0; |
| 298 | } else { |
| 299 | /* gap between S2 and S3 */ |
| 300 | h->loss_start = tfrc_rx_hist_index(h, 2); |
| 301 | h->loss_count = 1; |
| 302 | } |
| 303 | |
Gerrit Renker | 2013c7e | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 304 | } else { /* gap between S1 and S2 */ |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 305 | h->loss_start = tfrc_rx_hist_index(h, 1); |
| 306 | h->loss_count = 2; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * tfrc_rx_handle_loss - Loss detection and further processing |
| 312 | * @h: The non-empty RX history object |
| 313 | * @lh: Loss Intervals database to update |
| 314 | * @skb: Currently received packet |
| 315 | * @ndp: The NDP count belonging to @skb |
| 316 | * @calc_first_li: Caller-dependent computation of first loss interval in @lh |
| 317 | * @sk: Used by @calc_first_li (see tfrc_lh_interval_add) |
| 318 | * Chooses action according to pending loss, updates LI database when a new |
| 319 | * loss was detected, and does required post-processing. Returns 1 when caller |
| 320 | * should send feedback, 0 otherwise. |
Gerrit Renker | b552c62 | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 321 | * Since it also takes care of reordering during loss detection and updates the |
| 322 | * records accordingly, the caller should not perform any more RX history |
| 323 | * operations when loss_count is greater than 0 after calling this function. |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 324 | */ |
| 325 | int tfrc_rx_handle_loss(struct tfrc_rx_hist *h, |
| 326 | struct tfrc_loss_hist *lh, |
Gerrit Renker | 5b5d0e7 | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 327 | struct sk_buff *skb, const u64 ndp, |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 328 | u32 (*calc_first_li)(struct sock *), struct sock *sk) |
| 329 | { |
| 330 | int is_new_loss = 0; |
| 331 | |
Gerrit Renker | b552c62 | 2008-07-13 11:51:40 +0100 | [diff] [blame] | 332 | if (h->loss_count == 0) { |
| 333 | __do_track_loss(h, skb, ndp); |
| 334 | } else if (h->loss_count == 1) { |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 335 | __one_after_loss(h, skb, ndp); |
| 336 | } else if (h->loss_count != 2) { |
| 337 | DCCP_BUG("invalid loss_count %d", h->loss_count); |
| 338 | } else if (__two_after_loss(h, skb, ndp)) { |
| 339 | /* |
| 340 | * Update Loss Interval database and recycle RX records |
| 341 | */ |
| 342 | is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk); |
| 343 | __three_after_loss(h); |
| 344 | } |
| 345 | return is_new_loss; |
| 346 | } |
Gerrit Renker | 8a9c7e9 | 2007-12-12 13:50:51 -0200 | [diff] [blame] | 347 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 348 | int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h) |
| 349 | { |
| 350 | int i; |
| 351 | |
| 352 | for (i = 0; i <= TFRC_NDUPACK; i++) { |
| 353 | h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC); |
| 354 | if (h->ring[i] == NULL) |
| 355 | goto out_free; |
Arnaldo Carvalho de Melo | 072ab6c | 2005-08-28 01:19:14 -0300 | [diff] [blame] | 356 | } |
Arnaldo Carvalho de Melo | 072ab6c | 2005-08-28 01:19:14 -0300 | [diff] [blame] | 357 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 358 | h->loss_count = h->loss_start = 0; |
| 359 | return 0; |
Arnaldo Carvalho de Melo | 072ab6c | 2005-08-28 01:19:14 -0300 | [diff] [blame] | 360 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 361 | out_free: |
| 362 | while (i-- != 0) { |
| 363 | kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]); |
| 364 | h->ring[i] = NULL; |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 365 | } |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 366 | return -ENOBUFS; |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 367 | } |
| 368 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 369 | void tfrc_rx_hist_purge(struct tfrc_rx_hist *h) |
| 370 | { |
| 371 | int i; |
| 372 | |
| 373 | for (i = 0; i <= TFRC_NDUPACK; ++i) |
| 374 | if (h->ring[i] != NULL) { |
| 375 | kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]); |
| 376 | h->ring[i] = NULL; |
| 377 | } |
| 378 | } |
Arnaldo Carvalho de Melo | 8c60f3f | 2005-08-10 12:59:38 -0300 | [diff] [blame] | 379 | |
Arnaldo Carvalho de Melo | b84a218 | 2007-12-06 13:18:11 -0200 | [diff] [blame] | 380 | /** |
| 381 | * tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against |
| 382 | */ |
| 383 | static inline struct tfrc_rx_hist_entry * |
| 384 | tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h) |
| 385 | { |
| 386 | return h->ring[0]; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * tfrc_rx_hist_rtt_prev_s: previously suitable (wrt rtt_last_s) RTT-sampling entry |
| 391 | */ |
| 392 | static inline struct tfrc_rx_hist_entry * |
| 393 | tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h) |
| 394 | { |
| 395 | return h->ring[h->rtt_sample_prev]; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal |
| 400 | * Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able |
| 401 | * to compute a sample with given data - calling function should check this. |
| 402 | */ |
| 403 | u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb) |
| 404 | { |
| 405 | u32 sample = 0, |
| 406 | delta_v = SUB16(dccp_hdr(skb)->dccph_ccval, |
| 407 | tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval); |
| 408 | |
| 409 | if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */ |
| 410 | if (h->rtt_sample_prev == 2) { /* previous candidate stored */ |
| 411 | sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval, |
| 412 | tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval); |
| 413 | if (sample) |
| 414 | sample = 4 / sample * |
| 415 | ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp, |
| 416 | tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp); |
| 417 | else /* |
| 418 | * FIXME: This condition is in principle not |
| 419 | * possible but occurs when CCID is used for |
| 420 | * two-way data traffic. I have tried to trace |
| 421 | * it, but the cause does not seem to be here. |
| 422 | */ |
| 423 | DCCP_BUG("please report to dccp@vger.kernel.org" |
| 424 | " => prev = %u, last = %u", |
| 425 | tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval, |
| 426 | tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval); |
| 427 | } else if (delta_v < 1) { |
| 428 | h->rtt_sample_prev = 1; |
| 429 | goto keep_ref_for_next_time; |
| 430 | } |
| 431 | |
| 432 | } else if (delta_v == 4) /* optimal match */ |
| 433 | sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp)); |
| 434 | else { /* suboptimal match */ |
| 435 | h->rtt_sample_prev = 2; |
| 436 | goto keep_ref_for_next_time; |
| 437 | } |
| 438 | |
| 439 | if (unlikely(sample > DCCP_SANE_RTT_MAX)) { |
| 440 | DCCP_WARN("RTT sample %u too large, using max\n", sample); |
| 441 | sample = DCCP_SANE_RTT_MAX; |
| 442 | } |
| 443 | |
| 444 | h->rtt_sample_prev = 0; /* use current entry as next reference */ |
| 445 | keep_ref_for_next_time: |
| 446 | |
| 447 | return sample; |
| 448 | } |