blob: 4607d888ac934c8024da8964c0e4161b8d09c797 [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
53
Damjan Marion00a9dca2016-08-17 17:05:46 +020054static u8 *
55format_af_packet_device_name (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010056{
57 u32 i = va_arg (*args, u32);
Damjan Marion00a9dca2016-08-17 17:05:46 +020058 af_packet_main_t *apm = &af_packet_main;
59 af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
Damjan Marion83243a02016-02-29 13:09:30 +010060
61 s = format (s, "host-%s", apif->host_if_name);
62 return s;
63}
64
Damjan Marion00a9dca2016-08-17 17:05:46 +020065static u8 *
66format_af_packet_device (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010067{
68 s = format (s, "Linux PACKET socket interface");
69 return s;
70}
71
Damjan Marion00a9dca2016-08-17 17:05:46 +020072static u8 *
73format_af_packet_tx_trace (u8 * s, va_list * args)
Damjan Marion83243a02016-02-29 13:09:30 +010074{
75 s = format (s, "Unimplemented...");
76 return s;
77}
78
79static uword
80af_packet_interface_tx (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +020081 vlib_node_runtime_t * node, vlib_frame_t * frame)
Damjan Marion83243a02016-02-29 13:09:30 +010082{
Damjan Marion00a9dca2016-08-17 17:05:46 +020083 af_packet_main_t *apm = &af_packet_main;
84 u32 *buffers = vlib_frame_args (frame);
Damjan Marion83243a02016-02-29 13:09:30 +010085 u32 n_left = frame->n_vectors;
86 u32 n_sent = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +020087 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
88 af_packet_if_t *apif =
89 pool_elt_at_index (apm->interfaces, rd->dev_instance);
Damjan Marion83243a02016-02-29 13:09:30 +010090 int block = 0;
91 u32 block_size = apif->tx_req->tp_block_size;
92 u32 frame_size = apif->tx_req->tp_frame_size;
93 u32 frame_num = apif->tx_req->tp_frame_nr;
Damjan Marion00a9dca2016-08-17 17:05:46 +020094 u8 *block_start = apif->tx_ring + block * block_size;
Damjan Marion83243a02016-02-29 13:09:30 +010095 u32 tx_frame = apif->next_tx_frame;
Damjan Marion00a9dca2016-08-17 17:05:46 +020096 struct tpacket2_hdr *tph;
Damjan Marion83243a02016-02-29 13:09:30 +010097 u32 frame_not_ready = 0;
98
Damjan Marion1927da22017-03-27 17:08:20 +020099 clib_spinlock_lock_if_init (&apif->lockp);
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100100
Damjan Marion00a9dca2016-08-17 17:05:46 +0200101 while (n_left > 0)
Damjan Marion83243a02016-02-29 13:09:30 +0100102 {
103 u32 len;
104 u32 offset = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200105 vlib_buffer_t *b0;
Damjan Marion83243a02016-02-29 13:09:30 +0100106 n_left--;
107 u32 bi = buffers[0];
108 buffers++;
109
110 tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
111
Damjan Marion00a9dca2016-08-17 17:05:46 +0200112 if (PREDICT_FALSE
113 (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
Damjan Marion83243a02016-02-29 13:09:30 +0100114 {
115 frame_not_ready++;
116 goto next;
117 }
118
119 do
120 {
121 b0 = vlib_get_buffer (vm, bi);
122 len = b0->current_length;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200123 clib_memcpy ((u8 *) tph +
124 TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + offset,
125 vlib_buffer_get_current (b0), len);
Damjan Marion83243a02016-02-29 13:09:30 +0100126 offset += len;
127 }
Jim Gibson22db11b2017-03-27 19:46:12 +0000128 while ((bi =
129 (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
Damjan Marion83243a02016-02-29 13:09:30 +0100130
131 tph->tp_len = tph->tp_snaplen = offset;
132 tph->tp_status = TP_STATUS_SEND_REQUEST;
133 n_sent++;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200134 next:
Chris Luke4b46c842016-05-23 21:30:26 -0400135 /* check if we've exhausted the ring */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200136 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
137 break;
Chris Luke4b46c842016-05-23 21:30:26 -0400138
Damjan Marion83243a02016-02-29 13:09:30 +0100139 tx_frame = (tx_frame + 1) % frame_num;
140 }
141
Damjan Marion00a9dca2016-08-17 17:05:46 +0200142 CLIB_MEMORY_BARRIER ();
Damjan Marion83243a02016-02-29 13:09:30 +0100143
Damjan Marion00a9dca2016-08-17 17:05:46 +0200144 if (PREDICT_TRUE (n_sent))
Damjan Marion83243a02016-02-29 13:09:30 +0100145 {
146 apif->next_tx_frame = tx_frame;
Chris Luke4b46c842016-05-23 21:30:26 -0400147
Damjan Marion00a9dca2016-08-17 17:05:46 +0200148 if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
149 MSG_DONTWAIT, NULL, 0) == -1))
150 {
151 /* Uh-oh, drop & move on, but count whether it was fatal or not.
152 * Note that we have no reliable way to properly determine the
153 * disposition of the packets we just enqueued for delivery.
154 */
155 vlib_error_count (vm, node->node_index,
156 unix_error_is_fatal (errno) ?
157 AF_PACKET_TX_ERROR_TXRING_FATAL :
158 AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
159 }
Damjan Marion83243a02016-02-29 13:09:30 +0100160 }
161
Damjan Marion1927da22017-03-27 17:08:20 +0200162 clib_spinlock_unlock_if_init (&apif->lockp);
Mohsin KAZMIcf751ec2017-01-18 11:59:45 +0100163
Damjan Marion00a9dca2016-08-17 17:05:46 +0200164 if (PREDICT_FALSE (frame_not_ready))
165 vlib_error_count (vm, node->node_index,
166 AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
Chris Luke4b46c842016-05-23 21:30:26 -0400167
Damjan Marion00a9dca2016-08-17 17:05:46 +0200168 if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
Chris Luke4b46c842016-05-23 21:30:26 -0400169 vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200170 n_left);
Damjan Marion83243a02016-02-29 13:09:30 +0100171
172 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
173 return frame->n_vectors;
174}
175
176static void
Damjan Marion00a9dca2016-08-17 17:05:46 +0200177af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
178 u32 node_index)
Damjan Marion83243a02016-02-29 13:09:30 +0100179{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200180 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100181 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200182 af_packet_if_t *apif =
183 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Damjan Marion83243a02016-02-29 13:09:30 +0100184
185 /* Shut off redirection */
186 if (node_index == ~0)
187 {
188 apif->per_interface_next_index = node_index;
189 return;
190 }
191
192 apif->per_interface_next_index =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200193 vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
194 node_index);
Damjan Marion83243a02016-02-29 13:09:30 +0100195}
196
Damjan Marion00a9dca2016-08-17 17:05:46 +0200197static void
198af_packet_clear_hw_interface_counters (u32 instance)
Damjan Marion83243a02016-02-29 13:09:30 +0100199{
200 /* Nothing for now */
201}
202
203static clib_error_t *
Damjan Marion00a9dca2016-08-17 17:05:46 +0200204af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
205 u32 flags)
Damjan Marion83243a02016-02-29 13:09:30 +0100206{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200207 af_packet_main_t *apm = &af_packet_main;
Damjan Marion83243a02016-02-29 13:09:30 +0100208 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200209 af_packet_if_t *apif =
210 pool_elt_at_index (apm->interfaces, hw->dev_instance);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700211 u32 hw_flags;
Ray Kinsellac855b732017-04-21 12:24:43 +0100212 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
213 struct ifreq ifr;
214
215 /* if interface is a bridge ignore */
216 if (apif->host_if_index < 0)
Ray Kinsella2038ad02017-05-18 11:56:28 +0100217 goto error; /* no error */
Ray Kinsellac855b732017-04-21 12:24:43 +0100218
219 /* use host_if_index in case host name has changed */
220 ifr.ifr_ifindex = apif->host_if_index;
221 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
222 {
223 clib_unix_warning ("af_packet_%s ioctl could not retrieve eth name",
224 apif->host_if_name);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100225 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100226 }
Damjan Marion83243a02016-02-29 13:09:30 +0100227
228 apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
229
Ray Kinsellac855b732017-04-21 12:24:43 +0100230 if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
231 {
232 clib_unix_warning ("af_packet_%s error: %d",
233 apif->is_admin_up ? "up" : "down", rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100234 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100235 }
236
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700237 if (apif->is_admin_up)
Ray Kinsellac855b732017-04-21 12:24:43 +0100238 {
239 hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
240 ifr.ifr_flags |= IFF_UP;
241 }
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700242 else
Ray Kinsellac855b732017-04-21 12:24:43 +0100243 {
244 hw_flags = 0;
245 ifr.ifr_flags &= ~IFF_UP;
246 }
247
248 if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
249 {
250 clib_unix_warning ("af_packet_%s error: %d",
251 apif->is_admin_up ? "up" : "down", rv);
Ray Kinsella2038ad02017-05-18 11:56:28 +0100252 goto error;
Ray Kinsellac855b732017-04-21 12:24:43 +0100253 }
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700254
Damjan Marion00a9dca2016-08-17 17:05:46 +0200255 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700256
Ray Kinsella2038ad02017-05-18 11:56:28 +0100257error:
Ray Kinsellac855b732017-04-21 12:24:43 +0100258 close (fd);
259
260 return 0; /* no error */
Damjan Marion83243a02016-02-29 13:09:30 +0100261}
262
263static clib_error_t *
264af_packet_subif_add_del_function (vnet_main_t * vnm,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200265 u32 hw_if_index,
266 struct vnet_sw_interface_t *st, int is_add)
Damjan Marion83243a02016-02-29 13:09:30 +0100267{
268 /* Nothing for now */
269 return 0;
270}
271
Ray Kinsella2038ad02017-05-18 11:56:28 +0100272static clib_error_t *af_packet_set_mac_address_function
273 (struct vnet_hw_interface_t *hi, char *address)
274{
275 af_packet_main_t *apm = &af_packet_main;
276 af_packet_if_t *apif =
277 pool_elt_at_index (apm->interfaces, hi->dev_instance);
278 int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
279 struct ifreq ifr;
280
281 /* if interface is a bridge ignore */
282 if (apif->host_if_index < 0)
283 goto error; /* no error */
284
285 /* use host_if_index in case host name has changed */
286 ifr.ifr_ifindex = apif->host_if_index;
287 if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
288 {
289 clib_unix_warning
290 ("af_packet_%s ioctl could not retrieve eth name, error: %d",
291 apif->host_if_name, rv);
292 goto error;
293 }
294
295 clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
296 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
297
298 if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
299 {
300 clib_unix_warning ("af_packet_%s ioctl could not set mac, error: %d",
301 apif->host_if_name, rv);
302 goto error;
303 }
304
305error:
306 close (fd);
307
308 return 0; /* no error */
309}
310
Damjan Marion00a9dca2016-08-17 17:05:46 +0200311/* *INDENT-OFF* */
Damjan Marion83243a02016-02-29 13:09:30 +0100312VNET_DEVICE_CLASS (af_packet_device_class) = {
313 .name = "af-packet",
314 .tx_function = af_packet_interface_tx,
315 .format_device_name = format_af_packet_device_name,
316 .format_device = format_af_packet_device,
317 .format_tx_trace = format_af_packet_tx_trace,
318 .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
319 .tx_function_error_strings = af_packet_tx_func_error_strings,
320 .rx_redirect_to_node = af_packet_set_interface_next_node,
321 .clear_counters = af_packet_clear_hw_interface_counters,
322 .admin_up_down_function = af_packet_interface_admin_up_down,
323 .subif_add_del_function = af_packet_subif_add_del_function,
Ray Kinsella2038ad02017-05-18 11:56:28 +0100324 .mac_addr_change_function = af_packet_set_mac_address_function,
Alpesh Patel83cc4e12016-04-05 12:49:30 -0700325};
Damjan Marion1c80e832016-05-11 23:07:18 +0200326
327VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class,
328 af_packet_interface_tx)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200329/* *INDENT-ON* */
330
331/*
332 * fd.io coding-style-patch-verification: ON
333 *
334 * Local Variables:
335 * eval: (c-set-style "gnu")
336 * End:
337 */