11 #include <arpa/inet.h> 15 #include "ns3/simulator.h" 19 NS_LOG_COMPONENT_DEFINE(
"BgpRouting");
20 NS_OBJECT_ENSURE_REGISTERED(BgpRouting);
22 BgpRouting::BgpRouting() {
27 TypeId BgpRouting::GetTypeId (
void) {
28 static TypeId tid = TypeId (
"ns3::BgpRouting")
29 .SetParent<Ipv4RoutingProtocol>()
30 .SetGroupName (
"Internet")
31 .AddConstructor<BgpRouting>();
36 Ptr<Ipv4Route> BgpRouting::RouteOutput (Ptr<Packet> p,
const Ipv4Header &header,
37 Ptr<NetDevice> oif, Socket::SocketErrno &sockerr) {
38 NS_ASSERT(_ipv4 !=
nullptr);
39 Ipv4Address dst = header.GetDestination();
41 if (dst.IsMulticast()) {
42 NS_LOG_INFO(
"multicast destination " << dst <<
" not supported by BgpRouting.");
46 NS_LOG_DEBUG(
"looking for destination " << dst <<
" in rib.");
50 if (rslt ==
nullptr) {
51 NS_LOG_INFO(
"no matching entry in rib for destination " << dst <<
".");
56 Ipv4Address gateway (nexthop);
62 NS_LOG_WARN(
"no device found for nexthop " << gateway <<
", and not bonded.");
66 Ptr<Ipv4Route> route = Create<Ipv4Route>();
67 route->SetDestination(dst);
68 route->SetGateway(gateway);
69 route->SetOutputDevice(dev);
71 Ipv4Address source = header.GetSource();
72 if (source.IsAny() || source == Ipv4Address(0x66666666)) source = _ipv4->GetAddress(_ipv4->GetInterfaceForDevice(dev), 0).GetLocal();
73 route->SetSource(source);
78 bool BgpRouting::RouteInput (Ptr<const Packet> p,
const Ipv4Header &header, Ptr<const NetDevice> idev,
79 UnicastForwardCallback ucb, MulticastForwardCallback mcb, LocalDeliverCallback lcb,
81 NS_ASSERT(_ipv4 !=
nullptr);
82 NS_ASSERT(_ipv4->GetInterfaceForDevice (idev) >= 0);
84 Ipv4Address dst = header.GetDestination();
85 uint32_t iface = _ipv4->GetInterfaceForDevice(idev);
88 if (dst.IsMulticast()) {
89 NS_LOG_LOGIC(
"ignoring multicast destination " << dst <<
" .");
94 if (_ipv4->IsDestinationAddress(dst, iface)) {
95 NS_LOG_LOGIC(
"destination " << dst <<
" is local.");
96 lcb(p, header, iface);
102 if (rslt ==
nullptr) {
103 NS_LOG_INFO(
"no matching entry in rib for destination " << dst <<
".");
108 Ipv4Address gateway (nexthop);
109 NS_LOG_LOGIC(
"nexthop of " << dst <<
" is at " << gateway <<
".");
113 if (dev ==
nullptr) {
114 NS_LOG_WARN(
"no device found for nexthop " << gateway <<
".");
118 Ptr<Ipv4Route> route = Create<Ipv4Route>();
119 route->SetDestination(dst);
120 route->SetGateway(gateway);
121 route->SetOutputDevice(dev);
122 route->SetSource(header.GetSource());
124 ucb(route, p, header);
137 uint32_t n_ifaces = _ipv4->GetNInterfaces();
139 for (uint32_t iface_id = 0; iface_id < n_ifaces; iface_id++) {
140 uint32_t n_addrs = _ipv4->GetNAddresses(iface_id);
141 for (uint32_t addr_id = 0; addr_id < n_addrs; addr_id++) {
142 Ipv4InterfaceAddress addr = _ipv4->GetAddress(iface_id, addr_id);
143 Ipv4Mask mask = addr.GetMask();
144 if (addr.GetLocal().CombineMask(mask) == nexthop.CombineMask(mask)) {
145 if (_ipv4->IsForwarding(iface_id) && _ipv4->IsUp(iface_id)) {
146 return _ipv4->GetNetDevice(iface_id);
147 }
else NS_LOG_INFO(
"interface " << iface_id <<
" has matching address but not up or not in forwarding mode.");
165 uint32_t n_ifaces = _ipv4->GetNInterfaces();
167 for (uint32_t iface_id = 0; iface_id < n_ifaces; iface_id++) {
168 uint32_t n_addrs = _ipv4->GetNAddresses(iface_id);
169 for (uint32_t addr_id = 0; addr_id < n_addrs; addr_id++) {
170 Ipv4InterfaceAddress addr = _ipv4->GetAddress(iface_id, addr_id);
171 Ipv4Mask mask = addr.GetMask();
172 if (addr.GetLocal().CombineMask(mask) == nexthop.CombineMask(mask)) {
178 return Ipv4InterfaceAddress(Ipv4Address((uint32_t) 0), Ipv4Mask((uint32_t) 0));
191 void BgpRouting::NotifyInterfaceUp (uint32_t interface) {}
192 void BgpRouting::NotifyInterfaceDown (uint32_t interface) {}
193 void BgpRouting::NotifyAddAddress (uint32_t interface, Ipv4InterfaceAddress address) {}
194 void BgpRouting::NotifyRemoveAddress (uint32_t interface, Ipv4InterfaceAddress address) {}
197 void BgpRouting::SetIpv4 (Ptr<Ipv4> ipv4) {
201 void BgpRouting::PrintRoutingTable (Ptr<OutputStreamWrapper> stream, Time::Unit unit)
const {
202 std::ostream* os = stream->GetStream();
204 *os <<
"BGP routing table for node" << _ipv4->GetObject<Node>()->GetId()
205 <<
", time: " << Simulator::Now().As(unit) << std::endl;
207 uint8_t print_buffer[4096];
209 for (
const std::pair<libbgp::BgpRib4EntryKey, libbgp::BgpRib4Entry> &entry_pair : _rib->
get()) {
212 <<
" from " << Ipv4Address(ntohl(entry.
src_router_id)) << std::endl
213 <<
"Attribues: " << std::endl;
214 for (
const std::shared_ptr<libbgp::BgpPathAttrib> &attr : entry.
attribs) {
215 attr->print(1, print_buffer, 4096);
uint32_t getPrefix() const
std::vector< std::shared_ptr< BgpPathAttrib > > attribs
Ipv4InterfaceAddress GetAddressByNexthop(const Ipv4Address &nexthop) const
Look for the interface address that can reach a given nexthop.
ns3 BGP module: routing protocol (Ipv4RoutingProtocol)
uint8_t getLength() const
void SetRib(const libbgp::BgpRib4 *rib)
Set the libbgp Routing Information Base to use.
const std::vector< BgpRib4Entry > & get() const
uint32_t getNexthop() const
Ptr< NetDevice > GetDeviceByNexthop(const Ipv4Address &nexthop) const
Look for device to output/forward packet by nexthop.
const BgpRib4Entry * lookup(uint32_t dest) const