blob: 07c2e1e4f7115241c0f198d25bbd59cdd90d3783 [file] [log] [blame]
Patrick Ventured26fff42018-09-18 15:37:59 -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 "config.h"
18
19#include "ethstats.hpp"
20
William A. Kennington III539f03f2019-02-12 13:28:45 -080021#include <ipmid/api.h>
Patrick Ventured26fff42018-09-18 15:37:59 -070022
23#include <cstdint>
24#include <cstring>
Patrick Ventured087d812019-03-08 09:12:50 -080025#include <filesystem>
Patrick Ventured26fff42018-09-18 15:37:59 -070026#include <fstream>
William A. Kennington III539f03f2019-02-12 13:28:45 -080027#include <ipmid/iana.hpp>
28#include <ipmid/oemopenbmc.hpp>
29#include <ipmid/oemrouter.hpp>
Patrick Ventured26fff42018-09-18 15:37:59 -070030#include <map>
31#include <sstream>
32#include <string>
33
Patrick Ventured26fff42018-09-18 15:37:59 -070034namespace ethstats
35{
Patrick Ventured087d812019-03-08 09:12:50 -080036namespace fs = std::filesystem;
Patrick Ventured26fff42018-09-18 15:37:59 -070037
38// If this changes in the future, there should be some alternative
39// source for the information if possible to provide continuined functionality.
40static 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 Venture6e1725a2019-03-07 17:06:27 -080074static ipmi_ret_t HandleEthStatCommand(ipmi_cmd_t cmd __attribute__((unused)),
75 const uint8_t* reqBuf,
Patrick Ventured26fff42018-09-18 15:37:59 -070076 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 Venture00801892018-11-12 07:44:59 -080089 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Ventured26fff42018-09-18 15:37:59 -070090 }
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 Venture00801892018-11-12 07:44:59 -0800101 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Ventured26fff42018-09-18 15:37:59 -0700102 }
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 Venture00801892018-11-12 07:44:59 -0800132 return IPMI_CC_INVALID_FIELD_REQUEST;
Patrick Ventured26fff42018-09-18 15:37:59 -0700133 }
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 Venture00801892018-11-12 07:44:59 -0800144 return IPMI_CC_INVALID_FIELD_REQUEST;
Patrick Ventured26fff42018-09-18 15:37:59 -0700145 }
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 Venture00801892018-11-12 07:44:59 -0800162 return IPMI_CC_UNSPECIFIED_ERROR;
Patrick Ventured26fff42018-09-18 15:37:59 -0700163 }
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
176void setupGlobalOemEthStats() __attribute__((constructor));
177
178void setupGlobalOemEthStats()
179{
180 oem::Router* oemRouter = oem::mutableRouter();
181
Patrick Ventured72f8b52019-03-07 16:54:57 -0800182#if ENABLE_GOOGLE
Patrick Ventured26fff42018-09-18 15:37:59 -0700183 /* Install in Google OEM Namespace when enabled. */
184 std::fprintf(stderr,
185 "Registering OEM:[%#08X], Cmd:[%#04X] for Ethstats Commands\n",
Patrick Ventureddee9d02018-11-15 14:50:52 -0800186 oem::googOemNumber, oem::Cmd::ethStatsCmd);
Patrick Ventured26fff42018-09-18 15:37:59 -0700187
Patrick Ventureddee9d02018-11-15 14:50:52 -0800188 oemRouter->registerHandler(oem::googOemNumber, oem::Cmd::ethStatsCmd,
Patrick Ventured26fff42018-09-18 15:37:59 -0700189 HandleEthStatCommand);
190#endif
191
192 std::fprintf(stderr,
193 "Registering OEM:[%#08X], Cmd:[%#04X] for Ethstats Commands\n",
Patrick Ventureddee9d02018-11-15 14:50:52 -0800194 oem::obmcOemNumber, oem::Cmd::ethStatsCmd);
Patrick Ventured26fff42018-09-18 15:37:59 -0700195
Patrick Ventureddee9d02018-11-15 14:50:52 -0800196 oemRouter->registerHandler(oem::obmcOemNumber, oem::Cmd::ethStatsCmd,
Patrick Ventured26fff42018-09-18 15:37:59 -0700197 HandleEthStatCommand);
198}
199
200} // namespace ethstats