blob: 37167c375708f0b59160f94fde52b04d9d8bcbb0 [file] [log] [blame]
Patrick Venture4d49ae62018-09-17 11:35:32 -07001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "cable.hpp"
18
Patrick Venture0e9aae52020-08-13 13:07:09 -070019#include "commands.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070020#include "errors.hpp"
21#include "handler.hpp"
Patrick Venture4d49ae62018-09-17 11:35:32 -070022
23#include <cstdint>
Patrick Venturece07ee02018-09-19 18:09:32 -070024#include <cstring>
Patrick Venture4d49ae62018-09-17 11:35:32 -070025#include <string>
Patrick Venture4d49ae62018-09-17 11:35:32 -070026
27namespace google
28{
29namespace ipmi
30{
Patrick Venture4d49ae62018-09-17 11:35:32 -070031
32struct CableRequest
33{
34 uint8_t subcommand;
Patrick Venture45fad1b2019-03-18 16:52:14 -070035 uint8_t ifNameLength;
Patrick Venture4d49ae62018-09-17 11:35:32 -070036} __attribute__((packed));
37
Patrick Venture45fad1b2019-03-18 16:52:14 -070038ipmi_ret_t cableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen,
Patrick Ventured2037c62019-03-15 10:29:47 -070039 const HandlerInterface* handler)
Patrick Venture4d49ae62018-09-17 11:35:32 -070040{
41 // There is an IPMI LAN channel statistics command which could be used for
42 // this type of check, however, we're not able to wait for the OpenBMC
43 // implementation to stabilize related to the network management.
44 //
45 // There is a link status file, but it is "unknown" to start with...
46 // The path we're checking: /sys/class/net/eth1/statistics/rx_packets
47
Patrick Venture45fad1b2019-03-18 16:52:14 -070048 // This command is expecting: [0x00][len][ifName]
Patrick Venture4d49ae62018-09-17 11:35:32 -070049 if ((*dataLen) < sizeof(struct CableRequest) + sizeof(uint8_t))
50 {
Patrick Venturece07ee02018-09-19 18:09:32 -070051 std::fprintf(stderr, "Invalid command length: %u\n",
52 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080053 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070054 }
55
56 const auto request =
57 reinterpret_cast<const struct CableRequest*>(&reqBuf[0]);
58
59 // Sanity check the object contents.
Patrick Venture45fad1b2019-03-18 16:52:14 -070060 if (request->ifNameLength == 0)
Patrick Venture4d49ae62018-09-17 11:35:32 -070061 {
Patrick Venturece07ee02018-09-19 18:09:32 -070062 std::fprintf(stderr, "Invalid string length: %d\n",
Patrick Venture45fad1b2019-03-18 16:52:14 -070063 request->ifNameLength);
Patrick Venturefff98612018-11-12 09:05:54 -080064 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070065 }
66
67 // Verify the request buffer contains the object and the string.
Patrick Venture45fad1b2019-03-18 16:52:14 -070068 if ((*dataLen) < (sizeof(struct CableRequest) + request->ifNameLength))
Patrick Venture4d49ae62018-09-17 11:35:32 -070069 {
Patrick Venturece07ee02018-09-19 18:09:32 -070070 std::fprintf(stderr, "*dataLen too small: %u\n",
71 static_cast<uint32_t>(*dataLen));
Patrick Venturefff98612018-11-12 09:05:54 -080072 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Venture4d49ae62018-09-17 11:35:32 -070073 }
74
75 // Maximum length one can specify, plus null terminator.
76 char nameBuf[256] = {};
Patrick Venture4d49ae62018-09-17 11:35:32 -070077 // Copy the string out of the request buffer.
William A. Kennington III5d789732021-06-24 03:39:31 -070078 std::memcpy(&nameBuf[0], request + 1, request->ifNameLength);
Patrick Venture4d49ae62018-09-17 11:35:32 -070079 std::string name = nameBuf;
Patrick Ventured2037c62019-03-15 10:29:47 -070080 int64_t count;
Patrick Venture4d49ae62018-09-17 11:35:32 -070081
Patrick Venture4d49ae62018-09-17 11:35:32 -070082 try
83 {
Patrick Ventured2037c62019-03-15 10:29:47 -070084 count = handler->getRxPackets(name);
Patrick Venture4d49ae62018-09-17 11:35:32 -070085 }
Patrick Ventured2037c62019-03-15 10:29:47 -070086 catch (const IpmiException& e)
Patrick Venture4d49ae62018-09-17 11:35:32 -070087 {
Patrick Ventured2037c62019-03-15 10:29:47 -070088 return e.getIpmiError();
Patrick Venture4d49ae62018-09-17 11:35:32 -070089 }
90
91 struct CableReply reply;
92 reply.subcommand = SysCableCheck;
Patrick Venture4d49ae62018-09-17 11:35:32 -070093
94 // If we have received packets then there is a cable present.
95 reply.value = (count > 0) ? 1 : 0;
96
97 // Return the subcommand and the result.
Patrick Venturece07ee02018-09-19 18:09:32 -070098 std::memcpy(&replyBuf[0], &reply, sizeof(struct CableReply));
Patrick Venture4d49ae62018-09-17 11:35:32 -070099 (*dataLen) = sizeof(struct CableReply);
100
101 return IPMI_CC_OK;
102}
103
104} // namespace ipmi
105} // namespace google