Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 19 | #include "errors.hpp" |
| 20 | #include "handler.hpp" |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 21 | #include "main.hpp" |
| 22 | |
| 23 | #include <cstdint> |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 24 | #include <cstring> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 25 | #include <string> |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 26 | |
| 27 | namespace google |
| 28 | { |
| 29 | namespace ipmi |
| 30 | { |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 31 | |
| 32 | struct CableRequest |
| 33 | { |
| 34 | uint8_t subcommand; |
| 35 | uint8_t if_name_len; |
| 36 | uint8_t if_name[0]; |
| 37 | } __attribute__((packed)); |
| 38 | |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 39 | ipmi_ret_t CableCheck(const uint8_t* reqBuf, uint8_t* replyBuf, size_t* dataLen, |
| 40 | const HandlerInterface* handler) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 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][if_name] |
| 50 | if ((*dataLen) < sizeof(struct CableRequest) + sizeof(uint8_t)) |
| 51 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 52 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 53 | static_cast<uint32_t>(*dataLen)); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 54 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | const auto request = |
| 58 | reinterpret_cast<const struct CableRequest*>(&reqBuf[0]); |
| 59 | |
| 60 | // Sanity check the object contents. |
| 61 | if (request->if_name_len == 0) |
| 62 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 63 | std::fprintf(stderr, "Invalid string length: %d\n", |
| 64 | request->if_name_len); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 65 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Verify the request buffer contains the object and the string. |
| 69 | if ((*dataLen) < (sizeof(struct CableRequest) + request->if_name_len)) |
| 70 | { |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 71 | std::fprintf(stderr, "*dataLen too small: %u\n", |
| 72 | static_cast<uint32_t>(*dataLen)); |
Patrick Venture | fff9861 | 2018-11-12 09:05:54 -0800 | [diff] [blame] | 73 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Maximum length one can specify, plus null terminator. |
| 77 | char nameBuf[256] = {}; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 78 | // Copy the string out of the request buffer. |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 79 | std::memcpy(&nameBuf[0], request->if_name, request->if_name_len); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 80 | std::string name = nameBuf; |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 81 | int64_t count; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 82 | |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 83 | try |
| 84 | { |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 85 | count = handler->getRxPackets(name); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 86 | } |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 87 | catch (const IpmiException& e) |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 88 | { |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 89 | return e.getIpmiError(); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | struct CableReply reply; |
| 93 | reply.subcommand = SysCableCheck; |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 94 | |
| 95 | // If we have received packets then there is a cable present. |
| 96 | reply.value = (count > 0) ? 1 : 0; |
| 97 | |
| 98 | // Return the subcommand and the result. |
Patrick Venture | ce07ee0 | 2018-09-19 18:09:32 -0700 | [diff] [blame] | 99 | std::memcpy(&replyBuf[0], &reply, sizeof(struct CableReply)); |
Patrick Venture | 4d49ae6 | 2018-09-17 11:35:32 -0700 | [diff] [blame] | 100 | (*dataLen) = sizeof(struct CableReply); |
| 101 | |
| 102 | return IPMI_CC_OK; |
| 103 | } |
| 104 | |
| 105 | } // namespace ipmi |
| 106 | } // namespace google |