backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / vrrp / utils.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 Isaku Yamahata <yamahata at private email ne jp>
3 #
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
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
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
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
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
21
22
23 def may_add_vlan(packet, vlan_id):
24     """
25     :type packet: ryu.lib.packet.packet.Packet
26     :param packet:
27     :type vlan_id: int (0 <= vlan_id <= 4095) or None (= No VLAN)
28     :param vlan_id:
29     """
30     if vlan_id is None:
31         return
32
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)
38
39
40 def get_dp(app, dpid):
41     """
42     :type dpid: datapath id
43     :param dpid:
44     :rtype: ryu.controller.controller.Datapath
45     :returns: datapath corresponding to dpid
46     """
47     switches = topo_api.get_switch(app, dpid)
48     if not switches:
49         return None
50     assert len(switches) == 1
51     return switches[0].dp
52
53
54 def dp_packet_out(dp, port_no, data):
55     # OF 1.2
56     ofproto = dp.ofproto
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)
63
64
65 def dp_flow_mod(dp, table, command, priority, match, instructions,
66                 out_port=None):
67     # OF 1.2
68     ofproto = dp.ofproto
69     ofproto_parser = dp.ofproto_parser
70     if out_port is None:
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)
76     dp.send_msg(flow_mod)