From patchwork Tue Feb 19 05:38:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lawrence Brakmo X-Patchwork-Id: 1044448 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=fb.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=fb.com header.i=@fb.com header.b="hjVXU1gM"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 443V0b3R2Vz9s3l for ; Tue, 19 Feb 2019 16:39:11 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726845AbfBSFjG (ORCPT ); Tue, 19 Feb 2019 00:39:06 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:52158 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725764AbfBSFjE (ORCPT ); Tue, 19 Feb 2019 00:39:04 -0500 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x1J5d0rL003570 for ; Mon, 18 Feb 2019 21:39:04 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-type; s=facebook; bh=LYM0bXLJ09563K5Qi+Sanh1Hu0vg4uvQeiy/qLEYFSI=; b=hjVXU1gMf+5vGIng2efzKgADSzi+KdPIWWbZKsDA+NfYC59P0MWNtqx1bth4H+kuZUxu 7RSpty0XYPpEyfC06Hczdhz4R5lFsfDo3b1yQF9YQbtM66CGT3v8Zo9x3nwt89xbmeVH MtQS8/QOL9Fz72QFzdWp8q8qGqvCW5Yc7VA= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 2qr8yhgbbk-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 18 Feb 2019 21:39:03 -0800 Received: from mx-out.facebook.com (2620:10d:c0a1:3::13) by mail.thefacebook.com (2620:10d:c021:18::171) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1531.3; Mon, 18 Feb 2019 21:38:33 -0800 Received: by devbig009.ftw2.facebook.com (Postfix, from userid 10340) id 5B3DC5AE1EBC; Mon, 18 Feb 2019 21:38:30 -0800 (PST) Smtp-Origin-Hostprefix: devbig From: brakmo Smtp-Origin-Hostname: devbig009.ftw2.facebook.com To: netdev CC: Martin Lau , Alexei Starovoitov , Daniel Borkmann --cc=Kernel Team <"daniel@iogearbox.netKernel-team"@fb.com> Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next 1/9] bpf: Add bpf helper bpf_tcp_enter_cwr Date: Mon, 18 Feb 2019 21:38:30 -0800 Message-ID: <20190219053830.2086578-1-brakmo@fb.com> X-Mailer: git-send-email 2.17.1 X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-02-19_04:, , signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This patch adds a new bpf helper BPF_FUNC_tcp_enter_cwr "int bpf_tcp_enter_cwr(struct bpf_tcp_sock *tp)". It is added to BPF_PROG_TYPE_CGROUP_SKB typed bpf_prog which currently can be attached to the ingress and egress path. This helper makes a tcp_sock enter CWR state. It can be used by a bpf_prog to manage egress network bandwidth limit per cgroupv2. A later patch will have a sample program to show how it can be used to limit bandwidth usage per cgroupv2. Signed-off-by: Lawrence Brakmo Signed-off-by: Martin KaFai Lau --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 9 ++++++++- kernel/bpf/verifier.c | 4 ++++ net/core/filter.c | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index de18227b3d95..525628c913c9 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -195,6 +195,7 @@ enum bpf_arg_type { ARG_PTR_TO_SOCKET, /* pointer to bpf_sock */ ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */ ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */ + ARG_PTR_TO_TCP_SOCK, /* pointer to tcp_sock */ }; /* type of values returned from helper functions */ diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index bcdd2474eee7..9e9f4f1a0370 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2359,6 +2359,12 @@ union bpf_attr { * Return * A **struct bpf_tcp_sock** pointer on success, or NULL in * case of failure. + * + * int bpf_tcp_enter_cwr(struct bpf_tcp_sock *tp) + * Description + * Make a tcp_sock enter CWR state. + * Return + * 0 */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -2457,7 +2463,8 @@ union bpf_attr { FN(spin_lock), \ FN(spin_unlock), \ FN(sk_fullsock), \ - FN(tcp_sock), + FN(tcp_sock), \ + FN(tcp_enter_cwr), /* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1b9496c41383..95fb385c6f3c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2424,6 +2424,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno, return -EFAULT; } meta->ptr_id = reg->id; + } else if (arg_type == ARG_PTR_TO_TCP_SOCK) { + expected_type = PTR_TO_TCP_SOCK; + if (type != expected_type) + goto err_type; } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { if (meta->func_id == BPF_FUNC_spin_lock) { if (process_spin_lock(env, regno, true)) diff --git a/net/core/filter.c b/net/core/filter.c index b584cb42a803..f51c4a781844 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5426,6 +5426,18 @@ static const struct bpf_func_proto bpf_tcp_sock_proto = { .arg1_type = ARG_PTR_TO_SOCK_COMMON, }; +BPF_CALL_1(bpf_tcp_enter_cwr, struct tcp_sock *, tp) +{ + tcp_enter_cwr((struct sock *)tp); + return 0; +} + +static const struct bpf_func_proto bpf_tcp_enter_cwr_proto = { + .func = bpf_tcp_enter_cwr, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_TCP_SOCK, +}; #endif /* CONFIG_INET */ bool bpf_helper_changes_pkt_data(void *func) @@ -5585,6 +5597,8 @@ cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) #ifdef CONFIG_INET case BPF_FUNC_tcp_sock: return &bpf_tcp_sock_proto; + case BPF_FUNC_tcp_enter_cwr: + return &bpf_tcp_enter_cwr_proto; #endif default: return sk_filter_func_proto(func_id, prog);