From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 927ECC433F5 for ; Sat, 23 Apr 2022 20:05:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236640AbiDWUIq (ORCPT ); Sat, 23 Apr 2022 16:08:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233954AbiDWUIo (ORCPT ); Sat, 23 Apr 2022 16:08:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5C7A039BAE for ; Sat, 23 Apr 2022 13:05:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1650744345; 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=omqCTV7z0t9cAhQIWiyGEWe7yOix1dL5G0MldE6hRWE=; b=O0NH1oeFosajw3OoE/hGHtvrQMzYJzp8WNBwKfhSpl5wev78J0Nzuma7EaJrZHKKVUD8ae jpACEerXZKwurUEtpM7d6TesGa3AAgyLmh1m3Uk1RHwfHv/dS3kJNwVQdSaAmssikYNgNh mlSolmMWAvrGm7xxj7u3Ge/oW1FzTfM= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-54-dYe4JbWYPIGQV-xAZdCFMw-1; Sat, 23 Apr 2022 16:05:44 -0400 X-MC-Unique: dYe4JbWYPIGQV-xAZdCFMw-1 Received: by mail-ej1-f69.google.com with SMTP id go12-20020a1709070d8c00b006f009400732so5121154ejc.1 for ; Sat, 23 Apr 2022 13:05:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version:content-transfer-encoding; bh=omqCTV7z0t9cAhQIWiyGEWe7yOix1dL5G0MldE6hRWE=; b=B2Hle08Fruz+UDS9QqClGs+i+dmmJWbJzYNL9CgFkMjvfV+uiAPmk+oSDsMbtHwBXQ MLrWztSvVAbAlRjTX/qlkVX8AIZSBL7mKd0IYjFAydFB7a8ORd+4ffQQsxRCiEWKlglc 8RpGyL9GMIwI/NvmLose18TphDcQ04R7PCHJdUXwZlzYmLdONiT/EEa17Fg6ov+KqLNY F0kJ11m+07Kcbn+gOstEwvqHKfQHzeSCidXzwj+aHvbFFXmbjWuG2C9WKrPP8HfWCY5/ b0Ews0RRegpRc7a9rJZ6aD3cbjrknkcPPn2NTQRf02aHVg+fd9dHgZ55RcX/WLnwovuS DeWA== X-Gm-Message-State: AOAM531WfqPb52OW/yRwaX7yDWHfsBbgCN+Th5aZBt3CeF4G9BFL/ITM iuBuEseHkFVRLOHqIqvbEGLRYYymMiK6vy2INFkP2kq6Tj8OiYr3/wFtB2vXNxghcsSZIxywfft mAV6PZIuJhCzc X-Received: by 2002:aa7:c793:0:b0:408:4a69:90b4 with SMTP id n19-20020aa7c793000000b004084a6990b4mr11441731eds.58.1650744342331; Sat, 23 Apr 2022 13:05:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNATyw5D0bERdGDSDbR5cr0iO3FsZbehP/gW+psGoNThVAdHDplZOXPi3qHJP4nJhcVj+Z6w== X-Received: by 2002:aa7:c793:0:b0:408:4a69:90b4 with SMTP id n19-20020aa7c793000000b004084a6990b4mr11441658eds.58.1650744341414; Sat, 23 Apr 2022 13:05:41 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id e19-20020a056402105300b004162d0b4cbbsm2502266edu.93.2022.04.23.13.05.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Apr 2022 13:05:40 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id DE7992D1FA3; Sat, 23 Apr 2022 22:05:39 +0200 (CEST) From: Toke =?utf-8?Q?H=C3=B8iland-J=C3=B8rgensen?= To: Alexander Lobakin Cc: Larysa Zaremba , bpf , netdev , Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , Magnus Karlsson , Maciej Fijalkowski , Alexander Lobakin Subject: Re: Accessing XDP packet memory from the end In-Reply-To: <20220422164137.875143-1-alexandr.lobakin@intel.com> References: <20220421155620.81048-1-larysa.zaremba@intel.com> <87czhagxuw.fsf@toke.dk> <20220422164137.875143-1-alexandr.lobakin@intel.com> X-Clacks-Overhead: GNU Terry Pratchett Date: Sat, 23 Apr 2022 22:05:39 +0200 Message-ID: <87a6cbd0q4.fsf@toke.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Alexander Lobakin writes: > From: Toke H=C3=B8iland-J=C3=B8rgensen > Date: Thu, 21 Apr 2022 19:17:11 +0200 > >> Larysa Zaremba writes: >>=20 >> > Dear all, >> > Our team has encountered a need of accessing data_meta in a following = way: >> > >> > int xdp_meta_prog(struct xdp_md *ctx) >> > { >> > void *data_meta_ptr =3D (void *)(long)ctx->data_meta; >> > void *data_end =3D (void *)(long)ctx->data_end; >> > void *data =3D (void *)(long)ctx->data; >> > u64 data_size =3D sizeof(u32); >> > u32 magic_meta; >> > u8 offset; >> > >> > offset =3D (u8)((s64)data - (s64)data_meta_ptr); >> > if (offset < data_size) { >> > bpf_printk("invalid offset: %ld\n", offset); >> > return XDP_DROP; >> > } >> > >> > data_meta_ptr +=3D offset; >> > data_meta_ptr -=3D data_size; >> > >> > if (data_meta_ptr + data_size > data) { >> > return XDP_DROP; >> > } >> >=20=09=09 >> > magic_meta =3D *((u32 *)data); >> > bpf_printk("Magic: %d\n", magic_meta); >> > return XDP_PASS; >> > } >> > >> > Unfortunately, verifier claims this code attempts to access packet with >> > an offset of -2 (a constant part) and negative offset is generally for= bidden. >> > >> > For now we have 2 solutions, one is using bpf_xdp_adjust_meta(), >> > which is pretty good, but not ideal for the hot path. >> > The second one is the patch at the end. >> > >> > Do you see any other way of accessing memory from the end of data_meta= /data? >> > What do you think about both suggested solutions? >>=20 >> The problem is that the compiler is generating code that the verifier >> doesn't understand. It's notoriously hard to get LLVM to produce code >> that preserves the right bounds checks which is why projects like Cilium >> use helpers with inline ASM to produce the right loads, like in [0]. >>=20 >> Adapting that cilium helper to load from the metadata area, your example >> can be rewritten as follows (which works just fine with no verifier >> changes): >>=20 >> static __always_inline int >> xdp_load_meta_bytes(const struct xdp_md *ctx, __u64 off, void *to, const= __u64 len) >> { >> void *from; >> int ret; >> /* LLVM tends to generate code that verifier doesn't understand, >> * so force it the way we want it in order to open up a range >> * on the reg. >> */ >> asm volatile("r1 =3D *(u32 *)(%[ctx] +8)\n\t" >> "r2 =3D *(u32 *)(%[ctx] +0)\n\t" >> "%[off] &=3D %[offmax]\n\t" >> "r1 +=3D %[off]\n\t" >> "%[from] =3D r1\n\t" >> "r1 +=3D %[len]\n\t" >> "if r1 > r2 goto +2\n\t" >> "%[ret] =3D 0\n\t" >> "goto +1\n\t" >> "%[ret] =3D %[errno]\n\t" >> : [ret]"=3Dr"(ret), [from]"=3Dr"(from) >> : [ctx]"r"(ctx), [off]"r"(off), [len]"ri"(len), >> [offmax]"i"(__CTX_OFF_MAX), [errno]"i"(-EINVAL) >> : "r1", "r2"); >> if (!ret) >> __builtin_memcpy(to, from, len); >> return ret; >> } >>=20 >>=20 >> SEC("xdp") >> int xdp_meta_prog(struct xdp_md *ctx) >> { >> void *data_meta_ptr =3D (void *)(long)ctx->data_meta; >> void *data =3D (void *)(long)ctx->data; >> __u32 magic_meta; >> __u8 offset; >> int ret; >>=20 >> offset =3D (__u8)((__s64)data - (__s64)data_meta_ptr); >> ret =3D xdp_load_meta_bytes(ctx, offset - 4, &magic_meta, sizeof(magic_= meta)); >> if (ret) { >> bpf_printk("load bytes failed: %d\n", ret); >> return XDP_DROP; >> } >>=20 >> bpf_printk("Magic: %d\n", magic_meta); >> return XDP_PASS; >> } > > At the moment, we use this (based on Cilium's and your), it works > just like we want C code to work previously: > > #define __CTX_OFF_MAX 0xff > > static __always_inline void * > can_i_access_meta_please(const struct xdp_md *ctx, __u64 off, const __u64= len) > { > void *ret; > > /* LLVM tends to generate code that verifier doesn't understand, > * so force it the way we want it in order to open up a range > * on the reg. > */ > asm volatile("r1 =3D *(u32 *)(%[ctx] +8)\n\t" > "r2 =3D *(u32 *)(%[ctx] +0)\n\t" > "%[off] &=3D %[offmax]\n\t" > "r1 +=3D %[off]\n\t" > "%[ret] =3D r1\n\t" > "r1 +=3D %[len]\n\t" > "if r1 > r2 goto +1\n\t" > "goto +1\n\t" > "%[ret] =3D %[null]\n\t" > : [ret]"=3Dr"(ret) > : [ctx]"r"(ctx), [off]"r"(off), [len]"ri"(len), > [offmax]"i"(__CTX_OFF_MAX), [null]"i"(NULL) > : "r1", "r2"); > > return ret; > } > > SEC("xdp") > int xdp_prognum_n0_meta(struct xdp_md *ctx) > { > void *data_meta =3D (void *)(__s64)ctx->data_meta; > void *data =3D (void *)(__s64)ctx->data; > struct xdp_meta_generic *md; > __u64 offset; > > offset =3D (__u64)((__s64)data - (__s64)data_meta); > > md =3D can_i_access_meta_please(ctx, offset, sizeof(*md)); > if (__builtin_expect(!md, 0)) { > bpf_printk("No you can't\n"); > return XDP_DROP; > } > > bpf_printk("Magic: 0x%04x\n", md->magic_id); > return XDP_PASS; > } > > Thanks for the help! Great! You're welcome! :) > It's a shame LLVM still suck on generating correct object code from C. > I guess we'll define a helper above in one of the headers to not > copy-paste it back and forth between each program wanting to access > only the generic part of the metadata (which is always being placed at > the end). Yeah, it would be nice if LLVM could just generate code that works, but in the meantime we'll just have to define a helper. I suspect we'll need to define some helper functions to work with xdp-hints style metadata field anyway, so wrapping the reader into that somewhere would probably make sense, no? -Toke