Skip to content

Commit 6a74872

Browse files
committed
timestamp of the IP packet acquired in the fast path
1 parent 2957bfb commit 6a74872

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
#include <iostream>
2222
#include <ctime>
2323
#include <fstream>
24+
#include <chrono>
2425
#include "unistd.h"
2526
#include "Packetcapture.h"
2627
#include "Packetcapture_dp_ingress.h"
2728
#include "Packetcapture_dp_egress.h"
2829
#define ON_T 0
2930
#define OFF_T 1
31+
#define BILLION 1000000000
32+
#define MILLION 1000000
33+
#define ONE_THOUSAND 1000
3034

3135

3236
typedef int bpf_int32;
@@ -52,6 +56,14 @@ struct pcap_pkthdr {
5256
Packetcapture::Packetcapture(const std::string name, const PacketcaptureJsonObject &conf)
5357
: TransparentCube(conf.getBase(), { packetcapture_code_ingress }, { packetcapture_code_egress }),
5458
PacketcaptureBase(name) {
59+
60+
gettimeofday(&ts, NULL); //getting actual time
61+
std::uint64_t uptime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
62+
time_t sec_off = uptime / BILLION;
63+
ts.tv_sec -= sec_off;
64+
uptime -= sec_off * BILLION;
65+
ts.tv_usec -= uptime;
66+
5567
logger()->info("Creating Packetcapture instance");
5668
setCapture(conf.getCapture());
5769
setAnomimize(conf.getAnomimize());
@@ -79,7 +91,6 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
7991

8092
PacketJsonObject pj;
8193
auto p = std::shared_ptr<Packet>(new Packet(*this, pj));
82-
struct timeval tp;
8394
std::streamsize len;
8495

8596
if (dt == "") {
@@ -105,7 +116,17 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
105116
myFile.write(reinterpret_cast<const char*>(pcap_header), sizeof(*pcap_header));
106117
writeHeader = false;
107118
}
108-
gettimeofday(&tp, NULL);
119+
struct timeval tp;
120+
tp.tv_sec = ts.tv_sec;
121+
tp.tv_usec = ts.tv_usec;
122+
time_t sec_off = temp_offset/BILLION; /* from nanoseconds to seconds */
123+
temp_offset -= sec_off*BILLION;
124+
tp.tv_usec += temp_offset/ONE_THOUSAND; /* from nanoseconds to microseconds */
125+
tp.tv_sec += sec_off;
126+
sec_off = tp.tv_usec/MILLION; /* from microseconds to seconds */
127+
tp.tv_usec -= sec_off*MILLION;
128+
tp.tv_sec += sec_off;
129+
109130
p->setTimestampSeconds((uint32_t) tp.tv_sec);
110131
p->setTimestampMicroseconds((uint32_t) tp.tv_usec);
111132
p->setPacketlen((uint32_t) packet.size());
@@ -140,13 +161,15 @@ void Packetcapture::packet_in(polycube::service::Direction direction,
140161

141162
switch (direction) {
142163
case polycube::service::Direction::INGRESS:
164+
temp_offset = get_array_table<uint64_t>("packet_timestamp", 0, ProgramType::INGRESS).get(0x0);
143165
if (getNetworkmode() == true) {
144166
addPacket(packet); /* store the packet in the FIFO queue*/
145167
} else {
146168
writeDump(packet);
147169
}
148170
break;
149171
case polycube::service::Direction::EGRESS:
172+
temp_offset = get_array_table<uint64_t>("packet_timestamp", 0, ProgramType::EGRESS).get(0x0);
150173
if (getNetworkmode() == true) {
151174
addPacket(packet); /* store the packet in the FIFO queue*/
152175
} else {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Packetcapture : public PacketcaptureBase {
4444
bool writeHeader;
4545
std::ofstream myFile;
4646
std::string dt;
47+
struct timeval ts;
48+
uint64_t temp_offset;
4749

4850
void addPacket(const std::vector<uint8_t> &packet);
4951

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ struct packetHeaders {
7777
*/
7878
BPF_ARRAY(pkt_header, struct packetHeaders, 1);
7979

80+
/*
81+
* BPF map where a single element, packet timestamp
82+
*/
83+
BPF_ARRAY(packet_timestamp, uint64_t, 1);
84+
8085
/*
8186
* BPF map where a single element, working mode [ON | OFF]
8287
*/
@@ -193,6 +198,13 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
193198

194199
}
195200

201+
/* Getting packet timestamp */
202+
uint64_t *pkt_timestamp = packet_timestamp.lookup(&key);
203+
if (!pkt_timestamp){
204+
return RX_DROP;
205+
}
206+
*pkt_timestamp = bpf_ktime_get_ns();
207+
196208
/*TODO: Now the traffic will be sent to the controller and will be processed in the slow path.
197209
* This solution will have a big impact on performance.
198210
* Currently, non-IP traffic is also sent to the controller, if this type of traffic is to be excluded

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ struct packetHeaders {
7777
*/
7878
BPF_ARRAY(pkt_header, struct packetHeaders, 1);
7979

80+
/*
81+
* BPF map where a single element, packet timestamp
82+
*/
83+
BPF_ARRAY(packet_timestamp, uint64_t, 1);
84+
8085
/*
8186
* BPF map where a single element, working mode [ON | OFF]
8287
*/
@@ -186,7 +191,14 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
186191
}
187192

188193
}
189-
194+
195+
/* Getting packet timestamp */
196+
uint64_t *pkt_timestamp = packet_timestamp.lookup(&key);
197+
if (!pkt_timestamp){
198+
return RX_DROP;
199+
}
200+
*pkt_timestamp = bpf_ktime_get_ns();
201+
190202
/*TODO: Now the traffic will be sent to the controller and will be processed in the slow path.
191203
* This solution will have a big impact on performance.
192204
* Currently, non-IP traffic is also sent to the controller, if this type of traffic is to be excluded

0 commit comments

Comments
 (0)