Initial commit of vpp code.
[vpp.git] / vnet / vnet / unix / pcap2pg.c
1 /*
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  */
17
18 #include <vnet/unix/pcap.h>
19 #include <vnet/ethernet/packet.h>
20 #include <stdio.h>
21
22 pcap_main_t pcap_main;
23
24 static char * pg_fmt = 
25   "packet-generator new {\n"
26   "    name s%d\n"
27   "    limit 1\n"
28   "    size %d-%d\n"
29   "    node ethernet-input\n";
30
31
32 void stream_boilerplate (FILE *ofp, int i, u8 * pkt)
33 {
34   fformat(ofp, pg_fmt, i, vec_len(pkt), vec_len(pkt));
35 }
36
37 int pcap2pg (pcap_main_t * pm, FILE *ofp)
38 {
39   int i, j;
40   u8 *pkt;
41    
42   for (i = 0; i < vec_len (pm->packets_read); i++)
43     {
44       int offset;
45       ethernet_header_t * h;
46       u64 ethertype;
47
48       pkt = pm->packets_read[i];
49       h = (ethernet_header_t *)pkt;
50
51       stream_boilerplate (ofp, i, pkt);
52
53       fformat (ofp, "    data {\n");
54       
55       ethertype = clib_net_to_host_u16 (h->type);
56
57       /* 
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.
62        */
63       if (ethertype == 0x8100 /* 802.1q vlan */)
64         {
65           u16 * vlan_ethertype = (u16 *)(h+1);
66           ethertype = clib_net_to_host_u16(vlan_ethertype[0]);
67           offset = 18;
68         }
69       else
70         offset = 14;
71
72       fformat (ofp, 
73                "          0x%04x: %02x%02x.%02x%02x.%02x%02x"
74                " -> %02x%02x.%02x%02x.%02x%02x\n",
75                ethertype,
76                h->src_address[0],
77                h->src_address[1],
78                h->src_address[2],
79                h->src_address[3],
80                h->src_address[4],
81                h->src_address[5],
82                h->dst_address[0],
83                h->dst_address[1],
84                h->dst_address[2],
85                h->dst_address[3],
86                h->dst_address[4],
87                h->dst_address[5]);
88
89       fformat (ofp, "      hex 0x");
90
91       for (j = offset; j < vec_len (pkt); j++)
92           fformat (ofp, "%02x", pkt[j]);
93
94       fformat (ofp, " }\n");
95       fformat (ofp, "}\n\n");
96     }
97   return 0;
98 }
99
100 int main (int argc, char **argv)
101 {
102   unformat_input_t input;
103   pcap_main_t * pm = &pcap_main;
104   u8 * input_file = 0, * output_file = 0;
105   FILE * ofp;
106   clib_error_t * error;
107
108   unformat_init_command_line (&input, argv);
109
110   while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
111     {
112       if (unformat(&input, "-i %s", &input_file)
113           || unformat (&input, "input %s", &input_file))
114         ;
115       else if (unformat (&input, "-o %s", &output_file)
116                || unformat (&input, "output %s", &output_file))
117         ;
118       else 
119         {
120         usage:
121           fformat(stderr, 
122                   "usage: pcap2pg -i <input-file> [-o <output-file>]\n");
123           exit (1);
124         }
125     }
126
127   if (input_file == 0)
128     goto usage;
129   
130   pm->file_name = (char *)input_file;
131   error = pcap_read (pm);
132
133   if (error)
134     {
135       clib_error_report (error);
136       exit (1);
137     }
138
139   if (output_file)
140     {
141       ofp = fopen ((char *)output_file, "rw");
142       if (ofp == NULL)
143         clib_unix_warning ("Couldn't create '%s'", output_file);
144       exit (1);
145     }
146   else
147     {
148       ofp = stdout;
149     }
150   
151   pcap2pg (pm, ofp);
152
153   fclose (ofp);
154   exit (0);
155 }