31 #include <rte_version.h> 34 #include <rte_mempool.h> 39 #define MBUF_SIZE (1500 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM) 40 #define POOL_SIZE (4 * 4096) 43 enum change_linkstate {
49 static int interface_set_carrier(
const char *name,
int status);
50 static int op_config_network_if(uint16_t port_id, uint8_t if_up);
51 #if RTE_VER_YEAR >= 18 52 static int op_config_mac_address(uint16_t port_id, uint8_t mac_addr[]);
55 static struct rte_mempool *kni_pool;
56 static struct rte_kni *kni_if;
57 static struct rte_kni_conf
conf;
58 static struct rte_kni_ops ops = {
60 .config_network_if = op_config_network_if,
61 #if RTE_VER_YEAR >= 18 62 .config_mac_address = op_config_mac_address,
65 static int change_linkstate = LST_NOOP;
75 if (rte_kni_init(1) != 0) {
76 fprintf(stderr,
"kni_init: rte_kni_init failed\n");
82 kni_pool = rte_mempool_create(
"tas_kni", POOL_SIZE, MBUF_SIZE, 32,
83 sizeof(
struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init, NULL,
84 rte_pktmbuf_init, NULL, rte_socket_id(), 0);
85 if (kni_pool == NULL) {
86 fprintf(stderr,
"kni_init: rte_mempool_create failed\n");
92 strncpy(
conf.name, config.
kni_name, RTE_KNI_NAMESIZE - 1);
93 conf.name[RTE_KNI_NAMESIZE - 1] = 0;
94 conf.mbuf_size = MBUF_SIZE;
95 #if RTE_VER_YEAR >= 18 101 if ((kni_if = rte_kni_alloc(kni_pool, &
conf, &ops)) == NULL) {
102 fprintf(stderr,
"kni_init: rte_kni_alloc failed\n");
117 if ((mb = rte_pktmbuf_alloc(kni_pool)) == NULL) {
118 fprintf(stderr,
"kni_packet: mbuf alloc failed\n");
122 if ((dst = rte_pktmbuf_append(mb, len)) == NULL) {
123 fprintf(stderr,
"kni_packet: mbuf append failed\n");
127 memcpy(dst, pkt, len);
128 if (rte_kni_tx_burst(kni_if, &mb, 1) != 1) {
129 fprintf(stderr,
"kni_packet: send failed\n");
130 rte_pktmbuf_free(mb);
145 if (change_linkstate != LST_NOOP) {
146 if (interface_set_carrier(config.
kni_name, change_linkstate == LST_UP)) {
147 fprintf(stderr,
"kni_poll: linkstate update failed\n");
149 change_linkstate = LST_NOOP;
152 rte_kni_handle_request(kni_if);
154 n = rte_kni_rx_burst(kni_if, &mb, 1);
157 memcpy(buf, rte_pktmbuf_mtod(mb,
void *), rte_pktmbuf_pkt_len(mb));
160 fprintf(stderr,
"kni_poll: send failed\n");
163 rte_pktmbuf_free(mb);
168 static int interface_set_carrier(
const char *name,
int status)
172 const char *st = status ?
"1" :
"0";
174 sprintf(path,
"/sys/class/net/%s/carrier", name);
176 if ((fd = open(path, O_WRONLY)) < 0) {
177 perror(
"interface_set_carrier: open failed");
181 if ((ret = write(fd, st, 2)) != 2) {
182 perror(
"interface_set_carrier: write failed");
191 static int op_config_network_if(uint16_t port_id, uint8_t if_up)
193 change_linkstate = (if_up ? LST_UP : LST_DOWN);
197 #if RTE_VER_YEAR >= 18 198 static int op_config_mac_address(uint16_t port_id, uint8_t mac_addr[])
void kni_packet(const void *pkt, uint16_t len)
int nicif_tx_alloc(uint16_t len, void **buf, uint32_t *opaque)
void nicif_tx_send(uint32_t opaque, int no_ts)