Adding a new Port Type
Let's say we need to add a new Port Type for a special
Xyz type of port(s).
The two main classes of interest for this activity are
PortManager discovers all the ports in the system and is responsible for instantiating an appropriate 'port' class for each of them. It uses the PCAP API
pcap_findalldevs() for discovering the ports in the system.
You need to implement a new subclass say 'XyzPort' by subclassing 'AbstractPort'. You need to modify
PortManager, so that it instantiates an object of this class for ports of type Xyz
At a high level, responsibilities of a Port class are -
- Collect Rx/Tx Stats at all times
- Transmit Packets on request
- Capture Packets on request
You will need to override all the pure virtual functions in
AbstractPort for your new
For Rx/Tx stats, just update the protected
stats_ data member periodically and that should be enough.
Drone pre-builds the Transmit Packet List before it starts transmitting. The actual packet buffer should be allocated and owned by the
AbstractPort subclass. However, the algorithm to create the packetList is implemented within
AbstractPort itself and it calls the below pure virtual functions that needs to be implemented by each subclass -
virtual void clearPacketList()
Empty the packet buffer
virtual void setPacketListLoopMode(bool loop, quint64 secDelay, quint64 nsecDelay)
true, the entire PacketList should be transmitted in a loop with secDelay:nsecDelay between repeats
virtual bool appendToPacketList(long sec, long nsec, const uchar *packet, int length)
Append the packet of the specified length to the packetList. The packet should be transmitted at the time specified by (sec,nsec). The first packet added to the packetList will have the time as (0,0) and all subsequent packets will have monotonically increasing time. In other words, the time specified is relative to the first packet (which has time 0) and not to the previous packet
There is one other related function that you need to implement -
loopNextPacketSet(). To understand what this function does, you first need to understand the packetList model used by drone.
Drone tries to minimize the number of packets for each stream in the packetList to minimize the memory required and the time required to prebuild the PacketList. For each active stream,
AbstractPort calculates the minimum number of packets required - the packetSet . If the number of packets to be transmitted is greater than this packetSet, we configure the packetSet to be repeated (or looped) multiple times. Note that the number of packets to be transmitted may not be a convenient multiple of the number of packets in the packetSet, so some packets may need to be sent after all the repetitions of the packetSet is complete.
For a stream, if p is the number of packets to be transmitted,
AbstractPort calculates the minimum number of packets required in the packetSet as x, number of repetitions of these x packets as n and any remaining packets as y
p = (n * x) + y
AbstractPort while building the packetList will call
loopNextPacketSet() with values for
x. Note that this API is called before adding the 'x' number of packets using
virtual void loopNextPacketSet(qint64 size, qint64 repeats, long repeatDelaySec, long repeatDelayNsec)
The next size number of packets added to the packetList via
appendToPacketList() should be repeated repeats number of times, with the time delay between the repeats as specified.
e.g. if you have 3 active streams, the packetList will be structured as -
((n1*x1) + y1) + ((n2*x2) + y2) + ((n3*x3) + y3)
Note that the packetList
loop is different from the packetSet
repeats. The former is for the entire packetList, while the latter is for each stream within the packetList.
The other pure virtual functions that you need to implement should be obvious and self-evident (see
You can use
PcapPort as a reference. It is a good idea to read about the PCAP API (from libpcap/winpcap websites) and how the API is used by
PcapPort to achieve the
AbstractPort functionality. Further specializations of the PcapPort class for each platform is available
For packet capture, implement the start/stop APIs and the captured data is returned by captureData() and is expected to be in a format that Wireshark can understand.