checksum-offloads.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Checksum Offloads in the Linux Networking Stack
  2. Introduction
  3. ============
  4. This document describes a set of techniques in the Linux networking stack
  5. to take advantage of checksum offload capabilities of various NICs.
  6. The following technologies are described:
  7. * TX Checksum Offload
  8. * LCO: Local Checksum Offload
  9. * RCO: Remote Checksum Offload
  10. Things that should be documented here but aren't yet:
  11. * RX Checksum Offload
  12. * CHECKSUM_UNNECESSARY conversion
  13. TX Checksum Offload
  14. ===================
  15. The interface for offloading a transmit checksum to a device is explained
  16. in detail in comments near the top of include/linux/skbuff.h.
  17. In brief, it allows to request the device fill in a single ones-complement
  18. checksum defined by the sk_buff fields skb->csum_start and
  19. skb->csum_offset. The device should compute the 16-bit ones-complement
  20. checksum (i.e. the 'IP-style' checksum) from csum_start to the end of the
  21. packet, and fill in the result at (csum_start + csum_offset).
  22. Because csum_offset cannot be negative, this ensures that the previous
  23. value of the checksum field is included in the checksum computation, thus
  24. it can be used to supply any needed corrections to the checksum (such as
  25. the sum of the pseudo-header for UDP or TCP).
  26. This interface only allows a single checksum to be offloaded. Where
  27. encapsulation is used, the packet may have multiple checksum fields in
  28. different header layers, and the rest will have to be handled by another
  29. mechanism such as LCO or RCO.
  30. No offloading of the IP header checksum is performed; it is always done in
  31. software. This is OK because when we build the IP header, we obviously
  32. have it in cache, so summing it isn't expensive. It's also rather short.
  33. The requirements for GSO are more complicated, because when segmenting an
  34. encapsulated packet both the inner and outer checksums may need to be
  35. edited or recomputed for each resulting segment. See the skbuff.h comment
  36. (section 'E') for more details.
  37. A driver declares its offload capabilities in netdev->hw_features; see
  38. Documentation/networking/netdev-features for more. Note that a device
  39. which only advertises NETIF_F_IP[V6]_CSUM must still obey the csum_start
  40. and csum_offset given in the SKB; if it tries to deduce these itself in
  41. hardware (as some NICs do) the driver should check that the values in the
  42. SKB match those which the hardware will deduce, and if not, fall back to
  43. checksumming in software instead (with skb_checksum_help or one of the
  44. skb_csum_off_chk* functions as mentioned in include/linux/skbuff.h). This
  45. is a pain, but that's what you get when hardware tries to be clever.
  46. The stack should, for the most part, assume that checksum offload is
  47. supported by the underlying device. The only place that should check is
  48. validate_xmit_skb(), and the functions it calls directly or indirectly.
  49. That function compares the offload features requested by the SKB (which
  50. may include other offloads besides TX Checksum Offload) and, if they are
  51. not supported or enabled on the device (determined by netdev->features),
  52. performs the corresponding offload in software. In the case of TX
  53. Checksum Offload, that means calling skb_checksum_help(skb).
  54. LCO: Local Checksum Offload
  55. ===========================
  56. LCO is a technique for efficiently computing the outer checksum of an
  57. encapsulated datagram when the inner checksum is due to be offloaded.
  58. The ones-complement sum of a correctly checksummed TCP or UDP packet is
  59. equal to the complement of the sum of the pseudo header, because everything
  60. else gets 'cancelled out' by the checksum field. This is because the sum was
  61. complemented before being written to the checksum field.
  62. More generally, this holds in any case where the 'IP-style' ones complement
  63. checksum is used, and thus any checksum that TX Checksum Offload supports.
  64. That is, if we have set up TX Checksum Offload with a start/offset pair, we
  65. know that after the device has filled in that checksum, the ones
  66. complement sum from csum_start to the end of the packet will be equal to
  67. the complement of whatever value we put in the checksum field beforehand.
  68. This allows us to compute the outer checksum without looking at the payload:
  69. we simply stop summing when we get to csum_start, then add the complement of
  70. the 16-bit word at (csum_start + csum_offset).
  71. Then, when the true inner checksum is filled in (either by hardware or by
  72. skb_checksum_help()), the outer checksum will become correct by virtue of
  73. the arithmetic.
  74. LCO is performed by the stack when constructing an outer UDP header for an
  75. encapsulation such as VXLAN or GENEVE, in udp_set_csum(). Similarly for
  76. the IPv6 equivalents, in udp6_set_csum().
  77. It is also performed when constructing an IPv4 GRE header, in
  78. net/ipv4/ip_gre.c:build_header(). It is *not* currently performed when
  79. constructing an IPv6 GRE header; the GRE checksum is computed over the
  80. whole packet in net/ipv6/ip6_gre.c:ip6gre_xmit2(), but it should be
  81. possible to use LCO here as IPv6 GRE still uses an IP-style checksum.
  82. All of the LCO implementations use a helper function lco_csum(), in
  83. include/linux/skbuff.h.
  84. LCO can safely be used for nested encapsulations; in this case, the outer
  85. encapsulation layer will sum over both its own header and the 'middle'
  86. header. This does mean that the 'middle' header will get summed multiple
  87. times, but there doesn't seem to be a way to avoid that without incurring
  88. bigger costs (e.g. in SKB bloat).
  89. RCO: Remote Checksum Offload
  90. ============================
  91. RCO is a technique for eliding the inner checksum of an encapsulated
  92. datagram, allowing the outer checksum to be offloaded. It does, however,
  93. involve a change to the encapsulation protocols, which the receiver must
  94. also support. For this reason, it is disabled by default.
  95. RCO is detailed in the following Internet-Drafts:
  96. https://tools.ietf.org/html/draft-herbert-remotecsumoffload-00
  97. https://tools.ietf.org/html/draft-herbert-vxlan-rco-00
  98. In Linux, RCO is implemented individually in each encapsulation protocol,
  99. and most tunnel types have flags controlling its use. For instance, VXLAN
  100. has the flag VXLAN_F_REMCSUM_TX (per struct vxlan_rdst) to indicate that
  101. RCO should be used when transmitting to a given remote destination.