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
32 "packet-generator new {\n"
34 " limit 1\n" " size %d-%d\n" " node ethernet-input\n";
38 * @brief Packet Generator Stream boilerplate
45 stream_boilerplate (FILE * ofp, int i, u8 * pkt)
47 fformat (ofp, pg_fmt, i, vec_len (pkt), vec_len (pkt));
51 * @brief Conversion of PCAP file to PG file format
53 * @param *pm - pcap_main_t
60 pcap2pg (pcap_main_t * pm, FILE * ofp)
65 for (i = 0; i < vec_len (pm->packets_read); i++)
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",
108 h->dst_address[3], h->dst_address[4], h->dst_address[5]);
110 fformat (ofp, " hex 0x");
112 for (j = offset; j < vec_len (pkt); j++)
113 fformat (ofp, "%02x", pkt[j]);
115 fformat (ofp, " }\n");
116 fformat (ofp, "}\n\n");
123 * usage: pcap2pg -i <input-file> [-o <output-file>]
126 main (int argc, char **argv)
128 unformat_input_t input;
129 pcap_main_t *pm = &pcap_main;
130 u8 *input_file = 0, *output_file = 0;
134 unformat_init_command_line (&input, argv);
136 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
138 if (unformat (&input, "-i %s", &input_file)
139 || unformat (&input, "input %s", &input_file))
141 else if (unformat (&input, "-o %s", &output_file)
142 || unformat (&input, "output %s", &output_file))
148 "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
156 pm->file_name = (char *) input_file;
157 error = pcap_read (pm);
161 clib_error_report (error);
167 ofp = fopen ((char *) output_file, "w+");
169 clib_unix_warning ("Couldn't create '%s'", output_file);
184 * fd.io coding-style-patch-verification: ON
187 * eval: (c-set-style "gnu")