From: sudhir kumar Subject: Re: [PATCH][KVM-AUTOTEST] TAP network support in kvm-autotest Date: Tue, 2 Jun 2009 12:36:20 +0530 Message-ID: References: <893076616.211371242296910759.JavaMail.root@zmail05.collab.prod.int.phx2.redhat.com> <1756214842.211491242297658011.JavaMail.root@zmail05.collab.prod.int.phx2.redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Jason Wang , ulublin@redhat.com, kvm@vger.kernel.org To: Michael Goldish Return-path: Received: from yw-out-2324.google.com ([74.125.46.31]:61261 "EHLO yw-out-2324.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759222AbZFBHGT convert rfc822-to-8bit (ORCPT ); Tue, 2 Jun 2009 03:06:19 -0400 Received: by yw-out-2324.google.com with SMTP id 5so4583241ywb.1 for ; Tue, 02 Jun 2009 00:06:20 -0700 (PDT) In-Reply-To: <1756214842.211491242297658011.JavaMail.root@zmail05.collab.prod.int.phx2.redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On Thu, May 14, 2009 at 4:10 PM, Michael Goldish = wrote: > Hi Jason, > > We already have patches that implement similar functionality here in > TLV, as mentioned in the to-do list (item #4 under 'Framework'). > They're not yet committed upstream because they're still quite fresh. > > Still, your patch looks good and is quite similar to mine. The main > difference is that I use MAC/IP address pools specified by the user, > instead of random MACs with arp/nmap to detect the matching IP > addresses. > > I will post my patch to the mailing list soon, but it will come > together with quite a few other patches that I haven't posted yet, so > please be patient. > > Comments/questions: > > Why do you use nmap in addition to arp? In what cases will arp not > suffice? I'm a little put off by the fact that nmap imposes an > additional requirement on the host. Three hosts I've tried don't come > with nmap installed by default. Using nmap is very slow. It puts lot of delay in the complete test exec= ution. Michael, How do your patches implement ip search of the guest? system arp might not be sufficient as it can have only certain fix number of entries and might not have entry for the newly assigned guest ip. I would like to have a quick look on your patches. thanks in advance. > > Please see additional comments below. > > ----- "Jason Wang" wrote: > >> Hi All: >> This patch tries to add tap network support in kvm-autotest. Multipl= e >> nics connected to different bridges could be achieved through this >> script. Public bridge is important for testing real network traffic >> and migration. The patch gives each nic with randomly generated mac >> address. The ip address required in the test could be dynamically >> probed through nmap/arp. Only the ip address of first NIC is used >> through the test. >> >> Example: >> nics =3D nic1 nic2 >> network =3D bridge >> bridge =3D switch >> ifup =3D/etc/qemu-ifup-switch >> ifdown =3D/etc/qemu-ifdown-switch >> >> This would make the virtual machine have two nics both of which are >> connected to a bridge with the name of 'switch'. Ifup/ifdown scripts >> are also specified. >> >> Another Example: >> nics =3D nic1 nic2 >> network =3D bridge >> bridge =3D switch >> bridge_nic2 =3D virbr0 >> ifup =3D/etc/qemu-ifup-switch >> ifup_nic2 =3D /etc/qemu-ifup-virbr0 >> >> This would makes the virtual machine have two nics: nic1 are connect= ed >> to bridge 'switch' and nci2 are connected to bridge 'virbr0'. >> >> Public mode and user mode nic could also be mixed: >> nics =3D nic1 nic2 >> network =3D bridge >> network_nic2 =3D user >> >> Looking forward for comments and suggestions. >> >> From: jason >> Date: Wed, 13 May 2009 16:15:28 +0800 >> Subject: [PATCH] Add tap networking support. >> >> --- >> =A0client/tests/kvm_runtest_2/kvm_utils.py | =A0 =A07 +++ >> =A0client/tests/kvm_runtest_2/kvm_vm.py =A0 =A0| =A0 74 >> ++++++++++++++++++++++++++----- >> =A02 files changed, 69 insertions(+), 12 deletions(-) >> >> diff --git a/client/tests/kvm_runtest_2/kvm_utils.py >> b/client/tests/kvm_runtest_2/kvm_utils.py >> index be8ad95..0d1f7f8 100644 >> --- a/client/tests/kvm_runtest_2/kvm_utils.py >> +++ b/client/tests/kvm_runtest_2/kvm_utils.py >> @@ -773,3 +773,10 @@ def md5sum_file(filename, size=3DNone): >> =A0 =A0 =A0 =A0 =A0size -=3D len(data) >> =A0 =A0 =A0f.close() >> =A0 =A0 =A0return o.hexdigest() >> + >> +def random_mac(): >> + =A0 =A0mac=3D[0x00,0x16,0x30, >> + =A0 =A0 =A0 =A0 random.randint(0x00,0x09), >> + =A0 =A0 =A0 =A0 random.randint(0x00,0x09), >> + =A0 =A0 =A0 =A0 random.randint(0x00,0x09)] >> + =A0 =A0return ':'.join(map(lambda x: "%02x" %x,mac)) > > Random MAC addresses will not necessarily work everywhere, as far as > I know. That's why I prefer user specified MAC/IP address ranges. > >> diff --git a/client/tests/kvm_runtest_2/kvm_vm.py >> b/client/tests/kvm_runtest_2/kvm_vm.py >> index fab839f..ea7dab6 100644 >> --- a/client/tests/kvm_runtest_2/kvm_vm.py >> +++ b/client/tests/kvm_runtest_2/kvm_vm.py >> @@ -105,6 +105,10 @@ class VM: >> =A0 =A0 =A0 =A0 =A0self.qemu_path =3D qemu_path >> =A0 =A0 =A0 =A0 =A0self.image_dir =3D image_dir >> =A0 =A0 =A0 =A0 =A0self.iso_dir =3D iso_dir >> + =A0 =A0 =A0 =A0self.macaddr =3D [] >> + =A0 =A0 =A0 =A0for nic_name in kvm_utils.get_sub_dict_names(params= ,"nics"): >> + =A0 =A0 =A0 =A0 =A0 =A0macaddr =3D kvm_utils.random_mac() >> + =A0 =A0 =A0 =A0 =A0 =A0self.macaddr.append(macaddr) >> >> =A0 =A0 =A0def verify_process_identity(self): >> =A0 =A0 =A0 =A0 =A0"""Make sure .pid really points to the original q= emu >> process. >> @@ -189,9 +193,25 @@ class VM: >> =A0 =A0 =A0 =A0 =A0for nic_name in kvm_utils.get_sub_dict_names(para= ms, >> "nics"): >> =A0 =A0 =A0 =A0 =A0 =A0 =A0nic_params =3D kvm_utils.get_sub_dict(par= ams, nic_name) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D " -net nic,vlan=3D%d" % vla= n >> + =A0 =A0 =A0 =A0 =A0 =A0net =3D nic_params.get("network") >> + =A0 =A0 =A0 =A0 =A0 =A0if net =3D=3D "bridge": >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D ",macaddr=3D%s" % sel= f.macaddr[vlan] >> =A0 =A0 =A0 =A0 =A0 =A0 =A0if nic_params.get("nic_model"): >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D ",model=3D%s" % nic= _params.get("nic_model") >> - =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D " -net user,vlan=3D%d" % vlan >> + =A0 =A0 =A0 =A0 =A0 =A0if net =3D=3D "bridge": >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D " -net tap,vlan=3D%d"= % vlan >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ifup =3D nic_params.get("ifup") >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if ifup: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D ",script=3D%s= " % ifup >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0else: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D ",script=3D/e= tc/qemu-ifup" > > Why not just leave 'script' out if the user doesn't specify 'ifup'? > There's no good reason to prefer /etc/qemu-ifup to /etc/kvm-ifup or > anything else, so I think it's best to leave it up to qemu if the > user has no preference. It's also slightly shorter. > >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ifdown =3D nic_params.get("ifdown") >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if ifdown: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D ",downscript=3D= %s" % ifdown >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0else: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D ",downscript=3D= no" > > The same applies here. > > This is just my opinion; I'd like to hear your thoughts on this too. > >> + =A0 =A0 =A0 =A0 =A0 =A0else: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D " -net user,vlan=3D%d= " % vlan >> =A0 =A0 =A0 =A0 =A0 =A0 =A0vlan +=3D 1 >> >> =A0 =A0 =A0 =A0 =A0mem =3D params.get("mem") >> @@ -206,11 +226,11 @@ class VM: >> =A0 =A0 =A0 =A0 =A0extra_params =3D params.get("extra_params") >> =A0 =A0 =A0 =A0 =A0if extra_params: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D " %s" % extra_params >> - >> + >> =A0 =A0 =A0 =A0 =A0for redir_name in kvm_utils.get_sub_dict_names(pa= rams, >> "redirs"): >> =A0 =A0 =A0 =A0 =A0 =A0 =A0redir_params =3D kvm_utils.get_sub_dict(p= arams, >> redir_name) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0guest_port =3D int(redir_params.get("gues= t_port")) >> - =A0 =A0 =A0 =A0 =A0 =A0host_port =3D self.get_port(guest_port) >> + =A0 =A0 =A0 =A0 =A0 =A0host_port =3D self.get_port(guest_port,True= ) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0qemu_cmd +=3D " -redir tcp:%s::%s" % (hos= t_port, >> guest_port) >> >> =A0 =A0 =A0 =A0 =A0if params.get("display") =3D=3D "vnc": >> @@ -467,27 +487,57 @@ class VM: >> =A0 =A0 =A0 =A0 =A0If port redirection is used, return 'localhost' (= the guest >> has no IP >> =A0 =A0 =A0 =A0 =A0address of its own). =A0Otherwise return the gues= t's IP >> address. >> =A0 =A0 =A0 =A0 =A0""" >> - =A0 =A0 =A0 =A0# Currently redirection is always used, so return >> 'localhost' >> - =A0 =A0 =A0 =A0return "localhost" >> + =A0 =A0 =A0 =A0if self.params.get("network") =3D=3D "bridge": >> + =A0 =A0 =A0 =A0 =A0 =A0# probing ip address through arp >> + =A0 =A0 =A0 =A0 =A0 =A0bridge_name =3D self.params['bridge'] >> + =A0 =A0 =A0 =A0 =A0 =A0macaddr =3D self.macaddr[0] > > I think VM.get_address() should take an index parameter, instead of > just return the first address. The index parameter can default to 0. > >> + =A0 =A0 =A0 =A0 =A0 =A0lines =3D os.popen("arp -a").readlines() >> + =A0 =A0 =A0 =A0 =A0 =A0for line in lines: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if macaddr in line: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return line.split()[1].stri= p('()') >> + >> + =A0 =A0 =A0 =A0 =A0 =A0# probing ip address through nmap >> + =A0 =A0 =A0 =A0 =A0 =A0lines =3D os.popen("ip route").readlines() >> + =A0 =A0 =A0 =A0 =A0 =A0birdge_network =3D None >> + =A0 =A0 =A0 =A0 =A0 =A0for line in lines: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if bridge_name in line: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0bridge_network =3D line.spl= it()[0] >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0break >> + >> + =A0 =A0 =A0 =A0 =A0 =A0if bridge_network !=3D None: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0lines =3D os.popen("nmap -sP -n %s"= % >> bridge_network).readlines() >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0lastline =3D None >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0for line in lines: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if macaddr in line: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return lastline.spl= it()[1] >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0lastline =3D line >> + >> + =A0 =A0 =A0 =A0 =A0 =A0# could not found ip address >> + =A0 =A0 =A0 =A0 =A0 =A0return None >> + =A0 =A0 =A0 =A0else: >> + =A0 =A0 =A0 =A0 =A0 =A0return "localhost" >> >> - =A0 =A0def get_port(self, port): >> + =A0 =A0def get_port(self, port, query =3D False): >> =A0 =A0 =A0 =A0 =A0"""Return the port in host space corresponding to= port in >> guest space. >> >> =A0 =A0 =A0 =A0 =A0If port redirection is used, return the host port= redirected >> to guest port port. >> =A0 =A0 =A0 =A0 =A0Otherwise return port. >> =A0 =A0 =A0 =A0 =A0""" >> - =A0 =A0 =A0 =A0# Currently redirection is always used, so use the = redirs >> dict >> - =A0 =A0 =A0 =A0if self.redirs.has_key(port): >> - =A0 =A0 =A0 =A0 =A0 =A0return self.redirs[port] >> + >> + =A0 =A0 =A0 =A0if query =3D=3D True or self.params.get("network") = !=3D "bridge": > > Why do we need a 'query' parameter here? It looks to me like > self.params.get("network") !=3D "bridge" > should suffice. > >> + =A0 =A0 =A0 =A0 =A0 =A0if self.redirs.has_key(port): >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return self.redirs[port] >> + =A0 =A0 =A0 =A0 =A0 =A0else: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0kvm_log.debug("Warning: guest port = %s requested but >> not redirected" % port) >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return None >> =A0 =A0 =A0 =A0 =A0else: >> - =A0 =A0 =A0 =A0 =A0 =A0kvm_log.debug("Warning: guest port %s reque= sted but not >> redirected" % port) >> - =A0 =A0 =A0 =A0 =A0 =A0return None >> + =A0 =A0 =A0 =A0 =A0 =A0return port >> >> =A0 =A0 =A0def is_sshd_running(self, timeout=3D10): >> =A0 =A0 =A0 =A0 =A0"""Return True iff the guest's SSH port is respon= sive.""" >> =A0 =A0 =A0 =A0 =A0address =3D self.get_address() >> =A0 =A0 =A0 =A0 =A0port =3D self.get_port(int(self.params.get("ssh_p= ort"))) >> - =A0 =A0 =A0 =A0if not port: >> + =A0 =A0 =A0 =A0if not port or not address: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0return False >> =A0 =A0 =A0 =A0 =A0return kvm_utils.is_sshd_running(address, port, >> timeout=3Dtimeout) > > Again, I will do my best to post my patches soon. > > Thanks, > Michael > -- > To unsubscribe from this list: send the line "unsubscribe kvm" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at =A0http://vger.kernel.org/majordomo-info.html > --=20 Sudhir Kumar