Skip to content

Commit 0ea269c

Browse files
authored
Merge pull request #155 from BonnyAD9/add-vlan-to-flow-key
Add VLAN ID to flow key
2 parents b35cf4b + 622dc57 commit 0ea269c

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

include/ipfixprobe/packet.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct Packet : public Record {
5959
uint8_t ip_flags;
6060
ipaddr_t src_ip;
6161
ipaddr_t dst_ip;
62+
uint32_t vlan_id;
6263

6364
uint16_t src_port;
6465
uint16_t dst_port;
@@ -93,7 +94,7 @@ struct Packet : public Record {
9394
ts({0}),
9495
dst_mac(), src_mac(), ethertype(0),
9596
ip_len(0), ip_payload_len(0), ip_version(0), ip_ttl(0),
96-
ip_proto(0), ip_tos(0), ip_flags(0), src_ip({0}), dst_ip({0}),
97+
ip_proto(0), ip_tos(0), ip_flags(0), src_ip({0}), dst_ip({0}), vlan_id(0),
9798
src_port(0), dst_port(0), tcp_flags(0), tcp_window(0),
9899
tcp_options(0), tcp_mss(0), tcp_seq(0), tcp_ack(0),
99100
packet(nullptr), packet_len(0), packet_len_wire(0),

input/parser.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,23 @@ inline uint16_t parse_eth_hdr(const u_char *data_ptr, uint16_t data_len, Packet
8888
memcpy(pkt->dst_mac, eth->h_dest, 6);
8989
memcpy(pkt->src_mac, eth->h_source, 6);
9090

91-
if (ethertype == ETH_P_8021AD) {
91+
// set the default value in case there is no VLAN ID
92+
pkt->vlan_id = 0;
93+
94+
if (ethertype == ETH_P_8021AD || ethertype == ETH_P_8021Q) {
9295
if (4 > data_len - hdr_len) {
9396
throw "Parser detected malformed packet";
9497
}
95-
DEBUG_CODE(uint16_t vlan = ntohs(*(uint16_t *) (data_ptr + hdr_len)));
96-
DEBUG_MSG("\t802.1ad field:\n");
98+
99+
// only the most outer vlan id is extracted
100+
uint16_t vlan = ntohs(*(uint16_t *) (data_ptr + hdr_len));
101+
// VLAN ID is the 12 LSb
102+
pkt->vlan_id = vlan & 0x0FFF;
103+
104+
DEBUG_MSG("\t%s field:\n", (ethertype == ETH_P_8021AD ? "802.1ad" : "802.1q"));
97105
DEBUG_MSG("\t\tPriority:\t%u\n", ((vlan & 0xE000) >> 12));
98106
DEBUG_MSG("\t\tCFI:\t\t%u\n", ((vlan & 0x1000) >> 11));
99-
DEBUG_MSG("\t\tVLAN:\t\t%u\n", (vlan & 0x0FFF));
107+
DEBUG_MSG("\t\tVLAN:\t\t%u\n", (pkt->vlan_id));
100108

101109
hdr_len += 4;
102110
ethertype = ntohs(*(uint16_t *) (data_ptr + hdr_len - 2));

storage/cache.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,15 @@ bool NHTFlowCache::create_hash_key(Packet &pkt)
492492
key_v4->dst_port = pkt.dst_port;
493493
key_v4->src_ip = pkt.src_ip.v4;
494494
key_v4->dst_ip = pkt.dst_ip.v4;
495+
key_v4->vlan_id = pkt.vlan_id;
495496

496497
key_v4_inv->proto = pkt.ip_proto;
497498
key_v4_inv->ip_version = IP::v4;
498499
key_v4_inv->src_port = pkt.dst_port;
499500
key_v4_inv->dst_port = pkt.src_port;
500501
key_v4_inv->src_ip = pkt.dst_ip.v4;
501502
key_v4_inv->dst_ip = pkt.src_ip.v4;
503+
key_v4_inv->vlan_id = pkt.vlan_id;
502504

503505
m_keylen = sizeof(flow_key_v4_t);
504506
return true;
@@ -512,13 +514,15 @@ bool NHTFlowCache::create_hash_key(Packet &pkt)
512514
key_v6->dst_port = pkt.dst_port;
513515
memcpy(key_v6->src_ip, pkt.src_ip.v6, sizeof(pkt.src_ip.v6));
514516
memcpy(key_v6->dst_ip, pkt.dst_ip.v6, sizeof(pkt.dst_ip.v6));
517+
key_v6->vlan_id = pkt.vlan_id;
515518

516519
key_v6_inv->proto = pkt.ip_proto;
517520
key_v6_inv->ip_version = IP::v6;
518521
key_v6_inv->src_port = pkt.dst_port;
519522
key_v6_inv->dst_port = pkt.src_port;
520523
memcpy(key_v6_inv->src_ip, pkt.dst_ip.v6, sizeof(pkt.dst_ip.v6));
521524
memcpy(key_v6_inv->dst_ip, pkt.src_ip.v6, sizeof(pkt.src_ip.v6));
525+
key_v6_inv->vlan_id = pkt.vlan_id;
522526

523527
m_keylen = sizeof(flow_key_v6_t);
524528
return true;

storage/cache.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct __attribute__((packed)) flow_key_v4_t {
4848
uint8_t ip_version;
4949
uint32_t src_ip;
5050
uint32_t dst_ip;
51+
uint16_t vlan_id;
5152
};
5253

5354
struct __attribute__((packed)) flow_key_v6_t {
@@ -57,6 +58,7 @@ struct __attribute__((packed)) flow_key_v6_t {
5758
uint8_t ip_version;
5859
uint8_t src_ip[16];
5960
uint8_t dst_ip[16];
61+
uint16_t vlan_id;
6062
};
6163

6264
#define MAX_KEY_LENGTH (max<size_t>(sizeof(flow_key_v4_t), sizeof(flow_key_v6_t)))

0 commit comments

Comments
 (0)