| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Virtual Ethernet switch definitions. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __ETH_SWITCH_H__ |
| 9 |
#define __ETH_SWITCH_H__ |
| 10 |
|
| 11 |
#include <pthread.h> |
| 12 |
|
| 13 |
#include "utils.h" |
| 14 |
#include "net.h" |
| 15 |
#include "net_io.h" |
| 16 |
|
| 17 |
/* Hash entries for the MAC address table */ |
| 18 |
#define ETHSW_HASH_SIZE 4096 |
| 19 |
|
| 20 |
/* Maximum port number */ |
| 21 |
#define ETHSW_MAX_NIO 64 |
| 22 |
|
| 23 |
/* Maximum packet size */ |
| 24 |
#define ETHSW_MAX_PKT_SIZE 2048 |
| 25 |
|
| 26 |
/* Port types: access, 802.1Q, 802.1Q tunnel (QinQ) */ |
| 27 |
enum { |
| 28 |
ETHSW_PORT_TYPE_ACCESS = 1, |
| 29 |
ETHSW_PORT_TYPE_DOT1Q, |
| 30 |
ETHSW_PORT_TYPE_QINQ, |
| 31 |
ETHSW_PORT_TYPE_ISL, |
| 32 |
}; |
| 33 |
|
| 34 |
/* Received packet */ |
| 35 |
typedef struct ethsw_packet ethsw_packet_t; |
| 36 |
struct ethsw_packet { |
| 37 |
u_char *pkt; |
| 38 |
ssize_t pkt_len; |
| 39 |
netio_desc_t *input_port; |
| 40 |
u_int input_vlan; |
| 41 |
int input_tag; |
| 42 |
}; |
| 43 |
|
| 44 |
/* MAC address table entry */ |
| 45 |
typedef struct ethsw_mac_entry ethsw_mac_entry_t; |
| 46 |
struct ethsw_mac_entry { |
| 47 |
netio_desc_t *nio; |
| 48 |
n_eth_addr_t mac_addr; |
| 49 |
m_uint16_t vlan_id; |
| 50 |
}; |
| 51 |
|
| 52 |
/* Virtual Ethernet switch */ |
| 53 |
typedef struct ethsw_table ethsw_table_t; |
| 54 |
struct ethsw_table { |
| 55 |
char *name; |
| 56 |
pthread_mutex_t lock; |
| 57 |
int debug; |
| 58 |
|
| 59 |
/* Virtual Ports */ |
| 60 |
netio_desc_t *nio[ETHSW_MAX_NIO]; |
| 61 |
|
| 62 |
/* MAC address table */ |
| 63 |
ethsw_mac_entry_t mac_addr_table[ETHSW_HASH_SIZE]; |
| 64 |
}; |
| 65 |
|
| 66 |
/* Packet input vector */ |
| 67 |
typedef void (*ethsw_input_vector_t)(ethsw_table_t *t,ethsw_packet_t *sp, |
| 68 |
netio_desc_t *output_port); |
| 69 |
|
| 70 |
/* "foreach" vector */ |
| 71 |
typedef void (*ethsw_foreach_entry_t)(ethsw_table_t *t, |
| 72 |
ethsw_mac_entry_t *entry, |
| 73 |
void *opt); |
| 74 |
|
| 75 |
#define ETHSW_LOCK(t) pthread_mutex_lock(&(t)->lock) |
| 76 |
#define ETHSW_UNLOCK(t) pthread_mutex_unlock(&(t)->lock) |
| 77 |
#define ETHSW_TRYLOCK(t) pthread_mutex_trylock(&(t)->lock) |
| 78 |
|
| 79 |
/* Acquire a reference to an Ethernet switch (increment reference count) */ |
| 80 |
ethsw_table_t *ethsw_acquire(char *name); |
| 81 |
|
| 82 |
/* Release an Ethernet switch (decrement reference count) */ |
| 83 |
int ethsw_release(char *name); |
| 84 |
|
| 85 |
/* Create a virtual ethernet switch */ |
| 86 |
ethsw_table_t *ethsw_create(char *name); |
| 87 |
|
| 88 |
/* Add a NetIO descriptor to a virtual ethernet switch */ |
| 89 |
int ethsw_add_netio(ethsw_table_t *t,char *nio_name); |
| 90 |
|
| 91 |
/* Remove a NetIO descriptor from a virtual ethernet switch */ |
| 92 |
int ethsw_remove_netio(ethsw_table_t *t,char *nio_name); |
| 93 |
|
| 94 |
/* Clear the MAC address table */ |
| 95 |
int ethsw_clear_mac_addr_table(ethsw_table_t *t); |
| 96 |
|
| 97 |
/* Iterate over all entries of the MAC address table */ |
| 98 |
int ethsw_iterate_mac_addr_table(ethsw_table_t *t,ethsw_foreach_entry_t cb, |
| 99 |
void *opt_arg); |
| 100 |
|
| 101 |
/* Set port as an access port */ |
| 102 |
int ethsw_set_access_port(ethsw_table_t *t,char *nio_name,u_int vlan_id); |
| 103 |
|
| 104 |
/* Set port as a 802.1q trunk port */ |
| 105 |
int ethsw_set_dot1q_port(ethsw_table_t *t,char *nio_name,u_int native_vlan); |
| 106 |
|
| 107 |
/* Set port as a Q-in-Q port */ |
| 108 |
int ethsw_set_qinq_port(ethsw_table_t *t,char *nio_name,u_int outer_vlan,m_uint16_t ethertype); |
| 109 |
|
| 110 |
/* Save the configuration of a switch */ |
| 111 |
void ethsw_save_config(ethsw_table_t *t,FILE *fd); |
| 112 |
|
| 113 |
/* Save configurations of all Ethernet switches */ |
| 114 |
void ethsw_save_config_all(FILE *fd); |
| 115 |
|
| 116 |
/* Delete a virtual ethernet switch */ |
| 117 |
int ethsw_delete(char *name); |
| 118 |
|
| 119 |
/* Delete all virtual ethernet switches */ |
| 120 |
int ethsw_delete_all(void); |
| 121 |
|
| 122 |
/* Start a virtual Ethernet switch */ |
| 123 |
int ethsw_start(char *filename); |
| 124 |
|
| 125 |
#endif |