blob: ef9a226783d7f145c1c88427ddcc9878aa3e224c [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
Patrick Williamsb80a0252024-08-16 15:21:31 -040074std::vector<std::uint8_t> IpmiHandler::sendPacket(
75 std::uint8_t netfn, std::uint8_t cmd, std::vector<std::uint8_t>& data)
Patrick Venture123b5c02019-03-05 14:01:00 -080076{
Willy Tu13224372024-07-15 17:00:15 +000077 open();
Patrick Venture123b5c02019-03-05 14:01:00 -080078
Patrick Venture123b5c02019-03-05 14:01:00 -080079 constexpr int ipmiOEMLun = 0;
Patrick Venture123b5c02019-03-05 14:01:00 -080080 constexpr int fifteenMs = 15 * 1000;
81 constexpr int ipmiReadTimeout = fifteenMs;
82 constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
83 constexpr int ipmiOk = 0;
84
85 /* We have a handle to the IPMI device. */
Patrick Venture570fcc62020-06-26 13:56:24 -070086 std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer = {};
Patrick Venture123b5c02019-03-05 14:01:00 -080087
88 /* Build address. */
Patrick Venture48125b52020-06-24 08:46:20 -070089 ipmi_system_interface_addr systemAddress{};
Patrick Venture123b5c02019-03-05 14:01:00 -080090 systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
91 systemAddress.channel = IPMI_BMC_CHANNEL;
92 systemAddress.lun = ipmiOEMLun;
93
94 /* Build request. */
Patrick Venture48125b52020-06-24 08:46:20 -070095 ipmi_req request{};
Patrick Venture123b5c02019-03-05 14:01:00 -080096 request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
97 request.addr_len = sizeof(systemAddress);
Anh Phan626bf762024-07-12 11:51:02 +000098 request.msgid = sequence.fetch_add(1, std::memory_order_relaxed);
Patrick Venture123b5c02019-03-05 14:01:00 -080099 request.msg.data = reinterpret_cast<unsigned char*>(data.data());
100 request.msg.data_len = data.size();
Patrick Venture958f1ce2019-05-31 17:07:25 -0700101 request.msg.netfn = netfn;
102 request.msg.cmd = cmd;
Patrick Venture123b5c02019-03-05 14:01:00 -0800103
Patrick Venture48125b52020-06-24 08:46:20 -0700104 ipmi_recv reply{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800105 reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
106 reply.addr_len = sizeof(systemAddress);
107 reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
108 reply.msg.data_len = responseBuffer.size();
109
110 /* Try to send request. */
111 int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
112 if (rc < 0)
113 {
114 throw IpmiException("Unable to send IPMI request.");
115 }
116
117 /* Could use sdeventplus, but for only one type of event is it worth it? */
Patrick Venture48125b52020-06-24 08:46:20 -0700118 pollfd pfd{};
Patrick Venture123b5c02019-03-05 14:01:00 -0800119 pfd.fd = fd;
120 pfd.events = POLLIN;
121
122 do
123 {
124 rc = sys->poll(&pfd, 1, ipmiReadTimeout);
125 if (rc < 0)
126 {
127 if (errno == EINTR)
128 {
129 continue;
130 }
Willy Tu13224372024-07-15 17:00:15 +0000131 throw IpmiException("Polling Error occurred.");
Patrick Venture123b5c02019-03-05 14:01:00 -0800132 }
133 else if (rc == 0)
134 {
135 throw IpmiException("Timeout waiting for reply.");
136 }
137
138 /* Yay, happy case! */
139 rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
140 if (rc < 0)
141 {
142 throw IpmiException("Unable to read reply.");
143 }
144
145 if (request.msgid != reply.msgid)
146 {
147 std::fprintf(stderr, "Received wrong message, trying again.\n");
148 }
149 } while (request.msgid != reply.msgid);
150
151 if (responseBuffer[0] != ipmiOk)
152 {
153 throw IpmiException(static_cast<int>(responseBuffer[0]));
154 }
155
156 std::vector<std::uint8_t> returning;
157 auto dataLen = reply.msg.data_len - 1;
158
159 returning.insert(returning.begin(), responseBuffer.begin() + 1,
160 responseBuffer.begin() + dataLen + 1);
161
Patrick Venture123b5c02019-03-05 14:01:00 -0800162 return returning;
163}
164
Patrick Venture1470bec2019-03-06 07:33:12 -0800165} // namespace ipmiblob