-
Notifications
You must be signed in to change notification settings - Fork 390
/
sflow_pktgen.h
194 lines (168 loc) · 6.13 KB
/
sflow_pktgen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*/
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <analytics/sflow.h>
class PktHeaderGen {
public:
const static int kMaxPktHeaderLen = 128;
explicit PktHeaderGen() :
len_(0) {
memset(buff_, 0, kMaxPktHeaderLen);
}
~PktHeaderGen() {
}
void WriteEthHdr(const std::string& dmac, const std::string& smac,
uint16_t ether_type) {
struct ether_header* eth = (struct ether_header*)(buff_ + len_);
MacAddress::FromString(dmac).ToArray(eth->ether_dhost,
sizeof(eth->ether_dhost));
MacAddress::FromString(smac).ToArray(eth->ether_shost,
sizeof(eth->ether_shost));
eth->ether_type = htons(ether_type);
len_ += sizeof(struct ether_header);
}
void WriteIpHdr(const std::string& sip, const std::string& dip,
uint16_t proto, uint16_t ip_len) {
struct ip* iph = (struct ip*)(buff_ + len_);
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_len = htons(ip_len);
iph->ip_src.s_addr = inet_addr(sip.c_str());
iph->ip_dst.s_addr = inet_addr(dip.c_str());
iph->ip_p = proto;
len_ += sizeof(struct ip);
}
void WriteUdpHdr(uint16_t sport, uint16_t dport) {
struct udphdr* udp = (struct udphdr*)(buff_ + len_);
udp->source = htons(sport);
udp->dest = htons(dport);
len_ += sizeof(udphdr);
}
void WriteTcpHdr(uint16_t sport, uint16_t dport) {
struct tcphdr* tcp = (struct tcphdr*)(buff_ + len_);
tcp->source = htons(sport);
tcp->dest = htons(dport);
len_ += sizeof(tcphdr);
}
void WriteIcmpHdr(uint16_t icmp_type, uint16_t icmpid) {
struct icmp* icmp = (struct icmp*)(buff_ + len_);
icmp->icmp_type = icmp_type;
icmp->icmp_id = icmpid;
len_ += sizeof(struct icmp);
}
const uint8_t* GetPktHeader() const {
return buff_;
}
size_t GetPktHeaderLen() const {
return len_;
}
private:
uint8_t buff_[kMaxPktHeaderLen];
size_t len_;
};
class SFlowPktGen {
public:
const static int kMaxSFlowPktLen = 1024;
explicit SFlowPktGen() :
encode_ptr_(buff_),
end_ptr_(buff_+(kMaxSFlowPktLen/4)) {
memset(buff_, 0, kMaxSFlowPktLen);
}
~SFlowPktGen() {
}
void WriteHeader(const SFlowHeader& sflow_header) {
WriteData32(sflow_header.version);
WriteIpAddress(sflow_header.agent_ip_address);
WriteData32(sflow_header.agent_subid);
WriteData32(sflow_header.seqno);
WriteData32(sflow_header.uptime);
WriteData32(sflow_header.nsamples);
}
void WriteFlowSample(const SFlowFlowSample& flow_sample) {
WriteData32(flow_sample.type);
WriteData32(flow_sample.length);
WriteData32(flow_sample.seqno);
if (flow_sample.type == SFLOW_FLOW_SAMPLE_EXPANDED) {
WriteData32(flow_sample.sourceid_type);
WriteData32(flow_sample.sourceid_index);
} else {
uint32_t sourceid = (flow_sample.sourceid_type << 24) |
(flow_sample.sourceid_index);
WriteData32(sourceid);
}
WriteData32(flow_sample.sample_rate);
WriteData32(flow_sample.sample_pool);
WriteData32(flow_sample.drops);
if (flow_sample.type == SFLOW_FLOW_SAMPLE_EXPANDED) {
WriteData32(flow_sample.input_port_format);
WriteData32(flow_sample.input_port);
WriteData32(flow_sample.output_port_format);
WriteData32(flow_sample.output_port);
} else {
uint32_t input = (flow_sample.input_port_format << 30) |
(flow_sample.input_port);
WriteData32(input);
uint32_t output = (flow_sample.output_port_format << 30) |
(flow_sample.output_port);
WriteData32(output);
}
WriteData32(flow_sample.nflow_records);
boost::ptr_vector<SFlowFlowRecord>::const_iterator it =
flow_sample.flow_records.begin();
for (; it != flow_sample.flow_records.end(); ++it) {
WriteData32(it->type);
WriteData32(it->length);
switch(it->type) {
case SFLOW_FLOW_HEADER: {
SFlowFlowHeader& flow_header = (SFlowFlowHeader&)*it;
WriteData32(flow_header.protocol);
WriteData32(flow_header.frame_length);
WriteData32(flow_header.stripped);
WriteData32(flow_header.header_length);
WriteBytes(flow_header.header, flow_header.header_length);
break;
}
default:
assert(0);
}
}
}
void WriteCounterSample(const SFlowSample& counter_sample) {
std::auto_ptr<uint8_t> counter_sample_data(
new uint8_t[counter_sample.length]);
WriteData32(counter_sample.type);
WriteData32(counter_sample.length);
WriteBytes(counter_sample_data.get(), counter_sample.length);
}
const uint8_t* GetSFlowPkt() const {
return reinterpret_cast<const uint8_t*>(buff_);
}
size_t GetSFlowPktLen() const {
return (encode_ptr_ - buff_) * 4;
}
private:
void WriteData32(uint32_t data) {
assert(encode_ptr_+1 <= end_ptr_);
*encode_ptr_++ = htonl(data);
}
void WriteBytes(const uint8_t* bytes, size_t len) {
assert(encode_ptr_+((len+3)/4) <= end_ptr_);
memcpy(encode_ptr_, bytes, len);
encode_ptr_ += ((len+3)/4);
}
void WriteIpAddress(const IpAddress& ipaddr) {
if (ipaddr.is_v4()) {
WriteData32(SFLOW_IPADDR_V4);
WriteBytes(ipaddr.to_v4().to_bytes().c_array(), 4);
} else {
WriteData32(SFLOW_IPADDR_V6);
WriteBytes(ipaddr.to_v6().to_bytes().c_array(), 16);
}
}
uint32_t buff_[kMaxSFlowPktLen/4];
uint32_t* encode_ptr_;
const uint32_t* const end_ptr_;
};