blob: e27dad7a412a2caf0159c435cf3db439a12aff8b [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 */
Dave Barach3ae28732018-11-16 17:19:00 -050022#include <vppinfra/pcap.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070023#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 */
sharath reddy1b0c9832017-11-29 20:08:11 +053031static char *pg_fmt =
Ed Warnickecb9cada2015-12-08 15:45:58 -070032 "packet-generator new {\n"
33 " name s%d\n"
sharath reddy1b0c9832017-11-29 20:08:11 +053034 " limit 1\n" " size %d-%d\n" " node ethernet-input\n";
Ed Warnickecb9cada2015-12-08 15:45:58 -070035
36
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070037/**
38 * @brief Packet Generator Stream boilerplate
39 *
40 * @param *ofp - FILE
41 * @param i - int
42 * @param *pkt - u8
43 */
sharath reddy1b0c9832017-11-29 20:08:11 +053044void
45stream_boilerplate (FILE * ofp, int i, u8 * pkt)
Ed Warnickecb9cada2015-12-08 15:45:58 -070046{
sharath reddy1b0c9832017-11-29 20:08:11 +053047 fformat (ofp, pg_fmt, i, vec_len (pkt), vec_len (pkt));
Ed Warnickecb9cada2015-12-08 15:45:58 -070048}
49
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070050/**
51 * @brief Conversion of PCAP file to PG file format
52 *
53 * @param *pm - pcap_main_t
54 * @param *ofp - FILE
55 *
56 * @return rc - int
57 *
58 */
sharath reddy1b0c9832017-11-29 20:08:11 +053059int
60pcap2pg (pcap_main_t * pm, FILE * ofp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070061{
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;
sharath reddy1b0c9832017-11-29 20:08:11 +053068 ethernet_header_t *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 u64 ethertype;
70
71 pkt = pm->packets_read[i];
sharath reddy1b0c9832017-11-29 20:08:11 +053072 h = (ethernet_header_t *) pkt;
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
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 */
sharath reddy1b0c9832017-11-29 20:08:11 +053086 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 else
sharath reddy1b0c9832017-11-29 20:08:11 +053093 offset = 14;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -070095 fformat (ofp,
sharath reddy1b0c9832017-11-29 20:08:11 +053096 " 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], h->dst_address[4], h->dst_address[5]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
110 fformat (ofp, " hex 0x");
111
112 for (j = offset; j < vec_len (pkt); j++)
sharath reddy1b0c9832017-11-29 20:08:11 +0530113 fformat (ofp, "%02x", pkt[j]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114
115 fformat (ofp, " }\n");
116 fformat (ofp, "}\n\n");
117 }
118 return 0;
119}
120
Keith Burns (alagalah)07203af2016-08-25 13:37:37 -0700121/**
122 * @brief pcap2pg.
123 * usage: pcap2pg -i <input-file> [-o <output-file>]
124 */
sharath reddy1b0c9832017-11-29 20:08:11 +0530125int
126main (int argc, char **argv)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127{
128 unformat_input_t input;
sharath reddy1b0c9832017-11-29 20:08:11 +0530129 pcap_main_t *pm = &pcap_main;
130 u8 *input_file = 0, *output_file = 0;
131 FILE *ofp;
132 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
134 unformat_init_command_line (&input, argv);
135
136 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
137 {
sharath reddy1b0c9832017-11-29 20:08:11 +0530138 if (unformat (&input, "-i %s", &input_file)
139 || unformat (&input, "input %s", &input_file))
140 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 else if (unformat (&input, "-o %s", &output_file)
sharath reddy1b0c9832017-11-29 20:08:11 +0530142 || unformat (&input, "output %s", &output_file))
143 ;
144 else
145 {
146 usage:
147 fformat (stderr,
148 "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
149 exit (1);
150 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 }
152
153 if (input_file == 0)
154 goto usage;
sharath reddy1b0c9832017-11-29 20:08:11 +0530155
156 pm->file_name = (char *) input_file;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 error = pcap_read (pm);
158
159 if (error)
160 {
161 clib_error_report (error);
162 exit (1);
163 }
164
165 if (output_file)
166 {
Dave Baracha0cc5ab2018-02-23 12:09:41 -0500167 ofp = fopen ((char *) output_file, "w+");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 if (ofp == NULL)
sharath reddy1b0c9832017-11-29 20:08:11 +0530169 clib_unix_warning ("Couldn't create '%s'", output_file);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 exit (1);
171 }
172 else
173 {
174 ofp = stdout;
175 }
sharath reddy1b0c9832017-11-29 20:08:11 +0530176
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177 pcap2pg (pm, ofp);
178
179 fclose (ofp);
180 exit (0);
181}
sharath reddy1b0c9832017-11-29 20:08:11 +0530182
183/*
184 * fd.io coding-style-patch-verification: ON
185 *
186 * Local Variables:
187 * eval: (c-set-style "gnu")
188 * End:
189 */