summaryrefslogtreecommitdiff
path: root/include/dump_offload.hpp
blob: 0c470b473e15062e6133b7e1f82b113c598627e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#pragma once

#include <signal.h>
#include <sys/select.h>

#include <boost/beast/core/flat_static_buffer.hpp>
#include <boost/beast/http.hpp>
#include <boost/process.hpp>
#include <cstdio>
#include <cstdlib>

namespace crow
{
namespace obmc_dump
{

inline void handleDumpOffloadUrl(const crow::Request& req, crow::Response& res,
                                 const std::string& entryId);
inline void resetHandler();

// The max network block device buffer size is 128kb plus 16bytes
// for the message header
static constexpr auto nbdBufferSize = 131088;

/** class Handler
 *  handles data transfer between nbd-client and nbd-server.
 *  This handler invokes nbd-proxy and reads data from socket
 *  and writes on to nbd-client and vice-versa
 */
class Handler : public std::enable_shared_from_this<Handler>
{
  public:
    Handler(const std::string& media, boost::asio::io_context& ios,
            const std::string& entryID) :
        pipeOut(ios),
        pipeIn(ios), media(media), entryID(entryID), doingWrite(false),
        negotiationDone(false), writeonnbd(false),
        outputBuffer(std::make_unique<
                     boost::beast::flat_static_buffer<nbdBufferSize>>()),
        inputBuffer(
            std::make_unique<boost::beast::flat_static_buffer<nbdBufferSize>>())
    {
    }

    ~Handler()
    {
    }

    /**
     * @brief  Invokes InitiateOffload method of dump manager which
     *         directs pldm to start writing on the nbd device.
     *
     * @return void
     */
    void initiateOffloadOnNbdDevice()
    {
        crow::connections::systemBus->async_method_call(
            [this,
             self(shared_from_this())](const boost::system::error_code ec) {
                if (ec)
                {
                    BMCWEB_LOG_ERROR << "DBUS response error: " << ec;
                    resetBuffers();
                    resetHandler();
                    return;
                }
            },
            "xyz.openbmc_project.Dump.Manager",
            "/xyz/openbmc_project/dump/entry/" + entryID,
            "xyz.openbmc_project.Dump.Entry", "InitiateOffload", "/dev/nbd1");
    }

    /**
     * @brief  Kills nbd-proxy
     *
     * @return void
     */
    void doClose()
    {
        int rc = kill(proxy.id(), SIGTERM);
        if (rc)
        {
            return;
        }
        proxy.wait();
    }

    /**
     * @brief  Starts nbd-proxy
     *
     * @return void
     */
    void connect()
    {
        std::error_code ec;
        proxy = boost::process::child("/usr/sbin/nbd-proxy", media,
                                      boost::process::std_out > pipeOut,
                                      boost::process::std_in < pipeIn, ec);
        if (ec)
        {
            BMCWEB_LOG_ERROR << "Couldn't connect to nbd-proxy: "
                             << ec.message();
            resetHandler();
            return;
        }
        doRead();
    }

    /**
     * @brief  Wait for data on tcp socket from nbd-server.
     *
     * @return void
     */
    void waitForMessageOnSocket()
    {

        std::size_t bytes = inputBuffer->capacity() - inputBuffer->size();

        (*stream).async_read_some(
            inputBuffer->prepare(bytes),
            [this,
             self(shared_from_this())](const boost::system::error_code& ec,
                                       std::size_t bytes_transferred) {
                if (ec)
                {
                    BMCWEB_LOG_DEBUG << "Error while reading on socket";
                    doClose();
                    resetBuffers();
                    resetHandler();
                    return;
                }

                inputBuffer->commit(bytes_transferred);
                doWrite();
            });
    }

    /**
     * @brief  Writes data on input pipe of nbd-client.
     *
     * @return void
     */
    void doWrite()
    {

        if (doingWrite)
        {
            BMCWEB_LOG_DEBUG << "Already writing.  Bailing out";
            return;
        }

        if (inputBuffer->size() == 0)
        {
            BMCWEB_LOG_DEBUG << "inputBuffer empty.  Bailing out";
            return;
        }

        doingWrite = true;
        boost::asio::async_write(
            pipeIn, inputBuffer->data(),
            [this, self(shared_from_this())](const boost::beast::error_code& ec,
                                             std::size_t bytesWritten) {
                if (ec)
                {
                    BMCWEB_LOG_DEBUG << "VM socket port closed";
                    doClose();
                    resetBuffers();
                    resetHandler();
                    return;
                }

                doingWrite = false;

                if (negotiationDone == false)
                {
                    // "gDf" is NBD reply magic
                    std::string reply_magic("gDf");
                    std::string reply_string(
                        static_cast<char*>(inputBuffer->data().data()),
                        bytesWritten);
                    std::size_t found = reply_string.find(reply_magic);
                    if (found != std::string::npos)
                    {
                        negotiationDone = true;
                        writeonnbd = true;
                    }
                }

                inputBuffer->consume(bytesWritten);
                waitForMessageOnSocket();
                if (writeonnbd)
                {
                    // NBD Negotiation Complete!!!!. Notify Dump manager to
                    // start dumping the actual data over NBD device
                    initiateOffloadOnNbdDevice();
                    writeonnbd = false;
                }
            });
    }

    /**
     * @brief  Reads data on output pipe of nbd-client and write on
     *         tcp socket.
     *
     * @return void
     */
    void doRead()
    {
        std::size_t bytes = outputBuffer->capacity() - outputBuffer->size();

        pipeOut.async_read_some(
            outputBuffer->prepare(bytes),
            [this, self(shared_from_this())](
                const boost::system::error_code& ec, std::size_t bytesRead) {
                if (ec)
                {
                    BMCWEB_LOG_ERROR << "Couldn't read from VM port: " << ec;
                    doClose();
                    resetBuffers();
                    resetHandler();
                    return;
                }

                outputBuffer->commit(bytesRead);

                boost::asio::async_write(
                    *stream, outputBuffer->data(),
                    [this](const boost::system::error_code& ec,
                           std::size_t bytes_transferred) {
                        if (ec)
                        {
                            BMCWEB_LOG_DEBUG << "Error while writing on socket";
                            doClose();
                            resetBuffers();
                            resetHandler();
                            return;
                        }

                        outputBuffer->consume(bytes_transferred);
                        doRead();
                    });
            });
    }

    /**
     * @brief  Resets input and output buffers.
     * @return void
     */
    void resetBuffers()
    {
#if BOOST_VERSION >= 107000
        this->inputBuffer->clear();
        this->outputBuffer->clear();
#else
        this->inputBuffer->reset();
        this->outputBuffer->reset();
#endif
    }

    boost::process::async_pipe pipeOut;
    boost::process::async_pipe pipeIn;
    boost::process::child proxy;
    std::string media;
    std::string entryID;
    bool doingWrite;
    bool negotiationDone;
    bool writeonnbd;
    std::unique_ptr<boost::beast::flat_static_buffer<nbdBufferSize>>
        outputBuffer;
    std::unique_ptr<boost::beast::flat_static_buffer<nbdBufferSize>>
        inputBuffer;
    std::shared_ptr<crow::Request::Adaptor> stream;
};

static std::shared_ptr<Handler> handler;
inline void resetHandler()
{

    handler.reset();
}
inline void handleDumpOffloadUrl(const crow::Request& req, crow::Response& res,
                                 const std::string& entryId)
{

    // Run only one instance of Handler, one dump offload can happen at a time
    if (handler != nullptr)
    {
        BMCWEB_LOG_ERROR << "Handler already running";
        res.result(boost::beast::http::status::service_unavailable);
        res.jsonValue["Description"] = "Service is already being used";
        res.end();
        return;
    }

    const char* media = "1";
    boost::asio::io_context* io_con = req.ioService;

    handler = std::make_shared<Handler>(media, *io_con, entryId);
    handler->stream =
        std::make_shared<crow::Request::Adaptor>(std::move(req.socket()));
    handler->connect();
    handler->waitForMessageOnSocket();
}
} // namespace obmc_dump
} // namespace crow