debuggers.hg

view tools/python/xen/xend/server/messages.py @ 3639:ef59b38283a5

bitkeeper revision 1.1159.212.64 (42000361XrdUoEK_vD6RMga2eLmwZg)

Allow specification of backend vif MAC addresses. If a backend
MAC is not provided, generate one as before

Signed-off-by: Jody Belka <knew-xen@pimb.org>
Signed-off-by: ian.pratt@cl.cam.ac.uk
author iap10@labyrinth.cl.cam.ac.uk
date Tue Feb 01 22:32:01 2005 +0000 (2005-02-01)
parents 3ced9b0f4dab
children bbe8541361dd
line source
1 import sys
2 import struct
3 import types
5 from xen.lowlevel import xu
7 DEBUG = 0
9 """ All message formats.
10 Added to incrementally for the various message types.
11 See below.
12 """
13 msg_formats = {}
15 #============================================================================
16 # Console message types.
17 #============================================================================
19 CMSG_CONSOLE = 0
21 console_formats = { 'console_data': (CMSG_CONSOLE, 0) }
23 msg_formats.update(console_formats)
25 #============================================================================
26 # Block interface message types.
27 #============================================================================
29 CMSG_BLKIF_BE = 1
30 CMSG_BLKIF_FE = 2
32 CMSG_BLKIF_FE_INTERFACE_STATUS = 0
33 CMSG_BLKIF_FE_DRIVER_STATUS = 32
34 CMSG_BLKIF_FE_INTERFACE_CONNECT = 33
35 CMSG_BLKIF_FE_INTERFACE_DISCONNECT = 34
36 CMSG_BLKIF_FE_INTERFACE_QUERY = 35
38 CMSG_BLKIF_BE_CREATE = 0
39 CMSG_BLKIF_BE_DESTROY = 1
40 CMSG_BLKIF_BE_CONNECT = 2
41 CMSG_BLKIF_BE_DISCONNECT = 3
42 CMSG_BLKIF_BE_VBD_CREATE = 4
43 CMSG_BLKIF_BE_VBD_DESTROY = 5
44 CMSG_BLKIF_BE_VBD_GROW = 6
45 CMSG_BLKIF_BE_VBD_SHRINK = 7
46 CMSG_BLKIF_BE_DRIVER_STATUS = 32
48 BLKIF_DRIVER_STATUS_DOWN = 0
49 BLKIF_DRIVER_STATUS_UP = 1
51 BLKIF_INTERFACE_STATUS_CLOSED = 0 #/* Interface doesn't exist. */
52 BLKIF_INTERFACE_STATUS_DISCONNECTED = 1 #/* Exists but is disconnected. */
53 BLKIF_INTERFACE_STATUS_CONNECTED = 2 #/* Exists and is connected. */
54 BLKIF_INTERFACE_STATUS_CHANGED = 3 #/* A device has been added or removed. */
56 BLKIF_BE_STATUS_OKAY = 0
57 BLKIF_BE_STATUS_ERROR = 1
58 BLKIF_BE_STATUS_INTERFACE_EXISTS = 2
59 BLKIF_BE_STATUS_INTERFACE_NOT_FOUND = 3
60 BLKIF_BE_STATUS_INTERFACE_CONNECTED = 4
61 BLKIF_BE_STATUS_VBD_EXISTS = 5
62 BLKIF_BE_STATUS_VBD_NOT_FOUND = 6
63 BLKIF_BE_STATUS_OUT_OF_MEMORY = 7
64 BLKIF_BE_STATUS_EXTENT_NOT_FOUND = 8
65 BLKIF_BE_STATUS_MAPPING_ERROR = 9
67 blkif_formats = {
68 'blkif_be_connect_t':
69 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_CONNECT),
70 # Connect be to fe (in response to blkif_fe_interface_connect_t).
72 'blkif_be_create_t':
73 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_CREATE),
74 # Create be.
76 'blkif_be_disconnect_t':
77 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_DISCONNECT),
78 # Disconnect be from fe.
80 'blkif_be_destroy_t':
81 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_DESTROY),
82 # Destroy be (after disconnect).
83 # Make be do this even if no disconnect (and destroy all vbd too).
85 'blkif_be_vbd_create_t':
86 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_VBD_CREATE),
87 # Create a vbd device.
89 'blkif_be_vbd_grow_t':
90 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_VBD_GROW),
91 # Change the size of a vbd device. Remove?
92 # Do in one go in blkif_be_vbd_create_t.
94 'blkif_be_vbd_destroy_t':
95 (CMSG_BLKIF_BE, CMSG_BLKIF_BE_VBD_DESTROY),
96 # Destroy a vbd.
98 # Add message to query be for state and vbds.
100 'blkif_fe_interface_status_t':
101 (CMSG_BLKIF_FE, CMSG_BLKIF_FE_INTERFACE_STATUS),
102 # Notify device status to fe.
103 # Also used to notify 'any' device change with status BLKIF_INTERFACE_STATUS_CHANGED.
104 # Rename to blkif_fe_interface_status.
106 'blkif_fe_driver_status_t':
107 (CMSG_BLKIF_FE, CMSG_BLKIF_FE_DRIVER_STATUS),
108 # Comes from fe, treated as notifying that fe has come up/changed.
109 # Xend sets be(s) to BLKIF_INTERFACE_STATUS_DISCONNECTED,
110 # sends blkif_fe_interface_status_t to fe (from each be).
111 #
112 # Rename to blkif_fe_driver_status.
113 # Reply with i/f count.
114 # The i/f sends probes (using -ve trick), we reply with the info.
116 'blkif_fe_interface_connect_t':
117 (CMSG_BLKIF_FE, CMSG_BLKIF_FE_INTERFACE_CONNECT),
118 # Comes from fe, passing shmem frame to use for be.
119 # fe sends when gets blkif_fe_interface_status_t with state NETIF_INTERFACE_STATUS_DISCONNECTED.
120 # Xend creates event channel and notifies be.
121 # Then notifies fe of event channel with blkif_fe_interface_status_t.
123 # Add message to kick fe to probe for devices.
124 # Just report new devices to fe?
126 #
127 # Add message for fe to probe a device.
128 # And probing with id -1 should return first.
129 # And probing with id -n should return first device with id > n.
131 # Add message to query fe for state and vbds.
132 }
134 msg_formats.update(blkif_formats)
136 #============================================================================
137 # Network interface message types.
138 #============================================================================
140 CMSG_NETIF_BE = 3
141 CMSG_NETIF_FE = 4
143 CMSG_NETIF_FE_INTERFACE_STATUS = 0
144 CMSG_NETIF_FE_DRIVER_STATUS = 32
145 CMSG_NETIF_FE_INTERFACE_CONNECT = 33
146 CMSG_NETIF_FE_INTERFACE_DISCONNECT = 34
147 CMSG_NETIF_FE_INTERFACE_QUERY = 35
149 CMSG_NETIF_BE_CREATE = 0
150 CMSG_NETIF_BE_DESTROY = 1
151 CMSG_NETIF_BE_CONNECT = 2
152 CMSG_NETIF_BE_DISCONNECT = 3
153 CMSG_NETIF_BE_DRIVER_STATUS = 32
155 NETIF_INTERFACE_STATUS_CLOSED = 0 #/* Interface doesn't exist. */
156 NETIF_INTERFACE_STATUS_DISCONNECTED = 1 #/* Exists but is disconnected. */
157 NETIF_INTERFACE_STATUS_CONNECTED = 2 #/* Exists and is connected. */
158 NETIF_INTERFACE_STATUS_CHANGED = 3 #/* A device has been added or removed. */
160 NETIF_DRIVER_STATUS_DOWN = 0
161 NETIF_DRIVER_STATUS_UP = 1
163 netif_formats = {
164 'netif_be_connect_t':
165 (CMSG_NETIF_BE, CMSG_NETIF_BE_CONNECT),
167 'netif_be_create_t':
168 (CMSG_NETIF_BE, CMSG_NETIF_BE_CREATE),
170 'netif_be_disconnect_t':
171 (CMSG_NETIF_BE, CMSG_NETIF_BE_DISCONNECT),
173 'netif_be_destroy_t':
174 (CMSG_NETIF_BE, CMSG_NETIF_BE_DESTROY),
176 'netif_be_driver_status_t':
177 (CMSG_NETIF_BE, CMSG_NETIF_BE_DRIVER_STATUS),
179 'netif_fe_driver_status_t':
180 (CMSG_NETIF_FE, CMSG_NETIF_FE_DRIVER_STATUS),
182 'netif_fe_interface_connect_t':
183 (CMSG_NETIF_FE, CMSG_NETIF_FE_INTERFACE_CONNECT),
185 'netif_fe_interface_status_t':
186 (CMSG_NETIF_FE, CMSG_NETIF_FE_INTERFACE_STATUS),
187 }
189 msg_formats.update(netif_formats)
191 #============================================================================
192 # USB interface message types.
193 #============================================================================
195 CMSG_USBIF_BE = 8
196 CMSG_USBIF_FE = 9
198 CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED = 0
200 CMSG_USBIF_FE_DRIVER_STATUS_CHANGED = 32
201 CMSG_USBIF_FE_INTERFACE_CONNECT = 33
202 CMSG_USBIF_FE_INTERFACE_DISCONNECT = 34
204 USBIF_DRIVER_STATUS_DOWN = 0
205 USBIF_DRIVER_STATUS_UP = 1
207 USBIF_INTERFACE_STATUS_DESTROYED = 0 #/* Interface doesn't exist. */
208 USBIF_INTERFACE_STATUS_DISCONNECTED = 1 #/* Exists but is disconnected. */
209 USBIF_INTERFACE_STATUS_CONNECTED = 2 #/* Exists and is connected. */
211 CMSG_USBIF_BE_CREATE = 0
212 CMSG_USBIF_BE_DESTROY = 1
213 CMSG_USBIF_BE_CONNECT = 2
215 CMSG_USBIF_BE_DISCONNECT = 3
216 CMSG_USBIF_BE_CLAIM_PORT = 4
217 CMSG_USBIF_BE_RELEASE_PORT = 5
219 CMSG_USBIF_BE_DRIVER_STATUS_CHANGED = 32
221 USBIF_BE_STATUS_OKAY = 0
222 USBIF_BE_STATUS_ERROR = 1
224 USBIF_BE_STATUS_INTERFACE_EXISTS = 2
225 USBIF_BE_STATUS_INTERFACE_NOT_FOUND = 3
226 USBIF_BE_STATUS_INTERFACE_CONNECTED = 4
227 USBIF_BE_STATUS_OUT_OF_MEMORY = 7
228 USBIF_BE_STATUS_MAPPING_ERROR = 9
230 usbif_formats = {
231 'usbif_be_create_t':
232 (CMSG_USBIF_BE, CMSG_USBIF_BE_CREATE),
233 'usbif_be_destroy_t':
234 (CMSG_USBIF_BE, CMSG_USBIF_BE_DESTROY),
235 'usbif_be_connect_t':
236 (CMSG_USBIF_BE, CMSG_USBIF_BE_CONNECT),
237 'usbif_be_disconnect_t':
238 (CMSG_USBIF_BE, CMSG_USBIF_BE_DISCONNECT),
239 'usbif_be_claim_port_t':
240 (CMSG_USBIF_BE, CMSG_USBIF_BE_CLAIM_PORT),
241 'usbif_be_release_port_t':
242 (CMSG_USBIF_BE, CMSG_USBIF_BE_RELEASE_PORT),
243 'usbif_fe_interface_status_changed_t':
244 (CMSG_USBIF_FE, CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED),
245 'usbif_fe_driver_status_changed_t':
246 (CMSG_USBIF_FE, CMSG_USBIF_FE_DRIVER_STATUS_CHANGED),
247 'usbif_fe_interface_connect_t':
248 (CMSG_USBIF_FE, CMSG_USBIF_FE_INTERFACE_CONNECT),
249 'usbif_fe_interface_disconnect_t':
250 (CMSG_USBIF_FE, CMSG_USBIF_FE_INTERFACE_DISCONNECT)
251 }
253 msg_formats.update(usbif_formats)
255 #============================================================================
256 # Domain shutdown message types.
257 #============================================================================
259 CMSG_SHUTDOWN = 6
261 CMSG_SHUTDOWN_POWEROFF = 0
262 CMSG_SHUTDOWN_REBOOT = 1
263 CMSG_SHUTDOWN_SUSPEND = 2
264 CMSG_SHUTDOWN_SYSRQ = 3
266 STOPCODE_shutdown = 0
267 STOPCODE_reboot = 1
268 STOPCODE_suspend = 2
269 STOPCODE_sysrq = 3
271 shutdown_formats = {
272 'shutdown_poweroff_t':
273 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_POWEROFF),
275 'shutdown_reboot_t':
276 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_REBOOT),
278 'shutdown_suspend_t':
279 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_SUSPEND),
281 'shutdown_sysrq_t':
282 (CMSG_SHUTDOWN, CMSG_SHUTDOWN_SYSRQ)
283 }
285 msg_formats.update(shutdown_formats)
287 #============================================================================
288 # Domain memory reservation message.
289 #============================================================================
291 CMSG_MEM_REQUEST = 7
292 CMSG_MEM_REQUEST_SET = 0
294 mem_request_formats = {
295 'mem_request_t':
296 (CMSG_MEM_REQUEST, CMSG_MEM_REQUEST_SET)
297 }
299 msg_formats.update(mem_request_formats)
301 #============================================================================
302 class Msg:
303 pass
305 _next_msgid = 0
307 def nextid():
308 """Generate the next message id.
310 @return: message id
311 @rtype: int
312 """
313 global _next_msgid
314 _next_msgid += 1
315 return _next_msgid
317 def packMsg(ty, params):
318 """Pack a message.
319 Any I{mac} parameter is passed in as an int[6] array and converted.
321 @param ty: message type name
322 @type ty: string
323 @param params: message parameters
324 @type params: dicy
325 @return: message
326 @rtype: xu message
327 """
328 msgid = nextid()
329 if DEBUG: print '>packMsg', msgid, ty, params
330 (major, minor) = msg_formats[ty]
331 args = {}
332 for (k, v) in params.items():
333 if k in ['mac', 'be_mac']:
334 for i in range(0, 6):
335 args['%s[%d]' % (k, i)] = v[i]
336 else:
337 args[k] = v
338 msg = xu.message(major, minor, msgid, args)
339 if DEBUG: print '<packMsg', msg.get_header()['id'], ty, args
340 return msg
342 def unpackMsg(ty, msg):
343 """Unpack a message.
344 Any mac addresses in the message are converted to int[6] array
345 in the return dict.
347 @param ty: message type
348 @type ty: string
349 @param msg: message
350 @type msg: xu message
351 @return: parameters
352 @rtype: dict
353 """
354 args = msg.get_payload()
355 if DEBUG: print '>unpackMsg', args
356 if isinstance(args, types.StringType):
357 args = {'value': args}
358 else:
359 mac = [0, 0, 0, 0, 0, 0]
360 macs = []
361 for (k, v) in args.items():
362 if k.startswith('mac['):
363 macs.append(k)
364 i = int(k[4:5])
365 mac[i] = v
366 else:
367 pass
368 if macs:
369 args['mac'] = mac
370 print 'macs=', macs
371 print 'args=', args
372 for k in macs:
373 del args[k]
374 if DEBUG:
375 msgid = msg.get_header()['id']
376 print '<unpackMsg', msgid, ty, args
377 return args
379 def msgTypeName(ty, subty):
380 """Convert a message type, subtype pair to a message type name.
382 @param ty: message type
383 @type ty: int
384 @param subty: message subtype
385 @type ty: int
386 @return: message type name (or None)
387 @rtype: string or None
388 """
389 for (name, info) in msg_formats.items():
390 if info[0] == ty and info[1] == subty:
391 return name
392 return None
394 def printMsg(msg, out=sys.stdout, all=0):
395 """Print a message.
397 @param msg: message
398 @type msg: xu message
399 @param out: where to print to
400 @type out: stream
401 @param all: print payload if true
402 @type all: bool
403 """
404 hdr = msg.get_header()
405 major = hdr['type']
406 minor = hdr['subtype']
407 msgid = hdr['id']
408 ty = msgTypeName(major, minor)
409 print >>out, 'message:', 'type=', ty, '%d:%d' % (major, minor), 'id=%d' % msgid
410 if all:
411 print >>out, 'payload=', msg.get_payload()