qpolicy.c 3.4 KB

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