14. L2 Forwarding with Crypto Sample Application

The L2 Forwarding with Crypto (l2fwd-crypto) sample application is a simple example of packet processing using the Data Plane Development Kit (DPDK), in conjunction with the Cryptodev library.

14.1. Overview

The L2 Forwarding with Crypto sample application performs a crypto operation (cipher/hash) specified by the user from command line (or using the default values), with a crypto device capable of doing that operation, for each packet that is received on an RX_PORT and performs L2 forwarding. The destination port is the adjacent port from the enabled portmask, that is, if the first four ports are enabled (portmask 0xf), ports 0 and 1 forward into each other, and ports 2 and 3 forward into each other. Also, if MAC addresses updating is enabled, the MAC addresses are affected as follows:

  • The source MAC address is replaced by the TX_PORT MAC address
  • The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID

14.2. Compiling the Application

To compile the sample application see Compiling the Sample Applications.

The application is located in the l2fwd-crypt sub-directory.

14.3. Running the Application

The application requires a number of command line options:

./<build_dir>/examples/dpdk-l2fwd-crypto [EAL options] -- [-p PORTMASK] [-q NQ] [-s] [-T PERIOD] /
[--cdev_type HW/SW/ANY] [--chain HASH_CIPHER/CIPHER_HASH/CIPHER_ONLY/HASH_ONLY/AEAD] /
[--cipher_algo ALGO] [--cipher_op ENCRYPT/DECRYPT] [--cipher_dataunit_len SIZE] /
[--cipher_key KEY] [--cipher_key_random_size SIZE] [--cipher_iv IV] /
[--cipher_iv_random_size SIZE] /
[--auth_algo ALGO] [--auth_op GENERATE/VERIFY] [--auth_key KEY] /
[--auth_key_random_size SIZE] [--auth_iv IV] [--auth_iv_random_size SIZE] /
[--aead_algo ALGO] [--aead_op ENCRYPT/DECRYPT] [--aead_key KEY] /
[--aead_key_random_size SIZE] [--aead_iv] [--aead_iv_random_size SIZE] /
[--aad AAD] [--aad_random_size SIZE] /
[--digest size SIZE] [--sessionless] [--cryptodev_mask MASK] /
[--mac-updating] [--no-mac-updating]

where,

  • p PORTMASK: A hexadecimal bitmask of the ports to configure. (Default is all the ports.)

  • q NQ: A number of queues (=ports) per lcore. (Default is 1.)

  • s: manage all ports from a single core.

  • T PERIOD: statistics will be refreshed each PERIOD seconds.

    (0 to disable, 10 default, 86400 maximum.)

  • cdev_type: select preferred crypto device type: HW, SW or anything (ANY).

    (Default is ANY.)

  • chain: select the operation chaining to perform: Cipher->Hash (CIPHER_HASH),

    Hash->Cipher (HASH_CIPHER), Cipher (CIPHER_ONLY), Hash (HASH_ONLY)

    or AEAD (AEAD).

    (Default is Cipher->Hash.)

  • cipher_algo: select the ciphering algorithm. (Default is aes-cbc.)

  • cipher_op: select the ciphering operation to perform: ENCRYPT or DECRYPT.

    (Default is ENCRYPT.)

  • cipher_dataunit_len: set the length of the cipher data-unit.

  • cipher_key: set the ciphering key to be used. Bytes have to be separated with “:”.

  • cipher_key_random_size: set the size of the ciphering key,

    which will be generated randomly.

    Note that if –cipher_key is used, this will be ignored.

  • cipher_iv: set the cipher IV to be used. Bytes have to be separated with “:”.

  • cipher_iv_random_size: set the size of the cipher IV, which will be generated randomly.

    Note that if –cipher_iv is used, this will be ignored.

  • auth_algo: select the authentication algorithm. (Default is sha1-hmac.)

  • auth_op: select the authentication operation to perform: GENERATE or VERIFY.

    (Default is GENERATE.)

  • auth_key: set the authentication key to be used. Bytes have to be separated with “:”.

  • auth_key_random_size: set the size of the authentication key,

    which will be generated randomly.

    Note that if –auth_key is used, this will be ignored.

  • auth_iv: set the auth IV to be used. Bytes have to be separated with “:”.

  • auth_iv_random_size: set the size of the auth IV, which will be generated randomly.

    Note that if –auth_iv is used, this will be ignored.

  • aead_algo: select the AEAD algorithm. (Default is aes-gcm.)

  • aead_op: select the AEAD operation to perform: ENCRYPT or DECRYPT.

    (Default is ENCRYPT.)

  • aead_key: set the AEAD key to be used. Bytes have to be separated with “:”.

  • aead_key_random_size: set the size of the AEAD key,

    which will be generated randomly.

    Note that if –aead_key is used, this will be ignored.

  • aead_iv: set the AEAD IV to be used. Bytes have to be separated with “:”.

  • aead_iv_random_size: set the size of the AEAD IV, which will be generated randomly.

    Note that if –aead_iv is used, this will be ignored.

  • aad: set the AAD to be used. Bytes have to be separated with “:”.

  • aad_random_size: set the size of the AAD, which will be generated randomly.

    Note that if –aad is used, this will be ignored.

  • digest_size: set the size of the digest to be generated/verified.

  • sessionless: no crypto session will be created.

  • cryptodev_mask: A hexadecimal bitmask of the cryptodevs to be used by the application.

    (Default is all cryptodevs.)

  • [no-]mac-updating: Enable or disable MAC addresses updating. (Enabled by default.)

The application requires that crypto devices capable of performing the specified crypto operation are available on application initialization. This means that HW crypto device/s must be bound to a DPDK driver or a SW crypto device/s (virtual crypto PMD) must be created (using –vdev).

To run the application in linux environment with 2 lcores, 2 ports and 2 crypto devices, issue the command:

$ ./<build_dir>/examples/dpdk-l2fwd-crypto -l 0-1 --vdev "crypto_aesni_mb0" \
--vdev "crypto_aesni_mb1" -- -p 0x3 --chain CIPHER_HASH \
--cipher_op ENCRYPT --cipher_algo aes-cbc \
--cipher_key 00:01:02:03:04:05:06:07:08:09:0a:0b:0c:0d:0e:0f \
--auth_op GENERATE --auth_algo aes-xcbc-mac \
--auth_key 10:11:12:13:14:15:16:17:18:19:1a:1b:1c:1d:1e:1f

Refer to the DPDK Getting Started Guide for general information on running applications and the Environment Abstraction Layer (EAL) options.

Note

  • The l2fwd-crypto sample application requires IPv4 packets for crypto operation.
  • If multiple Ethernet ports are passed, then equal number of crypto devices are to be passed.
  • All crypto devices shall use the same session.

14.4. Explanation

The L2 forward with Crypto application demonstrates the performance of a crypto operation on a packet received on an RX PORT before forwarding it to a TX PORT.

The following figure illustrates a sample flow of a packet in the application, from reception until transmission.

../_images/l2_fwd_encrypt_flow.svg

Fig. 14.1 Encryption flow through the L2 Forwarding with Crypto Application

The following sections provide some explanation of the application.

14.4.1. Crypto operation specification

All the packets received in all the ports get transformed by the crypto device/s (ciphering and/or authentication). The crypto operation to be performed on the packet is parsed from the command line. (Go to “Running the Application” section for all the options.)

If no parameter is passed, the default crypto operation is:

  • Encryption with AES-CBC with 128 bit key.
  • Authentication with SHA1-HMAC (generation).
  • Keys, IV and AAD are generated randomly.

There are two methods to pass keys, IV and ADD from the command line:

  • Passing the full key, separated bytes by “:”:

    --cipher_key 00:11:22:33:44
    
  • Passing the size, so key is generated randomly:

    --cipher_key_random_size 16
    
Note:
If full key is passed (first method) and the size is passed as well (second method), the latter will be ignored.

Size of these keys are checked (regardless the method), before starting the app, to make sure that it is supported by the crypto devices.

14.4.2. Crypto device initialization

Once the encryption operation is defined, crypto devices are initialized. The crypto devices must be either bound to a DPDK driver (if they are physical devices) or created using the EAL option –vdev (if they are virtual devices), when running the application.

The initialize_cryptodevs() function performs the device initialization. It iterates through the list of the available crypto devices and checks which ones are capable of performing the operation. Each device has a set of capabilities associated with it, which are stored in the device info structure, so the function checks if the operation is within the structure of each device.

The following code checks if the device supports the specified cipher algorithm (similar for the authentication algorithm):

cap = check_device_support_cipher_algo(options, &dev_info,
				cdev_id);
if (cap == NULL)
	return -1;

if (check_iv_param(&cap->sym.cipher.iv_size,
		options->cipher_iv_param,
		options->cipher_iv_random_size,
		options->cipher_iv.length) != 0) {
	RTE_LOG(DEBUG, USER1,
		"Device %u does not support IV length\n",
		cdev_id);
	return -1;
}

If a capable crypto device is found, key sizes are checked to see if they are supported (cipher key and IV for the ciphering):


/*
 * Check if length of provided cipher key is supported
 * by the algorithm chosen.
 */
if (options->ckey_param) {
	if (check_supported_size(
			options->cipher_xform.cipher.key.length,
			cap->sym.cipher.key_size.min,
			cap->sym.cipher.key_size.max,
			cap->sym.cipher.key_size.increment)
				!= 0) {
		if (dev_info.feature_flags &
		    RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY) {
			RTE_LOG(DEBUG, USER1,
			"Key length does not match the device "
			"%u capability. Key may be wrapped\n",
			cdev_id);
		} else {
			RTE_LOG(DEBUG, USER1,
			"Key length does not match the device "
			"%u capability\n",
			cdev_id);
			return -1;
		}
	}

/*
 * Check if length of the cipher key to be randomly generated
 * is supported by the algorithm chosen.
 */
} else if (options->ckey_random_size != -1) {
	if (check_supported_size(options->ckey_random_size,
			cap->sym.cipher.key_size.min,
			cap->sym.cipher.key_size.max,
			cap->sym.cipher.key_size.increment)
				!= 0) {
		RTE_LOG(DEBUG, USER1,
			"Device %u does not support cipher "
			"key length\n",
			cdev_id);
		return -1;
	}
}

if (options->cipher_xform.cipher.dataunit_len > 0) {
	if (!(dev_info.feature_flags &
		RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS)) {
		RTE_LOG(DEBUG, USER1,
			"Device %u does not support "
			"cipher multiple data units\n",
			cdev_id);
		return -1;
	}
	if (cap->sym.cipher.dataunit_set != 0) {
		int ret = 0;

		switch (options->cipher_xform.cipher.dataunit_len) {
		case 512:
			if (!(cap->sym.cipher.dataunit_set &
				RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES))
				ret = -1;
			break;
		case 4096:
			if (!(cap->sym.cipher.dataunit_set &
				RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_4096_BYTES))
				ret = -1;
			break;
		case 1048576:
			if (!(cap->sym.cipher.dataunit_set &
				RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_1_MEGABYTES))
				ret = -1;
			break;
		default:
			ret = -1;
		}
		if (ret == -1) {
			RTE_LOG(DEBUG, USER1,
				"Device %u does not support "
				"data-unit length %u\n",
				cdev_id,
				options->cipher_xform.cipher.dataunit_len);
			return -1;
		}
	}
}

After all the checks, the device is configured and it is added to the crypto device list.

Note:
The number of crypto devices that supports the specified crypto operation must be at least the number of ports to be used.

14.4.3. Session creation

The crypto operation has a crypto session associated to it, which contains information such as the transform chain to perform (e.g. ciphering then hashing), pointers to the keys, lengths… etc.

This session is created and is later attached to the crypto operation:

static void *
initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
{
	struct rte_crypto_sym_xform *first_xform;
	int retval = rte_cryptodev_socket_id(cdev_id);

	if (retval < 0)
		return NULL;

	uint8_t socket_id = (uint8_t) retval;

	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
		first_xform = &options->aead_xform;
	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
		first_xform = &options->cipher_xform;
		first_xform->next = &options->auth_xform;
	} else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
		first_xform = &options->auth_xform;
		first_xform->next = &options->cipher_xform;
	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
		first_xform = &options->cipher_xform;
	} else {
		first_xform = &options->auth_xform;
	}

	return rte_cryptodev_sym_session_create(cdev_id, first_xform,
			session_pool_socket[socket_id].sess_mp);
}

14.4.4. Crypto operation creation

Given N packets received from an RX PORT, N crypto operations are allocated and filled:

if (nb_rx) {
	/*
	 * If we can't allocate a crypto_ops, then drop
	 * the rest of the burst and dequeue and
	 * process the packets to free offload structs
	 */
	if (rte_crypto_op_bulk_alloc(
			l2fwd_crypto_op_pool,
			RTE_CRYPTO_OP_TYPE_SYMMETRIC,
			ops_burst, nb_rx) !=
					nb_rx) {
		for (j = 0; j < nb_rx; j++)
			rte_pktmbuf_free(pkts_burst[j]);

		nb_rx = 0;
	}

After filling the crypto operation (including session attachment), the mbuf which will be transformed is attached to it:

op->sym->m_src = m;

Since no destination mbuf is set, the source mbuf will be overwritten after the operation is done (in-place).

14.4.5. Crypto operation enqueuing/dequeuing

Once the operation has been created, it has to be enqueued in one of the crypto devices. Before doing so, for performance reasons, the operation stays in a buffer. When the buffer has enough operations (MAX_PKT_BURST), they are enqueued in the device, which will perform the operation at that moment:

static int
l2fwd_crypto_enqueue(struct rte_crypto_op *op,
		struct l2fwd_crypto_params *cparams)
{
	unsigned lcore_id, len;
	struct lcore_queue_conf *qconf;

	lcore_id = rte_lcore_id();

	qconf = &lcore_queue_conf[lcore_id];
	len = qconf->op_buf[cparams->dev_id].len;
	qconf->op_buf[cparams->dev_id].buffer[len] = op;
	len++;

	/* enough ops to be sent */
	if (len == MAX_PKT_BURST) {
		l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
		len = 0;
	}

	qconf->op_buf[cparams->dev_id].len = len;
	return 0;
}
static int
l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
		struct l2fwd_crypto_params *cparams)
{
	struct rte_crypto_op **op_buffer;
	unsigned ret;

	op_buffer = (struct rte_crypto_op **)
			qconf->op_buf[cparams->dev_id].buffer;

	ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
			cparams->qp_id,	op_buffer, (uint16_t) n);

	crypto_statistics[cparams->dev_id].enqueued += ret;
	if (unlikely(ret < n)) {
		crypto_statistics[cparams->dev_id].errors += (n - ret);
		do {
			rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
			rte_crypto_op_free(op_buffer[ret]);
		} while (++ret < n);
	}

	return 0;
}

After this, the operations are dequeued from the device, and the transformed mbuf is extracted from the operation. Then, the operation is freed and the mbuf is forwarded as it is done in the L2 forwarding application.

do {
	nb_rx = rte_cryptodev_dequeue_burst(
			cparams->dev_id, cparams->qp_id,
			ops_burst, MAX_PKT_BURST);

	crypto_statistics[cparams->dev_id].dequeued +=
			nb_rx;

	/* Forward crypto'd packets */
	for (j = 0; j < nb_rx; j++) {
		m = ops_burst[j]->sym->m_src;

		rte_crypto_op_free(ops_burst[j]);
		l2fwd_simple_forward(m, portid,
				options);
	}
} while (nb_rx == MAX_PKT_BURST);