qpolicy.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. } 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. },
  73. [DCCPQ_POLICY_PRIO] = {
  74. .push = qpolicy_simple_push,
  75. .full = qpolicy_prio_full,
  76. .top = qpolicy_prio_best_skb,
  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. }