qpolicy.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * @params: parameter passed to policy operation
  60. */
  61. struct dccp_qpolicy_operations {
  62. void (*push) (struct sock *sk, struct sk_buff *skb);
  63. bool (*full) (struct sock *sk);
  64. struct sk_buff* (*top) (struct sock *sk);
  65. __be32 params;
  66. };
  67. static struct dccp_qpolicy_operations qpol_table[DCCPQ_POLICY_MAX] = {
  68. [DCCPQ_POLICY_SIMPLE] = {
  69. .push = qpolicy_simple_push,
  70. .full = qpolicy_simple_full,
  71. .top = qpolicy_simple_top,
  72. .params = 0,
  73. },
  74. [DCCPQ_POLICY_PRIO] = {
  75. .push = qpolicy_simple_push,
  76. .full = qpolicy_prio_full,
  77. .top = qpolicy_prio_best_skb,
  78. .params = DCCP_SCM_PRIORITY,
  79. },
  80. };
  81. /*
  82. * Externally visible interface
  83. */
  84. void dccp_qpolicy_push(struct sock *sk, struct sk_buff *skb)
  85. {
  86. qpol_table[dccp_sk(sk)->dccps_qpolicy].push(sk, skb);
  87. }
  88. bool dccp_qpolicy_full(struct sock *sk)
  89. {
  90. return qpol_table[dccp_sk(sk)->dccps_qpolicy].full(sk);
  91. }
  92. void dccp_qpolicy_drop(struct sock *sk, struct sk_buff *skb)
  93. {
  94. if (skb != NULL) {
  95. skb_unlink(skb, &sk->sk_write_queue);
  96. kfree_skb(skb);
  97. }
  98. }
  99. struct sk_buff *dccp_qpolicy_top(struct sock *sk)
  100. {
  101. return qpol_table[dccp_sk(sk)->dccps_qpolicy].top(sk);
  102. }
  103. struct sk_buff *dccp_qpolicy_pop(struct sock *sk)
  104. {
  105. struct sk_buff *skb = dccp_qpolicy_top(sk);
  106. if (skb != NULL) {
  107. /* Clear any skb fields that we used internally */
  108. skb->priority = 0;
  109. skb_unlink(skb, &sk->sk_write_queue);
  110. }
  111. return skb;
  112. }
  113. bool dccp_qpolicy_param_ok(struct sock *sk, __be32 param)
  114. {
  115. /* check if exactly one bit is set */
  116. if (!param || (param & (param - 1)))
  117. return false;
  118. return (qpol_table[dccp_sk(sk)->dccps_qpolicy].params & param) == param;
  119. }