/[dynamips]/upstream/dynamips-0.2.5/hv_ethsw.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/dynamips-0.2.5/hv_ethsw.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Sat Oct 6 16:01:44 2007 UTC (12 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 7034 byte(s)
import 0.2.5 from upstream

1 /*
2 * Cisco 7200 (Predator) simulation platform.
3 * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
4 *
5 * Hypervisor Ethernet switch 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 <sys/mman.h>
15 #include <signal.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <assert.h>
19 #include <stdarg.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <pthread.h>
25
26 #include "mips64.h"
27 #include "dynamips.h"
28 #include "utils.h"
29 #include "net.h"
30 #include "eth_switch.h"
31 #include "crc.h"
32 #include "net_io.h"
33 #include "registry.h"
34 #include "hypervisor.h"
35
36 /* Create a new Ethernet switch object */
37 static int cmd_create(hypervisor_conn_t *conn,int argc,char *argv[])
38 {
39 ethsw_table_t *t;
40
41 if (!(t = ethsw_create(argv[0]))) {
42 hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
43 "unable to create Ethernet switch '%s'",
44 argv[0]);
45 return(-1);
46 }
47
48 ethsw_release(argv[0]);
49 hypervisor_send_reply(conn,HSC_INFO_OK,1,"ETHSW '%s' created",argv[0]);
50 return(0);
51 }
52
53 /* Delete an Ethernet switch */
54 static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[])
55 {
56 int res;
57
58 res = ethsw_delete(argv[0]);
59
60 if (res == 1) {
61 hypervisor_send_reply(conn,HSC_INFO_OK,1,"ETHSW '%s' deleted",argv[0]);
62 } else {
63 hypervisor_send_reply(conn,HSC_ERR_DELETE,1,
64 "unable to delete ETHSW '%s'",argv[0]);
65 }
66
67 return(res);
68 }
69
70 /*
71 * Add a NIO to an Ethernet switch.
72 *
73 * Parameters: <ethsw_name> <nio_name>
74 */
75 static int cmd_add_nio(hypervisor_conn_t *conn,int argc,char *argv[])
76 {
77 ethsw_table_t *t;
78
79 if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
80 return(-1);
81
82 if (ethsw_add_netio(t,argv[1]) == -1) {
83 ethsw_release(argv[0]);
84 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
85 "unable to bind NIO '%s' to switch '%s'",
86 argv[1],argv[0]);
87 return(-1);
88 }
89
90 ethsw_release(argv[0]);
91 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' bound.",argv[1]);
92 return(0);
93 }
94
95 /*
96 * Remove a NIO from an Ethernet switch
97 *
98 * Parameters: <ethsw_name> <nio_name>
99 */
100 static int cmd_remove_nio(hypervisor_conn_t *conn,int argc,char *argv[])
101 {
102 ethsw_table_t *t;
103
104 if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
105 return(-1);
106
107 if (ethsw_remove_netio(t,argv[1]) == -1) {
108 ethsw_release(argv[0]);
109 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
110 "unable to bind NIO '%s' to switch '%s'",
111 argv[1],argv[0]);
112 return(-1);
113 }
114
115 ethsw_release(argv[0]);
116 hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' unbound.",argv[1]);
117 return(0);
118 }
119
120 /*
121 * Set a port as an access port.
122 *
123 * Parameters: <ethsw_name> <nio> <VLAN>
124 */
125 static int cmd_set_access_port(hypervisor_conn_t *conn,int argc,char *argv[])
126 {
127 ethsw_table_t *t;
128
129 if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
130 return(-1);
131
132 if (ethsw_set_access_port(t,argv[1],atoi(argv[2])) == -1) {
133 ethsw_release(argv[0]);
134 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
135 "unable to apply port settings");
136 return(-1);
137 }
138
139 ethsw_release(argv[0]);
140 hypervisor_send_reply(conn,HSC_INFO_OK,1,"Port settings OK");
141 return(0);
142 }
143
144 /*
145 * Set a port as a trunk (802.1Q) port.
146 *
147 * Parameters: <ethsw_name> <nio> <native_VLAN>
148 */
149 static int cmd_set_dot1q_port(hypervisor_conn_t *conn,int argc,char *argv[])
150 {
151 ethsw_table_t *t;
152
153 if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
154 return(-1);
155
156 if (ethsw_set_dot1q_port(t,argv[1],atoi(argv[2])) == -1) {
157 ethsw_release(argv[0]);
158 hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
159 "unable to apply port settings");
160 return(-1);
161 }
162
163 ethsw_release(argv[0]);
164 hypervisor_send_reply(conn,HSC_INFO_OK,1,"Port settings OK");
165 return(0);
166 }
167
168 /* Clear the MAC address table */
169 static int cmd_clear_mac_addr_table(hypervisor_conn_t *conn,
170 int argc,char *argv[])
171 {
172 ethsw_table_t *t;
173
174 if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
175 return(-1);
176
177 ethsw_clear_mac_addr_table(t);
178 ethsw_release(argv[0]);
179 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
180 return(0);
181 }
182
183 /* Show the MAC address table */
184 static void cmd_show_mac_addr_entry(ethsw_table_t *t,ethsw_mac_entry_t *entry,
185 hypervisor_conn_t *conn)
186 {
187 hypervisor_send_reply(conn,HSC_INFO_MSG,0,
188 "%2.2x%2.2x.%2.2x%2.2x.%2.2x%2.2x %u %s",
189 entry->mac_addr.eth_addr_byte[0],
190 entry->mac_addr.eth_addr_byte[1],
191 entry->mac_addr.eth_addr_byte[2],
192 entry->mac_addr.eth_addr_byte[3],
193 entry->mac_addr.eth_addr_byte[4],
194 entry->mac_addr.eth_addr_byte[5],
195 entry->vlan_id,
196 entry->nio->name);
197 }
198
199 static int cmd_show_mac_addr_table(hypervisor_conn_t *conn,
200 int argc,char *argv[])
201 {
202 ethsw_table_t *t;
203
204 if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
205 return(-1);
206
207 ethsw_iterate_mac_addr_table(t,
208 (ethsw_foreach_entry_t)cmd_show_mac_addr_entry,
209 conn);
210
211 ethsw_release(argv[0]);
212 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
213 return(0);
214 }
215
216
217 /* Show info about a ETHSW object */
218 static void cmd_show_list(registry_entry_t *entry,void *opt,int *err)
219 {
220 hypervisor_conn_t *conn = opt;
221 hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name);
222 }
223
224 /* Ethernet switch List */
225 static int cmd_list(hypervisor_conn_t *conn,int argc,char *argv[])
226 {
227 int err = 0;
228 registry_foreach_type(OBJ_TYPE_ETHSW,cmd_show_list,conn,&err);
229 hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
230 return(0);
231 }
232
233 /* ETHSW commands */
234 static hypervisor_cmd_t ethsw_cmd_array[] = {
235 { "create", 1, 1, cmd_create, NULL },
236 { "delete", 1, 1, cmd_delete, NULL },
237 { "add_nio", 2, 2, cmd_add_nio, NULL },
238 { "remove_nio", 2, 2, cmd_remove_nio, NULL },
239 { "set_access_port", 3, 3, cmd_set_access_port, NULL },
240 { "set_dot1q_port", 3, 3, cmd_set_dot1q_port, NULL },
241 { "clear_mac_addr_table", 1, 1, cmd_clear_mac_addr_table, NULL },
242 { "show_mac_addr_table", 1, 1, cmd_show_mac_addr_table, NULL },
243 { "list", 0, 0, cmd_list, NULL },
244 { NULL, -1, -1, NULL, NULL },
245 };
246
247 /* Hypervisor Ethernet switch initialization */
248 int hypervisor_ethsw_init(void)
249 {
250 hypervisor_module_t *module;
251
252 module = hypervisor_register_module("ethsw");
253 assert(module != NULL);
254
255 hypervisor_register_cmd_array(module,ethsw_cmd_array);
256 return(0);
257 }

  ViewVC Help
Powered by ViewVC 1.1.26