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