2 * pcap2pg.c: convert pcap input to pg input
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/unix/pcap.h>
19 #include <vnet/ethernet/packet.h>
22 pcap_main_t pcap_main;
24 static char * pg_fmt =
25 "packet-generator new {\n"
29 " node ethernet-input\n";
32 void stream_boilerplate (FILE *ofp, int i, u8 * pkt)
34 fformat(ofp, pg_fmt, i, vec_len(pkt), vec_len(pkt));
37 int pcap2pg (pcap_main_t * pm, FILE *ofp)
42 for (i = 0; i < vec_len (pm->packets_read); i++)
45 ethernet_header_t * h;
48 pkt = pm->packets_read[i];
49 h = (ethernet_header_t *)pkt;
51 stream_boilerplate (ofp, i, pkt);
53 fformat (ofp, " data {\n");
55 ethertype = clib_net_to_host_u16 (h->type);
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.
63 if (ethertype == 0x8100 /* 802.1q vlan */)
65 u16 * vlan_ethertype = (u16 *)(h+1);
66 ethertype = clib_net_to_host_u16(vlan_ethertype[0]);
73 " 0x%04x: %02x%02x.%02x%02x.%02x%02x"
74 " -> %02x%02x.%02x%02x.%02x%02x\n",
89 fformat (ofp, " hex 0x");
91 for (j = offset; j < vec_len (pkt); j++)
92 fformat (ofp, "%02x", pkt[j]);
94 fformat (ofp, " }\n");
95 fformat (ofp, "}\n\n");
100 int main (int argc, char **argv)
102 unformat_input_t input;
103 pcap_main_t * pm = &pcap_main;
104 u8 * input_file = 0, * output_file = 0;
106 clib_error_t * error;
108 unformat_init_command_line (&input, argv);
110 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
112 if (unformat(&input, "-i %s", &input_file)
113 || unformat (&input, "input %s", &input_file))
115 else if (unformat (&input, "-o %s", &output_file)
116 || unformat (&input, "output %s", &output_file))
122 "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
130 pm->file_name = (char *)input_file;
131 error = pcap_read (pm);
135 clib_error_report (error);
141 ofp = fopen ((char *)output_file, "rw");
143 clib_unix_warning ("Couldn't create '%s'", output_file);