4ff459a066ac603b8a6a6b3a445d3c694fee6844
[vpp.git] / src / plugins / vmxnet3 / input.c
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
24 #include <vmxnet3/vmxnet3.h>
25
26 #define foreach_vmxnet3_input_error \
27   _(BUFFER_ALLOC, "buffer alloc error") \
28   _(RX_PACKET_NO_SOP, "Rx packet error - no SOP") \
29   _(RX_PACKET, "Rx packet error") \
30   _(NO_BUFFER, "Rx no buffer error")
31
32 typedef enum
33 {
34 #define _(f,s) VMXNET3_INPUT_ERROR_##f,
35   foreach_vmxnet3_input_error
36 #undef _
37     VMXNET3_INPUT_N_ERROR,
38 } vmxnet3_input_error_t;
39
40 static __clib_unused char *vmxnet3_input_error_strings[] = {
41 #define _(n,s) s,
42   foreach_vmxnet3_input_error
43 #undef _
44 };
45
46 static_always_inline u16
47 vmxnet3_find_rid (vmxnet3_device_t * vd, vmxnet3_rx_comp * rx_comp)
48 {
49   u32 rid;
50
51   // rid is bits 16-25 (10 bits number)
52   rid = rx_comp->index & (0xffffffff >> 6);
53   rid >>= 16;
54   if ((rid >= vd->num_rx_queues) && (rid < (vd->num_rx_queues << 1)))
55     return 1;
56   else
57     return 0;
58 }
59
60 static_always_inline void
61 vmxnet3_rx_comp_ring_advance_next (vmxnet3_rxq_t * rxq)
62 {
63   vmxnet3_rx_comp_ring *comp_ring = &rxq->rx_comp_ring;
64
65   comp_ring->next++;
66   if (PREDICT_FALSE (comp_ring->next == rxq->size))
67     {
68       comp_ring->next = 0;
69       comp_ring->gen ^= VMXNET3_RXCF_GEN;
70     }
71 }
72
73 static_always_inline uword
74 vmxnet3_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
75                              vlib_frame_t * frame, vmxnet3_device_t * vd,
76                              u16 qid)
77 {
78   vnet_main_t *vnm = vnet_get_main ();
79   uword n_trace = vlib_get_trace_count (vm, node);
80   u32 n_rx_packets = 0, n_rx_bytes = 0;
81   vmxnet3_rx_comp *rx_comp;
82   u32 comp_idx;
83   u32 desc_idx;
84   vmxnet3_rxq_t *rxq;
85   u32 thread_index = vm->thread_index;
86   u32 buffer_indices[VLIB_FRAME_SIZE], *bi;
87   u16 nexts[VLIB_FRAME_SIZE], *next;
88   vmxnet3_rx_ring *ring;
89   vmxnet3_rx_comp_ring *comp_ring;
90   u16 rid;
91   vlib_buffer_t *prev_b0 = 0, *hb = 0;
92   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
93   u8 known_next = 0, got_packet = 0;
94   vmxnet3_rx_desc *rxd;
95   clib_error_t *error;
96
97   rxq = vec_elt_at_index (vd->rxqs, qid);
98   comp_ring = &rxq->rx_comp_ring;
99   bi = buffer_indices;
100   next = nexts;
101   while (PREDICT_TRUE (n_rx_packets < VLIB_FRAME_SIZE) &&
102          (comp_ring->gen ==
103           (rxq->rx_comp[comp_ring->next].flags & VMXNET3_RXCF_GEN)))
104     {
105       vlib_buffer_t *b0;
106       u32 bi0;
107
108       comp_idx = comp_ring->next;
109       rx_comp = &rxq->rx_comp[comp_idx];
110
111       rid = vmxnet3_find_rid (vd, rx_comp);
112       ring = &rxq->rx_ring[rid];
113
114       if (PREDICT_TRUE (ring->fill >= 1))
115         ring->fill--;
116       else
117         {
118           vlib_error_count (vm, node->node_index,
119                             VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
120           break;
121         }
122
123       vmxnet3_rx_comp_ring_advance_next (rxq);
124       desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
125       ring->consume = desc_idx;
126       rxd = &rxq->rx_desc[rid][desc_idx];
127
128       bi0 = ring->bufs[desc_idx];
129       ring->bufs[desc_idx] = ~0;
130
131       b0 = vlib_get_buffer (vm, bi0);
132       vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
133       vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
134       vnet_buffer (b0)->feature_arc_index = 0;
135       b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
136       b0->current_data = 0;
137       b0->total_length_not_including_first_buffer = 0;
138       b0->next_buffer = 0;
139       b0->flags = 0;
140       b0->error = 0;
141       b0->current_config_index = 0;
142       ASSERT (b0->current_length != 0);
143
144       if (PREDICT_FALSE ((rx_comp->index & VMXNET3_RXCI_EOP) &&
145                          (rx_comp->len & VMXNET3_RXCL_ERROR)))
146         {
147           vlib_buffer_free_one (vm, bi0);
148           vlib_error_count (vm, node->node_index,
149                             VMXNET3_INPUT_ERROR_RX_PACKET, 1);
150           if (hb && vlib_get_buffer_index (vm, hb) != bi0)
151             {
152               vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
153               hb = 0;
154             }
155           prev_b0 = 0;
156           continue;
157         }
158
159       if (rx_comp->index & VMXNET3_RXCI_SOP)
160         {
161           ASSERT (!(rxd->flags & VMXNET3_RXF_BTYPE));
162           /* start segment */
163           hb = b0;
164           bi[0] = bi0;
165           if (!(rx_comp->index & VMXNET3_RXCI_EOP))
166             {
167               hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
168               prev_b0 = b0;
169             }
170           else
171             {
172               /*
173                * Both start and end of packet is set. It is a complete packet
174                */
175               prev_b0 = 0;
176               got_packet = 1;
177             }
178         }
179       else if (rx_comp->index & VMXNET3_RXCI_EOP)
180         {
181           /* end of segment */
182           if (prev_b0)
183             {
184               prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
185               prev_b0->next_buffer = bi0;
186               hb->total_length_not_including_first_buffer +=
187                 b0->current_length;
188               prev_b0 = 0;
189               got_packet = 1;
190             }
191           else
192             {
193               /* EOP without SOP, error */
194               vlib_error_count (vm, node->node_index,
195                                 VMXNET3_INPUT_ERROR_RX_PACKET_NO_SOP, 1);
196               vlib_buffer_free_one (vm, bi0);
197               if (hb && vlib_get_buffer_index (vm, hb) != bi0)
198                 {
199                   vlib_buffer_free_one (vm, vlib_get_buffer_index (vm, hb));
200                   hb = 0;
201                 }
202               continue;
203             }
204         }
205       else if (prev_b0)         // !sop && !eop
206         {
207           /* mid chain */
208           ASSERT (rxd->flags & VMXNET3_RXF_BTYPE);
209           prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
210           prev_b0->next_buffer = bi0;
211           prev_b0 = b0;
212           hb->total_length_not_including_first_buffer += b0->current_length;
213         }
214       else
215         {
216           ASSERT (0);
217         }
218
219       n_rx_bytes += b0->current_length;
220
221       if (got_packet)
222         {
223           ethernet_header_t *e = (ethernet_header_t *) hb->data;
224
225           if (PREDICT_FALSE (vd->per_interface_next_index != ~0))
226             {
227               next_index = vd->per_interface_next_index;
228               known_next = 1;
229             }
230
231           if (PREDICT_FALSE
232               (vnet_device_input_have_features (vd->sw_if_index)))
233             {
234               vnet_feature_start_device_input_x1 (vd->sw_if_index,
235                                                   &next_index, hb);
236               known_next = 1;
237             }
238
239           if (PREDICT_FALSE (known_next))
240             {
241               next[0] = next_index;
242             }
243           else
244             {
245               if (ethernet_frame_is_tagged (e->type))
246                 next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
247               else
248                 {
249                   if (rx_comp->flags & VMXNET3_RXCF_IP4)
250                     {
251                       next[0] = VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT;
252                       hb->flags |= VNET_BUFFER_F_IS_IP4;
253                       vlib_buffer_advance (hb,
254                                            device_input_next_node_advance
255                                            [next[0]]);
256                     }
257                   else if (rx_comp->flags & VMXNET3_RXCF_IP6)
258                     {
259                       next[0] = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
260                       hb->flags |= VNET_BUFFER_F_IS_IP6;
261                       vlib_buffer_advance (hb,
262                                            device_input_next_node_advance
263                                            [next[0]]);
264                     }
265                   else
266                     {
267                       next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
268                     }
269                 }
270             }
271
272           n_rx_packets++;
273           next++;
274           bi++;
275           hb = 0;
276           got_packet = 0;
277         }
278     }
279
280   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
281     {
282       u32 n_left = n_rx_packets;
283
284       bi = buffer_indices;
285       next = nexts;
286       while (n_trace && n_left)
287         {
288           vlib_buffer_t *b;
289           vmxnet3_input_trace_t *tr;
290
291           b = vlib_get_buffer (vm, bi[0]);
292           vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
293           tr = vlib_add_trace (vm, node, b, sizeof (*tr));
294           tr->next_index = next[0];
295           tr->hw_if_index = vd->hw_if_index;
296           tr->buffer = *b;
297
298           n_trace--;
299           n_left--;
300           bi++;
301           next++;
302         }
303       vlib_set_trace_count (vm, node, n_trace);
304     }
305
306   if (PREDICT_TRUE (n_rx_packets))
307     {
308       vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
309                                    n_rx_packets);
310       vlib_increment_combined_counter
311         (vnm->interface_main.combined_sw_if_counters +
312          VNET_INTERFACE_COUNTER_RX, thread_index,
313          vd->hw_if_index, n_rx_packets, n_rx_bytes);
314     }
315
316   error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
317   if (PREDICT_FALSE (error != 0))
318     {
319       vlib_error_count (vm, node->node_index,
320                         VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
321     }
322   error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
323   if (PREDICT_FALSE (error != 0))
324     {
325       vlib_error_count (vm, node->node_index,
326                         VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
327     }
328
329   return n_rx_packets;
330 }
331
332 VLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
333                                    vlib_node_runtime_t * node,
334                                    vlib_frame_t * frame)
335 {
336   u32 n_rx = 0;
337   vmxnet3_main_t *vmxm = &vmxnet3_main;
338   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
339   vnet_device_and_queue_t *dq;
340
341   foreach_device_and_queue (dq, rt->devices_and_queues)
342   {
343     vmxnet3_device_t *vd;
344     vd = vec_elt_at_index (vmxm->devices, dq->dev_instance);
345     if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
346       continue;
347     n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, dq->queue_id);
348   }
349   return n_rx;
350 }
351
352 #ifndef CLIB_MARCH_VARIANT
353 /* *INDENT-OFF* */
354 VLIB_REGISTER_NODE (vmxnet3_input_node) = {
355   .name = "vmxnet3-input",
356   .sibling_of = "device-input",
357   .format_trace = format_vmxnet3_input_trace,
358   .type = VLIB_NODE_TYPE_INPUT,
359   .state = VLIB_NODE_STATE_DISABLED,
360   .n_errors = VMXNET3_INPUT_N_ERROR,
361   .error_strings = vmxnet3_input_error_strings,
362 };
363 #endif
364
365 /* *INDENT-ON* */
366
367 /*
368  * fd.io coding-style-patch-verification: ON
369  *
370  * Local Variables:
371  * eval: (c-set-style "gnu")
372  * End:
373  */