Skip to content

Commit 84bdb2f

Browse files
committed
Update Packetcapture.cpp and added timestamp comments
1 parent 6a74872 commit 84bdb2f

3 files changed

Lines changed: 41 additions & 78 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ Packetcapture::Packetcapture(const std::string name, const PacketcaptureJsonObje
5757
: TransparentCube(conf.getBase(), { packetcapture_code_ingress }, { packetcapture_code_egress }),
5858
PacketcaptureBase(name) {
5959

60+
/*
61+
* The timestamp of the packet is acquired in the fast path using 'bpf_ktime_get_ns()'
62+
* This function returns the time elapsed since system booted, in nanoseconds.
63+
* See line 173 in Packetcapture_dp_egress.c and Packetcapture_dp_ingress.c
64+
*
65+
* The packet's timestamp must be in epoch so now we will store the current
66+
* time in epoch format and then the packet capture time offset,that was stored in the fast path,
67+
* will be added to it.
68+
*
69+
* Now we are calculating the system start time in epoch.
70+
* 'timeval struct ts' rapresents the system start time in epoch calculated as
71+
* actual time - system uptime
72+
*
73+
* See line 138 of this file for more details
74+
*/
75+
76+
6077
gettimeofday(&ts, NULL); //getting actual time
6178
std::uint64_t uptime = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
6279
time_t sec_off = uptime / BILLION;
@@ -116,6 +133,12 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
116133
myFile.write(reinterpret_cast<const char*>(pcap_header), sizeof(*pcap_header));
117134
writeHeader = false;
118135
}
136+
137+
/*
138+
* 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
140+
*/
141+
119142
struct timeval tp;
120143
tp.tv_sec = ts.tv_sec;
121144
tp.tv_usec = ts.tv_usec;

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

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,6 @@ struct eth_hdr {
5959
__be16 proto;
6060
} __attribute__((packed));
6161

62-
struct packetHeaders {
63-
uint64_t srcMac;
64-
uint64_t dstMac;
65-
uint16_t vlan;
66-
bool vlan_present;
67-
bool ip;
68-
uint32_t srcIp;
69-
uint32_t dstIp;
70-
uint8_t l4proto;
71-
uint16_t srcPort;
72-
uint16_t dstPort;
73-
};
74-
75-
/*
76-
* BPF map where a single element, the packet header
77-
*/
78-
BPF_ARRAY(pkt_header, struct packetHeaders, 1);
7962

8063
/*
8164
* BPF map where a single element, packet timestamp
@@ -104,12 +87,6 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
10487
if (*status == OFF){
10588
return RX_OK;
10689
}
107-
108-
struct packetHeaders *pkt;
109-
pkt = pkt_header.lookup(&key);
110-
if (pkt == NULL) {
111-
return RX_DROP;
112-
}
11390

11491
/* Parsing L2 */
11592
void *data = (void *)(long)ctx->data;
@@ -118,20 +95,16 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
11895
if (data + sizeof(*ethernet) > data_end)
11996
return RX_DROP;
12097

121-
pkt->srcMac = ethernet->src;
122-
pkt->dstMac = ethernet->dst;
12398
uint16_t ether_type = ethernet->proto;
12499

125100
if (ctx->vlan_present) {
126101
ether_type = ctx->vlan_proto;
127-
pkt->vlan = (uint16_t)(ctx->vlan_tci & 0x0fff);
128102
}
129103

130104
/* Parsing L3 */
131105
struct iphdr *ip = NULL;
132106
struct tcphdr *tcp = NULL;
133107
struct udphdr *udp = NULL;
134-
pkt->ip = 0;
135108

136109
if (ether_type == bpf_htons(ETH_P_IP)) {
137110
ip = data + sizeof(*ethernet);
@@ -166,14 +139,11 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
166139
}
167140
}
168141

169-
pkt->ip = 1;
170-
pkt->srcIp = ip->saddr;
171-
pkt->dstIp = ip->daddr;
142+
uint8_t header_len = 4 * ip->ihl;
172143
if (ip->protocol == IPPROTO_TCP) {
173-
tcp = data + sizeof(*ethernet) + sizeof(*ip);
174-
if (data + sizeof(*ethernet) + sizeof(*ip) + sizeof(*tcp) > data_end)
144+
tcp = data + sizeof(*ethernet) + header_len;
145+
if (data + sizeof(*ethernet) + header_len + sizeof(*tcp) > data_end)
175146
return RX_DROP;
176-
pkt->l4proto = IPPROTO_TCP;
177147

178148
/* src port filter */
179149
if ((filters_tab->src_port_flag == true) && (filters_tab->src_port_filter != ntohs(tcp->source))){
@@ -184,21 +154,18 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
184154
if ((filters_tab->dst_port_flag == true) && (filters_tab->dst_port_filter != ntohs(tcp->dest))){
185155
return RX_OK;
186156
}
187-
188-
pkt->srcPort = tcp->source;
189-
pkt->dstPort = tcp->dest;
190157
} else if (ip->protocol == IPPROTO_UDP) {
191-
udp = data + sizeof(*ethernet) + sizeof(*ip);
192-
if (data + sizeof(*ethernet) + sizeof(*ip) + sizeof(*udp) > data_end)
158+
udp = data + sizeof(*ethernet) + header_len;
159+
if (data + sizeof(*ethernet) + header_len + sizeof(*udp) > data_end)
193160
return RX_DROP;
194-
pkt->l4proto = IPPROTO_UDP;
195-
pkt->srcPort = udp->source;
196-
pkt->dstPort = udp->dest;
197161
}
198162

199163
}
200164

201-
/* Getting packet timestamp */
165+
/* Getting packet timestamp
166+
*
167+
* see line 61 in Packetcapture.cpp for more details about timestamping algorithm
168+
*/
202169
uint64_t *pkt_timestamp = packet_timestamp.lookup(&key);
203170
if (!pkt_timestamp){
204171
return RX_DROP;

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

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,6 @@ struct eth_hdr {
5959
__be16 proto;
6060
} __attribute__((packed));
6161

62-
struct packetHeaders {
63-
uint64_t srcMac;
64-
uint64_t dstMac;
65-
uint16_t vlan;
66-
bool vlan_present;
67-
bool ip;
68-
uint32_t srcIp;
69-
uint32_t dstIp;
70-
uint8_t l4proto;
71-
uint16_t srcPort;
72-
uint16_t dstPort;
73-
};
74-
75-
/*
76-
* BPF map where a single element, the packet header
77-
*/
78-
BPF_ARRAY(pkt_header, struct packetHeaders, 1);
7962

8063
/*
8164
* BPF map where a single element, packet timestamp
@@ -104,11 +87,6 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
10487
if (*status == OFF){
10588
return RX_OK;
10689
}
107-
struct packetHeaders *pkt;
108-
pkt = pkt_header.lookup(&key);
109-
if (pkt == NULL) {
110-
return RX_DROP;
111-
}
11290

11391
/* Parsing L2 */
11492
void *data = (void *)(long)ctx->data;
@@ -117,13 +95,10 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
11795
if (data + sizeof(*ethernet) > data_end)
11896
return RX_DROP;
11997

120-
pkt->srcMac = ethernet->src;
121-
pkt->dstMac = ethernet->dst;
12298
uint16_t ether_type = ethernet->proto;
12399

124100
if (ctx->vlan_present) {
125101
ether_type = ctx->vlan_proto;
126-
pkt->vlan = (uint16_t)(ctx->vlan_tci & 0x0fff);
127102
}
128103

129104
/* Parsing L3 */
@@ -164,9 +139,10 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
164139
}
165140
}
166141

142+
uint8_t header_len = 4 * ip->ihl;
167143
if (ip->protocol == IPPROTO_TCP) {
168-
tcp = data + sizeof(*ethernet) + sizeof(*ip);
169-
if (data + sizeof(*ethernet) + sizeof(*ip) + sizeof(*tcp) > data_end)
144+
tcp = data + sizeof(*ethernet) + header_len;
145+
if (data + sizeof(*ethernet) + header_len + sizeof(*tcp) > data_end)
170146
return RX_DROP;
171147

172148
/* src port filter */
@@ -178,21 +154,18 @@ static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *m
178154
if ((filters_tab->dst_port_flag == true) && (filters_tab->dst_port_filter != ntohs(tcp->dest))){
179155
return RX_OK;
180156
}
181-
182-
pkt->srcPort = tcp->source;
183-
pkt->dstPort = tcp->dest;
184157
} else if (ip->protocol == IPPROTO_UDP) {
185-
udp = data + sizeof(*ethernet) + sizeof(*ip);
186-
if (data + sizeof(*ethernet) + sizeof(*ip) + sizeof(*udp) > data_end)
158+
udp = data + sizeof(*ethernet) + header_len;
159+
if (data + sizeof(*ethernet) + header_len + sizeof(*udp) > data_end)
187160
return RX_DROP;
188-
pkt->l4proto = IPPROTO_UDP;
189-
pkt->srcPort = udp->source;
190-
pkt->dstPort = udp->dest;
191161
}
192162

193163
}
194164

195-
/* Getting packet timestamp */
165+
/* Getting packet timestamp
166+
*
167+
* see line 61 in Packetcapture.cpp for more details about timestamping algorithm
168+
*/
196169
uint64_t *pkt_timestamp = packet_timestamp.lookup(&key);
197170
if (!pkt_timestamp){
198171
return RX_DROP;

0 commit comments

Comments
 (0)