blob: a3a3072f9557a771d569badbe26423f0eeb5f2e8 [file] [log] [blame]
Dave Barach3ae28732018-11-16 17:19:00 -05001/*
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#ifndef included_vppinfra_pcap_funcs_h
17#define included_vppinfra_pcap_funcs_h
18
19/** Write out data to output file. */
20clib_error_t *pcap_write (pcap_main_t * pm);
21
22/** Read data from file. */
23clib_error_t *pcap_read (pcap_main_t * pm);
24
Jack Xu9af7e2e2019-03-27 11:51:32 -040025/** Close the file created by pcap_write function. */
26clib_error_t *pcap_close (pcap_main_t * pm);
27
Dave Barach3ae28732018-11-16 17:19:00 -050028/**
29 * @brief Add packet
30 *
31 * @param *pm - pcap_main_t
32 * @param time_now - f64
33 * @param n_bytes_in_trace - u32
34 * @param n_bytes_in_packet - u32
35 *
36 * @return Packet Data
37 *
38 */
39static inline void *
40pcap_add_packet (pcap_main_t * pm,
41 f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
42{
43 pcap_packet_header_t *h;
44 u8 *d;
45
46 vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
47 h = (void *) (d);
48 h->time_in_sec = time_now;
49 h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
50 h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
51 h->n_bytes_in_packet = n_bytes_in_packet;
52 pm->n_packets_captured++;
53 return h->data;
54}
55
56/**
57 * @brief Add buffer (vlib_buffer_t) to the trace
58 *
59 * @param *pm - pcap_main_t
60 * @param *vm - vlib_main_t
61 * @param buffer_index - u32
62 * @param n_bytes_in_trace - u32
63 *
64 */
65static inline void
66pcap_add_buffer (pcap_main_t * pm,
67 struct vlib_main_t *vm, u32 buffer_index,
68 u32 n_bytes_in_trace)
69{
70 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
71 u32 n = vlib_buffer_length_in_chain (vm, b);
72 i32 n_left = clib_min (n_bytes_in_trace, n);
73 f64 time_now = vlib_time_now (vm);
74 void *d;
75
76 if (PREDICT_TRUE (pm->n_packets_captured < pm->n_packets_to_capture))
77 {
78 clib_spinlock_lock_if_init (&pm->lock);
79 d = pcap_add_packet (pm, time_now, n_left, n);
80 while (1)
81 {
82 u32 copy_length = clib_min ((u32) n_left, b->current_length);
83 clib_memcpy_fast (d, b->data + b->current_data, copy_length);
84 n_left -= b->current_length;
85 if (n_left <= 0)
86 break;
87 d += b->current_length;
88 ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
89 b = vlib_get_buffer (vm, b->next_buffer);
90 }
91 clib_spinlock_unlock_if_init (&pm->lock);
92 }
93}
94
95#endif /* included_vppinfra_pcap_funcs_h */
96
97/*
98 * fd.io coding-style-patch-verification: ON
99 *
100 * Local Variables:
101 * eval: (c-set-style "gnu")
102 * End:
103 */