예제 #1
0
파일: ft.c 프로젝트: heecheolsong/indigo
ft_instance_t
ft_create(ft_config_t *config)
{
    ft_instance_t ft;
    int bytes;
    int idx;

    /* Allocate the flow table itself */
    ft = aim_zmalloc(sizeof(*ft));
    INDIGO_MEM_COPY(&ft->config,  config, sizeof(ft_config_t));

    list_init(&ft->all_list);

    /* Allocate and init buckets for each search type */
    bytes = sizeof(list_head_t) * config->strict_match_bucket_count;
    ft->strict_match_buckets = aim_zmalloc(bytes);
    for (idx = 0; idx < config->strict_match_bucket_count; idx++) {
        list_init(&ft->strict_match_buckets[idx]);
    }

    bytes = sizeof(list_head_t) * config->flow_id_bucket_count;
    ft->flow_id_buckets = aim_zmalloc(bytes);
    for (idx = 0; idx < config->flow_id_bucket_count; idx++) {
        list_init(&ft->flow_id_buckets[idx]);
    }

    bytes = sizeof(list_head_t) * (1 << FT_COOKIE_PREFIX_LEN);
    ft->cookie_buckets = aim_zmalloc(bytes);
    for (idx = 0; idx < (1 << FT_COOKIE_PREFIX_LEN); idx++) {
        list_init(&ft->cookie_buckets[idx]);
    }

    for (idx = 0; idx < FT_MAX_TABLES; idx++) {
        ft_table_t *table = &ft->tables[idx];
        table->checksum_buckets_size = 128;
        table->checksum_shift = 64 - aim_log2_u32(table->checksum_buckets_size);
        table->checksum_buckets = aim_zmalloc(table->checksum_buckets_size * sizeof(uint64_t));
    }

    return ft;
}
예제 #2
0
int aim_main(int argc, char* argv[])
{
    int i;

    {
        const char* tstStrings[] = { "This", "is", "a", "complete", "sentence." };
        char* join = aim_strjoin(" ", tstStrings, AIM_ARRAYSIZE(tstStrings));
        if(strcmp(join, "This is a complete sentence.")) {
            printf("fail: join='%s'\n", join);
        }
        aim_free(join);
    }

    for(i = 0; i < argc; i++) {
        aim_printf(&aim_pvs_stdout, "arg%d: '%s'\n", i, argv[i]);
    }

    {
        /* Test data */
        char data[2500];
        memset(data, 0xFF, sizeof(data));
        aim_printf(&aim_pvs_stdout, "data is %{data}", data, sizeof(data));
    }

    {
        char* sdata = "DEADBEEFCAFE";
        char* data;
        int size;
        aim_sparse(&sdata, &aim_pvs_stdout, "{data}", &data, &size);
        aim_printf(&aim_pvs_stdout, "data is %{data}\n", data, size);
        aim_free(data);
    }

    utest_list();

    AIM_LOG_MSG("Should print 1-27");
    AIM_LOG_MSG("%d %d %d %d %d %d %d %d %d "
                "%d %d %d %d %d %d %d %d %d "
                "%d %d %d %d %d %d %d %d %d",
                1, 2, 3, 4, 5, 6, 7, 8, 9,
                10, 11, 12, 13, 14, 15, 16, 17, 18,
                19, 20, 21, 22, 23, 24, 25, 26, 27);


    aim_printf(&aim_pvs_stdout, "aim_pvs_stdout from %s:%d\n",
               __FILE__, __LINE__);


    {
        char c;
        aim_pvs_t* pvs = aim_pvs_buffer_create();
        aim_printf(pvs, "\nConsider ");
        aim_printf(pvs, "%s ", "the");
        aim_printf(pvs, "alphabet: ");
        for(c = 'A'; c <= 'Z'; c++) {
            aim_printf(pvs, "%c", c);
        }
        aim_printf(pvs, "\n");
        {
            char* s = aim_pvs_buffer_get(pvs);
            aim_printf(&aim_pvs_stdout, "first: %s", s);
            free(s);
            aim_printf(pvs, "(second)");
            s = aim_pvs_buffer_get(pvs);
            aim_printf(&aim_pvs_stdout, "second: %s", s);
            free(s);
            aim_pvs_destroy(pvs);
        }
        {
            aim_ratelimiter_t rl;
            aim_ratelimiter_init(&rl, 10, 5, NULL);

            /* 5 (6?) tokens available at t=0 */
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) == 0);
            assert(aim_ratelimiter_limit(&rl, 0) < 0);

            /* Another token at t=10 */
            assert(aim_ratelimiter_limit(&rl, 10) == 0);
            assert(aim_ratelimiter_limit(&rl, 10) < 0);

            /* Nothing at t=15 */
            assert(aim_ratelimiter_limit(&rl, 15) < 0);

            /* 4 more tokens granted by t=50 */
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) == 0);
            assert(aim_ratelimiter_limit(&rl, 50) < 0);
        }
        {
            aim_printf(&aim_pvs_stdout, "valgrind_status=%d\n",
                       aim_valgrind_status());
        }

        AIM_LOG_MSG("%{aim_error}", AIM_ERROR_PARAM);
    }

    /* Test integer power of 2 utilities */
    {
       assert(!aim_is_pow2_u32(0));
       assert(aim_log2_u32(0) == 0);

       assert(aim_is_pow2_u32(1));
       assert(aim_log2_u32(1) == 0);

       assert(aim_is_pow2_u32(2));
       assert(aim_log2_u32(2) == 1);

       assert(!aim_is_pow2_u32(3));
       assert(aim_log2_u32(3) == 1);

       assert(aim_is_pow2_u32(4));
       assert(aim_log2_u32(4) == 2);

       assert(!aim_is_pow2_u32(5));
       assert(aim_log2_u32(5) == 2);

       assert(!aim_is_pow2_u32(6));
       assert(aim_log2_u32(6) == 2);

       assert(!aim_is_pow2_u32(7));
       assert(aim_log2_u32(7) == 2);

       assert(aim_is_pow2_u32(8));
       assert(aim_log2_u32(8) == 3);

       assert(!aim_is_pow2_u32(2147483647));
       assert(aim_log2_u32(2147483647) == 30);

       assert(aim_is_pow2_u32(2147483648));
       assert(aim_log2_u32(2147483648) == 31);

       assert(!aim_is_pow2_u32(4294967295));
       assert(aim_log2_u32(4294967295) == 31);
    }

    return 0;
}