-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_parser.c
178 lines (158 loc) · 4.92 KB
/
cmd_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Copyright (c) 2018-present VoerEir AB - All Rights Reserved.
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* Created by Ashok Kumar <[email protected]>, Dec 2018
**/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <errno.h>
#include <getopt.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <rte_ethdev.h>
#include <signal.h>
#include <stdbool.h>
#include <rte_ip.h>
#include <rte_ether.h>
#include "parser.h"
#include "ip.h"
struct configuration config;
// Default values.
/*config.iteration_no = 0;
config.extra_timer_period = 10;
config.warm_up_time_period = 2;
config.iteration_no = 1;
config.iterations = 20;
*/
static int parse_number(const char *q_arg) {
char *end = NULL;
int n;
/* parse number string */
n = strtol(q_arg, &end, 10);
if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
return -1;
return n;
}
static int is_valid_digit(const char *c) {
if (c == NULL)
return 0;
while (*c) {
if (!isdigit(*c)) {
return 0;
}
c++;
}
return 1;
}
static int get_ip_address_type(const char *ip_str) {
struct in6_addr ip6;
struct in_addr ip4;
int type = AF_MAX;
if(inet_pton(AF_INET6, ip_str, &ip6) == 1) {
type = AF_INET6;
}
else if(inet_pton(AF_INET, ip_str, &ip4) == 1) {
type = AF_INET;
}
return type;
}
static int process_ip_addr(const char *what, const char *ip_str, struct sockaddr_in *addr, struct sockaddr_in6 *addr6) {
int ip_type = get_ip_address_type(ip_str);
if (ip_type == AF_MAX) {
rte_exit(EXIT_FAILURE,"Invalid IP Address\n");
}
else {
char *ip = strndup(ip_str, strlen(ip_str));
if (ip_type == AF_INET) {
inet_pton(ip_type, ip, &addr->sin_addr);
ipv4_addr_dump(what, &addr->sin_addr);
} else {
inet_pton(ip_type, ip, &addr6->sin6_addr);
ipv6_addr_dump(what, &addr6->sin6_addr);
}
}
return ip_type;
}
/* Parse the argument given in the command line of the application */
struct configuration *parse_args(int argc, char **argv) {
int opt, retval;
char *prgname = argv[0];
int self_ip_type = AF_MAX;
int remote_ip_type = AF_MAX;
// Default values.
config.iteration_no = 1;
config.extra_timer_period = 10;
config.warm_up_time_period = 2;
config.iterations = 20;
config.rx_queues = 1;
/* initialize Configuration */
//memset(&config, 0, sizeof(config));
while ((opt = getopt(argc, argv, short_options)) != EOF) {
switch (opt) {
/* timer period */
case 't':
config.timer_period = parse_number(optarg);
if (config.timer_period < 0) {
rte_exit(EXIT_FAILURE, "Invalid execution time period provided.\n");
}
break;
case 'w':
config.warm_up_time_period = parse_number(optarg);
if (config.warm_up_time_period < 0) {
rte_exit(EXIT_FAILURE,"Invalid warmup time provided.\n");
}
break;
case 'e':
config.extra_timer_period = parse_number(optarg);
if (config.extra_timer_period < 0) {
rte_exit(EXIT_FAILURE,"Invalid extra timer provided.\n");
}
break;
case 'i':
self_ip_type = process_ip_addr("Self IP: ", optarg, &config.self_ipaddr, &config.self_ipaddr6);
printf("\n\r");
break;
case 's':
remote_ip_type = process_ip_addr("Remote IP: ", optarg, &config.remote_ipaddr, &config.remote_ipaddr6);
printf("\n\r");
break;
/* long options */
case 'n':
config.iteration_no = parse_number(optarg);
if (config.iteration_no < 0) {
rte_exit(EXIT_FAILURE,"Invalid iteration number provided.\n");
}
break;
/* long options */
case 'q':
config.rx_queues = parse_number(optarg);
if (config.rx_queues < 0) {
rte_exit(EXIT_FAILURE,"Invalid rx queues provided.\n");
}
break;
/* long options */
case 'r':
config.iterations = parse_number(optarg);
if (config.iterations < 0) {
rte_exit(EXIT_FAILURE,"Invalid iteration number provided.\n");
}
break;
default:
//usage(prgname);
break;
}
}
if (config.iteration_no > config.iterations) {
rte_exit(EXIT_FAILURE,"Current iteration number cannot be higher than iterations.\n");
}
if (self_ip_type != remote_ip_type) {
rte_exit(EXIT_FAILURE, "Only same type of IP ports are supported.\n");
}
config.ipv4 = (self_ip_type == AF_INET) ? true : false;
if (optind >= 0)
argv[optind-1] = prgname;
return &config;
}