devices: add support for offloads
[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 <vlib/vlib.h>
27 #include <vlib/unix/unix.h>
28 #include <vnet/ip/ip.h>
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/ip/ip4_packet.h>
31 #include <vnet/ip/ip6_packet.h>
32 #include <vnet/ip/ip_psh_cksum.h>
33 #include <vnet/tcp/tcp_packet.h>
34 #include <vnet/udp/udp_packet.h>
35
36 #include <vnet/devices/af_packet/af_packet.h>
37 #include <vnet/devices/virtio/virtio_std.h>
38
39 #define foreach_af_packet_tx_func_error               \
40 _(FRAME_NOT_READY, "tx frame not ready")              \
41 _(TXRING_EAGAIN,   "tx sendto temporary failure")     \
42 _(TXRING_FATAL,    "tx sendto fatal failure")         \
43 _(TXRING_OVERRUN,  "tx ring overrun")
44
45 typedef enum
46 {
47 #define _(f,s) AF_PACKET_TX_ERROR_##f,
48   foreach_af_packet_tx_func_error
49 #undef _
50     AF_PACKET_TX_N_ERROR,
51 } af_packet_tx_func_error_t;
52
53 static char *af_packet_tx_func_error_strings[] = {
54 #define _(n,s) s,
55   foreach_af_packet_tx_func_error
56 #undef _
57 };
58
59
60 #ifndef CLIB_MARCH_VARIANT
61 u8 *
62 format_af_packet_device_name (u8 * s, va_list * args)
63 {
64   u32 i = va_arg (*args, u32);
65   af_packet_main_t *apm = &af_packet_main;
66   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, i);
67
68   s = format (s, "host-%s", apif->host_if_name);
69   return s;
70 }
71 #endif /* CLIB_MARCH_VARIANT */
72
73 static u8 *
74 format_af_packet_device (u8 * s, va_list * args)
75 {
76   u32 dev_instance = va_arg (*args, u32);
77   u32 indent = format_get_indent (s);
78   int __clib_unused verbose = va_arg (*args, int);
79
80   af_packet_main_t *apm = &af_packet_main;
81   af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, dev_instance);
82   clib_spinlock_lock_if_init (&apif->lockp);
83   u32 tx_block_sz = apif->tx_req->tp_block_size;
84   u32 tx_frame_sz = apif->tx_req->tp_frame_size;
85   u32 tx_frame_nr = apif->tx_req->tp_frame_nr;
86   u32 tx_block_nr = apif->tx_req->tp_block_nr;
87   u32 rx_block_size = apif->rx_req->tp_block_size;
88   u32 rx_frame_size = apif->rx_req->tp_frame_size;
89   u32 rx_frame_nr = apif->rx_req->tp_frame_nr;
90   u32 rx_block_nr = apif->rx_req->tp_block_nr;
91   int block = 0;
92   u8 *tx_block_start = apif->tx_ring[block];
93   u32 tx_frame = apif->next_tx_frame;
94   tpacket3_hdr_t *tph;
95
96   s = format (s, "Linux PACKET socket interface\n");
97   s = format (s, "%UTX block size:%d nr:%d  TX frame size:%d nr:%d\n",
98               format_white_space, indent, tx_block_sz, tx_block_nr,
99               tx_frame_sz, tx_frame_nr);
100   s = format (s, "%URX block size:%d nr:%d  RX frame size:%d nr:%d\n",
101               format_white_space, indent, rx_block_size, rx_block_nr,
102               rx_frame_size, rx_frame_nr);
103   s = format (s, "%Unext frame:%d\n", format_white_space, indent,
104               apif->next_tx_frame);
105
106   int n_send_req = 0, n_avail = 0, n_sending = 0, n_tot = 0, n_wrong = 0;
107   do
108     {
109       tph = (tpacket3_hdr_t *) (tx_block_start + tx_frame * tx_frame_sz);
110       tx_frame = (tx_frame + 1) % tx_frame_nr;
111       if (tph->tp_status == 0)
112         n_avail++;
113       else if (tph->tp_status & TP_STATUS_SEND_REQUEST)
114         n_send_req++;
115       else if (tph->tp_status & TP_STATUS_SENDING)
116         n_sending++;
117       else
118         n_wrong++;
119       n_tot++;
120     }
121   while (tx_frame != apif->next_tx_frame);
122   s = format (s, "%Uavailable:%d request:%d sending:%d wrong:%d total:%d\n",
123               format_white_space, indent, n_avail, n_send_req, n_sending,
124               n_wrong, n_tot);
125
126   clib_spinlock_unlock_if_init (&apif->lockp);
127   return s;
128 }
129
130 static u8 *
131 format_af_packet_tx_trace (u8 * s, va_list * args)
132 {
133   s = format (s, "Unimplemented...");
134   return s;
135 }
136
137 static_always_inline void
138 fill_gso_offload (vlib_buffer_t *b0, vnet_virtio_net_hdr_t *vnet_hdr)
139 {
140   vnet_buffer_oflags_t oflags = vnet_buffer (b0)->oflags;
141   if (b0->flags & VNET_BUFFER_F_IS_IP4)
142     {
143       ip4_header_t *ip4;
144       vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
145       vnet_hdr->gso_size = vnet_buffer2 (b0)->gso_size;
146       vnet_hdr->hdr_len =
147         vnet_buffer (b0)->l4_hdr_offset + vnet_buffer2 (b0)->gso_l4_hdr_sz;
148       vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
149       vnet_hdr->csum_start = vnet_buffer (b0)->l4_hdr_offset; // 0x22;
150       vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
151       ip4 = (ip4_header_t *) (vlib_buffer_get_current (b0) +
152                               vnet_buffer (b0)->l3_hdr_offset);
153       if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
154         ip4->checksum = ip4_header_checksum (ip4);
155     }
156   else if (b0->flags & VNET_BUFFER_F_IS_IP6)
157     {
158       vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
159       vnet_hdr->gso_size = vnet_buffer2 (b0)->gso_size;
160       vnet_hdr->hdr_len =
161         vnet_buffer (b0)->l4_hdr_offset + vnet_buffer2 (b0)->gso_l4_hdr_sz;
162       vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
163       vnet_hdr->csum_start = vnet_buffer (b0)->l4_hdr_offset; // 0x36;
164       vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
165     }
166 }
167
168 static_always_inline void
169 fill_cksum_offload (vlib_buffer_t *b0, vnet_virtio_net_hdr_t *vnet_hdr)
170 {
171   vnet_buffer_oflags_t oflags = vnet_buffer (b0)->oflags;
172   if (b0->flags & VNET_BUFFER_F_IS_IP4)
173     {
174       ip4_header_t *ip4;
175       ip4 = (ip4_header_t *) (vlib_buffer_get_current (b0) +
176                               vnet_buffer (b0)->l3_hdr_offset);
177       if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM)
178         ip4->checksum = ip4_header_checksum (ip4);
179       vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
180       vnet_hdr->csum_start = 0x22;
181       if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
182         {
183           tcp_header_t *tcp =
184             (tcp_header_t *) (vlib_buffer_get_current (b0) +
185                               vnet_buffer (b0)->l4_hdr_offset);
186           tcp->checksum = ip4_pseudo_header_cksum (ip4);
187           vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
188         }
189       else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
190         {
191           udp_header_t *udp =
192             (udp_header_t *) (vlib_buffer_get_current (b0) +
193                               vnet_buffer (b0)->l4_hdr_offset);
194           udp->checksum = ip4_pseudo_header_cksum (ip4);
195           vnet_hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
196         }
197     }
198   else if (b0->flags & VNET_BUFFER_F_IS_IP6)
199     {
200       ip6_header_t *ip6;
201       vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
202       vnet_hdr->csum_start = 0x36;
203       ip6 = (ip6_header_t *) (vlib_buffer_get_current (b0) +
204                               vnet_buffer (b0)->l3_hdr_offset);
205       if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM)
206         {
207           tcp_header_t *tcp =
208             (tcp_header_t *) (vlib_buffer_get_current (b0) +
209                               vnet_buffer (b0)->l4_hdr_offset);
210           tcp->checksum = ip6_pseudo_header_cksum (ip6);
211           vnet_hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
212         }
213       else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)
214         {
215           udp_header_t *udp =
216             (udp_header_t *) (vlib_buffer_get_current (b0) +
217                               vnet_buffer (b0)->l4_hdr_offset);
218           udp->checksum = ip6_pseudo_header_cksum (ip6);
219           vnet_hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
220         }
221     }
222 }
223
224 VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (vlib_main_t * vm,
225                                                   vlib_node_runtime_t * node,
226                                                   vlib_frame_t * frame)
227 {
228   af_packet_main_t *apm = &af_packet_main;
229   u32 *buffers = vlib_frame_vector_args (frame);
230   u32 n_left = frame->n_vectors;
231   u32 n_sent = 0;
232   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
233   af_packet_if_t *apif =
234     pool_elt_at_index (apm->interfaces, rd->dev_instance);
235   clib_spinlock_lock_if_init (&apif->lockp);
236   u32 block = 0;
237   u32 frame_size = apif->tx_req->tp_frame_size;
238   u32 frame_num = apif->tx_req->tp_frame_nr;
239   u8 *block_start = apif->tx_ring[block];
240   u32 tx_frame = apif->next_tx_frame;
241   tpacket3_hdr_t *tph;
242   u32 frame_not_ready = 0;
243   u8 is_cksum_gso_enabled = (apif->is_cksum_gso_enabled == 1) ? 1 : 0;
244
245   while (n_left)
246     {
247       u32 len;
248       vnet_virtio_net_hdr_t *vnet_hdr = 0;
249       u32 offset = 0;
250       vlib_buffer_t *b0 = 0;
251       u32 bi = buffers[0];
252
253       n_left--;
254       buffers++;
255
256       tph = (tpacket3_hdr_t *) (block_start + tx_frame * frame_size);
257       if (PREDICT_FALSE (tph->tp_status &
258                          (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)))
259         {
260           frame_not_ready++;
261           goto next;
262         }
263
264       b0 = vlib_get_buffer (vm, bi);
265
266       if (is_cksum_gso_enabled)
267         {
268           vnet_hdr =
269             (vnet_virtio_net_hdr_t *) ((u8 *) tph + TPACKET_ALIGN (sizeof (
270                                                       tpacket3_hdr_t)));
271
272           clib_memset_u8 (vnet_hdr, 0, sizeof (vnet_virtio_net_hdr_t));
273           offset = sizeof (vnet_virtio_net_hdr_t);
274
275           if (b0->flags & VNET_BUFFER_F_GSO)
276             fill_gso_offload (b0, vnet_hdr);
277           else if (b0->flags & VNET_BUFFER_F_OFFLOAD)
278             fill_cksum_offload (b0, vnet_hdr);
279         }
280
281       len = b0->current_length;
282       clib_memcpy_fast ((u8 *) tph + TPACKET_ALIGN (sizeof (tpacket3_hdr_t)) +
283                           offset,
284                         vlib_buffer_get_current (b0), len);
285       offset += len;
286
287       while (b0->flags & VLIB_BUFFER_NEXT_PRESENT)
288         {
289           b0 = vlib_get_buffer (vm, b0->next_buffer);
290           len = b0->current_length;
291           clib_memcpy_fast ((u8 *) tph +
292                               TPACKET_ALIGN (sizeof (tpacket3_hdr_t)) + offset,
293                             vlib_buffer_get_current (b0), len);
294           offset += len;
295         }
296
297       tph->tp_len = tph->tp_snaplen = offset;
298       tph->tp_status = TP_STATUS_SEND_REQUEST;
299       n_sent++;
300
301       tx_frame = (tx_frame + 1) % frame_num;
302
303     next:
304       /* check if we've exhausted the ring */
305       if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
306         break;
307     }
308
309   CLIB_MEMORY_BARRIER ();
310
311   if (PREDICT_TRUE (n_sent))
312     {
313       apif->next_tx_frame = tx_frame;
314
315       if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) ==
316                          -1))
317         {
318           /* Uh-oh, drop & move on, but count whether it was fatal or not.
319            * Note that we have no reliable way to properly determine the
320            * disposition of the packets we just enqueued for delivery.
321            */
322           vlib_error_count (vm, node->node_index,
323                             unix_error_is_fatal (errno) ?
324                               AF_PACKET_TX_ERROR_TXRING_FATAL :
325                               AF_PACKET_TX_ERROR_TXRING_EAGAIN,
326                             n_sent);
327         }
328     }
329
330   clib_spinlock_unlock_if_init (&apif->lockp);
331
332   if (PREDICT_FALSE (frame_not_ready))
333     vlib_error_count (vm, node->node_index,
334                       AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
335
336   if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
337     vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN,
338                       n_left);
339
340   vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors);
341   return frame->n_vectors;
342 }
343
344 static void
345 af_packet_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
346                                    u32 node_index)
347 {
348   af_packet_main_t *apm = &af_packet_main;
349   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
350   af_packet_if_t *apif =
351     pool_elt_at_index (apm->interfaces, hw->dev_instance);
352
353   /* Shut off redirection */
354   if (node_index == ~0)
355     {
356       apif->per_interface_next_index = node_index;
357       return;
358     }
359
360   apif->per_interface_next_index =
361     vlib_node_add_next (vlib_get_main (), af_packet_input_node.index,
362                         node_index);
363 }
364
365 static void
366 af_packet_clear_hw_interface_counters (u32 instance)
367 {
368   /* Nothing for now */
369 }
370
371 static clib_error_t *
372 af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
373                                    u32 flags)
374 {
375   af_packet_main_t *apm = &af_packet_main;
376   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
377   af_packet_if_t *apif =
378     pool_elt_at_index (apm->interfaces, hw->dev_instance);
379   u32 hw_flags;
380   int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
381   struct ifreq ifr;
382
383   if (0 > fd)
384     {
385       vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
386                      apif->host_if_name);
387       return 0;
388     }
389
390   /* if interface is a bridge ignore */
391   if (apif->host_if_index < 0)
392     goto error;                 /* no error */
393
394   /* use host_if_index in case host name has changed */
395   ifr.ifr_ifindex = apif->host_if_index;
396   if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
397     {
398       vlib_log_warn (apm->log_class,
399                      "af_packet_%s ioctl could not retrieve eth name",
400                      apif->host_if_name);
401       goto error;
402     }
403
404   apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
405
406   if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
407     {
408       vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
409                      apif->is_admin_up ? "up" : "down", rv);
410       goto error;
411     }
412
413   if (apif->is_admin_up)
414     {
415       hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
416       ifr.ifr_flags |= IFF_UP;
417     }
418   else
419     {
420       hw_flags = 0;
421       ifr.ifr_flags &= ~IFF_UP;
422     }
423
424   if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
425     {
426       vlib_log_warn (apm->log_class, "af_packet_%s error: %d",
427                      apif->is_admin_up ? "up" : "down", rv);
428       goto error;
429     }
430
431   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
432
433 error:
434   if (0 <= fd)
435     close (fd);
436
437   return 0;                     /* no error */
438 }
439
440 static clib_error_t *
441 af_packet_subif_add_del_function (vnet_main_t * vnm,
442                                   u32 hw_if_index,
443                                   struct vnet_sw_interface_t *st, int is_add)
444 {
445   /* Nothing for now */
446   return 0;
447 }
448
449 static clib_error_t *af_packet_set_mac_address_function
450   (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address)
451 {
452   af_packet_main_t *apm = &af_packet_main;
453   af_packet_if_t *apif =
454     pool_elt_at_index (apm->interfaces, hi->dev_instance);
455   int rv, fd;
456   struct ifreq ifr;
457
458   if (apif->mode == AF_PACKET_IF_MODE_IP)
459     {
460       vlib_log_warn (apm->log_class, "af_packet_%s interface is in IP mode",
461                      apif->host_if_name);
462       return clib_error_return (0,
463                                 " MAC update failed, interface is in IP mode");
464     }
465
466   fd = socket (AF_UNIX, SOCK_DGRAM, 0);
467   if (0 > fd)
468     {
469       vlib_log_warn (apm->log_class, "af_packet_%s could not open socket",
470                      apif->host_if_name);
471       return 0;
472     }
473
474   /* if interface is a bridge ignore */
475   if (apif->host_if_index < 0)
476     goto error;                 /* no error */
477
478   /* use host_if_index in case host name has changed */
479   ifr.ifr_ifindex = apif->host_if_index;
480   if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
481     {
482       vlib_log_warn
483         (apm->log_class,
484          "af_packet_%s ioctl could not retrieve eth name, error: %d",
485          apif->host_if_name, rv);
486       goto error;
487     }
488
489   clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
490   ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
491
492   if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
493     {
494       vlib_log_warn (apm->log_class,
495                      "af_packet_%s ioctl could not set mac, error: %d",
496                      apif->host_if_name, rv);
497       goto error;
498     }
499
500 error:
501
502   if (0 <= fd)
503     close (fd);
504
505   return 0;                     /* no error */
506 }
507
508 VNET_DEVICE_CLASS (af_packet_device_class) = {
509   .name = "af-packet",
510   .format_device_name = format_af_packet_device_name,
511   .format_device = format_af_packet_device,
512   .format_tx_trace = format_af_packet_tx_trace,
513   .tx_function_n_errors = AF_PACKET_TX_N_ERROR,
514   .tx_function_error_strings = af_packet_tx_func_error_strings,
515   .rx_redirect_to_node = af_packet_set_interface_next_node,
516   .clear_counters = af_packet_clear_hw_interface_counters,
517   .admin_up_down_function = af_packet_interface_admin_up_down,
518   .subif_add_del_function = af_packet_subif_add_del_function,
519   .mac_addr_change_function = af_packet_set_mac_address_function,
520 };
521
522 /*
523  * fd.io coding-style-patch-verification: ON
524  *
525  * Local Variables:
526  * eval: (c-set-style "gnu")
527  * End:
528  */