#include "qemu/cutils.h"
#include "qemu/config-file.h"
#include "qemu/ctype.h"
+#include "qemu/id.h"
#include "qemu/iov.h"
#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/sysemu.h"
#include "sysemu/runstate.h"
-#include "sysemu/sysemu.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)
#endif
static VMChangeStateEntry *net_change_state_entry;
-static QTAILQ_HEAD(, NetClientState) net_clients;
+NetClientStateList 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 parse_host_port(struct sockaddr_in *saddr, const char *str,
- Error **errp)
+int convert_host_port(struct sockaddr_in *saddr, const char *host,
+ const char *port, Error **errp)
{
- gchar **substrings;
struct hostent *he;
- const char *addr, *p, *r;
- int port, ret = 0;
+ const char *r;
+ long p;
- substrings = g_strsplit(str, ":", 2);
- if (!substrings || !substrings[0] || !substrings[1]) {
- error_setg(errp, "host address '%s' doesn't contain ':' "
- "separating host from port", str);
- ret = -1;
- goto out;
- }
-
- addr = substrings[0];
- p = substrings[1];
+ memset(saddr, 0, sizeof(*saddr));
saddr->sin_family = AF_INET;
- if (addr[0] == '\0') {
+ if (host[0] == '\0') {
saddr->sin_addr.s_addr = 0;
} else {
- if (qemu_isdigit(addr[0])) {
- if (!inet_aton(addr, &saddr->sin_addr)) {
+ if (qemu_isdigit(host[0])) {
+ if (!inet_aton(host, &saddr->sin_addr)) {
error_setg(errp, "host address '%s' is not a valid "
- "IPv4 address", addr);
- ret = -1;
- goto out;
+ "IPv4 address", host);
+ return -1;
}
} else {
- he = gethostbyname(addr);
+ he = gethostbyname(host);
if (he == NULL) {
- error_setg(errp, "can't resolve host address '%s'", addr);
- ret = -1;
- goto out;
+ error_setg(errp, "can't resolve host address '%s'", host);
+ return -1;
}
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);
+ 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;
+ int ret;
+
+ substrings = g_strsplit(str, ":", 2);
+ if (!substrings || !substrings[0] || !substrings[1]) {
+ error_setg(errp, "host address '%s' doesn't contain ':' "
+ "separating host from port", str);
ret = -1;
goto out;
}
- saddr->sin_port = htons(port);
+
+ ret = convert_host_port(saddr, substrings[0], substrings[1], errp);
out:
g_strfreev(substrings);
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};
NetClientState *peer,
const char *model,
const char *name,
- NetClientDestructor *destructor)
+ NetClientDestructor *destructor,
+ bool is_datapath)
{
nc->info = info;
nc->model = g_strdup(model);
nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
nc->destructor = destructor;
+ nc->is_datapath = is_datapath;
QTAILQ_INIT(&nc->filters);
}
nc = g_malloc0(info->size);
qemu_net_client_setup(nc, info, peer, model, name,
- qemu_net_client_destructor);
+ qemu_net_client_destructor, true);
+
+ return nc;
+}
+
+NetClientState *qemu_new_net_control_client(NetClientInfo *info,
+ NetClientState *peer,
+ const char *model,
+ const char *name)
+{
+ NetClientState *nc;
+
+ assert(info->size >= sizeof(NetClientState));
+
+ nc = g_malloc0(info->size);
+ qemu_net_client_setup(nc, info, peer, model, name,
+ qemu_net_client_destructor, false);
return nc;
}
for (i = 0; i < queues; i++) {
qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
- NULL);
+ NULL, true);
nic->ncs[i].queue_index = i;
}
return nc->info->has_ufo(nc);
}
+bool qemu_has_uso(NetClientState *nc)
+{
+ if (!nc || !nc->info->has_uso) {
+ return false;
+ }
+
+ return nc->info->has_uso(nc);
+}
+
bool qemu_has_vnet_hdr(NetClientState *nc)
{
if (!nc || !nc->info->has_vnet_hdr) {
return nc->info->has_vnet_hdr_len(nc, len);
}
+bool qemu_get_using_vnet_hdr(NetClientState *nc)
+{
+ if (!nc || !nc->info->get_using_vnet_hdr) {
+ return false;
+ }
+
+ return nc->info->get_using_vnet_hdr(nc);
+}
+
void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
{
if (!nc || !nc->info->using_vnet_hdr) {
}
void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
- int ecn, int ufo)
+ int ecn, int ufo, int uso4, int uso6)
{
if (!nc || !nc->info->set_offload) {
return;
}
- nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
+ nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo, uso4, uso6);
+}
+
+int qemu_get_vnet_hdr_len(NetClientState *nc)
+{
+ if (!nc || !nc->info->get_vnet_hdr_len) {
+ return 0;
+ }
+
+ return nc->info->get_vnet_hdr_len(nc);
}
void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
int qemu_set_vnet_le(NetClientState *nc, bool is_le)
{
-#ifdef HOST_WORDS_BIGENDIAN
+#if HOST_BIG_ENDIAN
if (!nc || !nc->info->set_vnet_le) {
return -ENOSYS;
}
int qemu_set_vnet_be(NetClientState *nc, bool is_be)
{
-#ifdef HOST_WORDS_BIGENDIAN
+#if HOST_BIG_ENDIAN
return 0;
#else
if (!nc || !nc->info->set_vnet_be) {
#endif
}
+int qemu_can_receive_packet(NetClientState *nc)
+{
+ if (nc->receive_disabled) {
+ return 0;
+ } else if (nc->info->can_receive &&
+ !nc->info->can_receive(nc)) {
+ return 0;
+ }
+ return 1;
+}
+
int qemu_can_send_packet(NetClientState *sender)
{
int vm_running = runstate_is_running();
return 1;
}
- if (sender->peer->receive_disabled) {
- return 0;
- } else if (sender->peer->info->can_receive &&
- !sender->peer->info->can_receive(sender->peer)) {
- return 0;
- }
- return 1;
+ return qemu_can_receive_packet(sender->peer);
}
static ssize_t filter_receive_iov(NetClientState *nc,
return qemu_send_packet_async(nc, buf, size, NULL);
}
+ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size)
+{
+ if (!qemu_can_receive_packet(nc)) {
+ return 0;
+ }
+
+ return qemu_net_queue_receive(nc->incoming_queue, buf, size);
+}
+
+ssize_t qemu_receive_packet_iov(NetClientState *nc, const struct iovec *iov,
+ int iovcnt)
+{
+ if (!qemu_can_receive_packet(nc)) {
+ return 0;
+ }
+
+ return qemu_net_queue_receive_iov(nc->incoming_queue, iov, iovcnt);
+}
+
ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
{
return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
return -1;
}
+GPtrArray *qemu_get_nic_models(const char *device_type)
+{
+ GPtrArray *nic_models = g_ptr_array_new();
+ GSList *list = object_class_get_list_sorted(device_type, false);
+
+ while (list) {
+ DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
+ TYPE_DEVICE);
+ GSList *next;
+ if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
+ dc->user_creatable) {
+ const char *name = object_class_get_name(list->data);
+ /*
+ * A network device might also be something else than a NIC, see
+ * e.g. the "rocker" device. Thus we have to look for the "netdev"
+ * property, too. Unfortunately, some devices like virtio-net only
+ * create this property during instance_init, so we have to create
+ * a temporary instance here to be able to check it.
+ */
+ Object *obj = object_new_with_class(OBJECT_CLASS(dc));
+ if (object_property_find(obj, "netdev")) {
+ g_ptr_array_add(nic_models, (gpointer)name);
+ }
+ object_unref(obj);
+ }
+ next = list->next;
+ g_slist_free_1(list);
+ list = next;
+ }
+ g_ptr_array_add(nic_models, NULL);
+
+ return nic_models;
+}
+
int qemu_show_nic_models(const char *arg, const char *const *models)
{
int i;
return 0;
}
- printf("Supported NIC models:\n");
+ printf("Available NIC models:\n");
for (i = 0 ; models[i]; i++) {
printf("%s\n", models[i]);
}
memset(nd, 0, sizeof(*nd));
- if (nic->has_netdev) {
+ if (nic->netdev) {
nd->netdev = qemu_find_netdev(nic->netdev);
if (!nd->netdev) {
error_setg(errp, "netdev '%s' not found", nic->netdev);
nd->netdev = peer;
}
nd->name = g_strdup(name);
- if (nic->has_model) {
+ if (nic->model) {
nd->model = g_strdup(nic->model);
}
- if (nic->has_addr) {
+ if (nic->addr) {
nd->devaddr = g_strdup(nic->addr);
}
- if (nic->has_macaddr &&
+ if (nic->macaddr &&
net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
error_setg(errp, "invalid syntax for ethernet address");
return -1;
}
- if (nic->has_macaddr &&
+ if (nic->macaddr &&
is_multicast_ether_addr(nd->macaddr.a)) {
error_setg(errp,
"NIC cannot have multicast MAC address (odd 1st byte)");
#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
#ifdef CONFIG_NETMAP
[NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
#endif
+#ifdef CONFIG_AF_XDP
+ [NET_CLIENT_DRIVER_AF_XDP] = net_init_af_xdp,
+#endif
#ifdef CONFIG_NET_BRIDGE
[NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
#endif
#ifdef CONFIG_L2TPV3
[NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
#endif
+#ifdef CONFIG_VMNET
+ [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host,
+ [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared,
+ [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged,
+#endif /* CONFIG_VMNET */
};
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;
}
/* Do not add to a hub if it's a nic with a netdev= parameter. */
if (netdev->type != NET_CLIENT_DRIVER_NIC ||
- !netdev->u.nic.has_netdev) {
+ !netdev->u.nic.netdev) {
peer = net_hub_add_port(0, NULL, NULL);
}
}
int idx;
const char *available_netdevs[] = {
"socket",
+ "stream",
+ "dgram",
"hubport",
"tap",
#ifdef CONFIG_SLIRP
#ifdef CONFIG_NETMAP
"netmap",
#endif
+#ifdef CONFIG_AF_XDP
+ "af-xdp",
+#endif
#ifdef CONFIG_POSIX
"vhost-user",
#endif
#ifdef CONFIG_VHOST_VDPA
"vhost-vdpa",
+#endif
+#ifdef CONFIG_VMNET
+ "vmnet-host",
+ "vmnet-shared",
+ "vmnet-bridged",
#endif
};
/* Create an ID for -net if the user did not specify one */
if (!is_netdev && !qemu_opts_id(opts)) {
- static int idx;
- qemu_opts_set_id(opts, g_strdup_printf("__org.qemu.net%i", idx++));
+ qemu_opts_set_id(opts, id_generate(ID_NET));
}
if (visit_type_Netdev(v, NULL, &object, errp)) {
void qmp_netdev_add(Netdev *netdev, Error **errp)
{
+ if (!id_wellformed(netdev->id)) {
+ error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
+ return;
+ }
+
net_client_init1(netdev, true, errp);
}
}
}
-RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
- Error **errp)
+RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp)
{
NetClientState *nc;
RxFilterInfoList *filter_list = NULL, **tail = &filter_list;
QTAILQ_FOREACH(nc, &net_clients, next) {
RxFilterInfo *info;
- if (has_name && strcmp(nc->name, name) != 0) {
+ if (name && strcmp(nc->name, name) != 0) {
continue;
}
/* only query rx-filter information of NIC */
if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
- if (has_name) {
+ if (name) {
error_setg(errp, "net client(%s) isn't a NIC", name);
assert(!filter_list);
return NULL;
if (nc->info->query_rx_filter) {
info = nc->info->query_rx_filter(nc);
QAPI_LIST_APPEND(tail, info);
- } else if (has_name) {
+ } else if (name) {
error_setg(errp, "net client(%s) doesn't support"
" rx-filter querying", name);
assert(!filter_list);
return NULL;
}
- if (has_name) {
+ if (name) {
break;
}
}
- if (filter_list == NULL && has_name) {
+ if (filter_list == NULL && name) {
error_setg(errp, "invalid net client name: %s", name);
}
return filter_list;
}
-void hmp_info_network(Monitor *mon, const QDict *qdict)
-{
- NetClientState *nc, *peer;
- NetClientDriver type;
-
- net_hub_info(mon);
-
- QTAILQ_FOREACH(nc, &net_clients, next) {
- peer = nc->peer;
- type = nc->info->type;
-
- /* Skip if already printed in hub info */
- if (net_hub_id_for_client(nc, NULL) == 0) {
- continue;
- }
-
- if (!peer || type == NET_CLIENT_DRIVER_NIC) {
- print_net_client(mon, nc);
- } /* else it's a netdev connected to a NIC, printed with the NIC */
- if (peer && type == NET_CLIENT_DRIVER_NIC) {
- monitor_printf(mon, " \\ ");
- print_net_client(mon, peer);
- }
- }
-}
-
void colo_notify_filters_event(int event, Error **errp)
{
NetClientState *nc;
}
}
-static void net_vm_change_state_handler(void *opaque, int running,
+static void net_vm_change_state_handler(void *opaque, bool running,
RunState state)
{
NetClientState *nc;
{
NetClientState *nc;
+ /*cleanup colo compare module for COLO*/
+ colo_compare_cleanup();
+
/* We may del multiple entries during qemu_del_net_client(),
* so QTAILQ_FOREACH_SAFE() is also not safe here.
*/
const char *type;
type = qemu_opt_get(opts, "type");
- if (type && g_str_equal(type, "none")) {
- return 0; /* Nothing to do, default_net is cleared in vl.c */
+ if (type) {
+ if (g_str_equal(type, "none")) {
+ return 0; /* Nothing to do, default_net is cleared in vl.c */
+ }
+ if (is_help_option(type)) {
+ GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
+ show_netdevs();
+ printf("\n");
+ qemu_show_nic_models(type, (const char **)nic_models->pdata);
+ g_ptr_array_free(nic_models, true);
+ exit(0);
+ }
}
idx = nic_get_free_idx();
/* Create an ID if the user did not specify one */
nd_id = g_strdup(qemu_opts_id(opts));
if (!nd_id) {
- nd_id = g_strdup_printf("__org.qemu.nic%i", idx);
+ nd_id = id_generate(ID_NET);
qemu_opts_set_id(opts, nd_id);
}
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);
+
+ qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL,
+ &error_fatal);
+}
+
+/*
+ * 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)
+{
+ 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);
}
-int net_client_parse(QemuOptsList *opts_list, const char *optarg)
+void net_client_parse(QemuOptsList *opts_list, const char *optarg)
{
if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
- return -1;
+ exit(1);
}
-
- return 0;
}
/* From FreeBSD */