blob: 3ce5d92394181bf212775ae86e118632c82fbc74 [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>
Willy Tu13224372024-07-15 17:00:15 +000030#include <functional>
Patrick Venture2fe4c652019-05-13 07:37:33 -070031#include <memory>
Willy Tu13224372024-07-15 17:00:15 +000032#include <mutex>
Patrick Venture123b5c02019-03-05 14:01:00 -080033#include <sstream>
34#include <string>
35#include <vector>
36
Patrick Venture1470bec2019-03-06 07:33:12 -080037namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080038{
39
Patrick Venture2fe4c652019-05-13 07:37:33 -070040std::unique_ptr<IpmiInterface> IpmiHandler::CreateIpmiHandler()
41{
Patrick Venture92a4a622021-02-04 10:45:00 -080042 return std::make_unique<IpmiHandler>(std::make_unique<internal::SysImpl>());
Patrick Venture2fe4c652019-05-13 07:37:33 -070043}
44
Patrick Venture123b5c02019-03-05 14:01:00 -080045void IpmiHandler::open()
46{
Willy Tu13224372024-07-15 17:00:15 +000047 std::lock_guard<std::mutex> guard(openMutex);
48 if (fd >= 0)
49 {
50 return;
51 }
Patrick Venture123b5c02019-03-05 14:01:00 -080052
Willy Tu13224372024-07-15 17:00:15 +000053 constexpr int device = 0;
54 const std::array<std::string, 3> formats = {"/dev/ipmi", "/dev/ipmi/",
55 "/dev/ipmidev/"};
56 for (const std::string& format : formats)
Patrick Venture123b5c02019-03-05 14:01:00 -080057 {
58 std::ostringstream path;
59 path << format << device;
60
61 fd = sys->open(path.str().c_str(), O_RDWR);
62 if (fd < 0)
63 {
64 continue;
65 }
66 break;
67 }
68
69 if (fd < 0)
70 {
71 throw IpmiException("Unable to open any ipmi devices");
72 }
73}
74
75std::vector<std::uint8_t>
Patrick Venture958f1ce2019-05-31 17:07:25 -070076 IpmiHandler::sendPacket(std::uint8_t netfn, std::uint8_t cmd,
77 std::vector<std::uint8_t>& data)
Patrick Venture123b5c02019-03-05 14:01:00 -080078{
Willy Tu13224372024-07-15 17:00:15 +000079 open();
Patrick Venture123b5c02019-03-05 14:01:00 -080080
Patrick Venture123b5c02019-03-05 14:01:00 -080081 constexpr int ipmiOEMLun = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080082 constexpr int fifteenMs = 15 * 1000;
83 constexpr int ipmiReadTimeout = fifteenMs;
84 constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
85 constexpr int ipmiOk = 0;
86
87 /* We have a handle to the IPMI device. */
Patrick Venture570fcc62020-06-26 13:56:24 -070088 std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer = {};
Patrick Venture123b5c02019-03-05 14:01:00 -080089
90 /* Build address. */
Patrick Venture48125b52020-06-24 08:46:20 -070091 ipmi_system_interface_addr systemAddress{};
Patrick Venture123b5c02019-03-05 14:01:00 -080092 systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
93 systemAddress.channel = IPMI_BMC_CHANNEL;
94 systemAddress.lun = ipmiOEMLun;
95
96 /* Build request. */
Patrick Venture48125b52020-06-24 08:46:20 -070097 ipmi_req request{};
Patrick Venture123b5c02019-03-05 14:01:00 -080098 request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
99 request.addr_len = sizeof(systemAddress);
Anh Phan626bf762024-07-12 11:51:02 +0000100 request.msgid = sequence.fetch_add(1, std::memory_order_relaxed);
Patrick Venture123b5c02019-03-05 14:01:00 -0800101 request.msg.data = reinterpret_cast<unsigned char*>(data.data());
102 request.msg.data_len = data.size();
Patrick Venture958f1ce2019-05-31 17:07:25 -0700103 request.msg.netfn = netfn;
104 request.msg.cmd = cmd;
Patrick Venture123b5c02019-03-05 14:01:00 -0800105
Patrick Venture48125b52020-06-24 08:46:20 -0700106 ipmi_recv reply{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800107 reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
108 reply.addr_len = sizeof(systemAddress);
109 reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
110 reply.msg.data_len = responseBuffer.size();
111
112 /* Try to send request. */
113 int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
114 if (rc < 0)
115 {
116 throw IpmiException("Unable to send IPMI request.");
117 }
118
119 /* Could use sdeventplus, but for only one type of event is it worth it? */
Patrick Venture48125b52020-06-24 08:46:20 -0700120 pollfd pfd{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800121 pfd.fd = fd;
122 pfd.events = POLLIN;
123
124 do
125 {
126 rc = sys->poll(&pfd, 1, ipmiReadTimeout);
127 if (rc < 0)
128 {
129 if (errno == EINTR)
130 {
131 continue;
132 }
Willy Tu13224372024-07-15 17:00:15 +0000133 throw IpmiException("Polling Error occurred.");
Patrick Venture123b5c02019-03-05 14:01:00 -0800134 }
135 else if (rc == 0)
136 {
137 throw IpmiException("Timeout waiting for reply.");
138 }
139
140 /* Yay, happy case! */
141 rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
142 if (rc < 0)
143 {
144 throw IpmiException("Unable to read reply.");
145 }
146
147 if (request.msgid != reply.msgid)
148 {
149 std::fprintf(stderr, "Received wrong message, trying again.\n");
150 }
151 } while (request.msgid != reply.msgid);
152
153 if (responseBuffer[0] != ipmiOk)
154 {
155 throw IpmiException(static_cast<int>(responseBuffer[0]));
156 }
157
158 std::vector<std::uint8_t> returning;
159 auto dataLen = reply.msg.data_len - 1;
160
161 returning.insert(returning.begin(), responseBuffer.begin() + 1,
162 responseBuffer.begin() + dataLen + 1);
163
Patrick Venture123b5c02019-03-05 14:01:00 -0800164 return returning;
165}
166
Patrick Venture1470bec2019-03-06 07:33:12 -0800167} // namespace ipmiblob