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