doc: fix references to old binding script
[dpdk.git] / lib / librte_ivshmem / rte_ivshmem.c
index 4a5e7b0..c26edb6 100644 (file)
@@ -1,13 +1,13 @@
 /*-
  *   BSD LICENSE
- * 
+ *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  *   All rights reserved.
- * 
+ *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
@@ -17,7 +17,7 @@
  *     * Neither the name of Intel Corporation nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
- * 
+ *
  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -229,7 +229,7 @@ get_hugefile_by_virt_addr(uint64_t virt_addr, struct memseg_cache_entry * e)
        }
 
        /* calculate offset and copy the file path */
-       rte_snprintf(e->filepath, RTE_PTR_DIFF(path_end, start) + 1, "%s", start);
+       snprintf(e->filepath, RTE_PTR_DIFF(path_end, start) + 1, "%s", start);
 
        e->offset = virt_addr - start_addr;
 
@@ -377,6 +377,8 @@ build_config(struct rte_ivshmem_metadata * metadata)
                        for (j = biggest_idx - 1; j >= i; j--) {
                                memcpy(&pages[j+1], &pages[j], sizeof(struct rte_memseg));
                                memset(&pages[j], 0, sizeof(struct rte_memseg));
+                               if (j == 0)
+                                       break;
                        }
 
                        /* put old biggest segment to its new place */
@@ -469,10 +471,21 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
                struct ivshmem_config * config)
 {
        struct rte_ivshmem_metadata_entry * entry;
-       unsigned i;
+       unsigned i, idx;
+       struct rte_mem_config *mcfg;
+
+       if (mz->len == 0) {
+               RTE_LOG(ERR, EAL, "Trying to add an empty memzone\n");
+               return -1;
+       }
 
        rte_spinlock_lock(&config->sl);
 
+       mcfg = rte_eal_get_configuration()->mem_config;
+
+       /* it prevents the memzone being freed while we add it to the metadata */
+       rte_rwlock_write_lock(&mcfg->mlock);
+
        /* find free slot in this config */
        for (i = 0; i < RTE_DIM(config->metadata->entry); i++) {
                entry = &config->metadata->entry[i];
@@ -503,9 +516,17 @@ add_memzone_to_metadata(const struct rte_memzone * mz,
                goto fail;
        }
 
+       idx = ((uintptr_t)mz - (uintptr_t)mcfg->memzone);
+       idx = idx / sizeof(struct rte_memzone);
+
+       /* mark the memzone not freeable */
+       mcfg->memzone[idx].ioremap_addr = mz->phys_addr;
+
+       rte_rwlock_write_unlock(&mcfg->mlock);
        rte_spinlock_unlock(&config->sl);
        return 0;
 fail:
+       rte_rwlock_write_unlock(&mcfg->mlock);
        rte_spinlock_unlock(&config->sl);
        return -1;
 }
@@ -527,26 +548,40 @@ add_ring_to_metadata(const struct rte_ring * r,
 }
 
 static int
-add_mempool_to_metadata(const struct rte_mempool * mp,
-               struct ivshmem_config * config)
+add_mempool_memzone_to_metadata(const void *addr,
+               struct ivshmem_config *config)
 {
-       struct rte_memzone * mz;
-       int ret;
+       struct rte_memzone *mz;
 
-       mz = get_memzone_by_addr(mp);
-       ret = 0;
+       mz = get_memzone_by_addr(addr);
 
        if (!mz) {
                RTE_LOG(ERR, EAL, "Cannot find memzone for mempool!\n");
                return -1;
        }
 
-       /* mempool consists of memzone and ring */
-       ret = add_memzone_to_metadata(mz, config);
+       return add_memzone_to_metadata(mz, config);
+}
+
+static int
+add_mempool_to_metadata(const struct rte_mempool *mp,
+               struct ivshmem_config *config)
+{
+       struct rte_mempool_memhdr *memhdr;
+       int ret;
+
+       ret = add_mempool_memzone_to_metadata(mp, config);
        if (ret < 0)
                return -1;
 
-       return add_ring_to_metadata(mp->ring, config);
+       STAILQ_FOREACH(memhdr, &mp->mem_list, next) {
+               ret = add_mempool_memzone_to_metadata(memhdr->addr, config);
+               if (ret < 0)
+                       return -1;
+       }
+
+       /* mempool consists of memzone and ring */
+       return add_ring_to_metadata(mp->pool_data, config);
 }
 
 int
@@ -606,7 +641,7 @@ rte_ivshmem_metadata_add_mempool(const struct rte_mempool * mp, const char * nam
 static inline void
 ivshmem_config_path(char *buffer, size_t bufflen, const char *name)
 {
-       rte_snprintf(buffer, bufflen, IVSHMEM_CONFIG_FILE_FMT, name);
+       snprintf(buffer, bufflen, IVSHMEM_CONFIG_FILE_FMT, name);
 }
 
 
@@ -707,7 +742,7 @@ int rte_ivshmem_metadata_create(const char *name)
        /* Metadata setup */
        memset(ivshmem_config->metadata, 0, sizeof(struct rte_ivshmem_metadata));
        ivshmem_config->metadata->magic_number = IVSHMEM_MAGIC;
-       rte_snprintf(ivshmem_config->metadata->name,
+       snprintf(ivshmem_config->metadata->name,
                        sizeof(ivshmem_config->metadata->name), "%s", name);
 
        rte_spinlock_unlock(&global_cfg_sl);
@@ -738,7 +773,7 @@ rte_ivshmem_metadata_cmdline_generate(char *buffer, unsigned size, const char *n
        rte_spinlock_lock(&config->sl);
 
        /* prepare metadata file path */
-       rte_snprintf(cfg_file_path, sizeof(cfg_file_path), IVSHMEM_CONFIG_FILE_FMT,
+       snprintf(cfg_file_path, sizeof(cfg_file_path), IVSHMEM_CONFIG_FILE_FMT,
                        config->metadata->name);
 
        ms_cache = config->memseg_cache;
@@ -754,7 +789,7 @@ rte_ivshmem_metadata_cmdline_generate(char *buffer, unsigned size, const char *n
                entry = &ms_cache[iter];
 
                /* Offset and sizes within the current pathname */
-               tmplen = rte_snprintf(cmdline_ptr, remaining_len, IVSHMEM_QEMU_CMD_FD_FMT,
+               tmplen = snprintf(cmdline_ptr, remaining_len, IVSHMEM_QEMU_CMD_FD_FMT,
                                entry->filepath, entry->offset, entry->len);
 
                shared_mem_size += entry->len;
@@ -775,9 +810,9 @@ rte_ivshmem_metadata_cmdline_generate(char *buffer, unsigned size, const char *n
        zero_size = total_size - shared_mem_size - METADATA_SIZE_ALIGNED;
 
        /* add /dev/zero to command-line to fill the space */
-       tmplen = rte_snprintf(cmdline_ptr, remaining_len, IVSHMEM_QEMU_CMD_FD_FMT,
+       tmplen = snprintf(cmdline_ptr, remaining_len, IVSHMEM_QEMU_CMD_FD_FMT,
                        "/dev/zero",
-                       0x0,
+                       (uint64_t)0x0,
                        zero_size);
 
        cmdline_ptr = RTE_PTR_ADD(cmdline_ptr, tmplen);
@@ -790,9 +825,9 @@ rte_ivshmem_metadata_cmdline_generate(char *buffer, unsigned size, const char *n
        }
 
        /* add metadata file to the end of command-line */
-       tmplen = rte_snprintf(cmdline_ptr, remaining_len, IVSHMEM_QEMU_CMD_FD_FMT,
+       tmplen = snprintf(cmdline_ptr, remaining_len, IVSHMEM_QEMU_CMD_FD_FMT,
                        cfg_file_path,
-                       0x0,
+                       (uint64_t)0x0,
                        METADATA_SIZE_ALIGNED);
 
        cmdline_ptr = RTE_PTR_ADD(cmdline_ptr, tmplen);
@@ -812,7 +847,7 @@ rte_ivshmem_metadata_cmdline_generate(char *buffer, unsigned size, const char *n
                return -1;
        }
        /* complete the command-line */
-       rte_snprintf(buffer, size,
+       snprintf(buffer, size,
                        IVSHMEM_QEMU_CMD_LINE_HEADER_FMT,
                        total_size >> 20,
                        cmdline);