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_malloc.h>
53 #include <rte_common.h>
54 #include <rte_ivshmem.h>
56 #include "eal_internal_cfg.h"
57 #include "eal_private.h"
59 #define PCI_VENDOR_ID_IVSHMEM 0x1Af4
60 #define PCI_DEVICE_ID_IVSHMEM 0x1110
62 #define IVSHMEM_MAGIC 0x0BADC0DE
64 #define IVSHMEM_RESOURCE_PATH "/sys/bus/pci/devices/%04x:%02x:%02x.%x/resource2"
65 #define IVSHMEM_CONFIG_PATH "/var/run/.%s_ivshmem_config"
70 #define FULL (PHYS|VIRT|IOREMAP)
72 #define METADATA_SIZE_ALIGNED \
73 (RTE_ALIGN_CEIL(sizeof(struct rte_ivshmem_metadata),pagesz))
75 #define CONTAINS(x,y)\
76 (((y).addr_64 >= (x).addr_64) && ((y).addr_64 < (x).addr_64 + (x).len))
78 #define DIM(x) (sizeof(x)/sizeof(x[0]))
80 struct ivshmem_pci_device {
82 phys_addr_t ioremap_addr;
85 /* data type to store in config */
86 struct ivshmem_segment {
87 struct rte_ivshmem_metadata_entry entry;
91 struct ivshmem_shared_config {
92 struct ivshmem_segment segment[RTE_MAX_MEMSEG];
94 struct ivshmem_pci_device pci_devs[RTE_LIBRTE_IVSHMEM_MAX_PCI_DEVS];
95 uint32_t pci_devs_idx;
97 static struct ivshmem_shared_config * ivshmem_config;
98 static int memseg_idx;
101 /* Tailq heads to add rings to */
102 TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
109 is_ivshmem_device(struct rte_pci_device * dev)
111 return dev->id.vendor_id == PCI_VENDOR_ID_IVSHMEM
112 && dev->id.device_id == PCI_DEVICE_ID_IVSHMEM;
116 map_metadata(int fd, uint64_t len)
118 size_t metadata_len = sizeof(struct rte_ivshmem_metadata);
119 size_t aligned_len = METADATA_SIZE_ALIGNED;
121 return mmap(NULL, metadata_len, PROT_READ | PROT_WRITE,
122 MAP_SHARED, fd, len - aligned_len);
126 unmap_metadata(void * ptr)
128 munmap(ptr, sizeof(struct rte_ivshmem_metadata));
132 has_ivshmem_metadata(int fd, uint64_t len)
134 struct rte_ivshmem_metadata metadata;
137 ptr = map_metadata(fd, len);
139 if (ptr == MAP_FAILED)
142 metadata = *(struct rte_ivshmem_metadata*) (ptr);
146 return metadata.magic_number == IVSHMEM_MAGIC;
150 remove_segment(struct ivshmem_segment * ms, int len, int idx)
154 for (i = idx; i < len - 1; i++)
155 memcpy(&ms[i], &ms[i+1], sizeof(struct ivshmem_segment));
156 memset(&ms[len-1], 0, sizeof(struct ivshmem_segment));
160 overlap(const struct rte_memzone * mz1, const struct rte_memzone * mz2)
162 uint64_t start1, end1, start2, end2;
163 uint64_t p_start1, p_end1, p_start2, p_end2;
164 uint64_t i_start1, i_end1, i_start2, i_end2;
167 /* gather virtual addresses */
168 start1 = mz1->addr_64;
169 end1 = mz1->addr_64 + mz1->len;
170 start2 = mz2->addr_64;
171 end2 = mz2->addr_64 + mz2->len;
173 /* gather physical addresses */
174 p_start1 = mz1->phys_addr;
175 p_end1 = mz1->phys_addr + mz1->len;
176 p_start2 = mz2->phys_addr;
177 p_end2 = mz2->phys_addr + mz2->len;
179 /* gather ioremap addresses */
180 i_start1 = mz1->ioremap_addr;
181 i_end1 = mz1->ioremap_addr + mz1->len;
182 i_start2 = mz2->ioremap_addr;
183 i_end2 = mz2->ioremap_addr + mz2->len;
185 /* check for overlap in virtual addresses */
186 if (start1 >= start2 && start1 < end2)
188 if (start2 >= start1 && start2 < end1)
191 /* check for overlap in physical addresses */
192 if (p_start1 >= p_start2 && p_start1 < p_end2)
194 if (p_start2 >= p_start1 && p_start2 < p_end1)
197 /* check for overlap in ioremap addresses */
198 if (i_start1 >= i_start2 && i_start1 < i_end2)
200 if (i_start2 >= i_start1 && i_start2 < i_end1)
207 adjacent(const struct rte_memzone * mz1, const struct rte_memzone * mz2)
209 uint64_t start1, end1, start2, end2;
210 uint64_t p_start1, p_end1, p_start2, p_end2;
211 uint64_t i_start1, i_end1, i_start2, i_end2;
214 /* gather virtual addresses */
215 start1 = mz1->addr_64;
216 end1 = mz1->addr_64 + mz1->len;
217 start2 = mz2->addr_64;
218 end2 = mz2->addr_64 + mz2->len;
220 /* gather physical addresses */
221 p_start1 = mz1->phys_addr;
222 p_end1 = mz1->phys_addr + mz1->len;
223 p_start2 = mz2->phys_addr;
224 p_end2 = mz2->phys_addr + mz2->len;
226 /* gather ioremap addresses */
227 i_start1 = mz1->ioremap_addr;
228 i_end1 = mz1->ioremap_addr + mz1->len;
229 i_start2 = mz2->ioremap_addr;
230 i_end2 = mz2->ioremap_addr + mz2->len;
232 /* check if segments are virtually adjacent */
238 /* check if segments are physically adjacent */
239 if (p_start1 == p_end2)
241 if (p_start2 == p_end1)
244 /* check if segments are ioremap-adjacent */
245 if (i_start1 == i_end2)
247 if (i_start2 == i_end1)
254 has_adjacent_segments(struct ivshmem_segment * ms, int len)
258 for (i = 0; i < len; i++)
259 for (j = i + 1; j < len; j++) {
260 /* we're only interested in fully adjacent segments; partially
261 * adjacent segments can coexist.
263 if (adjacent(&ms[i].entry.mz, &ms[j].entry.mz) == FULL)
270 has_overlapping_segments(struct ivshmem_segment * ms, int len)
274 for (i = 0; i < len; i++)
275 for (j = i + 1; j < len; j++)
276 if (overlap(&ms[i].entry.mz, &ms[j].entry.mz))
282 seg_compare(const void * a, const void * b)
284 const struct ivshmem_segment * s1 = (const struct ivshmem_segment*) a;
285 const struct ivshmem_segment * s2 = (const struct ivshmem_segment*) b;
287 /* move unallocated zones to the end */
288 if (s1->entry.mz.addr == NULL && s2->entry.mz.addr == NULL)
290 if (s1->entry.mz.addr == 0)
292 if (s2->entry.mz.addr == 0)
295 return s1->entry.mz.phys_addr > s2->entry.mz.phys_addr;
298 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
300 entry_dump(struct rte_ivshmem_metadata_entry *e)
302 RTE_LOG(DEBUG, EAL, "\tvirt: %p-%p\n", e->mz.addr,
303 RTE_PTR_ADD(e->mz.addr, e->mz.len));
304 RTE_LOG(DEBUG, EAL, "\tphys: 0x%" PRIx64 "-0x%" PRIx64 "\n",
306 e->mz.phys_addr + e->mz.len);
307 RTE_LOG(DEBUG, EAL, "\tio: 0x%" PRIx64 "-0x%" PRIx64 "\n",
309 e->mz.ioremap_addr + e->mz.len);
310 RTE_LOG(DEBUG, EAL, "\tlen: 0x%" PRIx64 "\n", e->mz.len);
311 RTE_LOG(DEBUG, EAL, "\toff: 0x%" PRIx64 "\n", e->offset);
321 /* read through metadata mapped from the IVSHMEM device */
323 read_metadata(char * path, int path_len, int fd, uint64_t flen)
325 struct rte_ivshmem_metadata metadata;
326 struct rte_ivshmem_metadata_entry * entry;
330 ptr = map_metadata(fd, flen);
332 if (ptr == MAP_FAILED)
335 metadata = *(struct rte_ivshmem_metadata*) (ptr);
339 RTE_LOG(DEBUG, EAL, "Parsing metadata for \"%s\"\n", metadata.name);
341 idx = ivshmem_config->segment_idx;
343 for (i = 0; i < RTE_LIBRTE_IVSHMEM_MAX_ENTRIES &&
344 idx <= RTE_MAX_MEMSEG; i++) {
346 if (idx == RTE_MAX_MEMSEG) {
347 RTE_LOG(ERR, EAL, "Not enough memory segments!\n");
351 entry = &metadata.entry[i];
353 /* stop on uninitialized memzone */
354 if (entry->mz.len == 0)
357 /* copy metadata entry */
358 memcpy(&ivshmem_config->segment[idx].entry, entry,
359 sizeof(struct rte_ivshmem_metadata_entry));
362 snprintf(ivshmem_config->segment[idx].path, path_len, "%s", path);
366 ivshmem_config->segment_idx = idx;
371 /* check through each segment and look for adjacent or overlapping ones. */
373 cleanup_segments(struct ivshmem_segment * ms, int tbl_len)
375 struct ivshmem_segment * s, * tmp;
376 int i, j, concat, seg_adjacent, seg_overlapping;
377 uint64_t start1, start2, end1, end2, p_start1, p_start2, i_start1, i_start2;
379 qsort(ms, tbl_len, sizeof(struct ivshmem_segment),
382 while (has_overlapping_segments(ms, tbl_len) ||
383 has_adjacent_segments(ms, tbl_len)) {
385 for (i = 0; i < tbl_len; i++) {
390 for (j = i + 1; j < tbl_len; j++) {
393 /* check if this segment is overlapping with existing segment,
394 * or is adjacent to existing segment */
395 seg_overlapping = overlap(&s->entry.mz, &tmp->entry.mz);
396 seg_adjacent = adjacent(&s->entry.mz, &tmp->entry.mz);
398 /* check if segments fully overlap or are fully adjacent */
399 if ((seg_adjacent == FULL) || (seg_overlapping == FULL)) {
401 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
402 RTE_LOG(DEBUG, EAL, "Concatenating segments\n");
403 RTE_LOG(DEBUG, EAL, "Segment %i:\n", i);
404 entry_dump(&s->entry);
405 RTE_LOG(DEBUG, EAL, "Segment %i:\n", j);
406 entry_dump(&tmp->entry);
409 start1 = s->entry.mz.addr_64;
410 start2 = tmp->entry.mz.addr_64;
411 p_start1 = s->entry.mz.phys_addr;
412 p_start2 = tmp->entry.mz.phys_addr;
413 i_start1 = s->entry.mz.ioremap_addr;
414 i_start2 = tmp->entry.mz.ioremap_addr;
415 end1 = s->entry.mz.addr_64 + s->entry.mz.len;
416 end2 = tmp->entry.mz.addr_64 + tmp->entry.mz.len;
418 /* settle for minimum start address and maximum length */
419 s->entry.mz.addr_64 = RTE_MIN(start1, start2);
420 s->entry.mz.phys_addr = RTE_MIN(p_start1, p_start2);
421 s->entry.mz.ioremap_addr = RTE_MIN(i_start1, i_start2);
422 s->entry.offset = RTE_MIN(s->entry.offset, tmp->entry.offset);
423 s->entry.mz.len = RTE_MAX(end1, end2) - s->entry.mz.addr_64;
426 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
427 RTE_LOG(DEBUG, EAL, "Resulting segment:\n");
428 entry_dump(&s->entry);
432 /* if segments not fully overlap, we have an error condition.
433 * adjacent segments can coexist.
435 else if (seg_overlapping > 0) {
436 RTE_LOG(ERR, EAL, "Segments %i and %i overlap!\n", i, j);
437 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
438 RTE_LOG(DEBUG, EAL, "Segment %i:\n", i);
439 entry_dump(&s->entry);
440 RTE_LOG(DEBUG, EAL, "Segment %i:\n", j);
441 entry_dump(&tmp->entry);
448 /* if we concatenated, remove segment at j */
450 remove_segment(ms, tbl_len, j);
461 create_shared_config(void)
466 /* build ivshmem config file path */
467 snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
468 internal_config.hugefile_prefix);
470 fd = open(path, O_CREAT | O_RDWR, 0600);
473 RTE_LOG(ERR, EAL, "Could not open %s: %s\n", path, strerror(errno));
477 /* try ex-locking first - if the file is locked, we have a problem */
478 if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
479 RTE_LOG(ERR, EAL, "Locking %s failed: %s\n", path, strerror(errno));
484 if (ftruncate(fd, sizeof(struct ivshmem_shared_config)) < 0) {
485 RTE_LOG(ERR, EAL, "ftruncate failed: %s\n", strerror(errno));
489 ivshmem_config = mmap(NULL, sizeof(struct ivshmem_shared_config),
490 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
492 if (ivshmem_config == MAP_FAILED)
495 memset(ivshmem_config, 0, sizeof(struct ivshmem_shared_config));
497 /* change the exclusive lock we got earlier to a shared lock */
498 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
499 RTE_LOG(ERR, EAL, "Locking %s failed: %s \n", path, strerror(errno));
508 /* open shared config file and, if present, map the config.
509 * having no config file is not an error condition, as we later check if
510 * ivshmem_config is NULL (if it is, that means nothing was mapped). */
512 open_shared_config(void)
517 /* build ivshmem config file path */
518 snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
519 internal_config.hugefile_prefix);
521 fd = open(path, O_RDONLY);
523 /* if the file doesn't exist, just return success */
524 if (fd < 0 && errno == ENOENT)
526 /* else we have an error condition */
528 RTE_LOG(ERR, EAL, "Could not open %s: %s\n",
529 path, strerror(errno));
533 /* try ex-locking first - if the lock *does* succeed, this means it's a
534 * stray config file, so it should be deleted.
536 if (flock(fd, LOCK_EX | LOCK_NB) != -1) {
538 /* if we can't remove the file, something is wrong */
539 if (unlink(path) < 0) {
540 RTE_LOG(ERR, EAL, "Could not remove %s: %s\n", path,
545 /* release the lock */
549 /* return success as having a stray config file is equivalent to not
550 * having config file at all.
555 ivshmem_config = mmap(NULL, sizeof(struct ivshmem_shared_config),
556 PROT_READ, MAP_SHARED, fd, 0);
558 if (ivshmem_config == MAP_FAILED)
561 /* place a shared lock on config file */
562 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
563 RTE_LOG(ERR, EAL, "Locking %s failed: %s \n", path, strerror(errno));
573 * This function does the following:
575 * 1) Builds a table of ivshmem_segments with proper offset alignment
576 * 2) Cleans up that table so that we don't have any overlapping or adjacent
578 * 3) Creates memsegs from this table and maps them into memory.
581 map_all_segments(void)
583 struct ivshmem_segment ms_tbl[RTE_MAX_MEMSEG];
584 struct ivshmem_pci_device * pci_dev;
585 struct rte_mem_config * mcfg;
586 struct ivshmem_segment * seg;
589 struct rte_memzone mz;
590 struct rte_memseg ms;
593 phys_addr_t ioremap_addr;
597 memset(ms_tbl, 0, sizeof(ms_tbl));
598 memset(&mz, 0, sizeof(struct rte_memzone));
599 memset(&ms, 0, sizeof(struct rte_memseg));
601 /* first, build a table of memsegs to map, to avoid failed mmaps due to
604 for (i = 0; i < ivshmem_config->segment_idx && i <= RTE_MAX_MEMSEG; i++) {
605 if (i == RTE_MAX_MEMSEG) {
606 RTE_LOG(ERR, EAL, "Too many segments requested!\n");
610 seg = &ivshmem_config->segment[i];
612 /* copy segment to table */
613 memcpy(&ms_tbl[i], seg, sizeof(struct ivshmem_segment));
615 /* find ioremap addr */
616 for (j = 0; j < DIM(ivshmem_config->pci_devs); j++) {
617 pci_dev = &ivshmem_config->pci_devs[j];
618 if (!strncmp(pci_dev->path, seg->path, sizeof(pci_dev->path))) {
619 ioremap_addr = pci_dev->ioremap_addr;
623 if (ioremap_addr == 0) {
624 RTE_LOG(ERR, EAL, "Cannot find ioremap addr!\n");
628 /* work out alignments */
629 align = seg->entry.mz.addr_64 -
630 RTE_ALIGN_FLOOR(seg->entry.mz.addr_64, 0x1000);
631 len = RTE_ALIGN_CEIL(seg->entry.mz.len + align, 0x1000);
633 /* save original alignments */
634 ms_tbl[i].align = align;
636 /* create a memory zone */
637 mz.addr_64 = seg->entry.mz.addr_64 - align;
639 mz.hugepage_sz = seg->entry.mz.hugepage_sz;
640 mz.phys_addr = seg->entry.mz.phys_addr - align;
642 /* find true physical address */
643 mz.ioremap_addr = ioremap_addr + seg->entry.offset - align;
645 ms_tbl[i].entry.offset = seg->entry.offset - align;
647 memcpy(&ms_tbl[i].entry.mz, &mz, sizeof(struct rte_memzone));
650 /* clean up the segments */
651 memseg_idx = cleanup_segments(ms_tbl, ivshmem_config->segment_idx);
656 mcfg = rte_eal_get_configuration()->mem_config;
658 fd_zero = open("/dev/zero", O_RDWR);
661 RTE_LOG(ERR, EAL, "Cannot open /dev/zero: %s\n", strerror(errno));
665 /* create memsegs and put them into DPDK memory */
666 for (i = 0; i < (unsigned) memseg_idx; i++) {
670 ms.addr_64 = seg->entry.mz.addr_64;
671 ms.hugepage_sz = seg->entry.mz.hugepage_sz;
672 ms.len = seg->entry.mz.len;
673 ms.nchannel = rte_memory_get_nchannel();
674 ms.nrank = rte_memory_get_nrank();
675 ms.phys_addr = seg->entry.mz.phys_addr;
676 ms.ioremap_addr = seg->entry.mz.ioremap_addr;
677 ms.socket_id = seg->entry.mz.socket_id;
679 base_addr = mmap(ms.addr, ms.len,
680 PROT_READ | PROT_WRITE, MAP_PRIVATE, fd_zero, 0);
682 if (base_addr == MAP_FAILED || base_addr != ms.addr) {
683 RTE_LOG(ERR, EAL, "Cannot map /dev/zero!\n");
687 fd = open(seg->path, O_RDWR);
690 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", seg->path,
695 munmap(ms.addr, ms.len);
697 base_addr = mmap(ms.addr, ms.len,
698 PROT_READ | PROT_WRITE, MAP_SHARED, fd,
702 if (base_addr == MAP_FAILED || base_addr != ms.addr) {
703 RTE_LOG(ERR, EAL, "Cannot map segment into memory: "
704 "expected %p got %p (%s)\n", ms.addr, base_addr,
709 RTE_LOG(DEBUG, EAL, "Memory segment mapped: %p (len %" PRIx64 ") at "
710 "offset 0x%" PRIx64 "\n",
711 ms.addr, ms.len, seg->entry.offset);
713 /* put the pointers back into their real positions using original
715 ms.addr_64 += seg->align;
716 ms.phys_addr += seg->align;
717 ms.ioremap_addr += seg->align;
718 ms.len -= seg->align;
720 /* at this point, the rest of DPDK memory is not initialized, so we
721 * expect memsegs to be empty */
722 memcpy(&mcfg->memseg[i], &ms,
723 sizeof(struct rte_memseg));
727 RTE_LOG(DEBUG, EAL, "IVSHMEM segment found, size: 0x%lx\n",
734 /* this happens at a later stage, after general EAL memory initialization */
736 rte_eal_ivshmem_obj_init(void)
738 struct rte_ring_list* ring_list = NULL;
739 struct rte_mem_config * mcfg;
740 struct ivshmem_segment * seg;
741 struct rte_memzone * mz;
743 struct rte_tailq_entry *te;
747 /* secondary process would not need any object discovery - it'll all
748 * already be in shared config */
749 if (rte_eal_process_type() != RTE_PROC_PRIMARY || ivshmem_config == NULL)
752 /* check that we have an initialised ring tail queue */
753 ring_list = RTE_TAILQ_LOOKUP(RTE_TAILQ_RING_NAME, rte_ring_list);
754 if (ring_list == NULL) {
755 RTE_LOG(ERR, EAL, "No rte_ring tailq found!\n");
759 mcfg = rte_eal_get_configuration()->mem_config;
761 /* create memzones */
762 for (i = 0; i < ivshmem_config->segment_idx && i <= RTE_MAX_MEMZONE; i++) {
764 seg = &ivshmem_config->segment[i];
767 if (mcfg->memzone_cnt == RTE_MAX_MEMZONE) {
768 RTE_LOG(ERR, EAL, "No more memory zones available!\n");
772 idx = mcfg->memzone_cnt;
774 RTE_LOG(DEBUG, EAL, "Found memzone: '%s' at %p (len 0x%" PRIx64 ")\n",
775 seg->entry.mz.name, seg->entry.mz.addr, seg->entry.mz.len);
777 memcpy(&mcfg->memzone[idx], &seg->entry.mz,
778 sizeof(struct rte_memzone));
780 /* find ioremap address */
781 for (ms = 0; ms <= RTE_MAX_MEMSEG; ms++) {
782 if (ms == RTE_MAX_MEMSEG) {
783 RTE_LOG(ERR, EAL, "Physical address of segment not found!\n");
786 if (CONTAINS(mcfg->memseg[ms], mcfg->memzone[idx])) {
787 offset = mcfg->memzone[idx].addr_64 -
788 mcfg->memseg[ms].addr_64;
789 mcfg->memzone[idx].ioremap_addr = mcfg->memseg[ms].ioremap_addr +
798 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
801 for (i = 0; i < mcfg->memzone_cnt; i++) {
802 mz = &mcfg->memzone[i];
804 /* check if memzone has a ring prefix */
805 if (strncmp(mz->name, RTE_RING_MZ_PREFIX,
806 sizeof(RTE_RING_MZ_PREFIX) - 1) != 0)
809 r = (struct rte_ring*) (mz->addr_64);
811 te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
813 RTE_LOG(ERR, EAL, "Cannot allocate ring tailq entry!\n");
817 te->data = (void *) r;
819 TAILQ_INSERT_TAIL(ring_list, te, next);
821 RTE_LOG(DEBUG, EAL, "Found ring: '%s' at %p\n", r->name, mz->addr);
823 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
825 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
826 rte_memzone_dump(stdout);
827 rte_ring_list_dump(stdout);
833 /* initialize ivshmem structures */
834 int rte_eal_ivshmem_init(void)
836 struct rte_pci_device * dev;
837 struct rte_pci_resource * res;
841 /* initialize everything to 0 */
842 memset(path, 0, sizeof(path));
843 ivshmem_config = NULL;
845 pagesz = getpagesize();
847 RTE_LOG(DEBUG, EAL, "Searching for IVSHMEM devices...\n");
849 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
851 if (open_shared_config() < 0) {
852 RTE_LOG(ERR, EAL, "Could not open IVSHMEM config!\n");
858 TAILQ_FOREACH(dev, &pci_device_list, next) {
860 if (is_ivshmem_device(dev)) {
862 /* IVSHMEM memory is always on BAR2 */
863 res = &dev->mem_resource[2];
865 /* if we don't have a BAR2 */
869 /* construct pci device path */
870 snprintf(path, sizeof(path), IVSHMEM_RESOURCE_PATH,
871 dev->addr.domain, dev->addr.bus, dev->addr.devid,
874 /* try to find memseg */
875 fd = open(path, O_RDWR);
877 RTE_LOG(ERR, EAL, "Could not open %s\n", path);
881 /* check if it's a DPDK IVSHMEM device */
882 ret = has_ivshmem_metadata(fd, res->len);
887 /* config file creation is deferred until the first
888 * DPDK device is found. then, it has to be created
890 if (ivshmem_config == NULL &&
891 create_shared_config() < 0) {
892 RTE_LOG(ERR, EAL, "Could not create IVSHMEM config!\n");
897 if (read_metadata(path, sizeof(path), fd, res->len) < 0) {
898 RTE_LOG(ERR, EAL, "Could not read metadata from"
899 " device %02x:%02x.%x!\n", dev->addr.bus,
900 dev->addr.devid, dev->addr.function);
905 if (ivshmem_config->pci_devs_idx == RTE_LIBRTE_IVSHMEM_MAX_PCI_DEVS) {
906 RTE_LOG(WARNING, EAL,
907 "IVSHMEM PCI device limit exceeded. Increase "
908 "CONFIG_RTE_LIBRTE_IVSHMEM_MAX_PCI_DEVS in "
909 "your config file.\n");
913 RTE_LOG(INFO, EAL, "Found IVSHMEM device %02x:%02x.%x\n",
914 dev->addr.bus, dev->addr.devid, dev->addr.function);
916 ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].ioremap_addr = res->phys_addr;
917 snprintf(ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].path,
918 sizeof(ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].path),
921 ivshmem_config->pci_devs_idx++;
925 RTE_LOG(ERR, EAL, "Could not read IVSHMEM device: %s\n",
930 /* not a DPDK device */
932 RTE_LOG(DEBUG, EAL, "Skipping non-DPDK IVSHMEM device\n");
934 /* close the BAR fd */
940 /* ivshmem_config is not NULL only if config was created and/or mapped */
941 if (ivshmem_config) {
942 if (map_all_segments() < 0) {
943 RTE_LOG(ERR, EAL, "Mapping IVSHMEM segments failed!\n");
948 RTE_LOG(DEBUG, EAL, "No IVSHMEM configuration found! \n");