FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
buffer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <unistd.h>
17 #include <errno.h>
18 
19 #include <rte_config.h>
20 #include <rte_mbuf.h>
21 #include <rte_ethdev.h>
22 #include <rte_vfio.h>
23 #include <rte_version.h>
24 
25 #include <vlib/vlib.h>
26 #include <dpdk/buffer.h>
27 
28 STATIC_ASSERT (VLIB_BUFFER_PRE_DATA_SIZE == RTE_PKTMBUF_HEADROOM,
29  "VLIB_BUFFER_PRE_DATA_SIZE must be equal to RTE_PKTMBUF_HEADROOM");
30 
31 extern struct rte_mbuf *dpdk_mbuf_template_by_pool_index;
32 #ifndef CLIB_MARCH_VARIANT
33 struct rte_mempool **dpdk_mempool_by_buffer_pool_index = 0;
35 struct rte_mbuf *dpdk_mbuf_template_by_pool_index = 0;
36 
39 {
40  uword buffer_mem_start = vm->buffer_main->buffer_mem_start;
41  struct rte_mempool *mp, *nmp;
42  struct rte_pktmbuf_pool_private priv;
43  enum rte_iova_mode iova_mode;
44  u32 i;
45  u8 *name = 0;
46 
47  u32 elt_size =
48  sizeof (struct rte_mbuf) + sizeof (vlib_buffer_t) + bp->data_size;
49 
50  /* create empty mempools */
55 
56  /* normal mempool */
57  name = format (name, "vpp pool %u%c", bp->index, 0);
58  mp = rte_mempool_create_empty ((char *) name, bp->n_buffers,
59  elt_size, 512, sizeof (priv),
60  bp->numa_node, 0);
61  if (!mp)
62  {
63  vec_free (name);
64  return clib_error_return (0,
65  "failed to create normal mempool for numa node %u",
66  bp->index);
67  }
68  vec_reset_length (name);
69 
70  /* non-cached mempool */
71  name = format (name, "vpp pool %u (no cache)%c", bp->index, 0);
72  nmp = rte_mempool_create_empty ((char *) name, bp->n_buffers,
73  elt_size, 0, sizeof (priv),
74  bp->numa_node, 0);
75  if (!nmp)
76  {
77  rte_mempool_free (mp);
78  vec_free (name);
79  return clib_error_return (0,
80  "failed to create non-cache mempool for numa nude %u",
81  bp->index);
82  }
83  vec_free (name);
84 
85  dpdk_mempool_by_buffer_pool_index[bp->index] = mp;
87 
88  mp->pool_id = nmp->pool_id = bp->index;
89 
90  rte_mempool_set_ops_byname (mp, "vpp", NULL);
91  rte_mempool_set_ops_byname (nmp, "vpp-no-cache", NULL);
92 
93  /* Call the mempool priv initializer */
94  priv.mbuf_data_room_size = VLIB_BUFFER_PRE_DATA_SIZE +
96  priv.mbuf_priv_size = VLIB_BUFFER_HDR_SIZE;
97  rte_pktmbuf_pool_init (mp, &priv);
98  rte_pktmbuf_pool_init (nmp, &priv);
99 
100  iova_mode = rte_eal_iova_mode ();
101 
102  /* populate mempool object buffer header */
103  for (i = 0; i < bp->n_buffers; i++)
104  {
105  struct rte_mempool_objhdr *hdr;
106  vlib_buffer_t *b = vlib_get_buffer (vm, bp->buffers[i]);
107  struct rte_mbuf *mb = rte_mbuf_from_vlib_buffer (b);
108  hdr = (struct rte_mempool_objhdr *) RTE_PTR_SUB (mb, sizeof (*hdr));
109  hdr->mp = mp;
110  hdr->iova = (iova_mode == RTE_IOVA_VA) ?
111  pointer_to_uword (mb) : vlib_physmem_get_pa (vm, mb);
112  STAILQ_INSERT_TAIL (&mp->elt_list, hdr, next);
113  STAILQ_INSERT_TAIL (&nmp->elt_list, hdr, next);
114  mp->populated_size++;
115  nmp->populated_size++;
116  }
117 
118  /* call the object initializers */
119  rte_mempool_obj_iter (mp, rte_pktmbuf_init, 0);
120 
121  /* create mbuf header tempate from the first buffer in the pool */
126  (buffer_mem_start, *bp->buffers,
127  0)), sizeof (struct rte_mbuf));
128 
129  for (i = 0; i < bp->n_buffers; i++)
130  {
131  vlib_buffer_t *b;
132  b = vlib_buffer_ptr_from_index (buffer_mem_start, bp->buffers[i], 0);
133  vlib_buffer_copy_template (b, &bp->buffer_template);
134  }
135 
136  /* map DMA pages if at least one physical device exists */
137  if (rte_eth_dev_count_avail ())
138  {
139  uword i;
140  size_t page_sz;
141  vlib_physmem_map_t *pm;
142  int do_vfio_map = 1;
143 
144  pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
145  page_sz = 1ULL << pm->log2_page_size;
146 
147  for (i = 0; i < pm->n_pages; i++)
148  {
149  char *va = ((char *) pm->base) + i * page_sz;
150  uword pa = (iova_mode == RTE_IOVA_VA) ?
151  pointer_to_uword (va) : pm->page_table[i];
152 
153  if (do_vfio_map &&
154 #if RTE_VERSION < RTE_VERSION_NUM(19, 11, 0, 0)
155  rte_vfio_dma_map (pointer_to_uword (va), pa, page_sz))
156 #else
157  rte_vfio_container_dma_map (RTE_VFIO_DEFAULT_CONTAINER_FD,
158  pointer_to_uword (va), pa, page_sz))
159 #endif
160  do_vfio_map = 0;
161 
162  struct rte_mempool_memhdr *memhdr;
163  memhdr = clib_mem_alloc (sizeof (*memhdr));
164  memhdr->mp = mp;
165  memhdr->addr = va;
166  memhdr->iova = pa;
167  memhdr->len = page_sz;
168  memhdr->free_cb = 0;
169  memhdr->opaque = 0;
170 
171  STAILQ_INSERT_TAIL (&mp->mem_list, memhdr, next);
172  mp->nb_mem_chunks++;
173  }
174  }
175 
176  return 0;
177 }
178 
179 static int
180 dpdk_ops_vpp_alloc (struct rte_mempool *mp)
181 {
182  clib_warning ("");
183  return 0;
184 }
185 
186 static void
187 dpdk_ops_vpp_free (struct rte_mempool *mp)
188 {
189  clib_warning ("");
190 }
191 
192 #endif
193 
196 {
197  /* Only non-replicated packets (b->ref_count == 1) expected */
198 
199  struct rte_mbuf *mb = obj;
201  ASSERT (b->ref_count == 1);
204 }
205 
206 int
207 CLIB_MULTIARCH_FN (dpdk_ops_vpp_enqueue) (struct rte_mempool * mp,
208  void *const *obj_table, unsigned n)
209 {
210  const int batch_size = 32;
212  vlib_buffer_t bt;
213  u8 buffer_pool_index = mp->pool_id;
214  vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
215  u32 bufs[batch_size];
216  u32 n_left = n;
217  void *const *obj = obj_table;
218 
220 
221  while (n_left >= 4)
222  {
223  dpdk_ops_vpp_enqueue_one (&bt, obj[0]);
224  dpdk_ops_vpp_enqueue_one (&bt, obj[1]);
225  dpdk_ops_vpp_enqueue_one (&bt, obj[2]);
226  dpdk_ops_vpp_enqueue_one (&bt, obj[3]);
227  obj += 4;
228  n_left -= 4;
229  }
230 
231  while (n_left)
232  {
233  dpdk_ops_vpp_enqueue_one (&bt, obj[0]);
234  obj += 1;
235  n_left -= 1;
236  }
237 
238  while (n >= batch_size)
239  {
240  vlib_get_buffer_indices_with_offset (vm, (void **) obj_table, bufs,
241  batch_size,
242  sizeof (struct rte_mbuf));
243  vlib_buffer_pool_put (vm, buffer_pool_index, bufs, batch_size);
244  n -= batch_size;
245  obj_table += batch_size;
246  }
247 
248  if (n)
249  {
250  vlib_get_buffer_indices_with_offset (vm, (void **) obj_table, bufs,
251  n, sizeof (struct rte_mbuf));
252  vlib_buffer_pool_put (vm, buffer_pool_index, bufs, n);
253  }
254 
255  return 0;
256 }
257 
259 
262  struct rte_mempool *new, void *obj,
263  vlib_buffer_t * bt)
264 {
265  struct rte_mbuf *mb = obj;
267 
268  if (clib_atomic_sub_fetch (&b->ref_count, 1) == 0)
269  {
270  u32 bi = vlib_get_buffer_index (vm, b);
272  vlib_buffer_pool_put (vm, bt->buffer_pool_index, &bi, 1);
273  return;
274  }
275 }
276 
277 int
279  void *const *obj_table,
280  unsigned n)
281 {
283  vlib_buffer_t bt;
284  struct rte_mempool *mp;
285  mp = dpdk_mempool_by_buffer_pool_index[cmp->pool_id];
286  u8 buffer_pool_index = cmp->pool_id;
287  vlib_buffer_pool_t *bp = vlib_get_buffer_pool (vm, buffer_pool_index);
289 
290  while (n >= 4)
291  {
292  dpdk_ops_vpp_enqueue_no_cache_one (vm, cmp, mp, obj_table[0], &bt);
293  dpdk_ops_vpp_enqueue_no_cache_one (vm, cmp, mp, obj_table[1], &bt);
294  dpdk_ops_vpp_enqueue_no_cache_one (vm, cmp, mp, obj_table[2], &bt);
295  dpdk_ops_vpp_enqueue_no_cache_one (vm, cmp, mp, obj_table[3], &bt);
296  obj_table += 4;
297  n -= 4;
298  }
299 
300  while (n)
301  {
302  dpdk_ops_vpp_enqueue_no_cache_one (vm, cmp, mp, obj_table[0], &bt);
303  obj_table += 1;
304  n -= 1;
305  }
306 
307  return 0;
308 }
309 
311 
313 dpdk_mbuf_init_from_template (struct rte_mbuf **mba, struct rte_mbuf *mt,
314  int count)
315 {
316  /* Assumptions about rte_mbuf layout */
317  STATIC_ASSERT_OFFSET_OF (struct rte_mbuf, buf_addr, 0);
318  STATIC_ASSERT_OFFSET_OF (struct rte_mbuf, buf_iova, 8);
319  STATIC_ASSERT_SIZEOF_ELT (struct rte_mbuf, buf_iova, 8);
320  STATIC_ASSERT_SIZEOF_ELT (struct rte_mbuf, buf_iova, 8);
321  STATIC_ASSERT_SIZEOF (struct rte_mbuf, 128);
322 
323  while (count--)
324  {
325  struct rte_mbuf *mb = mba[0];
326  int i;
327  /* bytes 0 .. 15 hold buf_addr and buf_iova which we need to preserve */
328  /* copy bytes 16 .. 31 */
329  *((u8x16 *) mb + 1) = *((u8x16 *) mt + 1);
330 
331  /* copy bytes 32 .. 127 */
332 #ifdef CLIB_HAVE_VEC256
333  for (i = 1; i < 4; i++)
334  *((u8x32 *) mb + i) = *((u8x32 *) mt + i);
335 #else
336  for (i = 2; i < 8; i++)
337  *((u8x16 *) mb + i) = *((u8x16 *) mt + i);
338 #endif
339  mba++;
340  }
341 }
342 
343 int
344 CLIB_MULTIARCH_FN (dpdk_ops_vpp_dequeue) (struct rte_mempool * mp,
345  void **obj_table, unsigned n)
346 {
347  const int batch_size = 32;
349  u32 bufs[batch_size], total = 0, n_alloc = 0;
350  u8 buffer_pool_index = mp->pool_id;
351  void **obj = obj_table;
352  struct rte_mbuf t = dpdk_mbuf_template_by_pool_index[buffer_pool_index];
353 
354  while (n >= batch_size)
355  {
356  n_alloc = vlib_buffer_alloc_from_pool (vm, bufs, batch_size,
357  buffer_pool_index);
358  if (n_alloc != batch_size)
359  goto alloc_fail;
360 
361  vlib_get_buffers_with_offset (vm, bufs, obj, batch_size,
362  -(i32) sizeof (struct rte_mbuf));
363  dpdk_mbuf_init_from_template ((struct rte_mbuf **) obj, &t, batch_size);
364  total += batch_size;
365  obj += batch_size;
366  n -= batch_size;
367  }
368 
369  if (n)
370  {
371  n_alloc = vlib_buffer_alloc_from_pool (vm, bufs, n, buffer_pool_index);
372 
373  if (n_alloc != n)
374  goto alloc_fail;
375 
376  vlib_get_buffers_with_offset (vm, bufs, obj, n,
377  -(i32) sizeof (struct rte_mbuf));
378  dpdk_mbuf_init_from_template ((struct rte_mbuf **) obj, &t, n);
379  }
380 
381  return 0;
382 
383 alloc_fail:
384  /* dpdk doesn't support partial alloc, so we need to return what we
385  already got */
386  if (n_alloc)
387  vlib_buffer_pool_put (vm, buffer_pool_index, bufs, n_alloc);
388  obj = obj_table;
389  while (total)
390  {
391  vlib_get_buffer_indices_with_offset (vm, obj, bufs, batch_size,
392  sizeof (struct rte_mbuf));
393  vlib_buffer_pool_put (vm, buffer_pool_index, bufs, batch_size);
394 
395  obj += batch_size;
396  total -= batch_size;
397  }
398  return -ENOENT;
399 }
400 
402 
403 #ifndef CLIB_MARCH_VARIANT
404 
405 static int
406 dpdk_ops_vpp_dequeue_no_cache (struct rte_mempool *mp, void **obj_table,
407  unsigned n)
408 {
409  clib_error ("bug");
410  return 0;
411 }
412 
413 static unsigned
414 dpdk_ops_vpp_get_count (const struct rte_mempool *mp)
415 {
416  clib_warning ("");
417  return 0;
418 }
419 
420 static unsigned
421 dpdk_ops_vpp_get_count_no_cache (const struct rte_mempool *mp)
422 {
423  struct rte_mempool *cmp;
425  return dpdk_ops_vpp_get_count (cmp);
426 }
427 
428 clib_error_t *
430 {
431  clib_error_t *err;
432  vlib_buffer_pool_t *bp;
433 
434  struct rte_mempool_ops ops = { };
435 
436  strncpy (ops.name, "vpp", 4);
437  ops.alloc = dpdk_ops_vpp_alloc;
438  ops.free = dpdk_ops_vpp_free;
439  ops.get_count = dpdk_ops_vpp_get_count;
442  rte_mempool_register_ops (&ops);
443 
444  strncpy (ops.name, "vpp-no-cache", 13);
445  ops.get_count = dpdk_ops_vpp_get_count_no_cache;
447  ops.dequeue = dpdk_ops_vpp_dequeue_no_cache;
448  rte_mempool_register_ops (&ops);
449 
450  /* *INDENT-OFF* */
452  if (bp->start && (err = dpdk_buffer_pool_init (vm, bp)))
453  return err;
454  /* *INDENT-ON* */
455  return 0;
456 }
457 
458 VLIB_BUFFER_SET_EXT_HDR_SIZE (sizeof (struct rte_mempool_objhdr) +
459  sizeof (struct rte_mbuf));
460 
461 #endif
462 
463 /** @endcond */
464 /*
465  * fd.io coding-style-patch-verification: ON
466  *
467  * Local Variables:
468  * eval: (c-set-style "gnu")
469  * End:
470  */
u8 count
Definition: dhcp.api:208
#define vlib_buffer_from_rte_mbuf(x)
Definition: buffer.h:20
#define CLIB_MARCH_FN_POINTER(fn)
Definition: cpu.h:87
STATIC_ASSERT_OFFSET_OF(vlib_buffer_t, template_end, 64)
#define clib_error(format, args...)
Definition: error.h:62
static int dpdk_ops_vpp_alloc(struct rte_mempool *mp)
Definition: buffer.c:180
static_always_inline void dpdk_mbuf_init_from_template(struct rte_mbuf **mba, struct rte_mbuf *mt, int count)
Definition: buffer.c:313
static_always_inline vlib_buffer_pool_t * vlib_get_buffer_pool(vlib_main_t *vm, u8 buffer_pool_index)
Definition: buffer_funcs.h:485
#define NULL
Definition: clib.h:58
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
int i
vlib_buffer_main_t * buffer_main
Definition: main.h:152
uword * page_table
Definition: physmem.h:51
VLIB_BUFFER_SET_EXT_HDR_SIZE(sizeof(struct rte_mempool_objhdr)+sizeof(struct rte_mbuf))
static_always_inline void vlib_get_buffers_with_offset(vlib_main_t *vm, u32 *bi, void **b, int count, i32 offset)
Translate array of buffer indices into buffer pointers with offset.
Definition: buffer_funcs.h:178
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
Definition: physmem.c:87
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:451
unsigned char u8
Definition: types.h:56
u8 buffer_pool_index
index of buffer pool this buffer belongs.
Definition: buffer.h:133
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
#define clib_memcpy(d, s, n)
Definition: string.h:180
#define static_always_inline
Definition: clib.h:99
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
clib_error_t * dpdk_buffer_pool_init(vlib_main_t *vm, vlib_buffer_pool_t *bp)
Definition: buffer.c:38
unsigned int u32
Definition: types.h:88
static void dpdk_ops_vpp_free(struct rte_mempool *mp)
Definition: buffer.c:187
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:257
static_always_inline void dpdk_ops_vpp_enqueue_one(vlib_buffer_t *bt, void *obj)
Definition: buffer.c:195
static_always_inline void vlib_buffer_copy_template(vlib_buffer_t *b, vlib_buffer_t *bt)
Definition: buffer_funcs.h:145
#define rte_mbuf_from_vlib_buffer(x)
Definition: buffer.h:19
vlib_buffer_t buffer_template
Definition: buffer.h:442
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:454
static_always_inline void vlib_get_buffer_indices_with_offset(vlib_main_t *vm, void **b, u32 *bi, uword count, i32 offset)
Translate array of buffer pointers into buffer indices with offset.
Definition: buffer_funcs.h:276
STATIC_ASSERT(VLIB_BUFFER_PRE_DATA_SIZE==RTE_PKTMBUF_HEADROOM, "VLIB_BUFFER_PRE_DATA_SIZE must be equal to RTE_PKTMBUF_HEADROOM")
#define clib_atomic_sub_fetch(a, b)
Definition: atomics.h:31
static_always_inline u32 vlib_buffer_get_default_data_size(vlib_main_t *vm)
Definition: buffer_funcs.h:96
#define STATIC_ASSERT_SIZEOF_ELT(d, e, s)
vlib_main_t * vm
Definition: buffer.c:323
static_always_inline void vlib_buffer_pool_put(vlib_main_t *vm, u8 buffer_pool_index, u32 *buffers, u32 n_buffers)
Definition: buffer_funcs.h:698
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
#define clib_warning(format, args...)
Definition: error.h:59
int CLIB_MULTIARCH_FN() dpdk_ops_vpp_enqueue_no_cache(struct rte_mempool *cmp, void *const *obj_table, unsigned n)
Definition: buffer.c:278
int CLIB_MULTIARCH_FN() dpdk_ops_vpp_enqueue(struct rte_mempool *mp, void *const *obj_table, unsigned n)
Definition: buffer.c:207
string name[64]
Definition: ip.api:44
CLIB_MARCH_FN_REGISTRATION(dpdk_ops_vpp_enqueue)
#define VLIB_BUFFER_HDR_SIZE
Definition: buffer.h:194
signed int i32
Definition: types.h:77
static u32 vlib_buffer_alloc_from_pool(vlib_main_t *vm, u32 *buffers, u32 n_buffers, u8 buffer_pool_index)
Allocate buffers from specific pool into supplied array.
Definition: buffer_funcs.h:530
struct rte_mbuf * dpdk_mbuf_template_by_pool_index
Definition: buffer.c:35
#define ASSERT(truth)
static_always_inline void dpdk_ops_vpp_enqueue_no_cache_one(vlib_main_t *vm, struct rte_mempool *old, struct rte_mempool *new, void *obj, vlib_buffer_t *bt)
Definition: buffer.c:261
static void * vlib_buffer_ptr_from_index(uword buffer_mem_start, u32 buffer_index, uword offset)
Definition: buffer_funcs.h:71
static void * clib_mem_alloc(uword size)
Definition: mem.h:153
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static u64 vlib_physmem_get_pa(vlib_main_t *vm, void *mem)
clib_error_t * dpdk_buffer_pools_create(vlib_main_t *vm)
Definition: buffer.c:429
struct rte_mempool ** dpdk_no_cache_mempool_by_buffer_pool_index
Definition: buffer.c:34
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
uword buffer_mem_start
Definition: buffer.h:452
static unsigned dpdk_ops_vpp_get_count_no_cache(const struct rte_mempool *mp)
Definition: buffer.c:421
static unsigned dpdk_ops_vpp_get_count(const struct rte_mempool *mp)
Definition: buffer.c:414
#define vec_foreach(var, vec)
Vector iterator.
static int dpdk_ops_vpp_dequeue_no_cache(struct rte_mempool *mp, void **obj_table, unsigned n)
Definition: buffer.c:406
struct rte_mempool ** dpdk_mempool_by_buffer_pool_index
Definition: buffer.c:33
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define STATIC_ASSERT_SIZEOF(d, s)
int CLIB_MULTIARCH_FN() dpdk_ops_vpp_dequeue(struct rte_mempool *mp, void **obj_table, unsigned n)
Definition: buffer.c:344
volatile u8 ref_count
Reference count for this buffer.
Definition: buffer.h:130
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
#define CLIB_MULTIARCH_FN(fn)
Definition: cpu.h:56