1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
3 * Copyright (C) 2020 Marvell International Ltd.
6 #include <rte_event_eth_tx_adapter.h>
10 #include "event_helper.h"
12 #include "ipsec-secgw.h"
13 #include "ipsec_worker.h"
15 static inline enum pkt_type
16 process_ipsec_get_pkt_type(struct rte_mbuf *pkt, uint8_t **nlp)
18 struct rte_ether_hdr *eth;
20 eth = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
21 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
22 *nlp = RTE_PTR_ADD(eth, RTE_ETHER_HDR_LEN +
23 offsetof(struct ip, ip_p));
24 if (**nlp == IPPROTO_ESP)
25 return PKT_TYPE_IPSEC_IPV4;
27 return PKT_TYPE_PLAIN_IPV4;
28 } else if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
29 *nlp = RTE_PTR_ADD(eth, RTE_ETHER_HDR_LEN +
30 offsetof(struct ip6_hdr, ip6_nxt));
31 if (**nlp == IPPROTO_ESP)
32 return PKT_TYPE_IPSEC_IPV6;
34 return PKT_TYPE_PLAIN_IPV6;
37 /* Unknown/Unsupported type */
38 return PKT_TYPE_INVALID;
42 update_mac_addrs(struct rte_mbuf *pkt, uint16_t portid)
44 struct rte_ether_hdr *ethhdr;
46 ethhdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
47 memcpy(ðhdr->s_addr, ðaddr_tbl[portid].src, RTE_ETHER_ADDR_LEN);
48 memcpy(ðhdr->d_addr, ðaddr_tbl[portid].dst, RTE_ETHER_ADDR_LEN);
52 ipsec_event_pre_forward(struct rte_mbuf *m, unsigned int port_id)
54 /* Save the destination port in the mbuf */
57 /* Save eth queue for Tx */
58 rte_event_eth_tx_adapter_txq_set(m, 0);
62 prepare_out_sessions_tbl(struct sa_ctx *sa_out,
63 struct rte_security_session **sess_tbl, uint16_t size)
65 struct rte_ipsec_session *pri_sess;
72 for (i = 0; i < sa_out->nb_sa; i++) {
78 pri_sess = ipsec_get_primary_session(sa);
83 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
85 RTE_LOG(ERR, IPSEC, "Invalid session type %d\n",
90 if (sa->portid >= size) {
92 "Port id >= than table size %d, %d\n",
97 /* Use only first inline session found for a given port */
98 if (sess_tbl[sa->portid])
100 sess_tbl[sa->portid] = pri_sess->security.ses;
105 check_sp(struct sp_ctx *sp, const uint8_t *nlp, uint32_t *sa_idx)
109 if (unlikely(sp == NULL))
112 rte_acl_classify((struct rte_acl_ctx *)sp, &nlp, &res, 1,
113 DEFAULT_MAX_CATEGORIES);
115 if (unlikely(res == DISCARD))
117 else if (res == BYPASS) {
126 static inline uint16_t
127 route4_pkt(struct rte_mbuf *pkt, struct rt_ctx *rt_ctx)
134 offset = RTE_ETHER_HDR_LEN + offsetof(struct ip, ip_dst);
135 dst_ip = *rte_pktmbuf_mtod_offset(pkt, uint32_t *, offset);
136 dst_ip = rte_be_to_cpu_32(dst_ip);
138 ret = rte_lpm_lookup((struct rte_lpm *)rt_ctx, dst_ip, &hop);
146 return RTE_MAX_ETHPORTS;
149 /* TODO: To be tested */
150 static inline uint16_t
151 route6_pkt(struct rte_mbuf *pkt, struct rt_ctx *rt_ctx)
159 offset = RTE_ETHER_HDR_LEN + offsetof(struct ip6_hdr, ip6_dst);
160 ip6_dst = rte_pktmbuf_mtod_offset(pkt, uint8_t *, offset);
161 memcpy(&dst_ip[0], ip6_dst, 16);
163 ret = rte_lpm6_lookup((struct rte_lpm6 *)rt_ctx, dst_ip, &hop);
171 return RTE_MAX_ETHPORTS;
174 static inline uint16_t
175 get_route(struct rte_mbuf *pkt, struct route_table *rt, enum pkt_type type)
177 if (type == PKT_TYPE_PLAIN_IPV4 || type == PKT_TYPE_IPSEC_IPV4)
178 return route4_pkt(pkt, rt->rt4_ctx);
179 else if (type == PKT_TYPE_PLAIN_IPV6 || type == PKT_TYPE_IPSEC_IPV6)
180 return route6_pkt(pkt, rt->rt6_ctx);
182 return RTE_MAX_ETHPORTS;
186 process_ipsec_ev_inbound(struct ipsec_ctx *ctx, struct route_table *rt,
187 struct rte_event *ev)
189 struct ipsec_sa *sa = NULL;
190 struct rte_mbuf *pkt;
191 uint16_t port_id = 0;
196 /* Get pkt from event */
199 /* Check the packet type */
200 type = process_ipsec_get_pkt_type(pkt, &nlp);
203 case PKT_TYPE_PLAIN_IPV4:
204 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
205 if (unlikely(pkt->ol_flags &
206 PKT_RX_SEC_OFFLOAD_FAILED)) {
208 "Inbound security offload failed\n");
209 goto drop_pkt_and_exit;
211 sa = *(struct ipsec_sa **)rte_security_dynfield(pkt);
214 /* Check if we have a match */
215 if (check_sp(ctx->sp4_ctx, nlp, &sa_idx) == 0) {
217 goto drop_pkt_and_exit;
221 case PKT_TYPE_PLAIN_IPV6:
222 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
223 if (unlikely(pkt->ol_flags &
224 PKT_RX_SEC_OFFLOAD_FAILED)) {
226 "Inbound security offload failed\n");
227 goto drop_pkt_and_exit;
229 sa = *(struct ipsec_sa **)rte_security_dynfield(pkt);
232 /* Check if we have a match */
233 if (check_sp(ctx->sp6_ctx, nlp, &sa_idx) == 0) {
235 goto drop_pkt_and_exit;
240 RTE_LOG(ERR, IPSEC, "Unsupported packet type = %d\n", type);
241 goto drop_pkt_and_exit;
244 /* Check if the packet has to be bypassed */
245 if (sa_idx == BYPASS)
246 goto route_and_send_pkt;
248 /* Validate sa_idx */
249 if (sa_idx >= ctx->sa_ctx->nb_sa)
250 goto drop_pkt_and_exit;
252 /* Else the packet has to be protected with SA */
254 /* If the packet was IPsec processed, then SA pointer should be set */
256 goto drop_pkt_and_exit;
258 /* SPI on the packet should match with the one in SA */
259 if (unlikely(sa->spi != ctx->sa_ctx->sa[sa_idx].spi))
260 goto drop_pkt_and_exit;
263 port_id = get_route(pkt, rt, type);
264 if (unlikely(port_id == RTE_MAX_ETHPORTS)) {
266 goto drop_pkt_and_exit;
268 /* else, we have a matching route */
270 /* Update mac addresses */
271 update_mac_addrs(pkt, port_id);
273 /* Update the event with the dest port */
274 ipsec_event_pre_forward(pkt, port_id);
275 return PKT_FORWARDED;
278 RTE_LOG(ERR, IPSEC, "Inbound packet dropped\n");
279 rte_pktmbuf_free(pkt);
285 process_ipsec_ev_outbound(struct ipsec_ctx *ctx, struct route_table *rt,
286 struct rte_event *ev)
288 struct rte_ipsec_session *sess;
289 struct sa_ctx *sa_ctx;
290 struct rte_mbuf *pkt;
291 uint16_t port_id = 0;
297 /* Get pkt from event */
300 /* Check the packet type */
301 type = process_ipsec_get_pkt_type(pkt, &nlp);
304 case PKT_TYPE_PLAIN_IPV4:
305 /* Check if we have a match */
306 if (check_sp(ctx->sp4_ctx, nlp, &sa_idx) == 0) {
308 goto drop_pkt_and_exit;
311 case PKT_TYPE_PLAIN_IPV6:
312 /* Check if we have a match */
313 if (check_sp(ctx->sp6_ctx, nlp, &sa_idx) == 0) {
315 goto drop_pkt_and_exit;
320 * Only plain IPv4 & IPv6 packets are allowed
321 * on protected port. Drop the rest.
323 RTE_LOG(ERR, IPSEC, "Unsupported packet type = %d\n", type);
324 goto drop_pkt_and_exit;
327 /* Check if the packet has to be bypassed */
328 if (sa_idx == BYPASS) {
329 port_id = get_route(pkt, rt, type);
330 if (unlikely(port_id == RTE_MAX_ETHPORTS)) {
332 goto drop_pkt_and_exit;
334 /* else, we have a matching route */
338 /* Validate sa_idx */
339 if (sa_idx >= ctx->sa_ctx->nb_sa)
340 goto drop_pkt_and_exit;
342 /* Else the packet has to be protected */
345 sa_ctx = ctx->sa_ctx;
348 sa = &(sa_ctx->sa[sa_idx]);
350 /* Get IPsec session */
351 sess = ipsec_get_primary_session(sa);
353 /* Allow only inline protocol for now */
354 if (sess->type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
355 RTE_LOG(ERR, IPSEC, "SA type not supported\n");
356 goto drop_pkt_and_exit;
359 if (sess->security.ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
360 *(struct rte_security_session **)rte_security_dynfield(pkt) =
363 /* Mark the packet for Tx security offload */
364 pkt->ol_flags |= PKT_TX_SEC_OFFLOAD;
366 /* Get the port to which this pkt need to be submitted */
367 port_id = sa->portid;
370 /* Update mac addresses */
371 update_mac_addrs(pkt, port_id);
373 /* Update the event with the dest port */
374 ipsec_event_pre_forward(pkt, port_id);
375 return PKT_FORWARDED;
378 RTE_LOG(ERR, IPSEC, "Outbound packet dropped\n");
379 rte_pktmbuf_free(pkt);
385 * Event mode exposes various operating modes depending on the
386 * capabilities of the event device and the operating mode
390 /* Workers registered */
391 #define IPSEC_EVENTMODE_WORKERS 2
395 * Operating parameters : non-burst - Tx internal port - driver mode
398 ipsec_wrkr_non_burst_int_port_drv_mode(struct eh_event_link_info *links,
401 struct rte_security_session *sess_tbl[RTE_MAX_ETHPORTS] = { NULL };
402 unsigned int nb_rx = 0;
403 struct rte_mbuf *pkt;
409 /* Check if we have links registered for this lcore */
411 /* No links registered - exit */
416 lcore_id = rte_lcore_id();
419 socket_id = rte_lcore_to_socket_id(lcore_id);
422 * Prepare security sessions table. In outbound driver mode
423 * we always use first session configured for a given port
425 prepare_out_sessions_tbl(socket_ctx[socket_id].sa_out, sess_tbl,
429 "Launching event mode worker (non-burst - Tx internal port - "
430 "driver mode) on lcore %d\n", lcore_id);
432 /* We have valid links */
434 /* Check if it's single link */
437 "Multiple links not supported. Using first link\n");
440 RTE_LOG(INFO, IPSEC, " -- lcoreid=%u event_port_id=%u\n", lcore_id,
441 links[0].event_port_id);
442 while (!force_quit) {
443 /* Read packet from event queues */
444 nb_rx = rte_event_dequeue_burst(links[0].eventdev_id,
445 links[0].event_port_id,
448 0 /* timeout_ticks */);
456 rte_prefetch0(rte_pktmbuf_mtod(pkt, void *));
459 ipsec_event_pre_forward(pkt, port_id);
461 if (!is_unprotected_port(port_id)) {
463 if (unlikely(!sess_tbl[port_id])) {
464 rte_pktmbuf_free(pkt);
468 /* Save security session */
469 if (rte_security_dynfield_is_registered())
470 *(struct rte_security_session **)
471 rte_security_dynfield(pkt) =
474 /* Mark the packet for Tx security offload */
475 pkt->ol_flags |= PKT_TX_SEC_OFFLOAD;
479 * Since tx internal port is available, events can be
480 * directly enqueued to the adapter and it would be
481 * internally submitted to the eth device.
483 rte_event_eth_tx_adapter_enqueue(links[0].eventdev_id,
484 links[0].event_port_id,
493 * Operating parameters : non-burst - Tx internal port - app mode
496 ipsec_wrkr_non_burst_int_port_app_mode(struct eh_event_link_info *links,
499 struct lcore_conf_ev_tx_int_port_wrkr lconf;
500 unsigned int nb_rx = 0;
506 /* Check if we have links registered for this lcore */
508 /* No links registered - exit */
512 /* We have valid links */
515 lcore_id = rte_lcore_id();
518 socket_id = rte_lcore_to_socket_id(lcore_id);
520 /* Save routing table */
521 lconf.rt.rt4_ctx = socket_ctx[socket_id].rt_ip4;
522 lconf.rt.rt6_ctx = socket_ctx[socket_id].rt_ip6;
523 lconf.inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
524 lconf.inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
525 lconf.inbound.sa_ctx = socket_ctx[socket_id].sa_in;
526 lconf.inbound.session_pool = socket_ctx[socket_id].session_pool;
527 lconf.inbound.session_priv_pool =
528 socket_ctx[socket_id].session_priv_pool;
529 lconf.outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
530 lconf.outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
531 lconf.outbound.sa_ctx = socket_ctx[socket_id].sa_out;
532 lconf.outbound.session_pool = socket_ctx[socket_id].session_pool;
533 lconf.outbound.session_priv_pool =
534 socket_ctx[socket_id].session_priv_pool;
537 "Launching event mode worker (non-burst - Tx internal port - "
538 "app mode) on lcore %d\n", lcore_id);
540 /* Check if it's single link */
543 "Multiple links not supported. Using first link\n");
546 RTE_LOG(INFO, IPSEC, " -- lcoreid=%u event_port_id=%u\n", lcore_id,
547 links[0].event_port_id);
549 while (!force_quit) {
550 /* Read packet from event queues */
551 nb_rx = rte_event_dequeue_burst(links[0].eventdev_id,
552 links[0].event_port_id,
555 0 /* timeout_ticks */);
560 if (unlikely(ev.event_type != RTE_EVENT_TYPE_ETHDEV)) {
561 RTE_LOG(ERR, IPSEC, "Invalid event type %u",
567 if (is_unprotected_port(ev.mbuf->port))
568 ret = process_ipsec_ev_inbound(&lconf.inbound,
571 ret = process_ipsec_ev_outbound(&lconf.outbound,
574 /* The pkt has been dropped */
578 * Since tx internal port is available, events can be
579 * directly enqueued to the adapter and it would be
580 * internally submitted to the eth device.
582 rte_event_eth_tx_adapter_enqueue(links[0].eventdev_id,
583 links[0].event_port_id,
591 ipsec_eventmode_populate_wrkr_params(struct eh_app_worker_params *wrkrs)
593 struct eh_app_worker_params *wrkr;
594 uint8_t nb_wrkr_param = 0;
599 /* Non-burst - Tx internal port - driver mode */
600 wrkr->cap.burst = EH_RX_TYPE_NON_BURST;
601 wrkr->cap.tx_internal_port = EH_TX_TYPE_INTERNAL_PORT;
602 wrkr->cap.ipsec_mode = EH_IPSEC_MODE_TYPE_DRIVER;
603 wrkr->worker_thread = ipsec_wrkr_non_burst_int_port_drv_mode;
607 /* Non-burst - Tx internal port - app mode */
608 wrkr->cap.burst = EH_RX_TYPE_NON_BURST;
609 wrkr->cap.tx_internal_port = EH_TX_TYPE_INTERNAL_PORT;
610 wrkr->cap.ipsec_mode = EH_IPSEC_MODE_TYPE_APP;
611 wrkr->worker_thread = ipsec_wrkr_non_burst_int_port_app_mode;
614 return nb_wrkr_param;
618 ipsec_eventmode_worker(struct eh_conf *conf)
620 struct eh_app_worker_params ipsec_wrkr[IPSEC_EVENTMODE_WORKERS] = {
622 uint8_t nb_wrkr_param;
624 /* Populate l2fwd_wrkr params */
625 nb_wrkr_param = ipsec_eventmode_populate_wrkr_params(ipsec_wrkr);
628 * Launch correct worker after checking
629 * the event device's capabilities.
631 eh_launch_worker(conf, ipsec_wrkr, nb_wrkr_param);
634 int ipsec_launch_one_lcore(void *args)
636 struct eh_conf *conf;
638 conf = (struct eh_conf *)args;
640 if (conf->mode == EH_PKT_TRANSFER_MODE_POLL) {
641 /* Run in poll mode */
642 ipsec_poll_mode_worker();
643 } else if (conf->mode == EH_PKT_TRANSFER_MODE_EVENT) {
644 /* Run in event mode */
645 ipsec_eventmode_worker(conf);