Line data Source code
1 : /*
2 : *------------------------------------------------------------------
3 : * af_packet.c - linux kernel packet interface
4 : *
5 : * Copyright (c) 2016 Cisco and/or its affiliates.
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at:
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : *------------------------------------------------------------------
18 : */
19 :
20 : #include <linux/if_ether.h>
21 : #include <linux/if_packet.h>
22 : #include <sys/ioctl.h>
23 : #include <net/if.h>
24 : #include <dirent.h>
25 : #include <sys/stat.h>
26 : #include <sys/types.h>
27 : #include <fcntl.h>
28 :
29 : #include <vppinfra/linux/sysfs.h>
30 : #include <vlib/vlib.h>
31 : #include <vlib/unix/unix.h>
32 : #include <vnet/ip/ip.h>
33 : #include <vnet/devices/netlink.h>
34 : #include <vnet/ethernet/ethernet.h>
35 : #include <vnet/interface/rx_queue_funcs.h>
36 : #include <vnet/interface/tx_queue_funcs.h>
37 :
38 : #include <af_packet/af_packet.h>
39 :
40 : af_packet_main_t af_packet_main;
41 :
42 6719 : VNET_HW_INTERFACE_CLASS (af_packet_ip_device_hw_interface_class, static) = {
43 : .name = "af-packet-ip-device",
44 : .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
45 : };
46 :
47 : #define AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK 1024
48 : #define AF_PACKET_DEFAULT_TX_FRAME_SIZE (2048 * 33) // GSO packet of 64KB
49 : #define AF_PACKET_TX_BLOCK_NR 1
50 :
51 : #define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK_V2 1024
52 : #define AF_PACKET_DEFAULT_RX_FRAME_SIZE_V2 (2048 * 33) // GSO packet of 64KB
53 : #define AF_PACKET_RX_BLOCK_NR_V2 1
54 :
55 : #define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK 32
56 : #define AF_PACKET_DEFAULT_RX_FRAME_SIZE 2048
57 : #define AF_PACKET_RX_BLOCK_NR 160
58 :
59 : /*defined in net/if.h but clashes with dpdk headers */
60 : unsigned int if_nametoindex (const char *ifname);
61 :
62 : static clib_error_t *
63 0 : af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
64 : u32 frame_size)
65 : {
66 : clib_error_t *error, *rv;
67 0 : af_packet_main_t *apm = &af_packet_main;
68 0 : af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance);
69 :
70 0 : error = vnet_netlink_set_link_mtu (apif->host_if_index,
71 0 : frame_size + hi->frame_overhead);
72 :
73 0 : if (error)
74 : {
75 0 : vlib_log_err (apm->log_class, "netlink failed to change MTU: %U",
76 : format_clib_error, error);
77 0 : rv = vnet_error (VNET_ERR_SYSCALL_ERROR_1, "netlink error: %U",
78 : format_clib_error, error);
79 0 : clib_error_free (error);
80 0 : return rv;
81 : }
82 : else
83 0 : apif->host_mtu = frame_size + hi->frame_overhead;
84 0 : return 0;
85 : }
86 :
87 : static int
88 252 : af_packet_read_mtu (af_packet_if_t *apif)
89 : {
90 252 : af_packet_main_t *apm = &af_packet_main;
91 : clib_error_t *error;
92 252 : error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu);
93 252 : if (error)
94 : {
95 0 : vlib_log_err (apm->log_class, "netlink failed to get MTU: %U",
96 : format_clib_error, error);
97 0 : clib_error_free (error);
98 0 : return VNET_API_ERROR_SYSCALL_ERROR_1;
99 : }
100 252 : return 0;
101 : }
102 :
103 : static clib_error_t *
104 193529 : af_packet_fd_read_ready (clib_file_t * uf)
105 : {
106 193529 : vnet_main_t *vnm = vnet_get_main ();
107 :
108 : /* Schedule the rx node */
109 193529 : vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data);
110 193529 : return 0;
111 : }
112 :
113 : static clib_error_t *
114 0 : af_packet_fd_error (clib_file_t *uf)
115 : {
116 0 : af_packet_main_t *apm = &af_packet_main;
117 0 : clib_error_t *err = 0;
118 : u64 u64;
119 :
120 0 : int ret = read (uf->file_descriptor, (char *) &u64, sizeof (u64));
121 :
122 0 : if (ret < 0)
123 : {
124 0 : err = clib_error_return_unix (0, "");
125 0 : vlib_log_notice (apm->log_class, "fd %u %U", uf->file_descriptor,
126 : format_clib_error, err);
127 0 : clib_error_free (err);
128 : }
129 :
130 0 : return 0;
131 : }
132 :
133 : static int
134 252 : is_bridge (const u8 * host_if_name)
135 : {
136 : u8 *s;
137 252 : DIR *dir = NULL;
138 :
139 252 : s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
140 252 : dir = opendir ((char *) s);
141 252 : vec_free (s);
142 :
143 252 : if (dir)
144 : {
145 0 : closedir (dir);
146 0 : return 0;
147 : }
148 :
149 252 : return -1;
150 : }
151 :
152 : static void
153 252 : af_packet_set_rx_queues (vlib_main_t *vm, af_packet_if_t *apif)
154 : {
155 252 : vnet_main_t *vnm = vnet_get_main ();
156 : af_packet_queue_t *rx_queue;
157 :
158 252 : vnet_hw_if_set_input_node (vnm, apif->hw_if_index,
159 : af_packet_input_node.index);
160 :
161 504 : vec_foreach (rx_queue, apif->rx_queues)
162 : {
163 504 : rx_queue->queue_index = vnet_hw_if_register_rx_queue (
164 252 : vnm, apif->hw_if_index, rx_queue->queue_id, VNET_HW_IF_RXQ_THREAD_ANY);
165 :
166 : {
167 252 : clib_file_t template = { 0 };
168 252 : template.read_function = af_packet_fd_read_ready;
169 252 : template.error_function = af_packet_fd_error;
170 252 : template.file_descriptor = rx_queue->fd;
171 252 : template.private_data = rx_queue->queue_index;
172 252 : template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
173 252 : template.description =
174 252 : format (0, "%U queue %u", format_af_packet_device_name,
175 252 : apif->dev_instance, rx_queue->queue_id);
176 252 : rx_queue->clib_file_index = clib_file_add (&file_main, &template);
177 : }
178 252 : vnet_hw_if_set_rx_queue_file_index (vnm, rx_queue->queue_index,
179 : rx_queue->clib_file_index);
180 252 : vnet_hw_if_set_rx_queue_mode (vnm, rx_queue->queue_index,
181 : VNET_HW_IF_RX_MODE_INTERRUPT);
182 252 : rx_queue->mode = VNET_HW_IF_RX_MODE_INTERRUPT;
183 : }
184 252 : vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
185 252 : }
186 :
187 : static void
188 252 : af_packet_set_tx_queues (vlib_main_t *vm, af_packet_if_t *apif)
189 : {
190 252 : vnet_main_t *vnm = vnet_get_main ();
191 252 : af_packet_main_t *apm = &af_packet_main;
192 : af_packet_queue_t *tx_queue;
193 :
194 504 : vec_foreach (tx_queue, apif->tx_queues)
195 : {
196 252 : tx_queue->queue_index = vnet_hw_if_register_tx_queue (
197 252 : vnm, apif->hw_if_index, tx_queue->queue_id);
198 : }
199 :
200 252 : if (apif->num_txqs == 0)
201 : {
202 0 : vlib_log_err (apm->log_class, "Interface %U has 0 txq",
203 : format_vnet_hw_if_index_name, vnm, apif->hw_if_index);
204 0 : return;
205 : }
206 :
207 504 : for (u32 j = 0; j < vlib_get_n_threads (); j++)
208 : {
209 252 : u32 qi = apif->tx_queues[j % apif->num_txqs].queue_index;
210 252 : vnet_hw_if_tx_queue_assign_thread (vnm, qi, j);
211 : }
212 :
213 252 : vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
214 : }
215 :
216 : static int
217 252 : create_packet_sock (int host_if_index, tpacket_req_u_t *rx_req,
218 : tpacket_req_u_t *tx_req, int *fd, af_packet_ring_t *ring,
219 : u32 fanout_id, af_packet_if_flags_t *flags, int ver)
220 : {
221 252 : af_packet_main_t *apm = &af_packet_main;
222 : struct sockaddr_ll sll;
223 252 : socklen_t req_sz = sizeof (tpacket_req3_t);
224 : int ret;
225 252 : u32 ring_sz = 0;
226 :
227 252 : if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
228 : {
229 0 : vlib_log_err (apm->log_class,
230 : "Failed to create AF_PACKET socket: %s (errno %d)",
231 : strerror (errno), errno);
232 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
233 0 : goto error;
234 : }
235 :
236 : /* bind before rx ring is cfged so we don't receive packets from other interfaces */
237 252 : clib_memset (&sll, 0, sizeof (sll));
238 252 : sll.sll_family = PF_PACKET;
239 252 : sll.sll_protocol = htons (ETH_P_ALL);
240 252 : sll.sll_ifindex = host_if_index;
241 252 : if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
242 : {
243 0 : vlib_log_err (apm->log_class,
244 : "Failed to bind rx packet socket: %s (errno %d)",
245 : strerror (errno), errno);
246 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
247 0 : goto error;
248 : }
249 :
250 252 : if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
251 : {
252 0 : vlib_log_err (apm->log_class,
253 : "Failed to set rx packet interface version: %s (errno %d)",
254 : strerror (errno), errno);
255 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
256 0 : goto error;
257 : }
258 :
259 252 : int opt = 1;
260 252 : if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
261 : {
262 0 : vlib_log_err (
263 : apm->log_class,
264 : "Failed to set packet tx ring error handling option: %s (errno %d)",
265 : strerror (errno), errno);
266 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
267 0 : goto error;
268 : }
269 :
270 252 : if (*flags & AF_PACKET_IF_FLAGS_CKSUM_GSO)
271 : {
272 :
273 144 : int opt2 = 1;
274 144 : if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt2, sizeof (opt2)) <
275 : 0)
276 : {
277 : // remove the flag
278 0 : *flags &= ~AF_PACKET_IF_FLAGS_CKSUM_GSO;
279 0 : vlib_log_debug (apm->log_class,
280 : "Failed to set packet vnet hdr error handling "
281 : "option: %s (errno %d)",
282 : strerror (errno), errno);
283 : }
284 : }
285 :
286 : #if defined(PACKET_QDISC_BYPASS)
287 252 : if (*flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS)
288 : /* Introduced with Linux 3.14 so the ifdef should eventually be removed */
289 252 : if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) <
290 : 0)
291 : {
292 : // remove the flag
293 0 : *flags &= ~AF_PACKET_IF_FLAGS_QDISC_BYPASS;
294 0 : vlib_log_debug (apm->log_class,
295 : "Failed to set qdisc bypass error "
296 : "handling option: %s (errno %d)",
297 : strerror (errno), errno);
298 : }
299 : #endif
300 :
301 252 : if (rx_req)
302 : {
303 252 : if (*flags & AF_PACKET_IF_FLAGS_FANOUT)
304 : {
305 0 : int fanout = ((fanout_id & 0xffff) | ((PACKET_FANOUT_HASH) << 16));
306 0 : if (setsockopt (*fd, SOL_PACKET, PACKET_FANOUT, &fanout,
307 : sizeof (fanout)) < 0)
308 : {
309 : // remove the flag
310 0 : *flags &= ~AF_PACKET_IF_FLAGS_FANOUT;
311 0 : vlib_log_err (apm->log_class,
312 : "Failed to set fanout options: %s (errno %d)",
313 : strerror (errno), errno);
314 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
315 0 : goto error;
316 : }
317 : }
318 252 : if (ver == TPACKET_V2)
319 : {
320 120 : req_sz = sizeof (tpacket_req_t);
321 120 : ring_sz += rx_req->req.tp_block_size * rx_req->req.tp_block_nr;
322 : }
323 : else
324 132 : ring_sz += rx_req->req3.tp_block_size * rx_req->req3.tp_block_nr;
325 252 : if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
326 : {
327 0 : vlib_log_err (apm->log_class,
328 : "Failed to set packet rx ring options: %s (errno %d)",
329 : strerror (errno), errno);
330 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
331 0 : goto error;
332 : }
333 : }
334 :
335 252 : if (tx_req)
336 : {
337 252 : if (ver == TPACKET_V2)
338 : {
339 120 : req_sz = sizeof (tpacket_req_t);
340 120 : ring_sz += tx_req->req.tp_block_size * tx_req->req.tp_block_nr;
341 : }
342 : else
343 132 : ring_sz += tx_req->req3.tp_block_size * tx_req->req3.tp_block_nr;
344 252 : if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
345 : {
346 0 : vlib_log_err (apm->log_class,
347 : "Failed to set packet tx ring options: %s (errno %d)",
348 : strerror (errno), errno);
349 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
350 0 : goto error;
351 : }
352 : }
353 252 : ring->ring_start_addr = mmap (NULL, ring_sz, PROT_READ | PROT_WRITE,
354 : MAP_SHARED | MAP_LOCKED, *fd, 0);
355 252 : if (ring->ring_start_addr == MAP_FAILED)
356 : {
357 0 : vlib_log_err (apm->log_class, "mmap failure: %s (errno %d)",
358 : strerror (errno), errno);
359 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
360 0 : goto error;
361 : }
362 :
363 252 : ring->ring_size = ring_sz;
364 :
365 252 : return 0;
366 0 : error:
367 0 : if (*fd >= 0)
368 : {
369 0 : close (*fd);
370 0 : *fd = -1;
371 : }
372 0 : return ret;
373 : }
374 :
375 : int
376 252 : af_packet_queue_init (vlib_main_t *vm, af_packet_if_t *apif,
377 : af_packet_create_if_arg_t *arg,
378 : af_packet_queue_t *rx_queue, af_packet_queue_t *tx_queue,
379 : u8 queue_id)
380 : {
381 252 : af_packet_main_t *apm = &af_packet_main;
382 252 : tpacket_req_u_t *rx_req = 0;
383 252 : tpacket_req_u_t *tx_req = 0;
384 252 : int ret, fd = -1;
385 252 : af_packet_ring_t ring = { 0 };
386 252 : u8 *ring_addr = 0;
387 : u32 rx_frames_per_block, tx_frames_per_block;
388 : u32 rx_frame_size, tx_frame_size;
389 252 : u32 i = 0;
390 :
391 252 : if (rx_queue)
392 : {
393 504 : rx_frames_per_block = arg->rx_frames_per_block ?
394 504 : arg->rx_frames_per_block :
395 252 : ((apif->version == TPACKET_V3) ?
396 252 : AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK :
397 : AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK_V2);
398 :
399 252 : rx_frame_size =
400 252 : arg->rx_frame_size ?
401 504 : arg->rx_frame_size :
402 252 : ((apif->version == TPACKET_V3) ? AF_PACKET_DEFAULT_RX_FRAME_SIZE :
403 : AF_PACKET_DEFAULT_RX_FRAME_SIZE_V2);
404 252 : vec_validate (rx_queue->rx_req, 0);
405 252 : rx_queue->rx_req->req.tp_block_size =
406 252 : rx_frame_size * rx_frames_per_block;
407 252 : rx_queue->rx_req->req.tp_frame_size = rx_frame_size;
408 504 : rx_queue->rx_req->req.tp_block_nr = (apif->version == TPACKET_V3) ?
409 252 : AF_PACKET_RX_BLOCK_NR :
410 : AF_PACKET_RX_BLOCK_NR_V2;
411 252 : rx_queue->rx_req->req.tp_frame_nr =
412 252 : rx_queue->rx_req->req.tp_block_nr * rx_frames_per_block;
413 252 : if (apif->version == TPACKET_V3)
414 : {
415 132 : rx_queue->rx_req->req3.tp_retire_blk_tov = 1; // 1 ms block timout
416 132 : rx_queue->rx_req->req3.tp_feature_req_word = 0;
417 132 : rx_queue->rx_req->req3.tp_sizeof_priv = 0;
418 : }
419 252 : rx_req = rx_queue->rx_req;
420 : }
421 252 : if (tx_queue)
422 : {
423 504 : tx_frames_per_block = arg->tx_frames_per_block ?
424 252 : arg->tx_frames_per_block :
425 : AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK;
426 252 : tx_frame_size = arg->tx_frame_size ? arg->tx_frame_size :
427 : AF_PACKET_DEFAULT_TX_FRAME_SIZE;
428 :
429 252 : vec_validate (tx_queue->tx_req, 0);
430 252 : tx_queue->tx_req->req.tp_block_size =
431 252 : tx_frame_size * tx_frames_per_block;
432 252 : tx_queue->tx_req->req.tp_frame_size = tx_frame_size;
433 252 : tx_queue->tx_req->req.tp_block_nr = AF_PACKET_TX_BLOCK_NR;
434 252 : tx_queue->tx_req->req.tp_frame_nr =
435 : AF_PACKET_TX_BLOCK_NR * tx_frames_per_block;
436 252 : if (apif->version == TPACKET_V3)
437 : {
438 132 : tx_queue->tx_req->req3.tp_retire_blk_tov = 0;
439 132 : tx_queue->tx_req->req3.tp_sizeof_priv = 0;
440 132 : tx_queue->tx_req->req3.tp_feature_req_word = 0;
441 : }
442 252 : tx_req = tx_queue->tx_req;
443 : }
444 :
445 252 : if (rx_queue || tx_queue)
446 : {
447 : ret =
448 252 : create_packet_sock (apif->host_if_index, rx_req, tx_req, &fd, &ring,
449 252 : apif->dev_instance, &arg->flags, apif->version);
450 :
451 252 : if (ret != 0)
452 0 : goto error;
453 :
454 252 : vec_add1 (apif->rings, ring);
455 252 : ring_addr = ring.ring_start_addr;
456 : }
457 :
458 252 : if (rx_queue)
459 : {
460 252 : rx_queue->fd = fd;
461 252 : vec_validate (rx_queue->rx_ring, rx_queue->rx_req->req.tp_block_nr - 1);
462 21492 : vec_foreach_index (i, rx_queue->rx_ring)
463 : {
464 21240 : rx_queue->rx_ring[i] =
465 21240 : ring_addr + i * rx_queue->rx_req->req.tp_block_size;
466 : }
467 :
468 252 : rx_queue->next_rx_block = 0;
469 252 : rx_queue->queue_id = queue_id;
470 252 : rx_queue->is_rx_pending = 0;
471 252 : ring_addr = ring_addr + rx_queue->rx_req->req.tp_block_size *
472 252 : rx_queue->rx_req->req.tp_block_nr;
473 : }
474 :
475 252 : if (tx_queue)
476 : {
477 252 : tx_queue->fd = fd;
478 252 : vec_validate (tx_queue->tx_ring, tx_queue->tx_req->req.tp_block_nr - 1);
479 504 : vec_foreach_index (i, tx_queue->tx_ring)
480 : {
481 252 : tx_queue->tx_ring[i] =
482 252 : ring_addr + i * tx_queue->tx_req->req.tp_block_size;
483 : }
484 :
485 252 : tx_queue->next_tx_frame = 0;
486 252 : tx_queue->queue_id = queue_id;
487 252 : tx_queue->is_tx_pending = 0;
488 252 : clib_spinlock_init (&tx_queue->lockp);
489 : }
490 :
491 252 : return 0;
492 0 : error:
493 0 : vlib_log_err (apm->log_class, "Failed to set queue %u error", queue_id);
494 0 : if (rx_queue)
495 0 : vec_free (rx_queue->rx_req);
496 0 : if (tx_queue)
497 0 : vec_free (tx_queue->tx_req);
498 0 : return ret;
499 : }
500 :
501 : int
502 252 : af_packet_device_init (vlib_main_t *vm, af_packet_if_t *apif,
503 : af_packet_create_if_arg_t *args)
504 : {
505 252 : af_packet_main_t *apm = &af_packet_main;
506 252 : af_packet_queue_t *rx_queue = 0;
507 252 : af_packet_queue_t *tx_queue = 0;
508 252 : u16 nq = clib_min (args->num_rxqs, args->num_txqs);
509 252 : u16 i = 0;
510 252 : int ret = 0;
511 :
512 : // enable fanout feature for multi-rxqs
513 252 : if (args->num_rxqs > 1)
514 0 : args->flags |= AF_PACKET_IF_FLAGS_FANOUT;
515 :
516 252 : vec_validate (apif->rx_queues, args->num_rxqs - 1);
517 252 : vec_validate (apif->tx_queues, args->num_txqs - 1);
518 :
519 504 : for (; i < nq; i++)
520 : {
521 252 : rx_queue = vec_elt_at_index (apif->rx_queues, i);
522 252 : tx_queue = vec_elt_at_index (apif->tx_queues, i);
523 252 : ret = af_packet_queue_init (vm, apif, args, rx_queue, tx_queue, i);
524 252 : if (ret != 0)
525 0 : goto error;
526 : }
527 :
528 252 : if (args->num_rxqs > args->num_txqs)
529 : {
530 0 : for (; i < args->num_rxqs; i++)
531 : {
532 0 : rx_queue = vec_elt_at_index (apif->rx_queues, i);
533 0 : ret = af_packet_queue_init (vm, apif, args, rx_queue, 0, i);
534 0 : if (ret != 0)
535 0 : goto error;
536 : }
537 : }
538 252 : else if (args->num_txqs > args->num_rxqs)
539 : {
540 0 : for (; i < args->num_txqs; i++)
541 : {
542 0 : tx_queue = vec_elt_at_index (apif->tx_queues, i);
543 0 : ret = af_packet_queue_init (vm, apif, args, 0, tx_queue, i);
544 0 : if (ret != 0)
545 0 : goto error;
546 : }
547 : }
548 :
549 252 : apif->num_rxqs = args->num_rxqs;
550 252 : apif->num_txqs = args->num_txqs;
551 :
552 252 : return 0;
553 0 : error:
554 0 : vlib_log_err (apm->log_class, "Failed to init device error");
555 0 : return ret;
556 : }
557 :
558 : int
559 252 : af_packet_create_if (af_packet_create_if_arg_t *arg)
560 : {
561 252 : af_packet_main_t *apm = &af_packet_main;
562 252 : vlib_main_t *vm = vlib_get_main ();
563 252 : int fd2 = -1;
564 : struct ifreq ifr;
565 252 : af_packet_if_t *apif = 0;
566 : u8 hw_addr[6];
567 : vnet_sw_interface_t *sw;
568 252 : vnet_main_t *vnm = vnet_get_main ();
569 252 : vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE;
570 : uword *p;
571 : uword if_index;
572 252 : u8 *host_if_name_dup = 0;
573 252 : int host_if_index = -1;
574 252 : int ret = 0;
575 :
576 252 : p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name);
577 252 : if (p)
578 : {
579 0 : apif = vec_elt_at_index (apm->interfaces, p[0]);
580 0 : arg->sw_if_index = apif->sw_if_index;
581 0 : return VNET_API_ERROR_IF_ALREADY_EXISTS;
582 : }
583 :
584 252 : host_if_name_dup = vec_dup (arg->host_if_name);
585 :
586 : /*
587 : * make sure host side of interface is 'UP' before binding AF_PACKET
588 : * socket on it.
589 : */
590 252 : if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
591 : {
592 0 : vlib_log_debug (apm->log_class,
593 : "Failed to create AF_UNIX socket: %s (errno %d)",
594 : strerror (errno), errno);
595 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
596 0 : goto error;
597 : }
598 :
599 252 : clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name,
600 : vec_len (arg->host_if_name));
601 252 : if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
602 : {
603 0 : vlib_log_debug (
604 : apm->log_class,
605 : "Failed to retrieve the interface (%s) index: %s (errno %d)",
606 : arg->host_if_name, strerror (errno), errno);
607 0 : ret = VNET_API_ERROR_INVALID_INTERFACE;
608 0 : goto error;
609 : }
610 :
611 252 : host_if_index = ifr.ifr_ifindex;
612 252 : if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
613 : {
614 0 : vlib_log_debug (apm->log_class,
615 : "Failed to get the active flag: %s (errno %d)",
616 : strerror (errno), errno);
617 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
618 0 : goto error;
619 : }
620 :
621 252 : if (!(ifr.ifr_flags & IFF_UP))
622 : {
623 0 : ifr.ifr_flags |= IFF_UP;
624 0 : if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
625 : {
626 0 : vlib_log_debug (apm->log_class,
627 : "Failed to set the active flag: %s (errno %d)",
628 : strerror (errno), errno);
629 0 : ret = VNET_API_ERROR_SYSCALL_ERROR_1;
630 0 : goto error;
631 : }
632 : }
633 :
634 252 : if (fd2 > -1)
635 : {
636 252 : close (fd2);
637 252 : fd2 = -1;
638 : }
639 :
640 252 : ret = is_bridge (arg->host_if_name);
641 252 : if (ret == 0) /* is a bridge, ignore state */
642 0 : host_if_index = -1;
643 :
644 : /* So far everything looks good, let's create interface */
645 252 : pool_get (apm->interfaces, apif);
646 252 : if_index = apif - apm->interfaces;
647 :
648 252 : apif->dev_instance = if_index;
649 252 : apif->host_if_index = host_if_index;
650 252 : apif->host_if_name = host_if_name_dup;
651 252 : apif->per_interface_next_index = ~0;
652 252 : apif->mode = arg->mode;
653 :
654 252 : if (arg->is_v2)
655 120 : apif->version = TPACKET_V2;
656 : else
657 132 : apif->version = TPACKET_V3;
658 :
659 252 : ret = af_packet_device_init (vm, apif, arg);
660 252 : if (ret != 0)
661 0 : goto error;
662 :
663 252 : ret = af_packet_read_mtu (apif);
664 252 : if (ret != 0)
665 0 : goto error;
666 :
667 :
668 252 : if (apif->mode != AF_PACKET_IF_MODE_IP)
669 : {
670 252 : vnet_eth_interface_registration_t eir = {};
671 : /*use configured or generate random MAC address */
672 252 : if (arg->hw_addr)
673 0 : clib_memcpy (hw_addr, arg->hw_addr, 6);
674 : else
675 : {
676 252 : f64 now = vlib_time_now (vm);
677 : u32 rnd;
678 252 : rnd = (u32) (now * 1e6);
679 252 : rnd = random_u32 (&rnd);
680 :
681 252 : clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
682 252 : hw_addr[0] = 2;
683 252 : hw_addr[1] = 0xfe;
684 : }
685 :
686 252 : eir.dev_class_index = af_packet_device_class.index;
687 252 : eir.dev_instance = apif->dev_instance;
688 252 : eir.address = hw_addr;
689 252 : eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size;
690 252 : apif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
691 : }
692 : else
693 : {
694 0 : apif->hw_if_index = vnet_register_interface (
695 0 : vnm, af_packet_device_class.index, apif->dev_instance,
696 0 : af_packet_ip_device_hw_interface_class.index, apif->dev_instance);
697 : }
698 :
699 252 : sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
700 252 : apif->sw_if_index = sw->sw_if_index;
701 :
702 252 : af_packet_set_rx_queues (vm, apif);
703 252 : af_packet_set_tx_queues (vm, apif);
704 :
705 252 : if (arg->flags & AF_PACKET_IF_FLAGS_FANOUT)
706 0 : apif->is_fanout_enabled = 1;
707 :
708 252 : apif->is_qdisc_bypass_enabled =
709 252 : (arg->flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS);
710 :
711 252 : if (arg->flags & AF_PACKET_IF_FLAGS_CKSUM_GSO)
712 144 : apif->is_cksum_gso_enabled = 1;
713 :
714 252 : if (apif->is_cksum_gso_enabled)
715 144 : caps |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_IP4_CKSUM |
716 : VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
717 :
718 252 : vnet_hw_if_set_caps (vnm, apif->hw_if_index, caps);
719 252 : vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
720 : VNET_HW_INTERFACE_FLAG_LINK_UP);
721 :
722 252 : mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
723 : 0);
724 252 : arg->sw_if_index = apif->sw_if_index;
725 :
726 252 : return 0;
727 :
728 0 : error:
729 0 : if (fd2 > -1)
730 : {
731 0 : close (fd2);
732 0 : fd2 = -1;
733 : }
734 0 : vec_free (host_if_name_dup);
735 0 : if (apif)
736 : {
737 0 : memset (apif, 0, sizeof (*apif));
738 0 : pool_put (apm->interfaces, apif);
739 : }
740 0 : return ret;
741 : }
742 :
743 : static int
744 252 : af_packet_rx_queue_free (af_packet_if_t *apif, af_packet_queue_t *rx_queue)
745 : {
746 252 : clib_file_del_by_index (&file_main, rx_queue->clib_file_index);
747 252 : close (rx_queue->fd);
748 252 : rx_queue->fd = -1;
749 252 : rx_queue->rx_ring = NULL;
750 252 : vec_free (rx_queue->rx_req);
751 252 : rx_queue->rx_req = NULL;
752 252 : return 0;
753 : }
754 :
755 : static int
756 252 : af_packet_tx_queue_free (af_packet_if_t *apif, af_packet_queue_t *tx_queue)
757 : {
758 252 : close (tx_queue->fd);
759 252 : tx_queue->fd = -1;
760 252 : clib_spinlock_free (&tx_queue->lockp);
761 252 : tx_queue->tx_ring = NULL;
762 252 : vec_free (tx_queue->tx_req);
763 252 : tx_queue->tx_req = NULL;
764 252 : return 0;
765 : }
766 :
767 : static int
768 252 : af_packet_ring_free (af_packet_if_t *apif, af_packet_ring_t *ring)
769 : {
770 252 : af_packet_main_t *apm = &af_packet_main;
771 :
772 252 : if (ring)
773 : {
774 : // FIXME: unmap the memory
775 252 : if (munmap (ring->ring_start_addr, ring->ring_size))
776 0 : vlib_log_warn (apm->log_class,
777 : "Host interface %s could not free ring %p of size %u",
778 : apif->host_if_name, ring->ring_start_addr,
779 : ring->ring_size);
780 : else
781 252 : ring->ring_start_addr = 0;
782 : }
783 :
784 252 : return 0;
785 : }
786 :
787 : int
788 252 : af_packet_delete_if (u8 *host_if_name)
789 : {
790 252 : vnet_main_t *vnm = vnet_get_main ();
791 252 : af_packet_main_t *apm = &af_packet_main;
792 : af_packet_if_t *apif;
793 : af_packet_queue_t *rx_queue;
794 : af_packet_queue_t *tx_queue;
795 : af_packet_ring_t *ring;
796 : uword *p;
797 :
798 252 : p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
799 252 : if (p == NULL)
800 : {
801 0 : vlib_log_warn (apm->log_class, "Host interface %s does not exist",
802 : host_if_name);
803 0 : return VNET_API_ERROR_SYSCALL_ERROR_1;
804 : }
805 252 : apif = pool_elt_at_index (apm->interfaces, p[0]);
806 :
807 : /* bring down the interface */
808 252 : vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
809 252 : if (apif->mode != AF_PACKET_IF_MODE_IP)
810 252 : ethernet_delete_interface (vnm, apif->hw_if_index);
811 : else
812 0 : vnet_delete_hw_interface (vnm, apif->hw_if_index);
813 :
814 : /* clean up */
815 504 : vec_foreach (rx_queue, apif->rx_queues)
816 252 : af_packet_rx_queue_free (apif, rx_queue);
817 504 : vec_foreach (tx_queue, apif->tx_queues)
818 252 : af_packet_tx_queue_free (apif, tx_queue);
819 504 : vec_foreach (ring, apif->rings)
820 252 : af_packet_ring_free (apif, ring);
821 :
822 252 : vec_free (apif->rx_queues);
823 252 : apif->rx_queues = NULL;
824 252 : vec_free (apif->tx_queues);
825 252 : apif->tx_queues = NULL;
826 252 : vec_free (apif->rings);
827 252 : apif->rings = NULL;
828 :
829 252 : vec_free (apif->host_if_name);
830 252 : apif->host_if_name = NULL;
831 252 : apif->host_if_index = -1;
832 :
833 252 : mhash_unset (&apm->if_index_by_host_if_name, host_if_name, p);
834 :
835 252 : memset (apif, 0, sizeof (*apif));
836 252 : pool_put (apm->interfaces, apif);
837 :
838 252 : return 0;
839 : }
840 :
841 : int
842 0 : af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set)
843 : {
844 : // deprecated ...
845 0 : return 0;
846 : }
847 :
848 : int
849 276 : af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
850 : {
851 276 : af_packet_main_t *apm = &af_packet_main;
852 : af_packet_if_t *apif;
853 276 : af_packet_if_detail_t *r_af_packet_ifs = NULL;
854 276 : af_packet_if_detail_t *af_packet_if = NULL;
855 :
856 528 : pool_foreach (apif, apm->interfaces)
857 : {
858 252 : vec_add2 (r_af_packet_ifs, af_packet_if, 1);
859 252 : af_packet_if->sw_if_index = apif->sw_if_index;
860 252 : if (apif->host_if_name)
861 : {
862 252 : clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
863 : MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
864 : strlen ((const char *) apif->host_if_name)));
865 : }
866 : }
867 :
868 276 : *out_af_packet_ifs = r_af_packet_ifs;
869 :
870 276 : return 0;
871 : }
872 :
873 : static clib_error_t *
874 559 : af_packet_init (vlib_main_t * vm)
875 : {
876 559 : af_packet_main_t *apm = &af_packet_main;
877 559 : vlib_thread_main_t *tm = vlib_get_thread_main ();
878 :
879 559 : clib_memset (apm, 0, sizeof (af_packet_main_t));
880 :
881 559 : mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
882 :
883 559 : vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
884 : CLIB_CACHE_LINE_BYTES);
885 :
886 559 : apm->log_class = vlib_log_register_class ("af_packet", 0);
887 559 : vlib_log_debug (apm->log_class, "initialized");
888 :
889 559 : return 0;
890 : }
891 :
892 1119 : VLIB_INIT_FUNCTION (af_packet_init);
893 :
894 : /*
895 : * fd.io coding-style-patch-verification: ON
896 : *
897 : * Local Variables:
898 : * eval: (c-set-style "gnu")
899 : * End:
900 : */
|