| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | package Net::OpenFlow; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use Carp; | ||
| 9 | use Net::OpenFlow::Protocol; | ||
| 10 | |||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Net::OpenFlow - Communicate with OpenFlow switches. | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version 0.02 | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | This module allows communication with an OpenFlow compliant switch. | ||
| 23 | |||
| 24 | use Net::OpenFlow; | ||
| 25 | |||
| 26 | |||
| 27 | <create connection to switch> | ||
| 28 | |||
| 29 | my $of = Net::OpenFlow->new(io_socket => $fh); | ||
| 30 | |||
| 31 | $of->send($of_message); | ||
| 32 | |||
| 33 | my $of_message = $of->recv($xid); | ||
| 34 | |||
| 35 | my $of_message_type = $of_message->{'ofp_header'}{'of_type'}; | ||
| 36 | |||
| 37 | =head1 FUNCTIONS | ||
| 38 | |||
| 39 | =over | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | our $VERSION = 0.02; | ||
| 44 | |||
| 45 | my $private_data = {}; | ||
| 46 | |||
| 47 | sub minimum_version($$) { | ||
| 48 | my $self = shift; | ||
| 49 | my $version = shift; | ||
| 50 | |||
| 51 | my $ret = $Net::OpenFlow::Protocol::openflow_version; | ||
| 52 | |||
| 53 | if ($version < $ret) { | ||
| 54 | $ret = $version; | ||
| 55 | } | ||
| 56 | |||
| 57 | # $self->__debug($self->__function_debug(q{minimum_version}, $ret, [$version])); | ||
| 58 | |||
| 59 | return $ret; | ||
| 60 | } | ||
| 61 | |||
| 62 | =item C<< new >> | ||
| 63 | |||
| 64 | This is the constructor for the Net::OpenFlow module. | ||
| 65 | |||
| 66 | my $of = Net::OpenFlow->new; | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub new { | ||
| 71 | my $class = shift; | ||
| 72 | |||
| 73 | my $ret = {}; | ||
| 74 | |||
| 75 | bless $ret, $class; | ||
| 76 | |||
| 77 | $ret->new_init($ret->__fixup_params(@_)); | ||
| 78 | |||
| 79 | return $ret; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub new_init { | ||
| 83 | my $self = shift; | ||
| 84 | my $attr = shift; | ||
| 85 | |||
| 86 | my $version = ($attr->{'version'} // 0x01); | ||
| 87 | |||
| 88 | my $ofp; | ||
| 89 | |||
| 90 | if (defined($attr->{'debug'}) and ($attr->{'debug'} =~ m{^\d+$})) { | ||
| 91 | $ofp = Net::OpenFlow::Protocol::Debug->new; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $ofp = Net::OpenFlow::Protocol->new; | ||
| 95 | } | ||
| 96 | |||
| 97 | if (defined($attr->{'io_socket'})) { | ||
| 98 | my $fd = fileno($attr->{'io_socket'}); | ||
| 99 | |||
| 100 | if (defined $fd) { | ||
| 101 | eval { | ||
| 102 | $attr->{'io_socket'}->can(q{read}); | ||
| 103 | $attr->{'io_socket'}->can(q{send}); | ||
| 104 | }; | ||
| 105 | |||
| 106 | if ($@) { | ||
| 107 | croak $@; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | croak q{Not a valid file handle}; | ||
| 112 | } | ||
| 113 | |||
| 114 | $private_data->{$self}{'io_socket'} = $attr->{'io_socket'}; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | croak q{No socket specified}; | ||
| 118 | } | ||
| 119 | |||
| 120 | $self->protocol($ofp); | ||
| 121 | } | ||
| 122 | |||
| 123 | =item C<< protocol >> | ||
| 124 | |||
| 125 | This function will return an object of type Net::OpenFlow::Protocol so that messages can be constructed. | ||
| 126 | |||
| 127 | my $ofp = $of->protocol; | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | sub protocol($;$) { | ||
| 132 | my $self = shift; | ||
| 133 | my $object = shift; | ||
| 134 | |||
| 135 | if (defined $object) { | ||
| 136 | $private_data->{$self}{'Net::OpenFlow::Protocol'} = $object; | ||
| 137 | } | ||
| 138 | |||
| 139 | eval { | ||
| 140 | $private_data->{$self}{'Net::OpenFlow::Protocol'}->isa(q{Net::OpenFlow::Protocol}); | ||
| 141 | }; | ||
| 142 | |||
| 143 | if ($@) { | ||
| 144 | croak $@; | ||
| 145 | } | ||
| 146 | |||
| 147 | return $private_data->{$self}{'Net::OpenFlow::Protocol'}; | ||
| 148 | } | ||
| 149 | |||
| 150 | =item C<< recv >> | ||
| 151 | |||
| 152 | This function will read the OpenFlow message from the file handle and return a decoded representation. | ||
| 153 | |||
| 154 | my $of_message = $of->recv($xid); | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub recv($$) { | ||
| 159 | my ($self, $xid) = @_; | ||
| 160 | |||
| 161 | my $of_message; | ||
| 162 | |||
| 163 | eval { | ||
| 164 | local $SIG{'__DIE__'}; | ||
| 165 | local $SIG{'__WARN__'}; | ||
| 166 | |||
| 167 | my $buf; | ||
| 168 | |||
| 169 | $private_data->{$self}{'io_socket'}->read($buf, $Net::OpenFlow::Protocol::header_length); | ||
| 170 | |||
| 171 | $of_message = $buf; | ||
| 172 | |||
| 173 | my $ofp_header = $self->protocol->struct_decode__ofp_header(\$buf); | ||
| 174 | |||
| 175 | unless ($ofp_header->{'version'} <= $Net::OpenFlow::Protocol::openflow_version) { | ||
| 176 | croak q{Unsupported version}; | ||
| 177 | } | ||
| 178 | |||
| 179 | my $bytes_remaining = ($ofp_header->{'length'} - $Net::OpenFlow::Protocol::header_length); | ||
| 180 | |||
| 181 | if ($bytes_remaining) { | ||
| 182 | $private_data->{$self}{'io_socket'}->read($buf, $bytes_remaining); | ||
| 183 | |||
| 184 | $of_message .= $buf; | ||
| 185 | } | ||
| 186 | }; | ||
| 187 | |||
| 188 | if ($@) { | ||
| 189 | croak $@; | ||
| 190 | } | ||
| 191 | |||
| 192 | if (defined $xid) { | ||
| 193 | # unless ($xid == $ofp_header->{'xid'}) { | ||
| 194 | # croak q{Mismatched xid}; | ||
| 195 | # } | ||
| 196 | } | ||
| 197 | |||
| 198 | my $ret = $self->protocol->ofpt_decode(\$of_message); | ||
| 199 | |||
| 200 | return $ret; | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | =item C<< send >> | ||
| 205 | |||
| 206 | This function will send the message specified by $of_message to the file handle $fh. The file handle must have a send() function for this to work. | ||
| 207 | The IO::Socket family are the most likely use case for this function. | ||
| 208 | |||
| 209 | my $of_message = $of->protocol->ofpt_encode(0x01, q{OFPT_HELLO}, 1); | ||
| 210 | |||
| 211 | $of->send($of_message); | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub send($$) { | ||
| 216 | my ($self, $of_message) = @_; | ||
| 217 | |||
| 218 | my $ret; | ||
| 219 | |||
| 220 | eval { | ||
| 221 | local $SIG{'__DIE__'}; | ||
| 222 | local $SIG{'__WARN__'}; | ||
| 223 | |||
| 224 | $ret = $private_data->{$self}{'io_socket'}->send($of_message); | ||
| 225 | }; | ||
| 226 | |||
| 227 | if ($@) { | ||
| 228 | croak $@; | ||
| 229 | } | ||
| 230 | |||
| 231 | return $ret; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub __fixup_params { | ||
| 235 | my $self = shift; | ||
| 236 | |||
| 237 | my $ret = {}; | ||
| 238 | |||
| 239 | if ((scalar(@_) % 2) == 0) { | ||
| 240 | while (my ($key, $value) = splice(@_, 0, 2)) { | ||
| 241 | $ret->{$key} = $value; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | elsif (scalar(@_) == 1) { | ||
| 245 | my $param = $_[0]; | ||
| 246 | |||
| 247 | my $ref_type = ref($param); | ||
| 248 | |||
| 249 | if ($ref_type eq q{HASH}) { | ||
| 250 | $ret = $param; | ||
| 251 | } | ||
| 252 | elsif ($ref_type eq q{ARRAY}) { | ||
| 253 | $ret = $self->__fixup_params(@{$param}); | ||
| 254 | } | ||
| 255 | else { | ||
| 256 | $ret->{'version'} = $param; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | else { | ||
| 260 | croak q{Invalid parameters}; | ||
| 261 | } | ||
| 262 | |||
| 263 | return $ret; | ||
| 264 | } | ||
| 265 | |||
| 266 | 1; | ||
| 267 | |||
| 268 | =back | ||
| 269 | |||
| 270 | =cut | ||
| 271 |