Esempio n. 1
0
static inline bool
s_initialize(test_thread_t tt,
             lagopus_mutex_t start_lock,
             lagopus_bbq_t bbq,
             ssize_t n,
             const char *name) {
  bool ret = false;

  if (tt != NULL) {
    lagopus_result_t r;
    if ((r = lagopus_thread_create((lagopus_thread_t *)&tt,
                                   s_test_thread_main,
                                   s_test_thread_finalize,
                                   s_test_thread_destroy,
                                   name, NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(r);
      goto done;
    }
    tt->m_start_lock = start_lock;
    tt->m_q = bbq;
    tt->m_n_puts = n;
    ret = true;
  }
done:
  return ret;
}
Esempio n. 2
0
static inline bool
s_check_create_common(lagopus_thread_t *thd_ptr, lagopus_result_t require_ret,
                      lagopus_thread_main_proc_t mproc, const char *name) {
  bool result = false;
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  ret = lagopus_thread_create(thd_ptr, mproc, finalize_proc, freeup_proc,
                              name, NULL);
  if (ret == LAGOPUS_RESULT_OK) {
    ret = lagopus_thread_free_when_destroy(thd_ptr);
    if (ret == require_ret) {
      result = s_check_get_thread_id(thd_ptr, LAGOPUS_RESULT_NOT_STARTED);
    } else {
      lagopus_perror(ret);
      TEST_FAIL_MESSAGE("create failed");
      result = false;
    }
  } else {
    if (ret == require_ret) {
      result = true;
    } else {
      lagopus_perror(ret);
      TEST_FAIL_MESSAGE("create failed");
      result = false;
    }
  }
  return result;
}
Esempio n. 3
0
static inline bool
s_initialize(test_thread_t tt, size_t n) {
  bool ret = false;

  if (tt != NULL) {
    tt->m_data = (int *)malloc(n * sizeof(int));
    if (tt->m_data != NULL) {
      lagopus_result_t r;
      if ((r = lagopus_mutex_create(&(tt->m_lock))) != LAGOPUS_RESULT_OK) {
        lagopus_perror(r);
        goto initfailure;
      }
      if ((r = lagopus_thread_create((lagopus_thread_t *)&tt,
                                     s_test_thread_main,
                                     s_test_thread_finalize,
                                     s_test_thread_destroy,
                                     "test", NULL)) != LAGOPUS_RESULT_OK) {
        lagopus_perror(r);
        goto initfailure;
      }
      tt->m_n_data = n;
      tt->m_is_operational = false;
      tt->m_stop_requested = false;

      ret = true;
    }
  initfailure:
    if (ret != true) {
      free((void *)tt->m_data);
    }
  }

  return ret;
}
Esempio n. 4
0
void
test_thread_create_params(void) {
  lagopus_thread_t m_thread = NULL;
  TEST_ASSERT_EQUAL_MESSAGE(
    LAGOPUS_RESULT_INVALID_ARGS,
    lagopus_thread_create(NULL, main_proc, finalize_proc,
                          freeup_proc, "m_thread", NULL),
    "thread is null");
  lagopus_thread_destroy(&m_thread);

  m_thread = NULL;
  TEST_ASSERT_EQUAL_MESSAGE(
    LAGOPUS_RESULT_INVALID_ARGS,
    lagopus_thread_create(&m_thread, NULL, finalize_proc,
                          freeup_proc, "m_thread", NULL),
    "main_proc is null");
  lagopus_thread_destroy(&m_thread);

  m_thread = NULL;
  TEST_ASSERT_EQUAL_MESSAGE(
    LAGOPUS_RESULT_OK,
    lagopus_thread_create(&m_thread, main_proc, NULL,
                          freeup_proc, "m_thread", NULL),
    "finalize_proc is null");
  lagopus_thread_destroy(&m_thread);

  m_thread = NULL;
  TEST_ASSERT_EQUAL_MESSAGE(
    LAGOPUS_RESULT_OK,
    lagopus_thread_create(&m_thread, main_proc, finalize_proc,
                          NULL, "m_thread", NULL),
    "freeup_proc is null");
  lagopus_thread_destroy(&m_thread);

  m_thread = NULL;
  TEST_ASSERT_EQUAL_MESSAGE(
    LAGOPUS_RESULT_OK,
    lagopus_thread_create(&m_thread, main_proc, finalize_proc,
                          freeup_proc, NULL, NULL),
    "thread_name is null");
  lagopus_thread_destroy(&m_thread);
}
Esempio n. 5
0
lagopus_result_t
dataplane_initialize(int argc,
                     const char *const argv[],
                     __UNUSED void *extarg,
                     lagopus_thread_t **thdptr) {
#ifdef HAVE_DPDK
  static struct dataplane_arg dparg;
#endif /* HAVE_DPDK */
  static struct dataplane_arg sockarg;
  static struct dataplane_arg timerarg;
  lagopus_result_t nb_ports;

  nb_ports = lagopus_dataplane_init(argc, argv);
  if (nb_ports < 0) {
    lagopus_msg_fatal("lagopus_dataplane_init failed\n");
    return nb_ports;
  }
  dp_api_init();
#ifdef HAVE_DPDK
  if (rawsocket_only_mode != true) {
    dparg.threadptr = &dpdk_thread;
    dparg.lock = &dpdk_lock;
    lagopus_thread_create(&dpdk_thread, dpdk_thread_loop,
                          dp_finalproc, dp_freeproc, "dataplane", &dparg);

    if (lagopus_mutex_create(&dpdk_lock) != LAGOPUS_RESULT_OK) {
      lagopus_exit_fatal("lagopus_mutex_create");
    }
  }
#endif /* HAVE_DPDK */

  lagopus_meter_init();
  lagopus_register_action_hook = lagopus_set_action_function;
  lagopus_register_instruction_hook = lagopus_set_instruction_function;
  flowinfo_init();

  timerarg.threadptr = &timer_thread;
  timerarg.lock = &timer_lock;
  timerarg.running = NULL;
  timerthread_initialize(&timerarg);

  sockarg.threadptr = &sock_thread;
  sockarg.lock = &sock_lock;
  sockarg.running = NULL;
  sockthread_initialize(&sockarg);

  *thdptr = &sock_thread;

  return LAGOPUS_RESULT_OK;
}
Esempio n. 6
0
static lagopus_result_t
dummy_module_initialize(int argc,
                        const char *const argv[],
                        void *extarg,
                        lagopus_thread_t **thdptr) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;

  (void)extarg;

  lagopus_msg_debug(5, "called.\n");

  if (thdptr != NULL) {
    *thdptr = NULL;
  }

  s_lock();
  {
    if (s_is_initialized == false) {
      int i;

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

      ret = lagopus_thread_create(&s_thd,
                                  s_dummy_thd_main,
                                  s_dummy_thd_finalize,
                                  s_dummy_thd_destroy,
                                  "dummy", NULL);
      if (ret == LAGOPUS_RESULT_OK) {
        s_is_initialized = true;
        if (thdptr != NULL) {
          *thdptr = &s_thd;
        }
      }
    } else {
      ret = LAGOPUS_RESULT_OK;
    }
  }
  s_unlock();

  return ret;
}
Esempio n. 7
0
int
main(int argc, char const *const argv[]) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  int cpu = 0;
  lagopus_thread_t thd = NULL;

  if (argc > 1) {
    int tmp;
    if (lagopus_str_parse_int32(argv[1], &tmp) == LAGOPUS_RESULT_OK &&
        tmp > 0) {
      cpu = tmp;
    }
  }

  if ((ret = lagopus_thread_create(&thd,
                                   s_main_proc,
                                   NULL,
                                   NULL,
                                   (const char *) "thread",
                                   (void *) NULL)) == LAGOPUS_RESULT_OK) {
    if ((ret = lagopus_thread_set_cpu_affinity(&thd,
               -1)) == LAGOPUS_RESULT_OK &&
        (ret = lagopus_thread_set_cpu_affinity(&thd,
               cpu)) == LAGOPUS_RESULT_OK) {
      if ((ret = lagopus_thread_start(&thd, false)) == LAGOPUS_RESULT_OK) {
        ret = lagopus_thread_wait(&thd, -1LL);
      }
    }
  }

  if (ret != LAGOPUS_RESULT_OK) {
    lagopus_perror(ret);
    return 1;
  } else {
    return 0;
  }
}
Esempio n. 8
0
static void
initialize_internal(void) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  void *logger_client_arg = NULL;
  char *master_agentx_socket = NULL;
  char *ping_interval_string = NULL;
  uint16_t ping_interval = 0;

  state = SNMPMGR_NONE;

  /* `is_thread` is able to be changed only once here */
  is_thread = is_thread_dummy;
  if (is_thread == false && thdptr_dummy != NULL) {
    *thdptr_dummy = NULL;
  } else if (thdptr_dummy != NULL) {
    *thdptr_dummy = &snmpmgr;
  } else {
    /* never reached! */
    return;
  }

  if (lagopus_mutex_create(&snmp_lock) != LAGOPUS_RESULT_OK) {
    return;
  }

  if (lagopus_mutex_create(&snmp_state_lock) != LAGOPUS_RESULT_OK) {
    return;
  }

  snmp_set_do_debugging(false);

  /* setup Net-SNMP logger to use the lagopus logging function */
  if (snmp_register_callback(SNMP_CALLBACK_LIBRARY,
                             SNMP_CALLBACK_LOGGING,
                             snmp_log_callback_wrapper,
                             logger_client_arg) != SNMPERR_SUCCESS) {
    return;
  }
  snmp_enable_calllog();

  /* setup the SNMP module to be Agentx subagent */
  netsnmp_enable_subagent();

#ifdef THE_CONFSYS_ERA
  master_agentx_socket = config_get("snmp master-agentx-socket WORD");
  if (master_agentx_socket == NULL) {
    config_set_default("snmp master-agentx-socket WORD",
                       (char *)DEFAULT_SNMPMGR_AGENTX_SOCKET);
    master_agentx_socket = config_get("snmp master-agentx-socket WORD");
  }
#else
  /*
   * FIXME:
   *	Fetch it from the datastore.
   */
  master_agentx_socket = (char *)DEFAULT_SNMPMGR_AGENTX_SOCKET;
#endif /* THE_CONFSYS_ERA */
  lagopus_msg_debug(25, "master agentx socket is %s\n", master_agentx_socket);

  if (netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
                            NETSNMP_DS_AGENT_X_SOCKET,
                            master_agentx_socket)
      != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  /* don't read/write configuration files */
  if (netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
                             NETSNMP_DS_LIB_DONT_PERSIST_STATE,
                             true) != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }
  if (netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
                             NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD,
                             true) != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  /* the interval [sec] to send ping to AgentX master agent */
#ifdef THE_CONFSYS_ERA
  ping_interval_string = config_get("snmp ping-interval-second 1-255");
  if (ping_interval_string != NULL &&
      (ret = check_ping_interval(ping_interval_string, &ping_interval))
      != LAGOPUS_RESULT_OK) {
    config_set_default("snmp ping-interval-second 1-255",
                       (char *) __STR__(DEFAULT_SNMPMGR_AGENTX_PING_INTERVAL_SEC));
    ping_interval_string = config_get("snmp ping-interval-second 1-255");
    ping_interval = DEFAULT_SNMPMGR_AGENTX_PING_INTERVAL_SEC; /* default value */
  }
#else
  /*
   * FIXME:
   *	Fetch it from the datastore.
   */
  ping_interval = DEFAULT_SNMPMGR_AGENTX_PING_INTERVAL_SEC;
#endif /* THE_CONFSYS_ERA */

  if (netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
                         NETSNMP_DS_AGENT_AGENTX_PING_INTERVAL,
                         (int)ping_interval)
      != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  if (init_agent(SNMP_TYPE) != SNMPERR_SUCCESS) {
    initialize_ret = LAGOPUS_RESULT_SNMP_API_ERROR;
    return;
  }

  if ((ret = lagopus_hashmap_create(&iftable_entry_hash,
                                    LAGOPUS_HASHMAP_TYPE_STRING,
                                    delete_entry)) != LAGOPUS_RESULT_OK) {
    lagopus_perror(ret);
    exit(1);
  }

  init_ifTable();
  init_ifNumber();
  init_dot1dBaseBridgeAddress();
  init_dot1dBaseNumPorts();
  init_dot1dBasePortTable();
  init_dot1dBaseType();

  if (is_thread == false) {
    initialize_ret = LAGOPUS_RESULT_OK;
  } else {
    keep_running = true;
    initialize_ret = lagopus_thread_create(&snmpmgr,
                                           snmpmgr_thread_loop,
                                           snmpmgr_thread_finally_called,
                                           NULL,
                                           "snmpmgr",
                                           NULL);
  }

  lagopus_msg_info("SNMP manager initialized.\n");
}
Esempio n. 9
0
int
main(int argc, char const *const argv[]) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  int i;
  int n = 10000;
  lagopus_thread_t thread = NULL;
  pthread_t tid = LAGOPUS_INVALID_THREAD;

  lagopus_log_set_debug_level(10);

  if (argc > 1) {
    int tmp;
    if (lagopus_str_parse_int32(argv[1], &tmp) == LAGOPUS_RESULT_OK &&
        tmp > 0) {
      n = tmp;
    }
  }

  /* create, start, wait, destroy */
  for (i = 0; i < n; i++) {
    thread = NULL;

    if ((ret = lagopus_thread_create(
                 (lagopus_thread_t *)&thread,
                 (lagopus_thread_main_proc_t)s_main_proc,
                 (lagopus_thread_finalize_proc_t)s_finalize_proc,
                 (lagopus_thread_freeup_proc_t)s_freeup_proc,
                 (const char *) "thread",
                 (void *) NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't create a thread.\n");
    } else {
      fprintf(stderr, "create a thread [%d]\n", i);
      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          != LAGOPUS_RESULT_NOT_STARTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't start the thread.\n");
    } else {
      ret = lagopus_thread_get_pthread_id(&thread, &tid);
      if (ret == LAGOPUS_RESULT_OK ||
          ret == LAGOPUS_RESULT_ALREADY_HALTED) {
        fprintf(stderr, " start thread:  %lu\n", tid);
      } else {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, false);

    if ((ret = lagopus_thread_wait(&thread, 1000LL * 1000LL * 1000LL)) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_msg_error("Can't wait the thread.\n");
    } else {
      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          == LAGOPUS_RESULT_OK) {
        lagopus_msg_error(
          "Thread has been completed, but I can get thread ID, %lu\n",
          tid);
      } else if (ret != LAGOPUS_RESULT_ALREADY_HALTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, false);

    lagopus_thread_destroy(&thread);
    fprintf(stderr, "destroy the thread [%d]\n", i);

    fprintf(stderr, "\n");
  }

  /* create, start, cancel, destroy */
  for (i = 0; i < n; i++) {
    thread = NULL;

    if ((ret = lagopus_thread_create(
                 (lagopus_thread_t *)&thread,
                 (lagopus_thread_main_proc_t)s_main_proc_with_sleep,
                 (lagopus_thread_finalize_proc_t)s_finalize_proc,
                 (lagopus_thread_freeup_proc_t)s_freeup_proc,
                 (const char *) "thread",
                 (void *) NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't create a thread.\n");
    } else {
      fprintf(stderr, "create a thread [%d]\n", i);

      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          != LAGOPUS_RESULT_NOT_STARTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't start the thread.\n");
    } else {
      ret = lagopus_thread_get_pthread_id(&thread, &tid);
      if (ret == LAGOPUS_RESULT_OK ||
          ret == LAGOPUS_RESULT_ALREADY_HALTED) {
        fprintf(stderr, " start thread:  %lu\n", tid);
      } else {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, false);

    if ((ret = lagopus_thread_cancel(&thread)) !=
        LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_msg_error("Can't cancel the thread.\n");
    } else {
      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          == LAGOPUS_RESULT_OK) {
        lagopus_msg_error(
          "Thread has been canceled, but I can get thread ID, %lu\n",
          tid);
      } else if (ret != LAGOPUS_RESULT_NOT_STARTED &&
                 ret != LAGOPUS_RESULT_ALREADY_HALTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    s_check_cancelled(&thread, true);

    lagopus_thread_destroy(&thread);
    fprintf(stderr, "destroy the thread [%d]\n", i);

    fprintf(stderr, "\n");
  }

  /* create, start, destroy */
  for (i = 0; i < n; i++) {
    thread = NULL;

    if ((ret = lagopus_thread_create(
                 (lagopus_thread_t *)&thread,
                 (lagopus_thread_main_proc_t)s_main_proc_with_sleep,
                 (lagopus_thread_finalize_proc_t)s_finalize_proc,
                 (lagopus_thread_freeup_proc_t)s_freeup_proc,
                 (const char *) "thread",
                 (void *) NULL)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't create a thread.\n");
    } else {
      fprintf(stderr, "create a thread [%d]\n", i);

      if ((ret = lagopus_thread_get_pthread_id(&thread, &tid))
          != LAGOPUS_RESULT_NOT_STARTED) {
        lagopus_perror(ret);
        goto done;
      }
    }

    if ((ret = lagopus_thread_start(&thread, false)) != LAGOPUS_RESULT_OK) {
      lagopus_perror(ret);
      lagopus_exit_fatal("Can't start the thread.\n");
    } else {
      ret = lagopus_thread_get_pthread_id(&thread, &tid);
      if (ret == LAGOPUS_RESULT_OK ||
          ret == LAGOPUS_RESULT_ALREADY_HALTED) {
        fprintf(stderr, " start thread:  %lu\n", tid);
      } else {
        lagopus_perror(ret);
        goto done;
      }
    }

    lagopus_thread_destroy(&thread);
    fprintf(stderr, "destroy the thread [%d]\n", i);

    fprintf(stderr, "\n");
  }

done:
  return (ret == LAGOPUS_RESULT_OK) ? 0 : 1;
}
Esempio n. 10
0
static lagopus_result_t
sockthread_initialize(void *arg) {
  lagopus_thread_create(&sock_thread, rawsock_thread_loop,
                        dp_finalproc, dp_freeproc, "dataplane rawswock", arg);
  return LAGOPUS_RESULT_OK;
}
Esempio n. 11
0
static lagopus_result_t
timerthread_initialize(void *arg) {
  lagopus_thread_create(&timer_thread, dp_timer_loop,
                        dp_finalproc, dp_freeproc, "dataplane timer", arg);
  return LAGOPUS_RESULT_OK;
}