-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcable.cpp
99 lines (83 loc) · 2.96 KB
/
cable.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
// Copyright 2021 Google LLC
//
// 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.
#include "cable.hpp"
#include "commands.hpp"
#include "errors.hpp"
#include "handler.hpp"
#include <ipmid/api-types.hpp>
#include <stdplus/print.hpp>
#include <cstdint>
#include <cstring>
#include <span>
#include <string>
#include <vector>
namespace google
{
namespace ipmi
{
struct CableRequest
{
uint8_t ifNameLength;
} __attribute__((packed));
Resp cableCheck(std::span<const uint8_t> data, const HandlerInterface* handler)
{
// There is an IPMI LAN channel statistics command which could be used for
// this type of check, however, we're not able to wait for the OpenBMC
// implementation to stabilize related to the network management.
//
// There is a link status file, but it is "unknown" to start with...
// The path we're checking: /sys/class/net/eth1/statistics/rx_packets
// This command is expecting: [0x00][len][ifName]
// data should have [len][ifName]
if (data.size() < sizeof(struct CableRequest))
{
stdplus::print(stderr, "Invalid command length: {}\n", data.size());
return ::ipmi::responseReqDataLenInvalid();
}
const auto request =
reinterpret_cast<const struct CableRequest*>(data.data());
// Sanity check the object contents.
if (request->ifNameLength == 0)
{
stdplus::print(stderr, "Invalid string length: {}\n",
request->ifNameLength);
return ::ipmi::responseReqDataLenInvalid();
}
// Verify the request buffer contains the object and the string.
if (data.size() < (sizeof(struct CableRequest) + request->ifNameLength))
{
stdplus::print(stderr, "*dataLen too small: {}\n", data.size());
return ::ipmi::responseReqDataLenInvalid();
}
// Maximum length one can specify, plus null terminator.
char nameBuf[256] = {};
// Copy the string out of the request buffer.
std::memcpy(&nameBuf[0], request + 1, request->ifNameLength);
std::string name = nameBuf;
int64_t count;
try
{
count = handler->getRxPackets(name);
}
catch (const IpmiException& e)
{
return ::ipmi::response(e.getIpmiError());
}
// If we have received packets then there is a cable present.
std::uint8_t value = (count > 0) ? 1 : 0;
return ::ipmi::responseSuccess(SysOEMCommands::SysCableCheck,
std::vector<std::uint8_t>{value});
}
} // namespace ipmi
} // namespace google