blob: 2c174fbedf9f12e3425264e41cb9a46a1a75d2fb [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 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070043#ifndef included_vnet_pcap_h
44#define included_vnet_pcap_h
45
46#include <vlib/vlib.h>
47
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070048/**
49 * @brief Packet types supported by PCAP
50 *
51 * null 0
52 * ethernet 1
53 * ppp 9
54 * ip 12
55 * hdlc 104
56 */
57#define foreach_vnet_pcap_packet_type \
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 _ (null, 0) \
59 _ (ethernet, 1) \
60 _ (ppp, 9) \
61 _ (ip, 12) \
62 _ (hdlc, 104)
63
sharath reddy1b0c9832017-11-29 20:08:11 +053064typedef enum
65{
Ed Warnickecb9cada2015-12-08 15:45:58 -070066#define _(f,n) PCAP_PACKET_TYPE_##f = (n),
67 foreach_vnet_pcap_packet_type
68#undef _
69} pcap_packet_type_t;
70
71#define foreach_pcap_file_header \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070072 /** 0xa1b2c3d4 host byte order. \
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 0xd4c3b2a1 => need to byte swap everything. */ \
74 _ (u32, magic) \
75 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070076 /** Currently major 2 minor 4. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 _ (u16, major_version) \
78 _ (u16, minor_version) \
79 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070080 /** 0 for GMT. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 _ (u32, time_zone) \
82 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070083 /** Accuracy of timestamps. Typically set to 0. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 _ (u32, sigfigs) \
85 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070086 /** Size of largest packet in file. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 _ (u32, max_packet_size_in_bytes) \
88 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070089 /** One of vnet_pcap_packet_type_t. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 _ (u32, packet_type)
91
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070092/** File header struct */
sharath reddy1b0c9832017-11-29 20:08:11 +053093typedef struct
94{
Ed Warnickecb9cada2015-12-08 15:45:58 -070095#define _(t, f) t f;
96 foreach_pcap_file_header
97#undef _
98} pcap_file_header_t;
99
100#define foreach_pcap_packet_header \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700101 /** Time stamp in seconds */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 _ (u32, time_in_sec) \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700103 /** Time stamp in microseconds. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 _ (u32, time_in_usec) \
105 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700106 /** Number of bytes stored in file. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 _ (u32, n_packet_bytes_stored_in_file) \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700108 /** Number of bytes in actual packet. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 _ (u32, n_bytes_in_packet)
110
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700111/** Packet header. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530112typedef struct
113{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114#define _(t, f) t f;
115 foreach_pcap_packet_header
116#undef _
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700117 /** Packet data follows. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 u8 data[0];
119} pcap_packet_header_t;
120
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700121/**
122 * @brief PCAP main state data structure
123 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530124typedef struct
125{
Dave Barachf91080c2018-07-26 12:27:27 -0400126 /** spinlock to protect e.g. pcap_data */
127 clib_spinlock_t lock;
128
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700129 /** File name of pcap output. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530130 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700132 /** Number of packets to capture. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 u32 n_packets_to_capture;
134
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700135 /** Packet type */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 pcap_packet_type_t packet_type;
137
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700138 /** Number of packets currently captured. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 u32 n_packets_captured;
140
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700141 /** flags */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 u32 flags;
143#define PCAP_MAIN_INIT_DONE (1 << 0)
144
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700145 /** File descriptor for reading/writing. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 int file_descriptor;
147
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700148 /** Bytes written */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 u32 n_pcap_data_written;
150
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700151 /** Vector of pcap data. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530152 u8 *pcap_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700154 /** Packets read from file. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530155 u8 **packets_read;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Dave Barach78c56892018-05-16 11:34:35 -0400157 /** Timestamps */
158 u64 *timestamps;
159
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700160 /** Min/Max Packet bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u32 min_packet_bytes, max_packet_bytes;
162} pcap_main_t;
163
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700164/** Write out data to output file. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530165clib_error_t *pcap_write (pcap_main_t * pm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700167/** Read data from file. */
sharath reddy1b0c9832017-11-29 20:08:11 +0530168clib_error_t *pcap_read (pcap_main_t * pm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700170/**
171 * @brief Add packet
172 *
173 * @param *pm - pcap_main_t
174 * @param time_now - f64
175 * @param n_bytes_in_trace - u32
176 * @param n_bytes_in_packet - u32
177 *
178 * @return Packet Data
179 *
180 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181static inline void *
182pcap_add_packet (pcap_main_t * pm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530183 f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184{
sharath reddy1b0c9832017-11-29 20:08:11 +0530185 pcap_packet_header_t *h;
186 u8 *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
189 h = (void *) (d);
190 h->time_in_sec = time_now;
sharath reddy1b0c9832017-11-29 20:08:11 +0530191 h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
193 h->n_bytes_in_packet = n_bytes_in_packet;
194 pm->n_packets_captured++;
195 return h->data;
196}
197
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700198/**
199 * @brief Add buffer (vlib_buffer_t) to the trace
200 *
201 * @param *pm - pcap_main_t
202 * @param *vm - vlib_main_t
203 * @param buffer_index - u32
204 * @param n_bytes_in_trace - u32
205 *
206 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207static inline void
208pcap_add_buffer (pcap_main_t * pm,
sharath reddy1b0c9832017-11-29 20:08:11 +0530209 vlib_main_t * vm, u32 buffer_index, u32 n_bytes_in_trace)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210{
sharath reddy1b0c9832017-11-29 20:08:11 +0530211 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 u32 n = vlib_buffer_length_in_chain (vm, b);
213 i32 n_left = clib_min (n_bytes_in_trace, n);
214 f64 time_now = vlib_time_now (vm);
sharath reddy1b0c9832017-11-29 20:08:11 +0530215 void *d;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Dave Barachc6f186b2018-05-25 17:36:05 -0400217 if (PREDICT_TRUE (pm->n_packets_captured < pm->n_packets_to_capture))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 {
Dave Barachf91080c2018-07-26 12:27:27 -0400219 clib_spinlock_lock_if_init (&pm->lock);
Dave Barachc6f186b2018-05-25 17:36:05 -0400220 d = pcap_add_packet (pm, time_now, n_left, n);
221 while (1)
222 {
223 u32 copy_length = clib_min ((u32) n_left, b->current_length);
224 clib_memcpy (d, b->data + b->current_data, copy_length);
225 n_left -= b->current_length;
226 if (n_left <= 0)
227 break;
228 d += b->current_length;
229 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
230 b = vlib_get_buffer (vm, b->next_buffer);
231 }
Dave Barachf91080c2018-07-26 12:27:27 -0400232 clib_spinlock_unlock_if_init (&pm->lock);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234}
235
236#endif /* included_vnet_pcap_h */
sharath reddy1b0c9832017-11-29 20:08:11 +0530237
238/*
239 * fd.io coding-style-patch-verification: ON
240 *
241 * Local Variables:
242 * eval: (c-set-style "gnu")
243 * End:
244 */