Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp208352ybm; Wed, 22 May 2019 01:39:04 -0700 (PDT) X-Google-Smtp-Source: APXvYqz9SzMOubUeGMi9zIP0hiARb1o365NOqJpCMuMCvzg9FUn7itGMq//HRSO532z1/ZQx2X4u X-Received: by 2002:a62:198e:: with SMTP id 136mr75468824pfz.180.1558514344482; Wed, 22 May 2019 01:39:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558514344; cv=none; d=google.com; s=arc-20160816; b=Cdr+kLxrhZl3ApzjINc9324gJT+vUTUbGPco6fsC8dZDfwh3D7v3yCmSphwN3+jW+4 CsailZZb/3/upWj3lz0aEwViTseL/62t/xBN2BfL4nrNanE4OBxEj92GfK9kNWNoNQxj BkyCiAGXyZO7v77+EY+Ol0tozEpgosg/i/VKkzvZTwzhrh8PuOaLMShUv2w1IFzE/pQi FQUekZAtb7gEL3uaY2U2F1kr0DnXGnEe7oAcWlk/l39LAoEz7NeuyBz0ELQ5GTVemCUF MrFkK4Xz0wgyFh/3m7dgSLBBiQA8+8I7a08ZvRcQKnnL39QBp6zPE+IodxuMm6njIbX+ gq2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=WwDM6cgk1DlZjPsTsyOUSiW4Ua4wRBH3CeZwcT3TF4g=; b=kw2C0lLyVvDLk0uT/qrs0rsDf44JZXRZOSk5t4AQZLyqOZEELYd+pS5A2duBwqS2yM AJsmksr0LNcKp7rvqplMhpbKDkDZT3Gouc0096bF16E48wAfWkesEDIaQdy8MADZppLx x5f1kNK0FPXHTIlrnrAUBEBBxi4aU3L43BawBsBICKlNqmV+YZY2+G3OHb/++/1Uqe6H FgPA+tBlcF9gy7xCIaZiuJfn3f1MYAVQ0aQ3U/PBcuylZowaI9iunWOOXi6UgFt69urW YBcGSA5SL6oG7IGTPekPdoAQOReSQDZdtML/Gi86nOenWfyyIgKaXb4el5Tm8sZk7TST a3EA== ARC-Authentication-Results: i=1; mx.google.com; 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 v10si25476197plg.155.2019.05.22.01.38.49; Wed, 22 May 2019 01:39:04 -0700 (PDT) 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; 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 S1728907AbfEVIhh (ORCPT + 99 others); Wed, 22 May 2019 04:37:37 -0400 Received: from www62.your-server.de ([213.133.104.62]:45058 "EHLO www62.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728609AbfEVIhg (ORCPT ); Wed, 22 May 2019 04:37:36 -0400 Received: from [78.46.172.3] (helo=sslproxy06.your-server.de) by www62.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89_1) (envelope-from ) id 1hTMkR-0004Uo-If; Wed, 22 May 2019 10:37:19 +0200 Received: from [2a02:120b:c3fc:feb0:dda7:bd28:a848:50e2] (helo=linux.home) by sslproxy06.your-server.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1hTMkR-000LEU-B8; Wed, 22 May 2019 10:37:19 +0200 Subject: Re: tc_classid access in skb bpf context To: Matthew Cover , Alexei Starovoitov , Martin KaFai Lau , Song Liu , Yonghong Song , "David S. Miller" , "netdev@vger.kernel.org" , "bpf@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: Matthew Cover References: From: Daniel Borkmann Message-ID: <73d5b951-2598-0d7f-5b6e-8925cc61989a@iogearbox.net> Date: Wed, 22 May 2019 10:37:18 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Authenticated-Sender: daniel@iogearbox.net X-Virus-Scanned: Clear (ClamAV 0.100.3/25456/Tue May 21 09:56:54 2019) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 05/22/2019 01:52 AM, Matthew Cover wrote: > __sk_buff has a member tc_classid which I'm interested in accessing from the skb bpf context. > > A bpf program which accesses skb->tc_classid compiles, but fails verification; the specific failure is "invalid bpf_context access". > > if (skb->tc_classid != 0) > return 1; > return 0; > > Some of the tests in tools/testing/selftests/bpf/verifier/ (those on tc_classid) further confirm that this is, in all likelihood, intentional behavior. > > The very similar bpf program which instead accesses skb->mark works as desired. > > if (skb->mark != 0) > return 1; > return 0; You should be able to access skb->tc_classid, perhaps you're using the wrong program type? BPF_PROG_TYPE_SCHED_CLS is supposed to work (if not we'd have a regression). > I built a kernel (v5.1) with 4 instances of the following line removed from net/core/filter.c to test the behavior when the instructions pass verification. > > switch (off) { > - case bpf_ctx_range(struct __sk_buff, tc_classid): > ... > return false; > > It appears skb->tc_classid is always zero within my bpf program, even when I verify by other means (e.g. netfilter) that the value is set non-zero. > > I gather that sk_buff proper sometimes (i.e. at some layers) has qdisc_skb_cb stored in skb->cb, but not always. > > I suspect that the tc_classid is available at l3 (and therefore to utils like netfilter, ip route, tc), but not at l2 (and not to AF_PACKET). From tc/BPF context you can use it; it's been long time, but I think back then we mapped it into cb[] so it can be used within the BPF context to pass skb data around e.g. between tail calls, and cls_bpf_classify() when in direct-action mode which likely everyone is/should-be using then maps that skb->tc_classid u16 cb[] value to res->classid on program return which then in either sch_handle_ingress() or sch_handle_egress() is transferred into the skb->tc_index. > Is it impractical to make skb->tc_classid available in this bpf context or is there just some plumbing which hasn't been connected yet? > > Is my suspicion that skb->cb no longer contains qdisc_skb_cb due to crossing a layer boundary well founded? > > I'm willing to look into hooking things together as time permits if it's a feasible task. > > It's trivial to have iptables match on tc_classid and set a mark which is available to bpf at l2, but I'd like to better understand this. > > Thanks, > Matt C. >