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/tcp/tcp_packet.h>
27 #include <vnet/interface/rx_queue_funcs.h>
28 #include <vmxnet3/vmxnet3.h>
30 #define foreach_vmxnet3_input_error \
31 _(BUFFER_ALLOC, "buffer alloc error") \
32 _(RX_PACKET_NO_SOP, "Rx packet error - no SOP") \
33 _(RX_PACKET, "Rx packet error") \
34 _(RX_PACKET_EOP, "Rx packet error found on EOP") \
35 _(NO_BUFFER, "Rx no buffer error")
39 #define _(f,s) VMXNET3_INPUT_ERROR_##f,
40 foreach_vmxnet3_input_error
42 VMXNET3_INPUT_N_ERROR,
43 } vmxnet3_input_error_t;
45 static __clib_unused char *vmxnet3_input_error_strings[] = {
47 foreach_vmxnet3_input_error
51 static_always_inline u16
52 vmxnet3_find_rid (vmxnet3_device_t * vd, vmxnet3_rx_comp * rx_comp)
56 // rid is bits 16-25 (10 bits number)
57 rid = rx_comp->index & (0xffffffff >> 6);
59 if ((rid >= vd->num_rx_queues) && (rid < (vd->num_rx_queues << 1)))
65 static_always_inline void
66 vmxnet3_rx_comp_ring_advance_next (vmxnet3_rxq_t * rxq)
68 vmxnet3_rx_comp_ring *comp_ring = &rxq->rx_comp_ring;
71 if (PREDICT_FALSE (comp_ring->next == rxq->size))
74 comp_ring->gen ^= VMXNET3_RXCF_GEN;
78 static_always_inline void
79 vmxnet3_handle_offload (vmxnet3_rx_comp * rx_comp, vlib_buffer_t * hb,
83 vnet_buffer_oflags_t oflags = 0;
85 if (rx_comp->flags & VMXNET3_RXCF_IP4)
87 ip4_header_t *ip4 = (ip4_header_t *) (hb->data +
88 sizeof (ethernet_header_t));
90 vnet_buffer (hb)->l2_hdr_offset = 0;
91 vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
92 vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
93 ip4_header_bytes (ip4);
94 hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
95 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
96 VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP4;
98 /* checksum offload */
99 if (!(rx_comp->index & VMXNET3_RXCI_CNC))
101 if (!(rx_comp->flags & VMXNET3_RXCF_IPC))
103 oflags |= VNET_BUFFER_OFFLOAD_F_IP_CKSUM;
106 if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
108 if (rx_comp->flags & VMXNET3_RXCF_TCP)
110 oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
112 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
114 oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
121 if (rx_comp->flags & VMXNET3_RXCF_TCP)
124 (tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
125 l4_hdr_sz = tcp_header_bytes (tcp);
127 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
130 (udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
131 l4_hdr_sz = sizeof (*udp);
133 vnet_buffer2 (hb)->gso_size = gso_size;
134 vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
135 hb->flags |= VNET_BUFFER_F_GSO;
138 else if (rx_comp->flags & VMXNET3_RXCF_IP6)
140 vnet_buffer (hb)->l2_hdr_offset = 0;
141 vnet_buffer (hb)->l3_hdr_offset = sizeof (ethernet_header_t);
142 vnet_buffer (hb)->l4_hdr_offset = sizeof (ethernet_header_t) +
143 sizeof (ip6_header_t);
144 hb->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
145 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
146 VNET_BUFFER_F_L4_HDR_OFFSET_VALID | VNET_BUFFER_F_IS_IP6;
148 /* checksum offload */
149 if (!(rx_comp->index & VMXNET3_RXCI_CNC))
151 if (!(rx_comp->flags & VMXNET3_RXCF_TUC))
153 if (rx_comp->flags & VMXNET3_RXCF_TCP)
156 (tcp_header_t *) (hb->data +
157 vnet_buffer (hb)->l4_hdr_offset);
158 oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM;
161 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
164 (udp_header_t *) (hb->data +
165 vnet_buffer (hb)->l4_hdr_offset);
166 oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM;
174 if (rx_comp->flags & VMXNET3_RXCF_TCP)
177 (tcp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
178 l4_hdr_sz = tcp_header_bytes (tcp);
180 else if (rx_comp->flags & VMXNET3_RXCF_UDP)
183 (udp_header_t *) (hb->data + vnet_buffer (hb)->l4_hdr_offset);
184 l4_hdr_sz = sizeof (*udp);
186 vnet_buffer2 (hb)->gso_size = gso_size;
187 vnet_buffer2 (hb)->gso_l4_hdr_sz = l4_hdr_sz;
188 hb->flags |= VNET_BUFFER_F_GSO;
192 vnet_buffer_offload_flags_set (hb, oflags);
195 static_always_inline uword
196 vmxnet3_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
197 vlib_frame_t * frame, vmxnet3_device_t * vd,
200 vnet_main_t *vnm = vnet_get_main ();
201 uword n_trace = vlib_get_trace_count (vm, node);
202 u32 n_rx_packets = 0, n_rx_bytes = 0;
203 vmxnet3_rx_comp *rx_comp;
206 u32 thread_index = vm->thread_index;
207 u32 buffer_indices[VLIB_FRAME_SIZE], *bi;
208 u16 nexts[VLIB_FRAME_SIZE], *next;
209 vmxnet3_rx_ring *ring;
210 vmxnet3_rx_comp_ring *comp_ring;
212 vlib_buffer_t *prev_b0 = 0, *hb = 0;
213 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
214 u8 known_next = 0, got_packet = 0;
215 vmxnet3_rx_desc *rxd;
219 rxq = vec_elt_at_index (vd->rxqs, qid);
220 comp_ring = &rxq->rx_comp_ring;
223 rx_comp = &rxq->rx_comp[comp_ring->next];
225 while (PREDICT_TRUE ((n_rx_packets < VLIB_FRAME_SIZE) &&
227 (rx_comp->flags & VMXNET3_RXCF_GEN))))
232 rid = vmxnet3_find_rid (vd, rx_comp);
233 ring = &rxq->rx_ring[rid];
235 if (PREDICT_TRUE (ring->fill >= 1))
239 vlib_error_count (vm, node->node_index,
240 VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
243 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
250 desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
251 ring->consume = desc_idx;
252 rxd = &rxq->rx_desc[rid][desc_idx];
254 bi0 = ring->bufs[desc_idx];
255 ring->bufs[desc_idx] = ~0;
257 b0 = vlib_get_buffer (vm, bi0);
258 vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
259 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
260 vnet_buffer (b0)->feature_arc_index = 0;
261 b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
262 b0->current_data = 0;
263 b0->total_length_not_including_first_buffer = 0;
267 b0->current_config_index = 0;
269 if (PREDICT_FALSE ((rx_comp->index & VMXNET3_RXCI_EOP) &&
270 (rx_comp->len & VMXNET3_RXCL_ERROR)))
272 vlib_buffer_free_one (vm, bi0);
273 vlib_error_count (vm, node->node_index,
274 VMXNET3_INPUT_ERROR_RX_PACKET_EOP, 1);
275 if (hb && vlib_get_buffer_index (vm, hb) != bi0)
277 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
284 if (rx_comp->index & VMXNET3_RXCI_SOP)
286 ASSERT (!(rxd->flags & VMXNET3_RXF_BTYPE));
288 if (vd->gso_enable &&
289 (rx_comp->flags & VMXNET3_RXCF_CT) == VMXNET3_RXCOMP_TYPE_LRO)
291 vmxnet3_rx_comp_ext *lro = (vmxnet3_rx_comp_ext *) rx_comp;
293 gso_size = lro->flags & VMXNET3_RXECF_MSS_MASK;
298 if (!(rx_comp->index & VMXNET3_RXCI_EOP))
300 hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
306 * Both start and end of packet is set. It is a complete packet
312 else if (rx_comp->index & VMXNET3_RXCI_EOP)
315 if (PREDICT_TRUE (prev_b0 != 0))
317 if (PREDICT_TRUE (b0->current_length != 0))
319 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
320 prev_b0->next_buffer = bi0;
321 hb->total_length_not_including_first_buffer +=
326 vlib_buffer_free_one (vm, bi0);
333 /* EOP without SOP, error */
334 vlib_error_count (vm, node->node_index,
335 VMXNET3_INPUT_ERROR_RX_PACKET_NO_SOP, 1);
336 vlib_buffer_free_one (vm, bi0);
337 if (hb && vlib_get_buffer_index (vm, hb) != bi0)
339 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
345 else if (prev_b0) // !sop && !eop
348 ASSERT (rxd->flags & VMXNET3_RXF_BTYPE);
349 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
350 prev_b0->next_buffer = bi0;
352 hb->total_length_not_including_first_buffer += b0->current_length;
356 vlib_error_count (vm, node->node_index,
357 VMXNET3_INPUT_ERROR_RX_PACKET, 1);
358 vlib_buffer_free_one (vm, bi0);
359 if (hb && vlib_get_buffer_index (vm, hb) != bi0)
361 vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
367 n_rx_bytes += b0->current_length;
371 if (PREDICT_FALSE (vd->per_interface_next_index != ~0))
373 next_index = vd->per_interface_next_index;
378 (vnet_device_input_have_features (vd->sw_if_index)))
380 vnet_feature_start_device_input (vd->sw_if_index, &next_index,
385 if (PREDICT_FALSE (known_next))
386 next[0] = next_index;
389 ethernet_header_t *e = (ethernet_header_t *) hb->data;
391 next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
392 if (!ethernet_frame_is_tagged (ntohs (e->type)))
393 vmxnet3_handle_offload (rx_comp, hb, gso_size);
405 vmxnet3_rx_comp_ring_advance_next (rxq);
406 rx_comp = &rxq->rx_comp[comp_ring->next];
409 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
411 u32 n_left = n_rx_packets;
415 while (n_trace && n_left)
417 vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
420 (vm, node, next[0], b, /* follow_chain */ 0)))
422 vmxnet3_input_trace_t *tr =
423 vlib_add_trace (vm, node, b, sizeof (*tr));
424 tr->next_index = next[0];
425 tr->hw_if_index = vd->hw_if_index;
433 vlib_set_trace_count (vm, node, n_trace);
436 if (PREDICT_TRUE (n_rx_packets))
438 vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
440 vlib_increment_combined_counter
441 (vnm->interface_main.combined_sw_if_counters +
442 VNET_INTERFACE_COUNTER_RX, thread_index,
443 vd->sw_if_index, n_rx_packets, n_rx_bytes);
446 error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
447 if (PREDICT_FALSE (error != 0))
449 vlib_error_count (vm, node->node_index,
450 VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
452 error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
453 if (PREDICT_FALSE (error != 0))
455 vlib_error_count (vm, node->node_index,
456 VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
462 VLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
463 vlib_node_runtime_t * node,
464 vlib_frame_t * frame)
467 vmxnet3_main_t *vmxm = &vmxnet3_main;
468 vnet_hw_if_rxq_poll_vector_t *pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
469 vnet_hw_if_rxq_poll_vector_t *pve;
471 vec_foreach (pve, pv)
473 vmxnet3_device_t *vd;
474 vd = vec_elt_at_index (vmxm->devices, pve->dev_instance);
475 if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
477 n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, pve->queue_id);
482 #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,
497 * fd.io coding-style-patch-verification: ON
500 * eval: (c-set-style "gnu")