ia64/xen-unstable

view tools/python/xen/xend/server/messages.py @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 import sys
19 import struct
20 import types
22 from xen.lowlevel import xu
24 DEBUG = False
26 #PORT_WILDCARD = 0xefffffff
28 """Wildcard for the control message types."""
29 TYPE_WILDCARD = 0xffff
31 """ All message formats.
32 Added to incrementally for the various message types.
33 See below.
34 """
35 msg_formats = {}
37 #============================================================================
38 # Block interface message types.
39 #============================================================================
41 CMSG_BLKIF_BE = 1
42 CMSG_BLKIF_FE = 2
44 CMSG_BLKIF_FE_INTERFACE_STATUS = 0
45 CMSG_BLKIF_FE_DRIVER_STATUS = 32
46 CMSG_BLKIF_FE_INTERFACE_CONNECT = 33
47 CMSG_BLKIF_FE_INTERFACE_DISCONNECT = 34
48 CMSG_BLKIF_FE_INTERFACE_QUERY = 35
50 CMSG_BLKIF_BE_CREATE = 0
51 CMSG_BLKIF_BE_DESTROY = 1
52 CMSG_BLKIF_BE_CONNECT = 2
53 CMSG_BLKIF_BE_DISCONNECT = 3
54 CMSG_BLKIF_BE_VBD_CREATE = 4
55 CMSG_BLKIF_BE_VBD_DESTROY = 5
56 CMSG_BLKIF_BE_DRIVER_STATUS = 32
58 BLKIF_DRIVER_STATUS_DOWN = 0
59 BLKIF_DRIVER_STATUS_UP = 1
61 BLKIF_INTERFACE_STATUS_CLOSED = 0 #/* Interface doesn't exist. */
62 BLKIF_INTERFACE_STATUS_DISCONNECTED = 1 #/* Exists but is disconnected. */
63 BLKIF_INTERFACE_STATUS_CONNECTED = 2 #/* Exists and is connected. */
64 BLKIF_INTERFACE_STATUS_CHANGED = 3 #/* A device has been added or removed. */
66 BLKIF_BE_STATUS_OKAY = 0
67 BLKIF_BE_STATUS_ERROR = 1
68 BLKIF_BE_STATUS_INTERFACE_EXISTS = 2
69 BLKIF_BE_STATUS_INTERFACE_NOT_FOUND = 3
70 BLKIF_BE_STATUS_INTERFACE_CONNECTED = 4
71 BLKIF_BE_STATUS_VBD_EXISTS = 5
72 BLKIF_BE_STATUS_VBD_NOT_FOUND = 6
73 BLKIF_BE_STATUS_OUT_OF_MEMORY = 7
74 BLKIF_BE_STATUS_PHYSDEV_NOT_FOUND = 8
75 BLKIF_BE_STATUS_MAPPING_ERROR = 9
77 blkif_formats = {
78 'blkif_be_connect_t':
79 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_CONNECT),
80 # Connect be to fe (in response to blkif_fe_interface_connect_t).
82 'blkif_be_create_t':
83 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_CREATE),
84 # Create be.
86 'blkif_be_disconnect_t':
87 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_DISCONNECT),
88 # Disconnect be from fe.
90 'blkif_be_destroy_t':
91 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_DESTROY),
92 # Destroy be (after disconnect).
93 # Make be do this even if no disconnect (and destroy all vbd too).
95 'blkif_be_vbd_create_t':
96 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_VBD_CREATE),
97 # Create a vbd device.
99 'blkif_be_vbd_destroy_t':
100 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_VBD_DESTROY),
101 # Destroy a vbd.
103 # Add message to query be for state and vbds.
105 'blkif_fe_interface_status_t':
106 (CMSG_BLKIF_FE, CMSG_BLKIF_FE_INTERFACE_STATUS),
107 # Notify device status to fe.
108 # Also used to notify 'any' device change with status BLKIF_INTERFACE_STATUS_CHANGED.
110 'blkif_fe_driver_status_t':
111 (CMSG_BLKIF_FE, CMSG_BLKIF_FE_DRIVER_STATUS),
112 # Comes from fe, treated as notifying that fe has come up/changed.
113 # Xend sets be(s) to BLKIF_INTERFACE_STATUS_DISCONNECTED,
114 # sends blkif_fe_interface_status_t to fe (from each be).
115 #
116 # Reply with i/f count.
117 # The i/f sends probes (using -ve trick), we reply with the info.
119 'blkif_fe_interface_connect_t':
120 (CMSG_BLKIF_FE, CMSG_BLKIF_FE_INTERFACE_CONNECT),
121 # Comes from fe, passing shmem frame to use for be.
122 # fe sends when gets blkif_fe_interface_status_t with state NETIF_INTERFACE_STATUS_DISCONNECTED.
123 # Xend creates event channel and notifies be.
124 # Then notifies fe of event channel with blkif_fe_interface_status_t.
126 # Add message to kick fe to probe for devices.
127 # Just report new devices to fe?
129 #
130 # Add message for fe to probe a device.
131 # And probing with id -1 should return first.
132 # And probing with id -n should return first device with id > n.
134 # Add message to query fe for state and vbds.
135 }
137 msg_formats.update(blkif_formats)
139 #============================================================================
140 # Network interface message types.
141 #============================================================================
143 CMSG_NETIF_BE = 3
144 CMSG_NETIF_FE = 4
146 CMSG_NETIF_FE_INTERFACE_STATUS = 0
147 CMSG_NETIF_FE_DRIVER_STATUS = 32
148 CMSG_NETIF_FE_INTERFACE_CONNECT = 33
149 CMSG_NETIF_FE_INTERFACE_DISCONNECT = 34
150 CMSG_NETIF_FE_INTERFACE_QUERY = 35
152 CMSG_NETIF_BE_CREATE = 0
153 CMSG_NETIF_BE_DESTROY = 1
154 CMSG_NETIF_BE_CONNECT = 2
155 CMSG_NETIF_BE_DISCONNECT = 3
156 CMSG_NETIF_BE_CREDITLIMIT = 4
157 CMSG_NETIF_BE_DRIVER_STATUS = 32
159 NETIF_INTERFACE_STATUS_CLOSED = 0 #/* Interface doesn't exist. */
160 NETIF_INTERFACE_STATUS_DISCONNECTED = 1 #/* Exists but is disconnected. */
161 NETIF_INTERFACE_STATUS_CONNECTED = 2 #/* Exists and is connected. */
162 NETIF_INTERFACE_STATUS_CHANGED = 3 #/* A device has been added or removed. */
164 NETIF_DRIVER_STATUS_DOWN = 0
165 NETIF_DRIVER_STATUS_UP = 1
167 netif_formats = {
168 'netif_be_connect_t':
169 (CMSG_NETIF_BE, CMSG_NETIF_BE_CONNECT),
171 'netif_be_create_t':
172 (CMSG_NETIF_BE, CMSG_NETIF_BE_CREATE),
174 'netif_be_disconnect_t':
175 (CMSG_NETIF_BE, CMSG_NETIF_BE_DISCONNECT),
177 'netif_be_destroy_t':
178 (CMSG_NETIF_BE, CMSG_NETIF_BE_DESTROY),
180 'netif_be_creditlimit_t':
181 (CMSG_NETIF_BE, CMSG_NETIF_BE_CREDITLIMIT),
183 'netif_be_driver_status_t':
184 (CMSG_NETIF_BE, CMSG_NETIF_BE_DRIVER_STATUS),
186 'netif_fe_driver_status_t':
187 (CMSG_NETIF_FE, CMSG_NETIF_FE_DRIVER_STATUS),
189 'netif_fe_interface_connect_t':
190 (CMSG_NETIF_FE, CMSG_NETIF_FE_INTERFACE_CONNECT),
192 'netif_fe_interface_status_t':
193 (CMSG_NETIF_FE, CMSG_NETIF_FE_INTERFACE_STATUS),
194 }
196 msg_formats.update(netif_formats)
198 #============================================================================
199 # USB interface message types.
200 #============================================================================
202 CMSG_USBIF_BE = 8
203 CMSG_USBIF_FE = 9
205 CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED = 0
207 CMSG_USBIF_FE_DRIVER_STATUS_CHANGED = 32
208 CMSG_USBIF_FE_INTERFACE_CONNECT = 33
209 CMSG_USBIF_FE_INTERFACE_DISCONNECT = 34
211 USBIF_DRIVER_STATUS_DOWN = 0
212 USBIF_DRIVER_STATUS_UP = 1
214 USBIF_INTERFACE_STATUS_DESTROYED = 0 #/* Interface doesn't exist. */
215 USBIF_INTERFACE_STATUS_DISCONNECTED = 1 #/* Exists but is disconnected. */
216 USBIF_INTERFACE_STATUS_CONNECTED = 2 #/* Exists and is connected. */
218 CMSG_USBIF_BE_CREATE = 0
219 CMSG_USBIF_BE_DESTROY = 1
220 CMSG_USBIF_BE_CONNECT = 2
222 CMSG_USBIF_BE_DISCONNECT = 3
223 CMSG_USBIF_BE_CLAIM_PORT = 4
224 CMSG_USBIF_BE_RELEASE_PORT = 5
226 CMSG_USBIF_BE_DRIVER_STATUS_CHANGED = 32
228 USBIF_BE_STATUS_OKAY = 0
229 USBIF_BE_STATUS_ERROR = 1
231 USBIF_BE_STATUS_INTERFACE_EXISTS = 2
232 USBIF_BE_STATUS_INTERFACE_NOT_FOUND = 3
233 USBIF_BE_STATUS_INTERFACE_CONNECTED = 4
234 USBIF_BE_STATUS_OUT_OF_MEMORY = 7
235 USBIF_BE_STATUS_MAPPING_ERROR = 9
237 usbif_formats = {
238 'usbif_be_create_t':
239 (CMSG_USBIF_BE, CMSG_USBIF_BE_CREATE),
241 'usbif_be_destroy_t':
242 (CMSG_USBIF_BE, CMSG_USBIF_BE_DESTROY),
244 'usbif_be_connect_t':
245 (CMSG_USBIF_BE, CMSG_USBIF_BE_CONNECT),
247 'usbif_be_disconnect_t':
248 (CMSG_USBIF_BE, CMSG_USBIF_BE_DISCONNECT),
250 'usbif_be_claim_port_t':
251 (CMSG_USBIF_BE, CMSG_USBIF_BE_CLAIM_PORT),
253 'usbif_be_release_port_t':
254 (CMSG_USBIF_BE, CMSG_USBIF_BE_RELEASE_PORT),
256 'usbif_fe_interface_status_changed_t':
257 (CMSG_USBIF_FE, CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED),
259 'usbif_fe_driver_status_changed_t':
260 (CMSG_USBIF_FE, CMSG_USBIF_FE_DRIVER_STATUS_CHANGED),
262 'usbif_fe_interface_connect_t':
263 (CMSG_USBIF_FE, CMSG_USBIF_FE_INTERFACE_CONNECT),
265 'usbif_fe_interface_disconnect_t':
266 (CMSG_USBIF_FE, CMSG_USBIF_FE_INTERFACE_DISCONNECT),
268 }
270 msg_formats.update(usbif_formats)
272 #============================================================================
273 # Domain shutdown message types.
274 #============================================================================
276 CMSG_SHUTDOWN = 6
278 CMSG_SHUTDOWN_POWEROFF = 0
279 CMSG_SHUTDOWN_REBOOT = 1
280 CMSG_SHUTDOWN_SUSPEND = 2
281 CMSG_SHUTDOWN_SYSRQ = 3
283 STOPCODE_shutdown = 0
284 STOPCODE_reboot = 1
285 STOPCODE_suspend = 2
286 STOPCODE_sysrq = 3
288 shutdown_formats = {
289 'shutdown_poweroff_t':
290 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_POWEROFF),
292 'shutdown_reboot_t':
293 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_REBOOT),
295 'shutdown_suspend_t':
296 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_SUSPEND),
298 'shutdown_sysrq_t':
299 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_SYSRQ)
300 }
302 msg_formats.update(shutdown_formats)
304 #============================================================================
305 # Domain memory reservation message.
306 #============================================================================
308 CMSG_MEM_REQUEST = 7
309 CMSG_MEM_REQUEST_SET = 0
311 mem_request_formats = {
312 'mem_request_t':
313 (CMSG_MEM_REQUEST, CMSG_MEM_REQUEST_SET)
314 }
316 msg_formats.update(mem_request_formats)
318 #============================================================================
319 # Domain vcpu hotplug message.
320 #============================================================================
322 CMSG_VCPU_HOTPLUG = 10
323 CMSG_VCPU_HOTPLUG_OFF = 0
324 CMSG_VCPU_HOTPLUG_ON = 1
326 vcpu_hotplug_formats = {
327 'vcpu_hotplug_off_t':
328 (CMSG_VCPU_HOTPLUG, CMSG_VCPU_HOTPLUG_OFF),
330 'vcpu_hotplug_on_t':
331 (CMSG_VCPU_HOTPLUG, CMSG_VCPU_HOTPLUG_ON)
332 }
334 msg_formats.update(vcpu_hotplug_formats)
336 #============================================================================
337 class Msg:
338 pass
340 _next_msgid = 0
342 def nextid():
343 """Generate the next message id.
345 @return: message id
346 @rtype: int
347 """
348 global _next_msgid
349 _next_msgid += 1
350 return _next_msgid
352 def packMsg(ty, params):
353 """Pack a message.
354 Any I{mac} parameter is passed in as an int[6] array and converted.
356 @param ty: message type name
357 @type ty: string
358 @param params: message parameters
359 @type params: dicy
360 @return: message
361 @rtype: xu message
362 """
363 msgid = nextid()
364 if DEBUG: print '>packMsg', msgid, ty, params
365 (major, minor) = msg_formats[ty]
366 args = {}
367 for (k, v) in params.items():
368 if k in ['mac', 'be_mac']:
369 for i in range(0, 6):
370 args['%s[%d]' % (k, i)] = v[i]
371 else:
372 args[k] = v
373 msg = xu.message(major, minor, msgid, args)
374 if DEBUG: print '<packMsg', msg.get_header()['id'], ty, args
375 return msg
377 def unpackMsg(ty, msg):
378 """Unpack a message.
379 Any mac addresses in the message are converted to int[6] array
380 in the return dict.
382 @param ty: message type
383 @type ty: string
384 @param msg: message
385 @type msg: xu message
386 @return: parameters
387 @rtype: dict
388 """
389 args = msg.get_payload()
390 if DEBUG: print '>unpackMsg', args
391 if isinstance(args, types.StringType):
392 args = {'value': args}
393 else:
394 mac = [0, 0, 0, 0, 0, 0]
395 macs = []
396 for (k, v) in args.items():
397 if k.startswith('mac['):
398 macs.append(k)
399 i = int(k[4:5])
400 mac[i] = v
401 else:
402 pass
403 if macs:
404 args['mac'] = mac
405 #print 'macs=', macs
406 #print 'args=', args
407 for k in macs:
408 del args[k]
409 if DEBUG:
410 msgid = msg.get_header()['id']
411 print '<unpackMsg', msgid, ty, args
412 return args
414 def msgTypeName(ty, subty):
415 """Convert a message type, subtype pair to a message type name.
417 @param ty: message type
418 @type ty: int
419 @param subty: message subtype
420 @type ty: int
421 @return: message type name (or None)
422 @rtype: string or None
423 """
424 for (name, info) in msg_formats.items():
425 if info[0] == ty and info[1] == subty:
426 return name
427 return None
429 def printMsg(msg, out=sys.stdout, all=False):
430 """Print a message.
432 @param msg: message
433 @type msg: xu message
434 @param out: where to print to
435 @type out: stream
436 @param all: print payload if true
437 @type all: bool
438 """
439 hdr = msg.get_header()
440 major = hdr['type']
441 minor = hdr['subtype']
442 msgid = hdr['id']
443 ty = msgTypeName(major, minor)
444 print >>out, 'message:', 'type=', ty, '%d:%d' % (major, minor), 'id=%d' % msgid
445 if all:
446 print >>out, 'payload=', msg.get_payload()
449 def getMessageType(msg):
450 """Get a 2-tuple of the message type and subtype.
452 @param msg: message
453 @type msg: xu message
454 @return: type info
455 @rtype: (int, int)
456 """
457 hdr = msg.get_header()
458 return (hdr['type'], hdr.get('subtype'))
460 def getMessageId(msg):
461 hdr = msg.get_header()
462 return hdr['id']