#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <string.h>
#include <sys/queue.h>
#include <stdarg.h>
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <netinet/in.h>
#include "l3fwd.h"
#if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_CRC32)
#define EM_HASH_CRC 1
#endif
#ifdef EM_HASH_CRC
#define DEFAULT_HASH_FUNC rte_hash_crc
#else
#define DEFAULT_HASH_FUNC rte_jhash
#endif
#define IPV6_ADDR_LEN 16
struct ipv4_5tuple {
uint32_t ip_dst;
uint32_t ip_src;
uint16_t port_dst;
uint16_t port_src;
uint8_t proto;
} __attribute__((__packed__));
union ipv4_5tuple_host {
struct {
uint8_t pad0;
uint8_t proto;
uint16_t pad1;
uint32_t ip_src;
uint32_t ip_dst;
uint16_t port_src;
uint16_t port_dst;
};
xmm_t xmm;
};
#define XMM_NUM_IN_IPV6_5TUPLE 3
struct ipv6_5tuple {
uint8_t ip_dst[IPV6_ADDR_LEN];
uint8_t ip_src[IPV6_ADDR_LEN];
uint16_t port_dst;
uint16_t port_src;
uint8_t proto;
} __attribute__((__packed__));
union ipv6_5tuple_host {
struct {
uint16_t pad0;
uint8_t proto;
uint8_t pad1;
uint8_t ip_src[IPV6_ADDR_LEN];
uint8_t ip_dst[IPV6_ADDR_LEN];
uint16_t port_src;
uint16_t port_dst;
uint64_t reserve;
};
xmm_t xmm[XMM_NUM_IN_IPV6_5TUPLE];
};
struct ipv4_l3fwd_em_route {
struct ipv4_5tuple key;
uint8_t if_out;
};
struct ipv6_l3fwd_em_route {
struct ipv6_5tuple key;
uint8_t if_out;
};
static struct ipv4_l3fwd_em_route ipv4_l3fwd_em_route_array[] = {
{{
RTE_IPV4(101, 0, 0, 0),
RTE_IPV4(100, 10, 0, 1), 101, 11, IPPROTO_TCP}, 0},
{{
RTE_IPV4(201, 0, 0, 0),
RTE_IPV4(200, 20, 0, 1), 102, 12, IPPROTO_TCP}, 1},
{{
RTE_IPV4(111, 0, 0, 0),
RTE_IPV4(100, 30, 0, 1), 101, 11, IPPROTO_TCP}, 2},
{{
RTE_IPV4(211, 0, 0, 0),
RTE_IPV4(200, 40, 0, 1), 102, 12, IPPROTO_TCP}, 3},
};
static struct ipv6_l3fwd_em_route ipv6_l3fwd_em_route_array[] = {
{{
{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
101, 11, IPPROTO_TCP}, 0},
{{
{0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
{0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
102, 12, IPPROTO_TCP}, 1},
{{
{0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
{0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
101, 11, IPPROTO_TCP}, 2},
{{
{0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
{0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
102, 12, IPPROTO_TCP}, 3},
};
struct rte_hash *ipv4_l3fwd_em_lookup_struct[NB_SOCKETS];
struct rte_hash *ipv6_l3fwd_em_lookup_struct[NB_SOCKETS];
static inline uint32_t
ipv4_hash_crc(
const void *data,
__rte_unused uint32_t data_len,
uint32_t init_val)
{
const union ipv4_5tuple_host *k;
uint32_t t;
const uint32_t *p;
k = data;
t = k->proto;
p = (const uint32_t *)&k->port_src;
#ifdef EM_HASH_CRC
#else
#endif
return init_val;
}
static inline uint32_t
ipv6_hash_crc(
const void *data,
__rte_unused uint32_t data_len,
uint32_t init_val)
{
const union ipv6_5tuple_host *k;
uint32_t t;
const uint32_t *p;
#ifdef EM_HASH_CRC
const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
#endif
k = data;
t = k->proto;
p = (const uint32_t *)&k->port_src;
#ifdef EM_HASH_CRC
ip_src0 = (const uint32_t *) k->ip_src;
ip_src1 = (const uint32_t *)(k->ip_src+4);
ip_src2 = (const uint32_t *)(k->ip_src+8);
ip_src3 = (const uint32_t *)(k->ip_src+12);
ip_dst0 = (const uint32_t *) k->ip_dst;
ip_dst1 = (const uint32_t *)(k->ip_dst+4);
ip_dst2 = (const uint32_t *)(k->ip_dst+8);
ip_dst3 = (const uint32_t *)(k->ip_dst+12);
#else
sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
#endif
return init_val;
}
#define IPV4_L3FWD_EM_NUM_ROUTES \
(sizeof(ipv4_l3fwd_em_route_array) / sizeof(ipv4_l3fwd_em_route_array[0]))
#define IPV6_L3FWD_EM_NUM_ROUTES \
(sizeof(ipv6_l3fwd_em_route_array) / sizeof(ipv6_l3fwd_em_route_array[0]))
static rte_xmm_t mask0;
static rte_xmm_t mask1;
static rte_xmm_t mask2;
#if defined(RTE_MACHINE_CPUFLAG_SSE2)
static inline xmm_t
em_mask_key(void *key, xmm_t mask)
{
__m128i data = _mm_loadu_si128((__m128i *)(key));
return _mm_and_si128(data, mask);
}
#elif defined(RTE_MACHINE_CPUFLAG_NEON)
static inline xmm_t
em_mask_key(void *key, xmm_t mask)
{
int32x4_t data = vld1q_s32((int32_t *)key);
return vandq_s32(data, mask);
}
#elif defined(RTE_MACHINE_CPUFLAG_ALTIVEC)
static inline xmm_t
em_mask_key(void *key, xmm_t mask)
{
xmm_t data = vec_ld(0, (xmm_t *)(key));
return vec_and(data, mask);
}
#else
#error No vector engine (SSE, NEON, ALTIVEC) available, check your toolchain
#endif
static inline uint16_t
em_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
{
int ret = 0;
union ipv4_5tuple_host key;
struct rte_hash *ipv4_l3fwd_lookup_struct =
ipv4_hdr = (uint8_t *)ipv4_hdr +
key.xmm = em_mask_key(ipv4_hdr, mask0.x);
return (ret < 0) ? portid : ipv4_l3fwd_out_if[ret];
}
static inline uint16_t
em_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
{
int ret = 0;
union ipv6_5tuple_host key;
struct rte_hash *ipv6_l3fwd_lookup_struct =
ipv6_hdr = (uint8_t *)ipv6_hdr +
void *data0 = ipv6_hdr;
void *data1 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t);
void *data2 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t) + sizeof(xmm_t);
key.xmm[0] = em_mask_key(data0, mask1.x);
#if defined RTE_ARCH_X86
key.xmm[1] = _mm_loadu_si128(data1);
#else
key.xmm[1] = *(xmm_t *)data1;
#endif
key.xmm[2] = em_mask_key(data2, mask2.x);
return (ret < 0) ? portid : ipv6_l3fwd_out_if[ret];
}
#if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
#if defined(NO_HASH_MULTI_LOOKUP)
#include "l3fwd_em_sequential.h"
#else
#include "l3fwd_em_hlm.h"
#endif
#else
#include "l3fwd_em.h"
#endif
static void
convert_ipv4_5tuple(struct ipv4_5tuple *key1,
union ipv4_5tuple_host *key2)
{
key2->proto = key1->proto;
key2->pad0 = 0;
key2->pad1 = 0;
}
static void
convert_ipv6_5tuple(struct ipv6_5tuple *key1,
union ipv6_5tuple_host *key2)
{
uint32_t i;
for (i = 0; i < 16; i++) {
key2->ip_dst[i] = key1->ip_dst[i];
key2->ip_src[i] = key1->ip_src[i];
}
key2->proto = key1->proto;
key2->pad0 = 0;
key2->pad1 = 0;
key2->reserve = 0;
}
#define BYTE_VALUE_MAX 256
#define ALL_32_BITS 0xffffffff
#define BIT_8_TO_15 0x0000ff00
static inline void
populate_ipv4_few_flow_into_table(
const struct rte_hash *h)
{
uint32_t i;
int32_t ret;
mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
ALL_32_BITS, ALL_32_BITS} };
for (i = 0; i < IPV4_L3FWD_EM_NUM_ROUTES; i++) {
struct ipv4_l3fwd_em_route entry;
union ipv4_5tuple_host newkey;
entry = ipv4_l3fwd_em_route_array[i];
convert_ipv4_5tuple(&entry.key, &newkey);
if (ret < 0) {
rte_exit(EXIT_FAILURE,
"Unable to add entry %" PRIu32
" to the l3fwd hash.\n", i);
}
ipv4_l3fwd_out_if[ret] = entry.if_out;
}
printf("Hash: Adding 0x%" PRIx64 " keys\n",
(uint64_t)IPV4_L3FWD_EM_NUM_ROUTES);
}
#define BIT_16_TO_23 0x00ff0000
static inline void
populate_ipv6_few_flow_into_table(
const struct rte_hash *h)
{
uint32_t i;
int32_t ret;
mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
ALL_32_BITS, ALL_32_BITS} };
mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
for (i = 0; i < IPV6_L3FWD_EM_NUM_ROUTES; i++) {
struct ipv6_l3fwd_em_route entry;
union ipv6_5tuple_host newkey;
entry = ipv6_l3fwd_em_route_array[i];
convert_ipv6_5tuple(&entry.key, &newkey);
if (ret < 0) {
rte_exit(EXIT_FAILURE,
"Unable to add entry %" PRIu32
" to the l3fwd hash.\n", i);
}
ipv6_l3fwd_out_if[ret] = entry.if_out;
}
printf("Hash: Adding 0x%" PRIx64 "keys\n",
(uint64_t)IPV6_L3FWD_EM_NUM_ROUTES);
}
#define NUMBER_PORT_USED 4
static inline void
populate_ipv4_many_flow_into_table(
const struct rte_hash *h,
unsigned int nr_flow)
{
unsigned i;
mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
ALL_32_BITS, ALL_32_BITS} };
for (i = 0; i < nr_flow; i++) {
struct ipv4_l3fwd_em_route entry;
union ipv4_5tuple_host newkey;
uint8_t a = (uint8_t)
((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
uint8_t b = (uint8_t)
(((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
uint8_t c = (uint8_t)
((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
memset(&entry, 0, sizeof(entry));
switch (i & (NUMBER_PORT_USED - 1)) {
case 0:
entry = ipv4_l3fwd_em_route_array[0];
entry.key.ip_dst =
RTE_IPV4(101, c, b, a);
break;
case 1:
entry = ipv4_l3fwd_em_route_array[1];
entry.key.ip_dst =
RTE_IPV4(201, c, b, a);
break;
case 2:
entry = ipv4_l3fwd_em_route_array[2];
entry.key.ip_dst =
RTE_IPV4(111, c, b, a);
break;
case 3:
entry = ipv4_l3fwd_em_route_array[3];
entry.key.ip_dst =
RTE_IPV4(211, c, b, a);
break;
};
convert_ipv4_5tuple(&entry.key, &newkey);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Unable to add entry %u\n", i);
ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
}
printf("Hash: Adding 0x%x keys\n", nr_flow);
}
static inline void
populate_ipv6_many_flow_into_table(
const struct rte_hash *h,
unsigned int nr_flow)
{
unsigned i;
mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
ALL_32_BITS, ALL_32_BITS} };
mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
for (i = 0; i < nr_flow; i++) {
struct ipv6_l3fwd_em_route entry;
union ipv6_5tuple_host newkey;
uint8_t a = (uint8_t)
((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
uint8_t b = (uint8_t)
(((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
uint8_t c = (uint8_t)
((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
memset(&entry, 0, sizeof(entry));
switch (i & (NUMBER_PORT_USED - 1)) {
case 0:
entry = ipv6_l3fwd_em_route_array[0];
break;
case 1:
entry = ipv6_l3fwd_em_route_array[1];
break;
case 2:
entry = ipv6_l3fwd_em_route_array[2];
break;
case 3:
entry = ipv6_l3fwd_em_route_array[3];
break;
};
entry.key.ip_dst[13] = c;
entry.key.ip_dst[14] = b;
entry.key.ip_dst[15] = a;
convert_ipv6_5tuple(&entry.key, &newkey);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Unable to add entry %u\n", i);
ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
}
printf("Hash: Adding 0x%x keys\n", nr_flow);
}
int
em_check_ptype(int portid)
{
int i, ret;
int ptype_l3_ipv4_ext = 0;
int ptype_l3_ipv6_ext = 0;
int ptype_l4_tcp = 0;
int ptype_l4_udp = 0;
if (ret <= 0)
return 0;
uint32_t ptypes[ret];
for (i = 0; i < ret; ++i) {
switch (ptypes[i]) {
ptype_l3_ipv4_ext = 1;
break;
ptype_l3_ipv6_ext = 1;
break;
ptype_l4_tcp = 1;
break;
ptype_l4_udp = 1;
break;
}
}
if (ptype_l3_ipv4_ext == 0)
printf("port %d cannot parse RTE_PTYPE_L3_IPV4_EXT\n", portid);
if (ptype_l3_ipv6_ext == 0)
printf("port %d cannot parse RTE_PTYPE_L3_IPV6_EXT\n", portid);
if (!ptype_l3_ipv4_ext || !ptype_l3_ipv6_ext)
return 0;
if (ptype_l4_tcp == 0)
printf("port %d cannot parse RTE_PTYPE_L4_TCP\n", portid);
if (ptype_l4_udp == 0)
printf("port %d cannot parse RTE_PTYPE_L4_UDP\n", portid);
if (ptype_l4_tcp && ptype_l4_udp)
return 1;
return 0;
}
static inline void
{
void *l3;
int hdr_len;
} else
if (ipv6_hdr->
proto == IPPROTO_TCP)
else if (ipv6_hdr->
proto == IPPROTO_UDP)
else
}
}
uint16_t
struct rte_mbuf *pkts[], uint16_t nb_pkts,
{
unsigned i;
for (i = 0; i < nb_pkts; ++i)
em_parse_ptype(pkts[i]);
return nb_pkts;
}
int
em_main_loop(__attribute__((unused)) void *dummy)
{
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
unsigned lcore_id;
uint64_t prev_tsc, diff_tsc, cur_tsc;
int i, nb_rx;
uint8_t queueid;
uint16_t portid;
struct lcore_conf *qconf;
US_PER_S * BURST_TX_DRAIN_US;
prev_tsc = 0;
qconf = &lcore_conf[lcore_id];
if (qconf->n_rx_queue == 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 < qconf->n_rx_queue; i++) {
portid = qconf->rx_queue_list[i].port_id;
queueid = qconf->rx_queue_list[i].queue_id;
" -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
lcore_id, portid, queueid);
}
while (!force_quit) {
cur_tsc = rte_rdtsc();
diff_tsc = cur_tsc - prev_tsc;
for (i = 0; i < qconf->n_tx_port; ++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 < qconf->n_rx_queue; ++i) {
portid = qconf->rx_queue_list[i].port_id;
queueid = qconf->rx_queue_list[i].queue_id;
MAX_PKT_BURST);
if (nb_rx == 0)
continue;
#if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
l3fwd_em_send_packets(nb_rx, pkts_burst,
portid, qconf);
#else
l3fwd_em_no_opt_send_packets(nb_rx, pkts_burst,
portid, qconf);
#endif
}
}
return 0;
}
void
setup_hash(const int socketid)
{
.entries = L3FWD_HASH_ENTRIES,
.key_len = sizeof(union ipv4_5tuple_host),
.hash_func = ipv4_hash_crc,
.hash_func_init_val = 0,
};
.entries = L3FWD_HASH_ENTRIES,
.key_len = sizeof(union ipv6_5tuple_host),
.hash_func = ipv6_hash_crc,
.hash_func_init_val = 0,
};
char s[64];
snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
ipv4_l3fwd_hash_params.
name = s;
ipv4_l3fwd_em_lookup_struct[socketid] =
if (ipv4_l3fwd_em_lookup_struct[socketid] == NULL)
"Unable to create the l3fwd hash on socket %d\n",
socketid);
snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
ipv6_l3fwd_hash_params.
name = s;
ipv6_l3fwd_em_lookup_struct[socketid] =
if (ipv6_l3fwd_em_lookup_struct[socketid] == NULL)
"Unable to create the l3fwd hash on socket %d\n",
socketid);
if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
if (ipv6 == 0) {
populate_ipv4_many_flow_into_table(
ipv4_l3fwd_em_lookup_struct[socketid],
hash_entry_number);
} else {
populate_ipv6_many_flow_into_table(
ipv6_l3fwd_em_lookup_struct[socketid],
hash_entry_number);
}
} else {
if (ipv6 == 0) {
populate_ipv4_few_flow_into_table(
ipv4_l3fwd_em_lookup_struct[socketid]);
} else {
populate_ipv6_few_flow_into_table(
ipv6_l3fwd_em_lookup_struct[socketid]);
}
}
}
void *
em_get_ipv4_l3fwd_lookup_struct(const int socketid)
{
return ipv4_l3fwd_em_lookup_struct[socketid];
}
void *
em_get_ipv6_l3fwd_lookup_struct(const int socketid)
{
return ipv6_l3fwd_em_lookup_struct[socketid];
}