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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
*------------------------------------------------------------------
* af_packet.c - linux kernel packet interface
*
* Copyright (c) 2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*------------------------------------------------------------------
*/
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/devices/af_packet/af_packet.h>
#define AF_PACKET_DEBUG_SOCKET 0
#define AF_PACKET_TX_FRAMES_PER_BLOCK 1024
#define AF_PACKET_TX_FRAME_SIZE (2048 * 5)
#define AF_PACKET_TX_BLOCK_NR 1
#define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \
AF_PACKET_TX_FRAMES_PER_BLOCK)
#define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \
AF_PACKET_TX_FRAMES_PER_BLOCK)
#define AF_PACKET_RX_FRAMES_PER_BLOCK 1024
#define AF_PACKET_RX_FRAME_SIZE (2048 * 5)
#define AF_PACKET_RX_BLOCK_NR 1
#define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \
AF_PACKET_RX_FRAMES_PER_BLOCK)
#define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \
AF_PACKET_RX_FRAMES_PER_BLOCK)
#if AF_PACKET_DEBUG_SOCKET == 1
#define DBG_SOCK(args...) clib_warning(args);
#else
#define DBG_SOCK(args...)
#endif
/*defined in net/if.h but clashes with dpdk headers */
unsigned int if_nametoindex(const char *ifname);
typedef struct tpacket_req tpacket_req_t;
static u32
af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
{
/* nothing for now */
return 0;
}
static clib_error_t * af_packet_fd_read_ready (unix_file_t * uf)
{
vlib_main_t * vm = vlib_get_main();
af_packet_main_t * apm = &af_packet_main;
u32 idx = uf->private_data;
apm->pending_input_bitmap = clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
/* Schedule the rx node */
vlib_node_set_interrupt_pending (vm, af_packet_input_node.index);
return 0;
}
static int
create_packet_v2_sock(u8 * name, tpacket_req_t * rx_req, tpacket_req_t * tx_req,
int *fd, u8 ** ring)
{
int ret, err;
struct sockaddr_ll sll;
uint host_if_index;
int ver = TPACKET_V2;
socklen_t req_sz = sizeof(struct tpacket_req);
u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
tx_req->tp_block_size * tx_req->tp_block_nr;
host_if_index = if_nametoindex((const char *) name);
if (!host_if_index)
{
DBG_SOCK("Wrong host interface name");
ret = VNET_API_ERROR_INVALID_INTERFACE;
goto error;
}
if ((*fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
DBG_SOCK("Failed to create socket");
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
if ((err = setsockopt(*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver))) < 0)
{
DBG_SOCK("Failed to set rx packet interface version");
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
int opt = 1;
if ((err = setsockopt(*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof(opt))) < 0)
{
DBG_SOCK("Failed to set rx packet interface version");
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
if ((err = setsockopt(*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
{
DBG_SOCK("Failed to set packet rx ring options");
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
if ((err = setsockopt(*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
{
DBG_SOCK("Failed to set packet rx ring options");
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
*ring = mmap(NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd, 0);
if (*ring == MAP_FAILED)
{
DBG_SOCK("mmap failure");
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
memset(&sll, 0, sizeof(sll));
sll.sll_family = PF_PACKET;
sll.sll_protocol = htons(ETH_P_ALL);
sll.sll_ifindex = host_if_index;
if ((err = bind(*fd, (struct sockaddr *) &sll, sizeof(sll))) < 0)
{
DBG_SOCK("Failed to bind rx packet socket (error %d)", err);
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
return 0;
error:
close(*fd);
return ret;
}
int
af_packet_create_if(vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set)
{
af_packet_main_t * apm = &af_packet_main;
int ret, fd = -1;
struct tpacket_req * rx_req = 0;
struct tpacket_req * tx_req = 0;
u8 * ring = 0;
af_packet_if_t * apif = 0;
u8 hw_addr[6];
clib_error_t * error;
vnet_sw_interface_t * sw;
vnet_main_t *vnm = vnet_get_main();
uword * p;
uword if_index;
p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
if (p)
{
return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
}
vec_validate(rx_req, 0);
rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE;
rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE;
rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR;
vec_validate(tx_req, 0);
tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE;
tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE;
tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
ret = create_packet_v2_sock(host_if_name, rx_req, tx_req, &fd, &ring);
if (ret != 0)
goto error;
/* So far everything looks good, let's create interface */
pool_get (apm->interfaces, apif);
if_index = apif - apm->interfaces;
apif->fd = fd;
apif->rx_ring = ring;
apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
apif->rx_req = rx_req;
apif->tx_req = tx_req;
apif->host_if_name = host_if_name;
{
unix_file_t template = {0};
template.read_function = af_packet_fd_read_ready;
template.file_descriptor = fd;
template.private_data = if_index;
template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
apif->unix_file_index = unix_file_add (&unix_main, &template);
}
/*use configured or generate random MAC address */
if (hw_addr_set)
memcpy(hw_addr, hw_addr_set, 6);
else
{
f64 now = vlib_time_now(vm);
u32 rnd;
rnd = (u32) (now * 1e6);
rnd = random_u32 (&rnd);
memcpy (hw_addr+2, &rnd, sizeof(rnd));
hw_addr[0] = 2;
hw_addr[1] = 0xfe;
}
error = ethernet_register_interface(vnm, af_packet_device_class.index,
if_index, hw_addr, &apif->hw_if_index,
af_packet_eth_flag_change);
if (error)
{
memset(apif, 0, sizeof(*apif));
pool_put(apm->interfaces, apif);
clib_error_report (error);
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
apif->sw_if_index = sw->sw_if_index;
vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
VNET_HW_INTERFACE_FLAG_LINK_UP);
mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name, &if_index, 0);
return 0;
error:
vec_free(host_if_name);
vec_free(rx_req);
vec_free(tx_req);
return ret;
}
static clib_error_t *
af_packet_init (vlib_main_t * vm)
{
af_packet_main_t * apm = &af_packet_main;
memset (apm, 0, sizeof (af_packet_main_t));
mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
return 0;
}
VLIB_INIT_FUNCTION (af_packet_init);
|