slirp: Move smb, redir, tftp and bootp parameters and -net channel

So far a couple of slirp-related parameters were expressed via
stand-alone command line options. This it inconsistent and unintuitive.
Moreover, it prevents both dynamically reconfigured (host_net_add/
delete) and multi-instance slirp.

This patch refactors the configuration by turning -smb, -redir, -tftp
and -bootp as well as -net channel into options of "-net user". The old
stand-alone command line options are still processed, but no longer
advertised. This allows smooth migration of management applications to
to the new syntax and also the extension of that syntax later in this
series.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Jan Kiszka 2009-06-24 14:42:28 +02:00 committed by Anthony Liguori
parent 5db4af8bc8
commit ad196a9d0c
9 changed files with 264 additions and 152 deletions

173
net.c
View File

@ -669,22 +669,28 @@ static void config_error(Monitor *mon, const char *fmt, ...)
/* slirp network adapter */
#define SLIRP_CFG_REDIR 1
struct slirp_config_str {
struct slirp_config_str *next;
const char *str;
int flags;
char str[1024];
};
static int slirp_inited;
static struct slirp_config_str *slirp_redirs;
#ifndef _WIN32
static const char *slirp_smb_export;
#endif
static struct slirp_config_str *slirp_configs;
const char *legacy_tftp_prefix;
const char *legacy_bootp_filename;
static VLANClientState *slirp_vc;
static void slirp_redirection(Monitor *mon, const char *redir_str);
static void vmchannel_init(Monitor *mon, const char *config_str);
#ifndef _WIN32
static const char *legacy_smb_export;
static void slirp_smb(const char *exported_dir);
#endif
static void slirp_redirection(Monitor *mon, const char *redir_str);
int slirp_can_output(void)
{
@ -724,27 +730,42 @@ static void net_slirp_cleanup(VLANClientState *vc)
slirp_in_use = 0;
}
static int net_slirp_init(VLANState *vlan, const char *model, const char *name,
int restricted, const char *ip)
static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
const char *name, int restricted, const char *ip,
const char *tftp_export, const char *bootfile,
const char *smb_export)
{
if (slirp_in_use) {
/* slirp only supports a single instance so far */
return -1;
}
if (!slirp_inited) {
if (!tftp_export) {
tftp_export = legacy_tftp_prefix;
}
if (!bootfile) {
bootfile = legacy_bootp_filename;
}
slirp_inited = 1;
slirp_init(restricted, ip);
slirp_init(restricted, ip, tftp_export, bootfile);
while (slirp_redirs) {
struct slirp_config_str *config = slirp_redirs;
while (slirp_configs) {
struct slirp_config_str *config = slirp_configs;
slirp_redirection(NULL, config->str);
slirp_redirs = config->next;
if (config->flags & SLIRP_CFG_REDIR) {
slirp_redirection(mon, config->str);
} else {
vmchannel_init(mon, config->str);
}
slirp_configs = config->next;
qemu_free(config);
}
#ifndef _WIN32
if (slirp_smb_export) {
slirp_smb(slirp_smb_export);
if (!smb_export) {
smb_export = legacy_smb_export;
}
if (smb_export) {
slirp_smb(smb_export);
}
#endif
}
@ -853,9 +874,10 @@ void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2
monitor_printf(mon, "user mode network stack not in use\n");
} else {
config = qemu_malloc(sizeof(*config));
config->str = redir_str;
config->next = slirp_redirs;
slirp_redirs = config;
pstrcpy(config->str, sizeof(config->str), redir_str);
config->flags = SLIRP_CFG_REDIR;
config->next = slirp_configs;
slirp_configs = config;
}
return;
}
@ -952,14 +974,14 @@ static void slirp_smb(const char *exported_dir)
slirp_add_exec(0, smb_cmdline, 4, 139);
}
/* automatic user mode samba server configuration */
/* automatic user mode samba server configuration (legacy interface) */
void net_slirp_smb(const char *exported_dir)
{
if (slirp_smb_export) {
if (legacy_smb_export) {
fprintf(stderr, "-smb given twice\n");
exit(1);
}
slirp_smb_export = exported_dir;
legacy_smb_export = exported_dir;
if (slirp_inited) {
slirp_smb(exported_dir);
}
@ -989,6 +1011,36 @@ static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
slirp_socket_recv(4, vmc->port, buf, size);
}
static void vmchannel_init(Monitor *mon, const char *config_str)
{
struct VMChannel *vmc;
char *devname;
char name[20];
int port;
port = strtol(config_str, &devname, 10);
if (port < 1 || port > 65535 || *devname != ':') {
config_error(mon, "invalid vmchannel port number\n");
return;
}
devname++;
vmc = qemu_malloc(sizeof(struct VMChannel));
snprintf(name, sizeof(name), "vmchannel%d", port);
vmc->hd = qemu_chr_open(name, devname, NULL);
if (!vmc->hd) {
config_error(mon, "could not open vmchannel device '%s'\n", devname);
qemu_free(vmc);
return;
}
vmc->port = port;
slirp_add_exec(3, vmc->hd, 4, port);
qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
NULL, vmc);
return;
}
#endif /* CONFIG_SLIRP */
#if !defined(_WIN32)
@ -2200,10 +2252,16 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
#ifdef CONFIG_SLIRP
if (!strcmp(device, "user")) {
static const char * const slirp_params[] = {
"vlan", "name", "hostname", "restrict", "ip", NULL
"vlan", "name", "hostname", "restrict", "ip", "tftp", "bootfile",
"smb", "redir", "channel", NULL
};
struct slirp_config_str *config;
char *tftp_export = NULL;
char *bootfile = NULL;
char *smb_export = NULL;
int restricted = 0;
char *ip = NULL;
const char *q;
if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
@ -2219,34 +2277,59 @@ int net_client_init(Monitor *mon, const char *device, const char *p)
if (get_param_value(buf, sizeof(buf), "ip", p)) {
ip = qemu_strdup(buf);
}
if (get_param_value(buf, sizeof(buf), "tftp", p)) {
tftp_export = qemu_strdup(buf);
}
if (get_param_value(buf, sizeof(buf), "bootfile", p)) {
bootfile = qemu_strdup(buf);
}
if (get_param_value(buf, sizeof(buf), "smb", p)) {
smb_export = qemu_strdup(buf);
}
q = p;
while (1) {
config = qemu_malloc(sizeof(*config));
if (!get_next_param_value(config->str, sizeof(config->str),
"redir", &q)) {
break;
}
config->flags = SLIRP_CFG_REDIR;
config->next = slirp_configs;
slirp_configs = config;
config = NULL;
}
q = p;
while (1) {
config = qemu_malloc(sizeof(*config));
if (!get_next_param_value(config->str, sizeof(config->str),
"channel", &q)) {
break;
}
config->flags = 0;
config->next = slirp_configs;
slirp_configs = config;
config = NULL;
}
qemu_free(config);
vlan->nb_host_devs++;
ret = net_slirp_init(vlan, device, name, restricted, ip);
ret = net_slirp_init(mon, vlan, device, name, restricted, ip,
tftp_export, bootfile, smb_export);
qemu_free(ip);
qemu_free(tftp_export);
qemu_free(bootfile);
qemu_free(smb_export);
} else if (!strcmp(device, "channel")) {
long port;
char name[20], *devname;
struct VMChannel *vmc;
if (!slirp_inited) {
struct slirp_config_str *config;
port = strtol(p, &devname, 10);
devname++;
if (port < 1 || port > 65535) {
config_error(mon, "vmchannel wrong port number\n");
ret = -1;
goto out;
config = qemu_malloc(sizeof(*config));
pstrcpy(config->str, sizeof(config->str), p);
config->flags = 0;
config->next = slirp_configs;
slirp_configs = config;
} else {
vmchannel_init(mon, p);
}
vmc = malloc(sizeof(struct VMChannel));
snprintf(name, 20, "vmchannel%ld", port);
vmc->hd = qemu_chr_open(name, devname, NULL);
if (!vmc->hd) {
config_error(mon, "could not open vmchannel device '%s'\n",
devname);
ret = -1;
goto out;
}
vmc->port = port;
slirp_add_exec(3, vmc->hd, 4, port);
qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
NULL, vmc);
ret = 0;
} else
#endif

3
net.h
View File

@ -125,6 +125,9 @@ uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
void net_checksum_calculate(uint8_t *data, int length);
/* from net.c */
extern const char *legacy_tftp_prefix;
extern const char *legacy_bootp_filename;
int net_client_init(Monitor *mon, const char *device, const char *p);
void net_client_uninit(NICInfo *nd);
int net_client_parse(const char *str);

View File

@ -735,13 +735,27 @@ STEXI
@table @option
ETEXI
HXCOMM Legacy slirp options (now moved to -net user):
#ifdef CONFIG_SLIRP
DEF("tftp", HAS_ARG, QEMU_OPTION_tftp, "")
DEF("bootp", HAS_ARG, QEMU_OPTION_bootp, "")
DEF("redir", HAS_ARG, QEMU_OPTION_redir, "")
#ifndef _WIN32
DEF("smb", HAS_ARG, QEMU_OPTION_smb, "")
#endif
#endif
DEF("net", HAS_ARG, QEMU_OPTION_net,
"-net nic[,vlan=n][,macaddr=mac][,model=type][,name=str][,addr=str][,vectors=v]\n"
" create a new Network Interface Card and connect it to VLAN 'n'\n"
#ifdef CONFIG_SLIRP
"-net user[,vlan=n][,name=str][,hostname=host]\n"
" connect the user mode network stack to VLAN 'n' and send\n"
" hostname 'host' to DHCP clients\n"
"-net user[,vlan=n][,name=str][ip=netaddr][,restrict=y|n][,hostname=host]\n"
" [,tftp=dir][,bootfile=f][,redir=rule][,channel=rule]"
#ifndef _WIN32
"[,smb=dir]\n"
#endif
" connect the user mode network stack to VLAN 'n', configure its\n"
" DHCP server and enabled optional services\n"
#endif
#ifdef _WIN32
"-net tap[,vlan=n][,name=str],ifname=name\n"
@ -794,13 +808,102 @@ Valid values for @var{type} are
Not all devices are supported on all targets. Use -net nic,model=?
for a list of available devices for your target.
@item -net user[,vlan=@var{n}][,hostname=@var{name}][,name=@var{name}]
@item -net user[,@var{option}][,@var{option}][,...]
Use the user mode network stack which requires no administrator
privilege to run. @option{hostname=name} can be used to specify the client
hostname reported by the builtin DHCP server.
privilege to run. Valid options are:
@item -net channel,@var{port}:@var{dev}
Forward @option{user} TCP connection to port @var{port} to character device @var{dev}
@table @code
@item vlan=@var{n}
Connect user mode stack to VLAN @var{n} (@var{n} = 0 is the default).
@item name=@var{name}
Assign symbolic name for use in monitor commands.
@item ip=@var{netaddr}
Set IP network address the guest will see (default: 10.0.2.x).
@item restrict=y|yes|n|no
If this options is enabled, the guest will be isolated, i.e. it will not be
able to contact the host and no guest IP packets will be routed over the host
to the outside. This option does not affect explicitly set forwarding rule.
@item hostname=@var{name}
Specifies the client hostname reported by the builtin DHCP server.
@item tftp=@var{dir}
When using the user mode network stack, activate a built-in TFTP
server. The files in @var{dir} will be exposed as the root of a TFTP server.
The TFTP client on the guest must be configured in binary mode (use the command
@code{bin} of the Unix TFTP client). The host IP address on the guest is
10.0.2.2 by default.
@item bootfile=@var{file}
When using the user mode network stack, broadcast @var{file} as the BOOTP
filename. In conjunction with @option{tftp}, this can be used to network boot
a guest from a local directory.
Example (using pxelinux):
@example
qemu -hda linux.img -boot n -net user,tftp=/path/to/tftp/files,bootfile=/pxelinux.0
@end example
@item smb=@var{dir}
When using the user mode network stack, activate a built-in SMB
server so that Windows OSes can access to the host files in @file{@var{dir}}
transparently.
In the guest Windows OS, the line:
@example
10.0.2.4 smbserver
@end example
must be added in the file @file{C:\WINDOWS\LMHOSTS} (for windows 9x/Me)
or @file{C:\WINNT\SYSTEM32\DRIVERS\ETC\LMHOSTS} (Windows NT/2000).
Then @file{@var{dir}} can be accessed in @file{\\smbserver\qemu}.
Note that a SAMBA server must be installed on the host OS in
@file{/usr/sbin/smbd}. QEMU was tested successfully with smbd versions from
Red Hat 9, Fedora Core 3 and OpenSUSE 11.x.
@item redir=[tcp|udp]:@var{host-port}:[@var{guest-host}]:@var{guest-port}
Redirect incoming TCP or UDP connections to the host port @var{host-port} to
the guest @var{guest-host} on guest port @var{guest-port}. If @var{guest-host}
is not specified, its value is 10.0.2.15 (default address given by the built-in
DHCP server). If no connection type is specified, TCP is used. This option can
be given multiple times.
For example, to redirect host X11 connection from screen 1 to guest
screen 0, use the following:
@example
# on the host
qemu -net user,redir=tcp:6001::6000 [...]
# this host xterm should open in the guest X11 server
xterm -display :1
@end example
To redirect telnet connections from host port 5555 to telnet port on
the guest, use the following:
@example
# on the host
qemu -net user,redir=tcp:5555::23 [...]
telnet localhost 5555
@end example
Then when you use on the host @code{telnet localhost 5555}, you
connect to the guest telnet server.
@item channel=@var{port}:@var{dev}
Forward guest TCP connections to port @var{port} on the host to character
device @var{dev}. This option can be given multiple times.
@end table
Note: Legacy stand-alone options -tftp, -bootp, -smb and -redir are still
processed and applied to -net user. Mixing them with the new configuration
syntax gives undefined results. Their use for new applications is discouraged
as they will be removed from future versions.
@item -net tap[,vlan=@var{n}][,name=@var{name}][,fd=@var{h}][,ifname=@var{name}][,script=@var{file}][,downscript=@var{dfile}]
Connect the host TAP network interface @var{name} to VLAN @var{n}, use
@ -906,96 +1009,6 @@ libpcap, so it can be analyzed with tools such as tcpdump or Wireshark.
Indicate that no network devices should be configured. It is used to
override the default configuration (@option{-net nic -net user}) which
is activated if no @option{-net} options are provided.
ETEXI
#ifdef CONFIG_SLIRP
DEF("tftp", HAS_ARG, QEMU_OPTION_tftp, \
"-tftp dir allow tftp access to files in dir [-net user]\n")
#endif
STEXI
@item -tftp @var{dir}
When using the user mode network stack, activate a built-in TFTP
server. The files in @var{dir} will be exposed as the root of a TFTP server.
The TFTP client on the guest must be configured in binary mode (use the command
@code{bin} of the Unix TFTP client). The host IP address on the guest is as
usual 10.0.2.2.
ETEXI
#ifdef CONFIG_SLIRP
DEF("bootp", HAS_ARG, QEMU_OPTION_bootp, \
"-bootp file advertise file in BOOTP replies\n")
#endif
STEXI
@item -bootp @var{file}
When using the user mode network stack, broadcast @var{file} as the BOOTP
filename. In conjunction with @option{-tftp}, this can be used to network boot
a guest from a local directory.
Example (using pxelinux):
@example
qemu -hda linux.img -boot n -tftp /path/to/tftp/files -bootp /pxelinux.0
@end example
ETEXI
#ifndef _WIN32
DEF("smb", HAS_ARG, QEMU_OPTION_smb, \
"-smb dir allow SMB access to files in 'dir' [-net user]\n")
#endif
STEXI
@item -smb @var{dir}
When using the user mode network stack, activate a built-in SMB
server so that Windows OSes can access to the host files in @file{@var{dir}}
transparently.
In the guest Windows OS, the line:
@example
10.0.2.4 smbserver
@end example
must be added in the file @file{C:\WINDOWS\LMHOSTS} (for windows 9x/Me)
or @file{C:\WINNT\SYSTEM32\DRIVERS\ETC\LMHOSTS} (Windows NT/2000).
Then @file{@var{dir}} can be accessed in @file{\\smbserver\qemu}.
Note that a SAMBA server must be installed on the host OS in
@file{/usr/sbin/smbd}. QEMU was tested successfully with smbd version
2.2.7a from the Red Hat 9 and version 3.0.10-1.fc3 from Fedora Core 3.
ETEXI
#ifdef CONFIG_SLIRP
DEF("redir", HAS_ARG, QEMU_OPTION_redir, \
"-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" \
" redirect TCP or UDP connections from host to guest [-net user]\n")
#endif
STEXI
@item -redir [tcp|udp]:@var{host-port}:[@var{guest-host}]:@var{guest-port}
When using the user mode network stack, redirect incoming TCP or UDP
connections to the host port @var{host-port} to the guest
@var{guest-host} on guest port @var{guest-port}. If @var{guest-host}
is not specified, its value is 10.0.2.15 (default address given by the
built-in DHCP server). If no connection type is specified, TCP is used.
For example, to redirect host X11 connection from screen 1 to guest
screen 0, use the following:
@example
# on the host
qemu -redir tcp:6001::6000 [...]
# this host xterm should open in the guest X11 server
xterm -display :1
@end example
To redirect telnet connections from host port 5555 to telnet port on
the guest, use the following:
@example
# on the host
qemu -redir tcp:5555::23 [...]
telnet localhost 5555
@end example
Then when you use on the host @code{telnet localhost 5555}, you
connect to the guest telnet server.
@end table
ETEXI

View File

@ -38,7 +38,7 @@ typedef struct {
static BOOTPClient bootp_clients[NB_ADDR];
const char *bootp_filename;
char *bootp_filename;
static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };

View File

@ -5,7 +5,8 @@
extern "C" {
#endif
void slirp_init(int restricted, const char *special_ip);
void slirp_init(int restricted, const char *special_ip, const char *tftp_path,
const char *bootfile);
void slirp_select_fill(int *pnfds,
fd_set *readfds, fd_set *writefds, fd_set *xfds);
@ -24,9 +25,7 @@ int slirp_redir(int is_udp, int host_port,
int slirp_add_exec(int do_pty, const void *args, int addr_low_byte,
int guest_port);
extern const char *tftp_prefix;
extern char slirp_hostname[33];
extern const char *bootp_filename;
void slirp_stats(void);
void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf,

View File

@ -46,6 +46,8 @@ extern int tcp_keepintvl;
extern uint8_t client_ethaddr[6];
extern const char *slirp_special_ip;
extern int slirp_restrict;
extern char *tftp_prefix;
extern char *bootp_filename;
#define PROTO_SLIP 0x1
#ifdef USE_PPP

View File

@ -171,7 +171,8 @@ static void slirp_cleanup(void)
static void slirp_state_save(QEMUFile *f, void *opaque);
static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
void slirp_init(int restricted, const char *special_ip)
void slirp_init(int restricted, const char *special_ip, const char *tftp_path,
const char *bootfile)
{
// debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
@ -203,6 +204,17 @@ void slirp_init(int restricted, const char *special_ip)
if (special_ip)
slirp_special_ip = special_ip;
qemu_free(tftp_prefix);
tftp_prefix = NULL;
if (tftp_path) {
tftp_prefix = qemu_strdup(tftp_path);
}
qemu_free(bootp_filename);
bootp_filename = NULL;
if (bootfile) {
bootp_filename = qemu_strdup(bootfile);
}
inet_aton(slirp_special_ip, &special_addr);
alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
getouraddr();

View File

@ -37,7 +37,7 @@ struct tftp_session {
static struct tftp_session tftp_sessions[TFTP_SESSIONS_MAX];
const char *tftp_prefix;
char *tftp_prefix;
static void tftp_session_update(struct tftp_session *spt)
{

6
vl.c
View File

@ -5308,14 +5308,14 @@ int main(int argc, char **argv, char **envp)
break;
#ifdef CONFIG_SLIRP
case QEMU_OPTION_tftp:
tftp_prefix = optarg;
legacy_tftp_prefix = optarg;
break;
case QEMU_OPTION_bootp:
bootp_filename = optarg;
legacy_bootp_filename = optarg;
break;
#ifndef _WIN32
case QEMU_OPTION_smb:
net_slirp_smb(optarg);
net_slirp_smb(optarg);
break;
#endif
case QEMU_OPTION_redir: