FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
input.c
Go to the documentation of this file.
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 <rdma/rdma.h>
25 
26 #define foreach_rdma_input_error \
27  _(BUFFER_ALLOC, "buffer alloc error")
28 
29 typedef enum
30 {
31 #define _(f,s) RDMA_INPUT_ERROR_##f,
33 #undef _
36 
37 static __clib_unused char *rdma_input_error_strings[] = {
38 #define _(n,s) s,
40 #undef _
41 };
42 
45  rdma_rxq_t * rxq)
46 {
47  u32 n_alloc, n;
48  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
49  struct ibv_recv_wr wr[VLIB_FRAME_SIZE], *w = wr;
50  struct ibv_sge sge[VLIB_FRAME_SIZE], *s = sge;
51  u32 slot = rxq->tail & (rxq->size - 1);
52 
53  /* do not enqueue more packet than ring space */
54  n_alloc = clib_min (VLIB_FRAME_SIZE, rxq->size - (rxq->tail - rxq->head));
55 
56  /* do not bother to allocate if too small */
57  if (n_alloc < 16)
58  return;
59 
60  /* avoid wrap-around logic in core loop */
61  n_alloc = clib_min (n_alloc, rxq->size - slot);
62 
63  n = n_alloc =
64  vlib_buffer_alloc_to_ring_from_pool (vm, rxq->bufs, slot, rxq->size,
65  n_alloc, rd->pool);
66 
67  /* if ring is full or allocation error, do nothing */
68  if (PREDICT_FALSE (0 == n_alloc))
69  return;
70 
71  vlib_get_buffers (vm, &rxq->bufs[slot], bufs, n_alloc);
72 
73  while (n >= 4)
74  {
75  if (PREDICT_TRUE (n >= 8))
76  {
77  CLIB_PREFETCH (&s[4 + 0], 4 * sizeof (s[0]), STORE);
78  CLIB_PREFETCH (&w[4 + 0], 4 * sizeof (w[0]), STORE);
79  }
80 
81  s[0].addr = vlib_buffer_get_va (b[0]);
82  s[0].length = vlib_buffer_get_default_data_size (vm);
83  s[0].lkey = rd->lkey;
84 
85  s[1].addr = vlib_buffer_get_va (b[1]);
86  s[1].length = vlib_buffer_get_default_data_size (vm);
87  s[1].lkey = rd->lkey;
88 
89  s[2].addr = vlib_buffer_get_va (b[2]);
90  s[2].length = vlib_buffer_get_default_data_size (vm);
91  s[2].lkey = rd->lkey;
92 
93  s[3].addr = vlib_buffer_get_va (b[3]);
94  s[3].length = vlib_buffer_get_default_data_size (vm);
95  s[3].lkey = rd->lkey;
96 
97  w[0].next = &w[0] + 1;
98  w[0].sg_list = &s[0];
99  w[0].num_sge = 1;
100 
101  w[1].next = &w[1] + 1;
102  w[1].sg_list = &s[1];
103  w[1].num_sge = 1;
104 
105  w[2].next = &w[2] + 1;
106  w[2].sg_list = &s[2];
107  w[2].num_sge = 1;
108 
109  w[3].next = &w[3] + 1;
110  w[3].sg_list = &s[3];
111  w[3].num_sge = 1;
112 
113  s += 4;
114  w += 4;
115  b += 4;
116  n -= 4;
117  }
118 
119  while (n >= 1)
120  {
121  s[0].addr = vlib_buffer_get_va (b[0]);
122  s[0].length = vlib_buffer_get_default_data_size (vm);
123  s[0].lkey = rd->lkey;
124 
125  w[0].next = &w[0] + 1;
126  w[0].sg_list = &s[0];
127  w[0].num_sge = 1;
128 
129  s += 1;
130  w += 1;
131  b += 1;
132  n -= 1;
133  }
134 
135  w[-1].next = 0; /* fix next pointer in WR linked-list last item */
136 
137  n = n_alloc;
138  if (ibv_post_wq_recv (rxq->wq, wr, &w) != 0)
139  {
140  n = w - wr;
141  vlib_buffer_free_from_ring (vm, rxq->bufs, slot + n, rxq->size,
142  n_alloc - n);
143  }
144 
145  rxq->tail += n;
146 }
147 
150  const rdma_device_t * rd, u32 n_left, const u32 * bi,
151  u32 next_index)
152 {
153  u32 n_trace, i;
154 
155  if (PREDICT_TRUE (0 == (n_trace = vlib_get_trace_count (vm, node))))
156  return;
157 
158  i = 0;
159  while (n_trace && n_left)
160  {
161  vlib_buffer_t *b;
162  rdma_input_trace_t *tr;
163  b = vlib_get_buffer (vm, bi[0]);
164  vlib_trace_buffer (vm, node, next_index, b,
165  /* follow_chain */ 0);
166  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
167  tr->next_index = next_index;
168  tr->hw_if_index = rd->hw_if_index;
169 
170  /* next */
171  n_trace--;
172  n_left--;
173  bi++;
174  i++;
175  }
176  vlib_set_trace_count (vm, node, n_trace);
177 }
178 
181  const rdma_device_t * rd, u32 next_index)
182 {
183  vlib_next_frame_t *nf;
184  vlib_frame_t *f;
186 
188  return;
189 
190  nf =
193  f = vlib_get_frame (vm, nf->frame);
195  /* FIXME: f->flags |= ETH_INPUT_FRAME_F_IP4_CKSUM_OK; */
196 
197  ef = vlib_frame_scalar_args (f);
198  ef->sw_if_index = rd->sw_if_index;
199  ef->hw_if_index = rd->hw_if_index;
200 }
201 
204  u32 * next, u32 * bi, struct ibv_wc * wc,
205  u32 n_left_from, vlib_buffer_t * bt)
206 {
207  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
208  u32 n_rx_bytes[4] = { 0 };
209 
210  vlib_get_buffers (vm, bi, bufs, n_left_from);
211  ASSERT (bt->buffer_pool_index == bufs[0]->buffer_pool_index);
212 
213  while (n_left_from >= 4)
214  {
215  if (PREDICT_TRUE (n_left_from >= 8))
216  {
217  CLIB_PREFETCH (&wc[4 + 0], CLIB_CACHE_LINE_BYTES, LOAD);
218  CLIB_PREFETCH (&wc[4 + 1], CLIB_CACHE_LINE_BYTES, LOAD);
219  CLIB_PREFETCH (&wc[4 + 2], CLIB_CACHE_LINE_BYTES, LOAD);
220  CLIB_PREFETCH (&wc[4 + 3], CLIB_CACHE_LINE_BYTES, LOAD);
221  vlib_prefetch_buffer_header (b[4 + 0], STORE);
222  vlib_prefetch_buffer_header (b[4 + 1], STORE);
223  vlib_prefetch_buffer_header (b[4 + 2], STORE);
224  vlib_prefetch_buffer_header (b[4 + 3], STORE);
225  }
226 
227  vlib_buffer_copy_indices (next, bi, 4);
228 
229  vlib_buffer_copy_template (b[0], bt);
230  vlib_buffer_copy_template (b[1], bt);
231  vlib_buffer_copy_template (b[2], bt);
232  vlib_buffer_copy_template (b[3], bt);
233 
234  b[0]->current_length = wc[0].byte_len;
235  b[1]->current_length = wc[1].byte_len;
236  b[2]->current_length = wc[2].byte_len;
237  b[3]->current_length = wc[3].byte_len;
238 
239  n_rx_bytes[0] += wc[0].byte_len;
240  n_rx_bytes[1] += wc[1].byte_len;
241  n_rx_bytes[2] += wc[2].byte_len;
242  n_rx_bytes[3] += wc[3].byte_len;
243 
244  next += 4;
245  bi += 4;
246  b += 4;
247  wc += 4;
248  n_left_from -= 4;
249  }
250 
251  while (n_left_from >= 1)
252  {
253  vlib_buffer_copy_indices (next, bi, 1);
254  vlib_buffer_copy_template (b[0], bt);
255  b[0]->current_length = wc[0].byte_len;
256  n_rx_bytes[0] += wc[0].byte_len;
257 
258  next += 1;
259  bi += 1;
260  b += 1;
261  wc += 1;
262  n_left_from -= 1;
263  }
264 
265  return n_rx_bytes[0] + n_rx_bytes[1] + n_rx_bytes[2] + n_rx_bytes[3];
266 }
267 
270  vlib_frame_t * frame, rdma_device_t * rd, u16 qid)
271 {
272  vnet_main_t *vnm = vnet_get_main ();
273  rdma_rxq_t *rxq = vec_elt_at_index (rd->rxqs, qid);
274  struct ibv_wc wc[VLIB_FRAME_SIZE];
275  vlib_buffer_t bt;
276  u32 next_index, *to_next, n_left_to_next;
277  u32 n_rx_packets, n_rx_bytes;
278  u32 slot, n_tail;
279 
280  ASSERT (rxq->size >= VLIB_FRAME_SIZE && is_pow2 (rxq->size));
281  ASSERT (rxq->tail - rxq->head <= rxq->size);
282 
283  n_rx_packets = ibv_poll_cq (rxq->cq, VLIB_FRAME_SIZE, wc);
284  ASSERT (n_rx_packets <= rxq->tail - rxq->head);
285 
286  if (PREDICT_FALSE (n_rx_packets <= 0))
287  {
288  rdma_device_input_refill (vm, rd, rxq);
289  return 0;
290  }
291 
292  /* init buffer template */
293  clib_memset_u64 (&bt, 0,
295  template_end) / sizeof (u64));
296  vnet_buffer (&bt)->sw_if_index[VLIB_RX] = rd->sw_if_index;
297  vnet_buffer (&bt)->sw_if_index[VLIB_TX] = ~0;
298  bt.buffer_pool_index = rd->pool;
299  bt.ref_count = 1;
300 
301  /* update buffer template for input feature arcs if any */
302  next_index = rd->per_interface_next_index;
304  vnet_feature_start_device_input_x1 (rd->sw_if_index, &next_index, &bt);
305 
306  vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next);
307  ASSERT (n_rx_packets <= n_left_to_next);
308 
309  /*
310  * avoid wrap-around logic in core loop
311  * we requested VLIB_FRAME_SIZE packets and rxq->size >= VLIB_FRAME_SIZE
312  * => we can process all packets in 2 iterations max
313  */
314  slot = rxq->head & (rxq->size - 1);
315  n_tail = clib_min (n_rx_packets, rxq->size - slot);
316  n_rx_bytes =
317  rdma_device_input_bufs (vm, rd, &to_next[0], &rxq->bufs[slot], wc, n_tail,
318  &bt);
319  if (n_tail < n_rx_packets)
320  n_rx_bytes +=
321  rdma_device_input_bufs (vm, rd, &to_next[n_tail], &rxq->bufs[0],
322  &wc[n_tail], n_rx_packets - n_tail, &bt);
323  rdma_device_input_ethernet (vm, node, rd, next_index);
324 
325  vlib_put_next_frame (vm, node, next_index, n_left_to_next - n_rx_packets);
326 
327  rxq->head += n_rx_packets;
328 
329  rdma_device_input_trace (vm, node, rd, n_rx_packets, to_next, next_index);
330 
334  rd->hw_if_index, n_rx_packets, n_rx_bytes);
335 
336  rdma_device_input_refill (vm, rd, rxq);
337 
338  return n_rx_packets;
339 }
340 
344 {
345  u32 n_rx = 0;
346  rdma_main_t *rm = &rdma_main;
347  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
349 
351  {
352  rdma_device_t *rd;
353  rd = vec_elt_at_index (rm->devices, dq->dev_instance);
354  if (PREDICT_TRUE (rd->flags & RDMA_DEVICE_F_ADMIN_UP))
355  n_rx += rdma_device_input_inline (vm, node, frame, rd, dq->queue_id);
356  }
357  return n_rx;
358 }
359 
360 /* *INDENT-OFF* */
362  .name = "rdma-input",
364  .sibling_of = "device-input",
365  .format_trace = format_rdma_input_trace,
366  .type = VLIB_NODE_TYPE_INPUT,
367  .state = VLIB_NODE_STATE_DISABLED,
368  .n_errors = RDMA_INPUT_N_ERROR,
369  .error_strings = rdma_input_error_strings,
370 };
371 
372 /* *INDENT-ON* */
373 
374 
375 /*
376  * fd.io coding-style-patch-verification: ON
377  *
378  * Local Variables:
379  * eval: (c-set-style "gnu")
380  * End:
381  */
vnet_device_and_queue_t * devices_and_queues
Definition: devices.h:69
#define clib_min(x, y)
Definition: clib.h:295
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:187
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:220
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:56
#define PREDICT_TRUE(x)
Definition: clib.h:112
unsigned long u64
Definition: types.h:89
u32 size
Definition: rdma.h:46
#define VLIB_NODE_FLAG_TRACE_SUPPORTED
Definition: node.h:306
u32 dev_instance
Definition: rdma.h:83
u32 thread_index
Definition: main.h:218
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
static vlib_frame_t * vlib_get_frame(vlib_main_t *vm, vlib_frame_t *f)
Definition: node_funcs.h:216
struct ibv_wq * wq
Definition: rdma.h:44
u32 per_interface_next_index
Definition: rdma.h:71
format_function_t format_rdma_input_trace
Definition: rdma.h:135
int i
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:65
#define VLIB_NODE_FN(node)
Definition: node.h:202
static u32 vlib_buffer_alloc_to_ring_from_pool(vlib_main_t *vm, u32 *ring, u32 start, u32 ring_size, u32 n_buffers, u8 buffer_pool_index)
Allocate buffers into ring from specific buffer pool.
Definition: buffer_funcs.h:675
u32 tail
Definition: rdma.h:48
u8 buffer_pool_index
index of buffer pool this buffer belongs.
Definition: buffer.h:133
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:130
#define static_always_inline
Definition: clib.h:99
#define ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX
Definition: ethernet.h:52
rdma_device_t * devices
Definition: rdma.h:98
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:863
static_always_inline int vnet_device_input_have_features(u32 sw_if_index)
Definition: feature.h:308
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define vlib_get_new_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:343
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:378
static vlib_next_frame_t * vlib_node_runtime_get_next_frame(vlib_main_t *vm, vlib_node_runtime_t *n, u32 next_index)
Definition: node_funcs.h:264
u32 flags
Definition: rdma.h:70
static void vlib_buffer_free_from_ring(vlib_main_t *vm, u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
Free buffers from ring.
Definition: buffer_funcs.h:937
u32 * bufs
Definition: rdma.h:45
static_always_inline void vlib_buffer_copy_template(vlib_buffer_t *b, vlib_buffer_t *bt)
Definition: buffer_funcs.h:145
unsigned short u16
Definition: types.h:57
u8 pool
Definition: rdma.h:75
vlib_node_registration_t rdma_input_node
(constructor) VLIB_REGISTER_NODE (rdma_input_node)
Definition: input.c:361
struct ibv_cq * cq
Definition: rdma.h:43
#define PREDICT_FALSE(x)
Definition: clib.h:111
vlib_main_t * vm
Definition: in2out_ed.c:1810
static_always_inline u32 vlib_buffer_get_default_data_size(vlib_main_t *vm)
Definition: buffer_funcs.h:96
u8 slot
Definition: pci_types.api:22
u32 hw_if_index
Definition: rdma.h:73
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
rdma_main_t rdma_main
Definition: device.c:46
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
static_always_inline uword rdma_device_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, rdma_device_t *rd, u16 qid)
Definition: input.c:269
static_always_inline void rdma_device_input_refill(vlib_main_t *vm, rdma_device_t *rd, rdma_rxq_t *rxq)
Definition: input.c:44
u32 lkey
Definition: rdma.h:74
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:258
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:456
static __clib_unused char * rdma_input_error_strings[]
Definition: input.c:37
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
u32 sw_if_index
Definition: rdma.h:72
u32 head
Definition: rdma.h:47
#define ASSERT(truth)
static_always_inline u32 rdma_device_input_bufs(vlib_main_t *vm, const rdma_device_t *rd, u32 *next, u32 *bi, struct ibv_wc *wc, u32 n_left_from, vlib_buffer_t *bt)
Definition: input.c:203
vlib_frame_t * frame
Definition: node.h:406
u16 flags
Definition: node.h:388
rdma_rxq_t * rxqs
Definition: rdma.h:68
#define foreach_rdma_input_error
Definition: input.c:26
static uword is_pow2(uword x)
Definition: clib.h:235
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
static_always_inline void rdma_device_input_ethernet(vlib_main_t *vm, vlib_node_runtime_t *node, const rdma_device_t *rd, u32 next_index)
Definition: input.c:180
#define foreach_device_and_queue(var, vec)
Definition: devices.h:161
Definition: defs.h:47
static_always_inline void vlib_buffer_copy_indices(u32 *dst, u32 *src, u32 n_indices)
Definition: buffer_funcs.h:102
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static_always_inline void clib_memset_u64(void *p, u64 val, uword count)
Definition: string.h:295
#define vnet_buffer(b)
Definition: buffer.h:408
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:315
static uword vlib_buffer_get_va(vlib_buffer_t *b)
Definition: buffer.h:217
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:203
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:244
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
rdma_input_error_t
Definition: input.c:29
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
Definition: defs.h:46
static_always_inline void rdma_device_input_trace(vlib_main_t *vm, vlib_node_runtime_t *node, const rdma_device_t *rd, u32 n_left, const u32 *bi, u32 next_index)
Definition: input.c:149