net: add rte prefix to IP structure
[dpdk.git] / examples / l3fwd / l3fwd_em.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #ifndef __L3FWD_EM_H__
6 #define __L3FWD_EM_H__
7
8 static __rte_always_inline void
9 l3fwd_em_simple_forward(struct rte_mbuf *m, uint16_t portid,
10                 struct lcore_conf *qconf)
11 {
12         struct rte_ether_hdr *eth_hdr;
13         struct rte_ipv4_hdr *ipv4_hdr;
14         uint16_t dst_port;
15         uint32_t tcp_or_udp;
16         uint32_t l3_ptypes;
17
18         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
19         tcp_or_udp = m->packet_type & (RTE_PTYPE_L4_TCP | RTE_PTYPE_L4_UDP);
20         l3_ptypes = m->packet_type & RTE_PTYPE_L3_MASK;
21
22         if (tcp_or_udp && (l3_ptypes == RTE_PTYPE_L3_IPV4)) {
23                 /* Handle IPv4 headers.*/
24                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
25                                                 sizeof(struct rte_ether_hdr));
26
27 #ifdef DO_RFC_1812_CHECKS
28                 /* Check to make sure the packet is valid (RFC1812) */
29                 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
30                         rte_pktmbuf_free(m);
31                         return;
32                 }
33 #endif
34                 dst_port = em_get_ipv4_dst_port(ipv4_hdr, portid,
35                                                 qconf->ipv4_lookup_struct);
36
37                 if (dst_port >= RTE_MAX_ETHPORTS ||
38                         (enabled_port_mask & 1 << dst_port) == 0)
39                         dst_port = portid;
40
41 #ifdef DO_RFC_1812_CHECKS
42                 /* Update time to live and header checksum */
43                 --(ipv4_hdr->time_to_live);
44                 ++(ipv4_hdr->hdr_checksum);
45 #endif
46                 /* dst addr */
47                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
48
49                 /* src addr */
50                 rte_ether_addr_copy(&ports_eth_addr[dst_port],
51                                 &eth_hdr->s_addr);
52
53                 send_single_packet(qconf, m, dst_port);
54         } else if (tcp_or_udp && (l3_ptypes == RTE_PTYPE_L3_IPV6)) {
55                 /* Handle IPv6 headers.*/
56                 struct rte_ipv6_hdr *ipv6_hdr;
57
58                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
59                                                 sizeof(struct rte_ether_hdr));
60
61                 dst_port = em_get_ipv6_dst_port(ipv6_hdr, portid,
62                                         qconf->ipv6_lookup_struct);
63
64                 if (dst_port >= RTE_MAX_ETHPORTS ||
65                         (enabled_port_mask & 1 << dst_port) == 0)
66                         dst_port = portid;
67
68                 /* dst addr */
69                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
70
71                 /* src addr */
72                 rte_ether_addr_copy(&ports_eth_addr[dst_port],
73                                 &eth_hdr->s_addr);
74
75                 send_single_packet(qconf, m, dst_port);
76         } else {
77                 /* Free the mbuf that contains non-IPV4/IPV6 packet */
78                 rte_pktmbuf_free(m);
79         }
80 }
81
82 /*
83  * Buffer non-optimized handling of packets, invoked
84  * from main_loop.
85  */
86 static inline void
87 l3fwd_em_no_opt_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
88                         uint16_t portid, struct lcore_conf *qconf)
89 {
90         int32_t j;
91
92         /* Prefetch first packets */
93         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
94                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
95
96         /*
97          * Prefetch and forward already prefetched
98          * packets.
99          */
100         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
101                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
102                                 j + PREFETCH_OFFSET], void *));
103                 l3fwd_em_simple_forward(pkts_burst[j], portid, qconf);
104         }
105
106         /* Forward remaining prefetched packets */
107         for (; j < nb_rx; j++)
108                 l3fwd_em_simple_forward(pkts_burst[j], portid, qconf);
109 }
110
111 #endif /* __L3FWD_EM_H__ */