blob: dd53af5377a1461550aa1907ca7065d637ebdd26 [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>
Willy Tu13224372024-07-15 17:00:15 +000031#include <mutex>
Patrick Venture123b5c02019-03-05 14:01:00 -080032#include <sstream>
33#include <string>
34#include <vector>
35
Patrick Venture1470bec2019-03-06 07:33:12 -080036namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080037{
38
Patrick Venture2fe4c652019-05-13 07:37:33 -070039std::unique_ptr<IpmiInterface> IpmiHandler::CreateIpmiHandler()
40{
Patrick Venture92a4a622021-02-04 10:45:00 -080041 return std::make_unique<IpmiHandler>(std::make_unique<internal::SysImpl>());
Patrick Venture2fe4c652019-05-13 07:37:33 -070042}
43
Patrick Venture123b5c02019-03-05 14:01:00 -080044void IpmiHandler::open()
45{
Willy Tu13224372024-07-15 17:00:15 +000046 std::lock_guard<std::mutex> guard(openMutex);
47 if (fd >= 0)
48 {
49 return;
50 }
Patrick Venture123b5c02019-03-05 14:01:00 -080051
Willy Tu13224372024-07-15 17:00:15 +000052 constexpr int device = 0;
53 const std::array<std::string, 3> formats = {"/dev/ipmi", "/dev/ipmi/",
54 "/dev/ipmidev/"};
55 for (const std::string& format : formats)
Patrick Venture123b5c02019-03-05 14:01:00 -080056 {
57 std::ostringstream path;
58 path << format << device;
59
60 fd = sys->open(path.str().c_str(), O_RDWR);
61 if (fd < 0)
62 {
63 continue;
64 }
65 break;
66 }
67
68 if (fd < 0)
69 {
70 throw IpmiException("Unable to open any ipmi devices");
71 }
72}
73
74std::vector<std::uint8_t>
Patrick Venture958f1ce2019-05-31 17:07:25 -070075 IpmiHandler::sendPacket(std::uint8_t netfn, std::uint8_t cmd,
76 std::vector<std::uint8_t>& data)
Patrick Venture123b5c02019-03-05 14:01:00 -080077{
Willy Tu13224372024-07-15 17:00:15 +000078 open();
Patrick Venture123b5c02019-03-05 14:01:00 -080079
Patrick Venture123b5c02019-03-05 14:01:00 -080080 constexpr int ipmiOEMLun = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080081 constexpr int fifteenMs = 15 * 1000;
82 constexpr int ipmiReadTimeout = fifteenMs;
83 constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
84 constexpr int ipmiOk = 0;
85
86 /* We have a handle to the IPMI device. */
Patrick Venture570fcc62020-06-26 13:56:24 -070087 std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer = {};
Patrick Venture123b5c02019-03-05 14:01:00 -080088
89 /* Build address. */
Patrick Venture48125b52020-06-24 08:46:20 -070090 ipmi_system_interface_addr systemAddress{};
Patrick Venture123b5c02019-03-05 14:01:00 -080091 systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
92 systemAddress.channel = IPMI_BMC_CHANNEL;
93 systemAddress.lun = ipmiOEMLun;
94
95 /* Build request. */
Patrick Venture48125b52020-06-24 08:46:20 -070096 ipmi_req request{};
Patrick Venture123b5c02019-03-05 14:01:00 -080097 request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
98 request.addr_len = sizeof(systemAddress);
Anh Phan626bf762024-07-12 11:51:02 +000099 request.msgid = sequence.fetch_add(1, std::memory_order_relaxed);
Patrick Venture123b5c02019-03-05 14:01:00 -0800100 request.msg.data = reinterpret_cast<unsigned char*>(data.data());
101 request.msg.data_len = data.size();
Patrick Venture958f1ce2019-05-31 17:07:25 -0700102 request.msg.netfn = netfn;
103 request.msg.cmd = cmd;
Patrick Venture123b5c02019-03-05 14:01:00 -0800104
Patrick Venture48125b52020-06-24 08:46:20 -0700105 ipmi_recv reply{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800106 reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
107 reply.addr_len = sizeof(systemAddress);
108 reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
109 reply.msg.data_len = responseBuffer.size();
110
111 /* Try to send request. */
112 int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
113 if (rc < 0)
114 {
115 throw IpmiException("Unable to send IPMI request.");
116 }
117
118 /* Could use sdeventplus, but for only one type of event is it worth it? */
Patrick Venture48125b52020-06-24 08:46:20 -0700119 pollfd pfd{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800120 pfd.fd = fd;
121 pfd.events = POLLIN;
122
123 do
124 {
125 rc = sys->poll(&pfd, 1, ipmiReadTimeout);
126 if (rc < 0)
127 {
128 if (errno == EINTR)
129 {
130 continue;
131 }
Willy Tu13224372024-07-15 17:00:15 +0000132 throw IpmiException("Polling Error occurred.");
Patrick Venture123b5c02019-03-05 14:01:00 -0800133 }
134 else if (rc == 0)
135 {
136 throw IpmiException("Timeout waiting for reply.");
137 }
138
139 /* Yay, happy case! */
140 rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
141 if (rc < 0)
142 {
143 throw IpmiException("Unable to read reply.");
144 }
145
146 if (request.msgid != reply.msgid)
147 {
148 std::fprintf(stderr, "Received wrong message, trying again.\n");
149 }
150 } while (request.msgid != reply.msgid);
151
152 if (responseBuffer[0] != ipmiOk)
153 {
154 throw IpmiException(static_cast<int>(responseBuffer[0]));
155 }
156
157 std::vector<std::uint8_t> returning;
158 auto dataLen = reply.msg.data_len - 1;
159
160 returning.insert(returning.begin(), responseBuffer.begin() + 1,
161 responseBuffer.begin() + dataLen + 1);
162
Patrick Venture123b5c02019-03-05 14:01:00 -0800163 return returning;
164}
165
Patrick Venture1470bec2019-03-06 07:33:12 -0800166} // namespace ipmiblob