blob: b6b99a0465cfe9918e1c118391461d7b70a276aa [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{
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +010070 u32 dev_instance = va_arg (*args, u32);
71 u32 indent = format_get_indent (s);
72 int __clib_unused verbose = va_arg (*args, int);
73
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;
80 int block = 0;
81 u8 *block_start = apif->tx_ring + block * block_size;
82 u32 tx_frame = apif->next_tx_frame;
83 struct tpacket2_hdr *tph;
84
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,
89 apif->next_tx_frame);
90
91 int n_send_req = 0, n_avail = 0, n_sending = 0, n_tot = 0, n_wrong = 0;
92 do
93 {
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)
97 n_avail++;
98 else if (tph->tp_status & TP_STATUS_SEND_REQUEST)
99 n_send_req++;
100 else if (tph->tp_status & TP_STATUS_SENDING)
101 n_sending++;
102 else
103 n_wrong++;
104 n_tot++;
105 }
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,
109 n_wrong, n_tot);
110
111 clib_spinlock_unlock_if_init (&apif->lockp);
Damjan Marion83243a02016-02-29 13:09:30 +0100112 return s;
113}
114
Damjan Marion00a9dca2016-08-17 17:05:46 +0200115static u8 *
116format_af_packet_tx_trace (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +0100117{
118 s = format (s, "Unimplemented...");
119 return s;
120}
121
Filip Tehlaraee73642019-03-13 05:50:44 -0700122VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (vlib_main_t * vm,
123 vlib_node_runtime_t * node,
124 vlib_frame_t * frame)
Damjan Marion83243a02016-02-29 13:09:30 +0100125{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200126 af_packet_main_t *apm = &af_packet_main;
Damjan Mariona3d59862018-11-10 10:23:00 +0100127 u32 *buffers = vlib_frame_vector_args (frame);
Damjan Marion83243a02016-02-29 13:09:30 +0100128 u32 n_left = frame->n_vectors;
129 u32 n_sent = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200130 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);
Pierre Pfister8cedff22018-02-07 15:48:21 +0100133 clib_spinlock_lock_if_init (&apif->lockp);
Damjan Marion83243a02016-02-29 13:09:30 +0100134 int block = 0;
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;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200138 u8 *block_start = apif->tx_ring + block * block_size;
Damjan Marion83243a02016-02-29 13:09:30 +0100139 u32 tx_frame = apif->next_tx_frame;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200140 struct tpacket2_hdr *tph;
Damjan Marion83243a02016-02-29 13:09:30 +0100141 u32 frame_not_ready = 0;
142
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100143 while (n_left)
Damjan Marion83243a02016-02-29 13:09:30 +0100144 {
145 u32 len;
146 u32 offset = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200147 vlib_buffer_t *b0;
Damjan Marion83243a02016-02-29 13:09:30 +0100148 n_left--;
149 u32 bi = buffers[0];
150 buffers++;
151
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100152 nextframe:
Damjan Marion83243a02016-02-29 13:09:30 +0100153 tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100154 if (PREDICT_FALSE (tph->tp_status &
155 (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
Damjan Marion83243a02016-02-29 13:09:30 +0100156 {
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100157 tx_frame = (tx_frame + 1) % frame_num;
Damjan Marion83243a02016-02-29 13:09:30 +0100158 frame_not_ready++;
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100159 /* check if we've exhausted the ring */
160 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
161 break;
162 goto nextframe;
Damjan Marion83243a02016-02-29 13:09:30 +0100163 }
164
165 do
166 {
167 b0 = vlib_get_buffer (vm, bi);
168 len = b0->current_length;
Dave Barach178cf492018-11-13 16:34:13 -0500169 clib_memcpy_fast ((u8 *) tph +
170 TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) +
171 offset, vlib_buffer_get_current (b0), len);
Damjan Marion83243a02016-02-29 13:09:30 +0100172 offset += len;
173 }
Jim Gibson22db11b2017-03-27 19:46:12 +0000174 while ((bi =
175 (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
Damjan Marion83243a02016-02-29 13:09:30 +0100176
177 tph->tp_len = tph->tp_snaplen = offset;
178 tph->tp_status = TP_STATUS_SEND_REQUEST;
179 n_sent++;
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100180
Florin Coras837503c2017-11-28 11:59:20 -0800181 tx_frame = (tx_frame + 1) % frame_num;
182
Chris Luke4b46c842016-05-23 21:30:26 -0400183 /* check if we've exhausted the ring */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200184 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
185 break;
Damjan Marion83243a02016-02-29 13:09:30 +0100186 }
187
Damjan Marion00a9dca2016-08-17 17:05:46 +0200188 CLIB_MEMORY_BARRIER ();
Damjan Marion83243a02016-02-29 13:09:30 +0100189
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100190 apif->next_tx_frame = tx_frame;
Chris Luke4b46c842016-05-23 21:30:26 -0400191
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100192 if (PREDICT_TRUE (n_sent))
193 if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) ==
194 -1))
195 {
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.
199 */
200 vlib_error_count (vm, node->node_index,
201 unix_error_is_fatal (errno) ?
Damjan Marion00a9dca2016-08-17 17:05:46 +0200202 AF_PACKET_TX_ERROR_TXRING_FATAL :
Nathan Skrzypczakffc6bdc2021-02-01 17:13:59 +0100203 AF_PACKET_TX_ERROR_TXRING_EAGAIN,
204 n_sent);
205 }
Damjan Marion83243a02016-02-29 13:09:30 +0100206
Damjan Marion1927da22017-03-27 17:08:20 +0200207 clib_spinlock_unlock_if_init (&apif->lockp);
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100208
Damjan Marion00a9dca2016-08-17 17:05:46 +0200209 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);
Chris Luke4b46c842016-05-23 21:30:26 -0400212
Damjan Marion00a9dca2016-08-17 17:05:46 +0200213 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
Chris Luke4b46c842016-05-23 21:30:26 -0400214 vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200215 n_left);
Damjan Marion83243a02016-02-29 13:09:30 +0100216
Damjan Mariona3d59862018-11-10 10:23:00 +0100217 vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
Damjan Marion83243a02016-02-29 13:09:30 +0100218 return frame->n_vectors;
219}
220
221static void
Damjan Marion00a9dca2016-08-17 17:05:46 +0200222af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
223 u32 node_index)
Damjan Marion83243a02016-02-29 13:09:30 +0100224{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200225 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100226 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200227 af_packet_if_t *apif =
228 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Damjan Marion83243a02016-02-29 13:09:30 +0100229
230 /* Shut off redirection */
231 if (node_index == ~0)
232 {
233 apif->per_interface_next_index = node_index;
234 return;
235 }
236
237 apif->per_interface_next_index =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200238 vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
239 node_index);
Damjan Marion83243a02016-02-29 13:09:30 +0100240}
241
Damjan Marion00a9dca2016-08-17 17:05:46 +0200242static void
243af_packet_clear_hw_interface_counters (u32 instance)
Damjan Marion83243a02016-02-29 13:09:30 +0100244{
245 /* Nothing for now */
246}
247
248static clib_error_t *
Damjan Marion00a9dca2016-08-17 17:05:46 +0200249af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
250 u32 flags)
Damjan Marion83243a02016-02-29 13:09:30 +0100251{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200252 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100253 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200254 af_packet_if_t *apif =
255 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700256 u32 hw_flags;
Ray Kinsellac855b732017-04-21 12:24:43 +0100257 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
258 struct ifreq ifr;
259
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100260 if (0 > fd)
261 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200262 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
263 apif->host_if_name);
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100264 return 0;
265 }
266
Ray Kinsellac855b732017-04-21 12:24:43 +0100267 /* if interface is a bridge ignore */
268 if (apif->host_if_index < 0)
Ray Kinsella2038ad02017-05-18 11:56:28 +0100269 goto error; /* no error */
Ray Kinsellac855b732017-04-21 12:24:43 +0100270
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)
274 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200275 vlib_log_warn (apm->log_class,
276 "af_packet_%s ioctl could not retrieve eth name",
277 apif->host_if_name);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100278 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100279 }
Damjan Marion83243a02016-02-29 13:09:30 +0100280
281 apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
282
Ray Kinsellac855b732017-04-21 12:24:43 +0100283 if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
284 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200285 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
286 apif->is_admin_up ? "up" : "down", rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100287 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100288 }
289
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700290 if (apif->is_admin_up)
Ray Kinsellac855b732017-04-21 12:24:43 +0100291 {
292 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
293 ifr.ifr_flags |= IFF_UP;
294 }
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700295 else
Ray Kinsellac855b732017-04-21 12:24:43 +0100296 {
297 hw_flags = 0;
298 ifr.ifr_flags &= ~IFF_UP;
299 }
300
301 if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
302 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200303 vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
304 apif->is_admin_up ? "up" : "down", rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100305 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100306 }
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700307
Damjan Marion00a9dca2016-08-17 17:05:46 +0200308 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700309
Ray Kinsella2038ad02017-05-18 11:56:28 +0100310error:
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100311 if (0 <= fd)
312 close (fd);
Ray Kinsellac855b732017-04-21 12:24:43 +0100313
314 return 0; /* no error */
Damjan Marion83243a02016-02-29 13:09:30 +0100315}
316
317static clib_error_t *
318af_packet_subif_add_del_function (vnet_main_t * vnm,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200319 u32 hw_if_index,
320 struct vnet_sw_interface_t *st, int is_add)
Damjan Marion83243a02016-02-29 13:09:30 +0100321{
322 /* Nothing for now */
323 return 0;
324}
325
Ray Kinsella2038ad02017-05-18 11:56:28 +0100326static clib_error_t *af_packet_set_mac_address_function
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700327 (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
Ray Kinsella2038ad02017-05-18 11:56:28 +0100328{
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);
333 struct ifreq ifr;
334
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100335 if (0 > fd)
336 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200337 vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
338 apif->host_if_name);
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100339 return 0;
340 }
341
Ray Kinsella2038ad02017-05-18 11:56:28 +0100342 /* if interface is a bridge ignore */
343 if (apif->host_if_index < 0)
344 goto error; /* no error */
345
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)
349 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200350 vlib_log_warn
351 (apm->log_class,
352 "af_packet_%s ioctl could not retrieve eth name, error: %d",
Ray Kinsella2038ad02017-05-18 11:56:28 +0100353 apif->host_if_name, rv);
354 goto error;
355 }
356
357 clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
358 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
359
360 if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
361 {
Mohsin Kazmiacba9f72018-05-17 15:42:27 +0200362 vlib_log_warn (apm->log_class,
363 "af_packet_%s ioctl could not set mac, error: %d",
364 apif->host_if_name, rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100365 goto error;
366 }
367
368error:
Ray Kinsellae6cc9cc2017-05-20 13:42:30 +0100369
370 if (0 <= fd)
371 close (fd);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100372
373 return 0; /* no error */
374}
375
Damjan Marion00a9dca2016-08-17 17:05:46 +0200376/* *INDENT-OFF* */
Damjan Marion83243a02016-02-29 13:09:30 +0100377VNET_DEVICE_CLASS (af_packet_device_class) = {
378 .name = "af-packet",
Damjan Marion83243a02016-02-29 13:09:30 +0100379 .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,
Ray Kinsella2038ad02017-05-18 11:56:28 +0100388 .mac_addr_change_function = af_packet_set_mac_address_function,
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700389};
Damjan Marion00a9dca2016-08-17 17:05:46 +0200390/* *INDENT-ON* */
391
392/*
393 * fd.io coding-style-patch-verification: ON
394 *
395 * Local Variables:
396 * eval: (c-set-style "gnu")
397 * End:
398 */