Gary Boon | a9ed6f7 | 2019-07-22 10:57:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | /** |
| 17 | * @file |
| 18 | * @brief MPCAP utility definitions |
| 19 | */ |
Dave Barach | 2a41919 | 2020-05-19 09:56:22 -0400 | [diff] [blame] | 20 | #ifndef included_vppinfra_mpcap_h |
| 21 | #define included_vppinfra_mpcap_h |
Gary Boon | a9ed6f7 | 2019-07-22 10:57:56 -0400 | [diff] [blame] | 22 | |
Gary Boon | a9ed6f7 | 2019-07-22 10:57:56 -0400 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | #include <vppinfra/time_range.h> |
Dave Barach | 2a41919 | 2020-05-19 09:56:22 -0400 | [diff] [blame] | 27 | #include <vppinfra/lock.h> |
Gary Boon | a9ed6f7 | 2019-07-22 10:57:56 -0400 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * @brief Packet types supported by MPCAP |
| 31 | * |
| 32 | * null 0 |
| 33 | * ethernet 1 |
| 34 | * ppp 9 |
| 35 | * ip 12 |
| 36 | * hdlc 104 |
| 37 | */ |
| 38 | #define foreach_vnet_mpcap_packet_type \ |
| 39 | _ (null, 0) \ |
| 40 | _ (ethernet, 1) \ |
| 41 | _ (ppp, 9) \ |
| 42 | _ (ip, 12) \ |
| 43 | _ (hdlc, 104) |
| 44 | |
| 45 | typedef enum |
| 46 | { |
| 47 | #define _(f,n) MPCAP_PACKET_TYPE_##f = (n), |
| 48 | foreach_vnet_mpcap_packet_type |
| 49 | #undef _ |
| 50 | } mpcap_packet_type_t; |
| 51 | |
| 52 | #define foreach_mpcap_file_header \ |
| 53 | /** 0xa1b2c3d4 host byte order. \ |
| 54 | 0xd4c3b2a1 => need to byte swap everything. */ \ |
| 55 | _ (u32, magic) \ |
| 56 | \ |
| 57 | /** Currently major 2 minor 4. */ \ |
| 58 | _ (u16, major_version) \ |
| 59 | _ (u16, minor_version) \ |
| 60 | \ |
| 61 | /** 0 for GMT. */ \ |
| 62 | _ (u32, time_zone) \ |
| 63 | \ |
| 64 | /** Accuracy of timestamps. Typically set to 0. */ \ |
| 65 | _ (u32, sigfigs) \ |
| 66 | \ |
| 67 | /** Size of largest packet in file. */ \ |
| 68 | _ (u32, max_packet_size_in_bytes) \ |
| 69 | \ |
| 70 | /** One of vnet_mpcap_packet_type_t. */ \ |
| 71 | _ (u32, packet_type) |
| 72 | |
| 73 | /** File header struct */ |
| 74 | typedef struct |
| 75 | { |
| 76 | #define _(t, f) t f; |
| 77 | foreach_mpcap_file_header |
| 78 | #undef _ |
| 79 | } mpcap_file_header_t; |
| 80 | |
| 81 | #define foreach_mpcap_packet_header \ |
| 82 | /** Time stamp in seconds */ \ |
| 83 | _ (u32, time_in_sec) \ |
| 84 | /** Time stamp in microseconds. */ \ |
| 85 | _ (u32, time_in_usec) \ |
| 86 | \ |
| 87 | /** Number of bytes stored in file. */ \ |
| 88 | _ (u32, n_packet_bytes_stored_in_file) \ |
| 89 | /** Number of bytes in actual packet. */ \ |
| 90 | _ (u32, n_bytes_in_packet) |
| 91 | |
| 92 | /** Packet header. */ |
| 93 | typedef struct |
| 94 | { |
| 95 | #define _(t, f) t f; |
| 96 | foreach_mpcap_packet_header |
| 97 | #undef _ |
| 98 | /** Packet data follows. */ |
| 99 | u8 data[0]; |
| 100 | } mpcap_packet_header_t; |
| 101 | |
| 102 | /** |
| 103 | * @brief MPCAP main state data structure |
| 104 | */ |
| 105 | typedef struct |
| 106 | { |
| 107 | /** File name of mpcap output. */ |
| 108 | char *file_name; |
| 109 | |
| 110 | /** spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE */ |
| 111 | clib_spinlock_t lock; |
| 112 | |
| 113 | /** Number of packets to capture. */ |
| 114 | u32 n_packets_to_capture; |
| 115 | |
| 116 | /** Packet type */ |
| 117 | mpcap_packet_type_t packet_type; |
| 118 | |
| 119 | /** Maximum file size */ |
| 120 | u64 max_file_size; |
| 121 | |
| 122 | /** Base address */ |
| 123 | u8 *file_baseva; |
| 124 | |
| 125 | /** current memory address */ |
| 126 | u8 *current_va; |
| 127 | |
| 128 | /** Number of packets currently captured. */ |
| 129 | u32 n_packets_captured; |
| 130 | |
| 131 | /** Pointer to file header in svm, for ease of updating */ |
| 132 | mpcap_file_header_t *file_header; |
| 133 | |
| 134 | /** flags */ |
| 135 | u32 flags; |
| 136 | #define MPCAP_FLAG_INIT_DONE (1 << 0) |
| 137 | #define MPCAP_FLAG_THREAD_SAFE (1 << 1) |
| 138 | #define MPCAP_FLAG_WRITE_ENABLE (1 << 2) |
| 139 | |
| 140 | /** Bytes written */ |
| 141 | u32 n_mpcap_data_written; |
| 142 | |
| 143 | /** Vector of mpcap data. */ |
| 144 | u8 *mpcap_data; |
| 145 | |
| 146 | /** Packets in mapped mpcap file. */ |
| 147 | u64 packets_read; |
| 148 | |
| 149 | /** Min/Max Packet bytes */ |
| 150 | u32 min_packet_bytes, max_packet_bytes; |
| 151 | } mpcap_main_t; |
| 152 | |
| 153 | /* Some sensible default size */ |
| 154 | #define MPCAP_DEFAULT_FILE_SIZE (10<<20) |
| 155 | |
| 156 | /** initialize a mpcap file (for writing) */ |
| 157 | clib_error_t *mpcap_init (mpcap_main_t * pm); |
| 158 | |
| 159 | /** Flush / unmap a mpcap file */ |
| 160 | clib_error_t *mpcap_close (mpcap_main_t * pm); |
| 161 | |
| 162 | /** mmap a mpcap data file. */ |
| 163 | clib_error_t *mpcap_map (mpcap_main_t * pm); |
| 164 | |
Dave Barach | 2a41919 | 2020-05-19 09:56:22 -0400 | [diff] [blame] | 165 | #endif /* included_vppinfra_mpcap_h */ |
Gary Boon | a9ed6f7 | 2019-07-22 10:57:56 -0400 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * fd.io coding-style-patch-verification: ON |
| 169 | * |
| 170 | * Local Variables: |
| 171 | * eval: (c-set-style "gnu") |
| 172 | * End: |
| 173 | */ |