FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
physmem.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 <sys/types.h>
18 #include <sys/mount.h>
19 #include <sys/mman.h>
20 #include <sys/fcntl.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 
24 #include <vppinfra/linux/syscall.h>
25 #include <vppinfra/linux/sysfs.h>
26 #include <vlib/vlib.h>
27 #include <vlib/physmem.h>
28 #include <vlib/unix/unix.h>
29 #include <vlib/pci/pci.h>
30 #include <vlib/linux/vfio.h>
31 
32 #if defined(__x86_64__) && !defined(CLIB_SANITIZE_ADDR)
33 /* we keep physmem in low 38 bits of VA address space as some
34  IOMMU implamentation cannot map above that range */
35 #define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR (1ULL << 36)
36 #else
37 /* let kernel decide */
38 #define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR 0
39 #endif
40 
43  u32 log2_page_sz, u32 numa_node,
44  u32 * map_index)
45 {
48  vlib_physmem_map_t *map;
50  clib_error_t *error = 0;
51  void *va;
52  uword i;
53 
54  va = clib_pmalloc_create_shared_arena (pm, name, size, log2_page_sz,
55  numa_node);
56 
57  if (va == 0)
58  return clib_error_return (0, "%U", format_clib_error,
60 
61  a = clib_pmalloc_get_arena (pm, va);
62 
63  pool_get (vpm->maps, map);
64  *map_index = map->index = map - vpm->maps;
65  map->base = va;
66  map->fd = a->fd;
67  map->n_pages = a->n_pages * a->subpages_per_page;
69  map->numa_node = a->numa_node;
70 
71  for (i = 0; i < a->n_pages; i++)
72  {
73  uword pa =
74  clib_pmalloc_get_pa (pm, (u8 *) va + (i << a->log2_subpage_sz));
75 
76  /* maybe iova */
77  if (pa == 0)
78  pa = pointer_to_uword (va);
79 
80  vec_add1 (map->page_table, pa);
81  }
82 
83  return error;
84 }
85 
88 {
90  return pool_elt_at_index (vpm->maps, index);
91 }
92 
95 {
97  clib_error_t *error = 0;
98  u64 *pt = 0;
99  void *p;
100 
101  /* check if pagemap is accessible */
102  pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
103  if (pt && pt[0])
105  vec_free (pt);
106 
107  if ((error = linux_vfio_init (vm)))
108  return error;
109 
112  memset (p, 0, sizeof (clib_pmalloc_main_t));
113  vpm->pmalloc_main = (clib_pmalloc_main_t *) p;
114 
115  if (vpm->base_addr == 0)
117 
119 
120  /* update base_addr and max_size per actual allocation */
121  vpm->base_addr = (uword) vpm->pmalloc_main->base;
122  vpm->max_size = (uword) vpm->pmalloc_main->max_pages <<
124 
125  return error;
126 }
127 
128 static clib_error_t *
130  unformat_input_t * input, vlib_cli_command_t * cmd)
131 {
132  vlib_physmem_main_t *vpm = &vm->physmem_main;
133  unformat_input_t _line_input, *line_input = &_line_input;
134  u32 verbose = 0, map = 0;
135 
136  if (unformat_user (input, unformat_line_input, line_input))
137  {
138  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
139  {
140  if (unformat (line_input, "verbose"))
141  verbose = 1;
142  else if (unformat (line_input, "v"))
143  verbose = 1;
144  else if (unformat (line_input, "detail"))
145  verbose = 2;
146  else if (unformat (line_input, "d"))
147  verbose = 2;
148  else if (unformat (line_input, "map"))
149  map = 1;
150  else
151  break;
152  }
153  unformat_free (line_input);
154  }
155 
156  if (map)
158  else
159  vlib_cli_output (vm, " %U", format_pmalloc, vpm->pmalloc_main, verbose);
160 
161  return 0;
162 }
163 
164 /* *INDENT-OFF* */
165 VLIB_CLI_COMMAND (show_physmem_command, static) = {
166  .path = "show physmem",
167  .short_help = "show physmem [verbose | detail | map]",
168  .function = show_physmem,
169 };
170 /* *INDENT-ON* */
171 
172 static clib_error_t *
174 {
175  vlib_physmem_main_t *vpm = &vm->physmem_main;
176 
178  {
179  if (unformat (input, "base-addr 0x%lx", &vpm->base_addr))
180  ;
181  else if (unformat (input, "max-size %U",
183  ;
184  else
185  return unformat_parse_error (input);
186  }
187 
188  unformat_free (input);
189  return 0;
190 }
191 
193 
194 /*
195  * fd.io coding-style-patch-verification: ON
196  *
197  * Local Variables:
198  * eval: (c-set-style "gnu")
199  * End:
200  */
u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
a
Definition: bitmap.h:538
vlib_physmem_main_t physmem_main
Definition: main.h:155
unsigned long u64
Definition: types.h:89
format_function_t format_pmalloc_map
Definition: pmalloc.h:122
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
int i
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
uword * page_table
Definition: physmem.h:51
void * clib_pmalloc_create_shared_arena(clib_pmalloc_main_t *pm, char *name, uword size, u32 log2_page_sz, u32 numa_node)
Definition: pmalloc.c:406
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
Definition: physmem.c:87
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:237
unsigned char u8
Definition: types.h:56
static uword min_log2(uword x)
Definition: clib.h:144
clib_pmalloc_main_t * pmalloc_main
Definition: physmem.h:64
int clib_pmalloc_init(clib_pmalloc_main_t *pm, uword base_addr, uword size)
Definition: pmalloc.c:64
#define VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP
Definition: physmem.h:61
format_function_t format_pmalloc
Definition: pmalloc.h:121
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
unformat_function_t unformat_line_input
Definition: format.h:283
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:519
clib_error_t * vlib_physmem_shared_map_create(vlib_main_t *vm, char *name, uword size, u32 log2_page_sz, u32 numa_node, u32 *map_index)
Definition: physmem.c:42
struct _unformat_input_t unformat_input_t
u64 size
Definition: vhost_user.h:140
clib_error_t * linux_vfio_init(vlib_main_t *vm)
Definition: vfio.c:239
vlib_main_t * vm
Definition: in2out_ed.c:1810
static clib_error_t * clib_pmalloc_last_error(clib_pmalloc_main_t *pm)
Definition: pmalloc.h:125
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:226
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
string name[64]
Definition: ip.api:44
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
vlib_physmem_map_t * maps
Definition: physmem.h:63
static uword pointer_to_uword(const void *p)
Definition: types.h:131
#define unformat_parse_error(input)
Definition: format.h:269
static clib_error_t * show_physmem(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: physmem.c:129
static clib_pmalloc_arena_t * clib_pmalloc_get_arena(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.h:142
clib_error_t * vlib_physmem_init(vlib_main_t *vm)
Definition: physmem.c:94
static clib_error_t * vlib_physmem_config(vlib_main_t *vm, unformat_input_t *input)
Definition: physmem.c:173
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
u64 * clib_mem_vm_get_paddr(void *mem, int log2_page_size, int n_pages)
Definition: mem.c:347
#define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR
Definition: physmem.c:35
unformat_function_t unformat_memory_size
Definition: format.h:296
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:161
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
static uword clib_pmalloc_get_pa(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.h:149