aboutsummaryrefslogtreecommitdiff
path: root/libvtv/vtv_map.h
blob: 86fce4aec9246d8a72ace7516264ad6adb071b4e (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
/* Copyright (C) 2012-2024 Free Software Foundation, Inc.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */

#ifndef _VTV_MAP_H
#define _VTV_MAP_H 1

#include <string.h>

#ifdef __MINGW32__
#include <stdint.h>
#include "vtv_utils.h"
#else
#include <vtv_utils.h>
#endif

inline uint64_t
load8bytes (const void *p)
{
  uint64_t result;
  memcpy (&result, p, 8);
  return result;
}

/* Insert_only_hash_map maps keys to values.  The implementation is a
   basic hash table with open addressing.  The keys are not "owned" by
   the table; it only stores pointers to keys.  The key type is
   specified below (see insert_only_hash_map::key_type) and is,
   roughly speaking, a string of any length with the string length and
   a hash code stored at the front.  The code here does not compute
   any hash codes, but rather uses what's given.  */

template<typename T, typename Alloc>
class insert_only_hash_map
  {
    public:
      typedef size_t size_type;
      typedef T value_type;
      typedef Alloc alloc_type;
      enum { min_capacity = 4 };
#if HASHMAP_STATS
  enum { stats = true };
#else
  enum { stats = false };
#endif

  /* Keys are a byte string (up to 2^32 - 1 long) plus a uint32_t
     that's used as a hash code.  The latter can encode arbitrary
     information at the client's discretion, so, e.g., multiple keys
     that are the same string still "differ" if the hash codes differ.
     Keys are equal if the first 8 bytes are equal and the next n
     bytes are equal.  */
  struct key_type
  {
    uint32_t n;
    uint32_t hash;
    char bytes[0];

    bool
    equals (const key_type *k) const;
  };

  /* Create an empty map with a reasonable number of buckets for the
     expected size.  Returns NULL if the allocator fails.  */

  static insert_only_hash_map *
  create (size_type expected_size);

  /* The opposite of create().  Free the memory for the given map.  */

  static void
  destroy (insert_only_hash_map *m)
  { Alloc().dealloc (m, m->size_in_bytes_); }

  /* Return a map identical to this except that *k is mapped to v.
     Typcially it's done by modifying this in place, but if a resize
     is necessary then this is deallocated and a new map is returned.
     Requires k to be non-NULL.  Does nothing and returns NULL if the
     allocator fails.  */

  insert_only_hash_map*
  put (const key_type *k, const value_type &v)
  { return this->put_internal (k, v, false); }

  /* If *k is a key in this then set *v to point to the corresponding
     value.  Otherwise, do the equivalent of insert(k, value_type())
     and, if that succeeds, set *v to point to the inserted value.
     Requires k to be non-NULL.  Does nothing and returns NULL if the
     allocator fails.  Typically returns this, but will return a new
     insert_only_hash_map if a resize occurs.  If the return value is
     non-NULL, *v is set and it's valid until a resize of the map that
     is the return value.  */

  insert_only_hash_map *
  find_or_add_key (const key_type *k, value_type **v);

  /* Get the value corresponding to *k.  Returns NULL if there is
     none.  Requires k to be non-NULL.  The return value is valid
     until any resize.  */
  const value_type *get (const key_type *k) const;

  size_type
  size () const
  { return num_entries_; }

  bool
  empty () const
  { return this->size () == 0; }

  size_type
  bucket_count () const
  { return num_buckets_; }

 private:
  typedef std::pair <const key_type *, value_type> bucket_type;

  insert_only_hash_map *put_internal (const key_type *, const value_type &,
				      bool);

  /* This function determines when to resize the table.  */
  bool
  is_too_full (size_type entries) const
  { return entries > (this->bucket_count () * 0.7); }

  /* Return a copy with double the number of buckets.  Returns NULL if
     the allocator fails.  Otherwise, calls destroy (this).  */
  insert_only_hash_map *destructive_copy ();

 /* Must be a power of 2 not less than min_capacity. */
  size_type num_buckets_; 
  size_type num_entries_;
  size_type size_in_bytes_;
  bucket_type buckets[0];  /* Actual array size is num_buckets.  */
};

template <typename T, typename Alloc>
insert_only_hash_map <T, Alloc> *
insert_only_hash_map <T, Alloc>::create (size_type expected_size)
{
  size_t cap = min_capacity;
  while (expected_size >= cap)
    {
      cap *= 2;
    }
  size_t size_in_bytes = sizeof (insert_only_hash_map <T, Alloc>)
                                                  + cap * sizeof (bucket_type);
  insert_only_hash_map <T, Alloc>* result =
      static_cast <insert_only_hash_map <T, Alloc>*> (Alloc ()
                                                       .alloc (size_in_bytes));
  if (result != NULL)
    {
      result->size_in_bytes_ = size_in_bytes;
      result->num_buckets_ = cap;
      result->num_entries_ = 0;
      memset (result->buckets, 0, cap * sizeof (bucket_type));
    }
  return result;
}

template <typename T, typename Alloc>
insert_only_hash_map <T, Alloc>*
insert_only_hash_map <T, Alloc>::destructive_copy ()
{
  insert_only_hash_map* copy = create (this->bucket_count ());
  if (copy == NULL)
    return NULL;
  VTV_DEBUG_ASSERT (copy->bucket_count () == 2 * this->bucket_count ());
  for (size_type i = 0; i < this->bucket_count (); i++)
    if (this->buckets[i].first != NULL)
      copy->put_internal (this->buckets[i].first, this->buckets[i].second,
			  true);
  VTV_DEBUG_ASSERT (copy->size () == this->size ());
  destroy (this);
  return copy;
}

template <typename T, typename Alloc>
insert_only_hash_map <T, Alloc>*
insert_only_hash_map <T, Alloc>::find_or_add_key (const key_type *k,
						  value_type **v)
{
  /* Table size is always a power of 2.  */
  const size_type mask = this->bucket_count () - 1;
  size_type bucket_index = k->hash & mask;
  size_type step = 1;
  for (;;)
    {
      bucket_type &bucket = this->buckets[bucket_index];
      if (bucket.first == NULL)
        {
          /* Key was not present. */
          if (this->is_too_full (this->size () + 1))
            {
              insert_only_hash_map <T, Alloc>* result =
		                                     this->destructive_copy ();
              return result == NULL
                  ? NULL
                  : result->find_or_add_key (k, v);
            }
          else
            {
              bucket.first = k;
              bucket.second = T ();
              this->num_entries_++;
              *v = &bucket.second;
              return this;
            }
        }
      else if (bucket.first->equals (k))
        {
          /* Key was present. */
          *v = &bucket.second;
          return this;
        }
      else
        bucket_index = (bucket_index + step++) & mask;
    }
}

template <typename T, typename Alloc>
insert_only_hash_map <T, Alloc>*
insert_only_hash_map <T, Alloc>::put_internal (
				     const insert_only_hash_map::key_type *k,
				     const insert_only_hash_map::value_type &v,
				     bool unique_key_and_resize_not_needed)
{
  /* Table size is always a power of 2.  */
  const size_type mask = this->bucket_count () - 1;
  size_type bucket_index = k->hash & mask;
  size_type step = 1;
  for (;;)
    {
      bucket_type &bucket = this->buckets[bucket_index];
      if (bucket.first == NULL)
        {
          /* Key was not present.  */
          if (!unique_key_and_resize_not_needed
              && this->is_too_full (this->size () + 1))
            {
              insert_only_hash_map <T, Alloc>* result =
                                                     this->destructive_copy ();
              return result == NULL
                  ? NULL
                  : result->put_internal (k, v, true);
            }
          else
            {
              bucket.first = k;
              bucket.second = v;
              this->num_entries_++;
              return this;
            }
        }
      else if (!unique_key_and_resize_not_needed && bucket.first->equals (k))
        {
          /* Key was present.  Just change the value.  */
          bucket.second = v;
          return this;
        }
      else
        bucket_index = (bucket_index + step++) & mask;
    }
}

template <typename T, typename Alloc>
inline const typename insert_only_hash_map <T, Alloc>::value_type*
insert_only_hash_map <T, Alloc>::get (const insert_only_hash_map::key_type *k)
                                                                          const
{
  /* Table size is always a power of 2.  */
  const size_type mask = this->bucket_count () - 1;
  size_type bucket_index = k->hash & mask;
  size_type step = 1;
  for (;;)
    {
      const bucket_type &bucket = this->buckets[bucket_index];
      if (bucket.first == NULL)
        return NULL;
      else if (bucket.first->equals (k))
        return &bucket.second;
      else
        bucket_index = (bucket_index + step++) & mask;
    }
}

template <typename T, typename Alloc>
inline bool
insert_only_hash_map <T, Alloc>::key_type::equals (
             const typename insert_only_hash_map <T, Alloc>::key_type *k) const
{
  const char* x = reinterpret_cast <const char *> (k);
  const char* y = reinterpret_cast <const char *> (this);
  return (load8bytes (x) == load8bytes (y)
          && memcmp (x + 8, y + 8, this->n) == 0);
}

#endif  /* _VTV_MAP_H  */
>host_vnet_hdr_len; memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); iovp = iov_copy; iovcnt++; } return tap_write_packet(s, iovp, iovcnt); } static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size) { struct iovec iov = { .iov_base = (void *)buf, .iov_len = size }; return tap_receive_iov(nc, &iov, 1); } #ifndef __sun__ ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) { return read(tapfd, buf, maxlen); } #endif static void tap_send_completed(NetClientState *nc, ssize_t len) { TAPState *s = DO_UPCAST(TAPState, nc, nc); tap_read_poll(s, true); } static void tap_send(void *opaque) { TAPState *s = opaque; int size; int packets = 0; while (true) { uint8_t *buf = s->buf; uint8_t min_pkt[ETH_ZLEN]; size_t min_pktsz = sizeof(min_pkt); size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); if (size <= 0) { break; } if (s->host_vnet_hdr_len && !s->using_vnet_hdr) { buf += s->host_vnet_hdr_len; size -= s->host_vnet_hdr_len; } if (net_peer_needs_padding(&s->nc)) { if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) { buf = min_pkt; size = min_pktsz; } } size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed); if (size == 0) { tap_read_poll(s, false); break; } else if (size < 0) { break; } /* * When the host keeps receiving more packets while tap_send() is * running we can hog the BQL. Limit the number of * packets that are processed per tap_send() callback to prevent * stalling the guest. */ packets++; if (packets >= 50) { break; } } } static bool tap_has_ufo(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); assert(nc->info->type == NET_CLIENT_DRIVER_TAP); return s->has_ufo; } static bool tap_has_uso(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); assert(nc->info->type == NET_CLIENT_DRIVER_TAP); return s->has_uso; } static bool tap_has_vnet_hdr(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); assert(nc->info->type == NET_CLIENT_DRIVER_TAP); return !!s->host_vnet_hdr_len; } static bool tap_has_vnet_hdr_len(NetClientState *nc, int len) { return tap_has_vnet_hdr(nc); } static void tap_set_vnet_hdr_len(NetClientState *nc, int len) { TAPState *s = DO_UPCAST(TAPState, nc, nc); assert(nc->info->type == NET_CLIENT_DRIVER_TAP); tap_fd_set_vnet_hdr_len(s->fd, len); s->host_vnet_hdr_len = len; s->using_vnet_hdr = true; } static int tap_set_vnet_le(NetClientState *nc, bool is_le) { TAPState *s = DO_UPCAST(TAPState, nc, nc); return tap_fd_set_vnet_le(s->fd, is_le); } static int tap_set_vnet_be(NetClientState *nc, bool is_be) { TAPState *s = DO_UPCAST(TAPState, nc, nc); return tap_fd_set_vnet_be(s->fd, is_be); } static void tap_set_offload(NetClientState *nc, int csum, int tso4, int tso6, int ecn, int ufo, int uso4, int uso6) { TAPState *s = DO_UPCAST(TAPState, nc, nc); if (s->fd < 0) { return; } tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo, uso4, uso6); } static void tap_exit_notify(Notifier *notifier, void *data) { TAPState *s = container_of(notifier, TAPState, exit); Error *err = NULL; if (s->down_script[0]) { launch_script(s->down_script, s->down_script_arg, s->fd, &err); if (err) { error_report_err(err); } } } static void tap_cleanup(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); if (s->vhost_net) { vhost_net_cleanup(s->vhost_net); g_free(s->vhost_net); s->vhost_net = NULL; } qemu_purge_queued_packets(nc); tap_exit_notify(&s->exit, NULL); qemu_remove_exit_notifier(&s->exit); tap_read_poll(s, false); tap_write_poll(s, false); close(s->fd); s->fd = -1; } static void tap_poll(NetClientState *nc, bool enable) { TAPState *s = DO_UPCAST(TAPState, nc, nc); tap_read_poll(s, enable); tap_write_poll(s, enable); } static bool tap_set_steering_ebpf(NetClientState *nc, int prog_fd) { TAPState *s = DO_UPCAST(TAPState, nc, nc); assert(nc->info->type == NET_CLIENT_DRIVER_TAP); return tap_fd_set_steering_ebpf(s->fd, prog_fd) == 0; } int tap_get_fd(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); assert(nc->info->type == NET_CLIENT_DRIVER_TAP); return s->fd; } /* fd support */ static NetClientInfo net_tap_info = { .type = NET_CLIENT_DRIVER_TAP, .size = sizeof(TAPState), .receive = tap_receive, .receive_iov = tap_receive_iov, .poll = tap_poll, .cleanup = tap_cleanup, .has_ufo = tap_has_ufo, .has_uso = tap_has_uso, .has_vnet_hdr = tap_has_vnet_hdr, .has_vnet_hdr_len = tap_has_vnet_hdr_len, .set_offload = tap_set_offload, .set_vnet_hdr_len = tap_set_vnet_hdr_len, .set_vnet_le = tap_set_vnet_le, .set_vnet_be = tap_set_vnet_be, .set_steering_ebpf = tap_set_steering_ebpf, }; static TAPState *net_tap_fd_init(NetClientState *peer, const char *model, const char *name, int fd, int vnet_hdr) { NetClientState *nc; TAPState *s; nc = qemu_new_net_client(&net_tap_info, peer, model, name); s = DO_UPCAST(TAPState, nc, nc); s->fd = fd; s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0; s->using_vnet_hdr = false; s->has_ufo = tap_probe_has_ufo(s->fd); s->has_uso = tap_probe_has_uso(s->fd); s->enabled = true; tap_set_offload(&s->nc, 0, 0, 0, 0, 0, 0, 0); /* * Make sure host header length is set correctly in tap: * it might have been modified by another instance of qemu. */ if (vnet_hdr) { tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len); } tap_read_poll(s, true); s->vhost_net = NULL; s->exit.notify = tap_exit_notify; qemu_add_exit_notifier(&s->exit); return s; } static void launch_script(const char *setup_script, const char *ifname, int fd, Error **errp) { int pid, status; char *args[3]; char **parg; /* try to launch network script */ pid = fork(); if (pid < 0) { error_setg_errno(errp, errno, "could not launch network script %s", setup_script); return; } if (pid == 0) { int open_max = sysconf(_SC_OPEN_MAX), i; for (i = 3; i < open_max; i++) { if (i != fd) { close(i); } } parg = args; *parg++ = (char *)setup_script; *parg++ = (char *)ifname; *parg = NULL; execv(setup_script, args); _exit(1); } else { while (waitpid(pid, &status, 0) != pid) { /* loop */ } if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { return; } error_setg(errp, "network script %s failed with status %d", setup_script, status); } } static int recv_fd(int c) { int fd; uint8_t msgbuf[CMSG_SPACE(sizeof(fd))]; struct msghdr msg = { .msg_control = msgbuf, .msg_controllen = sizeof(msgbuf), }; struct cmsghdr *cmsg; struct iovec iov; uint8_t req[1]; ssize_t len; cmsg = CMSG_FIRSTHDR(&msg); cmsg->cmsg_level = SOL_SOCKET; cmsg->cmsg_type = SCM_RIGHTS; cmsg->cmsg_len = CMSG_LEN(sizeof(fd)); msg.msg_controllen = cmsg->cmsg_len; iov.iov_base = req; iov.iov_len = sizeof(req); msg.msg_iov = &iov; msg.msg_iovlen = 1; len = recvmsg(c, &msg, 0); if (len > 0) { memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd)); return fd; } return len; } static int net_bridge_run_helper(const char *helper, const char *bridge, Error **errp) { sigset_t oldmask, mask; g_autofree char *default_helper = NULL; int pid, status; char *args[5]; char **parg; int sv[2]; sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigprocmask(SIG_BLOCK, &mask, &oldmask); if (!helper) { helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER); } if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { error_setg_errno(errp, errno, "socketpair() failed"); return -1; } /* try to launch bridge helper */ pid = fork(); if (pid < 0) { error_setg_errno(errp, errno, "Can't fork bridge helper"); return -1; } if (pid == 0) { int open_max = sysconf(_SC_OPEN_MAX), i; char *fd_buf = NULL; char *br_buf = NULL; char *helper_cmd = NULL; for (i = 3; i < open_max; i++) { if (i != sv[1]) { close(i); } } fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]); if (strrchr(helper, ' ') || strrchr(helper, '\t')) { /* assume helper is a command */ if (strstr(helper, "--br=") == NULL) { br_buf = g_strdup_printf("%s%s", "--br=", bridge); } helper_cmd = g_strdup_printf("%s %s %s %s", helper, "--use-vnet", fd_buf, br_buf ? br_buf : ""); parg = args; *parg++ = (char *)"sh"; *parg++ = (char *)"-c"; *parg++ = helper_cmd; *parg++ = NULL; execv("/bin/sh", args); g_free(helper_cmd); } else { /* assume helper is just the executable path name */ br_buf = g_strdup_printf("%s%s", "--br=", bridge); parg = args; *parg++ = (char *)helper; *parg++ = (char *)"--use-vnet"; *parg++ = fd_buf; *parg++ = br_buf; *parg++ = NULL; execv(helper, args); } g_free(fd_buf); g_free(br_buf); _exit(1); } else { int fd; int saved_errno; close(sv[1]); fd = RETRY_ON_EINTR(recv_fd(sv[0])); saved_errno = errno; close(sv[0]); while (waitpid(pid, &status, 0) != pid) { /* loop */ } sigprocmask(SIG_SETMASK, &oldmask, NULL); if (fd < 0) { error_setg_errno(errp, saved_errno, "failed to recv file descriptor"); return -1; } if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { error_setg(errp, "bridge helper failed"); return -1; } return fd; } } int net_init_bridge(const Netdev *netdev, const char *name, NetClientState *peer, Error **errp) { const NetdevBridgeOptions *bridge; const char *helper, *br; TAPState *s; int fd, vnet_hdr; assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE); bridge = &netdev->u.bridge; helper = bridge->helper; br = bridge->br ?: DEFAULT_BRIDGE_INTERFACE; fd = net_bridge_run_helper(helper, br, errp); if (fd == -1) { return -1; } if (!g_unix_set_fd_nonblocking(fd, true, NULL)) { error_setg_errno(errp, errno, "Failed to set FD nonblocking"); return -1; } vnet_hdr = tap_probe_vnet_hdr(fd, errp); if (vnet_hdr < 0) { close(fd); return -1; } s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); qemu_set_info_str(&s->nc, "helper=%s,br=%s", helper, br); return 0; } static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr, const char *setup_script, char *ifname, size_t ifname_sz, int mq_required, Error **errp) { Error *err = NULL; int fd, vnet_hdr_required; if (tap->has_vnet_hdr) { *vnet_hdr = tap->vnet_hdr; vnet_hdr_required = *vnet_hdr; } else { *vnet_hdr = 1; vnet_hdr_required = 0; } fd = RETRY_ON_EINTR(tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required, mq_required, errp)); if (fd < 0) { return -1; } if (setup_script && setup_script[0] != '\0' && strcmp(setup_script, "no") != 0) { launch_script(setup_script, ifname, fd, &err); if (err) { error_propagate(errp, err); close(fd); return -1; } } return fd; } #define MAX_TAP_QUEUES 1024 static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, const char *model, const char *name, const char *ifname, const char *script, const char *downscript, const char *vhostfdname, int vnet_hdr, int fd, Error **errp) { Error *err = NULL; TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr); int vhostfd; tap_set_sndbuf(s->fd, tap, &err); if (err) { error_propagate(errp, err); goto failed; } if (tap->fd || tap->fds) { qemu_set_info_str(&s->nc, "fd=%d", fd); } else if (tap->helper) { qemu_set_info_str(&s->nc, "helper=%s", tap->helper); } else { qemu_set_info_str(&s->nc, "ifname=%s,script=%s,downscript=%s", ifname, script, downscript); if (strcmp(downscript, "no") != 0) { snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); } } if (tap->has_vhost ? tap->vhost : vhostfdname || (tap->has_vhostforce && tap->vhostforce)) { VhostNetOptions options; options.backend_type = VHOST_BACKEND_TYPE_KERNEL; options.net_backend = &s->nc; if (tap->has_poll_us) { options.busyloop_timeout = tap->poll_us; } else { options.busyloop_timeout = 0; } if (vhostfdname) { vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err); if (vhostfd == -1) { error_propagate(errp, err); goto failed; } if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) { error_setg_errno(errp, errno, "%s: Can't use file descriptor %d", name, fd); goto failed; } } else { vhostfd = open("/dev/vhost-net", O_RDWR); if (vhostfd < 0) { error_setg_errno(errp, errno, "tap: open vhost char device failed"); goto failed; } if (!g_unix_set_fd_nonblocking(vhostfd, true, NULL)) { error_setg_errno(errp, errno, "Failed to set FD nonblocking"); goto failed; } } options.opaque = (void *)(uintptr_t)vhostfd; options.nvqs = 2; s->vhost_net = vhost_net_init(&options); if (!s->vhost_net) { error_setg(errp, "vhost-net requested but could not be initialized"); goto failed; } } else if (vhostfdname) { error_setg(errp, "vhostfd(s)= is not valid without vhost"); goto failed; } return; failed: qemu_del_net_client(&s->nc); } static int get_fds(char *str, char *fds[], int max) { char *ptr = str, *this; size_t len = strlen(str); int i = 0; while (i < max && ptr < str + len) { this = strchr(ptr, ':'); if (this == NULL) { fds[i] = g_strdup(ptr); } else { fds[i] = g_strndup(ptr, this - ptr); } i++; if (this == NULL) { break; } else { ptr = this + 1; } } return i; } int net_init_tap(const Netdev *netdev, const char *name, NetClientState *peer, Error **errp) { const NetdevTapOptions *tap; int fd, vnet_hdr = 0, i = 0, queues; /* for the no-fd, no-helper case */ const char *script; const char *downscript; Error *err = NULL; const char *vhostfdname; char ifname[128]; int ret = 0; assert(netdev->type == NET_CLIENT_DRIVER_TAP); tap = &netdev->u.tap; queues = tap->has_queues ? tap->queues : 1; vhostfdname = tap->vhostfd; script = tap->script; downscript = tap->downscript; /* QEMU hubs do not support multiqueue tap, in this case peer is set. * For -netdev, peer is always NULL. */ if (peer && (tap->has_queues || tap->fds || tap->vhostfds)) { error_setg(errp, "Multiqueue tap cannot be used with hubs"); return -1; } if (tap->fd) { if (tap->ifname || tap->script || tap->downscript || tap->has_vnet_hdr || tap->helper || tap->has_queues || tap->fds || tap->vhostfds) { error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " "helper=, queues=, fds=, and vhostfds= " "are invalid with fd="); return -1; } fd = monitor_fd_param(monitor_cur(), tap->fd, errp); if (fd == -1) { return -1; } if (!g_unix_set_fd_nonblocking(fd, true, NULL)) { error_setg_errno(errp, errno, "%s: Can't use file descriptor %d", name, fd); close(fd); return -1; } vnet_hdr = tap_probe_vnet_hdr(fd, errp); if (vnet_hdr < 0) { close(fd); return -1; } net_init_tap_one(tap, peer, "tap", name, NULL, script, downscript, vhostfdname, vnet_hdr, fd, &err); if (err) { error_propagate(errp, err); close(fd); return -1; } } else if (tap->fds) { char **fds; char **vhost_fds; int nfds = 0, nvhosts = 0; if (tap->ifname || tap->script || tap->downscript || tap->has_vnet_hdr || tap->helper || tap->has_queues || tap->vhostfd) { error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " "helper=, queues=, and vhostfd= " "are invalid with fds="); return -1; } fds = g_new0(char *, MAX_TAP_QUEUES); vhost_fds = g_new0(char *, MAX_TAP_QUEUES); nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES); if (tap->vhostfds) { nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES); if (nfds != nvhosts) { error_setg(errp, "The number of fds passed does not match " "the number of vhostfds passed"); ret = -1; goto free_fail; } } for (i = 0; i < nfds; i++) { fd = monitor_fd_param(monitor_cur(), fds[i], errp); if (fd == -1) { ret = -1; goto free_fail; } ret = g_unix_set_fd_nonblocking(fd, true, NULL); if (!ret) { error_setg_errno(errp, errno, "%s: Can't use file descriptor %d", name, fd); goto free_fail; } if (i == 0) { vnet_hdr = tap_probe_vnet_hdr(fd, errp); if (vnet_hdr < 0) { ret = -1; goto free_fail; } } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) { error_setg(errp, "vnet_hdr not consistent across given tap fds"); ret = -1; goto free_fail; } net_init_tap_one(tap, peer, "tap", name, ifname, script, downscript, tap->vhostfds ? vhost_fds[i] : NULL, vnet_hdr, fd, &err); if (err) { error_propagate(errp, err); ret = -1; goto free_fail; } } free_fail: for (i = 0; i < nvhosts; i++) { g_free(vhost_fds[i]); } for (i = 0; i < nfds; i++) { g_free(fds[i]); } g_free(fds); g_free(vhost_fds); return ret; } else if (tap->helper) { if (tap->ifname || tap->script || tap->downscript || tap->has_vnet_hdr || tap->has_queues || tap->vhostfds) { error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, " "queues=, and vhostfds= are invalid with helper="); return -1; } fd = net_bridge_run_helper(tap->helper, tap->br ?: DEFAULT_BRIDGE_INTERFACE, errp); if (fd == -1) { return -1; } if (!g_unix_set_fd_nonblocking(fd, true, NULL)) { error_setg_errno(errp, errno, "Failed to set FD nonblocking"); return -1; } vnet_hdr = tap_probe_vnet_hdr(fd, errp); if (vnet_hdr < 0) { close(fd); return -1; } net_init_tap_one(tap, peer, "bridge", name, ifname, script, downscript, vhostfdname, vnet_hdr, fd, &err); if (err) { error_propagate(errp, err); close(fd); return -1; } } else { g_autofree char *default_script = NULL; g_autofree char *default_downscript = NULL; if (tap->vhostfds) { error_setg(errp, "vhostfds= is invalid if fds= wasn't specified"); return -1; } if (!script) { script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT); } if (!downscript) { downscript = default_downscript = get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT); } if (tap->ifname) { pstrcpy(ifname, sizeof ifname, tap->ifname); } else { ifname[0] = '\0'; } for (i = 0; i < queues; i++) { fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script, ifname, sizeof ifname, queues > 1, errp); if (fd == -1) { return -1; } if (queues > 1 && i == 0 && !tap->ifname) { if (tap_fd_get_ifname(fd, ifname)) { error_setg(errp, "Fail to get ifname"); close(fd); return -1; } } net_init_tap_one(tap, peer, "tap", name, ifname, i >= 1 ? "no" : script, i >= 1 ? "no" : downscript, vhostfdname, vnet_hdr, fd, &err); if (err) { error_propagate(errp, err); close(fd); return -1; } } } return 0; } VHostNetState *tap_get_vhost_net(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); assert(nc->info->type == NET_CLIENT_DRIVER_TAP); return s->vhost_net; } int tap_enable(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); int ret; if (s->enabled) { return 0; } else { ret = tap_fd_enable(s->fd); if (ret == 0) { s->enabled = true; tap_update_fd_handler(s); } return ret; } } int tap_disable(NetClientState *nc) { TAPState *s = DO_UPCAST(TAPState, nc, nc); int ret; if (s->enabled == 0) { return 0; } else { ret = tap_fd_disable(s->fd); if (ret == 0) { qemu_purge_queued_packets(nc); s->enabled = false; tap_update_fd_handler(s); } return ret; } }