2 * Copyright (c) 2018 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.
18 * @brief MPCAP utility definitions
20 #ifndef included_vnet_mpcap_h
21 #define included_vnet_mpcap_h
23 #include <vlib/vlib.h>
24 #include <sys/types.h>
27 #include <vppinfra/time_range.h>
30 * @brief Packet types supported by MPCAP
38 #define foreach_vnet_mpcap_packet_type \
47 #define _(f,n) MPCAP_PACKET_TYPE_##f = (n),
48 foreach_vnet_mpcap_packet_type
50 } mpcap_packet_type_t;
52 #define foreach_mpcap_file_header \
53 /** 0xa1b2c3d4 host byte order. \
54 0xd4c3b2a1 => need to byte swap everything. */ \
57 /** Currently major 2 minor 4. */ \
58 _ (u16, major_version) \
59 _ (u16, minor_version) \
64 /** Accuracy of timestamps. Typically set to 0. */ \
67 /** Size of largest packet in file. */ \
68 _ (u32, max_packet_size_in_bytes) \
70 /** One of vnet_mpcap_packet_type_t. */ \
73 /** File header struct */
77 foreach_mpcap_file_header
79 } mpcap_file_header_t;
81 #define foreach_mpcap_packet_header \
82 /** Time stamp in seconds */ \
83 _ (u32, time_in_sec) \
84 /** Time stamp in microseconds. */ \
85 _ (u32, time_in_usec) \
87 /** Number of bytes stored in file. */ \
88 _ (u32, n_packet_bytes_stored_in_file) \
89 /** Number of bytes in actual packet. */ \
90 _ (u32, n_bytes_in_packet)
96 foreach_mpcap_packet_header
98 /** Packet data follows. */
100 } mpcap_packet_header_t;
103 * @brief MPCAP main state data structure
107 /** File name of mpcap output. */
110 /** spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE */
111 clib_spinlock_t lock;
113 /** Number of packets to capture. */
114 u32 n_packets_to_capture;
117 mpcap_packet_type_t packet_type;
119 /** Maximum file size */
125 /** current memory address */
128 /** Number of packets currently captured. */
129 u32 n_packets_captured;
131 /** Pointer to file header in svm, for ease of updating */
132 mpcap_file_header_t *file_header;
136 #define MPCAP_FLAG_INIT_DONE (1 << 0)
137 #define MPCAP_FLAG_THREAD_SAFE (1 << 1)
138 #define MPCAP_FLAG_WRITE_ENABLE (1 << 2)
141 u32 n_mpcap_data_written;
143 /** Vector of mpcap data. */
146 /** Packets in mapped mpcap file. */
149 /** Min/Max Packet bytes */
150 u32 min_packet_bytes, max_packet_bytes;
153 /* Some sensible default size */
154 #define MPCAP_DEFAULT_FILE_SIZE (10<<20)
156 /** initialize a mpcap file (for writing) */
157 clib_error_t *mpcap_init (mpcap_main_t * pm);
159 /** Flush / unmap a mpcap file */
160 clib_error_t *mpcap_close (mpcap_main_t * pm);
162 /** mmap a mpcap data file. */
163 clib_error_t *mpcap_map (mpcap_main_t * pm);
168 * @param *pm - mpcap_main_t
169 * @param time_now - f64
170 * @param n_bytes_in_trace - u32
171 * @param n_bytes_in_packet - u32
173 * @return Packet Data
177 mpcap_add_packet (mpcap_main_t * pm,
178 f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
180 mpcap_packet_header_t *h;
183 /* File already closed? */
184 if (PREDICT_FALSE (pm->flags & MPCAP_FLAG_INIT_DONE) == 0)
188 pm->current_va += sizeof (h[0]) + n_bytes_in_trace;
191 if (PREDICT_FALSE (pm->current_va >= pm->file_baseva + pm->max_file_size))
194 h->time_in_sec = time_now;
195 h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
196 h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
197 h->n_bytes_in_packet = n_bytes_in_packet;
198 pm->n_packets_captured++;
203 * @brief Add buffer (vlib_buffer_t) to the trace
205 * @param *pm - mpcap_main_t
206 * @param *vm - vlib_main_t
207 * @param time_now - f64
208 * @param buffer_index - u32
209 * @param n_bytes_in_trace - u32
213 mpcap_add_buffer (mpcap_main_t * pm,
215 f64 time_now, u32 buffer_index, u32 n_bytes_in_trace)
217 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
218 u32 n = vlib_buffer_length_in_chain (vm, b);
219 i32 n_left = clib_min (n_bytes_in_trace, n);
222 clib_spinlock_lock_if_init (&pm->lock);
224 d = mpcap_add_packet (pm, time_now, n_left, n);
225 if (PREDICT_FALSE (d == 0))
228 clib_spinlock_unlock_if_init (&pm->lock);
234 u32 copy_length = clib_min ((u32) n_left, b->current_length);
235 clib_memcpy (d, b->data + b->current_data, copy_length);
236 n_left -= b->current_length;
239 d += b->current_length;
240 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
241 b = vlib_get_buffer (vm, b->next_buffer);
243 if (pm->n_packets_captured >= pm->n_packets_to_capture)
246 clib_spinlock_unlock_if_init (&pm->lock);
249 #endif /* included_vnet_mpcap_h */
252 * fd.io coding-style-patch-verification: ON
255 * eval: (c-set-style "gnu")