资料链接: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
1 #! /bin/python 2 3 import re 4 import urllib 5 import urllib2 6 import json 7 import requests 8 import json 9 import time 10 11 # get switch dpid 12 post_url = 'http://127.0.0.1:8080/stats/switches'; 13 req = urllib2.Request(post_url) 14 response = urllib2.urlopen(req) 15 dpid_data = response.read().strip('[]') 16 17 # show switch group 18 post_url = 'http://127.0.0.1:8080/stats/groupdesc/%s' % dpid_data; 19 req = urllib2.Request(post_url) 20 response = urllib2.urlopen(req) 21 print response.read() 22 23 # add switch group 24 post_url = 'http://127.0.0.1:8080/stats/groupentry/add'; 25 group_data = { 26 "dpid": dpid_data, 27 "type": "SELECT", 28 "fields": "ip_src", 29 "group_id": 1, 30 "buckets": [{ 31 "weight": 1, 32 "actions": [{ 33 "type": "OUTPUT", 34 "port": 1 35 }] 36 }, 37 { 38 "weight": 1, 39 "actions": [{ 40 "type": "OUTPUT", 41 "port": 2 42 }] 43 }, 44 { 45 "weight": 1, 46 "actions": [{ 47 "type": "OUTPUT", 48 "port": 3 49 }] 50 }] 51 } 52 data = json.dumps(group_data) 53 req = urllib2.Request(post_url) 54 response = urllib2.urlopen(req,data=data) 55 print response.read() 56 57 # show flows 58 post_url = 'http://127.0.0.1:8080/stats/flow/%s' % dpid_data; 59 req = urllib2.Request(post_url) 60 response = urllib2.urlopen(req) 61 print response.read() 62 63 # add flow 64 post_url = 'http://127.0.0.1:8080/stats/flowentry/add'; 65 flows_data = { 66 "dpid": dpid_data, 67 "table_id": 0, 68 "cookie": 10000, 69 "priority": 1000, 70 "match":{ 71 "dl_type": "0x8000", 72 "in_port": 5, 73 "dl_vlan": 100, 74 #"dl_vlan": "0x1005" # Describe sum of VLAN-ID(e.g. 5) | OFPVID_PRESENT(0x1000) 75 "eth_src": "aa:bb:cc:11:22:33", 76 "eth_dst": "aa:bb:cc:11:22:33", 77 "ipv4_dst": "192.168.10.10/255.255.255.0", "eth_type": 2048, 78 "ipv4_src": "192.168.0.1", "eth_type": 2048 79 }, 80 "actions":[ 81 { 82 "type": "PUSH_VLAN", 83 "ethertype": 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame 84 }, 85 { 86 "type": "SET_FIELD", 87 "field": "vlan_vid", 88 "value": 4102 # Describe sum of vlan_id(e.g. 6) | OFPVID_PRESENT(0x1000=4096) 89 }, 90 { 91 "type": "OUTPUT", 92 "port": 20 93 } 94 ] 95 } 96 data = json.dumps(flows_data) 97 req = urllib2.Request(post_url) 98 response = urllib2.urlopen(req,data=data) 99 print response.read()