blob: 217a61f4cb47d6398c90e8dc009d7fb070eaa709 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 */
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070017/**
18 * @file
19 * @brief Functions to convert PCAP file format to VPP PG (Packet Generator)
20 *
21 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070022#include <vnet/unix/pcap.h>
23#include <vnet/ethernet/packet.h>
24#include <stdio.h>
25
26pcap_main_t pcap_main;
27
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070028/**
29 * @brief char * to seed a PG file
30 */
31static char * pg_fmt =
Ed Warnickecb9cada2015-12-08 15:45:58 -070032 "packet-generator new {\n"
33 " name s%d\n"
34 " limit 1\n"
35 " size %d-%d\n"
36 " node ethernet-input\n";
37
38
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070039/**
40 * @brief Packet Generator Stream boilerplate
41 *
42 * @param *ofp - FILE
43 * @param i - int
44 * @param *pkt - u8
45 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070046void stream_boilerplate (FILE *ofp, int i, u8 * pkt)
47{
48 fformat(ofp, pg_fmt, i, vec_len(pkt), vec_len(pkt));
49}
50
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070051/**
52 * @brief Conversion of PCAP file to PG file format
53 *
54 * @param *pm - pcap_main_t
55 * @param *ofp - FILE
56 *
57 * @return rc - int
58 *
59 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070060int pcap2pg (pcap_main_t * pm, FILE *ofp)
61{
62 int i, j;
63 u8 *pkt;
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070064
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 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");
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070077
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 ethertype = clib_net_to_host_u16 (h->type);
79
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070080 /**
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 * In vnet terms, packet generator interfaces are not ethernets.
82 * They don't have vlan tables.
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070083 * This transforms captured 802.1q VLAN packets into
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 * 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
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070095 fformat (ofp,
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 " 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],
109 h->dst_address[4],
110 h->dst_address[5]);
111
112 fformat (ofp, " hex 0x");
113
114 for (j = offset; j < vec_len (pkt); j++)
115 fformat (ofp, "%02x", pkt[j]);
116
117 fformat (ofp, " }\n");
118 fformat (ofp, "}\n\n");
119 }
120 return 0;
121}
122
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700123/**
124 * @brief pcap2pg.
125 * usage: pcap2pg -i <input-file> [-o <output-file>]
126 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127int main (int argc, char **argv)
128{
129 unformat_input_t input;
130 pcap_main_t * pm = &pcap_main;
131 u8 * input_file = 0, * output_file = 0;
132 FILE * ofp;
133 clib_error_t * error;
134
135 unformat_init_command_line (&input, argv);
136
137 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
138 {
139 if (unformat(&input, "-i %s", &input_file)
140 || unformat (&input, "input %s", &input_file))
141 ;
142 else if (unformat (&input, "-o %s", &output_file)
143 || unformat (&input, "output %s", &output_file))
144 ;
145 else
146 {
147 usage:
148 fformat(stderr,
149 "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
150 exit (1);
151 }
152 }
153
154 if (input_file == 0)
155 goto usage;
156
157 pm->file_name = (char *)input_file;
158 error = pcap_read (pm);
159
160 if (error)
161 {
162 clib_error_report (error);
163 exit (1);
164 }
165
166 if (output_file)
167 {
168 ofp = fopen ((char *)output_file, "rw");
169 if (ofp == NULL)
170 clib_unix_warning ("Couldn't create '%s'", output_file);
171 exit (1);
172 }
173 else
174 {
175 ofp = stdout;
176 }
177
178 pcap2pg (pm, ofp);
179
180 fclose (ofp);
181 exit (0);
182}