diff options
Diffstat (limited to 'src/framework/hal')
-rw-r--r-- | src/framework/hal/hal.c | 883 | ||||
-rw-r--r-- | src/framework/hal/hal.h | 157 |
2 files changed, 504 insertions, 536 deletions
diff --git a/src/framework/hal/hal.c b/src/framework/hal/hal.c index 49b1fb7..c9e31cf 100644 --- a/src/framework/hal/hal.c +++ b/src/framework/hal/hal.c @@ -14,14 +14,14 @@ * limitations under the License. */ -#include "common_sys_config.h" -#include "common_mem_mbuf.h" +#include <errno.h> #include "nstack_log.h" #include "nstack_securec.h" #include "hal.h" -#include "hal_api.h" +#include "nsfw_hal_api.h" #define HAL_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) + /* *INDENT-OFF* */ static char hal_invalid_char_script[] = {'|', ';', '&', '$', '>', '<', '`', '\\', '\"', '\'', '(', ')', '[', ']', '~', '?', '*' @@ -39,26 +39,25 @@ static int netif_ops_init_flag = 0; netif_inst_t netif_tbl[HAL_MAX_NIC_NUM]; /* *INDENT-ON* */ -void -hal_io_adpt_register (const netif_ops_t * ops) +void hal_io_adpt_register(const netif_ops_t * ops) { - int icnt = 0; - if (netif_ops_init_flag == 0) + int icnt = 0; + if (netif_ops_init_flag == 0) { - (void) MEMSET_S (&netif_ops_table[0], sizeof (netif_ops_table), 0, - sizeof (netif_ops_table)); - netif_ops_init_flag = 1; + (void) memset_s(&netif_ops_table[0], sizeof(netif_ops_table), 0, + sizeof(netif_ops_table)); + netif_ops_init_flag = 1; } - for (icnt = 0; icnt < HAL_DRV_MAX; icnt++) + for (icnt = 0; icnt < HAL_DRV_MAX; icnt++) { - if (netif_ops_table[icnt] == 0) + if (netif_ops_table[icnt] == NULL) { - netif_ops_table[icnt] = ops; - break; + netif_ops_table[icnt] = ops; + break; } } - return; + return; } /***************************************************************************** @@ -73,43 +72,43 @@ hal_io_adpt_register (const netif_ops_t * ops) Calls : Called By : *****************************************************************************/ -int -hal_snprintf (char *buffer, size_t buflen, const char *format, ...) +int hal_snprintf(char *buffer, size_t buflen, const char *format, ...) { - int len; - va_list ap; + int len; + va_list ap; - /* check buffer validity */ - if (NULL == buffer || 0 == buflen) + /* need check buffer validity */ + if (NULL == buffer || 0 == buflen) { - goto einval_error; + goto einval_error; } - if (format == NULL) + if (format == NULL) { - buffer[0] = '\0'; + buffer[0] = '\0'; - goto einval_error; + goto einval_error; } - (void) va_start (ap, format); - len = VSNPRINTF_S (buffer, buflen, buflen - 1, format, ap); + (void) va_start(ap, format); /*no need to init */ + /*There are some unsafe function ,need to be replace with safe function */ + len = vsnprintf_s(buffer, buflen, buflen - 1, format, ap); - if (-1 == len) + if (-1 == len) { - va_end (ap); - goto einval_error; + va_end(ap); + goto einval_error; } - va_end (ap); + va_end(ap); - buffer[buflen - 1] = '\0'; + buffer[buflen - 1] = '\0'; - return len; + return len; -einval_error: - errno = EINVAL; - return -1; + einval_error: + errno = EINVAL; + return -1; } /***************************************************************************** @@ -121,99 +120,100 @@ einval_error: Calls : Called By : *****************************************************************************/ -int -hal_is_script_valid (const char *cmd) +int hal_is_script_valid(const char *cmd) { - unsigned int i; + unsigned int i; - if (cmd) + if (cmd) { - char *cmd_str = (char *) cmd; + char *cmd_str = (char *) cmd; - /* skip space */ - while (*cmd_str == ' ' || *cmd_str == '\t') + /* skip space */ + while (*cmd_str == ' ' || *cmd_str == '\t') { - cmd_str++; + cmd_str++; } - /* cmd can not start with ./ and ../ */ - for (i = 0; i < HAL_ARRAY_SIZE (hal_invalid_str_script_begin); i++) + /* cmd can not start with ./ and ../ */ + for (i = 0; i < HAL_ARRAY_SIZE(hal_invalid_str_script_begin); i++) { - if (strstr (cmd_str, hal_invalid_str_script_begin[i]) == cmd_str) + if (strstr(cmd_str, hal_invalid_str_script_begin[i]) == cmd_str) { - return 0; + return 0; } } - /* cmd can not include | ; $ and so on */ - for (i = 0; i < HAL_ARRAY_SIZE (hal_invalid_char_script); i++) + /* cmd can not include | ; $ and so on */ + for (i = 0; i < HAL_ARRAY_SIZE(hal_invalid_char_script); i++) { - if (strchr (cmd, hal_invalid_char_script[i])) + if (strchr(cmd, hal_invalid_char_script[i])) { - return 0; + return 0; } } - /* cmd can not include && || >> and so on */ - for (i = 0; i < HAL_ARRAY_SIZE (hal_invalid_str_script); i++) + /* cmd can not include && || >> and so on */ + for (i = 0; i < HAL_ARRAY_SIZE(hal_invalid_str_script); i++) { - if (strstr (cmd, hal_invalid_str_script[i])) + if (strstr(cmd, hal_invalid_str_script[i])) { - return 0; + return 0; } } - return 1; + return 1; } - return 0; + return 0; } -/***************************************************************************** - Prototype : hal_run_script - Description : run shell script - Input : const char* cmd - char* result_buf - size_t max_result_len - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -int -hal_run_script (const char *cmd, char *result_buf, size_t max_result_len) +int hal_run_script(const char *cmd, char *result_buf, size_t max_result_len) { - size_t n; - if (!cmd || !result_buf || max_result_len <= 1) + size_t n; + if (!cmd || !result_buf || max_result_len <= 1) { - return -1; + return -1; } - FILE *fd = popen (cmd, "r"); + FILE *fd = popen(cmd, "r"); - if (fd != NULL) + if (fd != NULL) { - n = fread (result_buf, sizeof (char), max_result_len - 1, fd); + n = fread(result_buf, sizeof(char), max_result_len - 1, fd); - if (n == 0) + if (n == 0) { - result_buf[0] = '\0'; + result_buf[0] = '\0'; } - else if ('\n' == result_buf[n - 1]) + else if ('\n' == result_buf[n - 1]) { - result_buf[n - 1] = '\0'; + result_buf[n - 1] = '\0'; } - /* make it null terminated */ - else + /* to make it null terminated */ + else { - result_buf[n] = '\0'; + result_buf[n] = '\0'; } - (void) pclose (fd); - return n; + (void) pclose(fd); + return n; } - return -1; + return -1; +} + +/* whether the nstack config file enable tso feature or not */ +static int config_enable_tso() +{ + /* tso not support for now */ + return 0; +} + +/* whether the nstack config file enable rx offload feature or not */ +static int config_enable_rx_offload() +{ + /* rx_offload enabled by config file */ + return 1; } /***************************************************************************** @@ -226,35 +226,34 @@ hal_run_script (const char *cmd, char *result_buf, size_t max_result_len) Calls : Called By : *****************************************************************************/ -int -hal_init_global (int argc, char **argv) +int hal_init_global(int argc, char **argv) { - int ret; - int netif_type; + int ret; + int netif_type; - ret = - MEMSET_S (netif_tbl, HAL_MAX_NIC_NUM * sizeof (netif_inst_t), 0, - HAL_MAX_NIC_NUM * sizeof (netif_inst_t)); - if (EOK != ret) + ret = + memset_s(netif_tbl, HAL_MAX_NIC_NUM * sizeof(netif_inst_t), 0, + HAL_MAX_NIC_NUM * sizeof(netif_inst_t)); + if (EOK != ret) { - NSHAL_LOGERR ("MEMSET_S failed"); - return -1; + NSHAL_LOGERR("memset_s failed"); + return -1; } - for (netif_type = 0; netif_ops_table[netif_type]; ++netif_type) + for (netif_type = 0; netif_ops_table[netif_type]; ++netif_type) { - if (netif_ops_table[netif_type]->init_global) + if (netif_ops_table[netif_type]->init_global) { - if (netif_ops_table[netif_type]->init_global (argc, argv)) + if (netif_ops_table[netif_type]->init_global(argc, argv)) { - NSHAL_LOGERR ("failed to init global]netif type=%d", - netif_type); - return -1; + NSHAL_LOGERR("failed to init global]netif type=%d", + netif_type); + return -1; } } } - return 0; + return 0; } /***************************************************************************** @@ -266,24 +265,24 @@ hal_init_global (int argc, char **argv) Calls : Called By : *****************************************************************************/ -int -hal_init_local () +int hal_init_local() { - int netif_type; + int netif_type; - for (netif_type = 0; netif_ops_table[netif_type]; ++netif_type) + for (netif_type = 0; netif_ops_table[netif_type]; ++netif_type) { - if (netif_ops_table[netif_type]->init_local) + if (netif_ops_table[netif_type]->init_local) { - if (netif_ops_table[netif_type]->init_local ()) + if (netif_ops_table[netif_type]->init_local()) { - NSHAL_LOGERR ("failed to init local]netif type=%d", netif_type); - return -1; + NSHAL_LOGERR("failed to init local]netif type=%d", + netif_type); + return -1; } } } - return 0; + return 0; } /***************************************************************************** @@ -295,96 +294,113 @@ hal_init_local () Calls : Called By : *****************************************************************************/ -hal_hdl_t -hal_get_invalid_hdl () +hal_hdl_t hal_get_invalid_hdl() { - return hal_invaldi_hdl; + return hal_invaldi_hdl; } -/***************************************************************************** - Prototype : hal_create - Description : create hal object - Input : const char* name - hal_netif_config_t* conf - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -hal_hdl_t -hal_create (const char *name, const char *nic_type, hal_netif_config_t * conf) +hal_hdl_t hal_create(const char *name, const char *nic_type, + hal_netif_config_t * conf) { - int ret = -1; - uint32_t netif_type; - netif_inst_t *inst; + int ret = -1; + uint32_t netif_type; + netif_inst_t *inst; + + /* which features netif support */ + struct hal_netif_hw_feature port_capa; - if ((NULL == name) || (NULL == conf)) + if ((NULL == name) || (NULL == conf)) { - NSHAL_LOGERR ("invalid para]name=%p,conf=%p", name, conf); - return hal_get_invalid_hdl (); + NSHAL_LOGERR("invalid para]name=%p,conf=%p", name, conf); + return hal_get_invalid_hdl(); } - inst = alloc_netif_inst (); + inst = alloc_netif_inst(); - if (NULL == inst) + if (NULL == inst) { - NSHAL_LOGERR ("failed to alloc netif inst]netif name=%s", name); + NSHAL_LOGERR("failed to alloc netif inst]netif name=%s", name); - return hal_get_invalid_hdl (); + return hal_get_invalid_hdl(); } - /*open */ - for (netif_type = 0; NULL != netif_ops_table[netif_type]; ++netif_type) + /*open */ + for (netif_type = 0; NULL != netif_ops_table[netif_type]; ++netif_type) { - ret = netif_ops_table[netif_type]->open (inst, name, nic_type); + ret = netif_ops_table[netif_type]->open(inst, name, nic_type); - if (0 == ret) + if (0 == ret) { - inst->ops = netif_ops_table[netif_type]; + inst->ops = netif_ops_table[netif_type]; - NSHAL_LOGINF ("netif ops]netif type=%u, netif name=%s", netif_type, - inst->ops->name); + NSHAL_LOGINF("netif ops]netif type=%u, netif name=%s", + netif_type, inst->ops->name); - break; + break; } } - if (ret != 0) + if (ret != 0) { - inst->state = NETIF_STATE_FREE; + inst->state = NETIF_STATE_FREE; - inst->ops = NULL; + inst->ops = NULL; - NSHAL_LOGERR ("open fail]netif name=%s", name); + NSHAL_LOGERR("open fail]netif name=%s", name); - return hal_get_invalid_hdl (); + return hal_get_invalid_hdl(); } - /*config */ - ret = inst->ops->config (inst, conf); + /* make sure which capabilities nic hardware support */ + ret = inst->ops->capability(inst, &port_capa); + if (ret != 0) + { + inst->state = NETIF_STATE_FREE; + + NSHAL_LOGERR("config fail]netif name=%s", name); + + return hal_get_invalid_hdl(); + } + + /* set port config per port capabilities */ + conf->hw_config.rx_csum_ip_flag = port_capa.rx_csum_ip + && config_enable_rx_offload()? 1 : 0; + conf->hw_config.rx_csum_l4_flag = port_capa.rx_csum_l4 + && config_enable_rx_offload()? 1 : 0; + conf->hw_config.tx_csum_ip_flag = port_capa.tx_csum_ip; + conf->hw_config.tx_csum_l4_flag = port_capa.tx_csum_tcp + && port_capa.tx_csum_udp; + conf->hw_config.tx_tso_flag = port_capa.tx_tso + && config_enable_tso()? 1 : 0; + + /*config */ + ret = inst->ops->config(inst, conf); - if (ret != 0) + if (ret != 0) { - inst->state = NETIF_STATE_FREE; + inst->state = NETIF_STATE_FREE; - NSHAL_LOGERR ("config fail]netif name=%s", name); + NSHAL_LOGERR("config fail]netif name=%s", name); - return hal_get_invalid_hdl (); + return hal_get_invalid_hdl(); } - /*start */ - ret = inst->ops->start (inst); + /* update netif offload value per config */ + inst->hw_config = conf->hw_config; - if (ret != 0) + /*start */ + ret = inst->ops->start(inst); + + if (ret != 0) { - inst->state = NETIF_STATE_FREE; + inst->state = NETIF_STATE_FREE; - NSHAL_LOGERR ("start fail]netif name=%s", name); + NSHAL_LOGERR("start fail]netif name=%s", name); - return hal_get_invalid_hdl (); + return hal_get_invalid_hdl(); } - return inst->hdl; + return inst->hdl; } /***************************************************************************** @@ -398,328 +414,229 @@ hal_create (const char *name, const char *nic_type, hal_netif_config_t * conf) Calls : Called By : *****************************************************************************/ -hal_hdl_t -hal_bond (const char *bond_name, uint8_t slave_num, hal_hdl_t slave_hdl[]) +hal_hdl_t hal_bond(const char *bond_name, uint8_t slave_num, + hal_hdl_t slave_hdl[]) { - int i, ret; - netif_inst_t *inst; - netif_inst_t *slave_inst[HAL_MAX_SLAVES_PER_BOND]; + int i, ret; + netif_inst_t *inst; + netif_inst_t *slave_inst[HAL_MAX_SLAVES_PER_BOND]; - if ((0 == slave_num) || (HAL_MAX_SLAVES_PER_BOND < slave_num) - || (NULL == bond_name) || (NULL == slave_hdl)) + if ((0 == slave_num) || (HAL_MAX_SLAVES_PER_BOND < slave_num) + || (NULL == bond_name) || (NULL == slave_hdl)) { - NSHAL_LOGERR ("invalid para]bond_name=%p,slave_num=%u,slave_hdl=%p,", - bond_name, slave_num, slave_hdl); + NSHAL_LOGERR("invalid para]bond_name=%p,slave_num=%u,slave_hdl=%p,", + bond_name, slave_num, slave_hdl); - return hal_get_invalid_hdl (); + return hal_get_invalid_hdl(); } - for (i = 0; i < slave_num; i++) + for (i = 0; i < slave_num; i++) { - slave_inst[i] = get_netif_inst (slave_hdl[i]); + slave_inst[i] = get_netif_inst(slave_hdl[i]); - if (NULL == slave_inst[i]) + if (NULL == slave_inst[i]) { - NSHAL_LOGERR ("invalid para slave_hdl]index=%d, slave_inst=%d", i, - slave_hdl[i].id); - return hal_get_invalid_hdl (); + NSHAL_LOGERR("invalid para slave_hdl]index=%d, slave_inst=%d", i, + slave_hdl[i].id); + return hal_get_invalid_hdl(); } } - inst = alloc_netif_inst (); + inst = alloc_netif_inst(); - if (NULL == inst) + if (NULL == inst) { - NSHAL_LOGERR ("failed to alloc nic inst]bond_name=%s", bond_name); - return hal_get_invalid_hdl (); + NSHAL_LOGERR("failed to alloc nic inst]bond_name=%s", bond_name); + return hal_get_invalid_hdl(); } - inst->ops = slave_inst[0]->ops; + inst->ops = slave_inst[0]->ops; + inst->hw_config = slave_inst[0]->hw_config; - ret = inst->ops->bond (inst, bond_name, slave_num, slave_inst); + ret = inst->ops->bond(inst, bond_name, slave_num, slave_inst); - if (0 != ret) + if (0 != ret) { - inst->state = NETIF_STATE_FREE; + inst->state = NETIF_STATE_FREE; - inst->ops = NULL; + inst->ops = NULL; - NSHAL_LOGERR ("bond netif fail]bond_name=%s", bond_name); + NSHAL_LOGERR("bond netif fail]bond_name=%s", bond_name); - return hal_get_invalid_hdl (); + return hal_get_invalid_hdl(); } - return inst->hdl; + return inst->hdl; } -/***************************************************************************** - Prototype : hal_close - Description : destroy hal object - Input : hal_hdl_t hdl - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -int -hal_close (hal_hdl_t hdl) +int hal_close(hal_hdl_t hdl) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return -1; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return -1; } - inst->state = NETIF_STATE_FREE; + inst->state = NETIF_STATE_FREE; - return inst->ops->close (inst); + return inst->ops->close(inst); } -/***************************************************************************** - Prototype : hal_stop - Description : stop recv packet - Input : hal_hdl_t hdl - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -int -hal_stop (hal_hdl_t hdl) +int hal_stop(hal_hdl_t hdl) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return -1; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return -1; } - return inst->ops->stop (inst); + return inst->ops->stop(inst); } -/***************************************************************************** - Prototype : hal_get_mtu - Description : get the mtu from nic - Input : hal_hdl_t hdl - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -uint32_t -hal_get_mtu (hal_hdl_t hdl) +uint32_t hal_get_mtu(hal_hdl_t hdl) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return 0; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return 0; } - return inst->ops->mtu (inst); + return inst->ops->mtu(inst); } -/***************************************************************************** - Prototype : hal_get_macaddr - Description : in normal mode, get the mac addr from nic - in bond mode1, get the mac addr from primary nic - Input : hal_hdl_t hdl - void* mac_addr - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -void -hal_get_macaddr (hal_hdl_t hdl, void *mac_addr) +void hal_get_macaddr(hal_hdl_t hdl, void *mac_addr) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return; } - (void) inst->ops->macaddr (inst, mac_addr); + (void) inst->ops->macaddr(inst, mac_addr); } /***************************************************************************** Prototype : hal_get_capability Description : get offload capability from nic Input : hal_hdl_t hdl - hal_netif_capa_t* info + strcuct hal_netif_hw_feature* info Output : None Return Value : Calls : Called By : *****************************************************************************/ -void -hal_get_capability (hal_hdl_t hdl, hal_netif_capa_t * info) +void hal_get_capability(hal_hdl_t hdl, struct hal_netif_hw_feature *info) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return; } - (void) inst->ops->capability (inst, info); + (void) inst->ops->capability(inst, info); } -/***************************************************************************** - Prototype : hal_recv_packet - Description : recv packet from nic - Input : hal_hdl_t hdl - uint16_t queue_id - struct common_mem_mbuf** rx_pkts - uint16_t nb_pkts - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -uint16_t -hal_recv_packet (hal_hdl_t hdl, uint16_t queue_id, - hal_mbuf_t ** rx_pkts, uint16_t nb_pkts) +uint16_t hal_recv_packet(hal_hdl_t hdl, uint16_t queue_id, + void **rx_pkts, uint16_t nb_pkts) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return 0; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return 0; } - return inst->ops->recv (inst, queue_id, rx_pkts, nb_pkts); + return inst->ops->recv(inst, queue_id, rx_pkts, nb_pkts); } -/***************************************************************************** - Prototype : hal_send_packet - Description : send packet to nic - Input : hal_hdl_t hdl - uint16_t queue_id - struct common_mem_mbuf** tx_pkts - uint16_t nb_pkts - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -uint16_t -hal_send_packet (hal_hdl_t hdl, uint16_t queue_id, - hal_mbuf_t ** tx_pkts, uint16_t nb_pkts) +uint16_t hal_send_packet(hal_hdl_t hdl, uint16_t queue_id, + void **tx_pkts, uint16_t nb_pkts) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return 0; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return 0; } - return inst->ops->send (inst, queue_id, tx_pkts, nb_pkts); + return inst->ops->send(inst, queue_id, tx_pkts, nb_pkts); } -/***************************************************************************** - Prototype : hal_link_status - Description : get link status form nic - Input : hal_hdl_t hdl - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -uint32_t -hal_link_status (hal_hdl_t hdl) +uint32_t hal_link_status(hal_hdl_t hdl) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return 0; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return 0; } - return inst->ops->link_status (inst); + return inst->ops->link_status(inst); } -/***************************************************************************** - Prototype : hal_stats - Description : get link statistics form nic - Input : hal_hdl_t hdl - hal_netif_stats_t* stats - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -int -hal_stats (hal_hdl_t hdl, hal_netif_stats_t * stats) +int hal_stats(hal_hdl_t hdl, hal_netif_stats_t * stats) { - netif_inst_t *inst; + netif_inst_t *inst; - if (NULL == stats) + if (NULL == stats) { - NSHAL_LOGERR ("invalid para"); - return -1; + NSHAL_LOGERR("invalid para"); + return -1; } - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return -1; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return -1; } - return inst->ops->stats (inst, stats); + return inst->ops->stats(inst, stats); } -/***************************************************************************** - Prototype : hal_stats_reset - Description : reset link statistics to nic - Input : hal_hdl_t hdl - Output : None - Return Value : - Calls : - Called By : -*****************************************************************************/ -void -hal_stats_reset (hal_hdl_t hdl) +void hal_stats_reset(hal_hdl_t hdl) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return; } - (void) inst->ops->stats_reset (inst); + (void) inst->ops->stats_reset(inst); } /***************************************************************************** @@ -734,28 +651,27 @@ hal_stats_reset (hal_hdl_t hdl) Calls : Called By : *****************************************************************************/ -int -hal_add_mcastaddr (hal_hdl_t hdl, void *mc_addr_set, - void *mc_addr, uint32_t nb_mc_addr) +int hal_add_mcastaddr(hal_hdl_t hdl, void *mc_addr_set, + void *mc_addr, uint32_t nb_mc_addr) { - int ret; - netif_inst_t *inst; + int ret; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return -1; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return -1; } - ret = inst->ops->mcastaddr (inst, mc_addr_set, mc_addr, nb_mc_addr); - /* if set mcast addr failed, we have to manually add the mac addr to nic. */ - if (ret < 0) + ret = inst->ops->mcastaddr(inst, mc_addr_set, mc_addr, nb_mc_addr); + /* if set mcast addr failed, we have to manually add the mac addr to nic. */ + if (ret < 0) { - ret = inst->ops->add_mac (inst, mc_addr); + ret = inst->ops->add_mac(inst, mc_addr); } - return ret; + return ret; } /***************************************************************************** @@ -770,28 +686,27 @@ hal_add_mcastaddr (hal_hdl_t hdl, void *mc_addr_set, Calls : Called By : *****************************************************************************/ -int -hal_del_mcastaddr (hal_hdl_t hdl, void *mc_addr_set, - void *mc_addr, uint32_t nb_mc_addr) +int hal_del_mcastaddr(hal_hdl_t hdl, void *mc_addr_set, + void *mc_addr, uint32_t nb_mc_addr) { - int ret; - netif_inst_t *inst; + int ret; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return -1; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return -1; } - ret = inst->ops->mcastaddr (inst, mc_addr_set, mc_addr, nb_mc_addr); - /* if set mcast addr failed, we have to manually delete the mac addr from nic. */ - if (ret < 0) + ret = inst->ops->mcastaddr(inst, mc_addr_set, mc_addr, nb_mc_addr); + /* if set mcast addr failed, we have to manually delete the mac addr from nic. */ + if (ret < 0) { - ret = inst->ops->rmv_mac (inst, mc_addr); + ret = inst->ops->rmv_mac(inst, mc_addr); } - return ret; + return ret; } /***************************************************************************** @@ -804,20 +719,19 @@ hal_del_mcastaddr (hal_hdl_t hdl, void *mc_addr_set, Calls : Called By : *****************************************************************************/ -void -hal_set_allmulti_mode (hal_hdl_t hdl, uint8_t enable) +void hal_set_allmulti_mode(hal_hdl_t hdl, uint8_t enable) { - netif_inst_t *inst; + netif_inst_t *inst; - inst = get_netif_inst (hdl); + inst = get_netif_inst(hdl); - if (inst == NULL) + if (inst == NULL) { - NSHAL_LOGERR ("netif does not exist]inst=%i", HAL_HDL_TO_ID (hdl)); - return; + NSHAL_LOGERR("netif does not exist]inst=%i", HAL_HDL_TO_ID(hdl)); + return; } - (void) inst->ops->allmcast (inst, enable); + (void) inst->ops->allmcast(inst, enable); } /***************************************************************************** @@ -829,35 +743,154 @@ hal_set_allmulti_mode (hal_hdl_t hdl, uint8_t enable) Calls : Called By : *****************************************************************************/ -uint32_t -hal_is_nic_exist (const char *name) +uint32_t hal_is_nic_exist(const char *name) { - char script_cmmd[HAL_SCRIPT_LENGTH]; - char result_buf[HAL_SCRIPT_LENGTH]; - int len_out; - int retval; + char script_cmmd[HAL_SCRIPT_LENGTH]; + char result_buf[HAL_SCRIPT_LENGTH]; + int len_out; + int retval; - if (!hal_is_script_valid (name)) + if (!hal_is_script_valid(name)) { - NSHAL_LOGERR ("nic name is not valid"); - return 0; + NSHAL_LOGERR("nic name is not valid"); + return 0; } - retval = - hal_snprintf (script_cmmd, sizeof (script_cmmd), - "sudo ifconfig -a | grep -w \"%s[ :]\"", name); - if (-1 == retval) + // no need to use sudo, as inquire has no need of high permission + retval = + hal_snprintf(script_cmmd, sizeof(script_cmmd), + "ifconfig -a | grep \"%s[ :]\"", name); + /* check return value */ + if (-1 == retval) { - NSHAL_LOGERR ("rte_snprintf failed]retval=%d", retval); - return 0; + NSHAL_LOGERR("hal_snprintf failed]retval=%d", retval); + return 0; } - len_out = hal_run_script (script_cmmd, result_buf, sizeof (result_buf) - 1); - /* buffer not initialized, should take length as decision */ - if (0 != len_out) + len_out = hal_run_script(script_cmmd, result_buf, sizeof(result_buf) - 1); + /* buffer not initialized, should take length as decision */ + if (0 >= len_out) + { + NSHAL_LOGERR("hal_run_script fail]len_out=%d", len_out); + return 0; + } + + return 1; +} + +int hal_check_rss(hal_hdl_t hdl, uint32_t saddr, uint32_t daddr, + uint16_t sport, uint16_t dport, uint16_t * thread_index) +{ + if (!thread_index) + { + NSHAL_LOGERR("thread_index is null"); + return -1; + } + + netif_inst_t *inst; + inst = get_netif_inst(hdl); + if (inst == NULL) + { + NSHAL_LOGERR("inst is null]inst=%d", HAL_HDL_TO_ID(hdl)); + return -1; + } + + return inst->ops->check_rss(inst, saddr, daddr, sport, dport, + thread_index); +} + +/***************************************************************************** +* Prototype : hal_bond_switch +* Description : bond switch API of hal layer +* Input : +* Output : None +* Return Value : int +* Calls : +* Called By : +*****************************************************************************/ +int hal_bond_switch(const char *bond_name) +{ + netif_inst_t *inst = get_netif_inst_by_name(bond_name); + if (!inst) + { + NSHAL_LOGERR("fail to get netif]name=%s", bond_name); + return -1; + } + + return inst->ops->port_switch(inst); +} + +/***************************************************************************** +* Prototype : hal_get_bond_primary +* Description : get bond primary API of hal layer +* Input : +* Output : None +* Return Value : int +* Calls : +* Called By : +*****************************************************************************/ +int hal_get_bond_primary(const char *bond_name) +{ + netif_inst_t *inst = get_netif_inst_by_name(bond_name); + if (!inst) + { + NSHAL_LOGERR("fail to get netif]name=%s", bond_name); + return -1; + } + + int primary_id = inst->ops->get_bond_primary(inst); + if (primary_id < 0) + { + NSHAL_LOGERR("get_bond_primary id fail]name=%s", bond_name); + return -1; + } + + int i; + for (i = 0; i < HAL_MAX_SLAVES_PER_BOND; i++) + { + if (primary_id == inst->data.dpdk_if.slave_port[i]) + { + return i; //found it! + } + } + + NSHAL_LOGERR + ("not found primary_id from slave_port array]primary_id=%d,slave[0]=%u,slave[1]=%u", + primary_id, inst->data.dpdk_if.slave_port[0], + inst->data.dpdk_if.slave_port[1]); + return -1; +} + +int hal_check_rss6(hal_hdl_t hdl, uint32_t saddr[4], uint32_t daddr[4], + uint16_t sport, uint16_t dport, uint16_t * thread_index) +{ + if (!thread_index) + { + NSHAL_LOGERR("thread_index is null"); + return -1; + } + + netif_inst_t *inst; + inst = get_netif_inst(hdl); + if (inst == NULL) + { + NSHAL_LOGERR("inst is null]inst=%d", HAL_HDL_TO_ID(hdl)); + return -1; + } + + return inst->ops->check_rss6(inst, saddr, daddr, sport, dport, + thread_index); +} + +struct hal_netif_hw_config *hal_get_netif_hw_config(hal_hdl_t hdl) +{ + netif_inst_t *inst; + inst = get_netif_inst(hdl); + if (inst == NULL) { - return 1; + NSHAL_LOGERR("inst is null]inst=%d", HAL_HDL_TO_ID(hdl)); + return NULL; } - return 0; + return &inst->hw_config; } diff --git a/src/framework/hal/hal.h b/src/framework/hal/hal.h index 5a8f51e..9d41bc2 100644 --- a/src/framework/hal/hal.h +++ b/src/framework/hal/hal.h @@ -18,8 +18,7 @@ #define _HAL_H_ #include <stdint.h> -#include "hal_api.h" -#include "nstack_log.h" +#include "nsfw_hal_api.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -29,150 +28,86 @@ extern "C" { #define HAL_DRV_MAX 32 -#define HAL_IO_REGISTER(name, ops) \ - static __attribute__((__constructor__)) void __hal_register##name(void) \ - {\ - hal_io_adpt_register(ops); \ - } \ - - #define HAL_MAX_PCI_ADDR_LEN 16 -#define HAL_MAX_DRIVER_NAME_LEN 128 - -#define HAL_MAX_PATH_LEN 4096 //max path length on linux is 4096 - #define HAL_SCRIPT_LENGTH 256 #define HAL_HDL_TO_ID(hdl) (hdl.id) -/* IO using DPDK interface */ -typedef struct dpdk_if -{ - uint8_t port_id; /**< DPDK port identifier */ - uint8_t slave_num; - uint8_t slave_port[HAL_MAX_SLAVES_PER_BOND]; - - uint32_t hw_vlan_filter:1; - uint32_t hw_vlan_strip:1; - uint32_t rsv30:30; - - uint32_t rx_queue_num; - uint32_t rx_ring_size[HAL_ETH_MAX_QUEUE_NUM]; - hal_mempool_t *rx_pool[HAL_ETH_MAX_QUEUE_NUM]; - - uint32_t tx_queue_num; - uint32_t tx_ring_size[HAL_ETH_MAX_QUEUE_NUM]; - - char pci_addr[HAL_MAX_PCI_ADDR_LEN]; - char nic_name[HAL_MAX_NIC_NAME_LEN]; - char nic_type[HAL_MAX_NIC_NAME_LEN]; - char driver_name[HAL_MAX_DRIVER_NAME_LEN]; -} dpdk_if_t; +extern netif_inst_t netif_tbl[HAL_MAX_NIC_NUM]; -typedef struct netif_inst +static inline netif_inst_t *alloc_netif_inst() { - enum - { - NETIF_STATE_FREE = 0, - NETIF_STATE_ACTIVE - } state; + int i; + netif_inst_t *inst; - hal_hdl_t hdl; + for (i = 0; i < HAL_MAX_NIC_NUM; ++i) + { + inst = &netif_tbl[i]; - const struct netif_ops *ops; /**< Implementation specific methods */ + if (NETIF_STATE_FREE == inst->state) + { + inst->state = NETIF_STATE_ACTIVE; - union - { - dpdk_if_t dpdk_if; /**< using DPDK for IO */ - } data; + inst->hdl.id = i; -} netif_inst_t; + return inst; + } + } -typedef struct netif_ops -{ - const char *name; - int (*init_global) (int argc, char **argv); - int (*init_local) (void); - int (*open) (netif_inst_t * inst, const char *name, const char *type); - int (*close) (netif_inst_t * inst); - int (*start) (netif_inst_t * inst); - int (*stop) (netif_inst_t * inst); - int (*bond) (netif_inst_t * inst, const char *bond_name, - uint8_t slave_num, netif_inst_t * slave[]); - uint32_t (*mtu) (netif_inst_t * inst); - int (*macaddr) (netif_inst_t * inst, void *mac_addr); - int (*capability) (netif_inst_t * inst, hal_netif_capa_t * info); - uint16_t (*recv) (netif_inst_t * inst, uint16_t queue_id, - hal_mbuf_t ** rx_pkts, uint16_t nb_pkts); - uint16_t (*send) (netif_inst_t * inst, uint16_t queue_id, - hal_mbuf_t ** tx_pkts, uint16_t nb_pkts); - uint32_t (*link_status) (netif_inst_t * inst); - int (*stats) (netif_inst_t * inst, hal_netif_stats_t * stats); - int (*stats_reset) (netif_inst_t * inst); - int (*config) (netif_inst_t * inst, hal_netif_config_t * conf); - int (*mcastaddr) (netif_inst_t * inst, void *mc_addr_set, - void *mc_addr, uint32_t nb_mc_addr); - int (*add_mac) (netif_inst_t * inst, void *mc_addr); - int (*rmv_mac) (netif_inst_t * inst, void *mc_addr); - int (*allmcast) (netif_inst_t * inst, uint8_t enable); -} netif_ops_t; + return NULL; -extern netif_inst_t netif_tbl[HAL_MAX_NIC_NUM]; +} -static inline netif_inst_t * -alloc_netif_inst () +static inline netif_inst_t *get_netif_inst(hal_hdl_t hdl) { - int i; - netif_inst_t *inst; + netif_inst_t *inst; - for (i = 0; i < HAL_MAX_NIC_NUM; ++i) + if (unlikely(!hal_is_valid(hdl))) { - inst = &netif_tbl[i]; + NSHAL_LOGERR("inst id is not valid]inst=%i, HAL_MAX_NIC_NUM=%d", + HAL_HDL_TO_ID(hdl), HAL_MAX_NIC_NUM); - if (NETIF_STATE_FREE == inst->state) - { - inst->state = NETIF_STATE_ACTIVE; + return NULL; + } - inst->hdl.id = i; + inst = &netif_tbl[HAL_HDL_TO_ID(hdl)]; - return inst; - } - } + if (unlikely((NETIF_STATE_ACTIVE != inst->state) || (NULL == inst->ops))) + { + NSHAL_LOGERR("netif is not active]inst=%i", HAL_HDL_TO_ID(hdl)); - return NULL; + return NULL; + } + return inst; } -static inline netif_inst_t * -get_netif_inst (hal_hdl_t hdl) +static inline netif_inst_t *get_netif_inst_by_name(const char *name) { - netif_inst_t *inst; + int i; + netif_inst_t *inst; - if (unlikely (!hal_is_valid (hdl))) + if (!name) { - NSHAL_LOGERR ("inst id is not valid]inst=%i, HAL_MAX_NIC_NUM=%d", - HAL_HDL_TO_ID (hdl), HAL_MAX_NIC_NUM); - - return NULL; + return NULL; } - inst = &netif_tbl[HAL_HDL_TO_ID (hdl)]; - - if (unlikely ((NETIF_STATE_ACTIVE != inst->state) || (NULL == inst->ops))) + for (i = 0; i < HAL_MAX_NIC_NUM; ++i) { - NSHAL_LOGERR ("netif is not active]inst=%i", HAL_HDL_TO_ID (hdl)); + inst = &netif_tbl[i]; - return NULL; + if (NETIF_STATE_ACTIVE == inst->state + && 0 == strncmp(name, inst->data.dpdk_if.nic_name, + HAL_MAX_NIC_NAME_LEN)) + { + return inst; + } } - return inst; -} + return NULL; -int hal_snprintf (char *buffer, size_t buflen, const char *format, ...); -int hal_is_script_valid (const char *cmd); -int hal_run_script (const char *cmd, char *result_buf, size_t max_result_len); -void hal_io_adpt_register (const netif_ops_t * ops); +} #ifdef __cplusplus /* *INDENT-OFF* */ |