diff --git a/Documentation/lkl.txt b/Documentation/lkl.txt index b19dadf0335cef..631a8831f6641d 100644 --- a/Documentation/lkl.txt +++ b/Documentation/lkl.txt @@ -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. diff --git a/arch/lkl/Kconfig b/arch/lkl/Kconfig index 1e808d6691a678..568364a9fcdfad 100644 --- a/arch/lkl/Kconfig +++ b/arch/lkl/Kconfig @@ -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 diff --git a/tools/lkl/lib/hijack/init.c b/tools/lkl/lib/hijack/init.c index 43fc8b9e987ae4..01788760f880aa 100644 --- a/tools/lkl/lib/hijack/init.c +++ b/tools/lkl/lib/hijack/init.c @@ -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) { @@ -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; diff --git a/tools/lkl/tests/net-test.c b/tools/lkl/tests/net-test.c index 8dcdef7978c0cc..c41b066cc6d55d 100644 --- a/tools/lkl/tests/net-test.c +++ b/tools/lkl/tests/net-test.c @@ -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; @@ -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 [gateway]\n", argv[0]); + if (argc < 3) { + printf("usage %s |dhcp [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]; @@ -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; diff --git a/tools/lkl/tests/net.sh b/tools/lkl/tests/net.sh index 2fb1f74b040f82..49c289d2da86e6 100755 --- a/tools/lkl/tests/net.sh +++ b/tools/lkl/tests/net.sh @@ -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 { @@ -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 @@ -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 @@ -44,7 +52,7 @@ 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 @@ -52,5 +60,5 @@ fi # 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