| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Hypervisor Frame-Relay 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 "frame_relay.h" |
| 28 |
#include "crc.h" |
| 29 |
#include "net_io.h" |
| 30 |
#include "registry.h" |
| 31 |
#include "hypervisor.h" |
| 32 |
|
| 33 |
/* Create a new FRSW object */ |
| 34 |
static int cmd_create(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 35 |
{ |
| 36 |
frsw_table_t *t; |
| 37 |
|
| 38 |
if (!(t = frsw_create_table(argv[0]))) { |
| 39 |
hypervisor_send_reply(conn,HSC_ERR_CREATE,1, |
| 40 |
"unable to create frame-relay switch '%s'", |
| 41 |
argv[0]); |
| 42 |
return(-1); |
| 43 |
} |
| 44 |
|
| 45 |
frsw_release(argv[0]); |
| 46 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"FRSW '%s' created",argv[0]); |
| 47 |
return(0); |
| 48 |
} |
| 49 |
|
| 50 |
/* Rename a Frame-Relay switch */ |
| 51 |
static int cmd_rename(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 52 |
{ |
| 53 |
frsw_table_t *t; |
| 54 |
char *newname; |
| 55 |
|
| 56 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_FRSW))) |
| 57 |
return(-1); |
| 58 |
|
| 59 |
if (registry_exists(argv[1],OBJ_TYPE_FRSW)) { |
| 60 |
frsw_release(argv[0]); |
| 61 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 62 |
"unable to rename FRSW '%s', '%s' already exists", |
| 63 |
argv[0],argv[1]); |
| 64 |
return(-1); |
| 65 |
} |
| 66 |
|
| 67 |
if(!(newname = mp_strdup(&t->mp,argv[1]))) { |
| 68 |
frsw_release(argv[0]); |
| 69 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 70 |
"unable to rename FRSW '%s', out of memory", |
| 71 |
argv[0]); |
| 72 |
return(-1); |
| 73 |
} |
| 74 |
|
| 75 |
if (registry_rename(argv[0],newname,OBJ_TYPE_FRSW)) { |
| 76 |
mp_free(newname); |
| 77 |
frsw_release(argv[0]); |
| 78 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 79 |
"unable to rename FRSW '%s'", |
| 80 |
argv[0]); |
| 81 |
return(-1); |
| 82 |
} |
| 83 |
|
| 84 |
mp_free(t->name); |
| 85 |
t->name = newname; |
| 86 |
|
| 87 |
frsw_release(argv[1]); |
| 88 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"FRSW '%s' renamed to '%s'",argv[0],argv[1]); |
| 89 |
return(0); |
| 90 |
} |
| 91 |
|
| 92 |
/* Delete a Frame-Relay switch */ |
| 93 |
static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 94 |
{ |
| 95 |
int res; |
| 96 |
|
| 97 |
res = frsw_delete(argv[0]); |
| 98 |
|
| 99 |
if (res == 1) { |
| 100 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"FRSW '%s' deleted",argv[0]); |
| 101 |
} else { |
| 102 |
hypervisor_send_reply(conn,HSC_ERR_DELETE,1, |
| 103 |
"unable to delete FRSW '%s'",argv[0]); |
| 104 |
} |
| 105 |
|
| 106 |
return(res); |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* Create a Virtual Circuit |
| 111 |
* |
| 112 |
* Parameters: <frsw_name> <input_nio> <input_dlci> <output_nio> <output_dlci> |
| 113 |
*/ |
| 114 |
static int cmd_create_vc(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 115 |
{ |
| 116 |
frsw_table_t *t; |
| 117 |
|
| 118 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_FRSW))) |
| 119 |
return(-1); |
| 120 |
|
| 121 |
/* create the connection */ |
| 122 |
if (frsw_create_vc(t,argv[1],atoi(argv[2]),argv[3],atoi(argv[4])) == -1) { |
| 123 |
frsw_release(argv[0]); |
| 124 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1,"unable to create VC"); |
| 125 |
return(-1); |
| 126 |
} |
| 127 |
|
| 128 |
frsw_release(argv[0]); |
| 129 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"VC created"); |
| 130 |
return(0); |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* Delete a Virtual Circuit |
| 135 |
* |
| 136 |
* Parameters: <frsw_name> <input_nio> <input_dlci> <output_nio> <output_dlci> |
| 137 |
*/ |
| 138 |
static int cmd_delete_vc(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 139 |
{ |
| 140 |
frsw_table_t *t; |
| 141 |
|
| 142 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_FRSW))) |
| 143 |
return(-1); |
| 144 |
|
| 145 |
/* delete the connection */ |
| 146 |
if (frsw_delete_vc(t,argv[1],atoi(argv[2]),argv[3],atoi(argv[4])) == -1) { |
| 147 |
frsw_release(argv[0]); |
| 148 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1,"unable to delete VC"); |
| 149 |
return(-1); |
| 150 |
} |
| 151 |
|
| 152 |
frsw_release(argv[0]); |
| 153 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"VC deleted"); |
| 154 |
return(0); |
| 155 |
} |
| 156 |
|
| 157 |
/* Show info about a FRSW object */ |
| 158 |
static void cmd_show_list(registry_entry_t *entry,void *opt,int *err) |
| 159 |
{ |
| 160 |
hypervisor_conn_t *conn = opt; |
| 161 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name); |
| 162 |
} |
| 163 |
|
| 164 |
/* Frame-Relay switch List */ |
| 165 |
static int cmd_list(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 166 |
{ |
| 167 |
int err = 0; |
| 168 |
registry_foreach_type(OBJ_TYPE_FRSW,cmd_show_list,conn,&err); |
| 169 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 170 |
return(0); |
| 171 |
} |
| 172 |
|
| 173 |
/* FRSW commands */ |
| 174 |
static hypervisor_cmd_t frsw_cmd_array[] = { |
| 175 |
{ "create", 1, 1, cmd_create, NULL }, |
| 176 |
{ "rename", 2, 2, cmd_rename, NULL }, |
| 177 |
{ "delete", 1, 1, cmd_delete, NULL }, |
| 178 |
{ "create_vc", 5, 5, cmd_create_vc, NULL }, |
| 179 |
{ "delete_vc", 5, 5, cmd_delete_vc, NULL }, |
| 180 |
{ "list", 0, 0, cmd_list, NULL }, |
| 181 |
{ NULL, -1, -1, NULL, NULL }, |
| 182 |
}; |
| 183 |
|
| 184 |
/* Hypervisor Frame-Relay switch initialization */ |
| 185 |
int hypervisor_frsw_init(void) |
| 186 |
{ |
| 187 |
hypervisor_module_t *module; |
| 188 |
|
| 189 |
module = hypervisor_register_module("frsw",NULL); |
| 190 |
assert(module != NULL); |
| 191 |
|
| 192 |
hypervisor_register_cmd_array(module,frsw_cmd_array); |
| 193 |
return(0); |
| 194 |
} |