From nobody Wed Nov 5 16:43:36 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1535640889922310.03297267571736; Thu, 30 Aug 2018 07:54:49 -0700 (PDT) Received: from localhost ([::1]:49371 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvOLQ-0000e3-PD for importer@patchew.org; Thu, 30 Aug 2018 10:54:48 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33671) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fvOAP-0005sc-Ak for qemu-devel@nongnu.org; Thu, 30 Aug 2018 10:43:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fvNur-0003dc-67 for qemu-devel@nongnu.org; Thu, 30 Aug 2018 10:27:22 -0400 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:54505) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fvNup-0003bk-3x for qemu-devel@nongnu.org; Thu, 30 Aug 2018 10:27:20 -0400 Received: by mail-wm0-x242.google.com with SMTP id c14-v6so2210970wmb.4 for ; Thu, 30 Aug 2018 07:27:18 -0700 (PDT) Received: from bark.daynix ([141.226.163.173]) by smtp.gmail.com with ESMTPSA id t4-v6sm10483893wrb.45.2018.08.30.07.27.15 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 30 Aug 2018 07:27:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AGoanAXucW5UPvxe2b/l6nbTMS9BmIpRS5YUu0y2DFU=; b=j/2PUgUPxVRtq7gHIHUSmNGXWGHcj5xgcuuFdtJy/jKzC88V+Nlokq8XIcmkneFDOh xzjrqZN3pfCdmaKy5Kj+jvj4h9FnFqSvNsGmSbOflNwKnKk+j2ICHREvkoUbMpkWHUW2 7JpEnlz6BSEdDAvlLwruzIXXEoCTjpmJFRRGU5ZK2xrvA2k5erhXK/yNa537rsTfZxsU An4caJsTWrgR4gsGrcNIRT4CkBosk4miHEi93ypeehcJzdFFet7Hha6hLJnL86FCexlL lwbtvnlSasGcUqmtdAR2k5CCsb8HY0pTf73RxVmFQn66MCwXaHiQuUMecCmSGJhzPy5C BsrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AGoanAXucW5UPvxe2b/l6nbTMS9BmIpRS5YUu0y2DFU=; b=HwWP0WC742IvXZ3oDhgtYiFfFl0GsBnq16KI6SqNW3R+iTSDVMJfCK/Q2jN1tP8cfO PXrL/Lebl0r2245AajJ2Yl5zS6k2iH1b/PZ6gduz7GYjPMH2ttzn7Cn5PB20Vi0zymoT akL6PUFDG62mAJ96GxIg0Xk+e8oMSFwAnOBRfb3lqtQa0aYhtTGNimxGR/B3Unfxr5gE F5/MdPqrlfHWMwCvk2MFQku2PHRAecoAcka/y/d3974w28oo0NrQEwr1puBS6D7cAxYH +r5B/TJ3YnRm0CQqVe0CRqNmZSBwY0ojhzUQjmN03XnUPKtkdtnWmsnkmIAYoVE7C1Ns nkbQ== X-Gm-Message-State: APzg51BpmYqYJftx/JgCBNWQ4AeZHW3Aw4d5OnkmfQfhlJA/MvDJdGsR vTzIr6zSjJKHL7QF6WORBqkZFXNYTlQ= X-Google-Smtp-Source: ANB0VdZ7puMWF+bZgIdf94mffihxZMj3Kjj8sEjxATgsACww0mEwIGQ+Tu6IP+S1Pko0/vNs5dJspQ== X-Received: by 2002:a1c:68f:: with SMTP id 137-v6mr2083812wmg.82.1535639236721; Thu, 30 Aug 2018 07:27:16 -0700 (PDT) From: Sameeh Jubran To: qemu-devel@nongnu.org, Jason Wang Date: Thu, 30 Aug 2018 17:27:04 +0300 Message-Id: <20180830142708.14311-3-sameeh@daynix.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180830142708.14311-1-sameeh@daynix.com> References: <20180830142708.14311-1-sameeh@daynix.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [RFC 2/6] tap: Add support for bpf ioctls X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yan Vugenfirer Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Sameeh Jubran Starting from kernel v4.16 tun device supports TUNSETSTEERINGEBPF and TUNSETFILTEREBPF. Signed-off-by: Sameeh Jubran --- include/net/net.h | 3 ++- net/tap-bsd.c | 5 +++++ net/tap-linux.c | 29 ++++++++++++++++++++++++++++- net/tap-linux.h | 3 ++- net/tap-solaris.c | 5 +++++ net/tap-stub.c | 5 +++++ net/tap.c | 8 ++++++++ net/tap_int.h | 1 + qapi/net.json | 11 +++++++++++ 9 files changed, 67 insertions(+), 3 deletions(-) diff --git a/include/net/net.h b/include/net/net.h index 1425960f76..e7d1baac10 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -39,7 +39,6 @@ typedef struct NICConf { DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \ DEFINE_PROP_NETDEV("netdev", _state, _conf.peers) =20 - /* Net clients */ =20 typedef void (NetPoll)(NetClientState *, bool enable); @@ -60,6 +59,7 @@ typedef int (SetVnetLE)(NetClientState *, bool); typedef int (SetVnetBE)(NetClientState *, bool); typedef struct SocketReadState SocketReadState; typedef void (SocketReadStateFinalize)(SocketReadState *rs); +typedef int (SetBPFFilter)(NetClientState *, int, BPFType); =20 typedef struct NetClientInfo { NetClientDriver type; @@ -80,6 +80,7 @@ typedef struct NetClientInfo { SetVnetHdrLen *set_vnet_hdr_len; SetVnetLE *set_vnet_le; SetVnetBE *set_vnet_be; + SetBPFFilter *set_bpf_filter; } NetClientInfo; =20 struct NetClientState { diff --git a/net/tap-bsd.c b/net/tap-bsd.c index 6c9692263d..fccf17bad0 100644 --- a/net/tap-bsd.c +++ b/net/tap-bsd.c @@ -259,3 +259,8 @@ int tap_fd_get_ifname(int fd, char *ifname) { return -1; } + +int tap_fd_load_bpf(int fd, int bpf_fd, BPFType type) +{ + return -1; +} diff --git a/net/tap-linux.c b/net/tap-linux.c index 535b1ddb61..e8ee54f3b3 100644 --- a/net/tap-linux.c +++ b/net/tap-linux.c @@ -305,7 +305,8 @@ int tap_fd_get_ifname(int fd, char *ifname) { struct ifreq ifr; =20 - if (ioctl(fd, TUNGETIFF, &ifr) !=3D 0) { + if (ioctl(fd, TUNGETIFF, &ifr) !=3D 0) + { error_report("TUNGETIFF ioctl() failed: %s", strerror(errno)); return -1; @@ -314,3 +315,29 @@ int tap_fd_get_ifname(int fd, char *ifname) pstrcpy(ifname, sizeof(ifr.ifr_name), ifr.ifr_name); return 0; } + + +int tap_fd_load_bpf(int fd, int bpf_fd, BPFType type) +{ + int ioctl_num =3D 0; + switch (type) + { + case BPF_TYPE_FILTER: + ioctl_num =3D TUNSETFILTEREBPF; + break; + + case BPF_TYPE_STEERING: + ioctl_num =3D TUNSETSTEERINGEBPF; + break; + + default: + error_report("Unknown bpf_type"); + return -1; + } + + if (ioctl(fd, ioctl_num, &bpf_fd) !=3D 0) { + error_report("#%d ioctl() failed: %s", ioctl_num, strerror(errno)); + return -1; + } + return 0; +} diff --git a/net/tap-linux.h b/net/tap-linux.h index 2f36d100fc..7348169fc2 100644 --- a/net/tap-linux.h +++ b/net/tap-linux.h @@ -31,7 +31,8 @@ #define TUNSETQUEUE _IOW('T', 217, int) #define TUNSETVNETLE _IOW('T', 220, int) #define TUNSETVNETBE _IOW('T', 222, int) - +#define TUNSETSTEERINGEBPF _IOR('T', 224, int) +#define TUNSETFILTEREBPF _IOR('T', 225, int) #endif =20 /* TUNSETIFF ifr flags */ diff --git a/net/tap-solaris.c b/net/tap-solaris.c index a2a92356c1..a5a6248c7d 100644 --- a/net/tap-solaris.c +++ b/net/tap-solaris.c @@ -254,3 +254,8 @@ int tap_fd_get_ifname(int fd, char *ifname) { return -1; } + +int tap_fd_load_bpf(int fd, int bpf_fd, BPFType type) +{ + return -1; +} diff --git a/net/tap-stub.c b/net/tap-stub.c index a9ab8f8293..d059a32435 100644 --- a/net/tap-stub.c +++ b/net/tap-stub.c @@ -85,3 +85,8 @@ int tap_fd_get_ifname(int fd, char *ifname) { return -1; } + +int tap_fd_load_bpf(int fd, int bpf_fd, BPFType type) +{ + return -1; +} diff --git a/net/tap.c b/net/tap.c index 2126f4882d..ee98fecd40 100644 --- a/net/tap.c +++ b/net/tap.c @@ -342,6 +342,13 @@ int tap_get_fd(NetClientState *nc) return s->fd; } =20 +static int tap_set_bpf_filter(NetClientState *nc, int bpf_fd, BPFType type) +{ + TAPState *s =3D DO_UPCAST(TAPState, nc, nc); + assert(nc->info->type =3D=3D NET_CLIENT_DRIVER_TAP); + return tap_fd_load_bpf(s->fd, bpf_fd, type); +} + /* fd support */ =20 static NetClientInfo net_tap_info =3D { @@ -360,6 +367,7 @@ static NetClientInfo net_tap_info =3D { .set_vnet_hdr_len =3D tap_set_vnet_hdr_len, .set_vnet_le =3D tap_set_vnet_le, .set_vnet_be =3D tap_set_vnet_be, + .set_bpf_filter =3D tap_set_bpf_filter, }; =20 static TAPState *net_tap_fd_init(NetClientState *peer, diff --git a/net/tap_int.h b/net/tap_int.h index 9f931d52d6..3e1603a88e 100644 --- a/net/tap_int.h +++ b/net/tap_int.h @@ -45,5 +45,6 @@ int tap_fd_set_vnet_be(int fd, int vnet_is_be); int tap_fd_enable(int fd); int tap_fd_disable(int fd); int tap_fd_get_ifname(int fd, char *ifname); +int tap_fd_load_bpf(int fd, int bpf_fd, BPFType type); =20 #endif /* NET_TAP_INT_H */ diff --git a/qapi/net.json b/qapi/net.json index 6b7d93cb59..ce0a688444 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -692,3 +692,14 @@ ## { 'event': 'NIC_RX_FILTER_CHANGED', 'data': { '*name': 'str', 'path': 'str' } } + +## +# @BPFType: +# +# BPF programs types provided as an argument for tap bpf ioctls +# +# Since: 2.12 +# +## +{ 'enum': 'BPFType', + 'data': [ 'filter', 'steering' ] } --=20 2.13.6