/
main.cpp
421 lines (371 loc) · 12.2 KB
/
main.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef ENABLE_IPMI_SNOOP
#include "ipmisnoop/ipmisnoop.hpp"
#endif
#include "lpcsnoop/snoop.hpp"
#include <endian.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/epoll.h>
#include <systemd/sd-event.h>
#include <unistd.h>
#include <sdeventplus/event.hpp>
#include <sdeventplus/source/event.hpp>
#include <sdeventplus/source/io.hpp>
#include <sdeventplus/source/signal.hpp>
#include <sdeventplus/source/time.hpp>
#include <sdeventplus/utility/sdbus.hpp>
#include <stdplus/signal.hpp>
#include <chrono>
#include <cstdint>
#include <exception>
#include <functional>
#include <iostream>
#include <optional>
#include <thread>
static size_t codeSize = 1; /* Size of each POST code in bytes */
static bool verbose = false;
static std::function<bool(uint64_t&, ssize_t)> procPostCode;
static void usage(const char* name)
{
fprintf(stderr,
"Usage: %s\n"
#ifdef ENABLE_IPMI_SNOOP
" -h, --host <host instances> Default is '0'\n"
#else
" -d, --device <DEVICE> use <DEVICE> file.\n"
" -r, --rate-limit=<N> Only process N POST codes from the "
"device per second.\n"
" -b, --bytes <SIZE> set POST code length to <SIZE> bytes. "
"Default is 1\n"
#endif
" -v, --verbose Prints verbose information while running\n\n",
name);
}
/**
* Call once for each POST code received. If the number of POST codes exceeds
* the configured rate limit, this function will disable the snoop device IO
* source until the end of the 1 second interval, then re-enable it.
*
* @return Whether the rate limit is exceeded.
*/
bool rateLimit(PostReporter& reporter, sdeventplus::source::IO& ioSource)
{
if (reporter.rateLimit == 0)
{
// Rate limiting is disabled.
return false;
}
using Clock = sdeventplus::Clock<sdeventplus::ClockId::Monotonic>;
static constexpr std::chrono::seconds rateLimitInterval(1);
static unsigned int rateLimitCount = 0;
static Clock::time_point rateLimitEndTime;
const sdeventplus::Event& event = ioSource.get_event();
if (rateLimitCount == 0)
{
// Initialize the end time when we start a new interval
rateLimitEndTime = Clock(event).now() + rateLimitInterval;
}
if (++rateLimitCount < reporter.rateLimit)
{
return false;
}
rateLimitCount = 0;
if (rateLimitEndTime < Clock(event).now())
{
return false;
}
if (verbose)
{
fprintf(stderr, "Hit POST code rate limit - disabling temporarily\n");
}
ioSource.set_enabled(sdeventplus::source::Enabled::Off);
sdeventplus::source::Time<sdeventplus::ClockId::Monotonic>(
event, rateLimitEndTime, std::chrono::milliseconds(100),
[&ioSource](auto&, auto) {
if (verbose)
{
fprintf(stderr, "Reenabling POST code handler\n");
}
ioSource.set_enabled(sdeventplus::source::Enabled::On);
}).set_floating(true);
return true;
}
/*
* Split input code into multiple 2 bytes PCC code, If the PCC code prefix
* matches the check code, store each PCC code in aspeedPCCBuffer, or clear
* aspeedPCCBuffer if the prefix does not match.
*
* Each PCC code contains one byte of port number (MSB) and another byte of
* partial postcode (LSB). To get a complete postcode, the PCC code should
* followed the sequence of 0x40AA, 0x41BB, 0x42CC & 0x43DD. When
* aspeedPCCBuffer contains enough PCC codes, the postcode will be assigned as
* 0xDDCCBBAA.
*/
bool aspeedPCC(uint64_t& code, ssize_t readb)
{
// Size of data coming from the PCC hardware
constexpr size_t pccSize = sizeof(uint16_t);
// Required PCC count of a full postcode, if codeSize is 8 bytes, it means
// it require 4 PCC codes in correct sequence to get a complete postcode.
const size_t fullPostPCCCount = codeSize / pccSize;
// A PCC buffer for storing PCC code in sequence.
static std::vector<uint16_t> aspeedPCCBuffer;
constexpr uint16_t firstPCCPortNumber = 0x4000;
constexpr uint16_t pccPortNumberMask = 0xFF00;
constexpr uint16_t pccPostCodeMask = 0x00FF;
constexpr uint8_t byteShift = 8;
uint16_t* codePtr = reinterpret_cast<uint16_t*>(&code);
for (size_t i = 0; i < (readb / pccSize); i++)
{
uint16_t checkCode = firstPCCPortNumber +
((aspeedPCCBuffer.size() % fullPostPCCCount)
<< byteShift);
if (checkCode == (codePtr[i] & pccPortNumberMask))
{
aspeedPCCBuffer.emplace_back(codePtr[i]);
}
else
{
aspeedPCCBuffer.clear();
// keep the PCC code if codePtr[i] matches with 0x40XX as first PCC
// code in buffer.
if ((codePtr[i] & pccPortNumberMask) == firstPCCPortNumber)
{
aspeedPCCBuffer.emplace_back(codePtr[i]);
}
}
}
if (aspeedPCCBuffer.size() < fullPostPCCCount)
{
// not receive full postcode yet.
return false;
}
// Remove the prefix bytes and combine the partial postcodes together.
code = 0;
for (size_t i = 0; i < fullPostPCCCount; i++)
{
code |= static_cast<uint64_t>(aspeedPCCBuffer[i] & pccPostCodeMask)
<< (byteShift * i);
}
aspeedPCCBuffer.erase(aspeedPCCBuffer.begin(),
aspeedPCCBuffer.begin() + fullPostPCCCount);
return true;
}
/*
* Callback handling IO event from the POST code fd. i.e. there is new
* POST code available to read.
*/
void PostCodeEventHandler(PostReporter* reporter, sdeventplus::source::IO& s,
int postFd, uint32_t)
{
uint64_t code = 0;
ssize_t readb;
while ((readb = read(postFd, &code, codeSize)) > 0)
{
if (procPostCode && procPostCode(code, readb) == false)
{
return;
}
code = le64toh(code);
if (verbose)
{
fprintf(stderr, "Code: 0x%" PRIx64 "\n", code);
}
// HACK: Always send property changed signal even for the same code
// since we are single threaded, external users will never see the
// first value.
reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true);
reporter->value(std::make_tuple(code, secondary_post_code_t{}));
// read depends on old data being cleared since it doens't always read
// the full code size
code = 0;
if (rateLimit(*reporter, s))
{
return;
}
}
if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
{
return;
}
/* Read failure. */
if (readb == 0)
{
fprintf(stderr, "Unexpected EOF reading postcode\n");
}
else
{
fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
}
s.get_event().exit(1);
}
/*
* TODO(venture): this only listens one of the possible snoop ports, but
* doesn't share the namespace.
*
* This polls() the lpc snoop character device and it owns the dbus object
* whose value is the latest port 80h value.
*/
int main(int argc, char* argv[])
{
int postFd = -1;
unsigned int rateLimit = 0;
int opt;
std::vector<std::string> host;
// clang-format off
static const struct option long_options[] = {
#ifdef ENABLE_IPMI_SNOOP
{"host", optional_argument, NULL, 'h'},
#else
{"device", optional_argument, NULL, 'd'},
{"rate-limit", optional_argument, NULL, 'r'},
{"bytes", required_argument, NULL, 'b'},
#endif
{"verbose", no_argument, NULL, 'v'},
{0, 0, 0, 0}
};
// clang-format on
constexpr const char* optstring =
#ifdef ENABLE_IPMI_SNOOP
"h:"
#else
"d:r:b:"
#endif
"v";
while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1)
{
switch (opt)
{
case 0:
break;
case 'h':
{
std::string_view instances = optarg;
size_t pos = 0;
while ((pos = instances.find(" ")) != std::string::npos)
{
host.emplace_back(instances.substr(0, pos));
instances.remove_prefix(pos + 1);
}
host.emplace_back(instances);
break;
}
case 'b':
{
codeSize = atoi(optarg);
if (codeSize < 1 || codeSize > 8)
{
fprintf(stderr,
"Invalid POST code size '%s'. Must be "
"an integer from 1 to 8.\n",
optarg);
exit(EXIT_FAILURE);
}
break;
}
case 'd':
if (std::string(optarg).starts_with("/dev/aspeed-lpc-pcc"))
{
procPostCode = aspeedPCC;
}
postFd = open(optarg, O_NONBLOCK);
if (postFd < 0)
{
fprintf(stderr, "Unable to open: %s\n", optarg);
return -1;
}
break;
case 'r':
{
int argVal = -1;
try
{
argVal = std::stoi(optarg);
}
catch (...)
{}
if (argVal < 1)
{
fprintf(stderr, "Invalid rate limit '%s'. Must be >= 1.\n",
optarg);
return EXIT_FAILURE;
}
rateLimit = static_cast<unsigned int>(argVal);
fprintf(stderr, "Rate limiting to %d POST codes per second.\n",
argVal);
break;
}
case 'v':
verbose = true;
break;
default:
usage(argv[0]);
return EXIT_FAILURE;
}
}
auto bus = sdbusplus::bus::new_default();
#ifdef ENABLE_IPMI_SNOOP
std::cout << "Verbose = " << verbose << std::endl;
int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host);
if (ret < 0)
{
fprintf(stderr, "Error in postCodeIpmiHandler\n");
return ret;
}
return 0;
#endif
bool deferSignals = true;
// Add systemd object manager.
sdbusplus::server::manager_t snoopdManager(bus, snoopObject);
PostReporter reporter(bus, snoopObject, deferSignals);
reporter.emit_object_added();
bus.request_name(snoopDbus);
// Create sdevent and add IO source
try
{
sdeventplus::Event event = sdeventplus::Event::get_default();
std::optional<sdeventplus::source::IO> reporterSource;
if (postFd > 0)
{
reporter.rateLimit = rateLimit;
reporterSource.emplace(
event, postFd, EPOLLIN,
std::bind_front(PostCodeEventHandler, &reporter));
}
// Enable bus to handle incoming IO and bus events
auto intCb = [](sdeventplus::source::Signal& source,
const struct signalfd_siginfo*) {
source.get_event().exit(0);
};
stdplus::signal::block(SIGINT);
sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true);
stdplus::signal::block(SIGTERM);
sdeventplus::source::Signal(event, SIGTERM, std::move(intCb))
.set_floating(true);
return sdeventplus::utility::loopWithBus(event, bus);
}
catch (const std::exception& e)
{
fprintf(stderr, "%s\n", e.what());
}
if (postFd > -1)
{
close(postFd);
}
return 0;
}