#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "l3fwd.h"
#if defined RTE_ARCH_X86
#include "l3fwd_sse.h"
#elif defined __ARM_NEON
#include "l3fwd_neon.h"
#elif defined RTE_ARCH_PPC_64
#include "l3fwd_altivec.h"
#else
#include "l3fwd_common.h"
#endif
#include "l3fwd_event.h"
#include "l3fwd_route.h"
#define FIB_PREFETCH_OFFSET 4
#define FIB_DEFAULT_HOP 999
#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];
static inline void
uint32_t *ipv4, uint32_t *ipv4_cnt,
uint32_t *ipv6_cnt, uint8_t *ip_type)
{
struct rte_ipv4_hdr *ipv4_hdr;
struct rte_ipv6_hdr *ipv6_hdr;
ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
*ip_type = 1;
(*ipv4_cnt)++;
}
else {
ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
*ipv6 = ipv6_hdr->dst_addr;
*ip_type = 0;
(*ipv6_cnt)++;
}
}
#if !defined FIB_SEND_MULTI
static inline void
process_packet(
struct rte_mbuf *pkt, uint16_t *hop)
{
#if defined DO_RFC_1812_CHECKS
rfc1812_process(
1),
#endif
*(uint64_t *)ð_hdr->
dst_addr = dest_eth_addr[*hop];
}
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;
for (j = 0; j < nb_tx; j++) {
process_packet(pkts_burst[j], &hops[j]);
if (hops[j] == BAD_PORT) {
continue;
}
send_single_packet(qconf, pkts_burst[j], hops[j]);
}
}
#endif
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];
uint16_t hops[SENDM_PORT_OVERHEAD(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;
for (i = 0; i < FIB_PREFETCH_OFFSET && i < nb_rx; i++)
for (i = 0; i < (nb_rx - FIB_PREFETCH_OFFSET); i++) {
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]);
}
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]);
ipv4_arr, hopsv4, ipv4_cnt);
if (ipv6_cnt > 0)
ipv6_arr, hopsv6, ipv6_cnt);
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
}
int
{
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
unsigned int lcore_id;
uint64_t prev_tsc, diff_tsc, cur_tsc;
int i, nb_rx;
uint16_t portid;
uint16_t queueid;
struct lcore_conf *qconf;
US_PER_S * BURST_TX_DRAIN_US;
qconf = &lcore_conf[lcore_id];
const uint16_t n_rx_q = qconf->n_rx_queue;
const uint16_t n_tx_p = qconf->n_tx_port;
if (n_rx_q == 0) {
RTE_LOG(INFO, L3FWD,
"lcore %u has nothing to do\n", lcore_id);
return 0;
}
RTE_LOG(INFO, L3FWD,
"entering main loop on lcore %u\n", lcore_id);
for (i = 0; i < n_rx_q; i++) {
portid = qconf->rx_queue_list[i].port_id;
queueid = qconf->rx_queue_list[i].queue_id;
" -- lcoreid=%u portid=%u rxqueueid=%" PRIu16 "\n",
lcore_id, portid, queueid);
}
cur_tsc = rte_rdtsc();
prev_tsc = cur_tsc;
while (!force_quit) {
diff_tsc = cur_tsc - prev_tsc;
for (i = 0; i < n_tx_p; ++i) {
portid = qconf->tx_port_id[i];
if (qconf->tx_mbufs[portid].len == 0)
continue;
send_burst(qconf,
qconf->tx_mbufs[portid].len,
portid);
qconf->tx_mbufs[portid].len = 0;
}
prev_tsc = cur_tsc;
}
for (i = 0; i < n_rx_q; ++i) {
portid = qconf->rx_queue_list[i].port_id;
queueid = qconf->rx_queue_list[i].queue_id;
nb_pkt_per_burst);
if (nb_rx == 0)
continue;
fib_send_packets(nb_rx, pkts_burst, portid, qconf);
}
cur_tsc = rte_rdtsc();
}
return 0;
}
#ifdef RTE_LIB_EVENTDEV
fib_event_loop(struct l3fwd_event_resources *evt_rsrc,
const uint8_t flags)
{
const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
evt_rsrc->evq.nb_queues - 1];
const uint8_t event_d_id = evt_rsrc->event_d_id;
const uint16_t deq_len = evt_rsrc->deq_depth;
int i, nb_enq = 0, nb_deq = 0;
struct lcore_conf *lconf;
unsigned int lcore_id;
uint32_t ipv4_arr[MAX_PKT_BURST];
uint64_t hopsv4[MAX_PKT_BURST], hopsv6[MAX_PKT_BURST];
uint16_t nh, hops[MAX_PKT_BURST];
uint8_t type_arr[MAX_PKT_BURST];
uint32_t ipv4_cnt, ipv6_cnt;
uint32_t ipv4_arr_assem, ipv6_arr_assem;
if (event_p_id < 0)
return;
lconf = &lcore_conf[lcore_id];
RTE_LOG(INFO, L3FWD,
"entering %s on lcore %u\n", __func__, lcore_id);
while (!force_quit) {
events, deq_len, 0);
if (nb_deq == 0) {
continue;
}
ipv4_cnt = 0;
ipv6_cnt = 0;
ipv4_arr_assem = 0;
ipv6_arr_assem = 0;
for (i = 0; i < FIB_PREFETCH_OFFSET && i < nb_deq; i++)
for (i = 0; i < (nb_deq - FIB_PREFETCH_OFFSET); i++) {
if (flags & L3FWD_EVENT_TX_ENQ) {
events[i].queue_id = tx_q_id;
}
if (flags & L3FWD_EVENT_TX_DIRECT)
0);
i + FIB_PREFETCH_OFFSET].mbuf,
void *));
fib_parse_packet(events[i].mbuf,
&ipv4_arr[ipv4_cnt], &ipv4_cnt,
&ipv6_arr[ipv6_cnt], &ipv6_cnt,
&type_arr[i]);
}
for (; i < nb_deq; i++) {
if (flags & L3FWD_EVENT_TX_ENQ) {
events[i].queue_id = tx_q_id;
}
if (flags & L3FWD_EVENT_TX_DIRECT)
0);
fib_parse_packet(events[i].mbuf,
&ipv4_arr[ipv4_cnt], &ipv4_cnt,
&ipv6_arr[ipv6_cnt], &ipv6_cnt,
&type_arr[i]);
}
ipv4_arr, hopsv4, ipv4_cnt);
if (ipv6_cnt > 0)
ipv6_arr, hopsv6, ipv6_cnt);
for (i = 0; i < nb_deq; 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 :
events[i].mbuf->port;
process_packet(events[i].mbuf, &hops[i]);
events[i].mbuf->port = hops[i] != BAD_PORT ?
hops[i] :
events[i].mbuf->port;
}
if (flags & L3FWD_EVENT_TX_ENQ) {
events, nb_deq);
while (nb_enq < nb_deq && !force_quit)
event_p_id, events + nb_enq,
nb_deq - nb_enq);
}
if (flags & L3FWD_EVENT_TX_DIRECT) {
event_p_id, events, nb_deq, 0);
while (nb_enq < nb_deq && !force_quit)
event_d_id, event_p_id,
events + nb_enq,
nb_deq - nb_enq, 0);
}
}
l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
nb_deq, 0);
}
{
struct l3fwd_event_resources *evt_rsrc =
l3fwd_get_eventdev_rsrc();
fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
return 0;
}
{
struct l3fwd_event_resources *evt_rsrc =
l3fwd_get_eventdev_rsrc();
fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
return 0;
}
{
struct l3fwd_event_resources *evt_rsrc =
l3fwd_get_eventdev_rsrc();
fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ);
return 0;
}
{
struct l3fwd_event_resources *evt_rsrc =
l3fwd_get_eventdev_rsrc();
fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ);
return 0;
}
fib_process_event_vector(struct rte_event_vector *vec, uint8_t *type_arr,
struct rte_ipv6_addr *ipv6_arr, uint64_t *hopsv4, uint64_t *hopsv6,
uint32_t *ipv4_arr, uint16_t *hops)
{
uint32_t ipv4_arr_assem, ipv6_arr_assem;
uint32_t ipv4_cnt, ipv6_cnt;
struct lcore_conf *lconf;
uint16_t nh;
int i;
ipv4_cnt = 0;
ipv6_cnt = 0;
ipv4_arr_assem = 0;
ipv6_arr_assem = 0;
for (i = 0; i < FIB_PREFETCH_OFFSET && i < vec->nb_elem; i++)
for (i = 0; i < (vec->nb_elem - FIB_PREFETCH_OFFSET); i++) {
void *));
fib_parse_packet(mbufs[i], &ipv4_arr[ipv4_cnt], &ipv4_cnt,
&ipv6_arr[ipv6_cnt], &ipv6_cnt, &type_arr[i]);
}
for (; i < vec->nb_elem; i++)
fib_parse_packet(mbufs[i], &ipv4_arr[ipv4_cnt], &ipv4_cnt,
&ipv6_arr[ipv6_cnt], &ipv6_cnt, &type_arr[i]);
ipv4_cnt);
if (ipv6_cnt > 0)
lconf->ipv6_lookup_struct,
ipv6_arr, hopsv6,
ipv6_cnt);
for (i = 0; i < vec->nb_elem; i++) {
if (type_arr[i])
nh = (uint16_t)hopsv4[ipv4_arr_assem++];
else
nh = (uint16_t)hopsv6[ipv6_arr_assem++];
if (nh != FIB_DEFAULT_HOP)
hops[i] = nh;
else
hops[i] = vec->attr_valid ? vec->port :
vec->mbufs[i]->port;
}
#if defined FIB_SEND_MULTI
uint16_t k;
for (i = 0; i != k; i += FWDSTEP)
processx4_step3(&vec->mbufs[i], &hops[i]);
for (; i < vec->nb_elem; i++)
process_packet(vec->mbufs[i], &hops[i]);
#else
for (i = 0; i < vec->nb_elem; i++)
process_packet(vec->mbufs[i], &hops[i]);
#endif
process_event_vector(vec, hops);
}
fib_event_loop_vector(struct l3fwd_event_resources *evt_rsrc,
const uint8_t flags)
{
const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
const uint8_t tx_q_id =
evt_rsrc->evq.event_q_id[evt_rsrc->evq.nb_queues - 1];
const uint8_t event_d_id = evt_rsrc->event_d_id;
const uint16_t deq_len = evt_rsrc->deq_depth;
uint8_t *type_arr;
int nb_enq = 0, nb_deq = 0, i;
uint64_t *hopsv4, *hopsv6;
uint32_t *ipv4_arr;
uint16_t *hops;
uintptr_t mem;
"vector_fib",
(sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint64_t) +
sizeof(uint64_t) + sizeof(uint16_t) + sizeof(uint8_t *) +
evt_rsrc->vector_size,
RTE_CACHE_LINE_SIZE);
if (mem == 0)
return;
ipv4_arr = (uint32_t *)mem;
type_arr = (uint8_t *)&ipv4_arr[evt_rsrc->vector_size];
hopsv4 = (uint64_t *)&type_arr[evt_rsrc->vector_size];
hopsv6 = (uint64_t *)&hopsv4[evt_rsrc->vector_size];
hops = (uint16_t *)&hopsv6[evt_rsrc->vector_size];
if (event_p_id < 0) {
return;
}
RTE_LOG(INFO, L3FWD,
"entering %s on lcore %u\n", __func__,
while (!force_quit) {
deq_len, 0);
if (nb_deq == 0) {
continue;
}
for (i = 0; i < nb_deq; i++) {
if (flags & L3FWD_EVENT_TX_ENQ) {
events[i].queue_id = tx_q_id;
}
fib_process_event_vector(events[i].vec, type_arr,
ipv6_arr, hopsv4, hopsv6,
ipv4_arr, hops);
}
if (flags & L3FWD_EVENT_TX_ENQ) {
events, nb_deq);
while (nb_enq < nb_deq && !force_quit)
event_d_id, event_p_id, events + nb_enq,
nb_deq - nb_enq);
}
if (flags & L3FWD_EVENT_TX_DIRECT) {
event_d_id, event_p_id, events, nb_deq, 0);
while (nb_enq < nb_deq && !force_quit)
event_d_id, event_p_id, events + nb_enq,
nb_deq - nb_enq, 0);
}
}
l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
nb_deq, 1);
}
{
struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
fib_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
return 0;
}
fib_event_main_loop_tx_d_burst_vector(
__rte_unused void *dummy)
{
struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
fib_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
return 0;
}
{
struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
fib_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
return 0;
}
fib_event_main_loop_tx_q_burst_vector(
__rte_unused void *dummy)
{
struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
fib_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
return 0;
}
#endif
void
setup_fib(const int socketid)
{
int i;
int ret;
char s[64];
char abuf[INET6_ADDRSTRLEN];
config_ipv4.
max_routes = (1 << 16);
config_ipv4.
dir24_8.
nh_sz = RTE_FIB_DIR24_8_4B;
config_ipv4.dir24_8.
num_tbl8 = (1 << 15);
snprintf(s, sizeof(s), "IPV4_L3FWD_FIB_%d", socketid);
ipv4_l3fwd_fib_lookup_struct[socketid] =
if (ipv4_l3fwd_fib_lookup_struct[socketid] == NULL)
"Unable to create the l3fwd FIB table on socket %d\n",
socketid);
for (i = 0; i < route_num_v4; i++) {
struct in_addr in;
if ((1 << route_base_v4[i].if_out &
enabled_port_mask) == 0)
continue;
if (ret < 0)
"Unable to get device info for port %u\n",
route_base_v4[i].if_out);
ret =
rte_fib_add(ipv4_l3fwd_fib_lookup_struct[socketid],
route_base_v4[i].ip,
route_base_v4[i].depth,
route_base_v4[i].if_out);
if (ret < 0) {
free(route_base_v4);
"Unable to add entry %u to the l3fwd FIB table on socket %d\n",
i, socketid);
}
in.s_addr = htonl(route_base_v4[i].ip);
if (inet_ntop(AF_INET, &in, abuf, sizeof(abuf)) != NULL) {
printf("FIB: Adding route %s / %d (%d) [%s]\n", abuf,
route_base_v4[i].depth,
route_base_v4[i].if_out,
} else {
printf("FIB: IPv4 route added to port %d [%s]\n",
route_base_v4[i].if_out,
}
}
snprintf(s, sizeof(s), "IPV6_L3FWD_FIB_%d", socketid);
config.max_routes = (1 << 16) - 1;
config.rib_ext_sz = 0;
config.default_nh = FIB_DEFAULT_HOP;
config.trie.nh_sz = RTE_FIB6_TRIE_4B;
config.trie.num_tbl8 = (1 << 15);
&config);
if (ipv6_l3fwd_fib_lookup_struct[socketid] == NULL) {
free(route_base_v4);
"Unable to create the l3fwd FIB table on socket %d\n",
socketid);
}
for (i = 0; i < route_num_v6; i++) {
if ((1 << route_base_v6[i].if_out &
enabled_port_mask) == 0)
continue;
if (ret < 0)
"Unable to get device info for port %u\n",
route_base_v6[i].if_out);
&route_base_v6[i].ip6,
route_base_v6[i].depth,
route_base_v6[i].if_out);
if (ret < 0) {
free(route_base_v4);
free(route_base_v6);
"Unable to add entry %u to the l3fwd FIB table on socket %d\n",
i, socketid);
}
if (inet_ntop(AF_INET6, &route_base_v6[i].ip6,
abuf, sizeof(abuf)) != NULL) {
printf("FIB: Adding route %s / %d (%d) [%s]\n", abuf,
route_base_v6[i].depth,
route_base_v6[i].if_out,
} else {
printf("FIB: IPv6 route added to port %d [%s]\n",
route_base_v6[i].if_out,
}
}
}
void *
fib_get_ipv4_l3fwd_lookup_struct(const int socketid)
{
return ipv4_l3fwd_fib_lookup_struct[socketid];
}
void *
fib_get_ipv6_l3fwd_lookup_struct(const int socketid)
{
return ipv6_l3fwd_fib_lookup_struct[socketid];
}
static uint32_t rte_be_to_cpu_32(rte_be32_t x)
#define RTE_ALIGN_FLOOR(val, align)
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_always_inline
uint64_t rte_get_tsc_hz(void)
const char * rte_dev_name(const struct rte_device *dev)
static uint16_t rte_eth_rx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **rx_pkts, const uint16_t nb_pkts)
int rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info) __rte_warn_unused_result
static void rte_ether_addr_copy(const struct rte_ether_addr *__restrict ea_from, struct rte_ether_addr *__restrict ea_to)
static __rte_always_inline void rte_event_eth_tx_adapter_txq_set(struct rte_mbuf *pkt, uint16_t queue)
static uint16_t rte_event_eth_tx_adapter_enqueue(uint8_t dev_id, uint8_t port_id, struct rte_event ev[], uint16_t nb_events, const uint8_t flags)
static uint16_t rte_event_dequeue_burst(uint8_t dev_id, uint8_t port_id, struct rte_event ev[], uint16_t nb_events, uint64_t timeout_ticks)
#define RTE_EVENT_OP_FORWARD
static uint16_t rte_event_enqueue_burst(uint8_t dev_id, uint8_t port_id, const struct rte_event ev[], uint16_t nb_events)
int rte_fib6_lookup_bulk(struct rte_fib6 *fib, const struct rte_ipv6_addr *ips, uint64_t *next_hops, int n)
int rte_fib6_add(struct rte_fib6 *fib, const struct rte_ipv6_addr *ip, uint8_t depth, uint64_t next_hop)
struct rte_fib6 * rte_fib6_create(const char *name, int socket_id, struct rte_fib6_conf *conf)
struct rte_fib * rte_fib_create(const char *name, int socket_id, struct rte_fib_conf *conf)
int rte_fib_lookup_bulk(struct rte_fib *fib, uint32_t *ips, uint64_t *next_hops, int n)
int rte_fib_add(struct rte_fib *fib, uint32_t ip, uint8_t depth, uint64_t next_hop)
static unsigned rte_lcore_id(void)
#define RTE_LOG(l, t,...)
void * rte_zmalloc(const char *type, size_t size, unsigned align) __rte_alloc_size(2) __rte_alloc_align(3) __rte_malloc __rte_dealloc_free
static void rte_pktmbuf_free(struct rte_mbuf *m)
#define rte_pktmbuf_mtod(m, t)
#define RTE_PTYPE_L3_IPV4
static void rte_pause(void)
static void rte_prefetch0(const volatile void *p)
struct rte_ether_addr src_addr
struct rte_ether_addr dst_addr