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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * pcap.h: libpcap packet capture format
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
41 * @brief PCAP utility definitions
43 #ifndef included_vnet_pcap_h
44 #define included_vnet_pcap_h
46 #include <vlib/vlib.h>
49 * @brief Packet types supported by PCAP
57 #define foreach_vnet_pcap_packet_type \
66 #define _(f,n) PCAP_PACKET_TYPE_##f = (n),
67 foreach_vnet_pcap_packet_type
71 #define foreach_pcap_file_header \
72 /** 0xa1b2c3d4 host byte order. \
73 0xd4c3b2a1 => need to byte swap everything. */ \
76 /** Currently major 2 minor 4. */ \
77 _ (u16, major_version) \
78 _ (u16, minor_version) \
83 /** Accuracy of timestamps. Typically set to 0. */ \
86 /** Size of largest packet in file. */ \
87 _ (u32, max_packet_size_in_bytes) \
89 /** One of vnet_pcap_packet_type_t. */ \
92 /** File header struct */
96 foreach_pcap_file_header
100 #define foreach_pcap_packet_header \
101 /** Time stamp in seconds */ \
102 _ (u32, time_in_sec) \
103 /** Time stamp in microseconds. */ \
104 _ (u32, time_in_usec) \
106 /** Number of bytes stored in file. */ \
107 _ (u32, n_packet_bytes_stored_in_file) \
108 /** Number of bytes in actual packet. */ \
109 _ (u32, n_bytes_in_packet)
111 /** Packet header. */
115 foreach_pcap_packet_header
117 /** Packet data follows. */
119 } pcap_packet_header_t;
122 * @brief PCAP main state data structure
126 /** spinlock to protect e.g. pcap_data */
127 clib_spinlock_t lock;
129 /** File name of pcap output. */
132 /** Number of packets to capture. */
133 u32 n_packets_to_capture;
136 pcap_packet_type_t packet_type;
138 /** Number of packets currently captured. */
139 u32 n_packets_captured;
143 #define PCAP_MAIN_INIT_DONE (1 << 0)
145 /** File descriptor for reading/writing. */
149 u32 n_pcap_data_written;
151 /** Vector of pcap data. */
154 /** Packets read from file. */
160 /** Min/Max Packet bytes */
161 u32 min_packet_bytes, max_packet_bytes;
164 /** Write out data to output file. */
165 clib_error_t *pcap_write (pcap_main_t * pm);
167 /** Read data from file. */
168 clib_error_t *pcap_read (pcap_main_t * pm);
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
178 * @return Packet Data
182 pcap_add_packet (pcap_main_t * pm,
183 f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
185 pcap_packet_header_t *h;
188 vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
190 h->time_in_sec = time_now;
191 h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
192 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++;
199 * @brief Add buffer (vlib_buffer_t) to the trace
201 * @param *pm - pcap_main_t
202 * @param *vm - vlib_main_t
203 * @param buffer_index - u32
204 * @param n_bytes_in_trace - u32
208 pcap_add_buffer (pcap_main_t * pm,
209 vlib_main_t * vm, u32 buffer_index, u32 n_bytes_in_trace)
211 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
212 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);
217 if (PREDICT_TRUE (pm->n_packets_captured < pm->n_packets_to_capture))
219 clib_spinlock_lock_if_init (&pm->lock);
220 d = pcap_add_packet (pm, time_now, n_left, n);
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;
228 d += b->current_length;
229 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
230 b = vlib_get_buffer (vm, b->next_buffer);
232 clib_spinlock_unlock_if_init (&pm->lock);
236 #endif /* included_vnet_pcap_h */
239 * fd.io coding-style-patch-verification: ON
242 * eval: (c-set-style "gnu")