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.
19 * @brief Functions to convert PCAP file format to VPP PG (Packet Generator)
22 #include <vnet/unix/pcap.h>
23 #include <vnet/ethernet/packet.h>
26 pcap_main_t pcap_main;
29 * @brief char * to seed a PG file
31 static char * pg_fmt =
32 "packet-generator new {\n"
36 " node ethernet-input\n";
40 * @brief Packet Generator Stream boilerplate
46 void stream_boilerplate (FILE *ofp, int i, u8 * pkt)
48 fformat(ofp, pg_fmt, i, vec_len(pkt), vec_len(pkt));
52 * @brief Conversion of PCAP file to PG file format
54 * @param *pm - pcap_main_t
60 int pcap2pg (pcap_main_t * pm, FILE *ofp)
65 for (i = 0; i < vec_len (pm->packets_read); i++)
68 ethernet_header_t * h;
71 pkt = pm->packets_read[i];
72 h = (ethernet_header_t *)pkt;
74 stream_boilerplate (ofp, i, pkt);
76 fformat (ofp, " data {\n");
78 ethertype = clib_net_to_host_u16 (h->type);
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.
86 if (ethertype == 0x8100 /* 802.1q vlan */)
88 u16 * vlan_ethertype = (u16 *)(h+1);
89 ethertype = clib_net_to_host_u16(vlan_ethertype[0]);
96 " 0x%04x: %02x%02x.%02x%02x.%02x%02x"
97 " -> %02x%02x.%02x%02x.%02x%02x\n",
112 fformat (ofp, " hex 0x");
114 for (j = offset; j < vec_len (pkt); j++)
115 fformat (ofp, "%02x", pkt[j]);
117 fformat (ofp, " }\n");
118 fformat (ofp, "}\n\n");
125 * usage: pcap2pg -i <input-file> [-o <output-file>]
127 int main (int argc, char **argv)
129 unformat_input_t input;
130 pcap_main_t * pm = &pcap_main;
131 u8 * input_file = 0, * output_file = 0;
133 clib_error_t * error;
135 unformat_init_command_line (&input, argv);
137 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
139 if (unformat(&input, "-i %s", &input_file)
140 || unformat (&input, "input %s", &input_file))
142 else if (unformat (&input, "-o %s", &output_file)
143 || unformat (&input, "output %s", &output_file))
149 "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
157 pm->file_name = (char *)input_file;
158 error = pcap_read (pm);
162 clib_error_report (error);
168 ofp = fopen ((char *)output_file, "rw");
170 clib_unix_warning ("Couldn't create '%s'", output_file);