Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -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 "config.h" |
| 18 | |
| 19 | #include "ethstats.hpp" |
| 20 | |
William A. Kennington III | 539f03f | 2019-02-12 13:28:45 -0800 | [diff] [blame] | 21 | #include <ipmid/api.h> |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 22 | |
| 23 | #include <cstdint> |
| 24 | #include <cstring> |
Patrick Venture | d087d81 | 2019-03-08 09:12:50 -0800 | [diff] [blame^] | 25 | #include <filesystem> |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 26 | #include <fstream> |
William A. Kennington III | 539f03f | 2019-02-12 13:28:45 -0800 | [diff] [blame] | 27 | #include <ipmid/iana.hpp> |
| 28 | #include <ipmid/oemopenbmc.hpp> |
| 29 | #include <ipmid/oemrouter.hpp> |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 30 | #include <map> |
| 31 | #include <sstream> |
| 32 | #include <string> |
| 33 | |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 34 | namespace ethstats |
| 35 | { |
Patrick Venture | d087d81 | 2019-03-08 09:12:50 -0800 | [diff] [blame^] | 36 | namespace fs = std::filesystem; |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 37 | |
| 38 | // If this changes in the future, there should be some alternative |
| 39 | // source for the information if possible to provide continuined functionality. |
| 40 | static const std::map<uint8_t, std::string> statLookup = { |
| 41 | {RX_BYTES, "rx_bytes"}, |
| 42 | {RX_COMPRESSED, "rx_compressed"}, |
| 43 | {RX_CRC_ERRORS, "rx_crc_errors"}, |
| 44 | {RX_DROPPED, "rx_dropped"}, |
| 45 | {RX_ERRORS, "rx_errors"}, |
| 46 | {RX_FIFO_ERRORS, "rx_fifo_errors"}, |
| 47 | {RX_FRAME_ERRORS, "rx_frame_errors"}, |
| 48 | {RX_LENGTH_ERRORS, "rx_length_errors"}, |
| 49 | {RX_MISSED_ERRORS, "rx_missed_errors"}, |
| 50 | {RX_NOHANDLER, "rx_nohandler"}, |
| 51 | {RX_OVER_ERRORS, "rx_over_errors"}, |
| 52 | {RX_PACKETS, "rx_packets"}, |
| 53 | {TX_ABORTED_ERRORS, "tx_aborted_errors"}, |
| 54 | {TX_BYTES, "tx_bytes"}, |
| 55 | {TX_CARRIER_ERRORS, "tx_carrier_errors"}, |
| 56 | {TX_COMPRESSED, "tx_compressed"}, |
| 57 | {TX_DROPPED, "tx_dropped"}, |
| 58 | {TX_ERRORS, "tx_errors"}, |
| 59 | {TX_FIFO_ERRORS, "tx_fifo_errors"}, |
| 60 | {TX_HEARTBEAT_ERRORS, "tx_heartbeat_errors"}, |
| 61 | {TX_PACKETS, "tx_packets"}, |
| 62 | {TX_WINDOW_ERRORS, "tx_window_errors"}, |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * Handle the OEM IPMI EthStat Command. |
| 67 | * |
| 68 | * @param[in] cmd - the OEM command |
| 69 | * @param[in] reqBuf - The IPMI request buffer. |
| 70 | * @param[in,out] replyCmdBuf - the IPMI reply buffer. |
| 71 | * @param[in,out] dataLen - The length of the request and reply. |
| 72 | * @return the IPMI result code. |
| 73 | */ |
Patrick Venture | 6e1725a | 2019-03-07 17:06:27 -0800 | [diff] [blame] | 74 | static ipmi_ret_t HandleEthStatCommand(ipmi_cmd_t cmd __attribute__((unused)), |
| 75 | const uint8_t* reqBuf, |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 76 | uint8_t* replyCmdBuf, size_t* dataLen) |
| 77 | { |
| 78 | auto reqLength = (*dataLen); |
| 79 | |
| 80 | // Verify the reqBuf is the minimum length. |
| 81 | // [0] == statistics id |
| 82 | // [1] == if_name_length |
| 83 | // [2..N] == if_name |
| 84 | // In theory the smallest can be a one-letter name. (3 bytes). |
| 85 | if (reqLength < sizeof(struct EthStatRequest) + sizeof(uint8_t)) |
| 86 | { |
| 87 | std::fprintf(stderr, "*dataLen too small: %u\n", |
| 88 | static_cast<uint32_t>(reqLength)); |
Patrick Venture | 0080189 | 2018-11-12 07:44:59 -0800 | [diff] [blame] | 89 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // using struct prefix due to nature as c-style pod struct. |
| 93 | struct EthStatRequest request; |
| 94 | std::memcpy(&request, &reqBuf[0], sizeof(request)); |
| 95 | auto nameLen = static_cast<uint32_t>(request.if_name_len); |
| 96 | |
| 97 | if (reqLength < (sizeof(request) + nameLen)) |
| 98 | { |
| 99 | std::fprintf(stderr, "*dataLen too small: %u\n", |
| 100 | static_cast<uint32_t>(reqLength)); |
Patrick Venture | 0080189 | 2018-11-12 07:44:59 -0800 | [diff] [blame] | 101 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Check the statistic to see if we recognize it. |
| 105 | auto stat = statLookup.find(request.statId); |
| 106 | if (stat == statLookup.end()) |
| 107 | { |
| 108 | std::fprintf(stderr, "stat not known: 0x%x\n", request.statId); |
| 109 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 110 | } |
| 111 | |
| 112 | // The if_name handling plus a few other things was taken from the |
| 113 | // CableCheck command implementation. |
| 114 | // |
| 115 | // Ok, so we know what statistic they want. Let's validate their |
| 116 | // if_name. The string is length delimited (like dns). |
| 117 | |
| 118 | // Copy the string out of the request buffer. |
| 119 | // Maximum length is 256 bytes, excluding the nul-terminator. |
| 120 | auto name = std::string( |
| 121 | reinterpret_cast<const char*>(&reqBuf[0] + sizeof(request)), nameLen); |
| 122 | |
| 123 | // Minor sanity & security check (of course, I'm less certain if unicode |
| 124 | // comes into play here. |
| 125 | // |
| 126 | // Basically you can't easily inject ../ or /../ into the path below. |
| 127 | // Decided to make this more robust, although since it appends to the path |
| 128 | // it would limit any exposure. |
| 129 | if (name.find("/") != std::string::npos) |
| 130 | { |
| 131 | std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str()); |
Patrick Venture | 0080189 | 2018-11-12 07:44:59 -0800 | [diff] [blame] | 132 | return IPMI_CC_INVALID_FIELD_REQUEST; |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // TODO: Transition to using the netlink api. |
| 136 | std::ostringstream opath; |
| 137 | opath << "/sys/class/net/" << name << "/statistics/" << stat->second; |
| 138 | std::string path = opath.str(); |
| 139 | |
| 140 | std::error_code ec; |
| 141 | if (!fs::exists(path, ec)) |
| 142 | { |
| 143 | std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str()); |
Patrick Venture | 0080189 | 2018-11-12 07:44:59 -0800 | [diff] [blame] | 144 | return IPMI_CC_INVALID_FIELD_REQUEST; |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 145 | } |
| 146 | // We're uninterested in the state of ec. |
| 147 | |
| 148 | // Read the file and check the result. |
| 149 | // We read the number as int64, then check to make sure it's positive |
| 150 | // before casting to uint64. |
| 151 | uint64_t value = 0; |
| 152 | std::ifstream ifs; |
| 153 | ifs.exceptions(std::ifstream::failbit); |
| 154 | |
| 155 | try |
| 156 | { |
| 157 | ifs.open(path); |
| 158 | ifs >> value; |
| 159 | } |
| 160 | catch (std::ios_base::failure& fail) |
| 161 | { |
Patrick Venture | 0080189 | 2018-11-12 07:44:59 -0800 | [diff] [blame] | 162 | return IPMI_CC_UNSPECIFIED_ERROR; |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | struct EthStatReply reply; |
| 166 | reply.statId = request.statId; |
| 167 | reply.value = value; |
| 168 | |
| 169 | // Store the result. |
| 170 | std::memcpy(&replyCmdBuf[0], &reply, sizeof(reply)); |
| 171 | (*dataLen) = sizeof(reply); |
| 172 | |
| 173 | return IPMI_CC_OK; |
| 174 | } |
| 175 | |
| 176 | void setupGlobalOemEthStats() __attribute__((constructor)); |
| 177 | |
| 178 | void setupGlobalOemEthStats() |
| 179 | { |
| 180 | oem::Router* oemRouter = oem::mutableRouter(); |
| 181 | |
Patrick Venture | d72f8b5 | 2019-03-07 16:54:57 -0800 | [diff] [blame] | 182 | #if ENABLE_GOOGLE |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 183 | /* Install in Google OEM Namespace when enabled. */ |
| 184 | std::fprintf(stderr, |
| 185 | "Registering OEM:[%#08X], Cmd:[%#04X] for Ethstats Commands\n", |
Patrick Venture | ddee9d0 | 2018-11-15 14:50:52 -0800 | [diff] [blame] | 186 | oem::googOemNumber, oem::Cmd::ethStatsCmd); |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 187 | |
Patrick Venture | ddee9d0 | 2018-11-15 14:50:52 -0800 | [diff] [blame] | 188 | oemRouter->registerHandler(oem::googOemNumber, oem::Cmd::ethStatsCmd, |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 189 | HandleEthStatCommand); |
| 190 | #endif |
| 191 | |
| 192 | std::fprintf(stderr, |
| 193 | "Registering OEM:[%#08X], Cmd:[%#04X] for Ethstats Commands\n", |
Patrick Venture | ddee9d0 | 2018-11-15 14:50:52 -0800 | [diff] [blame] | 194 | oem::obmcOemNumber, oem::Cmd::ethStatsCmd); |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 195 | |
Patrick Venture | ddee9d0 | 2018-11-15 14:50:52 -0800 | [diff] [blame] | 196 | oemRouter->registerHandler(oem::obmcOemNumber, oem::Cmd::ethStatsCmd, |
Patrick Venture | d26fff4 | 2018-09-18 15:37:59 -0700 | [diff] [blame] | 197 | HandleEthStatCommand); |
| 198 | } |
| 199 | |
| 200 | } // namespace ethstats |