+/* Configure how many packets ahead to prefetch for fib. */
+#define FIB_PREFETCH_OFFSET 4
+
+/* A non-existent portid is needed to denote a default hop for fib. */
+#define FIB_DEFAULT_HOP 999
+
+/*
+ * If the machine has SSE, NEON or PPC 64 then multiple packets
+ * can be sent at once if not only single packets will be sent
+ */
+#if defined RTE_ARCH_X86 || defined __ARM_NEON \
+ || defined RTE_ARCH_PPC_64
+#define FIB_SEND_MULTI
+#endif
+
+static struct rte_fib *ipv4_l3fwd_fib_lookup_struct[NB_SOCKETS];
+static struct rte_fib6 *ipv6_l3fwd_fib_lookup_struct[NB_SOCKETS];
+
+/* Parse packet type and ip address. */
+static inline void
+fib_parse_packet(struct rte_mbuf *mbuf,
+ uint32_t *ipv4, uint32_t *ipv4_cnt,
+ uint8_t ipv6[RTE_FIB6_IPV6_ADDR_SIZE],
+ uint32_t *ipv6_cnt, uint8_t *ip_type)
+{
+ struct rte_ether_hdr *eth_hdr;
+ struct rte_ipv4_hdr *ipv4_hdr;
+ struct rte_ipv6_hdr *ipv6_hdr;
+
+ eth_hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
+ /* IPv4 */
+ if (mbuf->packet_type & RTE_PTYPE_L3_IPV4) {
+ ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
+ *ipv4 = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
+ /* Store type of packet in type_arr (IPv4=1, IPv6=0). */
+ *ip_type = 1;
+ (*ipv4_cnt)++;
+ }
+ /* IPv6 */
+ else {
+ ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
+ rte_mov16(ipv6, (const uint8_t *)ipv6_hdr->dst_addr);
+ *ip_type = 0;
+ (*ipv6_cnt)++;
+ }
+}
+
+/*
+ * If the machine does not have SSE, NEON or PPC 64 then the packets
+ * are sent one at a time using send_single_packet()
+ */
+#if !defined FIB_SEND_MULTI
+static inline void
+fib_send_single(int nb_tx, struct lcore_conf *qconf,
+ struct rte_mbuf **pkts_burst, uint16_t hops[nb_tx])
+{
+ int32_t j;
+ struct rte_ether_hdr *eth_hdr;
+
+ for (j = 0; j < nb_tx; j++) {
+ /* Run rfc1812 if packet is ipv4 and checks enabled. */
+#if defined DO_RFC_1812_CHECKS
+ rfc1812_process((struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(
+ pkts_burst[j], struct rte_ether_hdr *) + 1),
+ &hops[j], pkts_burst[j]->packet_type);
+#endif
+
+ /* Set MAC addresses. */
+ eth_hdr = rte_pktmbuf_mtod(pkts_burst[j],
+ struct rte_ether_hdr *);
+ *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[hops[j]];
+ rte_ether_addr_copy(&ports_eth_addr[hops[j]],
+ ð_hdr->s_addr);
+
+ /* Send single packet. */
+ send_single_packet(qconf, pkts_burst[j], hops[j]);
+ }
+}
+#endif
+
+/* Bulk parse, fib lookup and send. */
+static inline void
+fib_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
+ uint16_t portid, struct lcore_conf *qconf)
+{
+ uint32_t ipv4_arr[nb_rx];
+ uint8_t ipv6_arr[nb_rx][RTE_FIB6_IPV6_ADDR_SIZE];
+ uint16_t hops[nb_rx];
+ uint64_t hopsv4[nb_rx], hopsv6[nb_rx];
+ uint8_t type_arr[nb_rx];
+ uint32_t ipv4_cnt = 0, ipv6_cnt = 0;
+ uint32_t ipv4_arr_assem = 0, ipv6_arr_assem = 0;
+ uint16_t nh;
+ int32_t i;
+
+ /* Prefetch first packets. */
+ for (i = 0; i < FIB_PREFETCH_OFFSET && i < nb_rx; i++)
+ rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i], void *));
+
+ /* Parse packet info and prefetch. */
+ for (i = 0; i < (nb_rx - FIB_PREFETCH_OFFSET); i++) {
+ /* Prefetch packet. */
+ rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
+ i + FIB_PREFETCH_OFFSET], void *));
+ fib_parse_packet(pkts_burst[i],
+ &ipv4_arr[ipv4_cnt], &ipv4_cnt,
+ ipv6_arr[ipv6_cnt], &ipv6_cnt,
+ &type_arr[i]);
+ }
+
+ /* Parse remaining packet info. */
+ for (; i < nb_rx; i++)
+ fib_parse_packet(pkts_burst[i],
+ &ipv4_arr[ipv4_cnt], &ipv4_cnt,
+ ipv6_arr[ipv6_cnt], &ipv6_cnt,
+ &type_arr[i]);
+
+ /* Lookup IPv4 hops if IPv4 packets are present. */
+ if (likely(ipv4_cnt > 0))
+ rte_fib_lookup_bulk(qconf->ipv4_lookup_struct,
+ ipv4_arr, hopsv4, ipv4_cnt);
+
+ /* Lookup IPv6 hops if IPv6 packets are present. */
+ if (ipv6_cnt > 0)
+ rte_fib6_lookup_bulk(qconf->ipv6_lookup_struct,
+ ipv6_arr, hopsv6, ipv6_cnt);
+
+ /* Add IPv4 and IPv6 hops to one array depending on type. */
+ for (i = 0; i < nb_rx; i++) {
+ if (type_arr[i])
+ nh = (uint16_t)hopsv4[ipv4_arr_assem++];
+ else
+ nh = (uint16_t)hopsv6[ipv6_arr_assem++];
+ hops[i] = nh != FIB_DEFAULT_HOP ? nh : portid;
+ }
+
+#if defined FIB_SEND_MULTI
+ send_packets_multi(qconf, pkts_burst, hops, nb_rx);
+#else
+ fib_send_single(nb_rx, qconf, pkts_burst, hops);
+#endif
+}
+