21 #include "ns3/udp-l4-protocol.h"
22 #include "ns3/tcp-l4-protocol.h"
33 return "The ns-3 team";
45 return "Simulated OpenFlow Switch";
57 uint8_t ea[ETH_ADDR_LEN];
59 return eth_addr_to_uint64 (ea);
65 static TypeId tid = TypeId (
"ns3::OpenFlowSwitchNetDevice")
67 .SetGroupName (
"Openflow")
70 "The identification of the OpenFlowSwitchNetDevice/Datapath, needed for OpenFlow compatibility.",
71 UintegerValue (GenerateId ()),
73 MakeUintegerChecker<uint64_t> ())
74 .AddAttribute (
"FlowTableLookupDelay",
75 "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.",
79 .AddAttribute (
"Flags",
80 "Flags to turn different functionality on/off, such as whether to inform the controller when a flow expires, or how to handle fragments.",
83 MakeUintegerChecker<uint16_t> ())
84 .AddAttribute (
"FlowTableMissSendLength",
85 "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.",
86 UintegerValue (OFP_DEFAULT_MISS_SEND_LEN),
88 MakeUintegerChecker<uint16_t> ())
100 m_channel = CreateObject<BridgeChannel> ();
108 m_chain = chain_create ();
111 NS_LOG_ERROR (
"Not enough memory to create the flow table.");
114 m_ports.reserve (DP_MAX_PORTS);
115 vport_table_init (&m_vportTable);
118 OpenFlowSwitchNetDevice::~OpenFlowSwitchNetDevice ()
124 OpenFlowSwitchNetDevice::DoDispose ()
128 for (Ports_t::iterator b = m_ports.begin (), e = m_ports.end (); b != e; b++)
130 SendPortStatus (*b, OFPPR_DELETE);
137 chain_destroy (m_chain);
138 RBTreeDestroy (m_vportTable.table);
141 NetDevice::DoDispose ();
145 OpenFlowSwitchNetDevice::SetController (Ptr<ofi::Controller> c)
147 if (m_controller != 0)
154 m_controller->AddSwitch (
this);
158 OpenFlowSwitchNetDevice::AddSwitchPort (Ptr<NetDevice> switchPort)
162 if (!Mac48Address::IsMatchingType (switchPort->GetAddress ()))
164 NS_FATAL_ERROR (
"Device does not support eui 48 addresses: cannot be added to switch.");
166 if (!switchPort->SupportsSendFrom ())
168 NS_FATAL_ERROR (
"Device does not support SendFrom: cannot be added to switch.");
170 if (m_address == Mac48Address ())
172 m_address = Mac48Address::ConvertFrom (switchPort->GetAddress ());
175 if (m_ports.size () < DP_MAX_PORTS)
179 p.netdev = switchPort;
180 m_ports.push_back (p);
183 SendPortStatus (p, OFPPR_ADD);
185 NS_LOG_DEBUG (
"RegisterProtocolHandler for " << switchPort->GetInstanceTypeId ().GetName ());
186 m_node->RegisterProtocolHandler (
MakeCallback (&OpenFlowSwitchNetDevice::ReceiveFromDevice,
this),
187 0, switchPort,
true);
188 m_channel->AddChannel (switchPort->GetChannel ());
199 OpenFlowSwitchNetDevice::SetIfIndex (
const uint32_t index)
206 OpenFlowSwitchNetDevice::GetIfIndex (
void)
const
213 OpenFlowSwitchNetDevice::GetChannel (
void)
const
220 OpenFlowSwitchNetDevice::SetAddress (Address
address)
223 m_address = Mac48Address::ConvertFrom (address);
227 OpenFlowSwitchNetDevice::GetAddress (
void)
const
234 OpenFlowSwitchNetDevice::SetMtu (
const uint16_t mtu)
242 OpenFlowSwitchNetDevice::GetMtu (
void)
const
250 OpenFlowSwitchNetDevice::IsLinkUp (
void)
const
258 OpenFlowSwitchNetDevice::AddLinkChangeCallback (Callback<void> callback)
263 OpenFlowSwitchNetDevice::IsBroadcast (
void)
const
270 OpenFlowSwitchNetDevice::GetBroadcast (
void)
const
273 return Mac48Address (
"ff:ff:ff:ff:ff:ff");
284 OpenFlowSwitchNetDevice::GetMulticast (Ipv4Address multicastGroup)
const
287 Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
293 OpenFlowSwitchNetDevice::IsPointToPoint (
void)
const
300 OpenFlowSwitchNetDevice::IsBridge (
void)
const
307 OpenFlowSwitchNetDevice::DoOutput (uint32_t packet_uid,
int in_port,
size_t max_len,
int out_port,
bool ignore_no_fwd)
309 if (out_port != OFPP_CONTROLLER)
311 OutputPort (packet_uid, in_port, out_port, ignore_no_fwd);
315 OutputControl (packet_uid, in_port, max_len, OFPR_ACTION);
320 OpenFlowSwitchNetDevice::Send (Ptr<Packet> packet,
const Address& dest, uint16_t protocolNumber)
323 return SendFrom (packet, m_address, dest, protocolNumber);
327 OpenFlowSwitchNetDevice::SendFrom (Ptr<Packet> packet,
const Address& src,
const Address& dest, uint16_t protocolNumber)
331 ofpbuf *buffer = BufferFromPacket (packet,src,dest,GetMtu (),protocolNumber);
334 ofi::SwitchPacketMetadata
data;
335 data.packet = packet;
336 data.buffer = buffer;
337 data.protocolNumber = protocolNumber;
338 data.src = Address (src);
339 data.dst = Address (dest);
340 m_packetData.insert (std::make_pair (packet_uid, data));
342 RunThroughFlowTable (packet_uid, -1);
349 OpenFlowSwitchNetDevice::GetNode (
void)
const
356 OpenFlowSwitchNetDevice::SetNode (Ptr<Node> node)
363 OpenFlowSwitchNetDevice::NeedsArp (
void)
const
370 OpenFlowSwitchNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
377 OpenFlowSwitchNetDevice::SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb)
380 m_promiscRxCallback = cb;
384 OpenFlowSwitchNetDevice::SupportsSendFrom ()
const
391 OpenFlowSwitchNetDevice::GetMulticast (Ipv6Address addr)
const
394 return Mac48Address::GetMulticast (addr);
399 OpenFlowSwitchNetDevice::AddVPort (
const ofp_vport_mod *ovpm)
401 size_t actions_len = ntohs (ovpm->header.length) -
sizeof *ovpm;
402 unsigned int vport = ntohl (ovpm->vport);
403 unsigned int parent_port = ntohl (ovpm->parent_port);
406 vport_table_entry *vpe = vport_table_lookup (&m_vportTable, vport);
410 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
416 if (v_code != ACT_VALIDATION_OK)
418 SendErrorMsg (OFPET_BAD_ACTION, v_code, ovpm, ntohs (ovpm->header.length));
422 vpe = vport_table_entry_alloc (actions_len);
425 vpe->parent_port = parent_port;
426 if (vport < OFPP_VP_START || vport > OFPP_VP_END)
428 NS_LOG_ERROR (
"port " << vport <<
" is not in the virtual port range (" << OFPP_VP_START <<
"-" << OFPP_VP_END <<
")");
429 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
430 free_vport_table_entry (vpe);
434 vpe->port_acts->actions_len = actions_len;
435 memcpy (vpe->port_acts->actions, ovpm->actions, actions_len);
437 int error = insert_vport_table_entry (&m_vportTable, vpe);
440 NS_LOG_ERROR (
"could not insert port table entry for port " << vport);
447 OpenFlowSwitchNetDevice::BufferFromPacket (Ptr<Packet> packet, Address src, Address dst,
int mtu, uint16_t protocol)
449 NS_LOG_INFO (
"Creating Openflow buffer from packet.");
456 const int headroom = 128 + 2;
457 const int hard_header = VLAN_ETH_HEADER_LEN;
458 ofpbuf *buffer = ofpbuf_new (headroom + hard_header + mtu);
459 buffer->data = (
char*)buffer->data + headroom + hard_header;
461 int l2_length = 0, l3_length = 0, l4_length = 0;
464 EthernetHeader eth_hd;
465 if (packet->PeekHeader (eth_hd))
467 buffer->l2 =
new eth_header;
468 eth_header* eth_h = (eth_header*)buffer->l2;
469 dst.CopyTo (eth_h->eth_dst);
470 src.CopyTo (eth_h->eth_src);
471 eth_h->eth_type = htons (ETH_TYPE_IP);
474 l2_length = ETH_HEADER_LEN;
478 if (protocol == Ipv4L3Protocol::PROT_NUMBER)
481 if (packet->PeekHeader (ip_hd))
483 buffer->l3 =
new ip_header;
484 ip_header* ip_h = (ip_header*)buffer->l3;
485 ip_h->ip_ihl_ver = IP_IHL_VER (5, IP_VERSION);
486 ip_h->ip_tos = ip_hd.GetTos ();
487 ip_h->ip_tot_len = packet->GetSize ();
488 ip_h->ip_id = ip_hd.GetIdentification ();
489 ip_h->ip_frag_off = ip_hd.GetFragmentOffset ();
490 ip_h->ip_ttl = ip_hd.GetTtl ();
491 ip_h->ip_proto = ip_hd.GetProtocol ();
492 ip_h->ip_src = htonl (ip_hd.GetSource ().Get ());
493 ip_h->ip_dst = htonl (ip_hd.GetDestination ().Get ());
494 ip_h->ip_csum = csum (&ip_h,
sizeof ip_h);
497 l3_length = IP_HEADER_LEN;
504 if (packet->PeekHeader (arp_hd))
506 buffer->l3 =
new arp_eth_header;
507 arp_eth_header* arp_h = (arp_eth_header*)buffer->l3;
508 arp_h->ar_hrd = ARP_HRD_ETHERNET;
509 arp_h->ar_pro = ARP_PRO_IP;
510 arp_h->ar_op = arp_hd.m_type;
511 arp_hd.GetDestinationHardwareAddress ().CopyTo (arp_h->ar_tha);
512 arp_hd.GetSourceHardwareAddress ().CopyTo (arp_h->ar_sha);
513 arp_h->ar_tpa = arp_hd.GetDestinationIpv4Address ().Get ();
514 arp_h->ar_spa = arp_hd.GetSourceIpv4Address ().Get ();
515 arp_h->ar_hln =
sizeof arp_h->ar_tha;
516 arp_h->ar_pln =
sizeof arp_h->ar_tpa;
519 l3_length = ARP_ETH_HEADER_LEN;
523 if (protocol == Ipv4L3Protocol::PROT_NUMBER)
525 ip_header* ip_h = (ip_header*)buffer->l3;
526 if (ip_h->ip_proto == TcpL4Protocol::PROT_NUMBER)
529 if (packet->PeekHeader (tcp_hd))
531 buffer->l4 =
new tcp_header;
532 tcp_header* tcp_h = (tcp_header*)buffer->l4;
533 tcp_h->tcp_src = htons (tcp_hd.GetSourcePort ());
534 tcp_h->tcp_dst = htons (tcp_hd.GetDestinationPort ());
535 tcp_h->tcp_seq = tcp_hd.GetSequenceNumber ().GetValue ();
536 tcp_h->tcp_ack = tcp_hd.GetAckNumber ().GetValue ();
537 tcp_h->tcp_ctl = TCP_FLAGS (tcp_hd.GetFlags ());
538 tcp_h->tcp_winsz = tcp_hd.GetWindowSize ();
539 tcp_h->tcp_urg = tcp_hd.GetUrgentPointer ();
540 tcp_h->tcp_csum = csum (&tcp_h,
sizeof tcp_h);
543 l4_length = TCP_HEADER_LEN;
546 else if (ip_h->ip_proto == UdpL4Protocol::PROT_NUMBER)
549 if (packet->PeekHeader (udp_hd))
551 buffer->l4 =
new udp_header;
552 udp_header* udp_h = (udp_header*)buffer->l4;
553 udp_h->udp_src = htons (udp_hd.GetSourcePort ());
554 udp_h->udp_dst = htons (udp_hd.GetDestinationPort ());
555 udp_h->udp_len = htons (UDP_HEADER_LEN + packet->GetSize ());
557 ip_header* ip_h = (ip_header*)buffer->l3;
558 uint32_t udp_csum = csum_add32 (0, ip_h->ip_src);
559 udp_csum = csum_add32 (udp_csum, ip_h->ip_dst);
560 udp_csum = csum_add16 (udp_csum, IP_TYPE_UDP << 8);
561 udp_csum = csum_add16 (udp_csum, udp_h->udp_len);
562 udp_csum = csum_continue (udp_csum, udp_h,
sizeof udp_h);
563 udp_h->udp_csum = csum_finish (csum_continue (udp_csum, buffer->data, buffer->size));
566 l4_length = UDP_HEADER_LEN;
572 packet->CopyData ((uint8_t*)buffer->data, packet->GetSize ());
576 ofpbuf_push (buffer, buffer->l4, l4_length);
577 delete (tcp_header*)buffer->l4;
581 ofpbuf_push (buffer, buffer->l3, l3_length);
582 delete (ip_header*)buffer->l3;
586 ofpbuf_push (buffer, buffer->l2, l2_length);
587 delete (eth_header*)buffer->l2;
594 OpenFlowSwitchNetDevice::ReceiveFromDevice (Ptr<NetDevice> netdev, Ptr<const Packet> packet, uint16_t protocol,
595 const Address& src,
const Address& dst, PacketType packetType)
598 NS_LOG_INFO (
"--------------------------------------------");
601 if (!m_promiscRxCallback.IsNull ())
603 m_promiscRxCallback (
this, packet, protocol, src, dst, packetType);
606 Mac48Address dst48 = Mac48Address::ConvertFrom (dst);
607 NS_LOG_INFO (
"Received packet from " << Mac48Address::ConvertFrom (src) <<
" looking for " << dst48);
609 for (
size_t i = 0; i < m_ports.size (); i++)
611 if (m_ports[i].netdev == netdev)
613 if (packetType == PACKET_HOST && dst48 == m_address)
615 m_rxCallback (
this, packet, protocol, src);
617 else if (packetType == PACKET_BROADCAST || packetType == PACKET_MULTICAST || packetType == PACKET_OTHERHOST)
619 if (packetType == PACKET_OTHERHOST && dst48 == m_address)
621 m_rxCallback (
this, packet, protocol, src);
625 if (packetType != PACKET_OTHERHOST)
627 m_rxCallback (
this, packet, protocol, src);
630 ofi::SwitchPacketMetadata
data;
631 data.packet = packet->Copy ();
633 ofpbuf *buffer = BufferFromPacket (data.packet,src,dst,netdev->GetMtu (),protocol);
634 m_ports[i].rx_packets++;
635 m_ports[i].rx_bytes += buffer->size;
636 data.buffer = buffer;
639 data.protocolNumber = protocol;
640 data.src = Address (src);
641 data.dst = Address (dst);
642 m_packetData.insert (std::make_pair (packet_uid, data));
644 RunThroughFlowTable (packet_uid, i);
654 if (now >=
Seconds (m_lastExecute.GetSeconds () + 1))
657 for (
size_t i = 0; i < m_ports.size (); i++)
659 if (UpdatePortStatus (m_ports[i]))
661 SendPortStatus (m_ports[i], OFPPR_MODIFY);
666 List deleted = LIST_INITIALIZER (&deleted);
668 chain_timeout (m_chain, &deleted);
669 LIST_FOR_EACH_SAFE (f, n, sw_flow, node, &deleted)
671 std::ostringstream str;
673 for (
int i = 0; i < 6; i++)
674 str << (i!=0 ?
":" :
"") << std::hex << f->key.flow.dl_src[i]/16 << f->key.flow.dl_src[i]%16;
676 for (
int i = 0; i < 6; i++)
677 str << (i!=0 ?
":" :
"") << std::hex << f->key.flow.dl_dst[i]/16 << f->key.flow.dl_dst[i]%16;
681 SendFlowExpired (f, (ofp_flow_expired_reason)f->reason);
682 list_remove (&f->node);
691 OpenFlowSwitchNetDevice::OutputAll (uint32_t packet_uid,
int in_port,
bool flood)
697 for (
size_t i = 0; i < m_ports.size (); i++)
699 if (i == (
unsigned)in_port)
703 if (flood && m_ports[i].config & OFPPC_NO_FLOOD)
709 OutputPort (packet_uid, in_port, prev_port,
false);
715 OutputPort (packet_uid, in_port, prev_port,
false);
722 OpenFlowSwitchNetDevice::OutputPacket (uint32_t packet_uid,
int out_port)
724 if (out_port >= 0 && out_port < DP_MAX_PORTS)
726 ofi::Port& p = m_ports[out_port];
727 if (p.netdev != 0 && !(p.config & OFPPC_PORT_DOWN))
729 ofi::SwitchPacketMetadata data = m_packetData.find (packet_uid)->second;
730 size_t bufsize = data.buffer->size;
731 NS_LOG_INFO (
"Sending packet " << data.packet->GetUid () <<
" over port " << out_port);
732 if (p.netdev->SendFrom (data.packet->Copy (), data.src, data.dst, data.protocolNumber))
735 p.tx_bytes += bufsize;
745 NS_LOG_DEBUG (
"can't forward to bad port " << out_port);
749 OpenFlowSwitchNetDevice::OutputPort (uint32_t packet_uid,
int in_port,
int out_port,
bool ignore_no_fwd)
753 if (out_port == OFPP_FLOOD)
755 OutputAll (packet_uid, in_port,
true);
757 else if (out_port == OFPP_ALL)
759 OutputAll (packet_uid, in_port,
false);
761 else if (out_port == OFPP_CONTROLLER)
763 OutputControl (packet_uid, in_port, 0, OFPR_ACTION);
765 else if (out_port == OFPP_IN_PORT)
767 OutputPacket (packet_uid, in_port);
769 else if (out_port == OFPP_TABLE)
771 RunThroughFlowTable (packet_uid, in_port < DP_MAX_PORTS ? in_port : -1,
false);
773 else if (out_port >= OFPP_VP_START && out_port <= OFPP_VP_END)
776 NS_LOG_INFO (
"packet sent to virtual port " << out_port);
777 if (in_port < DP_MAX_PORTS)
779 RunThroughVPortTable (packet_uid, in_port, out_port);
783 RunThroughVPortTable (packet_uid, -1, out_port);
786 else if (in_port == out_port)
792 OutputPacket (packet_uid, out_port);
797 OpenFlowSwitchNetDevice::MakeOpenflowReply (
size_t openflow_len, uint8_t type, ofpbuf **bufferp)
799 return make_openflow_xid (openflow_len, type, 0, bufferp);
803 OpenFlowSwitchNetDevice::SendOpenflowBuffer (ofpbuf *buffer)
805 if (m_controller != 0)
807 update_openflow_length (buffer);
808 m_controller->ReceiveFromSwitch (
this, buffer);
815 OpenFlowSwitchNetDevice::OutputControl (uint32_t packet_uid,
int in_port,
size_t max_len,
int reason)
819 ofpbuf* buffer = m_packetData.find (packet_uid)->second.buffer;
820 size_t total_len = buffer->size;
821 if (packet_uid != std::numeric_limits<uint32_t>::max () && max_len != 0 && buffer->size > max_len)
823 buffer->size = max_len;
826 ofp_packet_in *opi = (ofp_packet_in*)ofpbuf_push_uninit (buffer, offsetof (ofp_packet_in, data));
827 opi->header.version = OFP_VERSION;
828 opi->header.type = OFPT_PACKET_IN;
829 opi->header.length = htons (buffer->size);
830 opi->header.xid = htonl (0);
831 opi->buffer_id = htonl (packet_uid);
832 opi->total_len = htons (total_len);
833 opi->in_port = htons (in_port);
834 opi->reason = reason;
836 SendOpenflowBuffer (buffer);
840 OpenFlowSwitchNetDevice::FillPortDesc (ofi::Port p, ofp_phy_port *desc)
842 desc->port_no = htons (GetSwitchPortIndex (p));
844 std::ostringstream nm;
845 nm <<
"eth" << GetSwitchPortIndex (p);
846 strncpy ((
char *)desc->name, nm.str ().c_str (),
sizeof desc->name);
848 p.netdev->GetAddress ().CopyTo (desc->hw_addr);
849 desc->config = htonl (p.config);
850 desc->state = htonl (p.state);
855 desc->advertised = 0;
860 OpenFlowSwitchNetDevice::SendFeaturesReply ()
863 ofp_switch_features *ofr = (ofp_switch_features*)MakeOpenflowReply (
sizeof *ofr, OFPT_FEATURES_REPLY, &buffer);
864 ofr->datapath_id = htonll (m_id);
865 ofr->n_tables = m_chain->n_tables;
870 for (
size_t i = 0; i < m_ports.size (); i++)
872 ofp_phy_port* opp = (ofp_phy_port*)ofpbuf_put_zeros (buffer,
sizeof *opp);
873 FillPortDesc (m_ports[i], opp);
876 SendOpenflowBuffer (buffer);
880 OpenFlowSwitchNetDevice::SendVPortTableFeatures ()
883 ofp_vport_table_features *ovtfr = (ofp_vport_table_features*)MakeOpenflowReply (
sizeof *ovtfr, OFPT_VPORT_TABLE_FEATURES_REPLY, &buffer);
885 ovtfr->max_vports = htonl (m_vportTable.max_vports);
886 ovtfr->max_chain_depth = htons (-1);
887 ovtfr->mixed_chaining =
true;
888 SendOpenflowBuffer (buffer);
892 OpenFlowSwitchNetDevice::UpdatePortStatus (ofi::Port& p)
894 uint32_t orig_config = p.config;
895 uint32_t orig_state = p.state;
898 p.config &= ~OFPPC_PORT_DOWN;
900 if (p.netdev->IsLinkUp ())
902 p.state &= ~OFPPS_LINK_DOWN;
906 p.state |= OFPPS_LINK_DOWN;
909 return ((orig_config != p.config) || (orig_state != p.state));
913 OpenFlowSwitchNetDevice::SendPortStatus (ofi::Port p, uint8_t status)
916 ofp_port_status *ops = (ofp_port_status*)MakeOpenflowReply (
sizeof *ops, OFPT_PORT_STATUS, &buffer);
917 ops->reason = status;
918 memset (ops->pad, 0,
sizeof ops->pad);
919 FillPortDesc (p, &ops->desc);
921 SendOpenflowBuffer (buffer);
922 ofpbuf_delete (buffer);
926 OpenFlowSwitchNetDevice::SendFlowExpired (sw_flow *flow,
enum ofp_flow_expired_reason reason)
929 ofp_flow_expired *ofe = (ofp_flow_expired*)MakeOpenflowReply (
sizeof *ofe, OFPT_FLOW_EXPIRED, &buffer);
930 flow_fill_match (&ofe->match, &flow->key);
932 ofe->priority = htons (flow->priority);
933 ofe->reason = reason;
934 memset (ofe->pad, 0,
sizeof ofe->pad);
936 ofe->duration = htonl (time_now () - flow->created);
937 memset (ofe->pad2, 0,
sizeof ofe->pad2);
938 ofe->packet_count = htonll (flow->packet_count);
939 ofe->byte_count = htonll (flow->byte_count);
940 SendOpenflowBuffer (buffer);
944 OpenFlowSwitchNetDevice::SendErrorMsg (uint16_t type, uint16_t code,
const void *data,
size_t len)
947 ofp_error_msg *oem = (ofp_error_msg*)MakeOpenflowReply (
sizeof(*oem) + len, OFPT_ERROR, &buffer);
948 oem->type = htons (type);
949 oem->code = htons (code);
950 memcpy (oem->data, data, len);
951 SendOpenflowBuffer (buffer);
955 OpenFlowSwitchNetDevice::FlowTableLookup (sw_flow_key key, ofpbuf* buffer, uint32_t packet_uid,
int port,
bool send_to_controller)
957 sw_flow *flow = chain_lookup (m_chain, &key);
961 flow_used (flow, buffer);
962 ofi::ExecuteActions (
this, packet_uid, buffer, &key, flow->sf_acts->actions, flow->sf_acts->actions_len,
false);
968 if (send_to_controller)
970 OutputControl (packet_uid, port, m_missSendLen, OFPR_NO_MATCH);
975 m_packetData.erase (packet_uid);
977 ofpbuf_delete (buffer);
981 OpenFlowSwitchNetDevice::RunThroughFlowTable (uint32_t packet_uid,
int port,
bool send_to_controller)
983 ofi::SwitchPacketMetadata data = m_packetData.find (packet_uid)->second;
984 ofpbuf* buffer = data.buffer;
989 if (flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow) && (m_flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP)
991 ofpbuf_delete (buffer);
999 mpls_h.value = ntohl (*((uint32_t*)buffer->l2_5));
1000 if (mpls_h.ttl == 1)
1005 m_ports[
port].mpls_ttl0_dropped++;
1014 uint32_t config = m_ports[
port].config;
1015 if (config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP)
1016 && config & (!eth_addr_equals (key.flow.dl_dst, stp_eth_addr) ? OFPPC_NO_RECV : OFPPC_NO_RECV_STP))
1023 Simulator::Schedule (m_lookupDelay, &OpenFlowSwitchNetDevice::FlowTableLookup,
this, key, buffer, packet_uid, port, send_to_controller);
1027 OpenFlowSwitchNetDevice::RunThroughVPortTable (uint32_t packet_uid,
int port, uint32_t vport)
1029 ofpbuf* buffer = m_packetData.find (packet_uid)->second.buffer;
1035 if (flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow)
1036 && (m_flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP)
1042 vport_table_entry *vpe = vport_table_lookup (&m_vportTable, vport);
1043 m_vportTable.lookup_count++;
1046 m_vportTable.port_match_count++;
1050 ofi::ExecuteVPortActions (
this, packet_uid, m_packetData.find (packet_uid)->second.buffer, &key, vpe->port_acts->actions, vpe->port_acts->actions_len);
1051 vport_used (vpe, buffer);
1052 if (vpe->parent_port_ptr == 0)
1056 if (vpe->parent_port <= OFPP_VP_START)
1058 OutputPort (packet_uid, port != -1 ? port : OFPP_NONE, vpe->parent_port,
false);
1067 m_vportTable.chain_match_count++;
1070 vpe = vpe->parent_port_ptr;
1077 OpenFlowSwitchNetDevice::ReceiveFeaturesRequest (
const void *msg)
1079 SendFeaturesReply ();
1084 OpenFlowSwitchNetDevice::ReceiveVPortTableFeaturesRequest (
const void *msg)
1086 SendVPortTableFeatures ();
1091 OpenFlowSwitchNetDevice::ReceiveGetConfigRequest (
const void *msg)
1094 ofp_switch_config *osc = (ofp_switch_config*)MakeOpenflowReply (
sizeof *osc, OFPT_GET_CONFIG_REPLY, &buffer);
1095 osc->flags = htons (m_flags);
1096 osc->miss_send_len = htons (m_missSendLen);
1098 return SendOpenflowBuffer (buffer);
1102 OpenFlowSwitchNetDevice::ReceiveSetConfig (
const void *msg)
1104 const ofp_switch_config *osc = (ofp_switch_config*)msg;
1106 int n_flags = ntohs (osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
1107 if ((n_flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL && (n_flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP)
1109 n_flags = (n_flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
1113 m_missSendLen = ntohs (osc->miss_send_len);
1118 OpenFlowSwitchNetDevice::ReceivePacketOut (
const void *msg)
1120 const ofp_packet_out *opo = (ofp_packet_out*)msg;
1122 size_t actions_len = ntohs (opo->actions_len);
1124 if (actions_len > (ntohs (opo->header.length) -
sizeof *opo))
1126 NS_LOG_DEBUG (
"message too short for number of actions");
1130 if (ntohl (opo->buffer_id) == (uint32_t) -1)
1133 int data_len = ntohs (opo->header.length) -
sizeof *opo - actions_len;
1134 buffer = ofpbuf_new (data_len);
1135 ofpbuf_put (buffer, (uint8_t *)opo->actions + actions_len, data_len);
1147 flow_extract (buffer, opo->in_port, &key.flow);
1150 if (v_code != ACT_VALIDATION_OK)
1152 SendErrorMsg (OFPET_BAD_ACTION, v_code, msg, ntohs (opo->header.length));
1153 ofpbuf_delete (buffer);
1157 ofi::ExecuteActions (
this, opo->buffer_id, buffer, &key, opo->actions, actions_len,
true);
1162 OpenFlowSwitchNetDevice::ReceivePortMod (
const void *msg)
1164 ofp_port_mod* opm = (ofp_port_mod*)msg;
1166 int port = opm->port_no;
1167 if (port < DP_MAX_PORTS)
1169 ofi::Port& p = m_ports[
port];
1172 Mac48Address hw_addr = Mac48Address ();
1173 hw_addr.CopyFrom (opm->hw_addr);
1174 if (p.netdev->GetAddress () != hw_addr)
1181 uint32_t config_mask = ntohl (opm->mask);
1182 p.config &= ~config_mask;
1183 p.config |= ntohl (opm->config) & config_mask;
1186 if (opm->mask & htonl (OFPPC_PORT_DOWN))
1188 if ((opm->config & htonl (OFPPC_PORT_DOWN)) && (p.config & OFPPC_PORT_DOWN) == 0)
1190 p.config |= OFPPC_PORT_DOWN;
1193 else if ((opm->config & htonl (OFPPC_PORT_DOWN)) == 0 && (p.config & OFPPC_PORT_DOWN))
1195 p.config &= ~OFPPC_PORT_DOWN;
1206 OpenFlowSwitchNetDevice::ReceiveVPortMod (
const void *msg)
1208 const ofp_vport_mod *ovpm = (ofp_vport_mod*)msg;
1210 uint16_t command = ntohs (ovpm->command);
1211 if (command == OFPVP_ADD)
1213 return AddVPort (ovpm);
1215 else if (command == OFPVP_DELETE)
1217 if (remove_vport_table_entry (&m_vportTable, ntohl (ovpm->vport)))
1219 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
1227 OpenFlowSwitchNetDevice::AddFlow (
const ofp_flow_mod *ofm)
1229 size_t actions_len = ntohs (ofm->header.length) -
sizeof *ofm;
1232 sw_flow *flow = flow_alloc (actions_len);
1235 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1242 flow_extract_match (&flow->key, &ofm->match);
1245 if (v_code != ACT_VALIDATION_OK)
1247 SendErrorMsg (OFPET_BAD_ACTION, v_code, ofm, ntohs (ofm->header.length));
1249 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1257 flow->priority = flow->key.wildcards ? ntohs (ofm->priority) : -1;
1258 flow->idle_timeout = ntohs (ofm->idle_timeout);
1259 flow->hard_timeout = ntohs (ofm->hard_timeout);
1260 flow->used = flow->created = time_now ();
1261 flow->sf_acts->actions_len = actions_len;
1262 flow->byte_count = 0;
1263 flow->packet_count = 0;
1264 memcpy (flow->sf_acts->actions, ofm->actions, actions_len);
1267 int error = chain_insert (m_chain, flow);
1270 if (error == -ENOBUFS)
1272 SendErrorMsg (OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL, ofm, ntohs (ofm->header.length));
1275 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1283 if (ntohl (ofm->buffer_id) != std::numeric_limits<uint32_t>::max ())
1289 flow_used (flow, buffer);
1290 flow_extract (buffer, ofm->match.in_port, &key.flow);
1291 ofi::ExecuteActions (
this, ofm->buffer_id, buffer, &key, ofm->actions, actions_len,
false);
1292 ofpbuf_delete (buffer);
1303 OpenFlowSwitchNetDevice::ModFlow (
const ofp_flow_mod *ofm)
1306 flow_extract_match (&key, &ofm->match);
1308 size_t actions_len = ntohs (ofm->header.length) -
sizeof *ofm;
1311 if (v_code != ACT_VALIDATION_OK)
1313 SendErrorMsg ((ofp_error_type)OFPET_BAD_ACTION, v_code, ofm, ntohs (ofm->header.length));
1314 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1321 uint16_t priority = key.wildcards ? ntohs (ofm->priority) : -1;
1322 int strict = (ofm->command == htons (OFPFC_MODIFY_STRICT)) ? 1 : 0;
1323 chain_modify (m_chain, &key, priority, strict, ofm->actions, actions_len);
1325 if (ntohl (ofm->buffer_id) != std::numeric_limits<uint32_t>::max ())
1330 sw_flow_key skb_key;
1331 flow_extract (buffer, ofm->match.in_port, &skb_key.flow);
1332 ofi::ExecuteActions (
this, ofm->buffer_id, buffer, &skb_key, ofm->actions, actions_len,
false);
1333 ofpbuf_delete (buffer);
1344 OpenFlowSwitchNetDevice::ReceiveFlow (
const void *msg)
1347 const ofp_flow_mod *ofm = (ofp_flow_mod*)msg;
1348 uint16_t command = ntohs (ofm->command);
1350 if (command == OFPFC_ADD)
1352 return AddFlow (ofm);
1354 else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT))
1356 return ModFlow (ofm);
1358 else if (command == OFPFC_DELETE)
1361 flow_extract_match (&key, &ofm->match);
1362 return chain_delete (m_chain, &key, ofm->out_port, 0, 0) ? 0 : -ESRCH;
1364 else if (command == OFPFC_DELETE_STRICT)
1368 flow_extract_match (&key, &ofm->match);
1369 priority = key.wildcards ? ntohs (ofm->priority) : -1;
1370 return chain_delete (m_chain, &key, ofm->out_port, priority, 1) ? 0 : -ESRCH;
1379 OpenFlowSwitchNetDevice::StatsDump (ofi::StatsDumpCallback *cb)
1381 ofp_stats_reply *osr;
1390 osr = (ofp_stats_reply*)MakeOpenflowReply (
sizeof *osr, OFPT_STATS_REPLY, &buffer);
1391 osr->type = htons (cb->s->type);
1394 err = cb->s->DoDump (
this, cb->state, buffer);
1404 osr = (ofp_stats_reply*)ofpbuf_at_assert (buffer, 0,
sizeof *osr);
1405 osr->flags = ntohs (OFPSF_REPLY_MORE);
1408 int err2 = SendOpenflowBuffer (buffer);
1419 OpenFlowSwitchNetDevice::StatsDone (ofi::StatsDumpCallback *cb)
1423 cb->s->DoCleanup (cb->state);
1430 OpenFlowSwitchNetDevice::ReceiveStatsRequest (
const void *oh)
1432 const ofp_stats_request *rq = (ofp_stats_request*)oh;
1433 size_t rq_len = ntohs (rq->header.length);
1434 int type = ntohs (rq->type);
1435 int body_len = rq_len - offsetof (ofp_stats_request, body);
1436 ofi::Stats* st =
new ofi::Stats ((ofp_stats_types)type, (
unsigned)body_len);
1443 ofi::StatsDumpCallback cb;
1445 cb.rq = (ofp_stats_request*)xmemdup (rq, rq_len);
1452 int err = cb.s->DoInit (rq->body, body_len, &cb.state);
1455 NS_LOG_WARN (
"failed initialization of stats request type " << type <<
": " << strerror (-err));
1461 if (m_controller != 0)
1463 m_controller->StartDump (&cb);
1467 NS_LOG_ERROR (
"Switch needs to be registered to a controller in order to start the stats reply.");
1474 OpenFlowSwitchNetDevice::ReceiveEchoRequest (
const void *oh)
1476 return SendOpenflowBuffer (make_echo_reply ((ofp_header*)oh));
1480 OpenFlowSwitchNetDevice::ReceiveEchoReply (
const void *oh)
1486 OpenFlowSwitchNetDevice::ForwardControlInput (
const void *msg,
size_t length)
1489 ofp_header *oh = (ofp_header*) msg;
1490 if (ntohs (oh->length) > length)
1494 assert (oh->version == OFP_VERSION);
1501 case OFPT_FEATURES_REQUEST:
1502 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveFeaturesRequest (msg);
1504 case OFPT_GET_CONFIG_REQUEST:
1505 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveGetConfigRequest (msg);
1507 case OFPT_SET_CONFIG:
1508 error = length <
sizeof(ofp_switch_config) ? -EFAULT : ReceiveSetConfig (msg);
1510 case OFPT_PACKET_OUT:
1511 error = length <
sizeof(ofp_packet_out) ? -EFAULT : ReceivePacketOut (msg);
1514 error = length <
sizeof(ofp_flow_mod) ? -EFAULT : ReceiveFlow (msg);
1517 error = length <
sizeof(ofp_port_mod) ? -EFAULT : ReceivePortMod (msg);
1519 case OFPT_STATS_REQUEST:
1520 error = length <
sizeof(ofp_stats_request) ? -EFAULT : ReceiveStatsRequest (msg);
1522 case OFPT_ECHO_REQUEST:
1523 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveEchoRequest (msg);
1525 case OFPT_ECHO_REPLY:
1526 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveEchoReply (msg);
1528 case OFPT_VPORT_MOD:
1529 error = length <
sizeof(ofp_vport_mod) ? -EFAULT : ReceiveVPortMod (msg);
1531 case OFPT_VPORT_TABLE_FEATURES_REQUEST:
1532 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveVPortTableFeaturesRequest (msg);
1535 SendErrorMsg ((ofp_error_type)OFPET_BAD_REQUEST, (ofp_bad_request_code)OFPBRC_BAD_TYPE, msg, length);
1541 free ((ofpbuf*)msg);
1547 OpenFlowSwitchNetDevice::GetChain ()
1553 OpenFlowSwitchNetDevice::GetNSwitchPorts (
void)
const
1556 return m_ports.size ();
1560 OpenFlowSwitchNetDevice::GetSwitchPort (uint32_t n)
const
1567 OpenFlowSwitchNetDevice::GetSwitchPortIndex (ofi::Port p)
1569 for (
size_t i = 0; i < m_ports.size (); i++)
1571 if (m_ports[i].netdev == p.netdev)
1580 OpenFlowSwitchNetDevice::GetVPortTable ()
1582 return m_vportTable;
1587 #endif // NS3_OPENFLOW
void discard_buffer(uint32_t id)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
TypeId AddConstructor(void)
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.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
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)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
static const char * GetHardwareDescription()
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
#define NS_FATAL_ERROR(msg)
Fatal error handling.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
ofpbuf * retrieve_buffer(uint32_t id)
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
static TypeId GetTypeId(void)
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
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()
#define OFP_SUPPORTED_VPORT_TABLE_ACTIONS
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
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.
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Time Seconds(double value)
Construct a Time in the indicated unit.
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
uint16_t ValidateVPortActions(const ofp_action_header *actions, size_t actions_len)
Validates a list of virtual port table entry actions.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
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].