Berkeley Packet Filter (BPF) zero copy buffer

The Berkeley Packet Filter or BPF provides, on some Unix-like systems, a raw interface to data link layers, permitting raw link-layer packets to be sent and received. In addition, if the driver for the network interface supports promiscuous mode, it allows the interface to be put into that mode, so that all packets on the network, even those destined for other hosts, can be received. 

In addition, it supports "filtering" packets, so that only "interesting" packets will be supplied to the software using BPF; this can avoid copying "uninteresting" packets from the operating system kernel to software running in user mode, reducing the CPU requirement to capture packets and the buffer space required to avoid dropping packets. BPF's filtering capabilities are implemented as an interpreter for a machine language for the BPF virtual machine; programs in that language can fetch data from the packet, perform arithmetic operations on data from the packet, and compare the results against constants or against data in the packet or test bits in the results, accepting or rejecting the packet based on the results of those tests. On some platforms, including FreeBSD andWinPcapjust-in-time compilation is used to convert virtual machine instructions into native code in order to further avoid overhead.

Kernel-mode interpreters for that same virtual machine language are used in raw data link layer mechanisms in other operating systems, such as Tru64 Unix, and for socket filters in Linux and in the WinPcap packet capture mechanism.

A user-mode interpreter for it is provided with the libpcap/WinPcap implementation of the pcap application programming interface (API), so that, when capturing packets on systems without kernel-mode support for that filtering mechanism, packets can be filtered in user mode; code using the pcap API will work on both types of systems, although, on systems where the filtering is done in user mode, all packets, including those that will be filtered out, are copied from the kernel to user space. That interpreter can also be used when reading a file containing packets captured using pcap.

BPF is sometimes used to refer just to the filtering mechanism, rather than to the entire interface.

BSD kernels implement routines such as bpf_mtap() and bpf_tap(), with some wrapping them in macros such as BPF_MTAP() and BPF_TAP(), which are called by network interface drivers (and pseudo-drivers) to deliver incoming and outgoing packets to the BPF mechanism.


 


 


 


 


 


 


 


 

[参考]

1. http://www.watson.org/~robert/freebsd/2007asiabsdcon/20070309-devsummit-zerocopybpf.pdf

2. http://en.wikipedia.org/wiki/Berkeley_Packet_Filter

时间: 2024-08-18 14:30:28

Berkeley Packet Filter (BPF) zero copy buffer的相关文章

Linux中的DTrace:BPF进入4.9内核

随着 BPF 追踪系统(基于时间采样)最后一个主要功能被合并至 Linux 4.9-rc1 版本的内核中,现在 Linux 内核拥有类似 DTrace 的原生追踪功能.DTrace 是 Solaris 系统中的高级追踪器.对于长期使用 DTrace 的用户和专家,这将是一个振奋人心的里程碑!现在在 Linux 系统上,你可以在生产环境中使用安全的.低负载的定制追踪系统,通过执行时间的柱状图和频率统计等信息,分析应用的性能以及内核. 用于 Linux 的追踪项目有很多,但是这个最终被合并进 Lin

Linux 中的 DTrace :BPF 进入 4.9 内核

随着 BPF 追踪系统(基于时间采样)最后一个主要功能被合并至 Linux 4.9-rc1 版本的内核中,现在 Linux 内核拥有类似 DTrace 的原生追踪功能.DTrace 是 Solaris 系统中的高级追踪器.对于长期使用 DTrace 的用户和专家,这将是一个振奋人心的里程碑!现在在 Linux 系统上,你可以在生产环境中使用安全的.低负载的定制追踪系统,通过执行时间的柱状图和频率统计等信息,分析应用的性能以及内核. 用于 Linux 的追踪项目有很多,但是这个最终被合并进 Lin

阿里内核月报2014年4月

Ideas for supporting shingled magnetic recording (SMR) 在2014年LSF&MM峰会上,Dave Chinner和Ted Ts'o一起主持了一个跨越2天而占用2个时间段的会议. 这个会议的主题是,是文件系统还是块设备层才是支持SMR设备的正确接口.最后,它的讨论范围 有点超出这个主题. Zone信息接口和缓存 Ts'o一开始就描叙了他正在处理的一个SMR设备返回的Zone信息kernel级别的C接口.SMR设备将会 报告驱动器中存在的Zone

阿里内核月报2014年7月-8月

Capsicum for Linux Capsicum: 一种基于文件句柄的新安全模型 Capsicum是一种源自FreeBSD的安全模型,与Linux下众多LSM的相同之处在于它们都是基于权限管理的,而不同之处在于LSM针对的操作对象非常丰富,有进程.VMA.端口.带有标签的文件等等,而Capsicum操作的对象非常单一:文件句柄.例如,一个fd必须带有CAP_READ才能被读取,必须带有CAP_SEEK才能被lseek(),必须带有CAP_MMAP_W才能被mmap()建立可写映射,针对io

阿里内核月报2014年12月

Live kernel patching on track for 3.20 运行时间敏感的用户一直以来都很希望有一种方法可以在不重启系统的情况下对运行的操作系统内核打补丁.目前有几个还没有进入主线内核的实现(比如:kpatch, Kgraft).很明显,这些实现不可能全部进入upstream内核.从最近Jiri Kosina发出的一组补丁来看, 内核热升级补丁的开发似乎又向前迈进了一步,kpatch和kGraft的开发,并且内核热升级的核心功能已经被Reviewed/Acked过.并且双方都同

采用FreeBSD IPSec Tunnel方式连接两个局域网络

在实际的工作中,经常遇到需要将处在不同地点的两个局域网络连接起来的要求.采用FreeBSD IPSec Tunnel方式可以方便的将两个局域网络连接起来,且具有很好的安全性.下面将用一个案例来讲述如何采 用这种方式连接两个局域网络. 假设有如下的网络结构: 两台FreeBSD机器都分别配置为两个子网的防火墙,并对内核配置增加IPSec支持,相关的内核配置参 数如下: # IP security (crypto; define w/ IPSEC) options IPSEC options IPS

FreeBSD服务器的安装与优化(下)

"简单的东西不容易出问题." 至理名言!事实上,削减操作系统中那些对你没用的功能是一件非常重要的事情.这不仅仅意味着一个更小的操作系统,占用更少的磁盘空间(现在的硬盘少说也有几十GB,几兆的空间根本就是九牛一毛),而且意味着你被攻击的可能性也更低--简而言之,你不需要担心操作系统中没有安装的模块存在问题会对你造成影响. 这篇文章中,我们将一起对FreeBSD-i386的内核进行优化配置.这篇文章是针对FreeBSD4.7-STABLE写的,并且,FreeBSD 4.x的配置不会和这篇文

Linux系统中字符串搜索命令ngrep的用法

  ngrep 是grep的网络版,他力求更多的grep特征,用于搜寻指定的数据包.由于安装ngrep需用到ibpcap库, 所以支持大量的操作系统和网络协议,能识别TCP.UDP和ICMP包. 安装ngrep 下载地址 代码如下: git clone git://git.code.sf.net/p/ngrep/code ngrep-code 进入目录 代码如下: cd ngrep-code ./configure --with-pcap-includes=/usr/local/include/

如何选择 Linux 上的跟踪器

跟踪器tracer是一个高级的性能分析和诊断工具,但是不要让这名词唬住你,如果你使用过 strace 和 tcpdump,其实你就已经使用过跟踪器了.系统跟踪器可以获取更多的系统调用和数据包.它们通常能跟踪任意的内核和应用程序. 有太多的 Linux 跟踪器可以选择.每一种都有其官方的(或非官方的)的卡通的独角兽吉祥物,足够撑起一台"儿童剧"了. 那么我们应该使用哪个跟踪器呢? 我会为两类读者回答这个问题,多数人和性能/内核工程师.过一段时间这些可能会发生变化,我会持续跟进并补充,大概