Ns-3 is a great simulation framework. It's powerful, complex, and the documentation is good. However, we can't document everything. As a consequence, sometimes an how-to is useful.
Of course ns-3 has a how-to wiki page, but sometimes it's better to write interesting stuff also elsewhere.
Anyway, no more chitchatting. The how-to.
Suppose you have 2 nodes, each one with its NetDevice. You install IP and assign addresses. Business as usual.
Now you decide that you want to assign a specific address to a node. Not just a specific one, one extra address.
Let's say that you want this:
Node 0:
1- fe80::200:ff:fe00:1/64
2- 2001:db8::200:ff:fe00:1/64
3- 2001:1:f00d:cafe::42/64
Node 1:
1- fe80::200:ff:fe00:2/64
2- 2001:db8::200:ff:fe00:2/64
3- 2001:1:f00d:cafe::666/64
Note the SLAAC configured addresses (1 and 2) and the manually added addresses (3).
How to do that ?
Easy... sort of. It's easy once you know how.
// d is the NetDeviceContainer
Ipv6AddressHelper ipv6;
NS_LOG_INFO ("Assign IPv6 Addresses.");
Ipv6InterfaceContainer i = ipv6.Assign (d);
// arbitrary address assignment
{Ptr<NetDevice> device = d.Get (0);
Ptr<Node> node = device->GetNode ();
Ptr<Ipv6> ipv6proto = node->GetObject<Ipv6> ();
int32_t ifIndex = 0;
ifIndex = ipv6proto->GetInterfaceForDevice (device);
Ipv6InterfaceAddress ipv6Addr =
Ipv6InterfaceAddress (
Ipv6Address ("2001:1:f00d:cafe::42"), Ipv6Prefix (64));
ipv6proto->AddAddress (ifIndex, ipv6Addr);}
That's for Node 1. Node 2 is similar.
Easy? Yes.
Obvious? Nope.
What's the problem and why it's not easier?
The answer is: you need the interface index. Unfortunately, only the Ipv6 class knows the interface index for a given NetDevice, so you first have to find what is that index, then it's a matter of one instruction.
If you think that the interface is at index 1 (because it is), then you're going to have a bad time.
That's because the interface 0 is the Loopback, and you can't be totally sure that a given NetDevice will have a specific index at IP level.
You can, of course, put a static number there (e.g., if the Node just have one NetDevice, it's kinda safe to assume that the index is 1. However, this assumption will not hold anymore if you add more NetDevices.
Anyway, adding multiple IPv6 global addresses to a NetDevice is possible, and it's not too hard once you know how to do it.
BTW... let's try a little test. Why the two nodes need two global addresses? What address 2 can not do?
No comments:
Post a Comment