first public release
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_memory.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <stdint.h>
41 #include <inttypes.h>
42 #include <string.h>
43 #include <stdarg.h>
44 #include <sys/mman.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/queue.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <limits.h>
51 #include <errno.h>
52 #include <sys/ioctl.h>
53
54 #include <rte_log.h>
55 #include <rte_memory.h>
56 #include <rte_memzone.h>
57 #include <rte_launch.h>
58 #include <rte_tailq.h>
59 #include <rte_eal.h>
60 #include <rte_per_lcore.h>
61 #include <rte_lcore.h>
62 #include <rte_common.h>
63 #include <rte_string_fns.h>
64
65 #include "eal_private.h"
66 #include "eal_internal_cfg.h"
67 #include "eal_fs_paths.h"
68 #include "eal_hugepages.h"
69
70 /**
71  * @file
72  * Huge page mapping under linux
73  *
74  * To reserve a big contiguous amount of memory, we use the hugepage
75  * feature of linux. For that, we need to have hugetlbfs mounted. This
76  * code will create many files in this directory (one per page) and
77  * map them in virtual memory. For each page, we will retrieve its
78  * physical address and remap it in order to have a virtual contiguous
79  * zone as well as a physical contiguous zone.
80  */
81
82
83 #define RANDOMIZE_VA_SPACE_FILE "/proc/sys/kernel/randomize_va_space"
84
85 /*
86  * Check whether address-space layout randomization is enabled in
87  * the kernel. This is important for multi-process as it can prevent
88  * two processes mapping data to the same virtual address
89  * Returns:
90  *    0 - address space randomization disabled
91  *    1/2 - address space randomization enabled
92  *    negative error code on error
93  */
94 static int
95 aslr_enabled(void)
96 {
97         char c;
98         int retval, fd = open(RANDOMIZE_VA_SPACE_FILE, O_RDONLY);
99         if (fd < 0)
100                 return -errno;
101         retval = read(fd, &c, 1);
102         close(fd);
103         if (retval < 0)
104                 return -errno;
105         if (retval == 0)
106                 return -EIO;
107         switch (c) {
108                 case '0' : return 0;
109                 case '1' : return 1;
110                 case '2' : return 2;
111                 default: return -EINVAL;
112         }
113 }
114
115 /*
116  * Try to mmap *size bytes in /dev/zero. If it is succesful, return the
117  * pointer to the mmap'd area and keep *size unmodified. Else, retry
118  * with a smaller zone: decrease *size by hugepage_sz until it reaches
119  * 0. In this case, return NULL. Note: this function returns an address
120  * which is a multiple of hugepage size.
121  */
122 static void *
123 get_virtual_area(uint64_t *size, uint64_t hugepage_sz)
124 {
125         void *addr;
126         int fd;
127         long aligned_addr;
128
129         RTE_LOG(INFO, EAL, "Ask a virtual area of 0x%"PRIx64" bytes\n", *size);
130
131         fd = open("/dev/zero", O_RDONLY);
132         if (fd < 0){
133                 RTE_LOG(ERR, EAL, "Cannot open /dev/zero\n");
134                 return NULL;
135         }
136         do {
137                 addr = mmap(NULL, (*size) + hugepage_sz, PROT_READ, MAP_PRIVATE, fd, 0);
138                 if (addr == MAP_FAILED)
139                         *size -= hugepage_sz;
140         } while (addr == MAP_FAILED && *size > 0);
141
142         if (addr == MAP_FAILED) {
143                 close(fd);
144                 RTE_LOG(INFO, EAL, "Cannot get a virtual area\n");
145                 return NULL;
146         }
147
148         munmap(addr, (*size) + hugepage_sz);
149         close(fd);
150
151         /* align addr to a huge page size boundary */
152         aligned_addr = (long)addr;
153         aligned_addr += (hugepage_sz - 1);
154         aligned_addr &= (~(hugepage_sz - 1));
155         addr = (void *)(aligned_addr);
156
157         RTE_LOG(INFO, EAL, "Virtual area found at %p (size = 0x%"PRIx64")\n",
158                 addr, *size);
159
160         return addr;
161 }
162
163 /*
164  * Mmap all hugepages of hugepage table: it first open a file in
165  * hugetlbfs, then mmap() hugepage_sz data in it. If orig is set, the
166  * virtual address is stored in hugepg_tbl[i].orig_va, else it is stored
167  * in hugepg_tbl[i].final_va. The second mapping (when orig is 0) tries to
168  * map continguous physical blocks in contiguous virtual blocks.
169  */
170 static int
171 map_all_hugepages(struct hugepage *hugepg_tbl,
172                 struct hugepage_info *hpi, int orig)
173 {
174         int fd;
175         unsigned i;
176         void *virtaddr;
177         void *vma_addr = NULL;
178         uint64_t vma_len = 0;
179
180         for (i = 0; i < hpi->num_pages; i++) {
181                 uint64_t hugepage_sz = hpi->hugepage_sz;
182
183                 if (orig) {
184                         hugepg_tbl[i].file_id = i;
185                         hugepg_tbl[i].size = hugepage_sz;
186                         eal_get_hugefile_path(hugepg_tbl[i].filepath,
187                                         sizeof(hugepg_tbl[i].filepath), hpi->hugedir,
188                                         hugepg_tbl[i].file_id);
189                         hugepg_tbl[i].filepath[sizeof(hugepg_tbl[i].filepath) - 1] = '\0';
190                 }
191 #ifndef RTE_ARCH_X86_64
192                 /* for 32-bit systems, don't remap 1G pages, just reuse original
193                  * map address as final map address.
194                  */
195                 else if (hugepage_sz == RTE_PGSIZE_1G){
196                         hugepg_tbl[i].final_va = hugepg_tbl[i].orig_va;
197                         hugepg_tbl[i].orig_va = NULL;
198                         continue;
199                 }
200 #endif
201                 else if (vma_len == 0) {
202                         unsigned j, num_pages;
203
204                         /* reserve a virtual area for next contiguous
205                          * physical block: count the number of
206                          * contiguous physical pages. */
207                         for (j = i+1; j < hpi->num_pages ; j++) {
208                                 if (hugepg_tbl[j].physaddr !=
209                                     hugepg_tbl[j-1].physaddr + hugepage_sz)
210                                         break;
211                         }
212                         num_pages = j - i;
213                         vma_len = num_pages * hugepage_sz;
214
215                         /* get the biggest virtual memory area up to
216                          * vma_len. If it fails, vma_addr is NULL, so
217                          * let the kernel provide the address. */
218                         vma_addr = get_virtual_area(&vma_len, hpi->hugepage_sz);
219                         if (vma_addr == NULL)
220                                 vma_len = hugepage_sz;
221                 }
222
223                 fd = open(hugepg_tbl[i].filepath, O_CREAT | O_RDWR, 0755);
224                 if (fd < 0) {
225                         RTE_LOG(ERR, EAL, "%s(): open failed: %s", __func__,
226                                         strerror(errno));
227                         return -1;
228                 }
229
230                 virtaddr = mmap(vma_addr, hugepage_sz, PROT_READ | PROT_WRITE,
231                                 MAP_SHARED, fd, 0);
232                 if (virtaddr == MAP_FAILED) {
233                         RTE_LOG(ERR, EAL, "%s(): mmap failed: %s", __func__,
234                                         strerror(errno));
235                         close(fd);
236                         return -1;
237                 }
238                 if (orig) {
239                         hugepg_tbl[i].orig_va = virtaddr;
240                         memset(virtaddr, 0, hugepage_sz);
241                 }
242                 else {
243                         hugepg_tbl[i].final_va = virtaddr;
244                 }
245
246                 vma_addr = (char *)vma_addr + hugepage_sz;
247                 vma_len -= hugepage_sz;
248                 close(fd);
249         }
250         return 0;
251 }
252
253 /* Unmap all hugepages from original mapping. */
254 static int
255 unmap_all_hugepages_orig(struct hugepage *hugepg_tbl, struct hugepage_info *hpi)
256 {
257         unsigned i;
258         for (i = 0; i < hpi->num_pages; i++) {
259                 if (hugepg_tbl[i].orig_va) {
260                         munmap(hugepg_tbl[i].orig_va, hpi->hugepage_sz);
261                         hugepg_tbl[i].orig_va = NULL;
262                 }
263         }
264         return 0;
265 }
266
267 /*
268  * For each hugepage in hugepg_tbl, fill the physaddr value. We find
269  * it by browsing the /proc/self/pagemap special file.
270  */
271 static int
272 find_physaddr(struct hugepage *hugepg_tbl, struct hugepage_info *hpi)
273 {
274         int fd;
275         unsigned i;
276         uint64_t page;
277         unsigned long virt_pfn;
278         int page_size;
279
280         /* standard page size */
281         page_size = getpagesize();
282
283         fd = open("/proc/self/pagemap", O_RDONLY);
284         if (fd < 0) {
285                 RTE_LOG(ERR, EAL, "%s(): cannot open /proc/self/pagemap: %s",
286                         __func__, strerror(errno));
287                 return -1;
288         }
289
290         for (i = 0; i < hpi->num_pages; i++) {
291                 off_t offset;
292                 virt_pfn = (unsigned long)hugepg_tbl[i].orig_va /
293                         page_size;
294                 offset = sizeof(uint64_t) * virt_pfn;
295                 if (lseek(fd, offset, SEEK_SET) != offset){
296                         RTE_LOG(ERR, EAL, "%s(): seek error in /proc/self/pagemap: %s",
297                                         __func__, strerror(errno));
298                         close(fd);
299                         return -1;
300                 }
301                 if (read(fd, &page, sizeof(uint64_t)) < 0) {
302                         RTE_LOG(ERR, EAL, "%s(): cannot read /proc/self/pagemap: %s",
303                                         __func__, strerror(errno));
304                         close(fd);
305                         return -1;
306                 }
307
308                 /*
309                  * the pfn (page frame number) are bits 0-54 (see
310                  * pagemap.txt in linux Documentation)
311                  */
312                 hugepg_tbl[i].physaddr = ((page & 0x7fffffffffffffULL) * page_size);
313         }
314         close(fd);
315         return 0;
316 }
317
318 /*
319  * Parse /proc/self/numa_maps to get the NUMA socket ID for each huge
320  * page.
321  */
322 static int
323 find_numasocket(struct hugepage *hugepg_tbl, struct hugepage_info *hpi)
324 {
325         int socket_id;
326         char *end, *nodestr;
327         unsigned i, hp_count = 0;
328         uint64_t virt_addr;
329         char buf[BUFSIZ];
330         char hugedir_str[PATH_MAX];
331         FILE *f;
332
333         f = fopen("/proc/self/numa_maps", "r");
334         if (f == NULL) {
335                 RTE_LOG(INFO, EAL, "cannot open /proc/self/numa_maps,"
336                                 "consider that all memory is in socket_id 0");
337                 return 0;
338         }
339
340         rte_snprintf(hugedir_str, sizeof(hugedir_str),
341                         "%s/", hpi->hugedir);
342
343         /* parse numa map */
344         while (fgets(buf, sizeof(buf), f) != NULL) {
345
346                 /* ignore non huge page */
347                 if (strstr(buf, " huge ") == NULL &&
348                                 strstr(buf, hugedir_str) == NULL)
349                         continue;
350
351                 /* get zone addr */
352                 virt_addr = strtoull(buf, &end, 16);
353                 if (virt_addr == 0 || end == buf) {
354                         RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
355                         goto error;
356                 }
357
358                 /* get node id (socket id) */
359                 nodestr = strstr(buf, " N");
360                 if (nodestr == NULL) {
361                         RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
362                         goto error;
363                 }
364                 nodestr += 2;
365                 end = strstr(nodestr, "=");
366                 if (end == NULL) {
367                         RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
368                         goto error;
369                 }
370                 end[0] = '\0';
371                 end = NULL;
372
373                 socket_id = strtoul(nodestr, &end, 0);
374                 if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) {
375                         RTE_LOG(ERR, EAL, "%s(): error in numa_maps parsing\n", __func__);
376                         goto error;
377                 }
378
379                 /* if we find this page in our mappings, set socket_id */
380                 for (i = 0; i < hpi->num_pages; i++) {
381                         void *va = (void *)(unsigned long)virt_addr;
382                         if (hugepg_tbl[i].orig_va == va) {
383                                 hugepg_tbl[i].socket_id = socket_id;
384                                 hp_count++;
385                         }
386                 }
387         }
388         if (hp_count < hpi->num_pages)
389                 goto error;
390         fclose(f);
391         return 0;
392
393 error:
394         fclose(f);
395         return -1;
396 }
397
398 /*
399  * Sort the hugepg_tbl by physical address (lower addresses first). We
400  * use a slow algorithm, but we won't have millions of pages, and this
401  * is only done at init time.
402  */
403 static int
404 sort_by_physaddr(struct hugepage *hugepg_tbl, struct hugepage_info *hpi)
405 {
406         unsigned i, j;
407         int smallest_idx;
408         uint64_t smallest_addr;
409         struct hugepage tmp;
410
411         for (i = 0; i < hpi->num_pages; i++) {
412                 smallest_addr = 0;
413                 smallest_idx = -1;
414
415                 /*
416                  * browse all entries starting at 'i', and find the
417                  * entry with the smallest addr
418                  */
419                 for (j=i; j<hpi->num_pages; j++) {
420
421                         if (smallest_addr == 0 ||
422                             hugepg_tbl[j].physaddr < smallest_addr) {
423                                 smallest_addr = hugepg_tbl[j].physaddr;
424                                 smallest_idx = j;
425                         }
426                 }
427
428                 /* should not happen */
429                 if (smallest_idx == -1) {
430                         RTE_LOG(ERR, EAL, "%s(): error in physaddr sorting\n", __func__);
431                         return -1;
432                 }
433
434                 /* swap the 2 entries in the table */
435                 memcpy(&tmp, &hugepg_tbl[smallest_idx], sizeof(struct hugepage));
436                 memcpy(&hugepg_tbl[smallest_idx], &hugepg_tbl[i],
437                                 sizeof(struct hugepage));
438                 memcpy(&hugepg_tbl[i], &tmp, sizeof(struct hugepage));
439         }
440         return 0;
441 }
442
443 /*
444  * Uses mmap to create a shared memory area for storage of data
445  *Used in this file to store the hugepage file map on disk
446  */
447 static void *
448 create_shared_memory(const char *filename, const size_t mem_size)
449 {
450         void *retval;
451         int fd = open(filename, O_CREAT | O_RDWR, 0666);
452         if (fd < 0)
453                 return NULL;
454         if (ftruncate(fd, mem_size) < 0) {
455                 close(fd);
456                 return NULL;
457         }
458         retval = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
459         close(fd);
460         return retval;
461 }
462
463 /*
464  * This function takes in the list of hugepage sizes and the
465  * number of pages thereof, and calculates the best number of
466  * pages of each size to fulfill the request for <memory> ram
467  */
468 static int
469 calc_num_pages(uint64_t memory,
470                 struct hugepage_info *hp_info,
471                 struct hugepage_info *hp_used,
472                 unsigned num_hp_info)
473 {
474         unsigned i = 0;
475         int total_num_pages = 0;
476         if (num_hp_info == 0)
477                 return -1;
478
479         for (i = 0; i < num_hp_info; i++){
480                 hp_used[i].hugepage_sz = hp_info[i].hugepage_sz;
481                 hp_used[i].hugedir = hp_info[i].hugedir;
482                 hp_used[i].num_pages = RTE_MIN(memory / hp_info[i].hugepage_sz,
483                                 hp_info[i].num_pages);
484
485                 memory -= hp_used[i].num_pages * hp_used[i].hugepage_sz;
486                 total_num_pages += hp_used[i].num_pages;
487
488                 /* check if we have met all memory requests */
489                 if (memory == 0)
490                         break;
491                 /* check if we have any more pages left at this size, if so
492                  * move on to next size */
493                 if (hp_used[i].num_pages == hp_info[i].num_pages)
494                         continue;
495                 /* At this point we know that there are more pages available that are
496                  * bigger than the memory we want, so lets see if we can get enough
497                  * from other page sizes.
498                  */
499                 unsigned j;
500                 uint64_t remaining_mem = 0;
501                 for (j = i+1; j < num_hp_info; j++)
502                         remaining_mem += hp_info[j].hugepage_sz * hp_info[j].num_pages;
503
504                 /* is there enough other memory, if not allocate another page and quit*/
505                 if (remaining_mem < memory){
506                         memory -= hp_info[i].hugepage_sz;
507                         hp_used[i].num_pages++;
508                         total_num_pages++;
509                         break; /* we are done */
510                 }
511         }
512         return total_num_pages;
513 }
514
515 /*
516  * Prepare physical memory mapping: fill configuration structure with
517  * these infos, return 0 on success.
518  *  1. map N huge pages in separate files in hugetlbfs
519  *  2. find associated physical addr
520  *  3. find associated NUMA socket ID
521  *  4. sort all huge pages by physical address
522  *  5. remap these N huge pages in the correct order
523  *  6. unmap the first mapping
524  *  7. fill memsegs in configuration with contiguous zones
525  */
526 static int
527 rte_eal_hugepage_init(void)
528 {
529         struct rte_mem_config *mcfg;
530         struct hugepage *hugepage;
531         struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];
532         int i, j, new_memseg;
533         int nrpages;
534         void *addr;
535
536         memset(used_hp, 0, sizeof(used_hp));
537
538         /* get pointer to global configuration */
539         mcfg = rte_eal_get_configuration()->mem_config;
540
541         /* for debug purposes, hugetlbfs can be disabled */
542         if (internal_config.no_hugetlbfs) {
543                 addr = malloc(internal_config.memory);
544                 mcfg->memseg[0].phys_addr = (unsigned long)addr;
545                 mcfg->memseg[0].addr = addr;
546                 mcfg->memseg[0].len = internal_config.memory;
547                 mcfg->memseg[0].socket_id = 0;
548                 return 0;
549         }
550
551         nrpages = calc_num_pages(internal_config.memory,
552                         &internal_config.hugepage_info[0], &used_hp[0],
553                         internal_config.num_hugepage_sizes);
554         for (i = 0; i < (int)internal_config.num_hugepage_sizes; i++)
555                 RTE_LOG(INFO, EAL, "Requesting %u pages of size %"PRIu64"\n",
556                                 used_hp[i].num_pages, used_hp[i].hugepage_sz);
557
558         hugepage = create_shared_memory(eal_hugepage_info_path(),
559                         nrpages * sizeof(struct hugepage));
560         if (hugepage == NULL)
561                 return -1;
562         memset(hugepage, 0, nrpages * sizeof(struct hugepage));
563
564         unsigned hp_offset = 0; /* where we start the current page size entries */
565         for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
566                 struct hugepage_info *hpi = &used_hp[i];
567                 if (hpi->num_pages == 0)
568                         continue;
569
570                 if (map_all_hugepages(&hugepage[hp_offset], hpi, 1) < 0){
571                         RTE_LOG(DEBUG, EAL, "Failed to mmap %u MB hugepages\n",
572                                         (unsigned)(hpi->hugepage_sz / 0x100000));
573                         goto fail;
574                 }
575
576                 if (find_physaddr(&hugepage[hp_offset], hpi) < 0){
577                         RTE_LOG(DEBUG, EAL, "Failed to find phys addr for %u MB pages\n",
578                                         (unsigned)(hpi->hugepage_sz / 0x100000));
579                         goto fail;
580                 }
581
582                 if (find_numasocket(&hugepage[hp_offset], hpi) < 0){
583                         RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
584                                         (unsigned)(hpi->hugepage_sz / 0x100000));
585                         goto fail;
586                 }
587
588                 if (sort_by_physaddr(&hugepage[hp_offset], hpi) < 0)
589                         goto fail;
590
591                 if (map_all_hugepages(&hugepage[hp_offset], hpi, 0) < 0){
592                         RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
593                                         (unsigned)(hpi->hugepage_sz / 0x100000));
594                         goto fail;
595                 }
596
597                 if (unmap_all_hugepages_orig(&hugepage[hp_offset], hpi) < 0)
598                         goto fail;
599
600                 /* we have processed a num of hugepages of this size, so inc offset */
601                 hp_offset += hpi->num_pages;
602         }
603
604         memset(mcfg->memseg, 0, sizeof(mcfg->memseg));
605         j = -1;
606         for (i = 0; i < nrpages; i++) {
607                 new_memseg = 0;
608
609                 /* if this is a new section, create a new memseg */
610                 if (i == 0)
611                         new_memseg = 1;
612                 else if (hugepage[i].socket_id != hugepage[i-1].socket_id)
613                         new_memseg = 1;
614                 else if (hugepage[i].size != hugepage[i-1].size)
615                         new_memseg = 1;
616                 else if ((hugepage[i].physaddr - hugepage[i-1].physaddr) !=
617                          hugepage[i].size)
618                         new_memseg = 1;
619                 else if (((unsigned long)hugepage[i].final_va -
620                      (unsigned long)hugepage[i-1].final_va) != hugepage[i].size)
621                         new_memseg = 1;
622
623                 if (new_memseg) {
624                         j += 1;
625                         if (j == RTE_MAX_MEMSEG)
626                                 break;
627
628                         mcfg->memseg[j].phys_addr = hugepage[i].physaddr;
629                         mcfg->memseg[j].addr = hugepage[i].final_va;
630                         mcfg->memseg[j].len = hugepage[i].size;
631                         mcfg->memseg[j].socket_id = hugepage[i].socket_id;
632                         mcfg->memseg[j].hugepage_sz = hugepage[i].size;
633                 }
634                 /* continuation of previous memseg */
635                 else {
636                         mcfg->memseg[j].len += mcfg->memseg[j].hugepage_sz;
637                 }
638                 hugepage[i].memseg_id = j;
639         }
640
641         return 0;
642
643
644  fail:
645         return -1;
646 }
647
648 /*
649  * uses fstat to report the size of a file on disk
650  */
651 static off_t
652 getFileSize(int fd)
653 {
654         struct stat st;
655         if (fstat(fd, &st) < 0)
656                 return 0;
657         return st.st_size;
658 }
659
660 /*
661  * This creates the memory mappings in the secondary process to match that of
662  * the server process. It goes through each memory segment in the DPDK runtime
663  * configuration and finds the hugepages which form that segment, mapping them
664  * in order to form a contiguous block in the virtual memory space
665  */
666 static int
667 rte_eal_hugepage_attach(void)
668 {
669         const struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
670         const struct hugepage *hp = NULL;
671         unsigned num_hp = 0;
672         unsigned i, s = 0; /* s used to track the segment number */
673         off_t size;
674         int fd, fd_zero = -1, fd_hugepage = -1;
675
676         if (aslr_enabled() > 0) {
677                 RTE_LOG(WARNING, EAL, "WARNING: Address Space Layout Randomization "
678                                 "(ASLR) is enabled in the kernel.\n");
679                 RTE_LOG(WARNING, EAL, "   This may cause issues with mapping memory "
680                                 "into secondary processes\n");
681         }
682
683         fd_zero = open("/dev/zero", O_RDONLY);
684         if (fd_zero < 0) {
685                 RTE_LOG(ERR, EAL, "Could not open /dev/zero\n");
686                 goto error;
687         }
688         fd_hugepage = open(eal_hugepage_info_path(), O_RDONLY);
689         if (fd_hugepage < 0) {
690                 RTE_LOG(ERR, EAL, "Could not open %s\n", eal_hugepage_info_path());
691                 goto error;
692         }
693
694         size = getFileSize(fd_hugepage);
695         hp = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd_hugepage, 0);
696         if (hp == NULL) {
697                 RTE_LOG(ERR, EAL, "Could not mmap %s\n", eal_hugepage_info_path());
698                 goto error;
699         }
700
701         num_hp = size / sizeof(struct hugepage);
702         RTE_LOG(DEBUG, EAL, "Analysing %u hugepages\n", num_hp);
703
704         while (s < RTE_MAX_MEMSEG && mcfg->memseg[s].len > 0){
705                 void *addr, *base_addr;
706                 uintptr_t offset = 0;
707
708                 /* fdzero is mmapped to get a contiguous block of virtual addresses
709                  * get a block of free memory of the appropriate size -
710                  * use mmap to attempt to get an identical address as server.
711                  */
712                 base_addr = mmap(mcfg->memseg[s].addr, mcfg->memseg[s].len,
713                                 PROT_READ, MAP_PRIVATE, fd_zero, 0);
714                 if (base_addr == MAP_FAILED || base_addr != mcfg->memseg[s].addr) {
715                         RTE_LOG(ERR, EAL, "Could not mmap %llu bytes "
716                                 "in /dev/zero to requested address [%p]\n",
717                                 (unsigned long long)mcfg->memseg[s].len,
718                                 mcfg->memseg[s].addr);
719                         if (aslr_enabled() > 0)
720                                 RTE_LOG(ERR, EAL, "It is recommended to disable ASLR in the kernel "
721                                                 "and retry running both primary and secondary processes\n");
722                         goto error;
723                 }
724                 /* free memory so we can map the hugepages into the space */
725                 munmap(base_addr, mcfg->memseg[s].len);
726
727                 /* find the hugepages for this segment and map them
728                  * we don't need to worry about order, as the server sorted the
729                  * entries before it did the second mmap of them */
730                 for (i = 0; i < num_hp && offset < mcfg->memseg[s].len; i++){
731                         if (hp[i].memseg_id == (int)s){
732                                 fd = open(hp[i].filepath, O_RDWR);
733                                 if (fd < 0) {
734                                         RTE_LOG(ERR, EAL, "Could not open %s\n",
735                                                 hp[i].filepath);
736                                         goto error;
737                                 }
738                                 addr = mmap(RTE_PTR_ADD(base_addr, offset),
739                                                 hp[i].size, PROT_READ | PROT_WRITE,
740                                                 MAP_SHARED | MAP_FIXED, fd, 0);
741                                 close(fd); /* close file both on success and on failure */
742                                 if (addr == MAP_FAILED) {
743                                         RTE_LOG(ERR, EAL, "Could not mmap %s\n",
744                                                 hp[i].filepath);
745                                         goto error;
746                                 }
747                                 offset+=hp[i].size;
748                         }
749                 }
750                 RTE_LOG(DEBUG, EAL, "Mapped segment %u of size 0x%llx\n", s,
751                                 (unsigned long long)mcfg->memseg[s].len);
752                 s++;
753         }
754         close(fd_zero);
755         close(fd_hugepage);
756         return 0;
757
758 error:
759         if (fd_zero >= 0)
760                 close(fd_zero);
761         if (fd_hugepage >= 0)
762                 close(fd_hugepage);
763         return -1;
764 }
765
766 static int
767 rte_eal_memdevice_init(void)
768 {
769         struct rte_config *config;
770
771         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
772                 return 0;
773
774         config = rte_eal_get_configuration();
775         config->mem_config->nchannel = internal_config.force_nchannel;
776         config->mem_config->nrank = internal_config.force_nrank;
777
778         return 0;
779 }
780
781
782 /* init memory subsystem */
783 int
784 rte_eal_memory_init(void)
785 {
786         const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
787                         rte_eal_hugepage_init() :
788                         rte_eal_hugepage_attach();
789         if (retval < 0)
790                 return -1;
791
792         if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
793                 return -1;
794
795         return 0;
796 }