blob: 0d7c44ec6d07568687367e53dc0786383189c93e [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. */
Patrick Venture570fcc62020-06-26 13:56:24 -070083 std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer = {};
Patrick Venture123b5c02019-03-05 14:01:00 -080084
85 /* Build address. */
Patrick Venture48125b52020-06-24 08:46:20 -070086 ipmi_system_interface_addr systemAddress{};
Patrick Venture123b5c02019-03-05 14:01:00 -080087 systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
88 systemAddress.channel = IPMI_BMC_CHANNEL;
89 systemAddress.lun = ipmiOEMLun;
90
91 /* Build request. */
Patrick Venture48125b52020-06-24 08:46:20 -070092 ipmi_req request{};
Patrick Venture123b5c02019-03-05 14:01:00 -080093 request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
94 request.addr_len = sizeof(systemAddress);
95 request.msgid = sequence++;
96 request.msg.data = reinterpret_cast<unsigned char*>(data.data());
97 request.msg.data_len = data.size();
Patrick Venture958f1ce2019-05-31 17:07:25 -070098 request.msg.netfn = netfn;
99 request.msg.cmd = cmd;
Patrick Venture123b5c02019-03-05 14:01:00 -0800100
Patrick Venture48125b52020-06-24 08:46:20 -0700101 ipmi_recv reply{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800102 reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
103 reply.addr_len = sizeof(systemAddress);
104 reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
105 reply.msg.data_len = responseBuffer.size();
106
107 /* Try to send request. */
108 int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
109 if (rc < 0)
110 {
111 throw IpmiException("Unable to send IPMI request.");
112 }
113
114 /* Could use sdeventplus, but for only one type of event is it worth it? */
Patrick Venture48125b52020-06-24 08:46:20 -0700115 pollfd pfd{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800116 pfd.fd = fd;
117 pfd.events = POLLIN;
118
119 do
120 {
121 rc = sys->poll(&pfd, 1, ipmiReadTimeout);
122 if (rc < 0)
123 {
124 if (errno == EINTR)
125 {
126 continue;
127 }
128 throw IpmiException("Error occurred.");
129 }
130 else if (rc == 0)
131 {
132 throw IpmiException("Timeout waiting for reply.");
133 }
134
135 /* Yay, happy case! */
136 rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
137 if (rc < 0)
138 {
139 throw IpmiException("Unable to read reply.");
140 }
141
142 if (request.msgid != reply.msgid)
143 {
144 std::fprintf(stderr, "Received wrong message, trying again.\n");
145 }
146 } while (request.msgid != reply.msgid);
147
148 if (responseBuffer[0] != ipmiOk)
149 {
150 throw IpmiException(static_cast<int>(responseBuffer[0]));
151 }
152
153 std::vector<std::uint8_t> returning;
154 auto dataLen = reply.msg.data_len - 1;
155
156 returning.insert(returning.begin(), responseBuffer.begin() + 1,
157 responseBuffer.begin() + dataLen + 1);
158
Patrick Venture123b5c02019-03-05 14:01:00 -0800159 return returning;
160}
161
Patrick Venture1470bec2019-03-06 07:33:12 -0800162} // namespace ipmiblob