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_packet.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
24 #include <net/if_arp.h>
26 #include <vlib/vlib.h>
27 #include <vlib/unix/unix.h>
28 #include <vnet/ip/ip.h>
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/ip/ip4_packet.h>
31 #include <vnet/ip/ip6_packet.h>
32 #include <vnet/ip/ip_psh_cksum.h>
33 #include <vnet/tcp/tcp_packet.h>
34 #include <vnet/udp/udp_packet.h>
36 #include <vnet/devices/af_packet/af_packet.h>
37 #include <vnet/devices/virtio/virtio_std.h>
39 #define foreach_af_packet_tx_func_error \
40 _(FRAME_NOT_READY, "tx frame not ready") \
41 _(TXRING_EAGAIN, "tx sendto temporary failure") \
42 _(TXRING_FATAL, "tx sendto fatal failure") \
43 _(TXRING_OVERRUN, "tx ring overrun")
47 #define _(f,s) AF_PACKET_TX_ERROR_##f,
48 foreach_af_packet_tx_func_error
51 } af_packet_tx_func_error_t;
53 static char *af_packet_tx_func_error_strings[] = {
55 foreach_af_packet_tx_func_error
60 #ifndef CLIB_MARCH_VARIANT
62 format_af_packet_device_name (u8 * s, va_list * args)
64 u32 i = va_arg (*args, u32);
65 af_packet_main_t *apm = &af_packet_main;
66 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
68 s = format (s, "host-%s", apif->host_if_name);
71 #endif /* CLIB_MARCH_VARIANT */
74 format_af_packet_device (u8 * s, va_list * args)
76 u32 dev_instance = va_arg (*args, u32);
77 u32 indent = format_get_indent (s);
78 int __clib_unused verbose = va_arg (*args, int);
80 af_packet_main_t *apm = &af_packet_main;
81 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, dev_instance);
82 clib_spinlock_lock_if_init (&apif->lockp);
83 u32 tx_block_sz = apif->tx_req->tp_block_size;
84 u32 tx_frame_sz = apif->tx_req->tp_frame_size;
85 u32 tx_frame_nr = apif->tx_req->tp_frame_nr;
86 u32 tx_block_nr = apif->tx_req->tp_block_nr;
87 u32 rx_block_size = apif->rx_req->tp_block_size;
88 u32 rx_frame_size = apif->rx_req->tp_frame_size;
89 u32 rx_frame_nr = apif->rx_req->tp_frame_nr;
90 u32 rx_block_nr = apif->rx_req->tp_block_nr;
92 u8 *tx_block_start = apif->tx_ring[block];
93 u32 tx_frame = apif->next_tx_frame;
96 s = format (s, "Linux PACKET socket interface\n");
97 s = format (s, "%UTX block size:%d nr:%d TX frame size:%d nr:%d\n",
98 format_white_space, indent, tx_block_sz, tx_block_nr,
99 tx_frame_sz, tx_frame_nr);
100 s = format (s, "%URX block size:%d nr:%d RX frame size:%d nr:%d\n",
101 format_white_space, indent, rx_block_size, rx_block_nr,
102 rx_frame_size, rx_frame_nr);
103 s = format (s, "%Unext frame:%d\n", format_white_space, indent,
104 apif->next_tx_frame);
106 int n_send_req = 0, n_avail = 0, n_sending = 0, n_tot = 0, n_wrong = 0;
109 tph = (tpacket3_hdr_t *) (tx_block_start + tx_frame * tx_frame_sz);
110 tx_frame = (tx_frame + 1) % tx_frame_nr;
111 if (tph->tp_status == 0)
113 else if (tph->tp_status & TP_STATUS_SEND_REQUEST)
115 else if (tph->tp_status & TP_STATUS_SENDING)
121 while (tx_frame != apif->next_tx_frame);
122 s = format (s, "%Uavailable:%d request:%d sending:%d wrong:%d total:%d\n",
123 format_white_space, indent, n_avail, n_send_req, n_sending,
126 clib_spinlock_unlock_if_init (&apif->lockp);
131 format_af_packet_tx_trace (u8 * s, va_list * args)
133 s = format (s, "Unimplemented...");
137 static_always_inline void
138 fill_gso_offload (vlib_buffer_t *b0, vnet_virtio_net_hdr_t *vnet_hdr)
140 vnet_buffer_oflags_t oflags = vnet_buffer (b0)->oflags;
141 if (b0->flags & VNET_BUFFER_F_IS_IP4)
144 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
145 vnet_hdr->gso_size = vnet_buffer2 (b0)->gso_size;
147 vnet_buffer (b0)->l4_hdr_offset + vnet_buffer2 (b0)->gso_l4_hdr_sz;
148 vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
149 vnet_hdr->csum_start = vnet_buffer (b0)->l4_hdr_offset; // 0x22;
150 vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
151 ip4 = (ip4_header_t *) (vlib_buffer_get_current (b0) +
152 vnet_buffer (b0)->l3_hdr_offset);
153 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
154 ip4->checksum = ip4_header_checksum (ip4);
156 else if (b0->flags & VNET_BUFFER_F_IS_IP6)
158 vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
159 vnet_hdr->gso_size = vnet_buffer2 (b0)->gso_size;
161 vnet_buffer (b0)->l4_hdr_offset + vnet_buffer2 (b0)->gso_l4_hdr_sz;
162 vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
163 vnet_hdr->csum_start = vnet_buffer (b0)->l4_hdr_offset; // 0x36;
164 vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
168 static_always_inline void
169 fill_cksum_offload (vlib_buffer_t *b0, vnet_virtio_net_hdr_t *vnet_hdr)
171 vnet_buffer_oflags_t oflags = vnet_buffer (b0)->oflags;
172 if (b0->flags & VNET_BUFFER_F_IS_IP4)
175 ip4 = (ip4_header_t *) (vlib_buffer_get_current (b0) +
176 vnet_buffer (b0)->l3_hdr_offset);
177 if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
178 ip4->checksum = ip4_header_checksum (ip4);
179 vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
180 vnet_hdr->csum_start = 0x22;
181 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
184 (tcp_header_t *) (vlib_buffer_get_current (b0) +
185 vnet_buffer (b0)->l4_hdr_offset);
186 tcp->checksum = ip4_pseudo_header_cksum (ip4);
187 vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
189 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
192 (udp_header_t *) (vlib_buffer_get_current (b0) +
193 vnet_buffer (b0)->l4_hdr_offset);
194 udp->checksum = ip4_pseudo_header_cksum (ip4);
195 vnet_hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
198 else if (b0->flags & VNET_BUFFER_F_IS_IP6)
201 vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
202 vnet_hdr->csum_start = 0x36;
203 ip6 = (ip6_header_t *) (vlib_buffer_get_current (b0) +
204 vnet_buffer (b0)->l3_hdr_offset);
205 if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
208 (tcp_header_t *) (vlib_buffer_get_current (b0) +
209 vnet_buffer (b0)->l4_hdr_offset);
210 tcp->checksum = ip6_pseudo_header_cksum (ip6);
211 vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
213 else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
216 (udp_header_t *) (vlib_buffer_get_current (b0) +
217 vnet_buffer (b0)->l4_hdr_offset);
218 udp->checksum = ip6_pseudo_header_cksum (ip6);
219 vnet_hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
224 VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (vlib_main_t * vm,
225 vlib_node_runtime_t * node,
226 vlib_frame_t * frame)
228 af_packet_main_t *apm = &af_packet_main;
229 u32 *buffers = vlib_frame_vector_args (frame);
230 u32 n_left = frame->n_vectors;
232 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
233 af_packet_if_t *apif =
234 pool_elt_at_index (apm->interfaces, rd->dev_instance);
235 clib_spinlock_lock_if_init (&apif->lockp);
237 u32 frame_size = apif->tx_req->tp_frame_size;
238 u32 frame_num = apif->tx_req->tp_frame_nr;
239 u8 *block_start = apif->tx_ring[block];
240 u32 tx_frame = apif->next_tx_frame;
242 u32 frame_not_ready = 0;
243 u8 is_cksum_gso_enabled = (apif->is_cksum_gso_enabled == 1) ? 1 : 0;
248 vnet_virtio_net_hdr_t *vnet_hdr = 0;
250 vlib_buffer_t *b0 = 0;
256 tph = (tpacket3_hdr_t *) (block_start + tx_frame * frame_size);
257 if (PREDICT_FALSE (tph->tp_status &
258 (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
264 b0 = vlib_get_buffer (vm, bi);
266 if (is_cksum_gso_enabled)
269 (vnet_virtio_net_hdr_t *) ((u8 *) tph + TPACKET_ALIGN (sizeof (
272 clib_memset_u8 (vnet_hdr, 0, sizeof (vnet_virtio_net_hdr_t));
273 offset = sizeof (vnet_virtio_net_hdr_t);
275 if (b0->flags & VNET_BUFFER_F_GSO)
276 fill_gso_offload (b0, vnet_hdr);
277 else if (b0->flags & VNET_BUFFER_F_OFFLOAD)
278 fill_cksum_offload (b0, vnet_hdr);
281 len = b0->current_length;
282 clib_memcpy_fast ((u8 *) tph + TPACKET_ALIGN (sizeof (tpacket3_hdr_t)) +
284 vlib_buffer_get_current (b0), len);
287 while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
289 b0 = vlib_get_buffer (vm, b0->next_buffer);
290 len = b0->current_length;
291 clib_memcpy_fast ((u8 *) tph +
292 TPACKET_ALIGN (sizeof (tpacket3_hdr_t)) + offset,
293 vlib_buffer_get_current (b0), len);
297 tph->tp_len = tph->tp_snaplen = offset;
298 tph->tp_status = TP_STATUS_SEND_REQUEST;
301 tx_frame = (tx_frame + 1) % frame_num;
304 /* check if we've exhausted the ring */
305 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
309 CLIB_MEMORY_BARRIER ();
311 if (PREDICT_TRUE (n_sent))
313 apif->next_tx_frame = tx_frame;
315 if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) ==
318 /* Uh-oh, drop & move on, but count whether it was fatal or not.
319 * Note that we have no reliable way to properly determine the
320 * disposition of the packets we just enqueued for delivery.
322 vlib_error_count (vm, node->node_index,
323 unix_error_is_fatal (errno) ?
324 AF_PACKET_TX_ERROR_TXRING_FATAL :
325 AF_PACKET_TX_ERROR_TXRING_EAGAIN,
330 clib_spinlock_unlock_if_init (&apif->lockp);
332 if (PREDICT_FALSE (frame_not_ready))
333 vlib_error_count (vm, node->node_index,
334 AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
336 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
337 vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
340 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
341 return frame->n_vectors;
345 af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
348 af_packet_main_t *apm = &af_packet_main;
349 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
350 af_packet_if_t *apif =
351 pool_elt_at_index (apm->interfaces, hw->dev_instance);
353 /* Shut off redirection */
354 if (node_index == ~0)
356 apif->per_interface_next_index = node_index;
360 apif->per_interface_next_index =
361 vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
366 af_packet_clear_hw_interface_counters (u32 instance)
368 /* Nothing for now */
371 static clib_error_t *
372 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
375 af_packet_main_t *apm = &af_packet_main;
376 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
377 af_packet_if_t *apif =
378 pool_elt_at_index (apm->interfaces, hw->dev_instance);
380 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
385 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
390 /* if interface is a bridge ignore */
391 if (apif->host_if_index < 0)
392 goto error; /* no error */
394 /* use host_if_index in case host name has changed */
395 ifr.ifr_ifindex = apif->host_if_index;
396 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
398 vlib_log_warn (apm->log_class,
399 "af_packet_%s ioctl could not retrieve eth name",
404 apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
406 if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
408 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
409 apif->is_admin_up ? "up" : "down", rv);
413 if (apif->is_admin_up)
415 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
416 ifr.ifr_flags |= IFF_UP;
421 ifr.ifr_flags &= ~IFF_UP;
424 if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
426 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
427 apif->is_admin_up ? "up" : "down", rv);
431 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
437 return 0; /* no error */
440 static clib_error_t *
441 af_packet_subif_add_del_function (vnet_main_t * vnm,
443 struct vnet_sw_interface_t *st, int is_add)
445 /* Nothing for now */
449 static clib_error_t *af_packet_set_mac_address_function
450 (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
452 af_packet_main_t *apm = &af_packet_main;
453 af_packet_if_t *apif =
454 pool_elt_at_index (apm->interfaces, hi->dev_instance);
458 if (apif->mode == AF_PACKET_IF_MODE_IP)
460 vlib_log_warn (apm->log_class, "af_packet_%s interface is in IP mode",
462 return clib_error_return (0,
463 " MAC update failed, interface is in IP mode");
466 fd = socket (AF_UNIX, SOCK_DGRAM, 0);
469 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
474 /* if interface is a bridge ignore */
475 if (apif->host_if_index < 0)
476 goto error; /* no error */
478 /* use host_if_index in case host name has changed */
479 ifr.ifr_ifindex = apif->host_if_index;
480 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
484 "af_packet_%s ioctl could not retrieve eth name, error: %d",
485 apif->host_if_name, rv);
489 clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
490 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
492 if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
494 vlib_log_warn (apm->log_class,
495 "af_packet_%s ioctl could not set mac, error: %d",
496 apif->host_if_name, rv);
505 return 0; /* no error */
508 VNET_DEVICE_CLASS (af_packet_device_class) = {
510 .format_device_name = format_af_packet_device_name,
511 .format_device = format_af_packet_device,
512 .format_tx_trace = format_af_packet_tx_trace,
513 .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
514 .tx_function_error_strings = af_packet_tx_func_error_strings,
515 .rx_redirect_to_node = af_packet_set_interface_next_node,
516 .clear_counters = af_packet_clear_hw_interface_counters,
517 .admin_up_down_function = af_packet_interface_admin_up_down,
518 .subif_add_del_function = af_packet_subif_add_del_function,
519 .mac_addr_change_function = af_packet_set_mac_address_function,
523 * fd.io coding-style-patch-verification: ON
526 * eval: (c-set-style "gnu")