Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame^] | 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. |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 14 | |
| 15 | #include "cable.hpp" |
| 16 | |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 17 | #include "commands.hpp" |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 18 | #include "errors.hpp" |
| 19 | #include "handler.hpp" |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 20 | |
| 21 | #include <cstdint> |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 22 | #include <cstring> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 23 | #include <string> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 24 | |
| 25 | namespace google |
| 26 | { |
| 27 | namespace ipmi |
| 28 | { |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 29 | |
| 30 | struct CableRequest |
| 31 | { |
| 32 | uint8_t subcommand; |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 33 | uint8_t ifNameLength; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 34 | } __attribute__((packed)); |
| 35 | |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 36 | ipmi_ret_t cableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen, |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 37 | const HandlerInterface* handler) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 38 | { |
| 39 | // There is an IPMI LAN channel statistics command which could be used for |
| 40 | // this type of check, however, we're not able to wait for the OpenBMC |
| 41 | // implementation to stabilize related to the network management. |
| 42 | // |
| 43 | // There is a link status file, but it is "unknown" to start with... |
| 44 | // The path we're checking: /sys/class/net/eth1/statistics/rx_packets |
| 45 | |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 46 | // This command is expecting: [0x00][len][ifName] |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 47 | if ((*dataLen) < sizeof(struct CableRequest) + sizeof(uint8_t)) |
| 48 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 49 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 50 | static_cast<uint32_t>(*dataLen)); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 51 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | const auto request = |
| 55 | reinterpret_cast<const struct CableRequest*>(&reqBuf[0]); |
| 56 | |
| 57 | // Sanity check the object contents. |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 58 | if (request->ifNameLength == 0) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 59 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 60 | std::fprintf(stderr, "Invalid string length: %d\n", |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 61 | request->ifNameLength); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 62 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Verify the request buffer contains the object and the string. |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 66 | if ((*dataLen) < (sizeof(struct CableRequest) + request->ifNameLength)) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 67 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 68 | std::fprintf(stderr, "*dataLen too small: %u\n", |
| 69 | static_cast<uint32_t>(*dataLen)); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 70 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Maximum length one can specify, plus null terminator. |
| 74 | char nameBuf[256] = {}; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 75 | // Copy the string out of the request buffer. |
William A. Kennington III | 5d78973 | 2021-06-24 03:39:31 -0700 | [diff] [blame] | 76 | std::memcpy(&nameBuf[0], request + 1, request->ifNameLength); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 77 | std::string name = nameBuf; |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 78 | int64_t count; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 79 | |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 80 | try |
| 81 | { |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 82 | count = handler->getRxPackets(name); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 83 | } |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 84 | catch (const IpmiException& e) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 85 | { |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 86 | return e.getIpmiError(); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | struct CableReply reply; |
| 90 | reply.subcommand = SysCableCheck; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 91 | |
| 92 | // If we have received packets then there is a cable present. |
| 93 | reply.value = (count > 0) ? 1 : 0; |
| 94 | |
| 95 | // Return the subcommand and the result. |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 96 | std::memcpy(&replyBuf[0], &reply, sizeof(struct CableReply)); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 97 | (*dataLen) = sizeof(struct CableReply); |
| 98 | |
| 99 | return IPMI_CC_OK; |
| 100 | } |
| 101 | |
| 102 | } // namespace ipmi |
| 103 | } // namespace google |