32 return "The ns-3 team";
44 return "Simulated OpenFlow Switch";
56 uint8_t ea[ETH_ADDR_LEN];
58 return eth_addr_to_uint64 (ea);
64 static TypeId tid = TypeId (
"ns3::OpenFlowSwitchNetDevice")
66 .AddConstructor<OpenFlowSwitchNetDevice> ()
68 "The identification of the OpenFlowSwitchNetDevice/Datapath, needed for OpenFlow compatibility.",
69 UintegerValue (GenerateId ()),
71 MakeUintegerChecker<uint64_t> ())
73 "A real switch will have an overhead for looking up in the flow table. For the default, we simulate a standard TCAM on an FPGA.",
74 TimeValue (NanoSeconds (30)),
78 "Flags to turn different functionality on/off, such as whether to inform the controller when a flow expires, or how to handle fragments.",
81 MakeUintegerChecker<uint16_t> ())
83 "When forwarding a packet the switch didn't match up to the controller, it can be more efficient to forward only the first x bytes.",
84 UintegerValue (OFP_DEFAULT_MISS_SEND_LEN),
86 MakeUintegerChecker<uint16_t> ())
98 m_channel = CreateObject<BridgeChannel> ();
106 m_chain = chain_create ();
109 NS_LOG_ERROR (
"Not enough memory to create the flow table.");
112 m_ports.reserve (DP_MAX_PORTS);
113 vport_table_init (&m_vportTable);
116 OpenFlowSwitchNetDevice::~OpenFlowSwitchNetDevice ()
122 OpenFlowSwitchNetDevice::DoDispose ()
126 for (Ports_t::iterator b = m_ports.begin (), e = m_ports.end (); b != e; b++)
128 SendPortStatus (*b, OFPPR_DELETE);
135 chain_destroy (m_chain);
136 RBTreeDestroy (m_vportTable.table);
139 NetDevice::DoDispose ();
143 OpenFlowSwitchNetDevice::SetController (Ptr<ofi::Controller> c)
145 if (m_controller != 0)
152 m_controller->AddSwitch (
this);
156 OpenFlowSwitchNetDevice::AddSwitchPort (Ptr<NetDevice> switchPort)
160 if (!Mac48Address::IsMatchingType (switchPort->GetAddress ()))
162 NS_FATAL_ERROR (
"Device does not support eui 48 addresses: cannot be added to switch.");
164 if (!switchPort->SupportsSendFrom ())
166 NS_FATAL_ERROR (
"Device does not support SendFrom: cannot be added to switch.");
168 if (m_address == Mac48Address ())
170 m_address = Mac48Address::ConvertFrom (switchPort->GetAddress ());
173 if (m_ports.size () < DP_MAX_PORTS)
177 p.netdev = switchPort;
178 m_ports.push_back (p);
181 SendPortStatus (p, OFPPR_ADD);
183 NS_LOG_DEBUG (
"RegisterProtocolHandler for " << switchPort->GetInstanceTypeId ().GetName ());
184 m_node->RegisterProtocolHandler (
MakeCallback (&OpenFlowSwitchNetDevice::ReceiveFromDevice,
this),
185 0, switchPort,
true);
186 m_channel->AddChannel (switchPort->GetChannel ());
197 OpenFlowSwitchNetDevice::SetIfIndex (
const uint32_t index)
204 OpenFlowSwitchNetDevice::GetIfIndex (
void)
const
211 OpenFlowSwitchNetDevice::GetChannel (
void)
const
218 OpenFlowSwitchNetDevice::SetAddress (Address
address)
221 m_address = Mac48Address::ConvertFrom (address);
225 OpenFlowSwitchNetDevice::GetAddress (
void)
const
232 OpenFlowSwitchNetDevice::SetMtu (
const uint16_t mtu)
240 OpenFlowSwitchNetDevice::GetMtu (
void)
const
248 OpenFlowSwitchNetDevice::IsLinkUp (
void)
const
256 OpenFlowSwitchNetDevice::AddLinkChangeCallback (Callback<void> callback)
261 OpenFlowSwitchNetDevice::IsBroadcast (
void)
const
268 OpenFlowSwitchNetDevice::GetBroadcast (
void)
const
271 return Mac48Address (
"ff:ff:ff:ff:ff:ff");
282 OpenFlowSwitchNetDevice::GetMulticast (Ipv4Address multicastGroup)
const
285 Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
291 OpenFlowSwitchNetDevice::IsPointToPoint (
void)
const
298 OpenFlowSwitchNetDevice::IsBridge (
void)
const
305 OpenFlowSwitchNetDevice::DoOutput (uint32_t packet_uid,
int in_port,
size_t max_len,
int out_port,
bool ignore_no_fwd)
307 if (out_port != OFPP_CONTROLLER)
309 OutputPort (packet_uid, in_port, out_port, ignore_no_fwd);
313 OutputControl (packet_uid, in_port, max_len, OFPR_ACTION);
318 OpenFlowSwitchNetDevice::Send (Ptr<Packet> packet,
const Address& dest, uint16_t protocolNumber)
321 return SendFrom (packet, m_address, dest, protocolNumber);
325 OpenFlowSwitchNetDevice::SendFrom (Ptr<Packet> packet,
const Address& src,
const Address& dest, uint16_t protocolNumber)
329 ofpbuf *buffer = BufferFromPacket (packet,src,dest,GetMtu (),protocolNumber);
332 ofi::SwitchPacketMetadata
data;
333 data.packet = packet;
334 data.buffer = buffer;
335 data.protocolNumber = protocolNumber;
336 data.src = Address (src);
337 data.dst = Address (dest);
338 m_packetData.insert (std::make_pair (packet_uid, data));
340 RunThroughFlowTable (packet_uid, -1);
347 OpenFlowSwitchNetDevice::GetNode (
void)
const
354 OpenFlowSwitchNetDevice::SetNode (Ptr<Node> node)
361 OpenFlowSwitchNetDevice::NeedsArp (
void)
const
368 OpenFlowSwitchNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
375 OpenFlowSwitchNetDevice::SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb)
378 m_promiscRxCallback = cb;
382 OpenFlowSwitchNetDevice::SupportsSendFrom ()
const
389 OpenFlowSwitchNetDevice::GetMulticast (Ipv6Address addr)
const
392 return Mac48Address::GetMulticast (addr);
397 OpenFlowSwitchNetDevice::AddVPort (
const ofp_vport_mod *ovpm)
399 size_t actions_len = ntohs (ovpm->header.length) -
sizeof *ovpm;
400 unsigned int vport = ntohl (ovpm->vport);
401 unsigned int parent_port = ntohl (ovpm->parent_port);
404 vport_table_entry *vpe = vport_table_lookup (&m_vportTable, vport);
408 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
414 if (v_code != ACT_VALIDATION_OK)
416 SendErrorMsg (OFPET_BAD_ACTION, v_code, ovpm, ntohs (ovpm->header.length));
420 vpe = vport_table_entry_alloc (actions_len);
423 vpe->parent_port = parent_port;
424 if (vport < OFPP_VP_START || vport > OFPP_VP_END)
426 NS_LOG_ERROR (
"port " << vport <<
" is not in the virtual port range (" << OFPP_VP_START <<
"-" << OFPP_VP_END <<
")");
427 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
428 free_vport_table_entry (vpe);
432 vpe->port_acts->actions_len = actions_len;
433 memcpy (vpe->port_acts->actions, ovpm->actions, actions_len);
435 int error = insert_vport_table_entry (&m_vportTable, vpe);
438 NS_LOG_ERROR (
"could not insert port table entry for port " << vport);
445 OpenFlowSwitchNetDevice::BufferFromPacket (Ptr<Packet> packet, Address src, Address dst,
int mtu, uint16_t protocol)
447 NS_LOG_INFO (
"Creating Openflow buffer from packet.");
454 const int headroom = 128 + 2;
455 const int hard_header = VLAN_ETH_HEADER_LEN;
456 ofpbuf *buffer = ofpbuf_new (headroom + hard_header + mtu);
457 buffer->data = (
char*)buffer->data + headroom + hard_header;
459 int l2_length = 0, l3_length = 0, l4_length = 0;
462 EthernetHeader eth_hd;
463 if (packet->PeekHeader (eth_hd))
465 buffer->l2 =
new eth_header;
466 eth_header* eth_h = (eth_header*)buffer->l2;
467 dst.CopyTo (eth_h->eth_dst);
468 src.CopyTo (eth_h->eth_src);
469 eth_h->eth_type = htons (ETH_TYPE_IP);
472 l2_length = ETH_HEADER_LEN;
476 if (protocol == Ipv4L3Protocol::PROT_NUMBER)
479 if (packet->PeekHeader (ip_hd))
481 buffer->l3 =
new ip_header;
482 ip_header* ip_h = (ip_header*)buffer->l3;
483 ip_h->ip_ihl_ver = IP_IHL_VER (5, IP_VERSION);
484 ip_h->ip_tos = ip_hd.GetTos ();
485 ip_h->ip_tot_len = packet->GetSize ();
486 ip_h->ip_id = ip_hd.GetIdentification ();
487 ip_h->ip_frag_off = ip_hd.GetFragmentOffset ();
488 ip_h->ip_ttl = ip_hd.GetTtl ();
489 ip_h->ip_proto = ip_hd.GetProtocol ();
490 ip_h->ip_src = htonl (ip_hd.GetSource ().Get ());
491 ip_h->ip_dst = htonl (ip_hd.GetDestination ().Get ());
492 ip_h->ip_csum = csum (&ip_h,
sizeof ip_h);
495 l3_length = IP_HEADER_LEN;
502 if (packet->PeekHeader (arp_hd))
504 buffer->l3 =
new arp_eth_header;
505 arp_eth_header* arp_h = (arp_eth_header*)buffer->l3;
506 arp_h->ar_hrd = ARP_HRD_ETHERNET;
507 arp_h->ar_pro = ARP_PRO_IP;
508 arp_h->ar_op = arp_hd.m_type;
509 arp_hd.GetDestinationHardwareAddress ().CopyTo (arp_h->ar_tha);
510 arp_hd.GetSourceHardwareAddress ().CopyTo (arp_h->ar_sha);
511 arp_h->ar_tpa = arp_hd.GetDestinationIpv4Address ().Get ();
512 arp_h->ar_spa = arp_hd.GetSourceIpv4Address ().Get ();
513 arp_h->ar_hln =
sizeof arp_h->ar_tha;
514 arp_h->ar_pln =
sizeof arp_h->ar_tpa;
517 l3_length = ARP_ETH_HEADER_LEN;
522 if (packet->PeekHeader (tcp_hd))
524 buffer->l4 =
new tcp_header;
525 tcp_header* tcp_h = (tcp_header*)buffer->l4;
526 tcp_h->tcp_src = htons (tcp_hd.GetSourcePort ());
527 tcp_h->tcp_dst = htons (tcp_hd.GetDestinationPort ());
528 tcp_h->tcp_seq = tcp_hd.GetSequenceNumber ().GetValue ();
529 tcp_h->tcp_ack = tcp_hd.GetAckNumber ().GetValue ();
530 tcp_h->tcp_ctl = TCP_FLAGS (tcp_hd.GetFlags ());
531 tcp_h->tcp_winsz = tcp_hd.GetWindowSize ();
532 tcp_h->tcp_urg = tcp_hd.GetUrgentPointer ();
533 tcp_h->tcp_csum = csum (&tcp_h,
sizeof tcp_h);
536 l4_length = TCP_HEADER_LEN;
541 if (packet->PeekHeader (udp_hd))
543 buffer->l4 =
new udp_header;
544 udp_header* udp_h = (udp_header*)buffer->l4;
545 udp_h->udp_src = htons (udp_hd.GetSourcePort ());
546 udp_h->udp_dst = htons (udp_hd.GetDestinationPort ());
547 udp_h->udp_len = htons (UDP_HEADER_LEN + packet->GetSize ());
549 if (protocol == Ipv4L3Protocol::PROT_NUMBER)
551 ip_header* ip_h = (ip_header*)buffer->l3;
552 uint32_t udp_csum = csum_add32 (0, ip_h->ip_src);
553 udp_csum = csum_add32 (udp_csum, ip_h->ip_dst);
554 udp_csum = csum_add16 (udp_csum, IP_TYPE_UDP << 8);
555 udp_csum = csum_add16 (udp_csum, udp_h->udp_len);
556 udp_csum = csum_continue (udp_csum, udp_h,
sizeof udp_h);
557 udp_h->udp_csum = csum_finish (csum_continue (udp_csum, buffer->data, buffer->size));
561 udp_h->udp_csum = htons (0);
565 l4_length = UDP_HEADER_LEN;
570 packet->CopyData ((uint8_t*)buffer->data, packet->GetSize ());
574 ofpbuf_push (buffer, buffer->l4, l4_length);
575 delete (tcp_header*)buffer->l4;
579 ofpbuf_push (buffer, buffer->l3, l3_length);
580 delete (ip_header*)buffer->l3;
584 ofpbuf_push (buffer, buffer->l2, l2_length);
585 delete (eth_header*)buffer->l2;
592 OpenFlowSwitchNetDevice::ReceiveFromDevice (Ptr<NetDevice> netdev, Ptr<const Packet> packet, uint16_t protocol,
593 const Address& src,
const Address& dst, PacketType packetType)
596 NS_LOG_INFO (
"--------------------------------------------");
599 if (!m_promiscRxCallback.IsNull ())
601 m_promiscRxCallback (
this, packet, protocol, src, dst, packetType);
604 Mac48Address dst48 = Mac48Address::ConvertFrom (dst);
605 NS_LOG_INFO (
"Received packet from " << Mac48Address::ConvertFrom (src) <<
" looking for " << dst48);
607 for (
size_t i = 0; i < m_ports.size (); i++)
609 if (m_ports[i].netdev == netdev)
611 if (packetType == PACKET_HOST && dst48 == m_address)
613 m_rxCallback (
this, packet, protocol, src);
615 else if (packetType == PACKET_BROADCAST || packetType == PACKET_MULTICAST || packetType == PACKET_OTHERHOST)
617 if (packetType == PACKET_OTHERHOST && dst48 == m_address)
619 m_rxCallback (
this, packet, protocol, src);
623 if (packetType != PACKET_OTHERHOST)
625 m_rxCallback (
this, packet, protocol, src);
628 ofi::SwitchPacketMetadata
data;
629 data.packet = packet->Copy ();
631 ofpbuf *buffer = BufferFromPacket (data.packet,src,dst,netdev->GetMtu (),protocol);
632 m_ports[i].rx_packets++;
633 m_ports[i].rx_bytes += buffer->size;
634 data.buffer = buffer;
637 data.protocolNumber = protocol;
638 data.src = Address (src);
639 data.dst = Address (dst);
640 m_packetData.insert (std::make_pair (packet_uid, data));
642 RunThroughFlowTable (packet_uid, i);
652 if (now >=
Seconds (m_lastExecute.GetSeconds () + 1))
655 for (
size_t i = 0; i < m_ports.size (); i++)
657 if (UpdatePortStatus (m_ports[i]))
659 SendPortStatus (m_ports[i], OFPPR_MODIFY);
664 List deleted = LIST_INITIALIZER (&deleted);
666 chain_timeout (m_chain, &deleted);
667 LIST_FOR_EACH_SAFE (f, n, sw_flow, node, &deleted)
669 std::ostringstream str;
671 for (
int i = 0; i < 6; i++)
672 str << (i!=0 ?
":" :
"") << std::hex << f->key.flow.dl_src[i]/16 << f->key.flow.dl_src[i]%16;
674 for (
int i = 0; i < 6; i++)
675 str << (i!=0 ?
":" :
"") << std::hex << f->key.flow.dl_dst[i]/16 << f->key.flow.dl_dst[i]%16;
679 SendFlowExpired (f, (ofp_flow_expired_reason)f->reason);
680 list_remove (&f->node);
689 OpenFlowSwitchNetDevice::OutputAll (uint32_t packet_uid,
int in_port,
bool flood)
695 for (
size_t i = 0; i < m_ports.size (); i++)
697 if (i == (
unsigned)in_port)
701 if (flood && m_ports[i].config & OFPPC_NO_FLOOD)
707 OutputPort (packet_uid, in_port, prev_port,
false);
713 OutputPort (packet_uid, in_port, prev_port,
false);
720 OpenFlowSwitchNetDevice::OutputPacket (uint32_t packet_uid,
int out_port)
722 if (out_port >= 0 && out_port < DP_MAX_PORTS)
724 ofi::Port& p = m_ports[out_port];
725 if (p.netdev != 0 && !(p.config & OFPPC_PORT_DOWN))
727 ofi::SwitchPacketMetadata data = m_packetData.find (packet_uid)->second;
728 size_t bufsize = data.buffer->size;
729 NS_LOG_INFO (
"Sending packet " << data.packet->GetUid () <<
" over port " << out_port);
730 if (p.netdev->SendFrom (data.packet->Copy (), data.src, data.dst, data.protocolNumber))
733 p.tx_bytes += bufsize;
743 NS_LOG_DEBUG (
"can't forward to bad port " << out_port);
747 OpenFlowSwitchNetDevice::OutputPort (uint32_t packet_uid,
int in_port,
int out_port,
bool ignore_no_fwd)
751 if (out_port == OFPP_FLOOD)
753 OutputAll (packet_uid, in_port,
true);
755 else if (out_port == OFPP_ALL)
757 OutputAll (packet_uid, in_port,
false);
759 else if (out_port == OFPP_CONTROLLER)
761 OutputControl (packet_uid, in_port, 0, OFPR_ACTION);
763 else if (out_port == OFPP_IN_PORT)
765 OutputPacket (packet_uid, in_port);
767 else if (out_port == OFPP_TABLE)
769 RunThroughFlowTable (packet_uid, in_port < DP_MAX_PORTS ? in_port : -1,
false);
771 else if (out_port >= OFPP_VP_START && out_port <= OFPP_VP_END)
774 NS_LOG_INFO (
"packet sent to virtual port " << out_port);
775 if (in_port < DP_MAX_PORTS)
777 RunThroughVPortTable (packet_uid, in_port, out_port);
781 RunThroughVPortTable (packet_uid, -1, out_port);
784 else if (in_port == out_port)
790 OutputPacket (packet_uid, out_port);
795 OpenFlowSwitchNetDevice::MakeOpenflowReply (
size_t openflow_len, uint8_t type, ofpbuf **bufferp)
797 return make_openflow_xid (openflow_len, type, 0, bufferp);
801 OpenFlowSwitchNetDevice::SendOpenflowBuffer (ofpbuf *buffer)
803 if (m_controller != 0)
805 update_openflow_length (buffer);
806 m_controller->ReceiveFromSwitch (
this, buffer);
813 OpenFlowSwitchNetDevice::OutputControl (uint32_t packet_uid,
int in_port,
size_t max_len,
int reason)
817 ofpbuf* buffer = m_packetData.find (packet_uid)->second.buffer;
818 size_t total_len = buffer->size;
819 if (packet_uid != std::numeric_limits<uint32_t>::max () && max_len != 0 && buffer->size > max_len)
821 buffer->size = max_len;
824 ofp_packet_in *opi = (ofp_packet_in*)ofpbuf_push_uninit (buffer, offsetof (ofp_packet_in, data));
825 opi->header.version = OFP_VERSION;
826 opi->header.type = OFPT_PACKET_IN;
827 opi->header.length = htons (buffer->size);
828 opi->header.xid = htonl (0);
829 opi->buffer_id = htonl (packet_uid);
830 opi->total_len = htons (total_len);
831 opi->in_port = htons (in_port);
832 opi->reason = reason;
834 SendOpenflowBuffer (buffer);
838 OpenFlowSwitchNetDevice::FillPortDesc (ofi::Port p, ofp_phy_port *desc)
840 desc->port_no = htons (GetSwitchPortIndex (p));
842 std::ostringstream nm;
843 nm <<
"eth" << GetSwitchPortIndex (p);
844 strncpy ((
char *)desc->name, nm.str ().c_str (),
sizeof desc->name);
846 p.netdev->GetAddress ().CopyTo (desc->hw_addr);
847 desc->config = htonl (p.config);
848 desc->state = htonl (p.state);
853 desc->advertised = 0;
858 OpenFlowSwitchNetDevice::SendFeaturesReply ()
861 ofp_switch_features *ofr = (ofp_switch_features*)MakeOpenflowReply (
sizeof *ofr, OFPT_FEATURES_REPLY, &buffer);
862 ofr->datapath_id = htonll (m_id);
863 ofr->n_tables = m_chain->n_tables;
868 for (
size_t i = 0; i < m_ports.size (); i++)
870 ofp_phy_port* opp = (ofp_phy_port*)ofpbuf_put_zeros (buffer,
sizeof *opp);
871 FillPortDesc (m_ports[i], opp);
874 SendOpenflowBuffer (buffer);
878 OpenFlowSwitchNetDevice::SendVPortTableFeatures ()
881 ofp_vport_table_features *ovtfr = (ofp_vport_table_features*)MakeOpenflowReply (
sizeof *ovtfr, OFPT_VPORT_TABLE_FEATURES_REPLY, &buffer);
883 ovtfr->max_vports = htonl (m_vportTable.max_vports);
884 ovtfr->max_chain_depth = htons (-1);
885 ovtfr->mixed_chaining =
true;
886 SendOpenflowBuffer (buffer);
890 OpenFlowSwitchNetDevice::UpdatePortStatus (ofi::Port& p)
892 uint32_t orig_config = p.config;
893 uint32_t orig_state = p.state;
896 p.config &= ~OFPPC_PORT_DOWN;
898 if (p.netdev->IsLinkUp ())
900 p.state &= ~OFPPS_LINK_DOWN;
904 p.state |= OFPPS_LINK_DOWN;
907 return ((orig_config != p.config) || (orig_state != p.state));
911 OpenFlowSwitchNetDevice::SendPortStatus (ofi::Port p, uint8_t status)
914 ofp_port_status *ops = (ofp_port_status*)MakeOpenflowReply (
sizeof *ops, OFPT_PORT_STATUS, &buffer);
915 ops->reason = status;
916 memset (ops->pad, 0,
sizeof ops->pad);
917 FillPortDesc (p, &ops->desc);
919 SendOpenflowBuffer (buffer);
920 ofpbuf_delete (buffer);
924 OpenFlowSwitchNetDevice::SendFlowExpired (sw_flow *flow,
enum ofp_flow_expired_reason reason)
927 ofp_flow_expired *ofe = (ofp_flow_expired*)MakeOpenflowReply (
sizeof *ofe, OFPT_FLOW_EXPIRED, &buffer);
928 flow_fill_match (&ofe->match, &flow->key);
930 ofe->priority = htons (flow->priority);
931 ofe->reason = reason;
932 memset (ofe->pad, 0,
sizeof ofe->pad);
934 ofe->duration = htonl (time_now () - flow->created);
935 memset (ofe->pad2, 0,
sizeof ofe->pad2);
936 ofe->packet_count = htonll (flow->packet_count);
937 ofe->byte_count = htonll (flow->byte_count);
938 SendOpenflowBuffer (buffer);
942 OpenFlowSwitchNetDevice::SendErrorMsg (uint16_t type, uint16_t code,
const void *data,
size_t len)
945 ofp_error_msg *oem = (ofp_error_msg*)MakeOpenflowReply (
sizeof(*oem) + len, OFPT_ERROR, &buffer);
946 oem->type = htons (type);
947 oem->code = htons (code);
948 memcpy (oem->data, data, len);
949 SendOpenflowBuffer (buffer);
953 OpenFlowSwitchNetDevice::FlowTableLookup (sw_flow_key key, ofpbuf* buffer, uint32_t packet_uid,
int port,
bool send_to_controller)
955 sw_flow *flow = chain_lookup (m_chain, &key);
959 flow_used (flow, buffer);
960 ofi::ExecuteActions (
this, packet_uid, buffer, &key, flow->sf_acts->actions, flow->sf_acts->actions_len,
false);
966 if (send_to_controller)
968 OutputControl (packet_uid, port, m_missSendLen, OFPR_NO_MATCH);
973 m_packetData.erase (packet_uid);
975 ofpbuf_delete (buffer);
979 OpenFlowSwitchNetDevice::RunThroughFlowTable (uint32_t packet_uid,
int port,
bool send_to_controller)
981 ofi::SwitchPacketMetadata data = m_packetData.find (packet_uid)->second;
982 ofpbuf* buffer = data.buffer;
987 if (flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow) && (m_flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP)
989 ofpbuf_delete (buffer);
997 mpls_h.value = ntohl (*((uint32_t*)buffer->l2_5));
1003 m_ports[
port].mpls_ttl0_dropped++;
1012 uint32_t config = m_ports[
port].config;
1013 if (config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP)
1014 && config & (!eth_addr_equals (key.flow.dl_dst, stp_eth_addr) ? OFPPC_NO_RECV : OFPPC_NO_RECV_STP))
1021 Simulator::Schedule (m_lookupDelay, &OpenFlowSwitchNetDevice::FlowTableLookup,
this, key, buffer, packet_uid, port, send_to_controller);
1025 OpenFlowSwitchNetDevice::RunThroughVPortTable (uint32_t packet_uid,
int port, uint32_t vport)
1027 ofpbuf* buffer = m_packetData.find (packet_uid)->second.buffer;
1033 if (flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow)
1034 && (m_flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP)
1040 vport_table_entry *vpe = vport_table_lookup (&m_vportTable, vport);
1041 m_vportTable.lookup_count++;
1044 m_vportTable.port_match_count++;
1048 ofi::ExecuteVPortActions (
this, packet_uid, m_packetData.find (packet_uid)->second.buffer, &key, vpe->port_acts->actions, vpe->port_acts->actions_len);
1049 vport_used (vpe, buffer);
1050 if (vpe->parent_port_ptr == 0)
1054 if (vpe->parent_port <= OFPP_VP_START)
1056 OutputPort (packet_uid, port != -1 ? port : OFPP_NONE, vpe->parent_port,
false);
1065 m_vportTable.chain_match_count++;
1068 vpe = vpe->parent_port_ptr;
1075 OpenFlowSwitchNetDevice::ReceiveFeaturesRequest (
const void *msg)
1077 SendFeaturesReply ();
1082 OpenFlowSwitchNetDevice::ReceiveVPortTableFeaturesRequest (
const void *msg)
1084 SendVPortTableFeatures ();
1089 OpenFlowSwitchNetDevice::ReceiveGetConfigRequest (
const void *msg)
1092 ofp_switch_config *osc = (ofp_switch_config*)MakeOpenflowReply (
sizeof *osc, OFPT_GET_CONFIG_REPLY, &buffer);
1093 osc->flags = htons (m_flags);
1094 osc->miss_send_len = htons (m_missSendLen);
1096 return SendOpenflowBuffer (buffer);
1100 OpenFlowSwitchNetDevice::ReceiveSetConfig (
const void *msg)
1102 const ofp_switch_config *osc = (ofp_switch_config*)msg;
1104 int n_flags = ntohs (osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
1105 if ((n_flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL && (n_flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP)
1107 n_flags = (n_flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
1111 m_missSendLen = ntohs (osc->miss_send_len);
1116 OpenFlowSwitchNetDevice::ReceivePacketOut (
const void *msg)
1118 const ofp_packet_out *opo = (ofp_packet_out*)msg;
1120 size_t actions_len = ntohs (opo->actions_len);
1122 if (actions_len > (ntohs (opo->header.length) -
sizeof *opo))
1124 NS_LOG_DEBUG (
"message too short for number of actions");
1128 if (ntohl (opo->buffer_id) == (uint32_t) -1)
1131 int data_len = ntohs (opo->header.length) -
sizeof *opo - actions_len;
1132 buffer = ofpbuf_new (data_len);
1133 ofpbuf_put (buffer, (uint8_t *)opo->actions + actions_len, data_len);
1145 flow_extract (buffer, opo->in_port, &key.flow);
1148 if (v_code != ACT_VALIDATION_OK)
1150 SendErrorMsg (OFPET_BAD_ACTION, v_code, msg, ntohs (opo->header.length));
1151 ofpbuf_delete (buffer);
1155 ofi::ExecuteActions (
this, opo->buffer_id, buffer, &key, opo->actions, actions_len,
true);
1160 OpenFlowSwitchNetDevice::ReceivePortMod (
const void *msg)
1162 ofp_port_mod* opm = (ofp_port_mod*)msg;
1164 int port = opm->port_no;
1165 if (port < DP_MAX_PORTS)
1167 ofi::Port& p = m_ports[
port];
1170 Mac48Address hw_addr = Mac48Address ();
1171 hw_addr.CopyFrom (opm->hw_addr);
1172 if (p.netdev->GetAddress () != hw_addr)
1179 uint32_t config_mask = ntohl (opm->mask);
1180 p.config &= ~config_mask;
1181 p.config |= ntohl (opm->config) & config_mask;
1184 if (opm->mask & htonl (OFPPC_PORT_DOWN))
1186 if ((opm->config & htonl (OFPPC_PORT_DOWN)) && (p.config & OFPPC_PORT_DOWN) == 0)
1188 p.config |= OFPPC_PORT_DOWN;
1191 else if ((opm->config & htonl (OFPPC_PORT_DOWN)) == 0 && (p.config & OFPPC_PORT_DOWN))
1193 p.config &= ~OFPPC_PORT_DOWN;
1204 OpenFlowSwitchNetDevice::ReceiveVPortMod (
const void *msg)
1206 const ofp_vport_mod *ovpm = (ofp_vport_mod*)msg;
1208 uint16_t command = ntohs (ovpm->command);
1209 if (command == OFPVP_ADD)
1211 return AddVPort (ovpm);
1213 else if (command == OFPVP_DELETE)
1215 if (remove_vport_table_entry (&m_vportTable, ntohl (ovpm->vport)))
1217 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
1225 OpenFlowSwitchNetDevice::AddFlow (
const ofp_flow_mod *ofm)
1227 size_t actions_len = ntohs (ofm->header.length) -
sizeof *ofm;
1230 sw_flow *flow = flow_alloc (actions_len);
1233 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1240 flow_extract_match (&flow->key, &ofm->match);
1243 if (v_code != ACT_VALIDATION_OK)
1245 SendErrorMsg (OFPET_BAD_ACTION, v_code, ofm, ntohs (ofm->header.length));
1247 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1255 flow->priority = flow->key.wildcards ? ntohs (ofm->priority) : -1;
1256 flow->idle_timeout = ntohs (ofm->idle_timeout);
1257 flow->hard_timeout = ntohs (ofm->hard_timeout);
1258 flow->used = flow->created = time_now ();
1259 flow->sf_acts->actions_len = actions_len;
1260 flow->byte_count = 0;
1261 flow->packet_count = 0;
1262 memcpy (flow->sf_acts->actions, ofm->actions, actions_len);
1265 int error = chain_insert (m_chain, flow);
1268 if (error == -ENOBUFS)
1270 SendErrorMsg (OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL, ofm, ntohs (ofm->header.length));
1273 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1281 if (ntohl (ofm->buffer_id) != std::numeric_limits<uint32_t>::max ())
1287 flow_used (flow, buffer);
1288 flow_extract (buffer, ofm->match.in_port, &key.flow);
1289 ofi::ExecuteActions (
this, ofm->buffer_id, buffer, &key, ofm->actions, actions_len,
false);
1290 ofpbuf_delete (buffer);
1301 OpenFlowSwitchNetDevice::ModFlow (
const ofp_flow_mod *ofm)
1304 flow_extract_match (&key, &ofm->match);
1306 size_t actions_len = ntohs (ofm->header.length) -
sizeof *ofm;
1309 if (v_code != ACT_VALIDATION_OK)
1311 SendErrorMsg ((ofp_error_type)OFPET_BAD_ACTION, v_code, ofm, ntohs (ofm->header.length));
1312 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1319 uint16_t priority = key.wildcards ? ntohs (ofm->priority) : -1;
1320 int strict = (ofm->command == htons (OFPFC_MODIFY_STRICT)) ? 1 : 0;
1321 chain_modify (m_chain, &key, priority, strict, ofm->actions, actions_len);
1323 if (ntohl (ofm->buffer_id) != std::numeric_limits<uint32_t>::max ())
1328 sw_flow_key skb_key;
1329 flow_extract (buffer, ofm->match.in_port, &skb_key.flow);
1330 ofi::ExecuteActions (
this, ofm->buffer_id, buffer, &skb_key, ofm->actions, actions_len,
false);
1331 ofpbuf_delete (buffer);
1342 OpenFlowSwitchNetDevice::ReceiveFlow (
const void *msg)
1345 const ofp_flow_mod *ofm = (ofp_flow_mod*)msg;
1346 uint16_t command = ntohs (ofm->command);
1348 if (command == OFPFC_ADD)
1350 return AddFlow (ofm);
1352 else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT))
1354 return ModFlow (ofm);
1356 else if (command == OFPFC_DELETE)
1359 flow_extract_match (&key, &ofm->match);
1360 return chain_delete (m_chain, &key, ofm->out_port, 0, 0) ? 0 : -ESRCH;
1362 else if (command == OFPFC_DELETE_STRICT)
1366 flow_extract_match (&key, &ofm->match);
1367 priority = key.wildcards ? ntohs (ofm->priority) : -1;
1368 return chain_delete (m_chain, &key, ofm->out_port, priority, 1) ? 0 : -ESRCH;
1377 OpenFlowSwitchNetDevice::StatsDump (ofi::StatsDumpCallback *cb)
1379 ofp_stats_reply *osr;
1388 osr = (ofp_stats_reply*)MakeOpenflowReply (
sizeof *osr, OFPT_STATS_REPLY, &buffer);
1389 osr->type = htons (cb->s->type);
1392 err = cb->s->DoDump (
this, cb->state, buffer);
1402 osr = (ofp_stats_reply*)ofpbuf_at_assert (buffer, 0,
sizeof *osr);
1403 osr->flags = ntohs (OFPSF_REPLY_MORE);
1406 int err2 = SendOpenflowBuffer (buffer);
1417 OpenFlowSwitchNetDevice::StatsDone (ofi::StatsDumpCallback *cb)
1421 cb->s->DoCleanup (cb->state);
1428 OpenFlowSwitchNetDevice::ReceiveStatsRequest (
const void *oh)
1430 const ofp_stats_request *rq = (ofp_stats_request*)oh;
1431 size_t rq_len = ntohs (rq->header.length);
1432 int type = ntohs (rq->type);
1433 int body_len = rq_len - offsetof (ofp_stats_request, body);
1434 ofi::Stats* st =
new ofi::Stats ((ofp_stats_types)type, (
unsigned)body_len);
1441 ofi::StatsDumpCallback cb;
1443 cb.rq = (ofp_stats_request*)xmemdup (rq, rq_len);
1450 int err = cb.s->DoInit (rq->body, body_len, &cb.state);
1453 NS_LOG_WARN (
"failed initialization of stats request type " << type <<
": " << strerror (-err));
1459 if (m_controller != 0)
1461 m_controller->StartDump (&cb);
1465 NS_LOG_ERROR (
"Switch needs to be registered to a controller in order to start the stats reply.");
1472 OpenFlowSwitchNetDevice::ReceiveEchoRequest (
const void *oh)
1474 return SendOpenflowBuffer (make_echo_reply ((ofp_header*)oh));
1478 OpenFlowSwitchNetDevice::ReceiveEchoReply (
const void *oh)
1484 OpenFlowSwitchNetDevice::ForwardControlInput (
const void *msg,
size_t length)
1487 ofp_header *oh = (ofp_header*) msg;
1488 if (ntohs (oh->length) > length)
1492 assert (oh->version == OFP_VERSION);
1499 case OFPT_FEATURES_REQUEST:
1500 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveFeaturesRequest (msg);
1502 case OFPT_GET_CONFIG_REQUEST:
1503 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveGetConfigRequest (msg);
1505 case OFPT_SET_CONFIG:
1506 error = length <
sizeof(ofp_switch_config) ? -EFAULT : ReceiveSetConfig (msg);
1508 case OFPT_PACKET_OUT:
1509 error = length <
sizeof(ofp_packet_out) ? -EFAULT : ReceivePacketOut (msg);
1512 error = length <
sizeof(ofp_flow_mod) ? -EFAULT : ReceiveFlow (msg);
1515 error = length <
sizeof(ofp_port_mod) ? -EFAULT : ReceivePortMod (msg);
1517 case OFPT_STATS_REQUEST:
1518 error = length <
sizeof(ofp_stats_request) ? -EFAULT : ReceiveStatsRequest (msg);
1520 case OFPT_ECHO_REQUEST:
1521 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveEchoRequest (msg);
1523 case OFPT_ECHO_REPLY:
1524 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveEchoReply (msg);
1526 case OFPT_VPORT_MOD:
1527 error = length <
sizeof(ofp_vport_mod) ? -EFAULT : ReceiveVPortMod (msg);
1529 case OFPT_VPORT_TABLE_FEATURES_REQUEST:
1530 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveVPortTableFeaturesRequest (msg);
1533 SendErrorMsg ((ofp_error_type)OFPET_BAD_REQUEST, (ofp_bad_request_code)OFPBRC_BAD_TYPE, msg, length);
1539 free ((ofpbuf*)msg);
1545 OpenFlowSwitchNetDevice::GetChain ()
1551 OpenFlowSwitchNetDevice::GetNSwitchPorts (
void)
const
1554 return m_ports.size ();
1558 OpenFlowSwitchNetDevice::GetSwitchPort (uint32_t n)
const
1565 OpenFlowSwitchNetDevice::GetSwitchPortIndex (ofi::Port p)
1567 for (
size_t i = 0; i < m_ports.size (); i++)
1569 if (m_ports[i].netdev == p.netdev)
1578 OpenFlowSwitchNetDevice::GetVPortTable ()
1580 return m_vportTable;
1585 #endif // NS3_OPENFLOW
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
void discard_buffer(uint32_t id)
#define NS_LOG_FUNCTION(parameters)
void 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)
Executes a list of flow table actions.
static const char * GetSerialNumber()
uint16_t m_flags
Flags; configurable by the controller.
bool IsMulticast(const Address &ad)
Address family-independent test for a multicast address.
#define NS_ASSERT(condition)
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
#define NS_LOG_COMPONENT_DEFINE(name)
static const char * GetHardwareDescription()
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
ofpbuf * retrieve_buffer(uint32_t id)
#define NS_FATAL_ERROR(msg)
fatal error handling
static TypeId GetTypeId(void)
static const char * GetManufacturerDescription()
void ExecuteVPortActions(Ptr< OpenFlowSwitchNetDevice > swtch, uint64_t packet_uid, ofpbuf *buffer, sw_flow_key *key, const ofp_action_header *actions, size_t actions_len)
Executes a list of virtual port table entry actions.
uint64_t m_id
Unique identifier for this switch, needed for OpenFlow.
#define OFP_SUPPORTED_ACTIONS
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
uint32_t save_buffer(ofpbuf *)
static const char * GetSoftwareDescription()
TypeId AddAttribute(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeAccessor > accessor, Ptr< const AttributeChecker > checker)
#define OFP_SUPPORTED_VPORT_TABLE_ACTIONS
OpenFlowSwitchNetDevice()
uint16_t ValidateActions(const sw_flow_key *key, const ofp_action_header *actions, size_t actions_len)
Validates a list of flow table actions.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
#define NS_LOG_DEBUG(msg)
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
#define NS_LOG_ERROR(msg)
uint16_t ValidateVPortActions(const ofp_action_header *actions, size_t actions_len)
Validates a list of virtual port table entry actions.
uint16_t m_missSendLen
Flow Table Miss Send Length; configurable by the controller.
TypeId SetParent(TypeId tid)
#define OFP_SUPPORTED_CAPABILITIES
Time m_lookupDelay
Flow Table Lookup Delay [overhead].