ROFL-pipeline  v0.6.0dev
switch_port.c
1 #include "switch_port.h"
2 
3 #include <string.h>
4 #include "platform/lock.h"
5 #include "platform/memory.h"
6 #include "openflow/of_switch.h"
7 
8 /*
9 * Inits a port structure including statistics
10 */
11 switch_port_t* switch_port_init(char* name, bool up, port_type_t type, port_state_t state){
12 
13  switch_port_t* port;
14 
15  //Allocate space
16  port = platform_malloc_shared(sizeof(switch_port_t));
17 
18  if(!port)
19  return NULL;
20 
21  //Memset stats. Must be here!
22  platform_memset(&port->stats,0,sizeof(port->stats));
23 
24  //Clear queues
25  port->max_queues = SWITCH_PORT_MAX_QUEUES;
26  platform_memset(&port->queues,0,sizeof(port->queues));
27 
28  //Init mutexes
29  port->mutex = platform_mutex_init(NULL);
30  if(!port->mutex){
32  return NULL;
33  }
34 
35  port->stats.mutex = platform_mutex_init(NULL);
36  if(!port->stats.mutex){
37  platform_mutex_destroy(port->mutex);
39  return NULL;
40  }
41 
42  //Fill values
43  port->type = type;
44  port->up = up;
45  port->state = state;
46 
47  //Copy name
48  strncpy(port->name, name, SWITCH_PORT_MAX_LEN_NAME);
49 
50  //Set to initial values the rest
51  port->forward_packets = true;
52  port->drop_received = false;
53  port->no_flood = false;
54  port->curr = port->advertised = port->supported = port->peer = 0x0;
55  port->curr_speed = port->curr_max_speed = 0;
56  port->of_port_num = 0;
57  port->of_generate_packet_in = true;
58  port->attached_sw = NULL;
59 
60  //Platform state
61  port->platform_port_state = NULL;
62 
63  return port;
64 }
65 
66 rofl_result_t switch_port_destroy(switch_port_t* port){
67 
68  unsigned int i;
69 
70  //Destroy queues
71  for(i=0;i<SWITCH_PORT_MAX_QUEUES;i++){
72  if(port->queues[i].set)
73  __port_queue_destroy(&port->queues[i]);
74  }
75 
76  //Destroy port stats mutex
77  platform_mutex_destroy(port->stats.mutex);
78 
79  //Destroy port mutex
80  platform_mutex_destroy(port->mutex);
81 
82  //Free shared port
84 
85  return ROFL_SUCCESS;
86 }
87 
88 
89 /*
90 * Add queue to port
91 */
92 rofl_result_t switch_port_add_queue(switch_port_t* port, uint32_t id, char* name, uint16_t length, uint16_t min_rate, uint16_t max_rate){
93 
94  platform_mutex_lock(port->mutex);
95 
96  if(id >=SWITCH_PORT_MAX_QUEUES ||
97  port->queues[id].set ){
98  platform_mutex_unlock(port->mutex);
99  return ROFL_FAILURE;
100  }
101 
102  //Init switch queue
103  __port_queue_init(&port->queues[id], id, name, length, min_rate, max_rate);
104 
105  platform_mutex_unlock(port->mutex);
106  return ROFL_SUCCESS;
107 
108 }
109 
110 /*
111 * Remove queue from port
112 */
113 rofl_result_t switch_port_remove_queue(switch_port_t* port, uint32_t id){
114 
115  platform_mutex_lock(port->mutex);
116 
117  if(id >=SWITCH_PORT_MAX_QUEUES ||
118  !port->queues[id].set ){
119  platform_mutex_unlock(port->mutex);
120  return ROFL_FAILURE;
121  }
122 
123  //destroy queue
124  __port_queue_destroy(&port->queues[id]);
125 
126  platform_mutex_unlock(port->mutex);
127  return ROFL_SUCCESS;
128 }
129 
130 /*
131 * Conveninent wrappers just to avoid messing up with the bitmaps
132 */
133 void switch_port_add_capabilities(bitmap32_t* bitmap, bitmap32_t features){
134  *bitmap |= features;
135 }
136 void switch_port_remove_capabilities(bitmap32_t* bitmap, bitmap32_t features){
137  *bitmap &= (~features);
138 }
140  if(speed > PORT_FEATURE_1TB_FD)
141  return;
142  port->curr_speed = speed;
143 }
145  if(speed > PORT_FEATURE_1TB_FD)
146  return;
147  port->curr_max_speed = speed;
148 }
149 
150 
151 /*
152 * Internal call to copy the port
153 */
154 switch_port_snapshot_t* __switch_port_get_snapshot(switch_port_t* port){
155 
156  int i;
157 
158  //Allocate it
160 
161  if(!s)
162  return NULL;
163 
164  //Copy the contents of the port
165  memcpy(s, port, sizeof(switch_port_snapshot_t)); //switch_port_t == switch_port_snapshot_t
166 
167  //Leave the appropiate values blank;
168  s->platform_port_state=s->mutex=s->attached_sw=s->stats.mutex=NULL;
169  for(i=0;i<SWITCH_PORT_MAX_QUEUES;i++)
170  s->queues[i].stats.mutex = NULL;
171 
172  //Copy missing information
173  s->attached_sw = NULL;
174  s->is_attached_to_sw = (port->attached_sw != NULL);
175  if(port->attached_sw)
176  s->attached_sw_dpid = port->attached_sw->dpid;
177  else
178  s->attached_sw_dpid = 0x0;
179 
180  return s;
181 }
182 
184 
186 
187  if(!copy)
188  return NULL;
189 
190  memcpy(copy, orig, sizeof(switch_port_snapshot_t));
191 
192  return copy;
193 }
194 
196  if(port)
197  platform_free_shared(port);
198 }
199 
200 //Destroy a port name list
202  if(list){
203  platform_free_shared(list->names);
204  platform_free_shared(list);
205  }
206 }
queue_stats_t stats
Queue statistics.
Definition: port_queue.h:81
Defines the locking interface used by the library. The user of the library MUST provide an implementa...
enum port_features port_features_t
Port features.
Defines the memory management interface used by the library. The user of the library MUST provide an ...
void platform_mutex_lock(platform_mutex_t *mutex)
Locks the platform_mutex_t mutex.
void switch_port_destroy_snapshot(switch_port_snapshot_t *port)
Destroy a switch port snapshot.
Definition: switch_port.c:195
void switch_port_name_list_destroy(switch_port_name_list_t *list)
Destroys a port name list, previously generated by calling fwd_module_get_all_port_names() ...
Definition: switch_port.c:201
void platform_mutex_unlock(platform_mutex_t *mutex)
Unlocks the platform_mutex_t mutex.
void switch_port_set_current_speed(switch_port_t *port, port_features_t speed)
Sets current speed.
Definition: switch_port.c:139
port_state_t
Port state.
Definition: switch_port.h:36
switch_port_snapshot_t * switch_port_clone_snapshot(switch_port_snapshot_t *orig)
Clone a snapshot.
Definition: switch_port.c:183
Port abstraction and API.
ROFL_BEGIN_DECLS platform_mutex_t * platform_mutex_init(void *params)
Allocates and initializes a mutex.
void * platform_memset(void *src, int c, size_t length)
Sets 'c' to the whole chunk of memory.
void switch_port_add_capabilities(bitmap32_t *bitmap, bitmap32_t features)
Adds capabilities to the port.
Definition: switch_port.c:133
enum port_type port_type_t
Port type enumeration.
switch_port_t * switch_port_init(char *name, bool up, port_type_t type, port_state_t state)
Init a switch_port structure.
Definition: switch_port.c:11
rofl_result_t switch_port_remove_queue(switch_port_t *port, uint32_t id)
Remove queue from port.
Definition: switch_port.c:113
Port abstraction.
Definition: switch_port.h:145
rofl_result_t switch_port_destroy(switch_port_t *port)
Destroy a switch_port structure.
Definition: switch_port.c:66
void platform_mutex_destroy(platform_mutex_t *mutex)
Destroys and deallocates a mutex previously inited by platform_mutex_init().
rofl_result_t switch_port_add_queue(switch_port_t *port, uint32_t id, char *name, uint16_t length, uint16_t min_rate, uint16_t max_rate)
Add queue to port.
Definition: switch_port.c:92
void switch_port_set_current_max_speed(switch_port_t *port, port_features_t speed)
Sets current MAX speed.
Definition: switch_port.c:144
void switch_port_remove_capabilities(bitmap32_t *bitmap, bitmap32_t features)
Removes capabilities to the port.
Definition: switch_port.c:136
void * platform_malloc_shared(size_t length)
Allocates a chunk of dynamic memory of size length, which must be accessible (R/W) for all the thread...
OpenFlow logical switch meta-abstraction.
void platform_free_shared(void *data)
Frees a chunk of dynamic memory previously allocated with platform_malloc_shared().