-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe.c
215 lines (180 loc) · 5.83 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
/*-
* 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;
static struct rte_table_netflow *table_ref;
// 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 vlan)
{
struct rte_table_netflow *t = table_ref;
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;
/* To silence warnings */
k.port_src = 0;
k.port_dst = 0;
/* 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);
//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)
{
pktType_e pType;
pType = packet_type(m);
switch((int)pType) {
case ETHER_TYPE_ARP: //printf("arp\n");
break;
case ETHER_TYPE_IPv4: //printf("ipv4\n");
process_ipv4(m, 0); break;
case ETHER_TYPE_IPv6: //printf("ipv6\n");
break;
case ETHER_TYPE_VLAN: //printf("vlan\n");
break;
case UNKNOWN_PACKET: //printf("unknown\n");/* 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, struct rte_table_netflow *t)
{
int j;
table_ref = t;
/* 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]);
}
/* TODO */
// Additional processing like DPI
/* Handle remaining prefetched packets */
for (; j < nb_rx; j++)
packet_classify(pkts[j]);
}