| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 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 <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 "utils.h" |
| 26 |
#include "net.h" |
| 27 |
#include "eth_switch.h" |
| 28 |
#include "crc.h" |
| 29 |
#include "net_io.h" |
| 30 |
#include "registry.h" |
| 31 |
#include "hypervisor.h" |
| 32 |
|
| 33 |
/* Create a new Ethernet switch object */ |
| 34 |
static int cmd_create(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 35 |
{ |
| 36 |
ethsw_table_t *t; |
| 37 |
|
| 38 |
if (!(t = ethsw_create(argv[0]))) { |
| 39 |
hypervisor_send_reply(conn,HSC_ERR_CREATE,1, |
| 40 |
"unable to create Ethernet switch '%s'", |
| 41 |
argv[0]); |
| 42 |
return(-1); |
| 43 |
} |
| 44 |
|
| 45 |
ethsw_release(argv[0]); |
| 46 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ETHSW '%s' created",argv[0]); |
| 47 |
return(0); |
| 48 |
} |
| 49 |
|
| 50 |
/* Rename an Ethernet switch */ |
| 51 |
static int cmd_rename(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 52 |
{ |
| 53 |
ethsw_table_t *t; |
| 54 |
char *newname; |
| 55 |
|
| 56 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 57 |
return(-1); |
| 58 |
|
| 59 |
if (registry_exists(argv[1],OBJ_TYPE_ETHSW)) { |
| 60 |
ethsw_release(argv[0]); |
| 61 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 62 |
"unable to rename ETHSW '%s', '%s' already exists", |
| 63 |
argv[0],argv[1]); |
| 64 |
return(-1); |
| 65 |
} |
| 66 |
|
| 67 |
if(!(newname = strdup(argv[1]))) { |
| 68 |
ethsw_release(argv[0]); |
| 69 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 70 |
"unable to rename ETHSW '%s', out of memory", |
| 71 |
argv[0]); |
| 72 |
return(-1); |
| 73 |
} |
| 74 |
|
| 75 |
if (registry_rename(argv[0],newname,OBJ_TYPE_ETHSW)) { |
| 76 |
free(newname); |
| 77 |
ethsw_release(argv[0]); |
| 78 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 79 |
"unable to rename ETHSW '%s'", |
| 80 |
argv[0]); |
| 81 |
return(-1); |
| 82 |
} |
| 83 |
|
| 84 |
free(t->name); |
| 85 |
t->name = newname; |
| 86 |
|
| 87 |
ethsw_release(argv[1]); |
| 88 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ETHSW '%s' renamed to '%s'",argv[0],argv[1]); |
| 89 |
return(0); |
| 90 |
} |
| 91 |
|
| 92 |
/* Delete an Ethernet switch */ |
| 93 |
static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 94 |
{ |
| 95 |
int res; |
| 96 |
|
| 97 |
res = ethsw_delete(argv[0]); |
| 98 |
|
| 99 |
if (res == 1) { |
| 100 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ETHSW '%s' deleted",argv[0]); |
| 101 |
} else { |
| 102 |
hypervisor_send_reply(conn,HSC_ERR_DELETE,1, |
| 103 |
"unable to delete ETHSW '%s'",argv[0]); |
| 104 |
} |
| 105 |
|
| 106 |
return(res); |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* Add a NIO to an Ethernet switch. |
| 111 |
* |
| 112 |
* Parameters: <ethsw_name> <nio_name> |
| 113 |
*/ |
| 114 |
static int cmd_add_nio(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 115 |
{ |
| 116 |
ethsw_table_t *t; |
| 117 |
|
| 118 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 119 |
return(-1); |
| 120 |
|
| 121 |
if (ethsw_add_netio(t,argv[1]) == -1) { |
| 122 |
ethsw_release(argv[0]); |
| 123 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1, |
| 124 |
"unable to bind NIO '%s' to switch '%s'", |
| 125 |
argv[1],argv[0]); |
| 126 |
return(-1); |
| 127 |
} |
| 128 |
|
| 129 |
ethsw_release(argv[0]); |
| 130 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' bound.",argv[1]); |
| 131 |
return(0); |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* Remove a NIO from an Ethernet switch |
| 136 |
* |
| 137 |
* Parameters: <ethsw_name> <nio_name> |
| 138 |
*/ |
| 139 |
static int cmd_remove_nio(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 140 |
{ |
| 141 |
ethsw_table_t *t; |
| 142 |
|
| 143 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 144 |
return(-1); |
| 145 |
|
| 146 |
if (ethsw_remove_netio(t,argv[1]) == -1) { |
| 147 |
ethsw_release(argv[0]); |
| 148 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1, |
| 149 |
"unable to bind NIO '%s' to switch '%s'", |
| 150 |
argv[1],argv[0]); |
| 151 |
return(-1); |
| 152 |
} |
| 153 |
|
| 154 |
ethsw_release(argv[0]); |
| 155 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' unbound.",argv[1]); |
| 156 |
return(0); |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* Set a port as an access port. |
| 161 |
* |
| 162 |
* Parameters: <ethsw_name> <nio> <VLAN> |
| 163 |
*/ |
| 164 |
static int cmd_set_access_port(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 165 |
{ |
| 166 |
ethsw_table_t *t; |
| 167 |
|
| 168 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 169 |
return(-1); |
| 170 |
|
| 171 |
if (ethsw_set_access_port(t,argv[1],atoi(argv[2])) == -1) { |
| 172 |
ethsw_release(argv[0]); |
| 173 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1, |
| 174 |
"unable to apply port settings"); |
| 175 |
return(-1); |
| 176 |
} |
| 177 |
|
| 178 |
ethsw_release(argv[0]); |
| 179 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"Port settings OK"); |
| 180 |
return(0); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Set a port as a trunk (802.1Q) port. |
| 185 |
* |
| 186 |
* Parameters: <ethsw_name> <nio> <native_VLAN> |
| 187 |
*/ |
| 188 |
static int cmd_set_dot1q_port(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 189 |
{ |
| 190 |
ethsw_table_t *t; |
| 191 |
|
| 192 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 193 |
return(-1); |
| 194 |
|
| 195 |
if (ethsw_set_dot1q_port(t,argv[1],atoi(argv[2])) == -1) { |
| 196 |
ethsw_release(argv[0]); |
| 197 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1, |
| 198 |
"unable to apply port settings"); |
| 199 |
return(-1); |
| 200 |
} |
| 201 |
|
| 202 |
ethsw_release(argv[0]); |
| 203 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"Port settings OK"); |
| 204 |
return(0); |
| 205 |
} |
| 206 |
|
| 207 |
/* |
| 208 |
* Set a port as a trunk (QinQ) port. |
| 209 |
* |
| 210 |
* Parameters: <ethsw_name> <nio> <outer_VLAN> <ethertype> |
| 211 |
*/ |
| 212 |
static int cmd_set_qinq_port(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 213 |
{ |
| 214 |
ethsw_table_t *t; |
| 215 |
m_uint16_t ethertype = N_ETH_PROTO_DOT1Q; |
| 216 |
|
| 217 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 218 |
return(-1); |
| 219 |
|
| 220 |
if (argc == 4) { |
| 221 |
sscanf(argv[3], "0x%hx", ðertype); |
| 222 |
} |
| 223 |
if (ethsw_set_qinq_port(t,argv[1],atoi(argv[2]),ethertype) == -1) { |
| 224 |
ethsw_release(argv[0]); |
| 225 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1, |
| 226 |
"unable to apply port settings"); |
| 227 |
return(-1); |
| 228 |
} |
| 229 |
|
| 230 |
ethsw_release(argv[0]); |
| 231 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"Port settings OK"); |
| 232 |
return(0); |
| 233 |
} |
| 234 |
|
| 235 |
/* Clear the MAC address table */ |
| 236 |
static int cmd_clear_mac_addr_table(hypervisor_conn_t *conn, |
| 237 |
int argc,char *argv[]) |
| 238 |
{ |
| 239 |
ethsw_table_t *t; |
| 240 |
|
| 241 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 242 |
return(-1); |
| 243 |
|
| 244 |
ethsw_clear_mac_addr_table(t); |
| 245 |
ethsw_release(argv[0]); |
| 246 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 247 |
return(0); |
| 248 |
} |
| 249 |
|
| 250 |
/* Show the MAC address table */ |
| 251 |
static void cmd_show_mac_addr_entry(ethsw_table_t *t,ethsw_mac_entry_t *entry, |
| 252 |
hypervisor_conn_t *conn) |
| 253 |
{ |
| 254 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0, |
| 255 |
"%2.2x%2.2x.%2.2x%2.2x.%2.2x%2.2x %u %s", |
| 256 |
entry->mac_addr.eth_addr_byte[0], |
| 257 |
entry->mac_addr.eth_addr_byte[1], |
| 258 |
entry->mac_addr.eth_addr_byte[2], |
| 259 |
entry->mac_addr.eth_addr_byte[3], |
| 260 |
entry->mac_addr.eth_addr_byte[4], |
| 261 |
entry->mac_addr.eth_addr_byte[5], |
| 262 |
entry->vlan_id, |
| 263 |
entry->nio->name); |
| 264 |
} |
| 265 |
|
| 266 |
static int cmd_show_mac_addr_table(hypervisor_conn_t *conn, |
| 267 |
int argc,char *argv[]) |
| 268 |
{ |
| 269 |
ethsw_table_t *t; |
| 270 |
|
| 271 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW))) |
| 272 |
return(-1); |
| 273 |
|
| 274 |
ethsw_iterate_mac_addr_table(t, |
| 275 |
(ethsw_foreach_entry_t)cmd_show_mac_addr_entry, |
| 276 |
conn); |
| 277 |
|
| 278 |
ethsw_release(argv[0]); |
| 279 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 280 |
return(0); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/* Show info about a ETHSW object */ |
| 285 |
static void cmd_show_list(registry_entry_t *entry,void *opt,int *err) |
| 286 |
{ |
| 287 |
hypervisor_conn_t *conn = opt; |
| 288 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name); |
| 289 |
} |
| 290 |
|
| 291 |
/* Ethernet switch List */ |
| 292 |
static int cmd_list(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 293 |
{ |
| 294 |
int err = 0; |
| 295 |
registry_foreach_type(OBJ_TYPE_ETHSW,cmd_show_list,conn,&err); |
| 296 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 297 |
return(0); |
| 298 |
} |
| 299 |
|
| 300 |
/* ETHSW commands */ |
| 301 |
static hypervisor_cmd_t ethsw_cmd_array[] = { |
| 302 |
{ "create", 1, 1, cmd_create, NULL }, |
| 303 |
{ "rename", 2, 2, cmd_rename, NULL }, |
| 304 |
{ "delete", 1, 1, cmd_delete, NULL }, |
| 305 |
{ "add_nio", 2, 2, cmd_add_nio, NULL }, |
| 306 |
{ "remove_nio", 2, 2, cmd_remove_nio, NULL }, |
| 307 |
{ "set_access_port", 3, 3, cmd_set_access_port, NULL }, |
| 308 |
{ "set_dot1q_port", 3, 3, cmd_set_dot1q_port, NULL }, |
| 309 |
{ "set_qinq_port", 3, 4, cmd_set_qinq_port, NULL }, |
| 310 |
{ "clear_mac_addr_table", 1, 1, cmd_clear_mac_addr_table, NULL }, |
| 311 |
{ "show_mac_addr_table", 1, 1, cmd_show_mac_addr_table, NULL }, |
| 312 |
{ "list", 0, 0, cmd_list, NULL }, |
| 313 |
{ NULL, -1, -1, NULL, NULL }, |
| 314 |
}; |
| 315 |
|
| 316 |
/* Hypervisor Ethernet switch initialization */ |
| 317 |
int hypervisor_ethsw_init(void) |
| 318 |
{ |
| 319 |
hypervisor_module_t *module; |
| 320 |
|
| 321 |
module = hypervisor_register_module("ethsw",NULL); |
| 322 |
assert(module != NULL); |
| 323 |
|
| 324 |
hypervisor_register_cmd_array(module,ethsw_cmd_array); |
| 325 |
return(0); |
| 326 |
} |