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

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