Skip to content

Query switch information (ruby)

sun-lei edited this page Apr 25, 2011 · 3 revisions

Query switch information

Ruby In this tutorial, we develop an application to query switch information in more detail. The information that can be got from an OpenFlow switch is defined in the OpenFlow specification as follows.

  • datapath ID
  • max packets buffered at once
  • number of flow tables
  • capbility
  • OpenFlow actions supported
  • information of physical ports

Get switch information

In order to get the information of an OpenFlow switch, you should send the features request message to it.

# create a features request message and send it out
send_message FeaturesRequest.new, datapath_id

When is the proper time to send a feature request message? The answer is after the preparation of OpenFlow switch is completed. You can send the message inside the event handler function of switch_ready, which we developed in the previous tutorial.

def switch_ready datapath_id
  send_message FeaturesRequest.new, datapath_id
end

To handle the reply of features request, we should create a features_reply event handler function. A possible sample code of a features_reply event handler function is as follows, in which all information is printed out by info() function.

def features_reply message
  info "datapath_id: %#x" % message.datapath_id
  info "transaction_id: %#x" % message.transaction_id
  info "n_buffers: %u" % message.n_buffers
  info "n_tables: %u" % message.n_tables
  info "capabilities: %u" % message.capabilities
  info "actions: %u" % message.actions
  info "ports: %s" % message.ports.collect { | each | each.number }.sort.join( ", " )
end

The final code is as follows.

class SwitchInfoController < Controller
  def switch_ready datapath_id
    send_message FeaturesRequest.new, datapath_id
  end


  def features_reply message
    info "datapath_id: %#x" % message.datapath_id
    info "transaction_id: %#x" % message.transaction_id
    info "n_buffers: %u" % message.n_buffers
    info "n_tables: %u" % message.n_tables
    info "capabilities: %u" % message.capabilities
    info "actions: %u" % message.actions
    info "ports: %s" % message.ports.collect { | each | each.number }.sort.join( ", " )
  end
end

Execution

OK, let us run it.

% ./trema
trema> vswitch { datapath_id "0xabc" }
trema> app { path "./switch_info.rb" }
trema> run
datapath_id: 0xabc
transaction_id: 0x27aa0001
n_buffers: 256
n_tables: 2
capabilities: 135
actions: 2047
ports: 65534

The information of the virtual swicth is shown as above, the datapath_id should be 0xabc if it works well. Please note that the number of port is 1 (shown as 65534), and the port is used to control the OpenFlow switch.

Increasing the number of ports

Let us have more fun! First, increase the port number and then confirm the information from the newly added port. As following code shows, we first add a switch 0xdef. Then we connect the previous switch with the new one.

trema> vswitch { datapath_id "0xdef" }
trema> link "0xabc", "0xdef"  # switch 0xabc is connected to a port of switch 0xdef

The connection is completed by link, whose synax is as follows.

trema> link switch_name_1, switch_name_2

When the code is executed, each switch consists of two ports: one is connected to the controller and the other is connected the other switch.

trema> run
datapath_id: 0xdef
transaction_id: 0x7200002
n_buffers: 256
n_tables: 2
capabilities: 135
actions: 2047
ports: 1, 65534  # it has 2 ports
datapath_id: 0xabc
transaction_id: 0x7200001
n_buffers: 256
n_tables: 2
capabilities: 135
actions: 2047
ports: 1, 65534  # it has 2 ports

Summary

In this tutorial, we have developed an application to query the information of an OpenFlow switch. What we learned is summarized as follows.

  • In order to get the information of an OpenFlow switch, you should send features request message first
  • <FeaturesRequest.new is in charge of creating features reqeust messages
  • send_message() is used to send messages
  • features_reply() is handling features reply messages
  • How to use link to create a link between switches