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;
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;
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 pkt->userdata = sess->security.ses;
362 /* Mark the packet for Tx security offload */
363 pkt->ol_flags |= PKT_TX_SEC_OFFLOAD;
365 /* Get the port to which this pkt need to be submitted */
366 port_id = sa->portid;
369 /* Update mac addresses */
370 update_mac_addrs(pkt, port_id);
372 /* Update the event with the dest port */
373 ipsec_event_pre_forward(pkt, port_id);
374 return PKT_FORWARDED;
377 RTE_LOG(ERR, IPSEC, "Outbound packet dropped\n");
378 rte_pktmbuf_free(pkt);
384 * Event mode exposes various operating modes depending on the
385 * capabilities of the event device and the operating mode
389 /* Workers registered */
390 #define IPSEC_EVENTMODE_WORKERS 2
394 * Operating parameters : non-burst - Tx internal port - driver mode
397 ipsec_wrkr_non_burst_int_port_drv_mode(struct eh_event_link_info *links,
400 struct rte_security_session *sess_tbl[RTE_MAX_ETHPORTS] = { NULL };
401 unsigned int nb_rx = 0;
402 struct rte_mbuf *pkt;
408 /* Check if we have links registered for this lcore */
410 /* No links registered - exit */
415 lcore_id = rte_lcore_id();
418 socket_id = rte_lcore_to_socket_id(lcore_id);
421 * Prepare security sessions table. In outbound driver mode
422 * we always use first session configured for a given port
424 prepare_out_sessions_tbl(socket_ctx[socket_id].sa_out, sess_tbl,
428 "Launching event mode worker (non-burst - Tx internal port - "
429 "driver mode) on lcore %d\n", lcore_id);
431 /* We have valid links */
433 /* Check if it's single link */
436 "Multiple links not supported. Using first link\n");
439 RTE_LOG(INFO, IPSEC, " -- lcoreid=%u event_port_id=%u\n", lcore_id,
440 links[0].event_port_id);
441 while (!force_quit) {
442 /* Read packet from event queues */
443 nb_rx = rte_event_dequeue_burst(links[0].eventdev_id,
444 links[0].event_port_id,
447 0 /* timeout_ticks */);
455 rte_prefetch0(rte_pktmbuf_mtod(pkt, void *));
458 ipsec_event_pre_forward(pkt, port_id);
460 if (!is_unprotected_port(port_id)) {
462 if (unlikely(!sess_tbl[port_id])) {
463 rte_pktmbuf_free(pkt);
467 /* Save security session */
468 pkt->userdata = sess_tbl[port_id];
470 /* Mark the packet for Tx security offload */
471 pkt->ol_flags |= PKT_TX_SEC_OFFLOAD;
475 * Since tx internal port is available, events can be
476 * directly enqueued to the adapter and it would be
477 * internally submitted to the eth device.
479 rte_event_eth_tx_adapter_enqueue(links[0].eventdev_id,
480 links[0].event_port_id,
489 * Operating parameters : non-burst - Tx internal port - app mode
492 ipsec_wrkr_non_burst_int_port_app_mode(struct eh_event_link_info *links,
495 struct lcore_conf_ev_tx_int_port_wrkr lconf;
496 unsigned int nb_rx = 0;
502 /* Check if we have links registered for this lcore */
504 /* No links registered - exit */
508 /* We have valid links */
511 lcore_id = rte_lcore_id();
514 socket_id = rte_lcore_to_socket_id(lcore_id);
516 /* Save routing table */
517 lconf.rt.rt4_ctx = socket_ctx[socket_id].rt_ip4;
518 lconf.rt.rt6_ctx = socket_ctx[socket_id].rt_ip6;
519 lconf.inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
520 lconf.inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
521 lconf.inbound.sa_ctx = socket_ctx[socket_id].sa_in;
522 lconf.inbound.session_pool = socket_ctx[socket_id].session_pool;
523 lconf.inbound.session_priv_pool =
524 socket_ctx[socket_id].session_priv_pool;
525 lconf.outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
526 lconf.outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
527 lconf.outbound.sa_ctx = socket_ctx[socket_id].sa_out;
528 lconf.outbound.session_pool = socket_ctx[socket_id].session_pool;
529 lconf.outbound.session_priv_pool =
530 socket_ctx[socket_id].session_priv_pool;
533 "Launching event mode worker (non-burst - Tx internal port - "
534 "app mode) on lcore %d\n", lcore_id);
536 /* Check if it's single link */
539 "Multiple links not supported. Using first link\n");
542 RTE_LOG(INFO, IPSEC, " -- lcoreid=%u event_port_id=%u\n", lcore_id,
543 links[0].event_port_id);
545 while (!force_quit) {
546 /* Read packet from event queues */
547 nb_rx = rte_event_dequeue_burst(links[0].eventdev_id,
548 links[0].event_port_id,
551 0 /* timeout_ticks */);
556 if (unlikely(ev.event_type != RTE_EVENT_TYPE_ETHDEV)) {
557 RTE_LOG(ERR, IPSEC, "Invalid event type %u",
563 if (is_unprotected_port(ev.mbuf->port))
564 ret = process_ipsec_ev_inbound(&lconf.inbound,
567 ret = process_ipsec_ev_outbound(&lconf.outbound,
570 /* The pkt has been dropped */
574 * Since tx internal port is available, events can be
575 * directly enqueued to the adapter and it would be
576 * internally submitted to the eth device.
578 rte_event_eth_tx_adapter_enqueue(links[0].eventdev_id,
579 links[0].event_port_id,
587 ipsec_eventmode_populate_wrkr_params(struct eh_app_worker_params *wrkrs)
589 struct eh_app_worker_params *wrkr;
590 uint8_t nb_wrkr_param = 0;
595 /* Non-burst - Tx internal port - driver mode */
596 wrkr->cap.burst = EH_RX_TYPE_NON_BURST;
597 wrkr->cap.tx_internal_port = EH_TX_TYPE_INTERNAL_PORT;
598 wrkr->cap.ipsec_mode = EH_IPSEC_MODE_TYPE_DRIVER;
599 wrkr->worker_thread = ipsec_wrkr_non_burst_int_port_drv_mode;
603 /* Non-burst - Tx internal port - app mode */
604 wrkr->cap.burst = EH_RX_TYPE_NON_BURST;
605 wrkr->cap.tx_internal_port = EH_TX_TYPE_INTERNAL_PORT;
606 wrkr->cap.ipsec_mode = EH_IPSEC_MODE_TYPE_APP;
607 wrkr->worker_thread = ipsec_wrkr_non_burst_int_port_app_mode;
610 return nb_wrkr_param;
614 ipsec_eventmode_worker(struct eh_conf *conf)
616 struct eh_app_worker_params ipsec_wrkr[IPSEC_EVENTMODE_WORKERS] = {
618 uint8_t nb_wrkr_param;
620 /* Populate l2fwd_wrkr params */
621 nb_wrkr_param = ipsec_eventmode_populate_wrkr_params(ipsec_wrkr);
624 * Launch correct worker after checking
625 * the event device's capabilities.
627 eh_launch_worker(conf, ipsec_wrkr, nb_wrkr_param);
630 int ipsec_launch_one_lcore(void *args)
632 struct eh_conf *conf;
634 conf = (struct eh_conf *)args;
636 if (conf->mode == EH_PKT_TRANSFER_MODE_POLL) {
637 /* Run in poll mode */
638 ipsec_poll_mode_worker();
639 } else if (conf->mode == EH_PKT_TRANSFER_MODE_EVENT) {
640 /* Run in event mode */
641 ipsec_eventmode_worker(conf);