Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp913010ybl; Wed, 11 Dec 2019 09:25:36 -0800 (PST) X-Google-Smtp-Source: APXvYqzBM8gyXUukr6eYZ8BqER0Yri/aJ4IcmBZq2QkonmzEAxn+ml/lpBf6drtJ73EGcbrRwUwR X-Received: by 2002:a9d:6a4c:: with SMTP id h12mr3155853otn.81.1576085136869; Wed, 11 Dec 2019 09:25:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576085136; cv=none; d=google.com; s=arc-20160816; b=qzBdiZSaMqxLOw8Az2u3G0/Ml3YI5GiNT5Su35XD5WJT/nbg/dqTLoiKDA7BA0a1BK wek39YrJTDH+10TiW0z7d0IUO2nuXCZcVrHfbP4BEseIfptAUCnF49c0cYCMS/V44iBP FZPhFjMQGHvmEsLvvJj1+e6HQpgaB3qNUioPdC67JaK8pyHQ1Q/Qnb6hDTZ6bUEd3UJ5 oX+MmCJx+0cjchpKQkkJhqH3n9payZL4EI+ecT6JjlJlx1sxpNCN2HWlce92Bd+msutM eaw9ymtieK+EiV9CHaR6uHUWpn2axT4OIy6RXZey0Fh+UBmRnAImxn1WtG4kTsKbF0Kb e6WQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=kXZ4aqS0uFPSyTpXVREqajZ2V8XvicWtvP1bC/iBXA0=; b=tERT1oI7DSjB8dUvthSRC3hXvx6ioLismy5o9/kzQvwbqw8nwTCd2n+re2Txiyg1n4 dHKduLuoHy9kZLkjciZcAkeFNZQCCqqhd/L6bvzn26yITx9FLtp9pPG38H2I2sWqx2R9 WlqZVG7HaB5DapYorikANolE8ThFC3w5FNI0yen1dwT+XycRdLYETgnnNosaA6ZE7Dct 3Cu7Y232gziCO4Zn8jwdB9L5WsKVDD8U1hqknPpoehpIiQV1aH2RoX+PXEXn418whcD3 HebdL6ONtaKwUtZR90mIqebWe2y4DGJ/+e4vrk1SUdV7beELsgA9QndNnDBzKyIBLYIO lYyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fomichev-me.20150623.gappssmtp.com header.s=20150623 header.b=MhspcRSZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u18si1543198oia.80.2019.12.11.09.25.24; Wed, 11 Dec 2019 09:25:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fomichev-me.20150623.gappssmtp.com header.s=20150623 header.b=MhspcRSZ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731128AbfLKRYf (ORCPT + 99 others); Wed, 11 Dec 2019 12:24:35 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:45742 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730927AbfLKRYe (ORCPT ); Wed, 11 Dec 2019 12:24:34 -0500 Received: by mail-pg1-f193.google.com with SMTP id b9so10670447pgk.12 for ; Wed, 11 Dec 2019 09:24:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fomichev-me.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=kXZ4aqS0uFPSyTpXVREqajZ2V8XvicWtvP1bC/iBXA0=; b=MhspcRSZVu7valP3RyaPPWgFdz8/QMgQeVJJ2pRuqjM7KuaSUhImmxUXvLqJHULlV9 hcfZ3gSd7/E+yG4sAL30QUImCTx2MrAbZB5x3T1/zYZlYdi3uOS2SZOrfVAmPVTArZzx kb2Gq4rOUSInPffbEAu3bKqeEIF0dV6jcfPGzPlUknSA+ChqI9BtHhaLc//8vVaz1Gyh 7xX1GkzwHG0rcacg3f00JYEcLj2vuF+5l1dQh80lRRhtztXlYOlLlNzcjpoBbM4jKPZs HyyFsfq6/ykX/17GbAXQbd2ymutX5vArM710aRCUuqVYUQ7VpidmybLDGo/ZpmWy+zxQ FNdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=kXZ4aqS0uFPSyTpXVREqajZ2V8XvicWtvP1bC/iBXA0=; b=Q+b24kSurSCX0JYAAHjBzbSESnDQKCOaY6++z3fnYxpExewSif0aSAoF5BSWTonPjp vQSUG7QaOMabusyMB70EyUE9jPVNtYsMCirrlMkEFggho13D78BkORFUaMcJ7hgxbk/n OHcnJWWWhQ7rlluZNdJEHsko9LkJOo0LhIsDvgdVFcP3MvQZEv04tnuP16r4RF1o8Wdb 4dA3R1+P/5vuMEnRjkP8M6tmx9LdrydJziHBWfzGFLWhO5A/JZrilOOedjZ8GOP99/uo yAa5gCdVo98Cx1JNibTKRTnb1we4Oj4m7B7F1LfYqqk8nLRwYEIXm+IqYgwnLIL632Xu LBAw== X-Gm-Message-State: APjAAAU6x7Dy31LWNwt0QNHcVNKPKPI7EnRlsawuSFVmfh6nA4IbNTMa o8k7znrlROhXJ/pEMIrqtaWCJA== X-Received: by 2002:a62:446:: with SMTP id 67mr4738128pfe.109.1576085073816; Wed, 11 Dec 2019 09:24:33 -0800 (PST) Received: from localhost ([2601:646:8f00:18d9:d0fa:7a4b:764f:de48]) by smtp.gmail.com with ESMTPSA id a14sm3619810pfn.22.2019.12.11.09.24.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Dec 2019 09:24:33 -0800 (PST) Date: Wed, 11 Dec 2019 09:24:32 -0800 From: Stanislav Fomichev To: Andrii Nakryiko Cc: Jakub Kicinski , Andrii Nakryiko , LKML , bpf , Networking , Alexei Starovoitov , Daniel Borkmann , Kernel Team Subject: Re: [PATCH bpf-next 11/15] bpftool: add skeleton codegen command Message-ID: <20191211172432.GC3105713@mini-arch> References: <20191210011438.4182911-1-andriin@fb.com> <20191210011438.4182911-12-andriin@fb.com> <20191209175745.2d96a1f0@cakuba.netronome.com> <20191210100536.7a57d5e1@cakuba.netronome.com> <20191210214407.GA3105713@mini-arch> <20191210225900.GB3105713@mini-arch> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.2 (2019-09-21) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12/10, Andrii Nakryiko wrote: > On Tue, Dec 10, 2019 at 2:59 PM Stanislav Fomichev wrote: > > > > On 12/10, Andrii Nakryiko wrote: > > > On Tue, Dec 10, 2019 at 1:44 PM Stanislav Fomichev wrote: > > > > > > > > On 12/10, Jakub Kicinski wrote: > > > > > On Tue, 10 Dec 2019 09:11:31 -0800, Andrii Nakryiko wrote: > > > > > > On Mon, Dec 9, 2019 at 5:57 PM Jakub Kicinski wrote: > > > > > > > On Mon, 9 Dec 2019 17:14:34 -0800, Andrii Nakryiko wrote: > > > > > > > > struct { > > > > > > > > /* used by libbpf's skeleton API */ > > > > > > > > struct bpf_object_skeleton *skeleton; > > > > > > > > /* bpf_object for libbpf APIs */ > > > > > > > > struct bpf_object *obj; > > > > > > > > struct { > > > > > > > > /* for every defined map in BPF object: */ > > > > > > > > struct bpf_map *; > > > > > > > > } maps; > > > > > > > > struct { > > > > > > > > /* for every program in BPF object: */ > > > > > > > > struct bpf_program *; > > > > > > > > } progs; > > > > > > > > struct { > > > > > > > > /* for every program in BPF object: */ > > > > > > > > struct bpf_link *; > > > > > > > > } links; > > > > > > > > /* for every present global data section: */ > > > > > > > > struct __ { > > > > > > > > /* memory layout of corresponding data section, > > > > > > > > * with every defined variable represented as a struct field > > > > > > > > * with exactly the same type, but without const/volatile > > > > > > > > * modifiers, e.g.: > > > > > > > > */ > > > > > > > > int *my_var_1; > > > > > > > > ... > > > > > > > > } *; > > > > > > > > }; > > > > > > > > > > > > > > I think I understand how this is useful, but perhaps the problem here > > > > > > > is that we're using C for everything, and simple programs for which > > > > > > > loading the ELF is majority of the code would be better of being > > > > > > > written in a dynamic language like python? Would it perhaps be a > > > > > > > better idea to work on some high-level language bindings than spend > > > > > > > time writing code gens and working around limitations of C? > > > > > > > > > > > > None of this work prevents Python bindings and other improvements, is > > > > > > it? Patches, as always, are greatly appreciated ;) > > > > > > > > > > This "do it yourself" shit is not really funny :/ > > > > > > > > > > I'll stop providing feedback on BPF patches if you guy keep saying > > > > > that :/ Maybe that's what you want. > > > > > > > > > > > This skeleton stuff is not just to save code, but in general to > > > > > > simplify and streamline working with BPF program from userspace side. > > > > > > Fortunately or not, but there are a lot of real-world applications > > > > > > written in C and C++ that could benefit from this, so this is still > > > > > > immensely useful. selftests/bpf themselves benefit a lot from this > > > > > > work, see few of the last patches in this series. > > > > > > > > > > Maybe those applications are written in C and C++ _because_ there > > > > > are no bindings for high level languages. I just wish BPF programming > > > > > was less weird and adding some funky codegen is not getting us closer > > > > > to that goal. > > > > > > > > > > In my experience code gen is nothing more than a hack to work around > > > > > bad APIs, but experiences differ so that's not a solid argument. > > > > *nod* > > > > > > > > We have a nice set of C++ wrappers around libbpf internally, so we can do > > > > something like BpfMap and get a much better interface > > > > with type checking. Maybe we should focus on higher level languages instead? > > > > We are open to open-sourcing our C++ bits if you want to collaborate. > > > > > > Python/C++ bindings and API wrappers are an orthogonal concerns here. > > > I personally think it would be great to have both Python and C++ > > > specific API that uses libbpf under the cover. The only debatable > > > thing is the logistics: where the source code lives, how it's kept in > > > sync with libbpf, how we avoid crippling libbpf itself because > > > something is hard or inconvenient to adapt w/ Python, etc. > > > > [..] > > > The problem I'm trying to solve here is not really C-specific. I don't > > > think you can solve it without code generation for C++. How do you > > > "generate" BPF program-specific layout of .data, .bss, .rodata, etc > > > data sections in such a way, where it's type safe (to the degree that > > > language allows that, of course) and is not "stringly-based" API? This > > > skeleton stuff provides a natural, convenient and type-safe way to > > > work with global data from userspace pretty much at the same level of > > > performance and convenience, as from BPF side. How can you achieve > > > that w/ C++ without code generation? As for Python, sure you can do > > > dynamic lookups based on just the name of property/method, but amount > > > of overheads is not acceptable for all applications (and Python itself > > > is not acceptable for those applications). In addition to that, C is > > > the best way for other less popular languages (e.g., Rust) to leverage > > > libbpf without investing lots of effort in re-implementing libbpf in > > > Rust. > > I'd say that a libbpf API similar to dlopen/dlsym is a more > > straightforward thing to do. Have a way to "open" a section and > > a way to find a symbol in it. Yes, it's a string-based API, > > but there is nothing wrong with it. IMO, this is easier to > > use/understand and I suppose Python/C++ wrappers are trivial. > > Without digging through libbpf source code (or actually, look at code, > but don't run any test program), what's the name of the map > corresponding to .bss section, if object file is > some_bpf_object_file.o? If you got it right (congrats, btw, it took me > multiple attempts to memorize the pattern), how much time did you > spend looking it up? Now compare it to `skel->maps.bss`. Further, if > you use anonymous structs for your global vars, good luck maintaining > two copies of that: one for BPF side and one for userspace. As your average author of BPF programs I don't really care which section my symbol ends up into. Just give me an api to mmap all "global" sections (or a call per section which does all the naming magic inside) and lookup symbol by name; I can cast it to a proper type and set it. RE anonymous structs: maybe don't use them if you want to share the data between bpf and userspace? > I never said there is anything wrong with current straightforward > libbpf API, but I also never said it's the easiest and most > user-friendly way to work with BPF either. So we'll have both > code-generated interface and existing API. Furthermore, they are > interoperable (you can pass skel->maps.whatever to any of the existing > libbpf APIs, same for progs, links, obj itself). But there isn't much > that can beat performance and usability of code-generated .data, .bss, > .rodata (and now .extern) layout. I haven't looked closely enough, but is there a libbpf api to get an offset of a variable? Suppose I have the following in bpf.c: int a; int b; Can I get an offset of 'b' in the .bss without manually parsing BTF? TBH, I don't buy the performance argument for these global maps. When you did the mmap patchset for the array, you said it yourself that it's about convenience and not performance. > > As for type-safety: it's C, forget about it :-) > > C is weakly, but still typed language. There are types and they are > helpful. Yes, you can disregard them and re-interpret values as > anything, but that's beside the point. My point was that there is a certain mental model when working with this type of external symbols which feels "natural" for C (dlopen/dlsym). But I agree with you, that as long as code-gen is optional and there is an alternative api in libbpf, we should be good.