From 7838b53763b4e0f866ec40da05445122a863ff26 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Tue, 3 Dec 2019 09:47:23 +0800 Subject: [PATCH] acrn-config: add virtio-net mediator support for launch config Add virtio-net mediator support to parse and get 'virtio-network' item value from launch config files, these values are editable by user. Tracked-On: #4185 Signed-off-by: Wei Liu Acked-by: Victor Sun --- misc/acrn-config/launch_config/com.py | 84 ++++++++++++------- misc/acrn-config/launch_config/launch_item.py | 1 + misc/acrn-config/library/common.py | 12 ++- 3 files changed, 66 insertions(+), 31 deletions(-) diff --git a/misc/acrn-config/launch_config/com.py b/misc/acrn-config/launch_config/com.py index 98957ae25..3f789f10f 100644 --- a/misc/acrn-config/launch_config/com.py +++ b/misc/acrn-config/launch_config/com.py @@ -33,11 +33,12 @@ def is_mount_needed(virt_io, vmid): return False -def tap_uos_net(names, vmid, config): +def tap_uos_net(names, virt_io, vmid, config): uos_type = names['uos_types'][vmid] board_name = names['board_name'] vm_name = launch_cfg_lib.undline_name(uos_type).lower() + if uos_type in ("CLEARLINUX", "ANDROID", "ALIOS"): if board_name in ("apl-mrb", "apl-up2"): print('if [ ! -f "/data/{}/{}.img" ]; then'.format(vm_name, vm_name), file=config) @@ -54,26 +55,13 @@ def tap_uos_net(names, vmid, config): if uos_type in ("VXWORKS", "ZEPHYR", "WINDOWS", "PREEMPT-RT LINUX"): print("vm_name={}_vm$1".format(vm_name), file=config) - if uos_type in ("CLEARLINUX", "ANDROID", "ALIOS"): - if board_name in ("apl-mrb", "apl-up2"): - print("# create a unique tap device for each VM", file=config) - print("tap=tap_$3", file=config) - print('tap_exist=$(ip a | grep "$tap" | awk \'{print $1}\')', file=config) - print('if [ "$tap_exist"x != "x" ]; then', file=config) - print(' echo "tap device existed, reuse $tap"', file=config) - print("else", file=config) - print(" ip tuntap add dev $tap mode tap", file=config) - print("fi", file=config) - print("", file=config) - print("# if acrn-br0 exists, add VM's unique tap device under it", file=config) - print("br_exist=$(ip a | grep acrn-br0 | awk '{print $1}')", file=config) - print('if [ "$br_exist"x != "x" -a "$tap_exist"x = "x" ]; then', file=config) - print(' echo "acrn-br0 bridge aleady exists, adding new tap device to it..."', file=config) - print(' ip link set "$tap" master acrn-br0', file=config) - print(' ip link set dev "$tap" down', file=config) - print(' ip link set dev "$tap" up', file=config) - print("fi", file=config) - print("", file=config) + for net in virt_io['network'][vmid]: + if net: + net_name = net + if ',' in net: + net_name = net.split(',')[0] + print("tap_net {}".format(net_name), file=config) + print("#check if the vm is running or not", file=config) print("vm_ps=$(pgrep -a -f acrn-dm)", file=config) print('result=$(echo $vm_ps | grep -w "${vm_name}")', file=config) @@ -252,9 +240,44 @@ def log_level_set(uos_type, config): print('logger_setting="--logger_setting console,level=4;kmsg,level=3;disk,level=5"', file=config) print("", file=config) +def tap_network(virt_io, vmid, config): + + none_i = 0 + tap_net_list = virt_io['network'][vmid] + for net in tap_net_list: + if net == None: + none_i += 1 + tap_net_num = len(tap_net_list) - none_i + + if tap_net_num >= 1: + print("function tap_net() {", file=config) + print("# create a unique tap device for each VM", file=config) + print("tap=$1", file=config) + print('tap_exist=$(ip a | grep "$tap" | awk \'{print $1}\')', file=config) + print('if [ "$tap_exist"x != "x" ]; then', file=config) + print(' echo "tap device existed, reuse $tap"', file=config) + print("else", file=config) + print(" ip tuntap add dev $tap mode tap", file=config) + print("fi", file=config) + print("", file=config) + print("# if acrn-br0 exists, add VM's unique tap device under it", file=config) + print("br_exist=$(ip a | grep acrn-br0 | awk '{print $1}')", file=config) + print('if [ "$br_exist"x != "x" -a "$tap_exist"x = "x" ]; then', file=config) + print(' echo "acrn-br0 bridge aleady exists, adding new tap device to it..."', file=config) + print(' ip link set "$tap" master acrn-br0', file=config) + print(' ip link set dev "$tap" down', file=config) + print(' ip link set dev "$tap" up', file=config) + print("fi", file=config) + print("}", file=config) + print("", file=config) + + +def launch_begin(names, virt_io, vmid, config): + board_name = names['board_name'] + uos_type = names['uos_types'][vmid] -def launch_begin(board_name, uos_type, config): launch_uos = launch_cfg_lib.undline_name(uos_type).lower() + tap_network(virt_io, vmid, config) run_container(board_name, uos_type, config) print("function launch_{}()".format(launch_uos), file=config) print("{", file=config) @@ -434,6 +457,14 @@ def virtio_args_set(dm, virt_io, vmid, config): if blk: print(" -s {},virtio-blk,{} \\".format(launch_cfg_lib.virtual_dev_slot("virtio-blk{}".format(blk)), blk.strip(':')), file=config) + # virtio-net set, the value type is a list + for net in virt_io['network'][vmid]: + if net: + net_name = net + if ',' in net: + net_name = net.split(',')[0] + print(" -s {},virtio-net,{} \\".format(launch_cfg_lib.virtual_dev_slot("virtio-net{}".format(net)), net_name), file=config) + def dm_arg_set(names, sel, virt_io, dm, vmid, config): @@ -473,7 +504,6 @@ def dm_arg_set(names, sel, virt_io, dm, vmid, config): print(" --pm_notify_channel uart \\", file=config) print(' --pm_by_vuart pty,/run/acrn/life_mngr_$vm_name \\', file=config) print(' -l com2,/run/acrn/life_mngr_$vm_name \\', file=config) - print(" -s {},virtio-net,tap0 \\".format(launch_cfg_lib.virtual_dev_slot("virtio-net")), file=config) # mac_seed if uos_type in ("CLEARLINUX", "ANDROID", "ALIOS"): @@ -525,10 +555,6 @@ def dm_arg_set(names, sel, virt_io, dm, vmid, config): launch_cfg_lib.virtual_dev_slot("virtio-console(hvc0)"), launch_cfg_lib.RE_CONSOLE_MAP['virtio-console(hvc0)']), file=config) - if uos_type in ("CLEARLINUX", "ANDROID", "ALIOS"): - if board_name in ("apl-mrb", "apl-up2"): - print(" -s {},virtio-net,$tap \\".format(launch_cfg_lib.virtual_dev_slot("virtio-net")), file=config) - if uos_type in ("CLEARLINUX", "ANDROID", "ALIOS"): print(" -s {},virtio-hyper_dmabuf \\".format(launch_cfg_lib.virtual_dev_slot("virtio-hyper_dmabuf")), file=config) if board_name == "apl-mrb": @@ -558,8 +584,8 @@ def gen(names, pt_sel, virt_io, dm, vmid, config): pt.gen_pt_head(names, pt_sel, vmid, config) # gen launch header - launch_begin(board_name, uos_type, config) - tap_uos_net(names, vmid, config) + launch_begin(names, virt_io, vmid, config) + tap_uos_net(names, virt_io, vmid, config) # passthrough device pt.gen_pt(names, pt_sel, vmid, config) diff --git a/misc/acrn-config/launch_config/launch_item.py b/misc/acrn-config/launch_config/launch_item.py index 77cdb6f87..aa2fc1d74 100644 --- a/misc/acrn-config/launch_config/launch_item.py +++ b/misc/acrn-config/launch_config/launch_item.py @@ -151,3 +151,4 @@ class VirtioDeviceSelect(): def get_virtio(self): self.dev["input"] = launch_cfg_lib.get_leaf_tag_map(self.launch_info, "virtio_devices", "input") self.dev["block"] = launch_cfg_lib.get_leaf_tag_map(self.launch_info, "virtio_devices", "block") + self.dev["network"] = launch_cfg_lib.get_leaf_tag_map(self.launch_info, "virtio_devices", "network") diff --git a/misc/acrn-config/library/common.py b/misc/acrn-config/library/common.py index 83824b63b..3845415a8 100644 --- a/misc/acrn-config/library/common.py +++ b/misc/acrn-config/library/common.py @@ -23,7 +23,7 @@ GUEST_FLAG = ["0UL", "GUEST_FLAG_SECURE_WORLD_ENABLED", "GUEST_FLAG_LAPIC_PASSTH START_HPA_SIZE_LIST = ['0x20000000', '0x40000000', '0x80000000', 'CONFIG_SOS_RAM_SIZE', 'VM0_MEM_SIZE'] -MULTI_ITEM = ["guest_flag", "pcpu_id", "input", "block"] +MULTI_ITEM = ["guest_flag", "pcpu_id", "input", "block", "network"] class MultiItem(): @@ -34,7 +34,7 @@ class MultiItem(): self.vir_input = [] self.vir_block = [] self.vir_console = [] - self.vir_net = [] + self.vir_network = [] class TmpItem(): @@ -420,6 +420,10 @@ def get_leaf_value(tmp, tag_str, leaf): if leaf.tag == "block" and tag_str == "block": tmp.multi.vir_block.append(leaf.text) + # get virtio-net for vm + if leaf.tag == "network" and tag_str == "network": + tmp.multi.vir_network.append(leaf.text) + def get_sub_value(tmp, tag_str, vm_id): @@ -440,6 +444,10 @@ def get_sub_value(tmp, tag_str, vm_id): if tmp.multi.vir_block and tag_str == "block": tmp.tag[vm_id] = tmp.multi.vir_block + # append virtio network for vm + if tmp.multi.vir_network and tag_str == "network": + tmp.tag[vm_id] = tmp.multi.vir_network + def get_leaf_tag_map(config_file, branch_tag, tag_str): """