examples/vm_power_mgr: add port initialisation
[dpdk.git] / examples / vm_power_manager / 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 <string.h>
36 #include <stdint.h>
37 #include <sys/epoll.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <signal.h>
42 #include <errno.h>
43
44 #include <sys/queue.h>
45
46 #include <rte_common.h>
47 #include <rte_eal.h>
48 #include <rte_launch.h>
49 #include <rte_log.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_ethdev.h>
53 #include <getopt.h>
54 #include <rte_cycles.h>
55 #include <rte_debug.h>
56
57 #include "channel_manager.h"
58 #include "channel_monitor.h"
59 #include "power_manager.h"
60 #include "vm_power_cli.h"
61
62 #define RX_RING_SIZE 512
63 #define TX_RING_SIZE 512
64
65 #define NUM_MBUFS 8191
66 #define MBUF_CACHE_SIZE 250
67 #define BURST_SIZE 32
68
69 static uint32_t enabled_port_mask;
70 static volatile bool force_quit;
71
72 /****************/
73 static const struct rte_eth_conf port_conf_default = {
74         .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN }
75 };
76
77 static inline int
78 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
79 {
80         struct rte_eth_conf port_conf = port_conf_default;
81         const uint16_t rx_rings = 1, tx_rings = 1;
82         int retval;
83         uint16_t q;
84
85         if (port >= rte_eth_dev_count())
86                 return -1;
87
88         /* Configure the Ethernet device. */
89         retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
90         if (retval != 0)
91                 return retval;
92
93         /* Allocate and set up 1 RX queue per Ethernet port. */
94         for (q = 0; q < rx_rings; q++) {
95                 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
96                                 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
97                 if (retval < 0)
98                         return retval;
99         }
100
101         /* Allocate and set up 1 TX queue per Ethernet port. */
102         for (q = 0; q < tx_rings; q++) {
103                 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
104                                 rte_eth_dev_socket_id(port), NULL);
105                 if (retval < 0)
106                         return retval;
107         }
108
109         /* Start the Ethernet port. */
110         retval = rte_eth_dev_start(port);
111         if (retval < 0)
112                 return retval;
113
114         /* Display the port MAC address. */
115         struct ether_addr addr;
116         rte_eth_macaddr_get(port, &addr);
117         printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
118                            " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
119                         (unsigned int)port,
120                         addr.addr_bytes[0], addr.addr_bytes[1],
121                         addr.addr_bytes[2], addr.addr_bytes[3],
122                         addr.addr_bytes[4], addr.addr_bytes[5]);
123
124         /* Enable RX in promiscuous mode for the Ethernet device. */
125         rte_eth_promiscuous_enable(port);
126
127
128         return 0;
129 }
130
131 static int
132 parse_portmask(const char *portmask)
133 {
134         char *end = NULL;
135         unsigned long pm;
136
137         /* parse hexadecimal string */
138         pm = strtoul(portmask, &end, 16);
139         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
140                 return -1;
141
142         if (pm == 0)
143                 return -1;
144
145         return pm;
146 }
147 /* Parse the argument given in the command line of the application */
148 static int
149 parse_args(int argc, char **argv)
150 {
151         int opt, ret;
152         char **argvopt;
153         int option_index;
154         char *prgname = argv[0];
155         static struct option lgopts[] = {
156                 { "mac-updating", no_argument, 0, 1},
157                 { "no-mac-updating", no_argument, 0, 0},
158                 {NULL, 0, 0, 0}
159         };
160         argvopt = argv;
161
162         while ((opt = getopt_long(argc, argvopt, "p:q:T:",
163                                   lgopts, &option_index)) != EOF) {
164
165                 switch (opt) {
166                 /* portmask */
167                 case 'p':
168                         enabled_port_mask = parse_portmask(optarg);
169                         if (enabled_port_mask == 0) {
170                                 printf("invalid portmask\n");
171                                 return -1;
172                         }
173                         break;
174                 /* long options */
175                 case 0:
176                         break;
177
178                 default:
179                         return -1;
180                 }
181         }
182
183         if (optind >= 0)
184                 argv[optind-1] = prgname;
185
186         ret = optind-1;
187         optind = 0; /* reset getopt lib */
188         return ret;
189 }
190
191 static void
192 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
193 {
194 #define CHECK_INTERVAL 100 /* 100ms */
195 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
196         uint16_t portid, count, all_ports_up, print_flag = 0;
197         struct rte_eth_link link;
198
199         printf("\nChecking link status");
200         fflush(stdout);
201         for (count = 0; count <= MAX_CHECK_TIME; count++) {
202                 if (force_quit)
203                         return;
204                 all_ports_up = 1;
205                 for (portid = 0; portid < port_num; portid++) {
206                         if (force_quit)
207                                 return;
208                         if ((port_mask & (1 << portid)) == 0)
209                                 continue;
210                         memset(&link, 0, sizeof(link));
211                         rte_eth_link_get_nowait(portid, &link);
212                         /* print link status if flag set */
213                         if (print_flag == 1) {
214                                 if (link.link_status)
215                                         printf("Port %d Link Up - speed %u "
216                                                 "Mbps - %s\n", (uint16_t)portid,
217                                                 (unsigned int)link.link_speed,
218                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
219                                         ("full-duplex") : ("half-duplex\n"));
220                                 else
221                                         printf("Port %d Link Down\n",
222                                                 (uint16_t)portid);
223                                 continue;
224                         }
225                        /* clear all_ports_up flag if any link down */
226                         if (link.link_status == ETH_LINK_DOWN) {
227                                 all_ports_up = 0;
228                                 break;
229                         }
230                 }
231                 /* after finally printing all link status, get out */
232                 if (print_flag == 1)
233                         break;
234
235                 if (all_ports_up == 0) {
236                         printf(".");
237                         fflush(stdout);
238                         rte_delay_ms(CHECK_INTERVAL);
239                 }
240
241                 /* set the print_flag if all ports up or timeout */
242                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
243                         print_flag = 1;
244                         printf("done\n");
245                 }
246         }
247 }
248 static int
249 run_monitor(__attribute__((unused)) void *arg)
250 {
251         if (channel_monitor_init() < 0) {
252                 printf("Unable to initialize channel monitor\n");
253                 return -1;
254         }
255         run_channel_monitor();
256         return 0;
257 }
258
259 static void
260 sig_handler(int signo)
261 {
262         printf("Received signal %d, exiting...\n", signo);
263         channel_monitor_exit();
264         channel_manager_exit();
265         power_manager_exit();
266
267 }
268
269 int
270 main(int argc, char **argv)
271 {
272         int ret;
273         unsigned lcore_id;
274         unsigned int nb_ports;
275         struct rte_mempool *mbuf_pool;
276         uint16_t portid;
277
278
279         ret = rte_eal_init(argc, argv);
280         if (ret < 0)
281                 rte_panic("Cannot init EAL\n");
282
283         signal(SIGINT, sig_handler);
284         signal(SIGTERM, sig_handler);
285
286         argc -= ret;
287         argv += ret;
288
289         /* parse application arguments (after the EAL ones) */
290         ret = parse_args(argc, argv);
291         if (ret < 0)
292                 rte_exit(EXIT_FAILURE, "Invalid arguments\n");
293
294         nb_ports = rte_eth_dev_count();
295
296         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
297                 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
298
299         if (mbuf_pool == NULL)
300                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
301
302         /* Initialize ports. */
303         for (portid = 0; portid < nb_ports; portid++) {
304                 if ((enabled_port_mask & (1 << portid)) == 0)
305                         continue;
306                 if (port_init(portid, mbuf_pool) != 0)
307                         rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
308                                         portid);
309         }
310
311         lcore_id = rte_get_next_lcore(-1, 1, 0);
312         if (lcore_id == RTE_MAX_LCORE) {
313                 RTE_LOG(ERR, EAL, "A minimum of two cores are required to run "
314                                 "application\n");
315                 return 0;
316         }
317
318         check_all_ports_link_status(nb_ports, enabled_port_mask);
319         rte_eal_remote_launch(run_monitor, NULL, lcore_id);
320
321         if (power_manager_init() < 0) {
322                 printf("Unable to initialize power manager\n");
323                 return -1;
324         }
325         if (channel_manager_init(CHANNEL_MGR_DEFAULT_HV_PATH) < 0) {
326                 printf("Unable to initialize channel manager\n");
327                 return -1;
328         }
329         run_cli(NULL);
330
331         rte_eal_mp_wait_lcore();
332         return 0;
333 }