xfrm_device.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. .. SPDX-License-Identifier: GPL-2.0
  2. .. _xfrm_device:
  3. ===============================================
  4. XFRM device - offloading the IPsec computations
  5. ===============================================
  6. Shannon Nelson <shannon.nelson@oracle.com>
  7. Leon Romanovsky <leonro@nvidia.com>
  8. Overview
  9. ========
  10. IPsec is a useful feature for securing network traffic, but the
  11. computational cost is high: a 10Gbps link can easily be brought down
  12. to under 1Gbps, depending on the traffic and link configuration.
  13. Luckily, there are NICs that offer a hardware based IPsec offload which
  14. can radically increase throughput and decrease CPU utilization. The XFRM
  15. Device interface allows NIC drivers to offer to the stack access to the
  16. hardware offload.
  17. Right now, there are two types of hardware offload that kernel supports.
  18. * IPsec crypto offload:
  19. * NIC performs encrypt/decrypt
  20. * Kernel does everything else
  21. * IPsec packet offload:
  22. * NIC performs encrypt/decrypt
  23. * NIC does encapsulation
  24. * Kernel and NIC have SA and policy in-sync
  25. * NIC handles the SA and policies states
  26. * The Kernel talks to the keymanager
  27. Userland access to the offload is typically through a system such as
  28. libreswan or KAME/raccoon, but the iproute2 'ip xfrm' command set can
  29. be handy when experimenting. An example command might look something
  30. like this for crypto offload:
  31. ip x s add proto esp dst 14.0.0.70 src 14.0.0.52 spi 0x07 mode transport \
  32. reqid 0x07 replay-window 32 \
  33. aead 'rfc4106(gcm(aes))' 0x44434241343332312423222114131211f4f3f2f1 128 \
  34. sel src 14.0.0.52/24 dst 14.0.0.70/24 proto tcp \
  35. offload dev eth4 dir in
  36. and for packet offload
  37. ip x s add proto esp dst 14.0.0.70 src 14.0.0.52 spi 0x07 mode transport \
  38. reqid 0x07 replay-window 32 \
  39. aead 'rfc4106(gcm(aes))' 0x44434241343332312423222114131211f4f3f2f1 128 \
  40. sel src 14.0.0.52/24 dst 14.0.0.70/24 proto tcp \
  41. offload packet dev eth4 dir in
  42. ip x p add src 14.0.0.70 dst 14.0.0.52 offload packet dev eth4 dir in
  43. tmpl src 14.0.0.70 dst 14.0.0.52 proto esp reqid 10000 mode transport
  44. Yes, that's ugly, but that's what shell scripts and/or libreswan are for.
  45. Callbacks to implement
  46. ======================
  47. ::
  48. /* from include/linux/netdevice.h */
  49. struct xfrmdev_ops {
  50. /* Crypto and Packet offload callbacks */
  51. int (*xdo_dev_state_add) (struct xfrm_state *x, struct netlink_ext_ack *extack);
  52. void (*xdo_dev_state_delete) (struct xfrm_state *x);
  53. void (*xdo_dev_state_free) (struct xfrm_state *x);
  54. bool (*xdo_dev_offload_ok) (struct sk_buff *skb,
  55. struct xfrm_state *x);
  56. void (*xdo_dev_state_advance_esn) (struct xfrm_state *x);
  57. void (*xdo_dev_state_update_stats) (struct xfrm_state *x);
  58. /* Solely packet offload callbacks */
  59. int (*xdo_dev_policy_add) (struct xfrm_policy *x, struct netlink_ext_ack *extack);
  60. void (*xdo_dev_policy_delete) (struct xfrm_policy *x);
  61. void (*xdo_dev_policy_free) (struct xfrm_policy *x);
  62. };
  63. The NIC driver offering ipsec offload will need to implement callbacks
  64. relevant to supported offload to make the offload available to the network
  65. stack's XFRM subsystem. Additionally, the feature bits NETIF_F_HW_ESP and
  66. NETIF_F_HW_ESP_TX_CSUM will signal the availability of the offload.
  67. Flow
  68. ====
  69. At probe time and before the call to register_netdev(), the driver should
  70. set up local data structures and XFRM callbacks, and set the feature bits.
  71. The XFRM code's listener will finish the setup on NETDEV_REGISTER.
  72. ::
  73. adapter->netdev->xfrmdev_ops = &ixgbe_xfrmdev_ops;
  74. adapter->netdev->features |= NETIF_F_HW_ESP;
  75. adapter->netdev->hw_enc_features |= NETIF_F_HW_ESP;
  76. When new SAs are set up with a request for "offload" feature, the
  77. driver's xdo_dev_state_add() will be given the new SA to be offloaded
  78. and an indication of whether it is for Rx or Tx. The driver should
  79. - verify the algorithm is supported for offloads
  80. - store the SA information (key, salt, target-ip, protocol, etc)
  81. - enable the HW offload of the SA
  82. - return status value:
  83. =========== ===================================
  84. 0 success
  85. -EOPNETSUPP offload not supported, try SW IPsec,
  86. not applicable for packet offload mode
  87. other fail the request
  88. =========== ===================================
  89. The driver can also set an offload_handle in the SA, an opaque void pointer
  90. that can be used to convey context into the fast-path offload requests::
  91. xs->xso.offload_handle = context;
  92. When the network stack is preparing an IPsec packet for an SA that has
  93. been setup for offload, it first calls into xdo_dev_offload_ok() with
  94. the skb and the intended offload state to ask the driver if the offload
  95. will serviceable. This can check the packet information to be sure the
  96. offload can be supported (e.g. IPv4 or IPv6, no IPv4 options, etc) and
  97. return true of false to signify its support.
  98. Crypto offload mode:
  99. When ready to send, the driver needs to inspect the Tx packet for the
  100. offload information, including the opaque context, and set up the packet
  101. send accordingly::
  102. xs = xfrm_input_state(skb);
  103. context = xs->xso.offload_handle;
  104. set up HW for send
  105. The stack has already inserted the appropriate IPsec headers in the
  106. packet data, the offload just needs to do the encryption and fix up the
  107. header values.
  108. When a packet is received and the HW has indicated that it offloaded a
  109. decryption, the driver needs to add a reference to the decoded SA into
  110. the packet's skb. At this point the data should be decrypted but the
  111. IPsec headers are still in the packet data; they are removed later up
  112. the stack in xfrm_input().
  113. find and hold the SA that was used to the Rx skb::
  114. get spi, protocol, and destination IP from packet headers
  115. xs = find xs from (spi, protocol, dest_IP)
  116. xfrm_state_hold(xs);
  117. store the state information into the skb::
  118. sp = secpath_set(skb);
  119. if (!sp) return;
  120. sp->xvec[sp->len++] = xs;
  121. sp->olen++;
  122. indicate the success and/or error status of the offload::
  123. xo = xfrm_offload(skb);
  124. xo->flags = CRYPTO_DONE;
  125. xo->status = crypto_status;
  126. hand the packet to napi_gro_receive() as usual
  127. In ESN mode, xdo_dev_state_advance_esn() is called from xfrm_replay_advance_esn().
  128. Driver will check packet seq number and update HW ESN state machine if needed.
  129. Packet offload mode:
  130. HW adds and deletes XFRM headers. So in RX path, XFRM stack is bypassed if HW
  131. reported success. In TX path, the packet lefts kernel without extra header
  132. and not encrypted, the HW is responsible to perform it.
  133. When the SA is removed by the user, the driver's xdo_dev_state_delete()
  134. and xdo_dev_policy_delete() are asked to disable the offload. Later,
  135. xdo_dev_state_free() and xdo_dev_policy_free() are called from a garbage
  136. collection routine after all reference counts to the state and policy
  137. have been removed and any remaining resources can be cleared for the
  138. offload state. How these are used by the driver will depend on specific
  139. hardware needs.
  140. As a netdev is set to DOWN the XFRM stack's netdev listener will call
  141. xdo_dev_state_delete(), xdo_dev_policy_delete(), xdo_dev_state_free() and
  142. xdo_dev_policy_free() on any remaining offloaded states.
  143. Outcome of HW handling packets, the XFRM core can't count hard, soft limits.
  144. The HW/driver are responsible to perform it and provide accurate data when
  145. xdo_dev_state_update_stats() is called. In case of one of these limits
  146. occuried, the driver needs to call to xfrm_state_check_expire() to make sure
  147. that XFRM performs rekeying sequence.