VPP-1001 - update AF Packet Driver to for modern kernels
[vpp.git] / src / vnet / devices / af_packet / device.c
1 /*
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>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <net/if.h>
24 #include <net/if_arp.h>
25
26 #include <linux/virtio_net.h>
27
28 #include <vlib/vlib.h>
29 #include <vlib/unix/unix.h>
30 #include <vnet/ip/ip.h>
31 #include <vnet/ethernet/ethernet.h>
32
33 #include <vnet/devices/af_packet/af_packet.h>
34
35 #define foreach_af_packet_tx_func_error               \
36 _(FRAME_NOT_READY, "tx frame not ready")              \
37 _(TXRING_EAGAIN,   "tx sendto temporary failure")     \
38 _(TXRING_FATAL,    "tx sendto fatal failure")         \
39 _(TXRING_OVERRUN,  "tx ring overrun")
40
41 typedef enum
42 {
43 #define _(f,s) AF_PACKET_TX_ERROR_##f,
44   foreach_af_packet_tx_func_error
45 #undef _
46     AF_PACKET_TX_N_ERROR,
47 } af_packet_tx_func_error_t;
48
49 static char *af_packet_tx_func_error_strings[] = {
50 #define _(n,s) s,
51   foreach_af_packet_tx_func_error
52 #undef _
53 };
54
55 static u8 *
56 format_af_packet_device_name (u8 * s, va_list * args)
57 {
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);
61
62   s = format (s, "host-%s", apif->host_if_name);
63   return s;
64 }
65
66 static u8 *
67 format_af_packet_device (u8 * s, va_list * args)
68 {
69   s = format (s, "Linux PACKET socket interface");
70   return s;
71 }
72
73 static u8 *
74 format_af_packet_tx_trace (u8 * s, va_list * args)
75 {
76   s = format (s, "Unimplemented...");
77   return s;
78 }
79
80
81 static_always_inline void
82 af_packet_buffer_tx_offload (vlib_buffer_t * b, struct virtio_net_hdr *vhdr)
83 {
84   /* For now - just mark the data as valid,
85    * DPDK csums on input, tap presently operates in legacy
86    * compatibility mode where the kernel checksums CSUM_PARTIAL
87    * for it and we have fixed the af_packet input
88    *
89    * In the future, locally originated frames, etc can be made
90    * to fit this convention so that they are not checksummed
91    * unless needed.
92    **/
93   vhdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
94 }
95
96
97 static uword
98 af_packet_interface_tx (vlib_main_t * vm,
99                         vlib_node_runtime_t * node, vlib_frame_t * frame)
100 {
101   af_packet_main_t *apm = &af_packet_main;
102   u32 *buffers = vlib_frame_args (frame);
103   u32 n_left = frame->n_vectors;
104   u32 n_sent = 0;
105   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
106   af_packet_if_t *apif =
107     pool_elt_at_index (apm->interfaces, rd->dev_instance);
108   int block = 0;
109   u32 block_size = apif->tx_req->tp_block_size;
110   u32 frame_size = apif->tx_req->tp_frame_size;
111   u32 frame_num = apif->tx_req->tp_frame_nr;
112   u8 *block_start = apif->tx_ring + block * block_size;
113   u32 tx_frame = apif->next_tx_frame;
114   struct tpacket2_hdr *tph;
115   u32 frame_not_ready = 0;
116
117   clib_spinlock_lock_if_init (&apif->lockp);
118
119   while (n_left > 0)
120     {
121       u32 len;
122       u32 offset = 0;
123       if (PREDICT_TRUE ((apm->flags & AF_PACKET_USES_VNET_HEADERS) != 0))
124         {
125           offset = sizeof (struct virtio_net_hdr);
126         }
127       vlib_buffer_t *b0;
128       n_left--;
129       u32 bi = buffers[0];
130       buffers++;
131
132       tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size);
133
134       if (PREDICT_FALSE
135           (tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
136         {
137           frame_not_ready++;
138           goto next;
139         }
140
141       do
142         {
143           b0 = vlib_get_buffer (vm, bi);
144           if (PREDICT_TRUE ((apm->flags & AF_PACKET_USES_VNET_HEADERS) != 0))
145             {
146               u8 *vh =
147                 (u8 *) tph + TPACKET_ALIGN (sizeof (struct tpacket2_hdr));
148               af_packet_buffer_tx_offload (b0, (struct virtio_net_hdr *) vh);
149             }
150           len = b0->current_length;
151           clib_memcpy ((u8 *) tph +
152                        TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + offset,
153                        vlib_buffer_get_current (b0), len);
154           offset += len;
155         }
156       while ((bi =
157               (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
158
159       tph->tp_len = tph->tp_snaplen = offset;
160       tph->tp_status = TP_STATUS_SEND_REQUEST;
161       n_sent++;
162     next:
163       /* check if we've exhausted the ring */
164       if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
165         break;
166
167       tx_frame = (tx_frame + 1) % frame_num;
168     }
169
170   CLIB_MEMORY_BARRIER ();
171
172   if (PREDICT_TRUE (n_sent))
173     {
174       apif->next_tx_frame = tx_frame;
175
176       if (PREDICT_FALSE (sendto (apif->fd, NULL, 0,
177                                  MSG_DONTWAIT, NULL, 0) == -1))
178         {
179           /* Uh-oh, drop & move on, but count whether it was fatal or not.
180            * Note that we have no reliable way to properly determine the
181            * disposition of the packets we just enqueued for delivery.
182            */
183           vlib_error_count (vm, node->node_index,
184                             unix_error_is_fatal (errno) ?
185                             AF_PACKET_TX_ERROR_TXRING_FATAL :
186                             AF_PACKET_TX_ERROR_TXRING_EAGAIN, n_sent);
187         }
188     }
189
190   clib_spinlock_unlock_if_init (&apif->lockp);
191
192   if (PREDICT_FALSE (frame_not_ready))
193     vlib_error_count (vm, node->node_index,
194                       AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
195
196   if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
197     vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
198                       n_left);
199
200   vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
201   return frame->n_vectors;
202 }
203
204 static void
205 af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
206                                    u32 node_index)
207 {
208   af_packet_main_t *apm = &af_packet_main;
209   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
210   af_packet_if_t *apif =
211     pool_elt_at_index (apm->interfaces, hw->dev_instance);
212
213   /* Shut off redirection */
214   if (node_index == ~0)
215     {
216       apif->per_interface_next_index = node_index;
217       return;
218     }
219
220   apif->per_interface_next_index =
221     vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
222                         node_index);
223 }
224
225 static void
226 af_packet_clear_hw_interface_counters (u32 instance)
227 {
228   /* Nothing for now */
229 }
230
231 static clib_error_t *
232 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
233                                    u32 flags)
234 {
235   af_packet_main_t *apm = &af_packet_main;
236   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
237   af_packet_if_t *apif =
238     pool_elt_at_index (apm->interfaces, hw->dev_instance);
239   u32 hw_flags;
240   int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
241   struct ifreq ifr;
242
243   if (0 > fd)
244     {
245       clib_unix_warning ("af_packet_%s could not open socket",
246                          apif->host_if_name);
247       return 0;
248     }
249
250   /* if interface is a bridge ignore */
251   if (apif->host_if_index < 0)
252     goto error;                 /* no error */
253
254   /* use host_if_index in case host name has changed */
255   ifr.ifr_ifindex = apif->host_if_index;
256   if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
257     {
258       clib_unix_warning ("af_packet_%s ioctl could not retrieve eth name",
259                          apif->host_if_name);
260       goto error;
261     }
262
263   apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
264
265   if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
266     {
267       clib_unix_warning ("af_packet_%s error: %d",
268                          apif->is_admin_up ? "up" : "down", rv);
269       goto error;
270     }
271
272   if (apif->is_admin_up)
273     {
274       hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
275       ifr.ifr_flags |= IFF_UP;
276     }
277   else
278     {
279       hw_flags = 0;
280       ifr.ifr_flags &= ~IFF_UP;
281     }
282
283   if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
284     {
285       clib_unix_warning ("af_packet_%s error: %d",
286                          apif->is_admin_up ? "up" : "down", rv);
287       goto error;
288     }
289
290   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
291
292 error:
293   if (0 <= fd)
294     close (fd);
295
296   return 0;                     /* no error */
297 }
298
299 static clib_error_t *
300 af_packet_subif_add_del_function (vnet_main_t * vnm,
301                                   u32 hw_if_index,
302                                   struct vnet_sw_interface_t *st, int is_add)
303 {
304   /* Nothing for now */
305   return 0;
306 }
307
308 static clib_error_t *af_packet_set_mac_address_function
309   (struct vnet_hw_interface_t *hi, char *address)
310 {
311   af_packet_main_t *apm = &af_packet_main;
312   af_packet_if_t *apif =
313     pool_elt_at_index (apm->interfaces, hi->dev_instance);
314   int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
315   struct ifreq ifr;
316
317   if (0 > fd)
318     {
319       clib_unix_warning ("af_packet_%s could not open socket",
320                          apif->host_if_name);
321       return 0;
322     }
323
324   /* if interface is a bridge ignore */
325   if (apif->host_if_index < 0)
326     goto error;                 /* no error */
327
328   /* use host_if_index in case host name has changed */
329   ifr.ifr_ifindex = apif->host_if_index;
330   if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
331     {
332       clib_unix_warning
333         ("af_packet_%s ioctl could not retrieve eth name, error: %d",
334          apif->host_if_name, rv);
335       goto error;
336     }
337
338   clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
339   ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
340
341   if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
342     {
343       clib_unix_warning ("af_packet_%s ioctl could not set mac, error: %d",
344                          apif->host_if_name, rv);
345       goto error;
346     }
347
348 error:
349
350   if (0 <= fd)
351     close (fd);
352
353   return 0;                     /* no error */
354 }
355
356 /* *INDENT-OFF* */
357 VNET_DEVICE_CLASS (af_packet_device_class) = {
358   .name = "af-packet",
359   .tx_function = af_packet_interface_tx,
360   .format_device_name = format_af_packet_device_name,
361   .format_device = format_af_packet_device,
362   .format_tx_trace = format_af_packet_tx_trace,
363   .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
364   .tx_function_error_strings = af_packet_tx_func_error_strings,
365   .rx_redirect_to_node = af_packet_set_interface_next_node,
366   .clear_counters = af_packet_clear_hw_interface_counters,
367   .admin_up_down_function = af_packet_interface_admin_up_down,
368   .subif_add_del_function = af_packet_subif_add_del_function,
369   .mac_addr_change_function = af_packet_set_mac_address_function,
370 };
371
372 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class,
373                                    af_packet_interface_tx)
374 /* *INDENT-ON* */
375
376 /*
377  * fd.io coding-style-patch-verification: ON
378  *
379  * Local Variables:
380  * eval: (c-set-style "gnu")
381  * End:
382  */