Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

[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>

authored by

Jeff Dike and committed by
Linus Torvalds
a3c77c67 98fdffcc

+179 -243
+3 -3
arch/um/drivers/Makefile
··· 22 22 obj-$(CONFIG_SSL) += ssl.o 23 23 obj-$(CONFIG_STDERR_CONSOLE) += stderr_console.o 24 24 25 - obj-$(CONFIG_UML_NET_SLIP) += slip.o 26 - obj-$(CONFIG_UML_NET_SLIRP) += slirp.o 25 + obj-$(CONFIG_UML_NET_SLIP) += slip.o slip_common.o 26 + obj-$(CONFIG_UML_NET_SLIRP) += slirp.o slip_common.o 27 27 obj-$(CONFIG_UML_NET_DAEMON) += daemon.o 28 28 obj-$(CONFIG_UML_NET_MCAST) += mcast.o 29 29 #obj-$(CONFIG_UML_NET_PCAP) += pcap.o $(PCAP) ··· 41 41 obj-$(CONFIG_BLK_DEV_COW_COMMON) += cow_user.o 42 42 obj-$(CONFIG_UML_RANDOM) += random.o 43 43 44 - USER_OBJS := fd.o null.o pty.o tty.o xterm.o 44 + USER_OBJS := fd.o null.o pty.o tty.o xterm.o slip_common.o 45 45 46 46 include arch/um/scripts/Makefile.rules
+2 -21
arch/um/drivers/slip.h
··· 1 1 #ifndef __UM_SLIP_H 2 2 #define __UM_SLIP_H 3 3 4 - #define BUF_SIZE 1500 5 - /* two bytes each for a (pathological) max packet of escaped chars + * 6 - * terminating END char + initial END char */ 7 - #define ENC_BUF_SIZE (2 * BUF_SIZE + 2) 4 + #include "slip_common.h" 8 5 9 6 struct slip_data { 10 7 void *dev; ··· 9 12 char *addr; 10 13 char *gate_addr; 11 14 int slave; 12 - unsigned char ibuf[ENC_BUF_SIZE]; 13 - unsigned char obuf[ENC_BUF_SIZE]; 14 - int more; /* more data: do not read fd until ibuf has been drained */ 15 - int pos; 16 - int esc; 15 + struct slip_proto slip; 17 16 }; 18 17 19 18 extern struct net_user_info slip_user_info; 20 19 21 - extern int set_umn_addr(int fd, char *addr, char *ptp_addr); 22 20 extern int slip_user_read(int fd, void *buf, int len, struct slip_data *pri); 23 21 extern int slip_user_write(int fd, void *buf, int len, struct slip_data *pri); 24 22 25 23 #endif 26 - 27 - /* 28 - * Overrides for Emacs so that we follow Linus's tabbing style. 29 - * Emacs will notice this stuff at the end of the file and automatically 30 - * adjust the settings for this buffer only. This must remain at the end 31 - * of the file. 32 - * --------------------------------------------------------------------------- 33 - * Local variables: 34 - * c-file-style: "linux" 35 - * End: 36 - */
+54
arch/um/drivers/slip_common.c
··· 1 + #include <string.h> 2 + #include "slip_common.h" 3 + #include "net_user.h" 4 + 5 + int slip_proto_read(int fd, void *buf, int len, struct slip_proto *slip) 6 + { 7 + int i, n, size, start; 8 + 9 + if(slip->more > 0){ 10 + i = 0; 11 + while(i < slip->more){ 12 + size = slip_unesc(slip->ibuf[i++], slip->ibuf, 13 + &slip->pos, &slip->esc); 14 + if(size){ 15 + memcpy(buf, slip->ibuf, size); 16 + memmove(slip->ibuf, &slip->ibuf[i], 17 + slip->more - i); 18 + slip->more = slip->more - i; 19 + return size; 20 + } 21 + } 22 + slip->more = 0; 23 + } 24 + 25 + n = net_read(fd, &slip->ibuf[slip->pos], 26 + sizeof(slip->ibuf) - slip->pos); 27 + if(n <= 0) 28 + return n; 29 + 30 + start = slip->pos; 31 + for(i = 0; i < n; i++){ 32 + size = slip_unesc(slip->ibuf[start + i], slip->ibuf,&slip->pos, 33 + &slip->esc); 34 + if(size){ 35 + memcpy(buf, slip->ibuf, size); 36 + memmove(slip->ibuf, &slip->ibuf[start+i+1], 37 + n - (i + 1)); 38 + slip->more = n - (i + 1); 39 + return size; 40 + } 41 + } 42 + return 0; 43 + } 44 + 45 + int slip_proto_write(int fd, void *buf, int len, struct slip_proto *slip) 46 + { 47 + int actual, n; 48 + 49 + actual = slip_esc(buf, slip->obuf, len); 50 + n = net_write(fd, slip->obuf, actual); 51 + if(n < 0) 52 + return n; 53 + else return len; 54 + }
+6 -6
arch/um/drivers/slip_kern.c
··· 26 26 .addr = NULL, 27 27 .gate_addr = init->gate_addr, 28 28 .slave = -1, 29 - .ibuf = { '\0' }, 30 - .obuf = { '\0' }, 31 - .pos = 0, 32 - .esc = 0, 29 + .slip = SLIP_PROTO_INIT, 33 30 .dev = dev }); 34 31 35 32 dev->init = NULL; 33 + dev->header_cache_update = NULL; 34 + dev->hard_header_cache = NULL; 35 + dev->hard_header = NULL; 36 36 dev->hard_header_len = 0; 37 - dev->addr_len = 4; 38 - dev->type = ARPHRD_ETHER; 37 + dev->addr_len = 0; 38 + dev->type = ARPHRD_SLIP; 39 39 dev->tx_queue_len = 256; 40 40 dev->flags = IFF_NOARP; 41 41 printk("SLIP backend - SLIP IP = %s\n", spri->gate_addr);
+27 -17
arch/um/drivers/slip_proto.h arch/um/drivers/slip_common.h
··· 1 - /* 2 - * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) 3 - * Licensed under the GPL 4 - */ 1 + #ifndef __UM_SLIP_COMMON_H 2 + #define __UM_SLIP_COMMON_H 5 3 6 - #ifndef __UM_SLIP_PROTO_H__ 7 - #define __UM_SLIP_PROTO_H__ 4 + #define BUF_SIZE 1500 5 + /* two bytes each for a (pathological) max packet of escaped chars + * 6 + * terminating END char + initial END char */ 7 + #define ENC_BUF_SIZE (2 * BUF_SIZE + 2) 8 8 9 9 /* SLIP protocol characters. */ 10 10 #define SLIP_END 0300 /* indicates end of frame */ ··· 80 80 return (ptr - d); 81 81 } 82 82 83 - #endif 83 + struct slip_proto { 84 + unsigned char ibuf[ENC_BUF_SIZE]; 85 + unsigned char obuf[ENC_BUF_SIZE]; 86 + int more; /* more data: do not read fd until ibuf has been drained */ 87 + int pos; 88 + int esc; 89 + }; 84 90 85 - /* 86 - * Overrides for Emacs so that we follow Linus's tabbing style. 87 - * Emacs will notice this stuff at the end of the file and automatically 88 - * adjust the settings for this buffer only. This must remain at the end 89 - * of the file. 90 - * --------------------------------------------------------------------------- 91 - * Local variables: 92 - * c-file-style: "linux" 93 - * End: 94 - */ 91 + #define SLIP_PROTO_INIT { \ 92 + .ibuf = { '\0' }, \ 93 + .obuf = { '\0' }, \ 94 + .more = 0, \ 95 + .pos = 0, \ 96 + .esc = 0 \ 97 + } 98 + 99 + extern int slip_proto_read(int fd, void *buf, int len, 100 + struct slip_proto *slip); 101 + extern int slip_proto_write(int fd, void *buf, int len, 102 + struct slip_proto *slip); 103 + 104 + #endif
+63 -89
arch/um/drivers/slip_user.c
··· 13 13 #include "user.h" 14 14 #include "net_user.h" 15 15 #include "slip.h" 16 - #include "slip_proto.h" 16 + #include "slip_common.h" 17 17 #include "helper.h" 18 18 #include "os.h" 19 19 ··· 77 77 err = os_pipe(fds, 1, 0); 78 78 if(err < 0){ 79 79 printk("slip_tramp : pipe failed, err = %d\n", -err); 80 - return(err); 80 + goto out; 81 81 } 82 82 83 83 err = 0; 84 84 pe_data.stdin = fd; 85 85 pe_data.stdout = fds[1]; 86 86 pe_data.close_me = fds[0]; 87 - pid = run_helper(slip_pre_exec, &pe_data, argv, NULL); 87 + err = run_helper(slip_pre_exec, &pe_data, argv, NULL); 88 + if(err < 0) 89 + goto out_close; 90 + pid = err; 88 91 89 - if(pid < 0) err = pid; 90 - else { 91 - output_len = page_size(); 92 - output = um_kmalloc(output_len); 93 - if(output == NULL) 94 - printk("slip_tramp : failed to allocate output " 95 - "buffer\n"); 96 - 97 - os_close_file(fds[1]); 98 - read_output(fds[0], output, output_len); 99 - if(output != NULL){ 100 - printk("%s", output); 101 - kfree(output); 102 - } 103 - CATCH_EINTR(err = waitpid(pid, &status, 0)); 104 - if(err < 0) 105 - err = errno; 106 - else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){ 107 - printk("'%s' didn't exit with status 0\n", argv[0]); 108 - err = -EINVAL; 109 - } 92 + output_len = page_size(); 93 + output = um_kmalloc(output_len); 94 + if(output == NULL){ 95 + printk("slip_tramp : failed to allocate output buffer\n"); 96 + os_kill_process(pid, 1); 97 + err = -ENOMEM; 98 + goto out_free; 110 99 } 100 + 101 + os_close_file(fds[1]); 102 + read_output(fds[0], output, output_len); 103 + printk("%s", output); 104 + 105 + CATCH_EINTR(err = waitpid(pid, &status, 0)); 106 + if(err < 0) 107 + err = errno; 108 + else if(!WIFEXITED(status) || (WEXITSTATUS(status) != 0)){ 109 + printk("'%s' didn't exit with status 0\n", argv[0]); 110 + err = -EINVAL; 111 + } 112 + else err = 0; 111 113 112 114 os_close_file(fds[0]); 113 115 114 - return(err); 116 + out_free: 117 + kfree(output); 118 + return err; 119 + 120 + out_close: 121 + os_close_file(fds[0]); 122 + os_close_file(fds[1]); 123 + out: 124 + return err; 115 125 } 116 126 117 127 static int slip_open(void *data) ··· 133 123 NULL }; 134 124 int sfd, mfd, err; 135 125 136 - mfd = get_pty(); 137 - if(mfd < 0){ 138 - printk("umn : Failed to open pty, err = %d\n", -mfd); 139 - return(mfd); 126 + err = get_pty(); 127 + if(err < 0){ 128 + printk("slip-open : Failed to open pty, err = %d\n", -err); 129 + goto out; 140 130 } 141 - sfd = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0); 142 - if(sfd < 0){ 143 - printk("Couldn't open tty for slip line, err = %d\n", -sfd); 144 - os_close_file(mfd); 145 - return(sfd); 131 + mfd = err; 132 + 133 + err = os_open_file(ptsname(mfd), of_rdwr(OPENFLAGS()), 0); 134 + if(err < 0){ 135 + printk("Couldn't open tty for slip line, err = %d\n", -err); 136 + goto out_close; 146 137 } 147 - if(set_up_tty(sfd)) return(-1); 138 + sfd = err; 139 + 140 + if(set_up_tty(sfd)) 141 + goto out_close2; 142 + 148 143 pri->slave = sfd; 149 - pri->pos = 0; 150 - pri->esc = 0; 144 + pri->slip.pos = 0; 145 + pri->slip.esc = 0; 151 146 if(pri->gate_addr != NULL){ 152 147 sprintf(version_buf, "%d", UML_NET_VERSION); 153 148 strcpy(gate_buf, pri->gate_addr); ··· 161 146 162 147 if(err < 0){ 163 148 printk("slip_tramp failed - err = %d\n", -err); 164 - return(err); 149 + goto out_close2; 165 150 } 166 151 err = os_get_ifname(pri->slave, pri->name); 167 152 if(err < 0){ 168 153 printk("get_ifname failed, err = %d\n", -err); 169 - return(err); 154 + goto out_close2; 170 155 } 171 156 iter_addresses(pri->dev, open_addr, pri->name); 172 157 } ··· 175 160 if(err < 0){ 176 161 printk("Failed to set slip discipline encapsulation - " 177 162 "err = %d\n", -err); 178 - return(err); 163 + goto out_close2; 179 164 } 180 165 } 181 166 return(mfd); 167 + out_close2: 168 + os_close_file(sfd); 169 + out_close: 170 + os_close_file(mfd); 171 + out: 172 + return err; 182 173 } 183 174 184 175 static void slip_close(int fd, void *data) ··· 211 190 212 191 int slip_user_read(int fd, void *buf, int len, struct slip_data *pri) 213 192 { 214 - int i, n, size, start; 215 - 216 - if(pri->more>0) { 217 - i = 0; 218 - while(i < pri->more) { 219 - size = slip_unesc(pri->ibuf[i++], 220 - pri->ibuf, &pri->pos, &pri->esc); 221 - if(size){ 222 - memcpy(buf, pri->ibuf, size); 223 - memmove(pri->ibuf, &pri->ibuf[i], pri->more-i); 224 - pri->more=pri->more-i; 225 - return(size); 226 - } 227 - } 228 - pri->more=0; 229 - } 230 - 231 - n = net_read(fd, &pri->ibuf[pri->pos], sizeof(pri->ibuf) - pri->pos); 232 - if(n <= 0) return(n); 233 - 234 - start = pri->pos; 235 - for(i = 0; i < n; i++){ 236 - size = slip_unesc(pri->ibuf[start + i], 237 - pri->ibuf, &pri->pos, &pri->esc); 238 - if(size){ 239 - memcpy(buf, pri->ibuf, size); 240 - memmove(pri->ibuf, &pri->ibuf[start+i+1], n-(i+1)); 241 - pri->more=n-(i+1); 242 - return(size); 243 - } 244 - } 245 - return(0); 193 + return slip_proto_read(fd, buf, len, &pri->slip); 246 194 } 247 195 248 196 int slip_user_write(int fd, void *buf, int len, struct slip_data *pri) 249 197 { 250 - int actual, n; 251 - 252 - actual = slip_esc(buf, pri->obuf, len); 253 - n = net_write(fd, pri->obuf, actual); 254 - if(n < 0) return(n); 255 - else return(len); 198 + return slip_proto_write(fd, buf, len, &pri->slip); 256 199 } 257 200 258 201 static int slip_set_mtu(int mtu, void *data) ··· 252 267 .delete_address = slip_del_addr, 253 268 .max_packet = BUF_SIZE 254 269 }; 255 - 256 - /* 257 - * Overrides for Emacs so that we follow Linus's tabbing style. 258 - * Emacs will notice this stuff at the end of the file and automatically 259 - * adjust the settings for this buffer only. This must remain at the end 260 - * of the file. 261 - * --------------------------------------------------------------------------- 262 - * Local variables: 263 - * c-file-style: "linux" 264 - * End: 265 - */
+4 -22
arch/um/drivers/slirp.h
··· 1 1 #ifndef __UM_SLIRP_H 2 2 #define __UM_SLIRP_H 3 3 4 - #define BUF_SIZE 1500 5 - /* two bytes each for a (pathological) max packet of escaped chars + * 6 - * terminating END char + initial END char */ 7 - #define ENC_BUF_SIZE (2 * BUF_SIZE + 2) 4 + #include "slip_common.h" 8 5 9 6 #define SLIRP_MAX_ARGS 100 10 7 /* ··· 21 24 struct arg_list_dummy_wrapper argw; 22 25 int pid; 23 26 int slave; 24 - unsigned char ibuf[ENC_BUF_SIZE]; 25 - unsigned char obuf[ENC_BUF_SIZE]; 26 - int more; /* more data: do not read fd until ibuf has been drained */ 27 - int pos; 28 - int esc; 27 + struct slip_proto slip; 29 28 }; 30 29 31 30 extern struct net_user_info slirp_user_info; 32 31 33 - extern int set_umn_addr(int fd, char *addr, char *ptp_addr); 34 32 extern int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri); 35 - extern int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri); 33 + extern int slirp_user_write(int fd, void *buf, int len, 34 + struct slirp_data *pri); 36 35 37 36 #endif 38 - 39 - /* 40 - * Overrides for Emacs so that we follow Linus's tabbing style. 41 - * Emacs will notice this stuff at the end of the file and automatically 42 - * adjust the settings for this buffer only. This must remain at the end 43 - * of the file. 44 - * --------------------------------------------------------------------------- 45 - * Local variables: 46 - * c-file-style: "linux" 47 - * End: 48 - */
+1 -4
arch/um/drivers/slirp_kern.c
··· 25 25 { .argw = init->argw, 26 26 .pid = -1, 27 27 .slave = -1, 28 - .ibuf = { '\0' }, 29 - .obuf = { '\0' }, 30 - .pos = 0, 31 - .esc = 0, 28 + .slip = SLIP_PROTO_INIT, 32 29 .dev = dev }); 33 30 34 31 dev->init = NULL;
+19 -81
arch/um/drivers/slirp_user.c
··· 12 12 #include "user.h" 13 13 #include "net_user.h" 14 14 #include "slirp.h" 15 - #include "slip_proto.h" 15 + #include "slip_common.h" 16 16 #include "helper.h" 17 17 #include "os.h" 18 18 ··· 48 48 return(pid); 49 49 } 50 50 51 - /* XXX This is just a trivial wrapper around os_pipe */ 52 - static int slirp_datachan(int *mfd, int *sfd) 53 - { 54 - int fds[2], err; 55 - 56 - err = os_pipe(fds, 1, 1); 57 - if(err < 0){ 58 - printk("slirp_datachan: Failed to open pipe, err = %d\n", -err); 59 - return(err); 60 - } 61 - 62 - *mfd = fds[0]; 63 - *sfd = fds[1]; 64 - return(0); 65 - } 66 - 67 51 static int slirp_open(void *data) 68 52 { 69 53 struct slirp_data *pri = data; 70 - int sfd, mfd, pid, err; 54 + int fds[2], pid, err; 71 55 72 - err = slirp_datachan(&mfd, &sfd); 56 + err = os_pipe(fds, 1, 1); 73 57 if(err) 74 58 return(err); 75 59 76 - pid = slirp_tramp(pri->argw.argv, sfd); 77 - 78 - if(pid < 0){ 79 - printk("slirp_tramp failed - errno = %d\n", -pid); 80 - os_close_file(sfd); 81 - os_close_file(mfd); 82 - return(pid); 60 + err = slirp_tramp(pri->argw.argv, fds[1]); 61 + if(err < 0){ 62 + printk("slirp_tramp failed - errno = %d\n", -err); 63 + goto out; 83 64 } 65 + pid = err; 84 66 85 - pri->slave = sfd; 86 - pri->pos = 0; 87 - pri->esc = 0; 67 + pri->slave = fds[1]; 68 + pri->slip.pos = 0; 69 + pri->slip.esc = 0; 70 + pri->pid = err; 88 71 89 - pri->pid = pid; 90 - 91 - return(mfd); 72 + return(fds[0]); 73 + out: 74 + os_close_file(fds[0]); 75 + os_close_file(fds[1]); 76 + return err; 92 77 } 93 78 94 79 static void slirp_close(int fd, void *data) ··· 114 129 115 130 int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri) 116 131 { 117 - int i, n, size, start; 118 - 119 - if(pri->more>0) { 120 - i = 0; 121 - while(i < pri->more) { 122 - size = slip_unesc(pri->ibuf[i++], 123 - pri->ibuf,&pri->pos,&pri->esc); 124 - if(size){ 125 - memcpy(buf, pri->ibuf, size); 126 - memmove(pri->ibuf, &pri->ibuf[i], pri->more-i); 127 - pri->more=pri->more-i; 128 - return(size); 129 - } 130 - } 131 - pri->more=0; 132 - } 133 - 134 - n = net_read(fd, &pri->ibuf[pri->pos], sizeof(pri->ibuf) - pri->pos); 135 - if(n <= 0) return(n); 136 - 137 - start = pri->pos; 138 - for(i = 0; i < n; i++){ 139 - size = slip_unesc(pri->ibuf[start + i], 140 - pri->ibuf,&pri->pos,&pri->esc); 141 - if(size){ 142 - memcpy(buf, pri->ibuf, size); 143 - memmove(pri->ibuf, &pri->ibuf[start+i+1], n-(i+1)); 144 - pri->more=n-(i+1); 145 - return(size); 146 - } 147 - } 148 - return(0); 132 + return slip_proto_read(fd, buf, len, &pri->slip); 149 133 } 150 134 151 135 int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri) 152 136 { 153 - int actual, n; 154 - 155 - actual = slip_esc(buf, pri->obuf, len); 156 - n = net_write(fd, pri->obuf, actual); 157 - if(n < 0) return(n); 158 - else return(len); 137 + return slip_proto_write(fd, buf, len, &pri->slip); 159 138 } 160 139 161 140 static int slirp_set_mtu(int mtu, void *data) ··· 137 188 .delete_address = NULL, 138 189 .max_packet = BUF_SIZE 139 190 }; 140 - 141 - /* 142 - * Overrides for Emacs so that we follow Linus's tabbing style. 143 - * Emacs will notice this stuff at the end of the file and automatically 144 - * adjust the settings for this buffer only. This must remain at the end 145 - * of the file. 146 - * --------------------------------------------------------------------------- 147 - * Local variables: 148 - * c-file-style: "linux" 149 - * End: 150 - */