1 // Copyright 2012 Google, Inc. All rights reserved.
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
7 // This benchmark reads in file <tempdir>/gopacket_benchmark.pcap and measures
8 // the time it takes to decode all packets from that file. If the file doesn't
9 // exist, it's pulled down from a publicly available location. However, you can
10 // feel free to substitute your own file at that location, in which case the
11 // benchmark will run on your own data.
13 // It's also useful for figuring out which packets may be causing errors. Pass
14 // in the --printErrors flag, and it'll print out error layers for each packet
15 // that has them. This includes any packets that it's just unable to decode,
16 // which is a great way to find new protocols to decode, and get test packets to
17 // write tests for them.
25 "github.com/google/gopacket"
26 "github.com/google/gopacket/layers"
27 "github.com/google/gopacket/pcap"
28 "github.com/google/gopacket/tcpassembly"
38 var decodeLazy *bool = flag.Bool("lazy", false, "If true, use lazy decoding")
39 var decodeNoCopy *bool = flag.Bool("nocopy", true, "If true, avoid an extra copy when decoding packets")
40 var printErrors *bool = flag.Bool("printErrors", false, "If true, check for and print error layers.")
41 var printLayers *bool = flag.Bool("printLayers", false, "If true, print out the layers of each packet")
42 var repeat *int = flag.Int("repeat", 5, "Read over the file N times")
43 var cpuProfile *string = flag.String("cpuprofile", "", "If set, write CPU profile to filename")
44 var url *string = flag.String("url", "http://www.ll.mit.edu/mission/communications/cyber/CSTcorpora/ideval/data/1999/training/week1/tuesday/inside.tcpdump.gz", "URL to gzip'd pcap file")
46 type BufferPacketSource struct {
49 ci []gopacket.CaptureInfo
52 func NewBufferPacketSource(p gopacket.PacketDataSource) *BufferPacketSource {
54 b := &BufferPacketSource{}
56 data, ci, err := p.ReadPacketData()
60 b.data = append(b.data, data)
61 b.ci = append(b.ci, ci)
63 duration := time.Since(start)
64 fmt.Printf("Reading packet data into memory: %d packets in %v, %v per packet\n", len(b.data), duration, duration/time.Duration(len(b.data)))
68 func (b *BufferPacketSource) ReadPacketData() (data []byte, ci gopacket.CaptureInfo, err error) {
69 if b.index >= len(b.data) {
73 data = b.data[b.index]
79 func (b *BufferPacketSource) Reset() {
86 filename := os.TempDir() + string(os.PathSeparator) + "gopacket_benchmark.pcap"
87 if _, err := os.Stat(filename); err != nil {
88 // This URL points to a publicly available packet data set from a DARPA
89 // intrusion detection evaluation. See
90 // http://www.ll.mit.edu/mission/communications/cyber/CSTcorpora/ideval/data/1999/training/week1/index.html
92 fmt.Println("Local pcap file", filename, "doesn't exist, reading from", *url)
93 if resp, err := http.Get(*url); err != nil {
95 } else if out, err := os.Create(filename); err != nil {
97 } else if gz, err := gzip.NewReader(resp.Body); err != nil {
99 } else if n, err := io.Copy(out, gz); err != nil {
101 } else if err := gz.Close(); err != nil {
103 } else if err := out.Close(); err != nil {
106 fmt.Println("Successfully read", n, "bytes from url, unzipped to local storage")
109 fmt.Println("Reading file once through to hopefully cache most of it")
110 if f, err := os.Open(filename); err != nil {
112 } else if n, err := io.Copy(ioutil.Discard, f); err != nil {
114 } else if err := f.Close(); err != nil {
117 fmt.Println("Read in file", filename, ", total of", n, "bytes")
119 if *cpuProfile != "" {
120 if cpu, err := os.Create(*cpuProfile); err != nil {
122 } else if err := pprof.StartCPUProfile(cpu); err != nil {
126 pprof.StopCPUProfile()
131 var packetDataSource *BufferPacketSource
132 var packetSource *gopacket.PacketSource
133 fmt.Printf("Opening file %q for read\n", filename)
134 if h, err := pcap.OpenOffline(filename); err != nil {
137 fmt.Println("Reading all packets into memory with BufferPacketSource.")
139 packetDataSource = NewBufferPacketSource(h)
140 duration := time.Since(start)
141 fmt.Printf("Time to read packet data into memory from file: %v\n", duration)
142 packetSource = gopacket.NewPacketSource(packetDataSource, h.LinkType())
143 packetSource.DecodeOptions.Lazy = *decodeLazy
144 packetSource.DecodeOptions.NoCopy = *decodeNoCopy
147 for i := 0; i < *repeat; i++ {
148 packetDataSource.Reset()
149 fmt.Printf("Benchmarking decode %d/%d\n", i+1, *repeat)
150 benchmarkPacketDecode(packetSource)
153 for i := 0; i < *repeat; i++ {
154 packetDataSource.Reset()
155 fmt.Printf("Benchmarking decoding layer parser %d/%d\n", i+1, *repeat)
156 benchmarkLayerDecode(packetDataSource, false)
159 for i := 0; i < *repeat; i++ {
160 packetDataSource.Reset()
161 fmt.Printf("Benchmarking decoding layer parser with assembly %d/%d\n", i+1, *repeat)
162 benchmarkLayerDecode(packetDataSource, true)
166 func benchmarkPacketDecode(packetSource *gopacket.PacketSource) {
167 count, errors := 0, 0
169 for packet, err := packetSource.NextPacket(); err != io.EOF; packet, err = packetSource.NextPacket() {
171 fmt.Println("Error reading in packet:", err)
176 if *printErrors && packet.ErrorLayer() != nil {
177 fmt.Println("\n\n\nError decoding packet:", packet.ErrorLayer().Error())
178 fmt.Println(hex.Dump(packet.Data()))
179 fmt.Printf("%#v\n", packet.Data())
183 if *printLayers || hasError {
184 fmt.Printf("\n=== PACKET %d ===\n", count)
185 for _, l := range packet.Layers() {
186 fmt.Printf("--- LAYER %v ---\n%#v\n\n", l.LayerType(), l)
191 duration := time.Since(start)
192 fmt.Printf("\tRead in %v packets in %v, %v per packet\n", count, duration, duration/time.Duration(count))
194 fmt.Printf("%v errors, successfully decoded %.02f%%\n", errors, float64(count-errors)*100.0/float64(count))
198 type streamFactory struct {
201 func (s *streamFactory) New(netFlow, tcpFlow gopacket.Flow) tcpassembly.Stream {
204 func (s *streamFactory) Reassembled([]tcpassembly.Reassembly) {
206 func (s *streamFactory) ReassemblyComplete() {
209 func benchmarkLayerDecode(source *BufferPacketSource, assemble bool) {
212 var eth layers.Ethernet
214 var icmp layers.ICMPv4
215 var payload gopacket.Payload
216 parser := gopacket.NewDecodingLayerParser(
217 layers.LayerTypeEthernet,
218 ð, &ip, &icmp, &tcp, &udp, &payload)
219 pool := tcpassembly.NewStreamPool(&streamFactory{})
220 assembler := tcpassembly.NewAssembler(pool)
221 var decoded []gopacket.LayerType
223 packets, decodedlayers, assembled := 0, 0, 0
226 data, ci, err := source.ReadPacketData()
229 } else if err != nil {
230 fmt.Println("Error reading packet: ", err)
233 err = parser.DecodeLayers(data, &decoded)
234 for _, typ := range decoded {
236 if typ == layers.LayerTypeTCP && assemble {
238 assembler.AssembleWithTimestamp(ip.NetworkFlow(), &tcp, ci.Timestamp)
245 duration := time.Since(start)
246 fmt.Printf("\tRead in %d packets in %v, decoded %v layers, assembled %v packets: %v per packet\n", packets, duration, decodedlayers, assembled, duration/time.Duration(packets))