diff options
author | 2016-11-16 08:09:26 +0000 | |
---|---|---|
committer | 2016-11-16 08:09:26 +0000 | |
commit | 345c954aa06544a31f89e2e6d34a23983a740d78 (patch) | |
tree | f87b0514d51e7816ae8bc16ab1d57976096f09de | |
parent | Use memcpy() to copy structures into freshly malloc'ated buffers, (diff) | |
download | wireguard-openbsd-345c954aa06544a31f89e2e6d34a23983a740d78.tar.xz wireguard-openbsd-345c954aa06544a31f89e2e6d34a23983a740d78.zip |
Remove code duplication for receiving/sending OFP packets and simplify
packet-in building.
ok reyk@
-rw-r--r-- | regress/usr.sbin/switchd/OFP.pm | 13 | ||||
-rw-r--r-- | regress/usr.sbin/switchd/run.pl | 68 |
2 files changed, 46 insertions, 35 deletions
diff --git a/regress/usr.sbin/switchd/OFP.pm b/regress/usr.sbin/switchd/OFP.pm index e98853a7960..a18606ecf8f 100644 --- a/regress/usr.sbin/switchd/OFP.pm +++ b/regress/usr.sbin/switchd/OFP.pm @@ -89,8 +89,11 @@ sub encode { my $class = shift; my $self = shift; my $pkt = ''; + my $datalen = length($self->{data}); - $self->{length} = 8; + if ($self->{length} == 0) { + $self->{length} = 8 + $datalen; + } $pkt = pack("CCnN", $self->{version}, $self->{type}, $self->{length}, $self->{xid}); @@ -98,16 +101,16 @@ sub encode { if ($self->{version} == 1) { # PACKET_IN if ($self->{type} == 10) { - $self->{length} += 10 + length($self->{data}); - $pkt = pack("CCnNNnnCCa*", + $self->{length} += 10; + $pkt = pack("CCnNNnnxxa*", $self->{version}, $self->{type}, $self->{length}, $self->{xid}, $self->{buffer_id}, length($self->{data}), - $self->{port}, 0, 0, $self->{data}); + $self->{port}, $self->{data}); } } - return ($pkt); + return ($pkt); } # diff --git a/regress/usr.sbin/switchd/run.pl b/regress/usr.sbin/switchd/run.pl index d14731bd273..e1d5b650b8d 100644 --- a/regress/usr.sbin/switchd/run.pl +++ b/regress/usr.sbin/switchd/run.pl @@ -1,5 +1,5 @@ #!/usr/bin/perl -w -# $OpenBSD: run.pl,v 1.5 2016/09/28 10:09:59 reyk Exp $ +# $OpenBSD: run.pl,v 1.6 2016/11/16 08:09:26 rzalamena Exp $ # Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org> # @@ -54,32 +54,53 @@ sub ofp_debug { } +sub ofp_input { + my $self = shift; + my $pkt; + my $pktext; + my $ofp; + my $ofplen; + + # Read the OFP payload head + $self->{sock}->recv($pkt, 8); + $ofp = NetPacket::OFP->decode($pkt) or + fatal('ofp_input', 'Failed to decode OFP header'); + + # Read the body and decode it. + $ofplen = $ofp->{length}; + if ($ofplen > 8) { + $self->{sock}->recv($pktext, ($ofplen - 8)); + $ofp = NetPacket::OFP->decode($pkt . $pktext) or + fatal('ofp_input', 'Failed to decode OFP'); + } + ofp_debug('<', $ofp); + + return ($ofp); +} + +sub ofp_output { + my $self = shift; + my $pkt = shift; + my $ofp = NetPacket::OFP->decode($pkt); + + ofp_debug('>', $ofp); + $self->{sock}->send($pkt); +} + sub ofp_hello { my $class; my $self = shift; my $hello = NetPacket::OFP->decode() or fatal($class, "new packet"); - my $resp = (); my $pkt; - my $resppkt; $hello->{version} = $self->{version}; $hello->{type} = OFP_T_HELLO(); $hello->{xid} = $self->{xid}++; - $pkt = NetPacket::OFP->encode($hello); - ofp_debug(">", $hello); - # XXX timeout - $self->{sock}->send($pkt); - $self->{sock}->recv($resppkt, 65535); - - $resp = NetPacket::OFP->decode($resppkt) or - fatal($class, "recv'ed packet"); - - ofp_debug("<", $resp); - - return ($resp); + ofp_output($self, $pkt); + return (ofp_input($self)); } sub ofp_packet_in { @@ -87,32 +108,19 @@ sub ofp_packet_in { my $self = shift; my $data = shift; my $pktin = NetPacket::OFP->decode() or fatal($class, "new packet"); - my $resp = (); my $pkt; - my $resppkt; $pktin->{version} = $self->{version}; $pktin->{type} = OFP_T_PACKET_IN(); $pktin->{xid} = $self->{xid}++; $pktin->{data} = $data; - $pktin->{length} += length($data); $pktin->{buffer_id} = $self->{count} || 1; $pktin->{port} = $self->{port} || OFP_PORT_NORMAL(); - $pkt = NetPacket::OFP->encode($pktin); - ofp_debug(">", $pktin); - # XXX timeout - $self->{sock}->send($pkt); - $self->{sock}->recv($resppkt, 65535); - - $resp = NetPacket::OFP->decode($resppkt) or - fatal($class, "recv'ed packet"); - - ofp_debug("<", $resp); - - return ($resp); + ofp_output($self, $pkt); + return (ofp_input($self)); } sub packet_send { |