atm_misc.c 2.6 KB

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