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 92C315A026F
	for <passt-dev@passt.top>; Thu,  3 Aug 2023 07:40:49 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
	s=mimecast20190719; t=1691041248;
	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=CDxAhqMKoiXH5J6EiH30mM758pUF5d+0dlbkUkykmsM=;
	b=OjxL5jSCLhxHHOFwuwOehVvNE8ihQtGyckYMDNlvG2kq8s1UOAkYYWVdhU1MbZ85Ooev8H
	my3wWamVD3l96Ja46xLQdZW6pfEU5xBNgT9QPzxtmJVgM2J+tAqcB0tvOE04h5HqKlXB73
	pR8oJxlhdxSU/JNskQ2etf9VSumrWe0=
Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com
 [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 us-mta-173-4_mKml7zMm-X1u2eoxQz_w-1; Thu, 03 Aug 2023 01:40:46 -0400
X-MC-Unique: 4_mKml7zMm-X1u2eoxQz_w-1
Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3])
	(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 67445800159;
	Thu,  3 Aug 2023 05:40:46 +0000 (UTC)
Received: from elisabeth (unknown [10.39.208.24])
	by smtp.corp.redhat.com (Postfix) with ESMTPS id C06991121325;
	Thu,  3 Aug 2023 05:40:45 +0000 (UTC)
Date: Thu, 3 Aug 2023 07:40:40 +0200
From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Subject: Re: [PATCH 01/17] netlink: Split up functionality if nl_link()
Message-ID: <20230803074040.67511759@elisabeth>
In-Reply-To: <ZMstKFhcJu2+S1QS@zatzit>
References: <20230724060936.952659-1-david@gibson.dropbear.id.au>
	<20230724060936.952659-2-david@gibson.dropbear.id.au>
	<20230803004729.03ca0e36@elisabeth>
	<ZMsMTByU6J7ce/xx@zatzit>
	<ZMstKFhcJu2+S1QS@zatzit>
Organization: Red Hat
MIME-Version: 1.0
X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Message-ID-Hash: RKHTR6UTL6SSNZHOD45UNJWCKNS7QLI2
X-Message-ID-Hash: RKHTR6UTL6SSNZHOD45UNJWCKNS7QLI2
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: passt-dev@passt.top
X-Mailman-Version: 3.3.8
Precedence: list
List-Id: Development discussion and patches for passt <passt-dev.passt.top>
Archived-At: <https://archives.passt.top/passt-dev/20230803074040.67511759@elisabeth/>
Archived-At: <https://passt.top/hyperkitty/list/passt-dev@passt.top/message/RKHTR6UTL6SSNZHOD45UNJWCKNS7QLI2/>
List-Archive: <https://archives.passt.top/passt-dev/>
List-Archive: <https://passt.top/hyperkitty/list/passt-dev@passt.top/>
List-Help: <mailto:passt-dev-request@passt.top?subject=help>
List-Owner: <mailto:passt-dev-owner@passt.top>
List-Post: <mailto:passt-dev@passt.top>
List-Subscribe: <mailto:passt-dev-join@passt.top>
List-Unsubscribe: <mailto:passt-dev-leave@passt.top>

On Thu, 3 Aug 2023 14:29:28 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> On Thu, Aug 03, 2023 at 12:09:16PM +1000, David Gibson wrote:
> > On Thu, Aug 03, 2023 at 12:47:29AM +0200, Stefano Brivio wrote:  
> [snip]
> > > > -void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu)
> > > > +void nl_link_get_mac(int ns, unsigned int ifi, void *mac)
> > > >  {
> > > > -	int change = !MAC_IS_ZERO(mac) || up || mtu;
> > > >  	struct req_t {
> > > >  		struct nlmsghdr nlh;
> > > >  		struct ifinfomsg ifm;
> > > > -		struct rtattr rta;
> > > > -		union {
> > > > -			unsigned char mac[ETH_ALEN];
> > > > -			struct {
> > > > -				unsigned int mtu;
> > > > -			} mtu;
> > > > -		} set;
> > > >  	} req = {
> > > > -		.nlh.nlmsg_type   = change ? RTM_NEWLINK : RTM_GETLINK,
> > > > -		.nlh.nlmsg_len    = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
> > > > -		.nlh.nlmsg_flags  = NLM_F_REQUEST | (change ? NLM_F_ACK : 0),
> > > > +		.nlh.nlmsg_type	  = RTM_GETLINK,
> > > > +		.nlh.nlmsg_len	  = sizeof(req),  
> > > 
> > > I don't think there's a practical issue with this, but there were two
> > > reasons why I used NLMSG_LENGTH(sizeof(struct ifinfomsg)) instead:
> > > 
> > > - NLMSG_LENGTH() aligns to 4 bytes, not to whatever
> > >   architecture-dependent alignment we might have: the message might
> > >   actually be smaller  
> > 
> > Oof... so.  On the one hand, I see the issue; if these are different,
> > I'm not sure what the effect will be.  On the other hand, if we use
> > NLMSG_LENGTH and it *is* longer than the structure size, we'll be
> > saying that this message is longer than the datagram containing it.
> > I'm not sure what the effect of that will be either.  
> 
> Duh, sorry, I realized I had this backwards.  NLSMSG_LENGTH() is the
> non-aligned length, sizeof() may include alignment.  I'll rework based
> on that understanding.

Right, I was about to write you that... or rather, NLMSG_LENGTH() is
the (presumably) lesser-aligned length. Also, if you check pretty much
any example in iproute2, nlmsg_len is always set like that, using
NLMSG_LENGTH() on the payload.

> > Not really sure what to do about this.
> >   
> > > - I see that this works with gcc and clang, but, strictly
> > >   speaking, is the size of the struct known "before"
> > >   (sequence-point-wise) we're done initialising it? I have a very vague
> > >   memory of this not working with gcc 2.9 or suchlike -- which is not a
> > >   problem, as long as our new friend C11 actually supports this (but
> > >   I'm not entirely sure).  
> > 
> > I'm pretty sure it's ok, regardless of C11 state.  It's not really a
> > question of sequence points: those are about the ordering of run time
> > operations.  Even though the structure is being defined inline,
> > determining it's size and layout will still happen at compile time,
> > whereas the initialization is obviously a runtime event.

Ah, sorry, yes, of course. Still I remember that failing spectacularly
in a distant past. But you just checked with gcc 4-ish I guess, so I
guess it would have been fine anyway.

-- 
Stefano