loss_interval.c 3.5 KB

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