ia64/xen-unstable

view tools/xcs/xcsdump.c @ 6422:e24fd7012ffb

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 10:09:39 2005 +0000 (2005-08-25)
parents 2f20c2fce2c5 6e899a3840b2
children 4abd299ef2f6
line source
1 /* xcsdump.c
2 *
3 * little tool to sniff control messages.
4 *
5 * Copyright (c) 2004, Andrew Warfield
6 *
7 * Modifications by Anthony Liguori <aliguori@us.ibm.com> are:
8 * Copyright (C) 2005, International Business Machines, Corp.
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #include <ctype.h>
19 #include <xenctrl.h>
20 #include <xen/xen.h>
21 #include <xen/io/domain_controller.h>
22 #include <getopt.h>
23 #include "xcs_proto.h"
24 #include "xcs.h"
26 #include "dump.h"
28 static int xcs_ctrl_fd = -1; /* connection to the xcs server. */
29 static int xcs_data_fd = -1; /* connection to the xcs server. */
31 int sock_connect(char *path)
32 {
33 struct sockaddr_un addr;
34 int ret, len, fd;
36 fd = socket(AF_UNIX, SOCK_STREAM, 0);
37 if (fd < 0)
38 {
39 printf("error creating xcs socket!\n");
40 return -1;
41 }
43 addr.sun_family = AF_UNIX;
44 strcpy(addr.sun_path, path);
45 len = sizeof(addr.sun_family) + strlen(addr.sun_path) + 1;
47 ret = connect(fd, (struct sockaddr *)&addr, len);
48 if (ret < 0)
49 {
50 printf("error connecting to xcs!\n");
51 return -1;
52 }
54 return fd;
55 }
57 void sock_disconnect(int *fd)
58 {
59 close(*fd);
60 *fd = -1;
61 }
63 void xcs_read(int fd, xcs_msg_t *msg)
64 {
65 int ret;
67 ret = read(fd, msg, sizeof(xcs_msg_t));
68 if (ret != sizeof(xcs_msg_t)) {
69 printf("read error\n");
70 exit(-1);
71 }
72 }
74 void xcs_send(int fd, xcs_msg_t *msg)
75 {
76 int ret;
78 ret = send(fd, msg, sizeof(xcs_msg_t), 0);
79 if (ret != sizeof(xcs_msg_t) )
80 {
81 printf("send error\n");
82 exit(-1);
83 }
84 }
87 int main(int argc, char* argv[])
88 {
89 int ret;
90 xcs_msg_t msg;
91 control_msg_t *cmsg;
92 int verbose = 0;
93 int ch;
95 while ((ch = getopt(argc, argv, "hv:")) != -1)
96 {
97 switch (ch)
98 {
99 case 'v':
100 verbose = atoi(optarg);
101 break;
102 case 'h':
103 printf("Usage: %s [-v FLAGS]\n"
104 "Displays XCS control message traffic.\n"
105 "\n"
106 "FLAGS is a bitmask where each bit (numbering starts from LSB) represents\n"
107 "whether to display a particular message type.\n"
108 "\n"
109 "For example, -v 1022 will display all messages except for console messages.\n"
110 , argv[0]);
111 exit(0);
112 break;
113 }
114 }
116 ret = sock_connect(XCS_SUN_PATH);
117 if (ret < 0)
118 {
119 printf("connect failed!\n");
120 exit(-1);
121 }
122 xcs_ctrl_fd = ret;
124 memset(&msg, 0, sizeof(msg));
125 msg.type = XCS_CONNECT_CTRL;
126 xcs_send(xcs_ctrl_fd, &msg);
127 xcs_read(xcs_ctrl_fd, &msg);
128 if (msg.result != XCS_RSLT_OK)
129 {
130 printf("Error connecting control channel\n");
131 exit(-1);
132 }
134 ret = sock_connect(XCS_SUN_PATH);
135 if (ret < 0)
136 {
137 printf("connect failed!\n");
138 exit(-1);
139 }
140 xcs_data_fd = ret;
142 msg.type = XCS_CONNECT_DATA;
143 /* session id is set from before... */
144 xcs_send(xcs_data_fd, &msg);
145 xcs_read(xcs_data_fd, &msg);
146 if (msg.result != XCS_RSLT_OK)
147 {
148 printf("Error connecting data channel\n");
149 exit(-1);
150 }
152 msg.type = XCS_MSG_BIND;
153 msg.u.bind.port = PORT_WILDCARD;
154 msg.u.bind.type = TYPE_WILDCARD;
155 xcs_send(xcs_ctrl_fd, &msg);
156 xcs_read(xcs_ctrl_fd, &msg);
157 if (msg.result != XCS_RSLT_OK)
158 {
159 printf("Error binding.\n");
160 exit(-1);
161 }
164 while (1)
165 {
166 xcs_read(xcs_data_fd, &msg);
167 cmsg = &msg.u.control.msg;
169 switch (msg.type)
170 {
171 case XCS_REQUEST:
172 if (!verbose || verbose & (1 << msg.u.control.msg.type))
173 {
174 printf("[REQUEST ] : (dom:%u port:%d) (type:(%d,%d) len %d)\n",
175 msg.u.control.remote_dom,
176 msg.u.control.local_port,
177 msg.u.control.msg.type,
178 msg.u.control.msg.subtype,
179 msg.u.control.msg.length);
181 dump_msg(cmsg, verbose);
182 }
183 break;
184 case XCS_RESPONSE:
185 if (!verbose || verbose & (1 << msg.u.control.msg.type))
186 {
187 printf("[RESPONSE] : (dom:%u port:%d) (type:(%d,%d) len %d)\n",
188 msg.u.control.remote_dom,
189 msg.u.control.local_port,
190 msg.u.control.msg.type,
191 msg.u.control.msg.subtype,
192 msg.u.control.msg.length);
194 dump_msg(cmsg, verbose);
195 }
196 break;
197 case XCS_VIRQ:
198 printf("[VIRQ ] : %d\n", msg.u.control.local_port);
199 break;
200 default:
201 printf("[UNKNOWN ] : %d\n", msg.type);
202 }
203 }
205 return(0);
206 }