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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
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 #include <vnet/interface/rx_queue_funcs.h>
27 #include <vmxnet3/vmxnet3.h>
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")
38 #define _(f,s) VMXNET3_INPUT_ERROR_##f,
39 foreach_vmxnet3_input_error
41 VMXNET3_INPUT_N_ERROR,
42 } vmxnet3_input_error_t;
44 static __clib_unused char *vmxnet3_input_error_strings[] = {
46 foreach_vmxnet3_input_error
50 static_always_inline u16
51 vmxnet3_find_rid (vmxnet3_device_t * vd, vmxnet3_rx_comp * rx_comp)
55 // rid is bits 16-25 (10 bits number)
56 rid = rx_comp->index & (0xffffffff >> 6);
58 if ((rid >= vd->num_rx_queues) && (rid < (vd->num_rx_queues << 1)))
64 static_always_inline void
65 vmxnet3_rx_comp_ring_advance_next (vmxnet3_rxq_t * rxq)
67 vmxnet3_rx_comp_ring *comp_ring = &rxq->rx_comp_ring;
70 if (PREDICT_FALSE (comp_ring->next == rxq->size))
73 comp_ring->gen ^= VMXNET3_RXCF_GEN;
77 static_always_inline void
78 vmxnet3_handle_offload (vmxnet3_rx_comp * rx_comp, vlib_buffer_t * hb,
82 vnet_buffer_oflags_t oflags = 0;
84 if (rx_comp->flags & VMXNET3_RXCF_IP4)
86 ip4_header_t *ip4 = (ip4_header_t *) (hb->data +
87 sizeof (ethernet_header_t));
89 vnet_buffer (hb)->l2_hdr_offset = 0;
90 vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
91 vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
92 ip4_header_bytes (ip4);
93 hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
94 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
95 VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP4;
97 /* checksum offload */
98 if (!(rx_comp->index & VMXNET3_RXCI_CNC))
100 if (!(rx_comp->flags & VMXNET3_RXCF_IPC))
102 oflags |= VNET_BUFFER_OFFLOAD_F_IP_CKSUM;
105 if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
107 if (rx_comp->flags & VMXNET3_RXCF_TCP)
109 oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
111 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
113 oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
120 if (rx_comp->flags & VMXNET3_RXCF_TCP)
123 (tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
124 l4_hdr_sz = tcp_header_bytes (tcp);
126 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
129 (udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
130 l4_hdr_sz = sizeof (*udp);
132 vnet_buffer2 (hb)->gso_size = gso_size;
133 vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
134 hb->flags |= VNET_BUFFER_F_GSO;
137 else if (rx_comp->flags & VMXNET3_RXCF_IP6)
139 vnet_buffer (hb)->l2_hdr_offset = 0;
140 vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
141 vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
142 sizeof (ip6_header_t);
143 hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
144 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
145 VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP6;
147 /* checksum offload */
148 if (!(rx_comp->index & VMXNET3_RXCI_CNC))
150 if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
152 if (rx_comp->flags & VMXNET3_RXCF_TCP)
155 (tcp_header_t *) (hb->data +
156 vnet_buffer (hb)->l4_hdr_offset);
157 oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
160 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
163 (udp_header_t *) (hb->data +
164 vnet_buffer (hb)->l4_hdr_offset);
165 oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
173 if (rx_comp->flags & VMXNET3_RXCF_TCP)
176 (tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
177 l4_hdr_sz = tcp_header_bytes (tcp);
179 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
182 (udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
183 l4_hdr_sz = sizeof (*udp);
185 vnet_buffer2 (hb)->gso_size = gso_size;
186 vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
187 hb->flags |= VNET_BUFFER_F_GSO;
191 vnet_buffer_offload_flags_set (hb, oflags);
194 static_always_inline uword
195 vmxnet3_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
196 vlib_frame_t * frame, vmxnet3_device_t * vd,
199 vnet_main_t *vnm = vnet_get_main ();
200 uword n_trace = vlib_get_trace_count (vm, node);
201 u32 n_rx_packets = 0, n_rx_bytes = 0;
202 vmxnet3_rx_comp *rx_comp;
205 u32 thread_index = vm->thread_index;
206 u32 buffer_indices[VLIB_FRAME_SIZE], *bi;
207 u16 nexts[VLIB_FRAME_SIZE], *next;
208 vmxnet3_rx_ring *ring;
209 vmxnet3_rx_comp_ring *comp_ring;
211 vlib_buffer_t *prev_b0 = 0, *hb = 0;
212 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
213 u8 known_next = 0, got_packet = 0;
214 vmxnet3_rx_desc *rxd;
218 rxq = vec_elt_at_index (vd->rxqs, qid);
219 comp_ring = &rxq->rx_comp_ring;
222 rx_comp = &rxq->rx_comp[comp_ring->next];
224 while (PREDICT_TRUE ((n_rx_packets < VLIB_FRAME_SIZE) &&
226 (rx_comp->flags & VMXNET3_RXCF_GEN))))
231 rid = vmxnet3_find_rid (vd, rx_comp);
232 ring = &rxq->rx_ring[rid];
234 if (PREDICT_TRUE (ring->fill >= 1))
238 vlib_error_count (vm, node->node_index,
239 VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
242 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
249 desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
250 ring->consume = desc_idx;
251 rxd = &rxq->rx_desc[rid][desc_idx];
253 bi0 = ring->bufs[desc_idx];
254 ring->bufs[desc_idx] = ~0;
256 b0 = vlib_get_buffer (vm, bi0);
257 vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
258 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
259 vnet_buffer (b0)->feature_arc_index = 0;
260 b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
261 b0->current_data = 0;
262 b0->total_length_not_including_first_buffer = 0;
266 b0->current_config_index = 0;
268 if (PREDICT_FALSE ((rx_comp->index & VMXNET3_RXCI_EOP) &&
269 (rx_comp->len & VMXNET3_RXCL_ERROR)))
271 vlib_buffer_free_one (vm, bi0);
272 vlib_error_count (vm, node->node_index,
273 VMXNET3_INPUT_ERROR_RX_PACKET_EOP, 1);
274 if (hb && vlib_get_buffer_index (vm, hb) != bi0)
276 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
283 if (rx_comp->index & VMXNET3_RXCI_SOP)
285 ASSERT (!(rxd->flags & VMXNET3_RXF_BTYPE));
287 if (vd->gso_enable &&
288 (rx_comp->flags & VMXNET3_RXCF_CT) == VMXNET3_RXCOMP_TYPE_LRO)
290 vmxnet3_rx_comp_ext *lro = (vmxnet3_rx_comp_ext *) rx_comp;
292 gso_size = lro->flags & VMXNET3_RXECF_MSS_MASK;
297 if (!(rx_comp->index & VMXNET3_RXCI_EOP))
299 hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
305 * Both start and end of packet is set. It is a complete packet
311 else if (rx_comp->index & VMXNET3_RXCI_EOP)
314 if (PREDICT_TRUE (prev_b0 != 0))
316 if (PREDICT_TRUE (b0->current_length != 0))
318 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
319 prev_b0->next_buffer = bi0;
320 hb->total_length_not_including_first_buffer +=
325 vlib_buffer_free_one (vm, bi0);
332 /* EOP without SOP, error */
333 vlib_error_count (vm, node->node_index,
334 VMXNET3_INPUT_ERROR_RX_PACKET_NO_SOP, 1);
335 vlib_buffer_free_one (vm, bi0);
336 if (hb && vlib_get_buffer_index (vm, hb) != bi0)
338 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
344 else if (prev_b0) // !sop && !eop
347 ASSERT (rxd->flags & VMXNET3_RXF_BTYPE);
348 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
349 prev_b0->next_buffer = bi0;
351 hb->total_length_not_including_first_buffer += b0->current_length;
355 vlib_error_count (vm, node->node_index,
356 VMXNET3_INPUT_ERROR_RX_PACKET, 1);
357 vlib_buffer_free_one (vm, bi0);
358 if (hb && vlib_get_buffer_index (vm, hb) != bi0)
360 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
366 n_rx_bytes += b0->current_length;
370 if (PREDICT_FALSE (vd->per_interface_next_index != ~0))
372 next_index = vd->per_interface_next_index;
377 (vnet_device_input_have_features (vd->sw_if_index)))
379 vnet_feature_start_device_input_x1 (vd->sw_if_index,
384 if (PREDICT_FALSE (known_next))
385 next[0] = next_index;
388 ethernet_header_t *e = (ethernet_header_t *) hb->data;
390 next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
391 if (!ethernet_frame_is_tagged (ntohs (e->type)))
392 vmxnet3_handle_offload (rx_comp, hb, gso_size);
404 vmxnet3_rx_comp_ring_advance_next (rxq);
405 rx_comp = &rxq->rx_comp[comp_ring->next];
408 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
410 u32 n_left = n_rx_packets;
414 while (n_trace && n_left)
416 vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
419 (vm, node, next[0], b, /* follow_chain */ 0)))
421 vmxnet3_input_trace_t *tr =
422 vlib_add_trace (vm, node, b, sizeof (*tr));
423 tr->next_index = next[0];
424 tr->hw_if_index = vd->hw_if_index;
432 vlib_set_trace_count (vm, node, n_trace);
435 if (PREDICT_TRUE (n_rx_packets))
437 vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
439 vlib_increment_combined_counter
440 (vnm->interface_main.combined_sw_if_counters +
441 VNET_INTERFACE_COUNTER_RX, thread_index,
442 vd->sw_if_index, n_rx_packets, n_rx_bytes);
445 error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
446 if (PREDICT_FALSE (error != 0))
448 vlib_error_count (vm, node->node_index,
449 VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
451 error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
452 if (PREDICT_FALSE (error != 0))
454 vlib_error_count (vm, node->node_index,
455 VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
461 VLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
462 vlib_node_runtime_t * node,
463 vlib_frame_t * frame)
466 vmxnet3_main_t *vmxm = &vmxnet3_main;
467 vnet_hw_if_rxq_poll_vector_t *pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
468 vnet_hw_if_rxq_poll_vector_t *pve;
470 vec_foreach (pve, pv)
472 vmxnet3_device_t *vd;
473 vd = vec_elt_at_index (vmxm->devices, pve->dev_instance);
474 if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
476 n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, pve->queue_id);
481 #ifndef CLIB_MARCH_VARIANT
483 VLIB_REGISTER_NODE (vmxnet3_input_node) = {
484 .name = "vmxnet3-input",
485 .sibling_of = "device-input",
486 .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
487 .format_trace = format_vmxnet3_input_trace,
488 .type = VLIB_NODE_TYPE_INPUT,
489 .state = VLIB_NODE_STATE_DISABLED,
490 .n_errors = VMXNET3_INPUT_N_ERROR,
491 .error_strings = vmxnet3_input_error_strings,
498 * fd.io coding-style-patch-verification: ON
501 * eval: (c-set-style "gnu")