examples: use SPDX tag for Intel copyright files
[dpdk.git] / examples / quota_watermark / qw / args.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 <unistd.h>
8
9 #include <rte_common.h>
10 #include <rte_lcore.h>
11
12 #include "args.h"
13
14
15 unsigned int portmask = 0;
16
17
18 static void
19 usage(const char *prgname)
20 {
21         fprintf(stderr, "Usage: %s [EAL args] -- -p <portmask>\n"
22                         "-p PORTMASK: hexadecimal bitmask of NIC ports to configure\n",
23                         prgname);
24 }
25
26 static unsigned long
27 parse_portmask(const char *portmask_str)
28 {
29         return strtoul(portmask_str, NULL, 16);
30 }
31
32 static void
33 check_core_count(void)
34 {
35         if (rte_lcore_count() < 3)
36                 rte_exit(EXIT_FAILURE,
37                                 "At least 3 cores need to be passed in the coremask\n");
38 }
39
40 static void
41 check_portmask_value(unsigned int portmask)
42 {
43         unsigned int port_nb = 0;
44
45         port_nb = __builtin_popcount(portmask);
46
47         if (port_nb == 0)
48                 rte_exit(EXIT_FAILURE,
49                                 "At least 2 ports need to be passed in the portmask\n");
50
51         if (port_nb % 2 != 0)
52                 rte_exit(EXIT_FAILURE,
53                                 "An even number of ports is required in the portmask\n");
54 }
55
56 int
57 parse_qw_args(int argc, char **argv)
58 {
59         int opt;
60
61         while ((opt = getopt(argc, argv, "h:p:")) != -1) {
62                 switch (opt) {
63                 case 'h':
64                         usage(argv[0]);
65                         break;
66                 case 'p':
67                         portmask = parse_portmask(optarg);
68                         break;
69                 default:
70                         usage(argv[0]);
71                 }
72         }
73
74         check_core_count();
75         check_portmask_value(portmask);
76
77         return 0;
78 }