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 #ifndef included_vppinfra_pcap_funcs_h
17 #define included_vppinfra_pcap_funcs_h
19 /** Write out data to output file. */
20 clib_error_t *pcap_write (pcap_main_t * pm);
22 /** Read data from file. */
23 clib_error_t *pcap_read (pcap_main_t * pm);
28 * @param *pm - pcap_main_t
29 * @param time_now - f64
30 * @param n_bytes_in_trace - u32
31 * @param n_bytes_in_packet - u32
37 pcap_add_packet (pcap_main_t * pm,
38 f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
40 pcap_packet_header_t *h;
43 vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
45 h->time_in_sec = time_now;
46 h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
47 h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
48 h->n_bytes_in_packet = n_bytes_in_packet;
49 pm->n_packets_captured++;
54 * @brief Add buffer (vlib_buffer_t) to the trace
56 * @param *pm - pcap_main_t
57 * @param *vm - vlib_main_t
58 * @param buffer_index - u32
59 * @param n_bytes_in_trace - u32
63 pcap_add_buffer (pcap_main_t * pm,
64 struct vlib_main_t *vm, u32 buffer_index,
67 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
68 u32 n = vlib_buffer_length_in_chain (vm, b);
69 i32 n_left = clib_min (n_bytes_in_trace, n);
70 f64 time_now = vlib_time_now (vm);
73 if (PREDICT_TRUE (pm->n_packets_captured < pm->n_packets_to_capture))
75 clib_spinlock_lock_if_init (&pm->lock);
76 d = pcap_add_packet (pm, time_now, n_left, n);
79 u32 copy_length = clib_min ((u32) n_left, b->current_length);
80 clib_memcpy_fast (d, b->data + b->current_data, copy_length);
81 n_left -= b->current_length;
84 d += b->current_length;
85 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
86 b = vlib_get_buffer (vm, b->next_buffer);
88 clib_spinlock_unlock_if_init (&pm->lock);
92 #endif /* included_vppinfra_pcap_funcs_h */
95 * fd.io coding-style-patch-verification: ON
98 * eval: (c-set-style "gnu")