From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 76A5A5A005E for ; Thu, 9 Feb 2023 11:54:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675940081; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sef0dOElYew1KB8QG43GQT1qlHrPCCjDiWMFImD7eiA=; b=CHMFiXDPc+CIIhAl1SWfIHMk70PAQBRsk0Ud3oHQDAm4eqWmz6anvyex1Byxurvx3oLiIQ jdc0MDfycss0n3VioT96jaguVUPt++yuN72iSoaup3p6JwcYs+7yJaCr4bkBUOIil3lrGb K+oOwhrWb3hCCV+iArEjjXTZumsINPY= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-619-Hq4E0TcbOl6RXgTLAfMDBw-1; Thu, 09 Feb 2023 05:54:40 -0500 X-MC-Unique: Hq4E0TcbOl6RXgTLAfMDBw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 124483848C26 for ; Thu, 9 Feb 2023 10:54:40 +0000 (UTC) Received: from maya.cloud.tilaa.com (ovpn-208-4.brq.redhat.com [10.40.208.4]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A46902026D4B; Thu, 9 Feb 2023 10:54:39 +0000 (UTC) Date: Thu, 9 Feb 2023 11:54:36 +0100 From: Stefano Brivio To: Michal =?UTF-8?B?UHLDrXZvem7DrWs=?= Subject: Re: [libvirt PATCH] qemu: allow passt to self-daemonize Message-ID: <20230209115436.2e3d8ed1@elisabeth> In-Reply-To: References: <20230208231310.1728051-1-laine@redhat.com> <41d2c66b-cb07-6e4a-4dd9-615ce46d5497@redhat.com> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 56UFBG6OSBGF73P43SBJ3VYPBRLPSDLU X-Message-ID-Hash: 56UFBG6OSBGF73P43SBJ3VYPBRLPSDLU X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: "Daniel P. =?UTF-8?B?QmVycmFuZ8Op?=" , libvir-list@redhat.com, passt-dev@passt.top, Laine Stump X-Mailman-Version: 3.3.3 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Thu, 9 Feb 2023 11:10:21 +0100 Michal Pr=C3=ADvozn=C3=ADk wrote: > On 2/9/23 10:56, Daniel P. Berrang=C3=A9 wrote: > > On Thu, Feb 09, 2023 at 09:52:00AM +0100, Michal Pr=C3=ADvozn=C3=ADk wr= ote: =20 > >> On 2/9/23 00:13, Laine Stump wrote: =20 > >>> I initially had the passt process being started in an identical > >>> fashion to the slirp-helper - libvirt was daemonizing the new process > >>> and recording its pid in a pidfile. The problem with this is that, > >>> since it is daemonized immediately, any startup error in passt happen= s > >>> after the daemonization, and thus isn't seen by libvirt - libvirt > >>> believes that the process has started successfully and continues on > >>> its merry way. The result was that sometimes a guest would be started= , > >>> but there would be no passt process for qemu to use for network > >>> traffic. > >>> > >>> Instead, we should be starting passt in the same manner we start > >>> dnsmasq - we just exec it as normal (along with a request that passt > >>> create the pidfile, which is just another option on the passt > >>> commandline) and wait for the child process to exit; passt then has a > >>> chance to parse its commandline and complete all the setup prior to > >>> daemonizing itself; if it encounters an error and exits with a non-0 > >>> code, libvirt will see the code and know about the failure. We can > >>> then grab the output from stderr, log that so the "user" has some ide= a > >>> of what went wrong, and then fail the guest startup. > >>> > >>> Signed-off-by: Laine Stump > >>> --- > >>> src/qemu/qemu_passt.c | 9 ++++----- > >>> 1 file changed, 4 insertions(+), 5 deletions(-) > >>> > >>> diff --git a/src/qemu/qemu_passt.c b/src/qemu/qemu_passt.c > >>> index 0f09bf3db8..f640a69c00 100644 > >>> --- a/src/qemu/qemu_passt.c > >>> +++ b/src/qemu/qemu_passt.c > >>> @@ -141,24 +141,23 @@ qemuPasstStart(virDomainObj *vm, > >>> g_autofree char *passtSocketName =3D qemuPasstCreateSocketPath(v= m, net); > >>> g_autoptr(virCommand) cmd =3D NULL; > >>> g_autofree char *pidfile =3D qemuPasstCreatePidFilename(vm, net)= ; > >>> + g_autofree char *errbuf =3D NULL; > >>> char macaddr[VIR_MAC_STRING_BUFLEN]; > >>> size_t i; > >>> pid_t pid =3D (pid_t) -1; > >>> int exitstatus =3D 0; > >>> int cmdret =3D 0; > >>> - VIR_AUTOCLOSE errfd =3D -1; > >>> =20 > >>> cmd =3D virCommandNew(PASST); > >>> =20 > >>> virCommandClearCaps(cmd); > >>> - virCommandSetPidFile(cmd, pidfile); > >>> - virCommandSetErrorFD(cmd, &errfd); > >>> - virCommandDaemonize(cmd); > >>> + virCommandSetErrorBuffer(cmd, &errbuf); > >>> =20 > >>> virCommandAddArgList(cmd, > >>> "--one-off", > >>> "--socket", passtSocketName, > >>> "--mac-addr", virMacAddrFormat(&net->mac, m= acaddr), > >>> + "--pid", pidfile, =20 > >> > >> The only problem with this approach is that our virPidFile*() function= s > >> rely on locking the very first byte. And when reading the pidfile, we > >> try to lock the file and if we succeeded it means the file wasn't lock= ed > >> which means the process holding the lock died and thus the pid in the > >> pidfile is stale. > >> > >> Now, I don't see passt locking the pidfile at all. So effectively, aft= er > >> this patch qemuPasstStop() would do nothing (well, okay, it'll remove > >> the pidfile), qemuPasstSetupCgroup() does nothing, etc. > >> > >> What we usually do in this case, is: we let our code write the pidfile > >> (just like the current code does), but then have a loop that waits a b= it > >> for socket to show up. If it doesn't in say 5 seconds we kill the chil= d > >> process (which we know the PID of). You can take inspiration from: > >> qemuDBusStart() or qemuProcessStartManagedPRDaemon(). =20 > >=20 > > Busy waiting for sockets is nasty though. Depending on how passt is > > written it might not be needed. If passt creates the listen() > > socket and does all the important initialization steps that are liable > > to fail, *before* it daemonizes, then we can synchronize without busy > > waiting. It does. In my opinion it could simply be handled like it's done for dnsmasq -- from networkStartDhcpDaemon(): if (virCommandRun(cmd, NULL) < 0) return -1; /* * There really is no race here - when dnsmasq daemonizes, its * leader process stays around until its child has actually * written its pidfile. So by time virCommandRun exits it has * waitpid'd and guaranteed the proess has started and written a * pid */ > > ie waitpid() for passt leader process to exit. Then check if > > the socket exists. If it does, then passt has daemonized and is listeni= ng > > and running, if it does not, then passt failed. =20 >=20 > That still requires passt to hold the pidfile open and locked, neither > of which is happening with the current code. ...is this still a requirement even if qemuPasstStop() just needs to remove the PID file? --=20 Stefano