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 == 0)) {
122 else if (res == BYPASS) {
131 static inline uint16_t
132 route4_pkt(struct rte_mbuf *pkt, struct rt_ctx *rt_ctx)
139 offset = RTE_ETHER_HDR_LEN + offsetof(struct ip, ip_dst);
140 dst_ip = *rte_pktmbuf_mtod_offset(pkt, uint32_t *, offset);
141 dst_ip = rte_be_to_cpu_32(dst_ip);
143 ret = rte_lpm_lookup((struct rte_lpm *)rt_ctx, dst_ip, &hop);
151 return RTE_MAX_ETHPORTS;
154 /* TODO: To be tested */
155 static inline uint16_t
156 route6_pkt(struct rte_mbuf *pkt, struct rt_ctx *rt_ctx)
164 offset = RTE_ETHER_HDR_LEN + offsetof(struct ip6_hdr, ip6_dst);
165 ip6_dst = rte_pktmbuf_mtod_offset(pkt, uint8_t *, offset);
166 memcpy(&dst_ip[0], ip6_dst, 16);
168 ret = rte_lpm6_lookup((struct rte_lpm6 *)rt_ctx, dst_ip, &hop);
176 return RTE_MAX_ETHPORTS;
179 static inline uint16_t
180 get_route(struct rte_mbuf *pkt, struct route_table *rt, enum pkt_type type)
182 if (type == PKT_TYPE_PLAIN_IPV4 || type == PKT_TYPE_IPSEC_IPV4)
183 return route4_pkt(pkt, rt->rt4_ctx);
184 else if (type == PKT_TYPE_PLAIN_IPV6 || type == PKT_TYPE_IPSEC_IPV6)
185 return route6_pkt(pkt, rt->rt6_ctx);
187 return RTE_MAX_ETHPORTS;
191 process_ipsec_ev_inbound(struct ipsec_ctx *ctx, struct route_table *rt,
192 struct rte_event *ev)
194 struct ipsec_sa *sa = NULL;
195 struct rte_mbuf *pkt;
196 uint16_t port_id = 0;
201 /* Get pkt from event */
204 /* Check the packet type */
205 type = process_ipsec_get_pkt_type(pkt, &nlp);
208 case PKT_TYPE_PLAIN_IPV4:
209 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
210 if (unlikely(pkt->ol_flags &
211 PKT_RX_SEC_OFFLOAD_FAILED)) {
213 "Inbound security offload failed\n");
214 goto drop_pkt_and_exit;
219 /* Check if we have a match */
220 if (check_sp(ctx->sp4_ctx, nlp, &sa_idx) == 0) {
222 goto drop_pkt_and_exit;
226 case PKT_TYPE_PLAIN_IPV6:
227 if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
228 if (unlikely(pkt->ol_flags &
229 PKT_RX_SEC_OFFLOAD_FAILED)) {
231 "Inbound security offload failed\n");
232 goto drop_pkt_and_exit;
237 /* Check if we have a match */
238 if (check_sp(ctx->sp6_ctx, nlp, &sa_idx) == 0) {
240 goto drop_pkt_and_exit;
245 RTE_LOG(ERR, IPSEC, "Unsupported packet type = %d\n", type);
246 goto drop_pkt_and_exit;
249 /* Check if the packet has to be bypassed */
250 if (sa_idx == BYPASS)
251 goto route_and_send_pkt;
253 /* Validate sa_idx */
254 if (sa_idx >= ctx->sa_ctx->nb_sa)
255 goto drop_pkt_and_exit;
257 /* Else the packet has to be protected with SA */
259 /* If the packet was IPsec processed, then SA pointer should be set */
261 goto drop_pkt_and_exit;
263 /* SPI on the packet should match with the one in SA */
264 if (unlikely(sa->spi != ctx->sa_ctx->sa[sa_idx].spi))
265 goto drop_pkt_and_exit;
268 port_id = get_route(pkt, rt, type);
269 if (unlikely(port_id == RTE_MAX_ETHPORTS)) {
271 goto drop_pkt_and_exit;
273 /* else, we have a matching route */
275 /* Update mac addresses */
276 update_mac_addrs(pkt, port_id);
278 /* Update the event with the dest port */
279 ipsec_event_pre_forward(pkt, port_id);
280 return PKT_FORWARDED;
283 RTE_LOG(ERR, IPSEC, "Inbound packet dropped\n");
284 rte_pktmbuf_free(pkt);
290 process_ipsec_ev_outbound(struct ipsec_ctx *ctx, struct route_table *rt,
291 struct rte_event *ev)
293 struct rte_ipsec_session *sess;
294 struct sa_ctx *sa_ctx;
295 struct rte_mbuf *pkt;
296 uint16_t port_id = 0;
302 /* Get pkt from event */
305 /* Check the packet type */
306 type = process_ipsec_get_pkt_type(pkt, &nlp);
309 case PKT_TYPE_PLAIN_IPV4:
310 /* Check if we have a match */
311 if (check_sp(ctx->sp4_ctx, nlp, &sa_idx) == 0) {
313 goto drop_pkt_and_exit;
316 case PKT_TYPE_PLAIN_IPV6:
317 /* Check if we have a match */
318 if (check_sp(ctx->sp6_ctx, nlp, &sa_idx) == 0) {
320 goto drop_pkt_and_exit;
325 * Only plain IPv4 & IPv6 packets are allowed
326 * on protected port. Drop the rest.
328 RTE_LOG(ERR, IPSEC, "Unsupported packet type = %d\n", type);
329 goto drop_pkt_and_exit;
332 /* Check if the packet has to be bypassed */
333 if (sa_idx == BYPASS) {
334 port_id = get_route(pkt, rt, type);
335 if (unlikely(port_id == RTE_MAX_ETHPORTS)) {
337 goto drop_pkt_and_exit;
339 /* else, we have a matching route */
343 /* Validate sa_idx */
344 if (sa_idx >= ctx->sa_ctx->nb_sa)
345 goto drop_pkt_and_exit;
347 /* Else the packet has to be protected */
350 sa_ctx = ctx->sa_ctx;
353 sa = &(sa_ctx->sa[sa_idx]);
355 /* Get IPsec session */
356 sess = ipsec_get_primary_session(sa);
358 /* Allow only inline protocol for now */
359 if (sess->type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
360 RTE_LOG(ERR, IPSEC, "SA type not supported\n");
361 goto drop_pkt_and_exit;
364 if (sess->security.ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
365 pkt->userdata = sess->security.ses;
367 /* Mark the packet for Tx security offload */
368 pkt->ol_flags |= PKT_TX_SEC_OFFLOAD;
370 /* Get the port to which this pkt need to be submitted */
371 port_id = sa->portid;
374 /* Update mac addresses */
375 update_mac_addrs(pkt, port_id);
377 /* Update the event with the dest port */
378 ipsec_event_pre_forward(pkt, port_id);
379 return PKT_FORWARDED;
382 RTE_LOG(ERR, IPSEC, "Outbound packet dropped\n");
383 rte_pktmbuf_free(pkt);
389 * Event mode exposes various operating modes depending on the
390 * capabilities of the event device and the operating mode
394 /* Workers registered */
395 #define IPSEC_EVENTMODE_WORKERS 2
399 * Operating parameters : non-burst - Tx internal port - driver mode
402 ipsec_wrkr_non_burst_int_port_drv_mode(struct eh_event_link_info *links,
405 struct rte_security_session *sess_tbl[RTE_MAX_ETHPORTS] = { NULL };
406 unsigned int nb_rx = 0;
407 struct rte_mbuf *pkt;
413 /* Check if we have links registered for this lcore */
415 /* No links registered - exit */
420 lcore_id = rte_lcore_id();
423 socket_id = rte_lcore_to_socket_id(lcore_id);
426 * Prepare security sessions table. In outbound driver mode
427 * we always use first session configured for a given port
429 prepare_out_sessions_tbl(socket_ctx[socket_id].sa_out, sess_tbl,
433 "Launching event mode worker (non-burst - Tx internal port - "
434 "driver mode) on lcore %d\n", lcore_id);
436 /* We have valid links */
438 /* Check if it's single link */
441 "Multiple links not supported. Using first link\n");
444 RTE_LOG(INFO, IPSEC, " -- lcoreid=%u event_port_id=%u\n", lcore_id,
445 links[0].event_port_id);
446 while (!force_quit) {
447 /* Read packet from event queues */
448 nb_rx = rte_event_dequeue_burst(links[0].eventdev_id,
449 links[0].event_port_id,
452 0 /* timeout_ticks */);
460 rte_prefetch0(rte_pktmbuf_mtod(pkt, void *));
463 ipsec_event_pre_forward(pkt, port_id);
465 if (!is_unprotected_port(port_id)) {
467 if (unlikely(!sess_tbl[port_id])) {
468 rte_pktmbuf_free(pkt);
472 /* Save security session */
473 pkt->userdata = sess_tbl[port_id];
475 /* Mark the packet for Tx security offload */
476 pkt->ol_flags |= PKT_TX_SEC_OFFLOAD;
480 * Since tx internal port is available, events can be
481 * directly enqueued to the adapter and it would be
482 * internally submitted to the eth device.
484 rte_event_eth_tx_adapter_enqueue(links[0].eventdev_id,
485 links[0].event_port_id,
494 * Operating parameters : non-burst - Tx internal port - app mode
497 ipsec_wrkr_non_burst_int_port_app_mode(struct eh_event_link_info *links,
500 struct lcore_conf_ev_tx_int_port_wrkr lconf;
501 unsigned int nb_rx = 0;
507 /* Check if we have links registered for this lcore */
509 /* No links registered - exit */
513 /* We have valid links */
516 lcore_id = rte_lcore_id();
519 socket_id = rte_lcore_to_socket_id(lcore_id);
521 /* Save routing table */
522 lconf.rt.rt4_ctx = socket_ctx[socket_id].rt_ip4;
523 lconf.rt.rt6_ctx = socket_ctx[socket_id].rt_ip6;
524 lconf.inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
525 lconf.inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
526 lconf.inbound.sa_ctx = socket_ctx[socket_id].sa_in;
527 lconf.inbound.session_pool = socket_ctx[socket_id].session_pool;
528 lconf.inbound.session_priv_pool =
529 socket_ctx[socket_id].session_priv_pool;
530 lconf.outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
531 lconf.outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
532 lconf.outbound.sa_ctx = socket_ctx[socket_id].sa_out;
533 lconf.outbound.session_pool = socket_ctx[socket_id].session_pool;
534 lconf.outbound.session_priv_pool =
535 socket_ctx[socket_id].session_priv_pool;
538 "Launching event mode worker (non-burst - Tx internal port - "
539 "app mode) on lcore %d\n", lcore_id);
541 /* Check if it's single link */
544 "Multiple links not supported. Using first link\n");
547 RTE_LOG(INFO, IPSEC, " -- lcoreid=%u event_port_id=%u\n", lcore_id,
548 links[0].event_port_id);
550 while (!force_quit) {
551 /* Read packet from event queues */
552 nb_rx = rte_event_dequeue_burst(links[0].eventdev_id,
553 links[0].event_port_id,
556 0 /* timeout_ticks */);
561 if (unlikely(ev.event_type != RTE_EVENT_TYPE_ETHDEV)) {
562 RTE_LOG(ERR, IPSEC, "Invalid event type %u",
568 if (is_unprotected_port(ev.mbuf->port))
569 ret = process_ipsec_ev_inbound(&lconf.inbound,
572 ret = process_ipsec_ev_outbound(&lconf.outbound,
575 /* The pkt has been dropped */
579 * Since tx internal port is available, events can be
580 * directly enqueued to the adapter and it would be
581 * internally submitted to the eth device.
583 rte_event_eth_tx_adapter_enqueue(links[0].eventdev_id,
584 links[0].event_port_id,
592 ipsec_eventmode_populate_wrkr_params(struct eh_app_worker_params *wrkrs)
594 struct eh_app_worker_params *wrkr;
595 uint8_t nb_wrkr_param = 0;
600 /* Non-burst - Tx internal port - driver mode */
601 wrkr->cap.burst = EH_RX_TYPE_NON_BURST;
602 wrkr->cap.tx_internal_port = EH_TX_TYPE_INTERNAL_PORT;
603 wrkr->cap.ipsec_mode = EH_IPSEC_MODE_TYPE_DRIVER;
604 wrkr->worker_thread = ipsec_wrkr_non_burst_int_port_drv_mode;
608 /* Non-burst - Tx internal port - app mode */
609 wrkr->cap.burst = EH_RX_TYPE_NON_BURST;
610 wrkr->cap.tx_internal_port = EH_TX_TYPE_INTERNAL_PORT;
611 wrkr->cap.ipsec_mode = EH_IPSEC_MODE_TYPE_APP;
612 wrkr->worker_thread = ipsec_wrkr_non_burst_int_port_app_mode;
615 return nb_wrkr_param;
619 ipsec_eventmode_worker(struct eh_conf *conf)
621 struct eh_app_worker_params ipsec_wrkr[IPSEC_EVENTMODE_WORKERS] = {
623 uint8_t nb_wrkr_param;
625 /* Populate l2fwd_wrkr params */
626 nb_wrkr_param = ipsec_eventmode_populate_wrkr_params(ipsec_wrkr);
629 * Launch correct worker after checking
630 * the event device's capabilities.
632 eh_launch_worker(conf, ipsec_wrkr, nb_wrkr_param);
635 int ipsec_launch_one_lcore(void *args)
637 struct eh_conf *conf;
639 conf = (struct eh_conf *)args;
641 if (conf->mode == EH_PKT_TRANSFER_MODE_POLL) {
642 /* Run in poll mode */
643 ipsec_poll_mode_worker();
644 } else if (conf->mode == EH_PKT_TRANSFER_MODE_EVENT) {
645 /* Run in event mode */
646 ipsec_eventmode_worker(conf);