FD.io VPP  v21.10.1-2-g0a485f517
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/sysfs.h>
25 #include <vlib/vlib.h>
26 #include <vlib/physmem.h>
27 #include <vlib/unix/unix.h>
28 #include <vlib/pci/pci.h>
29 #include <vlib/linux/vfio.h>
30 
31 #if defined(__x86_64__) && !defined(CLIB_SANITIZE_ADDR)
32 /* we keep physmem in low 38 bits of VA address space as some
33  IOMMU implamentation cannot map above that range */
34 #define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR (1ULL << 36)
35 #else
36 /* let kernel decide */
37 #define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR 0
38 #endif
39 
42  u32 log2_page_sz, u32 numa_node,
43  u32 * map_index)
44 {
49  clib_error_t *error = 0;
50  void *va;
51  uword i;
52 
53  va = clib_pmalloc_create_shared_arena (pm, name, size, log2_page_sz,
54  numa_node);
55 
56  if (va == 0)
57  return clib_error_return (0, "%U", format_clib_error,
59 
60  a = clib_pmalloc_get_arena (pm, va);
61 
62  pool_get (vpm->maps, map);
63  *map_index = map->index = map - vpm->maps;
64  map->base = va;
65  map->fd = a->fd;
66  map->n_pages = a->n_pages * a->subpages_per_page;
67  map->log2_page_size = a->log2_subpage_sz;
68  map->numa_node = a->numa_node;
69 
70  for (i = 0; i < a->n_pages; i++)
71  {
72  uword pa =
73  clib_pmalloc_get_pa (pm, (u8 *) va + (i << a->log2_subpage_sz));
74 
75  /* maybe iova */
76  if (pa == 0)
77  pa = pointer_to_uword (va);
78 
79  vec_add1 (map->page_table, pa);
80  }
81 
82  return error;
83 }
84 
87 {
89  return pool_elt_at_index (vpm->maps, index);
90 }
91 
94 {
96  clib_error_t *error = 0;
97  u64 *pt = 0;
98  void *p;
99 
100  /* check if pagemap is accessible */
101  pt = clib_mem_vm_get_paddr (&pt, min_log2 (sysconf (_SC_PAGESIZE)), 1);
102  if (pt && pt[0])
104  vec_free (pt);
105 
106  if ((error = linux_vfio_init (vm)))
107  return error;
108 
111  memset (p, 0, sizeof (clib_pmalloc_main_t));
112  vpm->pmalloc_main = (clib_pmalloc_main_t *) p;
113 
114  if (vpm->base_addr == 0)
116 
118 
119  /* update base_addr and max_size per actual allocation */
120  vpm->base_addr = (uword) vpm->pmalloc_main->base;
121  vpm->max_size = (uword) vpm->pmalloc_main->max_pages <<
123 
124  return error;
125 }
126 
127 static clib_error_t *
129  unformat_input_t * input, vlib_cli_command_t * cmd)
130 {
132  unformat_input_t _line_input, *line_input = &_line_input;
133  u32 verbose = 0, map = 0;
134 
135  if (unformat_user (input, unformat_line_input, line_input))
136  {
137  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
138  {
139  if (unformat (line_input, "verbose"))
140  verbose = 1;
141  else if (unformat (line_input, "v"))
142  verbose = 1;
143  else if (unformat (line_input, "detail"))
144  verbose = 2;
145  else if (unformat (line_input, "d"))
146  verbose = 2;
147  else if (unformat (line_input, "map"))
148  map = 1;
149  else
150  break;
151  }
152  unformat_free (line_input);
153  }
154 
155  if (map)
157  else
158  vlib_cli_output (vm, " %U", format_pmalloc, vpm->pmalloc_main, verbose);
159 
160  return 0;
161 }
162 
163 /* *INDENT-OFF* */
165  .path = "show physmem",
166  .short_help = "show physmem [verbose | detail | map]",
167  .function = show_physmem,
168 };
169 /* *INDENT-ON* */
170 
171 static clib_error_t *
173 {
175 
177  {
178  if (unformat (input, "base-addr 0x%lx", &vpm->base_addr))
179  ;
180  else if (unformat (input, "max-size %U",
182  ;
183  else
184  return unformat_parse_error (input);
185  }
186 
187  unformat_free (input);
188  return 0;
189 }
190 
192 
193 /*
194  * fd.io coding-style-patch-verification: ON
195  *
196  * Local Variables:
197  * eval: (c-set-style "gnu")
198  * End:
199  */
vlib_physmem_main_t::max_size
uword max_size
Definition: physmem.h:60
vlib.h
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
format_pmalloc_map
format_function_t format_pmalloc_map
Definition: pmalloc.h:119
vlib_physmem_map_t
Definition: physmem.h:45
pointer_to_uword
static uword pointer_to_uword(const void *p)
Definition: types.h:131
unformat_line_input
unformat_function_t unformat_line_input
Definition: format.h:275
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
name
string name[64]
Definition: fib.api:25
format_pmalloc
format_function_t format_pmalloc
Definition: pmalloc.h:118
clib_pmalloc_get_pa
static uword clib_pmalloc_get_pa(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.h:146
vlib_physmem_main_t::base_addr
uword base_addr
Definition: physmem.h:59
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
unformat_parse_error
#define unformat_parse_error(input)
Definition: format.h:261
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
clib_pmalloc_arena_t
Definition: pmalloc.h:45
vlib_physmem_main_t::pmalloc_main
clib_pmalloc_main_t * pmalloc_main
Definition: physmem.h:64
VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP
#define VLIB_PHYSMEM_MAIN_F_HAVE_PAGEMAP
Definition: physmem.h:61
vlib_physmem_get_map
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
Definition: physmem.c:86
show_physmem_command
static vlib_cli_command_t show_physmem_command
(constructor) VLIB_CLI_COMMAND (show_physmem_command)
Definition: physmem.c:164
unformat_input_t
struct _unformat_input_t unformat_input_t
error
Definition: cJSON.c:88
sysfs.h
VLIB_EARLY_CONFIG_FUNCTION
#define VLIB_EARLY_CONFIG_FUNCTION(x, n,...)
Definition: init.h:220
format_clib_error
__clib_export u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
unformat_memory_size
unformat_function_t unformat_memory_size
Definition: format.h:288
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
vlib_main_t::physmem_main
vlib_physmem_main_t physmem_main
Definition: main.h:170
map
counters map
Definition: map.api:356
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
vlib_config_function_runtime_t
Definition: init.h:68
uword
u64 uword
Definition: types.h:112
clib_pmalloc_init
__clib_export int clib_pmalloc_init(clib_pmalloc_main_t *pm, uword base_addr, uword size)
Definition: pmalloc.c:50
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
clib_pmalloc_main_t
Definition: pmalloc.h:60
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:58
vlib_physmem_shared_map_create
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:41
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
clib_pmalloc_last_error
static clib_error_t * clib_pmalloc_last_error(clib_pmalloc_main_t *pm)
Definition: pmalloc.h:122
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
size
u32 size
Definition: vhost_user.h:125
index
u32 index
Definition: flow_types.api:221
clib_pmalloc_main_t::base
u8 * base
Definition: pmalloc.h:67
vlib_physmem_init
clib_error_t * vlib_physmem_init(vlib_main_t *vm)
Definition: physmem.c:93
u64
unsigned long u64
Definition: types.h:89
u32
unsigned int u32
Definition: types.h:88
VLIB_PHYSMEM_DEFAULT_BASE_ADDDR
#define VLIB_PHYSMEM_DEFAULT_BASE_ADDDR
Definition: physmem.c:34
pci.h
clib_pmalloc_main_t::def_log2_page_sz
clib_mem_page_sz_t def_log2_page_sz
Definition: pmalloc.h:70
linux_vfio_init
clib_error_t * linux_vfio_init(vlib_main_t *vm)
Definition: vfio.c:240
vlib_physmem_config
static clib_error_t * vlib_physmem_config(vlib_main_t *vm, unformat_input_t *input)
Definition: physmem.c:172
physmem.h
vlib_main_t
Definition: main.h:102
clib_pmalloc_main_t::max_pages
u32 max_pages
Definition: pmalloc.h:73
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
a
a
Definition: bitmap.h:525
vfio.h
unix.h
show_physmem
static clib_error_t * show_physmem(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: physmem.c:128
i
int i
Definition: flowhash_template.h:376
vlib_physmem_main_t
Definition: physmem.h:56
clib_mem_alloc_aligned
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:264
clib_mem_vm_get_paddr
__clib_export u64 * clib_mem_vm_get_paddr(void *mem, clib_mem_page_sz_t log2_page_size, int n_pages)
Definition: mem.c:566
vlib_physmem_main_t::flags
u32 flags
Definition: physmem.h:58
clib_pmalloc_create_shared_arena
__clib_export 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:371
vlib_physmem_main_t::maps
vlib_physmem_map_t * maps
Definition: physmem.h:63
min_log2
static uword min_log2(uword x)
Definition: clib.h:176
vlib_cli_command_t
Definition: cli.h:92
clib_pmalloc_get_arena
static clib_pmalloc_arena_t * clib_pmalloc_get_arena(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.h:139
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137