second try
[vsorcdistro/.git] / ryu / doc / source / test-vrrp.rst
1 ===================
2 Testing VRRP Module
3 ===================
4
5 This page describes how to test Ryu VRRP service
6
7 Running integrated tests
8 ========================
9
10 Some testing scripts are available.
11
12 * ryu/tests/integrated/test_vrrp_linux_multi.py
13 * ryu/tests/integrated/test_vrrp_multi.py
14
15 Each files include how to run in the comment.
16 Please refer to it.
17
18
19 Running multiple Ryu VRRP in network namespace
20 ==============================================
21
22 The following command lines set up necessary bridges and interfaces.
23
24 And then run RYU-VRRP::
25
26     # ip netns add gateway1
27     # ip netns add gateway2
28
29     # brctl addbr vrrp-br0
30     # brctl addbr vrrp-br1
31
32     # ip link add veth0 type veth peer name veth0-br0
33     # ip link add veth1 type veth peer name veth1-br0
34     # ip link add veth2 type veth peer name veth2-br0
35     # ip link add veth3 type veth peer name veth3-br1
36     # ip link add veth4 type veth peer name veth4-br1
37     # ip link add veth5 type veth peer name veth5-br1
38
39     # brctl addif vrrp-br0 veth0-br0
40     # brctl addif vrrp-br0 veth1-br0
41     # brctl addif vrrp-br0 veth2-br0
42     # brctl addif vrrp-br1 veth3-br1
43     # brctl addif vrrp-br1 veth4-br1
44     # brctl addif vrrp-br1 veth5-br1
45
46     # ip link set vrrp-br0 up
47     # ip link set vrrp-br1 up
48
49     # ip link set veth0 up
50     # ip link set veth0-br0 up
51     # ip link set veth1-br0 up
52     # ip link set veth2-br0 up
53     # ip link set veth3-br1 up
54     # ip link set veth4-br1 up
55     # ip link set veth5 up
56     # ip link set veth5-br1 up
57
58     # ip link set veth1 netns gateway1
59     # ip link set veth2 netns gateway2
60     # ip link set veth3 netns gateway1
61     # ip link set veth4 netns gateway2
62
63     # ip netns exec gateway1 ip link set veth1 up
64     # ip netns exec gateway2 ip link set veth2 up
65     # ip netns exec gateway1 ip link set veth3 up
66     # ip netns exec gateway2 ip link set veth4 up
67
68     # ip netns exec gateway1 .ryu-vrrp veth1 '10.0.0.2' 254
69     # ip netns exec gateway2 .ryu-vrrp veth2 '10.0.0.3' 100
70
71
72 .. admonition:: Caveats
73
74    Please make sure that all interfaces and bridges are UP.
75    Don't forget interfaces in netns gateway1/gateway2.
76
77 ::
78
79                     ^ veth5
80                     |
81                     V veth5-br1
82             -----------------------
83             |Linux Brirge vrrp-br1|
84             -----------------------
85      veth3-br1^            ^ veth4-br1
86               |            |
87          veth3V            V veth4
88          ----------       ----------
89          |netns   |       |netns   |
90          |gateway1|       |gateway2|
91          |ryu-vrrp|       |ryu-vrrp|
92          ----------       ----------
93          veth1^            ^ veth2
94               |            |
95      veth1-br0V            V veth2-br0
96             -----------------------
97             |Linux Brirge vrrp-br0|
98             -----------------------
99                     ^ veth0-br0
100                     |
101                     V veth0
102
103
104
105 Here's the helper executable, ryu-vrrp::
106
107     #!/usr/bin/env python
108     #
109     # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
110     # Copyright (C) 2013 Isaku Yamahata <yamahata at valinux co jp>
111     #
112     # Licensed under the Apache License, Version 2.0 (the "License");
113     # you may not use this file except in compliance with the License.
114     # You may obtain a copy of the License at
115     #
116     #    http://www.apache.org/licenses/LICENSE-2.0
117     #
118     # Unless required by applicable law or agreed to in writing, software
119     # distributed under the License is distributed on an "AS IS" BASIS,
120     # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
121     # implied.
122     # See the License for the specific language governing permissions and
123     # limitations under the License.
124     
125     from ryu.lib import hub
126     hub.patch()
127     
128     # TODO:
129     #   Right now, we have our own patched copy of ovs python bindings
130     #   Once our modification is upstreamed and widely deployed,
131     #   use it
132     #
133     # NOTE: this modifies sys.path and thus affects the following imports.
134     # eg. oslo.config.cfg.
135     import ryu.contrib
136     
137     from oslo.config import cfg
138     import logging
139     import netaddr
140     import sys
141     import time
142     
143     from ryu import log
144     log.early_init_log(logging.DEBUG)
145     
146     from ryu import flags
147     from ryu import version
148     from ryu.base import app_manager
149     from ryu.controller import controller
150     from ryu.lib import mac as lib_mac
151     from ryu.lib.packet import vrrp
152     from ryu.services.protocols.vrrp import api as vrrp_api
153     from ryu.services.protocols.vrrp import event as vrrp_event
154     
155     
156     CONF = cfg.CONF
157     
158     _VRID = 7
159     _IP_ADDRESS = '10.0.0.1'
160     _PRIORITY = 100
161     
162     
163     class VRRPTestRouter(app_manager.RyuApp):
164         def __init__(self, *args, **kwargs):
165             super(VRRPTestRouter, self).__init__(*args, **kwargs)
166             print args
167             self.logger.debug('vrrp_config %s', args)
168             self._ifname = args[0]
169             self._primary_ip_address = args[1]
170             self._priority = int(args[2])
171     
172         def start(self):
173             print 'start'
174             hub.spawn(self._main)
175     
176         def _main(self):
177             print self
178             interface = vrrp_event.VRRPInterfaceNetworkDevice(
179                 lib_mac.DONTCARE, self._primary_ip_address, None, self._ifname)
180             self.logger.debug('%s', interface)
181     
182             ip_addresses = [_IP_ADDRESS]
183             config = vrrp_event.VRRPConfig(
184                 version=vrrp.VRRP_VERSION_V3, vrid=_VRID, priority=self._priority,
185                 ip_addresses=ip_addresses)
186             self.logger.debug('%s', config)
187     
188             rep = vrrp_api.vrrp_config(self, interface, config)
189             self.logger.debug('%s', rep)
190     
191     
192     def main():
193         vrrp_config = sys.argv[-3:]
194         sys.argv = sys.argv[:-3]
195         CONF(project='ryu', version='ryu-vrrp %s' % version)
196     
197         log.init_log()
198         # always enable ofp for now.
199         app_lists = ['ryu.services.protocols.vrrp.manager',
200                      'ryu.services.protocols.vrrp.dumper',
201                      'ryu.services.protocols.vrrp.sample_manager']
202     
203         app_mgr = app_manager.AppManager.get_instance()
204         app_mgr.load_apps(app_lists)
205         contexts = app_mgr.create_contexts()
206         app_mgr.instantiate_apps(**contexts)
207         vrrp_router = app_mgr.instantiate(VRRPTestRouter, *vrrp_config, **contexts)
208         vrrp_router.start()
209     
210         while True:
211             time.sleep(999999)
212     
213         app_mgr.close()
214     
215     
216     if __name__ == "__main__":
217         main()