Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2017 Intel Corporation
3 : : */
4 : :
5 : : #include <ethdev_driver.h>
6 : : #include <ethdev_pci.h>
7 : : #include <rte_ip.h>
8 : : #include <rte_jhash.h>
9 : : #include <rte_security_driver.h>
10 : : #include <rte_cryptodev.h>
11 : : #include <rte_flow.h>
12 : :
13 : : #include "base/ixgbe_type.h"
14 : : #include "base/ixgbe_api.h"
15 : : #include "ixgbe_ethdev.h"
16 : : #include "ixgbe_ipsec.h"
17 : :
18 : : #define IXGBE_REGISTER_POLL_WAIT_5_MS 5
19 : :
20 : : #define IXGBE_WAIT_RREAD \
21 : : IXGBE_WRITE_REG_THEN_POLL_MASK(hw, IXGBE_IPSRXIDX, reg_val, \
22 : : IPSRXIDX_READ, IXGBE_REGISTER_POLL_WAIT_5_MS)
23 : : #define IXGBE_WAIT_RWRITE \
24 : : IXGBE_WRITE_REG_THEN_POLL_MASK(hw, IXGBE_IPSRXIDX, reg_val, \
25 : : IPSRXIDX_WRITE, IXGBE_REGISTER_POLL_WAIT_5_MS)
26 : : #define IXGBE_WAIT_TREAD \
27 : : IXGBE_WRITE_REG_THEN_POLL_MASK(hw, IXGBE_IPSTXIDX, reg_val, \
28 : : IPSRXIDX_READ, IXGBE_REGISTER_POLL_WAIT_5_MS)
29 : : #define IXGBE_WAIT_TWRITE \
30 : : IXGBE_WRITE_REG_THEN_POLL_MASK(hw, IXGBE_IPSTXIDX, reg_val, \
31 : : IPSRXIDX_WRITE, IXGBE_REGISTER_POLL_WAIT_5_MS)
32 : :
33 : : static inline void
34 : 0 : ixgbe_crypto_write_rx_ip(struct ixgbe_hw *hw, uint32_t idx,
35 : : const struct ipaddr *ip, bool enable)
36 : : {
37 : 0 : uint32_t reg_val = IPSRXIDX_WRITE | IPSRXIDX_TABLE_IP | (idx << 3);
38 : 0 : uint32_t addr[4] = {0};
39 : :
40 [ # # ]: 0 : if (enable)
41 : 0 : reg_val |= IPSRXIDX_RX_EN;
42 : :
43 [ # # ]: 0 : if (ip->type == IPv4)
44 : : /* only write last 4 bytes */
45 : 0 : addr[3] = ip->ipv4;
46 : : else
47 : : memcpy(addr, ip->ipv6, sizeof(addr));
48 : :
49 : 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(0), addr[0]);
50 : 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(1), addr[1]);
51 : 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(2), addr[2]);
52 : 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPADDR(3), addr[3]);
53 [ # # # # ]: 0 : IXGBE_WAIT_RWRITE;
54 : 0 : }
55 : :
56 : : static inline void
57 : 0 : ixgbe_crypto_write_rx_spi(struct ixgbe_hw *hw, uint32_t idx,
58 : : uint32_t spi, uint32_t ip_idx, bool enable)
59 : : {
60 : 0 : uint32_t reg_val = IPSRXIDX_WRITE | IPSRXIDX_TABLE_SPI | (idx << 3);
61 : :
62 [ # # ]: 0 : if (enable)
63 : 0 : reg_val |= IPSRXIDX_RX_EN;
64 : :
65 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXSPI, rte_cpu_to_be_32(spi));
66 : 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXIPIDX, ip_idx);
67 [ # # # # ]: 0 : IXGBE_WAIT_RWRITE;
68 : 0 : }
69 : :
70 : : static inline void
71 : 0 : ixgbe_crypto_write_rx_key(struct ixgbe_hw *hw, uint32_t idx,
72 : : const uint8_t *key, uint32_t salt, uint32_t mode, bool enable)
73 : : {
74 : 0 : uint32_t reg_val = IPSRXIDX_WRITE | IPSRXIDX_TABLE_KEY | (idx << 3);
75 : :
76 [ # # ]: 0 : if (enable)
77 : 0 : reg_val |= IPSRXIDX_RX_EN;
78 : :
79 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(0),
80 : : rte_cpu_to_be_32(*(const uint32_t *)&key[12]));
81 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(1),
82 : : rte_cpu_to_be_32(*(const uint32_t *)&key[8]));
83 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(2),
84 : : rte_cpu_to_be_32(*(const uint32_t *)&key[4]));
85 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXKEY(3),
86 : : rte_cpu_to_be_32(*(const uint32_t *)&key[0]));
87 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXSALT, rte_cpu_to_be_32(salt));
88 : 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSRXMOD, mode);
89 [ # # # # ]: 0 : IXGBE_WAIT_RWRITE;
90 : 0 : }
91 : :
92 : : static inline void
93 : 0 : ixgbe_crypto_write_tx_key(struct ixgbe_hw *hw, uint32_t idx,
94 : : const uint8_t *key, uint32_t salt, bool enable)
95 : : {
96 : 0 : uint32_t reg_val = IPSRXIDX_WRITE | (idx << 3);
97 : :
98 [ # # ]: 0 : if (enable)
99 : 0 : reg_val |= IPSRXIDX_TX_EN;
100 : :
101 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(0),
102 : : rte_cpu_to_be_32(*(const uint32_t *)&key[12]));
103 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(1),
104 : : rte_cpu_to_be_32(*(const uint32_t *)&key[8]));
105 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(2),
106 : : rte_cpu_to_be_32(*(const uint32_t *)&key[4]));
107 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSTXKEY(3),
108 : : rte_cpu_to_be_32(*(const uint32_t *)&key[0]));
109 [ # # ]: 0 : IXGBE_WRITE_REG(hw, IXGBE_IPSTXSALT, rte_cpu_to_be_32(salt));
110 [ # # # # ]: 0 : IXGBE_WAIT_TWRITE;
111 : 0 : }
112 : :
113 : : static void
114 : 0 : ixgbe_crypto_clear_ipsec_tables(struct rte_eth_dev *dev)
115 : : {
116 : 0 : struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
117 : : struct ixgbe_ipsec *priv = IXGBE_DEV_PRIVATE_TO_IPSEC(
118 : : dev->data->dev_private);
119 : 0 : const struct ipaddr ip = {0};
120 : 0 : const uint8_t key[16] = {0};
121 : : int i = 0;
122 : :
123 : : /* clear Rx IP table*/
124 [ # # ]: 0 : for (i = 0; i < IPSEC_MAX_RX_IP_COUNT; i++)
125 : 0 : ixgbe_crypto_write_rx_ip(hw, i, &ip, false);
126 : :
127 : : /* clear Rx SPI and Rx/Tx SA tables*/
128 [ # # ]: 0 : for (i = 0; i < IPSEC_MAX_SA_COUNT; i++) {
129 : 0 : ixgbe_crypto_write_rx_spi(hw, i, 0, 0, false);
130 : 0 : ixgbe_crypto_write_rx_key(hw, i, key, 0, 0, false);
131 : 0 : ixgbe_crypto_write_tx_key(hw, i, key, 0, false);
132 : :
133 : 0 : priv->tx_sa_tbl[i] = (struct ixgbe_crypto_tx_sa_table){0};
134 : : }
135 : 0 : }
136 : :
137 : : void
138 : 0 : ixgbe_crypto_install_rx_sa(struct rte_eth_dev_data *dev_data,
139 : : const struct ixgbe_crypto_rx_sa *sa)
140 : : {
141 : 0 : struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev_data->dev_private);
142 : : uint8_t key[16];
143 : :
144 : 0 : ixgbe_crypto_write_rx_ip(hw, sa->ip_idx, &sa->dst_ip, true);
145 : 0 : ixgbe_crypto_write_rx_spi(hw, sa->sa_idx, sa->spi, sa->ip_idx, true);
146 : 0 : memcpy(key, sa->key, sizeof(key));
147 : 0 : ixgbe_crypto_write_rx_key(hw, sa->sa_idx, (const uint8_t *)key,
148 : 0 : sa->salt, sa->mode, true);
149 : 0 : rte_memzero_explicit(key, sizeof(key));
150 : 0 : }
151 : :
152 : : void
153 : 0 : ixgbe_crypto_uninstall_rx_sa(struct rte_eth_dev_data *dev_data,
154 : : const struct ixgbe_crypto_rx_sa *sa, bool clear_ip)
155 : : {
156 : 0 : struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev_data->dev_private);
157 : 0 : const uint8_t key[16] = {0};
158 : :
159 : 0 : ixgbe_crypto_write_rx_spi(hw, sa->sa_idx, 0, 0, false);
160 : 0 : ixgbe_crypto_write_rx_key(hw, sa->sa_idx, key, 0, 0, false);
161 [ # # ]: 0 : if (clear_ip) {
162 : 0 : const struct ipaddr ip = {0};
163 : :
164 : 0 : ixgbe_crypto_write_rx_ip(hw, sa->ip_idx, &ip, false);
165 : : }
166 : 0 : }
167 : :
168 : : static int
169 : 0 : ixgbe_crypto_add_tx_sa(struct ixgbe_crypto_session *ic_session)
170 : : {
171 : 0 : struct rte_eth_dev_data *dev_data = ic_session->dev_data;
172 : 0 : struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev_data->dev_private);
173 : : struct ixgbe_ipsec *priv = IXGBE_DEV_PRIVATE_TO_IPSEC(dev_data->dev_private);
174 : : struct ixgbe_crypto_tx_sa_table *txsa;
175 : : uint8_t key[16];
176 : : int sa_index = -1;
177 : : int i;
178 : :
179 [ # # ]: 0 : for (i = 0; i < IPSEC_MAX_SA_COUNT; i++) {
180 [ # # ]: 0 : if (priv->tx_sa_tbl[i].used == 0) {
181 : : sa_index = i;
182 : : break;
183 : : }
184 : : }
185 [ # # ]: 0 : if (sa_index < 0) {
186 : 0 : PMD_DRV_LOG(ERR, "No free entry left in the Tx SA table");
187 : 0 : return -ENOSPC;
188 : : }
189 : : txsa = &priv->tx_sa_tbl[sa_index];
190 : 0 : txsa->spi = ic_session->spi;
191 : 0 : txsa->used = 1;
192 : 0 : ic_session->sa_index = sa_index;
193 : :
194 : 0 : memcpy(key, ic_session->key, ic_session->key_len);
195 : 0 : ixgbe_crypto_write_tx_key(hw, sa_index, (const uint8_t *)key,
196 : : ic_session->salt, true);
197 : 0 : rte_memzero_explicit(key, sizeof(key));
198 : :
199 : 0 : return 0;
200 : : }
201 : :
202 : : static int
203 : 0 : ixgbe_crypto_remove_tx_sa(struct ixgbe_crypto_session *ic_session)
204 : : {
205 : 0 : struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(ic_session->dev_data->dev_private);
206 : : struct ixgbe_ipsec *priv =
207 : : IXGBE_DEV_PRIVATE_TO_IPSEC(ic_session->dev_data->dev_private);
208 : : struct ixgbe_crypto_tx_sa_table *txsa;
209 : 0 : const uint8_t key[16] = {0};
210 : :
211 [ # # ]: 0 : if (ic_session->sa_index >= IPSEC_MAX_SA_COUNT)
212 : : return -ENOENT;
213 : : txsa = &priv->tx_sa_tbl[ic_session->sa_index];
214 [ # # # # ]: 0 : if (txsa->used == 0 || txsa->spi != ic_session->spi)
215 : : return -ENOENT;
216 : :
217 : 0 : ixgbe_crypto_write_tx_key(hw, ic_session->sa_index, key, 0, false);
218 : 0 : *txsa = (struct ixgbe_crypto_tx_sa_table){0};
219 : :
220 : 0 : return 0;
221 : : }
222 : :
223 : : static int
224 : 0 : ixgbe_crypto_session_state_acquire(struct ixgbe_crypto_session *ic_session)
225 : : {
226 : : uint16_t expected;
227 : :
228 : 0 : expected = rte_atomic_load_explicit(&ic_session->refcnt,
229 : : rte_memory_order_acquire);
230 [ # # ]: 0 : while (expected != IXGBE_SECURITY_SESSION_DESTROYING) {
231 [ # # ]: 0 : if (expected >= IXGBE_SECURITY_SESSION_REFCNT_MAX)
232 : : return -ENOSPC;
233 [ # # ]: 0 : if (rte_atomic_compare_exchange_strong_explicit(&ic_session->refcnt,
234 : : &expected, expected + 1,
235 : : rte_memory_order_acq_rel, rte_memory_order_acquire))
236 : : return 0;
237 : : }
238 : :
239 : : return -EBUSY;
240 : : }
241 : :
242 : : static int
243 : 0 : ixgbe_crypto_session_state_release(struct ixgbe_crypto_session *ic_session)
244 : : {
245 : : uint16_t expected;
246 : :
247 : 0 : expected = rte_atomic_load_explicit(&ic_session->refcnt,
248 : : rte_memory_order_acquire);
249 [ # # ]: 0 : while (expected != IXGBE_SECURITY_SESSION_DESTROYING) {
250 [ # # ]: 0 : if (expected == IXGBE_SECURITY_SESSION_UNBOUND)
251 : : return -EINVAL;
252 [ # # ]: 0 : if (rte_atomic_compare_exchange_strong_explicit(&ic_session->refcnt,
253 : : &expected, expected - 1,
254 : : rte_memory_order_release, rte_memory_order_relaxed))
255 : : return 0;
256 : : }
257 : :
258 : : return -EINVAL;
259 : : }
260 : :
261 : : int
262 : 0 : ixgbe_crypto_session_acquire(struct rte_security_session *session)
263 : : {
264 : 0 : struct ixgbe_crypto_session *ic_session = SECURITY_GET_SESS_PRIV(session);
265 : :
266 : 0 : return ixgbe_crypto_session_state_acquire(ic_session);
267 : : }
268 : :
269 : : int
270 : 0 : ixgbe_crypto_session_release(struct rte_security_session *session)
271 : : {
272 : 0 : struct ixgbe_crypto_session *ic_session = SECURITY_GET_SESS_PRIV(session);
273 : :
274 : 0 : return ixgbe_crypto_session_state_release(ic_session);
275 : : }
276 : :
277 : : static int
278 : : ixgbe_crypto_session_disable(struct rte_security_session *session)
279 : : {
280 : : struct ixgbe_crypto_session *ic_session = SECURITY_GET_SESS_PRIV(session);
281 : : uint16_t expected;
282 : :
283 : : /*
284 : : * Rx sessions must have no flow references before removal. Tx sessions
285 : : * retain the reference acquired when their SA was allocated.
286 : : */
287 : 0 : expected = ic_session->op == IXGBE_OP_AUTHENTICATED_DECRYPTION ?
288 : 0 : IXGBE_SECURITY_SESSION_UNBOUND : 1;
289 [ # # ]: 0 : if (!rte_atomic_compare_exchange_strong_explicit(&ic_session->refcnt,
290 : : &expected, IXGBE_SECURITY_SESSION_DESTROYING,
291 : : rte_memory_order_acq_rel, rte_memory_order_acquire))
292 : : return -EBUSY;
293 : : return 0;
294 : : }
295 : :
296 : : static int
297 : 0 : ixgbe_crypto_create_session(void *device,
298 : : struct rte_security_session_conf *conf,
299 : : struct rte_security_session *session)
300 : : {
301 : : struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)device;
302 : 0 : struct ixgbe_crypto_session *ic_session = SECURITY_GET_SESS_PRIV(session);
303 : : struct rte_crypto_aead_xform *aead_xform;
304 : 0 : struct rte_eth_conf *dev_conf = ð_dev->data->dev_conf;
305 : : int ret;
306 : :
307 [ # # ]: 0 : if (conf->crypto_xform->type != RTE_CRYPTO_SYM_XFORM_AEAD ||
308 [ # # ]: 0 : conf->crypto_xform->aead.algo !=
309 : : RTE_CRYPTO_AEAD_AES_GCM) {
310 : 0 : PMD_DRV_LOG(ERR, "Unsupported crypto transformation mode");
311 : 0 : return -ENOTSUP;
312 : : }
313 : : aead_xform = &conf->crypto_xform->aead;
314 : :
315 : : /* Only 16-byte keys are supported. */
316 [ # # ]: 0 : if (aead_xform->key.length != 16) {
317 : 0 : PMD_DRV_LOG(ERR, "Unsupported key length %u", aead_xform->key.length);
318 : 0 : return -ENOTSUP;
319 : : }
320 : :
321 [ # # ]: 0 : if (conf->ipsec.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
322 [ # # ]: 0 : if (dev_conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_SECURITY) {
323 : 0 : ic_session->op = IXGBE_OP_AUTHENTICATED_DECRYPTION;
324 : : } else {
325 : 0 : PMD_DRV_LOG(ERR, "IPsec decryption not enabled");
326 : 0 : return -ENOTSUP;
327 : : }
328 : : } else {
329 [ # # ]: 0 : if (dev_conf->txmode.offloads & RTE_ETH_TX_OFFLOAD_SECURITY) {
330 : 0 : ic_session->op = IXGBE_OP_AUTHENTICATED_ENCRYPTION;
331 : : } else {
332 : 0 : PMD_DRV_LOG(ERR, "IPsec encryption not enabled");
333 : 0 : return -ENOTSUP;
334 : : }
335 : : }
336 : :
337 [ # # ]: 0 : memcpy(ic_session->key, aead_xform->key.data, sizeof(ic_session->key));
338 : 0 : ic_session->key_len = aead_xform->key.length;
339 : 0 : memcpy(&ic_session->salt,
340 : 0 : &aead_xform->key.data[aead_xform->key.length], 4);
341 : 0 : ic_session->spi = conf->ipsec.spi;
342 : 0 : ic_session->dev_data = eth_dev->data;
343 : :
344 : 0 : rte_atomic_store_explicit(&ic_session->refcnt,
345 : : IXGBE_SECURITY_SESSION_UNBOUND,
346 : : rte_memory_order_relaxed);
347 : :
348 : : /* only handle tx, as rx sessions are created by rte_flow */
349 [ # # ]: 0 : if (ic_session->op == IXGBE_OP_AUTHENTICATED_ENCRYPTION) {
350 [ # # ]: 0 : if (ixgbe_crypto_add_tx_sa(ic_session)) {
351 : 0 : PMD_DRV_LOG(ERR, "Failed to add SA");
352 : 0 : rte_memzero_explicit(ic_session, sizeof(*ic_session));
353 : 0 : return -EPERM;
354 : : }
355 : 0 : ret = ixgbe_crypto_session_state_acquire(ic_session);
356 [ # # ]: 0 : if (ret != 0) {
357 : 0 : ixgbe_crypto_remove_tx_sa(ic_session);
358 : 0 : rte_memzero_explicit(ic_session, sizeof(*ic_session));
359 : 0 : return ret;
360 : : }
361 : : }
362 : :
363 : : return 0;
364 : : }
365 : :
366 : : static unsigned int
367 : 0 : ixgbe_crypto_session_get_size(__rte_unused void *device)
368 : : {
369 : 0 : return sizeof(struct ixgbe_crypto_session);
370 : : }
371 : :
372 : : static int
373 : 0 : ixgbe_crypto_remove_session(void *device,
374 : : struct rte_security_session *session)
375 : : {
376 : : struct rte_eth_dev *eth_dev = device;
377 : 0 : struct ixgbe_crypto_session *ic_session = SECURITY_GET_SESS_PRIV(session);
378 : : int ret;
379 : :
380 [ # # ]: 0 : if (eth_dev->data != ic_session->dev_data) {
381 : 0 : PMD_DRV_LOG(ERR, "Session not bound to this device");
382 : 0 : return -ENODEV;
383 : : }
384 : :
385 : : if (ixgbe_crypto_session_disable(session) < 0) {
386 : 0 : PMD_DRV_LOG(ERR, "Session is still in use");
387 : 0 : return -EBUSY;
388 : : }
389 : :
390 [ # # ]: 0 : if (ic_session->op == IXGBE_OP_AUTHENTICATED_ENCRYPTION) {
391 : 0 : ret = ixgbe_crypto_remove_tx_sa(ic_session);
392 [ # # ]: 0 : if (ret != 0) {
393 : 0 : PMD_DRV_LOG(ERR, "Failed to remove session");
394 : : /* set refcnt back to 1 to re-enable the session */
395 : 0 : rte_atomic_store_explicit(&ic_session->refcnt, 1,
396 : : rte_memory_order_release);
397 : 0 : return ret;
398 : : }
399 : : }
400 : :
401 : 0 : rte_memzero_explicit(ic_session, sizeof(*ic_session));
402 : 0 : return 0;
403 : : }
404 : :
405 : : static inline uint8_t
406 : : ixgbe_crypto_compute_pad_len(struct rte_mbuf *m)
407 : : {
408 : 0 : if (m->nb_segs == 1) {
409 : : /* 16 bytes ICV + 2 bytes ESP trailer + payload padding size
410 : : * payload padding size is stored at <pkt_len - 18>
411 : : */
412 : 0 : uint8_t *esp_pad_len = rte_pktmbuf_mtod_offset(m, uint8_t *,
413 : : rte_pktmbuf_pkt_len(m) -
414 : : (ESP_TRAILER_SIZE + ESP_ICV_SIZE));
415 : 0 : return *esp_pad_len + ESP_TRAILER_SIZE + ESP_ICV_SIZE;
416 : : }
417 : : return 0;
418 : : }
419 : :
420 : : static int
421 : 0 : ixgbe_crypto_update_mb(void *device __rte_unused,
422 : : struct rte_security_session *session,
423 : : struct rte_mbuf *m, void *params __rte_unused)
424 : : {
425 : : struct ixgbe_crypto_session *ic_session = SECURITY_GET_SESS_PRIV(session);
426 [ # # ]: 0 : if (ic_session->op == IXGBE_OP_AUTHENTICATED_ENCRYPTION) {
427 : : union ixgbe_crypto_tx_desc_md *mdata =
428 : : (union ixgbe_crypto_tx_desc_md *)
429 : : rte_security_dynfield(m);
430 : 0 : mdata->enc = 1;
431 [ # # ]: 0 : mdata->sa_idx = ic_session->sa_index;
432 : 0 : mdata->pad_len = ixgbe_crypto_compute_pad_len(m);
433 : : }
434 : 0 : return 0;
435 : : }
436 : :
437 : :
438 : : static const struct rte_security_capability *
439 : 0 : ixgbe_crypto_capabilities_get(void *device __rte_unused)
440 : : {
441 : : static const struct rte_cryptodev_capabilities
442 : : aes_gcm_gmac_crypto_capabilities[] = {
443 : : { /* AES GMAC (128-bit) */
444 : : .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
445 : : {.sym = {
446 : : .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
447 : : {.auth = {
448 : : .algo = RTE_CRYPTO_AUTH_AES_GMAC,
449 : : .block_size = 16,
450 : : .key_size = {
451 : : .min = 16,
452 : : .max = 16,
453 : : .increment = 0
454 : : },
455 : : .digest_size = {
456 : : .min = 16,
457 : : .max = 16,
458 : : .increment = 0
459 : : },
460 : : .iv_size = {
461 : : .min = 12,
462 : : .max = 12,
463 : : .increment = 0
464 : : }
465 : : }, }
466 : : }, }
467 : : },
468 : : { /* AES GCM (128-bit) */
469 : : .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
470 : : {.sym = {
471 : : .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
472 : : {.aead = {
473 : : .algo = RTE_CRYPTO_AEAD_AES_GCM,
474 : : .block_size = 16,
475 : : .key_size = {
476 : : .min = 16,
477 : : .max = 16,
478 : : .increment = 0
479 : : },
480 : : .digest_size = {
481 : : .min = 16,
482 : : .max = 16,
483 : : .increment = 0
484 : : },
485 : : .aad_size = {
486 : : .min = 0,
487 : : .max = 65535,
488 : : .increment = 1
489 : : },
490 : : .iv_size = {
491 : : .min = 12,
492 : : .max = 12,
493 : : .increment = 0
494 : : }
495 : : }, }
496 : : }, }
497 : : },
498 : : {
499 : : .op = RTE_CRYPTO_OP_TYPE_UNDEFINED,
500 : : {.sym = {
501 : : .xform_type = RTE_CRYPTO_SYM_XFORM_NOT_SPECIFIED
502 : : }, }
503 : : },
504 : : };
505 : :
506 : : static const struct rte_security_capability
507 : : ixgbe_security_capabilities[] = {
508 : : { /* IPsec Inline Crypto ESP Transport Egress */
509 : : .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
510 : : .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
511 : : {.ipsec = {
512 : : .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
513 : : .mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
514 : : .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
515 : : .options = { 0 }
516 : : } },
517 : : .crypto_capabilities = aes_gcm_gmac_crypto_capabilities,
518 : : .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
519 : : },
520 : : { /* IPsec Inline Crypto ESP Transport Ingress */
521 : : .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
522 : : .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
523 : : {.ipsec = {
524 : : .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
525 : : .mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
526 : : .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
527 : : .options = { 0 }
528 : : } },
529 : : .crypto_capabilities = aes_gcm_gmac_crypto_capabilities,
530 : : .ol_flags = 0
531 : : },
532 : : { /* IPsec Inline Crypto ESP Tunnel Egress */
533 : : .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
534 : : .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
535 : : {.ipsec = {
536 : : .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
537 : : .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
538 : : .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
539 : : .options = { 0 }
540 : : } },
541 : : .crypto_capabilities = aes_gcm_gmac_crypto_capabilities,
542 : : .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
543 : : },
544 : : { /* IPsec Inline Crypto ESP Tunnel Ingress */
545 : : .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
546 : : .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
547 : : {.ipsec = {
548 : : .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
549 : : .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
550 : : .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
551 : : .options = { 0 }
552 : : } },
553 : : .crypto_capabilities = aes_gcm_gmac_crypto_capabilities,
554 : : .ol_flags = 0
555 : : },
556 : : {
557 : : .action = RTE_SECURITY_ACTION_TYPE_NONE
558 : : }
559 : : };
560 : :
561 : 0 : return ixgbe_security_capabilities;
562 : : }
563 : :
564 : :
565 : : int
566 : 0 : ixgbe_crypto_enable_ipsec(struct rte_eth_dev *dev)
567 : : {
568 : 0 : struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
569 : : uint32_t reg;
570 : : uint64_t rx_offloads;
571 : : uint64_t tx_offloads;
572 : :
573 : 0 : rx_offloads = dev->data->dev_conf.rxmode.offloads;
574 : 0 : tx_offloads = dev->data->dev_conf.txmode.offloads;
575 : :
576 : : /* sanity checks */
577 [ # # ]: 0 : if (rx_offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO) {
578 : 0 : PMD_DRV_LOG(ERR, "RSC and IPsec not supported");
579 : 0 : return -1;
580 : : }
581 [ # # ]: 0 : if (rx_offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC) {
582 : 0 : PMD_DRV_LOG(ERR, "HW CRC strip needs to be enabled for IPsec");
583 : 0 : return -1;
584 : : }
585 : :
586 : :
587 : : /* Set IXGBE_SECTXBUFFAF to 0x15 as required in the datasheet*/
588 : 0 : IXGBE_WRITE_REG(hw, IXGBE_SECTXBUFFAF, 0x15);
589 : :
590 : : /* IFG needs to be set to 3 when we are using security. Otherwise a Tx
591 : : * hang will occur with heavy traffic.
592 : : */
593 : 0 : reg = IXGBE_READ_REG(hw, IXGBE_SECTXMINIFG);
594 : 0 : reg = (reg & 0xFFFFFFF0) | 0x3;
595 : 0 : IXGBE_WRITE_REG(hw, IXGBE_SECTXMINIFG, reg);
596 : :
597 : 0 : reg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
598 : 0 : reg |= IXGBE_HLREG0_TXCRCEN | IXGBE_HLREG0_RXCRCSTRP;
599 : 0 : IXGBE_WRITE_REG(hw, IXGBE_HLREG0, reg);
600 : :
601 [ # # ]: 0 : if (rx_offloads & RTE_ETH_RX_OFFLOAD_SECURITY) {
602 : 0 : IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, 0);
603 : 0 : reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
604 [ # # ]: 0 : if (reg != 0) {
605 : 0 : PMD_DRV_LOG(ERR, "Error enabling Rx Crypto");
606 : 0 : return -1;
607 : : }
608 : : }
609 [ # # ]: 0 : if (tx_offloads & RTE_ETH_TX_OFFLOAD_SECURITY) {
610 : 0 : IXGBE_WRITE_REG(hw, IXGBE_SECTXCTRL,
611 : : IXGBE_SECTXCTRL_STORE_FORWARD);
612 : 0 : reg = IXGBE_READ_REG(hw, IXGBE_SECTXCTRL);
613 [ # # ]: 0 : if (reg != IXGBE_SECTXCTRL_STORE_FORWARD) {
614 : 0 : PMD_DRV_LOG(ERR, "Error enabling Rx Crypto");
615 : 0 : return -1;
616 : : }
617 : : }
618 : :
619 : 0 : ixgbe_crypto_clear_ipsec_tables(dev);
620 : :
621 : 0 : return 0;
622 : : }
623 : :
624 : : static struct rte_security_ops ixgbe_security_ops = {
625 : : .session_create = ixgbe_crypto_create_session,
626 : : .session_update = NULL,
627 : : .session_get_size = ixgbe_crypto_session_get_size,
628 : : .session_stats_get = NULL,
629 : : .session_destroy = ixgbe_crypto_remove_session,
630 : : .set_pkt_metadata = ixgbe_crypto_update_mb,
631 : : .capabilities_get = ixgbe_crypto_capabilities_get
632 : : };
633 : :
634 : : static int
635 : : ixgbe_crypto_capable(struct rte_eth_dev *dev)
636 : : {
637 : 0 : struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
638 : : uint32_t reg_i, reg, capable = 1;
639 : : /* test if rx crypto can be enabled and then write back initial value*/
640 : 0 : reg_i = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
641 : 0 : IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, 0);
642 : 0 : reg = IXGBE_READ_REG(hw, IXGBE_SECRXCTRL);
643 [ # # ]: 0 : if (reg != 0)
644 : : capable = 0;
645 : 0 : IXGBE_WRITE_REG(hw, IXGBE_SECRXCTRL, reg_i);
646 : 0 : return capable;
647 : : }
648 : :
649 : : int
650 : 0 : ixgbe_ipsec_ctx_create(struct rte_eth_dev *dev)
651 : : {
652 : : struct rte_security_ctx *ctx = NULL;
653 : :
654 [ # # ]: 0 : if (ixgbe_crypto_capable(dev)) {
655 : 0 : ctx = rte_malloc("rte_security_instances_ops",
656 : : sizeof(struct rte_security_ctx), 0);
657 [ # # ]: 0 : if (ctx) {
658 : 0 : ctx->device = (void *)dev;
659 : 0 : ctx->ops = &ixgbe_security_ops;
660 : 0 : ctx->sess_cnt = 0;
661 : 0 : dev->security_ctx = ctx;
662 : : } else {
663 : : return -ENOMEM;
664 : : }
665 : : }
666 [ # # ]: 0 : if (rte_security_dynfield_register() < 0)
667 : 0 : return -rte_errno;
668 : : return 0;
669 : : }
|