VPP-305: Documentation for vnet/vnet/unix
[vpp.git] / vnet / vnet / unix / pcap.c
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  * pcap.c: libpcap packet capture format
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39
40 #include <vnet/unix/pcap.h>
41 #include <sys/fcntl.h>
42
43 /**
44  * @file
45  * @brief PCAP function.
46  *  Usage
47  *
48  * #include <vnet/unix/pcap.h>
49  *
50  * static pcap_main_t pcap = {
51  *  .file_name = "/tmp/ip4",
52  *  .n_packets_to_capture = 2,
53  *  .packet_type = PCAP_PACKET_TYPE_ip,
54  * };
55  *
56  * To add a buffer:
57  *
58  *  pcap_add_buffer (&pcap, vm, pi0, 128);
59  *
60  * File will be written after n_packets_to_capture or call to pcap_write (&pcap).
61  *
62 */
63
64 /**
65  * @brief Close PCAP file
66  *
67  * @return rc - clib_error_t
68  *
69  */
70 clib_error_t *
71 pcap_close (pcap_main_t * pm)
72 {
73   close (pm->file_descriptor);
74   pm->flags &= ~PCAP_MAIN_INIT_DONE;
75   pm->file_descriptor = -1;
76   return 0;
77 }
78
79 /**
80  * @brief Write PCAP file
81  *
82  * @return rc - clib_error_t
83  *
84  */
85 clib_error_t *
86 pcap_write (pcap_main_t * pm)
87 {
88   clib_error_t * error = 0;
89
90   if (! (pm->flags & PCAP_MAIN_INIT_DONE))
91     {
92       pcap_file_header_t fh;
93       int n;
94
95       if (! pm->file_name)
96         pm->file_name = "/tmp/vnet.pcap";
97
98       pm->file_descriptor = open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664);
99       if (pm->file_descriptor < 0)
100         {
101           error = clib_error_return_unix (0, "failed to open `%s'", pm->file_name);
102           goto done;
103         }
104
105       pm->flags |= PCAP_MAIN_INIT_DONE;
106       pm->n_packets_captured = 0;
107       pm->n_pcap_data_written = 0;
108
109       /* Write file header. */
110       memset (&fh, 0, sizeof (fh));
111       fh.magic = 0xa1b2c3d4;
112       fh.major_version = 2;
113       fh.minor_version = 4;
114       fh.time_zone = 0;
115       fh.max_packet_size_in_bytes = 1 << 16;
116       fh.packet_type = pm->packet_type;
117       n = write (pm->file_descriptor, &fh, sizeof (fh));
118       if (n != sizeof (fh))
119         {
120           if (n < 0)
121             error = clib_error_return_unix (0, "write file header `%s'", pm->file_name);
122           else
123             error = clib_error_return (0, "short write of file header `%s'", pm->file_name);
124           goto done;
125         }
126     }
127
128   while (vec_len (pm->pcap_data) > pm->n_pcap_data_written)
129     {
130       int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
131
132       n = write (pm->file_descriptor,
133                  vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written), n);
134
135       if (n < 0 && unix_error_is_fatal (errno))
136         {
137           error = clib_error_return_unix (0, "write `%s'", pm->file_name);
138           goto done;
139         }
140         pm->n_pcap_data_written += n;
141       }
142
143   if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
144     {
145       vec_reset_length (pm->pcap_data);
146       pm->n_pcap_data_written = 0;
147     }
148
149   if (pm->n_packets_captured >= pm->n_packets_to_capture)
150     pcap_close(pm);
151
152  done:
153   if (error)
154     {
155       if (pm->file_descriptor >= 0)
156         close (pm->file_descriptor);
157     }
158   return error;
159 }
160
161 /**
162  * @brief Read PCAP file
163  *
164  * @return rc - clib_error_t
165  *
166  */
167 clib_error_t * pcap_read (pcap_main_t * pm)
168 {
169   clib_error_t * error = 0;
170   int fd, need_swap, n;
171   pcap_file_header_t fh;
172   pcap_packet_header_t ph;
173
174   fd = open (pm->file_name, O_RDONLY);
175   if (fd < 0)
176     {
177       error = clib_error_return_unix (0, "open `%s'", pm->file_name);
178       goto done;
179     }
180
181   if (read (fd, &fh, sizeof (fh)) != sizeof (fh))
182     {
183       error = clib_error_return_unix (0, "read file header `%s'", pm->file_name);
184       goto done;
185     }
186
187   need_swap = 0;
188   if (fh.magic == 0xd4c3b2a1)
189     {
190       need_swap = 1;
191 #define _(t,f) fh.f = clib_byte_swap_##t (fh.f);
192       foreach_pcap_file_header;
193 #undef _
194     }    
195
196   if (fh.magic != 0xa1b2c3d4)
197     {
198       error = clib_error_return (0, "bad magic `%s'", pm->file_name);
199       goto done;
200     }
201
202   pm->min_packet_bytes = 0;
203   pm->max_packet_bytes = 0;
204   while ((n = read (fd, &ph, sizeof (ph))) != 0)
205     {
206       u8 * data;
207
208       if (need_swap)
209         {
210 #define _(t,f) ph.f = clib_byte_swap_##t (ph.f);
211           foreach_pcap_packet_header;
212 #undef _
213         }
214
215       data = vec_new (u8, ph.n_bytes_in_packet);
216       if (read (fd, data, ph.n_packet_bytes_stored_in_file) != ph.n_packet_bytes_stored_in_file)
217         {
218           error = clib_error_return (0, "short read `%s'", pm->file_name);
219           goto done;
220         }
221
222       if (vec_len (pm->packets_read) == 0)
223         pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet;
224       else
225         {
226           pm->min_packet_bytes = clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet);
227           pm->max_packet_bytes = clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet);
228         }
229
230       vec_add1 (pm->packets_read, data);
231     }
232
233  done:
234   if (fd >= 0)
235     close (fd);
236   return error;
237
238 }