#include <sys/time.h>
#include <zlib.h>
-/* Needed early for HOST_BSD etc. */
+/* Needed early for CONFIG_BSD etc. */
#include "config-host.h"
#ifndef _WIN32
#include <dirent.h>
#include <netdb.h>
#include <sys/select.h>
-#ifdef HOST_BSD
+#ifdef CONFIG_BSD
#include <sys/stat.h>
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <libutil.h>
#include <libvdeplug.h>
#endif
-#ifdef _WIN32
-#include <windows.h>
-#include <malloc.h>
-#include <sys/timeb.h>
-#include <mmsystem.h>
-#define getopt_long_only getopt_long
-#define memalign(align, size) malloc(size)
-#endif
-
#include "qemu-common.h"
#include "net.h"
#include "monitor.h"
#include "qemu-log.h"
#include "slirp/libslirp.h"
+#include "qemu-queue.h"
static VLANState *first_vlan;
return 0;
}
-#if !defined(_WIN32) && 0
-static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
-{
- const char *p;
- int len;
-
- len = MIN(108, strlen(str));
- p = strchr(str, ',');
- if (p)
- len = MIN(len, p - str);
-
- memset(uaddr, 0, sizeof(*uaddr));
-
- uaddr->sun_family = AF_UNIX;
- memcpy(uaddr->sun_path, str, len);
-
- return 0;
-}
-#endif
-
void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
{
snprintf(vc->info_str, sizeof(vc->info_str),
void qemu_purge_queued_packets(VLANClientState *vc)
{
- VLANPacket **pp = &vc->vlan->send_queue;
-
- while (*pp != NULL) {
- VLANPacket *packet = *pp;
+ VLANPacket *packet, *next;
+ QTAILQ_FOREACH_SAFE(packet, &vc->vlan->send_queue, entry, next) {
if (packet->sender == vc) {
- *pp = packet->next;
+ QTAILQ_REMOVE(&vc->vlan->send_queue, packet, entry);
qemu_free(packet);
- } else {
- pp = &packet->next;
}
}
}
void qemu_flush_queued_packets(VLANClientState *vc)
{
- VLANPacket *packet;
-
- while ((packet = vc->vlan->send_queue) != NULL) {
+ while (!QTAILQ_EMPTY(&vc->vlan->send_queue)) {
+ VLANPacket *packet;
int ret;
- vc->vlan->send_queue = packet->next;
+ packet = QTAILQ_FIRST(&vc->vlan->send_queue);
+ QTAILQ_REMOVE(&vc->vlan->send_queue, packet, entry);
ret = qemu_deliver_packet(packet->sender, packet->data, packet->size);
if (ret == 0 && packet->sent_cb != NULL) {
- packet->next = vc->vlan->send_queue;
- vc->vlan->send_queue = packet;
+ QTAILQ_INSERT_HEAD(&vc->vlan->send_queue, packet, entry);
break;
}
VLANPacket *packet;
packet = qemu_malloc(sizeof(VLANPacket) + size);
- packet->next = sender->vlan->send_queue;
packet->sender = sender;
packet->size = size;
packet->sent_cb = sent_cb;
memcpy(packet->data, buf, size);
- sender->vlan->send_queue = packet;
+
+ QTAILQ_INSERT_TAIL(&sender->vlan->send_queue, packet, entry);
}
ssize_t qemu_send_packet_async(VLANClientState *sender,
max_len = calc_iov_length(iov, iovcnt);
packet = qemu_malloc(sizeof(VLANPacket) + max_len);
- packet->next = sender->vlan->send_queue;
packet->sender = sender;
packet->sent_cb = sent_cb;
packet->size = 0;
packet->size += len;
}
- sender->vlan->send_queue = packet;
+ QTAILQ_INSERT_TAIL(&sender->vlan->send_queue, packet, entry);
return packet->size;
}
};
typedef struct SlirpState {
- TAILQ_ENTRY(SlirpState) entry;
+ QTAILQ_ENTRY(SlirpState) entry;
VLANClientState *vc;
Slirp *slirp;
#ifndef _WIN32
static struct slirp_config_str *slirp_configs;
const char *legacy_tftp_prefix;
const char *legacy_bootp_filename;
-static TAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
- TAILQ_HEAD_INITIALIZER(slirp_stacks);
+static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
+ QTAILQ_HEAD_INITIALIZER(slirp_stacks);
static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
int legacy_format);
slirp_cleanup(s->slirp);
slirp_smb_cleanup(s);
- TAILQ_REMOVE(&slirp_stacks, s, entry);
+ QTAILQ_REMOVE(&slirp_stacks, s, entry);
qemu_free(s);
}
const char *vsmbserver)
{
/* default settings according to historic slirp */
- struct in_addr net = { .s_addr = htonl(0x0a000000) }; /* 10.0.0.0 */
- struct in_addr mask = { .s_addr = htonl(0xff000000) }; /* 255.0.0.0 */
+ struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
+ struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
s = qemu_mallocz(sizeof(SlirpState));
s->slirp = slirp_init(restricted, net, mask, host, vhostname,
tftp_export, bootfile, dhcp, dns, s);
- TAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
+ QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
while (slirp_configs) {
struct slirp_config_str *config = slirp_configs;
}
return vc->opaque;
} else {
- if (TAILQ_EMPTY(&slirp_stacks)) {
+ if (QTAILQ_EMPTY(&slirp_stacks)) {
monitor_printf(mon, "user mode network stack not in use\n");
return NULL;
}
- return TAILQ_FIRST(&slirp_stacks);
+ return QTAILQ_FIRST(&slirp_stacks);
}
}
-void net_slirp_hostfwd_remove(Monitor *mon, const char *arg1,
- const char *arg2, const char *arg3)
+void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict)
{
struct in_addr host_addr = { .s_addr = INADDR_ANY };
int host_port;
SlirpState *s;
int is_udp = 0;
int err;
+ const char *arg1 = qdict_get_str(qdict, "arg1");
+ const char *arg2 = qdict_get_try_str(qdict, "arg2");
+ const char *arg3 = qdict_get_try_str(qdict, "arg3");
if (arg2) {
s = slirp_lookup(mon, arg1, arg2);
host_port = atoi(p);
- err = slirp_remove_hostfwd(TAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
+ err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
host_addr, host_port);
monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
config_error(mon, "invalid host forwarding rule '%s'\n", redir_str);
}
-void net_slirp_hostfwd_add(Monitor *mon, const char *arg1,
- const char *arg2, const char *arg3)
+void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict)
{
const char *redir_str;
SlirpState *s;
+ const char *arg1 = qdict_get_str(qdict, "arg1");
+ const char *arg2 = qdict_get_try_str(qdict, "arg2");
+ const char *arg3 = qdict_get_try_str(qdict, "arg3");
if (arg2) {
s = slirp_lookup(mon, arg1, arg2);
{
struct slirp_config_str *config;
- if (TAILQ_EMPTY(&slirp_stacks)) {
+ if (QTAILQ_EMPTY(&slirp_stacks)) {
config = qemu_malloc(sizeof(*config));
pstrcpy(config->str, sizeof(config->str), redir_str);
config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
return;
}
- slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), NULL, redir_str, 1);
+ slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), NULL, redir_str, 1);
}
#ifndef _WIN32
snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
SMBD_COMMAND, smb_conf);
- if (slirp_add_exec(s->slirp, 0, smb_cmdline, vserver_addr, 139) < 0) {
+ if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) {
slirp_smb_cleanup(s);
config_error(mon, "conflicting/invalid smbserver address\n");
}
exit(1);
}
legacy_smb_export = exported_dir;
- if (!TAILQ_EMPTY(&slirp_stacks)) {
- slirp_smb(TAILQ_FIRST(&slirp_stacks), NULL, exported_dir,
+ if (!QTAILQ_EMPTY(&slirp_stacks)) {
+ slirp_smb(QTAILQ_FIRST(&slirp_stacks), NULL, exported_dir,
vserver_addr);
}
}
qemu_free(fwd);
return;
}
- fwd->server = server;
- fwd->port = port;
- fwd->slirp = s->slirp;
- if (slirp_add_exec(s->slirp, 3, fwd->hd, server, port) < 0) {
+ if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
config_error(mon, "conflicting/invalid host:port in guest forwarding "
"rule '%s'\n", config_str);
qemu_free(fwd);
return;
}
+ fwd->server = server;
+ fwd->port = port;
+ fwd->slirp = s->slirp;
+
qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
NULL, fwd);
return;
{
SlirpState *s;
- TAILQ_FOREACH(s, &slirp_stacks, entry) {
+ QTAILQ_FOREACH(s, &slirp_stacks, entry) {
monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
slirp_connection_info(s->slirp, mon);
}
return s;
}
-#if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
+#if defined (CONFIG_BSD) || defined (__FreeBSD_kernel__)
static int tap_open(char *ifname, int ifname_size)
{
int fd;
const char *model, const char *name,
int fd, int is_connected)
{
- int so_type=-1, optlen=sizeof(so_type);
+ int so_type = -1, optlen=sizeof(so_type);
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
(socklen_t *)&optlen)< 0) {
return size;
}
- ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
+ ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec());
caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
hdr.ts.tv_sec = ts / 1000000;
}
vlan = qemu_mallocz(sizeof(VLANState));
vlan->id = id;
+ QTAILQ_INIT(&vlan->send_queue);
vlan->next = NULL;
pvlan = &first_vlan;
while (*pvlan != NULL)
return -1;
}
+int qemu_show_nic_models(const char *arg, const char *const *models)
+{
+ int i;
+
+ if (!arg || strcmp(arg, "?"))
+ return 0;
+
+ fprintf(stderr, "qemu: Supported NIC models: ");
+ for (i = 0 ; models[i]; i++)
+ fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
+ return 1;
+}
+
void qemu_check_nic_model(NICInfo *nd, const char *model)
{
const char *models[2];
models[0] = model;
models[1] = NULL;
- qemu_check_nic_model_list(nd, models, model);
+ if (qemu_show_nic_models(nd->model, models))
+ exit(0);
+ if (qemu_find_nic_model(nd, models, model) < 0)
+ exit(1);
}
-void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
- const char *default_model)
+int qemu_find_nic_model(NICInfo *nd, const char * const *models,
+ const char *default_model)
{
- int i, exit_status = 0;
+ int i;
if (!nd->model)
nd->model = strdup(default_model);
- if (strcmp(nd->model, "?") != 0) {
- for (i = 0 ; models[i]; i++)
- if (strcmp(nd->model, models[i]) == 0)
- return;
-
- fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
- exit_status = 1;
+ for (i = 0 ; models[i]; i++) {
+ if (strcmp(nd->model, models[i]) == 0)
+ return i;
}
- fprintf(stderr, "qemu: Supported NIC models: ");
- for (i = 0 ; models[i]; i++)
- fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
+ qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
+ return -1;
+}
- exit(exit_status);
+static int net_handle_fd_param(Monitor *mon, const char *param)
+{
+ if (!qemu_isdigit(param[0])) {
+ int fd;
+
+ fd = monitor_get_fd(mon, param);
+ if (fd == -1) {
+ config_error(mon, "No file descriptor named %s found", param);
+ return -1;
+ }
+
+ return fd;
+ } else {
+ return strtol(param, NULL, 0);
+ }
}
int net_client_init(Monitor *mon, const char *device, const char *p)
}
if (!strcmp(device, "nic")) {
static const char * const nic_params[] = {
- "vlan", "name", "macaddr", "model", "addr", "vectors", NULL
+ "vlan", "name", "macaddr", "model", "addr", "id", "vectors", NULL
};
NICInfo *nd;
uint8_t *macaddr;
if (get_param_value(buf, sizeof(buf), "addr", p)) {
nd->devaddr = strdup(buf);
}
+ if (get_param_value(buf, sizeof(buf), "id", p)) {
+ nd->id = strdup(buf);
+ }
nd->nvectors = NIC_NVECTORS_UNSPECIFIED;
if (get_param_value(buf, sizeof(buf), "vectors", p)) {
char *endptr;
qemu_free(smb_export);
qemu_free(vsmbsrv);
} else if (!strcmp(device, "channel")) {
- if (TAILQ_EMPTY(&slirp_stacks)) {
+ if (QTAILQ_EMPTY(&slirp_stacks)) {
struct slirp_config_str *config;
config = qemu_malloc(sizeof(*config));
config->next = slirp_configs;
slirp_configs = config;
} else {
- slirp_guestfwd(TAILQ_FIRST(&slirp_stacks), mon, p, 1);
+ slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), mon, p, 1);
}
ret = 0;
} else
static const char * const fd_params[] = {
"vlan", "name", "fd", "sndbuf", NULL
};
+ ret = -1;
if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
- ret = -1;
goto out;
}
- fd = strtol(buf, NULL, 0);
+ fd = net_handle_fd_param(mon, buf);
+ if (fd == -1) {
+ goto out;
+ }
fcntl(fd, F_SETFL, O_NONBLOCK);
s = net_tap_fd_init(vlan, device, name, fd);
+ if (!s) {
+ close(fd);
+ }
} else {
static const char * const tap_params[] = {
"vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL
"vlan", "name", "fd", NULL
};
int fd;
+ ret = -1;
if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
- ret = -1;
goto out;
}
- fd = strtol(buf, NULL, 0);
- ret = -1;
- if (net_socket_fd_init(vlan, device, name, fd, 1))
- ret = 0;
+ fd = net_handle_fd_param(mon, buf);
+ if (fd == -1) {
+ goto out;
+ }
+ if (!net_socket_fd_init(vlan, device, name, fd, 1)) {
+ close(fd);
+ goto out;
+ }
+ ret = 0;
} else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
static const char * const listen_params[] = {
"vlan", "name", "listen", NULL
{
nd->vlan->nb_guest_devs--;
nb_nics--;
- nd->used = 0;
- free((void *)nd->model);
+
+ qemu_free((void *)nd->model);
+ qemu_free((void *)nd->name);
+ qemu_free((void *)nd->devaddr);
+ qemu_free((void *)nd->id);
+
+ memset(nd, 0, sizeof(*nd));
}
static int net_host_check_device(const char *device)
return 0;
}
-void net_host_device_add(Monitor *mon, const char *device, const char *opts)
+void net_host_device_add(Monitor *mon, const QDict *qdict)
{
+ const char *device = qdict_get_str(qdict, "device");
+ const char *opts = qdict_get_try_str(qdict, "opts");
+
if (!net_host_check_device(device)) {
monitor_printf(mon, "invalid host network device %s\n", device);
return;
}
}
-void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
+void net_host_device_remove(Monitor *mon, const QDict *qdict)
{
VLANClientState *vc;
+ int vlan_id = qdict_get_int(qdict, "vlan_id");
+ const char *device = qdict_get_str(qdict, "device");
vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
if (!vc) {
}
}
-int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
+void do_set_link(Monitor *mon, const QDict *qdict)
{
VLANState *vlan;
VLANClientState *vc = NULL;
+ const char *name = qdict_get_str(qdict, "name");
+ const char *up_or_down = qdict_get_str(qdict, "up_or_down");
for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
for (vc = vlan->first_client; vc != NULL; vc = vc->next)
done:
if (!vc) {
- monitor_printf(mon, "could not find network device '%s'", name);
- return 0;
+ monitor_printf(mon, "could not find network device '%s'\n", name);
+ return;
}
if (strcmp(up_or_down, "up") == 0)
if (vc->link_status_changed)
vc->link_status_changed(vc);
-
- return 1;
}
void net_cleanup(void)