File: hv_ethsw.c

package info (click to toggle)
dynamips 0.2.7-0.2.8RC2-4
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 3,856 kB
  • ctags: 9,893
  • sloc: ansic: 69,846; makefile: 238; sh: 169; perl: 20
file content (255 lines) | stat: -rw-r--r-- 6,988 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Cisco router simulation platform.
 * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
 *
 * Hypervisor Ethernet switch routines.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>

#include "utils.h"
#include "net.h"
#include "eth_switch.h"
#include "crc.h"
#include "net_io.h"
#include "registry.h"
#include "hypervisor.h"

/* Create a new Ethernet switch object */
static int cmd_create(hypervisor_conn_t *conn,int argc,char *argv[])
{
   ethsw_table_t *t;

   if (!(t = ethsw_create(argv[0]))) {
      hypervisor_send_reply(conn,HSC_ERR_CREATE,1,
                            "unable to create Ethernet switch '%s'",
                            argv[0]);
      return(-1);
   }

   ethsw_release(argv[0]);
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"ETHSW '%s' created",argv[0]);
   return(0);
}

/* Delete an Ethernet switch */
static int cmd_delete(hypervisor_conn_t *conn,int argc,char *argv[])
{
   int res;

   res = ethsw_delete(argv[0]);

   if (res == 1) {
      hypervisor_send_reply(conn,HSC_INFO_OK,1,"ETHSW '%s' deleted",argv[0]);
   } else {
      hypervisor_send_reply(conn,HSC_ERR_DELETE,1,
                            "unable to delete ETHSW '%s'",argv[0]);
   }

   return(res);
}

/* 
 * Add a NIO to an Ethernet switch.
 *
 * Parameters: <ethsw_name> <nio_name>
 */
static int cmd_add_nio(hypervisor_conn_t *conn,int argc,char *argv[])
{
   ethsw_table_t *t;

   if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
      return(-1);
   
   if (ethsw_add_netio(t,argv[1]) == -1) {
      ethsw_release(argv[0]);
      hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
                            "unable to bind NIO '%s' to switch '%s'",
                            argv[1],argv[0]);
      return(-1);
   }

   ethsw_release(argv[0]);
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' bound.",argv[1]);
   return(0);
}

/* 
 * Remove a NIO from an Ethernet switch
 *
 * Parameters: <ethsw_name> <nio_name>
 */
static int cmd_remove_nio(hypervisor_conn_t *conn,int argc,char *argv[])
{
   ethsw_table_t *t;

   if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
      return(-1);
   
   if (ethsw_remove_netio(t,argv[1]) == -1) {
      ethsw_release(argv[0]);
      hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
                            "unable to bind NIO '%s' to switch '%s'",
                            argv[1],argv[0]);
      return(-1);
   }

   ethsw_release(argv[0]);
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"NIO '%s' unbound.",argv[1]);
   return(0);
}

/* 
 * Set a port as an access port.
 *
 * Parameters: <ethsw_name> <nio> <VLAN>
 */
static int cmd_set_access_port(hypervisor_conn_t *conn,int argc,char *argv[])
{
   ethsw_table_t *t;

   if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
      return(-1);
   
   if (ethsw_set_access_port(t,argv[1],atoi(argv[2])) == -1) {
      ethsw_release(argv[0]);
      hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
                            "unable to apply port settings");
      return(-1);
   }

   ethsw_release(argv[0]);   
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"Port settings OK");
   return(0);
}

/* 
 * Set a port as a trunk (802.1Q) port.
 *
 * Parameters: <ethsw_name> <nio> <native_VLAN>
 */
static int cmd_set_dot1q_port(hypervisor_conn_t *conn,int argc,char *argv[])
{
   ethsw_table_t *t;

   if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
      return(-1);
   
   if (ethsw_set_dot1q_port(t,argv[1],atoi(argv[2])) == -1) {
      ethsw_release(argv[0]);
      hypervisor_send_reply(conn,HSC_ERR_BINDING,1,
                            "unable to apply port settings");
      return(-1);
   }

   ethsw_release(argv[0]);   
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"Port settings OK");
   return(0);
}

/* Clear the MAC address table */
static int cmd_clear_mac_addr_table(hypervisor_conn_t *conn,
                                   int argc,char *argv[])
{
   ethsw_table_t *t;

   if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
      return(-1);

   ethsw_clear_mac_addr_table(t);
   ethsw_release(argv[0]);   
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
   return(0);
}

/* Show the MAC address table */
static void cmd_show_mac_addr_entry(ethsw_table_t *t,ethsw_mac_entry_t *entry,
                                    hypervisor_conn_t *conn)
{
   hypervisor_send_reply(conn,HSC_INFO_MSG,0,
                         "%2.2x%2.2x.%2.2x%2.2x.%2.2x%2.2x  %u  %s",
                         entry->mac_addr.eth_addr_byte[0],
                         entry->mac_addr.eth_addr_byte[1],
                         entry->mac_addr.eth_addr_byte[2],
                         entry->mac_addr.eth_addr_byte[3],
                         entry->mac_addr.eth_addr_byte[4],
                         entry->mac_addr.eth_addr_byte[5],
                         entry->vlan_id,
                         entry->nio->name);
}

static int cmd_show_mac_addr_table(hypervisor_conn_t *conn,
                                   int argc,char *argv[])
{
   ethsw_table_t *t;

   if (!(t = hypervisor_find_object(conn,argv[0],OBJ_TYPE_ETHSW)))
      return(-1);
   
   ethsw_iterate_mac_addr_table(t,
                                (ethsw_foreach_entry_t)cmd_show_mac_addr_entry,
                                conn);

   ethsw_release(argv[0]);   
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
   return(0);
}


/* Show info about a ETHSW object */
static void cmd_show_list(registry_entry_t *entry,void *opt,int *err)
{
   hypervisor_conn_t *conn = opt;
   hypervisor_send_reply(conn,HSC_INFO_MSG,0,"%s",entry->name);
}

/* Ethernet switch List */
static int cmd_list(hypervisor_conn_t *conn,int argc,char *argv[])
{
   int err = 0;
   registry_foreach_type(OBJ_TYPE_ETHSW,cmd_show_list,conn,&err);
   hypervisor_send_reply(conn,HSC_INFO_OK,1,"OK");
   return(0);
}

/* ETHSW commands */
static hypervisor_cmd_t ethsw_cmd_array[] = {
   { "create", 1, 1, cmd_create, NULL },
   { "delete", 1, 1, cmd_delete, NULL },
   { "add_nio", 2, 2, cmd_add_nio, NULL },
   { "remove_nio", 2, 2, cmd_remove_nio, NULL },
   { "set_access_port", 3, 3, cmd_set_access_port, NULL },
   { "set_dot1q_port", 3, 3, cmd_set_dot1q_port, NULL },
   { "clear_mac_addr_table", 1, 1, cmd_clear_mac_addr_table, NULL },
   { "show_mac_addr_table", 1, 1, cmd_show_mac_addr_table, NULL },
   { "list", 0, 0, cmd_list, NULL },
   { NULL, -1, -1, NULL, NULL },
};

/* Hypervisor Ethernet switch initialization */
int hypervisor_ethsw_init(void)
{
   hypervisor_module_t *module;

   module = hypervisor_register_module("ethsw",NULL);
   assert(module != NULL);

   hypervisor_register_cmd_array(module,ethsw_cmd_array);
   return(0);
}