加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

dpdk helloworld代码分析

(2015-11-27 17:14:51)
标签:

dpdkhelloworld代码分

分类: DPDK
转自网络,仅供学习
http://www.cnblogs.com/chanwai1219/p/3632263.html

dpdk helloworld代码分析


int
MAIN(int argc, char **argv)
{
    int ret;
    unsigned lcore_id;

    ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_panic("Cannot init EAL\n");

    
    RTE_LCORE_FOREACH_SLAVE(lcore_id) {
        rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
    }

    
    lcore_hello(NULL);

    rte_eal_mp_wait_lcore();
    return 0;
}

 

程序的流程如下图所示:

http://images.cnitblog.com/blog/470066/201403/291106468758473.png

 

 

代码首先初始化了Environment Abstraction Layer(EAL),EAL主要提供了以下功能


• Intel® DPDK loading and launching
• Support for multi-process and multi-thread execution types
• Core affinity/assignment procedures
• System memory allocation/de-allocation
• Atomic/lock operations
• Time reference
• PCI bus access
• Trace and debug functions
• CPU feature identification
• Interrupt handling
• Alarm operations

 

num_pages



int
rte_eal_init(int argc, char **argv)
{
    int i, fctret, ret;
    pthread_t thread_id;
    static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
    struct shared_driver *solib = NULL;
    const char *logid;

    
    if (!rte_atomic32_test_and_set(&run_once))
        return -1;

    logid = strrchr(argv[0], '/');
    logid = strdup(logid ? logid + 1: argv[0]);

    thread_id = pthread_self();

    if (rte_eal_log_early_init() < 0)
        rte_panic("Cannot init early logs\n");

    
    if (rte_eal_cpu_init() < 0)
        rte_panic("Cannot detect lcores\n");

    
    fctret = eal_parse_args(argc, argv);
    if (fctret < 0)
        exit(1);

    
    if (internal_config.no_hugetlbfs == 0 &&
            internal_config.process_type != RTE_PROC_SECONDARY &&
            internal_config.xen_dom0_support == 0 &&
            eal_hugepage_info_init() < 0)
        rte_panic("Cannot get hugepage information\n");

    
    if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
        if (internal_config.no_hugetlbfs)
            internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
        else
            internal_config.memory = eal_get_hugepage_mem_size();
    }

    if (internal_config.vmware_tsc_map == 1) {
#ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
        rte_cycles_vmware_tsc_map = 1;
        RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
                "you must have monitor_control.pseudo_perfctr = TRUE\n");
#else
        RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
                "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
#endif
    }

    rte_srand(rte_rdtsc());

    
    rte_config_init();

    
    if (rte_eal_iopl_init() == 0)
        rte_config.flags |= EAL_FLG_HIGH_IOPL;
    
    
    if (rte_eal_pci_init() < 0)
        rte_panic("Cannot init PCI\n");

#ifdef RTE_LIBRTE_IVSHMEM
    if (rte_eal_ivshmem_init() < 0)
        rte_panic("Cannot init IVSHMEM\n");
#endif

    
    if (rte_eal_memory_init() < 0)
        rte_panic("Cannot init memory\n");

    
    eal_hugedirs_unlock();
    
    
    if (rte_eal_memzone_init() < 0)
        rte_panic("Cannot init memzone\n");

    
    if (rte_eal_tailqs_init() < 0)
        rte_panic("Cannot init tail queues for objects\n");

#ifdef RTE_LIBRTE_IVSHMEM
    if (rte_eal_ivshmem_obj_init() < 0)
        rte_panic("Cannot init IVSHMEM objects\n");
#endif

    if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
        rte_panic("Cannot init logs\n");

    
    if (rte_eal_alarm_init() < 0)
        rte_panic("Cannot init interrupt-handling thread\n");

    
    if (rte_eal_intr_init() < 0)
        rte_panic("Cannot init interrupt-handling thread\n");

    
    if (rte_eal_timer_init() < 0)
        rte_panic("Cannot init HPET or TSC timers\n");

    
    eal_check_mem_on_local_socket();

    
    rte_eal_mcfg_complete();

    
    if (rte_eal_non_pci_ethdev_init() < 0)
        rte_panic("Cannot init non-PCI eth_devs\n");

    
    TAILQ_FOREACH(solib, &solib_list, next) {
        solib->lib_handle = dlopen(solib->name, RTLD_NOW);
        if ((solib->lib_handle == NULL) && (solib->name[0] != '/')) {
            
            char sopath[PATH_MAX];
            snprintf(sopath, sizeof(sopath), "./%s", solib->name);
            solib->lib_handle = dlopen(sopath, RTLD_NOW);
        }
        if (solib->lib_handle == NULL)
            RTE_LOG(WARNING, EAL, "%s\n", dlerror());
    }

    RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
        rte_config.master_lcore, (int)thread_id);

    
    RTE_LCORE_FOREACH_SLAVE(i) {

        
        if (pipe(lcore_config[i].pipe_master2slave) < 0)
            rte_panic("Cannot create pipe\n");
        if (pipe(lcore_config[i].pipe_slave2master) < 0)
            rte_panic("Cannot create pipe\n");

        lcore_config[i].state = WAIT;

        
        ret = pthread_create(&lcore_config[i].thread_id, NULL,
                     eal_thread_loop, NULL);
        if (ret != 0)
            rte_panic("Cannot create thread\n");
    }

    
    eal_thread_init_master(rte_config.master_lcore);

    
    
    rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
    rte_eal_mp_wait_lcore();

    return fctret;
}

 

下面主要分析一下内存的初始化过程

 

对于process type是PRIMARY的调用rte_eal_hugepage_init; SECONDARY的调用rte_eal_hugepage_attach;



int
rte_eal_memory_init(void)
{
    RTE_LOG(INFO, EAL, "Setting up memory...\n");
    const int retval = rte_eal_process_type() == RTE_PROC_PRIMARY ?
            rte_eal_hugepage_init() :
            rte_eal_hugepage_attach();
    if (retval < 0)
        return -1;

    if (internal_config.no_shconf == 0 && rte_eal_memdevice_init() < 0)
        return -1;

    return 0;
}

 



static int
rte_eal_hugepage_init(void)
{
    struct rte_mem_config *mcfg;
    struct hugepage_file *hugepage, *tmp_hp = NULL;
    struct hugepage_info used_hp[MAX_HUGEPAGE_SIZES];

    uint64_t memory[RTE_MAX_NUMA_NODES];

    unsigned hp_offset;
    int i, j, new_memseg;
    int nr_hugefiles, nr_hugepages = 0;
    void *addr;
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
    int new_pages_count[MAX_HUGEPAGE_SIZES];
#endif

    memset(used_hp, 0, sizeof(used_hp));

    
    mcfg = rte_eal_get_configuration()->mem_config;

    
    if (internal_config.no_hugetlbfs) {
        
        addr = malloc(internal_config.memory);
        mcfg->memseg[0].phys_addr = (phys_addr_t)(uintptr_t)addr;
        mcfg->memseg[0].addr = addr;
        mcfg->memseg[0].len = internal_config.memory;
        mcfg->memseg[0].socket_id = SOCKET_ID_ANY;
        return 0;
    }


    if (internal_config.xen_dom0_support) {
#ifdef RTE_LIBRTE_XEN_DOM0
        
        if (rte_xen_dom0_memory_init() < 0)
            return -1;
        else
            return 0;
#endif
    }


    
    for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
        
        used_hp[i].hugepage_sz = internal_config.hugepage_info[i].hugepage_sz;

        nr_hugepages += internal_config.hugepage_info[i].num_pages[0];
    }

    
    
    tmp_hp = malloc(nr_hugepages * sizeof(struct hugepage_file));
    if (tmp_hp == NULL)
        goto fail;

    memset(tmp_hp, 0, nr_hugepages * sizeof(struct hugepage_file));

    hp_offset = 0; 

    
    for (i = 0; i < (int)internal_config.num_hugepage_sizes; i ++){
        struct hugepage_info *hpi;

        
        hpi = &internal_config.hugepage_info[i];

        if (hpi->num_pages[0] == 0)
            continue;

        
        
        if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 1) < 0){
            RTE_LOG(DEBUG, EAL, "Failed to mmap %u MB hugepages\n",
                    (unsigned)(hpi->hugepage_sz / 0x100000));
            goto fail;
        }

        
        
        if (find_physaddrs(&tmp_hp[hp_offset], hpi) < 0){
            RTE_LOG(DEBUG, EAL, "Failed to find phys addr for %u MB pages\n",
                    (unsigned)(hpi->hugepage_sz / 0x100000));
            goto fail;
        }

        
        if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){
            RTE_LOG(DEBUG, EAL, "Failed to find NUMA socket for %u MB pages\n",
                    (unsigned)(hpi->hugepage_sz / 0x100000));
            goto fail;
        }

        
        if (sort_by_physaddr(&tmp_hp[hp_offset], hpi) < 0)
            goto fail;

#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
        
        new_pages_count[i] = remap_all_hugepages(&tmp_hp[hp_offset], hpi);
        if (new_pages_count[i] < 0){
            RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
                    (unsigned)(hpi->hugepage_sz / 0x100000));
            goto fail;
        }

        
        hp_offset += new_pages_count[i];
#else
        
        
        if (map_all_hugepages(&tmp_hp[hp_offset], hpi, 0) < 0){
            RTE_LOG(DEBUG, EAL, "Failed to remap %u MB pages\n",
                    (unsigned)(hpi->hugepage_sz / 0x100000));
            goto fail;
        }

        
        
        if (unmap_all_hugepages_orig(&tmp_hp[hp_offset], hpi) < 0)
            goto fail;

        
        hp_offset += hpi->num_pages[0];
#endif
    }

#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
    nr_hugefiles = 0;
    for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
        nr_hugefiles += new_pages_count[i];
    }
#else
    nr_hugefiles = nr_hugepages;
#endif

    
    
    for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++)
        for (j = 0; j < RTE_MAX_NUMA_NODES; j++)
            internal_config.hugepage_info[i].num_pages[j] = 0;

    
    
    for (i = 0; i < nr_hugefiles; i++) {
        int socket = tmp_hp[i].socket_id;

        
        for (j = 0; j < (int) internal_config.num_hugepage_sizes; j++) {
            if (tmp_hp[i].size ==
                    internal_config.hugepage_info[j].hugepage_sz) {
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
                    internal_config.hugepage_info[j].num_pages[socket] +=
                        tmp_hp[i].repeated;
#else
                internal_config.hugepage_info[j].num_pages[socket]++;
#endif
            }
        }
    }

    
    for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
        memory[i] = internal_config.socket_mem[i];

    
    
    nr_hugepages = calc_num_pages_per_socket(memory,
            internal_config.hugepage_info, used_hp,
            internal_config.num_hugepage_sizes);

    
    if (nr_hugepages < 0)
        goto fail;

    
    for (i = 0; i < (int) internal_config.num_hugepage_sizes; i++) {
        for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
            if (used_hp[i].num_pages[j] > 0) {
                RTE_LOG(INFO, EAL,
                        "Requesting %u pages of size %uMB"
                        " from socket %i\n",
                        used_hp[i].num_pages[j],
                        (unsigned)
                            (used_hp[i].hugepage_sz / 0x100000),
                        j);
            }
        }
    }

    
    
    hugepage = create_shared_memory(eal_hugepage_info_path(),
            nr_hugefiles * sizeof(struct hugepage_file));

    if (hugepage == NULL) {
        RTE_LOG(ERR, EAL, "Failed to create shared memory!\n");
        goto fail;
    }
    memset(hugepage, 0, nr_hugefiles * sizeof(struct hugepage_file));

    
    
    if (unmap_unneeded_hugepages(tmp_hp, used_hp,
            internal_config.num_hugepage_sizes) < 0) {
        RTE_LOG(ERR, EAL, "Unmapping and locking hugepages failed!\n");
        goto fail;
    }

    
    
    if (copy_hugepages_to_shared_mem(hugepage, nr_hugefiles,
            tmp_hp, nr_hugefiles) < 0) {
        RTE_LOG(ERR, EAL, "Copying tables to shared memory failed!\n");
        goto fail;
    }

    
    free(tmp_hp);
    tmp_hp = NULL;

    
    for (j = 0; j < RTE_MAX_MEMSEG; j++)
        if (mcfg->memseg[j].addr == NULL) {
            
            j--;
            break;
        }

    for (i = 0; i < nr_hugefiles; i++) {
        new_memseg = 0;

        
        if (i == 0)
            new_memseg = 1;
        else if (hugepage[i].socket_id != hugepage[i-1].socket_id)
            new_memseg = 1;
        else if (hugepage[i].size != hugepage[i-1].size)
            new_memseg = 1;
        else if ((hugepage[i].physaddr - hugepage[i-1].physaddr) !=
            hugepage[i].size)
            new_memseg = 1;
        else if (((unsigned long)hugepage[i].final_va -
            (unsigned long)hugepage[i-1].final_va) != hugepage[i].size)
            new_memseg = 1;

        
        if (new_memseg) {
            j += 1;
            if (j == RTE_MAX_MEMSEG)
                break;

            mcfg->memseg[j].phys_addr = hugepage[i].physaddr;
            mcfg->memseg[j].addr = hugepage[i].final_va;
#ifdef RTE_EAL_SINGLE_FILE_SEGMENTS
            mcfg->memseg[j].len = hugepage[i].size * hugepage[i].repeated;
#else
            mcfg->memseg[j].len = hugepage[i].size;
#endif
            mcfg->memseg[j].socket_id = hugepage[i].socket_id;
            mcfg->memseg[j].hugepage_sz = hugepage[i].size;
        }
        
        else {
            mcfg->memseg[j].len += mcfg->memseg[j].hugepage_sz;
        }
        hugepage[i].memseg_id = j;
    }

    if (i <<span style="margin: 0px; padding: 0px; line-height: 12px; font-family: 'Courier New' !important;"> nr_hugefiles) {
        RTE_LOG(ERR, EAL, "Can only reserve %d pages "
            "from %d requested\n"
            "Current %s=%d is not enough\n"
            "Please either increase it or request less amount "
            "of memory.\n",
            i, nr_hugefiles, RTE_STR(CONFIG_RTE_MAX_MEMSEG),
            RTE_MAX_MEMSEG);
        return (-ENOMEM);
    }

    return 0;

fail:
    if (tmp_hp)
        free(tmp_hp);
    return -1;
}

0

阅读 收藏 喜欢 打印举报/Report
后一篇:字典树介绍
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有