-
Notifications
You must be signed in to change notification settings - Fork 0
/
yourcode.cpp
104 lines (98 loc) · 3.45 KB
/
yourcode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "shared.h"
static struct rte_eth_conf port_conf = {
.rxmode = {
.offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM,
},
.txmode = {
.mq_mode = RTE_ETH_MQ_TX_NONE,
},
};
void userInit(int argc, char **argv) {
struct rte_eth_dev_info dev_info;
int portid;
rte_eal_init(argc, argv);
RTE_ETH_FOREACH_DEV(portid) {
/** Create Mempool **/
int socketid = rte_socket_id();
char s[128];
snprintf(s, sizeof(s), "mbuf_pool_%d", portid);
mempool[portid] = rte_pktmbuf_pool_create(
s, 512, MEMPOOL_CACHE_SIZE, 0,
RTE_MBUF_DEFAULT_BUF_SIZE, socketid
);
/** Init Device Settings **/
struct rte_eth_conf local_port_conf = port_conf;
rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
rte_eth_dev_info_get(portid, &dev_info);
uint16_t nb_rxd = 128;
uint16_t nb_txd = 128;
rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
/** Init RX Queue **/
rte_eth_rx_queue_setup(portid, queueid,
nb_rxd, socketid,
&dev_info.default_rxconf,
mempool[portid]);
/** Init TX Queue **/
rte_eth_tx_queue_setup(portid, 0, nb_txd, socketid, &dev_info.default_txconf);
/** Start Dev **/
rte_eth_promiscuous_enable(portid);
rte_eth_dev_start(portid);
}
}
int userLoop() {
int portid;
RTE_ETH_FOREACH_DEV(portid) {
if (tx_buffer[portid].size()) {
int sent = 0;
while (sent < tx_buffer[portid].size()) {
auto n_sent = rte_eth_tx_burst(portid, 0,
tx_buffer[portid].data() + sent,
tx_buffer[portid].size() - sent);
sent += n_sent;
}
}
tx_buffer[portid].clear();
}
RTE_ETH_FOREACH_DEV(portid) {
struct rte_mbuf* pkts[32];
auto n_pkt = rte_eth_rx_burst(portid, 0, pkts, 32);
for (int i = 0; i < n_pkts; i ++) {
auto pkt = pkts[i];
auto eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr*);
auto ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
ipv4_hdr->time_to_live--;
ipv4_hdr->hdr_checksum++;
uint32_t dst_ip = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
auto next_hop = userGetNextHop(dst_ip);
if (next_hop == -1)
rte_pktmbuf_free(pkt);
else
tx_buffer[next_hop].push_back(pkt);
}
}
}
int userAddLPMRule(uint32_t dst_ip, uint8_t cidr, uint8_t dst_port) {
if (!rule_table) {
char s[64];
snprintf(s, sizeof(s), "IPV4_L3FWD_LPM");
struct rte_lpm_config config_ipv4;
config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
config_ipv4.flags = 0;
rule_table = rte_lpm_create(s, socketid, &config_ipv4);
}
if (!rule_table) return -1;
return rte_lpm_add(rule_table, dst_ip, cidr, dst_port);
}
int userDelLPMRule(uint32_t dst_ip, uint8_t cidr) {
if (!rule_table) return -1;
return rte_lpm_delete(rule_table, dst_ip, cidr);
}
int userGetNextHop(uint32_t dst_ip, uint8_t cidr) {
if (!rule_table)
return -1;
uint32_t ret;
if (rte_lpm_lookup(rule_table, dst_ip, &ret) == 0) return (int)ret;
return -1;
}