vlib: store buffer memory information in the buffer_main
[vpp.git] / src / vlib / unix / physmem.c
1 /*
2  * Copyright (c) 2015 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  * physmem.c: Unix physical memory
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vlib/unix/physmem.h>
41
42 static physmem_main_t physmem_main;
43
44 static void *
45 unix_physmem_alloc_aligned (vlib_physmem_main_t * vpm, uword n_bytes,
46                             uword alignment)
47 {
48   physmem_main_t *pm = &physmem_main;
49   uword lo_offset, hi_offset;
50   uword *to_free = 0;
51
52   /* IO memory is always at least cache aligned. */
53   alignment = clib_max (alignment, CLIB_CACHE_LINE_BYTES);
54
55   while (1)
56     {
57       mheap_get_aligned (pm->heap, n_bytes,
58                          /* align */ alignment,
59                          /* align offset */ 0,
60                          &lo_offset);
61
62       /* Allocation failed? */
63       if (lo_offset == ~0)
64         break;
65
66       /* Make sure allocation does not span DMA physical chunk boundary. */
67       hi_offset = lo_offset + n_bytes - 1;
68
69       if ((lo_offset >> vpm->log2_n_bytes_per_page) ==
70           (hi_offset >> vpm->log2_n_bytes_per_page))
71         break;
72
73       /* Allocation would span chunk boundary, queue it to be freed as soon as
74          we find suitable chunk. */
75       vec_add1 (to_free, lo_offset);
76     }
77
78   if (to_free != 0)
79     {
80       uword i;
81       for (i = 0; i < vec_len (to_free); i++)
82         mheap_put (pm->heap, to_free[i]);
83       vec_free (to_free);
84     }
85
86   return lo_offset != ~0 ? pm->heap + lo_offset : 0;
87 }
88
89 static void
90 unix_physmem_free (void *x)
91 {
92   physmem_main_t *pm = &physmem_main;
93
94   /* Return object to region's heap. */
95   mheap_put (pm->heap, x - pm->heap);
96 }
97
98 static void
99 htlb_shutdown (void)
100 {
101   physmem_main_t *pm = &physmem_main;
102
103   if (!pm->shmid)
104     return;
105   shmctl (pm->shmid, IPC_RMID, 0);
106   pm->shmid = 0;
107 }
108
109 /* try to use huge TLB pgs if possible */
110 static int
111 htlb_init (vlib_main_t * vm)
112 {
113   vlib_physmem_main_t *vpm = &vm->physmem_main;
114   physmem_main_t *pm = &physmem_main;
115   u64 hugepagesize, pagesize;
116   u64 pfn, seek_loc;
117   u64 cur, physaddr, ptbits;
118   int fd, i;
119
120   pm->shmid = shmget (11 /* key, my amp goes to 11 */ , pm->mem_size,
121                       IPC_CREAT | SHM_HUGETLB | SHM_R | SHM_W);
122   if (pm->shmid < 0)
123     {
124       clib_unix_warning ("shmget");
125       return 0;
126     }
127
128   pm->mem = shmat (pm->shmid, NULL, 0 /* flags */ );
129   if (pm->mem == 0)
130     {
131       shmctl (pm->shmid, IPC_RMID, 0);
132       return 0;
133     }
134
135   memset (pm->mem, 0, pm->mem_size);
136
137   /* $$$ get page size info from /proc/meminfo */
138   hugepagesize = 2 << 20;
139   pagesize = 4 << 10;
140   vpm->log2_n_bytes_per_page = min_log2 (hugepagesize);
141   vec_resize (vpm->page_table, pm->mem_size / hugepagesize);
142
143   vpm->page_mask = pow2_mask (vpm->log2_n_bytes_per_page);
144   vpm->virtual.start = pointer_to_uword (pm->mem);
145   vpm->virtual.size = pm->mem_size;
146   vpm->virtual.end = vpm->virtual.start + vpm->virtual.size;
147
148   fd = open ("/proc/self/pagemap", O_RDONLY);
149
150   if (fd < 0)
151     {
152       (void) shmdt (pm->mem);
153       return 0;
154     }
155
156   pm->heap = mheap_alloc_with_flags (pm->mem, pm->mem_size,
157                                      /* Don't want mheap mmap/munmap with IO memory. */
158                                      MHEAP_FLAG_DISABLE_VM);
159
160   cur = pointer_to_uword (pm->mem);
161   i = 0;
162
163   while (cur < pointer_to_uword (pm->mem) + pm->mem_size)
164     {
165       pfn = (u64) cur / pagesize;
166       seek_loc = pfn * sizeof (u64);
167       if (lseek (fd, seek_loc, SEEK_SET) != seek_loc)
168         {
169           clib_unix_warning ("lseek to 0x%llx", seek_loc);
170           shmctl (pm->shmid, IPC_RMID, 0);
171           close (fd);
172           return 0;
173         }
174       if (read (fd, &ptbits, sizeof (ptbits)) != (sizeof (ptbits)))
175         {
176           clib_unix_warning ("read ptbits");
177           shmctl (pm->shmid, IPC_RMID, 0);
178           close (fd);
179           return 0;
180         }
181
182       /* bits 0-54 are the physical page number */
183       physaddr = (ptbits & 0x7fffffffffffffULL) * pagesize;
184       if (CLIB_DEBUG > 1)
185         fformat (stderr, "pm: virtual 0x%llx physical 0x%llx\n",
186                  cur, physaddr);
187       vpm->page_table[i++] = physaddr;
188
189       cur += hugepagesize;
190     }
191   close (fd);
192   atexit (htlb_shutdown);
193   return 1;
194 }
195
196 int vlib_app_physmem_init (vlib_main_t * vm,
197                            physmem_main_t * pm, int) __attribute__ ((weak));
198 int
199 vlib_app_physmem_init (vlib_main_t * vm, physmem_main_t * pm, int x)
200 {
201   return 0;
202 }
203
204 clib_error_t *
205 unix_physmem_init (vlib_main_t * vm, int physical_memory_required)
206 {
207   vlib_physmem_main_t *vpm = &vm->physmem_main;
208   physmem_main_t *pm = &physmem_main;
209   clib_error_t *error = 0;
210
211   /* Avoid multiple calls. */
212   if (vm->os_physmem_alloc_aligned)
213     return error;
214
215   vm->os_physmem_alloc_aligned = unix_physmem_alloc_aligned;
216   vm->os_physmem_free = unix_physmem_free;
217   pm->mem = MAP_FAILED;
218
219   if (pm->mem_size == 0)
220     pm->mem_size = 16 << 20;
221
222   /* OK, Mr. App, you tell us */
223   if (vlib_app_physmem_init (vm, pm, physical_memory_required))
224     return 0;
225
226   if (!pm->no_hugepages && htlb_init (vm))
227     {
228       fformat (stderr, "%s: use huge pages\n", __FUNCTION__);
229       return 0;
230     }
231
232   pm->mem =
233     mmap (0, pm->mem_size, PROT_READ | PROT_WRITE,
234           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
235   if (pm->mem == MAP_FAILED)
236     {
237       error = clib_error_return_unix (0, "mmap");
238       goto done;
239     }
240
241   pm->heap = mheap_alloc (pm->mem, pm->mem_size);
242
243   /* Identity map with a single page. */
244   vpm->log2_n_bytes_per_page = min_log2 (pm->mem_size);
245   vec_add1 (vpm->page_table, pointer_to_uword (pm->mem));
246
247   vpm->page_mask = pow2_mask (vpm->log2_n_bytes_per_page);
248   vpm->virtual.start = pointer_to_uword (pm->mem);
249   vpm->virtual.size = pm->mem_size;
250   vpm->virtual.end = vpm->virtual.start + vpm->virtual.size;
251   vpm->is_fake = 1;
252
253   fformat (stderr, "%s: use fake dma pages\n", __FUNCTION__);
254
255 done:
256   if (error)
257     {
258       if (pm->mem != MAP_FAILED)
259         munmap (pm->mem, pm->mem_size);
260     }
261   return error;
262 }
263
264 static clib_error_t *
265 show_physmem (vlib_main_t * vm,
266               unformat_input_t * input, vlib_cli_command_t * cmd)
267 {
268   physmem_main_t *pm = &physmem_main;
269
270   if (pm->heap)
271     vlib_cli_output (vm, "%U", format_mheap, pm->heap, /* verbose */ 1);
272   else
273     vlib_cli_output (vm, "No physmem allocated.");
274   return 0;
275 }
276
277 /* *INDENT-OFF* */
278 VLIB_CLI_COMMAND (show_physmem_command, static) = {
279   .path = "show physmem",
280   .short_help = "Show physical memory allocation",
281   .function = show_physmem,
282 };
283 /* *INDENT-ON* */
284
285 static clib_error_t *
286 show_affinity (vlib_main_t * vm,
287                unformat_input_t * input, vlib_cli_command_t * cmd)
288 {
289   cpu_set_t set;
290   cpu_set_t *setp = &set;
291   int i, rv;
292   u8 *s = 0;
293   int first_set_bit_in_run = -1;
294   int last_set_bit_in_run = -1;
295   int output_done = 0;
296
297   rv = sched_getaffinity (0 /* pid, 0 = this proc */ ,
298                           sizeof (*setp), setp);
299   if (rv < 0)
300     {
301       vlib_cli_output (vm, "Couldn't get affinity mask: %s\n",
302                        strerror (errno));
303       return 0;
304     }
305
306   for (i = 0; i < 64; i++)
307     {
308       if (CPU_ISSET (i, setp))
309         {
310           if (first_set_bit_in_run == -1)
311             {
312               first_set_bit_in_run = i;
313               last_set_bit_in_run = i;
314               if (output_done)
315                 s = format (s, ",");
316               s = format (s, "%d-", i);
317               output_done = 1;
318             }
319           else
320             {
321               if (i == (last_set_bit_in_run + 1))
322                 last_set_bit_in_run = i;
323             }
324         }
325       else
326         {
327           if (first_set_bit_in_run != -1)
328             {
329               if (first_set_bit_in_run == (i - 1))
330                 {
331                   _vec_len (s) -= 2 + ((first_set_bit_in_run / 10));
332                 }
333               s = format (s, "%d", last_set_bit_in_run);
334               first_set_bit_in_run = -1;
335               last_set_bit_in_run = -1;
336             }
337         }
338     }
339
340   if (first_set_bit_in_run != -1)
341     s = format (s, "%d", first_set_bit_in_run);
342
343   vlib_cli_output (vm, "Process runs on: %v", s);
344   return 0;
345 }
346
347 /* *INDENT-OFF* */
348 VLIB_CLI_COMMAND (show_affinity_command, static) = {
349   .path = "show affinity",
350   .short_help = "Show process cpu affinity",
351   .function = show_affinity,
352 };
353 /* *INDENT-ON* */
354
355 static clib_error_t *
356 set_affinity (vlib_main_t * vm,
357               unformat_input_t * input, vlib_cli_command_t * cmd)
358 {
359   cpu_set_t set;
360   cpu_set_t *setp = &set;
361   int i, rv;
362   int another_round;
363   u32 first, last;
364
365   memset (setp, 0, sizeof (*setp));
366
367   do
368     {
369       another_round = 0;
370       if (unformat (input, "%d-%d,", &first, &last))
371         {
372           if (first > 64 || last > 64)
373             {
374             barf1:
375               vlib_cli_output (vm, "range %d-%d invalid", first, last);
376               return 0;
377             }
378
379           for (i = first; i <= last; i++)
380             CPU_SET (i, setp);
381           another_round = 1;
382         }
383       else if (unformat (input, "%d-%d", &first, &last))
384         {
385           if (first > 64 || last > 64)
386             goto barf1;
387
388           for (i = first; i <= last; i++)
389             CPU_SET (i, setp);
390         }
391       else if (unformat (input, "%d,", &first))
392         {
393           if (first > 64)
394             {
395             barf2:
396               vlib_cli_output (vm, "cpu %d invalid", first);
397               return 0;
398             }
399           CPU_SET (first, setp);
400           another_round = 1;
401         }
402       else if (unformat (input, "%d", &first))
403         {
404           if (first > 64)
405             goto barf2;
406
407           CPU_SET (first, setp);
408         }
409     }
410   while (another_round);
411
412   rv = sched_setaffinity (0 /* pid, 0 = this proc */ ,
413                           sizeof (*setp), setp);
414
415   if (rv < 0)
416     {
417       vlib_cli_output (vm, "Couldn't get affinity mask: %s\n",
418                        strerror (errno));
419       return 0;
420     }
421   return show_affinity (vm, input, cmd);
422 }
423
424 /* *INDENT-OFF* */
425 VLIB_CLI_COMMAND (set_affinity_command, static) = {
426   .path = "set affinity",
427   .short_help = "Set process cpu affinity",
428   .function = set_affinity,
429 };
430 /* *INDENT-ON* */
431
432 static clib_error_t *
433 vlib_physmem_configure (vlib_main_t * vm, unformat_input_t * input)
434 {
435   physmem_main_t *pm = &physmem_main;
436   u32 size_in_mb;
437
438   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
439     {
440       if (unformat (input, "no-huge") || unformat (input, "no-huge-pages"))
441         pm->no_hugepages = 1;
442
443       else if (unformat (input, "size-in-mb %d", &size_in_mb) ||
444                unformat (input, "size %d", &size_in_mb))
445         pm->mem_size = size_in_mb << 20;
446       else
447         return unformat_parse_error (input);
448     }
449
450   unformat_free (input);
451   return 0;
452 }
453
454 VLIB_EARLY_CONFIG_FUNCTION (vlib_physmem_configure, "physmem");
455
456 /*
457  * fd.io coding-style-patch-verification: ON
458  *
459  * Local Variables:
460  * eval: (c-set-style "gnu")
461  * End:
462  */