backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / integrated / test_vrrp_multi.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 # Copyright (C) 2013 Isaku Yamahata <yamahata at valinux co 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 r"""
18 Usage:
19 PYTHONPATH=. ./bin/ryu-manager --verbose \
20              ryu.topology.switches \
21              ryu.tests.integrated.test_vrrp_multi \
22              ryu.services.protocols.vrrp.dumper
23
24 ryu.services.protocols.vrrp.dumper is optional.
25
26          +---+          ----------------
27       /--|OVS|<--veth-->|              |
28    Ryu   +---+          | linux bridge |<--veth--> command to generate packets
29       \--|OVS|<--veth-->|              |
30          +---+          ----------------
31
32 configure OVSs to connect ryu
33 example
34 # brctl addbr b0
35 # ip link add veth0-ovs type veth peer name veth0-br
36 # ip link add veth1-ovs type veth peer name veth1-br
37 # brctl addif b0 veth0-br
38 # brctl addif b0 veth1-br
39 # brctl show
40 bridge name     bridge id               STP enabled     interfaces
41 b0              8000.6642e5822497       no              veth0-br
42                                                         veth1-br
43 ovs-system              0000.122038293b55       no
44
45 # ovs-vsctl add-br s0
46 # ovs-vsctl add-port s0 veth0-ovs
47 # ovs-vsctl add-br s1
48 # ovs-vsctl add-port s1 veth1-ovs
49 # ovs-vsctl set-controller s0 tcp:127.0.0.1:6633
50 # ovs-vsctl set bridge s0 protocols='[OpenFlow12]'
51 # ovs-vsctl set-controller s1 tcp:127.0.0.1:6633
52 # ovs-vsctl set bridge s1 protocols='[OpenFlow12]'
53 # ovs-vsctl show
54 20c2a046-ae7e-4453-a576-11034db24985
55     Manager "ptcp:6634"
56     Bridge "s0"
57         Controller "tcp:127.0.0.1:6633"
58             is_connected: true
59         Port "veth0-ovs"
60             Interface "veth0-ovs"
61         Port "s0"
62             Interface "s0"
63                 type: internal
64     Bridge "s1"
65         Controller "tcp:127.0.0.1:6633"
66             is_connected: true
67         Port "veth1-ovs"
68             Interface "veth1-ovs"
69         Port "s1"
70             Interface "s1"
71                 type: internal
72     ovs_version: "1.9.90"
73 # ip link veth0-br set up
74 # ip link veth0-ovs set up
75 # ip link veth1-br set up
76 # ip link veth1-ovs set up
77 # ip link b0 set up
78 """
79
80 from ryu.base import app_manager
81 from ryu.controller import handler
82 from ryu.lib import dpid as lib_dpid
83 from ryu.lib import hub
84 from ryu.lib.packet import vrrp
85 from ryu.services.protocols.vrrp import api as vrrp_api
86 from ryu.services.protocols.vrrp import event as vrrp_event
87 from ryu.services.protocols.vrrp import monitor_openflow
88 from ryu.topology import event as topo_event
89 from ryu.topology import api as topo_api
90
91 from . import vrrp_common
92
93
94 class VRRPConfigApp(vrrp_common.VRRPCommon):
95     _IFNAME0 = 0
96     _IFNAME1 = 1
97
98     def __init__(self, *args, **kwargs):
99         super(VRRPConfigApp, self).__init__(*args, **kwargs)
100         self.start_main = False
101
102     @handler.set_ev_cls(topo_event.EventSwitchEnter)
103     def _switch_enter_handler(self, ev):
104         if self.start_main:
105             return
106
107         switches = topo_api.get_switch(self)
108         if len(switches) < 2:
109             return
110
111         self.start_main = True
112         app_mgr = app_manager.AppManager.get_instance()
113         self.logger.debug('%s', app_mgr.applications)
114         self.switches = app_mgr.applications['switches']
115         hub.spawn(self._main)
116
117     def _configure_vrrp_router(self, vrrp_version, priority,
118                                ip_addr, switch_index, vrid):
119         switches = self.switches
120         self.logger.debug('%s', switches.dps)
121         dpid = sorted(switches.dps.keys())[switch_index]
122         self.logger.debug('%s', lib_dpid.dpid_to_str(dpid))
123         self.logger.debug('%s', switches.port_state)
124         # hack: use the smallest port no to avoid picking OVS local port
125         port_no = sorted(switches.port_state[dpid].keys())[0]
126         self.logger.debug('%d', port_no)
127         port = switches.port_state[dpid][port_no]
128         self.logger.debug('%s', port)
129         mac = port.hw_addr
130         self.logger.debug('%s', mac)
131
132         interface = vrrp_event.VRRPInterfaceOpenFlow(
133             mac, ip_addr, None, dpid, port_no)
134         self.logger.debug('%s', interface)
135
136         config = vrrp_event.VRRPConfig(
137             version=vrrp_version, vrid=vrid, priority=priority,
138             ip_addresses=[ip_addr])
139         self.logger.debug('%s', config)
140
141         rep = vrrp_api.vrrp_config(self, interface, config)
142         self.logger.debug('%s', rep)
143         return rep