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 .AddConstructor<OpenFlowSwitchNetDevice> ()
69 "The identification of the OpenFlowSwitchNetDevice/Datapath, needed for OpenFlow compatibility.",
70 UintegerValue (GenerateId ()),
72 MakeUintegerChecker<uint64_t> ())
74 "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 "Flags to turn different functionality on/off, such as whether to inform the controller when a flow expires, or how to handle fragments.",
82 MakeUintegerChecker<uint16_t> ())
84 "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.",
85 UintegerValue (OFP_DEFAULT_MISS_SEND_LEN),
87 MakeUintegerChecker<uint16_t> ())
99 m_channel = CreateObject<BridgeChannel> ();
107 m_chain = chain_create ();
110 NS_LOG_ERROR (
"Not enough memory to create the flow table.");
113 m_ports.reserve (DP_MAX_PORTS);
114 vport_table_init (&m_vportTable);
117 OpenFlowSwitchNetDevice::~OpenFlowSwitchNetDevice ()
123 OpenFlowSwitchNetDevice::DoDispose ()
127 for (Ports_t::iterator b = m_ports.begin (), e = m_ports.end (); b != e; b++)
129 SendPortStatus (*b, OFPPR_DELETE);
136 chain_destroy (m_chain);
137 RBTreeDestroy (m_vportTable.table);
140 NetDevice::DoDispose ();
144 OpenFlowSwitchNetDevice::SetController (Ptr<ofi::Controller> c)
146 if (m_controller != 0)
153 m_controller->AddSwitch (
this);
157 OpenFlowSwitchNetDevice::AddSwitchPort (Ptr<NetDevice> switchPort)
161 if (!Mac48Address::IsMatchingType (switchPort->GetAddress ()))
163 NS_FATAL_ERROR (
"Device does not support eui 48 addresses: cannot be added to switch.");
165 if (!switchPort->SupportsSendFrom ())
167 NS_FATAL_ERROR (
"Device does not support SendFrom: cannot be added to switch.");
169 if (m_address == Mac48Address ())
171 m_address = Mac48Address::ConvertFrom (switchPort->GetAddress ());
174 if (m_ports.size () < DP_MAX_PORTS)
178 p.netdev = switchPort;
179 m_ports.push_back (p);
182 SendPortStatus (p, OFPPR_ADD);
184 NS_LOG_DEBUG (
"RegisterProtocolHandler for " << switchPort->GetInstanceTypeId ().GetName ());
185 m_node->RegisterProtocolHandler (
MakeCallback (&OpenFlowSwitchNetDevice::ReceiveFromDevice,
this),
186 0, switchPort,
true);
187 m_channel->AddChannel (switchPort->GetChannel ());
198 OpenFlowSwitchNetDevice::SetIfIndex (
const uint32_t index)
205 OpenFlowSwitchNetDevice::GetIfIndex (
void)
const
212 OpenFlowSwitchNetDevice::GetChannel (
void)
const
219 OpenFlowSwitchNetDevice::SetAddress (Address
address)
222 m_address = Mac48Address::ConvertFrom (address);
226 OpenFlowSwitchNetDevice::GetAddress (
void)
const
233 OpenFlowSwitchNetDevice::SetMtu (
const uint16_t mtu)
241 OpenFlowSwitchNetDevice::GetMtu (
void)
const
249 OpenFlowSwitchNetDevice::IsLinkUp (
void)
const
257 OpenFlowSwitchNetDevice::AddLinkChangeCallback (Callback<void> callback)
262 OpenFlowSwitchNetDevice::IsBroadcast (
void)
const
269 OpenFlowSwitchNetDevice::GetBroadcast (
void)
const
272 return Mac48Address (
"ff:ff:ff:ff:ff:ff");
283 OpenFlowSwitchNetDevice::GetMulticast (Ipv4Address multicastGroup)
const
286 Mac48Address multicast = Mac48Address::GetMulticast (multicastGroup);
292 OpenFlowSwitchNetDevice::IsPointToPoint (
void)
const
299 OpenFlowSwitchNetDevice::IsBridge (
void)
const
306 OpenFlowSwitchNetDevice::DoOutput (uint32_t packet_uid,
int in_port,
size_t max_len,
int out_port,
bool ignore_no_fwd)
308 if (out_port != OFPP_CONTROLLER)
310 OutputPort (packet_uid, in_port, out_port, ignore_no_fwd);
314 OutputControl (packet_uid, in_port, max_len, OFPR_ACTION);
319 OpenFlowSwitchNetDevice::Send (Ptr<Packet> packet,
const Address& dest, uint16_t protocolNumber)
322 return SendFrom (packet, m_address, dest, protocolNumber);
326 OpenFlowSwitchNetDevice::SendFrom (Ptr<Packet> packet,
const Address& src,
const Address& dest, uint16_t protocolNumber)
330 ofpbuf *buffer = BufferFromPacket (packet,src,dest,GetMtu (),protocolNumber);
333 ofi::SwitchPacketMetadata
data;
334 data.packet = packet;
335 data.buffer = buffer;
336 data.protocolNumber = protocolNumber;
337 data.src = Address (src);
338 data.dst = Address (dest);
339 m_packetData.insert (std::make_pair (packet_uid, data));
341 RunThroughFlowTable (packet_uid, -1);
348 OpenFlowSwitchNetDevice::GetNode (
void)
const
355 OpenFlowSwitchNetDevice::SetNode (Ptr<Node> node)
362 OpenFlowSwitchNetDevice::NeedsArp (
void)
const
369 OpenFlowSwitchNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
376 OpenFlowSwitchNetDevice::SetPromiscReceiveCallback (NetDevice::PromiscReceiveCallback cb)
379 m_promiscRxCallback = cb;
383 OpenFlowSwitchNetDevice::SupportsSendFrom ()
const
390 OpenFlowSwitchNetDevice::GetMulticast (Ipv6Address addr)
const
393 return Mac48Address::GetMulticast (addr);
398 OpenFlowSwitchNetDevice::AddVPort (
const ofp_vport_mod *ovpm)
400 size_t actions_len = ntohs (ovpm->header.length) -
sizeof *ovpm;
401 unsigned int vport = ntohl (ovpm->vport);
402 unsigned int parent_port = ntohl (ovpm->parent_port);
405 vport_table_entry *vpe = vport_table_lookup (&m_vportTable, vport);
409 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
415 if (v_code != ACT_VALIDATION_OK)
417 SendErrorMsg (OFPET_BAD_ACTION, v_code, ovpm, ntohs (ovpm->header.length));
421 vpe = vport_table_entry_alloc (actions_len);
424 vpe->parent_port = parent_port;
425 if (vport < OFPP_VP_START || vport > OFPP_VP_END)
427 NS_LOG_ERROR (
"port " << vport <<
" is not in the virtual port range (" << OFPP_VP_START <<
"-" << OFPP_VP_END <<
")");
428 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
429 free_vport_table_entry (vpe);
433 vpe->port_acts->actions_len = actions_len;
434 memcpy (vpe->port_acts->actions, ovpm->actions, actions_len);
436 int error = insert_vport_table_entry (&m_vportTable, vpe);
439 NS_LOG_ERROR (
"could not insert port table entry for port " << vport);
446 OpenFlowSwitchNetDevice::BufferFromPacket (Ptr<Packet> packet, Address src, Address dst,
int mtu, uint16_t protocol)
448 NS_LOG_INFO (
"Creating Openflow buffer from packet.");
455 const int headroom = 128 + 2;
456 const int hard_header = VLAN_ETH_HEADER_LEN;
457 ofpbuf *buffer = ofpbuf_new (headroom + hard_header + mtu);
458 buffer->data = (
char*)buffer->data + headroom + hard_header;
460 int l2_length = 0, l3_length = 0, l4_length = 0;
463 EthernetHeader eth_hd;
464 if (packet->PeekHeader (eth_hd))
466 buffer->l2 =
new eth_header;
467 eth_header* eth_h = (eth_header*)buffer->l2;
468 dst.CopyTo (eth_h->eth_dst);
469 src.CopyTo (eth_h->eth_src);
470 eth_h->eth_type = htons (ETH_TYPE_IP);
473 l2_length = ETH_HEADER_LEN;
477 if (protocol == Ipv4L3Protocol::PROT_NUMBER)
480 if (packet->PeekHeader (ip_hd))
482 buffer->l3 =
new ip_header;
483 ip_header* ip_h = (ip_header*)buffer->l3;
484 ip_h->ip_ihl_ver = IP_IHL_VER (5, IP_VERSION);
485 ip_h->ip_tos = ip_hd.GetTos ();
486 ip_h->ip_tot_len = packet->GetSize ();
487 ip_h->ip_id = ip_hd.GetIdentification ();
488 ip_h->ip_frag_off = ip_hd.GetFragmentOffset ();
489 ip_h->ip_ttl = ip_hd.GetTtl ();
490 ip_h->ip_proto = ip_hd.GetProtocol ();
491 ip_h->ip_src = htonl (ip_hd.GetSource ().Get ());
492 ip_h->ip_dst = htonl (ip_hd.GetDestination ().Get ());
493 ip_h->ip_csum = csum (&ip_h,
sizeof ip_h);
496 l3_length = IP_HEADER_LEN;
503 if (packet->PeekHeader (arp_hd))
505 buffer->l3 =
new arp_eth_header;
506 arp_eth_header* arp_h = (arp_eth_header*)buffer->l3;
507 arp_h->ar_hrd = ARP_HRD_ETHERNET;
508 arp_h->ar_pro = ARP_PRO_IP;
509 arp_h->ar_op = arp_hd.m_type;
510 arp_hd.GetDestinationHardwareAddress ().CopyTo (arp_h->ar_tha);
511 arp_hd.GetSourceHardwareAddress ().CopyTo (arp_h->ar_sha);
512 arp_h->ar_tpa = arp_hd.GetDestinationIpv4Address ().Get ();
513 arp_h->ar_spa = arp_hd.GetSourceIpv4Address ().Get ();
514 arp_h->ar_hln =
sizeof arp_h->ar_tha;
515 arp_h->ar_pln =
sizeof arp_h->ar_tpa;
518 l3_length = ARP_ETH_HEADER_LEN;
522 if (protocol == Ipv4L3Protocol::PROT_NUMBER)
524 ip_header* ip_h = (ip_header*)buffer->l3;
525 if (ip_h->ip_proto == TcpL4Protocol::PROT_NUMBER)
528 if (packet->PeekHeader (tcp_hd))
530 buffer->l4 =
new tcp_header;
531 tcp_header* tcp_h = (tcp_header*)buffer->l4;
532 tcp_h->tcp_src = htons (tcp_hd.GetSourcePort ());
533 tcp_h->tcp_dst = htons (tcp_hd.GetDestinationPort ());
534 tcp_h->tcp_seq = tcp_hd.GetSequenceNumber ().GetValue ();
535 tcp_h->tcp_ack = tcp_hd.GetAckNumber ().GetValue ();
536 tcp_h->tcp_ctl = TCP_FLAGS (tcp_hd.GetFlags ());
537 tcp_h->tcp_winsz = tcp_hd.GetWindowSize ();
538 tcp_h->tcp_urg = tcp_hd.GetUrgentPointer ();
539 tcp_h->tcp_csum = csum (&tcp_h,
sizeof tcp_h);
542 l4_length = TCP_HEADER_LEN;
545 else if (ip_h->ip_proto == UdpL4Protocol::PROT_NUMBER)
548 if (packet->PeekHeader (udp_hd))
550 buffer->l4 =
new udp_header;
551 udp_header* udp_h = (udp_header*)buffer->l4;
552 udp_h->udp_src = htons (udp_hd.GetSourcePort ());
553 udp_h->udp_dst = htons (udp_hd.GetDestinationPort ());
554 udp_h->udp_len = htons (UDP_HEADER_LEN + packet->GetSize ());
556 ip_header* ip_h = (ip_header*)buffer->l3;
557 uint32_t udp_csum = csum_add32 (0, ip_h->ip_src);
558 udp_csum = csum_add32 (udp_csum, ip_h->ip_dst);
559 udp_csum = csum_add16 (udp_csum, IP_TYPE_UDP << 8);
560 udp_csum = csum_add16 (udp_csum, udp_h->udp_len);
561 udp_csum = csum_continue (udp_csum, udp_h,
sizeof udp_h);
562 udp_h->udp_csum = csum_finish (csum_continue (udp_csum, buffer->data, buffer->size));
565 l4_length = UDP_HEADER_LEN;
571 packet->CopyData ((uint8_t*)buffer->data, packet->GetSize ());
575 ofpbuf_push (buffer, buffer->l4, l4_length);
576 delete (tcp_header*)buffer->l4;
580 ofpbuf_push (buffer, buffer->l3, l3_length);
581 delete (ip_header*)buffer->l3;
585 ofpbuf_push (buffer, buffer->l2, l2_length);
586 delete (eth_header*)buffer->l2;
593 OpenFlowSwitchNetDevice::ReceiveFromDevice (Ptr<NetDevice> netdev, Ptr<const Packet> packet, uint16_t protocol,
594 const Address& src,
const Address& dst, PacketType packetType)
597 NS_LOG_INFO (
"--------------------------------------------");
600 if (!m_promiscRxCallback.IsNull ())
602 m_promiscRxCallback (
this, packet, protocol, src, dst, packetType);
605 Mac48Address dst48 = Mac48Address::ConvertFrom (dst);
606 NS_LOG_INFO (
"Received packet from " << Mac48Address::ConvertFrom (src) <<
" looking for " << dst48);
608 for (
size_t i = 0; i < m_ports.size (); i++)
610 if (m_ports[i].netdev == netdev)
612 if (packetType == PACKET_HOST && dst48 == m_address)
614 m_rxCallback (
this, packet, protocol, src);
616 else if (packetType == PACKET_BROADCAST || packetType == PACKET_MULTICAST || packetType == PACKET_OTHERHOST)
618 if (packetType == PACKET_OTHERHOST && dst48 == m_address)
620 m_rxCallback (
this, packet, protocol, src);
624 if (packetType != PACKET_OTHERHOST)
626 m_rxCallback (
this, packet, protocol, src);
629 ofi::SwitchPacketMetadata
data;
630 data.packet = packet->Copy ();
632 ofpbuf *buffer = BufferFromPacket (data.packet,src,dst,netdev->GetMtu (),protocol);
633 m_ports[i].rx_packets++;
634 m_ports[i].rx_bytes += buffer->size;
635 data.buffer = buffer;
638 data.protocolNumber = protocol;
639 data.src = Address (src);
640 data.dst = Address (dst);
641 m_packetData.insert (std::make_pair (packet_uid, data));
643 RunThroughFlowTable (packet_uid, i);
653 if (now >=
Seconds (m_lastExecute.GetSeconds () + 1))
656 for (
size_t i = 0; i < m_ports.size (); i++)
658 if (UpdatePortStatus (m_ports[i]))
660 SendPortStatus (m_ports[i], OFPPR_MODIFY);
665 List deleted = LIST_INITIALIZER (&deleted);
667 chain_timeout (m_chain, &deleted);
668 LIST_FOR_EACH_SAFE (f, n, sw_flow, node, &deleted)
670 std::ostringstream str;
672 for (
int i = 0; i < 6; i++)
673 str << (i!=0 ?
":" :
"") << std::hex << f->key.flow.dl_src[i]/16 << f->key.flow.dl_src[i]%16;
675 for (
int i = 0; i < 6; i++)
676 str << (i!=0 ?
":" :
"") << std::hex << f->key.flow.dl_dst[i]/16 << f->key.flow.dl_dst[i]%16;
680 SendFlowExpired (f, (ofp_flow_expired_reason)f->reason);
681 list_remove (&f->node);
690 OpenFlowSwitchNetDevice::OutputAll (uint32_t packet_uid,
int in_port,
bool flood)
696 for (
size_t i = 0; i < m_ports.size (); i++)
698 if (i == (
unsigned)in_port)
702 if (flood && m_ports[i].config & OFPPC_NO_FLOOD)
708 OutputPort (packet_uid, in_port, prev_port,
false);
714 OutputPort (packet_uid, in_port, prev_port,
false);
721 OpenFlowSwitchNetDevice::OutputPacket (uint32_t packet_uid,
int out_port)
723 if (out_port >= 0 && out_port < DP_MAX_PORTS)
725 ofi::Port& p = m_ports[out_port];
726 if (p.netdev != 0 && !(p.config & OFPPC_PORT_DOWN))
728 ofi::SwitchPacketMetadata data = m_packetData.find (packet_uid)->second;
729 size_t bufsize = data.buffer->size;
730 NS_LOG_INFO (
"Sending packet " << data.packet->GetUid () <<
" over port " << out_port);
731 if (p.netdev->SendFrom (data.packet->Copy (), data.src, data.dst, data.protocolNumber))
734 p.tx_bytes += bufsize;
744 NS_LOG_DEBUG (
"can't forward to bad port " << out_port);
748 OpenFlowSwitchNetDevice::OutputPort (uint32_t packet_uid,
int in_port,
int out_port,
bool ignore_no_fwd)
752 if (out_port == OFPP_FLOOD)
754 OutputAll (packet_uid, in_port,
true);
756 else if (out_port == OFPP_ALL)
758 OutputAll (packet_uid, in_port,
false);
760 else if (out_port == OFPP_CONTROLLER)
762 OutputControl (packet_uid, in_port, 0, OFPR_ACTION);
764 else if (out_port == OFPP_IN_PORT)
766 OutputPacket (packet_uid, in_port);
768 else if (out_port == OFPP_TABLE)
770 RunThroughFlowTable (packet_uid, in_port < DP_MAX_PORTS ? in_port : -1,
false);
772 else if (out_port >= OFPP_VP_START && out_port <= OFPP_VP_END)
775 NS_LOG_INFO (
"packet sent to virtual port " << out_port);
776 if (in_port < DP_MAX_PORTS)
778 RunThroughVPortTable (packet_uid, in_port, out_port);
782 RunThroughVPortTable (packet_uid, -1, out_port);
785 else if (in_port == out_port)
791 OutputPacket (packet_uid, out_port);
796 OpenFlowSwitchNetDevice::MakeOpenflowReply (
size_t openflow_len, uint8_t type, ofpbuf **bufferp)
798 return make_openflow_xid (openflow_len, type, 0, bufferp);
802 OpenFlowSwitchNetDevice::SendOpenflowBuffer (ofpbuf *buffer)
804 if (m_controller != 0)
806 update_openflow_length (buffer);
807 m_controller->ReceiveFromSwitch (
this, buffer);
814 OpenFlowSwitchNetDevice::OutputControl (uint32_t packet_uid,
int in_port,
size_t max_len,
int reason)
818 ofpbuf* buffer = m_packetData.find (packet_uid)->second.buffer;
819 size_t total_len = buffer->size;
820 if (packet_uid != std::numeric_limits<uint32_t>::max () && max_len != 0 && buffer->size > max_len)
822 buffer->size = max_len;
825 ofp_packet_in *opi = (ofp_packet_in*)ofpbuf_push_uninit (buffer, offsetof (ofp_packet_in, data));
826 opi->header.version = OFP_VERSION;
827 opi->header.type = OFPT_PACKET_IN;
828 opi->header.length = htons (buffer->size);
829 opi->header.xid = htonl (0);
830 opi->buffer_id = htonl (packet_uid);
831 opi->total_len = htons (total_len);
832 opi->in_port = htons (in_port);
833 opi->reason = reason;
835 SendOpenflowBuffer (buffer);
839 OpenFlowSwitchNetDevice::FillPortDesc (ofi::Port p, ofp_phy_port *desc)
841 desc->port_no = htons (GetSwitchPortIndex (p));
843 std::ostringstream nm;
844 nm <<
"eth" << GetSwitchPortIndex (p);
845 strncpy ((
char *)desc->name, nm.str ().c_str (),
sizeof desc->name);
847 p.netdev->GetAddress ().CopyTo (desc->hw_addr);
848 desc->config = htonl (p.config);
849 desc->state = htonl (p.state);
854 desc->advertised = 0;
859 OpenFlowSwitchNetDevice::SendFeaturesReply ()
862 ofp_switch_features *ofr = (ofp_switch_features*)MakeOpenflowReply (
sizeof *ofr, OFPT_FEATURES_REPLY, &buffer);
863 ofr->datapath_id = htonll (m_id);
864 ofr->n_tables = m_chain->n_tables;
869 for (
size_t i = 0; i < m_ports.size (); i++)
871 ofp_phy_port* opp = (ofp_phy_port*)ofpbuf_put_zeros (buffer,
sizeof *opp);
872 FillPortDesc (m_ports[i], opp);
875 SendOpenflowBuffer (buffer);
879 OpenFlowSwitchNetDevice::SendVPortTableFeatures ()
882 ofp_vport_table_features *ovtfr = (ofp_vport_table_features*)MakeOpenflowReply (
sizeof *ovtfr, OFPT_VPORT_TABLE_FEATURES_REPLY, &buffer);
884 ovtfr->max_vports = htonl (m_vportTable.max_vports);
885 ovtfr->max_chain_depth = htons (-1);
886 ovtfr->mixed_chaining =
true;
887 SendOpenflowBuffer (buffer);
891 OpenFlowSwitchNetDevice::UpdatePortStatus (ofi::Port& p)
893 uint32_t orig_config = p.config;
894 uint32_t orig_state = p.state;
897 p.config &= ~OFPPC_PORT_DOWN;
899 if (p.netdev->IsLinkUp ())
901 p.state &= ~OFPPS_LINK_DOWN;
905 p.state |= OFPPS_LINK_DOWN;
908 return ((orig_config != p.config) || (orig_state != p.state));
912 OpenFlowSwitchNetDevice::SendPortStatus (ofi::Port p, uint8_t status)
915 ofp_port_status *ops = (ofp_port_status*)MakeOpenflowReply (
sizeof *ops, OFPT_PORT_STATUS, &buffer);
916 ops->reason = status;
917 memset (ops->pad, 0,
sizeof ops->pad);
918 FillPortDesc (p, &ops->desc);
920 SendOpenflowBuffer (buffer);
921 ofpbuf_delete (buffer);
925 OpenFlowSwitchNetDevice::SendFlowExpired (sw_flow *flow,
enum ofp_flow_expired_reason reason)
928 ofp_flow_expired *ofe = (ofp_flow_expired*)MakeOpenflowReply (
sizeof *ofe, OFPT_FLOW_EXPIRED, &buffer);
929 flow_fill_match (&ofe->match, &flow->key);
931 ofe->priority = htons (flow->priority);
932 ofe->reason = reason;
933 memset (ofe->pad, 0,
sizeof ofe->pad);
935 ofe->duration = htonl (time_now () - flow->created);
936 memset (ofe->pad2, 0,
sizeof ofe->pad2);
937 ofe->packet_count = htonll (flow->packet_count);
938 ofe->byte_count = htonll (flow->byte_count);
939 SendOpenflowBuffer (buffer);
943 OpenFlowSwitchNetDevice::SendErrorMsg (uint16_t type, uint16_t code,
const void *data,
size_t len)
946 ofp_error_msg *oem = (ofp_error_msg*)MakeOpenflowReply (
sizeof(*oem) + len, OFPT_ERROR, &buffer);
947 oem->type = htons (type);
948 oem->code = htons (code);
949 memcpy (oem->data, data, len);
950 SendOpenflowBuffer (buffer);
954 OpenFlowSwitchNetDevice::FlowTableLookup (sw_flow_key key, ofpbuf* buffer, uint32_t packet_uid,
int port,
bool send_to_controller)
956 sw_flow *flow = chain_lookup (m_chain, &key);
960 flow_used (flow, buffer);
961 ofi::ExecuteActions (
this, packet_uid, buffer, &key, flow->sf_acts->actions, flow->sf_acts->actions_len,
false);
967 if (send_to_controller)
969 OutputControl (packet_uid, port, m_missSendLen, OFPR_NO_MATCH);
974 m_packetData.erase (packet_uid);
976 ofpbuf_delete (buffer);
980 OpenFlowSwitchNetDevice::RunThroughFlowTable (uint32_t packet_uid,
int port,
bool send_to_controller)
982 ofi::SwitchPacketMetadata data = m_packetData.find (packet_uid)->second;
983 ofpbuf* buffer = data.buffer;
988 if (flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow) && (m_flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP)
990 ofpbuf_delete (buffer);
998 mpls_h.value = ntohl (*((uint32_t*)buffer->l2_5));
1004 m_ports[
port].mpls_ttl0_dropped++;
1013 uint32_t config = m_ports[
port].config;
1014 if (config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP)
1015 && config & (!eth_addr_equals (key.flow.dl_dst, stp_eth_addr) ? OFPPC_NO_RECV : OFPPC_NO_RECV_STP))
1022 Simulator::Schedule (m_lookupDelay, &OpenFlowSwitchNetDevice::FlowTableLookup,
this, key, buffer, packet_uid, port, send_to_controller);
1026 OpenFlowSwitchNetDevice::RunThroughVPortTable (uint32_t packet_uid,
int port, uint32_t vport)
1028 ofpbuf* buffer = m_packetData.find (packet_uid)->second.buffer;
1034 if (flow_extract (buffer, port != -1 ? port : OFPP_NONE, &key.flow)
1035 && (m_flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP)
1041 vport_table_entry *vpe = vport_table_lookup (&m_vportTable, vport);
1042 m_vportTable.lookup_count++;
1045 m_vportTable.port_match_count++;
1049 ofi::ExecuteVPortActions (
this, packet_uid, m_packetData.find (packet_uid)->second.buffer, &key, vpe->port_acts->actions, vpe->port_acts->actions_len);
1050 vport_used (vpe, buffer);
1051 if (vpe->parent_port_ptr == 0)
1055 if (vpe->parent_port <= OFPP_VP_START)
1057 OutputPort (packet_uid, port != -1 ? port : OFPP_NONE, vpe->parent_port,
false);
1066 m_vportTable.chain_match_count++;
1069 vpe = vpe->parent_port_ptr;
1076 OpenFlowSwitchNetDevice::ReceiveFeaturesRequest (
const void *msg)
1078 SendFeaturesReply ();
1083 OpenFlowSwitchNetDevice::ReceiveVPortTableFeaturesRequest (
const void *msg)
1085 SendVPortTableFeatures ();
1090 OpenFlowSwitchNetDevice::ReceiveGetConfigRequest (
const void *msg)
1093 ofp_switch_config *osc = (ofp_switch_config*)MakeOpenflowReply (
sizeof *osc, OFPT_GET_CONFIG_REPLY, &buffer);
1094 osc->flags = htons (m_flags);
1095 osc->miss_send_len = htons (m_missSendLen);
1097 return SendOpenflowBuffer (buffer);
1101 OpenFlowSwitchNetDevice::ReceiveSetConfig (
const void *msg)
1103 const ofp_switch_config *osc = (ofp_switch_config*)msg;
1105 int n_flags = ntohs (osc->flags) & (OFPC_SEND_FLOW_EXP | OFPC_FRAG_MASK);
1106 if ((n_flags & OFPC_FRAG_MASK) != OFPC_FRAG_NORMAL && (n_flags & OFPC_FRAG_MASK) != OFPC_FRAG_DROP)
1108 n_flags = (n_flags & ~OFPC_FRAG_MASK) | OFPC_FRAG_DROP;
1112 m_missSendLen = ntohs (osc->miss_send_len);
1117 OpenFlowSwitchNetDevice::ReceivePacketOut (
const void *msg)
1119 const ofp_packet_out *opo = (ofp_packet_out*)msg;
1121 size_t actions_len = ntohs (opo->actions_len);
1123 if (actions_len > (ntohs (opo->header.length) -
sizeof *opo))
1125 NS_LOG_DEBUG (
"message too short for number of actions");
1129 if (ntohl (opo->buffer_id) == (uint32_t) -1)
1132 int data_len = ntohs (opo->header.length) -
sizeof *opo - actions_len;
1133 buffer = ofpbuf_new (data_len);
1134 ofpbuf_put (buffer, (uint8_t *)opo->actions + actions_len, data_len);
1146 flow_extract (buffer, opo->in_port, &key.flow);
1149 if (v_code != ACT_VALIDATION_OK)
1151 SendErrorMsg (OFPET_BAD_ACTION, v_code, msg, ntohs (opo->header.length));
1152 ofpbuf_delete (buffer);
1156 ofi::ExecuteActions (
this, opo->buffer_id, buffer, &key, opo->actions, actions_len,
true);
1161 OpenFlowSwitchNetDevice::ReceivePortMod (
const void *msg)
1163 ofp_port_mod* opm = (ofp_port_mod*)msg;
1165 int port = opm->port_no;
1166 if (port < DP_MAX_PORTS)
1168 ofi::Port& p = m_ports[
port];
1171 Mac48Address hw_addr = Mac48Address ();
1172 hw_addr.CopyFrom (opm->hw_addr);
1173 if (p.netdev->GetAddress () != hw_addr)
1180 uint32_t config_mask = ntohl (opm->mask);
1181 p.config &= ~config_mask;
1182 p.config |= ntohl (opm->config) & config_mask;
1185 if (opm->mask & htonl (OFPPC_PORT_DOWN))
1187 if ((opm->config & htonl (OFPPC_PORT_DOWN)) && (p.config & OFPPC_PORT_DOWN) == 0)
1189 p.config |= OFPPC_PORT_DOWN;
1192 else if ((opm->config & htonl (OFPPC_PORT_DOWN)) == 0 && (p.config & OFPPC_PORT_DOWN))
1194 p.config &= ~OFPPC_PORT_DOWN;
1205 OpenFlowSwitchNetDevice::ReceiveVPortMod (
const void *msg)
1207 const ofp_vport_mod *ovpm = (ofp_vport_mod*)msg;
1209 uint16_t command = ntohs (ovpm->command);
1210 if (command == OFPVP_ADD)
1212 return AddVPort (ovpm);
1214 else if (command == OFPVP_DELETE)
1216 if (remove_vport_table_entry (&m_vportTable, ntohl (ovpm->vport)))
1218 SendErrorMsg (OFPET_BAD_ACTION, OFPET_VPORT_MOD_FAILED, ovpm, ntohs (ovpm->header.length));
1226 OpenFlowSwitchNetDevice::AddFlow (
const ofp_flow_mod *ofm)
1228 size_t actions_len = ntohs (ofm->header.length) -
sizeof *ofm;
1231 sw_flow *flow = flow_alloc (actions_len);
1234 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1241 flow_extract_match (&flow->key, &ofm->match);
1244 if (v_code != ACT_VALIDATION_OK)
1246 SendErrorMsg (OFPET_BAD_ACTION, v_code, ofm, ntohs (ofm->header.length));
1248 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1256 flow->priority = flow->key.wildcards ? ntohs (ofm->priority) : -1;
1257 flow->idle_timeout = ntohs (ofm->idle_timeout);
1258 flow->hard_timeout = ntohs (ofm->hard_timeout);
1259 flow->used = flow->created = time_now ();
1260 flow->sf_acts->actions_len = actions_len;
1261 flow->byte_count = 0;
1262 flow->packet_count = 0;
1263 memcpy (flow->sf_acts->actions, ofm->actions, actions_len);
1266 int error = chain_insert (m_chain, flow);
1269 if (error == -ENOBUFS)
1271 SendErrorMsg (OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL, ofm, ntohs (ofm->header.length));
1274 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1282 if (ntohl (ofm->buffer_id) != std::numeric_limits<uint32_t>::max ())
1288 flow_used (flow, buffer);
1289 flow_extract (buffer, ofm->match.in_port, &key.flow);
1290 ofi::ExecuteActions (
this, ofm->buffer_id, buffer, &key, ofm->actions, actions_len,
false);
1291 ofpbuf_delete (buffer);
1302 OpenFlowSwitchNetDevice::ModFlow (
const ofp_flow_mod *ofm)
1305 flow_extract_match (&key, &ofm->match);
1307 size_t actions_len = ntohs (ofm->header.length) -
sizeof *ofm;
1310 if (v_code != ACT_VALIDATION_OK)
1312 SendErrorMsg ((ofp_error_type)OFPET_BAD_ACTION, v_code, ofm, ntohs (ofm->header.length));
1313 if (ntohl (ofm->buffer_id) != (uint32_t) -1)
1320 uint16_t priority = key.wildcards ? ntohs (ofm->priority) : -1;
1321 int strict = (ofm->command == htons (OFPFC_MODIFY_STRICT)) ? 1 : 0;
1322 chain_modify (m_chain, &key, priority, strict, ofm->actions, actions_len);
1324 if (ntohl (ofm->buffer_id) != std::numeric_limits<uint32_t>::max ())
1329 sw_flow_key skb_key;
1330 flow_extract (buffer, ofm->match.in_port, &skb_key.flow);
1331 ofi::ExecuteActions (
this, ofm->buffer_id, buffer, &skb_key, ofm->actions, actions_len,
false);
1332 ofpbuf_delete (buffer);
1343 OpenFlowSwitchNetDevice::ReceiveFlow (
const void *msg)
1346 const ofp_flow_mod *ofm = (ofp_flow_mod*)msg;
1347 uint16_t command = ntohs (ofm->command);
1349 if (command == OFPFC_ADD)
1351 return AddFlow (ofm);
1353 else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT))
1355 return ModFlow (ofm);
1357 else if (command == OFPFC_DELETE)
1360 flow_extract_match (&key, &ofm->match);
1361 return chain_delete (m_chain, &key, ofm->out_port, 0, 0) ? 0 : -ESRCH;
1363 else if (command == OFPFC_DELETE_STRICT)
1367 flow_extract_match (&key, &ofm->match);
1368 priority = key.wildcards ? ntohs (ofm->priority) : -1;
1369 return chain_delete (m_chain, &key, ofm->out_port, priority, 1) ? 0 : -ESRCH;
1378 OpenFlowSwitchNetDevice::StatsDump (ofi::StatsDumpCallback *cb)
1380 ofp_stats_reply *osr;
1389 osr = (ofp_stats_reply*)MakeOpenflowReply (
sizeof *osr, OFPT_STATS_REPLY, &buffer);
1390 osr->type = htons (cb->s->type);
1393 err = cb->s->DoDump (
this, cb->state, buffer);
1403 osr = (ofp_stats_reply*)ofpbuf_at_assert (buffer, 0,
sizeof *osr);
1404 osr->flags = ntohs (OFPSF_REPLY_MORE);
1407 int err2 = SendOpenflowBuffer (buffer);
1418 OpenFlowSwitchNetDevice::StatsDone (ofi::StatsDumpCallback *cb)
1422 cb->s->DoCleanup (cb->state);
1429 OpenFlowSwitchNetDevice::ReceiveStatsRequest (
const void *oh)
1431 const ofp_stats_request *rq = (ofp_stats_request*)oh;
1432 size_t rq_len = ntohs (rq->header.length);
1433 int type = ntohs (rq->type);
1434 int body_len = rq_len - offsetof (ofp_stats_request, body);
1435 ofi::Stats* st =
new ofi::Stats ((ofp_stats_types)type, (
unsigned)body_len);
1442 ofi::StatsDumpCallback cb;
1444 cb.rq = (ofp_stats_request*)xmemdup (rq, rq_len);
1451 int err = cb.s->DoInit (rq->body, body_len, &cb.state);
1454 NS_LOG_WARN (
"failed initialization of stats request type " << type <<
": " << strerror (-err));
1460 if (m_controller != 0)
1462 m_controller->StartDump (&cb);
1466 NS_LOG_ERROR (
"Switch needs to be registered to a controller in order to start the stats reply.");
1473 OpenFlowSwitchNetDevice::ReceiveEchoRequest (
const void *oh)
1475 return SendOpenflowBuffer (make_echo_reply ((ofp_header*)oh));
1479 OpenFlowSwitchNetDevice::ReceiveEchoReply (
const void *oh)
1485 OpenFlowSwitchNetDevice::ForwardControlInput (
const void *msg,
size_t length)
1488 ofp_header *oh = (ofp_header*) msg;
1489 if (ntohs (oh->length) > length)
1493 assert (oh->version == OFP_VERSION);
1500 case OFPT_FEATURES_REQUEST:
1501 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveFeaturesRequest (msg);
1503 case OFPT_GET_CONFIG_REQUEST:
1504 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveGetConfigRequest (msg);
1506 case OFPT_SET_CONFIG:
1507 error = length <
sizeof(ofp_switch_config) ? -EFAULT : ReceiveSetConfig (msg);
1509 case OFPT_PACKET_OUT:
1510 error = length <
sizeof(ofp_packet_out) ? -EFAULT : ReceivePacketOut (msg);
1513 error = length <
sizeof(ofp_flow_mod) ? -EFAULT : ReceiveFlow (msg);
1516 error = length <
sizeof(ofp_port_mod) ? -EFAULT : ReceivePortMod (msg);
1518 case OFPT_STATS_REQUEST:
1519 error = length <
sizeof(ofp_stats_request) ? -EFAULT : ReceiveStatsRequest (msg);
1521 case OFPT_ECHO_REQUEST:
1522 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveEchoRequest (msg);
1524 case OFPT_ECHO_REPLY:
1525 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveEchoReply (msg);
1527 case OFPT_VPORT_MOD:
1528 error = length <
sizeof(ofp_vport_mod) ? -EFAULT : ReceiveVPortMod (msg);
1530 case OFPT_VPORT_TABLE_FEATURES_REQUEST:
1531 error = length <
sizeof(ofp_header) ? -EFAULT : ReceiveVPortTableFeaturesRequest (msg);
1534 SendErrorMsg ((ofp_error_type)OFPET_BAD_REQUEST, (ofp_bad_request_code)OFPBRC_BAD_TYPE, msg, length);
1540 free ((ofpbuf*)msg);
1546 OpenFlowSwitchNetDevice::GetChain ()
1552 OpenFlowSwitchNetDevice::GetNSwitchPorts (
void)
const
1555 return m_ports.size ();
1559 OpenFlowSwitchNetDevice::GetSwitchPort (uint32_t n)
const
1566 OpenFlowSwitchNetDevice::GetSwitchPortIndex (ofi::Port p)
1568 for (
size_t i = 0; i < m_ports.size (); i++)
1570 if (m_ports[i].netdev == p.netdev)
1579 OpenFlowSwitchNetDevice::GetVPortTable ()
1581 return m_vportTable;
1586 #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 "...
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()
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
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].