Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 "helper.hpp" |
| 18 | |
| 19 | #include "status.hpp" |
| 20 | #include "tool_errors.hpp" |
| 21 | |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 22 | #include <ipmiblob/blob_errors.hpp> |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 23 | #include <ipmiblob/blob_interface.hpp> |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 24 | |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 26 | #include <chrono> |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 27 | #include <optional> |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 28 | #include <thread> |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 29 | #include <utility> |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 30 | |
| 31 | namespace host_tool |
| 32 | { |
| 33 | |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 34 | template <typename Check> |
| 35 | static auto pollStat(std::uint16_t session, ipmiblob::BlobInterface* blob, |
| 36 | Check&& check) |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 37 | { |
| 38 | using namespace std::chrono_literals; |
| 39 | |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 40 | constexpr auto maxSleep = 1s; |
| 41 | constexpr auto printInterval = 30s; |
| 42 | constexpr auto timeout = 30min; |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 43 | |
| 44 | try |
| 45 | { |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 46 | auto start = std::chrono::steady_clock::now(); |
| 47 | auto last_print = start; |
| 48 | auto last_check = start; |
| 49 | auto check_interval = 50ms; |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 50 | |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 51 | while (true) |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 52 | { |
| 53 | ipmiblob::StatResponse resp = blob->getStat(session); |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 54 | auto ret = check(resp); |
| 55 | if (ret.has_value()) |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 56 | { |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 57 | std::fprintf(stderr, "success\n"); |
| 58 | return std::move(*ret); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 59 | } |
| 60 | |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 61 | auto cur = std::chrono::steady_clock::now(); |
| 62 | if (cur - last_print >= printInterval) |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 63 | { |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 64 | std::fprintf(stderr, "running\n"); |
| 65 | last_print = cur; |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 66 | } |
| 67 | |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 68 | auto sleep = check_interval - (cur - last_check); |
| 69 | last_check = cur; |
| 70 | // Check that we don't timeout immediately after sleeping |
| 71 | // to avoid an extra sleep without checking |
| 72 | if (cur - start > timeout - sleep) |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 73 | { |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 74 | throw ToolException("Stat check timed out"); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 75 | } |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 76 | check_interval = std::min<decltype(check_interval)>( |
| 77 | check_interval * 2, maxSleep); |
| 78 | std::this_thread::sleep_for(sleep); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | catch (const ipmiblob::BlobException& b) |
| 82 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 83 | throw ToolException( |
| 84 | "blob exception received: " + std::string(b.what())); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 85 | } |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 86 | } |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 87 | |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 88 | /* Poll an open verification session. Handling closing the session is not yet |
| 89 | * owned by this method. |
| 90 | */ |
| 91 | void pollStatus(std::uint16_t session, ipmiblob::BlobInterface* blob) |
| 92 | { |
| 93 | pollStat(session, blob, |
| 94 | [](const ipmiblob::StatResponse& resp) -> std::optional<bool> { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 95 | if (resp.metadata.size() != 1) |
| 96 | { |
| 97 | throw ToolException("Invalid stat metadata"); |
| 98 | } |
| 99 | auto result = |
| 100 | static_cast<ipmi_flash::ActionStatus>(resp.metadata[0]); |
| 101 | switch (result) |
| 102 | { |
| 103 | case ipmi_flash::ActionStatus::failed: |
| 104 | throw ToolException("BMC reported failure"); |
| 105 | case ipmi_flash::ActionStatus::unknown: |
| 106 | case ipmi_flash::ActionStatus::running: |
| 107 | return std::nullopt; |
| 108 | case ipmi_flash::ActionStatus::success: |
| 109 | return true; |
| 110 | default: |
| 111 | throw ToolException("Unrecognized action status"); |
| 112 | } |
| 113 | }); |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 116 | /* Poll an open blob session for reading. |
| 117 | * |
| 118 | * The committing bit indicates that the blob is not available for reading now |
| 119 | * and the reader might come back and check the state later. |
| 120 | * |
| 121 | * Polling finishes under the following conditions: |
| 122 | * - The open_read bit set -> stat successful |
| 123 | * - The open_read and committing bits unset -> stat failed; |
| 124 | * - Blob exception was received; |
| 125 | * - Time ran out. |
| 126 | * Polling continues when the open_read bit unset and committing bit set. |
| 127 | * If the blob is not open_read and not committing, then it is an error to the |
| 128 | * reader. |
| 129 | */ |
William A. Kennington III | f88bcf3 | 2021-10-14 02:15:10 -0700 | [diff] [blame] | 130 | uint32_t pollReadReady(std::uint16_t session, ipmiblob::BlobInterface* blob) |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 131 | { |
William A. Kennington III | cf37663 | 2021-10-13 21:40:11 -0700 | [diff] [blame] | 132 | return pollStat( |
| 133 | session, blob, |
| 134 | [](const ipmiblob::StatResponse& resp) -> std::optional<uint32_t> { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 135 | if (resp.blob_state & ipmiblob::StateFlags::open_read) |
| 136 | { |
| 137 | return resp.size; |
| 138 | } |
| 139 | if (resp.blob_state & ipmiblob::StateFlags::committing) |
| 140 | { |
| 141 | return std::nullopt; |
| 142 | } |
| 143 | throw ToolException("BMC blob failed to become ready"); |
| 144 | }); |
Jie Yang | 328f520 | 2021-03-16 00:52:07 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Vivekanand Veeracholan | c7fa2c2 | 2021-02-18 18:05:41 -0800 | [diff] [blame] | 147 | void* memcpyAligned(void* destination, const void* source, std::size_t size) |
| 148 | { |
| 149 | std::size_t i = 0; |
| 150 | std::size_t bytesCopied = 0; |
| 151 | |
Willy Tu | fd16f6d | 2021-09-16 21:31:58 -0700 | [diff] [blame] | 152 | if (!(reinterpret_cast<std::uintptr_t>(destination) % |
| 153 | sizeof(std::uint64_t)) && |
| 154 | !(reinterpret_cast<std::uintptr_t>(source) % sizeof(std::uint64_t))) |
Vivekanand Veeracholan | c7fa2c2 | 2021-02-18 18:05:41 -0800 | [diff] [blame] | 155 | { |
| 156 | auto src64 = reinterpret_cast<const volatile std::uint64_t*>(source); |
| 157 | auto dest64 = reinterpret_cast<volatile std::uint64_t*>(destination); |
| 158 | |
| 159 | for (i = 0; i < size / sizeof(std::uint64_t); i++) |
| 160 | { |
| 161 | *dest64++ = *src64++; |
| 162 | bytesCopied += sizeof(std::uint64_t); |
| 163 | } |
| 164 | } |
| 165 | |
Patrick Williams | 1038836 | 2023-05-10 07:51:09 -0500 | [diff] [blame] | 166 | auto srcMem8 = reinterpret_cast<const volatile std::uint8_t*>(source) + |
| 167 | bytesCopied; |
| 168 | auto destMem8 = reinterpret_cast<volatile std::uint8_t*>(destination) + |
| 169 | bytesCopied; |
Vivekanand Veeracholan | c7fa2c2 | 2021-02-18 18:05:41 -0800 | [diff] [blame] | 170 | |
| 171 | for (i = bytesCopied; i < size; i++) |
| 172 | { |
| 173 | *destMem8++ = *srcMem8++; |
| 174 | } |
| 175 | |
| 176 | return destination; |
| 177 | } |
| 178 | |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 179 | } // namespace host_tool |