-----BEGIN PGP SIGNATURE-----

Version: GnuPG v1
 
 iQEcBAABAgAGBQJjW2i1AAoJEO8Ells5jWIR5HMIAIvDEmWQ2eZ1R+CfsefXkD5H
 W3RSZbMrOHR6sb9cbYpqK/vWmH8E/jZkKY4n/q7vQ3QerFMeDPgxu0Qn43iElLXS
 iGHhC51fa5IwJNDomjUGI8oJzyk0sxAbgwOjXZ4qbAkk9KeQYWU+JqYghvOrBYbd
 VaIwEiBlECuBy0DKx2gBTfxgeuw3V3WvtjpKeZVRlR+4vLQtI9Goga78qIPfjpH2
 sN/lFhZGaX1FT8DSft0oCCBxCK8ZaNzmMpmr39a8+e4g/EJZC9xbgNkl3fN0yYa5
 P0nluv/Z6e1TZ4FBDaiVFysTS5WnS0KRjUrodzctiGECE3sQiAvkWbKZ+QdGrlw=
 =0/b/
 -----END PGP SIGNATURE-----

Merge tag 'net-pull-request' of https://github.com/jasowang/qemu into staging

# -----BEGIN PGP SIGNATURE-----
# Version: GnuPG v1
#
# iQEcBAABAgAGBQJjW2i1AAoJEO8Ells5jWIR5HMIAIvDEmWQ2eZ1R+CfsefXkD5H
# W3RSZbMrOHR6sb9cbYpqK/vWmH8E/jZkKY4n/q7vQ3QerFMeDPgxu0Qn43iElLXS
# iGHhC51fa5IwJNDomjUGI8oJzyk0sxAbgwOjXZ4qbAkk9KeQYWU+JqYghvOrBYbd
# VaIwEiBlECuBy0DKx2gBTfxgeuw3V3WvtjpKeZVRlR+4vLQtI9Goga78qIPfjpH2
# sN/lFhZGaX1FT8DSft0oCCBxCK8ZaNzmMpmr39a8+e4g/EJZC9xbgNkl3fN0yYa5
# P0nluv/Z6e1TZ4FBDaiVFysTS5WnS0KRjUrodzctiGECE3sQiAvkWbKZ+QdGrlw=
# =0/b/
# -----END PGP SIGNATURE-----
# gpg: Signature made Fri 28 Oct 2022 01:29:25 EDT
# gpg:                using RSA key EF04965B398D6211
# gpg: Good signature from "Jason Wang (Jason Wang on RedHat) <jasowang@redhat.com>" [full]
# Primary key fingerprint: 215D 46F4 8246 689E C77F  3562 EF04 965B 398D 6211

* tag 'net-pull-request' of https://github.com/jasowang/qemu: (26 commits)
  net: stream: add QAPI events to report connection state
  net: stream: move to QIO to enable additional parameters
  qemu-sockets: update socket_uri() and socket_parse() to be consistent
  qemu-sockets: move and rename SocketAddress_to_str()
  net: dgram: add unix socket
  net: dgram: move mcast specific code from net_socket_fd_init_dgram()
  net: dgram: make dgram_dst generic
  net: stream: add unix socket
  net: stream: Don't ignore EINVAL on netdev socket connection
  net: socket: Don't ignore EINVAL on netdev socket connection
  qapi: net: add stream and dgram netdevs
  net: introduce qemu_set_info_str() function
  qapi: net: introduce a way to bypass qemu_opts_parse_noisily()
  net: simplify net_client_parse() error management
  net: remove the @errp argument of net_client_inits()
  net: introduce convert_host_port()
  vhost: Accept event idx flag
  vhost: use avail event idx on vhost_svq_kick
  vhost: toggle device callbacks using used event idx
  vhost: allocate event_idx fields on vring
  ...

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Stefan Hajnoczi 2022-10-30 18:30:00 -04:00
commit 2281c822ee
25 changed files with 1480 additions and 217 deletions

View File

@ -1276,7 +1276,7 @@ ERST
{
.name = "netdev_add",
.args_type = "netdev:O",
.params = "[user|tap|socket|vde|bridge|hubport|netmap|vhost-user"
.params = "[user|tap|socket|stream|dgram|vde|bridge|hubport|netmap|vhost-user"
#ifdef CONFIG_VMNET
"|vmnet-host|vmnet-shared|vmnet-bridged"
#endif

View File

@ -2526,6 +2526,7 @@ static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
VirtIONet *n = qemu_get_nic_opaque(nc);
VirtIONetQueue *q = virtio_net_get_subqueue(nc);
VirtIODevice *vdev = VIRTIO_DEVICE(n);
int ret;
virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
virtio_notify(vdev, q->tx_vq);
@ -2534,7 +2535,22 @@ static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
q->async_tx.elem = NULL;
virtio_queue_set_notification(q->tx_vq, 1);
virtio_net_flush_tx(q);
ret = virtio_net_flush_tx(q);
if (ret >= n->tx_burst) {
/*
* the flush has been stopped by tx_burst
* we will not receive notification for the
* remainining part, so re-schedule
*/
virtio_queue_set_notification(q->tx_vq, 0);
if (q->tx_bh) {
qemu_bh_schedule(q->tx_bh);
} else {
timer_mod(q->tx_timer,
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
}
q->tx_waiting = 1;
}
}
/* TX */
@ -2633,6 +2649,8 @@ drop:
return num_packets;
}
static void virtio_net_tx_timer(void *opaque);
static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
{
VirtIONet *n = VIRTIO_NET(vdev);
@ -2650,15 +2668,13 @@ static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
}
if (q->tx_waiting) {
virtio_queue_set_notification(vq, 1);
/* We already have queued packets, immediately flush */
timer_del(q->tx_timer);
q->tx_waiting = 0;
if (virtio_net_flush_tx(q) == -EINVAL) {
return;
}
virtio_net_tx_timer(q);
} else {
/* re-arm timer to flush it (and more) on next tick */
timer_mod(q->tx_timer,
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
q->tx_waiting = 1;
virtio_queue_set_notification(vq, 0);
}
@ -2691,6 +2707,8 @@ static void virtio_net_tx_timer(void *opaque)
VirtIONetQueue *q = opaque;
VirtIONet *n = q->n;
VirtIODevice *vdev = VIRTIO_DEVICE(n);
int ret;
/* This happens when device was stopped but BH wasn't. */
if (!vdev->vm_running) {
/* Make sure tx waiting is set, so we'll run when restarted. */
@ -2705,8 +2723,33 @@ static void virtio_net_tx_timer(void *opaque)
return;
}
ret = virtio_net_flush_tx(q);
if (ret == -EBUSY || ret == -EINVAL) {
return;
}
/*
* If we flush a full burst of packets, assume there are
* more coming and immediately rearm
*/
if (ret >= n->tx_burst) {
q->tx_waiting = 1;
timer_mod(q->tx_timer,
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
return;
}
/*
* If less than a full burst, re-enable notification and flush
* anything that may have come in while we weren't looking. If
* we find something, assume the guest is still active and rearm
*/
virtio_queue_set_notification(q->tx_vq, 1);
virtio_net_flush_tx(q);
ret = virtio_net_flush_tx(q);
if (ret > 0) {
virtio_queue_set_notification(q->tx_vq, 0);
q->tx_waiting = 1;
timer_mod(q->tx_timer,
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
}
}
static void virtio_net_tx_bh(void *opaque)

View File

@ -296,9 +296,8 @@ static int net_init(struct XenLegacyDevice *xendev)
netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf,
"xen", NULL, netdev);
snprintf(qemu_get_queue(netdev->nic)->info_str,
sizeof(qemu_get_queue(netdev->nic)->info_str),
"nic: xenbus vif macaddr=%s", netdev->mac);
qemu_set_info_str(qemu_get_queue(netdev->nic),
"nic: xenbus vif macaddr=%s", netdev->mac);
/* fill info */
xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1);

View File

@ -33,6 +33,7 @@ bool vhost_svq_valid_features(uint64_t features, Error **errp)
++b) {
switch (b) {
case VIRTIO_F_ANY_LAYOUT:
case VIRTIO_RING_F_EVENT_IDX:
continue;
case VIRTIO_F_ACCESS_PLATFORM:
@ -218,12 +219,22 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq,
static void vhost_svq_kick(VhostShadowVirtqueue *svq)
{
bool needs_kick;
/*
* We need to expose the available array entries before checking the used
* flags
*/
smp_mb();
if (svq->vring.used->flags & VRING_USED_F_NO_NOTIFY) {
if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
uint16_t avail_event = *(uint16_t *)(&svq->vring.used->ring[svq->vring.num]);
needs_kick = vring_need_event(avail_event, svq->shadow_avail_idx, svq->shadow_avail_idx - 1);
} else {
needs_kick = !(svq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
}
if (!needs_kick) {
return;
}
@ -369,15 +380,27 @@ static bool vhost_svq_more_used(VhostShadowVirtqueue *svq)
*/
static bool vhost_svq_enable_notification(VhostShadowVirtqueue *svq)
{
svq->vring.avail->flags &= ~cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
/* Make sure the flag is written before the read of used_idx */
if (virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
uint16_t *used_event = (uint16_t *)&svq->vring.avail->ring[svq->vring.num];
*used_event = svq->shadow_used_idx;
} else {
svq->vring.avail->flags &= ~cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
}
/* Make sure the event is enabled before the read of used_idx */
smp_mb();
return !vhost_svq_more_used(svq);
}
static void vhost_svq_disable_notification(VhostShadowVirtqueue *svq)
{
svq->vring.avail->flags |= cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
/*
* No need to disable notification in the event idx case, since used event
* index is already an index too far away.
*/
if (!virtio_vdev_has_feature(svq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
svq->vring.avail->flags |= cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
}
}
static uint16_t vhost_svq_last_desc_of_chain(const VhostShadowVirtqueue *svq,
@ -570,16 +593,16 @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq,
size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq)
{
size_t desc_size = sizeof(vring_desc_t) * svq->vring.num;
size_t avail_size = offsetof(vring_avail_t, ring) +
sizeof(uint16_t) * svq->vring.num;
size_t avail_size = offsetof(vring_avail_t, ring[svq->vring.num]) +
sizeof(uint16_t);
return ROUND_UP(desc_size + avail_size, qemu_real_host_page_size());
}
size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq)
{
size_t used_size = offsetof(vring_used_t, ring) +
sizeof(vring_used_elem_t) * svq->vring.num;
size_t used_size = offsetof(vring_used_t, ring[svq->vring.num]) +
sizeof(uint16_t);
return ROUND_UP(used_size, qemu_real_host_page_size());
}

View File

@ -177,6 +177,7 @@ ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
void qemu_purge_queued_packets(NetClientState *nc);
void qemu_flush_queued_packets(NetClientState *nc);
void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
void qemu_set_info_str(NetClientState *nc, const char *fmt, ...);
void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
bool qemu_has_ufo(NetClientState *nc);
bool qemu_has_vnet_hdr(NetClientState *nc);
@ -220,9 +221,11 @@ extern NICInfo nd_table[MAX_NICS];
extern const char *host_net_devices[];
/* from net.c */
int net_client_parse(QemuOptsList *opts_list, const char *str);
bool netdev_is_modern(const char *optarg);
void netdev_parse_modern(const char *optarg);
void net_client_parse(QemuOptsList *opts_list, const char *str);
void show_netdevs(void);
int net_init_clients(Error **errp);
void net_init_clients(void);
void net_check_clients(void);
void net_cleanup(void);
void hmp_host_net_add(Monitor *mon, const QDict *qdict);

View File

@ -58,6 +58,7 @@ NetworkAddressFamily inet_netfamily(int family);
int unix_listen(const char *path, Error **errp);
int unix_connect(const char *path, Error **errp);
char *socket_uri(SocketAddress *addr);
SocketAddress *socket_parse(const char *str, Error **errp);
int socket_connect(SocketAddress *addr, Error **errp);
int socket_listen(SocketAddress *addr, int num, Error **errp);
@ -65,6 +66,8 @@ void socket_listen_cleanup(int fd, Error **errp);
int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp);
/* Old, ipv4 only bits. Don't use for new code. */
int convert_host_port(struct sockaddr_in *saddr, const char *host,
const char *port, Error **errp);
int parse_host_port(struct sockaddr_in *saddr, const char *str,
Error **errp);
int socket_init(void);
@ -139,5 +142,4 @@ SocketAddress *socket_address_flatten(SocketAddressLegacy *addr);
* Return 0 on success.
*/
int socket_address_parse_named_fd(SocketAddress *addr, Error **errp);
#endif /* QEMU_SOCKETS_H */

View File

@ -199,27 +199,6 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
qapi_free_MouseInfoList(mice_list);
}
static char *SocketAddress_to_str(SocketAddress *addr)
{
switch (addr->type) {
case SOCKET_ADDRESS_TYPE_INET:
return g_strdup_printf("tcp:%s:%s",
addr->u.inet.host,
addr->u.inet.port);
case SOCKET_ADDRESS_TYPE_UNIX:
return g_strdup_printf("unix:%s",
addr->u.q_unix.path);
case SOCKET_ADDRESS_TYPE_FD:
return g_strdup_printf("fd:%s", addr->u.fd.str);
case SOCKET_ADDRESS_TYPE_VSOCK:
return g_strdup_printf("tcp:%s:%s",
addr->u.vsock.cid,
addr->u.vsock.port);
default:
return g_strdup("unknown address type");
}
}
void hmp_info_migrate(Monitor *mon, const QDict *qdict)
{
MigrationInfo *info;
@ -382,7 +361,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict)
monitor_printf(mon, "socket address: [\n");
for (addr = info->socket_address; addr; addr = addr->next) {
char *s = SocketAddress_to_str(addr->value);
char *s = socket_uri(addr->value);
monitor_printf(mon, "\t%s\n", s);
g_free(s);
}

View File

@ -40,6 +40,12 @@ int net_init_hubport(const Netdev *netdev, const char *name,
int net_init_socket(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp);
int net_init_stream(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp);
int net_init_dgram(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp);
int net_init_tap(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp);

623
net/dgram.c Normal file
View File

@ -0,0 +1,623 @@
/*
* QEMU System Emulator
*
* Copyright (c) 2003-2008 Fabrice Bellard
* Copyright (c) 2022 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "net/net.h"
#include "clients.h"
#include "monitor/monitor.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/option.h"
#include "qemu/sockets.h"
#include "qemu/iov.h"
#include "qemu/main-loop.h"
#include "qemu/cutils.h"
typedef struct NetDgramState {
NetClientState nc;
int fd;
SocketReadState rs;
bool read_poll; /* waiting to receive data? */
bool write_poll; /* waiting to transmit data? */
/* contains destination iff connectionless */
struct sockaddr *dest_addr;
socklen_t dest_len;
} NetDgramState;
static void net_dgram_send(void *opaque);
static void net_dgram_writable(void *opaque);
static void net_dgram_update_fd_handler(NetDgramState *s)
{
qemu_set_fd_handler(s->fd,
s->read_poll ? net_dgram_send : NULL,
s->write_poll ? net_dgram_writable : NULL,
s);
}
static void net_dgram_read_poll(NetDgramState *s, bool enable)
{
s->read_poll = enable;
net_dgram_update_fd_handler(s);
}
static void net_dgram_write_poll(NetDgramState *s, bool enable)
{
s->write_poll = enable;
net_dgram_update_fd_handler(s);
}
static void net_dgram_writable(void *opaque)
{
NetDgramState *s = opaque;
net_dgram_write_poll(s, false);
qemu_flush_queued_packets(&s->nc);
}
static ssize_t net_dgram_receive(NetClientState *nc,
const uint8_t *buf, size_t size)
{
NetDgramState *s = DO_UPCAST(NetDgramState, nc, nc);
ssize_t ret;
do {
if (s->dest_addr) {
ret = sendto(s->fd, buf, size, 0, s->dest_addr, s->dest_len);
} else {
ret = send(s->fd, buf, size, 0);
}
} while (ret == -1 && errno == EINTR);
if (ret == -1 && errno == EAGAIN) {
net_dgram_write_poll(s, true);
return 0;
}
return ret;
}
static void net_dgram_send_completed(NetClientState *nc, ssize_t len)
{
NetDgramState *s = DO_UPCAST(NetDgramState, nc, nc);
if (!s->read_poll) {
net_dgram_read_poll(s, true);
}
}
static void net_dgram_rs_finalize(SocketReadState *rs)
{
NetDgramState *s = container_of(rs, NetDgramState, rs);
if (qemu_send_packet_async(&s->nc, rs->buf,
rs->packet_len,
net_dgram_send_completed) == 0) {
net_dgram_read_poll(s, false);
}
}
static void net_dgram_send(void *opaque)
{
NetDgramState *s = opaque;
int size;
size = recv(s->fd, s->rs.buf, sizeof(s->rs.buf), 0);
if (size < 0) {
return;
}
if (size == 0) {
/* end of connection */
net_dgram_read_poll(s, false);
net_dgram_write_poll(s, false);
return;
}
if (qemu_send_packet_async(&s->nc, s->rs.buf, size,
net_dgram_send_completed) == 0) {
net_dgram_read_poll(s, false);
}
}
static int net_dgram_mcast_create(struct sockaddr_in *mcastaddr,
struct in_addr *localaddr,
Error **errp)
{
struct ip_mreq imr;
int fd;
int val, ret;
#ifdef __OpenBSD__
unsigned char loop;
#else
int loop;
#endif
if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
error_setg(errp, "specified mcastaddr %s (0x%08x) "
"does not contain a multicast address",
inet_ntoa(mcastaddr->sin_addr),
(int)ntohl(mcastaddr->sin_addr.s_addr));
return -1;
}
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
error_setg_errno(errp, errno, "can't create datagram socket");
return -1;
}
/*
* Allow multiple sockets to bind the same multicast ip and port by setting
* SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
* on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
* only on posix systems.
*/
val = 1;
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
if (ret < 0) {
error_setg_errno(errp, errno, "can't set socket option SO_REUSEADDR");
goto fail;
}
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind ip=%s to socket",
inet_ntoa(mcastaddr->sin_addr));
goto fail;
}
/* Add host to multicast group */
imr.imr_multiaddr = mcastaddr->sin_addr;
if (localaddr) {
imr.imr_interface = *localaddr;
} else {
imr.imr_interface.s_addr = htonl(INADDR_ANY);
}
ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
&imr, sizeof(struct ip_mreq));
if (ret < 0) {
error_setg_errno(errp, errno,
"can't add socket to multicast group %s",
inet_ntoa(imr.imr_multiaddr));
goto fail;
}
/* Force mcast msgs to loopback (eg. several QEMUs in same host */
loop = 1;
ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
&loop, sizeof(loop));
if (ret < 0) {
error_setg_errno(errp, errno,
"can't force multicast message to loopback");
goto fail;
}
/* If a bind address is given, only send packets from that address */
if (localaddr != NULL) {
ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
localaddr, sizeof(*localaddr));
if (ret < 0) {
error_setg_errno(errp, errno,
"can't set the default network send interface");
goto fail;
}
}
qemu_socket_set_nonblock(fd);
return fd;
fail:
if (fd >= 0) {
closesocket(fd);
}
return -1;
}
static void net_dgram_cleanup(NetClientState *nc)
{
NetDgramState *s = DO_UPCAST(NetDgramState, nc, nc);
if (s->fd != -1) {
net_dgram_read_poll(s, false);
net_dgram_write_poll(s, false);
close(s->fd);
s->fd = -1;
}
g_free(s->dest_addr);
s->dest_addr = NULL;
s->dest_len = 0;
}
static NetClientInfo net_dgram_socket_info = {
.type = NET_CLIENT_DRIVER_DGRAM,
.size = sizeof(NetDgramState),
.receive = net_dgram_receive,
.cleanup = net_dgram_cleanup,
};
static NetDgramState *net_dgram_fd_init(NetClientState *peer,
const char *model,
const char *name,
int fd,
Error **errp)
{
NetClientState *nc;
NetDgramState *s;
nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
s = DO_UPCAST(NetDgramState, nc, nc);
s->fd = fd;
net_socket_rs_init(&s->rs, net_dgram_rs_finalize, false);
net_dgram_read_poll(s, true);
return s;
}
static int net_dgram_mcast_init(NetClientState *peer,
const char *model,
const char *name,
SocketAddress *remote,
SocketAddress *local,
Error **errp)
{
NetDgramState *s;
int fd, ret;
struct sockaddr_in *saddr;
if (remote->type != SOCKET_ADDRESS_TYPE_INET) {
error_setg(errp, "multicast only support inet type");
return -1;
}
saddr = g_new(struct sockaddr_in, 1);
if (convert_host_port(saddr, remote->u.inet.host, remote->u.inet.port,
errp) < 0) {
g_free(saddr);
return -1;
}
if (!local) {
fd = net_dgram_mcast_create(saddr, NULL, errp);
if (fd < 0) {
g_free(saddr);
return -1;
}
} else {
switch (local->type) {
case SOCKET_ADDRESS_TYPE_INET: {
struct in_addr localaddr;
if (inet_aton(local->u.inet.host, &localaddr) == 0) {
g_free(saddr);
error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
local->u.inet.host);
return -1;
}
fd = net_dgram_mcast_create(saddr, &localaddr, errp);
if (fd < 0) {
g_free(saddr);
return -1;
}
break;
}
case SOCKET_ADDRESS_TYPE_FD: {
int newfd;
fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
if (fd == -1) {
g_free(saddr);
return -1;
}
ret = qemu_socket_try_set_nonblock(fd);
if (ret < 0) {
g_free(saddr);
error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
name, fd);
return -1;
}
/*
* fd passed: multicast: "learn" dest_addr address from bound
* address and save it. Because this may be "shared" socket from a
* "master" process, datagrams would be recv() by ONLY ONE process:
* we must "clone" this dgram socket --jjo
*/
saddr = g_new(struct sockaddr_in, 1);
if (convert_host_port(saddr, local->u.inet.host, local->u.inet.port,
errp) < 0) {
g_free(saddr);
closesocket(fd);
return -1;
}
/* must be bound */
if (saddr->sin_addr.s_addr == 0) {
error_setg(errp, "can't setup multicast destination address");
g_free(saddr);
closesocket(fd);
return -1;
}
/* clone dgram socket */
newfd = net_dgram_mcast_create(saddr, NULL, errp);
if (newfd < 0) {
g_free(saddr);
closesocket(fd);
return -1;
}
/* clone newfd to fd, close newfd */
dup2(newfd, fd);
close(newfd);
break;
}
default:
g_free(saddr);
error_setg(errp, "only support inet or fd type for local");
return -1;
}
}
s = net_dgram_fd_init(peer, model, name, fd, errp);
if (!s) {
g_free(saddr);
return -1;
}
g_assert(s->dest_addr == NULL);
s->dest_addr = (struct sockaddr *)saddr;
s->dest_len = sizeof(*saddr);
if (!local) {
qemu_set_info_str(&s->nc, "mcast=%s:%d",
inet_ntoa(saddr->sin_addr),
ntohs(saddr->sin_port));
} else {
switch (local->type) {
case SOCKET_ADDRESS_TYPE_INET:
qemu_set_info_str(&s->nc, "mcast=%s:%d",
inet_ntoa(saddr->sin_addr),
ntohs(saddr->sin_port));
break;
case SOCKET_ADDRESS_TYPE_FD:
qemu_set_info_str(&s->nc, "fd=%d (cloned mcast=%s:%d)",
fd, inet_ntoa(saddr->sin_addr),
ntohs(saddr->sin_port));
break;
default:
g_assert_not_reached();
}
}
return 0;
}
int net_init_dgram(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp)
{
NetDgramState *s;
int fd, ret;
SocketAddress *remote, *local;
struct sockaddr *dest_addr;
struct sockaddr_in laddr_in, raddr_in;
struct sockaddr_un laddr_un, raddr_un;
socklen_t dest_len;
assert(netdev->type == NET_CLIENT_DRIVER_DGRAM);
remote = netdev->u.dgram.remote;
local = netdev->u.dgram.local;
/* detect multicast address */
if (remote && remote->type == SOCKET_ADDRESS_TYPE_INET) {
struct sockaddr_in mcastaddr;
if (convert_host_port(&mcastaddr, remote->u.inet.host,
remote->u.inet.port, errp) < 0) {
return -1;
}
if (IN_MULTICAST(ntohl(mcastaddr.sin_addr.s_addr))) {
return net_dgram_mcast_init(peer, "dram", name, remote, local,
errp);
}
}
/* unicast address */
if (!local) {
error_setg(errp, "dgram requires local= parameter");
return -1;
}
if (remote) {
if (local->type == SOCKET_ADDRESS_TYPE_FD) {
error_setg(errp, "don't set remote with local.fd");
return -1;
}
if (remote->type != local->type) {
error_setg(errp, "remote and local types must be the same");
return -1;
}
} else {
if (local->type != SOCKET_ADDRESS_TYPE_FD) {
error_setg(errp,
"type=inet or type=unix requires remote parameter");
return -1;
}
}
switch (local->type) {
case SOCKET_ADDRESS_TYPE_INET:
if (convert_host_port(&laddr_in, local->u.inet.host, local->u.inet.port,
errp) < 0) {
return -1;
}
if (convert_host_port(&raddr_in, remote->u.inet.host,
remote->u.inet.port, errp) < 0) {
return -1;
}
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
error_setg_errno(errp, errno, "can't create datagram socket");
return -1;
}
ret = socket_set_fast_reuse(fd);
if (ret < 0) {
error_setg_errno(errp, errno,
"can't set socket option SO_REUSEADDR");
closesocket(fd);
return -1;
}
ret = bind(fd, (struct sockaddr *)&laddr_in, sizeof(laddr_in));
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind ip=%s to socket",
inet_ntoa(laddr_in.sin_addr));
closesocket(fd);
return -1;
}
qemu_socket_set_nonblock(fd);
dest_len = sizeof(raddr_in);
dest_addr = g_malloc(dest_len);
memcpy(dest_addr, &raddr_in, dest_len);
break;
case SOCKET_ADDRESS_TYPE_UNIX:
ret = unlink(local->u.q_unix.path);
if (ret < 0 && errno != ENOENT) {
error_setg_errno(errp, errno, "failed to unlink socket %s",
local->u.q_unix.path);
return -1;
}
laddr_un.sun_family = PF_UNIX;
ret = snprintf(laddr_un.sun_path, sizeof(laddr_un.sun_path), "%s",
local->u.q_unix.path);
if (ret < 0 || ret >= sizeof(laddr_un.sun_path)) {
error_setg(errp, "UNIX socket path '%s' is too long",
local->u.q_unix.path);
error_append_hint(errp, "Path must be less than %zu bytes\n",
sizeof(laddr_un.sun_path));
}
raddr_un.sun_family = PF_UNIX;
ret = snprintf(raddr_un.sun_path, sizeof(raddr_un.sun_path), "%s",
remote->u.q_unix.path);
if (ret < 0 || ret >= sizeof(raddr_un.sun_path)) {
error_setg(errp, "UNIX socket path '%s' is too long",
remote->u.q_unix.path);
error_append_hint(errp, "Path must be less than %zu bytes\n",
sizeof(raddr_un.sun_path));
}
fd = qemu_socket(PF_UNIX, SOCK_DGRAM, 0);
if (fd < 0) {
error_setg_errno(errp, errno, "can't create datagram socket");
return -1;
}
ret = bind(fd, (struct sockaddr *)&laddr_un, sizeof(laddr_un));
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind unix=%s to socket",
laddr_un.sun_path);
closesocket(fd);
return -1;
}
qemu_socket_set_nonblock(fd);
dest_len = sizeof(raddr_un);
dest_addr = g_malloc(dest_len);
memcpy(dest_addr, &raddr_un, dest_len);
break;
case SOCKET_ADDRESS_TYPE_FD:
fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
if (fd == -1) {
return -1;
}
ret = qemu_socket_try_set_nonblock(fd);
if (ret < 0) {
error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
name, fd);
return -1;
}
dest_addr = NULL;
dest_len = 0;
break;
default:
error_setg(errp, "only support inet or fd type for local");
return -1;
}
s = net_dgram_fd_init(peer, "dgram", name, fd, errp);
if (!s) {
return -1;
}
if (remote) {
g_assert(s->dest_addr == NULL);
s->dest_addr = dest_addr;
s->dest_len = dest_len;
}
switch (local->type) {
case SOCKET_ADDRESS_TYPE_INET:
qemu_set_info_str(&s->nc, "udp=%s:%d/%s:%d",
inet_ntoa(laddr_in.sin_addr),
ntohs(laddr_in.sin_port),
inet_ntoa(raddr_in.sin_addr),
ntohs(raddr_in.sin_port));
break;
case SOCKET_ADDRESS_TYPE_UNIX:
qemu_set_info_str(&s->nc, "udp=%s:%s",
laddr_un.sun_path, raddr_un.sun_path);
break;
case SOCKET_ADDRESS_TYPE_FD: {
SocketAddress *sa;
SocketAddressType sa_type;
sa = socket_local_address(fd, errp);
if (sa) {
sa_type = sa->type;
qapi_free_SocketAddress(sa);
qemu_set_info_str(&s->nc, "fd=%d %s", fd,
SocketAddressType_str(sa_type));
} else {
qemu_set_info_str(&s->nc, "fd=%d", fd);
}
break;
}
default:
g_assert_not_reached();
}
return 0;
}

View File

@ -313,6 +313,8 @@ void net_hub_check_clients(void)
case NET_CLIENT_DRIVER_USER:
case NET_CLIENT_DRIVER_TAP:
case NET_CLIENT_DRIVER_SOCKET:
case NET_CLIENT_DRIVER_STREAM:
case NET_CLIENT_DRIVER_DGRAM:
case NET_CLIENT_DRIVER_VDE:
case NET_CLIENT_DRIVER_VHOST_USER:
has_host_dev = 1;

View File

@ -723,8 +723,7 @@ int net_init_l2tpv3(const Netdev *netdev,
l2tpv3_read_poll(s, true);
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"l2tpv3: connected");
qemu_set_info_str(&s->nc, "l2tpv3: connected");
return 0;
outerr:
qemu_del_net_client(nc);

View File

@ -13,6 +13,8 @@ softmmu_ss.add(files(
'net.c',
'queue.c',
'socket.c',
'stream.c',
'dgram.c',
'util.c',
))

218
net/net.c
View File

@ -48,12 +48,14 @@
#include "qemu/qemu-print.h"
#include "qemu/main-loop.h"
#include "qemu/option.h"
#include "qemu/keyval.h"
#include "qapi/error.h"
#include "qapi/opts-visitor.h"
#include "sysemu/runstate.h"
#include "net/colo-compare.h"
#include "net/filter.h"
#include "qapi/string-output-visitor.h"
#include "qapi/qobject-input-visitor.h"
/* Net bridge is currently not supported for W32. */
#if !defined(_WIN32)
@ -63,18 +65,60 @@
static VMChangeStateEntry *net_change_state_entry;
static QTAILQ_HEAD(, NetClientState) net_clients;
typedef struct NetdevQueueEntry {
Netdev *nd;
Location loc;
QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
} NetdevQueueEntry;
typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
/***********************************************************/
/* network device redirectors */
int convert_host_port(struct sockaddr_in *saddr, const char *host,
const char *port, Error **errp)
{
struct hostent *he;
const char *r;
long p;
memset(saddr, 0, sizeof(*saddr));
saddr->sin_family = AF_INET;
if (host[0] == '\0') {
saddr->sin_addr.s_addr = 0;
} else {
if (qemu_isdigit(host[0])) {
if (!inet_aton(host, &saddr->sin_addr)) {
error_setg(errp, "host address '%s' is not a valid "
"IPv4 address", host);
return -1;
}
} else {
he = gethostbyname(host);
if (he == NULL) {
error_setg(errp, "can't resolve host address '%s'", host);
return -1;
}
saddr->sin_addr = *(struct in_addr *)he->h_addr;
}
}
if (qemu_strtol(port, &r, 0, &p) != 0) {
error_setg(errp, "port number '%s' is invalid", port);
return -1;
}
saddr->sin_port = htons(p);
return 0;
}
int parse_host_port(struct sockaddr_in *saddr, const char *str,
Error **errp)
{
gchar **substrings;
struct hostent *he;
const char *addr, *p, *r;
int port, ret = 0;
memset(saddr, 0, sizeof(*saddr));
int ret;
substrings = g_strsplit(str, ":", 2);
if (!substrings || !substrings[0] || !substrings[1]) {
@ -84,37 +128,7 @@ int parse_host_port(struct sockaddr_in *saddr, const char *str,
goto out;
}
addr = substrings[0];
p = substrings[1];
saddr->sin_family = AF_INET;
if (addr[0] == '\0') {
saddr->sin_addr.s_addr = 0;
} else {
if (qemu_isdigit(addr[0])) {
if (!inet_aton(addr, &saddr->sin_addr)) {
error_setg(errp, "host address '%s' is not a valid "
"IPv4 address", addr);
ret = -1;
goto out;
}
} else {
he = gethostbyname(addr);
if (he == NULL) {
error_setg(errp, "can't resolve host address '%s'", addr);
ret = -1;
goto out;
}
saddr->sin_addr = *(struct in_addr *)he->h_addr;
}
}
port = strtol(p, (char **)&r, 0);
if (r == p) {
error_setg(errp, "port number '%s' is invalid", p);
ret = -1;
goto out;
}
saddr->sin_port = htons(port);
ret = convert_host_port(saddr, substrings[0], substrings[1], errp);
out:
g_strfreev(substrings);
@ -128,13 +142,20 @@ char *qemu_mac_strdup_printf(const uint8_t *macaddr)
macaddr[3], macaddr[4], macaddr[5]);
}
void qemu_set_info_str(NetClientState *nc, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(nc->info_str, sizeof(nc->info_str), fmt, ap);
va_end(ap);
}
void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
{
snprintf(nc->info_str, sizeof(nc->info_str),
"model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
nc->model,
macaddr[0], macaddr[1], macaddr[2],
macaddr[3], macaddr[4], macaddr[5]);
qemu_set_info_str(nc, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
nc->model, macaddr[0], macaddr[1], macaddr[2],
macaddr[3], macaddr[4], macaddr[5]);
}
static int mac_table[256] = {0};
@ -1001,6 +1022,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
#endif
[NET_CLIENT_DRIVER_TAP] = net_init_tap,
[NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
[NET_CLIENT_DRIVER_STREAM] = net_init_stream,
[NET_CLIENT_DRIVER_DGRAM] = net_init_dgram,
#ifdef CONFIG_VDE
[NET_CLIENT_DRIVER_VDE] = net_init_vde,
#endif
@ -1036,19 +1059,23 @@ static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
if (is_netdev) {
if (netdev->type == NET_CLIENT_DRIVER_NIC ||
!net_client_init_fun[netdev->type]) {
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
"a netdev backend type");
error_setg(errp, "network backend '%s' is not compiled into this binary",
NetClientDriver_str(netdev->type));
return -1;
}
} else {
if (netdev->type == NET_CLIENT_DRIVER_NONE) {
return 0; /* nothing to do */
}
if (netdev->type == NET_CLIENT_DRIVER_HUBPORT ||
!net_client_init_fun[netdev->type]) {
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
"a net backend type (maybe it is not compiled "
"into this binary)");
if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) {
error_setg(errp, "network backend '%s' is only supported with -netdev/-nic",
NetClientDriver_str(netdev->type));
return -1;
}
if (!net_client_init_fun[netdev->type]) {
error_setg(errp, "network backend '%s' is not compiled into this binary",
NetClientDriver_str(netdev->type));
return -1;
}
@ -1088,6 +1115,8 @@ void show_netdevs(void)
int idx;
const char *available_netdevs[] = {
"socket",
"stream",
"dgram",
"hubport",
"tap",
#ifdef CONFIG_SLIRP
@ -1560,36 +1589,97 @@ out:
return ret;
}
int net_init_clients(Error **errp)
static void netdev_init_modern(void)
{
while (!QSIMPLEQ_EMPTY(&nd_queue)) {
NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
loc_push_restore(&nd->loc);
net_client_init1(nd->nd, true, &error_fatal);
loc_pop(&nd->loc);
qapi_free_Netdev(nd->nd);
g_free(nd);
}
}
void net_init_clients(void)
{
net_change_state_entry =
qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
QTAILQ_INIT(&net_clients);
if (qemu_opts_foreach(qemu_find_opts("netdev"),
net_init_netdev, NULL, errp)) {
return -1;
}
netdev_init_modern();
if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, errp)) {
return -1;
}
qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL,
&error_fatal);
if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, errp)) {
return -1;
}
qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL,
&error_fatal);
return 0;
qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL,
&error_fatal);
}
int net_client_parse(QemuOptsList *opts_list, const char *optarg)
/*
* Does this -netdev argument use modern rather than traditional syntax?
* Modern syntax is to be parsed with netdev_parse_modern().
* Traditional syntax is to be parsed with net_client_parse().
*/
bool netdev_is_modern(const char *optarg)
{
if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
return -1;
QemuOpts *opts;
bool is_modern;
const char *type;
static QemuOptsList dummy_opts = {
.name = "netdev",
.implied_opt_name = "type",
.head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
.desc = { { } },
};
if (optarg[0] == '{') {
/* This is JSON, which means it's modern syntax */
return true;
}
return 0;
opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort);
qemu_opts_do_parse(opts, optarg, dummy_opts.implied_opt_name,
&error_abort);
type = qemu_opt_get(opts, "type");
is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
qemu_opts_reset(&dummy_opts);
return is_modern;
}
/*
* netdev_parse_modern() uses modern, more expressive syntax than
* net_client_parse(), but supports only the -netdev option.
* netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
* appends to @qemu_netdev_opts.
*/
void netdev_parse_modern(const char *optarg)
{
Visitor *v;
NetdevQueueEntry *nd;
v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
nd = g_new(NetdevQueueEntry, 1);
visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
visit_free(v);
loc_save(&nd->loc);
QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
}
void net_client_parse(QemuOptsList *opts_list, const char *optarg)
{
if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
exit(1);
}
}
/* From FreeBSD */

View File

@ -611,9 +611,8 @@ static int net_slirp_init(NetClientState *peer, const char *model,
nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
snprintf(nc->info_str, sizeof(nc->info_str),
"net=%s,restrict=%s", inet_ntoa(net),
restricted ? "on" : "off");
qemu_set_info_str(nc, "net=%s,restrict=%s", inet_ntoa(net),
restricted ? "on" : "off");
s = DO_UPCAST(SlirpState, nc, nc);

View File

@ -179,7 +179,7 @@ static void net_socket_send(void *opaque)
s->fd = -1;
net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
s->nc.link_down = true;
memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
qemu_set_info_str(&s->nc, "");
return;
}
@ -387,16 +387,15 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
/* mcast: save bound address as dst */
if (is_connected && mcast != NULL) {
s->dgram_dst = saddr;
snprintf(nc->info_str, sizeof(nc->info_str),
"socket: fd=%d (cloned mcast=%s:%d)",
fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
qemu_set_info_str(nc, "socket: fd=%d (cloned mcast=%s:%d)", fd,
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
} else {
if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) {
s->dgram_dst.sin_family = AF_UNIX;
}
snprintf(nc->info_str, sizeof(nc->info_str),
"socket: fd=%d %s", fd, SocketAddressType_str(sa_type));
qemu_set_info_str(nc, "socket: fd=%d %s", fd,
SocketAddressType_str(sa_type));
}
return s;
@ -430,7 +429,7 @@ static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
nc = qemu_new_net_client(&net_socket_info, peer, model, name);
snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
qemu_set_info_str(nc, "socket: fd=%d", fd);
s = DO_UPCAST(NetSocketState, nc, nc);
@ -497,9 +496,8 @@ static void net_socket_accept(void *opaque)
s->fd = fd;
s->nc.link_down = false;
net_socket_connect(s);
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"socket: connection from %s:%d",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
qemu_set_info_str(&s->nc, "socket: connection from %s:%d",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
}
static int net_socket_listen_init(NetClientState *peer,
@ -579,8 +577,7 @@ static int net_socket_connect_init(NetClientState *peer,
if (errno == EINTR || errno == EWOULDBLOCK) {
/* continue */
} else if (errno == EINPROGRESS ||
errno == EALREADY ||
errno == EINVAL) {
errno == EALREADY) {
break;
} else {
error_setg_errno(errp, errno, "can't connect socket");
@ -597,9 +594,8 @@ static int net_socket_connect_init(NetClientState *peer,
return -1;
}
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"socket: connect to %s:%d",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
qemu_set_info_str(&s->nc, "socket: connect to %s:%d",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
return 0;
}
@ -642,9 +638,8 @@ static int net_socket_mcast_init(NetClientState *peer,
s->dgram_dst = saddr;
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"socket: mcast=%s:%d",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
qemu_set_info_str(&s->nc, "socket: mcast=%s:%d",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
return 0;
}
@ -697,9 +692,8 @@ static int net_socket_udp_init(NetClientState *peer,
s->dgram_dst = raddr;
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"socket: udp=%s:%d",
inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
qemu_set_info_str(&s->nc, "socket: udp=%s:%d", inet_ntoa(raddr.sin_addr),
ntohs(raddr.sin_port));
return 0;
}

386
net/stream.c Normal file
View File

@ -0,0 +1,386 @@
/*
* QEMU System Emulator
*
* Copyright (c) 2003-2008 Fabrice Bellard
* Copyright (c) 2022 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "qemu/osdep.h"
#include "net/net.h"
#include "clients.h"
#include "monitor/monitor.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/option.h"
#include "qemu/sockets.h"
#include "qemu/iov.h"
#include "qemu/main-loop.h"
#include "qemu/cutils.h"
#include "io/channel.h"
#include "io/channel-socket.h"
#include "io/net-listener.h"
#include "qapi/qapi-events-net.h"
typedef struct NetStreamState {
NetClientState nc;
QIOChannel *listen_ioc;
QIONetListener *listener;
QIOChannel *ioc;
guint ioc_read_tag;
guint ioc_write_tag;
SocketReadState rs;
unsigned int send_index; /* number of bytes sent*/
} NetStreamState;
static void net_stream_listen(QIONetListener *listener,
QIOChannelSocket *cioc,
void *opaque);
static gboolean net_stream_writable(QIOChannel *ioc,
GIOCondition condition,
gpointer data)
{
NetStreamState *s = data;
s->ioc_write_tag = 0;
qemu_flush_queued_packets(&s->nc);
return G_SOURCE_REMOVE;
}
static ssize_t net_stream_receive(NetClientState *nc, const uint8_t *buf,
size_t size)
{
NetStreamState *s = DO_UPCAST(NetStreamState, nc, nc);
uint32_t len = htonl(size);
struct iovec iov[] = {
{
.iov_base = &len,
.iov_len = sizeof(len),
}, {
.iov_base = (void *)buf,
.iov_len = size,
},
};
struct iovec local_iov[2];
unsigned int nlocal_iov;
size_t remaining;
ssize_t ret;
remaining = iov_size(iov, 2) - s->send_index;
nlocal_iov = iov_copy(local_iov, 2, iov, 2, s->send_index, remaining);
ret = qio_channel_writev(s->ioc, local_iov, nlocal_iov, NULL);
if (ret == QIO_CHANNEL_ERR_BLOCK) {
ret = 0; /* handled further down */
}
if (ret == -1) {
s->send_index = 0;
return -errno;
}
if (ret < (ssize_t)remaining) {
s->send_index += ret;
s->ioc_write_tag = qio_channel_add_watch(s->ioc, G_IO_OUT,
net_stream_writable, s, NULL);
return 0;
}
s->send_index = 0;
return size;
}
static gboolean net_stream_send(QIOChannel *ioc,
GIOCondition condition,
gpointer data);
static void net_stream_send_completed(NetClientState *nc, ssize_t len)
{
NetStreamState *s = DO_UPCAST(NetStreamState, nc, nc);
if (!s->ioc_read_tag) {
s->ioc_read_tag = qio_channel_add_watch(s->ioc, G_IO_IN,
net_stream_send, s, NULL);
}
}
static void net_stream_rs_finalize(SocketReadState *rs)
{
NetStreamState *s = container_of(rs, NetStreamState, rs);
if (qemu_send_packet_async(&s->nc, rs->buf,
rs->packet_len,
net_stream_send_completed) == 0) {
if (s->ioc_read_tag) {
g_source_remove(s->ioc_read_tag);
s->ioc_read_tag = 0;
}
}
}
static gboolean net_stream_send(QIOChannel *ioc,
GIOCondition condition,
gpointer data)
{
NetStreamState *s = data;
int size;
int ret;
char buf1[NET_BUFSIZE];
const char *buf;
size = qio_channel_read(s->ioc, buf1, sizeof(buf1), NULL);
if (size < 0) {
if (errno != EWOULDBLOCK) {
goto eoc;
}
} else if (size == 0) {
/* end of connection */
eoc:
s->ioc_read_tag = 0;
if (s->ioc_write_tag) {
g_source_remove(s->ioc_write_tag);
s->ioc_write_tag = 0;
}
if (s->listener) {
qio_net_listener_set_client_func(s->listener, net_stream_listen,
s, NULL);
}
object_unref(OBJECT(s->ioc));
s->ioc = NULL;
net_socket_rs_init(&s->rs, net_stream_rs_finalize, false);
s->nc.link_down = true;
qemu_set_info_str(&s->nc, "");
qapi_event_send_netdev_stream_disconnected(s->nc.name);
return G_SOURCE_REMOVE;
}
buf = buf1;
ret = net_fill_rstate(&s->rs, (const uint8_t *)buf, size);
if (ret == -1) {
goto eoc;
}
return G_SOURCE_CONTINUE;
}
static void net_stream_cleanup(NetClientState *nc)
{
NetStreamState *s = DO_UPCAST(NetStreamState, nc, nc);
if (s->ioc) {
if (QIO_CHANNEL_SOCKET(s->ioc)->fd != -1) {
if (s->ioc_read_tag) {
g_source_remove(s->ioc_read_tag);
s->ioc_read_tag = 0;
}
if (s->ioc_write_tag) {
g_source_remove(s->ioc_write_tag);
s->ioc_write_tag = 0;
}
}
object_unref(OBJECT(s->ioc));
s->ioc = NULL;
}
if (s->listen_ioc) {
if (s->listener) {
qio_net_listener_disconnect(s->listener);
object_unref(OBJECT(s->listener));
s->listener = NULL;
}
object_unref(OBJECT(s->listen_ioc));
s->listen_ioc = NULL;
}
}
static NetClientInfo net_stream_info = {
.type = NET_CLIENT_DRIVER_STREAM,
.size = sizeof(NetStreamState),
.receive = net_stream_receive,
.cleanup = net_stream_cleanup,
};
static void net_stream_listen(QIONetListener *listener,
QIOChannelSocket *cioc,
void *opaque)
{
NetStreamState *s = opaque;
SocketAddress *addr;
char *uri;
object_ref(OBJECT(cioc));
qio_net_listener_set_client_func(s->listener, NULL, s, NULL);
s->ioc = QIO_CHANNEL(cioc);
qio_channel_set_name(s->ioc, "stream-server");
s->nc.link_down = false;
s->ioc_read_tag = qio_channel_add_watch(s->ioc, G_IO_IN, net_stream_send,
s, NULL);
if (cioc->localAddr.ss_family == AF_UNIX) {
addr = qio_channel_socket_get_local_address(cioc, NULL);
} else {
addr = qio_channel_socket_get_remote_address(cioc, NULL);
}
g_assert(addr != NULL);
uri = socket_uri(addr);
qemu_set_info_str(&s->nc, uri);
g_free(uri);
qapi_event_send_netdev_stream_connected(s->nc.name, addr);
qapi_free_SocketAddress(addr);
}
static void net_stream_server_listening(QIOTask *task, gpointer opaque)
{
NetStreamState *s = opaque;
QIOChannelSocket *listen_sioc = QIO_CHANNEL_SOCKET(s->listen_ioc);
SocketAddress *addr;
int ret;
if (listen_sioc->fd < 0) {
qemu_set_info_str(&s->nc, "connection error");
return;
}
addr = qio_channel_socket_get_local_address(listen_sioc, NULL);
g_assert(addr != NULL);
ret = qemu_socket_try_set_nonblock(listen_sioc->fd);
if (addr->type == SOCKET_ADDRESS_TYPE_FD && ret < 0) {
qemu_set_info_str(&s->nc, "can't use file descriptor %s (errno %d)",
addr->u.fd.str, -ret);
return;
}
g_assert(ret == 0);
qapi_free_SocketAddress(addr);
s->nc.link_down = true;
s->listener = qio_net_listener_new();
net_socket_rs_init(&s->rs, net_stream_rs_finalize, false);
qio_net_listener_set_client_func(s->listener, net_stream_listen, s, NULL);
qio_net_listener_add(s->listener, listen_sioc);
}
static int net_stream_server_init(NetClientState *peer,
const char *model,
const char *name,
SocketAddress *addr,
Error **errp)
{
NetClientState *nc;
NetStreamState *s;
QIOChannelSocket *listen_sioc = qio_channel_socket_new();
nc = qemu_new_net_client(&net_stream_info, peer, model, name);
s = DO_UPCAST(NetStreamState, nc, nc);
s->listen_ioc = QIO_CHANNEL(listen_sioc);
qio_channel_socket_listen_async(listen_sioc, addr, 0,
net_stream_server_listening, s,
NULL, NULL);
return 0;
}
static void net_stream_client_connected(QIOTask *task, gpointer opaque)
{
NetStreamState *s = opaque;
QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(s->ioc);
SocketAddress *addr;
gchar *uri;
int ret;
if (sioc->fd < 0) {
qemu_set_info_str(&s->nc, "connection error");
goto error;
}
addr = qio_channel_socket_get_remote_address(sioc, NULL);
g_assert(addr != NULL);
uri = socket_uri(addr);
qemu_set_info_str(&s->nc, uri);
g_free(uri);
ret = qemu_socket_try_set_nonblock(sioc->fd);
if (addr->type == SOCKET_ADDRESS_TYPE_FD && ret < 0) {
qemu_set_info_str(&s->nc, "can't use file descriptor %s (errno %d)",
addr->u.fd.str, -ret);
qapi_free_SocketAddress(addr);
goto error;
}
g_assert(ret == 0);
net_socket_rs_init(&s->rs, net_stream_rs_finalize, false);
/* Disable Nagle algorithm on TCP sockets to reduce latency */
qio_channel_set_delay(s->ioc, false);
s->ioc_read_tag = qio_channel_add_watch(s->ioc, G_IO_IN, net_stream_send,
s, NULL);
s->nc.link_down = false;
qapi_event_send_netdev_stream_connected(s->nc.name, addr);
qapi_free_SocketAddress(addr);
return;
error:
object_unref(OBJECT(s->ioc));
s->ioc = NULL;
}
static int net_stream_client_init(NetClientState *peer,
const char *model,
const char *name,
SocketAddress *addr,
Error **errp)
{
NetStreamState *s;
NetClientState *nc;
QIOChannelSocket *sioc = qio_channel_socket_new();
nc = qemu_new_net_client(&net_stream_info, peer, model, name);
s = DO_UPCAST(NetStreamState, nc, nc);
s->ioc = QIO_CHANNEL(sioc);
s->nc.link_down = true;
qio_channel_socket_connect_async(sioc, addr,
net_stream_client_connected, s,
NULL, NULL);
return 0;
}
int net_init_stream(const Netdev *netdev, const char *name,
NetClientState *peer, Error **errp)
{
const NetdevStreamOptions *sock;
assert(netdev->type == NET_CLIENT_DRIVER_STREAM);
sock = &netdev->u.stream;
if (!sock->has_server || !sock->server) {
return net_stream_client_init(peer, "stream", name, sock->addr, errp);
}
return net_stream_server_init(peer, "stream", name, sock->addr, errp);
}

View File

@ -789,8 +789,7 @@ static int tap_win32_init(NetClientState *peer, const char *model,
s = DO_UPCAST(TAPState, nc, nc);
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"tap: ifname=%s", ifname);
qemu_set_info_str(&s->nc, "tap: ifname=%s", ifname);
s->handle = handle;

View File

@ -630,8 +630,7 @@ int net_init_bridge(const Netdev *netdev, const char *name,
}
s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
br);
qemu_set_info_str(&s->nc, "helper=%s,br=%s", helper, br);
return 0;
}
@ -690,14 +689,12 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
}
if (tap->has_fd || tap->has_fds) {
snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
qemu_set_info_str(&s->nc, "fd=%d", fd);
} else if (tap->has_helper) {
snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
tap->helper);
qemu_set_info_str(&s->nc, "helper=%s", tap->helper);
} else {
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
"ifname=%s,script=%s,downscript=%s", ifname, script,
downscript);
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);

View File

@ -98,8 +98,7 @@ static int net_vde_init(NetClientState *peer, const char *model,
nc = qemu_new_net_client(&net_vde_info, peer, model, name);
snprintf(nc->info_str, sizeof(nc->info_str), "sock=%s,fd=%d",
sock, vde_datafd(vde));
qemu_set_info_str(nc, "sock=%s,fd=%d", sock, vde_datafd(vde));
s = DO_UPCAST(VDEState, nc, nc);

View File

@ -341,8 +341,7 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
user = g_new0(struct VhostUserState, 1);
for (i = 0; i < queues; i++) {
nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
i, chr->label);
qemu_set_info_str(nc, "vhost-user%d to %s", i, chr->label);
nc->queue_index = i;
if (!nc0) {
nc0 = nc;

View File

@ -63,7 +63,6 @@ const int vdpa_feature_bits[] = {
VIRTIO_NET_F_CTRL_RX,
VIRTIO_NET_F_CTRL_RX_EXTRA,
VIRTIO_NET_F_CTRL_VLAN,
VIRTIO_NET_F_GUEST_ANNOUNCE,
VIRTIO_NET_F_CTRL_MAC_ADDR,
VIRTIO_NET_F_RSS,
VIRTIO_NET_F_MQ,
@ -462,48 +461,6 @@ static NetClientInfo net_vhost_vdpa_cvq_info = {
.check_peer_type = vhost_vdpa_check_peer_type,
};
/**
* Do not forward commands not supported by SVQ. Otherwise, the device could
* accept it and qemu would not know how to update the device model.
*/
static bool vhost_vdpa_net_cvq_validate_cmd(const void *out_buf, size_t len)
{
struct virtio_net_ctrl_hdr ctrl;
if (unlikely(len < sizeof(ctrl))) {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: invalid legnth of out buffer %zu\n", __func__, len);
return false;
}
memcpy(&ctrl, out_buf, sizeof(ctrl));
switch (ctrl.class) {
case VIRTIO_NET_CTRL_MAC:
switch (ctrl.cmd) {
case VIRTIO_NET_CTRL_MAC_ADDR_SET:
return true;
default:
qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mac cmd %u\n",
__func__, ctrl.cmd);
};
break;
case VIRTIO_NET_CTRL_MQ:
switch (ctrl.cmd) {
case VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET:
return true;
default:
qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mq cmd %u\n",
__func__, ctrl.cmd);
};
break;
default:
qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid control class %u\n",
__func__, ctrl.class);
};
return false;
}
/**
* Validate and copy control virtqueue commands.
*
@ -527,16 +484,10 @@ static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
.iov_len = sizeof(status),
};
ssize_t dev_written = -EINVAL;
bool ok;
out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
s->cvq_cmd_out_buffer,
vhost_vdpa_net_cvq_cmd_len());
ok = vhost_vdpa_net_cvq_validate_cmd(s->cvq_cmd_out_buffer, out.iov_len);
if (unlikely(!ok)) {
goto out;
}
dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status));
if (unlikely(dev_written < 0)) {
goto out;
@ -593,7 +544,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
device, name);
}
snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA);
qemu_set_info_str(nc, TYPE_VHOST_VDPA);
s = DO_UPCAST(VhostVDPAState, nc, nc);
s->vhost_vdpa.device_fd = vdpa_device_fd;
@ -683,14 +634,29 @@ int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
opts = &netdev->u.vhost_vdpa;
if (!opts->vhostdev) {
error_setg(errp, "vdpa character device not specified with vhostdev");
if (!opts->has_vhostdev && !opts->has_vhostfd) {
error_setg(errp,
"vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
return -1;
}
vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
if (vdpa_device_fd == -1) {
return -errno;
if (opts->has_vhostdev && opts->has_vhostfd) {
error_setg(errp,
"vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
return -1;
}
if (opts->has_vhostdev) {
vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
if (vdpa_device_fd == -1) {
return -errno;
}
} else if (opts->has_vhostfd) {
vdpa_device_fd = monitor_fd_param(monitor_cur(), opts->vhostfd, errp);
if (vdpa_device_fd == -1) {
error_prepend(errp, "vhost-vdpa: unable to parse vhostfd: ");
return -1;
}
}
r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);

View File

@ -7,6 +7,7 @@
##
{ 'include': 'common.json' }
{ 'include': 'sockets.json' }
##
# @set_link:
@ -442,6 +443,8 @@
# @vhostdev: path of vhost-vdpa device
# (default:'/dev/vhost-vdpa-0')
#
# @vhostfd: file descriptor of an already opened vhost vdpa device
#
# @queues: number of queues to be created for multiqueue vhost-vdpa
# (default: 1)
#
@ -456,6 +459,7 @@
{ 'struct': 'NetdevVhostVDPAOptions',
'data': {
'*vhostdev': 'str',
'*vhostfd': 'str',
'*queues': 'int',
'*x-svq': {'type': 'bool', 'features' : [ 'unstable'] } } }
@ -573,6 +577,60 @@
'*isolated': 'bool' },
'if': 'CONFIG_VMNET' }
##
# @NetdevStreamOptions:
#
# Configuration info for stream socket netdev
#
# @addr: socket address to listen on (server=true)
# or connect to (server=false)
# @server: create server socket (default: false)
#
# Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
#
# Since: 7.2
##
{ 'struct': 'NetdevStreamOptions',
'data': {
'addr': 'SocketAddress',
'*server': 'bool' } }
##
# @NetdevDgramOptions:
#
# Configuration info for datagram socket netdev.
#
# @remote: remote address
# @local: local address
#
# Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
#
# If remote address is present and it's a multicast address, local address
# is optional. Otherwise local address is required and remote address is
# optional.
#
# .. table:: Valid parameters combination table
# :widths: auto
#
# ============= ======== =====
# remote local okay?
# ============= ======== =====
# absent absent no
# absent not fd no
# absent fd yes
# multicast absent yes
# multicast present yes
# not multicast absent no
# not multicast present yes
# ============= ======== =====
#
# Since: 7.2
##
{ 'struct': 'NetdevDgramOptions',
'data': {
'*local': 'SocketAddress',
'*remote': 'SocketAddress' } }
##
# @NetClientDriver:
#
@ -584,10 +642,13 @@
# @vmnet-host since 7.1
# @vmnet-shared since 7.1
# @vmnet-bridged since 7.1
# @stream since 7.2
# @dgram since 7.2
##
{ 'enum': 'NetClientDriver',
'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde',
'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa',
'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream',
'dgram', 'vde', 'bridge', 'hubport', 'netmap', 'vhost-user',
'vhost-vdpa',
{ 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' },
{ 'name': 'vmnet-shared', 'if': 'CONFIG_VMNET' },
{ 'name': 'vmnet-bridged', 'if': 'CONFIG_VMNET' }] }
@ -607,6 +668,8 @@
# 'vmnet-host' - since 7.1
# 'vmnet-shared' - since 7.1
# 'vmnet-bridged' - since 7.1
# 'stream' since 7.2
# 'dgram' since 7.2
##
{ 'union': 'Netdev',
'base': { 'id': 'str', 'type': 'NetClientDriver' },
@ -617,6 +680,8 @@
'tap': 'NetdevTapOptions',
'l2tpv3': 'NetdevL2TPv3Options',
'socket': 'NetdevSocketOptions',
'stream': 'NetdevStreamOptions',
'dgram': 'NetdevDgramOptions',
'vde': 'NetdevVdeOptions',
'bridge': 'NetdevBridgeOptions',
'hubport': 'NetdevHubPortOptions',
@ -833,3 +898,52 @@
##
{ 'event': 'FAILOVER_NEGOTIATED',
'data': {'device-id': 'str'} }
##
# @NETDEV_STREAM_CONNECTED:
#
# Emitted when the netdev stream backend is connected
#
# @netdev-id: QEMU netdev id that is connected
# @addr: The destination address
#
# Since: 7.2
#
# Example:
#
# <- { "event": "NETDEV_STREAM_CONNECTED",
# "data": { "netdev-id": "netdev0",
# "addr": { "port": "47666", "ipv6": true,
# "host": "::1", "type": "inet" } },
# "timestamp": { "seconds": 1666269863, "microseconds": 311222 } }
#
# or
#
# <- { "event": "NETDEV_STREAM_CONNECTED",
# "data": { "netdev-id": "netdev0",
# "addr": { "path": "/tmp/qemu0", "type": "unix" } },
# "timestamp": { "seconds": 1666269706, "microseconds": 413651 } }
#
##
{ 'event': 'NETDEV_STREAM_CONNECTED',
'data': { 'netdev-id': 'str',
'addr': 'SocketAddress' } }
##
# @NETDEV_STREAM_DISCONNECTED:
#
# Emitted when the netdev stream backend is disconnected
#
# @netdev-id: QEMU netdev id that is disconnected
#
# Since: 7.2
#
# Example:
#
# <- { 'event': 'NETDEV_STREAM_DISCONNECTED',
# 'data': {'netdev-id': 'netdev0'},
# 'timestamp': {'seconds': 1663330937, 'microseconds': 526695} }
#
##
{ 'event': 'NETDEV_STREAM_DISCONNECTED',
'data': { 'netdev-id': 'str' } }

View File

@ -2772,6 +2772,20 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
"-netdev socket,id=str[,fd=h][,udp=host:port][,localaddr=host:port]\n"
" configure a network backend to connect to another network\n"
" using an UDP tunnel\n"
"-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port[,to=maxport][,numeric=on|off][,keep-alive=on|off][,mptcp=on|off][,addr.ipv4=on|off][,addr.ipv6=on|off]\n"
"-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path[,abstract=on|off][,tight=on|off]\n"
"-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=file-descriptor\n"
" configure a network backend to connect to another network\n"
" using a socket connection in stream mode.\n"
"-netdev dgram,id=str,remote.type=inet,remote.host=maddr,remote.port=port[,local.type=inet,local.host=addr]\n"
"-netdev dgram,id=str,remote.type=inet,remote.host=maddr,remote.port=port[,local.type=fd,local.str=file-descriptor]\n"
" configure a network backend to connect to a multicast maddr and port\n"
" use ``local.host=addr`` to specify the host address to send packets from\n"
"-netdev dgram,id=str,local.type=inet,local.host=addr,local.port=port[,remote.type=inet,remote.host=addr,remote.port=port]\n"
"-netdev dgram,id=str,local.type=unix,local.path=path[,remote.type=unix,remote.path=path]\n"
"-netdev dgram,id=str,local.type=fd,local.str=file-descriptor\n"
" configure a network backend to connect to another network\n"
" using an UDP tunnel\n"
#ifdef CONFIG_VDE
"-netdev vde,id=str[,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
" configure a network backend to connect to port 'n' of a vde switch\n"
@ -2790,8 +2804,10 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
" configure a vhost-user network, backed by a chardev 'dev'\n"
#endif
#ifdef __linux__
"-netdev vhost-vdpa,id=str,vhostdev=/path/to/dev\n"
"-netdev vhost-vdpa,id=str[,vhostdev=/path/to/dev][,vhostfd=h]\n"
" configure a vhost-vdpa network,Establish a vhost-vdpa netdev\n"
" use 'vhostdev=/path/to/dev' to open a vhost vdpa device\n"
" use 'vhostfd=h' to connect to an already opened vhost vdpa device\n"
#endif
#ifdef CONFIG_VMNET
"-netdev vmnet-host,id=str[,isolated=on|off][,net-uuid=uuid]\n"
@ -3296,7 +3312,7 @@ SRST
-netdev type=vhost-user,id=net0,chardev=chr0 \
-device virtio-net-pci,netdev=net0
``-netdev vhost-vdpa,vhostdev=/path/to/dev``
``-netdev vhost-vdpa[,vhostdev=/path/to/dev][,vhostfd=h]``
Establish a vhost-vdpa netdev.
vDPA device is a device that uses a datapath which complies with

View File

@ -1904,7 +1904,7 @@ static void qemu_create_late_backends(void)
qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
}
net_init_clients(&error_fatal);
net_init_clients();
object_option_foreach_add(object_create_late);
@ -2801,21 +2801,19 @@ void qemu_init(int argc, char **argv)
break;
case QEMU_OPTION_netdev:
default_net = 0;
if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
exit(1);
if (netdev_is_modern(optarg)) {
netdev_parse_modern(optarg);
} else {
net_client_parse(qemu_find_opts("netdev"), optarg);
}
break;
case QEMU_OPTION_nic:
default_net = 0;
if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
exit(1);
}
net_client_parse(qemu_find_opts("nic"), optarg);
break;
case QEMU_OPTION_net:
default_net = 0;
if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
exit(1);
}
net_client_parse(qemu_find_opts("net"), optarg);
break;
#ifdef CONFIG_LIBISCSI
case QEMU_OPTION_iscsi:

View File

@ -1076,6 +1076,26 @@ int unix_connect(const char *path, Error **errp)
return sock;
}
char *socket_uri(SocketAddress *addr)
{
switch (addr->type) {
case SOCKET_ADDRESS_TYPE_INET:
return g_strdup_printf("tcp:%s:%s",
addr->u.inet.host,
addr->u.inet.port);
case SOCKET_ADDRESS_TYPE_UNIX:
return g_strdup_printf("unix:%s",
addr->u.q_unix.path);
case SOCKET_ADDRESS_TYPE_FD:
return g_strdup_printf("fd:%s", addr->u.fd.str);
case SOCKET_ADDRESS_TYPE_VSOCK:
return g_strdup_printf("vsock:%s:%s",
addr->u.vsock.cid,
addr->u.vsock.port);
default:
return g_strdup("unknown address type");
}
}
SocketAddress *socket_parse(const char *str, Error **errp)
{
@ -1103,6 +1123,11 @@ SocketAddress *socket_parse(const char *str, Error **errp)
if (vsock_parse(&addr->u.vsock, str + strlen("vsock:"), errp)) {
goto fail;
}
} else if (strstart(str, "tcp:", NULL)) {
addr->type = SOCKET_ADDRESS_TYPE_INET;
if (inet_parse(&addr->u.inet, str + strlen("tcp:"), errp)) {
goto fail;
}
} else {
addr->type = SOCKET_ADDRESS_TYPE_INET;
if (inet_parse(&addr->u.inet, str, errp)) {