atm_misc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* net/atm/atm_misc.c - Various functions for use by ATM drivers */
  2. /* Written 1995-2000 by Werner Almesberger, EPFL ICA */
  3. #include <linux/module.h>
  4. #include <linux/atm.h>
  5. #include <linux/atmdev.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/sonet.h>
  8. #include <linux/bitops.h>
  9. #include <linux/errno.h>
  10. #include <linux/atomic.h>
  11. int atm_charge(struct atm_vcc *vcc, int truesize)
  12. {
  13. atm_force_charge(vcc, truesize);
  14. if (atomic_read(&sk_atm(vcc)->sk_rmem_alloc) <= sk_atm(vcc)->sk_rcvbuf)
  15. return 1;
  16. atm_return(vcc, truesize);
  17. atomic_inc(&vcc->stats->rx_drop);
  18. return 0;
  19. }
  20. EXPORT_SYMBOL(atm_charge);
  21. struct sk_buff *atm_alloc_charge(struct atm_vcc *vcc, int pdu_size,
  22. gfp_t gfp_flags)
  23. {
  24. struct sock *sk = sk_atm(vcc);
  25. int guess = SKB_TRUESIZE(pdu_size);
  26. atm_force_charge(vcc, guess);
  27. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
  28. struct sk_buff *skb = alloc_skb(pdu_size, gfp_flags);
  29. if (skb) {
  30. atomic_add(skb->truesize-guess,
  31. &sk->sk_rmem_alloc);
  32. return skb;
  33. }
  34. }
  35. atm_return(vcc, guess);
  36. atomic_inc(&vcc->stats->rx_drop);
  37. return NULL;
  38. }
  39. EXPORT_SYMBOL(atm_alloc_charge);
  40. /*
  41. * atm_pcr_goal returns the positive PCR if it should be rounded up, the
  42. * negative PCR if it should be rounded down, and zero if the maximum available
  43. * bandwidth should be used.
  44. *
  45. * The rules are as follows (* = maximum, - = absent (0), x = value "x",
  46. * (x+ = x or next value above x, x- = x or next value below):
  47. *
  48. * min max pcr result min max pcr result
  49. * - - - * (UBR only) x - - x+
  50. * - - * * x - * *
  51. * - - z z- x - z z-
  52. * - * - * x * - x+
  53. * - * * * x * * *
  54. * - * z z- x * z z-
  55. * - y - y- x y - x+
  56. * - y * y- x y * y-
  57. * - y z z- x y z z-
  58. *
  59. * All non-error cases can be converted with the following simple set of rules:
  60. *
  61. * if pcr == z then z-
  62. * else if min == x && pcr == - then x+
  63. * else if max == y then y-
  64. * else *
  65. */
  66. int atm_pcr_goal(const struct atm_trafprm *tp)
  67. {
  68. if (tp->pcr && tp->pcr != ATM_MAX_PCR)
  69. return -tp->pcr;
  70. if (tp->min_pcr && !tp->pcr)
  71. return tp->min_pcr;
  72. if (tp->max_pcr != ATM_MAX_PCR)
  73. return -tp->max_pcr;
  74. return 0;
  75. }
  76. EXPORT_SYMBOL(atm_pcr_goal);
  77. void sonet_copy_stats(struct k_sonet_stats *from, struct sonet_stats *to)
  78. {
  79. #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
  80. __SONET_ITEMS
  81. #undef __HANDLE_ITEM
  82. }
  83. EXPORT_SYMBOL(sonet_copy_stats);
  84. void sonet_subtract_stats(struct k_sonet_stats *from, struct sonet_stats *to)
  85. {
  86. #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
  87. __SONET_ITEMS
  88. #undef __HANDLE_ITEM
  89. }
  90. EXPORT_SYMBOL(sonet_subtract_stats);