Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 1 | /* |
| 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 Tu | d942a65 | 2024-07-15 16:54:06 +0000 | [diff] [blame] | 27 | #include <atomic> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 28 | #include <cstdint> |
| 29 | #include <cstring> |
Willy Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame^] | 30 | #include <functional> |
Patrick Venture | 2fe4c65 | 2019-05-13 07:37:33 -0700 | [diff] [blame] | 31 | #include <memory> |
Willy Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame^] | 32 | #include <mutex> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 33 | #include <sstream> |
| 34 | #include <string> |
| 35 | #include <vector> |
| 36 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 37 | namespace ipmiblob |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 38 | { |
| 39 | |
Patrick Venture | 2fe4c65 | 2019-05-13 07:37:33 -0700 | [diff] [blame] | 40 | std::unique_ptr<IpmiInterface> IpmiHandler::CreateIpmiHandler() |
| 41 | { |
Patrick Venture | 92a4a62 | 2021-02-04 10:45:00 -0800 | [diff] [blame] | 42 | return std::make_unique<IpmiHandler>(std::make_unique<internal::SysImpl>()); |
Patrick Venture | 2fe4c65 | 2019-05-13 07:37:33 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 45 | void IpmiHandler::open() |
| 46 | { |
Willy Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame^] | 47 | std::lock_guard<std::mutex> guard(openMutex); |
| 48 | if (fd >= 0) |
| 49 | { |
| 50 | return; |
| 51 | } |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 52 | |
Willy Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame^] | 53 | 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 Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 57 | { |
| 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 | |
| 75 | std::vector<std::uint8_t> |
Patrick Venture | 958f1ce | 2019-05-31 17:07:25 -0700 | [diff] [blame] | 76 | IpmiHandler::sendPacket(std::uint8_t netfn, std::uint8_t cmd, |
| 77 | std::vector<std::uint8_t>& data) |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 78 | { |
Willy Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame^] | 79 | open(); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 80 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 81 | constexpr int ipmiOEMLun = 0; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 82 | 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 Venture | 570fcc6 | 2020-06-26 13:56:24 -0700 | [diff] [blame] | 88 | std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer = {}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 89 | |
| 90 | /* Build address. */ |
Patrick Venture | 48125b5 | 2020-06-24 08:46:20 -0700 | [diff] [blame] | 91 | ipmi_system_interface_addr systemAddress{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 92 | systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 93 | systemAddress.channel = IPMI_BMC_CHANNEL; |
| 94 | systemAddress.lun = ipmiOEMLun; |
| 95 | |
| 96 | /* Build request. */ |
Patrick Venture | 48125b5 | 2020-06-24 08:46:20 -0700 | [diff] [blame] | 97 | ipmi_req request{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 98 | request.addr = reinterpret_cast<unsigned char*>(&systemAddress); |
| 99 | request.addr_len = sizeof(systemAddress); |
Anh Phan | 626bf76 | 2024-07-12 11:51:02 +0000 | [diff] [blame] | 100 | request.msgid = sequence.fetch_add(1, std::memory_order_relaxed); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 101 | request.msg.data = reinterpret_cast<unsigned char*>(data.data()); |
| 102 | request.msg.data_len = data.size(); |
Patrick Venture | 958f1ce | 2019-05-31 17:07:25 -0700 | [diff] [blame] | 103 | request.msg.netfn = netfn; |
| 104 | request.msg.cmd = cmd; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 105 | |
Patrick Venture | 48125b5 | 2020-06-24 08:46:20 -0700 | [diff] [blame] | 106 | ipmi_recv reply{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 107 | 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 Venture | 48125b5 | 2020-06-24 08:46:20 -0700 | [diff] [blame] | 120 | pollfd pfd{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 121 | 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 Tu | 1322437 | 2024-07-15 17:00:15 +0000 | [diff] [blame^] | 133 | throw IpmiException("Polling Error occurred."); |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 134 | } |
| 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 Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 164 | return returning; |
| 165 | } |
| 166 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 167 | } // namespace ipmiblob |