Skip to content

Commit a558706

Browse files
committed
Update timestamp algorithm
Update packetcapture service
1 parent 84bdb2f commit a558706

4 files changed

Lines changed: 44 additions & 35 deletions

File tree

src/services/pcn-packetcapture/src/Packetcapture.cpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
#define ON_T 0
3030
#define OFF_T 1
3131
#define BILLION 1000000000
32-
#define MILLION 1000000
33-
#define ONE_THOUSAND 1000
34-
3532

3633
typedef int bpf_int32;
3734
typedef u_int bpf_u_int32;
@@ -70,16 +67,22 @@ Packetcapture::Packetcapture(const std::string name, const PacketcaptureJsonObje
7067
* 'timeval struct ts' rapresents the system start time in epoch calculated as
7168
* actual time - system uptime
7269
*
73-
* See line 138 of this file for more details
70+
* See line 141 or 307 of this file for more details
7471
*/
7572

7673

77-
gettimeofday(&ts, NULL); //getting actual time
78-
std::uint64_t uptime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
79-
time_t sec_off = uptime / BILLION;
80-
ts.tv_sec -= sec_off;
81-
uptime -= sec_off * BILLION;
82-
ts.tv_usec -= uptime;
74+
timeP = std::chrono::high_resolution_clock::now(); //getting actual time
75+
76+
std::chrono::nanoseconds uptime(0u);
77+
double uptime_seconds;
78+
if (std::ifstream("/proc/uptime", std::ios::in) >> uptime_seconds)
79+
{
80+
uptime = std::chrono::nanoseconds(
81+
static_cast<unsigned long long>(uptime_seconds*BILLION)
82+
);
83+
}
84+
85+
timeP -= uptime;
8386

8487
logger()->info("Creating Packetcapture instance");
8588
setCapture(conf.getCapture());
@@ -136,20 +139,13 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
136139

137140
/*
138141
* Here the packet capture time offset must be added to the system boot time stored in epoch format.
139-
* See line 61 of this file to see system boot time stored in epoch algorithm
142+
* See line 58 of this file to see system boot time stored in epoch algorithm
140143
*/
141144

142145
struct timeval tp;
143-
tp.tv_sec = ts.tv_sec;
144-
tp.tv_usec = ts.tv_usec;
145-
time_t sec_off = temp_offset/BILLION; /* from nanoseconds to seconds */
146-
temp_offset -= sec_off*BILLION;
147-
tp.tv_usec += temp_offset/ONE_THOUSAND; /* from nanoseconds to microseconds */
148-
tp.tv_sec += sec_off;
149-
sec_off = tp.tv_usec/MILLION; /* from microseconds to seconds */
150-
tp.tv_usec -= sec_off*MILLION;
151-
tp.tv_sec += sec_off;
152-
146+
std::chrono::system_clock::duration tp_dur = (timeP + temp_offset).time_since_epoch();
147+
to_timeval(tp_dur, tp);
148+
153149
p->setTimestampSeconds((uint32_t) tp.tv_sec);
154150
p->setTimestampMicroseconds((uint32_t) tp.tv_usec);
155151
p->setPacketlen((uint32_t) packet.size());
@@ -181,18 +177,18 @@ void Packetcapture::packet_in(polycube::service::Direction direction,
181177
const std::vector<uint8_t> &packet) {
182178

183179
Tins::EthernetII pkt(&packet[0], packet.size());
184-
180+
185181
switch (direction) {
186182
case polycube::service::Direction::INGRESS:
187-
temp_offset = get_array_table<uint64_t>("packet_timestamp", 0, ProgramType::INGRESS).get(0x0);
183+
temp_offset = std::chrono::nanoseconds(static_cast<unsigned long long>(get_array_table<uint64_t>("packet_timestamp", 0, ProgramType::INGRESS).get(0x0)));
188184
if (getNetworkmode() == true) {
189185
addPacket(packet); /* store the packet in the FIFO queue*/
190186
} else {
191187
writeDump(packet);
192188
}
193189
break;
194190
case polycube::service::Direction::EGRESS:
195-
temp_offset = get_array_table<uint64_t>("packet_timestamp", 0, ProgramType::EGRESS).get(0x0);
191+
temp_offset = std::chrono::nanoseconds(static_cast<unsigned long long>(get_array_table<uint64_t>("packet_timestamp", 0, ProgramType::EGRESS).get(0x0)));
196192
if (getNetworkmode() == true) {
197193
addPacket(packet); /* store the packet in the FIFO queue*/
198194
} else {
@@ -306,8 +302,16 @@ std::shared_ptr<Packet> Packetcapture::getPacket() {
306302
void Packetcapture::addPacket(const std::vector<uint8_t> &packet) {
307303
PacketJsonObject pj;
308304
auto p = std::shared_ptr<Packet>(new Packet(*this, pj));
305+
306+
/*
307+
* Here the packet capture time offset must be added to the system boot time stored in epoch format.
308+
* See line 58 of this file to see system boot time stored in epoch algorithm
309+
*/
310+
309311
struct timeval tp;
310-
gettimeofday(&tp, NULL);
312+
std::chrono::system_clock::duration tp_dur = (timeP + temp_offset).time_since_epoch();
313+
to_timeval(tp_dur, tp);
314+
311315
p->setTimestampSeconds((uint32_t) tp.tv_sec);
312316
p->setTimestampMicroseconds((uint32_t) tp.tv_usec);
313317
p->setPacketlen((uint32_t) packet.size());
@@ -403,3 +407,10 @@ void Packetcapture::replaceGlobalheader(const GlobalheaderJsonObject &conf) {
403407
void Packetcapture::delGlobalheader() {
404408
throw std::runtime_error("Packetcapture::delGlobalheader: method not implemented");
405409
}
410+
411+
void Packetcapture::to_timeval(std::chrono::system_clock::duration& d, struct timeval& tv) {
412+
std::chrono::seconds const sec = std::chrono::duration_cast<std::chrono::seconds>(d);
413+
414+
tv.tv_sec = sec.count();
415+
tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d - sec).count();
416+
}

src/services/pcn-packetcapture/src/Packetcapture.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919

2020
#include <list>
2121
#include <fstream>
22-
22+
#include <chrono>
2323
#include "../base/PacketcaptureBase.h"
24-
2524
#include "Filters.h"
2625
#include "Packet.h"
2726
#include "Globalheader.h"
28-
29-
3027
#include <tins/ethernetII.h>
3128
#include <tins/tins.h>
3229

@@ -44,10 +41,11 @@ class Packetcapture : public PacketcaptureBase {
4441
bool writeHeader;
4542
std::ofstream myFile;
4643
std::string dt;
47-
struct timeval ts;
48-
uint64_t temp_offset;
49-
50-
void addPacket(const std::vector<uint8_t> &packet);
44+
std::chrono::system_clock::time_point timeP;
45+
std::chrono::nanoseconds temp_offset;
46+
47+
void addPacket(const std::vector<uint8_t> &packet);
48+
void to_timeval(std::chrono::system_clock::duration& d, struct timeval& tv);
5149

5250
public:
5351
Packetcapture(const std::string name, const PacketcaptureJsonObject &conf);

src/services/pcn-packetcapture/src/Packetcapture_dp_egress.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
164164

165165
/* Getting packet timestamp
166166
*
167-
* see line 61 in Packetcapture.cpp for more details about timestamping algorithm
167+
* see line 58 in Packetcapture.cpp for more details about timestamping algorithm
168168
*/
169169
uint64_t *pkt_timestamp = packet_timestamp.lookup(&key);
170170
if (!pkt_timestamp){

src/services/pcn-packetcapture/src/Packetcapture_dp_ingress.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
164164

165165
/* Getting packet timestamp
166166
*
167-
* see line 61 in Packetcapture.cpp for more details about timestamping algorithm
167+
* see line 58 in Packetcapture.cpp for more details about timestamping algorithm
168168
*/
169169
uint64_t *pkt_timestamp = packet_timestamp.lookup(&key);
170170
if (!pkt_timestamp){

0 commit comments

Comments
 (0)