[dpdk-dev,v3,3/6] EAL: Add new EAL "--range-virtaddr" option

Message ID 1456129075-14909-4-git-send-email-mukawa@igel.co.jp (mailing list archive)
State Superseded, archived
Headers

Commit Message

Tetsuya Mukawa Feb. 22, 2016, 8:17 a.m. UTC
  The option specifies how to mmap EAL memory.
If the option is specified like '--range-virtaddr=<addr1>-<addr2>',
EAL will check /proc/maps, then tries to find free region between addr1
and addr2. If a region is found, EAL will treat it as if 'base-virtaddr'
is specified. Because of this, the option will not work with
'--base-virtaddr'.

Signed-off-by: Tetsuya Mukawa <mukawa@igel.co.jp>
---
 lib/librte_eal/common/eal_common_options.c |  9 ++++
 lib/librte_eal/common/eal_internal_cfg.h   |  2 +
 lib/librte_eal/common/eal_options.h        |  2 +
 lib/librte_eal/linuxapp/eal/eal.c          | 39 ++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_memory.c   | 82 +++++++++++++++++++++++++++++-
 5 files changed, 133 insertions(+), 1 deletion(-)
  

Comments

Jianfeng Tan March 4, 2016, 2:20 a.m. UTC | #1
Hi Tetsuya,

On 2/22/2016 4:17 PM, Tetsuya Mukawa wrote:
> The option specifies how to mmap EAL memory.
> If the option is specified like '--range-virtaddr=<addr1>-<addr2>',
> EAL will check /proc/maps, then tries to find free region between addr1
> and addr2. If a region is found, EAL will treat it as if 'base-virtaddr'
> is specified. Because of this, the option will not work with
> '--base-virtaddr'.
>
> Signed-off-by: Tetsuya Mukawa <mukawa@igel.co.jp>
> ---
>   lib/librte_eal/common/eal_common_options.c |  9 ++++
>   lib/librte_eal/common/eal_internal_cfg.h   |  2 +
>   lib/librte_eal/common/eal_options.h        |  2 +
>   lib/librte_eal/linuxapp/eal/eal.c          | 39 ++++++++++++++
>   lib/librte_eal/linuxapp/eal/eal_memory.c   | 82 +++++++++++++++++++++++++++++-
>   5 files changed, 133 insertions(+), 1 deletion(-)
>
> diff --git a/lib/librte_eal/common/eal_common_options.c b/lib/librte_eal/common/eal_common_options.c
> index 65bccbd..3b4f789 100644
> --- a/lib/librte_eal/common/eal_common_options.c
> +++ b/lib/librte_eal/common/eal_common_options.c
> @@ -74,6 +74,7 @@ eal_short_options[] =
>   const struct option
>   eal_long_options[] = {
>   	{OPT_BASE_VIRTADDR,     1, NULL, OPT_BASE_VIRTADDR_NUM    },
> +	{OPT_RANGE_VIRTADDR,    1, NULL, OPT_RANGE_VIRTADDR_NUM   },
>   	{OPT_CREATE_UIO_DEV,    0, NULL, OPT_CREATE_UIO_DEV_NUM   },
>   	{OPT_FILE_PREFIX,       1, NULL, OPT_FILE_PREFIX_NUM      },
>   	{OPT_HELP,              0, NULL, OPT_HELP_NUM             },
> @@ -137,6 +138,8 @@ eal_reset_internal_config(struct internal_config *internal_cfg)
>   	for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
>   		internal_cfg->hugepage_info[i].lock_descriptor = -1;
>   	internal_cfg->base_virtaddr = 0;
> +	internal_cfg->range_virtaddr_start = 0;
> +	internal_cfg->range_virtaddr_end = 0;
>   
>   	internal_cfg->syslog_facility = LOG_DAEMON;
>   	/* default value from build option */
> @@ -985,6 +988,12 @@ eal_check_common_options(struct internal_config *internal_cfg)
>   		return -1;
>   	}
>   
> +	if (internal_cfg->base_virtaddr && internal_cfg->range_virtaddr_end) {
> +		RTE_LOG(ERR, EAL, "Option --"OPT_RANGE_VIRTADDR" cannot "
> +			"be specified together with --"OPT_BASE_VIRTADDR"\n");
> +		return -1;
> +	}
> +
>   	return 0;
>   }
>   
> diff --git a/lib/librte_eal/common/eal_internal_cfg.h b/lib/librte_eal/common/eal_internal_cfg.h
> index 9117ed9..0734630 100644
> --- a/lib/librte_eal/common/eal_internal_cfg.h
> +++ b/lib/librte_eal/common/eal_internal_cfg.h
> @@ -78,6 +78,8 @@ struct internal_config {
>   	volatile unsigned force_sockets;
>   	volatile uint64_t socket_mem[RTE_MAX_NUMA_NODES]; /**< amount of memory per socket */
>   	uintptr_t base_virtaddr;          /**< base address to try and reserve memory from */
> +	uintptr_t range_virtaddr_start;   /**< start address of mappable region */
> +	uintptr_t range_virtaddr_end;     /**< end address of mappable region */
>   	volatile int syslog_facility;	  /**< facility passed to openlog() */
>   	volatile uint32_t log_level;	  /**< default log level */
>   	/** default interrupt mode for VFIO */
> diff --git a/lib/librte_eal/common/eal_options.h b/lib/librte_eal/common/eal_options.h
> index e5da14a..8e4cf1d 100644
> --- a/lib/librte_eal/common/eal_options.h
> +++ b/lib/librte_eal/common/eal_options.h
> @@ -47,6 +47,8 @@ enum {
>   	OPT_LONG_MIN_NUM = 256,
>   #define OPT_BASE_VIRTADDR     "base-virtaddr"
>   	OPT_BASE_VIRTADDR_NUM,
> +#define OPT_RANGE_VIRTADDR    "range-virtaddr"
> +	OPT_RANGE_VIRTADDR_NUM,
>   #define OPT_CREATE_UIO_DEV    "create-uio-dev"
>   	OPT_CREATE_UIO_DEV_NUM,
>   #define OPT_FILE_PREFIX       "file-prefix"
> diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c
> index 82f34f7..80f1995 100644
> --- a/lib/librte_eal/linuxapp/eal/eal.c
> +++ b/lib/librte_eal/linuxapp/eal/eal.c
> @@ -444,6 +444,35 @@ eal_parse_base_virtaddr(const char *arg)
>   }
>   
>   static int
> +eal_parse_range_virtaddr(const char *range)
> +{
> +	char *p, *endptr;
> +	uint64_t tmp_start, tmp_end;
> +
> +	p = strchr(range, '-');
> +	if (p == NULL)
> +		return -1;
> +	*p++ = '\0';
> +
> +	errno = 0;
> +	tmp_start = strtoul(range, &endptr, 0);
> +	if ((errno != 0) || endptr == NULL || (*endptr != '\0'))
> +		return -1;
> +
> +	tmp_end = strtoul(p, &endptr, 0);
> +	if ((errno != 0) || endptr == NULL || (*endptr != '\0'))
> +		return -1;
> +
> +	if (tmp_start >= tmp_end)
> +		return -1;
> +
> +	internal_config.range_virtaddr_start = tmp_start;
> +	internal_config.range_virtaddr_end = tmp_end;
> +
> +	return 0;
> +}
> +
> +static int
>   eal_parse_vfio_intr(const char *mode)
>   {
>   	unsigned i;
> @@ -604,6 +633,16 @@ eal_parse_args(int argc, char **argv)
>   			}
>   			break;
>   
> +		case OPT_RANGE_VIRTADDR_NUM:
> +			if (eal_parse_range_virtaddr(optarg) < 0) {
> +				RTE_LOG(ERR, EAL, "invalid parameter for --"
> +						OPT_RANGE_VIRTADDR "\n");
> +				eal_usage(prgname);
> +				ret = -1;
> +				goto out;
> +			}
> +			break;
> +
>   		case OPT_VFIO_INTR_NUM:
>   			if (eal_parse_vfio_intr(optarg) < 0) {
>   				RTE_LOG(ERR, EAL, "invalid parameters for --"
> diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c
> index a6b3616..d608273 100644
> --- a/lib/librte_eal/linuxapp/eal/eal_memory.c
> +++ b/lib/librte_eal/linuxapp/eal/eal_memory.c
> @@ -251,6 +251,66 @@ aslr_enabled(void)
>   }
>   
>   /*
> + * Find memory space that fits user request.
> + */
> +static uintptr_t
> +rte_eal_get_free_region(uint64_t pagesz)
> +{
> +	uint64_t alloc_size, start, end, next_start;
> +	uint64_t low_limit, high_limit;
> +	uintptr_t addr = 0;
> +	char buf[1024], *p;
> +	FILE *fp;
> +
> +	alloc_size = internal_config.memory;
> +	low_limit = internal_config.range_virtaddr_start;
> +	high_limit = internal_config.range_virtaddr_end;
> +
> +	/* allocation size should be aligned by page size */
> +	if (alloc_size != RTE_ALIGN_CEIL(alloc_size, pagesz)) {
> +		rte_panic("Invalid allocation size 0x%lx\n", alloc_size);
> +		return NULL;

This line causes compiling error:
lib/librte_eal/linuxapp/eal/eal_memory.c:272:3: error: return makes 
integer from pointer without a cast [-Werror]
    return NULL;
    ^


Thanks,
Jianfeng

> +	}
> +
> +	fp = fopen("/proc/self/maps", "r");
> +	if (fp == NULL) {
> +		rte_panic("Cannot open /proc/self/maps\n");
> +		return NULL;

Ditto.

Thanks,
Jianfeng

> +	}
> +
> +	next_start = 0;
> +	do {
> +		start = next_start;
> +
> +		if ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
> +			if (sscanf(p, "%lx-%lx ", &end, &next_start) < 2)
> +				break;
> +
> +			next_start = RTE_ALIGN_CEIL(next_start, alloc_size);
> +			end = RTE_ALIGN_CEIL(end, alloc_size) - 1;
> +		} else
> +			end = UINT64_MAX;
> +
> +		if (start >= high_limit)
> +			break;
> +		if (end < low_limit)
> +			continue;
> +
> +		start = RTE_MAX(start, low_limit);
> +		end = RTE_MIN(end, high_limit - 1);
> +
> +		if (end - start >= alloc_size - 1) {
> +			addr = start;
> +			break;
> +		}
> +	} while (end != UINT64_MAX);
> +
> +	fclose(fp);
> +
> +	return addr;
> +}
> +
> +/*
>    * Try to mmap *size bytes in /dev/zero. If it is successful, return the
>    * pointer to the mmap'd area and keep *size unmodified. Else, retry
>    * with a smaller zone: decrease *size by hugepage_sz until it reaches
> @@ -1126,6 +1186,25 @@ rte_eal_hugepage_init(void)
>   	/* get pointer to global configuration */
>   	mcfg = rte_eal_get_configuration()->mem_config;
>   
> +	if (internal_config.range_virtaddr_end) {
> +		uint64_t pagesize = RTE_PGSIZE_4K;
> +		struct hugepage_info *hpi;
> +		unsigned n;
> +		uintptr_t addr;
> +
> +		/* determine maximum hugepage size */
> +		for (n = 0; n < internal_config.num_hugepage_sizes; n++) {
> +			hpi = &internal_config.hugepage_info[n];
> +			pagesize = RTE_MAX(hpi->hugepage_sz, pagesize);
> +		}
> +
> +		addr = rte_eal_get_free_region(pagesize);
> +		if (addr == 0)
> +			RTE_LOG(WARNING, EAL,
> +				"no free space to mmap in specified region\n");
> +		internal_config.base_virtaddr = addr;
> +	}
> +
>   	/* when hugetlbfs is disabled or single-file option is specified */
>   	if (internal_config.no_hugetlbfs || internal_config.single_file) {
>   		int fd;
> @@ -1158,7 +1237,8 @@ rte_eal_hugepage_init(void)
>   			return -1;
>   		}
>   
> -		addr = mmap(NULL, internal_config.memory,
> +		addr = mmap((void *)internal_config.base_virtaddr,
> +			    internal_config.memory,
>   			    PROT_READ | PROT_WRITE,
>   			    MAP_SHARED | MAP_POPULATE, fd, 0);
>   		if (addr == MAP_FAILED) {
  
Tetsuya Mukawa March 9, 2016, 8:33 a.m. UTC | #2
The patches will work on below patch series.
 - [PATCH v2 0/5] virtio support for container

[Changes]
v4 changes:
 - Rebase on latest master.
 - Split patches.
 - To abstract qtest code more, change interface between current virtio
   code and qtest code.
 - Rename qtest.c to qtest_utils.c
 - Change implementation like below.
   - Set pci device information out of qtest abstraction, then pass it to
     qtest to initialize devices.
 - Remove redundant condition checking from qtest_raw_send/recv().
 - Fix return value of qtest_raw_send().

v3 changes:
 - Rebase on latest master.
 - remove "-qtest-virtio" option, then add "--range-virtaddr" and
   "--align-memsize" options.
 - Fix typos in qtest.c

v2 changes:
 - Rebase on above patch seiries.
 - Rebase on master
 - Add "--qtest-virtio" EAL option.
 - Fixes in qtest.c
  - Fix error handling for the case qtest connection is closed.
  - Use eventfd for interrupt messaging.
  - Use linux header for PCI register definitions.
  - Fix qtest_raw_send/recv to handle error correctly.
  - Fix bit mask of PCI_CONFIG_ADDR.
  - Describe memory and ioport usage of qtest guest in qtest.c
  - Remove loop that is for finding PCI devices.


[Abstraction]

Normally, virtio-net PMD only works on VM, because there is no virtio-net device on host.
This patches extend  virtio-net PMD to be able to work on host as virtual PMD.
But we didn't implement virtio-net device as a part of virtio-net PMD.
To prepare virtio-net device for the PMD, start QEMU process with special QTest mode, then connect it from virtio-net PMD through unix domain socket.

The PMD can connect to anywhere QEMU virtio-net device can.
For example, the PMD can connects to vhost-net kernel module and vhost-user backend application.
Similar to virtio-net PMD on QEMU, application memory that uses virtio-net PMD will be shared between vhost backend application.
But vhost backend application memory will not be shared.

Main target of this PMD is container like docker, rkt, lxc and etc.
We can isolate related processes(virtio-net PMD process, QEMU and vhost-user backend process) by container.
But, to communicate through unix domain socket, shared directory will be needed.


[How to use]

 Please use QEMU-2.5.1, or above.
 (So far, QEMU-2.5.1 hasn't been released yet, so please checkout master from QEMU repository)

 - Compile
 Set "CONFIG_RTE_VIRTIO_VDEV_QTEST=y" in config/common_linux.
 Then compile it.

 - Start QEMU like below.
 $ qemu-system-x86_64 \
              -machine pc-i440fx-1.4,accel=qtest \
              -display none -qtest-log /dev/null \
              -qtest unix:/tmp/socket,server \
              -netdev type=tap,script=/etc/qemu-ifup,id=net0,queues=1 \
              -device virtio-net-pci,netdev=net0,mq=on,disable-modern=false,addr=3 \
              -chardev socket,id=chr1,path=/tmp/ivshmem,server \
              -device ivshmem,size=1G,chardev=chr1,vectors=1,addr=4

 - Start DPDK application like below
 $ testpmd -c f -n 1 -m 1024 --no-pci --single-file --qtest-virtio \
             --vdev="eth_qtest_virtio0,qtest=/tmp/socket,ivshmem=/tmp/ivshmem"\
             -- --disable-hw-vlan --txqflags=0xf00 -i

(*1) Please Specify same memory size in QEMU and DPDK command line.
(*2) Should use qemu-2.5.1, or above.
(*3) QEMU process is needed per port.
(*4) virtio-1.0 device are only supported.
(*5) The vhost backends like vhost-net and vhost-user can be specified.
(*6) In most cases, just using above command is enough, but you can also
     specify other QEMU virtio-net options.
(*7) Only checked "pc-i440fx-1.4" machine, but may work with other
     machines. It depends on a machine has piix3 south bridge.
     If the machine doesn't have, virtio-net PMD cannot receive status
     changed interrupts.
(*8) Should not add "--enable-kvm" to QEMU command line.


[Detailed Description]

 - virtio-net device implementation
The PMD uses QEMU virtio-net device. To do that, QEMU QTest functionality is used.
QTest is a test framework of QEMU devices. It allows us to implement a device driver outside of QEMU.
With QTest, we can implement DPDK application and virtio-net PMD as standalone process on host.
When QEMU is invoked as QTest mode, any guest code will not run.
To know more about QTest, see below.
http://wiki.qemu.org/Features/QTest

 - probing devices
QTest provides a unix domain socket. Through this socket, driver process can access to I/O port and memory of QEMU virtual machine.
The PMD will send I/O port accesses to probe pci devices.
If we can find virtio-net and ivshmem device, initialize the devices.
Also, I/O port accesses of virtio-net PMD will be sent through socket, and virtio-net PMD can initialize vitio-net device on QEMU correctly.

 - ivshmem device to share memory
To share memory that virtio-net PMD process uses, ivshmem device will be used.
Because ivshmem device can only handle one file descriptor, shared memory should be consist of one file.
To allocate such a memory, EAL has new option called "--single-file".
Also, the hugepages should be mapped between "1 << 31" to "1 << 44".
To map like above, EAL has one more new option called "-qtest-virtio".
While initializing ivshmem device, we can set BAR(Base Address Register).
It represents which memory QEMU vcpu can access to this shared memory.
We will specify host virtual address of shared memory as this address.
It is very useful because we don't need to apply patch to QEMU to calculate address offset.
(For example, if virtio-net PMD process will allocate memory from shared memory, then specify the virtual address of it to virtio-net register, QEMU virtio-net device can understand it without calculating address offset.)


Tetsuya Mukawa (12):
  virtio: Retrieve driver name from eth_dev
  vhost: Add a function to check virtio device type
  EAL: Add a new "--range-virtaddr" option
  EAL: Add a new "--align-memsize" option
  virtio,qtest: Add QTest utility basic functions
  virtio,qtest: Add pci device initialization function to qtest utils
  virtio,qtest: Add functionality to share memory between QTest guest
  virtio,qtest: Add functionality to handle interrupt
  virtio,qtest: Add misc functions to handle pci information
  virtio: Add QTest support to vtpci abstraction
  virtio: Add QTest support for virtio-net PMD
  docs: add release note for qtest virtio container support

 config/common_base                         |    1 +
 doc/guides/rel_notes/release_16_04.rst     |    3 +
 drivers/net/virtio/Makefile                |    4 +
 drivers/net/virtio/qtest.h                 |   94 +++
 drivers/net/virtio/qtest_utils.c           | 1223 ++++++++++++++++++++++++++++
 drivers/net/virtio/qtest_utils.h           |  355 ++++++++
 drivers/net/virtio/virtio_ethdev.c         |  509 +++++++++++-
 drivers/net/virtio/virtio_ethdev.h         |   32 +
 drivers/net/virtio/virtio_pci.c            |  368 ++++++++-
 drivers/net/virtio/virtio_pci.h            |    9 +-
 lib/librte_eal/common/eal_common_options.c |   17 +
 lib/librte_eal/common/eal_internal_cfg.h   |    3 +
 lib/librte_eal/common/eal_options.h        |    4 +
 lib/librte_eal/linuxapp/eal/eal.c          |   43 +
 lib/librte_eal/linuxapp/eal/eal_memory.c   |   91 ++-
 15 files changed, 2687 insertions(+), 69 deletions(-)
 create mode 100644 drivers/net/virtio/qtest.h
 create mode 100644 drivers/net/virtio/qtest_utils.c
 create mode 100644 drivers/net/virtio/qtest_utils.h
  

Patch

diff --git a/lib/librte_eal/common/eal_common_options.c b/lib/librte_eal/common/eal_common_options.c
index 65bccbd..3b4f789 100644
--- a/lib/librte_eal/common/eal_common_options.c
+++ b/lib/librte_eal/common/eal_common_options.c
@@ -74,6 +74,7 @@  eal_short_options[] =
 const struct option
 eal_long_options[] = {
 	{OPT_BASE_VIRTADDR,     1, NULL, OPT_BASE_VIRTADDR_NUM    },
+	{OPT_RANGE_VIRTADDR,    1, NULL, OPT_RANGE_VIRTADDR_NUM   },
 	{OPT_CREATE_UIO_DEV,    0, NULL, OPT_CREATE_UIO_DEV_NUM   },
 	{OPT_FILE_PREFIX,       1, NULL, OPT_FILE_PREFIX_NUM      },
 	{OPT_HELP,              0, NULL, OPT_HELP_NUM             },
@@ -137,6 +138,8 @@  eal_reset_internal_config(struct internal_config *internal_cfg)
 	for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
 		internal_cfg->hugepage_info[i].lock_descriptor = -1;
 	internal_cfg->base_virtaddr = 0;
+	internal_cfg->range_virtaddr_start = 0;
+	internal_cfg->range_virtaddr_end = 0;
 
 	internal_cfg->syslog_facility = LOG_DAEMON;
 	/* default value from build option */
@@ -985,6 +988,12 @@  eal_check_common_options(struct internal_config *internal_cfg)
 		return -1;
 	}
 
+	if (internal_cfg->base_virtaddr && internal_cfg->range_virtaddr_end) {
+		RTE_LOG(ERR, EAL, "Option --"OPT_RANGE_VIRTADDR" cannot "
+			"be specified together with --"OPT_BASE_VIRTADDR"\n");
+		return -1;
+	}
+
 	return 0;
 }
 
diff --git a/lib/librte_eal/common/eal_internal_cfg.h b/lib/librte_eal/common/eal_internal_cfg.h
index 9117ed9..0734630 100644
--- a/lib/librte_eal/common/eal_internal_cfg.h
+++ b/lib/librte_eal/common/eal_internal_cfg.h
@@ -78,6 +78,8 @@  struct internal_config {
 	volatile unsigned force_sockets;
 	volatile uint64_t socket_mem[RTE_MAX_NUMA_NODES]; /**< amount of memory per socket */
 	uintptr_t base_virtaddr;          /**< base address to try and reserve memory from */
+	uintptr_t range_virtaddr_start;   /**< start address of mappable region */
+	uintptr_t range_virtaddr_end;     /**< end address of mappable region */
 	volatile int syslog_facility;	  /**< facility passed to openlog() */
 	volatile uint32_t log_level;	  /**< default log level */
 	/** default interrupt mode for VFIO */
diff --git a/lib/librte_eal/common/eal_options.h b/lib/librte_eal/common/eal_options.h
index e5da14a..8e4cf1d 100644
--- a/lib/librte_eal/common/eal_options.h
+++ b/lib/librte_eal/common/eal_options.h
@@ -47,6 +47,8 @@  enum {
 	OPT_LONG_MIN_NUM = 256,
 #define OPT_BASE_VIRTADDR     "base-virtaddr"
 	OPT_BASE_VIRTADDR_NUM,
+#define OPT_RANGE_VIRTADDR    "range-virtaddr"
+	OPT_RANGE_VIRTADDR_NUM,
 #define OPT_CREATE_UIO_DEV    "create-uio-dev"
 	OPT_CREATE_UIO_DEV_NUM,
 #define OPT_FILE_PREFIX       "file-prefix"
diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/eal/eal.c
index 82f34f7..80f1995 100644
--- a/lib/librte_eal/linuxapp/eal/eal.c
+++ b/lib/librte_eal/linuxapp/eal/eal.c
@@ -444,6 +444,35 @@  eal_parse_base_virtaddr(const char *arg)
 }
 
 static int
+eal_parse_range_virtaddr(const char *range)
+{
+	char *p, *endptr;
+	uint64_t tmp_start, tmp_end;
+
+	p = strchr(range, '-');
+	if (p == NULL)
+		return -1;
+	*p++ = '\0';
+
+	errno = 0;
+	tmp_start = strtoul(range, &endptr, 0);
+	if ((errno != 0) || endptr == NULL || (*endptr != '\0'))
+		return -1;
+
+	tmp_end = strtoul(p, &endptr, 0);
+	if ((errno != 0) || endptr == NULL || (*endptr != '\0'))
+		return -1;
+
+	if (tmp_start >= tmp_end)
+		return -1;
+
+	internal_config.range_virtaddr_start = tmp_start;
+	internal_config.range_virtaddr_end = tmp_end;
+
+	return 0;
+}
+
+static int
 eal_parse_vfio_intr(const char *mode)
 {
 	unsigned i;
@@ -604,6 +633,16 @@  eal_parse_args(int argc, char **argv)
 			}
 			break;
 
+		case OPT_RANGE_VIRTADDR_NUM:
+			if (eal_parse_range_virtaddr(optarg) < 0) {
+				RTE_LOG(ERR, EAL, "invalid parameter for --"
+						OPT_RANGE_VIRTADDR "\n");
+				eal_usage(prgname);
+				ret = -1;
+				goto out;
+			}
+			break;
+
 		case OPT_VFIO_INTR_NUM:
 			if (eal_parse_vfio_intr(optarg) < 0) {
 				RTE_LOG(ERR, EAL, "invalid parameters for --"
diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c
index a6b3616..d608273 100644
--- a/lib/librte_eal/linuxapp/eal/eal_memory.c
+++ b/lib/librte_eal/linuxapp/eal/eal_memory.c
@@ -251,6 +251,66 @@  aslr_enabled(void)
 }
 
 /*
+ * Find memory space that fits user request.
+ */
+static uintptr_t
+rte_eal_get_free_region(uint64_t pagesz)
+{
+	uint64_t alloc_size, start, end, next_start;
+	uint64_t low_limit, high_limit;
+	uintptr_t addr = 0;
+	char buf[1024], *p;
+	FILE *fp;
+
+	alloc_size = internal_config.memory;
+	low_limit = internal_config.range_virtaddr_start;
+	high_limit = internal_config.range_virtaddr_end;
+
+	/* allocation size should be aligned by page size */
+	if (alloc_size != RTE_ALIGN_CEIL(alloc_size, pagesz)) {
+		rte_panic("Invalid allocation size 0x%lx\n", alloc_size);
+		return NULL;
+	}
+
+	fp = fopen("/proc/self/maps", "r");
+	if (fp == NULL) {
+		rte_panic("Cannot open /proc/self/maps\n");
+		return NULL;
+	}
+
+	next_start = 0;
+	do {
+		start = next_start;
+
+		if ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
+			if (sscanf(p, "%lx-%lx ", &end, &next_start) < 2)
+				break;
+
+			next_start = RTE_ALIGN_CEIL(next_start, alloc_size);
+			end = RTE_ALIGN_CEIL(end, alloc_size) - 1;
+		} else
+			end = UINT64_MAX;
+
+		if (start >= high_limit)
+			break;
+		if (end < low_limit)
+			continue;
+
+		start = RTE_MAX(start, low_limit);
+		end = RTE_MIN(end, high_limit - 1);
+
+		if (end - start >= alloc_size - 1) {
+			addr = start;
+			break;
+		}
+	} while (end != UINT64_MAX);
+
+	fclose(fp);
+
+	return addr;
+}
+
+/*
  * Try to mmap *size bytes in /dev/zero. If it is successful, return the
  * pointer to the mmap'd area and keep *size unmodified. Else, retry
  * with a smaller zone: decrease *size by hugepage_sz until it reaches
@@ -1126,6 +1186,25 @@  rte_eal_hugepage_init(void)
 	/* get pointer to global configuration */
 	mcfg = rte_eal_get_configuration()->mem_config;
 
+	if (internal_config.range_virtaddr_end) {
+		uint64_t pagesize = RTE_PGSIZE_4K;
+		struct hugepage_info *hpi;
+		unsigned n;
+		uintptr_t addr;
+
+		/* determine maximum hugepage size */
+		for (n = 0; n < internal_config.num_hugepage_sizes; n++) {
+			hpi = &internal_config.hugepage_info[n];
+			pagesize = RTE_MAX(hpi->hugepage_sz, pagesize);
+		}
+
+		addr = rte_eal_get_free_region(pagesize);
+		if (addr == 0)
+			RTE_LOG(WARNING, EAL,
+				"no free space to mmap in specified region\n");
+		internal_config.base_virtaddr = addr;
+	}
+
 	/* when hugetlbfs is disabled or single-file option is specified */
 	if (internal_config.no_hugetlbfs || internal_config.single_file) {
 		int fd;
@@ -1158,7 +1237,8 @@  rte_eal_hugepage_init(void)
 			return -1;
 		}
 
-		addr = mmap(NULL, internal_config.memory,
+		addr = mmap((void *)internal_config.base_virtaddr,
+			    internal_config.memory,
 			    PROT_READ | PROT_WRITE,
 			    MAP_SHARED | MAP_POPULATE, fd, 0);
 		if (addr == MAP_FAILED) {