af_packet: fix the device input feature arc support
[vpp.git] / src / plugins / af_packet / node.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
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/interface/rx_queue_funcs.h>
27 #include <vnet/feature/feature.h>
28 #include <vnet/ethernet/packet.h>
29
30 #include <af_packet/af_packet.h>
31 #include <vnet/devices/virtio/virtio_std.h>
32
33 #define foreach_af_packet_input_error                                         \
34   _ (PARTIAL_PKT, "partial packet")                                           \
35   _ (TIMEDOUT_BLK, "timed out block")                                         \
36   _ (TOTAL_RECV_BLK, "total received block")
37 typedef enum
38 {
39 #define _(f,s) AF_PACKET_INPUT_ERROR_##f,
40   foreach_af_packet_input_error
41 #undef _
42     AF_PACKET_INPUT_N_ERROR,
43 } af_packet_input_error_t;
44
45 static char *af_packet_input_error_strings[] = {
46 #define _(n,s) s,
47   foreach_af_packet_input_error
48 #undef _
49 };
50
51 typedef struct
52 {
53   u32 next_index;
54   u32 hw_if_index;
55   u16 queue_id;
56   int block;
57   u32 pkt_num;
58   void *block_start;
59   block_desc_t bd;
60   union
61   {
62     tpacket3_hdr_t tph3;
63     tpacket2_hdr_t tph2;
64   };
65   vnet_virtio_net_hdr_t vnet_hdr;
66   u8 is_v3;
67 } af_packet_input_trace_t;
68
69 static u8 *
70 format_af_packet_input_trace (u8 * s, va_list * args)
71 {
72   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74   af_packet_input_trace_t *t = va_arg (*args, af_packet_input_trace_t *);
75   u32 indent = format_get_indent (s);
76
77   s = format (s, "af_packet: hw_if_index %d rx-queue %u next-index %d",
78               t->hw_if_index, t->queue_id, t->next_index);
79
80   if (t->is_v3)
81     {
82       s = format (
83         s, "\n%Ublock %u:\n%Uaddress %p version %u seq_num %lu pkt_num %u",
84         format_white_space, indent + 2, t->block, format_white_space,
85         indent + 4, t->block_start, t->bd.version, t->bd.hdr.bh1.seq_num,
86         t->pkt_num);
87       s = format (
88         s,
89         "\n%Utpacket3_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
90         "\n%Usec 0x%x nsec 0x%x vlan %U"
91 #ifdef TP_STATUS_VLAN_TPID_VALID
92         " vlan_tpid %u"
93 #endif
94         ,
95         format_white_space, indent + 2, format_white_space, indent + 4,
96         t->tph3.tp_status, t->tph3.tp_len, t->tph3.tp_snaplen, t->tph3.tp_mac,
97         t->tph3.tp_net, format_white_space, indent + 4, t->tph3.tp_sec,
98         t->tph3.tp_nsec, format_ethernet_vlan_tci, t->tph3.hv1.tp_vlan_tci
99 #ifdef TP_STATUS_VLAN_TPID_VALID
100         ,
101         t->tph3.hv1.tp_vlan_tpid
102 #endif
103       );
104     }
105   else
106     {
107       s = format (
108         s,
109         "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u"
110         "\n%Usec 0x%x nsec 0x%x vlan %U"
111 #ifdef TP_STATUS_VLAN_TPID_VALID
112         " vlan_tpid %u"
113 #endif
114         ,
115         format_white_space, indent + 2, format_white_space, indent + 4,
116         t->tph2.tp_status, t->tph2.tp_len, t->tph2.tp_snaplen, t->tph2.tp_mac,
117         t->tph2.tp_net, format_white_space, indent + 4, t->tph2.tp_sec,
118         t->tph2.tp_nsec, format_ethernet_vlan_tci, t->tph2.tp_vlan_tci
119 #ifdef TP_STATUS_VLAN_TPID_VALID
120         ,
121         t->tph2.tp_vlan_tpid
122 #endif
123       );
124     }
125
126   s = format (s,
127               "\n%Uvnet-hdr:\n%Uflags 0x%02x gso_type 0x%02x hdr_len %u"
128               "\n%Ugso_size %u csum_start %u csum_offset %u",
129               format_white_space, indent + 2, format_white_space, indent + 4,
130               t->vnet_hdr.flags, t->vnet_hdr.gso_type, t->vnet_hdr.hdr_len,
131               format_white_space, indent + 4, t->vnet_hdr.gso_size,
132               t->vnet_hdr.csum_start, t->vnet_hdr.csum_offset);
133   return s;
134 }
135
136 always_inline void
137 buffer_add_to_chain (vlib_buffer_t *b, vlib_buffer_t *first_b,
138                      vlib_buffer_t *prev_b, u32 bi)
139 {
140   /* update first buffer */
141   first_b->total_length_not_including_first_buffer += b->current_length;
142
143   /* update previous buffer */
144   prev_b->next_buffer = bi;
145   prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
146
147   /* update current buffer */
148   b->next_buffer = ~0;
149 }
150
151 static_always_inline void
152 fill_gso_offload (vlib_buffer_t *b, u32 gso_size, u8 l4_hdr_sz)
153 {
154   b->flags |= VNET_BUFFER_F_GSO;
155   vnet_buffer2 (b)->gso_size = gso_size;
156   vnet_buffer2 (b)->gso_l4_hdr_sz = l4_hdr_sz;
157 }
158
159 static_always_inline void
160 fill_cksum_offload (vlib_buffer_t *b, u8 *l4_hdr_sz, u8 is_ip)
161 {
162   vnet_buffer_oflags_t oflags = 0;
163   u16 l2hdr_sz = 0;
164   u16 ethertype = 0;
165   u8 l4_proto = 0;
166
167   if (is_ip)
168     {
169       switch (b->data[0] & 0xf0)
170         {
171         case 0x40:
172           ethertype = ETHERNET_TYPE_IP4;
173           break;
174         case 0x60:
175           ethertype = ETHERNET_TYPE_IP6;
176           break;
177         }
178     }
179   else
180     {
181       ethernet_header_t *eth = (ethernet_header_t *) b->data;
182       ethertype = clib_net_to_host_u16 (eth->type);
183       l2hdr_sz = sizeof (ethernet_header_t);
184       if (ethernet_frame_is_tagged (ethertype))
185         {
186           ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eth + 1);
187
188           ethertype = clib_net_to_host_u16 (vlan->type);
189           l2hdr_sz += sizeof (*vlan);
190           if (ethertype == ETHERNET_TYPE_VLAN)
191             {
192               vlan++;
193               ethertype = clib_net_to_host_u16 (vlan->type);
194               l2hdr_sz += sizeof (*vlan);
195             }
196         }
197     }
198
199   vnet_buffer (b)->l2_hdr_offset = 0;
200   vnet_buffer (b)->l3_hdr_offset = l2hdr_sz;
201
202   if (ethertype == ETHERNET_TYPE_IP4)
203     {
204       ip4_header_t *ip4 = (ip4_header_t *) (b->data + l2hdr_sz);
205       vnet_buffer (b)->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
206       b->flags |= (VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
207                    VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
208                    VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
209
210       l4_proto = ip4->protocol;
211     }
212   else if (ethertype == ETHERNET_TYPE_IP6)
213     {
214       ip6_header_t *ip6 = (ip6_header_t *) (b->data + l2hdr_sz);
215       b->flags |= (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
216                    VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
217                    VNET_BUFFER_F_L4_HDR_OFFSET_VALID);
218       u16 ip6_hdr_len = sizeof (ip6_header_t);
219
220       if (ip6_ext_hdr (ip6->protocol))
221         {
222           ip6_ext_header_t *p = (void *) (ip6 + 1);
223           ip6_hdr_len += ip6_ext_header_len (p);
224           while (ip6_ext_hdr (p->next_hdr))
225             {
226               ip6_hdr_len += ip6_ext_header_len (p);
227               p = ip6_ext_next_header (p);
228             }
229           l4_proto = p->next_hdr;
230         }
231       else
232         l4_proto = ip6->protocol;
233       vnet_buffer (b)->l4_hdr_offset = l2hdr_sz + ip6_hdr_len;
234     }
235
236   if (l4_proto == IP_PROTOCOL_TCP)
237     {
238       oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
239       tcp_header_t *tcp =
240         (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
241       *l4_hdr_sz = tcp_header_bytes (tcp);
242     }
243   else if (l4_proto == IP_PROTOCOL_UDP)
244     {
245       oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
246       *l4_hdr_sz = sizeof (udp_header_t);
247     }
248
249   if (oflags)
250     vnet_buffer_offload_flags_set (b, oflags);
251 }
252
253 always_inline uword
254 af_packet_v3_device_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
255                               vlib_frame_t *frame, af_packet_if_t *apif,
256                               u16 queue_id, u8 is_cksum_gso_enabled)
257 {
258   af_packet_main_t *apm = &af_packet_main;
259   af_packet_queue_t *rx_queue = vec_elt_at_index (apif->rx_queues, queue_id);
260   tpacket3_hdr_t *tph;
261   u32 next_index;
262   u32 n_free_bufs;
263   u32 n_rx_packets = 0;
264   u32 n_rx_bytes = 0;
265   u32 timedout_blk = 0;
266   u32 total = 0;
267   u32 *to_next = 0;
268   u32 block = rx_queue->next_rx_block;
269   u32 block_nr = rx_queue->rx_req->req3.tp_block_nr;
270   u8 *block_start = 0;
271   uword n_trace = vlib_get_trace_count (vm, node);
272   u32 thread_index = vm->thread_index;
273   u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
274   u32 min_bufs = rx_queue->rx_req->req3.tp_frame_size / n_buffer_bytes;
275   u32 num_pkts = 0;
276   u32 rx_frame_offset = 0;
277   block_desc_t *bd = 0;
278   u32 sw_if_index = apif->sw_if_index;
279   u8 is_ip = (apif->mode == AF_PACKET_IF_MODE_IP);
280
281   if (is_ip)
282     next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
283   else
284     next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
285
286   if ((((block_desc_t *) (block_start = rx_queue->rx_ring[block]))
287          ->hdr.bh1.block_status &
288        TP_STATUS_USER) != 0)
289     {
290       u32 n_required = 0;
291       bd = (block_desc_t *) block_start;
292
293       if (PREDICT_FALSE (rx_queue->is_rx_pending))
294         {
295           num_pkts = rx_queue->num_rx_pkts;
296           rx_frame_offset = rx_queue->rx_frame_offset;
297           rx_queue->is_rx_pending = 0;
298         }
299       else
300         {
301           num_pkts = bd->hdr.bh1.num_pkts;
302           rx_frame_offset = bd->hdr.bh1.offset_to_first_pkt;
303           total++;
304
305           if (TP_STATUS_BLK_TMO & bd->hdr.bh1.block_status)
306             timedout_blk++;
307         }
308
309       n_required = clib_max (num_pkts, VLIB_FRAME_SIZE);
310       n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
311       if (PREDICT_FALSE (n_free_bufs < n_required))
312         {
313           vec_validate (apm->rx_buffers[thread_index],
314                         n_required + n_free_bufs - 1);
315           n_free_bufs += vlib_buffer_alloc (
316             vm, &apm->rx_buffers[thread_index][n_free_bufs], n_required);
317           vec_set_len (apm->rx_buffers[thread_index], n_free_bufs);
318         }
319
320       while (num_pkts && (n_free_bufs >= min_bufs))
321         {
322           u32 next0 = next_index;
323           u32 n_left_to_next;
324
325           vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
326
327           while (num_pkts && n_left_to_next && (n_free_bufs >= min_bufs))
328             {
329               tph = (tpacket3_hdr_t *) (block_start + rx_frame_offset);
330
331               if (num_pkts > 1)
332                 CLIB_PREFETCH (block_start + rx_frame_offset +
333                                  tph->tp_next_offset,
334                                2 * CLIB_CACHE_LINE_BYTES, LOAD);
335
336               vlib_buffer_t *b0 = 0, *first_b0 = 0, *prev_b0 = 0;
337               vnet_virtio_net_hdr_t *vnet_hdr = 0;
338               u32 data_len = tph->tp_snaplen;
339               u32 offset = 0;
340               u32 bi0 = ~0, first_bi0 = ~0;
341               u8 l4_hdr_sz = 0;
342
343               if (is_cksum_gso_enabled)
344                 vnet_hdr =
345                   (vnet_virtio_net_hdr_t *) ((u8 *) tph + tph->tp_mac -
346                                              sizeof (vnet_virtio_net_hdr_t));
347
348               // save current state and return
349               if (PREDICT_FALSE (((data_len / n_buffer_bytes) + 1) >
350                                  vec_len (apm->rx_buffers[thread_index])))
351                 {
352                   rx_queue->rx_frame_offset = rx_frame_offset;
353                   rx_queue->num_rx_pkts = num_pkts;
354                   rx_queue->is_rx_pending = 1;
355                   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
356                   goto done;
357                 }
358
359               while (data_len)
360                 {
361                   /* grab free buffer */
362                   u32 last_empty_buffer =
363                     vec_len (apm->rx_buffers[thread_index]) - 1;
364                   bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
365                   vec_set_len (apm->rx_buffers[thread_index],
366                                last_empty_buffer);
367                   n_free_bufs--;
368
369                   /* copy data */
370                   u32 bytes_to_copy =
371                     data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
372                   u32 vlan_len = 0;
373                   u32 bytes_copied = 0;
374
375                   b0 = vlib_get_buffer (vm, bi0);
376                   b0->current_data = 0;
377
378                   /* Kernel removes VLAN headers, so reconstruct VLAN */
379                   if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
380                     {
381                       if (PREDICT_TRUE (offset == 0))
382                         {
383                           clib_memcpy_fast (vlib_buffer_get_current (b0),
384                                             (u8 *) tph + tph->tp_mac,
385                                             sizeof (ethernet_header_t));
386                           ethernet_header_t *eth =
387                             vlib_buffer_get_current (b0);
388                           ethernet_vlan_header_t *vlan =
389                             (ethernet_vlan_header_t *) (eth + 1);
390                           vlan->priority_cfi_and_id =
391                             clib_host_to_net_u16 (tph->hv1.tp_vlan_tci);
392                           vlan->type = eth->type;
393                           eth->type =
394                             clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
395                           vlan_len = sizeof (ethernet_vlan_header_t);
396                           bytes_copied = sizeof (ethernet_header_t);
397                         }
398                     }
399                   clib_memcpy_fast (((u8 *) vlib_buffer_get_current (b0)) +
400                                       bytes_copied + vlan_len,
401                                     (u8 *) tph + tph->tp_mac + offset +
402                                       bytes_copied,
403                                     (bytes_to_copy - bytes_copied));
404
405                   /* fill buffer header */
406                   b0->current_length = bytes_to_copy + vlan_len;
407
408                   if (offset == 0)
409                     {
410                       b0->total_length_not_including_first_buffer = 0;
411                       b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
412                       vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
413                       vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~0;
414                       first_b0 = b0;
415                       first_bi0 = bi0;
416                       if (is_cksum_gso_enabled)
417                         {
418                           if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
419                             fill_cksum_offload (first_b0, &l4_hdr_sz, is_ip);
420                           if (vnet_hdr->gso_type & (VIRTIO_NET_HDR_GSO_TCPV4 |
421                                                     VIRTIO_NET_HDR_GSO_TCPV6))
422                             fill_gso_offload (first_b0, vnet_hdr->gso_size,
423                                               l4_hdr_sz);
424                         }
425                     }
426                   else
427                     buffer_add_to_chain (b0, first_b0, prev_b0, bi0);
428
429                   prev_b0 = b0;
430                   offset += bytes_to_copy;
431                   data_len -= bytes_to_copy;
432                 }
433               n_rx_packets++;
434               n_rx_bytes += tph->tp_snaplen;
435               to_next[0] = first_bi0;
436               to_next += 1;
437               n_left_to_next--;
438
439               /* drop partial packets */
440               if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen))
441                 {
442                   next0 = VNET_DEVICE_INPUT_NEXT_DROP;
443                   first_b0->error =
444                     node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT];
445                 }
446               else
447                 {
448                   if (PREDICT_FALSE (apif->mode == AF_PACKET_IF_MODE_IP))
449                     {
450                       switch (first_b0->data[0] & 0xf0)
451                         {
452                         case 0x40:
453                           next0 = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
454                           break;
455                         case 0x60:
456                           next0 = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
457                           break;
458                         default:
459                           next0 = VNET_DEVICE_INPUT_NEXT_DROP;
460                           break;
461                         }
462                       if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
463                         next0 = apif->per_interface_next_index;
464                     }
465                   else
466                     {
467                       next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
468                       if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
469                         next0 = apif->per_interface_next_index;
470                       /* redirect if feature path enabled */
471                       vnet_feature_start_device_input (sw_if_index, &next0,
472                                                        first_b0);
473                     }
474                 }
475
476               /* trace */
477               if (PREDICT_FALSE (n_trace > 0 &&
478                                  vlib_trace_buffer (vm, node, next0, first_b0,
479                                                     /* follow_chain */ 0)))
480                 {
481                   af_packet_input_trace_t *tr;
482                   vlib_set_trace_count (vm, node, --n_trace);
483                   tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
484                   tr->is_v3 = 1;
485                   tr->next_index = next0;
486                   tr->hw_if_index = apif->hw_if_index;
487                   tr->queue_id = queue_id;
488                   tr->block = block;
489                   tr->block_start = bd;
490                   tr->pkt_num = bd->hdr.bh1.num_pkts - num_pkts;
491                   clib_memcpy_fast (&tr->bd, bd, sizeof (block_desc_t));
492                   clib_memcpy_fast (&tr->tph3, tph, sizeof (tpacket3_hdr_t));
493                   if (is_cksum_gso_enabled)
494                     clib_memcpy_fast (&tr->vnet_hdr, vnet_hdr,
495                                       sizeof (vnet_virtio_net_hdr_t));
496                   else
497                     clib_memset_u8 (&tr->vnet_hdr, 0,
498                                     sizeof (vnet_virtio_net_hdr_t));
499                 }
500
501               /* enque and take next packet */
502               vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
503                                                n_left_to_next, first_bi0,
504                                                next0);
505
506               /* next packet */
507               num_pkts--;
508               rx_frame_offset += tph->tp_next_offset;
509             }
510
511           vlib_put_next_frame (vm, node, next_index, n_left_to_next);
512         }
513
514       if (PREDICT_TRUE (num_pkts == 0))
515         {
516           bd->hdr.bh1.block_status = TP_STATUS_KERNEL;
517           block = (block + 1) % block_nr;
518         }
519       else
520         {
521           rx_queue->rx_frame_offset = rx_frame_offset;
522           rx_queue->num_rx_pkts = num_pkts;
523           rx_queue->is_rx_pending = 1;
524         }
525     }
526
527   rx_queue->next_rx_block = block;
528
529 done:
530
531   if (apm->polling_count == 0)
532     {
533       if ((((block_desc_t *) (block_start = rx_queue->rx_ring[block]))
534              ->hdr.bh1.block_status &
535            TP_STATUS_USER) != 0)
536         vlib_node_set_state (vm, node->node_index, VLIB_NODE_STATE_POLLING);
537       else
538         vlib_node_set_state (vm, node->node_index, VLIB_NODE_STATE_INTERRUPT);
539     }
540
541   vlib_error_count (vm, node->node_index, AF_PACKET_INPUT_ERROR_TOTAL_RECV_BLK,
542                     total);
543   vlib_error_count (vm, node->node_index, AF_PACKET_INPUT_ERROR_TIMEDOUT_BLK,
544                     timedout_blk);
545
546   vlib_increment_combined_counter
547     (vnet_get_main ()->interface_main.combined_sw_if_counters
548      + VNET_INTERFACE_COUNTER_RX,
549      vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
550
551   vnet_device_increment_rx_packets (thread_index, n_rx_packets);
552   return n_rx_packets;
553 }
554
555 always_inline uword
556 af_packet_v2_device_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
557                               vlib_frame_t *frame, af_packet_if_t *apif,
558                               u16 queue_id, u8 is_cksum_gso_enabled)
559 {
560   af_packet_main_t *apm = &af_packet_main;
561   af_packet_queue_t *rx_queue = vec_elt_at_index (apif->rx_queues, queue_id);
562   tpacket2_hdr_t *tph;
563   u32 next_index;
564   u32 block = 0;
565   u32 rx_frame;
566   u32 n_free_bufs;
567   u32 n_rx_packets = 0;
568   u32 n_rx_bytes = 0;
569   u32 *to_next = 0;
570   u32 frame_size = rx_queue->rx_req->req.tp_frame_size;
571   u32 frame_num = rx_queue->rx_req->req.tp_frame_nr;
572   u8 *block_start = rx_queue->rx_ring[block];
573   uword n_trace = vlib_get_trace_count (vm, node);
574   u32 thread_index = vm->thread_index;
575   u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm);
576   u32 min_bufs = rx_queue->rx_req->req.tp_frame_size / n_buffer_bytes;
577   u32 sw_if_index = apif->sw_if_index;
578   u8 is_ip = (apif->mode == AF_PACKET_IF_MODE_IP);
579
580   if (is_ip)
581     next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
582   else
583     next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
584
585   n_free_bufs = vec_len (apm->rx_buffers[thread_index]);
586   if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE))
587     {
588       vec_validate (apm->rx_buffers[thread_index],
589                     VLIB_FRAME_SIZE + n_free_bufs - 1);
590       n_free_bufs += vlib_buffer_alloc (
591         vm, &apm->rx_buffers[thread_index][n_free_bufs], VLIB_FRAME_SIZE);
592       vec_set_len (apm->rx_buffers[thread_index], n_free_bufs);
593     }
594
595   rx_frame = rx_queue->next_rx_frame;
596   tph = (tpacket2_hdr_t *) (block_start + rx_frame * frame_size);
597   while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs))
598     {
599       vlib_buffer_t *b0 = 0, *first_b0 = 0, *prev_b0 = 0;
600       u32 next0 = next_index;
601
602       u32 n_left_to_next;
603       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
604       while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) &&
605              n_left_to_next)
606         {
607           vnet_virtio_net_hdr_t *vnet_hdr = 0;
608           u32 data_len = tph->tp_snaplen;
609           u32 offset = 0;
610           u32 bi0 = 0, first_bi0 = 0;
611           u8 l4_hdr_sz = 0;
612
613           if (is_cksum_gso_enabled)
614             vnet_hdr =
615               (vnet_virtio_net_hdr_t *) ((u8 *) tph + tph->tp_mac -
616                                          sizeof (vnet_virtio_net_hdr_t));
617           while (data_len)
618             {
619               /* grab free buffer */
620               u32 last_empty_buffer =
621                 vec_len (apm->rx_buffers[thread_index]) - 1;
622               bi0 = apm->rx_buffers[thread_index][last_empty_buffer];
623               b0 = vlib_get_buffer (vm, bi0);
624               vec_set_len (apm->rx_buffers[thread_index], last_empty_buffer);
625               n_free_bufs--;
626
627               /* copy data */
628               u32 bytes_to_copy =
629                 data_len > n_buffer_bytes ? n_buffer_bytes : data_len;
630               u32 vlan_len = 0;
631               u32 bytes_copied = 0;
632               b0->current_data = 0;
633               /* Kernel removes VLAN headers, so reconstruct VLAN */
634               if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID))
635                 {
636                   if (PREDICT_TRUE (offset == 0))
637                     {
638                       clib_memcpy_fast (vlib_buffer_get_current (b0),
639                                         (u8 *) tph + tph->tp_mac,
640                                         sizeof (ethernet_header_t));
641                       ethernet_header_t *eth = vlib_buffer_get_current (b0);
642                       ethernet_vlan_header_t *vlan =
643                         (ethernet_vlan_header_t *) (eth + 1);
644                       vlan->priority_cfi_and_id =
645                         clib_host_to_net_u16 (tph->tp_vlan_tci);
646                       vlan->type = eth->type;
647                       eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
648                       vlan_len = sizeof (ethernet_vlan_header_t);
649                       bytes_copied = sizeof (ethernet_header_t);
650                     }
651                 }
652               clib_memcpy_fast (((u8 *) vlib_buffer_get_current (b0)) +
653                                   bytes_copied + vlan_len,
654                                 (u8 *) tph + tph->tp_mac + offset +
655                                   bytes_copied,
656                                 (bytes_to_copy - bytes_copied));
657
658               /* fill buffer header */
659               b0->current_length = bytes_to_copy + vlan_len;
660
661               if (offset == 0)
662                 {
663                   b0->total_length_not_including_first_buffer = 0;
664                   b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
665                   vnet_buffer (b0)->sw_if_index[VLIB_RX] = sw_if_index;
666                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~0;
667                   first_bi0 = bi0;
668                   first_b0 = vlib_get_buffer (vm, first_bi0);
669
670                   if (is_cksum_gso_enabled)
671                     {
672                       if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
673                         fill_cksum_offload (first_b0, &l4_hdr_sz, is_ip);
674                       if (vnet_hdr->gso_type & (VIRTIO_NET_HDR_GSO_TCPV4 |
675                                                 VIRTIO_NET_HDR_GSO_TCPV6))
676                         fill_gso_offload (first_b0, vnet_hdr->gso_size,
677                                           l4_hdr_sz);
678                     }
679                 }
680               else
681                 buffer_add_to_chain (b0, first_b0, prev_b0, bi0);
682
683               prev_b0 = b0;
684               offset += bytes_to_copy;
685               data_len -= bytes_to_copy;
686             }
687           n_rx_packets++;
688           n_rx_bytes += tph->tp_snaplen;
689           to_next[0] = first_bi0;
690           to_next += 1;
691           n_left_to_next--;
692
693           /* drop partial packets */
694           if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen))
695             {
696               next0 = VNET_DEVICE_INPUT_NEXT_DROP;
697               first_b0->error =
698                 node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT];
699             }
700           else
701             {
702               if (PREDICT_FALSE (is_ip))
703                 {
704                   switch (first_b0->data[0] & 0xf0)
705                     {
706                     case 0x40:
707                       next0 = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
708                       break;
709                     case 0x60:
710                       next0 = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
711                       break;
712                     default:
713                       next0 = VNET_DEVICE_INPUT_NEXT_DROP;
714                       break;
715                     }
716                   if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
717                     next0 = apif->per_interface_next_index;
718                 }
719               else
720                 {
721                   next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
722                   if (PREDICT_FALSE (apif->per_interface_next_index != ~0))
723                     next0 = apif->per_interface_next_index;
724                   /* redirect if feature path enabled */
725                   vnet_feature_start_device_input (sw_if_index, &next0,
726                                                    first_b0);
727                 }
728             }
729
730           /* trace */
731           if (PREDICT_FALSE (n_trace > 0 &&
732                              vlib_trace_buffer (vm, node, next0, first_b0,
733                                                 /* follow_chain */ 0)))
734             {
735               af_packet_input_trace_t *tr;
736               vlib_set_trace_count (vm, node, --n_trace);
737               tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr));
738               tr->is_v3 = 0;
739               tr->next_index = next0;
740               tr->hw_if_index = apif->hw_if_index;
741               tr->queue_id = queue_id;
742               clib_memcpy_fast (&tr->tph2, tph, sizeof (struct tpacket2_hdr));
743               if (is_cksum_gso_enabled)
744                 clib_memcpy_fast (&tr->vnet_hdr, vnet_hdr,
745                                   sizeof (vnet_virtio_net_hdr_t));
746               else
747                 clib_memset_u8 (&tr->vnet_hdr, 0,
748                                 sizeof (vnet_virtio_net_hdr_t));
749             }
750
751           /* enque and take next packet */
752           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
753                                            n_left_to_next, first_bi0, next0);
754
755           /* next packet */
756           tph->tp_status = TP_STATUS_KERNEL;
757           rx_frame = (rx_frame + 1) % frame_num;
758           tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size);
759         }
760
761       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
762     }
763
764   rx_queue->next_rx_frame = rx_frame;
765
766   vlib_increment_combined_counter (
767     vnet_get_main ()->interface_main.combined_sw_if_counters +
768       VNET_INTERFACE_COUNTER_RX,
769     vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes);
770
771   vnet_device_increment_rx_packets (thread_index, n_rx_packets);
772   return n_rx_packets;
773 }
774
775 always_inline uword
776 af_packet_device_input_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
777                            vlib_frame_t *frame, af_packet_if_t *apif,
778                            u16 queue_id, u8 is_cksum_gso_enabled)
779
780 {
781   if (apif->version == TPACKET_V3)
782     return af_packet_v3_device_input_fn (vm, node, frame, apif, queue_id,
783                                          is_cksum_gso_enabled);
784   else
785     return af_packet_v2_device_input_fn (vm, node, frame, apif, queue_id,
786                                          is_cksum_gso_enabled);
787 }
788
789 VLIB_NODE_FN (af_packet_input_node) (vlib_main_t * vm,
790                                      vlib_node_runtime_t * node,
791                                      vlib_frame_t * frame)
792 {
793   u32 n_rx_packets = 0;
794   af_packet_main_t *apm = &af_packet_main;
795   vnet_hw_if_rxq_poll_vector_t *pv;
796   pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
797   for (int i = 0; i < vec_len (pv); i++)
798     {
799       af_packet_if_t *apif;
800       apif = vec_elt_at_index (apm->interfaces, pv[i].dev_instance);
801       if (apif->is_admin_up)
802         {
803           if (apif->is_cksum_gso_enabled)
804             n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif,
805                                                        pv[i].queue_id, 1);
806           else
807             n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif,
808                                                        pv[i].queue_id, 0);
809         }
810     }
811   return n_rx_packets;
812 }
813
814 VLIB_REGISTER_NODE (af_packet_input_node) = {
815   .name = "af-packet-input",
816   .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
817   .sibling_of = "device-input",
818   .format_trace = format_af_packet_input_trace,
819   .type = VLIB_NODE_TYPE_INPUT,
820   .state = VLIB_NODE_STATE_INTERRUPT,
821   .n_errors = AF_PACKET_INPUT_N_ERROR,
822   .error_strings = af_packet_input_error_strings,
823 };
824
825
826 /*
827  * fd.io coding-style-patch-verification: ON
828  *
829  * Local Variables:
830  * eval: (c-set-style "gnu")
831  * End:
832  */