Debian Codegraph @

common/hv_c7200.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * Hypervisor C7200 routines.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <errno.h>
17 #include <assert.h>
18 #include <stdarg.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <pthread.h>
24
25 #include "cpu.h"
26 #include "vm.h"
27 #include "device.h"
28 #include "dev_c7200.h"
29 #include "dev_vtty.h"
30 #include "utils.h"
31 #include "net.h"
32 #include "atm.h"
33 #include "frame_relay.h"
34 #include "crc.h"
35 #include "net_io.h"
36 #include "net_io_bridge.h"
37 #ifdef GEN_ETH
38 #include "gen_eth.h"
39 #endif
40 #include "registry.h"
41 #include "hypervisor.h"
42
43 /* Set the NPE type */
44 static int cmd_set_npe(hypervisor_conn_t *conn,int argc,char *argv[])
45 {
46 vm_instance_t *vm;
47
48 if (!(vm = hypervisor_find_vm(conn,argv[0])))
49 return(-1);
50
51 if ((c7200_npe_set_type(VM_C7200(vm),argv[1])) == -1) {
52 vm_release(vm);
53 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
54 "unable to set NPE type for router '%s'",
55 argv[0]);
56 return(-1);
57 }
58
59 vm_release(vm);
60 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
61 return(0);
62 }
63
64 /* Set the Midplane type */
65 static int cmd_set_midplane(hypervisor_conn_t *conn,int argc,char *argv[])
66 {
67 vm_instance_t *vm;
68
69 if (!(vm = hypervisor_find_vm(conn,argv[0])))
70 return(-1);
71
72 if ((c7200_midplane_set_type(VM_C7200(vm),argv[1])) == -1) {
73 vm_release(vm);
74 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
75 "unable to set Midplane type for router '%s'",
76 argv[0]);
77 return(-1);
78 }
79
80 vm_release(vm);
81 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
82 return(0);
83 }
84
85 /* Set the system id */
86 static int cmd_set_system_id(hypervisor_conn_t *conn,int argc,char *argv[])
87 {
88 vm_instance_t *vm;
89
90 if (!(vm = hypervisor_find_vm(conn,argv[0])))
91 return(-1);
92
93 if (( c7200_set_system_id(VM_C7200(vm),argv[1])) == -1) {
94 vm_release(vm);
95 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
96 "unable to set the system id for router '%s'",
97 argv[0]);
98 return(-1);
99 }
100
101 vm_release(vm);
102 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
103 return(0);
104 }
105
106 /* Get the base MAC address for the chassis */
107 static int cmd_get_mac_addr(hypervisor_conn_t *conn,int argc,char *argv[])
108 {
109 vm_instance_t *vm;
110 c7200_t *router;
111
112 if (!(vm = hypervisor_find_vm(conn,argv[0])))
113 return(-1);
114
115 router = VM_C7200(vm);
116 hypervisor_send_reply(conn,HSC_INFO_OK,1,
117 "%2.2x%2.2x.%2.2x%2.2x.%2.2x%2.2x",
118 router->mac_addr.eth_addr_byte[0],
119 router->mac_addr.eth_addr_byte[1],
120 router->mac_addr.eth_addr_byte[2],
121 router->mac_addr.eth_addr_byte[3],
122 router->mac_addr.eth_addr_byte[4],
123 router->mac_addr.eth_addr_byte[5]);
124
125 vm_release(vm);
126 return(0);
127 }
128
129 /* Set the base MAC address for the chassis */
130 static int cmd_set_mac_addr(hypervisor_conn_t *conn,int argc,char *argv[])
131 {
132 vm_instance_t *vm;
133
134 if (!(vm = hypervisor_find_vm(conn,argv[0])))
135 return(-1);
136
137 if ((c7200_midplane_set_mac_addr(VM_C7200(vm),argv[1])) == -1) {
138 vm_release(vm);
139 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
140 "unable to set MAC address for router '%s'",
141 argv[0]);
142 return(-1);
143 }
144
145 vm_release(vm);
146 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
147 return(0);
148 }
149
150 /*
151 * Set temperature for a DS1620 sensor.
152 * This can be used to simulate environmental problems (overheat)
153 */
154 static int cmd_set_temp_sensor(hypervisor_conn_t *conn,int argc,char *argv[])
155 {
156 vm_instance_t *vm;
157 c7200_t *router;
158 u_int index;
159 int temp;
160
161 if (!(vm = hypervisor_find_vm(conn,argv[0])))
162 return(-1);
163
164 router = VM_C7200(vm);
165
166 index = atoi(argv[1]);
167 temp = atoi(argv[2]);
168
169 if (index < C7200_TEMP_SENSORS)
170 ds1620_set_temp(&router->ds1620_sensors[index],temp);
171
172 vm_release(vm);
173 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
174 return(0);
175 }
176
177 /*
178 * Set power supply status.
179 * This can be used to simulate environmental problems (power loss)
180 */
181 static int cmd_set_power_supply(hypervisor_conn_t *conn,int argc,char *argv[])
182 {
183 vm_instance_t *vm;
184 c7200_t *router;
185 u_int index;
186 int status;
187
188 if (!(vm = hypervisor_find_vm(conn,argv[0])))
189 return(-1);
190
191 router = VM_C7200(vm);
192
193 index = atoi(argv[1]);
194 status = atoi(argv[2]);
195
196 if (status)
197 router->ps_status |= 1 << index;
198 else
199 router->ps_status &= ~(1 << index);
200
201 /* only 2 power supplies */
202 router->ps_status &= 0x03;
203
204 vm_release(vm);
205 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
206 return(0);
207 }
208
209 /* Show C7200 hardware */
210 static int cmd_show_hardware(hypervisor_conn_t *conn,int argc,char *argv[])
211 {
212 vm_instance_t *vm;
213 c7200_t *router;
214
215 if (!(vm = hypervisor_find_vm(conn,argv[0])))
216 return(-1);
217
218 router = VM_C7200(vm);
219 c7200_show_hardware(router);
220
221 vm_release(vm);
222 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
223 return(0);
224 }
225
226 /* Show info about C7200 object */
227 static void cmd_show_c7200_list(registry_entry_t *entry,void *opt,int *err)
228 {
229 hypervisor_conn_t *conn = opt;
230 vm_instance_t *vm = entry->data;
231
232 if (vm->platform == conn->cur_module->opt)
233 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name);
234 }
235
236 /* C7200 List */
237 static int cmd_c7200_list(hypervisor_conn_t *conn,int argc,char *argv[])
238 {
239 int err = 0;
240 registry_foreach_type(OBJ_TYPE_VM,cmd_show_c7200_list,conn,&err);
241 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
242 return(0);
243 }
244
245 /* C7200 commands */
246 static hypervisor_cmd_t c7200_cmd_array[] = {
247 { "set_npe", 2, 2, cmd_set_npe, NULL },
248 { "set_midplane", 2, 2, cmd_set_midplane, NULL },
249 { "get_mac_addr", 1, 1, cmd_get_mac_addr, NULL },
250 { "set_mac_addr", 2, 2, cmd_set_mac_addr, NULL },
251 { "set_system_id", 2, 2, cmd_set_system_id, NULL },
252 { "set_temp_sensor", 3, 3, cmd_set_temp_sensor, NULL },
253 { "set_power_supply", 3, 3, cmd_set_power_supply, NULL },
254 { "show_hardware", 1, 1, cmd_show_hardware, NULL },
255 { "list", 0, 0, cmd_c7200_list, NULL },
256 { NULL, -1, -1, NULL, NULL },
257 };
258
259 /* Hypervisor C7200 initialization */
260 int hypervisor_c7200_init(vm_platform_t *platform)
261 {
262 hypervisor_module_t *module;
263
264 module = hypervisor_register_module(platform->name,platform);
265 assert(module != NULL);
266
267 hypervisor_register_cmd_array(module,c7200_cmd_array);
268 return(0);
269 }