From owner-svn-src-head@FreeBSD.ORG Thu Apr 2 13:37:57 2015 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 68AD7EC1; Thu, 2 Apr 2015 13:37:57 +0000 (UTC) Received: from mail-wg0-x232.google.com (mail-wg0-x232.google.com [IPv6:2a00:1450:400c:c00::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 1617360A; Thu, 2 Apr 2015 13:37:57 +0000 (UTC) Received: by wgoe14 with SMTP id e14so85487811wgo.0; Thu, 02 Apr 2015 06:37:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=qH7/pF/6bui25o/hxOf2y5hCCSjMz/sq7/PqaX2viBw=; b=LQZ/xqtx7iPVeun1E6zF5ssKVJ0tY0nSk8xJNuyNPkZHiaJ6mURbEJp75LgjYwp3Ps TzpwXL/00myeJrSG6jT0Lm1y2lzX+DtHp525QrAnKjWgXPuXswL9Uc+c2TNFAMUPv5F+ qNJvoLMIIGfxvuMmEslVsfMc1m9IJhjZJ5L/xSSFgBNQQFiw1c/iWpqab1J1PxxMilqO yWa1vi//S4l3tmBPtMdl/datmKxYP/CaM6g0VaSsdhKO5ImW2YTDV0MU0FqSCJW+Kn9L y3pFVKYPVmDQyhSdyfeqRxgYUTUzg08sMeWAnaKcHKcG19Cp/Jpqmr/3nGPaurzpe/Jj 2wyg== X-Received: by 10.180.107.5 with SMTP id gy5mr25226382wib.0.1427981875525; Thu, 02 Apr 2015 06:37:55 -0700 (PDT) Received: from dft-labs.eu (n1x0n-1-pt.tunnel.tserv5.lon1.ipv6.he.net. [2001:470:1f08:1f7::2]) by mx.google.com with ESMTPSA id n6sm7316991wjy.8.2015.04.02.06.37.53 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 02 Apr 2015 06:37:54 -0700 (PDT) Date: Thu, 2 Apr 2015 15:37:51 +0200 From: Mateusz Guzik To: Gleb Smirnoff Subject: Re: svn commit: r280971 - in head: contrib/ipfilter/tools share/man/man4 sys/contrib/ipfilter/netinet sys/netinet sys/netipsec sys/netpfil/pf Message-ID: <20150402133751.GA549@dft-labs.eu> References: <201504012226.t31MQedN044443@svn.freebsd.org> <1427929676.82583.103.camel@freebsd.org> <20150402123522.GC64665@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20150402123522.GC64665@FreeBSD.org> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org, Ian Lepore X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Apr 2015 13:37:57 -0000 On Thu, Apr 02, 2015 at 03:35:22PM +0300, Gleb Smirnoff wrote: > On Wed, Apr 01, 2015 at 05:07:56PM -0600, Ian Lepore wrote: > I> > Author: glebius > I> > Date: Wed Apr 1 22:26:39 2015 > I> > New Revision: 280971 > I> > URL: https://svnweb.freebsd.org/changeset/base/280971 > I> > > I> > Log: > I> > o Use new function ip_fillid() in all places throughout the kernel, > I> > where we want to create a new IP datagram. > I> > o Add support for RFC6864, which allows to set IP ID for atomic IP > I> > datagrams to any value, to improve performance. The behaviour is > I> > controlled by net.inet.ip.rfc6864 sysctl knob, which is enabled by > I> > default. > I> > o In case if we generate IP ID, use counter(9) to improve performance. > I> > o Gather all code related to IP ID into ip_id.c. > I> > > I> > Differential Revision: https://reviews.freebsd.org/D2177 > I> > Reviewed by: adrian, cy, rpaulo > I> > Tested by: Emeric POUPON > I> > Sponsored by: Netflix > I> > Sponsored by: Nginx, Inc. > I> > Relnotes: yes > I> > > I> [...] > I> > +void > I> > +ip_fillid(struct ip *ip) > I> > +{ > I> > + > I> > + /* > I> > + * Per RFC6864 Section 4 > I> > + * > I> > + * o Atomic datagrams: (DF==1) && (MF==0) && (frag_offset==0) > I> > + * o Non-atomic datagrams: (DF==0) || (MF==1) || (frag_offset>0) > I> > + */ > I> > + if (V_ip_rfc6864 && (ip->ip_off & htons(IP_DF)) == htons(IP_DF)) > I> > + ip->ip_id = 0; > I> > + else if (V_ip_do_randomid) > I> > + ip->ip_id = ip_randomid(); > I> > + else { > I> > + counter_u64_add(V_ip_id, 1); > I> > + ip->ip_id = htons((*(uint64_t *)zpcpu_get(V_ip_id)) & 0xffff); > I> > + } > I> > +} > I> > + > I> > I> This is completely bogus. It's a big opacity violation (it relies on > I> what should be opaque private internal implementation details of > I> counter(9)). The fact that the counter api doesn't provide a function > I> for retrieving one cpu's counter value should be a big clue there -- the > I> fact that you know the internals doesn't make it okay to reach behind > I> the counter and grab a value like that. It may not even be safe to do > I> so on any given architecture; it certainly isn't safe on arm, and that > I> line of code above will work only by accident because you're throwing > I> way all but 16 bits. > > I though about providing that API, but since it isn't safe in general, > I decided to not do that. > > I> But even more importantly, this WILL result in multiple threads using > I> the same value at the same time... > I> > I> - Thread A on CPU 1 and thread B on CPU 2 both begin executing here at > I> the same time, and both get through counter_u64_add(). > I> - Thread A keeps running and uses CPU 1's new value, call it 27. > I> - Thread B gets prempted between counter_u64_add() and zpcpu_get(). > I> When it resumes it's now on CPU 1, so it retrieves value 27 as well. > > This was already discussed in this thread: > > https://lists.freebsd.org/pipermail/svn-src-head/2015-March/069864.html > For this particular use-case you never care what CPU you are executing on, you only want to obtain a unique number. per-cpu counters can serve this purpose no problem, just provide an operation which guarantees to return the new value of the counter it incremented. Should be easily achieved with e.g. just pinning curthread to the cpu it executes on for the duration of inc + fetch. -- Mateusz Guzik