1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 Isaku Yamahata <yamahata at private email ne jp>
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 from ryu.lib.packet import ethernet
18 from ryu.lib.packet import vlan
19 from ryu.ofproto import ether
20 from ryu.topology import api as topo_api
23 def may_add_vlan(packet, vlan_id):
25 :type packet: ryu.lib.packet.packet.Packet
27 :type vlan_id: int (0 <= vlan_id <= 4095) or None (= No VLAN)
33 e = packet.protocols[0]
34 assert isinstance(e, ethernet.ethernet)
35 v = vlan.vlan(0, 0, vlan_id, e.ethertype)
36 e.ethertype = ether.ETH_TYPE_8021Q
37 packet.add_protocol(v)
40 def get_dp(app, dpid):
42 :type dpid: datapath id
44 :rtype: ryu.controller.controller.Datapath
45 :returns: datapath corresponding to dpid
47 switches = topo_api.get_switch(app, dpid)
50 assert len(switches) == 1
54 def dp_packet_out(dp, port_no, data):
57 ofproto_parser = dp.ofproto_parser
58 actions = [ofproto_parser.OFPActionOutput(port_no,
59 ofproto.OFPCML_NO_BUFFER)]
60 packet_out = ofproto_parser.OFPPacketOut(
61 dp, 0xffffffff, ofproto.OFPP_CONTROLLER, actions, data)
62 dp.send_msg(packet_out)
65 def dp_flow_mod(dp, table, command, priority, match, instructions,
69 ofproto_parser = dp.ofproto_parser
71 out_port = ofproto.OFPP_ANY
72 flow_mod = ofproto_parser.OFPFlowMod(
73 dp, 0, 0, table, command, 0, 0,
74 priority, 0xffffffff, out_port, ofproto.OFPG_ANY,
75 ofproto.OFPFF_CHECK_OVERLAP, match, instructions)