-
Notifications
You must be signed in to change notification settings - Fork 136
/
onvm_pkt_helper.h
235 lines (197 loc) · 6.82 KB
/
onvm_pkt_helper.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
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
/*********************************************************************
* openNetVM
* https://sdnfv.github.io
*
* BSD LICENSE
*
* Copyright(c)
* 2015-2019 George Washington University
* 2015-2019 University of California Riverside
* 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.
* * The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* 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.
*
* onvm_pkt_helper.h - packet helper routines
********************************************************************/
#ifndef _ONVM_PKT_HELPER_H_
#define _ONVM_PKT_HELPER_H_
#include <inttypes.h>
#include <rte_ether.h>
#include <rte_mempool.h>
struct port_info;
struct rte_mbuf;
struct rte_tcp_hdr;
struct rte_udp_hdr;
struct rte_ipv4_hdr;
#define IP_PROTOCOL_TCP 6
#define IP_PROTOCOL_UDP 17
#define SUPPORTS_IPV4_CHECKSUM_OFFLOAD (1 << 0)
#define SUPPORTS_TCP_CHECKSUM_OFFLOAD (1 << 1)
#define SUPPORTS_UDP_CHECKSUM_OFFLOAD (1 << 2)
#define PROTO_UDP 0x11
#define IPV4_VERSION_IHL 69
#define IPV4_TTL 64
#define UDP_SAMPLE_SRC_PORT 12345
#define UDP_SAMPLE_DST_PORT 54321
#define IPV4_SAMPLE_SRC (uint32_t) RTE_IPV4(10, 0, 0, 1)
#define IPV4_SAMPLE_DST (uint32_t) RTE_IPV4(10, 0, 0, 2)
#define SAMPLE_NIC_PORT 0
/* Returns the bitflags in the tcp header */
#define ONVM_PKT_GET_FLAGS(tcp, flags) \
do { \
(flags) = (((tcp)->data_off << 8) | (tcp)->tcp_flags) & 0b111111111; \
} while (0)
/* Sets the bitflags in the tcp header */
#define ONVM_PKT_SET_FLAGS(tcp, flags) \
do { \
(tcp)->tcp_flags = (flags)&0xFF; \
(tcp)->data_off |= ((flags) >> 8) & 0x1; \
} while (0)
/**
* Assign the source and destination MAC address of the packet to the specified
* source and destination port ID.
*/
int
onvm_pkt_set_mac_addr(struct rte_mbuf* pkt, unsigned src_port_id, unsigned dst_port_id, struct port_info* ports);
/**
* Swap the source MAC address of a packet with a specified destination port's MAC address.
*/
int
onvm_pkt_swap_src_mac_addr(struct rte_mbuf* pkt, unsigned dst_port_id, struct port_info* ports);
/**
* Swap the destination MAC address of a packet with a specified source port's MAC address.
*/
int
onvm_pkt_swap_dst_mac_addr(struct rte_mbuf* pkt, unsigned src_port_id, struct port_info* ports);
/**
* Return a pointer to the tcp/udp/ip header in the packet, or NULL if not a TCP packet
*/
struct rte_tcp_hdr*
onvm_pkt_tcp_hdr(struct rte_mbuf* pkt);
struct rte_ether_hdr*
onvm_pkt_ether_hdr(struct rte_mbuf* pkt);
struct rte_udp_hdr*
onvm_pkt_udp_hdr(struct rte_mbuf* pkt);
struct rte_ipv4_hdr*
onvm_pkt_ipv4_hdr(struct rte_mbuf* pkt);
/**
* Check the type of a packet. Return 1 if packet is of the specified type, else 0
*/
int
onvm_pkt_is_tcp(struct rte_mbuf* pkt);
int
onvm_pkt_is_udp(struct rte_mbuf* pkt);
int
onvm_pkt_is_ipv4(struct rte_mbuf* pkt);
/**
* Print out a packet or header. Check to be sure DPDK doesn't already do any of these
*/
void
onvm_pkt_print(struct rte_mbuf* pkt); // calls the funcs below
void
onvm_pkt_print_tcp(struct rte_tcp_hdr* hdr);
void
onvm_pkt_print_udp(struct rte_udp_hdr* hdr);
void
onvm_pkt_print_ipv4(struct rte_ipv4_hdr* hdr);
void
onvm_pkt_print_ether(struct rte_ether_hdr* hdr);
/**
* Parsing ip addr of form X.X.X.X into decimal form
*/
int
onvm_pkt_parse_ip(char* ip_str, uint32_t* dest);
/**
* Parse uint32 IP into a string
*/
void
onvm_pkt_parse_char_ip(char* ip_dest, uint32_t ip_src);
/**
* Parsing mac addr of form xx:xx:xx:xx:xx:xx into dest array
*/
int
onvm_pkt_parse_mac(char* mac_str, uint8_t* dest);
/**
* Packet checksum calculation routines
*/
uint32_t
onvm_pkt_get_checksum_offload_flags(uint8_t port_id);
/**
* Set the packet checksums for the passed in pkt
*/
void
onvm_pkt_set_checksums(struct rte_mbuf* pkt);
/**
* Fill the packet UDP header
*/
int
onvm_pkt_fill_udp(struct rte_udp_hdr* udp_hdr, uint16_t src_port, uint16_t dst_port, uint16_t payload_len);
/**
* Fill the packet IP header
*/
int
onvm_pkt_fill_ipv4(struct rte_ipv4_hdr* iph, uint32_t src, uint32_t dst, uint8_t l4_proto);
/**
* Fill the ether header
*/
int
onvm_pkt_fill_ether(struct rte_ether_hdr* eth_hdr, int port, struct rte_ether_addr* dst_mac_addr,
struct port_info* ports);
/**
* Swap the ether header values
*/
int
onvm_pkt_swap_ether_hdr(struct rte_ether_hdr* ether_hdr);
/**
* Swap the IP header values
*/
int
onvm_pkt_swap_ip_hdr(struct rte_ipv4_hdr* ip_hdr);
/**
* Swap the TCP header values
*/
int
onvm_pkt_swap_tcp_hdr(struct rte_tcp_hdr* tcp_hdr);
/**
* Generates a TCP packet with the provided values
*/
struct rte_mbuf*
onvm_pkt_generate_tcp(struct rte_mempool* pktmbuf_pool, struct rte_tcp_hdr* tcp_hdr, struct rte_ipv4_hdr* iph,
struct rte_ether_hdr* eth_hdr, uint8_t* options, size_t option_len, uint8_t* payload,
size_t payload_len);
/**
* Generates a UDP packet with the provided values
*/
struct rte_mbuf*
onvm_pkt_generate_udp(struct rte_mempool* pktmbuf_pool, struct rte_udp_hdr* udp_hdr, struct rte_ipv4_hdr* iph,
struct rte_ether_hdr* eth_hdr, uint8_t* payload, size_t payload_len);
/**
* Generates a sample UDP packet
*/
struct rte_mbuf*
onvm_pkt_generate_udp_sample(struct rte_mempool* pktmbuf_pool);
#endif // _ONVM_PKT_HELPER_H_"