i40evf: fix MAC deletion when stopping
[dpdk.git] / examples / l3fwd-vf / 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
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <signal.h>
45
46 #include <rte_common.h>
47 #include <rte_byteorder.h>
48 #include <rte_log.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_eal.h>
53 #include <rte_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_spinlock.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
63 #include <rte_pci.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_ring.h>
69 #include <rte_mempool.h>
70 #include <rte_mbuf.h>
71 #include <rte_ip.h>
72 #include <rte_tcp.h>
73 #include <rte_udp.h>
74 #include <rte_string_fns.h>
75
76 #define APP_LOOKUP_EXACT_MATCH          0
77 #define APP_LOOKUP_LPM                  1
78 #define DO_RFC_1812_CHECKS
79
80 //#define APP_LOOKUP_METHOD             APP_LOOKUP_EXACT_MATCH
81 #ifndef APP_LOOKUP_METHOD
82 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
83 #endif
84
85 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
86 #include <rte_hash.h>
87 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
88 #include <rte_lpm.h>
89 #else
90 #error "APP_LOOKUP_METHOD set to incorrect value"
91 #endif
92
93 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
94
95 #define MEMPOOL_CACHE_SIZE 256
96
97 /*
98  * This expression is used to calculate the number of mbufs needed depending on user input, taking
99  *  into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
100  *  RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
101  */
102
103 #define NB_MBUF RTE_MAX (                                                                                                                                       \
104                                 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT +                                                        \
105                                 nb_ports*nb_lcores*MAX_PKT_BURST +                                                                                      \
106                                 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT +                                                          \
107                                 nb_lcores*MEMPOOL_CACHE_SIZE),                                                                                          \
108                                 (unsigned)8192)
109
110 /*
111  * RX and TX Prefetch, Host, and Write-back threshold values should be
112  * carefully set for optimal performance. Consult the network
113  * controller's datasheet and supporting DPDK documentation for guidance
114  * on how these parameters should be set.
115  */
116 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
117 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
118 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
119
120 /*
121  * These default values are optimized for use with the Intel(R) 82599 10 GbE
122  * Controller and the DPDK ixgbe PMD. Consider using other values for other
123  * network controllers and/or network drivers.
124  */
125 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
126 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
127 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
128
129 #define MAX_PKT_BURST 32
130 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
131
132 #define NB_SOCKETS 8
133
134 #define SOCKET0 0
135
136 /* Configure how many packets ahead to prefetch, when reading packets */
137 #define PREFETCH_OFFSET 3
138
139 /*
140  * Configurable number of RX/TX ring descriptors
141  */
142 #define RTE_TEST_RX_DESC_DEFAULT 128
143 #define RTE_TEST_TX_DESC_DEFAULT 512
144 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
145 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
146
147 /* ethernet addresses of ports */
148 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
149
150 /* mask of enabled ports */
151 static uint32_t enabled_port_mask = 0;
152 static int numa_on = 1; /**< NUMA is enabled by default. */
153
154 struct mbuf_table {
155         uint16_t len;
156         struct rte_mbuf *m_table[MAX_PKT_BURST];
157 };
158
159 struct lcore_rx_queue {
160         uint8_t port_id;
161         uint8_t queue_id;
162 } __rte_cache_aligned;
163
164 #define MAX_RX_QUEUE_PER_LCORE 16
165 #define MAX_TX_QUEUE_PER_PORT 1
166 #define MAX_RX_QUEUE_PER_PORT 1
167
168 #define MAX_LCORE_PARAMS 1024
169 struct lcore_params {
170         uint8_t port_id;
171         uint8_t queue_id;
172         uint8_t lcore_id;
173 } __rte_cache_aligned;
174
175 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
176 static struct lcore_params lcore_params_array_default[] = {
177         {0, 0, 2},
178         {0, 1, 2},
179         {0, 2, 2},
180         {1, 0, 2},
181         {1, 1, 2},
182         {1, 2, 2},
183         {2, 0, 2},
184         {3, 0, 3},
185         {3, 1, 3},
186 };
187
188 static struct lcore_params * lcore_params = lcore_params_array_default;
189 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
190                                 sizeof(lcore_params_array_default[0]);
191
192 static struct rte_eth_conf port_conf = {
193         .rxmode = {
194                 .mq_mode        = ETH_MQ_RX_RSS,
195                 .max_rx_pkt_len = ETHER_MAX_LEN,
196                 .split_hdr_size = 0,
197                 .header_split   = 0, /**< Header Split disabled */
198                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
199                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
200                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
201                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
202         },
203         .rx_adv_conf = {
204                 .rss_conf = {
205                         .rss_key = NULL,
206                         .rss_hf = ETH_RSS_IP,
207                 },
208         },
209         .txmode = {
210                 .mq_mode = ETH_MQ_TX_NONE,
211         },
212 };
213
214 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
215
216
217 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
218
219 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
220 #include <rte_hash_crc.h>
221 #define DEFAULT_HASH_FUNC       rte_hash_crc
222 #else
223 #include <rte_jhash.h>
224 #define DEFAULT_HASH_FUNC       rte_jhash
225 #endif
226
227 struct ipv4_5tuple {
228         uint32_t ip_dst;
229         uint32_t ip_src;
230         uint16_t port_dst;
231         uint16_t port_src;
232         uint8_t proto;
233 } __attribute__((__packed__));
234
235 struct l3fwd_route {
236         struct ipv4_5tuple key;
237         uint8_t if_out;
238 };
239
240 static struct l3fwd_route l3fwd_route_array[] = {
241         {{IPv4(100,10,0,1), IPv4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
242         {{IPv4(100,20,0,2), IPv4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
243         {{IPv4(100,30,0,3), IPv4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
244         {{IPv4(100,40,0,4), IPv4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
245 };
246
247 typedef struct rte_hash lookup_struct_t;
248 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
249
250 #define L3FWD_HASH_ENTRIES      1024
251 struct rte_hash_parameters l3fwd_hash_params = {
252         .name = "l3fwd_hash_0",
253         .entries = L3FWD_HASH_ENTRIES,
254         .key_len = sizeof(struct ipv4_5tuple),
255         .hash_func = DEFAULT_HASH_FUNC,
256         .hash_func_init_val = 0,
257         .socket_id = SOCKET0,
258 };
259
260 #define L3FWD_NUM_ROUTES \
261         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
262
263 static uint8_t l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
264 #endif
265
266 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
267 struct l3fwd_route {
268         uint32_t ip;
269         uint8_t  depth;
270         uint8_t  if_out;
271 };
272
273 static struct l3fwd_route l3fwd_route_array[] = {
274         {IPv4(1,1,1,0), 24, 0},
275         {IPv4(2,1,1,0), 24, 1},
276         {IPv4(3,1,1,0), 24, 2},
277         {IPv4(4,1,1,0), 24, 3},
278         {IPv4(5,1,1,0), 24, 4},
279         {IPv4(6,1,1,0), 24, 5},
280         {IPv4(7,1,1,0), 24, 6},
281         {IPv4(8,1,1,0), 24, 7},
282 };
283
284 #define L3FWD_NUM_ROUTES \
285         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
286
287 #define L3FWD_LPM_MAX_RULES     1024
288
289 typedef struct rte_lpm lookup_struct_t;
290 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
291 #endif
292
293 struct lcore_conf {
294         uint16_t n_rx_queue;
295         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
296         uint16_t tx_queue_id;
297         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
298         lookup_struct_t * lookup_struct;
299 } __rte_cache_aligned;
300
301 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
302 static rte_spinlock_t spinlock_conf[RTE_MAX_ETHPORTS] = {RTE_SPINLOCK_INITIALIZER};
303 /* Send burst of packets on an output interface */
304 static inline int
305 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
306 {
307         struct rte_mbuf **m_table;
308         int ret;
309         uint16_t queueid;
310
311         queueid = qconf->tx_queue_id;
312         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
313
314         rte_spinlock_lock(&spinlock_conf[port]);
315         ret = rte_eth_tx_burst(port, queueid, m_table, n);
316         rte_spinlock_unlock(&spinlock_conf[port]);
317
318         if (unlikely(ret < n)) {
319                 do {
320                         rte_pktmbuf_free(m_table[ret]);
321                 } while (++ret < n);
322         }
323
324         return 0;
325 }
326
327 /* Enqueue a single packet, and send burst if queue is filled */
328 static inline int
329 send_single_packet(struct rte_mbuf *m, uint8_t port)
330 {
331         uint32_t lcore_id;
332         uint16_t len;
333         struct lcore_conf *qconf;
334
335         lcore_id = rte_lcore_id();
336
337         qconf = &lcore_conf[lcore_id];
338         len = qconf->tx_mbufs[port].len;
339         qconf->tx_mbufs[port].m_table[len] = m;
340         len++;
341
342         /* enough pkts to be sent */
343         if (unlikely(len == MAX_PKT_BURST)) {
344                 send_burst(qconf, MAX_PKT_BURST, port);
345                 len = 0;
346         }
347
348         qconf->tx_mbufs[port].len = len;
349         return 0;
350 }
351
352 #ifdef DO_RFC_1812_CHECKS
353 static inline int
354 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
355 {
356         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
357         /*
358          * 1. The packet length reported by the Link Layer must be large
359          * enough to hold the minimum length legal IP datagram (20 bytes).
360          */
361         if (link_len < sizeof(struct ipv4_hdr))
362                 return -1;
363
364         /* 2. The IP checksum must be correct. */
365         /* this is checked in H/W */
366
367         /*
368          * 3. The IP version number must be 4. If the version number is not 4
369          * then the packet may be another version of IP, such as IPng or
370          * ST-II.
371          */
372         if (((pkt->version_ihl) >> 4) != 4)
373                 return -3;
374         /*
375          * 4. The IP header length field must be large enough to hold the
376          * minimum length legal IP datagram (20 bytes = 5 words).
377          */
378         if ((pkt->version_ihl & 0xf) < 5)
379                 return -4;
380
381         /*
382          * 5. The IP total length field must be large enough to hold the IP
383          * datagram header, whose length is specified in the IP header length
384          * field.
385          */
386         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
387                 return -5;
388
389         return 0;
390 }
391 #endif
392
393 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
394 static void
395 print_key(struct ipv4_5tuple key)
396 {
397         printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, proto = %d\n",
398                (unsigned)key.ip_dst, (unsigned)key.ip_src, key.port_dst, key.port_src, key.proto);
399 }
400
401 static inline uint8_t
402 get_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
403 {
404         struct ipv4_5tuple key;
405         struct tcp_hdr *tcp;
406         struct udp_hdr *udp;
407         int ret = 0;
408
409         key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
410         key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
411         key.proto = ipv4_hdr->next_proto_id;
412
413         switch (ipv4_hdr->next_proto_id) {
414         case IPPROTO_TCP:
415                 tcp = (struct tcp_hdr *)((unsigned char *) ipv4_hdr +
416                                         sizeof(struct ipv4_hdr));
417                 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
418                 key.port_src = rte_be_to_cpu_16(tcp->src_port);
419                 break;
420
421         case IPPROTO_UDP:
422                 udp = (struct udp_hdr *)((unsigned char *) ipv4_hdr +
423                                         sizeof(struct ipv4_hdr));
424                 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
425                 key.port_src = rte_be_to_cpu_16(udp->src_port);
426                 break;
427
428         default:
429                 key.port_dst = 0;
430                 key.port_src = 0;
431         }
432
433         /* Find destination port */
434         ret = rte_hash_lookup(l3fwd_lookup_struct, (const void *)&key);
435         return (uint8_t)((ret < 0)? portid : l3fwd_out_if[ret]);
436 }
437 #endif
438
439 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
440 static inline uint8_t
441 get_dst_port(struct ipv4_hdr *ipv4_hdr,  uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
442 {
443         uint8_t next_hop;
444
445         return (uint8_t) ((rte_lpm_lookup(l3fwd_lookup_struct,
446                         rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0)?
447                         next_hop : portid);
448 }
449 #endif
450
451 static inline void
452 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, lookup_struct_t * l3fwd_lookup_struct)
453 {
454         struct ether_hdr *eth_hdr;
455         struct ipv4_hdr *ipv4_hdr;
456         void *tmp;
457         uint8_t dst_port;
458
459         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
460
461         ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
462                                            sizeof(struct ether_hdr));
463
464 #ifdef DO_RFC_1812_CHECKS
465         /* Check to make sure the packet is valid (RFC1812) */
466         if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
467                 rte_pktmbuf_free(m);
468                 return;
469         }
470 #endif
471
472         dst_port = get_dst_port(ipv4_hdr, portid, l3fwd_lookup_struct);
473         if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
474                 dst_port = portid;
475
476         /* 02:00:00:00:00:xx */
477         tmp = &eth_hdr->d_addr.addr_bytes[0];
478         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
479
480 #ifdef DO_RFC_1812_CHECKS
481         /* Update time to live and header checksum */
482         --(ipv4_hdr->time_to_live);
483         ++(ipv4_hdr->hdr_checksum);
484 #endif
485
486         /* src addr */
487         ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
488
489         send_single_packet(m, dst_port);
490
491 }
492
493 /* main processing loop */
494 static int
495 main_loop(__attribute__((unused)) void *dummy)
496 {
497         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
498         unsigned lcore_id;
499         uint64_t prev_tsc, diff_tsc, cur_tsc;
500         int i, j, nb_rx;
501         uint8_t portid, queueid;
502         struct lcore_conf *qconf;
503         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
504
505         prev_tsc = 0;
506
507         lcore_id = rte_lcore_id();
508         qconf = &lcore_conf[lcore_id];
509
510         if (qconf->n_rx_queue == 0) {
511                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
512                 return 0;
513         }
514
515         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
516
517         for (i = 0; i < qconf->n_rx_queue; i++) {
518
519                 portid = qconf->rx_queue_list[i].port_id;
520                 queueid = qconf->rx_queue_list[i].queue_id;
521                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
522                         portid, queueid);
523         }
524
525         while (1) {
526
527                 cur_tsc = rte_rdtsc();
528
529                 /*
530                  * TX burst queue drain
531                  */
532                 diff_tsc = cur_tsc - prev_tsc;
533                 if (unlikely(diff_tsc > drain_tsc)) {
534
535                         /*
536                          * This could be optimized (use queueid instead of
537                          * portid), but it is not called so often
538                          */
539                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
540                                 if (qconf->tx_mbufs[portid].len == 0)
541                                         continue;
542                                 send_burst(&lcore_conf[lcore_id],
543                                         qconf->tx_mbufs[portid].len,
544                                         portid);
545                                 qconf->tx_mbufs[portid].len = 0;
546                         }
547
548                         prev_tsc = cur_tsc;
549                 }
550
551                 /*
552                  * Read packet from RX queues
553                  */
554                 for (i = 0; i < qconf->n_rx_queue; ++i) {
555
556                         portid = qconf->rx_queue_list[i].port_id;
557                         queueid = qconf->rx_queue_list[i].queue_id;
558                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst, MAX_PKT_BURST);
559
560                         /* Prefetch first packets */
561                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
562                                 rte_prefetch0(rte_pktmbuf_mtod(
563                                                 pkts_burst[j], void *));
564                         }
565
566                         /* Prefetch and forward already prefetched packets */
567                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
568                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
569                                                 j + PREFETCH_OFFSET], void *));
570                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
571                         }
572
573                         /* Forward remaining prefetched packets */
574                         for (; j < nb_rx; j++) {
575                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
576                         }
577                 }
578         }
579 }
580
581 static int
582 check_lcore_params(void)
583 {
584         uint8_t queue, lcore;
585         uint16_t i;
586         int socketid;
587
588         for (i = 0; i < nb_lcore_params; ++i) {
589                 queue = lcore_params[i].queue_id;
590                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
591                         printf("invalid queue number: %hhu\n", queue);
592                         return -1;
593                 }
594                 lcore = lcore_params[i].lcore_id;
595                 if (!rte_lcore_is_enabled(lcore)) {
596                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
597                         return -1;
598                 }
599                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
600                         (numa_on == 0)) {
601                         printf("warning: lcore %hhu is on socket %d with numa off \n",
602                                 lcore, socketid);
603                 }
604         }
605         return 0;
606 }
607
608 static int
609 check_port_config(const unsigned nb_ports)
610 {
611         unsigned portid;
612         uint16_t i;
613
614         for (i = 0; i < nb_lcore_params; ++i) {
615                 portid = lcore_params[i].port_id;
616                 if ((enabled_port_mask & (1 << portid)) == 0) {
617                         printf("port %u is not enabled in port mask\n", portid);
618                         return -1;
619                 }
620                 if (portid >= nb_ports) {
621                         printf("port %u is not present on the board\n", portid);
622                         return -1;
623                 }
624         }
625         return 0;
626 }
627
628 static uint8_t
629 get_port_n_rx_queues(const uint8_t port)
630 {
631         int queue = -1;
632         uint16_t i;
633
634         for (i = 0; i < nb_lcore_params; ++i) {
635                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
636                         queue = lcore_params[i].queue_id;
637         }
638         return (uint8_t)(++queue);
639 }
640
641 static int
642 init_lcore_rx_queues(void)
643 {
644         uint16_t i, nb_rx_queue;
645         uint8_t lcore;
646
647         for (i = 0; i < nb_lcore_params; ++i) {
648                 lcore = lcore_params[i].lcore_id;
649                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
650                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
651                         printf("error: too many queues (%u) for lcore: %u\n",
652                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
653                         return -1;
654                 } else {
655                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
656                                 lcore_params[i].port_id;
657                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
658                                 lcore_params[i].queue_id;
659                         lcore_conf[lcore].n_rx_queue++;
660                 }
661         }
662         return 0;
663 }
664
665 /* display usage */
666 static void
667 print_usage(const char *prgname)
668 {
669         printf ("%s [EAL options] -- -p PORTMASK"
670                 "  [--config (port,queue,lcore)[,(port,queue,lcore]]\n"
671                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
672                 "  --config (port,queue,lcore): rx queues configuration\n"
673                 "  --no-numa: optional, disable numa awareness\n",
674                 prgname);
675 }
676
677 /* Custom handling of signals to handle process terminal */
678 static void
679 signal_handler(int signum)
680 {
681         uint8_t portid;
682         uint8_t nb_ports = rte_eth_dev_count();
683
684         /* When we receive a SIGINT signal */
685         if (signum == SIGINT) {
686                 for (portid = 0; portid < nb_ports; portid++) {
687                         /* skip ports that are not enabled */
688                         if ((enabled_port_mask & (1 << portid)) == 0)
689                                 continue;
690                         rte_eth_dev_close(portid);
691                 }
692         }
693         rte_exit(EXIT_SUCCESS, "\n User forced exit\n");
694 }
695 static int
696 parse_portmask(const char *portmask)
697 {
698         char *end = NULL;
699         unsigned long pm;
700
701         /* parse hexadecimal string */
702         pm = strtoul(portmask, &end, 16);
703         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
704                 return -1;
705
706         if (pm == 0)
707                 return -1;
708
709         return pm;
710 }
711
712 static int
713 parse_config(const char *q_arg)
714 {
715         char s[256];
716         const char *p, *p0 = q_arg;
717         char *end;
718         enum fieldnames {
719                 FLD_PORT = 0,
720                 FLD_QUEUE,
721                 FLD_LCORE,
722                 _NUM_FLD
723         };
724         unsigned long int_fld[_NUM_FLD];
725         char *str_fld[_NUM_FLD];
726         int i;
727         unsigned size;
728
729         nb_lcore_params = 0;
730
731         while ((p = strchr(p0,'(')) != NULL) {
732                 ++p;
733                 if((p0 = strchr(p,')')) == NULL)
734                         return -1;
735
736                 size = p0 - p;
737                 if(size >= sizeof(s))
738                         return -1;
739
740                 snprintf(s, sizeof(s), "%.*s", size, p);
741                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
742                         return -1;
743                 for (i = 0; i < _NUM_FLD; i++){
744                         errno = 0;
745                         int_fld[i] = strtoul(str_fld[i], &end, 0);
746                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
747                                 return -1;
748                 }
749                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
750                         printf("exceeded max number of lcore params: %hu\n",
751                                 nb_lcore_params);
752                         return -1;
753                 }
754                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
755                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
756                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
757                 ++nb_lcore_params;
758         }
759         lcore_params = lcore_params_array;
760         return 0;
761 }
762
763 /* Parse the argument given in the command line of the application */
764 static int
765 parse_args(int argc, char **argv)
766 {
767         int opt, ret;
768         char **argvopt;
769         int option_index;
770         char *prgname = argv[0];
771         static struct option lgopts[] = {
772                 {"config", 1, 0, 0},
773                 {"no-numa", 0, 0, 0},
774                 {NULL, 0, 0, 0}
775         };
776
777         argvopt = argv;
778
779         while ((opt = getopt_long(argc, argvopt, "p:",
780                                 lgopts, &option_index)) != EOF) {
781
782                 switch (opt) {
783                 /* portmask */
784                 case 'p':
785                         enabled_port_mask = parse_portmask(optarg);
786                         if (enabled_port_mask == 0) {
787                                 printf("invalid portmask\n");
788                                 print_usage(prgname);
789                                 return -1;
790                         }
791                         break;
792
793                 /* long options */
794                 case 0:
795                         if (!strcmp(lgopts[option_index].name, "config")) {
796                                 ret = parse_config(optarg);
797                                 if (ret) {
798                                         printf("invalid config\n");
799                                         print_usage(prgname);
800                                         return -1;
801                                 }
802                         }
803
804                         if (!strcmp(lgopts[option_index].name, "no-numa")) {
805                                 printf("numa is disabled \n");
806                                 numa_on = 0;
807                         }
808                         break;
809
810                 default:
811                         print_usage(prgname);
812                         return -1;
813                 }
814         }
815
816         if (optind >= 0)
817                 argv[optind-1] = prgname;
818
819         ret = optind-1;
820         optind = 0; /* reset getopt lib */
821         return ret;
822 }
823
824 static void
825 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
826 {
827         char buf[ETHER_ADDR_FMT_SIZE];
828         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
829         printf("%s%s", name, buf);
830 }
831
832 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
833 static void
834 setup_hash(int socketid)
835 {
836         unsigned i;
837         int ret;
838         char s[64];
839
840         /* create  hashes */
841         snprintf(s, sizeof(s), "l3fwd_hash_%d", socketid);
842         l3fwd_hash_params.name = s;
843         l3fwd_hash_params.socket_id = socketid;
844         l3fwd_lookup_struct[socketid] = rte_hash_create(&l3fwd_hash_params);
845         if (l3fwd_lookup_struct[socketid] == NULL)
846                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
847                                 "socket %d\n", socketid);
848
849         /* populate the hash */
850         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
851                 ret = rte_hash_add_key (l3fwd_lookup_struct[socketid],
852                                 (void *) &l3fwd_route_array[i].key);
853                 if (ret < 0) {
854                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
855                                 "l3fwd hash on socket %d\n", i, socketid);
856                 }
857                 l3fwd_out_if[ret] = l3fwd_route_array[i].if_out;
858                 printf("Hash: Adding key\n");
859                 print_key(l3fwd_route_array[i].key);
860         }
861 }
862 #endif
863
864 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
865 static void
866 setup_lpm(int socketid)
867 {
868         unsigned i;
869         int ret;
870         char s[64];
871
872         /* create the LPM table */
873         snprintf(s, sizeof(s), "L3FWD_LPM_%d", socketid);
874         l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
875                                 L3FWD_LPM_MAX_RULES, 0);
876         if (l3fwd_lookup_struct[socketid] == NULL)
877                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
878                                 " on socket %d\n", socketid);
879
880         /* populate the LPM table */
881         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
882                 ret = rte_lpm_add(l3fwd_lookup_struct[socketid],
883                         l3fwd_route_array[i].ip,
884                         l3fwd_route_array[i].depth,
885                         l3fwd_route_array[i].if_out);
886
887                 if (ret < 0) {
888                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
889                                 "l3fwd LPM table on socket %d\n",
890                                 i, socketid);
891                 }
892
893                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
894                         (unsigned)l3fwd_route_array[i].ip,
895                         l3fwd_route_array[i].depth,
896                         l3fwd_route_array[i].if_out);
897         }
898 }
899 #endif
900
901 static int
902 init_mem(unsigned nb_mbuf)
903 {
904         struct lcore_conf *qconf;
905         int socketid;
906         unsigned lcore_id;
907         char s[64];
908
909         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
910                 if (rte_lcore_is_enabled(lcore_id) == 0)
911                         continue;
912
913                 if (numa_on)
914                         socketid = rte_lcore_to_socket_id(lcore_id);
915                 else
916                         socketid = 0;
917
918                 if (socketid >= NB_SOCKETS) {
919                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
920                                 socketid, lcore_id, NB_SOCKETS);
921                 }
922                 if (pktmbuf_pool[socketid] == NULL) {
923                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
924                         pktmbuf_pool[socketid] = rte_pktmbuf_pool_create(s,
925                                 nb_mbuf, MEMPOOL_CACHE_SIZE, 0,
926                                 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
927                         if (pktmbuf_pool[socketid] == NULL)
928                                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n", socketid);
929                         else
930                                 printf("Allocated mbuf pool on socket %d\n", socketid);
931
932 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
933                         setup_lpm(socketid);
934 #else
935                         setup_hash(socketid);
936 #endif
937                 }
938                 qconf = &lcore_conf[lcore_id];
939                 qconf->lookup_struct = l3fwd_lookup_struct[socketid];
940         }
941         return 0;
942 }
943
944 int
945 main(int argc, char **argv)
946 {
947         struct lcore_conf *qconf;
948         struct rte_eth_dev_info dev_info;
949         struct rte_eth_txconf *txconf;
950         int ret;
951         unsigned nb_ports;
952         uint16_t queueid;
953         unsigned lcore_id;
954         uint32_t nb_lcores;
955         uint16_t n_tx_queue;
956         uint8_t portid, nb_rx_queue, queue, socketid;
957
958         signal(SIGINT, signal_handler);
959         /* init EAL */
960         ret = rte_eal_init(argc, argv);
961         if (ret < 0)
962                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
963         argc -= ret;
964         argv += ret;
965
966         /* parse application arguments (after the EAL ones) */
967         ret = parse_args(argc, argv);
968         if (ret < 0)
969                 rte_exit(EXIT_FAILURE, "Invalid L3FWD-VF parameters\n");
970
971         if (check_lcore_params() < 0)
972                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
973
974         ret = init_lcore_rx_queues();
975         if (ret < 0)
976                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
977
978         nb_ports = rte_eth_dev_count();
979         if (nb_ports > RTE_MAX_ETHPORTS)
980                 nb_ports = RTE_MAX_ETHPORTS;
981
982         if (check_port_config(nb_ports) < 0)
983                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
984
985         nb_lcores = rte_lcore_count();
986
987         /* initialize all ports */
988         for (portid = 0; portid < nb_ports; portid++) {
989                 /* skip ports that are not enabled */
990                 if ((enabled_port_mask & (1 << portid)) == 0) {
991                         printf("\nSkipping disabled port %d\n", portid);
992                         continue;
993                 }
994
995                 /* init port */
996                 printf("Initializing port %d ... ", portid );
997                 fflush(stdout);
998
999                 /* must always equal(=1) */
1000                 nb_rx_queue = get_port_n_rx_queues(portid);
1001                 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1002
1003                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1004                         nb_rx_queue, (unsigned)1 );
1005                 ret = rte_eth_dev_configure(portid, nb_rx_queue, n_tx_queue, &port_conf);
1006                 if (ret < 0)
1007                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
1008                                 ret, portid);
1009
1010                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1011                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1012                 printf(", ");
1013
1014                 ret = init_mem(NB_MBUF);
1015                 if (ret < 0)
1016                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
1017
1018                 /* init one TX queue */
1019                 socketid = (uint8_t)rte_lcore_to_socket_id(rte_get_master_lcore());
1020
1021                 printf("txq=%d,%d,%d ", portid, 0, socketid);
1022                 fflush(stdout);
1023
1024                 rte_eth_dev_info_get(portid, &dev_info);
1025                 txconf = &dev_info.default_txconf;
1026                 if (port_conf.rxmode.jumbo_frame)
1027                         txconf->txq_flags = 0;
1028                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1029                                                  socketid, txconf);
1030                 if (ret < 0)
1031                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1032                                 "port=%d\n", ret, portid);
1033
1034                 printf("\n");
1035         }
1036
1037         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1038                 if (rte_lcore_is_enabled(lcore_id) == 0)
1039                         continue;
1040                 qconf = &lcore_conf[lcore_id];
1041                 qconf->tx_queue_id = 0;
1042
1043                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1044                 fflush(stdout);
1045                 /* init RX queues */
1046                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1047                         portid = qconf->rx_queue_list[queue].port_id;
1048                         queueid = qconf->rx_queue_list[queue].queue_id;
1049
1050                         if (numa_on)
1051                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1052                         else
1053                                 socketid = 0;
1054
1055                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1056                         fflush(stdout);
1057
1058                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1059                                                 socketid, NULL,
1060                                                 pktmbuf_pool[socketid]);
1061                         if (ret < 0)
1062                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1063                                                 "port=%d\n", ret, portid);
1064                 }
1065         }
1066         printf("\n");
1067
1068         /* start ports */
1069         for (portid = 0; portid < nb_ports; portid++) {
1070                 if ((enabled_port_mask & (1 << portid)) == 0) {
1071                         continue;
1072                 }
1073                 /* Start device */
1074                 ret = rte_eth_dev_start(portid);
1075                 if (ret < 0)
1076                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1077                                 ret, portid);
1078
1079                 printf("done: Port %d\n", portid);
1080
1081         }
1082
1083         /* launch per-lcore init on every lcore */
1084         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1085         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1086                 if (rte_eal_wait_lcore(lcore_id) < 0)
1087                         return -1;
1088         }
1089
1090         return 0;
1091 }