af_xdp: workaround kernel race between poll() and sendmsg()
[vpp.git] / src / plugins / af_xdp / 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 <poll.h>
19 #include <vlib/vlib.h>
20 #include <vlib/unix/unix.h>
21 #include <vlib/pci/pci.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/devices/devices.h>
24 #include <vnet/interface/rx_queue_funcs.h>
25 #include "af_xdp.h"
26
27 #define foreach_af_xdp_input_error                                            \
28   _ (SYSCALL_REQUIRED, "syscall required")                                    \
29   _ (SYSCALL_FAILURES, "syscall failures")
30
31 typedef enum
32 {
33 #define _(f,s) AF_XDP_INPUT_ERROR_##f,
34   foreach_af_xdp_input_error
35 #undef _
36     AF_XDP_INPUT_N_ERROR,
37 } af_xdp_input_error_t;
38
39 static __clib_unused char *af_xdp_input_error_strings[] = {
40 #define _(n,s) s,
41   foreach_af_xdp_input_error
42 #undef _
43 };
44
45 static_always_inline void
46 af_xdp_device_input_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
47                            u32 n_left, const u32 * bi, u32 next_index,
48                            u32 hw_if_index)
49 {
50   u32 n_trace = vlib_get_trace_count (vm, node);
51
52   if (PREDICT_TRUE (0 == n_trace))
53     return;
54
55   while (n_trace && n_left)
56     {
57       vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
58       if (PREDICT_TRUE
59           (vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 0)))
60         {
61           af_xdp_input_trace_t *tr =
62             vlib_add_trace (vm, node, b, sizeof (*tr));
63           tr->next_index = next_index;
64           tr->hw_if_index = hw_if_index;
65           n_trace--;
66         }
67       n_left--;
68       bi++;
69     }
70
71   vlib_set_trace_count (vm, node, n_trace);
72 }
73
74 static_always_inline void
75 af_xdp_device_input_refill_db (vlib_main_t * vm,
76                                const vlib_node_runtime_t * node,
77                                af_xdp_device_t * ad, af_xdp_rxq_t * rxq,
78                                const u32 n_alloc)
79 {
80   xsk_ring_prod__submit (&rxq->fq, n_alloc);
81
82   if (AF_XDP_RXQ_MODE_INTERRUPT == rxq->mode ||
83       !xsk_ring_prod__needs_wakeup (&rxq->fq))
84     return;
85
86   vlib_error_count (vm, node->node_index, AF_XDP_INPUT_ERROR_SYSCALL_REQUIRED,
87                     1);
88
89   if (clib_spinlock_trylock_if_init (&rxq->syscall_lock))
90     {
91       struct pollfd fd = { .fd = rxq->xsk_fd, .events = POLLIN | POLLOUT };
92       int ret = poll (&fd, 1, 0);
93       clib_spinlock_unlock_if_init (&rxq->syscall_lock);
94       if (PREDICT_FALSE (ret < 0))
95         {
96           /* something bad is happening */
97           vlib_error_count (vm, node->node_index,
98                             AF_XDP_INPUT_ERROR_SYSCALL_FAILURES, 1);
99           af_xdp_device_error (ad, "rx poll() failed");
100         }
101     }
102 }
103
104 static_always_inline void
105 af_xdp_device_input_refill (vlib_main_t * vm,
106                             const vlib_node_runtime_t * node,
107                             af_xdp_device_t * ad, af_xdp_rxq_t * rxq,
108                             const int copy)
109 {
110   __u64 *fill;
111   const u32 size = rxq->fq.size;
112   const u32 mask = size - 1;
113   u32 bis[VLIB_FRAME_SIZE], *bi = bis;
114   u32 n_alloc, n, n_wrap;
115   u32 idx = 0;
116
117   ASSERT (mask == rxq->fq.mask);
118
119   /* do not enqueue more packet than ring space */
120   n_alloc = xsk_prod_nb_free (&rxq->fq, 16);
121   /* do not bother to allocate if too small */
122   if (n_alloc < 16)
123     return;
124
125   n_alloc = clib_min (n_alloc, ARRAY_LEN (bis));
126   n_alloc = vlib_buffer_alloc_from_pool (vm, bis, n_alloc, ad->pool);
127   n = xsk_ring_prod__reserve (&rxq->fq, n_alloc, &idx);
128   ASSERT (n == n_alloc);
129
130   fill = xsk_ring_prod__fill_addr (&rxq->fq, idx);
131   n = clib_min (n_alloc, size - (idx & mask));
132   n_wrap = n_alloc - n;
133
134   /*
135    * Note about headroom: for some reasons, there seem to be a discrepency
136    * between 0-copy and copy mode. See
137    * src/plugins/af_xdp/device.c:af_xdp_create_queue()
138    */
139 #define bi2addr(bi)                                                           \
140   (((bi) << CLIB_LOG2_CACHE_LINE_BYTES) + (copy ? XDP_PACKET_HEADROOM : 0))
141
142 wrap_around:
143
144   while (n >= 8)
145     {
146 #ifdef CLIB_HAVE_VEC256
147       u64x4 b0 = u64x4_from_u32x4 (*(u32x4u *) (bi + 0));
148       u64x4 b1 = u64x4_from_u32x4 (*(u32x4u *) (bi + 4));
149       *(u64x4u *) (fill + 0) = bi2addr (b0);
150       *(u64x4u *) (fill + 4) = bi2addr (b1);
151 #else
152       fill[0] = bi2addr (bi[0]);
153       fill[1] = bi2addr (bi[1]);
154       fill[2] = bi2addr (bi[2]);
155       fill[3] = bi2addr (bi[3]);
156       fill[4] = bi2addr (bi[4]);
157       fill[5] = bi2addr (bi[5]);
158       fill[6] = bi2addr (bi[6]);
159       fill[7] = bi2addr (bi[7]);
160 #endif
161       fill += 8;
162       bi += 8;
163       n -= 8;
164     }
165
166   while (n >= 1)
167     {
168       fill[0] = bi2addr (bi[0]);
169       fill += 1;
170       bi += 1;
171       n -= 1;
172     }
173
174   if (n_wrap)
175     {
176       fill = xsk_ring_prod__fill_addr (&rxq->fq, 0);
177       n = n_wrap;
178       n_wrap = 0;
179       goto wrap_around;
180     }
181
182   af_xdp_device_input_refill_db (vm, node, ad, rxq, n_alloc);
183 }
184
185 static_always_inline void
186 af_xdp_device_input_ethernet (vlib_main_t * vm, vlib_node_runtime_t * node,
187                               const u32 next_index, const u32 sw_if_index,
188                               const u32 hw_if_index)
189 {
190   vlib_next_frame_t *nf;
191   vlib_frame_t *f;
192   ethernet_input_frame_t *ef;
193
194   if (PREDICT_FALSE (VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT != next_index))
195     return;
196
197   nf =
198     vlib_node_runtime_get_next_frame (vm, node,
199                                       VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT);
200   f = vlib_get_frame (vm, nf->frame);
201   f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX;
202
203   ef = vlib_frame_scalar_args (f);
204   ef->sw_if_index = sw_if_index;
205   ef->hw_if_index = hw_if_index;
206 }
207
208 static_always_inline u32
209 af_xdp_device_input_bufs (vlib_main_t * vm, const af_xdp_device_t * ad,
210                           af_xdp_rxq_t * rxq, u32 * bis, const u32 n_rx,
211                           vlib_buffer_t * bt, u32 idx, const int copy)
212 {
213   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
214   u16 lens[VLIB_FRAME_SIZE], *len = lens;
215   const u32 mask = rxq->rx.mask;
216   u32 n = n_rx, *bi = bis, bytes = 0;
217
218 #define addr2bi(addr)                                                         \
219   (((addr) - (copy ? XDP_PACKET_HEADROOM : 0)) >> CLIB_LOG2_CACHE_LINE_BYTES)
220
221   while (n >= 1)
222     {
223       const struct xdp_desc *desc = xsk_ring_cons__rx_desc (&rxq->rx, idx);
224       bi[0] = addr2bi (xsk_umem__extract_addr (desc->addr));
225       ASSERT (vlib_buffer_is_known (vm, bi[0]) ==
226               VLIB_BUFFER_KNOWN_ALLOCATED);
227       len[0] = desc->len;
228       idx = (idx + 1) & mask;
229       bi += 1;
230       len += 1;
231       n -= 1;
232     }
233
234   vlib_get_buffers (vm, bis, bufs, n_rx);
235
236   n = n_rx;
237   len = lens;
238
239   while (n >= 8)
240     {
241       vlib_prefetch_buffer_header (b[4], LOAD);
242       vlib_buffer_copy_template (b[0], bt);
243       bytes += b[0]->current_length = len[0];
244
245       vlib_prefetch_buffer_header (b[5], LOAD);
246       vlib_buffer_copy_template (b[1], bt);
247       bytes += b[1]->current_length = len[1];
248
249       vlib_prefetch_buffer_header (b[6], LOAD);
250       vlib_buffer_copy_template (b[2], bt);
251       bytes += b[2]->current_length = len[2];
252
253       vlib_prefetch_buffer_header (b[7], LOAD);
254       vlib_buffer_copy_template (b[3], bt);
255       bytes += b[3]->current_length = len[3];
256
257       b += 4;
258       len += 4;
259       n -= 4;
260     }
261
262   while (n >= 1)
263     {
264       vlib_buffer_copy_template (b[0], bt);
265       bytes += b[0]->current_length = len[0];
266       b += 1;
267       len += 1;
268       n -= 1;
269     }
270
271   xsk_ring_cons__release (&rxq->rx, n_rx);
272   return bytes;
273 }
274
275 static_always_inline uword
276 af_xdp_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
277                             vlib_frame_t * frame, af_xdp_device_t * ad,
278                             u16 qid, const int copy)
279 {
280   vnet_main_t *vnm = vnet_get_main ();
281   af_xdp_rxq_t *rxq = vec_elt_at_index (ad->rxqs, qid);
282   vlib_buffer_t bt;
283   u32 next_index, *to_next, n_left_to_next;
284   u32 n_rx_packets, n_rx_bytes;
285   u32 idx;
286
287   n_rx_packets = xsk_ring_cons__peek (&rxq->rx, VLIB_FRAME_SIZE, &idx);
288
289   if (PREDICT_FALSE (0 == n_rx_packets))
290     goto refill;
291
292   vlib_buffer_copy_template (&bt, ad->buffer_template);
293   next_index = ad->per_interface_next_index;
294   if (PREDICT_FALSE (vnet_device_input_have_features (ad->sw_if_index)))
295     vnet_feature_start_device_input_x1 (ad->sw_if_index, &next_index, &bt);
296
297   vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
298
299   n_rx_bytes =
300     af_xdp_device_input_bufs (vm, ad, rxq, to_next, n_rx_packets, &bt, idx,
301                               copy);
302   af_xdp_device_input_ethernet (vm, node, next_index, ad->sw_if_index,
303                                 ad->hw_if_index);
304
305   vlib_put_next_frame (vm, node, next_index, n_left_to_next - n_rx_packets);
306
307   af_xdp_device_input_trace (vm, node, n_rx_packets, to_next, next_index,
308                              ad->hw_if_index);
309
310   vlib_increment_combined_counter
311     (vnm->interface_main.combined_sw_if_counters +
312      VNET_INTERFACE_COUNTER_RX, vm->thread_index,
313      ad->hw_if_index, n_rx_packets, n_rx_bytes);
314
315 refill:
316   af_xdp_device_input_refill (vm, node, ad, rxq, copy);
317
318   return n_rx_packets;
319 }
320
321 VLIB_NODE_FN (af_xdp_input_node) (vlib_main_t * vm,
322                                   vlib_node_runtime_t * node,
323                                   vlib_frame_t * frame)
324 {
325   u32 n_rx = 0;
326   af_xdp_main_t *am = &af_xdp_main;
327   vnet_hw_if_rxq_poll_vector_t *p,
328     *pv = vnet_hw_if_get_rxq_poll_vector (vm, node);
329
330   vec_foreach (p, pv)
331     {
332       af_xdp_device_t *ad = vec_elt_at_index (am->devices, p->dev_instance);
333       if ((ad->flags & AF_XDP_DEVICE_F_ADMIN_UP) == 0)
334         continue;
335       if (PREDICT_TRUE (ad->flags & AF_XDP_DEVICE_F_ZEROCOPY))
336         n_rx += af_xdp_device_input_inline (vm, node, frame, ad, p->queue_id,
337                                             /* copy */ 0);
338       else
339         n_rx += af_xdp_device_input_inline (vm, node, frame, ad, p->queue_id,
340                                             /* copy */ 1);
341     }
342
343   return n_rx;
344 }
345
346 /* *INDENT-OFF* */
347 VLIB_REGISTER_NODE (af_xdp_input_node) = {
348   .name = "af_xdp-input",
349   .sibling_of = "device-input",
350   .format_trace = format_af_xdp_input_trace,
351   .type = VLIB_NODE_TYPE_INPUT,
352   .state = VLIB_NODE_STATE_DISABLED,
353   .n_errors = AF_XDP_INPUT_N_ERROR,
354   .error_strings = af_xdp_input_error_strings,
355   .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED,
356 };
357 /* *INDENT-ON* */
358
359 /*
360  * fd.io coding-style-patch-verification: ON
361  *
362  * Local Variables:
363  * eval: (c-set-style "gnu")
364  * End:
365  */