CoRE Simulation Models for Real-time Networks

Changeset f28a79e in oppResultManagers


Ignore:
Timestamp:
04/06/16 19:49:26 (3 years ago)
Author:
Till Steinbach <till.steinbach@…>
Branches:
master
Children:
7176e6a
Parents:
2e5bd9b
Message:

first running version of pcapng recorder

Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • examples/pcapng/omnetpp.ini

    r2e5bd9b rf28a79e  
    11[General]
    22sim-time-limit = 100s
     3simtime-precision = ns
    34
    4 network = oppresultmanagers.examples.common.Network
     5network = oppresultmanagers.examples.pcapng.Network
    56
    67record-eventlog = true
     
    910
    1011eventlogmanager-class = "PCAPNGEventlogManager"
     12
     13pcapng-interfaces = "Network.node1.mac, Network.node2.mac"
     14pcapng-capturelength = 32
     15
     16
     17**.node1.cli.destAddress = "node2"
     18**.node2.cli.destAddress = "node1"
     19**.cli.sendInterval = exponential(1s)
  • src/oppresultmanagers/eventlog/pcapng/PCAPNGEventlogManager.cc

    r2e5bd9b rf28a79e  
    22
    33#include "pcapng.h"
     4
     5#include "inet/common/serializer/SerializerBase.h"
    46
    57Register_Class(PCAPNGEventlogManager);
     
    810        "${resultdir}/${configname}-${runnumber}.pcapng", "Name of the PCAPNG file to generate.");
    911
     12Register_PerRunConfigOption(CFGID_EVENTLOG_PCAPNG_INTERFACES, "pcapng-interfaces", CFG_STRING, "\"\"",
     13        "List of modules (comma or space separated) that appear as an interface in the pcapng file");
     14
     15Register_PerRunConfigOption(CFGID_EVENTLOG_PCAPNG_CAPTURELENGTH, "pcapng-capturelength", CFG_INT, "10000",
     16        "Maximum length of packet that is being captured (length will be still correct)");
     17
    1018PCAPNGEventlogManager::PCAPNGEventlogManager()
    1119{
    1220    recordEventlog = false;
    13     pcapfile = nullptr;
     21    buffer = malloc(60000);
     22    pcapwriter = new PCAPNGWriter(buffer, 60000);
     23
     24    recordEventlog = omnetpp::cConfiguration::parseBool(
     25            omnetpp::getEnvir()->getConfig()->getConfigEntry("record-eventlog").getValue(), "false");
     26    filename = omnetpp::getEnvir()->getConfig()->getAsFilename(CFGID_EVENTLOG_PCAPNG_FILE).c_str();
     27    pcapwriter->openFile(filename.c_str());
     28
     29    recordingStarted = false;
     30    capture_length =
     31            static_cast<size_t>(omnetpp::getEnvir()->getConfig()->getAsInt(CFGID_EVENTLOG_PCAPNG_CAPTURELENGTH));
    1432}
    1533
    1634PCAPNGEventlogManager::~PCAPNGEventlogManager()
    1735{
     36    if (recordingStarted)
     37    {
     38        stopRecording();
     39    }
     40    pcapwriter->closeFile();
    1841}
    1942
    2043void PCAPNGEventlogManager::startRecording()
    2144{
    22     if (!pcapfile)
     45    pcapwriter->openSection("Simulation (no real Hardware)", "", "OMNeT++");
     46
     47    std::string cfgobj = omnetpp::getEnvir()->getConfig()->getAsString(CFGID_EVENTLOG_PCAPNG_INTERFACES);
     48    std::vector<std::string> interfaceModules = omnetpp::cStringTokenizer(cfgobj.c_str(), ", ").asVector();
     49    for (std::vector<std::string>::const_iterator interfaceModule = interfaceModules.begin();
     50            interfaceModule != interfaceModules.end(); ++interfaceModule)
    2351    {
    24         // main switch
    25         recordEventlog = omnetpp::cConfiguration::parseBool(
    26                 omnetpp::getEnvir()->getConfig()->getConfigEntry("record-eventlog").getValue(), "false");
    27 
    28         // setup filename
    29         filename = omnetpp::getEnvir()->getConfig()->getAsFilename(CFGID_EVENTLOG_PCAPNG_FILE).c_str();
    30 
    31         //removeFile(filename.c_str(), "old eventlog file");
    32         //mkPath(directoryOf(filename.c_str()).c_str());
    33         pcapfile = fopen(filename.c_str(), "w");
    34         if (!pcapfile)
    35             throw omnetpp::cRuntimeError("Cannot open pcapng file `%s' for write", filename.c_str());
    36         ::printf("Recording pcapng to file `%s'...\n", filename.c_str());
    37 
    38         //Write section header
    39         struct block_header bh;
    40         struct section_header_block shb;
    41         struct block_trailer bt;
    42 
    43         struct option_header oh;
    44         struct option_header oh_end;
    45         oh_end.option_code = OPT_ENDOFOPT;
    46         oh_end.option_length = 0;
    47 
    48         bh.block_type = BT_SHB;
    49 
    50         shb.byte_order_magic = BYTE_ORDER_MAGIC;
    51         shb.major_version = PCAP_NG_VERSION_MAJOR;
    52         shb.minor_version = PCAP_NG_VERSION_MINOR;
    53         shb.section_length = -1;
    54 
    55 
    56         std::string appName = "OMNe";
    57         oh.option_code = SEC_USERAPPL;
    58         oh.option_length = appName.length();
    59 
    60         bh.total_length = sizeof(bh) + sizeof(shb) + sizeof(oh) + oh.option_length + sizeof(oh_end) + sizeof(bt);
    61         bt.total_length = bh.total_length;
    62 
    63         fwrite(&bh, sizeof(bh), 1, pcapfile);
    64         fwrite(&shb, sizeof(shb), 1, pcapfile);
    65 
    66         fwrite(&oh, sizeof(oh), 1, pcapfile);
    67         fwrite(appName.data(), oh.option_length, 1, pcapfile);
    68         fwrite(&oh_end, sizeof(oh_end), 1, pcapfile);
    69 
    70         fwrite(&bt, sizeof(bt), 1, pcapfile);
    71 
    72         struct interface_description_block idb;
    73         bh.block_type = BT_IDB;
    74         idb.linktype = LINKTYPE_ETHERNET;
    75         idb.snaplen = 1518;
    76         bh.total_length = sizeof(bh) + sizeof(idb) + sizeof(bt);
    77         bt.total_length = bh.total_length;
    78 
    79         fwrite(&bh, sizeof(bh), 1, pcapfile);
    80         fwrite(&idb, sizeof(idb), 1, pcapfile);
    81         fwrite(&bt, sizeof(bt), 1, pcapfile);
    82 
    83         struct enhanced_packet_block ehb;
    84 
    85         bh.block_type = BT_EPB;
    86         ehb.interface_id = 0;
    87         ehb.timestamp_high = 0; //simtime 64bit
    88         ehb.timestamp_low = 0; //simtime 64bit
    89         ehb.caplen = 0;
    90         ehb.len = 0;
    91 
    92         bh.total_length = sizeof(bh) + sizeof(ehb) + sizeof(bt);
    93         bt.total_length = bh.total_length;
    94 
    95         fwrite(&bh, sizeof(bh), 1, pcapfile);
    96         fwrite(&ehb, sizeof(ehb), 1, pcapfile);
    97         fwrite(&bt, sizeof(bt), 1, pcapfile);
     52        omnetpp::cModule *module = omnetpp::getSimulation()->getModuleByPath((*interfaceModule).c_str());
     53        if (!module)
     54        {
     55            throw omnetpp::cRuntimeError("error in ini file (pcapng-interfaces option): Module \"%s\" cannot be found",
     56                    (*interfaceModule).c_str());
     57        }
     58        interfaceMap[module] = pcapwriter->addInterface("", module->getFullPath(), capture_length,
     59                static_cast<uint8_t>(abs(omnetpp::simTime().getScaleExp())));
    9860    }
     61    recordingStarted = true;
    9962}
    10063
    10164void PCAPNGEventlogManager::stopRecording()
    10265{
    103     if (pcapfile)
     66    if (recordingStarted)
    10467    {
    105         fflush(pcapfile);
    106         fclose(pcapfile);
    107 
    108         pcapfile = nullptr;
     68        pcapwriter->closeSection();
     69        recordingStarted = false;
    10970    }
    11071}
     
    11273void PCAPNGEventlogManager::flush()
    11374{
    114     if (recordEventlog)
    115         fflush(pcapfile);
     75
    11676}
    11777
    118 void PCAPNGEventlogManager::simulationEvent(__attribute__((__unused__))   omnetpp::cEvent *event)
     78void PCAPNGEventlogManager::simulationEvent(__attribute__((__unused__))                   omnetpp::cEvent *event)
    11979{
    12080
     
    12383void PCAPNGEventlogManager::beginSend(omnetpp::cMessage *msg)
    12484{
     85    if (recordEventlog)
     86    {
     87        if (!recordingStarted)
     88        {
     89            startRecording();
     90        }
    12591
     92        if (omnetpp::cPacket* pkt = dynamic_cast<omnetpp::cPacket*>(msg))
     93        {
     94            if (dynamic_cast<omnetpp::cDatarateChannel*>(msg->getSenderGate()->findTransmissionChannel()))
     95            {
     96                if (interfaceMap.find(msg->getSenderModule()) != interfaceMap.end())
     97                {
     98                    char buffer[10000];
     99                    inet::serializer::Buffer wb(buffer, sizeof(buffer));
     100                    inet::serializer::Context c;
     101                    c.throwOnSerializerNotFound = false;
     102                    inet::serializer::SerializerBase::lookupAndSerialize(pkt, wb, c, inet::serializer::LINKTYPE,
     103                            inet::serializer::LINKTYPE_ETHERNET, capture_length);
     104                    EV << wb.getPos() << std::endl;
     105                    pcapwriter->addEnhancedPacket(static_cast<uint32_t>(interfaceMap[msg->getSenderModule()]), true,
     106                            static_cast<uint64_t>(msg->getSendingTime().raw()), pkt->getByteLength(), wb.getPos(),
     107                            buffer);
     108                }
     109            }
     110        }
     111    }
    126112}
    127 
    128 void PCAPNGEventlogManager::connectionCreated(omnetpp::cGate *srcgate)
    129 {
    130 
    131 }
  • src/oppresultmanagers/eventlog/pcapng/PCAPNGEventlogManager.h

    r2e5bd9b rf28a79e  
    33
    44#include "omnetpp.h"
     5#include "PCAPNGWriter.h"
    56
    67extern omnetpp::cConfigOption *CFGID_EVENTLOG_PCAPNG_FILE;
     8extern omnetpp::cConfigOption *CFGID_EVENTLOG_PCAPNG_INTERFACES;
     9extern omnetpp::cConfigOption *CFGID_EVENTLOG_PCAPNG_CAPTURELENGTH;
    710
    811/**
     
    1316    private:
    1417        omnetpp::opp_string filename;
    15         FILE *pcapfile;
    1618        bool recordEventlog;
     19        void* buffer;
     20        PCAPNGWriter *pcapwriter;
     21        bool recordingStarted = false;
     22        std::map<omnetpp::cModule*,size_t> interfaceMap;
     23        size_t capture_length;
    1724
    1825    public:
     
    4855        virtual void gateCreated(__attribute__((__unused__)) omnetpp::cGate *newgate) override {}
    4956        virtual void gateDeleted(__attribute__((__unused__)) omnetpp::cGate *gate) override {}
    50         virtual void connectionCreated(__attribute__((__unused__)) omnetpp::cGate *srcgate) override;
     57        virtual void connectionCreated(__attribute__((__unused__)) omnetpp::cGate *srcgate) override {}
    5158        virtual void connectionDeleted(__attribute__((__unused__)) omnetpp::cGate *srcgate) override {}
    5259        virtual void displayStringChanged(__attribute__((__unused__)) omnetpp::cComponent *component) override {}
  • src/oppresultmanagers/eventlog/pcapng/pcapng.h

    r2e5bd9b rf28a79e  
    99#define PCAPNG_H_
    1010
     11#include <sys/types.h>
     12#include <stdint.h>
     13
    1114/*
    1215 * Block types.
     
    1619 * Common part at the beginning of all blocks.
    1720 */
    18 struct block_header {
     21typedef struct {
    1922    uint32_t block_type;
    2023    uint32_t total_length;
    21 };
     24} block_header;
    2225
    2326/*
    2427 * Common trailer at the end of all blocks.
    2528 */
    26 struct block_trailer {
     29typedef struct {
    2730    uint32_t total_length;
    28 };
     31} block_trailer;
    2932
    3033/*
     
    3740 * Option header.
    3841 */
    39 struct option_header {
     42typedef struct {
    4043    u_short     option_code;
    4144    u_short     option_length;
    42 };
     45} option_header;
    4346
    4447
     
    5356#define BT_SHB          0x0A0D0D0A
    5457
    55 struct section_header_block {
     58typedef struct {
    5659    uint32_t    byte_order_magic;
    5760    u_short     major_version;
     
    5962    u_int64_t   section_length;
    6063    /* followed by options and trailer */
    61 };
     64} section_header_block;
    6265
    6366/*
     
    8689#define BT_IDB          0x00000001
    8790
    88 struct interface_description_block {
     91typedef struct {
    8992    u_short     linktype;
    9093    u_short     reserved;
    9194    uint32_t    snaplen;
    9295    /* followed by options and trailer */
    93 };
     96} interface_description_block;
    9497
    9598/*
     
    113116 * Linktypes in the IDB.
    114117 */
    115 #define LINKTYPE_ETHERNET   1
     118#define IDB_LINKTYPE_ETHERNET   1
    116119
    117120/*
     
    120123#define BT_EPB          0x00000006
    121124
    122 struct enhanced_packet_block {
     125typedef struct {
    123126    uint32_t interface_id;
    124127    uint32_t timestamp_high;
     
    127130    uint32_t len;
    128131    /* followed by packet data, options, and trailer */
    129 };
     132} enhanced_packet_block;
    130133
     134#define EP_FLAGS 2
     135#define EP_HASH 3
     136#define EP_DROPCOUNT 4
    131137
    132138#endif /* PCAPNG_H_ */
Note: See TracChangeset for help on using the changeset viewer.