]> git.proxmox.com Git - mirror_ovs.git/blame - tests/flowgen.pl
ofp-actions: Improve action error logging a bit more.
[mirror_ovs.git] / tests / flowgen.pl
CommitLineData
064af421
BP
1#! /usr/bin/perl
2
e0edde6f 3# Copyright (c) 2009, 2010, 2011 Nicira, Inc.
a14bc59f
BP
4#
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
064af421
BP
17use strict;
18use warnings;
19
20open(FLOWS, ">&=3");# or die "failed to open fd 3 for writing: $!\n";
21open(PACKETS, ">&=4");# or die "failed to open fd 4 for writing: $!\n";
22
23# Print pcap file header.
24print PACKETS pack('NnnNNNN',
25 0xa1b2c3d4, # magic number
26 2, # major version
27 4, # minor version
28 0, # time zone offset
29 0, # time stamp accuracy
30 1518, # snaplen
31 1); # Ethernet
32
33output(DL_HEADER => '802.2');
34
b9ec4ff7 35for my $dl_header (qw(802.2+SNAP Ethernet)) {
064af421 36 my %a = (DL_HEADER => $dl_header);
b9ec4ff7 37 for my $dl_vlan (qw(none zero nonzero)) {
064af421
BP
38 my %b = (%a, DL_VLAN => $dl_vlan);
39
40 # Non-IP case.
41 output(%b, DL_TYPE => 'non-ip');
42
b9ec4ff7 43 for my $ip_options (qw(no yes)) {
064af421 44 my %c = (%b, DL_TYPE => 'ip', IP_OPTIONS => $ip_options);
b9ec4ff7 45 for my $ip_fragment (qw(no first middle last)) {
064af421 46 my %d = (%c, IP_FRAGMENT => $ip_fragment);
b9ec4ff7 47 for my $tp_proto (qw(TCP TCP+options UDP ICMP other)) {
064af421
BP
48 output(%d, TP_PROTO => $tp_proto);
49 }
50 }
51 }
52 }
53}
54
55sub output {
56 my (%attrs) = @_;
57
58 # Compose flow.
59 my (%flow);
60 $flow{DL_SRC} = "00:02:e3:0f:80:a4";
61 $flow{DL_DST} = "00:1a:92:40:ac:05";
62 $flow{NW_PROTO} = 0;
834377ea 63 $flow{NW_TOS} = 0;
064af421
BP
64 $flow{NW_SRC} = '0.0.0.0';
65 $flow{NW_DST} = '0.0.0.0';
66 $flow{TP_SRC} = 0;
67 $flow{TP_DST} = 0;
68 if (defined($attrs{DL_VLAN})) {
69 my (%vlan_map) = ('none' => 0xffff,
70 'zero' => 0,
71 'nonzero' => 0x0123);
72 $flow{DL_VLAN} = $vlan_map{$attrs{DL_VLAN}};
73 } else {
74 $flow{DL_VLAN} = 0xffff; # OFP_VLAN_NONE
75 }
76 if ($attrs{DL_HEADER} eq '802.2') {
77 $flow{DL_TYPE} = 0x5ff; # OFP_DL_TYPE_NOT_ETH_TYPE
78 } elsif ($attrs{DL_TYPE} eq 'ip') {
79 $flow{DL_TYPE} = 0x0800; # ETH_TYPE_IP
80 $flow{NW_SRC} = '10.0.2.15';
81 $flow{NW_DST} = '192.168.1.20';
834377ea 82 $flow{NW_TOS} = 44;
064af421
BP
83 if ($attrs{TP_PROTO} eq 'other') {
84 $flow{NW_PROTO} = 42;
85 } elsif ($attrs{TP_PROTO} eq 'TCP' ||
86 $attrs{TP_PROTO} eq 'TCP+options') {
6767a2cc 87 $flow{NW_PROTO} = 6; # IPPROTO_TCP
064af421
BP
88 $flow{TP_SRC} = 6667;
89 $flow{TP_DST} = 9998;
90 } elsif ($attrs{TP_PROTO} eq 'UDP') {
6767a2cc 91 $flow{NW_PROTO} = 17; # IPPROTO_UDP
064af421
BP
92 $flow{TP_SRC} = 1112;
93 $flow{TP_DST} = 2223;
94 } elsif ($attrs{TP_PROTO} eq 'ICMP') {
6767a2cc 95 $flow{NW_PROTO} = 1; # IPPROTO_ICMP
064af421
BP
96 $flow{TP_SRC} = 8; # echo request
97 $flow{TP_DST} = 0; # code
98 } else {
99 die;
100 }
7257b535 101 if ($attrs{IP_FRAGMENT} ne 'no' && $attrs{IP_FRAGMENT} ne 'first') {
064af421
BP
102 $flow{TP_SRC} = $flow{TP_DST} = 0;
103 }
104 } elsif ($attrs{DL_TYPE} eq 'non-ip') {
105 $flow{DL_TYPE} = 0x5678;
106 } else {
107 die;
108 }
109
110 # Compose packet.
111 my $packet = '';
112 $packet .= pack_ethaddr($flow{DL_DST});
113 $packet .= pack_ethaddr($flow{DL_SRC});
50f06e16
BP
114 if ($flow{DL_VLAN} != 0xffff) {
115 $packet .= pack('nn', 0x8100, $flow{DL_VLAN});
116 }
117 my $len_ofs = length($packet);
064af421
BP
118 $packet .= pack('n', 0) if $attrs{DL_HEADER} =~ /^802.2/;
119 if ($attrs{DL_HEADER} eq '802.2') {
120 $packet .= pack('CCC', 0x42, 0x42, 0x03); # LLC for 802.1D STP.
121 } else {
122 if ($attrs{DL_HEADER} eq '802.2+SNAP') {
123 $packet .= pack('CCC', 0xaa, 0xaa, 0x03); # LLC for SNAP.
124 $packet .= pack('CCC', 0, 0, 0); # SNAP OUI.
125 }
064af421
BP
126 $packet .= pack('n', $flow{DL_TYPE});
127 if ($attrs{DL_TYPE} eq 'ip') {
128 my $ip = pack('CCnnnCCnNN',
129 (4 << 4) | 5, # version, hdrlen
834377ea 130 $flow{NW_TOS}, # type of service
064af421
BP
131 0, # total length (filled in later)
132 65432, # id
133 0, # frag offset
134 64, # ttl
135 $flow{NW_PROTO}, # protocol
136 0, # checksum
137 0x0a00020f, # source
138 0xc0a80114); # dest
139 if ($attrs{IP_OPTIONS} eq 'yes') {
140 substr($ip, 0, 1) = pack('C', (4 << 4) | 8);
141 $ip .= pack('CCnnnCCCx',
142 130, # type
143 11, # length
144 0x6bc5, # top secret
145 0xabcd,
146 0x1234,
147 1,
148 2,
149 3);
150 }
151 if ($attrs{IP_FRAGMENT} ne 'no') {
152 my (%frag_map) = ('first' => 0x2000, # more frags, ofs 0
153 'middle' => 0x2111, # more frags, ofs 0x888
154 'last' => 0x0222); # last frag, ofs 0x1110
155 substr($ip, 6, 2)
156 = pack('n', $frag_map{$attrs{IP_FRAGMENT}});
157 }
158
159 if ($attrs{TP_PROTO} =~ '^TCP') {
160 my $tcp = pack('nnNNnnnn',
7257b535
BP
161 $flow{TP_SRC}, # source port
162 $flow{TP_DST}, # dest port
163 87123455, # seqno
164 712378912, # ackno
064af421
BP
165 (5 << 12) | 0x02 | 0x10, # hdrlen, SYN, ACK
166 5823, # window size
167 18923, # checksum
7257b535 168 12893); # urgent pointer
064af421
BP
169 if ($attrs{TP_PROTO} eq 'TCP+options') {
170 substr($tcp, 12, 2) = pack('n', (6 << 12) | 0x02 | 0x10);
171 $tcp .= pack('CCn', 2, 4, 1975); # MSS option
172 }
173 $tcp .= 'payload';
174 $ip .= $tcp;
175 } elsif ($attrs{TP_PROTO} eq 'UDP') {
176 my $len = 15;
177 my $udp = pack('nnnn', $flow{TP_SRC}, $flow{TP_DST}, $len, 0);
178 $udp .= chr($len) while length($udp) < $len;
179 $ip .= $udp;
180 } elsif ($attrs{TP_PROTO} eq 'ICMP') {
181 $ip .= pack('CCnnn',
7257b535
BP
182 8, # echo request
183 0, # code
184 0, # checksum
185 736, # identifier
186 931); # sequence number
064af421
BP
187 } elsif ($attrs{TP_PROTO} eq 'other') {
188 $ip .= 'other header';
189 } else {
190 die;
191 }
064af421
BP
192 substr($ip, 2, 2) = pack('n', length($ip));
193 $packet .= $ip;
194 }
195 }
50f06e16
BP
196 if ($attrs{DL_HEADER} =~ /^802.2/) {
197 my $len = length ($packet);
198 $len -= 4 if $flow{DL_VLAN} != 0xffff;
199 substr($packet, $len_ofs, 2) = pack('n', $len);
200 }
064af421
BP
201
202 print join(' ', map("$_=$attrs{$_}", keys(%attrs))), "\n";
203 print join(' ', map("$_=$flow{$_}", keys(%flow))), "\n";
204 print "\n";
205
206 print FLOWS pack('Nn',
207 0, # wildcards
208 1); # in_port
209 print FLOWS pack_ethaddr($flow{DL_SRC});
210 print FLOWS pack_ethaddr($flow{DL_DST});
834377ea 211 print FLOWS pack('nCxnCCxxNNnn',
064af421 212 $flow{DL_VLAN},
959a2ecd 213 0, # DL_VLAN_PCP
064af421 214 $flow{DL_TYPE},
834377ea 215 $flow{NW_TOS},
064af421
BP
216 $flow{NW_PROTO},
217 inet_aton($flow{NW_SRC}),
218 inet_aton($flow{NW_DST}),
219 $flow{TP_SRC},
220 $flow{TP_DST});
221
222 print PACKETS pack('NNNN',
223 0, # timestamp seconds
224 0, # timestamp microseconds
225 length($packet), # bytes saved
226 length($packet)), # total length
227 $packet;
228}
229
230sub pack_ethaddr {
231 local ($_) = @_;
232 my $xx = '([0-9a-fA-F][0-9a-fA-F])';
233 my (@octets) = /$xx:$xx:$xx:$xx:$xx:$xx/;
234 @octets == 6 or die $_;
235 my ($out) = '';
236 $out .= pack('C', hex($_)) foreach @octets;
237 return $out;
238}
239
240sub inet_aton {
241 local ($_) = @_;
242 my ($a, $b, $c, $d) = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
243 defined $d or die $_;
244 return ($a << 24) | ($b << 16) | ($c << 8) | $d;
245}