[PATCH] uml: slirp and slip driver cleanups and fixes

This patch merges a lot of duplicated code in the slip and slirp drivers,
abstracts out the slip protocol, and makes the slip driver work in 2.6.

Signed-off-by: Jeff Dike <jdike@addtoit.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
This commit is contained in:
Jeff Dike 2005-06-13 15:52:18 -07:00 committed by Linus Torvalds
parent 98fdffccea
commit a3c77c67a4
9 changed files with 179 additions and 243 deletions

View File

@ -22,8 +22,8 @@ obj-y := stdio_console.o fd.o chan_kern.o chan_user.o line.o
obj-$(CONFIG_SSL) += ssl.o obj-$(CONFIG_SSL) += ssl.o
obj-$(CONFIG_STDERR_CONSOLE) += stderr_console.o obj-$(CONFIG_STDERR_CONSOLE) += stderr_console.o
obj-$(CONFIG_UML_NET_SLIP) += slip.o obj-$(CONFIG_UML_NET_SLIP) += slip.o slip_common.o
obj-$(CONFIG_UML_NET_SLIRP) += slirp.o obj-$(CONFIG_UML_NET_SLIRP) += slirp.o slip_common.o
obj-$(CONFIG_UML_NET_DAEMON) += daemon.o obj-$(CONFIG_UML_NET_DAEMON) += daemon.o
obj-$(CONFIG_UML_NET_MCAST) += mcast.o obj-$(CONFIG_UML_NET_MCAST) += mcast.o
#obj-$(CONFIG_UML_NET_PCAP) += pcap.o $(PCAP) #obj-$(CONFIG_UML_NET_PCAP) += pcap.o $(PCAP)
@ -41,6 +41,6 @@ obj-$(CONFIG_UML_WATCHDOG) += harddog.o
obj-$(CONFIG_BLK_DEV_COW_COMMON) += cow_user.o obj-$(CONFIG_BLK_DEV_COW_COMMON) += cow_user.o
obj-$(CONFIG_UML_RANDOM) += random.o obj-$(CONFIG_UML_RANDOM) += random.o
USER_OBJS := fd.o null.o pty.o tty.o xterm.o USER_OBJS := fd.o null.o pty.o tty.o xterm.o slip_common.o
include arch/um/scripts/Makefile.rules include arch/um/scripts/Makefile.rules

View File

@ -1,10 +1,7 @@
#ifndef __UM_SLIP_H #ifndef __UM_SLIP_H
#define __UM_SLIP_H #define __UM_SLIP_H
#define BUF_SIZE 1500 #include "slip_common.h"
/* two bytes each for a (pathological) max packet of escaped chars + *
* terminating END char + initial END char */
#define ENC_BUF_SIZE (2 * BUF_SIZE + 2)
struct slip_data { struct slip_data {
void *dev; void *dev;
@ -12,28 +9,12 @@ struct slip_data {
char *addr; char *addr;
char *gate_addr; char *gate_addr;
int slave; int slave;
unsigned char ibuf[ENC_BUF_SIZE]; struct slip_proto slip;
unsigned char obuf[ENC_BUF_SIZE];
int more; /* more data: do not read fd until ibuf has been drained */
int pos;
int esc;
}; };
extern struct net_user_info slip_user_info; extern struct net_user_info slip_user_info;
extern int set_umn_addr(int fd, char *addr, char *ptp_addr);
extern int slip_user_read(int fd, void *buf, int len, struct slip_data *pri); extern int slip_user_read(int fd, void *buf, int len, struct slip_data *pri);
extern int slip_user_write(int fd, void *buf, int len, struct slip_data *pri); extern int slip_user_write(int fd, void *buf, int len, struct slip_data *pri);
#endif #endif
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/

View File

@ -0,0 +1,54 @@
#include <string.h>
#include "slip_common.h"
#include "net_user.h"
int slip_proto_read(int fd, void *buf, int len, struct slip_proto *slip)
{
int i, n, size, start;
if(slip->more > 0){
i = 0;
while(i < slip->more){
size = slip_unesc(slip->ibuf[i++], slip->ibuf,
&slip->pos, &slip->esc);
if(size){
memcpy(buf, slip->ibuf, size);
memmove(slip->ibuf, &slip->ibuf[i],
slip->more - i);
slip->more = slip->more - i;
return size;
}
}
slip->more = 0;
}
n = net_read(fd, &slip->ibuf[slip->pos],
sizeof(slip->ibuf) - slip->pos);
if(n <= 0)
return n;
start = slip->pos;
for(i = 0; i < n; i++){
size = slip_unesc(slip->ibuf[start + i], slip->ibuf,&slip->pos,
&slip->esc);
if(size){
memcpy(buf, slip->ibuf, size);
memmove(slip->ibuf, &slip->ibuf[start+i+1],
n - (i + 1));
slip->more = n - (i + 1);
return size;
}
}
return 0;
}
int slip_proto_write(int fd, void *buf, int len, struct slip_proto *slip)
{
int actual, n;
actual = slip_esc(buf, slip->obuf, len);
n = net_write(fd, slip->obuf, actual);
if(n < 0)
return n;
else return len;
}

View File

@ -1,10 +1,10 @@
/* #ifndef __UM_SLIP_COMMON_H
* Copyright (C) 2002 Jeff Dike (jdike@karaya.com) #define __UM_SLIP_COMMON_H
* Licensed under the GPL
*/
#ifndef __UM_SLIP_PROTO_H__ #define BUF_SIZE 1500
#define __UM_SLIP_PROTO_H__ /* two bytes each for a (pathological) max packet of escaped chars + *
* terminating END char + initial END char */
#define ENC_BUF_SIZE (2 * BUF_SIZE + 2)
/* SLIP protocol characters. */ /* SLIP protocol characters. */
#define SLIP_END 0300 /* indicates end of frame */ #define SLIP_END 0300 /* indicates end of frame */
@ -80,15 +80,25 @@ static inline int slip_esc(unsigned char *s, unsigned char *d, int len)
return (ptr - d); return (ptr - d);
} }
#endif struct slip_proto {
unsigned char ibuf[ENC_BUF_SIZE];
unsigned char obuf[ENC_BUF_SIZE];
int more; /* more data: do not read fd until ibuf has been drained */
int pos;
int esc;
};
/* #define SLIP_PROTO_INIT { \
* Overrides for Emacs so that we follow Linus's tabbing style. .ibuf = { '\0' }, \
* Emacs will notice this stuff at the end of the file and automatically .obuf = { '\0' }, \
* adjust the settings for this buffer only. This must remain at the end .more = 0, \
* of the file. .pos = 0, \
* --------------------------------------------------------------------------- .esc = 0 \
* Local variables: }
* c-file-style: "linux"
* End: extern int slip_proto_read(int fd, void *buf, int len,
*/ struct slip_proto *slip);
extern int slip_proto_write(int fd, void *buf, int len,
struct slip_proto *slip);
#endif

View File

@ -26,16 +26,16 @@ void slip_init(struct net_device *dev, void *data)
.addr = NULL, .addr = NULL,
.gate_addr = init->gate_addr, .gate_addr = init->gate_addr,
.slave = -1, .slave = -1,
.ibuf = { '\0' }, .slip = SLIP_PROTO_INIT,
.obuf = { '\0' },
.pos = 0,
.esc = 0,
.dev = dev }); .dev = dev });
dev->init = NULL; dev->init = NULL;
dev->header_cache_update = NULL;
dev->hard_header_cache = NULL;
dev->hard_header = NULL;
dev->hard_header_len = 0; dev->hard_header_len = 0;
dev->addr_len = 4; dev->addr_len = 0;
dev->type = ARPHRD_ETHER; dev->type = ARPHRD_SLIP;
dev->tx_queue_len = 256; dev->tx_queue_len = 256;
dev->flags = IFF_NOARP; dev->flags = IFF_NOARP;
printk("SLIP backend - SLIP IP = %s\n", spri->gate_addr); printk("SLIP backend - SLIP IP = %s\n", spri->gate_addr);

View File

@ -13,7 +13,7 @@
#include "user.h" #include "user.h"
#include "net_user.h" #include "net_user.h"
#include "slip.h" #include "slip.h"
#include "slip_proto.h" #include "slip_common.h"
#include "helper.h" #include "helper.h"
#include "os.h" #include "os.h"
@ -77,41 +77,51 @@ static int slip_tramp(char **argv, int fd)
err = os_pipe(fds, 1, 0); err = os_pipe(fds, 1, 0);
if(err < 0){ if(err < 0){
printk("slip_tramp : pipe failed, err = %d\n", -err); printk("slip_tramp : pipe failed, err = %d\n", -err);
return(err); goto out;
} }
err = 0; err = 0;
pe_data.stdin = fd; pe_data.stdin = fd;
pe_data.stdout = fds[1]; pe_data.stdout = fds[1];
pe_data.close_me = fds[0]; pe_data.close_me = fds[0];
pid = run_helper(slip_pre_exec, &pe_data, argv, NULL); err = run_helper(slip_pre_exec, &pe_data, argv, NULL);
if(err < 0)
goto out_close;
pid = err;
if(pid < 0) err = pid; output_len = page_size();
else { output = um_kmalloc(output_len);
output_len = page_size(); if(output == NULL){
output = um_kmalloc(output_len); printk("slip_tramp : failed to allocate output buffer\n");
if(output == NULL) os_kill_process(pid, 1);
printk("slip_tramp : failed to allocate output " err = -ENOMEM;
"buffer\n"); goto out_free;
os_close_file(fds[1]);
read_output(fds[0], output, output_len);
if(output != NULL){
printk("%s", output);
kfree(output);
}
CATCH_EINTR(err = waitpid(pid, &status, 0));
if(err < 0)
err = errno;
else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){
printk("'%s' didn't exit with status 0\n", argv[0]);
err = -EINVAL;
}
} }
os_close_file(fds[1]);
read_output(fds[0], output, output_len);
printk("%s", output);
CATCH_EINTR(err = waitpid(pid, &status, 0));
if(err < 0)
err = errno;
else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){
printk("'%s' didn't exit with status 0\n", argv[0]);
err = -EINVAL;
}
else err = 0;
os_close_file(fds[0]); os_close_file(fds[0]);
return(err); out_free:
kfree(output);
return err;
out_close:
os_close_file(fds[0]);
os_close_file(fds[1]);
out:
return err;
} }
static int slip_open(void *data) static int slip_open(void *data)
@ -123,21 +133,26 @@ static int slip_open(void *data)
NULL }; NULL };
int sfd, mfd, err; int sfd, mfd, err;
mfd = get_pty(); err = get_pty();
if(mfd < 0){ if(err < 0){
printk("umn : Failed to open pty, err = %d\n", -mfd); printk("slip-open : Failed to open pty, err = %d\n", -err);
return(mfd); goto out;
} }
sfd = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0); mfd = err;
if(sfd < 0){
printk("Couldn't open tty for slip line, err = %d\n", -sfd); err = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0);
os_close_file(mfd); if(err < 0){
return(sfd); printk("Couldn't open tty for slip line, err = %d\n", -err);
goto out_close;
} }
if(set_up_tty(sfd)) return(-1); sfd = err;
if(set_up_tty(sfd))
goto out_close2;
pri->slave = sfd; pri->slave = sfd;
pri->pos = 0; pri->slip.pos = 0;
pri->esc = 0; pri->slip.esc = 0;
if(pri->gate_addr != NULL){ if(pri->gate_addr != NULL){
sprintf(version_buf, "%d", UML_NET_VERSION); sprintf(version_buf, "%d", UML_NET_VERSION);
strcpy(gate_buf, pri->gate_addr); strcpy(gate_buf, pri->gate_addr);
@ -146,12 +161,12 @@ static int slip_open(void *data)
if(err < 0){ if(err < 0){
printk("slip_tramp failed - err = %d\n", -err); printk("slip_tramp failed - err = %d\n", -err);
return(err); goto out_close2;
} }
err = os_get_ifname(pri->slave, pri->name); err = os_get_ifname(pri->slave, pri->name);
if(err < 0){ if(err < 0){
printk("get_ifname failed, err = %d\n", -err); printk("get_ifname failed, err = %d\n", -err);
return(err); goto out_close2;
} }
iter_addresses(pri->dev, open_addr, pri->name); iter_addresses(pri->dev, open_addr, pri->name);
} }
@ -160,10 +175,16 @@ static int slip_open(void *data)
if(err < 0){ if(err < 0){
printk("Failed to set slip discipline encapsulation - " printk("Failed to set slip discipline encapsulation - "
"err = %d\n", -err); "err = %d\n", -err);
return(err); goto out_close2;
} }
} }
return(mfd); return(mfd);
out_close2:
os_close_file(sfd);
out_close:
os_close_file(mfd);
out:
return err;
} }
static void slip_close(int fd, void *data) static void slip_close(int fd, void *data)
@ -190,48 +211,12 @@ static void slip_close(int fd, void *data)
int slip_user_read(int fd, void *buf, int len, struct slip_data *pri) int slip_user_read(int fd, void *buf, int len, struct slip_data *pri)
{ {
int i, n, size, start; return slip_proto_read(fd, buf, len, &pri->slip);
if(pri->more>0) {
i = 0;
while(i < pri->more) {
size = slip_unesc(pri->ibuf[i++],
pri->ibuf, &pri->pos, &pri->esc);
if(size){
memcpy(buf, pri->ibuf, size);
memmove(pri->ibuf, &pri->ibuf[i], pri->more-i);
pri->more=pri->more-i;
return(size);
}
}
pri->more=0;
}
n = net_read(fd, &pri->ibuf[pri->pos], sizeof(pri->ibuf) - pri->pos);
if(n <= 0) return(n);
start = pri->pos;
for(i = 0; i < n; i++){
size = slip_unesc(pri->ibuf[start + i],
pri->ibuf, &pri->pos, &pri->esc);
if(size){
memcpy(buf, pri->ibuf, size);
memmove(pri->ibuf, &pri->ibuf[start+i+1], n-(i+1));
pri->more=n-(i+1);
return(size);
}
}
return(0);
} }
int slip_user_write(int fd, void *buf, int len, struct slip_data *pri) int slip_user_write(int fd, void *buf, int len, struct slip_data *pri)
{ {
int actual, n; return slip_proto_write(fd, buf, len, &pri->slip);
actual = slip_esc(buf, pri->obuf, len);
n = net_write(fd, pri->obuf, actual);
if(n < 0) return(n);
else return(len);
} }
static int slip_set_mtu(int mtu, void *data) static int slip_set_mtu(int mtu, void *data)
@ -267,14 +252,3 @@ struct net_user_info slip_user_info = {
.delete_address = slip_del_addr, .delete_address = slip_del_addr,
.max_packet = BUF_SIZE .max_packet = BUF_SIZE
}; };
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/

View File

@ -1,10 +1,7 @@
#ifndef __UM_SLIRP_H #ifndef __UM_SLIRP_H
#define __UM_SLIRP_H #define __UM_SLIRP_H
#define BUF_SIZE 1500 #include "slip_common.h"
/* two bytes each for a (pathological) max packet of escaped chars + *
* terminating END char + initial END char */
#define ENC_BUF_SIZE (2 * BUF_SIZE + 2)
#define SLIRP_MAX_ARGS 100 #define SLIRP_MAX_ARGS 100
/* /*
@ -24,28 +21,13 @@ struct slirp_data {
struct arg_list_dummy_wrapper argw; struct arg_list_dummy_wrapper argw;
int pid; int pid;
int slave; int slave;
unsigned char ibuf[ENC_BUF_SIZE]; struct slip_proto slip;
unsigned char obuf[ENC_BUF_SIZE];
int more; /* more data: do not read fd until ibuf has been drained */
int pos;
int esc;
}; };
extern struct net_user_info slirp_user_info; extern struct net_user_info slirp_user_info;
extern int set_umn_addr(int fd, char *addr, char *ptp_addr);
extern int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri); extern int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri);
extern int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri); extern int slirp_user_write(int fd, void *buf, int len,
struct slirp_data *pri);
#endif #endif
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/

View File

@ -25,10 +25,7 @@ void slirp_init(struct net_device *dev, void *data)
{ .argw = init->argw, { .argw = init->argw,
.pid = -1, .pid = -1,
.slave = -1, .slave = -1,
.ibuf = { '\0' }, .slip = SLIP_PROTO_INIT,
.obuf = { '\0' },
.pos = 0,
.esc = 0,
.dev = dev }); .dev = dev });
dev->init = NULL; dev->init = NULL;

View File

@ -12,7 +12,7 @@
#include "user.h" #include "user.h"
#include "net_user.h" #include "net_user.h"
#include "slirp.h" #include "slirp.h"
#include "slip_proto.h" #include "slip_common.h"
#include "helper.h" #include "helper.h"
#include "os.h" #include "os.h"
@ -48,47 +48,32 @@ static int slirp_tramp(char **argv, int fd)
return(pid); return(pid);
} }
/* XXX This is just a trivial wrapper around os_pipe */
static int slirp_datachan(int *mfd, int *sfd)
{
int fds[2], err;
err = os_pipe(fds, 1, 1);
if(err < 0){
printk("slirp_datachan: Failed to open pipe, err = %d\n", -err);
return(err);
}
*mfd = fds[0];
*sfd = fds[1];
return(0);
}
static int slirp_open(void *data) static int slirp_open(void *data)
{ {
struct slirp_data *pri = data; struct slirp_data *pri = data;
int sfd, mfd, pid, err; int fds[2], pid, err;
err = slirp_datachan(&mfd, &sfd); err = os_pipe(fds, 1, 1);
if(err) if(err)
return(err); return(err);
pid = slirp_tramp(pri->argw.argv, sfd); err = slirp_tramp(pri->argw.argv, fds[1]);
if(err < 0){
if(pid < 0){ printk("slirp_tramp failed - errno = %d\n", -err);
printk("slirp_tramp failed - errno = %d\n", -pid); goto out;
os_close_file(sfd);
os_close_file(mfd);
return(pid);
} }
pid = err;
pri->slave = sfd; pri->slave = fds[1];
pri->pos = 0; pri->slip.pos = 0;
pri->esc = 0; pri->slip.esc = 0;
pri->pid = err;
pri->pid = pid; return(fds[0]);
out:
return(mfd); os_close_file(fds[0]);
os_close_file(fds[1]);
return err;
} }
static void slirp_close(int fd, void *data) static void slirp_close(int fd, void *data)
@ -129,48 +114,12 @@ static void slirp_close(int fd, void *data)
int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri) int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri)
{ {
int i, n, size, start; return slip_proto_read(fd, buf, len, &pri->slip);
if(pri->more>0) {
i = 0;
while(i < pri->more) {
size = slip_unesc(pri->ibuf[i++],
pri->ibuf,&pri->pos,&pri->esc);
if(size){
memcpy(buf, pri->ibuf, size);
memmove(pri->ibuf, &pri->ibuf[i], pri->more-i);
pri->more=pri->more-i;
return(size);
}
}
pri->more=0;
}
n = net_read(fd, &pri->ibuf[pri->pos], sizeof(pri->ibuf) - pri->pos);
if(n <= 0) return(n);
start = pri->pos;
for(i = 0; i < n; i++){
size = slip_unesc(pri->ibuf[start + i],
pri->ibuf,&pri->pos,&pri->esc);
if(size){
memcpy(buf, pri->ibuf, size);
memmove(pri->ibuf, &pri->ibuf[start+i+1], n-(i+1));
pri->more=n-(i+1);
return(size);
}
}
return(0);
} }
int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri) int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri)
{ {
int actual, n; return slip_proto_write(fd, buf, len, &pri->slip);
actual = slip_esc(buf, pri->obuf, len);
n = net_write(fd, pri->obuf, actual);
if(n < 0) return(n);
else return(len);
} }
static int slirp_set_mtu(int mtu, void *data) static int slirp_set_mtu(int mtu, void *data)
@ -188,14 +137,3 @@ struct net_user_info slirp_user_info = {
.delete_address = NULL, .delete_address = NULL,
.max_packet = BUF_SIZE .max_packet = BUF_SIZE
}; };
/*
* Overrides for Emacs so that we follow Linus's tabbing style.
* Emacs will notice this stuff at the end of the file and automatically
* adjust the settings for this buffer only. This must remain at the end
* of the file.
* ---------------------------------------------------------------------------
* Local variables:
* c-file-style: "linux"
* End:
*/