blob: 682fe45ee30bc30550b288ae7c81b474841e84af [file] [log] [blame]
Patrick Venture123b5c02019-03-05 14:01:00 -08001/*
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 "ipmi_handler.hpp"
18
19#include "ipmi_errors.hpp"
20
21#include <fcntl.h>
22#include <linux/ipmi.h>
23#include <linux/ipmi_msgdefs.h>
24#include <sys/ioctl.h>
25
26#include <array>
27#include <cstdint>
28#include <cstring>
Patrick Venture2fe4c652019-05-13 07:37:33 -070029#include <memory>
Patrick Venture123b5c02019-03-05 14:01:00 -080030#include <sstream>
31#include <string>
32#include <vector>
33
Patrick Venture1470bec2019-03-06 07:33:12 -080034namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080035{
36
Patrick Venture2fe4c652019-05-13 07:37:33 -070037std::unique_ptr<IpmiInterface> IpmiHandler::CreateIpmiHandler()
38{
39 return std::make_unique<IpmiHandler>();
40}
41
Patrick Venture123b5c02019-03-05 14:01:00 -080042void IpmiHandler::open()
43{
44 const int device = 0;
45 const std::vector<std::string> formats = {"/dev/ipmi", "/dev/ipmi/",
46 "/dev/ipmidev/"};
47
48 for (const auto& format : formats)
49 {
50 std::ostringstream path;
51 path << format << device;
52
53 fd = sys->open(path.str().c_str(), O_RDWR);
54 if (fd < 0)
55 {
56 continue;
57 }
58 break;
59 }
60
61 if (fd < 0)
62 {
63 throw IpmiException("Unable to open any ipmi devices");
64 }
65}
66
67std::vector<std::uint8_t>
Patrick Venture958f1ce2019-05-31 17:07:25 -070068 IpmiHandler::sendPacket(std::uint8_t netfn, std::uint8_t cmd,
69 std::vector<std::uint8_t>& data)
Patrick Venture123b5c02019-03-05 14:01:00 -080070{
71 if (fd < 0)
72 {
73 open();
74 }
75
Patrick Venture123b5c02019-03-05 14:01:00 -080076 constexpr int ipmiOEMLun = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080077 constexpr int fifteenMs = 15 * 1000;
78 constexpr int ipmiReadTimeout = fifteenMs;
79 constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
80 constexpr int ipmiOk = 0;
81
82 /* We have a handle to the IPMI device. */
83 std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer;
84
85 /* Build address. */
86 struct ipmi_system_interface_addr systemAddress;
87 systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
88 systemAddress.channel = IPMI_BMC_CHANNEL;
89 systemAddress.lun = ipmiOEMLun;
90
91 /* Build request. */
92 struct ipmi_req request;
93 std::memset(&request, 0, sizeof(request));
94 request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
95 request.addr_len = sizeof(systemAddress);
96 request.msgid = sequence++;
97 request.msg.data = reinterpret_cast<unsigned char*>(data.data());
98 request.msg.data_len = data.size();
Patrick Venture958f1ce2019-05-31 17:07:25 -070099 request.msg.netfn = netfn;
100 request.msg.cmd = cmd;
Patrick Venture123b5c02019-03-05 14:01:00 -0800101
102 struct ipmi_recv reply;
103 reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
104 reply.addr_len = sizeof(systemAddress);
105 reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
106 reply.msg.data_len = responseBuffer.size();
107
108 /* Try to send request. */
109 int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
110 if (rc < 0)
111 {
112 throw IpmiException("Unable to send IPMI request.");
113 }
114
115 /* Could use sdeventplus, but for only one type of event is it worth it? */
116 struct pollfd pfd;
117 pfd.fd = fd;
118 pfd.events = POLLIN;
119
120 do
121 {
122 rc = sys->poll(&pfd, 1, ipmiReadTimeout);
123 if (rc < 0)
124 {
125 if (errno == EINTR)
126 {
127 continue;
128 }
129 throw IpmiException("Error occurred.");
130 }
131 else if (rc == 0)
132 {
133 throw IpmiException("Timeout waiting for reply.");
134 }
135
136 /* Yay, happy case! */
137 rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
138 if (rc < 0)
139 {
140 throw IpmiException("Unable to read reply.");
141 }
142
143 if (request.msgid != reply.msgid)
144 {
145 std::fprintf(stderr, "Received wrong message, trying again.\n");
146 }
147 } while (request.msgid != reply.msgid);
148
149 if (responseBuffer[0] != ipmiOk)
150 {
151 throw IpmiException(static_cast<int>(responseBuffer[0]));
152 }
153
154 std::vector<std::uint8_t> returning;
155 auto dataLen = reply.msg.data_len - 1;
156
157 returning.insert(returning.begin(), responseBuffer.begin() + 1,
158 responseBuffer.begin() + dataLen + 1);
159
Patrick Venture123b5c02019-03-05 14:01:00 -0800160 return returning;
161}
162
Patrick Venture1470bec2019-03-06 07:33:12 -0800163} // namespace ipmiblob