1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipmi_handler.hpp"
#include "ipmi_errors.hpp"
#include <fcntl.h>
#include <linux/ipmi.h>
#include <linux/ipmi_msgdefs.h>
#include <sys/ioctl.h>
#include <array>
#include <cstdint>
#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
namespace ipmiblob
{
std::unique_ptr<IpmiInterface> IpmiHandler::CreateIpmiHandler()
{
return std::make_unique<IpmiHandler>();
}
void IpmiHandler::open()
{
const int device = 0;
const std::vector<std::string> formats = {"/dev/ipmi", "/dev/ipmi/",
"/dev/ipmidev/"};
for (const auto& format : formats)
{
std::ostringstream path;
path << format << device;
fd = sys->open(path.str().c_str(), O_RDWR);
if (fd < 0)
{
continue;
}
break;
}
if (fd < 0)
{
throw IpmiException("Unable to open any ipmi devices");
}
}
std::vector<std::uint8_t>
IpmiHandler::sendPacket(std::uint8_t netfn, std::uint8_t cmd,
std::vector<std::uint8_t>& data)
{
if (fd < 0)
{
open();
}
constexpr int ipmiOEMLun = 0;
constexpr int fifteenMs = 15 * 1000;
constexpr int ipmiReadTimeout = fifteenMs;
constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
constexpr int ipmiOk = 0;
/* We have a handle to the IPMI device. */
std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer;
/* Build address. */
struct ipmi_system_interface_addr systemAddress;
systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
systemAddress.channel = IPMI_BMC_CHANNEL;
systemAddress.lun = ipmiOEMLun;
/* Build request. */
struct ipmi_req request;
std::memset(&request, 0, sizeof(request));
request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
request.addr_len = sizeof(systemAddress);
request.msgid = sequence++;
request.msg.data = reinterpret_cast<unsigned char*>(data.data());
request.msg.data_len = data.size();
request.msg.netfn = netfn;
request.msg.cmd = cmd;
struct ipmi_recv reply;
reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
reply.addr_len = sizeof(systemAddress);
reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
reply.msg.data_len = responseBuffer.size();
/* Try to send request. */
int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
if (rc < 0)
{
throw IpmiException("Unable to send IPMI request.");
}
/* Could use sdeventplus, but for only one type of event is it worth it? */
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLIN;
do
{
rc = sys->poll(&pfd, 1, ipmiReadTimeout);
if (rc < 0)
{
if (errno == EINTR)
{
continue;
}
throw IpmiException("Error occurred.");
}
else if (rc == 0)
{
throw IpmiException("Timeout waiting for reply.");
}
/* Yay, happy case! */
rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
if (rc < 0)
{
throw IpmiException("Unable to read reply.");
}
if (request.msgid != reply.msgid)
{
std::fprintf(stderr, "Received wrong message, trying again.\n");
}
} while (request.msgid != reply.msgid);
if (responseBuffer[0] != ipmiOk)
{
throw IpmiException(static_cast<int>(responseBuffer[0]));
}
std::vector<std::uint8_t> returning;
auto dataLen = reply.msg.data_len - 1;
returning.insert(returning.begin(), responseBuffer.begin() + 1,
responseBuffer.begin() + dataLen + 1);
return returning;
}
} // namespace ipmiblob
|