Gary Boon | a9ed6f7 | 2019-07-22 10:57:56 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2019 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 | #include <sys/fcntl.h> |
| 17 | #include <vppinfra/mpcap.h> |
| 18 | |
| 19 | /* |
| 20 | * Unfortunately, the "make test" infra won't work with mapped pcap files. |
| 21 | * Given enough work [mostly in .py code], one could fix that. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @file |
| 26 | * @brief mapped pcap file support |
| 27 | * |
| 28 | * Usage: |
| 29 | * |
| 30 | * <code><pre> |
| 31 | * \#include <vnet/unix/mpcap.h> |
| 32 | * |
| 33 | * static mpcap_main_t mpcap = { |
| 34 | * .file_name = "/tmp/ip4", |
| 35 | * .n_packets_to_capture = 2, |
| 36 | * .packet_type = MPCAP_PACKET_TYPE_ip, |
| 37 | * }; |
| 38 | * </pre></code> |
| 39 | * |
| 40 | * To add a buffer: |
| 41 | * |
| 42 | * <code><pre>mpcap_add_buffer (&mpcap, vm, pi0, 128);</pre></code> |
| 43 | * |
| 44 | * File will be written after @c n_packets_to_capture |
| 45 | * or call to mpcap_close |
| 46 | * |
| 47 | */ |
| 48 | |
| 49 | /** |
| 50 | * @brief Close a mapped pcap file |
| 51 | * @param mpcap_main_t * pm |
| 52 | * @return rc - clib_error_t |
| 53 | * |
| 54 | */ |
| 55 | clib_error_t * |
| 56 | mpcap_close (mpcap_main_t * pm) |
| 57 | { |
| 58 | u64 actual_size = pm->current_va - pm->file_baseva; |
| 59 | |
| 60 | /* Not open? Done... */ |
| 61 | if ((pm->flags & MPCAP_FLAG_INIT_DONE) == 0) |
| 62 | return 0; |
| 63 | |
| 64 | (void) munmap (pm->file_baseva, pm->max_file_size); |
| 65 | pm->file_baseva = 0; |
| 66 | pm->current_va = 0; |
| 67 | pm->flags &= ~MPCAP_FLAG_INIT_DONE; |
| 68 | |
| 69 | if ((pm->flags & MPCAP_FLAG_WRITE_ENABLE) == 0) |
| 70 | return 0; |
| 71 | |
| 72 | if (truncate (pm->file_name, actual_size) < 0) |
| 73 | clib_unix_warning ("setting file size to %llu", actual_size); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @brief Initialize a mapped pcap file |
| 80 | * @param mpcap_main_t * pm |
| 81 | * @return rc - clib_error_t |
| 82 | * |
| 83 | */ |
| 84 | clib_error_t * |
| 85 | mpcap_init (mpcap_main_t * pm) |
| 86 | { |
| 87 | mpcap_file_header_t *fh; |
| 88 | u8 zero = 0; |
| 89 | int fd; |
| 90 | |
| 91 | if (pm->flags & MPCAP_FLAG_INIT_DONE) |
| 92 | return 0; |
| 93 | |
| 94 | if (!pm->file_name) |
| 95 | pm->file_name = "/tmp/vnet.mpcap"; |
| 96 | |
| 97 | if (pm->flags & MPCAP_FLAG_THREAD_SAFE) |
| 98 | clib_spinlock_init (&pm->lock); |
| 99 | |
| 100 | fd = open (pm->file_name, O_CREAT | O_TRUNC | O_RDWR, 0664); |
| 101 | if (fd < 0) |
| 102 | { |
| 103 | return clib_error_return_unix (0, "failed to create `%s'", |
| 104 | pm->file_name); |
| 105 | } |
| 106 | |
| 107 | if (pm->max_file_size == 0ULL) |
| 108 | pm->max_file_size = MPCAP_DEFAULT_FILE_SIZE; |
| 109 | |
| 110 | /* Round to a multiple of the page size */ |
| 111 | pm->max_file_size += (u64) clib_mem_get_page_size (); |
| 112 | pm->max_file_size &= ~(u64) clib_mem_get_page_size (); |
| 113 | |
| 114 | /* Set file size. */ |
| 115 | if (lseek (fd, pm->max_file_size - 1, SEEK_SET) == (off_t) - 1) |
| 116 | { |
| 117 | close (fd); |
| 118 | (void) unlink (pm->file_name); |
| 119 | return clib_error_return_unix (0, "file size seek"); |
| 120 | } |
| 121 | |
| 122 | if (write (fd, &zero, 1) != 1) |
| 123 | { |
| 124 | close (fd); |
| 125 | (void) unlink (pm->file_name); |
| 126 | return clib_error_return_unix (0, "file size write"); |
| 127 | } |
| 128 | |
| 129 | pm->file_baseva = mmap (0, pm->max_file_size, |
| 130 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 131 | if (pm->file_baseva == (u8 *) MAP_FAILED) |
| 132 | { |
| 133 | clib_error_t *error = clib_error_return_unix (0, "mmap"); |
| 134 | close (fd); |
| 135 | (void) unlink (pm->file_name); |
| 136 | return error; |
| 137 | } |
| 138 | (void) close (fd); |
| 139 | |
| 140 | pm->flags |= MPCAP_FLAG_INIT_DONE | MPCAP_FLAG_WRITE_ENABLE; |
| 141 | pm->n_packets_captured = 0; |
| 142 | pm->n_mpcap_data_written = 0; |
| 143 | |
| 144 | /* Initialize file header */ |
| 145 | fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva; |
| 146 | pm->current_va = pm->file_baseva + sizeof (*fh); |
| 147 | |
| 148 | fh->magic = 0xa1b2c3d4; |
| 149 | fh->major_version = 2; |
| 150 | fh->minor_version = 4; |
| 151 | fh->time_zone = 0; |
| 152 | fh->max_packet_size_in_bytes = 1 << 16; |
| 153 | fh->packet_type = pm->packet_type; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * @brief mmap a mapped pcap file, e.g. to read from another process |
| 160 | * @param pcap_main_t *pm |
| 161 | * @return rc - clib_error_t |
| 162 | */ |
| 163 | clib_error_t * |
| 164 | mpcap_map (mpcap_main_t * pm) |
| 165 | { |
| 166 | clib_error_t *error = 0; |
| 167 | int fd = -1; |
| 168 | mpcap_file_header_t *fh; |
| 169 | mpcap_packet_header_t *ph; |
| 170 | struct stat statb; |
| 171 | u64 packets_read = 0; |
| 172 | u32 min_packet_bytes = ~0; |
| 173 | u32 max_packet_bytes = 0; |
| 174 | |
| 175 | if (stat (pm->file_name, &statb) < 0) |
| 176 | { |
| 177 | error = clib_error_return_unix (0, "stat `%s'", pm->file_name); |
| 178 | goto done; |
| 179 | } |
| 180 | |
| 181 | fd = open (pm->file_name, O_RDONLY); |
| 182 | if (fd < 0) |
| 183 | { |
| 184 | error = clib_error_return_unix (0, "open `%s'", pm->file_name); |
| 185 | goto done; |
| 186 | } |
| 187 | |
| 188 | if (statb.st_size < sizeof (*fh) + sizeof (*ph)) |
| 189 | { |
| 190 | error = clib_error_return_unix (0, "`%s' is too short", pm->file_name); |
| 191 | goto done; |
| 192 | } |
| 193 | |
| 194 | pm->max_file_size = statb.st_size; |
| 195 | pm->file_baseva = mmap (0, statb.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 196 | if (pm->file_baseva == (u8 *) MAP_FAILED) |
| 197 | { |
| 198 | error = clib_error_return_unix (0, "mmap"); |
| 199 | goto done; |
| 200 | } |
| 201 | |
| 202 | pm->flags |= MPCAP_FLAG_INIT_DONE; |
| 203 | fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva; |
| 204 | ph = (mpcap_packet_header_t *) (fh + 1); |
| 205 | |
| 206 | if (fh->magic != 0xa1b2c3d4) |
| 207 | { |
| 208 | error = clib_error_return (0, "bad magic `%s'", pm->file_name); |
| 209 | goto done; |
| 210 | } |
| 211 | |
| 212 | /* for the client's convenience, count packets; compute min/max sizes */ |
| 213 | while (ph < (mpcap_packet_header_t *) pm->file_baseva + pm->max_file_size) |
| 214 | { |
| 215 | if (ph->n_packet_bytes_stored_in_file == 0) |
| 216 | break; |
| 217 | |
| 218 | packets_read++; |
| 219 | min_packet_bytes = |
| 220 | ph->n_packet_bytes_stored_in_file < |
| 221 | min_packet_bytes ? ph->n_packet_bytes_stored_in_file : |
| 222 | min_packet_bytes; |
| 223 | max_packet_bytes = |
| 224 | ph->n_packet_bytes_stored_in_file > |
| 225 | max_packet_bytes ? ph->n_packet_bytes_stored_in_file : |
| 226 | max_packet_bytes; |
| 227 | |
| 228 | ph = (mpcap_packet_header_t *) |
| 229 | (((u8 *) (ph)) + sizeof (*ph) + ph->n_packet_bytes_stored_in_file); |
| 230 | } |
| 231 | pm->packets_read = packets_read; |
| 232 | pm->min_packet_bytes = min_packet_bytes; |
| 233 | pm->max_packet_bytes = max_packet_bytes; |
| 234 | |
| 235 | done: |
| 236 | if (fd >= 0) |
| 237 | close (fd); |
| 238 | return error; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * fd.io coding-style-patch-verification: ON |
| 243 | * |
| 244 | * Local Variables: |
| 245 | * eval: (c-set-style "gnu") |
| 246 | * End: |
| 247 | */ |