loss_interval.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * net/dccp/ccids/lib/loss_interval.c
  3. *
  4. * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
  5. * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
  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. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include "loss_interval.h"
  16. struct dccp_li_hist *dccp_li_hist_new(const char *name)
  17. {
  18. struct dccp_li_hist *hist = kmalloc(sizeof(*hist), GFP_ATOMIC);
  19. static const char dccp_li_hist_mask[] = "li_hist_%s";
  20. char *slab_name;
  21. if (hist == NULL)
  22. goto out;
  23. slab_name = kmalloc(strlen(name) + sizeof(dccp_li_hist_mask) - 1,
  24. GFP_ATOMIC);
  25. if (slab_name == NULL)
  26. goto out_free_hist;
  27. sprintf(slab_name, dccp_li_hist_mask, name);
  28. hist->dccplih_slab = kmem_cache_create(slab_name,
  29. sizeof(struct dccp_li_hist_entry),
  30. 0, SLAB_HWCACHE_ALIGN,
  31. NULL, NULL);
  32. if (hist->dccplih_slab == NULL)
  33. goto out_free_slab_name;
  34. out:
  35. return hist;
  36. out_free_slab_name:
  37. kfree(slab_name);
  38. out_free_hist:
  39. kfree(hist);
  40. hist = NULL;
  41. goto out;
  42. }
  43. EXPORT_SYMBOL_GPL(dccp_li_hist_new);
  44. void dccp_li_hist_delete(struct dccp_li_hist *hist)
  45. {
  46. const char* name = kmem_cache_name(hist->dccplih_slab);
  47. kmem_cache_destroy(hist->dccplih_slab);
  48. kfree(name);
  49. kfree(hist);
  50. }
  51. EXPORT_SYMBOL_GPL(dccp_li_hist_delete);
  52. void dccp_li_hist_purge(struct dccp_li_hist *hist, struct list_head *list)
  53. {
  54. struct dccp_li_hist_entry *entry, *next;
  55. list_for_each_entry_safe(entry, next, list, dccplih_node) {
  56. list_del_init(&entry->dccplih_node);
  57. kmem_cache_free(hist->dccplih_slab, entry);
  58. }
  59. }
  60. EXPORT_SYMBOL_GPL(dccp_li_hist_purge);
  61. /* Weights used to calculate loss event rate */
  62. /*
  63. * These are integers as per section 8 of RFC3448. We can then divide by 4 *
  64. * when we use it.
  65. */
  66. static const int dccp_li_hist_w[DCCP_LI_HIST_IVAL_F_LENGTH] = {
  67. 4, 4, 4, 4, 3, 2, 1, 1,
  68. };
  69. u32 dccp_li_hist_calc_i_mean(struct list_head *list)
  70. {
  71. struct dccp_li_hist_entry *li_entry, *li_next;
  72. int i = 0;
  73. u32 i_tot;
  74. u32 i_tot0 = 0;
  75. u32 i_tot1 = 0;
  76. u32 w_tot = 0;
  77. list_for_each_entry_safe(li_entry, li_next, list, dccplih_node) {
  78. if (i < DCCP_LI_HIST_IVAL_F_LENGTH) {
  79. i_tot0 += li_entry->dccplih_interval * dccp_li_hist_w[i];
  80. w_tot += dccp_li_hist_w[i];
  81. }
  82. if (i != 0)
  83. i_tot1 += li_entry->dccplih_interval * dccp_li_hist_w[i - 1];
  84. if (++i > DCCP_LI_HIST_IVAL_F_LENGTH)
  85. break;
  86. }
  87. if (i != DCCP_LI_HIST_IVAL_F_LENGTH)
  88. return 0;
  89. i_tot = max(i_tot0, i_tot1);
  90. /* FIXME: Why do we do this? -Ian McDonald */
  91. if (i_tot * 4 < w_tot)
  92. i_tot = w_tot * 4;
  93. return i_tot * 4 / w_tot;
  94. }
  95. EXPORT_SYMBOL_GPL(dccp_li_hist_calc_i_mean);
  96. struct dccp_li_hist_entry *dccp_li_hist_interval_new(struct dccp_li_hist *hist,
  97. struct list_head *list,
  98. const u64 seq_loss,
  99. const u8 win_loss)
  100. {
  101. struct dccp_li_hist_entry *tail = NULL, *entry;
  102. int i;
  103. for (i = 0; i <= DCCP_LI_HIST_IVAL_F_LENGTH; ++i) {
  104. entry = dccp_li_hist_entry_new(hist, SLAB_ATOMIC);
  105. if (entry == NULL) {
  106. dccp_li_hist_purge(hist, list);
  107. return NULL;
  108. }
  109. if (tail == NULL)
  110. tail = entry;
  111. list_add(&entry->dccplih_node, list);
  112. }
  113. entry->dccplih_seqno = seq_loss;
  114. entry->dccplih_win_count = win_loss;
  115. return tail;
  116. }
  117. EXPORT_SYMBOL_GPL(dccp_li_hist_interval_new);