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