FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
mpcap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 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 #ifndef included_vnet_mpcap_h
17 #define included_vnet_mpcap_h
18 
19 #include <vnet/vnet.h>
20 #include <vppinfra/mpcap.h>
21 
22 /**
23  * @brief Add packet
24  *
25  * @param *pm - mpcap_main_t
26  * @param time_now - f64
27  * @param n_bytes_in_trace - u32
28  * @param n_bytes_in_packet - u32
29  *
30  * @return Packet Data
31  *
32  */
33 static inline void *
35  f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
36 {
38  u8 *d;
39 
40  /* File already closed? */
41  if (PREDICT_FALSE (pm->flags & MPCAP_FLAG_INIT_DONE) == 0)
42  return 0;
43 
44  d = pm->current_va;
45  pm->current_va += sizeof (h[0]) + n_bytes_in_trace;
46 
47  /* Out of space? */
48  if (PREDICT_FALSE (pm->current_va >= pm->file_baseva + pm->max_file_size))
49  return 0;
50  h = (void *) (d);
51  h->time_in_sec = time_now;
52  h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
53  h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
54  h->n_bytes_in_packet = n_bytes_in_packet;
55  pm->n_packets_captured++;
56  return h->data;
57 }
58 
59 /**
60  * @brief Add buffer (vlib_buffer_t) to the trace
61  *
62  * @param *pm - mpcap_main_t
63  * @param *vm - vlib_main_t
64  * @param time_now - f64
65  * @param buffer_index - u32
66  * @param n_bytes_in_trace - u32
67  *
68  */
69 static inline void
71  vlib_main_t * vm,
72  f64 time_now, u32 buffer_index, u32 n_bytes_in_trace)
73 {
74  vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
76  i32 n_left = clib_min (n_bytes_in_trace, n);
77  void *d;
78 
80 
81  d = mpcap_add_packet (pm, time_now, n_left, n);
82  if (PREDICT_FALSE (d == 0))
83  {
84  mpcap_close (pm);
86  return;
87  }
88 
89  while (1)
90  {
91  u32 copy_length = clib_min ((u32) n_left, b->current_length);
92  clib_memcpy (d, b->data + b->current_data, copy_length);
94  if (n_left <= 0)
95  break;
96  d += b->current_length;
97  ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
99  }
101  mpcap_close (pm);
102 
104 }
105 #endif /* included_vnet_mpcap_h */
106 
107 /*
108  * fd.io coding-style-patch-verification: ON
109  *
110  * Local Variables:
111  * eval: (c-set-style "gnu")
112  * End:
113  */
vlib_buffer_t::next_buffer
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:149
mpcap_main_t::current_va
u8 * current_va
current memory address
Definition: mpcap.h:125
mpcap_main_t::flags
u32 flags
flags
Definition: mpcap.h:134
mpcap_packet_header_t
Packet header.
Definition: mpcap.h:92
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
mpcap_main_t::lock
clib_spinlock_t lock
spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE
Definition: mpcap.h:110
clib_spinlock_lock_if_init
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:106
vlib_get_buffer
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
mpcap_main_t::file_baseva
u8 * file_baseva
Base address.
Definition: mpcap.h:122
vlib_buffer_length_in_chain
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:433
mpcap_main_t::max_file_size
u64 max_file_size
Maximum file size.
Definition: mpcap.h:119
h
h
Definition: flowhash_template.h:372
i32
signed int i32
Definition: types.h:77
vlib_buffer_t::current_data
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:119
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
mpcap.h
MPCAP utility definitions.
mpcap_close
clib_error_t * mpcap_close(mpcap_main_t *pm)
Flush / unmap a mpcap file.
Definition: mpcap.c:56
f64
double f64
Definition: types.h:142
mpcap_add_buffer
static void mpcap_add_buffer(mpcap_main_t *pm, vlib_main_t *vm, f64 time_now, u32 buffer_index, u32 n_bytes_in_trace)
Add buffer (vlib_buffer_t) to the trace.
Definition: mpcap.h:70
clib_min
#define clib_min(x, y)
Definition: clib.h:342
vlib_buffer_t::current_length
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
mpcap_main_t
MPCAP main state data structure.
Definition: mpcap.h:104
n_left
u32 n_left
Definition: interface_output.c:1078
vlib_main_t
Definition: main.h:102
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
MPCAP_FLAG_INIT_DONE
#define MPCAP_FLAG_INIT_DONE
Definition: mpcap.h:135
mpcap_main_t::n_packets_to_capture
u32 n_packets_to_capture
Number of packets to capture.
Definition: mpcap.h:113
vlib_buffer_t::data
u8 data[]
Packet data.
Definition: buffer.h:204
mpcap_main_t::n_packets_captured
u32 n_packets_captured
Number of packets currently captured.
Definition: mpcap.h:128
vnet.h
clib_spinlock_unlock_if_init
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:129
mpcap_add_packet
static void * mpcap_add_packet(mpcap_main_t *pm, f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
Add packet.
Definition: mpcap.h:34
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111