API usage examples

We are only adding APIs for the query and flow modification with the rest of the code not changed, so the steps for launching Mininet and Ryu are identical. What has changed is that now we see the WSGI server started on port 8080 and the two switches registered as datapath:

(4568) wsgi starting up on http://0.0.0.0:8080
...
DPSET: register datapath <ryu.controller.controller.Datapath object at 0x7f83556866d0>
DPSET: register datapath <ryu.controller.controller.Datapath object at 0x7f8355686b10>

We can now use the API to query for existing switches:

$ http GET http://localhost:8080/network/switches
HTTP/1.1 200 OK
Content-Length: 6
Content-Type: application/json; charset=UTF-8
Date: <skip>
[
1,
2
]

Then we can use the more specific URL to query switch descriptions:

$ http GET http://localhost:8080/network/desc/1
...
{
"1": {
"dp_desc": "None",
"hw_desc": "Open vSwitch",
"mfr_desc": "Nicira, Inc.",
"serial_num": "None",
"sw_desc": "2.3.90"
}
}

$ http GET http://localhost:8080/network/desc/2
...
{
"2": {
"dp_desc": "None",
"hw_desc": "Open vSwitch",
"mfr_desc": "Nicira, Inc.",
"serial_num": "None",
"sw_desc": "2.3.90"
}
}

At this point, the switch should only have one flow for sending flow misses to the controller, which we can verify using the API as well:

$ http GET http://localhost:8080/network/flow/1
HTTP/1.1 200 OK
Content-Length: 251
Content-Type: application/json; charset=UTF-8
Date: <skip>

{
"1": [
{
"actions": [
"OUTPUT:CONTROLLER"
],
"byte_count": 1026,
"cookie": 0,
"duration_nsec": 703000000,
"duration_sec": 48,
"flags": 0,
"hard_timeout": 0,
"idle_timeout": 0,
"length": 80,
"match": {},
"packet_count": 13,
"priority": 0,
"table_id": 0
}
]
}

We will then be able to add the flow to s1 with the API:

curl -H "Content-Type: application/json" 
-X POST -d '{"dpid":"1", "priority":"1", "match": {"in_port":"1", "dl_type":"2048", "priority":"2", "nw_src":"192.168.1.0/24", "nw_dst":"192.168.2.0/24"}, "actions":[{"type":"OUTPUT","port":"2"}]}' http://localhost:8080/network/flowentry/add

curl -H "Content-Type: application/json"
-X POST -d '{"dpid":"1", "priority":"1", "match": {"in_port":"2", "dl_type":"2048", "nw_src":"192.168.2.0/24", "nw_dst":"192.168.1.0/24"}, "actions":[{"type":"OUTPUT","port":"1"}]}' http://localhost:8080/network/flowentry/add

We can repeat the process for s2:

curl -H "Content-Type: application/json" 
-X POST -d '{"dpid":"2", "priority":"1", "match": {"in_port":"2", "dl_type":"2048", "nw_src":"192.168.1.0/24", "nw_dst":"192.168.2.0/24"}, "actions":[{"type":"OUTPUT","port":"1"}]}' http://localhost:8080/network/flowentry/add

curl -H "Content-Type: application/json"
-X POST -d '{"dpid":"2", "priority":"1", "match": {"in_port":"1", "dl_type":"2048", "nw_src":"192.168.2.0/24", "nw_dst":"192.168.1.0/24"}, "actions":[{"type":"OUTPUT","port":"2"}]}' http://localhost:8080/network/flowentry/add

We can use the same API for verifying flow entries. If you ever need to delete flows, you can simply change add to delete, as follows:

curl -H "Content-Type: application/json" 
-X POST -d '{"dpid":"1", "priority":"1", "match": {"in_port":"1", "dl_type":"2048", "priority":"2", "nw_src":"192.168.1.0/24", "nw_dst":"192.168.2.0/24"}, "actions":[{"type":"OUTPUT","port":"2"}]}' http://localhost:8080/network/flowentry/delete

The last step is to once again verify that the ping packets work:

mininet> h1 ping -c 2 h2
PING 192.168.2.10 (192.168.2.10) 56(84) bytes of data.
64 bytes from 192.168.2.10: icmp_seq=1 ttl=64 time=9.25 ms
64 bytes from 192.168.2.10: icmp_seq=2 ttl=64 time=0.079 ms

--- 192.168.2.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.079/4.664/9.250/4.586 ms

# Ryu application console output for ARP only with the flow match for
# ICMP packets.
Received ARP for 192.168.1.1
('ethernet', ethernet(dst='ff:ff:ff:ff:ff:ff',ethertype=2054,src='00:00:00:00:00:01'))
('arp', arp(dst_ip='192.168.1.1',dst_mac='00:00:00:00:00:00',hlen=6,hwtype=1,opcode=1,plen=4,proto=2048,src_ip='192.168.1.10',src_mac='00:00:00:00:00:01'))
datapath: 1 in_port: 1
EVENT ofp_event->MySimpleRestRouter EventOFPPacketIn
Received ARP for 192.168.2.1
('ethernet', ethernet(dst='ff:ff:ff:ff:ff:ff',ethertype=2054,src='00:00:00:00:00:02'))
('arp', arp(dst_ip='192.168.2.1',dst_mac='00:00:00:00:00:00',hlen=6,hwtype=1,opcode=1,plen=4,proto=2048,src_ip='192.168.2.10',src_mac='00:00:00:00:00:02'))
datapath: 2 in_port: 1

The OpenFlow REST API is one of the most useful features in the Ryu framework. It is widely used in many of the Ryu application examples and production software. In the next section, we will take a look at using the BGP library for the Ryu controller.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.69