FD.io VPP  v16.06
Vector Packet Processing
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 #include <vnet/unix/pcap.h>
19 #include <vnet/ethernet/packet.h>
20 #include <stdio.h>
21 
23 
24 static char * pg_fmt =
25  "packet-generator new {\n"
26  " name s%d\n"
27  " limit 1\n"
28  " size %d-%d\n"
29  " node ethernet-input\n";
30 
31 
32 void stream_boilerplate (FILE *ofp, int i, u8 * pkt)
33 {
34  fformat(ofp, pg_fmt, i, vec_len(pkt), vec_len(pkt));
35 }
36 
37 int pcap2pg (pcap_main_t * pm, FILE *ofp)
38 {
39  int i, j;
40  u8 *pkt;
41 
42  for (i = 0; i < vec_len (pm->packets_read); i++)
43  {
44  int offset;
46  u64 ethertype;
47 
48  pkt = pm->packets_read[i];
49  h = (ethernet_header_t *)pkt;
50 
51  stream_boilerplate (ofp, i, pkt);
52 
53  fformat (ofp, " data {\n");
54 
55  ethertype = clib_net_to_host_u16 (h->type);
56 
57  /*
58  * In vnet terms, packet generator interfaces are not ethernets.
59  * They don't have vlan tables.
60  * This dance transforms captured 802.1q VLAN packets into
61  * regular Ethernet packets.
62  */
63  if (ethertype == 0x8100 /* 802.1q vlan */)
64  {
65  u16 * vlan_ethertype = (u16 *)(h+1);
66  ethertype = clib_net_to_host_u16(vlan_ethertype[0]);
67  offset = 18;
68  }
69  else
70  offset = 14;
71 
72  fformat (ofp,
73  " 0x%04x: %02x%02x.%02x%02x.%02x%02x"
74  " -> %02x%02x.%02x%02x.%02x%02x\n",
75  ethertype,
76  h->src_address[0],
77  h->src_address[1],
78  h->src_address[2],
79  h->src_address[3],
80  h->src_address[4],
81  h->src_address[5],
82  h->dst_address[0],
83  h->dst_address[1],
84  h->dst_address[2],
85  h->dst_address[3],
86  h->dst_address[4],
87  h->dst_address[5]);
88 
89  fformat (ofp, " hex 0x");
90 
91  for (j = offset; j < vec_len (pkt); j++)
92  fformat (ofp, "%02x", pkt[j]);
93 
94  fformat (ofp, " }\n");
95  fformat (ofp, "}\n\n");
96  }
97  return 0;
98 }
99 
100 int main (int argc, char **argv)
101 {
102  unformat_input_t input;
103  pcap_main_t * pm = &pcap_main;
104  u8 * input_file = 0, * output_file = 0;
105  FILE * ofp;
106  clib_error_t * error;
107 
108  unformat_init_command_line (&input, argv);
109 
110  while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
111  {
112  if (unformat(&input, "-i %s", &input_file)
113  || unformat (&input, "input %s", &input_file))
114  ;
115  else if (unformat (&input, "-o %s", &output_file)
116  || unformat (&input, "output %s", &output_file))
117  ;
118  else
119  {
120  usage:
121  fformat(stderr,
122  "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
123  exit (1);
124  }
125  }
126 
127  if (input_file == 0)
128  goto usage;
129 
130  pm->file_name = (char *)input_file;
131  error = pcap_read (pm);
132 
133  if (error)
134  {
135  clib_error_report (error);
136  exit (1);
137  }
138 
139  if (output_file)
140  {
141  ofp = fopen ((char *)output_file, "rw");
142  if (ofp == NULL)
143  clib_unix_warning ("Couldn't create '%s'", output_file);
144  exit (1);
145  }
146  else
147  {
148  ofp = stdout;
149  }
150 
151  pcap2pg (pm, ofp);
152 
153  fclose (ofp);
154  exit (0);
155 }
char * file_name
Definition: pcap.h:107
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
#define NULL
Definition: clib.h:55
u8 src_address[6]
Definition: packet.h:52
static void usage(void)
Definition: health_check.c:14
#define clib_error_report(e)
Definition: error.h:126
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
u8 dst_address[6]
Definition: packet.h:51
int main(int argc, char **argv)
Definition: pcap2pg.c:100
unsigned long u64
Definition: types.h:89
void stream_boilerplate(FILE *ofp, int i, u8 *pkt)
Definition: pcap2pg.c:32
void unformat_init_command_line(unformat_input_t *input, char *argv[])
Definition: unformat.c:975
clib_error_t * pcap_read(pcap_main_t *pm)
Definition: pcap.c:142
#define clib_unix_warning(format, args...)
Definition: error.h:68
int pcap2pg(pcap_main_t *pm, FILE *ofp)
Definition: pcap2pg.c:37
unsigned short u16
Definition: types.h:57
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
word fformat(FILE *f, char *fmt,...)
Definition: format.c:437
static char * pg_fmt
Definition: pcap2pg.c:24
u8 ** packets_read
Definition: pcap.h:129
struct _unformat_input_t unformat_input_t
pcap_main_t pcap_main
Definition: pcap2pg.c:22