net_dim.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ======================================================
  2. Net DIM - Generic Network Dynamic Interrupt Moderation
  3. ======================================================
  4. :Author: Tal Gilboa <talgi@mellanox.com>
  5. .. contents:: :depth: 2
  6. Assumptions
  7. ===========
  8. This document assumes the reader has basic knowledge in network drivers
  9. and in general interrupt moderation.
  10. Introduction
  11. ============
  12. Dynamic Interrupt Moderation (DIM) (in networking) refers to changing the
  13. interrupt moderation configuration of a channel in order to optimize packet
  14. processing. The mechanism includes an algorithm which decides if and how to
  15. change moderation parameters for a channel, usually by performing an analysis on
  16. runtime data sampled from the system. Net DIM is such a mechanism. In each
  17. iteration of the algorithm, it analyses a given sample of the data, compares it
  18. to the previous sample and if required, it can decide to change some of the
  19. interrupt moderation configuration fields. The data sample is composed of data
  20. bandwidth, the number of packets and the number of events. The time between
  21. samples is also measured. Net DIM compares the current and the previous data and
  22. returns an adjusted interrupt moderation configuration object. In some cases,
  23. the algorithm might decide not to change anything. The configuration fields are
  24. the minimum duration (microseconds) allowed between events and the maximum
  25. number of wanted packets per event. The Net DIM algorithm ascribes importance to
  26. increase bandwidth over reducing interrupt rate.
  27. Net DIM Algorithm
  28. =================
  29. Each iteration of the Net DIM algorithm follows these steps:
  30. #. Calculates new data sample.
  31. #. Compares it to previous sample.
  32. #. Makes a decision - suggests interrupt moderation configuration fields.
  33. #. Applies a schedule work function, which applies suggested configuration.
  34. The first two steps are straightforward, both the new and the previous data are
  35. supplied by the driver registered to Net DIM. The previous data is the new data
  36. supplied to the previous iteration. The comparison step checks the difference
  37. between the new and previous data and decides on the result of the last step.
  38. A step would result as "better" if bandwidth increases and as "worse" if
  39. bandwidth reduces. If there is no change in bandwidth, the packet rate is
  40. compared in a similar fashion - increase == "better" and decrease == "worse".
  41. In case there is no change in the packet rate as well, the interrupt rate is
  42. compared. Here the algorithm tries to optimize for lower interrupt rate so an
  43. increase in the interrupt rate is considered "worse" and a decrease is
  44. considered "better". Step #2 has an optimization for avoiding false results: it
  45. only considers a difference between samples as valid if it is greater than a
  46. certain percentage. Also, since Net DIM does not measure anything by itself, it
  47. assumes the data provided by the driver is valid.
  48. Step #3 decides on the suggested configuration based on the result from step #2
  49. and the internal state of the algorithm. The states reflect the "direction" of
  50. the algorithm: is it going left (reducing moderation), right (increasing
  51. moderation) or standing still. Another optimization is that if a decision
  52. to stay still is made multiple times, the interval between iterations of the
  53. algorithm would increase in order to reduce calculation overhead. Also, after
  54. "parking" on one of the most left or most right decisions, the algorithm may
  55. decide to verify this decision by taking a step in the other direction. This is
  56. done in order to avoid getting stuck in a "deep sleep" scenario. Once a
  57. decision is made, an interrupt moderation configuration is selected from
  58. the predefined profiles.
  59. The last step is to notify the registered driver that it should apply the
  60. suggested configuration. This is done by scheduling a work function, defined by
  61. the Net DIM API and provided by the registered driver.
  62. As you can see, Net DIM itself does not actively interact with the system. It
  63. would have trouble making the correct decisions if the wrong data is supplied to
  64. it and it would be useless if the work function would not apply the suggested
  65. configuration. This does, however, allow the registered driver some room for
  66. manoeuvre as it may provide partial data or ignore the algorithm suggestion
  67. under some conditions.
  68. Registering a Network Device to DIM
  69. ===================================
  70. Net DIM API exposes the main function net_dim().
  71. This function is the entry point to the Net
  72. DIM algorithm and has to be called every time the driver would like to check if
  73. it should change interrupt moderation parameters. The driver should provide two
  74. data structures: :c:type:`struct dim <dim>` and
  75. :c:type:`struct dim_sample <dim_sample>`. :c:type:`struct dim <dim>`
  76. describes the state of DIM for a specific object (RX queue, TX queue,
  77. other queues, etc.). This includes the current selected profile, previous data
  78. samples, the callback function provided by the driver and more.
  79. :c:type:`struct dim_sample <dim_sample>` describes a data sample,
  80. which will be compared to the data sample stored in :c:type:`struct dim <dim>`
  81. in order to decide on the algorithm's next
  82. step. The sample should include bytes, packets and interrupts, measured by
  83. the driver.
  84. In order to use Net DIM from a networking driver, the driver needs to call the
  85. main net_dim() function. The recommended method is to call net_dim() on each
  86. interrupt. Since Net DIM has a built-in moderation and it might decide to skip
  87. iterations under certain conditions, there is no need to moderate the net_dim()
  88. calls as well. As mentioned above, the driver needs to provide an object of type
  89. :c:type:`struct dim <dim>` to the net_dim() function call. It is advised for
  90. each entity using Net DIM to hold a :c:type:`struct dim <dim>` as part of its
  91. data structure and use it as the main Net DIM API object.
  92. The :c:type:`struct dim_sample <dim_sample>` should hold the latest
  93. bytes, packets and interrupts count. No need to perform any calculations, just
  94. include the raw data.
  95. The net_dim() call itself does not return anything. Instead Net DIM relies on
  96. the driver to provide a callback function, which is called when the algorithm
  97. decides to make a change in the interrupt moderation parameters. This callback
  98. will be scheduled and run in a separate thread in order not to add overhead to
  99. the data flow. After the work is done, Net DIM algorithm needs to be set to
  100. the proper state in order to move to the next iteration.
  101. Example
  102. =======
  103. The following code demonstrates how to register a driver to Net DIM. The actual
  104. usage is not complete but it should make the outline of the usage clear.
  105. .. code-block:: c
  106. #include <linux/dim.h>
  107. /* Callback for net DIM to schedule on a decision to change moderation */
  108. void my_driver_do_dim_work(struct work_struct *work)
  109. {
  110. /* Get struct dim from struct work_struct */
  111. struct dim *dim = container_of(work, struct dim,
  112. work);
  113. /* Do interrupt moderation related stuff */
  114. ...
  115. /* Signal net DIM work is done and it should move to next iteration */
  116. dim->state = DIM_START_MEASURE;
  117. }
  118. /* My driver's interrupt handler */
  119. int my_driver_handle_interrupt(struct my_driver_entity *my_entity, ...)
  120. {
  121. ...
  122. /* A struct to hold current measured data */
  123. struct dim_sample dim_sample;
  124. ...
  125. /* Initiate data sample struct with current data */
  126. dim_update_sample(my_entity->events,
  127. my_entity->packets,
  128. my_entity->bytes,
  129. &dim_sample);
  130. /* Call net DIM */
  131. net_dim(&my_entity->dim, dim_sample);
  132. ...
  133. }
  134. /* My entity's initialization function (my_entity was already allocated) */
  135. int my_driver_init_my_entity(struct my_driver_entity *my_entity, ...)
  136. {
  137. ...
  138. /* Initiate struct work_struct with my driver's callback function */
  139. INIT_WORK(&my_entity->dim.work, my_driver_do_dim_work);
  140. ...
  141. }
  142. Dynamic Interrupt Moderation (DIM) library API
  143. ==============================================
  144. .. kernel-doc:: include/linux/dim.h
  145. :internal: