资料链接:http://ryu.readthedocs.io/en/latest/app/ofctl_rest.html
运行ryu进程:
ryu-manager --ofp-tcp-listen-port 6655 --wsapi-port 8080 --verbose /usr/lib/python2.7/site-packages/ryu/app/ofctl_rest.py
#! /bin/python import re
import urllib
import urllib2
import json
import requests
import json
import time # get switch dpid
post_url = 'http://127.0.0.1:8080/stats/switches';
req = urllib2.Request(post_url)
response = urllib2.urlopen(req)
dpid_data = response.read().strip('[]') # show switch group
post_url = 'http://127.0.0.1:8080/stats/groupdesc/%s' % dpid_data;
req = urllib2.Request(post_url)
response = urllib2.urlopen(req)
print response.read() # add switch group
post_url = 'http://127.0.0.1:8080/stats/groupentry/add';
group_data = {
"dpid": dpid_data,
"type": "SELECT",
"fields": "ip_src",
"group_id": 1,
"buckets": [{
"weight": 1,
"actions": [{
"type": "OUTPUT",
"port": 1
}]
},
{
"weight": 1,
"actions": [{
"type": "OUTPUT",
"port": 2
}]
},
{
"weight": 1,
"actions": [{
"type": "OUTPUT",
"port": 3
}]
}]
}
data = json.dumps(group_data)
req = urllib2.Request(post_url)
response = urllib2.urlopen(req,data=data)
print response.read() # show flows
post_url = 'http://127.0.0.1:8080/stats/flow/%s' % dpid_data;
req = urllib2.Request(post_url)
response = urllib2.urlopen(req)
print response.read() # add flow
post_url = 'http://127.0.0.1:8080/stats/flowentry/add';
flows_data = {
"dpid": dpid_data,
"table_id": 0,
"cookie": 10000,
"priority": 1000,
"match":{
"dl_type": "0x8000",
"in_port": 5,
"dl_vlan": 100,
#"dl_vlan": "0x1005" # Describe sum of VLAN-ID(e.g. 5) | OFPVID_PRESENT(0x1000)
"eth_src": "aa:bb:cc:11:22:33",
"eth_dst": "aa:bb:cc:11:22:33",
"ipv4_dst": "192.168.10.10/255.255.255.0", "eth_type": 2048,
"ipv4_src": "192.168.0.1", "eth_type": 2048
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4102 # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096)
},
{
"type": "OUTPUT",
"port": 20
}
]
}
data = json.dumps(flows_data)
req = urllib2.Request(post_url)
response = urllib2.urlopen(req,data=data)
print response.read()