2 * net/dccp/ccids/lib/loss_interval.c
4 * Copyright (c) 2005-7 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005-7 Ian McDonald <ian.mcdonald@jandi.co.nz>
6 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
16 #include "../../dccp.h"
17 #include "loss_interval.h"
18 #include "packet_history.h"
21 struct dccp_li_hist *dccp_li_hist_new(const char *name)
23 struct dccp_li_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
24 static const char dccp_li_hist_mask[] = "li_hist_%s";
30 slab_name = kmalloc(strlen(name) + sizeof(dccp_li_hist_mask) - 1,
32 if (slab_name == NULL)
35 sprintf(slab_name, dccp_li_hist_mask, name);
36 hist->dccplih_slab = kmem_cache_create(slab_name,
37 sizeof(struct dccp_li_hist_entry),
38 0, SLAB_HWCACHE_ALIGN,
40 if (hist->dccplih_slab == NULL)
41 goto out_free_slab_name;
52 EXPORT_SYMBOL_GPL(dccp_li_hist_new);
54 void dccp_li_hist_delete(struct dccp_li_hist *hist)
56 const char* name = kmem_cache_name(hist->dccplih_slab);
58 kmem_cache_destroy(hist->dccplih_slab);
63 EXPORT_SYMBOL_GPL(dccp_li_hist_delete);
65 void dccp_li_hist_purge(struct dccp_li_hist *hist, struct list_head *list)
67 struct dccp_li_hist_entry *entry, *next;
69 list_for_each_entry_safe(entry, next, list, dccplih_node) {
70 list_del_init(&entry->dccplih_node);
71 kmem_cache_free(hist->dccplih_slab, entry);
75 EXPORT_SYMBOL_GPL(dccp_li_hist_purge);
77 /* Weights used to calculate loss event rate */
79 * These are integers as per section 8 of RFC3448. We can then divide by 4 *
82 static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = {
83 4, 4, 4, 4, 3, 2, 1, 1,
86 u32 dccp_li_hist_calc_i_mean(struct list_head *list)
88 struct dccp_li_hist_entry *li_entry, *li_next;
95 list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) {
96 if (li_entry->dccplih_interval != ~0U) {
97 i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i];
98 w_tot += dccp_li_hist_w[i];
100 i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1];
104 if (++i > DCCP_LI_HIST_IVAL_F_LENGTH)
108 if (i != DCCP_LI_HIST_IVAL_F_LENGTH)
111 i_tot = max(i_tot0, i_tot1);
114 DCCP_WARN("w_tot = 0\n");
118 return i_tot / w_tot;
121 EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean);
123 static int dccp_li_hist_interval_new(struct dccp_li_hist *hist,
124 struct list_head *list,
125 const u64 seq_loss, const u8 win_loss)
127 struct dccp_li_hist_entry *entry;
130 for (i = 0; i < DCCP_LI_HIST_IVAL_F_LENGTH; i++) {
131 entry = dccp_li_hist_entry_new(hist, GFP_ATOMIC);
133 dccp_li_hist_purge(hist, list);
134 DCCP_BUG("loss interval list entry is NULL");
137 entry->dccplih_interval = ~0;
138 list_add(&entry->dccplih_node, list);
141 entry->dccplih_seqno = seq_loss;
142 entry->dccplih_win_count = win_loss;
146 /* calculate first loss interval
148 * returns estimated loss interval in usecs */
149 static u32 dccp_li_calc_first_li(struct sock *sk,
150 struct list_head *hist_list,
151 struct timeval *last_feedback,
152 u16 s, u32 bytes_recv,
155 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
157 suseconds_t rtt, delta;
158 struct timeval tstamp = { 0, 0 };
164 list_for_each_entry_safe(entry, next, hist_list, dccphrx_node) {
165 if (dccp_rx_hist_entry_data_packet(entry)) {
170 tstamp = entry->dccphrx_tstamp;
171 win_count = entry->dccphrx_ccval;
175 interval = win_count - entry->dccphrx_ccval;
177 interval += TFRC_WIN_COUNT_LIMIT;
185 if (unlikely(step == 0)) {
186 DCCP_WARN("%s(%p), packet history has no data packets!\n",
191 if (unlikely(interval == 0)) {
192 DCCP_WARN("%s(%p), Could not find a win_count interval > 0."
193 "Defaulting to 1\n", dccp_role(sk), sk);
198 DCCP_CRIT("tail is null\n");
202 delta = timeval_delta(&tstamp, &tail->dccphrx_tstamp);
203 DCCP_BUG_ON(delta < 0);
205 rtt = delta * 4 / interval;
206 dccp_pr_debug("%s(%p), approximated RTT to %dus\n",
207 dccp_role(sk), sk, (int)rtt);
210 * Determine the length of the first loss interval via inverse lookup.
211 * Assume that X_recv can be computed by the throughput equation
215 * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1].
217 if (rtt == 0) { /* would result in divide-by-zero */
218 DCCP_WARN("RTT==0\n");
222 dccp_timestamp(sk, &tstamp);
223 delta = timeval_delta(&tstamp, last_feedback);
224 DCCP_BUG_ON(delta <= 0);
226 x_recv = scaled_div32(bytes_recv, delta);
227 if (x_recv == 0) { /* would also trigger divide-by-zero */
228 DCCP_WARN("X_recv==0\n");
229 if (previous_x_recv == 0) {
230 DCCP_BUG("stored value of X_recv is zero");
233 x_recv = previous_x_recv;
236 fval = scaled_div(s, rtt);
237 fval = scaled_div32(fval, x_recv);
238 p = tfrc_calc_x_reverse_lookup(fval);
240 dccp_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
241 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
249 void dccp_li_update_li(struct sock *sk, struct dccp_li_hist *li_hist,
250 struct list_head *li_hist_list,
251 struct list_head *hist_list,
252 struct timeval *last_feedback, u16 s, u32 bytes_recv,
253 u32 previous_x_recv, u64 seq_loss, u8 win_loss)
255 struct dccp_li_hist_entry *head;
258 if (list_empty(li_hist_list)) {
259 if (!dccp_li_hist_interval_new(li_hist, li_hist_list,
263 head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
265 head->dccplih_interval = dccp_li_calc_first_li(sk, hist_list,
270 struct dccp_li_hist_entry *entry;
271 struct list_head *tail;
273 head = list_entry(li_hist_list->next, struct dccp_li_hist_entry,
275 /* FIXME win count check removed as was wrong */
276 /* should make this check with receive history */
277 /* and compare there as per section 10.2 of RFC4342 */
279 /* new loss event detected */
280 /* calculate last interval length */
281 seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
282 entry = dccp_li_hist_entry_new(li_hist, GFP_ATOMIC);
285 DCCP_BUG("out of memory - can not allocate entry");
289 list_add(&entry->dccplih_node, li_hist_list);
291 tail = li_hist_list->prev;
293 kmem_cache_free(li_hist->dccplih_slab, tail);
295 /* Create the newest interval */
296 entry->dccplih_seqno = seq_loss;
297 entry->dccplih_interval = seq_temp;
298 entry->dccplih_win_count = win_loss;
302 EXPORT_SYMBOL_GPL(dccp_li_update_li);