qpolicy.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * net/dccp/qpolicy.c
  4. *
  5. * Policy-based packet dequeueing interface for DCCP.
  6. *
  7. * Copyright (c) 2008 Tomasz Grobelny <tomasz@grobelny.oswiecenia.net>
  8. */
  9. #include "dccp.h"
  10. /*
  11. * Simple Dequeueing Policy:
  12. * If tx_qlen is different from 0, enqueue up to tx_qlen elements.
  13. */
  14. static void qpolicy_simple_push(struct sock *sk, struct sk_buff *skb)
  15. {
  16. skb_queue_tail(&sk->sk_write_queue, skb);
  17. }
  18. static bool qpolicy_simple_full(struct sock *sk)
  19. {
  20. return dccp_sk(sk)->dccps_tx_qlen &&
  21. sk->sk_write_queue.qlen >= dccp_sk(sk)->dccps_tx_qlen;
  22. }
  23. static struct sk_buff *qpolicy_simple_top(struct sock *sk)
  24. {
  25. return skb_peek(&sk->sk_write_queue);
  26. }
  27. /*
  28. * Priority-based Dequeueing Policy:
  29. * If tx_qlen is different from 0 and the queue has reached its upper bound
  30. * of tx_qlen elements, replace older packets lowest-priority-first.
  31. */
  32. static struct sk_buff *qpolicy_prio_best_skb(struct sock *sk)
  33. {
  34. struct sk_buff *skb, *best = NULL;
  35. skb_queue_walk(&sk->sk_write_queue, skb)
  36. if (best == NULL || skb->priority > best->priority)
  37. best = skb;
  38. return best;
  39. }
  40. static struct sk_buff *qpolicy_prio_worst_skb(struct sock *sk)
  41. {
  42. struct sk_buff *skb, *worst = NULL;
  43. skb_queue_walk(&sk->sk_write_queue, skb)
  44. if (worst == NULL || skb->priority < worst->priority)
  45. worst = skb;
  46. return worst;
  47. }
  48. static bool qpolicy_prio_full(struct sock *sk)
  49. {
  50. if (qpolicy_simple_full(sk))
  51. dccp_qpolicy_drop(sk, qpolicy_prio_worst_skb(sk));
  52. return false;
  53. }
  54. /**
  55. * struct dccp_qpolicy_operations - TX Packet Dequeueing Interface
  56. * @push: add a new @skb to the write queue
  57. * @full: indicates that no more packets will be admitted
  58. * @top: peeks at whatever the queueing policy defines as its `top'
  59. */
  60. static struct dccp_qpolicy_operations {
  61. void (*push) (struct sock *sk, struct sk_buff *skb);
  62. bool (*full) (struct sock *sk);
  63. struct sk_buff* (*top) (struct sock *sk);
  64. __be32 params;
  65. } qpol_table[DCCPQ_POLICY_MAX] = {
  66. [DCCPQ_POLICY_SIMPLE] = {
  67. .push = qpolicy_simple_push,
  68. .full = qpolicy_simple_full,
  69. .top = qpolicy_simple_top,
  70. .params = 0,
  71. },
  72. [DCCPQ_POLICY_PRIO] = {
  73. .push = qpolicy_simple_push,
  74. .full = qpolicy_prio_full,
  75. .top = qpolicy_prio_best_skb,
  76. .params = DCCP_SCM_PRIORITY,
  77. },
  78. };
  79. /*
  80. * Externally visible interface
  81. */
  82. void dccp_qpolicy_push(struct sock *sk, struct sk_buff *skb)
  83. {
  84. qpol_table[dccp_sk(sk)->dccps_qpolicy].push(sk, skb);
  85. }
  86. bool dccp_qpolicy_full(struct sock *sk)
  87. {
  88. return qpol_table[dccp_sk(sk)->dccps_qpolicy].full(sk);
  89. }
  90. void dccp_qpolicy_drop(struct sock *sk, struct sk_buff *skb)
  91. {
  92. if (skb != NULL) {
  93. skb_unlink(skb, &sk->sk_write_queue);
  94. kfree_skb(skb);
  95. }
  96. }
  97. struct sk_buff *dccp_qpolicy_top(struct sock *sk)
  98. {
  99. return qpol_table[dccp_sk(sk)->dccps_qpolicy].top(sk);
  100. }
  101. struct sk_buff *dccp_qpolicy_pop(struct sock *sk)
  102. {
  103. struct sk_buff *skb = dccp_qpolicy_top(sk);
  104. if (skb != NULL) {
  105. /* Clear any skb fields that we used internally */
  106. skb->priority = 0;
  107. skb_unlink(skb, &sk->sk_write_queue);
  108. }
  109. return skb;
  110. }
  111. bool dccp_qpolicy_param_ok(struct sock *sk, __be32 param)
  112. {
  113. /* check if exactly one bit is set */
  114. if (!param || (param & (param - 1)))
  115. return false;
  116. return (qpol_table[dccp_sk(sk)->dccps_qpolicy].params & param) == param;
  117. }