blob: 0f9bcad7476a9a8b6cf82b12e45b1ec98cc8c012 [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>
25#include <experimental/filesystem>
26#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{
36namespace fs = std::experimental::filesystem;
37
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 */
74static ipmi_ret_t HandleEthStatCommand(ipmi_cmd_t cmd, const uint8_t* reqBuf,
75 uint8_t* replyCmdBuf, size_t* dataLen)
76{
77 auto reqLength = (*dataLen);
78
79 // Verify the reqBuf is the minimum length.
80 // [0] == statistics id
81 // [1] == if_name_length
82 // [2..N] == if_name
83 // In theory the smallest can be a one-letter name. (3 bytes).
84 if (reqLength < sizeof(struct EthStatRequest) + sizeof(uint8_t))
85 {
86 std::fprintf(stderr, "*dataLen too small: %u\n",
87 static_cast<uint32_t>(reqLength));
Patrick Venture00801892018-11-12 07:44:59 -080088 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Ventured26fff42018-09-18 15:37:59 -070089 }
90
91 // using struct prefix due to nature as c-style pod struct.
92 struct EthStatRequest request;
93 std::memcpy(&request, &reqBuf[0], sizeof(request));
94 auto nameLen = static_cast<uint32_t>(request.if_name_len);
95
96 if (reqLength < (sizeof(request) + nameLen))
97 {
98 std::fprintf(stderr, "*dataLen too small: %u\n",
99 static_cast<uint32_t>(reqLength));
Patrick Venture00801892018-11-12 07:44:59 -0800100 return IPMI_CC_REQ_DATA_LEN_INVALID;
Patrick Ventured26fff42018-09-18 15:37:59 -0700101 }
102
103 // Check the statistic to see if we recognize it.
104 auto stat = statLookup.find(request.statId);
105 if (stat == statLookup.end())
106 {
107 std::fprintf(stderr, "stat not known: 0x%x\n", request.statId);
108 return IPMI_CC_INVALID_FIELD_REQUEST;
109 }
110
111 // The if_name handling plus a few other things was taken from the
112 // CableCheck command implementation.
113 //
114 // Ok, so we know what statistic they want. Let's validate their
115 // if_name. The string is length delimited (like dns).
116
117 // Copy the string out of the request buffer.
118 // Maximum length is 256 bytes, excluding the nul-terminator.
119 auto name = std::string(
120 reinterpret_cast<const char*>(&reqBuf[0] + sizeof(request)), nameLen);
121
122 // Minor sanity & security check (of course, I'm less certain if unicode
123 // comes into play here.
124 //
125 // Basically you can't easily inject ../ or /../ into the path below.
126 // Decided to make this more robust, although since it appends to the path
127 // it would limit any exposure.
128 if (name.find("/") != std::string::npos)
129 {
130 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
Patrick Venture00801892018-11-12 07:44:59 -0800131 return IPMI_CC_INVALID_FIELD_REQUEST;
Patrick Ventured26fff42018-09-18 15:37:59 -0700132 }
133
134 // TODO: Transition to using the netlink api.
135 std::ostringstream opath;
136 opath << "/sys/class/net/" << name << "/statistics/" << stat->second;
137 std::string path = opath.str();
138
139 std::error_code ec;
140 if (!fs::exists(path, ec))
141 {
142 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
Patrick Venture00801892018-11-12 07:44:59 -0800143 return IPMI_CC_INVALID_FIELD_REQUEST;
Patrick Ventured26fff42018-09-18 15:37:59 -0700144 }
145 // We're uninterested in the state of ec.
146
147 // Read the file and check the result.
148 // We read the number as int64, then check to make sure it's positive
149 // before casting to uint64.
150 uint64_t value = 0;
151 std::ifstream ifs;
152 ifs.exceptions(std::ifstream::failbit);
153
154 try
155 {
156 ifs.open(path);
157 ifs >> value;
158 }
159 catch (std::ios_base::failure& fail)
160 {
Patrick Venture00801892018-11-12 07:44:59 -0800161 return IPMI_CC_UNSPECIFIED_ERROR;
Patrick Ventured26fff42018-09-18 15:37:59 -0700162 }
163
164 struct EthStatReply reply;
165 reply.statId = request.statId;
166 reply.value = value;
167
168 // Store the result.
169 std::memcpy(&replyCmdBuf[0], &reply, sizeof(reply));
170 (*dataLen) = sizeof(reply);
171
172 return IPMI_CC_OK;
173}
174
175void setupGlobalOemEthStats() __attribute__((constructor));
176
177void setupGlobalOemEthStats()
178{
179 oem::Router* oemRouter = oem::mutableRouter();
180
Patrick Ventured72f8b52019-03-07 16:54:57 -0800181#if ENABLE_GOOGLE
Patrick Ventured26fff42018-09-18 15:37:59 -0700182 /* Install in Google OEM Namespace when enabled. */
183 std::fprintf(stderr,
184 "Registering OEM:[%#08X], Cmd:[%#04X] for Ethstats Commands\n",
Patrick Ventureddee9d02018-11-15 14:50:52 -0800185 oem::googOemNumber, oem::Cmd::ethStatsCmd);
Patrick Ventured26fff42018-09-18 15:37:59 -0700186
Patrick Ventureddee9d02018-11-15 14:50:52 -0800187 oemRouter->registerHandler(oem::googOemNumber, oem::Cmd::ethStatsCmd,
Patrick Ventured26fff42018-09-18 15:37:59 -0700188 HandleEthStatCommand);
189#endif
190
191 std::fprintf(stderr,
192 "Registering OEM:[%#08X], Cmd:[%#04X] for Ethstats Commands\n",
Patrick Ventureddee9d02018-11-15 14:50:52 -0800193 oem::obmcOemNumber, oem::Cmd::ethStatsCmd);
Patrick Ventured26fff42018-09-18 15:37:59 -0700194
Patrick Ventureddee9d02018-11-15 14:50:52 -0800195 oemRouter->registerHandler(oem::obmcOemNumber, oem::Cmd::ethStatsCmd,
Patrick Ventured26fff42018-09-18 15:37:59 -0700196 HandleEthStatCommand);
197}
198
199} // namespace ethstats