FD.io VPP
v21.10.1-2-g0a485f517
Vector Packet Processing
Main Page
Related Pages
Modules
Namespaces
Namespace List
Namespace Members
All
b
d
e
f
g
i
l
m
n
o
p
r
s
t
v
w
Functions
d
f
g
l
m
n
o
p
t
v
Variables
Typedefs
Enumerations
Enumerator
Data Structures
Data Structures
Data Structure Index
Class Hierarchy
Data Fields
All
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
r
s
t
u
v
w
z
~
Variables
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
c
e
g
h
k
m
n
o
r
s
Related Functions
c
d
e
h
i
m
o
p
r
s
v
Source
Files
Symbols
All
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Functions
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Variables
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Typedefs
a
b
c
d
e
f
g
h
i
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Enumerations
a
b
c
d
e
f
g
h
i
l
m
n
o
p
q
r
s
t
u
v
w
x
Enumerator
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
Macros
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
•
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Modules
Pages
pcap2pg.c
Go to the documentation of this file.
1
/*
2
* pcap2pg.c: convert pcap input to pg input
3
*
4
* Copyright (c) 2013 Cisco and/or its affiliates.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at:
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*/
17
/**
18
* @file
19
* @brief Functions to convert PCAP file format to VPP PG (Packet Generator)
20
*
21
*/
22
#include <
vppinfra/pcap.h
>
23
#include <
vnet/ethernet/packet.h
>
24
#include <stdio.h>
25
26
pcap_main_t
pcap_main
;
27
28
/**
29
* @brief char * to seed a PG file
30
*/
31
static
char
*
pg_fmt
=
32
"packet-generator new {\n"
33
" name s%d\n"
34
" limit 1\n"
" size %d-%d\n"
" node ethernet-input\n"
;
35
36
37
/**
38
* @brief Packet Generator Stream boilerplate
39
*
40
* @param *ofp - FILE
41
* @param i - int
42
* @param *pkt - u8
43
*/
44
void
45
stream_boilerplate
(FILE * ofp,
int
i
,
u8
* pkt)
46
{
47
fformat
(ofp,
pg_fmt
,
i
,
vec_len
(pkt),
vec_len
(pkt));
48
}
49
50
/**
51
* @brief Conversion of PCAP file to PG file format
52
*
53
* @param *pm - pcap_main_t
54
* @param *ofp - FILE
55
*
56
* @return rc - int
57
*
58
*/
59
int
60
pcap2pg
(
pcap_main_t
* pm, FILE * ofp)
61
{
62
int
i
, j;
63
u8
*pkt;
64
65
for
(
i
= 0;
i
<
vec_len
(pm->
packets_read
);
i
++)
66
{
67
int
offset
;
68
ethernet_header_t
*
h
;
69
u64
ethertype;
70
71
pkt = pm->
packets_read
[
i
];
72
h
= (
ethernet_header_t
*) pkt;
73
74
stream_boilerplate
(ofp,
i
, pkt);
75
76
fformat
(ofp,
" data {\n"
);
77
78
ethertype = clib_net_to_host_u16 (
h
->type);
79
80
/**
81
* In vnet terms, packet generator interfaces are not ethernets.
82
* They don't have vlan tables.
83
* This transforms captured 802.1q VLAN packets into
84
* regular Ethernet packets.
85
*/
86
if
(ethertype == 0x8100
/* 802.1q vlan */
)
87
{
88
u16
*vlan_ethertype = (
u16
*) (
h
+ 1);
89
ethertype = clib_net_to_host_u16 (vlan_ethertype[0]);
90
offset
= 18;
91
}
92
else
93
offset
= 14;
94
95
fformat
(ofp,
96
" 0x%04x: %02x%02x.%02x%02x.%02x%02x"
97
" -> %02x%02x.%02x%02x.%02x%02x\n"
,
98
ethertype,
99
h
->src_address[0],
100
h
->src_address[1],
101
h
->src_address[2],
102
h
->src_address[3],
103
h
->src_address[4],
104
h
->src_address[5],
105
h
->dst_address[0],
106
h
->dst_address[1],
107
h
->dst_address[2],
108
h
->dst_address[3],
h
->dst_address[4],
h
->dst_address[5]);
109
110
fformat
(ofp,
" hex 0x"
);
111
112
for
(j =
offset
; j <
vec_len
(pkt); j++)
113
fformat
(ofp,
"%02x"
, pkt[j]);
114
115
fformat
(ofp,
" }\n"
);
116
fformat
(ofp,
"}\n\n"
);
117
}
118
return
0;
119
}
120
121
/**
122
* @brief pcap2pg.
123
* usage: pcap2pg -i <input-file> [-o <output-file>]
124
*/
125
int
126
main
(
int
argc,
char
**argv)
127
{
128
unformat_input_t
input;
129
pcap_main_t
*pm = &
pcap_main
;
130
u8
*input_file = 0, *output_file = 0;
131
FILE *ofp;
132
clib_error_t
*
error
;
133
134
unformat_init_command_line
(&input, argv);
135
136
while
(
unformat_check_input
(&input) !=
UNFORMAT_END_OF_INPUT
)
137
{
138
if
(
unformat
(&input,
"-i %s"
, &input_file)
139
||
unformat
(&input,
"input %s"
, &input_file))
140
;
141
else
if
(
unformat
(&input,
"-o %s"
, &output_file)
142
||
unformat
(&input,
"output %s"
, &output_file))
143
;
144
else
145
{
146
usage
:
147
fformat
(stderr,
148
"usage: pcap2pg -i <input-file> [-o <output-file>]\n"
);
149
exit (1);
150
}
151
}
152
153
if
(input_file == 0)
154
goto
usage
;
155
156
pm->
file_name
= (
char
*) input_file;
157
error
=
pcap_read
(pm);
158
159
if
(
error
)
160
{
161
clib_error_report
(
error
);
162
exit (1);
163
}
164
165
if
(output_file)
166
{
167
ofp = fopen ((
char
*) output_file,
"w+"
);
168
if
(ofp == NULL)
169
clib_unix_warning
(
"Couldn't create '%s'"
, output_file);
170
exit (1);
171
}
172
else
173
{
174
ofp = stdout;
175
}
176
177
pcap2pg
(pm, ofp);
178
179
fclose (ofp);
180
exit (0);
181
}
182
183
/*
184
* fd.io coding-style-patch-verification: ON
185
*
186
* Local Variables:
187
* eval: (c-set-style "gnu")
188
* End:
189
*/
pcap_main
pcap_main_t pcap_main
Definition:
pcap2pg.c:26
pcap_read
__clib_export clib_error_t * pcap_read(pcap_main_t *pm)
Read PCAP file.
Definition:
pcap.c:176
usage
static void usage(void)
Definition:
health_check.c:14
main
int main(int argc, char **argv)
pcap2pg.
Definition:
pcap2pg.c:126
u16
unsigned short u16
Definition:
types.h:57
stream_boilerplate
void stream_boilerplate(FILE *ofp, int i, u8 *pkt)
Packet Generator Stream boilerplate.
Definition:
pcap2pg.c:45
clib_error_report
#define clib_error_report(e)
Definition:
error.h:113
unformat_input_t
struct _unformat_input_t unformat_input_t
pcap_main_t
PCAP main state data structure.
Definition:
pcap.h:155
h
h
Definition:
flowhash_template.h:372
clib_unix_warning
#define clib_unix_warning(format, args...)
Definition:
error.h:68
error
Definition:
cJSON.c:88
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition:
unformat.c:978
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition:
vec_bootstrap.h:142
packet.h
offset
struct clib_bihash_value offset
template key/value backing page structure
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition:
format.h:163
ethernet_header_t
Definition:
packet.h:52
pcap.h
PCAP utility definitions.
pcap_main_t::packets_read
u8 ** packets_read
Packets read from file.
Definition:
pcap.h:186
clib_bihash_value
template key/value backing page structure
Definition:
bihash_doc.h:44
pcap2pg
int pcap2pg(pcap_main_t *pm, FILE *ofp)
Conversion of PCAP file to PG file format.
Definition:
pcap2pg.c:60
u64
unsigned long u64
Definition:
types.h:89
fformat
__clib_export word fformat(FILE *f, char *fmt,...)
Definition:
format.c:466
u8
unsigned char u8
Definition:
types.h:56
clib_error_t
Definition:
clib_error.h:21
i
int i
Definition:
flowhash_template.h:376
unformat_init_command_line
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition:
unformat.c:1013
pcap_main_t::file_name
char * file_name
File name of pcap output.
Definition:
pcap.h:161
pg_fmt
static char * pg_fmt
char * to seed a PG file
Definition:
pcap2pg.c:31
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition:
format.h:137
src
vnet
unix
pcap2pg.c
Generated on Sat Jan 8 2022 10:37:04 for FD.io VPP by
1.8.17