| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Virtual Ethernet switch with VLAN/Trunk support. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <unistd.h> |
| 12 |
#include <pthread.h> |
| 13 |
#include <errno.h> |
| 14 |
#include <sys/select.h> |
| 15 |
#include <sys/time.h> |
| 16 |
#include <sys/types.h> |
| 17 |
#include <assert.h> |
| 18 |
|
| 19 |
#include "utils.h" |
| 20 |
#include "net.h" |
| 21 |
#include "registry.h" |
| 22 |
#include "net_io.h" |
| 23 |
#include "eth_switch.h" |
| 24 |
|
| 25 |
/* Debbuging message */ |
| 26 |
void ethsw_debug(ethsw_table_t *t,char *fmt,...) |
| 27 |
{ |
| 28 |
char module[128]; |
| 29 |
va_list ap; |
| 30 |
|
| 31 |
if (t->debug) { |
| 32 |
va_start(ap,fmt); |
| 33 |
snprintf(module,sizeof(module),"ETHSW %s",t->name); |
| 34 |
m_flog(log_file,module,fmt,ap); |
| 35 |
va_end(ap); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/* Compute hash index on the specified MAC address and VLAN */ |
| 40 |
static inline u_int ethsw_hash_index(n_eth_addr_t *addr,u_int vlan_id) |
| 41 |
{ |
| 42 |
u_int h_index; |
| 43 |
|
| 44 |
h_index = (addr->eth_addr_byte[0] << 8) | addr->eth_addr_byte[1]; |
| 45 |
h_index ^= (addr->eth_addr_byte[2] << 8) | addr->eth_addr_byte[3]; |
| 46 |
h_index ^= (addr->eth_addr_byte[4] << 8) | addr->eth_addr_byte[5]; |
| 47 |
h_index ^= vlan_id; |
| 48 |
return(h_index & (ETHSW_HASH_SIZE - 1)); |
| 49 |
} |
| 50 |
|
| 51 |
/* Invalidate the whole MAC address table */ |
| 52 |
static void ethsw_invalidate(ethsw_table_t *t) |
| 53 |
{ |
| 54 |
memset(t->mac_addr_table,0,sizeof(t->mac_addr_table)); |
| 55 |
} |
| 56 |
|
| 57 |
/* Invalidate entry of the MAC address table referring to the specified NIO */ |
| 58 |
static void ethsw_invalidate_port(ethsw_table_t *t,netio_desc_t *nio) |
| 59 |
{ |
| 60 |
ethsw_mac_entry_t *entry; |
| 61 |
int i; |
| 62 |
|
| 63 |
for(i=0;i<ETHSW_HASH_SIZE;i++) { |
| 64 |
entry = &t->mac_addr_table[i]; |
| 65 |
if (entry->nio == nio) { |
| 66 |
entry->nio = NULL; |
| 67 |
entry->vlan_id = 0; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/* Push a 802.1Q tag */ |
| 73 |
static void dot1q_push_tag(m_uint8_t *pkt,ethsw_packet_t *sp,u_int vlan,m_uint16_t ethertype) |
| 74 |
{ |
| 75 |
n_eth_dot1q_hdr_t *hdr; |
| 76 |
|
| 77 |
memcpy(pkt,sp->pkt,(N_ETH_HLEN - 2)); |
| 78 |
|
| 79 |
hdr = (n_eth_dot1q_hdr_t *)pkt; |
| 80 |
hdr->type = htons(ethertype); |
| 81 |
hdr->vlan_id = htons(sp->input_vlan); |
| 82 |
|
| 83 |
memcpy(pkt + sizeof(n_eth_dot1q_hdr_t), |
| 84 |
sp->pkt + (N_ETH_HLEN - 2), |
| 85 |
sp->pkt_len - (N_ETH_HLEN - 2)); |
| 86 |
} |
| 87 |
|
| 88 |
/* Pop a 802.1Q tag */ |
| 89 |
static void dot1q_pop_tag(m_uint8_t *pkt,ethsw_packet_t *sp) |
| 90 |
{ |
| 91 |
memcpy(pkt,sp->pkt,(N_ETH_HLEN - 2)); |
| 92 |
|
| 93 |
memcpy(pkt + (N_ETH_HLEN - 2), |
| 94 |
sp->pkt + sizeof(n_eth_dot1q_hdr_t), |
| 95 |
sp->pkt_len - sizeof(n_eth_dot1q_hdr_t)); |
| 96 |
} |
| 97 |
|
| 98 |
/* Input vector for ACCESS ports */ |
| 99 |
static void ethsw_iv_access(ethsw_table_t *t,ethsw_packet_t *sp, |
| 100 |
netio_desc_t *op) |
| 101 |
{ |
| 102 |
m_uint8_t pkt[ETHSW_MAX_PKT_SIZE+4]; |
| 103 |
|
| 104 |
switch(op->vlan_port_type) { |
| 105 |
/* Access -> Access: no special treatment */ |
| 106 |
case ETHSW_PORT_TYPE_ACCESS: |
| 107 |
netio_send(op,sp->pkt,sp->pkt_len); |
| 108 |
break; |
| 109 |
|
| 110 |
/* Access -> 802.1Q: push tag */ |
| 111 |
case ETHSW_PORT_TYPE_DOT1Q: |
| 112 |
/* |
| 113 |
* If the native VLAN of output port is the same as input, |
| 114 |
* forward the packet without adding the tag. |
| 115 |
*/ |
| 116 |
if (op->vlan_id == sp->input_vlan) { |
| 117 |
netio_send(op,sp->pkt,sp->pkt_len); |
| 118 |
} else { |
| 119 |
dot1q_push_tag(pkt,sp,op->vlan_id,sp->input_port->ethertype); |
| 120 |
netio_send(op,pkt,sp->pkt_len+4); |
| 121 |
} |
| 122 |
break; |
| 123 |
|
| 124 |
default: |
| 125 |
fprintf(stderr,"ethsw_iv_access: unknown port type %u\n", |
| 126 |
op->vlan_port_type); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/* Input vector for 802.1Q ports */ |
| 131 |
static void ethsw_iv_dot1q(ethsw_table_t *t,ethsw_packet_t *sp, |
| 132 |
netio_desc_t *op) |
| 133 |
{ |
| 134 |
m_uint8_t pkt[ETHSW_MAX_PKT_SIZE+4]; |
| 135 |
|
| 136 |
/* If we don't have an input tag, we work temporarily as an access port */ |
| 137 |
if (!sp->input_tag) { |
| 138 |
ethsw_iv_access(t,sp,op); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
switch(op->vlan_port_type) { |
| 143 |
/* 802.1Q -> Access: pop tag */ |
| 144 |
case ETHSW_PORT_TYPE_ACCESS: |
| 145 |
dot1q_pop_tag(pkt,sp); |
| 146 |
netio_send(op,pkt,sp->pkt_len-4); |
| 147 |
break; |
| 148 |
|
| 149 |
/* 802.1Q -> 802.1Q: pop tag if native VLAN in output otherwise no-op */ |
| 150 |
case ETHSW_PORT_TYPE_DOT1Q: |
| 151 |
if (op->vlan_id == sp->input_vlan) { |
| 152 |
dot1q_pop_tag(pkt,sp); |
| 153 |
netio_send(op,pkt,sp->pkt_len-4); |
| 154 |
} else { |
| 155 |
netio_send(op,sp->pkt,sp->pkt_len); |
| 156 |
} |
| 157 |
break; |
| 158 |
|
| 159 |
/* |
| 160 |
* 802.1Q -> QinQ: pop outer tag if native VLAN in the one specified |
| 161 |
* tunnel port. |
| 162 |
*/ |
| 163 |
case ETHSW_PORT_TYPE_QINQ: |
| 164 |
if (op->vlan_id == sp->input_vlan) { |
| 165 |
dot1q_pop_tag(pkt,sp); |
| 166 |
netio_send(op,pkt,sp->pkt_len-4); |
| 167 |
} |
| 168 |
break; |
| 169 |
|
| 170 |
default: |
| 171 |
fprintf(stderr,"ethsw_iv_dot1q: unknown port type %u\n", |
| 172 |
op->vlan_port_type); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/* Input vector for QinQ ports */ |
| 177 |
static void ethsw_iv_qinq(ethsw_table_t *t,ethsw_packet_t *sp, |
| 178 |
netio_desc_t *op) |
| 179 |
{ |
| 180 |
m_uint8_t pkt[ETHSW_MAX_PKT_SIZE+4]; |
| 181 |
|
| 182 |
switch(op->vlan_port_type) { |
| 183 |
/* QinQ -> 802.1Q: push outer tag */ |
| 184 |
case ETHSW_PORT_TYPE_DOT1Q: |
| 185 |
dot1q_push_tag(pkt,sp,sp->input_port->vlan_id,sp->input_port->ethertype); |
| 186 |
netio_send(op,pkt,sp->pkt_len+4); |
| 187 |
break; |
| 188 |
|
| 189 |
/* |
| 190 |
* QinQ -> QinQ: valid situation if we have the same customer connected |
| 191 |
* on two ports (so with identical VLAN id on tunnel ports). |
| 192 |
*/ |
| 193 |
case ETHSW_PORT_TYPE_QINQ: |
| 194 |
if (sp->input_port->vlan_id == op->vlan_id) { |
| 195 |
dot1q_pop_tag(pkt,sp); |
| 196 |
netio_send(op,pkt,sp->pkt_len-4); |
| 197 |
} |
| 198 |
break; |
| 199 |
|
| 200 |
default: |
| 201 |
fprintf(stderr,"ethsw_iv_dot1q: unknown port type %u\n", |
| 202 |
op->vlan_port_type); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/* Flood a packet */ |
| 207 |
static void ethsw_flood(ethsw_table_t *t,ethsw_packet_t *sp) |
| 208 |
{ |
| 209 |
ethsw_input_vector_t input_vector; |
| 210 |
netio_desc_t *op; |
| 211 |
int i; |
| 212 |
|
| 213 |
input_vector = sp->input_port->vlan_input_vector; |
| 214 |
assert(input_vector != NULL); |
| 215 |
|
| 216 |
for(i=0;i<ETHSW_MAX_NIO;i++) { |
| 217 |
op = t->nio[i]; |
| 218 |
|
| 219 |
if (!op || (op == sp->input_port)) |
| 220 |
continue; |
| 221 |
|
| 222 |
/* skip output port configured in access mode with a different vlan */ |
| 223 |
if ((op->vlan_port_type == ETHSW_PORT_TYPE_ACCESS) && |
| 224 |
(op->vlan_id != sp->input_vlan)) |
| 225 |
continue; |
| 226 |
|
| 227 |
/* send the packet to the output port */ |
| 228 |
input_vector(t,sp,op); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/* Forward a packet */ |
| 233 |
static void ethsw_forward(ethsw_table_t *t,ethsw_packet_t *sp) |
| 234 |
{ |
| 235 |
n_eth_hdr_t *hdr = (n_eth_hdr_t *)sp->pkt; |
| 236 |
ethsw_input_vector_t input_vector; |
| 237 |
ethsw_mac_entry_t *entry; |
| 238 |
u_int h_index; |
| 239 |
|
| 240 |
/* Learn the source MAC address */ |
| 241 |
h_index = ethsw_hash_index(&hdr->saddr,sp->input_vlan); |
| 242 |
entry = &t->mac_addr_table[h_index]; |
| 243 |
|
| 244 |
entry->nio = sp->input_port; |
| 245 |
entry->vlan_id = sp->input_vlan; |
| 246 |
entry->mac_addr = hdr->saddr; |
| 247 |
|
| 248 |
/* If we have a broadcast/multicast packet, flood it */ |
| 249 |
if (eth_addr_is_mcast(&hdr->daddr)) { |
| 250 |
ethsw_debug(t,"multicast dest, flooding packet.\n"); |
| 251 |
ethsw_flood(t,sp); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
/* Lookup on the destination MAC address (unicast) */ |
| 256 |
h_index = ethsw_hash_index(&hdr->daddr,sp->input_vlan); |
| 257 |
entry = &t->mac_addr_table[h_index]; |
| 258 |
|
| 259 |
/* If the dest MAC is unknown, flood the packet */ |
| 260 |
if (memcmp(&entry->mac_addr,&hdr->daddr,N_ETH_ALEN) || |
| 261 |
(entry->vlan_id != sp->input_vlan)) |
| 262 |
{ |
| 263 |
ethsw_debug(t,"unknown dest, flooding packet.\n"); |
| 264 |
ethsw_flood(t,sp); |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
/* Forward the packet to the output port only */ |
| 269 |
if (entry->nio != sp->input_port) { |
| 270 |
input_vector = sp->input_port->vlan_input_vector; |
| 271 |
assert(input_vector != NULL); |
| 272 |
input_vector(t,sp,entry->nio); |
| 273 |
} else { |
| 274 |
ethsw_debug(t,"source and dest ports identical, dropping.\n"); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/* Receive a packet and prepare its forwarding */ |
| 279 |
static inline int ethsw_receive(ethsw_table_t *t,netio_desc_t *nio, |
| 280 |
u_char *pkt,ssize_t pkt_len) |
| 281 |
{ |
| 282 |
n_eth_dot1q_hdr_t *dot1q_hdr; |
| 283 |
m_uint16_t ethertype; |
| 284 |
n_eth_isl_hdr_t *isl_hdr; |
| 285 |
n_eth_hdr_t *eth_hdr; |
| 286 |
n_eth_llc_hdr_t *llc_hdr; |
| 287 |
ethsw_packet_t sp; |
| 288 |
u_char *ptr; |
| 289 |
|
| 290 |
sp.input_port = nio; |
| 291 |
sp.input_vlan = 0; |
| 292 |
sp.pkt = pkt; |
| 293 |
sp.pkt_len = pkt_len; |
| 294 |
|
| 295 |
/* Skip runt packets */ |
| 296 |
if (sp.pkt_len < N_ETH_HLEN) |
| 297 |
return(-1); |
| 298 |
|
| 299 |
/* Determine the input VLAN */ |
| 300 |
switch(nio->vlan_port_type) { |
| 301 |
case ETHSW_PORT_TYPE_ACCESS: |
| 302 |
sp.input_vlan = nio->vlan_id; |
| 303 |
break; |
| 304 |
|
| 305 |
case ETHSW_PORT_TYPE_DOT1Q: |
| 306 |
dot1q_hdr = (n_eth_dot1q_hdr_t *)sp.pkt; |
| 307 |
ethertype = ntohs(dot1q_hdr->type); |
| 308 |
|
| 309 |
/* use the native VLAN if no tag is found */ |
| 310 |
if (ethertype != N_ETH_PROTO_DOT1Q && |
| 311 |
ethertype != N_ETH_PROTO_DOT1Q_2 && |
| 312 |
ethertype != N_ETH_PROTO_DOT1Q_3 && |
| 313 |
ethertype != N_ETH_PROTO_DOT1Q_4) { |
| 314 |
sp.input_vlan = nio->vlan_id; |
| 315 |
sp.input_tag = FALSE; |
| 316 |
} else { |
| 317 |
sp.input_vlan = ntohs(dot1q_hdr->vlan_id) & 0xFFF; |
| 318 |
sp.input_tag = TRUE; |
| 319 |
} |
| 320 |
break; |
| 321 |
|
| 322 |
case ETHSW_PORT_TYPE_QINQ: |
| 323 |
dot1q_hdr = (n_eth_dot1q_hdr_t *)sp.pkt; |
| 324 |
ethertype = ntohs(dot1q_hdr->type); |
| 325 |
|
| 326 |
/* Drop untagged traffic */ |
| 327 |
if (ethertype != N_ETH_PROTO_DOT1Q && |
| 328 |
ethertype != N_ETH_PROTO_DOT1Q_2 && |
| 329 |
ethertype != N_ETH_PROTO_DOT1Q_3 && |
| 330 |
ethertype != N_ETH_PROTO_DOT1Q_4) |
| 331 |
return(-1); |
| 332 |
|
| 333 |
/* The MAC address lookup is done on the outer VLAN */ |
| 334 |
sp.input_vlan = nio->vlan_id; |
| 335 |
break; |
| 336 |
|
| 337 |
case ETHSW_PORT_TYPE_ISL: |
| 338 |
/* Check that we have an ISL packet */ |
| 339 |
eth_hdr = (n_eth_hdr_t *)pkt; |
| 340 |
|
| 341 |
if (!eth_addr_is_cisco_isl(ð_hdr->daddr)) |
| 342 |
break; |
| 343 |
|
| 344 |
/* Verify LLC header */ |
| 345 |
llc_hdr = PTR_ADJUST(n_eth_llc_hdr_t *,eth_hdr,sizeof(n_eth_hdr_t)); |
| 346 |
if (!eth_llc_check_snap(llc_hdr)) |
| 347 |
break; |
| 348 |
|
| 349 |
/* Get the VLAN id */ |
| 350 |
isl_hdr = PTR_ADJUST(n_eth_isl_hdr_t *,llc_hdr, |
| 351 |
sizeof(n_eth_llc_hdr_t)); |
| 352 |
ptr = (u_char *)&isl_hdr->vlan; |
| 353 |
sp.input_vlan = (((u_int)ptr[0] << 8) | ptr[1]) >> 1; |
| 354 |
break; |
| 355 |
|
| 356 |
default: |
| 357 |
fprintf(stderr,"ethsw_receive: unknown port type %u\n", |
| 358 |
nio->vlan_port_type); |
| 359 |
return(-1); |
| 360 |
} |
| 361 |
|
| 362 |
if (sp.input_vlan != 0) |
| 363 |
ethsw_forward(t,&sp); |
| 364 |
return(0); |
| 365 |
} |
| 366 |
|
| 367 |
/* Receive a packet (handle the locking part) */ |
| 368 |
static int ethsw_recv_pkt(netio_desc_t *nio,u_char *pkt,ssize_t pkt_len, |
| 369 |
ethsw_table_t *t) |
| 370 |
{ |
| 371 |
if (ETHSW_TRYLOCK(t) == 0) { |
| 372 |
ethsw_receive(t,nio,pkt,pkt_len); |
| 373 |
ETHSW_UNLOCK(t); |
| 374 |
} |
| 375 |
return(0); |
| 376 |
} |
| 377 |
|
| 378 |
/* Set a port as an access port with the specified VLAN */ |
| 379 |
static void set_access_port(netio_desc_t *nio,u_int vlan_id) |
| 380 |
{ |
| 381 |
nio->vlan_port_type = ETHSW_PORT_TYPE_ACCESS; |
| 382 |
nio->vlan_id = vlan_id; |
| 383 |
nio->vlan_input_vector = ethsw_iv_access; |
| 384 |
nio->ethertype = N_ETH_PROTO_DOT1Q; |
| 385 |
} |
| 386 |
|
| 387 |
/* Set a port as a 802.1Q trunk port */ |
| 388 |
static void set_dot1q_port(netio_desc_t *nio,u_int native_vlan) |
| 389 |
{ |
| 390 |
nio->vlan_port_type = ETHSW_PORT_TYPE_DOT1Q; |
| 391 |
nio->vlan_id = native_vlan; |
| 392 |
nio->vlan_input_vector = ethsw_iv_dot1q; |
| 393 |
nio->ethertype = N_ETH_PROTO_DOT1Q; |
| 394 |
} |
| 395 |
|
| 396 |
/* Set a port as a Q-in-Q trunk port */ |
| 397 |
static void set_qinq_port(netio_desc_t *nio,u_int outer_vlan,m_uint16_t ethertype) |
| 398 |
{ |
| 399 |
nio->vlan_port_type = ETHSW_PORT_TYPE_QINQ; |
| 400 |
nio->vlan_id = outer_vlan; |
| 401 |
nio->vlan_input_vector = ethsw_iv_qinq; |
| 402 |
nio->ethertype = ethertype; |
| 403 |
} |
| 404 |
|
| 405 |
/* Acquire a reference to an Ethernet switch (increment reference count) */ |
| 406 |
ethsw_table_t *ethsw_acquire(char *name) |
| 407 |
{ |
| 408 |
return(registry_find(name,OBJ_TYPE_ETHSW)); |
| 409 |
} |
| 410 |
|
| 411 |
/* Release an Ethernet switch (decrement reference count) */ |
| 412 |
int ethsw_release(char *name) |
| 413 |
{ |
| 414 |
return(registry_unref(name,OBJ_TYPE_ETHSW)); |
| 415 |
} |
| 416 |
|
| 417 |
/* Create a virtual ethernet switch */ |
| 418 |
ethsw_table_t *ethsw_create(char *name) |
| 419 |
{ |
| 420 |
ethsw_table_t *t; |
| 421 |
|
| 422 |
/* Allocate a new switch structure */ |
| 423 |
if (!(t = malloc(sizeof(*t)))) |
| 424 |
return NULL; |
| 425 |
|
| 426 |
memset(t,0,sizeof(*t)); |
| 427 |
pthread_mutex_init(&t->lock,NULL); |
| 428 |
|
| 429 |
if (!(t->name = strdup(name))) |
| 430 |
goto err_name; |
| 431 |
|
| 432 |
/* Record this object in registry */ |
| 433 |
if (registry_add(t->name,OBJ_TYPE_ETHSW,t) == -1) { |
| 434 |
fprintf(stderr,"ethsw_create: unable to register switch '%s'\n",name); |
| 435 |
goto err_reg; |
| 436 |
} |
| 437 |
|
| 438 |
return t; |
| 439 |
|
| 440 |
err_reg: |
| 441 |
free(t->name); |
| 442 |
err_name: |
| 443 |
free(t); |
| 444 |
return NULL; |
| 445 |
} |
| 446 |
|
| 447 |
/* Add a NetIO descriptor to a virtual ethernet switch */ |
| 448 |
int ethsw_add_netio(ethsw_table_t *t,char *nio_name) |
| 449 |
{ |
| 450 |
netio_desc_t *nio; |
| 451 |
int i; |
| 452 |
|
| 453 |
ETHSW_LOCK(t); |
| 454 |
|
| 455 |
/* Try to find a free slot in the NIO array */ |
| 456 |
for(i=0;i<ETHSW_MAX_NIO;i++) |
| 457 |
if (t->nio[i] == NULL) |
| 458 |
break; |
| 459 |
|
| 460 |
/* No free slot found ... */ |
| 461 |
if (i == ETHSW_MAX_NIO) |
| 462 |
goto error; |
| 463 |
|
| 464 |
/* Acquire the NIO descriptor and increment its reference count */ |
| 465 |
if (!(nio = netio_acquire(nio_name))) |
| 466 |
goto error; |
| 467 |
|
| 468 |
/* By default, the port is an access port in VLAN 1 */ |
| 469 |
set_access_port(nio,1); |
| 470 |
|
| 471 |
t->nio[i] = nio; |
| 472 |
netio_rxl_add(nio,(netio_rx_handler_t)ethsw_recv_pkt,t,NULL); |
| 473 |
ETHSW_UNLOCK(t); |
| 474 |
return(0); |
| 475 |
|
| 476 |
error: |
| 477 |
ETHSW_UNLOCK(t); |
| 478 |
return(-1); |
| 479 |
} |
| 480 |
|
| 481 |
/* Free resources used by a NIO */ |
| 482 |
static void ethsw_free_nio(netio_desc_t *nio) |
| 483 |
{ |
| 484 |
netio_rxl_remove(nio); |
| 485 |
netio_release(nio->name); |
| 486 |
} |
| 487 |
|
| 488 |
/* Remove a NetIO descriptor from a virtual ethernet switch */ |
| 489 |
int ethsw_remove_netio(ethsw_table_t *t,char *nio_name) |
| 490 |
{ |
| 491 |
netio_desc_t *nio; |
| 492 |
int i; |
| 493 |
|
| 494 |
ETHSW_LOCK(t); |
| 495 |
|
| 496 |
if (!(nio = registry_exists(nio_name,OBJ_TYPE_NIO))) |
| 497 |
goto error; |
| 498 |
|
| 499 |
/* Try to find the NIO in the NIO array */ |
| 500 |
for(i=0;i<ETHSW_MAX_NIO;i++) |
| 501 |
if (t->nio[i] == nio) |
| 502 |
break; |
| 503 |
|
| 504 |
if (i == ETHSW_MAX_NIO) |
| 505 |
goto error; |
| 506 |
|
| 507 |
/* Invalidate this port in the MAC address table */ |
| 508 |
ethsw_invalidate_port(t,nio); |
| 509 |
t->nio[i] = NULL; |
| 510 |
|
| 511 |
ETHSW_UNLOCK(t); |
| 512 |
|
| 513 |
/* Remove the NIO from the RX multiplexer */ |
| 514 |
ethsw_free_nio(nio); |
| 515 |
return(0); |
| 516 |
|
| 517 |
error: |
| 518 |
ETHSW_UNLOCK(t); |
| 519 |
return(-1); |
| 520 |
} |
| 521 |
|
| 522 |
/* Clear the MAC address table */ |
| 523 |
int ethsw_clear_mac_addr_table(ethsw_table_t *t) |
| 524 |
{ |
| 525 |
ETHSW_LOCK(t); |
| 526 |
ethsw_invalidate(t); |
| 527 |
ETHSW_UNLOCK(t); |
| 528 |
return(0); |
| 529 |
} |
| 530 |
|
| 531 |
/* Iterate over all entries of the MAC address table */ |
| 532 |
int ethsw_iterate_mac_addr_table(ethsw_table_t *t,ethsw_foreach_entry_t cb, |
| 533 |
void *opt_arg) |
| 534 |
{ |
| 535 |
ethsw_mac_entry_t *entry; |
| 536 |
int i; |
| 537 |
|
| 538 |
ETHSW_LOCK(t); |
| 539 |
|
| 540 |
for(i=0;i<ETHSW_HASH_SIZE;i++) { |
| 541 |
entry = &t->mac_addr_table[i]; |
| 542 |
|
| 543 |
if (!entry->nio) |
| 544 |
continue; |
| 545 |
|
| 546 |
cb(t,entry,opt_arg); |
| 547 |
} |
| 548 |
|
| 549 |
ETHSW_UNLOCK(t); |
| 550 |
return(0); |
| 551 |
} |
| 552 |
|
| 553 |
/* Set port as an access port */ |
| 554 |
int ethsw_set_access_port(ethsw_table_t *t,char *nio_name,u_int vlan_id) |
| 555 |
{ |
| 556 |
int i,res = -1; |
| 557 |
|
| 558 |
ETHSW_LOCK(t); |
| 559 |
|
| 560 |
for(i=0;i<ETHSW_MAX_NIO;i++) |
| 561 |
if (t->nio[i] && !strcmp(t->nio[i]->name,nio_name)) { |
| 562 |
set_access_port(t->nio[i],vlan_id); |
| 563 |
res = 0; |
| 564 |
break; |
| 565 |
} |
| 566 |
|
| 567 |
ETHSW_UNLOCK(t); |
| 568 |
return(res); |
| 569 |
} |
| 570 |
|
| 571 |
/* Set port as a 802.1q trunk port */ |
| 572 |
int ethsw_set_dot1q_port(ethsw_table_t *t,char *nio_name,u_int native_vlan) |
| 573 |
{ |
| 574 |
int i,res = -1; |
| 575 |
|
| 576 |
ETHSW_LOCK(t); |
| 577 |
|
| 578 |
for(i=0;i<ETHSW_MAX_NIO;i++) |
| 579 |
if (t->nio[i] && !strcmp(t->nio[i]->name,nio_name)) { |
| 580 |
set_dot1q_port(t->nio[i],native_vlan); |
| 581 |
res = 0; |
| 582 |
break; |
| 583 |
} |
| 584 |
|
| 585 |
ETHSW_UNLOCK(t); |
| 586 |
return(res); |
| 587 |
} |
| 588 |
|
| 589 |
/* Set port as a Q-in-Q port */ |
| 590 |
int ethsw_set_qinq_port(ethsw_table_t *t,char *nio_name,u_int outer_vlan,m_uint16_t ethertype) |
| 591 |
{ |
| 592 |
int i,res = -1; |
| 593 |
|
| 594 |
if(ethertype != N_ETH_PROTO_DOT1Q && |
| 595 |
ethertype != N_ETH_PROTO_DOT1Q_2 && |
| 596 |
ethertype != N_ETH_PROTO_DOT1Q_3 && |
| 597 |
ethertype != N_ETH_PROTO_DOT1Q_4 ) { |
| 598 |
return -1; |
| 599 |
} |
| 600 |
|
| 601 |
ETHSW_LOCK(t); |
| 602 |
|
| 603 |
for(i=0;i<ETHSW_MAX_NIO;i++) |
| 604 |
if (t->nio[i] && !strcmp(t->nio[i]->name,nio_name)) { |
| 605 |
set_qinq_port(t->nio[i],outer_vlan,ethertype); |
| 606 |
res = 0; |
| 607 |
break; |
| 608 |
} |
| 609 |
|
| 610 |
ETHSW_UNLOCK(t); |
| 611 |
return(res); |
| 612 |
} |
| 613 |
|
| 614 |
/* Save the configuration of a switch */ |
| 615 |
void ethsw_save_config(ethsw_table_t *t,FILE *fd) |
| 616 |
{ |
| 617 |
netio_desc_t *nio; |
| 618 |
int i; |
| 619 |
|
| 620 |
fprintf(fd,"ethsw create %s\n",t->name); |
| 621 |
|
| 622 |
ETHSW_LOCK(t); |
| 623 |
|
| 624 |
for(i=0;i<ETHSW_MAX_NIO;i++) |
| 625 |
{ |
| 626 |
nio = t->nio[i]; |
| 627 |
|
| 628 |
fprintf(fd,"ethsw add_nio %s %s\n",t->name,nio->name); |
| 629 |
|
| 630 |
switch(nio->vlan_port_type) { |
| 631 |
case ETHSW_PORT_TYPE_ACCESS: |
| 632 |
fprintf(fd,"ethsw set_access_port %s %s %u\n", |
| 633 |
t->name,nio->name,nio->vlan_id); |
| 634 |
break; |
| 635 |
|
| 636 |
case ETHSW_PORT_TYPE_DOT1Q: |
| 637 |
fprintf(fd,"ethsw set_dot1q_port %s %s %u\n", |
| 638 |
t->name,nio->name,nio->vlan_id); |
| 639 |
break; |
| 640 |
|
| 641 |
case ETHSW_PORT_TYPE_QINQ: |
| 642 |
fprintf(fd,"ethsw set_qinq_port %s %s %u 0x%x\n", |
| 643 |
t->name,nio->name,nio->vlan_id,nio->ethertype); |
| 644 |
break; |
| 645 |
|
| 646 |
default: |
| 647 |
fprintf(stderr,"ethsw_save_config: unknown port type %u\n", |
| 648 |
nio->vlan_port_type); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
ETHSW_UNLOCK(t); |
| 653 |
|
| 654 |
fprintf(fd,"\n"); |
| 655 |
} |
| 656 |
|
| 657 |
/* Save configurations of all Ethernet switches */ |
| 658 |
static void ethsw_reg_save_config(registry_entry_t *entry,void *opt,int *err) |
| 659 |
{ |
| 660 |
ethsw_save_config((ethsw_table_t *)entry->data,(FILE *)opt); |
| 661 |
} |
| 662 |
|
| 663 |
void ethsw_save_config_all(FILE *fd) |
| 664 |
{ |
| 665 |
registry_foreach_type(OBJ_TYPE_ETHSW,ethsw_reg_save_config,fd,NULL); |
| 666 |
} |
| 667 |
|
| 668 |
/* Free resources used by a virtual ethernet switch */ |
| 669 |
static int ethsw_free(void *data,void *arg) |
| 670 |
{ |
| 671 |
ethsw_table_t *t = data; |
| 672 |
int i; |
| 673 |
|
| 674 |
for(i=0;i<ETHSW_MAX_NIO;i++) { |
| 675 |
if (!t->nio[i]) |
| 676 |
continue; |
| 677 |
|
| 678 |
ethsw_free_nio(t->nio[i]); |
| 679 |
} |
| 680 |
|
| 681 |
free(t->name); |
| 682 |
free(t); |
| 683 |
return(TRUE); |
| 684 |
} |
| 685 |
|
| 686 |
/* Delete a virtual ethernet switch */ |
| 687 |
int ethsw_delete(char *name) |
| 688 |
{ |
| 689 |
return(registry_delete_if_unused(name,OBJ_TYPE_ETHSW,ethsw_free,NULL)); |
| 690 |
} |
| 691 |
|
| 692 |
/* Delete all virtual ethernet switches */ |
| 693 |
int ethsw_delete_all(void) |
| 694 |
{ |
| 695 |
return(registry_delete_type(OBJ_TYPE_ETHSW,ethsw_free,NULL)); |
| 696 |
} |
| 697 |
|
| 698 |
/* Create a new interface */ |
| 699 |
static int ethsw_cfg_create_if(ethsw_table_t *t,char **tokens,int count) |
| 700 |
{ |
| 701 |
netio_desc_t *nio = NULL; |
| 702 |
int nio_type; |
| 703 |
|
| 704 |
nio_type = netio_get_type(tokens[2]); |
| 705 |
switch(nio_type) { |
| 706 |
case NETIO_TYPE_UNIX: |
| 707 |
if (count != 5) { |
| 708 |
fprintf(stderr,"ETHSW: invalid number of arguments " |
| 709 |
"for UNIX NIO\n"); |
| 710 |
break; |
| 711 |
} |
| 712 |
|
| 713 |
nio = netio_desc_create_unix(tokens[1],tokens[3],tokens[4]); |
| 714 |
break; |
| 715 |
|
| 716 |
case NETIO_TYPE_TAP: |
| 717 |
if (count != 4) { |
| 718 |
fprintf(stderr,"ETHSW: invalid number of arguments " |
| 719 |
"for TAP NIO\n"); |
| 720 |
break; |
| 721 |
} |
| 722 |
|
| 723 |
nio = netio_desc_create_tap(tokens[1],tokens[3]); |
| 724 |
break; |
| 725 |
|
| 726 |
case NETIO_TYPE_UDP: |
| 727 |
if (count != 6) { |
| 728 |
fprintf(stderr,"ETHSW: invalid number of arguments " |
| 729 |
"for UDP NIO\n"); |
| 730 |
break; |
| 731 |
} |
| 732 |
|
| 733 |
nio = netio_desc_create_udp(tokens[1],atoi(tokens[3]), |
| 734 |
tokens[4],atoi(tokens[5])); |
| 735 |
break; |
| 736 |
|
| 737 |
case NETIO_TYPE_TCP_CLI: |
| 738 |
if (count != 5) { |
| 739 |
fprintf(stderr,"ETHSW: invalid number of arguments " |
| 740 |
"for TCP CLI NIO\n"); |
| 741 |
break; |
| 742 |
} |
| 743 |
|
| 744 |
nio = netio_desc_create_tcp_cli(tokens[1],tokens[3],tokens[4]); |
| 745 |
break; |
| 746 |
|
| 747 |
case NETIO_TYPE_TCP_SER: |
| 748 |
if (count != 4) { |
| 749 |
fprintf(stderr,"ETHSW: invalid number of arguments " |
| 750 |
"for TCP SER NIO\n"); |
| 751 |
break; |
| 752 |
} |
| 753 |
|
| 754 |
nio = netio_desc_create_tcp_ser(tokens[1],tokens[3]); |
| 755 |
break; |
| 756 |
|
| 757 |
#ifdef GEN_ETH |
| 758 |
case NETIO_TYPE_GEN_ETH: |
| 759 |
if (count != 4) { |
| 760 |
fprintf(stderr,"ETHSW: invalid number of arguments " |
| 761 |
"for Generic Ethernet NIO\n"); |
| 762 |
break; |
| 763 |
} |
| 764 |
|
| 765 |
nio = netio_desc_create_geneth(tokens[1],tokens[3]); |
| 766 |
break; |
| 767 |
#endif |
| 768 |
|
| 769 |
#ifdef LINUX_ETH |
| 770 |
case NETIO_TYPE_LINUX_ETH: |
| 771 |
if (count != 4) { |
| 772 |
fprintf(stderr,"ETHSW: invalid number of arguments " |
| 773 |
"for Linux Ethernet NIO\n"); |
| 774 |
break; |
| 775 |
} |
| 776 |
|
| 777 |
nio = netio_desc_create_lnxeth(tokens[1],tokens[3]); |
| 778 |
break; |
| 779 |
#endif |
| 780 |
|
| 781 |
default: |
| 782 |
fprintf(stderr,"ETHSW: unknown/invalid NETIO type '%s'\n", |
| 783 |
tokens[2]); |
| 784 |
} |
| 785 |
|
| 786 |
if (!nio) { |
| 787 |
fprintf(stderr,"ETHSW: unable to create NETIO descriptor\n"); |
| 788 |
return(-1); |
| 789 |
} |
| 790 |
|
| 791 |
if (ethsw_add_netio(t,tokens[1]) == -1) { |
| 792 |
fprintf(stderr,"ETHSW: unable to add NETIO descriptor.\n"); |
| 793 |
netio_release(nio->name); |
| 794 |
return(-1); |
| 795 |
} |
| 796 |
|
| 797 |
netio_release(nio->name); |
| 798 |
return(0); |
| 799 |
} |
| 800 |
|
| 801 |
/* Set a port as an access port */ |
| 802 |
static int ethsw_cfg_set_access_port(ethsw_table_t *t,char **tokens,int count) |
| 803 |
{ |
| 804 |
/* 3 parameters: "ACCESS", IF, VLAN */ |
| 805 |
if (count != 3) { |
| 806 |
fprintf(stderr,"ETHSW: invalid access port description.\n"); |
| 807 |
return(-1); |
| 808 |
} |
| 809 |
|
| 810 |
return(ethsw_set_access_port(t,tokens[1],atoi(tokens[2]))); |
| 811 |
} |
| 812 |
|
| 813 |
/* Set a port as a 802.1q trunk port */ |
| 814 |
static int ethsw_cfg_set_dot1q_port(ethsw_table_t *t,char **tokens,int count) |
| 815 |
{ |
| 816 |
/* 3 parameters: "DOT1Q", IF, Native VLAN */ |
| 817 |
if (count != 3) { |
| 818 |
fprintf(stderr,"ETHSW: invalid trunk port description.\n"); |
| 819 |
return(-1); |
| 820 |
} |
| 821 |
|
| 822 |
return(ethsw_set_dot1q_port(t,tokens[1],atoi(tokens[2]))); |
| 823 |
} |
| 824 |
|
| 825 |
/* Set a port as a Q-in-Q port */ |
| 826 |
static int ethsw_cfg_set_qinq_port(ethsw_table_t *t,char **tokens,int count) |
| 827 |
{ |
| 828 |
m_uint16_t ethertype = N_ETH_PROTO_DOT1Q; |
| 829 |
/* 3 + 1 parameters: "QINQ", IF, Outer VLAN, Ethertype (optional) */ |
| 830 |
if (count == 4) { |
| 831 |
sscanf(tokens[3], "0x%hx", ðertype); |
| 832 |
} else if (count != 3) { |
| 833 |
fprintf(stderr,"ETHSW: invalid QinQ port description.\n"); |
| 834 |
return(-1); |
| 835 |
} |
| 836 |
|
| 837 |
return(ethsw_set_qinq_port(t,tokens[1],atoi(tokens[2]),ethertype)); |
| 838 |
} |
| 839 |
|
| 840 |
#define ETHSW_MAX_TOKENS 16 |
| 841 |
|
| 842 |
/* Handle a ETHSW configuration line */ |
| 843 |
static int ethsw_handle_cfg_line(ethsw_table_t *t,char *str) |
| 844 |
{ |
| 845 |
char *tokens[ETHSW_MAX_TOKENS]; |
| 846 |
int count; |
| 847 |
|
| 848 |
if ((count = m_strsplit(str,':',tokens,ETHSW_MAX_TOKENS)) <= 1) |
| 849 |
return(-1); |
| 850 |
|
| 851 |
if (!strcmp(tokens[0],"IF")) |
| 852 |
return(ethsw_cfg_create_if(t,tokens,count)); |
| 853 |
else if (!strcmp(tokens[0],"ACCESS")) |
| 854 |
return(ethsw_cfg_set_access_port(t,tokens,count)); |
| 855 |
else if (!strcmp(tokens[0],"DOT1Q")) |
| 856 |
return(ethsw_cfg_set_dot1q_port(t,tokens,count)); |
| 857 |
else if (!strcmp(tokens[0],"QINQ")) |
| 858 |
return(ethsw_cfg_set_qinq_port(t,tokens,count)); |
| 859 |
|
| 860 |
fprintf(stderr, |
| 861 |
"ETHSW: Unknown statement \"%s\" (allowed: IF,ACCESS,TRUNK)\n", |
| 862 |
tokens[0]); |
| 863 |
return(-1); |
| 864 |
} |
| 865 |
|
| 866 |
/* Read a ETHSW configuration file */ |
| 867 |
static int ethsw_read_cfg_file(ethsw_table_t *t,char *filename) |
| 868 |
{ |
| 869 |
char buffer[1024],*ptr; |
| 870 |
FILE *fd; |
| 871 |
|
| 872 |
if (!(fd = fopen(filename,"r"))) { |
| 873 |
perror("fopen"); |
| 874 |
return(-1); |
| 875 |
} |
| 876 |
|
| 877 |
while(!feof(fd)) { |
| 878 |
if (!fgets(buffer,sizeof(buffer),fd)) |
| 879 |
break; |
| 880 |
|
| 881 |
/* skip comments and end of line */ |
| 882 |
if ((ptr = strpbrk(buffer,"#\r\n")) != NULL) |
| 883 |
*ptr = 0; |
| 884 |
|
| 885 |
/* analyze non-empty lines */ |
| 886 |
if (strchr(buffer,':')) |
| 887 |
ethsw_handle_cfg_line(t,buffer); |
| 888 |
} |
| 889 |
|
| 890 |
fclose(fd); |
| 891 |
return(0); |
| 892 |
} |
| 893 |
|
| 894 |
/* Start a virtual Ethernet switch */ |
| 895 |
int ethsw_start(char *filename) |
| 896 |
{ |
| 897 |
ethsw_table_t *t; |
| 898 |
|
| 899 |
if (!(t = ethsw_create("default"))) { |
| 900 |
fprintf(stderr,"ETHSW: unable to create virtual fabric table.\n"); |
| 901 |
return(-1); |
| 902 |
} |
| 903 |
|
| 904 |
if (ethsw_read_cfg_file(t,filename) == -1) { |
| 905 |
fprintf(stderr,"ETHSW: unable to parse configuration file.\n"); |
| 906 |
return(-1); |
| 907 |
} |
| 908 |
|
| 909 |
ethsw_release("default"); |
| 910 |
return(0); |
| 911 |
} |