| 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_bridge.h" |
| 28 |
#include "crc.h" |
| 29 |
#include "net_io.h" |
| 30 |
#include "registry.h" |
| 31 |
#include "hypervisor.h" |
| 32 |
|
| 33 |
/* Create a new ATM bridge object */ |
| 34 |
static int cmd_create(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 35 |
{ |
| 36 |
atm_bridge_t *t; |
| 37 |
|
| 38 |
if (!(t = atm_bridge_create(argv[0]))) { |
| 39 |
hypervisor_send_reply(conn,HSC_ERR_CREATE,1, |
| 40 |
"unable to create ATM bridge '%s'", |
| 41 |
argv[0]); |
| 42 |
return(-1); |
| 43 |
} |
| 44 |
|
| 45 |
atm_bridge_release(argv[0]); |
| 46 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ATM bridge '%s' created",argv[0]); |
| 47 |
return(0); |
| 48 |
} |
| 49 |
|
| 50 |
/* Rename an ATM bridge */ |
| 51 |
static int cmd_rename(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 52 |
{ |
| 53 |
atm_bridge_t *t; |
| 54 |
char *newname; |
| 55 |
|
| 56 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATM_BRIDGE))) |
| 57 |
return(-1); |
| 58 |
|
| 59 |
if (registry_exists(argv[1],OBJ_TYPE_ATM_BRIDGE)) { |
| 60 |
atm_bridge_release(argv[0]); |
| 61 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 62 |
"unable to rename ATM bridge '%s', '%s' already exists", |
| 63 |
argv[0],argv[1]); |
| 64 |
return(-1); |
| 65 |
} |
| 66 |
|
| 67 |
if(!(newname = strdup(argv[1]))) { |
| 68 |
atm_bridge_release(argv[0]); |
| 69 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 70 |
"unable to rename ATM bridge '%s', out of memory", |
| 71 |
argv[0]); |
| 72 |
return(-1); |
| 73 |
} |
| 74 |
|
| 75 |
if (registry_rename(argv[0],newname,OBJ_TYPE_ATM_BRIDGE)) { |
| 76 |
free(newname); |
| 77 |
atm_bridge_release(argv[0]); |
| 78 |
hypervisor_send_reply(conn,HSC_ERR_RENAME,1, |
| 79 |
"unable to rename ATM bridge '%s'", |
| 80 |
argv[0]); |
| 81 |
return(-1); |
| 82 |
} |
| 83 |
|
| 84 |
free(t->name); |
| 85 |
t->name = newname; |
| 86 |
|
| 87 |
atm_bridge_release(argv[1]); |
| 88 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ATM bridge '%s' renamed to '%s'",argv[0],argv[1]); |
| 89 |
return(0); |
| 90 |
} |
| 91 |
|
| 92 |
/* Delete an ATM bridge */ |
| 93 |
static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 94 |
{ |
| 95 |
int res; |
| 96 |
|
| 97 |
res = atm_bridge_delete(argv[0]); |
| 98 |
|
| 99 |
if (res == 1) { |
| 100 |
hypervisor_send_reply(conn,HSC_INFO_OK,1, |
| 101 |
"ATM bridge '%s' deleted",argv[0]); |
| 102 |
} else { |
| 103 |
hypervisor_send_reply(conn,HSC_ERR_DELETE,1, |
| 104 |
"unable to delete ATM bridge '%s'",argv[0]); |
| 105 |
} |
| 106 |
|
| 107 |
return(res); |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
* Configure an ATM bridge |
| 112 |
* |
| 113 |
* Parameters: <atmbr_name> <eth_nio> <atm_nio> <vpi> <vci> |
| 114 |
*/ |
| 115 |
static int cmd_configure(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 116 |
{ |
| 117 |
atm_bridge_t *t; |
| 118 |
|
| 119 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATM_BRIDGE))) |
| 120 |
return(-1); |
| 121 |
|
| 122 |
/* create the connection */ |
| 123 |
if (atm_bridge_configure(t,argv[1],argv[2], |
| 124 |
atoi(argv[3]),atoi(argv[4])) == -1) |
| 125 |
{ |
| 126 |
atm_bridge_release(argv[0]); |
| 127 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1, |
| 128 |
"unable to configure bridge"); |
| 129 |
return(-1); |
| 130 |
} |
| 131 |
|
| 132 |
atm_bridge_release(argv[0]); |
| 133 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ATM bridge configured"); |
| 134 |
return(0); |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* Unconfigure a bridge |
| 139 |
*/ |
| 140 |
static int cmd_unconfigure(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 141 |
{ |
| 142 |
atm_bridge_t *t; |
| 143 |
|
| 144 |
if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ATM_BRIDGE))) |
| 145 |
return(-1); |
| 146 |
|
| 147 |
if (atm_bridge_unconfigure(t) == -1) { |
| 148 |
atm_bridge_release(argv[0]); |
| 149 |
hypervisor_send_reply(conn,HSC_ERR_BINDING,1, |
| 150 |
"unable to unconfigure bridge"); |
| 151 |
return(-1); |
| 152 |
} |
| 153 |
|
| 154 |
atm_bridge_release(argv[0]); |
| 155 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"ATM bridge unconfigured"); |
| 156 |
return(0); |
| 157 |
} |
| 158 |
|
| 159 |
/* Show info about a ATM bridge object */ |
| 160 |
static void cmd_show_list(registry_entry_t *entry,void *opt,int *err) |
| 161 |
{ |
| 162 |
hypervisor_conn_t *conn = opt; |
| 163 |
hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name); |
| 164 |
} |
| 165 |
|
| 166 |
/* ATM bridge List */ |
| 167 |
static int cmd_list(hypervisor_conn_t *conn,int argc,char *argv[]) |
| 168 |
{ |
| 169 |
int err = 0; |
| 170 |
registry_foreach_type(OBJ_TYPE_ATM_BRIDGE,cmd_show_list,conn,&err); |
| 171 |
hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK"); |
| 172 |
return(0); |
| 173 |
} |
| 174 |
|
| 175 |
/* ATM Bridge commands */ |
| 176 |
static hypervisor_cmd_t atmbr_cmd_array[] = { |
| 177 |
{ "create", 1, 1, cmd_create, NULL }, |
| 178 |
{ "rename", 2, 2, cmd_rename, NULL }, |
| 179 |
{ "delete", 1, 1, cmd_delete, NULL }, |
| 180 |
{ "configure", 5, 5, cmd_configure, NULL }, |
| 181 |
{ "unconfigure", 1, 1, cmd_unconfigure, NULL }, |
| 182 |
{ "list", 0, 0, cmd_list, NULL }, |
| 183 |
{ NULL, -1, -1, NULL, NULL }, |
| 184 |
}; |
| 185 |
|
| 186 |
/* Hypervisor ATM bridge initialization */ |
| 187 |
int hypervisor_atm_bridge_init(void) |
| 188 |
{ |
| 189 |
hypervisor_module_t *module; |
| 190 |
|
| 191 |
module = hypervisor_register_module("atm_bridge",NULL); |
| 192 |
assert(module != NULL); |
| 193 |
|
| 194 |
hypervisor_register_cmd_array(module,atmbr_cmd_array); |
| 195 |
return(0); |
| 196 |
} |