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.133.124]) by mail.toke.dk (Postfix) with ESMTPS id CF8F3A13B66 for ; Thu, 29 Jun 2023 13:30:58 +0200 (CEST) Authentication-Results: mail.toke.dk; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hi9l6FlT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688038256; 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=3NLpoJaCUlpc7t+o+wo/q9dNr31hOpM8qfprBGwoGOw=; b=hi9l6FlT/nJF96smlhHxSgqR5JWs6mW3EjLuCfeFTH/HM1bL5QQF+accBfhjuGGPUQNGxf /zp+nuxAiZdfVjX3jgT0WL/5dbi0y41911rdqjA7/n+IiBb/cpnAsTOxqZqmhEcXdf77jz bilveBIL+YiYuLwwK71ZHwvmbz7lcTk= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-76-ckqgXbGfO2ipBimzf7BjoA-1; Thu, 29 Jun 2023 07:30:55 -0400 X-MC-Unique: ckqgXbGfO2ipBimzf7BjoA-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-313f5021d9bso281238f8f.2 for ; Thu, 29 Jun 2023 04:30:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688038254; x=1690630254; h=content-transfer-encoding:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OrE5tHeZ/IwPEmEhShXe1stXd0nIK9ivrjyEuA87r4Y=; b=k8kXHWQi9C3UZp7kesPn6EUI2k4d9W0z0DaH8jH+SiFKlICdGpwnQ2RzgbbqCjl9HT zk7uP1JHVAD2oWcumoe+xCkaui2+2rJcONFoR44DaPS8dK9j6Qs/0F29dRWPMORU+H/Y ZLQ8ssaa+2+YHMD2lkIjc4pSGL9G2CxFNULPK6QWxoWL36HFtoLfg8etT6qAOGtnSY9o svGncgje1NnVrjZbFoqXg9l1wSmGqCGsfhnAViw809WISaaOChLNsotGs6MlOJqdNSPd 0MQFtkhpo/HvjKSoJ6G1OBdVS650jtF4a4bUCRfCTj5Dfy7YEGTVnu964EpXASGg399Q tVzQ== X-Gm-Message-State: ABy/qLbCdOTtxEWQj0vxz/qHRpeSSAQZCWDe/nICII9sZD2sZDs2Otsv PxltDq6yXuTJIxXoDMBdA4HoGu0MfkSpQzx0w22X4SlXmKR+/eKKdwySUAxdRLp+YpJA2vFyFrf s0/7N6pvuatTxA5PPy4dj X-Received: by 2002:a5d:6446:0:b0:314:1494:fe28 with SMTP id d6-20020a5d6446000000b003141494fe28mr1623805wrw.53.1688038253849; Thu, 29 Jun 2023 04:30:53 -0700 (PDT) X-Google-Smtp-Source: APBJJlFSTSdadA1qRPqBePYN2tzRKCLjDLo2mbmC5ERRoo56Vwlr/UTdkcDIWM579dYLFupgrB0vHw== X-Received: by 2002:a5d:6446:0:b0:314:1494:fe28 with SMTP id d6-20020a5d6446000000b003141494fe28mr1623772wrw.53.1688038253149; Thu, 29 Jun 2023 04:30:53 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id u18-20020a5d6ad2000000b00313e90d1d0dsm13783319wrw.112.2023.06.29.04.30.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jun 2023 04:30:52 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id CC683BC043B; Thu, 29 Jun 2023 13:30:51 +0200 (CEST) From: Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= To: Stanislav Fomichev , Magnus Karlsson In-Reply-To: References: <20230621170244.1283336-1-sdf@google.com> <20230621170244.1283336-4-sdf@google.com> <57b9fc14-c02e-f0e5-148d-a549ebab6cf6@brouer.com> <435d1630-c3f4-97fb-b6fe-9795d1f0bf33@redhat.com> X-Clacks-Overhead: GNU Terry Pratchett Date: Thu, 29 Jun 2023 13:30:51 +0200 Message-ID: <871qhuh5ec.fsf@toke.dk> MIME-Version: 1.0 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: 7EVBBMRPUL3LZ73NMRG2C7UFFZGUI4RP X-Message-ID-Hash: 7EVBBMRPUL3LZ73NMRG2C7UFFZGUI4RP X-MailFrom: toke@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: Jesper Dangaard Brouer , brouer@redhat.com, bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, haoluo@google.com, jolsa@kernel.org, =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= , "Karlsson, Magnus" , "xdp-hints@xdp-project.net" X-Mailman-Version: 3.3.8 Precedence: list Subject: [xdp-hints] Re: [RFC bpf-next v2 03/11] xsk: Support XDP_TX_METADATA_LEN List-Id: XDP hardware hints design discussion Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Stanislav Fomichev writes: > On Wed, Jun 28, 2023 at 1:09=E2=80=AFAM Magnus Karlsson > wrote: >> >> On Mon, 26 Jun 2023 at 19:06, Stanislav Fomichev wrote: >> > >> > On Sat, Jun 24, 2023 at 2:02=E2=80=AFAM Jesper Dangaard Brouer >> > wrote: >> > > >> > > >> > > >> > > On 23/06/2023 19.41, Stanislav Fomichev wrote: >> > > > On Fri, Jun 23, 2023 at 3:24=E2=80=AFAM Jesper Dangaard Brouer >> > > > wrote: >> > > >> >> > > >> >> > > >> >> > > >> On 22/06/2023 19.55, Stanislav Fomichev wrote: >> > > >>> On Thu, Jun 22, 2023 at 2:11=E2=80=AFAM Jesper D. Brouer wrote: >> > > >>>> >> > > >>>> >> > > >>>> This needs to be reviewed by AF_XDP maintainers Magnus and Bj= =C3=B8rn (Cc) >> > > >>>> >> > > >>>> On 21/06/2023 19.02, Stanislav Fomichev wrote: >> > > >>>>> For zerocopy mode, tx_desc->addr can point to the arbitrary of= fset >> > > >>>>> and carry some TX metadata in the headroom. For copy mode, the= re >> > > >>>>> is no way currently to populate skb metadata. >> > > >>>>> >> > > >>>>> Introduce new XDP_TX_METADATA_LEN that indicates how many byte= s >> > > >>>>> to treat as metadata. Metadata bytes come prior to tx_desc add= ress >> > > >>>>> (same as in RX case). >> > > >>>> >> > > >>>> From looking at the code, this introduces a socket option fo= r this TX >> > > >>>> metadata length (tx_metadata_len). >> > > >>>> This implies the same fixed TX metadata size is used for all pa= ckets. >> > > >>>> Maybe describe this in patch desc. >> > > >>> >> > > >>> I was planning to do a proper documentation page once we settle = on all >> > > >>> the details (similar to the one we have for rx). >> > > >>> >> > > >>>> What is the plan for dealing with cases that doesn't populate s= ame/full >> > > >>>> TX metadata size ? >> > > >>> >> > > >>> Do we need to support that? I was assuming that the TX layout wo= uld be >> > > >>> fixed between the userspace and BPF. >> > > >> >> > > >> I hope you don't mean fixed layout, as the whole point is adding >> > > >> flexibility and extensibility. >> > > > >> > > > I do mean a fixed layout between the userspace (af_xdp) and devtx = program. >> > > > At least fixed max size of the metadata. The userspace and the bpf >> > > > prog can then use this fixed space to implement some flexibility >> > > > (btf_ids, versioned structs, bitmasks, tlv, etc). >> > > > If we were to make the metalen vary per packet, we'd have to signa= l >> > > > its size per packet. Probably not worth it? >> > > >> > > Existing XDP metadata implementation also expand in a fixed/limited >> > > sized memory area, but communicate size per packet in this area (als= o >> > > for validation purposes). BUT for AF_XDP we don't have room for ano= ther >> > > pointer or size in the AF_XDP descriptor (see struct xdp_desc). >> > > >> > > >> > > > >> > > >>> If every packet would have a different metadata length, it seems= like >> > > >>> a nightmare to parse? >> > > >>> >> > > >> >> > > >> No parsing is really needed. We can simply use BTF IDs and type = cast in >> > > >> BPF-prog. Both BPF-prog and userspace have access to the local BT= F ids, >> > > >> see [1] and [2]. >> > > >> >> > > >> It seems we are talking slightly past each-other(?). Let me reph= rase >> > > >> and reframe the question, what is your *plan* for dealing with di= fferent >> > > >> *types* of TX metadata. The different struct *types* will of-cau= se have >> > > >> different sizes, but that is okay as long as they fit into the ma= ximum >> > > >> size set by this new socket option XDP_TX_METADATA_LEN. >> > > >> Thus, in principle I'm fine with XSK having configured a fixed he= adroom >> > > >> for metadata, but we need a plan for handling more than one type = and >> > > >> perhaps a xsk desc indicator/flag for knowing TX metadata isn't r= andom >> > > >> data ("leftover" since last time this mem was used). >> > > > >> > > > Yeah, I think the above correctly catches my expectation here. Som= e >> > > > headroom is reserved via XDP_TX_METADATA_LEN and the flexibility i= s >> > > > offloaded to the bpf program via btf_id/tlv/etc. >> > > > >> > > > Regarding leftover metadata: can we assume the userspace will take >> > > > care of setting it up? >> > > > >> > > >> With this kfunc approach, then things in-principle, becomes a con= tract >> > > >> between the "local" TX-hook BPF-prog and AF_XDP userspace. Thes= e two >> > > >> components can as illustrated here [1]+[2] can coordinate based o= n local >> > > >> BPF-prog BTF IDs. This approach works as-is today, but patchset >> > > >> selftests examples don't use this and instead have a very static >> > > >> approach (that people will copy-paste). >> > > >> >> > > >> An unsolved problem with TX-hook is that it can also get packets = from >> > > >> XDP_REDIRECT and even normal SKBs gets processed (right?). How d= oes the >> > > >> BPF-prog know if metadata is valid and intended to be used for e.= g. >> > > >> requesting the timestamp? (imagine metadata size happen to match) >> > > > >> > > > My assumption was the bpf program can do ifindex/netns filtering. = Plus >> > > > maybe check that the meta_len is the one that's expected. >> > > > Will that be enough to handle XDP_REDIRECT? >> > > >> > > I don't think so, using the meta_len (+ ifindex/netns) to communicat= e >> > > activation of TX hardware hints is too weak and not enough. This is= an >> > > implicit API for BPF-programmers to understand and can lead to impli= cit >> > > activation. >> > > >> > > Think about what will happen for your AF_XDP send use-case. For >> > > performance reasons AF_XDP don't zero out frame memory. Thus, meta_= len >> > > is fixed even if not used (and can contain garbage), it can by accid= ent >> > > create hard-to-debug situations. As discussed with Magnus+Maryam >> > > before, we found it was practical (and faster than mem zero) to exte= nd >> > > AF_XDP descriptor (see struct xdp_desc) with some flags to >> > > indicate/communicate this frame comes with TX metadata hints. >> > >> > What is that "if not used" situation? Can the metadata itself have >> > is_used bit? The userspace has to initialize at least that bit. >> > We can definitely add that extra "has_metadata" bit to the descriptor, >> > but I'm trying to understand whether we can do without it. >> >> To me, this "has_metadata" bit in the descriptor is just an >> optimization. If it is 0, then there is no need to go and check the >> metadata field and you save some performance. Regardless of this bit, >> you need some way to say "is_used" for each metadata entry (at least >> when the number of metadata entries is >1). Three options come to mind >> each with their pros and cons. >> >> #1: Let each metadata entry have an invalid state. Not possible for >> every metadata and requires the user/kernel to go scan through every >> entry for every packet. >> >> #2: Have a field of bits at the start of the metadata section (closest >> to packet data) that signifies if a metadata entry is valid or not. If >> there are N metadata entries in the metadata area, then N bits in this >> field would be used to signify if the corresponding metadata is used >> or not. Only requires the user/kernel to scan the valid entries plus >> one access for the "is_used" bits. >> >> #3: Have N bits in the AF_XDP descriptor options field instead of the >> N bits in the metadata area of #2. Faster but would consume many >> precious bits in the fixed descriptor and cap the number of metadata >> entries possible at around 8. E.g., 8 for Rx, 8 for Tx, 1 for the >> multi-buffer work, and 15 for some future use. Depends on how daring >> we are. >> >> The "has_metadata" bit suggestion can be combined with 1 or 2. >> Approach 3 is just a fine grained extension of the idea itself. >> >> IMO, the best approach unfortunately depends on the metadata itself. >> If it is rarely valid, you want something like the "has_metadata" bit. >> If it is nearly always valid and used, approach #1 (if possible for >> the metadata) should be the fastest. The decision also depends on the >> number of metadata entries you have per packet. Sorry that I do not >> have a good answer. My feeling is that we need something like #1 or >> #2, or maybe both, then if needed we can add the "has_metadata" bit or >> bits (#3) optimization. Can we do this encoding and choice (#1, #2, or >> a combo) in the eBPF program itself? Would provide us with the >> flexibility, if possible. > > Here is my take on it, lmk if I'm missing something: > > af_xdp users call this new setsockopt(XDP_TX_METADATA_LEN) when they > plan to use metadata on tx. > This essentially requires allocating a fixed headroom to carry the metada= ta. > af_xdp machinery exports this fixed len into the bpf programs somehow > (devtx_frame.meta_len in this series). > Then it's up to the userspace and bpf program to agree on the layout. > If not every packet is expected to carry the metadata, there might be > some bitmask in the metadata area to indicate that. > > Iow, the metadata isn't interpreted by the kernel. It's up to the prog > to interpret it and call appropriate kfunc to enable some offload. The reason for the flag on RX is mostly performance: there's a substantial performance hit from reading the metadata area because it's not cache-hot; we want to avoid that when no metadata is in use. Putting the flag inside the metadata area itself doesn't work for this, because then you incur the cache miss just to read the flag. This effect is probably most pronounced on RX (because the packet is coming in off the NIC, so very unlikely that the memory has been touched before), but I see no reason it could not also occur on TX (it'll mostly depend on data alignment I guess?). So I think having a separate "TX metadata" flag in the descriptor is probably worth it for the performance gains? > Jesper raises a valid point with "what about redirected packets?". But > I'm not sure we need to care? Presumably the programs that do > xdp_redirect will have to conform to the same metadata layout? I don't think they have to do anything different semantically (agreeing on the data format in the metadata area will have to be by some out of band mechanism); but the performance angle is definitely valid here as well... -Toke