Exemplo n.º 1
0
int
c_thread_start(void *hdl, int nthreads, int n_appthreads)
{
    ctrl_hdl_t *ctrl_hdl = hdl;
    struct thread_alloc_args args = { nthreads, n_appthreads, THREAD_MAIN, 0,
               hdl
    };
    struct c_main_ctx *main_ctx = c_alloc_thread_ctx(&args);
    ctrl_hdl->main_ctx = (void *)main_ctx;

    pthread_create(&main_ctx->cmn_ctx.thread, NULL, c_thread_main, main_ctx);
    return 0;
}
Exemplo n.º 2
0
int
c_thread_start(void *hdl, int nthreads, int n_appthreads)
{
    ctrl_hdl_t *ctrl_hdl = hdl;
    struct thread_alloc_args args = { nthreads, n_appthreads, THREAD_MAIN, 0,
                                      hdl };
    // Kajal: c_main_ctx and c_worker_ctx are different
    // c_main_ctx has the worker pool associated in 
    // struct c_worker_ctx **worker_pool;
    // The main context has the handler to the controller m_ctx->cmn_ctx.c_hdl

    // In this MUL model, we will be operating on the main_ctx and will add the switch
    // information in ctrl_hdl:
    // GHashTable *sw_hash_tbl;
    struct c_main_ctx *main_ctx = c_alloc_thread_ctx(&args);
    ctrl_hdl->main_ctx = (void *)main_ctx;

    // This is with attribute of pre-init
    pthread_create(&main_ctx->cmn_ctx.thread, NULL, c_thread_main, main_ctx);
    return 0;
}
Exemplo n.º 3
0
// This is where the switch threads get created. 
static int
c_main_thread_final_init(struct c_main_ctx *m_ctx)
{
    evutil_socket_t             c_listener;
    struct c_worker_ctx         *w_ctx, **w_ctx_slot;
    struct c_vty_ctx            *vty_ctx;
    struct c_app_ctx            *app_ctx, **app_ctx_slot;
    char                        ipc_path_str[64];
    int                         thread_idx;
    ctrl_hdl_t                  *ctrl_hdl = m_ctx->cmn_ctx.c_hdl;
    struct thread_alloc_args    t_args = { 0, 0, 
                                           THREAD_WORKER, 
                                           0, 
                                           m_ctx->cmn_ctx.c_hdl };

    // Kajal: No event handling
    //m_ctx->cmn_ctx.base = event_base_new();
    //assert(m_ctx->cmn_ctx.base); 

//    m_ctx->worker_pool = calloc(m_ctx->nthreads, sizeof(void *));
//    assert(m_ctx->worker_pool);

/*
    // Worker thread creation 
    for (thread_idx = 0; thread_idx < m_ctx->nthreads; thread_idx++) {
	
	// Indexing the worker thread in the main_ctx
        w_ctx_slot = c_tid_to_ctx_slot(m_ctx, thread_idx);

        t_args.thread_idx = thread_idx;
        w_ctx = c_alloc_thread_ctx(&t_args);
        assert(w_ctx);

        *w_ctx_slot = w_ctx;
        
	// Create Named pipes for IPC communication with main thread
        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_PATH, thread_idx); 
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
            && errno != EEXIST) {
            perror("");
            assert(0);
        }

	// Worker context is created for each thread, but, we will not
	// be using it.
        pthread_create(&w_ctx->cmn_ctx.thread, NULL, c_thread_main, w_ctx);

	// Save the fd for the named pipes in the worker context
	// Main ctx has the w_ctx saved. So main thread knows which is the 
	// fd on which the IPC communication needs to begin 
        w_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        w_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(w_ctx->main_wrk_conn.fd > 0);

	// Worker thread context is saved in the main_ctx and the controller handle
        ctrl_hdl->worker_ctx_list[thread_idx] = (void *)w_ctx;

    }

    // Switch listener 
    // Kajal: No need to create a socket to listen on. 
    // Library will use the callback defined in MUL to do processing.
    c_listener = c_server_socket_create(INADDR_ANY, C_LISTEN_PORT);

    assert(c_listener > 0);
    m_ctx->c_accept_event = event_new(m_ctx->cmn_ctx.base, -1, 
                                      EV_READ|EV_PERSIST,
                                      cc_of_new_conn_event_handler, (void*)m_ctx);
    event_add(m_ctx->c_accept_event, NULL);
*/

    m_ctx->app_pool = calloc(m_ctx->n_appthreads, sizeof(void *));
    assert(m_ctx->app_pool);

    /* Application thread creation */
    for (thread_idx = 0; thread_idx < m_ctx->n_appthreads; thread_idx++) {
        app_ctx_slot = c_tid_to_app_ctx_slot(m_ctx, thread_idx);

        t_args.thread_type = THREAD_APP;
        t_args.thread_idx = thread_idx;
        app_ctx = c_alloc_thread_ctx(&t_args);
        assert(app_ctx);

        *app_ctx_slot = app_ctx;

        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_APP_PATH, thread_idx); 
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
            && errno != EEXIST) {
            perror("");
            assert(0);
        }

        pthread_create(&app_ctx->cmn_ctx.thread, NULL, c_thread_main, app_ctx);

        app_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        app_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(app_ctx->main_wrk_conn.fd > 0);
    }

    /* VTY thread creation */    
    t_args.thread_type = THREAD_VTY;
    vty_ctx = c_alloc_thread_ctx(&t_args);
    assert(vty_ctx);
    pthread_create(&vty_ctx->cmn_ctx.thread, NULL, c_thread_main, vty_ctx);


    /* Application listener */
    c_listener = c_server_socket_create(INADDR_ANY, C_APP_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_accept_event = event_new(m_ctx->cmn_ctx.base, c_listener, 
                                          EV_READ|EV_PERSIST,
                                          c_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_accept_event, NULL);

    c_listener = c_server_socket_create(INADDR_ANY, C_APP_AUX_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_aux_accept_event= event_new(m_ctx->cmn_ctx.base, c_listener, 
                                          EV_READ|EV_PERSIST,
                                          c_aux_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_aux_accept_event, NULL);

    m_ctx->cmn_ctx.run_state = THREAD_STATE_RUNNING;

    c_set_thread_dfl_affinity();

    c_log_debug("%s: running tid(%u)", __FUNCTION__, (unsigned int)pthread_self());
    return 0;
}
Exemplo n.º 4
0
static int
c_main_thread_final_init(struct c_main_ctx *m_ctx)
{
    evutil_socket_t             c_listener;
    struct c_worker_ctx         *w_ctx, **w_ctx_slot;
    struct c_vty_ctx            *vty_ctx;
    struct c_app_ctx            *app_ctx, **app_ctx_slot;
    char                        ipc_path_str[64];
    int                         thread_idx;
    ctrl_hdl_t                  *ctrl_hdl = m_ctx->cmn_ctx.c_hdl;
    struct thread_alloc_args    t_args = { 0, 0,
               THREAD_WORKER,
               0,
               m_ctx->cmn_ctx.c_hdl
    };

    m_ctx->cmn_ctx.base = event_base_new();
    assert(m_ctx->cmn_ctx.base);

    m_ctx->worker_pool = calloc(m_ctx->nthreads, sizeof(void *));
    assert(m_ctx->worker_pool);

    m_ctx->app_pool = calloc(m_ctx->n_appthreads, sizeof(void *));
    assert(m_ctx->app_pool);

    /* Worker thread creation */
    for (thread_idx = 0; thread_idx < m_ctx->nthreads; thread_idx++) {
        w_ctx_slot = c_tid_to_ctx_slot(m_ctx, thread_idx);

        t_args.thread_idx = thread_idx;
        w_ctx = c_alloc_thread_ctx(&t_args);
        assert(w_ctx);

        *w_ctx_slot = w_ctx;

        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_PATH, thread_idx);
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
                && errno != EEXIST) {
            perror("");
            assert(0);
        }

        pthread_create(&w_ctx->cmn_ctx.thread, NULL, c_thread_main, w_ctx);

        w_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        w_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(w_ctx->main_wrk_conn.fd > 0);

        ctrl_hdl->worker_ctx_list[thread_idx] = (void *)w_ctx;

    }

    /* Application thread creation */
    for (thread_idx = 0; thread_idx < m_ctx->n_appthreads; thread_idx++) {
        app_ctx_slot = c_tid_to_app_ctx_slot(m_ctx, thread_idx);

        t_args.thread_type = THREAD_APP;
        t_args.thread_idx = thread_idx;
        app_ctx = c_alloc_thread_ctx(&t_args);
        assert(app_ctx);

        *app_ctx_slot = app_ctx;

        memset(ipc_path_str, 0, sizeof(ipc_path_str));
        snprintf(ipc_path_str, 63, "%s%d", C_IPC_APP_PATH, thread_idx);
        if (mkfifo(ipc_path_str, S_IRUSR | S_IWUSR | S_IWGRP) == -1
                && errno != EEXIST) {
            perror("");
            assert(0);
        }

        pthread_create(&app_ctx->cmn_ctx.thread, NULL, c_thread_main, app_ctx);

        app_ctx->main_wrk_conn.conn_type = C_CONN_TYPE_FILE;
        app_ctx->main_wrk_conn.fd = open(ipc_path_str, O_WRONLY);
        assert(app_ctx->main_wrk_conn.fd > 0);

    }

    /* VTY thread creation */
    t_args.thread_type = THREAD_VTY;
    vty_ctx = c_alloc_thread_ctx(&t_args);
    assert(vty_ctx);
    pthread_create(&vty_ctx->cmn_ctx.thread, NULL, c_thread_main, vty_ctx);


    /* Switch listener */
    c_listener = c_server_socket_create(INADDR_ANY, ctrl_hdl->c_port);
    assert(c_listener > 0);
    m_ctx->c_accept_event = event_new(m_ctx->cmn_ctx.base, c_listener,
                                      EV_READ|EV_PERSIST,
                                      c_accept, (void*)m_ctx);
    event_add(m_ctx->c_accept_event, NULL);

    /* Application listener */
    c_listener = c_server_socket_create(INADDR_ANY, C_APP_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_accept_event = event_new(m_ctx->cmn_ctx.base, c_listener,
                                          EV_READ|EV_PERSIST,
                                          c_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_accept_event, NULL);

    c_listener = c_server_socket_create(INADDR_ANY, C_APP_AUX_LISTEN_PORT);
    assert(c_listener);
    m_ctx->c_app_aux_accept_event= event_new(m_ctx->cmn_ctx.base, c_listener,
                                   EV_READ|EV_PERSIST,
                                   c_aux_app_accept, (void*)m_ctx);
    event_add(m_ctx->c_app_aux_accept_event, NULL);

    m_ctx->cmn_ctx.run_state = THREAD_STATE_RUNNING;

    c_set_thread_dfl_affinity();

    c_log_debug("%s: running tid(%u)", __FUNCTION__, (unsigned int)pthread_self());
    return 0;
}