Skip to content

Commit

Permalink
Merge pull request torvalds#275 from libos-nuse/feature-dhclient
Browse files Browse the repository at this point in the history
lkl: add dhcp client option
  • Loading branch information
thehajime committed Nov 29, 2016
2 parents b34b438 + 817b3ac commit e5d3c5c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
6 changes: 6 additions & 0 deletions Documentation/lkl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ are the list of those variable for your environment.
```
$ LKL_HIJACK_NET_IP=198.51.100.5 lkl-hijack.sh ip address show
```

Additionally, DHCP is experimentally available with the following syntax.
```
$ LKL_HIJACK_NET_IP=dhcp LKL_HIJACK_NET_IFTYPE=tap LKL_HIJACK_NET_IFPARAMS=tap0 lkl-hijack.sh ip address show
```

* LKL_HIJACK_NET_NETMASK_LEN

the network mask length of the interface specified by LKL_HIJACK_NET_TAP.
Expand Down
2 changes: 2 additions & 0 deletions arch/lkl/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ config LKL
select 64BIT if OUTPUT_FORMAT = "elf64-x86-64"
select HAVE_UNDERSCORE_SYMBOL_PREFIX if OUTPUT_FORMAT = "pe-i386"
select 64BIT if OUTPUT_FORMAT = "elf64-x86-64-freebsd"
select IP_PNP
select IP_PNP_DHCP

config OUTPUTFORMAT
string
Expand Down
8 changes: 7 additions & 1 deletion tools/lkl/lib/hijack/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ hijack_init(void)
cpu_set_t ori_cpu;
char *offload1 = getenv("LKL_HIJACK_OFFLOAD");
int offload = 0;
/* with dhcp client by default */
char *boot_cmdline = "ip=dhcp";

memset(&nd_args, 0, sizeof(struct lkl_netdev_args));
if (!debug) {
Expand Down Expand Up @@ -323,7 +325,11 @@ hijack_init(void)
if (single_cpu_mode == 1)
PinToFirstCpu(&ori_cpu);

ret = lkl_start_kernel(&lkl_host_ops, 64 * 1024 * 1024UL, "");
/* clear cmdline if non-dhcp case */
if ((ip && strcmp(ip, "dhcp")) || (nd_id == -1))
boot_cmdline = "";

ret = lkl_start_kernel(&lkl_host_ops, 64 * 1024 * 1024UL, boot_cmdline);
if (ret) {
fprintf(stderr, "can't start kernel: %s\n", lkl_strerror(ret));
return;
Expand Down
21 changes: 14 additions & 7 deletions tools/lkl/tests/net-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ static int test_icmp(char *str, int len)

iph = (struct iphdr *)buf;
icmp = (struct icmphdr *)(buf + iph->ihl * 4);
if (icmp->type != ICMP_ECHOREPLY || icmp->code != 0) {
/* DHCP server may issue an ICMP echo request to a dhcp client */
if ((icmp->type != ICMP_ECHOREPLY || icmp->code != 0) &&
(icmp->type != ICMP_ECHO)) {
snprintf(str, len, "no ICMP echo reply (type=%d, code=%d)",
icmp->type, icmp->code);
return TEST_FAILURE;
Expand All @@ -114,17 +116,19 @@ static int test_net_init(int argc, char **argv)
char *debug = getenv("LKL_DEBUG");
int ret, nd_id = -1, nd_ifindex = -1;
struct lkl_netdev *nd = NULL;
/* with dhcp client by default */
char boot_cmdline[256] = "\0";

if (argc < 6) {
printf("usage %s <iftype: tap|dpdk|raw> <ifname> <v4addr> <v4mask> <dstaddr> [gateway]\n", argv[0]);
if (argc < 3) {
printf("usage %s <iftype: tap|dpdk|raw> <ifname> <dstaddr> <v4addr>|dhcp <v4mask> [gateway]\n", argv[0]);
exit(0);
}

iftype = argv[1];
ifname = argv[2];
ip = argv[3];
netmask_len = argv[4];
dst = argv[5];
dst = argv[3];
ip = argv[4];
netmask_len = argv[5];

if (argc == 7)
gateway = argv[6];
Expand Down Expand Up @@ -156,7 +160,10 @@ static int test_net_init(int argc, char **argv)
lkl_host_ops.print = NULL;


ret = lkl_start_kernel(&lkl_host_ops, 64 * 1024 * 1024, "");
if (ip && !strcmp(ip, "dhcp"))
snprintf(boot_cmdline, sizeof(boot_cmdline), "ip=dhcp");

ret = lkl_start_kernel(&lkl_host_ops, 64 * 1024 * 1024, boot_cmdline);
if (ret) {
fprintf(stderr, "can't start kernel: %s\n", lkl_strerror(ret));
return -1;
Expand Down
18 changes: 13 additions & 5 deletions tools/lkl/tests/net.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

IFNAME=`ip route |grep default | awk '{print $5}'`
GW=`ip route |grep default | awk '{print $3}'`
IPADDR=`echo $GW | sed -r "s/([0-9]+\.[0-9]+\.[0-9]+\.)([0-9]+)$/\1\`expr \2 + 10\`/"`
HOST_IPADDR=`ip rou |grep ${IFNAME} | grep "scope link" | awk '{print $1}' | sed "s/\(.*\)\/.*/\1/"`
PLEN=`ip rou |grep ${IFNAME} | grep "scope link" | awk '{print $1}' | sed "s/.*\/\(.*\)/\1/"`
IPADDR=`echo ${HOST_IPADDR}|awk -F. '{printf ("%d.%d.%d.%d\n",$1,$2,$3,$4+10)}'`

script_dir=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd)
cd ${script_dir}

# And make sure we clean up when we're done
function clear_work_dir {
Expand All @@ -22,7 +26,7 @@ if [ -c /dev/net/tun ]; then
sudo ip link set dev lkl_ptt1 up
sudo ip addr add dev lkl_ptt1 192.168.14.1/24

./net-test tap lkl_ptt1 192.168.14.2 24 192.168.14.1
./net-test tap lkl_ptt1 192.168.14.1 192.168.14.2 24

sudo ip link set dev lkl_ptt1 down
sudo ip tuntap del dev lkl_ptt1 mode tap
Expand All @@ -33,7 +37,11 @@ echo "== RAW socket (LKL net) tests =="
if [ -n "`printenv CONFIG_AUTO_LKL_POSIX_HOST`" ] ; then
sudo ip link set dev ${IFNAME} promisc on
# this won't work if IFNAME is wifi since it rewrites the src macaddr
sudo ./net-test raw ${IFNAME} ${IPADDR} ${PLEN} 8.8.8.8 ${GW}
sudo ./net-test raw ${IFNAME} 8.8.8.8 ${IPADDR} ${PLEN} ${GW}

# DHCP test
echo " == DHCP with RAW socket test =="
sudo ./net-test raw ${IFNAME} 8.8.8.8 dhcp
sudo ip link set dev ${IFNAME} promisc off
fi

Expand All @@ -44,13 +52,13 @@ if ls /dev/tap* > /dev/null 2>&1 ; then
sudo ip link set dev lkl_vtap0 up
sudo chown ${USER} `ls /dev/tap*`

./net-test macvtap `ls /dev/tap*` ${IPADDR} ${PLEN} 8.8.8.8 ${GW}
./net-test macvtap `ls /dev/tap*` 8.8.8.8 ${IPADDR} ${PLEN} ${GW}
fi

# we disabled this DPDK test because it's unlikely possible to describe
# a generic set of commands for all environments to test with DPDK. users
# may customize those test commands for your host.
if false ; then
echo "== DPDK (LKL net) tests =="
sudo ./net-test dpdk dpdk0 192.168.15.2 24 192.168.15.1
sudo ./net-test dpdk dpdk0 192.168.15.1 192.168.15.2 24
fi

0 comments on commit e5d3c5c

Please sign in to comment.