blob: 2886fb5b6d5a9643477c266a202a5b5e8a6af4ce [file] [log] [blame]
Damjan Marion83243a02016-02-29 13:09:30 +01001/*
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_packet.h>
Ray Kinsella2038ad02017-05-18 11:56:28 +010021#include <sys/socket.h>
Ray Kinsellac855b732017-04-21 12:24:43 +010022#include <sys/ioctl.h>
23#include <net/if.h>
Ray Kinsella2038ad02017-05-18 11:56:28 +010024#include <net/if_arp.h>
Damjan Marion83243a02016-02-29 13:09:30 +010025
26#include <vlib/vlib.h>
27#include <vlib/unix/unix.h>
28#include <vnet/ip/ip.h>
29#include <vnet/ethernet/ethernet.h>
30
31#include <vnet/devices/af_packet/af_packet.h>
32
Chris Luke4b46c842016-05-23 21:30:26 -040033#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")
Damjan Marion83243a02016-02-29 13:09:30 +010038
Damjan Marion00a9dca2016-08-17 17:05:46 +020039typedef enum
40{
Damjan Marion83243a02016-02-29 13:09:30 +010041#define _(f,s) AF_PACKET_TX_ERROR_##f,
42 foreach_af_packet_tx_func_error
43#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +020044 AF_PACKET_TX_N_ERROR,
Damjan Marion83243a02016-02-29 13:09:30 +010045} af_packet_tx_func_error_t;
46
Damjan Marion00a9dca2016-08-17 17:05:46 +020047static char *af_packet_tx_func_error_strings[] = {
Damjan Marion83243a02016-02-29 13:09:30 +010048#define _(n,s) s,
Damjan Marion00a9dca2016-08-17 17:05:46 +020049 foreach_af_packet_tx_func_error
Damjan Marion83243a02016-02-29 13:09:30 +010050#undef _
51};
52
Damjan Marionc5170202017-10-11 14:15:47 +020053
Filip Tehlaraee73642019-03-13 05:50:44 -070054#ifndef CLIB_MARCH_VARIANT
Damjan Marionceab7882018-01-19 20:56:12 +010055u8 *
Damjan Marion00a9dca2016-08-17 17:05:46 +020056format_af_packet_device_name (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010057{
58 u32 i = va_arg (*args, u32);
Damjan Marion00a9dca2016-08-17 17:05:46 +020059 af_packet_main_t *apm = &af_packet_main;
60 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
Damjan Marion83243a02016-02-29 13:09:30 +010061
62 s = format (s, "host-%s", apif->host_if_name);
63 return s;
64}
Filip Tehlaraee73642019-03-13 05:50:44 -070065#endif /* CLIB_MARCH_VARIANT */
Damjan Marion83243a02016-02-29 13:09:30 +010066
Damjan Marion00a9dca2016-08-17 17:05:46 +020067static u8 *
68format_af_packet_device (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010069{
70 s = format (s, "Linux PACKET socket interface");
71 return s;
72}
73
Damjan Marion00a9dca2016-08-17 17:05:46 +020074static u8 *
75format_af_packet_tx_trace (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010076{
77 s = format (s, "Unimplemented...");
78 return s;
79}
80
Filip Tehlaraee73642019-03-13 05:50:44 -070081VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (vlib_main_t * vm,
82 vlib_node_runtime_t * node,
83 vlib_frame_t * frame)
Damjan Marion83243a02016-02-29 13:09:30 +010084{
Damjan Marion00a9dca2016-08-17 17:05:46 +020085 af_packet_main_t *apm = &af_packet_main;
Damjan Mariona3d59862018-11-10 10:23:00 +010086 u32 *buffers = vlib_frame_vector_args (frame);
Damjan Marion83243a02016-02-29 13:09:30 +010087 u32 n_left = frame->n_vectors;
88 u32 n_sent = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +020089 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
90 af_packet_if_t *apif =
91 pool_elt_at_index (apm->interfaces, rd->dev_instance);
Pierre Pfister8cedff22018-02-07 15:48:21 +010092 clib_spinlock_lock_if_init (&apif->lockp);
Damjan Marion83243a02016-02-29 13:09:30 +010093 int block = 0;
94 u32 block_size = apif->tx_req->tp_block_size;
95 u32 frame_size = apif->tx_req->tp_frame_size;
96 u32 frame_num = apif->tx_req->tp_frame_nr;
Damjan Marion00a9dca2016-08-17 17:05:46 +020097 u8 *block_start = apif->tx_ring + block * block_size;
Damjan Marion83243a02016-02-29 13:09:30 +010098 u32 tx_frame = apif->next_tx_frame;
Damjan Marion00a9dca2016-08-17 17:05:46 +020099 struct tpacket2_hdr *tph;
Damjan Marion83243a02016-02-29 13:09:30 +0100100 u32 frame_not_ready = 0;
101
Damjan Marion00a9dca2016-08-17 17:05:46 +0200102 while (n_left > 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100103 {
104 u32 len;
105 u32 offset = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200106 vlib_buffer_t *b0;
Damjan Marion83243a02016-02-29 13:09:30 +0100107 n_left--;
108 u32 bi = buffers[0];
109 buffers++;
110
111 tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
112
Damjan Marion00a9dca2016-08-17 17:05:46 +0200113 if (PREDICT_FALSE
114 (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
Damjan Marion83243a02016-02-29 13:09:30 +0100115 {
116 frame_not_ready++;
117 goto next;
118 }
119
120 do
121 {
122 b0 = vlib_get_buffer (vm, bi);
123 len = b0->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500124 clib_memcpy_fast ((u8 *) tph +
125 TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) +
126 offset, vlib_buffer_get_current (b0), len);
Damjan Marion83243a02016-02-29 13:09:30 +0100127 offset += len;
128 }
Jim Gibson22db11b2017-03-27 19:46:12 +0000129 while ((bi =
130 (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
Damjan Marion83243a02016-02-29 13:09:30 +0100131
132 tph->tp_len = tph->tp_snaplen = offset;
133 tph->tp_status = TP_STATUS_SEND_REQUEST;
134 n_sent++;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200135 next:
Florin Coras837503c2017-11-28 11:59:20 -0800136 tx_frame = (tx_frame + 1) % frame_num;
137
Chris Luke4b46c842016-05-23 21:30:26 -0400138 /* check if we've exhausted the ring */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200139 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
140 break;
Damjan Marion83243a02016-02-29 13:09:30 +0100141 }
142
Damjan Marion00a9dca2016-08-17 17:05:46 +0200143 CLIB_MEMORY_BARRIER ();
Damjan Marion83243a02016-02-29 13:09:30 +0100144
Damjan Marion00a9dca2016-08-17 17:05:46 +0200145 if (PREDICT_TRUE (n_sent))
Damjan Marion83243a02016-02-29 13:09:30 +0100146 {
147 apif->next_tx_frame = tx_frame;
Chris Luke4b46c842016-05-23 21:30:26 -0400148
Damjan Marion00a9dca2016-08-17 17:05:46 +0200149 if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
150 MSG_DONTWAIT, NULL, 0) == -1))
151 {
152 /* Uh-oh, drop & move on, but count whether it was fatal or not.
153 * Note that we have no reliable way to properly determine the
154 * disposition of the packets we just enqueued for delivery.
155 */
156 vlib_error_count (vm, node->node_index,
157 unix_error_is_fatal (errno) ?
158 AF_PACKET_TX_ERROR_TXRING_FATAL :
159 AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
160 }
Damjan Marion83243a02016-02-29 13:09:30 +0100161 }
162
Damjan Marion1927da22017-03-27 17:08:20 +0200163 clib_spinlock_unlock_if_init (&apif->lockp);
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100164
Damjan Marion00a9dca2016-08-17 17:05:46 +0200165 if (PREDICT_FALSE (frame_not_ready))
166 vlib_error_count (vm, node->node_index,
167 AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
Chris Luke4b46c842016-05-23 21:30:26 -0400168
Damjan Marion00a9dca2016-08-17 17:05:46 +0200169 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
Chris Luke4b46c842016-05-23 21:30:26 -0400170 vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200171 n_left);
Damjan Marion83243a02016-02-29 13:09:30 +0100172
Damjan Mariona3d59862018-11-10 10:23:00 +0100173 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
Damjan Marion83243a02016-02-29 13:09:30 +0100174 return frame->n_vectors;
175}
176
177static void
Damjan Marion00a9dca2016-08-17 17:05:46 +0200178af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
179 u32 node_index)
Damjan Marion83243a02016-02-29 13:09:30 +0100180{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200181 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100182 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200183 af_packet_if_t *apif =
184 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Damjan Marion83243a02016-02-29 13:09:30 +0100185
186 /* Shut off redirection */
187 if (node_index == ~0)
188 {
189 apif->per_interface_next_index = node_index;
190 return;
191 }
192
193 apif->per_interface_next_index =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200194 vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
195 node_index);
Damjan Marion83243a02016-02-29 13:09:30 +0100196}
197
Damjan Marion00a9dca2016-08-17 17:05:46 +0200198static void
199af_packet_clear_hw_interface_counters (u32 instance)
Damjan Marion83243a02016-02-29 13:09:30 +0100200{
201 /* Nothing for now */
202}
203
204static clib_error_t *
Damjan Marion00a9dca2016-08-17 17:05:46 +0200205af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
206 u32 flags)
Damjan Marion83243a02016-02-29 13:09:30 +0100207{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200208 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100209 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200210 af_packet_if_t *apif =
211 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700212 u32 hw_flags;
Ray Kinsellac855b732017-04-21 12:24:43 +0100213 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
214 struct ifreq ifr;
215
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100216 if (0 > fd)
217 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200218 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
219 apif->host_if_name);
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100220 return 0;
221 }
222
Ray Kinsellac855b732017-04-21 12:24:43 +0100223 /* if interface is a bridge ignore */
224 if (apif->host_if_index < 0)
Ray Kinsella2038ad02017-05-18 11:56:28 +0100225 goto error; /* no error */
Ray Kinsellac855b732017-04-21 12:24:43 +0100226
227 /* use host_if_index in case host name has changed */
228 ifr.ifr_ifindex = apif->host_if_index;
229 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
230 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200231 vlib_log_warn (apm->log_class,
232 "af_packet_%s ioctl could not retrieve eth name",
233 apif->host_if_name);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100234 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100235 }
Damjan Marion83243a02016-02-29 13:09:30 +0100236
237 apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
238
Ray Kinsellac855b732017-04-21 12:24:43 +0100239 if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
240 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200241 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
242 apif->is_admin_up ? "up" : "down", rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100243 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100244 }
245
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700246 if (apif->is_admin_up)
Ray Kinsellac855b732017-04-21 12:24:43 +0100247 {
248 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
249 ifr.ifr_flags |= IFF_UP;
250 }
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700251 else
Ray Kinsellac855b732017-04-21 12:24:43 +0100252 {
253 hw_flags = 0;
254 ifr.ifr_flags &= ~IFF_UP;
255 }
256
257 if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
258 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200259 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
260 apif->is_admin_up ? "up" : "down", rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100261 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100262 }
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700263
Damjan Marion00a9dca2016-08-17 17:05:46 +0200264 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700265
Ray Kinsella2038ad02017-05-18 11:56:28 +0100266error:
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100267 if (0 <= fd)
268 close (fd);
Ray Kinsellac855b732017-04-21 12:24:43 +0100269
270 return 0; /* no error */
Damjan Marion83243a02016-02-29 13:09:30 +0100271}
272
273static clib_error_t *
274af_packet_subif_add_del_function (vnet_main_t * vnm,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200275 u32 hw_if_index,
276 struct vnet_sw_interface_t *st, int is_add)
Damjan Marion83243a02016-02-29 13:09:30 +0100277{
278 /* Nothing for now */
279 return 0;
280}
281
Ray Kinsella2038ad02017-05-18 11:56:28 +0100282static clib_error_t *af_packet_set_mac_address_function
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700283 (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
Ray Kinsella2038ad02017-05-18 11:56:28 +0100284{
285 af_packet_main_t *apm = &af_packet_main;
286 af_packet_if_t *apif =
287 pool_elt_at_index (apm->interfaces, hi->dev_instance);
288 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
289 struct ifreq ifr;
290
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100291 if (0 > fd)
292 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200293 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
294 apif->host_if_name);
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100295 return 0;
296 }
297
Ray Kinsella2038ad02017-05-18 11:56:28 +0100298 /* if interface is a bridge ignore */
299 if (apif->host_if_index < 0)
300 goto error; /* no error */
301
302 /* use host_if_index in case host name has changed */
303 ifr.ifr_ifindex = apif->host_if_index;
304 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
305 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200306 vlib_log_warn
307 (apm->log_class,
308 "af_packet_%s ioctl could not retrieve eth name, error: %d",
Ray Kinsella2038ad02017-05-18 11:56:28 +0100309 apif->host_if_name, rv);
310 goto error;
311 }
312
313 clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
314 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
315
316 if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
317 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200318 vlib_log_warn (apm->log_class,
319 "af_packet_%s ioctl could not set mac, error: %d",
320 apif->host_if_name, rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100321 goto error;
322 }
323
324error:
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100325
326 if (0 <= fd)
327 close (fd);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100328
329 return 0; /* no error */
330}
331
Damjan Marion00a9dca2016-08-17 17:05:46 +0200332/* *INDENT-OFF* */
Damjan Marion83243a02016-02-29 13:09:30 +0100333VNET_DEVICE_CLASS (af_packet_device_class) = {
334 .name = "af-packet",
Damjan Marion83243a02016-02-29 13:09:30 +0100335 .format_device_name = format_af_packet_device_name,
336 .format_device = format_af_packet_device,
337 .format_tx_trace = format_af_packet_tx_trace,
338 .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
339 .tx_function_error_strings = af_packet_tx_func_error_strings,
340 .rx_redirect_to_node = af_packet_set_interface_next_node,
341 .clear_counters = af_packet_clear_hw_interface_counters,
342 .admin_up_down_function = af_packet_interface_admin_up_down,
343 .subif_add_del_function = af_packet_subif_add_del_function,
Ray Kinsella2038ad02017-05-18 11:56:28 +0100344 .mac_addr_change_function = af_packet_set_mac_address_function,
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700345};
Damjan Marion00a9dca2016-08-17 17:05:46 +0200346/* *INDENT-ON* */
347
348/*
349 * fd.io coding-style-patch-verification: ON
350 *
351 * Local Variables:
352 * eval: (c-set-style "gnu")
353 * End:
354 */