78a04ca28a2463e4f26fb73ee5fa083799e2d928
[dpdk.git] / examples / multi_process / l2fwd_fork / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #define _GNU_SOURCE
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <sched.h>
40 #include <inttypes.h>
41 #include <sys/types.h>
42 #include <sys/queue.h>
43 #include <netinet/in.h>
44 #include <setjmp.h>
45 #include <stdarg.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <getopt.h>
49
50 #include <rte_common.h>
51 #include <rte_log.h>
52 #include <rte_memory.h>
53 #include <rte_memcpy.h>
54 #include <rte_memzone.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_per_lcore.h>
58 #include <rte_launch.h>
59 #include <rte_atomic.h>
60 #include <rte_spinlock.h>
61 #include <rte_cycles.h>
62 #include <rte_prefetch.h>
63 #include <rte_lcore.h>
64 #include <rte_per_lcore.h>
65 #include <rte_branch_prediction.h>
66 #include <rte_interrupts.h>
67 #include <rte_pci.h>
68 #include <rte_random.h>
69 #include <rte_debug.h>
70 #include <rte_ether.h>
71 #include <rte_ethdev.h>
72 #include <rte_ring.h>
73 #include <rte_mempool.h>
74 #include <rte_mbuf.h>
75 #include <rte_malloc.h>
76
77 #include "flib.h"
78
79 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
80 #define MBUF_NAME       "mbuf_pool_%d"
81 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
82 #define NB_MBUF   8192
83 #define RING_MASTER_NAME        "l2fwd_ring_m2s_"
84 #define RING_SLAVE_NAME         "l2fwd_ring_s2m_"
85 #define MAX_NAME_LEN    32
86 /* RECREATE flag indicate needs initialize resource and launch slave_core again */
87 #define SLAVE_RECREATE_FLAG 0x1
88 /* RESTART flag indicate needs restart port and send START command again */
89 #define SLAVE_RESTART_FLAG 0x2
90 #define INVALID_MAPPING_ID      ((unsigned)LCORE_ID_ANY)
91 /* Maximum message buffer per slave */
92 #define NB_CORE_MSGBUF  32
93 enum l2fwd_cmd{
94         CMD_START,
95         CMD_STOP,
96 };
97
98 #define MAX_PKT_BURST 32
99 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
100
101 /*
102  * Configurable number of RX/TX ring descriptors
103  */
104 #define RTE_TEST_RX_DESC_DEFAULT 128
105 #define RTE_TEST_TX_DESC_DEFAULT 512
106 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
107 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
108
109 /* ethernet addresses of ports */
110 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
111
112 /* mask of enabled ports */
113 static uint32_t l2fwd_enabled_port_mask = 0;
114
115 /* list of enabled ports */
116 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
117
118 static unsigned int l2fwd_rx_queue_per_lcore = 1;
119
120 struct mbuf_table {
121         unsigned len;
122         struct rte_mbuf *m_table[MAX_PKT_BURST];
123 };
124
125 #define MAX_RX_QUEUE_PER_LCORE 16
126 #define MAX_TX_QUEUE_PER_PORT 16
127 struct lcore_queue_conf {
128         unsigned n_rx_port;
129         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
130         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
131
132 } __rte_cache_aligned;
133 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
134
135 struct lcore_resource_struct {
136         int enabled;    /* Only set in case this lcore involved into packet forwarding */
137         int flags;          /* Set only slave need to restart or recreate */
138         unsigned lcore_id;  /*  lcore ID */
139         unsigned pair_id;       /* dependency lcore ID on port */
140         char ring_name[2][MAX_NAME_LEN];
141         /* ring[0] for master send cmd, slave read */
142         /* ring[1] for slave send ack, master read */
143         struct rte_ring *ring[2];
144         int port_num;                                   /* Total port numbers */
145         uint8_t port[RTE_MAX_ETHPORTS]; /* Port id for that lcore to receive packets */
146 }__attribute__((packed)) __rte_cache_aligned;
147
148 static struct lcore_resource_struct lcore_resource[RTE_MAX_LCORE];
149 static struct rte_mempool *message_pool;
150 static rte_spinlock_t res_lock = RTE_SPINLOCK_INITIALIZER;
151 /* use floating processes */
152 static int float_proc = 0;
153 /* Save original cpu affinity */
154 struct cpu_aff_arg{
155         cpu_set_t set;
156         size_t size;
157 }cpu_aff;
158
159 static const struct rte_eth_conf port_conf = {
160         .rxmode = {
161                 .split_hdr_size = 0,
162                 .header_split   = 0, /**< Header Split disabled */
163                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
164                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
165                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
166                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
167         },
168         .txmode = {
169                 .mq_mode = ETH_MQ_TX_NONE,
170         },
171 };
172
173 static struct rte_mempool * l2fwd_pktmbuf_pool[RTE_MAX_ETHPORTS];
174
175 /* Per-port statistics struct */
176 struct l2fwd_port_statistics {
177         uint64_t tx;
178         uint64_t rx;
179         uint64_t dropped;
180 } __rte_cache_aligned;
181 struct l2fwd_port_statistics *port_statistics;
182 /**
183  * pointer to lcore ID mapping array, used to return lcore id in case slave
184  * process exited unexpectedly, use only floating process option applied
185  **/
186 unsigned *mapping_id;
187
188 /* A tsc-based timer responsible for triggering statistics printout */
189 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
190 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
191 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
192
193 static int l2fwd_launch_one_lcore(void *dummy);
194
195 /* Print out statistics on packets dropped */
196 static void
197 print_stats(void)
198 {
199         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
200         unsigned portid;
201
202         total_packets_dropped = 0;
203         total_packets_tx = 0;
204         total_packets_rx = 0;
205
206         const char clr[] = { 27, '[', '2', 'J', '\0' };
207         const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
208
209                 /* Clear screen and move to top left */
210         printf("%s%s", clr, topLeft);
211
212         printf("\nPort statistics ====================================");
213
214         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
215                 /* skip disabled ports */
216                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
217                         continue;
218                 printf("\nStatistics for port %u ------------------------------"
219                            "\nPackets sent: %24"PRIu64
220                            "\nPackets received: %20"PRIu64
221                            "\nPackets dropped: %21"PRIu64,
222                            portid,
223                            port_statistics[portid].tx,
224                            port_statistics[portid].rx,
225                            port_statistics[portid].dropped);
226
227                 total_packets_dropped += port_statistics[portid].dropped;
228                 total_packets_tx += port_statistics[portid].tx;
229                 total_packets_rx += port_statistics[portid].rx;
230         }
231         printf("\nAggregate statistics ==============================="
232                    "\nTotal packets sent: %18"PRIu64
233                    "\nTotal packets received: %14"PRIu64
234                    "\nTotal packets dropped: %15"PRIu64,
235                    total_packets_tx,
236                    total_packets_rx,
237                    total_packets_dropped);
238         printf("\n====================================================\n");
239 }
240
241 static int
242 clear_cpu_affinity(void)
243 {
244         int s;
245
246         s = sched_setaffinity(0, cpu_aff.size, &cpu_aff.set);
247         if (s != 0) {
248                 printf("sched_setaffinity failed:%s\n", strerror(errno));
249                 return -1;
250         }
251
252         return 0;
253 }
254
255 static int
256 get_cpu_affinity(void)
257 {
258         int s;
259
260         cpu_aff.size = sizeof(cpu_set_t);
261         CPU_ZERO(&cpu_aff.set);
262
263         s = sched_getaffinity(0, cpu_aff.size, &cpu_aff.set);
264         if (s != 0) {
265                 printf("sched_getaffinity failed:%s\n", strerror(errno));
266                 return -1;
267         }
268
269         return 0;
270 }
271
272 /**
273  * This fnciton demonstrates the approach to create ring in first instance
274  * or re-attach an existed ring in later instance.
275  **/
276 static struct rte_ring *
277 create_ring(const char *name, unsigned count,
278                                         int socket_id,unsigned flags)
279 {
280         struct rte_ring *ring;
281
282         if (name == NULL)
283                 return NULL;
284
285         /* If already create, just attached it */
286         if (likely((ring = rte_ring_lookup(name)) != NULL))
287                 return ring;
288
289         /* First call it, create one */
290         return rte_ring_create(name, count, socket_id, flags);
291 }
292
293 /* Malloc with rte_malloc on structures that shared by master and slave */
294 static int
295 l2fwd_malloc_shared_struct(void)
296 {
297         port_statistics = rte_zmalloc("port_stat",
298                                                 sizeof(struct l2fwd_port_statistics) * RTE_MAX_ETHPORTS,
299                                                 0);
300         if (port_statistics == NULL)
301                 return -1;
302
303         /* allocate  mapping_id array */
304         if (float_proc) {
305                 int i;
306                 mapping_id = rte_malloc("mapping_id", sizeof(unsigned) * RTE_MAX_LCORE,
307                                                                 0);
308
309                 if (mapping_id == NULL)
310                         return -1;
311
312                 for (i = 0 ;i < RTE_MAX_LCORE; i++)
313                         mapping_id[i] = INVALID_MAPPING_ID;
314         }
315         return 0;
316 }
317
318 /* Create ring which used for communicate among master and slave */
319 static int
320 create_ms_ring(unsigned slaveid)
321 {
322         unsigned flag = RING_F_SP_ENQ | RING_F_SC_DEQ;
323         struct lcore_resource_struct *res = &lcore_resource[slaveid];
324         unsigned socketid = rte_socket_id();
325
326         /* Always assume create ring on master socket_id */
327         /* Default only create a ring size 32 */
328         snprintf(res->ring_name[0], MAX_NAME_LEN, "%s%u",
329                         RING_MASTER_NAME, slaveid);
330         if ((res->ring[0] = create_ring(res->ring_name[0], NB_CORE_MSGBUF,
331                                 socketid, flag)) == NULL) {
332                 printf("Create m2s ring %s failed\n", res->ring_name[0]);
333                 return -1;
334         }
335
336         snprintf(res->ring_name[1], MAX_NAME_LEN, "%s%u",
337                         RING_SLAVE_NAME, slaveid);
338         if ((res->ring[1] = create_ring(res->ring_name[1], NB_CORE_MSGBUF,
339                 socketid, flag)) == NULL) {
340                 printf("Create s2m ring %s failed\n", res->ring_name[1]);
341                 return -1;
342         }
343
344         return 0;
345 }
346
347 /* send command to pair in paired master and slave ring */
348 static inline int
349 sendcmd(unsigned slaveid, enum l2fwd_cmd cmd, int is_master)
350 {
351         struct lcore_resource_struct *res = &lcore_resource[slaveid];
352         void *msg;
353         int fd = !is_master;
354
355         /* Only check master, it must be enabled and running if it is slave */
356         if (is_master && !res->enabled)
357                 return -1;
358
359         if (res->ring[fd] == NULL)
360                 return -1;
361
362         if (rte_mempool_get(message_pool, &msg) < 0) {
363                 printf("Error to get message buffer\n");
364                 return -1;
365         }
366
367         *(enum l2fwd_cmd *)msg = cmd;
368
369         if (rte_ring_enqueue(res->ring[fd], msg) != 0) {
370                 printf("Enqueue error\n");
371                 rte_mempool_put(message_pool, msg);
372                 return -1;
373         }
374
375         return 0;
376 }
377
378 /* Get command from pair in paired master and slave ring */
379 static inline int
380 getcmd(unsigned slaveid, enum l2fwd_cmd *cmd, int is_master)
381 {
382         struct lcore_resource_struct *res = &lcore_resource[slaveid];
383         void *msg;
384         int fd = !!is_master;
385         int ret;
386         /* Only check master, it must be enabled and running if it is slave */
387         if (is_master && (!res->enabled))
388                 return -1;
389
390         if (res->ring[fd] == NULL)
391                 return -1;
392
393         ret = rte_ring_dequeue(res->ring[fd], &msg);
394
395         if (ret == 0) {
396                 *cmd = *(enum l2fwd_cmd *)msg;
397                 rte_mempool_put(message_pool, msg);
398         }
399         return ret;
400 }
401
402 /* Master send command to slave and wait until ack received or error met */
403 static int
404 master_sendcmd_with_ack(unsigned slaveid, enum l2fwd_cmd cmd)
405 {
406         enum l2fwd_cmd ack_cmd;
407         int ret = -1;
408
409         if (sendcmd(slaveid, cmd, 1) != 0)
410                 rte_exit(EXIT_FAILURE, "Failed to send message\n");
411
412         /* Get ack */
413         while (1) {
414                 ret = getcmd(slaveid, &ack_cmd, 1);
415                 if (ret == 0 && cmd == ack_cmd)
416                         break;
417
418                 /* If slave not running yet, return an error */
419                 if (flib_query_slave_status(slaveid) != ST_RUN) {
420                         ret = -ENOENT;
421                         break;
422                 }
423         }
424
425         return ret;
426 }
427
428 /* restart all port that assigned to that slave lcore */
429 static int
430 reset_slave_all_ports(unsigned slaveid)
431 {
432         struct lcore_resource_struct *slave = &lcore_resource[slaveid];
433         int i, ret = 0;
434
435         /* stop/start port */
436         for (i = 0; i < slave->port_num; i++) {
437                 char buf_name[RTE_MEMPOOL_NAMESIZE];
438                 struct rte_mempool *pool;
439                 printf("Stop port :%d\n", slave->port[i]);
440                 rte_eth_dev_stop(slave->port[i]);
441                 snprintf(buf_name, RTE_MEMPOOL_NAMESIZE, MBUF_NAME, slave->port[i]);
442                 pool = rte_mempool_lookup(buf_name);
443                 if (pool)
444                         printf("Port %d mempool free object is %u(%u)\n", slave->port[i],
445                                 rte_mempool_count(pool), (unsigned)NB_MBUF);
446                 else
447                         printf("Can't find mempool %s\n", buf_name);
448
449                 printf("Start port :%d\n", slave->port[i]);
450                 ret = rte_eth_dev_start(slave->port[i]);
451                 if (ret != 0)
452                         break;
453         }
454         return ret;
455 }
456
457 static int
458 reset_shared_structures(unsigned slaveid)
459 {
460         int ret;
461         /* Only port are shared resource here */
462         ret = reset_slave_all_ports(slaveid);
463
464         return ret;
465 }
466
467 /**
468  * Call this function to re-create resource that needed for slave process that
469  * exited in last instance
470  **/
471 static int
472 init_slave_res(unsigned slaveid)
473 {
474         struct lcore_resource_struct *slave = &lcore_resource[slaveid];
475         enum l2fwd_cmd cmd;
476
477         if (!slave->enabled) {
478                 printf("Something wrong with lcore=%u enabled=%d\n",slaveid,
479                         slave->enabled);
480                 return -1;
481         }
482
483         /* Initialize ring */
484         if (create_ms_ring(slaveid) != 0)
485                 rte_exit(EXIT_FAILURE, "failed to create ring for slave %u\n",
486                                 slaveid);
487
488         /* drain un-read buffer if have */
489         while (getcmd(slaveid, &cmd, 1) == 0);
490         while (getcmd(slaveid, &cmd, 0) == 0);
491
492         return 0;
493 }
494
495 static int
496 recreate_one_slave(unsigned slaveid)
497 {
498         int ret = 0;
499         /* Re-initialize resource for stalled slave */
500         if ((ret = init_slave_res(slaveid)) != 0) {
501                 printf("Init slave=%u failed\n", slaveid);
502                 return ret;
503         }
504
505         if ((ret = flib_remote_launch(l2fwd_launch_one_lcore, NULL, slaveid))
506                 != 0)
507                 printf("Launch slave %u failed\n", slaveid);
508
509         return ret;
510 }
511
512 /**
513  * remapping resource belong to slave_id to new lcore that gets from flib_assign_lcore_id(),
514  * used only floating process option applied.
515  *
516  * @param slaveid
517  *   original lcore_id that apply for remapping
518  */
519 static void
520 remapping_slave_resource(unsigned slaveid, unsigned map_id)
521 {
522
523         /* remapping lcore_resource */
524         memcpy(&lcore_resource[map_id], &lcore_resource[slaveid],
525                         sizeof(struct lcore_resource_struct));
526
527         /* remapping lcore_queue_conf */
528         memcpy(&lcore_queue_conf[map_id], &lcore_queue_conf[slaveid],
529                         sizeof(struct lcore_queue_conf));
530 }
531
532 static int
533 reset_pair(unsigned slaveid, unsigned pairid)
534 {
535         int ret;
536         if ((ret = reset_shared_structures(slaveid)) != 0)
537                 goto back;
538
539         if((ret = reset_shared_structures(pairid)) != 0)
540                 goto back;
541
542         if (float_proc) {
543                 unsigned map_id = mapping_id[slaveid];
544
545                 if (map_id != INVALID_MAPPING_ID) {
546                         printf("%u return mapping id %u\n", slaveid, map_id);
547                         flib_free_lcore_id(map_id);
548                         mapping_id[slaveid] = INVALID_MAPPING_ID;
549                 }
550
551                 map_id = mapping_id[pairid];
552                 if (map_id != INVALID_MAPPING_ID) {
553                         printf("%u return mapping id %u\n", pairid, map_id);
554                         flib_free_lcore_id(map_id);
555                         mapping_id[pairid] = INVALID_MAPPING_ID;
556                 }
557         }
558
559         if((ret = recreate_one_slave(slaveid)) != 0)
560                 goto back;
561
562         ret = recreate_one_slave(pairid);
563
564 back:
565         return ret;
566 }
567
568 static void
569 slave_exit_cb(unsigned slaveid, __attribute__((unused))int stat)
570 {
571         struct lcore_resource_struct *slave = &lcore_resource[slaveid];
572
573         printf("Get slave %u leave info\n", slaveid);
574         if (!slave->enabled) {
575                 printf("Lcore=%u not registered for it's exit\n", slaveid);
576                 return;
577         }
578         rte_spinlock_lock(&res_lock);
579
580         /* Change the state and wait master to start them */
581         slave->flags = SLAVE_RECREATE_FLAG;
582
583         rte_spinlock_unlock(&res_lock);
584 }
585
586 /* Send the packet on an output interface */
587 static int
588 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n, uint8_t port)
589 {
590         struct rte_mbuf **m_table;
591         unsigned ret;
592         unsigned queueid =0;
593
594         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
595
596         ret = rte_eth_tx_burst(port, (uint16_t) queueid, m_table, (uint16_t) n);
597         port_statistics[port].tx += ret;
598         if (unlikely(ret < n)) {
599                 port_statistics[port].dropped += (n - ret);
600                 do {
601                         rte_pktmbuf_free(m_table[ret]);
602                 } while (++ret < n);
603         }
604
605         return 0;
606 }
607
608 /* Send the packet on an output interface */
609 static int
610 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
611 {
612         unsigned lcore_id, len;
613         struct lcore_queue_conf *qconf;
614
615         lcore_id = rte_lcore_id();
616
617         qconf = &lcore_queue_conf[lcore_id];
618         len = qconf->tx_mbufs[port].len;
619         qconf->tx_mbufs[port].m_table[len] = m;
620         len++;
621
622         /* enough pkts to be sent */
623         if (unlikely(len == MAX_PKT_BURST)) {
624                 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
625                 len = 0;
626         }
627
628         qconf->tx_mbufs[port].len = len;
629         return 0;
630 }
631
632 static void
633 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
634 {
635         struct ether_hdr *eth;
636         void *tmp;
637         unsigned dst_port;
638
639         dst_port = l2fwd_dst_ports[portid];
640         eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
641
642         /* 02:00:00:00:00:xx */
643         tmp = &eth->d_addr.addr_bytes[0];
644         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
645
646         /* src addr */
647         ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
648
649         l2fwd_send_packet(m, (uint8_t) dst_port);
650 }
651
652 /* main processing loop */
653 static void
654 l2fwd_main_loop(void)
655 {
656         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
657         struct rte_mbuf *m;
658         unsigned lcore_id;
659         uint64_t prev_tsc, diff_tsc, cur_tsc;
660         unsigned i, j, portid, nb_rx;
661         struct lcore_queue_conf *qconf;
662         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
663
664         prev_tsc = 0;
665
666         lcore_id = rte_lcore_id();
667
668         qconf = &lcore_queue_conf[lcore_id];
669
670         if (qconf->n_rx_port == 0) {
671                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
672                 return;
673         }
674
675         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
676
677         for (i = 0; i < qconf->n_rx_port; i++) {
678                 portid = qconf->rx_port_list[i];
679                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
680                         portid);
681         }
682
683         while (1) {
684                 enum l2fwd_cmd cmd;
685                 cur_tsc = rte_rdtsc();
686
687                 if (unlikely(getcmd(lcore_id, &cmd, 0) == 0)) {
688                         sendcmd(lcore_id, cmd, 0);
689
690                         /* If get stop command, stop forwarding and exit */
691                         if (cmd == CMD_STOP) {
692                                 return;
693                         }
694                 }
695
696                 /*
697                  * TX burst queue drain
698                  */
699                 diff_tsc = cur_tsc - prev_tsc;
700                 if (unlikely(diff_tsc > drain_tsc)) {
701
702                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
703                                 if (qconf->tx_mbufs[portid].len == 0)
704                                         continue;
705                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
706                                                  qconf->tx_mbufs[portid].len,
707                                                  (uint8_t) portid);
708                                 qconf->tx_mbufs[portid].len = 0;
709                         }
710                 }
711
712                 /*
713                  * Read packet from RX queues
714                  */
715                 for (i = 0; i < qconf->n_rx_port; i++) {
716
717                         portid = qconf->rx_port_list[i];
718                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
719                                                  pkts_burst, MAX_PKT_BURST);
720
721                         port_statistics[portid].rx += nb_rx;
722
723                         for (j = 0; j < nb_rx; j++) {
724                                 m = pkts_burst[j];
725                                 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
726                                 l2fwd_simple_forward(m, portid);
727                         }
728                 }
729         }
730 }
731
732 static int
733 l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy)
734 {
735         unsigned lcore_id = rte_lcore_id();
736
737         if (float_proc) {
738                 unsigned flcore_id;
739
740                 /* Change it to floating process, also change it's lcore_id */
741                 clear_cpu_affinity();
742                 RTE_PER_LCORE(_lcore_id) = 0;
743                 /* Get a lcore_id */
744                 if (flib_assign_lcore_id() < 0 ) {
745                         printf("flib_assign_lcore_id failed\n");
746                         return -1;
747                 }
748                 flcore_id = rte_lcore_id();
749                 /* Set mapping id, so master can return it after slave exited */
750                 mapping_id[lcore_id] = flcore_id;
751                 printf("Org lcore_id = %u, cur lcore_id = %u\n",
752                                 lcore_id, flcore_id);
753                 remapping_slave_resource(lcore_id, flcore_id);
754         }
755
756         l2fwd_main_loop();
757
758         /* return lcore_id before return */
759         if (float_proc) {
760                 flib_free_lcore_id(rte_lcore_id());
761                 mapping_id[lcore_id] = INVALID_MAPPING_ID;
762         }
763         return 0;
764 }
765
766 /* display usage */
767 static void
768 l2fwd_usage(const char *prgname)
769 {
770         printf("%s [EAL options] -- -p PORTMASK -s COREMASK [-q NQ] -f\n"
771                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
772                "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
773                "  -f use floating process which won't bind to any core to run\n"
774                    "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
775                prgname);
776 }
777
778 static int
779 l2fwd_parse_portmask(const char *portmask)
780 {
781         char *end = NULL;
782         unsigned long pm;
783
784         /* parse hexadecimal string */
785         pm = strtoul(portmask, &end, 16);
786         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
787                 return -1;
788
789         if (pm == 0)
790                 return -1;
791
792         return pm;
793 }
794
795 static unsigned int
796 l2fwd_parse_nqueue(const char *q_arg)
797 {
798         char *end = NULL;
799         unsigned long n;
800
801         /* parse hexadecimal string */
802         n = strtoul(q_arg, &end, 10);
803         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
804                 return 0;
805         if (n == 0)
806                 return 0;
807         if (n >= MAX_RX_QUEUE_PER_LCORE)
808                 return 0;
809
810         return n;
811 }
812
813 static int
814 l2fwd_parse_timer_period(const char *q_arg)
815 {
816         char *end = NULL;
817         int n;
818
819         /* parse number string */
820         n = strtol(q_arg, &end, 10);
821         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
822                 return -1;
823         if (n >= MAX_TIMER_PERIOD)
824                 return -1;
825
826         return n;
827 }
828
829 /* Parse the argument given in the command line of the application */
830 static int
831 l2fwd_parse_args(int argc, char **argv)
832 {
833         int opt, ret;
834         char **argvopt;
835         int option_index;
836         char *prgname = argv[0];
837         static struct option lgopts[] = {
838                 {NULL, 0, 0, 0}
839         };
840         int has_pmask = 0;
841
842         argvopt = argv;
843
844         while ((opt = getopt_long(argc, argvopt, "p:q:T:f",
845                                   lgopts, &option_index)) != EOF) {
846
847                 switch (opt) {
848                 /* portmask */
849                 case 'p':
850                         l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
851                         if (l2fwd_enabled_port_mask == 0) {
852                                 printf("invalid portmask\n");
853                                 l2fwd_usage(prgname);
854                                 return -1;
855                         }
856                         has_pmask = 1;
857                         break;
858
859                 /* nqueue */
860                 case 'q':
861                         l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
862                         if (l2fwd_rx_queue_per_lcore == 0) {
863                                 printf("invalid queue number\n");
864                                 l2fwd_usage(prgname);
865                                 return -1;
866                         }
867                         break;
868
869                 /* timer period */
870                 case 'T':
871                         timer_period = l2fwd_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
872                         if (timer_period < 0) {
873                                 printf("invalid timer period\n");
874                                 l2fwd_usage(prgname);
875                                 return -1;
876                         }
877                         break;
878
879                 /* use floating process */
880                 case 'f':
881                         float_proc = 1;
882                         break;
883
884                 /* long options */
885                 case 0:
886                         l2fwd_usage(prgname);
887                         return -1;
888
889                 default:
890                         l2fwd_usage(prgname);
891                         return -1;
892                 }
893         }
894
895         if (optind >= 0)
896                 argv[optind-1] = prgname;
897
898         if (!has_pmask) {
899                 l2fwd_usage(prgname);
900                 return -1;
901         }
902         ret = optind-1;
903         optind = 0; /* reset getopt lib */
904         return ret;
905 }
906
907 /* Check the link status of all ports in up to 9s, and print them finally */
908 static void
909 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
910 {
911 #define CHECK_INTERVAL 100 /* 100ms */
912 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
913         uint8_t portid, count, all_ports_up, print_flag = 0;
914         struct rte_eth_link link;
915
916         printf("\nChecking link status");
917         fflush(stdout);
918         for (count = 0; count <= MAX_CHECK_TIME; count++) {
919                 all_ports_up = 1;
920                 for (portid = 0; portid < port_num; portid++) {
921                         if ((port_mask & (1 << portid)) == 0)
922                                 continue;
923                         memset(&link, 0, sizeof(link));
924                         rte_eth_link_get_nowait(portid, &link);
925                         /* print link status if flag set */
926                         if (print_flag == 1) {
927                                 if (link.link_status)
928                                         printf("Port %d Link Up - speed %u "
929                                                 "Mbps - %s\n", (uint8_t)portid,
930                                                 (unsigned)link.link_speed,
931                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
932                                         ("full-duplex") : ("half-duplex\n"));
933                                 else
934                                         printf("Port %d Link Down\n",
935                                                 (uint8_t)portid);
936                                 continue;
937                         }
938                         /* clear all_ports_up flag if any link down */
939                         if (link.link_status == 0) {
940                                 all_ports_up = 0;
941                                 break;
942                         }
943                 }
944                 /* after finally printing all link status, get out */
945                 if (print_flag == 1)
946                         break;
947
948                 if (all_ports_up == 0) {
949                         printf(".");
950                         fflush(stdout);
951                         rte_delay_ms(CHECK_INTERVAL);
952                 }
953
954                 /* set the print_flag if all ports up or timeout */
955                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
956                         print_flag = 1;
957                         printf("done\n");
958                 }
959         }
960 }
961
962 int
963 main(int argc, char **argv)
964 {
965         struct lcore_queue_conf *qconf;
966         struct rte_eth_dev_info dev_info;
967         int ret;
968         uint8_t nb_ports;
969         uint8_t nb_ports_available;
970         uint8_t portid, last_port;
971         unsigned rx_lcore_id;
972         unsigned nb_ports_in_mask = 0;
973         unsigned i;
974         int flags = 0;
975         uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
976
977         /* Save cpu_affinity first, restore it in case it's floating process option */
978         if (get_cpu_affinity() != 0)
979                 rte_exit(EXIT_FAILURE, "get_cpu_affinity error\n");
980
981         /* Also tries to set cpu affinity to detect whether  it will fail in child process */
982         if(clear_cpu_affinity() != 0)
983                 rte_exit(EXIT_FAILURE, "clear_cpu_affinity error\n");
984
985         /* init EAL */
986         ret = rte_eal_init(argc, argv);
987         if (ret < 0)
988                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
989         argc -= ret;
990         argv += ret;
991
992         /* parse application arguments (after the EAL ones) */
993         ret = l2fwd_parse_args(argc, argv);
994         if (ret < 0)
995                 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
996
997         /*flib init */
998         if (flib_init() != 0)
999                 rte_exit(EXIT_FAILURE, "flib init error");
1000
1001         /**
1002           * Allocated structures that slave lcore would change. For those that slaves are
1003           * read only, needn't use malloc to share and global or static variables is ok since
1004           * slave inherit all the knowledge that master initialized.
1005           **/
1006         if (l2fwd_malloc_shared_struct() != 0)
1007                 rte_exit(EXIT_FAILURE, "malloc mem failed\n");
1008
1009         /* Initialize lcore_resource structures */
1010         memset(lcore_resource, 0, sizeof(lcore_resource));
1011         for (i = 0; i < RTE_MAX_LCORE; i++)
1012                 lcore_resource[i].lcore_id = i;
1013
1014         nb_ports = rte_eth_dev_count();
1015         if (nb_ports == 0)
1016                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
1017
1018         if (nb_ports > RTE_MAX_ETHPORTS)
1019                 nb_ports = RTE_MAX_ETHPORTS;
1020
1021         /* create the mbuf pool */
1022         for (portid = 0; portid < nb_ports; portid++) {
1023                 /* skip ports that are not enabled */
1024                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1025                         continue;
1026                 char buf_name[RTE_MEMPOOL_NAMESIZE];
1027                 flags = MEMPOOL_F_SP_PUT | MEMPOOL_F_SC_GET;
1028                 snprintf(buf_name, RTE_MEMPOOL_NAMESIZE, MBUF_NAME, portid);
1029                 l2fwd_pktmbuf_pool[portid] =
1030                         rte_mempool_create(buf_name, NB_MBUF,
1031                                            MBUF_SIZE, 32,
1032                                            sizeof(struct rte_pktmbuf_pool_private),
1033                                            rte_pktmbuf_pool_init, NULL,
1034                                            rte_pktmbuf_init, NULL,
1035                                            rte_socket_id(), flags);
1036                 if (l2fwd_pktmbuf_pool[portid] == NULL)
1037                         rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
1038
1039                 printf("Create mbuf %s\n", buf_name);
1040         }
1041
1042         /* reset l2fwd_dst_ports */
1043         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1044                 l2fwd_dst_ports[portid] = 0;
1045         last_port = 0;
1046
1047         /*
1048          * Each logical core is assigned a dedicated TX queue on each port.
1049          */
1050         for (portid = 0; portid < nb_ports; portid++) {
1051                 /* skip ports that are not enabled */
1052                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1053                         continue;
1054
1055                 if (nb_ports_in_mask % 2) {
1056                         l2fwd_dst_ports[portid] = last_port;
1057                         l2fwd_dst_ports[last_port] = portid;
1058                 }
1059                 else
1060                         last_port = portid;
1061
1062                 nb_ports_in_mask++;
1063
1064                 rte_eth_dev_info_get(portid, &dev_info);
1065         }
1066         if (nb_ports_in_mask % 2) {
1067                 printf("Notice: odd number of ports in portmask.\n");
1068                 l2fwd_dst_ports[last_port] = last_port;
1069         }
1070
1071         rx_lcore_id = 0;
1072         qconf = NULL;
1073
1074         /* Initialize the port/queue configuration of each logical core */
1075         for (portid = 0; portid < nb_ports; portid++) {
1076                 struct lcore_resource_struct *res;
1077                 /* skip ports that are not enabled */
1078                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1079                         continue;
1080
1081                 /* get the lcore_id for this port */
1082                 /* skip master lcore */
1083                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1084                            rte_get_master_lcore() == rx_lcore_id ||
1085                        lcore_queue_conf[rx_lcore_id].n_rx_port ==
1086                        l2fwd_rx_queue_per_lcore) {
1087
1088                         rx_lcore_id++;
1089                         if (rx_lcore_id >= RTE_MAX_LCORE)
1090                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
1091                 }
1092
1093                 if (qconf != &lcore_queue_conf[rx_lcore_id])
1094                         /* Assigned a new logical core in the loop above. */
1095                         qconf = &lcore_queue_conf[rx_lcore_id];
1096
1097                 qconf->rx_port_list[qconf->n_rx_port] = portid;
1098                 qconf->n_rx_port++;
1099
1100                 /* Save the port resource info into lcore_resource strucutres */
1101                 res = &lcore_resource[rx_lcore_id];
1102                 res->enabled = 1;
1103                 res->port[res->port_num++] = portid;
1104
1105                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid);
1106         }
1107
1108         nb_ports_available = nb_ports;
1109
1110         /* Initialise each port */
1111         for (portid = 0; portid < nb_ports; portid++) {
1112                 /* skip ports that are not enabled */
1113                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
1114                         printf("Skipping disabled port %u\n", (unsigned) portid);
1115                         nb_ports_available--;
1116                         continue;
1117                 }
1118                 /* init port */
1119                 printf("Initializing port %u... ", (unsigned) portid);
1120                 fflush(stdout);
1121                 ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1122                 if (ret < 0)
1123                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
1124                                   ret, (unsigned) portid);
1125
1126                 rte_eth_macaddr_get(portid,&l2fwd_ports_eth_addr[portid]);
1127
1128                 /* init one RX queue */
1129                 fflush(stdout);
1130                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1131                                              rte_eth_dev_socket_id(portid),
1132                                              NULL,
1133                                              l2fwd_pktmbuf_pool[portid]);
1134                 if (ret < 0)
1135                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
1136                                   ret, (unsigned) portid);
1137
1138                 /* init one TX queue on each port */
1139                 fflush(stdout);
1140                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1141                                 rte_eth_dev_socket_id(portid),
1142                                 NULL);
1143                 if (ret < 0)
1144                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
1145                                 ret, (unsigned) portid);
1146
1147                 /* Start device */
1148                 ret = rte_eth_dev_start(portid);
1149                 if (ret < 0)
1150                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
1151                                   ret, (unsigned) portid);
1152
1153                 printf("done: \n");
1154
1155                 rte_eth_promiscuous_enable(portid);
1156
1157                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1158                                 (unsigned) portid,
1159                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1160                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1161                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1162                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1163                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1164                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1165
1166                 /* initialize port stats */
1167                 //memset(&port_statistics, 0, sizeof(port_statistics));
1168         }
1169
1170         if (!nb_ports_available) {
1171                 rte_exit(EXIT_FAILURE,
1172                         "All available ports are disabled. Please set portmask.\n");
1173         }
1174
1175         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1176
1177         /* Record pair lcore */
1178         /**
1179          * Since l2fwd example would create pair between different neighbour port, that's
1180          * port 0 receive and forward to port 1, the same to port 1, these 2 ports will have
1181          * dependency. If one port stopped working (killed, for example), the port need to
1182          * be stopped/started again. During the time, another port need to wait until stop/start
1183          * procedure completed. So, record the pair relationship for those lcores working
1184          * on ports.
1185          **/
1186         for (portid = 0; portid < nb_ports; portid++) {
1187                 uint32_t pair_port;
1188                 unsigned lcore = 0, pair_lcore = 0;
1189                 unsigned j, find_lcore, find_pair_lcore;
1190                 /* skip ports that are not enabled */
1191                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
1192                         continue;
1193
1194                 /* Find pair ports' lcores */
1195                 find_lcore = find_pair_lcore = 0;
1196                 pair_port = l2fwd_dst_ports[portid];
1197                 for (i = 0; i < RTE_MAX_LCORE; i++) {
1198                         if (!rte_lcore_is_enabled(i))
1199                                 continue;
1200                         for (j = 0; j < lcore_queue_conf[i].n_rx_port;j++) {
1201                                 if (lcore_queue_conf[i].rx_port_list[j] == portid) {
1202                                         lcore = i;
1203                                         find_lcore = 1;
1204                                         break;
1205                                 }
1206                                 if (lcore_queue_conf[i].rx_port_list[j] == pair_port) {
1207                                         pair_lcore = i;
1208                                         find_pair_lcore = 1;
1209                                         break;
1210                                 }
1211                         }
1212                         if (find_lcore && find_pair_lcore)
1213                                 break;
1214                 }
1215                 if (!find_lcore || !find_pair_lcore)
1216                         rte_exit(EXIT_FAILURE, "Not find port=%d pair\n", portid);
1217
1218                 printf("lcore %u and %u paired\n", lcore, pair_lcore);
1219                 lcore_resource[lcore].pair_id = pair_lcore;
1220                 lcore_resource[pair_lcore].pair_id = lcore;
1221         }
1222
1223         /* Create message buffer for all master and slave */
1224         message_pool = rte_mempool_create("ms_msg_pool",
1225                            NB_CORE_MSGBUF * RTE_MAX_LCORE,
1226                            sizeof(enum l2fwd_cmd), NB_CORE_MSGBUF / 2,
1227                            0,
1228                            rte_pktmbuf_pool_init, NULL,
1229                            rte_pktmbuf_init, NULL,
1230                            rte_socket_id(), 0);
1231
1232         if (message_pool == NULL)
1233                 rte_exit(EXIT_FAILURE, "Create msg mempool failed\n");
1234
1235         /* Create ring for each master and slave pair, also register cb when slave leaves */
1236         for (i = 0; i < RTE_MAX_LCORE; i++) {
1237                 /**
1238                  * Only create ring and register slave_exit cb in case that core involved into
1239                  * packet forwarding
1240                  **/
1241                 if (lcore_resource[i].enabled) {
1242                         /* Create ring for master and slave communication */
1243                         ret = create_ms_ring(i);
1244                         if (ret != 0)
1245                                 rte_exit(EXIT_FAILURE, "Create ring for lcore=%u failed",
1246                                 i);
1247
1248                         if (flib_register_slave_exit_notify(i,
1249                                 slave_exit_cb) != 0)
1250                                 rte_exit(EXIT_FAILURE,
1251                                                 "Register master_trace_slave_exit failed");
1252                 }
1253         }
1254
1255         /* launch per-lcore init on every lcore except master */
1256         flib_mp_remote_launch(l2fwd_launch_one_lcore, NULL, SKIP_MASTER);
1257
1258         /* print statistics 10 second */
1259         prev_tsc = cur_tsc = rte_rdtsc();
1260         timer_tsc = 0;
1261         while (1) {
1262                 sleep(1);
1263                 cur_tsc = rte_rdtsc();
1264                 diff_tsc = cur_tsc - prev_tsc;
1265                 /* if timer is enabled */
1266                 if (timer_period > 0) {
1267
1268                         /* advance the timer */
1269                         timer_tsc += diff_tsc;
1270
1271                         /* if timer has reached its timeout */
1272                         if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
1273
1274                                 print_stats();
1275                                 /* reset the timer */
1276                                 timer_tsc = 0;
1277                         }
1278                 }
1279
1280                 prev_tsc = cur_tsc;
1281
1282                 /* Check any slave need restart or recreate */
1283                 rte_spinlock_lock(&res_lock);
1284                 for (i = 0; i < RTE_MAX_LCORE; i++) {
1285                         struct lcore_resource_struct *res  = &lcore_resource[i];
1286                         struct lcore_resource_struct *pair = &lcore_resource[res->pair_id];
1287
1288                         /* If find slave exited, try to reset pair */
1289                         if (res->enabled && res->flags && pair->enabled) {
1290                                 if (!pair->flags) {
1291                                         master_sendcmd_with_ack(pair->lcore_id, CMD_STOP);
1292                                         rte_spinlock_unlock(&res_lock);
1293                                         sleep(1);
1294                                         rte_spinlock_lock(&res_lock);
1295                                         if (pair->flags)
1296                                                 continue;
1297                                 }
1298                                 if (reset_pair(res->lcore_id, pair->lcore_id) != 0)
1299                                         rte_exit(EXIT_FAILURE, "failed to reset slave");
1300                                 res->flags  = 0;
1301                                 pair->flags = 0;
1302                         }
1303                 }
1304                 rte_spinlock_unlock(&res_lock);
1305         }
1306
1307 }