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>
24 #include <vmxnet3/vmxnet3.h>
26 #define foreach_vmxnet3_input_error \
27 _(BUFFER_ALLOC, "buffer alloc error") \
28 _(RX_PACKET, "Rx packet error") \
29 _(NO_BUFFER, "Rx no buffer error")
33 #define _(f,s) VMXNET3_INPUT_ERROR_##f,
34 foreach_vmxnet3_input_error
36 VMXNET3_INPUT_N_ERROR,
37 } vmxnet3_input_error_t;
39 static __clib_unused char *vmxnet3_input_error_strings[] = {
41 foreach_vmxnet3_input_error
45 static_always_inline u16
46 vmxnet3_find_rid (vmxnet3_device_t * vd, vmxnet3_rx_comp * rx_comp)
50 // rid is bits 16-25 (10 bits number)
51 rid = rx_comp->index & (0xffffffff >> 6);
53 if ((rid >= vd->num_rx_queues) && (rid < (vd->num_rx_queues << 1)))
59 static_always_inline void
60 vmxnet3_rx_comp_ring_advance_next (vmxnet3_rxq_t * rxq)
62 vmxnet3_rx_comp_ring *comp_ring = &rxq->rx_comp_ring;
65 if (PREDICT_FALSE (comp_ring->next == rxq->size))
68 comp_ring->gen ^= VMXNET3_RXCF_GEN;
72 static_always_inline uword
73 vmxnet3_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
74 vlib_frame_t * frame, vmxnet3_device_t * vd,
77 vnet_main_t *vnm = vnet_get_main ();
78 uword n_trace = vlib_get_trace_count (vm, node);
79 u32 n_rx_packets = 0, n_rx_bytes = 0;
80 vmxnet3_rx_comp *rx_comp;
84 u32 thread_index = vm->thread_index;
85 u32 buffer_indices[VLIB_FRAME_SIZE], *bi;
86 u16 nexts[VLIB_FRAME_SIZE], *next;
87 vmxnet3_rx_ring *ring;
88 vmxnet3_rx_comp_ring *comp_ring;
90 vlib_buffer_t *prev_b0 = 0, *hb = 0;
91 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
94 rxq = vec_elt_at_index (vd->rxqs, qid);
95 comp_ring = &rxq->rx_comp_ring;
98 while (comp_ring->gen ==
99 (rxq->rx_comp[comp_ring->next].flags & VMXNET3_RXCF_GEN))
104 comp_idx = comp_ring->next;
105 rx_comp = &rxq->rx_comp[comp_idx];
107 rid = vmxnet3_find_rid (vd, rx_comp);
108 ring = &rxq->rx_ring[rid];
110 if (PREDICT_TRUE (ring->fill >= 1))
114 vlib_error_count (vm, node->node_index,
115 VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
119 vmxnet3_rx_comp_ring_advance_next (rxq);
120 desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
121 ring->consume = desc_idx;
123 bi0 = ring->bufs[desc_idx];
124 ring->bufs[desc_idx] = ~0;
126 b0 = vlib_get_buffer (vm, bi0);
127 vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
128 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
129 vnet_buffer (b0)->feature_arc_index = 0;
130 b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
131 b0->current_data = 0;
132 b0->total_length_not_including_first_buffer = 0;
136 b0->current_config_index = 0;
137 ASSERT (b0->current_length != 0);
139 if (rx_comp->index & VMXNET3_RXCI_SOP)
144 if (!(rx_comp->index & VMXNET3_RXCI_EOP))
146 hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
147 b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
153 * Both start and end of packet is set. It is a complete packet
158 else if (rx_comp->index & VMXNET3_RXCI_EOP)
163 prev_b0->next_buffer = bi0;
164 prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
165 hb->total_length_not_including_first_buffer +=
167 prev_b0 = 0; // Get next packet
171 /* EOP without SOP, error */
173 vlib_error_count (vm, node->node_index,
174 VMXNET3_INPUT_ERROR_RX_PACKET, 1);
175 vlib_buffer_free_one (vm, bi0);
179 else if (prev_b0) // !sop && !eop
182 b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
183 prev_b0->next_buffer = bi0;
185 hb->total_length_not_including_first_buffer += b0->current_length;
192 n_rx_bytes += b0->current_length;
196 ethernet_header_t *e = (ethernet_header_t *) hb->data;
198 if (PREDICT_FALSE (vd->per_interface_next_index != ~0))
200 next_index = vd->per_interface_next_index;
205 (vnet_device_input_have_features (vd->sw_if_index)))
207 vnet_feature_start_device_input_x1 (vd->sw_if_index,
212 if (PREDICT_FALSE (known_next))
214 next[0] = next_index;
218 if (ethernet_frame_is_tagged (e->type))
219 next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
222 if (rx_comp->flags & VMXNET3_RXCF_IP4)
224 next[0] = VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT;
225 hb->flags |= VNET_BUFFER_F_IS_IP4;
226 vlib_buffer_advance (hb,
227 device_input_next_node_advance
230 else if (rx_comp->flags & VMXNET3_RXCF_IP6)
232 next[0] = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
233 hb->flags |= VNET_BUFFER_F_IS_IP6;
234 vlib_buffer_advance (hb,
235 device_input_next_node_advance
240 next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
252 if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
254 u32 n_left = n_rx_packets;
258 while (n_trace && n_left)
261 vmxnet3_input_trace_t *tr;
263 b = vlib_get_buffer (vm, bi[0]);
264 vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
265 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
266 tr->next_index = next[0];
267 tr->hw_if_index = vd->hw_if_index;
275 vlib_set_trace_count (vm, node, n_trace);
278 if (PREDICT_TRUE (n_rx_packets))
282 vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
284 vlib_increment_combined_counter
285 (vnm->interface_main.combined_sw_if_counters +
286 VNET_INTERFACE_COUNTER_RX, thread_index,
287 vd->hw_if_index, n_rx_packets, n_rx_bytes);
289 error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
290 if (PREDICT_FALSE (error != 0))
292 vlib_error_count (vm, node->node_index,
293 VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
295 error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
296 if (PREDICT_FALSE (error != 0))
298 vlib_error_count (vm, node->node_index,
299 VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
306 VLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
307 vlib_node_runtime_t * node,
308 vlib_frame_t * frame)
311 vmxnet3_main_t *vmxm = &vmxnet3_main;
312 vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
313 vnet_device_and_queue_t *dq;
315 foreach_device_and_queue (dq, rt->devices_and_queues)
317 vmxnet3_device_t *vd;
318 vd = vec_elt_at_index (vmxm->devices, dq->dev_instance);
319 if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
321 n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, dq->queue_id);
326 #ifndef CLIB_MARCH_VARIANT
328 VLIB_REGISTER_NODE (vmxnet3_input_node) = {
329 .name = "vmxnet3-input",
330 .sibling_of = "device-input",
331 .format_trace = format_vmxnet3_input_trace,
332 .type = VLIB_NODE_TYPE_INPUT,
333 .state = VLIB_NODE_STATE_DISABLED,
334 .n_errors = VMXNET3_INPUT_N_ERROR,
335 .error_strings = vmxnet3_input_error_strings,
342 * fd.io coding-style-patch-verification: ON
345 * eval: (c-set-style "gnu")