a0b9cbca5782a287003b953680ed1fb8f9053784
[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   u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
92   u8 known_next = 0;
93
94   rxq = vec_elt_at_index (vd->rxqs, qid);
95   comp_ring = &rxq->rx_comp_ring;
96   bi = buffer_indices;
97   next = nexts;
98   while (PREDICT_TRUE (n_rx_packets < VLIB_FRAME_SIZE) &&
99          (comp_ring->gen ==
100           (rxq->rx_comp[comp_ring->next].flags & VMXNET3_RXCF_GEN)))
101     {
102       vlib_buffer_t *b0;
103       u32 bi0;
104
105       comp_idx = comp_ring->next;
106       rx_comp = &rxq->rx_comp[comp_idx];
107
108       rid = vmxnet3_find_rid (vd, rx_comp);
109       ring = &rxq->rx_ring[rid];
110
111       if (PREDICT_TRUE (ring->fill >= 1))
112         ring->fill--;
113       else
114         {
115           vlib_error_count (vm, node->node_index,
116                             VMXNET3_INPUT_ERROR_NO_BUFFER, 1);
117           break;
118         }
119
120       vmxnet3_rx_comp_ring_advance_next (rxq);
121       desc_idx = rx_comp->index & VMXNET3_RXC_INDEX;
122       ring->consume = desc_idx;
123
124       bi0 = ring->bufs[desc_idx];
125       ring->bufs[desc_idx] = ~0;
126
127       b0 = vlib_get_buffer (vm, bi0);
128       vnet_buffer (b0)->sw_if_index[VLIB_RX] = vd->sw_if_index;
129       vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
130       vnet_buffer (b0)->feature_arc_index = 0;
131       b0->current_length = rx_comp->len & VMXNET3_RXCL_LEN_MASK;
132       b0->current_data = 0;
133       b0->total_length_not_including_first_buffer = 0;
134       b0->next_buffer = 0;
135       b0->flags = 0;
136       b0->error = 0;
137       b0->current_config_index = 0;
138       ASSERT (b0->current_length != 0);
139
140       if (rx_comp->index & VMXNET3_RXCI_SOP)
141         {
142           /* start segment */
143           hb = b0;
144           bi[0] = bi0;
145           if (!(rx_comp->index & VMXNET3_RXCI_EOP))
146             {
147               hb->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
148               b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
149               prev_b0 = b0;
150             }
151           else
152             {
153               /*
154                * Both start and end of packet is set. It is a complete packet
155                */
156               prev_b0 = 0;
157             }
158         }
159       else if (rx_comp->index & VMXNET3_RXCI_EOP)
160         {
161           /* end of segment */
162           if (prev_b0)
163             {
164               prev_b0->next_buffer = bi0;
165               prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
166               hb->total_length_not_including_first_buffer +=
167                 b0->current_length;
168               prev_b0 = 0;      // Get next packet
169             }
170           else
171             {
172               /* EOP without SOP, error */
173               hb = 0;
174               vlib_error_count (vm, node->node_index,
175                                 VMXNET3_INPUT_ERROR_RX_PACKET, 1);
176               vlib_buffer_free_one (vm, bi0);
177               continue;
178             }
179         }
180       else if (prev_b0)         // !sop && !eop
181         {
182           /* mid chain */
183           b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
184           prev_b0->next_buffer = bi0;
185           prev_b0 = b0;
186           hb->total_length_not_including_first_buffer += b0->current_length;
187         }
188       else
189         {
190           ASSERT (0);
191         }
192
193       n_rx_bytes += b0->current_length;
194
195       if (!prev_b0)
196         {
197           ethernet_header_t *e = (ethernet_header_t *) hb->data;
198
199           if (PREDICT_FALSE (vd->per_interface_next_index != ~0))
200             {
201               next_index = vd->per_interface_next_index;
202               known_next = 1;
203             }
204
205           if (PREDICT_FALSE
206               (vnet_device_input_have_features (vd->sw_if_index)))
207             {
208               vnet_feature_start_device_input_x1 (vd->sw_if_index,
209                                                   &next_index, hb);
210               known_next = 1;
211             }
212
213           if (PREDICT_FALSE (known_next))
214             {
215               next[0] = next_index;
216             }
217           else
218             {
219               if (ethernet_frame_is_tagged (e->type))
220                 next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
221               else
222                 {
223                   if (rx_comp->flags & VMXNET3_RXCF_IP4)
224                     {
225                       next[0] = VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT;
226                       hb->flags |= VNET_BUFFER_F_IS_IP4;
227                       vlib_buffer_advance (hb,
228                                            device_input_next_node_advance
229                                            [next[0]]);
230                     }
231                   else if (rx_comp->flags & VMXNET3_RXCF_IP6)
232                     {
233                       next[0] = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
234                       hb->flags |= VNET_BUFFER_F_IS_IP6;
235                       vlib_buffer_advance (hb,
236                                            device_input_next_node_advance
237                                            [next[0]]);
238                     }
239                   else
240                     {
241                       next[0] = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
242                     }
243                 }
244             }
245
246           n_rx_packets++;
247           next++;
248           bi++;
249           hb = 0;
250         }
251     }
252
253   if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
254     {
255       u32 n_left = n_rx_packets;
256
257       bi = buffer_indices;
258       next = nexts;
259       while (n_trace && n_left)
260         {
261           vlib_buffer_t *b;
262           vmxnet3_input_trace_t *tr;
263
264           b = vlib_get_buffer (vm, bi[0]);
265           vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
266           tr = vlib_add_trace (vm, node, b, sizeof (*tr));
267           tr->next_index = next[0];
268           tr->hw_if_index = vd->hw_if_index;
269           tr->buffer = *b;
270
271           n_trace--;
272           n_left--;
273           bi++;
274           next++;
275         }
276       vlib_set_trace_count (vm, node, n_trace);
277     }
278
279   if (PREDICT_TRUE (n_rx_packets))
280     {
281       clib_error_t *error;
282
283       vlib_buffer_enqueue_to_next (vm, node, buffer_indices, nexts,
284                                    n_rx_packets);
285       vlib_increment_combined_counter
286         (vnm->interface_main.combined_sw_if_counters +
287          VNET_INTERFACE_COUNTER_RX, thread_index,
288          vd->hw_if_index, n_rx_packets, n_rx_bytes);
289
290       error = vmxnet3_rxq_refill_ring0 (vm, vd, rxq);
291       if (PREDICT_FALSE (error != 0))
292         {
293           vlib_error_count (vm, node->node_index,
294                             VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
295         }
296       error = vmxnet3_rxq_refill_ring1 (vm, vd, rxq);
297       if (PREDICT_FALSE (error != 0))
298         {
299           vlib_error_count (vm, node->node_index,
300                             VMXNET3_INPUT_ERROR_BUFFER_ALLOC, 1);
301         }
302     }
303
304   return n_rx_packets;
305 }
306
307 VLIB_NODE_FN (vmxnet3_input_node) (vlib_main_t * vm,
308                                    vlib_node_runtime_t * node,
309                                    vlib_frame_t * frame)
310 {
311   u32 n_rx = 0;
312   vmxnet3_main_t *vmxm = &vmxnet3_main;
313   vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
314   vnet_device_and_queue_t *dq;
315
316   foreach_device_and_queue (dq, rt->devices_and_queues)
317   {
318     vmxnet3_device_t *vd;
319     vd = vec_elt_at_index (vmxm->devices, dq->dev_instance);
320     if ((vd->flags & VMXNET3_DEVICE_F_ADMIN_UP) == 0)
321       continue;
322     n_rx += vmxnet3_device_input_inline (vm, node, frame, vd, dq->queue_id);
323   }
324   return n_rx;
325 }
326
327 #ifndef CLIB_MARCH_VARIANT
328 /* *INDENT-OFF* */
329 VLIB_REGISTER_NODE (vmxnet3_input_node) = {
330   .name = "vmxnet3-input",
331   .sibling_of = "device-input",
332   .format_trace = format_vmxnet3_input_trace,
333   .type = VLIB_NODE_TYPE_INPUT,
334   .state = VLIB_NODE_STATE_DISABLED,
335   .n_errors = VMXNET3_INPUT_N_ERROR,
336   .error_strings = vmxnet3_input_error_strings,
337 };
338 #endif
339
340 /* *INDENT-ON* */
341
342 /*
343  * fd.io coding-style-patch-verification: ON
344  *
345  * Local Variables:
346  * eval: (c-set-style "gnu")
347  * End:
348  */