sch_plug.c 6.4 KB

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