4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifdef RTE_LIBRTE_IVSHMEM /* hide it from coverage */
42 #include <sys/queue.h>
46 #include <rte_memory.h>
48 #include <rte_eal_memconfig.h>
49 #include <rte_string_fns.h>
50 #include <rte_errno.h>
52 #include <rte_mempool.h>
53 #include <rte_malloc.h>
54 #include <rte_common.h>
55 #include <rte_ivshmem.h>
57 #include "eal_internal_cfg.h"
58 #include "eal_private.h"
60 #define PCI_VENDOR_ID_IVSHMEM 0x1Af4
61 #define PCI_DEVICE_ID_IVSHMEM 0x1110
63 #define IVSHMEM_MAGIC 0x0BADC0DE
65 #define IVSHMEM_RESOURCE_PATH "/sys/bus/pci/devices/%04x:%02x:%02x.%x/resource2"
66 #define IVSHMEM_CONFIG_PATH "/var/run/.%s_ivshmem_config"
71 #define FULL (PHYS|VIRT|IOREMAP)
73 #define METADATA_SIZE_ALIGNED \
74 (RTE_ALIGN_CEIL(sizeof(struct rte_ivshmem_metadata),pagesz))
76 #define CONTAINS(x,y)\
77 (((y).addr_64 >= (x).addr_64) && ((y).addr_64 < (x).addr_64 + (x).len))
79 #define DIM(x) (sizeof(x)/sizeof(x[0]))
81 struct ivshmem_pci_device {
83 phys_addr_t ioremap_addr;
86 /* data type to store in config */
87 struct ivshmem_segment {
88 struct rte_ivshmem_metadata_entry entry;
92 struct ivshmem_shared_config {
93 struct ivshmem_segment segment[RTE_MAX_MEMSEG];
95 struct ivshmem_pci_device pci_devs[RTE_LIBRTE_IVSHMEM_MAX_PCI_DEVS];
96 uint32_t pci_devs_idx;
98 static struct ivshmem_shared_config * ivshmem_config;
99 static int memseg_idx;
102 /* Tailq heads to add rings to */
103 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
110 is_ivshmem_device(struct rte_pci_device * dev)
112 return dev->id.vendor_id == PCI_VENDOR_ID_IVSHMEM
113 && dev->id.device_id == PCI_DEVICE_ID_IVSHMEM;
117 map_metadata(int fd, uint64_t len)
119 size_t metadata_len = sizeof(struct rte_ivshmem_metadata);
120 size_t aligned_len = METADATA_SIZE_ALIGNED;
122 return mmap(NULL, metadata_len, PROT_READ | PROT_WRITE,
123 MAP_SHARED, fd, len - aligned_len);
127 unmap_metadata(void * ptr)
129 munmap(ptr, sizeof(struct rte_ivshmem_metadata));
133 has_ivshmem_metadata(int fd, uint64_t len)
135 struct rte_ivshmem_metadata metadata;
138 ptr = map_metadata(fd, len);
140 if (ptr == MAP_FAILED)
143 metadata = *(struct rte_ivshmem_metadata*) (ptr);
147 return metadata.magic_number == IVSHMEM_MAGIC;
151 remove_segment(struct ivshmem_segment * ms, int len, int idx)
155 for (i = idx; i < len - 1; i++)
156 memcpy(&ms[i], &ms[i+1], sizeof(struct ivshmem_segment));
157 memset(&ms[len-1], 0, sizeof(struct ivshmem_segment));
161 overlap(const struct rte_memzone * mz1, const struct rte_memzone * mz2)
163 uint64_t start1, end1, start2, end2;
164 uint64_t p_start1, p_end1, p_start2, p_end2;
165 uint64_t i_start1, i_end1, i_start2, i_end2;
168 /* gather virtual addresses */
169 start1 = mz1->addr_64;
170 end1 = mz1->addr_64 + mz1->len;
171 start2 = mz2->addr_64;
172 end2 = mz2->addr_64 + mz2->len;
174 /* gather physical addresses */
175 p_start1 = mz1->phys_addr;
176 p_end1 = mz1->phys_addr + mz1->len;
177 p_start2 = mz2->phys_addr;
178 p_end2 = mz2->phys_addr + mz2->len;
180 /* gather ioremap addresses */
181 i_start1 = mz1->ioremap_addr;
182 i_end1 = mz1->ioremap_addr + mz1->len;
183 i_start2 = mz2->ioremap_addr;
184 i_end2 = mz2->ioremap_addr + mz2->len;
186 /* check for overlap in virtual addresses */
187 if (start1 > start2 && start1 < end2)
189 if (start2 >= start1 && start2 < end1)
192 /* check for overlap in physical addresses */
193 if (p_start1 > p_start2 && p_start1 < p_end2)
195 if (p_start2 > p_start1 && p_start2 < p_end1)
198 /* check for overlap in ioremap addresses */
199 if (i_start1 > i_start2 && i_start1 < i_end2)
201 if (i_start2 > i_start1 && i_start2 < i_end1)
208 adjacent(const struct rte_memzone * mz1, const struct rte_memzone * mz2)
210 uint64_t start1, end1, start2, end2;
211 uint64_t p_start1, p_end1, p_start2, p_end2;
212 uint64_t i_start1, i_end1, i_start2, i_end2;
215 /* gather virtual addresses */
216 start1 = mz1->addr_64;
217 end1 = mz1->addr_64 + mz1->len;
218 start2 = mz2->addr_64;
219 end2 = mz2->addr_64 + mz2->len;
221 /* gather physical addresses */
222 p_start1 = mz1->phys_addr;
223 p_end1 = mz1->phys_addr + mz1->len;
224 p_start2 = mz2->phys_addr;
225 p_end2 = mz2->phys_addr + mz2->len;
227 /* gather ioremap addresses */
228 i_start1 = mz1->ioremap_addr;
229 i_end1 = mz1->ioremap_addr + mz1->len;
230 i_start2 = mz2->ioremap_addr;
231 i_end2 = mz2->ioremap_addr + mz2->len;
233 /* check if segments are virtually adjacent */
239 /* check if segments are physically adjacent */
240 if (p_start1 == p_end2)
242 if (p_start2 == p_end1)
245 /* check if segments are ioremap-adjacent */
246 if (i_start1 == i_end2)
248 if (i_start2 == i_end1)
255 has_adjacent_segments(struct ivshmem_segment * ms, int len)
259 for (i = 0; i < len; i++)
260 for (j = i + 1; j < len; j++) {
261 /* we're only interested in fully adjacent segments; partially
262 * adjacent segments can coexist.
264 if (adjacent(&ms[i].entry.mz, &ms[j].entry.mz) == FULL)
271 has_overlapping_segments(struct ivshmem_segment * ms, int len)
275 for (i = 0; i < len; i++)
276 for (j = i + 1; j < len; j++)
277 if (overlap(&ms[i].entry.mz, &ms[j].entry.mz))
283 seg_compare(const void * a, const void * b)
285 const struct ivshmem_segment * s1 = (const struct ivshmem_segment*) a;
286 const struct ivshmem_segment * s2 = (const struct ivshmem_segment*) b;
288 /* move unallocated zones to the end */
289 if (s1->entry.mz.addr == NULL && s2->entry.mz.addr == NULL)
291 if (s1->entry.mz.addr == 0)
293 if (s2->entry.mz.addr == 0)
296 return s1->entry.mz.phys_addr > s2->entry.mz.phys_addr;
299 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
301 entry_dump(struct rte_ivshmem_metadata_entry *e)
303 RTE_LOG(DEBUG, EAL, "\tvirt: %p-%p\n", e->mz.addr,
304 RTE_PTR_ADD(e->mz.addr, e->mz.len));
305 RTE_LOG(DEBUG, EAL, "\tphys: 0x%" PRIx64 "-0x%" PRIx64 "\n",
307 e->mz.phys_addr + e->mz.len);
308 RTE_LOG(DEBUG, EAL, "\tio: 0x%" PRIx64 "-0x%" PRIx64 "\n",
310 e->mz.ioremap_addr + e->mz.len);
311 RTE_LOG(DEBUG, EAL, "\tlen: 0x%" PRIx64 "\n", e->mz.len);
312 RTE_LOG(DEBUG, EAL, "\toff: 0x%" PRIx64 "\n", e->offset);
322 /* read through metadata mapped from the IVSHMEM device */
324 read_metadata(char * path, int path_len, int fd, uint64_t flen)
326 struct rte_ivshmem_metadata metadata;
327 struct rte_ivshmem_metadata_entry * entry;
331 ptr = map_metadata(fd, flen);
333 if (ptr == MAP_FAILED)
336 metadata = *(struct rte_ivshmem_metadata*) (ptr);
340 RTE_LOG(DEBUG, EAL, "Parsing metadata for \"%s\"\n", metadata.name);
342 idx = ivshmem_config->segment_idx;
344 for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_ENTRIES &&
345 idx <= RTE_MAX_MEMSEG; i++) {
347 if (idx == RTE_MAX_MEMSEG) {
348 RTE_LOG(ERR, EAL, "Not enough memory segments!\n");
352 entry = &metadata.entry[i];
354 /* stop on uninitialized memzone */
355 if (entry->mz.len == 0)
358 /* copy metadata entry */
359 memcpy(&ivshmem_config->segment[idx].entry, entry,
360 sizeof(struct rte_ivshmem_metadata_entry));
363 snprintf(ivshmem_config->segment[idx].path, path_len, "%s", path);
367 ivshmem_config->segment_idx = idx;
372 /* check through each segment and look for adjacent or overlapping ones. */
374 cleanup_segments(struct ivshmem_segment * ms, int tbl_len)
376 struct ivshmem_segment * s, * tmp;
377 int i, j, concat, seg_adjacent, seg_overlapping;
378 uint64_t start1, start2, end1, end2, p_start1, p_start2, i_start1, i_start2;
380 qsort(ms, tbl_len, sizeof(struct ivshmem_segment),
383 while (has_overlapping_segments(ms, tbl_len) ||
384 has_adjacent_segments(ms, tbl_len)) {
386 for (i = 0; i < tbl_len; i++) {
391 for (j = i + 1; j < tbl_len; j++) {
394 /* check if this segment is overlapping with existing segment,
395 * or is adjacent to existing segment */
396 seg_overlapping = overlap(&s->entry.mz, &tmp->entry.mz);
397 seg_adjacent = adjacent(&s->entry.mz, &tmp->entry.mz);
399 /* check if segments fully overlap or are fully adjacent */
400 if ((seg_adjacent == FULL) || (seg_overlapping == FULL)) {
402 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
403 RTE_LOG(DEBUG, EAL, "Concatenating segments\n");
404 RTE_LOG(DEBUG, EAL, "Segment %i:\n", i);
405 entry_dump(&s->entry);
406 RTE_LOG(DEBUG, EAL, "Segment %i:\n", j);
407 entry_dump(&tmp->entry);
410 start1 = s->entry.mz.addr_64;
411 start2 = tmp->entry.mz.addr_64;
412 p_start1 = s->entry.mz.phys_addr;
413 p_start2 = tmp->entry.mz.phys_addr;
414 i_start1 = s->entry.mz.ioremap_addr;
415 i_start2 = tmp->entry.mz.ioremap_addr;
416 end1 = s->entry.mz.addr_64 + s->entry.mz.len;
417 end2 = tmp->entry.mz.addr_64 + tmp->entry.mz.len;
419 /* settle for minimum start address and maximum length */
420 s->entry.mz.addr_64 = RTE_MIN(start1, start2);
421 s->entry.mz.phys_addr = RTE_MIN(p_start1, p_start2);
422 s->entry.mz.ioremap_addr = RTE_MIN(i_start1, i_start2);
423 s->entry.offset = RTE_MIN(s->entry.offset, tmp->entry.offset);
424 s->entry.mz.len = RTE_MAX(end1, end2) - s->entry.mz.addr_64;
427 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
428 RTE_LOG(DEBUG, EAL, "Resulting segment:\n");
429 entry_dump(&s->entry);
433 /* if segments not fully overlap, we have an error condition.
434 * adjacent segments can coexist.
436 else if (seg_overlapping > 0) {
437 RTE_LOG(ERR, EAL, "Segments %i and %i overlap!\n", i, j);
438 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
439 RTE_LOG(DEBUG, EAL, "Segment %i:\n", i);
440 entry_dump(&s->entry);
441 RTE_LOG(DEBUG, EAL, "Segment %i:\n", j);
442 entry_dump(&tmp->entry);
449 /* if we concatenated, remove segment at j */
451 remove_segment(ms, tbl_len, j);
462 create_shared_config(void)
467 /* build ivshmem config file path */
468 snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
469 internal_config.hugefile_prefix);
471 fd = open(path, O_CREAT | O_RDWR, 0600);
474 RTE_LOG(ERR, EAL, "Could not open %s: %s\n", path, strerror(errno));
478 /* try ex-locking first - if the file is locked, we have a problem */
479 if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
480 RTE_LOG(ERR, EAL, "Locking %s failed: %s\n", path, strerror(errno));
485 if (ftruncate(fd, sizeof(struct ivshmem_shared_config)) < 0) {
486 RTE_LOG(ERR, EAL, "ftruncate failed: %s\n", strerror(errno));
490 ivshmem_config = mmap(NULL, sizeof(struct ivshmem_shared_config),
491 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
493 if (ivshmem_config == MAP_FAILED)
496 memset(ivshmem_config, 0, sizeof(struct ivshmem_shared_config));
498 /* change the exclusive lock we got earlier to a shared lock */
499 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
500 RTE_LOG(ERR, EAL, "Locking %s failed: %s \n", path, strerror(errno));
509 /* open shared config file and, if present, map the config.
510 * having no config file is not an error condition, as we later check if
511 * ivshmem_config is NULL (if it is, that means nothing was mapped). */
513 open_shared_config(void)
518 /* build ivshmem config file path */
519 snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
520 internal_config.hugefile_prefix);
522 fd = open(path, O_RDONLY);
524 /* if the file doesn't exist, just return success */
525 if (fd < 0 && errno == ENOENT)
527 /* else we have an error condition */
529 RTE_LOG(ERR, EAL, "Could not open %s: %s\n",
530 path, strerror(errno));
534 /* try ex-locking first - if the lock *does* succeed, this means it's a
535 * stray config file, so it should be deleted.
537 if (flock(fd, LOCK_EX | LOCK_NB) != -1) {
539 /* if we can't remove the file, something is wrong */
540 if (unlink(path) < 0) {
541 RTE_LOG(ERR, EAL, "Could not remove %s: %s\n", path,
546 /* release the lock */
550 /* return success as having a stray config file is equivalent to not
551 * having config file at all.
556 ivshmem_config = mmap(NULL, sizeof(struct ivshmem_shared_config),
557 PROT_READ, MAP_SHARED, fd, 0);
559 if (ivshmem_config == MAP_FAILED)
562 /* place a shared lock on config file */
563 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
564 RTE_LOG(ERR, EAL, "Locking %s failed: %s \n", path, strerror(errno));
574 * This function does the following:
576 * 1) Builds a table of ivshmem_segments with proper offset alignment
577 * 2) Cleans up that table so that we don't have any overlapping or adjacent
579 * 3) Creates memsegs from this table and maps them into memory.
582 map_all_segments(void)
584 struct ivshmem_segment ms_tbl[RTE_MAX_MEMSEG];
585 struct ivshmem_pci_device * pci_dev;
586 struct rte_mem_config * mcfg;
587 struct ivshmem_segment * seg;
590 struct rte_memzone mz;
591 struct rte_memseg ms;
594 phys_addr_t ioremap_addr;
598 memset(ms_tbl, 0, sizeof(ms_tbl));
599 memset(&mz, 0, sizeof(struct rte_memzone));
600 memset(&ms, 0, sizeof(struct rte_memseg));
602 /* first, build a table of memsegs to map, to avoid failed mmaps due to
605 for (i = 0; i < ivshmem_config->segment_idx && i <= RTE_MAX_MEMSEG; i++) {
606 if (i == RTE_MAX_MEMSEG) {
607 RTE_LOG(ERR, EAL, "Too many segments requested!\n");
611 seg = &ivshmem_config->segment[i];
613 /* copy segment to table */
614 memcpy(&ms_tbl[i], seg, sizeof(struct ivshmem_segment));
616 /* find ioremap addr */
617 for (j = 0; j < DIM(ivshmem_config->pci_devs); j++) {
618 pci_dev = &ivshmem_config->pci_devs[j];
619 if (!strncmp(pci_dev->path, seg->path, sizeof(pci_dev->path))) {
620 ioremap_addr = pci_dev->ioremap_addr;
624 if (ioremap_addr == 0) {
625 RTE_LOG(ERR, EAL, "Cannot find ioremap addr!\n");
629 /* work out alignments */
630 align = seg->entry.mz.addr_64 -
631 RTE_ALIGN_FLOOR(seg->entry.mz.addr_64, 0x1000);
632 len = RTE_ALIGN_CEIL(seg->entry.mz.len + align, 0x1000);
634 /* save original alignments */
635 ms_tbl[i].align = align;
637 /* create a memory zone */
638 mz.addr_64 = seg->entry.mz.addr_64 - align;
640 mz.hugepage_sz = seg->entry.mz.hugepage_sz;
641 mz.phys_addr = seg->entry.mz.phys_addr - align;
643 /* find true physical address */
644 mz.ioremap_addr = ioremap_addr + seg->entry.offset - align;
646 ms_tbl[i].entry.offset = seg->entry.offset - align;
648 memcpy(&ms_tbl[i].entry.mz, &mz, sizeof(struct rte_memzone));
651 /* clean up the segments */
652 memseg_idx = cleanup_segments(ms_tbl, ivshmem_config->segment_idx);
657 mcfg = rte_eal_get_configuration()->mem_config;
659 fd_zero = open("/dev/zero", O_RDWR);
662 RTE_LOG(ERR, EAL, "Cannot open /dev/zero: %s\n", strerror(errno));
666 /* create memsegs and put them into DPDK memory */
667 for (i = 0; i < (unsigned) memseg_idx; i++) {
671 ms.addr_64 = seg->entry.mz.addr_64;
672 ms.hugepage_sz = seg->entry.mz.hugepage_sz;
673 ms.len = seg->entry.mz.len;
674 ms.nchannel = rte_memory_get_nchannel();
675 ms.nrank = rte_memory_get_nrank();
676 ms.phys_addr = seg->entry.mz.phys_addr;
677 ms.ioremap_addr = seg->entry.mz.ioremap_addr;
678 ms.socket_id = seg->entry.mz.socket_id;
680 base_addr = mmap(ms.addr, ms.len,
681 PROT_READ | PROT_WRITE, MAP_PRIVATE, fd_zero, 0);
683 if (base_addr == MAP_FAILED || base_addr != ms.addr) {
684 RTE_LOG(ERR, EAL, "Cannot map /dev/zero!\n");
688 fd = open(seg->path, O_RDWR);
691 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", seg->path,
696 munmap(ms.addr, ms.len);
698 base_addr = mmap(ms.addr, ms.len,
699 PROT_READ | PROT_WRITE, MAP_SHARED, fd,
703 if (base_addr == MAP_FAILED || base_addr != ms.addr) {
704 RTE_LOG(ERR, EAL, "Cannot map segment into memory: "
705 "expected %p got %p (%s)\n", ms.addr, base_addr,
710 RTE_LOG(DEBUG, EAL, "Memory segment mapped: %p (len %" PRIx64 ") at "
711 "offset 0x%" PRIx64 "\n",
712 ms.addr, ms.len, seg->entry.offset);
714 /* put the pointers back into their real positions using original
716 ms.addr_64 += seg->align;
717 ms.phys_addr += seg->align;
718 ms.ioremap_addr += seg->align;
719 ms.len -= seg->align;
721 /* at this point, the rest of DPDK memory is not initialized, so we
722 * expect memsegs to be empty */
723 memcpy(&mcfg->memseg[i], &ms,
724 sizeof(struct rte_memseg));
728 RTE_LOG(DEBUG, EAL, "IVSHMEM segment found, size: 0x%lx\n",
735 /* this happens at a later stage, after general EAL memory initialization */
737 rte_eal_ivshmem_obj_init(void)
739 struct rte_ring_list* ring_list = NULL;
740 struct rte_mem_config * mcfg;
741 struct ivshmem_segment * seg;
742 struct rte_memzone * mz;
744 struct rte_tailq_entry *te;
748 /* secondary process would not need any object discovery - it'll all
749 * already be in shared config */
750 if (rte_eal_process_type() != RTE_PROC_PRIMARY || ivshmem_config == NULL)
753 /* check that we have an initialised ring tail queue */
754 ring_list = RTE_TAILQ_LOOKUP(RTE_TAILQ_RING_NAME, rte_ring_list);
755 if (ring_list == NULL) {
756 RTE_LOG(ERR, EAL, "No rte_ring tailq found!\n");
760 mcfg = rte_eal_get_configuration()->mem_config;
762 /* create memzones */
763 for (i = 0; i < ivshmem_config->segment_idx && i <= RTE_MAX_MEMZONE; i++) {
765 seg = &ivshmem_config->segment[i];
768 if (mcfg->memzone_cnt == RTE_MAX_MEMZONE) {
769 RTE_LOG(ERR, EAL, "No more memory zones available!\n");
773 idx = mcfg->memzone_cnt;
775 RTE_LOG(DEBUG, EAL, "Found memzone: '%s' at %p (len 0x%" PRIx64 ")\n",
776 seg->entry.mz.name, seg->entry.mz.addr, seg->entry.mz.len);
778 memcpy(&mcfg->memzone[idx], &seg->entry.mz,
779 sizeof(struct rte_memzone));
781 /* find ioremap address */
782 for (ms = 0; ms <= RTE_MAX_MEMSEG; ms++) {
783 if (ms == RTE_MAX_MEMSEG) {
784 RTE_LOG(ERR, EAL, "Physical address of segment not found!\n");
787 if (CONTAINS(mcfg->memseg[ms], mcfg->memzone[idx])) {
788 offset = mcfg->memzone[idx].addr_64 -
789 mcfg->memseg[ms].addr_64;
790 mcfg->memzone[idx].ioremap_addr = mcfg->memseg[ms].ioremap_addr +
799 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
802 for (i = 0; i < mcfg->memzone_cnt; i++) {
803 mz = &mcfg->memzone[i];
805 /* check if memzone has a ring prefix */
806 if (strncmp(mz->name, RTE_RING_MZ_PREFIX,
807 sizeof(RTE_RING_MZ_PREFIX) - 1) != 0)
810 r = (struct rte_ring*) (mz->addr_64);
812 te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
814 RTE_LOG(ERR, EAL, "Cannot allocate ring tailq entry!\n");
818 te->data = (void *) r;
820 TAILQ_INSERT_TAIL(ring_list, te, next);
822 RTE_LOG(DEBUG, EAL, "Found ring: '%s' at %p\n", r->name, mz->addr);
824 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
826 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
827 rte_memzone_dump(stdout);
828 rte_ring_list_dump(stdout);
834 /* initialize ivshmem structures */
835 int rte_eal_ivshmem_init(void)
837 struct rte_pci_device * dev;
838 struct rte_pci_resource * res;
842 /* initialize everything to 0 */
843 memset(path, 0, sizeof(path));
844 ivshmem_config = NULL;
846 pagesz = getpagesize();
848 RTE_LOG(DEBUG, EAL, "Searching for IVSHMEM devices...\n");
850 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
852 if (open_shared_config() < 0) {
853 RTE_LOG(ERR, EAL, "Could not open IVSHMEM config!\n");
859 TAILQ_FOREACH(dev, &pci_device_list, next) {
861 if (is_ivshmem_device(dev)) {
863 /* IVSHMEM memory is always on BAR2 */
864 res = &dev->mem_resource[2];
866 /* if we don't have a BAR2 */
870 /* construct pci device path */
871 snprintf(path, sizeof(path), IVSHMEM_RESOURCE_PATH,
872 dev->addr.domain, dev->addr.bus, dev->addr.devid,
875 /* try to find memseg */
876 fd = open(path, O_RDWR);
878 RTE_LOG(ERR, EAL, "Could not open %s\n", path);
882 /* check if it's a DPDK IVSHMEM device */
883 ret = has_ivshmem_metadata(fd, res->len);
888 /* config file creation is deferred until the first
889 * DPDK device is found. then, it has to be created
891 if (ivshmem_config == NULL &&
892 create_shared_config() < 0) {
893 RTE_LOG(ERR, EAL, "Could not create IVSHMEM config!\n");
898 if (read_metadata(path, sizeof(path), fd, res->len) < 0) {
899 RTE_LOG(ERR, EAL, "Could not read metadata from"
900 " device %02x:%02x.%x!\n", dev->addr.bus,
901 dev->addr.devid, dev->addr.function);
906 if (ivshmem_config->pci_devs_idx == RTE_LIBRTE_IVSHMEM_MAX_PCI_DEVS) {
907 RTE_LOG(WARNING, EAL,
908 "IVSHMEM PCI device limit exceeded. Increase "
909 "CONFIG_RTE_LIBRTE_IVSHMEM_MAX_PCI_DEVS in "
910 "your config file.\n");
914 RTE_LOG(INFO, EAL, "Found IVSHMEM device %02x:%02x.%x\n",
915 dev->addr.bus, dev->addr.devid, dev->addr.function);
917 ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].ioremap_addr = res->phys_addr;
918 snprintf(ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].path,
919 sizeof(ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].path),
922 ivshmem_config->pci_devs_idx++;
926 RTE_LOG(ERR, EAL, "Could not read IVSHMEM device: %s\n",
931 /* not a DPDK device */
933 RTE_LOG(DEBUG, EAL, "Skipping non-DPDK IVSHMEM device\n");
935 /* close the BAR fd */
941 /* ivshmem_config is not NULL only if config was created and/or mapped */
942 if (ivshmem_config) {
943 if (map_all_segments() < 0) {
944 RTE_LOG(ERR, EAL, "Mapping IVSHMEM segments failed!\n");
949 RTE_LOG(DEBUG, EAL, "No IVSHMEM configuration found! \n");