xref: /f-stack/lib/ff_dpdk_if.c (revision b467d9c4)
1 /*
2  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *   list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 #include <assert.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29 #include <errno.h>
30 
31 #include <rte_common.h>
32 #include <rte_byteorder.h>
33 #include <rte_log.h>
34 #include <rte_memory.h>
35 #include <rte_memcpy.h>
36 #include <rte_memzone.h>
37 #include <rte_config.h>
38 #include <rte_eal.h>
39 #include <rte_pci.h>
40 #include <rte_mbuf.h>
41 #include <rte_memory.h>
42 #include <rte_lcore.h>
43 #include <rte_launch.h>
44 #include <rte_ethdev.h>
45 #include <rte_debug.h>
46 #include <rte_common.h>
47 #include <rte_ether.h>
48 #include <rte_malloc.h>
49 #include <rte_cycles.h>
50 #include <rte_timer.h>
51 #include <rte_thash.h>
52 #include <rte_ip.h>
53 #include <rte_tcp.h>
54 #include <rte_udp.h>
55 
56 #include "ff_dpdk_if.h"
57 #include "ff_dpdk_pcap.h"
58 #include "ff_dpdk_kni.h"
59 #include "ff_config.h"
60 #include "ff_veth.h"
61 #include "ff_host_interface.h"
62 #include "ff_msg.h"
63 #include "ff_api.h"
64 #include "ff_memory.h"
65 
66 #ifdef FF_KNI
67 #define KNI_MBUF_MAX 2048
68 #define KNI_QUEUE_SIZE 2048
69 
70 int enable_kni;
71 static int kni_accept;
72 #endif
73 
74 static int numa_on;
75 
76 static unsigned idle_sleep;
77 static unsigned pkt_tx_delay;
78 
79 static struct rte_timer freebsd_clock;
80 
81 // Mellanox Linux's driver key
82 static uint8_t default_rsskey_40bytes[40] = {
83     0xd1, 0x81, 0xc6, 0x2c, 0xf7, 0xf4, 0xdb, 0x5b,
84     0x19, 0x83, 0xa2, 0xfc, 0x94, 0x3e, 0x1a, 0xdb,
85     0xd9, 0x38, 0x9e, 0x6b, 0xd1, 0x03, 0x9c, 0x2c,
86     0xa7, 0x44, 0x99, 0xad, 0x59, 0x3d, 0x56, 0xd9,
87     0xf3, 0x25, 0x3c, 0x06, 0x2a, 0xdc, 0x1f, 0xfc
88 };
89 
90 static int use_rsskey_52bytes = 0;
91 static uint8_t default_rsskey_52bytes[52] = {
92     0x44, 0x39, 0x79, 0x6b, 0xb5, 0x4c, 0x50, 0x23,
93     0xb6, 0x75, 0xea, 0x5b, 0x12, 0x4f, 0x9f, 0x30,
94     0xb8, 0xa2, 0xc0, 0x3d, 0xdf, 0xdc, 0x4d, 0x02,
95     0xa0, 0x8c, 0x9b, 0x33, 0x4a, 0xf6, 0x4a, 0x4c,
96     0x05, 0xc6, 0xfa, 0x34, 0x39, 0x58, 0xd8, 0x55,
97     0x7d, 0x99, 0x58, 0x3a, 0xe1, 0x38, 0xc9, 0x2e,
98     0x81, 0x15, 0x03, 0x66
99 };
100 
101 struct lcore_conf lcore_conf;
102 
103 struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
104 
105 static struct rte_ring **dispatch_ring[RTE_MAX_ETHPORTS];
106 static dispatch_func_t packet_dispatcher;
107 
108 static uint16_t rss_reta_size[RTE_MAX_ETHPORTS];
109 
110 static inline int send_single_packet(struct rte_mbuf *m, uint8_t port);
111 
112 struct ff_msg_ring {
113     char ring_name[2][RTE_RING_NAMESIZE];
114     /* ring[0] for lcore recv msg, other send */
115     /* ring[1] for lcore send msg, other read */
116     struct rte_ring *ring[2];
117 } __rte_cache_aligned;
118 
119 static struct ff_msg_ring msg_ring[RTE_MAX_LCORE];
120 static struct rte_mempool *message_pool;
121 static struct ff_dpdk_if_context *veth_ctx[RTE_MAX_ETHPORTS];
122 
123 static struct ff_top_args ff_top_status;
124 static struct ff_traffic_args ff_traffic;
125 extern void ff_hardclock(void);
126 
127 static void
128 ff_hardclock_job(__rte_unused struct rte_timer *timer,
129     __rte_unused void *arg) {
130     ff_hardclock();
131     ff_update_current_ts();
132 }
133 
134 struct ff_dpdk_if_context *
135 ff_dpdk_register_if(void *sc, void *ifp, struct ff_port_cfg *cfg)
136 {
137     struct ff_dpdk_if_context *ctx;
138 
139     ctx = calloc(1, sizeof(struct ff_dpdk_if_context));
140     if (ctx == NULL)
141         return NULL;
142 
143     ctx->sc = sc;
144     ctx->ifp = ifp;
145     ctx->port_id = cfg->port_id;
146     ctx->hw_features = cfg->hw_features;
147 
148     return ctx;
149 }
150 
151 void
152 ff_dpdk_deregister_if(struct ff_dpdk_if_context *ctx)
153 {
154     free(ctx);
155 }
156 
157 static void
158 check_all_ports_link_status(void)
159 {
160     #define CHECK_INTERVAL 100 /* 100ms */
161     #define MAX_CHECK_TIME 90  /* 9s (90 * 100ms) in total */
162 
163     uint16_t portid;
164     uint8_t count, all_ports_up, print_flag = 0;
165     struct rte_eth_link link;
166 
167     printf("\nChecking link status");
168     fflush(stdout);
169 
170     int i, nb_ports;
171     nb_ports = ff_global_cfg.dpdk.nb_ports;
172     for (count = 0; count <= MAX_CHECK_TIME; count++) {
173         all_ports_up = 1;
174         for (i = 0; i < nb_ports; i++) {
175             uint16_t portid = ff_global_cfg.dpdk.portid_list[i];
176             memset(&link, 0, sizeof(link));
177             rte_eth_link_get_nowait(portid, &link);
178 
179             /* print link status if flag set */
180             if (print_flag == 1) {
181                 if (link.link_status) {
182                     printf("Port %d Link Up - speed %u "
183                         "Mbps - %s\n", (int)portid,
184                         (unsigned)link.link_speed,
185                         (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
186                         ("full-duplex") : ("half-duplex\n"));
187                 } else {
188                     printf("Port %d Link Down\n", (int)portid);
189                 }
190                 continue;
191             }
192             /* clear all_ports_up flag if any link down */
193             if (link.link_status == 0) {
194                 all_ports_up = 0;
195                 break;
196             }
197         }
198 
199         /* after finally printing all link status, get out */
200         if (print_flag == 1)
201             break;
202 
203         if (all_ports_up == 0) {
204             printf(".");
205             fflush(stdout);
206             rte_delay_ms(CHECK_INTERVAL);
207         }
208 
209         /* set the print_flag if all ports up or timeout */
210         if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
211             print_flag = 1;
212             printf("done\n");
213         }
214     }
215 }
216 
217 static int
218 init_lcore_conf(void)
219 {
220     uint8_t nb_dev_ports = rte_eth_dev_count_avail();
221     if (nb_dev_ports == 0) {
222         rte_exit(EXIT_FAILURE, "No probed ethernet devices\n");
223     }
224 
225     if (ff_global_cfg.dpdk.max_portid >= nb_dev_ports) {
226         rte_exit(EXIT_FAILURE, "this machine doesn't have port %d.\n",
227                  ff_global_cfg.dpdk.max_portid);
228     }
229 
230     lcore_conf.port_cfgs = ff_global_cfg.dpdk.port_cfgs;
231     lcore_conf.proc_id = ff_global_cfg.dpdk.proc_id;
232 
233     uint16_t proc_id;
234     for (proc_id = 0; proc_id < ff_global_cfg.dpdk.nb_procs; proc_id++) {
235         uint16_t lcore_id = ff_global_cfg.dpdk.proc_lcore[proc_id];
236         if (!lcore_config[lcore_id].detected) {
237             rte_exit(EXIT_FAILURE, "lcore %u unavailable\n", lcore_id);
238         }
239     }
240 
241     uint16_t socket_id = 0;
242     if (numa_on) {
243         socket_id = rte_lcore_to_socket_id(rte_lcore_id());
244     }
245 
246     lcore_conf.socket_id = socket_id;
247 
248     uint16_t lcore_id = ff_global_cfg.dpdk.proc_lcore[lcore_conf.proc_id];
249     int j;
250     for (j = 0; j < ff_global_cfg.dpdk.nb_ports; ++j) {
251         uint16_t port_id = ff_global_cfg.dpdk.portid_list[j];
252         struct ff_port_cfg *pconf = &ff_global_cfg.dpdk.port_cfgs[port_id];
253 
254         int queueid = -1;
255         int i;
256         for (i = 0; i < pconf->nb_lcores; i++) {
257             if (pconf->lcore_list[i] == lcore_id) {
258                 queueid = i;
259             }
260         }
261         if (queueid < 0) {
262             continue;
263         }
264         printf("lcore: %u, port: %u, queue: %u\n", lcore_id, port_id, queueid);
265         uint16_t nb_rx_queue = lcore_conf.nb_rx_queue;
266         lcore_conf.rx_queue_list[nb_rx_queue].port_id = port_id;
267         lcore_conf.rx_queue_list[nb_rx_queue].queue_id = queueid;
268         lcore_conf.nb_rx_queue++;
269 
270         lcore_conf.tx_queue_id[port_id] = queueid;
271         lcore_conf.tx_port_id[lcore_conf.nb_tx_port] = port_id;
272         lcore_conf.nb_tx_port++;
273 
274         lcore_conf.pcap[port_id] = pconf->pcap;
275         lcore_conf.nb_queue_list[port_id] = pconf->nb_lcores;
276     }
277 
278     if (lcore_conf.nb_rx_queue == 0) {
279         rte_exit(EXIT_FAILURE, "lcore %u has nothing to do\n", lcore_id);
280     }
281 
282     return 0;
283 }
284 
285 static int
286 init_mem_pool(void)
287 {
288     uint8_t nb_ports = ff_global_cfg.dpdk.nb_ports;
289     uint32_t nb_lcores = ff_global_cfg.dpdk.nb_procs;
290     uint32_t nb_tx_queue = nb_lcores;
291     uint32_t nb_rx_queue = lcore_conf.nb_rx_queue * nb_lcores;
292 
293     unsigned nb_mbuf = RTE_ALIGN_CEIL (
294         (nb_rx_queue*RX_QUEUE_SIZE          +
295         nb_ports*nb_lcores*MAX_PKT_BURST    +
296         nb_ports*nb_tx_queue*TX_QUEUE_SIZE  +
297         nb_lcores*MEMPOOL_CACHE_SIZE +
298 #ifdef FF_KNI
299         nb_ports*KNI_MBUF_MAX +
300         nb_ports*KNI_QUEUE_SIZE +
301 #endif
302         nb_lcores*nb_ports*DISPATCH_RING_SIZE),
303         (unsigned)8192);
304 
305     unsigned socketid = 0;
306     uint16_t i, lcore_id;
307     char s[64];
308 
309     for (i = 0; i < ff_global_cfg.dpdk.nb_procs; i++) {
310         lcore_id = ff_global_cfg.dpdk.proc_lcore[i];
311         if (numa_on) {
312             socketid = rte_lcore_to_socket_id(lcore_id);
313         }
314 
315         if (socketid >= NB_SOCKETS) {
316             rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
317                 socketid, i, NB_SOCKETS);
318         }
319 
320         if (pktmbuf_pool[socketid] != NULL) {
321             continue;
322         }
323 
324         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
325             snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
326             pktmbuf_pool[socketid] =
327                 rte_pktmbuf_pool_create(s, nb_mbuf,
328                     MEMPOOL_CACHE_SIZE, 0,
329                     RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
330         } else {
331             snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
332             pktmbuf_pool[socketid] = rte_mempool_lookup(s);
333         }
334 
335         if (pktmbuf_pool[socketid] == NULL) {
336             rte_exit(EXIT_FAILURE, "Cannot create mbuf pool on socket %d\n", socketid);
337         } else {
338             printf("create mbuf pool on socket %d\n", socketid);
339         }
340 
341 #ifdef FF_USE_PAGE_ARRAY
342         nb_mbuf = RTE_ALIGN_CEIL (
343             nb_ports*nb_lcores*MAX_PKT_BURST    +
344             nb_ports*nb_tx_queue*TX_QUEUE_SIZE  +
345             nb_lcores*MEMPOOL_CACHE_SIZE,
346             (unsigned)4096);
347         ff_init_ref_pool(nb_mbuf, socketid);
348 #endif
349     }
350 
351     return 0;
352 }
353 
354 static struct rte_ring *
355 create_ring(const char *name, unsigned count, int socket_id, unsigned flags)
356 {
357     struct rte_ring *ring;
358 
359     if (name == NULL) {
360         rte_exit(EXIT_FAILURE, "create ring failed, no name!\n");
361     }
362 
363     if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
364         ring = rte_ring_create(name, count, socket_id, flags);
365     } else {
366         ring = rte_ring_lookup(name);
367     }
368 
369     if (ring == NULL) {
370         rte_exit(EXIT_FAILURE, "create ring:%s failed!\n", name);
371     }
372 
373     return ring;
374 }
375 
376 static int
377 init_dispatch_ring(void)
378 {
379     int j;
380     char name_buf[RTE_RING_NAMESIZE];
381     int queueid;
382 
383     unsigned socketid = lcore_conf.socket_id;
384 
385     /* Create ring according to ports actually being used. */
386     int nb_ports = ff_global_cfg.dpdk.nb_ports;
387     for (j = 0; j < nb_ports; j++) {
388         uint16_t portid = ff_global_cfg.dpdk.portid_list[j];
389         struct ff_port_cfg *pconf = &ff_global_cfg.dpdk.port_cfgs[portid];
390         int nb_queues = pconf->nb_lcores;
391         if (dispatch_ring[portid] == NULL) {
392             snprintf(name_buf, RTE_RING_NAMESIZE, "ring_ptr_p%d", portid);
393 
394             dispatch_ring[portid] = rte_zmalloc(name_buf,
395                 sizeof(struct rte_ring *) * nb_queues,
396                 RTE_CACHE_LINE_SIZE);
397             if (dispatch_ring[portid] == NULL) {
398                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%s (struct rte_ring*)) "
399                     "failed\n", name_buf);
400             }
401         }
402 
403         for(queueid = 0; queueid < nb_queues; ++queueid) {
404             snprintf(name_buf, RTE_RING_NAMESIZE, "dispatch_ring_p%d_q%d",
405                 portid, queueid);
406             dispatch_ring[portid][queueid] = create_ring(name_buf,
407                 DISPATCH_RING_SIZE, socketid, RING_F_SC_DEQ);
408 
409             if (dispatch_ring[portid][queueid] == NULL)
410                 rte_panic("create ring:%s failed!\n", name_buf);
411 
412             printf("create ring:%s success, %u ring entries are now free!\n",
413                 name_buf, rte_ring_free_count(dispatch_ring[portid][queueid]));
414         }
415     }
416 
417     return 0;
418 }
419 
420 static void
421 ff_msg_init(struct rte_mempool *mp,
422     __attribute__((unused)) void *opaque_arg,
423     void *obj, __attribute__((unused)) unsigned i)
424 {
425     struct ff_msg *msg = (struct ff_msg *)obj;
426     msg->msg_type = FF_UNKNOWN;
427     msg->buf_addr = (char *)msg + sizeof(struct ff_msg);
428     msg->buf_len = mp->elt_size - sizeof(struct ff_msg);
429 }
430 
431 static int
432 init_msg_ring(void)
433 {
434     uint16_t i;
435     uint16_t nb_procs = ff_global_cfg.dpdk.nb_procs;
436     unsigned socketid = lcore_conf.socket_id;
437 
438     /* Create message buffer pool */
439     if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
440         message_pool = rte_mempool_create(FF_MSG_POOL,
441            MSG_RING_SIZE * 2 * nb_procs,
442            MAX_MSG_BUF_SIZE, MSG_RING_SIZE / 2, 0,
443            NULL, NULL, ff_msg_init, NULL,
444            socketid, 0);
445     } else {
446         message_pool = rte_mempool_lookup(FF_MSG_POOL);
447     }
448 
449     if (message_pool == NULL) {
450         rte_panic("Create msg mempool failed\n");
451     }
452 
453     for(i = 0; i < nb_procs; ++i) {
454         snprintf(msg_ring[i].ring_name[0], RTE_RING_NAMESIZE,
455             "%s%u", FF_MSG_RING_IN, i);
456         snprintf(msg_ring[i].ring_name[1], RTE_RING_NAMESIZE,
457             "%s%u", FF_MSG_RING_OUT, i);
458 
459         msg_ring[i].ring[0] = create_ring(msg_ring[i].ring_name[0],
460             MSG_RING_SIZE, socketid, RING_F_SP_ENQ | RING_F_SC_DEQ);
461         if (msg_ring[i].ring[0] == NULL)
462             rte_panic("create ring::%s failed!\n", msg_ring[i].ring_name[0]);
463 
464         msg_ring[i].ring[1] = create_ring(msg_ring[i].ring_name[1],
465             MSG_RING_SIZE, socketid, RING_F_SP_ENQ | RING_F_SC_DEQ);
466         if (msg_ring[i].ring[1] == NULL)
467             rte_panic("create ring::%s failed!\n", msg_ring[i].ring_name[0]);
468     }
469 
470     return 0;
471 }
472 
473 #ifdef FF_KNI
474 static int
475 init_kni(void)
476 {
477     int nb_ports = rte_eth_dev_count_avail();
478     kni_accept = 0;
479     if(strcasecmp(ff_global_cfg.kni.method, "accept") == 0)
480         kni_accept = 1;
481 
482     ff_kni_init(nb_ports, ff_global_cfg.kni.tcp_port,
483         ff_global_cfg.kni.udp_port);
484 
485     unsigned socket_id = lcore_conf.socket_id;
486     struct rte_mempool *mbuf_pool = pktmbuf_pool[socket_id];
487 
488     nb_ports = ff_global_cfg.dpdk.nb_ports;
489     int i, ret;
490     for (i = 0; i < nb_ports; i++) {
491         uint16_t port_id = ff_global_cfg.dpdk.portid_list[i];
492         ff_kni_alloc(port_id, socket_id, mbuf_pool, KNI_QUEUE_SIZE);
493     }
494 
495     return 0;
496 }
497 #endif
498 
499 static void
500 set_rss_table(uint16_t port_id, uint16_t reta_size, uint16_t nb_queues)
501 {
502     if (reta_size == 0) {
503         return;
504     }
505 
506     int reta_conf_size = RTE_MAX(1, reta_size / RTE_RETA_GROUP_SIZE);
507     struct rte_eth_rss_reta_entry64 reta_conf[reta_conf_size];
508 
509     /* config HW indirection table */
510     unsigned i, j, hash=0;
511     for (i = 0; i < reta_conf_size; i++) {
512         reta_conf[i].mask = ~0ULL;
513         for (j = 0; j < RTE_RETA_GROUP_SIZE; j++) {
514             reta_conf[i].reta[j] = hash++ % nb_queues;
515         }
516     }
517 
518     if (rte_eth_dev_rss_reta_update(port_id, reta_conf, reta_size)) {
519         rte_exit(EXIT_FAILURE, "port[%d], failed to update rss table\n",
520             port_id);
521     }
522 }
523 
524 static int
525 init_port_start(void)
526 {
527     int nb_ports = ff_global_cfg.dpdk.nb_ports;
528     unsigned socketid = 0;
529     struct rte_mempool *mbuf_pool;
530     uint16_t i;
531 
532     for (i = 0; i < nb_ports; i++) {
533         uint16_t port_id = ff_global_cfg.dpdk.portid_list[i];
534         struct ff_port_cfg *pconf = &ff_global_cfg.dpdk.port_cfgs[port_id];
535         uint16_t nb_queues = pconf->nb_lcores;
536 
537         struct rte_eth_dev_info dev_info;
538         struct rte_eth_conf port_conf = {0};
539         struct rte_eth_rxconf rxq_conf;
540         struct rte_eth_txconf txq_conf;
541 
542         rte_eth_dev_info_get(port_id, &dev_info);
543 
544         if (nb_queues > dev_info.max_rx_queues) {
545             rte_exit(EXIT_FAILURE, "num_procs[%d] bigger than max_rx_queues[%d]\n",
546                 nb_queues,
547                 dev_info.max_rx_queues);
548         }
549 
550         if (nb_queues > dev_info.max_tx_queues) {
551             rte_exit(EXIT_FAILURE, "num_procs[%d] bigger than max_tx_queues[%d]\n",
552                 nb_queues,
553                 dev_info.max_tx_queues);
554         }
555 
556         struct ether_addr addr;
557         rte_eth_macaddr_get(port_id, &addr);
558         printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
559                    " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
560                 (unsigned)port_id,
561                 addr.addr_bytes[0], addr.addr_bytes[1],
562                 addr.addr_bytes[2], addr.addr_bytes[3],
563                 addr.addr_bytes[4], addr.addr_bytes[5]);
564 
565         rte_memcpy(pconf->mac,
566             addr.addr_bytes, ETHER_ADDR_LEN);
567 
568         /* Set RSS mode */
569         uint64_t default_rss_hf = ETH_RSS_PROTO_MASK;
570         port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
571         port_conf.rx_adv_conf.rss_conf.rss_hf = default_rss_hf;
572         if (dev_info.hash_key_size == 52) {
573             port_conf.rx_adv_conf.rss_conf.rss_key = default_rsskey_52bytes;
574             port_conf.rx_adv_conf.rss_conf.rss_key_len = 52;
575 	    use_rsskey_52bytes = 1;
576         }else{
577             port_conf.rx_adv_conf.rss_conf.rss_key = default_rsskey_40bytes;
578             port_conf.rx_adv_conf.rss_conf.rss_key_len = 40;
579         }
580         port_conf.rx_adv_conf.rss_conf.rss_hf &= dev_info.flow_type_rss_offloads;
581         if (port_conf.rx_adv_conf.rss_conf.rss_hf !=
582                 ETH_RSS_PROTO_MASK) {
583             printf("Port %u modified RSS hash function based on hardware support,"
584                     "requested:%#"PRIx64" configured:%#"PRIx64"\n",
585                     port_id, default_rss_hf,
586                     port_conf.rx_adv_conf.rss_conf.rss_hf);
587         }
588 
589         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) {
590             port_conf.txmode.offloads |=
591                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
592         }
593 
594         /* Set Rx VLAN stripping */
595         if (ff_global_cfg.dpdk.vlan_strip) {
596             if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_VLAN_STRIP) {
597                 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
598             }
599         }
600 
601         /* Enable HW CRC stripping */
602         port_conf.rxmode.offloads &= ~DEV_RX_OFFLOAD_KEEP_CRC;
603 
604         /* FIXME: Enable TCP LRO ?*/
605         #if 0
606         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO) {
607             printf("LRO is supported\n");
608             port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TCP_LRO;
609             pconf->hw_features.rx_lro = 1;
610         }
611         #endif
612 
613         /* Set Rx checksum checking */
614         if ((dev_info.rx_offload_capa & DEV_RX_OFFLOAD_IPV4_CKSUM) &&
615             (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_UDP_CKSUM) &&
616             (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_CKSUM)) {
617             printf("RX checksum offload supported\n");
618             port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM;
619             pconf->hw_features.rx_csum = 1;
620         }
621 
622         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)) {
623             printf("TX ip checksum offload supported\n");
624             port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
625             pconf->hw_features.tx_csum_ip = 1;
626         }
627 
628         if ((dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_CKSUM) &&
629             (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_CKSUM)) {
630             printf("TX TCP&UDP checksum offload supported\n");
631             port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM | DEV_TX_OFFLOAD_TCP_CKSUM;
632             pconf->hw_features.tx_csum_l4 = 1;
633         }
634 
635         if (ff_global_cfg.dpdk.tso) {
636             if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) {
637                 printf("TSO is supported\n");
638                 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_TSO;
639                 pconf->hw_features.tx_tso = 1;
640             }
641         } else {
642             printf("TSO is disabled\n");
643         }
644 
645         if (dev_info.reta_size) {
646             /* reta size must be power of 2 */
647             assert((dev_info.reta_size & (dev_info.reta_size - 1)) == 0);
648 
649             rss_reta_size[port_id] = dev_info.reta_size;
650             printf("port[%d]: rss table size: %d\n", port_id,
651                 dev_info.reta_size);
652         }
653 
654         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
655             continue;
656         }
657 
658         int ret = rte_eth_dev_configure(port_id, nb_queues, nb_queues, &port_conf);
659         if (ret != 0) {
660             return ret;
661         }
662 
663         static uint16_t nb_rxd = RX_QUEUE_SIZE;
664         static uint16_t nb_txd = TX_QUEUE_SIZE;
665         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
666         if (ret < 0)
667             printf("Could not adjust number of descriptors "
668                     "for port%u (%d)\n", (unsigned)port_id, ret);
669 
670         uint16_t q;
671         for (q = 0; q < nb_queues; q++) {
672             if (numa_on) {
673                 uint16_t lcore_id = lcore_conf.port_cfgs[port_id].lcore_list[q];
674                 socketid = rte_lcore_to_socket_id(lcore_id);
675             }
676             mbuf_pool = pktmbuf_pool[socketid];
677 
678             txq_conf = dev_info.default_txconf;
679             txq_conf.offloads = port_conf.txmode.offloads;
680             ret = rte_eth_tx_queue_setup(port_id, q, nb_txd,
681                 socketid, &txq_conf);
682             if (ret < 0) {
683                 return ret;
684             }
685 
686             rxq_conf = dev_info.default_rxconf;
687             rxq_conf.offloads = port_conf.rxmode.offloads;
688             ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd,
689                 socketid, &rxq_conf, mbuf_pool);
690             if (ret < 0) {
691                 return ret;
692             }
693         }
694 
695         ret = rte_eth_dev_start(port_id);
696         if (ret < 0) {
697             return ret;
698         }
699 
700         if (nb_queues > 1) {
701             /* set HW rss hash function to Toeplitz. */
702             if (!rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_HASH)) {
703                 struct rte_eth_hash_filter_info info = {0};
704                 info.info_type = RTE_ETH_HASH_FILTER_GLOBAL_CONFIG;
705                 info.info.global_conf.hash_func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
706 
707                 if (rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_HASH,
708                     RTE_ETH_FILTER_SET, &info) < 0) {
709                     rte_exit(EXIT_FAILURE, "port[%d] set hash func failed\n",
710                         port_id);
711                 }
712             }
713 
714             set_rss_table(port_id, dev_info.reta_size, nb_queues);
715         }
716 
717         /* Enable RX in promiscuous mode for the Ethernet device. */
718         if (ff_global_cfg.dpdk.promiscuous) {
719             rte_eth_promiscuous_enable(port_id);
720             ret = rte_eth_promiscuous_get(port_id);
721             if (ret == 1) {
722                 printf("set port %u to promiscuous mode ok\n", port_id);
723             } else {
724                 printf("set port %u to promiscuous mode error\n", port_id);
725             }
726         }
727 
728         /* Enable pcap dump */
729         if (pconf->pcap) {
730             ff_enable_pcap(pconf->pcap);
731         }
732     }
733 
734     if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
735         check_all_ports_link_status();
736     }
737 
738     return 0;
739 }
740 
741 static int
742 init_clock(void)
743 {
744     rte_timer_subsystem_init();
745     uint64_t hz = rte_get_timer_hz();
746     uint64_t intrs = MS_PER_S/ff_global_cfg.freebsd.hz;
747     uint64_t tsc = (hz + MS_PER_S - 1) / MS_PER_S*intrs;
748 
749     rte_timer_init(&freebsd_clock);
750     rte_timer_reset(&freebsd_clock, tsc, PERIODICAL,
751         rte_lcore_id(), &ff_hardclock_job, NULL);
752 
753     ff_update_current_ts();
754 
755     return 0;
756 }
757 
758 int
759 ff_dpdk_init(int argc, char **argv)
760 {
761     if (ff_global_cfg.dpdk.nb_procs < 1 ||
762         ff_global_cfg.dpdk.nb_procs > RTE_MAX_LCORE ||
763         ff_global_cfg.dpdk.proc_id >= ff_global_cfg.dpdk.nb_procs ||
764         ff_global_cfg.dpdk.proc_id < 0) {
765         printf("param num_procs[%d] or proc_id[%d] error!\n",
766             ff_global_cfg.dpdk.nb_procs,
767             ff_global_cfg.dpdk.proc_id);
768         exit(1);
769     }
770 
771     int ret = rte_eal_init(argc, argv);
772     if (ret < 0) {
773         rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
774     }
775 
776     numa_on = ff_global_cfg.dpdk.numa_on;
777 
778     idle_sleep = ff_global_cfg.dpdk.idle_sleep;
779     pkt_tx_delay = ff_global_cfg.dpdk.pkt_tx_delay > BURST_TX_DRAIN_US ? \
780         BURST_TX_DRAIN_US : ff_global_cfg.dpdk.pkt_tx_delay;
781 
782     init_lcore_conf();
783 
784     init_mem_pool();
785 
786     init_dispatch_ring();
787 
788     init_msg_ring();
789 
790 #ifdef FF_KNI
791     enable_kni = ff_global_cfg.kni.enable;
792     if (enable_kni) {
793         init_kni();
794     }
795 #endif
796 
797 #ifdef FF_USE_PAGE_ARRAY
798     ff_mmap_init();
799 #endif
800 
801     ret = init_port_start();
802     if (ret < 0) {
803         rte_exit(EXIT_FAILURE, "init_port_start failed\n");
804     }
805 
806     init_clock();
807 
808     return 0;
809 }
810 
811 static void
812 ff_veth_input(const struct ff_dpdk_if_context *ctx, struct rte_mbuf *pkt)
813 {
814     uint8_t rx_csum = ctx->hw_features.rx_csum;
815     if (rx_csum) {
816         if (pkt->ol_flags & (PKT_RX_IP_CKSUM_BAD | PKT_RX_L4_CKSUM_BAD)) {
817             rte_pktmbuf_free(pkt);
818             return;
819         }
820     }
821 
822     /*
823      * FIXME: should we save pkt->vlan_tci
824      * if (pkt->ol_flags & PKT_RX_VLAN_PKT)
825      */
826 
827     void *data = rte_pktmbuf_mtod(pkt, void*);
828     uint16_t len = rte_pktmbuf_data_len(pkt);
829 
830     void *hdr = ff_mbuf_gethdr(pkt, pkt->pkt_len, data, len, rx_csum);
831     if (hdr == NULL) {
832         rte_pktmbuf_free(pkt);
833         return;
834     }
835 
836     struct rte_mbuf *pn = pkt->next;
837     void *prev = hdr;
838     while(pn != NULL) {
839         data = rte_pktmbuf_mtod(pn, void*);
840         len = rte_pktmbuf_data_len(pn);
841 
842         void *mb = ff_mbuf_get(prev, data, len);
843         if (mb == NULL) {
844             ff_mbuf_free(hdr);
845             rte_pktmbuf_free(pkt);
846             return;
847         }
848         pn = pn->next;
849         prev = mb;
850     }
851 
852     ff_veth_process_packet(ctx->ifp, hdr);
853 }
854 
855 static enum FilterReturn
856 protocol_filter(const void *data, uint16_t len)
857 {
858     if(len < ETHER_HDR_LEN)
859         return FILTER_UNKNOWN;
860 
861     const struct ether_hdr *hdr;
862     hdr = (const struct ether_hdr *)data;
863     uint16_t eth_frame_type = rte_be_to_cpu_16(hdr->ether_type);
864 
865     if(eth_frame_type == ETHER_TYPE_ARP)
866         return FILTER_ARP;
867 
868 #ifdef INET6
869     if (eth_frame_type == ETHER_TYPE_IPv6) {
870         return ff_kni_proto_filter(data + ETHER_HDR_LEN,
871             len - ETHER_HDR_LEN, eth_frame_type);
872     }
873 #endif
874 
875 #ifndef FF_KNI
876     return FILTER_UNKNOWN;
877 #else
878     if (!enable_kni) {
879         return FILTER_UNKNOWN;
880     }
881 
882     if(eth_frame_type != ETHER_TYPE_IPv4)
883         return FILTER_UNKNOWN;
884 
885     return ff_kni_proto_filter(data + ETHER_HDR_LEN,
886         len - ETHER_HDR_LEN, eth_frame_type);
887 #endif
888 }
889 
890 static inline void
891 pktmbuf_deep_attach(struct rte_mbuf *mi, const struct rte_mbuf *m)
892 {
893     struct rte_mbuf *md;
894     void *src, *dst;
895 
896     dst = rte_pktmbuf_mtod(mi, void *);
897     src = rte_pktmbuf_mtod(m, void *);
898 
899     mi->data_len = m->data_len;
900     rte_memcpy(dst, src, m->data_len);
901 
902     mi->port = m->port;
903     mi->vlan_tci = m->vlan_tci;
904     mi->vlan_tci_outer = m->vlan_tci_outer;
905     mi->tx_offload = m->tx_offload;
906     mi->hash = m->hash;
907     mi->ol_flags = m->ol_flags;
908     mi->packet_type = m->packet_type;
909 }
910 
911 /* copied from rte_pktmbuf_clone */
912 static inline struct rte_mbuf *
913 pktmbuf_deep_clone(const struct rte_mbuf *md,
914     struct rte_mempool *mp)
915 {
916     struct rte_mbuf *mc, *mi, **prev;
917     uint32_t pktlen;
918     uint8_t nseg;
919 
920     if (unlikely ((mc = rte_pktmbuf_alloc(mp)) == NULL))
921         return NULL;
922 
923     mi = mc;
924     prev = &mi->next;
925     pktlen = md->pkt_len;
926     nseg = 0;
927 
928     do {
929         nseg++;
930         pktmbuf_deep_attach(mi, md);
931         *prev = mi;
932         prev = &mi->next;
933     } while ((md = md->next) != NULL &&
934         (mi = rte_pktmbuf_alloc(mp)) != NULL);
935 
936     *prev = NULL;
937     mc->nb_segs = nseg;
938     mc->pkt_len = pktlen;
939 
940     /* Allocation of new indirect segment failed */
941     if (unlikely (mi == NULL)) {
942         rte_pktmbuf_free(mc);
943         return NULL;
944     }
945 
946     __rte_mbuf_sanity_check(mc, 1);
947     return mc;
948 }
949 
950 static inline void
951 process_packets(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **bufs,
952     uint16_t count, const struct ff_dpdk_if_context *ctx, int pkts_from_ring)
953 {
954     struct lcore_conf *qconf = &lcore_conf;
955     uint16_t nb_queues = qconf->nb_queue_list[port_id];
956 
957     uint16_t i;
958     for (i = 0; i < count; i++) {
959         struct rte_mbuf *rtem = bufs[i];
960 
961         if (unlikely(qconf->pcap[port_id] != NULL)) {
962             if (!pkts_from_ring) {
963                 ff_dump_packets(qconf->pcap[port_id], rtem);
964             }
965         }
966 
967         void *data = rte_pktmbuf_mtod(rtem, void*);
968         uint16_t len = rte_pktmbuf_data_len(rtem);
969 
970         if (!pkts_from_ring) {
971             ff_traffic.rx_packets++;
972             ff_traffic.rx_bytes += len;
973         }
974 
975         if (!pkts_from_ring && packet_dispatcher) {
976             int ret = (*packet_dispatcher)(data, &len, queue_id, nb_queues);
977             if (ret == FF_DISPATCH_RESPONSE) {
978                 rte_pktmbuf_pkt_len(rtem) = rte_pktmbuf_data_len(rtem) = len;
979                 send_single_packet(rtem, port_id);
980                 continue;
981             }
982 
983             if (ret == FF_DISPATCH_ERROR || ret >= nb_queues) {
984                 rte_pktmbuf_free(rtem);
985                 continue;
986             }
987 
988             if (ret != queue_id) {
989                 ret = rte_ring_enqueue(dispatch_ring[port_id][ret], rtem);
990                 if (ret < 0)
991                     rte_pktmbuf_free(rtem);
992 
993                 continue;
994             }
995         }
996 
997         enum FilterReturn filter = protocol_filter(data, len);
998 #ifdef INET6
999         if (filter == FILTER_ARP || filter == FILTER_NDP) {
1000 #else
1001         if (filter == FILTER_ARP) {
1002 #endif
1003             struct rte_mempool *mbuf_pool;
1004             struct rte_mbuf *mbuf_clone;
1005             if (!pkts_from_ring) {
1006                 uint16_t j;
1007                 for(j = 0; j < nb_queues; ++j) {
1008                     if(j == queue_id)
1009                         continue;
1010 
1011                     unsigned socket_id = 0;
1012                     if (numa_on) {
1013                         uint16_t lcore_id = qconf->port_cfgs[port_id].lcore_list[j];
1014                         socket_id = rte_lcore_to_socket_id(lcore_id);
1015                     }
1016                     mbuf_pool = pktmbuf_pool[socket_id];
1017                     mbuf_clone = pktmbuf_deep_clone(rtem, mbuf_pool);
1018                     if(mbuf_clone) {
1019                         int ret = rte_ring_enqueue(dispatch_ring[port_id][j],
1020                             mbuf_clone);
1021                         if (ret < 0)
1022                             rte_pktmbuf_free(mbuf_clone);
1023                     }
1024                 }
1025             }
1026 
1027 #ifdef FF_KNI
1028             if (enable_kni && rte_eal_process_type() == RTE_PROC_PRIMARY) {
1029                 mbuf_pool = pktmbuf_pool[qconf->socket_id];
1030                 mbuf_clone = pktmbuf_deep_clone(rtem, mbuf_pool);
1031                 if(mbuf_clone) {
1032                     ff_kni_enqueue(port_id, mbuf_clone);
1033                 }
1034             }
1035 #endif
1036             ff_veth_input(ctx, rtem);
1037 #ifdef FF_KNI
1038         } else if (enable_kni &&
1039             ((filter == FILTER_KNI && kni_accept) ||
1040             (filter == FILTER_UNKNOWN && !kni_accept)) ) {
1041             ff_kni_enqueue(port_id, rtem);
1042 #endif
1043         } else {
1044             ff_veth_input(ctx, rtem);
1045         }
1046     }
1047 }
1048 
1049 static inline int
1050 process_dispatch_ring(uint16_t port_id, uint16_t queue_id,
1051     struct rte_mbuf **pkts_burst, const struct ff_dpdk_if_context *ctx)
1052 {
1053     /* read packet from ring buf and to process */
1054     uint16_t nb_rb;
1055     nb_rb = rte_ring_dequeue_burst(dispatch_ring[port_id][queue_id],
1056         (void **)pkts_burst, MAX_PKT_BURST, NULL);
1057 
1058     if(nb_rb > 0) {
1059         process_packets(port_id, queue_id, pkts_burst, nb_rb, ctx, 1);
1060     }
1061 
1062     return 0;
1063 }
1064 
1065 static inline void
1066 handle_sysctl_msg(struct ff_msg *msg)
1067 {
1068     int ret = ff_sysctl(msg->sysctl.name, msg->sysctl.namelen,
1069         msg->sysctl.old, msg->sysctl.oldlenp, msg->sysctl.new,
1070         msg->sysctl.newlen);
1071 
1072     if (ret < 0) {
1073         msg->result = errno;
1074     } else {
1075         msg->result = 0;
1076     }
1077 }
1078 
1079 static inline void
1080 handle_ioctl_msg(struct ff_msg *msg)
1081 {
1082     int fd, ret;
1083 #ifdef INET6
1084     if (msg->msg_type == FF_IOCTL6) {
1085         fd = ff_socket(AF_INET6, SOCK_DGRAM, 0);
1086     } else
1087 #endif
1088         fd = ff_socket(AF_INET, SOCK_DGRAM, 0);
1089 
1090     if (fd < 0) {
1091         ret = -1;
1092         goto done;
1093     }
1094 
1095     ret = ff_ioctl_freebsd(fd, msg->ioctl.cmd, msg->ioctl.data);
1096 
1097     ff_close(fd);
1098 
1099 done:
1100     if (ret < 0) {
1101         msg->result = errno;
1102     } else {
1103         msg->result = 0;
1104     }
1105 }
1106 
1107 static inline void
1108 handle_route_msg(struct ff_msg *msg)
1109 {
1110     int ret = ff_rtioctl(msg->route.fib, msg->route.data,
1111         &msg->route.len, msg->route.maxlen);
1112     if (ret < 0) {
1113         msg->result = errno;
1114     } else {
1115         msg->result = 0;
1116     }
1117 }
1118 
1119 static inline void
1120 handle_top_msg(struct ff_msg *msg)
1121 {
1122     msg->top = ff_top_status;
1123     msg->result = 0;
1124 }
1125 
1126 #ifdef FF_NETGRAPH
1127 static inline void
1128 handle_ngctl_msg(struct ff_msg *msg)
1129 {
1130     int ret = ff_ngctl(msg->ngctl.cmd, msg->ngctl.data);
1131     if (ret < 0) {
1132         msg->result = errno;
1133     } else {
1134         msg->result = 0;
1135         msg->ngctl.ret = ret;
1136     }
1137 }
1138 #endif
1139 
1140 #ifdef FF_IPFW
1141 static inline void
1142 handle_ipfw_msg(struct ff_msg *msg)
1143 {
1144     int fd, ret;
1145     fd = ff_socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
1146     if (fd < 0) {
1147         ret = -1;
1148         goto done;
1149     }
1150 
1151     switch (msg->ipfw.cmd) {
1152         case FF_IPFW_GET:
1153             ret = ff_getsockopt_freebsd(fd, msg->ipfw.level,
1154                 msg->ipfw.optname, msg->ipfw.optval,
1155                 msg->ipfw.optlen);
1156             break;
1157         case FF_IPFW_SET:
1158             ret = ff_setsockopt_freebsd(fd, msg->ipfw.level,
1159                 msg->ipfw.optname, msg->ipfw.optval,
1160                 *(msg->ipfw.optlen));
1161             break;
1162         default:
1163             ret = -1;
1164             errno = ENOTSUP;
1165             break;
1166     }
1167 
1168     ff_close(fd);
1169 
1170 done:
1171     if (ret < 0) {
1172         msg->result = errno;
1173     } else {
1174         msg->result = 0;
1175     }
1176 }
1177 #endif
1178 
1179 static inline void
1180 handle_traffic_msg(struct ff_msg *msg)
1181 {
1182     msg->traffic = ff_traffic;
1183     msg->result = 0;
1184 }
1185 
1186 static inline void
1187 handle_default_msg(struct ff_msg *msg)
1188 {
1189     msg->result = ENOTSUP;
1190 }
1191 
1192 static inline void
1193 handle_msg(struct ff_msg *msg, uint16_t proc_id)
1194 {
1195     switch (msg->msg_type) {
1196         case FF_SYSCTL:
1197             handle_sysctl_msg(msg);
1198             break;
1199         case FF_IOCTL:
1200 #ifdef INET6
1201         case FF_IOCTL6:
1202 #endif
1203             handle_ioctl_msg(msg);
1204             break;
1205         case FF_ROUTE:
1206             handle_route_msg(msg);
1207             break;
1208         case FF_TOP:
1209             handle_top_msg(msg);
1210             break;
1211 #ifdef FF_NETGRAPH
1212         case FF_NGCTL:
1213             handle_ngctl_msg(msg);
1214             break;
1215 #endif
1216 #ifdef FF_IPFW
1217         case FF_IPFW_CTL:
1218             handle_ipfw_msg(msg);
1219             break;
1220 #endif
1221         case FF_TRAFFIC:
1222             handle_traffic_msg(msg);
1223             break;
1224         default:
1225             handle_default_msg(msg);
1226             break;
1227     }
1228     rte_ring_enqueue(msg_ring[proc_id].ring[1], msg);
1229 }
1230 
1231 static inline int
1232 process_msg_ring(uint16_t proc_id)
1233 {
1234     void *msg;
1235     int ret = rte_ring_dequeue(msg_ring[proc_id].ring[0], &msg);
1236 
1237     if (unlikely(ret == 0)) {
1238         handle_msg((struct ff_msg *)msg, proc_id);
1239     }
1240 
1241     return 0;
1242 }
1243 
1244 /* Send burst of packets on an output interface */
1245 static inline int
1246 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
1247 {
1248     struct rte_mbuf **m_table;
1249     int ret;
1250     uint16_t queueid;
1251 
1252     queueid = qconf->tx_queue_id[port];
1253     m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
1254 
1255     if (unlikely(qconf->pcap[port] != NULL)) {
1256         uint16_t i;
1257         for (i = 0; i < n; i++) {
1258             ff_dump_packets(qconf->pcap[port], m_table[i]);
1259         }
1260     }
1261 
1262     ret = rte_eth_tx_burst(port, queueid, m_table, n);
1263     ff_traffic.tx_packets += ret;
1264     uint16_t i;
1265     for (i = 0; i < ret; i++) {
1266         ff_traffic.tx_bytes += rte_pktmbuf_pkt_len(m_table[i]);
1267 #ifdef FF_USE_PAGE_ARRAY
1268         if (qconf->tx_mbufs[port].bsd_m_table[i])
1269             ff_enq_tx_bsdmbuf(port, qconf->tx_mbufs[port].bsd_m_table[i], m_table[i]->nb_segs);
1270 #endif
1271     }
1272     if (unlikely(ret < n)) {
1273         do {
1274             rte_pktmbuf_free(m_table[ret]);
1275 #ifdef FF_USE_PAGE_ARRAY
1276             if ( qconf->tx_mbufs[port].bsd_m_table[ret] )
1277                 ff_mbuf_free(qconf->tx_mbufs[port].bsd_m_table[ret]);
1278 #endif
1279         } while (++ret < n);
1280     }
1281     return 0;
1282 }
1283 
1284 /* Enqueue a single packet, and send burst if queue is filled */
1285 static inline int
1286 send_single_packet(struct rte_mbuf *m, uint8_t port)
1287 {
1288     uint16_t len;
1289     struct lcore_conf *qconf;
1290 
1291     qconf = &lcore_conf;
1292     len = qconf->tx_mbufs[port].len;
1293     qconf->tx_mbufs[port].m_table[len] = m;
1294     len++;
1295 
1296     /* enough pkts to be sent */
1297     if (unlikely(len == MAX_PKT_BURST)) {
1298         send_burst(qconf, MAX_PKT_BURST, port);
1299         len = 0;
1300     }
1301 
1302     qconf->tx_mbufs[port].len = len;
1303     return 0;
1304 }
1305 
1306 int
1307 ff_dpdk_if_send(struct ff_dpdk_if_context *ctx, void *m,
1308     int total)
1309 {
1310 #ifdef FF_USE_PAGE_ARRAY
1311     struct lcore_conf *qconf = &lcore_conf;
1312     int    len = 0;
1313 
1314     len = ff_if_send_onepkt(ctx, m,total);
1315     if (unlikely(len == MAX_PKT_BURST)) {
1316         send_burst(qconf, MAX_PKT_BURST, ctx->port_id);
1317         len = 0;
1318     }
1319     qconf->tx_mbufs[ctx->port_id].len = len;
1320     return 0;
1321 #endif
1322     struct rte_mempool *mbuf_pool = pktmbuf_pool[lcore_conf.socket_id];
1323     struct rte_mbuf *head = rte_pktmbuf_alloc(mbuf_pool);
1324     if (head == NULL) {
1325         ff_mbuf_free(m);
1326         return -1;
1327     }
1328 
1329     head->pkt_len = total;
1330     head->nb_segs = 0;
1331 
1332     int off = 0;
1333     struct rte_mbuf *cur = head, *prev = NULL;
1334     while(total > 0) {
1335         if (cur == NULL) {
1336             cur = rte_pktmbuf_alloc(mbuf_pool);
1337             if (cur == NULL) {
1338                 rte_pktmbuf_free(head);
1339                 ff_mbuf_free(m);
1340                 return -1;
1341             }
1342         }
1343 
1344         if (prev != NULL) {
1345             prev->next = cur;
1346         }
1347         head->nb_segs++;
1348 
1349         prev = cur;
1350         void *data = rte_pktmbuf_mtod(cur, void*);
1351         int len = total > RTE_MBUF_DEFAULT_DATAROOM ? RTE_MBUF_DEFAULT_DATAROOM : total;
1352         int ret = ff_mbuf_copydata(m, data, off, len);
1353         if (ret < 0) {
1354             rte_pktmbuf_free(head);
1355             ff_mbuf_free(m);
1356             return -1;
1357         }
1358 
1359 
1360         cur->data_len = len;
1361         off += len;
1362         total -= len;
1363         cur = NULL;
1364     }
1365 
1366     struct ff_tx_offload offload = {0};
1367     ff_mbuf_tx_offload(m, &offload);
1368 
1369     void *data = rte_pktmbuf_mtod(head, void*);
1370 
1371     if (offload.ip_csum) {
1372         /* ipv6 not supported yet */
1373         struct ipv4_hdr *iph;
1374         int iph_len;
1375         iph = (struct ipv4_hdr *)(data + ETHER_HDR_LEN);
1376         iph_len = (iph->version_ihl & 0x0f) << 2;
1377 
1378         head->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
1379         head->l2_len = ETHER_HDR_LEN;
1380         head->l3_len = iph_len;
1381     }
1382 
1383     if (ctx->hw_features.tx_csum_l4) {
1384         struct ipv4_hdr *iph;
1385         int iph_len;
1386         iph = (struct ipv4_hdr *)(data + ETHER_HDR_LEN);
1387         iph_len = (iph->version_ihl & 0x0f) << 2;
1388 
1389         if (offload.tcp_csum) {
1390             head->ol_flags |= PKT_TX_TCP_CKSUM;
1391             head->l2_len = ETHER_HDR_LEN;
1392             head->l3_len = iph_len;
1393         }
1394 
1395         /*
1396          *  TCP segmentation offload.
1397          *
1398          *  - set the PKT_TX_TCP_SEG flag in mbuf->ol_flags (this flag
1399          *    implies PKT_TX_TCP_CKSUM)
1400          *  - set the flag PKT_TX_IPV4 or PKT_TX_IPV6
1401          *  - if it's IPv4, set the PKT_TX_IP_CKSUM flag and
1402          *    write the IP checksum to 0 in the packet
1403          *  - fill the mbuf offload information: l2_len,
1404          *    l3_len, l4_len, tso_segsz
1405          *  - calculate the pseudo header checksum without taking ip_len
1406          *    in account, and set it in the TCP header. Refer to
1407          *    rte_ipv4_phdr_cksum() and rte_ipv6_phdr_cksum() that can be
1408          *    used as helpers.
1409          */
1410         if (offload.tso_seg_size) {
1411             struct tcp_hdr *tcph;
1412             int tcph_len;
1413             tcph = (struct tcp_hdr *)((char *)iph + iph_len);
1414             tcph_len = (tcph->data_off & 0xf0) >> 2;
1415             tcph->cksum = rte_ipv4_phdr_cksum(iph, PKT_TX_TCP_SEG);
1416 
1417             head->ol_flags |= PKT_TX_TCP_SEG;
1418             head->l4_len = tcph_len;
1419             head->tso_segsz = offload.tso_seg_size;
1420         }
1421 
1422         if (offload.udp_csum) {
1423             head->ol_flags |= PKT_TX_UDP_CKSUM;
1424             head->l2_len = ETHER_HDR_LEN;
1425             head->l3_len = iph_len;
1426         }
1427     }
1428 
1429     ff_mbuf_free(m);
1430 
1431     return send_single_packet(head, ctx->port_id);
1432 }
1433 
1434 static int
1435 main_loop(void *arg)
1436 {
1437     struct loop_routine *lr = (struct loop_routine *)arg;
1438 
1439     struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1440     uint64_t prev_tsc, diff_tsc, cur_tsc, usch_tsc, div_tsc, usr_tsc, sys_tsc, end_tsc, idle_sleep_tsc;
1441     int i, j, nb_rx, idle;
1442     uint16_t port_id, queue_id;
1443     struct lcore_conf *qconf;
1444     uint64_t drain_tsc = 0;
1445     struct ff_dpdk_if_context *ctx;
1446 
1447     if (pkt_tx_delay) {
1448         drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * pkt_tx_delay;
1449     }
1450 
1451     prev_tsc = 0;
1452     usch_tsc = 0;
1453 
1454     qconf = &lcore_conf;
1455 
1456     while (1) {
1457         cur_tsc = rte_rdtsc();
1458         if (unlikely(freebsd_clock.expire < cur_tsc)) {
1459             rte_timer_manage();
1460         }
1461 
1462         idle = 1;
1463         sys_tsc = 0;
1464         usr_tsc = 0;
1465 
1466         /*
1467          * TX burst queue drain
1468          */
1469         diff_tsc = cur_tsc - prev_tsc;
1470         if (unlikely(diff_tsc >= drain_tsc)) {
1471             for (i = 0; i < qconf->nb_tx_port; i++) {
1472                 port_id = qconf->tx_port_id[i];
1473                 if (qconf->tx_mbufs[port_id].len == 0)
1474                     continue;
1475 
1476                 idle = 0;
1477 
1478                 send_burst(qconf,
1479                     qconf->tx_mbufs[port_id].len,
1480                     port_id);
1481                 qconf->tx_mbufs[port_id].len = 0;
1482             }
1483 
1484             prev_tsc = cur_tsc;
1485         }
1486 
1487         /*
1488          * Read packet from RX queues
1489          */
1490         for (i = 0; i < qconf->nb_rx_queue; ++i) {
1491             port_id = qconf->rx_queue_list[i].port_id;
1492             queue_id = qconf->rx_queue_list[i].queue_id;
1493             ctx = veth_ctx[port_id];
1494 
1495 #ifdef FF_KNI
1496             if (enable_kni && rte_eal_process_type() == RTE_PROC_PRIMARY) {
1497                 ff_kni_process(port_id, queue_id, pkts_burst, MAX_PKT_BURST);
1498             }
1499 #endif
1500 
1501             process_dispatch_ring(port_id, queue_id, pkts_burst, ctx);
1502 
1503             nb_rx = rte_eth_rx_burst(port_id, queue_id, pkts_burst,
1504                 MAX_PKT_BURST);
1505             if (nb_rx == 0)
1506                 continue;
1507 
1508             idle = 0;
1509 
1510             /* Prefetch first packets */
1511             for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1512                 rte_prefetch0(rte_pktmbuf_mtod(
1513                         pkts_burst[j], void *));
1514             }
1515 
1516             /* Prefetch and handle already prefetched packets */
1517             for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1518                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1519                         j + PREFETCH_OFFSET], void *));
1520                 process_packets(port_id, queue_id, &pkts_burst[j], 1, ctx, 0);
1521             }
1522 
1523             /* Handle remaining prefetched packets */
1524             for (; j < nb_rx; j++) {
1525                 process_packets(port_id, queue_id, &pkts_burst[j], 1, ctx, 0);
1526             }
1527         }
1528 
1529         process_msg_ring(qconf->proc_id);
1530 
1531         div_tsc = rte_rdtsc();
1532 
1533         if (likely(lr->loop != NULL && (!idle || cur_tsc - usch_tsc >= drain_tsc))) {
1534             usch_tsc = cur_tsc;
1535             lr->loop(lr->arg);
1536         }
1537 
1538         idle_sleep_tsc = rte_rdtsc();
1539         if (likely(idle && idle_sleep)) {
1540             usleep(idle_sleep);
1541             end_tsc = rte_rdtsc();
1542         } else {
1543             end_tsc = idle_sleep_tsc;
1544         }
1545 
1546         if (usch_tsc == cur_tsc) {
1547             usr_tsc = idle_sleep_tsc - div_tsc;
1548         }
1549 
1550         if (!idle) {
1551             sys_tsc = div_tsc - cur_tsc;
1552             ff_top_status.sys_tsc += sys_tsc;
1553         }
1554 
1555         ff_top_status.usr_tsc += usr_tsc;
1556         ff_top_status.work_tsc += end_tsc - cur_tsc;
1557         ff_top_status.idle_tsc += end_tsc - cur_tsc - usr_tsc - sys_tsc;
1558 
1559         ff_top_status.loops++;
1560     }
1561 
1562     return 0;
1563 }
1564 
1565 int
1566 ff_dpdk_if_up(void) {
1567     int i;
1568     struct lcore_conf *qconf = &lcore_conf;
1569     for (i = 0; i < qconf->nb_tx_port; i++) {
1570         uint16_t port_id = qconf->tx_port_id[i];
1571 
1572         struct ff_port_cfg *pconf = &qconf->port_cfgs[port_id];
1573         veth_ctx[port_id] = ff_veth_attach(pconf);
1574         if (veth_ctx[port_id] == NULL) {
1575             rte_exit(EXIT_FAILURE, "ff_veth_attach failed");
1576         }
1577     }
1578 
1579     return 0;
1580 }
1581 
1582 void
1583 ff_dpdk_run(loop_func_t loop, void *arg) {
1584     struct loop_routine *lr = rte_malloc(NULL,
1585         sizeof(struct loop_routine), 0);
1586     lr->loop = loop;
1587     lr->arg = arg;
1588     rte_eal_mp_remote_launch(main_loop, lr, CALL_MASTER);
1589     rte_eal_mp_wait_lcore();
1590     rte_free(lr);
1591 }
1592 
1593 void
1594 ff_dpdk_pktmbuf_free(void *m)
1595 {
1596     rte_pktmbuf_free((struct rte_mbuf *)m);
1597 }
1598 
1599 static uint32_t
1600 toeplitz_hash(unsigned keylen, const uint8_t *key,
1601     unsigned datalen, const uint8_t *data)
1602 {
1603     uint32_t hash = 0, v;
1604     u_int i, b;
1605 
1606     /* XXXRW: Perhaps an assertion about key length vs. data length? */
1607 
1608     v = (key[0]<<24) + (key[1]<<16) + (key[2] <<8) + key[3];
1609     for (i = 0; i < datalen; i++) {
1610         for (b = 0; b < 8; b++) {
1611             if (data[i] & (1<<(7-b)))
1612                 hash ^= v;
1613             v <<= 1;
1614             if ((i + 4) < keylen &&
1615                 (key[i+4] & (1<<(7-b))))
1616                 v |= 1;
1617         }
1618     }
1619     return (hash);
1620 }
1621 
1622 int
1623 ff_rss_check(void *softc, uint32_t saddr, uint32_t daddr,
1624     uint16_t sport, uint16_t dport)
1625 {
1626     struct lcore_conf *qconf = &lcore_conf;
1627     struct ff_dpdk_if_context *ctx = ff_veth_softc_to_hostc(softc);
1628     uint16_t nb_queues = qconf->nb_queue_list[ctx->port_id];
1629 
1630     if (nb_queues <= 1) {
1631         return 1;
1632     }
1633 
1634     uint16_t reta_size = rss_reta_size[ctx->port_id];
1635     uint16_t queueid = qconf->tx_queue_id[ctx->port_id];
1636 
1637     uint8_t data[sizeof(saddr) + sizeof(daddr) + sizeof(sport) +
1638         sizeof(dport)];
1639 
1640     unsigned datalen = 0;
1641 
1642     bcopy(&saddr, &data[datalen], sizeof(saddr));
1643     datalen += sizeof(saddr);
1644 
1645     bcopy(&daddr, &data[datalen], sizeof(daddr));
1646     datalen += sizeof(daddr);
1647 
1648     bcopy(&sport, &data[datalen], sizeof(sport));
1649     datalen += sizeof(sport);
1650 
1651     bcopy(&dport, &data[datalen], sizeof(dport));
1652     datalen += sizeof(dport);
1653 
1654     uint32_t hash = 0;
1655     if ( !use_rsskey_52bytes )
1656         hash = toeplitz_hash(sizeof(default_rsskey_40bytes),
1657             default_rsskey_40bytes, datalen, data);
1658     else
1659         hash = toeplitz_hash(sizeof(default_rsskey_52bytes),
1660 	    default_rsskey_52bytes, datalen, data);
1661     return ((hash & (reta_size - 1)) % nb_queues) == queueid;
1662 }
1663 
1664 void
1665 ff_regist_packet_dispatcher(dispatch_func_t func)
1666 {
1667     packet_dispatcher = func;
1668 }
1669 
1670 uint64_t
1671 ff_get_tsc_ns()
1672 {
1673     uint64_t cur_tsc = rte_rdtsc();
1674     uint64_t hz = rte_get_tsc_hz();
1675     return ((double)cur_tsc/(double)hz) * NS_PER_S;
1676 }
1677 
1678