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> |
| 27 | #include <cstdint> |
| 28 | #include <cstring> |
Patrick Venture | 2fe4c65 | 2019-05-13 07:37:33 -0700 | [diff] [blame] | 29 | #include <memory> |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 30 | #include <sstream> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 34 | namespace ipmiblob |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 35 | { |
| 36 | |
Patrick Venture | 2fe4c65 | 2019-05-13 07:37:33 -0700 | [diff] [blame] | 37 | std::unique_ptr<IpmiInterface> IpmiHandler::CreateIpmiHandler() |
| 38 | { |
| 39 | return std::make_unique<IpmiHandler>(); |
| 40 | } |
| 41 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 42 | void IpmiHandler::open() |
| 43 | { |
| 44 | const int device = 0; |
| 45 | const std::vector<std::string> formats = {"/dev/ipmi", "/dev/ipmi/", |
| 46 | "/dev/ipmidev/"}; |
| 47 | |
| 48 | for (const auto& format : formats) |
| 49 | { |
| 50 | std::ostringstream path; |
| 51 | path << format << device; |
| 52 | |
| 53 | fd = sys->open(path.str().c_str(), O_RDWR); |
| 54 | if (fd < 0) |
| 55 | { |
| 56 | continue; |
| 57 | } |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | if (fd < 0) |
| 62 | { |
| 63 | throw IpmiException("Unable to open any ipmi devices"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | std::vector<std::uint8_t> |
Patrick Venture | 958f1ce | 2019-05-31 17:07:25 -0700 | [diff] [blame] | 68 | IpmiHandler::sendPacket(std::uint8_t netfn, std::uint8_t cmd, |
| 69 | std::vector<std::uint8_t>& data) |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 70 | { |
| 71 | if (fd < 0) |
| 72 | { |
| 73 | open(); |
| 74 | } |
| 75 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 76 | constexpr int ipmiOEMLun = 0; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 77 | constexpr int fifteenMs = 15 * 1000; |
| 78 | constexpr int ipmiReadTimeout = fifteenMs; |
| 79 | constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH; |
| 80 | constexpr int ipmiOk = 0; |
| 81 | |
| 82 | /* We have a handle to the IPMI device. */ |
Patrick Venture | 570fcc6 | 2020-06-26 13:56:24 -0700 | [diff] [blame] | 83 | std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer = {}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 84 | |
| 85 | /* Build address. */ |
Patrick Venture | 48125b5 | 2020-06-24 08:46:20 -0700 | [diff] [blame] | 86 | ipmi_system_interface_addr systemAddress{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 87 | systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 88 | systemAddress.channel = IPMI_BMC_CHANNEL; |
| 89 | systemAddress.lun = ipmiOEMLun; |
| 90 | |
| 91 | /* Build request. */ |
Patrick Venture | 48125b5 | 2020-06-24 08:46:20 -0700 | [diff] [blame] | 92 | ipmi_req request{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 93 | request.addr = reinterpret_cast<unsigned char*>(&systemAddress); |
| 94 | request.addr_len = sizeof(systemAddress); |
| 95 | request.msgid = sequence++; |
| 96 | request.msg.data = reinterpret_cast<unsigned char*>(data.data()); |
| 97 | request.msg.data_len = data.size(); |
Patrick Venture | 958f1ce | 2019-05-31 17:07:25 -0700 | [diff] [blame] | 98 | request.msg.netfn = netfn; |
| 99 | request.msg.cmd = cmd; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 100 | |
Patrick Venture | 48125b5 | 2020-06-24 08:46:20 -0700 | [diff] [blame] | 101 | ipmi_recv reply{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 102 | reply.addr = reinterpret_cast<unsigned char*>(&systemAddress); |
| 103 | reply.addr_len = sizeof(systemAddress); |
| 104 | reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data()); |
| 105 | reply.msg.data_len = responseBuffer.size(); |
| 106 | |
| 107 | /* Try to send request. */ |
| 108 | int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request); |
| 109 | if (rc < 0) |
| 110 | { |
| 111 | throw IpmiException("Unable to send IPMI request."); |
| 112 | } |
| 113 | |
| 114 | /* 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] | 115 | pollfd pfd{}; |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 116 | pfd.fd = fd; |
| 117 | pfd.events = POLLIN; |
| 118 | |
| 119 | do |
| 120 | { |
| 121 | rc = sys->poll(&pfd, 1, ipmiReadTimeout); |
| 122 | if (rc < 0) |
| 123 | { |
| 124 | if (errno == EINTR) |
| 125 | { |
| 126 | continue; |
| 127 | } |
| 128 | throw IpmiException("Error occurred."); |
| 129 | } |
| 130 | else if (rc == 0) |
| 131 | { |
| 132 | throw IpmiException("Timeout waiting for reply."); |
| 133 | } |
| 134 | |
| 135 | /* Yay, happy case! */ |
| 136 | rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply); |
| 137 | if (rc < 0) |
| 138 | { |
| 139 | throw IpmiException("Unable to read reply."); |
| 140 | } |
| 141 | |
| 142 | if (request.msgid != reply.msgid) |
| 143 | { |
| 144 | std::fprintf(stderr, "Received wrong message, trying again.\n"); |
| 145 | } |
| 146 | } while (request.msgid != reply.msgid); |
| 147 | |
| 148 | if (responseBuffer[0] != ipmiOk) |
| 149 | { |
| 150 | throw IpmiException(static_cast<int>(responseBuffer[0])); |
| 151 | } |
| 152 | |
| 153 | std::vector<std::uint8_t> returning; |
| 154 | auto dataLen = reply.msg.data_len - 1; |
| 155 | |
| 156 | returning.insert(returning.begin(), responseBuffer.begin() + 1, |
| 157 | responseBuffer.begin() + dataLen + 1); |
| 158 | |
Patrick Venture | 123b5c0 | 2019-03-05 14:01:00 -0800 | [diff] [blame] | 159 | return returning; |
| 160 | } |
| 161 | |
Patrick Venture | 1470bec | 2019-03-06 07:33:12 -0800 | [diff] [blame] | 162 | } // namespace ipmiblob |