-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
159 lines (120 loc) · 4.65 KB
/
main.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
/*
* License: MIT
*
* Copyright (c) 2017-2020 James Bensley.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "main.h"
#include "threads.h"
#include "functions.c"
#include "sock_op.c"
#include "packet.c"
#include "packet_msg.c"
#include "packet_mmsg.c"
// Only inlcude these files if the required Kernel version is detected,
// otherwise the code won't compile:
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
#include "tpacket_v2.c"
#else
#include "tpacket_v2_bypass.c"
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
#include "tpacket_v3.c"
#else
#include "tpacket_v3_bypass.c"
#endif
#include "print_stats.c"
#include "threads.c"
int main(int argc, char *argv[]) {
// Check for root privileges
if (getuid() != 0) {
printf("Oops! Must be root to use this program.\n");
return EX_NOPERM;
}
// Global instance of all settings/values
struct etherate eth;
// Global pointer to eth object used by signal_handler()
eth_p = ð
// Declare sigint handler to cancel the worker and stats threads
signal (SIGINT, signal_handler);
// Set application defaults
etherate_setup(ð);
// Process CLI args
uint16_t cli_ret = cli_args(argc, argv, ð);
if (cli_ret == EXIT_FAILURE) {
etherate_cleanup(ð);
return cli_ret;
} else if (cli_ret == EX_SOFTWARE) {
etherate_cleanup(ð);
return EXIT_SUCCESS;
}
// Ensure an interface has been chosen
if (eth.sk_opt.if_index == -1) {
printf("Oops! No interface chosen.\n");
return EX_SOFTWARE;
}
if (eth.app_opt.verbose) printf("Verbose output enabled.\n");
// Put the chosen interface into promisc mode
int32_t promisc_ret = set_int_promisc(ð);
if (promisc_ret != EXIT_SUCCESS)
return promisc_ret;
printf("Frame size set to %" PRIu16 " bytes.\n", eth.frm_opt.frame_sz);
if (eth.app_opt.sk_type == SKT_PACKET_MMAP2) {
printf("Using raw socket with PACKET_MMAP and TX/RX_RING v2.\n");
} else if (eth.app_opt.sk_type == SKT_PACKET) {
printf("Using raw packet socket with send()/read().\n");
} else if (eth.app_opt.sk_type == SKT_SENDMSG) {
printf("Using raw packet socket with sendmsg()/recvmsg().\n");
} else if (eth.app_opt.sk_type == SKT_SENDMMSG) {
printf("Using raw packet socket with sendmmsg()/recvmmsg().\n");
} else if (eth.app_opt.sk_type == SKT_PACKET_MMAP3) {
printf("Using raw socket with PACKET_MMAP and TX/RX_RING v3.\n");
}
if (eth.app_opt.sk_mode == SKT_RX) {
printf("Running in Rx mode.\n");
} else if (eth.app_opt.sk_mode == SKT_TX) {
printf("Running in Tx mode.\n");
} else if (eth.app_opt.sk_mode == SKT_BIDI) {
printf("Running in bidirectional mode.\n");
}
if (eth.app_opt.verbose)
printf("Main thread pid is %" PRId32 ".\n", getpid());
// Fill the test frame buffer with random data
if (getrandom(eth.frm_opt.tx_buffer, eth.frm_opt.frame_sz, 0)
!= eth.frm_opt.frame_sz)
{
perror("Can't generate random frame data");
exit(EXIT_FAILURE);
}
// Create a copy of the program settings for each worker thread.
eth.thd_opt = calloc(sizeof(struct thd_opt), eth.app_opt.thd_nr);
thd_alloc(ð);
// Spawn the stats printing thread
if (thd_init_stats(ð) != EXIT_SUCCESS)
return EXIT_FAILURE;
// Spawn each worker thread
if (thd_spawn_workers(ð) != EXIT_SUCCESS)
return EXIT_FAILURE;
// Wait for worker and stats threads to finish, then clean up
thd_join_workers(ð);
thd_join_stats(ð);
etherate_cleanup(ð);
}