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