Skip to content

PD Interface Adjust / LLPD name Of station mode #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/pnet_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1161,14 +1161,14 @@ typedef struct pnet_ethaddr
#define PNET_STATION_NAME_MAX_SIZE (241)

/* Including termination. Standard says 14 (without termination) */
#define PNET_PORT_ID_MAX_SIZE (15)
#define PNET_PORT_NAME_MAX_SIZE (15)

/** Including termination */
#define PNET_LLDP_CHASSIS_ID_MAX_SIZE (PNET_CHASSIS_ID_MAX_SIZE)

/** Including termination */
#define PNET_LLDP_PORT_ID_MAX_SIZE \
(PNET_STATION_NAME_MAX_SIZE + PNET_PORT_ID_MAX_SIZE)
(PNET_STATION_NAME_MAX_SIZE + PNET_PORT_NAME_MAX_SIZE)

/**
* Network interface
Expand All @@ -1185,7 +1185,7 @@ typedef struct pnet_netif
typedef struct pnet_port_cfg
{
pnet_netif_t phy_port;
char port_id[PNET_LLDP_PORT_ID_MAX_SIZE]; /**< Terminated string */
char port_name[PNET_PORT_NAME_MAX_SIZE]; /**< Terminated string */
uint16_t rtclass_2_status;
uint16_t rtclass_3_status;
} pnet_port_cfg_t;
Expand Down Expand Up @@ -1214,7 +1214,7 @@ typedef struct pnet_ip_cfg
typedef struct pnet_if_cfg
{
pnet_netif_t main_port; /**< Main (DAP) network interface. */
pnet_ip_cfg_t ip_cfg; /**< IP Settings for main 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_if_cfg_t;
Expand Down
4 changes: 2 additions & 2 deletions sample_app/sampleapp_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,8 @@ int app_pnet_cfg_init_default (pnet_cfg_t * stack_config)
for (i = 0; i < PNET_MAX_PORT; i++)
{
snprintf (
stack_config->if_cfg.ports[i].port_id,
PNET_LLDP_PORT_ID_MAX_SIZE,
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;
Expand Down
257 changes: 203 additions & 54 deletions src/common/pf_lldp.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#define pnal_eth_get_status mock_pnal_eth_get_status
#endif

#define STRINGIFY(s) STRINGIFIED (s)
#define STRINGIFIED(s) #s

/**
* @file
* @brief Implements Link Layer Discovery Protocol (LLDP), for neighbourhood
Expand Down Expand Up @@ -280,27 +283,23 @@ static void pf_lldp_add_chassis_id_tlv (
/**
* @internal
* Insert the mandatory port_id TLV into a buffer.
* @param p_port_cfg In: LLDP configuration for this port
* @param p_port_id In: Port Id
* @param p_buf InOut: The buffer.
* @param p_pos InOut: The position in the buffer.
*/
static void pf_lldp_add_port_id_tlv (
const pnet_port_cfg_t * p_port_cfg,
const pf_lldp_port_id_t * p_port_id,
uint8_t * p_buf,
uint16_t * p_pos)
{
uint16_t len;

len = (uint16_t)strlen (p_port_cfg->port_id);

pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_PORT_ID, 1 + len);

pf_put_byte (
PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED,
pf_lldp_add_tlv_header (p_buf, p_pos, LLDP_TYPE_PORT_ID, 1 + p_port_id->len);
pf_put_byte (p_port_id->subtype, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
pf_put_mem (
p_port_id->string,
p_port_id->len,
PF_FRAME_BUFFER_SIZE,
p_buf,
p_pos);
pf_put_mem (p_port_cfg->port_id, len, PF_FRAME_BUFFER_SIZE, p_buf, p_pos);
}

/**
Expand Down Expand Up @@ -575,34 +574,42 @@ void pf_lldp_get_chassis_id (pnet_t * net, pf_lldp_chassis_id_t * p_chassis_id)
pf_cmina_get_device_macaddr (net);
char station_name[PNET_STATION_NAME_MAX_SIZE]; /** Terminated */

/* Try to use NameOfStation as Chassis ID.
*
* FIXME: Use of pf_cmina_get_station_name() is not thread-safe.
* Fix this, e.g. using a mutex.
*
* TODO: Add option to use SystemIdentification as Chassis ID.
* See IEC61158-6-10 table 361.
*/
pf_cmina_get_station_name (net, station_name);

p_chassis_id->len = strlen (station_name);
if (p_chassis_id->len == 0 || p_chassis_id->len >= PNET_LLDP_CHASSIS_ID_MAX_SIZE)
if (net->interface.name_of_device_mode == PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY)
{
/* Use the device MAC address */
p_chassis_id->subtype = PF_LLDP_SUBTYPE_MAC;
p_chassis_id->len = sizeof (pnet_ethaddr_t);
memcpy (
p_chassis_id->string,
device_mac_address->addr,
sizeof (pnet_ethaddr_t));
/*
* FIXME: Use of pf_cmina_get_station_name() is not thread-safe.
* Fix this, e.g. using a mutex.
*/
pf_cmina_get_station_name (net, station_name);
p_chassis_id->len = strlen (station_name);

if (
p_chassis_id->len == 0 ||
p_chassis_id->len >= PNET_LLDP_CHASSIS_ID_MAX_SIZE)
{
/* Use the device MAC address */
p_chassis_id->subtype = PF_LLDP_SUBTYPE_MAC;
p_chassis_id->len = sizeof (pnet_ethaddr_t);
memcpy (
p_chassis_id->string,
device_mac_address->addr,
sizeof (pnet_ethaddr_t));
}
else
{
/* Use the station name (NameOfStation/NameOfInterface) */
p_chassis_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
memcpy (p_chassis_id->string, station_name, p_chassis_id->len);
}
}
else
else /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
{
/* Use the locally assigned chassis ID name */
p_chassis_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
memcpy (p_chassis_id->string, station_name, p_chassis_id->len);
p_chassis_id->len = pf_lldp_get_system_description (
net,
p_chassis_id->string,
sizeof (p_chassis_id->string));
}

p_chassis_id->string[p_chassis_id->len] = '\0';
p_chassis_id->is_valid = true;
}
Expand All @@ -627,12 +634,35 @@ void pf_lldp_get_port_id (
pf_lldp_port_id_t * p_port_id)
{
const pnet_port_cfg_t * p_port_cfg = pf_port_get_config (net, loc_port_num);
char station_name[PNET_STATION_NAME_MAX_SIZE]; /** Terminated */

if (net->interface.name_of_device_mode == PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY)
{
snprintf (
p_port_id->string,
sizeof (p_port_id->string),
"%s",
p_port_cfg->port_name);
}
else /* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
{
pf_cmina_get_station_name (net, station_name);
if (strlen (station_name) == 0)
{
pf_lldp_mac_address_to_string (
p_port_cfg->phy_port.eth_addr.addr,
station_name,
sizeof (station_name));
}

snprintf (
p_port_id->string,
sizeof (p_port_id->string),
"%s.%s",
p_port_cfg->port_name,
station_name);
}

snprintf (
p_port_id->string,
sizeof (p_port_id->string),
"%s",
p_port_cfg->port_id);
p_port_id->subtype = PF_LLDP_SUBTYPE_LOCALLY_ASSIGNED;
p_port_id->len = strlen (p_port_id->string);
p_port_id->is_valid = true;
Expand Down Expand Up @@ -719,26 +749,100 @@ int pf_lldp_get_peer_station_name (
int loc_port_num,
pf_lldp_station_name_t * p_station_name)
{
bool is_received;
pf_lldp_chassis_id_t chassis_id;
pf_lldp_port_id_t port_id;
char * p;
bool found = false;

const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
memset (p_station_name, 0, sizeof (*p_station_name));

os_mutex_lock (net->lldp_mutex);
port_id = p_port_data->lldp.peer_info.port_id;
chassis_id = p_port_data->lldp.peer_info.chassis_id;
os_mutex_unlock (net->lldp_mutex);

if (port_id.is_valid)
{
p = strchr (port_id.string, '.');
if (p != NULL)
{
/* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD
* Example: port-001.dut
*/
snprintf (
p_station_name->string,
sizeof (p_station_name->string),
"%s",
p + 1);
found = true;
}
}

if (!found && chassis_id.is_valid)
{
/* PF_LLDP_NAME_OF_DEVICE_MODE_LEGACY */
if (chassis_id.subtype != PF_LLDP_SUBTYPE_MAC)
{
snprintf (
p_station_name->string,
sizeof (p_station_name->string),
"%s",
chassis_id.string);
}
else
{
pf_lldp_mac_address_to_string (
(uint8_t *)chassis_id.string,
p_station_name->string,
sizeof (p_station_name->string));
}
found = true;
}

/* Do not use peer mac address here since its status is unknown. */

/* Assume that peer's NameOfStation is the same as its ChassisId.
* TODO: Find out the correct way to choose what value to return.
* Value should be part of either received Chassis ID or Port ID.
*/
memset (p_station_name, 0, sizeof (*p_station_name));
snprintf (
p_station_name->string,
sizeof (p_station_name->string),
"%s",
p_port_data->lldp.peer_info.chassis_id.string);
p_station_name->len = strlen (p_station_name->string);
is_received = p_port_data->lldp.peer_info.chassis_id.is_valid;

return p_station_name->len ? 0 : -1;
}

int pf_lldp_get_peer_port_name (
pnet_t * net,
int loc_port_num,
pf_lldp_port_name_t * p_port_name)
{
pf_lldp_port_id_t port_id;
uint16_t i;
const pf_port_t * p_port_data = pf_port_get_state (net, loc_port_num);
memset (p_port_name, 0, sizeof (*p_port_name));

os_mutex_lock (net->lldp_mutex);
port_id = p_port_data->lldp.peer_info.port_id;
os_mutex_unlock (net->lldp_mutex);
return is_received ? 0 : -1;

if (port_id.is_valid)
{
for (i = 0; (i < port_id.len) && (port_id.string[i] != '\0') &&
(i < (sizeof (p_port_name->string) - 1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the condition i < (sizeof (p_port_name->string) - 1 introduced?
Given that i < port_id.len, this should always be true

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

port_id.string is 256 characters. I think I risk writing outside port_name->string if the port_id string is not formatted as it should be.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misread it as sizeof (port_id->string) instead of sizeof (p_port_name->string).

i++)
{
if (port_id.string[i] == '.')
{
/* PF_LLDP_NAME_OF_DEVICE_MODE_STANDARD */
break;
}
p_port_name->string[i] = port_id.string[i];
}

p_port_name->len = strlen (p_port_name->string);
}
else
{
return -1;
}

return p_port_name->len ? 0 : -1;
}

void pf_lldp_get_signal_delays (
Expand Down Expand Up @@ -812,6 +916,48 @@ int pf_lldp_get_peer_link_status (
return p_link_status->is_valid ? 0 : -1;
}

size_t pf_lldp_get_system_description (
const pnet_t * net,
char * system_description,
size_t system_description_max_len)
{
return snprintf (
system_description,
system_description_max_len,
/* clang-format off */
"%-" STRINGIFY (PNET_PRODUCT_NAME_MAX_LEN) "s "
"%-" STRINGIFY (PNET_ORDER_ID_MAX_LEN) "s "
"%-" STRINGIFY (PNET_SERIAL_NUMBER_MAX_LEN) "s "
"%5u "
"%c%3u%3u%3u",
/* clang-format on */
net->fspm_cfg.product_name,
net->fspm_cfg.im_0_data.im_order_id,
net->fspm_cfg.im_0_data.im_serial_number,
net->fspm_cfg.im_0_data.im_hardware_revision,
net->fspm_cfg.im_0_data.im_sw_revision_prefix,
net->fspm_cfg.im_0_data.im_sw_revision_functional_enhancement,
net->fspm_cfg.im_0_data.im_sw_revision_bug_fix,
net->fspm_cfg.im_0_data.im_sw_revision_internal_change);
}

void pf_lldp_mac_address_to_string (
const uint8_t * mac,
char * mac_str,
size_t mac_str_max_len)
{
snprintf (
mac_str,
mac_str_max_len,
"%02X-%02X-%02X-%02X-%02X-%02X",
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5]);
}

/**
* Construct Ethernet frame containing LLDP PDU as payload
*
Expand All @@ -830,6 +976,7 @@ size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[])
pf_cmina_get_device_macaddr (net);
pf_lldp_link_status_t link_status;
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);

Expand All @@ -838,7 +985,9 @@ size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[])
char ip_string[PNAL_INET_ADDRSTR_SIZE] = {0}; /** Terminated string */
const char * chassis_id_description = "<MAC address>";
#endif

pf_lldp_get_chassis_id (net, &chassis_id);
pf_lldp_get_port_id (net, loc_port_num, &port_id);
pf_lldp_get_link_status (net, loc_port_num, &link_status);
pf_lldp_get_management_address (net, &man_address);
#if LOG_DEBUG_ENABLED(PF_LLDP_LOG)
Expand All @@ -862,7 +1011,7 @@ size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[])
device_mac_address->addr[5],
ip_string,
chassis_id_description,
p_port_cfg->port_id);
port_id.string);
#endif

pos = 0;
Expand All @@ -875,7 +1024,7 @@ size_t pf_lldp_construct_frame (pnet_t * net, int loc_port_num, uint8_t buf[])

/* Add mandatory parts */
pf_lldp_add_chassis_id_tlv (&chassis_id, buf, &pos);
pf_lldp_add_port_id_tlv (p_port_cfg, buf, &pos);
pf_lldp_add_port_id_tlv (&port_id, buf, &pos);
pf_lldp_add_ttl_tlv (buf, &pos);

/* Add optional parts */
Expand Down
Loading