1 # Copyright (c) 2011 Nicira Networks
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 rpcserver is an XML RPC server that allows RPC client to initiate tests
19 from twisted.internet import reactor
20 from twisted.web import xmlrpc, server
21 from twisted.internet.error import CannotListenError
28 class TestArena(xmlrpc.XMLRPC):
30 This class contains all the functions that ovstest will call
31 remotely. The caller is responsible to use designated handleIds
32 for designated methods (e.g. do not mix UDP and TCP handles).
36 xmlrpc.XMLRPC.__init__(self)
40 def __acquire_handle(self, value):
42 Allocates new handle and assigns value object to it
44 handle = self.handle_id
45 self.handle_map[handle] = value
49 def __get_handle_resources(self, handle):
51 Return resources that were assigned to handle
53 return self.handle_map[handle]
55 def __delete_handle(self, handle):
57 Releases handle from handle_map
59 del self.handle_map[handle]
62 def xmlrpc_create_udp_listener(self, port):
64 Creates a UDP listener that will receive packets from UDP sender
67 listener = udp.UdpListener()
68 reactor.listenUDP(port, listener)
69 handle_id = self.__acquire_handle(listener)
70 except CannotListenError:
74 def xmlrpc_create_udp_sender(self, host, count, size, duration):
76 Send UDP datagrams to UDP listener
78 sender = udp.UdpSender(tuple(host), count, size, duration)
79 reactor.listenUDP(0, sender)
80 handle_id = self.__acquire_handle(sender)
83 def xmlrpc_get_udp_listener_results(self, handle):
85 Returns number of datagrams that were received
87 listener = self.__get_handle_resources(handle)
88 return listener.getResults()
90 def xmlrpc_get_udp_sender_results(self, handle):
92 Returns number of datagrams that were sent
94 sender = self.__get_handle_resources(handle)
95 return sender.getResults()
97 def xmlrpc_close_udp_listener(self, handle):
99 Releases UdpListener and all its resources
101 listener = self.__get_handle_resources(handle)
102 listener.transport.stopListening()
103 self.__delete_handle(handle)
106 def xmlrpc_close_udp_sender(self, handle):
108 Releases UdpSender and all its resources
110 sender = self.__get_handle_resources(handle)
111 sender.transport.stopListening()
112 self.__delete_handle(handle)
115 def xmlrpc_create_tcp_listener(self, port):
117 Creates a TcpListener that will accept connection from TcpSender
120 listener = tcp.TcpListenerFactory()
121 port = reactor.listenTCP(port, listener)
122 handle_id = self.__acquire_handle((listener, port))
124 except CannotListenError:
127 def xmlrpc_create_tcp_sender(self, his_ip, his_port, duration):
129 Creates a TcpSender that will connect to TcpListener
131 sender = tcp.TcpSenderFactory(duration)
132 connector = reactor.connectTCP(his_ip, his_port, sender)
133 handle_id = self.__acquire_handle((sender, connector))
136 def xmlrpc_get_tcp_listener_results(self, handle):
138 Returns number of bytes received
140 (listener, _) = self.__get_handle_resources(handle)
141 return listener.getResults()
143 def xmlrpc_get_tcp_sender_results(self, handle):
145 Returns number of bytes sent
147 (sender, _) = self.__get_handle_resources(handle)
148 return sender.getResults()
150 def xmlrpc_close_tcp_listener(self, handle):
152 Releases TcpListener and all its resources
155 (_, port) = self.__get_handle_resources(handle)
156 port.loseConnection()
157 self.__delete_handle(handle)
158 except exceptions.KeyError:
162 def xmlrpc_close_tcp_sender(self, handle):
164 Releases TcpSender and all its resources
167 (_, connector) = self.__get_handle_resources(handle)
168 connector.disconnect()
169 self.__delete_handle(handle)
170 except exceptions.KeyError:
175 def xmlrpc_get_interface(self, address):
177 Finds first interface that has given address
179 return util.get_interface(address)
181 def xmlrpc_get_interface_mtu(self, iface):
183 Returns MTU of the given interface
185 return util.get_interface_mtu(iface)
187 def xmlrpc_uname(self):
189 Return information about running kernel
193 def xmlrpc_get_driver(self, iface):
195 Returns driver version
197 return util.get_driver(iface)
200 def start_rpc_server(port):
201 RPC_SERVER = TestArena()
202 reactor.listenTCP(port, server.Site(RPC_SERVER))