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