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>
31 #include <vnet/devices/af_packet/af_packet.h>
33 #define foreach_af_packet_tx_func_error \
34 _(FRAME_NOT_READY, "tx frame not ready") \
35 _(TXRING_EAGAIN, "tx sendto temporary failure") \
36 _(TXRING_FATAL, "tx sendto fatal failure") \
37 _(TXRING_OVERRUN, "tx ring overrun")
41 #define _(f,s) AF_PACKET_TX_ERROR_##f,
42 foreach_af_packet_tx_func_error
45 } af_packet_tx_func_error_t;
47 static char *af_packet_tx_func_error_strings[] = {
49 foreach_af_packet_tx_func_error
54 #ifndef CLIB_MARCH_VARIANT
56 format_af_packet_device_name (u8 * s, va_list * args)
58 u32 i = va_arg (*args, u32);
59 af_packet_main_t *apm = &af_packet_main;
60 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
62 s = format (s, "host-%s", apif->host_if_name);
65 #endif /* CLIB_MARCH_VARIANT */
68 format_af_packet_device (u8 * s, va_list * args)
70 u32 dev_instance = va_arg (*args, u32);
71 u32 indent = format_get_indent (s);
72 int __clib_unused verbose = va_arg (*args, int);
74 af_packet_main_t *apm = &af_packet_main;
75 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, dev_instance);
76 clib_spinlock_lock_if_init (&apif->lockp);
77 u32 block_size = apif->tx_req->tp_block_size;
78 u32 frame_size = apif->tx_req->tp_frame_size;
79 u32 frame_num = apif->tx_req->tp_frame_nr;
81 u8 *block_start = apif->tx_ring + block * block_size;
82 u32 tx_frame = apif->next_tx_frame;
83 struct tpacket2_hdr *tph;
85 s = format (s, "Linux PACKET socket interface\n");
86 s = format (s, "%Ublock:%d frame:%d\n", format_white_space, indent,
87 block_size, frame_size);
88 s = format (s, "%Unext frame:%d\n", format_white_space, indent,
91 int n_send_req = 0, n_avail = 0, n_sending = 0, n_tot = 0, n_wrong = 0;
94 tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
95 tx_frame = (tx_frame + 1) % frame_num;
96 if (tph->tp_status == 0)
98 else if (tph->tp_status & TP_STATUS_SEND_REQUEST)
100 else if (tph->tp_status & TP_STATUS_SENDING)
106 while (tx_frame != apif->next_tx_frame);
107 s = format (s, "%Uavailable:%d request:%d sending:%d wrong:%d total:%d\n",
108 format_white_space, indent, n_avail, n_send_req, n_sending,
111 clib_spinlock_unlock_if_init (&apif->lockp);
116 format_af_packet_tx_trace (u8 * s, va_list * args)
118 s = format (s, "Unimplemented...");
122 VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (vlib_main_t * vm,
123 vlib_node_runtime_t * node,
124 vlib_frame_t * frame)
126 af_packet_main_t *apm = &af_packet_main;
127 u32 *buffers = vlib_frame_vector_args (frame);
128 u32 n_left = frame->n_vectors;
130 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
131 af_packet_if_t *apif =
132 pool_elt_at_index (apm->interfaces, rd->dev_instance);
133 clib_spinlock_lock_if_init (&apif->lockp);
135 u32 block_size = apif->tx_req->tp_block_size;
136 u32 frame_size = apif->tx_req->tp_frame_size;
137 u32 frame_num = apif->tx_req->tp_frame_nr;
138 u8 *block_start = apif->tx_ring + block * block_size;
139 u32 tx_frame = apif->next_tx_frame;
140 struct tpacket2_hdr *tph;
141 u32 frame_not_ready = 0;
153 tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
154 if (PREDICT_FALSE (tph->tp_status &
155 (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
157 tx_frame = (tx_frame + 1) % frame_num;
159 /* check if we've exhausted the ring */
160 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
167 b0 = vlib_get_buffer (vm, bi);
168 len = b0->current_length;
169 clib_memcpy_fast ((u8 *) tph +
170 TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) +
171 offset, vlib_buffer_get_current (b0), len);
175 (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
177 tph->tp_len = tph->tp_snaplen = offset;
178 tph->tp_status = TP_STATUS_SEND_REQUEST;
181 tx_frame = (tx_frame + 1) % frame_num;
183 /* check if we've exhausted the ring */
184 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
188 CLIB_MEMORY_BARRIER ();
190 apif->next_tx_frame = tx_frame;
192 if (PREDICT_TRUE (n_sent))
193 if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) ==
196 /* Uh-oh, drop & move on, but count whether it was fatal or not.
197 * Note that we have no reliable way to properly determine the
198 * disposition of the packets we just enqueued for delivery.
200 vlib_error_count (vm, node->node_index,
201 unix_error_is_fatal (errno) ?
202 AF_PACKET_TX_ERROR_TXRING_FATAL :
203 AF_PACKET_TX_ERROR_TXRING_EAGAIN,
207 clib_spinlock_unlock_if_init (&apif->lockp);
209 if (PREDICT_FALSE (frame_not_ready))
210 vlib_error_count (vm, node->node_index,
211 AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
213 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
214 vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
217 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
218 return frame->n_vectors;
222 af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
225 af_packet_main_t *apm = &af_packet_main;
226 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
227 af_packet_if_t *apif =
228 pool_elt_at_index (apm->interfaces, hw->dev_instance);
230 /* Shut off redirection */
231 if (node_index == ~0)
233 apif->per_interface_next_index = node_index;
237 apif->per_interface_next_index =
238 vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
243 af_packet_clear_hw_interface_counters (u32 instance)
245 /* Nothing for now */
248 static clib_error_t *
249 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
252 af_packet_main_t *apm = &af_packet_main;
253 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
254 af_packet_if_t *apif =
255 pool_elt_at_index (apm->interfaces, hw->dev_instance);
257 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
262 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
267 /* if interface is a bridge ignore */
268 if (apif->host_if_index < 0)
269 goto error; /* no error */
271 /* use host_if_index in case host name has changed */
272 ifr.ifr_ifindex = apif->host_if_index;
273 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
275 vlib_log_warn (apm->log_class,
276 "af_packet_%s ioctl could not retrieve eth name",
281 apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
283 if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
285 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
286 apif->is_admin_up ? "up" : "down", rv);
290 if (apif->is_admin_up)
292 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
293 ifr.ifr_flags |= IFF_UP;
298 ifr.ifr_flags &= ~IFF_UP;
301 if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
303 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
304 apif->is_admin_up ? "up" : "down", rv);
308 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
314 return 0; /* no error */
317 static clib_error_t *
318 af_packet_subif_add_del_function (vnet_main_t * vnm,
320 struct vnet_sw_interface_t *st, int is_add)
322 /* Nothing for now */
326 static clib_error_t *af_packet_set_mac_address_function
327 (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
329 af_packet_main_t *apm = &af_packet_main;
330 af_packet_if_t *apif =
331 pool_elt_at_index (apm->interfaces, hi->dev_instance);
332 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
337 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
342 /* if interface is a bridge ignore */
343 if (apif->host_if_index < 0)
344 goto error; /* no error */
346 /* use host_if_index in case host name has changed */
347 ifr.ifr_ifindex = apif->host_if_index;
348 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
352 "af_packet_%s ioctl could not retrieve eth name, error: %d",
353 apif->host_if_name, rv);
357 clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
358 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
360 if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
362 vlib_log_warn (apm->log_class,
363 "af_packet_%s ioctl could not set mac, error: %d",
364 apif->host_if_name, rv);
373 return 0; /* no error */
377 VNET_DEVICE_CLASS (af_packet_device_class) = {
379 .format_device_name = format_af_packet_device_name,
380 .format_device = format_af_packet_device,
381 .format_tx_trace = format_af_packet_tx_trace,
382 .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
383 .tx_function_error_strings = af_packet_tx_func_error_strings,
384 .rx_redirect_to_node = af_packet_set_interface_next_node,
385 .clear_counters = af_packet_clear_hw_interface_counters,
386 .admin_up_down_function = af_packet_interface_admin_up_down,
387 .subif_add_del_function = af_packet_subif_add_del_function,
388 .mac_addr_change_function = af_packet_set_mac_address_function,
393 * fd.io coding-style-patch-verification: ON
396 * eval: (c-set-style "gnu")