From f560cf69b38f9ca8289499db53de8018e1330dcc Mon Sep 17 00:00:00 2001 From: olabjorsne Date: Tue, 9 Feb 2021 15:18:03 +0100 Subject: [PATCH 1/2] Simplify application configuration Moved initialization of mac addresses and lldp port name to stack. --- include/pnet_api.h | 18 +----- sample_app/sampleapp_common.c | 95 ++++------------------------ sample_app/sampleapp_common.h | 5 +- src/common/pf_dcp.c | 6 +- src/common/pf_eth.c | 92 +++++++++++++++++++-------- src/common/pf_lldp.c | 48 ++++++-------- src/device/pf_block_writer.c | 19 ++---- src/device/pf_cmina.c | 37 +++++------ src/device/pf_cmrpc.c | 2 +- src/device/pf_cmrpc_epm.c | 4 +- src/device/pf_fspm.c | 2 +- src/device/pf_pdport.c | 7 +- src/device/pf_port.c | 9 ++- src/pf_types.h | 15 +++-- src/ports/linux/sampleapp_main.c | 1 + src/ports/rt-kernel/sampleapp_main.c | 1 + test/mocks.cpp | 13 ++++ test/mocks.h | 3 + test/test_fspm.cpp | 2 +- test/utils_for_testing.cpp | 21 +----- 20 files changed, 179 insertions(+), 221 deletions(-) diff --git a/include/pnet_api.h b/include/pnet_api.h index c22c0692..9e9f5308 100644 --- a/include/pnet_api.h +++ b/include/pnet_api.h @@ -1170,24 +1170,12 @@ typedef struct pnet_ethaddr #define PNET_LLDP_PORT_ID_MAX_SIZE \ (PNET_STATION_NAME_MAX_SIZE + PNET_PORT_NAME_MAX_SIZE) -/** - * Network interface - */ -typedef struct pnet_netif -{ - char if_name[PNET_INTERFACE_NAME_MAX_SIZE]; /**< Terminated string */ - pnet_ethaddr_t eth_addr; /**< Interface MAC address */ -} pnet_netif_t; - /** * Physical Port Configuration */ typedef struct pnet_port_cfg { - pnet_netif_t phy_port; - char port_name[PNET_PORT_NAME_MAX_SIZE]; /**< Terminated string */ - uint16_t rtclass_2_status; - uint16_t rtclass_3_status; + const char * netif_name; } pnet_port_cfg_t; /** @@ -1213,10 +1201,10 @@ typedef struct pnet_ip_cfg */ typedef struct pnet_if_cfg { - pnet_netif_t main_port; /**< Main (DAP) network interface. */ + const char * main_netif_name; /**< Main (DAP) network interface. */ pnet_ip_cfg_t ip_cfg; /**< IP Settings for main network interface */ - pnet_port_cfg_t ports[PNET_MAX_PORT]; /**< Physical ports (DAP ports) */ + pnet_port_cfg_t physical_ports[PNET_MAX_PORT]; } pnet_if_cfg_t; /** diff --git a/sample_app/sampleapp_common.c b/sample_app/sampleapp_common.c index 926e1606..e65c68cf 100644 --- a/sample_app/sampleapp_common.c +++ b/sample_app/sampleapp_common.c @@ -60,7 +60,7 @@ static void ip_to_string (pnal_ipaddr_t ip, char * outputstring) (uint8_t) (ip & 0xFF)); } -void app_mac_to_string (pnal_ethaddr_t mac, char * outputstring) +void app_mac_to_string (pnet_ethaddr_t mac, char * outputstring) { snprintf ( outputstring, @@ -1317,7 +1317,6 @@ void app_plug_dap (pnet_t * net, void * arg) int app_pnet_cfg_init_default (pnet_cfg_t * stack_config) { - uint16_t i; memset (stack_config, 0, sizeof (pnet_cfg_t)); stack_config->tick_us = APP_TICK_INTERVAL_US; @@ -1391,18 +1390,6 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config) /* Timing */ stack_config->min_device_interval = 32; /* Corresponds to 1 ms */ - /* LLDP settings */ - for (i = 0; i < PNET_MAX_PORT; i++) - { - snprintf ( - stack_config->if_cfg.ports[i].port_name, - sizeof (stack_config->if_cfg.ports[i].port_name), - "port-%03d", - i + 1); - stack_config->if_cfg.ports[i].rtclass_2_status = 0; - stack_config->if_cfg.ports[i].rtclass_3_status = 0; - } - /* Network configuration */ stack_config->send_hello = true; stack_config->if_cfg.ip_cfg.dhcp_enable = false; @@ -1415,50 +1402,6 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config) return 0; } -/** - * Initialize a network interface configuration from a network - * interface name. - * This includes reading the Ethernet MAC address. - * If the network interface can not be found or the operation - * fails to read the mac address from the network interface, - * error is returned. - * @param netif_name In: Network interface name - * @param p_netif Out: Network interface configuration - * @param verbosity In: Verbosity - * @return 0 on success - * -1 on error - */ -int app_port_cfg_init ( - const char * netif_name, - pnet_netif_t * p_netif, - int verbosity) -{ - pnal_ethaddr_t mac; - char mac_string[PNAL_ETH_ADDRSTR_SIZE]; /* Terminated string */ - - int ret = pnal_get_macaddress (netif_name, &mac); - if (ret != 0) - { - printf ( - "Error: The given network interface does not exist: \"%s\"\n", - netif_name); - return -1; - } - else - { - if (verbosity > 0) - { - app_mac_to_string (mac, mac_string); - printf ("%-10s %s\n", netif_name, mac_string); - } - } - - strncpy (p_netif->if_name, netif_name, PNET_INTERFACE_NAME_MAX_SIZE); - memcpy (p_netif->eth_addr.addr, mac.addr, sizeof (pnal_ethaddr_t)); - - return 0; -} - int app_get_netif_namelist ( const char * arg_str, app_netif_namelist_t * p_if_list, @@ -1528,6 +1471,7 @@ int app_get_netif_namelist ( int app_pnet_cfg_init_netifs ( const char * netif_list_str, + app_netif_namelist_t * if_list, pnet_cfg_t * p_cfg, int verbosity) { @@ -1536,48 +1480,35 @@ int app_pnet_cfg_init_netifs ( pnal_ipaddr_t ip; pnal_ipaddr_t netmask; pnal_ipaddr_t gateway; - app_netif_namelist_t if_list; - ret = app_get_netif_namelist (netif_list_str, &if_list, PNET_MAX_PORT); + ret = app_get_netif_namelist (netif_list_str, if_list, PNET_MAX_PORT); if (ret != 0) { return ret; } + p_cfg->if_cfg.main_netif_name = if_list->netif[0].name; if (verbosity > 0) { - printf ("Management port: "); - } - if ( - app_port_cfg_init ( - if_list.netif[0].name, - &p_cfg->if_cfg.main_port, - verbosity) != 0) - { - return -1; + printf ("Management port: %s\n", p_cfg->if_cfg.main_netif_name); } for (i = 1; i <= PNET_MAX_PORT; i++) { + p_cfg->if_cfg.physical_ports[i - 1].netif_name = if_list->netif[i].name; if (verbosity > 0) { - printf ("Physical port [%u]: ", i); - } - - if ( - app_port_cfg_init ( - if_list.netif[i].name, - &p_cfg->if_cfg.ports[i - 1].phy_port, - verbosity) != 0) - { - return -1; + printf ( + "Physical port [%u]: %s\n", + i, + p_cfg->if_cfg.physical_ports[i - 1].netif_name); } } /* Read IP, netmask, gateway from operating system */ - ip = pnal_get_ip_address (if_list.netif[0].name); - netmask = pnal_get_netmask (if_list.netif[0].name); - gateway = pnal_get_gateway (if_list.netif[0].name); + ip = pnal_get_ip_address (p_cfg->if_cfg.main_netif_name); + netmask = pnal_get_netmask (p_cfg->if_cfg.main_netif_name); + gateway = pnal_get_gateway (p_cfg->if_cfg.main_netif_name); if (verbosity > 0) { diff --git a/sample_app/sampleapp_common.h b/sample_app/sampleapp_common.h index 16ec7073..bb339e6a 100644 --- a/sample_app/sampleapp_common.h +++ b/sample_app/sampleapp_common.h @@ -242,6 +242,7 @@ typedef struct app_data_obj bool alarm_allowed; pnet_alarm_argument_t alarm_arg; struct cmd_args arguments; + app_netif_namelist_t if_list; uint32_t app_param_1; uint32_t app_param_2; uint8_t inputdata[APP_DATASIZE_INPUT]; @@ -276,7 +277,7 @@ typedef enum app_demo_state * @param outputstring Out: Resulting string buffer. Should have size * PNAL_ETH_ADDRSTR_SIZE. */ -void app_mac_to_string (pnal_ethaddr_t mac, char * outputstring); +void app_mac_to_string (pnet_ethaddr_t mac, char * outputstring); /** * Print out current IP address, netmask and default gateway. @@ -306,6 +307,7 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config); * * @param netif_list_str In: Comma separated list of network interfaces. * Terminated string. + * @param if_list Out: Network interface string storage. * @param p_cfg InOut: p-net configuration * @param verbosity In: Verbosity * @return 0 on success @@ -313,6 +315,7 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config); */ int app_pnet_cfg_init_netifs ( const char * netif_list_str, + app_netif_namelist_t * if_list, pnet_cfg_t * p_cfg, int verbosity); diff --git a/src/common/pf_dcp.c b/src/common/pf_dcp.c index 99a1c7de..480748fc 100644 --- a/src/common/pf_dcp.c +++ b/src/common/pf_dcp.c @@ -187,7 +187,7 @@ static void pf_dcp_responder (pnet_t * net, void * arg, uint32_t current_time) { if (net->dcp_delayed_response_waiting == true) { - if (pf_eth_send (net, net->eth_handle, p_buf) > 0) + if (pf_eth_send (net, net->pf_interface.main_port.handle, p_buf) > 0) { LOG_DEBUG ( PNET_LOG, @@ -1091,7 +1091,7 @@ static int pf_dcp_get_set ( p_dst_dcphdr->data_length = htons (dst_pos - dst_start); p_rsp->len = dst_pos; - if (pf_eth_send (net, net->eth_handle, p_rsp) > 0) + if (pf_eth_send (net, net->pf_interface.main_port.handle, p_rsp) > 0) { LOG_DEBUG ( PF_DCP_LOG, @@ -1366,7 +1366,7 @@ int pf_dcp_hello_req (pnet_t * net) p_dcphdr->data_length = htons (dst_pos - dst_start_pos); p_buf->len = dst_pos; - (void)pf_eth_send (net, net->eth_handle, p_buf); + (void)pf_eth_send (net, net->pf_interface.main_port.handle, p_buf); } pnal_buf_free (p_buf); } diff --git a/src/common/pf_eth.c b/src/common/pf_eth.c index 92c4f5d0..a60dfe4f 100644 --- a/src/common/pf_eth.c +++ b/src/common/pf_eth.c @@ -27,18 +27,65 @@ #ifdef UNIT_TEST #define pnal_eth_init mock_pnal_eth_init #define pnal_eth_send mock_pnal_eth_send +#define pnal_get_macaddress mock_pnal_get_macaddress #endif #include #include "pf_includes.h" +/** + * Init network interface. + * Set name, read mac address, register receive callback. + * + * @param net InOut: The p-net stack instance + * @param netif_name In: Network interface name + * @param eth_receive_type In: Ethernet frame types that shall be received + * by the network interface / port. + * @param netif Out: Initialized network interface. + * @return + */ +static int pf_eth_init_netif ( + pnet_t * net, + const char * netif_name, + pnal_ethertype_t eth_receive_type, + pf_netif_t * netif) +{ + pnal_ethaddr_t pnal_mac_addr; + + snprintf (netif->name, sizeof (netif->name), "%s", netif_name); + + netif->handle = + pnal_eth_init (netif->name, eth_receive_type, pf_eth_recv, (void *)net); + + if (netif->handle == NULL) + { + LOG_ERROR (PNET_LOG, "Failed to init \"%s\"\n", netif_name); + return -1; + } + + if (pnal_get_macaddress (netif_name, &pnal_mac_addr) != 0) + { + LOG_ERROR (PNET_LOG, "Failed read mac address on \"%s\"\n", netif_name); + return -1; + } + + memcpy ( + netif->mac_address.addr, + pnal_mac_addr.addr, + sizeof (netif->mac_address.addr)); + + return 0; +} + int pf_eth_init (pnet_t * net, const pnet_cfg_t * p_cfg) { int port; pf_port_iterator_t port_iterator; pf_port_t * p_port_data; - const pnet_port_cfg_t * p_port_cfg; pnal_ethertype_t main_port_receive_type; +#if PNET_MAX_PORT > 1 + const pnet_port_cfg_t * p_port_cfg; +#endif memset (net->eth_id_map, 0, sizeof (net->eth_id_map)); @@ -49,17 +96,13 @@ int pf_eth_init (pnet_t * net, const pnet_cfg_t * p_cfg) #endif /* Init management port */ - net->eth_handle = pnal_eth_init ( - p_cfg->if_cfg.main_port.if_name, - main_port_receive_type, - pf_eth_recv, - (void *)net); - if (net->eth_handle == NULL) + if ( + pf_eth_init_netif ( + net, + p_cfg->if_cfg.main_netif_name, + main_port_receive_type, + &net->pf_interface.main_port) != 0) { - LOG_ERROR ( - PNET_LOG, - "Failed to init \"%s\"\n", - p_cfg->if_cfg.main_port.if_name); return -1; } @@ -69,27 +112,24 @@ int pf_eth_init (pnet_t * net, const pnet_cfg_t * p_cfg) while (port != 0) { p_port_data = pf_port_get_state (net, port); - p_port_cfg = pf_port_get_config (net, port); #if PNET_MAX_PORT > 1 - p_port_data->eth_handle = pnal_eth_init ( - p_port_cfg->phy_port.if_name, - PNAL_ETHTYPE_LLDP, - pf_eth_recv, - (void *)net); -#else - /* In single port configuration managed port is also physical port 1 */ - p_port_data->eth_handle = net->eth_handle; -#endif + p_port_cfg = pf_port_get_config (net, port); - if (p_port_data->eth_handle == NULL) + if ( + pf_eth_init_netif ( + net, + p_port_cfg->netif_name, + PNAL_ETHTYPE_LLDP, + &p_port_data->netif) != 0) { - LOG_ERROR ( - PNET_LOG, - "Failed to init \"%s\"\n", - p_port_cfg->phy_port.if_name); return -1; } +#else + /* In single port configuration the managed port is also physical port 1 + */ + p_port_data->netif = net->pf_interface.main_port; +#endif port = pf_port_get_next (&port_iterator); } diff --git a/src/common/pf_lldp.c b/src/common/pf_lldp.c index 0706c237..b0026bbe 100644 --- a/src/common/pf_lldp.c +++ b/src/common/pf_lldp.c @@ -319,12 +319,14 @@ static void pf_lldp_add_ttl_tlv (uint8_t * p_buf, uint16_t * p_pos) * Insert the optional Profinet port status TLV into a buffer. * * The port status TLV is mandatory for ProfiNet. - * @param p_port_cfg In: LLDP configuration for this port - * @param p_buf InOut: The buffer. - * @param p_pos InOut: The position in the buffer. + * @param rtclass_2_status In: Use 0. Not supported. + * @param rtclass_3_status In: Use 0. Not supported. + * @param p_buf InOut: The buffer. + * @param p_pos InOut: The position in the buffer. */ static void pf_lldp_add_port_status ( - const pnet_port_cfg_t * p_port_cfg, + uint16_t rtclass_2_status, + uint16_t rtclass_3_status, uint8_t * p_buf, uint16_t * p_pos) { @@ -335,18 +337,8 @@ static void pf_lldp_add_port_status ( PF_FRAME_BUFFER_SIZE, p_buf, p_pos); - pf_put_uint16 ( - true, - p_port_cfg->rtclass_2_status, - PF_FRAME_BUFFER_SIZE, - p_buf, - p_pos); - pf_put_uint16 ( - true, - p_port_cfg->rtclass_3_status, - PF_FRAME_BUFFER_SIZE, - p_buf, - p_pos); + pf_put_uint16 (true, rtclass_2_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos); + pf_put_uint16 (true, rtclass_3_status, PF_FRAME_BUFFER_SIZE, p_buf, p_pos); } /** @@ -659,7 +651,7 @@ void pf_lldp_get_port_id ( int loc_port_num, pf_lldp_port_id_t * p_port_id) { - const pnet_port_cfg_t * p_port_cfg = pf_port_get_config (net, loc_port_num); + const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num); char station_name[PNET_STATION_NAME_MAX_SIZE]; /** Terminated */ const pnet_ethaddr_t * device_mac_address = pf_cmina_get_device_macaddr (net); @@ -672,7 +664,7 @@ void pf_lldp_get_port_id ( p_port_id->string, sizeof (p_port_id->string), "%s", - p_port_cfg->port_name); + p_port_data->port_name); } else /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */ { @@ -691,7 +683,7 @@ void pf_lldp_get_port_id ( p_port_id->string, sizeof (p_port_id->string), "%s.%s", - p_port_cfg->port_name, + p_port_data->port_name, station_name); } @@ -719,13 +711,13 @@ void pf_lldp_get_port_description ( int loc_port_num, pf_lldp_port_description_t * p_port_descr) { - const pnet_port_cfg_t * p_port_cfg = pf_port_get_config (net, loc_port_num); + const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num); snprintf ( p_port_descr->string, sizeof (p_port_descr->string), "%s", - p_port_cfg->phy_port.if_name); + p_port_data->netif.name); p_port_descr->len = strlen (p_port_descr->string); p_port_descr->is_valid = true; } @@ -757,7 +749,7 @@ void pf_lldp_get_management_address ( p_man_address->interface_number.subtype = 2; /* ifIndex */ p_man_address->interface_number.value = - pnal_get_interface_index (net->fspm_cfg.if_cfg.main_port.if_name); + pnal_get_interface_index (net->pf_interface.main_port.name); p_man_address->is_valid = true; } @@ -911,10 +903,10 @@ void pf_lldp_get_link_status ( pf_lldp_link_status_t * p_link_status) { pnal_eth_status_t status; - const pnet_port_cfg_t * p_port_cfg = pf_port_get_config (net, loc_port_num); + const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num); /* TODO: Better error handling */ - if (pnal_eth_get_status (p_port_cfg->phy_port.if_name, &status) != 0) + if (pnal_eth_get_status (p_port_data->netif.name, &status) != 0) { LOG_ERROR ( PF_LLDP_LOG, @@ -1010,7 +1002,7 @@ size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[]) pf_lldp_chassis_id_t chassis_id; pf_lldp_port_id_t port_id; pf_lldp_management_address_t man_address; - const pnet_port_cfg_t * p_port_cfg = pf_port_get_config (net, loc_port_num); + const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num); #if LOG_DEBUG_ENABLED(PF_LLDP_LOG) pnal_ipaddr_t ipaddr = pf_cmina_get_ipaddr (net); @@ -1052,7 +1044,7 @@ size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[]) buf, &pos, lldp_dst_addr, - p_port_cfg->phy_port.eth_addr); + p_port_data->netif.mac_address); /* Add mandatory parts */ pf_lldp_add_chassis_id_tlv (&chassis_id, buf, &pos); @@ -1060,7 +1052,7 @@ size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[]) pf_lldp_add_ttl_tlv (buf, &pos); /* Add optional parts */ - pf_lldp_add_port_status (p_port_cfg, buf, &pos); + pf_lldp_add_port_status (0, 0, buf, &pos); pf_lldp_add_chassis_mac (device_mac_address, buf, &pos); pf_lldp_add_ieee_mac_phy (&link_status, buf, &pos); pf_lldp_add_management (&man_address, buf, &pos); @@ -1091,7 +1083,7 @@ static void pf_lldp_send (pnet_t * net, int loc_port_num) p_buffer->len = pf_lldp_construct_frame (net, loc_port_num, p_buffer->payload); - (void)pf_eth_send (net, p_port_data->eth_handle, p_buffer); + (void)pf_eth_send (net, p_port_data->netif.handle, p_buffer); } pnal_buf_free (p_buffer); diff --git a/src/device/pf_block_writer.c b/src/device/pf_block_writer.c index dd8638b0..291e4976 100644 --- a/src/device/pf_block_writer.c +++ b/src/device/pf_block_writer.c @@ -3578,14 +3578,12 @@ void pf_put_pdport_data_real ( pf_lldp_port_name_t port_name; pnal_eth_status_t eth_status; const uint16_t subslot = pf_port_loc_port_num_to_dap_subslot (loc_port_num); - const pnet_port_cfg_t * p_port_config = - pf_port_get_config (net, loc_port_num); const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num); const pf_lldp_peer_info_t * p_peer_info = &p_port_data->lldp.peer_info; num_peers = p_port_data->lldp.is_peer_info_received ? 1 : 0; - if (pnal_eth_get_status (p_port_config->phy_port.if_name, ð_status) != 0) + if (pnal_eth_get_status (p_port_data->netif.name, ð_status) != 0) { memset (ð_status, 0, sizeof (eth_status)); } @@ -3608,12 +3606,12 @@ void pf_put_pdport_data_real ( pf_put_uint16 (is_big_endian, subslot, res_len, p_bytes, p_pos); /* Length OwnPortName */ - pf_put_byte (strlen (p_port_config->port_name), res_len, p_bytes, p_pos); + pf_put_byte (strlen (p_port_data->port_name), res_len, p_bytes, p_pos); /* OwnPortName */ pf_put_mem ( - p_port_config->port_name, - strlen (p_port_config->port_name), + p_port_data->port_name, + strlen (p_port_data->port_name), res_len, p_bytes, p_pos); @@ -3863,10 +3861,7 @@ static void pf_put_pd_multiblock_interface_and_statistics ( uint16_t block_len = 0; pnal_port_stats_t port_stats; - if ( - pnal_get_port_statistics ( - net->fspm_cfg.if_cfg.main_port.if_name, - &port_stats) != 0) + if (pnal_get_port_statistics (net->pf_interface.main_port.name, &port_stats) != 0) { memset (&port_stats, 0, sizeof (port_stats)); } @@ -3947,9 +3942,9 @@ static void pf_put_pd_multiblock_port_and_statistics ( uint16_t block_len = 0; pnal_port_stats_t port_stats; const uint16_t subslot = pf_port_loc_port_num_to_dap_subslot (loc_port_num); - const pnet_port_cfg_t * p_port_cfg = pf_port_get_config (net, loc_port_num); + const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num); - if (pnal_get_port_statistics (p_port_cfg->phy_port.if_name, &port_stats) != 0) + if (pnal_get_port_statistics (p_port_data->netif.name, &port_stats) != 0) { memset (&port_stats, 0, sizeof (port_stats)); } diff --git a/src/device/pf_cmina.c b/src/device/pf_cmina.c index 6b0f2eb5..46952178 100644 --- a/src/device/pf_cmina.c +++ b/src/device/pf_cmina.c @@ -191,7 +191,7 @@ int pf_cmina_set_default_cfg (pnet_t * net, uint16_t reset_mode) memcpy ( net->cmina_nonvolatile_dcp_ase.mac_address.addr, - p_cfg->if_cfg.main_port.eth_addr.addr, + net->pf_interface.main_port.mac_address.addr, sizeof (pnet_ethaddr_t)); strcpy (net->cmina_nonvolatile_dcp_ase.port_name, ""); /* Terminated */ @@ -420,7 +420,7 @@ void pf_cmina_dcp_set_commit (pnet_t * net) net->cmina_commit_ip_suite = false; res = pnal_set_ip_suite ( - net->fspm_cfg.if_cfg.main_port.if_name, + net->pf_interface.main_port.name, &net->cmina_current_dcp_ase.full_ip_suite.ip_suite.ip_addr, &net->cmina_current_dcp_ase.full_ip_suite.ip_suite.ip_mask, &net->cmina_current_dcp_ase.full_ip_suite.ip_suite.ip_gateway, @@ -1265,7 +1265,7 @@ pnal_ipaddr_t pf_cmina_get_gateway (const pnet_t * net) const pnet_ethaddr_t * pf_cmina_get_device_macaddr (const pnet_t * net) { - return &net->fspm_cfg.if_cfg.main_port.eth_addr; + return &net->pf_interface.main_port.mac_address; } /************************* Utilites ******************************************/ @@ -1344,19 +1344,16 @@ void pf_cmina_port_statistics_show (pnet_t * net) int port; pf_port_iterator_t port_iterator; pnal_port_stats_t stats; - const pnet_port_cfg_t * p_port_config; + pf_port_t * p_port_data; - if ( - pnal_get_port_statistics ( - net->fspm_cfg.if_cfg.main_port.if_name, - &stats) == 0) + if (pnal_get_port_statistics (net->pf_interface.main_port.name, &stats) == 0) { printf ( "Main interface %s In: %" PRIu32 " bytes %" PRIu32 " errors %" PRIu32 " discards Out: %" PRIu32 " bytes %" PRIu32 " errors %" PRIu32 " discards" "s\n", - net->fspm_cfg.if_cfg.main_port.if_name, + net->pf_interface.main_port.name, stats.if_in_octets, stats.if_in_errors, stats.if_in_discards, @@ -1368,23 +1365,23 @@ void pf_cmina_port_statistics_show (pnet_t * net) { printf ( "Did not find main interface %s\n", - net->fspm_cfg.if_cfg.main_port.if_name); + net->pf_interface.main_port.name); } pf_port_init_iterator_over_ports (net, &port_iterator); port = pf_port_get_next (&port_iterator); while (port != 0) { - p_port_config = pf_port_get_config (net, port); + p_port_data = pf_port_get_state (net, port); - if (pnal_get_port_statistics (p_port_config->phy_port.if_name, &stats) == 0) + if (pnal_get_port_statistics (p_port_data->netif.name, &stats) == 0) { printf ( "Port %s In: %" PRIu32 " bytes %" PRIu32 " errors %" PRIu32 " discards Out: %" PRIu32 " bytes %" PRIu32 " errors %" PRIu32 " discards" "s\n", - p_port_config->phy_port.if_name, + p_port_data->netif.name, stats.if_in_octets, stats.if_in_errors, stats.if_in_discards, @@ -1394,7 +1391,7 @@ void pf_cmina_port_statistics_show (pnet_t * net) } else { - printf ("Did not find port %s\n", p_port_config->phy_port.if_name); + printf ("Did not find port %s\n", p_port_data->netif.name); } port = pf_port_get_next (&port_iterator); @@ -1471,12 +1468,12 @@ void pf_cmina_show (pnet_t * net) printf ( "MAC : %02x:%02x:%02x:%02x:%02x:%02x\n", - p_cfg->if_cfg.main_port.eth_addr.addr[0], - p_cfg->if_cfg.main_port.eth_addr.addr[1], - p_cfg->if_cfg.main_port.eth_addr.addr[2], - p_cfg->if_cfg.main_port.eth_addr.addr[3], - p_cfg->if_cfg.main_port.eth_addr.addr[4], - p_cfg->if_cfg.main_port.eth_addr.addr[5]); + net->pf_interface.main_port.mac_address.addr[0], + net->pf_interface.main_port.mac_address.addr[1], + net->pf_interface.main_port.mac_address.addr[2], + net->pf_interface.main_port.mac_address.addr[3], + net->pf_interface.main_port.mac_address.addr[4], + net->pf_interface.main_port.mac_address.addr[5]); pf_cmina_port_statistics_show (net); } diff --git a/src/device/pf_cmrpc.c b/src/device/pf_cmrpc.c index 9f51c4a9..bb306072 100644 --- a/src/device/pf_cmrpc.c +++ b/src/device/pf_cmrpc.c @@ -471,7 +471,7 @@ static int pf_session_allocate (pnet_t * net, pf_session_info_t ** pp_sess) p_sess = &net->cmrpc_session_info[ix]; memset (p_sess, 0, sizeof (*p_sess)); p_sess->in_use = true; - p_sess->eth_handle = net->eth_handle; + p_sess->eth_handle = net->pf_interface.main_port.handle; p_sess->p_ar = NULL; p_sess->sequence_nmb_send = 0; p_sess->dcontrol_sequence_nmb = UINT32_MAX; diff --git a/src/device/pf_cmrpc_epm.c b/src/device/pf_cmrpc_epm.c index 85cb6eef..cc3c0da8 100644 --- a/src/device/pf_cmrpc_epm.c +++ b/src/device/pf_cmrpc_epm.c @@ -161,7 +161,7 @@ static int pf_cmrdr_add_epmv4_entry ( { pf_generate_epm_handle ( os_get_current_time_us(), - net->fspm_cfg.if_cfg.main_port.eth_addr, + net->pf_interface.main_port.mac_address, &p_lookup_rsp->rpc_handle); p_lookup_rsp->num_entry++; @@ -202,7 +202,7 @@ static int pf_cmrdr_add_pnio_entry ( { pf_generate_epm_handle ( os_get_current_time_us(), - net->fspm_cfg.if_cfg.main_port.eth_addr, + net->pf_interface.main_port.mac_address, &p_lookup_rsp->rpc_handle); /*Set the number of entries */ diff --git a/src/device/pf_fspm.c b/src/device/pf_fspm.c index 69ddfb58..b0f8a250 100644 --- a/src/device/pf_fspm.c +++ b/src/device/pf_fspm.c @@ -222,7 +222,7 @@ int pf_fspm_validate_configuration (const pnet_cfg_t * p_cfg) return -1; } - if (strlen (p_cfg->if_cfg.main_port.if_name) == 0) + if (strlen (p_cfg->if_cfg.main_netif_name) == 0) { LOG_ERROR ( PNET_LOG, diff --git a/src/device/pf_pdport.c b/src/device/pf_pdport.c index 8b4c7468..0e5b4c64 100644 --- a/src/device/pf_pdport.c +++ b/src/device/pf_pdport.c @@ -399,7 +399,6 @@ int pf_pdport_read_ind ( pf_port_iterator_t port_iterator; pnal_port_stats_t port_stats; pf_port_t * p_port_data; - const pnet_port_cfg_t * p_port_cfg; uint16_t slot = p_read_req->slot_number; uint16_t subslot = p_read_req->subslot_number; uint16_t index = p_read_req->index; @@ -532,12 +531,12 @@ int pf_pdport_read_ind ( if (loc_port_num == 0) { - netif_name = net->fspm_cfg.if_cfg.main_port.if_name; + netif_name = net->interface.main_port.name; } else { - p_port_cfg = pf_port_get_config (net, loc_port_num); - netif_name = p_port_cfg->phy_port.if_name; + p_port_data = pf_port_get_state (net, loc_port_num); + netif_name = p_port_data->netif.name; } if (pnal_get_port_statistics (netif_name, &port_stats) == 0) diff --git a/src/device/pf_port.c b/src/device/pf_port.c index a43b5db6..71875894 100644 --- a/src/device/pf_port.c +++ b/src/device/pf_port.c @@ -39,6 +39,11 @@ void pf_port_init (pnet_t * net) { p_port_data = pf_port_get_state (net, port); p_port_data->port_num = port; + snprintf ( + p_port_data->port_name, + sizeof (p_port_data->port_name), + "port-%03u", + (uint8_t)port); /* Cast to avoid format-truncation */ port = pf_port_get_next (&port_iterator); } } @@ -95,7 +100,7 @@ pf_port_t * pf_port_get_state (pnet_t * net, int loc_port_num) const pnet_port_cfg_t * pf_port_get_config (pnet_t * net, int loc_port_num) { CC_ASSERT (loc_port_num > 0 && loc_port_num <= PNET_MAX_PORT); - return &net->fspm_cfg.if_cfg.ports[loc_port_num - 1]; + return &net->fspm_cfg.if_cfg.physical_ports[loc_port_num - 1]; } uint16_t pf_port_loc_port_num_to_dap_subslot (int loc_port_num) @@ -132,7 +137,7 @@ int pf_port_get_port_number (pnet_t * net, pnal_eth_handle_t * eth_handle) for (loc_port_num = 1; loc_port_num <= PNET_MAX_PORT; loc_port_num++) { - if (net->pf_interface.port[loc_port_num - 1].eth_handle == eth_handle) + if (net->pf_interface.port[loc_port_num - 1].netif.handle == eth_handle) { return loc_port_num; } diff --git a/src/pf_types.h b/src/pf_types.h index b28a98e8..d0f9bf0d 100644 --- a/src/pf_types.h +++ b/src/pf_types.h @@ -2728,14 +2728,21 @@ typedef struct pf_lldp_port bool is_peer_info_received; } pf_lldp_port_t; +/** Network interface */ +typedef struct pf_netif +{ + char name[PNET_INTERFACE_NAME_MAX_SIZE]; /**< Terminated string */ + pnet_ethaddr_t mac_address; + pnal_eth_handle_t * handle; +} pf_netif_t; + /** * Port runtime data - * Note that physical port configuration - * is part of the configuration (pnet_port_cfg_t) */ typedef struct pf_port { - pnal_eth_handle_t * eth_handle; + pf_netif_t netif; + char port_name[PNET_PORT_NAME_MAX_SIZE]; /* Terminated string */ uint8_t port_num; pf_pdport_t pdport; pf_lldp_port_t lldp; @@ -2757,7 +2764,6 @@ struct pnet uint32_t dcp_sam_timeout; /* Handle to the SAM timeout instance */ uint32_t dcp_identresp_timeout; /* Handle to the DCP identify timeout instance */ - pnal_eth_handle_t * eth_handle; /* Management port */ pf_eth_frame_id_map_t eth_id_map[PF_ETH_MAX_MAP]; volatile pf_scheduler_timeouts_t scheduler_timeouts[PF_MAX_TIMEOUTS]; volatile uint32_t scheduler_timeout_first; @@ -2811,6 +2817,7 @@ struct pnet */ struct { + pf_netif_t main_port; /* Management port */ struct { bool active; diff --git a/src/ports/linux/sampleapp_main.c b/src/ports/linux/sampleapp_main.c index f94b9072..31a95bc2 100644 --- a/src/ports/linux/sampleapp_main.c +++ b/src/ports/linux/sampleapp_main.c @@ -396,6 +396,7 @@ int main (int argc, char * argv[]) ret = app_pnet_cfg_init_netifs ( appdata.arguments.eth_interfaces, + &appdata.if_list, &pnet_default_cfg, appdata.arguments.verbosity); if (ret != 0) diff --git a/src/ports/rt-kernel/sampleapp_main.c b/src/ports/rt-kernel/sampleapp_main.c index 7d81f63f..fa6292ea 100644 --- a/src/ports/rt-kernel/sampleapp_main.c +++ b/src/ports/rt-kernel/sampleapp_main.c @@ -198,6 +198,7 @@ int main (void) ret = app_pnet_cfg_init_netifs ( appdata.arguments.eth_interfaces, + &appdata.if_list, &pnet_default_cfg, appdata.arguments.verbosity); if (ret != 0) diff --git a/test/mocks.cpp b/test/mocks.cpp index 056bf2eb..40a9857b 100644 --- a/test/mocks.cpp +++ b/test/mocks.cpp @@ -117,6 +117,19 @@ int mock_pnal_eth_send (pnal_eth_handle_t * handle, pnal_buf_t * p_buf) return p_buf->len; } +int mock_pnal_get_macaddress ( + const char * interface_name, + pnal_ethaddr_t * p_mac) +{ + p_mac->addr[0] = 0x12; + p_mac->addr[1] = 0x34; + p_mac->addr[2] = 0x00; + p_mac->addr[3] = 0x78; + p_mac->addr[4] = 0x90; + p_mac->addr[5] = 0xab; + return 0; +} + int mock_pnal_udp_open (pnal_ipaddr_t addr, pnal_ipport_t port) { int ret = 2; diff --git a/test/mocks.h b/test/mocks.h index a9372cad..d6ec533d 100644 --- a/test/mocks.h +++ b/test/mocks.h @@ -102,6 +102,9 @@ pnal_eth_handle_t * mock_pnal_eth_init ( pnal_eth_callback_t * callback, void * arg); int mock_pnal_eth_send (pnal_eth_handle_t * handle, pnal_buf_t * buf); +int mock_pnal_get_macaddress ( + const char * interface_name, + pnal_ethaddr_t * p_mac); int mock_pnal_eth_get_status ( const char * interface_name, pnal_eth_status_t * status); diff --git a/test/test_fspm.cpp b/test/test_fspm.cpp index 511b5315..972433a1 100644 --- a/test/test_fspm.cpp +++ b/test/test_fspm.cpp @@ -37,7 +37,7 @@ TEST_F (FspmUnitTest, FspmCheckValidateConfiguration) cfg.tick_us = 1000; cfg.min_device_interval = 1; cfg.im_0_data.im_supported = 0; - strcpy (cfg.if_cfg.main_port.if_name, "eth0"); + cfg.if_cfg.main_netif_name = "eth0"; EXPECT_EQ (pf_fspm_validate_configuration (&cfg), 0); diff --git a/test/utils_for_testing.cpp b/test/utils_for_testing.cpp index ad5b56fd..dbb71be0 100644 --- a/test/utils_for_testing.cpp +++ b/test/utils_for_testing.cpp @@ -493,18 +493,7 @@ void PnetIntegrationTestBase::cfg_init() strcpy (pnet_default_cfg.station_name, ""); strcpy (pnet_default_cfg.product_name, "PNET unit tests"); - strcpy ( - pnet_default_cfg.if_cfg.ports[0].phy_port.if_name, - TEST_INTERFACE_NAME); - strcpy (pnet_default_cfg.if_cfg.ports[0].port_name, "port-001"); - pnet_default_cfg.if_cfg.ports[0].phy_port.eth_addr.addr[0] = 0x12; - pnet_default_cfg.if_cfg.ports[0].phy_port.eth_addr.addr[1] = 0x34; - pnet_default_cfg.if_cfg.ports[0].phy_port.eth_addr.addr[2] = 0x00; - pnet_default_cfg.if_cfg.ports[0].phy_port.eth_addr.addr[3] = 0x78; - pnet_default_cfg.if_cfg.ports[0].phy_port.eth_addr.addr[4] = 0x90; - pnet_default_cfg.if_cfg.ports[0].phy_port.eth_addr.addr[5] = 0xab; - pnet_default_cfg.if_cfg.ports[0].rtclass_2_status = 0; - pnet_default_cfg.if_cfg.ports[0].rtclass_3_status = 0; + pnet_default_cfg.if_cfg.physical_ports[0].netif_name = TEST_INTERFACE_NAME; /* Timing */ pnet_default_cfg.min_device_interval = 32; /* Corresponds to 1 ms */ @@ -512,13 +501,7 @@ void PnetIntegrationTestBase::cfg_init() /* Network configuration */ pnet_default_cfg.send_hello = 1; /* Send HELLO */ pnet_default_cfg.if_cfg.ip_cfg.dhcp_enable = 0; - strcpy (pnet_default_cfg.if_cfg.main_port.if_name, TEST_INTERFACE_NAME); - pnet_default_cfg.if_cfg.main_port.eth_addr.addr[0] = 0x12; - pnet_default_cfg.if_cfg.main_port.eth_addr.addr[1] = 0x34; - pnet_default_cfg.if_cfg.main_port.eth_addr.addr[2] = 0x00; - pnet_default_cfg.if_cfg.main_port.eth_addr.addr[3] = 0x78; - pnet_default_cfg.if_cfg.main_port.eth_addr.addr[4] = 0x90; - pnet_default_cfg.if_cfg.main_port.eth_addr.addr[5] = 0xab; + pnet_default_cfg.if_cfg.main_netif_name = TEST_INTERFACE_NAME; pnet_default_cfg.if_cfg.ip_cfg.ip_addr.a = 192; pnet_default_cfg.if_cfg.ip_cfg.ip_addr.b = 168; pnet_default_cfg.if_cfg.ip_cfg.ip_addr.c = 1; From a58e9ddb3435f8bc9fc087b12ceff7d170d66da8 Mon Sep 17 00:00:00 2001 From: olabjorsne Date: Tue, 9 Feb 2021 15:46:24 +0100 Subject: [PATCH 2/2] Rename PNET_MAX_PORT to PNET_NUMBER_OF_PHYSICAL_PORTS --- CMakeLists.txt | 4 +- doc/multiple_ports.rst | 6 +-- include/pnet_api.h | 2 +- options.h.in | 4 +- ...V2.4-RT-Labs-P-Net-Sample-App-20210202.xml | 2 +- sample_app/sampleapp_common.c | 13 +++-- sample_app/sampleapp_common.h | 26 +++++----- src/common/pf_cpm.c | 2 +- src/common/pf_eth.c | 18 ++----- src/common/pf_eth.h | 8 ++-- src/common/pf_lldp.c | 19 +++++--- src/common/pf_lldp.h | 48 ++++++++++++------- src/common/pf_snmp.c | 2 +- src/common/pf_snmp.h | 39 ++++++++++----- src/device/pf_block_writer.c | 3 +- src/device/pf_block_writer.h | 3 +- src/device/pf_fspm.c | 4 +- src/device/pf_pdport.c | 34 ++++++++----- src/device/pf_pdport.h | 6 ++- src/device/pf_port.c | 26 ++++++---- src/device/pf_port.h | 16 ++++--- src/pf_types.h | 4 +- src/ports/linux/sampleapp_main.c | 8 ++-- src/ports/rt-kernel/mib/lldp-ext-dot3-mib.c | 3 +- src/ports/rt-kernel/mib/lldp-ext-pno-mib.c | 3 +- src/ports/rt-kernel/mib/lldp-mib.c | 6 ++- src/ports/rt-kernel/sampleapp_main.c | 2 +- test/mocks.h | 2 +- test/test_cmrpc.cpp | 2 +- test/test_dcp.cpp | 6 ++- test/test_lldp.cpp | 2 +- test/test_port.cpp | 6 +-- 32 files changed, 194 insertions(+), 135 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 391626f1..5c06cfe5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,12 +60,12 @@ set(PNET_MAX_CR 2 set(PNET_MAX_SLOTS 5 CACHE STRING "Per API. Should be > 1 to allow at least one I/O module") set(PNET_MAX_SUBSLOTS 3 - CACHE STRING "Per slot (DAP requires 2 + PNET_MAX_PORT)") + CACHE STRING "Per slot (DAP requires 2 + PNET_NUMBER_OF_PHYSICAL_PORTS)") set(PNET_MAX_CHANNELS 1 CACHE STRING "Per sub-slot. Used for diagnosis") set(PNET_MAX_DFP_IOCR 2 CACHE STRING "Allowed values are 0 (zero) or 2") -set(PNET_MAX_PORT 1 +set(PNET_NUMBER_OF_PHYSICAL_PORTS 1 CACHE STRING "Number of physical ports") set(PNET_MAX_LOG_BOOK_ENTRIES 16 CACHE STRING "") diff --git a/doc/multiple_ports.rst b/doc/multiple_ports.rst index 293619b9..595880df 100644 --- a/doc/multiple_ports.rst +++ b/doc/multiple_ports.rst @@ -99,10 +99,10 @@ To run p-net and the sample application with multiple ports a couple of things need to be done. Note that the settings described in the following sections are changed by running ``ccmake .`` in the build folder. ``options.h`` will be regenerated. Another way to set the options is to -set them on the cmake command line (-DPNET_MAX_PORT=2 -DPNET_MAX_SUBSLOTS=4). +set them on the cmake command line (-DPNET_NUMBER_OF_PHYSICAL_PORTS=2 -DPNET_MAX_SUBSLOTS=4). -Reconfigure setting ``PNET_MAX_PORT`` to the actual number of physical ports available in the system. -For this example ``PNET_MAX_PORT`` shall be set to 2. +Reconfigure setting ``PNET_NUMBER_OF_PHYSICAL_PORTS`` to the actual number of physical ports available in the system. +For this example ``PNET_NUMBER_OF_PHYSICAL_PORTS`` shall be set to 2. Reconfigure setting ``PNET_MAX_SUBSLOTS``. Each additional port will require an additional subslot. For this example the ``PNET_MAX_SUBSLOTS`` should be be set to 4. diff --git a/include/pnet_api.h b/include/pnet_api.h index 9e9f5308..5c71399c 100644 --- a/include/pnet_api.h +++ b/include/pnet_api.h @@ -1204,7 +1204,7 @@ typedef struct pnet_if_cfg const char * main_netif_name; /**< Main (DAP) network interface. */ pnet_ip_cfg_t ip_cfg; /**< IP Settings for main network interface */ - pnet_port_cfg_t physical_ports[PNET_MAX_PORT]; + pnet_port_cfg_t physical_ports[PNET_NUMBER_OF_PHYSICAL_PORTS]; } pnet_if_cfg_t; /** diff --git a/options.h.in b/options.h.in index fa400557..334d589e 100644 --- a/options.h.in +++ b/options.h.in @@ -112,9 +112,9 @@ #define PNET_MAX_DFP_IOCR @PNET_MAX_DFP_IOCR@ #endif -#if !defined (PNET_MAX_PORT) +#if !defined (PNET_NUMBER_OF_PHYSICAL_PORTS) /** 2 for media redundancy. Currently only 1 is supported. */ -#define PNET_MAX_PORT @PNET_MAX_PORT@ +#define PNET_NUMBER_OF_PHYSICAL_PORTS @PNET_NUMBER_OF_PHYSICAL_PORTS@ #endif #if !defined (PNET_MAX_LOG_BOOK_ENTRIES) diff --git a/sample_app/GSDML-V2.4-RT-Labs-P-Net-Sample-App-20210202.xml b/sample_app/GSDML-V2.4-RT-Labs-P-Net-Sample-App-20210202.xml index 7963a105..abc24a1e 100644 --- a/sample_app/GSDML-V2.4-RT-Labs-P-Net-Sample-App-20210202.xml +++ b/sample_app/GSDML-V2.4-RT-Labs-P-Net-Sample-App-20210202.xml @@ -66,7 +66,7 @@ Current list works for Raspberry Pi, Linksys usb/ethernet dongle and xmc sample