blob: b51718af9a6d3d1fe55375771ef7e277b37ef0dd [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>
Willy Tud942a652024-07-15 16:54:06 +000027#include <atomic>
Patrick Venture123b5c02019-03-05 14:01:00 -080028#include <cstdint>
29#include <cstring>
Patrick Venture2fe4c652019-05-13 07:37:33 -070030#include <memory>
Patrick Venture123b5c02019-03-05 14:01:00 -080031#include <sstream>
32#include <string>
33#include <vector>
34
Patrick Venture1470bec2019-03-06 07:33:12 -080035namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080036{
37
Patrick Venture2fe4c652019-05-13 07:37:33 -070038std::unique_ptr<IpmiInterface> IpmiHandler::CreateIpmiHandler()
39{
Patrick Venture92a4a622021-02-04 10:45:00 -080040 return std::make_unique<IpmiHandler>(std::make_unique<internal::SysImpl>());
Patrick Venture2fe4c652019-05-13 07:37:33 -070041}
42
Patrick Venture123b5c02019-03-05 14:01:00 -080043void IpmiHandler::open()
44{
45 const int device = 0;
46 const std::vector<std::string> formats = {"/dev/ipmi", "/dev/ipmi/",
47 "/dev/ipmidev/"};
48
49 for (const auto& format : formats)
50 {
51 std::ostringstream path;
52 path << format << device;
53
54 fd = sys->open(path.str().c_str(), O_RDWR);
55 if (fd < 0)
56 {
57 continue;
58 }
59 break;
60 }
61
62 if (fd < 0)
63 {
64 throw IpmiException("Unable to open any ipmi devices");
65 }
66}
67
68std::vector<std::uint8_t>
Patrick Venture958f1ce2019-05-31 17:07:25 -070069 IpmiHandler::sendPacket(std::uint8_t netfn, std::uint8_t cmd,
70 std::vector<std::uint8_t>& data)
Patrick Venture123b5c02019-03-05 14:01:00 -080071{
72 if (fd < 0)
73 {
74 open();
75 }
76
Patrick Venture123b5c02019-03-05 14:01:00 -080077 constexpr int ipmiOEMLun = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080078 constexpr int fifteenMs = 15 * 1000;
79 constexpr int ipmiReadTimeout = fifteenMs;
80 constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
81 constexpr int ipmiOk = 0;
82
83 /* We have a handle to the IPMI device. */
Patrick Venture570fcc62020-06-26 13:56:24 -070084 std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer = {};
Patrick Venture123b5c02019-03-05 14:01:00 -080085
86 /* Build address. */
Patrick Venture48125b52020-06-24 08:46:20 -070087 ipmi_system_interface_addr systemAddress{};
Patrick Venture123b5c02019-03-05 14:01:00 -080088 systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
89 systemAddress.channel = IPMI_BMC_CHANNEL;
90 systemAddress.lun = ipmiOEMLun;
91
92 /* Build request. */
Patrick Venture48125b52020-06-24 08:46:20 -070093 ipmi_req request{};
Patrick Venture123b5c02019-03-05 14:01:00 -080094 request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
95 request.addr_len = sizeof(systemAddress);
Anh Phan626bf762024-07-12 11:51:02 +000096 request.msgid = sequence.fetch_add(1, std::memory_order_relaxed);
Patrick Venture123b5c02019-03-05 14:01:00 -080097 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
Patrick Venture48125b52020-06-24 08:46:20 -0700102 ipmi_recv reply{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800103 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? */
Patrick Venture48125b52020-06-24 08:46:20 -0700116 pollfd pfd{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800117 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