FD.io VPP  v20.05.1-6-gf53edbc3b
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);
75  u32 n = vlib_buffer_length_in_chain (vm, b);
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);
93  n_left -= b->current_length;
94  if (n_left <= 0)
95  break;
96  d += b->current_length;
97  ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
98  b = vlib_get_buffer (vm, b->next_buffer);
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  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define clib_min(x, y)
Definition: clib.h:319
u8 * file_baseva
Base address.
Definition: mpcap.h:123
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:110
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
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:402
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
#define clib_memcpy(d, s, n)
Definition: string.h:180
#define MPCAP_FLAG_INIT_DONE
Definition: mpcap.h:136
unsigned int u32
Definition: types.h:88
vec_header_t h
Definition: buffer.c:322
#define PREDICT_FALSE(x)
Definition: clib.h:118
MPCAP main state data structure.
Definition: mpcap.h:105
vlib_main_t * vm
Definition: in2out_ed.c:1599
MPCAP utility definitions.
clib_error_t * mpcap_close(mpcap_main_t *pm)
Flush / unmap a mpcap file.
Definition: mpcap.c:56
Packet header.
Definition: mpcap.h:93
u8 data[]
Packet data.
Definition: buffer.h:181
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
u32 flags
flags
Definition: mpcap.h:135
signed int i32
Definition: types.h:77
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
#define ASSERT(truth)
u32 n_packets_captured
Number of packets currently captured.
Definition: mpcap.h:129
clib_spinlock_t lock
spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE
Definition: mpcap.h:111
foreach_mpcap_packet_header u8 data[0]
Packet data follows.
Definition: mpcap.h:99
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
VLIB buffer representation.
Definition: buffer.h:102
u32 n_packets_to_capture
Number of packets to capture.
Definition: mpcap.h:114
u64 max_file_size
Maximum file size.
Definition: mpcap.h:120
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:95
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
u8 * current_va
current memory address
Definition: mpcap.h:126