Simulator architecture

htsim is an event-based simulator, designed to simulate many TCP flows fast. The rest of the simulator is bare-bones.

Base classes

The basic classes are in network.h. They are Each Packet knows its route, namely a vector of PacketSinks. Whenever a PacketSink receives a Packet p, it processes it appropriately and may either drop the packet
p.free()
or send it on its way
p.sendOn()
Also, each packet belongs to a PacketFlow (it's useful to group packets into flows, for logging purposes). The flow and the route are specified when the packet is created, along with other useful information, e.g.
TrafficLoggerSimple tl;
PacketFlow _myflow(tl);
route_t myroute;
myroute.push_back(hop1); myroute.push_back(hop2); // ...
// then for each new packet
TcpPacket* p = TcpPacket::newpkt(_flow, *_route, _last_acked+1, _mss);
p->sendOn();
In most cases, as illustrated here, we subclass Packet to store extra protocol-specific information e.g. sequence number.

Network objects

The principal network objects are TcpSrc, TcpSink, Queue and Pipe. They all inherit from PacketSink. htsim implement (roughly) TCP New Reno, with fast retransmit and fast recovery but without selective acknowledgement. Additionally, for speed, the TCP retransmit timers have coarse granularity: treat the results with suspicion if you see many flows entering timeouts.

Event-based simulation

htsim is an event-based simulation. There is a single EventList object which keeps track of which events need to occur when. Every object which wishes to be scheduled must inherit from EventSource, e.g.
class MyThing : public EventSource {
public:
  MyThing(simtime_picosec period, EventList& eventlist);
  void doNextEvent();
  simtime_picosec _period;
  }
MyThing::MyThing(simtime_picosec period, EventList& eventlist)
: EventSource(eventlist,"mything") { // all event sources have names, used for logging
  _period = period;
  eventlist.sourceIsPendingRel(_period);
  }
void MyThing::doNextEvent() {
  // do some work...
  eventlist().sourceIsPendingRel(_period);  
  // eventlist() returns the EventList specified when this object was created
  // eventlist().now() is the current simulation time
  // sourceIsPendingRel(t) means "schedule me for t from now", sourceIsPending() means "schedule me for time t"
  } 
Run a simulation by
EventList eventlist;
eventlist.setEndtime(timeFromSec(60)); // if this is not set, the simulation will run until there are no events left to handle
// build the simulation, and schedule the initial events
while (eventlist.doNextEvent()) {} // run the simulation