aboutsummaryrefslogtreecommitdiff
path: root/bbot/machine.cxx
blob: 460e80251bc8c092640e19d5f92725133f274bd3 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// file      : bbot/machine.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : TBC; see accompanying LICENSE file

#include <bbot/machine>

#include <unistd.h> // sleep()

#include <sys/un.h>   // sockaddr_un
#include <sys/socket.h>

#include <cstdio>  // snprintf()
#include <cstring> // strcpy()

#include <bbot/agent>
#include <bbot/machine-manifest>

using namespace std;
using namespace butl;

namespace bbot
{
  // Forward TFTP requests (UDP/69) coming from the machine to the specified
  // port.
  //
  // This allows the machine to connect to any "unknown" IP (e.g., link-local
  // 196.254.111.222) port 69 and end up being redirected to out TFTP server.
  //
  static void
  iptables (tracer& t,
            const char* a,
            const string& tap,
            const string& br,
            uint16_t port)
  {
    run (t,
         "sudo",             "iptables",
         "-t",               "nat",
         a,                  "PREROUTING",
         "-p",               "udp",
         "-m",               "udp",
         "-m",               "physdev",
         "-i",               br,
         "--physdev-in",     tap,
         "--dport",          69,
         "-j",               "DNAT",
         "--to-destination", iface_addr (br) + ':' + to_string (port));
  }

  static string
  create_tap (const string& br, uint16_t port)
  {
    tracer trace ("create_tap");

    string t ("tap" + tc_num);

    // First try to delete it in case there is one from a previous run.
    //
    run_exit (trace, "sudo", "ip", "tuntap", "delete", t, "mode", "tap");

    run (trace, "sudo", "ip", "tuntap", "add", t, "mode", "tap", "user", uid);
    run (trace, "sudo", "ip", "link", "set", t, "up");
    run (trace, "sudo", "ip", "link", "set", t, "master", br);

    iptables (trace, "-A", t, br, port); // Add.

    return t;
  }

  static void
  destroy_tap (const string& t, const string& br, uint16_t port)
  {
    tracer trace ("destroy_tap");
    iptables (trace, "-D", t, br, port); // Delete.
    run (trace, "sudo", "ip", "tuntap", "delete", t, "mode", "tap");
  }

  static string
  generate_mac ()
  {
    // The last two bits of the first byte are special: bit 1 indicates a
    // multicast address (which we don't want) while bit 1 -- local assignment
    // (which we do want).
    //
    char r[6 * 2 + 5 + 1];
    snprintf (r, sizeof (r),
              "%02x:%02x:%02x:%02x:%02x:%02x",
              (genrand<uint8_t> () & 0xFE) | 0x02,
              genrand<uint8_t> (),
              genrand<uint8_t> (),
              genrand<uint8_t> (),
              genrand<uint8_t> (),
              genrand<uint8_t> ());
    return r;
  }

  class kvm_machine: public machine
  {
  public:
    kvm_machine (const dir_path&,
                 const machine_manifest&,
                 const optional<string>& mac,
                 const string& br_iface,
                 uint16_t tftp_port);

    virtual bool
    shutdown (size_t& seconds) override;

    virtual void
    forcedown () override;

    virtual void
    suspend () override;

    bool
    wait (size_t& seconds) override;

    using machine::wait;

    virtual void
    print_info (diag_record&) override;

  private:
    void
    monitor_command (const string&);

  private:
    path kvm;     // Hypervisor binary.

    string br;     // Bridge network interface.
    string tap;    // Tap network interface.
    uint16_t port; // TFTP port.

    string vnc;   // QEMU VNC TCP addr:port.
    path monitor; // QEMU monitor UNIX socket.
    process proc;
  };

  kvm_machine::
  kvm_machine (const dir_path& md,
               const machine_manifest& mm,
               const optional<string>& omac,
               const string& br,
               uint16_t port)
      : machine (mm.mac ? *mm.mac : // Fixed mac from machine manifest.
                 omac   ? *omac   : // Generated mac from previous bootstrap.
                 generate_mac ()),
        kvm ("kvm"),
        br (br),
        tap (create_tap (br, port)),
        port (port),
        vnc ("127.0.0.1:" + to_string (5900 + stoul (tc_num))),
        monitor ("/tmp/" + tc_name + "-monitor")
  {
    tracer trace ("kvm_machine");

    if (sizeof (sockaddr_un::sun_path) <= monitor.size ())
      throw invalid_argument ("monitor unix socket path too long");

    // Start the VM.
    //
    // Notes:
    //
    // 1. For now we let qemu calculate sockets/cores/threads from the
    //    total number of CPUs (i.e., threads).
    //
    // 2. echo system_powerdown | socat - UNIX-CONNECT:.../monitor
    //
    proc = run_io_start (
      trace,
      fdnull (),
      2,
      2,
      md, // Run from the machine's directory.
      kvm,
      "-boot", "c", // Boot from disk.
      "-no-reboot", // Exit on VM reboot.
      //
      // Machine.
      //
      "-m",   to_string (ops.ram () / 1024) + "M",
      "-cpu", "host",
      "-smp", ops.cpu (),
      //
      // Network.
      //
      "-device", "virtio-net-pci,netdev=net0,mac=" + mac,
      "-netdev", "tap,id=net0,script=no,ifname=" + tap,
      //
      // Disk.
      //
      "-device", "virtio-scsi-pci,id=scsi",
      "-device", "scsi-hd,drive=disk0",
      "-drive",  "if=none,id=disk0,format=raw,file=disk.img",
      //
      // VNC & monitor.
      //
      "-vnc", "127.0.0.1:" + tc_num, // 5900 + tc_num
      "-monitor", "unix:" + monitor.string () + ",server,nowait");
  }

  // Connect to the QEMU monitor via the UNIX socket and send system_reset.
  // You may be wondering why not system_powerdown? The reason is that while
  // not all OS know how to power-down the machine, pretty much all of them
  // can reboot. So combined with the -no-reboot option above, we get the
  // same result in a more robust way.
  //
  // Note that this setup has one side effect: if the VM decided to reboot,
  // say, during bootstrap, then we will interpret it as a shutdown. Current
  // thinking saying this is good since we don't want our VMs to reboot
  // uncontrollably for security and predictability reasons (e.g., we don't
  // want Windows to decide to install updates -- this stuff should all be
  // disabled during the VM preparation).
  //
  // Actually, this turned out not to be entirely accurate: reset appears to
  // be a "hard reset" while powerdown causes a clean shutdown. So we use
  // powerdown to implement shutdown() and reset/-no-reboot for implement
  // forcedown().
  //
  bool kvm_machine::
  shutdown (size_t& seconds)
  {
    monitor_command ("system_powerdown");

    // Wait for up to the specified number if seconds for the machine to
    // shutdown.
    //
    return wait (seconds);
  }

  void kvm_machine::
  forcedown ()
  {
    monitor_command ("system_reset");
    wait ();
  }

  void kvm_machine::
  suspend ()
  {
    monitor_command ("stop");
  }

  void kvm_machine::
  print_info (diag_record& dr)
  {
    dr << info << "qemu pid: " << proc.id ()
       << info << "qemu vnc: " << vnc
       << info << "qemu monitor: unix:" << monitor;
  }

  bool kvm_machine::
  wait (size_t& sec)
  try
  {
    tracer trace ("kvm_machine::wait");

    bool t;
    for (; !(t = proc.try_wait ()) && sec != 0; --sec)
      sleep (1);

    if (t)
    {
      run_io_finish (trace, proc, kvm);

      destroy_tap (tap, br, port);
      try_rmfile (monitor, true); // QEMU doesn't seem to remove it.
    }

    return t;
  }
  catch (const process_error& e)
  {
    fail << "unable to execute " << kvm << ": " << e << endf;
  }

  void kvm_machine::
  monitor_command (const string& c)
  try
  {
    sockaddr_un addr;
    addr.sun_family = AF_LOCAL;
    strcpy (addr.sun_path, monitor.string ().c_str ()); // Size check in ctor

    auto_fd sock (socket (AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0));

    if (sock.get () == -1)
      throw_system_error (errno);

    if (connect (sock.get (),
                 reinterpret_cast<sockaddr*> (&addr),
                 sizeof (addr)) == -1)
      throw_system_error (errno);

    // Read until we get something.
    //
    auto readsome = [&sock] ()
    {
      ifdstream ifs (move (sock),
                     fdstream_mode::non_blocking,
                     ostream::badbit);

      char buf[256];
      for (streamsize n (0), m (0);
           n == 0 || m != 0;
           m = ifs.readsome (buf, sizeof (buf) - 1))
      {
        if (m != 0)
        {
          n += m;

          //buf[m] = '\0';
          //text << buf;
        }
      }

      sock = ifs.release ();
    };

    // Read QEMU welcome.
    //
    readsome ();

    // Write our command.
    //
    {
      ofdstream ofs (move (sock), fdstream_mode::blocking);
      ofs << c << endl;
      sock = ofs.release ();
    }

    // Read QEMU reply (may hit eof).
    //
    readsome ();
  }
  catch (const system_error& e)
  {
    fail << "unable to communicate with qemu monitor: " << e;
  }

  unique_ptr<machine>
  start_machine (const dir_path& md,
                 const machine_manifest& mm,
                 const optional<string>& mac,
                 const string& br_iface,
                 uint16_t tftp_port)
  {
    switch (mm.type)
    {
    case machine_type::kvm:
      return make_unique<kvm_machine> (md, mm, mac, br_iface, tftp_port);
    case machine_type::nspawn:
      assert (false); //@@ TODO
    }

    return nullptr;
  }
}