A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
openflow-interface.cc
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Blake Hurd <naimorai@gmail.com>
17  */
18 #ifdef NS3_OPENFLOW
19 
20 #include "openflow-interface.h"
21 #include "openflow-switch-net-device.h"
22 
23 namespace ns3 {
24 
25 namespace ofi {
26 
27 NS_LOG_COMPONENT_DEFINE ("OpenFlowInterface");
28 
29 Stats::Stats (ofp_stats_types _type, size_t body_len)
30 {
31  type = _type;
32  size_t min_body = 0, max_body = 0;
33 
34  switch (type)
35  {
36  case OFPST_DESC:
37  break;
38  case OFPST_FLOW:
39  min_body = max_body = sizeof(ofp_flow_stats_request);
40  break;
41  case OFPST_AGGREGATE:
42  min_body = max_body = sizeof(ofp_aggregate_stats_request);
43  break;
44  case OFPST_TABLE:
45  break;
46  case OFPST_PORT:
47  min_body = 0;
48  max_body = std::numeric_limits<size_t>::max (); // Not sure about this one. This would guarantee that the body_len is always acceptable.
49  break;
50  case OFPST_PORT_TABLE:
51  break;
52  default:
53  NS_LOG_ERROR ("received stats request of unknown type " << type);
54  return; // -EINVAL;
55  }
56 
57  if ((min_body != 0 || max_body != 0) && (body_len < min_body || body_len > max_body))
58  {
59  NS_LOG_ERROR ("stats request type " << type << " with bad body length " << body_len);
60  return; // -EINVAL;
61  }
62 }
63 
64 int
65 Stats::DoInit (const void *body, int body_len, void **state)
66 {
67  switch (type)
68  {
69  case OFPST_DESC:
70  return 0;
71  case OFPST_FLOW:
72  return FlowStatsInit (body, body_len, state);
73  case OFPST_AGGREGATE:
74  return AggregateStatsInit (body, body_len, state);
75  case OFPST_TABLE:
76  return 0;
77  case OFPST_PORT:
78  return PortStatsInit (body, body_len, state);
79  case OFPST_PORT_TABLE:
80  return 0;
81  case OFPST_VENDOR:
82  return 0;
83  }
84 
85  return 0;
86 }
87 
88 int
89 Stats::DoDump (Ptr<OpenFlowSwitchNetDevice> swtch, void *state, ofpbuf *buffer)
90 {
91  switch (type)
92  {
93  case OFPST_DESC:
94  return DescStatsDump (state, buffer);
95  case OFPST_FLOW:
96  return FlowStatsDump (swtch, (FlowStatsState *)state, buffer);
97  case OFPST_AGGREGATE:
98  return AggregateStatsDump (swtch, (ofp_aggregate_stats_request *)state, buffer);
99  case OFPST_TABLE:
100  return TableStatsDump (swtch, state, buffer);
101  case OFPST_PORT:
102  return PortStatsDump (swtch, (PortStatsState *)state, buffer);
103  case OFPST_PORT_TABLE:
104  return PortTableStatsDump (swtch, state, buffer);
105  case OFPST_VENDOR:
106  return 0;
107  }
108 
109  return 0;
110 }
111 
112 void
113 Stats::DoCleanup (void *state)
114 {
115  switch (type)
116  {
117  case OFPST_DESC:
118  break;
119  case OFPST_FLOW:
120  free ((FlowStatsState *)state);
121  break;
122  case OFPST_AGGREGATE:
123  free ((ofp_aggregate_stats_request *)state);
124  break;
125  case OFPST_TABLE:
126  break;
127  case OFPST_PORT:
128  free (((PortStatsState *)state)->ports);
129  free ((PortStatsState *)state);
130  break;
131  case OFPST_PORT_TABLE:
132  break;
133  case OFPST_VENDOR:
134  break;
135  }
136 }
137 
138 int
139 Stats::DescStatsDump (void *state, ofpbuf *buffer)
140 {
141  ofp_desc_stats *ods = (ofp_desc_stats*)ofpbuf_put_zeros (buffer, sizeof *ods);
142  strncpy (ods->mfr_desc, OpenFlowSwitchNetDevice::GetManufacturerDescription (), sizeof ods->mfr_desc);
143  strncpy (ods->hw_desc, OpenFlowSwitchNetDevice::GetHardwareDescription (), sizeof ods->hw_desc);
144  strncpy (ods->sw_desc, OpenFlowSwitchNetDevice::GetSoftwareDescription (), sizeof ods->sw_desc);
145  strncpy (ods->serial_num, OpenFlowSwitchNetDevice::GetSerialNumber (), sizeof ods->serial_num);
146  return 0;
147 }
148 
149 #define MAX_FLOW_STATS_BYTES 4096
150 
151 int
152 Stats::FlowStatsInit (const void *body, int body_len, void **state)
153 {
154  const ofp_flow_stats_request *fsr = (ofp_flow_stats_request*)body;
155  FlowStatsState *s = (FlowStatsState*)xmalloc (sizeof *s);
156 
157  s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
158  memset (&s->position, 0, sizeof s->position);
159  s->rq = *fsr;
160  *state = s;
161  return 0;
162 }
163 
164 int
165 Stats_FlowDumpCallback (sw_flow *flow, void* state)
166 {
167  Stats::FlowStatsState *s = (Stats::FlowStatsState*)state;
168 
169  // Fill Flow Stats
170  ofp_flow_stats *ofs;
171  int length = sizeof *ofs + flow->sf_acts->actions_len;
172  ofs = (ofp_flow_stats*)ofpbuf_put_zeros (s->buffer, length);
173  ofs->length = htons (length);
174  ofs->table_id = s->table_idx;
175  ofs->match.wildcards = htonl (flow->key.wildcards);
176  ofs->match.in_port = flow->key.flow.in_port;
177  memcpy (ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN);
178  memcpy (ofs->match.dl_dst, flow->key.flow.dl_dst, ETH_ADDR_LEN);
179  ofs->match.dl_vlan = flow->key.flow.dl_vlan;
180  ofs->match.dl_type = flow->key.flow.dl_type;
181  ofs->match.nw_src = flow->key.flow.nw_src;
182  ofs->match.nw_dst = flow->key.flow.nw_dst;
183  ofs->match.nw_proto = flow->key.flow.nw_proto;
184  ofs->match.tp_src = flow->key.flow.tp_src;
185  ofs->match.tp_dst = flow->key.flow.tp_dst;
186  ofs->duration = htonl (s->now - flow->created);
187  ofs->priority = htons (flow->priority);
188  ofs->idle_timeout = htons (flow->idle_timeout);
189  ofs->hard_timeout = htons (flow->hard_timeout);
190  ofs->packet_count = htonll (flow->packet_count);
191  ofs->byte_count = htonll (flow->byte_count);
192  memcpy (ofs->actions, flow->sf_acts->actions, flow->sf_acts->actions_len);
193 
194  return s->buffer->size >= MAX_FLOW_STATS_BYTES;
195 }
196 
197 int
198 Stats::FlowStatsDump (Ptr<OpenFlowSwitchNetDevice> swtch, FlowStatsState* s, ofpbuf *buffer)
199 {
200  sw_flow_key match_key;
201 
202  flow_extract_match (&match_key, &s->rq.match);
203 
204  s->buffer = buffer;
205  s->now = time_now ();
206  while (s->table_idx < swtch->GetChain ()->n_tables
207  && (s->rq.table_id == 0xff || s->rq.table_id == s->table_idx))
208  {
209  sw_table *table = swtch->GetChain ()->tables[s->table_idx];
210 
211  if (table->iterate (table, &match_key, s->rq.out_port, &s->position, Stats::FlowDumpCallback, s))
212  {
213  break;
214  }
215 
216  s->table_idx++;
217  memset (&s->position, 0, sizeof s->position);
218  }
219  return s->buffer->size >= MAX_FLOW_STATS_BYTES;
220 }
221 
222 int
223 Stats::AggregateStatsInit (const void *body, int body_len, void **state)
224 {
225  //ofp_aggregate_stats_request *s = (ofp_aggregate_stats_request*)body;
226  *state = (ofp_aggregate_stats_request*)body;
227  return 0;
228 }
229 
230 int
231 Stats_AggregateDumpCallback (sw_flow *flow, void *state)
232 {
233  ofp_aggregate_stats_reply *s = (ofp_aggregate_stats_reply*)state;
234  s->packet_count += flow->packet_count;
235  s->byte_count += flow->byte_count;
236  s->flow_count++;
237  return 0;
238 }
239 
240 int
241 Stats::AggregateStatsDump (Ptr<OpenFlowSwitchNetDevice> swtch, ofp_aggregate_stats_request *s, ofpbuf *buffer)
242 {
243  ofp_aggregate_stats_request *rq = s;
244  ofp_aggregate_stats_reply *rpy = (ofp_aggregate_stats_reply*)ofpbuf_put_zeros (buffer, sizeof *rpy);
245  sw_flow_key match_key;
246  flow_extract_match (&match_key, &rq->match);
247  int table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
248 
249  sw_table_position position;
250  memset (&position, 0, sizeof position);
251 
252  while (table_idx < swtch->GetChain ()->n_tables
253  && (rq->table_id == 0xff || rq->table_id == table_idx))
254  {
255  sw_table *table = swtch->GetChain ()->tables[table_idx];
256  int error = table->iterate (table, &match_key, rq->out_port, &position, Stats::AggregateDumpCallback, rpy);
257  if (error)
258  {
259  return error;
260  }
261 
262  table_idx++;
263  memset (&position, 0, sizeof position);
264  }
265 
266  rpy->packet_count = htonll (rpy->packet_count);
267  rpy->byte_count = htonll (rpy->byte_count);
268  rpy->flow_count = htonl (rpy->flow_count);
269  return 0;
270 }
271 
272 int
273 Stats::TableStatsDump (Ptr<OpenFlowSwitchNetDevice> swtch, void *state, ofpbuf *buffer)
274 {
275  sw_chain* ft = swtch->GetChain ();
276  for (int i = 0; i < ft->n_tables; i++)
277  {
278  ofp_table_stats *ots = (ofp_table_stats*)ofpbuf_put_zeros (buffer, sizeof *ots);
279  sw_table_stats stats;
280  ft->tables[i]->stats (ft->tables[i], &stats);
281  strncpy (ots->name, stats.name, sizeof ots->name);
282  ots->table_id = i;
283  ots->wildcards = htonl (stats.wildcards);
284  ots->max_entries = htonl (stats.max_flows);
285  ots->active_count = htonl (stats.n_flows);
286  ots->lookup_count = htonll (stats.n_lookup);
287  ots->matched_count = htonll (stats.n_matched);
288  }
289  return 0;
290 }
291 
292 // stats for the port table which is similar to stats for the flow tables
293 int
294 Stats::PortTableStatsDump (Ptr<OpenFlowSwitchNetDevice> swtch, void *state, ofpbuf *buffer)
295 {
296  ofp_vport_table_stats *opts = (ofp_vport_table_stats*)ofpbuf_put_zeros (buffer, sizeof *opts);
297  opts->max_vports = htonl (swtch->GetVPortTable ().max_vports);
298  opts->active_vports = htonl (swtch->GetVPortTable ().active_vports);
299  opts->lookup_count = htonll (swtch->GetVPortTable ().lookup_count);
300  opts->port_match_count = htonll (swtch->GetVPortTable ().port_match_count);
301  opts->chain_match_count = htonll (swtch->GetVPortTable ().chain_match_count);
302 
303  return 0;
304 }
305 
306 int
307 Stats::PortStatsInit (const void *body, int body_len, void **state)
308 {
309  PortStatsState *s = (PortStatsState*)xmalloc (sizeof *s);
310 
311  // the body contains a list of port numbers
312  s->ports = (uint32_t*)xmalloc (body_len);
313  memcpy (s->ports, body, body_len);
314  s->num_ports = body_len / sizeof(uint32_t);
315 
316  *state = s;
317  return 0;
318 }
319 
320 int
321 Stats::PortStatsDump (Ptr<OpenFlowSwitchNetDevice> swtch, PortStatsState *s, ofpbuf *buffer)
322 {
323  ofp_port_stats *ops;
324  uint32_t port;
325 
326  // port stats are different depending on whether port is physical or virtual
327  for (size_t i = 0; i < s->num_ports; i++)
328  {
329  port = ntohl (s->ports[i]);
330  // physical port?
331  if (port <= OFPP_MAX)
332  {
333  Port p = swtch->GetSwitchPort (port);
334 
335  if (p.netdev == 0)
336  {
337  continue;
338  }
339 
340  ops = (ofp_port_stats*)ofpbuf_put_zeros (buffer, sizeof *ops);
341  ops->port_no = htonl (swtch->GetSwitchPortIndex (p));
342  ops->rx_packets = htonll (p.rx_packets);
343  ops->tx_packets = htonll (p.tx_packets);
344  ops->rx_bytes = htonll (p.rx_bytes);
345  ops->tx_bytes = htonll (p.tx_bytes);
346  ops->rx_dropped = htonll (-1);
347  ops->tx_dropped = htonll (p.tx_dropped);
348  ops->rx_errors = htonll (-1);
349  ops->tx_errors = htonll (-1);
350  ops->rx_frame_err = htonll (-1);
351  ops->rx_over_err = htonll (-1);
352  ops->rx_crc_err = htonll (-1);
353  ops->collisions = htonll (-1);
354  ops->mpls_ttl0_dropped = htonll (p.mpls_ttl0_dropped);
355  ops++;
356  }
357  else if (port >= OFPP_VP_START && port <= OFPP_VP_END) // virtual port?
358  {
359  // lookup the virtual port
360  vport_table_t vt = swtch->GetVPortTable ();
361  vport_table_entry *vpe = vport_table_lookup (&vt, port);
362  if (vpe == 0)
363  {
364  NS_LOG_ERROR ("vport entry not found!");
365  continue;
366  }
367  // only tx_packets and tx_bytes are really relevant for virtual ports
368  ops = (ofp_port_stats*)ofpbuf_put_zeros (buffer, sizeof *ops);
369  ops->port_no = htonl (vpe->vport);
370  ops->rx_packets = htonll (-1);
371  ops->tx_packets = htonll (vpe->packet_count);
372  ops->rx_bytes = htonll (-1);
373  ops->tx_bytes = htonll (vpe->byte_count);
374  ops->rx_dropped = htonll (-1);
375  ops->tx_dropped = htonll (-1);
376  ops->rx_errors = htonll (-1);
377  ops->tx_errors = htonll (-1);
378  ops->rx_frame_err = htonll (-1);
379  ops->rx_over_err = htonll (-1);
380  ops->rx_crc_err = htonll (-1);
381  ops->collisions = htonll (-1);
382  ops->mpls_ttl0_dropped = htonll (-1);
383  ops++;
384  }
385  }
386  return 0;
387 }
388 
389 bool
390 Action::IsValidType (ofp_action_type type)
391 {
392  switch (type)
393  {
394  case OFPAT_OUTPUT:
395  case OFPAT_SET_VLAN_VID:
396  case OFPAT_SET_VLAN_PCP:
397  case OFPAT_STRIP_VLAN:
398  case OFPAT_SET_DL_SRC:
399  case OFPAT_SET_DL_DST:
400  case OFPAT_SET_NW_SRC:
401  case OFPAT_SET_NW_DST:
402  case OFPAT_SET_TP_SRC:
403  case OFPAT_SET_TP_DST:
404  case OFPAT_SET_MPLS_LABEL:
405  case OFPAT_SET_MPLS_EXP:
406  return true;
407  default:
408  return false;
409  }
410 }
411 
412 uint16_t
413 Action::Validate (ofp_action_type type, size_t len, const sw_flow_key *key, const ofp_action_header *ah)
414 {
415  size_t size = 0;
416 
417  switch (type)
418  {
419  case OFPAT_OUTPUT:
420  {
421  if (len != sizeof(ofp_action_output))
422  {
423  return OFPBAC_BAD_LEN;
424  }
425 
426  ofp_action_output *oa = (ofp_action_output *)ah;
427 
428  // To prevent loops, make sure there's no action to send to the OFP_TABLE virtual port.
429 
430  // port is now 32-bit
431  if (oa->port == OFPP_NONE || oa->port == key->flow.in_port) // htonl(OFPP_NONE);
432  { // if (oa->port == htons(OFPP_NONE) || oa->port == key->flow.in_port)
433  return OFPBAC_BAD_OUT_PORT;
434  }
435 
436  return ACT_VALIDATION_OK;
437  }
438  case OFPAT_SET_VLAN_VID:
439  size = sizeof(ofp_action_vlan_vid);
440  break;
441  case OFPAT_SET_VLAN_PCP:
442  size = sizeof(ofp_action_vlan_pcp);
443  break;
444  case OFPAT_STRIP_VLAN:
445  size = sizeof(ofp_action_header);
446  break;
447  case OFPAT_SET_DL_SRC:
448  case OFPAT_SET_DL_DST:
449  size = sizeof(ofp_action_dl_addr);
450  break;
451  case OFPAT_SET_NW_SRC:
452  case OFPAT_SET_NW_DST:
453  size = sizeof(ofp_action_nw_addr);
454  break;
455  case OFPAT_SET_TP_SRC:
456  case OFPAT_SET_TP_DST:
457  size = sizeof(ofp_action_tp_port);
458  break;
459  case OFPAT_SET_MPLS_LABEL:
460  size = sizeof(ofp_action_mpls_label);
461  break;
462  case OFPAT_SET_MPLS_EXP:
463  size = sizeof(ofp_action_mpls_exp);
464  break;
465  default:
466  break;
467  }
468 
469  if (len != size)
470  {
471  return OFPBAC_BAD_LEN;
472  }
473  return ACT_VALIDATION_OK;
474 }
475 
476 void
477 Action::Execute (ofp_action_type type, ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah)
478 {
479  switch (type)
480  {
481  case OFPAT_OUTPUT:
482  break;
483  case OFPAT_SET_VLAN_VID:
484  set_vlan_vid (buffer, key, ah);
485  break;
486  case OFPAT_SET_VLAN_PCP:
487  set_vlan_pcp (buffer, key, ah);
488  break;
489  case OFPAT_STRIP_VLAN:
490  strip_vlan (buffer, key, ah);
491  break;
492  case OFPAT_SET_DL_SRC:
493  case OFPAT_SET_DL_DST:
494  set_dl_addr (buffer, key, ah);
495  break;
496  case OFPAT_SET_NW_SRC:
497  case OFPAT_SET_NW_DST:
498  set_nw_addr (buffer, key, ah);
499  break;
500  case OFPAT_SET_TP_SRC:
501  case OFPAT_SET_TP_DST:
502  set_tp_port (buffer, key, ah);
503  break;
504  case OFPAT_SET_MPLS_LABEL:
505  set_mpls_label (buffer, key, ah);
506  break;
507  case OFPAT_SET_MPLS_EXP:
508  set_mpls_exp (buffer, key, ah);
509  break;
510  default:
511  break;
512  }
513 }
514 
515 bool
516 VPortAction::IsValidType (ofp_vport_action_type type)
517 {
518  switch (type)
519  {
520  case OFPPAT_POP_MPLS:
521  case OFPPAT_PUSH_MPLS:
522  case OFPPAT_SET_MPLS_LABEL:
523  case OFPPAT_SET_MPLS_EXP:
524  return true;
525  default:
526  return false;
527  }
528 }
529 
530 uint16_t
531 VPortAction::Validate (ofp_vport_action_type type, size_t len, const ofp_action_header *ah)
532 {
533  size_t size = 0;
534 
535  switch (type)
536  {
537  case OFPPAT_POP_MPLS:
538  size = sizeof(ofp_vport_action_pop_mpls);
539  break;
540  case OFPPAT_PUSH_MPLS:
541  size = sizeof(ofp_vport_action_push_mpls);
542  break;
543  case OFPPAT_SET_MPLS_LABEL:
544  size = sizeof(ofp_vport_action_set_mpls_label);
545  break;
546  case OFPPAT_SET_MPLS_EXP:
547  size = sizeof(ofp_vport_action_set_mpls_exp);
548  break;
549  default:
550  break;
551  }
552 
553  if (len != size)
554  {
555  return OFPBAC_BAD_LEN;
556  }
557  return ACT_VALIDATION_OK;
558 }
559 
560 void
561 VPortAction::Execute (ofp_vport_action_type type, ofpbuf *buffer, const sw_flow_key *key, const ofp_action_header *ah)
562 {
563  switch (type)
564  {
565  case OFPPAT_POP_MPLS:
566  {
567  ofp_vport_action_pop_mpls *opapm = (ofp_vport_action_pop_mpls *)ah;
568  pop_mpls_act (0, buffer, key, &opapm->apm);
569  break;
570  }
571  case OFPPAT_PUSH_MPLS:
572  {
573  ofp_vport_action_push_mpls *opapm = (ofp_vport_action_push_mpls *)ah;
574  push_mpls_act (0, buffer, key, &opapm->apm);
575  break;
576  }
577  case OFPPAT_SET_MPLS_LABEL:
578  {
579  ofp_vport_action_set_mpls_label *oparml = (ofp_vport_action_set_mpls_label *)ah;
580  set_mpls_label_act (buffer, key, oparml->label_out);
581  break;
582  }
583  case OFPPAT_SET_MPLS_EXP:
584  {
585  ofp_vport_action_set_mpls_exp *oparme = (ofp_vport_action_set_mpls_exp *)ah;
586  set_mpls_exp_act (buffer, key, oparme->exp);
587  break;
588  }
589  default:
590  break;
591  }
592 }
593 
594 bool
595 EricssonAction::IsValidType (er_action_type type)
596 {
597  switch (type)
598  {
599  case ERXT_POP_MPLS:
600  case ERXT_PUSH_MPLS:
601  return true;
602  default:
603  return false;
604  }
605 }
606 
607 uint16_t
608 EricssonAction::Validate (er_action_type type, size_t len)
609 {
610  size_t size = 0;
611 
612  switch (type)
613  {
614  case ERXT_POP_MPLS:
615  size = sizeof(er_action_pop_mpls);
616  break;
617  case ERXT_PUSH_MPLS:
618  size = sizeof(er_action_push_mpls);
619  break;
620  default:
621  break;
622  }
623 
624  if (len != size)
625  {
626  return OFPBAC_BAD_LEN;
627  }
628  return ACT_VALIDATION_OK;
629 }
630 
631 void
632 EricssonAction::Execute (er_action_type type, ofpbuf *buffer, const sw_flow_key *key, const er_action_header *ah)
633 {
634  switch (type)
635  {
636  case ERXT_POP_MPLS:
637  {
638  er_action_pop_mpls *erapm = (er_action_pop_mpls *)ah;
639  pop_mpls_act (0, buffer, key, &erapm->apm);
640  break;
641  }
642  case ERXT_PUSH_MPLS:
643  {
644  er_action_push_mpls *erapm = (er_action_push_mpls *)ah;
645  push_mpls_act (0, buffer, key, &erapm->apm);
646  break;
647  }
648  default:
649  break;
650  }
651 }
652 
653 void
654 Controller::AddSwitch (Ptr<OpenFlowSwitchNetDevice> swtch)
655 {
656  if (m_switches.find (swtch) != m_switches.end ())
657  {
658  NS_LOG_INFO ("This Controller has already registered this switch!");
659  }
660  else
661  {
662  m_switches.insert (swtch);
663  }
664 }
665 
666 void
667 Controller::SendToSwitch (Ptr<OpenFlowSwitchNetDevice> swtch, void * msg, size_t length)
668 {
669  if (m_switches.find (swtch) == m_switches.end ())
670  {
671  NS_LOG_ERROR ("Can't send to this switch, not registered to the Controller.");
672  return;
673  }
674 
675  swtch->ForwardControlInput (msg, length);
676 }
677 
678 ofp_flow_mod*
679 Controller::BuildFlow (sw_flow_key key, uint32_t buffer_id, uint16_t command, void* acts, size_t actions_len, int idle_timeout, int hard_timeout)
680 {
681  ofp_flow_mod* ofm = (ofp_flow_mod*)malloc (sizeof(ofp_flow_mod) + actions_len);
682  ofm->header.version = OFP_VERSION;
683  ofm->header.type = OFPT_FLOW_MOD;
684  ofm->header.length = htons (sizeof(ofp_flow_mod) + actions_len);
685  ofm->command = htons (command);
686  ofm->idle_timeout = htons (idle_timeout);
687  ofm->hard_timeout = htons (hard_timeout);
688  ofm->buffer_id = htonl (buffer_id);
689  ofm->priority = OFP_DEFAULT_PRIORITY;
690  memcpy (ofm->actions,acts,actions_len);
691 
692  ofm->match.wildcards = key.wildcards; // Wildcard fields
693  ofm->match.in_port = key.flow.in_port; // Input switch port
694  memcpy (ofm->match.dl_src, key.flow.dl_src, sizeof ofm->match.dl_src); // Ethernet source address.
695  memcpy (ofm->match.dl_dst, key.flow.dl_dst, sizeof ofm->match.dl_dst); // Ethernet destination address.
696  ofm->match.dl_vlan = key.flow.dl_vlan; // Input VLAN OFP_VLAN_NONE;
697  ofm->match.dl_type = key.flow.dl_type; // Ethernet frame type ETH_TYPE_IP;
698  ofm->match.nw_proto = key.flow.nw_proto; // IP Protocol
699  ofm->match.nw_src = key.flow.nw_src; // IP source address
700  ofm->match.nw_dst = key.flow.nw_dst; // IP destination address
701  ofm->match.tp_src = key.flow.tp_src; // TCP/UDP source port
702  ofm->match.tp_dst = key.flow.tp_dst; // TCP/UDP destination port
703  ofm->match.mpls_label1 = key.flow.mpls_label1; // Top of label stack htonl(MPLS_INVALID_LABEL);
704  ofm->match.mpls_label2 = key.flow.mpls_label1; // Second label (if available) htonl(MPLS_INVALID_LABEL);
705 
706  return ofm;
707 }
708 
709 uint8_t
710 Controller::GetPacketType (ofpbuf* buffer)
711 {
712  ofp_header* hdr = (ofp_header*)ofpbuf_try_pull (buffer, sizeof (ofp_header));
713  uint8_t type = hdr->type;
714  ofpbuf_push_uninit (buffer, sizeof (ofp_header));
715  return type;
716 }
717 
718 void
719 Controller::StartDump (StatsDumpCallback* cb)
720 {
721  if (cb != 0)
722  {
723  int error = 1;
724  while (error > 0) // Switch's StatsDump returns 1 if the reply isn't complete.
725  {
726  error = cb->swtch->StatsDump (cb);
727  }
728 
729  if (error != 0) // When the reply is complete, error will equal zero if there's no errors.
730  {
731  NS_LOG_WARN ("Dump Callback Error: " << strerror (-error));
732  }
733 
734  // Clean up
735  cb->swtch->StatsDone (cb);
736  }
737 }
738 
739 void
740 DropController::ReceiveFromSwitch (Ptr<OpenFlowSwitchNetDevice> swtch, ofpbuf* buffer)
741 {
742  if (m_switches.find (swtch) == m_switches.end ())
743  {
744  NS_LOG_ERROR ("Can't receive from this switch, not registered to the Controller.");
745  return;
746  }
747 
748  // We have received any packet at this point, so we pull the header to figure out what type of packet we're handling.
749  uint8_t type = GetPacketType (buffer);
750 
751  if (type == OFPT_PACKET_IN) // The switch didn't understand the packet it received, so it forwarded it to the controller.
752  {
753  ofp_packet_in * opi = (ofp_packet_in*)ofpbuf_try_pull (buffer, offsetof (ofp_packet_in, data));
754  int port = ntohs (opi->in_port);
755 
756  // Create matching key.
757  sw_flow_key key;
758  key.wildcards = 0;
759  flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow);
760 
761  ofp_flow_mod* ofm = BuildFlow (key, opi->buffer_id, OFPFC_ADD, 0, 0, OFP_FLOW_PERMANENT, OFP_FLOW_PERMANENT);
762  SendToSwitch (swtch, ofm, ofm->header.length);
763  }
764 }
765 
766 TypeId LearningController::GetTypeId (void)
767 {
768  static TypeId tid = TypeId ("ns3::ofi::LearningController")
769  .SetParent (Controller::GetTypeId ())
770  .AddConstructor<LearningController> ()
771  .AddAttribute ("ExpirationTime",
772  "Time it takes for learned MAC state entry/created flow to expire.",
773  TimeValue (Seconds (0)),
774  MakeTimeAccessor (&LearningController::m_expirationTime),
775  MakeTimeChecker ())
776  ;
777  return tid;
778 }
779 
780 void
781 LearningController::ReceiveFromSwitch (Ptr<OpenFlowSwitchNetDevice> swtch, ofpbuf* buffer)
782 {
783  if (m_switches.find (swtch) == m_switches.end ())
784  {
785  NS_LOG_ERROR ("Can't receive from this switch, not registered to the Controller.");
786  return;
787  }
788 
789  // We have received any packet at this point, so we pull the header to figure out what type of packet we're handling.
790  uint8_t type = GetPacketType (buffer);
791 
792  if (type == OFPT_PACKET_IN) // The switch didn't understand the packet it received, so it forwarded it to the controller.
793  {
794  ofp_packet_in * opi = (ofp_packet_in*)ofpbuf_try_pull (buffer, offsetof (ofp_packet_in, data));
795  int port = ntohs (opi->in_port);
796 
797  // Create matching key.
798  sw_flow_key key;
799  key.wildcards = 0;
800  flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow);
801 
802  uint16_t out_port = OFPP_FLOOD;
803  uint16_t in_port = ntohs (key.flow.in_port);
804 
805  // If the destination address is learned to a specific port, find it.
806  Mac48Address dst_addr;
807  dst_addr.CopyFrom (key.flow.dl_dst);
808  if (!dst_addr.IsBroadcast ())
809  {
810  LearnState_t::iterator st = m_learnState.find (dst_addr);
811  if (st != m_learnState.end ())
812  {
813  out_port = st->second.port;
814  }
815  else
816  {
817  NS_LOG_INFO ("Setting to flood; don't know yet what port " << dst_addr << " is connected to");
818  }
819  }
820  else
821  {
822  NS_LOG_INFO ("Setting to flood; this packet is a broadcast");
823  }
824 
825  // Create output-to-port action
826  ofp_action_output x[1];
827  x[0].type = htons (OFPAT_OUTPUT);
828  x[0].len = htons (sizeof(ofp_action_output));
829  x[0].port = out_port;
830 
831  // Create a new flow that outputs matched packets to a learned port, OFPP_FLOOD if there's no learned port.
832  ofp_flow_mod* ofm = BuildFlow (key, opi->buffer_id, OFPFC_ADD, x, sizeof(x), OFP_FLOW_PERMANENT, m_expirationTime.IsZero () ? OFP_FLOW_PERMANENT : m_expirationTime.GetSeconds ());
833  SendToSwitch (swtch, ofm, ofm->header.length);
834 
835  // We can learn a specific port for the source address for future use.
836  Mac48Address src_addr;
837  src_addr.CopyFrom (key.flow.dl_src);
838  LearnState_t::iterator st = m_learnState.find (src_addr);
839  if (st == m_learnState.end ()) // We haven't learned our source MAC yet.
840  {
841  LearnedState ls;
842  ls.port = in_port;
843  m_learnState.insert (std::make_pair (src_addr,ls));
844  NS_LOG_INFO ("Learned that " << src_addr << " can be found over port " << in_port);
845 
846  // Learn src_addr goes to a certain port.
847  ofp_action_output x2[1];
848  x2[0].type = htons (OFPAT_OUTPUT);
849  x2[0].len = htons (sizeof(ofp_action_output));
850  x2[0].port = in_port;
851 
852  // Switch MAC Addresses and ports to the flow we're modifying
853  src_addr.CopyTo (key.flow.dl_dst);
854  dst_addr.CopyTo (key.flow.dl_src);
855  key.flow.in_port = out_port;
856  ofp_flow_mod* ofm2 = BuildFlow (key, -1, OFPFC_MODIFY, x2, sizeof(x2), OFP_FLOW_PERMANENT, m_expirationTime.IsZero () ? OFP_FLOW_PERMANENT : m_expirationTime.GetSeconds ());
857  SendToSwitch (swtch, ofm2, ofm2->header.length);
858  }
859  }
860 }
861 
862 void
863 ExecuteActions (Ptr<OpenFlowSwitchNetDevice> swtch, uint64_t packet_uid, ofpbuf* buffer, sw_flow_key *key, const ofp_action_header *actions, size_t actions_len, int ignore_no_fwd)
864 {
866  /* Every output action needs a separate clone of 'buffer', but the common
867  * case is just a single output action, so that doing a clone and then
868  * freeing the original buffer is wasteful. So the following code is
869  * slightly obscure just to avoid that. */
870  int prev_port;
871  size_t max_len = 0; // Initialze to make compiler happy
872  uint16_t in_port = key->flow.in_port; // ntohs(key->flow.in_port);
873  uint8_t *p = (uint8_t *)actions;
874 
875  prev_port = -1;
876 
877  if (actions_len == 0)
878  {
879  NS_LOG_INFO ("No actions set to this flow. Dropping packet.");
880  return;
881  }
882 
883  /* The action list was already validated, so we can be a bit looser
884  * in our sanity-checking. */
885  while (actions_len > 0)
886  {
887  ofp_action_header *ah = (ofp_action_header *)p;
888  size_t len = htons (ah->len);
889 
890  if (prev_port != -1)
891  {
892  swtch->DoOutput (packet_uid, in_port, max_len, prev_port, ignore_no_fwd);
893  prev_port = -1;
894  }
895 
896  if (ah->type == htons (OFPAT_OUTPUT))
897  {
898  ofp_action_output *oa = (ofp_action_output *)p;
899 
900  // port is now 32-bits
901  prev_port = oa->port; // ntohl(oa->port);
902  // prev_port = ntohs(oa->port);
903  max_len = ntohs (oa->max_len);
904  }
905  else
906  {
907  uint16_t type = ntohs (ah->type);
908  if (Action::IsValidType ((ofp_action_type)type)) // Execute a built-in OpenFlow action against 'buffer'.
909  {
910  Action::Execute ((ofp_action_type)type, buffer, key, ah);
911  }
912  else if (type == OFPAT_VENDOR)
913  {
914  ExecuteVendor (buffer, key, ah);
915  }
916  }
917 
918  p += len;
919  actions_len -= len;
920  }
921 
922  if (prev_port != -1)
923  {
924  swtch->DoOutput (packet_uid, in_port, max_len, prev_port, ignore_no_fwd);
925  }
926 }
927 
928 uint16_t
929 ValidateActions (const sw_flow_key *key, const ofp_action_header *actions, size_t actions_len)
930 {
931  uint8_t *p = (uint8_t *)actions;
932  int err;
933 
934  while (actions_len >= sizeof(ofp_action_header))
935  {
936  ofp_action_header *ah = (ofp_action_header *)p;
937  size_t len = ntohs (ah->len);
938  uint16_t type;
939 
940  /* Make there's enough remaining data for the specified length
941  * and that the action length is a multiple of 64 bits. */
942  if ((actions_len < len) || (len % 8) != 0)
943  {
944  return OFPBAC_BAD_LEN;
945  }
946 
947  type = ntohs (ah->type);
948  if (Action::IsValidType ((ofp_action_type)type)) // Validate built-in OpenFlow actions.
949  {
950  err = Action::Validate ((ofp_action_type)type, len, key, ah);
951  if (err != ACT_VALIDATION_OK)
952  {
953  return err;
954  }
955  }
956  else if (type == OFPAT_VENDOR)
957  {
958  err = ValidateVendor (key, ah, len);
959  if (err != ACT_VALIDATION_OK)
960  {
961  return err;
962  }
963  }
964  else
965  {
966  return OFPBAC_BAD_TYPE;
967  }
968 
969  p += len;
970  actions_len -= len;
971  }
972 
973  // Check if there's any trailing garbage.
974  if (actions_len != 0)
975  {
976  return OFPBAC_BAD_LEN;
977  }
978 
979  return ACT_VALIDATION_OK;
980 }
981 
982 void
983 ExecuteVPortActions (Ptr<OpenFlowSwitchNetDevice> swtch, uint64_t packet_uid, ofpbuf* buffer, sw_flow_key *key, const ofp_action_header *actions, size_t actions_len)
984 {
985  /* Every output action needs a separate clone of 'buffer', but the common
986  * case is just a single output action, so that doing a clone and then
987  * freeing the original buffer is wasteful. So the following code is
988  * slightly obscure just to avoid that. */
989  int prev_port;
990  size_t max_len = 0; // Initialize to make compiler happy
991  uint16_t in_port = ntohs (key->flow.in_port);
992  uint8_t *p = (uint8_t *)actions;
993  uint16_t type;
994  ofp_action_output *oa;
995 
996  prev_port = -1;
997  /* The action list was already validated, so we can be a bit looser
998  * in our sanity-checking. */
999  while (actions_len > 0)
1000  {
1001  ofp_action_header *ah = (ofp_action_header *)p;
1002  size_t len = htons (ah->len);
1003  if (prev_port != -1)
1004  {
1005  swtch->DoOutput (packet_uid, in_port, max_len, prev_port, false);
1006  prev_port = -1;
1007  }
1008 
1009  if (ah->type == htons (OFPAT_OUTPUT))
1010  {
1011  oa = (ofp_action_output *)p;
1012  prev_port = ntohl (oa->port);
1013  max_len = ntohs (oa->max_len);
1014  }
1015  else
1016  {
1017  type = ah->type; // ntohs(ah->type);
1018  VPortAction::Execute ((ofp_vport_action_type)type, buffer, key, ah);
1019  }
1020 
1021  p += len;
1022  actions_len -= len;
1023  }
1024 
1025  if (prev_port != -1)
1026  {
1027  swtch->DoOutput (packet_uid, in_port, max_len, prev_port, false);
1028  }
1029 }
1030 
1031 uint16_t
1032 ValidateVPortActions (const ofp_action_header *actions, size_t actions_len)
1033 {
1034  uint8_t *p = (uint8_t *)actions;
1035  int err;
1036 
1037  while (actions_len >= sizeof(ofp_action_header))
1038  {
1039  ofp_action_header *ah = (ofp_action_header *)p;
1040  size_t len = ntohs (ah->len);
1041  uint16_t type;
1042 
1043  /* Make there's enough remaining data for the specified length
1044  * and that the action length is a multiple of 64 bits. */
1045  if ((actions_len < len) || (len % 8) != 0)
1046  {
1047  return OFPBAC_BAD_LEN;
1048  }
1049 
1050  type = ntohs (ah->type);
1051  if (VPortAction::IsValidType ((ofp_vport_action_type)type)) // Validate "built-in" OpenFlow port table actions.
1052  {
1053  err = VPortAction::Validate ((ofp_vport_action_type)type, len, ah);
1054  if (err != ACT_VALIDATION_OK)
1055  {
1056  return err;
1057  }
1058  }
1059  else
1060  {
1061  return OFPBAC_BAD_TYPE;
1062  }
1063 
1064  p += len;
1065  actions_len -= len;
1066  }
1067 
1068  // Check if there's any trailing garbage.
1069  if (actions_len != 0)
1070  {
1071  return OFPBAC_BAD_LEN;
1072  }
1073 
1074  return ACT_VALIDATION_OK;
1075 }
1076 
1077 void
1078 ExecuteVendor (ofpbuf *buffer, const sw_flow_key *key, const ofp_action_header *ah)
1079 {
1080  ofp_action_vendor_header *avh = (ofp_action_vendor_header *)ah;
1081 
1082  switch (ntohl (avh->vendor))
1083  {
1084  case NX_VENDOR_ID:
1085  // Nothing to execute yet.
1086  break;
1087  case ER_VENDOR_ID:
1088  {
1089  const er_action_header *erah = (const er_action_header *)avh;
1090  EricssonAction::Execute ((er_action_type)ntohs (erah->subtype), buffer, key, erah);
1091  break;
1092  }
1093  default:
1094  // This should not be possible due to prior validation.
1095  NS_LOG_INFO ("attempt to execute action with unknown vendor: " << ntohl (avh->vendor));
1096  break;
1097  }
1098 }
1099 
1100 uint16_t
1101 ValidateVendor (const sw_flow_key *key, const ofp_action_header *ah, uint16_t len)
1102 {
1103  ofp_action_vendor_header *avh;
1104  int ret = ACT_VALIDATION_OK;
1105 
1106  if (len < sizeof(ofp_action_vendor_header))
1107  {
1108  return OFPBAC_BAD_LEN;
1109  }
1110 
1111  avh = (ofp_action_vendor_header *)ah;
1112 
1113  switch (ntohl (avh->vendor))
1114  {
1115  case NX_VENDOR_ID: // Validate Nicara OpenFlow actions.
1116  ret = OFPBAC_BAD_VENDOR_TYPE; // Nothing to validate yet.
1117  break;
1118  case ER_VENDOR_ID: // Validate Ericsson OpenFlow actions.
1119  {
1120  const er_action_header *erah = (const er_action_header *)avh;
1121  ret = EricssonAction::Validate ((er_action_type)ntohs (erah->subtype), len);
1122  break;
1123  }
1124  default:
1125  return OFPBAC_BAD_VENDOR;
1126  }
1127 
1128  return ret;
1129 }
1130 
1131 }
1132 
1133 }
1134 
1135 #endif // NS3_OPENFLOW
void StartDump(StatsDumpCallback *cb)
Starts a callback-based, reliable, possibly multi-message reply to a request made by the controller...
static bool IsValidType(ofp_action_type type)
bool IsZero(void) const
Definition: nstime.h:220
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
int DoInit(const void *body, int body_len, void **state)
Prepares to dump some kind of statistics on the connected OpenFlowSwitchNetDevice.
#define NS_LOG_INFO(msg)
Definition: log.h:264
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
void ReceiveFromSwitch(Ptr< OpenFlowSwitchNetDevice > swtch, ofpbuf *buffer)
void ReceiveFromSwitch(Ptr< OpenFlowSwitchNetDevice > swtch, ofpbuf *buffer)
double GetSeconds(void) const
Definition: nstime.h:262
static uint16_t Validate(ofp_vport_action_type type, size_t len, const ofp_action_header *ah)
Validates the action on whether its data is valid or not.
static void Execute(er_action_type type, ofpbuf *buffer, const sw_flow_key *key, const er_action_header *ah)
Executes the action.
LearnState_t m_learnState
Learned state data.
static bool IsValidType(er_action_type type)
static void Execute(ofp_vport_action_type type, ofpbuf *buffer, const sw_flow_key *key, const ofp_action_header *ah)
Executes the action.
Switches_t m_switches
The collection of switches registered to this controller.
static uint16_t Validate(er_action_type type, size_t len)
Validates the action on whether its data is valid or not.
int DoDump(Ptr< OpenFlowSwitchNetDevice > swtch, void *state, ofpbuf *buffer)
Appends statistics for OpenFlowSwitchNetDevice to 'buffer'.
virtual void SendToSwitch(Ptr< OpenFlowSwitchNetDevice > swtch, void *msg, size_t length)
virtual void AddSwitch(Ptr< OpenFlowSwitchNetDevice > swtch)
ofp_flow_mod * BuildFlow(sw_flow_key key, uint32_t buffer_id, uint16_t command, void *acts, size_t actions_len, int idle_timeout, int hard_timeout)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
static uint16_t Validate(ofp_action_type type, size_t len, const sw_flow_key *key, const ofp_action_header *ah)
Validates the action on whether its data is valid or not.
#define NS_LOG_WARN(msg)
Definition: log.h:246
Time m_expirationTime
Time it takes for learned MAC state entry/created flow to expire.
uint8_t GetPacketType(ofpbuf *buffer)
#define NS_LOG_ERROR(msg)
Definition: log.h:237
static bool IsValidType(ofp_vport_action_type type)
static void Execute(ofp_action_type type, ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *ah)
Executes the action.
void DoCleanup(void *state)
Cleans any state created by the init or dump functions.