Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * net/dccp/ccids/lib/loss_interval.c |
| 3 | * |
| 4 | * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand. |
Ian McDonald | e6bccd3 | 2006-08-26 19:01:30 -0700 | [diff] [blame] | 5 | * Copyright (c) 2005-6 Ian McDonald <ian.mcdonald@jandi.co.nz> |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 6 | * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 7 | * |
| 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. |
| 12 | */ |
| 13 | |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 14 | #include <linux/module.h> |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 15 | #include <net/sock.h> |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 16 | #include "../../dccp.h" |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 17 | #include "loss_interval.h" |
| 18 | |
| 19 | struct dccp_li_hist *dccp_li_hist_new(const char *name) |
| 20 | { |
| 21 | struct dccp_li_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC); |
| 22 | static const char dccp_li_hist_mask[] = "li_hist_%s"; |
| 23 | char *slab_name; |
| 24 | |
| 25 | if (hist == NULL) |
| 26 | goto out; |
| 27 | |
| 28 | slab_name = kmalloc(strlen(name) + sizeof(dccp_li_hist_mask) - 1, |
| 29 | GFP_ATOMIC); |
| 30 | if (slab_name == NULL) |
| 31 | goto out_free_hist; |
| 32 | |
| 33 | sprintf(slab_name, dccp_li_hist_mask, name); |
| 34 | hist->dccplih_slab = kmem_cache_create(slab_name, |
| 35 | sizeof(struct dccp_li_hist_entry), |
| 36 | 0, SLAB_HWCACHE_ALIGN, |
| 37 | NULL, NULL); |
| 38 | if (hist->dccplih_slab == NULL) |
| 39 | goto out_free_slab_name; |
| 40 | out: |
| 41 | return hist; |
| 42 | out_free_slab_name: |
| 43 | kfree(slab_name); |
| 44 | out_free_hist: |
| 45 | kfree(hist); |
| 46 | hist = NULL; |
| 47 | goto out; |
| 48 | } |
| 49 | |
| 50 | EXPORT_SYMBOL_GPL(dccp_li_hist_new); |
| 51 | |
| 52 | void dccp_li_hist_delete(struct dccp_li_hist *hist) |
| 53 | { |
| 54 | const char* name = kmem_cache_name(hist->dccplih_slab); |
| 55 | |
| 56 | kmem_cache_destroy(hist->dccplih_slab); |
| 57 | kfree(name); |
| 58 | kfree(hist); |
| 59 | } |
| 60 | |
| 61 | EXPORT_SYMBOL_GPL(dccp_li_hist_delete); |
| 62 | |
| 63 | void dccp_li_hist_purge(struct dccp_li_hist *hist, struct list_head *list) |
| 64 | { |
| 65 | struct dccp_li_hist_entry *entry, *next; |
| 66 | |
| 67 | list_for_each_entry_safe(entry, next, list, dccplih_node) { |
| 68 | list_del_init(&entry->dccplih_node); |
| 69 | kmem_cache_free(hist->dccplih_slab, entry); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | EXPORT_SYMBOL_GPL(dccp_li_hist_purge); |
| 74 | |
| 75 | /* Weights used to calculate loss event rate */ |
| 76 | /* |
| 77 | * These are integers as per section 8 of RFC3448. We can then divide by 4 * |
| 78 | * when we use it. |
| 79 | */ |
| 80 | static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = { |
| 81 | 4, 4, 4, 4, 3, 2, 1, 1, |
| 82 | }; |
| 83 | |
| 84 | u32 dccp_li_hist_calc_i_mean(struct list_head *list) |
| 85 | { |
| 86 | struct dccp_li_hist_entry *li_entry, *li_next; |
| 87 | int i = 0; |
| 88 | u32 i_tot; |
| 89 | u32 i_tot0 = 0; |
| 90 | u32 i_tot1 = 0; |
| 91 | u32 w_tot = 0; |
| 92 | |
| 93 | list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) { |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 94 | if (li_entry->dccplih_interval != ~0) { |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 95 | i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i]; |
| 96 | w_tot += dccp_li_hist_w[i]; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 97 | if (i != 0) |
| 98 | i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1]; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 99 | } |
| 100 | |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 101 | |
| 102 | if (++i > DCCP_LI_HIST_IVAL_F_LENGTH) |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | if (i != DCCP_LI_HIST_IVAL_F_LENGTH) |
| 107 | return 0; |
| 108 | |
| 109 | i_tot = max(i_tot0, i_tot1); |
| 110 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 111 | if (!w_tot) { |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 112 | DCCP_WARN("w_tot = 0\n"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 113 | return 1; |
| 114 | } |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 115 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 116 | return i_tot / w_tot; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean); |
| 120 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 121 | int dccp_li_hist_interval_new(struct dccp_li_hist *hist, |
| 122 | struct list_head *list, const u64 seq_loss, const u8 win_loss) |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 123 | { |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 124 | struct dccp_li_hist_entry *entry; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 125 | int i; |
| 126 | |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 127 | for (i = 0; i < DCCP_LI_HIST_IVAL_F_LENGTH; i++) { |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 128 | entry = dccp_li_hist_entry_new(hist, GFP_ATOMIC); |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 129 | if (entry == NULL) { |
| 130 | dccp_li_hist_purge(hist, list); |
Gerrit Renker | 59348b1 | 2006-11-20 18:39:23 -0200 | [diff] [blame] | 131 | DCCP_BUG("loss interval list entry is NULL"); |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 132 | return 0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 133 | } |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 134 | entry->dccplih_interval = ~0; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 135 | list_add(&entry->dccplih_node, list); |
| 136 | } |
| 137 | |
| 138 | entry->dccplih_seqno = seq_loss; |
| 139 | entry->dccplih_win_count = win_loss; |
Ian McDonald | 66a377c | 2006-08-26 23:40:50 -0700 | [diff] [blame] | 140 | return 1; |
Arnaldo Carvalho de Melo | ae6706f | 2005-08-27 23:03:09 -0300 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | EXPORT_SYMBOL_GPL(dccp_li_hist_interval_new); |