2 *------------------------------------------------------------------
3 * af_packet.c - linux kernel packet interface
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <linux/if_ether.h>
21 #include <linux/if_packet.h>
24 #include <sys/types.h>
26 #include <linux/virtio_net.h>
28 #include <vppinfra/linux/sysfs.h>
29 #include <vlib/vlib.h>
30 #include <vlib/unix/unix.h>
31 #include <vnet/ip/ip.h>
32 #include <vnet/ethernet/ethernet.h>
34 #include <vnet/devices/af_packet/af_packet.h>
36 #define AF_PACKET_DEBUG_SOCKET 0
38 #define AF_PACKET_TX_FRAMES_PER_BLOCK 1024
39 #define AF_PACKET_TX_FRAME_SIZE (2048 * 5)
40 #define AF_PACKET_TX_BLOCK_NR 1
41 #define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \
42 AF_PACKET_TX_FRAMES_PER_BLOCK)
43 #define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \
44 AF_PACKET_TX_FRAMES_PER_BLOCK)
46 #define AF_PACKET_RX_FRAMES_PER_BLOCK 1024
47 #define AF_PACKET_RX_FRAME_SIZE (2048 * 5)
48 #define AF_PACKET_RX_BLOCK_NR 1
49 #define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \
50 AF_PACKET_RX_FRAMES_PER_BLOCK)
51 #define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \
52 AF_PACKET_RX_FRAMES_PER_BLOCK)
54 #if AF_PACKET_DEBUG_SOCKET == 1
55 #define DBG_SOCK(args...) clib_warning(args);
57 #define DBG_SOCK(args...)
60 /*defined in net/if.h but clashes with dpdk headers */
61 unsigned int if_nametoindex (const char *ifname);
63 typedef struct tpacket_req tpacket_req_t;
66 af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
71 af_packet_main_t *apm = &af_packet_main;
72 af_packet_if_t *apif =
73 pool_elt_at_index (apm->interfaces, hi->dev_instance);
75 if (ETHERNET_INTERFACE_FLAG_MTU == (flags & ETHERNET_INTERFACE_FLAG_MTU))
77 s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
79 error = clib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes);
84 clib_error_report (error);
85 return VNET_API_ERROR_SYSCALL_ERROR_1;
93 af_packet_fd_read_ready (clib_file_t * uf)
95 af_packet_main_t *apm = &af_packet_main;
96 vnet_main_t *vnm = vnet_get_main ();
97 u32 idx = uf->private_data;
98 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx);
100 apm->pending_input_bitmap =
101 clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
103 /* Schedule the rx node */
104 vnet_device_input_set_interrupt_pending (vnm, apif->hw_if_index, 0);
110 is_bridge (const u8 * host_if_name)
115 s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
116 dir = opendir ((char *) s);
129 create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
130 tpacket_req_t * tx_req, int *fd, u8 ** ring)
132 af_packet_main_t *apm = &af_packet_main;
134 struct sockaddr_ll sll;
135 int ver = TPACKET_V2;
136 socklen_t req_sz = sizeof (struct tpacket_req);
137 u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
138 tx_req->tp_block_size * tx_req->tp_block_nr;
140 if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
142 DBG_SOCK ("Failed to create socket");
143 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
147 if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt, sizeof (opt)) != 0)
149 DBG_SOCK ("Failed to enable vnet headers on the socket");
150 if ((apm->flags & AF_PACKET_USES_VNET_HEADERS) != 0)
152 /* Should never happen - vnet was already enabled once,
153 * but we fail to reenable it on a new interface
155 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
161 apm->flags |= AF_PACKET_USES_VNET_HEADERS;
163 #ifdef PACKET_QDISC_BYPASS
165 if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) !=
168 DBG_SOCK ("Failed to bypass Linux QDISC");
172 setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0)
174 DBG_SOCK ("Failed to set rx packet interface version");
175 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
181 setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt))) < 0)
183 DBG_SOCK ("Failed to set packet tx ring error handling option");
184 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
189 setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
191 DBG_SOCK ("Failed to set packet rx ring options");
192 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
197 setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
199 DBG_SOCK ("Failed to set packet rx ring options");
200 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
205 mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
207 if (*ring == MAP_FAILED)
209 DBG_SOCK ("mmap failure");
210 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
214 memset (&sll, 0, sizeof (sll));
215 sll.sll_family = PF_PACKET;
216 sll.sll_protocol = htons (ETH_P_ALL);
217 sll.sll_ifindex = host_if_index;
219 if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0)
221 DBG_SOCK ("Failed to bind rx packet socket (error %d)", err);
222 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
235 af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
238 af_packet_main_t *apm = &af_packet_main;
240 struct tpacket_req *rx_req = 0;
241 struct tpacket_req *tx_req = 0;
243 af_packet_if_t *apif = 0;
246 vnet_sw_interface_t *sw;
247 vnet_hw_interface_t *hw;
248 vlib_thread_main_t *tm = vlib_get_thread_main ();
249 vnet_main_t *vnm = vnet_get_main ();
252 u8 *host_if_name_dup = vec_dup (host_if_name);
253 int host_if_index = -1;
255 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
258 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
261 vec_validate (rx_req, 0);
262 rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE;
263 rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE;
264 rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR;
265 rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR;
267 vec_validate (tx_req, 0);
268 tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE;
269 tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE;
270 tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
271 tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
273 host_if_index = if_nametoindex ((const char *) host_if_name);
277 DBG_SOCK ("Wrong host interface name");
278 return VNET_API_ERROR_INVALID_INTERFACE;
281 ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
286 ret = is_bridge (host_if_name);
288 if (ret == 0) /* is a bridge, ignore state */
291 /* So far everything looks good, let's create interface */
292 pool_get (apm->interfaces, apif);
293 if_index = apif - apm->interfaces;
295 apif->host_if_index = host_if_index;
297 apif->rx_ring = ring;
298 apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
299 apif->rx_req = rx_req;
300 apif->tx_req = tx_req;
301 apif->host_if_name = host_if_name_dup;
302 apif->per_interface_next_index = ~0;
303 apif->next_tx_frame = 0;
304 apif->next_rx_frame = 0;
306 if (tm->n_vlib_mains > 1)
307 clib_spinlock_init (&apif->lockp);
310 clib_file_t template = { 0 };
311 template.read_function = af_packet_fd_read_ready;
312 template.file_descriptor = fd;
313 template.private_data = if_index;
314 template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
315 apif->clib_file_index = clib_file_add (&file_main, &template);
318 /*use configured or generate random MAC address */
320 clib_memcpy (hw_addr, hw_addr_set, 6);
323 f64 now = vlib_time_now (vm);
325 rnd = (u32) (now * 1e6);
326 rnd = random_u32 (&rnd);
328 clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd));
333 error = ethernet_register_interface (vnm, af_packet_device_class.index,
334 if_index, hw_addr, &apif->hw_if_index,
335 af_packet_eth_flag_change);
339 memset (apif, 0, sizeof (*apif));
340 pool_put (apm->interfaces, apif);
341 clib_error_report (error);
342 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
346 sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
347 hw = vnet_get_hw_interface (vnm, apif->hw_if_index);
348 apif->sw_if_index = sw->sw_if_index;
349 vnet_hw_interface_set_input_node (vnm, apif->hw_if_index,
350 af_packet_input_node.index);
352 vnet_hw_interface_assign_rx_thread (vnm, apif->hw_if_index, 0, /* queue */
355 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
356 vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
357 VNET_HW_INTERFACE_FLAG_LINK_UP);
359 vnet_hw_interface_set_rx_mode (vnm, apif->hw_if_index, 0,
360 VNET_HW_INTERFACE_RX_MODE_INTERRUPT);
362 mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
365 *sw_if_index = apif->sw_if_index;
370 vec_free (host_if_name_dup);
377 af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
379 vnet_main_t *vnm = vnet_get_main ();
380 af_packet_main_t *apm = &af_packet_main;
381 af_packet_if_t *apif;
386 p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
389 clib_warning ("Host interface %s does not exist", host_if_name);
390 return VNET_API_ERROR_SYSCALL_ERROR_1;
392 apif = pool_elt_at_index (apm->interfaces, p[0]);
393 if_index = apif - apm->interfaces;
395 /* bring down the interface */
396 vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
397 vnet_hw_interface_unassign_rx_thread (vnm, apif->hw_if_index, 0);
400 if (apif->clib_file_index != ~0)
402 clib_file_del (&file_main, file_main.file_pool + apif->clib_file_index);
403 apif->clib_file_index = ~0;
408 ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
409 apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
410 if (munmap (apif->rx_ring, ring_sz))
411 clib_warning ("Host interface %s could not free rx/tx ring",
413 apif->rx_ring = NULL;
414 apif->tx_ring = NULL;
417 vec_free (apif->rx_req);
419 vec_free (apif->tx_req);
422 vec_free (apif->host_if_name);
423 apif->host_if_name = NULL;
424 apif->host_if_index = -1;
426 mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index);
428 ethernet_delete_interface (vnm, apif->hw_if_index);
430 pool_put (apm->interfaces, apif);
435 static clib_error_t *
436 af_packet_init (vlib_main_t * vm)
438 af_packet_main_t *apm = &af_packet_main;
439 vlib_thread_main_t *tm = vlib_get_thread_main ();
441 memset (apm, 0, sizeof (af_packet_main_t));
443 mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
445 vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
446 CLIB_CACHE_LINE_BYTES);
451 VLIB_INIT_FUNCTION (af_packet_init);
454 * fd.io coding-style-patch-verification: ON
457 * eval: (c-set-style "gnu")