| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Hypervisor ATM 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 "atm.h" |
| 28 |
#include "crc.h" |
| 29 |
#include "net_io.h" |
| 30 |
#include "registry.h" |
| 31 |
#include "hypervisor.h" |
| 32 |
|
| 33 |
/* Create a new ATMSW object */ |
| 34 |
static int cmd_create(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 35 |
{ |
| 36 |
atmsw_table_t *t; |
| 37 |
|
| 38 |
if (!(t = atmsw_create_table(argv[0]))) { |
| 39 |
hypervisor_send_reply(conn,HSC_ERR_CREATE,1, |
| 40 |
"unable to create ATM switch '%s'", |
| 41 |
argv[0]); |
| 42 |
return(-1); |
| 43 |
} |
| 44 |
|
| 45 |
atmsw_release(argv[0]); |
| 46 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ATMSW '%s' created",argv[0]); |
| 47 |
return(0); |
| 48 |
} |
| 49 |
|
| 50 |
/* Rename an ATM switch */ |
| 51 |
static int cmd_rename(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 52 |
{ |
| 53 |
atmsw_table_t *t; |
| 54 |
char *newname; |
| 55 |
|
| 56 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATMSW))) |
| 57 |
return(-1); |
| 58 |
|
| 59 |
if (registry_exists(argv[1],OBJ_TYPE_ATMSW)) { |
| 60 |
atmsw_release(argv[0]); |
| 61 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 62 |
"unable to rename ATMSW '%s', '%s' already exists", |
| 63 |
argv[0],argv[1]); |
| 64 |
return(-1); |
| 65 |
} |
| 66 |
|
| 67 |
if(!(newname = mp_strdup(&t->mp,argv[1]))) { |
| 68 |
atmsw_release(argv[0]); |
| 69 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 70 |
"unable to rename ATMSW '%s', out of memory", |
| 71 |
argv[0]); |
| 72 |
return(-1); |
| 73 |
} |
| 74 |
|
| 75 |
if (registry_rename(argv[0],newname,OBJ_TYPE_ATMSW)) { |
| 76 |
mp_free(newname); |
| 77 |
atmsw_release(argv[0]); |
| 78 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 79 |
"unable to rename ATMSW '%s'", |
| 80 |
argv[0]); |
| 81 |
return(-1); |
| 82 |
} |
| 83 |
|
| 84 |
mp_free(t->name); |
| 85 |
t->name = newname; |
| 86 |
|
| 87 |
atmsw_release(argv[1]); |
| 88 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ATMSW '%s' renamed to '%s'",argv[0],argv[1]); |
| 89 |
return(0); |
| 90 |
} |
| 91 |
|
| 92 |
/* Delete an ATM switch */ |
| 93 |
static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 94 |
{ |
| 95 |
int res; |
| 96 |
|
| 97 |
res = atmsw_delete(argv[0]); |
| 98 |
|
| 99 |
if (res == 1) { |
| 100 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ATMSW '%s' deleted",argv[0]); |
| 101 |
} else { |
| 102 |
hypervisor_send_reply(conn,HSC_ERR_DELETE,1, |
| 103 |
"unable to delete ATMSW '%s'",argv[0]); |
| 104 |
} |
| 105 |
|
| 106 |
return(res); |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* Create a Virtual Path Connection |
| 111 |
* |
| 112 |
* Parameters: <atmsw_name> <input_nio> <input_vpi> <output_nio> <output_vpi> |
| 113 |
*/ |
| 114 |
static int cmd_create_vpc(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 115 |
{ |
| 116 |
atmsw_table_t *t; |
| 117 |
|
| 118 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATMSW))) |
| 119 |
return(-1); |
| 120 |
|
| 121 |
/* create the connection */ |
| 122 |
if (atmsw_create_vpc(t,argv[1],atoi(argv[2]),argv[3],atoi(argv[4])) == -1) { |
| 123 |
atmsw_release(argv[0]); |
| 124 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1,"unable to create VPC"); |
| 125 |
return(-1); |
| 126 |
} |
| 127 |
|
| 128 |
atmsw_release(argv[0]); |
| 129 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"VPC created"); |
| 130 |
return(0); |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* Delete a Virtual Path Connection |
| 135 |
* |
| 136 |
* Parameters: <atmsw_name> <input_nio> <input_vpi> <output_nio> <output_vpi> |
| 137 |
*/ |
| 138 |
static int cmd_delete_vpc(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 139 |
{ |
| 140 |
atmsw_table_t *t; |
| 141 |
|
| 142 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATMSW))) |
| 143 |
return(-1); |
| 144 |
|
| 145 |
/* delete the connection */ |
| 146 |
if (atmsw_delete_vpc(t,argv[1],atoi(argv[2]),argv[3],atoi(argv[4])) == -1) { |
| 147 |
atmsw_release(argv[0]); |
| 148 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1,"unable to delete VPC"); |
| 149 |
return(-1); |
| 150 |
} |
| 151 |
|
| 152 |
atmsw_release(argv[0]); |
| 153 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"VPC deleted"); |
| 154 |
return(0); |
| 155 |
} |
| 156 |
|
| 157 |
/* |
| 158 |
* Create a Virtual Circuit Connection |
| 159 |
* |
| 160 |
* Parameters: <atmsw_name> <input_nio> <input_vpi> <input_vci> |
| 161 |
* <output_nio> <output_vpi> <output_vci> |
| 162 |
*/ |
| 163 |
static int cmd_create_vcc(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 164 |
{ |
| 165 |
atmsw_table_t *t; |
| 166 |
|
| 167 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATMSW))) |
| 168 |
return(-1); |
| 169 |
|
| 170 |
/* create the connection */ |
| 171 |
if (atmsw_create_vcc(t,argv[1],atoi(argv[2]),atoi(argv[3]), |
| 172 |
argv[4],atoi(argv[5]),atoi(argv[6])) == -1) |
| 173 |
{ |
| 174 |
atmsw_release(argv[0]); |
| 175 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1,"unable to create VCC"); |
| 176 |
return(-1); |
| 177 |
} |
| 178 |
|
| 179 |
atmsw_release(argv[0]); |
| 180 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"VCC created"); |
| 181 |
return(0); |
| 182 |
} |
| 183 |
|
| 184 |
/* |
| 185 |
* Delete a Virtual Circuit Connection |
| 186 |
* |
| 187 |
* Parameters: <atmsw_name> <input_nio> <input_vpi> <input_vci> |
| 188 |
* <output_nio> <output_vpi> <output_vci> |
| 189 |
*/ |
| 190 |
static int cmd_delete_vcc(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 191 |
{ |
| 192 |
atmsw_table_t *t; |
| 193 |
|
| 194 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATMSW))) |
| 195 |
return(-1); |
| 196 |
|
| 197 |
/* create the connection */ |
| 198 |
if (atmsw_delete_vcc(t,argv[1],atoi(argv[2]),atoi(argv[3]), |
| 199 |
argv[4],atoi(argv[5]),atoi(argv[6])) == -1) |
| 200 |
{ |
| 201 |
atmsw_release(argv[0]); |
| 202 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1,"unable to delete VCC"); |
| 203 |
return(-1); |
| 204 |
} |
| 205 |
|
| 206 |
atmsw_release(argv[0]); |
| 207 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"VCC deleted"); |
| 208 |
return(0); |
| 209 |
} |
| 210 |
|
| 211 |
/* Show info about a ATM switch object */ |
| 212 |
static void cmd_show_list(registry_entry_t *entry,void *opt,int *err) |
| 213 |
{ |
| 214 |
hypervisor_conn_t *conn = opt; |
| 215 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name); |
| 216 |
} |
| 217 |
|
| 218 |
/* ATM switch List */ |
| 219 |
static int cmd_list(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 220 |
{ |
| 221 |
int err = 0; |
| 222 |
registry_foreach_type(OBJ_TYPE_ATMSW,cmd_show_list,conn,&err); |
| 223 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 224 |
return(0); |
| 225 |
} |
| 226 |
|
| 227 |
/* ATMSW commands */ |
| 228 |
static hypervisor_cmd_t atmsw_cmd_array[] = { |
| 229 |
{ "create", 1, 1, cmd_create, NULL }, |
| 230 |
{ "rename", 2, 2, cmd_rename, NULL }, |
| 231 |
{ "delete", 1, 1, cmd_delete, NULL }, |
| 232 |
{ "create_vpc", 5, 5, cmd_create_vpc, NULL }, |
| 233 |
{ "delete_vpc", 5, 5, cmd_delete_vpc, NULL }, |
| 234 |
{ "create_vcc", 7, 7, cmd_create_vcc, NULL }, |
| 235 |
{ "delete_vcc", 7, 7, cmd_delete_vcc, NULL }, |
| 236 |
{ "list", 0, 0, cmd_list, NULL }, |
| 237 |
{ NULL, -1, -1, NULL, NULL }, |
| 238 |
}; |
| 239 |
|
| 240 |
/* Hypervisor ATM switch initialization */ |
| 241 |
int hypervisor_atmsw_init(void) |
| 242 |
{ |
| 243 |
hypervisor_module_t *module; |
| 244 |
|
| 245 |
module = hypervisor_register_module("atmsw",NULL); |
| 246 |
assert(module != NULL); |
| 247 |
|
| 248 |
hypervisor_register_cmd_array(module,atmsw_cmd_array); |
| 249 |
return(0); |
| 250 |
} |