1/*
2 *------------------------------------------------------------------
3 * Copyright (c) 2018 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
18#include <vlib/vlib.h>
19#include <vlib/unix/unix.h>
20#include <vlib/pci/pci.h>
21#include <vnet/ethernet/ethernet.h>
22#include <vnet/devices/devices.h>
23#include <vnet/ip/ip6_packet.h>
24#include <vnet/ip/ip4_packet.h>
25#include <vnet/udp/udp_packet.h>
26
27#include <vmxnet3/vmxnet3.h>
28
29#define foreach_vmxnet3_input_error \
30  _(BUFFER_ALLOC, "buffer alloc error") \
31  _(RX_PACKET_NO_SOP, "Rx packet error - no SOP") \
32  _(RX_PACKET, "Rx packet error") \
33  _(RX_PACKET_EOP, "Rx packet error found on EOP") \
34  _(NO_BUFFER, "Rx no buffer error")
35
36typedef enum
37{
38#define _(f,s) VMXNET3_INPUT_ERROR_##f,
39  foreach_vmxnet3_input_error
40#undef _
41    VMXNET3_INPUT_N_ERROR,
42} vmxnet3_input_error_t;
43
44static __clib_unused char *vmxnet3_input_error_strings[] = {
45#define _(n,s) s,
46  foreach_vmxnet3_input_error
47#undef _
48};
49
50static_always_inline u16
51vmxnet3_find_rid (vmxnet3_device_t * vd, vmxnet3_rx_comp * rx_comp)
52{
53  u32 rid;
54
55  // rid is bits 16-25 (10 bits number)
56  rid = rx_comp->index & (0xffffffff >> 6);
57  rid >>= 16;
58  if ((rid >= vd->num_rx_queues) && (rid < (vd->num_rx_queues << 1)))
59    return 1;
60  else
61    return 0;
62}
63
64static_always_inline void
65vmxnet3_rx_comp_ring_advance_next (vmxnet3_rxq_t * rxq)
66{
67  vmxnet3_rx_comp_ring *comp_ring = &rxq->rx_comp_ring;
68
69  comp_ring->next++;
70  if (PREDICT_FALSE (comp_ring->next == rxq->size))
71    {
72      comp_ring->next = 0;
73      comp_ring->gen ^= VMXNET3_RXCF_GEN;
74    }
75}
76
77static_always_inline void
78vmxnet3_handle_offload (vmxnet3_rx_comp * rx_comp, vlib_buffer_t * hb,
79			u16 gso_size)
80{
81  u8 l4_hdr_sz = 0;
82
83  if (rx_comp->flags & VMXNET3_RXCF_IP4)
84    {
85      ip4_header_t *ip4 = (ip4_header_t *) (hb->data +
86					    sizeof (ethernet_header_t));
87
88      vnet_buffer (hb)->l2_hdr_offset = 0;
89      vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
90      vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
91	ip4_header_bytes (ip4);
92      hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
93	VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
94	VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP4;
95
96      /* checksum offload */
97      if (!(rx_comp->index & VMXNET3_RXCI_CNC))
98	{
99	  if (!(rx_comp->flags & VMXNET3_RXCF_IPC))
100	    {
101	      hb->flags |= VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
102	      ip4->checksum = 0;
103	    }
104	  if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
105	    {
106	      if (rx_comp->flags & VMXNET3_RXCF_TCP)
107		{
108		  tcp_header_t *tcp =
109		    (tcp_header_t *) (hb->data +
110				      vnet_buffer (hb)->l4_hdr_offset);
111		  hb->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
112		  tcp->checksum = 0;
113		}
114	      else if (rx_comp->flags & VMXNET3_RXCF_UDP)
115		{
116		  udp_header_t *udp =
117		    (udp_header_t *) (hb->data +
118				      vnet_buffer (hb)->l4_hdr_offset);
119		  hb->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
120		  udp->checksum = 0;
121		}
122	    }
123	}
124
125      if (gso_size)
126	{
127	  if (rx_comp->flags & VMXNET3_RXCF_TCP)
128	    {
129	      tcp_header_t *tcp =
130		(tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
131	      l4_hdr_sz = tcp_header_bytes (tcp);
132	    }
133	  else if (rx_comp->flags & VMXNET3_RXCF_UDP)
134	    {
135	      udp_header_t *udp =
136		(udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
137	      l4_hdr_sz = sizeof (*udp);
138	    }
139	  vnet_buffer2 (hb)->gso_size = gso_size;
140	  vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
141	  hb->flags |= VNET_BUFFER_F_GSO;
142	}
143    }
144  else if (rx_comp->flags & VMXNET3_RXCF_IP6)
145    {
146      vnet_buffer (hb)->l2_hdr_offset = 0;
147      vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
148      vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
149	sizeof (ip6_header_t);
150      hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
151	VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
152	VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP6;
153
154      /* checksum offload */
155      if (!(rx_comp->index & VMXNET3_RXCI_CNC))
156	{
157	  if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
158	    {
159	      if (rx_comp->flags & VMXNET3_RXCF_TCP)
160		{
161		  tcp_header_t *tcp =
162		    (tcp_header_t *) (hb->data +
163				      vnet_buffer (hb)->l4_hdr_offset);
164		  hb->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
165		  tcp->checksum = 0;
166		}
167	      else if (rx_comp->flags & VMXNET3_RXCF_UDP)
168		{
169		  udp_header_t *udp =
170		    (udp_header_t *) (hb->data +
171				      vnet_buffer (hb)->l4_hdr_offset);
172		  hb->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
173		  udp->checksum = 0;
174		}
175	    }
176	}
177
178      if (gso_size)
179	{
180	  if (rx_comp->flags & VMXNET3_RXCF_TCP)
181	    {
182	      tcp_header_t *tcp =
183		(tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
184	      l4_hdr_sz = tcp_header_bytes (tcp);
185	    }
186	  else if (rx_comp->flags & VMXNET3_RXCF_UDP)
187	    {
188	      udp_header_t *udp =
189		(udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
190	      l4_hdr_sz = sizeof (*udp);
191	    }
192	  vnet_buffer2 (hb)->gso_size = gso_size;
193	  vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
194	  hb->flags |= VNET_BUFFER_F_GSO;
195	}
196    }
197}
198
199static_always_inline uword
200vmxnet3_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
201			     vlib_frame_t * frame, vmxnet3_device_t * vd,
202			     u16 qid)
203{
204  vnet_main_t *vnm = vnet_get_main ();
205  uword n_trace = vlib_get_trace_count (vm, node);
206  u32 n_rx_packets = 0, n_rx_bytes = 0;
207  vmxnet3_rx_comp *rx_comp;
208  u32 desc_idx;
209  vmxnet3_rxq_t *rxq;
210  u32 thread_index = vm->thread_index;
211  u32 buffer_indices[VLIB_FRAME_SIZE], *bi;
212  u16 nexts[VLIB_FRAME_SIZE], *next;
213  vmxnet3_rx_ring *ring;
214  vmxnet3_rx_comp_ring *comp_ring;
215  u16 rid;
216  vlib_buffer_t *prev_b0 = 0, *hb = 0;
217  u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
218  u8 known_next = 0, got_packet = 0;
219  vmxnet3_rx_desc *rxd;
220  clib_error_t *error;
221  u16 gso_size = 0;
222
223  rxq = vec_elt_at_index (vd->rxqs, qid);
224  comp_ring = &rxq->rx_comp_ring;
225  bi = buffer_indices;
226  next = nexts;
227  rx_comp = &rxq->rx_comp[comp_ring->next];
228
229  while (PREDICT_TRUE ((n_rx_packets < VLIB_FRAME_SIZE) &&
230		       (comp_ring->gen ==
231			(rx_comp->flags & VMXNET3_RXCF_GEN))))
232    {
233      vlib_buffer_t *b0;
234      u32 bi0;
235
236      rid = vmxnet3_find_rid (vd, rx_comp);
237      ring = &rxq->rx_ring[rid];
238
239      if (PREDICT_TRUE (ring->fill >= 1))
240	ring->fill--;
241      else
242	{
243	  vlib_error_count (vm, node->node_index,
244			    VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
245	  if (hb)
246	    {
247	      vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
248	      hb = 0;
249	    }
250	  prev_b0 = 0;
251	  break;
252	}
253
254      desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
255      ring->consume = desc_idx;
256      rxd = &rxq->rx_desc[rid][desc_idx];
257
258      bi0 = ring->bufs[desc_idx];
259      ring->bufs[desc_idx] = ~0;
260
261      b0 = vlib_get_buffer (vm, bi0);
262      vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
263      vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
264      vnet_buffer (b0)->feature_arc_index = 0;
265      b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
266      b0->current_data = 0;
267      b0->total_length_not_including_first_buffer = 0;
268      b0->next_buffer = 0;
269      b0->flags = 0;
270      b0->error = 0;
271      b0->current_config_index = 0;
272      ASSERT (b0->current_length != 0);
273
274      if (PREDICT_FALSE ((rx_comp->index & VMXNET3_RXCI_EOP) &&
275			 (rx_comp->len & VMXNET3_RXCL_ERROR)))
276	{
277	  vlib_buffer_free_one (vm, bi0);
278	  vlib_error_count (vm, node->node_index,
279			    VMXNET3_INPUT_ERROR_RX_PACKET_EOP, 1);
280	  if (hb && vlib_get_buffer_index (vm, hb) != bi0)
281	    {
282	      vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
283	      hb = 0;
284	    }
285	  prev_b0 = 0;
286	  goto next;
287	}
288
289      if (rx_comp->index & VMXNET3_RXCI_SOP)
290	{
291	  ASSERT (!(rxd->flags & VMXNET3_RXF_BTYPE));
292	  /* start segment */
293	  if (vd->gso_enable &&
294	      (rx_comp->flags & VMXNET3_RXCF_CT) == VMXNET3_RXCOMP_TYPE_LRO)
295	    {
296	      vmxnet3_rx_comp_ext *lro = (vmxnet3_rx_comp_ext *) rx_comp;
297
298	      gso_size = lro->flags & VMXNET3_RXECF_MSS_MASK;
299	    }
300
301	  hb = b0;
302	  bi[0] = bi0;
303	  if (!(rx_comp->index & VMXNET3_RXCI_EOP))
304	    {
305	      hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
306	      prev_b0 = b0;
307	    }
308	  else
309	    {
310	      /*
311	       * Both start and end of packet is set. It is a complete packet
312	       */
313	      prev_b0 = 0;
314	      got_packet = 1;
315	    }
316	}
317      else if (rx_comp->index & VMXNET3_RXCI_EOP)
318	{
319	  /* end of segment */
320	  if (prev_b0)
321	    {
322	      prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
323	      prev_b0->next_buffer = bi0;
324	      hb->total_length_not_including_first_buffer +=
325		b0->current_length;
326	      prev_b0 = 0;
327	      got_packet = 1;
328	    }
329	  else
330	    {
331	      /* EOP without SOP, error */
332	      vlib_error_count (vm, node->node_index,
333				VMXNET3_INPUT_ERROR_RX_PACKET_NO_SOP, 1);
334	      vlib_buffer_free_one (vm, bi0);
335	      if (hb && vlib_get_buffer_index (vm, hb) != bi0)
336		{
337		  vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
338		  hb = 0;
339		}
340	      goto next;
341	    }
342	}
343      else if (prev_b0)		// !sop && !eop
344	{
345	  /* mid chain */
346	  ASSERT (rxd->flags & VMXNET3_RXF_BTYPE);
347	  prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
348	  prev_b0->next_buffer = bi0;
349	  prev_b0 = b0;
350	  hb->total_length_not_including_first_buffer += b0->current_length;
351	}
352      else
353	{
354	  vlib_error_count (vm, node->node_index,
355			    VMXNET3_INPUT_ERROR_RX_PACKET, 1);
356	  vlib_buffer_free_one (vm, bi0);
357	  if (hb && vlib_get_buffer_index (vm, hb) != bi0)
358	    {
359	      vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
360	      hb = 0;
361	    }
362	  goto next;
363	}
364
365      n_rx_bytes += b0->current_length;
366
367      if (got_packet)
368	{
369	  if (PREDICT_FALSE (vd->per_interface_next_index != ~0))
370	    {
371	      next_index = vd->per_interface_next_index;
372	      known_next = 1;
373	    }
374
375	  if (PREDICT_FALSE
376	      (vnet_device_input_have_features (vd->sw_if_index)))
377	    {
378	      vnet_feature_start_device_input_x1 (vd->sw_if_index,
379						  &next_index, hb);
380	      known_next = 1;
381	    }
382
383	  if (PREDICT_FALSE (known_next))
384	    next[0] = next_index;
385	  else
386	    {
387	      ethernet_header_t *e = (ethernet_header_t *) hb->data;
388
389	      next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
390	      if (!ethernet_frame_is_tagged (e->type))
391		vmxnet3_handle_offload (rx_comp, hb, gso_size);
392	    }
393
394	  n_rx_packets++;
395	  next++;
396	  bi++;
397	  hb = 0;
398	  got_packet = 0;
399	  gso_size = 0;
400	}
401
402    next:
403      vmxnet3_rx_comp_ring_advance_next (rxq);
404      rx_comp = &rxq->rx_comp[comp_ring->next];
405    }
406
407  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
408    {
409      u32 n_left = n_rx_packets;
410
411      bi = buffer_indices;
412      next = nexts;
413      while (n_trace && n_left)
414	{
415	  vlib_buffer_t *b;
416	  vmxnet3_input_trace_t *tr;
417
418	  b = vlib_get_buffer (vm, bi[0]);
419	  vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
420	  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
421	  tr->next_index = next[0];
422	  tr->hw_if_index = vd->hw_if_index;
423	  tr->buffer = *b;
424
425	  n_trace--;
426	  n_left--;
427	  bi++;
428	  next++;
429	}
430      vlib_set_trace_count (vm, node, n_trace);
431    }
432
433  if (PREDICT_TRUE (n_rx_packets))
434    {
435      vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
436				   n_rx_packets);
437      vlib_increment_combined_counter
438	(vnm->interface_main.combined_sw_if_counters +
439	 VNET_INTERFACE_COUNTER_RX, thread_index,
440	 vd->sw_if_index, n_rx_packets, n_rx_bytes);
441    }
442
443  error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
444  if (PREDICT_FALSE (error != 0))
445    {
446      vlib_error_count (vm, node->node_index,
447			VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
448    }
449  error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
450  if (PREDICT_FALSE (error != 0))
451    {
452      vlib_error_count (vm, node->node_index,
453			VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
454    }
455
456  return n_rx_packets;
457}
458
459VLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
460				   vlib_node_runtime_t * node,
461				   vlib_frame_t * frame)
462{
463  u32 n_rx = 0;
464  vmxnet3_main_t *vmxm = &vmxnet3_main;
465  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
466  vnet_device_and_queue_t *dq;
467
468  foreach_device_and_queue (dq, rt->devices_and_queues)
469  {
470    vmxnet3_device_t *vd;
471    vd = vec_elt_at_index (vmxm->devices, dq->dev_instance);
472    if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
473      continue;
474    n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, dq->queue_id);
475  }
476  return n_rx;
477}
478
479#ifndef CLIB_MARCH_VARIANT
480/* *INDENT-OFF* */
481VLIB_REGISTER_NODE (vmxnet3_input_node) = {
482  .name = "vmxnet3-input",
483  .sibling_of = "device-input",
484  .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
485  .format_trace = format_vmxnet3_input_trace,
486  .type = VLIB_NODE_TYPE_INPUT,
487  .state = VLIB_NODE_STATE_DISABLED,
488  .n_errors = VMXNET3_INPUT_N_ERROR,
489  .error_strings = vmxnet3_input_error_strings,
490};
491#endif
492
493/* *INDENT-ON* */
494
495/*
496 * fd.io coding-style-patch-verification: ON
497 *
498 * Local Variables:
499 * eval: (c-set-style "gnu")
500 * End:
501 */
502