blob: c3e1fef71f3e03ef7609d0495410d8557c3994ea [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * pcap.h: libpcap packet capture format
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070039/**
40 * @file
41 * @brief PCAP utility definitions
42 */
Dave Barach3ae28732018-11-16 17:19:00 -050043#ifndef included_vppinfra_pcap_h
44#define included_vppinfra_pcap_h
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
Dave Barach3ae28732018-11-16 17:19:00 -050046#include <vppinfra/types.h>
47#include <vppinfra/cache.h>
48#include <vppinfra/mem.h>
49#include <vppinfra/lock.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070051/**
Dave Barach7b01e9e2019-01-09 10:22:24 -050052 * @brief Known libpcap encap types
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070053 *
Dave Barach3ae28732018-11-16 17:19:00 -050054 * These codes end up in the pcap file header.
55 * If you decide to build a wireshark dissector,
56 * you'll need to know that these codes are mapped
57 * through the pcap_to_wtap_map[] array in .../wiretap/pcap-common.c.
58 *
59 * For example:
60 *
Dave Barach7b01e9e2019-01-09 10:22:24 -050061 * { 280, WTAP_ENCAP_VPP },
Dave Barach3ae28732018-11-16 17:19:00 -050062 *
Dave Barach7b01e9e2019-01-09 10:22:24 -050063 * A file with the officially-allocated vpp packet type PCAP_PACKET_TYPE_vpp
64 * aka 280, will need a top-level dissector registered to
65 * deal with WTAP_ENCAP_VPP [=206].
Dave Barach3ae28732018-11-16 17:19:00 -050066 *
67 * Something like so:
68 *
Dave Barach7b01e9e2019-01-09 10:22:24 -050069 * dissector_add_uint("wtap_encap", WTAP_ENCAP_VPP, vpp_dissector_handle);
Dave Barach3ae28732018-11-16 17:19:00 -050070 *
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070071 */
72#define foreach_vnet_pcap_packet_type \
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 _ (null, 0) \
74 _ (ethernet, 1) \
75 _ (ppp, 9) \
76 _ (ip, 12) \
Dave Barach3ae28732018-11-16 17:19:00 -050077 _ (hdlc, 104) \
78 _ (user0, 147) \
79 _ (user1, 148) \
80 _ (user2, 149) \
81 _ (user3, 150) \
82 _ (user4, 151) \
83 _ (user5, 152) \
84 _ (user6, 153) \
85 _ (user7, 154) \
86 _ (user8, 155) \
87 _ (user9, 156) \
88 _ (user10, 157) \
89 _ (user11, 158) \
90 _ (user12, 159) \
91 _ (user13, 160) \
92 _ (user14, 161) \
Dave Barach7b01e9e2019-01-09 10:22:24 -050093 _ (user15, 162) \
94 _ (vpp, 280) \
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
sharath reddy1b0c9832017-11-29 20:08:11 +053096typedef enum
97{
Ed Warnickecb9cada2015-12-08 15:45:58 -070098#define _(f,n) PCAP_PACKET_TYPE_##f = (n),
99 foreach_vnet_pcap_packet_type
100#undef _
101} pcap_packet_type_t;
102
103#define foreach_pcap_file_header \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700104 /** 0xa1b2c3d4 host byte order. \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 0xd4c3b2a1 => need to byte swap everything. */ \
106 _ (u32, magic) \
107 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700108 /** Currently major 2 minor 4. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 _ (u16, major_version) \
110 _ (u16, minor_version) \
111 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700112 /** 0 for GMT. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 _ (u32, time_zone) \
114 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700115 /** Accuracy of timestamps. Typically set to 0. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 _ (u32, sigfigs) \
117 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700118 /** Size of largest packet in file. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 _ (u32, max_packet_size_in_bytes) \
120 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700121 /** One of vnet_pcap_packet_type_t. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 _ (u32, packet_type)
123
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700124/** File header struct */
sharath reddy1b0c9832017-11-29 20:08:11 +0530125typedef struct
126{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127#define _(t, f) t f;
128 foreach_pcap_file_header
129#undef _
130} pcap_file_header_t;
131
132#define foreach_pcap_packet_header \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700133 /** Time stamp in seconds */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 _ (u32, time_in_sec) \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700135 /** Time stamp in microseconds. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 _ (u32, time_in_usec) \
137 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700138 /** Number of bytes stored in file. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 _ (u32, n_packet_bytes_stored_in_file) \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700140 /** Number of bytes in actual packet. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 _ (u32, n_bytes_in_packet)
142
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700143/** Packet header. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530144typedef struct
145{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146#define _(t, f) t f;
147 foreach_pcap_packet_header
148#undef _
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700149 /** Packet data follows. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 u8 data[0];
151} pcap_packet_header_t;
152
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700153/**
154 * @brief PCAP main state data structure
155 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530156typedef struct
157{
Dave Barachf91080c2018-07-26 12:27:27 -0400158 /** spinlock to protect e.g. pcap_data */
159 clib_spinlock_t lock;
160
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700161 /** File name of pcap output. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530162 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700164 /** Number of packets to capture. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 u32 n_packets_to_capture;
166
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700167 /** Packet type */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 pcap_packet_type_t packet_type;
169
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700170 /** Number of packets currently captured. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 u32 n_packets_captured;
172
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700173 /** flags */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 u32 flags;
175#define PCAP_MAIN_INIT_DONE (1 << 0)
176
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700177 /** File descriptor for reading/writing. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 int file_descriptor;
179
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700180 /** Bytes written */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 u32 n_pcap_data_written;
182
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700183 /** Vector of pcap data. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530184 u8 *pcap_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700186 /** Packets read from file. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530187 u8 **packets_read;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Dave Barach78c56892018-05-16 11:34:35 -0400189 /** Timestamps */
190 u64 *timestamps;
191
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700192 /** Min/Max Packet bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 u32 min_packet_bytes, max_packet_bytes;
194} pcap_main_t;
195
John Lo778ce572019-08-01 15:15:36 -0400196#define PCAP_DEF_PKT_TO_CAPTURE (100)
197
Dave Barach3ae28732018-11-16 17:19:00 -0500198#endif /* included_vppinfra_pcap_h */
sharath reddy1b0c9832017-11-29 20:08:11 +0530199
200/*
201 * fd.io coding-style-patch-verification: ON
202 *
203 * Local Variables:
204 * eval: (c-set-style "gnu")
205 * End:
206 */