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