#include "monitor.h"
#include "qemu-common.h"
#include "qemu_socket.h"
+#include "qmp-commands.h"
#include "hw/qdev.h"
#include "iov.h"
+/* Net bridge is currently not supported for W32. */
+#if !defined(_WIN32)
+# define CONFIG_NET_BRIDGE
+#endif
+
static QTAILQ_HEAD(, VLANState) vlans;
static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
static char *assign_name(VLANClientState *vc1, const char *model)
{
VLANState *vlan;
+ VLANClientState *vc;
char buf[256];
int id = 0;
QTAILQ_FOREACH(vlan, &vlans, next) {
- VLANClientState *vc;
-
QTAILQ_FOREACH(vc, &vlan->clients, next) {
if (vc != vc1 && strcmp(vc->model, model) == 0) {
id++;
}
}
+ QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
+ if (vc != vc1 && strcmp(vc->model, model) == 0) {
+ id++;
+ }
+ }
+
snprintf(buf, sizeof(buf), "%s.%d", model, id);
- return qemu_strdup(buf);
+ return g_strdup(buf);
}
static ssize_t qemu_deliver_packet(VLANClientState *sender,
assert(info->size >= sizeof(VLANClientState));
- vc = qemu_mallocz(info->size);
+ vc = g_malloc0(info->size);
vc->info = info;
- vc->model = qemu_strdup(model);
+ vc->model = g_strdup(model);
if (name) {
- vc->name = qemu_strdup(name);
+ vc->name = g_strdup(name);
} else {
vc->name = assign_name(vc, model);
}
vc->peer->peer = NULL;
}
}
- qemu_free(vc->name);
- qemu_free(vc->model);
- qemu_free(vc);
+ g_free(vc->name);
+ g_free(vc->model);
+ g_free(vc);
}
void qemu_del_vlan_client(VLANClientState *vc)
return NULL;
}
- vlan = qemu_mallocz(sizeof(VLANState));
+ vlan = g_malloc0(sizeof(VLANState));
vlan->id = id;
QTAILQ_INIT(&vlan->clients);
VLANClientState *vc;
QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
+ if (vc->info->type == NET_CLIENT_TYPE_NIC)
+ continue;
if (!strcmp(vc->name, id)) {
return vc;
}
int i;
if (!nd->model)
- nd->model = qemu_strdup(default_model);
+ nd->model = g_strdup(default_model);
for (i = 0 ; models[i]; i++) {
if (strcmp(nd->model, models[i]) == 0)
return i;
}
- error_report("qemu: Unsupported NIC model: %s", nd->model);
+ error_report("Unsupported NIC model: %s", nd->model);
return -1;
}
return -1;
}
} else {
- char *endptr = NULL;
-
- fd = strtol(param, &endptr, 10);
- if (*endptr || (fd == 0 && param == endptr)) {
- return -1;
- }
+ fd = qemu_parse_fd(param);
}
return fd;
}
-static int net_init_nic(QemuOpts *opts,
- Monitor *mon,
- const char *name,
- VLANState *vlan)
+static int net_init_nic(QemuOpts *opts, const char *name, VLANState *vlan)
{
int idx;
NICInfo *nd;
nd->vlan = vlan;
}
if (name) {
- nd->name = qemu_strdup(name);
+ nd->name = g_strdup(name);
}
if (qemu_opt_get(opts, "model")) {
- nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
+ nd->model = g_strdup(qemu_opt_get(opts, "model"));
}
if (qemu_opt_get(opts, "addr")) {
- nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
+ nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
}
- nd->macaddr[0] = 0x52;
- nd->macaddr[1] = 0x54;
- nd->macaddr[2] = 0x00;
- nd->macaddr[3] = 0x12;
- nd->macaddr[4] = 0x34;
- nd->macaddr[5] = 0x56 + idx;
-
if (qemu_opt_get(opts, "macaddr") &&
- net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
+ net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
error_report("invalid syntax for ethernet address");
return -1;
}
+ qemu_macaddr_default_if_unset(&nd->macaddr);
nd->nvectors = qemu_opt_get_number(opts, "vectors",
DEV_NVECTORS_UNSPECIFIED);
}
typedef int (*net_client_init_func)(QemuOpts *opts,
- Monitor *mon,
const char *name,
VLANState *vlan);
const char *type;
net_client_init_func init;
QemuOptDesc desc[NET_MAX_DESC];
-} net_client_types[] = {
- {
+} net_client_types[NET_CLIENT_TYPE_MAX] = {
+ [NET_CLIENT_TYPE_NONE] = {
.type = "none",
.desc = {
NET_COMMON_PARAMS_DESC,
{ /* end of list */ }
},
- }, {
+ },
+ [NET_CLIENT_TYPE_NIC] = {
.type = "nic",
.init = net_init_nic,
.desc = {
},
{ /* end of list */ }
},
+ },
#ifdef CONFIG_SLIRP
- }, {
+ [NET_CLIENT_TYPE_USER] = {
.type = "user",
.init = net_init_slirp,
.desc = {
},
{ /* end of list */ }
},
+ },
#endif
- }, {
+ [NET_CLIENT_TYPE_TAP] = {
.type = "tap",
.init = net_init_tap,
.desc = {
.type = QEMU_OPT_STRING,
.help = "script to shut down the interface",
}, {
+#ifdef CONFIG_NET_BRIDGE
+ .name = "helper",
+ .type = QEMU_OPT_STRING,
+ .help = "command to execute to configure bridge",
+ }, {
+#endif
.name = "sndbuf",
.type = QEMU_OPT_SIZE,
.help = "send buffer limit"
#endif /* _WIN32 */
{ /* end of list */ }
},
- }, {
+ },
+ [NET_CLIENT_TYPE_SOCKET] = {
.type = "socket",
.init = net_init_socket,
.desc = {
}, {
.name = "localaddr",
.type = QEMU_OPT_STRING,
- .help = "source address for multicast packets",
+ .help = "source address and port for multicast and udp packets",
+ }, {
+ .name = "udp",
+ .type = QEMU_OPT_STRING,
+ .help = "UDP unicast address and port number",
},
{ /* end of list */ }
},
+ },
#ifdef CONFIG_VDE
- }, {
+ [NET_CLIENT_TYPE_VDE] = {
.type = "vde",
.init = net_init_vde,
.desc = {
},
{ /* end of list */ }
},
+ },
#endif
- }, {
+ [NET_CLIENT_TYPE_DUMP] = {
.type = "dump",
.init = net_init_dump,
.desc = {
{ /* end of list */ }
},
},
- { /* end of list */ }
+#ifdef CONFIG_NET_BRIDGE
+ [NET_CLIENT_TYPE_BRIDGE] = {
+ .type = "bridge",
+ .init = net_init_bridge,
+ .desc = {
+ NET_COMMON_PARAMS_DESC,
+ {
+ .name = "br",
+ .type = QEMU_OPT_STRING,
+ .help = "bridge name",
+ }, {
+ .name = "helper",
+ .type = QEMU_OPT_STRING,
+ .help = "command to execute to configure bridge",
+ },
+ { /* end of list */ }
+ },
+ },
+#endif /* CONFIG_NET_BRIDGE */
};
-int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
+int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
{
const char *name;
const char *type;
type = qemu_opt_get(opts, "type");
if (!type) {
- qerror_report(QERR_MISSING_PARAMETER, "type");
+ error_set(errp, QERR_MISSING_PARAMETER, "type");
return -1;
}
if (is_netdev) {
if (strcmp(type, "tap") != 0 &&
+#ifdef CONFIG_NET_BRIDGE
+ strcmp(type, "bridge") != 0 &&
+#endif
#ifdef CONFIG_SLIRP
strcmp(type, "user") != 0 &&
#endif
strcmp(type, "vde") != 0 &&
#endif
strcmp(type, "socket") != 0) {
- qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
- "a netdev backend type");
+ error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
+ "a netdev backend type");
return -1;
}
if (qemu_opt_get(opts, "vlan")) {
- qerror_report(QERR_INVALID_PARAMETER, "vlan");
+ error_set(errp, QERR_INVALID_PARAMETER, "vlan");
return -1;
}
if (qemu_opt_get(opts, "name")) {
- qerror_report(QERR_INVALID_PARAMETER, "name");
+ error_set(errp, QERR_INVALID_PARAMETER, "name");
return -1;
}
if (!qemu_opts_id(opts)) {
- qerror_report(QERR_MISSING_PARAMETER, "id");
+ error_set(errp, QERR_MISSING_PARAMETER, "id");
return -1;
}
}
name = qemu_opt_get(opts, "name");
}
- for (i = 0; net_client_types[i].type != NULL; i++) {
- if (!strcmp(net_client_types[i].type, type)) {
+ for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
+ if (net_client_types[i].type != NULL &&
+ !strcmp(net_client_types[i].type, type)) {
+ Error *local_err = NULL;
VLANState *vlan = NULL;
int ret;
- if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
+ qemu_opts_validate(opts, &net_client_types[i].desc[0], &local_err);
+ if (error_is_set(&local_err)) {
+ error_propagate(errp, local_err);
return -1;
}
ret = 0;
if (net_client_types[i].init) {
- ret = net_client_types[i].init(opts, mon, name, vlan);
+ ret = net_client_types[i].init(opts, name, vlan);
if (ret < 0) {
/* TODO push error reporting into init() methods */
- qerror_report(QERR_DEVICE_INIT_FAILED, type);
+ error_set(errp, QERR_DEVICE_INIT_FAILED, type);
return -1;
}
}
}
}
- qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
- "a network client type");
+ error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
+ "a network client type");
return -1;
}
{
int i;
const char *valid_param_list[] = { "tap", "socket", "dump"
+#ifdef CONFIG_NET_BRIDGE
+ , "bridge"
+#endif
#ifdef CONFIG_SLIRP
,"user"
#endif
{
const char *device = qdict_get_str(qdict, "device");
const char *opts_str = qdict_get_try_str(qdict, "opts");
+ Error *local_err = NULL;
QemuOpts *opts;
if (!net_host_check_device(device)) {
qemu_opt_set(opts, "type", device);
- if (net_client_init(mon, opts, 0) < 0) {
+ net_client_init(opts, 0, &local_err);
+ if (error_is_set(&local_err)) {
+ qerror_report_err(local_err);
+ error_free(local_err);
monitor_printf(mon, "adding host network device %s failed\n", device);
}
}
qemu_del_vlan_client(vc);
}
-int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
+void netdev_add(QemuOpts *opts, Error **errp)
+{
+ net_client_init(opts, 1, errp);
+}
+
+int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
{
+ Error *local_err = NULL;
+ QemuOptsList *opts_list;
QemuOpts *opts;
- int res;
- opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
- if (!opts) {
- return -1;
+ opts_list = qemu_find_opts_err("netdev", &local_err);
+ if (error_is_set(&local_err)) {
+ goto exit_err;
}
- res = net_client_init(mon, opts, 1);
- if (res < 0) {
+ opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
+ if (error_is_set(&local_err)) {
+ goto exit_err;
+ }
+
+ netdev_add(opts, &local_err);
+ if (error_is_set(&local_err)) {
qemu_opts_del(opts);
+ goto exit_err;
}
- return res;
+ return 0;
+
+exit_err:
+ qerror_report_err(local_err);
+ error_free(local_err);
+ return -1;
}
-int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
+void qmp_netdev_del(const char *id, Error **errp)
{
- const char *id = qdict_get_str(qdict, "id");
VLANClientState *vc;
vc = qemu_find_netdev(id);
- if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) {
- qerror_report(QERR_DEVICE_NOT_FOUND, id);
- return -1;
+ if (!vc) {
+ error_set(errp, QERR_DEVICE_NOT_FOUND, id);
+ return;
}
+
qemu_del_vlan_client(vc);
- qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
- return 0;
+ qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp), id));
+}
+
+static void print_net_client(Monitor *mon, VLANClientState *vc)
+{
+ monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
+ net_client_types[vc->info->type].type, vc->info_str);
}
void do_info_network(Monitor *mon)
{
VLANState *vlan;
- VLANClientState *vc;
+ VLANClientState *vc, *peer;
+ net_client_type type;
QTAILQ_FOREACH(vlan, &vlans, next) {
monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
QTAILQ_FOREACH(vc, &vlan->clients, next) {
- monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
+ monitor_printf(mon, " ");
+ print_net_client(mon, vc);
}
}
monitor_printf(mon, "Devices not on any VLAN:\n");
QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
- monitor_printf(mon, " %s: %s", vc->name, vc->info_str);
- if (vc->peer) {
- monitor_printf(mon, " peer=%s", vc->peer->name);
+ peer = vc->peer;
+ type = vc->info->type;
+ if (!peer || type == NET_CLIENT_TYPE_NIC) {
+ monitor_printf(mon, " ");
+ print_net_client(mon, vc);
+ } /* else it's a netdev connected to a NIC, printed with the NIC */
+ if (peer && type == NET_CLIENT_TYPE_NIC) {
+ monitor_printf(mon, " \\ ");
+ print_net_client(mon, peer);
}
- monitor_printf(mon, "\n");
}
}
-int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
+void qmp_set_link(const char *name, bool up, Error **errp)
{
VLANState *vlan;
VLANClientState *vc = NULL;
- const char *name = qdict_get_str(qdict, "name");
- int up = qdict_get_bool(qdict, "up");
QTAILQ_FOREACH(vlan, &vlans, next) {
QTAILQ_FOREACH(vc, &vlan->clients, next) {
}
}
}
- vc = qemu_find_netdev(name);
+ QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
+ if (!strcmp(vc->name, name)) {
+ goto done;
+ }
+ }
done:
if (!vc) {
- qerror_report(QERR_DEVICE_NOT_FOUND, name);
- return -1;
+ error_set(errp, QERR_DEVICE_NOT_FOUND, name);
+ return;
}
vc->link_down = !up;
if (vc->peer && vc->peer->info->link_status_changed) {
vc->peer->info->link_status_changed(vc->peer);
}
- return 0;
}
void net_cleanup(void)
case NET_CLIENT_TYPE_NIC:
has_nic = 1;
break;
- case NET_CLIENT_TYPE_SLIRP:
+ case NET_CLIENT_TYPE_USER:
case NET_CLIENT_TYPE_TAP:
case NET_CLIENT_TYPE_SOCKET:
case NET_CLIENT_TYPE_VDE:
static int net_init_client(QemuOpts *opts, void *dummy)
{
- if (net_client_init(NULL, opts, 0) < 0)
+ Error *local_err = NULL;
+
+ net_client_init(opts, 0, &local_err);
+ if (error_is_set(&local_err)) {
+ qerror_report_err(local_err);
+ error_free(local_err);
return -1;
+ }
+
return 0;
}
static int net_init_netdev(QemuOpts *opts, void *dummy)
{
- return net_client_init(NULL, opts, 1);
+ Error *local_err = NULL;
+ int ret;
+
+ ret = net_client_init(opts, 1, &local_err);
+ if (error_is_set(&local_err)) {
+ qerror_report_err(local_err);
+ error_free(local_err);
+ return -1;
+ }
+
+ return ret;
}
int net_init_clients(void)
default_net = 0;
return 0;
}
+
+/* From FreeBSD */
+/* XXX: optimize */
+unsigned compute_mcast_idx(const uint8_t *ep)
+{
+ uint32_t crc;
+ int carry, i, j;
+ uint8_t b;
+
+ crc = 0xffffffff;
+ for (i = 0; i < 6; i++) {
+ b = *ep++;
+ for (j = 0; j < 8; j++) {
+ carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
+ crc <<= 1;
+ b >>= 1;
+ if (carry) {
+ crc = ((crc ^ POLYNOMIAL) | carry);
+ }
+ }
+ }
+ return crc >> 26;
+}