Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 1 | #include <sys/socket.h> |
| 2 | #include <sys/un.h> |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 3 | #include <chrono> |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 4 | #include <cmath> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include "main.hpp" |
| 7 | #include "sol_context.hpp" |
| 8 | #include "sol_manager.hpp" |
| 9 | |
| 10 | namespace sol |
| 11 | { |
| 12 | |
| 13 | using namespace phosphor::logging; |
| 14 | |
Tom Joseph | b81f761 | 2017-04-25 17:59:02 +0530 | [diff] [blame^] | 15 | CustomFD::~CustomFD() |
| 16 | { |
| 17 | if(fd >= 0) |
| 18 | { |
| 19 | // Remove the host console descriptor from the sd_event_loop |
| 20 | std::get<eventloop::EventLoop&>(singletonPool).stopHostConsole(); |
| 21 | close(fd); |
| 22 | } |
| 23 | } |
| 24 | |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 25 | void Manager::initHostConsoleFd() |
| 26 | { |
| 27 | struct sockaddr_un addr; |
| 28 | int rc = 0; |
| 29 | int fd = 0; |
| 30 | |
| 31 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 32 | if (fd < 0) |
| 33 | { |
| 34 | log<level::ERR>("Failed to open the host console socket", |
| 35 | entry("ERRNO=%d", errno)); |
| 36 | throw std::runtime_error("Failed to open the host console socket"); |
| 37 | } |
| 38 | |
| 39 | memset(&addr, 0, sizeof(addr)); |
| 40 | addr.sun_family = AF_UNIX; |
| 41 | memcpy(&addr.sun_path, &CONSOLE_SOCKET_PATH, CONSOLE_SOCKET_PATH_LEN); |
| 42 | consoleFD = std::make_unique<CustomFD>(fd); |
| 43 | auto& conFD = *(consoleFD.get()); |
| 44 | |
| 45 | rc = connect(conFD(), (struct sockaddr *)&addr, sizeof(addr)); |
| 46 | if (rc < 0) |
| 47 | { |
| 48 | log<level::ERR>("Failed to connect to host console socket address", |
| 49 | entry("ERRNO=%d", errno)); |
| 50 | consoleFD.reset(); |
| 51 | throw std::runtime_error("Failed to connect to console server"); |
| 52 | } |
| 53 | } |
| 54 | |
Tom Joseph | b51f641 | 2017-03-14 18:08:14 +0530 | [diff] [blame] | 55 | int Manager::writeConsoleSocket(const Buffer& input) const |
| 56 | { |
| 57 | auto inBuffer = input.data(); |
| 58 | auto inBufferSize = input.size(); |
| 59 | size_t pos = 0; |
| 60 | ssize_t rc = 0; |
| 61 | int errVal = 0; |
| 62 | auto& conFD = *(consoleFD.get()); |
| 63 | |
| 64 | for (pos = 0; pos < inBufferSize; pos += rc) |
| 65 | { |
| 66 | rc = write(conFD(), inBuffer + pos, inBufferSize - pos); |
| 67 | if (rc <= 0) |
| 68 | { |
| 69 | if (errno == EINTR) |
| 70 | { |
| 71 | log<level::INFO>(" Retrying to handle EINTR", |
| 72 | entry("ERRNO=%d", errno)); |
| 73 | rc = 0; |
| 74 | continue; |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | errVal = errno; |
| 79 | log<level::ERR>("Failed to write to host console socket", |
| 80 | entry("ERRNO=%d", errno)); |
| 81 | return -errVal; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Tom Joseph | 2e44e0e | 2017-03-14 18:09:11 +0530 | [diff] [blame] | 89 | void Manager::startPayloadInstance(uint8_t payloadInstance, |
| 90 | session::SessionID sessionID) |
| 91 | { |
| 92 | if (payloadMap.empty()) |
| 93 | { |
| 94 | initHostConsoleFd(); |
| 95 | |
| 96 | // Register the fd in the sd_event_loop |
| 97 | std::get<eventloop::EventLoop&>(singletonPool).startHostConsole( |
| 98 | *(consoleFD.get())); |
| 99 | } |
| 100 | |
| 101 | // Create the SOL Context data for payload instance |
| 102 | auto context = std::make_unique<Context>( |
| 103 | accumulateInterval, retryCount, payloadInstance, sessionID); |
| 104 | |
| 105 | /* |
| 106 | * Start payload event instance |
| 107 | * |
| 108 | * Accumulate interval is in 5 ms(milli secs) increments, since |
| 109 | * sd_event_add_time takes in micro secs, it is converted to micro secs. |
| 110 | * The Retry interval is in 10 ms (milli secs) increments. |
| 111 | */ |
| 112 | using namespace std::chrono_literals; |
| 113 | |
| 114 | auto accInterval = 5ms * accumulateInterval; |
| 115 | auto retryInterval = 10ms * retryThreshold; |
| 116 | |
| 117 | std::get<eventloop::EventLoop&>(singletonPool).startSOLPayloadInstance( |
| 118 | payloadInstance, |
| 119 | std::chrono::duration_cast<eventloop::IntervalType>(accInterval), |
| 120 | std::chrono::duration_cast<eventloop::IntervalType>(retryInterval)); |
| 121 | |
| 122 | payloadMap.emplace(payloadInstance, std::move(context)); |
| 123 | } |
| 124 | |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 125 | void Manager::stopPayloadInstance(uint8_t payloadInstance) |
| 126 | { |
| 127 | auto iter = payloadMap.find(payloadInstance); |
| 128 | if (iter == payloadMap.end()) |
| 129 | { |
| 130 | throw std::runtime_error("SOL Payload instance not found "); |
| 131 | } |
| 132 | |
| 133 | payloadMap.erase(iter); |
| 134 | |
| 135 | std::get<eventloop::EventLoop&>(singletonPool).stopSOLPayloadInstance( |
| 136 | payloadInstance); |
| 137 | |
| 138 | if (payloadMap.empty()) |
| 139 | { |
Tom Joseph | 4ff14b5 | 2017-03-14 18:10:52 +0530 | [diff] [blame] | 140 | consoleFD.reset(); |
| 141 | } |
| 142 | } |
| 143 | |
Tom Joseph | 22c5ad3 | 2017-03-14 18:04:22 +0530 | [diff] [blame] | 144 | } // namespace sol |