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