-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathprobe.c
254 lines (213 loc) · 7.02 KB
/
probe.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*-
* BSD LICENSE
*
* Copyright(c) 2014, Choonho Son choonho.som@gmail.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "probe.h"
extern probe_t probe;
// Allocate the netflow structure for global use
volatile int quit = 0;
/**************************************************************************//**
*
* packet_type - Examine a packet and return the type of packet
*
* DESCRIPTION
* Examine a packet and return the type of packet.
* the packet.
*
* RETURNS: N/A
*
* SEE ALSO:
*/
static __inline__ pktType_e
packet_type( struct rte_mbuf * m )
{
pktType_e ret;
struct ether_hdr *eth;
eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
ret = ntohs(eth->ether_type);
return ret;
}
#define PRINT_IP(x) printf("%d.%d.%d.%d", (x&0x000000ff), (x&0x0000ff00)>>8, (x&0x00ff0000)>>16, (x&0xff000000)>>24)
void
print_ipv4(struct ipv4_hdr *ip)
{
PRINT_IP(ip->src_addr);
printf("-(%d)->", ip->next_proto_id);
PRINT_IP(ip->dst_addr);
printf("\n");
}
void
print_flow(union rte_table_netflow_key *k)
{
PRINT_IP(k->ip_src);
printf(" (%d) -[%d]-> (%d) ", ntohs(k->port_src), k->proto, ntohs(k->port_dst));
PRINT_IP(k->ip_dst);
printf("\n");
}
/****************************************************************************
*
*/
void
process_ipv4(struct rte_mbuf * m, int pid, int vlan)
{
port_info_t *info = &probe.info[pid];
struct rte_table_netflow *t = (struct rte_table_netflow *)probe.table[pid];
hashBucket_t *bkt;
int key_found=0;
char proto;
struct ether_hdr *eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
struct ipv4_hdr *ip = (struct ipv4_hdr *)ð[1];
struct tcp_hdr *tcp;
struct udp_hdr *udp;
union rte_table_netflow_key k;
uint32_t init_val;
/* Adjust for a vlan header if present */
if (vlan)
ip = (struct ipv4_hdr *)((char *)ip + sizeof(struct vlan_hdr));
k.ip_src = ip->src_addr;
k.ip_dst = ip->dst_addr;
k.proto = ip->next_proto_id;
k.pad0 = 0;
k.pad1 = 0;
k.vlanId = vlan;
//print_ipv4(ip);
// based on proto, TCP/UDP/ICMP...
switch(ip->next_proto_id) {
case IPPROTO_UDP:
udp = (struct udp_hdr *)((unsigned char*)ip + sizeof(struct ipv4_hdr));
k.port_src = udp->src_port;
k.port_dst = udp->dst_port;
break;
case IPPROTO_TCP:
tcp = (struct tcp_hdr *)((unsigned char*)ip + sizeof(struct ipv4_hdr));
k.port_src = tcp->src_port;
k.port_dst = tcp->dst_port;
break;
default:
break;
}
rte_table_netflow_entry_add(t, &k, ip, &key_found, &bkt);
//print_flow(&k);
//TODO
// 1) decode flow header
// 2) pkt to hash
//printf("%" PRIu32 "\n", init_val);
// 3) process hash table (export flows)
}
/****************************************************************************
*
* packet_classify - Examine a packet and classify it for statistics
*
* DESCRIPTION
* Examine a packet and determine its type along with counting statistics around
* the packet.
*
* RETURNS: N/A
*
* SEE ALSO:
*/
#define FCS_SIZE 4
static void
packet_classify( struct rte_mbuf * m, int pid )
{
port_info_t * info = &probe.info[pid];
int plen = (m->pkt_len + FCS_SIZE);
pktType_e pType;
pType = packet_type(m);
switch((int)pType) {
case ETHER_TYPE_ARP: info->stats.arp_pkts++; printf("arp\n"); break;
case ETHER_TYPE_IPv4: info->stats.ip_pkts++; process_ipv4(m, pid, 0); break;
case ETHER_TYPE_IPv6: info->stats.ipv6_pkts++; break;
case ETHER_TYPE_VLAN: info->stats.vlan_pkts++; break;
case UNKNOWN_PACKET: /* FALL THRU */
default: break;
}
}
/*************************************************************
* packet classify - Classify a set of packets in one call
*
* DESCRIPTION
* Classify a list of packets and to improve clasify peformance.
*
* Return: N/A
*/
#define PREFETCH_OFFSET 3
static __inline__ void
packet_classify_bulk(struct rte_mbuf **pkts, int nb_rx, int pid)
{
int j;
/* Prefetch first packets */
for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
rte_prefetch0(rte_pktmbuf_mtod(pkts[j], void *));
/* Prefetch and handle already prefetched packets */
for (j = 0; j < (nb_rx-PREFETCH_OFFSET); j++) {
rte_prefetch0(rte_pktmbuf_mtod(pkts[j + PREFETCH_OFFSET], void *));
packet_classify(pkts[j], pid);
}
/* TODO */
// Additional processing like DPI
/* Handle remaining prefetched packets */
for (; j < nb_rx; j++)
packet_classify(pkts[j], pid);
}
int
launch_probe(__attribute__((unused)) void *arg)
{
uint8_t lcore_id = rte_lcore_id();
uint8_t nb_rx, pid, qid, i, j;
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
struct rte_mbuf *m;
printf("## lcore_id:%d\n", lcore_id);
/* find l2p mapping */
l2p_t *l2p;
for (i = 0; i <= _MAX_LCORE; i++) {
l2p = &probe.l2p[i];
if (l2p->lcore_id == lcore_id) {
pid = l2p->port_id;
qid = l2p->queue_id;
break;
}
}
printf("lcore ID:%d\n", lcore_id);
printf("port ID:%d\n", pid);
printf("queue ID:%d\n", qid);
while(!quit) {
// Read packet from RX Queue
// param0: port ID
// param1: queue ID
nb_rx = rte_eth_rx_burst(pid, qid, pkts_burst, MAX_PKT_BURST);
if (unlikely(nb_rx == 0)) continue;
//printf("lcore(%d) nb_packet(%d)\n", lcore_id, nb_rx);
// classify the packets
packet_classify_bulk(pkts_burst, nb_rx, pid);
// free the packets
for(j = 0; j < nb_rx; j++)
rte_pktmbuf_free(pkts_burst[j]);
}
return 0;
}