blob: ac2ca3afc8ad40c7e88ac10d1475dd0d361dbbf1 [file] [log] [blame]
Patrick Venturef085d912019-03-15 08:50:00 -07001/*
2 * Copyright 2019 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 "handler.hpp"
18
Patrick Ventured2037c62019-03-15 10:29:47 -070019#include "errors.hpp"
20
21#include <ipmid/api.h>
22
Patrick Venturebb90d4f2019-03-15 13:42:06 -070023#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070024#include <cstdio>
25#include <filesystem>
26#include <fstream>
27#include <sstream>
28#include <string>
29#include <tuple>
30
Patrick Venturef085d912019-03-15 08:50:00 -070031// The phosphor-host-ipmi daemon requires a configuration that maps
32// the if_name to the IPMI LAN channel. However, that doesn't strictly
33// define which is meant to be used for NCSI.
34#ifndef NCSI_IPMI_CHANNEL
35#define NCSI_IPMI_CHANNEL 1
36#endif
37
38#ifndef NCSI_IF_NAME
39#define NCSI_IF_NAME eth0
40#endif
41
42// To deal with receiving a string without quotes.
43#define QUOTE(name) #name
44#define STR(macro) QUOTE(macro)
45#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
46
47namespace google
48{
49namespace ipmi
50{
Patrick Ventured2037c62019-03-15 10:29:47 -070051namespace fs = std::filesystem;
Patrick Venturef085d912019-03-15 08:50:00 -070052
53std::tuple<std::uint8_t, std::string> Handler::getEthDetails() const
54{
55 return std::make_tuple(NCSI_IPMI_CHANNEL, NCSI_IF_NAME_STR);
56}
57
Patrick Ventured2037c62019-03-15 10:29:47 -070058std::int64_t Handler::getRxPackets(const std::string& name) const
59{
60 std::ostringstream opath;
61 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
62 std::string path = opath.str();
63
64 // Minor sanity & security check (of course, I'm less certain if unicode
65 // comes into play here.
66 //
67 // Basically you can't easily inject ../ or /../ into the path below.
68 if (name.find("/") != std::string::npos)
69 {
70 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
71 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
72 }
73
74 std::error_code ec;
75 if (!fs::exists(path, ec))
76 {
77 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
78 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
79 }
80 // We're uninterested in the state of ec.
81
82 int64_t count = 0;
83 std::ifstream ifs;
84 ifs.exceptions(std::ifstream::failbit);
85 try
86 {
87 ifs.open(path);
88 ifs >> count;
89 }
90 catch (std::ios_base::failure& fail)
91 {
92 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
93 }
94
95 return count;
96}
97
Patrick Venturebb90d4f2019-03-15 13:42:06 -070098VersionTuple Handler::getCpldVersion(unsigned int id) const
99{
100 std::ostringstream opath;
101 opath << "/run/cpld" << id << ".version";
102 // Check for file
103
104 std::error_code ec;
105 if (!fs::exists(opath.str(), ec))
106 {
107 std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
108 opath.str().c_str());
109 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
110 }
111 // We're uninterested in the state of ec.
112
113 // If file exists, read.
114 std::ifstream ifs;
115 ifs.exceptions(std::ifstream::failbit);
116 std::string value;
117 try
118 {
119 ifs.open(opath.str());
120 ifs >> value;
121 }
122 catch (std::ios_base::failure& fail)
123 {
124 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
125 }
126
127 // If value parses as expected, return version.
128 VersionTuple version = std::make_tuple(0, 0, 0, 0);
129
130 int num_fields =
131 std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
132 &std::get<0>(version), &std::get<1>(version),
133 &std::get<2>(version), &std::get<3>(version));
134 if (num_fields == 0)
135 {
136 std::fprintf(stderr, "Invalid version.\n");
137 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
138 }
139
140 return version;
141}
142
Patrick Venturef085d912019-03-15 08:50:00 -0700143Handler handlerImpl;
144
145} // namespace ipmi
146} // namespace google