blob: 6aaf32bef7e10db79ee230280d967635354158c3 [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
64typedef enum {
65#define _(f,n) PCAP_PACKET_TYPE_##f = (n),
66 foreach_vnet_pcap_packet_type
67#undef _
68} pcap_packet_type_t;
69
70#define foreach_pcap_file_header \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070071 /** 0xa1b2c3d4 host byte order. \
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 0xd4c3b2a1 => need to byte swap everything. */ \
73 _ (u32, magic) \
74 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070075 /** Currently major 2 minor 4. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 _ (u16, major_version) \
77 _ (u16, minor_version) \
78 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070079 /** 0 for GMT. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 _ (u32, time_zone) \
81 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070082 /** Accuracy of timestamps. Typically set to 0. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 _ (u32, sigfigs) \
84 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070085 /** Size of largest packet in file. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 _ (u32, max_packet_size_in_bytes) \
87 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070088 /** One of vnet_pcap_packet_type_t. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 _ (u32, packet_type)
90
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070091/** File header struct */
Ed Warnickecb9cada2015-12-08 15:45:58 -070092typedef struct {
93#define _(t, f) t f;
94 foreach_pcap_file_header
95#undef _
96} pcap_file_header_t;
97
98#define foreach_pcap_packet_header \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070099 /** Time stamp in seconds */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 _ (u32, time_in_sec) \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700101 /** Time stamp in microseconds. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102 _ (u32, time_in_usec) \
103 \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700104 /** Number of bytes stored in file. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 _ (u32, n_packet_bytes_stored_in_file) \
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700106 /** Number of bytes in actual packet. */ \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 _ (u32, n_bytes_in_packet)
108
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700109/** Packet header. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110typedef struct {
111#define _(t, f) t f;
112 foreach_pcap_packet_header
113#undef _
114
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700115 /** Packet data follows. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 u8 data[0];
117} pcap_packet_header_t;
118
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700119/**
120 * @brief PCAP main state data structure
121 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122typedef struct {
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700123 /** File name of pcap output. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 char * file_name;
125
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700126 /** Number of packets to capture. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 u32 n_packets_to_capture;
128
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700129 /** Packet type */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 pcap_packet_type_t packet_type;
131
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700132 /** Number of packets currently captured. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 u32 n_packets_captured;
134
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700135 /** flags */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 u32 flags;
137#define PCAP_MAIN_INIT_DONE (1 << 0)
138
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700139 /** File descriptor for reading/writing. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140 int file_descriptor;
141
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700142 /** Bytes written */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 u32 n_pcap_data_written;
144
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700145 /** Vector of pcap data. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 u8 * pcap_data;
147
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700148 /** Packets read from file. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 u8 ** packets_read;
150
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700151 /** Min/Max Packet bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 u32 min_packet_bytes, max_packet_bytes;
153} pcap_main_t;
154
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700155/** Write out data to output file. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156clib_error_t * pcap_write (pcap_main_t * pm);
157
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700158/** Read data from file. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159clib_error_t * pcap_read (pcap_main_t * pm);
160
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700161/**
162 * @brief Add packet
163 *
164 * @param *pm - pcap_main_t
165 * @param time_now - f64
166 * @param n_bytes_in_trace - u32
167 * @param n_bytes_in_packet - u32
168 *
169 * @return Packet Data
170 *
171 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172static inline void *
173pcap_add_packet (pcap_main_t * pm,
174 f64 time_now,
175 u32 n_bytes_in_trace,
176 u32 n_bytes_in_packet)
177{
178 pcap_packet_header_t * h;
179 u8 * d;
180
181 vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
182 h = (void *) (d);
183 h->time_in_sec = time_now;
184 h->time_in_usec = 1e6*(time_now - h->time_in_sec);
185 h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
186 h->n_bytes_in_packet = n_bytes_in_packet;
187 pm->n_packets_captured++;
188 return h->data;
189}
190
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700191/**
192 * @brief Add buffer (vlib_buffer_t) to the trace
193 *
194 * @param *pm - pcap_main_t
195 * @param *vm - vlib_main_t
196 * @param buffer_index - u32
197 * @param n_bytes_in_trace - u32
198 *
199 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200static inline void
201pcap_add_buffer (pcap_main_t * pm,
202 vlib_main_t * vm, u32 buffer_index,
203 u32 n_bytes_in_trace)
204{
205 vlib_buffer_t * b = vlib_get_buffer (vm, buffer_index);
206 u32 n = vlib_buffer_length_in_chain (vm, b);
207 i32 n_left = clib_min (n_bytes_in_trace, n);
208 f64 time_now = vlib_time_now (vm);
209 void * d;
210
Damjan Marion92217f32016-07-12 21:58:19 +0200211 d = pcap_add_packet (pm, time_now, n_left, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 while (1)
213 {
John Lo644ec432016-04-27 09:56:36 -0400214 u32 copy_length = clib_min ((u32) n_left, b->current_length);
215 clib_memcpy (d, b->data + b->current_data, copy_length);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216 n_left -= b->current_length;
217 if (n_left <= 0)
218 break;
219 d += b->current_length;
220 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
221 b = vlib_get_buffer (vm, b->next_buffer);
222 }
223
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700224 /** Flush output vector. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 if (vec_len (pm->pcap_data) >= 64*1024
226 || pm->n_packets_captured >= pm->n_packets_to_capture)
227 pcap_write (pm);
228}
229
230#endif /* included_vnet_pcap_h */