-
Notifications
You must be signed in to change notification settings - Fork 2
/
sv3.cc
206 lines (169 loc) · 4.9 KB
/
sv3.cc
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (C) 2013, Julian Stecklina <[email protected]>
// Economic rights: Technische Universitaet Dresden (Germany)
// This file is part of sv3.
// sv3 is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
// sv3 is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License version 2 for more details.
#include <cstdio>
#include <algorithm>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <signal.h>
#include <getopt.h>
#include <string>
#include <sstream>
#include <typeinfo>
#include <exceptions.hh>
#include <switch.hh>
#include <listener.hh>
#include <config.hh>
#include <tracing.hh>
#include <upstream.hh>
/// Tracing
#ifdef TRACING
void open_trace_file(std::string file)
{
if (not file.length()) {
std::stringstream ss;
ss << "trace-" << getpid();
file = ss.str();
}
printf("Trace output is in '%s'.\n\n", file.c_str());
int fd = open(file.c_str(), O_RDWR | O_TRUNC | O_CREAT, 0644);
if (fd < 0 or ftruncate(fd, Switch::TRACE_SIZE) != 0) {
perror("open");
exit(EXIT_FAILURE);
}
void *r = mmap(nullptr, Switch::TRACE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, 0);
if (r == MAP_FAILED) {
perror("mmap");
exit(EXIT_FAILURE);
}
Switch::trace_buffer = reinterpret_cast<char *>(r);
close(fd);
}
#endif
/// Signal handling
static Switch::Switch *signal_switch;
static bool signal_caught;
static void sigint_handler(int)
{
// Try graceful shutdown first. On second signal exit directly.
if (not signal_caught) {
signal_switch->shutdown();
signal_caught = true;
} else {
_Exit(EXIT_FAILURE);
}
}
/// Main function
#if defined(__GNUC__) && !defined(__clang__)
# define COMPILER "gcc "
# define COMPILER_VERSION __VERSION__
#elif defined(__clang__)
# define COMPILER "clang "
# define COMPILER_VERSION __clang_version__
#else
# define COMPILER "an unknown compiler"
# define COMPILER_VERSION ""
#endif
int main(int argc, char **argv)
{
printf(" ____\n"
" ____ __|_ / Userspace\n"
" (_-< |/ //_ < Software\n"
"/___/___/____/ Switch\n\n"
"Built from git revision "
#include "version.inc"
" with " COMPILER COMPILER_VERSION ".\n"
#ifdef SV3_BENCHMARK_OK
"Built with optimal compiler flags. Let the benchmarking commence!\n"
#else
"Suboptimal compilation flags. Do not use for benchmarking!\n"
#endif
"Blame Julian Stecklina <[email protected]>.\n\n");
int force = false;
static struct option long_options [] = {
{ "force", no_argument, &force, 1 },
{ "poll-us", required_argument, 0, 'p' },
{ "batch-size", required_argument, 0, 'b' },
#ifdef TRACING
{ "trace-file", required_argument, 0, 't' },
#endif
{ "upstream-port", required_argument, 0, 'u' },
{ 0, 0, 0, 0 },
};
int poll_us = 0;
int batch_size = 16;
#ifdef TRACING
std::string trace_file;
#endif
std::vector<std::string> upstream_port;
int opt;
int opt_idx;
while ((opt = getopt_long(argc, argv, "f", long_options, &opt_idx)) != -1) {
switch (opt) {
case 0:
continue;
case 'f':
force = true;
break;
case 'p':
poll_us = atoi(optarg);
break;
case 'b':
batch_size = atoi(optarg);
break;
#ifdef TRACING
case 't':
trace_file = optarg;
break;
#endif
case 'u':
upstream_port = string_split(optarg, ',');
if (upstream_port.size() >= 1)
break;
// FALLTHROUGH
case '?':
default: /* '?' */
fprintf(stderr,
"Usage: %s [-f|--force] [--poll-us us] [--batch-size n]\n"
" [--trace-file file]\n"
" [--upstream-port <type>,<arg1>,<arg2>,...]\n",
argv[0]);
return EXIT_FAILURE;
}
}
#ifdef TRACING
open_trace_file(trace_file);
#endif
try {
Switch::Switch sv3(poll_us, batch_size);
Switch::Listener listener(sv3, force);
if (upstream_port.size() != 0)
Switch::create_upstream_port(sv3, upstream_port);
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_handler = sigint_handler;
sa.sa_flags = 0;
signal_switch = &sv3;
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGTERM, &sa, nullptr);
sv3.loop();
return EXIT_SUCCESS;
} catch (Switch::Exception &e) {
fprintf(stderr, "\n%s:\n%s\n",
demangle(typeid(e).name()).c_str(), e.reason().c_str());
} catch (std::system_error &e) {
fprintf(stderr, "\nFatal system error: '%s'\n", e.what());
}
return EXIT_FAILURE;
}
// EOF