diff options
author | Gary Boon <gboon@cisco.com> | 2019-07-22 10:57:56 -0400 |
---|---|---|
committer | Dave Barach <openvpp@barachs.net> | 2019-07-24 01:04:59 +0000 |
commit | a9ed6f70202eb43ef50c304f3569505815360cb9 (patch) | |
tree | 43a1f9409f36efa3ae45a173bffd5a216a42f0b9 | |
parent | 3940de36728b768574a3e998389bb90d55f690d1 (diff) |
vppinfra: add mapped pcap file support
Type: feature
Change-Id: Ic720d56a6f8901efde2a58519bc9aa553205a9a6
Signed-off-by: Gary Boon <gboon@cisco.com>
Signed-off-by: Dave Barach <dave@barachs.net>
-rw-r--r-- | src/plugins/unittest/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/plugins/unittest/mpcap_node.c | 260 | ||||
-rw-r--r-- | src/vppinfra/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/vppinfra/mpcap.c | 247 | ||||
-rw-r--r-- | src/vppinfra/mpcap.h | 257 | ||||
-rw-r--r-- | test/test_mpcap.py | 55 |
6 files changed, 822 insertions, 0 deletions
diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt index 5ba21bd7acb..e074d317b0a 100644 --- a/src/plugins/unittest/CMakeLists.txt +++ b/src/plugins/unittest/CMakeLists.txt @@ -28,6 +28,7 @@ add_vpp_plugin(unittest lisp_cp_test.c llist_test.c mactime_test.c + mpcap_node.c mfib_test.c punt_test.c rbtree_test.c diff --git a/src/plugins/unittest/mpcap_node.c b/src/plugins/unittest/mpcap_node.c new file mode 100644 index 00000000000..8c0bc8677be --- /dev/null +++ b/src/plugins/unittest/mpcap_node.c @@ -0,0 +1,260 @@ +/* + * mpcap_node.c - unit-test plugin mapped pcap test node + * + * Copyright (c) <current-year> <your-organization> + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <vlib/vlib.h> +#include <vnet/vnet.h> +#include <vnet/pg/pg.h> +#include <vppinfra/error.h> +#include <vppinfra/mpcap.h> +#include <vnet/ethernet/ethernet.h> + +static mpcap_main_t test_mpcap_main = { + .file_name = "/tmp/mpcap_unittest.pcap", + .n_packets_to_capture = 15, + .packet_type = MPCAP_PACKET_TYPE_ethernet, +}; + +typedef struct +{ + u32 sw_if_index; + u32 next_index; +} mpcap_trace_t; + +#ifndef CLIB_MARCH_VARIANT + +/* packet trace format function */ +static u8 * +format_mpcap_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + mpcap_trace_t *t = va_arg (*args, mpcap_trace_t *); + + s = format (s, "MPCAP: sw_if_index %d, next index %d\n", + t->sw_if_index, t->next_index); + return s; +} + +vlib_node_registration_t mpcap_node; + +#endif /* CLIB_MARCH_VARIANT */ + +#define foreach_mpcap_error \ +_(PROCESSED, "Mapped pcap packets processed") + +typedef enum +{ +#define _(sym,str) MPCAP_ERROR_##sym, + foreach_mpcap_error +#undef _ + MPCAP_N_ERROR, +} mpcap_error_t; + +#ifndef CLIB_MARCH_VARIANT +static char *mpcap_error_strings[] = { +#define _(sym,string) string, + foreach_mpcap_error +#undef _ +}; +#endif /* CLIB_MARCH_VARIANT */ + +typedef enum +{ + MPCAP_NEXT_DROP, + MPCAP_N_NEXT, +} mpcap_next_t; + +always_inline uword +mpcap_inline (vlib_main_t * vm, + vlib_node_runtime_t * node, vlib_frame_t * frame, + int is_ip4, int is_trace) +{ + u32 n_left_from, *from; + vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; + u16 nexts[VLIB_FRAME_SIZE], *next; + mpcap_main_t *mm = &test_mpcap_main; + int i = 0; + f64 now = vlib_time_now (vm); + u32 error_index = node->errors[MPCAP_ERROR_PROCESSED]; + + if (!(mm->flags & MPCAP_FLAG_INIT_DONE)) + { + clib_error_t *error; + + error = mpcap_init (mm); + if (error) + clib_error_report (error); + mm->flags |= MPCAP_FLAG_INIT_DONE; + } + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + + vlib_get_buffers (vm, from, bufs, n_left_from); + b = bufs; + next = nexts; + + while (n_left_from >= 4) + { + /* Prefetch next iteration. */ + if (PREDICT_TRUE (n_left_from >= 8)) + { + vlib_prefetch_buffer_header (b[4], STORE); + vlib_prefetch_buffer_header (b[5], STORE); + vlib_prefetch_buffer_header (b[6], STORE); + vlib_prefetch_buffer_header (b[7], STORE); + CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE); + CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE); + } + + next[0] = 0; + next[1] = 0; + next[2] = 0; + next[3] = 0; + + b[0]->error = error_index; + b[1]->error = error_index; + b[2]->error = error_index; + b[3]->error = error_index; + + mpcap_add_buffer (mm, vm, now, from[i++], + vlib_buffer_length_in_chain (vm, b[0])); + + mpcap_add_buffer (mm, vm, now, from[i++], + vlib_buffer_length_in_chain (vm, b[1])); + + mpcap_add_buffer (mm, vm, now, from[i++], + vlib_buffer_length_in_chain (vm, b[2])); + + mpcap_add_buffer (mm, vm, now, from[i++], + vlib_buffer_length_in_chain (vm, b[3])); + + if (is_trace) + { + if (b[0]->flags & VLIB_BUFFER_IS_TRACED) + { + mpcap_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t)); + t->next_index = next[0]; + t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX]; + } + if (b[1]->flags & VLIB_BUFFER_IS_TRACED) + { + mpcap_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t)); + t->next_index = next[1]; + t->sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX]; + } + if (b[2]->flags & VLIB_BUFFER_IS_TRACED) + { + mpcap_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t)); + t->next_index = next[2]; + t->sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX]; + } + if (b[3]->flags & VLIB_BUFFER_IS_TRACED) + { + mpcap_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t)); + t->next_index = next[3]; + t->sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX]; + } + } + + b += 4; + next += 4; + n_left_from -= 4; + } + + while (n_left_from > 0) + { + mpcap_add_buffer (mm, vm, now, from[i++], + vlib_buffer_length_in_chain (vm, b[0])); + next[0] = 0; + b[0]->error = error_index; + + if (is_trace) + { + if (b[0]->flags & VLIB_BUFFER_IS_TRACED) + { + mpcap_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t)); + t->next_index = next[0]; + t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX]; + } + } + + b += 1; + next += 1; + n_left_from -= 1; + } + + vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); + + return frame->n_vectors; +} + +VLIB_NODE_FN (mpcap_node) (vlib_main_t * vm, vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE)) + return mpcap_inline (vm, node, frame, 1 /* is_ip4 */ , + 1 /* is_trace */ ); + else + return mpcap_inline (vm, node, frame, 1 /* is_ip4 */ , + 0 /* is_trace */ ); +} + +/* *INDENT-OFF* */ +#ifndef CLIB_MARCH_VARIANT +VLIB_REGISTER_NODE (mpcap_node) = +{ + .name = "mpcap-unittest", + .vector_size = sizeof (u32), + .format_trace = format_mpcap_trace, + .format_buffer = format_ethernet_header_with_length, + .unformat_buffer = unformat_ethernet_header, + + .type = VLIB_NODE_TYPE_INTERNAL, + + .n_errors = ARRAY_LEN(mpcap_error_strings), + .error_strings = mpcap_error_strings, + + .n_next_nodes = MPCAP_N_NEXT, + + /* edit / add dispositions here */ + .next_nodes = + { + [MPCAP_NEXT_DROP] = "error-drop", + }, +}; + +static clib_error_t * +mpcap_node_init (vlib_main_t *vm) +{ + /* So we can send ethernet/IP packets from the pg... */ + ethernet_setup_node (vm, mpcap_node.index); + return 0; +} +VLIB_INIT_FUNCTION (mpcap_node_init); + +#endif /* CLIB_MARCH_VARIANT */ +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt index 9c5804602a1..6cd30876fa0 100644 --- a/src/vppinfra/CMakeLists.txt +++ b/src/vppinfra/CMakeLists.txt @@ -56,6 +56,7 @@ set(VPPINFRA_SRCS macros.c maplog.c mhash.c + mpcap.c pcap.c pmalloc.c pool.c @@ -136,6 +137,7 @@ set(VPPINFRA_HEADERS mhash.h mheap_bootstrap.h mheap.h + mpcap.h os.h pcap.h pcap_funcs.h diff --git a/src/vppinfra/mpcap.c b/src/vppinfra/mpcap.c new file mode 100644 index 00000000000..902f52f1870 --- /dev/null +++ b/src/vppinfra/mpcap.c @@ -0,0 +1,247 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <sys/fcntl.h> +#include <vppinfra/mpcap.h> + +/* + * Unfortunately, the "make test" infra won't work with mapped pcap files. + * Given enough work [mostly in .py code], one could fix that. + */ + +/** + * @file + * @brief mapped pcap file support + * + * Usage: + * + * <code><pre> + * \#include <vnet/unix/mpcap.h> + * + * static mpcap_main_t mpcap = { + * .file_name = "/tmp/ip4", + * .n_packets_to_capture = 2, + * .packet_type = MPCAP_PACKET_TYPE_ip, + * }; + * </pre></code> + * + * To add a buffer: + * + * <code><pre>mpcap_add_buffer (&mpcap, vm, pi0, 128);</pre></code> + * + * File will be written after @c n_packets_to_capture + * or call to mpcap_close + * + */ + +/** + * @brief Close a mapped pcap file + * @param mpcap_main_t * pm + * @return rc - clib_error_t + * + */ +clib_error_t * +mpcap_close (mpcap_main_t * pm) +{ + u64 actual_size = pm->current_va - pm->file_baseva; + + /* Not open? Done... */ + if ((pm->flags & MPCAP_FLAG_INIT_DONE) == 0) + return 0; + + (void) munmap (pm->file_baseva, pm->max_file_size); + pm->file_baseva = 0; + pm->current_va = 0; + pm->flags &= ~MPCAP_FLAG_INIT_DONE; + + if ((pm->flags & MPCAP_FLAG_WRITE_ENABLE) == 0) + return 0; + + if (truncate (pm->file_name, actual_size) < 0) + clib_unix_warning ("setting file size to %llu", actual_size); + + return 0; +} + +/** + * @brief Initialize a mapped pcap file + * @param mpcap_main_t * pm + * @return rc - clib_error_t + * + */ +clib_error_t * +mpcap_init (mpcap_main_t * pm) +{ + mpcap_file_header_t *fh; + u8 zero = 0; + int fd; + + if (pm->flags & MPCAP_FLAG_INIT_DONE) + return 0; + + if (!pm->file_name) + pm->file_name = "/tmp/vnet.mpcap"; + + if (pm->flags & MPCAP_FLAG_THREAD_SAFE) + clib_spinlock_init (&pm->lock); + + fd = open (pm->file_name, O_CREAT | O_TRUNC | O_RDWR, 0664); + if (fd < 0) + { + return clib_error_return_unix (0, "failed to create `%s'", + pm->file_name); + } + + if (pm->max_file_size == 0ULL) + pm->max_file_size = MPCAP_DEFAULT_FILE_SIZE; + + /* Round to a multiple of the page size */ + pm->max_file_size += (u64) clib_mem_get_page_size (); + pm->max_file_size &= ~(u64) clib_mem_get_page_size (); + + /* Set file size. */ + if (lseek (fd, pm->max_file_size - 1, SEEK_SET) == (off_t) - 1) + { + close (fd); + (void) unlink (pm->file_name); + return clib_error_return_unix (0, "file size seek"); + } + + if (write (fd, &zero, 1) != 1) + { + close (fd); + (void) unlink (pm->file_name); + return clib_error_return_unix (0, "file size write"); + } + + pm->file_baseva = mmap (0, pm->max_file_size, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (pm->file_baseva == (u8 *) MAP_FAILED) + { + clib_error_t *error = clib_error_return_unix (0, "mmap"); + close (fd); + (void) unlink (pm->file_name); + return error; + } + (void) close (fd); + + pm->flags |= MPCAP_FLAG_INIT_DONE | MPCAP_FLAG_WRITE_ENABLE; + pm->n_packets_captured = 0; + pm->n_mpcap_data_written = 0; + + /* Initialize file header */ + fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva; + pm->current_va = pm->file_baseva + sizeof (*fh); + + fh->magic = 0xa1b2c3d4; + fh->major_version = 2; + fh->minor_version = 4; + fh->time_zone = 0; + fh->max_packet_size_in_bytes = 1 << 16; + fh->packet_type = pm->packet_type; + return 0; +} + + +/** + * @brief mmap a mapped pcap file, e.g. to read from another process + * @param pcap_main_t *pm + * @return rc - clib_error_t + */ +clib_error_t * +mpcap_map (mpcap_main_t * pm) +{ + clib_error_t *error = 0; + int fd = -1; + mpcap_file_header_t *fh; + mpcap_packet_header_t *ph; + struct stat statb; + u64 packets_read = 0; + u32 min_packet_bytes = ~0; + u32 max_packet_bytes = 0; + + if (stat (pm->file_name, &statb) < 0) + { + error = clib_error_return_unix (0, "stat `%s'", pm->file_name); + goto done; + } + + fd = open (pm->file_name, O_RDONLY); + if (fd < 0) + { + error = clib_error_return_unix (0, "open `%s'", pm->file_name); + goto done; + } + + if (statb.st_size < sizeof (*fh) + sizeof (*ph)) + { + error = clib_error_return_unix (0, "`%s' is too short", pm->file_name); + goto done; + } + + pm->max_file_size = statb.st_size; + pm->file_baseva = mmap (0, statb.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (pm->file_baseva == (u8 *) MAP_FAILED) + { + error = clib_error_return_unix (0, "mmap"); + goto done; + } + + pm->flags |= MPCAP_FLAG_INIT_DONE; + fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva; + ph = (mpcap_packet_header_t *) (fh + 1); + + if (fh->magic != 0xa1b2c3d4) + { + error = clib_error_return (0, "bad magic `%s'", pm->file_name); + goto done; + } + + /* for the client's convenience, count packets; compute min/max sizes */ + while (ph < (mpcap_packet_header_t *) pm->file_baseva + pm->max_file_size) + { + if (ph->n_packet_bytes_stored_in_file == 0) + break; + + packets_read++; + min_packet_bytes = + ph->n_packet_bytes_stored_in_file < + min_packet_bytes ? ph->n_packet_bytes_stored_in_file : + min_packet_bytes; + max_packet_bytes = + ph->n_packet_bytes_stored_in_file > + max_packet_bytes ? ph->n_packet_bytes_stored_in_file : + max_packet_bytes; + + ph = (mpcap_packet_header_t *) + (((u8 *) (ph)) + sizeof (*ph) + ph->n_packet_bytes_stored_in_file); + } + pm->packets_read = packets_read; + pm->min_packet_bytes = min_packet_bytes; + pm->max_packet_bytes = max_packet_bytes; + +done: + if (fd >= 0) + close (fd); + return error; +} + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vppinfra/mpcap.h b/src/vppinfra/mpcap.h new file mode 100644 index 00000000000..1710702d8bd --- /dev/null +++ b/src/vppinfra/mpcap.h @@ -0,0 +1,257 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * @brief MPCAP utility definitions + */ +#ifndef included_vnet_mpcap_h +#define included_vnet_mpcap_h + +#include <vlib/vlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <vppinfra/time_range.h> + +/** + * @brief Packet types supported by MPCAP + * + * null 0 + * ethernet 1 + * ppp 9 + * ip 12 + * hdlc 104 + */ +#define foreach_vnet_mpcap_packet_type \ + _ (null, 0) \ + _ (ethernet, 1) \ + _ (ppp, 9) \ + _ (ip, 12) \ + _ (hdlc, 104) + +typedef enum +{ +#define _(f,n) MPCAP_PACKET_TYPE_##f = (n), + foreach_vnet_mpcap_packet_type +#undef _ +} mpcap_packet_type_t; + +#define foreach_mpcap_file_header \ + /** 0xa1b2c3d4 host byte order. \ + 0xd4c3b2a1 => need to byte swap everything. */ \ + _ (u32, magic) \ + \ + /** Currently major 2 minor 4. */ \ + _ (u16, major_version) \ + _ (u16, minor_version) \ + \ + /** 0 for GMT. */ \ + _ (u32, time_zone) \ + \ + /** Accuracy of timestamps. Typically set to 0. */ \ + _ (u32, sigfigs) \ + \ + /** Size of largest packet in file. */ \ + _ (u32, max_packet_size_in_bytes) \ + \ + /** One of vnet_mpcap_packet_type_t. */ \ + _ (u32, packet_type) + +/** File header struct */ +typedef struct +{ +#define _(t, f) t f; + foreach_mpcap_file_header +#undef _ +} mpcap_file_header_t; + +#define foreach_mpcap_packet_header \ + /** Time stamp in seconds */ \ + _ (u32, time_in_sec) \ + /** Time stamp in microseconds. */ \ + _ (u32, time_in_usec) \ + \ + /** Number of bytes stored in file. */ \ + _ (u32, n_packet_bytes_stored_in_file) \ + /** Number of bytes in actual packet. */ \ + _ (u32, n_bytes_in_packet) + +/** Packet header. */ +typedef struct +{ +#define _(t, f) t f; + foreach_mpcap_packet_header +#undef _ + /** Packet data follows. */ + u8 data[0]; +} mpcap_packet_header_t; + +/** + * @brief MPCAP main state data structure + */ +typedef struct +{ + /** File name of mpcap output. */ + char *file_name; + + /** spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE */ + clib_spinlock_t lock; + + /** Number of packets to capture. */ + u32 n_packets_to_capture; + + /** Packet type */ + mpcap_packet_type_t packet_type; + + /** Maximum file size */ + u64 max_file_size; + + /** Base address */ + u8 *file_baseva; + + /** current memory address */ + u8 *current_va; + + /** Number of packets currently captured. */ + u32 n_packets_captured; + + /** Pointer to file header in svm, for ease of updating */ + mpcap_file_header_t *file_header; + + /** flags */ + u32 flags; +#define MPCAP_FLAG_INIT_DONE (1 << 0) +#define MPCAP_FLAG_THREAD_SAFE (1 << 1) +#define MPCAP_FLAG_WRITE_ENABLE (1 << 2) + + /** Bytes written */ + u32 n_mpcap_data_written; + + /** Vector of mpcap data. */ + u8 *mpcap_data; + + /** Packets in mapped mpcap file. */ + u64 packets_read; + + /** Min/Max Packet bytes */ + u32 min_packet_bytes, max_packet_bytes; +} mpcap_main_t; + +/* Some sensible default size */ +#define MPCAP_DEFAULT_FILE_SIZE (10<<20) + +/** initialize a mpcap file (for writing) */ +clib_error_t *mpcap_init (mpcap_main_t * pm); + +/** Flush / unmap a mpcap file */ +clib_error_t *mpcap_close (mpcap_main_t * pm); + +/** mmap a mpcap data file. */ +clib_error_t *mpcap_map (mpcap_main_t * pm); + +/** + * @brief Add packet + * + * @param *pm - mpcap_main_t + * @param time_now - f64 + * @param n_bytes_in_trace - u32 + * @param n_bytes_in_packet - u32 + * + * @return Packet Data + * + */ +static inline void * +mpcap_add_packet (mpcap_main_t * pm, + f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet) +{ + mpcap_packet_header_t *h; + u8 *d; + + /* File already closed? */ + if (PREDICT_FALSE (pm->flags & MPCAP_FLAG_INIT_DONE) == 0) + return 0; + + d = pm->current_va; + pm->current_va += sizeof (h[0]) + n_bytes_in_trace; + + /* Out of space? */ + if (PREDICT_FALSE (pm->current_va >= pm->file_baseva + pm->max_file_size)) + return 0; + h = (void *) (d); + h->time_in_sec = time_now; + h->time_in_usec = 1e6 * (time_now - h->time_in_sec); + h->n_packet_bytes_stored_in_file = n_bytes_in_trace; + h->n_bytes_in_packet = n_bytes_in_packet; + pm->n_packets_captured++; + return h->data; +} + +/** + * @brief Add buffer (vlib_buffer_t) to the trace + * + * @param *pm - mpcap_main_t + * @param *vm - vlib_main_t + * @param time_now - f64 + * @param buffer_index - u32 + * @param n_bytes_in_trace - u32 + * + */ +static inline void +mpcap_add_buffer (mpcap_main_t * pm, + vlib_main_t * vm, + f64 time_now, u32 buffer_index, u32 n_bytes_in_trace) +{ + vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index); + u32 n = vlib_buffer_length_in_chain (vm, b); + i32 n_left = clib_min (n_bytes_in_trace, n); + void *d; + + clib_spinlock_lock_if_init (&pm->lock); + + d = mpcap_add_packet (pm, time_now, n_left, n); + if (PREDICT_FALSE (d == 0)) + { + mpcap_close (pm); + clib_spinlock_unlock_if_init (&pm->lock); + return; + } + + while (1) + { + u32 copy_length = clib_min ((u32) n_left, b->current_length); + clib_memcpy (d, b->data + b->current_data, copy_length); + n_left -= b->current_length; + if (n_left <= 0) + break; + d += b->current_length; + ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT); + b = vlib_get_buffer (vm, b->next_buffer); + } + if (pm->n_packets_captured >= pm->n_packets_to_capture) + mpcap_close (pm); + + clib_spinlock_unlock_if_init (&pm->lock); +} + +#endif /* included_vnet_mpcap_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/test/test_mpcap.py b/test/test_mpcap.py new file mode 100644 index 00000000000..466e663c799 --- /dev/null +++ b/test/test_mpcap.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +import unittest + +from framework import VppTestCase, VppTestRunner, running_extended_tests +from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath +import os + + +class TestMpcap(VppTestCase): + """ Mpcap Unit Test Cases """ + + @classmethod + def setUpClass(cls): + super(TestMpcap, cls).setUpClass() + + @classmethod + def tearDownClass(cls): + super(TestMpcap, cls).tearDownClass() + + def setUp(self): + super(TestMpcap, self).setUp() + + def tearDown(self): + super(TestMpcap, self).tearDown() + + def test_mpcap_unittest(self): + """ Mapped pcap file test """ + cmds = ["packet-generator new {\n" + " name mpcap\n" + " limit 15\n" + " size 128-128\n" + " interface local0\n" + " node mpcap-unittest\n" + " data {\n" + " IP6: 00:d0:2d:5e:86:85 -> 00:0d:ea:d0:00:00\n" + " ICMP: db00::1 -> db00::2\n" + " incrementing 30\n" + " }\n", + "trace add pg-input 15", + "pa en", + "show trace", + "show error"] + + for cmd in cmds: + self.logger.info(self.vapi.cli(cmd)) + + size = os.path.getsize("/tmp/mpcap_unittest.pcap") + os.remove("/tmp/mpcap_unittest.pcap") + if size != 2184: + self.logger.critical("BUG: file size %d not 2184" % size) + self.assertNotIn('WrongMPCAPFileSize', 'WrongMPCAPFileSize') + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) |