xref: /dpdk/doc/guides/prog_guide/rte_security.rst (revision 1a08c379b9b5edeb0214378daa3fb2e56fba49ba)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright 2017 NXP
3
4
5
6Security Library
7================
8
9The security library provides a framework for management and provisioning
10of security protocol operations offloaded to hardware based devices. The
11library defines generic APIs to create and free security sessions which can
12support full protocol offload as well as inline crypto operation with
13NIC or crypto devices. The framework currently only supports the IPSec protocol
14and associated operations, other protocols will be added in future.
15
16Design Principles
17-----------------
18
19The security library provides an additional offload capability to an existing
20crypto device and/or ethernet device.
21
22.. code-block:: console
23
24               +---------------+
25               | rte_security  |
26               +---------------+
27                 \            /
28        +-----------+    +--------------+
29        |  NIC PMD  |    |  CRYPTO PMD  |
30        +-----------+    +--------------+
31
32.. note::
33
34    Currently, the security library does not support the case of multi-process.
35    It will be updated in the future releases.
36
37The supported offload types are explained in the sections below.
38
39Inline Crypto
40~~~~~~~~~~~~~
41
42RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
43The crypto processing for security protocol (e.g. IPSec) is processed
44inline during receive and transmission on NIC port. The flow based
45security action should be configured on the port.
46
47Ingress Data path - The packet is decrypted in RX path and relevant
48crypto status is set in Rx descriptors. After the successful inline
49crypto processing the packet is presented to host as a regular Rx packet
50however all security protocol related headers are still attached to the
51packet. e.g. In case of IPSec, the IPSec tunnel headers (if any),
52ESP/AH headers will remain in the packet but the received packet
53contains the decrypted data where the encrypted data was when the packet
54arrived. The driver Rx path check the descriptors and and based on the
55crypto status sets additional flags in the rte_mbuf.ol_flags field.
56
57.. note::
58
59    The underlying device may not support crypto processing for all ingress packet
60    matching to a particular flow (e.g. fragmented packets), such packets will
61    be passed as encrypted packets. It is the responsibility of application to
62    process such encrypted packets using other crypto driver instance.
63
64Egress Data path - The software prepares the egress packet by adding
65relevant security protocol headers. Only the data will not be
66encrypted by the software. The driver will accordingly configure the
67tx descriptors. The hardware device will encrypt the data before sending the
68the packet out.
69
70.. note::
71
72    The underlying device may support post encryption TSO.
73
74.. code-block:: console
75
76          Egress Data Path
77                 |
78        +--------|--------+
79        |  egress IPsec   |
80        |        |        |
81        | +------V------+ |
82        | | SADB lookup | |
83        | +------|------+ |
84        | +------V------+ |
85        | |   Tunnel    | |   <------ Add tunnel header to packet
86        | +------|------+ |
87        | +------V------+ |
88        | |     ESP     | |   <------ Add ESP header without trailer to packet
89        | |             | |   <------ Mark packet to be offloaded, add trailer
90        | +------|------+ |            meta-data to mbuf
91        +--------V--------+
92                 |
93        +--------V--------+
94        |    L2 Stack     |
95        +--------|--------+
96                 |
97        +--------V--------+
98        |                 |
99        |     NIC PMD     |   <------ Set hw context for inline crypto offload
100        |                 |
101        +--------|--------+
102                 |
103        +--------|--------+
104        |  HW ACCELERATED |   <------ Packet Encryption and
105        |        NIC      |           Authentication happens inline
106        |                 |
107        +-----------------+
108
109
110Inline protocol offload
111~~~~~~~~~~~~~~~~~~~~~~~
112
113RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
114The crypto and protocol processing for security protocol (e.g. IPSec)
115is processed inline during receive and transmission.  The flow based
116security action should be configured on the port.
117
118Ingress Data path - The packet is decrypted in the RX path and relevant
119crypto status is set in the Rx descriptors. After the successful inline
120crypto processing the packet is presented to the host as a regular Rx packet
121but all security protocol related headers are optionally removed from the
122packet. e.g. in the case of IPSec, the IPSec tunnel headers (if any),
123ESP/AH headers will be removed from the packet and the received packet
124will contains the decrypted packet only. The driver Rx path checks the
125descriptors and based on the crypto status sets additional flags in
126``rte_mbuf.ol_flags`` field. The driver would also set device-specific
127metadata in ``rte_mbuf.udata64`` field. This will allow the application
128to identify the security processing done on the packet.
129
130.. note::
131
132    The underlying device in this case is stateful. It is expected that
133    the device shall support crypto processing for all kind of packets matching
134    to a given flow, this includes fragmented packets (post reassembly).
135    E.g. in case of IPSec the device may internally manage anti-replay etc.
136    It will provide a configuration option for anti-replay behavior i.e. to drop
137    the packets or pass them to driver with error flags set in the descriptor.
138
139Egress Data path - The software will send the plain packet without any
140security protocol headers added to the packet. The driver will configure
141the security index and other requirement in tx descriptors.
142The hardware device will do security processing on the packet that includes
143adding the relevant protocol headers and encrypting the data before sending
144the packet out. The software should make sure that the buffer
145has required head room and tail room for any protocol header addition. The
146software may also do early fragmentation if the resultant packet is expected
147to cross the MTU size.
148
149
150.. note::
151
152    The underlying device will manage state information required for egress
153    processing. E.g. in case of IPSec, the seq number will be added to the
154    packet, however the device shall provide indication when the sequence number
155    is about to overflow. The underlying device may support post encryption TSO.
156
157.. code-block:: console
158
159         Egress Data Path
160                 |
161        +--------|--------+
162        |  egress IPsec   |
163        |        |        |
164        | +------V------+ |
165        | | SADB lookup | |
166        | +------|------+ |
167        | +------V------+ |
168        | |   Desc      | |   <------ Mark packet to be offloaded
169        | +------|------+ |
170        +--------V--------+
171                 |
172        +--------V--------+
173        |    L2 Stack     |
174        +--------|--------+
175                 |
176        +--------V--------+
177        |                 |
178        |     NIC PMD     |   <------ Set hw context for inline crypto offload
179        |                 |
180        +--------|--------+
181                 |
182        +--------|--------+
183        |  HW ACCELERATED |   <------ Add tunnel, ESP header etc header to
184        |        NIC      |           packet. Packet Encryption and
185        |                 |           Authentication happens inline.
186        +-----------------+
187
188
189Lookaside protocol offload
190~~~~~~~~~~~~~~~~~~~~~~~~~~
191
192RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
193This extends librte_cryptodev to support the programming of IPsec
194Security Association (SA) as part of a crypto session creation including
195the definition. In addition to standard crypto processing, as defined by
196the cryptodev, the security protocol processing is also offloaded to the
197crypto device.
198
199Decryption: The packet is sent to the crypto device for security
200protocol processing. The device will decrypt the packet and it will also
201optionally remove additional security headers from the packet.
202E.g. in case of IPSec, IPSec tunnel headers (if any), ESP/AH headers
203will be removed from the packet and the decrypted packet may contain
204plain data only.
205
206.. note::
207
208    In case of IPSec the device may internally manage anti-replay etc.
209    It will provide a configuration option for anti-replay behavior i.e. to drop
210    the packets or pass them to driver with error flags set in descriptor.
211
212Encryption: The software will submit the packet to cryptodev as usual
213for encryption, the hardware device in this case will also add the relevant
214security protocol header along with encrypting the packet. The software
215should make sure that the buffer has required head room and tail room
216for any protocol header addition.
217
218.. note::
219
220    In the case of IPSec, the seq number will be added to the packet,
221    It shall provide an indication when the sequence number is about to
222    overflow.
223
224.. code-block:: console
225
226          Egress Data Path
227                 |
228        +--------|--------+
229        |  egress IPsec   |
230        |        |        |
231        | +------V------+ |
232        | | SADB lookup | |   <------ SA maps to cryptodev session
233        | +------|------+ |
234        | +------|------+ |
235        | |      \--------------------\
236        | |    Crypto   | |           |  <- Crypto processing through
237        | |      /----------------\   |     inline crypto PMD
238        | +------|------+ |       |   |
239        +--------V--------+       |   |
240                 |                |   |
241        +--------V--------+       |   |  create   <-- SA is added to hw
242        |    L2 Stack     |       |   |  inline       using existing create
243        +--------|--------+       |   |  session      sym session APIs
244                 |                |   |    |
245        +--------V--------+   +---|---|----V---+
246        |                 |   |   \---/    |   | <--- Add tunnel, ESP header etc
247        |     NIC PMD     |   |   INLINE   |   |      header to packet.Packet
248        |                 |   | CRYPTO PMD |   |      Encryption/Decryption and
249        +--------|--------+   +----------------+      Authentication happens
250                 |                                    inline.
251        +--------|--------+
252        |       NIC       |
253        +--------|--------+
254                 V
255
256Device Features and Capabilities
257---------------------------------
258
259Device Capabilities For Security Operations
260~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
261
262The device (crypto or ethernet) capabilities which support security operations,
263are defined by the security action type, security protocol, protocol
264capabilities and corresponding crypto capabilities for security. For the full
265scope of the Security capability see definition of rte_security_capability
266structure in the *DPDK API Reference*.
267
268.. code-block:: c
269
270   struct rte_security_capability;
271
272Each driver (crypto or ethernet) defines its own private array of capabilities
273for the operations it supports. Below is an example of the capabilities for a
274PMD which supports the IPSec protocol.
275
276.. code-block:: c
277
278    static const struct rte_security_capability pmd_security_capabilities[] = {
279        { /* IPsec Lookaside Protocol offload ESP Tunnel Egress */
280                .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
281                .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
282                .ipsec = {
283                        .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
284                        .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
285                        .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
286                        .options = { 0 }
287                },
288                .crypto_capabilities = pmd_capabilities
289        },
290        { /* IPsec Lookaside Protocol offload ESP Tunnel Ingress */
291                .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
292                .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
293                .ipsec = {
294                        .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
295                        .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
296                        .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
297                        .options = { 0 }
298                },
299                .crypto_capabilities = pmd_capabilities
300        },
301        {
302                .action = RTE_SECURITY_ACTION_TYPE_NONE
303        }
304    };
305    static const struct rte_cryptodev_capabilities pmd_capabilities[] = {
306        {    /* SHA1 HMAC */
307            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
308            .sym = {
309                .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
310                .auth = {
311                    .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
312                    .block_size = 64,
313                    .key_size = {
314                        .min = 64,
315                        .max = 64,
316                        .increment = 0
317                    },
318                    .digest_size = {
319                        .min = 12,
320                        .max = 12,
321                        .increment = 0
322                    },
323                    .aad_size = { 0 },
324                    .iv_size = { 0 }
325                }
326            }
327        },
328        {    /* AES CBC */
329            .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
330            .sym = {
331                .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
332                .cipher = {
333                    .algo = RTE_CRYPTO_CIPHER_AES_CBC,
334                    .block_size = 16,
335                    .key_size = {
336                        .min = 16,
337                        .max = 32,
338                        .increment = 8
339                    },
340                    .iv_size = {
341                        .min = 16,
342                        .max = 16,
343                        .increment = 0
344                    }
345                }
346            }
347        }
348    }
349
350
351Capabilities Discovery
352~~~~~~~~~~~~~~~~~~~~~~
353
354Discovering the features and capabilities of a driver (crypto/ethernet)
355is achieved through the ``rte_security_capabilities_get()`` function.
356
357.. code-block:: c
358
359   const struct rte_security_capability *rte_security_capabilities_get(uint16_t id);
360
361This allows the user to query a specific driver and get all device
362security capabilities. It returns an array of ``rte_security_capability`` structures
363which contains all the capabilities for that device.
364
365Security Session Create/Free
366~~~~~~~~~~~~~~~~~~~~~~~~~~~~
367
368Security Sessions are created to store the immutable fields of a particular Security
369Association for a particular protocol which is defined by a security session
370configuration structure which is used in the operation processing of a packet flow.
371Sessions are used to manage protocol specific information as well as crypto parameters.
372Security sessions cache this immutable data in a optimal way for the underlying PMD
373and this allows further acceleration of the offload of Crypto workloads.
374
375The Security framework provides APIs to create and free sessions for crypto/ethernet
376devices, where sessions are mempool objects. It is the application's responsibility
377to create and manage the session mempools. The mempool object size should be able to
378accommodate the driver's private data of security session.
379
380Once the session mempools have been created, ``rte_security_session_create()``
381is used to allocate and initialize a session for the required crypto/ethernet device.
382
383Session APIs need a parameter ``rte_security_ctx`` to identify the crypto/ethernet
384security ops. This parameter can be retrieved using the APIs
385``rte_cryptodev_get_sec_ctx()`` (for crypto device) or ``rte_eth_dev_get_sec_ctx``
386(for ethernet port).
387
388Sessions already created can be updated with ``rte_security_session_update()``.
389
390When a session is no longer used, the user must call ``rte_security_session_destroy()``
391to free the driver private session data and return the memory back to the mempool.
392
393For look aside protocol offload to hardware crypto device, the ``rte_crypto_op``
394created by the application is attached to the security session by the API
395``rte_security_attach_session()``.
396
397For Inline Crypto and Inline protocol offload, device specific defined metadata is
398updated in the mbuf using ``rte_security_set_pkt_metadata()`` if
399``DEV_TX_OFFLOAD_SEC_NEED_MDATA`` is set.
400
401For inline protocol offloaded ingress traffic, the application can register a
402pointer, ``userdata`` , in the security session. When the packet is received,
403``rte_security_get_userdata()`` would return the userdata registered for the
404security session which processed the packet.
405
406.. note::
407
408    In case of inline processed packets, ``rte_mbuf.udata64`` field would be
409    used by the driver to relay information on the security processing
410    associated with the packet. In ingress, the driver would set this in Rx
411    path while in egress, ``rte_security_set_pkt_metadata()`` would perform a
412    similar operation. The application is expected not to modify the field
413    when it has relevant info. For ingress, this device-specific 64 bit value
414    is required to derive other information (like userdata), required for
415    identifying the security processing done on the packet.
416
417Security session configuration
418~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
419
420Security Session configuration structure is defined as ``rte_security_session_conf``
421
422.. code-block:: c
423
424    struct rte_security_session_conf {
425        enum rte_security_session_action_type action_type;
426        /**< Type of action to be performed on the session */
427        enum rte_security_session_protocol protocol;
428        /**< Security protocol to be configured */
429        union {
430                struct rte_security_ipsec_xform ipsec;
431                struct rte_security_macsec_xform macsec;
432        };
433        /**< Configuration parameters for security session */
434        struct rte_crypto_sym_xform *crypto_xform;
435        /**< Security Session Crypto Transformations */
436        void *userdata;
437        /**< Application specific userdata to be saved with session */
438    };
439
440The configuration structure reuses the ``rte_crypto_sym_xform`` struct for crypto related
441configuration. The ``rte_security_session_action_type`` struct is used to specify whether the
442session is configured for Lookaside Protocol offload or Inline Crypto or Inline Protocol
443Offload.
444
445.. code-block:: c
446
447    enum rte_security_session_action_type {
448        RTE_SECURITY_ACTION_TYPE_NONE,
449        /**< No security actions */
450        RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
451        /**< Crypto processing for security protocol is processed inline
452         * during transmission */
453        RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
454        /**< All security protocol processing is performed inline during
455         * transmission */
456        RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL
457        /**< All security protocol processing including crypto is performed
458         * on a lookaside accelerator */
459    };
460
461The ``rte_security_session_protocol`` is defined as
462
463.. code-block:: c
464
465    enum rte_security_session_protocol {
466        RTE_SECURITY_PROTOCOL_IPSEC,
467        /**< IPsec Protocol */
468        RTE_SECURITY_PROTOCOL_MACSEC,
469        /**< MACSec Protocol */
470    };
471
472Currently the library defines configuration parameters for IPSec only. For other
473protocols like MACSec, structures and enums are defined as place holders which
474will be updated in the future.
475
476IPsec related configuration parameters are defined in ``rte_security_ipsec_xform``
477
478.. code-block:: c
479
480    struct rte_security_ipsec_xform {
481        uint32_t spi;
482        /**< SA security parameter index */
483        uint32_t salt;
484        /**< SA salt */
485        struct rte_security_ipsec_sa_options options;
486        /**< various SA options */
487        enum rte_security_ipsec_sa_direction direction;
488        /**< IPSec SA Direction - Egress/Ingress */
489        enum rte_security_ipsec_sa_protocol proto;
490        /**< IPsec SA Protocol - AH/ESP */
491        enum rte_security_ipsec_sa_mode mode;
492        /**< IPsec SA Mode - transport/tunnel */
493        struct rte_security_ipsec_tunnel_param tunnel;
494        /**< Tunnel parameters, NULL for transport mode */
495    };
496
497
498Security API
499~~~~~~~~~~~~
500
501The rte_security Library API is described in the *DPDK API Reference* document.
502
503Flow based Security Session
504~~~~~~~~~~~~~~~~~~~~~~~~~~~
505
506In the case of NIC based offloads, the security session specified in the
507'rte_flow_action_security' must be created on the same port as the
508flow action that is being specified.
509
510The ingress/egress flow attribute should match that specified in the security
511session if the security session supports the definition of the direction.
512
513Multiple flows can be configured to use the same security session. For
514example if the security session specifies an egress IPsec SA, then multiple
515flows can be specified to that SA. In the case of an ingress IPsec SA then
516it is only valid to have a single flow to map to that security session.
517
518.. code-block:: console
519
520         Configuration Path
521                 |
522        +--------|--------+
523        |    Add/Remove   |
524        |     IPsec SA    |   <------ Build security flow action of
525        |        |        |           ipsec transform
526        |--------|--------|
527                 |
528        +--------V--------+
529        |   Flow API      |
530        +--------|--------+
531                 |
532        +--------V--------+
533        |                 |
534        |     NIC PMD     |   <------ Add/Remove SA to/from hw context
535        |                 |
536        +--------|--------+
537                 |
538        +--------|--------+
539        |  HW ACCELERATED |
540        |        NIC      |
541        |                 |
542        +--------|--------+
543
544* Add/Delete SA flow:
545  To add a new inline SA construct a rte_flow_item for Ethernet + IP + ESP
546  using the SA selectors and the ``rte_crypto_ipsec_xform`` as the ``rte_flow_action``.
547  Note that any rte_flow_items may be empty, which means it is not checked.
548
549.. code-block:: console
550
551    In its most basic form, IPsec flow specification is as follows:
552        +-------+     +----------+    +--------+    +-----+
553        |  Eth  | ->  |   IP4/6  | -> |   ESP  | -> | END |
554        +-------+     +----------+    +--------+    +-----+
555
556    However, the API can represent, IPsec crypto offload with any encapsulation:
557        +-------+            +--------+    +-----+
558        |  Eth  | ->  ... -> |   ESP  | -> | END |
559        +-------+            +--------+    +-----+
560