Commit f06885b3 authored by Anton Ivanov's avatar Anton Ivanov Committed by Richard Weinberger

um: vector: Add dynamic tap interfaces and scripting

Provide functionality roughly compatible with the existing qemu
ifup scripting:
* invocation of an ifup script. The interface name is passed as the
  first and only argument
* allocating tap interfaces on the fly if they are not explicitly
  specified
Signed-off-by: default avatarAnton Ivanov <anton.ivanov@cambridgegreys.com>
Signed-off-by: default avatarRichard Weinberger <richard@nod.at>
parent 273fe1b6
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#define ID_MAX 2 #define ID_MAX 2
#define TOKEN_IFNAME "ifname" #define TOKEN_IFNAME "ifname"
#define TOKEN_SCRIPT "ifup"
#define TRANS_RAW "raw" #define TRANS_RAW "raw"
#define TRANS_RAW_LEN strlen(TRANS_RAW) #define TRANS_RAW_LEN strlen(TRANS_RAW)
...@@ -53,6 +54,9 @@ ...@@ -53,6 +54,9 @@
#define MAX_UN_LEN 107 #define MAX_UN_LEN 107
static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char *template = "tapXXXXXX";
/* This is very ugly and brute force lookup, but it is done /* This is very ugly and brute force lookup, but it is done
* only once at initialization so not worth doing hashes or * only once at initialization so not worth doing hashes or
* anything more intelligent * anything more intelligent
...@@ -189,16 +193,21 @@ static int create_raw_fd(char *iface, int flags, int proto) ...@@ -189,16 +193,21 @@ static int create_raw_fd(char *iface, int flags, int proto)
return err; return err;
} }
static struct vector_fds *user_init_tap_fds(struct arglist *ifspec) static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
{ {
int fd = -1; int fd = -1, i;
char *iface; char *iface;
struct vector_fds *result = NULL; struct vector_fds *result = NULL;
bool dynamic = false;
char dynamic_ifname[IFNAMSIZ];
char *argv[] = {NULL, NULL, NULL, NULL};
iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME); iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
if (iface == NULL) { if (iface == NULL) {
printk(UM_KERN_ERR "uml_tap: failed to parse interface spec\n"); dynamic = true;
goto tap_cleanup; iface = dynamic_ifname;
srand(getpid());
} }
result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL); result = uml_kmalloc(sizeof(struct vector_fds), UM_GFP_KERNEL);
...@@ -212,14 +221,30 @@ static struct vector_fds *user_init_tap_fds(struct arglist *ifspec) ...@@ -212,14 +221,30 @@ static struct vector_fds *user_init_tap_fds(struct arglist *ifspec)
result->remote_addr_size = 0; result->remote_addr_size = 0;
/* TAP */ /* TAP */
do {
if (dynamic) {
strcpy(iface, template);
for (i = 0; i < strlen(iface); i++) {
if (iface[i] == 'X') {
iface[i] = padchar[rand() % strlen(padchar)];
}
}
}
fd = create_tap_fd(iface); fd = create_tap_fd(iface);
if (fd < 0) { if ((fd < 0) && (!dynamic)) {
printk(UM_KERN_ERR "uml_tap: failed to create tun interface\n"); printk(UM_KERN_ERR "uml_tap: failed to create tun interface\n");
goto tap_cleanup; goto tap_cleanup;
} }
result->tx_fd = fd; result->tx_fd = fd;
result->rx_fd = fd; result->rx_fd = fd;
} while (fd < 0);
argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
if (argv[0]) {
argv[1] = iface;
run_helper(NULL, NULL, argv);
}
return result; return result;
tap_cleanup: tap_cleanup:
printk(UM_KERN_ERR "user_init_tap: init failed, error %d", fd); printk(UM_KERN_ERR "user_init_tap: init failed, error %d", fd);
...@@ -231,6 +256,7 @@ static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec) ...@@ -231,6 +256,7 @@ static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec)
{ {
char *iface; char *iface;
struct vector_fds *result = NULL; struct vector_fds *result = NULL;
char *argv[] = {NULL, NULL, NULL, NULL};
iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME); iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
if (iface == NULL) { if (iface == NULL) {
...@@ -264,6 +290,12 @@ static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec) ...@@ -264,6 +290,12 @@ static struct vector_fds *user_init_hybrid_fds(struct arglist *ifspec)
"uml_tap: failed to create paired raw socket: %i\n", result->rx_fd); "uml_tap: failed to create paired raw socket: %i\n", result->rx_fd);
goto hybrid_cleanup; goto hybrid_cleanup;
} }
argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
if (argv[0]) {
argv[1] = iface;
run_helper(NULL, NULL, argv);
}
return result; return result;
hybrid_cleanup: hybrid_cleanup:
printk(UM_KERN_ERR "user_init_hybrid: init failed"); printk(UM_KERN_ERR "user_init_hybrid: init failed");
...@@ -407,6 +439,7 @@ static struct vector_fds *user_init_raw_fds(struct arglist *ifspec) ...@@ -407,6 +439,7 @@ static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
int err = -ENOMEM; int err = -ENOMEM;
char *iface; char *iface;
struct vector_fds *result = NULL; struct vector_fds *result = NULL;
char *argv[] = {NULL, NULL, NULL, NULL};
iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME); iface = uml_vector_fetch_arg(ifspec, TOKEN_IFNAME);
if (iface == NULL) if (iface == NULL)
...@@ -429,6 +462,11 @@ static struct vector_fds *user_init_raw_fds(struct arglist *ifspec) ...@@ -429,6 +462,11 @@ static struct vector_fds *user_init_raw_fds(struct arglist *ifspec)
result->remote_addr = NULL; result->remote_addr = NULL;
result->remote_addr_size = 0; result->remote_addr_size = 0;
} }
argv[0] = uml_vector_fetch_arg(ifspec, TOKEN_SCRIPT);
if (argv[0]) {
argv[1] = iface;
run_helper(NULL, NULL, argv);
}
return result; return result;
raw_cleanup: raw_cleanup:
printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err); printk(UM_KERN_ERR "user_init_raw: init failed, error %d", err);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment