examples: use SPDX tag for Intel copyright files
[dpdk.git] / examples / kni / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <string.h>
10 #include <sys/queue.h>
11 #include <stdarg.h>
12 #include <errno.h>
13 #include <getopt.h>
14
15 #include <netinet/in.h>
16 #include <linux/if.h>
17 #include <linux/if_tun.h>
18 #include <fcntl.h>
19 #include <sys/ioctl.h>
20 #include <unistd.h>
21 #include <signal.h>
22
23 #include <rte_common.h>
24 #include <rte_log.h>
25 #include <rte_memory.h>
26 #include <rte_memcpy.h>
27 #include <rte_eal.h>
28 #include <rte_per_lcore.h>
29 #include <rte_launch.h>
30 #include <rte_atomic.h>
31 #include <rte_lcore.h>
32 #include <rte_branch_prediction.h>
33 #include <rte_interrupts.h>
34 #include <rte_bus_pci.h>
35 #include <rte_debug.h>
36 #include <rte_ether.h>
37 #include <rte_ethdev.h>
38 #include <rte_mempool.h>
39 #include <rte_mbuf.h>
40 #include <rte_string_fns.h>
41 #include <rte_cycles.h>
42 #include <rte_malloc.h>
43 #include <rte_kni.h>
44
45 /* Macros for printing using RTE_LOG */
46 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
47
48 /* Max size of a single packet */
49 #define MAX_PACKET_SZ           2048
50
51 /* Size of the data buffer in each mbuf */
52 #define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
53
54 /* Number of mbufs in mempool that is created */
55 #define NB_MBUF                 (8192 * 16)
56
57 /* How many packets to attempt to read from NIC in one go */
58 #define PKT_BURST_SZ            32
59
60 /* How many objects (mbufs) to keep in per-lcore mempool cache */
61 #define MEMPOOL_CACHE_SZ        PKT_BURST_SZ
62
63 /* Number of RX ring descriptors */
64 #define NB_RXD                  128
65
66 /* Number of TX ring descriptors */
67 #define NB_TXD                  512
68
69 /* Total octets in ethernet header */
70 #define KNI_ENET_HEADER_SIZE    14
71
72 /* Total octets in the FCS */
73 #define KNI_ENET_FCS_SIZE       4
74
75 #define KNI_US_PER_SECOND       1000000
76 #define KNI_SECOND_PER_DAY      86400
77
78 #define KNI_MAX_KTHREAD 32
79 /*
80  * Structure of port parameters
81  */
82 struct kni_port_params {
83         uint16_t port_id;/* Port ID */
84         unsigned lcore_rx; /* lcore ID for RX */
85         unsigned lcore_tx; /* lcore ID for TX */
86         uint32_t nb_lcore_k; /* Number of lcores for KNI multi kernel threads */
87         uint32_t nb_kni; /* Number of KNI devices to be created */
88         unsigned lcore_k[KNI_MAX_KTHREAD]; /* lcore ID list for kthreads */
89         struct rte_kni *kni[KNI_MAX_KTHREAD]; /* KNI context pointers */
90 } __rte_cache_aligned;
91
92 static struct kni_port_params *kni_port_params_array[RTE_MAX_ETHPORTS];
93
94
95 /* Options for configuring ethernet port */
96 static struct rte_eth_conf port_conf = {
97         .rxmode = {
98                 .header_split = 0,      /* Header Split disabled */
99                 .hw_ip_checksum = 0,    /* IP checksum offload disabled */
100                 .hw_vlan_filter = 0,    /* VLAN filtering disabled */
101                 .jumbo_frame = 0,       /* Jumbo Frame Support disabled */
102                 .hw_strip_crc = 1,      /* CRC stripped by hardware */
103         },
104         .txmode = {
105                 .mq_mode = ETH_MQ_TX_NONE,
106         },
107 };
108
109 /* Mempool for mbufs */
110 static struct rte_mempool * pktmbuf_pool = NULL;
111
112 /* Mask of enabled ports */
113 static uint32_t ports_mask = 0;
114 /* Ports set in promiscuous mode off by default. */
115 static int promiscuous_on = 0;
116
117 /* Structure type for recording kni interface specific stats */
118 struct kni_interface_stats {
119         /* number of pkts received from NIC, and sent to KNI */
120         uint64_t rx_packets;
121
122         /* number of pkts received from NIC, but failed to send to KNI */
123         uint64_t rx_dropped;
124
125         /* number of pkts received from KNI, and sent to NIC */
126         uint64_t tx_packets;
127
128         /* number of pkts received from KNI, but failed to send to NIC */
129         uint64_t tx_dropped;
130 };
131
132 /* kni device statistics array */
133 static struct kni_interface_stats kni_stats[RTE_MAX_ETHPORTS];
134
135 static int kni_change_mtu(uint16_t port_id, unsigned int new_mtu);
136 static int kni_config_network_interface(uint16_t port_id, uint8_t if_up);
137
138 static rte_atomic32_t kni_stop = RTE_ATOMIC32_INIT(0);
139
140 /* Print out statistics on packets handled */
141 static void
142 print_stats(void)
143 {
144         uint16_t i;
145
146         printf("\n**KNI example application statistics**\n"
147                "======  ==============  ============  ============  ============  ============\n"
148                " Port    Lcore(RX/TX)    rx_packets    rx_dropped    tx_packets    tx_dropped\n"
149                "------  --------------  ------------  ------------  ------------  ------------\n");
150         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
151                 if (!kni_port_params_array[i])
152                         continue;
153
154                 printf("%7d %10u/%2u %13"PRIu64" %13"PRIu64" %13"PRIu64" "
155                                                         "%13"PRIu64"\n", i,
156                                         kni_port_params_array[i]->lcore_rx,
157                                         kni_port_params_array[i]->lcore_tx,
158                                                 kni_stats[i].rx_packets,
159                                                 kni_stats[i].rx_dropped,
160                                                 kni_stats[i].tx_packets,
161                                                 kni_stats[i].tx_dropped);
162         }
163         printf("======  ==============  ============  ============  ============  ============\n");
164 }
165
166 /* Custom handling of signals to handle stats and kni processing */
167 static void
168 signal_handler(int signum)
169 {
170         /* When we receive a USR1 signal, print stats */
171         if (signum == SIGUSR1) {
172                 print_stats();
173         }
174
175         /* When we receive a USR2 signal, reset stats */
176         if (signum == SIGUSR2) {
177                 memset(&kni_stats, 0, sizeof(kni_stats));
178                 printf("\n**Statistics have been reset**\n");
179                 return;
180         }
181
182         /* When we receive a RTMIN or SIGINT signal, stop kni processing */
183         if (signum == SIGRTMIN || signum == SIGINT){
184                 printf("SIGRTMIN is received, and the KNI processing is "
185                                                         "going to stop\n");
186                 rte_atomic32_inc(&kni_stop);
187                 return;
188         }
189 }
190
191 static void
192 kni_burst_free_mbufs(struct rte_mbuf **pkts, unsigned num)
193 {
194         unsigned i;
195
196         if (pkts == NULL)
197                 return;
198
199         for (i = 0; i < num; i++) {
200                 rte_pktmbuf_free(pkts[i]);
201                 pkts[i] = NULL;
202         }
203 }
204
205 /**
206  * Interface to burst rx and enqueue mbufs into rx_q
207  */
208 static void
209 kni_ingress(struct kni_port_params *p)
210 {
211         uint8_t i;
212         uint16_t port_id;
213         unsigned nb_rx, num;
214         uint32_t nb_kni;
215         struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
216
217         if (p == NULL)
218                 return;
219
220         nb_kni = p->nb_kni;
221         port_id = p->port_id;
222         for (i = 0; i < nb_kni; i++) {
223                 /* Burst rx from eth */
224                 nb_rx = rte_eth_rx_burst(port_id, 0, pkts_burst, PKT_BURST_SZ);
225                 if (unlikely(nb_rx > PKT_BURST_SZ)) {
226                         RTE_LOG(ERR, APP, "Error receiving from eth\n");
227                         return;
228                 }
229                 /* Burst tx to kni */
230                 num = rte_kni_tx_burst(p->kni[i], pkts_burst, nb_rx);
231                 kni_stats[port_id].rx_packets += num;
232
233                 rte_kni_handle_request(p->kni[i]);
234                 if (unlikely(num < nb_rx)) {
235                         /* Free mbufs not tx to kni interface */
236                         kni_burst_free_mbufs(&pkts_burst[num], nb_rx - num);
237                         kni_stats[port_id].rx_dropped += nb_rx - num;
238                 }
239         }
240 }
241
242 /**
243  * Interface to dequeue mbufs from tx_q and burst tx
244  */
245 static void
246 kni_egress(struct kni_port_params *p)
247 {
248         uint8_t i;
249         uint16_t port_id;
250         unsigned nb_tx, num;
251         uint32_t nb_kni;
252         struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
253
254         if (p == NULL)
255                 return;
256
257         nb_kni = p->nb_kni;
258         port_id = p->port_id;
259         for (i = 0; i < nb_kni; i++) {
260                 /* Burst rx from kni */
261                 num = rte_kni_rx_burst(p->kni[i], pkts_burst, PKT_BURST_SZ);
262                 if (unlikely(num > PKT_BURST_SZ)) {
263                         RTE_LOG(ERR, APP, "Error receiving from KNI\n");
264                         return;
265                 }
266                 /* Burst tx to eth */
267                 nb_tx = rte_eth_tx_burst(port_id, 0, pkts_burst, (uint16_t)num);
268                 kni_stats[port_id].tx_packets += nb_tx;
269                 if (unlikely(nb_tx < num)) {
270                         /* Free mbufs not tx to NIC */
271                         kni_burst_free_mbufs(&pkts_burst[nb_tx], num - nb_tx);
272                         kni_stats[port_id].tx_dropped += num - nb_tx;
273                 }
274         }
275 }
276
277 static int
278 main_loop(__rte_unused void *arg)
279 {
280         uint8_t i, nb_ports = rte_eth_dev_count();
281         int32_t f_stop;
282         const unsigned lcore_id = rte_lcore_id();
283         enum lcore_rxtx {
284                 LCORE_NONE,
285                 LCORE_RX,
286                 LCORE_TX,
287                 LCORE_MAX
288         };
289         enum lcore_rxtx flag = LCORE_NONE;
290
291         for (i = 0; i < nb_ports; i++) {
292                 if (!kni_port_params_array[i])
293                         continue;
294                 if (kni_port_params_array[i]->lcore_rx == (uint8_t)lcore_id) {
295                         flag = LCORE_RX;
296                         break;
297                 } else if (kni_port_params_array[i]->lcore_tx ==
298                                                 (uint8_t)lcore_id) {
299                         flag = LCORE_TX;
300                         break;
301                 }
302         }
303
304         if (flag == LCORE_RX) {
305                 RTE_LOG(INFO, APP, "Lcore %u is reading from port %d\n",
306                                         kni_port_params_array[i]->lcore_rx,
307                                         kni_port_params_array[i]->port_id);
308                 while (1) {
309                         f_stop = rte_atomic32_read(&kni_stop);
310                         if (f_stop)
311                                 break;
312                         kni_ingress(kni_port_params_array[i]);
313                 }
314         } else if (flag == LCORE_TX) {
315                 RTE_LOG(INFO, APP, "Lcore %u is writing to port %d\n",
316                                         kni_port_params_array[i]->lcore_tx,
317                                         kni_port_params_array[i]->port_id);
318                 while (1) {
319                         f_stop = rte_atomic32_read(&kni_stop);
320                         if (f_stop)
321                                 break;
322                         kni_egress(kni_port_params_array[i]);
323                 }
324         } else
325                 RTE_LOG(INFO, APP, "Lcore %u has nothing to do\n", lcore_id);
326
327         return 0;
328 }
329
330 /* Display usage instructions */
331 static void
332 print_usage(const char *prgname)
333 {
334         RTE_LOG(INFO, APP, "\nUsage: %s [EAL options] -- -p PORTMASK -P "
335                    "[--config (port,lcore_rx,lcore_tx,lcore_kthread...)"
336                    "[,(port,lcore_rx,lcore_tx,lcore_kthread...)]]\n"
337                    "    -p PORTMASK: hex bitmask of ports to use\n"
338                    "    -P : enable promiscuous mode\n"
339                    "    --config (port,lcore_rx,lcore_tx,lcore_kthread...): "
340                    "port and lcore configurations\n",
341                    prgname);
342 }
343
344 /* Convert string to unsigned number. 0 is returned if error occurs */
345 static uint32_t
346 parse_unsigned(const char *portmask)
347 {
348         char *end = NULL;
349         unsigned long num;
350
351         num = strtoul(portmask, &end, 16);
352         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
353                 return 0;
354
355         return (uint32_t)num;
356 }
357
358 static void
359 print_config(void)
360 {
361         uint32_t i, j;
362         struct kni_port_params **p = kni_port_params_array;
363
364         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
365                 if (!p[i])
366                         continue;
367                 RTE_LOG(DEBUG, APP, "Port ID: %d\n", p[i]->port_id);
368                 RTE_LOG(DEBUG, APP, "Rx lcore ID: %u, Tx lcore ID: %u\n",
369                                         p[i]->lcore_rx, p[i]->lcore_tx);
370                 for (j = 0; j < p[i]->nb_lcore_k; j++)
371                         RTE_LOG(DEBUG, APP, "Kernel thread lcore ID: %u\n",
372                                                         p[i]->lcore_k[j]);
373         }
374 }
375
376 static int
377 parse_config(const char *arg)
378 {
379         const char *p, *p0 = arg;
380         char s[256], *end;
381         unsigned size;
382         enum fieldnames {
383                 FLD_PORT = 0,
384                 FLD_LCORE_RX,
385                 FLD_LCORE_TX,
386                 _NUM_FLD = KNI_MAX_KTHREAD + 3,
387         };
388         int i, j, nb_token;
389         char *str_fld[_NUM_FLD];
390         unsigned long int_fld[_NUM_FLD];
391         uint16_t port_id, nb_kni_port_params = 0;
392
393         memset(&kni_port_params_array, 0, sizeof(kni_port_params_array));
394         while (((p = strchr(p0, '(')) != NULL) &&
395                 nb_kni_port_params < RTE_MAX_ETHPORTS) {
396                 p++;
397                 if ((p0 = strchr(p, ')')) == NULL)
398                         goto fail;
399                 size = p0 - p;
400                 if (size >= sizeof(s)) {
401                         printf("Invalid config parameters\n");
402                         goto fail;
403                 }
404                 snprintf(s, sizeof(s), "%.*s", size, p);
405                 nb_token = rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',');
406                 if (nb_token <= FLD_LCORE_TX) {
407                         printf("Invalid config parameters\n");
408                         goto fail;
409                 }
410                 for (i = 0; i < nb_token; i++) {
411                         errno = 0;
412                         int_fld[i] = strtoul(str_fld[i], &end, 0);
413                         if (errno != 0 || end == str_fld[i]) {
414                                 printf("Invalid config parameters\n");
415                                 goto fail;
416                         }
417                 }
418
419                 i = 0;
420                 port_id = int_fld[i++];
421                 if (port_id >= RTE_MAX_ETHPORTS) {
422                         printf("Port ID %d could not exceed the maximum %d\n",
423                                                 port_id, RTE_MAX_ETHPORTS);
424                         goto fail;
425                 }
426                 if (kni_port_params_array[port_id]) {
427                         printf("Port %d has been configured\n", port_id);
428                         goto fail;
429                 }
430                 kni_port_params_array[port_id] =
431                         rte_zmalloc("KNI_port_params",
432                                     sizeof(struct kni_port_params), RTE_CACHE_LINE_SIZE);
433                 kni_port_params_array[port_id]->port_id = port_id;
434                 kni_port_params_array[port_id]->lcore_rx =
435                                         (uint8_t)int_fld[i++];
436                 kni_port_params_array[port_id]->lcore_tx =
437                                         (uint8_t)int_fld[i++];
438                 if (kni_port_params_array[port_id]->lcore_rx >= RTE_MAX_LCORE ||
439                 kni_port_params_array[port_id]->lcore_tx >= RTE_MAX_LCORE) {
440                         printf("lcore_rx %u or lcore_tx %u ID could not "
441                                                 "exceed the maximum %u\n",
442                                 kni_port_params_array[port_id]->lcore_rx,
443                                 kni_port_params_array[port_id]->lcore_tx,
444                                                 (unsigned)RTE_MAX_LCORE);
445                         goto fail;
446                 }
447                 for (j = 0; i < nb_token && j < KNI_MAX_KTHREAD; i++, j++)
448                         kni_port_params_array[port_id]->lcore_k[j] =
449                                                 (uint8_t)int_fld[i];
450                 kni_port_params_array[port_id]->nb_lcore_k = j;
451         }
452         print_config();
453
454         return 0;
455
456 fail:
457         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
458                 if (kni_port_params_array[i]) {
459                         rte_free(kni_port_params_array[i]);
460                         kni_port_params_array[i] = NULL;
461                 }
462         }
463
464         return -1;
465 }
466
467 static int
468 validate_parameters(uint32_t portmask)
469 {
470         uint32_t i;
471
472         if (!portmask) {
473                 printf("No port configured in port mask\n");
474                 return -1;
475         }
476
477         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
478                 if (((portmask & (1 << i)) && !kni_port_params_array[i]) ||
479                         (!(portmask & (1 << i)) && kni_port_params_array[i]))
480                         rte_exit(EXIT_FAILURE, "portmask is not consistent "
481                                 "to port ids specified in --config\n");
482
483                 if (kni_port_params_array[i] && !rte_lcore_is_enabled(\
484                         (unsigned)(kni_port_params_array[i]->lcore_rx)))
485                         rte_exit(EXIT_FAILURE, "lcore id %u for "
486                                         "port %d receiving not enabled\n",
487                                         kni_port_params_array[i]->lcore_rx,
488                                         kni_port_params_array[i]->port_id);
489
490                 if (kni_port_params_array[i] && !rte_lcore_is_enabled(\
491                         (unsigned)(kni_port_params_array[i]->lcore_tx)))
492                         rte_exit(EXIT_FAILURE, "lcore id %u for "
493                                         "port %d transmitting not enabled\n",
494                                         kni_port_params_array[i]->lcore_tx,
495                                         kni_port_params_array[i]->port_id);
496
497         }
498
499         return 0;
500 }
501
502 #define CMDLINE_OPT_CONFIG  "config"
503
504 /* Parse the arguments given in the command line of the application */
505 static int
506 parse_args(int argc, char **argv)
507 {
508         int opt, longindex, ret = 0;
509         const char *prgname = argv[0];
510         static struct option longopts[] = {
511                 {CMDLINE_OPT_CONFIG, required_argument, NULL, 0},
512                 {NULL, 0, NULL, 0}
513         };
514
515         /* Disable printing messages within getopt() */
516         opterr = 0;
517
518         /* Parse command line */
519         while ((opt = getopt_long(argc, argv, "p:P", longopts,
520                                                 &longindex)) != EOF) {
521                 switch (opt) {
522                 case 'p':
523                         ports_mask = parse_unsigned(optarg);
524                         break;
525                 case 'P':
526                         promiscuous_on = 1;
527                         break;
528                 case 0:
529                         if (!strncmp(longopts[longindex].name,
530                                      CMDLINE_OPT_CONFIG,
531                                      sizeof(CMDLINE_OPT_CONFIG))) {
532                                 ret = parse_config(optarg);
533                                 if (ret) {
534                                         printf("Invalid config\n");
535                                         print_usage(prgname);
536                                         return -1;
537                                 }
538                         }
539                         break;
540                 default:
541                         print_usage(prgname);
542                         rte_exit(EXIT_FAILURE, "Invalid option specified\n");
543                 }
544         }
545
546         /* Check that options were parsed ok */
547         if (validate_parameters(ports_mask) < 0) {
548                 print_usage(prgname);
549                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
550         }
551
552         return ret;
553 }
554
555 /* Initialize KNI subsystem */
556 static void
557 init_kni(void)
558 {
559         unsigned int num_of_kni_ports = 0, i;
560         struct kni_port_params **params = kni_port_params_array;
561
562         /* Calculate the maximum number of KNI interfaces that will be used */
563         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
564                 if (kni_port_params_array[i]) {
565                         num_of_kni_ports += (params[i]->nb_lcore_k ?
566                                 params[i]->nb_lcore_k : 1);
567                 }
568         }
569
570         /* Invoke rte KNI init to preallocate the ports */
571         rte_kni_init(num_of_kni_ports);
572 }
573
574 /* Initialise a single port on an Ethernet device */
575 static void
576 init_port(uint16_t port)
577 {
578         int ret;
579         uint16_t nb_rxd = NB_RXD;
580         uint16_t nb_txd = NB_TXD;
581
582         /* Initialise device and RX/TX queues */
583         RTE_LOG(INFO, APP, "Initialising port %u ...\n", (unsigned)port);
584         fflush(stdout);
585         ret = rte_eth_dev_configure(port, 1, 1, &port_conf);
586         if (ret < 0)
587                 rte_exit(EXIT_FAILURE, "Could not configure port%u (%d)\n",
588                             (unsigned)port, ret);
589
590         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
591         if (ret < 0)
592                 rte_exit(EXIT_FAILURE, "Could not adjust number of descriptors "
593                                 "for port%u (%d)\n", (unsigned)port, ret);
594
595         ret = rte_eth_rx_queue_setup(port, 0, nb_rxd,
596                 rte_eth_dev_socket_id(port), NULL, pktmbuf_pool);
597         if (ret < 0)
598                 rte_exit(EXIT_FAILURE, "Could not setup up RX queue for "
599                                 "port%u (%d)\n", (unsigned)port, ret);
600
601         ret = rte_eth_tx_queue_setup(port, 0, nb_txd,
602                 rte_eth_dev_socket_id(port), NULL);
603         if (ret < 0)
604                 rte_exit(EXIT_FAILURE, "Could not setup up TX queue for "
605                                 "port%u (%d)\n", (unsigned)port, ret);
606
607         ret = rte_eth_dev_start(port);
608         if (ret < 0)
609                 rte_exit(EXIT_FAILURE, "Could not start port%u (%d)\n",
610                                                 (unsigned)port, ret);
611
612         if (promiscuous_on)
613                 rte_eth_promiscuous_enable(port);
614 }
615
616 /* Check the link status of all ports in up to 9s, and print them finally */
617 static void
618 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
619 {
620 #define CHECK_INTERVAL 100 /* 100ms */
621 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
622         uint16_t portid;
623         uint8_t count, all_ports_up, print_flag = 0;
624         struct rte_eth_link link;
625
626         printf("\nChecking link status\n");
627         fflush(stdout);
628         for (count = 0; count <= MAX_CHECK_TIME; count++) {
629                 all_ports_up = 1;
630                 for (portid = 0; portid < port_num; portid++) {
631                         if ((port_mask & (1 << portid)) == 0)
632                                 continue;
633                         memset(&link, 0, sizeof(link));
634                         rte_eth_link_get_nowait(portid, &link);
635                         /* print link status if flag set */
636                         if (print_flag == 1) {
637                                 if (link.link_status)
638                                         printf(
639                                         "Port%d Link Up - speed %uMbps - %s\n",
640                                                 portid, link.link_speed,
641                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
642                                         ("full-duplex") : ("half-duplex\n"));
643                                 else
644                                         printf("Port %d Link Down\n", portid);
645                                 continue;
646                         }
647                         /* clear all_ports_up flag if any link down */
648                         if (link.link_status == ETH_LINK_DOWN) {
649                                 all_ports_up = 0;
650                                 break;
651                         }
652                 }
653                 /* after finally printing all link status, get out */
654                 if (print_flag == 1)
655                         break;
656
657                 if (all_ports_up == 0) {
658                         printf(".");
659                         fflush(stdout);
660                         rte_delay_ms(CHECK_INTERVAL);
661                 }
662
663                 /* set the print_flag if all ports up or timeout */
664                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
665                         print_flag = 1;
666                         printf("done\n");
667                 }
668         }
669 }
670
671 /* Callback for request of changing MTU */
672 static int
673 kni_change_mtu(uint16_t port_id, unsigned int new_mtu)
674 {
675         int ret;
676         struct rte_eth_conf conf;
677
678         if (port_id >= rte_eth_dev_count()) {
679                 RTE_LOG(ERR, APP, "Invalid port id %d\n", port_id);
680                 return -EINVAL;
681         }
682
683         RTE_LOG(INFO, APP, "Change MTU of port %d to %u\n", port_id, new_mtu);
684
685         /* Stop specific port */
686         rte_eth_dev_stop(port_id);
687
688         memcpy(&conf, &port_conf, sizeof(conf));
689         /* Set new MTU */
690         if (new_mtu > ETHER_MAX_LEN)
691                 conf.rxmode.jumbo_frame = 1;
692         else
693                 conf.rxmode.jumbo_frame = 0;
694
695         /* mtu + length of header + length of FCS = max pkt length */
696         conf.rxmode.max_rx_pkt_len = new_mtu + KNI_ENET_HEADER_SIZE +
697                                                         KNI_ENET_FCS_SIZE;
698         ret = rte_eth_dev_configure(port_id, 1, 1, &conf);
699         if (ret < 0) {
700                 RTE_LOG(ERR, APP, "Fail to reconfigure port %d\n", port_id);
701                 return ret;
702         }
703
704         /* Restart specific port */
705         ret = rte_eth_dev_start(port_id);
706         if (ret < 0) {
707                 RTE_LOG(ERR, APP, "Fail to restart port %d\n", port_id);
708                 return ret;
709         }
710
711         return 0;
712 }
713
714 /* Callback for request of configuring network interface up/down */
715 static int
716 kni_config_network_interface(uint16_t port_id, uint8_t if_up)
717 {
718         int ret = 0;
719
720         if (port_id >= rte_eth_dev_count() || port_id >= RTE_MAX_ETHPORTS) {
721                 RTE_LOG(ERR, APP, "Invalid port id %d\n", port_id);
722                 return -EINVAL;
723         }
724
725         RTE_LOG(INFO, APP, "Configure network interface of %d %s\n",
726                                         port_id, if_up ? "up" : "down");
727
728         if (if_up != 0) { /* Configure network interface up */
729                 rte_eth_dev_stop(port_id);
730                 ret = rte_eth_dev_start(port_id);
731         } else /* Configure network interface down */
732                 rte_eth_dev_stop(port_id);
733
734         if (ret < 0)
735                 RTE_LOG(ERR, APP, "Failed to start port %d\n", port_id);
736
737         return ret;
738 }
739
740 static int
741 kni_alloc(uint16_t port_id)
742 {
743         uint8_t i;
744         struct rte_kni *kni;
745         struct rte_kni_conf conf;
746         struct kni_port_params **params = kni_port_params_array;
747
748         if (port_id >= RTE_MAX_ETHPORTS || !params[port_id])
749                 return -1;
750
751         params[port_id]->nb_kni = params[port_id]->nb_lcore_k ?
752                                 params[port_id]->nb_lcore_k : 1;
753
754         for (i = 0; i < params[port_id]->nb_kni; i++) {
755                 /* Clear conf at first */
756                 memset(&conf, 0, sizeof(conf));
757                 if (params[port_id]->nb_lcore_k) {
758                         snprintf(conf.name, RTE_KNI_NAMESIZE,
759                                         "vEth%u_%u", port_id, i);
760                         conf.core_id = params[port_id]->lcore_k[i];
761                         conf.force_bind = 1;
762                 } else
763                         snprintf(conf.name, RTE_KNI_NAMESIZE,
764                                                 "vEth%u", port_id);
765                 conf.group_id = port_id;
766                 conf.mbuf_size = MAX_PACKET_SZ;
767                 /*
768                  * The first KNI device associated to a port
769                  * is the master, for multiple kernel thread
770                  * environment.
771                  */
772                 if (i == 0) {
773                         struct rte_kni_ops ops;
774                         struct rte_eth_dev_info dev_info;
775
776                         memset(&dev_info, 0, sizeof(dev_info));
777                         rte_eth_dev_info_get(port_id, &dev_info);
778
779                         if (dev_info.pci_dev) {
780                                 conf.addr = dev_info.pci_dev->addr;
781                                 conf.id = dev_info.pci_dev->id;
782                         }
783
784                         memset(&ops, 0, sizeof(ops));
785                         ops.port_id = port_id;
786                         ops.change_mtu = kni_change_mtu;
787                         ops.config_network_if = kni_config_network_interface;
788
789                         kni = rte_kni_alloc(pktmbuf_pool, &conf, &ops);
790                 } else
791                         kni = rte_kni_alloc(pktmbuf_pool, &conf, NULL);
792
793                 if (!kni)
794                         rte_exit(EXIT_FAILURE, "Fail to create kni for "
795                                                 "port: %d\n", port_id);
796                 params[port_id]->kni[i] = kni;
797         }
798
799         return 0;
800 }
801
802 static int
803 kni_free_kni(uint16_t port_id)
804 {
805         uint8_t i;
806         struct kni_port_params **p = kni_port_params_array;
807
808         if (port_id >= RTE_MAX_ETHPORTS || !p[port_id])
809                 return -1;
810
811         for (i = 0; i < p[port_id]->nb_kni; i++) {
812                 if (rte_kni_release(p[port_id]->kni[i]))
813                         printf("Fail to release kni\n");
814                 p[port_id]->kni[i] = NULL;
815         }
816         rte_eth_dev_stop(port_id);
817
818         return 0;
819 }
820
821 /* Initialise ports/queues etc. and start main loop on each core */
822 int
823 main(int argc, char** argv)
824 {
825         int ret;
826         uint16_t nb_sys_ports, port;
827         unsigned i;
828
829         /* Associate signal_hanlder function with USR signals */
830         signal(SIGUSR1, signal_handler);
831         signal(SIGUSR2, signal_handler);
832         signal(SIGRTMIN, signal_handler);
833         signal(SIGINT, signal_handler);
834
835         /* Initialise EAL */
836         ret = rte_eal_init(argc, argv);
837         if (ret < 0)
838                 rte_exit(EXIT_FAILURE, "Could not initialise EAL (%d)\n", ret);
839         argc -= ret;
840         argv += ret;
841
842         /* Parse application arguments (after the EAL ones) */
843         ret = parse_args(argc, argv);
844         if (ret < 0)
845                 rte_exit(EXIT_FAILURE, "Could not parse input parameters\n");
846
847         /* Create the mbuf pool */
848         pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF,
849                 MEMPOOL_CACHE_SZ, 0, MBUF_DATA_SZ, rte_socket_id());
850         if (pktmbuf_pool == NULL) {
851                 rte_exit(EXIT_FAILURE, "Could not initialise mbuf pool\n");
852                 return -1;
853         }
854
855         /* Get number of ports found in scan */
856         nb_sys_ports = rte_eth_dev_count();
857         if (nb_sys_ports == 0)
858                 rte_exit(EXIT_FAILURE, "No supported Ethernet device found\n");
859
860         /* Check if the configured port ID is valid */
861         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
862                 if (kni_port_params_array[i] && i >= nb_sys_ports)
863                         rte_exit(EXIT_FAILURE, "Configured invalid "
864                                                 "port ID %u\n", i);
865
866         /* Initialize KNI subsystem */
867         init_kni();
868
869         /* Initialise each port */
870         for (port = 0; port < nb_sys_ports; port++) {
871                 /* Skip ports that are not enabled */
872                 if (!(ports_mask & (1 << port)))
873                         continue;
874                 init_port(port);
875
876                 if (port >= RTE_MAX_ETHPORTS)
877                         rte_exit(EXIT_FAILURE, "Can not use more than "
878                                 "%d ports for kni\n", RTE_MAX_ETHPORTS);
879
880                 kni_alloc(port);
881         }
882         check_all_ports_link_status(nb_sys_ports, ports_mask);
883
884         /* Launch per-lcore function on every lcore */
885         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
886         RTE_LCORE_FOREACH_SLAVE(i) {
887                 if (rte_eal_wait_lcore(i) < 0)
888                         return -1;
889         }
890
891         /* Release resources */
892         for (port = 0; port < nb_sys_ports; port++) {
893                 if (!(ports_mask & (1 << port)))
894                         continue;
895                 kni_free_kni(port);
896         }
897         for (i = 0; i < RTE_MAX_ETHPORTS; i++)
898                 if (kni_port_params_array[i]) {
899                         rte_free(kni_port_params_array[i]);
900                         kni_port_params_array[i] = NULL;
901                 }
902
903         return 0;
904 }