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 X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E72CCA9EAF for ; Mon, 21 Oct 2019 12:03:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EAF8820B7C for ; Mon, 21 Oct 2019 12:03:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="O7qeHzri" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727985AbfJUMDB (ORCPT ); Mon, 21 Oct 2019 08:03:01 -0400 Received: from mail-qt1-f194.google.com ([209.85.160.194]:38767 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727962AbfJUMDB (ORCPT ); Mon, 21 Oct 2019 08:03:01 -0400 Received: by mail-qt1-f194.google.com with SMTP id o25so7178159qtr.5; Mon, 21 Oct 2019 05:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=U8/udOB8d8afVM4Oiil7MLLGJE7IR3ULS8cNM3yTwf0=; b=O7qeHzritrAi5ln2djGB8me2A+mzu/G9YX49BXe973qpSe6aX5iPKqgS7kPuRn09Lb WpZxlZBZksbh36YHRGRmqm3c4uIca0X0idD58m8l+rMtCJE3yFR2iLd3uctah8IAQAvX GeS4+k81nijouRRLwoXDDkEY0cyPuqdPZNDKLi+J2KkPn+rectQ1xfn13yeAnQnq60Hi 1xQiuzY8jiKK9VrfBngn4XFbzj6f/j2asczOtEZcz//iU/arLWtcFUUVyw0oDhNiFnnR 0gOkf6rSZ4NTNK/x3+Mv7+kBR7N3Ex4sFZj3uYFj26nAAhmiRMRRfrhWeV+w8EPKJAQr GSYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=U8/udOB8d8afVM4Oiil7MLLGJE7IR3ULS8cNM3yTwf0=; b=OQebzz3pUKeqMCw46hWNbd4fd2tZk2qJdnPXvIb9zzGdP4N4qa3Lt67kl4TuXyBNWW BM1kP++QfxH9BbncaRjjIii/NQHXAPIqfCB1C3r57698i1MI1Ns6y7ISlsVx+7KCJ8Qc 0DXAINOE6vt7tPrOit0/UAGh7Z21EHuTo2nfDYw6UTAju5NE0qK4e9R/VaGUTC36YU6t W9B6u0h23iPKpMCmbGmdt8eYSQhDz12mPFLRxBOkkZwGn/pCicqI66nNr/mZ5UVIhWQV TSkLxDyzvZYscTSGE8NS334sK6n5o6bwFL7ZJROyq/CdpGJI+m+DRzQgi3a7MBIr04I8 oYZQ== X-Gm-Message-State: APjAAAWEC92s3bWwD5V+dHyM75MXBqQunWqCg/G7WEwKYWi4fOkfn68f x5Lb/HiWC/hp69fzMUIHP1jfHzLA2mMDgAgPgS4= X-Google-Smtp-Source: APXvYqwvV7DDvA6kJfv6a1UGR14BZ1tIjEvbM4WA/A6SnKgUEMc5M/4aiHb5ZPKpJGN4LRbB1/Bl9rtPlDK+lGjymCI= X-Received: by 2002:a05:6214:2c:: with SMTP id b12mr22998689qvr.10.1571659379702; Mon, 21 Oct 2019 05:02:59 -0700 (PDT) MIME-Version: 1.0 References: <20191021105938.11820-1-bjorn.topel@gmail.com> <87h842qpvi.fsf@toke.dk> In-Reply-To: <87h842qpvi.fsf@toke.dk> From: =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= Date: Mon, 21 Oct 2019 14:02:48 +0200 Message-ID: Subject: Re: [PATCH bpf-next v2] libbpf: use implicit XSKMAP lookup from AF_XDP XDP program To: =?UTF-8?B?VG9rZSBIw7hpbGFuZC1Kw7hyZ2Vuc2Vu?= Cc: Netdev , Alexei Starovoitov , Daniel Borkmann , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , bpf , Magnus Karlsson , "Karlsson, Magnus" , "Samudrala, Sridhar" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On Mon, 21 Oct 2019 at 13:50, Toke H=C3=B8iland-J=C3=B8rgensen wrote: > > Bj=C3=B6rn T=C3=B6pel writes: > > > From: Bj=C3=B6rn T=C3=B6pel > > > > In commit 43e74c0267a3 ("bpf_xdp_redirect_map: Perform map lookup in > > eBPF helper") the bpf_redirect_map() helper learned to do map lookup, > > which means that the explicit lookup in the XDP program for AF_XDP is > > not needed for post-5.3 kernels. > > > > This commit adds the implicit map lookup with default action, which > > improves the performance for the "rx_drop" [1] scenario with ~4%. > > > > For pre-5.3 kernels, the bpf_redirect_map() returns XDP_ABORTED, and a > > fallback path for backward compatibility is entered, where explicit > > lookup is still performed. This means a slight regression for older > > kernels (an additional bpf_redirect_map() call), but I consider that a > > fair punishment for users not upgrading their kernels. ;-) > > > > v1->v2: Backward compatibility (Toke) [2] > > > > [1] # xdpsock -i eth0 -z -r > > [2] https://lore.kernel.org/bpf/87pnirb3dc.fsf@toke.dk/ > > > > Suggested-by: Toke H=C3=B8iland-J=C3=B8rgensen > > Signed-off-by: Bj=C3=B6rn T=C3=B6pel > > --- > > tools/lib/bpf/xsk.c | 45 +++++++++++++++++++++++++++++++++++---------- > > 1 file changed, 35 insertions(+), 10 deletions(-) > > > > diff --git a/tools/lib/bpf/xsk.c b/tools/lib/bpf/xsk.c > > index b0f532544c91..391a126b3fd8 100644 > > --- a/tools/lib/bpf/xsk.c > > +++ b/tools/lib/bpf/xsk.c > > @@ -274,33 +274,58 @@ static int xsk_load_xdp_prog(struct xsk_socket *x= sk) > > /* This is the C-program: > > * SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx) > > * { > > - * int index =3D ctx->rx_queue_index; > > + * int ret, index =3D ctx->rx_queue_index; > > * > > * // A set entry here means that the correspnding queue_id > > * // has an active AF_XDP socket bound to it. > > + * ret =3D bpf_redirect_map(&xsks_map, index, XDP_PASS); > > + * ret &=3D XDP_PASS | XDP_REDIRECT; > > Why the masking? Looks a bit weird (XDP return codes are not defined as > bitmask values), and it's not really needed, is it? > bpf_redirect_map() returns a 32-bit signed int, so the upper 32-bit will need to be cleared. Having an explicit AND is one instruction less than two shifts. So, it's an optimization (every instruction is sacred). Compare these two: 0000000000000000 xdp_sock_prog: ; int ret, index =3D ctx->rx_queue_index; 0: 61 12 10 00 00 00 00 00 r2 =3D *(u32 *)(r1 + 16) 1: 63 2a fc ff 00 00 00 00 *(u32 *)(r10 - 4) =3D r2 ; ret =3D bpf_redirect_map(&xsks_map, index, XDP_PASS); 2: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 =3D 0 ll 4: b7 03 00 00 02 00 00 00 r3 =3D 2 5: 85 00 00 00 33 00 00 00 call 51 ; ret &=3D XDP_PASS | XDP_REDIRECT; 6: 57 00 00 00 06 00 00 00 r0 &=3D 6 ; if (ret) 7: 55 00 0d 00 00 00 00 00 if r0 !=3D 0 goto +13 8: bf a2 00 00 00 00 00 00 r2 =3D r10 ; if (bpf_map_lookup_elem(&xsks_map, &index)) 9: 07 02 00 00 fc ff ff ff r2 +=3D -4 10: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 =3D 0 ll 12: 85 00 00 00 01 00 00 00 call 1 13: bf 01 00 00 00 00 00 00 r1 =3D r0 14: b7 00 00 00 02 00 00 00 r0 =3D 2 15: 15 01 05 00 00 00 00 00 if r1 =3D=3D 0 goto +5 ; return bpf_redirect_map(&xsks_map, index, 0); 16: 61 a2 fc ff 00 00 00 00 r2 =3D *(u32 *)(r10 - 4) 17: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 =3D 0 ll 19: b7 03 00 00 00 00 00 00 r3 =3D 0 20: 85 00 00 00 33 00 00 00 call 51 00000000000000a8 LBB0_3: ; } 21: 95 00 00 00 00 00 00 00 exit Disassembly of section xdp_sock: 0000000000000000 xdp_sock_prog: ; int ret, index =3D ctx->rx_queue_index; 0: 61 12 10 00 00 00 00 00 r2 =3D *(u32 *)(r1 + 16) 1: 63 2a fc ff 00 00 00 00 *(u32 *)(r10 - 4) =3D r2 ; ret =3D bpf_redirect_map(&xsks_map, index, XDP_PASS); 2: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 =3D 0 ll 4: b7 03 00 00 02 00 00 00 r3 =3D 2 5: 85 00 00 00 33 00 00 00 call 51 6: 67 00 00 00 20 00 00 00 r0 <<=3D 32 7: c7 00 00 00 20 00 00 00 r0 s>>=3D 32 ; if (ret > 0) 8: 65 00 0d 00 00 00 00 00 if r0 s> 0 goto +13 9: bf a2 00 00 00 00 00 00 r2 =3D r10 ; if (bpf_map_lookup_elem(&xsks_map, &index)) 10: 07 02 00 00 fc ff ff ff r2 +=3D -4 11: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 =3D 0 ll 13: 85 00 00 00 01 00 00 00 call 1 14: bf 01 00 00 00 00 00 00 r1 =3D r0 15: b7 00 00 00 02 00 00 00 r0 =3D 2 16: 15 01 05 00 00 00 00 00 if r1 =3D=3D 0 goto +5 ; return bpf_redirect_map(&xsks_map, index, 0); 17: 61 a2 fc ff 00 00 00 00 r2 =3D *(u32 *)(r10 - 4) 18: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 =3D 0 ll 20: b7 03 00 00 00 00 00 00 r3 =3D 0 21: 85 00 00 00 33 00 00 00 call 51 00000000000000b0 LBB0_3: ; } 22: 95 00 00 00 00 00 00 00 exit Bj=C3=B6rn > -Toke >