summaryrefslogtreecommitdiff
path: root/regress/usr.sbin/switchd/OFP.pm
blob: be6112a3e24bbe784acc24acfb279135cb5d42af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#
# NetPacket::OFP - Decode and encode OpenFlow packets. 
#

package NetPacket::OFP;

#
# Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>.
#
# This package is free software and is provided "as is" without express 
# or implied warranty.  It may be used, redistributed and/or modified 
# under the terms of the Perl Artistic License (see
# http://www.perl.com/perl/misc/Artistic.html)
#

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
use NetPacket;

my $myclass;
BEGIN {
	$myclass = __PACKAGE__;
	$VERSION = "0.01";
}
sub Version () { "$myclass v$VERSION" }

BEGIN {
	@ISA = qw(Exporter NetPacket);

	@EXPORT = qw();

	@EXPORT_OK = qw(ofp_strip);

	%EXPORT_TAGS = (
		ALL         => [@EXPORT, @EXPORT_OK],
		strip       => [qw(ofp_strip)],
	);
}

#
# Get aligned packet size
#

sub ofp_align {
	my $matchlen = shift;

	return (($matchlen + (8 - 1)) & (~(8 - 1)));
}

#
# Decode the packet
#

sub decode {
	my $class = shift;
	my($pkt, $parent, @rest) = @_;
	my $self = {};

	# Class fields
	$self->{_parent} = $parent;
	$self->{_frame} = $pkt;

	$self->{version} = 1;
	$self->{type} = 0;
	$self->{length} = 0;
	$self->{xid} = 0;
	$self->{data} = '';

	# Decode OpenFlow packet
	if (defined($pkt)) {
		($self->{version}, $self->{type}, $self->{length},
		    $self->{xid}, $self->{data}) = unpack("CCnNa*", $pkt);
	}

	# Return a blessed object
	bless($self, $class);

	return ($self);
}

#
# Strip header from packet and return the data contained in it
#

undef &udp_strip;
*ofp_strip = \&strip;

sub strip {
	my ($pkt, @rest) = @_;
	my $ofp_obj = decode($pkt);
	return ($ofp_obj->data);
}   

#
# Encode a packet
#

sub encode {
	my $class = shift;
	my $self = shift;
	my $pkt = '';
	my $datalen = length($self->{data});

	if ($self->{length} == 0) {
		$self->{length} = 8 + $datalen;
	}

	if ($datalen == 0) {
		$pkt = pack('CCnN', $self->{version}, $self->{type},
		    $self->{length}, $self->{xid});
	} else {
		$pkt = pack('CCnNa*', $self->{version}, $self->{type},
		    $self->{length}, $self->{xid}, $self->{data});
	}

	return ($pkt);
}

#
# Table property Multipart reply
#

sub ofp_table_property {
	my $tp_type = shift;
	my $tp_payload = shift;
	my $tp_header;
	my ($datalen, $pad);

	# len = header + payload
	$datalen = 4 + length($tp_payload);

	$tp_header = pack('nna*',
	    $tp_type,			# type
	    $datalen,			# length
	    $tp_payload			# payload
	    );

	$pad = ofp_align($datalen) - $datalen;
	if ($pad > 0) {
		$tp_header .= pack("x[$pad]");
	}

	return ($tp_header);
}

sub ofp_table_features_reply {
	my $class = shift;
	my $self = shift;
	my $pkt = NetPacket::OFP->decode() or fatal($class, "new packet");
	my ($tf_header, $tf_payload);
	my ($tp_header, $tp_payload);
	my $mp_header;

	$tf_payload = '';

	#
	# instructions
	#
	$tp_payload = '';
	for (my $inst = main::OFP_INSTRUCTION_T_GOTO_TABLE();
	    $inst <= main::OFP_INSTRUCTION_T_METER(); $inst++) {
		$tp_payload .= pack('nn',
		    $inst,			# type
		    4				# length
		    );
	}

	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_INSTRUCTION(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_INSTRUCTION_MISS(),
		$tp_payload);

	#
	# Next tables
	#
	$tp_payload = '';

	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_NEXT_TABLES(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_NEXT_TABLES_MISS(),
		$tp_payload);

	#
	# Write / Apply actions
	#
	$tp_payload = pack('nn',
	    main::OFP_ACTION_OUTPUT(),		# type
	    4,					# length
	    );
	$tp_payload .= pack('nn',
	    main::OFP_ACTION_PUSH_VLAN(),	# type
	    4,					# length
	    );
	$tp_payload .= pack('nn',
	    main::OFP_ACTION_POP_VLAN(),	# type
	    4,					# length
	    );

	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_WRITE_ACTIONS(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_WRITE_ACTIONS_MISS(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_APPLY_ACTIONS(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_APPLY_ACTIONS_MISS(),
		$tp_payload);

	#
	# Match/Wildcards/Write set-field/Apply set-field
	#
	$tp_payload = pack('nCC',
	    main::OFP_OXM_C_OPENFLOW_BASIC(),	# class
	    main::OFP_XM_T_IN_PORT() << 1,	# type
	    4,					# length
	    );
	$tp_payload .= pack('nCC',
	    main::OFP_OXM_C_OPENFLOW_BASIC(),	# class
	    main::OFP_XM_T_ETH_TYPE() << 1,	# type
	    4,					# length
	    );
	$tp_payload .= pack('nCC',
	    main::OFP_OXM_C_OPENFLOW_BASIC(),	# class
	    main::OFP_XM_T_ETH_SRC() << 1,	# type
	    4,					# length
	    );
	$tp_payload .= pack('nCC',
	    main::OFP_OXM_C_OPENFLOW_BASIC(),	# class
	    main::OFP_XM_T_ETH_DST() << 1,	# type
	    4,					# length
	    );
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_MATCH(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_WILDCARDS(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_WRITE_SETFIELD(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_WRITE_SETFIELD_MISS(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_APPLY_SETFIELD(),
		$tp_payload);
	$tf_payload .=
	    ofp_table_property(main::OFP_TABLE_FEATPROP_APPLY_SETFIELD_MISS(),
		$tp_payload);

	#
	# Finish
	#
	$tf_header = pack('nCx[5]a[32]NNNNNN',
	    64 + length($tf_payload),	# length
	    0,				# tableid
	    'start',			# name
	    0x00000000, 0x00000000,	# metadata_match
	    0x00000000, 0x00000000,	# metadata_write
	    0x00000000,			# config
	    10000			# max_entries
	    );
	$tf_header .= $tf_payload;
	# XXX everything fits a single multipart reply, for now.

	$mp_header = pack('nnx[4]',
	    main::OFP_MP_T_TABLE_FEATURES(),	# multipart type
	    0					# multipart flags
	    );

	$mp_header .= $tf_header;

	$pkt->{version} = $self->{version};
	$pkt->{type} = main::OFP_T_MULTIPART_REPLY();
	$pkt->{xid} = $self->{xid}++;
	$pkt->{data} = $mp_header;
	$pkt = NetPacket::OFP->encode($pkt);

	main::ofp_output($self, $pkt);

	# Wait for new flow-mod
	main::ofp_input($self);
}

#
# Module initialisation
#

1;

# autoloaded methods go after the END token (&& pod) below

__END__

=head1 NAME

C<NetPacket::OFP> - Assemble and disassemble OpenFlow packets.

=head1 SYNOPSIS

  use NetPacket::OFP;

  $ofp_obj = NetPacket::OFP->decode($raw_pkt);
  $ofp_pkt = NetPacket::OFP->encode($ofp_obj);
  $ofp_data = NetPacket::OFP::strip($raw_pkt);

=head1 DESCRIPTION

C<NetPacket::OFP> provides a set of routines for assembling and
disassembling packets using OpenFlow.

=head2 Methods

=over

=item C<NetPacket::OFP-E<gt>decode([RAW PACKET])>

Decode the raw packet data given and return an object containing
instance data.  This method will quite happily decode garbage input.
It is the responsibility of the programmer to ensure valid packet data
is passed to this method.

=item C<NetPacket::OFP-E<gt>encode($ofp_obj)>

Return a OFP packet encoded with the instance data specified.

=back

=head2 Functions

=over

=item C<NetPacket::OFP::strip([RAW PACKET])>

Return the encapsulated data (or payload) contained in the OpenFlow
packet.  This data is suitable to be used as input for other
C<NetPacket::*> modules.

This function is equivalent to creating an object using the
C<decode()> constructor and returning the C<data> field of that
object.

=back

=head2 Instance data

The instance data for the C<NetPacket::OFP> object consists of
the following fields.

=over

=item version

The OpenFlow version.

=item type

The message type.

=item length

The total message length.

=item xid

The transaction Id.

=item data

The encapsulated data (payload) for this packet.

=back

=head2 Exports

=over

=item default

none

=item exportable

ofp_strip

=item tags

The following tags group together related exportable items.

=over

=item C<:strip>

Import the strip function C<ofp_strip>.

=item C<:ALL>

All the above exportable items.

=back

=back

=head1 COPYRIGHT

  Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>

  This package is free software and is provided "as is" without express 
  or implied warranty.  It may be used, redistributed and/or modified 
  under the terms of the Perl Artistic License (see
  http://www.perl.com/perl/misc/Artistic.html)

=head1 AUTHOR

Reyk Floeter E<lt>reyk@openbsd.orgE<gt>

=cut

# any real autoloaded methods go after this line