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:01 -04:00
25 changed files with 1480 additions and 217 deletions
+1 -1
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
+51 -8
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)
+2 -3
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);
+31 -8
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());
}
+5 -2
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);
+3 -1
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 */
+1 -22
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);
}
+6
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
View File
File diff suppressed because it is too large Load Diff
+2
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;
+1 -2
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);
+2
View File
@@ -13,6 +13,8 @@ softmmu_ss.add(files(
'net.c',
'queue.c',
'socket.c',
'stream.c',
'dgram.c',
'util.c',
))
+154 -64
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 */
+2 -3
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);
+15 -21
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
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);
}
+1 -2
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;
+5 -8
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);
+1 -2
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);
+1 -2
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;

Some files were not shown because too many files have changed in this diff Show More