1df7f8e8cSSteven Luong/*
2df7f8e8cSSteven Luong *------------------------------------------------------------------
3df7f8e8cSSteven Luong * Copyright (c) 2018 Cisco and/or its affiliates.
4df7f8e8cSSteven Luong * Licensed under the Apache License, Version 2.0 (the "License");
5df7f8e8cSSteven Luong * you may not use this file except in compliance with the License.
6df7f8e8cSSteven Luong * You may obtain a copy of the License at:
7df7f8e8cSSteven Luong *
8df7f8e8cSSteven Luong *     http://www.apache.org/licenses/LICENSE-2.0
9df7f8e8cSSteven Luong *
10df7f8e8cSSteven Luong * Unless required by applicable law or agreed to in writing, software
11df7f8e8cSSteven Luong * distributed under the License is distributed on an "AS IS" BASIS,
12df7f8e8cSSteven Luong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13df7f8e8cSSteven Luong * See the License for the specific language governing permissions and
14df7f8e8cSSteven Luong * limitations under the License.
15df7f8e8cSSteven Luong *------------------------------------------------------------------
16df7f8e8cSSteven Luong */
17df7f8e8cSSteven Luong
18df7f8e8cSSteven Luong#include <vlib/vlib.h>
19df7f8e8cSSteven Luong#include <vlib/unix/unix.h>
20df7f8e8cSSteven Luong#include <vlib/pci/pci.h>
21df7f8e8cSSteven Luong#include <vnet/ethernet/ethernet.h>
22df7f8e8cSSteven Luong#include <vnet/devices/devices.h>
23dd373728SSteven Luong#include <vnet/ip/ip6_packet.h>
24dd373728SSteven Luong#include <vnet/ip/ip4_packet.h>
25dd373728SSteven Luong#include <vnet/udp/udp_packet.h>
26df7f8e8cSSteven Luong
27df7f8e8cSSteven Luong#include <vmxnet3/vmxnet3.h>
28df7f8e8cSSteven Luong
29df7f8e8cSSteven Luong#define foreach_vmxnet3_input_error \
30df7f8e8cSSteven Luong  _(BUFFER_ALLOC, "buffer alloc error") \
313c6015b0SSteven  _(RX_PACKET_NO_SOP, "Rx packet error - no SOP") \
32df7f8e8cSSteven Luong  _(RX_PACKET, "Rx packet error") \
338b099536SSteven  _(RX_PACKET_EOP, "Rx packet error found on EOP") \
34df7f8e8cSSteven Luong  _(NO_BUFFER, "Rx no buffer error")
35df7f8e8cSSteven Luong
36df7f8e8cSSteven Luongtypedef enum
37df7f8e8cSSteven Luong{
38df7f8e8cSSteven Luong#define _(f,s) VMXNET3_INPUT_ERROR_##f,
39df7f8e8cSSteven Luong  foreach_vmxnet3_input_error
40df7f8e8cSSteven Luong#undef _
41df7f8e8cSSteven Luong    VMXNET3_INPUT_N_ERROR,
42df7f8e8cSSteven Luong} vmxnet3_input_error_t;
43df7f8e8cSSteven Luong
44df7f8e8cSSteven Luongstatic __clib_unused char *vmxnet3_input_error_strings[] = {
45df7f8e8cSSteven Luong#define _(n,s) s,
46df7f8e8cSSteven Luong  foreach_vmxnet3_input_error
47df7f8e8cSSteven Luong#undef _
48df7f8e8cSSteven Luong};
49df7f8e8cSSteven Luong
50df7f8e8cSSteven Luongstatic_always_inline u16
51df7f8e8cSSteven Luongvmxnet3_find_rid (vmxnet3_device_t * vd, vmxnet3_rx_comp * rx_comp)
52df7f8e8cSSteven Luong{
53df7f8e8cSSteven Luong  u32 rid;
54df7f8e8cSSteven Luong
55df7f8e8cSSteven Luong  // rid is bits 16-25 (10 bits number)
56df7f8e8cSSteven Luong  rid = rx_comp->index & (0xffffffff >> 6);
57df7f8e8cSSteven Luong  rid >>= 16;
58df7f8e8cSSteven Luong  if ((rid >= vd->num_rx_queues) && (rid < (vd->num_rx_queues << 1)))
59df7f8e8cSSteven Luong    return 1;
60df7f8e8cSSteven Luong  else
61df7f8e8cSSteven Luong    return 0;
62df7f8e8cSSteven Luong}
63df7f8e8cSSteven Luong
64df7f8e8cSSteven Luongstatic_always_inline void
65df7f8e8cSSteven Luongvmxnet3_rx_comp_ring_advance_next (vmxnet3_rxq_t * rxq)
66df7f8e8cSSteven Luong{
67df7f8e8cSSteven Luong  vmxnet3_rx_comp_ring *comp_ring = &rxq->rx_comp_ring;
68df7f8e8cSSteven Luong
69df7f8e8cSSteven Luong  comp_ring->next++;
70df7f8e8cSSteven Luong  if (PREDICT_FALSE (comp_ring->next == rxq->size))
71df7f8e8cSSteven Luong    {
72df7f8e8cSSteven Luong      comp_ring->next = 0;
73df7f8e8cSSteven Luong      comp_ring->gen ^= VMXNET3_RXCF_GEN;
74df7f8e8cSSteven Luong    }
75df7f8e8cSSteven Luong}
76df7f8e8cSSteven Luong
77dd373728SSteven Luongstatic_always_inline void
78dd373728SSteven Luongvmxnet3_handle_offload (vmxnet3_rx_comp * rx_comp, vlib_buffer_t * hb,
79678d64d0SSteven Luong			u16 gso_size)
80dd373728SSteven Luong{
81dd373728SSteven Luong  u8 l4_hdr_sz = 0;
82dd373728SSteven Luong
83dd373728SSteven Luong  if (rx_comp->flags & VMXNET3_RXCF_IP4)
84dd373728SSteven Luong    {
85dd373728SSteven Luong      ip4_header_t *ip4 = (ip4_header_t *) (hb->data +
86dd373728SSteven Luong					    sizeof (ethernet_header_t));
87dd373728SSteven Luong
88dd373728SSteven Luong      vnet_buffer (hb)->l2_hdr_offset = 0;
89dd373728SSteven Luong      vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
90dd373728SSteven Luong      vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
91dd373728SSteven Luong	ip4_header_bytes (ip4);
92dd373728SSteven Luong      hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
93dd373728SSteven Luong	VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
94dd373728SSteven Luong	VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP4;
95dd373728SSteven Luong
96dd373728SSteven Luong      /* checksum offload */
97dd373728SSteven Luong      if (!(rx_comp->index & VMXNET3_RXCI_CNC))
98dd373728SSteven Luong	{
99dd373728SSteven Luong	  if (!(rx_comp->flags & VMXNET3_RXCF_IPC))
100dd373728SSteven Luong	    {
101dd373728SSteven Luong	      hb->flags |= VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
102dd373728SSteven Luong	      ip4->checksum = 0;
103dd373728SSteven Luong	    }
104dd373728SSteven Luong	  if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
105dd373728SSteven Luong	    {
106dd373728SSteven Luong	      if (rx_comp->flags & VMXNET3_RXCF_TCP)
107dd373728SSteven Luong		{
108dd373728SSteven Luong		  tcp_header_t *tcp =
109dd373728SSteven Luong		    (tcp_header_t *) (hb->data +
110dd373728SSteven Luong				      vnet_buffer (hb)->l4_hdr_offset);
111dd373728SSteven Luong		  hb->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
112dd373728SSteven Luong		  tcp->checksum = 0;
113dd373728SSteven Luong		}
114dd373728SSteven Luong	      else if (rx_comp->flags & VMXNET3_RXCF_UDP)
115dd373728SSteven Luong		{
116dd373728SSteven Luong		  udp_header_t *udp =
117dd373728SSteven Luong		    (udp_header_t *) (hb->data +
118dd373728SSteven Luong				      vnet_buffer (hb)->l4_hdr_offset);
119dd373728SSteven Luong		  hb->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
120dd373728SSteven Luong		  udp->checksum = 0;
121dd373728SSteven Luong		}
122dd373728SSteven Luong	    }
123dd373728SSteven Luong	}
124dd373728SSteven Luong
125dd373728SSteven Luong      if (gso_size)
126dd373728SSteven Luong	{
1275a5c036cSSteven Luong	  if (rx_comp->flags & VMXNET3_RXCF_TCP)
1285a5c036cSSteven Luong	    {
1295a5c036cSSteven Luong	      tcp_header_t *tcp =
1305a5c036cSSteven Luong		(tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
1315a5c036cSSteven Luong	      l4_hdr_sz = tcp_header_bytes (tcp);
1325a5c036cSSteven Luong	    }
1335a5c036cSSteven Luong	  else if (rx_comp->flags & VMXNET3_RXCF_UDP)
1345a5c036cSSteven Luong	    {
1355a5c036cSSteven Luong	      udp_header_t *udp =
1365a5c036cSSteven Luong		(udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
1375a5c036cSSteven Luong	      l4_hdr_sz = sizeof (*udp);
1385a5c036cSSteven Luong	    }
139dd373728SSteven Luong	  vnet_buffer2 (hb)->gso_size = gso_size;
140dd373728SSteven Luong	  vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
141dd373728SSteven Luong	  hb->flags |= VNET_BUFFER_F_GSO;
142dd373728SSteven Luong	}
143dd373728SSteven Luong    }
144dd373728SSteven Luong  else if (rx_comp->flags & VMXNET3_RXCF_IP6)
145dd373728SSteven Luong    {
146dd373728SSteven Luong      vnet_buffer (hb)->l2_hdr_offset = 0;
147dd373728SSteven Luong      vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
148dd373728SSteven Luong      vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
149dd373728SSteven Luong	sizeof (ip6_header_t);
150dd373728SSteven Luong      hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
151dd373728SSteven Luong	VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
152dd373728SSteven Luong	VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP6;
153dd373728SSteven Luong
154dd373728SSteven Luong      /* checksum offload */
155dd373728SSteven Luong      if (!(rx_comp->index & VMXNET3_RXCI_CNC))
156dd373728SSteven Luong	{
157dd373728SSteven Luong	  if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
158dd373728SSteven Luong	    {
159dd373728SSteven Luong	      if (rx_comp->flags & VMXNET3_RXCF_TCP)
160dd373728SSteven Luong		{
161dd373728SSteven Luong		  tcp_header_t *tcp =
162dd373728SSteven Luong		    (tcp_header_t *) (hb->data +
163dd373728SSteven Luong				      vnet_buffer (hb)->l4_hdr_offset);
164dd373728SSteven Luong		  hb->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
165dd373728SSteven Luong		  tcp->checksum = 0;
166dd373728SSteven Luong		}
167dd373728SSteven Luong	      else if (rx_comp->flags & VMXNET3_RXCF_UDP)
168dd373728SSteven Luong		{
169dd373728SSteven Luong		  udp_header_t *udp =
170dd373728SSteven Luong		    (udp_header_t *) (hb->data +
171dd373728SSteven Luong				      vnet_buffer (hb)->l4_hdr_offset);
172dd373728SSteven Luong		  hb->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
173dd373728SSteven Luong		  udp->checksum = 0;
174dd373728SSteven Luong		}
175dd373728SSteven Luong	    }
176dd373728SSteven Luong	}
177dd373728SSteven Luong
178dd373728SSteven Luong      if (gso_size)
179dd373728SSteven Luong	{
1805a5c036cSSteven Luong	  if (rx_comp->flags & VMXNET3_RXCF_TCP)
1815a5c036cSSteven Luong	    {
1825a5c036cSSteven Luong	      tcp_header_t *tcp =
1835a5c036cSSteven Luong		(tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
1845a5c036cSSteven Luong	      l4_hdr_sz = tcp_header_bytes (tcp);
1855a5c036cSSteven Luong	    }
1865a5c036cSSteven Luong	  else if (rx_comp->flags & VMXNET3_RXCF_UDP)
1875a5c036cSSteven Luong	    {
1885a5c036cSSteven Luong	      udp_header_t *udp =
1895a5c036cSSteven Luong		(udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
1905a5c036cSSteven Luong	      l4_hdr_sz = sizeof (*udp);
1915a5c036cSSteven Luong	    }
192dd373728SSteven Luong	  vnet_buffer2 (hb)->gso_size = gso_size;
193dd373728SSteven Luong	  vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
194dd373728SSteven Luong	  hb->flags |= VNET_BUFFER_F_GSO;
195dd373728SSteven Luong	}
196dd373728SSteven Luong    }
197dd373728SSteven Luong}
198dd373728SSteven Luong
199df7f8e8cSSteven Luongstatic_always_inline uword
200df7f8e8cSSteven Luongvmxnet3_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
201df7f8e8cSSteven Luong			     vlib_frame_t * frame, vmxnet3_device_t * vd,
202df7f8e8cSSteven Luong			     u16 qid)
203df7f8e8cSSteven Luong{
204df7f8e8cSSteven Luong  vnet_main_t *vnm = vnet_get_main ();
205df7f8e8cSSteven Luong  uword n_trace = vlib_get_trace_count (vm, node);
206df7f8e8cSSteven Luong  u32 n_rx_packets = 0, n_rx_bytes = 0;
207df7f8e8cSSteven Luong  vmxnet3_rx_comp *rx_comp;
208df7f8e8cSSteven Luong  u32 desc_idx;
209df7f8e8cSSteven Luong  vmxnet3_rxq_t *rxq;
210df7f8e8cSSteven Luong  u32 thread_index = vm->thread_index;
211df7f8e8cSSteven Luong  u32 buffer_indices[VLIB_FRAME_SIZE], *bi;
212df7f8e8cSSteven Luong  u16 nexts[VLIB_FRAME_SIZE], *next;
213df7f8e8cSSteven Luong  vmxnet3_rx_ring *ring;
214df7f8e8cSSteven Luong  vmxnet3_rx_comp_ring *comp_ring;
215df7f8e8cSSteven Luong  u16 rid;
216df7f8e8cSSteven Luong  vlib_buffer_t *prev_b0 = 0, *hb = 0;
21733e6583eSSteven  u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
218744da656SSteven  u8 known_next = 0, got_packet = 0;
2193c6015b0SSteven  vmxnet3_rx_desc *rxd;
2203c6015b0SSteven  clib_error_t *error;
221dd373728SSteven Luong  u16 gso_size = 0;
222df7f8e8cSSteven Luong
223df7f8e8cSSteven Luong  rxq = vec_elt_at_index (vd->rxqs, qid);
224df7f8e8cSSteven Luong  comp_ring = &rxq->rx_comp_ring;
225df7f8e8cSSteven Luong  bi = buffer_indices;
226df7f8e8cSSteven Luong  next = nexts;
2278b099536SSteven  rx_comp = &rxq->rx_comp[comp_ring->next];
2288b099536SSteven
229005e4d50SSteven  while (PREDICT_TRUE ((n_rx_packets < VLIB_FRAME_SIZE) &&
230005e4d50SSteven		       (comp_ring->gen ==
231005e4d50SSteven			(rx_comp->flags & VMXNET3_RXCF_GEN))))
232df7f8e8cSSteven Luong    {
233df7f8e8cSSteven Luong      vlib_buffer_t *b0;
2346e06d667SSteven      u32 bi0;
235df7f8e8cSSteven Luong
236df7f8e8cSSteven Luong      rid = vmxnet3_find_rid (vd, rx_comp);
237df7f8e8cSSteven Luong      ring = &rxq->rx_ring[rid];
238df7f8e8cSSteven Luong
239df7f8e8cSSteven Luong      if (PREDICT_TRUE (ring->fill >= 1))
240df7f8e8cSSteven Luong	ring->fill--;
241df7f8e8cSSteven Luong      else
242df7f8e8cSSteven Luong	{
243df7f8e8cSSteven Luong	  vlib_error_count (vm, node->node_index,
244df7f8e8cSSteven Luong			    VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
2458b099536SSteven	  if (hb)
2468b099536SSteven	    {
2478b099536SSteven	      vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
2488b099536SSteven	      hb = 0;
2498b099536SSteven	    }
2508b099536SSteven	  prev_b0 = 0;
251df7f8e8cSSteven Luong	  break;
252df7f8e8cSSteven Luong	}
253df7f8e8cSSteven Luong
254df7f8e8cSSteven Luong      desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
255df7f8e8cSSteven Luong      ring->consume = desc_idx;
2563c6015b0SSteven      rxd = &rxq->rx_desc[rid][desc_idx];
257df7f8e8cSSteven Luong
2586e06d667SSteven      bi0 = ring->bufs[desc_idx];
259df7f8e8cSSteven Luong      ring->bufs[desc_idx] = ~0;
260df7f8e8cSSteven Luong
2616e06d667SSteven      b0 = vlib_get_buffer (vm, bi0);
262df7f8e8cSSteven Luong      vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
263df7f8e8cSSteven Luong      vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
26433e6583eSSteven      vnet_buffer (b0)->feature_arc_index = 0;
265df7f8e8cSSteven Luong      b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
266df7f8e8cSSteven Luong      b0->current_data = 0;
267df7f8e8cSSteven Luong      b0->total_length_not_including_first_buffer = 0;
268df7f8e8cSSteven Luong      b0->next_buffer = 0;
269df7f8e8cSSteven Luong      b0->flags = 0;
270df7f8e8cSSteven Luong      b0->error = 0;
27133e6583eSSteven      b0->current_config_index = 0;
272df7f8e8cSSteven Luong      ASSERT (b0->current_length != 0);
273df7f8e8cSSteven Luong
2743c6015b0SSteven      if (PREDICT_FALSE ((rx_comp->index & VMXNET3_RXCI_EOP) &&
2753c6015b0SSteven			 (rx_comp->len & VMXNET3_RXCL_ERROR)))
2763c6015b0SSteven	{
2773c6015b0SSteven	  vlib_buffer_free_one (vm, bi0);
2783c6015b0SSteven	  vlib_error_count (vm, node->node_index,
2798b099536SSteven			    VMXNET3_INPUT_ERROR_RX_PACKET_EOP, 1);
2803c6015b0SSteven	  if (hb && vlib_get_buffer_index (vm, hb) != bi0)
2813c6015b0SSteven	    {
2823c6015b0SSteven	      vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
2833c6015b0SSteven	      hb = 0;
2843c6015b0SSteven	    }
2853c6015b0SSteven	  prev_b0 = 0;
2868b099536SSteven	  goto next;
2873c6015b0SSteven	}
2883c6015b0SSteven
289df7f8e8cSSteven Luong      if (rx_comp->index & VMXNET3_RXCI_SOP)
290df7f8e8cSSteven Luong	{
2913c6015b0SSteven	  ASSERT (!(rxd->flags & VMXNET3_RXF_BTYPE));
292df7f8e8cSSteven Luong	  /* start segment */
2932985e0afSSteven Luong	  if (vd->gso_enable &&
294dd373728SSteven Luong	      (rx_comp->flags & VMXNET3_RXCF_CT) == VMXNET3_RXCOMP_TYPE_LRO)
295dd373728SSteven Luong	    {
296dd373728SSteven Luong	      vmxnet3_rx_comp_ext *lro = (vmxnet3_rx_comp_ext *) rx_comp;
297dd373728SSteven Luong
298dd373728SSteven Luong	      gso_size = lro->flags & VMXNET3_RXECF_MSS_MASK;
299dd373728SSteven Luong	    }
300dd373728SSteven Luong
301df7f8e8cSSteven Luong	  hb = b0;
3026e06d667SSteven	  bi[0] = bi0;
303df7f8e8cSSteven Luong	  if (!(rx_comp->index & VMXNET3_RXCI_EOP))
304df7f8e8cSSteven Luong	    {
305df7f8e8cSSteven Luong	      hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
306df7f8e8cSSteven Luong	      prev_b0 = b0;
307df7f8e8cSSteven Luong	    }
308df7f8e8cSSteven Luong	  else
309df7f8e8cSSteven Luong	    {
310df7f8e8cSSteven Luong	      /*
311df7f8e8cSSteven Luong	       * Both start and end of packet is set. It is a complete packet
312df7f8e8cSSteven Luong	       */
313df7f8e8cSSteven Luong	      prev_b0 = 0;
314744da656SSteven	      got_packet = 1;
315df7f8e8cSSteven Luong	    }
316df7f8e8cSSteven Luong	}
317df7f8e8cSSteven Luong      else if (rx_comp->index & VMXNET3_RXCI_EOP)
318df7f8e8cSSteven Luong	{
319df7f8e8cSSteven Luong	  /* end of segment */
320df7f8e8cSSteven Luong	  if (prev_b0)
321df7f8e8cSSteven Luong	    {
322df7f8e8cSSteven Luong	      prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
3233c6015b0SSteven	      prev_b0->next_buffer = bi0;
324df7f8e8cSSteven Luong	      hb->total_length_not_including_first_buffer +=
325df7f8e8cSSteven Luong		b0->current_length;
326744da656SSteven	      prev_b0 = 0;
327744da656SSteven	      got_packet = 1;
328df7f8e8cSSteven Luong	    }
329df7f8e8cSSteven Luong	  else
330df7f8e8cSSteven Luong	    {
331df7f8e8cSSteven Luong	      /* EOP without SOP, error */
332df7f8e8cSSteven Luong	      vlib_error_count (vm, node->node_index,
3333c6015b0SSteven				VMXNET3_INPUT_ERROR_RX_PACKET_NO_SOP, 1);
3346e06d667SSteven	      vlib_buffer_free_one (vm, bi0);
3353c6015b0SSteven	      if (hb && vlib_get_buffer_index (vm, hb) != bi0)
3363c6015b0SSteven		{
3373c6015b0SSteven		  vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
3383c6015b0SSteven		  hb = 0;
3393c6015b0SSteven		}
3408b099536SSteven	      goto next;
341df7f8e8cSSteven Luong	    }
342df7f8e8cSSteven Luong	}
343df7f8e8cSSteven Luong      else if (prev_b0)		// !sop && !eop
344df7f8e8cSSteven Luong	{
345df7f8e8cSSteven Luong	  /* mid chain */
3463c6015b0SSteven	  ASSERT (rxd->flags & VMXNET3_RXF_BTYPE);
3473c6015b0SSteven	  prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
3486e06d667SSteven	  prev_b0->next_buffer = bi0;
349df7f8e8cSSteven Luong	  prev_b0 = b0;
350df7f8e8cSSteven Luong	  hb->total_length_not_including_first_buffer += b0->current_length;
351df7f8e8cSSteven Luong	}
352df7f8e8cSSteven Luong      else
353df7f8e8cSSteven Luong	{
3548b099536SSteven	  vlib_error_count (vm, node->node_index,
3558b099536SSteven			    VMXNET3_INPUT_ERROR_RX_PACKET, 1);
3568b099536SSteven	  vlib_buffer_free_one (vm, bi0);
3578b099536SSteven	  if (hb && vlib_get_buffer_index (vm, hb) != bi0)
3588b099536SSteven	    {
3598b099536SSteven	      vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
3608b099536SSteven	      hb = 0;
3618b099536SSteven	    }
3628b099536SSteven	  goto next;
363df7f8e8cSSteven Luong	}
364df7f8e8cSSteven Luong
365df7f8e8cSSteven Luong      n_rx_bytes += b0->current_length;
366df7f8e8cSSteven Luong
367744da656SSteven      if (got_packet)
368df7f8e8cSSteven Luong	{
36933e6583eSSteven	  if (PREDICT_FALSE (vd->per_interface_next_index != ~0))
37033e6583eSSteven	    {
37133e6583eSSteven	      next_index = vd->per_interface_next_index;
37233e6583eSSteven	      known_next = 1;
37333e6583eSSteven	    }
37433e6583eSSteven
37533e6583eSSteven	  if (PREDICT_FALSE
37633e6583eSSteven	      (vnet_device_input_have_features (vd->sw_if_index)))
37733e6583eSSteven	    {
37833e6583eSSteven	      vnet_feature_start_device_input_x1 (vd->sw_if_index,
37933e6583eSSteven						  &next_index, hb);
38033e6583eSSteven	      known_next = 1;
38133e6583eSSteven	    }
38233e6583eSSteven
38333e6583eSSteven	  if (PREDICT_FALSE (known_next))
384678d64d0SSteven Luong	    next[0] = next_index;
38533e6583eSSteven	  else
38633e6583eSSteven	    {
387dd373728SSteven Luong	      ethernet_header_t *e = (ethernet_header_t *) hb->data;
388dd373728SSteven Luong
389678d64d0SSteven Luong	      next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
390678d64d0SSteven Luong	      if (!ethernet_frame_is_tagged (e->type))
391678d64d0SSteven Luong		vmxnet3_handle_offload (rx_comp, hb, gso_size);
39233e6583eSSteven	    }
39333e6583eSSteven
394df7f8e8cSSteven Luong	  n_rx_packets++;
395df7f8e8cSSteven Luong	  next++;
3966e06d667SSteven	  bi++;
397df7f8e8cSSteven Luong	  hb = 0;
398744da656SSteven	  got_packet = 0;
399dd373728SSteven Luong	  gso_size = 0;
400df7f8e8cSSteven Luong	}
4018b099536SSteven
4028b099536SSteven    next:
4038b099536SSteven      vmxnet3_rx_comp_ring_advance_next (rxq);
4048b099536SSteven      rx_comp = &rxq->rx_comp[comp_ring->next];
405df7f8e8cSSteven Luong    }
406df7f8e8cSSteven Luong
407df7f8e8cSSteven Luong  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
408df7f8e8cSSteven Luong    {
409df7f8e8cSSteven Luong      u32 n_left = n_rx_packets;
410df7f8e8cSSteven Luong
411df7f8e8cSSteven Luong      bi = buffer_indices;
412df7f8e8cSSteven Luong      next = nexts;
413df7f8e8cSSteven Luong      while (n_trace && n_left)
414df7f8e8cSSteven Luong	{
415df7f8e8cSSteven Luong	  vlib_buffer_t *b;
416df7f8e8cSSteven Luong	  vmxnet3_input_trace_t *tr;
417df7f8e8cSSteven Luong
418df7f8e8cSSteven Luong	  b = vlib_get_buffer (vm, bi[0]);
419df7f8e8cSSteven Luong	  vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
420df7f8e8cSSteven Luong	  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
421df7f8e8cSSteven Luong	  tr->next_index = next[0];
422df7f8e8cSSteven Luong	  tr->hw_if_index = vd->hw_if_index;
423df7f8e8cSSteven Luong	  tr->buffer = *b;
424df7f8e8cSSteven Luong
425df7f8e8cSSteven Luong	  n_trace--;
426df7f8e8cSSteven Luong	  n_left--;
427df7f8e8cSSteven Luong	  bi++;
428df7f8e8cSSteven Luong	  next++;
429df7f8e8cSSteven Luong	}
430df7f8e8cSSteven Luong      vlib_set_trace_count (vm, node, n_trace);
431df7f8e8cSSteven Luong    }
432df7f8e8cSSteven Luong
433df7f8e8cSSteven Luong  if (PREDICT_TRUE (n_rx_packets))
434df7f8e8cSSteven Luong    {
435df7f8e8cSSteven Luong      vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
436df7f8e8cSSteven Luong				   n_rx_packets);
437df7f8e8cSSteven Luong      vlib_increment_combined_counter
438df7f8e8cSSteven Luong	(vnm->interface_main.combined_sw_if_counters +
439df7f8e8cSSteven Luong	 VNET_INTERFACE_COUNTER_RX, thread_index,
440ddf625d6SSteven Luong	 vd->sw_if_index, n_rx_packets, n_rx_bytes);
4413c6015b0SSteven    }
442df7f8e8cSSteven Luong
4433c6015b0SSteven  error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
4443c6015b0SSteven  if (PREDICT_FALSE (error != 0))
4453c6015b0SSteven    {
4463c6015b0SSteven      vlib_error_count (vm, node->node_index,
4473c6015b0SSteven			VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
4483c6015b0SSteven    }
4493c6015b0SSteven  error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
4503c6015b0SSteven  if (PREDICT_FALSE (error != 0))
4513c6015b0SSteven    {
4523c6015b0SSteven      vlib_error_count (vm, node->node_index,
4533c6015b0SSteven			VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
454df7f8e8cSSteven Luong    }
455df7f8e8cSSteven Luong
456df7f8e8cSSteven Luong  return n_rx_packets;
457df7f8e8cSSteven Luong}
458df7f8e8cSSteven Luong
459df7f8e8cSSteven LuongVLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
460df7f8e8cSSteven Luong				   vlib_node_runtime_t * node,
461df7f8e8cSSteven Luong				   vlib_frame_t * frame)
462df7f8e8cSSteven Luong{
463df7f8e8cSSteven Luong  u32 n_rx = 0;
464df7f8e8cSSteven Luong  vmxnet3_main_t *vmxm = &vmxnet3_main;
465df7f8e8cSSteven Luong  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
466df7f8e8cSSteven Luong  vnet_device_and_queue_t *dq;
467df7f8e8cSSteven Luong
468df7f8e8cSSteven Luong  foreach_device_and_queue (dq, rt->devices_and_queues)
469df7f8e8cSSteven Luong  {
470df7f8e8cSSteven Luong    vmxnet3_device_t *vd;
471df7f8e8cSSteven Luong    vd = vec_elt_at_index (vmxm->devices, dq->dev_instance);
472df7f8e8cSSteven Luong    if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
473df7f8e8cSSteven Luong      continue;
474df7f8e8cSSteven Luong    n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, dq->queue_id);
475df7f8e8cSSteven Luong  }
476df7f8e8cSSteven Luong  return n_rx;
477df7f8e8cSSteven Luong}
478df7f8e8cSSteven Luong
479df7f8e8cSSteven Luong#ifndef CLIB_MARCH_VARIANT
480df7f8e8cSSteven Luong/* *INDENT-OFF* */
481df7f8e8cSSteven LuongVLIB_REGISTER_NODE (vmxnet3_input_node) = {
482df7f8e8cSSteven Luong  .name = "vmxnet3-input",
483df7f8e8cSSteven Luong  .sibling_of = "device-input",
4847ca5aaacSDamjan Marion  .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
485df7f8e8cSSteven Luong  .format_trace = format_vmxnet3_input_trace,
486df7f8e8cSSteven Luong  .type = VLIB_NODE_TYPE_INPUT,
487df7f8e8cSSteven Luong  .state = VLIB_NODE_STATE_DISABLED,
488df7f8e8cSSteven Luong  .n_errors = VMXNET3_INPUT_N_ERROR,
489df7f8e8cSSteven Luong  .error_strings = vmxnet3_input_error_strings,
490df7f8e8cSSteven Luong};
491df7f8e8cSSteven Luong#endif
492df7f8e8cSSteven Luong
493df7f8e8cSSteven Luong/* *INDENT-ON* */
494df7f8e8cSSteven Luong
495df7f8e8cSSteven Luong/*
496df7f8e8cSSteven Luong * fd.io coding-style-patch-verification: ON
497df7f8e8cSSteven Luong *
498df7f8e8cSSteven Luong * Local Variables:
499df7f8e8cSSteven Luong * eval: (c-set-style "gnu")
500df7f8e8cSSteven Luong * End:
501df7f8e8cSSteven Luong */
502