atm_misc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <asm/atomic.h>
  10. #include <asm/errno.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. struct sk_buff *atm_alloc_charge(struct atm_vcc *vcc,int pdu_size,
  21. gfp_t gfp_flags)
  22. {
  23. struct sock *sk = sk_atm(vcc);
  24. int guess = atm_guess_pdu2truesize(pdu_size);
  25. atm_force_charge(vcc,guess);
  26. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
  27. struct sk_buff *skb = alloc_skb(pdu_size,gfp_flags);
  28. if (skb) {
  29. atomic_add(skb->truesize-guess,
  30. &sk->sk_rmem_alloc);
  31. return skb;
  32. }
  33. }
  34. atm_return(vcc,guess);
  35. atomic_inc(&vcc->stats->rx_drop);
  36. return NULL;
  37. }
  38. /*
  39. * atm_pcr_goal returns the positive PCR if it should be rounded up, the
  40. * negative PCR if it should be rounded down, and zero if the maximum available
  41. * bandwidth should be used.
  42. *
  43. * The rules are as follows (* = maximum, - = absent (0), x = value "x",
  44. * (x+ = x or next value above x, x- = x or next value below):
  45. *
  46. * min max pcr result min max pcr result
  47. * - - - * (UBR only) x - - x+
  48. * - - * * x - * *
  49. * - - z z- x - z z-
  50. * - * - * x * - x+
  51. * - * * * x * * *
  52. * - * z z- x * z z-
  53. * - y - y- x y - x+
  54. * - y * y- x y * y-
  55. * - y z z- x y z z-
  56. *
  57. * All non-error cases can be converted with the following simple set of rules:
  58. *
  59. * if pcr == z then z-
  60. * else if min == x && pcr == - then x+
  61. * else if max == y then y-
  62. * else *
  63. */
  64. int atm_pcr_goal(const struct atm_trafprm *tp)
  65. {
  66. if (tp->pcr && tp->pcr != ATM_MAX_PCR)
  67. return -tp->pcr;
  68. if (tp->min_pcr && !tp->pcr)
  69. return tp->min_pcr;
  70. if (tp->max_pcr != ATM_MAX_PCR)
  71. return -tp->max_pcr;
  72. return 0;
  73. }
  74. void sonet_copy_stats(struct k_sonet_stats *from,struct sonet_stats *to)
  75. {
  76. #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
  77. __SONET_ITEMS
  78. #undef __HANDLE_ITEM
  79. }
  80. void sonet_subtract_stats(struct k_sonet_stats *from,struct sonet_stats *to)
  81. {
  82. #define __HANDLE_ITEM(i) atomic_sub(to->i,&from->i)
  83. __SONET_ITEMS
  84. #undef __HANDLE_ITEM
  85. }
  86. EXPORT_SYMBOL(atm_charge);
  87. EXPORT_SYMBOL(atm_alloc_charge);
  88. EXPORT_SYMBOL(atm_pcr_goal);
  89. EXPORT_SYMBOL(sonet_copy_stats);
  90. EXPORT_SYMBOL(sonet_subtract_stats);