fifo.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * lib/route/qdisc/fifo.c (p|b)fifo
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup qdisc
  13. * @defgroup qdisc_fifo Packet/Bytes FIFO (pfifo/bfifo)
  14. * @brief
  15. *
  16. * The FIFO qdisc comes in two flavours:
  17. * @par bfifo (Byte FIFO)
  18. * Allows enqueuing until the currently queued volume in bytes exceeds
  19. * the configured limit.backlog contains currently enqueued volume in bytes.
  20. *
  21. * @par pfifo (Packet FIFO)
  22. * Allows enquueing until the currently queued number of packets
  23. * exceeds the configured limit.
  24. *
  25. * The configuration is exactly the same, the decision which of
  26. * the two variations is going to be used is made based on the
  27. * kind of the qdisc (rtnl_tc_set_kind()).
  28. * @{
  29. */
  30. #include <netlink-private/netlink.h>
  31. #include <netlink-private/tc.h>
  32. #include <netlink/netlink.h>
  33. #include <netlink-private/route/tc-api.h>
  34. #include <netlink/route/qdisc.h>
  35. #include <netlink/route/qdisc/fifo.h>
  36. #include <netlink/utils.h>
  37. /** @cond SKIP */
  38. #define SCH_FIFO_ATTR_LIMIT 1
  39. /** @endcond */
  40. static int fifo_msg_parser(struct rtnl_tc *tc, void *data)
  41. {
  42. struct rtnl_fifo *fifo = data;
  43. struct tc_fifo_qopt *opt;
  44. if (tc->tc_opts->d_size < sizeof(struct tc_fifo_qopt))
  45. return -NLE_INVAL;
  46. opt = (struct tc_fifo_qopt *) tc->tc_opts->d_data;
  47. fifo->qf_limit = opt->limit;
  48. fifo->qf_mask = SCH_FIFO_ATTR_LIMIT;
  49. return 0;
  50. }
  51. static void pfifo_dump_line(struct rtnl_tc *tc, void *data,
  52. struct nl_dump_params *p)
  53. {
  54. struct rtnl_fifo *fifo = data;
  55. if (fifo)
  56. nl_dump(p, " limit %u packets", fifo->qf_limit);
  57. }
  58. static void bfifo_dump_line(struct rtnl_tc *tc, void *data,
  59. struct nl_dump_params *p)
  60. {
  61. struct rtnl_fifo *fifo = data;
  62. char *unit;
  63. double r;
  64. if (!fifo)
  65. return;
  66. r = nl_cancel_down_bytes(fifo->qf_limit, &unit);
  67. nl_dump(p, " limit %.1f%s", r, unit);
  68. }
  69. static int fifo_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
  70. {
  71. struct rtnl_fifo *fifo = data;
  72. struct tc_fifo_qopt opts = {0};
  73. if (!fifo || !(fifo->qf_mask & SCH_FIFO_ATTR_LIMIT))
  74. return -NLE_INVAL;
  75. opts.limit = fifo->qf_limit;
  76. return nlmsg_append(msg, &opts, sizeof(opts), NL_DONTPAD);
  77. }
  78. /**
  79. * @name Attribute Modification
  80. * @{
  81. */
  82. /**
  83. * Set limit of FIFO qdisc.
  84. * @arg qdisc FIFO qdisc to be modified.
  85. * @arg limit New limit.
  86. * @return 0 on success or a negative error code.
  87. */
  88. int rtnl_qdisc_fifo_set_limit(struct rtnl_qdisc *qdisc, int limit)
  89. {
  90. struct rtnl_fifo *fifo;
  91. if (!(fifo = rtnl_tc_data(TC_CAST(qdisc))))
  92. return -NLE_NOMEM;
  93. fifo->qf_limit = limit;
  94. fifo->qf_mask |= SCH_FIFO_ATTR_LIMIT;
  95. return 0;
  96. }
  97. /**
  98. * Get limit of a FIFO qdisc.
  99. * @arg qdisc FIFO qdisc.
  100. * @return Numeric limit or a negative error code.
  101. */
  102. int rtnl_qdisc_fifo_get_limit(struct rtnl_qdisc *qdisc)
  103. {
  104. struct rtnl_fifo *fifo;
  105. if (!(fifo = rtnl_tc_data(TC_CAST(qdisc))))
  106. return -NLE_NOMEM;
  107. if (fifo->qf_mask & SCH_FIFO_ATTR_LIMIT)
  108. return fifo->qf_limit;
  109. else
  110. return -NLE_NOATTR;
  111. }
  112. /** @} */
  113. static struct rtnl_tc_ops pfifo_ops = {
  114. .to_kind = "pfifo",
  115. .to_type = RTNL_TC_TYPE_QDISC,
  116. .to_size = sizeof(struct rtnl_fifo),
  117. .to_msg_parser = fifo_msg_parser,
  118. .to_dump[NL_DUMP_LINE] = pfifo_dump_line,
  119. .to_msg_fill = fifo_msg_fill,
  120. };
  121. static struct rtnl_tc_ops bfifo_ops = {
  122. .to_kind = "bfifo",
  123. .to_type = RTNL_TC_TYPE_QDISC,
  124. .to_size = sizeof(struct rtnl_fifo),
  125. .to_msg_parser = fifo_msg_parser,
  126. .to_dump[NL_DUMP_LINE] = bfifo_dump_line,
  127. .to_msg_fill = fifo_msg_fill,
  128. };
  129. static void __init fifo_init(void)
  130. {
  131. rtnl_tc_register(&pfifo_ops);
  132. rtnl_tc_register(&bfifo_ops);
  133. }
  134. static void __exit fifo_exit(void)
  135. {
  136. rtnl_tc_unregister(&pfifo_ops);
  137. rtnl_tc_unregister(&bfifo_ops);
  138. }
  139. /** @} */