| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* ATM utility functions and Virtual ATM switch. |
| 6 |
* |
| 7 |
* HEC and AAL5 CRC computation functions are from Charles Michael Heard |
| 8 |
* and can be found at (no licence specified, this is to check!): |
| 9 |
* |
| 10 |
* http://cell-relay.indiana.edu/cell-relay/publications/software/CRC/ |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
#include <unistd.h> |
| 17 |
#include <pthread.h> |
| 18 |
#include <errno.h> |
| 19 |
#include <sys/select.h> |
| 20 |
#include <sys/time.h> |
| 21 |
#include <sys/types.h> |
| 22 |
|
| 23 |
#include "utils.h" |
| 24 |
#include "registry.h" |
| 25 |
#include "atm.h" |
| 26 |
#include "net_io.h" |
| 27 |
|
| 28 |
/* RFC1483 bridged mode header */ |
| 29 |
m_uint8_t atm_rfc1483b_header[ATM_RFC1483B_HLEN] = { |
| 30 |
0xaa, 0xaa, 0x03, 0x00, 0x80, 0xc2, 0x00, 0x07, 0x00, 0x00, |
| 31 |
}; |
| 32 |
|
| 33 |
/********************************************************************/ |
| 34 |
#define HEC_GENERATOR 0x107 /* x^8 + x^2 + x + 1 */ |
| 35 |
#define COSET_LEADER 0x055 /* x^6 + x^4 + x^2 + 1 */ |
| 36 |
|
| 37 |
m_uint8_t hec_syndrome_table[256]; |
| 38 |
|
| 39 |
/* Generate a table of CRC-8 syndromes for all possible input bytes */ |
| 40 |
static void gen_syndrome_table(void) |
| 41 |
{ |
| 42 |
int i,j,syndrome; |
| 43 |
|
| 44 |
for(i=0;i<256;i++) { |
| 45 |
syndrome = i; |
| 46 |
|
| 47 |
for(j=0;j<8;j++) { |
| 48 |
if (syndrome & 0x80) |
| 49 |
syndrome = (syndrome << 1) ^ HEC_GENERATOR; |
| 50 |
else |
| 51 |
syndrome = (syndrome << 1); |
| 52 |
} |
| 53 |
hec_syndrome_table[i] = (unsigned char)syndrome; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/* Compute HEC field for ATM header */ |
| 58 |
m_uint8_t atm_compute_hec(m_uint8_t *cell_header) |
| 59 |
{ |
| 60 |
register m_uint8_t hec_accum = 0; |
| 61 |
register int i; |
| 62 |
|
| 63 |
/* |
| 64 |
* calculate CRC-8 remainder over first four bytes of cell header. |
| 65 |
* exclusive-or with coset leader & insert into fifth header byte. |
| 66 |
*/ |
| 67 |
for(i=0;i<4;i++) |
| 68 |
hec_accum = hec_syndrome_table[hec_accum ^ cell_header[i]]; |
| 69 |
|
| 70 |
return(hec_accum ^ COSET_LEADER); |
| 71 |
} |
| 72 |
|
| 73 |
/* Insert HEC field into an ATM header */ |
| 74 |
void atm_insert_hec(m_uint8_t *cell_header) |
| 75 |
{ |
| 76 |
cell_header[4] = atm_compute_hec(cell_header); |
| 77 |
} |
| 78 |
|
| 79 |
/* Initialize ATM code (for HEC checksums) */ |
| 80 |
void atm_init(void) |
| 81 |
{ |
| 82 |
gen_syndrome_table(); |
| 83 |
} |
| 84 |
|
| 85 |
/* VPC hash function */ |
| 86 |
static inline u_int atmsw_vpc_hash(u_int vpi) |
| 87 |
{ |
| 88 |
return((vpi ^ (vpi >> 8)) & (ATMSW_VP_HASH_SIZE-1)); |
| 89 |
} |
| 90 |
|
| 91 |
/* VCC hash function */ |
| 92 |
static inline u_int atmsw_vcc_hash(u_int vpi,u_int vci) |
| 93 |
{ |
| 94 |
return((vpi ^ vci) & (ATMSW_VC_HASH_SIZE-1)); |
| 95 |
} |
| 96 |
|
| 97 |
/* VP lookup */ |
| 98 |
atmsw_vp_conn_t *atmsw_vp_lookup(atmsw_table_t *t,netio_desc_t *input, |
| 99 |
u_int vpi) |
| 100 |
{ |
| 101 |
atmsw_vp_conn_t *swc; |
| 102 |
|
| 103 |
for(swc=t->vp_table[atmsw_vpc_hash(vpi)];swc;swc=swc->next) |
| 104 |
if ((swc->input == input) && (swc->vpi_in == vpi)) |
| 105 |
return swc; |
| 106 |
|
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
/* VC lookup */ |
| 111 |
atmsw_vc_conn_t *atmsw_vc_lookup(atmsw_table_t *t,netio_desc_t *input, |
| 112 |
u_int vpi,u_int vci) |
| 113 |
{ |
| 114 |
atmsw_vc_conn_t *swc; |
| 115 |
|
| 116 |
for(swc=t->vc_table[atmsw_vcc_hash(vpi,vci)];swc;swc=swc->next) |
| 117 |
if ((swc->input == input) && (swc->vpi_in == vpi) && |
| 118 |
(swc->vci_in == vci)) |
| 119 |
return swc; |
| 120 |
|
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
/* VP switching */ |
| 125 |
void atmsw_vp_switch(atmsw_vp_conn_t *vpc,m_uint8_t *cell) |
| 126 |
{ |
| 127 |
m_uint32_t atm_hdr; |
| 128 |
|
| 129 |
/* rewrite the atm header with new vpi */ |
| 130 |
atm_hdr = m_ntoh32(cell); |
| 131 |
atm_hdr &= ~ATM_HDR_VPI_MASK; |
| 132 |
atm_hdr |= vpc->vpi_out << ATM_HDR_VPI_SHIFT; |
| 133 |
m_hton32(cell,atm_hdr); |
| 134 |
|
| 135 |
/* recompute HEC field */ |
| 136 |
atm_insert_hec(cell); |
| 137 |
|
| 138 |
/* update the statistics counter */ |
| 139 |
vpc->cell_cnt++; |
| 140 |
} |
| 141 |
|
| 142 |
/* VC switching */ |
| 143 |
void atmsw_vc_switch(atmsw_vc_conn_t *vcc,m_uint8_t *cell) |
| 144 |
{ |
| 145 |
m_uint32_t atm_hdr; |
| 146 |
|
| 147 |
/* rewrite the atm header with new vpi/vci */ |
| 148 |
atm_hdr = m_ntoh32(cell); |
| 149 |
|
| 150 |
atm_hdr &= ~(ATM_HDR_VPI_MASK|ATM_HDR_VCI_MASK); |
| 151 |
atm_hdr |= vcc->vpi_out << ATM_HDR_VPI_SHIFT; |
| 152 |
atm_hdr |= vcc->vci_out << ATM_HDR_VCI_SHIFT; |
| 153 |
m_hton32(cell,atm_hdr); |
| 154 |
|
| 155 |
/* recompute HEC field */ |
| 156 |
atm_insert_hec(cell); |
| 157 |
|
| 158 |
/* update the statistics counter */ |
| 159 |
vcc->cell_cnt++; |
| 160 |
} |
| 161 |
|
| 162 |
/* Handle an ATM cell */ |
| 163 |
ssize_t atmsw_handle_cell(atmsw_table_t *t,netio_desc_t *input, |
| 164 |
m_uint8_t *cell) |
| 165 |
{ |
| 166 |
m_uint32_t atm_hdr,vpi,vci; |
| 167 |
netio_desc_t *output = NULL; |
| 168 |
atmsw_vp_conn_t *vpc; |
| 169 |
atmsw_vc_conn_t *vcc; |
| 170 |
ssize_t len; |
| 171 |
|
| 172 |
/* Extract VPI/VCI information */ |
| 173 |
atm_hdr = m_ntoh32(cell); |
| 174 |
|
| 175 |
vpi = (atm_hdr & ATM_HDR_VPI_MASK) >> ATM_HDR_VPI_SHIFT; |
| 176 |
vci = (atm_hdr & ATM_HDR_VCI_MASK) >> ATM_HDR_VCI_SHIFT; |
| 177 |
|
| 178 |
/* VP switching */ |
| 179 |
if ((vpc = atmsw_vp_lookup(t,input,vpi)) != NULL) { |
| 180 |
atmsw_vp_switch(vpc,cell); |
| 181 |
output = vpc->output; |
| 182 |
} else { |
| 183 |
/* VC switching */ |
| 184 |
if ((vcc = atmsw_vc_lookup(t,input,vpi,vci)) != NULL) { |
| 185 |
atmsw_vc_switch(vcc,cell); |
| 186 |
output = vcc->output; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
len = netio_send(output,cell,ATM_CELL_SIZE); |
| 191 |
|
| 192 |
if (len != ATM_CELL_SIZE) { |
| 193 |
t->cell_drop++; |
| 194 |
return(-1); |
| 195 |
} |
| 196 |
|
| 197 |
return(0); |
| 198 |
} |
| 199 |
|
| 200 |
/* Receive an ATM cell */ |
| 201 |
static int atmsw_recv_cell(netio_desc_t *nio,u_char *atm_cell,ssize_t cell_len, |
| 202 |
atmsw_table_t *t) |
| 203 |
{ |
| 204 |
int res; |
| 205 |
|
| 206 |
if (cell_len != ATM_CELL_SIZE) |
| 207 |
return(-1); |
| 208 |
|
| 209 |
ATMSW_LOCK(t); |
| 210 |
res = atmsw_handle_cell(t,nio,atm_cell); |
| 211 |
ATMSW_UNLOCK(t); |
| 212 |
return(res); |
| 213 |
} |
| 214 |
|
| 215 |
/* Acquire a reference to an ATM switch (increment reference count) */ |
| 216 |
atmsw_table_t *atmsw_acquire(char *name) |
| 217 |
{ |
| 218 |
return(registry_find(name,OBJ_TYPE_ATMSW)); |
| 219 |
} |
| 220 |
|
| 221 |
/* Release an ATM switch (decrement reference count) */ |
| 222 |
int atmsw_release(char *name) |
| 223 |
{ |
| 224 |
return(registry_unref(name,OBJ_TYPE_ATMSW)); |
| 225 |
} |
| 226 |
|
| 227 |
/* Create a virtual switch table */ |
| 228 |
atmsw_table_t *atmsw_create_table(char *name) |
| 229 |
{ |
| 230 |
atmsw_table_t *t; |
| 231 |
|
| 232 |
/* Allocate a new switch structure */ |
| 233 |
if (!(t = malloc(sizeof(*t)))) |
| 234 |
return NULL; |
| 235 |
|
| 236 |
memset(t,0,sizeof(*t)); |
| 237 |
pthread_mutex_init(&t->lock,NULL); |
| 238 |
mp_create_fixed_pool(&t->mp,"ATM Switch"); |
| 239 |
|
| 240 |
if (!(t->name = mp_strdup(&t->mp,name))) |
| 241 |
goto err_name; |
| 242 |
|
| 243 |
/* Record this object in registry */ |
| 244 |
if (registry_add(t->name,OBJ_TYPE_ATMSW,t) == -1) { |
| 245 |
fprintf(stderr,"atmsw_create_table: unable to create switch '%s'\n", |
| 246 |
name); |
| 247 |
goto err_reg; |
| 248 |
} |
| 249 |
|
| 250 |
return t; |
| 251 |
|
| 252 |
err_reg: |
| 253 |
err_name: |
| 254 |
mp_free_pool(&t->mp); |
| 255 |
free(t); |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
|
| 259 |
/* Free resources used by a VPC */ |
| 260 |
static void atmsw_release_vpc(atmsw_vp_conn_t *swc) |
| 261 |
{ |
| 262 |
if (swc) { |
| 263 |
/* release input NIO */ |
| 264 |
if (swc->input) { |
| 265 |
netio_rxl_remove(swc->input); |
| 266 |
netio_release(swc->input->name); |
| 267 |
} |
| 268 |
|
| 269 |
/* release output NIO */ |
| 270 |
if (swc->output) |
| 271 |
netio_release(swc->output->name); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/* Free resources used by a VCC */ |
| 276 |
static void atmsw_release_vcc(atmsw_vc_conn_t *swc) |
| 277 |
{ |
| 278 |
if (swc) { |
| 279 |
/* release input NIO */ |
| 280 |
if (swc->input) { |
| 281 |
netio_rxl_remove(swc->input); |
| 282 |
netio_release(swc->input->name); |
| 283 |
} |
| 284 |
|
| 285 |
/* release output NIO */ |
| 286 |
if (swc->output) |
| 287 |
netio_release(swc->output->name); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/* Create a VP switch connection */ |
| 292 |
int atmsw_create_vpc(atmsw_table_t *t,char *nio_input,u_int vpi_in, |
| 293 |
char *nio_output,u_int vpi_out) |
| 294 |
{ |
| 295 |
atmsw_vp_conn_t *swc; |
| 296 |
u_int hbucket; |
| 297 |
|
| 298 |
ATMSW_LOCK(t); |
| 299 |
|
| 300 |
/* Allocate a new switch connection */ |
| 301 |
if (!(swc = mp_alloc(&t->mp,sizeof(*swc)))) { |
| 302 |
ATMSW_UNLOCK(t); |
| 303 |
return(-1); |
| 304 |
} |
| 305 |
|
| 306 |
swc->input = netio_acquire(nio_input); |
| 307 |
swc->output = netio_acquire(nio_output); |
| 308 |
swc->vpi_in = vpi_in; |
| 309 |
swc->vpi_out = vpi_out; |
| 310 |
|
| 311 |
/* Check these NIOs are valid and the input VPI does not exists */ |
| 312 |
if (!swc->input || !swc->output || atmsw_vp_lookup(t,swc->input,vpi_in)) |
| 313 |
goto error; |
| 314 |
|
| 315 |
/* Add as a RX listener */ |
| 316 |
if (netio_rxl_add(swc->input,(netio_rx_handler_t)atmsw_recv_cell, |
| 317 |
t,NULL) == -1) |
| 318 |
goto error; |
| 319 |
|
| 320 |
hbucket = atmsw_vpc_hash(vpi_in); |
| 321 |
swc->next = t->vp_table[hbucket]; |
| 322 |
t->vp_table[hbucket] = swc; |
| 323 |
ATMSW_UNLOCK(t); |
| 324 |
return(0); |
| 325 |
|
| 326 |
error: |
| 327 |
ATMSW_UNLOCK(t); |
| 328 |
atmsw_release_vpc(swc); |
| 329 |
mp_free(swc); |
| 330 |
return(-1); |
| 331 |
} |
| 332 |
|
| 333 |
/* Delete a VP switch connection */ |
| 334 |
int atmsw_delete_vpc(atmsw_table_t *t,char *nio_input,u_int vpi_in, |
| 335 |
char *nio_output,u_int vpi_out) |
| 336 |
{ |
| 337 |
netio_desc_t *input,*output; |
| 338 |
atmsw_vp_conn_t **swc,*p; |
| 339 |
u_int hbucket; |
| 340 |
|
| 341 |
ATMSW_LOCK(t); |
| 342 |
|
| 343 |
input = registry_exists(nio_input,OBJ_TYPE_NIO); |
| 344 |
output = registry_exists(nio_output,OBJ_TYPE_NIO); |
| 345 |
|
| 346 |
if (!input || !output) { |
| 347 |
ATMSW_UNLOCK(t); |
| 348 |
return(-1); |
| 349 |
} |
| 350 |
|
| 351 |
hbucket = atmsw_vpc_hash(vpi_in); |
| 352 |
for(swc=&t->vp_table[hbucket];*swc;swc=&(*swc)->next) |
| 353 |
{ |
| 354 |
p = *swc; |
| 355 |
|
| 356 |
if ((p->input == input) && (p->output == output) && |
| 357 |
(p->vpi_in == vpi_in) && (p->vpi_out == vpi_out)) |
| 358 |
{ |
| 359 |
/* found a matching VP, remove it */ |
| 360 |
*swc = (*swc)->next; |
| 361 |
ATMSW_UNLOCK(t); |
| 362 |
|
| 363 |
atmsw_release_vpc(p); |
| 364 |
mp_free(p); |
| 365 |
return(0); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
ATMSW_UNLOCK(t); |
| 370 |
return(-1); |
| 371 |
} |
| 372 |
|
| 373 |
/* Create a VC switch connection */ |
| 374 |
int atmsw_create_vcc(atmsw_table_t *t, |
| 375 |
char *input,u_int vpi_in,u_int vci_in, |
| 376 |
char *output,u_int vpi_out,u_int vci_out) |
| 377 |
{ |
| 378 |
atmsw_vc_conn_t *swc; |
| 379 |
u_int hbucket; |
| 380 |
|
| 381 |
ATMSW_LOCK(t); |
| 382 |
|
| 383 |
/* Allocate a new switch connection */ |
| 384 |
if (!(swc = mp_alloc(&t->mp,sizeof(*swc)))) { |
| 385 |
ATMSW_UNLOCK(t); |
| 386 |
return(-1); |
| 387 |
} |
| 388 |
|
| 389 |
swc->input = netio_acquire(input); |
| 390 |
swc->output = netio_acquire(output); |
| 391 |
swc->vpi_in = vpi_in; |
| 392 |
swc->vci_in = vci_in; |
| 393 |
swc->vpi_out = vpi_out; |
| 394 |
swc->vci_out = vci_out; |
| 395 |
|
| 396 |
/* Ensure that there is not already VP switching */ |
| 397 |
if (atmsw_vp_lookup(t,swc->input,vpi_in) != NULL) { |
| 398 |
fprintf(stderr,"atmsw_create_vcc: VP switching already exists for " |
| 399 |
"VPI=%u\n",vpi_in); |
| 400 |
goto error; |
| 401 |
} |
| 402 |
|
| 403 |
/* Check these NIOs are valid and the input VPI does not exists */ |
| 404 |
if (!swc->input || !swc->output || |
| 405 |
atmsw_vc_lookup(t,swc->input,vpi_in,vci_in)) |
| 406 |
goto error; |
| 407 |
|
| 408 |
/* Add as a RX listener */ |
| 409 |
if (netio_rxl_add(swc->input,(netio_rx_handler_t)atmsw_recv_cell, |
| 410 |
t,NULL) == -1) |
| 411 |
goto error; |
| 412 |
|
| 413 |
hbucket = atmsw_vcc_hash(vpi_in,vci_in); |
| 414 |
swc->next = t->vc_table[hbucket]; |
| 415 |
t->vc_table[hbucket] = swc; |
| 416 |
ATMSW_UNLOCK(t); |
| 417 |
return(0); |
| 418 |
|
| 419 |
error: |
| 420 |
ATMSW_UNLOCK(t); |
| 421 |
atmsw_release_vcc(swc); |
| 422 |
mp_free(swc); |
| 423 |
return(-1); |
| 424 |
} |
| 425 |
|
| 426 |
/* Delete a VC switch connection */ |
| 427 |
int atmsw_delete_vcc(atmsw_table_t *t, |
| 428 |
char *nio_input,u_int vpi_in,u_int vci_in, |
| 429 |
char *nio_output,u_int vpi_out,u_int vci_out) |
| 430 |
{ |
| 431 |
netio_desc_t *input,*output; |
| 432 |
atmsw_vc_conn_t **swc,*p; |
| 433 |
u_int hbucket; |
| 434 |
|
| 435 |
ATMSW_LOCK(t); |
| 436 |
|
| 437 |
input = registry_exists(nio_input,OBJ_TYPE_NIO); |
| 438 |
output = registry_exists(nio_output,OBJ_TYPE_NIO); |
| 439 |
|
| 440 |
hbucket = atmsw_vcc_hash(vpi_in,vci_in); |
| 441 |
for(swc=&t->vc_table[hbucket];*swc;swc=&(*swc)->next) |
| 442 |
{ |
| 443 |
p = *swc; |
| 444 |
|
| 445 |
if ((p->input == input) && (p->output == output) && |
| 446 |
(p->vpi_in == vpi_in) && (p->vci_in == vci_in) && |
| 447 |
(p->vpi_out == vpi_out) && (p->vci_out == vci_out)) |
| 448 |
{ |
| 449 |
/* found a matching VP, remove it */ |
| 450 |
*swc = (*swc)->next; |
| 451 |
ATMSW_UNLOCK(t); |
| 452 |
|
| 453 |
atmsw_release_vcc(p); |
| 454 |
mp_free(p); |
| 455 |
return(0); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
ATMSW_UNLOCK(t); |
| 460 |
return(-1); |
| 461 |
} |
| 462 |
|
| 463 |
/* Free resources used by an ATM switch */ |
| 464 |
static int atmsw_free(void *data,void *arg) |
| 465 |
{ |
| 466 |
atmsw_table_t *t = data; |
| 467 |
atmsw_vp_conn_t *vp; |
| 468 |
atmsw_vc_conn_t *vc; |
| 469 |
int i; |
| 470 |
|
| 471 |
/* Remove all VPs */ |
| 472 |
for(i=0;i<ATMSW_VP_HASH_SIZE;i++) |
| 473 |
for(vp=t->vp_table[i];vp;vp=vp->next) |
| 474 |
atmsw_release_vpc(vp); |
| 475 |
|
| 476 |
/* Remove all VCs */ |
| 477 |
for(i=0;i<ATMSW_VC_HASH_SIZE;i++) |
| 478 |
for(vc=t->vc_table[i];vc;vc=vc->next) |
| 479 |
atmsw_release_vcc(vc); |
| 480 |
|
| 481 |
mp_free_pool(&t->mp); |
| 482 |
free(t); |
| 483 |
return(TRUE); |
| 484 |
} |
| 485 |
|
| 486 |
/* Delete an ATM switch */ |
| 487 |
int atmsw_delete(char *name) |
| 488 |
{ |
| 489 |
return(registry_delete_if_unused(name,OBJ_TYPE_ATMSW,atmsw_free,NULL)); |
| 490 |
} |
| 491 |
|
| 492 |
/* Delete all ATM switches */ |
| 493 |
int atmsw_delete_all(void) |
| 494 |
{ |
| 495 |
return(registry_delete_type(OBJ_TYPE_ATMSW,atmsw_free,NULL)); |
| 496 |
} |
| 497 |
|
| 498 |
/* Save the configuration of an ATM switch */ |
| 499 |
void atmsw_save_config(atmsw_table_t *t,FILE *fd) |
| 500 |
{ |
| 501 |
atmsw_vp_conn_t *vp; |
| 502 |
atmsw_vc_conn_t *vc; |
| 503 |
int i; |
| 504 |
|
| 505 |
fprintf(fd,"atmsw create %s\n",t->name); |
| 506 |
|
| 507 |
ATMSW_LOCK(t); |
| 508 |
|
| 509 |
for(i=0;i<ATMSW_VP_HASH_SIZE;i++) { |
| 510 |
for(vp=t->vp_table[i];vp;vp=vp->next) { |
| 511 |
fprintf(fd,"atmsw create_vpc %s %s %u %s %u\n", |
| 512 |
t->name,vp->input->name,vp->vpi_in, |
| 513 |
vp->output->name,vp->vpi_out); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
for(i=0;i<ATMSW_VC_HASH_SIZE;i++) { |
| 518 |
for(vc=t->vc_table[i];vc;vc=vc->next) { |
| 519 |
fprintf(fd,"atmsw create_vcc %s %s %u %u %s %u %u\n", |
| 520 |
t->name,vc->input->name,vc->vpi_in,vc->vci_in, |
| 521 |
vc->output->name,vc->vpi_out,vc->vci_out); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
ATMSW_UNLOCK(t); |
| 526 |
|
| 527 |
fprintf(fd,"\n"); |
| 528 |
} |
| 529 |
|
| 530 |
/* Save configurations of all ATM switches */ |
| 531 |
static void atmsw_reg_save_config(registry_entry_t *entry,void *opt,int *err) |
| 532 |
{ |
| 533 |
atmsw_save_config((atmsw_table_t *)entry->data,(FILE *)opt); |
| 534 |
} |
| 535 |
|
| 536 |
void atmsw_save_config_all(FILE *fd) |
| 537 |
{ |
| 538 |
registry_foreach_type(OBJ_TYPE_ATMSW,atmsw_reg_save_config,fd,NULL); |
| 539 |
} |
| 540 |
|
| 541 |
/* Create a new interface */ |
| 542 |
int atmsw_cfg_create_if(atmsw_table_t *t,char **tokens,int count) |
| 543 |
{ |
| 544 |
netio_desc_t *nio = NULL; |
| 545 |
int nio_type; |
| 546 |
|
| 547 |
/* at least: IF, interface name, NetIO type */ |
| 548 |
if (count < 3) { |
| 549 |
fprintf(stderr,"atmsw_cfg_create_if: invalid interface description\n"); |
| 550 |
return(-1); |
| 551 |
} |
| 552 |
|
| 553 |
nio_type = netio_get_type(tokens[2]); |
| 554 |
switch(nio_type) { |
| 555 |
case NETIO_TYPE_UNIX: |
| 556 |
if (count != 5) { |
| 557 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 558 |
"for UNIX NIO '%s'\n",tokens[1]); |
| 559 |
break; |
| 560 |
} |
| 561 |
|
| 562 |
nio = netio_desc_create_unix(tokens[1],tokens[3],tokens[4]); |
| 563 |
break; |
| 564 |
|
| 565 |
case NETIO_TYPE_UDP: |
| 566 |
if (count != 6) { |
| 567 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 568 |
"for UDP NIO '%s'\n",tokens[1]); |
| 569 |
break; |
| 570 |
} |
| 571 |
|
| 572 |
nio = netio_desc_create_udp(tokens[1],atoi(tokens[3]), |
| 573 |
tokens[4],atoi(tokens[5])); |
| 574 |
break; |
| 575 |
|
| 576 |
case NETIO_TYPE_TCP_CLI: |
| 577 |
if (count != 5) { |
| 578 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 579 |
"for TCP CLI NIO '%s'\n",tokens[1]); |
| 580 |
break; |
| 581 |
} |
| 582 |
|
| 583 |
nio = netio_desc_create_tcp_cli(tokens[1],tokens[3],tokens[4]); |
| 584 |
break; |
| 585 |
|
| 586 |
case NETIO_TYPE_TCP_SER: |
| 587 |
if (count != 4) { |
| 588 |
fprintf(stderr,"ATMSW: invalid number of arguments " |
| 589 |
"for TCP SER NIO '%s'\n",tokens[1]); |
| 590 |
break; |
| 591 |
} |
| 592 |
|
| 593 |
nio = netio_desc_create_tcp_ser(tokens[1],tokens[3]); |
| 594 |
break; |
| 595 |
|
| 596 |
default: |
| 597 |
fprintf(stderr,"ATMSW: unknown/invalid NETIO type '%s'\n", |
| 598 |
tokens[2]); |
| 599 |
} |
| 600 |
|
| 601 |
if (!nio) { |
| 602 |
fprintf(stderr,"ATMSW: unable to create NETIO descriptor of " |
| 603 |
"interface %s\n",tokens[1]); |
| 604 |
return(-1); |
| 605 |
} |
| 606 |
|
| 607 |
netio_release(nio->name); |
| 608 |
return(0); |
| 609 |
} |
| 610 |
|
| 611 |
/* Create a new Virtual Path Connection */ |
| 612 |
int atmsw_cfg_create_vpc(atmsw_table_t *t,char **tokens,int count) |
| 613 |
{ |
| 614 |
/* 5 parameters: "VP", InputIF, InVPI, OutputIF, OutVPI */ |
| 615 |
if (count != 5) { |
| 616 |
fprintf(stderr,"ATMSW: invalid VPC descriptor.\n"); |
| 617 |
return(-1); |
| 618 |
} |
| 619 |
|
| 620 |
return(atmsw_create_vpc(t,tokens[1],atoi(tokens[2]), |
| 621 |
tokens[3],atoi(tokens[4]))); |
| 622 |
} |
| 623 |
|
| 624 |
/* Create a new Virtual Channel Connection */ |
| 625 |
int atmsw_cfg_create_vcc(atmsw_table_t *t,char **tokens,int count) |
| 626 |
{ |
| 627 |
/* 7 parameters: "VP", InputIF, InVPI/VCI, OutputIF, OutVPI/VCI */ |
| 628 |
if (count != 7) { |
| 629 |
fprintf(stderr,"ATMSW: invalid VCC descriptor.\n"); |
| 630 |
return(-1); |
| 631 |
} |
| 632 |
|
| 633 |
return(atmsw_create_vcc(t,tokens[1],atoi(tokens[2]),atoi(tokens[3]), |
| 634 |
tokens[4],atoi(tokens[5]),atoi(tokens[6]))); |
| 635 |
} |
| 636 |
|
| 637 |
#define ATMSW_MAX_TOKENS 16 |
| 638 |
|
| 639 |
/* Handle an ATMSW configuration line */ |
| 640 |
int atmsw_handle_cfg_line(atmsw_table_t *t,char *str) |
| 641 |
{ |
| 642 |
char *tokens[ATMSW_MAX_TOKENS]; |
| 643 |
int count; |
| 644 |
|
| 645 |
if ((count = m_strsplit(str,':',tokens,ATMSW_MAX_TOKENS)) <= 1) |
| 646 |
return(-1); |
| 647 |
|
| 648 |
if (!strcmp(tokens[0],"IF")) |
| 649 |
return(atmsw_cfg_create_if(t,tokens,count)); |
| 650 |
else if (!strcmp(tokens[0],"VP")) |
| 651 |
return(atmsw_cfg_create_vpc(t,tokens,count)); |
| 652 |
else if (!strcmp(tokens[0],"VC")) |
| 653 |
return(atmsw_cfg_create_vcc(t,tokens,count)); |
| 654 |
|
| 655 |
fprintf(stderr,"ATMSW: Unknown statement \"%s\" (allowed: IF,VP,VC)\n", |
| 656 |
tokens[0]); |
| 657 |
return(-1); |
| 658 |
} |
| 659 |
|
| 660 |
/* Read an ATMSW configuration file */ |
| 661 |
int atmsw_read_cfg_file(atmsw_table_t *t,char *filename) |
| 662 |
{ |
| 663 |
char buffer[1024],*ptr; |
| 664 |
FILE *fd; |
| 665 |
|
| 666 |
if (!(fd = fopen(filename,"r"))) { |
| 667 |
perror("fopen"); |
| 668 |
return(-1); |
| 669 |
} |
| 670 |
|
| 671 |
while(!feof(fd)) { |
| 672 |
if (!fgets(buffer,sizeof(buffer),fd)) |
| 673 |
break; |
| 674 |
|
| 675 |
/* skip comments and end of line */ |
| 676 |
if ((ptr = strpbrk(buffer,"#\r\n")) != NULL) |
| 677 |
*ptr = 0; |
| 678 |
|
| 679 |
/* analyze non-empty lines */ |
| 680 |
if (strchr(buffer,':')) |
| 681 |
atmsw_handle_cfg_line(t,buffer); |
| 682 |
} |
| 683 |
|
| 684 |
fclose(fd); |
| 685 |
return(0); |
| 686 |
} |
| 687 |
|
| 688 |
/* Start a virtual ATM switch */ |
| 689 |
int atmsw_start(char *filename) |
| 690 |
{ |
| 691 |
atmsw_table_t *t; |
| 692 |
|
| 693 |
if (!(t = atmsw_create_table("default"))) { |
| 694 |
fprintf(stderr,"ATMSW: unable to create virtual fabric table.\n"); |
| 695 |
return(-1); |
| 696 |
} |
| 697 |
|
| 698 |
if (atmsw_read_cfg_file(t,filename) == -1) { |
| 699 |
fprintf(stderr,"ATMSW: unable to parse configuration file.\n"); |
| 700 |
return(-1); |
| 701 |
} |
| 702 |
|
| 703 |
atmsw_release("default"); |
| 704 |
return(0); |
| 705 |
} |