Blame SOURCES/open-lldp-v1.0.1-23-lldp-make-TTL-TLV-configurable.patch

ee47b4
From 986eb2e84fd3339a30a4ab09c6d788c63236fed6 Mon Sep 17 00:00:00 2001
ee47b4
From: Gary Loughnane <gary.loughnane@intel.com>
ee47b4
Date: Tue, 21 Apr 2015 17:45:41 +0000
ee47b4
Subject: [PATCH] lldp: make TTL TLV configurable
ee47b4
ee47b4
This allows the TTL TLV to be configured.
ee47b4
ee47b4
Currently the Time To Live TLV is a static value set to 120 seconds.
ee47b4
LLDP specification says this can be a value of between 0 and 65535
ee47b4
seconds. This patch makes the TTL TLV a configurable entity.
ee47b4
ee47b4
Signed-off-by: Gary Loughnane <gary.loughnane@intel.com>
ee47b4
Signed-off-by: John Fastabend <john.r.fastabend@intel.com>
ee47b4
---
ee47b4
 include/lldp_mand.h      |  1 +
ee47b4
 include/lldp_mand_cmds.h |  4 +++
ee47b4
 lldp/agent.c             |  2 +-
ee47b4
 lldp/ports.c             |  3 +-
ee47b4
 lldp/states.h            |  2 +-
ee47b4
 lldp/tx.c                | 30 ++++++++++++++--
ee47b4
 lldp_mand.c              | 21 ++++++++---
ee47b4
 lldp_mand_cmds.c         | 90 ++++++++++++++++++++++++++++++++++++++++++++++++
ee47b4
 8 files changed, 141 insertions(+), 12 deletions(-)
ee47b4
ee47b4
diff --git a/include/lldp_mand.h b/include/lldp_mand.h
ee47b4
index 9154b7c..fc21ede 100644
ee47b4
--- a/include/lldp_mand.h
ee47b4
+++ b/include/lldp_mand.h
ee47b4
@@ -93,4 +93,5 @@ void mand_unregister(struct lldp_module *mod);
ee47b4
 struct packed_tlv *mand_gettlv(struct port *, struct lldp_agent *);
ee47b4
 void mand_ifdown(char *, struct lldp_agent *);
ee47b4
 void mand_ifup(char *, struct lldp_agent *);
ee47b4
+void mand_update_ttl(const char *, u16);
ee47b4
 #endif /* _LLDP_MAND_H */
ee47b4
diff --git a/include/lldp_mand_cmds.h b/include/lldp_mand_cmds.h
ee47b4
index aacac7c..8effc6e 100644
ee47b4
--- a/include/lldp_mand_cmds.h
ee47b4
+++ b/include/lldp_mand_cmds.h
ee47b4
@@ -28,6 +28,10 @@
ee47b4
 #define _LLDP_MAND_CMDS_H
ee47b4
 
ee47b4
 #define ARG_MAND_SUBTYPE "subtype"
ee47b4
+#define ARG_TTL_VALUE "value"
ee47b4
+
ee47b4
+#define TTL_MIN_VAL 0x0
ee47b4
+#define TTL_MAX_VAL 0xFFFF
ee47b4
 
ee47b4
 struct arg_handlers *mand_get_arg_handlers();
ee47b4
 
ee47b4
diff --git a/lldp/agent.c b/lldp/agent.c
ee47b4
index 4bc5394..73ab054 100644
ee47b4
--- a/lldp/agent.c
ee47b4
+++ b/lldp/agent.c
ee47b4
@@ -101,7 +101,7 @@ void lldp_init_agent(struct port *port, struct lldp_agent *agent, int type)
ee47b4
 
ee47b4
 	/* init TX path */
ee47b4
 	txInitializeTimers(agent);
ee47b4
-	txInitializeLLDP(agent);
ee47b4
+	txInitializeLLDP(port, agent);
ee47b4
 }
ee47b4
 
ee47b4
 int lldp_add_agent(const char *ifname, enum agent_type type)
ee47b4
diff --git a/lldp/ports.c b/lldp/ports.c
ee47b4
index 3bd6a2a..6384f14 100644
ee47b4
--- a/lldp/ports.c
ee47b4
+++ b/lldp/ports.c
ee47b4
@@ -230,7 +230,6 @@ int reinit_port(const char *ifname)
ee47b4
 		/* Reset relevant state variables */
ee47b4
 		agent->tx.state  = TX_LLDP_INITIALIZE;
ee47b4
 		agent->rx.state = LLDP_WAIT_PORT_OPERATIONAL;
ee47b4
-		agent->tx.txTTL = 0;
ee47b4
 		agent->msap.length1 = 0;
ee47b4
 		agent->msap.msap1 = NULL;
ee47b4
 		agent->msap.length2 = 0;
ee47b4
@@ -243,7 +242,7 @@ int reinit_port(const char *ifname)
ee47b4
 
ee47b4
 		/* init TX path */
ee47b4
 		txInitializeTimers(agent);
ee47b4
-		txInitializeLLDP(agent);
ee47b4
+		txInitializeLLDP(port, agent);
ee47b4
 	}
ee47b4
 
ee47b4
 	return 0;
ee47b4
diff --git a/lldp/states.h b/lldp/states.h
ee47b4
index fa5f11f..7cf69b8 100644
ee47b4
--- a/lldp/states.h
ee47b4
+++ b/lldp/states.h
ee47b4
@@ -52,7 +52,7 @@ enum {
ee47b4
  * The txInitializeLLDP () procedure initializes the LLDP transmit module as
ee47b4
  * defined in 10.1.1.
ee47b4
 */
ee47b4
-void txInitializeLLDP(struct lldp_agent *agent);
ee47b4
+void txInitializeLLDP(struct port *port, struct lldp_agent *agent);
ee47b4
 
ee47b4
 /**
ee47b4
  * The mibConstrInfoLLDPDU () procedure constructs an information LLDPDU as
ee47b4
diff --git a/lldp/tx.c b/lldp/tx.c
ee47b4
index 69c1a1a..c3a5c62 100644
ee47b4
--- a/lldp/tx.c
ee47b4
+++ b/lldp/tx.c
ee47b4
@@ -34,6 +34,8 @@
ee47b4
 #include "lldp_tlv.h"
ee47b4
 #include "lldp_mod.h"
ee47b4
 #include "lldp_mand.h"
ee47b4
+#include "config.h"
ee47b4
+#include "lldp_mand_clif.h"
ee47b4
 
ee47b4
 bool mibConstrInfoLLDPDU(struct port *port, struct lldp_agent *agent)
ee47b4
 {
ee47b4
@@ -110,7 +112,29 @@ error:
ee47b4
 	return false;
ee47b4
 }
ee47b4
 
ee47b4
-void txInitializeLLDP(struct lldp_agent *agent)
ee47b4
+static u16 get_ttl_init_val(char *ifname, struct lldp_agent *agent)
ee47b4
+{
ee47b4
+	u16 ttl;
ee47b4
+	int config_ttl;
ee47b4
+	int read_config_err;
ee47b4
+	char arg_path[512] = { 0 };
ee47b4
+
ee47b4
+	snprintf(arg_path, sizeof(arg_path), "%s%08x.%s",
ee47b4
+			TLVID_PREFIX,
ee47b4
+			TLVID_NOUI(TIME_TO_LIVE_TLV),
ee47b4
+			ARG_TTL_VALUE);
ee47b4
+	read_config_err = get_config_setting(ifname, agent->type,
ee47b4
+			arg_path, &config_ttl, CONFIG_TYPE_INT);
ee47b4
+
ee47b4
+	if (read_config_err)
ee47b4
+		ttl = DEFAULT_TX_HOLD * DEFAULT_TX_INTERVAL;
ee47b4
+	else
ee47b4
+		ttl = (u16)(config_ttl);
ee47b4
+
ee47b4
+	return ttl;
ee47b4
+}
ee47b4
+
ee47b4
+void txInitializeLLDP(struct port *port, struct lldp_agent *agent)
ee47b4
 {
ee47b4
 	if (agent->tx.frameout) {
ee47b4
 		free(agent->tx.frameout);
ee47b4
@@ -125,7 +149,7 @@ void txInitializeLLDP(struct lldp_agent *agent)
ee47b4
 	agent->timers.msgTxInterval = DEFAULT_TX_INTERVAL;
ee47b4
 	agent->timers.msgFastTx     = FAST_TX_INTERVAL;
ee47b4
 
ee47b4
-	agent->tx.txTTL = 0;
ee47b4
+	agent->tx.txTTL = get_ttl_init_val(port->ifname, agent);
ee47b4
 	agent->msap.length1 = 0;
ee47b4
 	agent->msap.msap1 = NULL;
ee47b4
 	agent->msap.length2 = 0;
ee47b4
@@ -240,7 +264,7 @@ void run_tx_sm(struct port *port, struct lldp_agent *agent)
ee47b4
 	do {
ee47b4
 		switch(agent->tx.state) {
ee47b4
 		case TX_LLDP_INITIALIZE:
ee47b4
-			txInitializeLLDP(agent);
ee47b4
+			txInitializeLLDP(port, agent);
ee47b4
 			break;
ee47b4
 		case TX_IDLE:
ee47b4
 			process_tx_idle(agent);
ee47b4
diff --git a/lldp_mand.c b/lldp_mand.c
ee47b4
index b269d3f..652a454 100644
ee47b4
--- a/lldp_mand.c
ee47b4
+++ b/lldp_mand.c
ee47b4
@@ -446,11 +446,7 @@ static int mand_bld_ttl_tlv(struct mand_data *md, struct lldp_agent *agent)
ee47b4
 	}
ee47b4
 	memset(tlv->info, 0, tlv->length);
ee47b4
 
ee47b4
-	if (agent->tx.txTTL)
ee47b4
-		ttl = htons(agent->tx.txTTL);
ee47b4
-	else
ee47b4
-		ttl = htons(DEFAULT_TX_HOLD * DEFAULT_TX_INTERVAL);
ee47b4
-
ee47b4
+	ttl = htons(agent->tx.txTTL);
ee47b4
 	memcpy(tlv->info, &ttl, tlv->length);
ee47b4
 	LLDPAD_DBG("%s:%s:done:type=%d length=%d ttl=%d\n", __func__,
ee47b4
 		md->ifname, tlv->type, tlv->length, ntohs(ttl));
ee47b4
@@ -685,3 +681,18 @@ void mand_unregister(struct lldp_module *mod)
ee47b4
 	free(mod);
ee47b4
 	LLDPAD_INFO("%s:done\n", __func__); 
ee47b4
 }
ee47b4
+
ee47b4
+void mand_update_ttl(const char *ifname, u16 ttl_val)
ee47b4
+{
ee47b4
+	struct port *port = port_find_by_ifindex(get_ifidx(ifname));
ee47b4
+	struct lldp_agent *agent;
ee47b4
+
ee47b4
+	if (!port)
ee47b4
+		return;
ee47b4
+
ee47b4
+	LIST_FOREACH(agent, &port->agent_head, entry) {
ee47b4
+		agent->tx.txTTL = ttl_val;
ee47b4
+		agent->tx.localChange = 1;
ee47b4
+		agent->tx.txFast = agent->timers.txFastInit;
ee47b4
+	}
ee47b4
+}
ee47b4
diff --git a/lldp_mand_cmds.c b/lldp_mand_cmds.c
ee47b4
index 532337b..7d24bf8 100644
ee47b4
--- a/lldp_mand_cmds.c
ee47b4
+++ b/lldp_mand_cmds.c
ee47b4
@@ -58,6 +58,11 @@ static int get_mand_subtype(struct cmd *, char *, char *, char *, int);
ee47b4
 static int set_mand_subtype(struct cmd *, char *, char *, char *, int);
ee47b4
 static int test_mand_subtype(struct cmd *, char *, char *, char *, int);
ee47b4
 
ee47b4
+
ee47b4
+static int get_mand_ttl_value(struct cmd *, char *, char *, char *, int);
ee47b4
+static int set_mand_ttl_value(struct cmd *, char *, char *, char *, int);
ee47b4
+static int test_mand_ttl_value(struct cmd *, char *, char *, char *, int);
ee47b4
+
ee47b4
 static struct arg_handlers arg_handlers[] = {
ee47b4
 	{	.arg = ARG_ADMINSTATUS, .arg_class = LLDP_ARG,
ee47b4
 		.handle_get = get_arg_adminstatus,
ee47b4
@@ -72,6 +77,11 @@ static struct arg_handlers arg_handlers[] = {
ee47b4
 		.handle_get = get_mand_subtype,
ee47b4
 		.handle_set = set_mand_subtype,
ee47b4
 		.handle_test = test_mand_subtype, },
ee47b4
+	{	.arg = ARG_TTL_VALUE,
ee47b4
+		.arg_class = TLV_ARG,
ee47b4
+		.handle_get = get_mand_ttl_value,
ee47b4
+		.handle_set = set_mand_ttl_value,
ee47b4
+		.handle_test = test_mand_ttl_value, },
ee47b4
 	{	.arg = 0 }
ee47b4
 };
ee47b4
 
ee47b4
@@ -271,6 +281,86 @@ static int _set_mand_subtype(struct cmd *cmd, char *arg, char *argvalue,
ee47b4
 	return 0;
ee47b4
 }
ee47b4
 
ee47b4
+static int get_mand_ttl_value(struct cmd *cmd, char *arg,
ee47b4
+		UNUSED char *argvalue, char *obuf, int obuf_len)
ee47b4
+{
ee47b4
+	int ttl_val;
ee47b4
+	char string[8], arg_path[256];
ee47b4
+
ee47b4
+	if (cmd->cmd != cmd_gettlv)
ee47b4
+		return cmd_invalid;
ee47b4
+
ee47b4
+	switch (cmd->tlvid) {
ee47b4
+	case TIME_TO_LIVE_TLV:
ee47b4
+		snprintf(arg_path, sizeof(arg_path), "%s%08x.%s",
ee47b4
+			 TLVID_PREFIX, TLVID_NOUI(TIME_TO_LIVE_TLV),
ee47b4
+			 ARG_TTL_VALUE);
ee47b4
+		get_config_setting(cmd->ifname, cmd->type, arg_path,
ee47b4
+				   &ttl_val, CONFIG_TYPE_INT);
ee47b4
+		break;
ee47b4
+	case INVALID_TLVID:
ee47b4
+		return cmd_invalid;
ee47b4
+	default:
ee47b4
+		return cmd_not_applicable;
ee47b4
+	}
ee47b4
+
ee47b4
+	snprintf(string, sizeof(string), "%d", ttl_val);
ee47b4
+	snprintf(obuf, obuf_len, "%02x%s%04x%s",
ee47b4
+		 (unsigned int) strlen(arg), arg,
ee47b4
+		 (unsigned int)strlen(string), string);
ee47b4
+
ee47b4
+	return 0;
ee47b4
+}
ee47b4
+
ee47b4
+static int _set_mand_ttl_value(struct cmd *cmd, char *arg, char *argvalue,
ee47b4
+			     char *obuf, int obuf_len, bool test)
ee47b4
+{
ee47b4
+	int ttl_val;
ee47b4
+	char *end;
ee47b4
+	char arg_path[256];
ee47b4
+
ee47b4
+	if (cmd->cmd != cmd_settlv)
ee47b4
+		return cmd_invalid;
ee47b4
+
ee47b4
+	switch (cmd->tlvid) {
ee47b4
+	case TIME_TO_LIVE_TLV:
ee47b4
+		break;
ee47b4
+	case INVALID_TLVID:
ee47b4
+		return cmd_invalid;
ee47b4
+	default:
ee47b4
+		return cmd_not_applicable;
ee47b4
+	}
ee47b4
+
ee47b4
+	ttl_val = strtoul(argvalue, &end, 0);
ee47b4
+	if ((*end) || (TTL_MIN_VAL > ttl_val) || (ttl_val > TTL_MAX_VAL))
ee47b4
+		return cmd_bad_params;
ee47b4
+
ee47b4
+	if (test)
ee47b4
+		return cmd_success;
ee47b4
+
ee47b4
+	snprintf(arg_path, sizeof(arg_path), "%s%08x.%s", TLVID_PREFIX,
ee47b4
+			cmd->tlvid, arg);
ee47b4
+	snprintf(obuf, obuf_len, "%s=%s\n", arg, argvalue);
ee47b4
+	set_config_setting(cmd->ifname, cmd->type,
ee47b4
+			arg_path, &ttl_val, CONFIG_TYPE_INT);
ee47b4
+
ee47b4
+	mand_update_ttl(cmd->ifname, ttl_val);
ee47b4
+
ee47b4
+	return 0;
ee47b4
+}
ee47b4
+
ee47b4
+static int set_mand_ttl_value(struct cmd *cmd, char *arg, char *argvalue,
ee47b4
+		char *obuf, int obuf_len)
ee47b4
+{
ee47b4
+	return _set_mand_ttl_value(cmd, arg, argvalue, obuf, obuf_len, false);
ee47b4
+}
ee47b4
+
ee47b4
+static int test_mand_ttl_value(struct cmd *cmd, char *arg, char *argvalue,
ee47b4
+		char *obuf, int obuf_len)
ee47b4
+{
ee47b4
+	return _set_mand_ttl_value(cmd, arg, argvalue, obuf, obuf_len, true);
ee47b4
+}
ee47b4
+
ee47b4
 static int set_mand_subtype(struct cmd *cmd, char *arg, char *argvalue,
ee47b4
 			    char *obuf, int obuf_len)
ee47b4
 {
ee47b4
-- 
ee47b4
2.1.0
ee47b4