sch_plug.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * sch_plug.c Queue traffic until an explicit release command
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * There are two ways to use this qdisc:
  10. * 1. A simple "instantaneous" plug/unplug operation, by issuing an alternating
  11. * sequence of TCQ_PLUG_BUFFER & TCQ_PLUG_RELEASE_INDEFINITE commands.
  12. *
  13. * 2. For network output buffering (a.k.a output commit) functionality.
  14. * Output commit property is commonly used by applications using checkpoint
  15. * based fault-tolerance to ensure that the checkpoint from which a system
  16. * is being restored is consistent w.r.t outside world.
  17. *
  18. * Consider for e.g. Remus - a Virtual Machine checkpointing system,
  19. * wherein a VM is checkpointed, say every 50ms. The checkpoint is replicated
  20. * asynchronously to the backup host, while the VM continues executing the
  21. * next epoch speculatively.
  22. *
  23. * The following is a typical sequence of output buffer operations:
  24. * 1.At epoch i, start_buffer(i)
  25. * 2. At end of epoch i (i.e. after 50ms):
  26. * 2.1 Stop VM and take checkpoint(i).
  27. * 2.2 start_buffer(i+1) and Resume VM
  28. * 3. While speculatively executing epoch(i+1), asynchronously replicate
  29. * checkpoint(i) to backup host.
  30. * 4. When checkpoint_ack(i) is received from backup, release_buffer(i)
  31. * Thus, this Qdisc would receive the following sequence of commands:
  32. * TCQ_PLUG_BUFFER (epoch i)
  33. * .. TCQ_PLUG_BUFFER (epoch i+1)
  34. * ....TCQ_PLUG_RELEASE_ONE (epoch i)
  35. * ......TCQ_PLUG_BUFFER (epoch i+2)
  36. * ........
  37. */
  38. #include <linux/module.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/errno.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <net/pkt_sched.h>
  45. /*
  46. * State of the queue, when used for network output buffering:
  47. *
  48. * plug(i+1) plug(i) head
  49. * ------------------+--------------------+---------------->
  50. * | |
  51. * | |
  52. * pkts_current_epoch| pkts_last_epoch |pkts_to_release
  53. * ----------------->|<--------+--------->|+--------------->
  54. * v v
  55. *
  56. */
  57. struct plug_sched_data {
  58. /* If true, the dequeue function releases all packets
  59. * from head to end of the queue. The queue turns into
  60. * a pass-through queue for newly arriving packets.
  61. */
  62. bool unplug_indefinite;
  63. bool throttled;
  64. /* Queue Limit in bytes */
  65. u32 limit;
  66. /* Number of packets (output) from the current speculatively
  67. * executing epoch.
  68. */
  69. u32 pkts_current_epoch;
  70. /* Number of packets corresponding to the recently finished
  71. * epoch. These will be released when we receive a
  72. * TCQ_PLUG_RELEASE_ONE command. This command is typically
  73. * issued after committing a checkpoint at the target.
  74. */
  75. u32 pkts_last_epoch;
  76. /*
  77. * Number of packets from the head of the queue, that can
  78. * be released (committed checkpoint).
  79. */
  80. u32 pkts_to_release;
  81. };
  82. static int plug_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  83. struct sk_buff **to_free)
  84. {
  85. struct plug_sched_data *q = qdisc_priv(sch);
  86. if (likely(sch->qstats.backlog + skb->len <= q->limit)) {
  87. if (!q->unplug_indefinite)
  88. q->pkts_current_epoch++;
  89. return qdisc_enqueue_tail(skb, sch);
  90. }
  91. return qdisc_drop(skb, sch, to_free);
  92. }
  93. static struct sk_buff *plug_dequeue(struct Qdisc *sch)
  94. {
  95. struct plug_sched_data *q = qdisc_priv(sch);
  96. if (q->throttled)
  97. return NULL;
  98. if (!q->unplug_indefinite) {
  99. if (!q->pkts_to_release) {
  100. /* No more packets to dequeue. Block the queue
  101. * and wait for the next release command.
  102. */
  103. q->throttled = true;
  104. return NULL;
  105. }
  106. q->pkts_to_release--;
  107. }
  108. return qdisc_dequeue_head(sch);
  109. }
  110. static int plug_init(struct Qdisc *sch, struct nlattr *opt,
  111. struct netlink_ext_ack *extack)
  112. {
  113. struct plug_sched_data *q = qdisc_priv(sch);
  114. q->pkts_current_epoch = 0;
  115. q->pkts_last_epoch = 0;
  116. q->pkts_to_release = 0;
  117. q->unplug_indefinite = false;
  118. if (opt == NULL) {
  119. q->limit = qdisc_dev(sch)->tx_queue_len
  120. * psched_mtu(qdisc_dev(sch));
  121. } else {
  122. struct tc_plug_qopt *ctl = nla_data(opt);
  123. if (nla_len(opt) < sizeof(*ctl))
  124. return -EINVAL;
  125. q->limit = ctl->limit;
  126. }
  127. q->throttled = true;
  128. return 0;
  129. }
  130. /* Receives 4 types of messages:
  131. * TCQ_PLUG_BUFFER: Inset a plug into the queue and
  132. * buffer any incoming packets
  133. * TCQ_PLUG_RELEASE_ONE: Dequeue packets from queue head
  134. * to beginning of the next plug.
  135. * TCQ_PLUG_RELEASE_INDEFINITE: Dequeue all packets from queue.
  136. * Stop buffering packets until the next TCQ_PLUG_BUFFER
  137. * command is received (just act as a pass-thru queue).
  138. * TCQ_PLUG_LIMIT: Increase/decrease queue size
  139. */
  140. static int plug_change(struct Qdisc *sch, struct nlattr *opt,
  141. struct netlink_ext_ack *extack)
  142. {
  143. struct plug_sched_data *q = qdisc_priv(sch);
  144. struct tc_plug_qopt *msg;
  145. if (opt == NULL)
  146. return -EINVAL;
  147. msg = nla_data(opt);
  148. if (nla_len(opt) < sizeof(*msg))
  149. return -EINVAL;
  150. switch (msg->action) {
  151. case TCQ_PLUG_BUFFER:
  152. /* Save size of the current buffer */
  153. q->pkts_last_epoch = q->pkts_current_epoch;
  154. q->pkts_current_epoch = 0;
  155. if (q->unplug_indefinite)
  156. q->throttled = true;
  157. q->unplug_indefinite = false;
  158. break;
  159. case TCQ_PLUG_RELEASE_ONE:
  160. /* Add packets from the last complete buffer to the
  161. * packets to be released set.
  162. */
  163. q->pkts_to_release += q->pkts_last_epoch;
  164. q->pkts_last_epoch = 0;
  165. q->throttled = false;
  166. netif_schedule_queue(sch->dev_queue);
  167. break;
  168. case TCQ_PLUG_RELEASE_INDEFINITE:
  169. q->unplug_indefinite = true;
  170. q->pkts_to_release = 0;
  171. q->pkts_last_epoch = 0;
  172. q->pkts_current_epoch = 0;
  173. q->throttled = false;
  174. netif_schedule_queue(sch->dev_queue);
  175. break;
  176. case TCQ_PLUG_LIMIT:
  177. /* Limit is supplied in bytes */
  178. q->limit = msg->limit;
  179. break;
  180. default:
  181. return -EINVAL;
  182. }
  183. return 0;
  184. }
  185. static struct Qdisc_ops plug_qdisc_ops __read_mostly = {
  186. .id = "plug",
  187. .priv_size = sizeof(struct plug_sched_data),
  188. .enqueue = plug_enqueue,
  189. .dequeue = plug_dequeue,
  190. .peek = qdisc_peek_head,
  191. .init = plug_init,
  192. .change = plug_change,
  193. .reset = qdisc_reset_queue,
  194. .owner = THIS_MODULE,
  195. };
  196. static int __init plug_module_init(void)
  197. {
  198. return register_qdisc(&plug_qdisc_ops);
  199. }
  200. static void __exit plug_module_exit(void)
  201. {
  202. unregister_qdisc(&plug_qdisc_ops);
  203. }
  204. module_init(plug_module_init)
  205. module_exit(plug_module_exit)
  206. MODULE_LICENSE("GPL");