Imported Upstream version 16.04
[deb_dpdk.git] / examples / l3fwd / l3fwd_lpm.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __L3FWD_LPM_H__
35 #define __L3FWD_LPM_H__
36
37 static inline uint8_t
38 lpm_get_ipv4_dst_port(void *ipv4_hdr,  uint8_t portid, void *lookup_struct)
39 {
40         uint32_t next_hop;
41         struct rte_lpm *ipv4_l3fwd_lookup_struct =
42                 (struct rte_lpm *)lookup_struct;
43
44         return (uint8_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
45                 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
46                 &next_hop) == 0) ? next_hop : portid);
47 }
48
49 static inline uint8_t
50 lpm_get_ipv6_dst_port(void *ipv6_hdr,  uint8_t portid, void *lookup_struct)
51 {
52         uint8_t next_hop;
53         struct rte_lpm6 *ipv6_l3fwd_lookup_struct =
54                 (struct rte_lpm6 *)lookup_struct;
55
56         return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
57                         ((struct ipv6_hdr *)ipv6_hdr)->dst_addr,
58                         &next_hop) == 0) ?  next_hop : portid);
59 }
60
61 static inline __attribute__((always_inline)) void
62 l3fwd_lpm_simple_forward(struct rte_mbuf *m, uint8_t portid,
63                 struct lcore_conf *qconf)
64 {
65         struct ether_hdr *eth_hdr;
66         struct ipv4_hdr *ipv4_hdr;
67         uint8_t dst_port;
68
69         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
70
71         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
72                 /* Handle IPv4 headers.*/
73                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
74                                                    sizeof(struct ether_hdr));
75
76 #ifdef DO_RFC_1812_CHECKS
77                 /* Check to make sure the packet is valid (RFC1812) */
78                 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
79                         rte_pktmbuf_free(m);
80                         return;
81                 }
82 #endif
83                  dst_port = lpm_get_ipv4_dst_port(ipv4_hdr, portid,
84                                                 qconf->ipv4_lookup_struct);
85
86                 if (dst_port >= RTE_MAX_ETHPORTS ||
87                         (enabled_port_mask & 1 << dst_port) == 0)
88                         dst_port = portid;
89
90 #ifdef DO_RFC_1812_CHECKS
91                 /* Update time to live and header checksum */
92                 --(ipv4_hdr->time_to_live);
93                 ++(ipv4_hdr->hdr_checksum);
94 #endif
95                 /* dst addr */
96                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
97
98                 /* src addr */
99                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
100
101                 send_single_packet(qconf, m, dst_port);
102         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
103                 /* Handle IPv6 headers.*/
104                 struct ipv6_hdr *ipv6_hdr;
105
106                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
107                                                    sizeof(struct ether_hdr));
108
109                 dst_port = lpm_get_ipv6_dst_port(ipv6_hdr, portid,
110                                         qconf->ipv6_lookup_struct);
111
112                 if (dst_port >= RTE_MAX_ETHPORTS ||
113                         (enabled_port_mask & 1 << dst_port) == 0)
114                         dst_port = portid;
115
116                 /* dst addr */
117                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
118
119                 /* src addr */
120                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
121
122                 send_single_packet(qconf, m, dst_port);
123         } else {
124                 /* Free the mbuf that contains non-IPV4/IPV6 packet */
125                 rte_pktmbuf_free(m);
126         }
127 }
128
129 static inline void
130 l3fwd_lpm_no_opt_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
131                                 uint8_t portid, struct lcore_conf *qconf)
132 {
133         int32_t j;
134
135         /* Prefetch first packets */
136         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
137                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
138
139         /* Prefetch and forward already prefetched packets. */
140         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
141                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
142                                 j + PREFETCH_OFFSET], void *));
143                 l3fwd_lpm_simple_forward(pkts_burst[j], portid, qconf);
144         }
145
146         /* Forward remaining prefetched packets */
147         for (; j < nb_rx; j++)
148                 l3fwd_lpm_simple_forward(pkts_burst[j], portid, qconf);
149 }
150
151 #endif /* __L3FWD_LPM_H__ */