net: add rte prefix to ether structures
[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 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 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                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
51
52                 send_single_packet(qconf, m, dst_port);
53         } else if (tcp_or_udp && (l3_ptypes == RTE_PTYPE_L3_IPV6)) {
54                 /* Handle IPv6 headers.*/
55                 struct ipv6_hdr *ipv6_hdr;
56
57                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
58                                                 sizeof(struct rte_ether_hdr));
59
60                 dst_port = em_get_ipv6_dst_port(ipv6_hdr, portid,
61                                         qconf->ipv6_lookup_struct);
62
63                 if (dst_port >= RTE_MAX_ETHPORTS ||
64                         (enabled_port_mask & 1 << dst_port) == 0)
65                         dst_port = portid;
66
67                 /* dst addr */
68                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
69
70                 /* src addr */
71                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
72
73                 send_single_packet(qconf, m, dst_port);
74         } else {
75                 /* Free the mbuf that contains non-IPV4/IPV6 packet */
76                 rte_pktmbuf_free(m);
77         }
78 }
79
80 /*
81  * Buffer non-optimized handling of packets, invoked
82  * from main_loop.
83  */
84 static inline void
85 l3fwd_em_no_opt_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
86                         uint16_t portid, struct lcore_conf *qconf)
87 {
88         int32_t j;
89
90         /* Prefetch first packets */
91         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
92                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
93
94         /*
95          * Prefetch and forward already prefetched
96          * packets.
97          */
98         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
99                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
100                                 j + PREFETCH_OFFSET], void *));
101                 l3fwd_em_simple_forward(pkts_burst[j], portid, qconf);
102         }
103
104         /* Forward remaining prefetched packets */
105         for (; j < nb_rx; j++)
106                 l3fwd_em_simple_forward(pkts_burst[j], portid, qconf);
107 }
108
109 #endif /* __L3FWD_EM_H__ */