Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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.h: 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 | */ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 39 | /** |
| 40 | * @file |
| 41 | * @brief PCAP utility definitions |
| 42 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | #ifndef included_vnet_pcap_h |
| 44 | #define included_vnet_pcap_h |
| 45 | |
| 46 | #include <vlib/vlib.h> |
| 47 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 48 | /** |
| 49 | * @brief Packet types supported by PCAP |
| 50 | * |
| 51 | * null 0 |
| 52 | * ethernet 1 |
| 53 | * ppp 9 |
| 54 | * ip 12 |
| 55 | * hdlc 104 |
| 56 | */ |
| 57 | #define foreach_vnet_pcap_packet_type \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | _ (null, 0) \ |
| 59 | _ (ethernet, 1) \ |
| 60 | _ (ppp, 9) \ |
| 61 | _ (ip, 12) \ |
| 62 | _ (hdlc, 104) |
| 63 | |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 64 | typedef enum |
| 65 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | #define _(f,n) PCAP_PACKET_TYPE_##f = (n), |
| 67 | foreach_vnet_pcap_packet_type |
| 68 | #undef _ |
| 69 | } pcap_packet_type_t; |
| 70 | |
| 71 | #define foreach_pcap_file_header \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 72 | /** 0xa1b2c3d4 host byte order. \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | 0xd4c3b2a1 => need to byte swap everything. */ \ |
| 74 | _ (u32, magic) \ |
| 75 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 76 | /** Currently major 2 minor 4. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | _ (u16, major_version) \ |
| 78 | _ (u16, minor_version) \ |
| 79 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 80 | /** 0 for GMT. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | _ (u32, time_zone) \ |
| 82 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 83 | /** Accuracy of timestamps. Typically set to 0. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | _ (u32, sigfigs) \ |
| 85 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 86 | /** Size of largest packet in file. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | _ (u32, max_packet_size_in_bytes) \ |
| 88 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 89 | /** One of vnet_pcap_packet_type_t. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | _ (u32, packet_type) |
| 91 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 92 | /** File header struct */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 93 | typedef struct |
| 94 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | #define _(t, f) t f; |
| 96 | foreach_pcap_file_header |
| 97 | #undef _ |
| 98 | } pcap_file_header_t; |
| 99 | |
| 100 | #define foreach_pcap_packet_header \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 101 | /** Time stamp in seconds */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | _ (u32, time_in_sec) \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 103 | /** Time stamp in microseconds. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | _ (u32, time_in_usec) \ |
| 105 | \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 106 | /** Number of bytes stored in file. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 107 | _ (u32, n_packet_bytes_stored_in_file) \ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 108 | /** Number of bytes in actual packet. */ \ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | _ (u32, n_bytes_in_packet) |
| 110 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 111 | /** Packet header. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 112 | typedef struct |
| 113 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | #define _(t, f) t f; |
| 115 | foreach_pcap_packet_header |
| 116 | #undef _ |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 117 | /** Packet data follows. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | u8 data[0]; |
| 119 | } pcap_packet_header_t; |
| 120 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 121 | /** |
| 122 | * @brief PCAP main state data structure |
| 123 | */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 124 | typedef struct |
| 125 | { |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 126 | /** File name of pcap output. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 127 | char *file_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 129 | /** Number of packets to capture. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | u32 n_packets_to_capture; |
| 131 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 132 | /** Packet type */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | pcap_packet_type_t packet_type; |
| 134 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 135 | /** Number of packets currently captured. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | u32 n_packets_captured; |
| 137 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 138 | /** flags */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | u32 flags; |
| 140 | #define PCAP_MAIN_INIT_DONE (1 << 0) |
| 141 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 142 | /** File descriptor for reading/writing. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | int file_descriptor; |
| 144 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 145 | /** Bytes written */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | u32 n_pcap_data_written; |
| 147 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 148 | /** Vector of pcap data. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 149 | u8 *pcap_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 151 | /** Packets read from file. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 152 | u8 **packets_read; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 154 | /** Min/Max Packet bytes */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 155 | u32 min_packet_bytes, max_packet_bytes; |
| 156 | } pcap_main_t; |
| 157 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 158 | /** Write out data to output file. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 159 | clib_error_t *pcap_write (pcap_main_t * pm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 161 | /** Read data from file. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 162 | clib_error_t *pcap_read (pcap_main_t * pm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 164 | /** |
| 165 | * @brief Add packet |
| 166 | * |
| 167 | * @param *pm - pcap_main_t |
| 168 | * @param time_now - f64 |
| 169 | * @param n_bytes_in_trace - u32 |
| 170 | * @param n_bytes_in_packet - u32 |
| 171 | * |
| 172 | * @return Packet Data |
| 173 | * |
| 174 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 175 | static inline void * |
| 176 | pcap_add_packet (pcap_main_t * pm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 177 | f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 179 | pcap_packet_header_t *h; |
| 180 | u8 *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | |
| 182 | vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace); |
| 183 | h = (void *) (d); |
| 184 | h->time_in_sec = time_now; |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 185 | h->time_in_usec = 1e6 * (time_now - h->time_in_sec); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | h->n_packet_bytes_stored_in_file = n_bytes_in_trace; |
| 187 | h->n_bytes_in_packet = n_bytes_in_packet; |
| 188 | pm->n_packets_captured++; |
| 189 | return h->data; |
| 190 | } |
| 191 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 192 | /** |
| 193 | * @brief Add buffer (vlib_buffer_t) to the trace |
| 194 | * |
| 195 | * @param *pm - pcap_main_t |
| 196 | * @param *vm - vlib_main_t |
| 197 | * @param buffer_index - u32 |
| 198 | * @param n_bytes_in_trace - u32 |
| 199 | * |
| 200 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | static inline void |
| 202 | pcap_add_buffer (pcap_main_t * pm, |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 203 | vlib_main_t * vm, u32 buffer_index, u32 n_bytes_in_trace) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 204 | { |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 205 | vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | u32 n = vlib_buffer_length_in_chain (vm, b); |
| 207 | i32 n_left = clib_min (n_bytes_in_trace, n); |
| 208 | f64 time_now = vlib_time_now (vm); |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 209 | void *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
Damjan Marion | 92217f3 | 2016-07-12 21:58:19 +0200 | [diff] [blame] | 211 | d = pcap_add_packet (pm, time_now, n_left, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | while (1) |
| 213 | { |
John Lo | 644ec43 | 2016-04-27 09:56:36 -0400 | [diff] [blame] | 214 | u32 copy_length = clib_min ((u32) n_left, b->current_length); |
| 215 | clib_memcpy (d, b->data + b->current_data, copy_length); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | n_left -= b->current_length; |
| 217 | if (n_left <= 0) |
| 218 | break; |
| 219 | d += b->current_length; |
| 220 | ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT); |
| 221 | b = vlib_get_buffer (vm, b->next_buffer); |
| 222 | } |
| 223 | |
Keith Burns (alagalah) | 07203af | 2016-08-25 13:37:37 -0700 | [diff] [blame] | 224 | /** Flush output vector. */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 225 | if (vec_len (pm->pcap_data) >= 64 * 1024 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 226 | || pm->n_packets_captured >= pm->n_packets_to_capture) |
| 227 | pcap_write (pm); |
| 228 | } |
| 229 | |
| 230 | #endif /* included_vnet_pcap_h */ |
sharath reddy | 1b0c983 | 2017-11-29 20:08:11 +0530 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * fd.io coding-style-patch-verification: ON |
| 234 | * |
| 235 | * Local Variables: |
| 236 | * eval: (c-set-style "gnu") |
| 237 | * End: |
| 238 | */ |