diff options
| author | tongzongzhen <[email protected]> | 2024-08-06 14:07:47 +0800 |
|---|---|---|
| committer | tongzongzhen <[email protected]> | 2024-08-06 14:07:47 +0800 |
| commit | 47a6607c8d440e8a567d918d7739432dde6ef888 (patch) | |
| tree | 8de94f225dc77d22f88a6a21c1b2298cbfbfd9e9 | |
| parent | e279579397030323cb451cba23660ac823b3fab0 (diff) | |
fix pthread_create ret checkv4.8.19-20240806
- On success, pthread_create() returns 0; on error, it returns an error number. error number greater than zero.
- When the pthread_create() fails, errno is not set. The error number is stored in the return value.
| -rw-r--r-- | app/src/marsio.c | 8 | ||||
| -rw-r--r-- | app/src/tap.c | 6 | ||||
| -rw-r--r-- | service/src/app.c | 4 | ||||
| -rw-r--r-- | service/src/core.c | 12 | ||||
| -rw-r--r-- | service/src/devmgr.c | 4 | ||||
| -rw-r--r-- | service/src/http_serv.c | 5 | ||||
| -rw-r--r-- | service/src/olp_6500.c | 610 | ||||
| -rw-r--r-- | service/test/TestOLP.cc | 124 | ||||
| -rw-r--r-- | tools/tcpdump/tcpdump.c | 241 | ||||
| -rw-r--r-- | tunnat/src/core.cc | 149 |
10 files changed, 571 insertions, 592 deletions
diff --git a/app/src/marsio.c b/app/src/marsio.c index 244a2b2..94bb22f 100644 --- a/app/src/marsio.c +++ b/app/src/marsio.c @@ -355,9 +355,9 @@ int send_register_request(struct app_register_req * reg_req) pthread_t __pid = 0; ret = pthread_create(&__pid, NULL, marsio_survival_monitor, NULL); - if (ret < 0) + if (ret != 0) { - MR_ERROR("Launch marsio_survival_monitor thread failed : %s", strerror(errno)); + MR_ERROR("Launch marsio_survival_monitor thread failed : %s", strerror(ret)); goto error; } @@ -795,9 +795,9 @@ int marsio_init(struct mr_instance * instance, const char * appsym) mpapp_mp_cache_init(instance); pthread_t pid_ctrlplane_thread; int ret = pthread_create(&pid_ctrlplane_thread, NULL, mrapp_ctrlplane_thread, instance); - if (ret < 0) + if (ret != 0) { - MR_ERROR("Launch ctrlplane thread failed : %s", strerror(errno)); + MR_ERROR("Launch ctrlplane thread failed : %s", strerror(ret)); goto err; } diff --git a/app/src/tap.c b/app/src/tap.c index 12994ad..0e97c0f 100644 --- a/app/src/tap.c +++ b/app/src/tap.c @@ -118,7 +118,7 @@ static int tap_resp_dev_filter(struct vdev * vdev_desc, struct rte_mbuf * mbuf) LAYER_TYPE_ID_IPV4, }; - if(parser_result->nr_layers == 3 || parser_result->nr_layers == 2) + if (parser_result->nr_layers == 3 || parser_result->nr_layers == 2) { goto compare_layers; } @@ -397,9 +397,9 @@ int tap_representor_init(struct mr_instance * mr_instance, struct mr_vdev * vdev { int ret = pthread_create(&mr_instance->pid_tap_resp_poll, NULL, tap_representor_poll_thread_entry, (void *)mr_instance); - if (unlikely(ret < 0)) + if (unlikely(ret != 0)) { - MR_ERROR("failed at creating thread for tap representor poll routine: %s", strerror(errno)); + MR_ERROR("failed at creating thread for tap representor poll routine: %s", strerror(ret)); return -2; } } diff --git a/service/src/app.c b/service/src/app.c index d0db9fd..b3f86d0 100644 --- a/service/src/app.c +++ b/service/src/app.c @@ -203,9 +203,9 @@ int app_main_init(struct sc_main * sc) pthread_t __pid = 0; int ret = pthread_create(&__pid, NULL, apps_survival_monitor, NULL); - if (ret < 0) + if (ret != 0) { - MR_ERROR("Launch monitor_marsio_survival thread failed : %s", strerror(errno)); + MR_ERROR("Launch monitor_marsio_survival thread failed : %s", strerror(ret)); return RT_ERR; } diff --git a/service/src/core.c b/service/src/core.c index e3fe00a..d79a176 100644 --- a/service/src/core.c +++ b/service/src/core.c @@ -898,9 +898,9 @@ int config_reload_thread() } ret = pthread_create(&thread_id, NULL, config_reload_handler, (void *)(uintptr_t)sfd); - if (ret < 0) + if (ret != 0) { - MR_ERROR("config reload thread failed : %s", strerror(errno)); + MR_ERROR("config reload thread failed : %s", strerror(ret)); return RT_ERR; } @@ -1243,18 +1243,18 @@ int marsio_service_main(int argc, char * argv[]) pthread_t ctrlplane_thread_id; ret = pthread_create(&ctrlplane_thread_id, NULL, sc_ctrlplane_thread, sc); - if (ret < 0) + if (ret != 0) { - MR_ERROR("Launch ctrlplane thread failed : %s", strerror(errno)); + MR_ERROR("Launch ctrlplane thread failed : %s", strerror(ret)); ret = EXIT_FAILURE; goto quit; } pthread_t health_check_thread_id; ret = pthread_create(&health_check_thread_id, NULL, health_check_thread, sc); - if (ret < 0) + if (ret != 0) { - MR_ERROR("Launch health check thread failed : %s", strerror(errno)); + MR_ERROR("Launch health check thread failed : %s", strerror(ret)); ret = EXIT_FAILURE; goto quit; } diff --git a/service/src/devmgr.c b/service/src/devmgr.c index 7aa554f..d18159c 100644 --- a/service/src/devmgr.c +++ b/service/src/devmgr.c @@ -2807,9 +2807,9 @@ int devmgr_init(struct devmgr_main * devmgr_main) /* 启动物理设备状态更新线程 */ pthread_t _pid_link_update; ret = pthread_create(&_pid_link_update, NULL, dpdk_dev_link_state_update_thread, (void *)devmgr_main); - if (ret < 0) + if (ret != 0) { - MR_ERROR("PHYDEV link state update thread create failed: %s", strerror(errno)); + MR_ERROR("PHYDEV link state update thread create failed: %s", strerror(ret)); return RT_ERR; } diff --git a/service/src/http_serv.c b/service/src/http_serv.c index 57ea5c3..1e4017c 100644 --- a/service/src/http_serv.c +++ b/service/src/http_serv.c @@ -119,9 +119,10 @@ int http_serv_init(struct sc_main * sc_main) /* create a thread to do the event_base dispatch */ pthread_t tid; - if (pthread_create(&tid, NULL, http_serv_dispatch, base) != 0) + int ret = pthread_create(&tid, NULL, http_serv_dispatch, base); + if (ret != 0) { - MR_ERROR("failed to create thread for event_base dispatch"); + MR_ERROR("failed to create thread for event_base dispatch: %s", strerror(ret)); goto errout; } diff --git a/service/src/olp_6500.c b/service/src/olp_6500.c index a2c5f4f..b53e5a7 100644 --- a/service/src/olp_6500.c +++ b/service/src/olp_6500.c @@ -1,12 +1,12 @@ #include <assert.h> #include <net/if.h> +#include <sched.h> +#include <signal.h> #include <stdio.h> #include <sys/ioctl.h> #include <sys/queue.h> -#include <unistd.h> -#include <sched.h> -#include <signal.h> #include <time.h> +#include <unistd.h> #include <rte_bus_pci.h> #include <rte_config.h> @@ -24,38 +24,38 @@ #include <MESA_prof_load.h> #include <cJSON.h> #include <common.h> +#include <olp_dev.h> #include <rpc.h> #include <sc_common.h> #include <sc_devmgr.h> #include <sc_mrb.h> #include <sc_vdev.h> -#include <olp_dev.h> #define BUFF_MAX 256 #define NR_OLP6500_CHANNEL_PER_DEVICE_MAX (17) #define OLP6500_HEARTBEAT_SWITCH_OBJECT (0x0600) #define OLP6500_HEARTBEAT_PACKET_OBJECT (0x0601) -#define OLP6500_WORK_MODE_OBJECT (0x1010) -#define OLP6500_WORK_LINE_OBJECT (0x1070) -#define OLP6500_SWITCHBACK_OBJECT (0x1020) -#define OLP6500_CHANNEL_INFO_OBJECT (0x0200) -#define OLP6500_CHANNEL_CONF_OBJECT (0x0501) +#define OLP6500_WORK_MODE_OBJECT (0x1010) +#define OLP6500_WORK_LINE_OBJECT (0x1070) +#define OLP6500_SWITCHBACK_OBJECT (0x1020) +#define OLP6500_CHANNEL_INFO_OBJECT (0x0200) +#define OLP6500_CHANNEL_CONF_OBJECT (0x0501) -#define OLP6500_SET_LINE_INLINE (0x30) -#define OLP6500_SET_LINE_BYPASS (0xc0) +#define OLP6500_SET_LINE_INLINE (0x30) +#define OLP6500_SET_LINE_BYPASS (0xc0) -#define OLP6500_LINE_STATUS_INLINE (0x03) -#define OLP6500_LINE_STATUS_BYPASS (0x0c) +#define OLP6500_LINE_STATUS_INLINE (0x03) +#define OLP6500_LINE_STATUS_BYPASS (0x0c) -#define OLP6500_TIMER_ONE_CYCLE_US 1000 -#define OLP6500_RELOAD_CONFIG_CYCLE_US 1000 +#define OLP6500_TIMER_ONE_CYCLE_US 1000 +#define OLP6500_RELOAD_CONFIG_CYCLE_US 1000 -#define OLP6500_LOAD_CONFIG_NOT_READY 0 -#define OLP6500_LOAD_CONFIG_READY 1 +#define OLP6500_LOAD_CONFIG_NOT_READY 0 +#define OLP6500_LOAD_CONFIG_READY 1 -#define OLP6500_WORK_MODE_MANUL 1 -#define OLP6500_WORK_MODE_AUTO 2 +#define OLP6500_WORK_MODE_MANUL 1 +#define OLP6500_WORK_MODE_AUTO 2 enum { @@ -69,7 +69,7 @@ enum OLP_CONFIG_SET, }; -enum +enum { OLP_SET_HEARTBEAT_SWITCH = 0, OLP_SET_WORK_MODE = 1, @@ -84,7 +84,7 @@ enum OLP_OBJECT_MAX }; -enum +enum { OLP_SUCCESS = 0, OLP_ERR_CODE_CONN_FAIL = 1, @@ -116,7 +116,7 @@ struct heartbeat_switch_data uint8_t idle; uint8_t interval; uint8_t count; -}__attribute__((packed)); +} __attribute__((packed)); /* * Work Mode Data Format @@ -131,7 +131,7 @@ struct heartbeat_switch_data struct work_mode_data { uint8_t mode; -}__attribute__((packed)); +} __attribute__((packed)); /* * Work Line Data Format @@ -146,7 +146,7 @@ struct work_mode_data struct work_line_data { uint8_t line_status; -}__attribute__((packed)); +} __attribute__((packed)); /* * Switch Back Data Format @@ -161,7 +161,7 @@ struct work_line_data struct switch_back_data { uint8_t enable; -}__attribute__((packed)); +} __attribute__((packed)); struct channel_info_data { @@ -175,7 +175,7 @@ struct channel_info_data int16_t rx_optical_power; int16_t r1_optical_power; int16_t r2_optical_power; -}__attribute__((packed)); +} __attribute__((packed)); struct channel_conf_data { @@ -211,21 +211,21 @@ struct channel_conf_data uint8_t heart_interval; uint8_t heart_count; uint8_t CRC; -}__attribute__((packed)); +} __attribute__((packed)); /* * OLP6500 Format * 0 1 2 3 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | Action | Slot | Object + * | Action | Slot | Object * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | Port | Len | Data + * | Port | Len | Data * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * Action : SET/GET 0x00:get 0x01:set 8 bits * Slot : Channal ID 8 bits * Object : Object 16 bits - * Port : 0 represents the card, + * Port : 0 represents the card, * 1~8 represent the SFP module port numbers. 8 bits * Len : Data length 8 bits * Data @@ -247,9 +247,9 @@ struct olp6500_packet struct channel_conf_data channel_conf; char data[0]; }; -}__attribute__((packed)); +} __attribute__((packed)); -static char *g_dynamic_cfgfile; +static char * g_dynamic_cfgfile; int g_olp6500_config_reload; pthread_mutex_t g_olp6500_lock; uint16_t olp6500_object_map[OLP_OBJECT_MAX] = { @@ -264,44 +264,44 @@ uint16_t olp6500_object_map[OLP_OBJECT_MAX] = { static char * str_olp_device_type(enum olp_device_type device_type) { - switch(device_type) + switch (device_type) { - case OLP_DEVICE_TYPE_NIAGARA_3296: - return "NIAGARA-3296"; - case OLP_DEVICE_TYPE_OLP_6500: - return "OLP-6500"; - default: - return ""; + case OLP_DEVICE_TYPE_NIAGARA_3296: + return "NIAGARA-3296"; + case OLP_DEVICE_TYPE_OLP_6500: + return "OLP-6500"; + default: + return ""; } } static char * str_olp_connect_type(enum olp_connect_type conn_type) { - switch(conn_type) + switch (conn_type) { - case OLP_CONNECT_TYPE_COM: - return "COM"; - case OLP_CONNECT_TYPE_USB: - return "USB"; - case OLP_CONNECT_TYPE_NETWORK: - return "NETWORK"; - default: - return ""; + case OLP_CONNECT_TYPE_COM: + return "COM"; + case OLP_CONNECT_TYPE_USB: + return "USB"; + case OLP_CONNECT_TYPE_NETWORK: + return "NETWORK"; + default: + return ""; } } static char * str_olp_workmode(enum olp_channel_state state) { - switch(state) + switch (state) { - case OLP_CHANNEL_STATE_AUTO: - return "AUTO"; - case OLP_CHANNEL_STATE_FORCE_PASS: - return "FORCE-PASS"; - case OLP_CHANNEL_STATE_FORCE_BYPASS: - return "FORCE-BYPASS"; - default: - return ""; + case OLP_CHANNEL_STATE_AUTO: + return "AUTO"; + case OLP_CHANNEL_STATE_FORCE_PASS: + return "FORCE-PASS"; + case OLP_CHANNEL_STATE_FORCE_BYPASS: + return "FORCE-BYPASS"; + default: + return ""; } } @@ -388,8 +388,7 @@ static struct olp_dev_desc * olp6500_find_device_by_symbol(struct olp_device * o for (uint32_t i = 0; i < olp_dev->nr_olp_dev_descs; i++) { dev_desc = olp_dev->olp_dev_descs[i]; - if (dev_desc->type == OLP_DEVICE_TYPE_OLP_6500 && - strncmp(olp_dev_sym, dev_desc->devsym, MR_SYMBOL_MAX) == 0) + if (dev_desc->type == OLP_DEVICE_TYPE_OLP_6500 && strncmp(olp_dev_sym, dev_desc->devsym, MR_SYMBOL_MAX) == 0) { return dev_desc; } @@ -397,40 +396,40 @@ static struct olp_dev_desc * olp6500_find_device_by_symbol(struct olp_device * o return NULL; } -static char *olp6500_str_object(uint16_t object) +static char * olp6500_str_object(uint16_t object) { - switch(object) + switch (object) { - case OLP_SET_HEARTBEAT_SWITCH: - return "HEARTBEAT_SWITCH"; - case OLP_SET_WORK_MODE: - return "WORK_MODE"; - case OLP_SET_WORK_LINE: - return "WORK_LINE"; - case OLP_SET_SWITCHBACK_MODE: - return "SWITCHBACK_MODE"; - case OLP_HEARTBEAT_PACKET: - return "HEARTBEAT_PACKET"; - case OLP_GET_CHANNEL_INFO: - return "GET_CHANNEL_INFO"; - case OLP_GET_CHANNEL_CONF: - return "GET_CHANNEL_CONF"; - default: - break; + case OLP_SET_HEARTBEAT_SWITCH: + return "HEARTBEAT_SWITCH"; + case OLP_SET_WORK_MODE: + return "WORK_MODE"; + case OLP_SET_WORK_LINE: + return "WORK_LINE"; + case OLP_SET_SWITCHBACK_MODE: + return "SWITCHBACK_MODE"; + case OLP_HEARTBEAT_PACKET: + return "HEARTBEAT_PACKET"; + case OLP_GET_CHANNEL_INFO: + return "GET_CHANNEL_INFO"; + case OLP_GET_CHANNEL_CONF: + return "GET_CHANNEL_CONF"; + default: + break; } return NULL; } int olp6500_check_recv_data(char * buff, uint16_t object) { - struct olp6500_packet * pkt = (struct olp6500_packet*)buff; + struct olp6500_packet * pkt = (struct olp6500_packet *)buff; if (ntohs(pkt->object) != olp6500_object_map[object]) { MR_ERROR("deployment object [%s] failed! receive data: 0xFF 0xEE", olp6500_str_object(object)); return RT_ERR; } - + if (pkt->len == 2 && pkt->data[0] == 0xFF && pkt->data[1] == 0xEE) { MR_ERROR("deployment object [%s] failed! receive data: 0xFF 0xEE", olp6500_str_object(object)); @@ -439,7 +438,8 @@ int olp6500_check_recv_data(char * buff, uint16_t object) return RT_SUCCESS; } -static int olp6500_send_command_over_network_sync(struct olp_dev_desc * dev_desc, char * send_buff, int send_len, char * recv_buff, int recv_len) +static int olp6500_send_command_over_network_sync(struct olp_dev_desc * dev_desc, char * send_buff, int send_len, + char * recv_buff, int recv_len) { int ret = 0; int length = 0; @@ -458,18 +458,20 @@ static int olp6500_send_command_over_network_sync(struct olp_dev_desc * dev_desc setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - do { - ret = sendto(sock, send_buff, send_len, 0, (struct sockaddr*)&addr, sizeof(addr)); - } while(ret == -1 && errno == EINTR); + do + { + ret = sendto(sock, send_buff, send_len, 0, (struct sockaddr *)&addr, sizeof(addr)); + } while (ret == -1 && errno == EINTR); if (ret == -1) { goto errout; } - do { - length = recvfrom (sock, recv_buff, recv_len, 0, NULL, 0); - } while(length == -1 && errno == EINTR); + do + { + length = recvfrom(sock, recv_buff, recv_len, 0, NULL, 0); + } while (length == -1 && errno == EINTR); if (length <= 0) { @@ -482,24 +484,25 @@ errout: return RT_ERR; } -static int olp6500_send_command_sync(struct olp_dev_desc * dev_desc, uint16_t object, char * send_buff, int send_len, char * recv_buff, int recv_len) +static int olp6500_send_command_sync(struct olp_dev_desc * dev_desc, uint16_t object, char * send_buff, int send_len, + char * recv_buff, int recv_len) { int length = 0; switch (dev_desc->conn_type) { - case OLP_CONNECT_TYPE_NETWORK: - length = olp6500_send_command_over_network_sync(dev_desc, send_buff, send_len, recv_buff, recv_len); - if (length <= 0) - { - return RT_ERR; - } - break; - case OLP_CONNECT_TYPE_COM: - case OLP_CONNECT_TYPE_USB: - break; - default: - break; + case OLP_CONNECT_TYPE_NETWORK: + length = olp6500_send_command_over_network_sync(dev_desc, send_buff, send_len, recv_buff, recv_len); + if (length <= 0) + { + return RT_ERR; + } + break; + case OLP_CONNECT_TYPE_COM: + case OLP_CONNECT_TYPE_USB: + break; + default: + break; } return olp6500_check_recv_data(recv_buff, object); @@ -510,105 +513,105 @@ int olp6500_packet_construct(struct olp_channel * channel, int object, char * bu int length = 0; struct olp6500_packet * pkt = (struct olp6500_packet *)buff; - switch(object) + switch (object) { - case OLP_SET_HEARTBEAT_SWITCH: - pkt->action = OLP_CONFIG_SET; - pkt->slot = channel->olp_channel_id; - pkt->object = htons(olp6500_object_map[object]); - pkt->port = 0; - pkt->len = 4; - - if (channel->en_heartbeat == OLP_CHANNEL_HEARTBEAT_DISABLE) - { - pkt->hb_sw.enable = 0; - pkt->hb_sw.idle = 0; - pkt->hb_sw.interval = 0; - pkt->hb_sw.count = 0; - } - else - { - pkt->hb_sw.enable = channel->en_heartbeat; - pkt->hb_sw.idle = 1; - pkt->hb_sw.interval = channel->heartbeat_timeout_interval_in_ms/20; - pkt->hb_sw.count = channel->heartbeat_lost_threshold; - } - length = 10; - break; - case OLP_SET_WORK_MODE: + case OLP_SET_HEARTBEAT_SWITCH: + pkt->action = OLP_CONFIG_SET; + pkt->slot = channel->olp_channel_id; + pkt->object = htons(olp6500_object_map[object]); + pkt->port = 0; + pkt->len = 4; + + if (channel->en_heartbeat == OLP_CHANNEL_HEARTBEAT_DISABLE) + { + pkt->hb_sw.enable = 0; + pkt->hb_sw.idle = 0; + pkt->hb_sw.interval = 0; + pkt->hb_sw.count = 0; + } + else + { + pkt->hb_sw.enable = channel->en_heartbeat; + pkt->hb_sw.idle = 1; + pkt->hb_sw.interval = channel->heartbeat_timeout_interval_in_ms / 20; + pkt->hb_sw.count = channel->heartbeat_lost_threshold; + } + length = 10; + break; + case OLP_SET_WORK_MODE: + pkt->action = OLP_CONFIG_SET; + pkt->slot = channel->olp_channel_id; + pkt->object = htons(olp6500_object_map[object]); + pkt->port = 0; + pkt->len = 1; + + if (channel->state == OLP_CHANNEL_STATE_AUTO) + { + pkt->work_mode.mode = 2; + } + else + { + pkt->work_mode.mode = 1; + } + length = 7; + break; + case OLP_SET_WORK_LINE: + if (channel->state != OLP_CHANNEL_STATE_AUTO) + { pkt->action = OLP_CONFIG_SET; pkt->slot = channel->olp_channel_id; pkt->object = htons(olp6500_object_map[object]); pkt->port = 0; pkt->len = 1; - if (channel->state == OLP_CHANNEL_STATE_AUTO) + if (channel->state == OLP_CHANNEL_STATE_FORCE_PASS) { - pkt->work_mode.mode = 2; + pkt->work_line.line_status = OLP6500_SET_LINE_INLINE; } - else + else if (channel->state == OLP_CHANNEL_STATE_FORCE_BYPASS) { - pkt->work_mode.mode = 1; + pkt->work_line.line_status = OLP6500_SET_LINE_BYPASS; } - length = 7; - break; - case OLP_SET_WORK_LINE: - if (channel->state != OLP_CHANNEL_STATE_AUTO) + else { - pkt->action = OLP_CONFIG_SET; - pkt->slot = channel->olp_channel_id; - pkt->object = htons(olp6500_object_map[object]); - pkt->port = 0; - pkt->len = 1; - - if (channel->state == OLP_CHANNEL_STATE_FORCE_PASS) - { - pkt->work_line.line_status = OLP6500_SET_LINE_INLINE; - } - else if (channel->state == OLP_CHANNEL_STATE_FORCE_BYPASS) - { - pkt->work_line.line_status = OLP6500_SET_LINE_BYPASS; - } - else - { - goto out; - } - length = 7; + goto out; } - break; - case OLP_SET_SWITCHBACK_MODE: - pkt->action = OLP_CONFIG_SET; - pkt->slot = channel->olp_channel_id; - pkt->object = htons(olp6500_object_map[object]); - pkt->port = 0; - pkt->len = 1; - - pkt->sw_back.enable = channel->nonrevertive_mode==OLP_CHANNEL_NONREVERTIVE_ENABLE?0x02:0x01; length = 7; - break; - case OLP_HEARTBEAT_PACKET: - pkt->action = OLP_CONFIG_GET; - pkt->slot = channel->olp_channel_id; - pkt->object = htons(olp6500_object_map[object]); - pkt->port = 0; - pkt->len = 0; - length = 6; - case OLP_GET_CHANNEL_INFO: - pkt->action = OLP_CONFIG_GET; - pkt->slot = channel->olp_channel_id; - pkt->object = htons(olp6500_object_map[object]); - pkt->port = 0; - pkt->len = 0; - length = 6; - case OLP_GET_CHANNEL_CONF: - pkt->action = OLP_CONFIG_GET; - pkt->slot = channel->olp_channel_id; - pkt->object = htons(olp6500_object_map[object]); - pkt->port = 0; - pkt->len = 0; - length = 6; - default: - break; + } + break; + case OLP_SET_SWITCHBACK_MODE: + pkt->action = OLP_CONFIG_SET; + pkt->slot = channel->olp_channel_id; + pkt->object = htons(olp6500_object_map[object]); + pkt->port = 0; + pkt->len = 1; + + pkt->sw_back.enable = channel->nonrevertive_mode == OLP_CHANNEL_NONREVERTIVE_ENABLE ? 0x02 : 0x01; + length = 7; + break; + case OLP_HEARTBEAT_PACKET: + pkt->action = OLP_CONFIG_GET; + pkt->slot = channel->olp_channel_id; + pkt->object = htons(olp6500_object_map[object]); + pkt->port = 0; + pkt->len = 0; + length = 6; + case OLP_GET_CHANNEL_INFO: + pkt->action = OLP_CONFIG_GET; + pkt->slot = channel->olp_channel_id; + pkt->object = htons(olp6500_object_map[object]); + pkt->port = 0; + pkt->len = 0; + length = 6; + case OLP_GET_CHANNEL_CONF: + pkt->action = OLP_CONFIG_GET; + pkt->slot = channel->olp_channel_id; + pkt->object = htons(olp6500_object_map[object]); + pkt->port = 0; + pkt->len = 0; + length = 6; + default: + break; } out: return length; @@ -617,7 +620,7 @@ out: static void * olp6500_recv_heartbeat_handler(void * args) { pthread_detach(pthread_self()); - + struct olp_channel * channel = (struct olp_channel *)args; struct olp_dev_desc * dev_desc = channel->dev_desc; int len = 0; @@ -625,10 +628,10 @@ static void * olp6500_recv_heartbeat_handler(void * args) char buff[BUFF_MAX] = {0}; __atomic_fetch_add(&dev_desc->refcnt, 1, __ATOMIC_RELAXED); - while(__atomic_load_n(&channel->timer.enable, __ATOMIC_RELAXED) == OLP_TIMER_ENABLE) + while (__atomic_load_n(&channel->timer.enable, __ATOMIC_RELAXED) == OLP_TIMER_ENABLE) { usleep(OLP6500_TIMER_ONE_CYCLE_US); - len = recvfrom(sock, buff, BUFF_MAX-1, 0, NULL, 0); + len = recvfrom(sock, buff, BUFF_MAX - 1, 0, NULL, 0); if (len == -1) { continue; @@ -663,14 +666,15 @@ static void olp6500_send_heartbeat(int socket, struct olp_channel * channel) switch (dev_desc->conn_type) { - case OLP_CONNECT_TYPE_NETWORK: - do { - ret = sendto(socket, buff, pkt_len, 0, (struct sockaddr*)addr, sizeof(*addr)); - } while(ret == -1 && errno == EINTR); - - break; - default: - break; + case OLP_CONNECT_TYPE_NETWORK: + do + { + ret = sendto(socket, buff, pkt_len, 0, (struct sockaddr *)addr, sizeof(*addr)); + } while (ret == -1 && errno == EINTR); + + break; + default: + break; } return; } @@ -678,7 +682,7 @@ static void olp6500_send_heartbeat(int socket, struct olp_channel * channel) static void * olp6500_send_heartbeat_handler(void * args) { pthread_detach(pthread_self()); - + struct olp_channel * channel = (struct olp_channel *)args; struct olp_dev_desc * dev_desc = channel->dev_desc; struct timespec current_time; @@ -703,18 +707,19 @@ static void * olp6500_send_heartbeat_handler(void * args) channel->timer.fd = sock; ret = pthread_create(&tid, NULL, olp6500_recv_heartbeat_handler, (void *)channel); - if (ret < 0) + if (ret != 0) { - MR_ERROR("OLP_6500 create receive heartbeat packet thread failed : %s", strerror(errno)); + MR_ERROR("OLP_6500 create receive heartbeat packet thread failed : %s", strerror(ret)); return NULL; } __atomic_fetch_add(&dev_desc->refcnt, 1, __ATOMIC_RELAXED); - while(__atomic_load_n(&channel->timer.enable, __ATOMIC_RELAXED) == OLP_TIMER_ENABLE) + while (__atomic_load_n(&channel->timer.enable, __ATOMIC_RELAXED) == OLP_TIMER_ENABLE) { clock_gettime(CLOCK_MONOTONIC, ¤t_time); timespec_diff(&send_pkt_last_time, ¤t_time, &interval_time); - if ((interval_time.tv_sec * 1000 + interval_time.tv_nsec/1000/1000) >= channel->heartbeat_send_interval_in_ms) + if ((interval_time.tv_sec * 1000 + interval_time.tv_nsec / 1000 / 1000) >= + channel->heartbeat_send_interval_in_ms) { olp6500_send_heartbeat(sock, channel); send_pkt_last_time = current_time; @@ -738,9 +743,9 @@ static int olp6500_heartbeat_thread_create(struct olp_channel * channel) __atomic_exchange_n(&channel->timer.enable, OLP_TIMER_ENABLE, __ATOMIC_SEQ_CST); ret = pthread_create(&tid, NULL, olp6500_send_heartbeat_handler, (void *)channel); - if (ret < 0) + if (ret != 0) { - MR_ERROR("OLP_6500 create send heartbeat packet thread failed : %s", strerror(errno)); + MR_ERROR("OLP_6500 create send heartbeat packet thread failed : %s", strerror(ret)); return RT_ERR; } return RT_SUCCESS; @@ -755,7 +760,7 @@ int olp6500_apply_control_command_to_peer(struct olp_dev_desc * dev_desc, uint32 struct olp_channel * channel = NULL; channel = &dev_desc->channels[channel_id]; - + pkt_len = olp6500_packet_construct(channel, OLP_SET_HEARTBEAT_SWITCH, buff); ret = olp6500_send_command_sync(dev_desc, OLP_SET_HEARTBEAT_SWITCH, buff, pkt_len, buff, BUFF_MAX); if (ret != RT_SUCCESS) @@ -794,11 +799,9 @@ errout: if (channel->runtime.errcode != OLP_ERR_CODE_CONN_FAIL) { inet_ntop(AF_INET, &(dev_desc->network.addr.sin_addr), str_ip_addr, INET_ADDRSTRLEN); - MR_ERROR("The deployment of the OBP[%s:%u] configuration has encountered a failure. Reason: Connect %s:%u failed or configuration settings failed.", - dev_desc->devsym, - channel->olp_channel_id, - str_ip_addr, - ntohs(dev_desc->network.addr.sin_port)); + MR_ERROR("The deployment of the OBP[%s:%u] configuration has encountered a failure. Reason: Connect %s:%u " + "failed or configuration settings failed.", + dev_desc->devsym, channel->olp_channel_id, str_ip_addr, ntohs(dev_desc->network.addr.sin_port)); } channel->runtime.errcode = OLP_ERR_CODE_CONN_FAIL; return RT_ERR; @@ -813,7 +816,7 @@ static void * _olp6500_retry_apply_control_command(void * args) pthread_detach(pthread_self()); - while(1) + while (1) { sleep(10); @@ -860,9 +863,9 @@ static int olp6500_retry_apply_control_command(struct olp_manager_main * olp_mgr pthread_t tid; ret = pthread_create(&tid, NULL, _olp6500_retry_apply_control_command, olp_mgr_main); - if (ret < 0) + if (ret != 0) { - MR_ERROR("OLP_6500 create retry apply control command thread failed : %s", strerror(errno)); + MR_ERROR("OLP_6500 create retry apply control command thread failed : %s", strerror(ret)); return RT_ERR; } return RT_SUCCESS; @@ -911,9 +914,12 @@ static int olp6500_channel_config_load(struct olp_dev_desc * olp_dev_desc, char channel->dev_desc = olp_dev_desc; channel->en_heartbeat = olp_channel_heartbeat; - MESA_load_profile_uint_nodef(cfgfile, channel_section, "heartbeat_timeout_interval_in_ms", &channel->heartbeat_timeout_interval_in_ms); - MESA_load_profile_uint_nodef(cfgfile, channel_section, "heartbeat_send_interval_in_ms", &channel->heartbeat_send_interval_in_ms); - MESA_load_profile_uint_nodef(cfgfile, channel_section, "heartbeat_lost_threshold", &channel->heartbeat_lost_threshold); + MESA_load_profile_uint_nodef(cfgfile, channel_section, "heartbeat_timeout_interval_in_ms", + &channel->heartbeat_timeout_interval_in_ms); + MESA_load_profile_uint_nodef(cfgfile, channel_section, "heartbeat_send_interval_in_ms", + &channel->heartbeat_send_interval_in_ms); + MESA_load_profile_uint_nodef(cfgfile, channel_section, "heartbeat_lost_threshold", + &channel->heartbeat_lost_threshold); MESA_load_profile_uint_nodef(cfgfile, channel_section, "nonrevertive_mode", &channel->nonrevertive_mode); } return RT_SUCCESS; @@ -946,7 +952,8 @@ static int olp6500_config_load(struct olp_device * olp_dev, char * cfgfile) if (olp_dev->nr_olp_dev_descs == NR_OLP_DEVICE_MAX) { - MR_WARNING("failed to add OLP_6500 config, Configured quantity has reached its maximum limit[%d].", NR_OLP_DEVICE_MAX); + MR_WARNING("failed to add OLP_6500 config, Configured quantity has reached its maximum limit[%d].", + NR_OLP_DEVICE_MAX); continue; } @@ -1035,7 +1042,8 @@ static int olp6500_copy_device_state(struct olp_device * src, struct olp_device continue; } - dst_dev_desc->channels[channel->olp_channel_id].runtime.last_active_time = channel->runtime.last_active_time; + dst_dev_desc->channels[channel->olp_channel_id].runtime.last_active_time = + channel->runtime.last_active_time; dst_dev_desc->channels[channel->olp_channel_id].used = OLP_STATE_USED; dst_dev_desc->used = OLP_STATE_USED; @@ -1046,7 +1054,7 @@ static int olp6500_copy_device_state(struct olp_device * src, struct olp_device return RT_SUCCESS; } -static int olp6500_get_optical_power( char * buff, struct olp_channel * channel) +static int olp6500_get_optical_power(char * buff, struct olp_channel * channel) { struct olp6500_packet * pkt = (struct olp6500_packet *)buff; if (pkt->object != htons(OLP6500_CHANNEL_INFO_OBJECT) || pkt->len != 17) @@ -1058,16 +1066,17 @@ static int olp6500_get_optical_power( char * buff, struct olp_channel * channel) int16_t r2_optical_power = ntohs(pkt->channel_info.r2_optical_power); int16_t r3_optical_power = ntohs(pkt->channel_info.r3_optical_power); int16_t r4_optical_power = ntohs(pkt->channel_info.r4_optical_power); - channel->runtime.r1_optical_power = ((float)r1_optical_power)/100; - channel->runtime.r2_optical_power = ((float)r2_optical_power)/100; - channel->runtime.r3_optical_power = ((float)r3_optical_power)/100; - channel->runtime.r4_optical_power = ((float)r4_optical_power)/100; + channel->runtime.r1_optical_power = ((float)r1_optical_power) / 100; + channel->runtime.r2_optical_power = ((float)r2_optical_power) / 100; + channel->runtime.r3_optical_power = ((float)r3_optical_power) / 100; + channel->runtime.r4_optical_power = ((float)r4_optical_power) / 100; channel->runtime.workline = pkt->channel_info.work_line; return RT_SUCCESS; } -static int olp6500_check_channel_config(struct olp_channel * channel, char * buff, char *str_err_reason, int sz_err_reason) +static int olp6500_check_channel_config(struct olp_channel * channel, char * buff, char * str_err_reason, + int sz_err_reason) { struct olp6500_packet * pkt = (struct olp6500_packet *)buff; struct olp_dev_desc * dev_desc = channel->dev_desc; @@ -1080,18 +1089,17 @@ static int olp6500_check_channel_config(struct olp_channel * channel, char * buf if (pkt->channel_conf.heart_detect != channel->en_heartbeat) { - snprintf(str_err_reason, sz_err_reason, "The current device[%s:%u] configuration[heartbeat_mode:%s] is inconsistent with the administrator's settings[heartbeat_mode:%s].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.heart_detect?"enable":"disable", - channel->en_heartbeat?"enable":"disable"); + snprintf(str_err_reason, sz_err_reason, + "The current device[%s:%u] configuration[heartbeat_mode:%s] is inconsistent with the administrator's " + "settings[heartbeat_mode:%s].", + dev_desc->devsym, channel->olp_channel_id, pkt->channel_conf.heart_detect ? "enable" : "disable", + channel->en_heartbeat ? "enable" : "disable"); if (channel->runtime.errcode != OLP_ERR_CODE_HB_MODE_INCONSISTENT) { - MR_ERROR("The current device[%s:%u] configuration[heartbeat_mode:%s] is inconsistent with the administrator's settings[heartbeat_mode:%s].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.heart_detect?"enable":"disable", - channel->en_heartbeat?"enable":"disable"); + MR_ERROR("The current device[%s:%u] configuration[heartbeat_mode:%s] is inconsistent with the " + "administrator's settings[heartbeat_mode:%s].", + dev_desc->devsym, channel->olp_channel_id, pkt->channel_conf.heart_detect ? "enable" : "disable", + channel->en_heartbeat ? "enable" : "disable"); } channel->runtime.errcode = OLP_ERR_CODE_HB_MODE_INCONSISTENT; return RT_ERR; @@ -1099,20 +1107,19 @@ static int olp6500_check_channel_config(struct olp_channel * channel, char * buf if (channel->en_heartbeat) { - if (pkt->channel_conf.heart_interval != (channel->heartbeat_timeout_interval_in_ms/20)) + if (pkt->channel_conf.heart_interval != (channel->heartbeat_timeout_interval_in_ms / 20)) { - snprintf(str_err_reason, sz_err_reason, "The current device[%s:%u] configuration[heartbeat_timeout_interval_in_ms:%u] is inconsistent with the administrator's settings[heartbeat_timeout_interval_in_ms:%d].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.heart_interval*20, - channel->heartbeat_timeout_interval_in_ms); + snprintf(str_err_reason, sz_err_reason, + "The current device[%s:%u] configuration[heartbeat_timeout_interval_in_ms:%u] is inconsistent " + "with the administrator's settings[heartbeat_timeout_interval_in_ms:%d].", + dev_desc->devsym, channel->olp_channel_id, pkt->channel_conf.heart_interval * 20, + channel->heartbeat_timeout_interval_in_ms); if (channel->runtime.errcode != OLP_ERR_CODE_HB_INTERVAL_INCONSISTENT) { - MR_ERROR("The current device[%s:%u] configuration[heartbeat_timeout_interval_in_ms:%u] is inconsistent with the administrator's settings[heartbeat_timeout_interval_in_ms:%d].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.heart_interval*20, - channel->heartbeat_timeout_interval_in_ms); + MR_ERROR("The current device[%s:%u] configuration[heartbeat_timeout_interval_in_ms:%u] is inconsistent " + "with the administrator's settings[heartbeat_timeout_interval_in_ms:%d].", + dev_desc->devsym, channel->olp_channel_id, pkt->channel_conf.heart_interval * 20, + channel->heartbeat_timeout_interval_in_ms); } channel->runtime.errcode = OLP_ERR_CODE_HB_INTERVAL_INCONSISTENT; return RT_ERR; @@ -1120,18 +1127,17 @@ static int olp6500_check_channel_config(struct olp_channel * channel, char * buf if (pkt->channel_conf.heart_count != channel->heartbeat_lost_threshold) { - snprintf(str_err_reason, sz_err_reason, "The current device[%s:%u] configuration[heartbeat_lost_threshold:%u] is inconsistent with the administrator's settings[heartbeat_lost_threshold:%u].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.heart_count, - channel->heartbeat_lost_threshold); + snprintf(str_err_reason, sz_err_reason, + "The current device[%s:%u] configuration[heartbeat_lost_threshold:%u] is inconsistent with the " + "administrator's settings[heartbeat_lost_threshold:%u].", + dev_desc->devsym, channel->olp_channel_id, pkt->channel_conf.heart_count, + channel->heartbeat_lost_threshold); if (channel->runtime.errcode != OLP_ERR_CODE_HB_COUNT_INCONSISTENT) { - MR_ERROR("The current device[%s:%u] configuration[heartbeat_lost_threshold:%u] is inconsistent with the administrator's settings[heartbeat_lost_threshold:%u].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.heart_count, - channel->heartbeat_lost_threshold); + MR_ERROR("The current device[%s:%u] configuration[heartbeat_lost_threshold:%u] is inconsistent with " + "the administrator's settings[heartbeat_lost_threshold:%u].", + dev_desc->devsym, channel->olp_channel_id, pkt->channel_conf.heart_count, + channel->heartbeat_lost_threshold); } channel->runtime.errcode = OLP_ERR_CODE_HB_COUNT_INCONSISTENT; return RT_ERR; @@ -1150,18 +1156,19 @@ static int olp6500_check_channel_config(struct olp_channel * channel, char * buf if (pkt->channel_conf.work_mode != channel_work_mode) { - snprintf(str_err_reason, sz_err_reason, "The current device[%s:%u] configuration[workmode:%s] is inconsistent with the administrator's settings[workmode:%s].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.work_mode==OLP6500_WORK_MODE_AUTO?"auto":"manual", - channel_work_mode==OLP6500_WORK_MODE_AUTO?"auto":"manual"); + snprintf(str_err_reason, sz_err_reason, + "The current device[%s:%u] configuration[workmode:%s] is inconsistent with the administrator's " + "settings[workmode:%s].", + dev_desc->devsym, channel->olp_channel_id, + pkt->channel_conf.work_mode == OLP6500_WORK_MODE_AUTO ? "auto" : "manual", + channel_work_mode == OLP6500_WORK_MODE_AUTO ? "auto" : "manual"); if (channel->runtime.errcode != OLP_ERR_CODE_WORK_MODE_INCONSISTENT) { - MR_ERROR("The current device[%s:%u] configuration[workmode:%s] is inconsistent with the administrator's settings[workmode:%s].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.work_mode==OLP6500_WORK_MODE_AUTO?"auto":"manual", - channel_work_mode==OLP6500_WORK_MODE_AUTO?"auto":"manual"); + MR_ERROR("The current device[%s:%u] configuration[workmode:%s] is inconsistent with the administrator's " + "settings[workmode:%s].", + dev_desc->devsym, channel->olp_channel_id, + pkt->channel_conf.work_mode == OLP6500_WORK_MODE_AUTO ? "auto" : "manual", + channel_work_mode == OLP6500_WORK_MODE_AUTO ? "auto" : "manual"); } channel->runtime.errcode = OLP_ERR_CODE_WORK_MODE_INCONSISTENT; return RT_ERR; @@ -1181,18 +1188,19 @@ static int olp6500_check_channel_config(struct olp_channel * channel, char * buf if (pkt->channel_conf.work_line != channel_work_line) { - snprintf(str_err_reason, sz_err_reason, "The current device[%s:%u] configuration[workline:%s] is inconsistent with the administrator's settings[workline:%s].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.work_line==OLP6500_LINE_STATUS_INLINE?"inline":"bypass", - channel_work_line==OLP6500_LINE_STATUS_INLINE?"inline":"bypass"); + snprintf(str_err_reason, sz_err_reason, + "The current device[%s:%u] configuration[workline:%s] is inconsistent with the administrator's " + "settings[workline:%s].", + dev_desc->devsym, channel->olp_channel_id, + pkt->channel_conf.work_line == OLP6500_LINE_STATUS_INLINE ? "inline" : "bypass", + channel_work_line == OLP6500_LINE_STATUS_INLINE ? "inline" : "bypass"); if (channel->runtime.errcode != OLP_ERR_CODE_WORK_LINE_INCONSISTENT) { - MR_ERROR("The current device[%s:%u] configuration[workline:%s] is inconsistent with the administrator's settings[workline:%s].", - dev_desc->devsym, - channel->olp_channel_id, - pkt->channel_conf.work_line==OLP6500_LINE_STATUS_INLINE?"inline":"bypass", - channel_work_line==OLP6500_LINE_STATUS_INLINE?"inline":"bypass"); + MR_ERROR("The current device[%s:%u] configuration[workline:%s] is inconsistent with the " + "administrator's settings[workline:%s].", + dev_desc->devsym, channel->olp_channel_id, + pkt->channel_conf.work_line == OLP6500_LINE_STATUS_INLINE ? "inline" : "bypass", + channel_work_line == OLP6500_LINE_STATUS_INLINE ? "inline" : "bypass"); } channel->runtime.errcode = OLP_ERR_CODE_WORK_LINE_INCONSISTENT; return RT_ERR; @@ -1201,18 +1209,17 @@ static int olp6500_check_channel_config(struct olp_channel * channel, char * buf if ((!pkt->channel_conf.switch_back_mode) != channel->nonrevertive_mode) { - snprintf(str_err_reason, sz_err_reason, "The current device[%s:%u] configuration[nonrevertive_mode:%s] is inconsistent with the administrator's settings[nonrevertive_mode:%s].", - dev_desc->devsym, - channel->olp_channel_id, - (!pkt->channel_conf.switch_back_mode)?"yes":"no", - channel->nonrevertive_mode?"yes":"no"); + snprintf(str_err_reason, sz_err_reason, + "The current device[%s:%u] configuration[nonrevertive_mode:%s] is inconsistent with the " + "administrator's settings[nonrevertive_mode:%s].", + dev_desc->devsym, channel->olp_channel_id, (!pkt->channel_conf.switch_back_mode) ? "yes" : "no", + channel->nonrevertive_mode ? "yes" : "no"); if (channel->runtime.errcode != OLP_ERR_CODE_SW_BACK_INCONSISTENT) { - MR_ERROR("The current device[%s:%u] configuration[nonrevertive_mode:%s] is inconsistent with the administrator's settings[nonrevertive_mode:%s].", - dev_desc->devsym, - channel->olp_channel_id, - (!pkt->channel_conf.switch_back_mode)?"yes":"no", - channel->nonrevertive_mode?"yes":"no"); + MR_ERROR("The current device[%s:%u] configuration[nonrevertive_mode:%s] is inconsistent with the " + "administrator's settings[nonrevertive_mode:%s].", + dev_desc->devsym, channel->olp_channel_id, (!pkt->channel_conf.switch_back_mode) ? "yes" : "no", + channel->nonrevertive_mode ? "yes" : "no"); } channel->runtime.errcode = OLP_ERR_CODE_SW_BACK_INCONSISTENT; return RT_ERR; @@ -1268,7 +1275,10 @@ cJSON * olp6500_monit_loop(struct sc_main * sc_main) { cJSON_AddNumberToObject(j_channel, "channel_id", channel->olp_channel_id); inet_ntop(AF_INET, &(dev_desc->network.addr.sin_addr), str_ip_addr, INET_ADDRSTRLEN); - snprintf(str_err_reason, sizeof(str_err_reason), "The deployment of the OBP configuration has encountered a failure. Reason: Connect %s:%u failed or configuration settings failed. ", str_ip_addr, ntohs(dev_desc->network.addr.sin_port)); + snprintf(str_err_reason, sizeof(str_err_reason), + "The deployment of the OBP configuration has encountered a failure. Reason: Connect %s:%u " + "failed or configuration settings failed. ", + str_ip_addr, ntohs(dev_desc->network.addr.sin_port)); cJSON_AddStringToObject(j_channel, "error_reason", str_err_reason); cJSON_AddItemToArray(j_channel_array, j_channel); continue; @@ -1287,12 +1297,14 @@ cJSON * olp6500_monit_loop(struct sc_main * sc_main) snprintf(str_time, sizeof(str_time), "%ld", channel->runtime.last_active_time.tv_sec); cJSON_AddStringToObject(j_channel, "last_active_time", str_time); inet_ntop(AF_INET, &(dev_desc->network.addr.sin_addr), str_ip_addr, INET_ADDRSTRLEN); - snprintf(str_err_reason, sizeof(str_err_reason), "Get channel info error: connect %s:%u failed.", str_ip_addr, ntohs(dev_desc->network.addr.sin_port)); + snprintf(str_err_reason, sizeof(str_err_reason), "Get channel info error: connect %s:%u failed.", + str_ip_addr, ntohs(dev_desc->network.addr.sin_port)); cJSON_AddStringToObject(j_channel, "error_reason", str_err_reason); cJSON_AddItemToArray(j_channel_array, j_channel); if (channel->runtime.errcode != OLP_ERR_CODE_GET_CHANNEL_INFO_FAIL) { - MR_ERROR("Get channel info error: connect %s:%u failed.", str_ip_addr, ntohs(dev_desc->network.addr.sin_port)); + MR_ERROR("Get channel info error: connect %s:%u failed.", str_ip_addr, + ntohs(dev_desc->network.addr.sin_port)); } channel->runtime.errcode = OLP_ERR_CODE_GET_CHANNEL_INFO_FAIL; continue; @@ -1303,14 +1315,14 @@ cJSON * olp6500_monit_loop(struct sc_main * sc_main) cJSON_AddNumberToObject(j_channel, "channel_id", channel->olp_channel_id); switch (channel->runtime.workline) { - case OLP6500_LINE_STATUS_INLINE: - cJSON_AddStringToObject(j_channel, "workline", "inline"); - break; - case OLP6500_LINE_STATUS_BYPASS: - cJSON_AddStringToObject(j_channel, "workline", "bypass"); - break; - default: - cJSON_AddStringToObject(j_channel, "workline", "error"); + case OLP6500_LINE_STATUS_INLINE: + cJSON_AddStringToObject(j_channel, "workline", "inline"); + break; + case OLP6500_LINE_STATUS_BYPASS: + cJSON_AddStringToObject(j_channel, "workline", "bypass"); + break; + default: + cJSON_AddStringToObject(j_channel, "workline", "error"); } snprintf(str_optical_power, sizeof(str_optical_power), "%.2f", channel->runtime.r1_optical_power); @@ -1346,7 +1358,7 @@ cJSON * olp6500_monit_loop(struct sc_main * sc_main) dev_desc = olp_dev->olp_dev_descs[index]; __atomic_fetch_sub(&dev_desc->refcnt, 1, __ATOMIC_RELAXED); } - + pthread_mutex_unlock(&g_olp6500_lock); return j_obp_device_array; } @@ -1385,7 +1397,7 @@ static int olp6500_device_deinit(struct olp_device * olp_dev) for (uint32_t i = 0; i < olp_dev->nr_olp_dev_descs; i++) { dev_desc = olp_dev->olp_dev_descs[i]; - while(__atomic_load_n(&dev_desc->refcnt, __ATOMIC_RELAXED) > 0) + while (__atomic_load_n(&dev_desc->refcnt, __ATOMIC_RELAXED) > 0) { usleep(OLP6500_TIMER_ONE_CYCLE_US); } @@ -1416,7 +1428,7 @@ static void * olp6500_config_reload(void * args) pthread_detach(pthread_self()); - while(1) + while (1) { usleep(OLP6500_RELOAD_CONFIG_CYCLE_US); @@ -1459,9 +1471,9 @@ static int olp6500_create_thread_config_reload(struct olp_manager_main * olp_mgr pthread_t tid; ret = pthread_create(&tid, NULL, olp6500_config_reload, olp_mgr_main); - if (ret < 0) + if (ret != 0) { - MR_ERROR("OLP_6500 create config reload thread failed : %s", strerror(errno)); + MR_ERROR("OLP_6500 create config reload thread failed : %s", strerror(ret)); return RT_ERR; } return RT_SUCCESS; diff --git a/service/test/TestOLP.cc b/service/test/TestOLP.cc index 77b5bc9..6294737 100644 --- a/service/test/TestOLP.cc +++ b/service/test/TestOLP.cc @@ -4,7 +4,7 @@ #include <common.h> #include <olp_dev.h> -enum +enum { OLP_SET_HEARTBEAT_SWITCH = 0, OLP_SET_WORK_MODE = 1, @@ -27,7 +27,7 @@ struct olp6500_packet uint8_t port; uint8_t len; char data[0]; -}__attribute__((packed)); +} __attribute__((packed)); unsigned int g_logger_to_stdout = 1; unsigned int g_logger_level = LOG_DEBUG; @@ -39,17 +39,17 @@ uint8_t g_ctrlzone_id = 0; extern "C" { -extern int olp6500_packet_construct(struct olp_channel * channel, int type, char *buff); +extern int olp6500_packet_construct(struct olp_channel * channel, int type, char * buff); extern int olp6500_check_recv_data(char * buff, uint16_t object); extern int olp6500_apply_control_command_to_peer(struct olp_dev_desc * dev_desc, uint32_t channel_id); extern int olp6500_destroy_timer(struct olp_device * olp_dev); } -void olp_dev_desc_init(struct olp_dev_desc *dev_desc, struct olp_channel *channel) +void olp_dev_desc_init(struct olp_dev_desc * dev_desc, struct olp_channel * channel) { snprintf(dev_desc->devsym, sizeof(dev_desc->devsym), "olp0"); dev_desc->type = OLP_DEVICE_TYPE_OLP_6500; - + channel->olp_channel_id = 1; channel->state = OLP_CHANNEL_STATE_FORCE_PASS; channel->en_heartbeat = 1; @@ -91,7 +91,7 @@ TEST(OLP_PACKET, OLP_SET_HEARTBEAT_SWITCH) TEST(OLP_PACKET, OLP_SET_WORK_MODE) { int pkt_len = 0; - char buff[256] = {0}; + char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; @@ -114,7 +114,7 @@ TEST(OLP_PACKET, OLP_SET_WORK_MODE) TEST(OLP_PACKET, OLP_SET_WORK_LINE) { int pkt_len = 0; - char buff[256] = {0}; + char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; @@ -137,7 +137,7 @@ TEST(OLP_PACKET, OLP_SET_WORK_LINE) TEST(OLP_PACKET, OLP_SET_SWITCHBACK_MODE) { int pkt_len = 0; - char buff[256] = {0}; + char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; @@ -160,7 +160,7 @@ TEST(OLP_PACKET, OLP_SET_SWITCHBACK_MODE) TEST(OLP_PACKET, OLP_HEARTBEAT_PACKET) { int pkt_len = 0; - char buff[256] = {0}; + char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; @@ -182,7 +182,7 @@ TEST(OLP_PACKET, OLP_HEARTBEAT_PACKET) TEST(OLP_PACKET, OLP_GET_CHANNEL_INFO) { int pkt_len = 0; - char buff[256] = {0}; + char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; @@ -204,7 +204,7 @@ TEST(OLP_PACKET, OLP_GET_CHANNEL_INFO) TEST(OLP_PACKET, OLP_GET_CHANNEL_CONF) { int pkt_len = 0; - char buff[256] = {0}; + char buff[256] = {0}; struct olp_dev_desc dev_desc; struct olp_channel dev_channel; @@ -226,12 +226,8 @@ TEST(OLP_PACKET, OLP_GET_CHANNEL_CONF) TEST(OLP_RECV_PACKET, OLP_SET_HEARTBEAT_SWITCH) { int ret = 0; - unsigned char data_succ[] = { - 0x01, 0x00, 0x06, 0x00, 0x00, 0x04, 0x01, 0x01, 0x05, 0x03 - }; - unsigned char data_fail[] = { - 0x01, 0x00, 0x86, 0x00, 0x00, 0x02, 0xFF, 0xEE - }; + unsigned char data_succ[] = {0x01, 0x00, 0x06, 0x00, 0x00, 0x04, 0x01, 0x01, 0x05, 0x03}; + unsigned char data_fail[] = {0x01, 0x00, 0x86, 0x00, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_HEARTBEAT_SWITCH); EXPECT_TRUE(ret == 0); @@ -242,12 +238,8 @@ TEST(OLP_RECV_PACKET, OLP_SET_HEARTBEAT_SWITCH) TEST(OLP_RECV_PACKET, OLP_SET_WORK_MODE) { int ret = 0; - unsigned char data_succ[] = { - 0x01, 0x00, 0x10, 0x10, 0x00, 0x01, 0x02 - }; - unsigned char data_fail[] = { - 0x01, 0x00, 0x90, 0x10, 0x00, 0x02, 0xFF, 0xEE - }; + unsigned char data_succ[] = {0x01, 0x00, 0x10, 0x10, 0x00, 0x01, 0x02}; + unsigned char data_fail[] = {0x01, 0x00, 0x90, 0x10, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_WORK_MODE); EXPECT_TRUE(ret == 0); @@ -258,12 +250,8 @@ TEST(OLP_RECV_PACKET, OLP_SET_WORK_MODE) TEST(OLP_RECV_PACKET, OLP_SET_WORK_LINE) { int ret = 0; - unsigned char data_succ[] = { - 0x01, 0x00, 0x10, 0x70, 0x00, 0x01, 0x30 - }; - unsigned char data_fail[] = { - 0x01, 0x00, 0x90, 0x70, 0x00, 0x02, 0xFF, 0xEE - }; + unsigned char data_succ[] = {0x01, 0x00, 0x10, 0x70, 0x00, 0x01, 0x30}; + unsigned char data_fail[] = {0x01, 0x00, 0x90, 0x70, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_WORK_LINE); EXPECT_TRUE(ret == 0); @@ -274,12 +262,8 @@ TEST(OLP_RECV_PACKET, OLP_SET_WORK_LINE) TEST(OLP_RECV_PACKET, OLP_SET_SWITCHBACK_MODE) { int ret = 0; - unsigned char data_succ[] = { - 0x01, 0x00, 0x10, 0x20, 0x00, 0x01, 0x02 - }; - unsigned char data_fail[] = { - 0x01, 0x00, 0x90, 0x20, 0x00, 0x02, 0xFF, 0xEE - }; + unsigned char data_succ[] = {0x01, 0x00, 0x10, 0x20, 0x00, 0x01, 0x02}; + unsigned char data_fail[] = {0x01, 0x00, 0x90, 0x20, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_SET_SWITCHBACK_MODE); EXPECT_TRUE(ret == 0); @@ -290,12 +274,8 @@ TEST(OLP_RECV_PACKET, OLP_SET_SWITCHBACK_MODE) TEST(OLP_RECV_PACKET, OLP_HEARTBEAT_PACKET) { int ret = 0; - unsigned char data_succ[] = { - 0x00, 0x00, 0x06, 0x01, 0x00, 0x00 - }; - unsigned char data_fail[] = { - 0x00, 0x00, 0x86, 0x01, 0x00, 0x02, 0xFF, 0xEE - }; + unsigned char data_succ[] = {0x00, 0x00, 0x06, 0x01, 0x00, 0x00}; + unsigned char data_fail[] = {0x00, 0x00, 0x86, 0x01, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_HEARTBEAT_PACKET); EXPECT_TRUE(ret == 0); @@ -306,12 +286,8 @@ TEST(OLP_RECV_PACKET, OLP_HEARTBEAT_PACKET) TEST(OLP_RECV_PACKET, OLP_GET_CHANNEL_INFO) { int ret = 0; - unsigned char data_succ[] = { - 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 - }; - unsigned char data_fail[] = { - 0x00, 0x00, 0x82, 0x00, 0x00, 0x02, 0xFF, 0xEE - }; + unsigned char data_succ[] = {0x00, 0x00, 0x02, 0x00, 0x00, 0x00}; + unsigned char data_fail[] = {0x00, 0x00, 0x82, 0x00, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_GET_CHANNEL_INFO); EXPECT_TRUE(ret == 0); @@ -322,12 +298,8 @@ TEST(OLP_RECV_PACKET, OLP_GET_CHANNEL_INFO) TEST(OLP_RECV_PACKET, OLP_GET_CHANNEL_CONF) { int ret = 0; - unsigned char data_succ[] = { - 0x00, 0x00, 0x05, 0x01, 0x00, 0x00 - }; - unsigned char data_fail[] = { - 0x00, 0x00, 0x85, 0x01, 0x00, 0x02, 0xFF, 0xEE - }; + unsigned char data_succ[] = {0x00, 0x00, 0x05, 0x01, 0x00, 0x00}; + unsigned char data_fail[] = {0x00, 0x00, 0x85, 0x01, 0x00, 0x02, 0xFF, 0xEE}; ret = olp6500_check_recv_data((char *)data_succ, OLP_GET_CHANNEL_CONF); EXPECT_TRUE(ret == 0); @@ -341,7 +313,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_1) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -384,7 +356,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_2) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -427,7 +399,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_3) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -470,7 +442,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_4) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -513,7 +485,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_5) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -556,7 +528,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_6) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -599,7 +571,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_7) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -642,7 +614,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_8) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -685,7 +657,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_9) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -728,7 +700,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_10) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -771,7 +743,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_11) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -814,7 +786,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_12) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -857,7 +829,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_13) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -900,7 +872,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_14) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -943,7 +915,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_15) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -986,7 +958,7 @@ TEST(OLP_CONTROL_COMMAND, CHANNEL_16) struct olp_device olp_dev; struct olp_dev_desc olp_dev_desc; struct olp_channel olp_channel[NR_OLP6500_CHANNEL_PER_DEVICE_MAX]; - struct olp_channel *channel = NULL; + struct olp_channel * channel = NULL; memset(&olp_dev, 0, sizeof(olp_dev)); memset(&olp_dev_desc, 0, sizeof(olp_dev_desc)); @@ -1032,7 +1004,7 @@ void * olp_device_server(void * args) struct sockaddr_in addr; struct sockaddr cli_addr; socklen_t cli_len = sizeof(cli_addr); - struct olp6500_packet *pkt = NULL; + struct olp6500_packet * pkt = NULL; pthread_detach(pthread_self()); @@ -1044,8 +1016,8 @@ void * olp_device_server(void * args) addr.sin_family = AF_INET; addr.sin_port = htons(6800); addr.sin_addr.s_addr = htonl(INADDR_ANY); - bind(sock, (struct sockaddr*)&addr, sizeof(addr)); - while(1) + bind(sock, (struct sockaddr *)&addr, sizeof(addr)); + while (1) { memset(recv_buff, 0, sizeof(recv_buff)); memset(send_buff, 0, sizeof(send_buff)); @@ -1057,8 +1029,8 @@ void * olp_device_server(void * args) continue; } - pkt = (struct olp6500_packet*)recv_buff; - ret = sendto(sock, recv_buff, pkt->len+6, 0, &cli_addr, cli_len); + pkt = (struct olp6500_packet *)recv_buff; + ret = sendto(sock, recv_buff, pkt->len + 6, 0, &cli_addr, cli_len); if (ret == -1) { printf("sendto fail! error:%s\n", strerror(errno)); @@ -1075,9 +1047,9 @@ int main(int argc, char * argv[]) ::testing::InitGoogleTest(&argc, argv); ret = pthread_create(&tid, NULL, olp_device_server, NULL); - if (ret < 0) + if (ret != 0) { - MR_ERROR("olp devices server start error: %s", strerror(errno)); + MR_ERROR("olp devices server start error: %s", strerror(ret)); return -1; } return RUN_ALL_TESTS(); diff --git a/tools/tcpdump/tcpdump.c b/tools/tcpdump/tcpdump.c index 17b58aa..cfc0e02 100644 --- a/tools/tcpdump/tcpdump.c +++ b/tools/tcpdump/tcpdump.c @@ -1,46 +1,44 @@ /* -* author:liujun -* date:2016-10-09 -* func:simple tcpdump function for filtering the invalidate packets -* diff:multi-pthread and the packets provided by libpag -*/ + * author:liujun + * date:2016-10-09 + * func:simple tcpdump function for filtering the invalidate packets + * diff:multi-pthread and the packets provided by libpag + */ -#include <stdio.h> -#include <unistd.h> -#include <string.h> -#include <stdarg.h> -#include <time.h> -#include <stdlib.h> -#include <inttypes.h> +#include <arpa/inet.h> +#include <assert.h> #include <errno.h> #include <fcntl.h> -#include <arpa/inet.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <signal.h> +#include <inttypes.h> #include <pcap.h> #include <pthread.h> +#include <signal.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <sys/types.h> #include <time.h> -#include <assert.h> -#include <inttypes.h> +#include <unistd.h> +#include <common.h> #include <libgen.h> #include <marsio.h> -#include <common.h> struct mr_instance * g_mr_instance = NULL; struct mr_vdev * g_mr_vdev_handler = NULL; #ifndef DUMP_DEFAULT_STR_DUMPFILE -#define DUMP_DEFAULT_STR_DUMPFILE "default.pcap" +#define DUMP_DEFAULT_STR_DUMPFILE "default.pcap" #endif #ifndef DUMP_DEFAULT_NR_THREAD -#define DUMP_DEFAULT_NR_THREAD 1 +#define DUMP_DEFAULT_NR_THREAD 1 #endif #ifndef DUMP_BURST_MAX -#define DUMP_BURST_MAX 32 +#define DUMP_BURST_MAX 32 #endif /* 每线程句柄 */ @@ -62,8 +60,8 @@ struct dump_thread_instance uint64_t nr_expect_packets; /* 本线程报文数统计 */ uint64_t nr_dump_packets; - /* 本线程到达报文数统计 */ - uint64_t nr_recv_packets; + /* 本线程到达报文数统计 */ + uint64_t nr_recv_packets; }; /* 全局句柄 */ @@ -81,7 +79,7 @@ struct dump_instance cpu_mask_t cpu_mask; /* 捕包线程掩码,只有在掩码中的线程才真正执行捕包逻辑 */ cpu_mask_t capture_cpu_mask; - + /* 线程总数 */ unsigned int nr_thread; /* DUMPFILE文件基准名称,线程加后缀 */ @@ -106,19 +104,19 @@ struct dump_instance struct dump_instance * g_dump_instance = NULL; -void * dump_capture_thread(void *arg) +void * dump_capture_thread(void * arg) { struct dump_thread_instance * th_instance = (struct dump_thread_instance *)arg; struct dump_instance * instance = g_dump_instance; unsigned int sid = th_instance->thread_id; - + assert(th_instance != NULL); assert(instance != NULL); - - while (g_dump_instance->keep_running == 1) - { - if (th_instance->nr_expect_packets != 0 && - th_instance->nr_dump_packets >= th_instance->nr_expect_packets) break; + + while (g_dump_instance->keep_running == 1) + { + if (th_instance->nr_expect_packets != 0 && th_instance->nr_dump_packets >= th_instance->nr_expect_packets) + break; marsio_buff_t * mbufs[DUMP_BURST_MAX]; unsigned int nr_mbufs; @@ -127,22 +125,24 @@ void * dump_capture_thread(void *arg) unsigned int nr_mbufs_dump = 0; nr_mbufs = marsio_recv_all_burst(instance->mr_instance, sid, mbufs, DUMP_BURST_MAX); - if (nr_mbufs == 0) continue; + if (nr_mbufs == 0) + continue; /* BPF过滤 */ for (int i = 0; i < nr_mbufs; i++) { const char * pkt_ptr = marsio_buff_mtod(mbufs[i]); unsigned int pkt_len = marsio_buff_datalen(mbufs[i]); - - if (bpf_filter(th_instance->bpf_prog.bf_insns, - (const u_char *)pkt_ptr, pkt_len, pkt_len) == 0 || pkt_len > 65535) continue; + + if (bpf_filter(th_instance->bpf_prog.bf_insns, (const u_char *)pkt_ptr, pkt_len, pkt_len) == 0 || + pkt_len > 65535) + continue; mbufs_dump[nr_mbufs_dump++] = mbufs[i]; } struct pcap_pkthdr pcap_pkthdr[MR_BURST_MAX]; - + /* 写入DUMPFILE文件 */ for (int i = 0; i < nr_mbufs_dump; i++) { @@ -156,20 +156,19 @@ void * dump_capture_thread(void *arg) /* 写DUMPFILE */ pcap_dump((u_char *)th_instance->pcap_dumper, &pcap_pkthdr[i], (const u_char *)pkt_ptr); } - - th_instance->nr_recv_packets += nr_mbufs; - th_instance->nr_dump_packets += nr_mbufs_dump; - - marsio_buff_free(instance->mr_instance, mbufs, nr_mbufs, - MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY); - } - - return NULL; + + th_instance->nr_recv_packets += nr_mbufs; + th_instance->nr_dump_packets += nr_mbufs_dump; + + marsio_buff_free(instance->mr_instance, mbufs, nr_mbufs, MARSIO_SOCKET_ID_ANY, MARSIO_LCORE_ID_ANY); + } + + return NULL; } void usage() { - exit(0); + exit(0); } void sig_interupt_handle(int signo) @@ -179,52 +178,51 @@ void sig_interupt_handle(int signo) void dump_capture_result(struct dump_instance * instance) { - fprintf(stderr, "\nCapture statistics:\n"); + fprintf(stderr, "\nCapture statistics:\n"); - uint64_t total_recv_packets = 0; - uint64_t total_dump_packets = 0; + uint64_t total_recv_packets = 0; + uint64_t total_dump_packets = 0; - for (int i = 0; i < instance->nr_thread; i++) - { - if (instance->th_instances[i]->enable == 0) continue; + for (int i = 0; i < instance->nr_thread; i++) + { + if (instance->th_instances[i]->enable == 0) + continue; + + fprintf(stderr, " Thread %d: dumpfile %s, %" PRIu64 " packets recved, %" PRIu64 " packets captured.\n", i, + instance->th_instances[i]->str_thread_dumpfile, instance->th_instances[i]->nr_recv_packets, + instance->th_instances[i]->nr_dump_packets); - fprintf(stderr, " Thread %d: dumpfile %s, %"PRIu64" packets recved, %"PRIu64" packets captured.\n", - i, instance->th_instances[i]->str_thread_dumpfile, - instance->th_instances[i]->nr_recv_packets, - instance->th_instances[i]->nr_dump_packets); + total_recv_packets += instance->th_instances[i]->nr_recv_packets; + total_dump_packets += instance->th_instances[i]->nr_dump_packets; + } - total_recv_packets += instance->th_instances[i]->nr_recv_packets; - total_dump_packets += instance->th_instances[i]->nr_dump_packets; - } - - fprintf(stderr, " Summary : dumpfile %s, %"PRIu64" packets recved, %"PRIu64" packets captured.\n\n", - instance->str_dumpfile, total_recv_packets, total_dump_packets); + fprintf(stderr, " Summary : dumpfile %s, %" PRIu64 " packets recved, %" PRIu64 " packets captured.\n\n", + instance->str_dumpfile, total_recv_packets, total_dump_packets); - return; + return; } -void __thread_dumpfile_generate(const char * filename, unsigned int thread_id, - char * out_filename, size_t sz_max_out) +void __thread_dumpfile_generate(const char * filename, unsigned int thread_id, char * out_filename, size_t sz_max_out) { - char _filename[LINE_MAX] = { 0 }; + char _filename[LINE_MAX] = {0}; strncpy(_filename, filename, sizeof(_filename)); - + char * str_last_delimiter = strrchr(_filename, '.'); /* 没有拓展名,直接在文件后拼接线程号 */ - if(str_last_delimiter == NULL) + if (str_last_delimiter == NULL) { snprintf(out_filename, sz_max_out, "%s_%d", _filename, thread_id); return; } - char __basename[LINE_MAX] = { 0 }; - char __typename[LINE_MAX] = { 0 }; + char __basename[LINE_MAX] = {0}; + char __typename[LINE_MAX] = {0}; /* 生成basename,即最后一个点前面的部分 */ *str_last_delimiter = '\0'; snprintf(__basename, sizeof(__basename), "%s", _filename); - + /* 生成拓展名,包含点和后面的部分 */ *str_last_delimiter = '.'; snprintf(__typename, sizeof(__typename), "%s", str_last_delimiter); @@ -234,8 +232,7 @@ void __thread_dumpfile_generate(const char * filename, unsigned int thread_id, return; } -int dump_thread_init(struct dump_instance * instance, - struct dump_thread_instance * th_instance, unsigned int thread_id) +int dump_thread_init(struct dump_instance * instance, struct dump_thread_instance * th_instance, unsigned int thread_id) { /* 设置了线程掩码,需要判断本线程是否需要启动 */ if (mask_is_set(instance->capture_cpu_mask, thread_id) == 0) @@ -247,35 +244,33 @@ int dump_thread_init(struct dump_instance * instance, if (instance->fp_dumpfile) { /* 如果设置了FP,直接打开FILE结构体,通常是STDOUT */ - th_instance->pcap_dumper = pcap_dump_fopen(instance->pcap_handle_dead, - instance->fp_dumpfile); + th_instance->pcap_dumper = pcap_dump_fopen(instance->pcap_handle_dead, instance->fp_dumpfile); } else { /* 生成本线程使用的DUMPFILE文件名 */ - __thread_dumpfile_generate(instance->str_dumpfile, thread_id, - th_instance->str_thread_dumpfile, sizeof(th_instance->str_thread_dumpfile)); + __thread_dumpfile_generate(instance->str_dumpfile, thread_id, th_instance->str_thread_dumpfile, + sizeof(th_instance->str_thread_dumpfile)); /* 打开本线程使用的Dumper */ - th_instance->pcap_dumper = pcap_dump_open(instance->pcap_handle_dead, - th_instance->str_thread_dumpfile); + th_instance->pcap_dumper = pcap_dump_open(instance->pcap_handle_dead, th_instance->str_thread_dumpfile); } if (th_instance->pcap_dumper == NULL) { - fprintf(stderr, "Cannot open pcap dumper for thread %d, dumpfile is %s : %s.\n", - thread_id, instance->str_bpf_expr, pcap_geterr(instance->pcap_handle_dead)); + fprintf(stderr, "Cannot open pcap dumper for thread %d, dumpfile is %s : %s.\n", thread_id, + instance->str_bpf_expr, pcap_geterr(instance->pcap_handle_dead)); return RT_ERR; } /* 编译BPF规则 */ - int ret = pcap_compile(instance->pcap_handle_dead, &th_instance->bpf_prog, - instance->str_bpf_expr, 1, PCAP_NETMASK_UNKNOWN); + int ret = pcap_compile(instance->pcap_handle_dead, &th_instance->bpf_prog, instance->str_bpf_expr, 1, + PCAP_NETMASK_UNKNOWN); - if(ret < 0) + if (ret < 0) { - fprintf(stderr, "Cannot compile bpf rules for thread %d, bpf rule is %s: %s.\n", - thread_id, instance->str_bpf_expr, pcap_geterr(instance->pcap_handle_dead)); + fprintf(stderr, "Cannot compile bpf rules for thread %d, bpf rule is %s: %s.\n", thread_id, + instance->str_bpf_expr, pcap_geterr(instance->pcap_handle_dead)); return RT_ERR; } @@ -284,9 +279,9 @@ int dump_thread_init(struct dump_instance * instance, /* 创建线程 */ ret = pthread_create(&th_instance->pid, NULL, dump_capture_thread, (void *)th_instance); - if (ret < 0) + if (ret != 0) { - fprintf(stderr, "Cannot create capture thread %d : %s\n", thread_id, strerror(errno)); + fprintf(stderr, "Cannot create capture thread %d : %s\n", thread_id, strerror(ret)); return RT_ERR; } @@ -294,53 +289,55 @@ int dump_thread_init(struct dump_instance * instance, return RT_SUCCESS; } -int main(int argc, char **argv) +int main(int argc, char ** argv) { g_dump_instance = malloc(sizeof(struct dump_instance)); memset(g_dump_instance, 0, sizeof(struct dump_instance)); assert(argc > 0); snprintf(g_dump_instance->appsym, sizeof(g_dump_instance->appsym), "%s", basename(argv[0])); - snprintf(g_dump_instance->str_dumpfile, sizeof(g_dump_instance->str_dumpfile), - "%s", DUMP_DEFAULT_STR_DUMPFILE); + snprintf(g_dump_instance->str_dumpfile, sizeof(g_dump_instance->str_dumpfile), "%s", DUMP_DEFAULT_STR_DUMPFILE); g_dump_instance->nr_thread = DUMP_DEFAULT_NR_THREAD; g_dump_instance->keep_running = 1; g_dump_instance->capture_cpu_mask = (mask_t)-1; - + /* 处理命令行参数 */ int opt = 0; - while ((opt = getopt(argc, argv, "p:w:c:i:q:")) != -1) - { + while ((opt = getopt(argc, argv, "p:w:c:i:q:")) != -1) + { char * endptr = NULL; - switch (opt) + switch (opt) { - case 'p': /* 线程数 */ + case 'p': /* 线程数 */ g_dump_instance->nr_thread = strtoull(optarg, &endptr, 0); - if (g_dump_instance->nr_thread == 0 && endptr == optarg) usage(); + if (g_dump_instance->nr_thread == 0 && endptr == optarg) + usage(); break; - case 'c': /* 总预期报文数量 */ + case 'c': /* 总预期报文数量 */ g_dump_instance->nr_expect_packets = strtoull(optarg, &endptr, 0); - if (g_dump_instance->nr_expect_packets == 0 && endptr == optarg) usage(); - break; + if (g_dump_instance->nr_expect_packets == 0 && endptr == optarg) + usage(); + break; - case 'w': /* DUMPFILE文件模板 */ + case 'w': /* DUMPFILE文件模板 */ snprintf(g_dump_instance->str_dumpfile, sizeof(g_dump_instance->str_dumpfile), "%s", optarg); break; - case 'i': /* 网卡名称 */ + case 'i': /* 网卡名称 */ snprintf(g_dump_instance->devsym, sizeof(g_dump_instance->devsym), "%s", optarg); break; - case 'q': /* 线程掩码 */ - if (str_to_mask(optarg, &g_dump_instance->capture_cpu_mask) < 0) usage(); + case 'q': /* 线程掩码 */ + if (str_to_mask(optarg, &g_dump_instance->capture_cpu_mask) < 0) + usage(); break; - + default: - usage(); - } - } + usage(); + } + } /* 合成BPF规则 */ char str_bpf_expr[MR_STRING_MAX]; @@ -348,19 +345,19 @@ int main(int argc, char **argv) for (int i = optind; i < argc; i++) { - curser_str_bpf_expr += snprintf(str_bpf_expr + curser_str_bpf_expr, - sizeof(str_bpf_expr) - curser_str_bpf_expr, "%s ", argv[i]); + curser_str_bpf_expr += snprintf(str_bpf_expr + curser_str_bpf_expr, sizeof(str_bpf_expr) - curser_str_bpf_expr, + "%s ", argv[i]); } strncpy(g_dump_instance->str_bpf_expr, str_bpf_expr, sizeof(g_dump_instance->str_bpf_expr)); /* 打开PCAP句柄 */ - g_dump_instance->pcap_handle_dead = pcap_open_dead(DLT_EN10MB, 65535); - if (g_dump_instance->pcap_handle_dead == NULL) - { + g_dump_instance->pcap_handle_dead = pcap_open_dead(DLT_EN10MB, 65535); + if (g_dump_instance->pcap_handle_dead == NULL) + { fprintf(stderr, "Cannot open pcap handle: %s.\n", strerror(errno)); return RT_ERR; - } + } /* 打开MARSIO句柄 */ g_dump_instance->mr_instance = marsio_create(); @@ -372,8 +369,8 @@ int main(int argc, char **argv) } /* 打开MARSIO设备 */ - g_dump_instance->mr_vdev_handler = marsio_open_device(g_dump_instance->mr_instance, - g_dump_instance->devsym, g_dump_instance->nr_thread, g_dump_instance->nr_thread); + g_dump_instance->mr_vdev_handler = marsio_open_device(g_dump_instance->mr_instance, g_dump_instance->devsym, + g_dump_instance->nr_thread, g_dump_instance->nr_thread); if (g_dump_instance->mr_vdev_handler == NULL) { @@ -397,17 +394,19 @@ int main(int argc, char **argv) memset(g_dump_instance->th_instances[i], 0, sizeof(struct dump_thread_instance)); int ret = dump_thread_init(g_dump_instance, g_dump_instance->th_instances[i], i); - if (ret < 0) return ret; + if (ret < 0) + return ret; } /* 等待线程结束 */ for (int i = 0; i < g_dump_instance->nr_thread; i++) { - if (g_dump_instance->th_instances[i]->enable == 0) continue; + if (g_dump_instance->th_instances[i]->enable == 0) + continue; pthread_join(g_dump_instance->th_instances[i]->pid, NULL); } pcap_close(g_dump_instance->pcap_handle_dead); - dump_capture_result(g_dump_instance); - return 0; + dump_capture_result(g_dump_instance); + return 0; }
\ No newline at end of file diff --git a/tunnat/src/core.cc b/tunnat/src/core.cc index 1999d96..5788a01 100644 --- a/tunnat/src/core.cc +++ b/tunnat/src/core.cc @@ -1,19 +1,19 @@ +#include <cassert> #include <string> #include <vector> -#include <cassert> -#include <tunnat.h> #include <session.h> +#include <tunnat.h> extern "C" { -#include <systemd/sd-daemon.h> -#include <unistd.h> -#include <getopt.h> +#include <MESA_prof_load.h> #include <common.h> +#include <getopt.h> #include <marsio.h> -#include <MESA_prof_load.h> +#include <systemd/sd-daemon.h> +#include <unistd.h> } unsigned int g_logger_to_stdout = 1; @@ -42,17 +42,15 @@ std::vector<std::string> __strsplit(const std::string & s, char seperator) prev_pos = ++pos; } - output.push_back(s.substr(prev_pos, pos - prev_pos)); + output.push_back(s.substr(prev_pos, pos - prev_pos)); return output; } /* 单个设备打开 */ -int __open_single_device(TunnatInstance * instance, - std::vector<struct TunnatInstance::_devs> & vec_devices, std::string str_devsym, - unsigned nr_rxstream, unsigned int nr_txstream) +int __open_single_device(TunnatInstance * instance, std::vector<struct TunnatInstance::_devs> & vec_devices, + std::string str_devsym, unsigned nr_rxstream, unsigned int nr_txstream) { - struct mr_vdev * vdev = marsio_open_device(instance->mr_instance, - str_devsym.c_str(), nr_rxstream, nr_txstream); + struct mr_vdev * vdev = marsio_open_device(instance->mr_instance, str_devsym.c_str(), nr_rxstream, nr_txstream); if (vdev == NULL) { @@ -60,8 +58,7 @@ int __open_single_device(TunnatInstance * instance, return RT_ERR; } - struct mr_sendpath * sendpath = marsio_sendpath_create(instance->mr_instance, - MR_SENDPATH_VDEV, vdev); + struct mr_sendpath * sendpath = marsio_sendpath_create(instance->mr_instance, MR_SENDPATH_VDEV, vdev); if (sendpath == NULL) { @@ -84,10 +81,10 @@ int tunnat_devices_setup(TunnatInstance * instance) char cstr_virtdev_list[MR_STRING_MAX]; /* 读设备侧网卡、应用侧网卡名称 */ - MESA_load_profile_string_def(instance->cfgfile.c_str(), "tunnat", "phydev", - cstr_phydev_list, sizeof(cstr_phydev_list), ""); - MESA_load_profile_string_def(instance->cfgfile.c_str(), "tunnat", "virtdev", - cstr_virtdev_list, sizeof(cstr_virtdev_list), ""); + MESA_load_profile_string_def(instance->cfgfile.c_str(), "tunnat", "phydev", cstr_phydev_list, + sizeof(cstr_phydev_list), ""); + MESA_load_profile_string_def(instance->cfgfile.c_str(), "tunnat", "virtdev", cstr_virtdev_list, + sizeof(cstr_virtdev_list), ""); /* 按逗号切分 */ auto vec_phydev_list = __strsplit(std::string(cstr_phydev_list), ','); @@ -99,14 +96,14 @@ int tunnat_devices_setup(TunnatInstance * instance) /* 调用辅助函数,初始化网卡句柄 */ for (auto & str_devsym : vec_phydev_list) { - if (__open_single_device(instance, instance->phydevs, str_devsym, - nr_rxstream, nr_txstream) != RT_SUCCESS) return RT_ERR; + if (__open_single_device(instance, instance->phydevs, str_devsym, nr_rxstream, nr_txstream) != RT_SUCCESS) + return RT_ERR; } for (auto & str_devsym : vec_virtdev_list) { - if (__open_single_device(instance, instance->virtdevs, str_devsym, - nr_rxstream, nr_txstream) != RT_SUCCESS) return RT_ERR; + if (__open_single_device(instance, instance->virtdevs, str_devsym, nr_rxstream, nr_txstream) != RT_SUCCESS) + return RT_ERR; } /* 告警,用户没有指定设备侧网卡或应用侧网卡 */ @@ -123,7 +120,7 @@ int tunnat_devices_setup(TunnatInstance * instance) return RT_SUCCESS; } -#define C_I_VLAN_PAIR_MAX 2048 +#define C_I_VLAN_PAIR_MAX 2048 int tunnat_vlan_flipping_setup(TunnatInstance * instance) { @@ -132,7 +129,7 @@ int tunnat_vlan_flipping_setup(TunnatInstance * instance) /* 是否启用VLAN FLIPPING */ MESA_load_profile_uint_def(cfgfile, "vlan_flipping", "enable", &__enable_vlan_flipping, 0); - instance->en_vlan_flipping= __enable_vlan_flipping; + instance->en_vlan_flipping = __enable_vlan_flipping; if (!instance->en_vlan_flipping) { @@ -140,7 +137,7 @@ int tunnat_vlan_flipping_setup(TunnatInstance * instance) } /* 读VLAN FLIPPING映射表 */ - for(unsigned int i = 0; i < C_I_VLAN_PAIR_MAX; i++) + for (unsigned int i = 0; i < C_I_VLAN_PAIR_MAX; i++) { char c_router_cfg_name[MR_SYMBOL_MAX] = {}; char i_router_cfg_name[MR_SYMBOL_MAX] = {}; @@ -192,8 +189,8 @@ int tunnat_vlan_flipping_setup(TunnatInstance * instance) instance->vlan_flipping_map_is_c_router[u16_c_router_vlan_id] = true; instance->vlan_flipping_map_is_c_router[u16_i_router_vlan_id] = false; - MR_INFO("VLAN-FLIPPING-MAP %u: C-router-vlan-id %u, I-router-vlan-id %u, MAC FLIPPING: %s",i, - u16_c_router_vlan_id, u16_i_router_vlan_id, en_mac_flipping ? "true" : "false"); + MR_INFO("VLAN-FLIPPING-MAP %u: C-router-vlan-id %u, I-router-vlan-id %u, MAC FLIPPING: %s", i, + u16_c_router_vlan_id, u16_i_router_vlan_id, en_mac_flipping ? "true" : "false"); } return RT_SUCCESS; @@ -203,54 +200,54 @@ int tunnat_session_setup(TunnatInstance * instance) { /* 最大会话数 */ MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "nr_max_sessions", - &instance->sess_tb_nr_max_sessions, instance->sess_tb_nr_max_sessions); - + &instance->sess_tb_nr_max_sessions, instance->sess_tb_nr_max_sessions); + /* Hash表槽数量 */ - MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "nr_slots", - &instance->sess_tb_nr_slots, instance->sess_tb_nr_slots); + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "nr_slots", &instance->sess_tb_nr_slots, + instance->sess_tb_nr_slots); /* Hash表超时时间 */ - MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "expire_time", - &instance->sess_tb_expire_time, instance->sess_tb_expire_time); + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "expire_time", &instance->sess_tb_expire_time, + instance->sess_tb_expire_time); /* 使用四元组作为会话缓存的项 */ MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "use_tuple4_as_sskey", - &instance->sess_tb_order_by_tuple4, instance->sess_tb_order_by_tuple4); + &instance->sess_tb_order_by_tuple4, instance->sess_tb_order_by_tuple4); /* 记录Session的变化 */ MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "trace_ss_table_change", - &instance->sess_tb_log_change, instance->sess_tb_log_change); + &instance->sess_tb_log_change, instance->sess_tb_log_change); /* 检测流量迁移 */ - MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "check_ss_update", - &instance->sess_tb_check_update, instance->sess_tb_check_update); + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "check_ss_update", &instance->sess_tb_check_update, + instance->sess_tb_check_update); /* 链路信息表 */ MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "link_info_nr_max_sessions", - &instance->link_info_tb_nr_max_sessions, instance->link_info_tb_nr_max_sessions); + &instance->link_info_tb_nr_max_sessions, instance->link_info_tb_nr_max_sessions); MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "link_info_nr_slots", - &instance->link_info_tb_nr_slots, instance->link_info_tb_nr_slots); + &instance->link_info_tb_nr_slots, instance->link_info_tb_nr_slots); MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "link_info_expire_time", - &instance->link_info_tb_expire_time, instance->link_info_tb_expire_time); + &instance->link_info_tb_expire_time, instance->link_info_tb_expire_time); unsigned int __auto_reserve_tunnel; unsigned int __is_use_recent_tunnel; unsigned int __is_use_link_info_table; unsigned int __ctrlzone_addr_info_type; - MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "reverse_tunnel", - &__auto_reserve_tunnel, (unsigned int)instance->is_auto_reserve_tunnel); + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "reverse_tunnel", &__auto_reserve_tunnel, + (unsigned int)instance->is_auto_reserve_tunnel); - MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "use_recent_tunnel", - &__is_use_recent_tunnel, (unsigned int)instance->is_use_recent_tunnel); + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "use_recent_tunnel", &__is_use_recent_tunnel, + (unsigned int)instance->is_use_recent_tunnel); - MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "use_link_info_table", - &__is_use_link_info_table, (unsigned int)instance->is_use_link_info_table); + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "use_link_info_table", &__is_use_link_info_table, + (unsigned int)instance->is_use_link_info_table); MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "ctrlzone_addr_info_type", - &__ctrlzone_addr_info_type, (unsigned int)instance->ctrlzone_addr_info_type); + &__ctrlzone_addr_info_type, (unsigned int)instance->ctrlzone_addr_info_type); instance->is_auto_reserve_tunnel = __auto_reserve_tunnel > 0; instance->is_use_recent_tunnel = __is_use_recent_tunnel > 0; @@ -269,7 +266,7 @@ int tunnat_session_setup(TunnatInstance * instance) MR_INFO(" SessionTable Slots : %u", instance->sess_tb_nr_slots); MR_INFO(" SessionTable ExpireTime : %u", instance->sess_tb_expire_time); - if(instance->is_use_link_info_table) + if (instance->is_use_link_info_table) { MR_INFO(" LinkInfoTable Max Sessions : %u", instance->link_info_tb_nr_max_sessions); MR_INFO(" LinkInfoTable Slots : %u", instance->link_info_tb_nr_slots); @@ -282,10 +279,10 @@ int tunnat_session_setup(TunnatInstance * instance) int tunnat_mrinstance_setup(TunnatInstance * instance) { // 读CPU运行核心数 - unsigned int lcore_id_range[MR_TUNNAT_LCORE_MAX] = { 0 }; + unsigned int lcore_id_range[MR_TUNNAT_LCORE_MAX] = {0}; - int lcore_id_count = MESA_load_profile_uint_range(instance->cfgfile.c_str(), - "tunnat", "lcore_id", MR_TUNNAT_LCORE_MAX, lcore_id_range); + int lcore_id_count = MESA_load_profile_uint_range(instance->cfgfile.c_str(), "tunnat", "lcore_id", + MR_TUNNAT_LCORE_MAX, lcore_id_range); if (lcore_id_count < 0) { @@ -303,8 +300,8 @@ int tunnat_mrinstance_setup(TunnatInstance * instance) // 读应用名称 char cstr_appsym[MR_STRING_MAX]; - MESA_load_profile_string_def(instance->cfgfile.c_str(), "tunnat", "appsym", - cstr_appsym, sizeof(cstr_appsym), MR_TUNNAT_DEFAULT_APPSYM); + MESA_load_profile_string_def(instance->cfgfile.c_str(), "tunnat", "appsym", cstr_appsym, sizeof(cstr_appsym), + MR_TUNNAT_DEFAULT_APPSYM); instance->str_appsym = std::string(cstr_appsym); @@ -312,23 +309,21 @@ int tunnat_mrinstance_setup(TunnatInstance * instance) struct mr_instance * _mr_instance = marsio_create(); assert(_mr_instance != NULL); - int ret = marsio_option_set(_mr_instance, MARSIO_OPT_THREAD_MASK, - &instance->coremask, sizeof(instance->coremask)); + int ret = marsio_option_set(_mr_instance, MARSIO_OPT_THREAD_MASK, &instance->coremask, sizeof(instance->coremask)); if (ret < 0) { - MR_ERROR("Failed at set option THREAD_MASK for marsio instance: %s", - strerror(ret)); return RT_ERR; + MR_ERROR("Failed at set option THREAD_MASK for marsio instance: %s", strerror(ret)); + return RT_ERR; } unsigned int _true_value = 1; - ret = marsio_option_set(_mr_instance, MARSIO_OPT_EXIT_WHEN_ERR, - &_true_value, sizeof(_true_value)); + ret = marsio_option_set(_mr_instance, MARSIO_OPT_EXIT_WHEN_ERR, &_true_value, sizeof(_true_value)); if (ret < 0) { - MR_ERROR("Failed at set option EXIT_WHEN_ERR for marsio instance: %s", - strerror(ret)); return RT_ERR; + MR_ERROR("Failed at set option EXIT_WHEN_ERR for marsio instance: %s", strerror(ret)); + return RT_ERR; } // 初始化MR4句柄 @@ -342,16 +337,15 @@ int tunnat_mrinstance_setup(TunnatInstance * instance) instance->mr_instance = _mr_instance; // Idle threshold - MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "idle_threshold", - &instance->idle_threshold, MR_TUNNAT_DEFAULT_IDLE_POLL_THRESHOLD); + MESA_load_profile_uint_def(instance->cfgfile.c_str(), "tunnat", "idle_threshold", &instance->idle_threshold, + MR_TUNNAT_DEFAULT_IDLE_POLL_THRESHOLD); return RT_SUCCESS; } extern void * tunnat_thread_loop(void * arg); -int tunnat_thread_setup(TunnatInstance * instance, TunnatThreadInstance * th_instance, - unsigned int thread_id) +int tunnat_thread_setup(TunnatInstance * instance, TunnatThreadInstance * th_instance, unsigned int thread_id) { th_instance->thread_id = thread_id; th_instance->instance = instance; @@ -359,8 +353,8 @@ int tunnat_thread_setup(TunnatInstance * instance, TunnatThreadInstance * th_ins th_instance->th_stat = new TunnatThreadStat; th_instance->tun_container_array = new TunnelContainerArray[instance->nr_burst]; - int ret = th_instance->ss_table->Init(instance->sess_tb_nr_max_sessions, - instance->sess_tb_nr_slots, instance->sess_tb_expire_time); + int ret = th_instance->ss_table->Init(instance->sess_tb_nr_max_sessions, instance->sess_tb_nr_slots, + instance->sess_tb_expire_time); if (ret < 0) { @@ -369,11 +363,11 @@ int tunnat_thread_setup(TunnatInstance * instance, TunnatThreadInstance * th_ins return RT_ERR; } - if(instance->is_use_link_info_table) + if (instance->is_use_link_info_table) { th_instance->link_info_table = new LinkInfoTable; ret = th_instance->link_info_table->Init(instance->link_info_tb_nr_max_sessions, - instance->link_info_tb_nr_slots, instance->link_info_tb_expire_time); + instance->link_info_tb_nr_slots, instance->link_info_tb_expire_time); if (ret < 0) { @@ -447,28 +441,29 @@ int main(int argc, char * argv[]) { g_instance->thread_instances[i] = new TunnatThreadInstance; ret = tunnat_thread_setup(g_instance, g_instance->thread_instances[i], i); - if (ret < 0) goto err_out; + if (ret < 0) + goto err_out; - ret = pthread_create(&__pid[i], NULL, tunnat_thread_loop, - (void *)g_instance->thread_instances[i]); + ret = pthread_create(&__pid[i], NULL, tunnat_thread_loop, (void *)g_instance->thread_instances[i]); - if (ret < 0) + if (ret != 0) { - MR_ERROR("Create thread %d failed : %s", i, strerror(errno)); + MR_ERROR("Create thread %d failed : %s", i, strerror(ret)); goto err_out; } } pthread_t __pid_monit_thread; ret = pthread_create(&__pid_monit_thread, NULL, tunnat_monit_thread, g_instance); - if (ret < 0) + if (ret != 0) { - MR_ERROR("Create monitor thread failed : %s", strerror(errno)); + MR_ERROR("Create monitor thread failed : %s", strerror(ret)); goto err_out; } /* 采用NOTIFY方式启动,通知操作系统完成了初始化 */ - if (__check_is_notify()) sd_notify(0, "READY=1"); + if (__check_is_notify()) + sd_notify(0, "READY=1"); sleep(1); for (unsigned int i = 0; i < g_instance->nr_thread; i++) |
