Path: blob/master/net/dccp/ccids/lib/packet_history.c
15112 views
/*1* Copyright (c) 2007 The University of Aberdeen, Scotland, UK2* Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.3*4* An implementation of the DCCP protocol5*6* This code has been developed by the University of Waikato WAND7* research group. For further information please see http://www.wand.net.nz/8* or e-mail Ian McDonald - [email protected]9*10* This code also uses code from Lulea University, rereleased as GPL by its11* authors:12* Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon13*14* Changes to meet Linux coding standards, to make it meet latest ccid3 draft15* and to make it work as a loadable module in the DCCP stack written by16* Arnaldo Carvalho de Melo <[email protected]>.17*18* Copyright (c) 2005 Arnaldo Carvalho de Melo <[email protected]>19*20* This program is free software; you can redistribute it and/or modify21* it under the terms of the GNU General Public License as published by22* the Free Software Foundation; either version 2 of the License, or23* (at your option) any later version.24*25* This program is distributed in the hope that it will be useful,26* but WITHOUT ANY WARRANTY; without even the implied warranty of27* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the28* GNU General Public License for more details.29*30* You should have received a copy of the GNU General Public License31* along with this program; if not, write to the Free Software32* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.33*/3435#include <linux/string.h>36#include <linux/slab.h>37#include "packet_history.h"38#include "../../dccp.h"3940/*41* Transmitter History Routines42*/43static struct kmem_cache *tfrc_tx_hist_slab;4445int __init tfrc_tx_packet_history_init(void)46{47tfrc_tx_hist_slab = kmem_cache_create("tfrc_tx_hist",48sizeof(struct tfrc_tx_hist_entry),490, SLAB_HWCACHE_ALIGN, NULL);50return tfrc_tx_hist_slab == NULL ? -ENOBUFS : 0;51}5253void tfrc_tx_packet_history_exit(void)54{55if (tfrc_tx_hist_slab != NULL) {56kmem_cache_destroy(tfrc_tx_hist_slab);57tfrc_tx_hist_slab = NULL;58}59}6061int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno)62{63struct tfrc_tx_hist_entry *entry = kmem_cache_alloc(tfrc_tx_hist_slab, gfp_any());6465if (entry == NULL)66return -ENOBUFS;67entry->seqno = seqno;68entry->stamp = ktime_get_real();69entry->next = *headp;70*headp = entry;71return 0;72}7374void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp)75{76struct tfrc_tx_hist_entry *head = *headp;7778while (head != NULL) {79struct tfrc_tx_hist_entry *next = head->next;8081kmem_cache_free(tfrc_tx_hist_slab, head);82head = next;83}8485*headp = NULL;86}8788/*89* Receiver History Routines90*/91static struct kmem_cache *tfrc_rx_hist_slab;9293int __init tfrc_rx_packet_history_init(void)94{95tfrc_rx_hist_slab = kmem_cache_create("tfrc_rxh_cache",96sizeof(struct tfrc_rx_hist_entry),970, SLAB_HWCACHE_ALIGN, NULL);98return tfrc_rx_hist_slab == NULL ? -ENOBUFS : 0;99}100101void tfrc_rx_packet_history_exit(void)102{103if (tfrc_rx_hist_slab != NULL) {104kmem_cache_destroy(tfrc_rx_hist_slab);105tfrc_rx_hist_slab = NULL;106}107}108109static inline void tfrc_rx_hist_entry_from_skb(struct tfrc_rx_hist_entry *entry,110const struct sk_buff *skb,111const u64 ndp)112{113const struct dccp_hdr *dh = dccp_hdr(skb);114115entry->tfrchrx_seqno = DCCP_SKB_CB(skb)->dccpd_seq;116entry->tfrchrx_ccval = dh->dccph_ccval;117entry->tfrchrx_type = dh->dccph_type;118entry->tfrchrx_ndp = ndp;119entry->tfrchrx_tstamp = ktime_get_real();120}121122void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h,123const struct sk_buff *skb,124const u64 ndp)125{126struct tfrc_rx_hist_entry *entry = tfrc_rx_hist_last_rcv(h);127128tfrc_rx_hist_entry_from_skb(entry, skb, ndp);129}130131/* has the packet contained in skb been seen before? */132int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb)133{134const u64 seq = DCCP_SKB_CB(skb)->dccpd_seq;135int i;136137if (dccp_delta_seqno(tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno, seq) <= 0)138return 1;139140for (i = 1; i <= h->loss_count; i++)141if (tfrc_rx_hist_entry(h, i)->tfrchrx_seqno == seq)142return 1;143144return 0;145}146147static void tfrc_rx_hist_swap(struct tfrc_rx_hist *h, const u8 a, const u8 b)148{149const u8 idx_a = tfrc_rx_hist_index(h, a),150idx_b = tfrc_rx_hist_index(h, b);151struct tfrc_rx_hist_entry *tmp = h->ring[idx_a];152153h->ring[idx_a] = h->ring[idx_b];154h->ring[idx_b] = tmp;155}156157/*158* Private helper functions for loss detection.159*160* In the descriptions, `Si' refers to the sequence number of entry number i,161* whose NDP count is `Ni' (lower case is used for variables).162* Note: All __xxx_loss functions expect that a test against duplicates has been163* performed already: the seqno of the skb must not be less than the seqno164* of loss_prev; and it must not equal that of any valid history entry.165*/166static void __do_track_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u64 n1)167{168u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,169s1 = DCCP_SKB_CB(skb)->dccpd_seq;170171if (!dccp_loss_free(s0, s1, n1)) { /* gap between S0 and S1 */172h->loss_count = 1;173tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n1);174}175}176177static void __one_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n2)178{179u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,180s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,181s2 = DCCP_SKB_CB(skb)->dccpd_seq;182183if (likely(dccp_delta_seqno(s1, s2) > 0)) { /* S1 < S2 */184h->loss_count = 2;185tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n2);186return;187}188189/* S0 < S2 < S1 */190191if (dccp_loss_free(s0, s2, n2)) {192u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;193194if (dccp_loss_free(s2, s1, n1)) {195/* hole is filled: S0, S2, and S1 are consecutive */196h->loss_count = 0;197h->loss_start = tfrc_rx_hist_index(h, 1);198} else199/* gap between S2 and S1: just update loss_prev */200tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n2);201202} else { /* gap between S0 and S2 */203/*204* Reorder history to insert S2 between S0 and S1205*/206tfrc_rx_hist_swap(h, 0, 3);207h->loss_start = tfrc_rx_hist_index(h, 3);208tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n2);209h->loss_count = 2;210}211}212213/* return 1 if a new loss event has been identified */214static int __two_after_loss(struct tfrc_rx_hist *h, struct sk_buff *skb, u32 n3)215{216u64 s0 = tfrc_rx_hist_loss_prev(h)->tfrchrx_seqno,217s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,218s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,219s3 = DCCP_SKB_CB(skb)->dccpd_seq;220221if (likely(dccp_delta_seqno(s2, s3) > 0)) { /* S2 < S3 */222h->loss_count = 3;223tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 3), skb, n3);224return 1;225}226227/* S3 < S2 */228229if (dccp_delta_seqno(s1, s3) > 0) { /* S1 < S3 < S2 */230/*231* Reorder history to insert S3 between S1 and S2232*/233tfrc_rx_hist_swap(h, 2, 3);234tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 2), skb, n3);235h->loss_count = 3;236return 1;237}238239/* S0 < S3 < S1 */240241if (dccp_loss_free(s0, s3, n3)) {242u64 n1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_ndp;243244if (dccp_loss_free(s3, s1, n1)) {245/* hole between S0 and S1 filled by S3 */246u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp;247248if (dccp_loss_free(s1, s2, n2)) {249/* entire hole filled by S0, S3, S1, S2 */250h->loss_start = tfrc_rx_hist_index(h, 2);251h->loss_count = 0;252} else {253/* gap remains between S1 and S2 */254h->loss_start = tfrc_rx_hist_index(h, 1);255h->loss_count = 1;256}257258} else /* gap exists between S3 and S1, loss_count stays at 2 */259tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_loss_prev(h), skb, n3);260261return 0;262}263264/*265* The remaining case: S0 < S3 < S1 < S2; gap between S0 and S3266* Reorder history to insert S3 between S0 and S1.267*/268tfrc_rx_hist_swap(h, 0, 3);269h->loss_start = tfrc_rx_hist_index(h, 3);270tfrc_rx_hist_entry_from_skb(tfrc_rx_hist_entry(h, 1), skb, n3);271h->loss_count = 3;272273return 1;274}275276/* recycle RX history records to continue loss detection if necessary */277static void __three_after_loss(struct tfrc_rx_hist *h)278{279/*280* At this stage we know already that there is a gap between S0 and S1281* (since S0 was the highest sequence number received before detecting282* the loss). To recycle the loss record, it is thus only necessary to283* check for other possible gaps between S1/S2 and between S2/S3.284*/285u64 s1 = tfrc_rx_hist_entry(h, 1)->tfrchrx_seqno,286s2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_seqno,287s3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_seqno;288u64 n2 = tfrc_rx_hist_entry(h, 2)->tfrchrx_ndp,289n3 = tfrc_rx_hist_entry(h, 3)->tfrchrx_ndp;290291if (dccp_loss_free(s1, s2, n2)) {292293if (dccp_loss_free(s2, s3, n3)) {294/* no gap between S2 and S3: entire hole is filled */295h->loss_start = tfrc_rx_hist_index(h, 3);296h->loss_count = 0;297} else {298/* gap between S2 and S3 */299h->loss_start = tfrc_rx_hist_index(h, 2);300h->loss_count = 1;301}302303} else { /* gap between S1 and S2 */304h->loss_start = tfrc_rx_hist_index(h, 1);305h->loss_count = 2;306}307}308309/**310* tfrc_rx_handle_loss - Loss detection and further processing311* @h: The non-empty RX history object312* @lh: Loss Intervals database to update313* @skb: Currently received packet314* @ndp: The NDP count belonging to @skb315* @calc_first_li: Caller-dependent computation of first loss interval in @lh316* @sk: Used by @calc_first_li (see tfrc_lh_interval_add)317* Chooses action according to pending loss, updates LI database when a new318* loss was detected, and does required post-processing. Returns 1 when caller319* should send feedback, 0 otherwise.320* Since it also takes care of reordering during loss detection and updates the321* records accordingly, the caller should not perform any more RX history322* operations when loss_count is greater than 0 after calling this function.323*/324int tfrc_rx_handle_loss(struct tfrc_rx_hist *h,325struct tfrc_loss_hist *lh,326struct sk_buff *skb, const u64 ndp,327u32 (*calc_first_li)(struct sock *), struct sock *sk)328{329int is_new_loss = 0;330331if (h->loss_count == 0) {332__do_track_loss(h, skb, ndp);333} else if (h->loss_count == 1) {334__one_after_loss(h, skb, ndp);335} else if (h->loss_count != 2) {336DCCP_BUG("invalid loss_count %d", h->loss_count);337} else if (__two_after_loss(h, skb, ndp)) {338/*339* Update Loss Interval database and recycle RX records340*/341is_new_loss = tfrc_lh_interval_add(lh, h, calc_first_li, sk);342__three_after_loss(h);343}344return is_new_loss;345}346347int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h)348{349int i;350351for (i = 0; i <= TFRC_NDUPACK; i++) {352h->ring[i] = kmem_cache_alloc(tfrc_rx_hist_slab, GFP_ATOMIC);353if (h->ring[i] == NULL)354goto out_free;355}356357h->loss_count = h->loss_start = 0;358return 0;359360out_free:361while (i-- != 0) {362kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);363h->ring[i] = NULL;364}365return -ENOBUFS;366}367368void tfrc_rx_hist_purge(struct tfrc_rx_hist *h)369{370int i;371372for (i = 0; i <= TFRC_NDUPACK; ++i)373if (h->ring[i] != NULL) {374kmem_cache_free(tfrc_rx_hist_slab, h->ring[i]);375h->ring[i] = NULL;376}377}378379/**380* tfrc_rx_hist_rtt_last_s - reference entry to compute RTT samples against381*/382static inline struct tfrc_rx_hist_entry *383tfrc_rx_hist_rtt_last_s(const struct tfrc_rx_hist *h)384{385return h->ring[0];386}387388/**389* tfrc_rx_hist_rtt_prev_s: previously suitable (wrt rtt_last_s) RTT-sampling entry390*/391static inline struct tfrc_rx_hist_entry *392tfrc_rx_hist_rtt_prev_s(const struct tfrc_rx_hist *h)393{394return h->ring[h->rtt_sample_prev];395}396397/**398* tfrc_rx_hist_sample_rtt - Sample RTT from timestamp / CCVal399* Based on ideas presented in RFC 4342, 8.1. Returns 0 if it was not able400* to compute a sample with given data - calling function should check this.401*/402u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb)403{404u32 sample = 0,405delta_v = SUB16(dccp_hdr(skb)->dccph_ccval,406tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);407408if (delta_v < 1 || delta_v > 4) { /* unsuitable CCVal delta */409if (h->rtt_sample_prev == 2) { /* previous candidate stored */410sample = SUB16(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,411tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);412if (sample)413sample = 4 / sample *414ktime_us_delta(tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_tstamp,415tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp);416else /*417* FIXME: This condition is in principle not418* possible but occurs when CCID is used for419* two-way data traffic. I have tried to trace420* it, but the cause does not seem to be here.421*/422DCCP_BUG("please report to [email protected]"423" => prev = %u, last = %u",424tfrc_rx_hist_rtt_prev_s(h)->tfrchrx_ccval,425tfrc_rx_hist_rtt_last_s(h)->tfrchrx_ccval);426} else if (delta_v < 1) {427h->rtt_sample_prev = 1;428goto keep_ref_for_next_time;429}430431} else if (delta_v == 4) /* optimal match */432sample = ktime_to_us(net_timedelta(tfrc_rx_hist_rtt_last_s(h)->tfrchrx_tstamp));433else { /* suboptimal match */434h->rtt_sample_prev = 2;435goto keep_ref_for_next_time;436}437438if (unlikely(sample > DCCP_SANE_RTT_MAX)) {439DCCP_WARN("RTT sample %u too large, using max\n", sample);440sample = DCCP_SANE_RTT_MAX;441}442443h->rtt_sample_prev = 0; /* use current entry as next reference */444keep_ref_for_next_time:445446return sample;447}448449450