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