1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "cable.hpp"
16
17 #include "commands.hpp"
18 #include "errors.hpp"
19 #include "handler.hpp"
20
21 #include <ipmid/api-types.hpp>
22 #include <stdplus/print.hpp>
23
24 #include <cstdint>
25 #include <cstring>
26 #include <span>
27 #include <string>
28 #include <vector>
29
30 namespace google
31 {
32 namespace ipmi
33 {
34
35 struct CableRequest
36 {
37 uint8_t ifNameLength;
38 } __attribute__((packed));
39
cableCheck(std::span<const uint8_t> data,const HandlerInterface * handler)40 Resp cableCheck(std::span<const uint8_t> data, const HandlerInterface* handler)
41 {
42 // There is an IPMI LAN channel statistics command which could be used for
43 // this type of check, however, we're not able to wait for the OpenBMC
44 // implementation to stabilize related to the network management.
45 //
46 // There is a link status file, but it is "unknown" to start with...
47 // The path we're checking: /sys/class/net/eth1/statistics/rx_packets
48
49 // This command is expecting: [0x00][len][ifName]
50 // data should have [len][ifName]
51 if (data.size() < sizeof(struct CableRequest))
52 {
53 stdplus::print(stderr, "Invalid command length: {}\n", data.size());
54 return ::ipmi::responseReqDataLenInvalid();
55 }
56
57 const auto request =
58 reinterpret_cast<const struct CableRequest*>(data.data());
59
60 // Sanity check the object contents.
61 if (request->ifNameLength == 0)
62 {
63 stdplus::print(stderr, "Invalid string length: {}\n",
64 request->ifNameLength);
65 return ::ipmi::responseReqDataLenInvalid();
66 }
67
68 // Verify the request buffer contains the object and the string.
69 if (data.size() < (sizeof(struct CableRequest) + request->ifNameLength))
70 {
71 stdplus::print(stderr, "*dataLen too small: {}\n", data.size());
72 return ::ipmi::responseReqDataLenInvalid();
73 }
74
75 // Maximum length one can specify, plus null terminator.
76 char nameBuf[256] = {};
77 // Copy the string out of the request buffer.
78 std::memcpy(&nameBuf[0], request + 1, request->ifNameLength);
79 std::string name = nameBuf;
80 int64_t count;
81
82 try
83 {
84 count = handler->getRxPackets(name);
85 }
86 catch (const IpmiException& e)
87 {
88 return ::ipmi::response(e.getIpmiError());
89 }
90
91 // If we have received packets then there is a cable present.
92 std::uint8_t value = (count > 0) ? 1 : 0;
93
94 return ::ipmi::responseSuccess(SysOEMCommands::SysCableCheck,
95 std::vector<std::uint8_t>{value});
96 }
97
98 } // namespace ipmi
99 } // namespace google
100