vmxnet3 device driver
[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, "Rx packet error") \
29   _(NO_BUFFER, "Rx no buffer error")
30
31 typedef enum
32 {
33 #define _(f,s) VMXNET3_INPUT_ERROR_##f,
34   foreach_vmxnet3_input_error
35 #undef _
36     VMXNET3_INPUT_N_ERROR,
37 } vmxnet3_input_error_t;
38
39 static __clib_unused char *vmxnet3_input_error_strings[] = {
40 #define _(n,s) s,
41   foreach_vmxnet3_input_error
42 #undef _
43 };
44
45 static_always_inline u16
46 vmxnet3_find_rid (vmxnet3_device_t * vd, vmxnet3_rx_comp * rx_comp)
47 {
48   u32 rid;
49
50   // rid is bits 16-25 (10 bits number)
51   rid = rx_comp->index & (0xffffffff >> 6);
52   rid >>= 16;
53   if ((rid >= vd->num_rx_queues) && (rid < (vd->num_rx_queues << 1)))
54     return 1;
55   else
56     return 0;
57 }
58
59 static_always_inline void
60 vmxnet3_rx_comp_ring_advance_next (vmxnet3_rxq_t * rxq)
61 {
62   vmxnet3_rx_comp_ring *comp_ring = &rxq->rx_comp_ring;
63
64   comp_ring->next++;
65   if (PREDICT_FALSE (comp_ring->next == rxq->size))
66     {
67       comp_ring->next = 0;
68       comp_ring->gen ^= VMXNET3_RXCF_GEN;
69     }
70 }
71
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,
75                              u16 qid)
76 {
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;
81   u32 comp_idx;
82   u32 desc_idx;
83   vmxnet3_rxq_t *rxq;
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;
89   u16 rid;
90   vlib_buffer_t *prev_b0 = 0, *hb = 0;
91
92   rxq = vec_elt_at_index (vd->rxqs, qid);
93   comp_ring = &rxq->rx_comp_ring;
94   bi = buffer_indices;
95   next = nexts;
96   while (comp_ring->gen ==
97          (rxq->rx_comp[comp_ring->next].flags & VMXNET3_RXCF_GEN))
98     {
99       vlib_buffer_t *b0;
100
101       comp_idx = comp_ring->next;
102       rx_comp = &rxq->rx_comp[comp_idx];
103
104       rid = vmxnet3_find_rid (vd, rx_comp);
105       ring = &rxq->rx_ring[rid];
106
107       if (PREDICT_TRUE (ring->fill >= 1))
108         ring->fill--;
109       else
110         {
111           vlib_error_count (vm, node->node_index,
112                             VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
113           break;
114         }
115
116       vmxnet3_rx_comp_ring_advance_next (rxq);
117       desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
118       ring->consume = desc_idx;
119
120       bi[0] = ring->bufs[desc_idx];
121       ring->bufs[desc_idx] = ~0;
122
123       b0 = vlib_get_buffer (vm, bi[0]);
124       vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
125       vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
126       b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
127       b0->current_data = 0;
128       b0->total_length_not_including_first_buffer = 0;
129       b0->next_buffer = 0;
130       b0->flags = 0;
131       b0->error = 0;
132       ASSERT (b0->current_length != 0);
133
134       if (rx_comp->index & VMXNET3_RXCI_SOP)
135         {
136           /* start segment */
137           hb = b0;
138           if (!(rx_comp->index & VMXNET3_RXCI_EOP))
139             {
140               hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
141               b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
142               prev_b0 = b0;
143             }
144           else
145             {
146               /*
147                * Both start and end of packet is set. It is a complete packet
148                */
149               prev_b0 = 0;
150             }
151
152         }
153       else if (rx_comp->index & VMXNET3_RXCI_EOP)
154         {
155           /* end of segment */
156           if (prev_b0)
157             {
158               prev_b0->next_buffer = bi[0];
159               prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
160               hb->total_length_not_including_first_buffer +=
161                 b0->current_length;
162               prev_b0 = 0;      // Get next packet
163             }
164           else
165             {
166               /* EOP without SOP, error */
167               hb = 0;
168               bi++;
169               vlib_error_count (vm, node->node_index,
170                                 VMXNET3_INPUT_ERROR_RX_PACKET, 1);
171               vlib_buffer_free_one (vm, bi[0]);
172               continue;
173             }
174         }
175       else if (prev_b0)         // !sop && !eop
176         {
177           /* mid chain */
178           b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
179           prev_b0->next_buffer = bi[0];
180           prev_b0 = b0;
181           hb->total_length_not_including_first_buffer += b0->current_length;
182         }
183       else
184         {
185           ASSERT (0);
186         }
187
188       bi++;
189       n_rx_bytes += b0->current_length;
190
191       if (!prev_b0)
192         {
193           next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
194           n_rx_packets++;
195           next++;
196           hb = 0;
197         }
198     }
199
200   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
201     {
202       u32 n_left = n_rx_packets;
203
204       bi = buffer_indices;
205       next = nexts;
206       while (n_trace && n_left)
207         {
208           vlib_buffer_t *b;
209           vmxnet3_input_trace_t *tr;
210
211           b = vlib_get_buffer (vm, bi[0]);
212           vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
213           tr = vlib_add_trace (vm, node, b, sizeof (*tr));
214           tr->next_index = next[0];
215           tr->hw_if_index = vd->hw_if_index;
216           tr->buffer = *b;
217
218           n_trace--;
219           n_left--;
220           bi++;
221           next++;
222         }
223       vlib_set_trace_count (vm, node, n_trace);
224     }
225
226   if (PREDICT_TRUE (n_rx_packets))
227     {
228       clib_error_t *error;
229
230       vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
231                                    n_rx_packets);
232       vlib_increment_combined_counter
233         (vnm->interface_main.combined_sw_if_counters +
234          VNET_INTERFACE_COUNTER_RX, thread_index,
235          vd->hw_if_index, n_rx_packets, n_rx_bytes);
236
237       error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
238       if (PREDICT_FALSE (error != 0))
239         {
240           vlib_error_count (vm, node->node_index,
241                             VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
242         }
243       error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
244       if (PREDICT_FALSE (error != 0))
245         {
246           vlib_error_count (vm, node->node_index,
247                             VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
248         }
249     }
250
251   return n_rx_packets;
252 }
253
254 VLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
255                                    vlib_node_runtime_t * node,
256                                    vlib_frame_t * frame)
257 {
258   u32 n_rx = 0;
259   vmxnet3_main_t *vmxm = &vmxnet3_main;
260   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
261   vnet_device_and_queue_t *dq;
262
263   foreach_device_and_queue (dq, rt->devices_and_queues)
264   {
265     vmxnet3_device_t *vd;
266     vd = vec_elt_at_index (vmxm->devices, dq->dev_instance);
267     if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
268       continue;
269     n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, dq->queue_id);
270   }
271   return n_rx;
272 }
273
274 #ifndef CLIB_MARCH_VARIANT
275 /* *INDENT-OFF* */
276 VLIB_REGISTER_NODE (vmxnet3_input_node) = {
277   .name = "vmxnet3-input",
278   .sibling_of = "device-input",
279   .format_trace = format_vmxnet3_input_trace,
280   .type = VLIB_NODE_TYPE_INPUT,
281   .state = VLIB_NODE_STATE_DISABLED,
282   .n_errors = VMXNET3_INPUT_N_ERROR,
283   .error_strings = vmxnet3_input_error_strings,
284 };
285 #endif
286
287 /* *INDENT-ON* */
288
289 /*
290  * fd.io coding-style-patch-verification: ON
291  *
292  * Local Variables:
293  * eval: (c-set-style "gnu")
294  * End:
295  */